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 "st-" VERSION ", (c) 2010 st engineers\n" \
33 "usage: st [-t title] [-e cmd] [-v]\n"
36 #define ESC_TITLE_SIZ 256
37 #define ESC_BUF_SIZ 256
38 #define ESC_ARG_SIZ 16
39 #define DRAW_BUF_SIZ 1024
41 #define SERRNO strerror(errno)
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 #define MAX(a, b) ((a) < (b) ? (b) : (a))
44 #define LEN(a) (sizeof(a) / sizeof(a[0]))
45 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
46 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
47 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
48 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
49 #define IS_SET(flag) (term.mode & (flag))
51 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
52 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
53 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
54 CURSOR_SAVE
, CURSOR_LOAD
};
55 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
56 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
57 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8 };
58 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
59 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
62 char c
; /* character code */
63 char mode
; /* attribute flags */
64 int fg
; /* foreground */
65 int bg
; /* background */
66 char state
; /* state flags */
72 Glyph attr
; /* current char attributes */
78 /* CSI Escape sequence structs */
79 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
81 char buf
[ESC_BUF_SIZ
]; /* raw string */
82 int len
; /* raw string length */
85 int narg
; /* nb of args */
89 /* Internal representation of the screen */
93 Line
* line
; /* screen */
94 Line
* alt
; /* alternate screen */
95 TCursor c
; /* cursor */
96 int top
; /* top scroll limit */
97 int bot
; /* bottom scroll limit */
98 int mode
; /* terminal mode flags */
99 int esc
; /* escape state flags */
100 char title
[ESC_TITLE_SIZ
];
104 /* Purely graphic info */
111 int w
; /* window width */
112 int h
; /* window height */
113 int bufw
; /* pixmap width */
114 int bufh
; /* pixmap height */
115 int ch
; /* char height */
116 int cw
; /* char width */
125 /* Drawing Context */
127 unsigned long col
[256];
133 /* TODO: use better name for vars... */
138 struct {int x
, y
;} b
, e
;
144 static void die(const char *errstr
, ...);
145 static void draw(int);
146 static void execsh(void);
147 static void sigchld(int);
148 static void run(void);
150 static void csidump(void);
151 static void csihandle(void);
152 static void csiparse(void);
153 static void csireset(void);
155 static void tclearregion(int, int, int, int);
156 static void tcursor(int);
157 static void tdeletechar(int);
158 static void tdeleteline(int);
159 static void tinsertblank(int);
160 static void tinsertblankline(int);
161 static void tmoveto(int, int);
162 static void tnew(int, int);
163 static void tnewline(void);
164 static void tputtab(void);
165 static void tputc(char);
166 static void tputs(char*, int);
167 static void treset(void);
168 static void tresize(int, int);
169 static void tscrollup(int, int);
170 static void tscrolldown(int, int);
171 static void tsetattr(int*, int);
172 static void tsetchar(char);
173 static void tsetscroll(int, int);
174 static void tswapscreen(void);
176 static void ttynew(void);
177 static void ttyread(void);
178 static void ttyresize(int, int);
179 static void ttywrite(const char *, size_t);
181 static void xdraws(char *, Glyph
, int, int, int);
182 static void xhints(void);
183 static void xclear(int, int, int, int);
184 static void xdrawcursor(void);
185 static void xinit(void);
186 static void xloadcols(void);
187 static void xseturgency(int);
189 static void expose(XEvent
*);
190 static char* kmap(KeySym
);
191 static void kpress(XEvent
*);
192 static void resize(XEvent
*);
193 static void focus(XEvent
*);
194 static void brelease(XEvent
*);
195 static void bpress(XEvent
*);
196 static void bmotion(XEvent
*);
199 static void (*handler
[LASTEvent
])(XEvent
*) = {
202 [ConfigureNotify
] = resize
,
205 [MotionNotify
] = bmotion
,
206 [ButtonPress
] = bpress
,
207 [ButtonRelease
] = brelease
,
214 static CSIEscape escseq
;
217 static Selection sel
;
218 static char *opt_cmd
= NULL
;
219 static char *opt_title
= NULL
;
228 static inline int selected(int x
, int y
) {
229 if(sel
.ey
== y
&& sel
.by
== y
) {
230 int bx
= MIN(sel
.bx
, sel
.ex
);
231 int ex
= MAX(sel
.bx
, sel
.ex
);
232 return BETWEEN(x
, bx
, ex
);
234 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
235 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
238 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
239 if(b
) *b
= e
->xbutton
.state
,
240 *b
=*b
==4096?5:*b
==2048?4:*b
==1024?3:*b
==512?2:*b
==256?1:-1;
241 *x
= e
->xbutton
.x
/xw
.cw
;
242 *y
= e
->xbutton
.y
/xw
.ch
;
243 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
244 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
245 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
246 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
249 static void bpress(XEvent
*e
) {
251 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
252 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
255 static char *getseltext() {
260 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1);
261 ptr
= str
= malloc(sz
);
262 for(y
= 0; y
< term
.row
; y
++) {
263 for(x
= 0; x
< term
.col
; x
++)
264 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
)))
265 *ptr
= term
.line
[y
][x
].c
, ptr
++;
273 /* TODO: use X11 clipboard */
274 static void selcopy(char *str
) {
279 static void selpaste() {
281 ttywrite(sel
.clip
, strlen(sel
.clip
));
284 /* TODO: doubleclick to select word */
285 static void brelease(XEvent
*e
) {
288 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
289 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
295 selcopy(getseltext());
300 static void bmotion(XEvent
*e
) {
302 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
313 for(row
= 0; row
< term
.row
; row
++) {
314 for(col
= 0; col
< term
.col
; col
++) {
315 if(col
== term
.c
.x
&& row
== term
.c
.y
)
318 c
= term
.line
[row
][col
];
319 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
328 die(const char *errstr
, ...) {
331 va_start(ap
, errstr
);
332 vfprintf(stderr
, errstr
, ap
);
339 char *args
[] = {getenv("SHELL"), "-i", NULL
};
341 args
[0] = opt_cmd
, args
[1] = NULL
;
343 DEFAULT(args
[0], SHELL
);
344 putenv("TERM="TNAME
);
345 execvp(args
[0], args
);
351 if(waitpid(pid
, &stat
, 0) < 0)
352 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
354 exit(WEXITSTATUS(stat
));
363 /* seems to work fine on linux, openbsd and freebsd */
364 struct winsize w
= {term
.row
, term
.col
, 0, 0};
365 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
366 die("openpty failed: %s\n", SERRNO
);
368 switch(pid
= fork()) {
370 die("fork failed\n");
373 setsid(); /* create a new process group */
374 dup2(s
, STDIN_FILENO
);
375 dup2(s
, STDOUT_FILENO
);
376 dup2(s
, STDERR_FILENO
);
377 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
378 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
386 signal(SIGCHLD
, sigchld
);
393 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
395 fprintf(stderr
, "\n");
403 if((ret
= read(cmdfd
, buf
, LEN(buf
))) < 0)
404 die("Couldn't read from shell: %s\n", SERRNO
);
410 ttywrite(const char *s
, size_t n
) {
411 if(write(cmdfd
, s
, n
) == -1)
412 die("write error on tty: %s\n", SERRNO
);
416 ttyresize(int x
, int y
) {
421 w
.ws_xpixel
= w
.ws_ypixel
= 0;
422 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
423 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
430 if(mode
== CURSOR_SAVE
)
432 else if(mode
== CURSOR_LOAD
)
433 term
.c
= c
, tmoveto(c
.x
, c
.y
);
442 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
444 term
.top
= 0, term
.bot
= term
.row
- 1;
445 term
.mode
= MODE_WRAP
;
446 tclearregion(0, 0, term
.col
-1, term
.row
-1);
450 tnew(int col
, int row
) {
451 /* set screen size */
452 term
.row
= row
, term
.col
= col
;
453 term
.line
= malloc(term
.row
* sizeof(Line
));
454 term
.alt
= malloc(term
.row
* sizeof(Line
));
455 for(row
= 0 ; row
< term
.row
; row
++) {
456 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
457 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
465 Line
* tmp
= term
.line
;
466 term
.line
= term
.alt
;
468 term
.mode
^= MODE_ALTSCREEN
;
472 tscrolldown(int orig
, int n
) {
476 LIMIT(n
, 0, term
.bot
-orig
+1);
478 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
480 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
482 term
.line
[i
] = term
.line
[i
-n
];
483 term
.line
[i
-n
] = temp
;
488 tscrollup(int orig
, int n
) {
491 LIMIT(n
, 0, term
.bot
-orig
+1);
493 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
495 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
497 term
.line
[i
] = term
.line
[i
+n
];
498 term
.line
[i
+n
] = temp
;
505 if(term
.c
.y
== term
.bot
)
506 tscrollup(term
.top
, 1);
515 char *p
= escseq
.buf
;
519 escseq
.priv
= 1, p
++;
521 while(p
< escseq
.buf
+escseq
.len
) {
523 escseq
.arg
[escseq
.narg
] *= 10;
524 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
526 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
537 tmoveto(int x
, int y
) {
538 LIMIT(x
, 0, term
.col
-1);
539 LIMIT(y
, 0, term
.row
-1);
540 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
547 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
548 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
549 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
553 tclearregion(int x1
, int y1
, int x2
, int y2
) {
557 temp
= x1
, x1
= x2
, x2
= temp
;
559 temp
= y1
, y1
= y2
, y2
= temp
;
561 LIMIT(x1
, 0, term
.col
-1);
562 LIMIT(x2
, 0, term
.col
-1);
563 LIMIT(y1
, 0, term
.row
-1);
564 LIMIT(y2
, 0, term
.row
-1);
566 for(y
= y1
; y
<= y2
; y
++)
567 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
572 int src
= term
.c
.x
+ n
;
574 int size
= term
.col
- src
;
576 if(src
>= term
.col
) {
577 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
580 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
581 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
585 tinsertblank(int n
) {
588 int size
= term
.col
- dst
;
590 if(dst
>= term
.col
) {
591 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
594 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
595 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
599 tinsertblankline(int n
) {
600 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
603 tscrolldown(term
.c
.y
, n
);
608 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
611 tscrollup(term
.c
.y
, n
);
615 tsetattr(int *attr
, int l
) {
618 for(i
= 0; i
< l
; i
++) {
621 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
622 term
.c
.attr
.fg
= DefaultFG
;
623 term
.c
.attr
.bg
= DefaultBG
;
626 term
.c
.attr
.mode
|= ATTR_BOLD
;
629 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
632 term
.c
.attr
.mode
|= ATTR_REVERSE
;
635 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
638 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
641 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
644 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
646 if (BETWEEN(attr
[i
], 0, 255))
647 term
.c
.attr
.fg
= attr
[i
];
649 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
652 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
655 term
.c
.attr
.fg
= DefaultFG
;
658 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
660 if (BETWEEN(attr
[i
], 0, 255))
661 term
.c
.attr
.bg
= attr
[i
];
663 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
666 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
669 term
.c
.attr
.bg
= DefaultBG
;
672 if(BETWEEN(attr
[i
], 30, 37))
673 term
.c
.attr
.fg
= attr
[i
] - 30;
674 else if(BETWEEN(attr
[i
], 40, 47))
675 term
.c
.attr
.bg
= attr
[i
] - 40;
676 else if(BETWEEN(attr
[i
], 90, 97))
677 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
678 else if(BETWEEN(attr
[i
], 100, 107))
679 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
681 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
689 tsetscroll(int t
, int b
) {
692 LIMIT(t
, 0, term
.row
-1);
693 LIMIT(b
, 0, term
.row
-1);
705 switch(escseq
.mode
) {
708 printf("erresc: unknown csi ");
712 case '@': /* ICH -- Insert <n> blank char */
713 DEFAULT(escseq
.arg
[0], 1);
714 tinsertblank(escseq
.arg
[0]);
716 case 'A': /* CUU -- Cursor <n> Up */
718 DEFAULT(escseq
.arg
[0], 1);
719 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
721 case 'B': /* CUD -- Cursor <n> Down */
722 DEFAULT(escseq
.arg
[0], 1);
723 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
725 case 'C': /* CUF -- Cursor <n> Forward */
727 DEFAULT(escseq
.arg
[0], 1);
728 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
730 case 'D': /* CUB -- Cursor <n> Backward */
731 DEFAULT(escseq
.arg
[0], 1);
732 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
734 case 'E': /* CNL -- Cursor <n> Down and first col */
735 DEFAULT(escseq
.arg
[0], 1);
736 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
738 case 'F': /* CPL -- Cursor <n> Up and first col */
739 DEFAULT(escseq
.arg
[0], 1);
740 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
742 case 'G': /* CHA -- Move to <col> */
743 case '`': /* XXX: HPA -- same? */
744 DEFAULT(escseq
.arg
[0], 1);
745 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
747 case 'H': /* CUP -- Move to <row> <col> */
748 case 'f': /* XXX: HVP -- same? */
749 DEFAULT(escseq
.arg
[0], 1);
750 DEFAULT(escseq
.arg
[1], 1);
751 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
753 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
754 case 'J': /* ED -- Clear screen */
755 switch(escseq
.arg
[0]) {
757 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
760 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
763 tclearregion(0, 0, term
.col
-1, term
.row
-1);
769 case 'K': /* EL -- Clear line */
770 switch(escseq
.arg
[0]) {
772 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
775 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
778 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
782 case 'S': /* SU -- Scroll <n> line up */
783 DEFAULT(escseq
.arg
[0], 1);
784 tscrollup(term
.top
, escseq
.arg
[0]);
786 case 'T': /* SD -- Scroll <n> line down */
787 DEFAULT(escseq
.arg
[0], 1);
788 tscrolldown(term
.top
, escseq
.arg
[0]);
790 case 'L': /* IL -- Insert <n> blank lines */
791 DEFAULT(escseq
.arg
[0], 1);
792 tinsertblankline(escseq
.arg
[0]);
794 case 'l': /* RM -- Reset Mode */
796 switch(escseq
.arg
[0]) {
798 term
.mode
&= ~MODE_APPKEYPAD
;
800 case 5: /* TODO: DECSCNM -- Remove reverse video */
803 term
.mode
&= ~MODE_WRAP
;
805 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
808 term
.c
.state
|= CURSOR_HIDE
;
810 case 1049: /* = 1047 and 1048 */
812 if(IS_SET(MODE_ALTSCREEN
)) {
813 tclearregion(0, 0, term
.col
-1, term
.row
-1);
816 if(escseq
.arg
[0] == 1047)
819 tcursor(CURSOR_LOAD
);
825 switch(escseq
.arg
[0]) {
827 term
.mode
&= ~MODE_INSERT
;
834 case 'M': /* DL -- Delete <n> lines */
835 DEFAULT(escseq
.arg
[0], 1);
836 tdeleteline(escseq
.arg
[0]);
838 case 'X': /* ECH -- Erase <n> char */
839 DEFAULT(escseq
.arg
[0], 1);
840 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
842 case 'P': /* DCH -- Delete <n> char */
843 DEFAULT(escseq
.arg
[0], 1);
844 tdeletechar(escseq
.arg
[0]);
846 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
847 case 'd': /* VPA -- Move to <row> */
848 DEFAULT(escseq
.arg
[0], 1);
849 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
851 case 'h': /* SM -- Set terminal mode */
853 switch(escseq
.arg
[0]) {
855 term
.mode
|= MODE_APPKEYPAD
;
857 case 5: /* DECSCNM -- Reverve video */
858 /* TODO: set REVERSE on the whole screen (f) */
861 term
.mode
|= MODE_WRAP
;
863 case 12: /* att610 -- Start blinking cursor (IGNORED) */
864 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
865 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
868 term
.c
.state
&= ~CURSOR_HIDE
;
870 case 1049: /* = 1047 and 1048 */
872 if(IS_SET(MODE_ALTSCREEN
))
873 tclearregion(0, 0, term
.col
-1, term
.row
-1);
876 if(escseq
.arg
[0] == 1047)
879 tcursor(CURSOR_SAVE
);
881 default: goto unknown
;
884 switch(escseq
.arg
[0]) {
886 term
.mode
|= MODE_INSERT
;
888 default: goto unknown
;
892 case 'm': /* SGR -- Terminal attribute (color) */
893 tsetattr(escseq
.arg
, escseq
.narg
);
895 case 'r': /* DECSTBM -- Set Scrolling Region */
899 DEFAULT(escseq
.arg
[0], 1);
900 DEFAULT(escseq
.arg
[1], term
.row
);
901 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
905 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
906 tcursor(CURSOR_SAVE
);
908 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
909 tcursor(CURSOR_LOAD
);
917 printf("ESC [ %s", escseq
.priv
? "? " : "");
919 for(i
= 0; i
< escseq
.narg
; i
++)
920 printf("%d ", escseq
.arg
[i
]);
922 putchar(escseq
.mode
);
928 memset(&escseq
, 0, sizeof(escseq
));
933 int space
= TAB
- term
.c
.x
% TAB
;
934 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
939 if(term
.esc
& ESC_START
) {
940 if(term
.esc
& ESC_CSI
) {
941 escseq
.buf
[escseq
.len
++] = c
;
942 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
944 csiparse(), csihandle();
946 /* TODO: handle other OSC */
947 } else if(term
.esc
& ESC_OSC
) {
950 term
.esc
= ESC_START
| ESC_TITLE
;
952 } else if(term
.esc
& ESC_TITLE
) {
953 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
955 term
.title
[term
.titlelen
] = '\0';
956 XStoreName(xw
.dis
, xw
.win
, term
.title
);
958 term
.title
[term
.titlelen
++] = c
;
960 } else if(term
.esc
& ESC_ALTCHARSET
) {
962 case '0': /* Line drawing crap */
963 term
.c
.attr
.mode
|= ATTR_GFX
;
965 case 'B': /* Back to regular text */
966 term
.c
.attr
.mode
&= ~ATTR_GFX
;
969 printf("esc unhandled charset: ESC ( %c\n", c
);
981 term
.esc
|= ESC_ALTCHARSET
;
983 case 'D': /* IND -- Linefeed */
984 if(term
.c
.y
== term
.bot
)
985 tscrollup(term
.top
, 1);
987 tmoveto(term
.c
.x
, term
.c
.y
+1);
990 case 'E': /* NEL -- Next line */
994 case 'M': /* RI -- Reverse index */
995 if(term
.c
.y
== term
.top
)
996 tscrolldown(term
.top
, 1);
998 tmoveto(term
.c
.x
, term
.c
.y
-1);
1001 case 'c': /* RIS -- Reset to inital state */
1005 case '=': /* DECPAM -- Application keypad */
1006 term
.mode
|= MODE_APPKEYPAD
;
1009 case '>': /* DECPNM -- Normal keypad */
1010 term
.mode
&= ~MODE_APPKEYPAD
;
1013 case '7': /* DECSC -- Save Cursor */
1014 tcursor(CURSOR_SAVE
);
1017 case '8': /* DECRC -- Restore Cursor */
1018 tcursor(CURSOR_LOAD
);
1022 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
1032 tmoveto(term
.c
.x
-1, term
.c
.y
);
1035 tmoveto(0, term
.c
.y
);
1046 term
.esc
= ESC_START
;
1049 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1052 if(term
.c
.x
+1 < term
.col
)
1053 tmoveto(term
.c
.x
+1, term
.c
.y
);
1055 term
.c
.state
|= CURSOR_WRAPNEXT
;
1062 tputs(char *s
, int len
) {
1063 for(; len
> 0; len
--)
1068 tresize(int col
, int row
) {
1070 int minrow
= MIN(row
, term
.row
);
1071 int mincol
= MIN(col
, term
.col
);
1072 int slide
= term
.c
.y
- row
+ 1;
1074 if(col
< 1 || row
< 1)
1077 /* free unneeded rows */
1080 /* slide screen to keep cursor where we expect it -
1081 * tscrollup would work here, but we can optimize to
1082 * memmove because we're freeing the earlier lines */
1083 for(/* i = 0 */; i
< slide
; i
++) {
1087 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1088 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1090 for(i
+= row
; i
< term
.row
; i
++) {
1095 /* resize to new height */
1096 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1097 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1099 /* resize each row to new width, zero-pad if needed */
1100 for(i
= 0; i
< minrow
; i
++) {
1101 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1102 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1103 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1104 memset(term
.alt
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1107 /* allocate any new rows */
1108 for(/* i == minrow */; i
< row
; i
++) {
1109 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1110 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1113 /* update terminal size */
1114 term
.col
= col
, term
.row
= row
;
1115 /* make use of the LIMIT in tmoveto */
1116 tmoveto(term
.c
.x
, term
.c
.y
);
1117 /* reset scrolling region */
1118 tsetscroll(0, row
-1);
1125 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1127 for(i
= 0; i
< 16; i
++) {
1128 if (!XAllocNamedColor(xw
.dis
, xw
.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
, xw
.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
, xw
.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 XSetWindowAttributes attrs
;
1189 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1190 die("Can't open display\n");
1191 xw
.scr
= XDefaultScreen(xw
.dis
);
1194 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1195 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1197 /* XXX: Assuming same size for bold font */
1198 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1199 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1202 xw
.cmap
= XDefaultColormap(xw
.dis
, xw
.scr
);
1205 /* window - default size */
1206 xw
.bufh
= 24 * xw
.ch
;
1207 xw
.bufw
= 80 * xw
.cw
;
1208 xw
.h
= xw
.bufh
+ 2*BORDER
;
1209 xw
.w
= xw
.bufw
+ 2*BORDER
;
1211 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1212 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1213 attrs
.bit_gravity
= NorthWestGravity
;
1214 attrs
.event_mask
= ExposureMask
| KeyPressMask
1215 | StructureNotifyMask
| FocusChangeMask
| PointerMotionMask
1216 | ButtonPressMask
| ButtonReleaseMask
;
1217 attrs
.colormap
= xw
.cmap
;
1219 xw
.win
= XCreateWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1220 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dis
, xw
.scr
), InputOutput
,
1221 XDefaultVisual(xw
.dis
, xw
.scr
),
1222 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1225 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1227 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1229 XMapWindow(xw
.dis
, xw
.win
);
1231 XStoreName(xw
.dis
, xw
.win
, opt_title
? opt_title
: "st");
1236 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1237 unsigned long xfg
, xbg
;
1238 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1241 if(base
.mode
& ATTR_REVERSE
)
1242 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1244 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1246 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1247 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1249 if(base
.mode
& ATTR_GFX
)
1250 for(i
= 0; i
< len
; i
++) {
1251 char c
= gfx
[(unsigned int)s
[i
] % 256];
1254 else if(s
[i
] > 0x5f)
1258 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1259 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1261 if(base
.mode
& ATTR_UNDERLINE
)
1262 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1267 static int oldx
= 0;
1268 static int oldy
= 0;
1269 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1271 LIMIT(oldx
, 0, term
.col
-1);
1272 LIMIT(oldy
, 0, term
.row
-1);
1274 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1275 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1277 /* remove the old cursor */
1278 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1279 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1281 xclear(oldx
, oldy
, oldx
, oldy
);
1283 /* draw the new one */
1284 if(!(term
.c
.state
& CURSOR_HIDE
) && xw
.hasfocus
) {
1285 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1286 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1291 /* basic drawing routines */
1293 xdrawc(int x
, int y
, Glyph g
) {
1294 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1295 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1296 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1297 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1298 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1305 xclear(0, 0, term
.col
-1, term
.row
-1);
1306 for(y
= 0; y
< term
.row
; y
++)
1307 for(x
= 0; x
< term
.col
; x
++)
1308 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1309 xdrawc(x
, y
, term
.line
[y
][x
]);
1312 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1317 /* optimized drawing routine */
1319 draw(int redraw_all
) {
1322 char buf
[DRAW_BUF_SIZ
];
1324 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1325 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
);
1326 for(y
= 0; y
< term
.row
; y
++) {
1327 base
= term
.line
[y
][0];
1329 for(x
= 0; x
< term
.col
; x
++) {
1330 new = term
.line
[y
][x
];
1331 if(sel
.bx
!=-1 && new.c
&& selected(x
, y
))
1332 new.mode
^= ATTR_REVERSE
;
1333 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1334 i
>= DRAW_BUF_SIZ
)) {
1335 xdraws(buf
, base
, ox
, y
, i
);
1338 if(new.state
& GLYPH_SET
) {
1347 xdraws(buf
, base
, ox
, y
, i
);
1350 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1357 expose(XEvent
*ev
) {
1358 draw(SCREEN_REDRAW
);
1362 xseturgency(int add
) {
1363 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1364 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1365 XSetWMHints(xw
.dis
, xw
.win
, h
);
1371 if((xw
.hasfocus
= ev
->type
== FocusIn
))
1373 draw(SCREEN_UPDATE
);
1379 for(i
= 0; i
< LEN(key
); i
++)
1381 return (char*)key
[i
].s
;
1386 kpress(XEvent
*ev
) {
1387 XKeyEvent
*e
= &ev
->xkey
;
1395 meta
= e
->state
& Mod1Mask
;
1396 shift
= e
->state
& ShiftMask
;
1397 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1399 if((customkey
= kmap(ksym
)))
1400 ttywrite(customkey
, strlen(customkey
));
1402 buf
[sizeof(buf
)-1] = '\0';
1403 if(meta
&& len
== 1)
1404 ttywrite("\033", 1);
1412 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1417 selpaste(), draw(1);
1420 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1429 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1432 xw
.w
= e
->xconfigure
.width
;
1433 xw
.h
= e
->xconfigure
.height
;
1434 xw
.bufw
= xw
.w
- 2*BORDER
;
1435 xw
.bufh
= xw
.h
- 2*BORDER
;
1436 col
= xw
.bufw
/ xw
.cw
;
1437 row
= xw
.bufh
/ xw
.ch
;
1439 ttyresize(col
, row
);
1440 xw
.bufh
= MAX(1, xw
.bufh
);
1441 xw
.bufw
= MAX(1, xw
.bufw
);
1442 XFreePixmap(xw
.dis
, xw
.buf
);
1443 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1450 int xfd
= XConnectionNumber(xw
.dis
);
1454 FD_SET(cmdfd
, &rfd
);
1456 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1459 die("select failed: %s\n", SERRNO
);
1461 if(FD_ISSET(cmdfd
, &rfd
)) {
1463 draw(SCREEN_UPDATE
);
1465 while(XPending(xw
.dis
)) {
1466 XNextEvent(xw
.dis
, &ev
);
1467 if(handler
[ev
.type
])
1468 (handler
[ev
.type
])(&ev
);
1474 main(int argc
, char *argv
[]) {
1477 for(i
= 1; i
< argc
; i
++) {
1478 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1480 if(++i
< argc
) opt_title
= argv
[i
];
1483 if(++i
< argc
) opt_cmd
= argv
[i
];
1490 setlocale(LC_CTYPE
, "");