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 /* TODO: use X11 clipboard */
215 static inline int selected(int x, int y) {
216 if ((sel.ey==y && sel.by==y)) {
217 int bx = MIN(sel.bx, sel.ex);
218 int ex = MAX(sel.bx, sel.ex);
219 return (x>=bx && x<=ex);
221 return (((y>sel.b[1] && y<sel.e[1]) || (y==sel.e[1] && x<=sel.e[0])) || \
222 (y==sel.b[1] && x>=sel.b[0] && (x<=sel.e[0] || sel.b[1]!=sel.e[1])));
225 static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
226 if(b) *b = e->xbutton.state,
227 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
228 *x = e->xbutton.x/xw.cw;
229 *y = e->xbutton.y/xw.ch;
230 sel.b[0] = sel.by<sel.ey?sel.bx:sel.ex;
231 sel.b[1] = MIN(sel.by, sel.ey);
232 sel.e[0] = sel.by<sel.ey?sel.ex:sel.bx;
233 sel.e[1] = MAX(sel.by, sel.ey);
236 static void bpress(XEvent *e) {
238 sel.ex = sel.bx = e->xbutton.x/xw.cw;
239 sel.ey = sel.by = e->xbutton.y/xw.ch;
242 static char *getseltext() {
247 sz = ((xw.w/xw.ch) * (sel.e[1]-sel.b[1]+2));
248 ptr = str = malloc (sz);
249 for(y = 0; y < term.row; y++) {
250 for(x = 0; x < term.col; x++) {
251 if(term.line[y][x].state & GLYPH_SET && (ls=selected(x, y))) {
252 *ptr = term.line[y][x].c;
265 static void clipboard_copy(char *str) {
270 static void clipboard_paste() {
272 ttywrite(sel.clip, strlen(sel.clip));
275 // TODO: doubleclick to select word
276 static void brelease(XEvent *e) {
279 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
286 if(sel.bx==sel.ex && sel.by==sel.ey) {
292 clipboard_copy(getseltext());
297 static void bmotion(XEvent *e) {
299 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
310 for(row = 0; row < term.row; row++) {
311 for(col = 0; col < term.col; col++) {
312 if(col == term.c.x && row == term.c.y)
315 c = term.line[row][col];
316 putchar(c.state & GLYPH_SET ? c.c : '.');
325 die(const char *errstr, ...) {
328 va_start(ap, errstr);
329 vfprintf(stderr, errstr, ap);
336 char *args[3] = {getenv("SHELL"), "-i", NULL};
337 DEFAULT(args[0], SHELL); /* if getenv() failed */
338 putenv("TERM=" TNAME);
339 execvp(args[0], args);
345 if(waitpid(pid, &stat, 0) < 0)
346 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
348 exit(WEXITSTATUS(stat));
357 /* seems to work fine on linux, openbsd and freebsd */
358 struct winsize w = {term.row, term.col, 0, 0};
359 if(openpty(&m, &s, NULL, NULL, &w) < 0)
360 die("openpty failed: %s\n", SERRNO);
362 switch(pid = fork()) {
364 die("fork failed\n");
367 setsid(); /* create a new process group */
368 dup2(s, STDIN_FILENO);
369 dup2(s, STDOUT_FILENO);
370 dup2(s, STDERR_FILENO);
371 if(ioctl(s, TIOCSCTTY, NULL) < 0)
372 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
380 signal(SIGCHLD, sigchld);
387 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
389 fprintf(stderr, "\n");
394 char buf[BUFSIZ] = {0};
397 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
398 die("Couldn't read from shell: %s\n", SERRNO);
404 ttywrite(const char *s, size_t n) {
405 if(write(cmdfd, s, n) == -1)
406 die("write error on tty: %s\n", SERRNO);
410 ttyresize(int x, int y) {
415 w.ws_xpixel = w.ws_ypixel = 0;
416 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
417 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
424 if(mode == CURSOR_SAVE)
426 else if(mode == CURSOR_LOAD)
427 term.c = c, tmoveto(c.x, c.y);
436 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
438 term.top = 0, term.bot = term.row - 1;
439 term.mode = MODE_WRAP;
440 tclearregion(0, 0, term.col-1, term.row-1);
444 tnew(int col, int row) {
445 /* set screen size */
446 term.row = row, term.col = col;
447 term.line = malloc(term.row * sizeof(Line));
448 term.alt = malloc(term.row * sizeof(Line));
449 for(row = 0 ; row < term.row; row++) {
450 term.line[row] = malloc(term.col * sizeof(Glyph));
451 term.alt [row] = malloc(term.col * sizeof(Glyph));
459 Line* tmp = term.line;
460 term.line = term.alt;
462 term.mode ^= MODE_ALTSCREEN;
466 tscrolldown (int n) {
470 LIMIT(n, 0, term.bot-term.top+1);
472 for(i = 0; i < n; i++)
473 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
475 for(i = term.bot; i >= term.top+n; i--) {
477 term.line[i] = term.line[i-n];
478 term.line[i-n] = temp;
486 LIMIT(n, 0, term.bot-term.top+1);
488 for(i = 0; i < n; i++)
489 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
491 for(i = term.top; i <= term.bot-n; i++) {
493 term.line[i] = term.line[i+n];
494 term.line[i+n] = temp;
500 int y = term.c.y + 1;
502 tscrollup(1), y = term.bot;
509 char *p = escseq.buf;
513 escseq.priv = 1, p++;
515 while(p < escseq.buf+escseq.len) {
517 escseq.arg[escseq.narg] *= 10;
518 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
520 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
531 tmoveto(int x, int y) {
532 LIMIT(x, 0, term.col-1);
533 LIMIT(y, 0, term.row-1);
534 term.c.state &= ~CURSOR_WRAPNEXT;
541 term.line[term.c.y][term.c.x] = term.c.attr;
542 term.line[term.c.y][term.c.x].c = c;
543 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
547 tclearregion(int x1, int y1, int x2, int y2) {
551 temp = x1, x1 = x2, x2 = temp;
553 temp = y1, y1 = y2, y2 = temp;
555 LIMIT(x1, 0, term.col-1);
556 LIMIT(x2, 0, term.col-1);
557 LIMIT(y1, 0, term.row-1);
558 LIMIT(y2, 0, term.row-1);
560 for(y = y1; y <= y2; y++)
561 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
566 int src = term.c.x + n;
568 int size = term.col - src;
570 if(src >= term.col) {
571 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
574 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
575 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
579 tinsertblank(int n) {
582 int size = term.col - dst;
584 if(dst >= term.col) {
585 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
588 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
589 tclearregion(src, term.c.y, dst - 1, term.c.y);
593 tinsertblankline(int n) {
598 if(term.c.y > term.bot)
600 else if(term.c.y < term.top)
602 if(term.c.y + n >= bot) {
603 tclearregion(0, term.c.y, term.col-1, bot);
606 for(i = bot; i >= term.c.y+n; i--) {
607 /* swap deleted line <-> blanked line */
608 blank = term.line[i];
609 term.line[i] = term.line[i-n];
610 term.line[i-n] = blank;
612 memset(blank, 0, term.col * sizeof(Glyph));
622 if(term.c.y > term.bot)
624 else if(term.c.y < term.top)
626 if(term.c.y + n >= bot) {
627 tclearregion(0, term.c.y, term.col-1, bot);
630 for(i = term.c.y; i <= bot-n; i++) {
631 /* swap deleted line <-> blanked line */
632 blank = term.line[i];
633 term.line[i] = term.line[i+n];
634 term.line[i+n] = blank;
636 memset(blank, 0, term.col * sizeof(Glyph));
641 tsetattr(int *attr, int l) {
644 for(i = 0; i < l; i++) {
647 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
648 term.c.attr.fg = DefaultFG;
649 term.c.attr.bg = DefaultBG;
652 term.c.attr.mode |= ATTR_BOLD;
655 term.c.attr.mode |= ATTR_UNDERLINE;
658 term.c.attr.mode |= ATTR_REVERSE;
661 term.c.attr.mode &= ~ATTR_BOLD;
664 term.c.attr.mode &= ~ATTR_UNDERLINE;
667 term.c.attr.mode &= ~ATTR_REVERSE;
670 if (i + 2 < l && attr[i + 1] == 5) {
672 if (BETWEEN(attr[i], 0, 255))
673 term.c.attr.fg = attr[i];
675 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
678 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
681 term.c.attr.fg = DefaultFG;
684 if (i + 2 < l && attr[i + 1] == 5) {
686 if (BETWEEN(attr[i], 0, 255))
687 term.c.attr.bg = attr[i];
689 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
692 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
695 term.c.attr.bg = DefaultBG;
698 if(BETWEEN(attr[i], 30, 37))
699 term.c.attr.fg = attr[i] - 30;
700 else if(BETWEEN(attr[i], 40, 47))
701 term.c.attr.bg = attr[i] - 40;
702 else if(BETWEEN(attr[i], 90, 97))
703 term.c.attr.fg = attr[i] - 90 + 8;
704 else if(BETWEEN(attr[i], 100, 107))
705 term.c.attr.fg = attr[i] - 100 + 8;
707 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
714 tsetscroll(int t, int b) {
717 LIMIT(t, 0, term.row-1);
718 LIMIT(b, 0, term.row-1);
730 switch(escseq.mode) {
733 printf("erresc: unknown csi ");
737 case '@': /* ICH -- Insert <n> blank char */
738 DEFAULT(escseq.arg[0], 1);
739 tinsertblank(escseq.arg[0]);
741 case 'A': /* CUU -- Cursor <n> Up */
743 DEFAULT(escseq.arg[0], 1);
744 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
746 case 'B': /* CUD -- Cursor <n> Down */
747 DEFAULT(escseq.arg[0], 1);
748 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
750 case 'C': /* CUF -- Cursor <n> Forward */
752 DEFAULT(escseq.arg[0], 1);
753 tmoveto(term.c.x+escseq.arg[0], term.c.y);
755 case 'D': /* CUB -- Cursor <n> Backward */
756 DEFAULT(escseq.arg[0], 1);
757 tmoveto(term.c.x-escseq.arg[0], term.c.y);
759 case 'E': /* CNL -- Cursor <n> Down and first col */
760 DEFAULT(escseq.arg[0], 1);
761 tmoveto(0, term.c.y+escseq.arg[0]);
763 case 'F': /* CPL -- Cursor <n> Up and first col */
764 DEFAULT(escseq.arg[0], 1);
765 tmoveto(0, term.c.y-escseq.arg[0]);
767 case 'G': /* CHA -- Move to <col> */
768 case '`': /* XXX: HPA -- same? */
769 DEFAULT(escseq.arg[0], 1);
770 tmoveto(escseq.arg[0]-1, term.c.y);
772 case 'H': /* CUP -- Move to <row> <col> */
773 case 'f': /* XXX: HVP -- same? */
774 DEFAULT(escseq.arg[0], 1);
775 DEFAULT(escseq.arg[1], 1);
776 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
778 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
779 case 'J': /* ED -- Clear screen */
780 switch(escseq.arg[0]) {
782 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
785 tclearregion(0, 0, term.c.x, term.c.y);
788 tclearregion(0, 0, term.col-1, term.row-1);
794 case 'K': /* EL -- Clear line */
795 switch(escseq.arg[0]) {
797 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
800 tclearregion(0, term.c.y, term.c.x, term.c.y);
803 tclearregion(0, term.c.y, term.col-1, term.c.y);
807 case 'S': /* SU -- Scroll <n> line up */
808 DEFAULT(escseq.arg[0], 1);
809 tscrollup(escseq.arg[0]);
811 case 'T': /* SD -- Scroll <n> line down */
812 DEFAULT(escseq.arg[0], 1);
813 tscrolldown(escseq.arg[0]);
815 case 'L': /* IL -- Insert <n> blank lines */
816 DEFAULT(escseq.arg[0], 1);
817 tinsertblankline(escseq.arg[0]);
819 case 'l': /* RM -- Reset Mode */
821 switch(escseq.arg[0]) {
823 term.mode &= ~MODE_APPKEYPAD;
826 term.mode &= ~MODE_WRAP;
828 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
831 term.c.state |= CURSOR_HIDE;
833 case 1049: /* = 1047 and 1048 */
835 if(IS_SET(MODE_ALTSCREEN)) {
836 tclearregion(0, 0, term.col-1, term.row-1);
839 if(escseq.arg[0] == 1047)
842 tcursor(CURSOR_LOAD);
848 switch(escseq.arg[0]) {
850 term.mode &= ~MODE_INSERT;
857 case 'M': /* DL -- Delete <n> lines */
858 DEFAULT(escseq.arg[0], 1);
859 tdeleteline(escseq.arg[0]);
861 case 'X': /* ECH -- Erase <n> char */
862 DEFAULT(escseq.arg[0], 1);
863 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
865 case 'P': /* DCH -- Delete <n> char */
866 DEFAULT(escseq.arg[0], 1);
867 tdeletechar(escseq.arg[0]);
869 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
870 case 'd': /* VPA -- Move to <row> */
871 DEFAULT(escseq.arg[0], 1);
872 tmoveto(term.c.x, escseq.arg[0]-1);
874 case 'h': /* SM -- Set terminal mode */
876 switch(escseq.arg[0]) {
878 term.mode |= MODE_APPKEYPAD;
881 term.mode |= MODE_WRAP;
883 case 12: /* att610 -- Start blinking cursor (IGNORED) */
886 term.c.state &= ~CURSOR_HIDE;
888 case 1049: /* = 1047 and 1048 */
890 if(IS_SET(MODE_ALTSCREEN))
891 tclearregion(0, 0, term.col-1, term.row-1);
894 if(escseq.arg[0] == 1047)
897 tcursor(CURSOR_SAVE);
899 default: goto unknown;
902 switch(escseq.arg[0]) {
904 term.mode |= MODE_INSERT;
906 default: goto unknown;
910 case 'm': /* SGR -- Terminal attribute (color) */
911 tsetattr(escseq.arg, escseq.narg);
913 case 'r': /* DECSTBM -- Set Scrolling Region */
917 DEFAULT(escseq.arg[0], 1);
918 DEFAULT(escseq.arg[1], term.row);
919 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
923 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
924 tcursor(CURSOR_SAVE);
926 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
927 tcursor(CURSOR_LOAD);
935 printf("ESC [ %s", escseq.priv ? "? " : "");
937 for(i = 0; i < escseq.narg; i++)
938 printf("%d ", escseq.arg[i]);
940 putchar(escseq.mode);
946 memset(&escseq, 0, sizeof(escseq));
951 int space = TAB - term.c.x % TAB;
952 tmoveto(term.c.x + space, term.c.y);
957 if(term.esc & ESC_START) {
958 if(term.esc & ESC_CSI) {
959 escseq.buf[escseq.len++] = c;
960 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
962 csiparse(), csihandle();
964 } else if(term.esc & ESC_OSC) {
967 term.esc = ESC_START | ESC_TITLE;
969 } else if(term.esc & ESC_TITLE) {
970 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
972 term.title[term.titlelen] = '\0';
973 XStoreName(xw.dis, xw.win, term.title);
975 term.title[term.titlelen++] = c;
977 } else if(term.esc & ESC_ALTCHARSET) {
979 case '0': /* Line drawing crap */
980 term.c.attr.mode |= ATTR_GFX;
982 case 'B': /* Back to regular text */
983 term.c.attr.mode &= ~ATTR_GFX;
986 printf("esc unhandled charset: ESC ( %c\n", c);
998 term.esc |= ESC_ALTCHARSET;
1000 case 'D': /* IND -- Linefeed */
1001 if(term.c.y == term.bot)
1004 tmoveto(term.c.x, term.c.y+1);
1007 case 'E': /* NEL -- Next line */
1011 case 'M': /* RI -- Reverse index */
1012 if(term.c.y == term.top)
1015 tmoveto(term.c.x, term.c.y-1);
1018 case 'c': /* RIS -- Reset to inital state */
1022 case '=': /* DECPAM -- Application keypad */
1023 term.mode |= MODE_APPKEYPAD;
1026 case '>': /* DECPNM -- Normal keypad */
1027 term.mode &= ~MODE_APPKEYPAD;
1030 case '7': /* DECSC -- Save Cursor */
1031 tcursor(CURSOR_SAVE);
1034 case '8': /* DECRC -- Restore Cursor */
1035 tcursor(CURSOR_LOAD);
1039 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1049 tmoveto(term.c.x-1, term.c.y);
1052 tmoveto(0, term.c.y);
1063 term.esc = ESC_START;
1066 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1069 if(term.c.x+1 < term.col)
1070 tmoveto(term.c.x+1, term.c.y);
1072 term.c.state |= CURSOR_WRAPNEXT;
1079 tputs(char *s, int len) {
1080 for(; len > 0; len--)
1085 tresize(int col, int row) {
1087 int minrow = MIN(row, term.row);
1088 int mincol = MIN(col, term.col);
1090 if(col < 1 || row < 1)
1093 /* free uneeded rows */
1094 for(i = row; i < term.row; i++) {
1099 /* resize to new height */
1100 term.line = realloc(term.line, row * sizeof(Line));
1101 term.alt = realloc(term.alt, row * sizeof(Line));
1103 /* resize each row to new width, zero-pad if needed */
1104 for(i = 0; i < minrow; i++) {
1105 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1106 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1107 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1108 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1111 /* allocate any new rows */
1112 for(/* i == minrow */; i < row; i++) {
1113 term.line[i] = calloc(col, sizeof(Glyph));
1114 term.alt [i] = calloc(col, sizeof(Glyph));
1117 /* update terminal size */
1118 term.col = col, term.row = row;
1119 /* make use of the LIMIT in tmoveto */
1120 tmoveto(term.c.x, term.c.y);
1121 /* reset scrolling region */
1122 tsetscroll(0, row-1);
1129 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1130 unsigned long white = WhitePixel(xw.dis, xw.scr);
1132 for(i = 0; i < 16; i++) {
1133 if (!XAllocNamedColor(xw.dis, 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, 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, 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 if(!(xw.dis = XOpenDisplay(NULL)))
1193 die("Can't open display\n");
1194 xw.scr = XDefaultScreen(xw.dis);
1197 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1198 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1200 /* XXX: Assuming same size for bold font */
1201 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1202 xw.ch = dc.font->ascent + dc.font->descent;
1208 xw.h = term.row * xw.ch + 2*BORDER;
1209 xw.w = term.col * xw.cw + 2*BORDER;
1210 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1214 xw.bufw = xw.w - 2*BORDER;
1215 xw.bufh = xw.h - 2*BORDER;
1216 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1219 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1220 XMapWindow(xw.dis, xw.win);
1222 XStoreName(xw.dis, xw.win, "st");
1227 xdraws(char *s, Glyph base, int x, int y, int len) {
1228 unsigned long xfg, xbg;
1229 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1232 if(base.mode & ATTR_REVERSE)
1233 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1235 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1237 XSetBackground(xw.dis, dc.gc, xbg);
1238 XSetForeground(xw.dis, dc.gc, xfg);
1240 if(base.mode & ATTR_GFX)
1241 for(i = 0; i < len; i++)
1242 s[i] = gfx[(int)s[i]];
1244 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1245 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1247 if(base.mode & ATTR_UNDERLINE)
1248 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1253 static int oldx = 0;
1254 static int oldy = 0;
1255 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1257 LIMIT(oldx, 0, term.col-1);
1258 LIMIT(oldy, 0, term.row-1);
1260 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1261 g.c = term.line[term.c.y][term.c.x].c;
1263 /* remove the old cursor */
1264 if(term.line[oldy][oldx].state & GLYPH_SET)
1265 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1267 xclear(oldx, oldy, oldx, oldy);
1269 /* draw the new one */
1270 if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
1271 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1272 oldx = term.c.x, oldy = term.c.y;
1277 /* basic drawing routines */
1279 xdrawc(int x, int y, Glyph g) {
1280 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1281 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1282 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1283 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1284 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1291 xclear(0, 0, term.col-1, term.row-1);
1292 for(y = 0; y < term.row; y++)
1293 for(x = 0; x < term.col; x++)
1294 if(term.line[y][x].state & GLYPH_SET)
1295 xdrawc(x, y, term.line[y][x]);
1298 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1303 /* optimized drawing routine */
1305 draw(int redraw_all) {
1308 char buf[DRAW_BUF_SIZ];
1310 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1311 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
1312 for(y = 0; y < term.row; y++) {
1313 base = term.line[y][0];
1315 for(x = 0; x < term.col; x++) {
1316 new = term.line[y][x];
1317 if(sel.bx!=-1 && new.c && selected(x, y))
1318 new.mode ^= ATTR_REVERSE;
1319 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1320 i >= DRAW_BUF_SIZ)) {
1321 xdraws(buf, base, ox, y, i);
1324 if(new.state & GLYPH_SET) {
1333 xdraws(buf, base, ox, y, i);
1336 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1343 expose(XEvent *ev) {
1344 draw(SCREEN_REDRAW);
1348 xseturgency(int add) {
1349 XWMHints *h = XGetWMHints(xw.dis, xw.win);
1350 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1351 XSetWMHints(xw.dis, xw.win, h);
1357 if((xw.hasfocus = ev->type == FocusIn))
1359 draw(SCREEN_UPDATE);
1365 for(i = 0; i < LEN(key); i++)
1367 return (char*)key[i].s;
1372 kpress(XEvent *ev) {
1373 XKeyEvent *e = &ev->xkey;
1381 meta = e->state & Mod1Mask;
1382 shift = e->state & ShiftMask;
1383 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1385 if((customkey = kmap(ksym)))
1386 ttywrite(customkey, strlen(customkey));
1388 buf[sizeof(buf)-1] = '\0';
1389 if(meta && len == 1)
1390 ttywrite("\033", 1);
1398 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1403 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1406 fprintf(stderr, "errkey: %d\n", (int)ksym);
1415 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1418 xw.w = e->xconfigure.width;
1419 xw.h = e->xconfigure.height;
1420 xw.bufw = xw.w - 2*BORDER;
1421 xw.bufh = xw.h - 2*BORDER;
1422 col = xw.bufw / xw.cw;
1423 row = xw.bufh / xw.ch;
1425 ttyresize(col, row);
1426 xw.bufh = MAX(1, xw.bufh);
1427 xw.bufw = MAX(1, xw.bufw);
1428 XFreePixmap(xw.dis, xw.buf);
1429 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1430 draw(SCREEN_REDRAW);
1437 int xfd = XConnectionNumber(xw.dis);
1438 long mask = ExposureMask | KeyPressMask | StructureNotifyMask
1439 | FocusChangeMask | PointerMotionMask | ButtonPressMask
1440 | ButtonReleaseMask;
1442 XSelectInput(xw.dis, xw.win, mask);
1443 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1447 FD_SET(cmdfd, &rfd);
1449 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1452 die("select failed: %s\n", SERRNO);
1454 if(FD_ISSET(cmdfd, &rfd)) {
1456 draw(SCREEN_UPDATE);
1458 while(XPending(xw.dis)) {
1459 XNextEvent(xw.dis, &ev);
1460 if(handler[ev.type])
1461 (handler[ev.type])(&ev);
1467 main(int argc, char *argv[]) {
1468 if(argc == 2 && !strncmp("-v", argv[1], 3))
1469 die("st-" VERSION ", (c) 2010 st engineers\n");
1471 die("usage: st [-v]\n");
1472 setlocale(LC_CTYPE, "");