1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
15 #include <sys/ioctl.h>
16 #include <sys/select.h>
19 #include <sys/types.h>
23 #include <X11/Xatom.h>
25 #include <X11/Xutil.h>
26 #include <X11/cursorfont.h>
27 #include <X11/keysym.h>
28 #include <X11/extensions/Xdbe.h>
29 #include <X11/Xft/Xft.h>
30 #include <fontconfig/fontconfig.h>
34 #define Draw XftDraw *
35 #define Colour XftColor
36 #define Colourmap Colormap
40 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
42 #elif defined(__FreeBSD__) || defined(__DragonFly__)
47 "st " VERSION " (c) 2010-2013 st engineers\n" \
48 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
49 " [-t title] [-w windowid] [-e command ...]\n"
52 #define XEMBED_FOCUS_IN 4
53 #define XEMBED_FOCUS_OUT 5
57 #define ESC_BUF_SIZ (128*UTF_SIZ)
58 #define ESC_ARG_SIZ 16
59 #define STR_BUF_SIZ ESC_BUF_SIZ
60 #define STR_ARG_SIZ ESC_ARG_SIZ
61 #define DRAW_BUF_SIZ 20*1024
62 #define XK_ANY_MOD UINT_MAX
64 #define XK_SWITCH_MOD (1<<13)
66 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
69 #define SERRNO strerror(errno)
70 #define MIN(a, b) ((a) < (b) ? (a) : (b))
71 #define MAX(a, b) ((a) < (b) ? (b) : (a))
72 #define LEN(a) (sizeof(a) / sizeof(a[0]))
73 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
74 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
75 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
76 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
77 #define IS_SET(flag) ((term.mode & (flag)) != 0)
78 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
80 #define VT102ID "\033[?6c"
82 enum glyph_attribute
{
92 enum cursor_movement
{
115 MODE_MOUSEMOTION
= 64,
121 MODE_APPCURSOR
= 2048,
122 MODE_MOUSESGR
= 4096,
128 ESC_STR
= 4, /* DSC, OSC, PM, APC */
130 ESC_STR_END
= 16, /* a final string was encountered */
131 ESC_TEST
= 32, /* Enter in test mode */
142 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
144 typedef unsigned char uchar
;
145 typedef unsigned int uint
;
146 typedef unsigned long ulong
;
147 typedef unsigned short ushort
;
150 char c
[UTF_SIZ
]; /* character code */
151 uchar mode
; /* attribute flags */
152 ushort fg
; /* foreground */
153 ushort bg
; /* background */
154 uchar state
; /* state flags */
160 Glyph attr
; /* current char attributes */
166 /* CSI Escape sequence structs */
167 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
169 char buf
[ESC_BUF_SIZ
]; /* raw string */
170 int len
; /* raw string length */
172 int arg
[ESC_ARG_SIZ
];
173 int narg
; /* nb of args */
177 /* STR Escape sequence structs */
178 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
180 char type
; /* ESC type ... */
181 char buf
[STR_BUF_SIZ
]; /* raw string */
182 int len
; /* raw string length */
183 char *args
[STR_ARG_SIZ
];
184 int narg
; /* nb of args */
187 /* Internal representation of the screen */
189 int row
; /* nb row */
190 int col
; /* nb col */
191 Line
*line
; /* screen */
192 Line
*alt
; /* alternate screen */
193 bool *dirty
; /* dirtyness of lines */
194 TCursor c
; /* cursor */
195 int top
; /* top scroll limit */
196 int bot
; /* bottom scroll limit */
197 int mode
; /* terminal mode flags */
198 int esc
; /* escape state flags */
199 bool numlock
; /* lock numbers in keyboard */
203 /* Purely graphic info */
209 Atom xembed
, wmdeletewin
;
215 bool isfixed
; /* is fixed geometry? */
216 int fx
, fy
, fw
, fh
; /* fixed geometry */
217 int tw
, th
; /* tty width and height */
218 int w
, h
; /* window width and height */
219 int ch
; /* char height */
220 int cw
; /* char width */
221 char state
; /* focus, redraw, visible */
228 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
229 signed char appkey
; /* application keypad */
230 signed char appcursor
; /* application cursor */
231 signed char crlf
; /* crlf mode */
234 /* TODO: use better name for vars... */
245 struct timeval tclick1
;
246 struct timeval tclick2
;
259 void (*func
)(const Arg
*);
263 /* function definitions used in config.h */
264 static void xzoom(const Arg
*);
265 static void selpaste(const Arg
*);
266 static void numlock(const Arg
*);
268 /* Config.h for applying patches and the configuration. */
284 /* Drawing Context */
286 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
287 Font font
, bfont
, ifont
, ibfont
;
291 static void die(const char *, ...);
292 static void draw(void);
293 static void redraw(int);
294 static void drawregion(int, int, int, int);
295 static void execsh(void);
296 static void sigchld(int);
297 static void run(void);
299 static void csidump(void);
300 static void csihandle(void);
301 static void csiparse(void);
302 static void csireset(void);
303 static void strdump(void);
304 static void strhandle(void);
305 static void strparse(void);
306 static void strreset(void);
308 static void tclearregion(int, int, int, int, int);
309 static void tcursor(int);
310 static void tdeletechar(int);
311 static void tdeleteline(int);
312 static void tinsertblank(int);
313 static void tinsertblankline(int);
314 static void tmoveto(int, int);
315 static void tmoveato(int x
, int y
);
316 static void tnew(int, int);
317 static void tnewline(int);
318 static void tputtab(bool);
319 static void tputc(char *, int);
320 static void treset(void);
321 static int tresize(int, int);
322 static void tscrollup(int, int);
323 static void tscrolldown(int, int);
324 static void tsetattr(int*, int);
325 static void tsetchar(char *, Glyph
*, int, int);
326 static void tsetscroll(int, int);
327 static void tswapscreen(void);
328 static void tsetdirt(int, int);
329 static void tsetmode(bool, bool, int *, int);
330 static void tfulldirt(void);
331 static void techo(char *, int);
333 static inline bool match(uint
, uint
);
334 static void ttynew(void);
335 static void ttyread(void);
336 static void ttyresize(void);
337 static void ttywrite(const char *, size_t);
339 static void xdraws(char *, Glyph
, int, int, int, int);
340 static void xhints(void);
341 static void xclear(int, int, int, int);
342 static void xdrawcursor(void);
343 static void xinit(void);
344 static void xloadcols(void);
345 static int xloadfont(Font
*, FcPattern
*);
346 static void xloadfonts(char *, int);
347 static void xresettitle(void);
348 static void xseturgency(int);
349 static void xsetsel(char*);
350 static void xtermclear(int, int, int, int);
351 static void xunloadfonts(void);
352 static void xresize(int, int);
354 static void expose(XEvent
*);
355 static void visibility(XEvent
*);
356 static void unmap(XEvent
*);
357 static char *kmap(KeySym
, uint
);
358 static void kpress(XEvent
*);
359 static void cmessage(XEvent
*);
360 static void cresize(int, int);
361 static void resize(XEvent
*);
362 static void focus(XEvent
*);
363 static void brelease(XEvent
*);
364 static void bpress(XEvent
*);
365 static void bmotion(XEvent
*);
366 static void selnotify(XEvent
*);
367 static void selclear(XEvent
*);
368 static void selrequest(XEvent
*);
370 static void selinit(void);
371 static inline bool selected(int, int);
372 static void selcopy(void);
373 static void selscroll(int, int);
375 static int utf8decode(char *, long *);
376 static int utf8encode(long *, char *);
377 static int utf8size(char *);
378 static int isfullutf8(char *, int);
380 static ssize_t
xwrite(int, char *, size_t);
381 static void *xmalloc(size_t);
382 static void *xrealloc(void *, size_t);
383 static void *xcalloc(size_t, size_t);
385 static void (*handler
[LASTEvent
])(XEvent
*) = {
387 [ClientMessage
] = cmessage
,
388 [ConfigureNotify
] = resize
,
389 [VisibilityNotify
] = visibility
,
390 [UnmapNotify
] = unmap
,
394 [MotionNotify
] = bmotion
,
395 [ButtonPress
] = bpress
,
396 [ButtonRelease
] = brelease
,
397 [SelectionClear
] = selclear
,
398 [SelectionNotify
] = selnotify
,
399 [SelectionRequest
] = selrequest
,
406 static CSIEscape csiescseq
;
407 static STREscape strescseq
;
410 static Selection sel
;
411 static int iofd
= -1;
412 static char **opt_cmd
= NULL
;
413 static char *opt_io
= NULL
;
414 static char *opt_title
= NULL
;
415 static char *opt_embed
= NULL
;
416 static char *opt_class
= NULL
;
417 static char *opt_font
= NULL
;
421 static char *usedfont
= NULL
;
422 static int usedfontsize
= 0;
424 /* Font Ring Cache */
439 * Fontcache is a ring buffer, with frccur as current position and frclen as
440 * the current length of used elements.
443 static Fontcache frc
[1024];
444 static int frccur
= -1, frclen
= 0;
447 xwrite(int fd
, char *s
, size_t len
) {
451 ssize_t r
= write(fd
, s
, len
);
461 xmalloc(size_t len
) {
462 void *p
= malloc(len
);
465 die("Out of memory\n");
471 xrealloc(void *p
, size_t len
) {
472 if((p
= realloc(p
, len
)) == NULL
)
473 die("Out of memory\n");
479 xcalloc(size_t nmemb
, size_t size
) {
480 void *p
= calloc(nmemb
, size
);
483 die("Out of memory\n");
489 utf8decode(char *s
, long *u
) {
495 if(~c
& B7
) { /* 0xxxxxxx */
498 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
499 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
501 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
502 *u
= c
&(B3
|B2
|B1
|B0
);
504 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
511 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
513 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
516 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
519 if((n
== 1 && *u
< 0x80) ||
520 (n
== 2 && *u
< 0x800) ||
521 (n
== 3 && *u
< 0x10000) ||
522 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
534 utf8encode(long *u
, char *s
) {
542 *sp
= uc
; /* 0xxxxxxx */
544 } else if(*u
< 0x800) {
545 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
547 } else if(uc
< 0x10000) {
548 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
550 } else if(uc
<= 0x10FFFF) {
551 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
557 for(i
=n
,++sp
; i
>0; --i
,++sp
)
558 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
570 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
571 UTF-8 otherwise return 0 */
573 isfullutf8(char *s
, int b
) {
581 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
583 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
585 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
587 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
589 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
590 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
603 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
605 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
614 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
615 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
619 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
620 if(sel
.xtarget
== None
)
621 sel
.xtarget
= XA_STRING
;
629 return LIMIT(x
, 0, term
.col
-1);
637 return LIMIT(y
, 0, term
.row
-1);
641 selected(int x
, int y
) {
644 if(sel
.ey
== y
&& sel
.by
== y
) {
645 bx
= MIN(sel
.bx
, sel
.ex
);
646 ex
= MAX(sel
.bx
, sel
.ex
);
647 return BETWEEN(x
, bx
, ex
);
650 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
651 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
652 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
653 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
657 getbuttoninfo(XEvent
*e
) {
658 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
660 sel
.ex
= x2col(e
->xbutton
.x
);
661 sel
.ey
= y2row(e
->xbutton
.y
);
663 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
664 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
665 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
666 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
670 mousereport(XEvent
*e
) {
671 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
672 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
675 static int ob
, ox
, oy
;
678 if(e
->xbutton
.type
== MotionNotify
) {
679 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
683 } else if(!IS_SET(MODE_MOUSESGR
)
684 && (e
->xbutton
.type
== ButtonRelease
685 || button
== AnyButton
)) {
691 if(e
->xbutton
.type
== ButtonPress
) {
697 button
+= (state
& ShiftMask
? 4 : 0)
698 + (state
& Mod4Mask
? 8 : 0)
699 + (state
& ControlMask
? 16 : 0);
702 if(IS_SET(MODE_MOUSESGR
)) {
703 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
705 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
706 } else if(x
< 223 && y
< 223) {
707 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
708 32+button
, 32+x
+1, 32+y
+1);
718 if(IS_SET(MODE_MOUSE
)) {
720 } else if(e
->xbutton
.button
== Button1
) {
723 tsetdirt(sel
.b
.y
, sel
.e
.y
);
727 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
728 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
729 } else if(e
->xbutton
.button
== Button4
) {
731 } else if(e
->xbutton
.button
== Button5
) {
739 int x
, y
, bufsize
, is_selected
= 0, size
;
745 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
746 ptr
= str
= xmalloc(bufsize
);
748 /* append every set & selected glyph to the selection */
749 for(y
= 0; y
< term
.row
; y
++) {
750 gp
= &term
.line
[y
][0];
751 last
= gp
+ term
.col
;
753 while(--last
>= gp
&& !(last
->state
& GLYPH_SET
))
756 for(x
= 0; gp
<= last
; x
++, ++gp
) {
757 if(!(is_selected
= selected(x
, y
)))
760 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
762 memcpy(ptr
, p
, size
);
765 /* \n at the end of every selected line except for the last one */
766 if(is_selected
&& y
< sel
.e
.y
)
775 selnotify(XEvent
*e
) {
776 ulong nitems
, ofs
, rem
;
783 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
784 False
, AnyPropertyType
, &type
, &format
,
785 &nitems
, &rem
, &data
)) {
786 fprintf(stderr
, "Clipboard allocation failed\n");
789 ttywrite((const char *) data
, nitems
* format
/ 8);
791 /* number of 32-bit chunks returned */
792 ofs
+= nitems
* format
/ 32;
797 selpaste(const Arg
*dummy
) {
798 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
799 xw
.win
, CurrentTime
);
802 void selclear(XEvent
*e
) {
806 tsetdirt(sel
.b
.y
, sel
.e
.y
);
810 selrequest(XEvent
*e
) {
811 XSelectionRequestEvent
*xsre
;
813 Atom xa_targets
, string
;
815 xsre
= (XSelectionRequestEvent
*) e
;
816 xev
.type
= SelectionNotify
;
817 xev
.requestor
= xsre
->requestor
;
818 xev
.selection
= xsre
->selection
;
819 xev
.target
= xsre
->target
;
820 xev
.time
= xsre
->time
;
824 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
825 if(xsre
->target
== xa_targets
) {
826 /* respond with the supported type */
827 string
= sel
.xtarget
;
828 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
829 XA_ATOM
, 32, PropModeReplace
,
830 (uchar
*) &string
, 1);
831 xev
.property
= xsre
->property
;
832 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
833 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
834 xsre
->target
, 8, PropModeReplace
,
835 (uchar
*) sel
.clip
, strlen(sel
.clip
));
836 xev
.property
= xsre
->property
;
839 /* all done, send a notification to the listener */
840 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
841 fprintf(stderr
, "Error sending SelectionNotify event\n");
846 /* register the selection for both the clipboard and the primary */
852 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
854 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
855 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
859 brelease(XEvent
*e
) {
862 if(IS_SET(MODE_MOUSE
)) {
867 if(e
->xbutton
.button
== Button2
) {
869 } else if(e
->xbutton
.button
== Button1
) {
872 term
.dirty
[sel
.ey
] = 1;
873 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
875 gettimeofday(&now
, NULL
);
877 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
878 /* triple click on the line */
879 sel
.b
.x
= sel
.bx
= 0;
880 sel
.e
.x
= sel
.ex
= term
.col
;
881 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
883 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
884 /* double click to select word */
886 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
887 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
891 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
892 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
896 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
904 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
905 gettimeofday(&sel
.tclick1
, NULL
);
910 int starty
, endy
, oldey
, oldex
;
912 if(IS_SET(MODE_MOUSE
)) {
924 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
925 starty
= MIN(oldey
, sel
.ey
);
926 endy
= MAX(oldey
, sel
.ey
);
927 tsetdirt(starty
, endy
);
932 die(const char *errstr
, ...) {
935 va_start(ap
, errstr
);
936 vfprintf(stderr
, errstr
, ap
);
944 char *envshell
= getenv("SHELL");
945 const struct passwd
*pass
= getpwuid(getuid());
946 char buf
[sizeof(long) * 8 + 1];
953 setenv("LOGNAME", pass
->pw_name
, 1);
954 setenv("USER", pass
->pw_name
, 1);
955 setenv("SHELL", pass
->pw_shell
, 0);
956 setenv("HOME", pass
->pw_dir
, 0);
959 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
960 setenv("WINDOWID", buf
, 1);
962 signal(SIGCHLD
, SIG_DFL
);
963 signal(SIGHUP
, SIG_DFL
);
964 signal(SIGINT
, SIG_DFL
);
965 signal(SIGQUIT
, SIG_DFL
);
966 signal(SIGTERM
, SIG_DFL
);
967 signal(SIGALRM
, SIG_DFL
);
969 DEFAULT(envshell
, shell
);
970 setenv("TERM", termname
, 1);
971 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
972 execvp(args
[0], args
);
980 if(waitpid(pid
, &stat
, 0) < 0)
981 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
983 if(WIFEXITED(stat
)) {
984 exit(WEXITSTATUS(stat
));
993 struct winsize w
= {term
.row
, term
.col
, 0, 0};
995 /* seems to work fine on linux, openbsd and freebsd */
996 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
997 die("openpty failed: %s\n", SERRNO
);
999 switch(pid
= fork()) {
1001 die("fork failed\n");
1004 setsid(); /* create a new process group */
1005 dup2(s
, STDIN_FILENO
);
1006 dup2(s
, STDOUT_FILENO
);
1007 dup2(s
, STDERR_FILENO
);
1008 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1009 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1017 signal(SIGCHLD
, sigchld
);
1019 iofd
= (!strcmp(opt_io
, "-")) ?
1021 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1023 fprintf(stderr
, "Error opening %s:%s\n",
1024 opt_io
, strerror(errno
));
1034 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1036 fprintf(stderr
, "\n");
1041 static char buf
[BUFSIZ
];
1042 static int buflen
= 0;
1045 int charsize
; /* size of utf8 char in bytes */
1049 /* append read bytes to unprocessed bytes */
1050 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1051 die("Couldn't read from shell: %s\n", SERRNO
);
1053 /* process every complete utf8 char */
1056 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1057 charsize
= utf8decode(ptr
, &utf8c
);
1058 utf8encode(&utf8c
, s
);
1064 /* keep any uncomplete utf8 char for the next call */
1065 memmove(buf
, ptr
, buflen
);
1069 ttywrite(const char *s
, size_t n
) {
1070 if(write(cmdfd
, s
, n
) == -1)
1071 die("write error on tty: %s\n", SERRNO
);
1078 w
.ws_row
= term
.row
;
1079 w
.ws_col
= term
.col
;
1080 w
.ws_xpixel
= xw
.tw
;
1081 w
.ws_ypixel
= xw
.th
;
1082 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1083 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1087 tsetdirt(int top
, int bot
) {
1090 LIMIT(top
, 0, term
.row
-1);
1091 LIMIT(bot
, 0, term
.row
-1);
1093 for(i
= top
; i
<= bot
; i
++)
1099 tsetdirt(0, term
.row
-1);
1106 if(mode
== CURSOR_SAVE
) {
1108 } else if(mode
== CURSOR_LOAD
) {
1118 term
.c
= (TCursor
){{
1122 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1124 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1125 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1128 term
.bot
= term
.row
- 1;
1129 term
.mode
= MODE_WRAP
;
1131 tclearregion(0, 0, term
.col
-1, term
.row
-1, 0);
1133 tcursor(CURSOR_SAVE
);
1137 tnew(int col
, int row
) {
1138 /* set screen size */
1141 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1142 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1143 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1144 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1146 for(row
= 0; row
< term
.row
; row
++) {
1147 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1148 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1149 term
.dirty
[row
] = 0;
1153 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1160 Line
*tmp
= term
.line
;
1162 term
.line
= term
.alt
;
1164 term
.mode
^= MODE_ALTSCREEN
;
1169 tscrolldown(int orig
, int n
) {
1173 LIMIT(n
, 0, term
.bot
-orig
+1);
1175 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
, 0);
1177 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1178 temp
= term
.line
[i
];
1179 term
.line
[i
] = term
.line
[i
-n
];
1180 term
.line
[i
-n
] = temp
;
1183 term
.dirty
[i
-n
] = 1;
1190 tscrollup(int orig
, int n
) {
1193 LIMIT(n
, 0, term
.bot
-orig
+1);
1195 tclearregion(0, orig
, term
.col
-1, orig
+n
-1, 0);
1197 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1198 temp
= term
.line
[i
];
1199 term
.line
[i
] = term
.line
[i
+n
];
1200 term
.line
[i
+n
] = temp
;
1203 term
.dirty
[i
+n
] = 1;
1206 selscroll(orig
, -n
);
1210 selscroll(int orig
, int n
) {
1214 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1215 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1219 if(sel
.by
< term
.top
) {
1223 if(sel
.ey
> term
.bot
) {
1227 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1228 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1233 tnewline(int first_col
) {
1237 tscrollup(term
.top
, 1);
1241 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1246 /* int noarg = 1; */
1247 char *p
= csiescseq
.buf
;
1251 csiescseq
.priv
= 1, p
++;
1253 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1254 while(isdigit(*p
)) {
1255 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1256 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1258 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1259 csiescseq
.narg
++, p
++;
1261 csiescseq
.mode
= *p
;
1269 /* for absolute user moves, when decom is set */
1271 tmoveato(int x
, int y
) {
1272 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1276 tmoveto(int x
, int y
) {
1279 if(term
.c
.state
& CURSOR_ORIGIN
) {
1284 maxy
= term
.row
- 1;
1286 LIMIT(x
, 0, term
.col
-1);
1287 LIMIT(y
, miny
, maxy
);
1288 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1294 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1295 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1296 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1297 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1298 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1299 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1300 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1301 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1302 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1303 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1307 * The table is proudly stolen from rxvt.
1309 if(attr
->mode
& ATTR_GFX
) {
1310 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1311 && vt100_0
[c
[0] - 0x41]) {
1312 c
= vt100_0
[c
[0] - 0x41];
1317 term
.line
[y
][x
] = *attr
;
1318 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1319 term
.line
[y
][x
].state
|= GLYPH_SET
;
1323 tclearregion(int x1
, int y1
, int x2
, int y2
, int bce
) {
1327 temp
= x1
, x1
= x2
, x2
= temp
;
1329 temp
= y1
, y1
= y2
, y2
= temp
;
1331 LIMIT(x1
, 0, term
.col
-1);
1332 LIMIT(x2
, 0, term
.col
-1);
1333 LIMIT(y1
, 0, term
.row
-1);
1334 LIMIT(y2
, 0, term
.row
-1);
1336 for(y
= y1
; y
<= y2
; y
++) {
1338 for(x
= x1
; x
<= x2
; x
++) {
1340 term
.line
[y
][x
] = term
.c
.attr
;
1341 memcpy(term
.line
[y
][x
].c
, " ", 2);
1342 term
.line
[y
][x
].state
|= GLYPH_SET
;
1344 term
.line
[y
][x
].state
= 0;
1351 tdeletechar(int n
) {
1352 int src
= term
.c
.x
+ n
;
1354 int size
= term
.col
- src
;
1356 term
.dirty
[term
.c
.y
] = 1;
1358 if(src
>= term
.col
) {
1359 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1363 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1364 size
* sizeof(Glyph
));
1365 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1369 tinsertblank(int n
) {
1372 int size
= term
.col
- dst
;
1374 term
.dirty
[term
.c
.y
] = 1;
1376 if(dst
>= term
.col
) {
1377 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1381 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1382 size
* sizeof(Glyph
));
1383 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
, 0);
1387 tinsertblankline(int n
) {
1388 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1391 tscrolldown(term
.c
.y
, n
);
1395 tdeleteline(int n
) {
1396 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1399 tscrollup(term
.c
.y
, n
);
1403 tsetattr(int *attr
, int l
) {
1406 for(i
= 0; i
< l
; i
++) {
1409 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1410 | ATTR_ITALIC
| ATTR_BLINK
);
1411 term
.c
.attr
.fg
= defaultfg
;
1412 term
.c
.attr
.bg
= defaultbg
;
1415 term
.c
.attr
.mode
|= ATTR_BOLD
;
1418 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1421 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1423 case 5: /* slow blink */
1424 case 6: /* rapid blink */
1425 term
.c
.attr
.mode
|= ATTR_BLINK
;
1428 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1432 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1435 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1438 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1442 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1445 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1448 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1450 if(BETWEEN(attr
[i
], 0, 255)) {
1451 term
.c
.attr
.fg
= attr
[i
];
1454 "erresc: bad fgcolor %d\n",
1459 "erresc(38): gfx attr %d unknown\n",
1464 term
.c
.attr
.fg
= defaultfg
;
1467 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1469 if(BETWEEN(attr
[i
], 0, 255)) {
1470 term
.c
.attr
.bg
= attr
[i
];
1473 "erresc: bad bgcolor %d\n",
1478 "erresc(48): gfx attr %d unknown\n",
1483 term
.c
.attr
.bg
= defaultbg
;
1486 if(BETWEEN(attr
[i
], 30, 37)) {
1487 term
.c
.attr
.fg
= attr
[i
] - 30;
1488 } else if(BETWEEN(attr
[i
], 40, 47)) {
1489 term
.c
.attr
.bg
= attr
[i
] - 40;
1490 } else if(BETWEEN(attr
[i
], 90, 97)) {
1491 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1492 } else if(BETWEEN(attr
[i
], 100, 107)) {
1493 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1496 "erresc(default): gfx attr %d unknown\n",
1497 attr
[i
]), csidump();
1505 tsetscroll(int t
, int b
) {
1508 LIMIT(t
, 0, term
.row
-1);
1509 LIMIT(b
, 0, term
.row
-1);
1519 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1522 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1526 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1530 case 1: /* DECCKM -- Cursor key */
1531 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1533 case 5: /* DECSCNM -- Reverse video */
1535 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1536 if(mode
!= term
.mode
)
1537 redraw(REDRAW_TIMEOUT
);
1539 case 6: /* DECOM -- Origin */
1540 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1543 case 7: /* DECAWM -- Auto wrap */
1544 MODBIT(term
.mode
, set
, MODE_WRAP
);
1546 case 0: /* Error (IGNORED) */
1547 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1548 case 3: /* DECCOLM -- Column (IGNORED) */
1549 case 4: /* DECSCLM -- Scroll (IGNORED) */
1550 case 8: /* DECARM -- Auto repeat (IGNORED) */
1551 case 18: /* DECPFF -- Printer feed (IGNORED) */
1552 case 19: /* DECPEX -- Printer extent (IGNORED) */
1553 case 42: /* DECNRCM -- National characters (IGNORED) */
1554 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1556 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1557 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1559 case 1000: /* 1000,1002: enable xterm mouse report */
1560 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1561 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1564 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1565 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1568 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1570 case 1049: /* = 1047 and 1048 */
1573 alt
= IS_SET(MODE_ALTSCREEN
);
1575 tclearregion(0, 0, term
.col
-1,
1578 if(set
^ alt
) /* set is always 1 or 0 */
1585 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1589 "erresc: unknown private set/reset mode %d\n",
1595 case 0: /* Error (IGNORED) */
1597 case 2: /* KAM -- keyboard action */
1598 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1600 case 4: /* IRM -- Insertion-replacement */
1601 MODBIT(term
.mode
, set
, MODE_INSERT
);
1603 case 12: /* SRM -- Send/Receive */
1604 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1606 case 20: /* LNM -- Linefeed/new line */
1607 MODBIT(term
.mode
, set
, MODE_CRLF
);
1611 "erresc: unknown set/reset mode %d\n",
1623 switch(csiescseq
.mode
) {
1626 fprintf(stderr
, "erresc: unknown csi ");
1630 case '@': /* ICH -- Insert <n> blank char */
1631 DEFAULT(csiescseq
.arg
[0], 1);
1632 tinsertblank(csiescseq
.arg
[0]);
1634 case 'A': /* CUU -- Cursor <n> Up */
1635 DEFAULT(csiescseq
.arg
[0], 1);
1636 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1638 case 'B': /* CUD -- Cursor <n> Down */
1639 case 'e': /* VPR --Cursor <n> Down */
1640 DEFAULT(csiescseq
.arg
[0], 1);
1641 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1643 case 'c': /* DA -- Device Attributes */
1644 if(csiescseq
.arg
[0] == 0)
1645 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1647 case 'C': /* CUF -- Cursor <n> Forward */
1648 case 'a': /* HPR -- Cursor <n> Forward */
1649 DEFAULT(csiescseq
.arg
[0], 1);
1650 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1652 case 'D': /* CUB -- Cursor <n> Backward */
1653 DEFAULT(csiescseq
.arg
[0], 1);
1654 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1656 case 'E': /* CNL -- Cursor <n> Down and first col */
1657 DEFAULT(csiescseq
.arg
[0], 1);
1658 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1660 case 'F': /* CPL -- Cursor <n> Up and first col */
1661 DEFAULT(csiescseq
.arg
[0], 1);
1662 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1664 case 'g': /* TBC -- Tabulation clear */
1665 switch (csiescseq
.arg
[0]) {
1666 case 0: /* clear current tab stop */
1667 term
.tabs
[term
.c
.x
] = 0;
1669 case 3: /* clear all the tabs */
1670 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1676 case 'G': /* CHA -- Move to <col> */
1678 DEFAULT(csiescseq
.arg
[0], 1);
1679 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1681 case 'H': /* CUP -- Move to <row> <col> */
1683 DEFAULT(csiescseq
.arg
[0], 1);
1684 DEFAULT(csiescseq
.arg
[1], 1);
1685 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1687 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1688 DEFAULT(csiescseq
.arg
[0], 1);
1689 while(csiescseq
.arg
[0]--)
1692 case 'J': /* ED -- Clear screen */
1694 switch(csiescseq
.arg
[0]) {
1696 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1697 if(term
.c
.y
< term
.row
-1) {
1698 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1704 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1, 1);
1705 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1708 tclearregion(0, 0, term
.col
-1, term
.row
-1, 1);
1714 case 'K': /* EL -- Clear line */
1715 switch(csiescseq
.arg
[0]) {
1717 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1721 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1724 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1728 case 'S': /* SU -- Scroll <n> line up */
1729 DEFAULT(csiescseq
.arg
[0], 1);
1730 tscrollup(term
.top
, csiescseq
.arg
[0]);
1732 case 'T': /* SD -- Scroll <n> line down */
1733 DEFAULT(csiescseq
.arg
[0], 1);
1734 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1736 case 'L': /* IL -- Insert <n> blank lines */
1737 DEFAULT(csiescseq
.arg
[0], 1);
1738 tinsertblankline(csiescseq
.arg
[0]);
1740 case 'l': /* RM -- Reset Mode */
1741 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1743 case 'M': /* DL -- Delete <n> lines */
1744 DEFAULT(csiescseq
.arg
[0], 1);
1745 tdeleteline(csiescseq
.arg
[0]);
1747 case 'X': /* ECH -- Erase <n> char */
1748 DEFAULT(csiescseq
.arg
[0], 1);
1749 tclearregion(term
.c
.x
, term
.c
.y
,
1750 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
, 1);
1752 case 'P': /* DCH -- Delete <n> char */
1753 DEFAULT(csiescseq
.arg
[0], 1);
1754 tdeletechar(csiescseq
.arg
[0]);
1756 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1757 DEFAULT(csiescseq
.arg
[0], 1);
1758 while(csiescseq
.arg
[0]--)
1761 case 'd': /* VPA -- Move to <row> */
1762 DEFAULT(csiescseq
.arg
[0], 1);
1763 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1765 case 'h': /* SM -- Set terminal mode */
1766 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1768 case 'm': /* SGR -- Terminal attribute (color) */
1769 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1771 case 'r': /* DECSTBM -- Set Scrolling Region */
1772 if(csiescseq
.priv
) {
1775 DEFAULT(csiescseq
.arg
[0], 1);
1776 DEFAULT(csiescseq
.arg
[1], term
.row
);
1777 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1781 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1782 tcursor(CURSOR_SAVE
);
1784 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1785 tcursor(CURSOR_LOAD
);
1796 for(i
= 0; i
< csiescseq
.len
; i
++) {
1797 c
= csiescseq
.buf
[i
] & 0xff;
1800 } else if(c
== '\n') {
1802 } else if(c
== '\r') {
1804 } else if(c
== 0x1b) {
1807 printf("(%02x)", c
);
1815 memset(&csiescseq
, 0, sizeof(csiescseq
));
1823 * TODO: make this being useful in case of color palette change.
1829 switch(strescseq
.type
) {
1830 case ']': /* OSC -- Operating System Command */
1836 * TODO: Handle special chars in string, like umlauts.
1839 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1843 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1845 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1848 fprintf(stderr
, "erresc: unknown str ");
1853 case 'k': /* old title set compatibility */
1854 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1856 case 'P': /* DSC -- Device Control String */
1857 case '_': /* APC -- Application Program Command */
1858 case '^': /* PM -- Privacy Message */
1860 fprintf(stderr
, "erresc: unknown str ");
1870 * TODO: Implement parsing like for CSI when required.
1871 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1881 printf("ESC%c", strescseq
.type
);
1882 for(i
= 0; i
< strescseq
.len
; i
++) {
1883 c
= strescseq
.buf
[i
] & 0xff;
1886 } else if(c
== '\n') {
1888 } else if(c
== '\r') {
1890 } else if(c
== 0x1b) {
1893 printf("(%02x)", c
);
1901 memset(&strescseq
, 0, sizeof(strescseq
));
1905 tputtab(bool forward
) {
1911 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1916 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1919 tmoveto(x
, term
.c
.y
);
1923 techo(char *buf
, int len
) {
1924 for(; len
> 0; buf
++, len
--) {
1927 if(c
== '\033') { /* escape */
1930 } else if (c
< '\x20') { /* control code */
1931 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
1945 tputc(char *c
, int len
) {
1947 bool control
= ascii
< '\x20' || ascii
== 0177;
1950 if (xwrite(iofd
, c
, len
) < 0) {
1951 fprintf(stderr
, "Error writing in %s:%s\n",
1952 opt_io
, strerror(errno
));
1959 * STR sequences must be checked before anything else
1960 * because it can use some control codes as part of the sequence.
1962 if(term
.esc
& ESC_STR
) {
1965 term
.esc
= ESC_START
| ESC_STR_END
;
1967 case '\a': /* backwards compatibility to xterm */
1972 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
)) {
1973 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
1974 strescseq
.len
+= len
;
1977 * Here is a bug in terminals. If the user never sends
1978 * some code to stop the str or esc command, then st
1979 * will stop responding. But this is better than
1980 * silently failing with unknown characters. At least
1981 * then users will report back.
1983 * In the case users ever get fixed, here is the code:
1995 * Actions of control codes must be performed as soon they arrive
1996 * because they can be embedded inside a control sequence, and
1997 * they must not cause conflicts with sequences.
2005 tmoveto(term
.c
.x
-1, term
.c
.y
);
2008 tmoveto(0, term
.c
.y
);
2013 /* go to first col if the mode is set */
2014 tnewline(IS_SET(MODE_CRLF
));
2016 case '\a': /* BEL */
2017 if(!(xw
.state
& WIN_FOCUSED
))
2020 case '\033': /* ESC */
2022 term
.esc
= ESC_START
;
2024 case '\016': /* SO */
2025 case '\017': /* SI */
2027 * Different charsets are hard to handle. Applications
2028 * should use the right alt charset escapes for the
2029 * only reason they still exist: line drawing. The
2030 * rest is incompatible history st should not support.
2033 case '\032': /* SUB */
2034 case '\030': /* CAN */
2037 case '\005': /* ENQ (IGNORED) */
2038 case '\000': /* NUL (IGNORED) */
2039 case '\021': /* XON (IGNORED) */
2040 case '\023': /* XOFF (IGNORED) */
2041 case 0177: /* DEL (IGNORED) */
2044 } else if(term
.esc
& ESC_START
) {
2045 if(term
.esc
& ESC_CSI
) {
2046 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2047 if(BETWEEN(ascii
, 0x40, 0x7E)
2048 || csiescseq
.len
>= ESC_BUF_SIZ
) {
2050 csiparse(), csihandle();
2052 } else if(term
.esc
& ESC_STR_END
) {
2056 } else if(term
.esc
& ESC_ALTCHARSET
) {
2058 case '0': /* Line drawing set */
2059 term
.c
.attr
.mode
|= ATTR_GFX
;
2061 case 'B': /* USASCII */
2062 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2064 case 'A': /* UK (IGNORED) */
2065 case '<': /* multinational charset (IGNORED) */
2066 case '5': /* Finnish (IGNORED) */
2067 case 'C': /* Finnish (IGNORED) */
2068 case 'K': /* German (IGNORED) */
2071 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2074 } else if(term
.esc
& ESC_TEST
) {
2075 if(ascii
== '8') { /* DEC screen alignment test. */
2076 char E
[UTF_SIZ
] = "E";
2079 for(x
= 0; x
< term
.col
; ++x
) {
2080 for(y
= 0; y
< term
.row
; ++y
)
2081 tsetchar(E
, &term
.c
.attr
, x
, y
);
2088 term
.esc
|= ESC_CSI
;
2091 term
.esc
|= ESC_TEST
;
2093 case 'P': /* DCS -- Device Control String */
2094 case '_': /* APC -- Application Program Command */
2095 case '^': /* PM -- Privacy Message */
2096 case ']': /* OSC -- Operating System Command */
2097 case 'k': /* old title set compatibility */
2099 strescseq
.type
= ascii
;
2100 term
.esc
|= ESC_STR
;
2102 case '(': /* set primary charset G0 */
2103 term
.esc
|= ESC_ALTCHARSET
;
2105 case ')': /* set secondary charset G1 (IGNORED) */
2106 case '*': /* set tertiary charset G2 (IGNORED) */
2107 case '+': /* set quaternary charset G3 (IGNORED) */
2110 case 'D': /* IND -- Linefeed */
2111 if(term
.c
.y
== term
.bot
) {
2112 tscrollup(term
.top
, 1);
2114 tmoveto(term
.c
.x
, term
.c
.y
+1);
2118 case 'E': /* NEL -- Next line */
2119 tnewline(1); /* always go to first col */
2122 case 'H': /* HTS -- Horizontal tab stop */
2123 term
.tabs
[term
.c
.x
] = 1;
2126 case 'M': /* RI -- Reverse index */
2127 if(term
.c
.y
== term
.top
) {
2128 tscrolldown(term
.top
, 1);
2130 tmoveto(term
.c
.x
, term
.c
.y
-1);
2134 case 'Z': /* DECID -- Identify Terminal */
2135 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2138 case 'c': /* RIS -- Reset to inital state */
2143 case '=': /* DECPAM -- Application keypad */
2144 term
.mode
|= MODE_APPKEYPAD
;
2147 case '>': /* DECPNM -- Normal keypad */
2148 term
.mode
&= ~MODE_APPKEYPAD
;
2151 case '7': /* DECSC -- Save Cursor */
2152 tcursor(CURSOR_SAVE
);
2155 case '8': /* DECRC -- Restore Cursor */
2156 tcursor(CURSOR_LOAD
);
2159 case '\\': /* ST -- Stop */
2163 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2164 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2169 * All characters which form part of a sequence are not
2175 * Display control codes only if we are in graphic mode
2177 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2179 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2181 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2182 tnewline(1); /* always go to first col */
2184 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2185 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2186 &term
.line
[term
.c
.y
][term
.c
.x
],
2187 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2190 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2191 if(term
.c
.x
+1 < term
.col
) {
2192 tmoveto(term
.c
.x
+1, term
.c
.y
);
2194 term
.c
.state
|= CURSOR_WRAPNEXT
;
2199 tresize(int col
, int row
) {
2201 int minrow
= MIN(row
, term
.row
);
2202 int mincol
= MIN(col
, term
.col
);
2203 int slide
= term
.c
.y
- row
+ 1;
2206 if(col
< 1 || row
< 1)
2209 /* free unneeded rows */
2213 * slide screen to keep cursor where we expect it -
2214 * tscrollup would work here, but we can optimize to
2215 * memmove because we're freeing the earlier lines
2217 for(/* i = 0 */; i
< slide
; i
++) {
2221 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2222 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2224 for(i
+= row
; i
< term
.row
; i
++) {
2229 /* resize to new height */
2230 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2231 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2232 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2233 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2235 /* resize each row to new width, zero-pad if needed */
2236 for(i
= 0; i
< minrow
; i
++) {
2238 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2239 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2240 for(x
= mincol
; x
< col
; x
++) {
2241 term
.line
[i
][x
].state
= 0;
2242 term
.alt
[i
][x
].state
= 0;
2246 /* allocate any new rows */
2247 for(/* i == minrow */; i
< row
; i
++) {
2249 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2250 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2252 if(col
> term
.col
) {
2253 bp
= term
.tabs
+ term
.col
;
2255 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2256 while(--bp
> term
.tabs
&& !*bp
)
2258 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2261 /* update terminal size */
2264 /* reset scrolling region */
2265 tsetscroll(0, row
-1);
2266 /* make use of the LIMIT in tmoveto */
2267 tmoveto(term
.c
.x
, term
.c
.y
);
2273 xresize(int col
, int row
) {
2274 xw
.tw
= MAX(1, col
* xw
.cw
);
2275 xw
.th
= MAX(1, row
* xw
.ch
);
2278 XFreePixmap(xw
.dpy
, xw
.buf
);
2279 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2280 DefaultDepth(xw
.dpy
, xw
.scr
));
2281 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
].pixel
);
2282 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2285 XftDrawChange(xw
.draw
, xw
.buf
);
2291 XRenderColor color
= { .alpha
= 0xffff };
2293 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2294 for(i
= 0; i
< LEN(colorname
); i
++) {
2297 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2298 die("Could not allocate color '%s'\n", colorname
[i
]);
2302 /* load colors [16-255] ; same colors as xterm */
2303 for(i
= 16, r
= 0; r
< 6; r
++) {
2304 for(g
= 0; g
< 6; g
++) {
2305 for(b
= 0; b
< 6; b
++) {
2306 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2307 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2308 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2309 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2310 die("Could not allocate color %d\n", i
);
2317 for(r
= 0; r
< 24; r
++, i
++) {
2318 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2319 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2321 die("Could not allocate color %d\n", i
);
2327 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2328 XftDrawRect(xw
.draw
,
2329 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2330 borderpx
+ col1
* xw
.cw
,
2331 borderpx
+ row1
* xw
.ch
,
2332 (col2
-col1
+1) * xw
.cw
,
2333 (row2
-row1
+1) * xw
.ch
);
2337 * Absolute coordinates.
2340 xclear(int x1
, int y1
, int x2
, int y2
) {
2341 XftDrawRect(xw
.draw
,
2342 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2343 x1
, y1
, x2
-x1
, y2
-y1
);
2348 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2349 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2350 XSizeHints
*sizeh
= NULL
;
2352 sizeh
= XAllocSizeHints();
2353 if(xw
.isfixed
== False
) {
2354 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2355 sizeh
->height
= xw
.h
;
2356 sizeh
->width
= xw
.w
;
2357 sizeh
->height_inc
= xw
.ch
;
2358 sizeh
->width_inc
= xw
.cw
;
2359 sizeh
->base_height
= 2 * borderpx
;
2360 sizeh
->base_width
= 2 * borderpx
;
2362 sizeh
->flags
= PMaxSize
| PMinSize
;
2363 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2364 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2367 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2372 xloadfont(Font
*f
, FcPattern
*pattern
) {
2376 match
= FcFontMatch(NULL
, pattern
, &result
);
2380 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2381 FcPatternDestroy(match
);
2385 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2386 FcPatternDestroy(match
);
2390 f
->pattern
= FcPatternDuplicate(pattern
);
2392 f
->ascent
= f
->match
->ascent
;
2393 f
->descent
= f
->match
->descent
;
2395 f
->rbearing
= f
->match
->max_advance_width
;
2397 f
->height
= f
->match
->height
;
2398 f
->width
= f
->lbearing
+ f
->rbearing
;
2404 xloadfonts(char *fontstr
, int fontsize
) {
2409 if(fontstr
[0] == '-') {
2410 pattern
= XftXlfdParse(fontstr
, False
, False
);
2412 pattern
= FcNameParse((FcChar8
*)fontstr
);
2416 die("st: can't open font %s\n", fontstr
);
2419 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2420 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2421 usedfontsize
= fontsize
;
2423 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2424 if(result
== FcResultMatch
) {
2425 usedfontsize
= (int)fontval
;
2428 * Default font size is 12, if none given. This is to
2429 * have a known usedfontsize value.
2431 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2436 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2437 FcDefaultSubstitute(pattern
);
2439 if(xloadfont(&dc
.font
, pattern
))
2440 die("st: can't open font %s\n", fontstr
);
2442 /* Setting character width and height. */
2443 xw
.cw
= dc
.font
.width
;
2444 xw
.ch
= dc
.font
.height
;
2446 FcPatternDel(pattern
, FC_SLANT
);
2447 FcPatternDel(pattern
, FC_WEIGHT
);
2448 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2449 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2450 if(xloadfont(&dc
.bfont
, pattern
))
2451 die("st: can't open font %s\n", fontstr
);
2453 FcPatternDel(pattern
, FC_SLANT
);
2454 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2455 if(xloadfont(&dc
.ibfont
, pattern
))
2456 die("st: can't open font %s\n", fontstr
);
2458 FcPatternDel(pattern
, FC_WEIGHT
);
2459 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_MEDIUM
);
2460 if(xloadfont(&dc
.ifont
, pattern
))
2461 die("st: can't open font %s\n", fontstr
);
2463 FcPatternDestroy(pattern
);
2467 xunloadfonts(void) {
2471 * Free the loaded fonts in the font cache. This is done backwards
2474 for (i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2477 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2482 XftFontClose(xw
.dpy
, dc
.font
.match
);
2483 FcPatternDestroy(dc
.font
.pattern
);
2484 FcFontSetDestroy(dc
.font
.set
);
2485 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2486 FcPatternDestroy(dc
.bfont
.pattern
);
2487 FcFontSetDestroy(dc
.bfont
.set
);
2488 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2489 FcPatternDestroy(dc
.ifont
.pattern
);
2490 FcFontSetDestroy(dc
.ifont
.set
);
2491 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2492 FcPatternDestroy(dc
.ibfont
.pattern
);
2493 FcFontSetDestroy(dc
.ibfont
.set
);
2497 xzoom(const Arg
*arg
) {
2499 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2506 XSetWindowAttributes attrs
;
2510 int sw
, sh
, major
, minor
;
2512 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2513 die("Can't open display\n");
2514 xw
.scr
= XDefaultScreen(xw
.dpy
);
2515 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2519 die("Could not init fontconfig.\n");
2521 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2522 xloadfonts(usedfont
, 0);
2525 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2528 /* adjust fixed window geometry */
2530 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2531 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2533 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2535 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2540 /* window - default size */
2541 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2542 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2548 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2549 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2550 attrs
.bit_gravity
= NorthWestGravity
;
2551 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2552 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2553 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2554 attrs
.colormap
= xw
.cmap
;
2556 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2557 XRootWindow(xw
.dpy
, xw
.scr
);
2558 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2559 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2561 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2565 /* double buffering */
2567 if(XdbeQueryExtension(xw.dpy, &major, &minor)) {
2568 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win,
2573 memset(&gcvalues
, 0, sizeof(gcvalues
));
2574 gcvalues
.graphics_exposures
= False
;
2575 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2577 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2578 DefaultDepth(xw
.dpy
, xw
.scr
));
2579 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2580 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2586 /* Xft rendering context */
2587 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2590 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2591 XSetLocaleModifiers("@im=local");
2592 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2593 XSetLocaleModifiers("@im=");
2594 if((xw
.xim
= XOpenIM(xw
.dpy
,
2595 NULL
, NULL
, NULL
)) == NULL
) {
2596 die("XOpenIM failed. Could not open input"
2601 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2602 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2603 XNFocusWindow
, xw
.win
, NULL
);
2605 die("XCreateIC failed. Could not obtain input method.\n");
2607 /* white cursor, black outline */
2608 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2609 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2610 XRecolorCursor(xw
.dpy
, cursor
,
2611 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2612 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2614 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2615 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2616 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2619 XMapWindow(xw
.dpy
, xw
.win
);
2625 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2626 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2627 width
= charlen
* xw
.cw
, xp
, i
;
2629 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2632 Font
*font
= &dc
.font
;
2634 FcPattern
*fcpattern
, *fontpattern
;
2635 FcFontSet
*fcsets
[] = { NULL
};
2636 FcCharSet
*fccharset
;
2637 Colour
*fg
= &dc
.col
[base
.fg
], *bg
= &dc
.col
[base
.bg
],
2638 *temp
, revfg
, revbg
;
2639 XRenderColor colfg
, colbg
;
2641 frcflags
= FRC_NORMAL
;
2643 if(base
.mode
& ATTR_BOLD
) {
2644 if(BETWEEN(base
.fg
, 0, 7)) {
2645 /* basic system colors */
2646 fg
= &dc
.col
[base
.fg
+ 8];
2647 } else if(BETWEEN(base
.fg
, 16, 195)) {
2649 fg
= &dc
.col
[base
.fg
+ 36];
2650 } else if(BETWEEN(base
.fg
, 232, 251)) {
2652 fg
= &dc
.col
[base
.fg
+ 4];
2655 * Those ranges will not be brightened:
2656 * 8 - 15 – bright system colors
2657 * 196 - 231 – highest 256 color cube
2658 * 252 - 255 – brightest colors in greyscale
2661 frcflags
= FRC_BOLD
;
2664 if(base
.mode
& ATTR_ITALIC
) {
2666 frcflags
= FRC_ITALIC
;
2668 if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2670 frcflags
= FRC_ITALICBOLD
;
2673 if(IS_SET(MODE_REVERSE
)) {
2674 if(fg
== &dc
.col
[defaultfg
]) {
2675 fg
= &dc
.col
[defaultbg
];
2677 colfg
.red
= ~fg
->color
.red
;
2678 colfg
.green
= ~fg
->color
.green
;
2679 colfg
.blue
= ~fg
->color
.blue
;
2680 colfg
.alpha
= fg
->color
.alpha
;
2681 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2685 if(bg
== &dc
.col
[defaultbg
]) {
2686 bg
= &dc
.col
[defaultfg
];
2688 colbg
.red
= ~bg
->color
.red
;
2689 colbg
.green
= ~bg
->color
.green
;
2690 colbg
.blue
= ~bg
->color
.blue
;
2691 colbg
.alpha
= bg
->color
.alpha
;
2692 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2697 if(base
.mode
& ATTR_REVERSE
) {
2703 /* Intelligent cleaning up of the borders. */
2705 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2706 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2708 if(x
+ charlen
>= term
.col
) {
2709 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2710 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2713 xclear(winx
, 0, winx
+ width
, borderpx
);
2715 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2717 /* Clean up the region we want to draw to. */
2718 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2720 fcsets
[0] = font
->set
;
2721 for (xp
= winx
; bytelen
> 0;) {
2723 * Search for the range in the to be printed string of glyphs
2724 * that are in the main font. Then print that range. If
2725 * some glyph is found that is not in the font, do the
2733 u8cblen
= utf8decode(s
, &u8char
);
2737 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2738 if (!doesexist
|| bytelen
<= 0) {
2747 XftDrawStringUtf8(xw
.draw
, fg
,
2749 winy
+ font
->ascent
,
2752 xp
+= font
->width
* u8fl
;
2764 /* Search the font cache. */
2765 for (i
= 0; i
< frclen
; i
++, frp
--) {
2769 if (frc
[frp
].c
== u8char
2770 && frc
[frp
].flags
== frcflags
) {
2775 /* Nothing was found. */
2778 * Nothing was found in the cache. Now use
2779 * some dozen of Fontconfig calls to get the
2780 * font for one single character.
2782 fcpattern
= FcPatternDuplicate(font
->pattern
);
2783 fccharset
= FcCharSetCreate();
2785 FcCharSetAddChar(fccharset
, u8char
);
2786 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2788 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2791 FcConfigSubstitute(0, fcpattern
,
2793 FcDefaultSubstitute(fcpattern
);
2795 fontpattern
= FcFontSetMatch(0, fcsets
,
2796 FcTrue
, fcpattern
, &fcres
);
2799 * Overwrite or create the new cache entry
2804 if (frccur
>= LEN(frc
))
2806 if (frclen
> LEN(frc
)) {
2808 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2811 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2813 frc
[frccur
].c
= u8char
;
2814 frc
[frccur
].flags
= frcflags
;
2816 FcPatternDestroy(fcpattern
);
2817 FcCharSetDestroy(fccharset
);
2822 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2823 xp
, winy
+ frc
[frp
].font
->ascent
,
2824 (FcChar8
*)u8c
, u8cblen
);
2830 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2831 winy + font->ascent, (FcChar8 *)s, bytelen);
2834 if(base
.mode
& ATTR_UNDERLINE
) {
2835 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2842 static int oldx
= 0, oldy
= 0;
2844 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
, 0};
2846 LIMIT(oldx
, 0, term
.col
-1);
2847 LIMIT(oldy
, 0, term
.row
-1);
2849 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2850 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2852 /* remove the old cursor */
2853 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2854 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2855 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2858 xtermclear(oldx
, oldy
, oldx
, oldy
);
2861 /* draw the new one */
2862 if(!(IS_SET(MODE_HIDE
))) {
2863 if(!(xw
.state
& WIN_FOCUSED
))
2866 if(IS_SET(MODE_REVERSE
))
2867 g
.mode
|= ATTR_REVERSE
, g
.fg
= defaultcs
, g
.bg
= defaultfg
;
2870 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2871 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2877 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2881 redraw(int timeout
) {
2882 struct timespec tv
= {0, timeout
* 1000};
2888 nanosleep(&tv
, NULL
);
2889 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2895 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2897 drawregion(0, 0, term
.col
, term
.row
);
2899 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2901 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
2903 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
].pixel
);
2908 drawregion(int x1
, int y1
, int x2
, int y2
) {
2909 int ic
, ib
, x
, y
, ox
, sl
;
2911 char buf
[DRAW_BUF_SIZ
];
2912 bool ena_sel
= sel
.bx
!= -1;
2914 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
2917 if(!(xw
.state
& WIN_VISIBLE
))
2920 for(y
= y1
; y
< y2
; y
++) {
2924 xtermclear(0, y
, term
.col
, y
);
2926 base
= term
.line
[y
][0];
2928 for(x
= x1
; x
< x2
; x
++) {
2929 new = term
.line
[y
][x
];
2930 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2931 new.mode
^= ATTR_REVERSE
;
2932 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2933 || ATTRCMP(base
, new)
2934 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2935 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2938 if(new.state
& GLYPH_SET
) {
2944 sl
= utf8size(new.c
);
2945 memcpy(buf
+ib
, new.c
, sl
);
2951 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2957 expose(XEvent
*ev
) {
2958 XExposeEvent
*e
= &ev
->xexpose
;
2960 if(xw
.state
& WIN_REDRAW
) {
2962 xw
.state
&= ~WIN_REDRAW
;
2968 visibility(XEvent
*ev
) {
2969 XVisibilityEvent
*e
= &ev
->xvisibility
;
2971 if(e
->state
== VisibilityFullyObscured
) {
2972 xw
.state
&= ~WIN_VISIBLE
;
2973 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2974 /* need a full redraw for next Expose, not just a buf copy */
2975 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2981 xw
.state
&= ~WIN_VISIBLE
;
2985 xseturgency(int add
) {
2986 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2988 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2989 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2995 XFocusChangeEvent
*e
= &ev
->xfocus
;
2997 if(e
->mode
== NotifyGrab
)
3000 if(ev
->type
== FocusIn
) {
3001 XSetICFocus(xw
.xic
);
3002 xw
.state
|= WIN_FOCUSED
;
3005 XUnsetICFocus(xw
.xic
);
3006 xw
.state
&= ~WIN_FOCUSED
;
3011 match(uint mask
, uint state
) {
3012 state
&= ~(ignoremod
);
3014 if(mask
== XK_NO_MOD
&& state
)
3016 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3018 if((state
& mask
) != state
)
3024 numlock(const Arg
*dummy
) {
3029 kmap(KeySym k
, uint state
) {
3034 /* Check for mapped keys out of X11 function keys. */
3035 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3036 if(mappedkeys
[i
] == k
)
3039 if(i
== LEN(mappedkeys
)) {
3040 if((k
& 0xFFFF) < 0xFD00)
3044 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3050 if(!match(mask
, state
))
3053 if(kp
->appkey
> 0) {
3054 if(!IS_SET(MODE_APPKEYPAD
))
3056 if(term
.numlock
&& kp
->appkey
== 2)
3058 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3062 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3064 && !IS_SET(MODE_APPCURSOR
))) {
3068 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3069 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3080 kpress(XEvent
*ev
) {
3081 XKeyEvent
*e
= &ev
->xkey
;
3083 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3088 if (IS_SET(MODE_KBDLOCK
))
3091 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3092 e
->state
&= ~Mod2Mask
;
3094 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3095 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3096 bp
->func(&(bp
->arg
));
3101 /* 2. custom keys from config.h */
3102 if((customkey
= kmap(ksym
, e
->state
))) {
3103 len
= strlen(customkey
);
3104 memcpy(buf
, customkey
, len
);
3105 /* 2. hardcoded (overrides X lookup) */
3110 if (len
== 1 && e
->state
& Mod1Mask
)
3113 memcpy(cp
, xstr
, len
);
3114 len
= cp
- buf
+ len
;
3118 if(IS_SET(MODE_ECHO
))
3124 cmessage(XEvent
*e
) {
3127 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3129 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3130 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3131 xw
.state
|= WIN_FOCUSED
;
3133 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3134 xw
.state
&= ~WIN_FOCUSED
;
3136 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3137 /* Send SIGHUP to shell */
3144 cresize(int width
, int height
) {
3152 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3153 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3162 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3165 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3172 int xfd
= XConnectionNumber(xw
.dpy
), xev
;
3173 struct timeval drawtimeout
, *tv
= NULL
, now
, last
;
3175 gettimeofday(&last
, NULL
);
3177 for(xev
= actionfps
;;) {
3179 FD_SET(cmdfd
, &rfd
);
3181 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3184 die("select failed: %s\n", SERRNO
);
3187 gettimeofday(&now
, NULL
);
3188 drawtimeout
.tv_sec
= 0;
3189 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3192 if(FD_ISSET(cmdfd
, &rfd
))
3195 if(FD_ISSET(xfd
, &rfd
))
3198 if(TIMEDIFF(now
, last
) > \
3199 (xev
? (1000/xfps
) : (1000/actionfps
))) {
3200 while(XPending(xw
.dpy
)) {
3201 XNextEvent(xw
.dpy
, &ev
);
3202 if(XFilterEvent(&ev
, None
))
3204 if(handler
[ev
.type
])
3205 (handler
[ev
.type
])(&ev
);
3212 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3214 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
))
3221 main(int argc
, char *argv
[]) {
3222 int i
, bitm
, xr
, yr
;
3225 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3228 for(i
= 1; i
< argc
; i
++) {
3229 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
3232 opt_class
= argv
[i
];
3235 /* eat all remaining arguments */
3247 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
3252 if(bitm
& WidthValue
)
3254 if(bitm
& HeightValue
)
3256 if(bitm
& XNegative
&& xw
.fx
== 0)
3258 if(bitm
& XNegative
&& xw
.fy
== 0)
3261 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3270 opt_title
= argv
[i
];
3277 opt_embed
= argv
[i
];
3283 setlocale(LC_CTYPE
, "");
3284 XSetLocaleModifiers("");