1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
20 #include <X11/keysym.h>
21 #include <X11/Xutil.h>
26 #define ESC_TITLE_SIZ 256
27 #define ESC_BUF_SIZ 256
28 #define ESC_ARG_SIZ 16
29 #define DRAW_BUF_SIZ 1024
31 #define SERRNO strerror(errno)
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33 #define MAX(a, b) ((a) < (b) ? (b) : (a))
34 #define LEN(a) (sizeof(a) / sizeof(a[0]))
35 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
36 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
37 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
38 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
40 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
41 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
42 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_HIDE, CURSOR_DRAW, CURSOR_SAVE, CURSOR_LOAD };
43 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
44 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4 };
45 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
46 enum { SCREEN_UPDATE, SCREEN_REDRAW };
49 char c; /* character code */
50 char mode; /* attribute flags */
51 int fg; /* foreground */
52 int bg; /* background */
53 char state; /* state flags */
59 Glyph attr; /* current char attributes */
65 /* CSI Escape sequence structs */
66 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
68 char buf[ESC_BUF_SIZ]; /* raw string */
69 int len; /* raw string length */
72 int narg; /* nb of args */
76 /* Internal representation of the screen */
80 Line* line; /* screen */
81 TCursor c; /* cursor */
82 int top; /* top scroll limit */
83 int bot; /* bottom scroll limit */
84 int mode; /* terminal mode flags */
85 int esc; /* escape state flags */
86 char title[ESC_TITLE_SIZ];
90 /* Purely graphic info */
95 int w; /* window width */
96 int h; /* window height */
97 int ch; /* char height */
98 int cw; /* char width */
108 /* Drawing Context */
110 unsigned long col[LEN(colorname)];
115 static void die(const char *errstr, ...);
116 static void draw(int);
117 static void execsh(void);
118 static void sigchld(int);
119 static void run(void);
121 static void csidump(void);
122 static void csihandle(void);
123 static void csiparse(void);
124 static void csireset(void);
126 static void tclearregion(int, int, int, int);
127 static void tcursor(int);
128 static void tmovecursor(int);
129 static void tdeletechar(int);
130 static void tdeleteline(int);
131 static void tinsertblank(int);
132 static void tinsertblankline(int);
133 static void tmoveto(int, int);
134 static void tnew(int, int);
135 static void tnewline(void);
136 static void tputc(char);
137 static void tputs(char*, int);
138 static void tresize(int, int);
139 static void tscroll(void);
140 static void tsetattr(int*, int);
141 static void tsetchar(char);
142 static void tsetscroll(int, int);
144 static void ttynew(void);
145 static void ttyread(void);
146 static void ttyresize(int, int);
147 static void ttywrite(const char *, size_t);
149 static unsigned long xgetcol(const char *);
150 static void xclear(int, int, int, int);
151 static void xcursor(int);
152 static void xdrawc(int, int, Glyph);
153 static void xinit(void);
154 static void xscroll(void);
156 static void expose(XEvent *);
157 static char * kmap(KeySym);
158 static void kpress(XEvent *);
159 static void resize(XEvent *);
161 static void (*handler[LASTEvent])(XEvent *) = {
164 [ConfigureNotify] = resize
171 static CSIEscape escseq;
182 for(row = 0; row < term.row; row++) {
183 for(col = 0; col < term.col; col++) {
184 if(col == term.c.x && row == term.c.y)
187 c = term.line[row][col];
188 putchar(c.state & GLYPH_SET ? c.c : '.');
197 die(const char *errstr, ...) {
200 va_start(ap, errstr);
201 vfprintf(stderr, errstr, ap);
208 char *args[3] = {SHELL, "-i", NULL};
209 putenv("TERM=" TNAME);
214 xbell(void) { /* visual bell */
215 XRectangle r = { 0, 0, xw.w, xw.h };
216 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
217 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
225 if(waitpid(pid, &stat, 0) < 0)
226 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
228 exit(WEXITSTATUS(stat));
238 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
239 die("openpt failed: %s\n", SERRNO);
241 die("grandpt failed: %s\n", SERRNO);
243 die("unlockpt failed: %s\n", SERRNO);
244 if(!(pts = ptsname(m)))
245 die("ptsname failed: %s\n", SERRNO);
246 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
247 die("Couldn't open slave: %s\n", SERRNO);
248 fcntl(s, F_SETFL, O_NDELAY);
249 switch(pid = fork()) {
251 die("fork failed\n");
254 setsid(); /* create a new process group */
255 dup2(s, STDIN_FILENO);
256 dup2(s, STDOUT_FILENO);
257 dup2(s, STDERR_FILENO);
258 if(ioctl(s, TIOCSCTTY, NULL) < 0)
259 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
265 signal(SIGCHLD, sigchld);
272 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
274 fprintf(stderr, "\n");
279 char buf[BUFSIZ] = {0};
282 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
283 die("Couldn't read from shell: %s\n", SERRNO);
289 ttywrite(const char *s, size_t n) {
290 if(write(cmdfd, s, n) == -1)
291 die("write error on tty: %s\n", SERRNO);
295 ttyresize(int x, int y) {
300 w.ws_xpixel = w.ws_ypixel = 0;
301 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
302 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
309 if(mode == CURSOR_SAVE)
311 else if(mode == CURSOR_LOAD)
312 term.c = c, tmoveto(c.x, c.y);
316 tnew(int col, int row) { /* screen size */
317 term.row = row, term.col = col;
318 term.top = 0, term.bot = term.row - 1;
320 term.mode = MODE_WRAP;
322 term.c.attr.mode = ATTR_NULL;
323 term.c.attr.fg = DefaultFG;
324 term.c.attr.bg = DefaultBG;
325 term.c.x = term.c.y = 0;
327 /* allocate screen */
328 term.line = calloc(term.row, sizeof(Line));
329 for(row = 0 ; row < term.row; row++)
330 term.line[row] = calloc(term.col, sizeof(Glyph));
335 Line temp = term.line[term.top];
337 /* X stuff _before_ the line swapping (results in wrong line index) */
339 for(i = term.top; i < term.bot; i++)
340 term.line[i] = term.line[i+1];
341 memset(temp, 0, sizeof(Glyph) * term.col);
342 term.line[term.bot] = temp;
347 int y = term.c.y + 1;
349 tscroll(), y = term.bot;
356 char *p = escseq.buf;
360 escseq.priv = 1, p++;
362 while(p < escseq.buf+escseq.len) {
364 escseq.arg[escseq.narg] *= 10;
365 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
367 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
378 tmoveto(int x, int y) {
379 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
380 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
384 tmovecursor(int dir) {
385 int xf = term.c.x, yf = term.c.y;
396 if(term.mode & MODE_WRAP && xf < 0) {
397 xf = term.col-1, yf--;
399 yf = term.top, xf = 0;
404 if(term.mode & MODE_WRAP && xf >= term.col) {
407 yf = term.bot, tscroll();
416 term.line[term.c.y][term.c.x] = term.c.attr;
417 term.line[term.c.y][term.c.x].c = c;
418 term.line[term.c.y][term.c.x].state |= GLYPH_SET | GLYPH_DIRTY;
422 tclearregion(int x1, int y1, int x2, int y2) {
425 LIMIT(x1, 0, term.col-1);
426 LIMIT(x2, 0, term.col-1);
427 LIMIT(y1, 0, term.row-1);
428 LIMIT(y2, 0, term.row-1);
430 /* XXX: could be optimized */
431 for(x = x1; x <= x2; x++)
432 for(y = y1; y <= y2; y++)
433 memset(&term.line[y][x], 0, sizeof(Glyph));
435 xclear(x1, y1, x2, y2);
440 int src = term.c.x + n;
442 int size = term.col - src;
444 if(src >= term.col) {
445 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
448 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
449 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
453 tinsertblank(int n) {
456 int size = term.col - n - src;
458 if(dst >= term.col) {
459 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
462 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
463 tclearregion(src, term.c.y, dst, term.c.y);
467 tsetlinestate(int n, int state) {
469 for(i = 0; i < term.col; i++)
470 term.line[n][i].state |= state;
474 tinsertblankline(int n) {
479 if(term.c.y > term.bot)
481 else if(term.c.y < term.top)
483 if(term.c.y + n >= bot) {
484 tclearregion(0, term.c.y, term.col-1, bot);
487 for(i = bot; i >= term.c.y+n; i--) {
488 /* swap deleted line <-> blanked line */
489 blank = term.line[i];
490 term.line[i] = term.line[i-n];
491 term.line[i-n] = blank;
493 memset(blank, 0, term.col * sizeof(Glyph));
494 tsetlinestate(i, GLYPH_DIRTY);
495 tsetlinestate(i-n, GLYPH_DIRTY);
505 if(term.c.y > term.bot)
507 else if(term.c.y < term.top)
509 if(term.c.y + n >= bot) {
510 tclearregion(0, term.c.y, term.col-1, bot);
513 for(i = term.c.y; i <= bot-n; i++) {
514 /* swap deleted line <-> blanked line */
515 blank = term.line[i];
516 term.line[i] = term.line[i+n];
517 term.line[i+n] = blank;
519 memset(blank, 0, term.col * sizeof(Glyph));
520 tsetlinestate(i, GLYPH_DIRTY);
521 tsetlinestate(i-n, GLYPH_DIRTY);
526 tsetattr(int *attr, int l) {
529 for(i = 0; i < l; i++) {
532 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
533 term.c.attr.fg = DefaultFG;
534 term.c.attr.bg = DefaultBG;
537 term.c.attr.mode |= ATTR_BOLD;
540 term.c.attr.mode |= ATTR_UNDERLINE;
543 term.c.attr.mode |= ATTR_REVERSE;
546 term.c.hidden = CURSOR_HIDE;
549 term.c.attr.mode &= ~ATTR_BOLD;
552 term.c.attr.mode &= ~ATTR_UNDERLINE;
555 term.c.attr.mode &= ~ATTR_REVERSE;
558 term.c.attr.fg = DefaultFG;
561 term.c.attr.fg = DefaultBG;
564 if(BETWEEN(attr[i], 30, 37))
565 term.c.attr.fg = attr[i] - 30;
566 else if(BETWEEN(attr[i], 40, 47))
567 term.c.attr.bg = attr[i] - 40;
574 tsetscroll(int t, int b) {
577 LIMIT(t, 0, term.row-1);
578 LIMIT(b, 0, term.row-1);
590 switch(escseq.mode) {
593 printf("erresc: unknown sequence -- ");
597 case '@': /* ICH -- Insert <n> blank char */
598 DEFAULT(escseq.arg[0], 1);
599 tinsertblank(escseq.arg[0]);
601 case 'A': /* CUU -- Cursor <n> Up */
603 DEFAULT(escseq.arg[0], 1);
604 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
606 case 'B': /* CUD -- Cursor <n> Down */
607 DEFAULT(escseq.arg[0], 1);
608 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
610 case 'C': /* CUF -- Cursor <n> Forward */
612 DEFAULT(escseq.arg[0], 1);
613 tmoveto(term.c.x+escseq.arg[0], term.c.y);
615 case 'D': /* CUB -- Cursor <n> Backward */
616 DEFAULT(escseq.arg[0], 1);
617 tmoveto(term.c.x-escseq.arg[0], term.c.y);
619 case 'E': /* CNL -- Cursor <n> Down and first col */
620 DEFAULT(escseq.arg[0], 1);
621 tmoveto(0, term.c.y+escseq.arg[0]);
623 case 'F': /* CPL -- Cursor <n> Up and first col */
624 DEFAULT(escseq.arg[0], 1);
625 tmoveto(0, term.c.y-escseq.arg[0]);
627 case 'G': /* CHA -- Move to <col> */
628 case '`': /* XXX: HPA -- same? */
629 DEFAULT(escseq.arg[0], 1);
630 tmoveto(escseq.arg[0]-1, term.c.y);
632 case 'H': /* CUP -- Move to <row> <col> */
633 case 'f': /* XXX: HVP -- same? */
634 DEFAULT(escseq.arg[0], 1);
635 DEFAULT(escseq.arg[1], 1);
636 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
638 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
639 case 'J': /* ED -- Clear screen */
640 switch(escseq.arg[0]) {
642 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
645 tclearregion(0, 0, term.c.x, term.c.y);
648 tclearregion(0, 0, term.col-1, term.row-1);
650 case 3: /* XXX: erase saved lines (xterm) */
655 case 'K': /* EL -- Clear line */
656 switch(escseq.arg[0]) {
658 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
661 tclearregion(0, term.c.y, term.c.x, term.c.y);
664 tclearregion(0, term.c.y, term.col-1, term.c.y);
668 case 'S': /* XXX: SU -- Scroll <n> line up (faked) */
669 case 'L': /* IL -- Insert <n> blank lines */
670 DEFAULT(escseq.arg[0], 1);
671 tinsertblankline(escseq.arg[0]);
673 case 'l': /* RM -- Reset Mode */
675 switch(escseq.arg[0]) {
677 term.mode &= ~MODE_APPKEYPAD;
680 term.mode &= ~MODE_WRAP;
682 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
687 case 1048: /* XXX: no alt. screen to erase/save */
689 tcursor(CURSOR_LOAD);
690 tclearregion(0, 0, term.col-1, term.row-1);
696 switch(escseq.arg[0]) {
698 term.mode &= ~MODE_INSERT;
705 case 'M': /* DL -- Delete <n> lines */
706 DEFAULT(escseq.arg[0], 1);
707 tdeleteline(escseq.arg[0]);
709 case 'X': /* ECH -- Erase <n> char */
710 DEFAULT(escseq.arg[0], 1);
711 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
713 case 'P': /* DCH -- Delete <n> char */
714 DEFAULT(escseq.arg[0], 1);
715 tdeletechar(escseq.arg[0]);
717 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
718 case 'd': /* VPA -- Move to <row> */
719 DEFAULT(escseq.arg[0], 1);
720 tmoveto(term.c.x, escseq.arg[0]-1);
722 case 'h': /* SM -- Set terminal mode */
724 switch(escseq.arg[0]) {
726 term.mode |= MODE_APPKEYPAD;
729 term.mode |= MODE_WRAP;
731 case 12: /* att610 -- Start blinking cursor (IGNORED) */
737 case 1049: /* XXX: no alt. screen to erase/save */
738 tcursor(CURSOR_SAVE);
739 tclearregion(0, 0, term.col-1, term.row-1);
741 default: goto unknown;
744 switch(escseq.arg[0]) {
746 term.mode |= MODE_INSERT;
748 default: goto unknown;
752 case 'm': /* SGR -- Terminal attribute (color) */
753 tsetattr(escseq.arg, escseq.narg);
755 case 'r': /* DECSTBM -- Set Scrolling Region */
759 DEFAULT(escseq.arg[0], 1);
760 DEFAULT(escseq.arg[1], term.row);
761 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
764 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
765 tcursor(CURSOR_SAVE);
767 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
768 tcursor(CURSOR_LOAD);
776 printf("ESC [ %s", escseq.priv ? "? " : "");
778 for(i = 0; i < escseq.narg; i++)
779 printf("%d ", escseq.arg[i]);
781 putchar(escseq.mode);
787 memset(&escseq, 0, sizeof(escseq));
792 int space = TAB - term.c.x % TAB;
794 if(term.c.x + space >= term.col)
797 for(; space > 0; space--)
798 tmovecursor(CURSOR_RIGHT);
804 if(term.esc & ESC_START) {
805 if(term.esc & ESC_CSI) {
806 escseq.buf[escseq.len++] = c;
807 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
809 csiparse(), csihandle();
811 } else if(term.esc & ESC_OSC) {
814 term.esc = ESC_START | ESC_TITLE;
816 } else if(term.esc & ESC_TITLE) {
817 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
819 term.title[term.titlelen] = '\0';
820 XStoreName(xw.dis, xw.win, term.title);
822 term.title[term.titlelen++] = c;
824 } else if(term.esc & ESC_ALTCHARSET) {
826 case '0': /* Line drawing crap */
827 term.c.attr.mode |= ATTR_GFX;
829 case 'B': /* Back to regular text */
830 term.c.attr.mode &= ~ATTR_GFX;
833 printf("esc unhandled charset: ESC ( %c\n", c);
845 term.esc |= ESC_ALTCHARSET;
848 tmoveto(term.c.x, term.c.y-1);
852 tmoveto(term.c.x, term.c.y+1);
856 tmoveto(term.c.x+1, term.c.y);
860 tmoveto(term.c.x-1, term.c.y);
863 case '=': /* DECPAM */
864 term.mode |= MODE_APPKEYPAD;
867 case '>': /* DECPNM */
868 term.mode &= ~MODE_APPKEYPAD;
872 tcursor(CURSOR_SAVE);
875 tcursor(CURSOR_LOAD);
878 fprintf(stderr, "erresc: unknown sequence ESC %02X '%c'\n", c, isprint(c)?c:'.');
888 tmovecursor(CURSOR_LEFT);
891 tmoveto(0, term.c.y);
901 term.esc = ESC_START;
905 tmovecursor(CURSOR_RIGHT);
912 tputs(char *s, int len) {
913 for(; len > 0; len--)
918 tresize(int col, int row) {
921 int minrow = MIN(row, term.row);
922 int mincol = MIN(col, term.col);
924 if(col < 1 || row < 1)
927 line = calloc(row, sizeof(Line));
928 for(i = 0 ; i < row; i++)
929 line[i] = calloc(col, sizeof(Glyph));
931 for(i = 0 ; i < minrow; i++)
932 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
934 for(i = 0; i < term.row; i++)
938 LIMIT(term.c.x, 0, col-1);
939 LIMIT(term.c.y, 0, row-1);
940 LIMIT(term.top, 0, row-1);
941 LIMIT(term.bot, 0, row-1);
945 term.col = col, term.row = row;
949 xgetcol(const char *s) {
951 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
953 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
954 color.pixel = WhitePixel(xw.dis, xw.scr);
955 fprintf(stderr, "Could not allocate color '%s'\n", s);
961 xclear(int x1, int y1, int x2, int y2) {
962 XClearArea(xw.dis, xw.win,
963 x1 * xw.cw, y1 * xw.ch,
964 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
970 int srcy = (term.top+1) * xw.ch;
971 int dsty = term.top * xw.ch;
972 int height = (term.bot-term.top) * xw.ch;
974 xcursor(CURSOR_HIDE);
975 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
976 xclear(0, term.bot, term.col-1, term.bot);
982 unsigned long valuemask;
986 char *args[] = {NULL};
989 xw.dis = XOpenDisplay(NULL);
990 xw.scr = XDefaultScreen(xw.dis);
992 die("Can't open display\n");
995 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
996 die("Can't load font %s\n", FONT);
998 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
999 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
1002 for(i = 0; i < LEN(colorname); i++)
1003 dc.col[i] = xgetcol(colorname[i]);
1005 term.c.attr.fg = DefaultFG;
1006 term.c.attr.bg = DefaultBG;
1007 term.c.attr.mode = ATTR_NULL;
1009 xw.h = term.row * xw.ch;
1010 xw.w = term.col * xw.cw;
1011 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
1012 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1017 values.foreground = XWhitePixel(xw.dis, xw.scr);
1018 values.font = dc.font->fid;
1019 valuemask = GCForeground | GCFont;
1020 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
1021 XMapWindow(xw.dis, xw.win);
1023 chint.res_name = TNAME, chint.res_class = TNAME;
1024 wmhint.input = 1, wmhint.flags = InputHint;
1025 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
1026 shint.height = xw.h, shint.width = xw.w;
1027 shint.flags = PSize | PResizeInc;
1028 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
1029 XStoreName(xw.dis, xw.win, TNAME);
1034 xdraws(char *s, Glyph base, int x, int y, int len) {
1035 unsigned long xfg, xbg;
1036 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1039 if(base.mode & ATTR_REVERSE)
1040 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1042 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1044 XSetBackground(xw.dis, dc.gc, xbg);
1045 XSetForeground(xw.dis, dc.gc, xfg);
1047 if(base.mode & ATTR_GFX)
1048 for(i = 0; i < len; i++)
1051 XDrawImageString(xw.dis, xw.win, dc.gc, winx, winy, s, len);
1053 if(base.mode & ATTR_UNDERLINE)
1054 XDrawLine(xw.dis, xw.win, dc.gc, winx, winy+1, winx+width-1, winy+1);
1058 xdrawc(int x, int y, Glyph g) {
1059 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1060 unsigned long xfg, xbg;
1063 if(g.mode & ATTR_REVERSE)
1064 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
1066 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
1068 XSetBackground(xw.dis, dc.gc, xbg);
1069 XSetForeground(xw.dis, dc.gc, xfg);
1070 XDrawImageString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1075 static int oldx = 0;
1076 static int oldy = 0;
1077 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1079 LIMIT(oldx, 0, term.col-1);
1080 LIMIT(oldy, 0, term.row-1);
1082 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1083 g.c = term.line[term.c.y][term.c.x].c;
1084 /* remove the old cursor */
1085 if(term.line[oldy][oldx].state & GLYPH_SET)
1086 xdrawc(oldx, oldy, term.line[oldy][oldx]);
1088 xclear(oldx, oldy, oldx, oldy);
1089 /* draw the new one */
1090 if(mode == CURSOR_DRAW) {
1091 xdrawc(term.c.x, term.c.y, g);
1092 oldx = term.c.x, oldy = term.c.y;
1097 draw(int redraw_all) {
1100 char buf[DRAW_BUF_SIZ];
1102 for(y = 0; y < term.row; y++) {
1103 base = term.line[y][0];
1105 for(x = 0; x < term.col; x++) {
1106 new = term.line[y][x];
1107 if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
1110 xdraws(buf, base, ox, y, i);
1117 xdraws(buf, base, ox, y, i);
1120 xcursor(CURSOR_DRAW);
1124 expose(XEvent *ev) {
1125 draw(SCREEN_REDRAW);
1131 for(i = 0; i < LEN(key); i++)
1133 return (char*)key[i].s;
1138 kpress(XEvent *ev) {
1139 XKeyEvent *e = &ev->xkey;
1147 meta = e->state & Mod1Mask;
1148 shift = e->state & ShiftMask;
1149 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1151 if(customkey = kmap(ksym))
1152 ttywrite(customkey, strlen(customkey));
1154 buf[sizeof(buf)-1] = '\0';
1155 if(meta && len == 1)
1156 ttywrite("\033", 1);
1164 sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
1169 /* XXX: paste X clipboard */;
1172 fprintf(stderr, "errkey: %d\n", (int)ksym);
1180 col = e->xconfigure.width / xw.cw;
1181 row = e->xconfigure.height / xw.ch;
1183 if(term.col != col || term.row != row) {
1185 ttyresize(col, row);
1186 xw.w = e->xconfigure.width;
1187 xw.h = e->xconfigure.height;
1188 draw(SCREEN_REDRAW);
1196 int xfd = XConnectionNumber(xw.dis);
1199 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1200 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
1204 FD_SET(cmdfd, &rfd);
1206 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
1209 die("select failed: %s\n", SERRNO);
1211 if(FD_ISSET(cmdfd, &rfd)) {
1213 draw(SCREEN_UPDATE);
1215 while(XPending(xw.dis)) {
1216 XNextEvent(xw.dis, &ev);
1217 if(handler[ev.type])
1218 (handler[ev.type])(&ev);
1224 main(int argc, char *argv[]) {
1225 if(argc == 2 && !strncmp("-v", argv[1], 3))
1226 die("st-" VERSION ", © 2009 st engineers\n");
1228 die("usage: st [-v]\n");
1229 setlocale(LC_CTYPE, "");