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,
50 CURSOR_SAVE, CURSOR_LOAD };
51 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
52 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
53 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4 };
54 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
55 enum { SCREEN_UPDATE, SCREEN_REDRAW };
58 char c; /* character code */
59 char mode; /* attribute flags */
60 int fg; /* foreground */
61 int bg; /* background */
62 char state; /* state flags */
68 Glyph attr; /* current char attributes */
74 /* CSI Escape sequence structs */
75 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
77 char buf[ESC_BUF_SIZ]; /* raw string */
78 int len; /* raw string length */
81 int narg; /* nb of args */
85 /* Internal representation of the screen */
89 Line* line; /* screen */
90 TCursor c; /* cursor */
91 int top; /* top scroll limit */
92 int bot; /* bottom scroll limit */
93 int mode; /* terminal mode flags */
94 int esc; /* escape state flags */
95 char title[ESC_TITLE_SIZ];
99 /* Purely graphic info */
105 int w; /* window width */
106 int h; /* window height */
107 int bufw; /* pixmap width */
108 int bufh; /* pixmap height */
109 int ch; /* char height */
110 int cw; /* char width */
118 /* Drawing Context */
120 unsigned long col[256];
128 static void die(const char *errstr, ...);
129 static void draw(int);
130 static void execsh(void);
131 static void sigchld(int);
132 static void run(void);
134 static void csidump(void);
135 static void csihandle(void);
136 static void csiparse(void);
137 static void csireset(void);
139 static void tclearregion(int, int, int, int);
140 static void tcursor(int);
141 static void tdeletechar(int);
142 static void tdeleteline(int);
143 static void tinsertblank(int);
144 static void tinsertblankline(int);
145 static void tmoveto(int, int);
146 static void tnew(int, int);
147 static void tnewline(void);
148 static void tputtab(void);
149 static void tputc(char);
150 static void tputs(char*, int);
151 static void treset(void);
152 static void tresize(int, int);
153 static void tscrollup(int);
154 static void tscrolldown(int);
155 static void tsetattr(int*, int);
156 static void tsetchar(char);
157 static void tsetscroll(int, int);
159 static void ttynew(void);
160 static void ttyread(void);
161 static void ttyresize(int, int);
162 static void ttywrite(const char *, size_t);
164 static void xbell(void);
165 static void xdraws(char *, Glyph, int, int, int);
166 static void xhints(void);
167 static void xclear(int, int, int, int);
168 static void xdrawcursor(void);
169 static void xinit(void);
170 static void xloadcols(void);
172 static void expose(XEvent *);
173 static char* kmap(KeySym);
174 static void kpress(XEvent *);
175 static void resize(XEvent *);
177 static void (*handler[LASTEvent])(XEvent *) = {
180 [ConfigureNotify] = resize
187 static CSIEscape escseq;
198 for(row = 0; row < term.row; row++) {
199 for(col = 0; col < term.col; col++) {
200 if(col == term.c.x && row == term.c.y)
203 c = term.line[row][col];
204 putchar(c.state & GLYPH_SET ? c.c : '.');
213 die(const char *errstr, ...) {
216 va_start(ap, errstr);
217 vfprintf(stderr, errstr, ap);
224 char *args[3] = {getenv("SHELL"), "-i", NULL};
225 DEFAULT(args[0], "/bin/sh"); /* if getenv() failed */
226 putenv("TERM=" TNAME);
227 execvp(args[0], args);
232 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
233 XFillRectangle(xw.dis, xw.win, dc.gc, BORDER, BORDER, xw.bufw, xw.bufh);
242 if(waitpid(pid, &stat, 0) < 0)
243 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
245 exit(WEXITSTATUS(stat));
254 /* seems to work fine on linux, openbsd and freebsd */
255 struct winsize w = {term.row, term.col, 0, 0};
256 if(openpty(&m, &s, NULL, NULL, &w) < 0)
257 die("openpty failed: %s\n", SERRNO);
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);
333 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
335 term.top = 0, term.bot = term.row - 1;
336 term.mode = MODE_WRAP;
337 tclearregion(0, 0, term.col-1, term.row-1);
341 tnew(int col, int row) {
342 /* set screen size */
343 term.row = row, term.col = col;
344 term.line = malloc(term.row * sizeof(Line));
345 for(row = 0 ; row < term.row; row++)
346 term.line[row] = malloc(term.col * sizeof(Glyph));
352 tscrolldown (int n) {
356 LIMIT(n, 0, term.bot-term.top+1);
358 for(i = 0; i < n; i++)
359 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
361 for(i = term.bot; i >= term.top+n; i--) {
363 term.line[i] = term.line[i-n];
364 term.line[i-n] = temp;
372 LIMIT(n, 0, term.bot-term.top+1);
374 for(i = 0; i < n; i++)
375 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
377 for(i = term.top; i <= term.bot-n; i++) {
379 term.line[i] = term.line[i+n];
380 term.line[i+n] = temp;
386 int y = term.c.y + 1;
388 tscrollup(1), y = term.bot;
395 char *p = escseq.buf;
399 escseq.priv = 1, p++;
401 while(p < escseq.buf+escseq.len) {
403 escseq.arg[escseq.narg] *= 10;
404 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
406 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
417 tmoveto(int x, int y) {
418 LIMIT(x, 0, term.col-1);
419 LIMIT(y, 0, term.row-1);
420 term.c.state &= ~CURSOR_WRAPNEXT;
427 term.line[term.c.y][term.c.x] = term.c.attr;
428 term.line[term.c.y][term.c.x].c = c;
429 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
433 tclearregion(int x1, int y1, int x2, int y2) {
437 temp = x1, x1 = x2, x2 = temp;
439 temp = y1, y1 = y2, y2 = temp;
441 LIMIT(x1, 0, term.col-1);
442 LIMIT(x2, 0, term.col-1);
443 LIMIT(y1, 0, term.row-1);
444 LIMIT(y2, 0, term.row-1);
446 for(y = y1; y <= y2; y++)
447 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
452 int src = term.c.x + n;
454 int size = term.col - src;
456 if(src >= term.col) {
457 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
460 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
461 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
465 tinsertblank(int n) {
468 int size = term.col - dst;
470 if(dst >= term.col) {
471 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
474 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
475 tclearregion(src, term.c.y, dst - 1, term.c.y);
479 tinsertblankline(int n) {
484 if(term.c.y > term.bot)
486 else if(term.c.y < term.top)
488 if(term.c.y + n >= bot) {
489 tclearregion(0, term.c.y, term.col-1, bot);
492 for(i = bot; i >= term.c.y+n; i--) {
493 /* swap deleted line <-> blanked line */
494 blank = term.line[i];
495 term.line[i] = term.line[i-n];
496 term.line[i-n] = blank;
498 memset(blank, 0, term.col * sizeof(Glyph));
508 if(term.c.y > term.bot)
510 else if(term.c.y < term.top)
512 if(term.c.y + n >= bot) {
513 tclearregion(0, term.c.y, term.col-1, bot);
516 for(i = term.c.y; i <= bot-n; i++) {
517 /* swap deleted line <-> blanked line */
518 blank = term.line[i];
519 term.line[i] = term.line[i+n];
520 term.line[i+n] = blank;
522 memset(blank, 0, term.col * sizeof(Glyph));
527 tsetattr(int *attr, int l) {
530 for(i = 0; i < l; i++) {
533 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
534 term.c.attr.fg = DefaultFG;
535 term.c.attr.bg = DefaultBG;
538 term.c.attr.mode |= ATTR_BOLD;
541 term.c.attr.mode |= ATTR_UNDERLINE;
544 term.c.attr.mode |= ATTR_REVERSE;
547 term.c.attr.mode &= ~ATTR_BOLD;
550 term.c.attr.mode &= ~ATTR_UNDERLINE;
553 term.c.attr.mode &= ~ATTR_REVERSE;
556 if (i + 2 < l && attr[i + 1] == 5) {
558 if (BETWEEN(attr[i], 0, 255))
559 term.c.attr.fg = attr[i];
561 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
564 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
567 term.c.attr.fg = DefaultFG;
570 if (i + 2 < l && attr[i + 1] == 5) {
572 if (BETWEEN(attr[i], 0, 255))
573 term.c.attr.bg = attr[i];
575 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
578 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
581 term.c.attr.bg = DefaultBG;
584 if(BETWEEN(attr[i], 30, 37))
585 term.c.attr.fg = attr[i] - 30;
586 else if(BETWEEN(attr[i], 40, 47))
587 term.c.attr.bg = attr[i] - 40;
588 else if(BETWEEN(attr[i], 90, 97))
589 term.c.attr.fg = attr[i] - 90 + 8;
590 else if(BETWEEN(attr[i], 100, 107))
591 term.c.attr.fg = attr[i] - 100 + 8;
593 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
600 tsetscroll(int t, int b) {
603 LIMIT(t, 0, term.row-1);
604 LIMIT(b, 0, term.row-1);
616 switch(escseq.mode) {
619 printf("erresc: unknown csi ");
623 case '@': /* ICH -- Insert <n> blank char */
624 DEFAULT(escseq.arg[0], 1);
625 tinsertblank(escseq.arg[0]);
627 case 'A': /* CUU -- Cursor <n> Up */
629 DEFAULT(escseq.arg[0], 1);
630 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
632 case 'B': /* CUD -- Cursor <n> Down */
633 DEFAULT(escseq.arg[0], 1);
634 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
636 case 'C': /* CUF -- Cursor <n> Forward */
638 DEFAULT(escseq.arg[0], 1);
639 tmoveto(term.c.x+escseq.arg[0], term.c.y);
641 case 'D': /* CUB -- Cursor <n> Backward */
642 DEFAULT(escseq.arg[0], 1);
643 tmoveto(term.c.x-escseq.arg[0], term.c.y);
645 case 'E': /* CNL -- Cursor <n> Down and first col */
646 DEFAULT(escseq.arg[0], 1);
647 tmoveto(0, term.c.y+escseq.arg[0]);
649 case 'F': /* CPL -- Cursor <n> Up and first col */
650 DEFAULT(escseq.arg[0], 1);
651 tmoveto(0, term.c.y-escseq.arg[0]);
653 case 'G': /* CHA -- Move to <col> */
654 case '`': /* XXX: HPA -- same? */
655 DEFAULT(escseq.arg[0], 1);
656 tmoveto(escseq.arg[0]-1, term.c.y);
658 case 'H': /* CUP -- Move to <row> <col> */
659 case 'f': /* XXX: HVP -- same? */
660 DEFAULT(escseq.arg[0], 1);
661 DEFAULT(escseq.arg[1], 1);
662 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
664 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
665 case 'J': /* ED -- Clear screen */
666 switch(escseq.arg[0]) {
668 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
671 tclearregion(0, 0, term.c.x, term.c.y);
674 tclearregion(0, 0, term.col-1, term.row-1);
676 case 3: /* XXX: erase saved lines (xterm) */
681 case 'K': /* EL -- Clear line */
682 switch(escseq.arg[0]) {
684 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
687 tclearregion(0, term.c.y, term.c.x, term.c.y);
690 tclearregion(0, term.c.y, term.col-1, term.c.y);
694 case 'S': /* SU -- Scroll <n> line up */
695 DEFAULT(escseq.arg[0], 1);
696 tscrollup(escseq.arg[0]);
698 case 'T': /* SD -- Scroll <n> line down */
699 DEFAULT(escseq.arg[0], 1);
700 tscrolldown(escseq.arg[0]);
702 case 'L': /* IL -- Insert <n> blank lines */
703 DEFAULT(escseq.arg[0], 1);
704 tinsertblankline(escseq.arg[0]);
706 case 'l': /* RM -- Reset Mode */
708 switch(escseq.arg[0]) {
710 term.mode &= ~MODE_APPKEYPAD;
713 term.mode &= ~MODE_WRAP;
715 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
718 term.c.state |= CURSOR_HIDE;
720 case 1048: /* XXX: no alt. screen to erase/save */
722 tcursor(CURSOR_LOAD);
723 tclearregion(0, 0, term.col-1, term.row-1);
729 switch(escseq.arg[0]) {
731 term.mode &= ~MODE_INSERT;
738 case 'M': /* DL -- Delete <n> lines */
739 DEFAULT(escseq.arg[0], 1);
740 tdeleteline(escseq.arg[0]);
742 case 'X': /* ECH -- Erase <n> char */
743 DEFAULT(escseq.arg[0], 1);
744 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
746 case 'P': /* DCH -- Delete <n> char */
747 DEFAULT(escseq.arg[0], 1);
748 tdeletechar(escseq.arg[0]);
750 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
751 case 'd': /* VPA -- Move to <row> */
752 DEFAULT(escseq.arg[0], 1);
753 tmoveto(term.c.x, escseq.arg[0]-1);
755 case 'h': /* SM -- Set terminal mode */
757 switch(escseq.arg[0]) {
759 term.mode |= MODE_APPKEYPAD;
762 term.mode |= MODE_WRAP;
764 case 12: /* att610 -- Start blinking cursor (IGNORED) */
767 term.c.state &= ~CURSOR_HIDE;
770 case 1049: /* XXX: no alt. screen to erase/save */
771 tcursor(CURSOR_SAVE);
772 tclearregion(0, 0, term.col-1, term.row-1);
774 default: goto unknown;
777 switch(escseq.arg[0]) {
779 term.mode |= MODE_INSERT;
781 default: goto unknown;
785 case 'm': /* SGR -- Terminal attribute (color) */
786 tsetattr(escseq.arg, escseq.narg);
788 case 'r': /* DECSTBM -- Set Scrolling Region */
792 DEFAULT(escseq.arg[0], 1);
793 DEFAULT(escseq.arg[1], term.row);
794 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
798 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
799 tcursor(CURSOR_SAVE);
801 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
802 tcursor(CURSOR_LOAD);
810 printf("ESC [ %s", escseq.priv ? "? " : "");
812 for(i = 0; i < escseq.narg; i++)
813 printf("%d ", escseq.arg[i]);
815 putchar(escseq.mode);
821 memset(&escseq, 0, sizeof(escseq));
826 int space = TAB - term.c.x % TAB;
827 tmoveto(term.c.x + space, term.c.y);
832 if(term.esc & ESC_START) {
833 if(term.esc & ESC_CSI) {
834 escseq.buf[escseq.len++] = c;
835 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
837 csiparse(), csihandle();
839 } else if(term.esc & ESC_OSC) {
842 term.esc = ESC_START | ESC_TITLE;
844 } else if(term.esc & ESC_TITLE) {
845 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
847 term.title[term.titlelen] = '\0';
848 XStoreName(xw.dis, xw.win, term.title);
850 term.title[term.titlelen++] = c;
852 } else if(term.esc & ESC_ALTCHARSET) {
854 case '0': /* Line drawing crap */
855 term.c.attr.mode |= ATTR_GFX;
857 case 'B': /* Back to regular text */
858 term.c.attr.mode &= ~ATTR_GFX;
861 printf("esc unhandled charset: ESC ( %c\n", c);
873 term.esc |= ESC_ALTCHARSET;
875 case 'D': /* IND -- Linefeed */
876 if(term.c.y == term.bot)
879 tmoveto(term.c.x, term.c.y+1);
882 case 'E': /* NEL -- Next line */
886 case 'M': /* RI -- Reverse index */
887 if(term.c.y == term.top)
890 tmoveto(term.c.x, term.c.y-1);
893 case 'c': /* RIS -- Reset to inital state */
897 case '=': /* DECPAM */
898 term.mode |= MODE_APPKEYPAD;
901 case '>': /* DECPNM */
902 term.mode &= ~MODE_APPKEYPAD;
906 tcursor(CURSOR_SAVE);
910 tcursor(CURSOR_LOAD);
914 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
924 tmoveto(term.c.x-1, term.c.y);
927 tmoveto(0, term.c.y);
937 term.esc = ESC_START;
940 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
943 if(term.c.x+1 < term.col)
944 tmoveto(term.c.x+1, term.c.y);
946 term.c.state |= CURSOR_WRAPNEXT;
953 tputs(char *s, int len) {
954 for(; len > 0; len--)
959 tresize(int col, int row) {
961 int minrow = MIN(row, term.row);
962 int mincol = MIN(col, term.col);
964 if(col < 1 || row < 1)
967 /* free uneeded rows */
968 for(i = row; i < term.row; i++)
971 /* resize to new height */
972 term.line = realloc(term.line, row * sizeof(Line));
974 /* resize each row to new width, zero-pad if needed */
975 for(i = 0; i < minrow; i++) {
976 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
977 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
980 /* allocate any new rows */
981 for(/* i == minrow */; i < row; i++)
982 term.line[i] = calloc(col, sizeof(Glyph));
984 /* update terminal size */
985 term.col = col, term.row = row;
986 /* make use of the LIMIT in tmoveto */
987 tmoveto(term.c.x, term.c.y);
988 /* reset scrolling region */
989 tsetscroll(0, row-1);
996 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
997 unsigned long white = WhitePixel(xw.dis, xw.scr);
999 for(i = 0; i < 16; i++) {
1000 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1002 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1004 dc.col[i] = color.pixel;
1007 /* same colors as xterm */
1008 for(r = 0; r < 6; r++)
1009 for(g = 0; g < 6; g++)
1010 for(b = 0; b < 6; b++) {
1011 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1012 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1013 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1014 if (!XAllocColor(xw.dis, cmap, &color)) {
1016 fprintf(stderr, "Could not allocate color %d\n", i);
1018 dc.col[i] = color.pixel;
1022 for(r = 0; r < 24; r++, i++) {
1023 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1024 if (!XAllocColor(xw.dis, cmap, &color)) {
1026 fprintf(stderr, "Could not allocate color %d\n", i);
1028 dc.col[i] = color.pixel;
1033 xclear(int x1, int y1, int x2, int y2) {
1034 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1035 XFillRectangle(xw.dis, xw.buf, dc.gc,
1036 x1 * xw.cw, y1 * xw.ch,
1037 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1043 XClassHint class = {TNAME, TNAME};
1044 XWMHints wm = {.flags = InputHint, .input = 1};
1046 .flags = PSize | PResizeInc | PBaseSize,
1049 .height_inc = xw.ch,
1051 .base_height = 2*BORDER,
1052 .base_width = 2*BORDER,
1054 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1059 if(!(xw.dis = XOpenDisplay(NULL)))
1060 die("Can't open display\n");
1061 xw.scr = XDefaultScreen(xw.dis);
1064 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1065 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1067 /* XXX: Assuming same size for bold font */
1068 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1069 xw.ch = dc.font->ascent + dc.font->descent;
1075 xw.h = term.row * xw.ch + 2*BORDER;
1076 xw.w = term.col * xw.cw + 2*BORDER;
1077 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1081 xw.bufw = xw.w - 2*BORDER;
1082 xw.bufh = xw.h - 2*BORDER;
1083 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1085 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1086 XMapWindow(xw.dis, xw.win);
1088 XStoreName(xw.dis, xw.win, "st");
1093 xdraws(char *s, Glyph base, int x, int y, int len) {
1094 unsigned long xfg, xbg;
1095 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1098 if(base.mode & ATTR_REVERSE)
1099 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1101 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1103 XSetBackground(xw.dis, dc.gc, xbg);
1104 XSetForeground(xw.dis, dc.gc, xfg);
1106 if(base.mode & ATTR_GFX)
1107 for(i = 0; i < len; i++)
1108 s[i] = gfx[(int)s[i]];
1110 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1111 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1113 if(base.mode & ATTR_UNDERLINE)
1114 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1119 static int oldx = 0;
1120 static int oldy = 0;
1121 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1123 LIMIT(oldx, 0, term.col-1);
1124 LIMIT(oldy, 0, term.row-1);
1126 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1127 g.c = term.line[term.c.y][term.c.x].c;
1129 /* remove the old cursor */
1130 if(term.line[oldy][oldx].state & GLYPH_SET)
1131 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1133 xclear(oldx, oldy, oldx, oldy);
1135 /* draw the new one */
1136 if(!(term.c.state & CURSOR_HIDE)) {
1137 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1138 oldx = term.c.x, oldy = term.c.y;
1143 /* basic drawing routines */
1145 xdrawc(int x, int y, Glyph g) {
1146 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1147 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1148 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1149 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1150 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1157 xclear(0, 0, term.col-1, term.row-1);
1158 for(y = 0; y < term.row; y++)
1159 for(x = 0; x < term.col; x++)
1160 if(term.line[y][x].state & GLYPH_SET)
1161 xdrawc(x, y, term.line[y][x]);
1164 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1169 /* optimized drawing routine */
1171 draw(int redraw_all) {
1174 char buf[DRAW_BUF_SIZ];
1176 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1177 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
1178 for(y = 0; y < term.row; y++) {
1179 base = term.line[y][0];
1181 for(x = 0; x < term.col; x++) {
1182 new = term.line[y][x];
1183 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1184 i >= DRAW_BUF_SIZ)) {
1185 xdraws(buf, base, ox, y, i);
1188 if(new.state & GLYPH_SET) {
1197 xdraws(buf, base, ox, y, i);
1200 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1207 expose(XEvent *ev) {
1208 draw(SCREEN_REDRAW);
1214 for(i = 0; i < LEN(key); i++)
1216 return (char*)key[i].s;
1221 kpress(XEvent *ev) {
1222 XKeyEvent *e = &ev->xkey;
1230 meta = e->state & Mod1Mask;
1231 shift = e->state & ShiftMask;
1232 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1234 if((customkey = kmap(ksym)))
1235 ttywrite(customkey, strlen(customkey));
1237 buf[sizeof(buf)-1] = '\0';
1238 if(meta && len == 1)
1239 ttywrite("\033", 1);
1247 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1252 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1255 fprintf(stderr, "errkey: %d\n", (int)ksym);
1264 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1267 xw.w = e->xconfigure.width;
1268 xw.h = e->xconfigure.height;
1269 xw.bufw = xw.w - 2*BORDER;
1270 xw.bufh = xw.h - 2*BORDER;
1271 col = xw.bufw / xw.cw;
1272 row = xw.bufh / xw.ch;
1274 ttyresize(col, row);
1275 XFreePixmap(xw.dis, xw.buf);
1276 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1277 draw(SCREEN_REDRAW);
1284 int xfd = XConnectionNumber(xw.dis);
1287 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1288 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1292 FD_SET(cmdfd, &rfd);
1294 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1297 die("select failed: %s\n", SERRNO);
1299 if(FD_ISSET(cmdfd, &rfd)) {
1301 draw(SCREEN_UPDATE);
1303 while(XPending(xw.dis)) {
1304 XNextEvent(xw.dis, &ev);
1305 if(handler[ev.type])
1306 (handler[ev.type])(&ev);
1312 main(int argc, char *argv[]) {
1313 if(argc == 2 && !strncmp("-v", argv[1], 3))
1314 die("st-" VERSION ", (c) 2010 st engineers\n");
1316 die("usage: st [-v]\n");
1317 setlocale(LC_CTYPE, "");