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-2011 st engineers\n" \
38 "usage: st [-t title] [-c class] [-v] [-e command...]\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 TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
57 #define X2COL(x) (((x) - BORDER)/xw.cw)
58 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
60 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
61 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
62 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
63 CURSOR_SAVE, CURSOR_LOAD };
64 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
65 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
66 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
67 MODE_CRLF=16, MODE_MOUSEBTN=32, MODE_MOUSEMOTION=64, MODE_MOUSE=32|64, MODE_REVERSE=128 };
68 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
69 enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
72 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
75 char c[UTF_SIZ]; /* character code */
76 char mode; /* attribute flags */
77 int fg; /* foreground */
78 int bg; /* background */
79 char state; /* state flags */
85 Glyph attr; /* current char attributes */
91 /* CSI Escape sequence structs */
92 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
94 char buf[ESC_BUF_SIZ]; /* raw string */
95 int len; /* raw string length */
98 int narg; /* nb of args */
102 /* Internal representation of the screen */
104 int row; /* nb row */
105 int col; /* nb col */
106 Line* line; /* screen */
107 Line* alt; /* alternate screen */
108 TCursor c; /* cursor */
109 int top; /* top scroll limit */
110 int bot; /* bottom scroll limit */
111 int mode; /* terminal mode flags */
112 int esc; /* escape state flags */
113 char title[ESC_TITLE_SIZ];
117 /* Purely graphic info */
126 int w; /* window width */
127 int h; /* window height */
128 int bufw; /* pixmap width */
129 int bufh; /* pixmap height */
130 int ch; /* char height */
131 int cw; /* char width */
132 char state; /* focus, redraw, visible */
141 /* Drawing Context */
143 unsigned long col[256];
154 /* TODO: use better name for vars... */
159 struct {int x, y;} b, e;
162 struct timeval tclick1;
163 struct timeval tclick2;
168 static void die(const char*, ...);
169 static void draw(void);
170 static void drawregion(int, int, int, int);
171 static void execsh(void);
172 static void sigchld(int);
173 static void run(void);
175 static void csidump(void);
176 static void csihandle(void);
177 static void csiparse(void);
178 static void csireset(void);
180 static void tclearregion(int, int, int, int);
181 static void tcursor(int);
182 static void tdeletechar(int);
183 static void tdeleteline(int);
184 static void tinsertblank(int);
185 static void tinsertblankline(int);
186 static void tmoveto(int, int);
187 static void tnew(int, int);
188 static void tnewline(int);
189 static void tputtab(void);
190 static void tputc(char*);
191 static void treset(void);
192 static int tresize(int, int);
193 static void tscrollup(int, int);
194 static void tscrolldown(int, int);
195 static void tsetattr(int*, int);
196 static void tsetchar(char*);
197 static void tsetscroll(int, int);
198 static void tswapscreen(void);
200 static void ttynew(void);
201 static void ttyread(void);
202 static void ttyresize(int, int);
203 static void ttywrite(const char *, size_t);
205 static void xdraws(char *, Glyph, int, int, int, int);
206 static void xhints(void);
207 static void xclear(int, int, int, int);
208 static void xdrawcursor(void);
209 static void xinit(void);
210 static void xloadcols(void);
211 static void xseturgency(int);
212 static void xsetsel(char*);
213 static void xresize(int, int);
215 static void expose(XEvent *);
216 static void visibility(XEvent *);
217 static void unmap(XEvent *);
218 static char* kmap(KeySym, unsigned int state);
219 static void kpress(XEvent *);
220 static void resize(XEvent *);
221 static void focus(XEvent *);
222 static void brelease(XEvent *);
223 static void bpress(XEvent *);
224 static void bmotion(XEvent *);
225 static void selnotify(XEvent *);
226 static void selrequest(XEvent *);
228 static void selinit(void);
229 static inline int selected(int, int);
230 static void selcopy(void);
231 static void selpaste();
233 static int utf8decode(char *, long *);
234 static int utf8encode(long *, char *);
235 static int utf8size(char *);
236 static int isfullutf8(char *, int);
238 static void (*handler[LASTEvent])(XEvent *) = {
240 [ConfigureNotify] = resize,
241 [VisibilityNotify] = visibility,
242 [UnmapNotify] = unmap,
246 [MotionNotify] = bmotion,
247 [ButtonPress] = bpress,
248 [ButtonRelease] = brelease,
249 [SelectionNotify] = selnotify,
250 [SelectionRequest] = selrequest,
257 static CSIEscape escseq;
260 static Selection sel;
261 static char **opt_cmd = NULL;
262 static char *opt_title = NULL;
263 static char *opt_class = NULL;
266 utf8decode(char *s, long *u) {
272 if(~c&B7) { /* 0xxxxxxx */
275 } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
276 *u = c&(B4|B3|B2|B1|B0);
278 } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
279 *u = c&(B3|B2|B1|B0);
281 } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
286 for(i=n,++s; i>0; --i,++rtn,++s) {
288 if((c&(B7|B6)) != B7) /* 10xxxxxx */
291 *u |= c&(B5|B4|B3|B2|B1|B0);
293 if((n == 1 && *u < 0x80) ||
294 (n == 2 && *u < 0x800) ||
295 (n == 3 && *u < 0x10000) ||
296 (*u >= 0xD800 && *u <= 0xDFFF))
305 utf8encode(long *u, char *s) {
310 sp = (unsigned char*) s;
313 *sp = uc; /* 0xxxxxxx */
315 } else if(*u < 0x800) {
316 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
318 } else if(uc < 0x10000) {
319 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
321 } else if(uc <= 0x10FFFF) {
322 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
327 for(i=n,++sp; i>0; --i,++sp)
328 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
338 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
339 UTF-8 otherwise return 0 */
341 isfullutf8(char *s, int b) {
342 unsigned char *c1, *c2, *c3;
344 c1 = (unsigned char *) s;
345 c2 = (unsigned char *) ++s;
346 c3 = (unsigned char *) ++s;
349 else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
351 else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
353 ((b == 2) && (*c2&(B7|B6)) == B7)))
355 else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
357 ((b == 2) && (*c2&(B7|B6)) == B7) ||
358 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
366 unsigned char c = *s;
370 else if((c&(B7|B6|B5)) == (B7|B6))
372 else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
380 sel.tclick1.tv_sec = 0;
381 sel.tclick1.tv_usec = 0;
385 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
386 if(sel.xtarget == None)
387 sel.xtarget = XA_STRING;
391 selected(int x, int y) {
392 if(sel.ey == y && sel.by == y) {
393 int bx = MIN(sel.bx, sel.ex);
394 int ex = MAX(sel.bx, sel.ex);
395 return BETWEEN(x, bx, ex);
397 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
398 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
402 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
404 *b = e->xbutton.button;
406 *x = X2COL(e->xbutton.x);
407 *y = Y2ROW(e->xbutton.y);
408 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
409 sel.b.y = MIN(sel.by, sel.ey);
410 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
411 sel.e.y = MAX(sel.by, sel.ey);
415 mousereport(XEvent *e) {
416 int x = X2COL(e->xbutton.x);
417 int y = Y2ROW(e->xbutton.y);
418 int button = e->xbutton.button;
419 int state = e->xbutton.state;
420 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
421 static int ob, ox, oy;
424 if(e->xbutton.type == MotionNotify) {
425 if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
429 } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
435 if(e->xbutton.type == ButtonPress) {
441 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
442 + (state & Mod4Mask ? 8 : 0)
443 + (state & ControlMask ? 16 : 0);
445 ttywrite(buf, sizeof(buf));
450 if(IS_SET(MODE_MOUSE))
452 else if(e->xbutton.button == Button1) {
454 sel.ex = sel.bx = X2COL(e->xbutton.x);
455 sel.ey = sel.by = Y2ROW(e->xbutton.y);
462 int x, y, sz, sl, ls = 0;
467 sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
468 ptr = str = malloc(sz);
469 for(y = 0; y < term.row; y++) {
470 for(x = 0; x < term.col; x++)
471 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
472 sl = utf8size(term.line[y][x].c);
473 memcpy(ptr, term.line[y][x].c, sl);
476 if(ls && y < sel.e.y)
485 selnotify(XEvent *e) {
486 unsigned long nitems;
487 unsigned long ofs, rem;
494 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
495 False, AnyPropertyType, &type, &format,
496 &nitems, &rem, &data)) {
497 fprintf(stderr, "Clipboard allocation failed\n");
500 ttywrite((const char *) data, nitems * format / 8);
502 /* number of 32-bit chunks returned */
503 ofs += nitems * format / 32;
509 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
513 selrequest(XEvent *e) {
514 XSelectionRequestEvent *xsre;
518 xsre = (XSelectionRequestEvent *) e;
519 xev.type = SelectionNotify;
520 xev.requestor = xsre->requestor;
521 xev.selection = xsre->selection;
522 xev.target = xsre->target;
523 xev.time = xsre->time;
527 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
528 if(xsre->target == xa_targets) {
529 /* respond with the supported type */
530 Atom string = sel.xtarget;
531 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
532 XA_ATOM, 32, PropModeReplace,
533 (unsigned char *) &string, 1);
534 xev.property = xsre->property;
535 } else if(xsre->target == sel.xtarget) {
536 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
537 xsre->target, 8, PropModeReplace,
538 (unsigned char *) sel.clip, strlen(sel.clip));
539 xev.property = xsre->property;
542 /* all done, send a notification to the listener */
543 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
544 fprintf(stderr, "Error sending SelectionNotify event\n");
549 /* register the selection for both the clipboard and the primary */
555 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
557 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
558 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
564 brelease(XEvent *e) {
565 if(IS_SET(MODE_MOUSE)) {
569 if(e->xbutton.button == Button2)
571 else if(e->xbutton.button == Button1) {
573 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
574 if(sel.bx == sel.ex && sel.by == sel.ey) {
577 gettimeofday(&now, NULL);
579 if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
580 /* triple click on the line */
581 sel.b.x = sel.bx = 0;
582 sel.e.x = sel.ex = term.col;
583 sel.b.y = sel.e.y = sel.ey;
585 } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
586 /* double click to select word */
588 while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
589 term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
591 while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
592 term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
594 sel.b.y = sel.e.y = sel.ey;
600 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
601 gettimeofday(&sel.tclick1, NULL);
607 if(IS_SET(MODE_MOUSE)) {
612 int oldey = sel.ey, oldex = sel.ex;
613 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
615 if(oldey != sel.ey || oldex != sel.ex) {
616 int starty = MIN(oldey, sel.ey);
617 int endy = MAX(oldey, sel.ey);
618 drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row));
624 die(const char *errstr, ...) {
627 va_start(ap, errstr);
628 vfprintf(stderr, errstr, ap);
636 char *envshell = getenv("SHELL");
638 DEFAULT(envshell, "sh");
639 putenv("TERM="TNAME);
640 args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
641 execvp(args[0], args);
648 if(waitpid(pid, &stat, 0) < 0)
649 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
651 exit(WEXITSTATUS(stat));
660 /* seems to work fine on linux, openbsd and freebsd */
661 struct winsize w = {term.row, term.col, 0, 0};
662 if(openpty(&m, &s, NULL, NULL, &w) < 0)
663 die("openpty failed: %s\n", SERRNO);
665 switch(pid = fork()) {
667 die("fork failed\n");
670 setsid(); /* create a new process group */
671 dup2(s, STDIN_FILENO);
672 dup2(s, STDOUT_FILENO);
673 dup2(s, STDERR_FILENO);
674 if(ioctl(s, TIOCSCTTY, NULL) < 0)
675 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
683 signal(SIGCHLD, sigchld);
690 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
692 fprintf(stderr, "\n");
697 static char buf[BUFSIZ];
698 static int buflen = 0;
701 int charsize; /* size of utf8 char in bytes */
705 /* append read bytes to unprocessed bytes */
706 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
707 die("Couldn't read from shell: %s\n", SERRNO);
709 /* process every complete utf8 char */
712 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
713 charsize = utf8decode(ptr, &utf8c);
714 utf8encode(&utf8c, s);
720 /* keep any uncomplete utf8 char for the next call */
721 memmove(buf, ptr, buflen);
725 ttywrite(const char *s, size_t n) {
726 if(write(cmdfd, s, n) == -1)
727 die("write error on tty: %s\n", SERRNO);
731 ttyresize(int x, int y) {
736 w.ws_xpixel = w.ws_ypixel = 0;
737 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
738 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
745 if(mode == CURSOR_SAVE)
747 else if(mode == CURSOR_LOAD)
748 term.c = c, tmoveto(c.x, c.y);
757 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
759 term.top = 0, term.bot = term.row - 1;
760 term.mode = MODE_WRAP;
761 tclearregion(0, 0, term.col-1, term.row-1);
765 tnew(int col, int row) {
766 /* set screen size */
767 term.row = row, term.col = col;
768 term.line = malloc(term.row * sizeof(Line));
769 term.alt = malloc(term.row * sizeof(Line));
770 for(row = 0 ; row < term.row; row++) {
771 term.line[row] = malloc(term.col * sizeof(Glyph));
772 term.alt [row] = malloc(term.col * sizeof(Glyph));
780 Line* tmp = term.line;
781 term.line = term.alt;
783 term.mode ^= MODE_ALTSCREEN;
787 tscrolldown(int orig, int n) {
791 LIMIT(n, 0, term.bot-orig+1);
793 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
795 for(i = term.bot; i >= orig+n; i--) {
797 term.line[i] = term.line[i-n];
798 term.line[i-n] = temp;
803 tscrollup(int orig, int n) {
806 LIMIT(n, 0, term.bot-orig+1);
808 tclearregion(0, orig, term.col-1, orig+n-1);
810 for(i = orig; i <= term.bot-n; i++) {
812 term.line[i] = term.line[i+n];
813 term.line[i+n] = temp;
818 tnewline(int first_col) {
821 tscrollup(term.top, 1);
824 tmoveto(first_col ? 0 : term.c.x, y);
830 char *p = escseq.buf;
834 escseq.priv = 1, p++;
836 while(p < escseq.buf+escseq.len) {
838 escseq.arg[escseq.narg] *= 10;
839 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
841 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
852 tmoveto(int x, int y) {
853 LIMIT(x, 0, term.col-1);
854 LIMIT(y, 0, term.row-1);
855 term.c.state &= ~CURSOR_WRAPNEXT;
862 term.line[term.c.y][term.c.x] = term.c.attr;
863 memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
864 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
868 tclearregion(int x1, int y1, int x2, int y2) {
872 temp = x1, x1 = x2, x2 = temp;
874 temp = y1, y1 = y2, y2 = temp;
876 LIMIT(x1, 0, term.col-1);
877 LIMIT(x2, 0, term.col-1);
878 LIMIT(y1, 0, term.row-1);
879 LIMIT(y2, 0, term.row-1);
881 for(y = y1; y <= y2; y++)
882 for(x = x1; x <= x2; x++)
883 term.line[y][x].state = 0;
888 int src = term.c.x + n;
890 int size = term.col - src;
892 if(src >= term.col) {
893 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
896 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
897 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
901 tinsertblank(int n) {
904 int size = term.col - dst;
906 if(dst >= term.col) {
907 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
910 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
911 tclearregion(src, term.c.y, dst - 1, term.c.y);
915 tinsertblankline(int n) {
916 if(term.c.y < term.top || term.c.y > term.bot)
919 tscrolldown(term.c.y, n);
924 if(term.c.y < term.top || term.c.y > term.bot)
927 tscrollup(term.c.y, n);
931 tsetattr(int *attr, int l) {
934 for(i = 0; i < l; i++) {
937 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
938 term.c.attr.fg = DefaultFG;
939 term.c.attr.bg = DefaultBG;
942 term.c.attr.mode |= ATTR_BOLD;
945 term.c.attr.mode |= ATTR_UNDERLINE;
948 term.c.attr.mode |= ATTR_REVERSE;
951 term.c.attr.mode &= ~ATTR_BOLD;
954 term.c.attr.mode &= ~ATTR_UNDERLINE;
957 term.c.attr.mode &= ~ATTR_REVERSE;
960 if(i + 2 < l && attr[i + 1] == 5) {
962 if(BETWEEN(attr[i], 0, 255))
963 term.c.attr.fg = attr[i];
965 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
968 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
971 term.c.attr.fg = DefaultFG;
974 if(i + 2 < l && attr[i + 1] == 5) {
976 if(BETWEEN(attr[i], 0, 255))
977 term.c.attr.bg = attr[i];
979 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
982 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
985 term.c.attr.bg = DefaultBG;
988 if(BETWEEN(attr[i], 30, 37))
989 term.c.attr.fg = attr[i] - 30;
990 else if(BETWEEN(attr[i], 40, 47))
991 term.c.attr.bg = attr[i] - 40;
992 else if(BETWEEN(attr[i], 90, 97))
993 term.c.attr.fg = attr[i] - 90 + 8;
994 else if(BETWEEN(attr[i], 100, 107))
995 term.c.attr.fg = attr[i] - 100 + 8;
997 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
1005 tsetscroll(int t, int b) {
1008 LIMIT(t, 0, term.row-1);
1009 LIMIT(b, 0, term.row-1);
1021 switch(escseq.mode) {
1024 fprintf(stderr, "erresc: unknown csi ");
1028 case '@': /* ICH -- Insert <n> blank char */
1029 DEFAULT(escseq.arg[0], 1);
1030 tinsertblank(escseq.arg[0]);
1032 case 'A': /* CUU -- Cursor <n> Up */
1034 DEFAULT(escseq.arg[0], 1);
1035 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
1037 case 'B': /* CUD -- Cursor <n> Down */
1038 DEFAULT(escseq.arg[0], 1);
1039 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
1041 case 'C': /* CUF -- Cursor <n> Forward */
1043 DEFAULT(escseq.arg[0], 1);
1044 tmoveto(term.c.x+escseq.arg[0], term.c.y);
1046 case 'D': /* CUB -- Cursor <n> Backward */
1047 DEFAULT(escseq.arg[0], 1);
1048 tmoveto(term.c.x-escseq.arg[0], term.c.y);
1050 case 'E': /* CNL -- Cursor <n> Down and first col */
1051 DEFAULT(escseq.arg[0], 1);
1052 tmoveto(0, term.c.y+escseq.arg[0]);
1054 case 'F': /* CPL -- Cursor <n> Up and first col */
1055 DEFAULT(escseq.arg[0], 1);
1056 tmoveto(0, term.c.y-escseq.arg[0]);
1058 case 'G': /* CHA -- Move to <col> */
1059 case '`': /* XXX: HPA -- same? */
1060 DEFAULT(escseq.arg[0], 1);
1061 tmoveto(escseq.arg[0]-1, term.c.y);
1063 case 'H': /* CUP -- Move to <row> <col> */
1064 case 'f': /* XXX: HVP -- same? */
1065 DEFAULT(escseq.arg[0], 1);
1066 DEFAULT(escseq.arg[1], 1);
1067 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
1069 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1070 case 'J': /* ED -- Clear screen */
1071 switch(escseq.arg[0]) {
1073 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1074 if(term.c.y < term.row-1)
1075 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1079 tclearregion(0, 0, term.col-1, term.c.y-1);
1080 tclearregion(0, term.c.y, term.c.x, term.c.y);
1083 tclearregion(0, 0, term.col-1, term.row-1);
1089 case 'K': /* EL -- Clear line */
1090 switch(escseq.arg[0]) {
1092 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1095 tclearregion(0, term.c.y, term.c.x, term.c.y);
1098 tclearregion(0, term.c.y, term.col-1, term.c.y);
1102 case 'S': /* SU -- Scroll <n> line up */
1103 DEFAULT(escseq.arg[0], 1);
1104 tscrollup(term.top, escseq.arg[0]);
1106 case 'T': /* SD -- Scroll <n> line down */
1107 DEFAULT(escseq.arg[0], 1);
1108 tscrolldown(term.top, escseq.arg[0]);
1110 case 'L': /* IL -- Insert <n> blank lines */
1111 DEFAULT(escseq.arg[0], 1);
1112 tinsertblankline(escseq.arg[0]);
1114 case 'l': /* RM -- Reset Mode */
1116 switch(escseq.arg[0]) {
1118 term.mode &= ~MODE_APPKEYPAD;
1120 case 5: /* DECSCNM -- Remove reverse video */
1121 if(IS_SET(MODE_REVERSE)) {
1122 term.mode &= ~MODE_REVERSE;
1127 term.mode &= ~MODE_WRAP;
1129 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1132 term.mode &= ~MODE_CRLF;
1135 term.c.state |= CURSOR_HIDE;
1137 case 1000: /* disable X11 xterm mouse reporting */
1138 term.mode &= ~MODE_MOUSEBTN;
1141 term.mode &= ~MODE_MOUSEMOTION;
1143 case 1049: /* = 1047 and 1048 */
1145 if(IS_SET(MODE_ALTSCREEN)) {
1146 tclearregion(0, 0, term.col-1, term.row-1);
1149 if(escseq.arg[0] == 1047)
1152 tcursor(CURSOR_LOAD);
1158 switch(escseq.arg[0]) {
1160 term.mode &= ~MODE_INSERT;
1167 case 'M': /* DL -- Delete <n> lines */
1168 DEFAULT(escseq.arg[0], 1);
1169 tdeleteline(escseq.arg[0]);
1171 case 'X': /* ECH -- Erase <n> char */
1172 DEFAULT(escseq.arg[0], 1);
1173 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
1175 case 'P': /* DCH -- Delete <n> char */
1176 DEFAULT(escseq.arg[0], 1);
1177 tdeletechar(escseq.arg[0]);
1179 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1180 case 'd': /* VPA -- Move to <row> */
1181 DEFAULT(escseq.arg[0], 1);
1182 tmoveto(term.c.x, escseq.arg[0]-1);
1184 case 'h': /* SM -- Set terminal mode */
1186 switch(escseq.arg[0]) {
1188 term.mode |= MODE_APPKEYPAD;
1190 case 5: /* DECSCNM -- Reverve video */
1191 if(!IS_SET(MODE_REVERSE)) {
1192 term.mode |= MODE_REVERSE;
1197 term.mode |= MODE_WRAP;
1200 term.mode |= MODE_CRLF;
1202 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1203 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1204 if(escseq.narg > 1 && escseq.arg[1] != 25)
1207 term.c.state &= ~CURSOR_HIDE;
1209 case 1000: /* 1000,1002: enable xterm mouse report */
1210 term.mode |= MODE_MOUSEBTN;
1213 term.mode |= MODE_MOUSEMOTION;
1215 case 1049: /* = 1047 and 1048 */
1217 if(IS_SET(MODE_ALTSCREEN))
1218 tclearregion(0, 0, term.col-1, term.row-1);
1221 if(escseq.arg[0] == 1047)
1224 tcursor(CURSOR_SAVE);
1226 default: goto unknown;
1229 switch(escseq.arg[0]) {
1231 term.mode |= MODE_INSERT;
1233 default: goto unknown;
1237 case 'm': /* SGR -- Terminal attribute (color) */
1238 tsetattr(escseq.arg, escseq.narg);
1240 case 'r': /* DECSTBM -- Set Scrolling Region */
1244 DEFAULT(escseq.arg[0], 1);
1245 DEFAULT(escseq.arg[1], term.row);
1246 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
1250 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1251 tcursor(CURSOR_SAVE);
1253 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1254 tcursor(CURSOR_LOAD);
1262 printf("ESC [ %s", escseq.priv ? "? " : "");
1264 for(i = 0; i < escseq.narg; i++)
1265 printf("%d ", escseq.arg[i]);
1267 putchar(escseq.mode);
1273 memset(&escseq, 0, sizeof(escseq));
1278 int space = TAB - term.c.x % TAB;
1279 tmoveto(term.c.x + space, term.c.y);
1285 if(term.esc & ESC_START) {
1286 if(term.esc & ESC_CSI) {
1287 escseq.buf[escseq.len++] = ascii;
1288 if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
1290 csiparse(), csihandle();
1292 /* TODO: handle other OSC */
1293 } else if(term.esc & ESC_OSC) {
1296 term.esc = ESC_START | ESC_TITLE;
1298 } else if(term.esc & ESC_TITLE) {
1299 if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
1301 term.title[term.titlelen] = '\0';
1302 XStoreName(xw.dpy, xw.win, term.title);
1304 term.title[term.titlelen++] = ascii;
1306 } else if(term.esc & ESC_ALTCHARSET) {
1308 case '0': /* Line drawing crap */
1309 term.c.attr.mode |= ATTR_GFX;
1311 case 'B': /* Back to regular text */
1312 term.c.attr.mode &= ~ATTR_GFX;
1315 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1321 term.esc |= ESC_CSI;
1324 term.esc |= ESC_OSC;
1327 term.esc |= ESC_ALTCHARSET;
1329 case 'D': /* IND -- Linefeed */
1330 if(term.c.y == term.bot)
1331 tscrollup(term.top, 1);
1333 tmoveto(term.c.x, term.c.y+1);
1336 case 'E': /* NEL -- Next line */
1337 tnewline(1); /* always go to first col */
1340 case 'M': /* RI -- Reverse index */
1341 if(term.c.y == term.top)
1342 tscrolldown(term.top, 1);
1344 tmoveto(term.c.x, term.c.y-1);
1347 case 'c': /* RIS -- Reset to inital state */
1351 case '=': /* DECPAM -- Application keypad */
1352 term.mode |= MODE_APPKEYPAD;
1355 case '>': /* DECPNM -- Normal keypad */
1356 term.mode &= ~MODE_APPKEYPAD;
1359 case '7': /* DECSC -- Save Cursor */
1360 tcursor(CURSOR_SAVE);
1363 case '8': /* DECRC -- Restore Cursor */
1364 tcursor(CURSOR_LOAD);
1368 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1369 (unsigned char) ascii, isprint(ascii)?ascii:'.');
1379 tmoveto(term.c.x-1, term.c.y);
1382 tmoveto(0, term.c.y);
1387 /* go to first col if the mode is set */
1388 tnewline(IS_SET(MODE_CRLF));
1391 if(!(xw.state & WIN_FOCUSED))
1396 term.esc = ESC_START;
1399 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1400 tnewline(1); /* always go to first col */
1402 if(term.c.x+1 < term.col)
1403 tmoveto(term.c.x+1, term.c.y);
1405 term.c.state |= CURSOR_WRAPNEXT;
1411 tresize(int col, int row) {
1413 int minrow = MIN(row, term.row);
1414 int mincol = MIN(col, term.col);
1415 int slide = term.c.y - row + 1;
1417 if(col < 1 || row < 1)
1420 /* free unneeded rows */
1423 /* slide screen to keep cursor where we expect it -
1424 * tscrollup would work here, but we can optimize to
1425 * memmove because we're freeing the earlier lines */
1426 for(/* i = 0 */; i < slide; i++) {
1430 memmove(term.line, term.line + slide, row * sizeof(Line));
1431 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1433 for(i += row; i < term.row; i++) {
1438 /* resize to new height */
1439 term.line = realloc(term.line, row * sizeof(Line));
1440 term.alt = realloc(term.alt, row * sizeof(Line));
1442 /* resize each row to new width, zero-pad if needed */
1443 for(i = 0; i < minrow; i++) {
1444 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1445 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1446 for(x = mincol; x < col; x++) {
1447 term.line[i][x].state = 0;
1448 term.alt[i][x].state = 0;
1452 /* allocate any new rows */
1453 for(/* i == minrow */; i < row; i++) {
1454 term.line[i] = calloc(col, sizeof(Glyph));
1455 term.alt [i] = calloc(col, sizeof(Glyph));
1458 /* update terminal size */
1459 term.col = col, term.row = row;
1460 /* make use of the LIMIT in tmoveto */
1461 tmoveto(term.c.x, term.c.y);
1462 /* reset scrolling region */
1463 tsetscroll(0, row-1);
1468 xresize(int col, int row) {
1474 xw.bufw = MAX(1, col * xw.cw);
1475 xw.bufh = MAX(1, row * xw.ch);
1476 newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1477 XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
1478 XFreePixmap(xw.dpy, xw.buf);
1479 XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
1481 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
1482 xw.bufw-oldw, MIN(xw.bufh, oldh));
1483 else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
1484 XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
1485 xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
1488 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
1489 xw.bufw, xw.bufh-oldh);
1490 else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
1491 XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
1492 xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
1501 unsigned long white = WhitePixel(xw.dpy, xw.scr);
1503 for(i = 0; i < 16; i++) {
1504 if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1506 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1508 dc.col[i] = color.pixel;
1511 /* same colors as xterm */
1512 for(r = 0; r < 6; r++)
1513 for(g = 0; g < 6; g++)
1514 for(b = 0; b < 6; b++) {
1515 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1516 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1517 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1518 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1520 fprintf(stderr, "Could not allocate color %d\n", i);
1522 dc.col[i] = color.pixel;
1526 for(r = 0; r < 24; r++, i++) {
1527 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1528 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1530 fprintf(stderr, "Could not allocate color %d\n", i);
1532 dc.col[i] = color.pixel;
1537 xclear(int x1, int y1, int x2, int y2) {
1538 XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
1539 XFillRectangle(xw.dpy, xw.buf, dc.gc,
1540 x1 * xw.cw, y1 * xw.ch,
1541 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1546 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1547 XWMHints wm = {.flags = InputHint, .input = 1};
1549 .flags = PSize | PResizeInc | PBaseSize,
1552 .height_inc = xw.ch,
1554 .base_height = 2*BORDER,
1555 .base_width = 2*BORDER,
1557 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1561 xinitfont(char *fontstr) {
1563 char *def, **missing;
1567 set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1570 fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1571 XFreeStringList(missing);
1577 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
1578 XFontStruct **xfonts;
1582 *ascent = *descent = *lbearing = *rbearing = 0;
1583 n = XFontsOfFontSet(set, &xfonts, &font_names);
1584 for(i = 0; i < n; i++) {
1585 *ascent = MAX(*ascent, (*xfonts)->ascent);
1586 *descent = MAX(*descent, (*xfonts)->descent);
1587 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1588 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1594 initfonts(char *fontstr, char *bfontstr) {
1595 if((dc.font.set = xinitfont(fontstr)) == NULL ||
1596 (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1597 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1598 xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1599 &dc.font.lbearing, &dc.font.rbearing);
1600 xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1601 &dc.bfont.lbearing, &dc.bfont.rbearing);
1606 XSetWindowAttributes attrs;
1609 if(!(xw.dpy = XOpenDisplay(NULL)))
1610 die("Can't open display\n");
1611 xw.scr = XDefaultScreen(xw.dpy);
1614 initfonts(FONT, BOLDFONT);
1616 /* XXX: Assuming same size for bold font */
1617 xw.cw = dc.font.rbearing - dc.font.lbearing;
1618 xw.ch = dc.font.ascent + dc.font.descent;
1621 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1624 /* window - default size */
1625 xw.bufh = 24 * xw.ch;
1626 xw.bufw = 80 * xw.cw;
1627 xw.h = xw.bufh + 2*BORDER;
1628 xw.w = xw.bufw + 2*BORDER;
1630 attrs.background_pixel = dc.col[DefaultBG];
1631 attrs.border_pixel = dc.col[DefaultBG];
1632 attrs.bit_gravity = NorthWestGravity;
1633 attrs.event_mask = FocusChangeMask | KeyPressMask
1634 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1635 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
1636 attrs.colormap = xw.cmap;
1638 xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
1639 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1640 XDefaultVisual(xw.dpy, xw.scr),
1641 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1644 xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1648 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1649 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1650 | XIMStatusNothing, XNClientWindow, xw.win,
1651 XNFocusWindow, xw.win, NULL);
1653 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1655 /* white cursor, black outline */
1656 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
1657 XDefineCursor(xw.dpy, xw.win, cursor);
1658 XRecolorCursor(xw.dpy, cursor,
1659 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
1660 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
1662 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1663 XMapWindow(xw.dpy, xw.win);
1669 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1670 unsigned long xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
1671 int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1674 /* only switch default fg/bg if term is in RV mode */
1675 if(IS_SET(MODE_REVERSE)) {
1676 if(base.fg == DefaultFG)
1677 xfg = dc.col[DefaultBG];
1678 if(base.bg == DefaultBG)
1679 xbg = dc.col[DefaultFG];
1682 if(base.mode & ATTR_REVERSE)
1683 temp = xfg, xfg = xbg, xbg = temp;
1685 XSetBackground(xw.dpy, dc.gc, xbg);
1686 XSetForeground(xw.dpy, dc.gc, xfg);
1688 if(base.mode & ATTR_GFX) {
1689 for(i = 0; i < bytelen; i++) {
1690 char c = gfx[(unsigned int)s[i] % 256];
1693 else if(s[i] > 0x5f)
1698 XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
1699 dc.gc, winx, winy, s, bytelen);
1701 if(base.mode & ATTR_UNDERLINE)
1702 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1707 static int oldx = 0;
1708 static int oldy = 0;
1710 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
1712 LIMIT(oldx, 0, term.col-1);
1713 LIMIT(oldy, 0, term.row-1);
1715 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1716 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
1718 /* remove the old cursor */
1719 if(term.line[oldy][oldx].state & GLYPH_SET) {
1720 sl = utf8size(term.line[oldy][oldx].c);
1721 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
1723 xclear(oldx, oldy, oldx, oldy);
1725 /* draw the new one */
1726 if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
1728 if(IS_SET(MODE_REVERSE))
1729 g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
1730 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
1731 oldx = term.c.x, oldy = term.c.y;
1737 drawregion(0, 0, term.col, term.row);
1741 drawregion(int x1, int y1, int x2, int y2) {
1742 int ic, ib, x, y, ox, sl;
1744 char buf[DRAW_BUF_SIZ];
1746 if(!(xw.state & WIN_VISIBLE))
1749 xclear(x1, y1, x2-1, y2-1);
1750 for(y = y1; y < y2; y++) {
1751 base = term.line[y][0];
1753 for(x = x1; x < x2; x++) {
1754 new = term.line[y][x];
1755 if(sel.bx != -1 && *(new.c) && selected(x, y))
1756 new.mode ^= ATTR_REVERSE;
1757 if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1758 ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
1759 xdraws(buf, base, ox, y, ic, ib);
1762 if(new.state & GLYPH_SET) {
1767 sl = utf8size(new.c);
1768 memcpy(buf+ib, new.c, sl);
1774 xdraws(buf, base, ox, y, ic, ib);
1777 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1781 expose(XEvent *ev) {
1782 XExposeEvent *e = &ev->xexpose;
1783 if(xw.state & WIN_REDRAW) {
1785 xw.state &= ~WIN_REDRAW;
1789 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
1790 e->width, e->height, e->x, e->y);
1794 visibility(XEvent *ev) {
1795 XVisibilityEvent *e = &ev->xvisibility;
1796 if(e->state == VisibilityFullyObscured)
1797 xw.state &= ~WIN_VISIBLE;
1798 else if(!(xw.state & WIN_VISIBLE))
1799 /* need a full redraw for next Expose, not just a buf copy */
1800 xw.state |= WIN_VISIBLE | WIN_REDRAW;
1805 xw.state &= ~WIN_VISIBLE;
1809 xseturgency(int add) {
1810 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1811 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1812 XSetWMHints(xw.dpy, xw.win, h);
1818 if(ev->type == FocusIn) {
1819 xw.state |= WIN_FOCUSED;
1822 xw.state &= ~WIN_FOCUSED;
1827 kmap(KeySym k, unsigned int state) {
1829 for(i = 0; i < LEN(key); i++)
1830 if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state))
1831 return (char*)key[i].s;
1836 kpress(XEvent *ev) {
1837 XKeyEvent *e = &ev->xkey;
1846 meta = e->state & Mod1Mask;
1847 shift = e->state & ShiftMask;
1848 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
1850 /* 1. custom keys from config.h */
1851 if((customkey = kmap(ksym, e->state)))
1852 ttywrite(customkey, strlen(customkey));
1853 /* 2. hardcoded (overrides X lookup) */
1860 /* XXX: shift up/down doesn't work */
1861 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
1869 if(IS_SET(MODE_CRLF))
1870 ttywrite("\r\n", 2);
1877 if(meta && len == 1)
1878 ttywrite("\033", 1);
1889 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1892 xw.w = e->xconfigure.width;
1893 xw.h = e->xconfigure.height;
1894 col = (xw.w - 2*BORDER) / xw.cw;
1895 row = (xw.h - 2*BORDER) / xw.ch;
1896 if(col == term.col && row == term.row)
1898 if(tresize(col, row))
1900 ttyresize(col, row);
1908 int xfd = XConnectionNumber(xw.dpy);
1912 FD_SET(cmdfd, &rfd);
1914 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1917 die("select failed: %s\n", SERRNO);
1919 if(FD_ISSET(cmdfd, &rfd)) {
1923 while(XPending(xw.dpy)) {
1924 XNextEvent(xw.dpy, &ev);
1925 if(XFilterEvent(&ev, xw.win))
1927 if(handler[ev.type])
1928 (handler[ev.type])(&ev);
1934 main(int argc, char *argv[]) {
1937 for(i = 1; i < argc; i++) {
1938 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
1940 if(++i < argc) opt_title = argv[i];
1943 if(++i < argc) opt_class = argv[i];
1946 /* eat every remaining arguments */
1947 if(++i < argc) opt_cmd = &argv[i];
1956 setlocale(LC_CTYPE, "");