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
{
113 MODE_MOUSEMOTION
= 64,
119 MODE_APPCURSOR
= 2048,
120 MODE_MOUSESGR
= 4096,
126 ESC_STR
= 4, /* DSC, OSC, PM, APC */
128 ESC_STR_END
= 16, /* a final string was encountered */
129 ESC_TEST
= 32, /* Enter in test mode */
138 enum selection_type
{
145 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
147 typedef unsigned char uchar
;
148 typedef unsigned int uint
;
149 typedef unsigned long ulong
;
150 typedef unsigned short ushort
;
153 char c
[UTF_SIZ
]; /* character code */
154 uchar mode
; /* attribute flags */
155 ushort fg
; /* foreground */
156 ushort bg
; /* background */
157 uchar state
; /* state flags */
163 Glyph attr
; /* current char attributes */
169 /* CSI Escape sequence structs */
170 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
172 char buf
[ESC_BUF_SIZ
]; /* raw string */
173 int len
; /* raw string length */
175 int arg
[ESC_ARG_SIZ
];
176 int narg
; /* nb of args */
180 /* STR Escape sequence structs */
181 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
183 char type
; /* ESC type ... */
184 char buf
[STR_BUF_SIZ
]; /* raw string */
185 int len
; /* raw string length */
186 char *args
[STR_ARG_SIZ
];
187 int narg
; /* nb of args */
190 /* Internal representation of the screen */
192 int row
; /* nb row */
193 int col
; /* nb col */
194 Line
*line
; /* screen */
195 Line
*alt
; /* alternate screen */
196 bool *dirty
; /* dirtyness of lines */
197 TCursor c
; /* cursor */
198 int top
; /* top scroll limit */
199 int bot
; /* bottom scroll limit */
200 int mode
; /* terminal mode flags */
201 int esc
; /* escape state flags */
202 bool numlock
; /* lock numbers in keyboard */
206 /* Purely graphic info */
212 Atom xembed
, wmdeletewin
;
218 bool isfixed
; /* is fixed geometry? */
219 int fx
, fy
, fw
, fh
; /* fixed geometry */
220 int tw
, th
; /* tty width and height */
221 int w
, h
; /* window width and height */
222 int ch
; /* char height */
223 int cw
; /* char width */
224 char state
; /* focus, redraw, visible */
231 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
232 signed char appkey
; /* application keypad */
233 signed char appcursor
; /* application cursor */
234 signed char crlf
; /* crlf mode */
237 /* TODO: use better name for vars... */
249 struct timeval tclick1
;
250 struct timeval tclick2
;
263 void (*func
)(const Arg
*);
267 /* function definitions used in config.h */
268 static void clippaste(const Arg
*);
269 static void numlock(const Arg
*);
270 static void selpaste(const Arg
*);
271 static void xzoom(const Arg
*);
273 /* Config.h for applying patches and the configuration. */
289 /* Drawing Context */
291 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
292 Font font
, bfont
, ifont
, ibfont
;
296 static void die(const char *, ...);
297 static void draw(void);
298 static void redraw(int);
299 static void drawregion(int, int, int, int);
300 static void execsh(void);
301 static void sigchld(int);
302 static void run(void);
304 static void csidump(void);
305 static void csihandle(void);
306 static void csiparse(void);
307 static void csireset(void);
308 static void strdump(void);
309 static void strhandle(void);
310 static void strparse(void);
311 static void strreset(void);
313 static void tclearregion(int, int, int, int);
314 static void tcursor(int);
315 static void tdeletechar(int);
316 static void tdeleteline(int);
317 static void tinsertblank(int);
318 static void tinsertblankline(int);
319 static void tmoveto(int, int);
320 static void tmoveato(int x
, int y
);
321 static void tnew(int, int);
322 static void tnewline(int);
323 static void tputtab(bool);
324 static void tputc(char *, int);
325 static void treset(void);
326 static int tresize(int, int);
327 static void tscrollup(int, int);
328 static void tscrolldown(int, int);
329 static void tsetattr(int*, int);
330 static void tsetchar(char *, Glyph
*, int, int);
331 static void tsetscroll(int, int);
332 static void tswapscreen(void);
333 static void tsetdirt(int, int);
334 static void tsetmode(bool, bool, int *, int);
335 static void tfulldirt(void);
336 static void techo(char *, int);
338 static inline bool match(uint
, uint
);
339 static void ttynew(void);
340 static void ttyread(void);
341 static void ttyresize(void);
342 static void ttywrite(const char *, size_t);
344 static void xdraws(char *, Glyph
, int, int, int, int);
345 static void xhints(void);
346 static void xclear(int, int, int, int);
347 static void xdrawcursor(void);
348 static void xinit(void);
349 static void xloadcols(void);
350 static int xsetcolorname(int, const char *);
351 static int xloadfont(Font
*, FcPattern
*);
352 static void xloadfonts(char *, int);
353 static void xsettitle(char *);
354 static void xresettitle(void);
355 static void xseturgency(int);
356 static void xsetsel(char*);
357 static void xtermclear(int, int, int, int);
358 static void xunloadfonts(void);
359 static void xresize(int, int);
361 static void expose(XEvent
*);
362 static void visibility(XEvent
*);
363 static void unmap(XEvent
*);
364 static char *kmap(KeySym
, uint
);
365 static void kpress(XEvent
*);
366 static void cmessage(XEvent
*);
367 static void cresize(int, int);
368 static void resize(XEvent
*);
369 static void focus(XEvent
*);
370 static void brelease(XEvent
*);
371 static void bpress(XEvent
*);
372 static void bmotion(XEvent
*);
373 static void selnotify(XEvent
*);
374 static void selclear(XEvent
*);
375 static void selrequest(XEvent
*);
377 static void selinit(void);
378 static inline bool selected(int, int);
379 static void selcopy(void);
380 static void selscroll(int, int);
382 static int utf8decode(char *, long *);
383 static int utf8encode(long *, char *);
384 static int utf8size(char *);
385 static int isfullutf8(char *, int);
387 static ssize_t
xwrite(int, char *, size_t);
388 static void *xmalloc(size_t);
389 static void *xrealloc(void *, size_t);
390 static void *xcalloc(size_t, size_t);
392 static void (*handler
[LASTEvent
])(XEvent
*) = {
394 [ClientMessage
] = cmessage
,
395 [ConfigureNotify
] = resize
,
396 [VisibilityNotify
] = visibility
,
397 [UnmapNotify
] = unmap
,
401 [MotionNotify
] = bmotion
,
402 [ButtonPress
] = bpress
,
403 [ButtonRelease
] = brelease
,
404 [SelectionClear
] = selclear
,
405 [SelectionNotify
] = selnotify
,
406 [SelectionRequest
] = selrequest
,
413 static CSIEscape csiescseq
;
414 static STREscape strescseq
;
417 static Selection sel
;
418 static int iofd
= -1;
419 static char **opt_cmd
= NULL
;
420 static char *opt_io
= NULL
;
421 static char *opt_title
= NULL
;
422 static char *opt_embed
= NULL
;
423 static char *opt_class
= NULL
;
424 static char *opt_font
= NULL
;
426 static char *usedfont
= NULL
;
427 static int usedfontsize
= 0;
429 /* Font Ring Cache */
444 * Fontcache is a ring buffer, with frccur as current position and frclen as
445 * the current length of used elements.
448 static Fontcache frc
[1024];
449 static int frccur
= -1, frclen
= 0;
452 xwrite(int fd
, char *s
, size_t len
) {
456 ssize_t r
= write(fd
, s
, len
);
466 xmalloc(size_t len
) {
467 void *p
= malloc(len
);
470 die("Out of memory\n");
476 xrealloc(void *p
, size_t len
) {
477 if((p
= realloc(p
, len
)) == NULL
)
478 die("Out of memory\n");
484 xcalloc(size_t nmemb
, size_t size
) {
485 void *p
= calloc(nmemb
, size
);
488 die("Out of memory\n");
494 utf8decode(char *s
, long *u
) {
500 if(~c
& B7
) { /* 0xxxxxxx */
503 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
504 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
506 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
507 *u
= c
&(B3
|B2
|B1
|B0
);
509 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
516 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
518 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
521 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
524 if((n
== 1 && *u
< 0x80) ||
525 (n
== 2 && *u
< 0x800) ||
526 (n
== 3 && *u
< 0x10000) ||
527 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
539 utf8encode(long *u
, char *s
) {
547 *sp
= uc
; /* 0xxxxxxx */
549 } else if(*u
< 0x800) {
550 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
552 } else if(uc
< 0x10000) {
553 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
555 } else if(uc
<= 0x10FFFF) {
556 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
562 for(i
=n
,++sp
; i
>0; --i
,++sp
)
563 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
575 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
576 UTF-8 otherwise return 0 */
578 isfullutf8(char *s
, int b
) {
586 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
588 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
590 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
592 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
594 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
595 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
608 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
610 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
619 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
620 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
624 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
625 if(sel
.xtarget
== None
)
626 sel
.xtarget
= XA_STRING
;
634 return LIMIT(x
, 0, term
.col
-1);
642 return LIMIT(y
, 0, term
.row
-1);
646 selected(int x
, int y
) {
649 if(sel
.ey
== y
&& sel
.by
== y
) {
650 bx
= MIN(sel
.bx
, sel
.ex
);
651 ex
= MAX(sel
.bx
, sel
.ex
);
653 return BETWEEN(x
, bx
, ex
);
656 if(sel
.type
== SEL_RECTANGULAR
) {
657 return ((sel
.b
.y
<= y
&& y
<= sel
.e
.y
)
658 && (sel
.b
.x
<= x
&& x
<= sel
.e
.x
));
660 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
661 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
662 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
663 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
667 getbuttoninfo(XEvent
*e
) {
669 uint state
= e
->xbutton
.state
&~Button1Mask
;
671 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
673 sel
.ex
= x2col(e
->xbutton
.x
);
674 sel
.ey
= y2row(e
->xbutton
.y
);
676 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
677 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
678 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
679 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
681 sel
.type
= SEL_REGULAR
;
682 for(type
= 1; type
< LEN(selmasks
); ++type
) {
683 if(match(selmasks
[type
], state
)) {
691 mousereport(XEvent
*e
) {
692 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
693 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
696 static int ob
, ox
, oy
;
699 if(e
->xbutton
.type
== MotionNotify
) {
700 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
704 } else if(!IS_SET(MODE_MOUSESGR
)
705 && (e
->xbutton
.type
== ButtonRelease
706 || button
== AnyButton
)) {
712 if(e
->xbutton
.type
== ButtonPress
) {
718 button
+= (state
& ShiftMask
? 4 : 0)
719 + (state
& Mod4Mask
? 8 : 0)
720 + (state
& ControlMask
? 16 : 0);
723 if(IS_SET(MODE_MOUSESGR
)) {
724 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
726 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
727 } else if(x
< 223 && y
< 223) {
728 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
729 32+button
, 32+x
+1, 32+y
+1);
739 if(IS_SET(MODE_MOUSE
)) {
741 } else if(e
->xbutton
.button
== Button1
) {
744 tsetdirt(sel
.b
.y
, sel
.e
.y
);
748 sel
.type
= SEL_REGULAR
;
749 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
750 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
751 } else if(e
->xbutton
.button
== Button4
) {
753 } else if(e
->xbutton
.button
== Button5
) {
761 int x
, y
, bufsize
, isselected
= 0, size
;
767 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
768 ptr
= str
= xmalloc(bufsize
);
770 /* append every set & selected glyph to the selection */
771 for(y
= sel
.b
.y
; y
< sel
.e
.y
+ 1; y
++) {
773 gp
= &term
.line
[y
][0];
774 last
= gp
+ term
.col
;
776 while(--last
>= gp
&& !((last
->state
& GLYPH_SET
) && \
777 selected(last
- gp
, y
) && strcmp(last
->c
, " ") != 0))
780 for(x
= 0; gp
<= last
; x
++, ++gp
) {
781 if(!selected(x
, y
)) {
787 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
789 memcpy(ptr
, p
, size
);
794 * Copy and pasting of line endings is inconsistent
795 * in the inconsistent terminal and GUI world.
796 * The best solution seems like to produce '\n' when
797 * something is copied from st and convert '\n' to
798 * '\r', when something to be pasted is received by
800 * FIXME: Fix the computer world.
802 if(isselected
&& y
< sel
.e
.y
)
811 selnotify(XEvent
*e
) {
812 ulong nitems
, ofs
, rem
;
814 uchar
*data
, *last
, *repl
;
819 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
820 False
, AnyPropertyType
, &type
, &format
,
821 &nitems
, &rem
, &data
)) {
822 fprintf(stderr
, "Clipboard allocation failed\n");
827 * As seen in selcopy:
828 * Line endings are inconsistent in the terminal and GUI world
829 * copy and pasting. When receiving some selection data,
830 * replace all '\n' with '\r'.
831 * FIXME: Fix the computer world.
834 last
= data
+ nitems
* format
/ 8;
835 while((repl
= memchr(repl
, '\n', last
- repl
))) {
839 ttywrite((const char *)data
, nitems
* format
/ 8);
841 /* number of 32-bit chunks returned */
842 ofs
+= nitems
* format
/ 32;
847 selpaste(const Arg
*dummy
) {
848 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
849 xw
.win
, CurrentTime
);
853 clippaste(const Arg
*dummy
) {
856 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
857 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
858 xw
.win
, CurrentTime
);
862 selclear(XEvent
*e
) {
866 tsetdirt(sel
.b
.y
, sel
.e
.y
);
870 selrequest(XEvent
*e
) {
871 XSelectionRequestEvent
*xsre
;
873 Atom xa_targets
, string
;
875 xsre
= (XSelectionRequestEvent
*) e
;
876 xev
.type
= SelectionNotify
;
877 xev
.requestor
= xsre
->requestor
;
878 xev
.selection
= xsre
->selection
;
879 xev
.target
= xsre
->target
;
880 xev
.time
= xsre
->time
;
884 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
885 if(xsre
->target
== xa_targets
) {
886 /* respond with the supported type */
887 string
= sel
.xtarget
;
888 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
889 XA_ATOM
, 32, PropModeReplace
,
890 (uchar
*) &string
, 1);
891 xev
.property
= xsre
->property
;
892 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
893 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
894 xsre
->target
, 8, PropModeReplace
,
895 (uchar
*) sel
.clip
, strlen(sel
.clip
));
896 xev
.property
= xsre
->property
;
899 /* all done, send a notification to the listener */
900 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
901 fprintf(stderr
, "Error sending SelectionNotify event\n");
906 /* register the selection for both the clipboard and the primary */
912 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
914 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
915 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
919 brelease(XEvent
*e
) {
922 if(IS_SET(MODE_MOUSE
)) {
927 if(e
->xbutton
.button
== Button2
) {
929 } else if(e
->xbutton
.button
== Button1
) {
932 term
.dirty
[sel
.ey
] = 1;
933 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
935 gettimeofday(&now
, NULL
);
937 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
938 /* triple click on the line */
939 sel
.b
.x
= sel
.bx
= 0;
940 sel
.e
.x
= sel
.ex
= term
.col
;
941 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
943 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
944 /* double click to select word */
946 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
947 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
951 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
952 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
956 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
964 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
965 gettimeofday(&sel
.tclick1
, NULL
);
970 int oldey
, oldex
, oldsby
, oldsey
;
972 if(IS_SET(MODE_MOUSE
)) {
986 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
987 tsetdirt(MIN(sel
.b
.y
, oldsby
), MAX(sel
.e
.y
, oldsey
));
992 die(const char *errstr
, ...) {
995 va_start(ap
, errstr
);
996 vfprintf(stderr
, errstr
, ap
);
1004 char *envshell
= getenv("SHELL");
1005 const struct passwd
*pass
= getpwuid(getuid());
1006 char buf
[sizeof(long) * 8 + 1];
1008 unsetenv("COLUMNS");
1010 unsetenv("TERMCAP");
1013 setenv("LOGNAME", pass
->pw_name
, 1);
1014 setenv("USER", pass
->pw_name
, 1);
1015 setenv("SHELL", pass
->pw_shell
, 0);
1016 setenv("HOME", pass
->pw_dir
, 0);
1019 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1020 setenv("WINDOWID", buf
, 1);
1022 signal(SIGCHLD
, SIG_DFL
);
1023 signal(SIGHUP
, SIG_DFL
);
1024 signal(SIGINT
, SIG_DFL
);
1025 signal(SIGQUIT
, SIG_DFL
);
1026 signal(SIGTERM
, SIG_DFL
);
1027 signal(SIGALRM
, SIG_DFL
);
1029 DEFAULT(envshell
, shell
);
1030 setenv("TERM", termname
, 1);
1031 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1032 execvp(args
[0], args
);
1040 if(waitpid(pid
, &stat
, 0) < 0)
1041 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1043 if(WIFEXITED(stat
)) {
1044 exit(WEXITSTATUS(stat
));
1053 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1055 /* seems to work fine on linux, openbsd and freebsd */
1056 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1057 die("openpty failed: %s\n", SERRNO
);
1059 switch(pid
= fork()) {
1061 die("fork failed\n");
1064 setsid(); /* create a new process group */
1065 dup2(s
, STDIN_FILENO
);
1066 dup2(s
, STDOUT_FILENO
);
1067 dup2(s
, STDERR_FILENO
);
1068 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1069 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1077 signal(SIGCHLD
, sigchld
);
1079 iofd
= (!strcmp(opt_io
, "-")) ?
1081 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1083 fprintf(stderr
, "Error opening %s:%s\n",
1084 opt_io
, strerror(errno
));
1094 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1096 fprintf(stderr
, "\n");
1101 static char buf
[BUFSIZ
];
1102 static int buflen
= 0;
1105 int charsize
; /* size of utf8 char in bytes */
1109 /* append read bytes to unprocessed bytes */
1110 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1111 die("Couldn't read from shell: %s\n", SERRNO
);
1113 /* process every complete utf8 char */
1116 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1117 charsize
= utf8decode(ptr
, &utf8c
);
1118 utf8encode(&utf8c
, s
);
1124 /* keep any uncomplete utf8 char for the next call */
1125 memmove(buf
, ptr
, buflen
);
1129 ttywrite(const char *s
, size_t n
) {
1130 if(write(cmdfd
, s
, n
) == -1)
1131 die("write error on tty: %s\n", SERRNO
);
1138 w
.ws_row
= term
.row
;
1139 w
.ws_col
= term
.col
;
1140 w
.ws_xpixel
= xw
.tw
;
1141 w
.ws_ypixel
= xw
.th
;
1142 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1143 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1147 tsetdirt(int top
, int bot
) {
1150 LIMIT(top
, 0, term
.row
-1);
1151 LIMIT(bot
, 0, term
.row
-1);
1153 for(i
= top
; i
<= bot
; i
++)
1159 tsetdirt(0, term
.row
-1);
1166 if(mode
== CURSOR_SAVE
) {
1168 } else if(mode
== CURSOR_LOAD
) {
1178 term
.c
= (TCursor
){{
1182 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1184 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1185 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1188 term
.bot
= term
.row
- 1;
1189 term
.mode
= MODE_WRAP
;
1191 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1193 tcursor(CURSOR_SAVE
);
1197 tnew(int col
, int row
) {
1198 /* setting "zero" terminal to resize it later */
1199 memset(&term
, 0, sizeof(Term
));
1208 Line
*tmp
= term
.line
;
1210 term
.line
= term
.alt
;
1212 term
.mode
^= MODE_ALTSCREEN
;
1217 tscrolldown(int orig
, int n
) {
1221 LIMIT(n
, 0, term
.bot
-orig
+1);
1223 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1225 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1226 temp
= term
.line
[i
];
1227 term
.line
[i
] = term
.line
[i
-n
];
1228 term
.line
[i
-n
] = temp
;
1231 term
.dirty
[i
-n
] = 1;
1238 tscrollup(int orig
, int n
) {
1241 LIMIT(n
, 0, term
.bot
-orig
+1);
1243 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1245 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1246 temp
= term
.line
[i
];
1247 term
.line
[i
] = term
.line
[i
+n
];
1248 term
.line
[i
+n
] = temp
;
1251 term
.dirty
[i
+n
] = 1;
1254 selscroll(orig
, -n
);
1258 selscroll(int orig
, int n
) {
1262 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1263 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1267 if(sel
.type
== SEL_RECTANGULAR
) {
1268 if(sel
.by
< term
.top
)
1270 if(sel
.ey
> term
.bot
)
1273 if(sel
.by
< term
.top
) {
1277 if(sel
.ey
> term
.bot
) {
1282 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1283 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1288 tnewline(int first_col
) {
1292 tscrollup(term
.top
, 1);
1296 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1301 char *p
= csiescseq
.buf
, *np
;
1310 csiescseq
.buf
[csiescseq
.len
] = '\0';
1311 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1313 v
= strtol(p
, &np
, 10);
1316 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1318 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1320 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1324 csiescseq
.mode
= *p
;
1327 /* for absolute user moves, when decom is set */
1329 tmoveato(int x
, int y
) {
1330 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1334 tmoveto(int x
, int y
) {
1337 if(term
.c
.state
& CURSOR_ORIGIN
) {
1342 maxy
= term
.row
- 1;
1344 LIMIT(x
, 0, term
.col
-1);
1345 LIMIT(y
, miny
, maxy
);
1346 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1352 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1353 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1354 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1355 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1356 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1357 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1358 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1359 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1360 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1361 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1365 * The table is proudly stolen from rxvt.
1367 if(attr
->mode
& ATTR_GFX
) {
1368 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1369 && vt100_0
[c
[0] - 0x41]) {
1370 c
= vt100_0
[c
[0] - 0x41];
1375 term
.line
[y
][x
] = *attr
;
1376 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1377 term
.line
[y
][x
].state
|= GLYPH_SET
;
1381 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1385 temp
= x1
, x1
= x2
, x2
= temp
;
1387 temp
= y1
, y1
= y2
, y2
= temp
;
1389 LIMIT(x1
, 0, term
.col
-1);
1390 LIMIT(x2
, 0, term
.col
-1);
1391 LIMIT(y1
, 0, term
.row
-1);
1392 LIMIT(y2
, 0, term
.row
-1);
1394 for(y
= y1
; y
<= y2
; y
++) {
1396 for(x
= x1
; x
<= x2
; x
++) {
1397 term
.line
[y
][x
] = term
.c
.attr
;
1398 memcpy(term
.line
[y
][x
].c
, " ", 2);
1399 term
.line
[y
][x
].state
|= GLYPH_SET
;
1405 tdeletechar(int n
) {
1406 int src
= term
.c
.x
+ n
;
1408 int size
= term
.col
- src
;
1410 term
.dirty
[term
.c
.y
] = 1;
1412 if(src
>= term
.col
) {
1413 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1417 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1418 size
* sizeof(Glyph
));
1419 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1423 tinsertblank(int n
) {
1426 int size
= term
.col
- dst
;
1428 term
.dirty
[term
.c
.y
] = 1;
1430 if(dst
>= term
.col
) {
1431 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1435 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1436 size
* sizeof(Glyph
));
1437 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1441 tinsertblankline(int n
) {
1442 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1445 tscrolldown(term
.c
.y
, n
);
1449 tdeleteline(int n
) {
1450 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1453 tscrollup(term
.c
.y
, n
);
1457 tsetattr(int *attr
, int l
) {
1460 for(i
= 0; i
< l
; i
++) {
1463 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1464 | ATTR_BOLD
| ATTR_ITALIC \
1466 term
.c
.attr
.fg
= defaultfg
;
1467 term
.c
.attr
.bg
= defaultbg
;
1470 term
.c
.attr
.mode
|= ATTR_BOLD
;
1473 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1476 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1478 case 5: /* slow blink */
1479 case 6: /* rapid blink */
1480 term
.c
.attr
.mode
|= ATTR_BLINK
;
1483 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1487 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1490 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1493 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1497 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1500 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1503 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1505 if(BETWEEN(attr
[i
], 0, 255)) {
1506 term
.c
.attr
.fg
= attr
[i
];
1509 "erresc: bad fgcolor %d\n",
1514 "erresc(38): gfx attr %d unknown\n",
1519 term
.c
.attr
.fg
= defaultfg
;
1522 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1524 if(BETWEEN(attr
[i
], 0, 255)) {
1525 term
.c
.attr
.bg
= attr
[i
];
1528 "erresc: bad bgcolor %d\n",
1533 "erresc(48): gfx attr %d unknown\n",
1538 term
.c
.attr
.bg
= defaultbg
;
1541 if(BETWEEN(attr
[i
], 30, 37)) {
1542 term
.c
.attr
.fg
= attr
[i
] - 30;
1543 } else if(BETWEEN(attr
[i
], 40, 47)) {
1544 term
.c
.attr
.bg
= attr
[i
] - 40;
1545 } else if(BETWEEN(attr
[i
], 90, 97)) {
1546 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1547 } else if(BETWEEN(attr
[i
], 100, 107)) {
1548 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1551 "erresc(default): gfx attr %d unknown\n",
1552 attr
[i
]), csidump();
1560 tsetscroll(int t
, int b
) {
1563 LIMIT(t
, 0, term
.row
-1);
1564 LIMIT(b
, 0, term
.row
-1);
1574 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1577 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1581 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1585 case 1: /* DECCKM -- Cursor key */
1586 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1588 case 5: /* DECSCNM -- Reverse video */
1590 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1591 if(mode
!= term
.mode
)
1592 redraw(REDRAW_TIMEOUT
);
1594 case 6: /* DECOM -- Origin */
1595 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1598 case 7: /* DECAWM -- Auto wrap */
1599 MODBIT(term
.mode
, set
, MODE_WRAP
);
1601 case 0: /* Error (IGNORED) */
1602 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1603 case 3: /* DECCOLM -- Column (IGNORED) */
1604 case 4: /* DECSCLM -- Scroll (IGNORED) */
1605 case 8: /* DECARM -- Auto repeat (IGNORED) */
1606 case 18: /* DECPFF -- Printer feed (IGNORED) */
1607 case 19: /* DECPEX -- Printer extent (IGNORED) */
1608 case 42: /* DECNRCM -- National characters (IGNORED) */
1609 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1611 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1612 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1614 case 1000: /* 1000,1002: enable xterm mouse report */
1615 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1616 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1619 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1620 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1623 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1625 case 1049: /* = 1047 and 1048 */
1628 if (!allowaltscreen
)
1631 alt
= IS_SET(MODE_ALTSCREEN
);
1633 tclearregion(0, 0, term
.col
-1,
1636 if(set
^ alt
) /* set is always 1 or 0 */
1642 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1646 "erresc: unknown private set/reset mode %d\n",
1652 case 0: /* Error (IGNORED) */
1654 case 2: /* KAM -- keyboard action */
1655 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1657 case 4: /* IRM -- Insertion-replacement */
1658 MODBIT(term
.mode
, set
, MODE_INSERT
);
1660 case 12: /* SRM -- Send/Receive */
1661 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1663 case 20: /* LNM -- Linefeed/new line */
1664 MODBIT(term
.mode
, set
, MODE_CRLF
);
1668 "erresc: unknown set/reset mode %d\n",
1680 switch(csiescseq
.mode
) {
1683 fprintf(stderr
, "erresc: unknown csi ");
1687 case '@': /* ICH -- Insert <n> blank char */
1688 DEFAULT(csiescseq
.arg
[0], 1);
1689 tinsertblank(csiescseq
.arg
[0]);
1691 case 'A': /* CUU -- Cursor <n> Up */
1692 DEFAULT(csiescseq
.arg
[0], 1);
1693 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1695 case 'B': /* CUD -- Cursor <n> Down */
1696 case 'e': /* VPR --Cursor <n> Down */
1697 DEFAULT(csiescseq
.arg
[0], 1);
1698 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1700 case 'c': /* DA -- Device Attributes */
1701 if(csiescseq
.arg
[0] == 0)
1702 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1704 case 'C': /* CUF -- Cursor <n> Forward */
1705 case 'a': /* HPR -- Cursor <n> Forward */
1706 DEFAULT(csiescseq
.arg
[0], 1);
1707 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1709 case 'D': /* CUB -- Cursor <n> Backward */
1710 DEFAULT(csiescseq
.arg
[0], 1);
1711 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1713 case 'E': /* CNL -- Cursor <n> Down and first col */
1714 DEFAULT(csiescseq
.arg
[0], 1);
1715 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1717 case 'F': /* CPL -- Cursor <n> Up and first col */
1718 DEFAULT(csiescseq
.arg
[0], 1);
1719 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1721 case 'g': /* TBC -- Tabulation clear */
1722 switch(csiescseq
.arg
[0]) {
1723 case 0: /* clear current tab stop */
1724 term
.tabs
[term
.c
.x
] = 0;
1726 case 3: /* clear all the tabs */
1727 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1733 case 'G': /* CHA -- Move to <col> */
1735 DEFAULT(csiescseq
.arg
[0], 1);
1736 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1738 case 'H': /* CUP -- Move to <row> <col> */
1740 DEFAULT(csiescseq
.arg
[0], 1);
1741 DEFAULT(csiescseq
.arg
[1], 1);
1742 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1744 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1745 DEFAULT(csiescseq
.arg
[0], 1);
1746 while(csiescseq
.arg
[0]--)
1749 case 'J': /* ED -- Clear screen */
1751 switch(csiescseq
.arg
[0]) {
1753 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1754 if(term
.c
.y
< term
.row
-1) {
1755 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1761 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1762 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1765 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1771 case 'K': /* EL -- Clear line */
1772 switch(csiescseq
.arg
[0]) {
1774 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1778 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1781 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1785 case 'S': /* SU -- Scroll <n> line up */
1786 DEFAULT(csiescseq
.arg
[0], 1);
1787 tscrollup(term
.top
, csiescseq
.arg
[0]);
1789 case 'T': /* SD -- Scroll <n> line down */
1790 DEFAULT(csiescseq
.arg
[0], 1);
1791 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1793 case 'L': /* IL -- Insert <n> blank lines */
1794 DEFAULT(csiescseq
.arg
[0], 1);
1795 tinsertblankline(csiescseq
.arg
[0]);
1797 case 'l': /* RM -- Reset Mode */
1798 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1800 case 'M': /* DL -- Delete <n> lines */
1801 DEFAULT(csiescseq
.arg
[0], 1);
1802 tdeleteline(csiescseq
.arg
[0]);
1804 case 'X': /* ECH -- Erase <n> char */
1805 DEFAULT(csiescseq
.arg
[0], 1);
1806 tclearregion(term
.c
.x
, term
.c
.y
,
1807 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1809 case 'P': /* DCH -- Delete <n> char */
1810 DEFAULT(csiescseq
.arg
[0], 1);
1811 tdeletechar(csiescseq
.arg
[0]);
1813 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1814 DEFAULT(csiescseq
.arg
[0], 1);
1815 while(csiescseq
.arg
[0]--)
1818 case 'd': /* VPA -- Move to <row> */
1819 DEFAULT(csiescseq
.arg
[0], 1);
1820 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1822 case 'h': /* SM -- Set terminal mode */
1823 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1825 case 'm': /* SGR -- Terminal attribute (color) */
1826 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1828 case 'r': /* DECSTBM -- Set Scrolling Region */
1829 if(csiescseq
.priv
) {
1832 DEFAULT(csiescseq
.arg
[0], 1);
1833 DEFAULT(csiescseq
.arg
[1], term
.row
);
1834 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1838 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1839 tcursor(CURSOR_SAVE
);
1841 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1842 tcursor(CURSOR_LOAD
);
1853 for(i
= 0; i
< csiescseq
.len
; i
++) {
1854 c
= csiescseq
.buf
[i
] & 0xff;
1857 } else if(c
== '\n') {
1859 } else if(c
== '\r') {
1861 } else if(c
== 0x1b) {
1864 printf("(%02x)", c
);
1872 memset(&csiescseq
, 0, sizeof(csiescseq
));
1881 narg
= strescseq
.narg
;
1883 switch(strescseq
.type
) {
1884 case ']': /* OSC -- Operating System Command */
1885 switch(i
= atoi(strescseq
.args
[0])) {
1890 xsettitle(strescseq
.args
[1]);
1892 case 4: /* color set */
1895 p
= strescseq
.args
[2];
1897 case 104: /* color reset, here p = NULL */
1898 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
1899 if (!xsetcolorname(j
, p
)) {
1900 fprintf(stderr
, "erresc: invalid color %s\n", p
);
1903 * TODO if defaultbg color is changed, borders
1910 fprintf(stderr
, "erresc: unknown str ");
1915 case 'k': /* old title set compatibility */
1916 xsettitle(strescseq
.args
[0]);
1918 case 'P': /* DSC -- Device Control String */
1919 case '_': /* APC -- Application Program Command */
1920 case '^': /* PM -- Privacy Message */
1922 fprintf(stderr
, "erresc: unknown str ");
1931 char *p
= strescseq
.buf
;
1934 strescseq
.buf
[strescseq
.len
] = '\0';
1935 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
1936 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
1944 printf("ESC%c", strescseq
.type
);
1945 for(i
= 0; i
< strescseq
.len
; i
++) {
1946 c
= strescseq
.buf
[i
] & 0xff;
1949 } else if(isprint(c
)) {
1951 } else if(c
== '\n') {
1953 } else if(c
== '\r') {
1955 } else if(c
== 0x1b) {
1958 printf("(%02x)", c
);
1966 memset(&strescseq
, 0, sizeof(strescseq
));
1970 tputtab(bool forward
) {
1976 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1981 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1984 tmoveto(x
, term
.c
.y
);
1988 techo(char *buf
, int len
) {
1989 for(; len
> 0; buf
++, len
--) {
1992 if(c
== '\033') { /* escape */
1995 } else if(c
< '\x20') { /* control code */
1996 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2010 tputc(char *c
, int len
) {
2012 bool control
= ascii
< '\x20' || ascii
== 0177;
2015 if(xwrite(iofd
, c
, len
) < 0) {
2016 fprintf(stderr
, "Error writing in %s:%s\n",
2017 opt_io
, strerror(errno
));
2024 * STR sequences must be checked before anything else
2025 * because it can use some control codes as part of the sequence.
2027 if(term
.esc
& ESC_STR
) {
2030 term
.esc
= ESC_START
| ESC_STR_END
;
2032 case '\a': /* backwards compatibility to xterm */
2037 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2038 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2039 strescseq
.len
+= len
;
2042 * Here is a bug in terminals. If the user never sends
2043 * some code to stop the str or esc command, then st
2044 * will stop responding. But this is better than
2045 * silently failing with unknown characters. At least
2046 * then users will report back.
2048 * In the case users ever get fixed, here is the code:
2060 * Actions of control codes must be performed as soon they arrive
2061 * because they can be embedded inside a control sequence, and
2062 * they must not cause conflicts with sequences.
2070 tmoveto(term
.c
.x
-1, term
.c
.y
);
2073 tmoveto(0, term
.c
.y
);
2078 /* go to first col if the mode is set */
2079 tnewline(IS_SET(MODE_CRLF
));
2081 case '\a': /* BEL */
2082 if(!(xw
.state
& WIN_FOCUSED
))
2085 case '\033': /* ESC */
2087 term
.esc
= ESC_START
;
2089 case '\016': /* SO */
2090 case '\017': /* SI */
2092 * Different charsets are hard to handle. Applications
2093 * should use the right alt charset escapes for the
2094 * only reason they still exist: line drawing. The
2095 * rest is incompatible history st should not support.
2098 case '\032': /* SUB */
2099 case '\030': /* CAN */
2102 case '\005': /* ENQ (IGNORED) */
2103 case '\000': /* NUL (IGNORED) */
2104 case '\021': /* XON (IGNORED) */
2105 case '\023': /* XOFF (IGNORED) */
2106 case 0177: /* DEL (IGNORED) */
2109 } else if(term
.esc
& ESC_START
) {
2110 if(term
.esc
& ESC_CSI
) {
2111 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2112 if(BETWEEN(ascii
, 0x40, 0x7E)
2113 || csiescseq
.len
>= \
2114 sizeof(csiescseq
.buf
)-1) {
2119 } else if(term
.esc
& ESC_STR_END
) {
2123 } else if(term
.esc
& ESC_ALTCHARSET
) {
2125 case '0': /* Line drawing set */
2126 term
.c
.attr
.mode
|= ATTR_GFX
;
2128 case 'B': /* USASCII */
2129 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2131 case 'A': /* UK (IGNORED) */
2132 case '<': /* multinational charset (IGNORED) */
2133 case '5': /* Finnish (IGNORED) */
2134 case 'C': /* Finnish (IGNORED) */
2135 case 'K': /* German (IGNORED) */
2138 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2141 } else if(term
.esc
& ESC_TEST
) {
2142 if(ascii
== '8') { /* DEC screen alignment test. */
2143 char E
[UTF_SIZ
] = "E";
2146 for(x
= 0; x
< term
.col
; ++x
) {
2147 for(y
= 0; y
< term
.row
; ++y
)
2148 tsetchar(E
, &term
.c
.attr
, x
, y
);
2155 term
.esc
|= ESC_CSI
;
2158 term
.esc
|= ESC_TEST
;
2160 case 'P': /* DCS -- Device Control String */
2161 case '_': /* APC -- Application Program Command */
2162 case '^': /* PM -- Privacy Message */
2163 case ']': /* OSC -- Operating System Command */
2164 case 'k': /* old title set compatibility */
2166 strescseq
.type
= ascii
;
2167 term
.esc
|= ESC_STR
;
2169 case '(': /* set primary charset G0 */
2170 term
.esc
|= ESC_ALTCHARSET
;
2172 case ')': /* set secondary charset G1 (IGNORED) */
2173 case '*': /* set tertiary charset G2 (IGNORED) */
2174 case '+': /* set quaternary charset G3 (IGNORED) */
2177 case 'D': /* IND -- Linefeed */
2178 if(term
.c
.y
== term
.bot
) {
2179 tscrollup(term
.top
, 1);
2181 tmoveto(term
.c
.x
, term
.c
.y
+1);
2185 case 'E': /* NEL -- Next line */
2186 tnewline(1); /* always go to first col */
2189 case 'H': /* HTS -- Horizontal tab stop */
2190 term
.tabs
[term
.c
.x
] = 1;
2193 case 'M': /* RI -- Reverse index */
2194 if(term
.c
.y
== term
.top
) {
2195 tscrolldown(term
.top
, 1);
2197 tmoveto(term
.c
.x
, term
.c
.y
-1);
2201 case 'Z': /* DECID -- Identify Terminal */
2202 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2205 case 'c': /* RIS -- Reset to inital state */
2210 case '=': /* DECPAM -- Application keypad */
2211 term
.mode
|= MODE_APPKEYPAD
;
2214 case '>': /* DECPNM -- Normal keypad */
2215 term
.mode
&= ~MODE_APPKEYPAD
;
2218 case '7': /* DECSC -- Save Cursor */
2219 tcursor(CURSOR_SAVE
);
2222 case '8': /* DECRC -- Restore Cursor */
2223 tcursor(CURSOR_LOAD
);
2226 case '\\': /* ST -- Stop */
2230 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2231 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2236 * All characters which form part of a sequence are not
2242 * Display control codes only if we are in graphic mode
2244 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2246 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2248 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2249 tnewline(1); /* always go to first col */
2251 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2252 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2253 &term
.line
[term
.c
.y
][term
.c
.x
],
2254 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2257 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2258 if(term
.c
.x
+1 < term
.col
) {
2259 tmoveto(term
.c
.x
+1, term
.c
.y
);
2261 term
.c
.state
|= CURSOR_WRAPNEXT
;
2266 tresize(int col
, int row
) {
2268 int minrow
= MIN(row
, term
.row
);
2269 int mincol
= MIN(col
, term
.col
);
2270 int slide
= term
.c
.y
- row
+ 1;
2273 if(col
< 1 || row
< 1)
2276 /* free unneeded rows */
2280 * slide screen to keep cursor where we expect it -
2281 * tscrollup would work here, but we can optimize to
2282 * memmove because we're freeing the earlier lines
2284 for(/* i = 0 */; i
< slide
; i
++) {
2288 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2289 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2291 for(i
+= row
; i
< term
.row
; i
++) {
2296 /* resize to new height */
2297 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2298 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2299 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2300 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2302 /* resize each row to new width, zero-pad if needed */
2303 for(i
= 0; i
< minrow
; i
++) {
2305 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2306 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2307 for(x
= mincol
; x
< col
; x
++) {
2308 term
.line
[i
][x
].state
= 0;
2309 term
.alt
[i
][x
].state
= 0;
2313 /* allocate any new rows */
2314 for(/* i == minrow */; i
< row
; i
++) {
2316 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2317 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2319 if(col
> term
.col
) {
2320 bp
= term
.tabs
+ term
.col
;
2322 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2323 while(--bp
> term
.tabs
&& !*bp
)
2325 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2328 /* update terminal size */
2331 /* reset scrolling region */
2332 tsetscroll(0, row
-1);
2333 /* make use of the LIMIT in tmoveto */
2334 tmoveto(term
.c
.x
, term
.c
.y
);
2340 xresize(int col
, int row
) {
2341 xw
.tw
= MAX(1, col
* xw
.cw
);
2342 xw
.th
= MAX(1, row
* xw
.ch
);
2344 XFreePixmap(xw
.dpy
, xw
.buf
);
2345 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2346 DefaultDepth(xw
.dpy
, xw
.scr
));
2347 XftDrawChange(xw
.draw
, xw
.buf
);
2348 xclear(0, 0, xw
.w
, xw
.h
);
2351 static inline ushort
2352 sixd_to_16bit(int x
) {
2353 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2359 XRenderColor color
= { .alpha
= 0xffff };
2361 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2362 for(i
= 0; i
< LEN(colorname
); i
++) {
2365 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2366 die("Could not allocate color '%s'\n", colorname
[i
]);
2370 /* load colors [16-255] ; same colors as xterm */
2371 for(i
= 16, r
= 0; r
< 6; r
++) {
2372 for(g
= 0; g
< 6; g
++) {
2373 for(b
= 0; b
< 6; b
++) {
2374 color
.red
= sixd_to_16bit(r
);
2375 color
.green
= sixd_to_16bit(g
);
2376 color
.blue
= sixd_to_16bit(b
);
2377 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2378 die("Could not allocate color %d\n", i
);
2385 for(r
= 0; r
< 24; r
++, i
++) {
2386 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2387 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2389 die("Could not allocate color %d\n", i
);
2395 xsetcolorname(int x
, const char *name
) {
2396 XRenderColor color
= { .alpha
= 0xffff };
2398 if (x
< 0 || x
> LEN(colorname
))
2401 if(16 <= x
&& x
< 16 + 216) {
2402 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2403 color
.red
= sixd_to_16bit(r
);
2404 color
.green
= sixd_to_16bit(g
);
2405 color
.blue
= sixd_to_16bit(b
);
2406 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2407 return 0; /* something went wrong */
2410 } else if (16 + 216 <= x
&& x
< 256) {
2411 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2412 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2413 return 0; /* something went wrong */
2417 name
= colorname
[x
];
2420 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2427 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2428 XftDrawRect(xw
.draw
,
2429 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2430 borderpx
+ col1
* xw
.cw
,
2431 borderpx
+ row1
* xw
.ch
,
2432 (col2
-col1
+1) * xw
.cw
,
2433 (row2
-row1
+1) * xw
.ch
);
2437 * Absolute coordinates.
2440 xclear(int x1
, int y1
, int x2
, int y2
) {
2441 XftDrawRect(xw
.draw
,
2442 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2443 x1
, y1
, x2
-x1
, y2
-y1
);
2448 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2449 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2450 XSizeHints
*sizeh
= NULL
;
2452 sizeh
= XAllocSizeHints();
2453 if(xw
.isfixed
== False
) {
2454 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2455 sizeh
->height
= xw
.h
;
2456 sizeh
->width
= xw
.w
;
2457 sizeh
->height_inc
= xw
.ch
;
2458 sizeh
->width_inc
= xw
.cw
;
2459 sizeh
->base_height
= 2 * borderpx
;
2460 sizeh
->base_width
= 2 * borderpx
;
2462 sizeh
->flags
= PMaxSize
| PMinSize
;
2463 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2464 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2467 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2472 xloadfont(Font
*f
, FcPattern
*pattern
) {
2476 match
= FcFontMatch(NULL
, pattern
, &result
);
2480 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2481 FcPatternDestroy(match
);
2485 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2486 FcPatternDestroy(match
);
2490 f
->pattern
= FcPatternDuplicate(pattern
);
2492 f
->ascent
= f
->match
->ascent
;
2493 f
->descent
= f
->match
->descent
;
2495 f
->rbearing
= f
->match
->max_advance_width
;
2497 f
->height
= f
->ascent
+ f
->descent
;
2498 f
->width
= f
->lbearing
+ f
->rbearing
;
2504 xloadfonts(char *fontstr
, int fontsize
) {
2509 if(fontstr
[0] == '-') {
2510 pattern
= XftXlfdParse(fontstr
, False
, False
);
2512 pattern
= FcNameParse((FcChar8
*)fontstr
);
2516 die("st: can't open font %s\n", fontstr
);
2519 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2520 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2521 usedfontsize
= fontsize
;
2523 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2524 if(result
== FcResultMatch
) {
2525 usedfontsize
= (int)fontval
;
2528 * Default font size is 12, if none given. This is to
2529 * have a known usedfontsize value.
2531 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2536 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2537 FcDefaultSubstitute(pattern
);
2539 if(xloadfont(&dc
.font
, pattern
))
2540 die("st: can't open font %s\n", fontstr
);
2542 /* Setting character width and height. */
2543 xw
.cw
= dc
.font
.width
;
2544 xw
.ch
= dc
.font
.height
;
2546 FcPatternDel(pattern
, FC_SLANT
);
2547 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2548 if(xloadfont(&dc
.ifont
, pattern
))
2549 die("st: can't open font %s\n", fontstr
);
2551 FcPatternDel(pattern
, FC_WEIGHT
);
2552 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2553 if(xloadfont(&dc
.ibfont
, pattern
))
2554 die("st: can't open font %s\n", fontstr
);
2556 FcPatternDel(pattern
, FC_SLANT
);
2557 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2558 if(xloadfont(&dc
.bfont
, pattern
))
2559 die("st: can't open font %s\n", fontstr
);
2561 FcPatternDestroy(pattern
);
2565 xunloadfonts(void) {
2569 * Free the loaded fonts in the font cache. This is done backwards
2572 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2575 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2580 XftFontClose(xw
.dpy
, dc
.font
.match
);
2581 FcPatternDestroy(dc
.font
.pattern
);
2582 FcFontSetDestroy(dc
.font
.set
);
2583 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2584 FcPatternDestroy(dc
.bfont
.pattern
);
2585 FcFontSetDestroy(dc
.bfont
.set
);
2586 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2587 FcPatternDestroy(dc
.ifont
.pattern
);
2588 FcFontSetDestroy(dc
.ifont
.set
);
2589 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2590 FcPatternDestroy(dc
.ibfont
.pattern
);
2591 FcFontSetDestroy(dc
.ibfont
.set
);
2595 xzoom(const Arg
*arg
) {
2597 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2604 XSetWindowAttributes attrs
;
2610 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2611 die("Can't open display\n");
2612 xw
.scr
= XDefaultScreen(xw
.dpy
);
2613 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2617 die("Could not init fontconfig.\n");
2619 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2620 xloadfonts(usedfont
, 0);
2623 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2626 /* adjust fixed window geometry */
2628 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2629 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2631 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2633 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2638 /* window - default size */
2639 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2640 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2646 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2647 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2648 attrs
.bit_gravity
= NorthWestGravity
;
2649 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2650 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2651 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2652 attrs
.colormap
= xw
.cmap
;
2654 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2655 XRootWindow(xw
.dpy
, xw
.scr
);
2656 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2657 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2659 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2663 memset(&gcvalues
, 0, sizeof(gcvalues
));
2664 gcvalues
.graphics_exposures
= False
;
2665 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2667 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2668 DefaultDepth(xw
.dpy
, xw
.scr
));
2669 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2670 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2672 /* Xft rendering context */
2673 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2676 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2677 XSetLocaleModifiers("@im=local");
2678 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2679 XSetLocaleModifiers("@im=");
2680 if((xw
.xim
= XOpenIM(xw
.dpy
,
2681 NULL
, NULL
, NULL
)) == NULL
) {
2682 die("XOpenIM failed. Could not open input"
2687 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2688 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2689 XNFocusWindow
, xw
.win
, NULL
);
2691 die("XCreateIC failed. Could not obtain input method.\n");
2693 /* white cursor, black outline */
2694 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2695 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2696 XRecolorCursor(xw
.dpy
, cursor
,
2697 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2698 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2700 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2701 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2702 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2705 XMapWindow(xw
.dpy
, xw
.win
);
2711 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2712 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2713 width
= charlen
* xw
.cw
, xp
, i
;
2715 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2718 Font
*font
= &dc
.font
;
2720 FcPattern
*fcpattern
, *fontpattern
;
2721 FcFontSet
*fcsets
[] = { NULL
};
2722 FcCharSet
*fccharset
;
2723 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2724 XRenderColor colfg
, colbg
;
2726 frcflags
= FRC_NORMAL
;
2728 if(base
.mode
& ATTR_ITALIC
) {
2729 if(base
.fg
== defaultfg
)
2730 base
.fg
= defaultitalic
;
2732 frcflags
= FRC_ITALIC
;
2733 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2734 if(base
.fg
== defaultfg
)
2735 base
.fg
= defaultitalic
;
2737 frcflags
= FRC_ITALICBOLD
;
2738 } else if(base
.mode
& ATTR_UNDERLINE
) {
2739 if(base
.fg
== defaultfg
)
2740 base
.fg
= defaultunderline
;
2742 fg
= &dc
.col
[base
.fg
];
2743 bg
= &dc
.col
[base
.bg
];
2745 if(base
.mode
& ATTR_BOLD
) {
2746 if(BETWEEN(base
.fg
, 0, 7)) {
2747 /* basic system colors */
2748 fg
= &dc
.col
[base
.fg
+ 8];
2749 } else if(BETWEEN(base
.fg
, 16, 195)) {
2751 fg
= &dc
.col
[base
.fg
+ 36];
2752 } else if(BETWEEN(base
.fg
, 232, 251)) {
2754 fg
= &dc
.col
[base
.fg
+ 4];
2757 * Those ranges will not be brightened:
2758 * 8 - 15 – bright system colors
2759 * 196 - 231 – highest 256 color cube
2760 * 252 - 255 – brightest colors in greyscale
2763 frcflags
= FRC_BOLD
;
2766 if(IS_SET(MODE_REVERSE
)) {
2767 if(fg
== &dc
.col
[defaultfg
]) {
2768 fg
= &dc
.col
[defaultbg
];
2770 colfg
.red
= ~fg
->color
.red
;
2771 colfg
.green
= ~fg
->color
.green
;
2772 colfg
.blue
= ~fg
->color
.blue
;
2773 colfg
.alpha
= fg
->color
.alpha
;
2774 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2778 if(bg
== &dc
.col
[defaultbg
]) {
2779 bg
= &dc
.col
[defaultfg
];
2781 colbg
.red
= ~bg
->color
.red
;
2782 colbg
.green
= ~bg
->color
.green
;
2783 colbg
.blue
= ~bg
->color
.blue
;
2784 colbg
.alpha
= bg
->color
.alpha
;
2785 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2790 if(base
.mode
& ATTR_REVERSE
) {
2796 /* Intelligent cleaning up of the borders. */
2798 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2799 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2801 if(x
+ charlen
>= term
.col
) {
2802 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2803 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2806 xclear(winx
, 0, winx
+ width
, borderpx
);
2808 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2810 /* Clean up the region we want to draw to. */
2811 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2813 fcsets
[0] = font
->set
;
2814 for(xp
= winx
; bytelen
> 0;) {
2816 * Search for the range in the to be printed string of glyphs
2817 * that are in the main font. Then print that range. If
2818 * some glyph is found that is not in the font, do the
2826 u8cblen
= utf8decode(s
, &u8char
);
2830 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2831 if(!doesexist
|| bytelen
<= 0) {
2840 XftDrawStringUtf8(xw
.draw
, fg
,
2842 winy
+ font
->ascent
,
2845 xp
+= font
->width
* u8fl
;
2857 /* Search the font cache. */
2858 for(i
= 0; i
< frclen
; i
++, frp
--) {
2862 if(frc
[frp
].c
== u8char
2863 && frc
[frp
].flags
== frcflags
) {
2868 /* Nothing was found. */
2871 * Nothing was found in the cache. Now use
2872 * some dozen of Fontconfig calls to get the
2873 * font for one single character.
2875 fcpattern
= FcPatternDuplicate(font
->pattern
);
2876 fccharset
= FcCharSetCreate();
2878 FcCharSetAddChar(fccharset
, u8char
);
2879 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2881 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2884 FcConfigSubstitute(0, fcpattern
,
2886 FcDefaultSubstitute(fcpattern
);
2888 fontpattern
= FcFontSetMatch(0, fcsets
,
2889 FcTrue
, fcpattern
, &fcres
);
2892 * 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
) {
2956 if(IS_SET(MODE_REVERSE
)) {
2957 g
.mode
|= ATTR_REVERSE
;
2963 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2965 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2966 borderpx
+ term
.c
.x
* xw
.cw
,
2967 borderpx
+ term
.c
.y
* xw
.ch
,
2969 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2970 borderpx
+ term
.c
.x
* xw
.cw
,
2971 borderpx
+ term
.c
.y
* xw
.ch
,
2973 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2974 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
2975 borderpx
+ term
.c
.y
* xw
.ch
,
2977 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2978 borderpx
+ term
.c
.x
* xw
.cw
,
2979 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
2982 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2988 xsettitle(char *p
) {
2991 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
2993 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
2998 xsettitle(opt_title
? opt_title
: "st");
3002 redraw(int timeout
) {
3003 struct timespec tv
= {0, timeout
* 1000};
3009 nanosleep(&tv
, NULL
);
3010 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3016 drawregion(0, 0, term
.col
, term
.row
);
3017 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3019 XSetForeground(xw
.dpy
, dc
.gc
,
3020 dc
.col
[IS_SET(MODE_REVERSE
)?
3021 defaultfg
: defaultbg
].pixel
);
3025 drawregion(int x1
, int y1
, int x2
, int y2
) {
3026 int ic
, ib
, x
, y
, ox
, sl
;
3028 char buf
[DRAW_BUF_SIZ
];
3029 bool ena_sel
= sel
.bx
!= -1;
3031 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3034 if(!(xw
.state
& WIN_VISIBLE
))
3037 for(y
= y1
; y
< y2
; y
++) {
3041 xtermclear(0, y
, term
.col
, y
);
3043 base
= term
.line
[y
][0];
3045 for(x
= x1
; x
< x2
; x
++) {
3046 new = term
.line
[y
][x
];
3047 if(ena_sel
&& *(new.c
) && selected(x
, y
))
3048 new.mode
^= ATTR_REVERSE
;
3049 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
3050 || ATTRCMP(base
, new)
3051 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3052 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3055 if(new.state
& GLYPH_SET
) {
3061 sl
= utf8size(new.c
);
3062 memcpy(buf
+ib
, new.c
, sl
);
3068 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3074 expose(XEvent
*ev
) {
3075 XExposeEvent
*e
= &ev
->xexpose
;
3077 if(xw
.state
& WIN_REDRAW
) {
3079 xw
.state
&= ~WIN_REDRAW
;
3085 visibility(XEvent
*ev
) {
3086 XVisibilityEvent
*e
= &ev
->xvisibility
;
3088 if(e
->state
== VisibilityFullyObscured
) {
3089 xw
.state
&= ~WIN_VISIBLE
;
3090 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3091 /* need a full redraw for next Expose, not just a buf copy */
3092 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3098 xw
.state
&= ~WIN_VISIBLE
;
3102 xseturgency(int add
) {
3103 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3105 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3106 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3112 XFocusChangeEvent
*e
= &ev
->xfocus
;
3114 if(e
->mode
== NotifyGrab
)
3117 if(ev
->type
== FocusIn
) {
3118 XSetICFocus(xw
.xic
);
3119 xw
.state
|= WIN_FOCUSED
;
3122 XUnsetICFocus(xw
.xic
);
3123 xw
.state
&= ~WIN_FOCUSED
;
3128 match(uint mask
, uint state
) {
3129 state
&= ~(ignoremod
);
3131 if(mask
== XK_NO_MOD
&& state
)
3133 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3135 if((state
& mask
) != state
)
3141 numlock(const Arg
*dummy
) {
3146 kmap(KeySym k
, uint state
) {
3151 /* Check for mapped keys out of X11 function keys. */
3152 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3153 if(mappedkeys
[i
] == k
)
3156 if(i
== LEN(mappedkeys
)) {
3157 if((k
& 0xFFFF) < 0xFD00)
3161 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3167 if(!match(mask
, state
))
3170 if(kp
->appkey
> 0) {
3171 if(!IS_SET(MODE_APPKEYPAD
))
3173 if(term
.numlock
&& kp
->appkey
== 2)
3175 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3179 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3181 && !IS_SET(MODE_APPCURSOR
))) {
3185 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3186 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3197 kpress(XEvent
*ev
) {
3198 XKeyEvent
*e
= &ev
->xkey
;
3200 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3205 if(IS_SET(MODE_KBDLOCK
))
3208 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3209 e
->state
&= ~Mod2Mask
;
3211 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3212 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3213 bp
->func(&(bp
->arg
));
3218 /* 2. custom keys from config.h */
3219 if((customkey
= kmap(ksym
, e
->state
))) {
3220 len
= strlen(customkey
);
3221 memcpy(buf
, customkey
, len
);
3222 /* 2. hardcoded (overrides X lookup) */
3227 if(len
== 1 && e
->state
& Mod1Mask
)
3230 memcpy(cp
, xstr
, len
);
3231 len
= cp
- buf
+ len
;
3235 if(IS_SET(MODE_ECHO
))
3241 cmessage(XEvent
*e
) {
3244 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3246 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3247 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3248 xw
.state
|= WIN_FOCUSED
;
3250 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3251 xw
.state
&= ~WIN_FOCUSED
;
3253 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3254 /* Send SIGHUP to shell */
3261 cresize(int width
, int height
) {
3269 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3270 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3279 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3282 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3289 int xfd
= XConnectionNumber(xw
.dpy
), xev
;
3290 struct timeval drawtimeout
, *tv
= NULL
, now
, last
;
3292 gettimeofday(&last
, NULL
);
3294 for(xev
= actionfps
;;) {
3296 FD_SET(cmdfd
, &rfd
);
3298 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3301 die("select failed: %s\n", SERRNO
);
3304 gettimeofday(&now
, NULL
);
3305 drawtimeout
.tv_sec
= 0;
3306 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3309 if(FD_ISSET(cmdfd
, &rfd
))
3312 if(FD_ISSET(xfd
, &rfd
))
3315 if(TIMEDIFF(now
, last
) > \
3316 (xev
? (1000/xfps
) : (1000/actionfps
))) {
3317 while(XPending(xw
.dpy
)) {
3318 XNextEvent(xw
.dpy
, &ev
);
3319 if(XFilterEvent(&ev
, None
))
3321 if(handler
[ev
.type
])
3322 (handler
[ev
.type
])(&ev
);
3329 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3331 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
))
3339 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3340 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3341 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3345 main(int argc
, char *argv
[]) {
3349 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3354 allowaltscreen
= false;
3357 opt_class
= EARGF(usage());
3360 /* eat all remaining arguments */
3365 opt_font
= EARGF(usage());
3368 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3373 if(bitm
& WidthValue
)
3375 if(bitm
& HeightValue
)
3377 if(bitm
& XNegative
&& xw
.fx
== 0)
3379 if(bitm
& XNegative
&& xw
.fy
== 0)
3382 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3386 opt_io
= EARGF(usage());
3389 opt_title
= EARGF(usage());
3392 opt_embed
= EARGF(usage());
3400 setlocale(LC_CTYPE
, "");
3401 XSetLocaleModifiers("");
3407 cresize(xw
.h
, xw
.w
);