1 /* See LICENSE for licence details. */
15 #include <sys/ioctl.h>
16 #include <sys/select.h>
19 #include <sys/types.h>
24 #include <X11/Xatom.h>
26 #include <X11/Xutil.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <X11/Xft/Xft.h>
30 #include <fontconfig/fontconfig.h>
39 #define Draw XftDraw *
40 #define Colour XftColor
41 #define Colourmap Colormap
42 #define Rectangle XRectangle
46 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
48 #elif defined(__FreeBSD__) || defined(__DragonFly__)
54 #define XEMBED_FOCUS_IN 4
55 #define XEMBED_FOCUS_OUT 5
59 #define ESC_BUF_SIZ (128*UTF_SIZ)
60 #define ESC_ARG_SIZ 16
61 #define STR_BUF_SIZ ESC_BUF_SIZ
62 #define STR_ARG_SIZ ESC_ARG_SIZ
63 #define DRAW_BUF_SIZ 20*1024
64 #define XK_ANY_MOD UINT_MAX
66 #define XK_SWITCH_MOD (1<<13)
68 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
71 #define SERRNO strerror(errno)
72 #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 #define MAX(a, b) ((a) < (b) ? (b) : (a))
74 #define LEN(a) (sizeof(a) / sizeof(a[0]))
75 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
76 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
77 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
78 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
79 #define IS_SET(flag) ((term.mode & (flag)) != 0)
80 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
81 #define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
83 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
84 #define IS_TRUECOL(x) (1 << 24 & (x))
85 #define TRUERED(x) (((x) & 0xff0000) >> 8)
86 #define TRUEGREEN(x) (((x) & 0xff00))
87 #define TRUEBLUE(x) (((x) & 0xff) << 8)
90 #define VT102ID "\033[?6c"
92 enum glyph_attribute
{
105 enum cursor_movement
{
123 MODE_MOUSEMOTION
= 64,
128 MODE_APPCURSOR
= 2048,
129 MODE_MOUSESGR
= 4096,
134 MODE_MOUSEX10
= 131072,
135 MODE_MOUSEMANY
= 262144,
136 MODE_BRCKTPASTE
= 524288,
137 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
154 ESC_STR
= 4, /* DSC, OSC, PM, APC */
156 ESC_STR_END
= 16, /* a final string was encountered */
157 ESC_TEST
= 32, /* Enter in test mode */
166 enum selection_type
{
171 enum selection_snap
{
176 typedef unsigned char uchar
;
177 typedef unsigned int uint
;
178 typedef unsigned long ulong
;
179 typedef unsigned short ushort
;
182 char c
[UTF_SIZ
]; /* character code */
183 ushort mode
; /* attribute flags */
184 uint32_t fg
; /* foreground */
185 uint32_t bg
; /* background */
191 Glyph attr
; /* current char attributes */
197 /* CSI Escape sequence structs */
198 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
200 char buf
[ESC_BUF_SIZ
]; /* raw string */
201 int len
; /* raw string length */
203 int arg
[ESC_ARG_SIZ
];
204 int narg
; /* nb of args */
208 /* STR Escape sequence structs */
209 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
211 char type
; /* ESC type ... */
212 char buf
[STR_BUF_SIZ
]; /* raw string */
213 int len
; /* raw string length */
214 char *args
[STR_ARG_SIZ
];
215 int narg
; /* nb of args */
218 /* Internal representation of the screen */
220 int row
; /* nb row */
221 int col
; /* nb col */
222 Line
*line
; /* screen */
223 Line
*alt
; /* alternate screen */
224 bool *dirty
; /* dirtyness of lines */
225 TCursor c
; /* cursor */
226 int top
; /* top scroll limit */
227 int bot
; /* bottom scroll limit */
228 int mode
; /* terminal mode flags */
229 int esc
; /* escape state flags */
230 char trantbl
[4]; /* charset table translation */
231 int charset
; /* current charset */
232 int icharset
; /* selected charset for sequence */
233 bool numlock
; /* lock numbers in keyboard */
237 /* Purely graphic info */
243 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
248 XSetWindowAttributes attrs
;
250 bool isfixed
; /* is fixed geometry? */
251 int fx
, fy
, fw
, fh
; /* fixed geometry */
252 int tw
, th
; /* tty width and height */
253 int w
, h
; /* window width and height */
254 int ch
; /* char height */
255 int cw
; /* char width */
256 char state
; /* focus, redraw, visible */
269 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
270 signed char appkey
; /* application keypad */
271 signed char appcursor
; /* application cursor */
272 signed char crlf
; /* crlf mode */
280 * Selection variables:
281 * nb – normalized coordinates of the beginning of the selection
282 * ne – normalized coordinates of the end of the selection
283 * ob – original coordinates of the beginning of the selection
284 * oe – original coordinates of the end of the selection
293 struct timeval tclick1
;
294 struct timeval tclick2
;
307 void (*func
)(const Arg
*);
311 /* function definitions used in config.h */
312 static void clippaste(const Arg
*);
313 static void numlock(const Arg
*);
314 static void selpaste(const Arg
*);
315 static void xzoom(const Arg
*);
317 /* Config.h for applying patches and the configuration. */
333 /* Drawing Context */
335 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
336 Font font
, bfont
, ifont
, ibfont
;
340 static void die(const char *, ...);
341 static void draw(void);
342 static void redraw(int);
343 static void drawregion(int, int, int, int);
344 static void execsh(void);
345 static void sigchld(int);
346 static void run(void);
348 static void csidump(void);
349 static void csihandle(void);
350 static void csiparse(void);
351 static void csireset(void);
352 static void strdump(void);
353 static void strhandle(void);
354 static void strparse(void);
355 static void strreset(void);
357 static int tattrset(int);
358 static void tclearregion(int, int, int, int);
359 static void tcursor(int);
360 static void tdeletechar(int);
361 static void tdeleteline(int);
362 static void tinsertblank(int);
363 static void tinsertblankline(int);
364 static void tmoveto(int, int);
365 static void tmoveato(int x
, int y
);
366 static void tnew(int, int);
367 static void tnewline(int);
368 static void tputtab(bool);
369 static void tputc(char *, int);
370 static void treset(void);
371 static int tresize(int, int);
372 static void tscrollup(int, int);
373 static void tscrolldown(int, int);
374 static void tsetattr(int*, int);
375 static void tsetchar(char *, Glyph
*, int, int);
376 static void tsetscroll(int, int);
377 static void tswapscreen(void);
378 static void tsetdirt(int, int);
379 static void tsetdirtattr(int);
380 static void tsetmode(bool, bool, int *, int);
381 static void tfulldirt(void);
382 static void techo(char *, int);
383 static int32_t tdefcolor(int *, int *, int);
384 static void tselcs(void);
385 static void tdeftran(char);
386 static inline bool match(uint
, uint
);
387 static void ttynew(void);
388 static void ttyread(void);
389 static void ttyresize(void);
390 static void ttysend(char *, size_t);
391 static void ttywrite(const char *, size_t);
393 static void xdraws(char *, Glyph
, int, int, int, int);
394 static void xhints(void);
395 static void xclear(int, int, int, int);
396 static void xdrawcursor(void);
397 static void xinit(void);
398 static void xloadcols(void);
399 static int xsetcolorname(int, const char *);
400 static int xloadfont(Font
*, FcPattern
*);
401 static void xloadfonts(char *, double);
402 static int xloadfontset(Font
*);
403 static void xsettitle(char *);
404 static void xresettitle(void);
405 static void xsetpointermotion(int);
406 static void xseturgency(int);
407 static void xsetsel(char*);
408 static void xtermclear(int, int, int, int);
409 static void xunloadfont(Font
*f
);
410 static void xunloadfonts(void);
411 static void xresize(int, int);
413 static void expose(XEvent
*);
414 static void visibility(XEvent
*);
415 static void unmap(XEvent
*);
416 static char *kmap(KeySym
, uint
);
417 static void kpress(XEvent
*);
418 static void cmessage(XEvent
*);
419 static void cresize(int, int);
420 static void resize(XEvent
*);
421 static void focus(XEvent
*);
422 static void brelease(XEvent
*);
423 static void bpress(XEvent
*);
424 static void bmotion(XEvent
*);
425 static void selnotify(XEvent
*);
426 static void selclear(XEvent
*);
427 static void selrequest(XEvent
*);
429 static void selinit(void);
430 static void selsort(void);
431 static inline bool selected(int, int);
432 static void selcopy(void);
433 static void selscroll(int, int);
434 static void selsnap(int, int *, int *, int);
436 static int utf8decode(char *, long *);
437 static int utf8encode(long *, char *);
438 static int utf8size(char *);
439 static int isfullutf8(char *, int);
441 static ssize_t
xwrite(int, char *, size_t);
442 static void *xmalloc(size_t);
443 static void *xrealloc(void *, size_t);
445 static void (*handler
[LASTEvent
])(XEvent
*) = {
447 [ClientMessage
] = cmessage
,
448 [ConfigureNotify
] = resize
,
449 [VisibilityNotify
] = visibility
,
450 [UnmapNotify
] = unmap
,
454 [MotionNotify
] = bmotion
,
455 [ButtonPress
] = bpress
,
456 [ButtonRelease
] = brelease
,
457 [SelectionClear
] = selclear
,
458 [SelectionNotify
] = selnotify
,
459 [SelectionRequest
] = selrequest
,
466 static CSIEscape csiescseq
;
467 static STREscape strescseq
;
470 static Selection sel
;
471 static int iofd
= -1;
472 static char **opt_cmd
= NULL
;
473 static char *opt_io
= NULL
;
474 static char *opt_title
= NULL
;
475 static char *opt_embed
= NULL
;
476 static char *opt_class
= NULL
;
477 static char *opt_font
= NULL
;
478 static int oldbutton
= 3; /* button event on startup: 3 = release */
480 static char *usedfont
= NULL
;
481 static double usedfontsize
= 0;
483 /* Font Ring Cache */
496 /* Fontcache is an array now. A new font will be appended to the array. */
497 static Fontcache frc
[16];
498 static int frclen
= 0;
501 xwrite(int fd
, char *s
, size_t len
) {
505 ssize_t r
= write(fd
, s
, len
);
515 xmalloc(size_t len
) {
516 void *p
= malloc(len
);
519 die("Out of memory\n");
525 xrealloc(void *p
, size_t len
) {
526 if((p
= realloc(p
, len
)) == NULL
)
527 die("Out of memory\n");
533 utf8decode(char *s
, long *u
) {
539 if(~c
& 0x80) { /* 0xxxxxxx */
542 } else if((c
& 0xE0) == 0xC0) { /* 110xxxxx */
545 } else if((c
& 0xF0) == 0xE0) { /* 1110xxxx */
548 } else if((c
& 0xF8) == 0xF0) { /* 11110xxx */
555 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
557 if((c
& 0xC0) != 0x80) /* 10xxxxxx */
563 if((n
== 1 && *u
< 0x80) ||
564 (n
== 2 && *u
< 0x800) ||
565 (n
== 3 && *u
< 0x10000) ||
566 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
578 utf8encode(long *u
, char *s
) {
586 *sp
= uc
; /* 0xxxxxxx */
588 } else if(*u
< 0x800) {
589 *sp
= (uc
>> 6) | 0xC0; /* 110xxxxx */
591 } else if(uc
< 0x10000) {
592 *sp
= (uc
>> 12) | 0xE0; /* 1110xxxx */
594 } else if(uc
<= 0x10FFFF) {
595 *sp
= (uc
>> 18) | 0xF0; /* 11110xxx */
601 for(i
=n
,++sp
; i
>0; --i
,++sp
)
602 *sp
= ((uc
>> 6*(i
-1)) & 0x3F) | 0x80; /* 10xxxxxx */
614 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
615 UTF-8 otherwise return 0 */
617 isfullutf8(char *s
, int b
) {
625 } else if((*c1
& 0xE0) == 0xC0 && b
== 1) {
627 } else if((*c1
& 0xF0) == 0xE0 &&
629 ((b
== 2) && (*c2
& 0xC0) == 0x80))) {
631 } else if((*c1
& 0xF8) == 0xF0 &&
633 ((b
== 2) && (*c2
& 0xC0) == 0x80) ||
634 ((b
== 3) && (*c2
& 0xC0) == 0x80 && (*c3
& 0xC0) == 0x80))) {
647 } else if((c
& 0xE0) == 0xC0) {
649 } else if((c
& 0xF0) == 0xE0) {
658 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
659 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
663 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
664 if(sel
.xtarget
== None
)
665 sel
.xtarget
= XA_STRING
;
673 return LIMIT(x
, 0, term
.col
-1);
681 return LIMIT(y
, 0, term
.row
-1);
686 if(sel
.ob
.y
== sel
.oe
.y
) {
687 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
688 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
690 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
691 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
693 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
694 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
698 selected(int x
, int y
) {
699 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
700 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
702 if(sel
.type
== SEL_RECTANGULAR
) {
703 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
704 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
707 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
708 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
709 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
710 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
714 selsnap(int mode
, int *x
, int *y
, int direction
) {
720 * Snap around if the word wraps around at the end or
721 * beginning of a line.
724 if(direction
< 0 && *x
<= 0) {
725 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
733 if(direction
> 0 && *x
>= term
.col
-1) {
734 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
743 if(term
.line
[*y
][*x
+direction
].mode
& ATTR_WDUMMY
) {
748 if(strchr(worddelimiters
,
749 term
.line
[*y
][*x
+direction
].c
[0])) {
758 * Snap around if the the previous line or the current one
759 * has set ATTR_WRAP at its end. Then the whole next or
760 * previous line will be selected.
762 *x
= (direction
< 0) ? 0 : term
.col
- 1;
763 if(direction
< 0 && *y
> 0) {
764 for(; *y
> 0; *y
+= direction
) {
765 if(!(term
.line
[*y
-1][term
.col
-1].mode
770 } else if(direction
> 0 && *y
< term
.row
-1) {
771 for(; *y
< term
.row
; *y
+= direction
) {
772 if(!(term
.line
[*y
][term
.col
-1].mode
781 * Select the whole line when the end of line is reached.
785 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
795 getbuttoninfo(XEvent
*e
) {
797 uint state
= e
->xbutton
.state
&~Button1Mask
;
799 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
801 sel
.oe
.x
= x2col(e
->xbutton
.x
);
802 sel
.oe
.y
= y2row(e
->xbutton
.y
);
804 if(sel
.ob
.y
< sel
.oe
.y
805 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
806 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
807 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
809 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
810 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
814 sel
.type
= SEL_REGULAR
;
815 for(type
= 1; type
< LEN(selmasks
); ++type
) {
816 if(match(selmasks
[type
], state
)) {
824 mousereport(XEvent
*e
) {
825 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
826 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
832 if(e
->xbutton
.type
== MotionNotify
) {
833 if(x
== ox
&& y
== oy
)
835 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
837 /* MOUSE_MOTION: no reporting if no button is pressed */
838 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
841 button
= oldbutton
+ 32;
845 if(!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
852 if(e
->xbutton
.type
== ButtonPress
) {
856 } else if(e
->xbutton
.type
== ButtonRelease
) {
858 /* MODE_MOUSEX10: no button release reporting */
859 if(IS_SET(MODE_MOUSEX10
))
864 if(!IS_SET(MODE_MOUSEX10
)) {
865 button
+= (state
& ShiftMask
? 4 : 0)
866 + (state
& Mod4Mask
? 8 : 0)
867 + (state
& ControlMask
? 16 : 0);
871 if(IS_SET(MODE_MOUSESGR
)) {
872 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
874 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
875 } else if(x
< 223 && y
< 223) {
876 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
877 32+button
, 32+x
+1, 32+y
+1);
890 if(IS_SET(MODE_MOUSE
)) {
895 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
896 if(e
->xbutton
.button
== mk
->b
897 && match(mk
->mask
, e
->xbutton
.state
)) {
898 ttysend(mk
->s
, strlen(mk
->s
));
903 if(e
->xbutton
.button
== Button1
) {
904 gettimeofday(&now
, NULL
);
906 /* Clear previous selection, logically and visually. */
909 sel
.type
= SEL_REGULAR
;
910 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
911 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
914 * If the user clicks below predefined timeouts specific
915 * snapping behaviour is exposed.
917 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
918 sel
.snap
= SNAP_LINE
;
919 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
920 sel
.snap
= SNAP_WORD
;
924 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
925 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
929 * Draw selection, unless it's regular and we don't want to
930 * make clicks visible
934 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
936 sel
.tclick2
= sel
.tclick1
;
944 int x
, y
, bufsize
, size
, i
, ex
;
950 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
951 ptr
= str
= xmalloc(bufsize
);
953 /* append every set & selected glyph to the selection */
954 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
955 gp
= &term
.line
[y
][0];
956 last
= &gp
[term
.col
-1];
958 while(last
>= gp
&& !(selected(last
- gp
, y
) &&
959 strcmp(last
->c
, " ") != 0)) {
963 for(x
= 0; gp
<= last
; x
++, ++gp
) {
964 if(!selected(x
, y
) || (gp
->mode
& ATTR_WDUMMY
))
967 size
= utf8size(gp
->c
);
968 memcpy(ptr
, gp
->c
, size
);
973 * Copy and pasting of line endings is inconsistent
974 * in the inconsistent terminal and GUI world.
975 * The best solution seems like to produce '\n' when
976 * something is copied from st and convert '\n' to
977 * '\r', when something to be pasted is received by
979 * FIXME: Fix the computer world.
981 if(y
< sel
.ne
.y
&& x
> 0 && !((gp
-1)->mode
& ATTR_WRAP
))
985 * If the last selected line expands in the selection
986 * after the visible text '\n' is appended.
990 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
993 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
1005 selnotify(XEvent
*e
) {
1006 ulong nitems
, ofs
, rem
;
1008 uchar
*data
, *last
, *repl
;
1013 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
1014 False
, AnyPropertyType
, &type
, &format
,
1015 &nitems
, &rem
, &data
)) {
1016 fprintf(stderr
, "Clipboard allocation failed\n");
1021 * As seen in selcopy:
1022 * Line endings are inconsistent in the terminal and GUI world
1023 * copy and pasting. When receiving some selection data,
1024 * replace all '\n' with '\r'.
1025 * FIXME: Fix the computer world.
1028 last
= data
+ nitems
* format
/ 8;
1029 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1033 if(IS_SET(MODE_BRCKTPASTE
))
1034 ttywrite("\033[200~", 6);
1035 ttysend((char *)data
, nitems
* format
/ 8);
1036 if(IS_SET(MODE_BRCKTPASTE
))
1037 ttywrite("\033[201~", 6);
1039 /* number of 32-bit chunks returned */
1040 ofs
+= nitems
* format
/ 32;
1045 selpaste(const Arg
*dummy
) {
1046 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1047 xw
.win
, CurrentTime
);
1051 clippaste(const Arg
*dummy
) {
1054 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1055 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1056 xw
.win
, CurrentTime
);
1060 selclear(XEvent
*e
) {
1064 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1068 selrequest(XEvent
*e
) {
1069 XSelectionRequestEvent
*xsre
;
1070 XSelectionEvent xev
;
1071 Atom xa_targets
, string
;
1073 xsre
= (XSelectionRequestEvent
*) e
;
1074 xev
.type
= SelectionNotify
;
1075 xev
.requestor
= xsre
->requestor
;
1076 xev
.selection
= xsre
->selection
;
1077 xev
.target
= xsre
->target
;
1078 xev
.time
= xsre
->time
;
1080 xev
.property
= None
;
1082 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1083 if(xsre
->target
== xa_targets
) {
1084 /* respond with the supported type */
1085 string
= sel
.xtarget
;
1086 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1087 XA_ATOM
, 32, PropModeReplace
,
1088 (uchar
*) &string
, 1);
1089 xev
.property
= xsre
->property
;
1090 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1091 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1092 xsre
->target
, 8, PropModeReplace
,
1093 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1094 xev
.property
= xsre
->property
;
1097 /* all done, send a notification to the listener */
1098 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1099 fprintf(stderr
, "Error sending SelectionNotify event\n");
1103 xsetsel(char *str
) {
1104 /* register the selection for both the clipboard and the primary */
1110 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1112 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1113 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1117 brelease(XEvent
*e
) {
1118 if(IS_SET(MODE_MOUSE
)) {
1123 if(e
->xbutton
.button
== Button2
) {
1125 } else if(e
->xbutton
.button
== Button1
) {
1133 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1138 bmotion(XEvent
*e
) {
1139 int oldey
, oldex
, oldsby
, oldsey
;
1141 if(IS_SET(MODE_MOUSE
)) {
1156 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1157 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1161 die(const char *errstr
, ...) {
1164 va_start(ap
, errstr
);
1165 vfprintf(stderr
, errstr
, ap
);
1173 char *envshell
= getenv("SHELL");
1174 const struct passwd
*pass
= getpwuid(getuid());
1175 char buf
[sizeof(long) * 8 + 1];
1177 unsetenv("COLUMNS");
1179 unsetenv("TERMCAP");
1182 setenv("LOGNAME", pass
->pw_name
, 1);
1183 setenv("USER", pass
->pw_name
, 1);
1184 setenv("SHELL", pass
->pw_shell
, 0);
1185 setenv("HOME", pass
->pw_dir
, 0);
1188 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1189 setenv("WINDOWID", buf
, 1);
1191 signal(SIGCHLD
, SIG_DFL
);
1192 signal(SIGHUP
, SIG_DFL
);
1193 signal(SIGINT
, SIG_DFL
);
1194 signal(SIGQUIT
, SIG_DFL
);
1195 signal(SIGTERM
, SIG_DFL
);
1196 signal(SIGALRM
, SIG_DFL
);
1198 DEFAULT(envshell
, shell
);
1199 setenv("TERM", termname
, 1);
1200 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1201 execvp(args
[0], args
);
1209 if(waitpid(pid
, &stat
, 0) < 0)
1210 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1212 if(WIFEXITED(stat
)) {
1213 exit(WEXITSTATUS(stat
));
1222 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1224 /* seems to work fine on linux, openbsd and freebsd */
1225 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1226 die("openpty failed: %s\n", SERRNO
);
1228 switch(pid
= fork()) {
1230 die("fork failed\n");
1233 setsid(); /* create a new process group */
1234 dup2(s
, STDIN_FILENO
);
1235 dup2(s
, STDOUT_FILENO
);
1236 dup2(s
, STDERR_FILENO
);
1237 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1238 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1246 signal(SIGCHLD
, sigchld
);
1248 iofd
= (!strcmp(opt_io
, "-")) ?
1250 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1252 fprintf(stderr
, "Error opening %s:%s\n",
1253 opt_io
, strerror(errno
));
1263 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1265 fprintf(stderr
, "\n");
1270 static char buf
[BUFSIZ
];
1271 static int buflen
= 0;
1274 int charsize
; /* size of utf8 char in bytes */
1278 /* append read bytes to unprocessed bytes */
1279 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1280 die("Couldn't read from shell: %s\n", SERRNO
);
1282 /* process every complete utf8 char */
1285 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1286 charsize
= utf8decode(ptr
, &utf8c
);
1287 utf8encode(&utf8c
, s
);
1293 /* keep any uncomplete utf8 char for the next call */
1294 memmove(buf
, ptr
, buflen
);
1298 ttywrite(const char *s
, size_t n
) {
1299 if(write(cmdfd
, s
, n
) == -1)
1300 die("write error on tty: %s\n", SERRNO
);
1304 ttysend(char *s
, size_t n
) {
1306 if(IS_SET(MODE_ECHO
))
1314 w
.ws_row
= term
.row
;
1315 w
.ws_col
= term
.col
;
1316 w
.ws_xpixel
= xw
.tw
;
1317 w
.ws_ypixel
= xw
.th
;
1318 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1319 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1323 tattrset(int attr
) {
1326 for(i
= 0; i
< term
.row
-1; i
++) {
1327 for(j
= 0; j
< term
.col
-1; j
++) {
1328 if(term
.line
[i
][j
].mode
& attr
)
1337 tsetdirt(int top
, int bot
) {
1340 LIMIT(top
, 0, term
.row
-1);
1341 LIMIT(bot
, 0, term
.row
-1);
1343 for(i
= top
; i
<= bot
; i
++)
1348 tsetdirtattr(int attr
) {
1351 for(i
= 0; i
< term
.row
-1; i
++) {
1352 for(j
= 0; j
< term
.col
-1; j
++) {
1353 if(term
.line
[i
][j
].mode
& attr
) {
1363 tsetdirt(0, term
.row
-1);
1368 static TCursor c
[2];
1369 bool alt
= IS_SET(MODE_ALTSCREEN
);
1371 if(mode
== CURSOR_SAVE
) {
1373 } else if(mode
== CURSOR_LOAD
) {
1375 tmoveto(c
[alt
].x
, c
[alt
].y
);
1383 term
.c
= (TCursor
){{
1387 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1389 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1390 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1393 term
.bot
= term
.row
- 1;
1394 term
.mode
= MODE_WRAP
;
1395 memset(term
.trantbl
, sizeof(term
.trantbl
), CS_USA
);
1398 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1400 tcursor(CURSOR_SAVE
);
1404 tnew(int col
, int row
) {
1405 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1414 Line
*tmp
= term
.line
;
1416 term
.line
= term
.alt
;
1418 term
.mode
^= MODE_ALTSCREEN
;
1423 tscrolldown(int orig
, int n
) {
1427 LIMIT(n
, 0, term
.bot
-orig
+1);
1429 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1431 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1432 temp
= term
.line
[i
];
1433 term
.line
[i
] = term
.line
[i
-n
];
1434 term
.line
[i
-n
] = temp
;
1437 term
.dirty
[i
-n
] = 1;
1444 tscrollup(int orig
, int n
) {
1447 LIMIT(n
, 0, term
.bot
-orig
+1);
1449 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1451 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1452 temp
= term
.line
[i
];
1453 term
.line
[i
] = term
.line
[i
+n
];
1454 term
.line
[i
+n
] = temp
;
1457 term
.dirty
[i
+n
] = 1;
1460 selscroll(orig
, -n
);
1464 selscroll(int orig
, int n
) {
1468 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1469 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1473 if(sel
.type
== SEL_RECTANGULAR
) {
1474 if(sel
.ob
.y
< term
.top
)
1475 sel
.ob
.y
= term
.top
;
1476 if(sel
.oe
.y
> term
.bot
)
1477 sel
.oe
.y
= term
.bot
;
1479 if(sel
.ob
.y
< term
.top
) {
1480 sel
.ob
.y
= term
.top
;
1483 if(sel
.oe
.y
> term
.bot
) {
1484 sel
.oe
.y
= term
.bot
;
1485 sel
.oe
.x
= term
.col
;
1493 tnewline(int first_col
) {
1497 tscrollup(term
.top
, 1);
1501 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1506 char *p
= csiescseq
.buf
, *np
;
1515 csiescseq
.buf
[csiescseq
.len
] = '\0';
1516 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1518 v
= strtol(p
, &np
, 10);
1521 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1523 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1525 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1529 csiescseq
.mode
= *p
;
1532 /* for absolute user moves, when decom is set */
1534 tmoveato(int x
, int y
) {
1535 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1539 tmoveto(int x
, int y
) {
1542 if(term
.c
.state
& CURSOR_ORIGIN
) {
1547 maxy
= term
.row
- 1;
1549 LIMIT(x
, 0, term
.col
-1);
1550 LIMIT(y
, miny
, maxy
);
1551 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1557 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1558 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1559 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1560 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1561 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1562 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1563 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1564 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1565 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1566 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1570 * The table is proudly stolen from rxvt.
1572 if(attr
->mode
& ATTR_GFX
) {
1573 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1574 && vt100_0
[c
[0] - 0x41]) {
1575 c
= vt100_0
[c
[0] - 0x41];
1579 if(term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1580 if(x
+1 < term
.col
) {
1581 term
.line
[y
][x
+1].c
[0] = ' ';
1582 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1584 } else if(term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1585 term
.line
[y
][x
-1].c
[0] = ' ';
1586 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1590 term
.line
[y
][x
] = *attr
;
1591 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1595 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1599 temp
= x1
, x1
= x2
, x2
= temp
;
1601 temp
= y1
, y1
= y2
, y2
= temp
;
1603 LIMIT(x1
, 0, term
.col
-1);
1604 LIMIT(x2
, 0, term
.col
-1);
1605 LIMIT(y1
, 0, term
.row
-1);
1606 LIMIT(y2
, 0, term
.row
-1);
1608 for(y
= y1
; y
<= y2
; y
++) {
1610 for(x
= x1
; x
<= x2
; x
++) {
1613 term
.line
[y
][x
] = term
.c
.attr
;
1614 memcpy(term
.line
[y
][x
].c
, " ", 2);
1620 tdeletechar(int n
) {
1621 int src
= term
.c
.x
+ n
;
1623 int size
= term
.col
- src
;
1625 term
.dirty
[term
.c
.y
] = 1;
1627 if(src
>= term
.col
) {
1628 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1632 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1633 size
* sizeof(Glyph
));
1634 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1638 tinsertblank(int n
) {
1641 int size
= term
.col
- dst
;
1643 term
.dirty
[term
.c
.y
] = 1;
1645 if(dst
>= term
.col
) {
1646 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1650 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1651 size
* sizeof(Glyph
));
1652 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1656 tinsertblankline(int n
) {
1657 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1660 tscrolldown(term
.c
.y
, n
);
1664 tdeleteline(int n
) {
1665 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1668 tscrollup(term
.c
.y
, n
);
1672 tdefcolor(int *attr
, int *npar
, int l
) {
1676 switch (attr
[*npar
+ 1]) {
1677 case 2: /* direct colour in RGB space */
1678 if (*npar
+ 4 >= l
) {
1680 "erresc(38): Incorrect number of parameters (%d)\n",
1684 r
= attr
[*npar
+ 2];
1685 g
= attr
[*npar
+ 3];
1686 b
= attr
[*npar
+ 4];
1688 if(!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1689 fprintf(stderr
, "erresc: bad rgb color (%d,%d,%d)\n",
1692 idx
= TRUECOLOR(r
, g
, b
);
1694 case 5: /* indexed colour */
1695 if (*npar
+ 2 >= l
) {
1697 "erresc(38): Incorrect number of parameters (%d)\n",
1702 if(!BETWEEN(attr
[*npar
], 0, 255))
1703 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1707 case 0: /* implemented defined (only foreground) */
1708 case 1: /* transparent */
1709 case 3: /* direct colour in CMY space */
1710 case 4: /* direct colour in CMYK space */
1713 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1720 tsetattr(int *attr
, int l
) {
1724 for(i
= 0; i
< l
; i
++) {
1727 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1728 | ATTR_BOLD
| ATTR_ITALIC \
1730 term
.c
.attr
.fg
= defaultfg
;
1731 term
.c
.attr
.bg
= defaultbg
;
1734 term
.c
.attr
.mode
|= ATTR_BOLD
;
1737 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1740 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1742 case 5: /* slow blink */
1743 case 6: /* rapid blink */
1744 term
.c
.attr
.mode
|= ATTR_BLINK
;
1747 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1751 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1754 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1757 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1761 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1764 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1767 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1768 term
.c
.attr
.fg
= idx
;
1771 term
.c
.attr
.fg
= defaultfg
;
1774 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1775 term
.c
.attr
.bg
= idx
;
1778 term
.c
.attr
.bg
= defaultbg
;
1781 if(BETWEEN(attr
[i
], 30, 37)) {
1782 term
.c
.attr
.fg
= attr
[i
] - 30;
1783 } else if(BETWEEN(attr
[i
], 40, 47)) {
1784 term
.c
.attr
.bg
= attr
[i
] - 40;
1785 } else if(BETWEEN(attr
[i
], 90, 97)) {
1786 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1787 } else if(BETWEEN(attr
[i
], 100, 107)) {
1788 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1791 "erresc(default): gfx attr %d unknown\n",
1792 attr
[i
]), csidump();
1800 tsetscroll(int t
, int b
) {
1803 LIMIT(t
, 0, term
.row
-1);
1804 LIMIT(b
, 0, term
.row
-1);
1814 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1817 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1821 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1825 case 1: /* DECCKM -- Cursor key */
1826 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1828 case 5: /* DECSCNM -- Reverse video */
1830 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1831 if(mode
!= term
.mode
)
1832 redraw(REDRAW_TIMEOUT
);
1834 case 6: /* DECOM -- Origin */
1835 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1838 case 7: /* DECAWM -- Auto wrap */
1839 MODBIT(term
.mode
, set
, MODE_WRAP
);
1841 case 0: /* Error (IGNORED) */
1842 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1843 case 3: /* DECCOLM -- Column (IGNORED) */
1844 case 4: /* DECSCLM -- Scroll (IGNORED) */
1845 case 8: /* DECARM -- Auto repeat (IGNORED) */
1846 case 18: /* DECPFF -- Printer feed (IGNORED) */
1847 case 19: /* DECPEX -- Printer extent (IGNORED) */
1848 case 42: /* DECNRCM -- National characters (IGNORED) */
1849 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1851 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1852 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1854 case 9: /* X10 mouse compatibility mode */
1855 xsetpointermotion(0);
1856 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1857 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1859 case 1000: /* 1000: report button press */
1860 xsetpointermotion(0);
1861 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1862 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1864 case 1002: /* 1002: report motion on button press */
1865 xsetpointermotion(0);
1866 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1867 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1869 case 1003: /* 1003: enable all mouse motions */
1870 xsetpointermotion(set
);
1871 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1872 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1874 case 1004: /* 1004: send focus events to tty */
1875 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1877 case 1006: /* 1006: extended reporting mode */
1878 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1881 MODBIT(term
.mode
, set
, MODE_8BIT
);
1883 case 1049: /* swap screen & set/restore cursor as xterm */
1884 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1885 case 47: /* swap screen */
1887 if (!allowaltscreen
)
1889 alt
= IS_SET(MODE_ALTSCREEN
);
1891 tclearregion(0, 0, term
.col
-1,
1894 if(set
^ alt
) /* set is always 1 or 0 */
1900 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1902 case 2004: /* 2004: bracketed paste mode */
1903 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
1905 /* Not implemented mouse modes. See comments there. */
1906 case 1001: /* mouse highlight mode; can hang the
1907 terminal by design when implemented. */
1908 case 1005: /* UTF-8 mouse mode; will confuse
1909 applications not supporting UTF-8
1911 case 1015: /* urxvt mangled mouse mode; incompatible
1912 and can be mistaken for other control
1916 "erresc: unknown private set/reset mode %d\n",
1922 case 0: /* Error (IGNORED) */
1924 case 2: /* KAM -- keyboard action */
1925 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1927 case 4: /* IRM -- Insertion-replacement */
1928 MODBIT(term
.mode
, set
, MODE_INSERT
);
1930 case 12: /* SRM -- Send/Receive */
1931 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1933 case 20: /* LNM -- Linefeed/new line */
1934 MODBIT(term
.mode
, set
, MODE_CRLF
);
1938 "erresc: unknown set/reset mode %d\n",
1951 switch(csiescseq
.mode
) {
1954 fprintf(stderr
, "erresc: unknown csi ");
1958 case '@': /* ICH -- Insert <n> blank char */
1959 DEFAULT(csiescseq
.arg
[0], 1);
1960 tinsertblank(csiescseq
.arg
[0]);
1962 case 'A': /* CUU -- Cursor <n> Up */
1963 DEFAULT(csiescseq
.arg
[0], 1);
1964 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1966 case 'B': /* CUD -- Cursor <n> Down */
1967 case 'e': /* VPR --Cursor <n> Down */
1968 DEFAULT(csiescseq
.arg
[0], 1);
1969 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1971 case 'c': /* DA -- Device Attributes */
1972 if(csiescseq
.arg
[0] == 0)
1973 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1975 case 'C': /* CUF -- Cursor <n> Forward */
1976 case 'a': /* HPR -- Cursor <n> Forward */
1977 DEFAULT(csiescseq
.arg
[0], 1);
1978 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1980 case 'D': /* CUB -- Cursor <n> Backward */
1981 DEFAULT(csiescseq
.arg
[0], 1);
1982 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1984 case 'E': /* CNL -- Cursor <n> Down and first col */
1985 DEFAULT(csiescseq
.arg
[0], 1);
1986 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1988 case 'F': /* CPL -- Cursor <n> Up and first col */
1989 DEFAULT(csiescseq
.arg
[0], 1);
1990 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1992 case 'g': /* TBC -- Tabulation clear */
1993 switch(csiescseq
.arg
[0]) {
1994 case 0: /* clear current tab stop */
1995 term
.tabs
[term
.c
.x
] = 0;
1997 case 3: /* clear all the tabs */
1998 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2004 case 'G': /* CHA -- Move to <col> */
2006 DEFAULT(csiescseq
.arg
[0], 1);
2007 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2009 case 'H': /* CUP -- Move to <row> <col> */
2011 DEFAULT(csiescseq
.arg
[0], 1);
2012 DEFAULT(csiescseq
.arg
[1], 1);
2013 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2015 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2016 DEFAULT(csiescseq
.arg
[0], 1);
2017 while(csiescseq
.arg
[0]--)
2020 case 'J': /* ED -- Clear screen */
2022 switch(csiescseq
.arg
[0]) {
2024 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2025 if(term
.c
.y
< term
.row
-1) {
2026 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2032 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2033 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2036 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2042 case 'K': /* EL -- Clear line */
2043 switch(csiescseq
.arg
[0]) {
2045 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2049 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2052 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2056 case 'S': /* SU -- Scroll <n> line up */
2057 DEFAULT(csiescseq
.arg
[0], 1);
2058 tscrollup(term
.top
, csiescseq
.arg
[0]);
2060 case 'T': /* SD -- Scroll <n> line down */
2061 DEFAULT(csiescseq
.arg
[0], 1);
2062 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2064 case 'L': /* IL -- Insert <n> blank lines */
2065 DEFAULT(csiescseq
.arg
[0], 1);
2066 tinsertblankline(csiescseq
.arg
[0]);
2068 case 'l': /* RM -- Reset Mode */
2069 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2071 case 'M': /* DL -- Delete <n> lines */
2072 DEFAULT(csiescseq
.arg
[0], 1);
2073 tdeleteline(csiescseq
.arg
[0]);
2075 case 'X': /* ECH -- Erase <n> char */
2076 DEFAULT(csiescseq
.arg
[0], 1);
2077 tclearregion(term
.c
.x
, term
.c
.y
,
2078 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2080 case 'P': /* DCH -- Delete <n> char */
2081 DEFAULT(csiescseq
.arg
[0], 1);
2082 tdeletechar(csiescseq
.arg
[0]);
2084 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2085 DEFAULT(csiescseq
.arg
[0], 1);
2086 while(csiescseq
.arg
[0]--)
2089 case 'd': /* VPA -- Move to <row> */
2090 DEFAULT(csiescseq
.arg
[0], 1);
2091 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2093 case 'h': /* SM -- Set terminal mode */
2094 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2096 case 'm': /* SGR -- Terminal attribute (color) */
2097 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2099 case 'n': /* DSR – Device Status Report (cursor position) */
2100 if (csiescseq
.arg
[0] == 6) {
2101 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2102 term
.c
.y
+1, term
.c
.x
+1);
2106 case 'r': /* DECSTBM -- Set Scrolling Region */
2107 if(csiescseq
.priv
) {
2110 DEFAULT(csiescseq
.arg
[0], 1);
2111 DEFAULT(csiescseq
.arg
[1], term
.row
);
2112 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2116 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2117 tcursor(CURSOR_SAVE
);
2119 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2120 tcursor(CURSOR_LOAD
);
2131 for(i
= 0; i
< csiescseq
.len
; i
++) {
2132 c
= csiescseq
.buf
[i
] & 0xff;
2135 } else if(c
== '\n') {
2137 } else if(c
== '\r') {
2139 } else if(c
== 0x1b) {
2142 printf("(%02x)", c
);
2150 memset(&csiescseq
, 0, sizeof(csiescseq
));
2159 narg
= strescseq
.narg
;
2160 par
= atoi(strescseq
.args
[0]);
2162 switch(strescseq
.type
) {
2163 case ']': /* OSC -- Operating System Command */
2169 xsettitle(strescseq
.args
[1]);
2171 case 4: /* color set */
2174 p
= strescseq
.args
[2];
2176 case 104: /* color reset, here p = NULL */
2177 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2178 if (!xsetcolorname(j
, p
)) {
2179 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2182 * TODO if defaultbg color is changed, borders
2190 case 'k': /* old title set compatibility */
2191 xsettitle(strescseq
.args
[0]);
2193 case 'P': /* DSC -- Device Control String */
2194 case '_': /* APC -- Application Program Command */
2195 case '^': /* PM -- Privacy Message */
2199 fprintf(stderr
, "erresc: unknown str ");
2205 char *p
= strescseq
.buf
;
2208 strescseq
.buf
[strescseq
.len
] = '\0';
2209 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2210 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2218 printf("ESC%c", strescseq
.type
);
2219 for(i
= 0; i
< strescseq
.len
; i
++) {
2220 c
= strescseq
.buf
[i
] & 0xff;
2223 } else if(isprint(c
)) {
2225 } else if(c
== '\n') {
2227 } else if(c
== '\r') {
2229 } else if(c
== 0x1b) {
2232 printf("(%02x)", c
);
2240 memset(&strescseq
, 0, sizeof(strescseq
));
2244 tputtab(bool forward
) {
2250 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2255 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2258 tmoveto(x
, term
.c
.y
);
2262 techo(char *buf
, int len
) {
2263 for(; len
> 0; buf
++, len
--) {
2266 if(c
== '\033') { /* escape */
2269 } else if(c
< '\x20') { /* control code */
2270 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2284 tdeftran(char ascii
) {
2286 static char tbl
[][2] = {
2287 {'0', CS_GRAPHIC0
}, {'1', CS_GRAPHIC1
}, {'A', CS_UK
},
2288 {'B', CS_USA
}, {'<', CS_MULTI
}, {'K', CS_GER
},
2289 {'5', CS_FIN
}, {'C', CS_FIN
},
2293 for (bp
= &tbl
[0]; (c
= (*bp
)[0]) && c
!= ascii
; ++bp
)
2297 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2299 term
.trantbl
[term
.icharset
] = (*bp
)[1];
2304 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
)
2305 term
.c
.attr
.mode
|= ATTR_GFX
;
2307 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2311 tputc(char *c
, int len
) {
2313 bool control
= ascii
< '\x20' || ascii
== 0177;
2320 utf8decode(c
, &u8char
);
2321 width
= wcwidth(u8char
);
2325 if(xwrite(iofd
, c
, len
) < 0) {
2326 fprintf(stderr
, "Error writing in %s:%s\n",
2327 opt_io
, strerror(errno
));
2334 * STR sequences must be checked before anything else
2335 * because it can use some control codes as part of the sequence.
2337 if(term
.esc
& ESC_STR
) {
2340 term
.esc
= ESC_START
| ESC_STR_END
;
2342 case '\a': /* backwards compatibility to xterm */
2347 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2348 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2349 strescseq
.len
+= len
;
2352 * Here is a bug in terminals. If the user never sends
2353 * some code to stop the str or esc command, then st
2354 * will stop responding. But this is better than
2355 * silently failing with unknown characters. At least
2356 * then users will report back.
2358 * In the case users ever get fixed, here is the code:
2370 * Actions of control codes must be performed as soon they arrive
2371 * because they can be embedded inside a control sequence, and
2372 * they must not cause conflicts with sequences.
2380 tmoveto(term
.c
.x
-1, term
.c
.y
);
2383 tmoveto(0, term
.c
.y
);
2388 /* go to first col if the mode is set */
2389 tnewline(IS_SET(MODE_CRLF
));
2391 case '\a': /* BEL */
2392 if(!(xw
.state
& WIN_FOCUSED
))
2395 XBell(xw
.dpy
, bellvolume
);
2397 case '\033': /* ESC */
2399 term
.esc
= ESC_START
;
2401 case '\016': /* SO */
2405 case '\017': /* SI */
2409 case '\032': /* SUB */
2410 case '\030': /* CAN */
2413 case '\005': /* ENQ (IGNORED) */
2414 case '\000': /* NUL (IGNORED) */
2415 case '\021': /* XON (IGNORED) */
2416 case '\023': /* XOFF (IGNORED) */
2417 case 0177: /* DEL (IGNORED) */
2420 } else if(term
.esc
& ESC_START
) {
2421 if(term
.esc
& ESC_CSI
) {
2422 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2423 if(BETWEEN(ascii
, 0x40, 0x7E)
2424 || csiescseq
.len
>= \
2425 sizeof(csiescseq
.buf
)-1) {
2430 } else if(term
.esc
& ESC_STR_END
) {
2434 } else if(term
.esc
& ESC_ALTCHARSET
) {
2438 } else if(term
.esc
& ESC_TEST
) {
2439 if(ascii
== '8') { /* DEC screen alignment test. */
2440 char E
[UTF_SIZ
] = "E";
2443 for(x
= 0; x
< term
.col
; ++x
) {
2444 for(y
= 0; y
< term
.row
; ++y
)
2445 tsetchar(E
, &term
.c
.attr
, x
, y
);
2452 term
.esc
|= ESC_CSI
;
2455 term
.esc
|= ESC_TEST
;
2457 case 'P': /* DCS -- Device Control String */
2458 case '_': /* APC -- Application Program Command */
2459 case '^': /* PM -- Privacy Message */
2460 case ']': /* OSC -- Operating System Command */
2461 case 'k': /* old title set compatibility */
2463 strescseq
.type
= ascii
;
2464 term
.esc
|= ESC_STR
;
2466 case '(': /* set primary charset G0 */
2467 case ')': /* set secondary charset G1 */
2468 case '*': /* set tertiary charset G2 */
2469 case '+': /* set quaternary charset G3 */
2470 term
.icharset
= ascii
- '(';
2471 term
.esc
|= ESC_ALTCHARSET
;
2473 case 'D': /* IND -- Linefeed */
2474 if(term
.c
.y
== term
.bot
) {
2475 tscrollup(term
.top
, 1);
2477 tmoveto(term
.c
.x
, term
.c
.y
+1);
2481 case 'E': /* NEL -- Next line */
2482 tnewline(1); /* always go to first col */
2485 case 'H': /* HTS -- Horizontal tab stop */
2486 term
.tabs
[term
.c
.x
] = 1;
2489 case 'M': /* RI -- Reverse index */
2490 if(term
.c
.y
== term
.top
) {
2491 tscrolldown(term
.top
, 1);
2493 tmoveto(term
.c
.x
, term
.c
.y
-1);
2497 case 'Z': /* DECID -- Identify Terminal */
2498 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2501 case 'c': /* RIS -- Reset to inital state */
2507 case '=': /* DECPAM -- Application keypad */
2508 term
.mode
|= MODE_APPKEYPAD
;
2511 case '>': /* DECPNM -- Normal keypad */
2512 term
.mode
&= ~MODE_APPKEYPAD
;
2515 case '7': /* DECSC -- Save Cursor */
2516 tcursor(CURSOR_SAVE
);
2519 case '8': /* DECRC -- Restore Cursor */
2520 tcursor(CURSOR_LOAD
);
2523 case '\\': /* ST -- Stop */
2527 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2528 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2533 * All characters which form part of a sequence are not
2539 * Display control codes only if we are in graphic mode
2541 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2543 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2545 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2546 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2550 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2551 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2552 &term
.line
[term
.c
.y
][term
.c
.x
],
2553 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2556 if(term
.c
.x
+width
> term
.col
)
2559 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2562 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WIDE
;
2563 if(term
.c
.x
+1 < term
.col
) {
2564 term
.line
[term
.c
.y
][term
.c
.x
+1].c
[0] = '\0';
2565 term
.line
[term
.c
.y
][term
.c
.x
+1].mode
= ATTR_WDUMMY
;
2568 if(term
.c
.x
+width
< term
.col
) {
2569 tmoveto(term
.c
.x
+width
, term
.c
.y
);
2571 term
.c
.state
|= CURSOR_WRAPNEXT
;
2576 tresize(int col
, int row
) {
2578 int minrow
= MIN(row
, term
.row
);
2579 int mincol
= MIN(col
, term
.col
);
2580 int slide
= term
.c
.y
- row
+ 1;
2584 if(col
< 1 || row
< 1)
2587 /* free unneeded rows */
2591 * slide screen to keep cursor where we expect it -
2592 * tscrollup would work here, but we can optimize to
2593 * memmove because we're freeing the earlier lines
2595 for(/* i = 0 */; i
< slide
; i
++) {
2599 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2600 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2602 for(i
+= row
; i
< term
.row
; i
++) {
2607 /* resize to new height */
2608 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2609 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2610 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2611 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2613 /* resize each row to new width, zero-pad if needed */
2614 for(i
= 0; i
< minrow
; i
++) {
2616 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2617 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2620 /* allocate any new rows */
2621 for(/* i == minrow */; i
< row
; i
++) {
2623 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
2624 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
2626 if(col
> term
.col
) {
2627 bp
= term
.tabs
+ term
.col
;
2629 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2630 while(--bp
> term
.tabs
&& !*bp
)
2632 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2635 /* update terminal size */
2638 /* reset scrolling region */
2639 tsetscroll(0, row
-1);
2640 /* make use of the LIMIT in tmoveto */
2641 tmoveto(term
.c
.x
, term
.c
.y
);
2642 /* Clearing both screens */
2645 if(mincol
< col
&& 0 < minrow
) {
2646 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2648 if(0 < col
&& minrow
< row
) {
2649 tclearregion(0, minrow
, col
- 1, row
- 1);
2652 } while(orig
!= term
.line
);
2658 xresize(int col
, int row
) {
2659 xw
.tw
= MAX(1, col
* xw
.cw
);
2660 xw
.th
= MAX(1, row
* xw
.ch
);
2662 XFreePixmap(xw
.dpy
, xw
.buf
);
2663 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2664 DefaultDepth(xw
.dpy
, xw
.scr
));
2665 XftDrawChange(xw
.draw
, xw
.buf
);
2666 xclear(0, 0, xw
.w
, xw
.h
);
2669 static inline ushort
2670 sixd_to_16bit(int x
) {
2671 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2677 XRenderColor color
= { .alpha
= 0xffff };
2682 for (cp
= dc
.col
; cp
< dc
.col
+ LEN(dc
.col
); ++cp
)
2683 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
2686 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2687 for(i
= 0; i
< LEN(colorname
); i
++) {
2690 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2691 die("Could not allocate color '%s'\n", colorname
[i
]);
2695 /* load colors [16-255] ; same colors as xterm */
2696 for(i
= 16, r
= 0; r
< 6; r
++) {
2697 for(g
= 0; g
< 6; g
++) {
2698 for(b
= 0; b
< 6; b
++) {
2699 color
.red
= sixd_to_16bit(r
);
2700 color
.green
= sixd_to_16bit(g
);
2701 color
.blue
= sixd_to_16bit(b
);
2702 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2703 die("Could not allocate color %d\n", i
);
2710 for(r
= 0; r
< 24; r
++, i
++) {
2711 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2712 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2714 die("Could not allocate color %d\n", i
);
2721 xsetcolorname(int x
, const char *name
) {
2722 XRenderColor color
= { .alpha
= 0xffff };
2724 if (x
< 0 || x
> LEN(colorname
))
2727 if(16 <= x
&& x
< 16 + 216) {
2728 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2729 color
.red
= sixd_to_16bit(r
);
2730 color
.green
= sixd_to_16bit(g
);
2731 color
.blue
= sixd_to_16bit(b
);
2732 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2733 return 0; /* something went wrong */
2736 } else if (16 + 216 <= x
&& x
< 256) {
2737 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2738 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2739 return 0; /* something went wrong */
2743 name
= colorname
[x
];
2746 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2753 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2754 XftDrawRect(xw
.draw
,
2755 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2756 borderpx
+ col1
* xw
.cw
,
2757 borderpx
+ row1
* xw
.ch
,
2758 (col2
-col1
+1) * xw
.cw
,
2759 (row2
-row1
+1) * xw
.ch
);
2763 * Absolute coordinates.
2766 xclear(int x1
, int y1
, int x2
, int y2
) {
2767 XftDrawRect(xw
.draw
,
2768 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2769 x1
, y1
, x2
-x1
, y2
-y1
);
2774 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2775 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2776 XSizeHints
*sizeh
= NULL
;
2778 sizeh
= XAllocSizeHints();
2779 if(xw
.isfixed
== False
) {
2780 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2781 sizeh
->height
= xw
.h
;
2782 sizeh
->width
= xw
.w
;
2783 sizeh
->height_inc
= xw
.ch
;
2784 sizeh
->width_inc
= xw
.cw
;
2785 sizeh
->base_height
= 2 * borderpx
;
2786 sizeh
->base_width
= 2 * borderpx
;
2788 sizeh
->flags
= PMaxSize
| PMinSize
;
2789 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2790 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2793 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2798 xloadfont(Font
*f
, FcPattern
*pattern
) {
2802 match
= FcFontMatch(NULL
, pattern
, &result
);
2806 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2807 FcPatternDestroy(match
);
2812 f
->pattern
= FcPatternDuplicate(pattern
);
2814 f
->ascent
= f
->match
->ascent
;
2815 f
->descent
= f
->match
->descent
;
2817 f
->rbearing
= f
->match
->max_advance_width
;
2819 f
->height
= f
->ascent
+ f
->descent
;
2820 f
->width
= f
->lbearing
+ f
->rbearing
;
2826 xloadfonts(char *fontstr
, double fontsize
) {
2828 FcResult r_sz
, r_psz
;
2831 if(fontstr
[0] == '-') {
2832 pattern
= XftXlfdParse(fontstr
, False
, False
);
2834 pattern
= FcNameParse((FcChar8
*)fontstr
);
2838 die("st: can't open font %s\n", fontstr
);
2841 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2842 FcPatternDel(pattern
, FC_SIZE
);
2843 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2844 usedfontsize
= fontsize
;
2846 r_psz
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2847 r_sz
= FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
);
2848 if(r_psz
== FcResultMatch
) {
2849 usedfontsize
= fontval
;
2850 } else if(r_sz
== FcResultMatch
) {
2854 * Default font size is 12, if none given. This is to
2855 * have a known usedfontsize value.
2857 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2862 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2863 FcDefaultSubstitute(pattern
);
2865 if(xloadfont(&dc
.font
, pattern
))
2866 die("st: can't open font %s\n", fontstr
);
2868 if(usedfontsize
< 0) {
2869 FcPatternGetDouble(dc
.font
.match
->pattern
,
2870 FC_PIXEL_SIZE
, 0, &fontval
);
2871 usedfontsize
= fontval
;
2874 /* Setting character width and height. */
2875 xw
.cw
= CEIL(dc
.font
.width
* cwscale
);
2876 xw
.ch
= CEIL(dc
.font
.height
* chscale
);
2878 FcPatternDel(pattern
, FC_SLANT
);
2879 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2880 if(xloadfont(&dc
.ifont
, pattern
))
2881 die("st: can't open font %s\n", fontstr
);
2883 FcPatternDel(pattern
, FC_WEIGHT
);
2884 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2885 if(xloadfont(&dc
.ibfont
, pattern
))
2886 die("st: can't open font %s\n", fontstr
);
2888 FcPatternDel(pattern
, FC_SLANT
);
2889 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2890 if(xloadfont(&dc
.bfont
, pattern
))
2891 die("st: can't open font %s\n", fontstr
);
2893 FcPatternDestroy(pattern
);
2897 xloadfontset(Font
*f
) {
2900 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2906 xunloadfont(Font
*f
) {
2907 XftFontClose(xw
.dpy
, f
->match
);
2908 FcPatternDestroy(f
->pattern
);
2910 FcFontSetDestroy(f
->set
);
2914 xunloadfonts(void) {
2917 /* Free the loaded fonts in the font cache. */
2918 for(i
= 0; i
< frclen
; i
++) {
2919 XftFontClose(xw
.dpy
, frc
[i
].font
);
2923 xunloadfont(&dc
.font
);
2924 xunloadfont(&dc
.bfont
);
2925 xunloadfont(&dc
.ifont
);
2926 xunloadfont(&dc
.ibfont
);
2930 xzoom(const Arg
*arg
) {
2932 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2943 pid_t thispid
= getpid();
2945 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2946 die("Can't open display\n");
2947 xw
.scr
= XDefaultScreen(xw
.dpy
);
2948 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2952 die("Could not init fontconfig.\n");
2954 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2955 xloadfonts(usedfont
, 0);
2958 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2961 /* adjust fixed window geometry */
2963 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2964 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2966 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2968 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2973 /* window - default size */
2974 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2975 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2981 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2982 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2983 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2984 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2985 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2986 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2987 xw
.attrs
.colormap
= xw
.cmap
;
2989 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2990 XRootWindow(xw
.dpy
, xw
.scr
);
2991 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2992 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2993 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2994 | CWEventMask
| CWColormap
, &xw
.attrs
);
2996 memset(&gcvalues
, 0, sizeof(gcvalues
));
2997 gcvalues
.graphics_exposures
= False
;
2998 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3000 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3001 DefaultDepth(xw
.dpy
, xw
.scr
));
3002 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3003 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3005 /* Xft rendering context */
3006 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3009 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3010 XSetLocaleModifiers("@im=local");
3011 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3012 XSetLocaleModifiers("@im=");
3013 if((xw
.xim
= XOpenIM(xw
.dpy
,
3014 NULL
, NULL
, NULL
)) == NULL
) {
3015 die("XOpenIM failed. Could not open input"
3020 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3021 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3022 XNFocusWindow
, xw
.win
, NULL
);
3024 die("XCreateIC failed. Could not obtain input method.\n");
3026 /* white cursor, black outline */
3027 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
3028 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3029 XRecolorCursor(xw
.dpy
, cursor
,
3030 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
3031 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
3033 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3034 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3035 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3036 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3038 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3039 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3040 PropModeReplace
, (unsigned char *)&thispid
, 1);
3043 XMapWindow(xw
.dpy
, xw
.win
);
3049 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
3050 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3051 width
= charlen
* xw
.cw
, xp
, i
;
3053 int u8fl
, u8fblen
, u8cblen
, doesexist
;
3056 Font
*font
= &dc
.font
;
3058 FcPattern
*fcpattern
, *fontpattern
;
3059 FcFontSet
*fcsets
[] = { NULL
};
3060 FcCharSet
*fccharset
;
3061 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3062 XRenderColor colfg
, colbg
;
3066 frcflags
= FRC_NORMAL
;
3068 if(base
.mode
& ATTR_ITALIC
) {
3069 if(base
.fg
== defaultfg
)
3070 base
.fg
= defaultitalic
;
3072 frcflags
= FRC_ITALIC
;
3073 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
3074 if(base
.fg
== defaultfg
)
3075 base
.fg
= defaultitalic
;
3077 frcflags
= FRC_ITALICBOLD
;
3078 } else if(base
.mode
& ATTR_UNDERLINE
) {
3079 if(base
.fg
== defaultfg
)
3080 base
.fg
= defaultunderline
;
3082 if(IS_TRUECOL(base
.fg
)) {
3083 colfg
.alpha
= 0xffff;
3084 colfg
.red
= TRUERED(base
.fg
);
3085 colfg
.green
= TRUEGREEN(base
.fg
);
3086 colfg
.blue
= TRUEBLUE(base
.fg
);
3087 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3090 fg
= &dc
.col
[base
.fg
];
3093 if(IS_TRUECOL(base
.bg
)) {
3094 colbg
.alpha
= 0xffff;
3095 colbg
.green
= TRUEGREEN(base
.bg
);
3096 colbg
.red
= TRUERED(base
.bg
);
3097 colbg
.blue
= TRUEBLUE(base
.bg
);
3098 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3101 bg
= &dc
.col
[base
.bg
];
3106 if(base
.mode
& ATTR_BOLD
) {
3107 if(BETWEEN(base
.fg
, 0, 7)) {
3108 /* basic system colors */
3109 fg
= &dc
.col
[base
.fg
+ 8];
3110 } else if(BETWEEN(base
.fg
, 16, 195)) {
3112 fg
= &dc
.col
[base
.fg
+ 36];
3113 } else if(BETWEEN(base
.fg
, 232, 251)) {
3115 fg
= &dc
.col
[base
.fg
+ 4];
3118 * Those ranges will not be brightened:
3119 * 8 - 15 – bright system colors
3120 * 196 - 231 – highest 256 color cube
3121 * 252 - 255 – brightest colors in greyscale
3124 frcflags
= FRC_BOLD
;
3127 if(IS_SET(MODE_REVERSE
)) {
3128 if(fg
== &dc
.col
[defaultfg
]) {
3129 fg
= &dc
.col
[defaultbg
];
3131 colfg
.red
= ~fg
->color
.red
;
3132 colfg
.green
= ~fg
->color
.green
;
3133 colfg
.blue
= ~fg
->color
.blue
;
3134 colfg
.alpha
= fg
->color
.alpha
;
3135 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3139 if(bg
== &dc
.col
[defaultbg
]) {
3140 bg
= &dc
.col
[defaultfg
];
3142 colbg
.red
= ~bg
->color
.red
;
3143 colbg
.green
= ~bg
->color
.green
;
3144 colbg
.blue
= ~bg
->color
.blue
;
3145 colbg
.alpha
= bg
->color
.alpha
;
3146 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3151 if(base
.mode
& ATTR_REVERSE
) {
3157 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3160 /* Intelligent cleaning up of the borders. */
3162 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3163 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3165 if(x
+ charlen
>= term
.col
) {
3166 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3167 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3170 xclear(winx
, 0, winx
+ width
, borderpx
);
3172 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3174 /* Clean up the region we want to draw to. */
3175 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3177 /* Set the clip region because Xft is sometimes dirty. */
3182 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3184 for(xp
= winx
; bytelen
> 0;) {
3186 * Search for the range in the to be printed string of glyphs
3187 * that are in the main font. Then print that range. If
3188 * some glyph is found that is not in the font, do the
3194 oneatatime
= font
->width
!= xw
.cw
;
3197 u8cblen
= utf8decode(s
, &u8char
);
3201 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3202 if(oneatatime
|| !doesexist
|| bytelen
<= 0) {
3203 if(oneatatime
|| bytelen
<= 0) {
3211 XftDrawStringUtf8(xw
.draw
, fg
,
3213 winy
+ font
->ascent
,
3231 /* Search the font cache. */
3232 for(i
= 0; i
< frclen
; i
++) {
3233 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3234 && frc
[i
].flags
== frcflags
) {
3239 /* Nothing was found. */
3243 fcsets
[0] = font
->set
;
3246 * Nothing was found in the cache. Now use
3247 * some dozen of Fontconfig calls to get the
3248 * font for one single character.
3250 fcpattern
= FcPatternDuplicate(font
->pattern
);
3251 fccharset
= FcCharSetCreate();
3253 FcCharSetAddChar(fccharset
, u8char
);
3254 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3256 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3259 FcConfigSubstitute(0, fcpattern
,
3261 FcDefaultSubstitute(fcpattern
);
3263 fontpattern
= FcFontSetMatch(0, fcsets
,
3264 FcTrue
, fcpattern
, &fcres
);
3267 * Overwrite or create the new cache entry.
3269 if(frclen
>= LEN(frc
)) {
3270 frclen
= LEN(frc
) - 1;
3271 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3274 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3276 frc
[frclen
].flags
= frcflags
;
3281 FcPatternDestroy(fcpattern
);
3282 FcCharSetDestroy(fccharset
);
3285 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3286 xp
, winy
+ frc
[i
].font
->ascent
,
3287 (FcChar8
*)u8c
, u8cblen
);
3289 xp
+= xw
.cw
* wcwidth(u8char
);
3293 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3294 winy + font->ascent, (FcChar8 *)s, bytelen);
3297 if(base
.mode
& ATTR_UNDERLINE
) {
3298 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3302 /* Reset clip to none. */
3303 XftDrawSetClip(xw
.draw
, 0);
3308 static int oldx
= 0, oldy
= 0;
3309 int sl
, width
, curx
;
3310 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3312 LIMIT(oldx
, 0, term
.col
-1);
3313 LIMIT(oldy
, 0, term
.row
-1);
3317 /* adjust position if in dummy */
3318 if(term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3320 if(term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3323 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3325 /* remove the old cursor */
3326 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3327 width
= (term
.line
[oldy
][oldx
].mode
& ATTR_WIDE
)? 2 : 1;
3328 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3331 /* draw the new one */
3332 if(!(IS_SET(MODE_HIDE
))) {
3333 if(xw
.state
& WIN_FOCUSED
) {
3334 if(IS_SET(MODE_REVERSE
)) {
3335 g
.mode
|= ATTR_REVERSE
;
3341 width
= (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
)\
3343 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, width
, sl
);
3345 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3346 borderpx
+ curx
* xw
.cw
,
3347 borderpx
+ term
.c
.y
* xw
.ch
,
3349 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3350 borderpx
+ curx
* xw
.cw
,
3351 borderpx
+ term
.c
.y
* xw
.ch
,
3353 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3354 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3355 borderpx
+ term
.c
.y
* xw
.ch
,
3357 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3358 borderpx
+ curx
* xw
.cw
,
3359 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3362 oldx
= curx
, oldy
= term
.c
.y
;
3368 xsettitle(char *p
) {
3371 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3373 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3374 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3380 xsettitle(opt_title
? opt_title
: "st");
3384 redraw(int timeout
) {
3385 struct timespec tv
= {0, timeout
* 1000};
3391 nanosleep(&tv
, NULL
);
3392 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3398 drawregion(0, 0, term
.col
, term
.row
);
3399 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3401 XSetForeground(xw
.dpy
, dc
.gc
,
3402 dc
.col
[IS_SET(MODE_REVERSE
)?
3403 defaultfg
: defaultbg
].pixel
);
3407 drawregion(int x1
, int y1
, int x2
, int y2
) {
3408 int ic
, ib
, x
, y
, ox
, sl
;
3410 char buf
[DRAW_BUF_SIZ
];
3411 bool ena_sel
= sel
.ob
.x
!= -1;
3414 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3417 if(!(xw
.state
& WIN_VISIBLE
))
3420 for(y
= y1
; y
< y2
; y
++) {
3424 xtermclear(0, y
, term
.col
, y
);
3426 base
= term
.line
[y
][0];
3428 for(x
= x1
; x
< x2
; x
++) {
3429 new = term
.line
[y
][x
];
3430 if(new.mode
== ATTR_WDUMMY
)
3432 if(ena_sel
&& selected(x
, y
))
3433 new.mode
^= ATTR_REVERSE
;
3434 if(ib
> 0 && (ATTRCMP(base
, new)
3435 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3436 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3444 sl
= utf8decode(new.c
, &u8char
);
3445 memcpy(buf
+ib
, new.c
, sl
);
3447 ic
+= (new.mode
& ATTR_WIDE
)? 2 : 1;
3450 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3456 expose(XEvent
*ev
) {
3457 XExposeEvent
*e
= &ev
->xexpose
;
3459 if(xw
.state
& WIN_REDRAW
) {
3461 xw
.state
&= ~WIN_REDRAW
;
3467 visibility(XEvent
*ev
) {
3468 XVisibilityEvent
*e
= &ev
->xvisibility
;
3470 if(e
->state
== VisibilityFullyObscured
) {
3471 xw
.state
&= ~WIN_VISIBLE
;
3472 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3473 /* need a full redraw for next Expose, not just a buf copy */
3474 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3480 xw
.state
&= ~WIN_VISIBLE
;
3484 xsetpointermotion(int set
) {
3485 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3486 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3490 xseturgency(int add
) {
3491 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3493 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3494 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3500 XFocusChangeEvent
*e
= &ev
->xfocus
;
3502 if(e
->mode
== NotifyGrab
)
3505 if(ev
->type
== FocusIn
) {
3506 XSetICFocus(xw
.xic
);
3507 xw
.state
|= WIN_FOCUSED
;
3509 if(IS_SET(MODE_FOCUS
))
3510 ttywrite("\033[I", 3);
3512 XUnsetICFocus(xw
.xic
);
3513 xw
.state
&= ~WIN_FOCUSED
;
3514 if(IS_SET(MODE_FOCUS
))
3515 ttywrite("\033[O", 3);
3520 match(uint mask
, uint state
) {
3521 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
3525 numlock(const Arg
*dummy
) {
3530 kmap(KeySym k
, uint state
) {
3534 /* Check for mapped keys out of X11 function keys. */
3535 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3536 if(mappedkeys
[i
] == k
)
3539 if(i
== LEN(mappedkeys
)) {
3540 if((k
& 0xFFFF) < 0xFD00)
3544 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3548 if(!match(kp
->mask
, state
))
3551 if(IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
3553 if(term
.numlock
&& kp
->appkey
== 2)
3556 if(IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
3559 if(IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
3569 kpress(XEvent
*ev
) {
3570 XKeyEvent
*e
= &ev
->xkey
;
3572 char buf
[32], *customkey
;
3578 if(IS_SET(MODE_KBDLOCK
))
3581 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
3583 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3584 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3585 bp
->func(&(bp
->arg
));
3590 /* 2. custom keys from config.h */
3591 if((customkey
= kmap(ksym
, e
->state
))) {
3592 ttysend(customkey
, strlen(customkey
));
3596 /* 3. composed string from input method */
3599 if(len
== 1 && e
->state
& Mod1Mask
) {
3600 if(IS_SET(MODE_8BIT
)) {
3603 len
= utf8encode(&c
, buf
);
3616 cmessage(XEvent
*e
) {
3619 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3621 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3622 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3623 xw
.state
|= WIN_FOCUSED
;
3625 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3626 xw
.state
&= ~WIN_FOCUSED
;
3628 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3629 /* Send SIGHUP to shell */
3636 cresize(int width
, int height
) {
3644 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3645 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3654 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3657 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3663 int w
= xw
.w
, h
= xw
.h
;
3665 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3666 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3668 /* Waiting for window mapping */
3670 XNextEvent(xw
.dpy
, &ev
);
3671 if(ev
.type
== ConfigureNotify
) {
3672 w
= ev
.xconfigure
.width
;
3673 h
= ev
.xconfigure
.height
;
3674 } else if(ev
.type
== MapNotify
) {
3683 cresize(xw
.fw
, xw
.fh
);
3685 gettimeofday(&lastblink
, NULL
);
3686 gettimeofday(&last
, NULL
);
3688 for(xev
= actionfps
;;) {
3692 FD_SET(cmdfd
, &rfd
);
3695 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3698 die("select failed: %s\n", SERRNO
);
3700 if(FD_ISSET(cmdfd
, &rfd
)) {
3703 blinkset
= tattrset(ATTR_BLINK
);
3705 MODBIT(term
.mode
, 0, MODE_BLINK
);
3709 if(FD_ISSET(xfd
, &rfd
))
3712 gettimeofday(&now
, NULL
);
3713 drawtimeout
.tv_sec
= 0;
3714 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3718 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3719 tsetdirtattr(ATTR_BLINK
);
3720 term
.mode
^= MODE_BLINK
;
3721 gettimeofday(&lastblink
, NULL
);
3724 deltatime
= TIMEDIFF(now
, last
);
3725 if(deltatime
> (xev
? (1000/xfps
) : (1000/actionfps
))
3732 while(XPending(xw
.dpy
)) {
3733 XNextEvent(xw
.dpy
, &ev
);
3734 if(XFilterEvent(&ev
, None
))
3736 if(handler
[ev
.type
])
3737 (handler
[ev
.type
])(&ev
);
3743 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3745 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3747 if(TIMEDIFF(now
, lastblink
) \
3749 drawtimeout
.tv_usec
= 1;
3751 drawtimeout
.tv_usec
= (1000 * \
3766 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3767 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3768 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3772 main(int argc
, char *argv
[]) {
3777 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3782 allowaltscreen
= false;
3785 opt_class
= EARGF(usage());
3788 /* eat all remaining arguments */
3791 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3792 titles
= strdup(argv
[1]);
3793 opt_title
= basename(titles
);
3798 opt_font
= EARGF(usage());
3801 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3806 if(bitm
& WidthValue
)
3808 if(bitm
& HeightValue
)
3810 if(bitm
& XNegative
&& xw
.fx
== 0)
3812 if(bitm
& YNegative
&& xw
.fy
== 0)
3815 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3819 opt_io
= EARGF(usage());
3822 opt_title
= EARGF(usage());
3825 opt_embed
= EARGF(usage());
3833 setlocale(LC_CTYPE
, "");
3834 XSetLocaleModifiers("");