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_PRINT
= 1048576,
138 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
155 ESC_STR
= 4, /* DSC, OSC, PM, APC */
157 ESC_STR_END
= 16, /* a final string was encountered */
158 ESC_TEST
= 32, /* Enter in test mode */
167 enum selection_type
{
172 enum selection_snap
{
177 typedef unsigned char uchar
;
178 typedef unsigned int uint
;
179 typedef unsigned long ulong
;
180 typedef unsigned short ushort
;
183 char c
[UTF_SIZ
]; /* character code */
184 ushort mode
; /* attribute flags */
185 uint32_t fg
; /* foreground */
186 uint32_t bg
; /* background */
192 Glyph attr
; /* current char attributes */
198 /* CSI Escape sequence structs */
199 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
201 char buf
[ESC_BUF_SIZ
]; /* raw string */
202 int len
; /* raw string length */
204 int arg
[ESC_ARG_SIZ
];
205 int narg
; /* nb of args */
209 /* STR Escape sequence structs */
210 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
212 char type
; /* ESC type ... */
213 char buf
[STR_BUF_SIZ
]; /* raw string */
214 int len
; /* raw string length */
215 char *args
[STR_ARG_SIZ
];
216 int narg
; /* nb of args */
219 /* Internal representation of the screen */
221 int row
; /* nb row */
222 int col
; /* nb col */
223 Line
*line
; /* screen */
224 Line
*alt
; /* alternate screen */
225 bool *dirty
; /* dirtyness of lines */
226 TCursor c
; /* cursor */
227 int top
; /* top scroll limit */
228 int bot
; /* bottom scroll limit */
229 int mode
; /* terminal mode flags */
230 int esc
; /* escape state flags */
231 char trantbl
[4]; /* charset table translation */
232 int charset
; /* current charset */
233 int icharset
; /* selected charset for sequence */
234 bool numlock
; /* lock numbers in keyboard */
238 /* Purely graphic info */
244 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
249 XSetWindowAttributes attrs
;
251 bool isfixed
; /* is fixed geometry? */
252 int fx
, fy
, fw
, fh
; /* fixed geometry */
253 int tw
, th
; /* tty width and height */
254 int w
, h
; /* window width and height */
255 int ch
; /* char height */
256 int cw
; /* char width */
257 char state
; /* focus, redraw, visible */
270 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
271 signed char appkey
; /* application keypad */
272 signed char appcursor
; /* application cursor */
273 signed char crlf
; /* crlf mode */
281 * Selection variables:
282 * nb – normalized coordinates of the beginning of the selection
283 * ne – normalized coordinates of the end of the selection
284 * ob – original coordinates of the beginning of the selection
285 * oe – original coordinates of the end of the selection
294 struct timeval tclick1
;
295 struct timeval tclick2
;
308 void (*func
)(const Arg
*);
312 /* function definitions used in config.h */
313 static void clippaste(const Arg
*);
314 static void numlock(const Arg
*);
315 static void selpaste(const Arg
*);
316 static void xzoom(const Arg
*);
318 /* Config.h for applying patches and the configuration. */
334 /* Drawing Context */
336 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
337 Font font
, bfont
, ifont
, ibfont
;
341 static void die(const char *, ...);
342 static void draw(void);
343 static void redraw(int);
344 static void drawregion(int, int, int, int);
345 static void execsh(void);
346 static void sigchld(int);
347 static void run(void);
349 static void csidump(void);
350 static void csihandle(void);
351 static void csiparse(void);
352 static void csireset(void);
353 static void strdump(void);
354 static void strhandle(void);
355 static void strparse(void);
356 static void strreset(void);
358 static int tattrset(int);
359 static void tclearregion(int, int, int, int);
360 static void tcursor(int);
361 static void tdeletechar(int);
362 static void tdeleteline(int);
363 static void tinsertblank(int);
364 static void tinsertblankline(int);
365 static void tmoveto(int, int);
366 static void tmoveato(int x
, int y
);
367 static void tnew(int, int);
368 static void tnewline(int);
369 static void tputtab(bool);
370 static void tputc(char *, int);
371 static void treset(void);
372 static int tresize(int, int);
373 static void tscrollup(int, int);
374 static void tscrolldown(int, int);
375 static void tsetattr(int*, int);
376 static void tsetchar(char *, Glyph
*, int, int);
377 static void tsetscroll(int, int);
378 static void tswapscreen(void);
379 static void tsetdirt(int, int);
380 static void tsetdirtattr(int);
381 static void tsetmode(bool, bool, int *, int);
382 static void tfulldirt(void);
383 static void techo(char *, int);
384 static int32_t tdefcolor(int *, int *, int);
385 static void tselcs(void);
386 static void tdeftran(char);
387 static inline bool match(uint
, uint
);
388 static void ttynew(void);
389 static void ttyread(void);
390 static void ttyresize(void);
391 static void ttysend(char *, size_t);
392 static void ttywrite(const char *, size_t);
394 static void xdraws(char *, Glyph
, int, int, int, int);
395 static void xhints(void);
396 static void xclear(int, int, int, int);
397 static void xdrawcursor(void);
398 static void xinit(void);
399 static void xloadcols(void);
400 static int xsetcolorname(int, const char *);
401 static int xloadfont(Font
*, FcPattern
*);
402 static void xloadfonts(char *, double);
403 static int xloadfontset(Font
*);
404 static void xsettitle(char *);
405 static void xresettitle(void);
406 static void xsetpointermotion(int);
407 static void xseturgency(int);
408 static void xsetsel(char*);
409 static void xtermclear(int, int, int, int);
410 static void xunloadfont(Font
*f
);
411 static void xunloadfonts(void);
412 static void xresize(int, int);
414 static void expose(XEvent
*);
415 static void visibility(XEvent
*);
416 static void unmap(XEvent
*);
417 static char *kmap(KeySym
, uint
);
418 static void kpress(XEvent
*);
419 static void cmessage(XEvent
*);
420 static void cresize(int, int);
421 static void resize(XEvent
*);
422 static void focus(XEvent
*);
423 static void brelease(XEvent
*);
424 static void bpress(XEvent
*);
425 static void bmotion(XEvent
*);
426 static void selnotify(XEvent
*);
427 static void selclear(XEvent
*);
428 static void selrequest(XEvent
*);
430 static void selinit(void);
431 static void selsort(void);
432 static inline bool selected(int, int);
433 static void selcopy(void);
434 static void selscroll(int, int);
435 static void selsnap(int, int *, int *, int);
437 static int utf8decode(char *, long *);
438 static int utf8encode(long *, char *);
439 static int utf8size(char *);
440 static int isfullutf8(char *, int);
442 static ssize_t
xwrite(int, char *, size_t);
443 static void *xmalloc(size_t);
444 static void *xrealloc(void *, size_t);
445 static char *xstrdup(char *s
);
447 static void (*handler
[LASTEvent
])(XEvent
*) = {
449 [ClientMessage
] = cmessage
,
450 [ConfigureNotify
] = resize
,
451 [VisibilityNotify
] = visibility
,
452 [UnmapNotify
] = unmap
,
456 [MotionNotify
] = bmotion
,
457 [ButtonPress
] = bpress
,
458 [ButtonRelease
] = brelease
,
459 [SelectionClear
] = selclear
,
460 [SelectionNotify
] = selnotify
,
461 [SelectionRequest
] = selrequest
,
468 static CSIEscape csiescseq
;
469 static STREscape strescseq
;
472 static Selection sel
;
474 static char **opt_cmd
= NULL
;
475 static char *opt_io
= NULL
;
476 static char *opt_title
= NULL
;
477 static char *opt_embed
= NULL
;
478 static char *opt_class
= NULL
;
479 static char *opt_font
= NULL
;
480 static int oldbutton
= 3; /* button event on startup: 3 = release */
482 static char *usedfont
= NULL
;
483 static double usedfontsize
= 0;
485 /* Font Ring Cache */
498 /* Fontcache is an array now. A new font will be appended to the array. */
499 static Fontcache frc
[16];
500 static int frclen
= 0;
503 xwrite(int fd
, char *s
, size_t len
) {
507 ssize_t r
= write(fd
, s
, len
);
517 xmalloc(size_t len
) {
518 void *p
= malloc(len
);
521 die("Out of memory\n");
527 xrealloc(void *p
, size_t len
) {
528 if((p
= realloc(p
, len
)) == NULL
)
529 die("Out of memory\n");
539 die("Out of memory\n");
545 utf8decode(char *s
, long *u
) {
551 if(~c
& 0x80) { /* 0xxxxxxx */
554 } else if((c
& 0xE0) == 0xC0) { /* 110xxxxx */
557 } else if((c
& 0xF0) == 0xE0) { /* 1110xxxx */
560 } else if((c
& 0xF8) == 0xF0) { /* 11110xxx */
567 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
569 if((c
& 0xC0) != 0x80) /* 10xxxxxx */
575 if((n
== 1 && *u
< 0x80) ||
576 (n
== 2 && *u
< 0x800) ||
577 (n
== 3 && *u
< 0x10000) ||
578 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
590 utf8encode(long *u
, char *s
) {
598 *sp
= uc
; /* 0xxxxxxx */
600 } else if(*u
< 0x800) {
601 *sp
= (uc
>> 6) | 0xC0; /* 110xxxxx */
603 } else if(uc
< 0x10000) {
604 *sp
= (uc
>> 12) | 0xE0; /* 1110xxxx */
606 } else if(uc
<= 0x10FFFF) {
607 *sp
= (uc
>> 18) | 0xF0; /* 11110xxx */
613 for(i
=n
,++sp
; i
>0; --i
,++sp
)
614 *sp
= ((uc
>> 6*(i
-1)) & 0x3F) | 0x80; /* 10xxxxxx */
626 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
627 UTF-8 otherwise return 0 */
629 isfullutf8(char *s
, int b
) {
637 } else if((*c1
& 0xE0) == 0xC0 && b
== 1) {
639 } else if((*c1
& 0xF0) == 0xE0 &&
641 ((b
== 2) && (*c2
& 0xC0) == 0x80))) {
643 } else if((*c1
& 0xF8) == 0xF0 &&
645 ((b
== 2) && (*c2
& 0xC0) == 0x80) ||
646 ((b
== 3) && (*c2
& 0xC0) == 0x80 && (*c3
& 0xC0) == 0x80))) {
659 } else if((c
& 0xE0) == 0xC0) {
661 } else if((c
& 0xF0) == 0xE0) {
670 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
671 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
675 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
676 if(sel
.xtarget
== None
)
677 sel
.xtarget
= XA_STRING
;
685 return LIMIT(x
, 0, term
.col
-1);
693 return LIMIT(y
, 0, term
.row
-1);
698 if(sel
.ob
.y
== sel
.oe
.y
) {
699 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
700 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
702 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
703 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
705 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
706 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
710 selected(int x
, int y
) {
711 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
712 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
714 if(sel
.type
== SEL_RECTANGULAR
) {
715 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
716 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
719 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
720 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
721 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
722 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
726 selsnap(int mode
, int *x
, int *y
, int direction
) {
732 * Snap around if the word wraps around at the end or
733 * beginning of a line.
736 if(direction
< 0 && *x
<= 0) {
737 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
745 if(direction
> 0 && *x
>= term
.col
-1) {
746 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
755 if(term
.line
[*y
][*x
+direction
].mode
& ATTR_WDUMMY
) {
760 if(strchr(worddelimiters
,
761 term
.line
[*y
][*x
+direction
].c
[0])) {
770 * Snap around if the the previous line or the current one
771 * has set ATTR_WRAP at its end. Then the whole next or
772 * previous line will be selected.
774 *x
= (direction
< 0) ? 0 : term
.col
- 1;
775 if(direction
< 0 && *y
> 0) {
776 for(; *y
> 0; *y
+= direction
) {
777 if(!(term
.line
[*y
-1][term
.col
-1].mode
782 } else if(direction
> 0 && *y
< term
.row
-1) {
783 for(; *y
< term
.row
; *y
+= direction
) {
784 if(!(term
.line
[*y
][term
.col
-1].mode
793 * Select the whole line when the end of line is reached.
797 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
807 getbuttoninfo(XEvent
*e
) {
809 uint state
= e
->xbutton
.state
&~Button1Mask
;
811 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
813 sel
.oe
.x
= x2col(e
->xbutton
.x
);
814 sel
.oe
.y
= y2row(e
->xbutton
.y
);
816 if(sel
.ob
.y
< sel
.oe
.y
817 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
818 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
819 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
821 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
822 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
826 sel
.type
= SEL_REGULAR
;
827 for(type
= 1; type
< LEN(selmasks
); ++type
) {
828 if(match(selmasks
[type
], state
)) {
836 mousereport(XEvent
*e
) {
837 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
838 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
844 if(e
->xbutton
.type
== MotionNotify
) {
845 if(x
== ox
&& y
== oy
)
847 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
849 /* MOUSE_MOTION: no reporting if no button is pressed */
850 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
853 button
= oldbutton
+ 32;
857 if(!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
864 if(e
->xbutton
.type
== ButtonPress
) {
868 } else if(e
->xbutton
.type
== ButtonRelease
) {
870 /* MODE_MOUSEX10: no button release reporting */
871 if(IS_SET(MODE_MOUSEX10
))
876 if(!IS_SET(MODE_MOUSEX10
)) {
877 button
+= (state
& ShiftMask
? 4 : 0)
878 + (state
& Mod4Mask
? 8 : 0)
879 + (state
& ControlMask
? 16 : 0);
883 if(IS_SET(MODE_MOUSESGR
)) {
884 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
886 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
887 } else if(x
< 223 && y
< 223) {
888 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
889 32+button
, 32+x
+1, 32+y
+1);
902 if(IS_SET(MODE_MOUSE
)) {
907 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
908 if(e
->xbutton
.button
== mk
->b
909 && match(mk
->mask
, e
->xbutton
.state
)) {
910 ttysend(mk
->s
, strlen(mk
->s
));
915 if(e
->xbutton
.button
== Button1
) {
916 gettimeofday(&now
, NULL
);
918 /* Clear previous selection, logically and visually. */
921 sel
.type
= SEL_REGULAR
;
922 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
923 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
926 * If the user clicks below predefined timeouts specific
927 * snapping behaviour is exposed.
929 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
930 sel
.snap
= SNAP_LINE
;
931 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
932 sel
.snap
= SNAP_WORD
;
936 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
937 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
941 * Draw selection, unless it's regular and we don't want to
942 * make clicks visible
946 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
948 sel
.tclick2
= sel
.tclick1
;
956 int x
, y
, bufsize
, size
, i
, ex
;
962 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
963 ptr
= str
= xmalloc(bufsize
);
965 /* append every set & selected glyph to the selection */
966 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
967 gp
= &term
.line
[y
][0];
968 last
= &gp
[term
.col
-1];
970 while(last
>= gp
&& !(selected(last
- gp
, y
) &&
971 strcmp(last
->c
, " ") != 0)) {
975 for(x
= 0; gp
<= last
; x
++, ++gp
) {
976 if(!selected(x
, y
) || (gp
->mode
& ATTR_WDUMMY
))
979 size
= utf8size(gp
->c
);
980 memcpy(ptr
, gp
->c
, size
);
985 * Copy and pasting of line endings is inconsistent
986 * in the inconsistent terminal and GUI world.
987 * The best solution seems like to produce '\n' when
988 * something is copied from st and convert '\n' to
989 * '\r', when something to be pasted is received by
991 * FIXME: Fix the computer world.
993 if(y
< sel
.ne
.y
&& x
> 0 && !((gp
-1)->mode
& ATTR_WRAP
))
997 * If the last selected line expands in the selection
998 * after the visible text '\n' is appended.
1002 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
1005 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
1017 selnotify(XEvent
*e
) {
1018 ulong nitems
, ofs
, rem
;
1020 uchar
*data
, *last
, *repl
;
1025 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
1026 False
, AnyPropertyType
, &type
, &format
,
1027 &nitems
, &rem
, &data
)) {
1028 fprintf(stderr
, "Clipboard allocation failed\n");
1033 * As seen in selcopy:
1034 * Line endings are inconsistent in the terminal and GUI world
1035 * copy and pasting. When receiving some selection data,
1036 * replace all '\n' with '\r'.
1037 * FIXME: Fix the computer world.
1040 last
= data
+ nitems
* format
/ 8;
1041 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1045 if(IS_SET(MODE_BRCKTPASTE
))
1046 ttywrite("\033[200~", 6);
1047 ttysend((char *)data
, nitems
* format
/ 8);
1048 if(IS_SET(MODE_BRCKTPASTE
))
1049 ttywrite("\033[201~", 6);
1051 /* number of 32-bit chunks returned */
1052 ofs
+= nitems
* format
/ 32;
1057 selpaste(const Arg
*dummy
) {
1058 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1059 xw
.win
, CurrentTime
);
1063 clippaste(const Arg
*dummy
) {
1066 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1067 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1068 xw
.win
, CurrentTime
);
1072 selclear(XEvent
*e
) {
1076 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1080 selrequest(XEvent
*e
) {
1081 XSelectionRequestEvent
*xsre
;
1082 XSelectionEvent xev
;
1083 Atom xa_targets
, string
;
1085 xsre
= (XSelectionRequestEvent
*) e
;
1086 xev
.type
= SelectionNotify
;
1087 xev
.requestor
= xsre
->requestor
;
1088 xev
.selection
= xsre
->selection
;
1089 xev
.target
= xsre
->target
;
1090 xev
.time
= xsre
->time
;
1092 xev
.property
= None
;
1094 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1095 if(xsre
->target
== xa_targets
) {
1096 /* respond with the supported type */
1097 string
= sel
.xtarget
;
1098 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1099 XA_ATOM
, 32, PropModeReplace
,
1100 (uchar
*) &string
, 1);
1101 xev
.property
= xsre
->property
;
1102 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1103 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1104 xsre
->target
, 8, PropModeReplace
,
1105 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1106 xev
.property
= xsre
->property
;
1109 /* all done, send a notification to the listener */
1110 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1111 fprintf(stderr
, "Error sending SelectionNotify event\n");
1115 xsetsel(char *str
) {
1116 /* register the selection for both the clipboard and the primary */
1122 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1124 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1125 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1129 brelease(XEvent
*e
) {
1130 if(IS_SET(MODE_MOUSE
)) {
1135 if(e
->xbutton
.button
== Button2
) {
1137 } else if(e
->xbutton
.button
== Button1
) {
1145 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1150 bmotion(XEvent
*e
) {
1151 int oldey
, oldex
, oldsby
, oldsey
;
1153 if(IS_SET(MODE_MOUSE
)) {
1168 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1169 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1173 die(const char *errstr
, ...) {
1176 va_start(ap
, errstr
);
1177 vfprintf(stderr
, errstr
, ap
);
1185 char *envshell
= getenv("SHELL");
1186 const struct passwd
*pass
= getpwuid(getuid());
1187 char buf
[sizeof(long) * 8 + 1];
1189 unsetenv("COLUMNS");
1191 unsetenv("TERMCAP");
1194 setenv("LOGNAME", pass
->pw_name
, 1);
1195 setenv("USER", pass
->pw_name
, 1);
1196 setenv("SHELL", pass
->pw_shell
, 0);
1197 setenv("HOME", pass
->pw_dir
, 0);
1200 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1201 setenv("WINDOWID", buf
, 1);
1203 signal(SIGCHLD
, SIG_DFL
);
1204 signal(SIGHUP
, SIG_DFL
);
1205 signal(SIGINT
, SIG_DFL
);
1206 signal(SIGQUIT
, SIG_DFL
);
1207 signal(SIGTERM
, SIG_DFL
);
1208 signal(SIGALRM
, SIG_DFL
);
1210 DEFAULT(envshell
, shell
);
1211 setenv("TERM", termname
, 1);
1212 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1213 execvp(args
[0], args
);
1221 if(waitpid(pid
, &stat
, 0) < 0)
1222 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1224 if(WIFEXITED(stat
)) {
1225 exit(WEXITSTATUS(stat
));
1234 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1236 /* seems to work fine on linux, openbsd and freebsd */
1237 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1238 die("openpty failed: %s\n", SERRNO
);
1240 switch(pid
= fork()) {
1242 die("fork failed\n");
1245 setsid(); /* create a new process group */
1246 dup2(s
, STDIN_FILENO
);
1247 dup2(s
, STDOUT_FILENO
);
1248 dup2(s
, STDERR_FILENO
);
1249 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1250 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1258 signal(SIGCHLD
, sigchld
);
1260 term
.mode
|= MODE_PRINT
;
1261 iofd
= (!strcmp(opt_io
, "-")) ?
1263 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1265 fprintf(stderr
, "Error opening %s:%s\n",
1266 opt_io
, strerror(errno
));
1276 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1278 fprintf(stderr
, "\n");
1283 static char buf
[BUFSIZ
];
1284 static int buflen
= 0;
1287 int charsize
; /* size of utf8 char in bytes */
1291 /* append read bytes to unprocessed bytes */
1292 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1293 die("Couldn't read from shell: %s\n", SERRNO
);
1295 /* process every complete utf8 char */
1298 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1299 charsize
= utf8decode(ptr
, &utf8c
);
1300 utf8encode(&utf8c
, s
);
1306 /* keep any uncomplete utf8 char for the next call */
1307 memmove(buf
, ptr
, buflen
);
1311 ttywrite(const char *s
, size_t n
) {
1312 if(write(cmdfd
, s
, n
) == -1)
1313 die("write error on tty: %s\n", SERRNO
);
1317 ttysend(char *s
, size_t n
) {
1319 if(IS_SET(MODE_ECHO
))
1327 w
.ws_row
= term
.row
;
1328 w
.ws_col
= term
.col
;
1329 w
.ws_xpixel
= xw
.tw
;
1330 w
.ws_ypixel
= xw
.th
;
1331 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1332 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1336 tattrset(int attr
) {
1339 for(i
= 0; i
< term
.row
-1; i
++) {
1340 for(j
= 0; j
< term
.col
-1; j
++) {
1341 if(term
.line
[i
][j
].mode
& attr
)
1350 tsetdirt(int top
, int bot
) {
1353 LIMIT(top
, 0, term
.row
-1);
1354 LIMIT(bot
, 0, term
.row
-1);
1356 for(i
= top
; i
<= bot
; i
++)
1361 tsetdirtattr(int attr
) {
1364 for(i
= 0; i
< term
.row
-1; i
++) {
1365 for(j
= 0; j
< term
.col
-1; j
++) {
1366 if(term
.line
[i
][j
].mode
& attr
) {
1376 tsetdirt(0, term
.row
-1);
1381 static TCursor c
[2];
1382 bool alt
= IS_SET(MODE_ALTSCREEN
);
1384 if(mode
== CURSOR_SAVE
) {
1386 } else if(mode
== CURSOR_LOAD
) {
1388 tmoveto(c
[alt
].x
, c
[alt
].y
);
1396 term
.c
= (TCursor
){{
1400 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1402 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1403 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1406 term
.bot
= term
.row
- 1;
1407 term
.mode
= MODE_WRAP
;
1408 memset(term
.trantbl
, sizeof(term
.trantbl
), CS_USA
);
1411 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1413 tcursor(CURSOR_SAVE
);
1417 tnew(int col
, int row
) {
1418 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1427 Line
*tmp
= term
.line
;
1429 term
.line
= term
.alt
;
1431 term
.mode
^= MODE_ALTSCREEN
;
1436 tscrolldown(int orig
, int n
) {
1440 LIMIT(n
, 0, term
.bot
-orig
+1);
1442 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1444 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1445 temp
= term
.line
[i
];
1446 term
.line
[i
] = term
.line
[i
-n
];
1447 term
.line
[i
-n
] = temp
;
1450 term
.dirty
[i
-n
] = 1;
1457 tscrollup(int orig
, int n
) {
1460 LIMIT(n
, 0, term
.bot
-orig
+1);
1462 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1464 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1465 temp
= term
.line
[i
];
1466 term
.line
[i
] = term
.line
[i
+n
];
1467 term
.line
[i
+n
] = temp
;
1470 term
.dirty
[i
+n
] = 1;
1473 selscroll(orig
, -n
);
1477 selscroll(int orig
, int n
) {
1481 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1482 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1486 if(sel
.type
== SEL_RECTANGULAR
) {
1487 if(sel
.ob
.y
< term
.top
)
1488 sel
.ob
.y
= term
.top
;
1489 if(sel
.oe
.y
> term
.bot
)
1490 sel
.oe
.y
= term
.bot
;
1492 if(sel
.ob
.y
< term
.top
) {
1493 sel
.ob
.y
= term
.top
;
1496 if(sel
.oe
.y
> term
.bot
) {
1497 sel
.oe
.y
= term
.bot
;
1498 sel
.oe
.x
= term
.col
;
1506 tnewline(int first_col
) {
1510 tscrollup(term
.top
, 1);
1514 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1519 char *p
= csiescseq
.buf
, *np
;
1528 csiescseq
.buf
[csiescseq
.len
] = '\0';
1529 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1531 v
= strtol(p
, &np
, 10);
1534 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1536 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1538 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1542 csiescseq
.mode
= *p
;
1545 /* for absolute user moves, when decom is set */
1547 tmoveato(int x
, int y
) {
1548 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1552 tmoveto(int x
, int y
) {
1555 if(term
.c
.state
& CURSOR_ORIGIN
) {
1560 maxy
= term
.row
- 1;
1562 LIMIT(x
, 0, term
.col
-1);
1563 LIMIT(y
, miny
, maxy
);
1564 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1570 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1571 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1572 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1573 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1574 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1575 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1576 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1577 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1578 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1579 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1583 * The table is proudly stolen from rxvt.
1585 if(attr
->mode
& ATTR_GFX
) {
1586 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1587 && vt100_0
[c
[0] - 0x41]) {
1588 c
= vt100_0
[c
[0] - 0x41];
1592 if(term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1593 if(x
+1 < term
.col
) {
1594 term
.line
[y
][x
+1].c
[0] = ' ';
1595 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1597 } else if(term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1598 term
.line
[y
][x
-1].c
[0] = ' ';
1599 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1603 term
.line
[y
][x
] = *attr
;
1604 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1608 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1612 temp
= x1
, x1
= x2
, x2
= temp
;
1614 temp
= y1
, y1
= y2
, y2
= temp
;
1616 LIMIT(x1
, 0, term
.col
-1);
1617 LIMIT(x2
, 0, term
.col
-1);
1618 LIMIT(y1
, 0, term
.row
-1);
1619 LIMIT(y2
, 0, term
.row
-1);
1621 for(y
= y1
; y
<= y2
; y
++) {
1623 for(x
= x1
; x
<= x2
; x
++) {
1626 term
.line
[y
][x
] = term
.c
.attr
;
1627 memcpy(term
.line
[y
][x
].c
, " ", 2);
1633 tdeletechar(int n
) {
1634 int src
= term
.c
.x
+ n
;
1636 int size
= term
.col
- src
;
1638 term
.dirty
[term
.c
.y
] = 1;
1640 if(src
>= term
.col
) {
1641 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1645 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1646 size
* sizeof(Glyph
));
1647 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1651 tinsertblank(int n
) {
1654 int size
= term
.col
- dst
;
1656 term
.dirty
[term
.c
.y
] = 1;
1658 if(dst
>= term
.col
) {
1659 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1663 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1664 size
* sizeof(Glyph
));
1665 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1669 tinsertblankline(int n
) {
1670 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1673 tscrolldown(term
.c
.y
, n
);
1677 tdeleteline(int n
) {
1678 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1681 tscrollup(term
.c
.y
, n
);
1685 tdefcolor(int *attr
, int *npar
, int l
) {
1689 switch (attr
[*npar
+ 1]) {
1690 case 2: /* direct colour in RGB space */
1691 if (*npar
+ 4 >= l
) {
1693 "erresc(38): Incorrect number of parameters (%d)\n",
1697 r
= attr
[*npar
+ 2];
1698 g
= attr
[*npar
+ 3];
1699 b
= attr
[*npar
+ 4];
1701 if(!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1702 fprintf(stderr
, "erresc: bad rgb color (%d,%d,%d)\n",
1705 idx
= TRUECOLOR(r
, g
, b
);
1707 case 5: /* indexed colour */
1708 if (*npar
+ 2 >= l
) {
1710 "erresc(38): Incorrect number of parameters (%d)\n",
1715 if(!BETWEEN(attr
[*npar
], 0, 255))
1716 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1720 case 0: /* implemented defined (only foreground) */
1721 case 1: /* transparent */
1722 case 3: /* direct colour in CMY space */
1723 case 4: /* direct colour in CMYK space */
1726 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1733 tsetattr(int *attr
, int l
) {
1737 for(i
= 0; i
< l
; i
++) {
1740 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1741 | ATTR_BOLD
| ATTR_ITALIC \
1743 term
.c
.attr
.fg
= defaultfg
;
1744 term
.c
.attr
.bg
= defaultbg
;
1747 term
.c
.attr
.mode
|= ATTR_BOLD
;
1750 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1753 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1755 case 5: /* slow blink */
1756 case 6: /* rapid blink */
1757 term
.c
.attr
.mode
|= ATTR_BLINK
;
1760 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1764 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1767 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1770 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1774 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1777 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1780 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1781 term
.c
.attr
.fg
= idx
;
1784 term
.c
.attr
.fg
= defaultfg
;
1787 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1788 term
.c
.attr
.bg
= idx
;
1791 term
.c
.attr
.bg
= defaultbg
;
1794 if(BETWEEN(attr
[i
], 30, 37)) {
1795 term
.c
.attr
.fg
= attr
[i
] - 30;
1796 } else if(BETWEEN(attr
[i
], 40, 47)) {
1797 term
.c
.attr
.bg
= attr
[i
] - 40;
1798 } else if(BETWEEN(attr
[i
], 90, 97)) {
1799 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1800 } else if(BETWEEN(attr
[i
], 100, 107)) {
1801 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1804 "erresc(default): gfx attr %d unknown\n",
1805 attr
[i
]), csidump();
1813 tsetscroll(int t
, int b
) {
1816 LIMIT(t
, 0, term
.row
-1);
1817 LIMIT(b
, 0, term
.row
-1);
1827 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1830 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1834 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1838 case 1: /* DECCKM -- Cursor key */
1839 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1841 case 5: /* DECSCNM -- Reverse video */
1843 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1844 if(mode
!= term
.mode
)
1845 redraw(REDRAW_TIMEOUT
);
1847 case 6: /* DECOM -- Origin */
1848 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1851 case 7: /* DECAWM -- Auto wrap */
1852 MODBIT(term
.mode
, set
, MODE_WRAP
);
1854 case 0: /* Error (IGNORED) */
1855 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1856 case 3: /* DECCOLM -- Column (IGNORED) */
1857 case 4: /* DECSCLM -- Scroll (IGNORED) */
1858 case 8: /* DECARM -- Auto repeat (IGNORED) */
1859 case 18: /* DECPFF -- Printer feed (IGNORED) */
1860 case 19: /* DECPEX -- Printer extent (IGNORED) */
1861 case 42: /* DECNRCM -- National characters (IGNORED) */
1862 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1864 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1865 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1867 case 9: /* X10 mouse compatibility mode */
1868 xsetpointermotion(0);
1869 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1870 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1872 case 1000: /* 1000: report button press */
1873 xsetpointermotion(0);
1874 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1875 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1877 case 1002: /* 1002: report motion on button press */
1878 xsetpointermotion(0);
1879 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1880 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1882 case 1003: /* 1003: enable all mouse motions */
1883 xsetpointermotion(set
);
1884 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1885 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1887 case 1004: /* 1004: send focus events to tty */
1888 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1890 case 1006: /* 1006: extended reporting mode */
1891 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1894 MODBIT(term
.mode
, set
, MODE_8BIT
);
1896 case 1049: /* swap screen & set/restore cursor as xterm */
1897 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1898 case 47: /* swap screen */
1900 if (!allowaltscreen
)
1902 alt
= IS_SET(MODE_ALTSCREEN
);
1904 tclearregion(0, 0, term
.col
-1,
1907 if(set
^ alt
) /* set is always 1 or 0 */
1913 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1915 case 2004: /* 2004: bracketed paste mode */
1916 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
1918 /* Not implemented mouse modes. See comments there. */
1919 case 1001: /* mouse highlight mode; can hang the
1920 terminal by design when implemented. */
1921 case 1005: /* UTF-8 mouse mode; will confuse
1922 applications not supporting UTF-8
1924 case 1015: /* urxvt mangled mouse mode; incompatible
1925 and can be mistaken for other control
1929 "erresc: unknown private set/reset mode %d\n",
1935 case 0: /* Error (IGNORED) */
1937 case 2: /* KAM -- keyboard action */
1938 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1940 case 4: /* IRM -- Insertion-replacement */
1941 MODBIT(term
.mode
, set
, MODE_INSERT
);
1943 case 12: /* SRM -- Send/Receive */
1944 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1946 case 20: /* LNM -- Linefeed/new line */
1947 MODBIT(term
.mode
, set
, MODE_CRLF
);
1951 "erresc: unknown set/reset mode %d\n",
1964 switch(csiescseq
.mode
) {
1967 fprintf(stderr
, "erresc: unknown csi ");
1971 case '@': /* ICH -- Insert <n> blank char */
1972 DEFAULT(csiescseq
.arg
[0], 1);
1973 tinsertblank(csiescseq
.arg
[0]);
1975 case 'A': /* CUU -- Cursor <n> Up */
1976 DEFAULT(csiescseq
.arg
[0], 1);
1977 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1979 case 'B': /* CUD -- Cursor <n> Down */
1980 case 'e': /* VPR --Cursor <n> Down */
1981 DEFAULT(csiescseq
.arg
[0], 1);
1982 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1984 case 'i': /* MC -- Media Copy */
1985 switch(csiescseq
.arg
[0]) {
1989 term
.mode
&= ~MODE_PRINT
;
1992 term
.mode
|= MODE_PRINT
;
1996 case 'c': /* DA -- Device Attributes */
1997 if(csiescseq
.arg
[0] == 0)
1998 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2000 case 'C': /* CUF -- Cursor <n> Forward */
2001 case 'a': /* HPR -- Cursor <n> Forward */
2002 DEFAULT(csiescseq
.arg
[0], 1);
2003 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2005 case 'D': /* CUB -- Cursor <n> Backward */
2006 DEFAULT(csiescseq
.arg
[0], 1);
2007 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2009 case 'E': /* CNL -- Cursor <n> Down and first col */
2010 DEFAULT(csiescseq
.arg
[0], 1);
2011 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2013 case 'F': /* CPL -- Cursor <n> Up and first col */
2014 DEFAULT(csiescseq
.arg
[0], 1);
2015 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2017 case 'g': /* TBC -- Tabulation clear */
2018 switch(csiescseq
.arg
[0]) {
2019 case 0: /* clear current tab stop */
2020 term
.tabs
[term
.c
.x
] = 0;
2022 case 3: /* clear all the tabs */
2023 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2029 case 'G': /* CHA -- Move to <col> */
2031 DEFAULT(csiescseq
.arg
[0], 1);
2032 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2034 case 'H': /* CUP -- Move to <row> <col> */
2036 DEFAULT(csiescseq
.arg
[0], 1);
2037 DEFAULT(csiescseq
.arg
[1], 1);
2038 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2040 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2041 DEFAULT(csiescseq
.arg
[0], 1);
2042 while(csiescseq
.arg
[0]--)
2045 case 'J': /* ED -- Clear screen */
2047 switch(csiescseq
.arg
[0]) {
2049 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2050 if(term
.c
.y
< term
.row
-1) {
2051 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2057 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2058 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2061 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2067 case 'K': /* EL -- Clear line */
2068 switch(csiescseq
.arg
[0]) {
2070 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2074 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2077 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2081 case 'S': /* SU -- Scroll <n> line up */
2082 DEFAULT(csiescseq
.arg
[0], 1);
2083 tscrollup(term
.top
, csiescseq
.arg
[0]);
2085 case 'T': /* SD -- Scroll <n> line down */
2086 DEFAULT(csiescseq
.arg
[0], 1);
2087 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2089 case 'L': /* IL -- Insert <n> blank lines */
2090 DEFAULT(csiescseq
.arg
[0], 1);
2091 tinsertblankline(csiescseq
.arg
[0]);
2093 case 'l': /* RM -- Reset Mode */
2094 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2096 case 'M': /* DL -- Delete <n> lines */
2097 DEFAULT(csiescseq
.arg
[0], 1);
2098 tdeleteline(csiescseq
.arg
[0]);
2100 case 'X': /* ECH -- Erase <n> char */
2101 DEFAULT(csiescseq
.arg
[0], 1);
2102 tclearregion(term
.c
.x
, term
.c
.y
,
2103 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2105 case 'P': /* DCH -- Delete <n> char */
2106 DEFAULT(csiescseq
.arg
[0], 1);
2107 tdeletechar(csiescseq
.arg
[0]);
2109 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2110 DEFAULT(csiescseq
.arg
[0], 1);
2111 while(csiescseq
.arg
[0]--)
2114 case 'd': /* VPA -- Move to <row> */
2115 DEFAULT(csiescseq
.arg
[0], 1);
2116 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2118 case 'h': /* SM -- Set terminal mode */
2119 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2121 case 'm': /* SGR -- Terminal attribute (color) */
2122 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2124 case 'n': /* DSR – Device Status Report (cursor position) */
2125 if (csiescseq
.arg
[0] == 6) {
2126 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2127 term
.c
.y
+1, term
.c
.x
+1);
2131 case 'r': /* DECSTBM -- Set Scrolling Region */
2132 if(csiescseq
.priv
) {
2135 DEFAULT(csiescseq
.arg
[0], 1);
2136 DEFAULT(csiescseq
.arg
[1], term
.row
);
2137 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2141 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2142 tcursor(CURSOR_SAVE
);
2144 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2145 tcursor(CURSOR_LOAD
);
2156 for(i
= 0; i
< csiescseq
.len
; i
++) {
2157 c
= csiescseq
.buf
[i
] & 0xff;
2160 } else if(c
== '\n') {
2162 } else if(c
== '\r') {
2164 } else if(c
== 0x1b) {
2167 printf("(%02x)", c
);
2175 memset(&csiescseq
, 0, sizeof(csiescseq
));
2184 narg
= strescseq
.narg
;
2185 par
= atoi(strescseq
.args
[0]);
2187 switch(strescseq
.type
) {
2188 case ']': /* OSC -- Operating System Command */
2194 xsettitle(strescseq
.args
[1]);
2196 case 4: /* color set */
2199 p
= strescseq
.args
[2];
2201 case 104: /* color reset, here p = NULL */
2202 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2203 if (!xsetcolorname(j
, p
)) {
2204 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2207 * TODO if defaultbg color is changed, borders
2215 case 'k': /* old title set compatibility */
2216 xsettitle(strescseq
.args
[0]);
2218 case 'P': /* DSC -- Device Control String */
2219 case '_': /* APC -- Application Program Command */
2220 case '^': /* PM -- Privacy Message */
2224 fprintf(stderr
, "erresc: unknown str ");
2230 char *p
= strescseq
.buf
;
2233 strescseq
.buf
[strescseq
.len
] = '\0';
2234 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2235 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2243 printf("ESC%c", strescseq
.type
);
2244 for(i
= 0; i
< strescseq
.len
; i
++) {
2245 c
= strescseq
.buf
[i
] & 0xff;
2248 } else if(isprint(c
)) {
2250 } else if(c
== '\n') {
2252 } else if(c
== '\r') {
2254 } else if(c
== 0x1b) {
2257 printf("(%02x)", c
);
2265 memset(&strescseq
, 0, sizeof(strescseq
));
2269 tputtab(bool forward
) {
2275 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2280 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2283 tmoveto(x
, term
.c
.y
);
2287 techo(char *buf
, int len
) {
2288 for(; len
> 0; buf
++, len
--) {
2291 if(c
== '\033') { /* escape */
2294 } else if(c
< '\x20') { /* control code */
2295 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2309 tdeftran(char ascii
) {
2311 static char tbl
[][2] = {
2312 {'0', CS_GRAPHIC0
}, {'1', CS_GRAPHIC1
}, {'A', CS_UK
},
2313 {'B', CS_USA
}, {'<', CS_MULTI
}, {'K', CS_GER
},
2314 {'5', CS_FIN
}, {'C', CS_FIN
},
2318 for (bp
= &tbl
[0]; (c
= (*bp
)[0]) && c
!= ascii
; ++bp
)
2322 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2324 term
.trantbl
[term
.icharset
] = (*bp
)[1];
2329 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
)
2330 term
.c
.attr
.mode
|= ATTR_GFX
;
2332 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2336 tputc(char *c
, int len
) {
2338 bool control
= ascii
< '\x20' || ascii
== 0177;
2345 utf8decode(c
, &u8char
);
2346 width
= wcwidth(u8char
);
2349 if(IS_SET(MODE_PRINT
) && iofd
!= -1) {
2350 if(xwrite(iofd
, c
, len
) < 0) {
2351 fprintf(stderr
, "Error writing in %s:%s\n",
2352 opt_io
, strerror(errno
));
2359 * STR sequences must be checked before anything else
2360 * because it can use some control codes as part of the sequence.
2362 if(term
.esc
& ESC_STR
) {
2365 term
.esc
= ESC_START
| ESC_STR_END
;
2367 case '\a': /* backwards compatibility to xterm */
2372 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2373 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2374 strescseq
.len
+= len
;
2377 * Here is a bug in terminals. If the user never sends
2378 * some code to stop the str or esc command, then st
2379 * will stop responding. But this is better than
2380 * silently failing with unknown characters. At least
2381 * then users will report back.
2383 * In the case users ever get fixed, here is the code:
2395 * Actions of control codes must be performed as soon they arrive
2396 * because they can be embedded inside a control sequence, and
2397 * they must not cause conflicts with sequences.
2405 tmoveto(term
.c
.x
-1, term
.c
.y
);
2408 tmoveto(0, term
.c
.y
);
2413 /* go to first col if the mode is set */
2414 tnewline(IS_SET(MODE_CRLF
));
2416 case '\a': /* BEL */
2417 if(!(xw
.state
& WIN_FOCUSED
))
2420 XBell(xw
.dpy
, bellvolume
);
2422 case '\033': /* ESC */
2424 term
.esc
= ESC_START
;
2426 case '\016': /* SO */
2430 case '\017': /* SI */
2434 case '\032': /* SUB */
2435 case '\030': /* CAN */
2438 case '\005': /* ENQ (IGNORED) */
2439 case '\000': /* NUL (IGNORED) */
2440 case '\021': /* XON (IGNORED) */
2441 case '\023': /* XOFF (IGNORED) */
2442 case 0177: /* DEL (IGNORED) */
2445 } else if(term
.esc
& ESC_START
) {
2446 if(term
.esc
& ESC_CSI
) {
2447 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2448 if(BETWEEN(ascii
, 0x40, 0x7E)
2449 || csiescseq
.len
>= \
2450 sizeof(csiescseq
.buf
)-1) {
2455 } else if(term
.esc
& ESC_STR_END
) {
2459 } else if(term
.esc
& ESC_ALTCHARSET
) {
2463 } else if(term
.esc
& ESC_TEST
) {
2464 if(ascii
== '8') { /* DEC screen alignment test. */
2465 char E
[UTF_SIZ
] = "E";
2468 for(x
= 0; x
< term
.col
; ++x
) {
2469 for(y
= 0; y
< term
.row
; ++y
)
2470 tsetchar(E
, &term
.c
.attr
, x
, y
);
2477 term
.esc
|= ESC_CSI
;
2480 term
.esc
|= ESC_TEST
;
2482 case 'P': /* DCS -- Device Control String */
2483 case '_': /* APC -- Application Program Command */
2484 case '^': /* PM -- Privacy Message */
2485 case ']': /* OSC -- Operating System Command */
2486 case 'k': /* old title set compatibility */
2488 strescseq
.type
= ascii
;
2489 term
.esc
|= ESC_STR
;
2491 case '(': /* set primary charset G0 */
2492 case ')': /* set secondary charset G1 */
2493 case '*': /* set tertiary charset G2 */
2494 case '+': /* set quaternary charset G3 */
2495 term
.icharset
= ascii
- '(';
2496 term
.esc
|= ESC_ALTCHARSET
;
2498 case 'D': /* IND -- Linefeed */
2499 if(term
.c
.y
== term
.bot
) {
2500 tscrollup(term
.top
, 1);
2502 tmoveto(term
.c
.x
, term
.c
.y
+1);
2506 case 'E': /* NEL -- Next line */
2507 tnewline(1); /* always go to first col */
2510 case 'H': /* HTS -- Horizontal tab stop */
2511 term
.tabs
[term
.c
.x
] = 1;
2514 case 'M': /* RI -- Reverse index */
2515 if(term
.c
.y
== term
.top
) {
2516 tscrolldown(term
.top
, 1);
2518 tmoveto(term
.c
.x
, term
.c
.y
-1);
2522 case 'Z': /* DECID -- Identify Terminal */
2523 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2526 case 'c': /* RIS -- Reset to inital state */
2532 case '=': /* DECPAM -- Application keypad */
2533 term
.mode
|= MODE_APPKEYPAD
;
2536 case '>': /* DECPNM -- Normal keypad */
2537 term
.mode
&= ~MODE_APPKEYPAD
;
2540 case '7': /* DECSC -- Save Cursor */
2541 tcursor(CURSOR_SAVE
);
2544 case '8': /* DECRC -- Restore Cursor */
2545 tcursor(CURSOR_LOAD
);
2548 case '\\': /* ST -- Stop */
2552 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2553 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2558 * All characters which form part of a sequence are not
2564 * Display control codes only if we are in graphic mode
2566 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2568 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2570 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2571 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2575 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2576 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2577 &term
.line
[term
.c
.y
][term
.c
.x
],
2578 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2581 if(term
.c
.x
+width
> term
.col
)
2584 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2587 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WIDE
;
2588 if(term
.c
.x
+1 < term
.col
) {
2589 term
.line
[term
.c
.y
][term
.c
.x
+1].c
[0] = '\0';
2590 term
.line
[term
.c
.y
][term
.c
.x
+1].mode
= ATTR_WDUMMY
;
2593 if(term
.c
.x
+width
< term
.col
) {
2594 tmoveto(term
.c
.x
+width
, term
.c
.y
);
2596 term
.c
.state
|= CURSOR_WRAPNEXT
;
2601 tresize(int col
, int row
) {
2603 int minrow
= MIN(row
, term
.row
);
2604 int mincol
= MIN(col
, term
.col
);
2605 int slide
= term
.c
.y
- row
+ 1;
2609 if(col
< 1 || row
< 1)
2612 /* free unneeded rows */
2616 * slide screen to keep cursor where we expect it -
2617 * tscrollup would work here, but we can optimize to
2618 * memmove because we're freeing the earlier lines
2620 for(/* i = 0 */; i
< slide
; i
++) {
2624 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2625 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2627 for(i
+= row
; i
< term
.row
; i
++) {
2632 /* resize to new height */
2633 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2634 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2635 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2636 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2638 /* resize each row to new width, zero-pad if needed */
2639 for(i
= 0; i
< minrow
; i
++) {
2641 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2642 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2645 /* allocate any new rows */
2646 for(/* i == minrow */; i
< row
; i
++) {
2648 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
2649 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
2651 if(col
> term
.col
) {
2652 bp
= term
.tabs
+ term
.col
;
2654 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2655 while(--bp
> term
.tabs
&& !*bp
)
2657 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2660 /* update terminal size */
2663 /* reset scrolling region */
2664 tsetscroll(0, row
-1);
2665 /* make use of the LIMIT in tmoveto */
2666 tmoveto(term
.c
.x
, term
.c
.y
);
2667 /* Clearing both screens */
2670 if(mincol
< col
&& 0 < minrow
) {
2671 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2673 if(0 < col
&& minrow
< row
) {
2674 tclearregion(0, minrow
, col
- 1, row
- 1);
2677 } while(orig
!= term
.line
);
2683 xresize(int col
, int row
) {
2684 xw
.tw
= MAX(1, col
* xw
.cw
);
2685 xw
.th
= MAX(1, row
* xw
.ch
);
2687 XFreePixmap(xw
.dpy
, xw
.buf
);
2688 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2689 DefaultDepth(xw
.dpy
, xw
.scr
));
2690 XftDrawChange(xw
.draw
, xw
.buf
);
2691 xclear(0, 0, xw
.w
, xw
.h
);
2694 static inline ushort
2695 sixd_to_16bit(int x
) {
2696 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2702 XRenderColor color
= { .alpha
= 0xffff };
2707 for (cp
= dc
.col
; cp
< dc
.col
+ LEN(dc
.col
); ++cp
)
2708 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
2711 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2712 for(i
= 0; i
< LEN(colorname
); i
++) {
2715 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2716 die("Could not allocate color '%s'\n", colorname
[i
]);
2720 /* load colors [16-255] ; same colors as xterm */
2721 for(i
= 16, r
= 0; r
< 6; r
++) {
2722 for(g
= 0; g
< 6; g
++) {
2723 for(b
= 0; b
< 6; b
++) {
2724 color
.red
= sixd_to_16bit(r
);
2725 color
.green
= sixd_to_16bit(g
);
2726 color
.blue
= sixd_to_16bit(b
);
2727 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2728 die("Could not allocate color %d\n", i
);
2735 for(r
= 0; r
< 24; r
++, i
++) {
2736 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2737 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2739 die("Could not allocate color %d\n", i
);
2746 xsetcolorname(int x
, const char *name
) {
2747 XRenderColor color
= { .alpha
= 0xffff };
2749 if (x
< 0 || x
> LEN(colorname
))
2752 if(16 <= x
&& x
< 16 + 216) {
2753 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2754 color
.red
= sixd_to_16bit(r
);
2755 color
.green
= sixd_to_16bit(g
);
2756 color
.blue
= sixd_to_16bit(b
);
2757 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2758 return 0; /* something went wrong */
2761 } else if (16 + 216 <= x
&& x
< 256) {
2762 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2763 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2764 return 0; /* something went wrong */
2768 name
= colorname
[x
];
2771 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2778 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2779 XftDrawRect(xw
.draw
,
2780 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2781 borderpx
+ col1
* xw
.cw
,
2782 borderpx
+ row1
* xw
.ch
,
2783 (col2
-col1
+1) * xw
.cw
,
2784 (row2
-row1
+1) * xw
.ch
);
2788 * Absolute coordinates.
2791 xclear(int x1
, int y1
, int x2
, int y2
) {
2792 XftDrawRect(xw
.draw
,
2793 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2794 x1
, y1
, x2
-x1
, y2
-y1
);
2799 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2800 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2801 XSizeHints
*sizeh
= NULL
;
2803 sizeh
= XAllocSizeHints();
2804 if(xw
.isfixed
== False
) {
2805 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2806 sizeh
->height
= xw
.h
;
2807 sizeh
->width
= xw
.w
;
2808 sizeh
->height_inc
= xw
.ch
;
2809 sizeh
->width_inc
= xw
.cw
;
2810 sizeh
->base_height
= 2 * borderpx
;
2811 sizeh
->base_width
= 2 * borderpx
;
2813 sizeh
->flags
= PMaxSize
| PMinSize
;
2814 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2815 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2818 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2823 xloadfont(Font
*f
, FcPattern
*pattern
) {
2827 match
= FcFontMatch(NULL
, pattern
, &result
);
2831 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2832 FcPatternDestroy(match
);
2837 f
->pattern
= FcPatternDuplicate(pattern
);
2839 f
->ascent
= f
->match
->ascent
;
2840 f
->descent
= f
->match
->descent
;
2842 f
->rbearing
= f
->match
->max_advance_width
;
2844 f
->height
= f
->ascent
+ f
->descent
;
2845 f
->width
= f
->lbearing
+ f
->rbearing
;
2851 xloadfonts(char *fontstr
, double fontsize
) {
2853 FcResult r_sz
, r_psz
;
2856 if(fontstr
[0] == '-') {
2857 pattern
= XftXlfdParse(fontstr
, False
, False
);
2859 pattern
= FcNameParse((FcChar8
*)fontstr
);
2863 die("st: can't open font %s\n", fontstr
);
2866 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2867 FcPatternDel(pattern
, FC_SIZE
);
2868 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2869 usedfontsize
= fontsize
;
2871 r_psz
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2872 r_sz
= FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
);
2873 if(r_psz
== FcResultMatch
) {
2874 usedfontsize
= fontval
;
2875 } else if(r_sz
== FcResultMatch
) {
2879 * Default font size is 12, if none given. This is to
2880 * have a known usedfontsize value.
2882 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2887 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2888 FcDefaultSubstitute(pattern
);
2890 if(xloadfont(&dc
.font
, pattern
))
2891 die("st: can't open font %s\n", fontstr
);
2893 if(usedfontsize
< 0) {
2894 FcPatternGetDouble(dc
.font
.match
->pattern
,
2895 FC_PIXEL_SIZE
, 0, &fontval
);
2896 usedfontsize
= fontval
;
2899 /* Setting character width and height. */
2900 xw
.cw
= CEIL(dc
.font
.width
* cwscale
);
2901 xw
.ch
= CEIL(dc
.font
.height
* chscale
);
2903 FcPatternDel(pattern
, FC_SLANT
);
2904 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2905 if(xloadfont(&dc
.ifont
, pattern
))
2906 die("st: can't open font %s\n", fontstr
);
2908 FcPatternDel(pattern
, FC_WEIGHT
);
2909 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2910 if(xloadfont(&dc
.ibfont
, pattern
))
2911 die("st: can't open font %s\n", fontstr
);
2913 FcPatternDel(pattern
, FC_SLANT
);
2914 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2915 if(xloadfont(&dc
.bfont
, pattern
))
2916 die("st: can't open font %s\n", fontstr
);
2918 FcPatternDestroy(pattern
);
2922 xloadfontset(Font
*f
) {
2925 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2931 xunloadfont(Font
*f
) {
2932 XftFontClose(xw
.dpy
, f
->match
);
2933 FcPatternDestroy(f
->pattern
);
2935 FcFontSetDestroy(f
->set
);
2939 xunloadfonts(void) {
2942 /* Free the loaded fonts in the font cache. */
2943 for(i
= 0; i
< frclen
; i
++) {
2944 XftFontClose(xw
.dpy
, frc
[i
].font
);
2948 xunloadfont(&dc
.font
);
2949 xunloadfont(&dc
.bfont
);
2950 xunloadfont(&dc
.ifont
);
2951 xunloadfont(&dc
.ibfont
);
2955 xzoom(const Arg
*arg
) {
2957 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2968 pid_t thispid
= getpid();
2970 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2971 die("Can't open display\n");
2972 xw
.scr
= XDefaultScreen(xw
.dpy
);
2973 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2977 die("Could not init fontconfig.\n");
2979 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2980 xloadfonts(usedfont
, 0);
2983 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2986 /* adjust fixed window geometry */
2988 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2989 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2991 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2993 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2998 /* window - default size */
2999 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3000 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3006 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3007 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3008 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3009 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3010 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3011 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3012 xw
.attrs
.colormap
= xw
.cmap
;
3014 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
3015 XRootWindow(xw
.dpy
, xw
.scr
);
3016 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
3017 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3018 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3019 | CWEventMask
| CWColormap
, &xw
.attrs
);
3021 memset(&gcvalues
, 0, sizeof(gcvalues
));
3022 gcvalues
.graphics_exposures
= False
;
3023 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3025 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3026 DefaultDepth(xw
.dpy
, xw
.scr
));
3027 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3028 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3030 /* Xft rendering context */
3031 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3034 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3035 XSetLocaleModifiers("@im=local");
3036 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3037 XSetLocaleModifiers("@im=");
3038 if((xw
.xim
= XOpenIM(xw
.dpy
,
3039 NULL
, NULL
, NULL
)) == NULL
) {
3040 die("XOpenIM failed. Could not open input"
3045 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3046 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3047 XNFocusWindow
, xw
.win
, NULL
);
3049 die("XCreateIC failed. Could not obtain input method.\n");
3051 /* white cursor, black outline */
3052 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
3053 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3054 XRecolorCursor(xw
.dpy
, cursor
,
3055 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
3056 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
3058 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3059 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3060 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3061 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3063 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3064 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3065 PropModeReplace
, (unsigned char *)&thispid
, 1);
3068 XMapWindow(xw
.dpy
, xw
.win
);
3074 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
3075 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3076 width
= charlen
* xw
.cw
, xp
, i
;
3078 int u8fl
, u8fblen
, u8cblen
, doesexist
;
3081 Font
*font
= &dc
.font
;
3083 FcPattern
*fcpattern
, *fontpattern
;
3084 FcFontSet
*fcsets
[] = { NULL
};
3085 FcCharSet
*fccharset
;
3086 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3087 XRenderColor colfg
, colbg
;
3091 frcflags
= FRC_NORMAL
;
3093 if(base
.mode
& ATTR_ITALIC
) {
3094 if(base
.fg
== defaultfg
)
3095 base
.fg
= defaultitalic
;
3097 frcflags
= FRC_ITALIC
;
3098 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
3099 if(base
.fg
== defaultfg
)
3100 base
.fg
= defaultitalic
;
3102 frcflags
= FRC_ITALICBOLD
;
3103 } else if(base
.mode
& ATTR_UNDERLINE
) {
3104 if(base
.fg
== defaultfg
)
3105 base
.fg
= defaultunderline
;
3107 if(IS_TRUECOL(base
.fg
)) {
3108 colfg
.alpha
= 0xffff;
3109 colfg
.red
= TRUERED(base
.fg
);
3110 colfg
.green
= TRUEGREEN(base
.fg
);
3111 colfg
.blue
= TRUEBLUE(base
.fg
);
3112 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3115 fg
= &dc
.col
[base
.fg
];
3118 if(IS_TRUECOL(base
.bg
)) {
3119 colbg
.alpha
= 0xffff;
3120 colbg
.green
= TRUEGREEN(base
.bg
);
3121 colbg
.red
= TRUERED(base
.bg
);
3122 colbg
.blue
= TRUEBLUE(base
.bg
);
3123 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3126 bg
= &dc
.col
[base
.bg
];
3131 if(base
.mode
& ATTR_BOLD
) {
3132 if(BETWEEN(base
.fg
, 0, 7)) {
3133 /* basic system colors */
3134 fg
= &dc
.col
[base
.fg
+ 8];
3135 } else if(BETWEEN(base
.fg
, 16, 195)) {
3137 fg
= &dc
.col
[base
.fg
+ 36];
3138 } else if(BETWEEN(base
.fg
, 232, 251)) {
3140 fg
= &dc
.col
[base
.fg
+ 4];
3143 * Those ranges will not be brightened:
3144 * 8 - 15 – bright system colors
3145 * 196 - 231 – highest 256 color cube
3146 * 252 - 255 – brightest colors in greyscale
3149 frcflags
= FRC_BOLD
;
3152 if(IS_SET(MODE_REVERSE
)) {
3153 if(fg
== &dc
.col
[defaultfg
]) {
3154 fg
= &dc
.col
[defaultbg
];
3156 colfg
.red
= ~fg
->color
.red
;
3157 colfg
.green
= ~fg
->color
.green
;
3158 colfg
.blue
= ~fg
->color
.blue
;
3159 colfg
.alpha
= fg
->color
.alpha
;
3160 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3164 if(bg
== &dc
.col
[defaultbg
]) {
3165 bg
= &dc
.col
[defaultfg
];
3167 colbg
.red
= ~bg
->color
.red
;
3168 colbg
.green
= ~bg
->color
.green
;
3169 colbg
.blue
= ~bg
->color
.blue
;
3170 colbg
.alpha
= bg
->color
.alpha
;
3171 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3176 if(base
.mode
& ATTR_REVERSE
) {
3182 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3185 /* Intelligent cleaning up of the borders. */
3187 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3188 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3190 if(x
+ charlen
>= term
.col
) {
3191 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3192 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3195 xclear(winx
, 0, winx
+ width
, borderpx
);
3197 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3199 /* Clean up the region we want to draw to. */
3200 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3202 /* Set the clip region because Xft is sometimes dirty. */
3207 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3209 for(xp
= winx
; bytelen
> 0;) {
3211 * Search for the range in the to be printed string of glyphs
3212 * that are in the main font. Then print that range. If
3213 * some glyph is found that is not in the font, do the
3219 oneatatime
= font
->width
!= xw
.cw
;
3222 u8cblen
= utf8decode(s
, &u8char
);
3226 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3227 if(oneatatime
|| !doesexist
|| bytelen
<= 0) {
3228 if(oneatatime
|| bytelen
<= 0) {
3236 XftDrawStringUtf8(xw
.draw
, fg
,
3238 winy
+ font
->ascent
,
3256 /* Search the font cache. */
3257 for(i
= 0; i
< frclen
; i
++) {
3258 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3259 && frc
[i
].flags
== frcflags
) {
3264 /* Nothing was found. */
3268 fcsets
[0] = font
->set
;
3271 * Nothing was found in the cache. Now use
3272 * some dozen of Fontconfig calls to get the
3273 * font for one single character.
3275 fcpattern
= FcPatternDuplicate(font
->pattern
);
3276 fccharset
= FcCharSetCreate();
3278 FcCharSetAddChar(fccharset
, u8char
);
3279 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3281 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3284 FcConfigSubstitute(0, fcpattern
,
3286 FcDefaultSubstitute(fcpattern
);
3288 fontpattern
= FcFontSetMatch(0, fcsets
,
3289 FcTrue
, fcpattern
, &fcres
);
3292 * Overwrite or create the new cache entry.
3294 if(frclen
>= LEN(frc
)) {
3295 frclen
= LEN(frc
) - 1;
3296 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3299 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3301 frc
[frclen
].flags
= frcflags
;
3306 FcPatternDestroy(fcpattern
);
3307 FcCharSetDestroy(fccharset
);
3310 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3311 xp
, winy
+ frc
[i
].font
->ascent
,
3312 (FcChar8
*)u8c
, u8cblen
);
3314 xp
+= xw
.cw
* wcwidth(u8char
);
3318 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3319 winy + font->ascent, (FcChar8 *)s, bytelen);
3322 if(base
.mode
& ATTR_UNDERLINE
) {
3323 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3327 /* Reset clip to none. */
3328 XftDrawSetClip(xw
.draw
, 0);
3333 static int oldx
= 0, oldy
= 0;
3334 int sl
, width
, curx
;
3335 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3337 LIMIT(oldx
, 0, term
.col
-1);
3338 LIMIT(oldy
, 0, term
.row
-1);
3342 /* adjust position if in dummy */
3343 if(term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3345 if(term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3348 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3350 /* remove the old cursor */
3351 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3352 width
= (term
.line
[oldy
][oldx
].mode
& ATTR_WIDE
)? 2 : 1;
3353 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3356 /* draw the new one */
3357 if(!(IS_SET(MODE_HIDE
))) {
3358 if(xw
.state
& WIN_FOCUSED
) {
3359 if(IS_SET(MODE_REVERSE
)) {
3360 g
.mode
|= ATTR_REVERSE
;
3366 width
= (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
)\
3368 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, width
, sl
);
3370 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3371 borderpx
+ curx
* xw
.cw
,
3372 borderpx
+ term
.c
.y
* xw
.ch
,
3374 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3375 borderpx
+ curx
* xw
.cw
,
3376 borderpx
+ term
.c
.y
* xw
.ch
,
3378 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3379 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3380 borderpx
+ term
.c
.y
* xw
.ch
,
3382 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3383 borderpx
+ curx
* xw
.cw
,
3384 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3387 oldx
= curx
, oldy
= term
.c
.y
;
3393 xsettitle(char *p
) {
3396 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3398 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3399 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3405 xsettitle(opt_title
? opt_title
: "st");
3409 redraw(int timeout
) {
3410 struct timespec tv
= {0, timeout
* 1000};
3416 nanosleep(&tv
, NULL
);
3417 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3423 drawregion(0, 0, term
.col
, term
.row
);
3424 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3426 XSetForeground(xw
.dpy
, dc
.gc
,
3427 dc
.col
[IS_SET(MODE_REVERSE
)?
3428 defaultfg
: defaultbg
].pixel
);
3432 drawregion(int x1
, int y1
, int x2
, int y2
) {
3433 int ic
, ib
, x
, y
, ox
, sl
;
3435 char buf
[DRAW_BUF_SIZ
];
3436 bool ena_sel
= sel
.ob
.x
!= -1;
3439 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3442 if(!(xw
.state
& WIN_VISIBLE
))
3445 for(y
= y1
; y
< y2
; y
++) {
3449 xtermclear(0, y
, term
.col
, y
);
3451 base
= term
.line
[y
][0];
3453 for(x
= x1
; x
< x2
; x
++) {
3454 new = term
.line
[y
][x
];
3455 if(new.mode
== ATTR_WDUMMY
)
3457 if(ena_sel
&& selected(x
, y
))
3458 new.mode
^= ATTR_REVERSE
;
3459 if(ib
> 0 && (ATTRCMP(base
, new)
3460 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3461 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3469 sl
= utf8decode(new.c
, &u8char
);
3470 memcpy(buf
+ib
, new.c
, sl
);
3472 ic
+= (new.mode
& ATTR_WIDE
)? 2 : 1;
3475 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3481 expose(XEvent
*ev
) {
3482 XExposeEvent
*e
= &ev
->xexpose
;
3484 if(xw
.state
& WIN_REDRAW
) {
3486 xw
.state
&= ~WIN_REDRAW
;
3492 visibility(XEvent
*ev
) {
3493 XVisibilityEvent
*e
= &ev
->xvisibility
;
3495 if(e
->state
== VisibilityFullyObscured
) {
3496 xw
.state
&= ~WIN_VISIBLE
;
3497 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3498 /* need a full redraw for next Expose, not just a buf copy */
3499 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3505 xw
.state
&= ~WIN_VISIBLE
;
3509 xsetpointermotion(int set
) {
3510 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3511 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3515 xseturgency(int add
) {
3516 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3518 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3519 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3525 XFocusChangeEvent
*e
= &ev
->xfocus
;
3527 if(e
->mode
== NotifyGrab
)
3530 if(ev
->type
== FocusIn
) {
3531 XSetICFocus(xw
.xic
);
3532 xw
.state
|= WIN_FOCUSED
;
3534 if(IS_SET(MODE_FOCUS
))
3535 ttywrite("\033[I", 3);
3537 XUnsetICFocus(xw
.xic
);
3538 xw
.state
&= ~WIN_FOCUSED
;
3539 if(IS_SET(MODE_FOCUS
))
3540 ttywrite("\033[O", 3);
3545 match(uint mask
, uint state
) {
3546 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
3550 numlock(const Arg
*dummy
) {
3555 kmap(KeySym k
, uint state
) {
3559 /* Check for mapped keys out of X11 function keys. */
3560 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3561 if(mappedkeys
[i
] == k
)
3564 if(i
== LEN(mappedkeys
)) {
3565 if((k
& 0xFFFF) < 0xFD00)
3569 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3573 if(!match(kp
->mask
, state
))
3576 if(IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
3578 if(term
.numlock
&& kp
->appkey
== 2)
3581 if(IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
3584 if(IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
3594 kpress(XEvent
*ev
) {
3595 XKeyEvent
*e
= &ev
->xkey
;
3597 char buf
[32], *customkey
;
3603 if(IS_SET(MODE_KBDLOCK
))
3606 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
3608 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3609 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3610 bp
->func(&(bp
->arg
));
3615 /* 2. custom keys from config.h */
3616 if((customkey
= kmap(ksym
, e
->state
))) {
3617 ttysend(customkey
, strlen(customkey
));
3621 /* 3. composed string from input method */
3624 if(len
== 1 && e
->state
& Mod1Mask
) {
3625 if(IS_SET(MODE_8BIT
)) {
3628 len
= utf8encode(&c
, buf
);
3641 cmessage(XEvent
*e
) {
3644 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3646 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3647 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3648 xw
.state
|= WIN_FOCUSED
;
3650 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3651 xw
.state
&= ~WIN_FOCUSED
;
3653 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3654 /* Send SIGHUP to shell */
3661 cresize(int width
, int height
) {
3669 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3670 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3679 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3682 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3688 int w
= xw
.w
, h
= xw
.h
;
3690 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3691 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3693 /* Waiting for window mapping */
3695 XNextEvent(xw
.dpy
, &ev
);
3696 if(ev
.type
== ConfigureNotify
) {
3697 w
= ev
.xconfigure
.width
;
3698 h
= ev
.xconfigure
.height
;
3699 } else if(ev
.type
== MapNotify
) {
3708 cresize(xw
.fw
, xw
.fh
);
3710 gettimeofday(&lastblink
, NULL
);
3711 gettimeofday(&last
, NULL
);
3713 for(xev
= actionfps
;;) {
3717 FD_SET(cmdfd
, &rfd
);
3720 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3723 die("select failed: %s\n", SERRNO
);
3725 if(FD_ISSET(cmdfd
, &rfd
)) {
3728 blinkset
= tattrset(ATTR_BLINK
);
3730 MODBIT(term
.mode
, 0, MODE_BLINK
);
3734 if(FD_ISSET(xfd
, &rfd
))
3737 gettimeofday(&now
, NULL
);
3738 drawtimeout
.tv_sec
= 0;
3739 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3743 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3744 tsetdirtattr(ATTR_BLINK
);
3745 term
.mode
^= MODE_BLINK
;
3746 gettimeofday(&lastblink
, NULL
);
3749 deltatime
= TIMEDIFF(now
, last
);
3750 if(deltatime
> (xev
? (1000/xfps
) : (1000/actionfps
))
3757 while(XPending(xw
.dpy
)) {
3758 XNextEvent(xw
.dpy
, &ev
);
3759 if(XFilterEvent(&ev
, None
))
3761 if(handler
[ev
.type
])
3762 (handler
[ev
.type
])(&ev
);
3768 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3770 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3772 if(TIMEDIFF(now
, lastblink
) \
3774 drawtimeout
.tv_usec
= 1;
3776 drawtimeout
.tv_usec
= (1000 * \
3791 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3792 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3793 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3797 main(int argc
, char *argv
[]) {
3802 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3807 allowaltscreen
= false;
3810 opt_class
= EARGF(usage());
3813 /* eat all remaining arguments */
3816 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3817 titles
= xstrdup(argv
[1]);
3818 opt_title
= basename(titles
);
3823 opt_font
= EARGF(usage());
3826 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3831 if(bitm
& WidthValue
)
3833 if(bitm
& HeightValue
)
3835 if(bitm
& XNegative
&& xw
.fx
== 0)
3837 if(bitm
& YNegative
&& xw
.fy
== 0)
3840 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3844 opt_io
= EARGF(usage());
3847 opt_title
= EARGF(usage());
3850 opt_embed
= EARGF(usage());
3858 setlocale(LC_CTYPE
, "");
3859 XSetLocaleModifiers("");