Xinqi Bao's Git

factored code and fixed behaviour of tnewline().
[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/keysym.h>
21 #include <X11/Xutil.h>
22
23 #if defined(__linux)
24 #include <pty.h>
25 #elif defined(__OpenBSD__) || defined(__NetBSD__)
26 #include <util.h>
27 #elif defined(__FreeBSD__) || defined(__DragonFly__)
28 #include <libutil.h>
29 #endif
30
31 /* Arbitrary sizes */
32 #define ESC_TITLE_SIZ 256
33 #define ESC_BUF_SIZ 256
34 #define ESC_ARG_SIZ 16
35 #define DRAW_BUF_SIZ 1024
36
37 #define SERRNO strerror(errno)
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #define MAX(a, b) ((a) < (b) ? (b) : (a))
40 #define LEN(a) (sizeof(a) / sizeof(a[0]))
41 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
42 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
43 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
44 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
45 #define IS_SET(flag) (term.mode & (flag))
46
47 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
48 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
49 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
50 CURSOR_SAVE, CURSOR_LOAD };
51 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
52 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
53 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8 };
54 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
55 enum { SCREEN_UPDATE, SCREEN_REDRAW };
56
57 typedef struct {
58 char c; /* character code */
59 char mode; /* attribute flags */
60 int fg; /* foreground */
61 int bg; /* background */
62 char state; /* state flags */
63 } Glyph;
64
65 typedef Glyph* Line;
66
67 typedef struct {
68 Glyph attr; /* current char attributes */
69 int x;
70 int y;
71 char state;
72 } TCursor;
73
74 /* CSI Escape sequence structs */
75 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
76 typedef struct {
77 char buf[ESC_BUF_SIZ]; /* raw string */
78 int len; /* raw string length */
79 char priv;
80 int arg[ESC_ARG_SIZ];
81 int narg; /* nb of args */
82 char mode;
83 } CSIEscape;
84
85 /* Internal representation of the screen */
86 typedef struct {
87 int row; /* nb row */
88 int col; /* nb col */
89 Line* line; /* screen */
90 Line* alt; /* alternate screen */
91 TCursor c; /* cursor */
92 int top; /* top scroll limit */
93 int bot; /* bottom scroll limit */
94 int mode; /* terminal mode flags */
95 int esc; /* escape state flags */
96 char title[ESC_TITLE_SIZ];
97 int titlelen;
98 } Term;
99
100 /* Purely graphic info */
101 typedef struct {
102 Display* dis;
103 Window win;
104 Pixmap buf;
105 int scr;
106 int w; /* window width */
107 int h; /* window height */
108 int bufw; /* pixmap width */
109 int bufh; /* pixmap height */
110 int ch; /* char height */
111 int cw; /* char width */
112 int hasfocus;
113 } XWindow;
114
115 typedef struct {
116 KeySym k;
117 char s[ESC_BUF_SIZ];
118 } Key;
119
120 /* Drawing Context */
121 typedef struct {
122 unsigned long col[256];
123 XFontStruct* font;
124 XFontStruct* bfont;
125 GC gc;
126 } DC;
127
128 /* TODO: use better name for vars... */
129 typedef struct {
130 int mode;
131 int bx, by;
132 int ex, ey;
133 struct {int x, y;} b, e;
134 char *clip;
135 } Selection;
136
137 #include "config.h"
138
139 static void die(const char *errstr, ...);
140 static void draw(int);
141 static void execsh(void);
142 static void sigchld(int);
143 static void run(void);
144
145 static void csidump(void);
146 static void csihandle(void);
147 static void csiparse(void);
148 static void csireset(void);
149
150 static void tclearregion(int, int, int, int);
151 static void tcursor(int);
152 static void tdeletechar(int);
153 static void tdeleteline(int);
154 static void tinsertblank(int);
155 static void tinsertblankline(int);
156 static void tmoveto(int, int);
157 static void tnew(int, int);
158 static void tnewline(void);
159 static void tputtab(void);
160 static void tputc(char);
161 static void tputs(char*, int);
162 static void treset(void);
163 static void tresize(int, int);
164 static void tscrollup(int, int);
165 static void tscrolldown(int, int);
166 static void tsetattr(int*, int);
167 static void tsetchar(char);
168 static void tsetscroll(int, int);
169 static void tswapscreen(void);
170
171 static void ttynew(void);
172 static void ttyread(void);
173 static void ttyresize(int, int);
174 static void ttywrite(const char *, size_t);
175
176 static void xdraws(char *, Glyph, int, int, int);
177 static void xhints(void);
178 static void xclear(int, int, int, int);
179 static void xdrawcursor(void);
180 static void xinit(void);
181 static void xloadcols(void);
182 static void xseturgency(int);
183
184 static void expose(XEvent *);
185 static char* kmap(KeySym);
186 static void kpress(XEvent *);
187 static void resize(XEvent *);
188 static void focus(XEvent *);
189 static void brelease(XEvent *);
190 static void bpress(XEvent *);
191 static void bmotion(XEvent *);
192
193
194 static void (*handler[LASTEvent])(XEvent *) = {
195 [KeyPress] = kpress,
196 [Expose] = expose,
197 [ConfigureNotify] = resize,
198 [FocusIn] = focus,
199 [FocusOut] = focus,
200 [MotionNotify] = bmotion,
201 [ButtonPress] = bpress,
202 [ButtonRelease] = brelease,
203 };
204
205 /* Globals */
206 static DC dc;
207 static XWindow xw;
208 static Term term;
209 static CSIEscape escseq;
210 static int cmdfd;
211 static pid_t pid;
212 static Selection sel;
213
214 void
215 selinit(void) {
216 sel.mode = 0;
217 sel.bx = -1;
218 sel.clip = NULL;
219 }
220
221 static inline int selected(int x, int y) {
222 if(sel.ey == y && sel.by == y) {
223 int bx = MIN(sel.bx, sel.ex);
224 int ex = MAX(sel.bx, sel.ex);
225 return BETWEEN(x, bx, ex);
226 }
227 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
228 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
229 }
230
231 static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
232 if(b) *b = e->xbutton.state,
233 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
234 *x = e->xbutton.x/xw.cw;
235 *y = e->xbutton.y/xw.ch;
236 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
237 sel.b.y = MIN(sel.by, sel.ey);
238 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
239 sel.e.y = MAX(sel.by, sel.ey);
240 }
241
242 static void bpress(XEvent *e) {
243 sel.mode = 1;
244 sel.ex = sel.bx = e->xbutton.x/xw.cw;
245 sel.ey = sel.by = e->xbutton.y/xw.ch;
246 }
247
248 static char *getseltext() {
249 char *str, *ptr;
250 int ls, x, y, sz;
251 if(sel.bx == -1)
252 return NULL;
253 sz = (term.col+1) * (sel.e.y-sel.b.y+1);
254 ptr = str = malloc(sz);
255 for(y = 0; y < term.row; y++) {
256 for(x = 0; x < term.col; x++)
257 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y)))
258 *ptr = term.line[y][x].c, ptr++;
259 if(ls)
260 *ptr = '\n', ptr++;
261 }
262 *ptr = 0;
263 return str;
264 }
265
266 /* TODO: use X11 clipboard */
267 static void selcopy(char *str) {
268 free(sel.clip);
269 sel.clip = str;
270 }
271
272 static void selpaste() {
273 if(sel.clip)
274 ttywrite(sel.clip, strlen(sel.clip));
275 }
276
277 /* TODO: doubleclick to select word */
278 static void brelease(XEvent *e) {
279 int b;
280 sel.mode = 0;
281 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
282 if(sel.bx==sel.ex && sel.by==sel.ey) {
283 sel.bx = -1;
284 if(b==2)
285 selpaste();
286 } else {
287 if(b==1)
288 selcopy(getseltext());
289 }
290 draw(1);
291 }
292
293 static void bmotion(XEvent *e) {
294 if (sel.mode) {
295 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
296 draw(1);
297 }
298 }
299
300 #ifdef DEBUG
301 void
302 tdump(void) {
303 int row, col;
304 Glyph c;
305
306 for(row = 0; row < term.row; row++) {
307 for(col = 0; col < term.col; col++) {
308 if(col == term.c.x && row == term.c.y)
309 putchar('#');
310 else {
311 c = term.line[row][col];
312 putchar(c.state & GLYPH_SET ? c.c : '.');
313 }
314 }
315 putchar('\n');
316 }
317 }
318 #endif
319
320 void
321 die(const char *errstr, ...) {
322 va_list ap;
323
324 va_start(ap, errstr);
325 vfprintf(stderr, errstr, ap);
326 va_end(ap);
327 exit(EXIT_FAILURE);
328 }
329
330 void
331 execsh(void) {
332 char *args[3] = {getenv("SHELL"), "-i", NULL};
333 DEFAULT(args[0], SHELL); /* if getenv() failed */
334 putenv("TERM=" TNAME);
335 execvp(args[0], args);
336 }
337
338 void
339 sigchld(int a) {
340 int stat = 0;
341 if(waitpid(pid, &stat, 0) < 0)
342 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
343 if(WIFEXITED(stat))
344 exit(WEXITSTATUS(stat));
345 else
346 exit(EXIT_FAILURE);
347 }
348
349 void
350 ttynew(void) {
351 int m, s;
352
353 /* seems to work fine on linux, openbsd and freebsd */
354 struct winsize w = {term.row, term.col, 0, 0};
355 if(openpty(&m, &s, NULL, NULL, &w) < 0)
356 die("openpty failed: %s\n", SERRNO);
357
358 switch(pid = fork()) {
359 case -1:
360 die("fork failed\n");
361 break;
362 case 0:
363 setsid(); /* create a new process group */
364 dup2(s, STDIN_FILENO);
365 dup2(s, STDOUT_FILENO);
366 dup2(s, STDERR_FILENO);
367 if(ioctl(s, TIOCSCTTY, NULL) < 0)
368 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
369 close(s);
370 close(m);
371 execsh();
372 break;
373 default:
374 close(s);
375 cmdfd = m;
376 signal(SIGCHLD, sigchld);
377 }
378 }
379
380 void
381 dump(char c) {
382 static int col;
383 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
384 if(++col % 10 == 0)
385 fprintf(stderr, "\n");
386 }
387
388 void
389 ttyread(void) {
390 char buf[BUFSIZ];
391 int ret;
392
393 if((ret = read(cmdfd, buf, LEN(buf))) < 0)
394 die("Couldn't read from shell: %s\n", SERRNO);
395 else
396 tputs(buf, ret);
397 }
398
399 void
400 ttywrite(const char *s, size_t n) {
401 if(write(cmdfd, s, n) == -1)
402 die("write error on tty: %s\n", SERRNO);
403 }
404
405 void
406 ttyresize(int x, int y) {
407 struct winsize w;
408
409 w.ws_row = term.row;
410 w.ws_col = term.col;
411 w.ws_xpixel = w.ws_ypixel = 0;
412 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
413 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
414 }
415
416 void
417 tcursor(int mode) {
418 static TCursor c;
419
420 if(mode == CURSOR_SAVE)
421 c = term.c;
422 else if(mode == CURSOR_LOAD)
423 term.c = c, tmoveto(c.x, c.y);
424 }
425
426 void
427 treset(void) {
428 term.c = (TCursor){{
429 .mode = ATTR_NULL,
430 .fg = DefaultFG,
431 .bg = DefaultBG
432 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
433
434 term.top = 0, term.bot = term.row - 1;
435 term.mode = MODE_WRAP;
436 tclearregion(0, 0, term.col-1, term.row-1);
437 }
438
439 void
440 tnew(int col, int row) {
441 /* set screen size */
442 term.row = row, term.col = col;
443 term.line = malloc(term.row * sizeof(Line));
444 term.alt = malloc(term.row * sizeof(Line));
445 for(row = 0 ; row < term.row; row++) {
446 term.line[row] = malloc(term.col * sizeof(Glyph));
447 term.alt [row] = malloc(term.col * sizeof(Glyph));
448 }
449 /* setup screen */
450 treset();
451 }
452
453 void
454 tswapscreen(void) {
455 Line* tmp = term.line;
456 term.line = term.alt;
457 term.alt = tmp;
458 term.mode ^= MODE_ALTSCREEN;
459 }
460
461 void
462 tscrolldown(int orig, int n) {
463 int i;
464 Line temp;
465
466 LIMIT(n, 0, term.bot-orig+1);
467
468 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
469
470 for(i = term.bot; i >= orig+n; i--) {
471 temp = term.line[i];
472 term.line[i] = term.line[i-n];
473 term.line[i-n] = temp;
474 }
475 }
476
477 void
478 tscrollup(int orig, int n) {
479 int i;
480 Line temp;
481 LIMIT(n, 0, term.bot-orig+1);
482
483 tclearregion(0, orig, term.col-1, orig+n-1);
484
485 for(i = orig; i <= term.bot-n; i++) {
486 temp = term.line[i];
487 term.line[i] = term.line[i+n];
488 term.line[i+n] = temp;
489 }
490 }
491
492 void
493 tnewline(void) {
494 int y = term.c.y;
495 if(term.c.y == term.bot)
496 tscrollup(term.top, 1);
497 else
498 y++;
499 tmoveto(0, y);
500 }
501
502 void
503 csiparse(void) {
504 /* int noarg = 1; */
505 char *p = escseq.buf;
506
507 escseq.narg = 0;
508 if(*p == '?')
509 escseq.priv = 1, p++;
510
511 while(p < escseq.buf+escseq.len) {
512 while(isdigit(*p)) {
513 escseq.arg[escseq.narg] *= 10;
514 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
515 }
516 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
517 escseq.narg++, p++;
518 else {
519 escseq.mode = *p;
520 escseq.narg++;
521 return;
522 }
523 }
524 }
525
526 void
527 tmoveto(int x, int y) {
528 LIMIT(x, 0, term.col-1);
529 LIMIT(y, 0, term.row-1);
530 term.c.state &= ~CURSOR_WRAPNEXT;
531 term.c.x = x;
532 term.c.y = y;
533 }
534
535 void
536 tsetchar(char c) {
537 term.line[term.c.y][term.c.x] = term.c.attr;
538 term.line[term.c.y][term.c.x].c = c;
539 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
540 }
541
542 void
543 tclearregion(int x1, int y1, int x2, int y2) {
544 int y, temp;
545
546 if(x1 > x2)
547 temp = x1, x1 = x2, x2 = temp;
548 if(y1 > y2)
549 temp = y1, y1 = y2, y2 = temp;
550
551 LIMIT(x1, 0, term.col-1);
552 LIMIT(x2, 0, term.col-1);
553 LIMIT(y1, 0, term.row-1);
554 LIMIT(y2, 0, term.row-1);
555
556 for(y = y1; y <= y2; y++)
557 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
558 }
559
560 void
561 tdeletechar(int n) {
562 int src = term.c.x + n;
563 int dst = term.c.x;
564 int size = term.col - src;
565
566 if(src >= term.col) {
567 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
568 return;
569 }
570 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
571 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
572 }
573
574 void
575 tinsertblank(int n) {
576 int src = term.c.x;
577 int dst = src + n;
578 int size = term.col - dst;
579
580 if(dst >= term.col) {
581 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
582 return;
583 }
584 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
585 tclearregion(src, term.c.y, dst - 1, term.c.y);
586 }
587
588 void
589 tinsertblankline(int n) {
590 if(term.c.y < term.top || term.c.y > term.bot)
591 return;
592
593 tscrolldown(term.c.y, n);
594 }
595
596 void
597 tdeleteline(int n) {
598 if(term.c.y < term.top || term.c.y > term.bot)
599 return;
600
601 tscrollup(term.c.y, n);
602 }
603
604 void
605 tsetattr(int *attr, int l) {
606 int i;
607
608 for(i = 0; i < l; i++) {
609 switch(attr[i]) {
610 case 0:
611 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
612 term.c.attr.fg = DefaultFG;
613 term.c.attr.bg = DefaultBG;
614 break;
615 case 1:
616 term.c.attr.mode |= ATTR_BOLD;
617 break;
618 case 4:
619 term.c.attr.mode |= ATTR_UNDERLINE;
620 break;
621 case 7:
622 term.c.attr.mode |= ATTR_REVERSE;
623 break;
624 case 22:
625 term.c.attr.mode &= ~ATTR_BOLD;
626 break;
627 case 24:
628 term.c.attr.mode &= ~ATTR_UNDERLINE;
629 break;
630 case 27:
631 term.c.attr.mode &= ~ATTR_REVERSE;
632 break;
633 case 38:
634 if (i + 2 < l && attr[i + 1] == 5) {
635 i += 2;
636 if (BETWEEN(attr[i], 0, 255))
637 term.c.attr.fg = attr[i];
638 else
639 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
640 }
641 else
642 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
643 break;
644 case 39:
645 term.c.attr.fg = DefaultFG;
646 break;
647 case 48:
648 if (i + 2 < l && attr[i + 1] == 5) {
649 i += 2;
650 if (BETWEEN(attr[i], 0, 255))
651 term.c.attr.bg = attr[i];
652 else
653 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
654 }
655 else
656 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
657 break;
658 case 49:
659 term.c.attr.bg = DefaultBG;
660 break;
661 default:
662 if(BETWEEN(attr[i], 30, 37))
663 term.c.attr.fg = attr[i] - 30;
664 else if(BETWEEN(attr[i], 40, 47))
665 term.c.attr.bg = attr[i] - 40;
666 else if(BETWEEN(attr[i], 90, 97))
667 term.c.attr.fg = attr[i] - 90 + 8;
668 else if(BETWEEN(attr[i], 100, 107))
669 term.c.attr.fg = attr[i] - 100 + 8;
670 else
671 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
672 break;
673 }
674 }
675 }
676
677 void
678 tsetscroll(int t, int b) {
679 int temp;
680
681 LIMIT(t, 0, term.row-1);
682 LIMIT(b, 0, term.row-1);
683 if(t > b) {
684 temp = t;
685 t = b;
686 b = temp;
687 }
688 term.top = t;
689 term.bot = b;
690 }
691
692 void
693 csihandle(void) {
694 switch(escseq.mode) {
695 default:
696 unknown:
697 printf("erresc: unknown csi ");
698 csidump();
699 /* die(""); */
700 break;
701 case '@': /* ICH -- Insert <n> blank char */
702 DEFAULT(escseq.arg[0], 1);
703 tinsertblank(escseq.arg[0]);
704 break;
705 case 'A': /* CUU -- Cursor <n> Up */
706 case 'e':
707 DEFAULT(escseq.arg[0], 1);
708 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
709 break;
710 case 'B': /* CUD -- Cursor <n> Down */
711 DEFAULT(escseq.arg[0], 1);
712 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
713 break;
714 case 'C': /* CUF -- Cursor <n> Forward */
715 case 'a':
716 DEFAULT(escseq.arg[0], 1);
717 tmoveto(term.c.x+escseq.arg[0], term.c.y);
718 break;
719 case 'D': /* CUB -- Cursor <n> Backward */
720 DEFAULT(escseq.arg[0], 1);
721 tmoveto(term.c.x-escseq.arg[0], term.c.y);
722 break;
723 case 'E': /* CNL -- Cursor <n> Down and first col */
724 DEFAULT(escseq.arg[0], 1);
725 tmoveto(0, term.c.y+escseq.arg[0]);
726 break;
727 case 'F': /* CPL -- Cursor <n> Up and first col */
728 DEFAULT(escseq.arg[0], 1);
729 tmoveto(0, term.c.y-escseq.arg[0]);
730 break;
731 case 'G': /* CHA -- Move to <col> */
732 case '`': /* XXX: HPA -- same? */
733 DEFAULT(escseq.arg[0], 1);
734 tmoveto(escseq.arg[0]-1, term.c.y);
735 break;
736 case 'H': /* CUP -- Move to <row> <col> */
737 case 'f': /* XXX: HVP -- same? */
738 DEFAULT(escseq.arg[0], 1);
739 DEFAULT(escseq.arg[1], 1);
740 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
741 break;
742 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
743 case 'J': /* ED -- Clear screen */
744 switch(escseq.arg[0]) {
745 case 0: /* below */
746 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
747 break;
748 case 1: /* above */
749 tclearregion(0, 0, term.c.x, term.c.y);
750 break;
751 case 2: /* all */
752 tclearregion(0, 0, term.col-1, term.row-1);
753 break;
754 default:
755 goto unknown;
756 }
757 break;
758 case 'K': /* EL -- Clear line */
759 switch(escseq.arg[0]) {
760 case 0: /* right */
761 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
762 break;
763 case 1: /* left */
764 tclearregion(0, term.c.y, term.c.x, term.c.y);
765 break;
766 case 2: /* all */
767 tclearregion(0, term.c.y, term.col-1, term.c.y);
768 break;
769 }
770 break;
771 case 'S': /* SU -- Scroll <n> line up */
772 DEFAULT(escseq.arg[0], 1);
773 tscrollup(term.top, escseq.arg[0]);
774 break;
775 case 'T': /* SD -- Scroll <n> line down */
776 DEFAULT(escseq.arg[0], 1);
777 tscrolldown(term.top, escseq.arg[0]);
778 break;
779 case 'L': /* IL -- Insert <n> blank lines */
780 DEFAULT(escseq.arg[0], 1);
781 tinsertblankline(escseq.arg[0]);
782 break;
783 case 'l': /* RM -- Reset Mode */
784 if(escseq.priv) {
785 switch(escseq.arg[0]) {
786 case 1:
787 term.mode &= ~MODE_APPKEYPAD;
788 break;
789 case 7:
790 term.mode &= ~MODE_WRAP;
791 break;
792 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
793 break;
794 case 25:
795 term.c.state |= CURSOR_HIDE;
796 break;
797 case 1049: /* = 1047 and 1048 */
798 case 1047:
799 if(IS_SET(MODE_ALTSCREEN)) {
800 tclearregion(0, 0, term.col-1, term.row-1);
801 tswapscreen();
802 }
803 if(escseq.arg[0] == 1047)
804 break;
805 case 1048:
806 tcursor(CURSOR_LOAD);
807 break;
808 default:
809 goto unknown;
810 }
811 } else {
812 switch(escseq.arg[0]) {
813 case 4:
814 term.mode &= ~MODE_INSERT;
815 break;
816 default:
817 goto unknown;
818 }
819 }
820 break;
821 case 'M': /* DL -- Delete <n> lines */
822 DEFAULT(escseq.arg[0], 1);
823 tdeleteline(escseq.arg[0]);
824 break;
825 case 'X': /* ECH -- Erase <n> char */
826 DEFAULT(escseq.arg[0], 1);
827 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
828 break;
829 case 'P': /* DCH -- Delete <n> char */
830 DEFAULT(escseq.arg[0], 1);
831 tdeletechar(escseq.arg[0]);
832 break;
833 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
834 case 'd': /* VPA -- Move to <row> */
835 DEFAULT(escseq.arg[0], 1);
836 tmoveto(term.c.x, escseq.arg[0]-1);
837 break;
838 case 'h': /* SM -- Set terminal mode */
839 if(escseq.priv) {
840 switch(escseq.arg[0]) {
841 case 1:
842 term.mode |= MODE_APPKEYPAD;
843 break;
844 case 7:
845 term.mode |= MODE_WRAP;
846 break;
847 case 12: /* att610 -- Start blinking cursor (IGNORED) */
848 break;
849 case 25:
850 term.c.state &= ~CURSOR_HIDE;
851 break;
852 case 1049: /* = 1047 and 1048 */
853 case 1047:
854 if(IS_SET(MODE_ALTSCREEN))
855 tclearregion(0, 0, term.col-1, term.row-1);
856 else
857 tswapscreen();
858 if(escseq.arg[0] == 1047)
859 break;
860 case 1048:
861 tcursor(CURSOR_SAVE);
862 break;
863 default: goto unknown;
864 }
865 } else {
866 switch(escseq.arg[0]) {
867 case 4:
868 term.mode |= MODE_INSERT;
869 break;
870 default: goto unknown;
871 }
872 };
873 break;
874 case 'm': /* SGR -- Terminal attribute (color) */
875 tsetattr(escseq.arg, escseq.narg);
876 break;
877 case 'r': /* DECSTBM -- Set Scrolling Region */
878 if(escseq.priv)
879 goto unknown;
880 else {
881 DEFAULT(escseq.arg[0], 1);
882 DEFAULT(escseq.arg[1], term.row);
883 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
884 tmoveto(0, 0);
885 }
886 break;
887 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
888 tcursor(CURSOR_SAVE);
889 break;
890 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
891 tcursor(CURSOR_LOAD);
892 break;
893 }
894 }
895
896 void
897 csidump(void) {
898 int i;
899 printf("ESC [ %s", escseq.priv ? "? " : "");
900 if(escseq.narg)
901 for(i = 0; i < escseq.narg; i++)
902 printf("%d ", escseq.arg[i]);
903 if(escseq.mode)
904 putchar(escseq.mode);
905 putchar('\n');
906 }
907
908 void
909 csireset(void) {
910 memset(&escseq, 0, sizeof(escseq));
911 }
912
913 void
914 tputtab(void) {
915 int space = TAB - term.c.x % TAB;
916 tmoveto(term.c.x + space, term.c.y);
917 }
918
919 void
920 tputc(char c) {
921 if(term.esc & ESC_START) {
922 if(term.esc & ESC_CSI) {
923 escseq.buf[escseq.len++] = c;
924 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
925 term.esc = 0;
926 csiparse(), csihandle();
927 }
928 /* TODO: handle other OSC */
929 } else if(term.esc & ESC_OSC) {
930 if(c == ';') {
931 term.titlelen = 0;
932 term.esc = ESC_START | ESC_TITLE;
933 }
934 } else if(term.esc & ESC_TITLE) {
935 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
936 term.esc = 0;
937 term.title[term.titlelen] = '\0';
938 XStoreName(xw.dis, xw.win, term.title);
939 } else {
940 term.title[term.titlelen++] = c;
941 }
942 } else if(term.esc & ESC_ALTCHARSET) {
943 switch(c) {
944 case '0': /* Line drawing crap */
945 term.c.attr.mode |= ATTR_GFX;
946 break;
947 case 'B': /* Back to regular text */
948 term.c.attr.mode &= ~ATTR_GFX;
949 break;
950 default:
951 printf("esc unhandled charset: ESC ( %c\n", c);
952 }
953 term.esc = 0;
954 } else {
955 switch(c) {
956 case '[':
957 term.esc |= ESC_CSI;
958 break;
959 case ']':
960 term.esc |= ESC_OSC;
961 break;
962 case '(':
963 term.esc |= ESC_ALTCHARSET;
964 break;
965 case 'D': /* IND -- Linefeed */
966 if(term.c.y == term.bot)
967 tscrollup(term.top, 1);
968 else
969 tmoveto(term.c.x, term.c.y+1);
970 term.esc = 0;
971 break;
972 case 'E': /* NEL -- Next line */
973 tnewline();
974 term.esc = 0;
975 break;
976 case 'M': /* RI -- Reverse index */
977 if(term.c.y == term.top)
978 tscrolldown(term.top, 1);
979 else
980 tmoveto(term.c.x, term.c.y-1);
981 term.esc = 0;
982 break;
983 case 'c': /* RIS -- Reset to inital state */
984 treset();
985 term.esc = 0;
986 break;
987 case '=': /* DECPAM -- Application keypad */
988 term.mode |= MODE_APPKEYPAD;
989 term.esc = 0;
990 break;
991 case '>': /* DECPNM -- Normal keypad */
992 term.mode &= ~MODE_APPKEYPAD;
993 term.esc = 0;
994 break;
995 case '7': /* DECSC -- Save Cursor */
996 tcursor(CURSOR_SAVE);
997 term.esc = 0;
998 break;
999 case '8': /* DECRC -- Restore Cursor */
1000 tcursor(CURSOR_LOAD);
1001 term.esc = 0;
1002 break;
1003 default:
1004 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1005 term.esc = 0;
1006 }
1007 }
1008 } else {
1009 switch(c) {
1010 case '\t':
1011 tputtab();
1012 break;
1013 case '\b':
1014 tmoveto(term.c.x-1, term.c.y);
1015 break;
1016 case '\r':
1017 tmoveto(0, term.c.y);
1018 break;
1019 case '\n':
1020 tnewline();
1021 break;
1022 case '\a':
1023 if(!xw.hasfocus)
1024 xseturgency(1);
1025 break;
1026 case '\033':
1027 csireset();
1028 term.esc = ESC_START;
1029 break;
1030 default:
1031 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1032 tnewline();
1033 tsetchar(c);
1034 if(term.c.x+1 < term.col)
1035 tmoveto(term.c.x+1, term.c.y);
1036 else
1037 term.c.state |= CURSOR_WRAPNEXT;
1038 break;
1039 }
1040 }
1041 }
1042
1043 void
1044 tputs(char *s, int len) {
1045 for(; len > 0; len--)
1046 tputc(*s++);
1047 }
1048
1049 void
1050 tresize(int col, int row) {
1051 int i;
1052 int minrow = MIN(row, term.row);
1053 int mincol = MIN(col, term.col);
1054
1055 if(col < 1 || row < 1)
1056 return;
1057
1058 /* free uneeded rows */
1059 for(i = row; i < term.row; i++) {
1060 free(term.line[i]);
1061 free(term.alt[i]);
1062 }
1063
1064 /* resize to new height */
1065 term.line = realloc(term.line, row * sizeof(Line));
1066 term.alt = realloc(term.alt, row * sizeof(Line));
1067
1068 /* resize each row to new width, zero-pad if needed */
1069 for(i = 0; i < minrow; i++) {
1070 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1071 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1072 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1073 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1074 }
1075
1076 /* allocate any new rows */
1077 for(/* i == minrow */; i < row; i++) {
1078 term.line[i] = calloc(col, sizeof(Glyph));
1079 term.alt [i] = calloc(col, sizeof(Glyph));
1080 }
1081
1082 /* update terminal size */
1083 term.col = col, term.row = row;
1084 /* make use of the LIMIT in tmoveto */
1085 tmoveto(term.c.x, term.c.y);
1086 /* reset scrolling region */
1087 tsetscroll(0, row-1);
1088 }
1089
1090 void
1091 xloadcols(void) {
1092 int i, r, g, b;
1093 XColor color;
1094 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1095 unsigned long white = WhitePixel(xw.dis, xw.scr);
1096
1097 for(i = 0; i < 16; i++) {
1098 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1099 dc.col[i] = white;
1100 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1101 } else
1102 dc.col[i] = color.pixel;
1103 }
1104
1105 /* same colors as xterm */
1106 for(r = 0; r < 6; r++)
1107 for(g = 0; g < 6; g++)
1108 for(b = 0; b < 6; b++) {
1109 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1110 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1111 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1112 if (!XAllocColor(xw.dis, cmap, &color)) {
1113 dc.col[i] = white;
1114 fprintf(stderr, "Could not allocate color %d\n", i);
1115 } else
1116 dc.col[i] = color.pixel;
1117 i++;
1118 }
1119
1120 for(r = 0; r < 24; r++, i++) {
1121 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1122 if (!XAllocColor(xw.dis, cmap, &color)) {
1123 dc.col[i] = white;
1124 fprintf(stderr, "Could not allocate color %d\n", i);
1125 } else
1126 dc.col[i] = color.pixel;
1127 }
1128 }
1129
1130 void
1131 xclear(int x1, int y1, int x2, int y2) {
1132 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1133 XFillRectangle(xw.dis, xw.buf, dc.gc,
1134 x1 * xw.cw, y1 * xw.ch,
1135 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1136 }
1137
1138 void
1139 xhints(void)
1140 {
1141 XClassHint class = {TNAME, TNAME};
1142 XWMHints wm = {.flags = InputHint, .input = 1};
1143 XSizeHints size = {
1144 .flags = PSize | PResizeInc | PBaseSize,
1145 .height = xw.h,
1146 .width = xw.w,
1147 .height_inc = xw.ch,
1148 .width_inc = xw.cw,
1149 .base_height = 2*BORDER,
1150 .base_width = 2*BORDER,
1151 };
1152 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1153 }
1154
1155 void
1156 xinit(void) {
1157 if(!(xw.dis = XOpenDisplay(NULL)))
1158 die("Can't open display\n");
1159 xw.scr = XDefaultScreen(xw.dis);
1160
1161 /* font */
1162 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1163 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1164
1165 /* XXX: Assuming same size for bold font */
1166 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1167 xw.ch = dc.font->ascent + dc.font->descent;
1168
1169 /* colors */
1170 xloadcols();
1171
1172 /* windows */
1173 xw.bufh = term.row * xw.ch;
1174 xw.bufw = term.col * xw.cw;
1175 xw.h = xw.bufh + 2*BORDER;
1176 xw.w = xw.bufw + 2*BORDER;
1177 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1178 xw.w, xw.h, 0,
1179 dc.col[DefaultBG],
1180 dc.col[DefaultBG]);
1181 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1182 /* gc */
1183 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1184
1185 /* event mask */
1186 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask
1187 | StructureNotifyMask | FocusChangeMask | PointerMotionMask
1188 | ButtonPressMask | ButtonReleaseMask);
1189
1190 XMapWindow(xw.dis, xw.win);
1191 xhints();
1192 XStoreName(xw.dis, xw.win, "st");
1193 XSync(xw.dis, 0);
1194 }
1195
1196 void
1197 xdraws(char *s, Glyph base, int x, int y, int len) {
1198 unsigned long xfg, xbg;
1199 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1200 int i;
1201
1202 if(base.mode & ATTR_REVERSE)
1203 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1204 else
1205 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1206
1207 XSetBackground(xw.dis, dc.gc, xbg);
1208 XSetForeground(xw.dis, dc.gc, xfg);
1209
1210 if(base.mode & ATTR_GFX)
1211 for(i = 0; i < len; i++)
1212 s[i] = gfx[(int)s[i]];
1213
1214 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1215 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1216
1217 if(base.mode & ATTR_UNDERLINE)
1218 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1219 }
1220
1221 void
1222 xdrawcursor(void) {
1223 static int oldx = 0;
1224 static int oldy = 0;
1225 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1226
1227 LIMIT(oldx, 0, term.col-1);
1228 LIMIT(oldy, 0, term.row-1);
1229
1230 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1231 g.c = term.line[term.c.y][term.c.x].c;
1232
1233 /* remove the old cursor */
1234 if(term.line[oldy][oldx].state & GLYPH_SET)
1235 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1236 else
1237 xclear(oldx, oldy, oldx, oldy);
1238
1239 /* draw the new one */
1240 if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
1241 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1242 oldx = term.c.x, oldy = term.c.y;
1243 }
1244 }
1245
1246 #ifdef DEBUG
1247 /* basic drawing routines */
1248 void
1249 xdrawc(int x, int y, Glyph g) {
1250 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1251 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1252 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1253 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1254 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1255 }
1256
1257 void
1258 draw(int dummy) {
1259 int x, y;
1260
1261 xclear(0, 0, term.col-1, term.row-1);
1262 for(y = 0; y < term.row; y++)
1263 for(x = 0; x < term.col; x++)
1264 if(term.line[y][x].state & GLYPH_SET)
1265 xdrawc(x, y, term.line[y][x]);
1266
1267 xdrawcursor();
1268 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1269 XFlush(xw.dis);
1270 }
1271
1272 #else
1273 /* optimized drawing routine */
1274 void
1275 draw(int redraw_all) {
1276 int i, x, y, ox;
1277 Glyph base, new;
1278 char buf[DRAW_BUF_SIZ];
1279
1280 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1281 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
1282 for(y = 0; y < term.row; y++) {
1283 base = term.line[y][0];
1284 i = ox = 0;
1285 for(x = 0; x < term.col; x++) {
1286 new = term.line[y][x];
1287 if(sel.bx!=-1 && new.c && selected(x, y))
1288 new.mode ^= ATTR_REVERSE;
1289 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1290 i >= DRAW_BUF_SIZ)) {
1291 xdraws(buf, base, ox, y, i);
1292 i = 0;
1293 }
1294 if(new.state & GLYPH_SET) {
1295 if(i == 0) {
1296 ox = x;
1297 base = new;
1298 }
1299 buf[i++] = new.c;
1300 }
1301 }
1302 if(i > 0)
1303 xdraws(buf, base, ox, y, i);
1304 }
1305 xdrawcursor();
1306 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1307 XFlush(xw.dis);
1308 }
1309
1310 #endif
1311
1312 void
1313 expose(XEvent *ev) {
1314 draw(SCREEN_REDRAW);
1315 }
1316
1317 void
1318 xseturgency(int add) {
1319 XWMHints *h = XGetWMHints(xw.dis, xw.win);
1320 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1321 XSetWMHints(xw.dis, xw.win, h);
1322 XFree(h);
1323 }
1324
1325 void
1326 focus(XEvent *ev) {
1327 if((xw.hasfocus = ev->type == FocusIn))
1328 xseturgency(0);
1329 draw(SCREEN_UPDATE);
1330 }
1331
1332 char*
1333 kmap(KeySym k) {
1334 int i;
1335 for(i = 0; i < LEN(key); i++)
1336 if(key[i].k == k)
1337 return (char*)key[i].s;
1338 return NULL;
1339 }
1340
1341 void
1342 kpress(XEvent *ev) {
1343 XKeyEvent *e = &ev->xkey;
1344 KeySym ksym;
1345 char buf[32];
1346 char *customkey;
1347 int len;
1348 int meta;
1349 int shift;
1350
1351 meta = e->state & Mod1Mask;
1352 shift = e->state & ShiftMask;
1353 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1354
1355 if((customkey = kmap(ksym)))
1356 ttywrite(customkey, strlen(customkey));
1357 else if(len > 0) {
1358 buf[sizeof(buf)-1] = '\0';
1359 if(meta && len == 1)
1360 ttywrite("\033", 1);
1361 ttywrite(buf, len);
1362 } else
1363 switch(ksym) {
1364 case XK_Up:
1365 case XK_Down:
1366 case XK_Left:
1367 case XK_Right:
1368 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1369 ttywrite(buf, 3);
1370 break;
1371 case XK_Insert:
1372 if(shift)
1373 selpaste(), draw(1);
1374 break;
1375 default:
1376 fprintf(stderr, "errkey: %d\n", (int)ksym);
1377 break;
1378 }
1379 }
1380
1381 void
1382 resize(XEvent *e) {
1383 int col, row;
1384
1385 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1386 return;
1387
1388 xw.w = e->xconfigure.width;
1389 xw.h = e->xconfigure.height;
1390 xw.bufw = xw.w - 2*BORDER;
1391 xw.bufh = xw.h - 2*BORDER;
1392 col = xw.bufw / xw.cw;
1393 row = xw.bufh / xw.ch;
1394 tresize(col, row);
1395 ttyresize(col, row);
1396 xw.bufh = MAX(1, xw.bufh);
1397 xw.bufw = MAX(1, xw.bufw);
1398 XFreePixmap(xw.dis, xw.buf);
1399 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1400 draw(SCREEN_REDRAW);
1401 }
1402
1403 void
1404 run(void) {
1405 XEvent ev;
1406 fd_set rfd;
1407 int xfd = XConnectionNumber(xw.dis);
1408
1409 for(;;) {
1410 FD_ZERO(&rfd);
1411 FD_SET(cmdfd, &rfd);
1412 FD_SET(xfd, &rfd);
1413 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1414 if(errno == EINTR)
1415 continue;
1416 die("select failed: %s\n", SERRNO);
1417 }
1418 if(FD_ISSET(cmdfd, &rfd)) {
1419 ttyread();
1420 draw(SCREEN_UPDATE);
1421 }
1422 while(XPending(xw.dis)) {
1423 XNextEvent(xw.dis, &ev);
1424 if(handler[ev.type])
1425 (handler[ev.type])(&ev);
1426 }
1427 }
1428 }
1429
1430 int
1431 main(int argc, char *argv[]) {
1432 if(argc == 2 && !strncmp("-v", argv[1], 3))
1433 die("st-" VERSION ", (c) 2010 st engineers\n");
1434 else if(argc != 1)
1435 die("usage: st [-v]\n");
1436 setlocale(LC_CTYPE, "");
1437 tnew(80, 24);
1438 ttynew();
1439 xinit();
1440 selinit();
1441 run();
1442 return 0;
1443 }