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] [-w windowid] [-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
46 #define XK_NO_MOD UINT_MAX
49 #define SERRNO strerror(errno)
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 #define MAX(a, b) ((a) < (b) ? (b) : (a))
52 #define LEN(a) (sizeof(a) / sizeof(a[0]))
53 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
54 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
55 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
56 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
57 #define IS_SET(flag) (term.mode & (flag))
58 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
59 #define X2COL(x) (((x) - BORDER)/xw.cw)
60 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
62 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
63 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
64 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
65 CURSOR_SAVE, CURSOR_LOAD };
66 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
67 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
68 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
69 MODE_CRLF=16, MODE_MOUSEBTN=32, MODE_MOUSEMOTION=64, MODE_MOUSE=32|64, MODE_REVERSE=128 };
70 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
71 enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
74 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
77 char c[UTF_SIZ]; /* character code */
78 char mode; /* attribute flags */
79 int fg; /* foreground */
80 int bg; /* background */
81 char state; /* state flags */
87 Glyph attr; /* current char attributes */
93 /* CSI Escape sequence structs */
94 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
96 char buf[ESC_BUF_SIZ]; /* raw string */
97 int len; /* raw string length */
100 int narg; /* nb of args */
104 /* Internal representation of the screen */
106 int row; /* nb row */
107 int col; /* nb col */
108 Line* line; /* screen */
109 Line* alt; /* alternate screen */
110 TCursor c; /* cursor */
111 int top; /* top scroll limit */
112 int bot; /* bottom scroll limit */
113 int mode; /* terminal mode flags */
114 int esc; /* escape state flags */
115 char title[ESC_TITLE_SIZ];
119 /* Purely graphic info */
128 int w; /* window width */
129 int h; /* window height */
130 int bufw; /* pixmap width */
131 int bufh; /* pixmap height */
132 int ch; /* char height */
133 int cw; /* char width */
134 char state; /* focus, redraw, visible */
143 /* Drawing Context */
145 unsigned long col[256];
156 /* TODO: use better name for vars... */
161 struct {int x, y;} b, e;
164 struct timeval tclick1;
165 struct timeval tclick2;
170 static void die(const char*, ...);
171 static void draw(void);
172 static void drawregion(int, int, int, int);
173 static void execsh(void);
174 static void sigchld(int);
175 static void run(void);
177 static void csidump(void);
178 static void csihandle(void);
179 static void csiparse(void);
180 static void csireset(void);
182 static void tclearregion(int, int, int, int);
183 static void tcursor(int);
184 static void tdeletechar(int);
185 static void tdeleteline(int);
186 static void tinsertblank(int);
187 static void tinsertblankline(int);
188 static void tmoveto(int, int);
189 static void tnew(int, int);
190 static void tnewline(int);
191 static void tputtab(void);
192 static void tputc(char*);
193 static void treset(void);
194 static int tresize(int, int);
195 static void tscrollup(int, int);
196 static void tscrolldown(int, int);
197 static void tsetattr(int*, int);
198 static void tsetchar(char*);
199 static void tsetscroll(int, int);
200 static void tswapscreen(void);
202 static void ttynew(void);
203 static void ttyread(void);
204 static void ttyresize(int, int);
205 static void ttywrite(const char *, size_t);
207 static void xdraws(char *, Glyph, int, int, int, int);
208 static void xhints(void);
209 static void xclear(int, int, int, int);
210 static void xdrawcursor(void);
211 static void xinit(void);
212 static void xloadcols(void);
213 static void xseturgency(int);
214 static void xsetsel(char*);
215 static void xresize(int, int);
217 static void expose(XEvent *);
218 static void visibility(XEvent *);
219 static void unmap(XEvent *);
220 static char* kmap(KeySym, unsigned int state);
221 static void kpress(XEvent *);
222 static void resize(XEvent *);
223 static void focus(XEvent *);
224 static void brelease(XEvent *);
225 static void bpress(XEvent *);
226 static void bmotion(XEvent *);
227 static void selnotify(XEvent *);
228 static void selrequest(XEvent *);
230 static void selinit(void);
231 static inline int selected(int, int);
232 static void selcopy(void);
233 static void selpaste();
235 static int utf8decode(char *, long *);
236 static int utf8encode(long *, char *);
237 static int utf8size(char *);
238 static int isfullutf8(char *, int);
240 static void (*handler[LASTEvent])(XEvent *) = {
242 [ConfigureNotify] = resize,
243 [VisibilityNotify] = visibility,
244 [UnmapNotify] = unmap,
246 [EnterNotify] = focus,
247 [LeaveNotify] = focus,
250 [MotionNotify] = bmotion,
251 [ButtonPress] = bpress,
252 [ButtonRelease] = brelease,
253 [SelectionNotify] = selnotify,
254 [SelectionRequest] = selrequest,
261 static CSIEscape escseq;
264 static Selection sel;
265 static char **opt_cmd = NULL;
266 static char *opt_title = NULL;
267 static char *opt_embed = NULL;
268 static char *opt_class = NULL;
271 utf8decode(char *s, long *u) {
277 if(~c&B7) { /* 0xxxxxxx */
280 } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
281 *u = c&(B4|B3|B2|B1|B0);
283 } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
284 *u = c&(B3|B2|B1|B0);
286 } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
291 for(i=n,++s; i>0; --i,++rtn,++s) {
293 if((c&(B7|B6)) != B7) /* 10xxxxxx */
296 *u |= c&(B5|B4|B3|B2|B1|B0);
298 if((n == 1 && *u < 0x80) ||
299 (n == 2 && *u < 0x800) ||
300 (n == 3 && *u < 0x10000) ||
301 (*u >= 0xD800 && *u <= 0xDFFF))
310 utf8encode(long *u, char *s) {
315 sp = (unsigned char*) s;
318 *sp = uc; /* 0xxxxxxx */
320 } else if(*u < 0x800) {
321 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
323 } else if(uc < 0x10000) {
324 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
326 } else if(uc <= 0x10FFFF) {
327 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
332 for(i=n,++sp; i>0; --i,++sp)
333 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
343 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
344 UTF-8 otherwise return 0 */
346 isfullutf8(char *s, int b) {
347 unsigned char *c1, *c2, *c3;
349 c1 = (unsigned char *) s;
350 c2 = (unsigned char *) ++s;
351 c3 = (unsigned char *) ++s;
354 else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
356 else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
358 ((b == 2) && (*c2&(B7|B6)) == B7)))
360 else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
362 ((b == 2) && (*c2&(B7|B6)) == B7) ||
363 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
371 unsigned char c = *s;
375 else if((c&(B7|B6|B5)) == (B7|B6))
377 else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
385 sel.tclick1.tv_sec = 0;
386 sel.tclick1.tv_usec = 0;
390 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
391 if(sel.xtarget == None)
392 sel.xtarget = XA_STRING;
396 selected(int x, int y) {
397 if(sel.ey == y && sel.by == y) {
398 int bx = MIN(sel.bx, sel.ex);
399 int ex = MAX(sel.bx, sel.ex);
400 return BETWEEN(x, bx, ex);
402 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
403 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
407 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
409 *b = e->xbutton.button;
411 *x = X2COL(e->xbutton.x);
412 *y = Y2ROW(e->xbutton.y);
413 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
414 sel.b.y = MIN(sel.by, sel.ey);
415 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
416 sel.e.y = MAX(sel.by, sel.ey);
420 mousereport(XEvent *e) {
421 int x = X2COL(e->xbutton.x);
422 int y = Y2ROW(e->xbutton.y);
423 int button = e->xbutton.button;
424 int state = e->xbutton.state;
425 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
426 static int ob, ox, oy;
429 if(e->xbutton.type == MotionNotify) {
430 if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
434 } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
440 if(e->xbutton.type == ButtonPress) {
446 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
447 + (state & Mod4Mask ? 8 : 0)
448 + (state & ControlMask ? 16 : 0);
450 ttywrite(buf, sizeof(buf));
455 if(IS_SET(MODE_MOUSE))
457 else if(e->xbutton.button == Button1) {
459 sel.ex = sel.bx = X2COL(e->xbutton.x);
460 sel.ey = sel.by = Y2ROW(e->xbutton.y);
467 int x, y, sz, sl, ls = 0;
472 sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
473 ptr = str = malloc(sz);
474 for(y = 0; y < term.row; y++) {
475 for(x = 0; x < term.col; x++)
476 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
477 sl = utf8size(term.line[y][x].c);
478 memcpy(ptr, term.line[y][x].c, sl);
481 if(ls && y < sel.e.y)
490 selnotify(XEvent *e) {
491 unsigned long nitems, ofs, rem;
498 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
499 False, AnyPropertyType, &type, &format,
500 &nitems, &rem, &data)) {
501 fprintf(stderr, "Clipboard allocation failed\n");
504 ttywrite((const char *) data, nitems * format / 8);
506 /* number of 32-bit chunks returned */
507 ofs += nitems * format / 32;
513 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
517 selrequest(XEvent *e) {
518 XSelectionRequestEvent *xsre;
522 xsre = (XSelectionRequestEvent *) e;
523 xev.type = SelectionNotify;
524 xev.requestor = xsre->requestor;
525 xev.selection = xsre->selection;
526 xev.target = xsre->target;
527 xev.time = xsre->time;
531 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
532 if(xsre->target == xa_targets) {
533 /* respond with the supported type */
534 Atom string = sel.xtarget;
535 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
536 XA_ATOM, 32, PropModeReplace,
537 (unsigned char *) &string, 1);
538 xev.property = xsre->property;
539 } else if(xsre->target == sel.xtarget) {
540 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
541 xsre->target, 8, PropModeReplace,
542 (unsigned char *) sel.clip, strlen(sel.clip));
543 xev.property = xsre->property;
546 /* all done, send a notification to the listener */
547 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
548 fprintf(stderr, "Error sending SelectionNotify event\n");
553 /* register the selection for both the clipboard and the primary */
559 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
561 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
562 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
568 brelease(XEvent *e) {
569 if(IS_SET(MODE_MOUSE)) {
573 if(e->xbutton.button == Button2)
575 else if(e->xbutton.button == Button1) {
577 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
578 if(sel.bx == sel.ex && sel.by == sel.ey) {
581 gettimeofday(&now, NULL);
583 if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
584 /* triple click on the line */
585 sel.b.x = sel.bx = 0;
586 sel.e.x = sel.ex = term.col;
587 sel.b.y = sel.e.y = sel.ey;
589 } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
590 /* double click to select word */
592 while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
593 term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
595 while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
596 term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
598 sel.b.y = sel.e.y = sel.ey;
604 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
605 gettimeofday(&sel.tclick1, NULL);
611 if(IS_SET(MODE_MOUSE)) {
616 int oldey = sel.ey, oldex = sel.ex;
617 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
619 if(oldey != sel.ey || oldex != sel.ex) {
620 int starty = MIN(oldey, sel.ey);
621 int endy = MAX(oldey, sel.ey);
622 drawregion(0, (starty > 0 ? starty : 0), term.col, (endy < term.row ? endy+1 : term.row));
628 die(const char *errstr, ...) {
631 va_start(ap, errstr);
632 vfprintf(stderr, errstr, ap);
640 char *envshell = getenv("SHELL");
642 DEFAULT(envshell, "sh");
643 putenv("TERM="TNAME);
644 args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
645 execvp(args[0], args);
652 if(waitpid(pid, &stat, 0) < 0)
653 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
655 exit(WEXITSTATUS(stat));
664 /* seems to work fine on linux, openbsd and freebsd */
665 struct winsize w = {term.row, term.col, 0, 0};
666 if(openpty(&m, &s, NULL, NULL, &w) < 0)
667 die("openpty failed: %s\n", SERRNO);
669 switch(pid = fork()) {
671 die("fork failed\n");
674 setsid(); /* create a new process group */
675 dup2(s, STDIN_FILENO);
676 dup2(s, STDOUT_FILENO);
677 dup2(s, STDERR_FILENO);
678 if(ioctl(s, TIOCSCTTY, NULL) < 0)
679 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
687 signal(SIGCHLD, sigchld);
694 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
696 fprintf(stderr, "\n");
701 static char buf[BUFSIZ];
702 static int buflen = 0;
705 int charsize; /* size of utf8 char in bytes */
709 /* append read bytes to unprocessed bytes */
710 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
711 die("Couldn't read from shell: %s\n", SERRNO);
713 /* process every complete utf8 char */
716 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
717 charsize = utf8decode(ptr, &utf8c);
718 utf8encode(&utf8c, s);
724 /* keep any uncomplete utf8 char for the next call */
725 memmove(buf, ptr, buflen);
729 ttywrite(const char *s, size_t n) {
730 if(write(cmdfd, s, n) == -1)
731 die("write error on tty: %s\n", SERRNO);
735 ttyresize(int x, int y) {
740 w.ws_xpixel = w.ws_ypixel = 0;
741 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
742 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
749 if(mode == CURSOR_SAVE)
751 else if(mode == CURSOR_LOAD)
752 term.c = c, tmoveto(c.x, c.y);
761 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
763 term.top = 0, term.bot = term.row - 1;
764 term.mode = MODE_WRAP;
765 tclearregion(0, 0, term.col-1, term.row-1);
769 tnew(int col, int row) {
770 /* set screen size */
771 term.row = row, term.col = col;
772 term.line = malloc(term.row * sizeof(Line));
773 term.alt = malloc(term.row * sizeof(Line));
774 for(row = 0; row < term.row; row++) {
775 term.line[row] = malloc(term.col * sizeof(Glyph));
776 term.alt [row] = malloc(term.col * sizeof(Glyph));
784 Line* tmp = term.line;
785 term.line = term.alt;
787 term.mode ^= MODE_ALTSCREEN;
791 tscrolldown(int orig, int n) {
795 LIMIT(n, 0, term.bot-orig+1);
797 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
799 for(i = term.bot; i >= orig+n; i--) {
801 term.line[i] = term.line[i-n];
802 term.line[i-n] = temp;
807 tscrollup(int orig, int n) {
810 LIMIT(n, 0, term.bot-orig+1);
812 tclearregion(0, orig, term.col-1, orig+n-1);
814 for(i = orig; i <= term.bot-n; i++) {
816 term.line[i] = term.line[i+n];
817 term.line[i+n] = temp;
822 tnewline(int first_col) {
825 tscrollup(term.top, 1);
828 tmoveto(first_col ? 0 : term.c.x, y);
834 char *p = escseq.buf;
838 escseq.priv = 1, p++;
840 while(p < escseq.buf+escseq.len) {
842 escseq.arg[escseq.narg] *= 10;
843 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
845 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
856 tmoveto(int x, int y) {
857 LIMIT(x, 0, term.col-1);
858 LIMIT(y, 0, term.row-1);
859 term.c.state &= ~CURSOR_WRAPNEXT;
866 term.line[term.c.y][term.c.x] = term.c.attr;
867 memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
868 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
872 tclearregion(int x1, int y1, int x2, int y2) {
876 temp = x1, x1 = x2, x2 = temp;
878 temp = y1, y1 = y2, y2 = temp;
880 LIMIT(x1, 0, term.col-1);
881 LIMIT(x2, 0, term.col-1);
882 LIMIT(y1, 0, term.row-1);
883 LIMIT(y2, 0, term.row-1);
885 for(y = y1; y <= y2; y++)
886 for(x = x1; x <= x2; x++)
887 term.line[y][x].state = 0;
892 int src = term.c.x + n;
894 int size = term.col - src;
896 if(src >= term.col) {
897 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
900 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
901 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
905 tinsertblank(int n) {
908 int size = term.col - dst;
910 if(dst >= term.col) {
911 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
914 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
915 tclearregion(src, term.c.y, dst - 1, term.c.y);
919 tinsertblankline(int n) {
920 if(term.c.y < term.top || term.c.y > term.bot)
923 tscrolldown(term.c.y, n);
928 if(term.c.y < term.top || term.c.y > term.bot)
931 tscrollup(term.c.y, n);
935 tsetattr(int *attr, int l) {
938 for(i = 0; i < l; i++) {
941 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
942 term.c.attr.fg = DefaultFG;
943 term.c.attr.bg = DefaultBG;
946 term.c.attr.mode |= ATTR_BOLD;
949 term.c.attr.mode |= ATTR_UNDERLINE;
952 term.c.attr.mode |= ATTR_REVERSE;
955 term.c.attr.mode &= ~ATTR_BOLD;
958 term.c.attr.mode &= ~ATTR_UNDERLINE;
961 term.c.attr.mode &= ~ATTR_REVERSE;
964 if(i + 2 < l && attr[i + 1] == 5) {
966 if(BETWEEN(attr[i], 0, 255))
967 term.c.attr.fg = attr[i];
969 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
972 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
975 term.c.attr.fg = DefaultFG;
978 if(i + 2 < l && attr[i + 1] == 5) {
980 if(BETWEEN(attr[i], 0, 255))
981 term.c.attr.bg = attr[i];
983 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
986 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
989 term.c.attr.bg = DefaultBG;
992 if(BETWEEN(attr[i], 30, 37))
993 term.c.attr.fg = attr[i] - 30;
994 else if(BETWEEN(attr[i], 40, 47))
995 term.c.attr.bg = attr[i] - 40;
996 else if(BETWEEN(attr[i], 90, 97))
997 term.c.attr.fg = attr[i] - 90 + 8;
998 else if(BETWEEN(attr[i], 100, 107))
999 term.c.attr.fg = attr[i] - 100 + 8;
1001 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
1009 tsetscroll(int t, int b) {
1012 LIMIT(t, 0, term.row-1);
1013 LIMIT(b, 0, term.row-1);
1025 switch(escseq.mode) {
1028 fprintf(stderr, "erresc: unknown csi ");
1032 case '@': /* ICH -- Insert <n> blank char */
1033 DEFAULT(escseq.arg[0], 1);
1034 tinsertblank(escseq.arg[0]);
1036 case 'A': /* CUU -- Cursor <n> Up */
1038 DEFAULT(escseq.arg[0], 1);
1039 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
1041 case 'B': /* CUD -- Cursor <n> Down */
1042 DEFAULT(escseq.arg[0], 1);
1043 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
1045 case 'C': /* CUF -- Cursor <n> Forward */
1047 DEFAULT(escseq.arg[0], 1);
1048 tmoveto(term.c.x+escseq.arg[0], term.c.y);
1050 case 'D': /* CUB -- Cursor <n> Backward */
1051 DEFAULT(escseq.arg[0], 1);
1052 tmoveto(term.c.x-escseq.arg[0], term.c.y);
1054 case 'E': /* CNL -- Cursor <n> Down and first col */
1055 DEFAULT(escseq.arg[0], 1);
1056 tmoveto(0, term.c.y+escseq.arg[0]);
1058 case 'F': /* CPL -- Cursor <n> Up and first col */
1059 DEFAULT(escseq.arg[0], 1);
1060 tmoveto(0, term.c.y-escseq.arg[0]);
1062 case 'G': /* CHA -- Move to <col> */
1063 case '`': /* XXX: HPA -- same? */
1064 DEFAULT(escseq.arg[0], 1);
1065 tmoveto(escseq.arg[0]-1, term.c.y);
1067 case 'H': /* CUP -- Move to <row> <col> */
1068 case 'f': /* XXX: HVP -- same? */
1069 DEFAULT(escseq.arg[0], 1);
1070 DEFAULT(escseq.arg[1], 1);
1071 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
1073 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1074 case 'J': /* ED -- Clear screen */
1075 switch(escseq.arg[0]) {
1077 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1078 if(term.c.y < term.row-1)
1079 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1083 tclearregion(0, 0, term.col-1, term.c.y-1);
1084 tclearregion(0, term.c.y, term.c.x, term.c.y);
1087 tclearregion(0, 0, term.col-1, term.row-1);
1093 case 'K': /* EL -- Clear line */
1094 switch(escseq.arg[0]) {
1096 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1099 tclearregion(0, term.c.y, term.c.x, term.c.y);
1102 tclearregion(0, term.c.y, term.col-1, term.c.y);
1106 case 'S': /* SU -- Scroll <n> line up */
1107 DEFAULT(escseq.arg[0], 1);
1108 tscrollup(term.top, escseq.arg[0]);
1110 case 'T': /* SD -- Scroll <n> line down */
1111 DEFAULT(escseq.arg[0], 1);
1112 tscrolldown(term.top, escseq.arg[0]);
1114 case 'L': /* IL -- Insert <n> blank lines */
1115 DEFAULT(escseq.arg[0], 1);
1116 tinsertblankline(escseq.arg[0]);
1118 case 'l': /* RM -- Reset Mode */
1120 switch(escseq.arg[0]) {
1122 term.mode &= ~MODE_APPKEYPAD;
1124 case 5: /* DECSCNM -- Remove reverse video */
1125 if(IS_SET(MODE_REVERSE)) {
1126 term.mode &= ~MODE_REVERSE;
1131 term.mode &= ~MODE_WRAP;
1133 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1136 term.mode &= ~MODE_CRLF;
1139 term.c.state |= CURSOR_HIDE;
1141 case 1000: /* disable X11 xterm mouse reporting */
1142 term.mode &= ~MODE_MOUSEBTN;
1145 term.mode &= ~MODE_MOUSEMOTION;
1147 case 1049: /* = 1047 and 1048 */
1150 if(IS_SET(MODE_ALTSCREEN)) {
1151 tclearregion(0, 0, term.col-1, term.row-1);
1154 if(escseq.arg[0] != 1049)
1157 tcursor(CURSOR_LOAD);
1163 switch(escseq.arg[0]) {
1165 term.mode &= ~MODE_INSERT;
1172 case 'M': /* DL -- Delete <n> lines */
1173 DEFAULT(escseq.arg[0], 1);
1174 tdeleteline(escseq.arg[0]);
1176 case 'X': /* ECH -- Erase <n> char */
1177 DEFAULT(escseq.arg[0], 1);
1178 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
1180 case 'P': /* DCH -- Delete <n> char */
1181 DEFAULT(escseq.arg[0], 1);
1182 tdeletechar(escseq.arg[0]);
1184 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1185 case 'd': /* VPA -- Move to <row> */
1186 DEFAULT(escseq.arg[0], 1);
1187 tmoveto(term.c.x, escseq.arg[0]-1);
1189 case 'h': /* SM -- Set terminal mode */
1191 switch(escseq.arg[0]) {
1193 term.mode |= MODE_APPKEYPAD;
1195 case 5: /* DECSCNM -- Reverve video */
1196 if(!IS_SET(MODE_REVERSE)) {
1197 term.mode |= MODE_REVERSE;
1202 term.mode |= MODE_WRAP;
1205 term.mode |= MODE_CRLF;
1207 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1208 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1209 if(escseq.narg > 1 && escseq.arg[1] != 25)
1212 term.c.state &= ~CURSOR_HIDE;
1214 case 1000: /* 1000,1002: enable xterm mouse report */
1215 term.mode |= MODE_MOUSEBTN;
1218 term.mode |= MODE_MOUSEMOTION;
1220 case 1049: /* = 1047 and 1048 */
1223 if(IS_SET(MODE_ALTSCREEN))
1224 tclearregion(0, 0, term.col-1, term.row-1);
1227 if(escseq.arg[0] != 1049)
1230 tcursor(CURSOR_SAVE);
1232 default: goto unknown;
1235 switch(escseq.arg[0]) {
1237 term.mode |= MODE_INSERT;
1239 default: goto unknown;
1243 case 'm': /* SGR -- Terminal attribute (color) */
1244 tsetattr(escseq.arg, escseq.narg);
1246 case 'r': /* DECSTBM -- Set Scrolling Region */
1250 DEFAULT(escseq.arg[0], 1);
1251 DEFAULT(escseq.arg[1], term.row);
1252 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
1256 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1257 tcursor(CURSOR_SAVE);
1259 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1260 tcursor(CURSOR_LOAD);
1268 printf("ESC [ %s", escseq.priv ? "? " : "");
1270 for(i = 0; i < escseq.narg; i++)
1271 printf("%d ", escseq.arg[i]);
1273 putchar(escseq.mode);
1279 memset(&escseq, 0, sizeof(escseq));
1284 int space = TAB - term.c.x % TAB;
1285 tmoveto(term.c.x + space, term.c.y);
1291 if(term.esc & ESC_START) {
1292 if(term.esc & ESC_CSI) {
1293 escseq.buf[escseq.len++] = ascii;
1294 if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
1296 csiparse(), csihandle();
1298 /* TODO: handle other OSC */
1299 } else if(term.esc & ESC_OSC) {
1302 term.esc = ESC_START | ESC_TITLE;
1304 } else if(term.esc & ESC_TITLE) {
1305 if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
1307 term.title[term.titlelen] = '\0';
1308 XStoreName(xw.dpy, xw.win, term.title);
1310 term.title[term.titlelen++] = ascii;
1312 } else if(term.esc & ESC_ALTCHARSET) {
1314 case '0': /* Line drawing crap */
1315 term.c.attr.mode |= ATTR_GFX;
1317 case 'B': /* Back to regular text */
1318 term.c.attr.mode &= ~ATTR_GFX;
1321 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1327 term.esc |= ESC_CSI;
1330 term.esc |= ESC_OSC;
1333 term.esc |= ESC_ALTCHARSET;
1335 case 'D': /* IND -- Linefeed */
1336 if(term.c.y == term.bot)
1337 tscrollup(term.top, 1);
1339 tmoveto(term.c.x, term.c.y+1);
1342 case 'E': /* NEL -- Next line */
1343 tnewline(1); /* always go to first col */
1346 case 'M': /* RI -- Reverse index */
1347 if(term.c.y == term.top)
1348 tscrolldown(term.top, 1);
1350 tmoveto(term.c.x, term.c.y-1);
1353 case 'c': /* RIS -- Reset to inital state */
1357 case '=': /* DECPAM -- Application keypad */
1358 term.mode |= MODE_APPKEYPAD;
1361 case '>': /* DECPNM -- Normal keypad */
1362 term.mode &= ~MODE_APPKEYPAD;
1365 case '7': /* DECSC -- Save Cursor */
1366 tcursor(CURSOR_SAVE);
1369 case '8': /* DECRC -- Restore Cursor */
1370 tcursor(CURSOR_LOAD);
1374 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1375 (unsigned char) ascii, isprint(ascii)?ascii:'.');
1385 tmoveto(term.c.x-1, term.c.y);
1388 tmoveto(0, term.c.y);
1393 /* go to first col if the mode is set */
1394 tnewline(IS_SET(MODE_CRLF));
1397 if(!(xw.state & WIN_FOCUSED))
1402 term.esc = ESC_START;
1405 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1406 tnewline(1); /* always go to first col */
1408 if(term.c.x+1 < term.col)
1409 tmoveto(term.c.x+1, term.c.y);
1411 term.c.state |= CURSOR_WRAPNEXT;
1417 tresize(int col, int row) {
1419 int minrow = MIN(row, term.row);
1420 int mincol = MIN(col, term.col);
1421 int slide = term.c.y - row + 1;
1423 if(col < 1 || row < 1)
1426 /* free unneeded rows */
1429 /* slide screen to keep cursor where we expect it -
1430 * tscrollup would work here, but we can optimize to
1431 * memmove because we're freeing the earlier lines */
1432 for(/* i = 0 */; i < slide; i++) {
1436 memmove(term.line, term.line + slide, row * sizeof(Line));
1437 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1439 for(i += row; i < term.row; i++) {
1444 /* resize to new height */
1445 term.line = realloc(term.line, row * sizeof(Line));
1446 term.alt = realloc(term.alt, row * sizeof(Line));
1448 /* resize each row to new width, zero-pad if needed */
1449 for(i = 0; i < minrow; i++) {
1450 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1451 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1452 for(x = mincol; x < col; x++) {
1453 term.line[i][x].state = 0;
1454 term.alt[i][x].state = 0;
1458 /* allocate any new rows */
1459 for(/* i == minrow */; i < row; i++) {
1460 term.line[i] = calloc(col, sizeof(Glyph));
1461 term.alt [i] = calloc(col, sizeof(Glyph));
1464 /* update terminal size */
1465 term.col = col, term.row = row;
1466 /* make use of the LIMIT in tmoveto */
1467 tmoveto(term.c.x, term.c.y);
1468 /* reset scrolling region */
1469 tsetscroll(0, row-1);
1474 xresize(int col, int row) {
1480 xw.bufw = MAX(1, col * xw.cw);
1481 xw.bufh = MAX(1, row * xw.ch);
1482 newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1483 XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
1484 XFreePixmap(xw.dpy, xw.buf);
1485 XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
1487 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
1488 xw.bufw-oldw, MIN(xw.bufh, oldh));
1489 else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
1490 XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
1491 xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
1494 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
1495 xw.bufw, xw.bufh-oldh);
1496 else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
1497 XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
1498 xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
1507 unsigned long white = WhitePixel(xw.dpy, xw.scr);
1509 for(i = 0; i < 16; i++) {
1510 if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1512 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1514 dc.col[i] = color.pixel;
1517 /* same colors as xterm */
1518 for(r = 0; r < 6; r++)
1519 for(g = 0; g < 6; g++)
1520 for(b = 0; b < 6; b++) {
1521 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1522 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1523 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1524 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1526 fprintf(stderr, "Could not allocate color %d\n", i);
1528 dc.col[i] = color.pixel;
1532 for(r = 0; r < 24; r++, i++) {
1533 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1534 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1536 fprintf(stderr, "Could not allocate color %d\n", i);
1538 dc.col[i] = color.pixel;
1543 xclear(int x1, int y1, int x2, int y2) {
1544 XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
1545 XFillRectangle(xw.dpy, xw.buf, dc.gc,
1546 x1 * xw.cw, y1 * xw.ch,
1547 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1552 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1553 XWMHints wm = {.flags = InputHint, .input = 1};
1555 .flags = PSize | PResizeInc | PBaseSize,
1558 .height_inc = xw.ch,
1560 .base_height = 2*BORDER,
1561 .base_width = 2*BORDER,
1563 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1567 xinitfont(char *fontstr) {
1569 char *def, **missing;
1573 set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1576 fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1577 XFreeStringList(missing);
1583 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
1584 XFontStruct **xfonts;
1588 *ascent = *descent = *lbearing = *rbearing = 0;
1589 n = XFontsOfFontSet(set, &xfonts, &font_names);
1590 for(i = 0; i < n; i++) {
1591 *ascent = MAX(*ascent, (*xfonts)->ascent);
1592 *descent = MAX(*descent, (*xfonts)->descent);
1593 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1594 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1600 initfonts(char *fontstr, char *bfontstr) {
1601 if((dc.font.set = xinitfont(fontstr)) == NULL ||
1602 (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1603 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1604 xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1605 &dc.font.lbearing, &dc.font.rbearing);
1606 xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1607 &dc.bfont.lbearing, &dc.bfont.rbearing);
1612 XSetWindowAttributes attrs;
1616 if(!(xw.dpy = XOpenDisplay(NULL)))
1617 die("Can't open display\n");
1618 xw.scr = XDefaultScreen(xw.dpy);
1621 initfonts(FONT, BOLDFONT);
1623 /* XXX: Assuming same size for bold font */
1624 xw.cw = dc.font.rbearing - dc.font.lbearing;
1625 xw.ch = dc.font.ascent + dc.font.descent;
1628 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1631 /* window - default size */
1632 xw.bufh = 24 * xw.ch;
1633 xw.bufw = 80 * xw.cw;
1634 xw.h = xw.bufh + 2*BORDER;
1635 xw.w = xw.bufw + 2*BORDER;
1637 attrs.background_pixel = dc.col[DefaultBG];
1638 attrs.border_pixel = dc.col[DefaultBG];
1639 attrs.bit_gravity = NorthWestGravity;
1640 attrs.event_mask = FocusChangeMask | KeyPressMask
1641 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1642 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
1643 | EnterWindowMask | LeaveWindowMask;
1644 attrs.colormap = xw.cmap;
1646 parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
1647 xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
1648 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1649 XDefaultVisual(xw.dpy, xw.scr),
1650 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1653 xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1657 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1658 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1659 | XIMStatusNothing, XNClientWindow, xw.win,
1660 XNFocusWindow, xw.win, NULL);
1662 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1664 /* white cursor, black outline */
1665 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
1666 XDefineCursor(xw.dpy, xw.win, cursor);
1667 XRecolorCursor(xw.dpy, cursor,
1668 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
1669 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
1671 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1672 XMapWindow(xw.dpy, xw.win);
1678 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1679 unsigned long xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
1680 int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1683 /* only switch default fg/bg if term is in RV mode */
1684 if(IS_SET(MODE_REVERSE)) {
1685 if(base.fg == DefaultFG)
1686 xfg = dc.col[DefaultBG];
1687 if(base.bg == DefaultBG)
1688 xbg = dc.col[DefaultFG];
1691 if(base.mode & ATTR_REVERSE)
1692 temp = xfg, xfg = xbg, xbg = temp;
1694 XSetBackground(xw.dpy, dc.gc, xbg);
1695 XSetForeground(xw.dpy, dc.gc, xfg);
1697 if(base.mode & ATTR_GFX) {
1698 for(i = 0; i < bytelen; i++) {
1699 char c = gfx[(unsigned int)s[i] % 256];
1702 else if(s[i] > 0x5f)
1707 XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
1708 dc.gc, winx, winy, s, bytelen);
1710 if(base.mode & ATTR_UNDERLINE)
1711 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1716 static int oldx = 0;
1717 static int oldy = 0;
1719 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
1721 LIMIT(oldx, 0, term.col-1);
1722 LIMIT(oldy, 0, term.row-1);
1724 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1725 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
1727 /* remove the old cursor */
1728 if(term.line[oldy][oldx].state & GLYPH_SET) {
1729 sl = utf8size(term.line[oldy][oldx].c);
1730 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
1732 xclear(oldx, oldy, oldx, oldy);
1734 /* draw the new one */
1735 if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
1737 if(IS_SET(MODE_REVERSE))
1738 g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
1739 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
1740 oldx = term.c.x, oldy = term.c.y;
1746 drawregion(0, 0, term.col, term.row);
1750 drawregion(int x1, int y1, int x2, int y2) {
1751 int ic, ib, x, y, ox, sl;
1753 char buf[DRAW_BUF_SIZ];
1755 if(!(xw.state & WIN_VISIBLE))
1758 xclear(x1, y1, x2-1, y2-1);
1759 for(y = y1; y < y2; y++) {
1760 base = term.line[y][0];
1762 for(x = x1; x < x2; x++) {
1763 new = term.line[y][x];
1764 if(sel.bx != -1 && *(new.c) && selected(x, y))
1765 new.mode ^= ATTR_REVERSE;
1766 if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1767 ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
1768 xdraws(buf, base, ox, y, ic, ib);
1771 if(new.state & GLYPH_SET) {
1776 sl = utf8size(new.c);
1777 memcpy(buf+ib, new.c, sl);
1783 xdraws(buf, base, ox, y, ic, ib);
1786 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1790 expose(XEvent *ev) {
1791 XExposeEvent *e = &ev->xexpose;
1792 if(xw.state & WIN_REDRAW) {
1794 xw.state &= ~WIN_REDRAW;
1798 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
1799 e->width, e->height, e->x, e->y);
1803 visibility(XEvent *ev) {
1804 XVisibilityEvent *e = &ev->xvisibility;
1805 if(e->state == VisibilityFullyObscured)
1806 xw.state &= ~WIN_VISIBLE;
1807 else if(!(xw.state & WIN_VISIBLE))
1808 /* need a full redraw for next Expose, not just a buf copy */
1809 xw.state |= WIN_VISIBLE | WIN_REDRAW;
1814 xw.state &= ~WIN_VISIBLE;
1818 xseturgency(int add) {
1819 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1820 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1821 XSetWMHints(xw.dpy, xw.win, h);
1827 if(ev->type == FocusIn || ev->type == EnterNotify) {
1828 xw.state |= WIN_FOCUSED;
1831 xw.state &= ~WIN_FOCUSED;
1836 kmap(KeySym k, unsigned int state) {
1839 for(i = 0; i < LEN(key); i++) {
1840 unsigned int mask = key[i].mask;
1841 if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
1842 return (char*)key[i].s;
1848 kpress(XEvent *ev) {
1849 XKeyEvent *e = &ev->xkey;
1858 meta = e->state & Mod1Mask;
1859 shift = e->state & ShiftMask;
1860 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
1862 /* 1. custom keys from config.h */
1863 if((customkey = kmap(ksym, e->state)))
1864 ttywrite(customkey, strlen(customkey));
1865 /* 2. hardcoded (overrides X lookup) */
1872 /* XXX: shift up/down doesn't work */
1873 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
1881 if(IS_SET(MODE_CRLF))
1882 ttywrite("\r\n", 2);
1889 if(meta && len == 1)
1890 ttywrite("\033", 1);
1901 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1904 xw.w = e->xconfigure.width;
1905 xw.h = e->xconfigure.height;
1906 col = (xw.w - 2*BORDER) / xw.cw;
1907 row = (xw.h - 2*BORDER) / xw.ch;
1908 if(col == term.col && row == term.row)
1910 if(tresize(col, row))
1912 ttyresize(col, row);
1920 int xfd = XConnectionNumber(xw.dpy);
1924 FD_SET(cmdfd, &rfd);
1926 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1929 die("select failed: %s\n", SERRNO);
1931 if(FD_ISSET(cmdfd, &rfd)) {
1935 while(XPending(xw.dpy)) {
1936 XNextEvent(xw.dpy, &ev);
1937 if(XFilterEvent(&ev, xw.win))
1939 if(handler[ev.type])
1940 (handler[ev.type])(&ev);
1946 main(int argc, char *argv[]) {
1949 for(i = 1; i < argc; i++) {
1950 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
1952 if(++i < argc) opt_title = argv[i];
1955 if(++i < argc) opt_class = argv[i];
1958 if(++i < argc) opt_embed = argv[i];
1961 /* eat every remaining arguments */
1962 if(++i < argc) opt_cmd = &argv[i];
1971 setlocale(LC_CTYPE, "");