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/Xft/Xft.h>
29 #include <fontconfig/fontconfig.h>
33 #define Draw XftDraw *
34 #define Colour XftColor
35 #define Colourmap Colormap
39 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
41 #elif defined(__FreeBSD__) || defined(__DragonFly__)
46 "st " VERSION " (c) 2010-2013 st engineers\n" \
47 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
48 " [-t title] [-w windowid] [-e command ...]\n"
51 #define XEMBED_FOCUS_IN 4
52 #define XEMBED_FOCUS_OUT 5
56 #define ESC_BUF_SIZ (128*UTF_SIZ)
57 #define ESC_ARG_SIZ 16
58 #define STR_BUF_SIZ ESC_BUF_SIZ
59 #define STR_ARG_SIZ ESC_ARG_SIZ
60 #define DRAW_BUF_SIZ 20*1024
61 #define XK_ANY_MOD UINT_MAX
63 #define XK_SWITCH_MOD (1<<13)
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 */
139 enum selection_type
{
146 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
148 typedef unsigned char uchar
;
149 typedef unsigned int uint
;
150 typedef unsigned long ulong
;
151 typedef unsigned short ushort
;
154 char c
[UTF_SIZ
]; /* character code */
155 uchar mode
; /* attribute flags */
156 ushort fg
; /* foreground */
157 ushort bg
; /* background */
158 uchar state
; /* state flags */
164 Glyph attr
; /* current char attributes */
170 /* CSI Escape sequence structs */
171 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
173 char buf
[ESC_BUF_SIZ
]; /* raw string */
174 int len
; /* raw string length */
176 int arg
[ESC_ARG_SIZ
];
177 int narg
; /* nb of args */
181 /* STR Escape sequence structs */
182 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
184 char type
; /* ESC type ... */
185 char buf
[STR_BUF_SIZ
]; /* raw string */
186 int len
; /* raw string length */
187 char *args
[STR_ARG_SIZ
];
188 int narg
; /* nb of args */
191 /* Internal representation of the screen */
193 int row
; /* nb row */
194 int col
; /* nb col */
195 Line
*line
; /* screen */
196 Line
*alt
; /* alternate screen */
197 bool *dirty
; /* dirtyness of lines */
198 TCursor c
; /* cursor */
199 int top
; /* top scroll limit */
200 int bot
; /* bottom scroll limit */
201 int mode
; /* terminal mode flags */
202 int esc
; /* escape state flags */
203 bool numlock
; /* lock numbers in keyboard */
207 /* Purely graphic info */
213 Atom xembed
, wmdeletewin
;
219 bool isfixed
; /* is fixed geometry? */
220 int fx
, fy
, fw
, fh
; /* fixed geometry */
221 int tw
, th
; /* tty width and height */
222 int w
, h
; /* window width and height */
223 int ch
; /* char height */
224 int cw
; /* char width */
225 char state
; /* focus, redraw, visible */
232 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
233 signed char appkey
; /* application keypad */
234 signed char appcursor
; /* application cursor */
235 signed char crlf
; /* crlf mode */
238 /* TODO: use better name for vars... */
250 struct timeval tclick1
;
251 struct timeval tclick2
;
264 void (*func
)(const Arg
*);
268 /* function definitions used in config.h */
269 static void clippaste(const Arg
*);
270 static void numlock(const Arg
*);
271 static void selpaste(const Arg
*);
272 static void xzoom(const Arg
*);
274 /* Config.h for applying patches and the configuration. */
290 /* Drawing Context */
292 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
293 Font font
, bfont
, ifont
, ibfont
;
297 static void die(const char *, ...);
298 static void draw(void);
299 static void redraw(int);
300 static void drawregion(int, int, int, int);
301 static void execsh(void);
302 static void sigchld(int);
303 static void run(void);
305 static void csidump(void);
306 static void csihandle(void);
307 static void csiparse(void);
308 static void csireset(void);
309 static void strdump(void);
310 static void strhandle(void);
311 static void strparse(void);
312 static void strreset(void);
314 static void tclearregion(int, int, int, int, int);
315 static void tcursor(int);
316 static void tdeletechar(int);
317 static void tdeleteline(int);
318 static void tinsertblank(int);
319 static void tinsertblankline(int);
320 static void tmoveto(int, int);
321 static void tmoveato(int x
, int y
);
322 static void tnew(int, int);
323 static void tnewline(int);
324 static void tputtab(bool);
325 static void tputc(char *, int);
326 static void treset(void);
327 static int tresize(int, int);
328 static void tscrollup(int, int);
329 static void tscrolldown(int, int);
330 static void tsetattr(int*, int);
331 static void tsetchar(char *, Glyph
*, int, int);
332 static void tsetscroll(int, int);
333 static void tswapscreen(void);
334 static void tsetdirt(int, int);
335 static void tsetmode(bool, bool, int *, int);
336 static void tfulldirt(void);
337 static void techo(char *, int);
339 static inline bool match(uint
, uint
);
340 static void ttynew(void);
341 static void ttyread(void);
342 static void ttyresize(void);
343 static void ttywrite(const char *, size_t);
345 static void xdraws(char *, Glyph
, int, int, int, int);
346 static void xhints(void);
347 static void xclear(int, int, int, int);
348 static void xdrawcursor(void);
349 static void xinit(void);
350 static void xloadcols(void);
351 static int xsetcolorname(int, const char *);
352 static int xloadfont(Font
*, FcPattern
*);
353 static void xloadfonts(char *, int);
354 static void xsettitle(char *);
355 static void xresettitle(void);
356 static void xseturgency(int);
357 static void xsetsel(char*);
358 static void xtermclear(int, int, int, int);
359 static void xunloadfonts(void);
360 static void xresize(int, int);
362 static void expose(XEvent
*);
363 static void visibility(XEvent
*);
364 static void unmap(XEvent
*);
365 static char *kmap(KeySym
, uint
);
366 static void kpress(XEvent
*);
367 static void cmessage(XEvent
*);
368 static void cresize(int, int);
369 static void resize(XEvent
*);
370 static void focus(XEvent
*);
371 static void brelease(XEvent
*);
372 static void bpress(XEvent
*);
373 static void bmotion(XEvent
*);
374 static void selnotify(XEvent
*);
375 static void selclear(XEvent
*);
376 static void selrequest(XEvent
*);
378 static void selinit(void);
379 static inline bool selected(int, int);
380 static void selcopy(void);
381 static void selscroll(int, int);
383 static int utf8decode(char *, long *);
384 static int utf8encode(long *, char *);
385 static int utf8size(char *);
386 static int isfullutf8(char *, int);
388 static ssize_t
xwrite(int, char *, size_t);
389 static void *xmalloc(size_t);
390 static void *xrealloc(void *, size_t);
391 static void *xcalloc(size_t, size_t);
393 static void (*handler
[LASTEvent
])(XEvent
*) = {
395 [ClientMessage
] = cmessage
,
396 [ConfigureNotify
] = resize
,
397 [VisibilityNotify
] = visibility
,
398 [UnmapNotify
] = unmap
,
402 [MotionNotify
] = bmotion
,
403 [ButtonPress
] = bpress
,
404 [ButtonRelease
] = brelease
,
405 [SelectionClear
] = selclear
,
406 [SelectionNotify
] = selnotify
,
407 [SelectionRequest
] = selrequest
,
414 static CSIEscape csiescseq
;
415 static STREscape strescseq
;
418 static Selection sel
;
419 static int iofd
= -1;
420 static char **opt_cmd
= NULL
;
421 static char *opt_io
= NULL
;
422 static char *opt_title
= NULL
;
423 static char *opt_embed
= NULL
;
424 static char *opt_class
= NULL
;
425 static char *opt_font
= NULL
;
427 static char *usedfont
= NULL
;
428 static int usedfontsize
= 0;
430 /* Font Ring Cache */
445 * Fontcache is a ring buffer, with frccur as current position and frclen as
446 * the current length of used elements.
449 static Fontcache frc
[1024];
450 static int frccur
= -1, frclen
= 0;
453 xwrite(int fd
, char *s
, size_t len
) {
457 ssize_t r
= write(fd
, s
, len
);
467 xmalloc(size_t len
) {
468 void *p
= malloc(len
);
471 die("Out of memory\n");
477 xrealloc(void *p
, size_t len
) {
478 if((p
= realloc(p
, len
)) == NULL
)
479 die("Out of memory\n");
485 xcalloc(size_t nmemb
, size_t size
) {
486 void *p
= calloc(nmemb
, size
);
489 die("Out of memory\n");
495 utf8decode(char *s
, long *u
) {
501 if(~c
& B7
) { /* 0xxxxxxx */
504 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
505 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
507 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
508 *u
= c
&(B3
|B2
|B1
|B0
);
510 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
517 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
519 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
522 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
525 if((n
== 1 && *u
< 0x80) ||
526 (n
== 2 && *u
< 0x800) ||
527 (n
== 3 && *u
< 0x10000) ||
528 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
540 utf8encode(long *u
, char *s
) {
548 *sp
= uc
; /* 0xxxxxxx */
550 } else if(*u
< 0x800) {
551 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
553 } else if(uc
< 0x10000) {
554 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
556 } else if(uc
<= 0x10FFFF) {
557 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
563 for(i
=n
,++sp
; i
>0; --i
,++sp
)
564 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
576 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
577 UTF-8 otherwise return 0 */
579 isfullutf8(char *s
, int b
) {
587 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
589 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
591 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
593 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
595 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
596 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
609 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
611 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
620 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
621 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
625 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
626 if(sel
.xtarget
== None
)
627 sel
.xtarget
= XA_STRING
;
635 return LIMIT(x
, 0, term
.col
-1);
643 return LIMIT(y
, 0, term
.row
-1);
647 selected(int x
, int y
) {
650 if(sel
.ey
== y
&& sel
.by
== y
) {
651 bx
= MIN(sel
.bx
, sel
.ex
);
652 ex
= MAX(sel
.bx
, sel
.ex
);
653 return BETWEEN(x
, bx
, ex
);
656 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
657 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
658 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
659 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
662 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
663 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
664 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
665 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
666 case SEL_RECTANGULAR
:
667 return ((sel
.b
.y
<= y
&& y
<= sel
.e
.y
)
668 && (sel
.b
.x
<= x
&& x
<= sel
.e
.x
));
673 getbuttoninfo(XEvent
*e
) {
675 uint state
= e
->xbutton
.state
&~Button1Mask
;
677 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
679 sel
.ex
= x2col(e
->xbutton
.x
);
680 sel
.ey
= y2row(e
->xbutton
.y
);
682 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
683 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
684 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
685 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
687 sel
.type
= SEL_REGULAR
;
688 for(type
= 1; type
< LEN(selmasks
); ++type
) {
689 if(match(selmasks
[type
], state
)) {
697 mousereport(XEvent
*e
) {
698 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
699 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
702 static int ob
, ox
, oy
;
705 if(e
->xbutton
.type
== MotionNotify
) {
706 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
710 } else if(!IS_SET(MODE_MOUSESGR
)
711 && (e
->xbutton
.type
== ButtonRelease
712 || button
== AnyButton
)) {
718 if(e
->xbutton
.type
== ButtonPress
) {
724 button
+= (state
& ShiftMask
? 4 : 0)
725 + (state
& Mod4Mask
? 8 : 0)
726 + (state
& ControlMask
? 16 : 0);
729 if(IS_SET(MODE_MOUSESGR
)) {
730 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
732 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
733 } else if(x
< 223 && y
< 223) {
734 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
735 32+button
, 32+x
+1, 32+y
+1);
745 if(IS_SET(MODE_MOUSE
)) {
747 } else if(e
->xbutton
.button
== Button1
) {
750 tsetdirt(sel
.b
.y
, sel
.e
.y
);
754 sel
.type
= SEL_REGULAR
;
755 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
756 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
757 } else if(e
->xbutton
.button
== Button4
) {
759 } else if(e
->xbutton
.button
== Button5
) {
767 int x
, y
, bufsize
, is_selected
= 0, size
;
773 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
774 ptr
= str
= xmalloc(bufsize
);
776 /* append every set & selected glyph to the selection */
777 for(y
= sel
.b
.y
; y
< sel
.e
.y
+ 1; y
++) {
779 gp
= &term
.line
[y
][0];
780 last
= gp
+ term
.col
;
782 while(--last
>= gp
&& !(last
->state
& GLYPH_SET
))
785 for(x
= 0; gp
<= last
; x
++, ++gp
) {
786 if(!selected(x
, y
)) {
792 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
794 memcpy(ptr
, p
, size
);
797 /* \n at the end of every selected line except for the last one */
798 if(is_selected
&& y
< sel
.e
.y
)
807 selnotify(XEvent
*e
) {
808 ulong nitems
, ofs
, rem
;
815 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
816 False
, AnyPropertyType
, &type
, &format
,
817 &nitems
, &rem
, &data
)) {
818 fprintf(stderr
, "Clipboard allocation failed\n");
821 ttywrite((const char *) data
, nitems
* format
/ 8);
823 /* number of 32-bit chunks returned */
824 ofs
+= nitems
* format
/ 32;
829 selpaste(const Arg
*dummy
) {
830 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
831 xw
.win
, CurrentTime
);
835 clippaste(const Arg
*dummy
) {
838 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
839 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
840 xw
.win
, CurrentTime
);
844 selclear(XEvent
*e
) {
848 tsetdirt(sel
.b
.y
, sel
.e
.y
);
852 selrequest(XEvent
*e
) {
853 XSelectionRequestEvent
*xsre
;
855 Atom xa_targets
, string
;
857 xsre
= (XSelectionRequestEvent
*) e
;
858 xev
.type
= SelectionNotify
;
859 xev
.requestor
= xsre
->requestor
;
860 xev
.selection
= xsre
->selection
;
861 xev
.target
= xsre
->target
;
862 xev
.time
= xsre
->time
;
866 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
867 if(xsre
->target
== xa_targets
) {
868 /* respond with the supported type */
869 string
= sel
.xtarget
;
870 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
871 XA_ATOM
, 32, PropModeReplace
,
872 (uchar
*) &string
, 1);
873 xev
.property
= xsre
->property
;
874 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
875 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
876 xsre
->target
, 8, PropModeReplace
,
877 (uchar
*) sel
.clip
, strlen(sel
.clip
));
878 xev
.property
= xsre
->property
;
881 /* all done, send a notification to the listener */
882 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
883 fprintf(stderr
, "Error sending SelectionNotify event\n");
888 /* register the selection for both the clipboard and the primary */
894 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
896 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
897 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
901 brelease(XEvent
*e
) {
904 if(IS_SET(MODE_MOUSE
)) {
909 if(e
->xbutton
.button
== Button2
) {
911 } else if(e
->xbutton
.button
== Button1
) {
914 term
.dirty
[sel
.ey
] = 1;
915 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
917 gettimeofday(&now
, NULL
);
919 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
920 /* triple click on the line */
921 sel
.b
.x
= sel
.bx
= 0;
922 sel
.e
.x
= sel
.ex
= term
.col
;
923 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
925 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
926 /* double click to select word */
928 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
929 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
933 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
934 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
938 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
946 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
947 gettimeofday(&sel
.tclick1
, NULL
);
952 int oldey
, oldex
, oldsby
, oldsey
;
954 if(IS_SET(MODE_MOUSE
)) {
968 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
969 tsetdirt(MIN(sel
.b
.y
, oldsby
), MAX(sel
.e
.y
, oldsey
));
974 die(const char *errstr
, ...) {
977 va_start(ap
, errstr
);
978 vfprintf(stderr
, errstr
, ap
);
986 char *envshell
= getenv("SHELL");
987 const struct passwd
*pass
= getpwuid(getuid());
988 char buf
[sizeof(long) * 8 + 1];
995 setenv("LOGNAME", pass
->pw_name
, 1);
996 setenv("USER", pass
->pw_name
, 1);
997 setenv("SHELL", pass
->pw_shell
, 0);
998 setenv("HOME", pass
->pw_dir
, 0);
1001 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1002 setenv("WINDOWID", buf
, 1);
1004 signal(SIGCHLD
, SIG_DFL
);
1005 signal(SIGHUP
, SIG_DFL
);
1006 signal(SIGINT
, SIG_DFL
);
1007 signal(SIGQUIT
, SIG_DFL
);
1008 signal(SIGTERM
, SIG_DFL
);
1009 signal(SIGALRM
, SIG_DFL
);
1011 DEFAULT(envshell
, shell
);
1012 setenv("TERM", termname
, 1);
1013 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1014 execvp(args
[0], args
);
1022 if(waitpid(pid
, &stat
, 0) < 0)
1023 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1025 if(WIFEXITED(stat
)) {
1026 exit(WEXITSTATUS(stat
));
1035 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1037 /* seems to work fine on linux, openbsd and freebsd */
1038 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1039 die("openpty failed: %s\n", SERRNO
);
1041 switch(pid
= fork()) {
1043 die("fork failed\n");
1046 setsid(); /* create a new process group */
1047 dup2(s
, STDIN_FILENO
);
1048 dup2(s
, STDOUT_FILENO
);
1049 dup2(s
, STDERR_FILENO
);
1050 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1051 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1059 signal(SIGCHLD
, sigchld
);
1061 iofd
= (!strcmp(opt_io
, "-")) ?
1063 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1065 fprintf(stderr
, "Error opening %s:%s\n",
1066 opt_io
, strerror(errno
));
1076 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1078 fprintf(stderr
, "\n");
1083 static char buf
[BUFSIZ
];
1084 static int buflen
= 0;
1087 int charsize
; /* size of utf8 char in bytes */
1091 /* append read bytes to unprocessed bytes */
1092 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1093 die("Couldn't read from shell: %s\n", SERRNO
);
1095 /* process every complete utf8 char */
1098 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1099 charsize
= utf8decode(ptr
, &utf8c
);
1100 utf8encode(&utf8c
, s
);
1106 /* keep any uncomplete utf8 char for the next call */
1107 memmove(buf
, ptr
, buflen
);
1111 ttywrite(const char *s
, size_t n
) {
1112 if(write(cmdfd
, s
, n
) == -1)
1113 die("write error on tty: %s\n", SERRNO
);
1120 w
.ws_row
= term
.row
;
1121 w
.ws_col
= term
.col
;
1122 w
.ws_xpixel
= xw
.tw
;
1123 w
.ws_ypixel
= xw
.th
;
1124 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1125 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1129 tsetdirt(int top
, int bot
) {
1132 LIMIT(top
, 0, term
.row
-1);
1133 LIMIT(bot
, 0, term
.row
-1);
1135 for(i
= top
; i
<= bot
; i
++)
1141 tsetdirt(0, term
.row
-1);
1148 if(mode
== CURSOR_SAVE
) {
1150 } else if(mode
== CURSOR_LOAD
) {
1160 term
.c
= (TCursor
){{
1164 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1166 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1167 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1170 term
.bot
= term
.row
- 1;
1171 term
.mode
= MODE_WRAP
;
1173 tclearregion(0, 0, term
.col
-1, term
.row
-1, 0);
1175 tcursor(CURSOR_SAVE
);
1179 tnew(int col
, int row
) {
1180 /* set screen size */
1183 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1184 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1185 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1186 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1188 for(row
= 0; row
< term
.row
; row
++) {
1189 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1190 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1191 term
.dirty
[row
] = 0;
1195 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1202 Line
*tmp
= term
.line
;
1204 term
.line
= term
.alt
;
1206 term
.mode
^= MODE_ALTSCREEN
;
1211 tscrolldown(int orig
, int n
) {
1215 LIMIT(n
, 0, term
.bot
-orig
+1);
1217 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
, 0);
1219 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1220 temp
= term
.line
[i
];
1221 term
.line
[i
] = term
.line
[i
-n
];
1222 term
.line
[i
-n
] = temp
;
1225 term
.dirty
[i
-n
] = 1;
1232 tscrollup(int orig
, int n
) {
1235 LIMIT(n
, 0, term
.bot
-orig
+1);
1237 tclearregion(0, orig
, term
.col
-1, orig
+n
-1, 0);
1239 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1240 temp
= term
.line
[i
];
1241 term
.line
[i
] = term
.line
[i
+n
];
1242 term
.line
[i
+n
] = temp
;
1245 term
.dirty
[i
+n
] = 1;
1248 selscroll(orig
, -n
);
1252 selscroll(int orig
, int n
) {
1256 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1257 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1263 if(sel
.by
< term
.top
) {
1267 if(sel
.ey
> term
.bot
) {
1272 case SEL_RECTANGULAR
:
1273 if(sel
.by
< term
.top
)
1275 if(sel
.ey
> term
.bot
)
1279 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1280 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1285 tnewline(int first_col
) {
1289 tscrollup(term
.top
, 1);
1293 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1298 /* int noarg = 1; */
1299 char *p
= csiescseq
.buf
, *np
;
1308 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1310 v
= strtol(p
, &np
, 10);
1311 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1313 csiescseq
.arg
[csiescseq
.narg
] = v
;
1317 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1318 csiescseq
.narg
++, p
++;
1320 csiescseq
.mode
= *p
;
1328 /* for absolute user moves, when decom is set */
1330 tmoveato(int x
, int y
) {
1331 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1335 tmoveto(int x
, int y
) {
1338 if(term
.c
.state
& CURSOR_ORIGIN
) {
1343 maxy
= term
.row
- 1;
1345 LIMIT(x
, 0, term
.col
-1);
1346 LIMIT(y
, miny
, maxy
);
1347 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1353 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1354 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1355 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1356 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1357 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1358 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1359 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1360 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1361 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1362 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1366 * The table is proudly stolen from rxvt.
1368 if(attr
->mode
& ATTR_GFX
) {
1369 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1370 && vt100_0
[c
[0] - 0x41]) {
1371 c
= vt100_0
[c
[0] - 0x41];
1376 term
.line
[y
][x
] = *attr
;
1377 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1378 term
.line
[y
][x
].state
|= GLYPH_SET
;
1382 tclearregion(int x1
, int y1
, int x2
, int y2
, int bce
) {
1386 temp
= x1
, x1
= x2
, x2
= temp
;
1388 temp
= y1
, y1
= y2
, y2
= temp
;
1390 LIMIT(x1
, 0, term
.col
-1);
1391 LIMIT(x2
, 0, term
.col
-1);
1392 LIMIT(y1
, 0, term
.row
-1);
1393 LIMIT(y2
, 0, term
.row
-1);
1395 for(y
= y1
; y
<= y2
; y
++) {
1397 for(x
= x1
; x
<= x2
; x
++) {
1399 term
.line
[y
][x
] = term
.c
.attr
;
1400 memcpy(term
.line
[y
][x
].c
, " ", 2);
1401 term
.line
[y
][x
].state
|= GLYPH_SET
;
1403 term
.line
[y
][x
].state
= 0;
1410 tdeletechar(int n
) {
1411 int src
= term
.c
.x
+ n
;
1413 int size
= term
.col
- src
;
1415 term
.dirty
[term
.c
.y
] = 1;
1417 if(src
>= term
.col
) {
1418 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1422 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1423 size
* sizeof(Glyph
));
1424 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1428 tinsertblank(int n
) {
1431 int size
= term
.col
- dst
;
1433 term
.dirty
[term
.c
.y
] = 1;
1435 if(dst
>= term
.col
) {
1436 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 0);
1440 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1441 size
* sizeof(Glyph
));
1442 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
, 0);
1446 tinsertblankline(int n
) {
1447 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1450 tscrolldown(term
.c
.y
, n
);
1454 tdeleteline(int n
) {
1455 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1458 tscrollup(term
.c
.y
, n
);
1462 tsetattr(int *attr
, int l
) {
1465 for(i
= 0; i
< l
; i
++) {
1468 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1469 | ATTR_ITALIC
| ATTR_BLINK
);
1470 term
.c
.attr
.fg
= defaultfg
;
1471 term
.c
.attr
.bg
= defaultbg
;
1474 term
.c
.attr
.mode
|= ATTR_BOLD
;
1477 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1480 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1482 case 5: /* slow blink */
1483 case 6: /* rapid blink */
1484 term
.c
.attr
.mode
|= ATTR_BLINK
;
1487 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1491 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1494 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1497 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1501 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1504 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1507 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1509 if(BETWEEN(attr
[i
], 0, 255)) {
1510 term
.c
.attr
.fg
= attr
[i
];
1513 "erresc: bad fgcolor %d\n",
1518 "erresc(38): gfx attr %d unknown\n",
1523 term
.c
.attr
.fg
= defaultfg
;
1526 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1528 if(BETWEEN(attr
[i
], 0, 255)) {
1529 term
.c
.attr
.bg
= attr
[i
];
1532 "erresc: bad bgcolor %d\n",
1537 "erresc(48): gfx attr %d unknown\n",
1542 term
.c
.attr
.bg
= defaultbg
;
1545 if(BETWEEN(attr
[i
], 30, 37)) {
1546 term
.c
.attr
.fg
= attr
[i
] - 30;
1547 } else if(BETWEEN(attr
[i
], 40, 47)) {
1548 term
.c
.attr
.bg
= attr
[i
] - 40;
1549 } else if(BETWEEN(attr
[i
], 90, 97)) {
1550 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1551 } else if(BETWEEN(attr
[i
], 100, 107)) {
1552 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1555 "erresc(default): gfx attr %d unknown\n",
1556 attr
[i
]), csidump();
1564 tsetscroll(int t
, int b
) {
1567 LIMIT(t
, 0, term
.row
-1);
1568 LIMIT(b
, 0, term
.row
-1);
1578 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1581 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1585 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1589 case 1: /* DECCKM -- Cursor key */
1590 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1592 case 5: /* DECSCNM -- Reverse video */
1594 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1595 if(mode
!= term
.mode
)
1596 redraw(REDRAW_TIMEOUT
);
1598 case 6: /* DECOM -- Origin */
1599 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1602 case 7: /* DECAWM -- Auto wrap */
1603 MODBIT(term
.mode
, set
, MODE_WRAP
);
1605 case 0: /* Error (IGNORED) */
1606 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1607 case 3: /* DECCOLM -- Column (IGNORED) */
1608 case 4: /* DECSCLM -- Scroll (IGNORED) */
1609 case 8: /* DECARM -- Auto repeat (IGNORED) */
1610 case 18: /* DECPFF -- Printer feed (IGNORED) */
1611 case 19: /* DECPEX -- Printer extent (IGNORED) */
1612 case 42: /* DECNRCM -- National characters (IGNORED) */
1613 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1615 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1616 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1618 case 1000: /* 1000,1002: enable xterm mouse report */
1619 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1620 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1623 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1624 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1627 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1629 case 1049: /* = 1047 and 1048 */
1632 alt
= IS_SET(MODE_ALTSCREEN
);
1634 tclearregion(0, 0, term
.col
-1,
1637 if(set
^ alt
) /* set is always 1 or 0 */
1644 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1648 "erresc: unknown private set/reset mode %d\n",
1654 case 0: /* Error (IGNORED) */
1656 case 2: /* KAM -- keyboard action */
1657 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1659 case 4: /* IRM -- Insertion-replacement */
1660 MODBIT(term
.mode
, set
, MODE_INSERT
);
1662 case 12: /* SRM -- Send/Receive */
1663 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1665 case 20: /* LNM -- Linefeed/new line */
1666 MODBIT(term
.mode
, set
, MODE_CRLF
);
1670 "erresc: unknown set/reset mode %d\n",
1682 switch(csiescseq
.mode
) {
1685 fprintf(stderr
, "erresc: unknown csi ");
1689 case '@': /* ICH -- Insert <n> blank char */
1690 DEFAULT(csiescseq
.arg
[0], 1);
1691 tinsertblank(csiescseq
.arg
[0]);
1693 case 'A': /* CUU -- Cursor <n> Up */
1694 DEFAULT(csiescseq
.arg
[0], 1);
1695 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1697 case 'B': /* CUD -- Cursor <n> Down */
1698 case 'e': /* VPR --Cursor <n> Down */
1699 DEFAULT(csiescseq
.arg
[0], 1);
1700 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1702 case 'c': /* DA -- Device Attributes */
1703 if(csiescseq
.arg
[0] == 0)
1704 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1706 case 'C': /* CUF -- Cursor <n> Forward */
1707 case 'a': /* HPR -- Cursor <n> Forward */
1708 DEFAULT(csiescseq
.arg
[0], 1);
1709 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1711 case 'D': /* CUB -- Cursor <n> Backward */
1712 DEFAULT(csiescseq
.arg
[0], 1);
1713 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1715 case 'E': /* CNL -- Cursor <n> Down and first col */
1716 DEFAULT(csiescseq
.arg
[0], 1);
1717 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1719 case 'F': /* CPL -- Cursor <n> Up and first col */
1720 DEFAULT(csiescseq
.arg
[0], 1);
1721 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1723 case 'g': /* TBC -- Tabulation clear */
1724 switch(csiescseq
.arg
[0]) {
1725 case 0: /* clear current tab stop */
1726 term
.tabs
[term
.c
.x
] = 0;
1728 case 3: /* clear all the tabs */
1729 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1735 case 'G': /* CHA -- Move to <col> */
1737 DEFAULT(csiescseq
.arg
[0], 1);
1738 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1740 case 'H': /* CUP -- Move to <row> <col> */
1742 DEFAULT(csiescseq
.arg
[0], 1);
1743 DEFAULT(csiescseq
.arg
[1], 1);
1744 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1746 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1747 DEFAULT(csiescseq
.arg
[0], 1);
1748 while(csiescseq
.arg
[0]--)
1751 case 'J': /* ED -- Clear screen */
1753 switch(csiescseq
.arg
[0]) {
1755 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1756 if(term
.c
.y
< term
.row
-1) {
1757 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1763 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1, 1);
1764 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1767 tclearregion(0, 0, term
.col
-1, term
.row
-1, 1);
1773 case 'K': /* EL -- Clear line */
1774 switch(csiescseq
.arg
[0]) {
1776 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1780 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
, 1);
1783 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
, 1);
1787 case 'S': /* SU -- Scroll <n> line up */
1788 DEFAULT(csiescseq
.arg
[0], 1);
1789 tscrollup(term
.top
, csiescseq
.arg
[0]);
1791 case 'T': /* SD -- Scroll <n> line down */
1792 DEFAULT(csiescseq
.arg
[0], 1);
1793 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1795 case 'L': /* IL -- Insert <n> blank lines */
1796 DEFAULT(csiescseq
.arg
[0], 1);
1797 tinsertblankline(csiescseq
.arg
[0]);
1799 case 'l': /* RM -- Reset Mode */
1800 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1802 case 'M': /* DL -- Delete <n> lines */
1803 DEFAULT(csiescseq
.arg
[0], 1);
1804 tdeleteline(csiescseq
.arg
[0]);
1806 case 'X': /* ECH -- Erase <n> char */
1807 DEFAULT(csiescseq
.arg
[0], 1);
1808 tclearregion(term
.c
.x
, term
.c
.y
,
1809 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
, 1);
1811 case 'P': /* DCH -- Delete <n> char */
1812 DEFAULT(csiescseq
.arg
[0], 1);
1813 tdeletechar(csiescseq
.arg
[0]);
1815 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1816 DEFAULT(csiescseq
.arg
[0], 1);
1817 while(csiescseq
.arg
[0]--)
1820 case 'd': /* VPA -- Move to <row> */
1821 DEFAULT(csiescseq
.arg
[0], 1);
1822 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1824 case 'h': /* SM -- Set terminal mode */
1825 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1827 case 'm': /* SGR -- Terminal attribute (color) */
1828 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1830 case 'r': /* DECSTBM -- Set Scrolling Region */
1831 if(csiescseq
.priv
) {
1834 DEFAULT(csiescseq
.arg
[0], 1);
1835 DEFAULT(csiescseq
.arg
[1], term
.row
);
1836 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1840 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1841 tcursor(CURSOR_SAVE
);
1843 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1844 tcursor(CURSOR_LOAD
);
1855 for(i
= 0; i
< csiescseq
.len
; i
++) {
1856 c
= csiescseq
.buf
[i
] & 0xff;
1859 } else if(c
== '\n') {
1861 } else if(c
== '\r') {
1863 } else if(c
== 0x1b) {
1866 printf("(%02x)", c
);
1874 memset(&csiescseq
, 0, sizeof(csiescseq
));
1883 narg
= strescseq
.narg
;
1885 switch(strescseq
.type
) {
1886 case ']': /* OSC -- Operating System Command */
1887 switch(i
= atoi(strescseq
.args
[0])) {
1892 xsettitle(strescseq
.args
[1]);
1894 case 4: /* color set */
1897 p
= strescseq
.args
[2];
1899 case 104: /* color reset, here p = NULL */
1900 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
1901 if (!xsetcolorname(j
, p
)) {
1902 fprintf(stderr
, "erresc: invalid color %s\n", p
);
1905 * TODO if defaultbg color is changed, borders
1912 fprintf(stderr
, "erresc: unknown str ");
1917 case 'k': /* old title set compatibility */
1918 xsettitle(strescseq
.args
[0]);
1920 case 'P': /* DSC -- Device Control String */
1921 case '_': /* APC -- Application Program Command */
1922 case '^': /* PM -- Privacy Message */
1924 fprintf(stderr
, "erresc: unknown str ");
1933 char *p
= strescseq
.buf
, *np
, *sp
;
1936 np
= strtok_r(strescseq
.buf
, ";", &sp
);
1937 while(p
< strescseq
.buf
+strescseq
.len
&& np
!= NULL
) {
1938 strescseq
.args
[strescseq
.narg
++] = p
;
1940 np
= strtok_r(NULL
, ";", &sp
);
1951 printf("ESC%c", strescseq
.type
);
1952 for(i
= 0; i
< strescseq
.len
; i
++) {
1953 c
= strescseq
.buf
[i
] & 0xff;
1956 } else if(c
== '\n') {
1958 } else if(c
== '\r') {
1960 } else if(c
== 0x1b) {
1963 printf("(%02x)", c
);
1971 memset(&strescseq
, 0, sizeof(strescseq
));
1975 tputtab(bool forward
) {
1981 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1986 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1989 tmoveto(x
, term
.c
.y
);
1993 techo(char *buf
, int len
) {
1994 for(; len
> 0; buf
++, len
--) {
1997 if(c
== '\033') { /* escape */
2000 } else if(c
< '\x20') { /* control code */
2001 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2015 tputc(char *c
, int len
) {
2017 bool control
= ascii
< '\x20' || ascii
== 0177;
2020 if(xwrite(iofd
, c
, len
) < 0) {
2021 fprintf(stderr
, "Error writing in %s:%s\n",
2022 opt_io
, strerror(errno
));
2029 * STR sequences must be checked before anything else
2030 * because it can use some control codes as part of the sequence.
2032 if(term
.esc
& ESC_STR
) {
2035 term
.esc
= ESC_START
| ESC_STR_END
;
2037 case '\a': /* backwards compatibility to xterm */
2042 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
)) {
2043 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2044 strescseq
.len
+= len
;
2047 * Here is a bug in terminals. If the user never sends
2048 * some code to stop the str or esc command, then st
2049 * will stop responding. But this is better than
2050 * silently failing with unknown characters. At least
2051 * then users will report back.
2053 * In the case users ever get fixed, here is the code:
2065 * Actions of control codes must be performed as soon they arrive
2066 * because they can be embedded inside a control sequence, and
2067 * they must not cause conflicts with sequences.
2075 tmoveto(term
.c
.x
-1, term
.c
.y
);
2078 tmoveto(0, term
.c
.y
);
2083 /* go to first col if the mode is set */
2084 tnewline(IS_SET(MODE_CRLF
));
2086 case '\a': /* BEL */
2087 if(!(xw
.state
& WIN_FOCUSED
))
2090 case '\033': /* ESC */
2092 term
.esc
= ESC_START
;
2094 case '\016': /* SO */
2095 case '\017': /* SI */
2097 * Different charsets are hard to handle. Applications
2098 * should use the right alt charset escapes for the
2099 * only reason they still exist: line drawing. The
2100 * rest is incompatible history st should not support.
2103 case '\032': /* SUB */
2104 case '\030': /* CAN */
2107 case '\005': /* ENQ (IGNORED) */
2108 case '\000': /* NUL (IGNORED) */
2109 case '\021': /* XON (IGNORED) */
2110 case '\023': /* XOFF (IGNORED) */
2111 case 0177: /* DEL (IGNORED) */
2114 } else if(term
.esc
& ESC_START
) {
2115 if(term
.esc
& ESC_CSI
) {
2116 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2117 if(BETWEEN(ascii
, 0x40, 0x7E)
2118 || csiescseq
.len
>= ESC_BUF_SIZ
) {
2123 } else if(term
.esc
& ESC_STR_END
) {
2127 } else if(term
.esc
& ESC_ALTCHARSET
) {
2129 case '0': /* Line drawing set */
2130 term
.c
.attr
.mode
|= ATTR_GFX
;
2132 case 'B': /* USASCII */
2133 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2135 case 'A': /* UK (IGNORED) */
2136 case '<': /* multinational charset (IGNORED) */
2137 case '5': /* Finnish (IGNORED) */
2138 case 'C': /* Finnish (IGNORED) */
2139 case 'K': /* German (IGNORED) */
2142 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2145 } else if(term
.esc
& ESC_TEST
) {
2146 if(ascii
== '8') { /* DEC screen alignment test. */
2147 char E
[UTF_SIZ
] = "E";
2150 for(x
= 0; x
< term
.col
; ++x
) {
2151 for(y
= 0; y
< term
.row
; ++y
)
2152 tsetchar(E
, &term
.c
.attr
, x
, y
);
2159 term
.esc
|= ESC_CSI
;
2162 term
.esc
|= ESC_TEST
;
2164 case 'P': /* DCS -- Device Control String */
2165 case '_': /* APC -- Application Program Command */
2166 case '^': /* PM -- Privacy Message */
2167 case ']': /* OSC -- Operating System Command */
2168 case 'k': /* old title set compatibility */
2170 strescseq
.type
= ascii
;
2171 term
.esc
|= ESC_STR
;
2173 case '(': /* set primary charset G0 */
2174 term
.esc
|= ESC_ALTCHARSET
;
2176 case ')': /* set secondary charset G1 (IGNORED) */
2177 case '*': /* set tertiary charset G2 (IGNORED) */
2178 case '+': /* set quaternary charset G3 (IGNORED) */
2181 case 'D': /* IND -- Linefeed */
2182 if(term
.c
.y
== term
.bot
) {
2183 tscrollup(term
.top
, 1);
2185 tmoveto(term
.c
.x
, term
.c
.y
+1);
2189 case 'E': /* NEL -- Next line */
2190 tnewline(1); /* always go to first col */
2193 case 'H': /* HTS -- Horizontal tab stop */
2194 term
.tabs
[term
.c
.x
] = 1;
2197 case 'M': /* RI -- Reverse index */
2198 if(term
.c
.y
== term
.top
) {
2199 tscrolldown(term
.top
, 1);
2201 tmoveto(term
.c
.x
, term
.c
.y
-1);
2205 case 'Z': /* DECID -- Identify Terminal */
2206 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2209 case 'c': /* RIS -- Reset to inital state */
2214 case '=': /* DECPAM -- Application keypad */
2215 term
.mode
|= MODE_APPKEYPAD
;
2218 case '>': /* DECPNM -- Normal keypad */
2219 term
.mode
&= ~MODE_APPKEYPAD
;
2222 case '7': /* DECSC -- Save Cursor */
2223 tcursor(CURSOR_SAVE
);
2226 case '8': /* DECRC -- Restore Cursor */
2227 tcursor(CURSOR_LOAD
);
2230 case '\\': /* ST -- Stop */
2234 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2235 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2240 * All characters which form part of a sequence are not
2246 * Display control codes only if we are in graphic mode
2248 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2250 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2252 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2253 tnewline(1); /* always go to first col */
2255 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2256 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2257 &term
.line
[term
.c
.y
][term
.c
.x
],
2258 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2261 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2262 if(term
.c
.x
+1 < term
.col
) {
2263 tmoveto(term
.c
.x
+1, term
.c
.y
);
2265 term
.c
.state
|= CURSOR_WRAPNEXT
;
2270 tresize(int col
, int row
) {
2272 int minrow
= MIN(row
, term
.row
);
2273 int mincol
= MIN(col
, term
.col
);
2274 int slide
= term
.c
.y
- row
+ 1;
2277 if(col
< 1 || row
< 1)
2280 /* free unneeded rows */
2284 * slide screen to keep cursor where we expect it -
2285 * tscrollup would work here, but we can optimize to
2286 * memmove because we're freeing the earlier lines
2288 for(/* i = 0 */; i
< slide
; i
++) {
2292 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2293 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2295 for(i
+= row
; i
< term
.row
; i
++) {
2300 /* resize to new height */
2301 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2302 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2303 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2304 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2306 /* resize each row to new width, zero-pad if needed */
2307 for(i
= 0; i
< minrow
; i
++) {
2309 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2310 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2311 for(x
= mincol
; x
< col
; x
++) {
2312 term
.line
[i
][x
].state
= 0;
2313 term
.alt
[i
][x
].state
= 0;
2317 /* allocate any new rows */
2318 for(/* i == minrow */; i
< row
; i
++) {
2320 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2321 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2323 if(col
> term
.col
) {
2324 bp
= term
.tabs
+ term
.col
;
2326 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2327 while(--bp
> term
.tabs
&& !*bp
)
2329 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2332 /* update terminal size */
2335 /* reset scrolling region */
2336 tsetscroll(0, row
-1);
2337 /* make use of the LIMIT in tmoveto */
2338 tmoveto(term
.c
.x
, term
.c
.y
);
2344 xresize(int col
, int row
) {
2345 xw
.tw
= MAX(1, col
* xw
.cw
);
2346 xw
.th
= MAX(1, row
* xw
.ch
);
2348 XFreePixmap(xw
.dpy
, xw
.buf
);
2349 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2350 DefaultDepth(xw
.dpy
, xw
.scr
));
2351 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
].pixel
);
2352 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2354 XftDrawChange(xw
.draw
, xw
.buf
);
2357 static inline ushort
2358 sixd_to_16bit(int x
) {
2359 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2365 XRenderColor color
= { .alpha
= 0xffff };
2367 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2368 for(i
= 0; i
< LEN(colorname
); i
++) {
2371 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2372 die("Could not allocate color '%s'\n", colorname
[i
]);
2376 /* load colors [16-255] ; same colors as xterm */
2377 for(i
= 16, r
= 0; r
< 6; r
++) {
2378 for(g
= 0; g
< 6; g
++) {
2379 for(b
= 0; b
< 6; b
++) {
2380 color
.red
= sixd_to_16bit(r
);
2381 color
.green
= sixd_to_16bit(g
);
2382 color
.blue
= sixd_to_16bit(b
);
2383 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2384 die("Could not allocate color %d\n", i
);
2391 for(r
= 0; r
< 24; r
++, i
++) {
2392 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2393 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2395 die("Could not allocate color %d\n", i
);
2401 xsetcolorname(int x
, const char *name
) {
2402 XRenderColor color
= { .alpha
= 0xffff };
2404 if (x
< 0 || x
> LEN(colorname
))
2407 if(16 <= x
&& x
< 16 + 216) {
2408 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2409 color
.red
= sixd_to_16bit(r
);
2410 color
.green
= sixd_to_16bit(g
);
2411 color
.blue
= sixd_to_16bit(b
);
2412 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2413 return 0; /* something went wrong */
2416 } else if (16 + 216 <= x
&& x
< 256) {
2417 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2418 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2419 return 0; /* something went wrong */
2423 name
= colorname
[x
];
2426 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2433 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2434 XftDrawRect(xw
.draw
,
2435 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2436 borderpx
+ col1
* xw
.cw
,
2437 borderpx
+ row1
* xw
.ch
,
2438 (col2
-col1
+1) * xw
.cw
,
2439 (row2
-row1
+1) * xw
.ch
);
2443 * Absolute coordinates.
2446 xclear(int x1
, int y1
, int x2
, int y2
) {
2447 XftDrawRect(xw
.draw
,
2448 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2449 x1
, y1
, x2
-x1
, y2
-y1
);
2454 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2455 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2456 XSizeHints
*sizeh
= NULL
;
2458 sizeh
= XAllocSizeHints();
2459 if(xw
.isfixed
== False
) {
2460 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2461 sizeh
->height
= xw
.h
;
2462 sizeh
->width
= xw
.w
;
2463 sizeh
->height_inc
= xw
.ch
;
2464 sizeh
->width_inc
= xw
.cw
;
2465 sizeh
->base_height
= 2 * borderpx
;
2466 sizeh
->base_width
= 2 * borderpx
;
2468 sizeh
->flags
= PMaxSize
| PMinSize
;
2469 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2470 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2473 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2478 xloadfont(Font
*f
, FcPattern
*pattern
) {
2482 match
= FcFontMatch(NULL
, pattern
, &result
);
2486 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2487 FcPatternDestroy(match
);
2491 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2492 FcPatternDestroy(match
);
2496 f
->pattern
= FcPatternDuplicate(pattern
);
2498 f
->ascent
= f
->match
->ascent
;
2499 f
->descent
= f
->match
->descent
;
2501 f
->rbearing
= f
->match
->max_advance_width
;
2503 f
->height
= f
->match
->height
;
2504 f
->width
= f
->lbearing
+ f
->rbearing
;
2510 xloadfonts(char *fontstr
, int fontsize
) {
2515 if(fontstr
[0] == '-') {
2516 pattern
= XftXlfdParse(fontstr
, False
, False
);
2518 pattern
= FcNameParse((FcChar8
*)fontstr
);
2522 die("st: can't open font %s\n", fontstr
);
2525 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2526 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2527 usedfontsize
= fontsize
;
2529 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2530 if(result
== FcResultMatch
) {
2531 usedfontsize
= (int)fontval
;
2534 * Default font size is 12, if none given. This is to
2535 * have a known usedfontsize value.
2537 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2542 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2543 FcDefaultSubstitute(pattern
);
2545 if(xloadfont(&dc
.font
, pattern
))
2546 die("st: can't open font %s\n", fontstr
);
2548 /* Setting character width and height. */
2549 xw
.cw
= dc
.font
.width
;
2550 xw
.ch
= dc
.font
.height
;
2552 FcPatternDel(pattern
, FC_SLANT
);
2553 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2554 if(xloadfont(&dc
.ifont
, pattern
))
2555 die("st: can't open font %s\n", fontstr
);
2557 FcPatternDel(pattern
, FC_WEIGHT
);
2558 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2559 if(xloadfont(&dc
.ibfont
, pattern
))
2560 die("st: can't open font %s\n", fontstr
);
2562 FcPatternDel(pattern
, FC_SLANT
);
2563 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2564 if(xloadfont(&dc
.bfont
, pattern
))
2565 die("st: can't open font %s\n", fontstr
);
2567 FcPatternDestroy(pattern
);
2571 xunloadfonts(void) {
2575 * Free the loaded fonts in the font cache. This is done backwards
2578 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2581 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2586 XftFontClose(xw
.dpy
, dc
.font
.match
);
2587 FcPatternDestroy(dc
.font
.pattern
);
2588 FcFontSetDestroy(dc
.font
.set
);
2589 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2590 FcPatternDestroy(dc
.bfont
.pattern
);
2591 FcFontSetDestroy(dc
.bfont
.set
);
2592 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2593 FcPatternDestroy(dc
.ifont
.pattern
);
2594 FcFontSetDestroy(dc
.ifont
.set
);
2595 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2596 FcPatternDestroy(dc
.ibfont
.pattern
);
2597 FcFontSetDestroy(dc
.ibfont
.set
);
2601 xzoom(const Arg
*arg
) {
2603 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2610 XSetWindowAttributes attrs
;
2616 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2617 die("Can't open display\n");
2618 xw
.scr
= XDefaultScreen(xw
.dpy
);
2619 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2623 die("Could not init fontconfig.\n");
2625 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2626 xloadfonts(usedfont
, 0);
2629 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2632 /* adjust fixed window geometry */
2634 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2635 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2637 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2639 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2644 /* window - default size */
2645 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2646 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2652 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2653 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2654 attrs
.bit_gravity
= NorthWestGravity
;
2655 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2656 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2657 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2658 attrs
.colormap
= xw
.cmap
;
2660 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2661 XRootWindow(xw
.dpy
, xw
.scr
);
2662 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2663 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2665 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2669 memset(&gcvalues
, 0, sizeof(gcvalues
));
2670 gcvalues
.graphics_exposures
= False
;
2671 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2673 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2674 DefaultDepth(xw
.dpy
, xw
.scr
));
2675 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2676 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2678 /* Xft rendering context */
2679 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2682 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2683 XSetLocaleModifiers("@im=local");
2684 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2685 XSetLocaleModifiers("@im=");
2686 if((xw
.xim
= XOpenIM(xw
.dpy
,
2687 NULL
, NULL
, NULL
)) == NULL
) {
2688 die("XOpenIM failed. Could not open input"
2693 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2694 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2695 XNFocusWindow
, xw
.win
, NULL
);
2697 die("XCreateIC failed. Could not obtain input method.\n");
2699 /* white cursor, black outline */
2700 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2701 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2702 XRecolorCursor(xw
.dpy
, cursor
,
2703 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2704 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2706 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2707 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2708 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2711 XMapWindow(xw
.dpy
, xw
.win
);
2717 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2718 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2719 width
= charlen
* xw
.cw
, xp
, i
;
2721 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2724 Font
*font
= &dc
.font
;
2726 FcPattern
*fcpattern
, *fontpattern
;
2727 FcFontSet
*fcsets
[] = { NULL
};
2728 FcCharSet
*fccharset
;
2729 Colour
*fg
= &dc
.col
[base
.fg
], *bg
= &dc
.col
[base
.bg
],
2730 *temp
, revfg
, revbg
;
2731 XRenderColor colfg
, colbg
;
2733 frcflags
= FRC_NORMAL
;
2735 if(base
.mode
& ATTR_BOLD
) {
2736 if(BETWEEN(base
.fg
, 0, 7)) {
2737 /* basic system colors */
2738 fg
= &dc
.col
[base
.fg
+ 8];
2739 } else if(BETWEEN(base
.fg
, 16, 195)) {
2741 fg
= &dc
.col
[base
.fg
+ 36];
2742 } else if(BETWEEN(base
.fg
, 232, 251)) {
2744 fg
= &dc
.col
[base
.fg
+ 4];
2747 * Those ranges will not be brightened:
2748 * 8 - 15 – bright system colors
2749 * 196 - 231 – highest 256 color cube
2750 * 252 - 255 – brightest colors in greyscale
2753 frcflags
= FRC_BOLD
;
2756 if(base
.mode
& ATTR_ITALIC
) {
2758 frcflags
= FRC_ITALIC
;
2760 if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2762 frcflags
= FRC_ITALICBOLD
;
2765 if(IS_SET(MODE_REVERSE
)) {
2766 if(fg
== &dc
.col
[defaultfg
]) {
2767 fg
= &dc
.col
[defaultbg
];
2769 colfg
.red
= ~fg
->color
.red
;
2770 colfg
.green
= ~fg
->color
.green
;
2771 colfg
.blue
= ~fg
->color
.blue
;
2772 colfg
.alpha
= fg
->color
.alpha
;
2773 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2777 if(bg
== &dc
.col
[defaultbg
]) {
2778 bg
= &dc
.col
[defaultfg
];
2780 colbg
.red
= ~bg
->color
.red
;
2781 colbg
.green
= ~bg
->color
.green
;
2782 colbg
.blue
= ~bg
->color
.blue
;
2783 colbg
.alpha
= bg
->color
.alpha
;
2784 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2789 if(base
.mode
& ATTR_REVERSE
) {
2795 /* Intelligent cleaning up of the borders. */
2797 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2798 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2800 if(x
+ charlen
>= term
.col
) {
2801 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2802 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2805 xclear(winx
, 0, winx
+ width
, borderpx
);
2807 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2809 /* Clean up the region we want to draw to. */
2810 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2812 fcsets
[0] = font
->set
;
2813 for(xp
= winx
; bytelen
> 0;) {
2815 * Search for the range in the to be printed string of glyphs
2816 * that are in the main font. Then print that range. If
2817 * some glyph is found that is not in the font, do the
2825 u8cblen
= utf8decode(s
, &u8char
);
2829 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2830 if(!doesexist
|| bytelen
<= 0) {
2839 XftDrawStringUtf8(xw
.draw
, fg
,
2841 winy
+ font
->ascent
,
2844 xp
+= font
->width
* u8fl
;
2856 /* Search the font cache. */
2857 for(i
= 0; i
< frclen
; i
++, frp
--) {
2861 if(frc
[frp
].c
== u8char
2862 && frc
[frp
].flags
== frcflags
) {
2867 /* Nothing was found. */
2870 * Nothing was found in the cache. Now use
2871 * some dozen of Fontconfig calls to get the
2872 * font for one single character.
2874 fcpattern
= FcPatternDuplicate(font
->pattern
);
2875 fccharset
= FcCharSetCreate();
2877 FcCharSetAddChar(fccharset
, u8char
);
2878 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2880 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2883 FcConfigSubstitute(0, fcpattern
,
2885 FcDefaultSubstitute(fcpattern
);
2887 fontpattern
= FcFontSetMatch(0, fcsets
,
2888 FcTrue
, fcpattern
, &fcres
);
2891 * Overwrite or create the new cache entry
2896 if(frccur
>= LEN(frc
))
2898 if(frclen
> LEN(frc
)) {
2900 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2903 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2905 frc
[frccur
].c
= u8char
;
2906 frc
[frccur
].flags
= frcflags
;
2908 FcPatternDestroy(fcpattern
);
2909 FcCharSetDestroy(fccharset
);
2914 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2915 xp
, winy
+ frc
[frp
].font
->ascent
,
2916 (FcChar8
*)u8c
, u8cblen
);
2922 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2923 winy + font->ascent, (FcChar8 *)s, bytelen);
2926 if(base
.mode
& ATTR_UNDERLINE
) {
2927 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2934 static int oldx
= 0, oldy
= 0;
2936 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
, 0};
2938 LIMIT(oldx
, 0, term
.col
-1);
2939 LIMIT(oldy
, 0, term
.row
-1);
2941 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2942 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2944 /* remove the old cursor */
2945 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2946 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2947 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2950 xtermclear(oldx
, oldy
, oldx
, oldy
);
2953 /* draw the new one */
2954 if(!(IS_SET(MODE_HIDE
))) {
2955 if(!(xw
.state
& WIN_FOCUSED
))
2958 if(IS_SET(MODE_REVERSE
))
2959 g
.mode
|= ATTR_REVERSE
, g
.fg
= defaultcs
, g
.bg
= defaultfg
;
2962 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2963 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2969 xsettitle(char *p
) {
2972 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
2974 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
2979 xsettitle(opt_title
? opt_title
: "st");
2983 redraw(int timeout
) {
2984 struct timespec tv
= {0, timeout
* 1000};
2990 nanosleep(&tv
, NULL
);
2991 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2997 drawregion(0, 0, term
.col
, term
.row
);
2998 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3000 XSetForeground(xw
.dpy
, dc
.gc
,
3001 dc
.col
[IS_SET(MODE_REVERSE
)?
3002 defaultfg
: defaultbg
].pixel
);
3006 drawregion(int x1
, int y1
, int x2
, int y2
) {
3007 int ic
, ib
, x
, y
, ox
, sl
;
3009 char buf
[DRAW_BUF_SIZ
];
3010 bool ena_sel
= sel
.bx
!= -1;
3012 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3015 if(!(xw
.state
& WIN_VISIBLE
))
3018 for(y
= y1
; y
< y2
; y
++) {
3022 xtermclear(0, y
, term
.col
, y
);
3024 base
= term
.line
[y
][0];
3026 for(x
= x1
; x
< x2
; x
++) {
3027 new = term
.line
[y
][x
];
3028 if(ena_sel
&& *(new.c
) && selected(x
, y
))
3029 new.mode
^= ATTR_REVERSE
;
3030 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
3031 || ATTRCMP(base
, new)
3032 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3033 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3036 if(new.state
& GLYPH_SET
) {
3042 sl
= utf8size(new.c
);
3043 memcpy(buf
+ib
, new.c
, sl
);
3049 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3055 expose(XEvent
*ev
) {
3056 XExposeEvent
*e
= &ev
->xexpose
;
3058 if(xw
.state
& WIN_REDRAW
) {
3060 xw
.state
&= ~WIN_REDRAW
;
3066 visibility(XEvent
*ev
) {
3067 XVisibilityEvent
*e
= &ev
->xvisibility
;
3069 if(e
->state
== VisibilityFullyObscured
) {
3070 xw
.state
&= ~WIN_VISIBLE
;
3071 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3072 /* need a full redraw for next Expose, not just a buf copy */
3073 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3079 xw
.state
&= ~WIN_VISIBLE
;
3083 xseturgency(int add
) {
3084 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3086 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3087 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3093 XFocusChangeEvent
*e
= &ev
->xfocus
;
3095 if(e
->mode
== NotifyGrab
)
3098 if(ev
->type
== FocusIn
) {
3099 XSetICFocus(xw
.xic
);
3100 xw
.state
|= WIN_FOCUSED
;
3103 XUnsetICFocus(xw
.xic
);
3104 xw
.state
&= ~WIN_FOCUSED
;
3109 match(uint mask
, uint state
) {
3110 state
&= ~(ignoremod
);
3112 if(mask
== XK_NO_MOD
&& state
)
3114 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3116 if((state
& mask
) != state
)
3122 numlock(const Arg
*dummy
) {
3127 kmap(KeySym k
, uint state
) {
3132 /* Check for mapped keys out of X11 function keys. */
3133 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3134 if(mappedkeys
[i
] == k
)
3137 if(i
== LEN(mappedkeys
)) {
3138 if((k
& 0xFFFF) < 0xFD00)
3142 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3148 if(!match(mask
, state
))
3151 if(kp
->appkey
> 0) {
3152 if(!IS_SET(MODE_APPKEYPAD
))
3154 if(term
.numlock
&& kp
->appkey
== 2)
3156 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3160 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3162 && !IS_SET(MODE_APPCURSOR
))) {
3166 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3167 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3178 kpress(XEvent
*ev
) {
3179 XKeyEvent
*e
= &ev
->xkey
;
3181 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3186 if(IS_SET(MODE_KBDLOCK
))
3189 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3190 e
->state
&= ~Mod2Mask
;
3192 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3193 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3194 bp
->func(&(bp
->arg
));
3199 /* 2. custom keys from config.h */
3200 if((customkey
= kmap(ksym
, e
->state
))) {
3201 len
= strlen(customkey
);
3202 memcpy(buf
, customkey
, len
);
3203 /* 2. hardcoded (overrides X lookup) */
3208 if(len
== 1 && e
->state
& Mod1Mask
)
3211 memcpy(cp
, xstr
, len
);
3212 len
= cp
- buf
+ len
;
3216 if(IS_SET(MODE_ECHO
))
3222 cmessage(XEvent
*e
) {
3225 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3227 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3228 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3229 xw
.state
|= WIN_FOCUSED
;
3231 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3232 xw
.state
&= ~WIN_FOCUSED
;
3234 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3235 /* Send SIGHUP to shell */
3242 cresize(int width
, int height
) {
3250 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3251 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3260 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3263 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3270 int xfd
= XConnectionNumber(xw
.dpy
), xev
;
3271 struct timeval drawtimeout
, *tv
= NULL
, now
, last
;
3273 gettimeofday(&last
, NULL
);
3275 for(xev
= actionfps
;;) {
3277 FD_SET(cmdfd
, &rfd
);
3279 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3282 die("select failed: %s\n", SERRNO
);
3285 gettimeofday(&now
, NULL
);
3286 drawtimeout
.tv_sec
= 0;
3287 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3290 if(FD_ISSET(cmdfd
, &rfd
))
3293 if(FD_ISSET(xfd
, &rfd
))
3296 if(TIMEDIFF(now
, last
) > \
3297 (xev
? (1000/xfps
) : (1000/actionfps
))) {
3298 while(XPending(xw
.dpy
)) {
3299 XNextEvent(xw
.dpy
, &ev
);
3300 if(XFilterEvent(&ev
, None
))
3302 if(handler
[ev
.type
])
3303 (handler
[ev
.type
])(&ev
);
3310 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3312 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
))
3319 main(int argc
, char *argv
[]) {
3320 int i
, bitm
, xr
, yr
;
3323 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3326 for(i
= 1; i
< argc
; i
++) {
3327 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
3330 opt_class
= argv
[i
];
3333 /* eat all remaining arguments */
3345 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
3350 if(bitm
& WidthValue
)
3352 if(bitm
& HeightValue
)
3354 if(bitm
& XNegative
&& xw
.fx
== 0)
3356 if(bitm
& XNegative
&& xw
.fy
== 0)
3359 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3368 opt_title
= argv
[i
];
3375 opt_embed
= argv
[i
];
3381 setlocale(LC_CTYPE
, "");
3382 XSetLocaleModifiers("");