1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
19 #include <X11/Xatom.h>
21 #include <X11/Xutil.h>
22 #include <X11/cursorfont.h>
23 #include <X11/keysym.h>
30 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
32 #elif defined(__FreeBSD__) || defined(__DragonFly__)
37 "st-" VERSION ", (c) 2010 st engineers\n" \
38 "usage: st [-t title] [-c class] [-v] [-e cmd]\n"
41 #define ESC_TITLE_SIZ 256
42 #define ESC_BUF_SIZ 256
43 #define ESC_ARG_SIZ 16
44 #define DRAW_BUF_SIZ 1024
47 #define SERRNO strerror(errno)
48 #define MIN(a, b) ((a) < (b) ? (a) : (b))
49 #define MAX(a, b) ((a) < (b) ? (b) : (a))
50 #define LEN(a) (sizeof(a) / sizeof(a[0]))
51 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
52 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
53 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
54 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
55 #define IS_SET(flag) (term.mode & (flag))
56 #define TIMEDIFFERENCE(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
58 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
59 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
60 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
61 CURSOR_SAVE, CURSOR_LOAD };
62 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
63 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
64 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
65 MODE_CRLF=16, MODE_MOUSE=32 };
66 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
67 enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
70 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
73 char c[UTF_SIZ]; /* character code */
74 char mode; /* attribute flags */
75 int fg; /* foreground */
76 int bg; /* background */
77 char state; /* state flags */
83 Glyph attr; /* current char attributes */
89 /* CSI Escape sequence structs */
90 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
92 char buf[ESC_BUF_SIZ]; /* raw string */
93 int len; /* raw string length */
96 int narg; /* nb of args */
100 /* Internal representation of the screen */
102 int row; /* nb row */
103 int col; /* nb col */
104 Line* line; /* screen */
105 Line* alt; /* alternate screen */
106 TCursor c; /* cursor */
107 int top; /* top scroll limit */
108 int bot; /* bottom scroll limit */
109 int mode; /* terminal mode flags */
110 int esc; /* escape state flags */
111 char title[ESC_TITLE_SIZ];
115 /* Purely graphic info */
124 int w; /* window width */
125 int h; /* window height */
126 int bufw; /* pixmap width */
127 int bufh; /* pixmap height */
128 int ch; /* char height */
129 int cw; /* char width */
130 char state; /* focus, redraw, visible */
139 /* Drawing Context */
141 unsigned long col[256];
152 /* TODO: use better name for vars... */
157 struct {int x, y;} b, e;
160 struct timeval tclick1;
161 struct timeval tclick2;
166 static void die(const char*, ...);
167 static void draw(void);
168 static void drawregion(int, int, int, int);
169 static void execsh(void);
170 static void sigchld(int);
171 static void run(void);
173 static void csidump(void);
174 static void csihandle(void);
175 static void csiparse(void);
176 static void csireset(void);
178 static void tclearregion(int, int, int, int);
179 static void tcursor(int);
180 static void tdeletechar(int);
181 static void tdeleteline(int);
182 static void tinsertblank(int);
183 static void tinsertblankline(int);
184 static void tmoveto(int, int);
185 static void tnew(int, int);
186 static void tnewline(int);
187 static void tputtab(void);
188 static void tputc(char*);
189 static void treset(void);
190 static int tresize(int, int);
191 static void tscrollup(int, int);
192 static void tscrolldown(int, int);
193 static void tsetattr(int*, int);
194 static void tsetchar(char*);
195 static void tsetscroll(int, int);
196 static void tswapscreen(void);
198 static void ttynew(void);
199 static void ttyread(void);
200 static void ttyresize(int, int);
201 static void ttywrite(const char *, size_t);
203 static void xdraws(char *, Glyph, int, int, int, int);
204 static void xhints(void);
205 static void xclear(int, int, int, int);
206 static void xdrawcursor(void);
207 static void xinit(void);
208 static void xloadcols(void);
209 static void xseturgency(int);
210 static void xsetsel(char*);
211 static void xresize(int, int);
213 static void expose(XEvent *);
214 static void visibility(XEvent *);
215 static void unmap(XEvent *);
216 static char* kmap(KeySym, unsigned int state);
217 static void kpress(XEvent *);
218 static void resize(XEvent *);
219 static void focus(XEvent *);
220 static void brelease(XEvent *);
221 static void bpress(XEvent *);
222 static void bmotion(XEvent *);
223 static void selnotify(XEvent *);
224 static void selrequest(XEvent *);
226 static void selinit(void);
227 static inline int selected(int, int);
228 static void selcopy(void);
229 static void selpaste();
231 static int utf8decode(char *, long *);
232 static int utf8encode(long *, char *);
233 static int utf8size(char *);
234 static int isfullutf8(char *, int);
236 static void (*handler[LASTEvent])(XEvent *) = {
238 [ConfigureNotify] = resize,
239 [VisibilityNotify] = visibility,
240 [UnmapNotify] = unmap,
244 [MotionNotify] = bmotion,
245 [ButtonPress] = bpress,
246 [ButtonRelease] = brelease,
247 [SelectionNotify] = selnotify,
248 [SelectionRequest] = selrequest,
255 static CSIEscape escseq;
258 static Selection sel;
259 static char **opt_cmd = NULL;
260 static char *opt_title = NULL;
261 static char *opt_class = NULL;
264 utf8decode(char *s, long *u) {
270 if(~c&B7) { /* 0xxxxxxx */
273 } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
274 *u = c&(B4|B3|B2|B1|B0);
276 } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
277 *u = c&(B3|B2|B1|B0);
279 } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
284 for(i=n,++s; i>0; --i,++rtn,++s) {
286 if((c&(B7|B6)) != B7) /* 10xxxxxx */
289 *u |= c&(B5|B4|B3|B2|B1|B0);
291 if((n == 1 && *u < 0x80) ||
292 (n == 2 && *u < 0x800) ||
293 (n == 3 && *u < 0x10000) ||
294 (*u >= 0xD800 && *u <= 0xDFFF))
303 utf8encode(long *u, char *s) {
308 sp = (unsigned char*) s;
311 *sp = uc; /* 0xxxxxxx */
313 } else if(*u < 0x800) {
314 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
316 } else if(uc < 0x10000) {
317 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
319 } else if(uc <= 0x10FFFF) {
320 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
325 for(i=n,++sp; i>0; --i,++sp)
326 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
336 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
337 UTF-8 otherwise return 0 */
339 isfullutf8(char *s, int b) {
340 unsigned char *c1, *c2, *c3;
342 c1 = (unsigned char *) s;
343 c2 = (unsigned char *) ++s;
344 c3 = (unsigned char *) ++s;
347 else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
349 else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
351 ((b == 2) && (*c2&(B7|B6)) == B7)))
353 else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
355 ((b == 2) && (*c2&(B7|B6)) == B7) ||
356 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
364 unsigned char c = *s;
368 else if((c&(B7|B6|B5)) == (B7|B6))
370 else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
378 sel.tclick1.tv_sec = 0;
379 sel.tclick1.tv_usec = 0;
383 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
384 if(sel.xtarget == None)
385 sel.xtarget = XA_STRING;
389 selected(int x, int y) {
390 if(sel.ey == y && sel.by == y) {
391 int bx = MIN(sel.bx, sel.ex);
392 int ex = MAX(sel.bx, sel.ex);
393 return BETWEEN(x, bx, ex);
395 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
396 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
400 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
402 *b = e->xbutton.button;
404 *x = (e->xbutton.x - BORDER)/xw.cw;
405 *y = (e->xbutton.y - BORDER)/xw.ch;
406 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
407 sel.b.y = MIN(sel.by, sel.ey);
408 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
409 sel.e.y = MAX(sel.by, sel.ey);
413 mousereport(XEvent *e) {
414 int x = (e->xbutton.x - BORDER)/xw.cw;
415 int y = (e->xbutton.y - BORDER)/xw.ch;
416 int button = e->xbutton.button;
417 int state = e->xbutton.state;
418 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
420 if(!IS_SET(MODE_MOUSE))
424 if(e->xbutton.type == ButtonRelease || button == AnyButton)
432 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
433 + (state & Mod4Mask ? 8 : 0)
434 + (state & ControlMask ? 16 : 0);
436 ttywrite(buf, sizeof(buf));
443 sel.ex = sel.bx = (e->xbutton.x - BORDER)/xw.cw;
444 sel.ey = sel.by = (e->xbutton.y - BORDER)/xw.ch;
450 int x, y, sz, sl, ls = 0;
455 sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
456 ptr = str = malloc(sz);
457 for(y = 0; y < term.row; y++) {
458 for(x = 0; x < term.col; x++)
459 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
460 sl = utf8size(term.line[y][x].c);
461 memcpy(ptr, term.line[y][x].c, sl);
464 if(ls && y < sel.e.y)
473 selnotify(XEvent *e) {
474 unsigned long nitems;
475 unsigned long ofs, rem;
482 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
483 False, AnyPropertyType, &type, &format,
484 &nitems, &rem, &data)) {
485 fprintf(stderr, "Clipboard allocation failed\n");
488 ttywrite((const char *) data, nitems * format / 8);
490 /* number of 32-bit chunks returned */
491 ofs += nitems * format / 32;
497 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
501 selrequest(XEvent *e) {
502 XSelectionRequestEvent *xsre;
506 xsre = (XSelectionRequestEvent *) e;
507 xev.type = SelectionNotify;
508 xev.requestor = xsre->requestor;
509 xev.selection = xsre->selection;
510 xev.target = xsre->target;
511 xev.time = xsre->time;
515 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
516 if(xsre->target == xa_targets) {
517 /* respond with the supported type */
518 Atom string = sel.xtarget;
519 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
520 XA_ATOM, 32, PropModeReplace,
521 (unsigned char *) &string, 1);
522 xev.property = xsre->property;
523 } else if(xsre->target == sel.xtarget) {
524 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
525 xsre->target, 8, PropModeReplace,
526 (unsigned char *) sel.clip, strlen(sel.clip));
527 xev.property = xsre->property;
530 /* all done, send a notification to the listener */
531 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
532 fprintf(stderr, "Error sending SelectionNotify event\n");
537 /* register the selection for both the clipboard and the primary */
543 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
545 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
546 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
552 brelease(XEvent *e) {
556 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
558 if(sel.bx == sel.ex && sel.by == sel.ey) {
564 gettimeofday(&now, NULL);
566 if(TIMEDIFFERENCE(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
567 /* triple click on the line */
568 sel.b.x = sel.bx = 0;
569 sel.e.x = sel.ex = term.col;
570 sel.b.y = sel.e.y = sel.ey;
572 } else if(TIMEDIFFERENCE(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
573 /* double click to select word */
575 while(term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
576 term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
578 while(term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
579 term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
581 sel.b.y = sel.e.y = sel.ey;
587 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
588 gettimeofday(&sel.tclick1, NULL);
597 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
599 if(oldey != sel.ey || oldex != sel.ex) {
600 int starty = MIN(oldey, sel.ey);
601 int endy = MAX(oldey, sel.ey);
602 drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row));
608 die(const char *errstr, ...) {
611 va_start(ap, errstr);
612 vfprintf(stderr, errstr, ap);
620 char *envshell = getenv("SHELL");
622 DEFAULT(envshell, "sh");
623 putenv("TERM="TNAME);
624 args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
625 execvp(args[0], args);
632 if(waitpid(pid, &stat, 0) < 0)
633 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
635 exit(WEXITSTATUS(stat));
644 /* seems to work fine on linux, openbsd and freebsd */
645 struct winsize w = {term.row, term.col, 0, 0};
646 if(openpty(&m, &s, NULL, NULL, &w) < 0)
647 die("openpty failed: %s\n", SERRNO);
649 switch(pid = fork()) {
651 die("fork failed\n");
654 setsid(); /* create a new process group */
655 dup2(s, STDIN_FILENO);
656 dup2(s, STDOUT_FILENO);
657 dup2(s, STDERR_FILENO);
658 if(ioctl(s, TIOCSCTTY, NULL) < 0)
659 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
667 signal(SIGCHLD, sigchld);
674 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
676 fprintf(stderr, "\n");
681 static char buf[BUFSIZ];
682 static int buflen = 0;
685 int charsize; /* size of utf8 char in bytes */
689 /* append read bytes to unprocessed bytes */
690 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
691 die("Couldn't read from shell: %s\n", SERRNO);
693 /* process every complete utf8 char */
696 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
697 charsize = utf8decode(ptr, &utf8c);
698 utf8encode(&utf8c, s);
704 /* keep any uncomplete utf8 char for the next call */
705 memmove(buf, ptr, buflen);
709 ttywrite(const char *s, size_t n) {
710 if(write(cmdfd, s, n) == -1)
711 die("write error on tty: %s\n", SERRNO);
715 ttyresize(int x, int y) {
720 w.ws_xpixel = w.ws_ypixel = 0;
721 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
722 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
729 if(mode == CURSOR_SAVE)
731 else if(mode == CURSOR_LOAD)
732 term.c = c, tmoveto(c.x, c.y);
741 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
743 term.top = 0, term.bot = term.row - 1;
744 term.mode = MODE_WRAP;
745 tclearregion(0, 0, term.col-1, term.row-1);
749 tnew(int col, int row) {
750 /* set screen size */
751 term.row = row, term.col = col;
752 term.line = malloc(term.row * sizeof(Line));
753 term.alt = malloc(term.row * sizeof(Line));
754 for(row = 0 ; row < term.row; row++) {
755 term.line[row] = malloc(term.col * sizeof(Glyph));
756 term.alt [row] = malloc(term.col * sizeof(Glyph));
764 Line* tmp = term.line;
765 term.line = term.alt;
767 term.mode ^= MODE_ALTSCREEN;
771 tscrolldown(int orig, int n) {
775 LIMIT(n, 0, term.bot-orig+1);
777 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
779 for(i = term.bot; i >= orig+n; i--) {
781 term.line[i] = term.line[i-n];
782 term.line[i-n] = temp;
787 tscrollup(int orig, int n) {
790 LIMIT(n, 0, term.bot-orig+1);
792 tclearregion(0, orig, term.col-1, orig+n-1);
794 for(i = orig; i <= term.bot-n; i++) {
796 term.line[i] = term.line[i+n];
797 term.line[i+n] = temp;
802 tnewline(int first_col) {
805 tscrollup(term.top, 1);
808 tmoveto(first_col ? 0 : term.c.x, y);
814 char *p = escseq.buf;
818 escseq.priv = 1, p++;
820 while(p < escseq.buf+escseq.len) {
822 escseq.arg[escseq.narg] *= 10;
823 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
825 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
836 tmoveto(int x, int y) {
837 LIMIT(x, 0, term.col-1);
838 LIMIT(y, 0, term.row-1);
839 term.c.state &= ~CURSOR_WRAPNEXT;
846 term.line[term.c.y][term.c.x] = term.c.attr;
847 memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
848 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
852 tclearregion(int x1, int y1, int x2, int y2) {
856 temp = x1, x1 = x2, x2 = temp;
858 temp = y1, y1 = y2, y2 = temp;
860 LIMIT(x1, 0, term.col-1);
861 LIMIT(x2, 0, term.col-1);
862 LIMIT(y1, 0, term.row-1);
863 LIMIT(y2, 0, term.row-1);
865 for(y = y1; y <= y2; y++)
866 for(x = x1; x <= x2; x++)
867 term.line[y][x].state = 0;
872 int src = term.c.x + n;
874 int size = term.col - src;
876 if(src >= term.col) {
877 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
880 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
881 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
885 tinsertblank(int n) {
888 int size = term.col - dst;
890 if(dst >= term.col) {
891 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
894 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
895 tclearregion(src, term.c.y, dst - 1, term.c.y);
899 tinsertblankline(int n) {
900 if(term.c.y < term.top || term.c.y > term.bot)
903 tscrolldown(term.c.y, n);
908 if(term.c.y < term.top || term.c.y > term.bot)
911 tscrollup(term.c.y, n);
915 tsetattr(int *attr, int l) {
918 for(i = 0; i < l; i++) {
921 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
922 term.c.attr.fg = DefaultFG;
923 term.c.attr.bg = DefaultBG;
926 term.c.attr.mode |= ATTR_BOLD;
929 term.c.attr.mode |= ATTR_UNDERLINE;
932 term.c.attr.mode |= ATTR_REVERSE;
935 term.c.attr.mode &= ~ATTR_BOLD;
938 term.c.attr.mode &= ~ATTR_UNDERLINE;
941 term.c.attr.mode &= ~ATTR_REVERSE;
944 if(i + 2 < l && attr[i + 1] == 5) {
946 if(BETWEEN(attr[i], 0, 255))
947 term.c.attr.fg = attr[i];
949 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
952 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
955 term.c.attr.fg = DefaultFG;
958 if(i + 2 < l && attr[i + 1] == 5) {
960 if(BETWEEN(attr[i], 0, 255))
961 term.c.attr.bg = attr[i];
963 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
966 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
969 term.c.attr.bg = DefaultBG;
972 if(BETWEEN(attr[i], 30, 37))
973 term.c.attr.fg = attr[i] - 30;
974 else if(BETWEEN(attr[i], 40, 47))
975 term.c.attr.bg = attr[i] - 40;
976 else if(BETWEEN(attr[i], 90, 97))
977 term.c.attr.fg = attr[i] - 90 + 8;
978 else if(BETWEEN(attr[i], 100, 107))
979 term.c.attr.fg = attr[i] - 100 + 8;
981 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
989 tsetscroll(int t, int b) {
992 LIMIT(t, 0, term.row-1);
993 LIMIT(b, 0, term.row-1);
1005 switch(escseq.mode) {
1008 fprintf(stderr, "erresc: unknown csi ");
1012 case '@': /* ICH -- Insert <n> blank char */
1013 DEFAULT(escseq.arg[0], 1);
1014 tinsertblank(escseq.arg[0]);
1016 case 'A': /* CUU -- Cursor <n> Up */
1018 DEFAULT(escseq.arg[0], 1);
1019 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
1021 case 'B': /* CUD -- Cursor <n> Down */
1022 DEFAULT(escseq.arg[0], 1);
1023 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
1025 case 'C': /* CUF -- Cursor <n> Forward */
1027 DEFAULT(escseq.arg[0], 1);
1028 tmoveto(term.c.x+escseq.arg[0], term.c.y);
1030 case 'D': /* CUB -- Cursor <n> Backward */
1031 DEFAULT(escseq.arg[0], 1);
1032 tmoveto(term.c.x-escseq.arg[0], term.c.y);
1034 case 'E': /* CNL -- Cursor <n> Down and first col */
1035 DEFAULT(escseq.arg[0], 1);
1036 tmoveto(0, term.c.y+escseq.arg[0]);
1038 case 'F': /* CPL -- Cursor <n> Up and first col */
1039 DEFAULT(escseq.arg[0], 1);
1040 tmoveto(0, term.c.y-escseq.arg[0]);
1042 case 'G': /* CHA -- Move to <col> */
1043 case '`': /* XXX: HPA -- same? */
1044 DEFAULT(escseq.arg[0], 1);
1045 tmoveto(escseq.arg[0]-1, term.c.y);
1047 case 'H': /* CUP -- Move to <row> <col> */
1048 case 'f': /* XXX: HVP -- same? */
1049 DEFAULT(escseq.arg[0], 1);
1050 DEFAULT(escseq.arg[1], 1);
1051 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
1053 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1054 case 'J': /* ED -- Clear screen */
1055 switch(escseq.arg[0]) {
1057 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1058 if(term.c.y < term.row-1)
1059 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1063 tclearregion(0, 0, term.col-1, term.c.y-1);
1064 tclearregion(0, term.c.y, term.c.x, term.c.y);
1067 tclearregion(0, 0, term.col-1, term.row-1);
1073 case 'K': /* EL -- Clear line */
1074 switch(escseq.arg[0]) {
1076 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1079 tclearregion(0, term.c.y, term.c.x, term.c.y);
1082 tclearregion(0, term.c.y, term.col-1, term.c.y);
1086 case 'S': /* SU -- Scroll <n> line up */
1087 DEFAULT(escseq.arg[0], 1);
1088 tscrollup(term.top, escseq.arg[0]);
1090 case 'T': /* SD -- Scroll <n> line down */
1091 DEFAULT(escseq.arg[0], 1);
1092 tscrolldown(term.top, escseq.arg[0]);
1094 case 'L': /* IL -- Insert <n> blank lines */
1095 DEFAULT(escseq.arg[0], 1);
1096 tinsertblankline(escseq.arg[0]);
1098 case 'l': /* RM -- Reset Mode */
1100 switch(escseq.arg[0]) {
1102 term.mode &= ~MODE_APPKEYPAD;
1104 case 5: /* TODO: DECSCNM -- Remove reverse video */
1107 term.mode &= ~MODE_WRAP;
1109 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1112 term.mode &= ~MODE_CRLF;
1115 term.c.state |= CURSOR_HIDE;
1117 case 1000: /* disable X11 xterm mouse reporting */
1118 term.mode &= ~MODE_MOUSE;
1120 case 1049: /* = 1047 and 1048 */
1122 if(IS_SET(MODE_ALTSCREEN)) {
1123 tclearregion(0, 0, term.col-1, term.row-1);
1126 if(escseq.arg[0] == 1047)
1129 tcursor(CURSOR_LOAD);
1135 switch(escseq.arg[0]) {
1137 term.mode &= ~MODE_INSERT;
1144 case 'M': /* DL -- Delete <n> lines */
1145 DEFAULT(escseq.arg[0], 1);
1146 tdeleteline(escseq.arg[0]);
1148 case 'X': /* ECH -- Erase <n> char */
1149 DEFAULT(escseq.arg[0], 1);
1150 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
1152 case 'P': /* DCH -- Delete <n> char */
1153 DEFAULT(escseq.arg[0], 1);
1154 tdeletechar(escseq.arg[0]);
1156 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1157 case 'd': /* VPA -- Move to <row> */
1158 DEFAULT(escseq.arg[0], 1);
1159 tmoveto(term.c.x, escseq.arg[0]-1);
1161 case 'h': /* SM -- Set terminal mode */
1163 switch(escseq.arg[0]) {
1165 term.mode |= MODE_APPKEYPAD;
1167 case 5: /* DECSCNM -- Reverve video */
1168 /* TODO: set REVERSE on the whole screen (f) */
1171 term.mode |= MODE_WRAP;
1174 term.mode |= MODE_CRLF;
1176 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1177 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1178 if(escseq.narg > 1 && escseq.arg[1] != 25)
1181 term.c.state &= ~CURSOR_HIDE;
1183 case 1000: /* enable X11 xterm mouse reporting */
1184 term.mode |= MODE_MOUSE;
1186 case 1049: /* = 1047 and 1048 */
1188 if(IS_SET(MODE_ALTSCREEN))
1189 tclearregion(0, 0, term.col-1, term.row-1);
1192 if(escseq.arg[0] == 1047)
1195 tcursor(CURSOR_SAVE);
1197 default: goto unknown;
1200 switch(escseq.arg[0]) {
1202 term.mode |= MODE_INSERT;
1204 default: goto unknown;
1208 case 'm': /* SGR -- Terminal attribute (color) */
1209 tsetattr(escseq.arg, escseq.narg);
1211 case 'r': /* DECSTBM -- Set Scrolling Region */
1215 DEFAULT(escseq.arg[0], 1);
1216 DEFAULT(escseq.arg[1], term.row);
1217 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
1221 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1222 tcursor(CURSOR_SAVE);
1224 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1225 tcursor(CURSOR_LOAD);
1233 printf("ESC [ %s", escseq.priv ? "? " : "");
1235 for(i = 0; i < escseq.narg; i++)
1236 printf("%d ", escseq.arg[i]);
1238 putchar(escseq.mode);
1244 memset(&escseq, 0, sizeof(escseq));
1249 int space = TAB - term.c.x % TAB;
1250 tmoveto(term.c.x + space, term.c.y);
1256 if(term.esc & ESC_START) {
1257 if(term.esc & ESC_CSI) {
1258 escseq.buf[escseq.len++] = ascii;
1259 if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
1261 csiparse(), csihandle();
1263 /* TODO: handle other OSC */
1264 } else if(term.esc & ESC_OSC) {
1267 term.esc = ESC_START | ESC_TITLE;
1269 } else if(term.esc & ESC_TITLE) {
1270 if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
1272 term.title[term.titlelen] = '\0';
1273 XStoreName(xw.dpy, xw.win, term.title);
1275 term.title[term.titlelen++] = ascii;
1277 } else if(term.esc & ESC_ALTCHARSET) {
1279 case '0': /* Line drawing crap */
1280 term.c.attr.mode |= ATTR_GFX;
1282 case 'B': /* Back to regular text */
1283 term.c.attr.mode &= ~ATTR_GFX;
1286 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1292 term.esc |= ESC_CSI;
1295 term.esc |= ESC_OSC;
1298 term.esc |= ESC_ALTCHARSET;
1300 case 'D': /* IND -- Linefeed */
1301 if(term.c.y == term.bot)
1302 tscrollup(term.top, 1);
1304 tmoveto(term.c.x, term.c.y+1);
1307 case 'E': /* NEL -- Next line */
1308 tnewline(1); /* always go to first col */
1311 case 'M': /* RI -- Reverse index */
1312 if(term.c.y == term.top)
1313 tscrolldown(term.top, 1);
1315 tmoveto(term.c.x, term.c.y-1);
1318 case 'c': /* RIS -- Reset to inital state */
1322 case '=': /* DECPAM -- Application keypad */
1323 term.mode |= MODE_APPKEYPAD;
1326 case '>': /* DECPNM -- Normal keypad */
1327 term.mode &= ~MODE_APPKEYPAD;
1330 case '7': /* DECSC -- Save Cursor */
1331 tcursor(CURSOR_SAVE);
1334 case '8': /* DECRC -- Restore Cursor */
1335 tcursor(CURSOR_LOAD);
1339 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1340 (unsigned char) ascii, isprint(ascii)?ascii:'.');
1350 tmoveto(term.c.x-1, term.c.y);
1353 tmoveto(0, term.c.y);
1358 /* go to first col if the mode is set */
1359 tnewline(IS_SET(MODE_CRLF));
1362 if(!(xw.state & WIN_FOCUSED))
1367 term.esc = ESC_START;
1370 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1371 tnewline(1); /* always go to first col */
1373 if(term.c.x+1 < term.col)
1374 tmoveto(term.c.x+1, term.c.y);
1376 term.c.state |= CURSOR_WRAPNEXT;
1382 tresize(int col, int row) {
1384 int minrow = MIN(row, term.row);
1385 int mincol = MIN(col, term.col);
1386 int slide = term.c.y - row + 1;
1388 if(col < 1 || row < 1)
1391 /* free unneeded rows */
1394 /* slide screen to keep cursor where we expect it -
1395 * tscrollup would work here, but we can optimize to
1396 * memmove because we're freeing the earlier lines */
1397 for(/* i = 0 */; i < slide; i++) {
1401 memmove(term.line, term.line + slide, row * sizeof(Line));
1402 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1404 for(i += row; i < term.row; i++) {
1409 /* resize to new height */
1410 term.line = realloc(term.line, row * sizeof(Line));
1411 term.alt = realloc(term.alt, row * sizeof(Line));
1413 /* resize each row to new width, zero-pad if needed */
1414 for(i = 0; i < minrow; i++) {
1415 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1416 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1417 for(x = mincol; x < col; x++) {
1418 term.line[i][x].state = 0;
1419 term.alt[i][x].state = 0;
1423 /* allocate any new rows */
1424 for(/* i == minrow */; i < row; i++) {
1425 term.line[i] = calloc(col, sizeof(Glyph));
1426 term.alt [i] = calloc(col, sizeof(Glyph));
1429 /* update terminal size */
1430 term.col = col, term.row = row;
1431 /* make use of the LIMIT in tmoveto */
1432 tmoveto(term.c.x, term.c.y);
1433 /* reset scrolling region */
1434 tsetscroll(0, row-1);
1439 xresize(int col, int row) {
1445 xw.bufw = MAX(1, col * xw.cw);
1446 xw.bufh = MAX(1, row * xw.ch);
1447 newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1448 XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
1449 XFreePixmap(xw.dpy, xw.buf);
1450 XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
1452 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
1453 xw.bufw-oldw, MIN(xw.bufh, oldh));
1454 else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
1455 XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
1456 xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
1459 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
1460 xw.bufw, xw.bufh-oldh);
1461 else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
1462 XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
1463 xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
1472 unsigned long white = WhitePixel(xw.dpy, xw.scr);
1474 for(i = 0; i < 16; i++) {
1475 if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1477 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1479 dc.col[i] = color.pixel;
1482 /* same colors as xterm */
1483 for(r = 0; r < 6; r++)
1484 for(g = 0; g < 6; g++)
1485 for(b = 0; b < 6; b++) {
1486 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1487 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1488 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1489 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1491 fprintf(stderr, "Could not allocate color %d\n", i);
1493 dc.col[i] = color.pixel;
1497 for(r = 0; r < 24; r++, i++) {
1498 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1499 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1501 fprintf(stderr, "Could not allocate color %d\n", i);
1503 dc.col[i] = color.pixel;
1508 xclear(int x1, int y1, int x2, int y2) {
1509 XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
1510 XFillRectangle(xw.dpy, xw.buf, dc.gc,
1511 x1 * xw.cw, y1 * xw.ch,
1512 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1517 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1518 XWMHints wm = {.flags = InputHint, .input = 1};
1520 .flags = PSize | PResizeInc | PBaseSize,
1523 .height_inc = xw.ch,
1525 .base_height = 2*BORDER,
1526 .base_width = 2*BORDER,
1528 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1532 xinitfont(char *fontstr) {
1534 char *def, **missing;
1538 set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1541 fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1542 XFreeStringList(missing);
1548 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
1549 XFontStruct **xfonts;
1553 *ascent = *descent = *lbearing = *rbearing = 0;
1554 n = XFontsOfFontSet(set, &xfonts, &font_names);
1555 for(i = 0; i < n; i++) {
1556 *ascent = MAX(*ascent, (*xfonts)->ascent);
1557 *descent = MAX(*descent, (*xfonts)->descent);
1558 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1559 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1565 initfonts(char *fontstr, char *bfontstr) {
1566 if((dc.font.set = xinitfont(fontstr)) == NULL ||
1567 (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1568 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1569 xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1570 &dc.font.lbearing, &dc.font.rbearing);
1571 xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1572 &dc.bfont.lbearing, &dc.bfont.rbearing);
1577 XSetWindowAttributes attrs;
1580 if(!(xw.dpy = XOpenDisplay(NULL)))
1581 die("Can't open display\n");
1582 xw.scr = XDefaultScreen(xw.dpy);
1585 initfonts(FONT, BOLDFONT);
1587 /* XXX: Assuming same size for bold font */
1588 xw.cw = dc.font.rbearing - dc.font.lbearing;
1589 xw.ch = dc.font.ascent + dc.font.descent;
1592 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1595 /* window - default size */
1596 xw.bufh = 24 * xw.ch;
1597 xw.bufw = 80 * xw.cw;
1598 xw.h = xw.bufh + 2*BORDER;
1599 xw.w = xw.bufw + 2*BORDER;
1601 attrs.background_pixel = dc.col[DefaultBG];
1602 attrs.border_pixel = dc.col[DefaultBG];
1603 attrs.bit_gravity = NorthWestGravity;
1604 attrs.event_mask = FocusChangeMask | KeyPressMask
1605 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1606 | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
1607 attrs.colormap = xw.cmap;
1609 xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
1610 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1611 XDefaultVisual(xw.dpy, xw.scr),
1612 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1615 xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1619 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1620 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1621 | XIMStatusNothing, XNClientWindow, xw.win,
1622 XNFocusWindow, xw.win, NULL);
1624 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1626 /* white cursor, black outline */
1627 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
1628 XDefineCursor(xw.dpy, xw.win, cursor);
1629 XRecolorCursor(xw.dpy, cursor,
1630 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
1631 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
1633 XMapWindow(xw.dpy, xw.win);
1635 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1640 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1641 unsigned long xfg, xbg;
1642 int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1645 if(base.mode & ATTR_REVERSE)
1646 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1648 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1650 XSetBackground(xw.dpy, dc.gc, xbg);
1651 XSetForeground(xw.dpy, dc.gc, xfg);
1653 if(base.mode & ATTR_GFX) {
1654 for(i = 0; i < bytelen; i++) {
1655 char c = gfx[(unsigned int)s[i] % 256];
1658 else if(s[i] > 0x5f)
1663 XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
1664 dc.gc, winx, winy, s, bytelen);
1666 if(base.mode & ATTR_UNDERLINE)
1667 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1672 static int oldx = 0;
1673 static int oldy = 0;
1675 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
1677 LIMIT(oldx, 0, term.col-1);
1678 LIMIT(oldy, 0, term.row-1);
1680 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1681 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
1683 /* remove the old cursor */
1684 if(term.line[oldy][oldx].state & GLYPH_SET) {
1685 sl = utf8size(term.line[oldy][oldx].c);
1686 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
1688 xclear(oldx, oldy, oldx, oldy);
1690 /* draw the new one */
1691 if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
1693 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
1694 oldx = term.c.x, oldy = term.c.y;
1700 drawregion(0, 0, term.col, term.row);
1704 drawregion(int x1, int y1, int x2, int y2) {
1705 int ic, ib, x, y, ox, sl;
1707 char buf[DRAW_BUF_SIZ];
1709 if(!(xw.state & WIN_VISIBLE))
1712 xclear(x1, y1, x2-1, y2-1);
1713 for(y = y1; y < y2; y++) {
1714 base = term.line[y][0];
1716 for(x = x1; x < x2; x++) {
1717 new = term.line[y][x];
1718 if(sel.bx != -1 && *(new.c) && selected(x, y))
1719 new.mode ^= ATTR_REVERSE;
1720 if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1721 ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
1722 xdraws(buf, base, ox, y, ic, ib);
1725 if(new.state & GLYPH_SET) {
1730 sl = utf8size(new.c);
1731 memcpy(buf+ib, new.c, sl);
1737 xdraws(buf, base, ox, y, ic, ib);
1740 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1744 expose(XEvent *ev) {
1745 XExposeEvent *e = &ev->xexpose;
1746 if(xw.state & WIN_REDRAW) {
1748 xw.state &= ~WIN_REDRAW;
1752 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
1753 e->width, e->height, e->x, e->y);
1757 visibility(XEvent *ev) {
1758 XVisibilityEvent *e = &ev->xvisibility;
1759 if(e->state == VisibilityFullyObscured)
1760 xw.state &= ~WIN_VISIBLE;
1761 else if(!(xw.state & WIN_VISIBLE))
1762 /* need a full redraw for next Expose, not just a buf copy */
1763 xw.state |= WIN_VISIBLE | WIN_REDRAW;
1768 xw.state &= ~WIN_VISIBLE;
1772 xseturgency(int add) {
1773 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1774 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1775 XSetWMHints(xw.dpy, xw.win, h);
1781 if(ev->type == FocusIn) {
1782 xw.state |= WIN_FOCUSED;
1785 xw.state &= ~WIN_FOCUSED;
1790 kmap(KeySym k, unsigned int state) {
1792 for(i = 0; i < LEN(key); i++)
1793 if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state))
1794 return (char*)key[i].s;
1799 kpress(XEvent *ev) {
1800 XKeyEvent *e = &ev->xkey;
1809 meta = e->state & Mod1Mask;
1810 shift = e->state & ShiftMask;
1811 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
1813 /* 1. custom keys from config.h */
1814 if((customkey = kmap(ksym, e->state)))
1815 ttywrite(customkey, strlen(customkey));
1816 /* 2. hardcoded (overrides X lookup) */
1823 /* XXX: shift up/down doesn't work */
1824 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
1832 if(IS_SET(MODE_CRLF))
1833 ttywrite("\r\n", 2);
1840 if(meta && len == 1)
1841 ttywrite("\033", 1);
1852 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1855 xw.w = e->xconfigure.width;
1856 xw.h = e->xconfigure.height;
1857 col = (xw.w - 2*BORDER) / xw.cw;
1858 row = (xw.h - 2*BORDER) / xw.ch;
1859 if(col == term.col && row == term.row)
1861 if(tresize(col, row))
1863 ttyresize(col, row);
1871 int xfd = XConnectionNumber(xw.dpy);
1875 FD_SET(cmdfd, &rfd);
1877 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1880 die("select failed: %s\n", SERRNO);
1882 if(FD_ISSET(cmdfd, &rfd)) {
1886 while(XPending(xw.dpy)) {
1887 XNextEvent(xw.dpy, &ev);
1888 if(XFilterEvent(&ev, xw.win))
1890 if(handler[ev.type])
1891 (handler[ev.type])(&ev);
1897 main(int argc, char *argv[]) {
1900 for(i = 1; i < argc; i++) {
1901 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
1903 if(++i < argc) opt_title = argv[i];
1906 if(++i < argc) opt_class = argv[i];
1909 if(++i < argc) opt_cmd = &argv[i];
1915 /* -e eats every remaining arguments */
1919 setlocale(LC_CTYPE, "");