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
39 #define Rectangle XRectangle
43 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
45 #elif defined(__FreeBSD__) || defined(__DragonFly__)
51 #define XEMBED_FOCUS_IN 4
52 #define XEMBED_FOCUS_OUT 5
56 #define ESC_BUF_SIZ (128*UTF_SIZ)
57 #define ESC_ARG_SIZ 16
58 #define STR_BUF_SIZ ESC_BUF_SIZ
59 #define STR_ARG_SIZ ESC_ARG_SIZ
60 #define DRAW_BUF_SIZ 20*1024
61 #define XK_ANY_MOD UINT_MAX
63 #define XK_SWITCH_MOD (1<<13)
65 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
68 #define SERRNO strerror(errno)
69 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #define MAX(a, b) ((a) < (b) ? (b) : (a))
71 #define LEN(a) (sizeof(a) / sizeof(a[0]))
72 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
73 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
74 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
75 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
76 #define IS_SET(flag) ((term.mode & (flag)) != 0)
77 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
79 #define VT102ID "\033[?6c"
81 enum glyph_attribute
{
92 enum cursor_movement
{
110 MODE_MOUSEMOTION
= 64,
116 MODE_APPCURSOR
= 2048,
117 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
{
143 enum selection_snap
{
150 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
152 typedef unsigned char uchar
;
153 typedef unsigned int uint
;
154 typedef unsigned long ulong
;
155 typedef unsigned short ushort
;
158 char c
[UTF_SIZ
]; /* character code */
159 uchar mode
; /* attribute flags */
160 ushort fg
; /* foreground */
161 ushort bg
; /* background */
167 Glyph attr
; /* current char attributes */
173 /* CSI Escape sequence structs */
174 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
176 char buf
[ESC_BUF_SIZ
]; /* raw string */
177 int len
; /* raw string length */
179 int arg
[ESC_ARG_SIZ
];
180 int narg
; /* nb of args */
184 /* STR Escape sequence structs */
185 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
187 char type
; /* ESC type ... */
188 char buf
[STR_BUF_SIZ
]; /* raw string */
189 int len
; /* raw string length */
190 char *args
[STR_ARG_SIZ
];
191 int narg
; /* nb of args */
194 /* Internal representation of the screen */
196 int row
; /* nb row */
197 int col
; /* nb col */
198 Line
*line
; /* screen */
199 Line
*alt
; /* alternate screen */
200 bool *dirty
; /* dirtyness of lines */
201 TCursor c
; /* cursor */
202 int top
; /* top scroll limit */
203 int bot
; /* bottom scroll limit */
204 int mode
; /* terminal mode flags */
205 int esc
; /* escape state flags */
206 bool numlock
; /* lock numbers in keyboard */
210 /* Purely graphic info */
216 Atom xembed
, wmdeletewin
;
222 bool isfixed
; /* is fixed geometry? */
223 int fx
, fy
, fw
, fh
; /* fixed geometry */
224 int tw
, th
; /* tty width and height */
225 int w
, h
; /* window width and height */
226 int ch
; /* char height */
227 int cw
; /* char width */
228 char state
; /* focus, redraw, visible */
241 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
242 signed char appkey
; /* application keypad */
243 signed char appcursor
; /* application cursor */
244 signed char crlf
; /* crlf mode */
247 /* TODO: use better name for vars... */
260 struct timeval tclick1
;
261 struct timeval tclick2
;
274 void (*func
)(const Arg
*);
278 /* function definitions used in config.h */
279 static void clippaste(const Arg
*);
280 static void numlock(const Arg
*);
281 static void selpaste(const Arg
*);
282 static void xzoom(const Arg
*);
284 /* Config.h for applying patches and the configuration. */
300 /* Drawing Context */
302 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
303 Font font
, bfont
, ifont
, ibfont
;
307 static void die(const char *, ...);
308 static void draw(void);
309 static void redraw(int);
310 static void drawregion(int, int, int, int);
311 static void execsh(void);
312 static void sigchld(int);
313 static void run(void);
315 static void csidump(void);
316 static void csihandle(void);
317 static void csiparse(void);
318 static void csireset(void);
319 static void strdump(void);
320 static void strhandle(void);
321 static void strparse(void);
322 static void strreset(void);
324 static int tattrset(int);
325 static void tclearregion(int, int, int, int);
326 static void tcursor(int);
327 static void tdeletechar(int);
328 static void tdeleteline(int);
329 static void tinsertblank(int);
330 static void tinsertblankline(int);
331 static void tmoveto(int, int);
332 static void tmoveato(int x
, int y
);
333 static void tnew(int, int);
334 static void tnewline(int);
335 static void tputtab(bool);
336 static void tputc(char *, int);
337 static void treset(void);
338 static int tresize(int, int);
339 static void tscrollup(int, int);
340 static void tscrolldown(int, int);
341 static void tsetattr(int*, int);
342 static void tsetchar(char *, Glyph
*, int, int);
343 static void tsetscroll(int, int);
344 static void tswapscreen(void);
345 static void tsetdirt(int, int);
346 static void tsetdirtattr(int);
347 static void tsetmode(bool, bool, int *, int);
348 static void tfulldirt(void);
349 static void techo(char *, int);
351 static inline bool match(uint
, uint
);
352 static void ttynew(void);
353 static void ttyread(void);
354 static void ttyresize(void);
355 static void ttywrite(const char *, size_t);
357 static void xdraws(char *, Glyph
, int, int, int, int);
358 static void xhints(void);
359 static void xclear(int, int, int, int);
360 static void xdrawcursor(void);
361 static void xinit(void);
362 static void xloadcols(void);
363 static int xsetcolorname(int, const char *);
364 static int xloadfont(Font
*, FcPattern
*);
365 static void xloadfonts(char *, int);
366 static int xloadfontset(Font
*);
367 static void xsettitle(char *);
368 static void xresettitle(void);
369 static void xseturgency(int);
370 static void xsetsel(char*);
371 static void xtermclear(int, int, int, int);
372 static void xunloadfont(Font
*f
);
373 static void xunloadfonts(void);
374 static void xresize(int, int);
376 static void expose(XEvent
*);
377 static void visibility(XEvent
*);
378 static void unmap(XEvent
*);
379 static char *kmap(KeySym
, uint
);
380 static void kpress(XEvent
*);
381 static void cmessage(XEvent
*);
382 static void cresize(int, int);
383 static void resize(XEvent
*);
384 static void focus(XEvent
*);
385 static void brelease(XEvent
*);
386 static void bpress(XEvent
*);
387 static void bmotion(XEvent
*);
388 static void selnotify(XEvent
*);
389 static void selclear(XEvent
*);
390 static void selrequest(XEvent
*);
392 static void selinit(void);
393 static inline bool selected(int, int);
394 static void selcopy(void);
395 static void selscroll(int, int);
396 static void selsnap(int, int *, int *, int);
398 static int utf8decode(char *, long *);
399 static int utf8encode(long *, char *);
400 static int utf8size(char *);
401 static int isfullutf8(char *, int);
403 static ssize_t
xwrite(int, char *, size_t);
404 static void *xmalloc(size_t);
405 static void *xrealloc(void *, size_t);
406 static void *xcalloc(size_t, size_t);
408 static void (*handler
[LASTEvent
])(XEvent
*) = {
410 [ClientMessage
] = cmessage
,
411 [ConfigureNotify
] = resize
,
412 [VisibilityNotify
] = visibility
,
413 [UnmapNotify
] = unmap
,
417 [MotionNotify
] = bmotion
,
418 [ButtonPress
] = bpress
,
419 [ButtonRelease
] = brelease
,
420 [SelectionClear
] = selclear
,
421 [SelectionNotify
] = selnotify
,
422 [SelectionRequest
] = selrequest
,
429 static CSIEscape csiescseq
;
430 static STREscape strescseq
;
433 static Selection sel
;
434 static int iofd
= -1;
435 static char **opt_cmd
= NULL
;
436 static char *opt_io
= NULL
;
437 static char *opt_title
= NULL
;
438 static char *opt_embed
= NULL
;
439 static char *opt_class
= NULL
;
440 static char *opt_font
= NULL
;
442 static char *usedfont
= NULL
;
443 static int usedfontsize
= 0;
445 /* Font Ring Cache */
460 * Fontcache is a ring buffer, with frccur as current position and frclen as
461 * the current length of used elements.
464 static Fontcache frc
[1024];
465 static int frccur
= -1, frclen
= 0;
468 xwrite(int fd
, char *s
, size_t len
) {
472 ssize_t r
= write(fd
, s
, len
);
482 xmalloc(size_t len
) {
483 void *p
= malloc(len
);
486 die("Out of memory\n");
492 xrealloc(void *p
, size_t len
) {
493 if((p
= realloc(p
, len
)) == NULL
)
494 die("Out of memory\n");
500 xcalloc(size_t nmemb
, size_t size
) {
501 void *p
= calloc(nmemb
, size
);
504 die("Out of memory\n");
510 utf8decode(char *s
, long *u
) {
516 if(~c
& B7
) { /* 0xxxxxxx */
519 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
520 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
522 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
523 *u
= c
&(B3
|B2
|B1
|B0
);
525 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
532 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
534 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
537 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
540 if((n
== 1 && *u
< 0x80) ||
541 (n
== 2 && *u
< 0x800) ||
542 (n
== 3 && *u
< 0x10000) ||
543 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
555 utf8encode(long *u
, char *s
) {
563 *sp
= uc
; /* 0xxxxxxx */
565 } else if(*u
< 0x800) {
566 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
568 } else if(uc
< 0x10000) {
569 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
571 } else if(uc
<= 0x10FFFF) {
572 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
578 for(i
=n
,++sp
; i
>0; --i
,++sp
)
579 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
591 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
592 UTF-8 otherwise return 0 */
594 isfullutf8(char *s
, int b
) {
602 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
604 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
606 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
608 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
610 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
611 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
624 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
626 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
635 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
636 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
640 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
641 if(sel
.xtarget
== None
)
642 sel
.xtarget
= XA_STRING
;
650 return LIMIT(x
, 0, term
.col
-1);
658 return LIMIT(y
, 0, term
.row
-1);
662 selected(int x
, int y
) {
665 if(sel
.ey
== y
&& sel
.by
== y
) {
666 bx
= MIN(sel
.bx
, sel
.ex
);
667 ex
= MAX(sel
.bx
, sel
.ex
);
669 return BETWEEN(x
, bx
, ex
);
672 if(sel
.type
== SEL_RECTANGULAR
) {
673 return ((sel
.b
.y
<= y
&& y
<= sel
.e
.y
)
674 && (sel
.b
.x
<= x
&& x
<= sel
.e
.x
));
676 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
677 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
678 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
679 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
683 selsnap(int mode
, int *x
, int *y
, int direction
) {
689 * Snap around if the word wraps around at the end or
690 * beginning of a line.
693 if(direction
< 0 && *x
<= 0) {
694 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
702 if(direction
> 0 && *x
>= term
.col
-1) {
703 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
712 if(strchr(worddelimiters
,
713 term
.line
[*y
][*x
+ direction
].c
[0])) {
722 * Snap around if the the previous line or the current one
723 * has set ATTR_WRAP at its end. Then the whole next or
724 * previous line will be selected.
726 *x
= (direction
< 0) ? 0 : term
.col
- 1;
727 if(direction
< 0 && *y
> 0) {
728 for(; *y
> 0; *y
+= direction
) {
729 if(!(term
.line
[*y
-1][term
.col
-1].mode
734 } else if(direction
> 0 && *y
< term
.row
-1) {
735 for(; *y
< term
.row
; *y
+= direction
) {
736 if(!(term
.line
[*y
][term
.col
-1].mode
745 * Select the whole line when the end of line is reached.
749 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
759 getbuttoninfo(XEvent
*e
) {
761 uint state
= e
->xbutton
.state
&~Button1Mask
;
763 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
765 sel
.ex
= x2col(e
->xbutton
.x
);
766 sel
.ey
= y2row(e
->xbutton
.y
);
769 || (sel
.by
== sel
.ey
&& sel
.bx
< sel
.ex
)) {
770 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, -1);
771 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, +1);
773 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, -1);
774 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, +1);
777 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
778 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
779 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
780 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
782 sel
.type
= SEL_REGULAR
;
783 for(type
= 1; type
< LEN(selmasks
); ++type
) {
784 if(match(selmasks
[type
], state
)) {
792 mousereport(XEvent
*e
) {
793 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
794 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
797 static int ob
, ox
, oy
;
800 if(e
->xbutton
.type
== MotionNotify
) {
801 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
805 } else if(!IS_SET(MODE_MOUSESGR
)
806 && (e
->xbutton
.type
== ButtonRelease
807 || button
== AnyButton
)) {
813 if(e
->xbutton
.type
== ButtonPress
) {
819 button
+= (state
& ShiftMask
? 4 : 0)
820 + (state
& Mod4Mask
? 8 : 0)
821 + (state
& ControlMask
? 16 : 0);
824 if(IS_SET(MODE_MOUSESGR
)) {
825 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
827 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
828 } else if(x
< 223 && y
< 223) {
829 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
830 32+button
, 32+x
+1, 32+y
+1);
843 if(IS_SET(MODE_MOUSE
)) {
848 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
849 if(e
->xbutton
.button
== mk
->b
850 && match(mk
->mask
, e
->xbutton
.state
)) {
851 ttywrite(mk
->s
, strlen(mk
->s
));
852 if(IS_SET(MODE_ECHO
))
853 techo(mk
->s
, strlen(mk
->s
));
858 if(e
->xbutton
.button
== Button1
) {
859 gettimeofday(&now
, NULL
);
861 /* Clear previous selection, logically and visually. */
864 tsetdirt(sel
.b
.y
, sel
.e
.y
);
868 sel
.type
= SEL_REGULAR
;
869 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
870 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
873 * If the user clicks below predefined timeouts specific
874 * snapping behaviour is exposed.
876 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
877 sel
.snap
= SNAP_LINE
;
878 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
879 sel
.snap
= SNAP_WORD
;
883 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, -1);
884 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, +1);
891 * Draw selection, unless it's regular and we don't want to
892 * make clicks visible
896 tsetdirt(sel
.b
.y
, sel
.e
.y
);
899 sel
.tclick2
= sel
.tclick1
;
907 int x
, y
, bufsize
, size
, i
, ex
;
913 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
914 ptr
= str
= xmalloc(bufsize
);
916 /* append every set & selected glyph to the selection */
917 for(y
= sel
.b
.y
; y
< sel
.e
.y
+ 1; y
++) {
918 gp
= &term
.line
[y
][0];
919 last
= gp
+ term
.col
;
921 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
922 strcmp(last
->c
, " ") != 0))
925 for(x
= 0; gp
<= last
; x
++, ++gp
) {
929 size
= utf8size(gp
->c
);
930 memcpy(ptr
, gp
->c
, size
);
935 * Copy and pasting of line endings is inconsistent
936 * in the inconsistent terminal and GUI world.
937 * The best solution seems like to produce '\n' when
938 * something is copied from st and convert '\n' to
939 * '\r', when something to be pasted is received by
941 * FIXME: Fix the computer world.
943 if(y
< sel
.e
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
947 * If the last selected line expands in the selection
948 * after the visible text '\n' is appended.
952 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
955 if(sel
.b
.y
== sel
.e
.y
&& sel
.e
.x
< sel
.b
.x
)
967 selnotify(XEvent
*e
) {
968 ulong nitems
, ofs
, rem
;
970 uchar
*data
, *last
, *repl
;
975 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
976 False
, AnyPropertyType
, &type
, &format
,
977 &nitems
, &rem
, &data
)) {
978 fprintf(stderr
, "Clipboard allocation failed\n");
983 * As seen in selcopy:
984 * Line endings are inconsistent in the terminal and GUI world
985 * copy and pasting. When receiving some selection data,
986 * replace all '\n' with '\r'.
987 * FIXME: Fix the computer world.
990 last
= data
+ nitems
* format
/ 8;
991 while((repl
= memchr(repl
, '\n', last
- repl
))) {
995 ttywrite((const char *)data
, nitems
* format
/ 8);
997 /* number of 32-bit chunks returned */
998 ofs
+= nitems
* format
/ 32;
1003 selpaste(const Arg
*dummy
) {
1004 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1005 xw
.win
, CurrentTime
);
1009 clippaste(const Arg
*dummy
) {
1012 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1013 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1014 xw
.win
, CurrentTime
);
1018 selclear(XEvent
*e
) {
1022 tsetdirt(sel
.b
.y
, sel
.e
.y
);
1026 selrequest(XEvent
*e
) {
1027 XSelectionRequestEvent
*xsre
;
1028 XSelectionEvent xev
;
1029 Atom xa_targets
, string
;
1031 xsre
= (XSelectionRequestEvent
*) e
;
1032 xev
.type
= SelectionNotify
;
1033 xev
.requestor
= xsre
->requestor
;
1034 xev
.selection
= xsre
->selection
;
1035 xev
.target
= xsre
->target
;
1036 xev
.time
= xsre
->time
;
1038 xev
.property
= None
;
1040 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1041 if(xsre
->target
== xa_targets
) {
1042 /* respond with the supported type */
1043 string
= sel
.xtarget
;
1044 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1045 XA_ATOM
, 32, PropModeReplace
,
1046 (uchar
*) &string
, 1);
1047 xev
.property
= xsre
->property
;
1048 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1049 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1050 xsre
->target
, 8, PropModeReplace
,
1051 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1052 xev
.property
= xsre
->property
;
1055 /* all done, send a notification to the listener */
1056 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1057 fprintf(stderr
, "Error sending SelectionNotify event\n");
1061 xsetsel(char *str
) {
1062 /* register the selection for both the clipboard and the primary */
1068 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1070 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1071 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1075 brelease(XEvent
*e
) {
1076 if(IS_SET(MODE_MOUSE
)) {
1081 if(e
->xbutton
.button
== Button2
) {
1083 } else if(e
->xbutton
.button
== Button1
) {
1091 term
.dirty
[sel
.ey
] = 1;
1096 bmotion(XEvent
*e
) {
1097 int oldey
, oldex
, oldsby
, oldsey
;
1099 if(IS_SET(MODE_MOUSE
)) {
1114 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
1115 tsetdirt(MIN(sel
.b
.y
, oldsby
), MAX(sel
.e
.y
, oldsey
));
1120 die(const char *errstr
, ...) {
1123 va_start(ap
, errstr
);
1124 vfprintf(stderr
, errstr
, ap
);
1132 char *envshell
= getenv("SHELL");
1133 const struct passwd
*pass
= getpwuid(getuid());
1134 char buf
[sizeof(long) * 8 + 1];
1136 unsetenv("COLUMNS");
1138 unsetenv("TERMCAP");
1141 setenv("LOGNAME", pass
->pw_name
, 1);
1142 setenv("USER", pass
->pw_name
, 1);
1143 setenv("SHELL", pass
->pw_shell
, 0);
1144 setenv("HOME", pass
->pw_dir
, 0);
1147 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1148 setenv("WINDOWID", buf
, 1);
1150 signal(SIGCHLD
, SIG_DFL
);
1151 signal(SIGHUP
, SIG_DFL
);
1152 signal(SIGINT
, SIG_DFL
);
1153 signal(SIGQUIT
, SIG_DFL
);
1154 signal(SIGTERM
, SIG_DFL
);
1155 signal(SIGALRM
, SIG_DFL
);
1157 DEFAULT(envshell
, shell
);
1158 setenv("TERM", termname
, 1);
1159 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1160 execvp(args
[0], args
);
1168 if(waitpid(pid
, &stat
, 0) < 0)
1169 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1171 if(WIFEXITED(stat
)) {
1172 exit(WEXITSTATUS(stat
));
1181 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1183 /* seems to work fine on linux, openbsd and freebsd */
1184 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1185 die("openpty failed: %s\n", SERRNO
);
1187 switch(pid
= fork()) {
1189 die("fork failed\n");
1192 setsid(); /* create a new process group */
1193 dup2(s
, STDIN_FILENO
);
1194 dup2(s
, STDOUT_FILENO
);
1195 dup2(s
, STDERR_FILENO
);
1196 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1197 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1205 signal(SIGCHLD
, sigchld
);
1207 iofd
= (!strcmp(opt_io
, "-")) ?
1209 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1211 fprintf(stderr
, "Error opening %s:%s\n",
1212 opt_io
, strerror(errno
));
1222 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1224 fprintf(stderr
, "\n");
1229 static char buf
[BUFSIZ
];
1230 static int buflen
= 0;
1233 int charsize
; /* size of utf8 char in bytes */
1237 /* append read bytes to unprocessed bytes */
1238 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1239 die("Couldn't read from shell: %s\n", SERRNO
);
1241 /* process every complete utf8 char */
1244 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1245 charsize
= utf8decode(ptr
, &utf8c
);
1246 utf8encode(&utf8c
, s
);
1252 /* keep any uncomplete utf8 char for the next call */
1253 memmove(buf
, ptr
, buflen
);
1257 ttywrite(const char *s
, size_t n
) {
1258 if(write(cmdfd
, s
, n
) == -1)
1259 die("write error on tty: %s\n", SERRNO
);
1266 w
.ws_row
= term
.row
;
1267 w
.ws_col
= term
.col
;
1268 w
.ws_xpixel
= xw
.tw
;
1269 w
.ws_ypixel
= xw
.th
;
1270 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1271 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1275 tattrset(int attr
) {
1278 for(i
= 0; i
< term
.row
-1; i
++) {
1279 for(j
= 0; j
< term
.col
-1; j
++) {
1280 if(term
.line
[i
][j
].mode
& attr
)
1289 tsetdirt(int top
, int bot
) {
1292 LIMIT(top
, 0, term
.row
-1);
1293 LIMIT(bot
, 0, term
.row
-1);
1295 for(i
= top
; i
<= bot
; i
++)
1300 tsetdirtattr(int attr
) {
1303 for(i
= 0; i
< term
.row
-1; i
++) {
1304 for(j
= 0; j
< term
.col
-1; j
++) {
1305 if(term
.line
[i
][j
].mode
& attr
) {
1315 tsetdirt(0, term
.row
-1);
1322 if(mode
== CURSOR_SAVE
) {
1324 } else if(mode
== CURSOR_LOAD
) {
1334 term
.c
= (TCursor
){{
1338 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1340 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1341 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1344 term
.bot
= term
.row
- 1;
1345 term
.mode
= MODE_WRAP
;
1347 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1349 tcursor(CURSOR_SAVE
);
1353 tnew(int col
, int row
) {
1354 memset(&term
, 0, sizeof(Term
));
1363 Line
*tmp
= term
.line
;
1365 term
.line
= term
.alt
;
1367 term
.mode
^= MODE_ALTSCREEN
;
1372 tscrolldown(int orig
, int n
) {
1376 LIMIT(n
, 0, term
.bot
-orig
+1);
1378 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1380 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1381 temp
= term
.line
[i
];
1382 term
.line
[i
] = term
.line
[i
-n
];
1383 term
.line
[i
-n
] = temp
;
1386 term
.dirty
[i
-n
] = 1;
1393 tscrollup(int orig
, int n
) {
1396 LIMIT(n
, 0, term
.bot
-orig
+1);
1398 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1400 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1401 temp
= term
.line
[i
];
1402 term
.line
[i
] = term
.line
[i
+n
];
1403 term
.line
[i
+n
] = temp
;
1406 term
.dirty
[i
+n
] = 1;
1409 selscroll(orig
, -n
);
1413 selscroll(int orig
, int n
) {
1417 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1418 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1422 if(sel
.type
== SEL_RECTANGULAR
) {
1423 if(sel
.by
< term
.top
)
1425 if(sel
.ey
> term
.bot
)
1428 if(sel
.by
< term
.top
) {
1432 if(sel
.ey
> term
.bot
) {
1437 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1438 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1443 tnewline(int first_col
) {
1447 tscrollup(term
.top
, 1);
1451 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1456 char *p
= csiescseq
.buf
, *np
;
1465 csiescseq
.buf
[csiescseq
.len
] = '\0';
1466 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1468 v
= strtol(p
, &np
, 10);
1471 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1473 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1475 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1479 csiescseq
.mode
= *p
;
1482 /* for absolute user moves, when decom is set */
1484 tmoveato(int x
, int y
) {
1485 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1489 tmoveto(int x
, int y
) {
1492 if(term
.c
.state
& CURSOR_ORIGIN
) {
1497 maxy
= term
.row
- 1;
1499 LIMIT(x
, 0, term
.col
-1);
1500 LIMIT(y
, miny
, maxy
);
1501 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1507 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1508 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1509 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1510 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1511 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1512 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1513 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1514 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1515 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1516 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1520 * The table is proudly stolen from rxvt.
1522 if(attr
->mode
& ATTR_GFX
) {
1523 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1524 && vt100_0
[c
[0] - 0x41]) {
1525 c
= vt100_0
[c
[0] - 0x41];
1530 term
.line
[y
][x
] = *attr
;
1531 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1535 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1539 temp
= x1
, x1
= x2
, x2
= temp
;
1541 temp
= y1
, y1
= y2
, y2
= temp
;
1543 LIMIT(x1
, 0, term
.col
-1);
1544 LIMIT(x2
, 0, term
.col
-1);
1545 LIMIT(y1
, 0, term
.row
-1);
1546 LIMIT(y2
, 0, term
.row
-1);
1548 for(y
= y1
; y
<= y2
; y
++) {
1550 for(x
= x1
; x
<= x2
; x
++) {
1553 term
.line
[y
][x
] = term
.c
.attr
;
1554 memcpy(term
.line
[y
][x
].c
, " ", 2);
1560 tdeletechar(int n
) {
1561 int src
= term
.c
.x
+ n
;
1563 int size
= term
.col
- src
;
1565 term
.dirty
[term
.c
.y
] = 1;
1567 if(src
>= term
.col
) {
1568 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1572 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1573 size
* sizeof(Glyph
));
1574 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1578 tinsertblank(int n
) {
1581 int size
= term
.col
- dst
;
1583 term
.dirty
[term
.c
.y
] = 1;
1585 if(dst
>= term
.col
) {
1586 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1590 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1591 size
* sizeof(Glyph
));
1592 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1596 tinsertblankline(int n
) {
1597 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1600 tscrolldown(term
.c
.y
, n
);
1604 tdeleteline(int n
) {
1605 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1608 tscrollup(term
.c
.y
, n
);
1612 tsetattr(int *attr
, int l
) {
1615 for(i
= 0; i
< l
; i
++) {
1618 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1619 | ATTR_BOLD
| ATTR_ITALIC \
1621 term
.c
.attr
.fg
= defaultfg
;
1622 term
.c
.attr
.bg
= defaultbg
;
1625 term
.c
.attr
.mode
|= ATTR_BOLD
;
1628 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1631 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1633 case 5: /* slow blink */
1634 case 6: /* rapid blink */
1635 term
.c
.attr
.mode
|= ATTR_BLINK
;
1638 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1642 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1645 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1648 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1652 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1655 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1658 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1660 if(BETWEEN(attr
[i
], 0, 255)) {
1661 term
.c
.attr
.fg
= attr
[i
];
1664 "erresc: bad fgcolor %d\n",
1669 "erresc(38): gfx attr %d unknown\n",
1674 term
.c
.attr
.fg
= defaultfg
;
1677 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1679 if(BETWEEN(attr
[i
], 0, 255)) {
1680 term
.c
.attr
.bg
= attr
[i
];
1683 "erresc: bad bgcolor %d\n",
1688 "erresc(48): gfx attr %d unknown\n",
1693 term
.c
.attr
.bg
= defaultbg
;
1696 if(BETWEEN(attr
[i
], 30, 37)) {
1697 term
.c
.attr
.fg
= attr
[i
] - 30;
1698 } else if(BETWEEN(attr
[i
], 40, 47)) {
1699 term
.c
.attr
.bg
= attr
[i
] - 40;
1700 } else if(BETWEEN(attr
[i
], 90, 97)) {
1701 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1702 } else if(BETWEEN(attr
[i
], 100, 107)) {
1703 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1706 "erresc(default): gfx attr %d unknown\n",
1707 attr
[i
]), csidump();
1715 tsetscroll(int t
, int b
) {
1718 LIMIT(t
, 0, term
.row
-1);
1719 LIMIT(b
, 0, term
.row
-1);
1729 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1732 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1736 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1740 case 1: /* DECCKM -- Cursor key */
1741 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1743 case 5: /* DECSCNM -- Reverse video */
1745 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1746 if(mode
!= term
.mode
)
1747 redraw(REDRAW_TIMEOUT
);
1749 case 6: /* DECOM -- Origin */
1750 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1753 case 7: /* DECAWM -- Auto wrap */
1754 MODBIT(term
.mode
, set
, MODE_WRAP
);
1756 case 0: /* Error (IGNORED) */
1757 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1758 case 3: /* DECCOLM -- Column (IGNORED) */
1759 case 4: /* DECSCLM -- Scroll (IGNORED) */
1760 case 8: /* DECARM -- Auto repeat (IGNORED) */
1761 case 18: /* DECPFF -- Printer feed (IGNORED) */
1762 case 19: /* DECPEX -- Printer extent (IGNORED) */
1763 case 42: /* DECNRCM -- National characters (IGNORED) */
1764 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1766 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1767 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1769 case 1000: /* 1000,1002: enable xterm mouse report */
1770 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1771 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1774 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1775 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1778 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1781 MODBIT(term
.mode
, set
, MODE_8BIT
);
1783 case 1049: /* = 1047 and 1048 */
1786 if (!allowaltscreen
)
1789 alt
= IS_SET(MODE_ALTSCREEN
);
1791 tclearregion(0, 0, term
.col
-1,
1794 if(set
^ alt
) /* set is always 1 or 0 */
1800 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1804 "erresc: unknown private set/reset mode %d\n",
1810 case 0: /* Error (IGNORED) */
1812 case 2: /* KAM -- keyboard action */
1813 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1815 case 4: /* IRM -- Insertion-replacement */
1816 MODBIT(term
.mode
, set
, MODE_INSERT
);
1818 case 12: /* SRM -- Send/Receive */
1819 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1821 case 20: /* LNM -- Linefeed/new line */
1822 MODBIT(term
.mode
, set
, MODE_CRLF
);
1826 "erresc: unknown set/reset mode %d\n",
1838 switch(csiescseq
.mode
) {
1841 fprintf(stderr
, "erresc: unknown csi ");
1845 case '@': /* ICH -- Insert <n> blank char */
1846 DEFAULT(csiescseq
.arg
[0], 1);
1847 tinsertblank(csiescseq
.arg
[0]);
1849 case 'A': /* CUU -- Cursor <n> Up */
1850 DEFAULT(csiescseq
.arg
[0], 1);
1851 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1853 case 'B': /* CUD -- Cursor <n> Down */
1854 case 'e': /* VPR --Cursor <n> Down */
1855 DEFAULT(csiescseq
.arg
[0], 1);
1856 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1858 case 'c': /* DA -- Device Attributes */
1859 if(csiescseq
.arg
[0] == 0)
1860 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1862 case 'C': /* CUF -- Cursor <n> Forward */
1863 case 'a': /* HPR -- Cursor <n> Forward */
1864 DEFAULT(csiescseq
.arg
[0], 1);
1865 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1867 case 'D': /* CUB -- Cursor <n> Backward */
1868 DEFAULT(csiescseq
.arg
[0], 1);
1869 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1871 case 'E': /* CNL -- Cursor <n> Down and first col */
1872 DEFAULT(csiescseq
.arg
[0], 1);
1873 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1875 case 'F': /* CPL -- Cursor <n> Up and first col */
1876 DEFAULT(csiescseq
.arg
[0], 1);
1877 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1879 case 'g': /* TBC -- Tabulation clear */
1880 switch(csiescseq
.arg
[0]) {
1881 case 0: /* clear current tab stop */
1882 term
.tabs
[term
.c
.x
] = 0;
1884 case 3: /* clear all the tabs */
1885 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1891 case 'G': /* CHA -- Move to <col> */
1893 DEFAULT(csiescseq
.arg
[0], 1);
1894 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1896 case 'H': /* CUP -- Move to <row> <col> */
1898 DEFAULT(csiescseq
.arg
[0], 1);
1899 DEFAULT(csiescseq
.arg
[1], 1);
1900 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1902 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1903 DEFAULT(csiescseq
.arg
[0], 1);
1904 while(csiescseq
.arg
[0]--)
1907 case 'J': /* ED -- Clear screen */
1909 switch(csiescseq
.arg
[0]) {
1911 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1912 if(term
.c
.y
< term
.row
-1) {
1913 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1919 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1920 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1923 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1929 case 'K': /* EL -- Clear line */
1930 switch(csiescseq
.arg
[0]) {
1932 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1936 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1939 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1943 case 'S': /* SU -- Scroll <n> line up */
1944 DEFAULT(csiescseq
.arg
[0], 1);
1945 tscrollup(term
.top
, csiescseq
.arg
[0]);
1947 case 'T': /* SD -- Scroll <n> line down */
1948 DEFAULT(csiescseq
.arg
[0], 1);
1949 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1951 case 'L': /* IL -- Insert <n> blank lines */
1952 DEFAULT(csiescseq
.arg
[0], 1);
1953 tinsertblankline(csiescseq
.arg
[0]);
1955 case 'l': /* RM -- Reset Mode */
1956 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1958 case 'M': /* DL -- Delete <n> lines */
1959 DEFAULT(csiescseq
.arg
[0], 1);
1960 tdeleteline(csiescseq
.arg
[0]);
1962 case 'X': /* ECH -- Erase <n> char */
1963 DEFAULT(csiescseq
.arg
[0], 1);
1964 tclearregion(term
.c
.x
, term
.c
.y
,
1965 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1967 case 'P': /* DCH -- Delete <n> char */
1968 DEFAULT(csiescseq
.arg
[0], 1);
1969 tdeletechar(csiescseq
.arg
[0]);
1971 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1972 DEFAULT(csiescseq
.arg
[0], 1);
1973 while(csiescseq
.arg
[0]--)
1976 case 'd': /* VPA -- Move to <row> */
1977 DEFAULT(csiescseq
.arg
[0], 1);
1978 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1980 case 'h': /* SM -- Set terminal mode */
1981 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1983 case 'm': /* SGR -- Terminal attribute (color) */
1984 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1986 case 'r': /* DECSTBM -- Set Scrolling Region */
1987 if(csiescseq
.priv
) {
1990 DEFAULT(csiescseq
.arg
[0], 1);
1991 DEFAULT(csiescseq
.arg
[1], term
.row
);
1992 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1996 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1997 tcursor(CURSOR_SAVE
);
1999 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2000 tcursor(CURSOR_LOAD
);
2011 for(i
= 0; i
< csiescseq
.len
; i
++) {
2012 c
= csiescseq
.buf
[i
] & 0xff;
2015 } else if(c
== '\n') {
2017 } else if(c
== '\r') {
2019 } else if(c
== 0x1b) {
2022 printf("(%02x)", c
);
2030 memset(&csiescseq
, 0, sizeof(csiescseq
));
2039 narg
= strescseq
.narg
;
2041 switch(strescseq
.type
) {
2042 case ']': /* OSC -- Operating System Command */
2043 switch(i
= atoi(strescseq
.args
[0])) {
2048 xsettitle(strescseq
.args
[1]);
2050 case 4: /* color set */
2053 p
= strescseq
.args
[2];
2055 case 104: /* color reset, here p = NULL */
2056 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2057 if (!xsetcolorname(j
, p
)) {
2058 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2061 * TODO if defaultbg color is changed, borders
2068 fprintf(stderr
, "erresc: unknown str ");
2073 case 'k': /* old title set compatibility */
2074 xsettitle(strescseq
.args
[0]);
2076 case 'P': /* DSC -- Device Control String */
2077 case '_': /* APC -- Application Program Command */
2078 case '^': /* PM -- Privacy Message */
2080 fprintf(stderr
, "erresc: unknown str ");
2089 char *p
= strescseq
.buf
;
2092 strescseq
.buf
[strescseq
.len
] = '\0';
2093 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2094 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2102 printf("ESC%c", strescseq
.type
);
2103 for(i
= 0; i
< strescseq
.len
; i
++) {
2104 c
= strescseq
.buf
[i
] & 0xff;
2107 } else if(isprint(c
)) {
2109 } else if(c
== '\n') {
2111 } else if(c
== '\r') {
2113 } else if(c
== 0x1b) {
2116 printf("(%02x)", c
);
2124 memset(&strescseq
, 0, sizeof(strescseq
));
2128 tputtab(bool forward
) {
2134 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2139 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2142 tmoveto(x
, term
.c
.y
);
2146 techo(char *buf
, int len
) {
2147 for(; len
> 0; buf
++, len
--) {
2150 if(c
== '\033') { /* escape */
2153 } else if(c
< '\x20') { /* control code */
2154 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2168 tputc(char *c
, int len
) {
2170 bool control
= ascii
< '\x20' || ascii
== 0177;
2173 if(xwrite(iofd
, c
, len
) < 0) {
2174 fprintf(stderr
, "Error writing in %s:%s\n",
2175 opt_io
, strerror(errno
));
2182 * STR sequences must be checked before anything else
2183 * because it can use some control codes as part of the sequence.
2185 if(term
.esc
& ESC_STR
) {
2188 term
.esc
= ESC_START
| ESC_STR_END
;
2190 case '\a': /* backwards compatibility to xterm */
2195 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2196 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2197 strescseq
.len
+= len
;
2200 * Here is a bug in terminals. If the user never sends
2201 * some code to stop the str or esc command, then st
2202 * will stop responding. But this is better than
2203 * silently failing with unknown characters. At least
2204 * then users will report back.
2206 * In the case users ever get fixed, here is the code:
2218 * Actions of control codes must be performed as soon they arrive
2219 * because they can be embedded inside a control sequence, and
2220 * they must not cause conflicts with sequences.
2228 tmoveto(term
.c
.x
-1, term
.c
.y
);
2231 tmoveto(0, term
.c
.y
);
2236 /* go to first col if the mode is set */
2237 tnewline(IS_SET(MODE_CRLF
));
2239 case '\a': /* BEL */
2240 if(!(xw
.state
& WIN_FOCUSED
))
2243 case '\033': /* ESC */
2245 term
.esc
= ESC_START
;
2247 case '\016': /* SO */
2248 case '\017': /* SI */
2250 * Different charsets are hard to handle. Applications
2251 * should use the right alt charset escapes for the
2252 * only reason they still exist: line drawing. The
2253 * rest is incompatible history st should not support.
2256 case '\032': /* SUB */
2257 case '\030': /* CAN */
2260 case '\005': /* ENQ (IGNORED) */
2261 case '\000': /* NUL (IGNORED) */
2262 case '\021': /* XON (IGNORED) */
2263 case '\023': /* XOFF (IGNORED) */
2264 case 0177: /* DEL (IGNORED) */
2267 } else if(term
.esc
& ESC_START
) {
2268 if(term
.esc
& ESC_CSI
) {
2269 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2270 if(BETWEEN(ascii
, 0x40, 0x7E)
2271 || csiescseq
.len
>= \
2272 sizeof(csiescseq
.buf
)-1) {
2277 } else if(term
.esc
& ESC_STR_END
) {
2281 } else if(term
.esc
& ESC_ALTCHARSET
) {
2283 case '0': /* Line drawing set */
2284 term
.c
.attr
.mode
|= ATTR_GFX
;
2286 case 'B': /* USASCII */
2287 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2289 case 'A': /* UK (IGNORED) */
2290 case '<': /* multinational charset (IGNORED) */
2291 case '5': /* Finnish (IGNORED) */
2292 case 'C': /* Finnish (IGNORED) */
2293 case 'K': /* German (IGNORED) */
2296 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2299 } else if(term
.esc
& ESC_TEST
) {
2300 if(ascii
== '8') { /* DEC screen alignment test. */
2301 char E
[UTF_SIZ
] = "E";
2304 for(x
= 0; x
< term
.col
; ++x
) {
2305 for(y
= 0; y
< term
.row
; ++y
)
2306 tsetchar(E
, &term
.c
.attr
, x
, y
);
2313 term
.esc
|= ESC_CSI
;
2316 term
.esc
|= ESC_TEST
;
2318 case 'P': /* DCS -- Device Control String */
2319 case '_': /* APC -- Application Program Command */
2320 case '^': /* PM -- Privacy Message */
2321 case ']': /* OSC -- Operating System Command */
2322 case 'k': /* old title set compatibility */
2324 strescseq
.type
= ascii
;
2325 term
.esc
|= ESC_STR
;
2327 case '(': /* set primary charset G0 */
2328 term
.esc
|= ESC_ALTCHARSET
;
2330 case ')': /* set secondary charset G1 (IGNORED) */
2331 case '*': /* set tertiary charset G2 (IGNORED) */
2332 case '+': /* set quaternary charset G3 (IGNORED) */
2335 case 'D': /* IND -- Linefeed */
2336 if(term
.c
.y
== term
.bot
) {
2337 tscrollup(term
.top
, 1);
2339 tmoveto(term
.c
.x
, term
.c
.y
+1);
2343 case 'E': /* NEL -- Next line */
2344 tnewline(1); /* always go to first col */
2347 case 'H': /* HTS -- Horizontal tab stop */
2348 term
.tabs
[term
.c
.x
] = 1;
2351 case 'M': /* RI -- Reverse index */
2352 if(term
.c
.y
== term
.top
) {
2353 tscrolldown(term
.top
, 1);
2355 tmoveto(term
.c
.x
, term
.c
.y
-1);
2359 case 'Z': /* DECID -- Identify Terminal */
2360 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2363 case 'c': /* RIS -- Reset to inital state */
2368 case '=': /* DECPAM -- Application keypad */
2369 term
.mode
|= MODE_APPKEYPAD
;
2372 case '>': /* DECPNM -- Normal keypad */
2373 term
.mode
&= ~MODE_APPKEYPAD
;
2376 case '7': /* DECSC -- Save Cursor */
2377 tcursor(CURSOR_SAVE
);
2380 case '8': /* DECRC -- Restore Cursor */
2381 tcursor(CURSOR_LOAD
);
2384 case '\\': /* ST -- Stop */
2388 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2389 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2394 * All characters which form part of a sequence are not
2400 * Display control codes only if we are in graphic mode
2402 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2404 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2406 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2407 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2411 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2412 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2413 &term
.line
[term
.c
.y
][term
.c
.x
],
2414 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2417 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2418 if(term
.c
.x
+1 < term
.col
) {
2419 tmoveto(term
.c
.x
+1, term
.c
.y
);
2421 term
.c
.state
|= CURSOR_WRAPNEXT
;
2426 tresize(int col
, int row
) {
2428 int minrow
= MIN(row
, term
.row
);
2429 int mincol
= MIN(col
, term
.col
);
2430 int slide
= term
.c
.y
- row
+ 1;
2434 if(col
< 1 || row
< 1)
2437 /* free unneeded rows */
2441 * slide screen to keep cursor where we expect it -
2442 * tscrollup would work here, but we can optimize to
2443 * memmove because we're freeing the earlier lines
2445 for(/* i = 0 */; i
< slide
; i
++) {
2449 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2450 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2452 for(i
+= row
; i
< term
.row
; i
++) {
2457 /* resize to new height */
2458 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2459 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2460 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2461 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2463 /* resize each row to new width, zero-pad if needed */
2464 for(i
= 0; i
< minrow
; i
++) {
2466 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2467 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2470 /* allocate any new rows */
2471 for(/* i == minrow */; i
< row
; i
++) {
2473 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2474 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2476 if(col
> term
.col
) {
2477 bp
= term
.tabs
+ term
.col
;
2479 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2480 while(--bp
> term
.tabs
&& !*bp
)
2482 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2485 /* update terminal size */
2488 /* reset scrolling region */
2489 tsetscroll(0, row
-1);
2490 /* make use of the LIMIT in tmoveto */
2491 tmoveto(term
.c
.x
, term
.c
.y
);
2492 /* Clearing both screens */
2495 if(mincol
< col
&& 0 < minrow
) {
2496 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2498 if(0 < col
&& minrow
< row
) {
2499 tclearregion(0, minrow
, col
- 1, row
- 1);
2502 } while(orig
!= term
.line
);
2508 xresize(int col
, int row
) {
2509 xw
.tw
= MAX(1, col
* xw
.cw
);
2510 xw
.th
= MAX(1, row
* xw
.ch
);
2512 XFreePixmap(xw
.dpy
, xw
.buf
);
2513 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2514 DefaultDepth(xw
.dpy
, xw
.scr
));
2515 XftDrawChange(xw
.draw
, xw
.buf
);
2516 xclear(0, 0, xw
.w
, xw
.h
);
2519 static inline ushort
2520 sixd_to_16bit(int x
) {
2521 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2527 XRenderColor color
= { .alpha
= 0xffff };
2529 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2530 for(i
= 0; i
< LEN(colorname
); i
++) {
2533 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2534 die("Could not allocate color '%s'\n", colorname
[i
]);
2538 /* load colors [16-255] ; same colors as xterm */
2539 for(i
= 16, r
= 0; r
< 6; r
++) {
2540 for(g
= 0; g
< 6; g
++) {
2541 for(b
= 0; b
< 6; b
++) {
2542 color
.red
= sixd_to_16bit(r
);
2543 color
.green
= sixd_to_16bit(g
);
2544 color
.blue
= sixd_to_16bit(b
);
2545 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2546 die("Could not allocate color %d\n", i
);
2553 for(r
= 0; r
< 24; r
++, i
++) {
2554 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2555 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2557 die("Could not allocate color %d\n", i
);
2563 xsetcolorname(int x
, const char *name
) {
2564 XRenderColor color
= { .alpha
= 0xffff };
2566 if (x
< 0 || x
> LEN(colorname
))
2569 if(16 <= x
&& x
< 16 + 216) {
2570 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2571 color
.red
= sixd_to_16bit(r
);
2572 color
.green
= sixd_to_16bit(g
);
2573 color
.blue
= sixd_to_16bit(b
);
2574 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2575 return 0; /* something went wrong */
2578 } else if (16 + 216 <= x
&& x
< 256) {
2579 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2580 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2581 return 0; /* something went wrong */
2585 name
= colorname
[x
];
2588 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2595 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2596 XftDrawRect(xw
.draw
,
2597 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2598 borderpx
+ col1
* xw
.cw
,
2599 borderpx
+ row1
* xw
.ch
,
2600 (col2
-col1
+1) * xw
.cw
,
2601 (row2
-row1
+1) * xw
.ch
);
2605 * Absolute coordinates.
2608 xclear(int x1
, int y1
, int x2
, int y2
) {
2609 XftDrawRect(xw
.draw
,
2610 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2611 x1
, y1
, x2
-x1
, y2
-y1
);
2616 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2617 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2618 XSizeHints
*sizeh
= NULL
;
2620 sizeh
= XAllocSizeHints();
2621 if(xw
.isfixed
== False
) {
2622 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2623 sizeh
->height
= xw
.h
;
2624 sizeh
->width
= xw
.w
;
2625 sizeh
->height_inc
= xw
.ch
;
2626 sizeh
->width_inc
= xw
.cw
;
2627 sizeh
->base_height
= 2 * borderpx
;
2628 sizeh
->base_width
= 2 * borderpx
;
2630 sizeh
->flags
= PMaxSize
| PMinSize
;
2631 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2632 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2635 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2640 xloadfont(Font
*f
, FcPattern
*pattern
) {
2644 match
= FcFontMatch(NULL
, pattern
, &result
);
2648 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2649 FcPatternDestroy(match
);
2654 f
->pattern
= FcPatternDuplicate(pattern
);
2656 f
->ascent
= f
->match
->ascent
;
2657 f
->descent
= f
->match
->descent
;
2659 f
->rbearing
= f
->match
->max_advance_width
;
2661 f
->height
= f
->ascent
+ f
->descent
;
2662 f
->width
= f
->lbearing
+ f
->rbearing
;
2668 xloadfonts(char *fontstr
, int fontsize
) {
2673 if(fontstr
[0] == '-') {
2674 pattern
= XftXlfdParse(fontstr
, False
, False
);
2676 pattern
= FcNameParse((FcChar8
*)fontstr
);
2680 die("st: can't open font %s\n", fontstr
);
2683 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2684 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2685 usedfontsize
= fontsize
;
2687 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2688 if(result
== FcResultMatch
) {
2689 usedfontsize
= (int)fontval
;
2692 * Default font size is 12, if none given. This is to
2693 * have a known usedfontsize value.
2695 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2700 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2701 FcDefaultSubstitute(pattern
);
2703 if(xloadfont(&dc
.font
, pattern
))
2704 die("st: can't open font %s\n", fontstr
);
2706 /* Setting character width and height. */
2707 xw
.cw
= dc
.font
.width
;
2708 xw
.ch
= dc
.font
.height
;
2710 FcPatternDel(pattern
, FC_SLANT
);
2711 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2712 if(xloadfont(&dc
.ifont
, pattern
))
2713 die("st: can't open font %s\n", fontstr
);
2715 FcPatternDel(pattern
, FC_WEIGHT
);
2716 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2717 if(xloadfont(&dc
.ibfont
, pattern
))
2718 die("st: can't open font %s\n", fontstr
);
2720 FcPatternDel(pattern
, FC_SLANT
);
2721 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2722 if(xloadfont(&dc
.bfont
, pattern
))
2723 die("st: can't open font %s\n", fontstr
);
2725 FcPatternDestroy(pattern
);
2729 xloadfontset(Font
*f
) {
2732 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2738 xunloadfont(Font
*f
) {
2739 XftFontClose(xw
.dpy
, f
->match
);
2740 FcPatternDestroy(f
->pattern
);
2742 FcFontSetDestroy(f
->set
);
2746 xunloadfonts(void) {
2750 * Free the loaded fonts in the font cache. This is done backwards
2753 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2756 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2761 xunloadfont(&dc
.font
);
2762 xunloadfont(&dc
.bfont
);
2763 xunloadfont(&dc
.ifont
);
2764 xunloadfont(&dc
.ibfont
);
2768 xzoom(const Arg
*arg
) {
2770 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2777 XSetWindowAttributes attrs
;
2783 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2784 die("Can't open display\n");
2785 xw
.scr
= XDefaultScreen(xw
.dpy
);
2786 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2790 die("Could not init fontconfig.\n");
2792 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2793 xloadfonts(usedfont
, 0);
2796 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2799 /* adjust fixed window geometry */
2801 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2802 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2804 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2806 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2811 /* window - default size */
2812 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2813 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2819 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2820 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2821 attrs
.bit_gravity
= NorthWestGravity
;
2822 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2823 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2824 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2825 attrs
.colormap
= xw
.cmap
;
2827 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2828 XRootWindow(xw
.dpy
, xw
.scr
);
2829 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2830 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2832 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2836 memset(&gcvalues
, 0, sizeof(gcvalues
));
2837 gcvalues
.graphics_exposures
= False
;
2838 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2840 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2841 DefaultDepth(xw
.dpy
, xw
.scr
));
2842 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2843 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2845 /* Xft rendering context */
2846 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2849 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2850 XSetLocaleModifiers("@im=local");
2851 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2852 XSetLocaleModifiers("@im=");
2853 if((xw
.xim
= XOpenIM(xw
.dpy
,
2854 NULL
, NULL
, NULL
)) == NULL
) {
2855 die("XOpenIM failed. Could not open input"
2860 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2861 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2862 XNFocusWindow
, xw
.win
, NULL
);
2864 die("XCreateIC failed. Could not obtain input method.\n");
2866 /* white cursor, black outline */
2867 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2868 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2869 XRecolorCursor(xw
.dpy
, cursor
,
2870 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2871 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2873 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2874 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2875 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2878 XMapWindow(xw
.dpy
, xw
.win
);
2884 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2885 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2886 width
= charlen
* xw
.cw
, xp
, i
;
2888 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2891 Font
*font
= &dc
.font
;
2893 FcPattern
*fcpattern
, *fontpattern
;
2894 FcFontSet
*fcsets
[] = { NULL
};
2895 FcCharSet
*fccharset
;
2896 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2897 XRenderColor colfg
, colbg
;
2900 frcflags
= FRC_NORMAL
;
2902 if(base
.mode
& ATTR_ITALIC
) {
2903 if(base
.fg
== defaultfg
)
2904 base
.fg
= defaultitalic
;
2906 frcflags
= FRC_ITALIC
;
2907 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2908 if(base
.fg
== defaultfg
)
2909 base
.fg
= defaultitalic
;
2911 frcflags
= FRC_ITALICBOLD
;
2912 } else if(base
.mode
& ATTR_UNDERLINE
) {
2913 if(base
.fg
== defaultfg
)
2914 base
.fg
= defaultunderline
;
2916 fg
= &dc
.col
[base
.fg
];
2917 bg
= &dc
.col
[base
.bg
];
2919 if(base
.mode
& ATTR_BOLD
) {
2920 if(BETWEEN(base
.fg
, 0, 7)) {
2921 /* basic system colors */
2922 fg
= &dc
.col
[base
.fg
+ 8];
2923 } else if(BETWEEN(base
.fg
, 16, 195)) {
2925 fg
= &dc
.col
[base
.fg
+ 36];
2926 } else if(BETWEEN(base
.fg
, 232, 251)) {
2928 fg
= &dc
.col
[base
.fg
+ 4];
2931 * Those ranges will not be brightened:
2932 * 8 - 15 – bright system colors
2933 * 196 - 231 – highest 256 color cube
2934 * 252 - 255 – brightest colors in greyscale
2937 frcflags
= FRC_BOLD
;
2940 if(IS_SET(MODE_REVERSE
)) {
2941 if(fg
== &dc
.col
[defaultfg
]) {
2942 fg
= &dc
.col
[defaultbg
];
2944 colfg
.red
= ~fg
->color
.red
;
2945 colfg
.green
= ~fg
->color
.green
;
2946 colfg
.blue
= ~fg
->color
.blue
;
2947 colfg
.alpha
= fg
->color
.alpha
;
2948 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2952 if(bg
== &dc
.col
[defaultbg
]) {
2953 bg
= &dc
.col
[defaultfg
];
2955 colbg
.red
= ~bg
->color
.red
;
2956 colbg
.green
= ~bg
->color
.green
;
2957 colbg
.blue
= ~bg
->color
.blue
;
2958 colbg
.alpha
= bg
->color
.alpha
;
2959 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2964 if(base
.mode
& ATTR_REVERSE
) {
2970 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
2973 /* Intelligent cleaning up of the borders. */
2975 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2976 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2978 if(x
+ charlen
>= term
.col
) {
2979 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2980 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2983 xclear(winx
, 0, winx
+ width
, borderpx
);
2985 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2987 /* Clean up the region we want to draw to. */
2988 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2990 /* Set the clip region because Xft is sometimes dirty. */
2995 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
2997 for(xp
= winx
; bytelen
> 0;) {
2999 * Search for the range in the to be printed string of glyphs
3000 * that are in the main font. Then print that range. If
3001 * some glyph is found that is not in the font, do the
3009 u8cblen
= utf8decode(s
, &u8char
);
3013 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
3014 if(!doesexist
|| bytelen
<= 0) {
3023 XftDrawStringUtf8(xw
.draw
, fg
,
3025 winy
+ font
->ascent
,
3028 xp
+= font
->width
* u8fl
;
3041 /* Search the font cache. */
3042 for(i
= 0; i
< frclen
; i
++, frp
--) {
3046 if(frc
[frp
].c
== u8char
3047 && frc
[frp
].flags
== frcflags
) {
3052 /* Nothing was found. */
3056 fcsets
[0] = font
->set
;
3059 * Nothing was found in the cache. Now use
3060 * some dozen of Fontconfig calls to get the
3061 * font for one single character.
3063 fcpattern
= FcPatternDuplicate(font
->pattern
);
3064 fccharset
= FcCharSetCreate();
3066 FcCharSetAddChar(fccharset
, u8char
);
3067 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3069 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3072 FcConfigSubstitute(0, fcpattern
,
3074 FcDefaultSubstitute(fcpattern
);
3076 fontpattern
= FcFontSetMatch(0, fcsets
,
3077 FcTrue
, fcpattern
, &fcres
);
3080 * Overwrite or create the new cache entry.
3084 if(frccur
>= LEN(frc
))
3086 if(frclen
> LEN(frc
)) {
3088 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
3091 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
3093 frc
[frccur
].c
= u8char
;
3094 frc
[frccur
].flags
= frcflags
;
3096 FcPatternDestroy(fcpattern
);
3097 FcCharSetDestroy(fccharset
);
3102 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
3103 xp
, winy
+ frc
[frp
].font
->ascent
,
3104 (FcChar8
*)u8c
, u8cblen
);
3110 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3111 winy + font->ascent, (FcChar8 *)s, bytelen);
3114 if(base
.mode
& ATTR_UNDERLINE
) {
3115 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3119 /* Reset clip to none. */
3120 XftDrawSetClip(xw
.draw
, 0);
3125 static int oldx
= 0, oldy
= 0;
3127 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3129 LIMIT(oldx
, 0, term
.col
-1);
3130 LIMIT(oldy
, 0, term
.row
-1);
3132 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3134 /* remove the old cursor */
3135 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3136 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3139 /* draw the new one */
3140 if(!(IS_SET(MODE_HIDE
))) {
3141 if(xw
.state
& WIN_FOCUSED
) {
3142 if(IS_SET(MODE_REVERSE
)) {
3143 g
.mode
|= ATTR_REVERSE
;
3149 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3151 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3152 borderpx
+ term
.c
.x
* xw
.cw
,
3153 borderpx
+ term
.c
.y
* xw
.ch
,
3155 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3156 borderpx
+ term
.c
.x
* xw
.cw
,
3157 borderpx
+ term
.c
.y
* xw
.ch
,
3159 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3160 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3161 borderpx
+ term
.c
.y
* xw
.ch
,
3163 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3164 borderpx
+ term
.c
.x
* xw
.cw
,
3165 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3168 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3174 xsettitle(char *p
) {
3177 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3179 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3184 xsettitle(opt_title
? opt_title
: "st");
3188 redraw(int timeout
) {
3189 struct timespec tv
= {0, timeout
* 1000};
3195 nanosleep(&tv
, NULL
);
3196 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3202 drawregion(0, 0, term
.col
, term
.row
);
3203 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3205 XSetForeground(xw
.dpy
, dc
.gc
,
3206 dc
.col
[IS_SET(MODE_REVERSE
)?
3207 defaultfg
: defaultbg
].pixel
);
3211 drawregion(int x1
, int y1
, int x2
, int y2
) {
3212 int ic
, ib
, x
, y
, ox
, sl
;
3214 char buf
[DRAW_BUF_SIZ
];
3215 bool ena_sel
= sel
.bx
!= -1;
3217 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3220 if(!(xw
.state
& WIN_VISIBLE
))
3223 for(y
= y1
; y
< y2
; y
++) {
3227 xtermclear(0, y
, term
.col
, y
);
3229 base
= term
.line
[y
][0];
3231 for(x
= x1
; x
< x2
; x
++) {
3232 new = term
.line
[y
][x
];
3233 if(ena_sel
&& selected(x
, y
))
3234 new.mode
^= ATTR_REVERSE
;
3235 if(ib
> 0 && (ATTRCMP(base
, new)
3236 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3237 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3245 sl
= utf8size(new.c
);
3246 memcpy(buf
+ib
, new.c
, sl
);
3251 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3257 expose(XEvent
*ev
) {
3258 XExposeEvent
*e
= &ev
->xexpose
;
3260 if(xw
.state
& WIN_REDRAW
) {
3262 xw
.state
&= ~WIN_REDRAW
;
3268 visibility(XEvent
*ev
) {
3269 XVisibilityEvent
*e
= &ev
->xvisibility
;
3271 if(e
->state
== VisibilityFullyObscured
) {
3272 xw
.state
&= ~WIN_VISIBLE
;
3273 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3274 /* need a full redraw for next Expose, not just a buf copy */
3275 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3281 xw
.state
&= ~WIN_VISIBLE
;
3285 xseturgency(int add
) {
3286 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3288 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3289 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3295 XFocusChangeEvent
*e
= &ev
->xfocus
;
3297 if(e
->mode
== NotifyGrab
)
3300 if(ev
->type
== FocusIn
) {
3301 XSetICFocus(xw
.xic
);
3302 xw
.state
|= WIN_FOCUSED
;
3305 XUnsetICFocus(xw
.xic
);
3306 xw
.state
&= ~WIN_FOCUSED
;
3311 match(uint mask
, uint state
) {
3312 state
&= ~(ignoremod
);
3314 if(mask
== XK_NO_MOD
&& state
)
3316 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3318 if((state
& mask
) != state
)
3324 numlock(const Arg
*dummy
) {
3329 kmap(KeySym k
, uint state
) {
3334 /* Check for mapped keys out of X11 function keys. */
3335 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3336 if(mappedkeys
[i
] == k
)
3339 if(i
== LEN(mappedkeys
)) {
3340 if((k
& 0xFFFF) < 0xFD00)
3344 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3350 if(!match(mask
, state
))
3353 if(kp
->appkey
> 0) {
3354 if(!IS_SET(MODE_APPKEYPAD
))
3356 if(term
.numlock
&& kp
->appkey
== 2)
3358 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3362 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3364 && !IS_SET(MODE_APPCURSOR
))) {
3368 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3369 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3380 kpress(XEvent
*ev
) {
3381 XKeyEvent
*e
= &ev
->xkey
;
3383 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3389 if(IS_SET(MODE_KBDLOCK
))
3392 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3393 e
->state
&= ~Mod2Mask
;
3395 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3396 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3397 bp
->func(&(bp
->arg
));
3402 /* 2. custom keys from config.h */
3403 if((customkey
= kmap(ksym
, e
->state
))) {
3404 len
= strlen(customkey
);
3405 memcpy(buf
, customkey
, len
);
3406 /* 3. hardcoded (overrides X lookup) */
3411 if(len
== 1 && e
->state
& Mod1Mask
) {
3412 if(IS_SET(MODE_8BIT
)) {
3415 ret
= utf8encode(&c
, cp
);
3424 memcpy(cp
, xstr
, len
);
3425 len
= cp
- buf
+ len
;
3429 if(IS_SET(MODE_ECHO
))
3435 cmessage(XEvent
*e
) {
3438 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3440 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3441 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3442 xw
.state
|= WIN_FOCUSED
;
3444 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3445 xw
.state
&= ~WIN_FOCUSED
;
3447 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3448 /* Send SIGHUP to shell */
3455 cresize(int width
, int height
) {
3463 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3464 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3473 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3476 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3483 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3484 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3486 gettimeofday(&lastblink
, NULL
);
3487 gettimeofday(&last
, NULL
);
3489 for(xev
= actionfps
;;) {
3491 FD_SET(cmdfd
, &rfd
);
3494 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3497 die("select failed: %s\n", SERRNO
);
3499 if(FD_ISSET(cmdfd
, &rfd
)) {
3502 blinkset
= tattrset(ATTR_BLINK
);
3503 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3504 term
.mode
&= ~(MODE_BLINK
);
3508 if(FD_ISSET(xfd
, &rfd
))
3511 gettimeofday(&now
, NULL
);
3512 drawtimeout
.tv_sec
= 0;
3513 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3517 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3518 tsetdirtattr(ATTR_BLINK
);
3519 term
.mode
^= MODE_BLINK
;
3520 gettimeofday(&lastblink
, NULL
);
3523 if(TIMEDIFF(now
, last
) \
3524 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3530 while(XPending(xw
.dpy
)) {
3531 XNextEvent(xw
.dpy
, &ev
);
3532 if(XFilterEvent(&ev
, None
))
3534 if(handler
[ev
.type
])
3535 (handler
[ev
.type
])(&ev
);
3541 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3543 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3545 if(TIMEDIFF(now
, lastblink
) \
3547 drawtimeout
.tv_usec
= 1;
3549 drawtimeout
.tv_usec
= (1000 * \
3564 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3565 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3566 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3570 main(int argc
, char *argv
[]) {
3574 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3579 allowaltscreen
= false;
3582 opt_class
= EARGF(usage());
3585 /* eat all remaining arguments */
3590 opt_font
= EARGF(usage());
3593 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3598 if(bitm
& WidthValue
)
3600 if(bitm
& HeightValue
)
3602 if(bitm
& XNegative
&& xw
.fx
== 0)
3604 if(bitm
& XNegative
&& xw
.fy
== 0)
3607 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3611 opt_io
= EARGF(usage());
3614 opt_title
= EARGF(usage());
3617 opt_embed
= EARGF(usage());
3625 setlocale(LC_CTYPE
, "");
3626 XSetLocaleModifiers("");
3632 cresize(xw
.h
, xw
.w
);