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 */
2432 case '=': /* DECPAM -- Application keypad */
2433 term
.mode
|= MODE_APPKEYPAD
;
2436 case '>': /* DECPNM -- Normal keypad */
2437 term
.mode
&= ~MODE_APPKEYPAD
;
2440 case '7': /* DECSC -- Save Cursor */
2441 tcursor(CURSOR_SAVE
);
2444 case '8': /* DECRC -- Restore Cursor */
2445 tcursor(CURSOR_LOAD
);
2448 case '\\': /* ST -- Stop */
2452 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2453 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2458 * All characters which form part of a sequence are not
2464 * Display control codes only if we are in graphic mode
2466 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2468 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2470 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2471 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2475 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2476 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2477 &term
.line
[term
.c
.y
][term
.c
.x
],
2478 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2481 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2482 if(term
.c
.x
+1 < term
.col
) {
2483 tmoveto(term
.c
.x
+1, term
.c
.y
);
2485 term
.c
.state
|= CURSOR_WRAPNEXT
;
2490 tresize(int col
, int row
) {
2492 int minrow
= MIN(row
, term
.row
);
2493 int mincol
= MIN(col
, term
.col
);
2494 int slide
= term
.c
.y
- row
+ 1;
2498 if(col
< 1 || row
< 1)
2501 /* free unneeded rows */
2505 * slide screen to keep cursor where we expect it -
2506 * tscrollup would work here, but we can optimize to
2507 * memmove because we're freeing the earlier lines
2509 for(/* i = 0 */; i
< slide
; i
++) {
2513 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2514 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2516 for(i
+= row
; i
< term
.row
; i
++) {
2521 /* resize to new height */
2522 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2523 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2524 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2525 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2527 /* resize each row to new width, zero-pad if needed */
2528 for(i
= 0; i
< minrow
; i
++) {
2530 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2531 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2534 /* allocate any new rows */
2535 for(/* i == minrow */; i
< row
; i
++) {
2537 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2538 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2540 if(col
> term
.col
) {
2541 bp
= term
.tabs
+ term
.col
;
2543 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2544 while(--bp
> term
.tabs
&& !*bp
)
2546 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2549 /* update terminal size */
2552 /* reset scrolling region */
2553 tsetscroll(0, row
-1);
2554 /* make use of the LIMIT in tmoveto */
2555 tmoveto(term
.c
.x
, term
.c
.y
);
2556 /* Clearing both screens */
2559 if(mincol
< col
&& 0 < minrow
) {
2560 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2562 if(0 < col
&& minrow
< row
) {
2563 tclearregion(0, minrow
, col
- 1, row
- 1);
2566 } while(orig
!= term
.line
);
2572 xresize(int col
, int row
) {
2573 xw
.tw
= MAX(1, col
* xw
.cw
);
2574 xw
.th
= MAX(1, row
* xw
.ch
);
2576 XFreePixmap(xw
.dpy
, xw
.buf
);
2577 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2578 DefaultDepth(xw
.dpy
, xw
.scr
));
2579 XftDrawChange(xw
.draw
, xw
.buf
);
2580 xclear(0, 0, xw
.w
, xw
.h
);
2583 static inline ushort
2584 sixd_to_16bit(int x
) {
2585 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2591 XRenderColor color
= { .alpha
= 0xffff };
2593 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2594 for(i
= 0; i
< LEN(colorname
); i
++) {
2597 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2598 die("Could not allocate color '%s'\n", colorname
[i
]);
2602 /* load colors [16-255] ; same colors as xterm */
2603 for(i
= 16, r
= 0; r
< 6; r
++) {
2604 for(g
= 0; g
< 6; g
++) {
2605 for(b
= 0; b
< 6; b
++) {
2606 color
.red
= sixd_to_16bit(r
);
2607 color
.green
= sixd_to_16bit(g
);
2608 color
.blue
= sixd_to_16bit(b
);
2609 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2610 die("Could not allocate color %d\n", i
);
2617 for(r
= 0; r
< 24; r
++, i
++) {
2618 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2619 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2621 die("Could not allocate color %d\n", i
);
2627 xsetcolorname(int x
, const char *name
) {
2628 XRenderColor color
= { .alpha
= 0xffff };
2630 if (x
< 0 || x
> LEN(colorname
))
2633 if(16 <= x
&& x
< 16 + 216) {
2634 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2635 color
.red
= sixd_to_16bit(r
);
2636 color
.green
= sixd_to_16bit(g
);
2637 color
.blue
= sixd_to_16bit(b
);
2638 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2639 return 0; /* something went wrong */
2642 } else if (16 + 216 <= x
&& x
< 256) {
2643 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2644 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2645 return 0; /* something went wrong */
2649 name
= colorname
[x
];
2652 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2659 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2660 XftDrawRect(xw
.draw
,
2661 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2662 borderpx
+ col1
* xw
.cw
,
2663 borderpx
+ row1
* xw
.ch
,
2664 (col2
-col1
+1) * xw
.cw
,
2665 (row2
-row1
+1) * xw
.ch
);
2669 * Absolute coordinates.
2672 xclear(int x1
, int y1
, int x2
, int y2
) {
2673 XftDrawRect(xw
.draw
,
2674 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2675 x1
, y1
, x2
-x1
, y2
-y1
);
2680 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2681 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2682 XSizeHints
*sizeh
= NULL
;
2684 sizeh
= XAllocSizeHints();
2685 if(xw
.isfixed
== False
) {
2686 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2687 sizeh
->height
= xw
.h
;
2688 sizeh
->width
= xw
.w
;
2689 sizeh
->height_inc
= xw
.ch
;
2690 sizeh
->width_inc
= xw
.cw
;
2691 sizeh
->base_height
= 2 * borderpx
;
2692 sizeh
->base_width
= 2 * borderpx
;
2694 sizeh
->flags
= PMaxSize
| PMinSize
;
2695 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2696 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2699 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2704 xloadfont(Font
*f
, FcPattern
*pattern
) {
2708 match
= FcFontMatch(NULL
, pattern
, &result
);
2712 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2713 FcPatternDestroy(match
);
2718 f
->pattern
= FcPatternDuplicate(pattern
);
2720 f
->ascent
= f
->match
->ascent
;
2721 f
->descent
= f
->match
->descent
;
2723 f
->rbearing
= f
->match
->max_advance_width
;
2725 f
->height
= f
->ascent
+ f
->descent
;
2726 f
->width
= f
->lbearing
+ f
->rbearing
;
2732 xloadfonts(char *fontstr
, int fontsize
) {
2737 if(fontstr
[0] == '-') {
2738 pattern
= XftXlfdParse(fontstr
, False
, False
);
2740 pattern
= FcNameParse((FcChar8
*)fontstr
);
2744 die("st: can't open font %s\n", fontstr
);
2747 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2748 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2749 usedfontsize
= fontsize
;
2751 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2752 if(result
== FcResultMatch
) {
2753 usedfontsize
= (int)fontval
;
2756 * Default font size is 12, if none given. This is to
2757 * have a known usedfontsize value.
2759 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2764 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2765 FcDefaultSubstitute(pattern
);
2767 if(xloadfont(&dc
.font
, pattern
))
2768 die("st: can't open font %s\n", fontstr
);
2770 /* Setting character width and height. */
2771 xw
.cw
= dc
.font
.width
;
2772 xw
.ch
= dc
.font
.height
;
2774 FcPatternDel(pattern
, FC_SLANT
);
2775 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2776 if(xloadfont(&dc
.ifont
, pattern
))
2777 die("st: can't open font %s\n", fontstr
);
2779 FcPatternDel(pattern
, FC_WEIGHT
);
2780 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2781 if(xloadfont(&dc
.ibfont
, pattern
))
2782 die("st: can't open font %s\n", fontstr
);
2784 FcPatternDel(pattern
, FC_SLANT
);
2785 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2786 if(xloadfont(&dc
.bfont
, pattern
))
2787 die("st: can't open font %s\n", fontstr
);
2789 FcPatternDestroy(pattern
);
2793 xloadfontset(Font
*f
) {
2796 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2802 xunloadfont(Font
*f
) {
2803 XftFontClose(xw
.dpy
, f
->match
);
2804 FcPatternDestroy(f
->pattern
);
2806 FcFontSetDestroy(f
->set
);
2810 xunloadfonts(void) {
2813 /* Free the loaded fonts in the font cache. */
2814 for(i
= 0; i
< frclen
; i
++) {
2815 XftFontClose(xw
.dpy
, frc
[i
].font
);
2819 xunloadfont(&dc
.font
);
2820 xunloadfont(&dc
.bfont
);
2821 xunloadfont(&dc
.ifont
);
2822 xunloadfont(&dc
.ibfont
);
2826 xzoom(const Arg
*arg
) {
2828 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2840 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2841 die("Can't open display\n");
2842 xw
.scr
= XDefaultScreen(xw
.dpy
);
2843 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2847 die("Could not init fontconfig.\n");
2849 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2850 xloadfonts(usedfont
, 0);
2853 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2856 /* adjust fixed window geometry */
2858 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2859 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2861 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2863 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2868 /* window - default size */
2869 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2870 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2876 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2877 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2878 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2879 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2880 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2881 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2882 xw
.attrs
.colormap
= xw
.cmap
;
2884 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2885 XRootWindow(xw
.dpy
, xw
.scr
);
2886 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2887 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2888 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2889 | CWEventMask
| CWColormap
, &xw
.attrs
);
2891 memset(&gcvalues
, 0, sizeof(gcvalues
));
2892 gcvalues
.graphics_exposures
= False
;
2893 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2895 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2896 DefaultDepth(xw
.dpy
, xw
.scr
));
2897 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2898 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2900 /* Xft rendering context */
2901 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2904 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2905 XSetLocaleModifiers("@im=local");
2906 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2907 XSetLocaleModifiers("@im=");
2908 if((xw
.xim
= XOpenIM(xw
.dpy
,
2909 NULL
, NULL
, NULL
)) == NULL
) {
2910 die("XOpenIM failed. Could not open input"
2915 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2916 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2917 XNFocusWindow
, xw
.win
, NULL
);
2919 die("XCreateIC failed. Could not obtain input method.\n");
2921 /* white cursor, black outline */
2922 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2923 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2924 XRecolorCursor(xw
.dpy
, cursor
,
2925 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2926 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2928 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2929 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2930 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2933 XMapWindow(xw
.dpy
, xw
.win
);
2939 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2940 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2941 width
= charlen
* xw
.cw
, xp
, i
;
2943 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2946 Font
*font
= &dc
.font
;
2948 FcPattern
*fcpattern
, *fontpattern
;
2949 FcFontSet
*fcsets
[] = { NULL
};
2950 FcCharSet
*fccharset
;
2951 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
2952 XRenderColor colfg
, colbg
;
2955 frcflags
= FRC_NORMAL
;
2957 if(base
.mode
& ATTR_ITALIC
) {
2958 if(base
.fg
== defaultfg
)
2959 base
.fg
= defaultitalic
;
2961 frcflags
= FRC_ITALIC
;
2962 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2963 if(base
.fg
== defaultfg
)
2964 base
.fg
= defaultitalic
;
2966 frcflags
= FRC_ITALICBOLD
;
2967 } else if(base
.mode
& ATTR_UNDERLINE
) {
2968 if(base
.fg
== defaultfg
)
2969 base
.fg
= defaultunderline
;
2971 if(IS_TRUECOL(base
.fg
)) {
2972 colfg
.red
= TRUERED(base
.fg
);
2973 colfg
.green
= TRUEGREEN(base
.fg
);
2974 colfg
.blue
= TRUEBLUE(base
.fg
);
2975 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
2978 fg
= &dc
.col
[base
.fg
];
2981 if(IS_TRUECOL(base
.bg
)) {
2982 colbg
.green
= TRUEGREEN(base
.bg
);
2983 colbg
.red
= TRUERED(base
.bg
);
2984 colbg
.blue
= TRUEBLUE(base
.bg
);
2985 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
2988 bg
= &dc
.col
[base
.bg
];
2993 if(base
.mode
& ATTR_BOLD
) {
2994 if(BETWEEN(base
.fg
, 0, 7)) {
2995 /* basic system colors */
2996 fg
= &dc
.col
[base
.fg
+ 8];
2997 } else if(BETWEEN(base
.fg
, 16, 195)) {
2999 fg
= &dc
.col
[base
.fg
+ 36];
3000 } else if(BETWEEN(base
.fg
, 232, 251)) {
3002 fg
= &dc
.col
[base
.fg
+ 4];
3005 * Those ranges will not be brightened:
3006 * 8 - 15 – bright system colors
3007 * 196 - 231 – highest 256 color cube
3008 * 252 - 255 – brightest colors in greyscale
3011 frcflags
= FRC_BOLD
;
3014 if(IS_SET(MODE_REVERSE
)) {
3015 if(fg
== &dc
.col
[defaultfg
]) {
3016 fg
= &dc
.col
[defaultbg
];
3018 colfg
.red
= ~fg
->color
.red
;
3019 colfg
.green
= ~fg
->color
.green
;
3020 colfg
.blue
= ~fg
->color
.blue
;
3021 colfg
.alpha
= fg
->color
.alpha
;
3022 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3026 if(bg
== &dc
.col
[defaultbg
]) {
3027 bg
= &dc
.col
[defaultfg
];
3029 colbg
.red
= ~bg
->color
.red
;
3030 colbg
.green
= ~bg
->color
.green
;
3031 colbg
.blue
= ~bg
->color
.blue
;
3032 colbg
.alpha
= bg
->color
.alpha
;
3033 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3038 if(base
.mode
& ATTR_REVERSE
) {
3044 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3047 /* Intelligent cleaning up of the borders. */
3049 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3050 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3052 if(x
+ charlen
>= term
.col
) {
3053 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3054 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3057 xclear(winx
, 0, winx
+ width
, borderpx
);
3059 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3061 /* Clean up the region we want to draw to. */
3062 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3064 /* Set the clip region because Xft is sometimes dirty. */
3069 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3071 for(xp
= winx
; bytelen
> 0;) {
3073 * Search for the range in the to be printed string of glyphs
3074 * that are in the main font. Then print that range. If
3075 * some glyph is found that is not in the font, do the
3083 u8cblen
= utf8decode(s
, &u8char
);
3087 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3088 if(!doesexist
|| bytelen
<= 0) {
3097 XftDrawStringUtf8(xw
.draw
, fg
,
3099 winy
+ font
->ascent
,
3102 xp
+= font
->width
* u8fl
;
3114 /* Search the font cache. */
3115 for(i
= 0; i
< frclen
; i
++) {
3116 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3117 && frc
[i
].flags
== frcflags
) {
3122 /* Nothing was found. */
3126 fcsets
[0] = font
->set
;
3129 * Nothing was found in the cache. Now use
3130 * some dozen of Fontconfig calls to get the
3131 * font for one single character.
3133 fcpattern
= FcPatternDuplicate(font
->pattern
);
3134 fccharset
= FcCharSetCreate();
3136 FcCharSetAddChar(fccharset
, u8char
);
3137 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3139 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3142 FcConfigSubstitute(0, fcpattern
,
3144 FcDefaultSubstitute(fcpattern
);
3146 fontpattern
= FcFontSetMatch(0, fcsets
,
3147 FcTrue
, fcpattern
, &fcres
);
3150 * Overwrite or create the new cache entry.
3152 if(frclen
>= LEN(frc
)) {
3153 frclen
= LEN(frc
) - 1;
3154 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3157 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3159 frc
[frclen
].flags
= frcflags
;
3164 FcPatternDestroy(fcpattern
);
3165 FcCharSetDestroy(fccharset
);
3168 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3169 xp
, winy
+ frc
[i
].font
->ascent
,
3170 (FcChar8
*)u8c
, u8cblen
);
3176 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3177 winy + font->ascent, (FcChar8 *)s, bytelen);
3180 if(base
.mode
& ATTR_UNDERLINE
) {
3181 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3185 /* Reset clip to none. */
3186 XftDrawSetClip(xw
.draw
, 0);
3191 static int oldx
= 0, oldy
= 0;
3193 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3195 LIMIT(oldx
, 0, term
.col
-1);
3196 LIMIT(oldy
, 0, term
.row
-1);
3198 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3200 /* remove the old cursor */
3201 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3202 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3205 /* draw the new one */
3206 if(!(IS_SET(MODE_HIDE
))) {
3207 if(xw
.state
& WIN_FOCUSED
) {
3208 if(IS_SET(MODE_REVERSE
)) {
3209 g
.mode
|= ATTR_REVERSE
;
3215 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3217 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3218 borderpx
+ term
.c
.x
* xw
.cw
,
3219 borderpx
+ term
.c
.y
* xw
.ch
,
3221 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3222 borderpx
+ term
.c
.x
* xw
.cw
,
3223 borderpx
+ term
.c
.y
* xw
.ch
,
3225 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3226 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3227 borderpx
+ term
.c
.y
* xw
.ch
,
3229 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3230 borderpx
+ term
.c
.x
* xw
.cw
,
3231 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3234 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3240 xsettitle(char *p
) {
3243 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3245 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3250 xsettitle(opt_title
? opt_title
: "st");
3254 redraw(int timeout
) {
3255 struct timespec tv
= {0, timeout
* 1000};
3261 nanosleep(&tv
, NULL
);
3262 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3268 drawregion(0, 0, term
.col
, term
.row
);
3269 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3271 XSetForeground(xw
.dpy
, dc
.gc
,
3272 dc
.col
[IS_SET(MODE_REVERSE
)?
3273 defaultfg
: defaultbg
].pixel
);
3277 drawregion(int x1
, int y1
, int x2
, int y2
) {
3278 int ic
, ib
, x
, y
, ox
, sl
;
3280 char buf
[DRAW_BUF_SIZ
];
3281 bool ena_sel
= sel
.ob
.x
!= -1;
3283 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3286 if(!(xw
.state
& WIN_VISIBLE
))
3289 for(y
= y1
; y
< y2
; y
++) {
3293 xtermclear(0, y
, term
.col
, y
);
3295 base
= term
.line
[y
][0];
3297 for(x
= x1
; x
< x2
; x
++) {
3298 new = term
.line
[y
][x
];
3299 if(ena_sel
&& selected(x
, y
))
3300 new.mode
^= ATTR_REVERSE
;
3301 if(ib
> 0 && (ATTRCMP(base
, new)
3302 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3303 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3311 sl
= utf8size(new.c
);
3312 memcpy(buf
+ib
, new.c
, sl
);
3317 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3323 expose(XEvent
*ev
) {
3324 XExposeEvent
*e
= &ev
->xexpose
;
3326 if(xw
.state
& WIN_REDRAW
) {
3328 xw
.state
&= ~WIN_REDRAW
;
3334 visibility(XEvent
*ev
) {
3335 XVisibilityEvent
*e
= &ev
->xvisibility
;
3337 if(e
->state
== VisibilityFullyObscured
) {
3338 xw
.state
&= ~WIN_VISIBLE
;
3339 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3340 /* need a full redraw for next Expose, not just a buf copy */
3341 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3347 xw
.state
&= ~WIN_VISIBLE
;
3351 xsetpointermotion(int set
) {
3352 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3353 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3357 xseturgency(int add
) {
3358 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3360 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3361 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3367 XFocusChangeEvent
*e
= &ev
->xfocus
;
3369 if(e
->mode
== NotifyGrab
)
3372 if(ev
->type
== FocusIn
) {
3373 XSetICFocus(xw
.xic
);
3374 xw
.state
|= WIN_FOCUSED
;
3376 if(IS_SET(MODE_FOCUS
))
3377 ttywrite("\033[I", 3);
3379 XUnsetICFocus(xw
.xic
);
3380 xw
.state
&= ~WIN_FOCUSED
;
3381 if(IS_SET(MODE_FOCUS
))
3382 ttywrite("\033[O", 3);
3387 match(uint mask
, uint state
) {
3388 state
&= ~ignoremod
;
3390 if(mask
== XK_NO_MOD
&& state
)
3392 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3394 if(mask
== XK_ANY_MOD
)
3396 return state
== mask
;
3400 numlock(const Arg
*dummy
) {
3405 kmap(KeySym k
, uint state
) {
3409 /* Check for mapped keys out of X11 function keys. */
3410 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3411 if(mappedkeys
[i
] == k
)
3414 if(i
== LEN(mappedkeys
)) {
3415 if((k
& 0xFFFF) < 0xFD00)
3419 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3423 if(!match(kp
->mask
, state
))
3426 if(kp
->appkey
> 0) {
3427 if(!IS_SET(MODE_APPKEYPAD
))
3429 if(term
.numlock
&& kp
->appkey
== 2)
3431 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3435 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3437 && !IS_SET(MODE_APPCURSOR
))) {
3441 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3442 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3453 kpress(XEvent
*ev
) {
3454 XKeyEvent
*e
= &ev
->xkey
;
3456 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3462 if(IS_SET(MODE_KBDLOCK
))
3465 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3466 e
->state
&= ~Mod2Mask
;
3468 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3469 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3470 bp
->func(&(bp
->arg
));
3475 /* 2. custom keys from config.h */
3476 if((customkey
= kmap(ksym
, e
->state
))) {
3477 len
= strlen(customkey
);
3478 memcpy(buf
, customkey
, len
);
3479 /* 3. hardcoded (overrides X lookup) */
3484 if(len
== 1 && e
->state
& Mod1Mask
) {
3485 if(IS_SET(MODE_8BIT
)) {
3488 ret
= utf8encode(&c
, cp
);
3497 memcpy(cp
, xstr
, len
);
3498 len
= cp
- buf
+ len
;
3502 if(IS_SET(MODE_ECHO
))
3508 cmessage(XEvent
*e
) {
3511 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3513 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3514 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3515 xw
.state
|= WIN_FOCUSED
;
3517 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3518 xw
.state
&= ~WIN_FOCUSED
;
3520 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3521 /* Send SIGHUP to shell */
3528 cresize(int width
, int height
) {
3536 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3537 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3546 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3549 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3555 int w
= xw
.w
, h
= xw
.h
;
3557 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3558 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3560 /* Waiting for window mapping */
3562 XNextEvent(xw
.dpy
, &ev
);
3563 if(ev
.type
== ConfigureNotify
) {
3564 w
= ev
.xconfigure
.width
;
3565 h
= ev
.xconfigure
.height
;
3566 } else if(ev
.type
== MapNotify
) {
3574 cresize(xw
.fw
, xw
.fh
);
3577 gettimeofday(&lastblink
, NULL
);
3578 gettimeofday(&last
, NULL
);
3580 for(xev
= actionfps
;;) {
3582 FD_SET(cmdfd
, &rfd
);
3585 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3588 die("select failed: %s\n", SERRNO
);
3590 if(FD_ISSET(cmdfd
, &rfd
)) {
3593 blinkset
= tattrset(ATTR_BLINK
);
3594 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3595 term
.mode
&= ~(MODE_BLINK
);
3599 if(FD_ISSET(xfd
, &rfd
))
3602 gettimeofday(&now
, NULL
);
3603 drawtimeout
.tv_sec
= 0;
3604 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3608 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3609 tsetdirtattr(ATTR_BLINK
);
3610 term
.mode
^= MODE_BLINK
;
3611 gettimeofday(&lastblink
, NULL
);
3614 if(TIMEDIFF(now
, last
) \
3615 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3621 while(XPending(xw
.dpy
)) {
3622 XNextEvent(xw
.dpy
, &ev
);
3623 if(XFilterEvent(&ev
, None
))
3625 if(handler
[ev
.type
])
3626 (handler
[ev
.type
])(&ev
);
3632 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3634 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3636 if(TIMEDIFF(now
, lastblink
) \
3638 drawtimeout
.tv_usec
= 1;
3640 drawtimeout
.tv_usec
= (1000 * \
3655 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3656 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3657 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3661 main(int argc
, char *argv
[]) {
3666 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3671 allowaltscreen
= false;
3674 opt_class
= EARGF(usage());
3677 /* eat all remaining arguments */
3680 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3681 titles
= strdup(argv
[1]);
3682 opt_title
= basename(titles
);
3687 opt_font
= EARGF(usage());
3690 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3695 if(bitm
& WidthValue
)
3697 if(bitm
& HeightValue
)
3699 if(bitm
& XNegative
&& xw
.fx
== 0)
3701 if(bitm
& XNegative
&& xw
.fy
== 0)
3704 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3708 opt_io
= EARGF(usage());
3711 opt_title
= EARGF(usage());
3714 opt_embed
= EARGF(usage());
3722 setlocale(LC_CTYPE
, "");
3723 XSetLocaleModifiers("");