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... */
253 * Selection variables:
254 * nb – normalized coordinates of the beginning of the selection
255 * ne – normalized coordinates of the end of the selection
256 * ob – original coordinates of the beginning of the selection
257 * oe – original coordinates of the end of the selection
266 struct timeval tclick1
;
267 struct timeval tclick2
;
280 void (*func
)(const Arg
*);
284 /* function definitions used in config.h */
285 static void clippaste(const Arg
*);
286 static void numlock(const Arg
*);
287 static void selpaste(const Arg
*);
288 static void xzoom(const Arg
*);
290 /* Config.h for applying patches and the configuration. */
306 /* Drawing Context */
308 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
309 Font font
, bfont
, ifont
, ibfont
;
313 static void die(const char *, ...);
314 static void draw(void);
315 static void redraw(int);
316 static void drawregion(int, int, int, int);
317 static void execsh(void);
318 static void sigchld(int);
319 static void run(void);
321 static void csidump(void);
322 static void csihandle(void);
323 static void csiparse(void);
324 static void csireset(void);
325 static void strdump(void);
326 static void strhandle(void);
327 static void strparse(void);
328 static void strreset(void);
330 static int tattrset(int);
331 static void tclearregion(int, int, int, int);
332 static void tcursor(int);
333 static void tdeletechar(int);
334 static void tdeleteline(int);
335 static void tinsertblank(int);
336 static void tinsertblankline(int);
337 static void tmoveto(int, int);
338 static void tmoveato(int x
, int y
);
339 static void tnew(int, int);
340 static void tnewline(int);
341 static void tputtab(bool);
342 static void tputc(char *, int);
343 static void treset(void);
344 static int tresize(int, int);
345 static void tscrollup(int, int);
346 static void tscrolldown(int, int);
347 static void tsetattr(int*, int);
348 static void tsetchar(char *, Glyph
*, int, int);
349 static void tsetscroll(int, int);
350 static void tswapscreen(void);
351 static void tsetdirt(int, int);
352 static void tsetdirtattr(int);
353 static void tsetmode(bool, bool, int *, int);
354 static void tfulldirt(void);
355 static void techo(char *, int);
357 static inline bool match(uint
, uint
);
358 static void ttynew(void);
359 static void ttyread(void);
360 static void ttyresize(void);
361 static void ttywrite(const char *, size_t);
363 static void xdraws(char *, Glyph
, int, int, int, int);
364 static void xhints(void);
365 static void xclear(int, int, int, int);
366 static void xdrawcursor(void);
367 static void xinit(void);
368 static void xloadcols(void);
369 static int xsetcolorname(int, const char *);
370 static int xloadfont(Font
*, FcPattern
*);
371 static void xloadfonts(char *, int);
372 static int xloadfontset(Font
*);
373 static void xsettitle(char *);
374 static void xresettitle(void);
375 static void xseturgency(int);
376 static void xsetsel(char*);
377 static void xtermclear(int, int, int, int);
378 static void xunloadfont(Font
*f
);
379 static void xunloadfonts(void);
380 static void xresize(int, int);
382 static void expose(XEvent
*);
383 static void visibility(XEvent
*);
384 static void unmap(XEvent
*);
385 static char *kmap(KeySym
, uint
);
386 static void kpress(XEvent
*);
387 static void cmessage(XEvent
*);
388 static void cresize(int, int);
389 static void resize(XEvent
*);
390 static void focus(XEvent
*);
391 static void brelease(XEvent
*);
392 static void bpress(XEvent
*);
393 static void bmotion(XEvent
*);
394 static void selnotify(XEvent
*);
395 static void selclear(XEvent
*);
396 static void selrequest(XEvent
*);
398 static void selinit(void);
399 static void selsort(void);
400 static inline bool selected(int, int);
401 static void selcopy(void);
402 static void selscroll(int, int);
403 static void selsnap(int, int *, int *, int);
405 static int utf8decode(char *, long *);
406 static int utf8encode(long *, char *);
407 static int utf8size(char *);
408 static int isfullutf8(char *, int);
410 static ssize_t
xwrite(int, char *, size_t);
411 static void *xmalloc(size_t);
412 static void *xrealloc(void *, size_t);
413 static void *xcalloc(size_t, size_t);
415 static void (*handler
[LASTEvent
])(XEvent
*) = {
417 [ClientMessage
] = cmessage
,
418 [ConfigureNotify
] = resize
,
419 [VisibilityNotify
] = visibility
,
420 [UnmapNotify
] = unmap
,
424 [MotionNotify
] = bmotion
,
425 [ButtonPress
] = bpress
,
426 [ButtonRelease
] = brelease
,
427 [SelectionClear
] = selclear
,
428 [SelectionNotify
] = selnotify
,
429 [SelectionRequest
] = selrequest
,
436 static CSIEscape csiescseq
;
437 static STREscape strescseq
;
440 static Selection sel
;
441 static int iofd
= -1;
442 static char **opt_cmd
= NULL
;
443 static char *opt_io
= NULL
;
444 static char *opt_title
= NULL
;
445 static char *opt_embed
= NULL
;
446 static char *opt_class
= NULL
;
447 static char *opt_font
= NULL
;
449 static char *usedfont
= NULL
;
450 static int usedfontsize
= 0;
452 /* Font Ring Cache */
467 * Fontcache is a ring buffer, with frccur as current position and frclen as
468 * the current length of used elements.
471 static Fontcache frc
[1024];
472 static int frccur
= -1, frclen
= 0;
475 xwrite(int fd
, char *s
, size_t len
) {
479 ssize_t r
= write(fd
, s
, len
);
489 xmalloc(size_t len
) {
490 void *p
= malloc(len
);
493 die("Out of memory\n");
499 xrealloc(void *p
, size_t len
) {
500 if((p
= realloc(p
, len
)) == NULL
)
501 die("Out of memory\n");
507 xcalloc(size_t nmemb
, size_t size
) {
508 void *p
= calloc(nmemb
, size
);
511 die("Out of memory\n");
517 utf8decode(char *s
, long *u
) {
523 if(~c
& B7
) { /* 0xxxxxxx */
526 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
527 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
529 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
530 *u
= c
&(B3
|B2
|B1
|B0
);
532 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
539 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
541 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
544 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
547 if((n
== 1 && *u
< 0x80) ||
548 (n
== 2 && *u
< 0x800) ||
549 (n
== 3 && *u
< 0x10000) ||
550 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
562 utf8encode(long *u
, char *s
) {
570 *sp
= uc
; /* 0xxxxxxx */
572 } else if(*u
< 0x800) {
573 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
575 } else if(uc
< 0x10000) {
576 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
578 } else if(uc
<= 0x10FFFF) {
579 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
585 for(i
=n
,++sp
; i
>0; --i
,++sp
)
586 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
598 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
599 UTF-8 otherwise return 0 */
601 isfullutf8(char *s
, int b
) {
609 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
611 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
613 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
615 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
617 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
618 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
631 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
633 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
642 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
643 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
647 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
648 if(sel
.xtarget
== None
)
649 sel
.xtarget
= XA_STRING
;
657 return LIMIT(x
, 0, term
.col
-1);
665 return LIMIT(y
, 0, term
.row
-1);
670 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
671 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
672 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
673 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
677 selected(int x
, int y
) {
678 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
679 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.y
);
681 if(sel
.type
== SEL_RECTANGULAR
) {
682 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
683 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
686 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
687 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
688 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
689 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
693 selsnap(int mode
, int *x
, int *y
, int direction
) {
699 * Snap around if the word wraps around at the end or
700 * beginning of a line.
703 if(direction
< 0 && *x
<= 0) {
704 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
712 if(direction
> 0 && *x
>= term
.col
-1) {
713 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
722 if(strchr(worddelimiters
,
723 term
.line
[*y
][*x
+ direction
].c
[0])) {
732 * Snap around if the the previous line or the current one
733 * has set ATTR_WRAP at its end. Then the whole next or
734 * previous line will be selected.
736 *x
= (direction
< 0) ? 0 : term
.col
- 1;
737 if(direction
< 0 && *y
> 0) {
738 for(; *y
> 0; *y
+= direction
) {
739 if(!(term
.line
[*y
-1][term
.col
-1].mode
744 } else if(direction
> 0 && *y
< term
.row
-1) {
745 for(; *y
< term
.row
; *y
+= direction
) {
746 if(!(term
.line
[*y
][term
.col
-1].mode
755 * Select the whole line when the end of line is reached.
759 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
769 getbuttoninfo(XEvent
*e
) {
771 uint state
= e
->xbutton
.state
&~Button1Mask
;
773 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
775 sel
.oe
.x
= x2col(e
->xbutton
.x
);
776 sel
.oe
.y
= y2row(e
->xbutton
.y
);
778 if (sel
.ob
.y
< sel
.oe
.y
779 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
780 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
781 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
783 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
784 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
789 sel
.type
= SEL_REGULAR
;
790 for(type
= 1; type
< LEN(selmasks
); ++type
) {
791 if(match(selmasks
[type
], state
)) {
799 mousereport(XEvent
*e
) {
800 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
801 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
804 static int ob
, ox
, oy
;
807 if(e
->xbutton
.type
== MotionNotify
) {
808 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
813 } else if(!IS_SET(MODE_MOUSESGR
)
814 && (e
->xbutton
.type
== ButtonRelease
815 || button
== AnyButton
)) {
821 if(e
->xbutton
.type
== ButtonPress
) {
828 button
+= (state
& ShiftMask
? 4 : 0)
829 + (state
& Mod4Mask
? 8 : 0)
830 + (state
& ControlMask
? 16 : 0);
833 if(IS_SET(MODE_MOUSESGR
)) {
834 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
836 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
837 } else if(x
< 223 && y
< 223) {
838 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
839 32+button
, 32+x
+1, 32+y
+1);
852 if(IS_SET(MODE_MOUSE
)) {
857 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
858 if(e
->xbutton
.button
== mk
->b
859 && match(mk
->mask
, e
->xbutton
.state
)) {
860 ttywrite(mk
->s
, strlen(mk
->s
));
861 if(IS_SET(MODE_ECHO
))
862 techo(mk
->s
, strlen(mk
->s
));
867 if(e
->xbutton
.button
== Button1
) {
868 gettimeofday(&now
, NULL
);
870 /* Clear previous selection, logically and visually. */
873 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
877 sel
.type
= SEL_REGULAR
;
878 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
879 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
882 * If the user clicks below predefined timeouts specific
883 * snapping behaviour is exposed.
885 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
886 sel
.snap
= SNAP_LINE
;
887 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
888 sel
.snap
= SNAP_WORD
;
892 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
893 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
897 * Draw selection, unless it's regular and we don't want to
898 * make clicks visible
902 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
905 sel
.tclick2
= sel
.tclick1
;
913 int x
, y
, bufsize
, size
, i
, ex
;
919 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
920 ptr
= str
= xmalloc(bufsize
);
922 /* append every set & selected glyph to the selection */
923 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
924 gp
= &term
.line
[y
][0];
925 last
= gp
+ term
.col
;
927 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
928 strcmp(last
->c
, " ") != 0))
931 for(x
= 0; gp
<= last
; x
++, ++gp
) {
935 size
= utf8size(gp
->c
);
936 memcpy(ptr
, gp
->c
, size
);
941 * Copy and pasting of line endings is inconsistent
942 * in the inconsistent terminal and GUI world.
943 * The best solution seems like to produce '\n' when
944 * something is copied from st and convert '\n' to
945 * '\r', when something to be pasted is received by
947 * FIXME: Fix the computer world.
949 if(y
< sel
.ne
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
953 * If the last selected line expands in the selection
954 * after the visible text '\n' is appended.
958 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
961 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
973 selnotify(XEvent
*e
) {
974 ulong nitems
, ofs
, rem
;
976 uchar
*data
, *last
, *repl
;
981 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
982 False
, AnyPropertyType
, &type
, &format
,
983 &nitems
, &rem
, &data
)) {
984 fprintf(stderr
, "Clipboard allocation failed\n");
989 * As seen in selcopy:
990 * Line endings are inconsistent in the terminal and GUI world
991 * copy and pasting. When receiving some selection data,
992 * replace all '\n' with '\r'.
993 * FIXME: Fix the computer world.
996 last
= data
+ nitems
* format
/ 8;
997 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1001 ttywrite((const char *)data
, nitems
* format
/ 8);
1003 /* number of 32-bit chunks returned */
1004 ofs
+= nitems
* format
/ 32;
1009 selpaste(const Arg
*dummy
) {
1010 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1011 xw
.win
, CurrentTime
);
1015 clippaste(const Arg
*dummy
) {
1018 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1019 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1020 xw
.win
, CurrentTime
);
1024 selclear(XEvent
*e
) {
1028 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1032 selrequest(XEvent
*e
) {
1033 XSelectionRequestEvent
*xsre
;
1034 XSelectionEvent xev
;
1035 Atom xa_targets
, string
;
1037 xsre
= (XSelectionRequestEvent
*) e
;
1038 xev
.type
= SelectionNotify
;
1039 xev
.requestor
= xsre
->requestor
;
1040 xev
.selection
= xsre
->selection
;
1041 xev
.target
= xsre
->target
;
1042 xev
.time
= xsre
->time
;
1044 xev
.property
= None
;
1046 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1047 if(xsre
->target
== xa_targets
) {
1048 /* respond with the supported type */
1049 string
= sel
.xtarget
;
1050 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1051 XA_ATOM
, 32, PropModeReplace
,
1052 (uchar
*) &string
, 1);
1053 xev
.property
= xsre
->property
;
1054 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1055 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1056 xsre
->target
, 8, PropModeReplace
,
1057 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1058 xev
.property
= xsre
->property
;
1061 /* all done, send a notification to the listener */
1062 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1063 fprintf(stderr
, "Error sending SelectionNotify event\n");
1067 xsetsel(char *str
) {
1068 /* register the selection for both the clipboard and the primary */
1074 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1076 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1077 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1081 brelease(XEvent
*e
) {
1082 if(IS_SET(MODE_MOUSE
)) {
1087 if(e
->xbutton
.button
== Button2
) {
1089 } else if(e
->xbutton
.button
== Button1
) {
1097 term
.dirty
[sel
.oe
.y
] = 1;
1102 bmotion(XEvent
*e
) {
1103 int oldey
, oldex
, oldsby
, oldsey
;
1105 if(IS_SET(MODE_MOUSE
)) {
1120 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1121 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1125 die(const char *errstr
, ...) {
1128 va_start(ap
, errstr
);
1129 vfprintf(stderr
, errstr
, ap
);
1137 char *envshell
= getenv("SHELL");
1138 const struct passwd
*pass
= getpwuid(getuid());
1139 char buf
[sizeof(long) * 8 + 1];
1141 unsetenv("COLUMNS");
1143 unsetenv("TERMCAP");
1146 setenv("LOGNAME", pass
->pw_name
, 1);
1147 setenv("USER", pass
->pw_name
, 1);
1148 setenv("SHELL", pass
->pw_shell
, 0);
1149 setenv("HOME", pass
->pw_dir
, 0);
1152 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1153 setenv("WINDOWID", buf
, 1);
1155 signal(SIGCHLD
, SIG_DFL
);
1156 signal(SIGHUP
, SIG_DFL
);
1157 signal(SIGINT
, SIG_DFL
);
1158 signal(SIGQUIT
, SIG_DFL
);
1159 signal(SIGTERM
, SIG_DFL
);
1160 signal(SIGALRM
, SIG_DFL
);
1162 DEFAULT(envshell
, shell
);
1163 setenv("TERM", termname
, 1);
1164 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1165 execvp(args
[0], args
);
1173 if(waitpid(pid
, &stat
, 0) < 0)
1174 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1176 if(WIFEXITED(stat
)) {
1177 exit(WEXITSTATUS(stat
));
1186 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1188 /* seems to work fine on linux, openbsd and freebsd */
1189 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1190 die("openpty failed: %s\n", SERRNO
);
1192 switch(pid
= fork()) {
1194 die("fork failed\n");
1197 setsid(); /* create a new process group */
1198 dup2(s
, STDIN_FILENO
);
1199 dup2(s
, STDOUT_FILENO
);
1200 dup2(s
, STDERR_FILENO
);
1201 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1202 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1210 signal(SIGCHLD
, sigchld
);
1212 iofd
= (!strcmp(opt_io
, "-")) ?
1214 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1216 fprintf(stderr
, "Error opening %s:%s\n",
1217 opt_io
, strerror(errno
));
1227 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1229 fprintf(stderr
, "\n");
1234 static char buf
[BUFSIZ
];
1235 static int buflen
= 0;
1238 int charsize
; /* size of utf8 char in bytes */
1242 /* append read bytes to unprocessed bytes */
1243 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1244 die("Couldn't read from shell: %s\n", SERRNO
);
1246 /* process every complete utf8 char */
1249 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1250 charsize
= utf8decode(ptr
, &utf8c
);
1251 utf8encode(&utf8c
, s
);
1257 /* keep any uncomplete utf8 char for the next call */
1258 memmove(buf
, ptr
, buflen
);
1262 ttywrite(const char *s
, size_t n
) {
1263 if(write(cmdfd
, s
, n
) == -1)
1264 die("write error on tty: %s\n", SERRNO
);
1271 w
.ws_row
= term
.row
;
1272 w
.ws_col
= term
.col
;
1273 w
.ws_xpixel
= xw
.tw
;
1274 w
.ws_ypixel
= xw
.th
;
1275 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1276 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1280 tattrset(int attr
) {
1283 for(i
= 0; i
< term
.row
-1; i
++) {
1284 for(j
= 0; j
< term
.col
-1; j
++) {
1285 if(term
.line
[i
][j
].mode
& attr
)
1294 tsetdirt(int top
, int bot
) {
1297 LIMIT(top
, 0, term
.row
-1);
1298 LIMIT(bot
, 0, term
.row
-1);
1300 for(i
= top
; i
<= bot
; i
++)
1305 tsetdirtattr(int attr
) {
1308 for(i
= 0; i
< term
.row
-1; i
++) {
1309 for(j
= 0; j
< term
.col
-1; j
++) {
1310 if(term
.line
[i
][j
].mode
& attr
) {
1320 tsetdirt(0, term
.row
-1);
1327 if(mode
== CURSOR_SAVE
) {
1329 } else if(mode
== CURSOR_LOAD
) {
1339 term
.c
= (TCursor
){{
1343 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1345 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1346 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1349 term
.bot
= term
.row
- 1;
1350 term
.mode
= MODE_WRAP
;
1352 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1354 tcursor(CURSOR_SAVE
);
1358 tnew(int col
, int row
) {
1359 memset(&term
, 0, sizeof(Term
));
1368 Line
*tmp
= term
.line
;
1370 term
.line
= term
.alt
;
1372 term
.mode
^= MODE_ALTSCREEN
;
1377 tscrolldown(int orig
, int n
) {
1381 LIMIT(n
, 0, term
.bot
-orig
+1);
1383 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1385 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1386 temp
= term
.line
[i
];
1387 term
.line
[i
] = term
.line
[i
-n
];
1388 term
.line
[i
-n
] = temp
;
1391 term
.dirty
[i
-n
] = 1;
1398 tscrollup(int orig
, int n
) {
1401 LIMIT(n
, 0, term
.bot
-orig
+1);
1403 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1405 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1406 temp
= term
.line
[i
];
1407 term
.line
[i
] = term
.line
[i
+n
];
1408 term
.line
[i
+n
] = temp
;
1411 term
.dirty
[i
+n
] = 1;
1414 selscroll(orig
, -n
);
1418 selscroll(int orig
, int n
) {
1422 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1423 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1427 if(sel
.type
== SEL_RECTANGULAR
) {
1428 if(sel
.ob
.y
< term
.top
)
1429 sel
.ob
.y
= term
.top
;
1430 if(sel
.oe
.y
> term
.bot
)
1431 sel
.oe
.y
= term
.bot
;
1433 if(sel
.ob
.y
< term
.top
) {
1434 sel
.ob
.y
= term
.top
;
1437 if(sel
.oe
.y
> term
.bot
) {
1438 sel
.oe
.y
= term
.bot
;
1439 sel
.oe
.x
= term
.col
;
1447 tnewline(int first_col
) {
1451 tscrollup(term
.top
, 1);
1455 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1460 char *p
= csiescseq
.buf
, *np
;
1469 csiescseq
.buf
[csiescseq
.len
] = '\0';
1470 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1472 v
= strtol(p
, &np
, 10);
1475 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1477 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1479 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1483 csiescseq
.mode
= *p
;
1486 /* for absolute user moves, when decom is set */
1488 tmoveato(int x
, int y
) {
1489 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1493 tmoveto(int x
, int y
) {
1496 if(term
.c
.state
& CURSOR_ORIGIN
) {
1501 maxy
= term
.row
- 1;
1503 LIMIT(x
, 0, term
.col
-1);
1504 LIMIT(y
, miny
, maxy
);
1505 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1511 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1512 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1513 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1514 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1515 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1516 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1517 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1518 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1519 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1520 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1524 * The table is proudly stolen from rxvt.
1526 if(attr
->mode
& ATTR_GFX
) {
1527 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1528 && vt100_0
[c
[0] - 0x41]) {
1529 c
= vt100_0
[c
[0] - 0x41];
1534 term
.line
[y
][x
] = *attr
;
1535 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1539 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1543 temp
= x1
, x1
= x2
, x2
= temp
;
1545 temp
= y1
, y1
= y2
, y2
= temp
;
1547 LIMIT(x1
, 0, term
.col
-1);
1548 LIMIT(x2
, 0, term
.col
-1);
1549 LIMIT(y1
, 0, term
.row
-1);
1550 LIMIT(y2
, 0, term
.row
-1);
1552 for(y
= y1
; y
<= y2
; y
++) {
1554 for(x
= x1
; x
<= x2
; x
++) {
1557 term
.line
[y
][x
] = term
.c
.attr
;
1558 memcpy(term
.line
[y
][x
].c
, " ", 2);
1564 tdeletechar(int n
) {
1565 int src
= term
.c
.x
+ n
;
1567 int size
= term
.col
- src
;
1569 term
.dirty
[term
.c
.y
] = 1;
1571 if(src
>= term
.col
) {
1572 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1576 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1577 size
* sizeof(Glyph
));
1578 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1582 tinsertblank(int n
) {
1585 int size
= term
.col
- dst
;
1587 term
.dirty
[term
.c
.y
] = 1;
1589 if(dst
>= term
.col
) {
1590 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1594 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1595 size
* sizeof(Glyph
));
1596 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1600 tinsertblankline(int n
) {
1601 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1604 tscrolldown(term
.c
.y
, n
);
1608 tdeleteline(int n
) {
1609 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1612 tscrollup(term
.c
.y
, n
);
1616 tsetattr(int *attr
, int l
) {
1619 for(i
= 0; i
< l
; i
++) {
1622 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1623 | ATTR_BOLD
| ATTR_ITALIC \
1625 term
.c
.attr
.fg
= defaultfg
;
1626 term
.c
.attr
.bg
= defaultbg
;
1629 term
.c
.attr
.mode
|= ATTR_BOLD
;
1632 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1635 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1637 case 5: /* slow blink */
1638 case 6: /* rapid blink */
1639 term
.c
.attr
.mode
|= ATTR_BLINK
;
1642 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1646 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1649 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1652 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1656 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1659 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1662 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1664 if(BETWEEN(attr
[i
], 0, 255)) {
1665 term
.c
.attr
.fg
= attr
[i
];
1668 "erresc: bad fgcolor %d\n",
1673 "erresc(38): gfx attr %d unknown\n",
1678 term
.c
.attr
.fg
= defaultfg
;
1681 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1683 if(BETWEEN(attr
[i
], 0, 255)) {
1684 term
.c
.attr
.bg
= attr
[i
];
1687 "erresc: bad bgcolor %d\n",
1692 "erresc(48): gfx attr %d unknown\n",
1697 term
.c
.attr
.bg
= defaultbg
;
1700 if(BETWEEN(attr
[i
], 30, 37)) {
1701 term
.c
.attr
.fg
= attr
[i
] - 30;
1702 } else if(BETWEEN(attr
[i
], 40, 47)) {
1703 term
.c
.attr
.bg
= attr
[i
] - 40;
1704 } else if(BETWEEN(attr
[i
], 90, 97)) {
1705 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1706 } else if(BETWEEN(attr
[i
], 100, 107)) {
1707 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1710 "erresc(default): gfx attr %d unknown\n",
1711 attr
[i
]), csidump();
1719 tsetscroll(int t
, int b
) {
1722 LIMIT(t
, 0, term
.row
-1);
1723 LIMIT(b
, 0, term
.row
-1);
1733 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1736 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1740 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1744 case 1: /* DECCKM -- Cursor key */
1745 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1747 case 5: /* DECSCNM -- Reverse video */
1749 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1750 if(mode
!= term
.mode
)
1751 redraw(REDRAW_TIMEOUT
);
1753 case 6: /* DECOM -- Origin */
1754 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1757 case 7: /* DECAWM -- Auto wrap */
1758 MODBIT(term
.mode
, set
, MODE_WRAP
);
1760 case 0: /* Error (IGNORED) */
1761 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1762 case 3: /* DECCOLM -- Column (IGNORED) */
1763 case 4: /* DECSCLM -- Scroll (IGNORED) */
1764 case 8: /* DECARM -- Auto repeat (IGNORED) */
1765 case 18: /* DECPFF -- Printer feed (IGNORED) */
1766 case 19: /* DECPEX -- Printer extent (IGNORED) */
1767 case 42: /* DECNRCM -- National characters (IGNORED) */
1768 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1770 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1771 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1773 case 1000: /* 1000,1002: enable xterm mouse report */
1774 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1775 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1778 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1779 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1782 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1785 MODBIT(term
.mode
, set
, MODE_8BIT
);
1787 case 1049: /* = 1047 and 1048 */
1790 if (!allowaltscreen
)
1793 alt
= IS_SET(MODE_ALTSCREEN
);
1795 tclearregion(0, 0, term
.col
-1,
1798 if(set
^ alt
) /* set is always 1 or 0 */
1804 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1808 "erresc: unknown private set/reset mode %d\n",
1814 case 0: /* Error (IGNORED) */
1816 case 2: /* KAM -- keyboard action */
1817 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1819 case 4: /* IRM -- Insertion-replacement */
1820 MODBIT(term
.mode
, set
, MODE_INSERT
);
1822 case 12: /* SRM -- Send/Receive */
1823 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1825 case 20: /* LNM -- Linefeed/new line */
1826 MODBIT(term
.mode
, set
, MODE_CRLF
);
1830 "erresc: unknown set/reset mode %d\n",
1842 switch(csiescseq
.mode
) {
1845 fprintf(stderr
, "erresc: unknown csi ");
1849 case '@': /* ICH -- Insert <n> blank char */
1850 DEFAULT(csiescseq
.arg
[0], 1);
1851 tinsertblank(csiescseq
.arg
[0]);
1853 case 'A': /* CUU -- Cursor <n> Up */
1854 DEFAULT(csiescseq
.arg
[0], 1);
1855 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1857 case 'B': /* CUD -- Cursor <n> Down */
1858 case 'e': /* VPR --Cursor <n> Down */
1859 DEFAULT(csiescseq
.arg
[0], 1);
1860 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1862 case 'c': /* DA -- Device Attributes */
1863 if(csiescseq
.arg
[0] == 0)
1864 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1866 case 'C': /* CUF -- Cursor <n> Forward */
1867 case 'a': /* HPR -- Cursor <n> Forward */
1868 DEFAULT(csiescseq
.arg
[0], 1);
1869 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1871 case 'D': /* CUB -- Cursor <n> Backward */
1872 DEFAULT(csiescseq
.arg
[0], 1);
1873 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1875 case 'E': /* CNL -- Cursor <n> Down and first col */
1876 DEFAULT(csiescseq
.arg
[0], 1);
1877 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1879 case 'F': /* CPL -- Cursor <n> Up and first col */
1880 DEFAULT(csiescseq
.arg
[0], 1);
1881 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1883 case 'g': /* TBC -- Tabulation clear */
1884 switch(csiescseq
.arg
[0]) {
1885 case 0: /* clear current tab stop */
1886 term
.tabs
[term
.c
.x
] = 0;
1888 case 3: /* clear all the tabs */
1889 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1895 case 'G': /* CHA -- Move to <col> */
1897 DEFAULT(csiescseq
.arg
[0], 1);
1898 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1900 case 'H': /* CUP -- Move to <row> <col> */
1902 DEFAULT(csiescseq
.arg
[0], 1);
1903 DEFAULT(csiescseq
.arg
[1], 1);
1904 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1906 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1907 DEFAULT(csiescseq
.arg
[0], 1);
1908 while(csiescseq
.arg
[0]--)
1911 case 'J': /* ED -- Clear screen */
1913 switch(csiescseq
.arg
[0]) {
1915 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1916 if(term
.c
.y
< term
.row
-1) {
1917 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1923 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1924 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1927 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1933 case 'K': /* EL -- Clear line */
1934 switch(csiescseq
.arg
[0]) {
1936 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1940 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1943 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1947 case 'S': /* SU -- Scroll <n> line up */
1948 DEFAULT(csiescseq
.arg
[0], 1);
1949 tscrollup(term
.top
, csiescseq
.arg
[0]);
1951 case 'T': /* SD -- Scroll <n> line down */
1952 DEFAULT(csiescseq
.arg
[0], 1);
1953 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1955 case 'L': /* IL -- Insert <n> blank lines */
1956 DEFAULT(csiescseq
.arg
[0], 1);
1957 tinsertblankline(csiescseq
.arg
[0]);
1959 case 'l': /* RM -- Reset Mode */
1960 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1962 case 'M': /* DL -- Delete <n> lines */
1963 DEFAULT(csiescseq
.arg
[0], 1);
1964 tdeleteline(csiescseq
.arg
[0]);
1966 case 'X': /* ECH -- Erase <n> char */
1967 DEFAULT(csiescseq
.arg
[0], 1);
1968 tclearregion(term
.c
.x
, term
.c
.y
,
1969 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1971 case 'P': /* DCH -- Delete <n> char */
1972 DEFAULT(csiescseq
.arg
[0], 1);
1973 tdeletechar(csiescseq
.arg
[0]);
1975 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1976 DEFAULT(csiescseq
.arg
[0], 1);
1977 while(csiescseq
.arg
[0]--)
1980 case 'd': /* VPA -- Move to <row> */
1981 DEFAULT(csiescseq
.arg
[0], 1);
1982 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1984 case 'h': /* SM -- Set terminal mode */
1985 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1987 case 'm': /* SGR -- Terminal attribute (color) */
1988 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1990 case 'r': /* DECSTBM -- Set Scrolling Region */
1991 if(csiescseq
.priv
) {
1994 DEFAULT(csiescseq
.arg
[0], 1);
1995 DEFAULT(csiescseq
.arg
[1], term
.row
);
1996 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2000 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2001 tcursor(CURSOR_SAVE
);
2003 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2004 tcursor(CURSOR_LOAD
);
2015 for(i
= 0; i
< csiescseq
.len
; i
++) {
2016 c
= csiescseq
.buf
[i
] & 0xff;
2019 } else if(c
== '\n') {
2021 } else if(c
== '\r') {
2023 } else if(c
== 0x1b) {
2026 printf("(%02x)", c
);
2034 memset(&csiescseq
, 0, sizeof(csiescseq
));
2043 narg
= strescseq
.narg
;
2045 switch(strescseq
.type
) {
2046 case ']': /* OSC -- Operating System Command */
2047 switch(i
= atoi(strescseq
.args
[0])) {
2052 xsettitle(strescseq
.args
[1]);
2054 case 4: /* color set */
2057 p
= strescseq
.args
[2];
2059 case 104: /* color reset, here p = NULL */
2060 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2061 if (!xsetcolorname(j
, p
)) {
2062 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2065 * TODO if defaultbg color is changed, borders
2072 fprintf(stderr
, "erresc: unknown str ");
2077 case 'k': /* old title set compatibility */
2078 xsettitle(strescseq
.args
[0]);
2080 case 'P': /* DSC -- Device Control String */
2081 case '_': /* APC -- Application Program Command */
2082 case '^': /* PM -- Privacy Message */
2084 fprintf(stderr
, "erresc: unknown str ");
2093 char *p
= strescseq
.buf
;
2096 strescseq
.buf
[strescseq
.len
] = '\0';
2097 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2098 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2106 printf("ESC%c", strescseq
.type
);
2107 for(i
= 0; i
< strescseq
.len
; i
++) {
2108 c
= strescseq
.buf
[i
] & 0xff;
2111 } else if(isprint(c
)) {
2113 } else if(c
== '\n') {
2115 } else if(c
== '\r') {
2117 } else if(c
== 0x1b) {
2120 printf("(%02x)", c
);
2128 memset(&strescseq
, 0, sizeof(strescseq
));
2132 tputtab(bool forward
) {
2138 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2143 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2146 tmoveto(x
, term
.c
.y
);
2150 techo(char *buf
, int len
) {
2151 for(; len
> 0; buf
++, len
--) {
2154 if(c
== '\033') { /* escape */
2157 } else if(c
< '\x20') { /* control code */
2158 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2172 tputc(char *c
, int len
) {
2174 bool control
= ascii
< '\x20' || ascii
== 0177;
2177 if(xwrite(iofd
, c
, len
) < 0) {
2178 fprintf(stderr
, "Error writing in %s:%s\n",
2179 opt_io
, strerror(errno
));
2186 * STR sequences must be checked before anything else
2187 * because it can use some control codes as part of the sequence.
2189 if(term
.esc
& ESC_STR
) {
2192 term
.esc
= ESC_START
| ESC_STR_END
;
2194 case '\a': /* backwards compatibility to xterm */
2199 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2200 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2201 strescseq
.len
+= len
;
2204 * Here is a bug in terminals. If the user never sends
2205 * some code to stop the str or esc command, then st
2206 * will stop responding. But this is better than
2207 * silently failing with unknown characters. At least
2208 * then users will report back.
2210 * In the case users ever get fixed, here is the code:
2222 * Actions of control codes must be performed as soon they arrive
2223 * because they can be embedded inside a control sequence, and
2224 * they must not cause conflicts with sequences.
2232 tmoveto(term
.c
.x
-1, term
.c
.y
);
2235 tmoveto(0, term
.c
.y
);
2240 /* go to first col if the mode is set */
2241 tnewline(IS_SET(MODE_CRLF
));
2243 case '\a': /* BEL */
2244 if(!(xw
.state
& WIN_FOCUSED
))
2247 case '\033': /* ESC */
2249 term
.esc
= ESC_START
;
2251 case '\016': /* SO */
2252 case '\017': /* SI */
2254 * Different charsets are hard to handle. Applications
2255 * should use the right alt charset escapes for the
2256 * only reason they still exist: line drawing. The
2257 * rest is incompatible history st should not support.
2260 case '\032': /* SUB */
2261 case '\030': /* CAN */
2264 case '\005': /* ENQ (IGNORED) */
2265 case '\000': /* NUL (IGNORED) */
2266 case '\021': /* XON (IGNORED) */
2267 case '\023': /* XOFF (IGNORED) */
2268 case 0177: /* DEL (IGNORED) */
2271 } else if(term
.esc
& ESC_START
) {
2272 if(term
.esc
& ESC_CSI
) {
2273 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2274 if(BETWEEN(ascii
, 0x40, 0x7E)
2275 || csiescseq
.len
>= \
2276 sizeof(csiescseq
.buf
)-1) {
2281 } else if(term
.esc
& ESC_STR_END
) {
2285 } else if(term
.esc
& ESC_ALTCHARSET
) {
2287 case '0': /* Line drawing set */
2288 term
.c
.attr
.mode
|= ATTR_GFX
;
2290 case 'B': /* USASCII */
2291 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2293 case 'A': /* UK (IGNORED) */
2294 case '<': /* multinational charset (IGNORED) */
2295 case '5': /* Finnish (IGNORED) */
2296 case 'C': /* Finnish (IGNORED) */
2297 case 'K': /* German (IGNORED) */
2300 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2303 } else if(term
.esc
& ESC_TEST
) {
2304 if(ascii
== '8') { /* DEC screen alignment test. */
2305 char E
[UTF_SIZ
] = "E";
2308 for(x
= 0; x
< term
.col
; ++x
) {
2309 for(y
= 0; y
< term
.row
; ++y
)
2310 tsetchar(E
, &term
.c
.attr
, x
, y
);
2317 term
.esc
|= ESC_CSI
;
2320 term
.esc
|= ESC_TEST
;
2322 case 'P': /* DCS -- Device Control String */
2323 case '_': /* APC -- Application Program Command */
2324 case '^': /* PM -- Privacy Message */
2325 case ']': /* OSC -- Operating System Command */
2326 case 'k': /* old title set compatibility */
2328 strescseq
.type
= ascii
;
2329 term
.esc
|= ESC_STR
;
2331 case '(': /* set primary charset G0 */
2332 term
.esc
|= ESC_ALTCHARSET
;
2334 case ')': /* set secondary charset G1 (IGNORED) */
2335 case '*': /* set tertiary charset G2 (IGNORED) */
2336 case '+': /* set quaternary charset G3 (IGNORED) */
2339 case 'D': /* IND -- Linefeed */
2340 if(term
.c
.y
== term
.bot
) {
2341 tscrollup(term
.top
, 1);
2343 tmoveto(term
.c
.x
, term
.c
.y
+1);
2347 case 'E': /* NEL -- Next line */
2348 tnewline(1); /* always go to first col */
2351 case 'H': /* HTS -- Horizontal tab stop */
2352 term
.tabs
[term
.c
.x
] = 1;
2355 case 'M': /* RI -- Reverse index */
2356 if(term
.c
.y
== term
.top
) {
2357 tscrolldown(term
.top
, 1);
2359 tmoveto(term
.c
.x
, term
.c
.y
-1);
2363 case 'Z': /* DECID -- Identify Terminal */
2364 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2367 case 'c': /* RIS -- Reset to inital state */
2372 case '=': /* DECPAM -- Application keypad */
2373 term
.mode
|= MODE_APPKEYPAD
;
2376 case '>': /* DECPNM -- Normal keypad */
2377 term
.mode
&= ~MODE_APPKEYPAD
;
2380 case '7': /* DECSC -- Save Cursor */
2381 tcursor(CURSOR_SAVE
);
2384 case '8': /* DECRC -- Restore Cursor */
2385 tcursor(CURSOR_LOAD
);
2388 case '\\': /* ST -- Stop */
2392 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2393 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2398 * All characters which form part of a sequence are not
2404 * Display control codes only if we are in graphic mode
2406 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2408 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2410 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2411 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2415 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2416 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2417 &term
.line
[term
.c
.y
][term
.c
.x
],
2418 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2421 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2422 if(term
.c
.x
+1 < term
.col
) {
2423 tmoveto(term
.c
.x
+1, term
.c
.y
);
2425 term
.c
.state
|= CURSOR_WRAPNEXT
;
2430 tresize(int col
, int row
) {
2432 int minrow
= MIN(row
, term
.row
);
2433 int mincol
= MIN(col
, term
.col
);
2434 int slide
= term
.c
.y
- row
+ 1;
2438 if(col
< 1 || row
< 1)
2441 /* free unneeded rows */
2445 * slide screen to keep cursor where we expect it -
2446 * tscrollup would work here, but we can optimize to
2447 * memmove because we're freeing the earlier lines
2449 for(/* i = 0 */; i
< slide
; i
++) {
2453 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2454 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2456 for(i
+= row
; i
< term
.row
; i
++) {
2461 /* resize to new height */
2462 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2463 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2464 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2465 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2467 /* resize each row to new width, zero-pad if needed */
2468 for(i
= 0; i
< minrow
; i
++) {
2470 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2471 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2474 /* allocate any new rows */
2475 for(/* i == minrow */; i
< row
; i
++) {
2477 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2478 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2480 if(col
> term
.col
) {
2481 bp
= term
.tabs
+ term
.col
;
2483 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2484 while(--bp
> term
.tabs
&& !*bp
)
2486 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2489 /* update terminal size */
2492 /* reset scrolling region */
2493 tsetscroll(0, row
-1);
2494 /* make use of the LIMIT in tmoveto */
2495 tmoveto(term
.c
.x
, term
.c
.y
);
2496 /* Clearing both screens */
2499 if(mincol
< col
&& 0 < minrow
) {
2500 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2502 if(0 < col
&& minrow
< row
) {
2503 tclearregion(0, minrow
, col
- 1, row
- 1);
2506 } while(orig
!= term
.line
);
2512 xresize(int col
, int row
) {
2513 xw
.tw
= MAX(1, col
* xw
.cw
);
2514 xw
.th
= MAX(1, row
* xw
.ch
);
2516 XFreePixmap(xw
.dpy
, xw
.buf
);
2517 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2518 DefaultDepth(xw
.dpy
, xw
.scr
));
2519 XftDrawChange(xw
.draw
, xw
.buf
);
2520 xclear(0, 0, xw
.w
, xw
.h
);
2523 static inline ushort
2524 sixd_to_16bit(int x
) {
2525 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2531 XRenderColor color
= { .alpha
= 0xffff };
2533 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2534 for(i
= 0; i
< LEN(colorname
); i
++) {
2537 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2538 die("Could not allocate color '%s'\n", colorname
[i
]);
2542 /* load colors [16-255] ; same colors as xterm */
2543 for(i
= 16, r
= 0; r
< 6; r
++) {
2544 for(g
= 0; g
< 6; g
++) {
2545 for(b
= 0; b
< 6; b
++) {
2546 color
.red
= sixd_to_16bit(r
);
2547 color
.green
= sixd_to_16bit(g
);
2548 color
.blue
= sixd_to_16bit(b
);
2549 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2550 die("Could not allocate color %d\n", i
);
2557 for(r
= 0; r
< 24; r
++, i
++) {
2558 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2559 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2561 die("Could not allocate color %d\n", i
);
2567 xsetcolorname(int x
, const char *name
) {
2568 XRenderColor color
= { .alpha
= 0xffff };
2570 if (x
< 0 || x
> LEN(colorname
))
2573 if(16 <= x
&& x
< 16 + 216) {
2574 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2575 color
.red
= sixd_to_16bit(r
);
2576 color
.green
= sixd_to_16bit(g
);
2577 color
.blue
= sixd_to_16bit(b
);
2578 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2579 return 0; /* something went wrong */
2582 } else if (16 + 216 <= x
&& x
< 256) {
2583 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2584 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2585 return 0; /* something went wrong */
2589 name
= colorname
[x
];
2592 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2599 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2600 XftDrawRect(xw
.draw
,
2601 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2602 borderpx
+ col1
* xw
.cw
,
2603 borderpx
+ row1
* xw
.ch
,
2604 (col2
-col1
+1) * xw
.cw
,
2605 (row2
-row1
+1) * xw
.ch
);
2609 * Absolute coordinates.
2612 xclear(int x1
, int y1
, int x2
, int y2
) {
2613 XftDrawRect(xw
.draw
,
2614 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2615 x1
, y1
, x2
-x1
, y2
-y1
);
2620 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2621 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2622 XSizeHints
*sizeh
= NULL
;
2624 sizeh
= XAllocSizeHints();
2625 if(xw
.isfixed
== False
) {
2626 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2627 sizeh
->height
= xw
.h
;
2628 sizeh
->width
= xw
.w
;
2629 sizeh
->height_inc
= xw
.ch
;
2630 sizeh
->width_inc
= xw
.cw
;
2631 sizeh
->base_height
= 2 * borderpx
;
2632 sizeh
->base_width
= 2 * borderpx
;
2634 sizeh
->flags
= PMaxSize
| PMinSize
;
2635 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2636 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2639 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2644 xloadfont(Font
*f
, FcPattern
*pattern
) {
2648 match
= FcFontMatch(NULL
, pattern
, &result
);
2652 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2653 FcPatternDestroy(match
);
2658 f
->pattern
= FcPatternDuplicate(pattern
);
2660 f
->ascent
= f
->match
->ascent
;
2661 f
->descent
= f
->match
->descent
;
2663 f
->rbearing
= f
->match
->max_advance_width
;
2665 f
->height
= f
->ascent
+ f
->descent
;
2666 f
->width
= f
->lbearing
+ f
->rbearing
;
2672 xloadfonts(char *fontstr
, int fontsize
) {
2677 if(fontstr
[0] == '-') {
2678 pattern
= XftXlfdParse(fontstr
, False
, False
);
2680 pattern
= FcNameParse((FcChar8
*)fontstr
);
2684 die("st: can't open font %s\n", fontstr
);
2687 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2688 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2689 usedfontsize
= fontsize
;
2691 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2692 if(result
== FcResultMatch
) {
2693 usedfontsize
= (int)fontval
;
2696 * Default font size is 12, if none given. This is to
2697 * have a known usedfontsize value.
2699 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2704 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2705 FcDefaultSubstitute(pattern
);
2707 if(xloadfont(&dc
.font
, pattern
))
2708 die("st: can't open font %s\n", fontstr
);
2710 /* Setting character width and height. */
2711 xw
.cw
= dc
.font
.width
;
2712 xw
.ch
= dc
.font
.height
;
2714 FcPatternDel(pattern
, FC_SLANT
);
2715 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2716 if(xloadfont(&dc
.ifont
, pattern
))
2717 die("st: can't open font %s\n", fontstr
);
2719 FcPatternDel(pattern
, FC_WEIGHT
);
2720 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2721 if(xloadfont(&dc
.ibfont
, pattern
))
2722 die("st: can't open font %s\n", fontstr
);
2724 FcPatternDel(pattern
, FC_SLANT
);
2725 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2726 if(xloadfont(&dc
.bfont
, pattern
))
2727 die("st: can't open font %s\n", fontstr
);
2729 FcPatternDestroy(pattern
);
2733 xloadfontset(Font
*f
) {
2736 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2742 xunloadfont(Font
*f
) {
2743 XftFontClose(xw
.dpy
, f
->match
);
2744 FcPatternDestroy(f
->pattern
);
2746 FcFontSetDestroy(f
->set
);
2750 xunloadfonts(void) {
2754 * Free the loaded fonts in the font cache. This is done backwards
2757 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2760 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2765 xunloadfont(&dc
.font
);
2766 xunloadfont(&dc
.bfont
);
2767 xunloadfont(&dc
.ifont
);
2768 xunloadfont(&dc
.ibfont
);
2772 xzoom(const Arg
*arg
) {
2774 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2781 XSetWindowAttributes attrs
;
2787 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2788 die("Can't open display\n");
2789 xw
.scr
= XDefaultScreen(xw
.dpy
);
2790 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2794 die("Could not init fontconfig.\n");
2796 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2797 xloadfonts(usedfont
, 0);
2800 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2803 /* adjust fixed window geometry */
2805 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2806 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2808 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2810 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2815 /* window - default size */
2816 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2817 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2823 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2824 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2825 attrs
.bit_gravity
= NorthWestGravity
;
2826 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2827 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2828 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2829 attrs
.colormap
= xw
.cmap
;
2831 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2832 XRootWindow(xw
.dpy
, xw
.scr
);
2833 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2834 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2836 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2840 memset(&gcvalues
, 0, sizeof(gcvalues
));
2841 gcvalues
.graphics_exposures
= False
;
2842 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2844 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2845 DefaultDepth(xw
.dpy
, xw
.scr
));
2846 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2847 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2849 /* Xft rendering context */
2850 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2853 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2854 XSetLocaleModifiers("@im=local");
2855 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2856 XSetLocaleModifiers("@im=");
2857 if((xw
.xim
= XOpenIM(xw
.dpy
,
2858 NULL
, NULL
, NULL
)) == NULL
) {
2859 die("XOpenIM failed. Could not open input"
2864 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2865 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2866 XNFocusWindow
, xw
.win
, NULL
);
2868 die("XCreateIC failed. Could not obtain input method.\n");
2870 /* white cursor, black outline */
2871 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2872 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2873 XRecolorCursor(xw
.dpy
, cursor
,
2874 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2875 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2877 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2878 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2879 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2882 XMapWindow(xw
.dpy
, xw
.win
);
2888 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2889 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2890 width
= charlen
* xw
.cw
, xp
, i
;
2892 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2895 Font
*font
= &dc
.font
;
2897 FcPattern
*fcpattern
, *fontpattern
;
2898 FcFontSet
*fcsets
[] = { NULL
};
2899 FcCharSet
*fccharset
;
2900 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2901 XRenderColor colfg
, colbg
;
2904 frcflags
= FRC_NORMAL
;
2906 if(base
.mode
& ATTR_ITALIC
) {
2907 if(base
.fg
== defaultfg
)
2908 base
.fg
= defaultitalic
;
2910 frcflags
= FRC_ITALIC
;
2911 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2912 if(base
.fg
== defaultfg
)
2913 base
.fg
= defaultitalic
;
2915 frcflags
= FRC_ITALICBOLD
;
2916 } else if(base
.mode
& ATTR_UNDERLINE
) {
2917 if(base
.fg
== defaultfg
)
2918 base
.fg
= defaultunderline
;
2920 fg
= &dc
.col
[base
.fg
];
2921 bg
= &dc
.col
[base
.bg
];
2923 if(base
.mode
& ATTR_BOLD
) {
2924 if(BETWEEN(base
.fg
, 0, 7)) {
2925 /* basic system colors */
2926 fg
= &dc
.col
[base
.fg
+ 8];
2927 } else if(BETWEEN(base
.fg
, 16, 195)) {
2929 fg
= &dc
.col
[base
.fg
+ 36];
2930 } else if(BETWEEN(base
.fg
, 232, 251)) {
2932 fg
= &dc
.col
[base
.fg
+ 4];
2935 * Those ranges will not be brightened:
2936 * 8 - 15 – bright system colors
2937 * 196 - 231 – highest 256 color cube
2938 * 252 - 255 – brightest colors in greyscale
2941 frcflags
= FRC_BOLD
;
2944 if(IS_SET(MODE_REVERSE
)) {
2945 if(fg
== &dc
.col
[defaultfg
]) {
2946 fg
= &dc
.col
[defaultbg
];
2948 colfg
.red
= ~fg
->color
.red
;
2949 colfg
.green
= ~fg
->color
.green
;
2950 colfg
.blue
= ~fg
->color
.blue
;
2951 colfg
.alpha
= fg
->color
.alpha
;
2952 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2956 if(bg
== &dc
.col
[defaultbg
]) {
2957 bg
= &dc
.col
[defaultfg
];
2959 colbg
.red
= ~bg
->color
.red
;
2960 colbg
.green
= ~bg
->color
.green
;
2961 colbg
.blue
= ~bg
->color
.blue
;
2962 colbg
.alpha
= bg
->color
.alpha
;
2963 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2968 if(base
.mode
& ATTR_REVERSE
) {
2974 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
2977 /* Intelligent cleaning up of the borders. */
2979 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2980 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2982 if(x
+ charlen
>= term
.col
) {
2983 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2984 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2987 xclear(winx
, 0, winx
+ width
, borderpx
);
2989 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2991 /* Clean up the region we want to draw to. */
2992 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2994 /* Set the clip region because Xft is sometimes dirty. */
2999 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3001 for(xp
= winx
; bytelen
> 0;) {
3003 * Search for the range in the to be printed string of glyphs
3004 * that are in the main font. Then print that range. If
3005 * some glyph is found that is not in the font, do the
3013 u8cblen
= utf8decode(s
, &u8char
);
3017 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
3018 if(!doesexist
|| bytelen
<= 0) {
3027 XftDrawStringUtf8(xw
.draw
, fg
,
3029 winy
+ font
->ascent
,
3032 xp
+= font
->width
* u8fl
;
3045 /* Search the font cache. */
3046 for(i
= 0; i
< frclen
; i
++, frp
--) {
3050 if(frc
[frp
].c
== u8char
3051 && frc
[frp
].flags
== frcflags
) {
3056 /* Nothing was found. */
3060 fcsets
[0] = font
->set
;
3063 * Nothing was found in the cache. Now use
3064 * some dozen of Fontconfig calls to get the
3065 * font for one single character.
3067 fcpattern
= FcPatternDuplicate(font
->pattern
);
3068 fccharset
= FcCharSetCreate();
3070 FcCharSetAddChar(fccharset
, u8char
);
3071 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3073 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3076 FcConfigSubstitute(0, fcpattern
,
3078 FcDefaultSubstitute(fcpattern
);
3080 fontpattern
= FcFontSetMatch(0, fcsets
,
3081 FcTrue
, fcpattern
, &fcres
);
3084 * Overwrite or create the new cache entry.
3088 if(frccur
>= LEN(frc
))
3090 if(frclen
> LEN(frc
)) {
3092 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
3095 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
3097 frc
[frccur
].c
= u8char
;
3098 frc
[frccur
].flags
= frcflags
;
3100 FcPatternDestroy(fcpattern
);
3101 FcCharSetDestroy(fccharset
);
3106 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
3107 xp
, winy
+ frc
[frp
].font
->ascent
,
3108 (FcChar8
*)u8c
, u8cblen
);
3114 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3115 winy + font->ascent, (FcChar8 *)s, bytelen);
3118 if(base
.mode
& ATTR_UNDERLINE
) {
3119 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3123 /* Reset clip to none. */
3124 XftDrawSetClip(xw
.draw
, 0);
3129 static int oldx
= 0, oldy
= 0;
3131 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3133 LIMIT(oldx
, 0, term
.col
-1);
3134 LIMIT(oldy
, 0, term
.row
-1);
3136 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3138 /* remove the old cursor */
3139 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3140 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3143 /* draw the new one */
3144 if(!(IS_SET(MODE_HIDE
))) {
3145 if(xw
.state
& WIN_FOCUSED
) {
3146 if(IS_SET(MODE_REVERSE
)) {
3147 g
.mode
|= ATTR_REVERSE
;
3153 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
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
* xw
.cw
,
3161 borderpx
+ term
.c
.y
* xw
.ch
,
3163 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3164 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3165 borderpx
+ term
.c
.y
* xw
.ch
,
3167 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3168 borderpx
+ term
.c
.x
* xw
.cw
,
3169 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3172 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3178 xsettitle(char *p
) {
3181 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3183 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3188 xsettitle(opt_title
? opt_title
: "st");
3192 redraw(int timeout
) {
3193 struct timespec tv
= {0, timeout
* 1000};
3199 nanosleep(&tv
, NULL
);
3200 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3206 drawregion(0, 0, term
.col
, term
.row
);
3207 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3209 XSetForeground(xw
.dpy
, dc
.gc
,
3210 dc
.col
[IS_SET(MODE_REVERSE
)?
3211 defaultfg
: defaultbg
].pixel
);
3215 drawregion(int x1
, int y1
, int x2
, int y2
) {
3216 int ic
, ib
, x
, y
, ox
, sl
;
3218 char buf
[DRAW_BUF_SIZ
];
3219 bool ena_sel
= sel
.ob
.x
!= -1;
3221 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3224 if(!(xw
.state
& WIN_VISIBLE
))
3227 for(y
= y1
; y
< y2
; y
++) {
3231 xtermclear(0, y
, term
.col
, y
);
3233 base
= term
.line
[y
][0];
3235 for(x
= x1
; x
< x2
; x
++) {
3236 new = term
.line
[y
][x
];
3237 if(ena_sel
&& selected(x
, y
))
3238 new.mode
^= ATTR_REVERSE
;
3239 if(ib
> 0 && (ATTRCMP(base
, new)
3240 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3241 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3249 sl
= utf8size(new.c
);
3250 memcpy(buf
+ib
, new.c
, sl
);
3255 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3261 expose(XEvent
*ev
) {
3262 XExposeEvent
*e
= &ev
->xexpose
;
3264 if(xw
.state
& WIN_REDRAW
) {
3266 xw
.state
&= ~WIN_REDRAW
;
3272 visibility(XEvent
*ev
) {
3273 XVisibilityEvent
*e
= &ev
->xvisibility
;
3275 if(e
->state
== VisibilityFullyObscured
) {
3276 xw
.state
&= ~WIN_VISIBLE
;
3277 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3278 /* need a full redraw for next Expose, not just a buf copy */
3279 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3285 xw
.state
&= ~WIN_VISIBLE
;
3289 xseturgency(int add
) {
3290 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3292 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3293 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3299 XFocusChangeEvent
*e
= &ev
->xfocus
;
3301 if(e
->mode
== NotifyGrab
)
3304 if(ev
->type
== FocusIn
) {
3305 XSetICFocus(xw
.xic
);
3306 xw
.state
|= WIN_FOCUSED
;
3309 XUnsetICFocus(xw
.xic
);
3310 xw
.state
&= ~WIN_FOCUSED
;
3315 match(uint mask
, uint state
) {
3316 state
&= ~(ignoremod
);
3318 if(mask
== XK_NO_MOD
&& state
)
3320 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3322 if((state
& mask
) != state
)
3328 numlock(const Arg
*dummy
) {
3333 kmap(KeySym k
, uint state
) {
3338 /* Check for mapped keys out of X11 function keys. */
3339 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3340 if(mappedkeys
[i
] == k
)
3343 if(i
== LEN(mappedkeys
)) {
3344 if((k
& 0xFFFF) < 0xFD00)
3348 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3354 if(!match(mask
, state
))
3357 if(kp
->appkey
> 0) {
3358 if(!IS_SET(MODE_APPKEYPAD
))
3360 if(term
.numlock
&& kp
->appkey
== 2)
3362 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3366 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3368 && !IS_SET(MODE_APPCURSOR
))) {
3372 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3373 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3384 kpress(XEvent
*ev
) {
3385 XKeyEvent
*e
= &ev
->xkey
;
3387 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3393 if(IS_SET(MODE_KBDLOCK
))
3396 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3397 e
->state
&= ~Mod2Mask
;
3399 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3400 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3401 bp
->func(&(bp
->arg
));
3406 /* 2. custom keys from config.h */
3407 if((customkey
= kmap(ksym
, e
->state
))) {
3408 len
= strlen(customkey
);
3409 memcpy(buf
, customkey
, len
);
3410 /* 3. hardcoded (overrides X lookup) */
3415 if(len
== 1 && e
->state
& Mod1Mask
) {
3416 if(IS_SET(MODE_8BIT
)) {
3419 ret
= utf8encode(&c
, cp
);
3428 memcpy(cp
, xstr
, len
);
3429 len
= cp
- buf
+ len
;
3433 if(IS_SET(MODE_ECHO
))
3439 cmessage(XEvent
*e
) {
3442 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3444 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3445 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3446 xw
.state
|= WIN_FOCUSED
;
3448 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3449 xw
.state
&= ~WIN_FOCUSED
;
3451 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3452 /* Send SIGHUP to shell */
3459 cresize(int width
, int height
) {
3467 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3468 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3477 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3480 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3487 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3488 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3490 gettimeofday(&lastblink
, NULL
);
3491 gettimeofday(&last
, NULL
);
3493 for(xev
= actionfps
;;) {
3495 FD_SET(cmdfd
, &rfd
);
3498 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3501 die("select failed: %s\n", SERRNO
);
3503 if(FD_ISSET(cmdfd
, &rfd
)) {
3506 blinkset
= tattrset(ATTR_BLINK
);
3507 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3508 term
.mode
&= ~(MODE_BLINK
);
3512 if(FD_ISSET(xfd
, &rfd
))
3515 gettimeofday(&now
, NULL
);
3516 drawtimeout
.tv_sec
= 0;
3517 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3521 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3522 tsetdirtattr(ATTR_BLINK
);
3523 term
.mode
^= MODE_BLINK
;
3524 gettimeofday(&lastblink
, NULL
);
3527 if(TIMEDIFF(now
, last
) \
3528 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3534 while(XPending(xw
.dpy
)) {
3535 XNextEvent(xw
.dpy
, &ev
);
3536 if(XFilterEvent(&ev
, None
))
3538 if(handler
[ev
.type
])
3539 (handler
[ev
.type
])(&ev
);
3545 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3547 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3549 if(TIMEDIFF(now
, lastblink
) \
3551 drawtimeout
.tv_usec
= 1;
3553 drawtimeout
.tv_usec
= (1000 * \
3568 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3569 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3570 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3574 main(int argc
, char *argv
[]) {
3578 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3583 allowaltscreen
= false;
3586 opt_class
= EARGF(usage());
3589 /* eat all remaining arguments */
3594 opt_font
= EARGF(usage());
3597 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3602 if(bitm
& WidthValue
)
3604 if(bitm
& HeightValue
)
3606 if(bitm
& XNegative
&& xw
.fx
== 0)
3608 if(bitm
& XNegative
&& xw
.fy
== 0)
3611 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3615 opt_io
= EARGF(usage());
3618 opt_title
= EARGF(usage());
3621 opt_embed
= EARGF(usage());
3629 setlocale(LC_CTYPE
, "");
3630 XSetLocaleModifiers("");
3636 cresize(xw
.h
, xw
.w
);