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/Xatom.h>
21 #include <X11/keysym.h>
22 #include <X11/Xutil.h>
26 #elif defined(__OpenBSD__) || defined(__NetBSD__)
28 #elif defined(__FreeBSD__) || defined(__DragonFly__)
33 "st-" VERSION ", (c) 2010 st engineers\n" \
34 "usage: st [-t title] [-e cmd] [-v]\n"
37 #define ESC_TITLE_SIZ 256
38 #define ESC_BUF_SIZ 256
39 #define ESC_ARG_SIZ 16
40 #define DRAW_BUF_SIZ 1024
42 #define SERRNO strerror(errno)
43 #define MIN(a, b) ((a) < (b) ? (a) : (b))
44 #define MAX(a, b) ((a) < (b) ? (b) : (a))
45 #define LEN(a) (sizeof(a) / sizeof(a[0]))
46 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
47 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
48 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
49 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
50 #define IS_SET(flag) (term.mode & (flag))
52 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
53 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
54 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
55 CURSOR_SAVE
, CURSOR_LOAD
};
56 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
57 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
58 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
60 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
61 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
62 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
65 char c
; /* character code */
66 char mode
; /* attribute flags */
67 int fg
; /* foreground */
68 int bg
; /* background */
69 char state
; /* state flags */
75 Glyph attr
; /* current char attributes */
81 /* CSI Escape sequence structs */
82 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
84 char buf
[ESC_BUF_SIZ
]; /* raw string */
85 int len
; /* raw string length */
88 int narg
; /* nb of args */
92 /* Internal representation of the screen */
96 Line
* line
; /* screen */
97 Line
* alt
; /* alternate screen */
98 TCursor c
; /* cursor */
99 int top
; /* top scroll limit */
100 int bot
; /* bottom scroll limit */
101 int mode
; /* terminal mode flags */
102 int esc
; /* escape state flags */
103 char title
[ESC_TITLE_SIZ
];
107 /* Purely graphic info */
116 int w
; /* window width */
117 int h
; /* window height */
118 int bufw
; /* pixmap width */
119 int bufh
; /* pixmap height */
120 int ch
; /* char height */
121 int cw
; /* char width */
122 char state
; /* focus, redraw, visible */
130 /* Drawing Context */
132 unsigned long col
[256];
138 /* TODO: use better name for vars... */
143 struct {int x
, y
;} b
, e
;
149 static void die(const char *errstr
, ...);
150 static void draw(int);
151 static void execsh(void);
152 static void sigchld(int);
153 static void run(void);
155 static void csidump(void);
156 static void csihandle(void);
157 static void csiparse(void);
158 static void csireset(void);
160 static void tclearregion(int, int, int, int);
161 static void tcursor(int);
162 static void tdeletechar(int);
163 static void tdeleteline(int);
164 static void tinsertblank(int);
165 static void tinsertblankline(int);
166 static void tmoveto(int, int);
167 static void tnew(int, int);
168 static void tnewline(int);
169 static void tputtab(void);
170 static void tputc(char);
171 static void tputs(char*, int);
172 static void treset(void);
173 static void tresize(int, int);
174 static void tscrollup(int, int);
175 static void tscrolldown(int, int);
176 static void tsetattr(int*, int);
177 static void tsetchar(char);
178 static void tsetscroll(int, int);
179 static void tswapscreen(void);
181 static void ttynew(void);
182 static void ttyread(void);
183 static void ttyresize(int, int);
184 static void ttywrite(const char *, size_t);
186 static void xdraws(char *, Glyph
, int, int, int);
187 static void xhints(void);
188 static void xclear(int, int, int, int);
189 static void xdrawcursor(void);
190 static void xinit(void);
191 static void xloadcols(void);
192 static void xseturgency(int);
194 static void expose(XEvent
*);
195 static void visibility(XEvent
*);
196 static void unmap(XEvent
*);
197 static char* kmap(KeySym
);
198 static void kpress(XEvent
*);
199 static void resize(XEvent
*);
200 static void focus(XEvent
*);
201 static void brelease(XEvent
*);
202 static void bpress(XEvent
*);
203 static void bmotion(XEvent
*);
204 static void selection_notify(XEvent
*);
205 static void selection_request(XEvent
*);
207 static void (*handler
[LASTEvent
])(XEvent
*) = {
209 [ConfigureNotify
] = resize
,
210 [VisibilityNotify
] = visibility
,
211 [UnmapNotify
] = unmap
,
215 [MotionNotify
] = bmotion
,
216 [ButtonPress
] = bpress
,
217 [ButtonRelease
] = brelease
,
218 [SelectionNotify
] = selection_notify
,
219 [SelectionRequest
] = selection_request
,
226 static CSIEscape escseq
;
229 static Selection sel
;
230 static char *opt_cmd
= NULL
;
231 static char *opt_title
= NULL
;
240 static inline int selected(int x
, int y
) {
241 if(sel
.ey
== y
&& sel
.by
== y
) {
242 int bx
= MIN(sel
.bx
, sel
.ex
);
243 int ex
= MAX(sel
.bx
, sel
.ex
);
244 return BETWEEN(x
, bx
, ex
);
246 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
247 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
250 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
252 *b
= e
->xbutton
.button
;
254 *x
= e
->xbutton
.x
/xw
.cw
;
255 *y
= e
->xbutton
.y
/xw
.ch
;
256 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
257 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
258 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
259 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
262 static void bpress(XEvent
*e
) {
264 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
265 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
268 static char *getseltext() {
273 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1);
274 ptr
= str
= malloc(sz
);
275 for(y
= 0; y
< term
.row
; y
++) {
276 for(x
= 0; x
< term
.col
; x
++)
277 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
)))
278 *ptr
= term
.line
[y
][x
].c
, ptr
++;
286 static void selection_notify(XEvent
*e
) {
287 unsigned long nitems
;
288 unsigned long length
;
293 res
= XGetWindowProperty(xw
.dis
, xw
.win
, XA_PRIMARY
, 0, 0, False
,
294 AnyPropertyType
, &type
, &format
, &nitems
, &length
, &data
);
299 fprintf(stderr
, "Invalid paste, XGetWindowProperty0");
303 res
= XGetWindowProperty(xw
.dis
, xw
.win
, XA_PRIMARY
, 0, length
, False
,
304 AnyPropertyType
, &type
, &format
, &nitems
, &length
, &data
);
309 fprintf(stderr
, "Invalid paste, XGetWindowProperty0");
314 ttywrite((const char *) data
, nitems
* format
/ 8);
319 static void selpaste() {
320 XConvertSelection(xw
.dis
, XA_PRIMARY
, XA_STRING
, XA_PRIMARY
, xw
.win
, CurrentTime
);
323 static void selection_request(XEvent
*e
)
325 XSelectionRequestEvent
*xsre
;
330 xsre
= (XSelectionRequestEvent
*) e
;
331 xev
.type
= SelectionNotify
;
332 xev
.requestor
= xsre
->requestor
;
333 xev
.selection
= xsre
->selection
;
334 xev
.target
= xsre
->target
;
335 xev
.time
= xsre
->time
;
339 xa_targets
= XInternAtom(xw
.dis
, "TARGETS", 0);
340 if(xsre
->target
== xa_targets
) {
341 /* respond with the supported type */
342 Atom string
= XA_STRING
;
343 res
= XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
, XA_ATOM
, 32,
344 PropModeReplace
, (unsigned char *) &string
, 1);
351 fprintf(stderr
, "Error in selection_request, TARGETS");
354 xev
.property
= xsre
->property
;
356 } else if(xsre
->target
== XA_STRING
) {
357 res
= XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
358 xsre
->target
, 8, PropModeReplace
, (unsigned char *) sel
.clip
,
366 fprintf(stderr
, "Error in selection_request, XA_STRING");
369 xev
.property
= xsre
->property
;
373 /* all done, send a notification to the listener */
374 res
= XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
);
379 fprintf(stderr
, "Error in selection_requested, XSendEvent");
383 static void selcopy(char *str
) {
384 /* register the selection for both the clipboard and the primary */
391 res
= XSetSelectionOwner(xw
.dis
, XA_PRIMARY
, xw
.win
, CurrentTime
);
395 fprintf(stderr
, "Invalid copy, XSetSelectionOwner");
399 clipboard
= XInternAtom(xw
.dis
, "CLIPBOARD", 0);
400 res
= XSetSelectionOwner(xw
.dis
, clipboard
, xw
.win
, CurrentTime
);
404 fprintf(stderr
, "Invalid copy, XSetSelectionOwner");
411 /* TODO: doubleclick to select word */
412 static void brelease(XEvent
*e
) {
415 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
416 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
422 selcopy(getseltext());
427 static void bmotion(XEvent
*e
) {
429 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
440 for(row
= 0; row
< term
.row
; row
++) {
441 for(col
= 0; col
< term
.col
; col
++) {
442 if(col
== term
.c
.x
&& row
== term
.c
.y
)
445 c
= term
.line
[row
][col
];
446 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
455 die(const char *errstr
, ...) {
458 va_start(ap
, errstr
);
459 vfprintf(stderr
, errstr
, ap
);
466 char *args
[] = {getenv("SHELL"), "-i", NULL
};
468 args
[0] = opt_cmd
, args
[1] = NULL
;
470 DEFAULT(args
[0], SHELL
);
471 putenv("TERM="TNAME
);
472 execvp(args
[0], args
);
478 if(waitpid(pid
, &stat
, 0) < 0)
479 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
481 exit(WEXITSTATUS(stat
));
490 /* seems to work fine on linux, openbsd and freebsd */
491 struct winsize w
= {term
.row
, term
.col
, 0, 0};
492 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
493 die("openpty failed: %s\n", SERRNO
);
495 switch(pid
= fork()) {
497 die("fork failed\n");
500 setsid(); /* create a new process group */
501 dup2(s
, STDIN_FILENO
);
502 dup2(s
, STDOUT_FILENO
);
503 dup2(s
, STDERR_FILENO
);
504 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
505 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
513 signal(SIGCHLD
, sigchld
);
520 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
522 fprintf(stderr
, "\n");
530 if((ret
= read(cmdfd
, buf
, LEN(buf
))) < 0)
531 die("Couldn't read from shell: %s\n", SERRNO
);
537 ttywrite(const char *s
, size_t n
) {
538 if(write(cmdfd
, s
, n
) == -1)
539 die("write error on tty: %s\n", SERRNO
);
543 ttyresize(int x
, int y
) {
548 w
.ws_xpixel
= w
.ws_ypixel
= 0;
549 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
550 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
557 if(mode
== CURSOR_SAVE
)
559 else if(mode
== CURSOR_LOAD
)
560 term
.c
= c
, tmoveto(c
.x
, c
.y
);
569 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
571 term
.top
= 0, term
.bot
= term
.row
- 1;
572 term
.mode
= MODE_WRAP
;
573 tclearregion(0, 0, term
.col
-1, term
.row
-1);
577 tnew(int col
, int row
) {
578 /* set screen size */
579 term
.row
= row
, term
.col
= col
;
580 term
.line
= malloc(term
.row
* sizeof(Line
));
581 term
.alt
= malloc(term
.row
* sizeof(Line
));
582 for(row
= 0 ; row
< term
.row
; row
++) {
583 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
584 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
592 Line
* tmp
= term
.line
;
593 term
.line
= term
.alt
;
595 term
.mode
^= MODE_ALTSCREEN
;
599 tscrolldown(int orig
, int n
) {
603 LIMIT(n
, 0, term
.bot
-orig
+1);
605 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
607 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
609 term
.line
[i
] = term
.line
[i
-n
];
610 term
.line
[i
-n
] = temp
;
615 tscrollup(int orig
, int n
) {
618 LIMIT(n
, 0, term
.bot
-orig
+1);
620 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
622 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
624 term
.line
[i
] = term
.line
[i
+n
];
625 term
.line
[i
+n
] = temp
;
630 tnewline(int first_col
) {
633 tscrollup(term
.top
, 1);
636 tmoveto(first_col
? 0 : term
.c
.x
, y
);
642 char *p
= escseq
.buf
;
646 escseq
.priv
= 1, p
++;
648 while(p
< escseq
.buf
+escseq
.len
) {
650 escseq
.arg
[escseq
.narg
] *= 10;
651 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
653 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
664 tmoveto(int x
, int y
) {
665 LIMIT(x
, 0, term
.col
-1);
666 LIMIT(y
, 0, term
.row
-1);
667 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
674 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
675 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
676 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
680 tclearregion(int x1
, int y1
, int x2
, int y2
) {
684 temp
= x1
, x1
= x2
, x2
= temp
;
686 temp
= y1
, y1
= y2
, y2
= temp
;
688 LIMIT(x1
, 0, term
.col
-1);
689 LIMIT(x2
, 0, term
.col
-1);
690 LIMIT(y1
, 0, term
.row
-1);
691 LIMIT(y2
, 0, term
.row
-1);
693 for(y
= y1
; y
<= y2
; y
++)
694 for(x
= x1
; x
<= x2
; x
++)
695 term
.line
[y
][x
].state
= 0;
700 int src
= term
.c
.x
+ n
;
702 int size
= term
.col
- src
;
704 if(src
>= term
.col
) {
705 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
708 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
709 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
713 tinsertblank(int n
) {
716 int size
= term
.col
- dst
;
718 if(dst
>= term
.col
) {
719 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
722 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
723 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
727 tinsertblankline(int n
) {
728 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
731 tscrolldown(term
.c
.y
, n
);
736 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
739 tscrollup(term
.c
.y
, n
);
743 tsetattr(int *attr
, int l
) {
746 for(i
= 0; i
< l
; i
++) {
749 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
750 term
.c
.attr
.fg
= DefaultFG
;
751 term
.c
.attr
.bg
= DefaultBG
;
754 term
.c
.attr
.mode
|= ATTR_BOLD
;
757 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
760 term
.c
.attr
.mode
|= ATTR_REVERSE
;
763 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
766 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
769 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
772 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
774 if (BETWEEN(attr
[i
], 0, 255))
775 term
.c
.attr
.fg
= attr
[i
];
777 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
780 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
783 term
.c
.attr
.fg
= DefaultFG
;
786 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
788 if (BETWEEN(attr
[i
], 0, 255))
789 term
.c
.attr
.bg
= attr
[i
];
791 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
794 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
797 term
.c
.attr
.bg
= DefaultBG
;
800 if(BETWEEN(attr
[i
], 30, 37))
801 term
.c
.attr
.fg
= attr
[i
] - 30;
802 else if(BETWEEN(attr
[i
], 40, 47))
803 term
.c
.attr
.bg
= attr
[i
] - 40;
804 else if(BETWEEN(attr
[i
], 90, 97))
805 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
806 else if(BETWEEN(attr
[i
], 100, 107))
807 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
809 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
817 tsetscroll(int t
, int b
) {
820 LIMIT(t
, 0, term
.row
-1);
821 LIMIT(b
, 0, term
.row
-1);
833 switch(escseq
.mode
) {
836 printf("erresc: unknown csi ");
840 case '@': /* ICH -- Insert <n> blank char */
841 DEFAULT(escseq
.arg
[0], 1);
842 tinsertblank(escseq
.arg
[0]);
844 case 'A': /* CUU -- Cursor <n> Up */
846 DEFAULT(escseq
.arg
[0], 1);
847 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
849 case 'B': /* CUD -- Cursor <n> Down */
850 DEFAULT(escseq
.arg
[0], 1);
851 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
853 case 'C': /* CUF -- Cursor <n> Forward */
855 DEFAULT(escseq
.arg
[0], 1);
856 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
858 case 'D': /* CUB -- Cursor <n> Backward */
859 DEFAULT(escseq
.arg
[0], 1);
860 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
862 case 'E': /* CNL -- Cursor <n> Down and first col */
863 DEFAULT(escseq
.arg
[0], 1);
864 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
866 case 'F': /* CPL -- Cursor <n> Up and first col */
867 DEFAULT(escseq
.arg
[0], 1);
868 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
870 case 'G': /* CHA -- Move to <col> */
871 case '`': /* XXX: HPA -- same? */
872 DEFAULT(escseq
.arg
[0], 1);
873 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
875 case 'H': /* CUP -- Move to <row> <col> */
876 case 'f': /* XXX: HVP -- same? */
877 DEFAULT(escseq
.arg
[0], 1);
878 DEFAULT(escseq
.arg
[1], 1);
879 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
881 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
882 case 'J': /* ED -- Clear screen */
883 switch(escseq
.arg
[0]) {
885 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
888 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
891 tclearregion(0, 0, term
.col
-1, term
.row
-1);
897 case 'K': /* EL -- Clear line */
898 switch(escseq
.arg
[0]) {
900 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
903 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
906 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
910 case 'S': /* SU -- Scroll <n> line up */
911 DEFAULT(escseq
.arg
[0], 1);
912 tscrollup(term
.top
, escseq
.arg
[0]);
914 case 'T': /* SD -- Scroll <n> line down */
915 DEFAULT(escseq
.arg
[0], 1);
916 tscrolldown(term
.top
, escseq
.arg
[0]);
918 case 'L': /* IL -- Insert <n> blank lines */
919 DEFAULT(escseq
.arg
[0], 1);
920 tinsertblankline(escseq
.arg
[0]);
922 case 'l': /* RM -- Reset Mode */
924 switch(escseq
.arg
[0]) {
926 term
.mode
&= ~MODE_APPKEYPAD
;
928 case 5: /* TODO: DECSCNM -- Remove reverse video */
931 term
.mode
&= ~MODE_WRAP
;
933 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
936 term
.mode
&= ~MODE_CRLF
;
939 term
.c
.state
|= CURSOR_HIDE
;
941 case 1049: /* = 1047 and 1048 */
943 if(IS_SET(MODE_ALTSCREEN
)) {
944 tclearregion(0, 0, term
.col
-1, term
.row
-1);
947 if(escseq
.arg
[0] == 1047)
950 tcursor(CURSOR_LOAD
);
956 switch(escseq
.arg
[0]) {
958 term
.mode
&= ~MODE_INSERT
;
965 case 'M': /* DL -- Delete <n> lines */
966 DEFAULT(escseq
.arg
[0], 1);
967 tdeleteline(escseq
.arg
[0]);
969 case 'X': /* ECH -- Erase <n> char */
970 DEFAULT(escseq
.arg
[0], 1);
971 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
973 case 'P': /* DCH -- Delete <n> char */
974 DEFAULT(escseq
.arg
[0], 1);
975 tdeletechar(escseq
.arg
[0]);
977 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
978 case 'd': /* VPA -- Move to <row> */
979 DEFAULT(escseq
.arg
[0], 1);
980 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
982 case 'h': /* SM -- Set terminal mode */
984 switch(escseq
.arg
[0]) {
986 term
.mode
|= MODE_APPKEYPAD
;
988 case 5: /* DECSCNM -- Reverve video */
989 /* TODO: set REVERSE on the whole screen (f) */
992 term
.mode
|= MODE_WRAP
;
995 term
.mode
|= MODE_CRLF
;
997 case 12: /* att610 -- Start blinking cursor (IGNORED) */
998 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
999 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1002 term
.c
.state
&= ~CURSOR_HIDE
;
1004 case 1049: /* = 1047 and 1048 */
1006 if(IS_SET(MODE_ALTSCREEN
))
1007 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1010 if(escseq
.arg
[0] == 1047)
1013 tcursor(CURSOR_SAVE
);
1015 default: goto unknown
;
1018 switch(escseq
.arg
[0]) {
1020 term
.mode
|= MODE_INSERT
;
1022 default: goto unknown
;
1026 case 'm': /* SGR -- Terminal attribute (color) */
1027 tsetattr(escseq
.arg
, escseq
.narg
);
1029 case 'r': /* DECSTBM -- Set Scrolling Region */
1033 DEFAULT(escseq
.arg
[0], 1);
1034 DEFAULT(escseq
.arg
[1], term
.row
);
1035 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1039 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1040 tcursor(CURSOR_SAVE
);
1042 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1043 tcursor(CURSOR_LOAD
);
1051 printf("ESC [ %s", escseq
.priv
? "? " : "");
1053 for(i
= 0; i
< escseq
.narg
; i
++)
1054 printf("%d ", escseq
.arg
[i
]);
1056 putchar(escseq
.mode
);
1062 memset(&escseq
, 0, sizeof(escseq
));
1067 int space
= TAB
- term
.c
.x
% TAB
;
1068 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1073 if(term
.esc
& ESC_START
) {
1074 if(term
.esc
& ESC_CSI
) {
1075 escseq
.buf
[escseq
.len
++] = c
;
1076 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1078 csiparse(), csihandle();
1080 /* TODO: handle other OSC */
1081 } else if(term
.esc
& ESC_OSC
) {
1084 term
.esc
= ESC_START
| ESC_TITLE
;
1086 } else if(term
.esc
& ESC_TITLE
) {
1087 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1089 term
.title
[term
.titlelen
] = '\0';
1090 XStoreName(xw
.dis
, xw
.win
, term
.title
);
1092 term
.title
[term
.titlelen
++] = c
;
1094 } else if(term
.esc
& ESC_ALTCHARSET
) {
1096 case '0': /* Line drawing crap */
1097 term
.c
.attr
.mode
|= ATTR_GFX
;
1099 case 'B': /* Back to regular text */
1100 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1103 printf("esc unhandled charset: ESC ( %c\n", c
);
1109 term
.esc
|= ESC_CSI
;
1112 term
.esc
|= ESC_OSC
;
1115 term
.esc
|= ESC_ALTCHARSET
;
1117 case 'D': /* IND -- Linefeed */
1118 if(term
.c
.y
== term
.bot
)
1119 tscrollup(term
.top
, 1);
1121 tmoveto(term
.c
.x
, term
.c
.y
+1);
1124 case 'E': /* NEL -- Next line */
1125 tnewline(1); /* always go to first col */
1128 case 'M': /* RI -- Reverse index */
1129 if(term
.c
.y
== term
.top
)
1130 tscrolldown(term
.top
, 1);
1132 tmoveto(term
.c
.x
, term
.c
.y
-1);
1135 case 'c': /* RIS -- Reset to inital state */
1139 case '=': /* DECPAM -- Application keypad */
1140 term
.mode
|= MODE_APPKEYPAD
;
1143 case '>': /* DECPNM -- Normal keypad */
1144 term
.mode
&= ~MODE_APPKEYPAD
;
1147 case '7': /* DECSC -- Save Cursor */
1148 tcursor(CURSOR_SAVE
);
1151 case '8': /* DECRC -- Restore Cursor */
1152 tcursor(CURSOR_LOAD
);
1156 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
1166 tmoveto(term
.c
.x
-1, term
.c
.y
);
1169 tmoveto(0, term
.c
.y
);
1172 /* go to first col if the mode is set */
1173 tnewline(IS_SET(MODE_CRLF
));
1176 if(!(xw
.state
& WIN_FOCUSED
))
1181 term
.esc
= ESC_START
;
1184 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1185 tnewline(1); /* always go to first col */
1187 if(term
.c
.x
+1 < term
.col
)
1188 tmoveto(term
.c
.x
+1, term
.c
.y
);
1190 term
.c
.state
|= CURSOR_WRAPNEXT
;
1197 tputs(char *s
, int len
) {
1198 for(; len
> 0; len
--)
1203 tresize(int col
, int row
) {
1205 int minrow
= MIN(row
, term
.row
);
1206 int mincol
= MIN(col
, term
.col
);
1207 int slide
= term
.c
.y
- row
+ 1;
1209 if(col
< 1 || row
< 1)
1212 /* free unneeded rows */
1215 /* slide screen to keep cursor where we expect it -
1216 * tscrollup would work here, but we can optimize to
1217 * memmove because we're freeing the earlier lines */
1218 for(/* i = 0 */; i
< slide
; i
++) {
1222 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1223 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1225 for(i
+= row
; i
< term
.row
; i
++) {
1230 /* resize to new height */
1231 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1232 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1234 /* resize each row to new width, zero-pad if needed */
1235 for(i
= 0; i
< minrow
; i
++) {
1236 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1237 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1238 for(x
= mincol
; x
< col
; x
++) {
1239 term
.line
[i
][x
].state
= 0;
1240 term
.alt
[i
][x
].state
= 0;
1244 /* allocate any new rows */
1245 for(/* i == minrow */; i
< row
; i
++) {
1246 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1247 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1250 /* update terminal size */
1251 term
.col
= col
, term
.row
= row
;
1252 /* make use of the LIMIT in tmoveto */
1253 tmoveto(term
.c
.x
, term
.c
.y
);
1254 /* reset scrolling region */
1255 tsetscroll(0, row
-1);
1262 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1264 for(i
= 0; i
< 16; i
++) {
1265 if (!XAllocNamedColor(xw
.dis
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1267 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1269 dc
.col
[i
] = color
.pixel
;
1272 /* same colors as xterm */
1273 for(r
= 0; r
< 6; r
++)
1274 for(g
= 0; g
< 6; g
++)
1275 for(b
= 0; b
< 6; b
++) {
1276 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1277 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1278 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1279 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1281 fprintf(stderr
, "Could not allocate color %d\n", i
);
1283 dc
.col
[i
] = color
.pixel
;
1287 for(r
= 0; r
< 24; r
++, i
++) {
1288 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1289 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1291 fprintf(stderr
, "Could not allocate color %d\n", i
);
1293 dc
.col
[i
] = color
.pixel
;
1298 xclear(int x1
, int y1
, int x2
, int y2
) {
1299 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1300 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1301 x1
* xw
.cw
, y1
* xw
.ch
,
1302 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1308 XClassHint
class = {TNAME
, TNAME
};
1309 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1311 .flags
= PSize
| PResizeInc
| PBaseSize
,
1314 .height_inc
= xw
.ch
,
1316 .base_height
= 2*BORDER
,
1317 .base_width
= 2*BORDER
,
1319 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1324 XSetWindowAttributes attrs
;
1326 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1327 die("Can't open display\n");
1328 xw
.scr
= XDefaultScreen(xw
.dis
);
1331 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1332 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1334 /* XXX: Assuming same size for bold font */
1335 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1336 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1339 xw
.cmap
= XDefaultColormap(xw
.dis
, xw
.scr
);
1342 /* window - default size */
1343 xw
.bufh
= 24 * xw
.ch
;
1344 xw
.bufw
= 80 * xw
.cw
;
1345 xw
.h
= xw
.bufh
+ 2*BORDER
;
1346 xw
.w
= xw
.bufw
+ 2*BORDER
;
1348 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1349 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1350 attrs
.bit_gravity
= NorthWestGravity
;
1351 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1352 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1353 | PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1354 attrs
.colormap
= xw
.cmap
;
1356 xw
.win
= XCreateWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1357 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dis
, xw
.scr
), InputOutput
,
1358 XDefaultVisual(xw
.dis
, xw
.scr
),
1359 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1362 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1366 xw
.xim
= XOpenIM(xw
.dis
, NULL
, NULL
, NULL
);
1367 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1368 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1369 XNFocusWindow
, xw
.win
, NULL
);
1371 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1373 XMapWindow(xw
.dis
, xw
.win
);
1375 XStoreName(xw
.dis
, xw
.win
, opt_title
? opt_title
: "st");
1380 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1381 unsigned long xfg
, xbg
;
1382 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1385 if(base
.mode
& ATTR_REVERSE
)
1386 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1388 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1390 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1391 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1393 if(base
.mode
& ATTR_GFX
)
1394 for(i
= 0; i
< len
; i
++) {
1395 char c
= gfx
[(unsigned int)s
[i
] % 256];
1398 else if(s
[i
] > 0x5f)
1402 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1403 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1405 if(base
.mode
& ATTR_UNDERLINE
)
1406 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1411 static int oldx
= 0;
1412 static int oldy
= 0;
1413 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1415 LIMIT(oldx
, 0, term
.col
-1);
1416 LIMIT(oldy
, 0, term
.row
-1);
1418 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1419 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1421 /* remove the old cursor */
1422 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1423 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1425 xclear(oldx
, oldy
, oldx
, oldy
);
1427 /* draw the new one */
1428 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1429 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1430 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1435 /* basic drawing routines */
1437 xdrawc(int x
, int y
, Glyph g
) {
1438 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1439 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1440 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1441 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1442 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1449 xclear(0, 0, term
.col
-1, term
.row
-1);
1450 for(y
= 0; y
< term
.row
; y
++)
1451 for(x
= 0; x
< term
.col
; x
++)
1452 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1453 xdrawc(x
, y
, term
.line
[y
][x
]);
1456 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1461 /* optimized drawing routine */
1463 draw(int redraw_all
) {
1466 char buf
[DRAW_BUF_SIZ
];
1468 if(!(xw
.state
& WIN_VISIBLE
))
1471 xclear(0, 0, term
.col
-1, term
.row
-1);
1472 for(y
= 0; y
< term
.row
; y
++) {
1473 base
= term
.line
[y
][0];
1475 for(x
= 0; x
< term
.col
; x
++) {
1476 new = term
.line
[y
][x
];
1477 if(sel
.bx
!=-1 && new.c
&& selected(x
, y
))
1478 new.mode
^= ATTR_REVERSE
;
1479 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1480 i
>= DRAW_BUF_SIZ
)) {
1481 xdraws(buf
, base
, ox
, y
, i
);
1484 if(new.state
& GLYPH_SET
) {
1493 xdraws(buf
, base
, ox
, y
, i
);
1496 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1502 expose(XEvent
*ev
) {
1503 XExposeEvent
*e
= &ev
->xexpose
;
1504 if(xw
.state
& WIN_REDRAW
) {
1506 xw
.state
&= ~WIN_REDRAW
;
1507 draw(SCREEN_REDRAW
);
1510 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1511 e
->width
, e
->height
, e
->x
, e
->y
);
1515 visibility(XEvent
*ev
) {
1516 XVisibilityEvent
*e
= &ev
->xvisibility
;
1517 if(e
->state
== VisibilityFullyObscured
)
1518 xw
.state
&= ~WIN_VISIBLE
;
1519 else if(!(xw
.state
& WIN_VISIBLE
))
1520 /* need a full redraw for next Expose, not just a buf copy */
1521 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1526 xw
.state
&= ~WIN_VISIBLE
;
1530 xseturgency(int add
) {
1531 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1532 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1533 XSetWMHints(xw
.dis
, xw
.win
, h
);
1539 if(ev
->type
== FocusIn
) {
1540 xw
.state
|= WIN_FOCUSED
;
1543 xw
.state
&= ~WIN_FOCUSED
;
1544 draw(SCREEN_UPDATE
);
1550 for(i
= 0; i
< LEN(key
); i
++)
1552 return (char*)key
[i
].s
;
1557 kpress(XEvent
*ev
) {
1558 XKeyEvent
*e
= &ev
->xkey
;
1567 meta
= e
->state
& Mod1Mask
;
1568 shift
= e
->state
& ShiftMask
;
1569 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1571 /* 1. custom keys from config.h */
1572 if((customkey
= kmap(ksym
)))
1573 ttywrite(customkey
, strlen(customkey
));
1574 /* 2. hardcoded (overrides X lookup) */
1581 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1589 if(IS_SET(MODE_CRLF
))
1590 ttywrite("\r\n", 2);
1597 buf
[sizeof(buf
)-1] = '\0';
1598 if(meta
&& len
== 1)
1599 ttywrite("\033", 1);
1601 } else /* 4. nothing to send */
1602 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1611 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1614 xw
.w
= e
->xconfigure
.width
;
1615 xw
.h
= e
->xconfigure
.height
;
1616 xw
.bufw
= xw
.w
- 2*BORDER
;
1617 xw
.bufh
= xw
.h
- 2*BORDER
;
1618 col
= xw
.bufw
/ xw
.cw
;
1619 row
= xw
.bufh
/ xw
.ch
;
1621 ttyresize(col
, row
);
1622 xw
.bufh
= MAX(1, xw
.bufh
);
1623 xw
.bufw
= MAX(1, xw
.bufw
);
1624 XFreePixmap(xw
.dis
, xw
.buf
);
1625 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1632 int xfd
= XConnectionNumber(xw
.dis
);
1636 FD_SET(cmdfd
, &rfd
);
1638 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1641 die("select failed: %s\n", SERRNO
);
1643 if(FD_ISSET(cmdfd
, &rfd
)) {
1645 draw(SCREEN_UPDATE
);
1647 while(XPending(xw
.dis
)) {
1648 XNextEvent(xw
.dis
, &ev
);
1649 if (XFilterEvent(&ev
, xw
.win
))
1651 if(handler
[ev
.type
])
1652 (handler
[ev
.type
])(&ev
);
1658 main(int argc
, char *argv
[]) {
1661 for(i
= 1; i
< argc
; i
++) {
1662 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1664 if(++i
< argc
) opt_title
= argv
[i
];
1667 if(++i
< argc
) opt_cmd
= argv
[i
];
1674 setlocale(LC_CTYPE
, "");