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 */
64 /* CSI Escape sequence structs */
65 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
67 char buf[ESC_BUF_SIZ]; /* raw string */
68 int len; /* raw string length */
71 int narg; /* nb of args */
75 /* Internal representation of the screen */
79 Line* line; /* screen */
80 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 */
96 int w; /* window width */
97 int h; /* window height */
98 int ch; /* char height */
99 int cw; /* char width */
109 /* Drawing Context */
111 unsigned long col[LEN(colorname)];
117 static void die(const char *errstr, ...);
118 static void draw(int);
119 static void execsh(void);
120 static void sigchld(int);
121 static void run(void);
123 static void csidump(void);
124 static void csihandle(void);
125 static void csiparse(void);
126 static void csireset(void);
128 static void tclearregion(int, int, int, int);
129 static void tcursor(int);
130 static void tmovecursor(int);
131 static void tdeletechar(int);
132 static void tdeleteline(int);
133 static void tinsertblank(int);
134 static void tinsertblankline(int);
135 static void tmoveto(int, int);
136 static void tnew(int, int);
137 static void tnewline(void);
138 static void tputc(char);
139 static void tputs(char*, int);
140 static void treset(void);
141 static void tresize(int, int);
142 static void tscroll(void);
143 static void tscrollup(int);
144 static void tscrolldown(int);
145 static void tsetattr(int*, int);
146 static void tsetchar(char);
147 static void tsetscroll(int, int);
149 static void ttynew(void);
150 static void ttyread(void);
151 static void ttyresize(int, int);
152 static void ttywrite(const char *, size_t);
154 static unsigned long xgetcol(const char *);
155 static void xclear(int, int, int, int);
156 static void xcursor(int);
157 static void xinit(void);
159 static void expose(XEvent *);
160 static char* kmap(KeySym);
161 static void kpress(XEvent *);
162 static void resize(XEvent *);
164 static void (*handler[LASTEvent])(XEvent *) = {
167 [ConfigureNotify] = resize
174 static CSIEscape escseq;
185 for(row = 0; row < term.row; row++) {
186 for(col = 0; col < term.col; col++) {
187 if(col == term.c.x && row == term.c.y)
190 c = term.line[row][col];
191 putchar(c.state & GLYPH_SET ? c.c : '.');
200 die(const char *errstr, ...) {
203 va_start(ap, errstr);
204 vfprintf(stderr, errstr, ap);
211 char *args[3] = {getenv("SHELL"), "-i", NULL};
212 DEFAULT(args[0], "/bin/sh"); /* default shell if getenv() failed */
213 putenv("TERM=" TNAME);
214 execvp(args[0], args);
218 xbell(void) { /* visual bell */
219 XRectangle r = { BORDER, BORDER, xw.w, xw.h };
220 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
221 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
229 if(waitpid(pid, &stat, 0) < 0)
230 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
232 exit(WEXITSTATUS(stat));
242 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
243 die("openpt failed: %s\n", SERRNO);
245 die("grandpt failed: %s\n", SERRNO);
247 die("unlockpt failed: %s\n", SERRNO);
248 if(!(pts = ptsname(m)))
249 die("ptsname failed: %s\n", SERRNO);
250 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
251 die("Couldn't open slave: %s\n", SERRNO);
252 fcntl(s, F_SETFL, O_NDELAY);
253 switch(pid = fork()) {
255 die("fork failed\n");
258 setsid(); /* create a new process group */
259 dup2(s, STDIN_FILENO);
260 dup2(s, STDOUT_FILENO);
261 dup2(s, STDERR_FILENO);
262 if(ioctl(s, TIOCSCTTY, NULL) < 0)
263 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
269 signal(SIGCHLD, sigchld);
276 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
278 fprintf(stderr, "\n");
283 char buf[BUFSIZ] = {0};
286 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
287 die("Couldn't read from shell: %s\n", SERRNO);
293 ttywrite(const char *s, size_t n) {
294 if(write(cmdfd, s, n) == -1)
295 die("write error on tty: %s\n", SERRNO);
299 ttyresize(int x, int y) {
304 w.ws_xpixel = w.ws_ypixel = 0;
305 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
306 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
313 if(mode == CURSOR_SAVE)
315 else if(mode == CURSOR_LOAD)
316 term.c = c, tmoveto(c.x, c.y);
321 term.c.attr.mode = ATTR_NULL;
322 term.c.attr.fg = DefaultFG;
323 term.c.attr.bg = DefaultBG;
324 term.c.x = term.c.y = 0;
326 term.top = 0, term.bot = term.row - 1;
327 term.mode = MODE_WRAP;
328 tclearregion(0, 0, term.col-1, term.row-1);
332 tnew(int col, int row) { /* screen size */
333 term.row = row, term.col = col;
334 term.top = 0, term.bot = term.row - 1;
336 term.mode = MODE_WRAP;
338 term.c.attr.mode = ATTR_NULL;
339 term.c.attr.fg = DefaultFG;
340 term.c.attr.bg = DefaultBG;
341 term.c.x = term.c.y = 0;
343 /* allocate screen */
344 term.line = calloc(term.row, sizeof(Line));
345 for(row = 0 ; row < term.row; row++)
346 term.line[row] = calloc(term.col, sizeof(Glyph));
349 /* TODO: Replace with scrollup/scolldown */
352 Line temp = term.line[term.top];
355 for(i = term.top; i < term.bot; i++)
356 term.line[i] = term.line[i+1];
357 memset(temp, 0, sizeof(Glyph) * term.col);
358 term.line[term.bot] = temp;
362 tscrolldown (int n) {
366 LIMIT(n, 0, term.bot-term.top+1);
368 for(i = 0; i < n; i++)
369 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
371 for(i = term.bot; i >= term.top+n; i--) {
373 term.line[i] = term.line[i-n];
374 term.line[i-n] = temp;
382 LIMIT(n, 0, term.bot-term.top+1);
384 for(i = 0; i < n; i++)
385 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
387 for(i = term.top; i <= term.bot-n; i++) {
389 term.line[i] = term.line[i+n];
390 term.line[i+n] = temp;
396 int y = term.c.y + 1;
398 tscroll(), y = term.bot;
405 char *p = escseq.buf;
409 escseq.priv = 1, p++;
411 while(p < escseq.buf+escseq.len) {
413 escseq.arg[escseq.narg] *= 10;
414 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
416 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
427 tmoveto(int x, int y) {
428 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
429 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
433 tmovecursor(int dir) {
434 int xf = term.c.x, yf = term.c.y;
445 if(term.mode & MODE_WRAP && xf < 0) {
446 xf = term.col-1, yf--;
448 yf = term.top, xf = 0;
453 if(term.mode & MODE_WRAP && xf >= term.col) {
456 yf = term.bot, tscroll();
465 term.line[term.c.y][term.c.x] = term.c.attr;
466 term.line[term.c.y][term.c.x].c = c;
467 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
471 tclearregion(int x1, int y1, int x2, int y2) {
475 temp = x1, x1 = x2, x2 = temp;
477 temp = y1, y1 = y2, y2 = temp;
479 LIMIT(x1, 0, term.col-1);
480 LIMIT(x2, 0, term.col-1);
481 LIMIT(y1, 0, term.row-1);
482 LIMIT(y2, 0, term.row-1);
484 for(y = y1; y <= y2; y++)
485 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
490 int src = term.c.x + n;
492 int size = term.col - src;
494 if(src >= term.col) {
495 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
498 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
499 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
503 tinsertblank(int n) {
506 int size = term.col - n - src;
508 if(dst >= term.col) {
509 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
512 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
513 tclearregion(src, term.c.y, dst, term.c.y);
517 tinsertblankline(int n) {
522 if(term.c.y > term.bot)
524 else if(term.c.y < term.top)
526 if(term.c.y + n >= bot) {
527 tclearregion(0, term.c.y, term.col-1, bot);
530 for(i = bot; i >= term.c.y+n; i--) {
531 /* swap deleted line <-> blanked line */
532 blank = term.line[i];
533 term.line[i] = term.line[i-n];
534 term.line[i-n] = blank;
536 memset(blank, 0, term.col * sizeof(Glyph));
546 if(term.c.y > term.bot)
548 else if(term.c.y < term.top)
550 if(term.c.y + n >= bot) {
551 tclearregion(0, term.c.y, term.col-1, bot);
554 for(i = term.c.y; i <= bot-n; i++) {
555 /* swap deleted line <-> blanked line */
556 blank = term.line[i];
557 term.line[i] = term.line[i+n];
558 term.line[i+n] = blank;
560 memset(blank, 0, term.col * sizeof(Glyph));
565 tsetattr(int *attr, int l) {
568 for(i = 0; i < l; i++) {
571 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
572 term.c.attr.fg = DefaultFG;
573 term.c.attr.bg = DefaultBG;
576 term.c.attr.mode |= ATTR_BOLD;
579 term.c.attr.mode |= ATTR_UNDERLINE;
582 term.c.attr.mode |= ATTR_REVERSE;
585 term.c.attr.mode &= ~ATTR_BOLD;
588 term.c.attr.mode &= ~ATTR_UNDERLINE;
591 term.c.attr.mode &= ~ATTR_REVERSE;
594 term.c.attr.fg = DefaultFG;
597 term.c.attr.fg = DefaultBG;
600 if(BETWEEN(attr[i], 30, 37))
601 term.c.attr.fg = attr[i] - 30;
602 else if(BETWEEN(attr[i], 40, 47))
603 term.c.attr.bg = attr[i] - 40;
605 fprintf(stderr, "erresc: gfx attr %d unkown\n", attr[i]);
612 tsetscroll(int t, int b) {
615 LIMIT(t, 0, term.row-1);
616 LIMIT(b, 0, term.row-1);
628 switch(escseq.mode) {
631 printf("erresc: unknown csi ");
635 case '@': /* ICH -- Insert <n> blank char */
636 DEFAULT(escseq.arg[0], 1);
637 tinsertblank(escseq.arg[0]);
639 case 'A': /* CUU -- Cursor <n> Up */
641 DEFAULT(escseq.arg[0], 1);
642 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
644 case 'B': /* CUD -- Cursor <n> Down */
645 DEFAULT(escseq.arg[0], 1);
646 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
648 case 'C': /* CUF -- Cursor <n> Forward */
650 DEFAULT(escseq.arg[0], 1);
651 tmoveto(term.c.x+escseq.arg[0], term.c.y);
653 case 'D': /* CUB -- Cursor <n> Backward */
654 DEFAULT(escseq.arg[0], 1);
655 tmoveto(term.c.x-escseq.arg[0], term.c.y);
657 case 'E': /* CNL -- Cursor <n> Down and first col */
658 DEFAULT(escseq.arg[0], 1);
659 tmoveto(0, term.c.y+escseq.arg[0]);
661 case 'F': /* CPL -- Cursor <n> Up and first col */
662 DEFAULT(escseq.arg[0], 1);
663 tmoveto(0, term.c.y-escseq.arg[0]);
665 case 'G': /* CHA -- Move to <col> */
666 case '`': /* XXX: HPA -- same? */
667 DEFAULT(escseq.arg[0], 1);
668 tmoveto(escseq.arg[0]-1, term.c.y);
670 case 'H': /* CUP -- Move to <row> <col> */
671 case 'f': /* XXX: HVP -- same? */
672 DEFAULT(escseq.arg[0], 1);
673 DEFAULT(escseq.arg[1], 1);
674 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
676 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
677 case 'J': /* ED -- Clear screen */
678 switch(escseq.arg[0]) {
680 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
683 tclearregion(0, 0, term.c.x, term.c.y);
686 tclearregion(0, 0, term.col-1, term.row-1);
688 case 3: /* XXX: erase saved lines (xterm) */
693 case 'K': /* EL -- Clear line */
694 switch(escseq.arg[0]) {
696 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
699 tclearregion(0, term.c.y, term.c.x, term.c.y);
702 tclearregion(0, term.c.y, term.col-1, term.c.y);
706 case 'S': /* SU -- Scroll <n> line up */
707 DEFAULT(escseq.arg[0], 1);
708 tscrollup(escseq.arg[0]);
710 case 'T': /* SD -- Scroll <n> line down */
711 DEFAULT(escseq.arg[0], 1);
712 tscrolldown(escseq.arg[0]);
714 case 'L': /* IL -- Insert <n> blank lines */
715 DEFAULT(escseq.arg[0], 1);
716 tinsertblankline(escseq.arg[0]);
718 case 'l': /* RM -- Reset Mode */
720 switch(escseq.arg[0]) {
722 term.mode &= ~MODE_APPKEYPAD;
725 term.mode &= ~MODE_WRAP;
727 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
732 case 1048: /* XXX: no alt. screen to erase/save */
734 tcursor(CURSOR_LOAD);
735 tclearregion(0, 0, term.col-1, term.row-1);
741 switch(escseq.arg[0]) {
743 term.mode &= ~MODE_INSERT;
750 case 'M': /* DL -- Delete <n> lines */
751 DEFAULT(escseq.arg[0], 1);
752 tdeleteline(escseq.arg[0]);
754 case 'X': /* ECH -- Erase <n> char */
755 DEFAULT(escseq.arg[0], 1);
756 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
758 case 'P': /* DCH -- Delete <n> char */
759 DEFAULT(escseq.arg[0], 1);
760 tdeletechar(escseq.arg[0]);
762 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
763 case 'd': /* VPA -- Move to <row> */
764 DEFAULT(escseq.arg[0], 1);
765 tmoveto(term.c.x, escseq.arg[0]-1);
767 case 'h': /* SM -- Set terminal mode */
769 switch(escseq.arg[0]) {
771 term.mode |= MODE_APPKEYPAD;
774 term.mode |= MODE_WRAP;
776 case 12: /* att610 -- Start blinking cursor (IGNORED) */
782 case 1049: /* XXX: no alt. screen to erase/save */
783 tcursor(CURSOR_SAVE);
784 tclearregion(0, 0, term.col-1, term.row-1);
786 default: goto unknown;
789 switch(escseq.arg[0]) {
791 term.mode |= MODE_INSERT;
793 default: goto unknown;
797 case 'm': /* SGR -- Terminal attribute (color) */
798 tsetattr(escseq.arg, escseq.narg);
800 case 'r': /* DECSTBM -- Set Scrolling Region */
804 DEFAULT(escseq.arg[0], 1);
805 DEFAULT(escseq.arg[1], term.row);
806 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
809 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
810 tcursor(CURSOR_SAVE);
812 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
813 tcursor(CURSOR_LOAD);
821 printf("ESC [ %s", escseq.priv ? "? " : "");
823 for(i = 0; i < escseq.narg; i++)
824 printf("%d ", escseq.arg[i]);
826 putchar(escseq.mode);
832 memset(&escseq, 0, sizeof(escseq));
837 int space = TAB - term.c.x % TAB;
839 if(term.c.x + space >= term.col)
842 for(; space > 0; space--)
843 tmovecursor(CURSOR_RIGHT);
848 if(term.esc & ESC_START) {
849 if(term.esc & ESC_CSI) {
850 escseq.buf[escseq.len++] = c;
851 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
853 csiparse(), csihandle();
855 } else if(term.esc & ESC_OSC) {
858 term.esc = ESC_START | ESC_TITLE;
860 } else if(term.esc & ESC_TITLE) {
861 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
863 term.title[term.titlelen] = '\0';
864 XStoreName(xw.dis, xw.win, term.title);
866 term.title[term.titlelen++] = c;
868 } else if(term.esc & ESC_ALTCHARSET) {
870 case '0': /* Line drawing crap */
871 term.c.attr.mode |= ATTR_GFX;
873 case 'B': /* Back to regular text */
874 term.c.attr.mode &= ~ATTR_GFX;
877 printf("esc unhandled charset: ESC ( %c\n", c);
889 term.esc |= ESC_ALTCHARSET;
892 tmoveto(term.c.x, term.c.y-1);
896 tmoveto(term.c.x, term.c.y+1);
900 tmoveto(term.c.x+1, term.c.y);
903 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
904 tmoveto(term.c.x-1, term.c.y);
907 case 'E': /* NEL -- Next line */
911 case 'M': /* RI -- Reverse index */
912 if(term.c.y == term.top)
915 tmoveto(term.c.x, term.c.y-1);
918 case 'c': /* RIS -- Reset to inital state */
922 case '=': /* DECPAM */
923 term.mode |= MODE_APPKEYPAD;
926 case '>': /* DECPNM */
927 term.mode &= ~MODE_APPKEYPAD;
931 tcursor(CURSOR_SAVE);
935 tcursor(CURSOR_LOAD);
939 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
949 tmovecursor(CURSOR_LEFT);
952 tmoveto(0, term.c.y);
962 term.esc = ESC_START;
966 tmovecursor(CURSOR_RIGHT);
973 tputs(char *s, int len) {
974 for(; len > 0; len--)
979 tresize(int col, int row) {
982 int minrow = MIN(row, term.row);
983 int mincol = MIN(col, term.col);
985 if(col < 1 || row < 1)
988 line = calloc(row, sizeof(Line));
989 for(i = 0 ; i < row; i++)
990 line[i] = calloc(col, sizeof(Glyph));
992 for(i = 0 ; i < minrow; i++)
993 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
995 for(i = 0; i < term.row; i++)
999 LIMIT(term.c.x, 0, col-1);
1000 LIMIT(term.c.y, 0, row-1);
1001 LIMIT(term.top, 0, row-1);
1002 LIMIT(term.bot, 0, row-1);
1006 term.col = col, term.row = row;
1010 xgetcol(const char *s) {
1012 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1014 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
1015 color.pixel = WhitePixel(xw.dis, xw.scr);
1016 fprintf(stderr, "Could not allocate color '%s'\n", s);
1022 xclear(int x1, int y1, int x2, int y2) {
1023 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1024 XFillRectangle(xw.dis, xw.buf, dc.gc,
1025 x1 * xw.cw, y1 * xw.ch,
1026 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1034 char *args[] = {NULL};
1037 xw.dis = XOpenDisplay(NULL);
1038 xw.scr = XDefaultScreen(xw.dis);
1040 die("Can't open display\n");
1043 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1044 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1046 /* XXX: Assuming same size for bold font */
1047 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1048 xw.ch = dc.font->ascent + dc.font->descent;
1051 for(i = 0; i < LEN(colorname); i++)
1052 dc.col[i] = xgetcol(colorname[i]);
1054 term.c.attr.fg = DefaultFG;
1055 term.c.attr.bg = DefaultBG;
1056 term.c.attr.mode = ATTR_NULL;
1058 xw.h = term.row * xw.ch;
1059 xw.w = term.col * xw.cw;
1060 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1061 xw.w + 2*BORDER, xw.h + 2*BORDER, 0,
1064 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.w, xw.h, XDefaultDepth(xw.dis, xw.scr));
1066 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1067 XMapWindow(xw.dis, xw.win);
1069 chint.res_name = TNAME, chint.res_class = TNAME;
1070 wmhint.input = 1, wmhint.flags = InputHint;
1071 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
1072 shint.height = xw.h + 2*BORDER, shint.width = xw.w + 2*BORDER;
1073 shint.flags = PSize | PResizeInc;
1074 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
1075 XStoreName(xw.dis, xw.win, TNAME);
1076 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
1082 xdraws(char *s, Glyph base, int x, int y, int len) {
1083 unsigned long xfg, xbg;
1084 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1087 if(base.mode & ATTR_REVERSE)
1088 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1090 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1092 XSetBackground(xw.dis, dc.gc, xbg);
1093 XSetForeground(xw.dis, dc.gc, xfg);
1095 if(base.mode & ATTR_GFX)
1096 for(i = 0; i < len; i++)
1099 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1100 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1102 if(base.mode & ATTR_UNDERLINE)
1103 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1108 static int oldx = 0;
1109 static int oldy = 0;
1110 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1112 LIMIT(oldx, 0, term.col-1);
1113 LIMIT(oldy, 0, term.row-1);
1115 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1116 g.c = term.line[term.c.y][term.c.x].c;
1118 /* remove the old cursor */
1119 if(term.line[oldy][oldx].state & GLYPH_SET)
1120 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1122 xclear(oldx, oldy, oldx, oldy);
1124 /* draw the new one */
1125 if(mode == CURSOR_DRAW) {
1126 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1127 oldx = term.c.x, oldy = term.c.y;
1133 /* basic drawing routines */
1135 xdrawc(int x, int y, Glyph g) {
1136 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1137 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1138 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1139 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1140 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1147 xclear(0, 0, term.col-1, term.row-1);
1148 for(y = 0; y < term.row; y++)
1149 for(x = 0; x < term.col; x++)
1150 if(term.line[y][x].state & GLYPH_SET)
1151 xdrawc(x, y, term.line[y][x]);
1154 xcursor(CURSOR_DRAW);
1155 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.w, xw.h, BORDER, BORDER);
1161 draw(int redraw_all) {
1164 char buf[DRAW_BUF_SIZ];
1166 for(y = 0; y < term.row; y++) {
1167 base = term.line[y][0];
1169 for(x = 0; x < term.col; x++) {
1170 new = term.line[y][x];
1171 if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
1174 xdraws(buf, base, ox, y, i);
1181 xdraws(buf, base, ox, y, i);
1183 xcursor(term.hidec ? CURSOR_HIDE : CURSOR_DRAW);
1184 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.w, xw.h, BORDER, BORDER);
1189 expose(XEvent *ev) {
1190 draw(SCREEN_REDRAW);
1196 for(i = 0; i < LEN(key); i++)
1198 return (char*)key[i].s;
1203 kpress(XEvent *ev) {
1204 XKeyEvent *e = &ev->xkey;
1212 meta = e->state & Mod1Mask;
1213 shift = e->state & ShiftMask;
1214 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1216 if(customkey = kmap(ksym))
1217 ttywrite(customkey, strlen(customkey));
1219 buf[sizeof(buf)-1] = '\0';
1220 if(meta && len == 1)
1221 ttywrite("\033", 1);
1229 sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
1234 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1237 fprintf(stderr, "errkey: %d\n", (int)ksym);
1245 col = e->xconfigure.width / xw.cw;
1246 row = e->xconfigure.height / xw.ch;
1248 if(term.col != col || term.row != row) {
1250 ttyresize(col, row);
1251 xw.w = e->xconfigure.width;
1252 xw.h = e->xconfigure.height;
1253 XFreePixmap(xw.dis, xw.buf);
1254 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.w, xw.h, XDefaultDepth(xw.dis, xw.scr));
1255 draw(SCREEN_REDRAW);
1263 int xfd = XConnectionNumber(xw.dis);
1266 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1267 XResizeWindow(xw.dis, xw.win, xw.w+2*BORDER, xw.h+2*BORDER); /* fix resize bug in wmii (?) */
1271 FD_SET(cmdfd, &rfd);
1273 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
1276 die("select failed: %s\n", SERRNO);
1278 if(FD_ISSET(cmdfd, &rfd)) {
1280 draw(SCREEN_UPDATE);
1282 while(XPending(xw.dis)) {
1283 XNextEvent(xw.dis, &ev);
1284 if(handler[ev.type])
1285 (handler[ev.type])(&ev);
1291 main(int argc, char *argv[]) {
1292 if(argc == 2 && !strncmp("-v", argv[1], 3))
1293 die("st-" VERSION ", © 2009 st engineers\n");
1295 die("usage: st [-v]\n");
1296 setlocale(LC_CTYPE, "");