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];
130 static void die(const char *errstr, ...);
131 static void draw(int);
132 static void execsh(void);
133 static void sigchld(int);
134 static void run(void);
136 static void csidump(void);
137 static void csihandle(void);
138 static void csiparse(void);
139 static void csireset(void);
141 static void tclearregion(int, int, int, int);
142 static void tcursor(int);
143 static void tdeletechar(int);
144 static void tdeleteline(int);
145 static void tinsertblank(int);
146 static void tinsertblankline(int);
147 static void tmoveto(int, int);
148 static void tnew(int, int);
149 static void tnewline(void);
150 static void tputtab(void);
151 static void tputc(char);
152 static void tputs(char*, int);
153 static void treset(void);
154 static void tresize(int, int);
155 static void tscrollup(int);
156 static void tscrolldown(int);
157 static void tsetattr(int*, int);
158 static void tsetchar(char);
159 static void tsetscroll(int, int);
160 static void tswapscreen(void);
162 static void ttynew(void);
163 static void ttyread(void);
164 static void ttyresize(int, int);
165 static void ttywrite(const char *, size_t);
167 static void xdraws(char *, Glyph, int, int, int);
168 static void xhints(void);
169 static void xclear(int, int, int, int);
170 static void xdrawcursor(void);
171 static void xinit(void);
172 static void xloadcols(void);
173 static void xseturgency(int);
175 static void expose(XEvent *);
176 static char* kmap(KeySym);
177 static void kpress(XEvent *);
178 static void resize(XEvent *);
179 static void focus(XEvent *);
180 static void brelease(XEvent *e);
181 static void bpress(XEvent *e);
182 static void bmotion(XEvent *e);
185 static void (*handler[LASTEvent])(XEvent *) = {
188 [ConfigureNotify] = resize,
191 [MotionNotify] = bmotion,
192 [ButtonPress] = bpress,
193 [ButtonRelease] = brelease,
200 static CSIEscape escseq;
205 // TODO: use X11 clipboard
206 static int selmode = 0;
207 static int selbx = -1, selby;
208 static int selex, seley;
210 static const char *clipboard = NULL;
212 static inline int selected(int x, int y) {
213 if ((seley==y && selby==y)) {
214 int bx = MIN(selbx, selex);
215 int ex = MAX(selbx, selex);
216 return (x>=bx && x<=ex);
218 return (((y>sb[1] && y<se[1]) || (y==se[1] && x<=se[0])) || \
219 (y==sb[1] && x>=sb[0] && (x<=se[0] || sb[1]!=se[1])));
222 static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
223 if(b) *b = e->xbutton.state,
224 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
225 *x = e->xbutton.x/xw.cw;
226 *y = e->xbutton.y/xw.ch;
227 sb[0] = selby<seley?selbx:selex;
228 sb[1] = MIN(selby, seley);
229 se[0] = selby<seley?selex:selbx;
230 se[1] = MAX(selby, seley);
233 static void bpress(XEvent *e) {
235 selex = selbx = e->xbutton.x/xw.cw;
236 seley = selby = e->xbutton.y/xw.ch;
239 static char *getseltext() {
244 sz = ((xw.w/xw.ch) * (se[1]-sb[1]+2));
245 ptr = str = malloc (sz);
246 for(y = 0; y < term.row; y++) {
247 for(x = 0; x < term.col; x++) {
248 if(term.line[y][x].c && (ls=selected(x, y))) {
249 *ptr = term.line[y][x].c;
262 static void clipboard_copy(const char *str) {
263 free((void *)clipboard);
267 static void clipboard_paste() {
269 ttywrite(clipboard, strlen(clipboard));
272 // TODO: doubleclick to select word
273 static void brelease(XEvent *e) {
276 getbuttoninfo(e, &b, &selex, &seley);
283 if(selbx==selex && selby==seley) {
289 clipboard_copy(getseltext());
294 static void bmotion(XEvent *e) {
296 getbuttoninfo(e, NULL, &selex, &seley);
307 for(row = 0; row < term.row; row++) {
308 for(col = 0; col < term.col; col++) {
309 if(col == term.c.x && row == term.c.y)
312 c = term.line[row][col];
313 putchar(c.state & GLYPH_SET ? c.c : '.');
322 die(const char *errstr, ...) {
325 va_start(ap, errstr);
326 vfprintf(stderr, errstr, ap);
333 char *args[3] = {getenv("SHELL"), "-i", NULL};
334 DEFAULT(args[0], SHELL); /* if getenv() failed */
335 putenv("TERM=" TNAME);
336 execvp(args[0], args);
342 if(waitpid(pid, &stat, 0) < 0)
343 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
345 exit(WEXITSTATUS(stat));
354 /* seems to work fine on linux, openbsd and freebsd */
355 struct winsize w = {term.row, term.col, 0, 0};
356 if(openpty(&m, &s, NULL, NULL, &w) < 0)
357 die("openpty failed: %s\n", SERRNO);
359 switch(pid = fork()) {
361 die("fork failed\n");
364 setsid(); /* create a new process group */
365 dup2(s, STDIN_FILENO);
366 dup2(s, STDOUT_FILENO);
367 dup2(s, STDERR_FILENO);
368 if(ioctl(s, TIOCSCTTY, NULL) < 0)
369 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
377 signal(SIGCHLD, sigchld);
384 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
386 fprintf(stderr, "\n");
391 char buf[BUFSIZ] = {0};
394 if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
395 die("Couldn't read from shell: %s\n", SERRNO);
401 ttywrite(const char *s, size_t n) {
402 if(write(cmdfd, s, n) == -1)
403 die("write error on tty: %s\n", SERRNO);
407 ttyresize(int x, int y) {
412 w.ws_xpixel = w.ws_ypixel = 0;
413 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
414 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
421 if(mode == CURSOR_SAVE)
423 else if(mode == CURSOR_LOAD)
424 term.c = c, tmoveto(c.x, c.y);
433 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
435 term.top = 0, term.bot = term.row - 1;
436 term.mode = MODE_WRAP;
437 tclearregion(0, 0, term.col-1, term.row-1);
441 tnew(int col, int row) {
442 /* set screen size */
443 term.row = row, term.col = col;
444 term.line = malloc(term.row * sizeof(Line));
445 term.alt = malloc(term.row * sizeof(Line));
446 for(row = 0 ; row < term.row; row++) {
447 term.line[row] = malloc(term.col * sizeof(Glyph));
448 term.alt [row] = malloc(term.col * sizeof(Glyph));
456 Line* tmp = term.line;
457 term.line = term.alt;
459 term.mode ^= MODE_ALTSCREEN;
463 tscrolldown (int n) {
467 LIMIT(n, 0, term.bot-term.top+1);
469 for(i = 0; i < n; i++)
470 memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
472 for(i = term.bot; i >= term.top+n; i--) {
474 term.line[i] = term.line[i-n];
475 term.line[i-n] = temp;
483 LIMIT(n, 0, term.bot-term.top+1);
485 for(i = 0; i < n; i++)
486 memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
488 for(i = term.top; i <= term.bot-n; i++) {
490 term.line[i] = term.line[i+n];
491 term.line[i+n] = temp;
497 int y = term.c.y + 1;
499 tscrollup(1), y = term.bot;
506 char *p = escseq.buf;
510 escseq.priv = 1, p++;
512 while(p < escseq.buf+escseq.len) {
514 escseq.arg[escseq.narg] *= 10;
515 escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
517 if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
528 tmoveto(int x, int y) {
529 LIMIT(x, 0, term.col-1);
530 LIMIT(y, 0, term.row-1);
531 term.c.state &= ~CURSOR_WRAPNEXT;
538 term.line[term.c.y][term.c.x] = term.c.attr;
539 term.line[term.c.y][term.c.x].c = c;
540 term.line[term.c.y][term.c.x].state |= GLYPH_SET;
544 tclearregion(int x1, int y1, int x2, int y2) {
548 temp = x1, x1 = x2, x2 = temp;
550 temp = y1, y1 = y2, y2 = temp;
552 LIMIT(x1, 0, term.col-1);
553 LIMIT(x2, 0, term.col-1);
554 LIMIT(y1, 0, term.row-1);
555 LIMIT(y2, 0, term.row-1);
557 for(y = y1; y <= y2; y++)
558 memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
563 int src = term.c.x + n;
565 int size = term.col - src;
567 if(src >= term.col) {
568 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
571 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
572 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
576 tinsertblank(int n) {
579 int size = term.col - dst;
581 if(dst >= 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(src, term.c.y, dst - 1, term.c.y);
590 tinsertblankline(int n) {
595 if(term.c.y > term.bot)
597 else if(term.c.y < term.top)
599 if(term.c.y + n >= bot) {
600 tclearregion(0, term.c.y, term.col-1, bot);
603 for(i = bot; i >= term.c.y+n; i--) {
604 /* swap deleted line <-> blanked line */
605 blank = term.line[i];
606 term.line[i] = term.line[i-n];
607 term.line[i-n] = blank;
609 memset(blank, 0, term.col * sizeof(Glyph));
619 if(term.c.y > term.bot)
621 else if(term.c.y < term.top)
623 if(term.c.y + n >= bot) {
624 tclearregion(0, term.c.y, term.col-1, bot);
627 for(i = term.c.y; i <= bot-n; i++) {
628 /* swap deleted line <-> blanked line */
629 blank = term.line[i];
630 term.line[i] = term.line[i+n];
631 term.line[i+n] = blank;
633 memset(blank, 0, term.col * sizeof(Glyph));
638 tsetattr(int *attr, int l) {
641 for(i = 0; i < l; i++) {
644 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
645 term.c.attr.fg = DefaultFG;
646 term.c.attr.bg = DefaultBG;
649 term.c.attr.mode |= ATTR_BOLD;
652 term.c.attr.mode |= ATTR_UNDERLINE;
655 term.c.attr.mode |= ATTR_REVERSE;
658 term.c.attr.mode &= ~ATTR_BOLD;
661 term.c.attr.mode &= ~ATTR_UNDERLINE;
664 term.c.attr.mode &= ~ATTR_REVERSE;
667 if (i + 2 < l && attr[i + 1] == 5) {
669 if (BETWEEN(attr[i], 0, 255))
670 term.c.attr.fg = attr[i];
672 fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
675 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
678 term.c.attr.fg = DefaultFG;
681 if (i + 2 < l && attr[i + 1] == 5) {
683 if (BETWEEN(attr[i], 0, 255))
684 term.c.attr.bg = attr[i];
686 fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
689 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
692 term.c.attr.bg = DefaultBG;
695 if(BETWEEN(attr[i], 30, 37))
696 term.c.attr.fg = attr[i] - 30;
697 else if(BETWEEN(attr[i], 40, 47))
698 term.c.attr.bg = attr[i] - 40;
699 else if(BETWEEN(attr[i], 90, 97))
700 term.c.attr.fg = attr[i] - 90 + 8;
701 else if(BETWEEN(attr[i], 100, 107))
702 term.c.attr.fg = attr[i] - 100 + 8;
704 fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
711 tsetscroll(int t, int b) {
714 LIMIT(t, 0, term.row-1);
715 LIMIT(b, 0, term.row-1);
727 switch(escseq.mode) {
730 printf("erresc: unknown csi ");
734 case '@': /* ICH -- Insert <n> blank char */
735 DEFAULT(escseq.arg[0], 1);
736 tinsertblank(escseq.arg[0]);
738 case 'A': /* CUU -- Cursor <n> Up */
740 DEFAULT(escseq.arg[0], 1);
741 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
743 case 'B': /* CUD -- Cursor <n> Down */
744 DEFAULT(escseq.arg[0], 1);
745 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
747 case 'C': /* CUF -- Cursor <n> Forward */
749 DEFAULT(escseq.arg[0], 1);
750 tmoveto(term.c.x+escseq.arg[0], term.c.y);
752 case 'D': /* CUB -- Cursor <n> Backward */
753 DEFAULT(escseq.arg[0], 1);
754 tmoveto(term.c.x-escseq.arg[0], term.c.y);
756 case 'E': /* CNL -- Cursor <n> Down and first col */
757 DEFAULT(escseq.arg[0], 1);
758 tmoveto(0, term.c.y+escseq.arg[0]);
760 case 'F': /* CPL -- Cursor <n> Up and first col */
761 DEFAULT(escseq.arg[0], 1);
762 tmoveto(0, term.c.y-escseq.arg[0]);
764 case 'G': /* CHA -- Move to <col> */
765 case '`': /* XXX: HPA -- same? */
766 DEFAULT(escseq.arg[0], 1);
767 tmoveto(escseq.arg[0]-1, term.c.y);
769 case 'H': /* CUP -- Move to <row> <col> */
770 case 'f': /* XXX: HVP -- same? */
771 DEFAULT(escseq.arg[0], 1);
772 DEFAULT(escseq.arg[1], 1);
773 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
775 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
776 case 'J': /* ED -- Clear screen */
777 switch(escseq.arg[0]) {
779 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
782 tclearregion(0, 0, term.c.x, term.c.y);
785 tclearregion(0, 0, term.col-1, term.row-1);
787 case 3: /* XXX: erase saved lines (xterm) */
792 case 'K': /* EL -- Clear line */
793 switch(escseq.arg[0]) {
795 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
798 tclearregion(0, term.c.y, term.c.x, term.c.y);
801 tclearregion(0, term.c.y, term.col-1, term.c.y);
805 case 'S': /* SU -- Scroll <n> line up */
806 DEFAULT(escseq.arg[0], 1);
807 tscrollup(escseq.arg[0]);
809 case 'T': /* SD -- Scroll <n> line down */
810 DEFAULT(escseq.arg[0], 1);
811 tscrolldown(escseq.arg[0]);
813 case 'L': /* IL -- Insert <n> blank lines */
814 DEFAULT(escseq.arg[0], 1);
815 tinsertblankline(escseq.arg[0]);
817 case 'l': /* RM -- Reset Mode */
819 switch(escseq.arg[0]) {
821 term.mode &= ~MODE_APPKEYPAD;
824 term.mode &= ~MODE_WRAP;
826 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
829 term.c.state |= CURSOR_HIDE;
831 case 1049: /* = 1047 and 1048 */
833 if(IS_SET(MODE_ALTSCREEN)) {
834 tclearregion(0, 0, term.col-1, term.row-1);
837 if(escseq.arg[0] == 1047)
840 tcursor(CURSOR_LOAD);
846 switch(escseq.arg[0]) {
848 term.mode &= ~MODE_INSERT;
855 case 'M': /* DL -- Delete <n> lines */
856 DEFAULT(escseq.arg[0], 1);
857 tdeleteline(escseq.arg[0]);
859 case 'X': /* ECH -- Erase <n> char */
860 DEFAULT(escseq.arg[0], 1);
861 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
863 case 'P': /* DCH -- Delete <n> char */
864 DEFAULT(escseq.arg[0], 1);
865 tdeletechar(escseq.arg[0]);
867 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
868 case 'd': /* VPA -- Move to <row> */
869 DEFAULT(escseq.arg[0], 1);
870 tmoveto(term.c.x, escseq.arg[0]-1);
872 case 'h': /* SM -- Set terminal mode */
874 switch(escseq.arg[0]) {
876 term.mode |= MODE_APPKEYPAD;
879 term.mode |= MODE_WRAP;
881 case 12: /* att610 -- Start blinking cursor (IGNORED) */
884 term.c.state &= ~CURSOR_HIDE;
886 case 1049: /* = 1047 and 1048 */
888 if(IS_SET(MODE_ALTSCREEN))
889 tclearregion(0, 0, term.col-1, term.row-1);
892 if(escseq.arg[0] == 1047)
895 tcursor(CURSOR_SAVE);
897 default: goto unknown;
900 switch(escseq.arg[0]) {
902 term.mode |= MODE_INSERT;
904 default: goto unknown;
908 case 'm': /* SGR -- Terminal attribute (color) */
909 tsetattr(escseq.arg, escseq.narg);
911 case 'r': /* DECSTBM -- Set Scrolling Region */
915 DEFAULT(escseq.arg[0], 1);
916 DEFAULT(escseq.arg[1], term.row);
917 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
921 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
922 tcursor(CURSOR_SAVE);
924 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
925 tcursor(CURSOR_LOAD);
933 printf("ESC [ %s", escseq.priv ? "? " : "");
935 for(i = 0; i < escseq.narg; i++)
936 printf("%d ", escseq.arg[i]);
938 putchar(escseq.mode);
944 memset(&escseq, 0, sizeof(escseq));
949 int space = TAB - term.c.x % TAB;
950 tmoveto(term.c.x + space, term.c.y);
955 if(term.esc & ESC_START) {
956 if(term.esc & ESC_CSI) {
957 escseq.buf[escseq.len++] = c;
958 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
960 csiparse(), csihandle();
962 } else if(term.esc & ESC_OSC) {
965 term.esc = ESC_START | ESC_TITLE;
967 } else if(term.esc & ESC_TITLE) {
968 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
970 term.title[term.titlelen] = '\0';
971 XStoreName(xw.dis, xw.win, term.title);
973 term.title[term.titlelen++] = c;
975 } else if(term.esc & ESC_ALTCHARSET) {
977 case '0': /* Line drawing crap */
978 term.c.attr.mode |= ATTR_GFX;
980 case 'B': /* Back to regular text */
981 term.c.attr.mode &= ~ATTR_GFX;
984 printf("esc unhandled charset: ESC ( %c\n", c);
996 term.esc |= ESC_ALTCHARSET;
998 case 'D': /* IND -- Linefeed */
999 if(term.c.y == term.bot)
1002 tmoveto(term.c.x, term.c.y+1);
1005 case 'E': /* NEL -- Next line */
1009 case 'M': /* RI -- Reverse index */
1010 if(term.c.y == term.top)
1013 tmoveto(term.c.x, term.c.y-1);
1016 case 'c': /* RIS -- Reset to inital state */
1020 case '=': /* DECPAM -- Application keypad */
1021 term.mode |= MODE_APPKEYPAD;
1024 case '>': /* DECPNM -- Normal keypad */
1025 term.mode &= ~MODE_APPKEYPAD;
1028 case '7': /* DECSC -- Save Cursor*/
1029 tcursor(CURSOR_SAVE);
1032 case '8': /* DECRC -- Restore Cursor */
1033 tcursor(CURSOR_LOAD);
1037 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1047 tmoveto(term.c.x-1, term.c.y);
1050 tmoveto(0, term.c.y);
1061 term.esc = ESC_START;
1064 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1067 if(term.c.x+1 < term.col)
1068 tmoveto(term.c.x+1, term.c.y);
1070 term.c.state |= CURSOR_WRAPNEXT;
1077 tputs(char *s, int len) {
1078 for(; len > 0; len--)
1083 tresize(int col, int row) {
1085 int minrow = MIN(row, term.row);
1086 int mincol = MIN(col, term.col);
1088 if(col < 1 || row < 1)
1091 /* free uneeded rows */
1092 for(i = row; i < term.row; i++) {
1097 /* resize to new height */
1098 term.line = realloc(term.line, row * sizeof(Line));
1099 term.alt = realloc(term.alt, row * sizeof(Line));
1101 /* resize each row to new width, zero-pad if needed */
1102 for(i = 0; i < minrow; i++) {
1103 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1104 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1105 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1106 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1109 /* allocate any new rows */
1110 for(/* i == minrow */; i < row; i++) {
1111 term.line[i] = calloc(col, sizeof(Glyph));
1112 term.alt [i] = calloc(col, sizeof(Glyph));
1115 /* update terminal size */
1116 term.col = col, term.row = row;
1117 /* make use of the LIMIT in tmoveto */
1118 tmoveto(term.c.x, term.c.y);
1119 /* reset scrolling region */
1120 tsetscroll(0, row-1);
1127 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1128 unsigned long white = WhitePixel(xw.dis, xw.scr);
1130 for(i = 0; i < 16; i++) {
1131 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1133 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1135 dc.col[i] = color.pixel;
1138 /* same colors as xterm */
1139 for(r = 0; r < 6; r++)
1140 for(g = 0; g < 6; g++)
1141 for(b = 0; b < 6; b++) {
1142 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1143 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1144 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1145 if (!XAllocColor(xw.dis, cmap, &color)) {
1147 fprintf(stderr, "Could not allocate color %d\n", i);
1149 dc.col[i] = color.pixel;
1153 for(r = 0; r < 24; r++, i++) {
1154 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1155 if (!XAllocColor(xw.dis, cmap, &color)) {
1157 fprintf(stderr, "Could not allocate color %d\n", i);
1159 dc.col[i] = color.pixel;
1164 xclear(int x1, int y1, int x2, int y2) {
1165 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1166 XFillRectangle(xw.dis, xw.buf, dc.gc,
1167 x1 * xw.cw, y1 * xw.ch,
1168 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1174 XClassHint class = {TNAME, TNAME};
1175 XWMHints wm = {.flags = InputHint, .input = 1};
1177 .flags = PSize | PResizeInc | PBaseSize,
1180 .height_inc = xw.ch,
1182 .base_height = 2*BORDER,
1183 .base_width = 2*BORDER,
1185 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1190 if(!(xw.dis = XOpenDisplay(NULL)))
1191 die("Can't open display\n");
1192 xw.scr = XDefaultScreen(xw.dis);
1195 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1196 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1198 /* XXX: Assuming same size for bold font */
1199 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1200 xw.ch = dc.font->ascent + dc.font->descent;
1206 xw.h = term.row * xw.ch + 2*BORDER;
1207 xw.w = term.col * xw.cw + 2*BORDER;
1208 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1212 xw.bufw = xw.w - 2*BORDER;
1213 xw.bufh = xw.h - 2*BORDER;
1214 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);
1218 XMapWindow(xw.dis, xw.win);
1220 XStoreName(xw.dis, xw.win, "st");
1225 xdraws(char *s, Glyph base, int x, int y, int len) {
1226 unsigned long xfg, xbg;
1227 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1230 if(base.mode & ATTR_REVERSE)
1231 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1233 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1235 XSetBackground(xw.dis, dc.gc, xbg);
1236 XSetForeground(xw.dis, dc.gc, xfg);
1238 if(base.mode & ATTR_GFX)
1239 for(i = 0; i < len; i++)
1240 s[i] = gfx[(int)s[i]];
1242 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1243 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1245 if(base.mode & ATTR_UNDERLINE)
1246 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1251 static int oldx = 0;
1252 static int oldy = 0;
1253 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1255 LIMIT(oldx, 0, term.col-1);
1256 LIMIT(oldy, 0, term.row-1);
1258 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1259 g.c = term.line[term.c.y][term.c.x].c;
1261 /* remove the old cursor */
1262 if(term.line[oldy][oldx].state & GLYPH_SET)
1263 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1265 xclear(oldx, oldy, oldx, oldy);
1267 /* draw the new one */
1268 if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
1269 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1270 oldx = term.c.x, oldy = term.c.y;
1275 /* basic drawing routines */
1277 xdrawc(int x, int y, Glyph g) {
1278 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1279 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1280 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1281 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1282 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1289 xclear(0, 0, term.col-1, term.row-1);
1290 for(y = 0; y < term.row; y++)
1291 for(x = 0; x < term.col; x++)
1292 if(term.line[y][x].state & GLYPH_SET)
1293 xdrawc(x, y, term.line[y][x]);
1296 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1301 /* optimized drawing routine */
1303 draw(int redraw_all) {
1306 char buf[DRAW_BUF_SIZ];
1308 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1309 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
1310 for(y = 0; y < term.row; y++) {
1311 base = term.line[y][0];
1313 for(x = 0; x < term.col; x++) {
1314 new = term.line[y][x];
1315 if(selbx!=-1 && new.c && selected(x, y))
1316 new.mode ^= ATTR_REVERSE;
1317 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1318 i >= DRAW_BUF_SIZ)) {
1319 xdraws(buf, base, ox, y, i);
1322 if(new.state & GLYPH_SET) {
1331 xdraws(buf, base, ox, y, i);
1334 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1341 expose(XEvent *ev) {
1342 draw(SCREEN_REDRAW);
1346 xseturgency(int add) {
1347 XWMHints *h = XGetWMHints(xw.dis, xw.win);
1348 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1349 XSetWMHints(xw.dis, xw.win, h);
1355 if((xw.hasfocus = ev->type == FocusIn))
1357 draw(SCREEN_UPDATE);
1363 for(i = 0; i < LEN(key); i++)
1365 return (char*)key[i].s;
1370 kpress(XEvent *ev) {
1371 XKeyEvent *e = &ev->xkey;
1379 meta = e->state & Mod1Mask;
1380 shift = e->state & ShiftMask;
1381 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1383 if((customkey = kmap(ksym)))
1384 ttywrite(customkey, strlen(customkey));
1386 buf[sizeof(buf)-1] = '\0';
1387 if(meta && len == 1)
1388 ttywrite("\033", 1);
1396 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1401 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1404 fprintf(stderr, "errkey: %d\n", (int)ksym);
1413 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1416 xw.w = e->xconfigure.width;
1417 xw.h = e->xconfigure.height;
1418 xw.bufw = xw.w - 2*BORDER;
1419 xw.bufh = xw.h - 2*BORDER;
1420 col = xw.bufw / xw.cw;
1421 row = xw.bufh / xw.ch;
1423 ttyresize(col, row);
1424 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 | FocusChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
1440 XSelectInput(xw.dis, xw.win, mask);
1441 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
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, "");