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__) || defined(__NetBSD__)
27 #elif defined(__FreeBSD__) || defined(__DragonFly__)
32 "st-" VERSION ", (c) 2010 st engineers\n" \
33 "usage: st [-t title] [-e cmd] [-v]\n"
36 #define ESC_TITLE_SIZ 256
37 #define ESC_BUF_SIZ 256
38 #define ESC_ARG_SIZ 16
39 #define DRAW_BUF_SIZ 1024
41 #define SERRNO strerror(errno)
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 #define MAX(a, b) ((a) < (b) ? (b) : (a))
44 #define LEN(a) (sizeof(a) / sizeof(a[0]))
45 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
46 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
47 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
48 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
49 #define IS_SET(flag) (term.mode & (flag))
51 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
52 enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
53 enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
54 CURSOR_SAVE, CURSOR_LOAD };
55 enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
56 enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
57 enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8 };
58 enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
59 enum { SCREEN_UPDATE, SCREEN_REDRAW };
62 char c; /* character code */
63 char mode; /* attribute flags */
64 int fg; /* foreground */
65 int bg; /* background */
66 char state; /* state flags */
72 Glyph attr; /* current char attributes */
78 /* CSI Escape sequence structs */
79 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
81 char buf[ESC_BUF_SIZ]; /* raw string */
82 int len; /* raw string length */
85 int narg; /* nb of args */
89 /* Internal representation of the screen */
93 Line* line; /* screen */
94 Line* alt; /* alternate screen */
95 TCursor c; /* cursor */
96 int top; /* top scroll limit */
97 int bot; /* bottom scroll limit */
98 int mode; /* terminal mode flags */
99 int esc; /* escape state flags */
100 char title[ESC_TITLE_SIZ];
104 /* Purely graphic info */
111 int w; /* window width */
112 int h; /* window height */
113 int bufw; /* pixmap width */
114 int bufh; /* pixmap height */
115 int ch; /* char height */
116 int cw; /* char width */
118 int vis; /* is visible */
126 /* Drawing Context */
128 unsigned long col[256];
134 /* TODO: use better name for vars... */
139 struct {int x, y;} b, e;
145 static void die(const char *errstr, ...);
146 static void draw(int);
147 static void execsh(void);
148 static void sigchld(int);
149 static void run(void);
151 static void csidump(void);
152 static void csihandle(void);
153 static void csiparse(void);
154 static void csireset(void);
156 static void tclearregion(int, int, int, int);
157 static void tcursor(int);
158 static void tdeletechar(int);
159 static void tdeleteline(int);
160 static void tinsertblank(int);
161 static void tinsertblankline(int);
162 static void tmoveto(int, int);
163 static void tnew(int, int);
164 static void tnewline(void);
165 static void tputtab(void);
166 static void tputc(char);
167 static void tputs(char*, int);
168 static void treset(void);
169 static void tresize(int, int);
170 static void tscrollup(int, int);
171 static void tscrolldown(int, int);
172 static void tsetattr(int*, int);
173 static void tsetchar(char);
174 static void tsetscroll(int, int);
175 static void tswapscreen(void);
177 static void ttynew(void);
178 static void ttyread(void);
179 static void ttyresize(int, int);
180 static void ttywrite(const char *, size_t);
182 static void xdraws(char *, Glyph, int, int, int);
183 static void xhints(void);
184 static void xclear(int, int, int, int);
185 static void xdrawcursor(void);
186 static void xinit(void);
187 static void xloadcols(void);
188 static void xseturgency(int);
190 static void expose(XEvent *);
191 static void visibility(XEvent *);
192 static void unmap(XEvent *);
193 static char* kmap(KeySym);
194 static void kpress(XEvent *);
195 static void resize(XEvent *);
196 static void focus(XEvent *);
197 static void brelease(XEvent *);
198 static void bpress(XEvent *);
199 static void bmotion(XEvent *);
202 static void (*handler[LASTEvent])(XEvent *) = {
204 [ConfigureNotify] = resize,
205 [VisibilityNotify] = visibility,
206 [UnmapNotify] = unmap,
210 [MotionNotify] = bmotion,
211 [ButtonPress] = bpress,
212 [ButtonRelease] = brelease,
219 static CSIEscape escseq;
222 static Selection sel;
223 static char *opt_cmd = NULL;
224 static char *opt_title = NULL;
233 static inline int selected(int x, int y) {
234 if(sel.ey == y && sel.by == y) {
235 int bx = MIN(sel.bx, sel.ex);
236 int ex = MAX(sel.bx, sel.ex);
237 return BETWEEN(x, bx, ex);
239 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
240 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
243 static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
244 if(b) *b = e->xbutton.state,
245 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
246 *x = e->xbutton.x/xw.cw;
247 *y = e->xbutton.y/xw.ch;
248 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
249 sel.b.y = MIN(sel.by, sel.ey);
250 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
251 sel.e.y = MAX(sel.by, sel.ey);
254 static void bpress(XEvent *e) {
256 sel.ex = sel.bx = e->xbutton.x/xw.cw;
257 sel.ey = sel.by = e->xbutton.y/xw.ch;
260 static char *getseltext() {
265 sz = (term.col+1) * (sel.e.y-sel.b.y+1);
266 ptr = str = malloc(sz);
267 for(y = 0; y < term.row; y++) {
268 for(x = 0; x < term.col; x++)
269 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y)))
270 *ptr = term.line[y][x].c, ptr++;
278 /* TODO: use X11 clipboard */
279 static void selcopy(char *str) {
284 static void selpaste() {
286 ttywrite(sel.clip, strlen(sel.clip));
289 /* TODO: doubleclick to select word */
290 static void brelease(XEvent *e) {
293 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
294 if(sel.bx==sel.ex && sel.by==sel.ey) {
300 selcopy(getseltext());
305 static void bmotion(XEvent *e) {
307 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
318 for(row = 0; row < term.row; row++) {
319 for(col = 0; col < term.col; col++) {
320 if(col == term.c.x && row == term.c.y)
323 c = term.line[row][col];
324 putchar(c.state & GLYPH_SET ? c.c : '.');
333 die(const char *errstr, ...) {
336 va_start(ap, errstr);
337 vfprintf(stderr, errstr, ap);
344 char *args[] = {getenv("SHELL"), "-i", NULL};
346 args[0] = opt_cmd, args[1] = NULL;
348 DEFAULT(args[0], SHELL);
349 putenv("TERM="TNAME);
350 execvp(args[0], args);
356 if(waitpid(pid, &stat, 0) < 0)
357 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
359 exit(WEXITSTATUS(stat));
368 /* seems to work fine on linux, openbsd and freebsd */
369 struct winsize w = {term.row, term.col, 0, 0};
370 if(openpty(&m, &s, NULL, NULL, &w) < 0)
371 die("openpty failed: %s\n", SERRNO);
373 switch(pid = fork()) {
375 die("fork failed\n");
378 setsid(); /* create a new process group */
379 dup2(s, STDIN_FILENO);
380 dup2(s, STDOUT_FILENO);
381 dup2(s, STDERR_FILENO);
382 if(ioctl(s, TIOCSCTTY, NULL) < 0)
383 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
391 signal(SIGCHLD, sigchld);
398 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
400 fprintf(stderr, "\n");
408 if((ret = read(cmdfd, buf, LEN(buf))) < 0)
409 die("Couldn't read from shell: %s\n", SERRNO);
415 ttywrite(const char *s, size_t n) {
416 if(write(cmdfd, s, n) == -1)
417 die("write error on tty: %s\n", SERRNO);
421 ttyresize(int x, int y) {
426 w.ws_xpixel = w.ws_ypixel = 0;
427 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
428 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
435 if(mode == CURSOR_SAVE)
437 else if(mode == CURSOR_LOAD)
438 term.c = c, tmoveto(c.x, c.y);
447 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
449 term.top = 0, term.bot = term.row - 1;
450 term.mode = MODE_WRAP;
451 tclearregion(0, 0, term.col-1, term.row-1);
455 tnew(int col, int row) {
456 /* set screen size */
457 term.row = row, term.col = col;
458 term.line = malloc(term.row * sizeof(Line));
459 term.alt = malloc(term.row * sizeof(Line));
460 for(row = 0 ; row < term.row; row++) {
461 term.line[row] = malloc(term.col * sizeof(Glyph));
462 term.alt [row] = malloc(term.col * sizeof(Glyph));
470 Line* tmp = term.line;
471 term.line = term.alt;
473 term.mode ^= MODE_ALTSCREEN;
477 tscrolldown(int orig, int n) {
481 LIMIT(n, 0, term.bot-orig+1);
483 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
485 for(i = term.bot; i >= orig+n; i--) {
487 term.line[i] = term.line[i-n];
488 term.line[i-n] = temp;
493 tscrollup(int orig, int n) {
496 LIMIT(n, 0, term.bot-orig+1);
498 tclearregion(0, orig, term.col-1, orig+n-1);
500 for(i = orig; i <= term.bot-n; i++) {
502 term.line[i] = term.line[i+n];
503 term.line[i+n] = temp;
510 if(term.c.y == term.bot)
511 tscrollup(term.top, 1);
520 char *p = escseq.buf;
524 escseq.priv = 1, p++;
526 while(p < escseq.buf+escseq.len) {
528 escseq.arg[escseq.narg] *= 10;
529 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
531 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
542 tmoveto(int x, int y) {
543 LIMIT(x, 0, term.col-1);
544 LIMIT(y, 0, term.row-1);
545 term.c.state &= ~CURSOR_WRAPNEXT;
552 term.line[term.c.y][term.c.x] = term.c.attr;
553 term.line[term.c.y][term.c.x].c = c;
554 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
558 tclearregion(int x1, int y1, int x2, int y2) {
562 temp = x1, x1 = x2, x2 = temp;
564 temp = y1, y1 = y2, y2 = temp;
566 LIMIT(x1, 0, term.col-1);
567 LIMIT(x2, 0, term.col-1);
568 LIMIT(y1, 0, term.row-1);
569 LIMIT(y2, 0, term.row-1);
571 for(y = y1; y <= y2; y++)
572 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
577 int src = term.c.x + n;
579 int size = term.col - src;
581 if(src >= term.col) {
582 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
585 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
586 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
590 tinsertblank(int n) {
593 int size = term.col - dst;
595 if(dst >= term.col) {
596 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
599 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
600 tclearregion(src, term.c.y, dst - 1, term.c.y);
604 tinsertblankline(int n) {
605 if(term.c.y < term.top || term.c.y > term.bot)
608 tscrolldown(term.c.y, n);
613 if(term.c.y < term.top || term.c.y > term.bot)
616 tscrollup(term.c.y, n);
620 tsetattr(int *attr, int l) {
623 for(i = 0; i < l; i++) {
626 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
627 term.c.attr.fg = DefaultFG;
628 term.c.attr.bg = DefaultBG;
631 term.c.attr.mode |= ATTR_BOLD;
634 term.c.attr.mode |= ATTR_UNDERLINE;
637 term.c.attr.mode |= ATTR_REVERSE;
640 term.c.attr.mode &= ~ATTR_BOLD;
643 term.c.attr.mode &= ~ATTR_UNDERLINE;
646 term.c.attr.mode &= ~ATTR_REVERSE;
649 if (i + 2 < l && attr[i + 1] == 5) {
651 if (BETWEEN(attr[i], 0, 255))
652 term.c.attr.fg = attr[i];
654 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
657 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
660 term.c.attr.fg = DefaultFG;
663 if (i + 2 < l && attr[i + 1] == 5) {
665 if (BETWEEN(attr[i], 0, 255))
666 term.c.attr.bg = attr[i];
668 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
671 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
674 term.c.attr.bg = DefaultBG;
677 if(BETWEEN(attr[i], 30, 37))
678 term.c.attr.fg = attr[i] - 30;
679 else if(BETWEEN(attr[i], 40, 47))
680 term.c.attr.bg = attr[i] - 40;
681 else if(BETWEEN(attr[i], 90, 97))
682 term.c.attr.fg = attr[i] - 90 + 8;
683 else if(BETWEEN(attr[i], 100, 107))
684 term.c.attr.fg = attr[i] - 100 + 8;
686 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
694 tsetscroll(int t, int b) {
697 LIMIT(t, 0, term.row-1);
698 LIMIT(b, 0, term.row-1);
710 switch(escseq.mode) {
713 printf("erresc: unknown csi ");
717 case '@': /* ICH -- Insert <n> blank char */
718 DEFAULT(escseq.arg[0], 1);
719 tinsertblank(escseq.arg[0]);
721 case 'A': /* CUU -- Cursor <n> Up */
723 DEFAULT(escseq.arg[0], 1);
724 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
726 case 'B': /* CUD -- Cursor <n> Down */
727 DEFAULT(escseq.arg[0], 1);
728 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
730 case 'C': /* CUF -- Cursor <n> Forward */
732 DEFAULT(escseq.arg[0], 1);
733 tmoveto(term.c.x+escseq.arg[0], term.c.y);
735 case 'D': /* CUB -- Cursor <n> Backward */
736 DEFAULT(escseq.arg[0], 1);
737 tmoveto(term.c.x-escseq.arg[0], term.c.y);
739 case 'E': /* CNL -- Cursor <n> Down and first col */
740 DEFAULT(escseq.arg[0], 1);
741 tmoveto(0, term.c.y+escseq.arg[0]);
743 case 'F': /* CPL -- Cursor <n> Up and first col */
744 DEFAULT(escseq.arg[0], 1);
745 tmoveto(0, term.c.y-escseq.arg[0]);
747 case 'G': /* CHA -- Move to <col> */
748 case '`': /* XXX: HPA -- same? */
749 DEFAULT(escseq.arg[0], 1);
750 tmoveto(escseq.arg[0]-1, term.c.y);
752 case 'H': /* CUP -- Move to <row> <col> */
753 case 'f': /* XXX: HVP -- same? */
754 DEFAULT(escseq.arg[0], 1);
755 DEFAULT(escseq.arg[1], 1);
756 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
758 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
759 case 'J': /* ED -- Clear screen */
760 switch(escseq.arg[0]) {
762 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
765 tclearregion(0, 0, term.c.x, term.c.y);
768 tclearregion(0, 0, term.col-1, term.row-1);
774 case 'K': /* EL -- Clear line */
775 switch(escseq.arg[0]) {
777 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
780 tclearregion(0, term.c.y, term.c.x, term.c.y);
783 tclearregion(0, term.c.y, term.col-1, term.c.y);
787 case 'S': /* SU -- Scroll <n> line up */
788 DEFAULT(escseq.arg[0], 1);
789 tscrollup(term.top, escseq.arg[0]);
791 case 'T': /* SD -- Scroll <n> line down */
792 DEFAULT(escseq.arg[0], 1);
793 tscrolldown(term.top, escseq.arg[0]);
795 case 'L': /* IL -- Insert <n> blank lines */
796 DEFAULT(escseq.arg[0], 1);
797 tinsertblankline(escseq.arg[0]);
799 case 'l': /* RM -- Reset Mode */
801 switch(escseq.arg[0]) {
803 term.mode &= ~MODE_APPKEYPAD;
805 case 5: /* TODO: DECSCNM -- Remove reverse video */
808 term.mode &= ~MODE_WRAP;
810 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
813 term.c.state |= CURSOR_HIDE;
815 case 1049: /* = 1047 and 1048 */
817 if(IS_SET(MODE_ALTSCREEN)) {
818 tclearregion(0, 0, term.col-1, term.row-1);
821 if(escseq.arg[0] == 1047)
824 tcursor(CURSOR_LOAD);
830 switch(escseq.arg[0]) {
832 term.mode &= ~MODE_INSERT;
839 case 'M': /* DL -- Delete <n> lines */
840 DEFAULT(escseq.arg[0], 1);
841 tdeleteline(escseq.arg[0]);
843 case 'X': /* ECH -- Erase <n> char */
844 DEFAULT(escseq.arg[0], 1);
845 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
847 case 'P': /* DCH -- Delete <n> char */
848 DEFAULT(escseq.arg[0], 1);
849 tdeletechar(escseq.arg[0]);
851 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
852 case 'd': /* VPA -- Move to <row> */
853 DEFAULT(escseq.arg[0], 1);
854 tmoveto(term.c.x, escseq.arg[0]-1);
856 case 'h': /* SM -- Set terminal mode */
858 switch(escseq.arg[0]) {
860 term.mode |= MODE_APPKEYPAD;
862 case 5: /* DECSCNM -- Reverve video */
863 /* TODO: set REVERSE on the whole screen (f) */
866 term.mode |= MODE_WRAP;
868 case 12: /* att610 -- Start blinking cursor (IGNORED) */
869 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
870 if(escseq.narg > 1 && escseq.arg[1] != 25)
873 term.c.state &= ~CURSOR_HIDE;
875 case 1049: /* = 1047 and 1048 */
877 if(IS_SET(MODE_ALTSCREEN))
878 tclearregion(0, 0, term.col-1, term.row-1);
881 if(escseq.arg[0] == 1047)
884 tcursor(CURSOR_SAVE);
886 default: goto unknown;
889 switch(escseq.arg[0]) {
891 term.mode |= MODE_INSERT;
893 default: goto unknown;
897 case 'm': /* SGR -- Terminal attribute (color) */
898 tsetattr(escseq.arg, escseq.narg);
900 case 'r': /* DECSTBM -- Set Scrolling Region */
904 DEFAULT(escseq.arg[0], 1);
905 DEFAULT(escseq.arg[1], term.row);
906 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
910 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
911 tcursor(CURSOR_SAVE);
913 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
914 tcursor(CURSOR_LOAD);
922 printf("ESC [ %s", escseq.priv ? "? " : "");
924 for(i = 0; i < escseq.narg; i++)
925 printf("%d ", escseq.arg[i]);
927 putchar(escseq.mode);
933 memset(&escseq, 0, sizeof(escseq));
938 int space = TAB - term.c.x % TAB;
939 tmoveto(term.c.x + space, term.c.y);
944 if(term.esc & ESC_START) {
945 if(term.esc & ESC_CSI) {
946 escseq.buf[escseq.len++] = c;
947 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
949 csiparse(), csihandle();
951 /* TODO: handle other OSC */
952 } else if(term.esc & ESC_OSC) {
955 term.esc = ESC_START | ESC_TITLE;
957 } else if(term.esc & ESC_TITLE) {
958 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
960 term.title[term.titlelen] = '\0';
961 XStoreName(xw.dis, xw.win, term.title);
963 term.title[term.titlelen++] = c;
965 } else if(term.esc & ESC_ALTCHARSET) {
967 case '0': /* Line drawing crap */
968 term.c.attr.mode |= ATTR_GFX;
970 case 'B': /* Back to regular text */
971 term.c.attr.mode &= ~ATTR_GFX;
974 printf("esc unhandled charset: ESC ( %c\n", c);
986 term.esc |= ESC_ALTCHARSET;
988 case 'D': /* IND -- Linefeed */
989 if(term.c.y == term.bot)
990 tscrollup(term.top, 1);
992 tmoveto(term.c.x, term.c.y+1);
995 case 'E': /* NEL -- Next line */
999 case 'M': /* RI -- Reverse index */
1000 if(term.c.y == term.top)
1001 tscrolldown(term.top, 1);
1003 tmoveto(term.c.x, term.c.y-1);
1006 case 'c': /* RIS -- Reset to inital state */
1010 case '=': /* DECPAM -- Application keypad */
1011 term.mode |= MODE_APPKEYPAD;
1014 case '>': /* DECPNM -- Normal keypad */
1015 term.mode &= ~MODE_APPKEYPAD;
1018 case '7': /* DECSC -- Save Cursor */
1019 tcursor(CURSOR_SAVE);
1022 case '8': /* DECRC -- Restore Cursor */
1023 tcursor(CURSOR_LOAD);
1027 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1037 tmoveto(term.c.x-1, term.c.y);
1040 tmoveto(0, term.c.y);
1051 term.esc = ESC_START;
1054 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1057 if(term.c.x+1 < term.col)
1058 tmoveto(term.c.x+1, term.c.y);
1060 term.c.state |= CURSOR_WRAPNEXT;
1067 tputs(char *s, int len) {
1068 for(; len > 0; len--)
1073 tresize(int col, int row) {
1075 int minrow = MIN(row, term.row);
1076 int mincol = MIN(col, term.col);
1077 int slide = term.c.y - row + 1;
1079 if(col < 1 || row < 1)
1082 /* free unneeded rows */
1085 /* slide screen to keep cursor where we expect it -
1086 * tscrollup would work here, but we can optimize to
1087 * memmove because we're freeing the earlier lines */
1088 for(/* i = 0 */; i < slide; i++) {
1092 memmove(term.line, term.line + slide, row * sizeof(Line));
1093 memmove(term.alt, term.alt + slide, row * sizeof(Line));
1095 for(i += row; i < term.row; i++) {
1100 /* resize to new height */
1101 term.line = realloc(term.line, row * sizeof(Line));
1102 term.alt = realloc(term.alt, row * sizeof(Line));
1104 /* resize each row to new width, zero-pad if needed */
1105 for(i = 0; i < minrow; i++) {
1106 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1107 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1108 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1109 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1112 /* allocate any new rows */
1113 for(/* i == minrow */; i < row; i++) {
1114 term.line[i] = calloc(col, sizeof(Glyph));
1115 term.alt [i] = calloc(col, sizeof(Glyph));
1118 /* update terminal size */
1119 term.col = col, term.row = row;
1120 /* make use of the LIMIT in tmoveto */
1121 tmoveto(term.c.x, term.c.y);
1122 /* reset scrolling region */
1123 tsetscroll(0, row-1);
1130 unsigned long white = WhitePixel(xw.dis, xw.scr);
1132 for(i = 0; i < 16; i++) {
1133 if (!XAllocNamedColor(xw.dis, xw.cmap, colorname[i], &color, &color)) {
1135 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1137 dc.col[i] = color.pixel;
1140 /* same colors as xterm */
1141 for(r = 0; r < 6; r++)
1142 for(g = 0; g < 6; g++)
1143 for(b = 0; b < 6; b++) {
1144 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1145 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1146 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1147 if (!XAllocColor(xw.dis, xw.cmap, &color)) {
1149 fprintf(stderr, "Could not allocate color %d\n", i);
1151 dc.col[i] = color.pixel;
1155 for(r = 0; r < 24; r++, i++) {
1156 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1157 if (!XAllocColor(xw.dis, xw.cmap, &color)) {
1159 fprintf(stderr, "Could not allocate color %d\n", i);
1161 dc.col[i] = color.pixel;
1166 xclear(int x1, int y1, int x2, int y2) {
1167 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1168 XFillRectangle(xw.dis, xw.buf, dc.gc,
1169 x1 * xw.cw, y1 * xw.ch,
1170 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1176 XClassHint class = {TNAME, TNAME};
1177 XWMHints wm = {.flags = InputHint, .input = 1};
1179 .flags = PSize | PResizeInc | PBaseSize,
1182 .height_inc = xw.ch,
1184 .base_height = 2*BORDER,
1185 .base_width = 2*BORDER,
1187 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1192 XSetWindowAttributes attrs;
1194 if(!(xw.dis = XOpenDisplay(NULL)))
1195 die("Can't open display\n");
1196 xw.scr = XDefaultScreen(xw.dis);
1199 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1200 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1202 /* XXX: Assuming same size for bold font */
1203 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1204 xw.ch = dc.font->ascent + dc.font->descent;
1207 xw.cmap = XDefaultColormap(xw.dis, xw.scr);
1210 /* window - default size */
1211 xw.bufh = 24 * xw.ch;
1212 xw.bufw = 80 * xw.cw;
1213 xw.h = xw.bufh + 2*BORDER;
1214 xw.w = xw.bufw + 2*BORDER;
1216 attrs.background_pixel = dc.col[DefaultBG];
1217 attrs.border_pixel = dc.col[DefaultBG];
1218 attrs.bit_gravity = NorthWestGravity;
1219 attrs.event_mask = FocusChangeMask | KeyPressMask
1220 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1221 | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
1222 attrs.colormap = xw.cmap;
1224 xw.win = XCreateWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1225 xw.w, xw.h, 0, XDefaultDepth(xw.dis, xw.scr), InputOutput,
1226 XDefaultVisual(xw.dis, xw.scr),
1227 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
1230 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1232 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1234 XMapWindow(xw.dis, xw.win);
1236 XStoreName(xw.dis, xw.win, opt_title ? opt_title : "st");
1241 xdraws(char *s, Glyph base, int x, int y, int len) {
1242 unsigned long xfg, xbg;
1243 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1246 if(base.mode & ATTR_REVERSE)
1247 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1249 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1251 XSetBackground(xw.dis, dc.gc, xbg);
1252 XSetForeground(xw.dis, dc.gc, xfg);
1254 if(base.mode & ATTR_GFX)
1255 for(i = 0; i < len; i++) {
1256 char c = gfx[(unsigned int)s[i] % 256];
1259 else if(s[i] > 0x5f)
1263 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1264 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1266 if(base.mode & ATTR_UNDERLINE)
1267 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1272 static int oldx = 0;
1273 static int oldy = 0;
1274 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1276 LIMIT(oldx, 0, term.col-1);
1277 LIMIT(oldy, 0, term.row-1);
1279 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1280 g.c = term.line[term.c.y][term.c.x].c;
1282 /* remove the old cursor */
1283 if(term.line[oldy][oldx].state & GLYPH_SET)
1284 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1286 xclear(oldx, oldy, oldx, oldy);
1288 /* draw the new one */
1289 if(!(term.c.state & CURSOR_HIDE) && xw.focus) {
1290 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1291 oldx = term.c.x, oldy = term.c.y;
1296 /* basic drawing routines */
1298 xdrawc(int x, int y, Glyph g) {
1299 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1300 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1301 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1302 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1303 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1310 xclear(0, 0, term.col-1, term.row-1);
1311 for(y = 0; y < term.row; y++)
1312 for(x = 0; x < term.col; x++)
1313 if(term.line[y][x].state & GLYPH_SET)
1314 xdrawc(x, y, term.line[y][x]);
1317 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1322 /* optimized drawing routine */
1324 draw(int redraw_all) {
1327 char buf[DRAW_BUF_SIZ];
1332 xclear(0, 0, term.col-1, term.row-1);
1333 for(y = 0; y < term.row; y++) {
1334 base = term.line[y][0];
1336 for(x = 0; x < term.col; x++) {
1337 new = term.line[y][x];
1338 if(sel.bx!=-1 && new.c && selected(x, y))
1339 new.mode ^= ATTR_REVERSE;
1340 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1341 i >= DRAW_BUF_SIZ)) {
1342 xdraws(buf, base, ox, y, i);
1345 if(new.state & GLYPH_SET) {
1354 xdraws(buf, base, ox, y, i);
1357 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1364 expose(XEvent *ev) {
1365 draw(SCREEN_REDRAW);
1369 visibility(XEvent *ev) {
1370 XVisibilityEvent *e = &ev->xvisibility;
1371 /* XXX if this goes from 0 to 1, need a full redraw for next Expose,
1372 * not just a buf copy */
1373 xw.vis = e->state != VisibilityFullyObscured;
1382 xseturgency(int add) {
1383 XWMHints *h = XGetWMHints(xw.dis, xw.win);
1384 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1385 XSetWMHints(xw.dis, xw.win, h);
1391 if((xw.focus = ev->type == FocusIn))
1393 draw(SCREEN_UPDATE);
1399 for(i = 0; i < LEN(key); i++)
1401 return (char*)key[i].s;
1406 kpress(XEvent *ev) {
1407 XKeyEvent *e = &ev->xkey;
1415 meta = e->state & Mod1Mask;
1416 shift = e->state & ShiftMask;
1417 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1419 if((customkey = kmap(ksym)))
1420 ttywrite(customkey, strlen(customkey));
1422 buf[sizeof(buf)-1] = '\0';
1423 if(meta && len == 1)
1424 ttywrite("\033", 1);
1432 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1437 selpaste(), draw(1);
1440 fprintf(stderr, "errkey: %d\n", (int)ksym);
1449 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1452 xw.w = e->xconfigure.width;
1453 xw.h = e->xconfigure.height;
1454 xw.bufw = xw.w - 2*BORDER;
1455 xw.bufh = xw.h - 2*BORDER;
1456 col = xw.bufw / xw.cw;
1457 row = xw.bufh / xw.ch;
1459 ttyresize(col, row);
1460 xw.bufh = MAX(1, xw.bufh);
1461 xw.bufw = MAX(1, xw.bufw);
1462 XFreePixmap(xw.dis, xw.buf);
1463 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1470 int xfd = XConnectionNumber(xw.dis);
1474 FD_SET(cmdfd, &rfd);
1476 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1479 die("select failed: %s\n", SERRNO);
1481 if(FD_ISSET(cmdfd, &rfd)) {
1483 draw(SCREEN_UPDATE);
1485 while(XPending(xw.dis)) {
1486 XNextEvent(xw.dis, &ev);
1487 if(handler[ev.type])
1488 (handler[ev.type])(&ev);
1494 main(int argc, char *argv[]) {
1497 for(i = 1; i < argc; i++) {
1498 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
1500 if(++i < argc) opt_title = argv[i];
1503 if(++i < argc) opt_cmd = argv[i];
1510 setlocale(LC_CTYPE, "");