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
;
1107 case 3: /* enter standout (highlight) mode TODO: make it italic */
1108 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1111 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1114 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1117 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1119 case 23: /* leave standout (highlight) mode TODO: make it italic */
1120 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1123 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1126 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1129 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1131 if(BETWEEN(attr
[i
], 0, 255))
1132 term
.c
.attr
.fg
= attr
[i
];
1134 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1137 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1140 term
.c
.attr
.fg
= DefaultFG
;
1143 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1145 if(BETWEEN(attr
[i
], 0, 255))
1146 term
.c
.attr
.bg
= attr
[i
];
1148 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1151 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1154 term
.c
.attr
.bg
= DefaultBG
;
1157 if(BETWEEN(attr
[i
], 30, 37))
1158 term
.c
.attr
.fg
= attr
[i
] - 30;
1159 else if(BETWEEN(attr
[i
], 40, 47))
1160 term
.c
.attr
.bg
= attr
[i
] - 40;
1161 else if(BETWEEN(attr
[i
], 90, 97))
1162 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1163 else if(BETWEEN(attr
[i
], 100, 107))
1164 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1166 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1173 tsetscroll(int t
, int b
) {
1176 LIMIT(t
, 0, term
.row
-1);
1177 LIMIT(b
, 0, term
.row
-1);
1187 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1190 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1193 for (lim
= args
+ narg
; args
< lim
; ++args
) {
1197 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1199 case 5: /* DECSCNM -- Reverve video */
1201 MODBIT(term
.mode
,set
, MODE_REVERSE
);
1202 if (mode
!= term
.mode
)
1206 MODBIT(term
.mode
, set
, MODE_WRAP
);
1209 MODBIT(term
.mode
, set
, MODE_CRLF
);
1211 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1214 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1216 case 1000: /* 1000,1002: enable xterm mouse report */
1217 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1220 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1222 case 1049: /* = 1047 and 1048 */
1225 if(IS_SET(MODE_ALTSCREEN
))
1226 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1227 if ((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1228 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1235 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1239 "erresc: unknown private set/reset mode %d\n",
1246 MODBIT(term
.mode
, set
, MODE_INSERT
);
1250 "erresc: unknown set/reset mode %d\n",
1262 switch(csiescseq
.mode
) {
1265 fprintf(stderr
, "erresc: unknown csi ");
1269 case '@': /* ICH -- Insert <n> blank char */
1270 DEFAULT(csiescseq
.arg
[0], 1);
1271 tinsertblank(csiescseq
.arg
[0]);
1273 case 'A': /* CUU -- Cursor <n> Up */
1275 DEFAULT(csiescseq
.arg
[0], 1);
1276 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1278 case 'B': /* CUD -- Cursor <n> Down */
1279 DEFAULT(csiescseq
.arg
[0], 1);
1280 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1282 case 'C': /* CUF -- Cursor <n> Forward */
1284 DEFAULT(csiescseq
.arg
[0], 1);
1285 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1287 case 'D': /* CUB -- Cursor <n> Backward */
1288 DEFAULT(csiescseq
.arg
[0], 1);
1289 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1291 case 'E': /* CNL -- Cursor <n> Down and first col */
1292 DEFAULT(csiescseq
.arg
[0], 1);
1293 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1295 case 'F': /* CPL -- Cursor <n> Up and first col */
1296 DEFAULT(csiescseq
.arg
[0], 1);
1297 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1299 case 'g': /* TBC -- Tabulation clear */
1300 switch (csiescseq
.arg
[0]) {
1301 case 0: /* clear current tab stop */
1302 term
.tabs
[term
.c
.x
] = 0;
1304 case 3: /* clear all the tabs */
1305 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1311 case 'G': /* CHA -- Move to <col> */
1312 case '`': /* XXX: HPA -- same? */
1313 DEFAULT(csiescseq
.arg
[0], 1);
1314 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1316 case 'H': /* CUP -- Move to <row> <col> */
1317 case 'f': /* XXX: HVP -- same? */
1318 DEFAULT(csiescseq
.arg
[0], 1);
1319 DEFAULT(csiescseq
.arg
[1], 1);
1320 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1322 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1323 DEFAULT(csiescseq
.arg
[0], 1);
1324 while (csiescseq
.arg
[0]--)
1327 case 'J': /* ED -- Clear screen */
1329 switch(csiescseq
.arg
[0]) {
1331 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1332 if(term
.c
.y
< term
.row
-1)
1333 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1337 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1338 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1341 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1347 case 'K': /* EL -- Clear line */
1348 switch(csiescseq
.arg
[0]) {
1350 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1353 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1356 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1360 case 'S': /* SU -- Scroll <n> line up */
1361 DEFAULT(csiescseq
.arg
[0], 1);
1362 tscrollup(term
.top
, csiescseq
.arg
[0]);
1364 case 'T': /* SD -- Scroll <n> line down */
1365 DEFAULT(csiescseq
.arg
[0], 1);
1366 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1368 case 'L': /* IL -- Insert <n> blank lines */
1369 DEFAULT(csiescseq
.arg
[0], 1);
1370 tinsertblankline(csiescseq
.arg
[0]);
1372 case 'l': /* RM -- Reset Mode */
1373 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1375 case 'M': /* DL -- Delete <n> lines */
1376 DEFAULT(csiescseq
.arg
[0], 1);
1377 tdeleteline(csiescseq
.arg
[0]);
1379 case 'X': /* ECH -- Erase <n> char */
1380 DEFAULT(csiescseq
.arg
[0], 1);
1381 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1383 case 'P': /* DCH -- Delete <n> char */
1384 DEFAULT(csiescseq
.arg
[0], 1);
1385 tdeletechar(csiescseq
.arg
[0]);
1387 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1388 DEFAULT(csiescseq
.arg
[0], 1);
1389 while (csiescseq
.arg
[0]--)
1392 case 'd': /* VPA -- Move to <row> */
1393 DEFAULT(csiescseq
.arg
[0], 1);
1394 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1396 case 'h': /* SM -- Set terminal mode */
1397 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1399 case 'm': /* SGR -- Terminal attribute (color) */
1400 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1402 case 'r': /* DECSTBM -- Set Scrolling Region */
1406 DEFAULT(csiescseq
.arg
[0], 1);
1407 DEFAULT(csiescseq
.arg
[1], term
.row
);
1408 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1412 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1413 tcursor(CURSOR_SAVE
);
1415 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1416 tcursor(CURSOR_LOAD
);
1425 for(i
= 0; i
< csiescseq
.len
; i
++) {
1426 uint c
= csiescseq
.buf
[i
] & 0xff;
1427 if(isprint(c
)) putchar(c
);
1428 else if(c
== '\n') printf("(\\n)");
1429 else if(c
== '\r') printf("(\\r)");
1430 else if(c
== 0x1b) printf("(\\e)");
1431 else printf("(%02x)", c
);
1438 memset(&csiescseq
, 0, sizeof(csiescseq
));
1446 * TODO: make this being useful in case of color palette change.
1452 switch(strescseq
.type
) {
1453 case ']': /* OSC -- Operating System Command */
1459 * TODO: Handle special chars in string, like umlauts.
1462 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1466 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1468 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1471 fprintf(stderr
, "erresc: unknown str ");
1476 case 'P': /* DSC -- Device Control String */
1477 case '_': /* APC -- Application Program Command */
1478 case '^': /* PM -- Privacy Message */
1480 fprintf(stderr
, "erresc: unknown str ");
1490 * TODO: Implement parsing like for CSI when required.
1491 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1499 printf("ESC%c", strescseq
.type
);
1500 for(i
= 0; i
< strescseq
.len
; i
++) {
1501 uint c
= strescseq
.buf
[i
] & 0xff;
1502 if(isprint(c
)) putchar(c
);
1503 else if(c
== '\n') printf("(\\n)");
1504 else if(c
== '\r') printf("(\\r)");
1505 else if(c
== 0x1b) printf("(\\e)");
1506 else printf("(%02x)", c
);
1513 memset(&strescseq
, 0, sizeof(strescseq
));
1517 tputtab(bool forward
) {
1518 unsigned x
= term
.c
.x
;
1523 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1528 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1531 tmoveto(x
, term
.c
.y
);
1537 if(term
.esc
& ESC_START
) {
1538 if(term
.esc
& ESC_CSI
) {
1539 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1540 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1542 csiparse(), csihandle();
1544 } else if(term
.esc
& ESC_STR
) {
1547 term
.esc
= ESC_START
| ESC_STR_END
;
1549 case '\a': /* backwards compatibility to xterm */
1554 strescseq
.buf
[strescseq
.len
++] = ascii
;
1555 if (strescseq
.len
+1 >= STR_BUF_SIZ
) {
1560 } else if(term
.esc
& ESC_STR_END
) {
1564 } else if(term
.esc
& ESC_ALTCHARSET
) {
1566 case '0': /* Line drawing crap */
1567 term
.c
.attr
.mode
|= ATTR_GFX
;
1569 case 'B': /* Back to regular text */
1570 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1573 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1579 term
.esc
|= ESC_CSI
;
1581 case 'P': /* DCS -- Device Control String */
1582 case '_': /* APC -- Application Program Command */
1583 case '^': /* PM -- Privacy Message */
1584 case ']': /* OSC -- Operating System Command */
1586 strescseq
.type
= ascii
;
1587 term
.esc
|= ESC_STR
;
1590 term
.esc
|= ESC_ALTCHARSET
;
1592 case 'D': /* IND -- Linefeed */
1593 if(term
.c
.y
== term
.bot
)
1594 tscrollup(term
.top
, 1);
1596 tmoveto(term
.c
.x
, term
.c
.y
+1);
1599 case 'E': /* NEL -- Next line */
1600 tnewline(1); /* always go to first col */
1603 case 'H': /* HTS -- Horizontal tab stop */
1604 term
.tabs
[term
.c
.x
] = 1;
1607 case 'M': /* RI -- Reverse index */
1608 if(term
.c
.y
== term
.top
)
1609 tscrolldown(term
.top
, 1);
1611 tmoveto(term
.c
.x
, term
.c
.y
-1);
1614 case 'c': /* RIS -- Reset to inital state */
1618 case '=': /* DECPAM -- Application keypad */
1619 term
.mode
|= MODE_APPKEYPAD
;
1622 case '>': /* DECPNM -- Normal keypad */
1623 term
.mode
&= ~MODE_APPKEYPAD
;
1626 case '7': /* DECSC -- Save Cursor */
1627 tcursor(CURSOR_SAVE
);
1630 case '8': /* DECRC -- Restore Cursor */
1631 tcursor(CURSOR_LOAD
);
1634 case '\\': /* ST -- Stop */
1638 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1639 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1644 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1651 tmoveto(term
.c
.x
-1, term
.c
.y
);
1654 tmoveto(0, term
.c
.y
);
1659 /* go to first col if the mode is set */
1660 tnewline(IS_SET(MODE_CRLF
));
1663 if(!(xw
.state
& WIN_FOCUSED
))
1668 term
.esc
= ESC_START
;
1671 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1672 tnewline(1); /* always go to first col */
1674 if(term
.c
.x
+1 < term
.col
)
1675 tmoveto(term
.c
.x
+1, term
.c
.y
);
1677 term
.c
.state
|= CURSOR_WRAPNEXT
;
1683 tresize(int col
, int row
) {
1685 int minrow
= MIN(row
, term
.row
);
1686 int mincol
= MIN(col
, term
.col
);
1687 int slide
= term
.c
.y
- row
+ 1;
1689 if(col
< 1 || row
< 1)
1692 /* free unneeded rows */
1695 /* slide screen to keep cursor where we expect it -
1696 * tscrollup would work here, but we can optimize to
1697 * memmove because we're freeing the earlier lines */
1698 for(/* i = 0 */; i
< slide
; i
++) {
1702 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1703 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1705 for(i
+= row
; i
< term
.row
; i
++) {
1710 /* resize to new height */
1711 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1712 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1713 term
.dirty
= realloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1714 term
.tabs
= realloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1716 /* resize each row to new width, zero-pad if needed */
1717 for(i
= 0; i
< minrow
; i
++) {
1719 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1720 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1721 for(x
= mincol
; x
< col
; x
++) {
1722 term
.line
[i
][x
].state
= 0;
1723 term
.alt
[i
][x
].state
= 0;
1727 /* allocate any new rows */
1728 for(/* i == minrow */; i
< row
; i
++) {
1730 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1731 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1733 if (col
> term
.col
) {
1734 bool *bp
= term
.tabs
+ term
.col
;
1736 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1737 while (--bp
> term
.tabs
&& !*bp
)
1739 for (bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1742 /* update terminal size */
1743 term
.col
= col
, term
.row
= row
;
1744 /* make use of the LIMIT in tmoveto */
1745 tmoveto(term
.c
.x
, term
.c
.y
);
1746 /* reset scrolling region */
1747 tsetscroll(0, row
-1);
1753 xresize(int col
, int row
) {
1754 xw
.w
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1755 xw
.h
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1762 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1764 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1765 for(i
= 0; i
< LEN(colorname
); i
++) {
1768 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1770 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1772 dc
.col
[i
] = color
.pixel
;
1775 /* load colors [16-255] ; same colors as xterm */
1776 for(i
= 16, r
= 0; r
< 6; r
++)
1777 for(g
= 0; g
< 6; g
++)
1778 for(b
= 0; b
< 6; b
++) {
1779 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1780 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1781 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1782 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1784 fprintf(stderr
, "Could not allocate color %d\n", i
);
1786 dc
.col
[i
] = color
.pixel
;
1790 for(r
= 0; r
< 24; r
++, i
++) {
1791 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1792 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1794 fprintf(stderr
, "Could not allocate color %d\n", i
);
1796 dc
.col
[i
] = color
.pixel
;
1801 xclear(int x1
, int y1
, int x2
, int y2
) {
1802 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1803 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1804 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1805 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1810 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1811 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1813 .flags
= PSize
| PResizeInc
| PBaseSize
,
1816 .height_inc
= xw
.ch
,
1818 .base_height
= 2*BORDER
,
1819 .base_width
= 2*BORDER
,
1821 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1825 xinitfont(char *fontstr
) {
1827 char *def
, **missing
;
1831 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1834 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1835 XFreeStringList(missing
);
1841 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1842 XFontStruct
**xfonts
;
1846 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1847 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1848 for(i
= 0; i
< n
; i
++) {
1849 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1850 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1851 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1852 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1858 initfonts(char *fontstr
, char *bfontstr
) {
1859 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1860 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1861 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1862 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1863 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1864 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1865 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1870 XSetWindowAttributes attrs
;
1874 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1875 die("Can't open display\n");
1876 xw
.scr
= XDefaultScreen(xw
.dpy
);
1879 initfonts(FONT
, BOLDFONT
);
1881 /* XXX: Assuming same size for bold font */
1882 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1883 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1886 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1889 /* window - default size */
1890 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
1891 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
1893 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1894 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1895 attrs
.bit_gravity
= NorthWestGravity
;
1896 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1897 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1898 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1899 | EnterWindowMask
| LeaveWindowMask
;
1900 attrs
.colormap
= xw
.cmap
;
1902 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1903 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1904 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1905 XDefaultVisual(xw
.dpy
, xw
.scr
),
1906 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1909 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
1913 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1914 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1915 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1916 XNFocusWindow
, xw
.win
, NULL
);
1918 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1920 /* white cursor, black outline */
1921 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1922 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1923 XRecolorCursor(xw
.dpy
, cursor
,
1924 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1925 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1927 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1929 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1930 XMapWindow(xw
.dpy
, xw
.win
);
1936 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1937 int fg
= base
.fg
, bg
= base
.bg
, temp
;
1938 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1939 XFontSet fontset
= dc
.font
.set
;
1942 /* only switch default fg/bg if term is in RV mode */
1943 if(IS_SET(MODE_REVERSE
)) {
1950 if(base
.mode
& ATTR_REVERSE
)
1951 temp
= fg
, fg
= bg
, bg
= temp
;
1953 if(base
.mode
& ATTR_BOLD
) {
1955 fontset
= dc
.bfont
.set
;
1958 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
1959 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
1961 if(base
.mode
& ATTR_GFX
) {
1962 for(i
= 0; i
< bytelen
; i
++) {
1963 char c
= gfx
[(uint
)s
[i
] % 256];
1966 else if(s
[i
] > 0x5f)
1971 XmbDrawImageString(xw
.dpy
, xw
.buf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
1973 if(base
.mode
& ATTR_UNDERLINE
)
1974 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1977 /* copy buffer pixmap to screen pixmap */
1980 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
1981 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
1987 static int oldx
= 0;
1988 static int oldy
= 0;
1990 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1992 LIMIT(oldx
, 0, term
.col
-1);
1993 LIMIT(oldy
, 0, term
.row
-1);
1995 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1996 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1998 /* remove the old cursor */
1999 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2000 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2001 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2003 xclear(oldx
, oldy
, oldx
, oldy
);
2005 xcopy(oldx
, oldy
, 1, 1);
2007 /* draw the new one */
2008 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2009 if(!(xw
.state
& WIN_FOCUSED
))
2012 if(IS_SET(MODE_REVERSE
))
2013 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2016 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2017 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2020 xcopy(term
.c
.x
, term
.c
.y
, 1, 1);
2025 drawregion(0, 0, term
.col
, term
.row
);
2027 gettimeofday(&xw
.lastdraw
, NULL
);
2031 drawregion(int x1
, int y1
, int x2
, int y2
) {
2032 int ic
, ib
, x
, y
, ox
, sl
;
2034 char buf
[DRAW_BUF_SIZ
];
2036 if(!(xw
.state
& WIN_VISIBLE
))
2039 for(y
= y1
; y
< y2
; y
++) {
2042 xclear(0, y
, term
.col
, y
);
2044 base
= term
.line
[y
][0];
2046 for(x
= x1
; x
< x2
; x
++) {
2047 new = term
.line
[y
][x
];
2048 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
2049 new.mode
^= ATTR_REVERSE
;
2050 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2051 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2052 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2055 if(new.state
& GLYPH_SET
) {
2060 sl
= utf8size(new.c
);
2061 memcpy(buf
+ib
, new.c
, sl
);
2067 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2073 expose(XEvent
*ev
) {
2074 XExposeEvent
*e
= &ev
->xexpose
;
2075 if(xw
.state
& WIN_REDRAW
) {
2077 xw
.state
&= ~WIN_REDRAW
;
2083 visibility(XEvent
*ev
) {
2084 XVisibilityEvent
*e
= &ev
->xvisibility
;
2085 if(e
->state
== VisibilityFullyObscured
)
2086 xw
.state
&= ~WIN_VISIBLE
;
2087 else if(!(xw
.state
& WIN_VISIBLE
))
2088 /* need a full redraw for next Expose, not just a buf copy */
2089 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2094 xw
.state
&= ~WIN_VISIBLE
;
2098 xseturgency(int add
) {
2099 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2100 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2101 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2107 if(ev
->type
== FocusIn
) {
2108 xw
.state
|= WIN_FOCUSED
;
2111 xw
.state
&= ~WIN_FOCUSED
;
2116 kmap(KeySym k
, uint state
) {
2119 for(i
= 0; i
< LEN(key
); i
++) {
2120 uint mask
= key
[i
].mask
;
2121 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2122 return (char*)key
[i
].s
;
2128 kpress(XEvent
*ev
) {
2129 XKeyEvent
*e
= &ev
->xkey
;
2138 meta
= e
->state
& Mod1Mask
;
2139 shift
= e
->state
& ShiftMask
;
2140 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2142 /* 1. custom keys from config.h */
2143 if((customkey
= kmap(ksym
, e
->state
)))
2144 ttywrite(customkey
, strlen(customkey
));
2145 /* 2. hardcoded (overrides X lookup) */
2152 /* XXX: shift up/down doesn't work */
2153 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2161 if(IS_SET(MODE_CRLF
))
2162 ttywrite("\r\n", 2);
2169 if(meta
&& len
== 1)
2170 ttywrite("\033", 1);
2178 cmessage(XEvent
*e
) {
2180 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2181 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2182 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2183 xw
.state
|= WIN_FOCUSED
;
2185 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2186 xw
.state
&= ~WIN_FOCUSED
;
2196 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2199 xw
.w
= e
->xconfigure
.width
;
2200 xw
.h
= e
->xconfigure
.height
;
2201 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2202 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2203 if(col
== term
.col
&& row
== term
.row
)
2205 if(tresize(col
, row
))
2207 ttyresize(col
, row
);
2212 last_draw_too_old(void) {
2214 gettimeofday(&now
, NULL
);
2215 return TIMEDIFF(now
, xw
.lastdraw
) >= DRAW_TIMEOUT
/1000;
2222 int xfd
= XConnectionNumber(xw
.dpy
);
2223 struct timeval timeout
= {0};
2224 bool stuff_to_print
= 0;
2228 FD_SET(cmdfd
, &rfd
);
2231 timeout
.tv_usec
= SELECT_TIMEOUT
;
2232 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
2235 die("select failed: %s\n", SERRNO
);
2237 if(FD_ISSET(cmdfd
, &rfd
)) {
2242 if(stuff_to_print
&& last_draw_too_old()) {
2247 while(XPending(xw
.dpy
)) {
2248 XNextEvent(xw
.dpy
, &ev
);
2249 if(XFilterEvent(&ev
, xw
.win
))
2251 if(handler
[ev
.type
])
2252 (handler
[ev
.type
])(&ev
);
2258 main(int argc
, char *argv
[]) {
2261 for(i
= 1; i
< argc
; i
++) {
2262 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2264 if(++i
< argc
) opt_title
= argv
[i
];
2267 if(++i
< argc
) opt_class
= argv
[i
];
2270 if(++i
< argc
) opt_embed
= argv
[i
];
2273 /* eat every remaining arguments */
2274 if(++i
< argc
) opt_cmd
= &argv
[i
];
2283 setlocale(LC_CTYPE
, "");