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];
128 /* TODO: use better name for vars... */
133 struct {int x, y;} b, e;
139 static void die(const char *errstr, ...);
140 static void draw(int);
141 static void execsh(void);
142 static void sigchld(int);
143 static void run(void);
145 static void csidump(void);
146 static void csihandle(void);
147 static void csiparse(void);
148 static void csireset(void);
150 static void tclearregion(int, int, int, int);
151 static void tcursor(int);
152 static void tdeletechar(int);
153 static void tdeleteline(int);
154 static void tinsertblank(int);
155 static void tinsertblankline(int);
156 static void tmoveto(int, int);
157 static void tnew(int, int);
158 static void tnewline(void);
159 static void tputtab(void);
160 static void tputc(char);
161 static void tputs(char*, int);
162 static void treset(void);
163 static void tresize(int, int);
164 static void tscrollup(int);
165 static void tscrolldown(int);
166 static void tsetattr(int*, int);
167 static void tsetchar(char);
168 static void tsetscroll(int, int);
169 static void tswapscreen(void);
171 static void ttynew(void);
172 static void ttyread(void);
173 static void ttyresize(int, int);
174 static void ttywrite(const char *, size_t);
176 static void xdraws(char *, Glyph, int, int, int);
177 static void xhints(void);
178 static void xclear(int, int, int, int);
179 static void xdrawcursor(void);
180 static void xinit(void);
181 static void xloadcols(void);
182 static void xseturgency(int);
184 static void expose(XEvent *);
185 static char* kmap(KeySym);
186 static void kpress(XEvent *);
187 static void resize(XEvent *);
188 static void focus(XEvent *);
189 static void brelease(XEvent *);
190 static void bpress(XEvent *);
191 static void bmotion(XEvent *);
194 static void (*handler[LASTEvent])(XEvent *) = {
197 [ConfigureNotify] = resize,
200 [MotionNotify] = bmotion,
201 [ButtonPress] = bpress,
202 [ButtonRelease] = brelease,
209 static CSIEscape escseq;
212 static Selection sel;
221 static inline int selected(int x, int y) {
222 if(sel.ey == y && sel.by == y) {
223 int bx = MIN(sel.bx, sel.ex);
224 int ex = MAX(sel.bx, sel.ex);
225 return BETWEEN(x, bx, ex);
227 return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
228 || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
231 static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
232 if(b) *b = e->xbutton.state,
233 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
234 *x = e->xbutton.x/xw.cw;
235 *y = e->xbutton.y/xw.ch;
236 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
237 sel.b.y = MIN(sel.by, sel.ey);
238 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
239 sel.e.y = MAX(sel.by, sel.ey);
242 static void bpress(XEvent *e) {
244 sel.ex = sel.bx = e->xbutton.x/xw.cw;
245 sel.ey = sel.by = e->xbutton.y/xw.ch;
248 static char *getseltext() {
253 sz = (term.col+1) * (sel.e.y-sel.b.y+1);
254 ptr = str = malloc(sz);
255 for(y = 0; y < term.row; y++) {
256 for(x = 0; x < term.col; x++)
257 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y)))
258 *ptr = term.line[y][x].c, ptr++;
266 /* TODO: use X11 clipboard */
267 static void selcopy(char *str) {
272 static void selpaste() {
274 ttywrite(sel.clip, strlen(sel.clip));
277 /* TODO: doubleclick to select word */
278 static void brelease(XEvent *e) {
281 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
282 if(sel.bx==sel.ex && sel.by==sel.ey) {
288 selcopy(getseltext());
293 static void bmotion(XEvent *e) {
295 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
306 for(row = 0; row < term.row; row++) {
307 for(col = 0; col < term.col; col++) {
308 if(col == term.c.x && row == term.c.y)
311 c = term.line[row][col];
312 putchar(c.state & GLYPH_SET ? c.c : '.');
321 die(const char *errstr, ...) {
324 va_start(ap, errstr);
325 vfprintf(stderr, errstr, ap);
332 char *args[3] = {getenv("SHELL"), "-i", NULL};
333 DEFAULT(args[0], SHELL); /* if getenv() failed */
334 putenv("TERM=" TNAME);
335 execvp(args[0], args);
341 if(waitpid(pid, &stat, 0) < 0)
342 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
344 exit(WEXITSTATUS(stat));
353 /* seems to work fine on linux, openbsd and freebsd */
354 struct winsize w = {term.row, term.col, 0, 0};
355 if(openpty(&m, &s, NULL, NULL, &w) < 0)
356 die("openpty failed: %s\n", SERRNO);
358 switch(pid = fork()) {
360 die("fork failed\n");
363 setsid(); /* create a new process group */
364 dup2(s, STDIN_FILENO);
365 dup2(s, STDOUT_FILENO);
366 dup2(s, STDERR_FILENO);
367 if(ioctl(s, TIOCSCTTY, NULL) < 0)
368 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
376 signal(SIGCHLD, sigchld);
383 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
385 fprintf(stderr, "\n");
393 if((ret = read(cmdfd, buf, LEN(buf))) < 0)
394 die("Couldn't read from shell: %s\n", SERRNO);
400 ttywrite(const char *s, size_t n) {
401 if(write(cmdfd, s, n) == -1)
402 die("write error on tty: %s\n", SERRNO);
406 ttyresize(int x, int y) {
411 w.ws_xpixel = w.ws_ypixel = 0;
412 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
413 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
420 if(mode == CURSOR_SAVE)
422 else if(mode == CURSOR_LOAD)
423 term.c = c, tmoveto(c.x, c.y);
432 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
434 term.top = 0, term.bot = term.row - 1;
435 term.mode = MODE_WRAP;
436 tclearregion(0, 0, term.col-1, term.row-1);
440 tnew(int col, int row) {
441 /* set screen size */
442 term.row = row, term.col = col;
443 term.line = malloc(term.row * sizeof(Line));
444 term.alt = malloc(term.row * sizeof(Line));
445 for(row = 0 ; row < term.row; row++) {
446 term.line[row] = malloc(term.col * sizeof(Glyph));
447 term.alt [row] = malloc(term.col * sizeof(Glyph));
455 Line* tmp = term.line;
456 term.line = term.alt;
458 term.mode ^= MODE_ALTSCREEN;
462 tscrolldown (int n) {
466 LIMIT(n, 0, term.bot-term.top+1);
468 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
470 for(i = term.bot; i >= term.top+n; i--) {
472 term.line[i] = term.line[i-n];
473 term.line[i-n] = temp;
481 LIMIT(n, 0, term.bot-term.top+1);
483 tclearregion(0, term.top, term.col-1, term.top+n-1);
485 for(i = term.top; i <= term.bot-n; i++) {
487 term.line[i] = term.line[i+n];
488 term.line[i+n] = temp;
494 int y = term.c.y + 1;
496 tscrollup(1), y = term.bot;
503 char *p = escseq.buf;
507 escseq.priv = 1, p++;
509 while(p < escseq.buf+escseq.len) {
511 escseq.arg[escseq.narg] *= 10;
512 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
514 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
525 tmoveto(int x, int y) {
526 LIMIT(x, 0, term.col-1);
527 LIMIT(y, 0, term.row-1);
528 term.c.state &= ~CURSOR_WRAPNEXT;
535 term.line[term.c.y][term.c.x] = term.c.attr;
536 term.line[term.c.y][term.c.x].c = c;
537 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
541 tclearregion(int x1, int y1, int x2, int y2) {
545 temp = x1, x1 = x2, x2 = temp;
547 temp = y1, y1 = y2, y2 = temp;
549 LIMIT(x1, 0, term.col-1);
550 LIMIT(x2, 0, term.col-1);
551 LIMIT(y1, 0, term.row-1);
552 LIMIT(y2, 0, term.row-1);
554 for(y = y1; y <= y2; y++)
555 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
560 int src = term.c.x + n;
562 int size = term.col - src;
564 if(src >= term.col) {
565 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
568 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
569 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
573 tinsertblank(int n) {
576 int size = term.col - dst;
578 if(dst >= term.col) {
579 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
582 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
583 tclearregion(src, term.c.y, dst - 1, term.c.y);
587 tinsertblankline(int n) {
592 if(term.c.y > term.bot)
594 else if(term.c.y < term.top)
596 if(term.c.y + n >= bot) {
597 tclearregion(0, term.c.y, term.col-1, bot);
600 for(i = bot; i >= term.c.y+n; i--) {
601 /* swap deleted line <-> blanked line */
602 blank = term.line[i];
603 term.line[i] = term.line[i-n];
604 term.line[i-n] = blank;
606 memset(blank, 0, term.col * sizeof(Glyph));
616 if(term.c.y > term.bot)
618 else if(term.c.y < term.top)
620 if(term.c.y + n >= bot) {
621 tclearregion(0, term.c.y, term.col-1, bot);
624 for(i = term.c.y; i <= bot-n; i++) {
625 /* swap deleted line <-> blanked line */
626 blank = term.line[i];
627 term.line[i] = term.line[i+n];
628 term.line[i+n] = blank;
630 memset(blank, 0, term.col * sizeof(Glyph));
635 tsetattr(int *attr, int l) {
638 for(i = 0; i < l; i++) {
641 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
642 term.c.attr.fg = DefaultFG;
643 term.c.attr.bg = DefaultBG;
646 term.c.attr.mode |= ATTR_BOLD;
649 term.c.attr.mode |= ATTR_UNDERLINE;
652 term.c.attr.mode |= ATTR_REVERSE;
655 term.c.attr.mode &= ~ATTR_BOLD;
658 term.c.attr.mode &= ~ATTR_UNDERLINE;
661 term.c.attr.mode &= ~ATTR_REVERSE;
664 if (i + 2 < l && attr[i + 1] == 5) {
666 if (BETWEEN(attr[i], 0, 255))
667 term.c.attr.fg = attr[i];
669 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
672 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
675 term.c.attr.fg = DefaultFG;
678 if (i + 2 < l && attr[i + 1] == 5) {
680 if (BETWEEN(attr[i], 0, 255))
681 term.c.attr.bg = attr[i];
683 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
686 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
689 term.c.attr.bg = DefaultBG;
692 if(BETWEEN(attr[i], 30, 37))
693 term.c.attr.fg = attr[i] - 30;
694 else if(BETWEEN(attr[i], 40, 47))
695 term.c.attr.bg = attr[i] - 40;
696 else if(BETWEEN(attr[i], 90, 97))
697 term.c.attr.fg = attr[i] - 90 + 8;
698 else if(BETWEEN(attr[i], 100, 107))
699 term.c.attr.fg = attr[i] - 100 + 8;
701 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
708 tsetscroll(int t, int b) {
711 LIMIT(t, 0, term.row-1);
712 LIMIT(b, 0, term.row-1);
724 switch(escseq.mode) {
727 printf("erresc: unknown csi ");
731 case '@': /* ICH -- Insert <n> blank char */
732 DEFAULT(escseq.arg[0], 1);
733 tinsertblank(escseq.arg[0]);
735 case 'A': /* CUU -- Cursor <n> Up */
737 DEFAULT(escseq.arg[0], 1);
738 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
740 case 'B': /* CUD -- Cursor <n> Down */
741 DEFAULT(escseq.arg[0], 1);
742 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
744 case 'C': /* CUF -- Cursor <n> Forward */
746 DEFAULT(escseq.arg[0], 1);
747 tmoveto(term.c.x+escseq.arg[0], term.c.y);
749 case 'D': /* CUB -- Cursor <n> Backward */
750 DEFAULT(escseq.arg[0], 1);
751 tmoveto(term.c.x-escseq.arg[0], term.c.y);
753 case 'E': /* CNL -- Cursor <n> Down and first col */
754 DEFAULT(escseq.arg[0], 1);
755 tmoveto(0, term.c.y+escseq.arg[0]);
757 case 'F': /* CPL -- Cursor <n> Up and first col */
758 DEFAULT(escseq.arg[0], 1);
759 tmoveto(0, term.c.y-escseq.arg[0]);
761 case 'G': /* CHA -- Move to <col> */
762 case '`': /* XXX: HPA -- same? */
763 DEFAULT(escseq.arg[0], 1);
764 tmoveto(escseq.arg[0]-1, term.c.y);
766 case 'H': /* CUP -- Move to <row> <col> */
767 case 'f': /* XXX: HVP -- same? */
768 DEFAULT(escseq.arg[0], 1);
769 DEFAULT(escseq.arg[1], 1);
770 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
772 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
773 case 'J': /* ED -- Clear screen */
774 switch(escseq.arg[0]) {
776 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
779 tclearregion(0, 0, term.c.x, term.c.y);
782 tclearregion(0, 0, term.col-1, term.row-1);
788 case 'K': /* EL -- Clear line */
789 switch(escseq.arg[0]) {
791 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
794 tclearregion(0, term.c.y, term.c.x, term.c.y);
797 tclearregion(0, term.c.y, term.col-1, term.c.y);
801 case 'S': /* SU -- Scroll <n> line up */
802 DEFAULT(escseq.arg[0], 1);
803 tscrollup(escseq.arg[0]);
805 case 'T': /* SD -- Scroll <n> line down */
806 DEFAULT(escseq.arg[0], 1);
807 tscrolldown(escseq.arg[0]);
809 case 'L': /* IL -- Insert <n> blank lines */
810 DEFAULT(escseq.arg[0], 1);
811 tinsertblankline(escseq.arg[0]);
813 case 'l': /* RM -- Reset Mode */
815 switch(escseq.arg[0]) {
817 term.mode &= ~MODE_APPKEYPAD;
820 term.mode &= ~MODE_WRAP;
822 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
825 term.c.state |= CURSOR_HIDE;
827 case 1049: /* = 1047 and 1048 */
829 if(IS_SET(MODE_ALTSCREEN)) {
830 tclearregion(0, 0, term.col-1, term.row-1);
833 if(escseq.arg[0] == 1047)
836 tcursor(CURSOR_LOAD);
842 switch(escseq.arg[0]) {
844 term.mode &= ~MODE_INSERT;
851 case 'M': /* DL -- Delete <n> lines */
852 DEFAULT(escseq.arg[0], 1);
853 tdeleteline(escseq.arg[0]);
855 case 'X': /* ECH -- Erase <n> char */
856 DEFAULT(escseq.arg[0], 1);
857 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
859 case 'P': /* DCH -- Delete <n> char */
860 DEFAULT(escseq.arg[0], 1);
861 tdeletechar(escseq.arg[0]);
863 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
864 case 'd': /* VPA -- Move to <row> */
865 DEFAULT(escseq.arg[0], 1);
866 tmoveto(term.c.x, escseq.arg[0]-1);
868 case 'h': /* SM -- Set terminal mode */
870 switch(escseq.arg[0]) {
872 term.mode |= MODE_APPKEYPAD;
875 term.mode |= MODE_WRAP;
877 case 12: /* att610 -- Start blinking cursor (IGNORED) */
880 term.c.state &= ~CURSOR_HIDE;
882 case 1049: /* = 1047 and 1048 */
884 if(IS_SET(MODE_ALTSCREEN))
885 tclearregion(0, 0, term.col-1, term.row-1);
888 if(escseq.arg[0] == 1047)
891 tcursor(CURSOR_SAVE);
893 default: goto unknown;
896 switch(escseq.arg[0]) {
898 term.mode |= MODE_INSERT;
900 default: goto unknown;
904 case 'm': /* SGR -- Terminal attribute (color) */
905 tsetattr(escseq.arg, escseq.narg);
907 case 'r': /* DECSTBM -- Set Scrolling Region */
911 DEFAULT(escseq.arg[0], 1);
912 DEFAULT(escseq.arg[1], term.row);
913 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
917 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
918 tcursor(CURSOR_SAVE);
920 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
921 tcursor(CURSOR_LOAD);
929 printf("ESC [ %s", escseq.priv ? "? " : "");
931 for(i = 0; i < escseq.narg; i++)
932 printf("%d ", escseq.arg[i]);
934 putchar(escseq.mode);
940 memset(&escseq, 0, sizeof(escseq));
945 int space = TAB - term.c.x % TAB;
946 tmoveto(term.c.x + space, term.c.y);
951 if(term.esc & ESC_START) {
952 if(term.esc & ESC_CSI) {
953 escseq.buf[escseq.len++] = c;
954 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
956 csiparse(), csihandle();
958 /* TODO: handle other OSC */
959 } else if(term.esc & ESC_OSC) {
962 term.esc = ESC_START | ESC_TITLE;
964 } else if(term.esc & ESC_TITLE) {
965 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
967 term.title[term.titlelen] = '\0';
968 XStoreName(xw.dis, xw.win, term.title);
970 term.title[term.titlelen++] = c;
972 } else if(term.esc & ESC_ALTCHARSET) {
974 case '0': /* Line drawing crap */
975 term.c.attr.mode |= ATTR_GFX;
977 case 'B': /* Back to regular text */
978 term.c.attr.mode &= ~ATTR_GFX;
981 printf("esc unhandled charset: ESC ( %c\n", c);
993 term.esc |= ESC_ALTCHARSET;
995 case 'D': /* IND -- Linefeed */
996 if(term.c.y == term.bot)
999 tmoveto(term.c.x, term.c.y+1);
1002 case 'E': /* NEL -- Next line */
1006 case 'M': /* RI -- Reverse index */
1007 if(term.c.y == term.top)
1010 tmoveto(term.c.x, term.c.y-1);
1013 case 'c': /* RIS -- Reset to inital state */
1017 case '=': /* DECPAM -- Application keypad */
1018 term.mode |= MODE_APPKEYPAD;
1021 case '>': /* DECPNM -- Normal keypad */
1022 term.mode &= ~MODE_APPKEYPAD;
1025 case '7': /* DECSC -- Save Cursor */
1026 tcursor(CURSOR_SAVE);
1029 case '8': /* DECRC -- Restore Cursor */
1030 tcursor(CURSOR_LOAD);
1034 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1044 tmoveto(term.c.x-1, term.c.y);
1047 tmoveto(0, term.c.y);
1058 term.esc = ESC_START;
1061 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1064 if(term.c.x+1 < term.col)
1065 tmoveto(term.c.x+1, term.c.y);
1067 term.c.state |= CURSOR_WRAPNEXT;
1074 tputs(char *s, int len) {
1075 for(; len > 0; len--)
1080 tresize(int col, int row) {
1082 int minrow = MIN(row, term.row);
1083 int mincol = MIN(col, term.col);
1085 if(col < 1 || row < 1)
1088 /* free uneeded rows */
1089 for(i = row; i < term.row; i++) {
1094 /* resize to new height */
1095 term.line = realloc(term.line, row * sizeof(Line));
1096 term.alt = realloc(term.alt, row * sizeof(Line));
1098 /* resize each row to new width, zero-pad if needed */
1099 for(i = 0; i < minrow; i++) {
1100 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1101 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1102 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1103 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1106 /* allocate any new rows */
1107 for(/* i == minrow */; i < row; i++) {
1108 term.line[i] = calloc(col, sizeof(Glyph));
1109 term.alt [i] = calloc(col, sizeof(Glyph));
1112 /* update terminal size */
1113 term.col = col, term.row = row;
1114 /* make use of the LIMIT in tmoveto */
1115 tmoveto(term.c.x, term.c.y);
1116 /* reset scrolling region */
1117 tsetscroll(0, row-1);
1124 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1125 unsigned long white = WhitePixel(xw.dis, xw.scr);
1127 for(i = 0; i < 16; i++) {
1128 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1130 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1132 dc.col[i] = color.pixel;
1135 /* same colors as xterm */
1136 for(r = 0; r < 6; r++)
1137 for(g = 0; g < 6; g++)
1138 for(b = 0; b < 6; b++) {
1139 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1140 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1141 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1142 if (!XAllocColor(xw.dis, cmap, &color)) {
1144 fprintf(stderr, "Could not allocate color %d\n", i);
1146 dc.col[i] = color.pixel;
1150 for(r = 0; r < 24; r++, i++) {
1151 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1152 if (!XAllocColor(xw.dis, cmap, &color)) {
1154 fprintf(stderr, "Could not allocate color %d\n", i);
1156 dc.col[i] = color.pixel;
1161 xclear(int x1, int y1, int x2, int y2) {
1162 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1163 XFillRectangle(xw.dis, xw.buf, dc.gc,
1164 x1 * xw.cw, y1 * xw.ch,
1165 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1171 XClassHint class = {TNAME, TNAME};
1172 XWMHints wm = {.flags = InputHint, .input = 1};
1174 .flags = PSize | PResizeInc | PBaseSize,
1177 .height_inc = xw.ch,
1179 .base_height = 2*BORDER,
1180 .base_width = 2*BORDER,
1182 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1187 if(!(xw.dis = XOpenDisplay(NULL)))
1188 die("Can't open display\n");
1189 xw.scr = XDefaultScreen(xw.dis);
1192 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1193 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1195 /* XXX: Assuming same size for bold font */
1196 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1197 xw.ch = dc.font->ascent + dc.font->descent;
1203 xw.bufh = term.row * xw.ch;
1204 xw.bufw = term.col * xw.cw;
1205 xw.h = xw.bufh + 2*BORDER;
1206 xw.w = xw.bufw + 2*BORDER;
1207 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1211 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1213 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1216 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask
1217 | StructureNotifyMask | FocusChangeMask | PointerMotionMask
1218 | ButtonPressMask | ButtonReleaseMask);
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 selpaste(), draw(1);
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);
1441 FD_SET(cmdfd, &rfd);
1443 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1446 die("select failed: %s\n", SERRNO);
1448 if(FD_ISSET(cmdfd, &rfd)) {
1450 draw(SCREEN_UPDATE);
1452 while(XPending(xw.dis)) {
1453 XNextEvent(xw.dis, &ev);
1454 if(handler[ev.type])
1455 (handler[ev.type])(&ev);
1461 main(int argc, char *argv[]) {
1462 if(argc == 2 && !strncmp("-v", argv[1], 3))
1463 die("st-" VERSION ", (c) 2010 st engineers\n");
1465 die("usage: st [-v]\n");
1466 setlocale(LC_CTYPE, "");