1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/extensions/Xdbe.h>
31 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
33 #elif defined(__FreeBSD__) || defined(__DragonFly__)
38 "st " VERSION " (c) 2010-2012 st engineers\n" \
39 "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
42 #define XEMBED_FOCUS_IN 4
43 #define XEMBED_FOCUS_OUT 5
46 #define ESC_TITLE_SIZ 256
47 #define ESC_BUF_SIZ 256
48 #define ESC_ARG_SIZ 16
49 #define DRAW_BUF_SIZ 1024
51 #define XK_NO_MOD UINT_MAX
54 #define SELECT_TIMEOUT (20*1000) /* 20 ms */
55 #define DRAW_TIMEOUT (20*1000) /* 20 ms */
57 #define SERRNO strerror(errno)
58 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 #define MAX(a, b) ((a) < (b) ? (b) : (a))
60 #define LEN(a) (sizeof(a) / sizeof(a[0]))
61 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
62 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
63 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
64 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
65 #define IS_SET(flag) (term.mode & (flag))
66 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
67 #define X2COL(x) (((x) - BORDER)/xw.cw)
68 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
70 enum glyph_attribute {
78 enum cursor_movement {
105 MODE_MOUSEMOTION = 64,
126 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
128 typedef unsigned char uchar;
129 typedef unsigned int uint;
130 typedef unsigned long ulong;
131 typedef unsigned short ushort;
134 char c[UTF_SIZ]; /* character code */
135 uchar mode; /* attribute flags */
136 ushort fg; /* foreground */
137 ushort bg; /* background */
138 uchar state; /* state flags */
144 Glyph attr; /* current char attributes */
150 /* CSI Escape sequence structs */
151 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
153 char buf[ESC_BUF_SIZ]; /* raw string */
154 int len; /* raw string length */
156 int arg[ESC_ARG_SIZ];
157 int narg; /* nb of args */
161 /* Internal representation of the screen */
163 int row; /* nb row */
164 int col; /* nb col */
165 Line* line; /* screen */
166 Line* alt; /* alternate screen */
167 bool* dirty; /* dirtyness of lines */
168 TCursor c; /* cursor */
169 int top; /* top scroll limit */
170 int bot; /* bottom scroll limit */
171 int mode; /* terminal mode flags */
172 int esc; /* escape state flags */
173 char title[ESC_TITLE_SIZ];
177 /* Purely graphic info */
187 int w; /* window width */
188 int h; /* window height */
189 int bufw; /* pixmap width */
190 int bufh; /* pixmap height */
191 int ch; /* char height */
192 int cw; /* char width */
193 char state; /* focus, redraw, visible */
194 struct timeval lastdraw;
204 /* TODO: use better name for vars... */
209 struct {int x, y;} b, e;
212 struct timeval tclick1;
213 struct timeval tclick2;
218 /* Drawing Context */
220 ulong col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
231 static void die(const char*, ...);
232 static void draw(void);
233 static void drawregion(int, int, int, int);
234 static void execsh(void);
235 static void sigchld(int);
236 static void run(void);
237 static bool last_draw_too_old(void);
239 static void csidump(void);
240 static void csihandle(void);
241 static void csiparse(void);
242 static void csireset(void);
244 static void tclearregion(int, int, int, int);
245 static void tcursor(int);
246 static void tdeletechar(int);
247 static void tdeleteline(int);
248 static void tinsertblank(int);
249 static void tinsertblankline(int);
250 static void tmoveto(int, int);
251 static void tnew(int, int);
252 static void tnewline(int);
253 static void tputtab(void);
254 static void tputc(char*);
255 static void treset(void);
256 static int tresize(int, int);
257 static void tscrollup(int, int);
258 static void tscrolldown(int, int);
259 static void tsetattr(int*, int);
260 static void tsetchar(char*);
261 static void tsetscroll(int, int);
262 static void tswapscreen(void);
263 static void tsetdirt(int, int);
264 static void tfulldirt(void);
266 static void ttynew(void);
267 static void ttyread(void);
268 static void ttyresize(int, int);
269 static void ttywrite(const char *, size_t);
271 static void xdraws(char *, Glyph, int, int, int, int);
272 static void xhints(void);
273 static void xclear(int, int, int, int);
275 static void xdrawcursor(void);
276 static void xinit(void);
277 static void xloadcols(void);
278 static void xseturgency(int);
279 static void xsetsel(char*);
280 static void xresize(int, int);
282 static void expose(XEvent *);
283 static void visibility(XEvent *);
284 static void unmap(XEvent *);
285 static char* kmap(KeySym, uint);
286 static void kpress(XEvent *);
287 static void cmessage(XEvent *);
288 static void resize(XEvent *);
289 static void focus(XEvent *);
290 static void brelease(XEvent *);
291 static void bpress(XEvent *);
292 static void bmotion(XEvent *);
293 static void selnotify(XEvent *);
294 static void selrequest(XEvent *);
296 static void selinit(void);
297 static inline bool selected(int, int);
298 static void selcopy(void);
299 static void selpaste();
300 static void selscroll(int, int);
302 static int utf8decode(char *, long *);
303 static int utf8encode(long *, char *);
304 static int utf8size(char *);
305 static int isfullutf8(char *, int);
307 static void (*handler[LASTEvent])(XEvent *) = {
309 [ClientMessage] = cmessage,
310 [ConfigureNotify] = resize,
311 [VisibilityNotify] = visibility,
312 [UnmapNotify] = unmap,
316 [MotionNotify] = bmotion,
317 [ButtonPress] = bpress,
318 [ButtonRelease] = brelease,
319 [SelectionNotify] = selnotify,
320 [SelectionRequest] = selrequest,
327 static CSIEscape escseq;
330 static Selection sel;
331 static char **opt_cmd = NULL;
332 static char *opt_title = NULL;
333 static char *opt_embed = NULL;
334 static char *opt_class = NULL;
337 utf8decode(char *s, long *u) {
343 if(~c & B7) { /* 0xxxxxxx */
346 } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
347 *u = c&(B4|B3|B2|B1|B0);
349 } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
350 *u = c&(B3|B2|B1|B0);
352 } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
357 for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
359 if((c & (B7|B6)) != B7) /* 10xxxxxx */
362 *u |= c & (B5|B4|B3|B2|B1|B0);
364 if((n == 1 && *u < 0x80) ||
365 (n == 2 && *u < 0x800) ||
366 (n == 3 && *u < 0x10000) ||
367 (*u >= 0xD800 && *u <= 0xDFFF))
376 utf8encode(long *u, char *s) {
384 *sp = uc; /* 0xxxxxxx */
386 } else if(*u < 0x800) {
387 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
389 } else if(uc < 0x10000) {
390 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
392 } else if(uc <= 0x10FFFF) {
393 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
398 for(i=n,++sp; i>0; --i,++sp)
399 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
409 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
410 UTF-8 otherwise return 0 */
412 isfullutf8(char *s, int b) {
420 else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
422 else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
424 ((b == 2) && (*c2&(B7|B6)) == B7)))
426 else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
428 ((b == 2) && (*c2&(B7|B6)) == B7) ||
429 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
441 else if((c&(B7|B6|B5)) == (B7|B6))
443 else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
451 memset(&sel.tclick1, 0, sizeof(sel.tclick1));
452 memset(&sel.tclick2, 0, sizeof(sel.tclick2));
456 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
457 if(sel.xtarget == None)
458 sel.xtarget = XA_STRING;
462 selected(int x, int y) {
463 if(sel.ey == y && sel.by == y) {
464 int bx = MIN(sel.bx, sel.ex);
465 int ex = MAX(sel.bx, sel.ex);
466 return BETWEEN(x, bx, ex);
468 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
469 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
473 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
475 *b = e->xbutton.button;
477 *x = X2COL(e->xbutton.x);
478 *y = Y2ROW(e->xbutton.y);
479 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
480 sel.b.y = MIN(sel.by, sel.ey);
481 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
482 sel.e.y = MAX(sel.by, sel.ey);
486 mousereport(XEvent *e) {
487 int x = X2COL(e->xbutton.x);
488 int y = Y2ROW(e->xbutton.y);
489 int button = e->xbutton.button;
490 int state = e->xbutton.state;
491 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
492 static int ob, ox, oy;
495 if(e->xbutton.type == MotionNotify) {
496 if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
500 } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
506 if(e->xbutton.type == ButtonPress) {
512 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
513 + (state & Mod4Mask ? 8 : 0)
514 + (state & ControlMask ? 16 : 0);
516 ttywrite(buf, sizeof(buf));
521 if(IS_SET(MODE_MOUSE))
523 else if(e->xbutton.button == Button1) {
525 tsetdirt(sel.b.y, sel.e.y);
527 sel.ex = sel.bx = X2COL(e->xbutton.x);
528 sel.ey = sel.by = Y2ROW(e->xbutton.y);
535 int x, y, bufsize, is_selected = 0;
541 bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
542 ptr = str = malloc(bufsize);
544 /* append every set & selected glyph to the selection */
545 for(y = 0; y < term.row; y++) {
546 for(x = 0; x < term.col; x++) {
547 is_selected = selected(x, y);
548 if((term.line[y][x].state & GLYPH_SET) && is_selected) {
549 int size = utf8size(term.line[y][x].c);
550 memcpy(ptr, term.line[y][x].c, size);
555 /* \n at the end of every selected line except for the last one */
556 if(is_selected && y < sel.e.y)
565 selnotify(XEvent *e) {
566 ulong nitems, ofs, rem;
573 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
574 False, AnyPropertyType, &type, &format,
575 &nitems, &rem, &data)) {
576 fprintf(stderr, "Clipboard allocation failed\n");
579 ttywrite((const char *) data, nitems * format / 8);
581 /* number of 32-bit chunks returned */
582 ofs += nitems * format / 32;
588 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
592 selrequest(XEvent *e) {
593 XSelectionRequestEvent *xsre;
597 xsre = (XSelectionRequestEvent *) e;
598 xev.type = SelectionNotify;
599 xev.requestor = xsre->requestor;
600 xev.selection = xsre->selection;
601 xev.target = xsre->target;
602 xev.time = xsre->time;
606 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
607 if(xsre->target == xa_targets) {
608 /* respond with the supported type */
609 Atom string = sel.xtarget;
610 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
611 XA_ATOM, 32, PropModeReplace,
612 (uchar *) &string, 1);
613 xev.property = xsre->property;
614 } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
615 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
616 xsre->target, 8, PropModeReplace,
617 (uchar *) sel.clip, strlen(sel.clip));
618 xev.property = xsre->property;
621 /* all done, send a notification to the listener */
622 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
623 fprintf(stderr, "Error sending SelectionNotify event\n");
628 /* register the selection for both the clipboard and the primary */
634 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
636 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
637 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
643 brelease(XEvent *e) {
644 if(IS_SET(MODE_MOUSE)) {
648 if(e->xbutton.button == Button2)
650 else if(e->xbutton.button == Button1) {
652 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
653 term.dirty[sel.ey] = 1;
654 if(sel.bx == sel.ex && sel.by == sel.ey) {
657 gettimeofday(&now, NULL);
659 if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
660 /* triple click on the line */
661 sel.b.x = sel.bx = 0;
662 sel.e.x = sel.ex = term.col;
663 sel.b.y = sel.e.y = sel.ey;
665 } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
666 /* double click to select word */
668 while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
669 term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
671 while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
672 term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
674 sel.b.y = sel.e.y = sel.ey;
680 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
681 gettimeofday(&sel.tclick1, NULL);
687 if(IS_SET(MODE_MOUSE)) {
692 int oldey = sel.ey, oldex = sel.ex;
693 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
695 if(oldey != sel.ey || oldex != sel.ex) {
696 int starty = MIN(oldey, sel.ey);
697 int endy = MAX(oldey, sel.ey);
698 tsetdirt(starty, endy);
705 die(const char *errstr, ...) {
708 va_start(ap, errstr);
709 vfprintf(stderr, errstr, ap);
717 char *envshell = getenv("SHELL");
719 DEFAULT(envshell, SHELL);
720 putenv("TERM="TNAME);
721 args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
722 execvp(args[0], args);
729 if(waitpid(pid, &stat, 0) < 0)
730 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
732 exit(WEXITSTATUS(stat));
741 /* seems to work fine on linux, openbsd and freebsd */
742 struct winsize w = {term.row, term.col, 0, 0};
743 if(openpty(&m, &s, NULL, NULL, &w) < 0)
744 die("openpty failed: %s\n", SERRNO);
746 switch(pid = fork()) {
748 die("fork failed\n");
751 setsid(); /* create a new process group */
752 dup2(s, STDIN_FILENO);
753 dup2(s, STDOUT_FILENO);
754 dup2(s, STDERR_FILENO);
755 if(ioctl(s, TIOCSCTTY, NULL) < 0)
756 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
764 signal(SIGCHLD, sigchld);
771 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
773 fprintf(stderr, "\n");
778 static char buf[BUFSIZ];
779 static int buflen = 0;
782 int charsize; /* size of utf8 char in bytes */
786 /* append read bytes to unprocessed bytes */
787 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
788 die("Couldn't read from shell: %s\n", SERRNO);
790 /* process every complete utf8 char */
793 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
794 charsize = utf8decode(ptr, &utf8c);
795 utf8encode(&utf8c, s);
801 /* keep any uncomplete utf8 char for the next call */
802 memmove(buf, ptr, buflen);
806 ttywrite(const char *s, size_t n) {
807 if(write(cmdfd, s, n) == -1)
808 die("write error on tty: %s\n", SERRNO);
812 ttyresize(int x, int y) {
817 w.ws_xpixel = w.ws_ypixel = 0;
818 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
819 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
823 tsetdirt(int top, int bot)
827 LIMIT(top, 0, term.row-1);
828 LIMIT(bot, 0, term.row-1);
830 for(i = top; i <= bot; i++)
837 tsetdirt(0, term.row-1);
844 if(mode == CURSOR_SAVE)
846 else if(mode == CURSOR_LOAD)
847 term.c = c, tmoveto(c.x, c.y);
856 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
858 term.top = 0, term.bot = term.row - 1;
859 term.mode = MODE_WRAP;
860 tclearregion(0, 0, term.col-1, term.row-1);
864 tnew(int col, int row) {
865 /* set screen size */
866 term.row = row, term.col = col;
867 term.line = malloc(term.row * sizeof(Line));
868 term.alt = malloc(term.row * sizeof(Line));
869 term.dirty = malloc(term.row * sizeof(*term.dirty));
871 for(row = 0; row < term.row; row++) {
872 term.line[row] = malloc(term.col * sizeof(Glyph));
873 term.alt [row] = malloc(term.col * sizeof(Glyph));
882 Line* tmp = term.line;
883 term.line = term.alt;
885 term.mode ^= MODE_ALTSCREEN;
890 tscrolldown(int orig, int n) {
894 LIMIT(n, 0, term.bot-orig+1);
896 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
898 for(i = term.bot; i >= orig+n; i--) {
900 term.line[i] = term.line[i-n];
901 term.line[i-n] = temp;
911 tscrollup(int orig, int n) {
914 LIMIT(n, 0, term.bot-orig+1);
916 tclearregion(0, orig, term.col-1, orig+n-1);
918 for(i = orig; i <= term.bot-n; i++) {
920 term.line[i] = term.line[i+n];
921 term.line[i+n] = temp;
931 selscroll(int orig, int n) {
935 if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
936 if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
940 if(sel.by < term.top) {
944 if(sel.ey > term.bot) {
948 sel.b.y = sel.by, sel.b.x = sel.bx;
949 sel.e.y = sel.ey, sel.e.x = sel.ex;
954 tnewline(int first_col) {
957 tscrollup(term.top, 1);
960 tmoveto(first_col ? 0 : term.c.x, y);
966 char *p = escseq.buf;
970 escseq.priv = 1, p++;
972 while(p < escseq.buf+escseq.len) {
974 escseq.arg[escseq.narg] *= 10;
975 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
977 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
988 tmoveto(int x, int y) {
989 LIMIT(x, 0, term.col-1);
990 LIMIT(y, 0, term.row-1);
991 term.c.state &= ~CURSOR_WRAPNEXT;
998 term.dirty[term.c.y] = 1;
999 term.line[term.c.y][term.c.x] = term.c.attr;
1000 memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
1001 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
1005 tclearregion(int x1, int y1, int x2, int y2) {
1009 temp = x1, x1 = x2, x2 = temp;
1011 temp = y1, y1 = y2, y2 = temp;
1013 LIMIT(x1, 0, term.col-1);
1014 LIMIT(x2, 0, term.col-1);
1015 LIMIT(y1, 0, term.row-1);
1016 LIMIT(y2, 0, term.row-1);
1018 for(y = y1; y <= y2; y++) {
1020 for(x = x1; x <= x2; x++)
1021 term.line[y][x].state = 0;
1026 tdeletechar(int n) {
1027 int src = term.c.x + n;
1029 int size = term.col - src;
1031 term.dirty[term.c.y] = 1;
1033 if(src >= term.col) {
1034 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1037 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
1038 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
1042 tinsertblank(int n) {
1045 int size = term.col - dst;
1047 term.dirty[term.c.y] = 1;
1049 if(dst >= term.col) {
1050 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1053 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
1054 tclearregion(src, term.c.y, dst - 1, term.c.y);
1058 tinsertblankline(int n) {
1059 if(term.c.y < term.top || term.c.y > term.bot)
1062 tscrolldown(term.c.y, n);
1066 tdeleteline(int n) {
1067 if(term.c.y < term.top || term.c.y > term.bot)
1070 tscrollup(term.c.y, n);
1074 tsetattr(int *attr, int l) {
1077 for(i = 0; i < l; i++) {
1080 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
1081 term.c.attr.fg = DefaultFG;
1082 term.c.attr.bg = DefaultBG;
1085 term.c.attr.mode |= ATTR_BOLD;
1088 term.c.attr.mode |= ATTR_UNDERLINE;
1091 term.c.attr.mode |= ATTR_REVERSE;
1094 term.c.attr.mode &= ~ATTR_BOLD;
1097 term.c.attr.mode &= ~ATTR_UNDERLINE;
1100 term.c.attr.mode &= ~ATTR_REVERSE;
1103 if(i + 2 < l && attr[i + 1] == 5) {
1105 if(BETWEEN(attr[i], 0, 255))
1106 term.c.attr.fg = attr[i];
1108 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
1111 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
1114 term.c.attr.fg = DefaultFG;
1117 if(i + 2 < l && attr[i + 1] == 5) {
1119 if(BETWEEN(attr[i], 0, 255))
1120 term.c.attr.bg = attr[i];
1122 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
1125 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
1128 term.c.attr.bg = DefaultBG;
1131 if(BETWEEN(attr[i], 30, 37))
1132 term.c.attr.fg = attr[i] - 30;
1133 else if(BETWEEN(attr[i], 40, 47))
1134 term.c.attr.bg = attr[i] - 40;
1135 else if(BETWEEN(attr[i], 90, 97))
1136 term.c.attr.fg = attr[i] - 90 + 8;
1137 else if(BETWEEN(attr[i], 100, 107))
1138 term.c.attr.fg = attr[i] - 100 + 8;
1140 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
1148 tsetscroll(int t, int b) {
1151 LIMIT(t, 0, term.row-1);
1152 LIMIT(b, 0, term.row-1);
1164 switch(escseq.mode) {
1167 fprintf(stderr, "erresc: unknown csi ");
1171 case '@': /* ICH -- Insert <n> blank char */
1172 DEFAULT(escseq.arg[0], 1);
1173 tinsertblank(escseq.arg[0]);
1175 case 'A': /* CUU -- Cursor <n> Up */
1177 DEFAULT(escseq.arg[0], 1);
1178 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
1180 case 'B': /* CUD -- Cursor <n> Down */
1181 DEFAULT(escseq.arg[0], 1);
1182 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
1184 case 'C': /* CUF -- Cursor <n> Forward */
1186 DEFAULT(escseq.arg[0], 1);
1187 tmoveto(term.c.x+escseq.arg[0], term.c.y);
1189 case 'D': /* CUB -- Cursor <n> Backward */
1190 DEFAULT(escseq.arg[0], 1);
1191 tmoveto(term.c.x-escseq.arg[0], term.c.y);
1193 case 'E': /* CNL -- Cursor <n> Down and first col */
1194 DEFAULT(escseq.arg[0], 1);
1195 tmoveto(0, term.c.y+escseq.arg[0]);
1197 case 'F': /* CPL -- Cursor <n> Up and first col */
1198 DEFAULT(escseq.arg[0], 1);
1199 tmoveto(0, term.c.y-escseq.arg[0]);
1201 case 'G': /* CHA -- Move to <col> */
1202 case '`': /* XXX: HPA -- same? */
1203 DEFAULT(escseq.arg[0], 1);
1204 tmoveto(escseq.arg[0]-1, term.c.y);
1206 case 'H': /* CUP -- Move to <row> <col> */
1207 case 'f': /* XXX: HVP -- same? */
1208 DEFAULT(escseq.arg[0], 1);
1209 DEFAULT(escseq.arg[1], 1);
1210 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
1212 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1213 case 'J': /* ED -- Clear screen */
1215 switch(escseq.arg[0]) {
1217 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1218 if(term.c.y < term.row-1)
1219 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1223 tclearregion(0, 0, term.col-1, term.c.y-1);
1224 tclearregion(0, term.c.y, term.c.x, term.c.y);
1227 tclearregion(0, 0, term.col-1, term.row-1);
1233 case 'K': /* EL -- Clear line */
1234 switch(escseq.arg[0]) {
1236 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1239 tclearregion(0, term.c.y, term.c.x, term.c.y);
1242 tclearregion(0, term.c.y, term.col-1, term.c.y);
1246 case 'S': /* SU -- Scroll <n> line up */
1247 DEFAULT(escseq.arg[0], 1);
1248 tscrollup(term.top, escseq.arg[0]);
1250 case 'T': /* SD -- Scroll <n> line down */
1251 DEFAULT(escseq.arg[0], 1);
1252 tscrolldown(term.top, escseq.arg[0]);
1254 case 'L': /* IL -- Insert <n> blank lines */
1255 DEFAULT(escseq.arg[0], 1);
1256 tinsertblankline(escseq.arg[0]);
1258 case 'l': /* RM -- Reset Mode */
1260 switch(escseq.arg[0]) {
1262 term.mode &= ~MODE_APPKEYPAD;
1264 case 5: /* DECSCNM -- Remove reverse video */
1265 if(IS_SET(MODE_REVERSE)) {
1266 term.mode &= ~MODE_REVERSE;
1271 term.mode &= ~MODE_WRAP;
1273 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1276 term.mode &= ~MODE_CRLF;
1279 term.c.state |= CURSOR_HIDE;
1281 case 1000: /* disable X11 xterm mouse reporting */
1282 term.mode &= ~MODE_MOUSEBTN;
1285 term.mode &= ~MODE_MOUSEMOTION;
1287 case 1049: /* = 1047 and 1048 */
1290 if(IS_SET(MODE_ALTSCREEN)) {
1291 tclearregion(0, 0, term.col-1, term.row-1);
1294 if(escseq.arg[0] != 1049)
1297 tcursor(CURSOR_LOAD);
1303 switch(escseq.arg[0]) {
1305 term.mode &= ~MODE_INSERT;
1312 case 'M': /* DL -- Delete <n> lines */
1313 DEFAULT(escseq.arg[0], 1);
1314 tdeleteline(escseq.arg[0]);
1316 case 'X': /* ECH -- Erase <n> char */
1317 DEFAULT(escseq.arg[0], 1);
1318 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
1320 case 'P': /* DCH -- Delete <n> char */
1321 DEFAULT(escseq.arg[0], 1);
1322 tdeletechar(escseq.arg[0]);
1324 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1325 case 'd': /* VPA -- Move to <row> */
1326 DEFAULT(escseq.arg[0], 1);
1327 tmoveto(term.c.x, escseq.arg[0]-1);
1329 case 'h': /* SM -- Set terminal mode */
1331 switch(escseq.arg[0]) {
1333 term.mode |= MODE_APPKEYPAD;
1335 case 5: /* DECSCNM -- Reverve video */
1336 if(!IS_SET(MODE_REVERSE)) {
1337 term.mode |= MODE_REVERSE;
1342 term.mode |= MODE_WRAP;
1345 term.mode |= MODE_CRLF;
1347 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1348 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1349 if(escseq.narg > 1 && escseq.arg[1] != 25)
1352 term.c.state &= ~CURSOR_HIDE;
1354 case 1000: /* 1000,1002: enable xterm mouse report */
1355 term.mode |= MODE_MOUSEBTN;
1358 term.mode |= MODE_MOUSEMOTION;
1360 case 1049: /* = 1047 and 1048 */
1363 if(IS_SET(MODE_ALTSCREEN))
1364 tclearregion(0, 0, term.col-1, term.row-1);
1367 if(escseq.arg[0] != 1049)
1370 tcursor(CURSOR_SAVE);
1372 default: goto unknown;
1375 switch(escseq.arg[0]) {
1377 term.mode |= MODE_INSERT;
1379 default: goto unknown;
1383 case 'm': /* SGR -- Terminal attribute (color) */
1384 tsetattr(escseq.arg, escseq.narg);
1386 case 'r': /* DECSTBM -- Set Scrolling Region */
1390 DEFAULT(escseq.arg[0], 1);
1391 DEFAULT(escseq.arg[1], term.row);
1392 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
1396 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1397 tcursor(CURSOR_SAVE);
1399 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1400 tcursor(CURSOR_LOAD);
1409 for(i = 0; i < escseq.len; i++) {
1410 uint c = escseq.buf[i] & 0xff;
1411 if(isprint(c)) putchar(c);
1412 else if(c == '\n') printf("(\\n)");
1413 else if(c == '\r') printf("(\\r)");
1414 else if(c == 0x1b) printf("(\\e)");
1415 else printf("(%02x)", c);
1422 memset(&escseq, 0, sizeof(escseq));
1427 int space = TAB - term.c.x % TAB;
1428 tmoveto(term.c.x + space, term.c.y);
1434 if(term.esc & ESC_START) {
1435 if(term.esc & ESC_CSI) {
1436 escseq.buf[escseq.len++] = ascii;
1437 if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
1439 csiparse(), csihandle();
1441 /* TODO: handle other OSC */
1442 } else if(term.esc & ESC_OSC) {
1445 term.esc = ESC_START | ESC_TITLE;
1447 } else if(term.esc & ESC_TITLE) {
1448 if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
1450 term.title[term.titlelen] = '\0';
1451 XStoreName(xw.dpy, xw.win, term.title);
1453 term.title[term.titlelen++] = ascii;
1455 } else if(term.esc & ESC_ALTCHARSET) {
1457 case '0': /* Line drawing crap */
1458 term.c.attr.mode |= ATTR_GFX;
1460 case 'B': /* Back to regular text */
1461 term.c.attr.mode &= ~ATTR_GFX;
1464 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1470 term.esc |= ESC_CSI;
1473 term.esc |= ESC_OSC;
1476 term.esc |= ESC_ALTCHARSET;
1478 case 'D': /* IND -- Linefeed */
1479 if(term.c.y == term.bot)
1480 tscrollup(term.top, 1);
1482 tmoveto(term.c.x, term.c.y+1);
1485 case 'E': /* NEL -- Next line */
1486 tnewline(1); /* always go to first col */
1489 case 'M': /* RI -- Reverse index */
1490 if(term.c.y == term.top)
1491 tscrolldown(term.top, 1);
1493 tmoveto(term.c.x, term.c.y-1);
1496 case 'c': /* RIS -- Reset to inital state */
1500 case '=': /* DECPAM -- Application keypad */
1501 term.mode |= MODE_APPKEYPAD;
1504 case '>': /* DECPNM -- Normal keypad */
1505 term.mode &= ~MODE_APPKEYPAD;
1508 case '7': /* DECSC -- Save Cursor */
1509 tcursor(CURSOR_SAVE);
1512 case '8': /* DECRC -- Restore Cursor */
1513 tcursor(CURSOR_LOAD);
1517 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1518 (uchar) ascii, isprint(ascii)?ascii:'.');
1523 if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
1530 tmoveto(term.c.x-1, term.c.y);
1533 tmoveto(0, term.c.y);
1538 /* go to first col if the mode is set */
1539 tnewline(IS_SET(MODE_CRLF));
1542 if(!(xw.state & WIN_FOCUSED))
1547 term.esc = ESC_START;
1550 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1551 tnewline(1); /* always go to first col */
1553 if(term.c.x+1 < term.col)
1554 tmoveto(term.c.x+1, term.c.y);
1556 term.c.state |= CURSOR_WRAPNEXT;
1562 tresize(int col, int row) {
1564 int minrow = MIN(row, term.row);
1565 int mincol = MIN(col, term.col);
1566 int slide = term.c.y - row + 1;
1568 if(col < 1 || row < 1)
1571 /* free unneeded rows */
1574 /* slide screen to keep cursor where we expect it -
1575 * tscrollup would work here, but we can optimize to
1576 * memmove because we're freeing the earlier lines */
1577 for(/* i = 0 */; i < slide; i++) {
1581 memmove(term.line, term.line + slide, row * sizeof(Line));
1582 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1584 for(i += row; i < term.row; i++) {
1589 /* resize to new height */
1590 term.line = realloc(term.line, row * sizeof(Line));
1591 term.alt = realloc(term.alt, row * sizeof(Line));
1592 term.dirty = realloc(term.dirty, row * sizeof(*term.dirty));
1594 /* resize each row to new width, zero-pad if needed */
1595 for(i = 0; i < minrow; i++) {
1597 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1598 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1599 for(x = mincol; x < col; x++) {
1600 term.line[i][x].state = 0;
1601 term.alt[i][x].state = 0;
1605 /* allocate any new rows */
1606 for(/* i == minrow */; i < row; i++) {
1608 term.line[i] = calloc(col, sizeof(Glyph));
1609 term.alt [i] = calloc(col, sizeof(Glyph));
1612 /* update terminal size */
1613 term.col = col, term.row = row;
1614 /* make use of the LIMIT in tmoveto */
1615 tmoveto(term.c.x, term.c.y);
1616 /* reset scrolling region */
1617 tsetscroll(0, row-1);
1623 xresize(int col, int row) {
1624 xw.bufw = MAX(1, col * xw.cw);
1625 xw.bufh = MAX(1, row * xw.ch);
1632 ulong white = WhitePixel(xw.dpy, xw.scr);
1634 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1635 for(i = 0; i < LEN(colorname); i++) {
1638 if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1640 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1642 dc.col[i] = color.pixel;
1645 /* load colors [16-255] ; same colors as xterm */
1646 for(i = 16, r = 0; r < 6; r++)
1647 for(g = 0; g < 6; g++)
1648 for(b = 0; b < 6; b++) {
1649 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1650 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1651 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1652 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1654 fprintf(stderr, "Could not allocate color %d\n", i);
1656 dc.col[i] = color.pixel;
1660 for(r = 0; r < 24; r++, i++) {
1661 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1662 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1664 fprintf(stderr, "Could not allocate color %d\n", i);
1666 dc.col[i] = color.pixel;
1671 xclear(int x1, int y1, int x2, int y2) {
1672 XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
1673 XFillRectangle(xw.dpy, xw.buf, dc.gc,
1674 x1 * xw.cw, y1 * xw.ch,
1675 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1680 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1681 XWMHints wm = {.flags = InputHint, .input = 1};
1683 .flags = PSize | PResizeInc | PBaseSize,
1686 .height_inc = xw.ch,
1688 .base_height = 2*BORDER,
1689 .base_width = 2*BORDER,
1691 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1695 xinitfont(char *fontstr) {
1697 char *def, **missing;
1701 set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1704 fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1705 XFreeStringList(missing);
1711 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
1712 XFontStruct **xfonts;
1716 *ascent = *descent = *lbearing = *rbearing = 0;
1717 n = XFontsOfFontSet(set, &xfonts, &font_names);
1718 for(i = 0; i < n; i++) {
1719 *ascent = MAX(*ascent, (*xfonts)->ascent);
1720 *descent = MAX(*descent, (*xfonts)->descent);
1721 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1722 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1728 initfonts(char *fontstr, char *bfontstr) {
1729 if((dc.font.set = xinitfont(fontstr)) == NULL ||
1730 (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1731 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1732 xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1733 &dc.font.lbearing, &dc.font.rbearing);
1734 xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1735 &dc.bfont.lbearing, &dc.bfont.rbearing);
1740 XSetWindowAttributes attrs;
1744 if(!(xw.dpy = XOpenDisplay(NULL)))
1745 die("Can't open display\n");
1746 xw.scr = XDefaultScreen(xw.dpy);
1749 initfonts(FONT, BOLDFONT);
1751 /* XXX: Assuming same size for bold font */
1752 xw.cw = dc.font.rbearing - dc.font.lbearing;
1753 xw.ch = dc.font.ascent + dc.font.descent;
1756 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1759 /* window - default size */
1760 xw.bufh = term.row * xw.ch;
1761 xw.bufw = term.col * xw.cw;
1762 xw.h = xw.bufh + 2*BORDER;
1763 xw.w = xw.bufw + 2*BORDER;
1765 attrs.background_pixel = dc.col[DefaultBG];
1766 attrs.border_pixel = dc.col[DefaultBG];
1767 attrs.bit_gravity = NorthWestGravity;
1768 attrs.event_mask = FocusChangeMask | KeyPressMask
1769 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1770 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
1771 | EnterWindowMask | LeaveWindowMask;
1772 attrs.colormap = xw.cmap;
1774 parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
1775 xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
1776 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1777 XDefaultVisual(xw.dpy, xw.scr),
1778 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1781 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
1785 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1786 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1787 | XIMStatusNothing, XNClientWindow, xw.win,
1788 XNFocusWindow, xw.win, NULL);
1790 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1792 /* white cursor, black outline */
1793 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
1794 XDefineCursor(xw.dpy, xw.win, cursor);
1795 XRecolorCursor(xw.dpy, cursor,
1796 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
1797 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
1799 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
1801 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1802 XMapWindow(xw.dpy, xw.win);
1808 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1809 int fg = base.fg, bg = base.bg, temp;
1810 int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1811 XFontSet fontset = dc.font.set;
1814 /* only switch default fg/bg if term is in RV mode */
1815 if(IS_SET(MODE_REVERSE)) {
1822 if(base.mode & ATTR_REVERSE)
1823 temp = fg, fg = bg, bg = temp;
1825 if(base.mode & ATTR_BOLD) {
1827 fontset = dc.bfont.set;
1830 XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
1831 XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
1833 if(base.mode & ATTR_GFX) {
1834 for(i = 0; i < bytelen; i++) {
1835 char c = gfx[(uint)s[i] % 256];
1838 else if(s[i] > 0x5f)
1843 XmbDrawImageString(xw.dpy, xw.buf, fontset, dc.gc, winx, winy, s, bytelen);
1845 if(base.mode & ATTR_UNDERLINE)
1846 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1849 /* copy buffer pixmap to screen pixmap */
1852 XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
1853 XdbeSwapBuffers(xw.dpy, swpinfo, 1);
1859 static int oldx = 0;
1860 static int oldy = 0;
1862 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
1864 LIMIT(oldx, 0, term.col-1);
1865 LIMIT(oldy, 0, term.row-1);
1867 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1868 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
1870 /* remove the old cursor */
1871 if(term.line[oldy][oldx].state & GLYPH_SET) {
1872 sl = utf8size(term.line[oldy][oldx].c);
1873 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
1875 xclear(oldx, oldy, oldx, oldy);
1877 xcopy(oldx, oldy, 1, 1);
1879 /* draw the new one */
1880 if(!(term.c.state & CURSOR_HIDE)) {
1881 if(!(xw.state & WIN_FOCUSED))
1884 if(IS_SET(MODE_REVERSE))
1885 g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
1888 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
1889 oldx = term.c.x, oldy = term.c.y;
1892 xcopy(term.c.x, term.c.y, 1, 1);
1897 drawregion(0, 0, term.col, term.row);
1899 gettimeofday(&xw.lastdraw, NULL);
1903 drawregion(int x1, int y1, int x2, int y2) {
1904 int ic, ib, x, y, ox, sl;
1906 char buf[DRAW_BUF_SIZ];
1908 if(!(xw.state & WIN_VISIBLE))
1911 for(y = y1; y < y2; y++) {
1914 xclear(0, y, term.col, y);
1916 base = term.line[y][0];
1918 for(x = x1; x < x2; x++) {
1919 new = term.line[y][x];
1920 if(sel.bx != -1 && *(new.c) && selected(x, y))
1921 new.mode ^= ATTR_REVERSE;
1922 if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1923 ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
1924 xdraws(buf, base, ox, y, ic, ib);
1927 if(new.state & GLYPH_SET) {
1932 sl = utf8size(new.c);
1933 memcpy(buf+ib, new.c, sl);
1939 xdraws(buf, base, ox, y, ic, ib);
1945 expose(XEvent *ev) {
1946 XExposeEvent *e = &ev->xexpose;
1947 if(xw.state & WIN_REDRAW) {
1949 xw.state &= ~WIN_REDRAW;
1955 visibility(XEvent *ev) {
1956 XVisibilityEvent *e = &ev->xvisibility;
1957 if(e->state == VisibilityFullyObscured)
1958 xw.state &= ~WIN_VISIBLE;
1959 else if(!(xw.state & WIN_VISIBLE))
1960 /* need a full redraw for next Expose, not just a buf copy */
1961 xw.state |= WIN_VISIBLE | WIN_REDRAW;
1966 xw.state &= ~WIN_VISIBLE;
1970 xseturgency(int add) {
1971 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1972 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1973 XSetWMHints(xw.dpy, xw.win, h);
1979 if(ev->type == FocusIn) {
1980 xw.state |= WIN_FOCUSED;
1983 xw.state &= ~WIN_FOCUSED;
1988 kmap(KeySym k, uint state) {
1991 for(i = 0; i < LEN(key); i++) {
1992 uint mask = key[i].mask;
1993 if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
1994 return (char*)key[i].s;
2000 kpress(XEvent *ev) {
2001 XKeyEvent *e = &ev->xkey;
2010 meta = e->state & Mod1Mask;
2011 shift = e->state & ShiftMask;
2012 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
2014 /* 1. custom keys from config.h */
2015 if((customkey = kmap(ksym, e->state)))
2016 ttywrite(customkey, strlen(customkey));
2017 /* 2. hardcoded (overrides X lookup) */
2024 /* XXX: shift up/down doesn't work */
2025 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
2033 if(IS_SET(MODE_CRLF))
2034 ttywrite("\r\n", 2);
2041 if(meta && len == 1)
2042 ttywrite("\033", 1);
2050 cmessage(XEvent *e) {
2052 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2053 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
2054 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
2055 xw.state |= WIN_FOCUSED;
2057 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
2058 xw.state &= ~WIN_FOCUSED;
2068 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
2071 xw.w = e->xconfigure.width;
2072 xw.h = e->xconfigure.height;
2073 col = (xw.w - 2*BORDER) / xw.cw;
2074 row = (xw.h - 2*BORDER) / xw.ch;
2075 if(col == term.col && row == term.row)
2077 if(tresize(col, row))
2079 ttyresize(col, row);
2084 last_draw_too_old(void) {
2086 gettimeofday(&now, NULL);
2087 return TIMEDIFF(now, xw.lastdraw) >= DRAW_TIMEOUT/1000;
2094 int xfd = XConnectionNumber(xw.dpy);
2095 struct timeval timeout = {0};
2096 bool stuff_to_print = 0;
2100 FD_SET(cmdfd, &rfd);
2103 timeout.tv_usec = SELECT_TIMEOUT;
2104 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, &timeout) < 0) {
2107 die("select failed: %s\n", SERRNO);
2109 if(FD_ISSET(cmdfd, &rfd)) {
2114 if(stuff_to_print && last_draw_too_old()) {
2119 while(XPending(xw.dpy)) {
2120 XNextEvent(xw.dpy, &ev);
2121 if(XFilterEvent(&ev, xw.win))
2123 if(handler[ev.type])
2124 (handler[ev.type])(&ev);
2130 main(int argc, char *argv[]) {
2133 for(i = 1; i < argc; i++) {
2134 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
2136 if(++i < argc) opt_title = argv[i];
2139 if(++i < argc) opt_class = argv[i];
2142 if(++i < argc) opt_embed = argv[i];
2145 /* eat every remaining arguments */
2146 if(++i < argc) opt_cmd = &argv[i];
2155 setlocale(LC_CTYPE, "");