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 tfulldirt(void);
278 static void ttynew(void);
279 static void ttyread(void);
280 static void ttyresize(int, int);
281 static void ttywrite(const char *, size_t);
283 static void xdraws(char *, Glyph
, int, int, int, int);
284 static void xhints(void);
285 static void xclear(int, int, int, int);
287 static void xdrawcursor(void);
288 static void xinit(void);
289 static void xloadcols(void);
290 static void xseturgency(int);
291 static void xsetsel(char*);
292 static void xresize(int, int);
294 static void expose(XEvent
*);
295 static void visibility(XEvent
*);
296 static void unmap(XEvent
*);
297 static char* kmap(KeySym
, uint
);
298 static void kpress(XEvent
*);
299 static void cmessage(XEvent
*);
300 static void resize(XEvent
*);
301 static void focus(XEvent
*);
302 static void brelease(XEvent
*);
303 static void bpress(XEvent
*);
304 static void bmotion(XEvent
*);
305 static void selnotify(XEvent
*);
306 static void selrequest(XEvent
*);
308 static void selinit(void);
309 static inline bool selected(int, int);
310 static void selcopy(void);
311 static void selpaste();
312 static void selscroll(int, int);
314 static int utf8decode(char *, long *);
315 static int utf8encode(long *, char *);
316 static int utf8size(char *);
317 static int isfullutf8(char *, int);
319 static void (*handler
[LASTEvent
])(XEvent
*) = {
321 [ClientMessage
] = cmessage
,
322 [ConfigureNotify
] = resize
,
323 [VisibilityNotify
] = visibility
,
324 [UnmapNotify
] = unmap
,
328 [MotionNotify
] = bmotion
,
329 [ButtonPress
] = bpress
,
330 [ButtonRelease
] = brelease
,
331 [SelectionNotify
] = selnotify
,
332 [SelectionRequest
] = selrequest
,
339 static CSIEscape csiescseq
;
340 static STREscape strescseq
;
343 static Selection sel
;
344 static char **opt_cmd
= NULL
;
345 static char *opt_title
= NULL
;
346 static char *opt_embed
= NULL
;
347 static char *opt_class
= NULL
;
350 utf8decode(char *s
, long *u
) {
356 if(~c
& B7
) { /* 0xxxxxxx */
359 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
360 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
362 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
363 *u
= c
&(B3
|B2
|B1
|B0
);
365 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
370 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
372 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
375 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
377 if((n
== 1 && *u
< 0x80) ||
378 (n
== 2 && *u
< 0x800) ||
379 (n
== 3 && *u
< 0x10000) ||
380 (*u
>= 0xD800 && *u
<= 0xDFFF))
389 utf8encode(long *u
, char *s
) {
397 *sp
= uc
; /* 0xxxxxxx */
399 } else if(*u
< 0x800) {
400 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
402 } else if(uc
< 0x10000) {
403 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
405 } else if(uc
<= 0x10FFFF) {
406 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
411 for(i
=n
,++sp
; i
>0; --i
,++sp
)
412 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
422 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
423 UTF-8 otherwise return 0 */
425 isfullutf8(char *s
, int b
) {
433 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
435 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
437 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
439 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
441 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
442 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
454 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
456 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
464 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
465 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
469 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
470 if(sel
.xtarget
== None
)
471 sel
.xtarget
= XA_STRING
;
475 selected(int x
, int y
) {
476 if(sel
.ey
== y
&& sel
.by
== y
) {
477 int bx
= MIN(sel
.bx
, sel
.ex
);
478 int ex
= MAX(sel
.bx
, sel
.ex
);
479 return BETWEEN(x
, bx
, ex
);
481 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
482 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
486 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
488 *b
= e
->xbutton
.button
;
490 *x
= X2COL(e
->xbutton
.x
);
491 *y
= Y2ROW(e
->xbutton
.y
);
492 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
493 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
494 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
495 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
499 mousereport(XEvent
*e
) {
500 int x
= X2COL(e
->xbutton
.x
);
501 int y
= Y2ROW(e
->xbutton
.y
);
502 int button
= e
->xbutton
.button
;
503 int state
= e
->xbutton
.state
;
504 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
505 static int ob
, ox
, oy
;
508 if(e
->xbutton
.type
== MotionNotify
) {
509 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
513 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
519 if(e
->xbutton
.type
== ButtonPress
) {
525 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
526 + (state
& Mod4Mask
? 8 : 0)
527 + (state
& ControlMask
? 16 : 0);
529 ttywrite(buf
, sizeof(buf
));
534 if(IS_SET(MODE_MOUSE
))
536 else if(e
->xbutton
.button
== Button1
) {
538 tsetdirt(sel
.b
.y
, sel
.e
.y
);
540 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
541 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
548 int x
, y
, bufsize
, is_selected
= 0;
554 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
555 ptr
= str
= malloc(bufsize
);
557 /* append every set & selected glyph to the selection */
558 for(y
= 0; y
< term
.row
; y
++) {
559 for(x
= 0; x
< term
.col
; x
++) {
560 is_selected
= selected(x
, y
);
561 if((term
.line
[y
][x
].state
& GLYPH_SET
) && is_selected
) {
562 int size
= utf8size(term
.line
[y
][x
].c
);
563 memcpy(ptr
, term
.line
[y
][x
].c
, size
);
568 /* \n at the end of every selected line except for the last one */
569 if(is_selected
&& y
< sel
.e
.y
)
578 selnotify(XEvent
*e
) {
579 ulong nitems
, ofs
, rem
;
586 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
587 False
, AnyPropertyType
, &type
, &format
,
588 &nitems
, &rem
, &data
)) {
589 fprintf(stderr
, "Clipboard allocation failed\n");
592 ttywrite((const char *) data
, nitems
* format
/ 8);
594 /* number of 32-bit chunks returned */
595 ofs
+= nitems
* format
/ 32;
601 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
605 selrequest(XEvent
*e
) {
606 XSelectionRequestEvent
*xsre
;
610 xsre
= (XSelectionRequestEvent
*) e
;
611 xev
.type
= SelectionNotify
;
612 xev
.requestor
= xsre
->requestor
;
613 xev
.selection
= xsre
->selection
;
614 xev
.target
= xsre
->target
;
615 xev
.time
= xsre
->time
;
619 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
620 if(xsre
->target
== xa_targets
) {
621 /* respond with the supported type */
622 Atom string
= sel
.xtarget
;
623 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
624 XA_ATOM
, 32, PropModeReplace
,
625 (uchar
*) &string
, 1);
626 xev
.property
= xsre
->property
;
627 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
628 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
629 xsre
->target
, 8, PropModeReplace
,
630 (uchar
*) sel
.clip
, strlen(sel
.clip
));
631 xev
.property
= xsre
->property
;
634 /* all done, send a notification to the listener */
635 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
636 fprintf(stderr
, "Error sending SelectionNotify event\n");
641 /* register the selection for both the clipboard and the primary */
647 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
649 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
650 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
656 brelease(XEvent
*e
) {
657 if(IS_SET(MODE_MOUSE
)) {
661 if(e
->xbutton
.button
== Button2
)
663 else if(e
->xbutton
.button
== Button1
) {
665 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
666 term
.dirty
[sel
.ey
] = 1;
667 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
670 gettimeofday(&now
, NULL
);
672 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
673 /* triple click on the line */
674 sel
.b
.x
= sel
.bx
= 0;
675 sel
.e
.x
= sel
.ex
= term
.col
;
676 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
678 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
679 /* double click to select word */
681 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
682 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
684 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
685 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
687 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
693 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
694 gettimeofday(&sel
.tclick1
, NULL
);
700 if(IS_SET(MODE_MOUSE
)) {
705 int oldey
= sel
.ey
, oldex
= sel
.ex
;
706 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
708 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
709 int starty
= MIN(oldey
, sel
.ey
);
710 int endy
= MAX(oldey
, sel
.ey
);
711 tsetdirt(starty
, endy
);
718 die(const char *errstr
, ...) {
721 va_start(ap
, errstr
);
722 vfprintf(stderr
, errstr
, ap
);
730 char *envshell
= getenv("SHELL");
732 DEFAULT(envshell
, SHELL
);
733 putenv("TERM="TNAME
);
734 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
735 execvp(args
[0], args
);
742 if(waitpid(pid
, &stat
, 0) < 0)
743 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
745 exit(WEXITSTATUS(stat
));
754 /* seems to work fine on linux, openbsd and freebsd */
755 struct winsize w
= {term
.row
, term
.col
, 0, 0};
756 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
757 die("openpty failed: %s\n", SERRNO
);
759 switch(pid
= fork()) {
761 die("fork failed\n");
764 setsid(); /* create a new process group */
765 dup2(s
, STDIN_FILENO
);
766 dup2(s
, STDOUT_FILENO
);
767 dup2(s
, STDERR_FILENO
);
768 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
769 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
777 signal(SIGCHLD
, sigchld
);
784 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
786 fprintf(stderr
, "\n");
791 static char buf
[BUFSIZ
];
792 static int buflen
= 0;
795 int charsize
; /* size of utf8 char in bytes */
799 /* append read bytes to unprocessed bytes */
800 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
801 die("Couldn't read from shell: %s\n", SERRNO
);
803 /* process every complete utf8 char */
806 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
807 charsize
= utf8decode(ptr
, &utf8c
);
808 utf8encode(&utf8c
, s
);
814 /* keep any uncomplete utf8 char for the next call */
815 memmove(buf
, ptr
, buflen
);
819 ttywrite(const char *s
, size_t n
) {
820 if(write(cmdfd
, s
, n
) == -1)
821 die("write error on tty: %s\n", SERRNO
);
825 ttyresize(int x
, int y
) {
830 w
.ws_xpixel
= w
.ws_ypixel
= 0;
831 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
832 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
836 tsetdirt(int top
, int bot
)
840 LIMIT(top
, 0, term
.row
-1);
841 LIMIT(bot
, 0, term
.row
-1);
843 for(i
= top
; i
<= bot
; i
++)
850 tsetdirt(0, term
.row
-1);
857 if(mode
== CURSOR_SAVE
)
859 else if(mode
== CURSOR_LOAD
)
860 term
.c
= c
, tmoveto(c
.x
, c
.y
);
870 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
872 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
873 for (i
= TAB
; i
< term
.col
; i
+= TAB
)
875 term
.top
= 0, term
.bot
= term
.row
- 1;
876 term
.mode
= MODE_WRAP
;
877 tclearregion(0, 0, term
.col
-1, term
.row
-1);
881 tnew(int col
, int row
) {
882 /* set screen size */
883 term
.row
= row
, term
.col
= col
;
884 term
.line
= malloc(term
.row
* sizeof(Line
));
885 term
.alt
= malloc(term
.row
* sizeof(Line
));
886 term
.dirty
= malloc(term
.row
* sizeof(*term
.dirty
));
887 term
.tabs
= malloc(term
.col
* sizeof(*term
.tabs
));
889 for(row
= 0; row
< term
.row
; row
++) {
890 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
891 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
894 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
901 Line
* tmp
= term
.line
;
902 term
.line
= term
.alt
;
904 term
.mode
^= MODE_ALTSCREEN
;
909 tscrolldown(int orig
, int n
) {
913 LIMIT(n
, 0, term
.bot
-orig
+1);
915 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
917 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
919 term
.line
[i
] = term
.line
[i
-n
];
920 term
.line
[i
-n
] = temp
;
930 tscrollup(int orig
, int n
) {
933 LIMIT(n
, 0, term
.bot
-orig
+1);
935 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
937 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
939 term
.line
[i
] = term
.line
[i
+n
];
940 term
.line
[i
+n
] = temp
;
950 selscroll(int orig
, int n
) {
954 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
955 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
959 if(sel
.by
< term
.top
) {
963 if(sel
.ey
> term
.bot
) {
967 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
968 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
973 tnewline(int first_col
) {
976 tscrollup(term
.top
, 1);
979 tmoveto(first_col
? 0 : term
.c
.x
, y
);
985 char *p
= csiescseq
.buf
;
989 csiescseq
.priv
= 1, p
++;
991 while(p
< csiescseq
.buf
+csiescseq
.len
) {
993 csiescseq
.arg
[csiescseq
.narg
] *= 10;
994 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
996 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
997 csiescseq
.narg
++, p
++;
1007 tmoveto(int x
, int y
) {
1008 LIMIT(x
, 0, term
.col
-1);
1009 LIMIT(y
, 0, term
.row
-1);
1010 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1017 term
.dirty
[term
.c
.y
] = 1;
1018 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1019 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1020 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1024 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1028 temp
= x1
, x1
= x2
, x2
= temp
;
1030 temp
= y1
, y1
= y2
, y2
= temp
;
1032 LIMIT(x1
, 0, term
.col
-1);
1033 LIMIT(x2
, 0, term
.col
-1);
1034 LIMIT(y1
, 0, term
.row
-1);
1035 LIMIT(y2
, 0, term
.row
-1);
1037 for(y
= y1
; y
<= y2
; y
++) {
1039 for(x
= x1
; x
<= x2
; x
++)
1040 term
.line
[y
][x
].state
= 0;
1045 tdeletechar(int n
) {
1046 int src
= term
.c
.x
+ n
;
1048 int size
= term
.col
- src
;
1050 term
.dirty
[term
.c
.y
] = 1;
1052 if(src
>= term
.col
) {
1053 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1056 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1057 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1061 tinsertblank(int n
) {
1064 int size
= term
.col
- dst
;
1066 term
.dirty
[term
.c
.y
] = 1;
1068 if(dst
>= term
.col
) {
1069 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1072 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1073 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1077 tinsertblankline(int n
) {
1078 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1081 tscrolldown(term
.c
.y
, n
);
1085 tdeleteline(int n
) {
1086 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1089 tscrollup(term
.c
.y
, n
);
1093 tsetattr(int *attr
, int l
) {
1096 for(i
= 0; i
< l
; i
++) {
1099 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
1100 term
.c
.attr
.fg
= DefaultFG
;
1101 term
.c
.attr
.bg
= DefaultBG
;
1104 term
.c
.attr
.mode
|= ATTR_BOLD
;
1107 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1110 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1113 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1116 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1119 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1122 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1124 if(BETWEEN(attr
[i
], 0, 255))
1125 term
.c
.attr
.fg
= attr
[i
];
1127 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1130 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1133 term
.c
.attr
.fg
= DefaultFG
;
1136 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1138 if(BETWEEN(attr
[i
], 0, 255))
1139 term
.c
.attr
.bg
= attr
[i
];
1141 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1144 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1147 term
.c
.attr
.bg
= DefaultBG
;
1150 if(BETWEEN(attr
[i
], 30, 37))
1151 term
.c
.attr
.fg
= attr
[i
] - 30;
1152 else if(BETWEEN(attr
[i
], 40, 47))
1153 term
.c
.attr
.bg
= attr
[i
] - 40;
1154 else if(BETWEEN(attr
[i
], 90, 97))
1155 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1156 else if(BETWEEN(attr
[i
], 100, 107))
1157 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1159 fprintf(stderr
, "erresc: 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);
1183 switch(csiescseq
.mode
) {
1186 fprintf(stderr
, "erresc: unknown csi ");
1190 case '@': /* ICH -- Insert <n> blank char */
1191 DEFAULT(csiescseq
.arg
[0], 1);
1192 tinsertblank(csiescseq
.arg
[0]);
1194 case 'A': /* CUU -- Cursor <n> Up */
1196 DEFAULT(csiescseq
.arg
[0], 1);
1197 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1199 case 'B': /* CUD -- Cursor <n> Down */
1200 DEFAULT(csiescseq
.arg
[0], 1);
1201 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1203 case 'C': /* CUF -- Cursor <n> Forward */
1205 DEFAULT(csiescseq
.arg
[0], 1);
1206 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1208 case 'D': /* CUB -- Cursor <n> Backward */
1209 DEFAULT(csiescseq
.arg
[0], 1);
1210 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1212 case 'E': /* CNL -- Cursor <n> Down and first col */
1213 DEFAULT(csiescseq
.arg
[0], 1);
1214 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1216 case 'F': /* CPL -- Cursor <n> Up and first col */
1217 DEFAULT(csiescseq
.arg
[0], 1);
1218 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1220 case 'g': /* TBC -- Tabulation clear */
1221 switch (csiescseq
.arg
[0]) {
1222 case 0: /* clear current tab stop */
1223 term
.tabs
[term
.c
.x
] = 0;
1225 case 3: /* clear all the tabs */
1226 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1232 case 'G': /* CHA -- Move to <col> */
1233 case '`': /* XXX: HPA -- same? */
1234 DEFAULT(csiescseq
.arg
[0], 1);
1235 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1237 case 'H': /* CUP -- Move to <row> <col> */
1238 case 'f': /* XXX: HVP -- same? */
1239 DEFAULT(csiescseq
.arg
[0], 1);
1240 DEFAULT(csiescseq
.arg
[1], 1);
1241 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1243 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1244 DEFAULT(csiescseq
.arg
[0], 1);
1245 while (csiescseq
.arg
[0]--)
1248 case 'J': /* ED -- Clear screen */
1250 switch(csiescseq
.arg
[0]) {
1252 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1253 if(term
.c
.y
< term
.row
-1)
1254 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1258 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1259 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1262 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1268 case 'K': /* EL -- Clear line */
1269 switch(csiescseq
.arg
[0]) {
1271 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1274 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1277 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1281 case 'S': /* SU -- Scroll <n> line up */
1282 DEFAULT(csiescseq
.arg
[0], 1);
1283 tscrollup(term
.top
, csiescseq
.arg
[0]);
1285 case 'T': /* SD -- Scroll <n> line down */
1286 DEFAULT(csiescseq
.arg
[0], 1);
1287 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1289 case 'L': /* IL -- Insert <n> blank lines */
1290 DEFAULT(csiescseq
.arg
[0], 1);
1291 tinsertblankline(csiescseq
.arg
[0]);
1293 case 'l': /* RM -- Reset Mode */
1294 if(csiescseq
.priv
) {
1295 switch(csiescseq
.arg
[0]) {
1297 term
.mode
&= ~MODE_APPKEYPAD
;
1299 case 5: /* DECSCNM -- Remove reverse video */
1300 if(IS_SET(MODE_REVERSE
)) {
1301 term
.mode
&= ~MODE_REVERSE
;
1306 term
.mode
&= ~MODE_WRAP
;
1308 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1311 term
.mode
&= ~MODE_CRLF
;
1314 term
.c
.state
|= CURSOR_HIDE
;
1316 case 1000: /* disable X11 xterm mouse reporting */
1317 term
.mode
&= ~MODE_MOUSEBTN
;
1320 term
.mode
&= ~MODE_MOUSEMOTION
;
1322 case 1049: /* = 1047 and 1048 */
1325 if(IS_SET(MODE_ALTSCREEN
)) {
1326 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1329 if(csiescseq
.arg
[0] != 1049)
1332 tcursor(CURSOR_LOAD
);
1338 switch(csiescseq
.arg
[0]) {
1340 term
.mode
&= ~MODE_INSERT
;
1347 case 'M': /* DL -- Delete <n> lines */
1348 DEFAULT(csiescseq
.arg
[0], 1);
1349 tdeleteline(csiescseq
.arg
[0]);
1351 case 'X': /* ECH -- Erase <n> char */
1352 DEFAULT(csiescseq
.arg
[0], 1);
1353 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1355 case 'P': /* DCH -- Delete <n> char */
1356 DEFAULT(csiescseq
.arg
[0], 1);
1357 tdeletechar(csiescseq
.arg
[0]);
1359 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1360 DEFAULT(csiescseq
.arg
[0], 1);
1361 while (csiescseq
.arg
[0]--)
1364 case 'd': /* VPA -- Move to <row> */
1365 DEFAULT(csiescseq
.arg
[0], 1);
1366 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1368 case 'h': /* SM -- Set terminal mode */
1369 if(csiescseq
.priv
) {
1370 switch(csiescseq
.arg
[0]) {
1372 term
.mode
|= MODE_APPKEYPAD
;
1374 case 5: /* DECSCNM -- Reverve video */
1375 if(!IS_SET(MODE_REVERSE
)) {
1376 term
.mode
|= MODE_REVERSE
;
1381 term
.mode
|= MODE_WRAP
;
1384 term
.mode
|= MODE_CRLF
;
1386 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1387 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1388 if(csiescseq
.narg
> 1 && csiescseq
.arg
[1] != 25)
1391 term
.c
.state
&= ~CURSOR_HIDE
;
1393 case 1000: /* 1000,1002: enable xterm mouse report */
1394 term
.mode
|= MODE_MOUSEBTN
;
1397 term
.mode
|= MODE_MOUSEMOTION
;
1399 case 1049: /* = 1047 and 1048 */
1402 if(IS_SET(MODE_ALTSCREEN
))
1403 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1406 if(csiescseq
.arg
[0] != 1049)
1409 tcursor(CURSOR_SAVE
);
1411 default: goto unknown
;
1414 switch(csiescseq
.arg
[0]) {
1416 term
.mode
|= MODE_INSERT
;
1418 default: goto unknown
;
1422 case 'm': /* SGR -- Terminal attribute (color) */
1423 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1425 case 'r': /* DECSTBM -- Set Scrolling Region */
1429 DEFAULT(csiescseq
.arg
[0], 1);
1430 DEFAULT(csiescseq
.arg
[1], term
.row
);
1431 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1435 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1436 tcursor(CURSOR_SAVE
);
1438 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1439 tcursor(CURSOR_LOAD
);
1448 for(i
= 0; i
< csiescseq
.len
; i
++) {
1449 uint c
= csiescseq
.buf
[i
] & 0xff;
1450 if(isprint(c
)) putchar(c
);
1451 else if(c
== '\n') printf("(\\n)");
1452 else if(c
== '\r') printf("(\\r)");
1453 else if(c
== 0x1b) printf("(\\e)");
1454 else printf("(%02x)", c
);
1461 memset(&csiescseq
, 0, sizeof(csiescseq
));
1470 switch(strescseq
.type
) {
1471 case ']': /* OSC -- Operating System Command */
1477 * TODO: Handle special chars in string, like umlauts.
1480 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1484 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1486 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1489 fprintf(stderr
, "erresc: unknown str ");
1494 case 'P': /* DSC -- Device Control String */
1495 case '_': /* APC -- Application Program Command */
1496 case '^': /* PM -- Privacy Message */
1498 fprintf(stderr
, "erresc: unknown str ");
1508 * TODO: Implement parsing like for CSI when required.
1509 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1517 printf("ESC%c", strescseq
.type
);
1518 for(i
= 0; i
< strescseq
.len
; i
++) {
1519 uint c
= strescseq
.buf
[i
] & 0xff;
1520 if(isprint(c
)) putchar(c
);
1521 else if(c
== '\n') printf("(\\n)");
1522 else if(c
== '\r') printf("(\\r)");
1523 else if(c
== 0x1b) printf("(\\e)");
1524 else printf("(%02x)", c
);
1531 memset(&strescseq
, 0, sizeof(strescseq
));
1535 tputtab(bool forward
) {
1536 unsigned x
= term
.c
.x
;
1541 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1546 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1549 tmoveto(x
, term
.c
.y
);
1555 if(term
.esc
& ESC_START
) {
1556 if(term
.esc
& ESC_CSI
) {
1557 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1558 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1560 csiparse(), csihandle();
1562 } else if(term
.esc
& ESC_STR
) {
1565 term
.esc
= ESC_START
| ESC_STR_END
;
1567 case '\a': /* backwards compatibility to xterm */
1572 strescseq
.buf
[strescseq
.len
++] = ascii
;
1573 if (strescseq
.len
+1 >= STR_BUF_SIZ
) {
1578 } else if(term
.esc
& ESC_STR_END
) {
1582 } else if(term
.esc
& ESC_ALTCHARSET
) {
1584 case '0': /* Line drawing crap */
1585 term
.c
.attr
.mode
|= ATTR_GFX
;
1587 case 'B': /* Back to regular text */
1588 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1591 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1597 term
.esc
|= ESC_CSI
;
1599 case 'P': /* DCS -- Device Control String */
1600 case '_': /* APC -- Application Program Command */
1601 case '^': /* PM -- Privacy Message */
1602 case ']': /* OSC -- Operating System Command */
1604 strescseq
.type
= ascii
;
1605 term
.esc
|= ESC_STR
;
1608 term
.esc
|= ESC_ALTCHARSET
;
1610 case 'D': /* IND -- Linefeed */
1611 if(term
.c
.y
== term
.bot
)
1612 tscrollup(term
.top
, 1);
1614 tmoveto(term
.c
.x
, term
.c
.y
+1);
1617 case 'E': /* NEL -- Next line */
1618 tnewline(1); /* always go to first col */
1621 case 'H': /* HTS -- Horizontal tab stop */
1622 term
.tabs
[term
.c
.x
] = 1;
1625 case 'M': /* RI -- Reverse index */
1626 if(term
.c
.y
== term
.top
)
1627 tscrolldown(term
.top
, 1);
1629 tmoveto(term
.c
.x
, term
.c
.y
-1);
1632 case 'c': /* RIS -- Reset to inital state */
1636 case '=': /* DECPAM -- Application keypad */
1637 term
.mode
|= MODE_APPKEYPAD
;
1640 case '>': /* DECPNM -- Normal keypad */
1641 term
.mode
&= ~MODE_APPKEYPAD
;
1644 case '7': /* DECSC -- Save Cursor */
1645 tcursor(CURSOR_SAVE
);
1648 case '8': /* DECRC -- Restore Cursor */
1649 tcursor(CURSOR_LOAD
);
1652 case '\\': /* ST -- Stop */
1656 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1657 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1662 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1669 tmoveto(term
.c
.x
-1, term
.c
.y
);
1672 tmoveto(0, term
.c
.y
);
1677 /* go to first col if the mode is set */
1678 tnewline(IS_SET(MODE_CRLF
));
1681 if(!(xw
.state
& WIN_FOCUSED
))
1686 term
.esc
= ESC_START
;
1689 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1690 tnewline(1); /* always go to first col */
1692 if(term
.c
.x
+1 < term
.col
)
1693 tmoveto(term
.c
.x
+1, term
.c
.y
);
1695 term
.c
.state
|= CURSOR_WRAPNEXT
;
1701 tresize(int col
, int row
) {
1703 int minrow
= MIN(row
, term
.row
);
1704 int mincol
= MIN(col
, term
.col
);
1705 int slide
= term
.c
.y
- row
+ 1;
1707 if(col
< 1 || row
< 1)
1710 /* free unneeded rows */
1713 /* slide screen to keep cursor where we expect it -
1714 * tscrollup would work here, but we can optimize to
1715 * memmove because we're freeing the earlier lines */
1716 for(/* i = 0 */; i
< slide
; i
++) {
1720 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1721 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1723 for(i
+= row
; i
< term
.row
; i
++) {
1728 /* resize to new height */
1729 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1730 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1731 term
.dirty
= realloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1732 term
.tabs
= realloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1734 /* resize each row to new width, zero-pad if needed */
1735 for(i
= 0; i
< minrow
; i
++) {
1737 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1738 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1739 for(x
= mincol
; x
< col
; x
++) {
1740 term
.line
[i
][x
].state
= 0;
1741 term
.alt
[i
][x
].state
= 0;
1745 /* allocate any new rows */
1746 for(/* i == minrow */; i
< row
; i
++) {
1748 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1749 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1751 if (col
> term
.col
) {
1752 bool *bp
= term
.tabs
+ term
.col
;
1754 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1755 while (--bp
> term
.tabs
&& !*bp
)
1757 for (bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1760 /* update terminal size */
1761 term
.col
= col
, term
.row
= row
;
1762 /* make use of the LIMIT in tmoveto */
1763 tmoveto(term
.c
.x
, term
.c
.y
);
1764 /* reset scrolling region */
1765 tsetscroll(0, row
-1);
1771 xresize(int col
, int row
) {
1772 xw
.w
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1773 xw
.h
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1780 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1782 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1783 for(i
= 0; i
< LEN(colorname
); i
++) {
1786 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1788 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1790 dc
.col
[i
] = color
.pixel
;
1793 /* load colors [16-255] ; same colors as xterm */
1794 for(i
= 16, r
= 0; r
< 6; r
++)
1795 for(g
= 0; g
< 6; g
++)
1796 for(b
= 0; b
< 6; b
++) {
1797 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1798 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1799 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1800 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1802 fprintf(stderr
, "Could not allocate color %d\n", i
);
1804 dc
.col
[i
] = color
.pixel
;
1808 for(r
= 0; r
< 24; r
++, i
++) {
1809 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1810 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1812 fprintf(stderr
, "Could not allocate color %d\n", i
);
1814 dc
.col
[i
] = color
.pixel
;
1819 xclear(int x1
, int y1
, int x2
, int y2
) {
1820 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1821 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1822 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1823 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1828 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1829 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1831 .flags
= PSize
| PResizeInc
| PBaseSize
,
1834 .height_inc
= xw
.ch
,
1836 .base_height
= 2*BORDER
,
1837 .base_width
= 2*BORDER
,
1839 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1843 xinitfont(char *fontstr
) {
1845 char *def
, **missing
;
1849 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1852 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1853 XFreeStringList(missing
);
1859 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1860 XFontStruct
**xfonts
;
1864 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1865 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1866 for(i
= 0; i
< n
; i
++) {
1867 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1868 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1869 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1870 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1876 initfonts(char *fontstr
, char *bfontstr
) {
1877 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1878 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1879 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1880 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1881 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1882 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1883 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1888 XSetWindowAttributes attrs
;
1892 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1893 die("Can't open display\n");
1894 xw
.scr
= XDefaultScreen(xw
.dpy
);
1897 initfonts(FONT
, BOLDFONT
);
1899 /* XXX: Assuming same size for bold font */
1900 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1901 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1904 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1907 /* window - default size */
1908 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
1909 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
1911 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1912 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1913 attrs
.bit_gravity
= NorthWestGravity
;
1914 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1915 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1916 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1917 | EnterWindowMask
| LeaveWindowMask
;
1918 attrs
.colormap
= xw
.cmap
;
1920 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1921 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1922 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1923 XDefaultVisual(xw
.dpy
, xw
.scr
),
1924 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1927 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
1931 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1932 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1933 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1934 XNFocusWindow
, xw
.win
, NULL
);
1936 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1938 /* white cursor, black outline */
1939 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1940 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1941 XRecolorCursor(xw
.dpy
, cursor
,
1942 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1943 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1945 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1947 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1948 XMapWindow(xw
.dpy
, xw
.win
);
1954 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1955 int fg
= base
.fg
, bg
= base
.bg
, temp
;
1956 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1957 XFontSet fontset
= dc
.font
.set
;
1960 /* only switch default fg/bg if term is in RV mode */
1961 if(IS_SET(MODE_REVERSE
)) {
1968 if(base
.mode
& ATTR_REVERSE
)
1969 temp
= fg
, fg
= bg
, bg
= temp
;
1971 if(base
.mode
& ATTR_BOLD
) {
1973 fontset
= dc
.bfont
.set
;
1976 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
1977 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
1979 if(base
.mode
& ATTR_GFX
) {
1980 for(i
= 0; i
< bytelen
; i
++) {
1981 char c
= gfx
[(uint
)s
[i
] % 256];
1984 else if(s
[i
] > 0x5f)
1989 XmbDrawImageString(xw
.dpy
, xw
.buf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
1991 if(base
.mode
& ATTR_UNDERLINE
)
1992 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1995 /* copy buffer pixmap to screen pixmap */
1998 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
1999 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2005 static int oldx
= 0;
2006 static int oldy
= 0;
2008 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2010 LIMIT(oldx
, 0, term
.col
-1);
2011 LIMIT(oldy
, 0, term
.row
-1);
2013 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2014 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2016 /* remove the old cursor */
2017 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2018 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2019 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2021 xclear(oldx
, oldy
, oldx
, oldy
);
2023 xcopy(oldx
, oldy
, 1, 1);
2025 /* draw the new one */
2026 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2027 if(!(xw
.state
& WIN_FOCUSED
))
2030 if(IS_SET(MODE_REVERSE
))
2031 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2034 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2035 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2038 xcopy(term
.c
.x
, term
.c
.y
, 1, 1);
2043 drawregion(0, 0, term
.col
, term
.row
);
2045 gettimeofday(&xw
.lastdraw
, NULL
);
2049 drawregion(int x1
, int y1
, int x2
, int y2
) {
2050 int ic
, ib
, x
, y
, ox
, sl
;
2052 char buf
[DRAW_BUF_SIZ
];
2054 if(!(xw
.state
& WIN_VISIBLE
))
2057 for(y
= y1
; y
< y2
; y
++) {
2060 xclear(0, y
, term
.col
, y
);
2062 base
= term
.line
[y
][0];
2064 for(x
= x1
; x
< x2
; x
++) {
2065 new = term
.line
[y
][x
];
2066 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
2067 new.mode
^= ATTR_REVERSE
;
2068 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2069 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2070 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2073 if(new.state
& GLYPH_SET
) {
2078 sl
= utf8size(new.c
);
2079 memcpy(buf
+ib
, new.c
, sl
);
2085 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2091 expose(XEvent
*ev
) {
2092 XExposeEvent
*e
= &ev
->xexpose
;
2093 if(xw
.state
& WIN_REDRAW
) {
2095 xw
.state
&= ~WIN_REDRAW
;
2101 visibility(XEvent
*ev
) {
2102 XVisibilityEvent
*e
= &ev
->xvisibility
;
2103 if(e
->state
== VisibilityFullyObscured
)
2104 xw
.state
&= ~WIN_VISIBLE
;
2105 else if(!(xw
.state
& WIN_VISIBLE
))
2106 /* need a full redraw for next Expose, not just a buf copy */
2107 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2112 xw
.state
&= ~WIN_VISIBLE
;
2116 xseturgency(int add
) {
2117 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2118 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2119 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2125 if(ev
->type
== FocusIn
) {
2126 xw
.state
|= WIN_FOCUSED
;
2129 xw
.state
&= ~WIN_FOCUSED
;
2134 kmap(KeySym k
, uint state
) {
2137 for(i
= 0; i
< LEN(key
); i
++) {
2138 uint mask
= key
[i
].mask
;
2139 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2140 return (char*)key
[i
].s
;
2146 kpress(XEvent
*ev
) {
2147 XKeyEvent
*e
= &ev
->xkey
;
2156 meta
= e
->state
& Mod1Mask
;
2157 shift
= e
->state
& ShiftMask
;
2158 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2160 /* 1. custom keys from config.h */
2161 if((customkey
= kmap(ksym
, e
->state
)))
2162 ttywrite(customkey
, strlen(customkey
));
2163 /* 2. hardcoded (overrides X lookup) */
2170 /* XXX: shift up/down doesn't work */
2171 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2179 if(IS_SET(MODE_CRLF
))
2180 ttywrite("\r\n", 2);
2187 if(meta
&& len
== 1)
2188 ttywrite("\033", 1);
2196 cmessage(XEvent
*e
) {
2198 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2199 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2200 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2201 xw
.state
|= WIN_FOCUSED
;
2203 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2204 xw
.state
&= ~WIN_FOCUSED
;
2214 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2217 xw
.w
= e
->xconfigure
.width
;
2218 xw
.h
= e
->xconfigure
.height
;
2219 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2220 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2221 if(col
== term
.col
&& row
== term
.row
)
2223 if(tresize(col
, row
))
2225 ttyresize(col
, row
);
2230 last_draw_too_old(void) {
2232 gettimeofday(&now
, NULL
);
2233 return TIMEDIFF(now
, xw
.lastdraw
) >= DRAW_TIMEOUT
/1000;
2240 int xfd
= XConnectionNumber(xw
.dpy
);
2241 struct timeval timeout
= {0};
2242 bool stuff_to_print
= 0;
2246 FD_SET(cmdfd
, &rfd
);
2249 timeout
.tv_usec
= SELECT_TIMEOUT
;
2250 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
2253 die("select failed: %s\n", SERRNO
);
2255 if(FD_ISSET(cmdfd
, &rfd
)) {
2260 if(stuff_to_print
&& last_draw_too_old()) {
2265 while(XPending(xw
.dpy
)) {
2266 XNextEvent(xw
.dpy
, &ev
);
2267 if(XFilterEvent(&ev
, xw
.win
))
2269 if(handler
[ev
.type
])
2270 (handler
[ev
.type
])(&ev
);
2276 main(int argc
, char *argv
[]) {
2279 for(i
= 1; i
< argc
; i
++) {
2280 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2282 if(++i
< argc
) opt_title
= argv
[i
];
2285 if(++i
< argc
) opt_class
= argv
[i
];
2288 if(++i
< argc
) opt_embed
= argv
[i
];
2291 /* eat every remaining arguments */
2292 if(++i
< argc
) opt_cmd
= &argv
[i
];
2301 setlocale(LC_CTYPE
, "");