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 *, int);
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 int 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
;
958 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
959 strcmp(last
->c
, " ") != 0))
962 for(x
= 0; gp
<= last
; x
++, ++gp
) {
963 if(!selected(x
, y
) || (gp
->mode
& ATTR_WDUMMY
))
966 size
= utf8size(gp
->c
);
967 memcpy(ptr
, gp
->c
, size
);
972 * Copy and pasting of line endings is inconsistent
973 * in the inconsistent terminal and GUI world.
974 * The best solution seems like to produce '\n' when
975 * something is copied from st and convert '\n' to
976 * '\r', when something to be pasted is received by
978 * FIXME: Fix the computer world.
980 if(y
< sel
.ne
.y
&& x
> 0 && !((gp
-1)->mode
& ATTR_WRAP
))
984 * If the last selected line expands in the selection
985 * after the visible text '\n' is appended.
989 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
992 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
1004 selnotify(XEvent
*e
) {
1005 ulong nitems
, ofs
, rem
;
1007 uchar
*data
, *last
, *repl
;
1012 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
1013 False
, AnyPropertyType
, &type
, &format
,
1014 &nitems
, &rem
, &data
)) {
1015 fprintf(stderr
, "Clipboard allocation failed\n");
1020 * As seen in selcopy:
1021 * Line endings are inconsistent in the terminal and GUI world
1022 * copy and pasting. When receiving some selection data,
1023 * replace all '\n' with '\r'.
1024 * FIXME: Fix the computer world.
1027 last
= data
+ nitems
* format
/ 8;
1028 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1032 if(IS_SET(MODE_BRCKTPASTE
))
1033 ttywrite("\033[200~", 6);
1034 ttysend((char *)data
, nitems
* format
/ 8);
1035 if(IS_SET(MODE_BRCKTPASTE
))
1036 ttywrite("\033[201~", 6);
1038 /* number of 32-bit chunks returned */
1039 ofs
+= nitems
* format
/ 32;
1044 selpaste(const Arg
*dummy
) {
1045 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1046 xw
.win
, CurrentTime
);
1050 clippaste(const Arg
*dummy
) {
1053 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1054 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1055 xw
.win
, CurrentTime
);
1059 selclear(XEvent
*e
) {
1063 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1067 selrequest(XEvent
*e
) {
1068 XSelectionRequestEvent
*xsre
;
1069 XSelectionEvent xev
;
1070 Atom xa_targets
, string
;
1072 xsre
= (XSelectionRequestEvent
*) e
;
1073 xev
.type
= SelectionNotify
;
1074 xev
.requestor
= xsre
->requestor
;
1075 xev
.selection
= xsre
->selection
;
1076 xev
.target
= xsre
->target
;
1077 xev
.time
= xsre
->time
;
1079 xev
.property
= None
;
1081 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1082 if(xsre
->target
== xa_targets
) {
1083 /* respond with the supported type */
1084 string
= sel
.xtarget
;
1085 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1086 XA_ATOM
, 32, PropModeReplace
,
1087 (uchar
*) &string
, 1);
1088 xev
.property
= xsre
->property
;
1089 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1090 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1091 xsre
->target
, 8, PropModeReplace
,
1092 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1093 xev
.property
= xsre
->property
;
1096 /* all done, send a notification to the listener */
1097 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1098 fprintf(stderr
, "Error sending SelectionNotify event\n");
1102 xsetsel(char *str
) {
1103 /* register the selection for both the clipboard and the primary */
1109 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1111 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1112 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1116 brelease(XEvent
*e
) {
1117 if(IS_SET(MODE_MOUSE
)) {
1122 if(e
->xbutton
.button
== Button2
) {
1124 } else if(e
->xbutton
.button
== Button1
) {
1132 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1137 bmotion(XEvent
*e
) {
1138 int oldey
, oldex
, oldsby
, oldsey
;
1140 if(IS_SET(MODE_MOUSE
)) {
1155 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1156 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1160 die(const char *errstr
, ...) {
1163 va_start(ap
, errstr
);
1164 vfprintf(stderr
, errstr
, ap
);
1172 char *envshell
= getenv("SHELL");
1173 const struct passwd
*pass
= getpwuid(getuid());
1174 char buf
[sizeof(long) * 8 + 1];
1176 unsetenv("COLUMNS");
1178 unsetenv("TERMCAP");
1181 setenv("LOGNAME", pass
->pw_name
, 1);
1182 setenv("USER", pass
->pw_name
, 1);
1183 setenv("SHELL", pass
->pw_shell
, 0);
1184 setenv("HOME", pass
->pw_dir
, 0);
1187 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1188 setenv("WINDOWID", buf
, 1);
1190 signal(SIGCHLD
, SIG_DFL
);
1191 signal(SIGHUP
, SIG_DFL
);
1192 signal(SIGINT
, SIG_DFL
);
1193 signal(SIGQUIT
, SIG_DFL
);
1194 signal(SIGTERM
, SIG_DFL
);
1195 signal(SIGALRM
, SIG_DFL
);
1197 DEFAULT(envshell
, shell
);
1198 setenv("TERM", termname
, 1);
1199 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1200 execvp(args
[0], args
);
1208 if(waitpid(pid
, &stat
, 0) < 0)
1209 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1211 if(WIFEXITED(stat
)) {
1212 exit(WEXITSTATUS(stat
));
1221 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1223 /* seems to work fine on linux, openbsd and freebsd */
1224 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1225 die("openpty failed: %s\n", SERRNO
);
1227 switch(pid
= fork()) {
1229 die("fork failed\n");
1232 setsid(); /* create a new process group */
1233 dup2(s
, STDIN_FILENO
);
1234 dup2(s
, STDOUT_FILENO
);
1235 dup2(s
, STDERR_FILENO
);
1236 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1237 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1245 signal(SIGCHLD
, sigchld
);
1247 iofd
= (!strcmp(opt_io
, "-")) ?
1249 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1251 fprintf(stderr
, "Error opening %s:%s\n",
1252 opt_io
, strerror(errno
));
1262 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1264 fprintf(stderr
, "\n");
1269 static char buf
[BUFSIZ
];
1270 static int buflen
= 0;
1273 int charsize
; /* size of utf8 char in bytes */
1277 /* append read bytes to unprocessed bytes */
1278 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1279 die("Couldn't read from shell: %s\n", SERRNO
);
1281 /* process every complete utf8 char */
1284 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1285 charsize
= utf8decode(ptr
, &utf8c
);
1286 utf8encode(&utf8c
, s
);
1292 /* keep any uncomplete utf8 char for the next call */
1293 memmove(buf
, ptr
, buflen
);
1297 ttywrite(const char *s
, size_t n
) {
1298 if(write(cmdfd
, s
, n
) == -1)
1299 die("write error on tty: %s\n", SERRNO
);
1303 ttysend(char *s
, size_t n
) {
1305 if(IS_SET(MODE_ECHO
))
1313 w
.ws_row
= term
.row
;
1314 w
.ws_col
= term
.col
;
1315 w
.ws_xpixel
= xw
.tw
;
1316 w
.ws_ypixel
= xw
.th
;
1317 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1318 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1322 tattrset(int attr
) {
1325 for(i
= 0; i
< term
.row
-1; i
++) {
1326 for(j
= 0; j
< term
.col
-1; j
++) {
1327 if(term
.line
[i
][j
].mode
& attr
)
1336 tsetdirt(int top
, int bot
) {
1339 LIMIT(top
, 0, term
.row
-1);
1340 LIMIT(bot
, 0, term
.row
-1);
1342 for(i
= top
; i
<= bot
; i
++)
1347 tsetdirtattr(int attr
) {
1350 for(i
= 0; i
< term
.row
-1; i
++) {
1351 for(j
= 0; j
< term
.col
-1; j
++) {
1352 if(term
.line
[i
][j
].mode
& attr
) {
1362 tsetdirt(0, term
.row
-1);
1367 static TCursor c
[2];
1368 bool alt
= IS_SET(MODE_ALTSCREEN
);
1370 if(mode
== CURSOR_SAVE
) {
1372 } else if(mode
== CURSOR_LOAD
) {
1374 tmoveto(c
[alt
].x
, c
[alt
].y
);
1382 term
.c
= (TCursor
){{
1386 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1388 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1389 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1392 term
.bot
= term
.row
- 1;
1393 term
.mode
= MODE_WRAP
;
1394 memset(term
.trantbl
, sizeof(term
.trantbl
), CS_USA
);
1397 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1399 tcursor(CURSOR_SAVE
);
1403 tnew(int col
, int row
) {
1404 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1413 Line
*tmp
= term
.line
;
1415 term
.line
= term
.alt
;
1417 term
.mode
^= MODE_ALTSCREEN
;
1422 tscrolldown(int orig
, int n
) {
1426 LIMIT(n
, 0, term
.bot
-orig
+1);
1428 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1430 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1431 temp
= term
.line
[i
];
1432 term
.line
[i
] = term
.line
[i
-n
];
1433 term
.line
[i
-n
] = temp
;
1436 term
.dirty
[i
-n
] = 1;
1443 tscrollup(int orig
, int n
) {
1446 LIMIT(n
, 0, term
.bot
-orig
+1);
1448 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1450 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1451 temp
= term
.line
[i
];
1452 term
.line
[i
] = term
.line
[i
+n
];
1453 term
.line
[i
+n
] = temp
;
1456 term
.dirty
[i
+n
] = 1;
1459 selscroll(orig
, -n
);
1463 selscroll(int orig
, int n
) {
1467 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1468 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1472 if(sel
.type
== SEL_RECTANGULAR
) {
1473 if(sel
.ob
.y
< term
.top
)
1474 sel
.ob
.y
= term
.top
;
1475 if(sel
.oe
.y
> term
.bot
)
1476 sel
.oe
.y
= term
.bot
;
1478 if(sel
.ob
.y
< term
.top
) {
1479 sel
.ob
.y
= term
.top
;
1482 if(sel
.oe
.y
> term
.bot
) {
1483 sel
.oe
.y
= term
.bot
;
1484 sel
.oe
.x
= term
.col
;
1492 tnewline(int first_col
) {
1496 tscrollup(term
.top
, 1);
1500 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1505 char *p
= csiescseq
.buf
, *np
;
1514 csiescseq
.buf
[csiescseq
.len
] = '\0';
1515 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1517 v
= strtol(p
, &np
, 10);
1520 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1522 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1524 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1528 csiescseq
.mode
= *p
;
1531 /* for absolute user moves, when decom is set */
1533 tmoveato(int x
, int y
) {
1534 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1538 tmoveto(int x
, int y
) {
1541 if(term
.c
.state
& CURSOR_ORIGIN
) {
1546 maxy
= term
.row
- 1;
1548 LIMIT(x
, 0, term
.col
-1);
1549 LIMIT(y
, miny
, maxy
);
1550 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1556 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1557 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1558 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1559 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1560 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1561 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1562 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1563 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1564 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1565 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1569 * The table is proudly stolen from rxvt.
1571 if(attr
->mode
& ATTR_GFX
) {
1572 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1573 && vt100_0
[c
[0] - 0x41]) {
1574 c
= vt100_0
[c
[0] - 0x41];
1578 if(term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1579 if(x
+1 < term
.col
) {
1580 term
.line
[y
][x
+1].c
[0] = ' ';
1581 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1583 } else if(term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1584 term
.line
[y
][x
-1].c
[0] = ' ';
1585 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1589 term
.line
[y
][x
] = *attr
;
1590 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1594 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1598 temp
= x1
, x1
= x2
, x2
= temp
;
1600 temp
= y1
, y1
= y2
, y2
= temp
;
1602 LIMIT(x1
, 0, term
.col
-1);
1603 LIMIT(x2
, 0, term
.col
-1);
1604 LIMIT(y1
, 0, term
.row
-1);
1605 LIMIT(y2
, 0, term
.row
-1);
1607 for(y
= y1
; y
<= y2
; y
++) {
1609 for(x
= x1
; x
<= x2
; x
++) {
1612 term
.line
[y
][x
] = term
.c
.attr
;
1613 memcpy(term
.line
[y
][x
].c
, " ", 2);
1619 tdeletechar(int n
) {
1620 int src
= term
.c
.x
+ n
;
1622 int size
= term
.col
- src
;
1624 term
.dirty
[term
.c
.y
] = 1;
1626 if(src
>= term
.col
) {
1627 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1631 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1632 size
* sizeof(Glyph
));
1633 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1637 tinsertblank(int n
) {
1640 int size
= term
.col
- dst
;
1642 term
.dirty
[term
.c
.y
] = 1;
1644 if(dst
>= term
.col
) {
1645 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1649 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1650 size
* sizeof(Glyph
));
1651 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1655 tinsertblankline(int n
) {
1656 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1659 tscrolldown(term
.c
.y
, n
);
1663 tdeleteline(int n
) {
1664 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1667 tscrollup(term
.c
.y
, n
);
1671 tdefcolor(int *attr
, int *npar
, int l
) {
1675 switch (attr
[*npar
+ 1]) {
1676 case 2: /* direct colour in RGB space */
1677 if (*npar
+ 4 >= l
) {
1679 "erresc(38): Incorrect number of parameters (%d)\n",
1683 r
= attr
[*npar
+ 2];
1684 g
= attr
[*npar
+ 3];
1685 b
= attr
[*npar
+ 4];
1687 if(!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1688 fprintf(stderr
, "erresc: bad rgb color (%d,%d,%d)\n",
1691 idx
= TRUECOLOR(r
, g
, b
);
1693 case 5: /* indexed colour */
1694 if (*npar
+ 2 >= l
) {
1696 "erresc(38): Incorrect number of parameters (%d)\n",
1701 if(!BETWEEN(attr
[*npar
], 0, 255))
1702 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1706 case 0: /* implemented defined (only foreground) */
1707 case 1: /* transparent */
1708 case 3: /* direct colour in CMY space */
1709 case 4: /* direct colour in CMYK space */
1712 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1719 tsetattr(int *attr
, int l
) {
1723 for(i
= 0; i
< l
; i
++) {
1726 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1727 | ATTR_BOLD
| ATTR_ITALIC \
1729 term
.c
.attr
.fg
= defaultfg
;
1730 term
.c
.attr
.bg
= defaultbg
;
1733 term
.c
.attr
.mode
|= ATTR_BOLD
;
1736 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1739 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1741 case 5: /* slow blink */
1742 case 6: /* rapid blink */
1743 term
.c
.attr
.mode
|= ATTR_BLINK
;
1746 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1750 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1753 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1756 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1760 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1763 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1766 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1767 term
.c
.attr
.fg
= idx
;
1770 term
.c
.attr
.fg
= defaultfg
;
1773 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1774 term
.c
.attr
.bg
= idx
;
1777 term
.c
.attr
.bg
= defaultbg
;
1780 if(BETWEEN(attr
[i
], 30, 37)) {
1781 term
.c
.attr
.fg
= attr
[i
] - 30;
1782 } else if(BETWEEN(attr
[i
], 40, 47)) {
1783 term
.c
.attr
.bg
= attr
[i
] - 40;
1784 } else if(BETWEEN(attr
[i
], 90, 97)) {
1785 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1786 } else if(BETWEEN(attr
[i
], 100, 107)) {
1787 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1790 "erresc(default): gfx attr %d unknown\n",
1791 attr
[i
]), csidump();
1799 tsetscroll(int t
, int b
) {
1802 LIMIT(t
, 0, term
.row
-1);
1803 LIMIT(b
, 0, term
.row
-1);
1813 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1816 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1820 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1824 case 1: /* DECCKM -- Cursor key */
1825 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1827 case 5: /* DECSCNM -- Reverse video */
1829 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1830 if(mode
!= term
.mode
)
1831 redraw(REDRAW_TIMEOUT
);
1833 case 6: /* DECOM -- Origin */
1834 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1837 case 7: /* DECAWM -- Auto wrap */
1838 MODBIT(term
.mode
, set
, MODE_WRAP
);
1840 case 0: /* Error (IGNORED) */
1841 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1842 case 3: /* DECCOLM -- Column (IGNORED) */
1843 case 4: /* DECSCLM -- Scroll (IGNORED) */
1844 case 8: /* DECARM -- Auto repeat (IGNORED) */
1845 case 18: /* DECPFF -- Printer feed (IGNORED) */
1846 case 19: /* DECPEX -- Printer extent (IGNORED) */
1847 case 42: /* DECNRCM -- National characters (IGNORED) */
1848 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1850 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1851 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1853 case 9: /* X10 mouse compatibility mode */
1854 xsetpointermotion(0);
1855 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1856 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1858 case 1000: /* 1000: report button press */
1859 xsetpointermotion(0);
1860 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1861 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1863 case 1002: /* 1002: report motion on button press */
1864 xsetpointermotion(0);
1865 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1866 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1868 case 1003: /* 1003: enable all mouse motions */
1869 xsetpointermotion(set
);
1870 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1871 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1873 case 1004: /* 1004: send focus events to tty */
1874 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1876 case 1006: /* 1006: extended reporting mode */
1877 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1880 MODBIT(term
.mode
, set
, MODE_8BIT
);
1882 case 1049: /* swap screen & set/restore cursor as xterm */
1883 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1884 case 47: /* swap screen */
1886 if (!allowaltscreen
)
1888 alt
= IS_SET(MODE_ALTSCREEN
);
1890 tclearregion(0, 0, term
.col
-1,
1893 if(set
^ alt
) /* set is always 1 or 0 */
1899 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1901 case 2004: /* 2004: bracketed paste mode */
1902 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
1904 /* Not implemented mouse modes. See comments there. */
1905 case 1001: /* mouse highlight mode; can hang the
1906 terminal by design when implemented. */
1907 case 1005: /* UTF-8 mouse mode; will confuse
1908 applications not supporting UTF-8
1910 case 1015: /* urxvt mangled mouse mode; incompatible
1911 and can be mistaken for other control
1915 "erresc: unknown private set/reset mode %d\n",
1921 case 0: /* Error (IGNORED) */
1923 case 2: /* KAM -- keyboard action */
1924 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1926 case 4: /* IRM -- Insertion-replacement */
1927 MODBIT(term
.mode
, set
, MODE_INSERT
);
1929 case 12: /* SRM -- Send/Receive */
1930 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1932 case 20: /* LNM -- Linefeed/new line */
1933 MODBIT(term
.mode
, set
, MODE_CRLF
);
1937 "erresc: unknown set/reset mode %d\n",
1950 switch(csiescseq
.mode
) {
1953 fprintf(stderr
, "erresc: unknown csi ");
1957 case '@': /* ICH -- Insert <n> blank char */
1958 DEFAULT(csiescseq
.arg
[0], 1);
1959 tinsertblank(csiescseq
.arg
[0]);
1961 case 'A': /* CUU -- Cursor <n> Up */
1962 DEFAULT(csiescseq
.arg
[0], 1);
1963 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1965 case 'B': /* CUD -- Cursor <n> Down */
1966 case 'e': /* VPR --Cursor <n> Down */
1967 DEFAULT(csiescseq
.arg
[0], 1);
1968 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1970 case 'c': /* DA -- Device Attributes */
1971 if(csiescseq
.arg
[0] == 0)
1972 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1974 case 'C': /* CUF -- Cursor <n> Forward */
1975 case 'a': /* HPR -- Cursor <n> Forward */
1976 DEFAULT(csiescseq
.arg
[0], 1);
1977 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1979 case 'D': /* CUB -- Cursor <n> Backward */
1980 DEFAULT(csiescseq
.arg
[0], 1);
1981 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1983 case 'E': /* CNL -- Cursor <n> Down and first col */
1984 DEFAULT(csiescseq
.arg
[0], 1);
1985 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1987 case 'F': /* CPL -- Cursor <n> Up and first col */
1988 DEFAULT(csiescseq
.arg
[0], 1);
1989 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1991 case 'g': /* TBC -- Tabulation clear */
1992 switch(csiescseq
.arg
[0]) {
1993 case 0: /* clear current tab stop */
1994 term
.tabs
[term
.c
.x
] = 0;
1996 case 3: /* clear all the tabs */
1997 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2003 case 'G': /* CHA -- Move to <col> */
2005 DEFAULT(csiescseq
.arg
[0], 1);
2006 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2008 case 'H': /* CUP -- Move to <row> <col> */
2010 DEFAULT(csiescseq
.arg
[0], 1);
2011 DEFAULT(csiescseq
.arg
[1], 1);
2012 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2014 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2015 DEFAULT(csiescseq
.arg
[0], 1);
2016 while(csiescseq
.arg
[0]--)
2019 case 'J': /* ED -- Clear screen */
2021 switch(csiescseq
.arg
[0]) {
2023 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2024 if(term
.c
.y
< term
.row
-1) {
2025 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2031 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2032 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2035 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2041 case 'K': /* EL -- Clear line */
2042 switch(csiescseq
.arg
[0]) {
2044 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2048 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2051 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2055 case 'S': /* SU -- Scroll <n> line up */
2056 DEFAULT(csiescseq
.arg
[0], 1);
2057 tscrollup(term
.top
, csiescseq
.arg
[0]);
2059 case 'T': /* SD -- Scroll <n> line down */
2060 DEFAULT(csiescseq
.arg
[0], 1);
2061 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2063 case 'L': /* IL -- Insert <n> blank lines */
2064 DEFAULT(csiescseq
.arg
[0], 1);
2065 tinsertblankline(csiescseq
.arg
[0]);
2067 case 'l': /* RM -- Reset Mode */
2068 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2070 case 'M': /* DL -- Delete <n> lines */
2071 DEFAULT(csiescseq
.arg
[0], 1);
2072 tdeleteline(csiescseq
.arg
[0]);
2074 case 'X': /* ECH -- Erase <n> char */
2075 DEFAULT(csiescseq
.arg
[0], 1);
2076 tclearregion(term
.c
.x
, term
.c
.y
,
2077 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2079 case 'P': /* DCH -- Delete <n> char */
2080 DEFAULT(csiescseq
.arg
[0], 1);
2081 tdeletechar(csiescseq
.arg
[0]);
2083 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2084 DEFAULT(csiescseq
.arg
[0], 1);
2085 while(csiescseq
.arg
[0]--)
2088 case 'd': /* VPA -- Move to <row> */
2089 DEFAULT(csiescseq
.arg
[0], 1);
2090 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2092 case 'h': /* SM -- Set terminal mode */
2093 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2095 case 'm': /* SGR -- Terminal attribute (color) */
2096 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2098 case 'n': /* DSR – Device Status Report (cursor position) */
2099 if (csiescseq
.arg
[0] == 6) {
2100 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2101 term
.c
.y
+1, term
.c
.x
+1);
2105 case 'r': /* DECSTBM -- Set Scrolling Region */
2106 if(csiescseq
.priv
) {
2109 DEFAULT(csiescseq
.arg
[0], 1);
2110 DEFAULT(csiescseq
.arg
[1], term
.row
);
2111 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2115 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2116 tcursor(CURSOR_SAVE
);
2118 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2119 tcursor(CURSOR_LOAD
);
2130 for(i
= 0; i
< csiescseq
.len
; i
++) {
2131 c
= csiescseq
.buf
[i
] & 0xff;
2134 } else if(c
== '\n') {
2136 } else if(c
== '\r') {
2138 } else if(c
== 0x1b) {
2141 printf("(%02x)", c
);
2149 memset(&csiescseq
, 0, sizeof(csiescseq
));
2158 narg
= strescseq
.narg
;
2160 switch(strescseq
.type
) {
2161 case ']': /* OSC -- Operating System Command */
2162 switch(i
= atoi(strescseq
.args
[0])) {
2167 xsettitle(strescseq
.args
[1]);
2169 case 4: /* color set */
2172 p
= strescseq
.args
[2];
2174 case 104: /* color reset, here p = NULL */
2175 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2176 if (!xsetcolorname(j
, p
)) {
2177 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2180 * TODO if defaultbg color is changed, borders
2187 fprintf(stderr
, "erresc: unknown str ");
2192 case 'k': /* old title set compatibility */
2193 xsettitle(strescseq
.args
[0]);
2195 case 'P': /* DSC -- Device Control String */
2196 case '_': /* APC -- Application Program Command */
2197 case '^': /* PM -- Privacy Message */
2199 fprintf(stderr
, "erresc: unknown str ");
2208 char *p
= strescseq
.buf
;
2211 strescseq
.buf
[strescseq
.len
] = '\0';
2212 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2213 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2221 printf("ESC%c", strescseq
.type
);
2222 for(i
= 0; i
< strescseq
.len
; i
++) {
2223 c
= strescseq
.buf
[i
] & 0xff;
2226 } else if(isprint(c
)) {
2228 } else if(c
== '\n') {
2230 } else if(c
== '\r') {
2232 } else if(c
== 0x1b) {
2235 printf("(%02x)", c
);
2243 memset(&strescseq
, 0, sizeof(strescseq
));
2247 tputtab(bool forward
) {
2253 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2258 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2261 tmoveto(x
, term
.c
.y
);
2265 techo(char *buf
, int len
) {
2266 for(; len
> 0; buf
++, len
--) {
2269 if(c
== '\033') { /* escape */
2272 } else if(c
< '\x20') { /* control code */
2273 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2287 tdeftran(char ascii
) {
2289 static char tbl
[][2] = {
2290 {'0', CS_GRAPHIC0
}, {'1', CS_GRAPHIC1
}, {'A', CS_UK
},
2291 {'B', CS_USA
}, {'<', CS_MULTI
}, {'K', CS_GER
},
2292 {'5', CS_FIN
}, {'C', CS_FIN
},
2296 for (bp
= &tbl
[0]; (c
= (*bp
)[0]) && c
!= ascii
; ++bp
)
2300 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2302 term
.trantbl
[term
.icharset
] = (*bp
)[1];
2307 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
)
2308 term
.c
.attr
.mode
|= ATTR_GFX
;
2310 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2314 tputc(char *c
, int len
) {
2316 bool control
= ascii
< '\x20' || ascii
== 0177;
2323 utf8decode(c
, &u8char
);
2324 width
= wcwidth(u8char
);
2328 if(xwrite(iofd
, c
, len
) < 0) {
2329 fprintf(stderr
, "Error writing in %s:%s\n",
2330 opt_io
, strerror(errno
));
2337 * STR sequences must be checked before anything else
2338 * because it can use some control codes as part of the sequence.
2340 if(term
.esc
& ESC_STR
) {
2343 term
.esc
= ESC_START
| ESC_STR_END
;
2345 case '\a': /* backwards compatibility to xterm */
2350 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2351 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2352 strescseq
.len
+= len
;
2355 * Here is a bug in terminals. If the user never sends
2356 * some code to stop the str or esc command, then st
2357 * will stop responding. But this is better than
2358 * silently failing with unknown characters. At least
2359 * then users will report back.
2361 * In the case users ever get fixed, here is the code:
2373 * Actions of control codes must be performed as soon they arrive
2374 * because they can be embedded inside a control sequence, and
2375 * they must not cause conflicts with sequences.
2383 tmoveto(term
.c
.x
-1, term
.c
.y
);
2386 tmoveto(0, term
.c
.y
);
2391 /* go to first col if the mode is set */
2392 tnewline(IS_SET(MODE_CRLF
));
2394 case '\a': /* BEL */
2395 if(!(xw
.state
& WIN_FOCUSED
))
2398 XBell(xw
.dpy
, bellvolume
);
2400 case '\033': /* ESC */
2402 term
.esc
= ESC_START
;
2404 case '\016': /* SO */
2408 case '\017': /* SI */
2412 case '\032': /* SUB */
2413 case '\030': /* CAN */
2416 case '\005': /* ENQ (IGNORED) */
2417 case '\000': /* NUL (IGNORED) */
2418 case '\021': /* XON (IGNORED) */
2419 case '\023': /* XOFF (IGNORED) */
2420 case 0177: /* DEL (IGNORED) */
2423 } else if(term
.esc
& ESC_START
) {
2424 if(term
.esc
& ESC_CSI
) {
2425 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2426 if(BETWEEN(ascii
, 0x40, 0x7E)
2427 || csiescseq
.len
>= \
2428 sizeof(csiescseq
.buf
)-1) {
2433 } else if(term
.esc
& ESC_STR_END
) {
2437 } else if(term
.esc
& ESC_ALTCHARSET
) {
2441 } else if(term
.esc
& ESC_TEST
) {
2442 if(ascii
== '8') { /* DEC screen alignment test. */
2443 char E
[UTF_SIZ
] = "E";
2446 for(x
= 0; x
< term
.col
; ++x
) {
2447 for(y
= 0; y
< term
.row
; ++y
)
2448 tsetchar(E
, &term
.c
.attr
, x
, y
);
2455 term
.esc
|= ESC_CSI
;
2458 term
.esc
|= ESC_TEST
;
2460 case 'P': /* DCS -- Device Control String */
2461 case '_': /* APC -- Application Program Command */
2462 case '^': /* PM -- Privacy Message */
2463 case ']': /* OSC -- Operating System Command */
2464 case 'k': /* old title set compatibility */
2466 strescseq
.type
= ascii
;
2467 term
.esc
|= ESC_STR
;
2469 case '(': /* set primary charset G0 */
2470 case ')': /* set secondary charset G1 */
2471 case '*': /* set tertiary charset G2 */
2472 case '+': /* set quaternary charset G3 */
2473 term
.icharset
= ascii
- '(';
2474 term
.esc
|= ESC_ALTCHARSET
;
2476 case 'D': /* IND -- Linefeed */
2477 if(term
.c
.y
== term
.bot
) {
2478 tscrollup(term
.top
, 1);
2480 tmoveto(term
.c
.x
, term
.c
.y
+1);
2484 case 'E': /* NEL -- Next line */
2485 tnewline(1); /* always go to first col */
2488 case 'H': /* HTS -- Horizontal tab stop */
2489 term
.tabs
[term
.c
.x
] = 1;
2492 case 'M': /* RI -- Reverse index */
2493 if(term
.c
.y
== term
.top
) {
2494 tscrolldown(term
.top
, 1);
2496 tmoveto(term
.c
.x
, term
.c
.y
-1);
2500 case 'Z': /* DECID -- Identify Terminal */
2501 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2504 case 'c': /* RIS -- Reset to inital state */
2510 case '=': /* DECPAM -- Application keypad */
2511 term
.mode
|= MODE_APPKEYPAD
;
2514 case '>': /* DECPNM -- Normal keypad */
2515 term
.mode
&= ~MODE_APPKEYPAD
;
2518 case '7': /* DECSC -- Save Cursor */
2519 tcursor(CURSOR_SAVE
);
2522 case '8': /* DECRC -- Restore Cursor */
2523 tcursor(CURSOR_LOAD
);
2526 case '\\': /* ST -- Stop */
2530 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2531 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2536 * All characters which form part of a sequence are not
2542 * Display control codes only if we are in graphic mode
2544 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2546 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2548 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2549 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2553 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2554 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2555 &term
.line
[term
.c
.y
][term
.c
.x
],
2556 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2559 if(term
.c
.x
+width
> term
.col
)
2562 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2565 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WIDE
;
2566 if(term
.c
.x
+1 < term
.col
) {
2567 term
.line
[term
.c
.y
][term
.c
.x
+1].c
[0] = '\0';
2568 term
.line
[term
.c
.y
][term
.c
.x
+1].mode
= ATTR_WDUMMY
;
2571 if(term
.c
.x
+width
< term
.col
) {
2572 tmoveto(term
.c
.x
+width
, term
.c
.y
);
2574 term
.c
.state
|= CURSOR_WRAPNEXT
;
2579 tresize(int col
, int row
) {
2581 int minrow
= MIN(row
, term
.row
);
2582 int mincol
= MIN(col
, term
.col
);
2583 int slide
= term
.c
.y
- row
+ 1;
2587 if(col
< 1 || row
< 1)
2590 /* free unneeded rows */
2594 * slide screen to keep cursor where we expect it -
2595 * tscrollup would work here, but we can optimize to
2596 * memmove because we're freeing the earlier lines
2598 for(/* i = 0 */; i
< slide
; i
++) {
2602 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2603 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2605 for(i
+= row
; i
< term
.row
; i
++) {
2610 /* resize to new height */
2611 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2612 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2613 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2614 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2616 /* resize each row to new width, zero-pad if needed */
2617 for(i
= 0; i
< minrow
; i
++) {
2619 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2620 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2623 /* allocate any new rows */
2624 for(/* i == minrow */; i
< row
; i
++) {
2626 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
2627 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
2629 if(col
> term
.col
) {
2630 bp
= term
.tabs
+ term
.col
;
2632 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2633 while(--bp
> term
.tabs
&& !*bp
)
2635 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2638 /* update terminal size */
2641 /* reset scrolling region */
2642 tsetscroll(0, row
-1);
2643 /* make use of the LIMIT in tmoveto */
2644 tmoveto(term
.c
.x
, term
.c
.y
);
2645 /* Clearing both screens */
2648 if(mincol
< col
&& 0 < minrow
) {
2649 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2651 if(0 < col
&& minrow
< row
) {
2652 tclearregion(0, minrow
, col
- 1, row
- 1);
2655 } while(orig
!= term
.line
);
2661 xresize(int col
, int row
) {
2662 xw
.tw
= MAX(1, col
* xw
.cw
);
2663 xw
.th
= MAX(1, row
* xw
.ch
);
2665 XFreePixmap(xw
.dpy
, xw
.buf
);
2666 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2667 DefaultDepth(xw
.dpy
, xw
.scr
));
2668 XftDrawChange(xw
.draw
, xw
.buf
);
2669 xclear(0, 0, xw
.w
, xw
.h
);
2672 static inline ushort
2673 sixd_to_16bit(int x
) {
2674 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2680 XRenderColor color
= { .alpha
= 0xffff };
2685 for (cp
= dc
.col
; cp
< dc
.col
+ LEN(dc
.col
); ++cp
)
2686 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
2689 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2690 for(i
= 0; i
< LEN(colorname
); i
++) {
2693 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2694 die("Could not allocate color '%s'\n", colorname
[i
]);
2698 /* load colors [16-255] ; same colors as xterm */
2699 for(i
= 16, r
= 0; r
< 6; r
++) {
2700 for(g
= 0; g
< 6; g
++) {
2701 for(b
= 0; b
< 6; b
++) {
2702 color
.red
= sixd_to_16bit(r
);
2703 color
.green
= sixd_to_16bit(g
);
2704 color
.blue
= sixd_to_16bit(b
);
2705 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2706 die("Could not allocate color %d\n", i
);
2713 for(r
= 0; r
< 24; r
++, i
++) {
2714 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2715 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2717 die("Could not allocate color %d\n", i
);
2724 xsetcolorname(int x
, const char *name
) {
2725 XRenderColor color
= { .alpha
= 0xffff };
2727 if (x
< 0 || x
> LEN(colorname
))
2730 if(16 <= x
&& x
< 16 + 216) {
2731 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2732 color
.red
= sixd_to_16bit(r
);
2733 color
.green
= sixd_to_16bit(g
);
2734 color
.blue
= sixd_to_16bit(b
);
2735 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2736 return 0; /* something went wrong */
2739 } else if (16 + 216 <= x
&& x
< 256) {
2740 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2741 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2742 return 0; /* something went wrong */
2746 name
= colorname
[x
];
2749 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2756 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2757 XftDrawRect(xw
.draw
,
2758 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2759 borderpx
+ col1
* xw
.cw
,
2760 borderpx
+ row1
* xw
.ch
,
2761 (col2
-col1
+1) * xw
.cw
,
2762 (row2
-row1
+1) * xw
.ch
);
2766 * Absolute coordinates.
2769 xclear(int x1
, int y1
, int x2
, int y2
) {
2770 XftDrawRect(xw
.draw
,
2771 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2772 x1
, y1
, x2
-x1
, y2
-y1
);
2777 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2778 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2779 XSizeHints
*sizeh
= NULL
;
2781 sizeh
= XAllocSizeHints();
2782 if(xw
.isfixed
== False
) {
2783 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2784 sizeh
->height
= xw
.h
;
2785 sizeh
->width
= xw
.w
;
2786 sizeh
->height_inc
= xw
.ch
;
2787 sizeh
->width_inc
= xw
.cw
;
2788 sizeh
->base_height
= 2 * borderpx
;
2789 sizeh
->base_width
= 2 * borderpx
;
2791 sizeh
->flags
= PMaxSize
| PMinSize
;
2792 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2793 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2796 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2801 xloadfont(Font
*f
, FcPattern
*pattern
) {
2805 match
= FcFontMatch(NULL
, pattern
, &result
);
2809 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2810 FcPatternDestroy(match
);
2815 f
->pattern
= FcPatternDuplicate(pattern
);
2817 f
->ascent
= f
->match
->ascent
;
2818 f
->descent
= f
->match
->descent
;
2820 f
->rbearing
= f
->match
->max_advance_width
;
2822 f
->height
= f
->ascent
+ f
->descent
;
2823 f
->width
= f
->lbearing
+ f
->rbearing
;
2829 xloadfonts(char *fontstr
, int fontsize
) {
2834 if(fontstr
[0] == '-') {
2835 pattern
= XftXlfdParse(fontstr
, False
, False
);
2837 pattern
= FcNameParse((FcChar8
*)fontstr
);
2841 die("st: can't open font %s\n", fontstr
);
2844 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2845 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2846 usedfontsize
= fontsize
;
2848 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2849 if(result
== FcResultMatch
) {
2850 usedfontsize
= (int)fontval
;
2853 * Default font size is 12, if none given. This is to
2854 * have a known usedfontsize value.
2856 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2861 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2862 FcDefaultSubstitute(pattern
);
2864 if(xloadfont(&dc
.font
, pattern
))
2865 die("st: can't open font %s\n", fontstr
);
2867 /* Setting character width and height. */
2868 xw
.cw
= CEIL(dc
.font
.width
* cwscale
);
2869 xw
.ch
= CEIL(dc
.font
.height
* chscale
);
2871 FcPatternDel(pattern
, FC_SLANT
);
2872 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2873 if(xloadfont(&dc
.ifont
, pattern
))
2874 die("st: can't open font %s\n", fontstr
);
2876 FcPatternDel(pattern
, FC_WEIGHT
);
2877 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2878 if(xloadfont(&dc
.ibfont
, pattern
))
2879 die("st: can't open font %s\n", fontstr
);
2881 FcPatternDel(pattern
, FC_SLANT
);
2882 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2883 if(xloadfont(&dc
.bfont
, pattern
))
2884 die("st: can't open font %s\n", fontstr
);
2886 FcPatternDestroy(pattern
);
2890 xloadfontset(Font
*f
) {
2893 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2899 xunloadfont(Font
*f
) {
2900 XftFontClose(xw
.dpy
, f
->match
);
2901 FcPatternDestroy(f
->pattern
);
2903 FcFontSetDestroy(f
->set
);
2907 xunloadfonts(void) {
2910 /* Free the loaded fonts in the font cache. */
2911 for(i
= 0; i
< frclen
; i
++) {
2912 XftFontClose(xw
.dpy
, frc
[i
].font
);
2916 xunloadfont(&dc
.font
);
2917 xunloadfont(&dc
.bfont
);
2918 xunloadfont(&dc
.ifont
);
2919 xunloadfont(&dc
.ibfont
);
2923 xzoom(const Arg
*arg
) {
2925 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2936 pid_t thispid
= getpid();
2938 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2939 die("Can't open display\n");
2940 xw
.scr
= XDefaultScreen(xw
.dpy
);
2941 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2945 die("Could not init fontconfig.\n");
2947 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2948 xloadfonts(usedfont
, 0);
2951 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2954 /* adjust fixed window geometry */
2956 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2957 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2959 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2961 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2966 /* window - default size */
2967 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2968 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2974 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2975 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2976 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2977 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2978 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2979 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2980 xw
.attrs
.colormap
= xw
.cmap
;
2982 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2983 XRootWindow(xw
.dpy
, xw
.scr
);
2984 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2985 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2986 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2987 | CWEventMask
| CWColormap
, &xw
.attrs
);
2989 memset(&gcvalues
, 0, sizeof(gcvalues
));
2990 gcvalues
.graphics_exposures
= False
;
2991 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2993 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2994 DefaultDepth(xw
.dpy
, xw
.scr
));
2995 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2996 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2998 /* Xft rendering context */
2999 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3002 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3003 XSetLocaleModifiers("@im=local");
3004 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3005 XSetLocaleModifiers("@im=");
3006 if((xw
.xim
= XOpenIM(xw
.dpy
,
3007 NULL
, NULL
, NULL
)) == NULL
) {
3008 die("XOpenIM failed. Could not open input"
3013 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3014 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3015 XNFocusWindow
, xw
.win
, NULL
);
3017 die("XCreateIC failed. Could not obtain input method.\n");
3019 /* white cursor, black outline */
3020 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
3021 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3022 XRecolorCursor(xw
.dpy
, cursor
,
3023 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
3024 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
3026 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3027 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3028 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3029 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3031 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3032 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3033 PropModeReplace
, (unsigned char *)&thispid
, 1);
3036 XMapWindow(xw
.dpy
, xw
.win
);
3042 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
3043 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3044 width
= charlen
* xw
.cw
, xp
, i
;
3046 int u8fl
, u8fblen
, u8cblen
, doesexist
;
3049 Font
*font
= &dc
.font
;
3051 FcPattern
*fcpattern
, *fontpattern
;
3052 FcFontSet
*fcsets
[] = { NULL
};
3053 FcCharSet
*fccharset
;
3054 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3055 XRenderColor colfg
, colbg
;
3059 frcflags
= FRC_NORMAL
;
3061 if(base
.mode
& ATTR_ITALIC
) {
3062 if(base
.fg
== defaultfg
)
3063 base
.fg
= defaultitalic
;
3065 frcflags
= FRC_ITALIC
;
3066 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
3067 if(base
.fg
== defaultfg
)
3068 base
.fg
= defaultitalic
;
3070 frcflags
= FRC_ITALICBOLD
;
3071 } else if(base
.mode
& ATTR_UNDERLINE
) {
3072 if(base
.fg
== defaultfg
)
3073 base
.fg
= defaultunderline
;
3075 if(IS_TRUECOL(base
.fg
)) {
3076 colfg
.red
= TRUERED(base
.fg
);
3077 colfg
.green
= TRUEGREEN(base
.fg
);
3078 colfg
.blue
= TRUEBLUE(base
.fg
);
3079 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3082 fg
= &dc
.col
[base
.fg
];
3085 if(IS_TRUECOL(base
.bg
)) {
3086 colbg
.green
= TRUEGREEN(base
.bg
);
3087 colbg
.red
= TRUERED(base
.bg
);
3088 colbg
.blue
= TRUEBLUE(base
.bg
);
3089 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3092 bg
= &dc
.col
[base
.bg
];
3097 if(base
.mode
& ATTR_BOLD
) {
3098 if(BETWEEN(base
.fg
, 0, 7)) {
3099 /* basic system colors */
3100 fg
= &dc
.col
[base
.fg
+ 8];
3101 } else if(BETWEEN(base
.fg
, 16, 195)) {
3103 fg
= &dc
.col
[base
.fg
+ 36];
3104 } else if(BETWEEN(base
.fg
, 232, 251)) {
3106 fg
= &dc
.col
[base
.fg
+ 4];
3109 * Those ranges will not be brightened:
3110 * 8 - 15 – bright system colors
3111 * 196 - 231 – highest 256 color cube
3112 * 252 - 255 – brightest colors in greyscale
3115 frcflags
= FRC_BOLD
;
3118 if(IS_SET(MODE_REVERSE
)) {
3119 if(fg
== &dc
.col
[defaultfg
]) {
3120 fg
= &dc
.col
[defaultbg
];
3122 colfg
.red
= ~fg
->color
.red
;
3123 colfg
.green
= ~fg
->color
.green
;
3124 colfg
.blue
= ~fg
->color
.blue
;
3125 colfg
.alpha
= fg
->color
.alpha
;
3126 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3130 if(bg
== &dc
.col
[defaultbg
]) {
3131 bg
= &dc
.col
[defaultfg
];
3133 colbg
.red
= ~bg
->color
.red
;
3134 colbg
.green
= ~bg
->color
.green
;
3135 colbg
.blue
= ~bg
->color
.blue
;
3136 colbg
.alpha
= bg
->color
.alpha
;
3137 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3142 if(base
.mode
& ATTR_REVERSE
) {
3148 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3151 /* Intelligent cleaning up of the borders. */
3153 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3154 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3156 if(x
+ charlen
>= term
.col
) {
3157 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3158 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3161 xclear(winx
, 0, winx
+ width
, borderpx
);
3163 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3165 /* Clean up the region we want to draw to. */
3166 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3168 /* Set the clip region because Xft is sometimes dirty. */
3173 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3175 for(xp
= winx
; bytelen
> 0;) {
3177 * Search for the range in the to be printed string of glyphs
3178 * that are in the main font. Then print that range. If
3179 * some glyph is found that is not in the font, do the
3185 oneatatime
= font
->width
!= xw
.cw
;
3188 u8cblen
= utf8decode(s
, &u8char
);
3192 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3193 if(oneatatime
|| !doesexist
|| bytelen
<= 0) {
3194 if(oneatatime
|| bytelen
<= 0) {
3202 XftDrawStringUtf8(xw
.draw
, fg
,
3204 winy
+ font
->ascent
,
3222 /* Search the font cache. */
3223 for(i
= 0; i
< frclen
; i
++) {
3224 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3225 && frc
[i
].flags
== frcflags
) {
3230 /* Nothing was found. */
3234 fcsets
[0] = font
->set
;
3237 * Nothing was found in the cache. Now use
3238 * some dozen of Fontconfig calls to get the
3239 * font for one single character.
3241 fcpattern
= FcPatternDuplicate(font
->pattern
);
3242 fccharset
= FcCharSetCreate();
3244 FcCharSetAddChar(fccharset
, u8char
);
3245 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3247 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3250 FcConfigSubstitute(0, fcpattern
,
3252 FcDefaultSubstitute(fcpattern
);
3254 fontpattern
= FcFontSetMatch(0, fcsets
,
3255 FcTrue
, fcpattern
, &fcres
);
3258 * Overwrite or create the new cache entry.
3260 if(frclen
>= LEN(frc
)) {
3261 frclen
= LEN(frc
) - 1;
3262 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3265 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3267 frc
[frclen
].flags
= frcflags
;
3272 FcPatternDestroy(fcpattern
);
3273 FcCharSetDestroy(fccharset
);
3276 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3277 xp
, winy
+ frc
[i
].font
->ascent
,
3278 (FcChar8
*)u8c
, u8cblen
);
3280 xp
+= xw
.cw
* wcwidth(u8char
);
3284 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3285 winy + font->ascent, (FcChar8 *)s, bytelen);
3288 if(base
.mode
& ATTR_UNDERLINE
) {
3289 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3293 /* Reset clip to none. */
3294 XftDrawSetClip(xw
.draw
, 0);
3299 static int oldx
= 0, oldy
= 0;
3300 int sl
, width
, curx
;
3301 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3303 LIMIT(oldx
, 0, term
.col
-1);
3304 LIMIT(oldy
, 0, term
.row
-1);
3308 /* adjust position if in dummy */
3309 if(term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3311 if(term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3314 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3316 /* remove the old cursor */
3317 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3318 width
= (term
.line
[oldy
][oldx
].mode
& ATTR_WIDE
)? 2 : 1;
3319 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3322 /* draw the new one */
3323 if(!(IS_SET(MODE_HIDE
))) {
3324 if(xw
.state
& WIN_FOCUSED
) {
3325 if(IS_SET(MODE_REVERSE
)) {
3326 g
.mode
|= ATTR_REVERSE
;
3332 width
= (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
)\
3334 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, width
, sl
);
3336 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3337 borderpx
+ curx
* xw
.cw
,
3338 borderpx
+ term
.c
.y
* xw
.ch
,
3340 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3341 borderpx
+ curx
* xw
.cw
,
3342 borderpx
+ term
.c
.y
* xw
.ch
,
3344 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3345 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3346 borderpx
+ term
.c
.y
* xw
.ch
,
3348 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3349 borderpx
+ curx
* xw
.cw
,
3350 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3353 oldx
= curx
, oldy
= term
.c
.y
;
3359 xsettitle(char *p
) {
3362 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3364 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3365 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3371 xsettitle(opt_title
? opt_title
: "st");
3375 redraw(int timeout
) {
3376 struct timespec tv
= {0, timeout
* 1000};
3382 nanosleep(&tv
, NULL
);
3383 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3389 drawregion(0, 0, term
.col
, term
.row
);
3390 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3392 XSetForeground(xw
.dpy
, dc
.gc
,
3393 dc
.col
[IS_SET(MODE_REVERSE
)?
3394 defaultfg
: defaultbg
].pixel
);
3398 drawregion(int x1
, int y1
, int x2
, int y2
) {
3399 int ic
, ib
, x
, y
, ox
, sl
;
3401 char buf
[DRAW_BUF_SIZ
];
3402 bool ena_sel
= sel
.ob
.x
!= -1;
3405 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3408 if(!(xw
.state
& WIN_VISIBLE
))
3411 for(y
= y1
; y
< y2
; y
++) {
3415 xtermclear(0, y
, term
.col
, y
);
3417 base
= term
.line
[y
][0];
3419 for(x
= x1
; x
< x2
; x
++) {
3420 new = term
.line
[y
][x
];
3421 if(new.mode
== ATTR_WDUMMY
)
3423 if(ena_sel
&& selected(x
, y
))
3424 new.mode
^= ATTR_REVERSE
;
3425 if(ib
> 0 && (ATTRCMP(base
, new)
3426 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3427 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3435 sl
= utf8decode(new.c
, &u8char
);
3436 memcpy(buf
+ib
, new.c
, sl
);
3438 ic
+= (new.mode
& ATTR_WIDE
)? 2 : 1;
3441 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3447 expose(XEvent
*ev
) {
3448 XExposeEvent
*e
= &ev
->xexpose
;
3450 if(xw
.state
& WIN_REDRAW
) {
3452 xw
.state
&= ~WIN_REDRAW
;
3458 visibility(XEvent
*ev
) {
3459 XVisibilityEvent
*e
= &ev
->xvisibility
;
3461 if(e
->state
== VisibilityFullyObscured
) {
3462 xw
.state
&= ~WIN_VISIBLE
;
3463 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3464 /* need a full redraw for next Expose, not just a buf copy */
3465 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3471 xw
.state
&= ~WIN_VISIBLE
;
3475 xsetpointermotion(int set
) {
3476 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3477 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3481 xseturgency(int add
) {
3482 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3484 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3485 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3491 XFocusChangeEvent
*e
= &ev
->xfocus
;
3493 if(e
->mode
== NotifyGrab
)
3496 if(ev
->type
== FocusIn
) {
3497 XSetICFocus(xw
.xic
);
3498 xw
.state
|= WIN_FOCUSED
;
3500 if(IS_SET(MODE_FOCUS
))
3501 ttywrite("\033[I", 3);
3503 XUnsetICFocus(xw
.xic
);
3504 xw
.state
&= ~WIN_FOCUSED
;
3505 if(IS_SET(MODE_FOCUS
))
3506 ttywrite("\033[O", 3);
3511 match(uint mask
, uint state
) {
3512 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
3516 numlock(const Arg
*dummy
) {
3521 kmap(KeySym k
, uint state
) {
3525 /* Check for mapped keys out of X11 function keys. */
3526 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3527 if(mappedkeys
[i
] == k
)
3530 if(i
== LEN(mappedkeys
)) {
3531 if((k
& 0xFFFF) < 0xFD00)
3535 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3539 if(!match(kp
->mask
, state
))
3542 if(IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
3544 if(term
.numlock
&& kp
->appkey
== 2)
3547 if(IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
3550 if(IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
3560 kpress(XEvent
*ev
) {
3561 XKeyEvent
*e
= &ev
->xkey
;
3563 char buf
[32], *customkey
;
3569 if(IS_SET(MODE_KBDLOCK
))
3572 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
3574 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3575 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3576 bp
->func(&(bp
->arg
));
3581 /* 2. custom keys from config.h */
3582 if((customkey
= kmap(ksym
, e
->state
))) {
3583 ttysend(customkey
, strlen(customkey
));
3587 /* 3. composed string from input method */
3590 if(len
== 1 && e
->state
& Mod1Mask
) {
3591 if(IS_SET(MODE_8BIT
)) {
3594 len
= utf8encode(&c
, buf
);
3607 cmessage(XEvent
*e
) {
3610 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3612 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3613 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3614 xw
.state
|= WIN_FOCUSED
;
3616 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3617 xw
.state
&= ~WIN_FOCUSED
;
3619 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3620 /* Send SIGHUP to shell */
3627 cresize(int width
, int height
) {
3635 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3636 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3645 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3648 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3654 int w
= xw
.w
, h
= xw
.h
;
3656 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3657 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3659 /* Waiting for window mapping */
3661 XNextEvent(xw
.dpy
, &ev
);
3662 if(ev
.type
== ConfigureNotify
) {
3663 w
= ev
.xconfigure
.width
;
3664 h
= ev
.xconfigure
.height
;
3665 } else if(ev
.type
== MapNotify
) {
3673 cresize(xw
.fw
, xw
.fh
);
3676 gettimeofday(&lastblink
, NULL
);
3677 gettimeofday(&last
, NULL
);
3679 for(xev
= actionfps
;;) {
3683 FD_SET(cmdfd
, &rfd
);
3686 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3689 die("select failed: %s\n", SERRNO
);
3691 if(FD_ISSET(cmdfd
, &rfd
)) {
3694 blinkset
= tattrset(ATTR_BLINK
);
3696 MODBIT(term
.mode
, 0, MODE_BLINK
);
3700 if(FD_ISSET(xfd
, &rfd
))
3703 gettimeofday(&now
, NULL
);
3704 drawtimeout
.tv_sec
= 0;
3705 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3709 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3710 tsetdirtattr(ATTR_BLINK
);
3711 term
.mode
^= MODE_BLINK
;
3712 gettimeofday(&lastblink
, NULL
);
3715 deltatime
= TIMEDIFF(now
, last
);
3716 if(deltatime
> (xev
? (1000/xfps
) : (1000/actionfps
))
3723 while(XPending(xw
.dpy
)) {
3724 XNextEvent(xw
.dpy
, &ev
);
3725 if(XFilterEvent(&ev
, None
))
3727 if(handler
[ev
.type
])
3728 (handler
[ev
.type
])(&ev
);
3734 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3736 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3738 if(TIMEDIFF(now
, lastblink
) \
3740 drawtimeout
.tv_usec
= 1;
3742 drawtimeout
.tv_usec
= (1000 * \
3757 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3758 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3759 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3763 main(int argc
, char *argv
[]) {
3768 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3773 allowaltscreen
= false;
3776 opt_class
= EARGF(usage());
3779 /* eat all remaining arguments */
3782 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3783 titles
= strdup(argv
[1]);
3784 opt_title
= basename(titles
);
3789 opt_font
= EARGF(usage());
3792 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3797 if(bitm
& WidthValue
)
3799 if(bitm
& HeightValue
)
3801 if(bitm
& XNegative
&& xw
.fx
== 0)
3803 if(bitm
& YNegative
&& xw
.fy
== 0)
3806 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3810 opt_io
= EARGF(usage());
3813 opt_title
= EARGF(usage());
3816 opt_embed
= EARGF(usage());
3824 setlocale(LC_CTYPE
, "");
3825 XSetLocaleModifiers("");