1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/extensions/Xdbe.h>
31 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
33 #elif defined(__FreeBSD__) || defined(__DragonFly__)
38 "st " VERSION " (c) 2010-2012 st engineers\n" \
39 "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
42 #define XEMBED_FOCUS_IN 4
43 #define XEMBED_FOCUS_OUT 5
46 #define ESC_BUF_SIZ 256
47 #define ESC_ARG_SIZ 16
48 #define STR_BUF_SIZ 256
49 #define STR_ARG_SIZ 16
50 #define DRAW_BUF_SIZ 1024
52 #define XK_NO_MOD UINT_MAX
55 #define SELECT_TIMEOUT (20*1000) /* 20 ms */
56 #define DRAW_TIMEOUT (20*1000) /* 20 ms */
58 #define SERRNO strerror(errno)
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 #define LEN(a) (sizeof(a) / sizeof(a[0]))
62 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
63 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
64 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
65 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
66 #define IS_SET(flag) (term.mode & (flag))
67 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
68 #define X2COL(x) (((x) - BORDER)/xw.cw)
69 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
71 enum glyph_attribute
{
79 enum cursor_movement
{
106 MODE_MOUSEMOTION
= 64,
114 ESC_STR
= 4, /* DSC, OSC, PM, APC */
116 ESC_STR_END
= 16, /* a final string was encountered */
127 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
129 typedef unsigned char uchar
;
130 typedef unsigned int uint
;
131 typedef unsigned long ulong
;
132 typedef unsigned short ushort
;
135 char c
[UTF_SIZ
]; /* character code */
136 uchar mode
; /* attribute flags */
137 ushort fg
; /* foreground */
138 ushort bg
; /* background */
139 uchar state
; /* state flags */
145 Glyph attr
; /* current char attributes */
151 /* CSI Escape sequence structs */
152 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
154 char buf
[ESC_BUF_SIZ
]; /* raw string */
155 int len
; /* raw string length */
157 int arg
[ESC_ARG_SIZ
];
158 int narg
; /* nb of args */
162 /* STR Escape sequence structs */
163 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
165 char type
; /* ESC type ... */
166 char buf
[STR_BUF_SIZ
]; /* raw string */
167 int len
; /* raw string length */
168 char *args
[STR_ARG_SIZ
];
169 int narg
; /* nb of args */
172 /* Internal representation of the screen */
174 int row
; /* nb row */
175 int col
; /* nb col */
176 Line
* line
; /* screen */
177 Line
* alt
; /* alternate screen */
178 bool* dirty
; /* dirtyness of lines */
179 TCursor c
; /* cursor */
180 int top
; /* top scroll limit */
181 int bot
; /* bottom scroll limit */
182 int mode
; /* terminal mode flags */
183 int esc
; /* escape state flags */
187 /* Purely graphic info */
197 int w
; /* window width */
198 int h
; /* window height */
199 int ch
; /* char height */
200 int cw
; /* char width */
201 char state
; /* focus, redraw, visible */
202 struct timeval lastdraw
;
212 /* TODO: use better name for vars... */
217 struct {int x
, y
;} b
, e
;
220 struct timeval tclick1
;
221 struct timeval tclick2
;
226 /* Drawing Context */
228 ulong col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
239 static void die(const char*, ...);
240 static void draw(void);
241 static void drawregion(int, int, int, int);
242 static void execsh(void);
243 static void sigchld(int);
244 static void run(void);
245 static bool last_draw_too_old(void);
247 static void csidump(void);
248 static void csihandle(void);
249 static void csiparse(void);
250 static void csireset(void);
251 static void strdump(void);
252 static void strhandle(void);
253 static void strparse(void);
254 static void strreset(void);
256 static void tclearregion(int, int, int, int);
257 static void tcursor(int);
258 static void tdeletechar(int);
259 static void tdeleteline(int);
260 static void tinsertblank(int);
261 static void tinsertblankline(int);
262 static void tmoveto(int, int);
263 static void tnew(int, int);
264 static void tnewline(int);
265 static void tputtab(bool);
266 static void tputc(char*);
267 static void treset(void);
268 static int tresize(int, int);
269 static void tscrollup(int, int);
270 static void tscrolldown(int, int);
271 static void tsetattr(int*, int);
272 static void tsetchar(char*);
273 static void tsetscroll(int, int);
274 static void tswapscreen(void);
275 static void tsetdirt(int, int);
276 static void tsetmode(bool, bool, int *, int);
277 static void tfulldirt(void);
279 static void ttynew(void);
280 static void ttyread(void);
281 static void ttyresize(int, int);
282 static void ttywrite(const char *, size_t);
284 static void xdraws(char *, Glyph
, int, int, int, int);
285 static void xhints(void);
286 static void xclear(int, int, int, int);
288 static void xdrawcursor(void);
289 static void xinit(void);
290 static void xloadcols(void);
291 static void xseturgency(int);
292 static void xsetsel(char*);
293 static void xresize(int, int);
295 static void expose(XEvent
*);
296 static void visibility(XEvent
*);
297 static void unmap(XEvent
*);
298 static char* kmap(KeySym
, uint
);
299 static void kpress(XEvent
*);
300 static void cmessage(XEvent
*);
301 static void resize(XEvent
*);
302 static void focus(XEvent
*);
303 static void brelease(XEvent
*);
304 static void bpress(XEvent
*);
305 static void bmotion(XEvent
*);
306 static void selnotify(XEvent
*);
307 static void selrequest(XEvent
*);
309 static void selinit(void);
310 static inline bool selected(int, int);
311 static void selcopy(void);
312 static void selpaste();
313 static void selscroll(int, int);
315 static int utf8decode(char *, long *);
316 static int utf8encode(long *, char *);
317 static int utf8size(char *);
318 static int isfullutf8(char *, int);
320 static void (*handler
[LASTEvent
])(XEvent
*) = {
322 [ClientMessage
] = cmessage
,
323 [ConfigureNotify
] = resize
,
324 [VisibilityNotify
] = visibility
,
325 [UnmapNotify
] = unmap
,
329 [MotionNotify
] = bmotion
,
330 [ButtonPress
] = bpress
,
331 [ButtonRelease
] = brelease
,
332 [SelectionNotify
] = selnotify
,
333 [SelectionRequest
] = selrequest
,
340 static CSIEscape csiescseq
;
341 static STREscape strescseq
;
344 static Selection sel
;
345 static char **opt_cmd
= NULL
;
346 static char *opt_title
= NULL
;
347 static char *opt_embed
= NULL
;
348 static char *opt_class
= NULL
;
351 utf8decode(char *s
, long *u
) {
357 if(~c
& B7
) { /* 0xxxxxxx */
360 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
361 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
363 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
364 *u
= c
&(B3
|B2
|B1
|B0
);
366 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
371 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
373 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
376 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
378 if((n
== 1 && *u
< 0x80) ||
379 (n
== 2 && *u
< 0x800) ||
380 (n
== 3 && *u
< 0x10000) ||
381 (*u
>= 0xD800 && *u
<= 0xDFFF))
390 utf8encode(long *u
, char *s
) {
398 *sp
= uc
; /* 0xxxxxxx */
400 } else if(*u
< 0x800) {
401 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
403 } else if(uc
< 0x10000) {
404 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
406 } else if(uc
<= 0x10FFFF) {
407 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
412 for(i
=n
,++sp
; i
>0; --i
,++sp
)
413 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
423 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
424 UTF-8 otherwise return 0 */
426 isfullutf8(char *s
, int b
) {
434 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
436 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
438 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
440 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
442 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
443 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
455 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
457 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
465 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
466 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
470 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
471 if(sel
.xtarget
== None
)
472 sel
.xtarget
= XA_STRING
;
476 selected(int x
, int y
) {
477 if(sel
.ey
== y
&& sel
.by
== y
) {
478 int bx
= MIN(sel
.bx
, sel
.ex
);
479 int ex
= MAX(sel
.bx
, sel
.ex
);
480 return BETWEEN(x
, bx
, ex
);
482 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
483 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
487 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
489 *b
= e
->xbutton
.button
;
491 *x
= X2COL(e
->xbutton
.x
);
492 *y
= Y2ROW(e
->xbutton
.y
);
493 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
494 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
495 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
496 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
500 mousereport(XEvent
*e
) {
501 int x
= X2COL(e
->xbutton
.x
);
502 int y
= Y2ROW(e
->xbutton
.y
);
503 int button
= e
->xbutton
.button
;
504 int state
= e
->xbutton
.state
;
505 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
506 static int ob
, ox
, oy
;
509 if(e
->xbutton
.type
== MotionNotify
) {
510 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
514 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
520 if(e
->xbutton
.type
== ButtonPress
) {
526 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
527 + (state
& Mod4Mask
? 8 : 0)
528 + (state
& ControlMask
? 16 : 0);
530 ttywrite(buf
, sizeof(buf
));
535 if(IS_SET(MODE_MOUSE
))
537 else if(e
->xbutton
.button
== Button1
) {
539 tsetdirt(sel
.b
.y
, sel
.e
.y
);
541 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
542 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
549 int x
, y
, bufsize
, is_selected
= 0;
555 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
556 ptr
= str
= malloc(bufsize
);
558 /* append every set & selected glyph to the selection */
559 for(y
= 0; y
< term
.row
; y
++) {
560 for(x
= 0; x
< term
.col
; x
++) {
561 is_selected
= selected(x
, y
);
562 if((term
.line
[y
][x
].state
& GLYPH_SET
) && is_selected
) {
563 int size
= utf8size(term
.line
[y
][x
].c
);
564 memcpy(ptr
, term
.line
[y
][x
].c
, size
);
569 /* \n at the end of every selected line except for the last one */
570 if(is_selected
&& y
< sel
.e
.y
)
579 selnotify(XEvent
*e
) {
580 ulong nitems
, ofs
, rem
;
587 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
588 False
, AnyPropertyType
, &type
, &format
,
589 &nitems
, &rem
, &data
)) {
590 fprintf(stderr
, "Clipboard allocation failed\n");
593 ttywrite((const char *) data
, nitems
* format
/ 8);
595 /* number of 32-bit chunks returned */
596 ofs
+= nitems
* format
/ 32;
602 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
606 selrequest(XEvent
*e
) {
607 XSelectionRequestEvent
*xsre
;
611 xsre
= (XSelectionRequestEvent
*) e
;
612 xev
.type
= SelectionNotify
;
613 xev
.requestor
= xsre
->requestor
;
614 xev
.selection
= xsre
->selection
;
615 xev
.target
= xsre
->target
;
616 xev
.time
= xsre
->time
;
620 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
621 if(xsre
->target
== xa_targets
) {
622 /* respond with the supported type */
623 Atom string
= sel
.xtarget
;
624 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
625 XA_ATOM
, 32, PropModeReplace
,
626 (uchar
*) &string
, 1);
627 xev
.property
= xsre
->property
;
628 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
629 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
630 xsre
->target
, 8, PropModeReplace
,
631 (uchar
*) sel
.clip
, strlen(sel
.clip
));
632 xev
.property
= xsre
->property
;
635 /* all done, send a notification to the listener */
636 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
637 fprintf(stderr
, "Error sending SelectionNotify event\n");
642 /* register the selection for both the clipboard and the primary */
648 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
650 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
651 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
657 brelease(XEvent
*e
) {
658 if(IS_SET(MODE_MOUSE
)) {
662 if(e
->xbutton
.button
== Button2
)
664 else if(e
->xbutton
.button
== Button1
) {
666 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
667 term
.dirty
[sel
.ey
] = 1;
668 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
671 gettimeofday(&now
, NULL
);
673 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
674 /* triple click on the line */
675 sel
.b
.x
= sel
.bx
= 0;
676 sel
.e
.x
= sel
.ex
= term
.col
;
677 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
679 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
680 /* double click to select word */
682 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
683 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
685 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
686 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
688 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
694 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
695 gettimeofday(&sel
.tclick1
, NULL
);
701 if(IS_SET(MODE_MOUSE
)) {
706 int oldey
= sel
.ey
, oldex
= sel
.ex
;
707 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
709 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
710 int starty
= MIN(oldey
, sel
.ey
);
711 int endy
= MAX(oldey
, sel
.ey
);
712 tsetdirt(starty
, endy
);
719 die(const char *errstr
, ...) {
722 va_start(ap
, errstr
);
723 vfprintf(stderr
, errstr
, ap
);
731 char *envshell
= getenv("SHELL");
733 DEFAULT(envshell
, SHELL
);
734 putenv("TERM="TNAME
);
735 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
736 execvp(args
[0], args
);
743 if(waitpid(pid
, &stat
, 0) < 0)
744 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
746 exit(WEXITSTATUS(stat
));
755 /* seems to work fine on linux, openbsd and freebsd */
756 struct winsize w
= {term
.row
, term
.col
, 0, 0};
757 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
758 die("openpty failed: %s\n", SERRNO
);
760 switch(pid
= fork()) {
762 die("fork failed\n");
765 setsid(); /* create a new process group */
766 dup2(s
, STDIN_FILENO
);
767 dup2(s
, STDOUT_FILENO
);
768 dup2(s
, STDERR_FILENO
);
769 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
770 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
778 signal(SIGCHLD
, sigchld
);
785 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
787 fprintf(stderr
, "\n");
792 static char buf
[BUFSIZ
];
793 static int buflen
= 0;
796 int charsize
; /* size of utf8 char in bytes */
800 /* append read bytes to unprocessed bytes */
801 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
802 die("Couldn't read from shell: %s\n", SERRNO
);
804 /* process every complete utf8 char */
807 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
808 charsize
= utf8decode(ptr
, &utf8c
);
809 utf8encode(&utf8c
, s
);
815 /* keep any uncomplete utf8 char for the next call */
816 memmove(buf
, ptr
, buflen
);
820 ttywrite(const char *s
, size_t n
) {
821 if(write(cmdfd
, s
, n
) == -1)
822 die("write error on tty: %s\n", SERRNO
);
826 ttyresize(int x
, int y
) {
831 w
.ws_xpixel
= w
.ws_ypixel
= 0;
832 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
833 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
837 tsetdirt(int top
, int bot
)
841 LIMIT(top
, 0, term
.row
-1);
842 LIMIT(bot
, 0, term
.row
-1);
844 for(i
= top
; i
<= bot
; i
++)
851 tsetdirt(0, term
.row
-1);
858 if(mode
== CURSOR_SAVE
)
860 else if(mode
== CURSOR_LOAD
)
861 term
.c
= c
, tmoveto(c
.x
, c
.y
);
871 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
873 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
874 for (i
= TAB
; i
< term
.col
; i
+= TAB
)
876 term
.top
= 0, term
.bot
= term
.row
- 1;
877 term
.mode
= MODE_WRAP
;
878 tclearregion(0, 0, term
.col
-1, term
.row
-1);
882 tnew(int col
, int row
) {
883 /* set screen size */
884 term
.row
= row
, term
.col
= col
;
885 term
.line
= malloc(term
.row
* sizeof(Line
));
886 term
.alt
= malloc(term
.row
* sizeof(Line
));
887 term
.dirty
= malloc(term
.row
* sizeof(*term
.dirty
));
888 term
.tabs
= malloc(term
.col
* sizeof(*term
.tabs
));
890 for(row
= 0; row
< term
.row
; row
++) {
891 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
892 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
895 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
902 Line
* tmp
= term
.line
;
903 term
.line
= term
.alt
;
905 term
.mode
^= MODE_ALTSCREEN
;
910 tscrolldown(int orig
, int n
) {
914 LIMIT(n
, 0, term
.bot
-orig
+1);
916 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
918 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
920 term
.line
[i
] = term
.line
[i
-n
];
921 term
.line
[i
-n
] = temp
;
931 tscrollup(int orig
, int n
) {
934 LIMIT(n
, 0, term
.bot
-orig
+1);
936 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
938 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
940 term
.line
[i
] = term
.line
[i
+n
];
941 term
.line
[i
+n
] = temp
;
951 selscroll(int orig
, int n
) {
955 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
956 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
960 if(sel
.by
< term
.top
) {
964 if(sel
.ey
> term
.bot
) {
968 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
969 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
974 tnewline(int first_col
) {
977 tscrollup(term
.top
, 1);
980 tmoveto(first_col
? 0 : term
.c
.x
, y
);
986 char *p
= csiescseq
.buf
;
990 csiescseq
.priv
= 1, p
++;
992 while(p
< csiescseq
.buf
+csiescseq
.len
) {
994 csiescseq
.arg
[csiescseq
.narg
] *= 10;
995 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
997 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
998 csiescseq
.narg
++, p
++;
1000 csiescseq
.mode
= *p
;
1008 tmoveto(int x
, int y
) {
1009 LIMIT(x
, 0, term
.col
-1);
1010 LIMIT(y
, 0, term
.row
-1);
1011 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1018 term
.dirty
[term
.c
.y
] = 1;
1019 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1020 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1021 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1025 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1029 temp
= x1
, x1
= x2
, x2
= temp
;
1031 temp
= y1
, y1
= y2
, y2
= temp
;
1033 LIMIT(x1
, 0, term
.col
-1);
1034 LIMIT(x2
, 0, term
.col
-1);
1035 LIMIT(y1
, 0, term
.row
-1);
1036 LIMIT(y2
, 0, term
.row
-1);
1038 for(y
= y1
; y
<= y2
; y
++) {
1040 for(x
= x1
; x
<= x2
; x
++)
1041 term
.line
[y
][x
].state
= 0;
1046 tdeletechar(int n
) {
1047 int src
= term
.c
.x
+ n
;
1049 int size
= term
.col
- src
;
1051 term
.dirty
[term
.c
.y
] = 1;
1053 if(src
>= term
.col
) {
1054 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1057 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1058 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1062 tinsertblank(int n
) {
1065 int size
= term
.col
- dst
;
1067 term
.dirty
[term
.c
.y
] = 1;
1069 if(dst
>= term
.col
) {
1070 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1073 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1074 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1078 tinsertblankline(int n
) {
1079 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1082 tscrolldown(term
.c
.y
, n
);
1086 tdeleteline(int n
) {
1087 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1090 tscrollup(term
.c
.y
, n
);
1094 tsetattr(int *attr
, int l
) {
1097 for(i
= 0; i
< l
; i
++) {
1100 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
1101 term
.c
.attr
.fg
= DefaultFG
;
1102 term
.c
.attr
.bg
= DefaultBG
;
1105 term
.c
.attr
.mode
|= ATTR_BOLD
;
1108 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1111 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1114 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1117 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1120 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1123 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1125 if(BETWEEN(attr
[i
], 0, 255))
1126 term
.c
.attr
.fg
= attr
[i
];
1128 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1131 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1134 term
.c
.attr
.fg
= DefaultFG
;
1137 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1139 if(BETWEEN(attr
[i
], 0, 255))
1140 term
.c
.attr
.bg
= attr
[i
];
1142 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1145 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1148 term
.c
.attr
.bg
= DefaultBG
;
1151 if(BETWEEN(attr
[i
], 30, 37))
1152 term
.c
.attr
.fg
= attr
[i
] - 30;
1153 else if(BETWEEN(attr
[i
], 40, 47))
1154 term
.c
.attr
.bg
= attr
[i
] - 40;
1155 else if(BETWEEN(attr
[i
], 90, 97))
1156 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1157 else if(BETWEEN(attr
[i
], 100, 107))
1158 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1160 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1167 tsetscroll(int t
, int b
) {
1170 LIMIT(t
, 0, term
.row
-1);
1171 LIMIT(b
, 0, term
.row
-1);
1181 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1184 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1187 for (lim
= args
+ narg
; args
< lim
; ++args
) {
1191 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1193 case 5: /* DECSCNM -- Reverve video */
1195 MODBIT(term
.mode
,set
, MODE_REVERSE
);
1196 if (mode
!= term
.mode
)
1200 MODBIT(term
.mode
, set
, MODE_WRAP
);
1203 MODBIT(term
.mode
, set
, MODE_CRLF
);
1205 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1208 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1210 case 1000: /* 1000,1002: enable xterm mouse report */
1211 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1214 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1216 case 1049: /* = 1047 and 1048 */
1219 if(IS_SET(MODE_ALTSCREEN
))
1220 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1221 if ((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1222 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1229 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1233 "erresc: unknown private set/reset mode %d\n",
1240 MODBIT(term
.mode
, set
, MODE_INSERT
);
1244 "erresc: unknown set/reset mode %d\n",
1256 switch(csiescseq
.mode
) {
1259 fprintf(stderr
, "erresc: unknown csi ");
1263 case '@': /* ICH -- Insert <n> blank char */
1264 DEFAULT(csiescseq
.arg
[0], 1);
1265 tinsertblank(csiescseq
.arg
[0]);
1267 case 'A': /* CUU -- Cursor <n> Up */
1269 DEFAULT(csiescseq
.arg
[0], 1);
1270 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1272 case 'B': /* CUD -- Cursor <n> Down */
1273 DEFAULT(csiescseq
.arg
[0], 1);
1274 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1276 case 'C': /* CUF -- Cursor <n> Forward */
1278 DEFAULT(csiescseq
.arg
[0], 1);
1279 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1281 case 'D': /* CUB -- Cursor <n> Backward */
1282 DEFAULT(csiescseq
.arg
[0], 1);
1283 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1285 case 'E': /* CNL -- Cursor <n> Down and first col */
1286 DEFAULT(csiescseq
.arg
[0], 1);
1287 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1289 case 'F': /* CPL -- Cursor <n> Up and first col */
1290 DEFAULT(csiescseq
.arg
[0], 1);
1291 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1293 case 'g': /* TBC -- Tabulation clear */
1294 switch (csiescseq
.arg
[0]) {
1295 case 0: /* clear current tab stop */
1296 term
.tabs
[term
.c
.x
] = 0;
1298 case 3: /* clear all the tabs */
1299 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1305 case 'G': /* CHA -- Move to <col> */
1306 case '`': /* XXX: HPA -- same? */
1307 DEFAULT(csiescseq
.arg
[0], 1);
1308 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1310 case 'H': /* CUP -- Move to <row> <col> */
1311 case 'f': /* XXX: HVP -- same? */
1312 DEFAULT(csiescseq
.arg
[0], 1);
1313 DEFAULT(csiescseq
.arg
[1], 1);
1314 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1316 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1317 DEFAULT(csiescseq
.arg
[0], 1);
1318 while (csiescseq
.arg
[0]--)
1321 case 'J': /* ED -- Clear screen */
1323 switch(csiescseq
.arg
[0]) {
1325 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1326 if(term
.c
.y
< term
.row
-1)
1327 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1331 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1332 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1335 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1341 case 'K': /* EL -- Clear line */
1342 switch(csiescseq
.arg
[0]) {
1344 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1347 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1350 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1354 case 'S': /* SU -- Scroll <n> line up */
1355 DEFAULT(csiescseq
.arg
[0], 1);
1356 tscrollup(term
.top
, csiescseq
.arg
[0]);
1358 case 'T': /* SD -- Scroll <n> line down */
1359 DEFAULT(csiescseq
.arg
[0], 1);
1360 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1362 case 'L': /* IL -- Insert <n> blank lines */
1363 DEFAULT(csiescseq
.arg
[0], 1);
1364 tinsertblankline(csiescseq
.arg
[0]);
1366 case 'l': /* RM -- Reset Mode */
1367 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1369 case 'M': /* DL -- Delete <n> lines */
1370 DEFAULT(csiescseq
.arg
[0], 1);
1371 tdeleteline(csiescseq
.arg
[0]);
1373 case 'X': /* ECH -- Erase <n> char */
1374 DEFAULT(csiescseq
.arg
[0], 1);
1375 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1377 case 'P': /* DCH -- Delete <n> char */
1378 DEFAULT(csiescseq
.arg
[0], 1);
1379 tdeletechar(csiescseq
.arg
[0]);
1381 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1382 DEFAULT(csiescseq
.arg
[0], 1);
1383 while (csiescseq
.arg
[0]--)
1386 case 'd': /* VPA -- Move to <row> */
1387 DEFAULT(csiescseq
.arg
[0], 1);
1388 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1390 case 'h': /* SM -- Set terminal mode */
1391 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1393 case 'm': /* SGR -- Terminal attribute (color) */
1394 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1396 case 'r': /* DECSTBM -- Set Scrolling Region */
1400 DEFAULT(csiescseq
.arg
[0], 1);
1401 DEFAULT(csiescseq
.arg
[1], term
.row
);
1402 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1406 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1407 tcursor(CURSOR_SAVE
);
1409 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1410 tcursor(CURSOR_LOAD
);
1419 for(i
= 0; i
< csiescseq
.len
; i
++) {
1420 uint c
= csiescseq
.buf
[i
] & 0xff;
1421 if(isprint(c
)) putchar(c
);
1422 else if(c
== '\n') printf("(\\n)");
1423 else if(c
== '\r') printf("(\\r)");
1424 else if(c
== 0x1b) printf("(\\e)");
1425 else printf("(%02x)", c
);
1432 memset(&csiescseq
, 0, sizeof(csiescseq
));
1440 * TODO: make this being useful in case of color palette change.
1446 switch(strescseq
.type
) {
1447 case ']': /* OSC -- Operating System Command */
1453 * TODO: Handle special chars in string, like umlauts.
1456 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1460 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1462 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1465 fprintf(stderr
, "erresc: unknown str ");
1470 case 'P': /* DSC -- Device Control String */
1471 case '_': /* APC -- Application Program Command */
1472 case '^': /* PM -- Privacy Message */
1474 fprintf(stderr
, "erresc: unknown str ");
1484 * TODO: Implement parsing like for CSI when required.
1485 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1493 printf("ESC%c", strescseq
.type
);
1494 for(i
= 0; i
< strescseq
.len
; i
++) {
1495 uint c
= strescseq
.buf
[i
] & 0xff;
1496 if(isprint(c
)) putchar(c
);
1497 else if(c
== '\n') printf("(\\n)");
1498 else if(c
== '\r') printf("(\\r)");
1499 else if(c
== 0x1b) printf("(\\e)");
1500 else printf("(%02x)", c
);
1507 memset(&strescseq
, 0, sizeof(strescseq
));
1511 tputtab(bool forward
) {
1512 unsigned x
= term
.c
.x
;
1517 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1522 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1525 tmoveto(x
, term
.c
.y
);
1531 if(term
.esc
& ESC_START
) {
1532 if(term
.esc
& ESC_CSI
) {
1533 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1534 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1536 csiparse(), csihandle();
1538 } else if(term
.esc
& ESC_STR
) {
1541 term
.esc
= ESC_START
| ESC_STR_END
;
1543 case '\a': /* backwards compatibility to xterm */
1548 strescseq
.buf
[strescseq
.len
++] = ascii
;
1549 if (strescseq
.len
+1 >= STR_BUF_SIZ
) {
1554 } else if(term
.esc
& ESC_STR_END
) {
1558 } else if(term
.esc
& ESC_ALTCHARSET
) {
1560 case '0': /* Line drawing crap */
1561 term
.c
.attr
.mode
|= ATTR_GFX
;
1563 case 'B': /* Back to regular text */
1564 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1567 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1573 term
.esc
|= ESC_CSI
;
1575 case 'P': /* DCS -- Device Control String */
1576 case '_': /* APC -- Application Program Command */
1577 case '^': /* PM -- Privacy Message */
1578 case ']': /* OSC -- Operating System Command */
1580 strescseq
.type
= ascii
;
1581 term
.esc
|= ESC_STR
;
1584 term
.esc
|= ESC_ALTCHARSET
;
1586 case 'D': /* IND -- Linefeed */
1587 if(term
.c
.y
== term
.bot
)
1588 tscrollup(term
.top
, 1);
1590 tmoveto(term
.c
.x
, term
.c
.y
+1);
1593 case 'E': /* NEL -- Next line */
1594 tnewline(1); /* always go to first col */
1597 case 'H': /* HTS -- Horizontal tab stop */
1598 term
.tabs
[term
.c
.x
] = 1;
1601 case 'M': /* RI -- Reverse index */
1602 if(term
.c
.y
== term
.top
)
1603 tscrolldown(term
.top
, 1);
1605 tmoveto(term
.c
.x
, term
.c
.y
-1);
1608 case 'c': /* RIS -- Reset to inital state */
1612 case '=': /* DECPAM -- Application keypad */
1613 term
.mode
|= MODE_APPKEYPAD
;
1616 case '>': /* DECPNM -- Normal keypad */
1617 term
.mode
&= ~MODE_APPKEYPAD
;
1620 case '7': /* DECSC -- Save Cursor */
1621 tcursor(CURSOR_SAVE
);
1624 case '8': /* DECRC -- Restore Cursor */
1625 tcursor(CURSOR_LOAD
);
1628 case '\\': /* ST -- Stop */
1632 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1633 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1638 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1645 tmoveto(term
.c
.x
-1, term
.c
.y
);
1648 tmoveto(0, term
.c
.y
);
1653 /* go to first col if the mode is set */
1654 tnewline(IS_SET(MODE_CRLF
));
1657 if(!(xw
.state
& WIN_FOCUSED
))
1662 term
.esc
= ESC_START
;
1665 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1666 tnewline(1); /* always go to first col */
1668 if(term
.c
.x
+1 < term
.col
)
1669 tmoveto(term
.c
.x
+1, term
.c
.y
);
1671 term
.c
.state
|= CURSOR_WRAPNEXT
;
1677 tresize(int col
, int row
) {
1679 int minrow
= MIN(row
, term
.row
);
1680 int mincol
= MIN(col
, term
.col
);
1681 int slide
= term
.c
.y
- row
+ 1;
1683 if(col
< 1 || row
< 1)
1686 /* free unneeded rows */
1689 /* slide screen to keep cursor where we expect it -
1690 * tscrollup would work here, but we can optimize to
1691 * memmove because we're freeing the earlier lines */
1692 for(/* i = 0 */; i
< slide
; i
++) {
1696 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1697 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1699 for(i
+= row
; i
< term
.row
; i
++) {
1704 /* resize to new height */
1705 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1706 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1707 term
.dirty
= realloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1708 term
.tabs
= realloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1710 /* resize each row to new width, zero-pad if needed */
1711 for(i
= 0; i
< minrow
; i
++) {
1713 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1714 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1715 for(x
= mincol
; x
< col
; x
++) {
1716 term
.line
[i
][x
].state
= 0;
1717 term
.alt
[i
][x
].state
= 0;
1721 /* allocate any new rows */
1722 for(/* i == minrow */; i
< row
; i
++) {
1724 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1725 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1727 if (col
> term
.col
) {
1728 bool *bp
= term
.tabs
+ term
.col
;
1730 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1731 while (--bp
> term
.tabs
&& !*bp
)
1733 for (bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1736 /* update terminal size */
1737 term
.col
= col
, term
.row
= row
;
1738 /* make use of the LIMIT in tmoveto */
1739 tmoveto(term
.c
.x
, term
.c
.y
);
1740 /* reset scrolling region */
1741 tsetscroll(0, row
-1);
1747 xresize(int col
, int row
) {
1748 xw
.w
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1749 xw
.h
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1756 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1758 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1759 for(i
= 0; i
< LEN(colorname
); i
++) {
1762 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1764 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1766 dc
.col
[i
] = color
.pixel
;
1769 /* load colors [16-255] ; same colors as xterm */
1770 for(i
= 16, r
= 0; r
< 6; r
++)
1771 for(g
= 0; g
< 6; g
++)
1772 for(b
= 0; b
< 6; b
++) {
1773 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1774 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1775 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1776 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1778 fprintf(stderr
, "Could not allocate color %d\n", i
);
1780 dc
.col
[i
] = color
.pixel
;
1784 for(r
= 0; r
< 24; r
++, i
++) {
1785 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1786 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1788 fprintf(stderr
, "Could not allocate color %d\n", i
);
1790 dc
.col
[i
] = color
.pixel
;
1795 xclear(int x1
, int y1
, int x2
, int y2
) {
1796 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1797 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1798 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1799 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1804 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1805 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1807 .flags
= PSize
| PResizeInc
| PBaseSize
,
1810 .height_inc
= xw
.ch
,
1812 .base_height
= 2*BORDER
,
1813 .base_width
= 2*BORDER
,
1815 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1819 xinitfont(char *fontstr
) {
1821 char *def
, **missing
;
1825 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1828 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1829 XFreeStringList(missing
);
1835 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1836 XFontStruct
**xfonts
;
1840 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1841 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1842 for(i
= 0; i
< n
; i
++) {
1843 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1844 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1845 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1846 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1852 initfonts(char *fontstr
, char *bfontstr
) {
1853 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1854 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1855 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1856 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1857 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1858 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1859 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1864 XSetWindowAttributes attrs
;
1868 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1869 die("Can't open display\n");
1870 xw
.scr
= XDefaultScreen(xw
.dpy
);
1873 initfonts(FONT
, BOLDFONT
);
1875 /* XXX: Assuming same size for bold font */
1876 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1877 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1880 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1883 /* window - default size */
1884 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
1885 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
1887 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1888 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1889 attrs
.bit_gravity
= NorthWestGravity
;
1890 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1891 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1892 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1893 | EnterWindowMask
| LeaveWindowMask
;
1894 attrs
.colormap
= xw
.cmap
;
1896 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1897 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1898 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1899 XDefaultVisual(xw
.dpy
, xw
.scr
),
1900 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1903 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
1907 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1908 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1909 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1910 XNFocusWindow
, xw
.win
, NULL
);
1912 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1914 /* white cursor, black outline */
1915 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1916 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1917 XRecolorCursor(xw
.dpy
, cursor
,
1918 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1919 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1921 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1923 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1924 XMapWindow(xw
.dpy
, xw
.win
);
1930 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1931 int fg
= base
.fg
, bg
= base
.bg
, temp
;
1932 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1933 XFontSet fontset
= dc
.font
.set
;
1936 /* only switch default fg/bg if term is in RV mode */
1937 if(IS_SET(MODE_REVERSE
)) {
1944 if(base
.mode
& ATTR_REVERSE
)
1945 temp
= fg
, fg
= bg
, bg
= temp
;
1947 if(base
.mode
& ATTR_BOLD
) {
1949 fontset
= dc
.bfont
.set
;
1952 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
1953 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
1955 if(base
.mode
& ATTR_GFX
) {
1956 for(i
= 0; i
< bytelen
; i
++) {
1957 char c
= gfx
[(uint
)s
[i
] % 256];
1960 else if(s
[i
] > 0x5f)
1965 XmbDrawImageString(xw
.dpy
, xw
.buf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
1967 if(base
.mode
& ATTR_UNDERLINE
)
1968 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1971 /* copy buffer pixmap to screen pixmap */
1974 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
1975 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
1981 static int oldx
= 0;
1982 static int oldy
= 0;
1984 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1986 LIMIT(oldx
, 0, term
.col
-1);
1987 LIMIT(oldy
, 0, term
.row
-1);
1989 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1990 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1992 /* remove the old cursor */
1993 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1994 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1995 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1997 xclear(oldx
, oldy
, oldx
, oldy
);
1999 xcopy(oldx
, oldy
, 1, 1);
2001 /* draw the new one */
2002 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2003 if(!(xw
.state
& WIN_FOCUSED
))
2006 if(IS_SET(MODE_REVERSE
))
2007 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2010 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2011 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2014 xcopy(term
.c
.x
, term
.c
.y
, 1, 1);
2019 drawregion(0, 0, term
.col
, term
.row
);
2021 gettimeofday(&xw
.lastdraw
, NULL
);
2025 drawregion(int x1
, int y1
, int x2
, int y2
) {
2026 int ic
, ib
, x
, y
, ox
, sl
;
2028 char buf
[DRAW_BUF_SIZ
];
2030 if(!(xw
.state
& WIN_VISIBLE
))
2033 for(y
= y1
; y
< y2
; y
++) {
2036 xclear(0, y
, term
.col
, y
);
2038 base
= term
.line
[y
][0];
2040 for(x
= x1
; x
< x2
; x
++) {
2041 new = term
.line
[y
][x
];
2042 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
2043 new.mode
^= ATTR_REVERSE
;
2044 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2045 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2046 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2049 if(new.state
& GLYPH_SET
) {
2054 sl
= utf8size(new.c
);
2055 memcpy(buf
+ib
, new.c
, sl
);
2061 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2067 expose(XEvent
*ev
) {
2068 XExposeEvent
*e
= &ev
->xexpose
;
2069 if(xw
.state
& WIN_REDRAW
) {
2071 xw
.state
&= ~WIN_REDRAW
;
2077 visibility(XEvent
*ev
) {
2078 XVisibilityEvent
*e
= &ev
->xvisibility
;
2079 if(e
->state
== VisibilityFullyObscured
)
2080 xw
.state
&= ~WIN_VISIBLE
;
2081 else if(!(xw
.state
& WIN_VISIBLE
))
2082 /* need a full redraw for next Expose, not just a buf copy */
2083 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2088 xw
.state
&= ~WIN_VISIBLE
;
2092 xseturgency(int add
) {
2093 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2094 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2095 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2101 if(ev
->type
== FocusIn
) {
2102 xw
.state
|= WIN_FOCUSED
;
2105 xw
.state
&= ~WIN_FOCUSED
;
2110 kmap(KeySym k
, uint state
) {
2113 for(i
= 0; i
< LEN(key
); i
++) {
2114 uint mask
= key
[i
].mask
;
2115 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2116 return (char*)key
[i
].s
;
2122 kpress(XEvent
*ev
) {
2123 XKeyEvent
*e
= &ev
->xkey
;
2132 meta
= e
->state
& Mod1Mask
;
2133 shift
= e
->state
& ShiftMask
;
2134 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2136 /* 1. custom keys from config.h */
2137 if((customkey
= kmap(ksym
, e
->state
)))
2138 ttywrite(customkey
, strlen(customkey
));
2139 /* 2. hardcoded (overrides X lookup) */
2146 /* XXX: shift up/down doesn't work */
2147 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2155 if(IS_SET(MODE_CRLF
))
2156 ttywrite("\r\n", 2);
2163 if(meta
&& len
== 1)
2164 ttywrite("\033", 1);
2172 cmessage(XEvent
*e
) {
2174 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2175 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2176 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2177 xw
.state
|= WIN_FOCUSED
;
2179 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2180 xw
.state
&= ~WIN_FOCUSED
;
2190 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2193 xw
.w
= e
->xconfigure
.width
;
2194 xw
.h
= e
->xconfigure
.height
;
2195 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2196 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2197 if(col
== term
.col
&& row
== term
.row
)
2199 if(tresize(col
, row
))
2201 ttyresize(col
, row
);
2206 last_draw_too_old(void) {
2208 gettimeofday(&now
, NULL
);
2209 return TIMEDIFF(now
, xw
.lastdraw
) >= DRAW_TIMEOUT
/1000;
2216 int xfd
= XConnectionNumber(xw
.dpy
);
2217 struct timeval timeout
= {0};
2218 bool stuff_to_print
= 0;
2222 FD_SET(cmdfd
, &rfd
);
2225 timeout
.tv_usec
= SELECT_TIMEOUT
;
2226 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
2229 die("select failed: %s\n", SERRNO
);
2231 if(FD_ISSET(cmdfd
, &rfd
)) {
2236 if(stuff_to_print
&& last_draw_too_old()) {
2241 while(XPending(xw
.dpy
)) {
2242 XNextEvent(xw
.dpy
, &ev
);
2243 if(XFilterEvent(&ev
, xw
.win
))
2245 if(handler
[ev
.type
])
2246 (handler
[ev
.type
])(&ev
);
2252 main(int argc
, char *argv
[]) {
2255 for(i
= 1; i
< argc
; i
++) {
2256 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2258 if(++i
< argc
) opt_title
= argv
[i
];
2261 if(++i
< argc
) opt_class
= argv
[i
];
2264 if(++i
< argc
) opt_embed
= argv
[i
];
2267 /* eat every remaining arguments */
2268 if(++i
< argc
) opt_cmd
= &argv
[i
];
2277 setlocale(LC_CTYPE
, "");