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];
178 /* Purely graphic info */
188 int w; /* window width */
189 int h; /* window height */
190 int ch; /* char height */
191 int cw; /* char width */
192 char state; /* focus, redraw, visible */
193 struct timeval lastdraw;
203 /* TODO: use better name for vars... */
208 struct {int x, y;} b, e;
211 struct timeval tclick1;
212 struct timeval tclick2;
217 /* Drawing Context */
219 ulong col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
230 static void die(const char*, ...);
231 static void draw(void);
232 static void drawregion(int, int, int, int);
233 static void execsh(void);
234 static void sigchld(int);
235 static void run(void);
236 static bool last_draw_too_old(void);
238 static void csidump(void);
239 static void csihandle(void);
240 static void csiparse(void);
241 static void csireset(void);
243 static void tclearregion(int, int, int, int);
244 static void tcursor(int);
245 static void tdeletechar(int);
246 static void tdeleteline(int);
247 static void tinsertblank(int);
248 static void tinsertblankline(int);
249 static void tmoveto(int, int);
250 static void tnew(int, int);
251 static void tnewline(int);
252 static void tputtab(void);
253 static void tputc(char*);
254 static void treset(void);
255 static int tresize(int, int);
256 static void tscrollup(int, int);
257 static void tscrolldown(int, int);
258 static void tsetattr(int*, int);
259 static void tsetchar(char*);
260 static void tsetscroll(int, int);
261 static void tswapscreen(void);
262 static void tsetdirt(int, int);
263 static void tfulldirt(void);
265 static void ttynew(void);
266 static void ttyread(void);
267 static void ttyresize(int, int);
268 static void ttywrite(const char *, size_t);
270 static void xdraws(char *, Glyph, int, int, int, int);
271 static void xhints(void);
272 static void xclear(int, int, int, int);
274 static void xdrawcursor(void);
275 static void xinit(void);
276 static void xloadcols(void);
277 static void xseturgency(int);
278 static void xsetsel(char*);
279 static void xresize(int, int);
281 static void expose(XEvent *);
282 static void visibility(XEvent *);
283 static void unmap(XEvent *);
284 static char* kmap(KeySym, uint);
285 static void kpress(XEvent *);
286 static void cmessage(XEvent *);
287 static void resize(XEvent *);
288 static void focus(XEvent *);
289 static void brelease(XEvent *);
290 static void bpress(XEvent *);
291 static void bmotion(XEvent *);
292 static void selnotify(XEvent *);
293 static void selrequest(XEvent *);
295 static void selinit(void);
296 static inline bool selected(int, int);
297 static void selcopy(void);
298 static void selpaste();
299 static void selscroll(int, int);
301 static int utf8decode(char *, long *);
302 static int utf8encode(long *, char *);
303 static int utf8size(char *);
304 static int isfullutf8(char *, int);
306 static void (*handler[LASTEvent])(XEvent *) = {
308 [ClientMessage] = cmessage,
309 [ConfigureNotify] = resize,
310 [VisibilityNotify] = visibility,
311 [UnmapNotify] = unmap,
315 [MotionNotify] = bmotion,
316 [ButtonPress] = bpress,
317 [ButtonRelease] = brelease,
318 [SelectionNotify] = selnotify,
319 [SelectionRequest] = selrequest,
326 static CSIEscape escseq;
329 static Selection sel;
330 static char **opt_cmd = NULL;
331 static char *opt_title = NULL;
332 static char *opt_embed = NULL;
333 static char *opt_class = NULL;
336 utf8decode(char *s, long *u) {
342 if(~c & B7) { /* 0xxxxxxx */
345 } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
346 *u = c&(B4|B3|B2|B1|B0);
348 } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
349 *u = c&(B3|B2|B1|B0);
351 } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
356 for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
358 if((c & (B7|B6)) != B7) /* 10xxxxxx */
361 *u |= c & (B5|B4|B3|B2|B1|B0);
363 if((n == 1 && *u < 0x80) ||
364 (n == 2 && *u < 0x800) ||
365 (n == 3 && *u < 0x10000) ||
366 (*u >= 0xD800 && *u <= 0xDFFF))
375 utf8encode(long *u, char *s) {
383 *sp = uc; /* 0xxxxxxx */
385 } else if(*u < 0x800) {
386 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
388 } else if(uc < 0x10000) {
389 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
391 } else if(uc <= 0x10FFFF) {
392 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
397 for(i=n,++sp; i>0; --i,++sp)
398 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
408 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
409 UTF-8 otherwise return 0 */
411 isfullutf8(char *s, int b) {
419 else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
421 else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
423 ((b == 2) && (*c2&(B7|B6)) == B7)))
425 else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
427 ((b == 2) && (*c2&(B7|B6)) == B7) ||
428 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
440 else if((c&(B7|B6|B5)) == (B7|B6))
442 else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
450 memset(&sel.tclick1, 0, sizeof(sel.tclick1));
451 memset(&sel.tclick2, 0, sizeof(sel.tclick2));
455 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
456 if(sel.xtarget == None)
457 sel.xtarget = XA_STRING;
461 selected(int x, int y) {
462 if(sel.ey == y && sel.by == y) {
463 int bx = MIN(sel.bx, sel.ex);
464 int ex = MAX(sel.bx, sel.ex);
465 return BETWEEN(x, bx, ex);
467 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
468 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
472 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
474 *b = e->xbutton.button;
476 *x = X2COL(e->xbutton.x);
477 *y = Y2ROW(e->xbutton.y);
478 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
479 sel.b.y = MIN(sel.by, sel.ey);
480 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
481 sel.e.y = MAX(sel.by, sel.ey);
485 mousereport(XEvent *e) {
486 int x = X2COL(e->xbutton.x);
487 int y = Y2ROW(e->xbutton.y);
488 int button = e->xbutton.button;
489 int state = e->xbutton.state;
490 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
491 static int ob, ox, oy;
494 if(e->xbutton.type == MotionNotify) {
495 if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
499 } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
505 if(e->xbutton.type == ButtonPress) {
511 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
512 + (state & Mod4Mask ? 8 : 0)
513 + (state & ControlMask ? 16 : 0);
515 ttywrite(buf, sizeof(buf));
520 if(IS_SET(MODE_MOUSE))
522 else if(e->xbutton.button == Button1) {
524 tsetdirt(sel.b.y, sel.e.y);
526 sel.ex = sel.bx = X2COL(e->xbutton.x);
527 sel.ey = sel.by = Y2ROW(e->xbutton.y);
534 int x, y, bufsize, is_selected = 0;
540 bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
541 ptr = str = malloc(bufsize);
543 /* append every set & selected glyph to the selection */
544 for(y = 0; y < term.row; y++) {
545 for(x = 0; x < term.col; x++) {
546 is_selected = selected(x, y);
547 if((term.line[y][x].state & GLYPH_SET) && is_selected) {
548 int size = utf8size(term.line[y][x].c);
549 memcpy(ptr, term.line[y][x].c, size);
554 /* \n at the end of every selected line except for the last one */
555 if(is_selected && y < sel.e.y)
564 selnotify(XEvent *e) {
565 ulong nitems, ofs, rem;
572 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
573 False, AnyPropertyType, &type, &format,
574 &nitems, &rem, &data)) {
575 fprintf(stderr, "Clipboard allocation failed\n");
578 ttywrite((const char *) data, nitems * format / 8);
580 /* number of 32-bit chunks returned */
581 ofs += nitems * format / 32;
587 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
591 selrequest(XEvent *e) {
592 XSelectionRequestEvent *xsre;
596 xsre = (XSelectionRequestEvent *) e;
597 xev.type = SelectionNotify;
598 xev.requestor = xsre->requestor;
599 xev.selection = xsre->selection;
600 xev.target = xsre->target;
601 xev.time = xsre->time;
605 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
606 if(xsre->target == xa_targets) {
607 /* respond with the supported type */
608 Atom string = sel.xtarget;
609 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
610 XA_ATOM, 32, PropModeReplace,
611 (uchar *) &string, 1);
612 xev.property = xsre->property;
613 } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
614 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
615 xsre->target, 8, PropModeReplace,
616 (uchar *) sel.clip, strlen(sel.clip));
617 xev.property = xsre->property;
620 /* all done, send a notification to the listener */
621 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
622 fprintf(stderr, "Error sending SelectionNotify event\n");
627 /* register the selection for both the clipboard and the primary */
633 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
635 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
636 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
642 brelease(XEvent *e) {
643 if(IS_SET(MODE_MOUSE)) {
647 if(e->xbutton.button == Button2)
649 else if(e->xbutton.button == Button1) {
651 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
652 term.dirty[sel.ey] = 1;
653 if(sel.bx == sel.ex && sel.by == sel.ey) {
656 gettimeofday(&now, NULL);
658 if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
659 /* triple click on the line */
660 sel.b.x = sel.bx = 0;
661 sel.e.x = sel.ex = term.col;
662 sel.b.y = sel.e.y = sel.ey;
664 } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
665 /* double click to select word */
667 while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
668 term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
670 while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
671 term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
673 sel.b.y = sel.e.y = sel.ey;
679 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
680 gettimeofday(&sel.tclick1, NULL);
686 if(IS_SET(MODE_MOUSE)) {
691 int oldey = sel.ey, oldex = sel.ex;
692 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
694 if(oldey != sel.ey || oldex != sel.ex) {
695 int starty = MIN(oldey, sel.ey);
696 int endy = MAX(oldey, sel.ey);
697 tsetdirt(starty, endy);
704 die(const char *errstr, ...) {
707 va_start(ap, errstr);
708 vfprintf(stderr, errstr, ap);
716 char *envshell = getenv("SHELL");
718 DEFAULT(envshell, SHELL);
719 putenv("TERM="TNAME);
720 args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
721 execvp(args[0], args);
728 if(waitpid(pid, &stat, 0) < 0)
729 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
731 exit(WEXITSTATUS(stat));
740 /* seems to work fine on linux, openbsd and freebsd */
741 struct winsize w = {term.row, term.col, 0, 0};
742 if(openpty(&m, &s, NULL, NULL, &w) < 0)
743 die("openpty failed: %s\n", SERRNO);
745 switch(pid = fork()) {
747 die("fork failed\n");
750 setsid(); /* create a new process group */
751 dup2(s, STDIN_FILENO);
752 dup2(s, STDOUT_FILENO);
753 dup2(s, STDERR_FILENO);
754 if(ioctl(s, TIOCSCTTY, NULL) < 0)
755 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
763 signal(SIGCHLD, sigchld);
770 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
772 fprintf(stderr, "\n");
777 static char buf[BUFSIZ];
778 static int buflen = 0;
781 int charsize; /* size of utf8 char in bytes */
785 /* append read bytes to unprocessed bytes */
786 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
787 die("Couldn't read from shell: %s\n", SERRNO);
789 /* process every complete utf8 char */
792 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
793 charsize = utf8decode(ptr, &utf8c);
794 utf8encode(&utf8c, s);
800 /* keep any uncomplete utf8 char for the next call */
801 memmove(buf, ptr, buflen);
805 ttywrite(const char *s, size_t n) {
806 if(write(cmdfd, s, n) == -1)
807 die("write error on tty: %s\n", SERRNO);
811 ttyresize(int x, int y) {
816 w.ws_xpixel = w.ws_ypixel = 0;
817 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
818 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
822 tsetdirt(int top, int bot)
826 LIMIT(top, 0, term.row-1);
827 LIMIT(bot, 0, term.row-1);
829 for(i = top; i <= bot; i++)
836 tsetdirt(0, term.row-1);
843 if(mode == CURSOR_SAVE)
845 else if(mode == CURSOR_LOAD)
846 term.c = c, tmoveto(c.x, c.y);
856 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
858 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
859 for (i = TAB; i < term.col; i += TAB)
861 term.top = 0, term.bot = term.row - 1;
862 term.mode = MODE_WRAP;
863 tclearregion(0, 0, term.col-1, term.row-1);
867 tnew(int col, int row) {
868 /* set screen size */
869 term.row = row, term.col = col;
870 term.line = malloc(term.row * sizeof(Line));
871 term.alt = malloc(term.row * sizeof(Line));
872 term.dirty = malloc(term.row * sizeof(*term.dirty));
873 term.tabs = malloc(term.col * sizeof(*term.tabs));
875 for(row = 0; row < term.row; row++) {
876 term.line[row] = malloc(term.col * sizeof(Glyph));
877 term.alt [row] = malloc(term.col * sizeof(Glyph));
880 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
887 Line* tmp = term.line;
888 term.line = term.alt;
890 term.mode ^= MODE_ALTSCREEN;
895 tscrolldown(int orig, int n) {
899 LIMIT(n, 0, term.bot-orig+1);
901 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
903 for(i = term.bot; i >= orig+n; i--) {
905 term.line[i] = term.line[i-n];
906 term.line[i-n] = temp;
916 tscrollup(int orig, int n) {
919 LIMIT(n, 0, term.bot-orig+1);
921 tclearregion(0, orig, term.col-1, orig+n-1);
923 for(i = orig; i <= term.bot-n; i++) {
925 term.line[i] = term.line[i+n];
926 term.line[i+n] = temp;
936 selscroll(int orig, int n) {
940 if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
941 if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
945 if(sel.by < term.top) {
949 if(sel.ey > term.bot) {
953 sel.b.y = sel.by, sel.b.x = sel.bx;
954 sel.e.y = sel.ey, sel.e.x = sel.ex;
959 tnewline(int first_col) {
962 tscrollup(term.top, 1);
965 tmoveto(first_col ? 0 : term.c.x, y);
971 char *p = escseq.buf;
975 escseq.priv = 1, p++;
977 while(p < escseq.buf+escseq.len) {
979 escseq.arg[escseq.narg] *= 10;
980 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
982 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
993 tmoveto(int x, int y) {
994 LIMIT(x, 0, term.col-1);
995 LIMIT(y, 0, term.row-1);
996 term.c.state &= ~CURSOR_WRAPNEXT;
1003 term.dirty[term.c.y] = 1;
1004 term.line[term.c.y][term.c.x] = term.c.attr;
1005 memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
1006 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
1010 tclearregion(int x1, int y1, int x2, int y2) {
1014 temp = x1, x1 = x2, x2 = temp;
1016 temp = y1, y1 = y2, y2 = temp;
1018 LIMIT(x1, 0, term.col-1);
1019 LIMIT(x2, 0, term.col-1);
1020 LIMIT(y1, 0, term.row-1);
1021 LIMIT(y2, 0, term.row-1);
1023 for(y = y1; y <= y2; y++) {
1025 for(x = x1; x <= x2; x++)
1026 term.line[y][x].state = 0;
1031 tdeletechar(int n) {
1032 int src = term.c.x + n;
1034 int size = term.col - src;
1036 term.dirty[term.c.y] = 1;
1038 if(src >= term.col) {
1039 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1042 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
1043 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
1047 tinsertblank(int n) {
1050 int size = term.col - dst;
1052 term.dirty[term.c.y] = 1;
1054 if(dst >= term.col) {
1055 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1058 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
1059 tclearregion(src, term.c.y, dst - 1, term.c.y);
1063 tinsertblankline(int n) {
1064 if(term.c.y < term.top || term.c.y > term.bot)
1067 tscrolldown(term.c.y, n);
1071 tdeleteline(int n) {
1072 if(term.c.y < term.top || term.c.y > term.bot)
1075 tscrollup(term.c.y, n);
1079 tsetattr(int *attr, int l) {
1082 for(i = 0; i < l; i++) {
1085 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
1086 term.c.attr.fg = DefaultFG;
1087 term.c.attr.bg = DefaultBG;
1090 term.c.attr.mode |= ATTR_BOLD;
1093 term.c.attr.mode |= ATTR_UNDERLINE;
1096 term.c.attr.mode |= ATTR_REVERSE;
1099 term.c.attr.mode &= ~ATTR_BOLD;
1102 term.c.attr.mode &= ~ATTR_UNDERLINE;
1105 term.c.attr.mode &= ~ATTR_REVERSE;
1108 if(i + 2 < l && attr[i + 1] == 5) {
1110 if(BETWEEN(attr[i], 0, 255))
1111 term.c.attr.fg = attr[i];
1113 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
1116 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
1119 term.c.attr.fg = DefaultFG;
1122 if(i + 2 < l && attr[i + 1] == 5) {
1124 if(BETWEEN(attr[i], 0, 255))
1125 term.c.attr.bg = attr[i];
1127 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
1130 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
1133 term.c.attr.bg = DefaultBG;
1136 if(BETWEEN(attr[i], 30, 37))
1137 term.c.attr.fg = attr[i] - 30;
1138 else if(BETWEEN(attr[i], 40, 47))
1139 term.c.attr.bg = attr[i] - 40;
1140 else if(BETWEEN(attr[i], 90, 97))
1141 term.c.attr.fg = attr[i] - 90 + 8;
1142 else if(BETWEEN(attr[i], 100, 107))
1143 term.c.attr.fg = attr[i] - 100 + 8;
1145 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
1153 tsetscroll(int t, int b) {
1156 LIMIT(t, 0, term.row-1);
1157 LIMIT(b, 0, term.row-1);
1169 switch(escseq.mode) {
1172 fprintf(stderr, "erresc: unknown csi ");
1176 case '@': /* ICH -- Insert <n> blank char */
1177 DEFAULT(escseq.arg[0], 1);
1178 tinsertblank(escseq.arg[0]);
1180 case 'A': /* CUU -- Cursor <n> Up */
1182 DEFAULT(escseq.arg[0], 1);
1183 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
1185 case 'B': /* CUD -- Cursor <n> Down */
1186 DEFAULT(escseq.arg[0], 1);
1187 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
1189 case 'C': /* CUF -- Cursor <n> Forward */
1191 DEFAULT(escseq.arg[0], 1);
1192 tmoveto(term.c.x+escseq.arg[0], term.c.y);
1194 case 'D': /* CUB -- Cursor <n> Backward */
1195 DEFAULT(escseq.arg[0], 1);
1196 tmoveto(term.c.x-escseq.arg[0], term.c.y);
1198 case 'E': /* CNL -- Cursor <n> Down and first col */
1199 DEFAULT(escseq.arg[0], 1);
1200 tmoveto(0, term.c.y+escseq.arg[0]);
1202 case 'F': /* CPL -- Cursor <n> Up and first col */
1203 DEFAULT(escseq.arg[0], 1);
1204 tmoveto(0, term.c.y-escseq.arg[0]);
1206 case 'g': /* TBC -- Tabulation clear */
1207 switch (escseq.arg[0]) {
1208 case 0: /* clear current tab stop */
1209 term.tabs[term.c.x] = 0;
1211 case 3: /* clear all the tabs */
1212 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
1218 case 'G': /* CHA -- Move to <col> */
1219 case '`': /* XXX: HPA -- same? */
1220 DEFAULT(escseq.arg[0], 1);
1221 tmoveto(escseq.arg[0]-1, term.c.y);
1223 case 'H': /* CUP -- Move to <row> <col> */
1224 case 'f': /* XXX: HVP -- same? */
1225 DEFAULT(escseq.arg[0], 1);
1226 DEFAULT(escseq.arg[1], 1);
1227 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
1229 case 'J': /* ED -- Clear screen */
1231 switch(escseq.arg[0]) {
1233 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1234 if(term.c.y < term.row-1)
1235 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1239 tclearregion(0, 0, term.col-1, term.c.y-1);
1240 tclearregion(0, term.c.y, term.c.x, term.c.y);
1243 tclearregion(0, 0, term.col-1, term.row-1);
1249 case 'K': /* EL -- Clear line */
1250 switch(escseq.arg[0]) {
1252 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1255 tclearregion(0, term.c.y, term.c.x, term.c.y);
1258 tclearregion(0, term.c.y, term.col-1, term.c.y);
1262 case 'S': /* SU -- Scroll <n> line up */
1263 DEFAULT(escseq.arg[0], 1);
1264 tscrollup(term.top, escseq.arg[0]);
1266 case 'T': /* SD -- Scroll <n> line down */
1267 DEFAULT(escseq.arg[0], 1);
1268 tscrolldown(term.top, escseq.arg[0]);
1270 case 'L': /* IL -- Insert <n> blank lines */
1271 DEFAULT(escseq.arg[0], 1);
1272 tinsertblankline(escseq.arg[0]);
1274 case 'l': /* RM -- Reset Mode */
1276 switch(escseq.arg[0]) {
1278 term.mode &= ~MODE_APPKEYPAD;
1280 case 5: /* DECSCNM -- Remove reverse video */
1281 if(IS_SET(MODE_REVERSE)) {
1282 term.mode &= ~MODE_REVERSE;
1287 term.mode &= ~MODE_WRAP;
1289 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1292 term.mode &= ~MODE_CRLF;
1295 term.c.state |= CURSOR_HIDE;
1297 case 1000: /* disable X11 xterm mouse reporting */
1298 term.mode &= ~MODE_MOUSEBTN;
1301 term.mode &= ~MODE_MOUSEMOTION;
1303 case 1049: /* = 1047 and 1048 */
1306 if(IS_SET(MODE_ALTSCREEN)) {
1307 tclearregion(0, 0, term.col-1, term.row-1);
1310 if(escseq.arg[0] != 1049)
1313 tcursor(CURSOR_LOAD);
1319 switch(escseq.arg[0]) {
1321 term.mode &= ~MODE_INSERT;
1328 case 'M': /* DL -- Delete <n> lines */
1329 DEFAULT(escseq.arg[0], 1);
1330 tdeleteline(escseq.arg[0]);
1332 case 'X': /* ECH -- Erase <n> char */
1333 DEFAULT(escseq.arg[0], 1);
1334 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
1336 case 'P': /* DCH -- Delete <n> char */
1337 DEFAULT(escseq.arg[0], 1);
1338 tdeletechar(escseq.arg[0]);
1340 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1341 case 'd': /* VPA -- Move to <row> */
1342 DEFAULT(escseq.arg[0], 1);
1343 tmoveto(term.c.x, escseq.arg[0]-1);
1345 case 'h': /* SM -- Set terminal mode */
1347 switch(escseq.arg[0]) {
1349 term.mode |= MODE_APPKEYPAD;
1351 case 5: /* DECSCNM -- Reverve video */
1352 if(!IS_SET(MODE_REVERSE)) {
1353 term.mode |= MODE_REVERSE;
1358 term.mode |= MODE_WRAP;
1361 term.mode |= MODE_CRLF;
1363 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1364 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1365 if(escseq.narg > 1 && escseq.arg[1] != 25)
1368 term.c.state &= ~CURSOR_HIDE;
1370 case 1000: /* 1000,1002: enable xterm mouse report */
1371 term.mode |= MODE_MOUSEBTN;
1374 term.mode |= MODE_MOUSEMOTION;
1376 case 1049: /* = 1047 and 1048 */
1379 if(IS_SET(MODE_ALTSCREEN))
1380 tclearregion(0, 0, term.col-1, term.row-1);
1383 if(escseq.arg[0] != 1049)
1386 tcursor(CURSOR_SAVE);
1388 default: goto unknown;
1391 switch(escseq.arg[0]) {
1393 term.mode |= MODE_INSERT;
1395 default: goto unknown;
1399 case 'm': /* SGR -- Terminal attribute (color) */
1400 tsetattr(escseq.arg, escseq.narg);
1402 case 'r': /* DECSTBM -- Set Scrolling Region */
1406 DEFAULT(escseq.arg[0], 1);
1407 DEFAULT(escseq.arg[1], term.row);
1408 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
1412 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1413 tcursor(CURSOR_SAVE);
1415 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1416 tcursor(CURSOR_LOAD);
1425 for(i = 0; i < escseq.len; i++) {
1426 uint c = escseq.buf[i] & 0xff;
1427 if(isprint(c)) putchar(c);
1428 else if(c == '\n') printf("(\\n)");
1429 else if(c == '\r') printf("(\\r)");
1430 else if(c == 0x1b) printf("(\\e)");
1431 else printf("(%02x)", c);
1438 memset(&escseq, 0, sizeof(escseq));
1445 for (x = term.c.x + 1; x < term.col && !term.tabs[x]; ++x)
1447 tmoveto(x, term.c.y);
1453 if(term.esc & ESC_START) {
1454 if(term.esc & ESC_CSI) {
1455 escseq.buf[escseq.len++] = ascii;
1456 if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
1458 csiparse(), csihandle();
1460 /* TODO: handle other OSC */
1461 } else if(term.esc & ESC_OSC) {
1464 term.esc = ESC_START | ESC_TITLE;
1466 } else if(term.esc & ESC_TITLE) {
1467 if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
1469 term.title[term.titlelen] = '\0';
1470 XStoreName(xw.dpy, xw.win, term.title);
1472 term.title[term.titlelen++] = ascii;
1474 } else if(term.esc & ESC_ALTCHARSET) {
1476 case '0': /* Line drawing crap */
1477 term.c.attr.mode |= ATTR_GFX;
1479 case 'B': /* Back to regular text */
1480 term.c.attr.mode &= ~ATTR_GFX;
1483 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1489 term.esc |= ESC_CSI;
1492 term.esc |= ESC_OSC;
1495 term.esc |= ESC_ALTCHARSET;
1497 case 'D': /* IND -- Linefeed */
1498 if(term.c.y == term.bot)
1499 tscrollup(term.top, 1);
1501 tmoveto(term.c.x, term.c.y+1);
1504 case 'E': /* NEL -- Next line */
1505 tnewline(1); /* always go to first col */
1508 case 'H': /* HTS -- Horizontal tab stop */
1509 term.tabs[term.c.x] = 1;
1512 case 'M': /* RI -- Reverse index */
1513 if(term.c.y == term.top)
1514 tscrolldown(term.top, 1);
1516 tmoveto(term.c.x, term.c.y-1);
1519 case 'c': /* RIS -- Reset to inital state */
1523 case '=': /* DECPAM -- Application keypad */
1524 term.mode |= MODE_APPKEYPAD;
1527 case '>': /* DECPNM -- Normal keypad */
1528 term.mode &= ~MODE_APPKEYPAD;
1531 case '7': /* DECSC -- Save Cursor */
1532 tcursor(CURSOR_SAVE);
1535 case '8': /* DECRC -- Restore Cursor */
1536 tcursor(CURSOR_LOAD);
1540 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1541 (uchar) ascii, isprint(ascii)?ascii:'.');
1546 if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
1553 tmoveto(term.c.x-1, term.c.y);
1556 tmoveto(0, term.c.y);
1561 /* go to first col if the mode is set */
1562 tnewline(IS_SET(MODE_CRLF));
1565 if(!(xw.state & WIN_FOCUSED))
1570 term.esc = ESC_START;
1573 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1574 tnewline(1); /* always go to first col */
1576 if(term.c.x+1 < term.col)
1577 tmoveto(term.c.x+1, term.c.y);
1579 term.c.state |= CURSOR_WRAPNEXT;
1585 tresize(int col, int row) {
1587 int minrow = MIN(row, term.row);
1588 int mincol = MIN(col, term.col);
1589 int slide = term.c.y - row + 1;
1591 if(col < 1 || row < 1)
1594 /* free unneeded rows */
1597 /* slide screen to keep cursor where we expect it -
1598 * tscrollup would work here, but we can optimize to
1599 * memmove because we're freeing the earlier lines */
1600 for(/* i = 0 */; i < slide; i++) {
1604 memmove(term.line, term.line + slide, row * sizeof(Line));
1605 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1607 for(i += row; i < term.row; i++) {
1612 /* resize to new height */
1613 term.line = realloc(term.line, row * sizeof(Line));
1614 term.alt = realloc(term.alt, row * sizeof(Line));
1615 term.dirty = realloc(term.dirty, row * sizeof(*term.dirty));
1616 term.tabs = realloc(term.tabs, col * sizeof(*term.tabs));
1618 /* resize each row to new width, zero-pad if needed */
1619 for(i = 0; i < minrow; i++) {
1621 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1622 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1623 for(x = mincol; x < col; x++) {
1624 term.line[i][x].state = 0;
1625 term.alt[i][x].state = 0;
1629 /* allocate any new rows */
1630 for(/* i == minrow */; i < row; i++) {
1632 term.line[i] = calloc(col, sizeof(Glyph));
1633 term.alt [i] = calloc(col, sizeof(Glyph));
1635 if (col > term.col) {
1636 bool *bp = term.tabs + term.col;
1638 memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
1639 while (--bp > term.tabs && !*bp)
1641 for (bp += TAB; bp < term.tabs + col; bp += TAB)
1644 /* update terminal size */
1645 term.col = col, term.row = row;
1646 /* make use of the LIMIT in tmoveto */
1647 tmoveto(term.c.x, term.c.y);
1648 /* reset scrolling region */
1649 tsetscroll(0, row-1);
1655 xresize(int col, int row) {
1656 xw.w = MAX(1, 2*BORDER + col * xw.cw);
1657 xw.h = MAX(1, 2*BORDER + row * xw.ch);
1664 ulong white = WhitePixel(xw.dpy, xw.scr);
1666 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1667 for(i = 0; i < LEN(colorname); i++) {
1670 if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1672 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1674 dc.col[i] = color.pixel;
1677 /* load colors [16-255] ; same colors as xterm */
1678 for(i = 16, r = 0; r < 6; r++)
1679 for(g = 0; g < 6; g++)
1680 for(b = 0; b < 6; b++) {
1681 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1682 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1683 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1684 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1686 fprintf(stderr, "Could not allocate color %d\n", i);
1688 dc.col[i] = color.pixel;
1692 for(r = 0; r < 24; r++, i++) {
1693 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1694 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1696 fprintf(stderr, "Could not allocate color %d\n", i);
1698 dc.col[i] = color.pixel;
1703 xclear(int x1, int y1, int x2, int y2) {
1704 XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
1705 XFillRectangle(xw.dpy, xw.buf, dc.gc,
1706 BORDER + x1 * xw.cw, BORDER + y1 * xw.ch,
1707 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1712 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1713 XWMHints wm = {.flags = InputHint, .input = 1};
1715 .flags = PSize | PResizeInc | PBaseSize,
1718 .height_inc = xw.ch,
1720 .base_height = 2*BORDER,
1721 .base_width = 2*BORDER,
1723 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1727 xinitfont(char *fontstr) {
1729 char *def, **missing;
1733 set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1736 fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1737 XFreeStringList(missing);
1743 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
1744 XFontStruct **xfonts;
1748 *ascent = *descent = *lbearing = *rbearing = 0;
1749 n = XFontsOfFontSet(set, &xfonts, &font_names);
1750 for(i = 0; i < n; i++) {
1751 *ascent = MAX(*ascent, (*xfonts)->ascent);
1752 *descent = MAX(*descent, (*xfonts)->descent);
1753 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1754 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1760 initfonts(char *fontstr, char *bfontstr) {
1761 if((dc.font.set = xinitfont(fontstr)) == NULL ||
1762 (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1763 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1764 xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1765 &dc.font.lbearing, &dc.font.rbearing);
1766 xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1767 &dc.bfont.lbearing, &dc.bfont.rbearing);
1772 XSetWindowAttributes attrs;
1776 if(!(xw.dpy = XOpenDisplay(NULL)))
1777 die("Can't open display\n");
1778 xw.scr = XDefaultScreen(xw.dpy);
1781 initfonts(FONT, BOLDFONT);
1783 /* XXX: Assuming same size for bold font */
1784 xw.cw = dc.font.rbearing - dc.font.lbearing;
1785 xw.ch = dc.font.ascent + dc.font.descent;
1788 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1791 /* window - default size */
1792 xw.h = 2*BORDER + term.row * xw.ch;
1793 xw.w = 2*BORDER + term.col * xw.cw;
1795 attrs.background_pixel = dc.col[DefaultBG];
1796 attrs.border_pixel = dc.col[DefaultBG];
1797 attrs.bit_gravity = NorthWestGravity;
1798 attrs.event_mask = FocusChangeMask | KeyPressMask
1799 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1800 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
1801 | EnterWindowMask | LeaveWindowMask;
1802 attrs.colormap = xw.cmap;
1804 parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
1805 xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
1806 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1807 XDefaultVisual(xw.dpy, xw.scr),
1808 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1811 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
1815 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1816 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1817 | XIMStatusNothing, XNClientWindow, xw.win,
1818 XNFocusWindow, xw.win, NULL);
1820 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1822 /* white cursor, black outline */
1823 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
1824 XDefineCursor(xw.dpy, xw.win, cursor);
1825 XRecolorCursor(xw.dpy, cursor,
1826 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
1827 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
1829 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
1831 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1832 XMapWindow(xw.dpy, xw.win);
1838 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1839 int fg = base.fg, bg = base.bg, temp;
1840 int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1841 XFontSet fontset = dc.font.set;
1844 /* only switch default fg/bg if term is in RV mode */
1845 if(IS_SET(MODE_REVERSE)) {
1852 if(base.mode & ATTR_REVERSE)
1853 temp = fg, fg = bg, bg = temp;
1855 if(base.mode & ATTR_BOLD) {
1857 fontset = dc.bfont.set;
1860 XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
1861 XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
1863 if(base.mode & ATTR_GFX) {
1864 for(i = 0; i < bytelen; i++) {
1865 char c = gfx[(uint)s[i] % 256];
1868 else if(s[i] > 0x5f)
1873 XmbDrawImageString(xw.dpy, xw.buf, fontset, dc.gc, winx, winy, s, bytelen);
1875 if(base.mode & ATTR_UNDERLINE)
1876 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1879 /* copy buffer pixmap to screen pixmap */
1882 XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
1883 XdbeSwapBuffers(xw.dpy, swpinfo, 1);
1889 static int oldx = 0;
1890 static int oldy = 0;
1892 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
1894 LIMIT(oldx, 0, term.col-1);
1895 LIMIT(oldy, 0, term.row-1);
1897 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1898 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
1900 /* remove the old cursor */
1901 if(term.line[oldy][oldx].state & GLYPH_SET) {
1902 sl = utf8size(term.line[oldy][oldx].c);
1903 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
1905 xclear(oldx, oldy, oldx, oldy);
1907 xcopy(oldx, oldy, 1, 1);
1909 /* draw the new one */
1910 if(!(term.c.state & CURSOR_HIDE)) {
1911 if(!(xw.state & WIN_FOCUSED))
1914 if(IS_SET(MODE_REVERSE))
1915 g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
1918 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
1919 oldx = term.c.x, oldy = term.c.y;
1922 xcopy(term.c.x, term.c.y, 1, 1);
1927 drawregion(0, 0, term.col, term.row);
1929 gettimeofday(&xw.lastdraw, NULL);
1933 drawregion(int x1, int y1, int x2, int y2) {
1934 int ic, ib, x, y, ox, sl;
1936 char buf[DRAW_BUF_SIZ];
1938 if(!(xw.state & WIN_VISIBLE))
1941 for(y = y1; y < y2; y++) {
1944 xclear(0, y, term.col, y);
1946 base = term.line[y][0];
1948 for(x = x1; x < x2; x++) {
1949 new = term.line[y][x];
1950 if(sel.bx != -1 && *(new.c) && selected(x, y))
1951 new.mode ^= ATTR_REVERSE;
1952 if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1953 ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
1954 xdraws(buf, base, ox, y, ic, ib);
1957 if(new.state & GLYPH_SET) {
1962 sl = utf8size(new.c);
1963 memcpy(buf+ib, new.c, sl);
1969 xdraws(buf, base, ox, y, ic, ib);
1975 expose(XEvent *ev) {
1976 XExposeEvent *e = &ev->xexpose;
1977 if(xw.state & WIN_REDRAW) {
1979 xw.state &= ~WIN_REDRAW;
1985 visibility(XEvent *ev) {
1986 XVisibilityEvent *e = &ev->xvisibility;
1987 if(e->state == VisibilityFullyObscured)
1988 xw.state &= ~WIN_VISIBLE;
1989 else if(!(xw.state & WIN_VISIBLE))
1990 /* need a full redraw for next Expose, not just a buf copy */
1991 xw.state |= WIN_VISIBLE | WIN_REDRAW;
1996 xw.state &= ~WIN_VISIBLE;
2000 xseturgency(int add) {
2001 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
2002 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
2003 XSetWMHints(xw.dpy, xw.win, h);
2009 if(ev->type == FocusIn) {
2010 xw.state |= WIN_FOCUSED;
2013 xw.state &= ~WIN_FOCUSED;
2018 kmap(KeySym k, uint state) {
2021 for(i = 0; i < LEN(key); i++) {
2022 uint mask = key[i].mask;
2023 if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
2024 return (char*)key[i].s;
2030 kpress(XEvent *ev) {
2031 XKeyEvent *e = &ev->xkey;
2040 meta = e->state & Mod1Mask;
2041 shift = e->state & ShiftMask;
2042 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
2044 /* 1. custom keys from config.h */
2045 if((customkey = kmap(ksym, e->state)))
2046 ttywrite(customkey, strlen(customkey));
2047 /* 2. hardcoded (overrides X lookup) */
2054 /* XXX: shift up/down doesn't work */
2055 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
2063 if(IS_SET(MODE_CRLF))
2064 ttywrite("\r\n", 2);
2071 if(meta && len == 1)
2072 ttywrite("\033", 1);
2080 cmessage(XEvent *e) {
2082 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2083 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
2084 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
2085 xw.state |= WIN_FOCUSED;
2087 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
2088 xw.state &= ~WIN_FOCUSED;
2098 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
2101 xw.w = e->xconfigure.width;
2102 xw.h = e->xconfigure.height;
2103 col = (xw.w - 2*BORDER) / xw.cw;
2104 row = (xw.h - 2*BORDER) / xw.ch;
2105 if(col == term.col && row == term.row)
2107 if(tresize(col, row))
2109 ttyresize(col, row);
2114 last_draw_too_old(void) {
2116 gettimeofday(&now, NULL);
2117 return TIMEDIFF(now, xw.lastdraw) >= DRAW_TIMEOUT/1000;
2124 int xfd = XConnectionNumber(xw.dpy);
2125 struct timeval timeout = {0};
2126 bool stuff_to_print = 0;
2130 FD_SET(cmdfd, &rfd);
2133 timeout.tv_usec = SELECT_TIMEOUT;
2134 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, &timeout) < 0) {
2137 die("select failed: %s\n", SERRNO);
2139 if(FD_ISSET(cmdfd, &rfd)) {
2144 if(stuff_to_print && last_draw_too_old()) {
2149 while(XPending(xw.dpy)) {
2150 XNextEvent(xw.dpy, &ev);
2151 if(XFilterEvent(&ev, xw.win))
2153 if(handler[ev.type])
2154 (handler[ev.type])(&ev);
2160 main(int argc, char *argv[]) {
2163 for(i = 1; i < argc; i++) {
2164 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
2166 if(++i < argc) opt_title = argv[i];
2169 if(++i < argc) opt_class = argv[i];
2172 if(++i < argc) opt_embed = argv[i];
2175 /* eat every remaining arguments */
2176 if(++i < argc) opt_cmd = &argv[i];
2185 setlocale(LC_CTYPE, "");