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] [-g geometry]" \
40 " [-w windowid] [-v] [-f file] [-e command...]\n"
43 #define XEMBED_FOCUS_IN 4
44 #define XEMBED_FOCUS_OUT 5
47 #define ESC_BUF_SIZ 256
48 #define ESC_ARG_SIZ 16
49 #define STR_BUF_SIZ 256
50 #define STR_ARG_SIZ 16
51 #define DRAW_BUF_SIZ 1024
53 #define XK_NO_MOD UINT_MAX
56 #define SELECT_TIMEOUT (20*1000) /* 20 ms */
57 #define DRAW_TIMEOUT (20*1000) /* 20 ms */
58 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
60 #define SERRNO strerror(errno)
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 #define MAX(a, b) ((a) < (b) ? (b) : (a))
63 #define LEN(a) (sizeof(a) / sizeof(a[0]))
64 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
65 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
66 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
67 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
68 #define IS_SET(flag) (term.mode & (flag))
69 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
70 #define X2COL(x) (((x) - BORDER)/xw.cw)
71 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
73 enum glyph_attribute {
81 enum cursor_movement {
108 MODE_MOUSEMOTION = 64,
116 ESC_STR = 4, /* DSC, OSC, PM, APC */
118 ESC_STR_END = 16, /* a final string was encountered */
129 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
131 typedef unsigned char uchar;
132 typedef unsigned int uint;
133 typedef unsigned long ulong;
134 typedef unsigned short ushort;
137 char c[UTF_SIZ]; /* character code */
138 uchar mode; /* attribute flags */
139 ushort fg; /* foreground */
140 ushort bg; /* background */
141 uchar state; /* state flags */
147 Glyph attr; /* current char attributes */
153 /* CSI Escape sequence structs */
154 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
156 char buf[ESC_BUF_SIZ]; /* raw string */
157 int len; /* raw string length */
159 int arg[ESC_ARG_SIZ];
160 int narg; /* nb of args */
164 /* STR Escape sequence structs */
165 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
167 char type; /* ESC type ... */
168 char buf[STR_BUF_SIZ]; /* raw string */
169 int len; /* raw string length */
170 char *args[STR_ARG_SIZ];
171 int narg; /* nb of args */
174 /* Internal representation of the screen */
176 int row; /* nb row */
177 int col; /* nb col */
178 Line* line; /* screen */
179 Line* alt; /* alternate screen */
180 bool* dirty; /* dirtyness of lines */
181 TCursor c; /* cursor */
182 int top; /* top scroll limit */
183 int bot; /* bottom scroll limit */
184 int mode; /* terminal mode flags */
185 int esc; /* escape state flags */
189 /* Purely graphic info */
199 Bool isfixed; /* is fixed geometry? */
200 int fx, fy, fw, fh; /* fixed geometry */
201 int w; /* window width */
202 int h; /* window height */
203 int ch; /* char height */
204 int cw; /* char width */
205 char state; /* focus, redraw, visible */
206 struct timeval lastdraw;
216 /* TODO: use better name for vars... */
221 struct {int x, y;} b, e;
225 struct timeval tclick1;
226 struct timeval tclick2;
231 /* Drawing Context */
233 ulong col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
244 static void die(const char*, ...);
245 static void draw(void);
246 static void redraw(void);
247 static void drawregion(int, int, int, int);
248 static void execsh(void);
249 static void sigchld(int);
250 static void run(void);
251 static bool last_draw_too_old(void);
253 static void csidump(void);
254 static void csihandle(void);
255 static void csiparse(void);
256 static void csireset(void);
257 static void strdump(void);
258 static void strhandle(void);
259 static void strparse(void);
260 static void strreset(void);
262 static void tclearregion(int, int, int, int);
263 static void tcursor(int);
264 static void tdeletechar(int);
265 static void tdeleteline(int);
266 static void tinsertblank(int);
267 static void tinsertblankline(int);
268 static void tmoveto(int, int);
269 static void tnew(int, int);
270 static void tnewline(int);
271 static void tputtab(bool);
272 static void tputc(char*);
273 static void treset(void);
274 static int tresize(int, int);
275 static void tscrollup(int, int);
276 static void tscrolldown(int, int);
277 static void tsetattr(int*, int);
278 static void tsetchar(char*);
279 static void tsetscroll(int, int);
280 static void tswapscreen(void);
281 static void tsetdirt(int, int);
282 static void tsetmode(bool, bool, int *, int);
283 static void tfulldirt(void);
285 static void ttynew(void);
286 static void ttyread(void);
287 static void ttyresize(int, int);
288 static void ttywrite(const char *, size_t);
290 static void xdraws(char *, Glyph, int, int, int, int);
291 static void xhints(void);
292 static void xclear(int, int, int, int);
294 static void xdrawcursor(void);
295 static void xinit(void);
296 static void xloadcols(void);
297 static void xseturgency(int);
298 static void xsetsel(char*);
299 static void xresize(int, int);
301 static void expose(XEvent *);
302 static void visibility(XEvent *);
303 static void unmap(XEvent *);
304 static char* kmap(KeySym, uint);
305 static void kpress(XEvent *);
306 static void cmessage(XEvent *);
307 static void resize(XEvent *);
308 static void focus(XEvent *);
309 static void brelease(XEvent *);
310 static void bpress(XEvent *);
311 static void bmotion(XEvent *);
312 static void selnotify(XEvent *);
313 static void selrequest(XEvent *);
315 static void selinit(void);
316 static inline bool selected(int, int);
317 static void selcopy(void);
318 static void selpaste();
319 static void selscroll(int, int);
321 static int utf8decode(char *, long *);
322 static int utf8encode(long *, char *);
323 static int utf8size(char *);
324 static int isfullutf8(char *, int);
326 static void (*handler[LASTEvent])(XEvent *) = {
328 [ClientMessage] = cmessage,
329 [ConfigureNotify] = resize,
330 [VisibilityNotify] = visibility,
331 [UnmapNotify] = unmap,
335 [MotionNotify] = bmotion,
336 [ButtonPress] = bpress,
337 [ButtonRelease] = brelease,
338 [SelectionNotify] = selnotify,
339 [SelectionRequest] = selrequest,
346 static CSIEscape csiescseq;
347 static STREscape strescseq;
350 static Selection sel;
352 static char **opt_cmd = NULL;
353 static char *opt_io = NULL;
354 static char *opt_title = NULL;
355 static char *opt_embed = NULL;
356 static char *opt_class = NULL;
359 utf8decode(char *s, long *u) {
365 if(~c & B7) { /* 0xxxxxxx */
368 } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
369 *u = c&(B4|B3|B2|B1|B0);
371 } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
372 *u = c&(B3|B2|B1|B0);
374 } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
379 for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
381 if((c & (B7|B6)) != B7) /* 10xxxxxx */
384 *u |= c & (B5|B4|B3|B2|B1|B0);
386 if((n == 1 && *u < 0x80) ||
387 (n == 2 && *u < 0x800) ||
388 (n == 3 && *u < 0x10000) ||
389 (*u >= 0xD800 && *u <= 0xDFFF))
398 utf8encode(long *u, char *s) {
406 *sp = uc; /* 0xxxxxxx */
408 } else if(*u < 0x800) {
409 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
411 } else if(uc < 0x10000) {
412 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
414 } else if(uc <= 0x10FFFF) {
415 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
420 for(i=n,++sp; i>0; --i,++sp)
421 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
431 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
432 UTF-8 otherwise return 0 */
434 isfullutf8(char *s, int b) {
442 else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
444 else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
446 ((b == 2) && (*c2&(B7|B6)) == B7)))
448 else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
450 ((b == 2) && (*c2&(B7|B6)) == B7) ||
451 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
463 else if((c&(B7|B6|B5)) == (B7|B6))
465 else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
473 memset(&sel.tclick1, 0, sizeof(sel.tclick1));
474 memset(&sel.tclick2, 0, sizeof(sel.tclick2));
478 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
479 if(sel.xtarget == None)
480 sel.xtarget = XA_STRING;
484 selected(int x, int y) {
485 if(sel.ey == y && sel.by == y) {
486 int bx = MIN(sel.bx, sel.ex);
487 int ex = MAX(sel.bx, sel.ex);
488 return BETWEEN(x, bx, ex);
490 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
491 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
495 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
497 *b = e->xbutton.button;
499 *x = X2COL(e->xbutton.x);
500 *y = Y2ROW(e->xbutton.y);
501 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
502 sel.b.y = MIN(sel.by, sel.ey);
503 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
504 sel.e.y = MAX(sel.by, sel.ey);
508 mousereport(XEvent *e) {
509 int x = X2COL(e->xbutton.x);
510 int y = Y2ROW(e->xbutton.y);
511 int button = e->xbutton.button;
512 int state = e->xbutton.state;
513 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
514 static int ob, ox, oy;
517 if(e->xbutton.type == MotionNotify) {
518 if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
522 } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
528 if(e->xbutton.type == ButtonPress) {
534 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
535 + (state & Mod4Mask ? 8 : 0)
536 + (state & ControlMask ? 16 : 0);
538 ttywrite(buf, sizeof(buf));
543 if(IS_SET(MODE_MOUSE))
545 else if(e->xbutton.button == Button1) {
547 tsetdirt(sel.b.y, sel.e.y);
549 sel.ex = sel.bx = X2COL(e->xbutton.x);
550 sel.ey = sel.by = Y2ROW(e->xbutton.y);
557 int x, y, bufsize, is_selected = 0;
563 bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
564 ptr = str = malloc(bufsize);
566 /* append every set & selected glyph to the selection */
567 for(y = 0; y < term.row; y++) {
568 for(x = 0; x < term.col; x++) {
569 is_selected = selected(x, y);
570 if((term.line[y][x].state & GLYPH_SET) && is_selected) {
571 int size = utf8size(term.line[y][x].c);
572 memcpy(ptr, term.line[y][x].c, size);
577 /* \n at the end of every selected line except for the last one */
578 if(is_selected && y < sel.e.y)
583 sel.alt = IS_SET(MODE_ALTSCREEN);
588 selnotify(XEvent *e) {
589 ulong nitems, ofs, rem;
596 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
597 False, AnyPropertyType, &type, &format,
598 &nitems, &rem, &data)) {
599 fprintf(stderr, "Clipboard allocation failed\n");
602 ttywrite((const char *) data, nitems * format / 8);
604 /* number of 32-bit chunks returned */
605 ofs += nitems * format / 32;
611 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
615 selrequest(XEvent *e) {
616 XSelectionRequestEvent *xsre;
620 xsre = (XSelectionRequestEvent *) e;
621 xev.type = SelectionNotify;
622 xev.requestor = xsre->requestor;
623 xev.selection = xsre->selection;
624 xev.target = xsre->target;
625 xev.time = xsre->time;
629 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
630 if(xsre->target == xa_targets) {
631 /* respond with the supported type */
632 Atom string = sel.xtarget;
633 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
634 XA_ATOM, 32, PropModeReplace,
635 (uchar *) &string, 1);
636 xev.property = xsre->property;
637 } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
638 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
639 xsre->target, 8, PropModeReplace,
640 (uchar *) sel.clip, strlen(sel.clip));
641 xev.property = xsre->property;
644 /* all done, send a notification to the listener */
645 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
646 fprintf(stderr, "Error sending SelectionNotify event\n");
651 /* register the selection for both the clipboard and the primary */
657 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
659 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
660 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
666 brelease(XEvent *e) {
667 if(IS_SET(MODE_MOUSE)) {
671 if(e->xbutton.button == Button2)
673 else if(e->xbutton.button == Button1) {
675 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
676 term.dirty[sel.ey] = 1;
677 if(sel.bx == sel.ex && sel.by == sel.ey) {
680 gettimeofday(&now, NULL);
682 if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
683 /* triple click on the line */
684 sel.b.x = sel.bx = 0;
685 sel.e.x = sel.ex = term.col;
686 sel.b.y = sel.e.y = sel.ey;
688 } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
689 /* double click to select word */
691 while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
692 term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
694 while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
695 term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
697 sel.b.y = sel.e.y = sel.ey;
703 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
704 gettimeofday(&sel.tclick1, NULL);
710 if(IS_SET(MODE_MOUSE)) {
715 int oldey = sel.ey, oldex = sel.ex;
716 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
718 if(oldey != sel.ey || oldex != sel.ex) {
719 int starty = MIN(oldey, sel.ey);
720 int endy = MAX(oldey, sel.ey);
721 tsetdirt(starty, endy);
728 die(const char *errstr, ...) {
731 va_start(ap, errstr);
732 vfprintf(stderr, errstr, ap);
740 char *envshell = getenv("SHELL");
742 DEFAULT(envshell, SHELL);
743 putenv("TERM="TNAME);
744 args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
745 execvp(args[0], args);
752 if(waitpid(pid, &stat, 0) < 0)
753 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
755 exit(WEXITSTATUS(stat));
764 /* seems to work fine on linux, openbsd and freebsd */
765 struct winsize w = {term.row, term.col, 0, 0};
766 if(openpty(&m, &s, NULL, NULL, &w) < 0)
767 die("openpty failed: %s\n", SERRNO);
769 switch(pid = fork()) {
771 die("fork failed\n");
774 setsid(); /* create a new process group */
775 dup2(s, STDIN_FILENO);
776 dup2(s, STDOUT_FILENO);
777 dup2(s, STDERR_FILENO);
778 if(ioctl(s, TIOCSCTTY, NULL) < 0)
779 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
787 signal(SIGCHLD, sigchld);
788 if(opt_io && !(fileio = fopen(opt_io, "w"))) {
789 fprintf(stderr, "Error opening %s:%s\n",
790 opt_io, strerror(errno));
798 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
800 fprintf(stderr, "\n");
805 static char buf[BUFSIZ];
806 static int buflen = 0;
809 int charsize; /* size of utf8 char in bytes */
813 /* append read bytes to unprocessed bytes */
814 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
815 die("Couldn't read from shell: %s\n", SERRNO);
817 /* process every complete utf8 char */
820 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
821 charsize = utf8decode(ptr, &utf8c);
822 utf8encode(&utf8c, s);
828 /* keep any uncomplete utf8 char for the next call */
829 memmove(buf, ptr, buflen);
833 ttywrite(const char *s, size_t n) {
834 if(write(cmdfd, s, n) == -1)
835 die("write error on tty: %s\n", SERRNO);
839 ttyresize(int x, int y) {
846 w.ws_xpixel = w.ws_ypixel = 0;
847 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
848 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
852 tsetdirt(int top, int bot)
856 LIMIT(top, 0, term.row-1);
857 LIMIT(bot, 0, term.row-1);
859 for(i = top; i <= bot; i++)
866 tsetdirt(0, term.row-1);
873 if(mode == CURSOR_SAVE)
875 else if(mode == CURSOR_LOAD)
876 term.c = c, tmoveto(c.x, c.y);
886 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
888 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
889 for(i = TAB; i < term.col; i += TAB)
891 term.top = 0, term.bot = term.row - 1;
892 term.mode = MODE_WRAP;
893 tclearregion(0, 0, term.col-1, term.row-1);
897 tnew(int col, int row) {
898 /* set screen size */
899 term.row = row, term.col = col;
900 term.line = malloc(term.row * sizeof(Line));
901 term.alt = malloc(term.row * sizeof(Line));
902 term.dirty = malloc(term.row * sizeof(*term.dirty));
903 term.tabs = malloc(term.col * sizeof(*term.tabs));
905 for(row = 0; row < term.row; row++) {
906 term.line[row] = malloc(term.col * sizeof(Glyph));
907 term.alt [row] = malloc(term.col * sizeof(Glyph));
910 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
917 Line* tmp = term.line;
918 term.line = term.alt;
920 term.mode ^= MODE_ALTSCREEN;
925 tscrolldown(int orig, int n) {
929 LIMIT(n, 0, term.bot-orig+1);
931 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
933 for(i = term.bot; i >= orig+n; i--) {
935 term.line[i] = term.line[i-n];
936 term.line[i-n] = temp;
946 tscrollup(int orig, int n) {
949 LIMIT(n, 0, term.bot-orig+1);
951 tclearregion(0, orig, term.col-1, orig+n-1);
953 for(i = orig; i <= term.bot-n; i++) {
955 term.line[i] = term.line[i+n];
956 term.line[i+n] = temp;
966 selscroll(int orig, int n) {
970 if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
971 if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
975 if(sel.by < term.top) {
979 if(sel.ey > term.bot) {
983 sel.b.y = sel.by, sel.b.x = sel.bx;
984 sel.e.y = sel.ey, sel.e.x = sel.ex;
989 tnewline(int first_col) {
992 tscrollup(term.top, 1);
995 tmoveto(first_col ? 0 : term.c.x, y);
1000 /* int noarg = 1; */
1001 char *p = csiescseq.buf;
1005 csiescseq.priv = 1, p++;
1007 while(p < csiescseq.buf+csiescseq.len) {
1008 while(isdigit(*p)) {
1009 csiescseq.arg[csiescseq.narg] *= 10;
1010 csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
1012 if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ)
1013 csiescseq.narg++, p++;
1015 csiescseq.mode = *p;
1023 tmoveto(int x, int y) {
1024 LIMIT(x, 0, term.col-1);
1025 LIMIT(y, 0, term.row-1);
1026 term.c.state &= ~CURSOR_WRAPNEXT;
1033 term.dirty[term.c.y] = 1;
1034 term.line[term.c.y][term.c.x] = term.c.attr;
1035 memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
1036 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
1040 tclearregion(int x1, int y1, int x2, int y2) {
1044 temp = x1, x1 = x2, x2 = temp;
1046 temp = y1, y1 = y2, y2 = temp;
1048 LIMIT(x1, 0, term.col-1);
1049 LIMIT(x2, 0, term.col-1);
1050 LIMIT(y1, 0, term.row-1);
1051 LIMIT(y2, 0, term.row-1);
1053 for(y = y1; y <= y2; y++) {
1055 for(x = x1; x <= x2; x++)
1056 term.line[y][x].state = 0;
1061 tdeletechar(int n) {
1062 int src = term.c.x + n;
1064 int size = term.col - src;
1066 term.dirty[term.c.y] = 1;
1068 if(src >= term.col) {
1069 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1072 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
1073 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
1077 tinsertblank(int n) {
1080 int size = term.col - dst;
1082 term.dirty[term.c.y] = 1;
1084 if(dst >= term.col) {
1085 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1088 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
1089 tclearregion(src, term.c.y, dst - 1, term.c.y);
1093 tinsertblankline(int n) {
1094 if(term.c.y < term.top || term.c.y > term.bot)
1097 tscrolldown(term.c.y, n);
1101 tdeleteline(int n) {
1102 if(term.c.y < term.top || term.c.y > term.bot)
1105 tscrollup(term.c.y, n);
1109 tsetattr(int *attr, int l) {
1112 for(i = 0; i < l; i++) {
1115 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
1116 term.c.attr.fg = DefaultFG;
1117 term.c.attr.bg = DefaultBG;
1120 term.c.attr.mode |= ATTR_BOLD;
1122 case 3: /* enter standout (highlight) mode TODO: make it italic */
1123 term.c.attr.mode |= ATTR_REVERSE;
1126 term.c.attr.mode |= ATTR_UNDERLINE;
1129 term.c.attr.mode |= ATTR_REVERSE;
1132 term.c.attr.mode &= ~ATTR_BOLD;
1134 case 23: /* leave standout (highlight) mode TODO: make it italic */
1135 term.c.attr.mode &= ~ATTR_REVERSE;
1138 term.c.attr.mode &= ~ATTR_UNDERLINE;
1141 term.c.attr.mode &= ~ATTR_REVERSE;
1144 if(i + 2 < l && attr[i + 1] == 5) {
1146 if(BETWEEN(attr[i], 0, 255))
1147 term.c.attr.fg = attr[i];
1149 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
1152 fprintf(stderr, "erresc(38): gfx attr %d unknown\n", attr[i]);
1155 term.c.attr.fg = DefaultFG;
1158 if(i + 2 < l && attr[i + 1] == 5) {
1160 if(BETWEEN(attr[i], 0, 255))
1161 term.c.attr.bg = attr[i];
1163 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
1166 fprintf(stderr, "erresc(48): gfx attr %d unknown\n", attr[i]);
1169 term.c.attr.bg = DefaultBG;
1172 if(BETWEEN(attr[i], 30, 37))
1173 term.c.attr.fg = attr[i] - 30;
1174 else if(BETWEEN(attr[i], 40, 47))
1175 term.c.attr.bg = attr[i] - 40;
1176 else if(BETWEEN(attr[i], 90, 97))
1177 term.c.attr.fg = attr[i] - 90 + 8;
1178 else if(BETWEEN(attr[i], 100, 107))
1179 term.c.attr.fg = attr[i] - 100 + 8;
1181 fprintf(stderr, "erresc(default): gfx attr %d unknown\n", attr[i]), csidump();
1188 tsetscroll(int t, int b) {
1191 LIMIT(t, 0, term.row-1);
1192 LIMIT(b, 0, term.row-1);
1202 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1205 tsetmode(bool priv, bool set, int *args, int narg) {
1208 for(lim = args + narg; args < lim; ++args) {
1212 MODBIT(term.mode, set, MODE_APPKEYPAD);
1214 case 5: /* DECSCNM -- Reverve video */
1216 MODBIT(term.mode,set, MODE_REVERSE);
1217 if(mode != term.mode)
1221 MODBIT(term.mode, set, MODE_WRAP);
1224 MODBIT(term.mode, set, MODE_CRLF);
1226 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1229 MODBIT(term.c.state, !set, CURSOR_HIDE);
1231 case 1000: /* 1000,1002: enable xterm mouse report */
1232 MODBIT(term.mode, set, MODE_MOUSEBTN);
1235 MODBIT(term.mode, set, MODE_MOUSEMOTION);
1237 case 1049: /* = 1047 and 1048 */
1240 if(IS_SET(MODE_ALTSCREEN))
1241 tclearregion(0, 0, term.col-1, term.row-1);
1242 if((set && !IS_SET(MODE_ALTSCREEN)) ||
1243 (!set && IS_SET(MODE_ALTSCREEN))) {
1250 tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
1254 "erresc: unknown private set/reset mode %d\n",
1261 MODBIT(term.mode, set, MODE_INSERT);
1265 "erresc: unknown set/reset mode %d\n",
1277 switch(csiescseq.mode) {
1280 fprintf(stderr, "erresc: unknown csi ");
1284 case '@': /* ICH -- Insert <n> blank char */
1285 DEFAULT(csiescseq.arg[0], 1);
1286 tinsertblank(csiescseq.arg[0]);
1288 case 'A': /* CUU -- Cursor <n> Up */
1290 DEFAULT(csiescseq.arg[0], 1);
1291 tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
1293 case 'B': /* CUD -- Cursor <n> Down */
1294 DEFAULT(csiescseq.arg[0], 1);
1295 tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
1297 case 'C': /* CUF -- Cursor <n> Forward */
1299 DEFAULT(csiescseq.arg[0], 1);
1300 tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
1302 case 'D': /* CUB -- Cursor <n> Backward */
1303 DEFAULT(csiescseq.arg[0], 1);
1304 tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
1306 case 'E': /* CNL -- Cursor <n> Down and first col */
1307 DEFAULT(csiescseq.arg[0], 1);
1308 tmoveto(0, term.c.y+csiescseq.arg[0]);
1310 case 'F': /* CPL -- Cursor <n> Up and first col */
1311 DEFAULT(csiescseq.arg[0], 1);
1312 tmoveto(0, term.c.y-csiescseq.arg[0]);
1314 case 'g': /* TBC -- Tabulation clear */
1315 switch (csiescseq.arg[0]) {
1316 case 0: /* clear current tab stop */
1317 term.tabs[term.c.x] = 0;
1319 case 3: /* clear all the tabs */
1320 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
1326 case 'G': /* CHA -- Move to <col> */
1328 DEFAULT(csiescseq.arg[0], 1);
1329 tmoveto(csiescseq.arg[0]-1, term.c.y);
1331 case 'H': /* CUP -- Move to <row> <col> */
1333 DEFAULT(csiescseq.arg[0], 1);
1334 DEFAULT(csiescseq.arg[1], 1);
1335 tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
1337 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1338 DEFAULT(csiescseq.arg[0], 1);
1339 while(csiescseq.arg[0]--)
1342 case 'J': /* ED -- Clear screen */
1344 switch(csiescseq.arg[0]) {
1346 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1347 if(term.c.y < term.row-1)
1348 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1352 tclearregion(0, 0, term.col-1, term.c.y-1);
1353 tclearregion(0, term.c.y, term.c.x, term.c.y);
1356 tclearregion(0, 0, term.col-1, term.row-1);
1362 case 'K': /* EL -- Clear line */
1363 switch(csiescseq.arg[0]) {
1365 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1368 tclearregion(0, term.c.y, term.c.x, term.c.y);
1371 tclearregion(0, term.c.y, term.col-1, term.c.y);
1375 case 'S': /* SU -- Scroll <n> line up */
1376 DEFAULT(csiescseq.arg[0], 1);
1377 tscrollup(term.top, csiescseq.arg[0]);
1379 case 'T': /* SD -- Scroll <n> line down */
1380 DEFAULT(csiescseq.arg[0], 1);
1381 tscrolldown(term.top, csiescseq.arg[0]);
1383 case 'L': /* IL -- Insert <n> blank lines */
1384 DEFAULT(csiescseq.arg[0], 1);
1385 tinsertblankline(csiescseq.arg[0]);
1387 case 'l': /* RM -- Reset Mode */
1388 tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
1390 case 'M': /* DL -- Delete <n> lines */
1391 DEFAULT(csiescseq.arg[0], 1);
1392 tdeleteline(csiescseq.arg[0]);
1394 case 'X': /* ECH -- Erase <n> char */
1395 DEFAULT(csiescseq.arg[0], 1);
1396 tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0], term.c.y);
1398 case 'P': /* DCH -- Delete <n> char */
1399 DEFAULT(csiescseq.arg[0], 1);
1400 tdeletechar(csiescseq.arg[0]);
1402 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1403 DEFAULT(csiescseq.arg[0], 1);
1404 while(csiescseq.arg[0]--)
1407 case 'd': /* VPA -- Move to <row> */
1408 DEFAULT(csiescseq.arg[0], 1);
1409 tmoveto(term.c.x, csiescseq.arg[0]-1);
1411 case 'h': /* SM -- Set terminal mode */
1412 tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
1414 case 'm': /* SGR -- Terminal attribute (color) */
1415 tsetattr(csiescseq.arg, csiescseq.narg);
1417 case 'r': /* DECSTBM -- Set Scrolling Region */
1421 DEFAULT(csiescseq.arg[0], 1);
1422 DEFAULT(csiescseq.arg[1], term.row);
1423 tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
1427 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1428 tcursor(CURSOR_SAVE);
1430 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1431 tcursor(CURSOR_LOAD);
1440 for(i = 0; i < csiescseq.len; i++) {
1441 uint c = csiescseq.buf[i] & 0xff;
1442 if(isprint(c)) putchar(c);
1443 else if(c == '\n') printf("(\\n)");
1444 else if(c == '\r') printf("(\\r)");
1445 else if(c == 0x1b) printf("(\\e)");
1446 else printf("(%02x)", c);
1453 memset(&csiescseq, 0, sizeof(csiescseq));
1461 * TODO: make this being useful in case of color palette change.
1467 switch(strescseq.type) {
1468 case ']': /* OSC -- Operating System Command */
1474 * TODO: Handle special chars in string, like umlauts.
1477 XStoreName(xw.dpy, xw.win, strescseq.buf+2);
1481 XStoreName(xw.dpy, xw.win, strescseq.buf+1);
1483 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1486 fprintf(stderr, "erresc: unknown str ");
1491 case 'P': /* DSC -- Device Control String */
1492 case '_': /* APC -- Application Program Command */
1493 case '^': /* PM -- Privacy Message */
1495 fprintf(stderr, "erresc: unknown str ");
1505 * TODO: Implement parsing like for CSI when required.
1506 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1514 printf("ESC%c", strescseq.type);
1515 for(i = 0; i < strescseq.len; i++) {
1516 uint c = strescseq.buf[i] & 0xff;
1517 if(isprint(c)) putchar(c);
1518 else if(c == '\n') printf("(\\n)");
1519 else if(c == '\r') printf("(\\r)");
1520 else if(c == 0x1b) printf("(\\e)");
1521 else printf("(%02x)", c);
1528 memset(&strescseq, 0, sizeof(strescseq));
1532 tputtab(bool forward) {
1533 unsigned x = term.c.x;
1538 for(++x; x < term.col && !term.tabs[x]; ++x)
1543 for(--x; x > 0 && !term.tabs[x]; --x)
1546 tmoveto(x, term.c.y);
1554 putc(ascii, fileio);
1556 if(term.esc & ESC_START) {
1557 if(term.esc & ESC_CSI) {
1558 csiescseq.buf[csiescseq.len++] = ascii;
1559 if(BETWEEN(ascii, 0x40, 0x7E) || csiescseq.len >= ESC_BUF_SIZ) {
1561 csiparse(), csihandle();
1563 } else if(term.esc & ESC_STR) {
1566 term.esc = ESC_START | ESC_STR_END;
1568 case '\a': /* backwards compatibility to xterm */
1573 strescseq.buf[strescseq.len++] = ascii;
1574 if(strescseq.len+1 >= STR_BUF_SIZ) {
1579 } else if(term.esc & ESC_STR_END) {
1583 } else if(term.esc & ESC_ALTCHARSET) {
1585 case '0': /* Line drawing crap */
1586 term.c.attr.mode |= ATTR_GFX;
1588 case 'B': /* Back to regular text */
1589 term.c.attr.mode &= ~ATTR_GFX;
1592 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1598 term.esc |= ESC_CSI;
1600 case 'P': /* DCS -- Device Control String */
1601 case '_': /* APC -- Application Program Command */
1602 case '^': /* PM -- Privacy Message */
1603 case ']': /* OSC -- Operating System Command */
1605 strescseq.type = ascii;
1606 term.esc |= ESC_STR;
1609 term.esc |= ESC_ALTCHARSET;
1611 case 'D': /* IND -- Linefeed */
1612 if(term.c.y == term.bot)
1613 tscrollup(term.top, 1);
1615 tmoveto(term.c.x, term.c.y+1);
1618 case 'E': /* NEL -- Next line */
1619 tnewline(1); /* always go to first col */
1622 case 'H': /* HTS -- Horizontal tab stop */
1623 term.tabs[term.c.x] = 1;
1626 case 'M': /* RI -- Reverse index */
1627 if(term.c.y == term.top)
1628 tscrolldown(term.top, 1);
1630 tmoveto(term.c.x, term.c.y-1);
1633 case 'c': /* RIS -- Reset to inital state */
1637 case '=': /* DECPAM -- Application keypad */
1638 term.mode |= MODE_APPKEYPAD;
1641 case '>': /* DECPNM -- Normal keypad */
1642 term.mode &= ~MODE_APPKEYPAD;
1645 case '7': /* DECSC -- Save Cursor */
1646 tcursor(CURSOR_SAVE);
1649 case '8': /* DECRC -- Restore Cursor */
1650 tcursor(CURSOR_LOAD);
1653 case '\\': /* ST -- Stop */
1657 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1658 (uchar) ascii, isprint(ascii)?ascii:'.');
1663 if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
1670 tmoveto(term.c.x-1, term.c.y);
1673 tmoveto(0, term.c.y);
1678 /* go to first col if the mode is set */
1679 tnewline(IS_SET(MODE_CRLF));
1682 if(!(xw.state & WIN_FOCUSED))
1687 term.esc = ESC_START;
1690 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1691 tnewline(1); /* always go to first col */
1693 if(term.c.x+1 < term.col)
1694 tmoveto(term.c.x+1, term.c.y);
1696 term.c.state |= CURSOR_WRAPNEXT;
1702 tresize(int col, int row) {
1704 int minrow = MIN(row, term.row);
1705 int mincol = MIN(col, term.col);
1706 int slide = term.c.y - row + 1;
1708 if(col < 1 || row < 1)
1711 /* free unneeded rows */
1714 /* slide screen to keep cursor where we expect it -
1715 * tscrollup would work here, but we can optimize to
1716 * memmove because we're freeing the earlier lines */
1717 for(/* i = 0 */; i < slide; i++) {
1721 memmove(term.line, term.line + slide, row * sizeof(Line));
1722 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1724 for(i += row; i < term.row; i++) {
1729 /* resize to new height */
1730 term.line = realloc(term.line, row * sizeof(Line));
1731 term.alt = realloc(term.alt, row * sizeof(Line));
1732 term.dirty = realloc(term.dirty, row * sizeof(*term.dirty));
1733 term.tabs = realloc(term.tabs, col * sizeof(*term.tabs));
1735 /* resize each row to new width, zero-pad if needed */
1736 for(i = 0; i < minrow; i++) {
1738 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1739 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1740 for(x = mincol; x < col; x++) {
1741 term.line[i][x].state = 0;
1742 term.alt[i][x].state = 0;
1746 /* allocate any new rows */
1747 for(/* i == minrow */; i < row; i++) {
1749 term.line[i] = calloc(col, sizeof(Glyph));
1750 term.alt [i] = calloc(col, sizeof(Glyph));
1752 if(col > term.col) {
1753 bool *bp = term.tabs + term.col;
1755 memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
1756 while(--bp > term.tabs && !*bp)
1758 for(bp += TAB; bp < term.tabs + col; bp += TAB)
1761 /* update terminal size */
1762 term.col = col, term.row = row;
1763 /* make use of the LIMIT in tmoveto */
1764 tmoveto(term.c.x, term.c.y);
1765 /* reset scrolling region */
1766 tsetscroll(0, row-1);
1772 xresize(int col, int row) {
1773 xw.w = MAX(1, 2*BORDER + col * xw.cw);
1774 xw.h = MAX(1, 2*BORDER + row * xw.ch);
1781 ulong white = WhitePixel(xw.dpy, xw.scr);
1783 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1784 for(i = 0; i < LEN(colorname); i++) {
1787 if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1789 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1791 dc.col[i] = color.pixel;
1794 /* load colors [16-255] ; same colors as xterm */
1795 for(i = 16, r = 0; r < 6; r++)
1796 for(g = 0; g < 6; g++)
1797 for(b = 0; b < 6; b++) {
1798 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1799 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1800 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1801 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1803 fprintf(stderr, "Could not allocate color %d\n", i);
1805 dc.col[i] = color.pixel;
1809 for(r = 0; r < 24; r++, i++) {
1810 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1811 if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
1813 fprintf(stderr, "Could not allocate color %d\n", i);
1815 dc.col[i] = color.pixel;
1820 xclear(int x1, int y1, int x2, int y2) {
1821 XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
1822 XFillRectangle(xw.dpy, xw.buf, dc.gc,
1823 BORDER + x1 * xw.cw, BORDER + y1 * xw.ch,
1824 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1829 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1830 XWMHints wm = {.flags = InputHint, .input = 1};
1831 XSizeHints *sizeh = NULL;
1833 sizeh = XAllocSizeHints();
1834 if(xw.isfixed == False) {
1835 sizeh->flags = PSize | PResizeInc | PBaseSize;
1836 sizeh->height = xw.h;
1837 sizeh->width = xw.w;
1838 sizeh->height_inc = xw.ch;
1839 sizeh->width_inc = xw.cw;
1840 sizeh->base_height = 2*BORDER;
1841 sizeh->base_width = 2*BORDER;
1843 sizeh->flags = PMaxSize | PMinSize;
1844 sizeh->min_width = sizeh->max_width = xw.fw;
1845 sizeh->min_height = sizeh->max_height = xw.fh;
1848 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
1853 xinitfont(char *fontstr) {
1855 char *def, **missing;
1859 set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1862 fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1863 XFreeStringList(missing);
1869 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
1870 XFontStruct **xfonts;
1874 *ascent = *descent = *lbearing = *rbearing = 0;
1875 n = XFontsOfFontSet(set, &xfonts, &font_names);
1876 for(i = 0; i < n; i++) {
1877 *ascent = MAX(*ascent, (*xfonts)->ascent);
1878 *descent = MAX(*descent, (*xfonts)->descent);
1879 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1880 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1886 initfonts(char *fontstr, char *bfontstr) {
1887 if((dc.font.set = xinitfont(fontstr)) == NULL ||
1888 (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1889 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1890 xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1891 &dc.font.lbearing, &dc.font.rbearing);
1892 xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1893 &dc.bfont.lbearing, &dc.bfont.rbearing);
1898 XSetWindowAttributes attrs;
1903 if(!(xw.dpy = XOpenDisplay(NULL)))
1904 die("Can't open display\n");
1905 xw.scr = XDefaultScreen(xw.dpy);
1907 /* adjust fixed window geometry */
1909 sw = DisplayWidth(xw.dpy, xw.scr);
1910 sh = DisplayHeight(xw.dpy, xw.scr);
1912 xw.fx = sw + xw.fx - xw.fw - 1;
1914 xw.fy = sh + xw.fy - xw.fh - 1;
1919 /* window - default size */
1920 xw.h = 2*BORDER + term.row * xw.ch;
1921 xw.w = 2*BORDER + term.col * xw.cw;
1927 initfonts(FONT, BOLDFONT);
1929 /* XXX: Assuming same size for bold font */
1930 xw.cw = dc.font.rbearing - dc.font.lbearing;
1931 xw.ch = dc.font.ascent + dc.font.descent;
1934 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1937 attrs.background_pixel = dc.col[DefaultBG];
1938 attrs.border_pixel = dc.col[DefaultBG];
1939 attrs.bit_gravity = NorthWestGravity;
1940 attrs.event_mask = FocusChangeMask | KeyPressMask
1941 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1942 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
1943 | EnterWindowMask | LeaveWindowMask;
1944 attrs.colormap = xw.cmap;
1946 parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
1947 xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
1948 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1949 XDefaultVisual(xw.dpy, xw.scr),
1950 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1953 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
1957 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1958 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1959 | XIMStatusNothing, XNClientWindow, xw.win,
1960 XNFocusWindow, xw.win, NULL);
1962 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1964 /* white cursor, black outline */
1965 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
1966 XDefineCursor(xw.dpy, xw.win, cursor);
1967 XRecolorCursor(xw.dpy, cursor,
1968 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
1969 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
1971 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
1973 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1974 XMapWindow(xw.dpy, xw.win);
1980 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1981 int fg = base.fg, bg = base.bg, temp;
1982 int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1983 XFontSet fontset = dc.font.set;
1986 /* only switch default fg/bg if term is in RV mode */
1987 if(IS_SET(MODE_REVERSE)) {
1994 if(base.mode & ATTR_REVERSE)
1995 temp = fg, fg = bg, bg = temp;
1997 if(base.mode & ATTR_BOLD) {
1999 fontset = dc.bfont.set;
2002 XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
2003 XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
2005 if(base.mode & ATTR_GFX) {
2006 for(i = 0; i < bytelen; i++) {
2007 char c = gfx[(uint)s[i] % 256];
2010 else if(s[i] > 0x5f)
2015 XmbDrawImageString(xw.dpy, xw.buf, fontset, dc.gc, winx, winy, s, bytelen);
2017 if(base.mode & ATTR_UNDERLINE)
2018 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
2021 /* copy buffer pixmap to screen pixmap */
2024 XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
2025 XdbeSwapBuffers(xw.dpy, swpinfo, 1);
2031 static int oldx = 0;
2032 static int oldy = 0;
2034 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
2036 LIMIT(oldx, 0, term.col-1);
2037 LIMIT(oldy, 0, term.row-1);
2039 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
2040 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
2042 /* remove the old cursor */
2043 if(term.line[oldy][oldx].state & GLYPH_SET) {
2044 sl = utf8size(term.line[oldy][oldx].c);
2045 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
2047 xclear(oldx, oldy, oldx, oldy);
2049 xcopy(oldx, oldy, 1, 1);
2051 /* draw the new one */
2052 if(!(term.c.state & CURSOR_HIDE)) {
2053 if(!(xw.state & WIN_FOCUSED))
2056 if(IS_SET(MODE_REVERSE))
2057 g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
2060 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
2061 oldx = term.c.x, oldy = term.c.y;
2064 xcopy(term.c.x, term.c.y, 1, 1);
2069 struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
2072 nanosleep(&tv, NULL);
2077 drawregion(0, 0, term.col, term.row);
2079 gettimeofday(&xw.lastdraw, NULL);
2083 drawregion(int x1, int y1, int x2, int y2) {
2084 int ic, ib, x, y, ox, sl;
2086 char buf[DRAW_BUF_SIZ];
2087 bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
2089 if((sel.alt && !alt) || (!sel.alt && alt))
2091 if(!(xw.state & WIN_VISIBLE))
2094 for(y = y1; y < y2; y++) {
2097 xclear(0, y, term.col, y);
2099 base = term.line[y][0];
2101 for(x = x1; x < x2; x++) {
2102 new = term.line[y][x];
2103 if(ena_sel && *(new.c) && selected(x, y))
2104 new.mode ^= ATTR_REVERSE;
2105 if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
2106 ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
2107 xdraws(buf, base, ox, y, ic, ib);
2110 if(new.state & GLYPH_SET) {
2115 sl = utf8size(new.c);
2116 memcpy(buf+ib, new.c, sl);
2122 xdraws(buf, base, ox, y, ic, ib);
2128 expose(XEvent *ev) {
2129 XExposeEvent *e = &ev->xexpose;
2130 if(xw.state & WIN_REDRAW) {
2132 xw.state &= ~WIN_REDRAW;
2138 visibility(XEvent *ev) {
2139 XVisibilityEvent *e = &ev->xvisibility;
2140 if(e->state == VisibilityFullyObscured)
2141 xw.state &= ~WIN_VISIBLE;
2142 else if(!(xw.state & WIN_VISIBLE))
2143 /* need a full redraw for next Expose, not just a buf copy */
2144 xw.state |= WIN_VISIBLE | WIN_REDRAW;
2149 xw.state &= ~WIN_VISIBLE;
2153 xseturgency(int add) {
2154 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
2155 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
2156 XSetWMHints(xw.dpy, xw.win, h);
2162 if(ev->type == FocusIn) {
2163 xw.state |= WIN_FOCUSED;
2166 xw.state &= ~WIN_FOCUSED;
2171 kmap(KeySym k, uint state) {
2174 for(i = 0; i < LEN(key); i++) {
2175 uint mask = key[i].mask;
2176 if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
2177 return (char*)key[i].s;
2183 kpress(XEvent *ev) {
2184 XKeyEvent *e = &ev->xkey;
2193 meta = e->state & Mod1Mask;
2194 shift = e->state & ShiftMask;
2195 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
2197 /* 1. custom keys from config.h */
2198 if((customkey = kmap(ksym, e->state)))
2199 ttywrite(customkey, strlen(customkey));
2200 /* 2. hardcoded (overrides X lookup) */
2207 /* XXX: shift up/down doesn't work */
2208 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
2216 if(IS_SET(MODE_CRLF))
2217 ttywrite("\r\n", 2);
2224 if(meta && len == 1)
2225 ttywrite("\033", 1);
2233 cmessage(XEvent *e) {
2235 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2236 if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
2237 if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
2238 xw.state |= WIN_FOCUSED;
2240 } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
2241 xw.state &= ~WIN_FOCUSED;
2251 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
2254 xw.w = e->xconfigure.width;
2255 xw.h = e->xconfigure.height;
2256 col = (xw.w - 2*BORDER) / xw.cw;
2257 row = (xw.h - 2*BORDER) / xw.ch;
2258 if(col == term.col && row == term.row)
2260 if(tresize(col, row))
2263 ttyresize(col, row);
2267 last_draw_too_old(void) {
2269 gettimeofday(&now, NULL);
2270 return TIMEDIFF(now, xw.lastdraw) >= DRAW_TIMEOUT/1000;
2277 int xfd = XConnectionNumber(xw.dpy);
2278 struct timeval timeout = {0};
2279 bool stuff_to_print = 0;
2283 FD_SET(cmdfd, &rfd);
2286 timeout.tv_usec = SELECT_TIMEOUT;
2287 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, &timeout) < 0) {
2290 die("select failed: %s\n", SERRNO);
2292 if(FD_ISSET(cmdfd, &rfd)) {
2297 if(stuff_to_print && last_draw_too_old()) {
2302 while(XPending(xw.dpy)) {
2303 XNextEvent(xw.dpy, &ev);
2304 if(XFilterEvent(&ev, xw.win))
2306 if(handler[ev.type])
2307 (handler[ev.type])(&ev);
2313 main(int argc, char *argv[]) {
2314 int i, bitm, xr, yr;
2315 unsigned int wr, hr;
2317 xw.fw = xw.fh = xw.fx = xw.fy = 0;
2320 for(i = 1; i < argc; i++) {
2321 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
2323 if(++i < argc) opt_title = argv[i];
2326 if(++i < argc) opt_class = argv[i];
2329 if(++i < argc) opt_embed = argv[i];
2332 if(++i < argc) opt_io = argv[i];
2335 /* eat every remaining arguments */
2336 if(++i < argc) opt_cmd = &argv[i];
2342 bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
2347 if(bitm & WidthValue)
2349 if(bitm & HeightValue)
2351 if(bitm & XNegative && xw.fx == 0)
2353 if(bitm & XNegative && xw.fy == 0)
2356 if(xw.fh != 0 && xw.fw != 0)
2366 setlocale(LC_CTYPE, "");