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 #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, MODE_ALTSCREEN=8 };
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 Line* alt; /* alternate screen */
91 TCursor c; /* cursor */
92 int top; /* top scroll limit */
93 int bot; /* bottom scroll limit */
94 int mode; /* terminal mode flags */
95 int esc; /* escape state flags */
96 char title[ESC_TITLE_SIZ];
100 /* Purely graphic info */
106 int w; /* window width */
107 int h; /* window height */
108 int bufw; /* pixmap width */
109 int bufh; /* pixmap height */
110 int ch; /* char height */
111 int cw; /* char width */
120 /* Drawing Context */
122 unsigned long col[256];
138 static void die(const char *errstr, ...);
139 static void draw(int);
140 static void execsh(void);
141 static void sigchld(int);
142 static void run(void);
144 static void csidump(void);
145 static void csihandle(void);
146 static void csiparse(void);
147 static void csireset(void);
149 static void tclearregion(int, int, int, int);
150 static void tcursor(int);
151 static void tdeletechar(int);
152 static void tdeleteline(int);
153 static void tinsertblank(int);
154 static void tinsertblankline(int);
155 static void tmoveto(int, int);
156 static void tnew(int, int);
157 static void tnewline(void);
158 static void tputtab(void);
159 static void tputc(char);
160 static void tputs(char*, int);
161 static void treset(void);
162 static void tresize(int, int);
163 static void tscrollup(int);
164 static void tscrolldown(int);
165 static void tsetattr(int*, int);
166 static void tsetchar(char);
167 static void tsetscroll(int, int);
168 static void tswapscreen(void);
170 static void ttynew(void);
171 static void ttyread(void);
172 static void ttyresize(int, int);
173 static void ttywrite(const char *, size_t);
175 static void xdraws(char *, Glyph, int, int, int);
176 static void xhints(void);
177 static void xclear(int, int, int, int);
178 static void xdrawcursor(void);
179 static void xinit(void);
180 static void xloadcols(void);
181 static void xseturgency(int);
183 static void expose(XEvent *);
184 static char* kmap(KeySym);
185 static void kpress(XEvent *);
186 static void resize(XEvent *);
187 static void focus(XEvent *);
188 static void brelease(XEvent *e);
189 static void bpress(XEvent *e);
190 static void bmotion(XEvent *e);
193 static void (*handler[LASTEvent])(XEvent *) = {
196 [ConfigureNotify] = resize,
199 [MotionNotify] = bmotion,
200 [ButtonPress] = bpress,
201 [ButtonRelease] = brelease,
208 static CSIEscape escseq;
211 static Selection sel;
213 static inline int selected(int x, int y) {
214 if ((sel.ey==y && sel.by==y)) {
215 int bx = MIN(sel.bx, sel.ex);
216 int ex = MAX(sel.bx, sel.ex);
217 return (x>=bx && x<=ex);
219 return (((y>sel.b[1] && y<sel.e[1]) || (y==sel.e[1] && x<=sel.e[0])) || \
220 (y==sel.b[1] && x>=sel.b[0] && (x<=sel.e[0] || sel.b[1]!=sel.e[1])));
223 static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
224 if(b) *b = e->xbutton.state,
225 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
226 *x = e->xbutton.x/xw.cw;
227 *y = e->xbutton.y/xw.ch;
228 sel.b[0] = sel.by<sel.ey?sel.bx:sel.ex;
229 sel.b[1] = MIN(sel.by, sel.ey);
230 sel.e[0] = sel.by<sel.ey?sel.ex:sel.bx;
231 sel.e[1] = MAX(sel.by, sel.ey);
234 static void bpress(XEvent *e) {
236 sel.ex = sel.bx = e->xbutton.x/xw.cw;
237 sel.ey = sel.by = e->xbutton.y/xw.ch;
240 static char *getseltext() {
245 sz = ((xw.w/xw.ch) * (sel.e[1]-sel.b[1]+2));
246 ptr = str = malloc (sz);
247 for(y = 0; y < term.row; y++) {
248 for(x = 0; x < term.col; x++) {
249 if(term.line[y][x].state & GLYPH_SET && (ls=selected(x, y))) {
250 *ptr = term.line[y][x].c;
263 /* TODO: use X11 clipboard */
264 static void clipboard_copy(char *str) {
269 static void clipboard_paste() {
271 ttywrite(sel.clip, strlen(sel.clip));
274 /* TODO: doubleclick to select word */
275 static void brelease(XEvent *e) {
278 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
285 if(sel.bx==sel.ex && sel.by==sel.ey) {
291 clipboard_copy(getseltext());
296 static void bmotion(XEvent *e) {
298 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
309 for(row = 0; row < term.row; row++) {
310 for(col = 0; col < term.col; col++) {
311 if(col == term.c.x && row == term.c.y)
314 c = term.line[row][col];
315 putchar(c.state & GLYPH_SET ? c.c : '.');
324 die(const char *errstr, ...) {
327 va_start(ap, errstr);
328 vfprintf(stderr, errstr, ap);
335 char *args[3] = {getenv("SHELL"), "-i", NULL};
336 DEFAULT(args[0], SHELL); /* if getenv() failed */
337 putenv("TERM=" TNAME);
338 execvp(args[0], args);
344 if(waitpid(pid, &stat, 0) < 0)
345 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
347 exit(WEXITSTATUS(stat));
356 /* seems to work fine on linux, openbsd and freebsd */
357 struct winsize w = {term.row, term.col, 0, 0};
358 if(openpty(&m, &s, NULL, NULL, &w) < 0)
359 die("openpty failed: %s\n", SERRNO);
361 switch(pid = fork()) {
363 die("fork failed\n");
366 setsid(); /* create a new process group */
367 dup2(s, STDIN_FILENO);
368 dup2(s, STDOUT_FILENO);
369 dup2(s, STDERR_FILENO);
370 if(ioctl(s, TIOCSCTTY, NULL) < 0)
371 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
379 signal(SIGCHLD, sigchld);
386 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
388 fprintf(stderr, "\n");
393 char buf[BUFSIZ] = {0};
396 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
397 die("Couldn't read from shell: %s\n", SERRNO);
403 ttywrite(const char *s, size_t n) {
404 if(write(cmdfd, s, n) == -1)
405 die("write error on tty: %s\n", SERRNO);
409 ttyresize(int x, int y) {
414 w.ws_xpixel = w.ws_ypixel = 0;
415 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
416 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
423 if(mode == CURSOR_SAVE)
425 else if(mode == CURSOR_LOAD)
426 term.c = c, tmoveto(c.x, c.y);
435 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
437 term.top = 0, term.bot = term.row - 1;
438 term.mode = MODE_WRAP;
439 tclearregion(0, 0, term.col-1, term.row-1);
443 tnew(int col, int row) {
444 /* set screen size */
445 term.row = row, term.col = col;
446 term.line = malloc(term.row * sizeof(Line));
447 term.alt = malloc(term.row * sizeof(Line));
448 for(row = 0 ; row < term.row; row++) {
449 term.line[row] = malloc(term.col * sizeof(Glyph));
450 term.alt [row] = malloc(term.col * sizeof(Glyph));
458 Line* tmp = term.line;
459 term.line = term.alt;
461 term.mode ^= MODE_ALTSCREEN;
465 tscrolldown (int n) {
469 LIMIT(n, 0, term.bot-term.top+1);
471 for(i = 0; i < n; i++)
472 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
474 for(i = term.bot; i >= term.top+n; i--) {
476 term.line[i] = term.line[i-n];
477 term.line[i-n] = temp;
485 LIMIT(n, 0, term.bot-term.top+1);
487 for(i = 0; i < n; i++)
488 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
490 for(i = term.top; i <= term.bot-n; i++) {
492 term.line[i] = term.line[i+n];
493 term.line[i+n] = temp;
499 int y = term.c.y + 1;
501 tscrollup(1), y = term.bot;
508 char *p = escseq.buf;
512 escseq.priv = 1, p++;
514 while(p < escseq.buf+escseq.len) {
516 escseq.arg[escseq.narg] *= 10;
517 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
519 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
530 tmoveto(int x, int y) {
531 LIMIT(x, 0, term.col-1);
532 LIMIT(y, 0, term.row-1);
533 term.c.state &= ~CURSOR_WRAPNEXT;
540 term.line[term.c.y][term.c.x] = term.c.attr;
541 term.line[term.c.y][term.c.x].c = c;
542 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
546 tclearregion(int x1, int y1, int x2, int y2) {
550 temp = x1, x1 = x2, x2 = temp;
552 temp = y1, y1 = y2, y2 = temp;
554 LIMIT(x1, 0, term.col-1);
555 LIMIT(x2, 0, term.col-1);
556 LIMIT(y1, 0, term.row-1);
557 LIMIT(y2, 0, term.row-1);
559 for(y = y1; y <= y2; y++)
560 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
565 int src = term.c.x + n;
567 int size = term.col - src;
569 if(src >= term.col) {
570 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
573 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
574 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
578 tinsertblank(int n) {
581 int size = term.col - dst;
583 if(dst >= term.col) {
584 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
587 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
588 tclearregion(src, term.c.y, dst - 1, term.c.y);
592 tinsertblankline(int n) {
597 if(term.c.y > term.bot)
599 else if(term.c.y < term.top)
601 if(term.c.y + n >= bot) {
602 tclearregion(0, term.c.y, term.col-1, bot);
605 for(i = bot; i >= term.c.y+n; i--) {
606 /* swap deleted line <-> blanked line */
607 blank = term.line[i];
608 term.line[i] = term.line[i-n];
609 term.line[i-n] = blank;
611 memset(blank, 0, term.col * sizeof(Glyph));
621 if(term.c.y > term.bot)
623 else if(term.c.y < term.top)
625 if(term.c.y + n >= bot) {
626 tclearregion(0, term.c.y, term.col-1, bot);
629 for(i = term.c.y; i <= bot-n; i++) {
630 /* swap deleted line <-> blanked line */
631 blank = term.line[i];
632 term.line[i] = term.line[i+n];
633 term.line[i+n] = blank;
635 memset(blank, 0, term.col * sizeof(Glyph));
640 tsetattr(int *attr, int l) {
643 for(i = 0; i < l; i++) {
646 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
647 term.c.attr.fg = DefaultFG;
648 term.c.attr.bg = DefaultBG;
651 term.c.attr.mode |= ATTR_BOLD;
654 term.c.attr.mode |= ATTR_UNDERLINE;
657 term.c.attr.mode |= ATTR_REVERSE;
660 term.c.attr.mode &= ~ATTR_BOLD;
663 term.c.attr.mode &= ~ATTR_UNDERLINE;
666 term.c.attr.mode &= ~ATTR_REVERSE;
669 if (i + 2 < l && attr[i + 1] == 5) {
671 if (BETWEEN(attr[i], 0, 255))
672 term.c.attr.fg = attr[i];
674 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
677 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
680 term.c.attr.fg = DefaultFG;
683 if (i + 2 < l && attr[i + 1] == 5) {
685 if (BETWEEN(attr[i], 0, 255))
686 term.c.attr.bg = attr[i];
688 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
691 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
694 term.c.attr.bg = DefaultBG;
697 if(BETWEEN(attr[i], 30, 37))
698 term.c.attr.fg = attr[i] - 30;
699 else if(BETWEEN(attr[i], 40, 47))
700 term.c.attr.bg = attr[i] - 40;
701 else if(BETWEEN(attr[i], 90, 97))
702 term.c.attr.fg = attr[i] - 90 + 8;
703 else if(BETWEEN(attr[i], 100, 107))
704 term.c.attr.fg = attr[i] - 100 + 8;
706 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
713 tsetscroll(int t, int b) {
716 LIMIT(t, 0, term.row-1);
717 LIMIT(b, 0, term.row-1);
729 switch(escseq.mode) {
732 printf("erresc: unknown csi ");
736 case '@': /* ICH -- Insert <n> blank char */
737 DEFAULT(escseq.arg[0], 1);
738 tinsertblank(escseq.arg[0]);
740 case 'A': /* CUU -- Cursor <n> Up */
742 DEFAULT(escseq.arg[0], 1);
743 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
745 case 'B': /* CUD -- Cursor <n> Down */
746 DEFAULT(escseq.arg[0], 1);
747 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
749 case 'C': /* CUF -- Cursor <n> Forward */
751 DEFAULT(escseq.arg[0], 1);
752 tmoveto(term.c.x+escseq.arg[0], term.c.y);
754 case 'D': /* CUB -- Cursor <n> Backward */
755 DEFAULT(escseq.arg[0], 1);
756 tmoveto(term.c.x-escseq.arg[0], term.c.y);
758 case 'E': /* CNL -- Cursor <n> Down and first col */
759 DEFAULT(escseq.arg[0], 1);
760 tmoveto(0, term.c.y+escseq.arg[0]);
762 case 'F': /* CPL -- Cursor <n> Up and first col */
763 DEFAULT(escseq.arg[0], 1);
764 tmoveto(0, term.c.y-escseq.arg[0]);
766 case 'G': /* CHA -- Move to <col> */
767 case '`': /* XXX: HPA -- same? */
768 DEFAULT(escseq.arg[0], 1);
769 tmoveto(escseq.arg[0]-1, term.c.y);
771 case 'H': /* CUP -- Move to <row> <col> */
772 case 'f': /* XXX: HVP -- same? */
773 DEFAULT(escseq.arg[0], 1);
774 DEFAULT(escseq.arg[1], 1);
775 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
777 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
778 case 'J': /* ED -- Clear screen */
779 switch(escseq.arg[0]) {
781 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
784 tclearregion(0, 0, term.c.x, term.c.y);
787 tclearregion(0, 0, term.col-1, term.row-1);
793 case 'K': /* EL -- Clear line */
794 switch(escseq.arg[0]) {
796 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
799 tclearregion(0, term.c.y, term.c.x, term.c.y);
802 tclearregion(0, term.c.y, term.col-1, term.c.y);
806 case 'S': /* SU -- Scroll <n> line up */
807 DEFAULT(escseq.arg[0], 1);
808 tscrollup(escseq.arg[0]);
810 case 'T': /* SD -- Scroll <n> line down */
811 DEFAULT(escseq.arg[0], 1);
812 tscrolldown(escseq.arg[0]);
814 case 'L': /* IL -- Insert <n> blank lines */
815 DEFAULT(escseq.arg[0], 1);
816 tinsertblankline(escseq.arg[0]);
818 case 'l': /* RM -- Reset Mode */
820 switch(escseq.arg[0]) {
822 term.mode &= ~MODE_APPKEYPAD;
825 term.mode &= ~MODE_WRAP;
827 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
830 term.c.state |= CURSOR_HIDE;
832 case 1049: /* = 1047 and 1048 */
834 if(IS_SET(MODE_ALTSCREEN)) {
835 tclearregion(0, 0, term.col-1, term.row-1);
838 if(escseq.arg[0] == 1047)
841 tcursor(CURSOR_LOAD);
847 switch(escseq.arg[0]) {
849 term.mode &= ~MODE_INSERT;
856 case 'M': /* DL -- Delete <n> lines */
857 DEFAULT(escseq.arg[0], 1);
858 tdeleteline(escseq.arg[0]);
860 case 'X': /* ECH -- Erase <n> char */
861 DEFAULT(escseq.arg[0], 1);
862 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
864 case 'P': /* DCH -- Delete <n> char */
865 DEFAULT(escseq.arg[0], 1);
866 tdeletechar(escseq.arg[0]);
868 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
869 case 'd': /* VPA -- Move to <row> */
870 DEFAULT(escseq.arg[0], 1);
871 tmoveto(term.c.x, escseq.arg[0]-1);
873 case 'h': /* SM -- Set terminal mode */
875 switch(escseq.arg[0]) {
877 term.mode |= MODE_APPKEYPAD;
880 term.mode |= MODE_WRAP;
882 case 12: /* att610 -- Start blinking cursor (IGNORED) */
885 term.c.state &= ~CURSOR_HIDE;
887 case 1049: /* = 1047 and 1048 */
889 if(IS_SET(MODE_ALTSCREEN))
890 tclearregion(0, 0, term.col-1, term.row-1);
893 if(escseq.arg[0] == 1047)
896 tcursor(CURSOR_SAVE);
898 default: goto unknown;
901 switch(escseq.arg[0]) {
903 term.mode |= MODE_INSERT;
905 default: goto unknown;
909 case 'm': /* SGR -- Terminal attribute (color) */
910 tsetattr(escseq.arg, escseq.narg);
912 case 'r': /* DECSTBM -- Set Scrolling Region */
916 DEFAULT(escseq.arg[0], 1);
917 DEFAULT(escseq.arg[1], term.row);
918 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
922 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
923 tcursor(CURSOR_SAVE);
925 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
926 tcursor(CURSOR_LOAD);
934 printf("ESC [ %s", escseq.priv ? "? " : "");
936 for(i = 0; i < escseq.narg; i++)
937 printf("%d ", escseq.arg[i]);
939 putchar(escseq.mode);
945 memset(&escseq, 0, sizeof(escseq));
950 int space = TAB - term.c.x % TAB;
951 tmoveto(term.c.x + space, term.c.y);
956 if(term.esc & ESC_START) {
957 if(term.esc & ESC_CSI) {
958 escseq.buf[escseq.len++] = c;
959 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
961 csiparse(), csihandle();
963 } else if(term.esc & ESC_OSC) {
966 term.esc = ESC_START | ESC_TITLE;
968 } else if(term.esc & ESC_TITLE) {
969 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
971 term.title[term.titlelen] = '\0';
972 XStoreName(xw.dis, xw.win, term.title);
974 term.title[term.titlelen++] = c;
976 } else if(term.esc & ESC_ALTCHARSET) {
978 case '0': /* Line drawing crap */
979 term.c.attr.mode |= ATTR_GFX;
981 case 'B': /* Back to regular text */
982 term.c.attr.mode &= ~ATTR_GFX;
985 printf("esc unhandled charset: ESC ( %c\n", c);
997 term.esc |= ESC_ALTCHARSET;
999 case 'D': /* IND -- Linefeed */
1000 if(term.c.y == term.bot)
1003 tmoveto(term.c.x, term.c.y+1);
1006 case 'E': /* NEL -- Next line */
1010 case 'M': /* RI -- Reverse index */
1011 if(term.c.y == term.top)
1014 tmoveto(term.c.x, term.c.y-1);
1017 case 'c': /* RIS -- Reset to inital state */
1021 case '=': /* DECPAM -- Application keypad */
1022 term.mode |= MODE_APPKEYPAD;
1025 case '>': /* DECPNM -- Normal keypad */
1026 term.mode &= ~MODE_APPKEYPAD;
1029 case '7': /* DECSC -- Save Cursor */
1030 tcursor(CURSOR_SAVE);
1033 case '8': /* DECRC -- Restore Cursor */
1034 tcursor(CURSOR_LOAD);
1038 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1048 tmoveto(term.c.x-1, term.c.y);
1051 tmoveto(0, term.c.y);
1062 term.esc = ESC_START;
1065 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1068 if(term.c.x+1 < term.col)
1069 tmoveto(term.c.x+1, term.c.y);
1071 term.c.state |= CURSOR_WRAPNEXT;
1078 tputs(char *s, int len) {
1079 for(; len > 0; len--)
1084 tresize(int col, int row) {
1086 int minrow = MIN(row, term.row);
1087 int mincol = MIN(col, term.col);
1089 if(col < 1 || row < 1)
1092 /* free uneeded rows */
1093 for(i = row; i < term.row; i++) {
1098 /* resize to new height */
1099 term.line = realloc(term.line, row * sizeof(Line));
1100 term.alt = realloc(term.alt, row * sizeof(Line));
1102 /* resize each row to new width, zero-pad if needed */
1103 for(i = 0; i < minrow; i++) {
1104 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1105 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1106 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1107 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1110 /* allocate any new rows */
1111 for(/* i == minrow */; i < row; i++) {
1112 term.line[i] = calloc(col, sizeof(Glyph));
1113 term.alt [i] = calloc(col, sizeof(Glyph));
1116 /* update terminal size */
1117 term.col = col, term.row = row;
1118 /* make use of the LIMIT in tmoveto */
1119 tmoveto(term.c.x, term.c.y);
1120 /* reset scrolling region */
1121 tsetscroll(0, row-1);
1128 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1129 unsigned long white = WhitePixel(xw.dis, xw.scr);
1131 for(i = 0; i < 16; i++) {
1132 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1134 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1136 dc.col[i] = color.pixel;
1139 /* same colors as xterm */
1140 for(r = 0; r < 6; r++)
1141 for(g = 0; g < 6; g++)
1142 for(b = 0; b < 6; b++) {
1143 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1144 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1145 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1146 if (!XAllocColor(xw.dis, cmap, &color)) {
1148 fprintf(stderr, "Could not allocate color %d\n", i);
1150 dc.col[i] = color.pixel;
1154 for(r = 0; r < 24; r++, i++) {
1155 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1156 if (!XAllocColor(xw.dis, cmap, &color)) {
1158 fprintf(stderr, "Could not allocate color %d\n", i);
1160 dc.col[i] = color.pixel;
1165 xclear(int x1, int y1, int x2, int y2) {
1166 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1167 XFillRectangle(xw.dis, xw.buf, dc.gc,
1168 x1 * xw.cw, y1 * xw.ch,
1169 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1175 XClassHint class = {TNAME, TNAME};
1176 XWMHints wm = {.flags = InputHint, .input = 1};
1178 .flags = PSize | PResizeInc | PBaseSize,
1181 .height_inc = xw.ch,
1183 .base_height = 2*BORDER,
1184 .base_width = 2*BORDER,
1186 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1191 if(!(xw.dis = XOpenDisplay(NULL)))
1192 die("Can't open display\n");
1193 xw.scr = XDefaultScreen(xw.dis);
1196 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1197 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1199 /* XXX: Assuming same size for bold font */
1200 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1201 xw.ch = dc.font->ascent + dc.font->descent;
1207 xw.h = term.row * xw.ch + 2*BORDER;
1208 xw.w = term.col * xw.cw + 2*BORDER;
1209 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1213 xw.bufw = xw.w - 2*BORDER;
1214 xw.bufh = xw.h - 2*BORDER;
1215 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1217 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1220 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask
1221 | StructureNotifyMask | FocusChangeMask | PointerMotionMask
1222 | ButtonPressMask | ButtonReleaseMask);
1224 XMapWindow(xw.dis, xw.win);
1226 XStoreName(xw.dis, xw.win, "st");
1231 xdraws(char *s, Glyph base, int x, int y, int len) {
1232 unsigned long xfg, xbg;
1233 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1236 if(base.mode & ATTR_REVERSE)
1237 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1239 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1241 XSetBackground(xw.dis, dc.gc, xbg);
1242 XSetForeground(xw.dis, dc.gc, xfg);
1244 if(base.mode & ATTR_GFX)
1245 for(i = 0; i < len; i++)
1246 s[i] = gfx[(int)s[i]];
1248 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1249 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1251 if(base.mode & ATTR_UNDERLINE)
1252 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1257 static int oldx = 0;
1258 static int oldy = 0;
1259 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1261 LIMIT(oldx, 0, term.col-1);
1262 LIMIT(oldy, 0, term.row-1);
1264 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1265 g.c = term.line[term.c.y][term.c.x].c;
1267 /* remove the old cursor */
1268 if(term.line[oldy][oldx].state & GLYPH_SET)
1269 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1271 xclear(oldx, oldy, oldx, oldy);
1273 /* draw the new one */
1274 if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
1275 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1276 oldx = term.c.x, oldy = term.c.y;
1281 /* basic drawing routines */
1283 xdrawc(int x, int y, Glyph g) {
1284 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1285 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1286 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1287 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1288 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1295 xclear(0, 0, term.col-1, term.row-1);
1296 for(y = 0; y < term.row; y++)
1297 for(x = 0; x < term.col; x++)
1298 if(term.line[y][x].state & GLYPH_SET)
1299 xdrawc(x, y, term.line[y][x]);
1302 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1307 /* optimized drawing routine */
1309 draw(int redraw_all) {
1312 char buf[DRAW_BUF_SIZ];
1314 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1315 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
1316 for(y = 0; y < term.row; y++) {
1317 base = term.line[y][0];
1319 for(x = 0; x < term.col; x++) {
1320 new = term.line[y][x];
1321 if(sel.bx!=-1 && new.c && selected(x, y))
1322 new.mode ^= ATTR_REVERSE;
1323 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1324 i >= DRAW_BUF_SIZ)) {
1325 xdraws(buf, base, ox, y, i);
1328 if(new.state & GLYPH_SET) {
1337 xdraws(buf, base, ox, y, i);
1340 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1347 expose(XEvent *ev) {
1348 draw(SCREEN_REDRAW);
1352 xseturgency(int add) {
1353 XWMHints *h = XGetWMHints(xw.dis, xw.win);
1354 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1355 XSetWMHints(xw.dis, xw.win, h);
1361 if((xw.hasfocus = ev->type == FocusIn))
1363 draw(SCREEN_UPDATE);
1369 for(i = 0; i < LEN(key); i++)
1371 return (char*)key[i].s;
1376 kpress(XEvent *ev) {
1377 XKeyEvent *e = &ev->xkey;
1385 meta = e->state & Mod1Mask;
1386 shift = e->state & ShiftMask;
1387 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1389 if((customkey = kmap(ksym)))
1390 ttywrite(customkey, strlen(customkey));
1392 buf[sizeof(buf)-1] = '\0';
1393 if(meta && len == 1)
1394 ttywrite("\033", 1);
1402 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1407 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1410 fprintf(stderr, "errkey: %d\n", (int)ksym);
1419 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1422 xw.w = e->xconfigure.width;
1423 xw.h = e->xconfigure.height;
1424 xw.bufw = xw.w - 2*BORDER;
1425 xw.bufh = xw.h - 2*BORDER;
1426 col = xw.bufw / xw.cw;
1427 row = xw.bufh / xw.ch;
1429 ttyresize(col, row);
1430 xw.bufh = MAX(1, xw.bufh);
1431 xw.bufw = MAX(1, xw.bufw);
1432 XFreePixmap(xw.dis, xw.buf);
1433 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1434 draw(SCREEN_REDRAW);
1441 int xfd = XConnectionNumber(xw.dis);
1445 FD_SET(cmdfd, &rfd);
1447 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1450 die("select failed: %s\n", SERRNO);
1452 if(FD_ISSET(cmdfd, &rfd)) {
1454 draw(SCREEN_UPDATE);
1456 while(XPending(xw.dis)) {
1457 XNextEvent(xw.dis, &ev);
1458 if(handler[ev.type])
1459 (handler[ev.type])(&ev);
1465 main(int argc, char *argv[]) {
1466 if(argc == 2 && !strncmp("-v", argv[1], 3))
1467 die("st-" VERSION ", (c) 2010 st engineers\n");
1469 die("usage: st [-v]\n");
1470 setlocale(LC_CTYPE, "");