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,
121 MODE_MOUSESGR
= 4096,
127 ESC_STR
= 4, /* DSC, OSC, PM, APC */
129 ESC_STR_END
= 16, /* a final string was encountered */
130 ESC_TEST
= 32, /* Enter in test mode */
141 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
143 typedef unsigned char uchar
;
144 typedef unsigned int uint
;
145 typedef unsigned long ulong
;
146 typedef unsigned short ushort
;
149 char c
[UTF_SIZ
]; /* character code */
150 uchar mode
; /* attribute flags */
151 ushort fg
; /* foreground */
152 ushort bg
; /* background */
153 uchar state
; /* state flags */
159 Glyph attr
; /* current char attributes */
165 /* CSI Escape sequence structs */
166 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
168 char buf
[ESC_BUF_SIZ
]; /* raw string */
169 int len
; /* raw string length */
171 int arg
[ESC_ARG_SIZ
];
172 int narg
; /* nb of args */
176 /* STR Escape sequence structs */
177 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
179 char type
; /* ESC type ... */
180 char buf
[STR_BUF_SIZ
]; /* raw string */
181 int len
; /* raw string length */
182 char *args
[STR_ARG_SIZ
];
183 int narg
; /* nb of args */
186 /* Internal representation of the screen */
188 int row
; /* nb row */
189 int col
; /* nb col */
190 Line
*line
; /* screen */
191 Line
*alt
; /* alternate screen */
192 bool *dirty
; /* dirtyness of lines */
193 TCursor c
; /* cursor */
194 int top
; /* top scroll limit */
195 int bot
; /* bottom scroll limit */
196 int mode
; /* terminal mode flags */
197 int esc
; /* escape state flags */
198 bool numlock
; /* lock numbers in keyboard */
202 /* Purely graphic info */
208 Atom xembed
, wmdeletewin
;
214 bool isfixed
; /* is fixed geometry? */
215 int fx
, fy
, fw
, fh
; /* fixed geometry */
216 int tw
, th
; /* tty width and height */
217 int w
, h
; /* window width and height */
218 int ch
; /* char height */
219 int cw
; /* char width */
220 char state
; /* focus, redraw, visible */
227 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
228 signed char appkey
; /* application keypad */
229 signed char appcursor
; /* application cursor */
230 signed char crlf
; /* crlf mode */
233 /* TODO: use better name for vars... */
244 struct timeval tclick1
;
245 struct timeval tclick2
;
258 void (*func
)(const Arg
*);
262 /* function definitions used in config.h */
263 static void xzoom(const Arg
*);
264 static void selpaste(const Arg
*);
265 static void numlock(const Arg
*);
267 /* Config.h for applying patches and the configuration. */
283 /* Drawing Context */
285 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
286 Font font
, bfont
, ifont
, ibfont
;
290 static void die(const char *, ...);
291 static void draw(void);
292 static void redraw(int);
293 static void drawregion(int, int, int, int);
294 static void execsh(void);
295 static void sigchld(int);
296 static void run(void);
298 static void csidump(void);
299 static void csihandle(void);
300 static void csiparse(void);
301 static void csireset(void);
302 static void strdump(void);
303 static void strhandle(void);
304 static void strparse(void);
305 static void strreset(void);
307 static void tclearregion(int, int, int, int, int);
308 static void tcursor(int);
309 static void tdeletechar(int);
310 static void tdeleteline(int);
311 static void tinsertblank(int);
312 static void tinsertblankline(int);
313 static void tmoveto(int, int);
314 static void tmoveato(int x
, int y
);
315 static void tnew(int, int);
316 static void tnewline(int);
317 static void tputtab(bool);
318 static void tputc(char *, int);
319 static void treset(void);
320 static int tresize(int, int);
321 static void tscrollup(int, int);
322 static void tscrolldown(int, int);
323 static void tsetattr(int*, int);
324 static void tsetchar(char *, Glyph
*, int, int);
325 static void tsetscroll(int, int);
326 static void tswapscreen(void);
327 static void tsetdirt(int, int);
328 static void tsetmode(bool, bool, int *, int);
329 static void tfulldirt(void);
330 static void techo(char *, int);
332 static inline bool match(uint
, uint
);
333 static void ttynew(void);
334 static void ttyread(void);
335 static void ttyresize(void);
336 static void ttywrite(const char *, size_t);
338 static void xdraws(char *, Glyph
, int, int, int, int);
339 static void xhints(void);
340 static void xclear(int, int, int, int);
341 static void xdrawcursor(void);
342 static void xinit(void);
343 static void xloadcols(void);
344 static int xloadfont(Font
*, FcPattern
*);
345 static void xloadfonts(char *, int);
346 static void xresettitle(void);
347 static void xseturgency(int);
348 static void xsetsel(char*);
349 static void xtermclear(int, int, int, int);
350 static void xunloadfonts(void);
351 static void xresize(int, int);
353 static void expose(XEvent
*);
354 static void visibility(XEvent
*);
355 static void unmap(XEvent
*);
356 static char *kmap(KeySym
, uint
);
357 static void kpress(XEvent
*);
358 static void cmessage(XEvent
*);
359 static void cresize(int, int);
360 static void resize(XEvent
*);
361 static void focus(XEvent
*);
362 static void brelease(XEvent
*);
363 static void bpress(XEvent
*);
364 static void bmotion(XEvent
*);
365 static void selnotify(XEvent
*);
366 static void selclear(XEvent
*);
367 static void selrequest(XEvent
*);
369 static void selinit(void);
370 static inline bool selected(int, int);
371 static void selcopy(void);
372 static void selscroll(int, int);
374 static int utf8decode(char *, long *);
375 static int utf8encode(long *, char *);
376 static int utf8size(char *);
377 static int isfullutf8(char *, int);
379 static ssize_t
xwrite(int, char *, size_t);
380 static void *xmalloc(size_t);
381 static void *xrealloc(void *, size_t);
382 static void *xcalloc(size_t, size_t);
384 static void (*handler
[LASTEvent
])(XEvent
*) = {
386 [ClientMessage
] = cmessage
,
387 [ConfigureNotify
] = resize
,
388 [VisibilityNotify
] = visibility
,
389 [UnmapNotify
] = unmap
,
393 [MotionNotify
] = bmotion
,
394 [ButtonPress
] = bpress
,
395 [ButtonRelease
] = brelease
,
396 [SelectionClear
] = selclear
,
397 [SelectionNotify
] = selnotify
,
398 [SelectionRequest
] = selrequest
,
405 static CSIEscape csiescseq
;
406 static STREscape strescseq
;
409 static Selection sel
;
410 static int iofd
= -1;
411 static char **opt_cmd
= NULL
;
412 static char *opt_io
= NULL
;
413 static char *opt_title
= NULL
;
414 static char *opt_embed
= NULL
;
415 static char *opt_class
= NULL
;
416 static char *opt_font
= NULL
;
420 static char *usedfont
= NULL
;
421 static int usedfontsize
= 0;
423 /* Font Ring Cache */
438 * Fontcache is a ring buffer, with frccur as current position and frclen as
439 * the current length of used elements.
442 static Fontcache frc
[1024];
443 static int frccur
= -1, frclen
= 0;
446 xwrite(int fd
, char *s
, size_t len
) {
450 ssize_t r
= write(fd
, s
, len
);
460 xmalloc(size_t len
) {
461 void *p
= malloc(len
);
464 die("Out of memory\n");
470 xrealloc(void *p
, size_t len
) {
471 if((p
= realloc(p
, len
)) == NULL
)
472 die("Out of memory\n");
478 xcalloc(size_t nmemb
, size_t size
) {
479 void *p
= calloc(nmemb
, size
);
482 die("Out of memory\n");
488 utf8decode(char *s
, long *u
) {
494 if(~c
& B7
) { /* 0xxxxxxx */
497 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
498 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
500 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
501 *u
= c
&(B3
|B2
|B1
|B0
);
503 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
510 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
512 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
515 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
518 if((n
== 1 && *u
< 0x80) ||
519 (n
== 2 && *u
< 0x800) ||
520 (n
== 3 && *u
< 0x10000) ||
521 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
533 utf8encode(long *u
, char *s
) {
541 *sp
= uc
; /* 0xxxxxxx */
543 } else if(*u
< 0x800) {
544 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
546 } else if(uc
< 0x10000) {
547 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
549 } else if(uc
<= 0x10FFFF) {
550 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
556 for(i
=n
,++sp
; i
>0; --i
,++sp
)
557 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
569 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
570 UTF-8 otherwise return 0 */
572 isfullutf8(char *s
, int b
) {
580 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
582 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
584 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
586 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
588 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
589 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
602 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
604 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
613 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
614 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
618 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
619 if(sel
.xtarget
== None
)
620 sel
.xtarget
= XA_STRING
;
628 return LIMIT(x
, 0, term
.col
-1);
636 return LIMIT(y
, 0, term
.row
-1);
640 selected(int x
, int y
) {
643 if(sel
.ey
== y
&& sel
.by
== y
) {
644 bx
= MIN(sel
.bx
, sel
.ex
);
645 ex
= MAX(sel
.bx
, sel
.ex
);
646 return BETWEEN(x
, bx
, ex
);
649 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
650 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
651 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
652 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
656 getbuttoninfo(XEvent
*e
) {
657 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
659 sel
.ex
= x2col(e
->xbutton
.x
);
660 sel
.ey
= y2row(e
->xbutton
.y
);
662 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
663 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
664 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
665 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
669 mousereport(XEvent
*e
) {
670 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
671 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
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(!IS_SET(MODE_MOUSESGR
)
683 && (e
->xbutton
.type
== ButtonRelease
684 || button
== AnyButton
)) {
690 if(e
->xbutton
.type
== ButtonPress
) {
696 button
+= (state
& ShiftMask
? 4 : 0)
697 + (state
& Mod4Mask
? 8 : 0)
698 + (state
& ControlMask
? 16 : 0);
701 if(IS_SET(MODE_MOUSESGR
)) {
702 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
704 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
705 } else if(x
< 223 && y
< 223) {
706 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
707 32+button
, 32+x
+1, 32+y
+1);
717 if(IS_SET(MODE_MOUSE
)) {
719 } else if(e
->xbutton
.button
== Button1
) {
722 tsetdirt(sel
.b
.y
, sel
.e
.y
);
726 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
727 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
728 } else if(e
->xbutton
.button
== Button4
) {
730 } else if(e
->xbutton
.button
== Button5
) {
738 int x
, y
, bufsize
, is_selected
= 0, size
;
744 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
745 ptr
= str
= xmalloc(bufsize
);
747 /* append every set & selected glyph to the selection */
748 for(y
= 0; y
< term
.row
; y
++) {
749 gp
= &term
.line
[y
][0];
750 last
= gp
+ term
.col
;
752 while(--last
>= gp
&& !(last
->state
& GLYPH_SET
))
755 for(x
= 0; gp
<= last
; x
++, ++gp
) {
756 if(!(is_selected
= selected(x
, y
)))
759 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
761 memcpy(ptr
, p
, size
);
764 /* \n at the end of every selected line except for the last one */
765 if(is_selected
&& y
< sel
.e
.y
)
774 selnotify(XEvent
*e
) {
775 ulong nitems
, ofs
, rem
;
782 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
783 False
, AnyPropertyType
, &type
, &format
,
784 &nitems
, &rem
, &data
)) {
785 fprintf(stderr
, "Clipboard allocation failed\n");
788 ttywrite((const char *) data
, nitems
* format
/ 8);
790 /* number of 32-bit chunks returned */
791 ofs
+= nitems
* format
/ 32;
796 selpaste(const Arg
*dummy
) {
797 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
798 xw
.win
, CurrentTime
);
801 void selclear(XEvent
*e
) {
805 tsetdirt(sel
.b
.y
, sel
.e
.y
);
809 selrequest(XEvent
*e
) {
810 fprintf(stderr
, "selrequest\n");
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 fprintf(stderr
, "xsetsel: %s\n", str
);
847 /* register the selection for both the clipboard and the primary */
853 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
855 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
856 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
860 brelease(XEvent
*e
) {
863 if(IS_SET(MODE_MOUSE
)) {
868 if(e
->xbutton
.button
== Button2
) {
870 } else if(e
->xbutton
.button
== Button1
) {
873 term
.dirty
[sel
.ey
] = 1;
874 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
876 gettimeofday(&now
, NULL
);
878 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
879 /* triple click on the line */
880 sel
.b
.x
= sel
.bx
= 0;
881 sel
.e
.x
= sel
.ex
= term
.col
;
882 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
884 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
885 /* double click to select word */
887 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
888 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
892 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
893 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
897 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
905 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
906 gettimeofday(&sel
.tclick1
, NULL
);
911 int starty
, endy
, oldey
, oldex
;
913 if(IS_SET(MODE_MOUSE
)) {
925 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
926 starty
= MIN(oldey
, sel
.ey
);
927 endy
= MAX(oldey
, sel
.ey
);
928 tsetdirt(starty
, endy
);
933 die(const char *errstr
, ...) {
936 va_start(ap
, errstr
);
937 vfprintf(stderr
, errstr
, ap
);
945 char *envshell
= getenv("SHELL");
946 const struct passwd
*pass
= getpwuid(getuid());
947 char buf
[sizeof(long) * 8 + 1];
954 setenv("LOGNAME", pass
->pw_name
, 1);
955 setenv("USER", pass
->pw_name
, 1);
956 setenv("SHELL", pass
->pw_shell
, 0);
957 setenv("HOME", pass
->pw_dir
, 0);
960 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
961 setenv("WINDOWID", buf
, 1);
963 signal(SIGCHLD
, SIG_DFL
);
964 signal(SIGHUP
, SIG_DFL
);
965 signal(SIGINT
, SIG_DFL
);
966 signal(SIGQUIT
, SIG_DFL
);
967 signal(SIGTERM
, SIG_DFL
);
968 signal(SIGALRM
, SIG_DFL
);
970 DEFAULT(envshell
, shell
);
971 setenv("TERM", termname
, 1);
972 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
973 execvp(args
[0], args
);
981 if(waitpid(pid
, &stat
, 0) < 0)
982 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
984 if(WIFEXITED(stat
)) {
985 exit(WEXITSTATUS(stat
));
994 struct winsize w
= {term
.row
, term
.col
, 0, 0};
996 /* seems to work fine on linux, openbsd and freebsd */
997 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
998 die("openpty failed: %s\n", SERRNO
);
1000 switch(pid
= fork()) {
1002 die("fork failed\n");
1005 setsid(); /* create a new process group */
1006 dup2(s
, STDIN_FILENO
);
1007 dup2(s
, STDOUT_FILENO
);
1008 dup2(s
, STDERR_FILENO
);
1009 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1010 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1018 signal(SIGCHLD
, sigchld
);
1020 iofd
= (!strcmp(opt_io
, "-")) ?
1022 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1024 fprintf(stderr
, "Error opening %s:%s\n",
1025 opt_io
, strerror(errno
));
1035 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1037 fprintf(stderr
, "\n");
1042 static char buf
[BUFSIZ
];
1043 static int buflen
= 0;
1046 int charsize
; /* size of utf8 char in bytes */
1050 /* append read bytes to unprocessed bytes */
1051 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1052 die("Couldn't read from shell: %s\n", SERRNO
);
1054 /* process every complete utf8 char */
1057 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1058 charsize
= utf8decode(ptr
, &utf8c
);
1059 utf8encode(&utf8c
, s
);
1065 /* keep any uncomplete utf8 char for the next call */
1066 memmove(buf
, ptr
, buflen
);
1070 ttywrite(const char *s
, size_t n
) {
1071 if(write(cmdfd
, s
, n
) == -1)
1072 die("write error on tty: %s\n", SERRNO
);
1079 w
.ws_row
= term
.row
;
1080 w
.ws_col
= term
.col
;
1081 w
.ws_xpixel
= xw
.tw
;
1082 w
.ws_ypixel
= xw
.th
;
1083 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1084 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1088 tsetdirt(int top
, int bot
) {
1091 LIMIT(top
, 0, term
.row
-1);
1092 LIMIT(bot
, 0, term
.row
-1);
1094 for(i
= top
; i
<= bot
; i
++)
1100 tsetdirt(0, term
.row
-1);
1107 if(mode
== CURSOR_SAVE
) {
1109 } else if(mode
== CURSOR_LOAD
) {
1119 term
.c
= (TCursor
){{
1123 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1125 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1126 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1129 term
.bot
= term
.row
- 1;
1130 term
.mode
= MODE_WRAP
;
1132 tclearregion(0, 0, term
.col
-1, term
.row
-1, 0);
1134 tcursor(CURSOR_SAVE
);
1138 tnew(int col
, int row
) {
1139 /* set screen size */
1142 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1143 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1144 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1145 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1147 for(row
= 0; row
< term
.row
; row
++) {
1148 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1149 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1150 term
.dirty
[row
] = 0;
1154 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1161 Line
*tmp
= term
.line
;
1163 term
.line
= term
.alt
;
1165 term
.mode
^= MODE_ALTSCREEN
;
1170 tscrolldown(int orig
, int n
) {
1174 LIMIT(n
, 0, term
.bot
-orig
+1);
1176 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
, 0);
1178 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1179 temp
= term
.line
[i
];
1180 term
.line
[i
] = term
.line
[i
-n
];
1181 term
.line
[i
-n
] = temp
;
1184 term
.dirty
[i
-n
] = 1;
1191 tscrollup(int orig
, int n
) {
1194 LIMIT(n
, 0, term
.bot
-orig
+1);
1196 tclearregion(0, orig
, term
.col
-1, orig
+n
-1, 0);
1198 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1199 temp
= term
.line
[i
];
1200 term
.line
[i
] = term
.line
[i
+n
];
1201 term
.line
[i
+n
] = temp
;
1204 term
.dirty
[i
+n
] = 1;
1207 selscroll(orig
, -n
);
1211 selscroll(int orig
, int n
) {
1215 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1216 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1220 if(sel
.by
< term
.top
) {
1224 if(sel
.ey
> term
.bot
) {
1228 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1229 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1234 tnewline(int first_col
) {
1238 tscrollup(term
.top
, 1);
1242 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1247 /* int noarg = 1; */
1248 char *p
= csiescseq
.buf
;
1252 csiescseq
.priv
= 1, p
++;
1254 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1255 while(isdigit(*p
)) {
1256 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1257 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1259 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1260 csiescseq
.narg
++, p
++;
1262 csiescseq
.mode
= *p
;
1270 /* for absolute user moves, when decom is set */
1272 tmoveato(int x
, int y
) {
1273 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1277 tmoveto(int x
, int y
) {
1280 if(term
.c
.state
& CURSOR_ORIGIN
) {
1285 maxy
= term
.row
- 1;
1287 LIMIT(x
, 0, term
.col
-1);
1288 LIMIT(y
, miny
, maxy
);
1289 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1295 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1296 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1297 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1298 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1299 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1300 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1301 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1302 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1303 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1304 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1308 * The table is proudly stolen from rxvt.
1310 if(attr
->mode
& ATTR_GFX
) {
1311 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1312 && vt100_0
[c
[0] - 0x41]) {
1313 c
= vt100_0
[c
[0] - 0x41];
1318 term
.line
[y
][x
] = *attr
;
1319 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1320 term
.line
[y
][x
].state
|= GLYPH_SET
;
1324 tclearregion(int x1
, int y1
, int x2
, int y2
, int bce
) {
1328 temp
= x1
, x1
= x2
, x2
= temp
;
1330 temp
= y1
, y1
= y2
, y2
= temp
;
1332 LIMIT(x1
, 0, term
.col
-1);
1333 LIMIT(x2
, 0, term
.col
-1);
1334 LIMIT(y1
, 0, term
.row
-1);
1335 LIMIT(y2
, 0, term
.row
-1);
1337 for(y
= y1
; y
<= y2
; y
++) {
1339 for(x
= x1
; x
<= x2
; x
++) {
1341 term
.line
[y
][x
] = term
.c
.attr
;
1342 memcpy(term
.line
[y
][x
].c
, " ", 2);
1343 term
.line
[y
][x
].state
|= GLYPH_SET
;
1345 term
.line
[y
][x
].state
= 0;
1352 tdeletechar(int n
) {
1353 int src
= term
.c
.x
+ n
;
1355 int size
= term
.col
- src
;
1357 term
.dirty
[term
.c
.y
] = 1;
1359 if(src
>= term
.col
) {
1360 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1364 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1365 size
* sizeof(Glyph
));
1366 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1370 tinsertblank(int n
) {
1373 int size
= term
.col
- dst
;
1375 term
.dirty
[term
.c
.y
] = 1;
1377 if(dst
>= term
.col
) {
1378 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1382 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1383 size
* sizeof(Glyph
));
1384 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
, 0);
1388 tinsertblankline(int n
) {
1389 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1392 tscrolldown(term
.c
.y
, n
);
1396 tdeleteline(int n
) {
1397 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1400 tscrollup(term
.c
.y
, n
);
1404 tsetattr(int *attr
, int l
) {
1407 for(i
= 0; i
< l
; i
++) {
1410 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1411 | ATTR_ITALIC
| ATTR_BLINK
);
1412 term
.c
.attr
.fg
= defaultfg
;
1413 term
.c
.attr
.bg
= defaultbg
;
1416 term
.c
.attr
.mode
|= ATTR_BOLD
;
1419 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1422 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
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
;
1441 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1444 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1447 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1449 if(BETWEEN(attr
[i
], 0, 255)) {
1450 term
.c
.attr
.fg
= attr
[i
];
1453 "erresc: bad fgcolor %d\n",
1458 "erresc(38): gfx attr %d unknown\n",
1463 term
.c
.attr
.fg
= defaultfg
;
1466 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1468 if(BETWEEN(attr
[i
], 0, 255)) {
1469 term
.c
.attr
.bg
= attr
[i
];
1472 "erresc: bad bgcolor %d\n",
1477 "erresc(48): gfx attr %d unknown\n",
1482 term
.c
.attr
.bg
= defaultbg
;
1485 if(BETWEEN(attr
[i
], 30, 37)) {
1486 term
.c
.attr
.fg
= attr
[i
] - 30;
1487 } else if(BETWEEN(attr
[i
], 40, 47)) {
1488 term
.c
.attr
.bg
= attr
[i
] - 40;
1489 } else if(BETWEEN(attr
[i
], 90, 97)) {
1490 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1491 } else if(BETWEEN(attr
[i
], 100, 107)) {
1492 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1495 "erresc(default): gfx attr %d unknown\n",
1496 attr
[i
]), csidump();
1504 tsetscroll(int t
, int b
) {
1507 LIMIT(t
, 0, term
.row
-1);
1508 LIMIT(b
, 0, term
.row
-1);
1518 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1521 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1525 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1529 case 1: /* DECCKM -- Cursor key */
1530 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1532 case 5: /* DECSCNM -- Reverse video */
1534 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1535 if(mode
!= term
.mode
)
1536 redraw(REDRAW_TIMEOUT
);
1538 case 6: /* DECOM -- Origin */
1539 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1542 case 7: /* DECAWM -- Auto wrap */
1543 MODBIT(term
.mode
, set
, MODE_WRAP
);
1545 case 0: /* Error (IGNORED) */
1546 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1547 case 3: /* DECCOLM -- Column (IGNORED) */
1548 case 4: /* DECSCLM -- Scroll (IGNORED) */
1549 case 8: /* DECARM -- Auto repeat (IGNORED) */
1550 case 18: /* DECPFF -- Printer feed (IGNORED) */
1551 case 19: /* DECPEX -- Printer extent (IGNORED) */
1552 case 42: /* DECNRCM -- National characters (IGNORED) */
1553 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1555 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1556 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1558 case 1000: /* 1000,1002: enable xterm mouse report */
1559 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1562 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1565 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1567 case 1049: /* = 1047 and 1048 */
1570 alt
= IS_SET(MODE_ALTSCREEN
);
1572 tclearregion(0, 0, term
.col
-1,
1575 if(set
^ alt
) /* set is always 1 or 0 */
1582 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1586 "erresc: unknown private set/reset mode %d\n",
1592 case 0: /* Error (IGNORED) */
1594 case 2: /* KAM -- keyboard action */
1595 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1597 case 4: /* IRM -- Insertion-replacement */
1598 MODBIT(term
.mode
, set
, MODE_INSERT
);
1600 case 12: /* SRM -- Send/Receive */
1601 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1603 case 20: /* LNM -- Linefeed/new line */
1604 MODBIT(term
.mode
, set
, MODE_CRLF
);
1608 "erresc: unknown set/reset mode %d\n",
1620 switch(csiescseq
.mode
) {
1623 fprintf(stderr
, "erresc: unknown csi ");
1627 case '@': /* ICH -- Insert <n> blank char */
1628 DEFAULT(csiescseq
.arg
[0], 1);
1629 tinsertblank(csiescseq
.arg
[0]);
1631 case 'A': /* CUU -- Cursor <n> Up */
1632 DEFAULT(csiescseq
.arg
[0], 1);
1633 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1635 case 'B': /* CUD -- Cursor <n> Down */
1636 case 'e': /* VPR --Cursor <n> Down */
1637 DEFAULT(csiescseq
.arg
[0], 1);
1638 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1640 case 'c': /* DA -- Device Attributes */
1641 if(csiescseq
.arg
[0] == 0)
1642 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1644 case 'C': /* CUF -- Cursor <n> Forward */
1645 case 'a': /* HPR -- Cursor <n> Forward */
1646 DEFAULT(csiescseq
.arg
[0], 1);
1647 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1649 case 'D': /* CUB -- Cursor <n> Backward */
1650 DEFAULT(csiescseq
.arg
[0], 1);
1651 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1653 case 'E': /* CNL -- Cursor <n> Down and first col */
1654 DEFAULT(csiescseq
.arg
[0], 1);
1655 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1657 case 'F': /* CPL -- Cursor <n> Up and first col */
1658 DEFAULT(csiescseq
.arg
[0], 1);
1659 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1661 case 'g': /* TBC -- Tabulation clear */
1662 switch (csiescseq
.arg
[0]) {
1663 case 0: /* clear current tab stop */
1664 term
.tabs
[term
.c
.x
] = 0;
1666 case 3: /* clear all the tabs */
1667 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1673 case 'G': /* CHA -- Move to <col> */
1675 DEFAULT(csiescseq
.arg
[0], 1);
1676 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1678 case 'H': /* CUP -- Move to <row> <col> */
1680 DEFAULT(csiescseq
.arg
[0], 1);
1681 DEFAULT(csiescseq
.arg
[1], 1);
1682 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1684 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1685 DEFAULT(csiescseq
.arg
[0], 1);
1686 while(csiescseq
.arg
[0]--)
1689 case 'J': /* ED -- Clear screen */
1691 switch(csiescseq
.arg
[0]) {
1693 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1694 if(term
.c
.y
< term
.row
-1) {
1695 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1701 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1, 1);
1702 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1705 tclearregion(0, 0, term
.col
-1, term
.row
-1, 1);
1711 case 'K': /* EL -- Clear line */
1712 switch(csiescseq
.arg
[0]) {
1714 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1718 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1721 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1725 case 'S': /* SU -- Scroll <n> line up */
1726 DEFAULT(csiescseq
.arg
[0], 1);
1727 tscrollup(term
.top
, csiescseq
.arg
[0]);
1729 case 'T': /* SD -- Scroll <n> line down */
1730 DEFAULT(csiescseq
.arg
[0], 1);
1731 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1733 case 'L': /* IL -- Insert <n> blank lines */
1734 DEFAULT(csiescseq
.arg
[0], 1);
1735 tinsertblankline(csiescseq
.arg
[0]);
1737 case 'l': /* RM -- Reset Mode */
1738 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1740 case 'M': /* DL -- Delete <n> lines */
1741 DEFAULT(csiescseq
.arg
[0], 1);
1742 tdeleteline(csiescseq
.arg
[0]);
1744 case 'X': /* ECH -- Erase <n> char */
1745 DEFAULT(csiescseq
.arg
[0], 1);
1746 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0],
1749 case 'P': /* DCH -- Delete <n> char */
1750 DEFAULT(csiescseq
.arg
[0], 1);
1751 tdeletechar(csiescseq
.arg
[0]);
1753 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1754 DEFAULT(csiescseq
.arg
[0], 1);
1755 while(csiescseq
.arg
[0]--)
1758 case 'd': /* VPA -- Move to <row> */
1759 DEFAULT(csiescseq
.arg
[0], 1);
1760 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1762 case 'h': /* SM -- Set terminal mode */
1763 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1765 case 'm': /* SGR -- Terminal attribute (color) */
1766 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1768 case 'r': /* DECSTBM -- Set Scrolling Region */
1769 if(csiescseq
.priv
) {
1772 DEFAULT(csiescseq
.arg
[0], 1);
1773 DEFAULT(csiescseq
.arg
[1], term
.row
);
1774 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1778 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1779 tcursor(CURSOR_SAVE
);
1781 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1782 tcursor(CURSOR_LOAD
);
1793 for(i
= 0; i
< csiescseq
.len
; i
++) {
1794 c
= csiescseq
.buf
[i
] & 0xff;
1797 } else if(c
== '\n') {
1799 } else if(c
== '\r') {
1801 } else if(c
== 0x1b) {
1804 printf("(%02x)", c
);
1812 memset(&csiescseq
, 0, sizeof(csiescseq
));
1820 * TODO: make this being useful in case of color palette change.
1826 switch(strescseq
.type
) {
1827 case ']': /* OSC -- Operating System Command */
1833 * TODO: Handle special chars in string, like umlauts.
1836 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1840 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1842 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1845 fprintf(stderr
, "erresc: unknown str ");
1850 case 'k': /* old title set compatibility */
1851 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1853 case 'P': /* DSC -- Device Control String */
1854 case '_': /* APC -- Application Program Command */
1855 case '^': /* PM -- Privacy Message */
1857 fprintf(stderr
, "erresc: unknown str ");
1867 * TODO: Implement parsing like for CSI when required.
1868 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1878 printf("ESC%c", strescseq
.type
);
1879 for(i
= 0; i
< strescseq
.len
; i
++) {
1880 c
= strescseq
.buf
[i
] & 0xff;
1883 } else if(c
== '\n') {
1885 } else if(c
== '\r') {
1887 } else if(c
== 0x1b) {
1890 printf("(%02x)", c
);
1898 memset(&strescseq
, 0, sizeof(strescseq
));
1902 tputtab(bool forward
) {
1908 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1913 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1916 tmoveto(x
, term
.c
.y
);
1920 techo(char *buf
, int len
) {
1921 for(; len
> 0; buf
++, len
--) {
1924 if(c
== '\033') { /* escape */
1927 } else if (c
< '\x20') { /* control code */
1928 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
1942 tputc(char *c
, int len
) {
1944 bool control
= ascii
< '\x20' || ascii
== 0177;
1947 if (xwrite(iofd
, c
, len
) < 0) {
1948 fprintf(stderr
, "Error writing in %s:%s\n",
1949 opt_io
, strerror(errno
));
1956 * STR sequences must be checked before anything else
1957 * because it can use some control codes as part of the sequence.
1959 if(term
.esc
& ESC_STR
) {
1962 term
.esc
= ESC_START
| ESC_STR_END
;
1964 case '\a': /* backwards compatibility to xterm */
1969 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
)) {
1970 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
1971 strescseq
.len
+= len
;
1974 * Here is a bug in terminals. If the user never sends
1975 * some code to stop the str or esc command, then st
1976 * will stop responding. But this is better than
1977 * silently failing with unknown characters. At least
1978 * then users will report back.
1980 * In the case users ever get fixed, here is the code:
1992 * Actions of control codes must be performed as soon they arrive
1993 * because they can be embedded inside a control sequence, and
1994 * they must not cause conflicts with sequences.
2002 tmoveto(term
.c
.x
-1, term
.c
.y
);
2005 tmoveto(0, term
.c
.y
);
2010 /* go to first col if the mode is set */
2011 tnewline(IS_SET(MODE_CRLF
));
2013 case '\a': /* BEL */
2014 if(!(xw
.state
& WIN_FOCUSED
))
2017 case '\033': /* ESC */
2019 term
.esc
= ESC_START
;
2021 case '\016': /* SO */
2022 case '\017': /* SI */
2024 * Different charsets are hard to handle. Applications
2025 * should use the right alt charset escapes for the
2026 * only reason they still exist: line drawing. The
2027 * rest is incompatible history st should not support.
2030 case '\032': /* SUB */
2031 case '\030': /* CAN */
2034 case '\005': /* ENQ (IGNORED) */
2035 case '\000': /* NUL (IGNORED) */
2036 case '\021': /* XON (IGNORED) */
2037 case '\023': /* XOFF (IGNORED) */
2038 case 0177: /* DEL (IGNORED) */
2041 } else if(term
.esc
& ESC_START
) {
2042 if(term
.esc
& ESC_CSI
) {
2043 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2044 if(BETWEEN(ascii
, 0x40, 0x7E)
2045 || csiescseq
.len
>= ESC_BUF_SIZ
) {
2047 csiparse(), csihandle();
2049 } else if(term
.esc
& ESC_STR_END
) {
2053 } else if(term
.esc
& ESC_ALTCHARSET
) {
2055 case '0': /* Line drawing set */
2056 term
.c
.attr
.mode
|= ATTR_GFX
;
2058 case 'B': /* USASCII */
2059 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2061 case 'A': /* UK (IGNORED) */
2062 case '<': /* multinational charset (IGNORED) */
2063 case '5': /* Finnish (IGNORED) */
2064 case 'C': /* Finnish (IGNORED) */
2065 case 'K': /* German (IGNORED) */
2068 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2071 } else if(term
.esc
& ESC_TEST
) {
2072 if(ascii
== '8') { /* DEC screen alignment test. */
2073 char E
[UTF_SIZ
] = "E";
2076 for(x
= 0; x
< term
.col
; ++x
) {
2077 for(y
= 0; y
< term
.row
; ++y
)
2078 tsetchar(E
, &term
.c
.attr
, x
, y
);
2085 term
.esc
|= ESC_CSI
;
2088 term
.esc
|= ESC_TEST
;
2090 case 'P': /* DCS -- Device Control String */
2091 case '_': /* APC -- Application Program Command */
2092 case '^': /* PM -- Privacy Message */
2093 case ']': /* OSC -- Operating System Command */
2094 case 'k': /* old title set compatibility */
2096 strescseq
.type
= ascii
;
2097 term
.esc
|= ESC_STR
;
2099 case '(': /* set primary charset G0 */
2100 term
.esc
|= ESC_ALTCHARSET
;
2102 case ')': /* set secondary charset G1 (IGNORED) */
2103 case '*': /* set tertiary charset G2 (IGNORED) */
2104 case '+': /* set quaternary charset G3 (IGNORED) */
2107 case 'D': /* IND -- Linefeed */
2108 if(term
.c
.y
== term
.bot
) {
2109 tscrollup(term
.top
, 1);
2111 tmoveto(term
.c
.x
, term
.c
.y
+1);
2115 case 'E': /* NEL -- Next line */
2116 tnewline(1); /* always go to first col */
2119 case 'H': /* HTS -- Horizontal tab stop */
2120 term
.tabs
[term
.c
.x
] = 1;
2123 case 'M': /* RI -- Reverse index */
2124 if(term
.c
.y
== term
.top
) {
2125 tscrolldown(term
.top
, 1);
2127 tmoveto(term
.c
.x
, term
.c
.y
-1);
2131 case 'Z': /* DECID -- Identify Terminal */
2132 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2135 case 'c': /* RIS -- Reset to inital state */
2140 case '=': /* DECPAM -- Application keypad */
2141 term
.mode
|= MODE_APPKEYPAD
;
2144 case '>': /* DECPNM -- Normal keypad */
2145 term
.mode
&= ~MODE_APPKEYPAD
;
2148 case '7': /* DECSC -- Save Cursor */
2149 tcursor(CURSOR_SAVE
);
2152 case '8': /* DECRC -- Restore Cursor */
2153 tcursor(CURSOR_LOAD
);
2156 case '\\': /* ST -- Stop */
2160 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2161 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2166 * All characters which form part of a sequence are not
2172 * Display control codes only if we are in graphic mode
2174 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2176 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2178 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2179 tnewline(1); /* always go to first col */
2181 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2182 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2183 &term
.line
[term
.c
.y
][term
.c
.x
],
2184 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2187 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2188 if(term
.c
.x
+1 < term
.col
) {
2189 tmoveto(term
.c
.x
+1, term
.c
.y
);
2191 term
.c
.state
|= CURSOR_WRAPNEXT
;
2196 tresize(int col
, int row
) {
2198 int minrow
= MIN(row
, term
.row
);
2199 int mincol
= MIN(col
, term
.col
);
2200 int slide
= term
.c
.y
- row
+ 1;
2203 if(col
< 1 || row
< 1)
2206 /* free unneeded rows */
2209 /* slide screen to keep cursor where we expect it -
2210 * tscrollup would work here, but we can optimize to
2211 * memmove because we're freeing the earlier lines */
2212 for(/* i = 0 */; i
< slide
; i
++) {
2216 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2217 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2219 for(i
+= row
; i
< term
.row
; i
++) {
2224 /* resize to new height */
2225 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2226 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2227 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2228 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2230 /* resize each row to new width, zero-pad if needed */
2231 for(i
= 0; i
< minrow
; i
++) {
2233 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2234 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2235 for(x
= mincol
; x
< col
; x
++) {
2236 term
.line
[i
][x
].state
= 0;
2237 term
.alt
[i
][x
].state
= 0;
2241 /* allocate any new rows */
2242 for(/* i == minrow */; i
< row
; i
++) {
2244 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2245 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2247 if(col
> term
.col
) {
2248 bp
= term
.tabs
+ term
.col
;
2250 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2251 while(--bp
> term
.tabs
&& !*bp
)
2253 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2256 /* update terminal size */
2259 /* reset scrolling region */
2260 tsetscroll(0, row
-1);
2261 /* make use of the LIMIT in tmoveto */
2262 tmoveto(term
.c
.x
, term
.c
.y
);
2268 xresize(int col
, int row
) {
2269 xw
.tw
= MAX(1, col
* xw
.cw
);
2270 xw
.th
= MAX(1, row
* xw
.ch
);
2273 XFreePixmap(xw
.dpy
, xw
.buf
);
2274 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2275 DefaultDepth(xw
.dpy
, xw
.scr
));
2276 XSetForeground(xw
.dpy
, dc
.gc
, 0);
2277 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2280 XftDrawChange(xw
.draw
, xw
.buf
);
2286 XRenderColor color
= { .alpha
= 0 };
2288 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2289 for(i
= 0; i
< LEN(colorname
); i
++) {
2292 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2293 die("Could not allocate color '%s'\n", colorname
[i
]);
2297 /* load colors [16-255] ; same colors as xterm */
2298 for(i
= 16, r
= 0; r
< 6; r
++) {
2299 for(g
= 0; g
< 6; g
++) {
2300 for(b
= 0; b
< 6; b
++) {
2301 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2302 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2303 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2304 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2305 die("Could not allocate color %d\n", i
);
2312 for(r
= 0; r
< 24; r
++, i
++) {
2313 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2314 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2316 die("Could not allocate color %d\n", i
);
2322 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2323 XftDrawRect(xw
.draw
,
2324 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2325 borderpx
+ col1
* xw
.cw
,
2326 borderpx
+ row1
* xw
.ch
,
2327 (col2
-col1
+1) * xw
.cw
,
2328 (row2
-row1
+1) * xw
.ch
);
2332 * Absolute coordinates.
2335 xclear(int x1
, int y1
, int x2
, int y2
) {
2336 XftDrawRect(xw
.draw
,
2337 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2338 x1
, y1
, x2
-x1
, y2
-y1
);
2343 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2344 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2345 XSizeHints
*sizeh
= NULL
;
2347 sizeh
= XAllocSizeHints();
2348 if(xw
.isfixed
== False
) {
2349 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2350 sizeh
->height
= xw
.h
;
2351 sizeh
->width
= xw
.w
;
2352 sizeh
->height_inc
= xw
.ch
;
2353 sizeh
->width_inc
= xw
.cw
;
2354 sizeh
->base_height
= 2 * borderpx
;
2355 sizeh
->base_width
= 2 * borderpx
;
2357 sizeh
->flags
= PMaxSize
| PMinSize
;
2358 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2359 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2362 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2367 xloadfont(Font
*f
, FcPattern
*pattern
) {
2371 match
= FcFontMatch(NULL
, pattern
, &result
);
2375 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2376 FcPatternDestroy(match
);
2380 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2381 FcPatternDestroy(match
);
2385 f
->pattern
= FcPatternDuplicate(pattern
);
2387 f
->ascent
= f
->match
->ascent
;
2388 f
->descent
= f
->match
->descent
;
2390 f
->rbearing
= f
->match
->max_advance_width
;
2392 f
->height
= f
->match
->height
;
2393 f
->width
= f
->lbearing
+ f
->rbearing
;
2399 xloadfonts(char *fontstr
, int fontsize
) {
2404 if(fontstr
[0] == '-') {
2405 pattern
= XftXlfdParse(fontstr
, False
, False
);
2407 pattern
= FcNameParse((FcChar8
*)fontstr
);
2411 die("st: can't open font %s\n", fontstr
);
2414 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2415 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2416 usedfontsize
= fontsize
;
2418 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2419 if(result
== FcResultMatch
) {
2420 usedfontsize
= (int)fontval
;
2423 * Default font size is 12, if none given. This is to
2424 * have a known usedfontsize value.
2426 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2431 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2432 FcDefaultSubstitute(pattern
);
2434 if(xloadfont(&dc
.font
, pattern
))
2435 die("st: can't open font %s\n", fontstr
);
2437 /* Setting character width and height. */
2438 xw
.cw
= dc
.font
.width
;
2439 xw
.ch
= dc
.font
.height
;
2441 FcPatternDel(pattern
, FC_WEIGHT
);
2442 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2443 if(xloadfont(&dc
.bfont
, pattern
))
2444 die("st: can't open font %s\n", fontstr
);
2446 FcPatternDel(pattern
, FC_SLANT
);
2447 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2448 if(xloadfont(&dc
.ibfont
, pattern
))
2449 die("st: can't open font %s\n", fontstr
);
2451 FcPatternDel(pattern
, FC_WEIGHT
);
2452 if(xloadfont(&dc
.ifont
, pattern
))
2453 die("st: can't open font %s\n", fontstr
);
2455 FcPatternDestroy(pattern
);
2464 * Free the loaded fonts in the font cache. This is done backwards
2467 for (i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2470 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2475 XftFontClose(xw
.dpy
, dc
.font
.match
);
2476 FcPatternDestroy(dc
.font
.pattern
);
2477 FcFontSetDestroy(dc
.font
.set
);
2478 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2479 FcPatternDestroy(dc
.bfont
.pattern
);
2480 FcFontSetDestroy(dc
.bfont
.set
);
2481 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2482 FcPatternDestroy(dc
.ifont
.pattern
);
2483 FcFontSetDestroy(dc
.ifont
.set
);
2484 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2485 FcPatternDestroy(dc
.ibfont
.pattern
);
2486 FcFontSetDestroy(dc
.ibfont
.set
);
2490 xzoom(const Arg
*arg
)
2493 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2500 XSetWindowAttributes attrs
;
2504 int sw
, sh
, major
, minor
;
2506 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2507 die("Can't open display\n");
2508 xw
.scr
= XDefaultScreen(xw
.dpy
);
2509 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2513 die("Could not init fontconfig.\n");
2515 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2516 xloadfonts(usedfont
, 0);
2519 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2522 /* adjust fixed window geometry */
2524 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2525 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2527 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2529 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2534 /* window - default size */
2535 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2536 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2542 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2543 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2544 attrs
.bit_gravity
= NorthWestGravity
;
2545 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2546 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2547 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2548 attrs
.colormap
= xw
.cmap
;
2550 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2551 XRootWindow(xw
.dpy
, xw
.scr
);
2552 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2553 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2555 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2559 /* double buffering */
2561 if(XdbeQueryExtension(xw.dpy, &major, &minor)) {
2562 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win,
2567 memset(&gcvalues
, 0, sizeof(gcvalues
));
2568 gcvalues
.graphics_exposures
= False
;
2569 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2571 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2572 DefaultDepth(xw
.dpy
, xw
.scr
));
2573 XSetForeground(xw
.dpy
, dc
.gc
, 0);
2574 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2580 /* Xft rendering context */
2581 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2584 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2585 XSetLocaleModifiers("@im=local");
2586 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2587 XSetLocaleModifiers("@im=");
2588 if((xw
.xim
= XOpenIM(xw
.dpy
,
2589 NULL
, NULL
, NULL
)) == NULL
) {
2590 die("XOpenIM failed. Could not open input"
2595 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2596 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2597 XNFocusWindow
, xw
.win
, NULL
);
2599 die("XCreateIC failed. Could not obtain input method.\n");
2601 /* white cursor, black outline */
2602 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2603 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2604 XRecolorCursor(xw
.dpy
, cursor
,
2605 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2606 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2608 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2609 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2610 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2613 XMapWindow(xw
.dpy
, xw
.win
);
2619 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2620 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2621 width
= charlen
* xw
.cw
, xp
, i
;
2623 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2626 Font
*font
= &dc
.font
;
2628 FcPattern
*fcpattern
, *fontpattern
;
2629 FcFontSet
*fcsets
[] = { NULL
};
2630 FcCharSet
*fccharset
;
2631 Colour
*fg
= &dc
.col
[base
.fg
], *bg
= &dc
.col
[base
.bg
],
2632 *temp
, revfg
, revbg
;
2633 XRenderColor colfg
, colbg
;
2635 frcflags
= FRC_NORMAL
;
2637 if(base
.mode
& ATTR_BOLD
) {
2638 if(BETWEEN(base
.fg
, 0, 7)) {
2639 /* basic system colors */
2640 fg
= &dc
.col
[base
.fg
+ 8];
2641 } else if(BETWEEN(base
.fg
, 16, 195)) {
2643 fg
= &dc
.col
[base
.fg
+ 36];
2644 } else if(BETWEEN(base
.fg
, 232, 251)) {
2646 fg
= &dc
.col
[base
.fg
+ 4];
2649 * Those ranges will not be brightened:
2650 * 8 - 15 – bright system colors
2651 * 196 - 231 – highest 256 color cube
2652 * 252 - 255 – brightest colors in greyscale
2655 frcflags
= FRC_BOLD
;
2658 if(base
.mode
& ATTR_ITALIC
) {
2660 frcflags
= FRC_ITALIC
;
2662 if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2664 frcflags
= FRC_ITALICBOLD
;
2667 if(IS_SET(MODE_REVERSE
)) {
2668 if(fg
== &dc
.col
[defaultfg
]) {
2669 fg
= &dc
.col
[defaultbg
];
2671 colfg
.red
= ~fg
->color
.red
;
2672 colfg
.green
= ~fg
->color
.green
;
2673 colfg
.blue
= ~fg
->color
.blue
;
2674 colfg
.alpha
= fg
->color
.alpha
;
2675 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2679 if(bg
== &dc
.col
[defaultbg
]) {
2680 bg
= &dc
.col
[defaultfg
];
2682 colbg
.red
= ~bg
->color
.red
;
2683 colbg
.green
= ~bg
->color
.green
;
2684 colbg
.blue
= ~bg
->color
.blue
;
2685 colbg
.alpha
= bg
->color
.alpha
;
2686 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2691 if(base
.mode
& ATTR_REVERSE
) {
2697 /* Intelligent cleaning up of the borders. */
2699 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2700 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2702 if(x
+ charlen
>= term
.col
) {
2703 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2704 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2707 xclear(winx
, 0, winx
+ width
, borderpx
);
2709 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2711 /* Clean up the region we want to draw to. */
2712 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2714 fcsets
[0] = font
->set
;
2715 for (xp
= winx
; bytelen
> 0;) {
2717 * Search for the range in the to be printed string of glyphs
2718 * that are in the main font. Then print that range. If
2719 * some glyph is found that is not in the font, do the
2727 u8cblen
= utf8decode(s
, &u8char
);
2731 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2732 if (!doesexist
|| bytelen
<= 0) {
2741 XftDrawStringUtf8(xw
.draw
, fg
,
2743 winy
+ font
->ascent
,
2746 xp
+= font
->width
* u8fl
;
2758 /* Search the font cache. */
2759 for (i
= 0; i
< frclen
; i
++, frp
--) {
2763 if (frc
[frp
].c
== u8char
2764 && frc
[frp
].flags
== frcflags
) {
2769 /* Nothing was found. */
2772 * Nothing was found in the cache. Now use
2773 * some dozen of Fontconfig calls to get the
2774 * font for one single character.
2776 fcpattern
= FcPatternDuplicate(font
->pattern
);
2777 fccharset
= FcCharSetCreate();
2779 FcCharSetAddChar(fccharset
, u8char
);
2780 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2782 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2785 FcConfigSubstitute(0, fcpattern
,
2787 FcDefaultSubstitute(fcpattern
);
2789 fontpattern
= FcFontSetMatch(0, fcsets
,
2790 FcTrue
, fcpattern
, &fcres
);
2793 * Overwrite or create the new cache entry
2798 if (frccur
>= LEN(frc
))
2800 if (frclen
> LEN(frc
)) {
2802 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2805 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2807 frc
[frccur
].c
= u8char
;
2808 frc
[frccur
].flags
= frcflags
;
2810 FcPatternDestroy(fcpattern
);
2811 FcCharSetDestroy(fccharset
);
2816 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2817 xp
, winy
+ frc
[frp
].font
->ascent
,
2818 (FcChar8
*)u8c
, u8cblen
);
2824 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2825 winy + font->ascent, (FcChar8 *)s, bytelen);
2828 if(base
.mode
& ATTR_UNDERLINE
) {
2829 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2836 static int oldx
= 0, oldy
= 0;
2838 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
, 0};
2840 LIMIT(oldx
, 0, term
.col
-1);
2841 LIMIT(oldy
, 0, term
.row
-1);
2843 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2844 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2846 /* remove the old cursor */
2847 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2848 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2849 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2852 xtermclear(oldx
, oldy
, oldx
, oldy
);
2855 /* draw the new one */
2856 if(!(IS_SET(MODE_HIDE
))) {
2857 if(!(xw
.state
& WIN_FOCUSED
))
2860 if(IS_SET(MODE_REVERSE
))
2861 g
.mode
|= ATTR_REVERSE
, g
.fg
= defaultcs
, g
.bg
= defaultfg
;
2864 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2865 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2871 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2875 redraw(int timeout
) {
2876 struct timespec tv
= {0, timeout
* 1000};
2882 nanosleep(&tv
, NULL
);
2883 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2889 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2891 drawregion(0, 0, term
.col
, term
.row
);
2893 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2895 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
2897 XSetForeground(xw
.dpy
, dc
.gc
, 0);
2902 drawregion(int x1
, int y1
, int x2
, int y2
) {
2903 int ic
, ib
, x
, y
, ox
, sl
;
2905 char buf
[DRAW_BUF_SIZ
];
2906 bool ena_sel
= sel
.bx
!= -1;
2908 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
2911 if(!(xw
.state
& WIN_VISIBLE
))
2914 for(y
= y1
; y
< y2
; y
++) {
2918 xtermclear(0, y
, term
.col
, y
);
2920 base
= term
.line
[y
][0];
2922 for(x
= x1
; x
< x2
; x
++) {
2923 new = term
.line
[y
][x
];
2924 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2925 new.mode
^= ATTR_REVERSE
;
2926 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2927 || ATTRCMP(base
, new)
2928 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2929 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2932 if(new.state
& GLYPH_SET
) {
2938 sl
= utf8size(new.c
);
2939 memcpy(buf
+ib
, new.c
, sl
);
2945 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2951 expose(XEvent
*ev
) {
2952 XExposeEvent
*e
= &ev
->xexpose
;
2954 if(xw
.state
& WIN_REDRAW
) {
2956 xw
.state
&= ~WIN_REDRAW
;
2962 visibility(XEvent
*ev
) {
2963 XVisibilityEvent
*e
= &ev
->xvisibility
;
2965 if(e
->state
== VisibilityFullyObscured
) {
2966 xw
.state
&= ~WIN_VISIBLE
;
2967 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2968 /* need a full redraw for next Expose, not just a buf copy */
2969 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2975 xw
.state
&= ~WIN_VISIBLE
;
2979 xseturgency(int add
) {
2980 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2982 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2983 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2989 if(ev
->type
== FocusIn
) {
2990 XSetICFocus(xw
.xic
);
2991 xw
.state
|= WIN_FOCUSED
;
2994 XUnsetICFocus(xw
.xic
);
2995 xw
.state
&= ~WIN_FOCUSED
;
3000 match(uint mask
, uint state
) {
3001 if(mask
== XK_NO_MOD
&& state
)
3003 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3005 if((state
& mask
) != state
)
3011 numlock(const Arg
*dummy
) {
3016 kmap(KeySym k
, uint state
) {
3021 /* Check for mapped keys out of X11 function keys. */
3022 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3023 if(mappedkeys
[i
] == k
)
3026 if(i
== LEN(mappedkeys
)) {
3027 if((k
& 0xFFFF) < 0xFD00)
3031 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3037 if(!match(mask
, state
))
3040 if(kp
->appkey
> 0) {
3041 if(!IS_SET(MODE_APPKEYPAD
))
3043 if(term
.numlock
&& kp
->appkey
== 2)
3045 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3049 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3051 && !IS_SET(MODE_APPCURSOR
))) {
3055 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3056 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3067 kpress(XEvent
*ev
) {
3068 XKeyEvent
*e
= &ev
->xkey
;
3070 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3075 if (IS_SET(MODE_KBDLOCK
))
3078 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3079 e
->state
&= ~Mod2Mask
;
3081 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3082 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3083 bp
->func(&(bp
->arg
));
3088 /* 2. custom keys from config.h */
3089 if((customkey
= kmap(ksym
, e
->state
))) {
3090 len
= strlen(customkey
);
3091 memcpy(buf
, customkey
, len
);
3092 /* 2. hardcoded (overrides X lookup) */
3097 if (len
== 1 && e
->state
& Mod1Mask
)
3100 memcpy(cp
, xstr
, len
);
3101 len
= cp
- buf
+ len
;
3105 if(IS_SET(MODE_ECHO
))
3111 cmessage(XEvent
*e
) {
3113 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
3114 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3115 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3116 xw
.state
|= WIN_FOCUSED
;
3118 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3119 xw
.state
&= ~WIN_FOCUSED
;
3121 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3122 /* Send SIGHUP to shell */
3129 cresize(int width
, int height
)
3138 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3139 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3148 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3151 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3158 int xfd
= XConnectionNumber(xw
.dpy
), i
;
3159 struct timeval drawtimeout
, *tv
= NULL
;
3163 FD_SET(cmdfd
, &rfd
);
3165 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3168 die("select failed: %s\n", SERRNO
);
3172 * Stop after a certain number of reads so the user does not
3173 * feel like the system is stuttering.
3175 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
3179 * Just wait a bit so it isn't disturbing the
3180 * user and the system is able to write something.
3182 drawtimeout
.tv_sec
= 0;
3183 drawtimeout
.tv_usec
= 5;
3190 while(XPending(xw
.dpy
)) {
3191 XNextEvent(xw
.dpy
, &ev
);
3192 if(XFilterEvent(&ev
, None
))
3194 if(handler
[ev
.type
])
3195 (handler
[ev
.type
])(&ev
);
3204 main(int argc
, char *argv
[]) {
3205 int i
, bitm
, xr
, yr
;
3208 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3211 for(i
= 1; i
< argc
; i
++) {
3212 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
3215 opt_class
= argv
[i
];
3218 /* eat all remaining arguments */
3230 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
3235 if(bitm
& WidthValue
)
3237 if(bitm
& HeightValue
)
3239 if(bitm
& XNegative
&& xw
.fx
== 0)
3241 if(bitm
& XNegative
&& xw
.fy
== 0)
3244 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3253 opt_title
= argv
[i
];
3260 opt_embed
= argv
[i
];
3266 setlocale(LC_CTYPE
, "");
3267 XSetLocaleModifiers("");