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>
24 #define ESC_TITLE_SIZ 256
25 #define ESC_BUF_SIZ 256
26 #define ESC_ARG_SIZ 16
27 #define DRAW_BUF_SIZ 1024
29 #define SERRNO strerror(errno)
30 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 #define MAX(a, b) ((a) < (b) ? (b) : (a))
32 #define LEN(a) (sizeof(a) / sizeof(a[0]))
33 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
34 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
35 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
36 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
38 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
39 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
40 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_HIDE, CURSOR_DRAW,
41 CURSOR_SAVE, CURSOR_LOAD };
42 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
43 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4 };
44 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
45 enum { SCREEN_UPDATE, SCREEN_REDRAW };
48 char c; /* character code */
49 char mode; /* attribute flags */
50 int fg; /* foreground */
51 int bg; /* background */
52 char state; /* state flags */
58 Glyph attr; /* current char attributes */
63 /* CSI Escape sequence structs */
64 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
66 char buf[ESC_BUF_SIZ]; /* raw string */
67 int len; /* raw string length */
70 int narg; /* nb of args */
74 /* Internal representation of the screen */
78 Line* line; /* screen */
79 TCursor c; /* cursor */
81 int top; /* top scroll limit */
82 int bot; /* bottom scroll limit */
83 int mode; /* terminal mode flags */
84 int esc; /* escape state flags */
85 char title[ESC_TITLE_SIZ];
89 /* Purely graphic info */
95 int w; /* window width */
96 int h; /* window height */
97 int bufw; /* pixmap width */
98 int bufh; /* pixmap height */
99 int ch; /* char height */
100 int cw; /* char width */
108 /* Drawing Context */
110 unsigned long col[256];
118 static void die(const char *errstr, ...);
119 static void draw(int);
120 static void execsh(void);
121 static void sigchld(int);
122 static void run(void);
124 static void csidump(void);
125 static void csihandle(void);
126 static void csiparse(void);
127 static void csireset(void);
129 static void tclearregion(int, int, int, int);
130 static void tcursor(int);
131 static void tmovecursor(int);
132 static void tdeletechar(int);
133 static void tdeleteline(int);
134 static void tinsertblank(int);
135 static void tinsertblankline(int);
136 static void tmoveto(int, int);
137 static void tnew(int, int);
138 static void tnewline(void);
139 static void tputtab(void);
140 static void tputc(char);
141 static void tputs(char*, int);
142 static void treset(void);
143 static void tresize(int, int);
144 static void tscroll(void);
145 static void tscrollup(int);
146 static void tscrolldown(int);
147 static void tsetattr(int*, int);
148 static void tsetchar(char);
149 static void tsetscroll(int, int);
151 static void ttynew(void);
152 static void ttyread(void);
153 static void ttyresize(int, int);
154 static void ttywrite(const char *, size_t);
156 static void xbell(void);
157 static void xdraws(char *, Glyph, int, int, int);
158 static void xhints(void);
159 static void xclear(int, int, int, int);
160 static void xcursor(int);
161 static void xinit(void);
162 static void xloadcols(void);
164 static void expose(XEvent *);
165 static char* kmap(KeySym);
166 static void kpress(XEvent *);
167 static void resize(XEvent *);
169 static void (*handler[LASTEvent])(XEvent *) = {
172 [ConfigureNotify] = resize
179 static CSIEscape escseq;
190 for(row = 0; row < term.row; row++) {
191 for(col = 0; col < term.col; col++) {
192 if(col == term.c.x && row == term.c.y)
195 c = term.line[row][col];
196 putchar(c.state & GLYPH_SET ? c.c : '.');
205 die(const char *errstr, ...) {
208 va_start(ap, errstr);
209 vfprintf(stderr, errstr, ap);
216 char *args[3] = {getenv("SHELL"), "-i", NULL};
217 DEFAULT(args[0], "/bin/sh"); /* default shell if getenv() failed */
218 putenv("TERM=" TNAME);
219 execvp(args[0], args);
223 xbell(void) { /* visual bell */
224 XRectangle r = { BORDER, BORDER, xw.bufw, xw.bufh };
225 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
226 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
234 if(waitpid(pid, &stat, 0) < 0)
235 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
237 exit(WEXITSTATUS(stat));
247 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
248 die("openpt failed: %s\n", SERRNO);
250 die("grandpt failed: %s\n", SERRNO);
252 die("unlockpt failed: %s\n", SERRNO);
253 if(!(pts = ptsname(m)))
254 die("ptsname failed: %s\n", SERRNO);
255 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
256 die("Couldn't open slave: %s\n", SERRNO);
257 fcntl(s, F_SETFL, O_NDELAY);
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 TTIOCSTTY failed: %s\n", SERRNO);
274 signal(SIGCHLD, sigchld);
281 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
283 fprintf(stderr, "\n");
288 char buf[BUFSIZ] = {0};
291 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
292 die("Couldn't read from shell: %s\n", SERRNO);
298 ttywrite(const char *s, size_t n) {
299 if(write(cmdfd, s, n) == -1)
300 die("write error on tty: %s\n", SERRNO);
304 ttyresize(int x, int y) {
309 w.ws_xpixel = w.ws_ypixel = 0;
310 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
311 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
318 if(mode == CURSOR_SAVE)
320 else if(mode == CURSOR_LOAD)
321 term.c = c, tmoveto(c.x, c.y);
326 term.c.attr.mode = ATTR_NULL;
327 term.c.attr.fg = DefaultFG;
328 term.c.attr.bg = DefaultBG;
329 term.c.x = term.c.y = 0;
331 term.top = 0, term.bot = term.row - 1;
332 term.mode = MODE_WRAP;
333 tclearregion(0, 0, term.col-1, term.row-1);
337 tnew(int col, int row) { /* screen size */
338 term.row = row, term.col = col;
339 term.top = 0, term.bot = term.row - 1;
341 term.mode = MODE_WRAP;
343 term.c.attr.mode = ATTR_NULL;
344 term.c.attr.fg = DefaultFG;
345 term.c.attr.bg = DefaultBG;
346 term.c.x = term.c.y = 0;
348 /* allocate screen */
349 term.line = calloc(term.row, sizeof(Line));
350 for(row = 0 ; row < term.row; row++)
351 term.line[row] = calloc(term.col, sizeof(Glyph));
354 /* TODO: Replace with scrollup/scolldown */
357 Line temp = term.line[term.top];
360 for(i = term.top; i < term.bot; i++)
361 term.line[i] = term.line[i+1];
362 memset(temp, 0, sizeof(Glyph) * term.col);
363 term.line[term.bot] = temp;
367 tscrolldown (int n) {
371 LIMIT(n, 0, term.bot-term.top+1);
373 for(i = 0; i < n; i++)
374 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
376 for(i = term.bot; i >= term.top+n; i--) {
378 term.line[i] = term.line[i-n];
379 term.line[i-n] = temp;
387 LIMIT(n, 0, term.bot-term.top+1);
389 for(i = 0; i < n; i++)
390 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
392 for(i = term.top; i <= term.bot-n; i++) {
394 term.line[i] = term.line[i+n];
395 term.line[i+n] = temp;
401 int y = term.c.y + 1;
403 tscroll(), y = term.bot;
410 char *p = escseq.buf;
414 escseq.priv = 1, p++;
416 while(p < escseq.buf+escseq.len) {
418 escseq.arg[escseq.narg] *= 10;
419 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
421 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
432 tmoveto(int x, int y) {
433 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
434 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
438 tmovecursor(int dir) {
439 int xf = term.c.x, yf = term.c.y;
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-n, term.c.y, term.col-1, term.c.y);
503 tinsertblank(int n) {
506 int size = term.col - dst;
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 - 1, 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 if (i + 2 < l && attr[i + 1] == 5) {
596 if (BETWEEN(attr[i], 0, 255))
597 term.c.attr.fg = attr[i];
599 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
602 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
605 term.c.attr.fg = DefaultFG;
608 if (i + 2 < l && attr[i + 1] == 5) {
610 if (BETWEEN(attr[i], 0, 255))
611 term.c.attr.bg = attr[i];
613 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
616 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
619 term.c.attr.bg = DefaultBG;
622 if(BETWEEN(attr[i], 30, 37))
623 term.c.attr.fg = attr[i] - 30;
624 else if(BETWEEN(attr[i], 40, 47))
625 term.c.attr.bg = attr[i] - 40;
626 else if(BETWEEN(attr[i], 90, 97))
627 term.c.attr.fg = attr[i] - 90 + 8;
628 else if(BETWEEN(attr[i], 100, 107))
629 term.c.attr.fg = attr[i] - 100 + 8;
631 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
638 tsetscroll(int t, int b) {
641 LIMIT(t, 0, term.row-1);
642 LIMIT(b, 0, term.row-1);
654 switch(escseq.mode) {
657 printf("erresc: unknown csi ");
661 case '@': /* ICH -- Insert <n> blank char */
662 DEFAULT(escseq.arg[0], 1);
663 tinsertblank(escseq.arg[0]);
665 case 'A': /* CUU -- Cursor <n> Up */
667 DEFAULT(escseq.arg[0], 1);
668 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
670 case 'B': /* CUD -- Cursor <n> Down */
671 DEFAULT(escseq.arg[0], 1);
672 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
674 case 'C': /* CUF -- Cursor <n> Forward */
676 DEFAULT(escseq.arg[0], 1);
677 tmoveto(term.c.x+escseq.arg[0], term.c.y);
679 case 'D': /* CUB -- Cursor <n> Backward */
680 DEFAULT(escseq.arg[0], 1);
681 tmoveto(term.c.x-escseq.arg[0], term.c.y);
683 case 'E': /* CNL -- Cursor <n> Down and first col */
684 DEFAULT(escseq.arg[0], 1);
685 tmoveto(0, term.c.y+escseq.arg[0]);
687 case 'F': /* CPL -- Cursor <n> Up and first col */
688 DEFAULT(escseq.arg[0], 1);
689 tmoveto(0, term.c.y-escseq.arg[0]);
691 case 'G': /* CHA -- Move to <col> */
692 case '`': /* XXX: HPA -- same? */
693 DEFAULT(escseq.arg[0], 1);
694 tmoveto(escseq.arg[0]-1, term.c.y);
696 case 'H': /* CUP -- Move to <row> <col> */
697 case 'f': /* XXX: HVP -- same? */
698 DEFAULT(escseq.arg[0], 1);
699 DEFAULT(escseq.arg[1], 1);
700 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
702 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
703 case 'J': /* ED -- Clear screen */
704 switch(escseq.arg[0]) {
706 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
709 tclearregion(0, 0, term.c.x, term.c.y);
712 tclearregion(0, 0, term.col-1, term.row-1);
714 case 3: /* XXX: erase saved lines (xterm) */
719 case 'K': /* EL -- Clear line */
720 switch(escseq.arg[0]) {
722 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
725 tclearregion(0, term.c.y, term.c.x, term.c.y);
728 tclearregion(0, term.c.y, term.col-1, term.c.y);
732 case 'S': /* SU -- Scroll <n> line up */
733 DEFAULT(escseq.arg[0], 1);
734 tscrollup(escseq.arg[0]);
736 case 'T': /* SD -- Scroll <n> line down */
737 DEFAULT(escseq.arg[0], 1);
738 tscrolldown(escseq.arg[0]);
740 case 'L': /* IL -- Insert <n> blank lines */
741 DEFAULT(escseq.arg[0], 1);
742 tinsertblankline(escseq.arg[0]);
744 case 'l': /* RM -- Reset Mode */
746 switch(escseq.arg[0]) {
748 term.mode &= ~MODE_APPKEYPAD;
751 term.mode &= ~MODE_WRAP;
753 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
758 case 1048: /* XXX: no alt. screen to erase/save */
760 tcursor(CURSOR_LOAD);
761 tclearregion(0, 0, term.col-1, term.row-1);
767 switch(escseq.arg[0]) {
769 term.mode &= ~MODE_INSERT;
776 case 'M': /* DL -- Delete <n> lines */
777 DEFAULT(escseq.arg[0], 1);
778 tdeleteline(escseq.arg[0]);
780 case 'X': /* ECH -- Erase <n> char */
781 DEFAULT(escseq.arg[0], 1);
782 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
784 case 'P': /* DCH -- Delete <n> char */
785 DEFAULT(escseq.arg[0], 1);
786 tdeletechar(escseq.arg[0]);
788 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
789 case 'd': /* VPA -- Move to <row> */
790 DEFAULT(escseq.arg[0], 1);
791 tmoveto(term.c.x, escseq.arg[0]-1);
793 case 'h': /* SM -- Set terminal mode */
795 switch(escseq.arg[0]) {
797 term.mode |= MODE_APPKEYPAD;
800 term.mode |= MODE_WRAP;
802 case 12: /* att610 -- Start blinking cursor (IGNORED) */
808 case 1049: /* XXX: no alt. screen to erase/save */
809 tcursor(CURSOR_SAVE);
810 tclearregion(0, 0, term.col-1, term.row-1);
812 default: goto unknown;
815 switch(escseq.arg[0]) {
817 term.mode |= MODE_INSERT;
819 default: goto unknown;
823 case 'm': /* SGR -- Terminal attribute (color) */
824 tsetattr(escseq.arg, escseq.narg);
826 case 'r': /* DECSTBM -- Set Scrolling Region */
830 DEFAULT(escseq.arg[0], 1);
831 DEFAULT(escseq.arg[1], term.row);
832 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
835 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
836 tcursor(CURSOR_SAVE);
838 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
839 tcursor(CURSOR_LOAD);
847 printf("ESC [ %s", escseq.priv ? "? " : "");
849 for(i = 0; i < escseq.narg; i++)
850 printf("%d ", escseq.arg[i]);
852 putchar(escseq.mode);
858 memset(&escseq, 0, sizeof(escseq));
863 int space = TAB - term.c.x % TAB;
865 if(term.c.x + space >= term.col)
868 for(; space > 0; space--)
869 tmovecursor(CURSOR_RIGHT);
874 if(term.esc & ESC_START) {
875 if(term.esc & ESC_CSI) {
876 escseq.buf[escseq.len++] = c;
877 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
879 csiparse(), csihandle();
881 } else if(term.esc & ESC_OSC) {
884 term.esc = ESC_START | ESC_TITLE;
886 } else if(term.esc & ESC_TITLE) {
887 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
889 term.title[term.titlelen] = '\0';
890 XStoreName(xw.dis, xw.win, term.title);
892 term.title[term.titlelen++] = c;
894 } else if(term.esc & ESC_ALTCHARSET) {
896 case '0': /* Line drawing crap */
897 term.c.attr.mode |= ATTR_GFX;
899 case 'B': /* Back to regular text */
900 term.c.attr.mode &= ~ATTR_GFX;
903 printf("esc unhandled charset: ESC ( %c\n", c);
915 term.esc |= ESC_ALTCHARSET;
918 tmoveto(term.c.x, term.c.y-1);
922 tmoveto(term.c.x, term.c.y+1);
926 tmoveto(term.c.x+1, term.c.y);
929 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
930 tmoveto(term.c.x-1, term.c.y);
933 case 'E': /* NEL -- Next line */
937 case 'M': /* RI -- Reverse index */
938 if(term.c.y == term.top)
941 tmoveto(term.c.x, term.c.y-1);
944 case 'c': /* RIS -- Reset to inital state */
948 case '=': /* DECPAM */
949 term.mode |= MODE_APPKEYPAD;
952 case '>': /* DECPNM */
953 term.mode &= ~MODE_APPKEYPAD;
957 tcursor(CURSOR_SAVE);
961 tcursor(CURSOR_LOAD);
965 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
975 tmovecursor(CURSOR_LEFT);
978 tmoveto(0, term.c.y);
988 term.esc = ESC_START;
992 tmovecursor(CURSOR_RIGHT);
999 tputs(char *s, int len) {
1000 for(; len > 0; len--)
1005 tresize(int col, int row) {
1008 int minrow = MIN(row, term.row);
1009 int mincol = MIN(col, term.col);
1011 if(col < 1 || row < 1)
1014 line = calloc(row, sizeof(Line));
1015 for(i = 0 ; i < row; i++)
1016 line[i] = calloc(col, sizeof(Glyph));
1018 for(i = 0 ; i < minrow; i++)
1019 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
1021 for(i = 0; i < term.row; i++)
1025 LIMIT(term.c.x, 0, col-1);
1026 LIMIT(term.c.y, 0, row-1);
1027 LIMIT(term.top, 0, row-1);
1028 LIMIT(term.bot, 0, row-1);
1032 term.col = col, term.row = row;
1039 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1040 unsigned long white = WhitePixel(xw.dis, xw.scr);
1042 for(i = 0; i < 16; i++) {
1043 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1045 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1047 dc.col[i] = color.pixel;
1050 /* same colors as xterm */
1051 for(r = 0; r < 6; r++)
1052 for(g = 0; g < 6; g++)
1053 for(b = 0; b < 6; b++) {
1054 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1055 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1056 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1057 if (!XAllocColor(xw.dis, cmap, &color)) {
1059 fprintf(stderr, "Could not allocate color %d\n", i);
1061 dc.col[i] = color.pixel;
1065 for(r = 0; r < 24; r++, i++) {
1066 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1067 if (!XAllocColor(xw.dis, cmap, &color)) {
1069 fprintf(stderr, "Could not allocate color %d\n", i);
1071 dc.col[i] = color.pixel;
1076 xclear(int x1, int y1, int x2, int y2) {
1077 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1078 XFillRectangle(xw.dis, xw.buf, dc.gc,
1079 x1 * xw.cw, y1 * xw.ch,
1080 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1086 XClassHint class = {TNAME, TNAME};
1087 XWMHints wm = {.flags = InputHint, .input = 1};
1089 .flags = PSize | PResizeInc | PBaseSize,
1092 .height_inc = xw.ch,
1094 .base_height = 2*BORDER,
1095 .base_width = 2*BORDER,
1097 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1102 xw.dis = XOpenDisplay(NULL);
1103 xw.scr = XDefaultScreen(xw.dis);
1105 die("Can't open display\n");
1108 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1109 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1111 /* XXX: Assuming same size for bold font */
1112 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1113 xw.ch = dc.font->ascent + dc.font->descent;
1118 term.c.attr.fg = DefaultFG;
1119 term.c.attr.bg = DefaultBG;
1120 term.c.attr.mode = ATTR_NULL;
1122 xw.h = term.row * xw.ch + 2*BORDER;
1123 xw.w = term.col * xw.cw + 2*BORDER;
1124 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1128 xw.bufw = xw.w - 2*BORDER;
1129 xw.bufh = xw.h - 2*BORDER;
1130 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1132 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1133 XMapWindow(xw.dis, xw.win);
1135 XStoreName(xw.dis, xw.win, "st");
1140 xdraws(char *s, Glyph base, int x, int y, int len) {
1141 unsigned long xfg, xbg;
1142 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1145 if(base.mode & ATTR_REVERSE)
1146 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1148 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1150 XSetBackground(xw.dis, dc.gc, xbg);
1151 XSetForeground(xw.dis, dc.gc, xfg);
1153 if(base.mode & ATTR_GFX)
1154 for(i = 0; i < len; i++)
1155 s[i] = gfx[(int)s[i]];
1157 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1158 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1160 if(base.mode & ATTR_UNDERLINE)
1161 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1166 static int oldx = 0;
1167 static int oldy = 0;
1168 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1170 LIMIT(oldx, 0, term.col-1);
1171 LIMIT(oldy, 0, term.row-1);
1173 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1174 g.c = term.line[term.c.y][term.c.x].c;
1176 /* remove the old cursor */
1177 if(term.line[oldy][oldx].state & GLYPH_SET)
1178 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1180 xclear(oldx, oldy, oldx, oldy);
1182 /* draw the new one */
1183 if(mode == CURSOR_DRAW) {
1184 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1185 oldx = term.c.x, oldy = term.c.y;
1190 /* basic drawing routines */
1192 xdrawc(int x, int y, Glyph g) {
1193 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1194 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1195 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1196 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1197 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1204 xclear(0, 0, term.col-1, term.row-1);
1205 for(y = 0; y < term.row; y++)
1206 for(x = 0; x < term.col; x++)
1207 if(term.line[y][x].state & GLYPH_SET)
1208 xdrawc(x, y, term.line[y][x]);
1211 xcursor(CURSOR_DRAW);
1212 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1217 /* optimized drawing routine */
1219 draw(int redraw_all) {
1222 char buf[DRAW_BUF_SIZ];
1224 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1225 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
1226 for(y = 0; y < term.row; y++) {
1227 base = term.line[y][0];
1229 for(x = 0; x < term.col; x++) {
1230 new = term.line[y][x];
1231 if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
1234 xdraws(buf, base, ox, y, i);
1241 xdraws(buf, base, ox, y, i);
1243 xcursor(term.hidec ? CURSOR_HIDE : CURSOR_DRAW);
1244 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1251 expose(XEvent *ev) {
1252 draw(SCREEN_REDRAW);
1258 for(i = 0; i < LEN(key); i++)
1260 return (char*)key[i].s;
1265 kpress(XEvent *ev) {
1266 XKeyEvent *e = &ev->xkey;
1274 meta = e->state & Mod1Mask;
1275 shift = e->state & ShiftMask;
1276 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1278 if((customkey = kmap(ksym)))
1279 ttywrite(customkey, strlen(customkey));
1281 buf[sizeof(buf)-1] = '\0';
1282 if(meta && len == 1)
1283 ttywrite("\033", 1);
1291 sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
1296 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1299 fprintf(stderr, "errkey: %d\n", (int)ksym);
1308 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1311 xw.w = e->xconfigure.width;
1312 xw.h = e->xconfigure.height;
1313 xw.bufw = xw.w - 2*BORDER;
1314 xw.bufh = xw.h - 2*BORDER;
1315 col = xw.bufw / xw.cw;
1316 row = xw.bufh / xw.ch;
1318 ttyresize(col, row);
1319 XFreePixmap(xw.dis, xw.buf);
1320 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1321 draw(SCREEN_REDRAW);
1328 int xfd = XConnectionNumber(xw.dis);
1331 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1332 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1336 FD_SET(cmdfd, &rfd);
1338 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
1341 die("select failed: %s\n", SERRNO);
1343 if(FD_ISSET(cmdfd, &rfd)) {
1345 draw(SCREEN_UPDATE);
1347 while(XPending(xw.dis)) {
1348 XNextEvent(xw.dis, &ev);
1349 if(handler[ev.type])
1350 (handler[ev.type])(&ev);
1356 main(int argc, char *argv[]) {
1357 if(argc == 2 && !strncmp("-v", argv[1], 3))
1358 die("st-" VERSION ", © 2009 st engineers\n");
1360 die("usage: st [-v]\n");
1361 setlocale(LC_CTYPE, "");