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>
25 #elif defined(OPENBSD)
27 #elif defined(FREEBSD)
32 #define ESC_TITLE_SIZ 256
33 #define ESC_BUF_SIZ 256
34 #define ESC_ARG_SIZ 16
35 #define DRAW_BUF_SIZ 1024
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))
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, CURSOR_HIDE, CURSOR_DRAW,
50 CURSOR_SAVE, CURSOR_LOAD };
51 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
52 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4 };
53 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
54 enum { SCREEN_UPDATE, SCREEN_REDRAW };
57 char c; /* character code */
58 char mode; /* attribute flags */
59 int fg; /* foreground */
60 int bg; /* background */
61 char state; /* state flags */
67 Glyph attr; /* current char attributes */
73 /* CSI Escape sequence structs */
74 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
76 char buf[ESC_BUF_SIZ]; /* raw string */
77 int len; /* raw string length */
80 int narg; /* nb of args */
84 /* Internal representation of the screen */
88 Line* line; /* screen */
89 TCursor c; /* cursor */
90 int top; /* top scroll limit */
91 int bot; /* bottom scroll limit */
92 int mode; /* terminal mode flags */
93 int esc; /* escape state flags */
94 char title[ESC_TITLE_SIZ];
98 /* Purely graphic info */
104 int w; /* window width */
105 int h; /* window height */
106 int bufw; /* pixmap width */
107 int bufh; /* pixmap height */
108 int ch; /* char height */
109 int cw; /* char width */
117 /* Drawing Context */
119 unsigned long col[256];
127 static void die(const char *errstr, ...);
128 static void draw(int);
129 static void execsh(void);
130 static void sigchld(int);
131 static void run(void);
133 static void csidump(void);
134 static void csihandle(void);
135 static void csiparse(void);
136 static void csireset(void);
138 static void tclearregion(int, int, int, int);
139 static void tcursor(int);
140 static void tdeletechar(int);
141 static void tdeleteline(int);
142 static void tinsertblank(int);
143 static void tinsertblankline(int);
144 static void tmoveto(int, int);
145 static void tnew(int, int);
146 static void tnewline(void);
147 static void tputtab(void);
148 static void tputc(char);
149 static void tputs(char*, int);
150 static void treset(void);
151 static void tresize(int, int);
152 static void tscrollup(int);
153 static void tscrolldown(int);
154 static void tsetattr(int*, int);
155 static void tsetchar(char);
156 static void tsetscroll(int, int);
158 static void ttynew(void);
159 static void ttyread(void);
160 static void ttyresize(int, int);
161 static void ttywrite(const char *, size_t);
163 static void xbell(void);
164 static void xdraws(char *, Glyph, int, int, int);
165 static void xhints(void);
166 static void xclear(int, int, int, int);
167 static void xcursor(int);
168 static void xinit(void);
169 static void xloadcols(void);
171 static void expose(XEvent *);
172 static char* kmap(KeySym);
173 static void kpress(XEvent *);
174 static void resize(XEvent *);
176 static void (*handler[LASTEvent])(XEvent *) = {
179 [ConfigureNotify] = resize
186 static CSIEscape escseq;
197 for(row = 0; row < term.row; row++) {
198 for(col = 0; col < term.col; col++) {
199 if(col == term.c.x && row == term.c.y)
202 c = term.line[row][col];
203 putchar(c.state & GLYPH_SET ? c.c : '.');
212 die(const char *errstr, ...) {
215 va_start(ap, errstr);
216 vfprintf(stderr, errstr, ap);
223 char *args[3] = {getenv("SHELL"), "-i", NULL};
224 DEFAULT(args[0], "/bin/sh"); /* if getenv() failed */
225 putenv("TERM=" TNAME);
226 execvp(args[0], args);
231 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
232 XFillRectangle(xw.dis, xw.win, dc.gc, BORDER, BORDER, xw.bufw, xw.bufh);
241 if(waitpid(pid, &stat, 0) < 0)
242 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
244 exit(WEXITSTATUS(stat));
253 /* seems to work fine on linux, openbsd and freebsd */
254 struct winsize w = {term.row, term.col, 0, 0};
255 if(openpty(&m, &s, NULL, NULL, &w) < 0)
256 die("openpty failed: %s\n", SERRNO);
258 switch(pid = fork()) {
260 die("fork failed\n");
263 setsid(); /* create a new process group */
264 dup2(s, STDIN_FILENO);
265 dup2(s, STDOUT_FILENO);
266 dup2(s, STDERR_FILENO);
267 if(ioctl(s, TIOCSCTTY, NULL) < 0)
268 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
276 signal(SIGCHLD, sigchld);
283 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
285 fprintf(stderr, "\n");
290 char buf[BUFSIZ] = {0};
293 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
294 die("Couldn't read from shell: %s\n", SERRNO);
300 ttywrite(const char *s, size_t n) {
301 if(write(cmdfd, s, n) == -1)
302 die("write error on tty: %s\n", SERRNO);
306 ttyresize(int x, int y) {
311 w.ws_xpixel = w.ws_ypixel = 0;
312 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
313 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
320 if(mode == CURSOR_SAVE)
322 else if(mode == CURSOR_LOAD)
323 term.c = c, tmoveto(c.x, c.y);
332 }, .x = 0, .y = 0, .hide = 0};
334 term.top = 0, term.bot = term.row - 1;
335 term.mode = MODE_WRAP;
336 tclearregion(0, 0, term.col-1, term.row-1);
340 tnew(int col, int row) {
341 /* set screen size */
342 term.row = row, term.col = col;
343 term.line = malloc(term.row * sizeof(Line));
344 for(row = 0 ; row < term.row; row++)
345 term.line[row] = malloc(term.col * sizeof(Glyph));
351 tscrolldown (int n) {
355 LIMIT(n, 0, term.bot-term.top+1);
357 for(i = 0; i < n; i++)
358 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
360 for(i = term.bot; i >= term.top+n; i--) {
362 term.line[i] = term.line[i-n];
363 term.line[i-n] = temp;
371 LIMIT(n, 0, term.bot-term.top+1);
373 for(i = 0; i < n; i++)
374 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
376 for(i = term.top; i <= term.bot-n; i++) {
378 term.line[i] = term.line[i+n];
379 term.line[i+n] = temp;
385 int y = term.c.y + 1;
387 tscrollup(1), y = term.bot;
394 char *p = escseq.buf;
398 escseq.priv = 1, p++;
400 while(p < escseq.buf+escseq.len) {
402 escseq.arg[escseq.narg] *= 10;
403 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
405 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
416 tmoveto(int x, int y) {
417 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
418 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
423 term.line[term.c.y][term.c.x] = term.c.attr;
424 term.line[term.c.y][term.c.x].c = c;
425 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
429 tclearregion(int x1, int y1, int x2, int y2) {
433 temp = x1, x1 = x2, x2 = temp;
435 temp = y1, y1 = y2, y2 = temp;
437 LIMIT(x1, 0, term.col-1);
438 LIMIT(x2, 0, term.col-1);
439 LIMIT(y1, 0, term.row-1);
440 LIMIT(y2, 0, term.row-1);
442 for(y = y1; y <= y2; y++)
443 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
448 int src = term.c.x + n;
450 int size = term.col - src;
452 if(src >= term.col) {
453 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
456 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
457 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
461 tinsertblank(int n) {
464 int size = term.col - dst;
466 if(dst >= term.col) {
467 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
470 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
471 tclearregion(src, term.c.y, dst - 1, term.c.y);
475 tinsertblankline(int n) {
480 if(term.c.y > term.bot)
482 else if(term.c.y < term.top)
484 if(term.c.y + n >= bot) {
485 tclearregion(0, term.c.y, term.col-1, bot);
488 for(i = bot; i >= term.c.y+n; i--) {
489 /* swap deleted line <-> blanked line */
490 blank = term.line[i];
491 term.line[i] = term.line[i-n];
492 term.line[i-n] = blank;
494 memset(blank, 0, term.col * sizeof(Glyph));
504 if(term.c.y > term.bot)
506 else if(term.c.y < term.top)
508 if(term.c.y + n >= bot) {
509 tclearregion(0, term.c.y, term.col-1, bot);
512 for(i = term.c.y; i <= bot-n; i++) {
513 /* swap deleted line <-> blanked line */
514 blank = term.line[i];
515 term.line[i] = term.line[i+n];
516 term.line[i+n] = blank;
518 memset(blank, 0, term.col * sizeof(Glyph));
523 tsetattr(int *attr, int l) {
526 for(i = 0; i < l; i++) {
529 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
530 term.c.attr.fg = DefaultFG;
531 term.c.attr.bg = DefaultBG;
534 term.c.attr.mode |= ATTR_BOLD;
537 term.c.attr.mode |= ATTR_UNDERLINE;
540 term.c.attr.mode |= ATTR_REVERSE;
543 term.c.attr.mode &= ~ATTR_BOLD;
546 term.c.attr.mode &= ~ATTR_UNDERLINE;
549 term.c.attr.mode &= ~ATTR_REVERSE;
552 if (i + 2 < l && attr[i + 1] == 5) {
554 if (BETWEEN(attr[i], 0, 255))
555 term.c.attr.fg = attr[i];
557 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
560 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
563 term.c.attr.fg = DefaultFG;
566 if (i + 2 < l && attr[i + 1] == 5) {
568 if (BETWEEN(attr[i], 0, 255))
569 term.c.attr.bg = attr[i];
571 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
574 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
577 term.c.attr.bg = DefaultBG;
580 if(BETWEEN(attr[i], 30, 37))
581 term.c.attr.fg = attr[i] - 30;
582 else if(BETWEEN(attr[i], 40, 47))
583 term.c.attr.bg = attr[i] - 40;
584 else if(BETWEEN(attr[i], 90, 97))
585 term.c.attr.fg = attr[i] - 90 + 8;
586 else if(BETWEEN(attr[i], 100, 107))
587 term.c.attr.fg = attr[i] - 100 + 8;
589 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
596 tsetscroll(int t, int b) {
599 LIMIT(t, 0, term.row-1);
600 LIMIT(b, 0, term.row-1);
612 switch(escseq.mode) {
615 printf("erresc: unknown csi ");
619 case '@': /* ICH -- Insert <n> blank char */
620 DEFAULT(escseq.arg[0], 1);
621 tinsertblank(escseq.arg[0]);
623 case 'A': /* CUU -- Cursor <n> Up */
625 DEFAULT(escseq.arg[0], 1);
626 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
628 case 'B': /* CUD -- Cursor <n> Down */
629 DEFAULT(escseq.arg[0], 1);
630 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
632 case 'C': /* CUF -- Cursor <n> Forward */
634 DEFAULT(escseq.arg[0], 1);
635 tmoveto(term.c.x+escseq.arg[0], term.c.y);
637 case 'D': /* CUB -- Cursor <n> Backward */
638 DEFAULT(escseq.arg[0], 1);
639 tmoveto(term.c.x-escseq.arg[0], term.c.y);
641 case 'E': /* CNL -- Cursor <n> Down and first col */
642 DEFAULT(escseq.arg[0], 1);
643 tmoveto(0, term.c.y+escseq.arg[0]);
645 case 'F': /* CPL -- Cursor <n> Up and first col */
646 DEFAULT(escseq.arg[0], 1);
647 tmoveto(0, term.c.y-escseq.arg[0]);
649 case 'G': /* CHA -- Move to <col> */
650 case '`': /* XXX: HPA -- same? */
651 DEFAULT(escseq.arg[0], 1);
652 tmoveto(escseq.arg[0]-1, term.c.y);
654 case 'H': /* CUP -- Move to <row> <col> */
655 case 'f': /* XXX: HVP -- same? */
656 DEFAULT(escseq.arg[0], 1);
657 DEFAULT(escseq.arg[1], 1);
658 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
660 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
661 case 'J': /* ED -- Clear screen */
662 switch(escseq.arg[0]) {
664 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
667 tclearregion(0, 0, term.c.x, term.c.y);
670 tclearregion(0, 0, term.col-1, term.row-1);
672 case 3: /* XXX: erase saved lines (xterm) */
677 case 'K': /* EL -- Clear line */
678 switch(escseq.arg[0]) {
680 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
683 tclearregion(0, term.c.y, term.c.x, term.c.y);
686 tclearregion(0, term.c.y, term.col-1, term.c.y);
690 case 'S': /* SU -- Scroll <n> line up */
691 DEFAULT(escseq.arg[0], 1);
692 tscrollup(escseq.arg[0]);
694 case 'T': /* SD -- Scroll <n> line down */
695 DEFAULT(escseq.arg[0], 1);
696 tscrolldown(escseq.arg[0]);
698 case 'L': /* IL -- Insert <n> blank lines */
699 DEFAULT(escseq.arg[0], 1);
700 tinsertblankline(escseq.arg[0]);
702 case 'l': /* RM -- Reset Mode */
704 switch(escseq.arg[0]) {
706 term.mode &= ~MODE_APPKEYPAD;
709 term.mode &= ~MODE_WRAP;
711 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
716 case 1048: /* XXX: no alt. screen to erase/save */
718 tcursor(CURSOR_LOAD);
719 tclearregion(0, 0, term.col-1, term.row-1);
725 switch(escseq.arg[0]) {
727 term.mode &= ~MODE_INSERT;
734 case 'M': /* DL -- Delete <n> lines */
735 DEFAULT(escseq.arg[0], 1);
736 tdeleteline(escseq.arg[0]);
738 case 'X': /* ECH -- Erase <n> char */
739 DEFAULT(escseq.arg[0], 1);
740 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
742 case 'P': /* DCH -- Delete <n> char */
743 DEFAULT(escseq.arg[0], 1);
744 tdeletechar(escseq.arg[0]);
746 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
747 case 'd': /* VPA -- Move to <row> */
748 DEFAULT(escseq.arg[0], 1);
749 tmoveto(term.c.x, escseq.arg[0]-1);
751 case 'h': /* SM -- Set terminal mode */
753 switch(escseq.arg[0]) {
755 term.mode |= MODE_APPKEYPAD;
758 term.mode |= MODE_WRAP;
760 case 12: /* att610 -- Start blinking cursor (IGNORED) */
766 case 1049: /* XXX: no alt. screen to erase/save */
767 tcursor(CURSOR_SAVE);
768 tclearregion(0, 0, term.col-1, term.row-1);
770 default: goto unknown;
773 switch(escseq.arg[0]) {
775 term.mode |= MODE_INSERT;
777 default: goto unknown;
781 case 'm': /* SGR -- Terminal attribute (color) */
782 tsetattr(escseq.arg, escseq.narg);
784 case 'r': /* DECSTBM -- Set Scrolling Region */
788 DEFAULT(escseq.arg[0], 1);
789 DEFAULT(escseq.arg[1], term.row);
790 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
793 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
794 tcursor(CURSOR_SAVE);
796 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
797 tcursor(CURSOR_LOAD);
805 printf("ESC [ %s", escseq.priv ? "? " : "");
807 for(i = 0; i < escseq.narg; i++)
808 printf("%d ", escseq.arg[i]);
810 putchar(escseq.mode);
816 memset(&escseq, 0, sizeof(escseq));
821 int space = TAB - term.c.x % TAB;
822 tmoveto(term.c.x + space, term.c.y);
827 if(term.esc & ESC_START) {
828 if(term.esc & ESC_CSI) {
829 escseq.buf[escseq.len++] = c;
830 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
832 csiparse(), csihandle();
834 } else if(term.esc & ESC_OSC) {
837 term.esc = ESC_START | ESC_TITLE;
839 } else if(term.esc & ESC_TITLE) {
840 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
842 term.title[term.titlelen] = '\0';
843 XStoreName(xw.dis, xw.win, term.title);
845 term.title[term.titlelen++] = c;
847 } else if(term.esc & ESC_ALTCHARSET) {
849 case '0': /* Line drawing crap */
850 term.c.attr.mode |= ATTR_GFX;
852 case 'B': /* Back to regular text */
853 term.c.attr.mode &= ~ATTR_GFX;
856 printf("esc unhandled charset: ESC ( %c\n", c);
868 term.esc |= ESC_ALTCHARSET;
871 tmoveto(term.c.x, term.c.y-1);
875 tmoveto(term.c.x, term.c.y+1);
879 tmoveto(term.c.x+1, term.c.y);
882 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
883 tmoveto(term.c.x-1, term.c.y);
886 case 'E': /* NEL -- Next line */
890 case 'M': /* RI -- Reverse index */
891 if(term.c.y == term.top)
894 tmoveto(term.c.x, term.c.y-1);
897 case 'c': /* RIS -- Reset to inital state */
901 case '=': /* DECPAM */
902 term.mode |= MODE_APPKEYPAD;
905 case '>': /* DECPNM */
906 term.mode &= ~MODE_APPKEYPAD;
910 tcursor(CURSOR_SAVE);
914 tcursor(CURSOR_LOAD);
918 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
928 tmoveto(term.c.x-1, term.c.y);
931 tmoveto(0, term.c.y);
941 term.esc = ESC_START;
945 if(term.c.x+1 < term.col) {
946 tmoveto(term.c.x+1, term.c.y);
947 } else if(IS_SET(MODE_WRAP))
955 tputs(char *s, int len) {
956 for(; len > 0; len--)
961 tresize(int col, int row) {
963 int minrow = MIN(row, term.row);
964 int mincol = MIN(col, term.col);
966 if(col < 1 || row < 1)
969 /* free uneeded rows */
970 for(i = row; i < term.row; i++)
973 /* resize to new height */
974 term.line = realloc(term.line, row * sizeof(Line));
976 /* resize each row to new width, zero-pad if needed */
977 for(i = 0; i < minrow; i++) {
978 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
979 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
982 /* allocate any new rows */
983 for(/* i == minrow */; i < row; i++)
984 term.line[i] = calloc(col, sizeof(Glyph));
986 LIMIT(term.c.x, 0, col-1);
987 LIMIT(term.c.y, 0, row-1);
988 LIMIT(term.top, 0, row-1);
989 LIMIT(term.bot, 0, row-1);
992 term.col = col, term.row = row;
999 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1000 unsigned long white = WhitePixel(xw.dis, xw.scr);
1002 for(i = 0; i < 16; i++) {
1003 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1005 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1007 dc.col[i] = color.pixel;
1010 /* same colors as xterm */
1011 for(r = 0; r < 6; r++)
1012 for(g = 0; g < 6; g++)
1013 for(b = 0; b < 6; b++) {
1014 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1015 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1016 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1017 if (!XAllocColor(xw.dis, cmap, &color)) {
1019 fprintf(stderr, "Could not allocate color %d\n", i);
1021 dc.col[i] = color.pixel;
1025 for(r = 0; r < 24; r++, i++) {
1026 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1027 if (!XAllocColor(xw.dis, cmap, &color)) {
1029 fprintf(stderr, "Could not allocate color %d\n", i);
1031 dc.col[i] = color.pixel;
1036 xclear(int x1, int y1, int x2, int y2) {
1037 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1038 XFillRectangle(xw.dis, xw.buf, dc.gc,
1039 x1 * xw.cw, y1 * xw.ch,
1040 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1046 XClassHint class = {TNAME, TNAME};
1047 XWMHints wm = {.flags = InputHint, .input = 1};
1049 .flags = PSize | PResizeInc | PBaseSize,
1052 .height_inc = xw.ch,
1054 .base_height = 2*BORDER,
1055 .base_width = 2*BORDER,
1057 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1062 if(!(xw.dis = XOpenDisplay(NULL)))
1063 die("Can't open display\n");
1064 xw.scr = XDefaultScreen(xw.dis);
1067 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1068 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1070 /* XXX: Assuming same size for bold font */
1071 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1072 xw.ch = dc.font->ascent + dc.font->descent;
1077 term.c.attr.fg = DefaultFG;
1078 term.c.attr.bg = DefaultBG;
1079 term.c.attr.mode = ATTR_NULL;
1081 xw.h = term.row * xw.ch + 2*BORDER;
1082 xw.w = term.col * xw.cw + 2*BORDER;
1083 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1087 xw.bufw = xw.w - 2*BORDER;
1088 xw.bufh = xw.h - 2*BORDER;
1089 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1091 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1092 XMapWindow(xw.dis, xw.win);
1094 XStoreName(xw.dis, xw.win, "st");
1099 xdraws(char *s, Glyph base, int x, int y, int len) {
1100 unsigned long xfg, xbg;
1101 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1104 if(base.mode & ATTR_REVERSE)
1105 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1107 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1109 XSetBackground(xw.dis, dc.gc, xbg);
1110 XSetForeground(xw.dis, dc.gc, xfg);
1112 if(base.mode & ATTR_GFX)
1113 for(i = 0; i < len; i++)
1114 s[i] = gfx[(int)s[i]];
1116 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1117 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1119 if(base.mode & ATTR_UNDERLINE)
1120 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1125 static int oldx = 0;
1126 static int oldy = 0;
1127 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1129 LIMIT(oldx, 0, term.col-1);
1130 LIMIT(oldy, 0, term.row-1);
1132 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1133 g.c = term.line[term.c.y][term.c.x].c;
1135 /* remove the old cursor */
1136 if(term.line[oldy][oldx].state & GLYPH_SET)
1137 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1139 xclear(oldx, oldy, oldx, oldy);
1141 /* draw the new one */
1142 if(mode == CURSOR_DRAW) {
1143 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1144 oldx = term.c.x, oldy = term.c.y;
1149 /* basic drawing routines */
1151 xdrawc(int x, int y, Glyph g) {
1152 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1153 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1154 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1155 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1156 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1163 xclear(0, 0, term.col-1, term.row-1);
1164 for(y = 0; y < term.row; y++)
1165 for(x = 0; x < term.col; x++)
1166 if(term.line[y][x].state & GLYPH_SET)
1167 xdrawc(x, y, term.line[y][x]);
1170 xcursor(CURSOR_DRAW);
1171 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1176 /* optimized drawing routine */
1178 draw(int redraw_all) {
1181 char buf[DRAW_BUF_SIZ];
1183 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1184 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
1185 for(y = 0; y < term.row; y++) {
1186 base = term.line[y][0];
1188 for(x = 0; x < term.col; x++) {
1189 new = term.line[y][x];
1190 if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
1193 xdraws(buf, base, ox, y, i);
1200 xdraws(buf, base, ox, y, i);
1202 xcursor(term.c.hide ? CURSOR_HIDE : CURSOR_DRAW);
1203 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1210 expose(XEvent *ev) {
1211 draw(SCREEN_REDRAW);
1217 for(i = 0; i < LEN(key); i++)
1219 return (char*)key[i].s;
1224 kpress(XEvent *ev) {
1225 XKeyEvent *e = &ev->xkey;
1233 meta = e->state & Mod1Mask;
1234 shift = e->state & ShiftMask;
1235 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1237 if((customkey = kmap(ksym)))
1238 ttywrite(customkey, strlen(customkey));
1240 buf[sizeof(buf)-1] = '\0';
1241 if(meta && len == 1)
1242 ttywrite("\033", 1);
1250 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1255 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1258 fprintf(stderr, "errkey: %d\n", (int)ksym);
1267 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1270 xw.w = e->xconfigure.width;
1271 xw.h = e->xconfigure.height;
1272 xw.bufw = xw.w - 2*BORDER;
1273 xw.bufh = xw.h - 2*BORDER;
1274 col = xw.bufw / xw.cw;
1275 row = xw.bufh / xw.ch;
1277 ttyresize(col, row);
1278 XFreePixmap(xw.dis, xw.buf);
1279 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1280 draw(SCREEN_REDRAW);
1287 int xfd = XConnectionNumber(xw.dis);
1290 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1291 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1295 FD_SET(cmdfd, &rfd);
1297 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1300 die("select failed: %s\n", SERRNO);
1302 if(FD_ISSET(cmdfd, &rfd)) {
1304 draw(SCREEN_UPDATE);
1306 while(XPending(xw.dis)) {
1307 XNextEvent(xw.dis, &ev);
1308 if(handler[ev.type])
1309 (handler[ev.type])(&ev);
1315 main(int argc, char *argv[]) {
1316 if(argc == 2 && !strncmp("-v", argv[1], 3))
1317 die("st-" VERSION ", (c) 2010 st engineers\n");
1319 die("usage: st [-v]\n");
1320 setlocale(LC_CTYPE, "");