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 tputc(char);
140 static void tputs(char*, int);
141 static void treset(void);
142 static void tresize(int, int);
143 static void tscroll(void);
144 static void tscrollup(int);
145 static void tscrolldown(int);
146 static void tsetattr(int*, int);
147 static void tsetchar(char);
148 static void tsetscroll(int, int);
150 static void ttynew(void);
151 static void ttyread(void);
152 static void ttyresize(int, int);
153 static void ttywrite(const char *, size_t);
155 static void xclear(int, int, int, int);
156 static void xcursor(int);
157 static void xinit(void);
158 static void xloadcols(void);
160 static void expose(XEvent *);
161 static char* kmap(KeySym);
162 static void kpress(XEvent *);
163 static void resize(XEvent *);
165 static void (*handler[LASTEvent])(XEvent *) = {
168 [ConfigureNotify] = resize
175 static CSIEscape escseq;
186 for(row = 0; row < term.row; row++) {
187 for(col = 0; col < term.col; col++) {
188 if(col == term.c.x && row == term.c.y)
191 c = term.line[row][col];
192 putchar(c.state & GLYPH_SET ? c.c : '.');
201 die(const char *errstr, ...) {
204 va_start(ap, errstr);
205 vfprintf(stderr, errstr, ap);
212 char *args[3] = {getenv("SHELL"), "-i", NULL};
213 DEFAULT(args[0], "/bin/sh"); /* default shell if getenv() failed */
214 putenv("TERM=" TNAME);
215 execvp(args[0], args);
219 xbell(void) { /* visual bell */
220 XRectangle r = { BORDER, BORDER, xw.bufw, xw.bufh };
221 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
222 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
230 if(waitpid(pid, &stat, 0) < 0)
231 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
233 exit(WEXITSTATUS(stat));
243 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
244 die("openpt failed: %s\n", SERRNO);
246 die("grandpt failed: %s\n", SERRNO);
248 die("unlockpt failed: %s\n", SERRNO);
249 if(!(pts = ptsname(m)))
250 die("ptsname failed: %s\n", SERRNO);
251 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
252 die("Couldn't open slave: %s\n", SERRNO);
253 fcntl(s, F_SETFL, O_NDELAY);
254 switch(pid = fork()) {
256 die("fork failed\n");
259 setsid(); /* create a new process group */
260 dup2(s, STDIN_FILENO);
261 dup2(s, STDOUT_FILENO);
262 dup2(s, STDERR_FILENO);
263 if(ioctl(s, TIOCSCTTY, NULL) < 0)
264 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
270 signal(SIGCHLD, sigchld);
277 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
279 fprintf(stderr, "\n");
284 char buf[BUFSIZ] = {0};
287 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
288 die("Couldn't read from shell: %s\n", SERRNO);
294 ttywrite(const char *s, size_t n) {
295 if(write(cmdfd, s, n) == -1)
296 die("write error on tty: %s\n", SERRNO);
300 ttyresize(int x, int y) {
305 w.ws_xpixel = w.ws_ypixel = 0;
306 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
307 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
314 if(mode == CURSOR_SAVE)
316 else if(mode == CURSOR_LOAD)
317 term.c = c, tmoveto(c.x, c.y);
322 term.c.attr.mode = ATTR_NULL;
323 term.c.attr.fg = DefaultFG;
324 term.c.attr.bg = DefaultBG;
325 term.c.x = term.c.y = 0;
327 term.top = 0, term.bot = term.row - 1;
328 term.mode = MODE_WRAP;
329 tclearregion(0, 0, term.col-1, term.row-1);
333 tnew(int col, int row) { /* screen size */
334 term.row = row, term.col = col;
335 term.top = 0, term.bot = term.row - 1;
337 term.mode = MODE_WRAP;
339 term.c.attr.mode = ATTR_NULL;
340 term.c.attr.fg = DefaultFG;
341 term.c.attr.bg = DefaultBG;
342 term.c.x = term.c.y = 0;
344 /* allocate screen */
345 term.line = calloc(term.row, sizeof(Line));
346 for(row = 0 ; row < term.row; row++)
347 term.line[row] = calloc(term.col, sizeof(Glyph));
350 /* TODO: Replace with scrollup/scolldown */
353 Line temp = term.line[term.top];
356 for(i = term.top; i < term.bot; i++)
357 term.line[i] = term.line[i+1];
358 memset(temp, 0, sizeof(Glyph) * term.col);
359 term.line[term.bot] = temp;
363 tscrolldown (int n) {
367 LIMIT(n, 0, term.bot-term.top+1);
369 for(i = 0; i < n; i++)
370 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
372 for(i = term.bot; i >= term.top+n; i--) {
374 term.line[i] = term.line[i-n];
375 term.line[i-n] = temp;
383 LIMIT(n, 0, term.bot-term.top+1);
385 for(i = 0; i < n; i++)
386 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
388 for(i = term.top; i <= term.bot-n; i++) {
390 term.line[i] = term.line[i+n];
391 term.line[i+n] = temp;
397 int y = term.c.y + 1;
399 tscroll(), y = term.bot;
406 char *p = escseq.buf;
410 escseq.priv = 1, p++;
412 while(p < escseq.buf+escseq.len) {
414 escseq.arg[escseq.narg] *= 10;
415 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
417 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
428 tmoveto(int x, int y) {
429 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
430 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
434 tmovecursor(int dir) {
435 int xf = term.c.x, yf = term.c.y;
449 if(term.mode & MODE_WRAP && xf >= term.col) {
452 yf = term.bot, tscroll();
461 term.line[term.c.y][term.c.x] = term.c.attr;
462 term.line[term.c.y][term.c.x].c = c;
463 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
467 tclearregion(int x1, int y1, int x2, int y2) {
471 temp = x1, x1 = x2, x2 = temp;
473 temp = y1, y1 = y2, y2 = temp;
475 LIMIT(x1, 0, term.col-1);
476 LIMIT(x2, 0, term.col-1);
477 LIMIT(y1, 0, term.row-1);
478 LIMIT(y2, 0, term.row-1);
480 for(y = y1; y <= y2; y++)
481 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
486 int src = term.c.x + n;
488 int size = term.col - src;
490 if(src >= term.col) {
491 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
494 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
495 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
499 tinsertblank(int n) {
502 int size = term.col - dst;
504 if(dst >= term.col) {
505 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
508 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
509 tclearregion(src, term.c.y, dst - 1, term.c.y);
513 tinsertblankline(int n) {
518 if(term.c.y > term.bot)
520 else if(term.c.y < term.top)
522 if(term.c.y + n >= bot) {
523 tclearregion(0, term.c.y, term.col-1, bot);
526 for(i = bot; i >= term.c.y+n; i--) {
527 /* swap deleted line <-> blanked line */
528 blank = term.line[i];
529 term.line[i] = term.line[i-n];
530 term.line[i-n] = blank;
532 memset(blank, 0, term.col * sizeof(Glyph));
542 if(term.c.y > term.bot)
544 else if(term.c.y < term.top)
546 if(term.c.y + n >= bot) {
547 tclearregion(0, term.c.y, term.col-1, bot);
550 for(i = term.c.y; i <= bot-n; i++) {
551 /* swap deleted line <-> blanked line */
552 blank = term.line[i];
553 term.line[i] = term.line[i+n];
554 term.line[i+n] = blank;
556 memset(blank, 0, term.col * sizeof(Glyph));
561 tsetattr(int *attr, int l) {
564 for(i = 0; i < l; i++) {
567 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
568 term.c.attr.fg = DefaultFG;
569 term.c.attr.bg = DefaultBG;
572 term.c.attr.mode |= ATTR_BOLD;
575 term.c.attr.mode |= ATTR_UNDERLINE;
578 term.c.attr.mode |= ATTR_REVERSE;
581 term.c.attr.mode &= ~ATTR_BOLD;
584 term.c.attr.mode &= ~ATTR_UNDERLINE;
587 term.c.attr.mode &= ~ATTR_REVERSE;
590 if (i + 2 < l && attr[i + 1] == 5) {
592 if (BETWEEN(attr[i], 0, 255))
593 term.c.attr.fg = attr[i];
595 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
598 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
601 term.c.attr.fg = DefaultFG;
604 if (i + 2 < l && attr[i + 1] == 5) {
606 if (BETWEEN(attr[i], 0, 255))
607 term.c.attr.bg = attr[i];
609 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
612 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
615 term.c.attr.bg = DefaultBG;
618 if(BETWEEN(attr[i], 30, 37))
619 term.c.attr.fg = attr[i] - 30;
620 else if(BETWEEN(attr[i], 40, 47))
621 term.c.attr.bg = attr[i] - 40;
622 else if(BETWEEN(attr[i], 90, 97))
623 term.c.attr.fg = attr[i] - 90 + 8;
624 else if(BETWEEN(attr[i], 100, 107))
625 term.c.attr.fg = attr[i] - 100 + 8;
627 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
634 tsetscroll(int t, int b) {
637 LIMIT(t, 0, term.row-1);
638 LIMIT(b, 0, term.row-1);
650 switch(escseq.mode) {
653 printf("erresc: unknown csi ");
657 case '@': /* ICH -- Insert <n> blank char */
658 DEFAULT(escseq.arg[0], 1);
659 tinsertblank(escseq.arg[0]);
661 case 'A': /* CUU -- Cursor <n> Up */
663 DEFAULT(escseq.arg[0], 1);
664 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
666 case 'B': /* CUD -- Cursor <n> Down */
667 DEFAULT(escseq.arg[0], 1);
668 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
670 case 'C': /* CUF -- Cursor <n> Forward */
672 DEFAULT(escseq.arg[0], 1);
673 tmoveto(term.c.x+escseq.arg[0], term.c.y);
675 case 'D': /* CUB -- Cursor <n> Backward */
676 DEFAULT(escseq.arg[0], 1);
677 tmoveto(term.c.x-escseq.arg[0], term.c.y);
679 case 'E': /* CNL -- Cursor <n> Down and first col */
680 DEFAULT(escseq.arg[0], 1);
681 tmoveto(0, term.c.y+escseq.arg[0]);
683 case 'F': /* CPL -- Cursor <n> Up and first col */
684 DEFAULT(escseq.arg[0], 1);
685 tmoveto(0, term.c.y-escseq.arg[0]);
687 case 'G': /* CHA -- Move to <col> */
688 case '`': /* XXX: HPA -- same? */
689 DEFAULT(escseq.arg[0], 1);
690 tmoveto(escseq.arg[0]-1, term.c.y);
692 case 'H': /* CUP -- Move to <row> <col> */
693 case 'f': /* XXX: HVP -- same? */
694 DEFAULT(escseq.arg[0], 1);
695 DEFAULT(escseq.arg[1], 1);
696 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
698 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
699 case 'J': /* ED -- Clear screen */
700 switch(escseq.arg[0]) {
702 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
705 tclearregion(0, 0, term.c.x, term.c.y);
708 tclearregion(0, 0, term.col-1, term.row-1);
710 case 3: /* XXX: erase saved lines (xterm) */
715 case 'K': /* EL -- Clear line */
716 switch(escseq.arg[0]) {
718 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
721 tclearregion(0, term.c.y, term.c.x, term.c.y);
724 tclearregion(0, term.c.y, term.col-1, term.c.y);
728 case 'S': /* SU -- Scroll <n> line up */
729 DEFAULT(escseq.arg[0], 1);
730 tscrollup(escseq.arg[0]);
732 case 'T': /* SD -- Scroll <n> line down */
733 DEFAULT(escseq.arg[0], 1);
734 tscrolldown(escseq.arg[0]);
736 case 'L': /* IL -- Insert <n> blank lines */
737 DEFAULT(escseq.arg[0], 1);
738 tinsertblankline(escseq.arg[0]);
740 case 'l': /* RM -- Reset Mode */
742 switch(escseq.arg[0]) {
744 term.mode &= ~MODE_APPKEYPAD;
747 term.mode &= ~MODE_WRAP;
749 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
754 case 1048: /* XXX: no alt. screen to erase/save */
756 tcursor(CURSOR_LOAD);
757 tclearregion(0, 0, term.col-1, term.row-1);
763 switch(escseq.arg[0]) {
765 term.mode &= ~MODE_INSERT;
772 case 'M': /* DL -- Delete <n> lines */
773 DEFAULT(escseq.arg[0], 1);
774 tdeleteline(escseq.arg[0]);
776 case 'X': /* ECH -- Erase <n> char */
777 DEFAULT(escseq.arg[0], 1);
778 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
780 case 'P': /* DCH -- Delete <n> char */
781 DEFAULT(escseq.arg[0], 1);
782 tdeletechar(escseq.arg[0]);
784 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
785 case 'd': /* VPA -- Move to <row> */
786 DEFAULT(escseq.arg[0], 1);
787 tmoveto(term.c.x, escseq.arg[0]-1);
789 case 'h': /* SM -- Set terminal mode */
791 switch(escseq.arg[0]) {
793 term.mode |= MODE_APPKEYPAD;
796 term.mode |= MODE_WRAP;
798 case 12: /* att610 -- Start blinking cursor (IGNORED) */
804 case 1049: /* XXX: no alt. screen to erase/save */
805 tcursor(CURSOR_SAVE);
806 tclearregion(0, 0, term.col-1, term.row-1);
808 default: goto unknown;
811 switch(escseq.arg[0]) {
813 term.mode |= MODE_INSERT;
815 default: goto unknown;
819 case 'm': /* SGR -- Terminal attribute (color) */
820 tsetattr(escseq.arg, escseq.narg);
822 case 'r': /* DECSTBM -- Set Scrolling Region */
826 DEFAULT(escseq.arg[0], 1);
827 DEFAULT(escseq.arg[1], term.row);
828 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
831 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
832 tcursor(CURSOR_SAVE);
834 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
835 tcursor(CURSOR_LOAD);
843 printf("ESC [ %s", escseq.priv ? "? " : "");
845 for(i = 0; i < escseq.narg; i++)
846 printf("%d ", escseq.arg[i]);
848 putchar(escseq.mode);
854 memset(&escseq, 0, sizeof(escseq));
859 int space = TAB - term.c.x % TAB;
861 if(term.c.x + space >= term.col)
864 for(; space > 0; space--)
865 tmovecursor(CURSOR_RIGHT);
870 if(term.esc & ESC_START) {
871 if(term.esc & ESC_CSI) {
872 escseq.buf[escseq.len++] = c;
873 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
875 csiparse(), csihandle();
877 } else if(term.esc & ESC_OSC) {
880 term.esc = ESC_START | ESC_TITLE;
882 } else if(term.esc & ESC_TITLE) {
883 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
885 term.title[term.titlelen] = '\0';
886 XStoreName(xw.dis, xw.win, term.title);
888 term.title[term.titlelen++] = c;
890 } else if(term.esc & ESC_ALTCHARSET) {
892 case '0': /* Line drawing crap */
893 term.c.attr.mode |= ATTR_GFX;
895 case 'B': /* Back to regular text */
896 term.c.attr.mode &= ~ATTR_GFX;
899 printf("esc unhandled charset: ESC ( %c\n", c);
911 term.esc |= ESC_ALTCHARSET;
914 tmoveto(term.c.x, term.c.y-1);
918 tmoveto(term.c.x, term.c.y+1);
922 tmoveto(term.c.x+1, term.c.y);
925 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
926 tmoveto(term.c.x-1, term.c.y);
929 case 'E': /* NEL -- Next line */
933 case 'M': /* RI -- Reverse index */
934 if(term.c.y == term.top)
937 tmoveto(term.c.x, term.c.y-1);
940 case 'c': /* RIS -- Reset to inital state */
944 case '=': /* DECPAM */
945 term.mode |= MODE_APPKEYPAD;
948 case '>': /* DECPNM */
949 term.mode &= ~MODE_APPKEYPAD;
953 tcursor(CURSOR_SAVE);
957 tcursor(CURSOR_LOAD);
961 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
971 tmovecursor(CURSOR_LEFT);
974 tmoveto(0, term.c.y);
984 term.esc = ESC_START;
988 tmovecursor(CURSOR_RIGHT);
995 tputs(char *s, int len) {
996 for(; len > 0; len--)
1001 tresize(int col, int row) {
1004 int minrow = MIN(row, term.row);
1005 int mincol = MIN(col, term.col);
1007 if(col < 1 || row < 1)
1010 line = calloc(row, sizeof(Line));
1011 for(i = 0 ; i < row; i++)
1012 line[i] = calloc(col, sizeof(Glyph));
1014 for(i = 0 ; i < minrow; i++)
1015 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
1017 for(i = 0; i < term.row; i++)
1021 LIMIT(term.c.x, 0, col-1);
1022 LIMIT(term.c.y, 0, row-1);
1023 LIMIT(term.top, 0, row-1);
1024 LIMIT(term.bot, 0, row-1);
1028 term.col = col, term.row = row;
1035 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1036 unsigned long white = WhitePixel(xw.dis, xw.scr);
1038 for(i = 0; i < 16; i++) {
1039 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1041 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1043 dc.col[i] = color.pixel;
1046 /* same colors as xterm */
1047 for(r = 0; r < 6; r++)
1048 for(g = 0; g < 6; g++)
1049 for(b = 0; b < 6; b++) {
1050 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1051 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1052 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1053 if (!XAllocColor(xw.dis, cmap, &color)) {
1055 fprintf(stderr, "Could not allocate color %d\n", i);
1057 dc.col[i] = color.pixel;
1061 for(r = 0; r < 24; r++, i++) {
1062 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1063 if (!XAllocColor(xw.dis, cmap, &color)) {
1065 fprintf(stderr, "Could not allocate color %d\n", i);
1067 dc.col[i] = color.pixel;
1072 xclear(int x1, int y1, int x2, int y2) {
1073 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1074 XFillRectangle(xw.dis, xw.buf, dc.gc,
1075 x1 * xw.cw, y1 * xw.ch,
1076 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1082 XClassHint class = {TNAME, TNAME};
1083 XWMHints wm = {.flags = InputHint, .input = 1};
1085 .flags = PSize | PResizeInc | PBaseSize,
1088 .height_inc = xw.ch,
1090 .base_height = 2*BORDER,
1091 .base_width = 2*BORDER,
1093 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1098 xw.dis = XOpenDisplay(NULL);
1099 xw.scr = XDefaultScreen(xw.dis);
1101 die("Can't open display\n");
1104 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1105 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1107 /* XXX: Assuming same size for bold font */
1108 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1109 xw.ch = dc.font->ascent + dc.font->descent;
1114 term.c.attr.fg = DefaultFG;
1115 term.c.attr.bg = DefaultBG;
1116 term.c.attr.mode = ATTR_NULL;
1118 xw.h = term.row * xw.ch + 2*BORDER;
1119 xw.w = term.col * xw.cw + 2*BORDER;
1120 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1124 xw.bufw = xw.w - 2*BORDER;
1125 xw.bufh = xw.h - 2*BORDER;
1126 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1128 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1129 XMapWindow(xw.dis, xw.win);
1131 XStoreName(xw.dis, xw.win, "st");
1136 xdraws(char *s, Glyph base, int x, int y, int len) {
1137 unsigned long xfg, xbg;
1138 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1141 if(base.mode & ATTR_REVERSE)
1142 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1144 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1146 XSetBackground(xw.dis, dc.gc, xbg);
1147 XSetForeground(xw.dis, dc.gc, xfg);
1149 if(base.mode & ATTR_GFX)
1150 for(i = 0; i < len; i++)
1151 s[i] = gfx[(int)s[i]];
1153 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1154 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1156 if(base.mode & ATTR_UNDERLINE)
1157 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1162 static int oldx = 0;
1163 static int oldy = 0;
1164 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1166 LIMIT(oldx, 0, term.col-1);
1167 LIMIT(oldy, 0, term.row-1);
1169 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1170 g.c = term.line[term.c.y][term.c.x].c;
1172 /* remove the old cursor */
1173 if(term.line[oldy][oldx].state & GLYPH_SET)
1174 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1176 xclear(oldx, oldy, oldx, oldy);
1178 /* draw the new one */
1179 if(mode == CURSOR_DRAW) {
1180 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1181 oldx = term.c.x, oldy = term.c.y;
1186 /* basic drawing routines */
1188 xdrawc(int x, int y, Glyph g) {
1189 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1190 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1191 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1192 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1193 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1200 xclear(0, 0, term.col-1, term.row-1);
1201 for(y = 0; y < term.row; y++)
1202 for(x = 0; x < term.col; x++)
1203 if(term.line[y][x].state & GLYPH_SET)
1204 xdrawc(x, y, term.line[y][x]);
1207 xcursor(CURSOR_DRAW);
1208 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1213 /* optimized drawing routine */
1215 draw(int redraw_all) {
1218 char buf[DRAW_BUF_SIZ];
1220 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1221 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
1222 for(y = 0; y < term.row; y++) {
1223 base = term.line[y][0];
1225 for(x = 0; x < term.col; x++) {
1226 new = term.line[y][x];
1227 if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
1230 xdraws(buf, base, ox, y, i);
1237 xdraws(buf, base, ox, y, i);
1239 xcursor(term.hidec ? CURSOR_HIDE : CURSOR_DRAW);
1240 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1247 expose(XEvent *ev) {
1248 draw(SCREEN_REDRAW);
1254 for(i = 0; i < LEN(key); i++)
1256 return (char*)key[i].s;
1261 kpress(XEvent *ev) {
1262 XKeyEvent *e = &ev->xkey;
1270 meta = e->state & Mod1Mask;
1271 shift = e->state & ShiftMask;
1272 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1274 if((customkey = kmap(ksym)))
1275 ttywrite(customkey, strlen(customkey));
1277 buf[sizeof(buf)-1] = '\0';
1278 if(meta && len == 1)
1279 ttywrite("\033", 1);
1287 sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
1292 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1295 fprintf(stderr, "errkey: %d\n", (int)ksym);
1304 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1307 xw.w = e->xconfigure.width;
1308 xw.h = e->xconfigure.height;
1309 xw.bufw = xw.w - 2*BORDER;
1310 xw.bufh = xw.h - 2*BORDER;
1311 col = xw.bufw / xw.cw;
1312 row = xw.bufh / xw.ch;
1314 ttyresize(col, row);
1315 XFreePixmap(xw.dis, xw.buf);
1316 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1317 draw(SCREEN_REDRAW);
1324 int xfd = XConnectionNumber(xw.dis);
1327 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1328 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1332 FD_SET(cmdfd, &rfd);
1334 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
1337 die("select failed: %s\n", SERRNO);
1339 if(FD_ISSET(cmdfd, &rfd)) {
1341 draw(SCREEN_UPDATE);
1343 while(XPending(xw.dis)) {
1344 XNextEvent(xw.dis, &ev);
1345 if(handler[ev.type])
1346 (handler[ev.type])(&ev);
1352 main(int argc, char *argv[]) {
1353 if(argc == 2 && !strncmp("-v", argv[1], 3))
1354 die("st-" VERSION ", © 2009 st engineers\n");
1356 die("usage: st [-v]\n");
1357 setlocale(LC_CTYPE, "");