1 /* See LICENSE for licence details. */
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
23 #include <X11/Xatom.h>
25 #include <X11/Xutil.h>
26 #include <X11/cursorfont.h>
27 #include <X11/keysym.h>
28 #include <X11/Xft/Xft.h>
29 #include <fontconfig/fontconfig.h>
37 #define Draw XftDraw *
38 #define Colour XftColor
39 #define Colourmap Colormap
40 #define Rectangle XRectangle
44 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
46 #elif defined(__FreeBSD__) || defined(__DragonFly__)
52 #define XEMBED_FOCUS_IN 4
53 #define XEMBED_FOCUS_OUT 5
57 #define ESC_BUF_SIZ (128*UTF_SIZ)
58 #define ESC_ARG_SIZ 16
59 #define STR_BUF_SIZ ESC_BUF_SIZ
60 #define STR_ARG_SIZ ESC_ARG_SIZ
61 #define DRAW_BUF_SIZ 20*1024
62 #define XK_ANY_MOD UINT_MAX
64 #define XK_SWITCH_MOD (1<<13)
66 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
69 #define SERRNO strerror(errno)
70 #define MIN(a, b) ((a) < (b) ? (a) : (b))
71 #define MAX(a, b) ((a) < (b) ? (b) : (a))
72 #define LEN(a) (sizeof(a) / sizeof(a[0]))
73 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
74 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
75 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
76 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
77 #define IS_SET(flag) ((term.mode & (flag)) != 0)
78 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
80 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
81 #define IS_TRUECOL(x) (1 << 24 & (x))
82 #define TRUERED(x) (((x) & 0xff0000) >> 8)
83 #define TRUEGREEN(x) (((x) & 0xff00))
84 #define TRUEBLUE(x) (((x) & 0xff) << 8)
87 #define VT102ID "\033[?6c"
89 enum glyph_attribute
{
100 enum cursor_movement
{
118 MODE_MOUSEMOTION
= 64,
123 MODE_APPCURSOR
= 2048,
124 MODE_MOUSESGR
= 4096,
129 MODE_MOUSEX10
= 131072,
130 MODE_MOUSEMANY
= 262144,
131 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
138 ESC_STR
= 4, /* DSC, OSC, PM, APC */
140 ESC_STR_END
= 16, /* a final string was encountered */
141 ESC_TEST
= 32, /* Enter in test mode */
150 enum selection_type
{
155 enum selection_snap
{
160 typedef unsigned char uchar
;
161 typedef unsigned int uint
;
162 typedef unsigned long ulong
;
163 typedef unsigned short ushort
;
166 char c
[UTF_SIZ
]; /* character code */
167 uchar mode
; /* attribute flags */
168 ulong fg
; /* foreground */
169 ulong bg
; /* background */
175 Glyph attr
; /* current char attributes */
181 /* CSI Escape sequence structs */
182 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
184 char buf
[ESC_BUF_SIZ
]; /* raw string */
185 int len
; /* raw string length */
187 int arg
[ESC_ARG_SIZ
];
188 int narg
; /* nb of args */
192 /* STR Escape sequence structs */
193 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
195 char type
; /* ESC type ... */
196 char buf
[STR_BUF_SIZ
]; /* raw string */
197 int len
; /* raw string length */
198 char *args
[STR_ARG_SIZ
];
199 int narg
; /* nb of args */
202 /* Internal representation of the screen */
204 int row
; /* nb row */
205 int col
; /* nb col */
206 Line
*line
; /* screen */
207 Line
*alt
; /* alternate screen */
208 bool *dirty
; /* dirtyness of lines */
209 TCursor c
; /* cursor */
210 int top
; /* top scroll limit */
211 int bot
; /* bottom scroll limit */
212 int mode
; /* terminal mode flags */
213 int esc
; /* escape state flags */
214 bool numlock
; /* lock numbers in keyboard */
218 /* Purely graphic info */
224 Atom xembed
, wmdeletewin
;
229 XSetWindowAttributes attrs
;
231 bool isfixed
; /* is fixed geometry? */
232 int fx
, fy
, fw
, fh
; /* fixed geometry */
233 int tw
, th
; /* tty width and height */
234 int w
, h
; /* window width and height */
235 int ch
; /* char height */
236 int cw
; /* char width */
237 char state
; /* focus, redraw, visible */
250 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
251 signed char appkey
; /* application keypad */
252 signed char appcursor
; /* application cursor */
253 signed char crlf
; /* crlf mode */
261 * Selection variables:
262 * nb – normalized coordinates of the beginning of the selection
263 * ne – normalized coordinates of the end of the selection
264 * ob – original coordinates of the beginning of the selection
265 * oe – original coordinates of the end of the selection
274 struct timeval tclick1
;
275 struct timeval tclick2
;
288 void (*func
)(const Arg
*);
292 /* function definitions used in config.h */
293 static void clippaste(const Arg
*);
294 static void numlock(const Arg
*);
295 static void selpaste(const Arg
*);
296 static void xzoom(const Arg
*);
298 /* Config.h for applying patches and the configuration. */
314 /* Drawing Context */
316 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
317 Font font
, bfont
, ifont
, ibfont
;
321 static void die(const char *, ...);
322 static void draw(void);
323 static void redraw(int);
324 static void drawregion(int, int, int, int);
325 static void execsh(void);
326 static void sigchld(int);
327 static void run(void);
329 static void csidump(void);
330 static void csihandle(void);
331 static void csiparse(void);
332 static void csireset(void);
333 static void strdump(void);
334 static void strhandle(void);
335 static void strparse(void);
336 static void strreset(void);
338 static int tattrset(int);
339 static void tclearregion(int, int, int, int);
340 static void tcursor(int);
341 static void tdeletechar(int);
342 static void tdeleteline(int);
343 static void tinsertblank(int);
344 static void tinsertblankline(int);
345 static void tmoveto(int, int);
346 static void tmoveato(int x
, int y
);
347 static void tnew(int, int);
348 static void tnewline(int);
349 static void tputtab(bool);
350 static void tputc(char *, int);
351 static void treset(void);
352 static int tresize(int, int);
353 static void tscrollup(int, int);
354 static void tscrolldown(int, int);
355 static void tsetattr(int*, int);
356 static void tsetchar(char *, Glyph
*, int, int);
357 static void tsetscroll(int, int);
358 static void tswapscreen(void);
359 static void tsetdirt(int, int);
360 static void tsetdirtattr(int);
361 static void tsetmode(bool, bool, int *, int);
362 static void tfulldirt(void);
363 static void techo(char *, int);
364 static ulong
tdefcolor(int *, int *, int);
365 static inline bool match(uint
, uint
);
366 static void ttynew(void);
367 static void ttyread(void);
368 static void ttyresize(void);
369 static void ttywrite(const char *, size_t);
371 static void xdraws(char *, Glyph
, int, int, int, int);
372 static void xhints(void);
373 static void xclear(int, int, int, int);
374 static void xdrawcursor(void);
375 static void xinit(void);
376 static void xloadcols(void);
377 static int xsetcolorname(int, const char *);
378 static int xloadfont(Font
*, FcPattern
*);
379 static void xloadfonts(char *, int);
380 static int xloadfontset(Font
*);
381 static void xsettitle(char *);
382 static void xresettitle(void);
383 static void xsetpointermotion(int);
384 static void xseturgency(int);
385 static void xsetsel(char*);
386 static void xtermclear(int, int, int, int);
387 static void xunloadfont(Font
*f
);
388 static void xunloadfonts(void);
389 static void xresize(int, int);
391 static void expose(XEvent
*);
392 static void visibility(XEvent
*);
393 static void unmap(XEvent
*);
394 static char *kmap(KeySym
, uint
);
395 static void kpress(XEvent
*);
396 static void cmessage(XEvent
*);
397 static void cresize(int, int);
398 static void resize(XEvent
*);
399 static void focus(XEvent
*);
400 static void brelease(XEvent
*);
401 static void bpress(XEvent
*);
402 static void bmotion(XEvent
*);
403 static void selnotify(XEvent
*);
404 static void selclear(XEvent
*);
405 static void selrequest(XEvent
*);
407 static void selinit(void);
408 static void selsort(void);
409 static inline bool selected(int, int);
410 static void selcopy(void);
411 static void selscroll(int, int);
412 static void selsnap(int, int *, int *, int);
414 static int utf8decode(char *, long *);
415 static int utf8encode(long *, char *);
416 static int utf8size(char *);
417 static int isfullutf8(char *, int);
419 static ssize_t
xwrite(int, char *, size_t);
420 static void *xmalloc(size_t);
421 static void *xrealloc(void *, size_t);
422 static void *xcalloc(size_t, size_t);
424 static void (*handler
[LASTEvent
])(XEvent
*) = {
426 [ClientMessage
] = cmessage
,
427 [ConfigureNotify
] = resize
,
428 [VisibilityNotify
] = visibility
,
429 [UnmapNotify
] = unmap
,
433 [MotionNotify
] = bmotion
,
434 [ButtonPress
] = bpress
,
435 [ButtonRelease
] = brelease
,
436 [SelectionClear
] = selclear
,
437 [SelectionNotify
] = selnotify
,
438 [SelectionRequest
] = selrequest
,
445 static CSIEscape csiescseq
;
446 static STREscape strescseq
;
449 static Selection sel
;
450 static int iofd
= -1;
451 static char **opt_cmd
= NULL
;
452 static char *opt_io
= NULL
;
453 static char *opt_title
= NULL
;
454 static char *opt_embed
= NULL
;
455 static char *opt_class
= NULL
;
456 static char *opt_font
= NULL
;
457 static int oldbutton
= 3; /* button event on startup: 3 = release */
459 static char *usedfont
= NULL
;
460 static int usedfontsize
= 0;
462 /* Font Ring Cache */
475 /* Fontcache is an array now. A new font will be appended to the array. */
476 static Fontcache frc
[16];
477 static int frclen
= 0;
480 xwrite(int fd
, char *s
, size_t len
) {
484 ssize_t r
= write(fd
, s
, len
);
494 xmalloc(size_t len
) {
495 void *p
= malloc(len
);
498 die("Out of memory\n");
504 xrealloc(void *p
, size_t len
) {
505 if((p
= realloc(p
, len
)) == NULL
)
506 die("Out of memory\n");
512 xcalloc(size_t nmemb
, size_t size
) {
513 void *p
= calloc(nmemb
, size
);
516 die("Out of memory\n");
522 utf8decode(char *s
, long *u
) {
528 if(~c
& 0x80) { /* 0xxxxxxx */
531 } else if((c
& 0xE0) == 0xC0) { /* 110xxxxx */
534 } else if((c
& 0xF0) == 0xE0) { /* 1110xxxx */
537 } else if((c
& 0xF8) == 0xF0) { /* 11110xxx */
544 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
546 if((c
& 0xC0) != 0x80) /* 10xxxxxx */
552 if((n
== 1 && *u
< 0x80) ||
553 (n
== 2 && *u
< 0x800) ||
554 (n
== 3 && *u
< 0x10000) ||
555 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
567 utf8encode(long *u
, char *s
) {
575 *sp
= uc
; /* 0xxxxxxx */
577 } else if(*u
< 0x800) {
578 *sp
= (uc
>> 6) | 0xC0; /* 110xxxxx */
580 } else if(uc
< 0x10000) {
581 *sp
= (uc
>> 12) | 0xE0; /* 1110xxxx */
583 } else if(uc
<= 0x10FFFF) {
584 *sp
= (uc
>> 18) | 0xF0; /* 11110xxx */
590 for(i
=n
,++sp
; i
>0; --i
,++sp
)
591 *sp
= ((uc
>> 6*(i
-1)) & 0x3F) | 0x80; /* 10xxxxxx */
603 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
604 UTF-8 otherwise return 0 */
606 isfullutf8(char *s
, int b
) {
614 } else if((*c1
& 0xE0) == 0xC0 && b
== 1) {
616 } else if((*c1
& 0xF0) == 0xE0 &&
618 ((b
== 2) && (*c2
& 0xC0) == 0x80))) {
620 } else if((*c1
& 0xF8) == 0xF0 &&
622 ((b
== 2) && (*c2
& 0xC0) == 0x80) ||
623 ((b
== 3) && (*c2
& 0xC0) == 0x80 && (*c3
& 0xC0) == 0x80))) {
636 } else if((c
& 0xE0) == 0xC0) {
638 } else if((c
& 0xF0) == 0xE0) {
647 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
648 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
652 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
653 if(sel
.xtarget
== None
)
654 sel
.xtarget
= XA_STRING
;
662 return LIMIT(x
, 0, term
.col
-1);
670 return LIMIT(y
, 0, term
.row
-1);
675 if(sel
.ob
.y
== sel
.oe
.y
) {
676 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
677 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
679 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
680 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
682 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
683 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
687 selected(int x
, int y
) {
688 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
689 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
691 if(sel
.type
== SEL_RECTANGULAR
) {
692 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
693 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
696 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
697 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
698 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
699 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
703 selsnap(int mode
, int *x
, int *y
, int direction
) {
709 * Snap around if the word wraps around at the end or
710 * beginning of a line.
713 if(direction
< 0 && *x
<= 0) {
714 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
722 if(direction
> 0 && *x
>= term
.col
-1) {
723 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
732 if(strchr(worddelimiters
,
733 term
.line
[*y
][*x
+ direction
].c
[0])) {
742 * Snap around if the the previous line or the current one
743 * has set ATTR_WRAP at its end. Then the whole next or
744 * previous line will be selected.
746 *x
= (direction
< 0) ? 0 : term
.col
- 1;
747 if(direction
< 0 && *y
> 0) {
748 for(; *y
> 0; *y
+= direction
) {
749 if(!(term
.line
[*y
-1][term
.col
-1].mode
754 } else if(direction
> 0 && *y
< term
.row
-1) {
755 for(; *y
< term
.row
; *y
+= direction
) {
756 if(!(term
.line
[*y
][term
.col
-1].mode
765 * Select the whole line when the end of line is reached.
769 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
779 getbuttoninfo(XEvent
*e
) {
781 uint state
= e
->xbutton
.state
&~Button1Mask
;
783 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
785 sel
.oe
.x
= x2col(e
->xbutton
.x
);
786 sel
.oe
.y
= y2row(e
->xbutton
.y
);
788 if(sel
.ob
.y
< sel
.oe
.y
789 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
790 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
791 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
793 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
794 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
798 sel
.type
= SEL_REGULAR
;
799 for(type
= 1; type
< LEN(selmasks
); ++type
) {
800 if(match(selmasks
[type
], state
)) {
808 mousereport(XEvent
*e
) {
809 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
810 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
816 if(e
->xbutton
.type
== MotionNotify
) {
817 if(x
== ox
&& y
== oy
)
819 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
821 /* MOUSE_MOTION: no reporting if no button is pressed */
822 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
825 button
= oldbutton
+ 32;
828 } else if(!IS_SET(MODE_MOUSESGR
)
829 && (e
->xbutton
.type
== ButtonRelease
830 || button
== AnyButton
)) {
836 if(e
->xbutton
.type
== ButtonPress
) {
843 if(!IS_SET(MODE_MOUSEX10
)) {
844 button
+= (state
& ShiftMask
? 4 : 0)
845 + (state
& Mod4Mask
? 8 : 0)
846 + (state
& ControlMask
? 16 : 0);
850 if(IS_SET(MODE_MOUSESGR
)) {
851 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
853 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
854 } else if(x
< 223 && y
< 223) {
855 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
856 IS_SET(MODE_MOUSEX10
)? button
-1 : 32+button
,
870 if(IS_SET(MODE_MOUSE
)) {
875 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
876 if(e
->xbutton
.button
== mk
->b
877 && match(mk
->mask
, e
->xbutton
.state
)) {
878 ttywrite(mk
->s
, strlen(mk
->s
));
879 if(IS_SET(MODE_ECHO
))
880 techo(mk
->s
, strlen(mk
->s
));
885 if(e
->xbutton
.button
== Button1
) {
886 gettimeofday(&now
, NULL
);
888 /* Clear previous selection, logically and visually. */
891 sel
.type
= SEL_REGULAR
;
892 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
893 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
896 * If the user clicks below predefined timeouts specific
897 * snapping behaviour is exposed.
899 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
900 sel
.snap
= SNAP_LINE
;
901 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
902 sel
.snap
= SNAP_WORD
;
906 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
907 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
911 * Draw selection, unless it's regular and we don't want to
912 * make clicks visible
916 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
918 sel
.tclick2
= sel
.tclick1
;
926 int x
, y
, bufsize
, size
, i
, ex
;
932 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
933 ptr
= str
= xmalloc(bufsize
);
935 /* append every set & selected glyph to the selection */
936 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
937 gp
= &term
.line
[y
][0];
938 last
= gp
+ term
.col
;
940 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
941 strcmp(last
->c
, " ") != 0))
944 for(x
= 0; gp
<= last
; x
++, ++gp
) {
948 size
= utf8size(gp
->c
);
949 memcpy(ptr
, gp
->c
, size
);
954 * Copy and pasting of line endings is inconsistent
955 * in the inconsistent terminal and GUI world.
956 * The best solution seems like to produce '\n' when
957 * something is copied from st and convert '\n' to
958 * '\r', when something to be pasted is received by
960 * FIXME: Fix the computer world.
962 if(y
< sel
.ne
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
966 * If the last selected line expands in the selection
967 * after the visible text '\n' is appended.
971 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
974 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
986 selnotify(XEvent
*e
) {
987 ulong nitems
, ofs
, rem
;
989 uchar
*data
, *last
, *repl
;
994 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
995 False
, AnyPropertyType
, &type
, &format
,
996 &nitems
, &rem
, &data
)) {
997 fprintf(stderr
, "Clipboard allocation failed\n");
1002 * As seen in selcopy:
1003 * Line endings are inconsistent in the terminal and GUI world
1004 * copy and pasting. When receiving some selection data,
1005 * replace all '\n' with '\r'.
1006 * FIXME: Fix the computer world.
1009 last
= data
+ nitems
* format
/ 8;
1010 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1014 ttywrite((const char *)data
, nitems
* format
/ 8);
1016 /* number of 32-bit chunks returned */
1017 ofs
+= nitems
* format
/ 32;
1022 selpaste(const Arg
*dummy
) {
1023 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1024 xw
.win
, CurrentTime
);
1028 clippaste(const Arg
*dummy
) {
1031 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1032 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1033 xw
.win
, CurrentTime
);
1037 selclear(XEvent
*e
) {
1041 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1045 selrequest(XEvent
*e
) {
1046 XSelectionRequestEvent
*xsre
;
1047 XSelectionEvent xev
;
1048 Atom xa_targets
, string
;
1050 xsre
= (XSelectionRequestEvent
*) e
;
1051 xev
.type
= SelectionNotify
;
1052 xev
.requestor
= xsre
->requestor
;
1053 xev
.selection
= xsre
->selection
;
1054 xev
.target
= xsre
->target
;
1055 xev
.time
= xsre
->time
;
1057 xev
.property
= None
;
1059 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1060 if(xsre
->target
== xa_targets
) {
1061 /* respond with the supported type */
1062 string
= sel
.xtarget
;
1063 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1064 XA_ATOM
, 32, PropModeReplace
,
1065 (uchar
*) &string
, 1);
1066 xev
.property
= xsre
->property
;
1067 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1068 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1069 xsre
->target
, 8, PropModeReplace
,
1070 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1071 xev
.property
= xsre
->property
;
1074 /* all done, send a notification to the listener */
1075 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1076 fprintf(stderr
, "Error sending SelectionNotify event\n");
1080 xsetsel(char *str
) {
1081 /* register the selection for both the clipboard and the primary */
1087 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1089 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1090 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1094 brelease(XEvent
*e
) {
1095 if(IS_SET(MODE_MOUSE
)) {
1100 if(e
->xbutton
.button
== Button2
) {
1102 } else if(e
->xbutton
.button
== Button1
) {
1110 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1115 bmotion(XEvent
*e
) {
1116 int oldey
, oldex
, oldsby
, oldsey
;
1118 if(IS_SET(MODE_MOUSE
)) {
1133 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1134 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1138 die(const char *errstr
, ...) {
1141 va_start(ap
, errstr
);
1142 vfprintf(stderr
, errstr
, ap
);
1150 char *envshell
= getenv("SHELL");
1151 const struct passwd
*pass
= getpwuid(getuid());
1152 char buf
[sizeof(long) * 8 + 1];
1154 unsetenv("COLUMNS");
1156 unsetenv("TERMCAP");
1159 setenv("LOGNAME", pass
->pw_name
, 1);
1160 setenv("USER", pass
->pw_name
, 1);
1161 setenv("SHELL", pass
->pw_shell
, 0);
1162 setenv("HOME", pass
->pw_dir
, 0);
1165 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1166 setenv("WINDOWID", buf
, 1);
1168 signal(SIGCHLD
, SIG_DFL
);
1169 signal(SIGHUP
, SIG_DFL
);
1170 signal(SIGINT
, SIG_DFL
);
1171 signal(SIGQUIT
, SIG_DFL
);
1172 signal(SIGTERM
, SIG_DFL
);
1173 signal(SIGALRM
, SIG_DFL
);
1175 DEFAULT(envshell
, shell
);
1176 setenv("TERM", termname
, 1);
1177 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1178 execvp(args
[0], args
);
1186 if(waitpid(pid
, &stat
, 0) < 0)
1187 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1189 if(WIFEXITED(stat
)) {
1190 exit(WEXITSTATUS(stat
));
1199 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1201 /* seems to work fine on linux, openbsd and freebsd */
1202 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1203 die("openpty failed: %s\n", SERRNO
);
1205 switch(pid
= fork()) {
1207 die("fork failed\n");
1210 setsid(); /* create a new process group */
1211 dup2(s
, STDIN_FILENO
);
1212 dup2(s
, STDOUT_FILENO
);
1213 dup2(s
, STDERR_FILENO
);
1214 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1215 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1223 signal(SIGCHLD
, sigchld
);
1225 iofd
= (!strcmp(opt_io
, "-")) ?
1227 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1229 fprintf(stderr
, "Error opening %s:%s\n",
1230 opt_io
, strerror(errno
));
1240 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1242 fprintf(stderr
, "\n");
1247 static char buf
[BUFSIZ
];
1248 static int buflen
= 0;
1251 int charsize
; /* size of utf8 char in bytes */
1255 /* append read bytes to unprocessed bytes */
1256 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1257 die("Couldn't read from shell: %s\n", SERRNO
);
1259 /* process every complete utf8 char */
1262 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1263 charsize
= utf8decode(ptr
, &utf8c
);
1264 utf8encode(&utf8c
, s
);
1270 /* keep any uncomplete utf8 char for the next call */
1271 memmove(buf
, ptr
, buflen
);
1275 ttywrite(const char *s
, size_t n
) {
1276 if(write(cmdfd
, s
, n
) == -1)
1277 die("write error on tty: %s\n", SERRNO
);
1284 w
.ws_row
= term
.row
;
1285 w
.ws_col
= term
.col
;
1286 w
.ws_xpixel
= xw
.tw
;
1287 w
.ws_ypixel
= xw
.th
;
1288 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1289 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1293 tattrset(int attr
) {
1296 for(i
= 0; i
< term
.row
-1; i
++) {
1297 for(j
= 0; j
< term
.col
-1; j
++) {
1298 if(term
.line
[i
][j
].mode
& attr
)
1307 tsetdirt(int top
, int bot
) {
1310 LIMIT(top
, 0, term
.row
-1);
1311 LIMIT(bot
, 0, term
.row
-1);
1313 for(i
= top
; i
<= bot
; i
++)
1318 tsetdirtattr(int attr
) {
1321 for(i
= 0; i
< term
.row
-1; i
++) {
1322 for(j
= 0; j
< term
.col
-1; j
++) {
1323 if(term
.line
[i
][j
].mode
& attr
) {
1333 tsetdirt(0, term
.row
-1);
1340 if(mode
== CURSOR_SAVE
) {
1342 } else if(mode
== CURSOR_LOAD
) {
1352 term
.c
= (TCursor
){{
1356 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1358 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1359 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1362 term
.bot
= term
.row
- 1;
1363 term
.mode
= MODE_WRAP
;
1365 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1367 tcursor(CURSOR_SAVE
);
1371 tnew(int col
, int row
) {
1372 memset(&term
, 0, sizeof(Term
));
1381 Line
*tmp
= term
.line
;
1383 term
.line
= term
.alt
;
1385 term
.mode
^= MODE_ALTSCREEN
;
1390 tscrolldown(int orig
, int n
) {
1394 LIMIT(n
, 0, term
.bot
-orig
+1);
1396 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1398 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1399 temp
= term
.line
[i
];
1400 term
.line
[i
] = term
.line
[i
-n
];
1401 term
.line
[i
-n
] = temp
;
1404 term
.dirty
[i
-n
] = 1;
1411 tscrollup(int orig
, int n
) {
1414 LIMIT(n
, 0, term
.bot
-orig
+1);
1416 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1418 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1419 temp
= term
.line
[i
];
1420 term
.line
[i
] = term
.line
[i
+n
];
1421 term
.line
[i
+n
] = temp
;
1424 term
.dirty
[i
+n
] = 1;
1427 selscroll(orig
, -n
);
1431 selscroll(int orig
, int n
) {
1435 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1436 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1440 if(sel
.type
== SEL_RECTANGULAR
) {
1441 if(sel
.ob
.y
< term
.top
)
1442 sel
.ob
.y
= term
.top
;
1443 if(sel
.oe
.y
> term
.bot
)
1444 sel
.oe
.y
= term
.bot
;
1446 if(sel
.ob
.y
< term
.top
) {
1447 sel
.ob
.y
= term
.top
;
1450 if(sel
.oe
.y
> term
.bot
) {
1451 sel
.oe
.y
= term
.bot
;
1452 sel
.oe
.x
= term
.col
;
1460 tnewline(int first_col
) {
1464 tscrollup(term
.top
, 1);
1468 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1473 char *p
= csiescseq
.buf
, *np
;
1482 csiescseq
.buf
[csiescseq
.len
] = '\0';
1483 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1485 v
= strtol(p
, &np
, 10);
1488 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1490 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1492 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1496 csiescseq
.mode
= *p
;
1499 /* for absolute user moves, when decom is set */
1501 tmoveato(int x
, int y
) {
1502 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1506 tmoveto(int x
, int y
) {
1509 if(term
.c
.state
& CURSOR_ORIGIN
) {
1514 maxy
= term
.row
- 1;
1516 LIMIT(x
, 0, term
.col
-1);
1517 LIMIT(y
, miny
, maxy
);
1518 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1524 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1525 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1526 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1527 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1528 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1529 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1530 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1531 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1532 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1533 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1537 * The table is proudly stolen from rxvt.
1539 if(attr
->mode
& ATTR_GFX
) {
1540 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1541 && vt100_0
[c
[0] - 0x41]) {
1542 c
= vt100_0
[c
[0] - 0x41];
1547 term
.line
[y
][x
] = *attr
;
1548 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1552 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1556 temp
= x1
, x1
= x2
, x2
= temp
;
1558 temp
= y1
, y1
= y2
, y2
= temp
;
1560 LIMIT(x1
, 0, term
.col
-1);
1561 LIMIT(x2
, 0, term
.col
-1);
1562 LIMIT(y1
, 0, term
.row
-1);
1563 LIMIT(y2
, 0, term
.row
-1);
1565 for(y
= y1
; y
<= y2
; y
++) {
1567 for(x
= x1
; x
<= x2
; x
++) {
1570 term
.line
[y
][x
] = term
.c
.attr
;
1571 memcpy(term
.line
[y
][x
].c
, " ", 2);
1577 tdeletechar(int n
) {
1578 int src
= term
.c
.x
+ n
;
1580 int size
= term
.col
- src
;
1582 term
.dirty
[term
.c
.y
] = 1;
1584 if(src
>= term
.col
) {
1585 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1589 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1590 size
* sizeof(Glyph
));
1591 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1595 tinsertblank(int n
) {
1598 int size
= term
.col
- dst
;
1600 term
.dirty
[term
.c
.y
] = 1;
1602 if(dst
>= term
.col
) {
1603 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1607 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1608 size
* sizeof(Glyph
));
1609 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1613 tinsertblankline(int n
) {
1614 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1617 tscrolldown(term
.c
.y
, n
);
1621 tdeleteline(int n
) {
1622 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1625 tscrollup(term
.c
.y
, n
);
1629 tdefcolor(int *attr
, int *npar
, int l
) {
1633 switch (attr
[*npar
+ 1]) {
1634 case 2: /* direct colour in RGB space */
1635 if (*npar
+ 4 >= l
) {
1637 "erresc(38): Incorrect number of parameters (%d)\n",
1641 r
= attr
[*npar
+ 2];
1642 g
= attr
[*npar
+ 3];
1643 b
= attr
[*npar
+ 4];
1645 if(!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1646 fprintf(stderr
, "erresc: bad rgb color (%d,%d,%d)\n",
1649 idx
= TRUECOLOR(r
, g
, b
);
1651 case 5: /* indexed colour */
1652 if (*npar
+ 2 >= l
) {
1654 "erresc(38): Incorrect number of parameters (%d)\n",
1659 if(!BETWEEN(attr
[*npar
], 0, 255))
1660 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1664 case 0: /* implemented defined (only foreground) */
1665 case 1: /* transparent */
1666 case 3: /* direct colour in CMY space */
1667 case 4: /* direct colour in CMYK space */
1670 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1677 tsetattr(int *attr
, int l
) {
1681 for(i
= 0; i
< l
; i
++) {
1684 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1685 | ATTR_BOLD
| ATTR_ITALIC \
1687 term
.c
.attr
.fg
= defaultfg
;
1688 term
.c
.attr
.bg
= defaultbg
;
1691 term
.c
.attr
.mode
|= ATTR_BOLD
;
1694 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1697 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1699 case 5: /* slow blink */
1700 case 6: /* rapid blink */
1701 term
.c
.attr
.mode
|= ATTR_BLINK
;
1704 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1708 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1711 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1714 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1718 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1721 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1724 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1725 term
.c
.attr
.fg
= idx
;
1728 term
.c
.attr
.fg
= defaultfg
;
1731 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1732 term
.c
.attr
.bg
= idx
;
1735 term
.c
.attr
.bg
= defaultbg
;
1738 if(BETWEEN(attr
[i
], 30, 37)) {
1739 term
.c
.attr
.fg
= attr
[i
] - 30;
1740 } else if(BETWEEN(attr
[i
], 40, 47)) {
1741 term
.c
.attr
.bg
= attr
[i
] - 40;
1742 } else if(BETWEEN(attr
[i
], 90, 97)) {
1743 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1744 } else if(BETWEEN(attr
[i
], 100, 107)) {
1745 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1748 "erresc(default): gfx attr %d unknown\n",
1749 attr
[i
]), csidump();
1757 tsetscroll(int t
, int b
) {
1760 LIMIT(t
, 0, term
.row
-1);
1761 LIMIT(b
, 0, term
.row
-1);
1771 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1774 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1778 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1782 case 1: /* DECCKM -- Cursor key */
1783 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1785 case 5: /* DECSCNM -- Reverse video */
1787 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1788 if(mode
!= term
.mode
)
1789 redraw(REDRAW_TIMEOUT
);
1791 case 6: /* DECOM -- Origin */
1792 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1795 case 7: /* DECAWM -- Auto wrap */
1796 MODBIT(term
.mode
, set
, MODE_WRAP
);
1798 case 0: /* Error (IGNORED) */
1799 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1800 case 3: /* DECCOLM -- Column (IGNORED) */
1801 case 4: /* DECSCLM -- Scroll (IGNORED) */
1802 case 8: /* DECARM -- Auto repeat (IGNORED) */
1803 case 18: /* DECPFF -- Printer feed (IGNORED) */
1804 case 19: /* DECPEX -- Printer extent (IGNORED) */
1805 case 42: /* DECNRCM -- National characters (IGNORED) */
1806 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1808 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1809 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1811 case 9: /* X10 mouse compatibility mode */
1812 xsetpointermotion(0);
1813 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1814 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1816 case 1000: /* 1000: report button press */
1817 xsetpointermotion(0);
1818 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1819 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1821 case 1002: /* 1002: report motion on button press */
1822 xsetpointermotion(0);
1823 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1824 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1826 case 1003: /* 1003: enable all mouse motions */
1827 xsetpointermotion(set
);
1828 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1829 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1831 case 1004: /* 1004: send focus events to tty */
1832 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1834 case 1006: /* 1006: extended reporting mode */
1835 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1838 MODBIT(term
.mode
, set
, MODE_8BIT
);
1840 case 1049: /* = 1047 and 1048 */
1843 if (!allowaltscreen
)
1846 alt
= IS_SET(MODE_ALTSCREEN
);
1848 tclearregion(0, 0, term
.col
-1,
1851 if(set
^ alt
) /* set is always 1 or 0 */
1857 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1859 /* Not implemented mouse modes. See comments there. */
1860 case 1001: /* mouse highlight mode; can hang the
1861 terminal by design when implemented. */
1862 case 1005: /* UTF-8 mouse mode; will confuse
1863 applications not supporting UTF-8
1865 case 1015: /* urxvt mangled mouse mode; incompatible
1866 and can be mistaken for other control
1870 "erresc: unknown private set/reset mode %d\n",
1876 case 0: /* Error (IGNORED) */
1878 case 2: /* KAM -- keyboard action */
1879 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1881 case 4: /* IRM -- Insertion-replacement */
1882 MODBIT(term
.mode
, set
, MODE_INSERT
);
1884 case 12: /* SRM -- Send/Receive */
1885 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1887 case 20: /* LNM -- Linefeed/new line */
1888 MODBIT(term
.mode
, set
, MODE_CRLF
);
1892 "erresc: unknown set/reset mode %d\n",
1902 switch(csiescseq
.mode
) {
1905 fprintf(stderr
, "erresc: unknown csi ");
1909 case '@': /* ICH -- Insert <n> blank char */
1910 DEFAULT(csiescseq
.arg
[0], 1);
1911 tinsertblank(csiescseq
.arg
[0]);
1913 case 'A': /* CUU -- Cursor <n> Up */
1914 DEFAULT(csiescseq
.arg
[0], 1);
1915 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1917 case 'B': /* CUD -- Cursor <n> Down */
1918 case 'e': /* VPR --Cursor <n> Down */
1919 DEFAULT(csiescseq
.arg
[0], 1);
1920 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1922 case 'c': /* DA -- Device Attributes */
1923 if(csiescseq
.arg
[0] == 0)
1924 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1926 case 'C': /* CUF -- Cursor <n> Forward */
1927 case 'a': /* HPR -- Cursor <n> Forward */
1928 DEFAULT(csiescseq
.arg
[0], 1);
1929 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1931 case 'D': /* CUB -- Cursor <n> Backward */
1932 DEFAULT(csiescseq
.arg
[0], 1);
1933 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1935 case 'E': /* CNL -- Cursor <n> Down and first col */
1936 DEFAULT(csiescseq
.arg
[0], 1);
1937 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1939 case 'F': /* CPL -- Cursor <n> Up and first col */
1940 DEFAULT(csiescseq
.arg
[0], 1);
1941 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1943 case 'g': /* TBC -- Tabulation clear */
1944 switch(csiescseq
.arg
[0]) {
1945 case 0: /* clear current tab stop */
1946 term
.tabs
[term
.c
.x
] = 0;
1948 case 3: /* clear all the tabs */
1949 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1955 case 'G': /* CHA -- Move to <col> */
1957 DEFAULT(csiescseq
.arg
[0], 1);
1958 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1960 case 'H': /* CUP -- Move to <row> <col> */
1962 DEFAULT(csiescseq
.arg
[0], 1);
1963 DEFAULT(csiescseq
.arg
[1], 1);
1964 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1966 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1967 DEFAULT(csiescseq
.arg
[0], 1);
1968 while(csiescseq
.arg
[0]--)
1971 case 'J': /* ED -- Clear screen */
1973 switch(csiescseq
.arg
[0]) {
1975 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1976 if(term
.c
.y
< term
.row
-1) {
1977 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1983 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1984 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1987 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1993 case 'K': /* EL -- Clear line */
1994 switch(csiescseq
.arg
[0]) {
1996 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2000 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2003 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2007 case 'S': /* SU -- Scroll <n> line up */
2008 DEFAULT(csiescseq
.arg
[0], 1);
2009 tscrollup(term
.top
, csiescseq
.arg
[0]);
2011 case 'T': /* SD -- Scroll <n> line down */
2012 DEFAULT(csiescseq
.arg
[0], 1);
2013 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2015 case 'L': /* IL -- Insert <n> blank lines */
2016 DEFAULT(csiescseq
.arg
[0], 1);
2017 tinsertblankline(csiescseq
.arg
[0]);
2019 case 'l': /* RM -- Reset Mode */
2020 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2022 case 'M': /* DL -- Delete <n> lines */
2023 DEFAULT(csiescseq
.arg
[0], 1);
2024 tdeleteline(csiescseq
.arg
[0]);
2026 case 'X': /* ECH -- Erase <n> char */
2027 DEFAULT(csiescseq
.arg
[0], 1);
2028 tclearregion(term
.c
.x
, term
.c
.y
,
2029 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2031 case 'P': /* DCH -- Delete <n> char */
2032 DEFAULT(csiescseq
.arg
[0], 1);
2033 tdeletechar(csiescseq
.arg
[0]);
2035 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2036 DEFAULT(csiescseq
.arg
[0], 1);
2037 while(csiescseq
.arg
[0]--)
2040 case 'd': /* VPA -- Move to <row> */
2041 DEFAULT(csiescseq
.arg
[0], 1);
2042 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2044 case 'h': /* SM -- Set terminal mode */
2045 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2047 case 'm': /* SGR -- Terminal attribute (color) */
2048 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2050 case 'r': /* DECSTBM -- Set Scrolling Region */
2051 if(csiescseq
.priv
) {
2054 DEFAULT(csiescseq
.arg
[0], 1);
2055 DEFAULT(csiescseq
.arg
[1], term
.row
);
2056 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2060 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2061 tcursor(CURSOR_SAVE
);
2063 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2064 tcursor(CURSOR_LOAD
);
2075 for(i
= 0; i
< csiescseq
.len
; i
++) {
2076 c
= csiescseq
.buf
[i
] & 0xff;
2079 } else if(c
== '\n') {
2081 } else if(c
== '\r') {
2083 } else if(c
== 0x1b) {
2086 printf("(%02x)", c
);
2094 memset(&csiescseq
, 0, sizeof(csiescseq
));
2103 narg
= strescseq
.narg
;
2105 switch(strescseq
.type
) {
2106 case ']': /* OSC -- Operating System Command */
2107 switch(i
= atoi(strescseq
.args
[0])) {
2112 xsettitle(strescseq
.args
[1]);
2114 case 4: /* color set */
2117 p
= strescseq
.args
[2];
2119 case 104: /* color reset, here p = NULL */
2120 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2121 if (!xsetcolorname(j
, p
)) {
2122 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2125 * TODO if defaultbg color is changed, borders
2132 fprintf(stderr
, "erresc: unknown str ");
2137 case 'k': /* old title set compatibility */
2138 xsettitle(strescseq
.args
[0]);
2140 case 'P': /* DSC -- Device Control String */
2141 case '_': /* APC -- Application Program Command */
2142 case '^': /* PM -- Privacy Message */
2144 fprintf(stderr
, "erresc: unknown str ");
2153 char *p
= strescseq
.buf
;
2156 strescseq
.buf
[strescseq
.len
] = '\0';
2157 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2158 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2166 printf("ESC%c", strescseq
.type
);
2167 for(i
= 0; i
< strescseq
.len
; i
++) {
2168 c
= strescseq
.buf
[i
] & 0xff;
2171 } else if(isprint(c
)) {
2173 } else if(c
== '\n') {
2175 } else if(c
== '\r') {
2177 } else if(c
== 0x1b) {
2180 printf("(%02x)", c
);
2188 memset(&strescseq
, 0, sizeof(strescseq
));
2192 tputtab(bool forward
) {
2198 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2203 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2206 tmoveto(x
, term
.c
.y
);
2210 techo(char *buf
, int len
) {
2211 for(; len
> 0; buf
++, len
--) {
2214 if(c
== '\033') { /* escape */
2217 } else if(c
< '\x20') { /* control code */
2218 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2232 tputc(char *c
, int len
) {
2234 bool control
= ascii
< '\x20' || ascii
== 0177;
2237 if(xwrite(iofd
, c
, len
) < 0) {
2238 fprintf(stderr
, "Error writing in %s:%s\n",
2239 opt_io
, strerror(errno
));
2246 * STR sequences must be checked before anything else
2247 * because it can use some control codes as part of the sequence.
2249 if(term
.esc
& ESC_STR
) {
2252 term
.esc
= ESC_START
| ESC_STR_END
;
2254 case '\a': /* backwards compatibility to xterm */
2259 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2260 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2261 strescseq
.len
+= len
;
2264 * Here is a bug in terminals. If the user never sends
2265 * some code to stop the str or esc command, then st
2266 * will stop responding. But this is better than
2267 * silently failing with unknown characters. At least
2268 * then users will report back.
2270 * In the case users ever get fixed, here is the code:
2282 * Actions of control codes must be performed as soon they arrive
2283 * because they can be embedded inside a control sequence, and
2284 * they must not cause conflicts with sequences.
2292 tmoveto(term
.c
.x
-1, term
.c
.y
);
2295 tmoveto(0, term
.c
.y
);
2300 /* go to first col if the mode is set */
2301 tnewline(IS_SET(MODE_CRLF
));
2303 case '\a': /* BEL */
2304 if(!(xw
.state
& WIN_FOCUSED
))
2307 case '\033': /* ESC */
2309 term
.esc
= ESC_START
;
2311 case '\016': /* SO */
2312 case '\017': /* SI */
2314 * Different charsets are hard to handle. Applications
2315 * should use the right alt charset escapes for the
2316 * only reason they still exist: line drawing. The
2317 * rest is incompatible history st should not support.
2320 case '\032': /* SUB */
2321 case '\030': /* CAN */
2324 case '\005': /* ENQ (IGNORED) */
2325 case '\000': /* NUL (IGNORED) */
2326 case '\021': /* XON (IGNORED) */
2327 case '\023': /* XOFF (IGNORED) */
2328 case 0177: /* DEL (IGNORED) */
2331 } else if(term
.esc
& ESC_START
) {
2332 if(term
.esc
& ESC_CSI
) {
2333 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2334 if(BETWEEN(ascii
, 0x40, 0x7E)
2335 || csiescseq
.len
>= \
2336 sizeof(csiescseq
.buf
)-1) {
2341 } else if(term
.esc
& ESC_STR_END
) {
2345 } else if(term
.esc
& ESC_ALTCHARSET
) {
2347 case '0': /* Line drawing set */
2348 term
.c
.attr
.mode
|= ATTR_GFX
;
2350 case 'B': /* USASCII */
2351 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2353 case 'A': /* UK (IGNORED) */
2354 case '<': /* multinational charset (IGNORED) */
2355 case '5': /* Finnish (IGNORED) */
2356 case 'C': /* Finnish (IGNORED) */
2357 case 'K': /* German (IGNORED) */
2360 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2363 } else if(term
.esc
& ESC_TEST
) {
2364 if(ascii
== '8') { /* DEC screen alignment test. */
2365 char E
[UTF_SIZ
] = "E";
2368 for(x
= 0; x
< term
.col
; ++x
) {
2369 for(y
= 0; y
< term
.row
; ++y
)
2370 tsetchar(E
, &term
.c
.attr
, x
, y
);
2377 term
.esc
|= ESC_CSI
;
2380 term
.esc
|= ESC_TEST
;
2382 case 'P': /* DCS -- Device Control String */
2383 case '_': /* APC -- Application Program Command */
2384 case '^': /* PM -- Privacy Message */
2385 case ']': /* OSC -- Operating System Command */
2386 case 'k': /* old title set compatibility */
2388 strescseq
.type
= ascii
;
2389 term
.esc
|= ESC_STR
;
2391 case '(': /* set primary charset G0 */
2392 term
.esc
|= ESC_ALTCHARSET
;
2394 case ')': /* set secondary charset G1 (IGNORED) */
2395 case '*': /* set tertiary charset G2 (IGNORED) */
2396 case '+': /* set quaternary charset G3 (IGNORED) */
2399 case 'D': /* IND -- Linefeed */
2400 if(term
.c
.y
== term
.bot
) {
2401 tscrollup(term
.top
, 1);
2403 tmoveto(term
.c
.x
, term
.c
.y
+1);
2407 case 'E': /* NEL -- Next line */
2408 tnewline(1); /* always go to first col */
2411 case 'H': /* HTS -- Horizontal tab stop */
2412 term
.tabs
[term
.c
.x
] = 1;
2415 case 'M': /* RI -- Reverse index */
2416 if(term
.c
.y
== term
.top
) {
2417 tscrolldown(term
.top
, 1);
2419 tmoveto(term
.c
.x
, term
.c
.y
-1);
2423 case 'Z': /* DECID -- Identify Terminal */
2424 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2427 case 'c': /* RIS -- Reset to inital state */
2433 case '=': /* DECPAM -- Application keypad */
2434 term
.mode
|= MODE_APPKEYPAD
;
2437 case '>': /* DECPNM -- Normal keypad */
2438 term
.mode
&= ~MODE_APPKEYPAD
;
2441 case '7': /* DECSC -- Save Cursor */
2442 tcursor(CURSOR_SAVE
);
2445 case '8': /* DECRC -- Restore Cursor */
2446 tcursor(CURSOR_LOAD
);
2449 case '\\': /* ST -- Stop */
2453 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2454 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2459 * All characters which form part of a sequence are not
2465 * Display control codes only if we are in graphic mode
2467 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2469 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2471 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2472 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2476 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2477 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2478 &term
.line
[term
.c
.y
][term
.c
.x
],
2479 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2482 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2483 if(term
.c
.x
+1 < term
.col
) {
2484 tmoveto(term
.c
.x
+1, term
.c
.y
);
2486 term
.c
.state
|= CURSOR_WRAPNEXT
;
2491 tresize(int col
, int row
) {
2493 int minrow
= MIN(row
, term
.row
);
2494 int mincol
= MIN(col
, term
.col
);
2495 int slide
= term
.c
.y
- row
+ 1;
2499 if(col
< 1 || row
< 1)
2502 /* free unneeded rows */
2506 * slide screen to keep cursor where we expect it -
2507 * tscrollup would work here, but we can optimize to
2508 * memmove because we're freeing the earlier lines
2510 for(/* i = 0 */; i
< slide
; i
++) {
2514 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2515 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2517 for(i
+= row
; i
< term
.row
; i
++) {
2522 /* resize to new height */
2523 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2524 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2525 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2526 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2528 /* resize each row to new width, zero-pad if needed */
2529 for(i
= 0; i
< minrow
; i
++) {
2531 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2532 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2535 /* allocate any new rows */
2536 for(/* i == minrow */; i
< row
; i
++) {
2538 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2539 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2541 if(col
> term
.col
) {
2542 bp
= term
.tabs
+ term
.col
;
2544 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2545 while(--bp
> term
.tabs
&& !*bp
)
2547 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2550 /* update terminal size */
2553 /* reset scrolling region */
2554 tsetscroll(0, row
-1);
2555 /* make use of the LIMIT in tmoveto */
2556 tmoveto(term
.c
.x
, term
.c
.y
);
2557 /* Clearing both screens */
2560 if(mincol
< col
&& 0 < minrow
) {
2561 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2563 if(0 < col
&& minrow
< row
) {
2564 tclearregion(0, minrow
, col
- 1, row
- 1);
2567 } while(orig
!= term
.line
);
2573 xresize(int col
, int row
) {
2574 xw
.tw
= MAX(1, col
* xw
.cw
);
2575 xw
.th
= MAX(1, row
* xw
.ch
);
2577 XFreePixmap(xw
.dpy
, xw
.buf
);
2578 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2579 DefaultDepth(xw
.dpy
, xw
.scr
));
2580 XftDrawChange(xw
.draw
, xw
.buf
);
2581 xclear(0, 0, xw
.w
, xw
.h
);
2584 static inline ushort
2585 sixd_to_16bit(int x
) {
2586 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2592 XRenderColor color
= { .alpha
= 0xffff };
2597 for (cp
= dc
.col
; cp
< dc
.col
+ LEN(dc
.col
); ++cp
)
2598 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
2601 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2602 for(i
= 0; i
< LEN(colorname
); i
++) {
2605 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2606 die("Could not allocate color '%s'\n", colorname
[i
]);
2610 /* load colors [16-255] ; same colors as xterm */
2611 for(i
= 16, r
= 0; r
< 6; r
++) {
2612 for(g
= 0; g
< 6; g
++) {
2613 for(b
= 0; b
< 6; b
++) {
2614 color
.red
= sixd_to_16bit(r
);
2615 color
.green
= sixd_to_16bit(g
);
2616 color
.blue
= sixd_to_16bit(b
);
2617 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2618 die("Could not allocate color %d\n", i
);
2625 for(r
= 0; r
< 24; r
++, i
++) {
2626 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2627 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2629 die("Could not allocate color %d\n", i
);
2636 xsetcolorname(int x
, const char *name
) {
2637 XRenderColor color
= { .alpha
= 0xffff };
2639 if (x
< 0 || x
> LEN(colorname
))
2642 if(16 <= x
&& x
< 16 + 216) {
2643 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2644 color
.red
= sixd_to_16bit(r
);
2645 color
.green
= sixd_to_16bit(g
);
2646 color
.blue
= sixd_to_16bit(b
);
2647 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2648 return 0; /* something went wrong */
2651 } else if (16 + 216 <= x
&& x
< 256) {
2652 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2653 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2654 return 0; /* something went wrong */
2658 name
= colorname
[x
];
2661 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2668 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2669 XftDrawRect(xw
.draw
,
2670 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2671 borderpx
+ col1
* xw
.cw
,
2672 borderpx
+ row1
* xw
.ch
,
2673 (col2
-col1
+1) * xw
.cw
,
2674 (row2
-row1
+1) * xw
.ch
);
2678 * Absolute coordinates.
2681 xclear(int x1
, int y1
, int x2
, int y2
) {
2682 XftDrawRect(xw
.draw
,
2683 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2684 x1
, y1
, x2
-x1
, y2
-y1
);
2689 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2690 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2691 XSizeHints
*sizeh
= NULL
;
2693 sizeh
= XAllocSizeHints();
2694 if(xw
.isfixed
== False
) {
2695 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2696 sizeh
->height
= xw
.h
;
2697 sizeh
->width
= xw
.w
;
2698 sizeh
->height_inc
= xw
.ch
;
2699 sizeh
->width_inc
= xw
.cw
;
2700 sizeh
->base_height
= 2 * borderpx
;
2701 sizeh
->base_width
= 2 * borderpx
;
2703 sizeh
->flags
= PMaxSize
| PMinSize
;
2704 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2705 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2708 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2713 xloadfont(Font
*f
, FcPattern
*pattern
) {
2717 match
= FcFontMatch(NULL
, pattern
, &result
);
2721 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2722 FcPatternDestroy(match
);
2727 f
->pattern
= FcPatternDuplicate(pattern
);
2729 f
->ascent
= f
->match
->ascent
;
2730 f
->descent
= f
->match
->descent
;
2732 f
->rbearing
= f
->match
->max_advance_width
;
2734 f
->height
= f
->ascent
+ f
->descent
;
2735 f
->width
= f
->lbearing
+ f
->rbearing
;
2741 xloadfonts(char *fontstr
, int fontsize
) {
2746 if(fontstr
[0] == '-') {
2747 pattern
= XftXlfdParse(fontstr
, False
, False
);
2749 pattern
= FcNameParse((FcChar8
*)fontstr
);
2753 die("st: can't open font %s\n", fontstr
);
2756 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2757 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2758 usedfontsize
= fontsize
;
2760 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2761 if(result
== FcResultMatch
) {
2762 usedfontsize
= (int)fontval
;
2765 * Default font size is 12, if none given. This is to
2766 * have a known usedfontsize value.
2768 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2773 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2774 FcDefaultSubstitute(pattern
);
2776 if(xloadfont(&dc
.font
, pattern
))
2777 die("st: can't open font %s\n", fontstr
);
2779 /* Setting character width and height. */
2780 xw
.cw
= dc
.font
.width
;
2781 xw
.ch
= dc
.font
.height
;
2783 FcPatternDel(pattern
, FC_SLANT
);
2784 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2785 if(xloadfont(&dc
.ifont
, pattern
))
2786 die("st: can't open font %s\n", fontstr
);
2788 FcPatternDel(pattern
, FC_WEIGHT
);
2789 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2790 if(xloadfont(&dc
.ibfont
, pattern
))
2791 die("st: can't open font %s\n", fontstr
);
2793 FcPatternDel(pattern
, FC_SLANT
);
2794 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2795 if(xloadfont(&dc
.bfont
, pattern
))
2796 die("st: can't open font %s\n", fontstr
);
2798 FcPatternDestroy(pattern
);
2802 xloadfontset(Font
*f
) {
2805 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2811 xunloadfont(Font
*f
) {
2812 XftFontClose(xw
.dpy
, f
->match
);
2813 FcPatternDestroy(f
->pattern
);
2815 FcFontSetDestroy(f
->set
);
2819 xunloadfonts(void) {
2822 /* Free the loaded fonts in the font cache. */
2823 for(i
= 0; i
< frclen
; i
++) {
2824 XftFontClose(xw
.dpy
, frc
[i
].font
);
2828 xunloadfont(&dc
.font
);
2829 xunloadfont(&dc
.bfont
);
2830 xunloadfont(&dc
.ifont
);
2831 xunloadfont(&dc
.ibfont
);
2835 xzoom(const Arg
*arg
) {
2837 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2849 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2850 die("Can't open display\n");
2851 xw
.scr
= XDefaultScreen(xw
.dpy
);
2852 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2856 die("Could not init fontconfig.\n");
2858 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2859 xloadfonts(usedfont
, 0);
2862 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2865 /* adjust fixed window geometry */
2867 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2868 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2870 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2872 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2877 /* window - default size */
2878 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2879 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2885 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2886 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2887 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2888 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2889 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2890 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2891 xw
.attrs
.colormap
= xw
.cmap
;
2893 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2894 XRootWindow(xw
.dpy
, xw
.scr
);
2895 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2896 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2897 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2898 | CWEventMask
| CWColormap
, &xw
.attrs
);
2900 memset(&gcvalues
, 0, sizeof(gcvalues
));
2901 gcvalues
.graphics_exposures
= False
;
2902 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2904 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2905 DefaultDepth(xw
.dpy
, xw
.scr
));
2906 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2907 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2909 /* Xft rendering context */
2910 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2913 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2914 XSetLocaleModifiers("@im=local");
2915 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2916 XSetLocaleModifiers("@im=");
2917 if((xw
.xim
= XOpenIM(xw
.dpy
,
2918 NULL
, NULL
, NULL
)) == NULL
) {
2919 die("XOpenIM failed. Could not open input"
2924 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2925 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2926 XNFocusWindow
, xw
.win
, NULL
);
2928 die("XCreateIC failed. Could not obtain input method.\n");
2930 /* white cursor, black outline */
2931 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2932 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2933 XRecolorCursor(xw
.dpy
, cursor
,
2934 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2935 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2937 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2938 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2939 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2942 XMapWindow(xw
.dpy
, xw
.win
);
2948 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2949 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2950 width
= charlen
* xw
.cw
, xp
, i
;
2952 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2955 Font
*font
= &dc
.font
;
2957 FcPattern
*fcpattern
, *fontpattern
;
2958 FcFontSet
*fcsets
[] = { NULL
};
2959 FcCharSet
*fccharset
;
2960 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
2961 XRenderColor colfg
, colbg
;
2964 frcflags
= FRC_NORMAL
;
2966 if(base
.mode
& ATTR_ITALIC
) {
2967 if(base
.fg
== defaultfg
)
2968 base
.fg
= defaultitalic
;
2970 frcflags
= FRC_ITALIC
;
2971 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2972 if(base
.fg
== defaultfg
)
2973 base
.fg
= defaultitalic
;
2975 frcflags
= FRC_ITALICBOLD
;
2976 } else if(base
.mode
& ATTR_UNDERLINE
) {
2977 if(base
.fg
== defaultfg
)
2978 base
.fg
= defaultunderline
;
2980 if(IS_TRUECOL(base
.fg
)) {
2981 colfg
.red
= TRUERED(base
.fg
);
2982 colfg
.green
= TRUEGREEN(base
.fg
);
2983 colfg
.blue
= TRUEBLUE(base
.fg
);
2984 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
2987 fg
= &dc
.col
[base
.fg
];
2990 if(IS_TRUECOL(base
.bg
)) {
2991 colbg
.green
= TRUEGREEN(base
.bg
);
2992 colbg
.red
= TRUERED(base
.bg
);
2993 colbg
.blue
= TRUEBLUE(base
.bg
);
2994 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
2997 bg
= &dc
.col
[base
.bg
];
3002 if(base
.mode
& ATTR_BOLD
) {
3003 if(BETWEEN(base
.fg
, 0, 7)) {
3004 /* basic system colors */
3005 fg
= &dc
.col
[base
.fg
+ 8];
3006 } else if(BETWEEN(base
.fg
, 16, 195)) {
3008 fg
= &dc
.col
[base
.fg
+ 36];
3009 } else if(BETWEEN(base
.fg
, 232, 251)) {
3011 fg
= &dc
.col
[base
.fg
+ 4];
3014 * Those ranges will not be brightened:
3015 * 8 - 15 – bright system colors
3016 * 196 - 231 – highest 256 color cube
3017 * 252 - 255 – brightest colors in greyscale
3020 frcflags
= FRC_BOLD
;
3023 if(IS_SET(MODE_REVERSE
)) {
3024 if(fg
== &dc
.col
[defaultfg
]) {
3025 fg
= &dc
.col
[defaultbg
];
3027 colfg
.red
= ~fg
->color
.red
;
3028 colfg
.green
= ~fg
->color
.green
;
3029 colfg
.blue
= ~fg
->color
.blue
;
3030 colfg
.alpha
= fg
->color
.alpha
;
3031 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3035 if(bg
== &dc
.col
[defaultbg
]) {
3036 bg
= &dc
.col
[defaultfg
];
3038 colbg
.red
= ~bg
->color
.red
;
3039 colbg
.green
= ~bg
->color
.green
;
3040 colbg
.blue
= ~bg
->color
.blue
;
3041 colbg
.alpha
= bg
->color
.alpha
;
3042 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3047 if(base
.mode
& ATTR_REVERSE
) {
3053 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3056 /* Intelligent cleaning up of the borders. */
3058 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3059 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3061 if(x
+ charlen
>= term
.col
) {
3062 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3063 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3066 xclear(winx
, 0, winx
+ width
, borderpx
);
3068 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3070 /* Clean up the region we want to draw to. */
3071 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3073 /* Set the clip region because Xft is sometimes dirty. */
3078 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3080 for(xp
= winx
; bytelen
> 0;) {
3082 * Search for the range in the to be printed string of glyphs
3083 * that are in the main font. Then print that range. If
3084 * some glyph is found that is not in the font, do the
3092 u8cblen
= utf8decode(s
, &u8char
);
3096 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3097 if(!doesexist
|| bytelen
<= 0) {
3106 XftDrawStringUtf8(xw
.draw
, fg
,
3108 winy
+ font
->ascent
,
3111 xp
+= font
->width
* u8fl
;
3123 /* Search the font cache. */
3124 for(i
= 0; i
< frclen
; i
++) {
3125 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3126 && frc
[i
].flags
== frcflags
) {
3131 /* Nothing was found. */
3135 fcsets
[0] = font
->set
;
3138 * Nothing was found in the cache. Now use
3139 * some dozen of Fontconfig calls to get the
3140 * font for one single character.
3142 fcpattern
= FcPatternDuplicate(font
->pattern
);
3143 fccharset
= FcCharSetCreate();
3145 FcCharSetAddChar(fccharset
, u8char
);
3146 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3148 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3151 FcConfigSubstitute(0, fcpattern
,
3153 FcDefaultSubstitute(fcpattern
);
3155 fontpattern
= FcFontSetMatch(0, fcsets
,
3156 FcTrue
, fcpattern
, &fcres
);
3159 * Overwrite or create the new cache entry.
3161 if(frclen
>= LEN(frc
)) {
3162 frclen
= LEN(frc
) - 1;
3163 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3166 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3168 frc
[frclen
].flags
= frcflags
;
3173 FcPatternDestroy(fcpattern
);
3174 FcCharSetDestroy(fccharset
);
3177 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3178 xp
, winy
+ frc
[i
].font
->ascent
,
3179 (FcChar8
*)u8c
, u8cblen
);
3185 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3186 winy + font->ascent, (FcChar8 *)s, bytelen);
3189 if(base
.mode
& ATTR_UNDERLINE
) {
3190 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3194 /* Reset clip to none. */
3195 XftDrawSetClip(xw
.draw
, 0);
3200 static int oldx
= 0, oldy
= 0;
3202 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3204 LIMIT(oldx
, 0, term
.col
-1);
3205 LIMIT(oldy
, 0, term
.row
-1);
3207 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3209 /* remove the old cursor */
3210 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3211 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3214 /* draw the new one */
3215 if(!(IS_SET(MODE_HIDE
))) {
3216 if(xw
.state
& WIN_FOCUSED
) {
3217 if(IS_SET(MODE_REVERSE
)) {
3218 g
.mode
|= ATTR_REVERSE
;
3224 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3226 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3227 borderpx
+ term
.c
.x
* xw
.cw
,
3228 borderpx
+ term
.c
.y
* xw
.ch
,
3230 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3231 borderpx
+ term
.c
.x
* xw
.cw
,
3232 borderpx
+ term
.c
.y
* xw
.ch
,
3234 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3235 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3236 borderpx
+ term
.c
.y
* xw
.ch
,
3238 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3239 borderpx
+ term
.c
.x
* xw
.cw
,
3240 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3243 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3249 xsettitle(char *p
) {
3252 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3254 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3259 xsettitle(opt_title
? opt_title
: "st");
3263 redraw(int timeout
) {
3264 struct timespec tv
= {0, timeout
* 1000};
3270 nanosleep(&tv
, NULL
);
3271 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3277 drawregion(0, 0, term
.col
, term
.row
);
3278 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3280 XSetForeground(xw
.dpy
, dc
.gc
,
3281 dc
.col
[IS_SET(MODE_REVERSE
)?
3282 defaultfg
: defaultbg
].pixel
);
3286 drawregion(int x1
, int y1
, int x2
, int y2
) {
3287 int ic
, ib
, x
, y
, ox
, sl
;
3289 char buf
[DRAW_BUF_SIZ
];
3290 bool ena_sel
= sel
.ob
.x
!= -1;
3292 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3295 if(!(xw
.state
& WIN_VISIBLE
))
3298 for(y
= y1
; y
< y2
; y
++) {
3302 xtermclear(0, y
, term
.col
, y
);
3304 base
= term
.line
[y
][0];
3306 for(x
= x1
; x
< x2
; x
++) {
3307 new = term
.line
[y
][x
];
3308 if(ena_sel
&& selected(x
, y
))
3309 new.mode
^= ATTR_REVERSE
;
3310 if(ib
> 0 && (ATTRCMP(base
, new)
3311 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3312 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3320 sl
= utf8size(new.c
);
3321 memcpy(buf
+ib
, new.c
, sl
);
3326 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3332 expose(XEvent
*ev
) {
3333 XExposeEvent
*e
= &ev
->xexpose
;
3335 if(xw
.state
& WIN_REDRAW
) {
3337 xw
.state
&= ~WIN_REDRAW
;
3343 visibility(XEvent
*ev
) {
3344 XVisibilityEvent
*e
= &ev
->xvisibility
;
3346 if(e
->state
== VisibilityFullyObscured
) {
3347 xw
.state
&= ~WIN_VISIBLE
;
3348 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3349 /* need a full redraw for next Expose, not just a buf copy */
3350 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3356 xw
.state
&= ~WIN_VISIBLE
;
3360 xsetpointermotion(int set
) {
3361 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3362 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3366 xseturgency(int add
) {
3367 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3369 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3370 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3376 XFocusChangeEvent
*e
= &ev
->xfocus
;
3378 if(e
->mode
== NotifyGrab
)
3381 if(ev
->type
== FocusIn
) {
3382 XSetICFocus(xw
.xic
);
3383 xw
.state
|= WIN_FOCUSED
;
3385 if(IS_SET(MODE_FOCUS
))
3386 ttywrite("\033[I", 3);
3388 XUnsetICFocus(xw
.xic
);
3389 xw
.state
&= ~WIN_FOCUSED
;
3390 if(IS_SET(MODE_FOCUS
))
3391 ttywrite("\033[O", 3);
3396 match(uint mask
, uint state
) {
3397 state
&= ~ignoremod
;
3399 if(mask
== XK_NO_MOD
&& state
)
3401 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3403 if(mask
== XK_ANY_MOD
)
3405 return state
== mask
;
3409 numlock(const Arg
*dummy
) {
3414 kmap(KeySym k
, uint state
) {
3418 /* Check for mapped keys out of X11 function keys. */
3419 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3420 if(mappedkeys
[i
] == k
)
3423 if(i
== LEN(mappedkeys
)) {
3424 if((k
& 0xFFFF) < 0xFD00)
3428 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3432 if(!match(kp
->mask
, state
))
3435 if(kp
->appkey
> 0) {
3436 if(!IS_SET(MODE_APPKEYPAD
))
3438 if(term
.numlock
&& kp
->appkey
== 2)
3440 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3444 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3446 && !IS_SET(MODE_APPCURSOR
))) {
3450 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3451 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3462 kpress(XEvent
*ev
) {
3463 XKeyEvent
*e
= &ev
->xkey
;
3465 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3471 if(IS_SET(MODE_KBDLOCK
))
3474 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3475 e
->state
&= ~Mod2Mask
;
3477 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3478 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3479 bp
->func(&(bp
->arg
));
3484 /* 2. custom keys from config.h */
3485 if((customkey
= kmap(ksym
, e
->state
))) {
3486 len
= strlen(customkey
);
3487 memcpy(buf
, customkey
, len
);
3488 /* 3. hardcoded (overrides X lookup) */
3493 if(len
== 1 && e
->state
& Mod1Mask
) {
3494 if(IS_SET(MODE_8BIT
)) {
3497 ret
= utf8encode(&c
, cp
);
3506 memcpy(cp
, xstr
, len
);
3507 len
= cp
- buf
+ len
;
3511 if(IS_SET(MODE_ECHO
))
3517 cmessage(XEvent
*e
) {
3520 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3522 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3523 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3524 xw
.state
|= WIN_FOCUSED
;
3526 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3527 xw
.state
&= ~WIN_FOCUSED
;
3529 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3530 /* Send SIGHUP to shell */
3537 cresize(int width
, int height
) {
3545 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3546 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3555 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3558 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3564 int w
= xw
.w
, h
= xw
.h
;
3566 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3567 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3569 /* Waiting for window mapping */
3571 XNextEvent(xw
.dpy
, &ev
);
3572 if(ev
.type
== ConfigureNotify
) {
3573 w
= ev
.xconfigure
.width
;
3574 h
= ev
.xconfigure
.height
;
3575 } else if(ev
.type
== MapNotify
) {
3583 cresize(xw
.fw
, xw
.fh
);
3586 gettimeofday(&lastblink
, NULL
);
3587 gettimeofday(&last
, NULL
);
3589 for(xev
= actionfps
;;) {
3591 FD_SET(cmdfd
, &rfd
);
3594 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3597 die("select failed: %s\n", SERRNO
);
3599 if(FD_ISSET(cmdfd
, &rfd
)) {
3602 blinkset
= tattrset(ATTR_BLINK
);
3603 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3604 term
.mode
&= ~(MODE_BLINK
);
3608 if(FD_ISSET(xfd
, &rfd
))
3611 gettimeofday(&now
, NULL
);
3612 drawtimeout
.tv_sec
= 0;
3613 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3617 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3618 tsetdirtattr(ATTR_BLINK
);
3619 term
.mode
^= MODE_BLINK
;
3620 gettimeofday(&lastblink
, NULL
);
3623 if(TIMEDIFF(now
, last
) \
3624 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3630 while(XPending(xw
.dpy
)) {
3631 XNextEvent(xw
.dpy
, &ev
);
3632 if(XFilterEvent(&ev
, None
))
3634 if(handler
[ev
.type
])
3635 (handler
[ev
.type
])(&ev
);
3641 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3643 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3645 if(TIMEDIFF(now
, lastblink
) \
3647 drawtimeout
.tv_usec
= 1;
3649 drawtimeout
.tv_usec
= (1000 * \
3664 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3665 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3666 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3670 main(int argc
, char *argv
[]) {
3675 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3680 allowaltscreen
= false;
3683 opt_class
= EARGF(usage());
3686 /* eat all remaining arguments */
3689 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3690 titles
= strdup(argv
[1]);
3691 opt_title
= basename(titles
);
3696 opt_font
= EARGF(usage());
3699 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3704 if(bitm
& WidthValue
)
3706 if(bitm
& HeightValue
)
3708 if(bitm
& XNegative
&& xw
.fx
== 0)
3710 if(bitm
& XNegative
&& xw
.fy
== 0)
3713 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3717 opt_io
= EARGF(usage());
3720 opt_title
= EARGF(usage());
3723 opt_embed
= EARGF(usage());
3731 setlocale(LC_CTYPE
, "");
3732 XSetLocaleModifiers("");