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 if(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], "/bin/sh"); /* 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;
832 if(IS_SET(MODE_ALTSCREEN)) {
833 tclearregion(0, 0, term.col-1, term.row-1);
838 tcursor(CURSOR_LOAD);
841 tcursor(CURSOR_LOAD);
842 if(IS_SET(MODE_ALTSCREEN)) {
843 tclearregion(0, 0, term.col-1, term.row-1);
851 switch(escseq.arg[0]) {
853 term.mode &= ~MODE_INSERT;
860 case 'M': /* DL -- Delete <n> lines */
861 DEFAULT(escseq.arg[0], 1);
862 tdeleteline(escseq.arg[0]);
864 case 'X': /* ECH -- Erase <n> char */
865 DEFAULT(escseq.arg[0], 1);
866 tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
868 case 'P': /* DCH -- Delete <n> char */
869 DEFAULT(escseq.arg[0], 1);
870 tdeletechar(escseq.arg[0]);
872 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
873 case 'd': /* VPA -- Move to <row> */
874 DEFAULT(escseq.arg[0], 1);
875 tmoveto(term.c.x, escseq.arg[0]-1);
877 case 'h': /* SM -- Set terminal mode */
879 switch(escseq.arg[0]) {
881 term.mode |= MODE_APPKEYPAD;
884 term.mode |= MODE_WRAP;
886 case 12: /* att610 -- Start blinking cursor (IGNORED) */
889 term.c.state &= ~CURSOR_HIDE;
892 if(IS_SET(MODE_ALTSCREEN))
893 tclearregion(0, 0, term.col-1, term.row-1);
898 tcursor(CURSOR_SAVE);
901 tcursor(CURSOR_SAVE);
902 if(IS_SET(MODE_ALTSCREEN))
903 tclearregion(0, 0, term.col-1, term.row-1);
907 default: goto unknown;
910 switch(escseq.arg[0]) {
912 term.mode |= MODE_INSERT;
914 default: goto unknown;
918 case 'm': /* SGR -- Terminal attribute (color) */
919 tsetattr(escseq.arg, escseq.narg);
921 case 'r': /* DECSTBM -- Set Scrolling Region */
925 DEFAULT(escseq.arg[0], 1);
926 DEFAULT(escseq.arg[1], term.row);
927 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
931 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
932 tcursor(CURSOR_SAVE);
934 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
935 tcursor(CURSOR_LOAD);
943 printf("ESC [ %s", escseq.priv ? "? " : "");
945 for(i = 0; i < escseq.narg; i++)
946 printf("%d ", escseq.arg[i]);
948 putchar(escseq.mode);
954 memset(&escseq, 0, sizeof(escseq));
959 int space = TAB - term.c.x % TAB;
960 tmoveto(term.c.x + space, term.c.y);
965 if(term.esc & ESC_START) {
966 if(term.esc & ESC_CSI) {
967 escseq.buf[escseq.len++] = c;
968 if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
970 csiparse(), csihandle();
972 } else if(term.esc & ESC_OSC) {
975 term.esc = ESC_START | ESC_TITLE;
977 } else if(term.esc & ESC_TITLE) {
978 if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
980 term.title[term.titlelen] = '\0';
981 XStoreName(xw.dis, xw.win, term.title);
983 term.title[term.titlelen++] = c;
985 } else if(term.esc & ESC_ALTCHARSET) {
987 case '0': /* Line drawing crap */
988 term.c.attr.mode |= ATTR_GFX;
990 case 'B': /* Back to regular text */
991 term.c.attr.mode &= ~ATTR_GFX;
994 printf("esc unhandled charset: ESC ( %c\n", c);
1000 term.esc |= ESC_CSI;
1003 term.esc |= ESC_OSC;
1006 term.esc |= ESC_ALTCHARSET;
1008 case 'D': /* IND -- Linefeed */
1009 if(term.c.y == term.bot)
1012 tmoveto(term.c.x, term.c.y+1);
1015 case 'E': /* NEL -- Next line */
1019 case 'M': /* RI -- Reverse index */
1020 if(term.c.y == term.top)
1023 tmoveto(term.c.x, term.c.y-1);
1026 case 'c': /* RIS -- Reset to inital state */
1030 case '=': /* DECPAM -- Application keypad */
1031 term.mode |= MODE_APPKEYPAD;
1034 case '>': /* DECPNM -- Normal keypad */
1035 term.mode &= ~MODE_APPKEYPAD;
1038 case '7': /* DECSC -- Save Cursor*/
1039 tcursor(CURSOR_SAVE);
1042 case '8': /* DECRC -- Restore Cursor */
1043 tcursor(CURSOR_LOAD);
1047 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
1057 tmoveto(term.c.x-1, term.c.y);
1060 tmoveto(0, term.c.y);
1071 term.esc = ESC_START;
1074 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
1077 if(term.c.x+1 < term.col)
1078 tmoveto(term.c.x+1, term.c.y);
1080 term.c.state |= CURSOR_WRAPNEXT;
1087 tputs(char *s, int len) {
1088 for(; len > 0; len--)
1093 tresize(int col, int row) {
1095 int minrow = MIN(row, term.row);
1096 int mincol = MIN(col, term.col);
1098 if(col < 1 || row < 1)
1101 /* free uneeded rows */
1102 for(i = row; i < term.row; i++) {
1107 /* resize to new height */
1108 term.line = realloc(term.line, row * sizeof(Line));
1109 term.alt = realloc(term.alt, row * sizeof(Line));
1111 /* resize each row to new width, zero-pad if needed */
1112 for(i = 0; i < minrow; i++) {
1113 term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
1114 term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
1115 memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1116 memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
1119 /* allocate any new rows */
1120 for(/* i == minrow */; i < row; i++) {
1121 term.line[i] = calloc(col, sizeof(Glyph));
1122 term.alt [i] = calloc(col, sizeof(Glyph));
1125 /* update terminal size */
1126 term.col = col, term.row = row;
1127 /* make use of the LIMIT in tmoveto */
1128 tmoveto(term.c.x, term.c.y);
1129 /* reset scrolling region */
1130 tsetscroll(0, row-1);
1137 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
1138 unsigned long white = WhitePixel(xw.dis, xw.scr);
1140 for(i = 0; i < 16; i++) {
1141 if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
1143 fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
1145 dc.col[i] = color.pixel;
1148 /* same colors as xterm */
1149 for(r = 0; r < 6; r++)
1150 for(g = 0; g < 6; g++)
1151 for(b = 0; b < 6; b++) {
1152 color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
1153 color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
1154 color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
1155 if (!XAllocColor(xw.dis, cmap, &color)) {
1157 fprintf(stderr, "Could not allocate color %d\n", i);
1159 dc.col[i] = color.pixel;
1163 for(r = 0; r < 24; r++, i++) {
1164 color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
1165 if (!XAllocColor(xw.dis, cmap, &color)) {
1167 fprintf(stderr, "Could not allocate color %d\n", i);
1169 dc.col[i] = color.pixel;
1174 xclear(int x1, int y1, int x2, int y2) {
1175 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1176 XFillRectangle(xw.dis, xw.buf, dc.gc,
1177 x1 * xw.cw, y1 * xw.ch,
1178 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
1184 XClassHint class = {TNAME, TNAME};
1185 XWMHints wm = {.flags = InputHint, .input = 1};
1187 .flags = PSize | PResizeInc | PBaseSize,
1190 .height_inc = xw.ch,
1192 .base_height = 2*BORDER,
1193 .base_width = 2*BORDER,
1195 XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
1200 if(!(xw.dis = XOpenDisplay(NULL)))
1201 die("Can't open display\n");
1202 xw.scr = XDefaultScreen(xw.dis);
1205 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
1206 die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
1208 /* XXX: Assuming same size for bold font */
1209 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
1210 xw.ch = dc.font->ascent + dc.font->descent;
1216 xw.h = term.row * xw.ch + 2*BORDER;
1217 xw.w = term.col * xw.cw + 2*BORDER;
1218 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
1222 xw.bufw = xw.w - 2*BORDER;
1223 xw.bufh = xw.h - 2*BORDER;
1224 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1226 dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
1227 XMapWindow(xw.dis, xw.win);
1229 XStoreName(xw.dis, xw.win, "st");
1234 xdraws(char *s, Glyph base, int x, int y, int len) {
1235 unsigned long xfg, xbg;
1236 int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
1239 if(base.mode & ATTR_REVERSE)
1240 xfg = dc.col[base.bg], xbg = dc.col[base.fg];
1242 xfg = dc.col[base.fg], xbg = dc.col[base.bg];
1244 XSetBackground(xw.dis, dc.gc, xbg);
1245 XSetForeground(xw.dis, dc.gc, xfg);
1247 if(base.mode & ATTR_GFX)
1248 for(i = 0; i < len; i++)
1249 s[i] = gfx[(int)s[i]];
1251 XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1252 XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
1254 if(base.mode & ATTR_UNDERLINE)
1255 XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
1260 static int oldx = 0;
1261 static int oldy = 0;
1262 Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
1264 LIMIT(oldx, 0, term.col-1);
1265 LIMIT(oldy, 0, term.row-1);
1267 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
1268 g.c = term.line[term.c.y][term.c.x].c;
1270 /* remove the old cursor */
1271 if(term.line[oldy][oldx].state & GLYPH_SET)
1272 xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
1274 xclear(oldx, oldy, oldx, oldy);
1276 /* draw the new one */
1277 if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
1278 xdraws(&g.c, g, term.c.x, term.c.y, 1);
1279 oldx = term.c.x, oldy = term.c.y;
1284 /* basic drawing routines */
1286 xdrawc(int x, int y, Glyph g) {
1287 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
1288 XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
1289 XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
1290 XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
1291 XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
1298 xclear(0, 0, term.col-1, term.row-1);
1299 for(y = 0; y < term.row; y++)
1300 for(x = 0; x < term.col; x++)
1301 if(term.line[y][x].state & GLYPH_SET)
1302 xdrawc(x, y, term.line[y][x]);
1305 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1310 /* optimized drawing routine */
1312 draw(int redraw_all) {
1315 char buf[DRAW_BUF_SIZ];
1317 XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
1318 XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
1319 for(y = 0; y < term.row; y++) {
1320 base = term.line[y][0];
1322 for(x = 0; x < term.col; x++) {
1323 new = term.line[y][x];
1324 if(selbx!=-1 && new.c && selected(x, y))
1325 new.mode = ATTR_REVERSE;
1326 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1327 i >= DRAW_BUF_SIZ)) {
1328 xdraws(buf, base, ox, y, i);
1331 if(new.state & GLYPH_SET) {
1340 xdraws(buf, base, ox, y, i);
1343 XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
1350 expose(XEvent *ev) {
1351 draw(SCREEN_REDRAW);
1355 xseturgency(int add) {
1356 XWMHints *h = XGetWMHints(xw.dis, xw.win);
1357 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
1358 XSetWMHints(xw.dis, xw.win, h);
1364 if((xw.hasfocus = ev->type == FocusIn))
1366 draw(SCREEN_UPDATE);
1372 for(i = 0; i < LEN(key); i++)
1374 return (char*)key[i].s;
1379 kpress(XEvent *ev) {
1380 XKeyEvent *e = &ev->xkey;
1388 meta = e->state & Mod1Mask;
1389 shift = e->state & ShiftMask;
1390 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1392 if((customkey = kmap(ksym)))
1393 ttywrite(customkey, strlen(customkey));
1395 buf[sizeof(buf)-1] = '\0';
1396 if(meta && len == 1)
1397 ttywrite("\033", 1);
1405 sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
1410 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1413 fprintf(stderr, "errkey: %d\n", (int)ksym);
1422 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
1425 xw.w = e->xconfigure.width;
1426 xw.h = e->xconfigure.height;
1427 xw.bufw = xw.w - 2*BORDER;
1428 xw.bufh = xw.h - 2*BORDER;
1429 col = xw.bufw / xw.cw;
1430 row = xw.bufh / xw.ch;
1432 ttyresize(col, row);
1433 XFreePixmap(xw.dis, xw.buf);
1434 xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
1435 draw(SCREEN_REDRAW);
1442 int xfd = XConnectionNumber(xw.dis);
1443 long mask = ExposureMask | KeyPressMask | StructureNotifyMask | FocusChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
1445 XSelectInput(xw.dis, xw.win, mask);
1446 XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
1450 FD_SET(cmdfd, &rfd);
1452 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
1455 die("select failed: %s\n", SERRNO);
1457 if(FD_ISSET(cmdfd, &rfd)) {
1459 draw(SCREEN_UPDATE);
1461 while(XPending(xw.dis)) {
1462 XNextEvent(xw.dis, &ev);
1463 if(handler[ev.type])
1464 (handler[ev.type])(&ev);
1470 main(int argc, char *argv[]) {
1471 if(argc == 2 && !strncmp("-v", argv[1], 3))
1472 die("st-" VERSION ", (c) 2010 st engineers\n");
1474 die("usage: st [-v]\n");
1475 setlocale(LC_CTYPE, "");