1 /* See LICENSE for licence details. */
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/Xft/Xft.h>
28 #include <fontconfig/fontconfig.h>
36 #define Draw XftDraw *
37 #define Colour XftColor
38 #define Colourmap Colormap
42 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
44 #elif defined(__FreeBSD__) || defined(__DragonFly__)
50 #define XEMBED_FOCUS_IN 4
51 #define XEMBED_FOCUS_OUT 5
55 #define ESC_BUF_SIZ (128*UTF_SIZ)
56 #define ESC_ARG_SIZ 16
57 #define STR_BUF_SIZ ESC_BUF_SIZ
58 #define STR_ARG_SIZ ESC_ARG_SIZ
59 #define DRAW_BUF_SIZ 20*1024
60 #define XK_ANY_MOD UINT_MAX
62 #define XK_SWITCH_MOD (1<<13)
64 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
67 #define SERRNO strerror(errno)
68 #define MIN(a, b) ((a) < (b) ? (a) : (b))
69 #define MAX(a, b) ((a) < (b) ? (b) : (a))
70 #define LEN(a) (sizeof(a) / sizeof(a[0]))
71 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
72 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
73 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
74 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
75 #define IS_SET(flag) ((term.mode & (flag)) != 0)
76 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
78 #define VT102ID "\033[?6c"
80 enum glyph_attribute
{
90 enum cursor_movement
{
108 MODE_MOUSEMOTION
= 64,
114 MODE_APPCURSOR
= 2048,
115 MODE_MOUSESGR
= 4096,
121 ESC_STR
= 4, /* DSC, OSC, PM, APC */
123 ESC_STR_END
= 16, /* a final string was encountered */
124 ESC_TEST
= 32, /* Enter in test mode */
133 enum selection_type
{
140 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
142 typedef unsigned char uchar
;
143 typedef unsigned int uint
;
144 typedef unsigned long ulong
;
145 typedef unsigned short ushort
;
148 char c
[UTF_SIZ
]; /* character code */
149 uchar mode
; /* attribute flags */
150 ushort fg
; /* foreground */
151 ushort bg
; /* background */
157 Glyph attr
; /* current char attributes */
163 /* CSI Escape sequence structs */
164 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
166 char buf
[ESC_BUF_SIZ
]; /* raw string */
167 int len
; /* raw string length */
169 int arg
[ESC_ARG_SIZ
];
170 int narg
; /* nb of args */
174 /* STR Escape sequence structs */
175 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
177 char type
; /* ESC type ... */
178 char buf
[STR_BUF_SIZ
]; /* raw string */
179 int len
; /* raw string length */
180 char *args
[STR_ARG_SIZ
];
181 int narg
; /* nb of args */
184 /* Internal representation of the screen */
186 int row
; /* nb row */
187 int col
; /* nb col */
188 Line
*line
; /* screen */
189 Line
*alt
; /* alternate screen */
190 bool *dirty
; /* dirtyness of lines */
191 TCursor c
; /* cursor */
192 int top
; /* top scroll limit */
193 int bot
; /* bottom scroll limit */
194 int mode
; /* terminal mode flags */
195 int esc
; /* escape state flags */
196 bool numlock
; /* lock numbers in keyboard */
200 /* Purely graphic info */
206 Atom xembed
, wmdeletewin
;
212 bool isfixed
; /* is fixed geometry? */
213 int fx
, fy
, fw
, fh
; /* fixed geometry */
214 int tw
, th
; /* tty width and height */
215 int w
, h
; /* window width and height */
216 int ch
; /* char height */
217 int cw
; /* char width */
218 char state
; /* focus, redraw, visible */
225 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
226 signed char appkey
; /* application keypad */
227 signed char appcursor
; /* application cursor */
228 signed char crlf
; /* crlf mode */
231 /* TODO: use better name for vars... */
243 struct timeval tclick1
;
244 struct timeval tclick2
;
257 void (*func
)(const Arg
*);
261 /* function definitions used in config.h */
262 static void clippaste(const Arg
*);
263 static void numlock(const Arg
*);
264 static void selpaste(const Arg
*);
265 static void xzoom(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);
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 xsetcolorname(int, const char *);
345 static int xloadfont(Font
*, FcPattern
*);
346 static void xloadfonts(char *, int);
347 static void xsettitle(char *);
348 static void xresettitle(void);
349 static void xseturgency(int);
350 static void xsetsel(char*);
351 static void xtermclear(int, int, int, int);
352 static void xunloadfonts(void);
353 static void xresize(int, int);
355 static void expose(XEvent
*);
356 static void visibility(XEvent
*);
357 static void unmap(XEvent
*);
358 static char *kmap(KeySym
, uint
);
359 static void kpress(XEvent
*);
360 static void cmessage(XEvent
*);
361 static void cresize(int, int);
362 static void resize(XEvent
*);
363 static void focus(XEvent
*);
364 static void brelease(XEvent
*);
365 static void bpress(XEvent
*);
366 static void bmotion(XEvent
*);
367 static void selnotify(XEvent
*);
368 static void selclear(XEvent
*);
369 static void selrequest(XEvent
*);
371 static void selinit(void);
372 static inline bool selected(int, int);
373 static void selcopy(void);
374 static void selscroll(int, int);
376 static int utf8decode(char *, long *);
377 static int utf8encode(long *, char *);
378 static int utf8size(char *);
379 static int isfullutf8(char *, int);
381 static ssize_t
xwrite(int, char *, size_t);
382 static void *xmalloc(size_t);
383 static void *xrealloc(void *, size_t);
384 static void *xcalloc(size_t, size_t);
386 static void (*handler
[LASTEvent
])(XEvent
*) = {
388 [ClientMessage
] = cmessage
,
389 [ConfigureNotify
] = resize
,
390 [VisibilityNotify
] = visibility
,
391 [UnmapNotify
] = unmap
,
395 [MotionNotify
] = bmotion
,
396 [ButtonPress
] = bpress
,
397 [ButtonRelease
] = brelease
,
398 [SelectionClear
] = selclear
,
399 [SelectionNotify
] = selnotify
,
400 [SelectionRequest
] = selrequest
,
407 static CSIEscape csiescseq
;
408 static STREscape strescseq
;
411 static Selection sel
;
412 static int iofd
= -1;
413 static char **opt_cmd
= NULL
;
414 static char *opt_io
= NULL
;
415 static char *opt_title
= NULL
;
416 static char *opt_embed
= NULL
;
417 static char *opt_class
= NULL
;
418 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
);
647 return BETWEEN(x
, bx
, ex
);
650 if(sel
.type
== SEL_RECTANGULAR
) {
651 return ((sel
.b
.y
<= y
&& y
<= sel
.e
.y
)
652 && (sel
.b
.x
<= x
&& x
<= sel
.e
.x
));
654 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
655 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
656 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
657 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
661 getbuttoninfo(XEvent
*e
) {
663 uint state
= e
->xbutton
.state
&~Button1Mask
;
665 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
667 sel
.ex
= x2col(e
->xbutton
.x
);
668 sel
.ey
= y2row(e
->xbutton
.y
);
670 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
671 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
672 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
673 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
675 sel
.type
= SEL_REGULAR
;
676 for(type
= 1; type
< LEN(selmasks
); ++type
) {
677 if(match(selmasks
[type
], state
)) {
685 mousereport(XEvent
*e
) {
686 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
687 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
690 static int ob
, ox
, oy
;
693 if(e
->xbutton
.type
== MotionNotify
) {
694 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
698 } else if(!IS_SET(MODE_MOUSESGR
)
699 && (e
->xbutton
.type
== ButtonRelease
700 || button
== AnyButton
)) {
706 if(e
->xbutton
.type
== ButtonPress
) {
712 button
+= (state
& ShiftMask
? 4 : 0)
713 + (state
& Mod4Mask
? 8 : 0)
714 + (state
& ControlMask
? 16 : 0);
717 if(IS_SET(MODE_MOUSESGR
)) {
718 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
720 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
721 } else if(x
< 223 && y
< 223) {
722 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
723 32+button
, 32+x
+1, 32+y
+1);
733 if(IS_SET(MODE_MOUSE
)) {
735 } else if(e
->xbutton
.button
== Button1
) {
738 tsetdirt(sel
.b
.y
, sel
.e
.y
);
742 sel
.type
= SEL_REGULAR
;
743 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
744 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
745 } else if(e
->xbutton
.button
== Button4
) {
747 } else if(e
->xbutton
.button
== Button5
) {
755 int x
, y
, bufsize
, isselected
= 0, size
;
761 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
762 ptr
= str
= xmalloc(bufsize
);
764 /* append every set & selected glyph to the selection */
765 for(y
= sel
.b
.y
; y
< sel
.e
.y
+ 1; y
++) {
767 gp
= &term
.line
[y
][0];
768 last
= gp
+ term
.col
;
770 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
771 strcmp(last
->c
, " ") != 0))
774 for(x
= 0; gp
<= last
; x
++, ++gp
) {
775 if(!selected(x
, y
)) {
781 size
= utf8size(gp
->c
);
782 memcpy(ptr
, gp
->c
, size
);
787 * Copy and pasting of line endings is inconsistent
788 * in the inconsistent terminal and GUI world.
789 * The best solution seems like to produce '\n' when
790 * something is copied from st and convert '\n' to
791 * '\r', when something to be pasted is received by
793 * FIXME: Fix the computer world.
795 if(isselected
&& y
< sel
.e
.y
)
804 selnotify(XEvent
*e
) {
805 ulong nitems
, ofs
, rem
;
807 uchar
*data
, *last
, *repl
;
812 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
813 False
, AnyPropertyType
, &type
, &format
,
814 &nitems
, &rem
, &data
)) {
815 fprintf(stderr
, "Clipboard allocation failed\n");
820 * As seen in selcopy:
821 * Line endings are inconsistent in the terminal and GUI world
822 * copy and pasting. When receiving some selection data,
823 * replace all '\n' with '\r'.
824 * FIXME: Fix the computer world.
827 last
= data
+ nitems
* format
/ 8;
828 while((repl
= memchr(repl
, '\n', last
- repl
))) {
832 ttywrite((const char *)data
, nitems
* format
/ 8);
834 /* number of 32-bit chunks returned */
835 ofs
+= nitems
* format
/ 32;
840 selpaste(const Arg
*dummy
) {
841 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
842 xw
.win
, CurrentTime
);
846 clippaste(const Arg
*dummy
) {
849 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
850 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
851 xw
.win
, CurrentTime
);
855 selclear(XEvent
*e
) {
859 tsetdirt(sel
.b
.y
, sel
.e
.y
);
863 selrequest(XEvent
*e
) {
864 XSelectionRequestEvent
*xsre
;
866 Atom xa_targets
, string
;
868 xsre
= (XSelectionRequestEvent
*) e
;
869 xev
.type
= SelectionNotify
;
870 xev
.requestor
= xsre
->requestor
;
871 xev
.selection
= xsre
->selection
;
872 xev
.target
= xsre
->target
;
873 xev
.time
= xsre
->time
;
877 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
878 if(xsre
->target
== xa_targets
) {
879 /* respond with the supported type */
880 string
= sel
.xtarget
;
881 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
882 XA_ATOM
, 32, PropModeReplace
,
883 (uchar
*) &string
, 1);
884 xev
.property
= xsre
->property
;
885 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
886 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
887 xsre
->target
, 8, PropModeReplace
,
888 (uchar
*) sel
.clip
, strlen(sel
.clip
));
889 xev
.property
= xsre
->property
;
892 /* all done, send a notification to the listener */
893 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
894 fprintf(stderr
, "Error sending SelectionNotify event\n");
899 /* register the selection for both the clipboard and the primary */
905 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
907 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
908 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
912 brelease(XEvent
*e
) {
915 if(IS_SET(MODE_MOUSE
)) {
920 if(e
->xbutton
.button
== Button2
) {
922 } else if(e
->xbutton
.button
== Button1
) {
925 term
.dirty
[sel
.ey
] = 1;
926 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
928 gettimeofday(&now
, NULL
);
930 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
931 /* triple click on the line */
932 sel
.b
.x
= sel
.bx
= 0;
933 sel
.e
.x
= sel
.ex
= term
.col
;
934 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
936 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
937 /* double click to select word */
939 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
943 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
947 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
955 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
956 gettimeofday(&sel
.tclick1
, NULL
);
961 int oldey
, oldex
, oldsby
, oldsey
;
963 if(IS_SET(MODE_MOUSE
)) {
977 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
978 tsetdirt(MIN(sel
.b
.y
, oldsby
), MAX(sel
.e
.y
, oldsey
));
983 die(const char *errstr
, ...) {
986 va_start(ap
, errstr
);
987 vfprintf(stderr
, errstr
, ap
);
995 char *envshell
= getenv("SHELL");
996 const struct passwd
*pass
= getpwuid(getuid());
997 char buf
[sizeof(long) * 8 + 1];
1001 unsetenv("TERMCAP");
1004 setenv("LOGNAME", pass
->pw_name
, 1);
1005 setenv("USER", pass
->pw_name
, 1);
1006 setenv("SHELL", pass
->pw_shell
, 0);
1007 setenv("HOME", pass
->pw_dir
, 0);
1010 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1011 setenv("WINDOWID", buf
, 1);
1013 signal(SIGCHLD
, SIG_DFL
);
1014 signal(SIGHUP
, SIG_DFL
);
1015 signal(SIGINT
, SIG_DFL
);
1016 signal(SIGQUIT
, SIG_DFL
);
1017 signal(SIGTERM
, SIG_DFL
);
1018 signal(SIGALRM
, SIG_DFL
);
1020 DEFAULT(envshell
, shell
);
1021 setenv("TERM", termname
, 1);
1022 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1023 execvp(args
[0], args
);
1031 if(waitpid(pid
, &stat
, 0) < 0)
1032 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1034 if(WIFEXITED(stat
)) {
1035 exit(WEXITSTATUS(stat
));
1044 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1046 /* seems to work fine on linux, openbsd and freebsd */
1047 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1048 die("openpty failed: %s\n", SERRNO
);
1050 switch(pid
= fork()) {
1052 die("fork failed\n");
1055 setsid(); /* create a new process group */
1056 dup2(s
, STDIN_FILENO
);
1057 dup2(s
, STDOUT_FILENO
);
1058 dup2(s
, STDERR_FILENO
);
1059 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1060 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1068 signal(SIGCHLD
, sigchld
);
1070 iofd
= (!strcmp(opt_io
, "-")) ?
1072 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1074 fprintf(stderr
, "Error opening %s:%s\n",
1075 opt_io
, strerror(errno
));
1085 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1087 fprintf(stderr
, "\n");
1092 static char buf
[BUFSIZ
];
1093 static int buflen
= 0;
1096 int charsize
; /* size of utf8 char in bytes */
1100 /* append read bytes to unprocessed bytes */
1101 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1102 die("Couldn't read from shell: %s\n", SERRNO
);
1104 /* process every complete utf8 char */
1107 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1108 charsize
= utf8decode(ptr
, &utf8c
);
1109 utf8encode(&utf8c
, s
);
1115 /* keep any uncomplete utf8 char for the next call */
1116 memmove(buf
, ptr
, buflen
);
1120 ttywrite(const char *s
, size_t n
) {
1121 if(write(cmdfd
, s
, n
) == -1)
1122 die("write error on tty: %s\n", SERRNO
);
1129 w
.ws_row
= term
.row
;
1130 w
.ws_col
= term
.col
;
1131 w
.ws_xpixel
= xw
.tw
;
1132 w
.ws_ypixel
= xw
.th
;
1133 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1134 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1138 tsetdirt(int top
, int bot
) {
1141 LIMIT(top
, 0, term
.row
-1);
1142 LIMIT(bot
, 0, term
.row
-1);
1144 for(i
= top
; i
<= bot
; i
++)
1150 tsetdirt(0, term
.row
-1);
1157 if(mode
== CURSOR_SAVE
) {
1159 } else if(mode
== CURSOR_LOAD
) {
1169 term
.c
= (TCursor
){{
1173 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1175 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1176 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1179 term
.bot
= term
.row
- 1;
1180 term
.mode
= MODE_WRAP
;
1182 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1184 tcursor(CURSOR_SAVE
);
1188 tnew(int col
, int row
) {
1189 memset(&term
, 0, sizeof(Term
));
1198 Line
*tmp
= term
.line
;
1200 term
.line
= term
.alt
;
1202 term
.mode
^= MODE_ALTSCREEN
;
1207 tscrolldown(int orig
, int n
) {
1211 LIMIT(n
, 0, term
.bot
-orig
+1);
1213 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1215 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1216 temp
= term
.line
[i
];
1217 term
.line
[i
] = term
.line
[i
-n
];
1218 term
.line
[i
-n
] = temp
;
1221 term
.dirty
[i
-n
] = 1;
1228 tscrollup(int orig
, int n
) {
1231 LIMIT(n
, 0, term
.bot
-orig
+1);
1233 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1235 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1236 temp
= term
.line
[i
];
1237 term
.line
[i
] = term
.line
[i
+n
];
1238 term
.line
[i
+n
] = temp
;
1241 term
.dirty
[i
+n
] = 1;
1244 selscroll(orig
, -n
);
1248 selscroll(int orig
, int n
) {
1252 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1253 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1257 if(sel
.type
== SEL_RECTANGULAR
) {
1258 if(sel
.by
< term
.top
)
1260 if(sel
.ey
> term
.bot
)
1263 if(sel
.by
< term
.top
) {
1267 if(sel
.ey
> term
.bot
) {
1272 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1273 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1278 tnewline(int first_col
) {
1282 tscrollup(term
.top
, 1);
1286 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1291 char *p
= csiescseq
.buf
, *np
;
1300 csiescseq
.buf
[csiescseq
.len
] = '\0';
1301 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1303 v
= strtol(p
, &np
, 10);
1306 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1308 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1310 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1314 csiescseq
.mode
= *p
;
1317 /* for absolute user moves, when decom is set */
1319 tmoveato(int x
, int y
) {
1320 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1324 tmoveto(int x
, int y
) {
1327 if(term
.c
.state
& CURSOR_ORIGIN
) {
1332 maxy
= term
.row
- 1;
1334 LIMIT(x
, 0, term
.col
-1);
1335 LIMIT(y
, miny
, maxy
);
1336 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1342 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1343 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1344 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1345 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1346 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1347 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1348 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1349 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1350 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1351 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1355 * The table is proudly stolen from rxvt.
1357 if(attr
->mode
& ATTR_GFX
) {
1358 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1359 && vt100_0
[c
[0] - 0x41]) {
1360 c
= vt100_0
[c
[0] - 0x41];
1365 term
.line
[y
][x
] = *attr
;
1366 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1370 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1374 temp
= x1
, x1
= x2
, x2
= temp
;
1376 temp
= y1
, y1
= y2
, y2
= temp
;
1378 LIMIT(x1
, 0, term
.col
-1);
1379 LIMIT(x2
, 0, term
.col
-1);
1380 LIMIT(y1
, 0, term
.row
-1);
1381 LIMIT(y2
, 0, term
.row
-1);
1383 for(y
= y1
; y
<= y2
; y
++) {
1385 for(x
= x1
; x
<= x2
; x
++) {
1386 term
.line
[y
][x
] = term
.c
.attr
;
1387 memcpy(term
.line
[y
][x
].c
, " ", 2);
1393 tdeletechar(int n
) {
1394 int src
= term
.c
.x
+ n
;
1396 int size
= term
.col
- src
;
1398 term
.dirty
[term
.c
.y
] = 1;
1400 if(src
>= term
.col
) {
1401 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1405 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1406 size
* sizeof(Glyph
));
1407 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1411 tinsertblank(int n
) {
1414 int size
= term
.col
- dst
;
1416 term
.dirty
[term
.c
.y
] = 1;
1418 if(dst
>= term
.col
) {
1419 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1423 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1424 size
* sizeof(Glyph
));
1425 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1429 tinsertblankline(int n
) {
1430 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1433 tscrolldown(term
.c
.y
, n
);
1437 tdeleteline(int n
) {
1438 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1441 tscrollup(term
.c
.y
, n
);
1445 tsetattr(int *attr
, int l
) {
1448 for(i
= 0; i
< l
; i
++) {
1451 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1452 | ATTR_BOLD
| ATTR_ITALIC \
1454 term
.c
.attr
.fg
= defaultfg
;
1455 term
.c
.attr
.bg
= defaultbg
;
1458 term
.c
.attr
.mode
|= ATTR_BOLD
;
1461 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1464 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1466 case 5: /* slow blink */
1467 case 6: /* rapid blink */
1468 term
.c
.attr
.mode
|= ATTR_BLINK
;
1471 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1475 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1478 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1481 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1485 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1488 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1491 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1493 if(BETWEEN(attr
[i
], 0, 255)) {
1494 term
.c
.attr
.fg
= attr
[i
];
1497 "erresc: bad fgcolor %d\n",
1502 "erresc(38): gfx attr %d unknown\n",
1507 term
.c
.attr
.fg
= defaultfg
;
1510 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1512 if(BETWEEN(attr
[i
], 0, 255)) {
1513 term
.c
.attr
.bg
= attr
[i
];
1516 "erresc: bad bgcolor %d\n",
1521 "erresc(48): gfx attr %d unknown\n",
1526 term
.c
.attr
.bg
= defaultbg
;
1529 if(BETWEEN(attr
[i
], 30, 37)) {
1530 term
.c
.attr
.fg
= attr
[i
] - 30;
1531 } else if(BETWEEN(attr
[i
], 40, 47)) {
1532 term
.c
.attr
.bg
= attr
[i
] - 40;
1533 } else if(BETWEEN(attr
[i
], 90, 97)) {
1534 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1535 } else if(BETWEEN(attr
[i
], 100, 107)) {
1536 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1539 "erresc(default): gfx attr %d unknown\n",
1540 attr
[i
]), csidump();
1548 tsetscroll(int t
, int b
) {
1551 LIMIT(t
, 0, term
.row
-1);
1552 LIMIT(b
, 0, term
.row
-1);
1562 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1565 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1569 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1573 case 1: /* DECCKM -- Cursor key */
1574 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1576 case 5: /* DECSCNM -- Reverse video */
1578 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1579 if(mode
!= term
.mode
)
1580 redraw(REDRAW_TIMEOUT
);
1582 case 6: /* DECOM -- Origin */
1583 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1586 case 7: /* DECAWM -- Auto wrap */
1587 MODBIT(term
.mode
, set
, MODE_WRAP
);
1589 case 0: /* Error (IGNORED) */
1590 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1591 case 3: /* DECCOLM -- Column (IGNORED) */
1592 case 4: /* DECSCLM -- Scroll (IGNORED) */
1593 case 8: /* DECARM -- Auto repeat (IGNORED) */
1594 case 18: /* DECPFF -- Printer feed (IGNORED) */
1595 case 19: /* DECPEX -- Printer extent (IGNORED) */
1596 case 42: /* DECNRCM -- National characters (IGNORED) */
1597 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1599 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1600 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1602 case 1000: /* 1000,1002: enable xterm mouse report */
1603 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1604 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1607 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1608 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1611 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1613 case 1049: /* = 1047 and 1048 */
1616 if (!allowaltscreen
)
1619 alt
= IS_SET(MODE_ALTSCREEN
);
1621 tclearregion(0, 0, term
.col
-1,
1624 if(set
^ alt
) /* set is always 1 or 0 */
1630 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1634 "erresc: unknown private set/reset mode %d\n",
1640 case 0: /* Error (IGNORED) */
1642 case 2: /* KAM -- keyboard action */
1643 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1645 case 4: /* IRM -- Insertion-replacement */
1646 MODBIT(term
.mode
, set
, MODE_INSERT
);
1648 case 12: /* SRM -- Send/Receive */
1649 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1651 case 20: /* LNM -- Linefeed/new line */
1652 MODBIT(term
.mode
, set
, MODE_CRLF
);
1656 "erresc: unknown set/reset mode %d\n",
1668 switch(csiescseq
.mode
) {
1671 fprintf(stderr
, "erresc: unknown csi ");
1675 case '@': /* ICH -- Insert <n> blank char */
1676 DEFAULT(csiescseq
.arg
[0], 1);
1677 tinsertblank(csiescseq
.arg
[0]);
1679 case 'A': /* CUU -- Cursor <n> Up */
1680 DEFAULT(csiescseq
.arg
[0], 1);
1681 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1683 case 'B': /* CUD -- Cursor <n> Down */
1684 case 'e': /* VPR --Cursor <n> Down */
1685 DEFAULT(csiescseq
.arg
[0], 1);
1686 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1688 case 'c': /* DA -- Device Attributes */
1689 if(csiescseq
.arg
[0] == 0)
1690 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1692 case 'C': /* CUF -- Cursor <n> Forward */
1693 case 'a': /* HPR -- Cursor <n> Forward */
1694 DEFAULT(csiescseq
.arg
[0], 1);
1695 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1697 case 'D': /* CUB -- Cursor <n> Backward */
1698 DEFAULT(csiescseq
.arg
[0], 1);
1699 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1701 case 'E': /* CNL -- Cursor <n> Down and first col */
1702 DEFAULT(csiescseq
.arg
[0], 1);
1703 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1705 case 'F': /* CPL -- Cursor <n> Up and first col */
1706 DEFAULT(csiescseq
.arg
[0], 1);
1707 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1709 case 'g': /* TBC -- Tabulation clear */
1710 switch(csiescseq
.arg
[0]) {
1711 case 0: /* clear current tab stop */
1712 term
.tabs
[term
.c
.x
] = 0;
1714 case 3: /* clear all the tabs */
1715 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1721 case 'G': /* CHA -- Move to <col> */
1723 DEFAULT(csiescseq
.arg
[0], 1);
1724 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1726 case 'H': /* CUP -- Move to <row> <col> */
1728 DEFAULT(csiescseq
.arg
[0], 1);
1729 DEFAULT(csiescseq
.arg
[1], 1);
1730 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1732 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1733 DEFAULT(csiescseq
.arg
[0], 1);
1734 while(csiescseq
.arg
[0]--)
1737 case 'J': /* ED -- Clear screen */
1739 switch(csiescseq
.arg
[0]) {
1741 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1742 if(term
.c
.y
< term
.row
-1) {
1743 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1749 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1750 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1753 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1759 case 'K': /* EL -- Clear line */
1760 switch(csiescseq
.arg
[0]) {
1762 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1766 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1769 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1773 case 'S': /* SU -- Scroll <n> line up */
1774 DEFAULT(csiescseq
.arg
[0], 1);
1775 tscrollup(term
.top
, csiescseq
.arg
[0]);
1777 case 'T': /* SD -- Scroll <n> line down */
1778 DEFAULT(csiescseq
.arg
[0], 1);
1779 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1781 case 'L': /* IL -- Insert <n> blank lines */
1782 DEFAULT(csiescseq
.arg
[0], 1);
1783 tinsertblankline(csiescseq
.arg
[0]);
1785 case 'l': /* RM -- Reset Mode */
1786 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1788 case 'M': /* DL -- Delete <n> lines */
1789 DEFAULT(csiescseq
.arg
[0], 1);
1790 tdeleteline(csiescseq
.arg
[0]);
1792 case 'X': /* ECH -- Erase <n> char */
1793 DEFAULT(csiescseq
.arg
[0], 1);
1794 tclearregion(term
.c
.x
, term
.c
.y
,
1795 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1797 case 'P': /* DCH -- Delete <n> char */
1798 DEFAULT(csiescseq
.arg
[0], 1);
1799 tdeletechar(csiescseq
.arg
[0]);
1801 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1802 DEFAULT(csiescseq
.arg
[0], 1);
1803 while(csiescseq
.arg
[0]--)
1806 case 'd': /* VPA -- Move to <row> */
1807 DEFAULT(csiescseq
.arg
[0], 1);
1808 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1810 case 'h': /* SM -- Set terminal mode */
1811 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1813 case 'm': /* SGR -- Terminal attribute (color) */
1814 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1816 case 'r': /* DECSTBM -- Set Scrolling Region */
1817 if(csiescseq
.priv
) {
1820 DEFAULT(csiescseq
.arg
[0], 1);
1821 DEFAULT(csiescseq
.arg
[1], term
.row
);
1822 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1826 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1827 tcursor(CURSOR_SAVE
);
1829 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1830 tcursor(CURSOR_LOAD
);
1841 for(i
= 0; i
< csiescseq
.len
; i
++) {
1842 c
= csiescseq
.buf
[i
] & 0xff;
1845 } else if(c
== '\n') {
1847 } else if(c
== '\r') {
1849 } else if(c
== 0x1b) {
1852 printf("(%02x)", c
);
1860 memset(&csiescseq
, 0, sizeof(csiescseq
));
1869 narg
= strescseq
.narg
;
1871 switch(strescseq
.type
) {
1872 case ']': /* OSC -- Operating System Command */
1873 switch(i
= atoi(strescseq
.args
[0])) {
1878 xsettitle(strescseq
.args
[1]);
1880 case 4: /* color set */
1883 p
= strescseq
.args
[2];
1885 case 104: /* color reset, here p = NULL */
1886 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
1887 if (!xsetcolorname(j
, p
)) {
1888 fprintf(stderr
, "erresc: invalid color %s\n", p
);
1891 * TODO if defaultbg color is changed, borders
1898 fprintf(stderr
, "erresc: unknown str ");
1903 case 'k': /* old title set compatibility */
1904 xsettitle(strescseq
.args
[0]);
1906 case 'P': /* DSC -- Device Control String */
1907 case '_': /* APC -- Application Program Command */
1908 case '^': /* PM -- Privacy Message */
1910 fprintf(stderr
, "erresc: unknown str ");
1919 char *p
= strescseq
.buf
;
1922 strescseq
.buf
[strescseq
.len
] = '\0';
1923 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
1924 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
1932 printf("ESC%c", strescseq
.type
);
1933 for(i
= 0; i
< strescseq
.len
; i
++) {
1934 c
= strescseq
.buf
[i
] & 0xff;
1937 } else if(isprint(c
)) {
1939 } else if(c
== '\n') {
1941 } else if(c
== '\r') {
1943 } else if(c
== 0x1b) {
1946 printf("(%02x)", c
);
1954 memset(&strescseq
, 0, sizeof(strescseq
));
1958 tputtab(bool forward
) {
1964 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1969 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1972 tmoveto(x
, term
.c
.y
);
1976 techo(char *buf
, int len
) {
1977 for(; len
> 0; buf
++, len
--) {
1980 if(c
== '\033') { /* escape */
1983 } else if(c
< '\x20') { /* control code */
1984 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
1998 tputc(char *c
, int len
) {
2000 bool control
= ascii
< '\x20' || ascii
== 0177;
2003 if(xwrite(iofd
, c
, len
) < 0) {
2004 fprintf(stderr
, "Error writing in %s:%s\n",
2005 opt_io
, strerror(errno
));
2012 * STR sequences must be checked before anything else
2013 * because it can use some control codes as part of the sequence.
2015 if(term
.esc
& ESC_STR
) {
2018 term
.esc
= ESC_START
| ESC_STR_END
;
2020 case '\a': /* backwards compatibility to xterm */
2025 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2026 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2027 strescseq
.len
+= len
;
2030 * Here is a bug in terminals. If the user never sends
2031 * some code to stop the str or esc command, then st
2032 * will stop responding. But this is better than
2033 * silently failing with unknown characters. At least
2034 * then users will report back.
2036 * In the case users ever get fixed, here is the code:
2048 * Actions of control codes must be performed as soon they arrive
2049 * because they can be embedded inside a control sequence, and
2050 * they must not cause conflicts with sequences.
2058 tmoveto(term
.c
.x
-1, term
.c
.y
);
2061 tmoveto(0, term
.c
.y
);
2066 /* go to first col if the mode is set */
2067 tnewline(IS_SET(MODE_CRLF
));
2069 case '\a': /* BEL */
2070 if(!(xw
.state
& WIN_FOCUSED
))
2073 case '\033': /* ESC */
2075 term
.esc
= ESC_START
;
2077 case '\016': /* SO */
2078 case '\017': /* SI */
2080 * Different charsets are hard to handle. Applications
2081 * should use the right alt charset escapes for the
2082 * only reason they still exist: line drawing. The
2083 * rest is incompatible history st should not support.
2086 case '\032': /* SUB */
2087 case '\030': /* CAN */
2090 case '\005': /* ENQ (IGNORED) */
2091 case '\000': /* NUL (IGNORED) */
2092 case '\021': /* XON (IGNORED) */
2093 case '\023': /* XOFF (IGNORED) */
2094 case 0177: /* DEL (IGNORED) */
2097 } else if(term
.esc
& ESC_START
) {
2098 if(term
.esc
& ESC_CSI
) {
2099 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2100 if(BETWEEN(ascii
, 0x40, 0x7E)
2101 || csiescseq
.len
>= \
2102 sizeof(csiescseq
.buf
)-1) {
2107 } else if(term
.esc
& ESC_STR_END
) {
2111 } else if(term
.esc
& ESC_ALTCHARSET
) {
2113 case '0': /* Line drawing set */
2114 term
.c
.attr
.mode
|= ATTR_GFX
;
2116 case 'B': /* USASCII */
2117 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2119 case 'A': /* UK (IGNORED) */
2120 case '<': /* multinational charset (IGNORED) */
2121 case '5': /* Finnish (IGNORED) */
2122 case 'C': /* Finnish (IGNORED) */
2123 case 'K': /* German (IGNORED) */
2126 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2129 } else if(term
.esc
& ESC_TEST
) {
2130 if(ascii
== '8') { /* DEC screen alignment test. */
2131 char E
[UTF_SIZ
] = "E";
2134 for(x
= 0; x
< term
.col
; ++x
) {
2135 for(y
= 0; y
< term
.row
; ++y
)
2136 tsetchar(E
, &term
.c
.attr
, x
, y
);
2143 term
.esc
|= ESC_CSI
;
2146 term
.esc
|= ESC_TEST
;
2148 case 'P': /* DCS -- Device Control String */
2149 case '_': /* APC -- Application Program Command */
2150 case '^': /* PM -- Privacy Message */
2151 case ']': /* OSC -- Operating System Command */
2152 case 'k': /* old title set compatibility */
2154 strescseq
.type
= ascii
;
2155 term
.esc
|= ESC_STR
;
2157 case '(': /* set primary charset G0 */
2158 term
.esc
|= ESC_ALTCHARSET
;
2160 case ')': /* set secondary charset G1 (IGNORED) */
2161 case '*': /* set tertiary charset G2 (IGNORED) */
2162 case '+': /* set quaternary charset G3 (IGNORED) */
2165 case 'D': /* IND -- Linefeed */
2166 if(term
.c
.y
== term
.bot
) {
2167 tscrollup(term
.top
, 1);
2169 tmoveto(term
.c
.x
, term
.c
.y
+1);
2173 case 'E': /* NEL -- Next line */
2174 tnewline(1); /* always go to first col */
2177 case 'H': /* HTS -- Horizontal tab stop */
2178 term
.tabs
[term
.c
.x
] = 1;
2181 case 'M': /* RI -- Reverse index */
2182 if(term
.c
.y
== term
.top
) {
2183 tscrolldown(term
.top
, 1);
2185 tmoveto(term
.c
.x
, term
.c
.y
-1);
2189 case 'Z': /* DECID -- Identify Terminal */
2190 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2193 case 'c': /* RIS -- Reset to inital state */
2198 case '=': /* DECPAM -- Application keypad */
2199 term
.mode
|= MODE_APPKEYPAD
;
2202 case '>': /* DECPNM -- Normal keypad */
2203 term
.mode
&= ~MODE_APPKEYPAD
;
2206 case '7': /* DECSC -- Save Cursor */
2207 tcursor(CURSOR_SAVE
);
2210 case '8': /* DECRC -- Restore Cursor */
2211 tcursor(CURSOR_LOAD
);
2214 case '\\': /* ST -- Stop */
2218 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2219 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2224 * All characters which form part of a sequence are not
2230 * Display control codes only if we are in graphic mode
2232 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2234 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2236 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2237 tnewline(1); /* always go to first col */
2239 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2240 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2241 &term
.line
[term
.c
.y
][term
.c
.x
],
2242 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2245 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2246 if(term
.c
.x
+1 < term
.col
) {
2247 tmoveto(term
.c
.x
+1, term
.c
.y
);
2249 term
.c
.state
|= CURSOR_WRAPNEXT
;
2254 tresize(int col
, int row
) {
2256 int minrow
= MIN(row
, term
.row
);
2257 int mincol
= MIN(col
, term
.col
);
2258 int slide
= term
.c
.y
- row
+ 1;
2262 if(col
< 1 || row
< 1)
2265 /* free unneeded rows */
2269 * slide screen to keep cursor where we expect it -
2270 * tscrollup would work here, but we can optimize to
2271 * memmove because we're freeing the earlier lines
2273 for(/* i = 0 */; i
< slide
; i
++) {
2277 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2278 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2280 for(i
+= row
; i
< term
.row
; i
++) {
2285 /* resize to new height */
2286 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2287 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2288 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2289 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2291 /* resize each row to new width, zero-pad if needed */
2292 for(i
= 0; i
< minrow
; i
++) {
2294 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2295 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2298 /* allocate any new rows */
2299 for(/* i == minrow */; i
< row
; i
++) {
2301 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2302 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2304 if(col
> term
.col
) {
2305 bp
= term
.tabs
+ term
.col
;
2307 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2308 while(--bp
> term
.tabs
&& !*bp
)
2310 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2313 /* update terminal size */
2316 /* reset scrolling region */
2317 tsetscroll(0, row
-1);
2318 /* make use of the LIMIT in tmoveto */
2319 tmoveto(term
.c
.x
, term
.c
.y
);
2320 /* Clearing both screens */
2323 if(mincol
< col
&& 0 < minrow
) {
2324 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2326 if(0 < col
&& minrow
< row
) {
2327 tclearregion(0, minrow
, col
- 1, row
- 1);
2330 } while(orig
!= term
.line
);
2336 xresize(int col
, int row
) {
2337 xw
.tw
= MAX(1, col
* xw
.cw
);
2338 xw
.th
= MAX(1, row
* xw
.ch
);
2340 XFreePixmap(xw
.dpy
, xw
.buf
);
2341 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2342 DefaultDepth(xw
.dpy
, xw
.scr
));
2343 XftDrawChange(xw
.draw
, xw
.buf
);
2344 xclear(0, 0, xw
.w
, xw
.h
);
2347 static inline ushort
2348 sixd_to_16bit(int x
) {
2349 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2355 XRenderColor color
= { .alpha
= 0xffff };
2357 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2358 for(i
= 0; i
< LEN(colorname
); i
++) {
2361 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2362 die("Could not allocate color '%s'\n", colorname
[i
]);
2366 /* load colors [16-255] ; same colors as xterm */
2367 for(i
= 16, r
= 0; r
< 6; r
++) {
2368 for(g
= 0; g
< 6; g
++) {
2369 for(b
= 0; b
< 6; b
++) {
2370 color
.red
= sixd_to_16bit(r
);
2371 color
.green
= sixd_to_16bit(g
);
2372 color
.blue
= sixd_to_16bit(b
);
2373 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2374 die("Could not allocate color %d\n", i
);
2381 for(r
= 0; r
< 24; r
++, i
++) {
2382 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2383 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2385 die("Could not allocate color %d\n", i
);
2391 xsetcolorname(int x
, const char *name
) {
2392 XRenderColor color
= { .alpha
= 0xffff };
2394 if (x
< 0 || x
> LEN(colorname
))
2397 if(16 <= x
&& x
< 16 + 216) {
2398 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2399 color
.red
= sixd_to_16bit(r
);
2400 color
.green
= sixd_to_16bit(g
);
2401 color
.blue
= sixd_to_16bit(b
);
2402 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2403 return 0; /* something went wrong */
2406 } else if (16 + 216 <= x
&& x
< 256) {
2407 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2408 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2409 return 0; /* something went wrong */
2413 name
= colorname
[x
];
2416 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2423 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2424 XftDrawRect(xw
.draw
,
2425 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2426 borderpx
+ col1
* xw
.cw
,
2427 borderpx
+ row1
* xw
.ch
,
2428 (col2
-col1
+1) * xw
.cw
,
2429 (row2
-row1
+1) * xw
.ch
);
2433 * Absolute coordinates.
2436 xclear(int x1
, int y1
, int x2
, int y2
) {
2437 XftDrawRect(xw
.draw
,
2438 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2439 x1
, y1
, x2
-x1
, y2
-y1
);
2444 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2445 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2446 XSizeHints
*sizeh
= NULL
;
2448 sizeh
= XAllocSizeHints();
2449 if(xw
.isfixed
== False
) {
2450 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2451 sizeh
->height
= xw
.h
;
2452 sizeh
->width
= xw
.w
;
2453 sizeh
->height_inc
= xw
.ch
;
2454 sizeh
->width_inc
= xw
.cw
;
2455 sizeh
->base_height
= 2 * borderpx
;
2456 sizeh
->base_width
= 2 * borderpx
;
2458 sizeh
->flags
= PMaxSize
| PMinSize
;
2459 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2460 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2463 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2468 xloadfont(Font
*f
, FcPattern
*pattern
) {
2472 match
= FcFontMatch(NULL
, pattern
, &result
);
2476 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2477 FcPatternDestroy(match
);
2481 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2482 FcPatternDestroy(match
);
2486 f
->pattern
= FcPatternDuplicate(pattern
);
2488 f
->ascent
= f
->match
->ascent
;
2489 f
->descent
= f
->match
->descent
;
2491 f
->rbearing
= f
->match
->max_advance_width
;
2493 f
->height
= f
->ascent
+ f
->descent
;
2494 f
->width
= f
->lbearing
+ f
->rbearing
;
2500 xloadfonts(char *fontstr
, int fontsize
) {
2505 if(fontstr
[0] == '-') {
2506 pattern
= XftXlfdParse(fontstr
, False
, False
);
2508 pattern
= FcNameParse((FcChar8
*)fontstr
);
2512 die("st: can't open font %s\n", fontstr
);
2515 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2516 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2517 usedfontsize
= fontsize
;
2519 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2520 if(result
== FcResultMatch
) {
2521 usedfontsize
= (int)fontval
;
2524 * Default font size is 12, if none given. This is to
2525 * have a known usedfontsize value.
2527 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2532 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2533 FcDefaultSubstitute(pattern
);
2535 if(xloadfont(&dc
.font
, pattern
))
2536 die("st: can't open font %s\n", fontstr
);
2538 /* Setting character width and height. */
2539 xw
.cw
= dc
.font
.width
;
2540 xw
.ch
= dc
.font
.height
;
2542 FcPatternDel(pattern
, FC_SLANT
);
2543 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2544 if(xloadfont(&dc
.ifont
, pattern
))
2545 die("st: can't open font %s\n", fontstr
);
2547 FcPatternDel(pattern
, FC_WEIGHT
);
2548 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2549 if(xloadfont(&dc
.ibfont
, pattern
))
2550 die("st: can't open font %s\n", fontstr
);
2552 FcPatternDel(pattern
, FC_SLANT
);
2553 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2554 if(xloadfont(&dc
.bfont
, pattern
))
2555 die("st: can't open font %s\n", fontstr
);
2557 FcPatternDestroy(pattern
);
2561 xunloadfonts(void) {
2565 * Free the loaded fonts in the font cache. This is done backwards
2568 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2571 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2576 XftFontClose(xw
.dpy
, dc
.font
.match
);
2577 FcPatternDestroy(dc
.font
.pattern
);
2578 FcFontSetDestroy(dc
.font
.set
);
2579 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2580 FcPatternDestroy(dc
.bfont
.pattern
);
2581 FcFontSetDestroy(dc
.bfont
.set
);
2582 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2583 FcPatternDestroy(dc
.ifont
.pattern
);
2584 FcFontSetDestroy(dc
.ifont
.set
);
2585 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2586 FcPatternDestroy(dc
.ibfont
.pattern
);
2587 FcFontSetDestroy(dc
.ibfont
.set
);
2591 xzoom(const Arg
*arg
) {
2593 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2600 XSetWindowAttributes attrs
;
2606 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2607 die("Can't open display\n");
2608 xw
.scr
= XDefaultScreen(xw
.dpy
);
2609 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2613 die("Could not init fontconfig.\n");
2615 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2616 xloadfonts(usedfont
, 0);
2619 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2622 /* adjust fixed window geometry */
2624 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2625 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2627 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2629 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2634 /* window - default size */
2635 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2636 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2642 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2643 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2644 attrs
.bit_gravity
= NorthWestGravity
;
2645 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2646 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2647 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2648 attrs
.colormap
= xw
.cmap
;
2650 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2651 XRootWindow(xw
.dpy
, xw
.scr
);
2652 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2653 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2655 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2659 memset(&gcvalues
, 0, sizeof(gcvalues
));
2660 gcvalues
.graphics_exposures
= False
;
2661 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2663 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2664 DefaultDepth(xw
.dpy
, xw
.scr
));
2665 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2666 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2668 /* Xft rendering context */
2669 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2672 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2673 XSetLocaleModifiers("@im=local");
2674 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2675 XSetLocaleModifiers("@im=");
2676 if((xw
.xim
= XOpenIM(xw
.dpy
,
2677 NULL
, NULL
, NULL
)) == NULL
) {
2678 die("XOpenIM failed. Could not open input"
2683 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2684 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2685 XNFocusWindow
, xw
.win
, NULL
);
2687 die("XCreateIC failed. Could not obtain input method.\n");
2689 /* white cursor, black outline */
2690 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2691 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2692 XRecolorCursor(xw
.dpy
, cursor
,
2693 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2694 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2696 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2697 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2698 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2701 XMapWindow(xw
.dpy
, xw
.win
);
2707 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2708 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2709 width
= charlen
* xw
.cw
, xp
, i
;
2711 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2714 Font
*font
= &dc
.font
;
2716 FcPattern
*fcpattern
, *fontpattern
;
2717 FcFontSet
*fcsets
[] = { NULL
};
2718 FcCharSet
*fccharset
;
2719 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2720 XRenderColor colfg
, colbg
;
2722 frcflags
= FRC_NORMAL
;
2724 if(base
.mode
& ATTR_ITALIC
) {
2725 if(base
.fg
== defaultfg
)
2726 base
.fg
= defaultitalic
;
2728 frcflags
= FRC_ITALIC
;
2729 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2730 if(base
.fg
== defaultfg
)
2731 base
.fg
= defaultitalic
;
2733 frcflags
= FRC_ITALICBOLD
;
2734 } else if(base
.mode
& ATTR_UNDERLINE
) {
2735 if(base
.fg
== defaultfg
)
2736 base
.fg
= defaultunderline
;
2738 fg
= &dc
.col
[base
.fg
];
2739 bg
= &dc
.col
[base
.bg
];
2741 if(base
.mode
& ATTR_BOLD
) {
2742 if(BETWEEN(base
.fg
, 0, 7)) {
2743 /* basic system colors */
2744 fg
= &dc
.col
[base
.fg
+ 8];
2745 } else if(BETWEEN(base
.fg
, 16, 195)) {
2747 fg
= &dc
.col
[base
.fg
+ 36];
2748 } else if(BETWEEN(base
.fg
, 232, 251)) {
2750 fg
= &dc
.col
[base
.fg
+ 4];
2753 * Those ranges will not be brightened:
2754 * 8 - 15 – bright system colors
2755 * 196 - 231 – highest 256 color cube
2756 * 252 - 255 – brightest colors in greyscale
2759 frcflags
= FRC_BOLD
;
2762 if(IS_SET(MODE_REVERSE
)) {
2763 if(fg
== &dc
.col
[defaultfg
]) {
2764 fg
= &dc
.col
[defaultbg
];
2766 colfg
.red
= ~fg
->color
.red
;
2767 colfg
.green
= ~fg
->color
.green
;
2768 colfg
.blue
= ~fg
->color
.blue
;
2769 colfg
.alpha
= fg
->color
.alpha
;
2770 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2774 if(bg
== &dc
.col
[defaultbg
]) {
2775 bg
= &dc
.col
[defaultfg
];
2777 colbg
.red
= ~bg
->color
.red
;
2778 colbg
.green
= ~bg
->color
.green
;
2779 colbg
.blue
= ~bg
->color
.blue
;
2780 colbg
.alpha
= bg
->color
.alpha
;
2781 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2786 if(base
.mode
& ATTR_REVERSE
) {
2792 /* Intelligent cleaning up of the borders. */
2794 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2795 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2797 if(x
+ charlen
>= term
.col
) {
2798 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2799 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2802 xclear(winx
, 0, winx
+ width
, borderpx
);
2804 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2806 /* Clean up the region we want to draw to. */
2807 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2809 fcsets
[0] = font
->set
;
2810 for(xp
= winx
; bytelen
> 0;) {
2812 * Search for the range in the to be printed string of glyphs
2813 * that are in the main font. Then print that range. If
2814 * some glyph is found that is not in the font, do the
2822 u8cblen
= utf8decode(s
, &u8char
);
2826 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2827 if(!doesexist
|| bytelen
<= 0) {
2836 XftDrawStringUtf8(xw
.draw
, fg
,
2838 winy
+ font
->ascent
,
2841 xp
+= font
->width
* u8fl
;
2853 /* Search the font cache. */
2854 for(i
= 0; i
< frclen
; i
++, frp
--) {
2858 if(frc
[frp
].c
== u8char
2859 && frc
[frp
].flags
== frcflags
) {
2864 /* Nothing was found. */
2867 * Nothing was found in the cache. Now use
2868 * some dozen of Fontconfig calls to get the
2869 * font for one single character.
2871 fcpattern
= FcPatternDuplicate(font
->pattern
);
2872 fccharset
= FcCharSetCreate();
2874 FcCharSetAddChar(fccharset
, u8char
);
2875 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2877 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2880 FcConfigSubstitute(0, fcpattern
,
2882 FcDefaultSubstitute(fcpattern
);
2884 fontpattern
= FcFontSetMatch(0, fcsets
,
2885 FcTrue
, fcpattern
, &fcres
);
2888 * Overwrite or create the new cache entry.
2892 if(frccur
>= LEN(frc
))
2894 if(frclen
> LEN(frc
)) {
2896 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2899 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2901 frc
[frccur
].c
= u8char
;
2902 frc
[frccur
].flags
= frcflags
;
2904 FcPatternDestroy(fcpattern
);
2905 FcCharSetDestroy(fccharset
);
2910 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2911 xp
, winy
+ frc
[frp
].font
->ascent
,
2912 (FcChar8
*)u8c
, u8cblen
);
2918 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2919 winy + font->ascent, (FcChar8 *)s, bytelen);
2922 if(base
.mode
& ATTR_UNDERLINE
) {
2923 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2930 static int oldx
= 0, oldy
= 0;
2932 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
2934 LIMIT(oldx
, 0, term
.col
-1);
2935 LIMIT(oldy
, 0, term
.row
-1);
2937 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2939 /* remove the old cursor */
2940 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2941 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2944 /* draw the new one */
2945 if(!(IS_SET(MODE_HIDE
))) {
2946 if(xw
.state
& WIN_FOCUSED
) {
2947 if(IS_SET(MODE_REVERSE
)) {
2948 g
.mode
|= ATTR_REVERSE
;
2954 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2956 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2957 borderpx
+ term
.c
.x
* xw
.cw
,
2958 borderpx
+ term
.c
.y
* xw
.ch
,
2960 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2961 borderpx
+ term
.c
.x
* xw
.cw
,
2962 borderpx
+ term
.c
.y
* xw
.ch
,
2964 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2965 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
2966 borderpx
+ term
.c
.y
* xw
.ch
,
2968 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2969 borderpx
+ term
.c
.x
* xw
.cw
,
2970 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
2973 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2979 xsettitle(char *p
) {
2982 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
2984 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
2989 xsettitle(opt_title
? opt_title
: "st");
2993 redraw(int timeout
) {
2994 struct timespec tv
= {0, timeout
* 1000};
3000 nanosleep(&tv
, NULL
);
3001 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3007 drawregion(0, 0, term
.col
, term
.row
);
3008 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3010 XSetForeground(xw
.dpy
, dc
.gc
,
3011 dc
.col
[IS_SET(MODE_REVERSE
)?
3012 defaultfg
: defaultbg
].pixel
);
3016 drawregion(int x1
, int y1
, int x2
, int y2
) {
3017 int ic
, ib
, x
, y
, ox
, sl
;
3019 char buf
[DRAW_BUF_SIZ
];
3020 bool ena_sel
= sel
.bx
!= -1;
3022 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3025 if(!(xw
.state
& WIN_VISIBLE
))
3028 for(y
= y1
; y
< y2
; y
++) {
3032 xtermclear(0, y
, term
.col
, y
);
3034 base
= term
.line
[y
][0];
3036 for(x
= x1
; x
< x2
; x
++) {
3037 new = term
.line
[y
][x
];
3038 if(ena_sel
&& *(new.c
) && selected(x
, y
))
3039 new.mode
^= ATTR_REVERSE
;
3040 if(ib
> 0 && (ATTRCMP(base
, new)
3041 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3042 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3050 sl
= utf8size(new.c
);
3051 memcpy(buf
+ib
, new.c
, sl
);
3056 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3062 expose(XEvent
*ev
) {
3063 XExposeEvent
*e
= &ev
->xexpose
;
3065 if(xw
.state
& WIN_REDRAW
) {
3067 xw
.state
&= ~WIN_REDRAW
;
3073 visibility(XEvent
*ev
) {
3074 XVisibilityEvent
*e
= &ev
->xvisibility
;
3076 if(e
->state
== VisibilityFullyObscured
) {
3077 xw
.state
&= ~WIN_VISIBLE
;
3078 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3079 /* need a full redraw for next Expose, not just a buf copy */
3080 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3086 xw
.state
&= ~WIN_VISIBLE
;
3090 xseturgency(int add
) {
3091 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3093 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3094 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3100 XFocusChangeEvent
*e
= &ev
->xfocus
;
3102 if(e
->mode
== NotifyGrab
)
3105 if(ev
->type
== FocusIn
) {
3106 XSetICFocus(xw
.xic
);
3107 xw
.state
|= WIN_FOCUSED
;
3110 XUnsetICFocus(xw
.xic
);
3111 xw
.state
&= ~WIN_FOCUSED
;
3116 match(uint mask
, uint state
) {
3117 state
&= ~(ignoremod
);
3119 if(mask
== XK_NO_MOD
&& state
)
3121 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3123 if((state
& mask
) != state
)
3129 numlock(const Arg
*dummy
) {
3134 kmap(KeySym k
, uint state
) {
3139 /* Check for mapped keys out of X11 function keys. */
3140 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3141 if(mappedkeys
[i
] == k
)
3144 if(i
== LEN(mappedkeys
)) {
3145 if((k
& 0xFFFF) < 0xFD00)
3149 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3155 if(!match(mask
, state
))
3158 if(kp
->appkey
> 0) {
3159 if(!IS_SET(MODE_APPKEYPAD
))
3161 if(term
.numlock
&& kp
->appkey
== 2)
3163 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3167 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3169 && !IS_SET(MODE_APPCURSOR
))) {
3173 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3174 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3185 kpress(XEvent
*ev
) {
3186 XKeyEvent
*e
= &ev
->xkey
;
3188 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3193 if(IS_SET(MODE_KBDLOCK
))
3196 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3197 e
->state
&= ~Mod2Mask
;
3199 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3200 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3201 bp
->func(&(bp
->arg
));
3206 /* 2. custom keys from config.h */
3207 if((customkey
= kmap(ksym
, e
->state
))) {
3208 len
= strlen(customkey
);
3209 memcpy(buf
, customkey
, len
);
3210 /* 2. hardcoded (overrides X lookup) */
3215 if(len
== 1 && e
->state
& Mod1Mask
)
3218 memcpy(cp
, xstr
, len
);
3219 len
= cp
- buf
+ len
;
3223 if(IS_SET(MODE_ECHO
))
3229 cmessage(XEvent
*e
) {
3232 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3234 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3235 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3236 xw
.state
|= WIN_FOCUSED
;
3238 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3239 xw
.state
&= ~WIN_FOCUSED
;
3241 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3242 /* Send SIGHUP to shell */
3249 cresize(int width
, int height
) {
3257 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3258 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3267 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3270 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3277 int xfd
= XConnectionNumber(xw
.dpy
), xev
;
3278 struct timeval drawtimeout
, *tv
= NULL
, now
, last
;
3280 gettimeofday(&last
, NULL
);
3282 for(xev
= actionfps
;;) {
3284 FD_SET(cmdfd
, &rfd
);
3286 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3289 die("select failed: %s\n", SERRNO
);
3292 gettimeofday(&now
, NULL
);
3293 drawtimeout
.tv_sec
= 0;
3294 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3297 if(FD_ISSET(cmdfd
, &rfd
))
3300 if(FD_ISSET(xfd
, &rfd
))
3303 if(TIMEDIFF(now
, last
) > \
3304 (xev
? (1000/xfps
) : (1000/actionfps
))) {
3305 while(XPending(xw
.dpy
)) {
3306 XNextEvent(xw
.dpy
, &ev
);
3307 if(XFilterEvent(&ev
, None
))
3309 if(handler
[ev
.type
])
3310 (handler
[ev
.type
])(&ev
);
3317 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3319 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
))
3327 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3328 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3329 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3333 main(int argc
, char *argv
[]) {
3337 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3342 allowaltscreen
= false;
3345 opt_class
= EARGF(usage());
3348 /* eat all remaining arguments */
3353 opt_font
= EARGF(usage());
3356 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3361 if(bitm
& WidthValue
)
3363 if(bitm
& HeightValue
)
3365 if(bitm
& XNegative
&& xw
.fx
== 0)
3367 if(bitm
& XNegative
&& xw
.fy
== 0)
3370 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3374 opt_io
= EARGF(usage());
3377 opt_title
= EARGF(usage());
3380 opt_embed
= EARGF(usage());
3388 setlocale(LC_CTYPE
, "");
3389 XSetLocaleModifiers("");
3395 cresize(xw
.h
, xw
.w
);