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)
37 #define IS_SET(flag) (term.mode & flag)
39 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
40 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
41 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_HIDE, CURSOR_DRAW,
42 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 bufw; /* pixmap width */
99 int bufh; /* pixmap height */
100 int ch; /* char height */
101 int cw; /* char width */
109 /* Drawing Context */
111 unsigned long col[256];
119 static void die(const char *errstr, ...);
120 static void draw(int);
121 static void execsh(void);
122 static void sigchld(int);
123 static void run(void);
125 static void csidump(void);
126 static void csihandle(void);
127 static void csiparse(void);
128 static void csireset(void);
130 static void tclearregion(int, int, int, int);
131 static void tcursor(int);
132 static void twrapcursor(void);
133 static void tdeletechar(int);
134 static void tdeleteline(int);
135 static void tinsertblank(int);
136 static void tinsertblankline(int);
137 static void tmoveto(int, int);
138 static void tnew(int, int);
139 static void tnewline(void);
140 static void tputtab(void);
141 static void tputc(char);
142 static void tputs(char*, int);
143 static void treset(void);
144 static void tresize(int, int);
145 static void tscroll(void);
146 static void tscrollup(int);
147 static void tscrolldown(int);
148 static void tsetattr(int*, int);
149 static void tsetchar(char);
150 static void tsetscroll(int, int);
152 static void ttynew(void);
153 static void ttyread(void);
154 static void ttyresize(int, int);
155 static void ttywrite(const char *, size_t);
157 static void xbell(void);
158 static void xdraws(char *, Glyph, int, int, int);
159 static void xhints(void);
160 static void xclear(int, int, int, int);
161 static void xcursor(int);
162 static void xinit(void);
163 static void xloadcols(void);
165 static void expose(XEvent *);
166 static char* kmap(KeySym);
167 static void kpress(XEvent *);
168 static void resize(XEvent *);
170 static void (*handler[LASTEvent])(XEvent *) = {
173 [ConfigureNotify] = resize
180 static CSIEscape escseq;
191 for(row = 0; row < term.row; row++) {
192 for(col = 0; col < term.col; col++) {
193 if(col == term.c.x && row == term.c.y)
196 c = term.line[row][col];
197 putchar(c.state & GLYPH_SET ? c.c : '.');
206 die(const char *errstr, ...) {
209 va_start(ap, errstr);
210 vfprintf(stderr, errstr, ap);
217 char *args[3] = {getenv("SHELL"), "-i", NULL};
218 DEFAULT(args[0], "/bin/sh"); /* default shell if getenv() failed */
219 putenv("TERM=" TNAME);
220 execvp(args[0], args);
224 xbell(void) { /* visual bell */
225 XRectangle r = { BORDER, BORDER, xw.bufw, xw.bufh };
226 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
227 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
235 if(waitpid(pid, &stat, 0) < 0)
236 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
238 exit(WEXITSTATUS(stat));
248 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
249 die("openpt failed: %s\n", SERRNO);
251 die("grantpt failed: %s\n", SERRNO);
253 die("unlockpt failed: %s\n", SERRNO);
254 if(!(pts = ptsname(m)))
255 die("ptsname failed: %s\n", SERRNO);
256 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
257 die("Couldn't open slave: %s\n", SERRNO);
258 fcntl(s, F_SETFL, O_NDELAY);
259 switch(pid = fork()) {
261 die("fork failed\n");
264 setsid(); /* create a new process group */
265 dup2(s, STDIN_FILENO);
266 dup2(s, STDOUT_FILENO);
267 dup2(s, STDERR_FILENO);
268 if(ioctl(s, TIOCSCTTY, NULL) < 0)
269 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
277 signal(SIGCHLD, sigchld);
284 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
286 fprintf(stderr, "\n");
291 char buf[BUFSIZ] = {0};
294 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
295 die("Couldn't read from shell: %s\n", SERRNO);
301 ttywrite(const char *s, size_t n) {
302 if(write(cmdfd, s, n) == -1)
303 die("write error on tty: %s\n", SERRNO);
307 ttyresize(int x, int y) {
312 w.ws_xpixel = w.ws_ypixel = 0;
313 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
314 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
321 if(mode == CURSOR_SAVE)
323 else if(mode == CURSOR_LOAD)
324 term.c = c, tmoveto(c.x, c.y);
329 term.c.attr.mode = ATTR_NULL;
330 term.c.attr.fg = DefaultFG;
331 term.c.attr.bg = DefaultBG;
332 term.c.x = term.c.y = 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) { /* screen size */
341 term.row = row, term.col = col;
342 term.top = 0, term.bot = term.row - 1;
344 term.mode = MODE_WRAP;
346 term.c.attr.mode = ATTR_NULL;
347 term.c.attr.fg = DefaultFG;
348 term.c.attr.bg = DefaultBG;
349 term.c.x = term.c.y = 0;
351 /* allocate screen */
352 term.line = calloc(term.row, sizeof(Line));
353 for(row = 0 ; row < term.row; row++)
354 term.line[row] = calloc(term.col, sizeof(Glyph));
357 /* TODO: Replace with scrollup/scolldown */
360 Line temp = term.line[term.top];
363 for(i = term.top; i < term.bot; i++)
364 term.line[i] = term.line[i+1];
365 memset(temp, 0, sizeof(Glyph) * term.col);
366 term.line[term.bot] = temp;
370 tscrolldown (int n) {
374 LIMIT(n, 0, term.bot-term.top+1);
376 for(i = 0; i < n; i++)
377 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
379 for(i = term.bot; i >= term.top+n; i--) {
381 term.line[i] = term.line[i-n];
382 term.line[i-n] = temp;
390 LIMIT(n, 0, term.bot-term.top+1);
392 for(i = 0; i < n; i++)
393 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
395 for(i = term.top; i <= term.bot-n; i++) {
397 term.line[i] = term.line[i+n];
398 term.line[i+n] = temp;
404 int y = term.c.y + 1;
406 tscroll(), y = term.bot;
413 char *p = escseq.buf;
417 escseq.priv = 1, p++;
419 while(p < escseq.buf+escseq.len) {
421 escseq.arg[escseq.narg] *= 10;
422 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
424 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
435 tmoveto(int x, int y) {
436 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
437 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
444 tmoveto(0, term.bot);
452 term.line[term.c.y][term.c.x] = term.c.attr;
453 term.line[term.c.y][term.c.x].c = c;
454 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
458 tclearregion(int x1, int y1, int x2, int y2) {
462 temp = x1, x1 = x2, x2 = temp;
464 temp = y1, y1 = y2, y2 = temp;
466 LIMIT(x1, 0, term.col-1);
467 LIMIT(x2, 0, term.col-1);
468 LIMIT(y1, 0, term.row-1);
469 LIMIT(y2, 0, term.row-1);
471 for(y = y1; y <= y2; y++)
472 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
477 int src = term.c.x + n;
479 int size = term.col - src;
481 if(src >= term.col) {
482 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
485 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
486 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
490 tinsertblank(int n) {
493 int size = term.col - dst;
495 if(dst >= term.col) {
496 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
499 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
500 tclearregion(src, term.c.y, dst - 1, term.c.y);
504 tinsertblankline(int n) {
509 if(term.c.y > term.bot)
511 else if(term.c.y < term.top)
513 if(term.c.y + n >= bot) {
514 tclearregion(0, term.c.y, term.col-1, bot);
517 for(i = bot; i >= term.c.y+n; i--) {
518 /* swap deleted line <-> blanked line */
519 blank = term.line[i];
520 term.line[i] = term.line[i-n];
521 term.line[i-n] = blank;
523 memset(blank, 0, term.col * sizeof(Glyph));
533 if(term.c.y > term.bot)
535 else if(term.c.y < term.top)
537 if(term.c.y + n >= bot) {
538 tclearregion(0, term.c.y, term.col-1, bot);
541 for(i = term.c.y; i <= bot-n; i++) {
542 /* swap deleted line <-> blanked line */
543 blank = term.line[i];
544 term.line[i] = term.line[i+n];
545 term.line[i+n] = blank;
547 memset(blank, 0, term.col * sizeof(Glyph));
552 tsetattr(int *attr, int l) {
555 for(i = 0; i < l; i++) {
558 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
559 term.c.attr.fg = DefaultFG;
560 term.c.attr.bg = DefaultBG;
563 term.c.attr.mode |= ATTR_BOLD;
566 term.c.attr.mode |= ATTR_UNDERLINE;
569 term.c.attr.mode |= ATTR_REVERSE;
572 term.c.attr.mode &= ~ATTR_BOLD;
575 term.c.attr.mode &= ~ATTR_UNDERLINE;
578 term.c.attr.mode &= ~ATTR_REVERSE;
581 if (i + 2 < l && attr[i + 1] == 5) {
583 if (BETWEEN(attr[i], 0, 255))
584 term.c.attr.fg = attr[i];
586 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
589 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
592 term.c.attr.fg = DefaultFG;
595 if (i + 2 < l && attr[i + 1] == 5) {
597 if (BETWEEN(attr[i], 0, 255))
598 term.c.attr.bg = attr[i];
600 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
603 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
606 term.c.attr.bg = DefaultBG;
609 if(BETWEEN(attr[i], 30, 37))
610 term.c.attr.fg = attr[i] - 30;
611 else if(BETWEEN(attr[i], 40, 47))
612 term.c.attr.bg = attr[i] - 40;
613 else if(BETWEEN(attr[i], 90, 97))
614 term.c.attr.fg = attr[i] - 90 + 8;
615 else if(BETWEEN(attr[i], 100, 107))
616 term.c.attr.fg = attr[i] - 100 + 8;
618 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
625 tsetscroll(int t, int b) {
628 LIMIT(t, 0, term.row-1);
629 LIMIT(b, 0, term.row-1);
641 switch(escseq.mode) {
644 printf("erresc: unknown csi ");
648 case '@': /* ICH -- Insert <n> blank char */
649 DEFAULT(escseq.arg[0], 1);
650 tinsertblank(escseq.arg[0]);
652 case 'A': /* CUU -- Cursor <n> Up */
654 DEFAULT(escseq.arg[0], 1);
655 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
657 case 'B': /* CUD -- Cursor <n> Down */
658 DEFAULT(escseq.arg[0], 1);
659 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
661 case 'C': /* CUF -- Cursor <n> Forward */
663 DEFAULT(escseq.arg[0], 1);
664 tmoveto(term.c.x+escseq.arg[0], term.c.y);
666 case 'D': /* CUB -- Cursor <n> Backward */
667 DEFAULT(escseq.arg[0], 1);
668 tmoveto(term.c.x-escseq.arg[0], term.c.y);
670 case 'E': /* CNL -- Cursor <n> Down and first col */
671 DEFAULT(escseq.arg[0], 1);
672 tmoveto(0, term.c.y+escseq.arg[0]);
674 case 'F': /* CPL -- Cursor <n> Up and first col */
675 DEFAULT(escseq.arg[0], 1);
676 tmoveto(0, term.c.y-escseq.arg[0]);
678 case 'G': /* CHA -- Move to <col> */
679 case '`': /* XXX: HPA -- same? */
680 DEFAULT(escseq.arg[0], 1);
681 tmoveto(escseq.arg[0]-1, term.c.y);
683 case 'H': /* CUP -- Move to <row> <col> */
684 case 'f': /* XXX: HVP -- same? */
685 DEFAULT(escseq.arg[0], 1);
686 DEFAULT(escseq.arg[1], 1);
687 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
689 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
690 case 'J': /* ED -- Clear screen */
691 switch(escseq.arg[0]) {
693 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
696 tclearregion(0, 0, term.c.x, term.c.y);
699 tclearregion(0, 0, term.col-1, term.row-1);
701 case 3: /* XXX: erase saved lines (xterm) */
706 case 'K': /* EL -- Clear line */
707 switch(escseq.arg[0]) {
709 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
712 tclearregion(0, term.c.y, term.c.x, term.c.y);
715 tclearregion(0, term.c.y, term.col-1, term.c.y);
719 case 'S': /* SU -- Scroll <n> line up */
720 DEFAULT(escseq.arg[0], 1);
721 tscrollup(escseq.arg[0]);
723 case 'T': /* SD -- Scroll <n> line down */
724 DEFAULT(escseq.arg[0], 1);
725 tscrolldown(escseq.arg[0]);
727 case 'L': /* IL -- Insert <n> blank lines */
728 DEFAULT(escseq.arg[0], 1);
729 tinsertblankline(escseq.arg[0]);
731 case 'l': /* RM -- Reset Mode */
733 switch(escseq.arg[0]) {
735 term.mode &= ~MODE_APPKEYPAD;
738 term.mode &= ~MODE_WRAP;
740 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
745 case 1048: /* XXX: no alt. screen to erase/save */
747 tcursor(CURSOR_LOAD);
748 tclearregion(0, 0, term.col-1, term.row-1);
754 switch(escseq.arg[0]) {
756 term.mode &= ~MODE_INSERT;
763 case 'M': /* DL -- Delete <n> lines */
764 DEFAULT(escseq.arg[0], 1);
765 tdeleteline(escseq.arg[0]);
767 case 'X': /* ECH -- Erase <n> char */
768 DEFAULT(escseq.arg[0], 1);
769 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
771 case 'P': /* DCH -- Delete <n> char */
772 DEFAULT(escseq.arg[0], 1);
773 tdeletechar(escseq.arg[0]);
775 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
776 case 'd': /* VPA -- Move to <row> */
777 DEFAULT(escseq.arg[0], 1);
778 tmoveto(term.c.x, escseq.arg[0]-1);
780 case 'h': /* SM -- Set terminal mode */
782 switch(escseq.arg[0]) {
784 term.mode |= MODE_APPKEYPAD;
787 term.mode |= MODE_WRAP;
789 case 12: /* att610 -- Start blinking cursor (IGNORED) */
795 case 1049: /* XXX: no alt. screen to erase/save */
796 tcursor(CURSOR_SAVE);
797 tclearregion(0, 0, term.col-1, term.row-1);
799 default: goto unknown;
802 switch(escseq.arg[0]) {
804 term.mode |= MODE_INSERT;
806 default: goto unknown;
810 case 'm': /* SGR -- Terminal attribute (color) */
811 tsetattr(escseq.arg, escseq.narg);
813 case 'r': /* DECSTBM -- Set Scrolling Region */
817 DEFAULT(escseq.arg[0], 1);
818 DEFAULT(escseq.arg[1], term.row);
819 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
822 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
823 tcursor(CURSOR_SAVE);
825 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
826 tcursor(CURSOR_LOAD);
834 printf("ESC [ %s", escseq.priv ? "? " : "");
836 for(i = 0; i < escseq.narg; i++)
837 printf("%d ", escseq.arg[i]);
839 putchar(escseq.mode);
845 memset(&escseq, 0, sizeof(escseq));
850 int space = TAB - term.c.x % TAB;
851 tmoveto(term.c.x + space, term.c.y);
856 if(term.esc & ESC_START) {
857 if(term.esc & ESC_CSI) {
858 escseq.buf[escseq.len++] = c;
859 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
861 csiparse(), csihandle();
863 } else if(term.esc & ESC_OSC) {
866 term.esc = ESC_START | ESC_TITLE;
868 } else if(term.esc & ESC_TITLE) {
869 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
871 term.title[term.titlelen] = '\0';
872 XStoreName(xw.dis, xw.win, term.title);
874 term.title[term.titlelen++] = c;
876 } else if(term.esc & ESC_ALTCHARSET) {
878 case '0': /* Line drawing crap */
879 term.c.attr.mode |= ATTR_GFX;
881 case 'B': /* Back to regular text */
882 term.c.attr.mode &= ~ATTR_GFX;
885 printf("esc unhandled charset: ESC ( %c\n", c);
897 term.esc |= ESC_ALTCHARSET;
900 tmoveto(term.c.x, term.c.y-1);
904 tmoveto(term.c.x, term.c.y+1);
908 tmoveto(term.c.x+1, term.c.y);
911 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
912 tmoveto(term.c.x-1, term.c.y);
915 case 'E': /* NEL -- Next line */
919 case 'M': /* RI -- Reverse index */
920 if(term.c.y == term.top)
923 tmoveto(term.c.x, term.c.y-1);
926 case 'c': /* RIS -- Reset to inital state */
930 case '=': /* DECPAM */
931 term.mode |= MODE_APPKEYPAD;
934 case '>': /* DECPNM */
935 term.mode &= ~MODE_APPKEYPAD;
939 tcursor(CURSOR_SAVE);
943 tcursor(CURSOR_LOAD);
947 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
957 tmoveto(term.c.x-1, term.c.y);
960 tmoveto(0, term.c.y);
970 term.esc = ESC_START;
974 if(term.c.x+1 < term.col) {
975 tmoveto(term.c.x+1, term.c.y);
976 } else if(IS_SET(MODE_WRAP))
984 tputs(char *s, int len) {
985 for(; len > 0; len--)
990 tresize(int col, int row) {
992 int minrow = MIN(row, term.row);
993 int mincol = MIN(col, term.col);
995 if(col < 1 || row < 1)
998 for(i = row; i < term.row; i++)
1000 term.line = realloc(term.line, row * sizeof(Line));
1001 for(i = 0; i < minrow; i++) {
1002 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1003 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1005 for(/* i == minrow */; i < row; i++)
1006 term.line[i] = calloc(col, sizeof(Glyph));
1008 LIMIT(term.c.x, 0, col-1);
1009 LIMIT(term.c.y, 0, row-1);
1010 LIMIT(term.top, 0, row-1);
1011 LIMIT(term.bot, 0, row-1);
1014 term.col = col, term.row = row;
1021 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1022 unsigned long white = WhitePixel(xw.dis, xw.scr);
1024 for(i = 0; i < 16; i++) {
1025 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1027 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1029 dc.col[i] = color.pixel;
1032 /* same colors as xterm */
1033 for(r = 0; r < 6; r++)
1034 for(g = 0; g < 6; g++)
1035 for(b = 0; b < 6; b++) {
1036 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1037 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1038 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1039 if (!XAllocColor(xw.dis, cmap, &color)) {
1041 fprintf(stderr, "Could not allocate color %d\n", i);
1043 dc.col[i] = color.pixel;
1047 for(r = 0; r < 24; r++, i++) {
1048 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1049 if (!XAllocColor(xw.dis, cmap, &color)) {
1051 fprintf(stderr, "Could not allocate color %d\n", i);
1053 dc.col[i] = color.pixel;
1058 xclear(int x1, int y1, int x2, int y2) {
1059 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1060 XFillRectangle(xw.dis, xw.buf, dc.gc,
1061 x1 * xw.cw, y1 * xw.ch,
1062 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1068 XClassHint class = {TNAME, TNAME};
1069 XWMHints wm = {.flags = InputHint, .input = 1};
1071 .flags = PSize | PResizeInc | PBaseSize,
1074 .height_inc = xw.ch,
1076 .base_height = 2*BORDER,
1077 .base_width = 2*BORDER,
1079 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1084 xw.dis = XOpenDisplay(NULL);
1085 xw.scr = XDefaultScreen(xw.dis);
1087 die("Can't open display\n");
1090 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1091 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1093 /* XXX: Assuming same size for bold font */
1094 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1095 xw.ch = dc.font->ascent + dc.font->descent;
1100 term.c.attr.fg = DefaultFG;
1101 term.c.attr.bg = DefaultBG;
1102 term.c.attr.mode = ATTR_NULL;
1104 xw.h = term.row * xw.ch + 2*BORDER;
1105 xw.w = term.col * xw.cw + 2*BORDER;
1106 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1110 xw.bufw = xw.w - 2*BORDER;
1111 xw.bufh = xw.h - 2*BORDER;
1112 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1114 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1115 XMapWindow(xw.dis, xw.win);
1117 XStoreName(xw.dis, xw.win, "st");
1122 xdraws(char *s, Glyph base, int x, int y, int len) {
1123 unsigned long xfg, xbg;
1124 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1127 if(base.mode & ATTR_REVERSE)
1128 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1130 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1132 XSetBackground(xw.dis, dc.gc, xbg);
1133 XSetForeground(xw.dis, dc.gc, xfg);
1135 if(base.mode & ATTR_GFX)
1136 for(i = 0; i < len; i++)
1137 s[i] = gfx[(int)s[i]];
1139 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1140 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1142 if(base.mode & ATTR_UNDERLINE)
1143 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1148 static int oldx = 0;
1149 static int oldy = 0;
1150 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1152 LIMIT(oldx, 0, term.col-1);
1153 LIMIT(oldy, 0, term.row-1);
1155 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1156 g.c = term.line[term.c.y][term.c.x].c;
1158 /* remove the old cursor */
1159 if(term.line[oldy][oldx].state & GLYPH_SET)
1160 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1162 xclear(oldx, oldy, oldx, oldy);
1164 /* draw the new one */
1165 if(mode == CURSOR_DRAW) {
1166 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1167 oldx = term.c.x, oldy = term.c.y;
1172 /* basic drawing routines */
1174 xdrawc(int x, int y, Glyph g) {
1175 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1176 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1177 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1178 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1179 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1186 xclear(0, 0, term.col-1, term.row-1);
1187 for(y = 0; y < term.row; y++)
1188 for(x = 0; x < term.col; x++)
1189 if(term.line[y][x].state & GLYPH_SET)
1190 xdrawc(x, y, term.line[y][x]);
1193 xcursor(CURSOR_DRAW);
1194 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1199 /* optimized drawing routine */
1201 draw(int redraw_all) {
1204 char buf[DRAW_BUF_SIZ];
1206 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1207 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
1208 for(y = 0; y < term.row; y++) {
1209 base = term.line[y][0];
1211 for(x = 0; x < term.col; x++) {
1212 new = term.line[y][x];
1213 if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
1216 xdraws(buf, base, ox, y, i);
1223 xdraws(buf, base, ox, y, i);
1225 xcursor(term.hidec ? CURSOR_HIDE : CURSOR_DRAW);
1226 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1233 expose(XEvent *ev) {
1234 draw(SCREEN_REDRAW);
1240 for(i = 0; i < LEN(key); i++)
1242 return (char*)key[i].s;
1247 kpress(XEvent *ev) {
1248 XKeyEvent *e = &ev->xkey;
1256 meta = e->state & Mod1Mask;
1257 shift = e->state & ShiftMask;
1258 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1260 if((customkey = kmap(ksym)))
1261 ttywrite(customkey, strlen(customkey));
1263 buf[sizeof(buf)-1] = '\0';
1264 if(meta && len == 1)
1265 ttywrite("\033", 1);
1273 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1278 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1281 fprintf(stderr, "errkey: %d\n", (int)ksym);
1290 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1293 xw.w = e->xconfigure.width;
1294 xw.h = e->xconfigure.height;
1295 xw.bufw = xw.w - 2*BORDER;
1296 xw.bufh = xw.h - 2*BORDER;
1297 col = xw.bufw / xw.cw;
1298 row = xw.bufh / xw.ch;
1300 ttyresize(col, row);
1301 XFreePixmap(xw.dis, xw.buf);
1302 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1303 draw(SCREEN_REDRAW);
1310 int xfd = XConnectionNumber(xw.dis);
1313 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1314 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1318 FD_SET(cmdfd, &rfd);
1320 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
1323 die("select failed: %s\n", SERRNO);
1325 if(FD_ISSET(cmdfd, &rfd)) {
1327 draw(SCREEN_UPDATE);
1329 while(XPending(xw.dis)) {
1330 XNextEvent(xw.dis, &ev);
1331 if(handler[ev.type])
1332 (handler[ev.type])(&ev);
1338 main(int argc, char *argv[]) {
1339 if(argc == 2 && !strncmp("-v", argv[1], 3))
1340 die("st-" VERSION ", © 2009 st engineers\n");
1342 die("usage: st [-v]\n");
1343 setlocale(LC_CTYPE, "");