JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
change -e behaviour and update man page.
[st.git] / st.c
1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
3 #include <ctype.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <locale.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 #include <X11/Xlib.h>
20 #include <X11/Xatom.h>
21 #include <X11/keysym.h>
22 #include <X11/Xutil.h>
23
24 #if   defined(__linux)
25  #include <pty.h>
26 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
27  #include <util.h>
28 #elif defined(__FreeBSD__) || defined(__DragonFly__)
29  #include <libutil.h>
30 #endif
31
32 #define USAGE \
33         "st-" VERSION ", (c) 2010 st engineers\n" \
34         "usage: st [-t title] [-c class] [-e cmd] [-v]\n"
35
36 /* Arbitrary sizes */
37 #define ESC_TITLE_SIZ 256
38 #define ESC_BUF_SIZ   256
39 #define ESC_ARG_SIZ   16
40 #define DRAW_BUF_SIZ  1024
41 #define UTF_SIZ       4
42
43 #define SERRNO strerror(errno)
44 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
45 #define MAX(a, b)  ((a) < (b) ? (b) : (a))
46 #define LEN(a)     (sizeof(a) / sizeof(a[0]))
47 #define DEFAULT(a, b)     (a) = (a) ? (a) : (b)    
48 #define BETWEEN(x, a, b)  ((a) <= (x) && (x) <= (b))
49 #define LIMIT(x, a, b)    (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
50 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
51 #define IS_SET(flag) (term.mode & (flag))
52
53 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
54 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
55 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
56        CURSOR_SAVE, CURSOR_LOAD };
57 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
58 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
59 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8, 
60        MODE_CRLF=16 };
61 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
62 enum { SCREEN_UPDATE, SCREEN_REDRAW };
63 enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
64
65 #undef B0
66 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
67
68 typedef struct {
69         char c[UTF_SIZ];     /* character code */
70         char mode;  /* attribute flags */
71         int fg;     /* foreground      */
72         int bg;     /* background      */
73         char state; /* state flags     */
74 } Glyph;
75
76 typedef Glyph* Line;
77
78 typedef struct {
79         Glyph attr;      /* current char attributes */
80         int x;
81         int y;
82         char state;
83 } TCursor;
84
85 /* CSI Escape sequence structs */
86 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
87 typedef struct {
88         char buf[ESC_BUF_SIZ]; /* raw string */
89         int len;                           /* raw string length */
90         char priv;
91         int arg[ESC_ARG_SIZ];
92         int narg;                          /* nb of args */
93         char mode;
94 } CSIEscape;
95
96 /* Internal representation of the screen */
97 typedef struct {
98         int row;        /* nb row */  
99         int col;        /* nb col */
100         Line* line;     /* screen */
101         Line* alt;      /* alternate screen */
102         TCursor c;      /* cursor */
103         int top;        /* top    scroll limit */
104         int bot;        /* bottom scroll limit */
105         int mode;       /* terminal mode flags */
106         int esc;        /* escape state flags */
107         char title[ESC_TITLE_SIZ];
108         int titlelen;
109 } Term;
110
111 /* Purely graphic info */
112 typedef struct {
113         Display* dpy;
114         Colormap cmap;
115         Window win;
116         Pixmap buf;
117         XIM xim;
118         XIC xic;
119         int scr;
120         int w;  /* window width  */
121         int h;  /* window height */
122         int bufw; /* pixmap width  */
123         int bufh; /* pixmap height */
124         int ch; /* char height */
125         int cw; /* char width  */
126         char state; /* focus, redraw, visible */
127 } XWindow; 
128
129 typedef struct {
130         KeySym k;
131         char s[ESC_BUF_SIZ];
132 } Key;
133
134 /* Drawing Context */
135 typedef struct {
136         unsigned long col[256];
137         GC gc;
138         struct {
139                 int ascent;
140                 int descent;
141                 short lbearing;
142                 short rbearing;
143                 XFontSet set;
144         } font, bfont;
145 } DC;
146
147 /* TODO: use better name for vars... */
148 typedef struct {
149         int mode;
150         int bx, by;
151         int ex, ey;
152         struct {int x, y;}  b, e;
153         char *clip;
154 } Selection;
155
156 #include "config.h"
157
158 static void die(const char *errstr, ...);
159 static void draw(int);
160 static void execsh(void);
161 static void sigchld(int);
162 static void run(void);
163
164 static void csidump(void);
165 static void csihandle(void);
166 static void csiparse(void);
167 static void csireset(void);
168
169 static void tclearregion(int, int, int, int);
170 static void tcursor(int);
171 static void tdeletechar(int);
172 static void tdeleteline(int);
173 static void tinsertblank(int);
174 static void tinsertblankline(int);
175 static void tmoveto(int, int);
176 static void tnew(int, int);
177 static void tnewline(int);
178 static void tputtab(void);
179 static void tputc(char*);
180 static void treset(void);
181 static int tresize(int, int);
182 static void tscrollup(int, int);
183 static void tscrolldown(int, int);
184 static void tsetattr(int*, int);
185 static void tsetchar(char*);
186 static void tsetscroll(int, int);
187 static void tswapscreen(void);
188
189 static void ttynew(void);
190 static void ttyread(void);
191 static void ttyresize(int, int);
192 static void ttywrite(const char *, size_t);
193
194 static void xdraws(char *, Glyph, int, int, int, int);
195 static void xhints(void);
196 static void xclear(int, int, int, int);
197 static void xdrawcursor(void);
198 static void xinit(void);
199 static void xloadcols(void);
200 static void xseturgency(int);
201 static void xsetsel(char*);
202 static void xresize(int, int);
203
204 static void expose(XEvent *);
205 static void visibility(XEvent *);
206 static void unmap(XEvent *);
207 static char* kmap(KeySym);
208 static void kpress(XEvent *);
209 static void resize(XEvent *);
210 static void focus(XEvent *);
211 static void brelease(XEvent *);
212 static void bpress(XEvent *);
213 static void bmotion(XEvent *);
214 static void selnotify(XEvent *);
215 static void selrequest(XEvent *);
216
217 static void selinit(void);
218 static inline int selected(int, int);
219 static void selcopy(void);
220 static void selpaste(void);
221
222 static int utf8decode(char *, long *);
223 static int utf8encode(long *, char *);
224 static int utf8size(char *);
225 static int isfullutf8(char *, int);
226
227 static void (*handler[LASTEvent])(XEvent *) = {
228         [KeyPress] = kpress,
229         [ConfigureNotify] = resize,
230         [VisibilityNotify] = visibility,
231         [UnmapNotify] = unmap,
232         [Expose] = expose,
233         [FocusIn] = focus,
234         [FocusOut] = focus,
235         [MotionNotify] = bmotion,
236         [ButtonPress] = bpress,
237         [ButtonRelease] = brelease,
238         [SelectionNotify] = selnotify,
239         [SelectionRequest] = selrequest,
240 };
241
242 /* Globals */
243 static DC dc;
244 static XWindow xw;
245 static Term term;
246 static CSIEscape escseq;
247 static int cmdfd;
248 static pid_t pid;
249 static Selection sel;
250 static char **opt_cmd  = NULL;
251 static char *opt_title = NULL;
252 static char *opt_class = NULL;
253
254 int
255 utf8decode(char *s, long *u) {
256         unsigned char c;
257         int i, n, rtn;
258
259         rtn = 1;
260         c = *s;
261         if(~c&B7) { /* 0xxxxxxx */
262                 *u = c;
263                 return rtn;
264         } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
265                 *u = c&(B4|B3|B2|B1|B0);
266                 n = 1;
267         } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
268                 *u = c&(B3|B2|B1|B0);
269                 n = 2;
270         } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
271                 *u = c&(B2|B1|B0);
272                 n = 3;
273         } else
274                 goto invalid;
275         for(i=n,++s; i>0; --i,++rtn,++s) {
276                 c = *s;
277                 if((c&(B7|B6)) != B7) /* 10xxxxxx */
278                         goto invalid;
279                 *u <<= 6;
280                 *u |= c&(B5|B4|B3|B2|B1|B0);
281         }
282         if((n == 1 && *u < 0x80) ||
283            (n == 2 && *u < 0x800) ||
284            (n == 3 && *u < 0x10000) ||
285            (*u >= 0xD800 && *u <= 0xDFFF))
286                 goto invalid;
287         return rtn;
288 invalid:
289         *u = 0xFFFD;
290         return rtn;
291 }
292
293 int
294 utf8encode(long *u, char *s) {
295         unsigned char *sp;
296         unsigned long uc;
297         int i, n;
298
299         sp = (unsigned char*) s;
300         uc = *u;
301         if(uc < 0x80) {
302                 *sp = uc; /* 0xxxxxxx */
303                 return 1;
304         } else if(*u < 0x800) {
305                 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
306                 n = 1;
307         } else if(uc < 0x10000) {
308                 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
309                 n = 2;
310         } else if(uc <= 0x10FFFF) {
311                 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
312                 n = 3;
313         } else {
314                 goto invalid;
315         }
316         for(i=n,++sp; i>0; --i,++sp)
317                 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
318         return n+1;
319 invalid:
320         /* U+FFFD */
321         *s++ = '\xEF';
322         *s++ = '\xBF';
323         *s = '\xBD';
324         return 3;
325 }
326
327 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
328    UTF-8 otherwise return 0 */
329 int
330 isfullutf8(char *s, int b) {
331         unsigned char *c1, *c2, *c3;
332
333         c1 = (unsigned char *) s;
334         c2 = (unsigned char *) ++s;
335         c3 = (unsigned char *) ++s;
336         if(b < 1)
337                 return 0;
338         else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
339                 return 0;
340         else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
341             ((b == 1) || 
342             ((b == 2) && (*c2&(B7|B6)) == B7)))
343                 return 0;
344         else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
345             ((b == 1) ||
346             ((b == 2) && (*c2&(B7|B6)) == B7) ||
347             ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
348                 return 0;
349         else
350                 return 1;
351 }
352
353 int
354 utf8size(char *s) {
355         unsigned char c = *s;
356
357         if (~c&B7)
358                 return 1;
359         else if ((c&(B7|B6|B5)) == (B7|B6))
360                 return 2;
361         else if ((c&(B7|B6|B5|B4)) == (B7|B6|B5))
362                 return 3;
363         else 
364                 return 4;
365 }
366
367 void
368 selinit(void) {
369         sel.mode = 0;
370         sel.bx = -1;
371         sel.clip = NULL;
372 }
373
374 static inline int 
375 selected(int x, int y) {
376         if(sel.ey == y && sel.by == y) {
377                 int bx = MIN(sel.bx, sel.ex);
378                 int ex = MAX(sel.bx, sel.ex);
379                 return BETWEEN(x, bx, ex);
380         }
381         return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x)) 
382                 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
383 }
384
385 void
386 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
387         if(b) 
388                 *b = e->xbutton.button;
389
390         *x = e->xbutton.x/xw.cw;
391         *y = e->xbutton.y/xw.ch;
392         sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
393         sel.b.y = MIN(sel.by, sel.ey);
394         sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
395         sel.e.y = MAX(sel.by, sel.ey);
396 }
397
398 void
399 bpress(XEvent *e) {
400         sel.mode = 1;
401         sel.ex = sel.bx = e->xbutton.x/xw.cw;
402         sel.ey = sel.by = e->xbutton.y/xw.ch;
403 }
404
405 void
406 selcopy(void) {
407         char *str, *ptr;
408         int x, y, sz, sl, ls = 0;
409
410         if(sel.bx == -1)
411                 str = NULL;
412         else {
413                 sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
414                 ptr = str = malloc(sz);
415                 for(y = 0; y < term.row; y++) {
416                         for(x = 0; x < term.col; x++)
417                                 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
418                                         sl = utf8size(term.line[y][x].c);
419                                         memcpy(ptr, term.line[y][x].c, sl);
420                                         ptr += sl;
421                                 }
422                         if(ls)
423                                 *ptr = '\n', ptr++;
424                 }
425                 *ptr = 0;
426         }
427         xsetsel(str);
428 }
429
430 void
431 selnotify(XEvent *e) {
432         unsigned long nitems;
433         unsigned long ofs, rem;
434         int format;
435         unsigned char *data;
436         Atom type;
437
438         ofs = 0;
439         do {
440                 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
441                                         False, AnyPropertyType, &type, &format,
442                                         &nitems, &rem, &data)) {
443                         fprintf(stderr, "Clipboard allocation failed\n");
444                         return;
445                 }
446                 ttywrite((const char *) data, nitems * format / 8);
447                 XFree(data);
448                 /* number of 32-bit chunks returned */
449                 ofs += nitems * format / 32;
450         } while(rem > 0);
451 }
452
453 void
454 selpaste() {
455         XConvertSelection(xw.dpy, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime);
456 }
457
458 void
459 selrequest(XEvent *e) {
460         XSelectionRequestEvent *xsre;
461         XSelectionEvent xev;
462         Atom xa_targets;
463
464         xsre = (XSelectionRequestEvent *) e;
465         xev.type = SelectionNotify;
466         xev.requestor = xsre->requestor;
467         xev.selection = xsre->selection;
468         xev.target = xsre->target;
469         xev.time = xsre->time;
470         /* reject */
471         xev.property = None;
472
473         xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
474         if(xsre->target == xa_targets) {
475                 /* respond with the supported type */
476                 Atom string = XA_STRING;
477                 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
478                                 XA_ATOM, 32, PropModeReplace,
479                                 (unsigned char *) &string, 1);
480                 xev.property = xsre->property;
481         } else if(xsre->target == XA_STRING) {
482                 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
483                                 xsre->target, 8, PropModeReplace,
484                                 (unsigned char *) sel.clip, strlen(sel.clip));
485                 xev.property = xsre->property;
486         }
487
488         /* all done, send a notification to the listener */
489         if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
490                 fprintf(stderr, "Error sending SelectionNotify event\n");
491 }
492
493 void
494 xsetsel(char *str) {
495         /* register the selection for both the clipboard and the primary */
496         Atom clipboard;
497
498         free(sel.clip);
499         sel.clip = str;
500
501         XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
502
503         clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
504         XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
505
506         XFlush(xw.dpy);
507 }
508
509 /* TODO: doubleclick to select word */
510 void
511 brelease(XEvent *e) {
512         int b;
513         sel.mode = 0;
514         getbuttoninfo(e, &b, &sel.ex, &sel.ey);
515         if(sel.bx==sel.ex && sel.by==sel.ey) {
516                 sel.bx = -1;
517                 if(b==2)
518                         selpaste();
519         } else {
520                 if(b==1)
521                         selcopy();
522         }
523         draw(1);
524 }
525
526 void
527 bmotion(XEvent *e) {
528         if (sel.mode) {
529                 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
530                 /* XXX: draw() can't keep up, disabled for now.
531                    selection is visible on button release.
532                    draw(1); */
533         }
534 }
535
536 void
537 die(const char *errstr, ...) {
538         va_list ap;
539
540         va_start(ap, errstr);
541         vfprintf(stderr, errstr, ap);
542         va_end(ap);
543         exit(EXIT_FAILURE);
544 }
545
546 void
547 execsh(void) {
548         char **args;
549         char *envshell = getenv("SHELL");
550
551         DEFAULT(envshell, "sh");
552         putenv("TERM="TNAME);
553         args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
554         execvp(args[0], args);
555         exit(EXIT_FAILURE);
556 }
557
558 void 
559 sigchld(int a) {
560         int stat = 0;
561         if(waitpid(pid, &stat, 0) < 0)
562                 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
563         if(WIFEXITED(stat))
564                 exit(WEXITSTATUS(stat));
565         else
566                 exit(EXIT_FAILURE);
567 }
568
569 void
570 ttynew(void) {
571         int m, s;
572         
573         /* seems to work fine on linux, openbsd and freebsd */
574         struct winsize w = {term.row, term.col, 0, 0};
575         if(openpty(&m, &s, NULL, NULL, &w) < 0)
576                 die("openpty failed: %s\n", SERRNO);
577
578         switch(pid = fork()) {
579         case -1:
580                 die("fork failed\n");
581                 break;
582         case 0:
583                 setsid(); /* create a new process group */
584                 dup2(s, STDIN_FILENO);
585                 dup2(s, STDOUT_FILENO);
586                 dup2(s, STDERR_FILENO);
587                 if(ioctl(s, TIOCSCTTY, NULL) < 0)
588                         die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
589                 close(s);
590                 close(m);
591                 execsh();
592                 break;
593         default:
594                 close(s);
595                 cmdfd = m;
596                 signal(SIGCHLD, sigchld);
597         }
598 }
599
600 void
601 dump(char c) {
602         static int col;
603         fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
604         if(++col % 10 == 0)
605                 fprintf(stderr, "\n");
606 }
607
608 void
609 ttyread(void) {
610         static char buf[BUFSIZ];
611         static int buflen = 0; 
612         char *ptr;
613         char s[UTF_SIZ];
614         int charsize; /* size of utf8 char in bytes */
615         long utf8c;
616         int ret;
617
618         /* append read bytes to unprocessed bytes */
619         if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
620                 die("Couldn't read from shell: %s\n", SERRNO);
621
622         /* process every complete utf8 char */
623         buflen += ret;
624         ptr = buf;
625         while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
626                 charsize = utf8decode(ptr, &utf8c);
627                 utf8encode(&utf8c, s);
628                 tputc(s);
629                 ptr    += charsize;
630                 buflen -= charsize;
631         }
632
633         /* keep any uncomplete utf8 char for the next call */
634         memmove(buf, ptr, buflen);
635 }
636
637 void
638 ttywrite(const char *s, size_t n) {
639         if(write(cmdfd, s, n) == -1)
640                 die("write error on tty: %s\n", SERRNO);
641 }
642
643 void
644 ttyresize(int x, int y) {
645         struct winsize w;
646
647         w.ws_row = term.row;
648         w.ws_col = term.col;
649         w.ws_xpixel = w.ws_ypixel = 0;
650         if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
651                 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
652 }
653
654 void
655 tcursor(int mode) {
656         static TCursor c;
657
658         if(mode == CURSOR_SAVE)
659                 c = term.c;
660         else if(mode == CURSOR_LOAD)
661                 term.c = c, tmoveto(c.x, c.y);
662 }
663
664 void
665 treset(void) {
666         term.c = (TCursor){{
667                 .mode = ATTR_NULL, 
668                 .fg = DefaultFG, 
669                 .bg = DefaultBG
670         }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
671         
672         term.top = 0, term.bot = term.row - 1;
673         term.mode = MODE_WRAP;
674         tclearregion(0, 0, term.col-1, term.row-1);
675 }
676
677 void
678 tnew(int col, int row) {
679         /* set screen size */
680         term.row = row, term.col = col;
681         term.line = malloc(term.row * sizeof(Line));
682         term.alt  = malloc(term.row * sizeof(Line));
683         for(row = 0 ; row < term.row; row++) {
684                 term.line[row] = malloc(term.col * sizeof(Glyph));
685                 term.alt [row] = malloc(term.col * sizeof(Glyph));
686         }
687         /* setup screen */
688         treset();
689 }
690
691 void
692 tswapscreen(void) {
693         Line* tmp = term.line;
694         term.line = term.alt;
695         term.alt = tmp;
696         term.mode ^= MODE_ALTSCREEN;
697 }
698
699 void
700 tscrolldown(int orig, int n) {
701         int i;
702         Line temp;
703         
704         LIMIT(n, 0, term.bot-orig+1);
705
706         tclearregion(0, term.bot-n+1, term.col-1, term.bot);
707         
708         for(i = term.bot; i >= orig+n; i--) {
709                 temp = term.line[i];
710                 term.line[i] = term.line[i-n];
711                 term.line[i-n] = temp;
712         }
713 }
714
715 void
716 tscrollup(int orig, int n) {
717         int i;
718         Line temp;
719         LIMIT(n, 0, term.bot-orig+1);
720         
721         tclearregion(0, orig, term.col-1, orig+n-1);
722         
723         for(i = orig; i <= term.bot-n; i++) { 
724                  temp = term.line[i];
725                  term.line[i] = term.line[i+n]; 
726                  term.line[i+n] = temp;
727         }
728 }
729
730 void
731 tnewline(int first_col) {
732         int y = term.c.y;
733         if(y == term.bot)
734                 tscrollup(term.top, 1);
735         else
736                 y++;
737         tmoveto(first_col ? 0 : term.c.x, y);
738 }
739
740 void
741 csiparse(void) {
742         /* int noarg = 1; */
743         char *p = escseq.buf;
744
745         escseq.narg = 0;
746         if(*p == '?')
747                 escseq.priv = 1, p++;
748         
749         while(p < escseq.buf+escseq.len) {
750                 while(isdigit(*p)) {
751                         escseq.arg[escseq.narg] *= 10;
752                         escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
753                 }
754                 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
755                         escseq.narg++, p++;
756                 else {
757                         escseq.mode = *p;
758                         escseq.narg++;
759                         return;
760                 }
761         }
762 }
763
764 void
765 tmoveto(int x, int y) {
766         LIMIT(x, 0, term.col-1);
767         LIMIT(y, 0, term.row-1);
768         term.c.state &= ~CURSOR_WRAPNEXT;
769         term.c.x = x;
770         term.c.y = y;
771 }
772
773 void
774 tsetchar(char *c) {
775         term.line[term.c.y][term.c.x] = term.c.attr;
776         memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
777         term.line[term.c.y][term.c.x].state |= GLYPH_SET;
778 }
779
780 void
781 tclearregion(int x1, int y1, int x2, int y2) {
782         int x, y, temp;
783
784         if(x1 > x2)
785                 temp = x1, x1 = x2, x2 = temp;
786         if(y1 > y2)
787                 temp = y1, y1 = y2, y2 = temp;
788
789         LIMIT(x1, 0, term.col-1);
790         LIMIT(x2, 0, term.col-1);
791         LIMIT(y1, 0, term.row-1);
792         LIMIT(y2, 0, term.row-1);
793
794         for(y = y1; y <= y2; y++)
795                 for(x = x1; x <= x2; x++)
796                         term.line[y][x].state = 0;
797 }
798
799 void
800 tdeletechar(int n) {
801         int src = term.c.x + n;
802         int dst = term.c.x;
803         int size = term.col - src;
804
805         if(src >= term.col) {
806                 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
807                 return;
808         }
809         memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
810         tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
811 }
812
813 void
814 tinsertblank(int n) {
815         int src = term.c.x;
816         int dst = src + n;
817         int size = term.col - dst;
818
819         if(dst >= term.col) {
820                 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
821                 return;
822         }
823         memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
824         tclearregion(src, term.c.y, dst - 1, term.c.y);
825 }
826
827 void
828 tinsertblankline(int n) {
829         if(term.c.y < term.top || term.c.y > term.bot)
830                 return;
831
832         tscrolldown(term.c.y, n);
833 }
834
835 void
836 tdeleteline(int n) {
837         if(term.c.y < term.top || term.c.y > term.bot)
838                 return;
839
840         tscrollup(term.c.y, n);
841 }
842
843 void
844 tsetattr(int *attr, int l) {
845         int i;
846
847         for(i = 0; i < l; i++) {
848                 switch(attr[i]) {
849                 case 0:
850                         term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
851                         term.c.attr.fg = DefaultFG;
852                         term.c.attr.bg = DefaultBG;
853                         break;
854                 case 1:
855                         term.c.attr.mode |= ATTR_BOLD;   
856                         break;
857                 case 4: 
858                         term.c.attr.mode |= ATTR_UNDERLINE;
859                         break;
860                 case 7: 
861                         term.c.attr.mode |= ATTR_REVERSE;       
862                         break;
863                 case 22: 
864                         term.c.attr.mode &= ~ATTR_BOLD;  
865                         break;
866                 case 24: 
867                         term.c.attr.mode &= ~ATTR_UNDERLINE;
868                         break;
869                 case 27: 
870                         term.c.attr.mode &= ~ATTR_REVERSE;       
871                         break;
872                 case 38:
873                         if (i + 2 < l && attr[i + 1] == 5) {
874                                 i += 2;
875                                 if (BETWEEN(attr[i], 0, 255))
876                                         term.c.attr.fg = attr[i];
877                                 else
878                                         fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
879                         }
880                         else
881                                 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]); 
882                         break;
883                 case 39:
884                         term.c.attr.fg = DefaultFG;
885                         break;
886                 case 48:
887                         if (i + 2 < l && attr[i + 1] == 5) {
888                                 i += 2;
889                                 if (BETWEEN(attr[i], 0, 255))
890                                         term.c.attr.bg = attr[i];
891                                 else
892                                         fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
893                         }
894                         else
895                                 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]); 
896                         break;
897                 case 49:
898                         term.c.attr.bg = DefaultBG;
899                         break;
900                 default:
901                         if(BETWEEN(attr[i], 30, 37))
902                                 term.c.attr.fg = attr[i] - 30;
903                         else if(BETWEEN(attr[i], 40, 47))
904                                 term.c.attr.bg = attr[i] - 40;
905                         else if(BETWEEN(attr[i], 90, 97))
906                                 term.c.attr.fg = attr[i] - 90 + 8;
907                         else if(BETWEEN(attr[i], 100, 107))
908                                 term.c.attr.fg = attr[i] - 100 + 8;
909                         else 
910                                 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
911                         
912                         break;
913                 }
914         }
915 }
916
917 void
918 tsetscroll(int t, int b) {
919         int temp;
920
921         LIMIT(t, 0, term.row-1);
922         LIMIT(b, 0, term.row-1);
923         if(t > b) {
924                 temp = t;
925                 t = b;
926                 b = temp;
927         }
928         term.top = t;
929         term.bot = b;    
930 }
931
932 void
933 csihandle(void) {
934         switch(escseq.mode) {
935         default:
936         unknown:
937                 printf("erresc: unknown csi ");
938                 csidump();
939                 /* die(""); */
940                 break;
941         case '@': /* ICH -- Insert <n> blank char */
942                 DEFAULT(escseq.arg[0], 1);
943                 tinsertblank(escseq.arg[0]);
944                 break;
945         case 'A': /* CUU -- Cursor <n> Up */
946         case 'e':
947                 DEFAULT(escseq.arg[0], 1);
948                 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
949                 break;
950         case 'B': /* CUD -- Cursor <n> Down */
951                 DEFAULT(escseq.arg[0], 1);
952                 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
953                 break;
954         case 'C': /* CUF -- Cursor <n> Forward */
955         case 'a':
956                 DEFAULT(escseq.arg[0], 1);
957                 tmoveto(term.c.x+escseq.arg[0], term.c.y);
958                 break;
959         case 'D': /* CUB -- Cursor <n> Backward */
960                 DEFAULT(escseq.arg[0], 1);
961                 tmoveto(term.c.x-escseq.arg[0], term.c.y);
962                 break;
963         case 'E': /* CNL -- Cursor <n> Down and first col */
964                 DEFAULT(escseq.arg[0], 1);
965                 tmoveto(0, term.c.y+escseq.arg[0]);
966                 break;
967         case 'F': /* CPL -- Cursor <n> Up and first col */
968                 DEFAULT(escseq.arg[0], 1);
969                 tmoveto(0, term.c.y-escseq.arg[0]);
970                 break;
971         case 'G': /* CHA -- Move to <col> */
972         case '`': /* XXX: HPA -- same? */
973                 DEFAULT(escseq.arg[0], 1);
974                 tmoveto(escseq.arg[0]-1, term.c.y);
975                 break;
976         case 'H': /* CUP -- Move to <row> <col> */
977         case 'f': /* XXX: HVP -- same? */
978                 DEFAULT(escseq.arg[0], 1);
979                 DEFAULT(escseq.arg[1], 1);
980                 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
981                 break;
982         /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
983         case 'J': /* ED -- Clear screen */
984                 switch(escseq.arg[0]) {
985                 case 0: /* below */
986                         tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
987                         if(term.c.y < term.row-1)
988                                 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
989                         break;
990                 case 1: /* above */
991                         if(term.c.y > 1)
992                                 tclearregion(0, 0, term.col-1, term.c.y-1);
993                         tclearregion(0, term.c.y, term.c.x, term.c.y);
994                         break;
995                 case 2: /* all */
996                         tclearregion(0, 0, term.col-1, term.row-1);
997                         break;
998                 default:
999                         goto unknown;
1000                 }
1001                 break;
1002         case 'K': /* EL -- Clear line */
1003                 switch(escseq.arg[0]) {
1004                 case 0: /* right */
1005                         tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1006                         break;
1007                 case 1: /* left */
1008                         tclearregion(0, term.c.y, term.c.x, term.c.y);
1009                         break;
1010                 case 2: /* all */
1011                         tclearregion(0, term.c.y, term.col-1, term.c.y);
1012                         break;
1013                 }
1014                 break;
1015         case 'S': /* SU -- Scroll <n> line up */
1016                 DEFAULT(escseq.arg[0], 1);
1017                 tscrollup(term.top, escseq.arg[0]);
1018                 break;
1019         case 'T': /* SD -- Scroll <n> line down */
1020                 DEFAULT(escseq.arg[0], 1);
1021                 tscrolldown(term.top, escseq.arg[0]);
1022                 break;
1023         case 'L': /* IL -- Insert <n> blank lines */
1024                 DEFAULT(escseq.arg[0], 1);
1025                 tinsertblankline(escseq.arg[0]);
1026                 break;
1027         case 'l': /* RM -- Reset Mode */
1028                 if(escseq.priv) {
1029                         switch(escseq.arg[0]) {
1030                         case 1:
1031                                 term.mode &= ~MODE_APPKEYPAD;
1032                                 break;
1033                         case 5: /* TODO: DECSCNM -- Remove reverse video */
1034                                 break;
1035                         case 7:
1036                                 term.mode &= ~MODE_WRAP;
1037                                 break;
1038                         case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1039                                 break;
1040                         case 20:
1041                                 term.mode &= ~MODE_CRLF;
1042                                 break;
1043                         case 25:
1044                                 term.c.state |= CURSOR_HIDE;
1045                                 break;
1046                         case 1049: /* = 1047 and 1048 */
1047                         case 1047:
1048                                 if(IS_SET(MODE_ALTSCREEN)) {
1049                                         tclearregion(0, 0, term.col-1, term.row-1);
1050                                         tswapscreen();
1051                                 }
1052                                 if(escseq.arg[0] == 1047)
1053                                         break;
1054                         case 1048:
1055                                 tcursor(CURSOR_LOAD);
1056                                 break;
1057                         default:
1058                                 goto unknown;
1059                         }
1060                 } else {
1061                         switch(escseq.arg[0]) {
1062                         case 4:
1063                                 term.mode &= ~MODE_INSERT;
1064                                 break;
1065                         default:
1066                                 goto unknown;
1067                         }
1068                 }
1069                 break;
1070         case 'M': /* DL -- Delete <n> lines */
1071                 DEFAULT(escseq.arg[0], 1);
1072                 tdeleteline(escseq.arg[0]);
1073                 break;
1074         case 'X': /* ECH -- Erase <n> char */
1075                 DEFAULT(escseq.arg[0], 1);
1076                 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
1077                 break;
1078         case 'P': /* DCH -- Delete <n> char */
1079                 DEFAULT(escseq.arg[0], 1);
1080                 tdeletechar(escseq.arg[0]);
1081                 break;
1082         /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1083         case 'd': /* VPA -- Move to <row> */
1084                 DEFAULT(escseq.arg[0], 1);
1085                 tmoveto(term.c.x, escseq.arg[0]-1);
1086                 break;
1087         case 'h': /* SM -- Set terminal mode */
1088                 if(escseq.priv) {
1089                         switch(escseq.arg[0]) {
1090                         case 1:
1091                                 term.mode |= MODE_APPKEYPAD;
1092                                 break;
1093                         case 5: /* DECSCNM -- Reverve video */
1094                                 /* TODO: set REVERSE on the whole screen (f) */
1095                                 break;
1096                         case 7:
1097                                 term.mode |= MODE_WRAP;
1098                                 break;
1099                         case 20:
1100                                 term.mode |= MODE_CRLF;
1101                                 break;
1102                         case 12: /* att610 -- Start blinking cursor (IGNORED) */
1103                                  /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1104                                 if(escseq.narg > 1 && escseq.arg[1] != 25)
1105                                         break;
1106                         case 25:
1107                                 term.c.state &= ~CURSOR_HIDE;
1108                                 break;
1109                         case 1049: /* = 1047 and 1048 */
1110                         case 1047:
1111                                 if(IS_SET(MODE_ALTSCREEN))
1112                                         tclearregion(0, 0, term.col-1, term.row-1);
1113                                 else
1114                                         tswapscreen();
1115                                 if(escseq.arg[0] == 1047)
1116                                         break;
1117                         case 1048:
1118                                 tcursor(CURSOR_SAVE);
1119                                 break;
1120                         default: goto unknown;
1121                         }
1122                 } else {
1123                         switch(escseq.arg[0]) {
1124                         case 4:
1125                                 term.mode |= MODE_INSERT;
1126                                 break;
1127                         default: goto unknown;
1128                         }
1129                 };
1130                 break;
1131         case 'm': /* SGR -- Terminal attribute (color) */
1132                 tsetattr(escseq.arg, escseq.narg);
1133                 break;
1134         case 'r': /* DECSTBM -- Set Scrolling Region */
1135                 if(escseq.priv)
1136                         goto unknown;
1137                 else {
1138                         DEFAULT(escseq.arg[0], 1);
1139                         DEFAULT(escseq.arg[1], term.row);
1140                         tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
1141                         tmoveto(0, 0);
1142                 }
1143                 break;
1144         case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1145                 tcursor(CURSOR_SAVE);
1146                 break;
1147         case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1148                 tcursor(CURSOR_LOAD);
1149                 break;
1150         }
1151 }
1152
1153 void
1154 csidump(void) { 
1155         int i;
1156         printf("ESC [ %s", escseq.priv ? "? " : "");
1157         if(escseq.narg)
1158                 for(i = 0; i < escseq.narg; i++)
1159                         printf("%d ", escseq.arg[i]);
1160         if(escseq.mode)
1161                 putchar(escseq.mode);
1162         putchar('\n');
1163 }
1164
1165 void
1166 csireset(void) {
1167         memset(&escseq, 0, sizeof(escseq));
1168 }
1169
1170 void
1171 tputtab(void) {
1172         int space = TAB - term.c.x % TAB;
1173         tmoveto(term.c.x + space, term.c.y);
1174 }
1175
1176 void
1177 tputc(char *c) {
1178         char ascii = *c;
1179         if(term.esc & ESC_START) {
1180                 if(term.esc & ESC_CSI) {
1181                         escseq.buf[escseq.len++] = ascii;
1182                         if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
1183                                 term.esc = 0;
1184                                 csiparse(), csihandle();
1185                         }
1186                         /* TODO: handle other OSC */
1187                 } else if(term.esc & ESC_OSC) { 
1188                         if(ascii == ';') {
1189                                 term.titlelen = 0;
1190                                 term.esc = ESC_START | ESC_TITLE;
1191                         }
1192                 } else if(term.esc & ESC_TITLE) {
1193                         if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
1194                                 term.esc = 0;
1195                                 term.title[term.titlelen] = '\0';
1196                                 XStoreName(xw.dpy, xw.win, term.title);
1197                         } else {
1198                                 term.title[term.titlelen++] = ascii;
1199                         }
1200                 } else if(term.esc & ESC_ALTCHARSET) {
1201                         switch(ascii) {
1202                         case '0': /* Line drawing crap */
1203                                 term.c.attr.mode |= ATTR_GFX;
1204                                 break;
1205                         case 'B': /* Back to regular text */
1206                                 term.c.attr.mode &= ~ATTR_GFX;
1207                                 break;
1208                         default:
1209                                 printf("esc unhandled charset: ESC ( %c\n", ascii);
1210                         }
1211                         term.esc = 0;
1212                 } else {
1213                         switch(ascii) {
1214                         case '[':
1215                                 term.esc |= ESC_CSI;
1216                                 break;
1217                         case ']':
1218                                 term.esc |= ESC_OSC;
1219                                 break;
1220                         case '(':
1221                                 term.esc |= ESC_ALTCHARSET;
1222                                 break;
1223                         case 'D': /* IND -- Linefeed */
1224                                 if(term.c.y == term.bot)
1225                                         tscrollup(term.top, 1);
1226                                 else
1227                                         tmoveto(term.c.x, term.c.y+1);
1228                                 term.esc = 0;
1229                                 break;
1230                         case 'E': /* NEL -- Next line */
1231                                 tnewline(1); /* always go to first col */
1232                                 term.esc = 0;
1233                                 break;
1234                         case 'M': /* RI -- Reverse index */
1235                                 if(term.c.y == term.top)
1236                                         tscrolldown(term.top, 1);
1237                                 else
1238                                         tmoveto(term.c.x, term.c.y-1);
1239                                 term.esc = 0;
1240                                 break;
1241                         case 'c': /* RIS -- Reset to inital state */
1242                                 treset();
1243                                 term.esc = 0;
1244                                 break;
1245                         case '=': /* DECPAM -- Application keypad */
1246                                 term.mode |= MODE_APPKEYPAD;
1247                                 term.esc = 0;
1248                                 break;
1249                         case '>': /* DECPNM -- Normal keypad */
1250                                 term.mode &= ~MODE_APPKEYPAD;
1251                                 term.esc = 0;
1252                                 break;
1253                         case '7': /* DECSC -- Save Cursor */
1254                                 tcursor(CURSOR_SAVE);
1255                                 term.esc = 0;
1256                                 break;
1257                         case '8': /* DECRC -- Restore Cursor */
1258                                 tcursor(CURSOR_LOAD);
1259                                 term.esc = 0;
1260                                 break;
1261                         default:
1262                                 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1263                                     (unsigned char) ascii, isprint(ascii)?ascii:'.');
1264                                 term.esc = 0;
1265                         }
1266                 }
1267         } else {
1268                 switch(ascii) {
1269                 case '\t':
1270                         tputtab();
1271                         break;
1272                 case '\b':
1273                         tmoveto(term.c.x-1, term.c.y);
1274                         break;
1275                 case '\r':
1276                         tmoveto(0, term.c.y);
1277                         break;
1278                 case '\f':
1279                 case '\v':
1280                 case '\n':
1281                         /* go to first col if the mode is set */
1282                         tnewline(IS_SET(MODE_CRLF));
1283                         break;
1284                 case '\a':
1285                         if(!(xw.state & WIN_FOCUSED))
1286                                 xseturgency(1);
1287                         break;
1288                 case '\033':
1289                         csireset();
1290                         term.esc = ESC_START;
1291                         break;
1292                 default:
1293                         if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1294                                 tnewline(1); /* always go to first col */
1295                         tsetchar(c);
1296                         if(term.c.x+1 < term.col)
1297                                 tmoveto(term.c.x+1, term.c.y);
1298                         else
1299                                 term.c.state |= CURSOR_WRAPNEXT;
1300                         break;
1301                 }
1302         }
1303 }
1304
1305 int
1306 tresize(int col, int row) {
1307         int i, x;
1308         int minrow = MIN(row, term.row);
1309         int mincol = MIN(col, term.col);
1310         int slide = term.c.y - row + 1;
1311
1312         if(col < 1 || row < 1)
1313                 return 0;
1314
1315         /* free unneeded rows */
1316         i = 0;
1317         if(slide > 0) {
1318                 /* slide screen to keep cursor where we expect it -
1319                  * tscrollup would work here, but we can optimize to
1320                  * memmove because we're freeing the earlier lines */
1321                 for(/* i = 0 */; i < slide; i++) {
1322                         free(term.line[i]);
1323                         free(term.alt[i]);
1324                 }
1325                 memmove(term.line, term.line + slide, row * sizeof(Line));
1326                 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1327         }
1328         for(i += row; i < term.row; i++) {
1329                 free(term.line[i]);
1330                 free(term.alt[i]);
1331         }
1332
1333         /* resize to new height */
1334         term.line = realloc(term.line, row * sizeof(Line));
1335         term.alt  = realloc(term.alt,  row * sizeof(Line));
1336
1337         /* resize each row to new width, zero-pad if needed */
1338         for(i = 0; i < minrow; i++) {
1339                 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1340                 term.alt[i]  = realloc(term.alt[i],  col * sizeof(Glyph));
1341                 for(x = mincol; x < col; x++) {
1342                         term.line[i][x].state = 0;
1343                         term.alt[i][x].state = 0;
1344                 }
1345         }
1346
1347         /* allocate any new rows */
1348         for(/* i == minrow */; i < row; i++) {
1349                 term.line[i] = calloc(col, sizeof(Glyph));
1350                 term.alt [i] = calloc(col, sizeof(Glyph));
1351         }
1352         
1353         /* update terminal size */
1354         term.col = col, term.row = row;
1355         /* make use of the LIMIT in tmoveto */
1356         tmoveto(term.c.x, term.c.y);
1357         /* reset scrolling region */
1358         tsetscroll(0, row-1);
1359         return (slide > 0);
1360 }
1361
1362 void
1363 xresize(int col, int row) {
1364         Pixmap newbuf;
1365         int oldw, oldh;
1366
1367         oldw = xw.bufw;
1368         oldh = xw.bufh;
1369         xw.bufw = MAX(1, col * xw.cw);
1370         xw.bufh = MAX(1, row * xw.ch);
1371         newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1372         XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
1373         XFreePixmap(xw.dpy, xw.buf);
1374         XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
1375         if(xw.bufw > oldw)
1376                 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
1377                                 xw.bufw-oldw, MIN(xw.bufh, oldh));
1378         else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
1379                 XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
1380                                 xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
1381                                 False);
1382         if(xw.bufh > oldh)
1383                 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
1384                                 xw.bufw, xw.bufh-oldh);
1385         else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
1386                 XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
1387                                 xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
1388                                 False);
1389         xw.buf = newbuf;
1390 }
1391
1392 void
1393 xloadcols(void) {
1394         int i, r, g, b;
1395         XColor color;
1396         unsigned long white = WhitePixel(xw.dpy, xw.scr);
1397
1398         for(i = 0; i < 16; i++) {
1399                 if (!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
1400                         dc.col[i] = white;
1401                         fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1402                 } else
1403                         dc.col[i] = color.pixel;
1404         }
1405
1406         /* same colors as xterm */
1407         for(r = 0; r < 6; r++)
1408                 for(g = 0; g < 6; g++)
1409                         for(b = 0; b < 6; b++) {
1410                                 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1411                                 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1412                                 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1413                                 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1414                                         dc.col[i] = white;
1415                                         fprintf(stderr, "Could not allocate color %d\n", i);
1416                                 } else
1417                                         dc.col[i] = color.pixel;
1418                                 i++;
1419                         }
1420
1421         for(r = 0; r < 24; r++, i++) {
1422                 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1423                 if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
1424                         dc.col[i] = white;
1425                         fprintf(stderr, "Could not allocate color %d\n", i);
1426                 } else
1427                         dc.col[i] = color.pixel;
1428         }
1429 }
1430
1431 void
1432 xclear(int x1, int y1, int x2, int y2) {
1433         XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
1434         XFillRectangle(xw.dpy, xw.buf, dc.gc,
1435                        x1 * xw.cw, y1 * xw.ch,
1436                        (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1437 }
1438
1439 void
1440 xhints(void)
1441 {
1442         XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
1443         XWMHints wm = {.flags = InputHint, .input = 1};
1444         XSizeHints size = {
1445                 .flags = PSize | PResizeInc | PBaseSize,
1446                 .height = xw.h,
1447                 .width = xw.w,
1448                 .height_inc = xw.ch,
1449                 .width_inc = xw.cw,
1450                 .base_height = 2*BORDER,
1451                 .base_width = 2*BORDER,
1452         };
1453         XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1454 }
1455
1456 XFontSet
1457 xinitfont(char *fontstr)
1458 {
1459         XFontSet set;
1460         char *def, **missing;
1461         int n;
1462
1463         missing = NULL;
1464         set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
1465         if(missing) {
1466                 while(n--)
1467                         fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
1468                 XFreeStringList(missing);
1469         }
1470         return set;
1471 }
1472
1473 void
1474 xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing)
1475 {
1476         XFontStruct **xfonts;
1477         char **font_names;
1478         int i, n;
1479
1480         *ascent = *descent = *lbearing = *rbearing = 0;
1481         n = XFontsOfFontSet(set, &xfonts, &font_names);
1482         for(i = 0; i < n; i++) {
1483                 *ascent = MAX(*ascent, (*xfonts)->ascent);
1484                 *descent = MAX(*descent, (*xfonts)->descent);
1485                 *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
1486                 *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
1487                 xfonts++;
1488         }
1489 }
1490
1491 void
1492 initfonts(char *fontstr, char *bfontstr)
1493 {
1494         if((dc.font.set = xinitfont(fontstr)) == NULL ||
1495            (dc.bfont.set = xinitfont(bfontstr)) == NULL)
1496                 die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
1497         xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
1498             &dc.font.lbearing, &dc.font.rbearing);
1499         xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
1500             &dc.bfont.lbearing, &dc.bfont.rbearing);
1501 }
1502
1503 void
1504 xinit(void) {
1505         XSetWindowAttributes attrs;
1506
1507         if(!(xw.dpy = XOpenDisplay(NULL)))
1508                 die("Can't open display\n");
1509         xw.scr = XDefaultScreen(xw.dpy);
1510         
1511         /* font */
1512         initfonts(FONT, BOLDFONT);
1513
1514         /* XXX: Assuming same size for bold font */
1515         xw.cw = dc.font.rbearing - dc.font.lbearing;
1516         xw.ch = dc.font.ascent + dc.font.descent;
1517
1518         /* colors */
1519         xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1520         xloadcols();
1521
1522         /* window - default size */
1523         xw.bufh = 24 * xw.ch;
1524         xw.bufw = 80 * xw.cw;
1525         xw.h = xw.bufh + 2*BORDER;
1526         xw.w = xw.bufw + 2*BORDER;
1527
1528         attrs.background_pixel = dc.col[DefaultBG];
1529         attrs.border_pixel = dc.col[DefaultBG];
1530         attrs.bit_gravity = NorthWestGravity;
1531         attrs.event_mask = FocusChangeMask | KeyPressMask
1532                 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1533                 | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
1534         attrs.colormap = xw.cmap;
1535
1536         xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
1537                         xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1538                         XDefaultVisual(xw.dpy, xw.scr),
1539                         CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1540                         | CWColormap,
1541                         &attrs);
1542         xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
1543
1544
1545         /* input methods */
1546         xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
1547         xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing 
1548                                            | XIMStatusNothing, XNClientWindow, xw.win, 
1549                                            XNFocusWindow, xw.win, NULL);
1550         /* gc */
1551         dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
1552         
1553         XMapWindow(xw.dpy, xw.win);
1554         xhints();
1555         XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
1556         XSync(xw.dpy, 0);
1557 }
1558
1559 void
1560 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
1561         unsigned long xfg, xbg;
1562         int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
1563         int i;
1564
1565         if(base.mode & ATTR_REVERSE)
1566                 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1567         else
1568                 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1569
1570         XSetBackground(xw.dpy, dc.gc, xbg);
1571         XSetForeground(xw.dpy, dc.gc, xfg);
1572
1573         if(base.mode & ATTR_GFX) {
1574                 for(i = 0; i < bytelen; i++) {
1575                         char c = gfx[(unsigned int)s[i] % 256];
1576                         if(c)
1577                                 s[i] = c;
1578                         else if(s[i] > 0x5f)
1579                                 s[i] -= 0x5f;
1580                 }
1581         }
1582
1583         XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
1584             dc.gc, winx, winy, s, bytelen);
1585         
1586         if(base.mode & ATTR_UNDERLINE)
1587                 XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1588 }
1589
1590 void
1591 xdrawcursor(void) {
1592         static int oldx = 0;
1593         static int oldy = 0;
1594         int sl;
1595         Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
1596         
1597         LIMIT(oldx, 0, term.col-1);
1598         LIMIT(oldy, 0, term.row-1);
1599         
1600         if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1601                 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
1602
1603         /* remove the old cursor */
1604         if(term.line[oldy][oldx].state & GLYPH_SET) {
1605                 sl = utf8size(term.line[oldy][oldx].c);
1606                 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
1607         } else
1608                 xclear(oldx, oldy, oldx, oldy);
1609         
1610         /* draw the new one */
1611         if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
1612                 sl = utf8size(g.c);
1613                 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
1614                 oldx = term.c.x, oldy = term.c.y;
1615         }
1616 }
1617
1618 #ifdef DEBUG
1619 /* basic drawing routines */
1620 void
1621 xdrawc(int x, int y, Glyph g) {
1622         int sl = utf8size(g.c);
1623         XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1624         XSetBackground(xw.dpy, dc.gc, dc.col[g.bg]);
1625         XSetForeground(xw.dpy, dc.gc, dc.col[g.fg]);
1626         XmbDrawImageString(xw.dpy, xw.buf, g.mode&ATTR_BOLD?dc.bfont.fs:dc.font.fs,
1627             dc.gc, r.x, r.y+dc.font.ascent, g.c, sl);
1628 }
1629
1630 void
1631 draw(int dummy) {
1632         int x, y;
1633
1634         xclear(0, 0, term.col-1, term.row-1);
1635         for(y = 0; y < term.row; y++)
1636                 for(x = 0; x < term.col; x++)
1637                         if(term.line[y][x].state & GLYPH_SET)
1638                                 xdrawc(x, y, term.line[y][x]);
1639
1640         xdrawcursor();
1641         XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1642         XFlush(xw.dpy);
1643 }
1644
1645 #else
1646 /* optimized drawing routine */
1647 void
1648 draw(int redraw_all) {
1649         int ic, ib, x, y, ox, sl;
1650         Glyph base, new;
1651         char buf[DRAW_BUF_SIZ];
1652
1653         if(!(xw.state & WIN_VISIBLE))
1654                 return;
1655
1656         xclear(0, 0, term.col-1, term.row-1);
1657         for(y = 0; y < term.row; y++) {
1658                 base = term.line[y][0];
1659                 ic = ib = ox = 0;
1660                 for(x = 0; x < term.col; x++) {
1661                         new = term.line[y][x];
1662                         if(sel.bx!=-1 && *(new.c) && selected(x, y))
1663                                 new.mode ^= ATTR_REVERSE;
1664                         if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1665                                         ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
1666                                 xdraws(buf, base, ox, y, ic, ib);
1667                                 ic = ib = 0;
1668                         }
1669                         if(new.state & GLYPH_SET) {
1670                                 if(ib == 0) {
1671                                         ox = x;
1672                                         base = new;
1673                                 }
1674                                 sl = utf8size(new.c);
1675                                 memcpy(buf+ib, new.c, sl);
1676                                 ib += sl;
1677                                 ++ic;
1678                         }
1679                 }
1680                 if(ib > 0)
1681                         xdraws(buf, base, ox, y, ic, ib);
1682         }
1683         xdrawcursor();
1684         XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1685 }
1686
1687 #endif
1688
1689 void
1690 expose(XEvent *ev) {
1691         XExposeEvent *e = &ev->xexpose;
1692         if(xw.state & WIN_REDRAW) {
1693                 if(!e->count) {
1694                         xw.state &= ~WIN_REDRAW;
1695                         draw(SCREEN_REDRAW);
1696                 }
1697         } else
1698                 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
1699                                 e->width, e->height, e->x, e->y);
1700 }
1701
1702 void
1703 visibility(XEvent *ev) {
1704         XVisibilityEvent *e = &ev->xvisibility;
1705         if(e->state == VisibilityFullyObscured)
1706                 xw.state &= ~WIN_VISIBLE;
1707         else if(!(xw.state & WIN_VISIBLE))
1708                 /* need a full redraw for next Expose, not just a buf copy */
1709                 xw.state |= WIN_VISIBLE | WIN_REDRAW;
1710 }
1711
1712 void
1713 unmap(XEvent *ev) {
1714         xw.state &= ~WIN_VISIBLE;
1715 }
1716
1717 void
1718 xseturgency(int add) {
1719         XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1720         h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1721         XSetWMHints(xw.dpy, xw.win, h);
1722         XFree(h);
1723 }
1724
1725 void
1726 focus(XEvent *ev) {
1727         if(ev->type == FocusIn) {
1728                 xw.state |= WIN_FOCUSED;
1729                 xseturgency(0);
1730         } else
1731                 xw.state &= ~WIN_FOCUSED;
1732         draw(SCREEN_UPDATE);
1733 }
1734
1735 char*
1736 kmap(KeySym k) {
1737         int i;
1738         for(i = 0; i < LEN(key); i++)
1739                 if(key[i].k == k)
1740                         return (char*)key[i].s;
1741         return NULL;
1742 }
1743
1744 void
1745 kpress(XEvent *ev) {
1746         XKeyEvent *e = &ev->xkey;
1747         KeySym ksym;
1748         char buf[32];
1749         char *customkey;
1750         int len;
1751         int meta;
1752         int shift;
1753         Status status;
1754
1755         meta = e->state & Mod1Mask;
1756         shift = e->state & ShiftMask;
1757         len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
1758         
1759         /* 1. custom keys from config.h */
1760         if((customkey = kmap(ksym)))
1761                 ttywrite(customkey, strlen(customkey));
1762         /* 2. hardcoded (overrides X lookup) */
1763         else
1764                 switch(ksym) {
1765                 case XK_Up:
1766                 case XK_Down:
1767                 case XK_Left:
1768                 case XK_Right:
1769                         sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1770                         ttywrite(buf, 3);
1771                         break;
1772                 case XK_Insert:
1773                         if(shift)
1774                                 selpaste();
1775                         break;
1776                 case XK_Return:
1777                         if(IS_SET(MODE_CRLF))
1778                                 ttywrite("\r\n", 2);
1779                         else
1780                                 ttywrite("\r", 1);
1781                         break;
1782                         /* 3. X lookup  */
1783                 default:
1784                         if(len > 0) {
1785                                 if(meta && len == 1)
1786                                         ttywrite("\033", 1);
1787                                 ttywrite(buf, len);
1788                         } else /* 4. nothing to send */
1789                                 fprintf(stderr, "errkey: %d\n", (int)ksym);
1790                         break;
1791                 }
1792 }
1793
1794 void
1795 resize(XEvent *e) {
1796         int col, row;
1797         
1798         if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1799                 return;
1800         
1801         xw.w = e->xconfigure.width;
1802         xw.h = e->xconfigure.height;
1803         col = (xw.w - 2*BORDER) / xw.cw;
1804         row = (xw.h - 2*BORDER) / xw.ch;
1805         if(col == term.col && row == term.row)
1806                 return;
1807         if(tresize(col, row))
1808                 draw(SCREEN_REDRAW);
1809         ttyresize(col, row);
1810         xresize(col, row);
1811 }
1812
1813 void
1814 run(void) {
1815         XEvent ev;
1816         fd_set rfd;
1817         int xfd = XConnectionNumber(xw.dpy);
1818
1819         for(;;) {
1820                 FD_ZERO(&rfd);
1821                 FD_SET(cmdfd, &rfd);
1822                 FD_SET(xfd, &rfd);
1823                 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1824                         if(errno == EINTR)
1825                                 continue;
1826                         die("select failed: %s\n", SERRNO);
1827                 }
1828                 if(FD_ISSET(cmdfd, &rfd)) {
1829                         ttyread();
1830                         draw(SCREEN_UPDATE); 
1831                 }
1832                 while(XPending(xw.dpy)) {
1833                         XNextEvent(xw.dpy, &ev);
1834                         if (XFilterEvent(&ev, xw.win))
1835                                 continue;
1836                         if(handler[ev.type])
1837                                 (handler[ev.type])(&ev);
1838                 }
1839         }
1840 }
1841
1842 int
1843 main(int argc, char *argv[]) {
1844         int i;
1845         
1846         for(i = 1; i < argc; i++) {
1847                 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
1848                 case 't':
1849                         if(++i < argc) opt_title = argv[i];
1850                         break;
1851                 case 'c':
1852                         if(++i < argc) opt_class = argv[i];
1853                         break;
1854                 case 'e':
1855                         if(++i < argc) opt_cmd = &argv[i];
1856                         break;
1857                 case 'v':
1858                 default:
1859                         die(USAGE);
1860                 }
1861                 /* -e eats every remaining arguments */
1862                 if(opt_cmd)
1863                         break;
1864         }
1865         setlocale(LC_CTYPE, "");
1866         tnew(80, 24);
1867         ttynew();
1868         xinit();
1869         selinit();
1870         run();
1871         return 0;
1872 }