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
65 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
68 #define SERRNO strerror(errno)
69 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #define MAX(a, b) ((a) < (b) ? (b) : (a))
71 #define LEN(a) (sizeof(a) / sizeof(a[0]))
72 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
73 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
74 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
75 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
76 #define IS_SET(flag) ((term.mode & (flag)) != 0)
77 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
79 #define VT102ID "\033[?6c"
81 enum glyph_attribute
{
91 enum cursor_movement
{
114 MODE_MOUSEMOTION
= 64,
120 MODE_APPCURSOR
= 2048
126 ESC_STR
= 4, /* DSC, OSC, PM, APC */
128 ESC_STR_END
= 16, /* a final string was encountered */
129 ESC_TEST
= 32, /* Enter in test mode */
140 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
142 typedef unsigned char uchar
;
143 typedef unsigned int uint
;
144 typedef unsigned long ulong
;
145 typedef unsigned short ushort
;
148 char c
[UTF_SIZ
]; /* character code */
149 uchar mode
; /* attribute flags */
150 ushort fg
; /* foreground */
151 ushort bg
; /* background */
152 uchar state
; /* state flags */
158 Glyph attr
; /* current char attributes */
164 /* CSI Escape sequence structs */
165 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
167 char buf
[ESC_BUF_SIZ
]; /* raw string */
168 int len
; /* raw string length */
170 int arg
[ESC_ARG_SIZ
];
171 int narg
; /* nb of args */
175 /* STR Escape sequence structs */
176 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
178 char type
; /* ESC type ... */
179 char buf
[STR_BUF_SIZ
]; /* raw string */
180 int len
; /* raw string length */
181 char *args
[STR_ARG_SIZ
];
182 int narg
; /* nb of args */
185 /* Internal representation of the screen */
187 int row
; /* nb row */
188 int col
; /* nb col */
189 Line
*line
; /* screen */
190 Line
*alt
; /* alternate screen */
191 bool *dirty
; /* dirtyness of lines */
192 TCursor c
; /* cursor */
193 int top
; /* top scroll limit */
194 int bot
; /* bottom scroll limit */
195 int mode
; /* terminal mode flags */
196 int esc
; /* escape state flags */
197 bool numlock
; /* lock numbers in keyboard */
201 /* Purely graphic info */
207 Atom xembed
, wmdeletewin
;
213 bool isfixed
; /* is fixed geometry? */
214 int fx
, fy
, fw
, fh
; /* fixed geometry */
215 int tw
, th
; /* tty width and height */
216 int w
, h
; /* window width and height */
217 int ch
; /* char height */
218 int cw
; /* char width */
219 char state
; /* focus, redraw, visible */
226 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
227 signed char appkey
; /* application keypad */
228 signed char appcursor
; /* application cursor */
229 signed char crlf
; /* crlf mode */
232 /* TODO: use better name for vars... */
243 struct timeval tclick1
;
244 struct timeval tclick2
;
257 void (*func
)(const Arg
*);
261 /* function definitions used in config.h */
262 static void xzoom(const Arg
*);
263 static void selpaste(const Arg
*);
264 static void numlock(const Arg
*);
266 /* Config.h for applying patches and the configuration. */
282 /* Drawing Context */
284 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
285 Font font
, bfont
, ifont
, ibfont
;
289 static void die(const char *, ...);
290 static void draw(void);
291 static void redraw(int);
292 static void drawregion(int, int, int, int);
293 static void execsh(void);
294 static void sigchld(int);
295 static void run(void);
297 static void csidump(void);
298 static void csihandle(void);
299 static void csiparse(void);
300 static void csireset(void);
301 static void strdump(void);
302 static void strhandle(void);
303 static void strparse(void);
304 static void strreset(void);
306 static void tclearregion(int, int, int, int, int);
307 static void tcursor(int);
308 static void tdeletechar(int);
309 static void tdeleteline(int);
310 static void tinsertblank(int);
311 static void tinsertblankline(int);
312 static void tmoveto(int, int);
313 static void tmoveato(int x
, int y
);
314 static void tnew(int, int);
315 static void tnewline(int);
316 static void tputtab(bool);
317 static void tputc(char *, int);
318 static void treset(void);
319 static int tresize(int, int);
320 static void tscrollup(int, int);
321 static void tscrolldown(int, int);
322 static void tsetattr(int*, int);
323 static void tsetchar(char *, Glyph
*, int, int);
324 static void tsetscroll(int, int);
325 static void tswapscreen(void);
326 static void tsetdirt(int, int);
327 static void tsetmode(bool, bool, int *, int);
328 static void tfulldirt(void);
329 static void techo(char *, int);
331 static inline bool match(uint
, uint
);
332 static void ttynew(void);
333 static void ttyread(void);
334 static void ttyresize(void);
335 static void ttywrite(const char *, size_t);
337 static void xdraws(char *, Glyph
, int, int, int, int);
338 static void xhints(void);
339 static void xclear(int, int, int, int);
340 static void xdrawcursor(void);
341 static void xinit(void);
342 static void xloadcols(void);
343 static int xloadfont(Font
*, FcPattern
*);
344 static void xloadfonts(char *, int);
345 static void xresettitle(void);
346 static void xseturgency(int);
347 static void xsetsel(char*);
348 static void xtermclear(int, int, int, int);
349 static void xunloadfonts(void);
350 static void xresize(int, int);
352 static void expose(XEvent
*);
353 static void visibility(XEvent
*);
354 static void unmap(XEvent
*);
355 static char *kmap(KeySym
, uint
);
356 static void kpress(XEvent
*);
357 static void cmessage(XEvent
*);
358 static void cresize(int, int);
359 static void resize(XEvent
*);
360 static void focus(XEvent
*);
361 static void brelease(XEvent
*);
362 static void bpress(XEvent
*);
363 static void bmotion(XEvent
*);
364 static void selnotify(XEvent
*);
365 static void selclear(XEvent
*);
366 static void selrequest(XEvent
*);
368 static void selinit(void);
369 static inline bool selected(int, int);
370 static void selcopy(void);
371 static void selscroll(int, int);
373 static int utf8decode(char *, long *);
374 static int utf8encode(long *, char *);
375 static int utf8size(char *);
376 static int isfullutf8(char *, int);
378 static ssize_t
xwrite(int, char *, size_t);
379 static void *xmalloc(size_t);
380 static void *xrealloc(void *, size_t);
381 static void *xcalloc(size_t, size_t);
383 static void (*handler
[LASTEvent
])(XEvent
*) = {
385 [ClientMessage
] = cmessage
,
386 [ConfigureNotify
] = resize
,
387 [VisibilityNotify
] = visibility
,
388 [UnmapNotify
] = unmap
,
392 [MotionNotify
] = bmotion
,
393 [ButtonPress
] = bpress
,
394 [ButtonRelease
] = brelease
,
395 [SelectionClear
] = selclear
,
396 [SelectionNotify
] = selnotify
,
397 [SelectionRequest
] = selrequest
,
404 static CSIEscape csiescseq
;
405 static STREscape strescseq
;
408 static Selection sel
;
409 static int iofd
= -1;
410 static char **opt_cmd
= NULL
;
411 static char *opt_io
= NULL
;
412 static char *opt_title
= NULL
;
413 static char *opt_embed
= NULL
;
414 static char *opt_class
= NULL
;
415 static char *opt_font
= NULL
;
419 static char *usedfont
= NULL
;
420 static int usedfontsize
= 0;
422 /* Font Ring Cache */
437 * Fontcache is a ring buffer, with frccur as current position and frclen as
438 * the current length of used elements.
441 static Fontcache frc
[1024];
442 static int frccur
= -1, frclen
= 0;
445 xwrite(int fd
, char *s
, size_t len
) {
449 ssize_t r
= write(fd
, s
, len
);
459 xmalloc(size_t len
) {
460 void *p
= malloc(len
);
463 die("Out of memory\n");
469 xrealloc(void *p
, size_t len
) {
470 if((p
= realloc(p
, len
)) == NULL
)
471 die("Out of memory\n");
477 xcalloc(size_t nmemb
, size_t size
) {
478 void *p
= calloc(nmemb
, size
);
481 die("Out of memory\n");
487 utf8decode(char *s
, long *u
) {
493 if(~c
& B7
) { /* 0xxxxxxx */
496 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
497 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
499 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
500 *u
= c
&(B3
|B2
|B1
|B0
);
502 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
509 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
511 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
514 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
517 if((n
== 1 && *u
< 0x80) ||
518 (n
== 2 && *u
< 0x800) ||
519 (n
== 3 && *u
< 0x10000) ||
520 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
532 utf8encode(long *u
, char *s
) {
540 *sp
= uc
; /* 0xxxxxxx */
542 } else if(*u
< 0x800) {
543 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
545 } else if(uc
< 0x10000) {
546 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
548 } else if(uc
<= 0x10FFFF) {
549 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
555 for(i
=n
,++sp
; i
>0; --i
,++sp
)
556 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
568 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
569 UTF-8 otherwise return 0 */
571 isfullutf8(char *s
, int b
) {
579 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
581 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
583 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
585 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
587 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
588 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
601 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
603 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
612 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
613 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
617 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
618 if(sel
.xtarget
== None
)
619 sel
.xtarget
= XA_STRING
;
627 return LIMIT(x
, 0, term
.col
-1);
635 return LIMIT(y
, 0, term
.row
-1);
639 selected(int x
, int y
) {
642 if(sel
.ey
== y
&& sel
.by
== y
) {
643 bx
= MIN(sel
.bx
, sel
.ex
);
644 ex
= MAX(sel
.bx
, sel
.ex
);
645 return BETWEEN(x
, bx
, ex
);
648 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
649 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
650 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
651 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
655 getbuttoninfo(XEvent
*e
) {
656 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
658 sel
.ex
= x2col(e
->xbutton
.x
);
659 sel
.ey
= y2row(e
->xbutton
.y
);
661 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
662 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
663 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
664 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
668 mousereport(XEvent
*e
) {
669 int x
= x2col(e
->xbutton
.x
);
670 int y
= y2row(e
->xbutton
.y
);
671 int button
= e
->xbutton
.button
;
672 int state
= e
->xbutton
.state
;
673 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
674 static int ob
, ox
, oy
;
677 if(e
->xbutton
.type
== MotionNotify
) {
678 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
682 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
688 if(e
->xbutton
.type
== ButtonPress
) {
694 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
695 + (state
& Mod4Mask
? 8 : 0)
696 + (state
& ControlMask
? 16 : 0);
698 ttywrite(buf
, sizeof(buf
));
703 if(IS_SET(MODE_MOUSE
)) {
705 } else if(e
->xbutton
.button
== Button1
) {
708 tsetdirt(sel
.b
.y
, sel
.e
.y
);
712 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
713 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
714 } else if(e
->xbutton
.button
== Button4
) {
716 } else if(e
->xbutton
.button
== Button5
) {
724 int x
, y
, bufsize
, is_selected
= 0, size
;
730 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
731 ptr
= str
= xmalloc(bufsize
);
733 /* append every set & selected glyph to the selection */
734 for(y
= 0; y
< term
.row
; y
++) {
735 gp
= &term
.line
[y
][0];
736 last
= gp
+ term
.col
;
738 while(--last
>= gp
&& !(last
->state
& GLYPH_SET
))
741 for(x
= 0; gp
<= last
; x
++, ++gp
) {
742 if(!(is_selected
= selected(x
, y
)))
745 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
747 memcpy(ptr
, p
, size
);
750 /* \n at the end of every selected line except for the last one */
751 if(is_selected
&& y
< sel
.e
.y
)
760 selnotify(XEvent
*e
) {
761 ulong nitems
, ofs
, rem
;
768 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
769 False
, AnyPropertyType
, &type
, &format
,
770 &nitems
, &rem
, &data
)) {
771 fprintf(stderr
, "Clipboard allocation failed\n");
774 ttywrite((const char *) data
, nitems
* format
/ 8);
776 /* number of 32-bit chunks returned */
777 ofs
+= nitems
* format
/ 32;
782 selpaste(const Arg
*dummy
) {
783 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
784 xw
.win
, CurrentTime
);
787 void selclear(XEvent
*e
) {
791 tsetdirt(sel
.b
.y
, sel
.e
.y
);
795 selrequest(XEvent
*e
) {
796 fprintf(stderr
, "selrequest\n");
797 XSelectionRequestEvent
*xsre
;
799 Atom xa_targets
, string
;
801 xsre
= (XSelectionRequestEvent
*) e
;
802 xev
.type
= SelectionNotify
;
803 xev
.requestor
= xsre
->requestor
;
804 xev
.selection
= xsre
->selection
;
805 xev
.target
= xsre
->target
;
806 xev
.time
= xsre
->time
;
810 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
811 if(xsre
->target
== xa_targets
) {
812 /* respond with the supported type */
813 string
= sel
.xtarget
;
814 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
815 XA_ATOM
, 32, PropModeReplace
,
816 (uchar
*) &string
, 1);
817 xev
.property
= xsre
->property
;
818 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
819 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
820 xsre
->target
, 8, PropModeReplace
,
821 (uchar
*) sel
.clip
, strlen(sel
.clip
));
822 xev
.property
= xsre
->property
;
825 /* all done, send a notification to the listener */
826 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
827 fprintf(stderr
, "Error sending SelectionNotify event\n");
832 fprintf(stderr
, "xsetsel: %s\n", str
);
833 /* register the selection for both the clipboard and the primary */
839 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
841 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
842 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
846 brelease(XEvent
*e
) {
849 if(IS_SET(MODE_MOUSE
)) {
854 if(e
->xbutton
.button
== Button2
) {
856 } else if(e
->xbutton
.button
== Button1
) {
859 term
.dirty
[sel
.ey
] = 1;
860 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
862 gettimeofday(&now
, NULL
);
864 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
865 /* triple click on the line */
866 sel
.b
.x
= sel
.bx
= 0;
867 sel
.e
.x
= sel
.ex
= term
.col
;
868 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
870 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
871 /* double click to select word */
873 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
874 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
878 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
879 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
883 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
891 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
892 gettimeofday(&sel
.tclick1
, NULL
);
897 int starty
, endy
, oldey
, oldex
;
899 if(IS_SET(MODE_MOUSE
)) {
911 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
912 starty
= MIN(oldey
, sel
.ey
);
913 endy
= MAX(oldey
, sel
.ey
);
914 tsetdirt(starty
, endy
);
919 die(const char *errstr
, ...) {
922 va_start(ap
, errstr
);
923 vfprintf(stderr
, errstr
, ap
);
931 char *envshell
= getenv("SHELL");
932 const struct passwd
*pass
= getpwuid(getuid());
933 char buf
[sizeof(long) * 8 + 1];
940 setenv("LOGNAME", pass
->pw_name
, 1);
941 setenv("USER", pass
->pw_name
, 1);
942 setenv("SHELL", pass
->pw_shell
, 0);
943 setenv("HOME", pass
->pw_dir
, 0);
946 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
947 setenv("WINDOWID", buf
, 1);
949 signal(SIGCHLD
, SIG_DFL
);
950 signal(SIGHUP
, SIG_DFL
);
951 signal(SIGINT
, SIG_DFL
);
952 signal(SIGQUIT
, SIG_DFL
);
953 signal(SIGTERM
, SIG_DFL
);
954 signal(SIGALRM
, SIG_DFL
);
956 DEFAULT(envshell
, shell
);
957 setenv("TERM", termname
, 1);
958 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
959 execvp(args
[0], args
);
967 if(waitpid(pid
, &stat
, 0) < 0)
968 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
970 if(WIFEXITED(stat
)) {
971 exit(WEXITSTATUS(stat
));
980 struct winsize w
= {term
.row
, term
.col
, 0, 0};
982 /* seems to work fine on linux, openbsd and freebsd */
983 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
984 die("openpty failed: %s\n", SERRNO
);
986 switch(pid
= fork()) {
988 die("fork failed\n");
991 setsid(); /* create a new process group */
992 dup2(s
, STDIN_FILENO
);
993 dup2(s
, STDOUT_FILENO
);
994 dup2(s
, STDERR_FILENO
);
995 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
996 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1004 signal(SIGCHLD
, sigchld
);
1006 iofd
= (!strcmp(opt_io
, "-")) ?
1008 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1010 fprintf(stderr
, "Error opening %s:%s\n",
1011 opt_io
, strerror(errno
));
1021 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1023 fprintf(stderr
, "\n");
1028 static char buf
[BUFSIZ
];
1029 static int buflen
= 0;
1032 int charsize
; /* size of utf8 char in bytes */
1036 /* append read bytes to unprocessed bytes */
1037 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1038 die("Couldn't read from shell: %s\n", SERRNO
);
1040 /* process every complete utf8 char */
1043 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1044 charsize
= utf8decode(ptr
, &utf8c
);
1045 utf8encode(&utf8c
, s
);
1051 /* keep any uncomplete utf8 char for the next call */
1052 memmove(buf
, ptr
, buflen
);
1056 ttywrite(const char *s
, size_t n
) {
1057 if(write(cmdfd
, s
, n
) == -1)
1058 die("write error on tty: %s\n", SERRNO
);
1065 w
.ws_row
= term
.row
;
1066 w
.ws_col
= term
.col
;
1067 w
.ws_xpixel
= xw
.tw
;
1068 w
.ws_ypixel
= xw
.th
;
1069 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1070 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1074 tsetdirt(int top
, int bot
) {
1077 LIMIT(top
, 0, term
.row
-1);
1078 LIMIT(bot
, 0, term
.row
-1);
1080 for(i
= top
; i
<= bot
; i
++)
1086 tsetdirt(0, term
.row
-1);
1093 if(mode
== CURSOR_SAVE
) {
1095 } else if(mode
== CURSOR_LOAD
) {
1105 term
.c
= (TCursor
){{
1109 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1111 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1112 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1115 term
.bot
= term
.row
- 1;
1116 term
.mode
= MODE_WRAP
;
1118 tclearregion(0, 0, term
.col
-1, term
.row
-1, 0);
1120 tcursor(CURSOR_SAVE
);
1124 tnew(int col
, int row
) {
1125 /* set screen size */
1128 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1129 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1130 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1131 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1133 for(row
= 0; row
< term
.row
; row
++) {
1134 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1135 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1136 term
.dirty
[row
] = 0;
1140 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1147 Line
*tmp
= term
.line
;
1149 term
.line
= term
.alt
;
1151 term
.mode
^= MODE_ALTSCREEN
;
1156 tscrolldown(int orig
, int n
) {
1160 LIMIT(n
, 0, term
.bot
-orig
+1);
1162 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
, 0);
1164 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1165 temp
= term
.line
[i
];
1166 term
.line
[i
] = term
.line
[i
-n
];
1167 term
.line
[i
-n
] = temp
;
1170 term
.dirty
[i
-n
] = 1;
1177 tscrollup(int orig
, int n
) {
1180 LIMIT(n
, 0, term
.bot
-orig
+1);
1182 tclearregion(0, orig
, term
.col
-1, orig
+n
-1, 0);
1184 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1185 temp
= term
.line
[i
];
1186 term
.line
[i
] = term
.line
[i
+n
];
1187 term
.line
[i
+n
] = temp
;
1190 term
.dirty
[i
+n
] = 1;
1193 selscroll(orig
, -n
);
1197 selscroll(int orig
, int n
) {
1201 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1202 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1206 if(sel
.by
< term
.top
) {
1210 if(sel
.ey
> term
.bot
) {
1214 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1215 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1220 tnewline(int first_col
) {
1224 tscrollup(term
.top
, 1);
1228 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1233 /* int noarg = 1; */
1234 char *p
= csiescseq
.buf
;
1238 csiescseq
.priv
= 1, p
++;
1240 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1241 while(isdigit(*p
)) {
1242 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1243 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1245 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1246 csiescseq
.narg
++, p
++;
1248 csiescseq
.mode
= *p
;
1256 /* for absolute user moves, when decom is set */
1258 tmoveato(int x
, int y
) {
1259 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1263 tmoveto(int x
, int y
) {
1266 if(term
.c
.state
& CURSOR_ORIGIN
) {
1271 maxy
= term
.row
- 1;
1273 LIMIT(x
, 0, term
.col
-1);
1274 LIMIT(y
, miny
, maxy
);
1275 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1281 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1282 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1283 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1284 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1285 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1286 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1287 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1288 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1289 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1290 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1294 * The table is proudly stolen from rxvt.
1296 if(attr
->mode
& ATTR_GFX
) {
1297 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1298 && vt100_0
[c
[0] - 0x41]) {
1299 c
= vt100_0
[c
[0] - 0x41];
1304 term
.line
[y
][x
] = *attr
;
1305 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1306 term
.line
[y
][x
].state
|= GLYPH_SET
;
1310 tclearregion(int x1
, int y1
, int x2
, int y2
, int bce
) {
1314 temp
= x1
, x1
= x2
, x2
= temp
;
1316 temp
= y1
, y1
= y2
, y2
= temp
;
1318 LIMIT(x1
, 0, term
.col
-1);
1319 LIMIT(x2
, 0, term
.col
-1);
1320 LIMIT(y1
, 0, term
.row
-1);
1321 LIMIT(y2
, 0, term
.row
-1);
1323 for(y
= y1
; y
<= y2
; y
++) {
1325 for(x
= x1
; x
<= x2
; x
++) {
1327 term
.line
[y
][x
] = term
.c
.attr
;
1328 memcpy(term
.line
[y
][x
].c
, " ", 2);
1329 term
.line
[y
][x
].state
|= GLYPH_SET
;
1331 term
.line
[y
][x
].state
= 0;
1338 tdeletechar(int n
) {
1339 int src
= term
.c
.x
+ n
;
1341 int size
= term
.col
- src
;
1343 term
.dirty
[term
.c
.y
] = 1;
1345 if(src
>= term
.col
) {
1346 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1350 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1351 size
* sizeof(Glyph
));
1352 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1356 tinsertblank(int n
) {
1359 int size
= term
.col
- dst
;
1361 term
.dirty
[term
.c
.y
] = 1;
1363 if(dst
>= term
.col
) {
1364 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1368 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1369 size
* sizeof(Glyph
));
1370 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
, 0);
1374 tinsertblankline(int n
) {
1375 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1378 tscrolldown(term
.c
.y
, n
);
1382 tdeleteline(int n
) {
1383 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1386 tscrollup(term
.c
.y
, n
);
1390 tsetattr(int *attr
, int l
) {
1393 for(i
= 0; i
< l
; i
++) {
1396 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1397 | ATTR_ITALIC
| ATTR_BLINK
);
1398 term
.c
.attr
.fg
= defaultfg
;
1399 term
.c
.attr
.bg
= defaultbg
;
1402 term
.c
.attr
.mode
|= ATTR_BOLD
;
1405 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1408 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1411 term
.c
.attr
.mode
|= ATTR_BLINK
;
1414 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1418 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1421 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1424 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1427 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1430 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1433 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1435 if(BETWEEN(attr
[i
], 0, 255)) {
1436 term
.c
.attr
.fg
= attr
[i
];
1439 "erresc: bad fgcolor %d\n",
1444 "erresc(38): gfx attr %d unknown\n",
1449 term
.c
.attr
.fg
= defaultfg
;
1452 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1454 if(BETWEEN(attr
[i
], 0, 255)) {
1455 term
.c
.attr
.bg
= attr
[i
];
1458 "erresc: bad bgcolor %d\n",
1463 "erresc(48): gfx attr %d unknown\n",
1468 term
.c
.attr
.bg
= defaultbg
;
1471 if(BETWEEN(attr
[i
], 30, 37)) {
1472 term
.c
.attr
.fg
= attr
[i
] - 30;
1473 } else if(BETWEEN(attr
[i
], 40, 47)) {
1474 term
.c
.attr
.bg
= attr
[i
] - 40;
1475 } else if(BETWEEN(attr
[i
], 90, 97)) {
1476 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1477 } else if(BETWEEN(attr
[i
], 100, 107)) {
1478 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1481 "erresc(default): gfx attr %d unknown\n",
1482 attr
[i
]), csidump();
1490 tsetscroll(int t
, int b
) {
1493 LIMIT(t
, 0, term
.row
-1);
1494 LIMIT(b
, 0, term
.row
-1);
1504 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1507 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1511 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1515 case 1: /* DECCKM -- Cursor key */
1516 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1518 case 5: /* DECSCNM -- Reverse video */
1520 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1521 if(mode
!= term
.mode
)
1522 redraw(REDRAW_TIMEOUT
);
1524 case 6: /* DECOM -- Origin */
1525 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1528 case 7: /* DECAWM -- Auto wrap */
1529 MODBIT(term
.mode
, set
, MODE_WRAP
);
1531 case 0: /* Error (IGNORED) */
1532 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1533 case 3: /* DECCOLM -- Column (IGNORED) */
1534 case 4: /* DECSCLM -- Scroll (IGNORED) */
1535 case 8: /* DECARM -- Auto repeat (IGNORED) */
1536 case 18: /* DECPFF -- Printer feed (IGNORED) */
1537 case 19: /* DECPEX -- Printer extent (IGNORED) */
1538 case 42: /* DECNRCM -- National characters (IGNORED) */
1539 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1541 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1542 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1544 case 1000: /* 1000,1002: enable xterm mouse report */
1545 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1548 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1550 case 1049: /* = 1047 and 1048 */
1553 alt
= IS_SET(MODE_ALTSCREEN
);
1555 tclearregion(0, 0, term
.col
-1,
1558 if(set
^ alt
) /* set is always 1 or 0 */
1565 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1569 "erresc: unknown private set/reset mode %d\n",
1575 case 0: /* Error (IGNORED) */
1577 case 2: /* KAM -- keyboard action */
1578 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1580 case 4: /* IRM -- Insertion-replacement */
1581 MODBIT(term
.mode
, set
, MODE_INSERT
);
1583 case 12: /* SRM -- Send/Receive */
1584 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1586 case 20: /* LNM -- Linefeed/new line */
1587 MODBIT(term
.mode
, set
, MODE_CRLF
);
1591 "erresc: unknown set/reset mode %d\n",
1603 switch(csiescseq
.mode
) {
1606 fprintf(stderr
, "erresc: unknown csi ");
1610 case '@': /* ICH -- Insert <n> blank char */
1611 DEFAULT(csiescseq
.arg
[0], 1);
1612 tinsertblank(csiescseq
.arg
[0]);
1614 case 'A': /* CUU -- Cursor <n> Up */
1615 DEFAULT(csiescseq
.arg
[0], 1);
1616 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1618 case 'B': /* CUD -- Cursor <n> Down */
1619 case 'e': /* VPR --Cursor <n> Down */
1620 DEFAULT(csiescseq
.arg
[0], 1);
1621 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1623 case 'c': /* DA -- Device Attributes */
1624 if(csiescseq
.arg
[0] == 0)
1625 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1627 case 'C': /* CUF -- Cursor <n> Forward */
1628 case 'a': /* HPR -- Cursor <n> Forward */
1629 DEFAULT(csiescseq
.arg
[0], 1);
1630 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1632 case 'D': /* CUB -- Cursor <n> Backward */
1633 DEFAULT(csiescseq
.arg
[0], 1);
1634 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1636 case 'E': /* CNL -- Cursor <n> Down and first col */
1637 DEFAULT(csiescseq
.arg
[0], 1);
1638 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1640 case 'F': /* CPL -- Cursor <n> Up and first col */
1641 DEFAULT(csiescseq
.arg
[0], 1);
1642 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1644 case 'g': /* TBC -- Tabulation clear */
1645 switch (csiescseq
.arg
[0]) {
1646 case 0: /* clear current tab stop */
1647 term
.tabs
[term
.c
.x
] = 0;
1649 case 3: /* clear all the tabs */
1650 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1656 case 'G': /* CHA -- Move to <col> */
1658 DEFAULT(csiescseq
.arg
[0], 1);
1659 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1661 case 'H': /* CUP -- Move to <row> <col> */
1663 DEFAULT(csiescseq
.arg
[0], 1);
1664 DEFAULT(csiescseq
.arg
[1], 1);
1665 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1667 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1668 DEFAULT(csiescseq
.arg
[0], 1);
1669 while(csiescseq
.arg
[0]--)
1672 case 'J': /* ED -- Clear screen */
1674 switch(csiescseq
.arg
[0]) {
1676 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1677 if(term
.c
.y
< term
.row
-1) {
1678 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1684 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1, 1);
1685 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1688 tclearregion(0, 0, term
.col
-1, term
.row
-1, 1);
1694 case 'K': /* EL -- Clear line */
1695 switch(csiescseq
.arg
[0]) {
1697 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1701 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1704 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1708 case 'S': /* SU -- Scroll <n> line up */
1709 DEFAULT(csiescseq
.arg
[0], 1);
1710 tscrollup(term
.top
, csiescseq
.arg
[0]);
1712 case 'T': /* SD -- Scroll <n> line down */
1713 DEFAULT(csiescseq
.arg
[0], 1);
1714 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1716 case 'L': /* IL -- Insert <n> blank lines */
1717 DEFAULT(csiescseq
.arg
[0], 1);
1718 tinsertblankline(csiescseq
.arg
[0]);
1720 case 'l': /* RM -- Reset Mode */
1721 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1723 case 'M': /* DL -- Delete <n> lines */
1724 DEFAULT(csiescseq
.arg
[0], 1);
1725 tdeleteline(csiescseq
.arg
[0]);
1727 case 'X': /* ECH -- Erase <n> char */
1728 DEFAULT(csiescseq
.arg
[0], 1);
1729 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0],
1732 case 'P': /* DCH -- Delete <n> char */
1733 DEFAULT(csiescseq
.arg
[0], 1);
1734 tdeletechar(csiescseq
.arg
[0]);
1736 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1737 DEFAULT(csiescseq
.arg
[0], 1);
1738 while(csiescseq
.arg
[0]--)
1741 case 'd': /* VPA -- Move to <row> */
1742 DEFAULT(csiescseq
.arg
[0], 1);
1743 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1745 case 'h': /* SM -- Set terminal mode */
1746 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1748 case 'm': /* SGR -- Terminal attribute (color) */
1749 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1751 case 'r': /* DECSTBM -- Set Scrolling Region */
1752 if(csiescseq
.priv
) {
1755 DEFAULT(csiescseq
.arg
[0], 1);
1756 DEFAULT(csiescseq
.arg
[1], term
.row
);
1757 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1761 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1762 tcursor(CURSOR_SAVE
);
1764 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1765 tcursor(CURSOR_LOAD
);
1776 for(i
= 0; i
< csiescseq
.len
; i
++) {
1777 c
= csiescseq
.buf
[i
] & 0xff;
1780 } else if(c
== '\n') {
1782 } else if(c
== '\r') {
1784 } else if(c
== 0x1b) {
1787 printf("(%02x)", c
);
1795 memset(&csiescseq
, 0, sizeof(csiescseq
));
1803 * TODO: make this being useful in case of color palette change.
1809 switch(strescseq
.type
) {
1810 case ']': /* OSC -- Operating System Command */
1816 * TODO: Handle special chars in string, like umlauts.
1819 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1823 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1825 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1828 fprintf(stderr
, "erresc: unknown str ");
1833 case 'k': /* old title set compatibility */
1834 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1836 case 'P': /* DSC -- Device Control String */
1837 case '_': /* APC -- Application Program Command */
1838 case '^': /* PM -- Privacy Message */
1840 fprintf(stderr
, "erresc: unknown str ");
1850 * TODO: Implement parsing like for CSI when required.
1851 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1861 printf("ESC%c", strescseq
.type
);
1862 for(i
= 0; i
< strescseq
.len
; i
++) {
1863 c
= strescseq
.buf
[i
] & 0xff;
1866 } else if(c
== '\n') {
1868 } else if(c
== '\r') {
1870 } else if(c
== 0x1b) {
1873 printf("(%02x)", c
);
1881 memset(&strescseq
, 0, sizeof(strescseq
));
1885 tputtab(bool forward
) {
1891 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1896 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1899 tmoveto(x
, term
.c
.y
);
1903 techo(char *buf
, int len
) {
1904 for(; len
> 0; buf
++, len
--) {
1907 if(c
== '\033') { /* escape */
1910 } else if (c
< '\x20') { /* control code */
1911 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
1925 tputc(char *c
, int len
) {
1927 bool control
= ascii
< '\x20' || ascii
== 0177;
1930 if (xwrite(iofd
, c
, len
) < 0) {
1931 fprintf(stderr
, "Error writing in %s:%s\n",
1932 opt_io
, strerror(errno
));
1939 * STR sequences must be checked before anything else
1940 * because it can use some control codes as part of the sequence.
1942 if(term
.esc
& ESC_STR
) {
1945 term
.esc
= ESC_START
| ESC_STR_END
;
1947 case '\a': /* backwards compatibility to xterm */
1952 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
)) {
1953 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
1954 strescseq
.len
+= len
;
1957 * Here is a bug in terminals. If the user never sends
1958 * some code to stop the str or esc command, then st
1959 * will stop responding. But this is better than
1960 * silently failing with unknown characters. At least
1961 * then users will report back.
1963 * In the case users ever get fixed, here is the code:
1975 * Actions of control codes must be performed as soon they arrive
1976 * because they can be embedded inside a control sequence, and
1977 * they must not cause conflicts with sequences.
1985 tmoveto(term
.c
.x
-1, term
.c
.y
);
1988 tmoveto(0, term
.c
.y
);
1993 /* go to first col if the mode is set */
1994 tnewline(IS_SET(MODE_CRLF
));
1996 case '\a': /* BEL */
1997 if(!(xw
.state
& WIN_FOCUSED
))
2000 case '\033': /* ESC */
2002 term
.esc
= ESC_START
;
2004 case '\016': /* SO */
2005 case '\017': /* SI */
2007 * Different charsets are hard to handle. Applications
2008 * should use the right alt charset escapes for the
2009 * only reason they still exist: line drawing. The
2010 * rest is incompatible history st should not support.
2013 case '\032': /* SUB */
2014 case '\030': /* CAN */
2017 case '\005': /* ENQ (IGNORED) */
2018 case '\000': /* NUL (IGNORED) */
2019 case '\021': /* XON (IGNORED) */
2020 case '\023': /* XOFF (IGNORED) */
2021 case 0177: /* DEL (IGNORED) */
2024 } else if(term
.esc
& ESC_START
) {
2025 if(term
.esc
& ESC_CSI
) {
2026 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2027 if(BETWEEN(ascii
, 0x40, 0x7E)
2028 || csiescseq
.len
>= ESC_BUF_SIZ
) {
2030 csiparse(), csihandle();
2032 } else if(term
.esc
& ESC_STR_END
) {
2036 } else if(term
.esc
& ESC_ALTCHARSET
) {
2038 case '0': /* Line drawing set */
2039 term
.c
.attr
.mode
|= ATTR_GFX
;
2041 case 'B': /* USASCII */
2042 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2044 case 'A': /* UK (IGNORED) */
2045 case '<': /* multinational charset (IGNORED) */
2046 case '5': /* Finnish (IGNORED) */
2047 case 'C': /* Finnish (IGNORED) */
2048 case 'K': /* German (IGNORED) */
2051 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2054 } else if(term
.esc
& ESC_TEST
) {
2055 if(ascii
== '8') { /* DEC screen alignment test. */
2056 char E
[UTF_SIZ
] = "E";
2059 for(x
= 0; x
< term
.col
; ++x
) {
2060 for(y
= 0; y
< term
.row
; ++y
)
2061 tsetchar(E
, &term
.c
.attr
, x
, y
);
2068 term
.esc
|= ESC_CSI
;
2071 term
.esc
|= ESC_TEST
;
2073 case 'P': /* DCS -- Device Control String */
2074 case '_': /* APC -- Application Program Command */
2075 case '^': /* PM -- Privacy Message */
2076 case ']': /* OSC -- Operating System Command */
2077 case 'k': /* old title set compatibility */
2079 strescseq
.type
= ascii
;
2080 term
.esc
|= ESC_STR
;
2082 case '(': /* set primary charset G0 */
2083 term
.esc
|= ESC_ALTCHARSET
;
2085 case ')': /* set secondary charset G1 (IGNORED) */
2086 case '*': /* set tertiary charset G2 (IGNORED) */
2087 case '+': /* set quaternary charset G3 (IGNORED) */
2090 case 'D': /* IND -- Linefeed */
2091 if(term
.c
.y
== term
.bot
) {
2092 tscrollup(term
.top
, 1);
2094 tmoveto(term
.c
.x
, term
.c
.y
+1);
2098 case 'E': /* NEL -- Next line */
2099 tnewline(1); /* always go to first col */
2102 case 'H': /* HTS -- Horizontal tab stop */
2103 term
.tabs
[term
.c
.x
] = 1;
2106 case 'M': /* RI -- Reverse index */
2107 if(term
.c
.y
== term
.top
) {
2108 tscrolldown(term
.top
, 1);
2110 tmoveto(term
.c
.x
, term
.c
.y
-1);
2114 case 'Z': /* DECID -- Identify Terminal */
2115 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2118 case 'c': /* RIS -- Reset to inital state */
2123 case '=': /* DECPAM -- Application keypad */
2124 term
.mode
|= MODE_APPKEYPAD
;
2127 case '>': /* DECPNM -- Normal keypad */
2128 term
.mode
&= ~MODE_APPKEYPAD
;
2131 case '7': /* DECSC -- Save Cursor */
2132 tcursor(CURSOR_SAVE
);
2135 case '8': /* DECRC -- Restore Cursor */
2136 tcursor(CURSOR_LOAD
);
2139 case '\\': /* ST -- Stop */
2143 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2144 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2149 * All characters which form part of a sequence are not
2155 * Display control codes only if we are in graphic mode
2157 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2159 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2161 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2162 tnewline(1); /* always go to first col */
2164 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2165 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2166 &term
.line
[term
.c
.y
][term
.c
.x
],
2167 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2170 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2171 if(term
.c
.x
+1 < term
.col
) {
2172 tmoveto(term
.c
.x
+1, term
.c
.y
);
2174 term
.c
.state
|= CURSOR_WRAPNEXT
;
2179 tresize(int col
, int row
) {
2181 int minrow
= MIN(row
, term
.row
);
2182 int mincol
= MIN(col
, term
.col
);
2183 int slide
= term
.c
.y
- row
+ 1;
2186 if(col
< 1 || row
< 1)
2189 /* free unneeded rows */
2192 /* slide screen to keep cursor where we expect it -
2193 * tscrollup would work here, but we can optimize to
2194 * memmove because we're freeing the earlier lines */
2195 for(/* i = 0 */; i
< slide
; i
++) {
2199 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2200 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2202 for(i
+= row
; i
< term
.row
; i
++) {
2207 /* resize to new height */
2208 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2209 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2210 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2211 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2213 /* resize each row to new width, zero-pad if needed */
2214 for(i
= 0; i
< minrow
; i
++) {
2216 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2217 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2218 for(x
= mincol
; x
< col
; x
++) {
2219 term
.line
[i
][x
].state
= 0;
2220 term
.alt
[i
][x
].state
= 0;
2224 /* allocate any new rows */
2225 for(/* i == minrow */; i
< row
; i
++) {
2227 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2228 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2230 if(col
> term
.col
) {
2231 bp
= term
.tabs
+ term
.col
;
2233 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2234 while(--bp
> term
.tabs
&& !*bp
)
2236 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2239 /* update terminal size */
2242 /* reset scrolling region */
2243 tsetscroll(0, row
-1);
2244 /* make use of the LIMIT in tmoveto */
2245 tmoveto(term
.c
.x
, term
.c
.y
);
2251 xresize(int col
, int row
) {
2252 xw
.tw
= MAX(1, col
* xw
.cw
);
2253 xw
.th
= MAX(1, row
* xw
.ch
);
2256 XFreePixmap(xw
.dpy
, xw
.buf
);
2257 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2258 DefaultDepth(xw
.dpy
, xw
.scr
));
2259 XSetForeground(xw
.dpy
, dc
.gc
, 0);
2260 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2263 XftDrawChange(xw
.draw
, xw
.buf
);
2269 XRenderColor color
= { .alpha
= 0 };
2271 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2272 for(i
= 0; i
< LEN(colorname
); i
++) {
2275 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2276 die("Could not allocate color '%s'\n", colorname
[i
]);
2280 /* load colors [16-255] ; same colors as xterm */
2281 for(i
= 16, r
= 0; r
< 6; r
++) {
2282 for(g
= 0; g
< 6; g
++) {
2283 for(b
= 0; b
< 6; b
++) {
2284 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2285 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2286 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2287 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2288 die("Could not allocate color %d\n", i
);
2295 for(r
= 0; r
< 24; r
++, i
++) {
2296 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2297 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2299 die("Could not allocate color %d\n", i
);
2305 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2306 XftDrawRect(xw
.draw
,
2307 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2308 borderpx
+ col1
* xw
.cw
,
2309 borderpx
+ row1
* xw
.ch
,
2310 (col2
-col1
+1) * xw
.cw
,
2311 (row2
-row1
+1) * xw
.ch
);
2315 * Absolute coordinates.
2318 xclear(int x1
, int y1
, int x2
, int y2
) {
2319 XftDrawRect(xw
.draw
,
2320 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2321 x1
, y1
, x2
-x1
, y2
-y1
);
2326 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2327 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2328 XSizeHints
*sizeh
= NULL
;
2330 sizeh
= XAllocSizeHints();
2331 if(xw
.isfixed
== False
) {
2332 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2333 sizeh
->height
= xw
.h
;
2334 sizeh
->width
= xw
.w
;
2335 sizeh
->height_inc
= xw
.ch
;
2336 sizeh
->width_inc
= xw
.cw
;
2337 sizeh
->base_height
= 2 * borderpx
;
2338 sizeh
->base_width
= 2 * borderpx
;
2340 sizeh
->flags
= PMaxSize
| PMinSize
;
2341 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2342 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2345 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2350 xloadfont(Font
*f
, FcPattern
*pattern
) {
2354 match
= FcFontMatch(NULL
, pattern
, &result
);
2358 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2359 FcPatternDestroy(match
);
2363 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2364 FcPatternDestroy(match
);
2368 f
->pattern
= FcPatternDuplicate(pattern
);
2370 f
->ascent
= f
->match
->ascent
;
2371 f
->descent
= f
->match
->descent
;
2373 f
->rbearing
= f
->match
->max_advance_width
;
2375 f
->height
= f
->match
->height
;
2376 f
->width
= f
->lbearing
+ f
->rbearing
;
2382 xloadfonts(char *fontstr
, int fontsize
) {
2387 if(fontstr
[0] == '-') {
2388 pattern
= XftXlfdParse(fontstr
, False
, False
);
2390 pattern
= FcNameParse((FcChar8
*)fontstr
);
2394 die("st: can't open font %s\n", fontstr
);
2397 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2398 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2399 usedfontsize
= fontsize
;
2401 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2402 if(result
== FcResultMatch
) {
2403 usedfontsize
= (int)fontval
;
2406 * Default font size is 12, if none given. This is to
2407 * have a known usedfontsize value.
2409 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2414 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2415 FcDefaultSubstitute(pattern
);
2417 if(xloadfont(&dc
.font
, pattern
))
2418 die("st: can't open font %s\n", fontstr
);
2420 /* Setting character width and height. */
2421 xw
.cw
= dc
.font
.width
;
2422 xw
.ch
= dc
.font
.height
;
2424 FcPatternDel(pattern
, FC_WEIGHT
);
2425 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2426 if(xloadfont(&dc
.bfont
, pattern
))
2427 die("st: can't open font %s\n", fontstr
);
2429 FcPatternDel(pattern
, FC_SLANT
);
2430 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2431 if(xloadfont(&dc
.ibfont
, pattern
))
2432 die("st: can't open font %s\n", fontstr
);
2434 FcPatternDel(pattern
, FC_WEIGHT
);
2435 if(xloadfont(&dc
.ifont
, pattern
))
2436 die("st: can't open font %s\n", fontstr
);
2438 FcPatternDestroy(pattern
);
2447 * Free the loaded fonts in the font cache. This is done backwards
2450 for (i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2453 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2458 XftFontClose(xw
.dpy
, dc
.font
.match
);
2459 FcPatternDestroy(dc
.font
.pattern
);
2460 FcFontSetDestroy(dc
.font
.set
);
2461 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2462 FcPatternDestroy(dc
.bfont
.pattern
);
2463 FcFontSetDestroy(dc
.bfont
.set
);
2464 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2465 FcPatternDestroy(dc
.ifont
.pattern
);
2466 FcFontSetDestroy(dc
.ifont
.set
);
2467 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2468 FcPatternDestroy(dc
.ibfont
.pattern
);
2469 FcFontSetDestroy(dc
.ibfont
.set
);
2473 xzoom(const Arg
*arg
)
2476 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2483 XSetWindowAttributes attrs
;
2487 int sw
, sh
, major
, minor
;
2489 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2490 die("Can't open display\n");
2491 xw
.scr
= XDefaultScreen(xw
.dpy
);
2492 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2496 die("Could not init fontconfig.\n");
2498 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2499 xloadfonts(usedfont
, 0);
2502 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2505 /* adjust fixed window geometry */
2507 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2508 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2510 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2512 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2517 /* window - default size */
2518 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2519 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2525 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2526 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2527 attrs
.bit_gravity
= NorthWestGravity
;
2528 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2529 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2530 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2531 attrs
.colormap
= xw
.cmap
;
2533 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2534 XRootWindow(xw
.dpy
, xw
.scr
);
2535 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2536 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2538 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2542 /* double buffering */
2544 if(XdbeQueryExtension(xw.dpy, &major, &minor)) {
2545 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win,
2550 memset(&gcvalues
, 0, sizeof(gcvalues
));
2551 gcvalues
.graphics_exposures
= False
;
2552 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2554 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2555 DefaultDepth(xw
.dpy
, xw
.scr
));
2556 XSetForeground(xw
.dpy
, dc
.gc
, 0);
2557 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2563 /* Xft rendering context */
2564 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2567 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2568 XSetLocaleModifiers("@im=local");
2569 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2570 XSetLocaleModifiers("@im=");
2571 if((xw
.xim
= XOpenIM(xw
.dpy
,
2572 NULL
, NULL
, NULL
)) == NULL
) {
2573 die("XOpenIM failed. Could not open input"
2578 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2579 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2580 XNFocusWindow
, xw
.win
, NULL
);
2582 die("XCreateIC failed. Could not obtain input method.\n");
2584 /* white cursor, black outline */
2585 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2586 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2587 XRecolorCursor(xw
.dpy
, cursor
,
2588 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2589 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2591 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2592 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2593 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2596 XMapWindow(xw
.dpy
, xw
.win
);
2602 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2603 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2604 width
= charlen
* xw
.cw
, xp
, i
;
2606 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2609 Font
*font
= &dc
.font
;
2611 FcPattern
*fcpattern
, *fontpattern
;
2612 FcFontSet
*fcsets
[] = { NULL
};
2613 FcCharSet
*fccharset
;
2614 Colour
*fg
= &dc
.col
[base
.fg
], *bg
= &dc
.col
[base
.bg
],
2615 *temp
, revfg
, revbg
;
2616 XRenderColor colfg
, colbg
;
2618 frcflags
= FRC_NORMAL
;
2620 if(base
.mode
& ATTR_BOLD
) {
2621 if(BETWEEN(base
.fg
, 0, 7)) {
2622 /* basic system colors */
2623 fg
= &dc
.col
[base
.fg
+ 8];
2624 } else if(BETWEEN(base
.fg
, 16, 195)) {
2626 fg
= &dc
.col
[base
.fg
+ 36];
2627 } else if(BETWEEN(base
.fg
, 232, 251)) {
2629 fg
= &dc
.col
[base
.fg
+ 4];
2632 * Those ranges will not be brightened:
2633 * 8 - 15 – bright system colors
2634 * 196 - 231 – highest 256 color cube
2635 * 252 - 255 – brightest colors in greyscale
2638 frcflags
= FRC_BOLD
;
2641 if(base
.mode
& ATTR_ITALIC
) {
2643 frcflags
= FRC_ITALIC
;
2645 if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2647 frcflags
= FRC_ITALICBOLD
;
2650 if(IS_SET(MODE_REVERSE
)) {
2651 if(fg
== &dc
.col
[defaultfg
]) {
2652 fg
= &dc
.col
[defaultbg
];
2654 colfg
.red
= ~fg
->color
.red
;
2655 colfg
.green
= ~fg
->color
.green
;
2656 colfg
.blue
= ~fg
->color
.blue
;
2657 colfg
.alpha
= fg
->color
.alpha
;
2658 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2662 if(bg
== &dc
.col
[defaultbg
]) {
2663 bg
= &dc
.col
[defaultfg
];
2665 colbg
.red
= ~bg
->color
.red
;
2666 colbg
.green
= ~bg
->color
.green
;
2667 colbg
.blue
= ~bg
->color
.blue
;
2668 colbg
.alpha
= bg
->color
.alpha
;
2669 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2674 if(base
.mode
& ATTR_REVERSE
) {
2680 /* Intelligent cleaning up of the borders. */
2682 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2683 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2685 if(x
+ charlen
>= term
.col
) {
2686 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2687 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2690 xclear(winx
, 0, winx
+ width
, borderpx
);
2692 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2694 /* Clean up the region we want to draw to. */
2695 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2697 fcsets
[0] = font
->set
;
2698 for (xp
= winx
; bytelen
> 0;) {
2700 * Search for the range in the to be printed string of glyphs
2701 * that are in the main font. Then print that range. If
2702 * some glyph is found that is not in the font, do the
2710 u8cblen
= utf8decode(s
, &u8char
);
2714 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2715 if (!doesexist
|| bytelen
<= 0) {
2724 XftDrawStringUtf8(xw
.draw
, fg
,
2726 winy
+ font
->ascent
,
2729 xp
+= font
->width
* u8fl
;
2741 /* Search the font cache. */
2742 for (i
= 0; i
< frclen
; i
++, frp
--) {
2746 if (frc
[frp
].c
== u8char
2747 && frc
[frp
].flags
== frcflags
) {
2752 /* Nothing was found. */
2755 * Nothing was found in the cache. Now use
2756 * some dozen of Fontconfig calls to get the
2757 * font for one single character.
2759 fcpattern
= FcPatternDuplicate(font
->pattern
);
2760 fccharset
= FcCharSetCreate();
2762 FcCharSetAddChar(fccharset
, u8char
);
2763 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2765 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2768 FcConfigSubstitute(0, fcpattern
,
2770 FcDefaultSubstitute(fcpattern
);
2772 fontpattern
= FcFontSetMatch(0, fcsets
,
2773 FcTrue
, fcpattern
, &fcres
);
2776 * Overwrite or create the new cache entry
2781 if (frccur
>= LEN(frc
))
2783 if (frclen
> LEN(frc
)) {
2785 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2788 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2790 frc
[frccur
].c
= u8char
;
2791 frc
[frccur
].flags
= frcflags
;
2793 FcPatternDestroy(fcpattern
);
2794 FcCharSetDestroy(fccharset
);
2799 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2800 xp
, winy
+ frc
[frp
].font
->ascent
,
2801 (FcChar8
*)u8c
, u8cblen
);
2807 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2808 winy + font->ascent, (FcChar8 *)s, bytelen);
2811 if(base
.mode
& ATTR_UNDERLINE
) {
2812 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2819 static int oldx
= 0, oldy
= 0;
2821 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
, 0};
2823 LIMIT(oldx
, 0, term
.col
-1);
2824 LIMIT(oldy
, 0, term
.row
-1);
2826 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2827 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2829 /* remove the old cursor */
2830 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2831 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2832 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2835 xtermclear(oldx
, oldy
, oldx
, oldy
);
2838 /* draw the new one */
2839 if(!(IS_SET(MODE_HIDE
))) {
2840 if(!(xw
.state
& WIN_FOCUSED
))
2843 if(IS_SET(MODE_REVERSE
))
2844 g
.mode
|= ATTR_REVERSE
, g
.fg
= defaultcs
, g
.bg
= defaultfg
;
2847 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2848 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2854 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2858 redraw(int timeout
) {
2859 struct timespec tv
= {0, timeout
* 1000};
2865 nanosleep(&tv
, NULL
);
2866 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2872 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2874 drawregion(0, 0, term
.col
, term
.row
);
2876 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2878 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
2880 XSetForeground(xw
.dpy
, dc
.gc
, 0);
2885 drawregion(int x1
, int y1
, int x2
, int y2
) {
2886 int ic
, ib
, x
, y
, ox
, sl
;
2888 char buf
[DRAW_BUF_SIZ
];
2889 bool ena_sel
= sel
.bx
!= -1;
2891 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
2894 if(!(xw
.state
& WIN_VISIBLE
))
2897 for(y
= y1
; y
< y2
; y
++) {
2901 xtermclear(0, y
, term
.col
, y
);
2903 base
= term
.line
[y
][0];
2905 for(x
= x1
; x
< x2
; x
++) {
2906 new = term
.line
[y
][x
];
2907 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2908 new.mode
^= ATTR_REVERSE
;
2909 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2910 || ATTRCMP(base
, new)
2911 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2912 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2915 if(new.state
& GLYPH_SET
) {
2921 sl
= utf8size(new.c
);
2922 memcpy(buf
+ib
, new.c
, sl
);
2928 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2934 expose(XEvent
*ev
) {
2935 XExposeEvent
*e
= &ev
->xexpose
;
2937 if(xw
.state
& WIN_REDRAW
) {
2939 xw
.state
&= ~WIN_REDRAW
;
2945 visibility(XEvent
*ev
) {
2946 XVisibilityEvent
*e
= &ev
->xvisibility
;
2948 if(e
->state
== VisibilityFullyObscured
) {
2949 xw
.state
&= ~WIN_VISIBLE
;
2950 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2951 /* need a full redraw for next Expose, not just a buf copy */
2952 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2958 xw
.state
&= ~WIN_VISIBLE
;
2962 xseturgency(int add
) {
2963 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2965 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2966 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2972 if(ev
->type
== FocusIn
) {
2973 XSetICFocus(xw
.xic
);
2974 xw
.state
|= WIN_FOCUSED
;
2977 XUnsetICFocus(xw
.xic
);
2978 xw
.state
&= ~WIN_FOCUSED
;
2983 match(uint mask
, uint state
) {
2984 if(mask
== XK_NO_MOD
&& state
)
2986 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
2988 if((state
& mask
) != state
)
2994 numlock(const Arg
*dummy
) {
2999 kmap(KeySym k
, uint state
) {
3004 /* Check for mapped keys out of X11 function keys. */
3005 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3006 if(mappedkeys
[i
] == k
)
3009 if(i
== LEN(mappedkeys
)) {
3010 if((k
& 0xFFFF) < 0xFD00)
3014 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3020 if(!match(mask
, state
))
3023 if(kp
->appkey
> 0) {
3024 if(!IS_SET(MODE_APPKEYPAD
))
3026 if(term
.numlock
&& kp
->appkey
== 2)
3028 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3032 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3034 && !IS_SET(MODE_APPCURSOR
))) {
3038 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3039 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3050 kpress(XEvent
*ev
) {
3051 XKeyEvent
*e
= &ev
->xkey
;
3053 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3058 if (IS_SET(MODE_KBDLOCK
))
3061 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3062 e
->state
&= ~Mod2Mask
;
3064 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3065 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3066 bp
->func(&(bp
->arg
));
3071 /* 2. custom keys from config.h */
3072 if((customkey
= kmap(ksym
, e
->state
))) {
3073 len
= strlen(customkey
);
3074 memcpy(buf
, customkey
, len
);
3075 /* 2. hardcoded (overrides X lookup) */
3080 if (len
== 1 && e
->state
& Mod1Mask
)
3083 memcpy(cp
, xstr
, len
);
3084 len
= cp
- buf
+ len
;
3088 if(IS_SET(MODE_ECHO
))
3094 cmessage(XEvent
*e
) {
3096 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
3097 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3098 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3099 xw
.state
|= WIN_FOCUSED
;
3101 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3102 xw
.state
&= ~WIN_FOCUSED
;
3104 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3105 /* Send SIGHUP to shell */
3112 cresize(int width
, int height
)
3121 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3122 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3131 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3134 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3141 int xfd
= XConnectionNumber(xw
.dpy
), i
;
3142 struct timeval drawtimeout
, *tv
= NULL
;
3146 FD_SET(cmdfd
, &rfd
);
3148 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3151 die("select failed: %s\n", SERRNO
);
3155 * Stop after a certain number of reads so the user does not
3156 * feel like the system is stuttering.
3158 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
3162 * Just wait a bit so it isn't disturbing the
3163 * user and the system is able to write something.
3165 drawtimeout
.tv_sec
= 0;
3166 drawtimeout
.tv_usec
= 5;
3173 while(XPending(xw
.dpy
)) {
3174 XNextEvent(xw
.dpy
, &ev
);
3175 if(XFilterEvent(&ev
, None
))
3177 if(handler
[ev
.type
])
3178 (handler
[ev
.type
])(&ev
);
3187 main(int argc
, char *argv
[]) {
3188 int i
, bitm
, xr
, yr
;
3191 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3194 for(i
= 1; i
< argc
; i
++) {
3195 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
3198 opt_class
= argv
[i
];
3201 /* eat all remaining arguments */
3213 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
3218 if(bitm
& WidthValue
)
3220 if(bitm
& HeightValue
)
3222 if(bitm
& XNegative
&& xw
.fx
== 0)
3224 if(bitm
& XNegative
&& xw
.fy
== 0)
3227 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3236 opt_title
= argv
[i
];
3243 opt_embed
= argv
[i
];
3249 setlocale(LC_CTYPE
, "");
3250 XSetLocaleModifiers("");