1 /* See LICENSE for licence details. */
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/Xft/Xft.h>
28 #include <fontconfig/fontconfig.h>
36 #define Draw XftDraw *
37 #define Colour XftColor
38 #define Colourmap Colormap
39 #define Rectangle XRectangle
43 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
45 #elif defined(__FreeBSD__) || defined(__DragonFly__)
51 #define XEMBED_FOCUS_IN 4
52 #define XEMBED_FOCUS_OUT 5
56 #define ESC_BUF_SIZ (128*UTF_SIZ)
57 #define ESC_ARG_SIZ 16
58 #define STR_BUF_SIZ ESC_BUF_SIZ
59 #define STR_ARG_SIZ ESC_ARG_SIZ
60 #define DRAW_BUF_SIZ 20*1024
61 #define XK_ANY_MOD UINT_MAX
63 #define XK_SWITCH_MOD (1<<13)
65 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
68 #define SERRNO strerror(errno)
69 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #define MAX(a, b) ((a) < (b) ? (b) : (a))
71 #define LEN(a) (sizeof(a) / sizeof(a[0]))
72 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
73 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
74 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
75 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
76 #define IS_SET(flag) ((term.mode & (flag)) != 0)
77 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
79 #define VT102ID "\033[?6c"
81 enum glyph_attribute
{
92 enum cursor_movement
{
110 MODE_MOUSEMOTION
= 64,
115 MODE_APPCURSOR
= 2048,
116 MODE_MOUSESGR
= 4096,
121 MODE_MOUSEX10
= 131072,
122 MODE_MOUSEMANY
= 262144,
123 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
130 ESC_STR
= 4, /* DSC, OSC, PM, APC */
132 ESC_STR_END
= 16, /* a final string was encountered */
133 ESC_TEST
= 32, /* Enter in test mode */
142 enum selection_type
{
147 enum selection_snap
{
154 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
156 typedef unsigned char uchar
;
157 typedef unsigned int uint
;
158 typedef unsigned long ulong
;
159 typedef unsigned short ushort
;
162 char c
[UTF_SIZ
]; /* character code */
163 uchar mode
; /* attribute flags */
164 ushort fg
; /* foreground */
165 ushort bg
; /* background */
171 Glyph attr
; /* current char attributes */
177 /* CSI Escape sequence structs */
178 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
180 char buf
[ESC_BUF_SIZ
]; /* raw string */
181 int len
; /* raw string length */
183 int arg
[ESC_ARG_SIZ
];
184 int narg
; /* nb of args */
188 /* STR Escape sequence structs */
189 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
191 char type
; /* ESC type ... */
192 char buf
[STR_BUF_SIZ
]; /* raw string */
193 int len
; /* raw string length */
194 char *args
[STR_ARG_SIZ
];
195 int narg
; /* nb of args */
198 /* Internal representation of the screen */
200 int row
; /* nb row */
201 int col
; /* nb col */
202 Line
*line
; /* screen */
203 Line
*alt
; /* alternate screen */
204 bool *dirty
; /* dirtyness of lines */
205 TCursor c
; /* cursor */
206 int top
; /* top scroll limit */
207 int bot
; /* bottom scroll limit */
208 int mode
; /* terminal mode flags */
209 int esc
; /* escape state flags */
210 bool numlock
; /* lock numbers in keyboard */
214 /* Purely graphic info */
220 Atom xembed
, wmdeletewin
;
225 XSetWindowAttributes attrs
;
227 bool isfixed
; /* is fixed geometry? */
228 int fx
, fy
, fw
, fh
; /* fixed geometry */
229 int tw
, th
; /* tty width and height */
230 int w
, h
; /* window width and height */
231 int ch
; /* char height */
232 int cw
; /* char width */
233 char state
; /* focus, redraw, visible */
246 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
247 signed char appkey
; /* application keypad */
248 signed char appcursor
; /* application cursor */
249 signed char crlf
; /* crlf mode */
257 * Selection variables:
258 * nb – normalized coordinates of the beginning of the selection
259 * ne – normalized coordinates of the end of the selection
260 * ob – original coordinates of the beginning of the selection
261 * oe – original coordinates of the end of the selection
270 struct timeval tclick1
;
271 struct timeval tclick2
;
284 void (*func
)(const Arg
*);
288 /* function definitions used in config.h */
289 static void clippaste(const Arg
*);
290 static void numlock(const Arg
*);
291 static void selpaste(const Arg
*);
292 static void xzoom(const Arg
*);
294 /* Config.h for applying patches and the configuration. */
310 /* Drawing Context */
312 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
313 Font font
, bfont
, ifont
, ibfont
;
317 static void die(const char *, ...);
318 static void draw(void);
319 static void redraw(int);
320 static void drawregion(int, int, int, int);
321 static void execsh(void);
322 static void sigchld(int);
323 static void run(void);
325 static void csidump(void);
326 static void csihandle(void);
327 static void csiparse(void);
328 static void csireset(void);
329 static void strdump(void);
330 static void strhandle(void);
331 static void strparse(void);
332 static void strreset(void);
334 static int tattrset(int);
335 static void tclearregion(int, int, int, int);
336 static void tcursor(int);
337 static void tdeletechar(int);
338 static void tdeleteline(int);
339 static void tinsertblank(int);
340 static void tinsertblankline(int);
341 static void tmoveto(int, int);
342 static void tmoveato(int x
, int y
);
343 static void tnew(int, int);
344 static void tnewline(int);
345 static void tputtab(bool);
346 static void tputc(char *, int);
347 static void treset(void);
348 static int tresize(int, int);
349 static void tscrollup(int, int);
350 static void tscrolldown(int, int);
351 static void tsetattr(int*, int);
352 static void tsetchar(char *, Glyph
*, int, int);
353 static void tsetscroll(int, int);
354 static void tswapscreen(void);
355 static void tsetdirt(int, int);
356 static void tsetdirtattr(int);
357 static void tsetmode(bool, bool, int *, int);
358 static void tfulldirt(void);
359 static void techo(char *, int);
361 static inline bool match(uint
, uint
);
362 static void ttynew(void);
363 static void ttyread(void);
364 static void ttyresize(void);
365 static void ttywrite(const char *, size_t);
367 static void xdraws(char *, Glyph
, int, int, int, int);
368 static void xhints(void);
369 static void xclear(int, int, int, int);
370 static void xdrawcursor(void);
371 static void xinit(void);
372 static void xloadcols(void);
373 static int xsetcolorname(int, const char *);
374 static int xloadfont(Font
*, FcPattern
*);
375 static void xloadfonts(char *, int);
376 static int xloadfontset(Font
*);
377 static void xsettitle(char *);
378 static void xresettitle(void);
379 static void xsetpointermotion(int);
380 static void xseturgency(int);
381 static void xsetsel(char*);
382 static void xtermclear(int, int, int, int);
383 static void xunloadfont(Font
*f
);
384 static void xunloadfonts(void);
385 static void xresize(int, int);
387 static void expose(XEvent
*);
388 static void visibility(XEvent
*);
389 static void unmap(XEvent
*);
390 static char *kmap(KeySym
, uint
);
391 static void kpress(XEvent
*);
392 static void cmessage(XEvent
*);
393 static void cresize(int, int);
394 static void resize(XEvent
*);
395 static void focus(XEvent
*);
396 static void brelease(XEvent
*);
397 static void bpress(XEvent
*);
398 static void bmotion(XEvent
*);
399 static void selnotify(XEvent
*);
400 static void selclear(XEvent
*);
401 static void selrequest(XEvent
*);
403 static void selinit(void);
404 static void selsort(void);
405 static inline bool selected(int, int);
406 static void selcopy(void);
407 static void selscroll(int, int);
408 static void selsnap(int, int *, int *, int);
410 static int utf8decode(char *, long *);
411 static int utf8encode(long *, char *);
412 static int utf8size(char *);
413 static int isfullutf8(char *, int);
415 static ssize_t
xwrite(int, char *, size_t);
416 static void *xmalloc(size_t);
417 static void *xrealloc(void *, size_t);
418 static void *xcalloc(size_t, size_t);
420 static void (*handler
[LASTEvent
])(XEvent
*) = {
422 [ClientMessage
] = cmessage
,
423 [ConfigureNotify
] = resize
,
424 [VisibilityNotify
] = visibility
,
425 [UnmapNotify
] = unmap
,
429 [MotionNotify
] = bmotion
,
430 [ButtonPress
] = bpress
,
431 [ButtonRelease
] = brelease
,
432 [SelectionClear
] = selclear
,
433 [SelectionNotify
] = selnotify
,
434 [SelectionRequest
] = selrequest
,
441 static CSIEscape csiescseq
;
442 static STREscape strescseq
;
445 static Selection sel
;
446 static int iofd
= -1;
447 static char **opt_cmd
= NULL
;
448 static char *opt_io
= NULL
;
449 static char *opt_title
= NULL
;
450 static char *opt_embed
= NULL
;
451 static char *opt_class
= NULL
;
452 static char *opt_font
= NULL
;
453 static int oldbutton
= 3; /* button event on startup: 3 = release */
455 static char *usedfont
= NULL
;
456 static int usedfontsize
= 0;
458 /* Font Ring Cache */
473 * Fontcache is a ring buffer, with frccur as current position and frclen as
474 * the current length of used elements.
477 static Fontcache frc
[1024];
478 static int frccur
= -1, frclen
= 0;
481 xwrite(int fd
, char *s
, size_t len
) {
485 ssize_t r
= write(fd
, s
, len
);
495 xmalloc(size_t len
) {
496 void *p
= malloc(len
);
499 die("Out of memory\n");
505 xrealloc(void *p
, size_t len
) {
506 if((p
= realloc(p
, len
)) == NULL
)
507 die("Out of memory\n");
513 xcalloc(size_t nmemb
, size_t size
) {
514 void *p
= calloc(nmemb
, size
);
517 die("Out of memory\n");
523 utf8decode(char *s
, long *u
) {
529 if(~c
& B7
) { /* 0xxxxxxx */
532 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
533 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
535 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
536 *u
= c
&(B3
|B2
|B1
|B0
);
538 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
545 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
547 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
550 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
553 if((n
== 1 && *u
< 0x80) ||
554 (n
== 2 && *u
< 0x800) ||
555 (n
== 3 && *u
< 0x10000) ||
556 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
568 utf8encode(long *u
, char *s
) {
576 *sp
= uc
; /* 0xxxxxxx */
578 } else if(*u
< 0x800) {
579 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
581 } else if(uc
< 0x10000) {
582 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
584 } else if(uc
<= 0x10FFFF) {
585 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
591 for(i
=n
,++sp
; i
>0; --i
,++sp
)
592 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
604 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
605 UTF-8 otherwise return 0 */
607 isfullutf8(char *s
, int b
) {
615 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
617 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
619 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
621 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
623 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
624 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
637 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
639 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
648 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
649 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
653 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
654 if(sel
.xtarget
== None
)
655 sel
.xtarget
= XA_STRING
;
663 return LIMIT(x
, 0, term
.col
-1);
671 return LIMIT(y
, 0, term
.row
-1);
676 if(sel
.ob
.y
== sel
.oe
.y
) {
677 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
678 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
680 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
681 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
683 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
684 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
688 selected(int x
, int y
) {
689 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
690 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
692 if(sel
.type
== SEL_RECTANGULAR
) {
693 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
694 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
697 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
698 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
699 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
700 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
704 selsnap(int mode
, int *x
, int *y
, int direction
) {
710 * Snap around if the word wraps around at the end or
711 * beginning of a line.
714 if(direction
< 0 && *x
<= 0) {
715 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
723 if(direction
> 0 && *x
>= term
.col
-1) {
724 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
733 if(strchr(worddelimiters
,
734 term
.line
[*y
][*x
+ direction
].c
[0])) {
743 * Snap around if the the previous line or the current one
744 * has set ATTR_WRAP at its end. Then the whole next or
745 * previous line will be selected.
747 *x
= (direction
< 0) ? 0 : term
.col
- 1;
748 if(direction
< 0 && *y
> 0) {
749 for(; *y
> 0; *y
+= direction
) {
750 if(!(term
.line
[*y
-1][term
.col
-1].mode
755 } else if(direction
> 0 && *y
< term
.row
-1) {
756 for(; *y
< term
.row
; *y
+= direction
) {
757 if(!(term
.line
[*y
][term
.col
-1].mode
766 * Select the whole line when the end of line is reached.
770 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
780 getbuttoninfo(XEvent
*e
) {
782 uint state
= e
->xbutton
.state
&~Button1Mask
;
784 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
786 sel
.oe
.x
= x2col(e
->xbutton
.x
);
787 sel
.oe
.y
= y2row(e
->xbutton
.y
);
789 if(sel
.ob
.y
< sel
.oe
.y
790 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
791 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
792 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
794 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
795 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
799 sel
.type
= SEL_REGULAR
;
800 for(type
= 1; type
< LEN(selmasks
); ++type
) {
801 if(match(selmasks
[type
], state
)) {
809 mousereport(XEvent
*e
) {
810 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
811 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
817 if(e
->xbutton
.type
== MotionNotify
) {
818 if(x
== ox
&& y
== oy
)
820 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
822 /* MOUSE_MOTION: no reporting if no button is pressed */
823 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
826 button
= oldbutton
+ 32;
829 } else if(!IS_SET(MODE_MOUSESGR
)
830 && (e
->xbutton
.type
== ButtonRelease
831 || button
== AnyButton
)) {
837 if(e
->xbutton
.type
== ButtonPress
) {
844 if(!IS_SET(MODE_MOUSEX10
)) {
845 button
+= (state
& ShiftMask
? 4 : 0)
846 + (state
& Mod4Mask
? 8 : 0)
847 + (state
& ControlMask
? 16 : 0);
851 if(IS_SET(MODE_MOUSESGR
)) {
852 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
854 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
855 } else if(x
< 223 && y
< 223) {
856 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
857 IS_SET(MODE_MOUSEX10
)? button
-1 : 32+button
,
871 if(IS_SET(MODE_MOUSE
)) {
876 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
877 if(e
->xbutton
.button
== mk
->b
878 && match(mk
->mask
, e
->xbutton
.state
)) {
879 ttywrite(mk
->s
, strlen(mk
->s
));
880 if(IS_SET(MODE_ECHO
))
881 techo(mk
->s
, strlen(mk
->s
));
886 if(e
->xbutton
.button
== Button1
) {
887 gettimeofday(&now
, NULL
);
889 /* Clear previous selection, logically and visually. */
892 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
896 sel
.type
= SEL_REGULAR
;
897 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
898 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
901 * If the user clicks below predefined timeouts specific
902 * snapping behaviour is exposed.
904 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
905 sel
.snap
= SNAP_LINE
;
906 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
907 sel
.snap
= SNAP_WORD
;
911 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
912 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
916 * Draw selection, unless it's regular and we don't want to
917 * make clicks visible
921 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
924 sel
.tclick2
= sel
.tclick1
;
932 int x
, y
, bufsize
, size
, i
, ex
;
938 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
939 ptr
= str
= xmalloc(bufsize
);
941 /* append every set & selected glyph to the selection */
942 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
943 gp
= &term
.line
[y
][0];
944 last
= gp
+ term
.col
;
946 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
947 strcmp(last
->c
, " ") != 0))
950 for(x
= 0; gp
<= last
; x
++, ++gp
) {
954 size
= utf8size(gp
->c
);
955 memcpy(ptr
, gp
->c
, size
);
960 * Copy and pasting of line endings is inconsistent
961 * in the inconsistent terminal and GUI world.
962 * The best solution seems like to produce '\n' when
963 * something is copied from st and convert '\n' to
964 * '\r', when something to be pasted is received by
966 * FIXME: Fix the computer world.
968 if(y
< sel
.ne
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
972 * If the last selected line expands in the selection
973 * after the visible text '\n' is appended.
977 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
980 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
992 selnotify(XEvent
*e
) {
993 ulong nitems
, ofs
, rem
;
995 uchar
*data
, *last
, *repl
;
1000 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
1001 False
, AnyPropertyType
, &type
, &format
,
1002 &nitems
, &rem
, &data
)) {
1003 fprintf(stderr
, "Clipboard allocation failed\n");
1008 * As seen in selcopy:
1009 * Line endings are inconsistent in the terminal and GUI world
1010 * copy and pasting. When receiving some selection data,
1011 * replace all '\n' with '\r'.
1012 * FIXME: Fix the computer world.
1015 last
= data
+ nitems
* format
/ 8;
1016 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1020 ttywrite((const char *)data
, nitems
* format
/ 8);
1022 /* number of 32-bit chunks returned */
1023 ofs
+= nitems
* format
/ 32;
1028 selpaste(const Arg
*dummy
) {
1029 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1030 xw
.win
, CurrentTime
);
1034 clippaste(const Arg
*dummy
) {
1037 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1038 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1039 xw
.win
, CurrentTime
);
1043 selclear(XEvent
*e
) {
1047 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1051 selrequest(XEvent
*e
) {
1052 XSelectionRequestEvent
*xsre
;
1053 XSelectionEvent xev
;
1054 Atom xa_targets
, string
;
1056 xsre
= (XSelectionRequestEvent
*) e
;
1057 xev
.type
= SelectionNotify
;
1058 xev
.requestor
= xsre
->requestor
;
1059 xev
.selection
= xsre
->selection
;
1060 xev
.target
= xsre
->target
;
1061 xev
.time
= xsre
->time
;
1063 xev
.property
= None
;
1065 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1066 if(xsre
->target
== xa_targets
) {
1067 /* respond with the supported type */
1068 string
= sel
.xtarget
;
1069 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1070 XA_ATOM
, 32, PropModeReplace
,
1071 (uchar
*) &string
, 1);
1072 xev
.property
= xsre
->property
;
1073 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1074 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1075 xsre
->target
, 8, PropModeReplace
,
1076 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1077 xev
.property
= xsre
->property
;
1080 /* all done, send a notification to the listener */
1081 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1082 fprintf(stderr
, "Error sending SelectionNotify event\n");
1086 xsetsel(char *str
) {
1087 /* register the selection for both the clipboard and the primary */
1093 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1095 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1096 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1100 brelease(XEvent
*e
) {
1101 if(IS_SET(MODE_MOUSE
)) {
1106 if(e
->xbutton
.button
== Button2
) {
1108 } else if(e
->xbutton
.button
== Button1
) {
1116 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1121 bmotion(XEvent
*e
) {
1122 int oldey
, oldex
, oldsby
, oldsey
;
1124 if(IS_SET(MODE_MOUSE
)) {
1139 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1140 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1144 die(const char *errstr
, ...) {
1147 va_start(ap
, errstr
);
1148 vfprintf(stderr
, errstr
, ap
);
1156 char *envshell
= getenv("SHELL");
1157 const struct passwd
*pass
= getpwuid(getuid());
1158 char buf
[sizeof(long) * 8 + 1];
1160 unsetenv("COLUMNS");
1162 unsetenv("TERMCAP");
1165 setenv("LOGNAME", pass
->pw_name
, 1);
1166 setenv("USER", pass
->pw_name
, 1);
1167 setenv("SHELL", pass
->pw_shell
, 0);
1168 setenv("HOME", pass
->pw_dir
, 0);
1171 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1172 setenv("WINDOWID", buf
, 1);
1174 signal(SIGCHLD
, SIG_DFL
);
1175 signal(SIGHUP
, SIG_DFL
);
1176 signal(SIGINT
, SIG_DFL
);
1177 signal(SIGQUIT
, SIG_DFL
);
1178 signal(SIGTERM
, SIG_DFL
);
1179 signal(SIGALRM
, SIG_DFL
);
1181 DEFAULT(envshell
, shell
);
1182 setenv("TERM", termname
, 1);
1183 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1184 execvp(args
[0], args
);
1192 if(waitpid(pid
, &stat
, 0) < 0)
1193 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1195 if(WIFEXITED(stat
)) {
1196 exit(WEXITSTATUS(stat
));
1205 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1207 /* seems to work fine on linux, openbsd and freebsd */
1208 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1209 die("openpty failed: %s\n", SERRNO
);
1211 switch(pid
= fork()) {
1213 die("fork failed\n");
1216 setsid(); /* create a new process group */
1217 dup2(s
, STDIN_FILENO
);
1218 dup2(s
, STDOUT_FILENO
);
1219 dup2(s
, STDERR_FILENO
);
1220 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1221 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1229 signal(SIGCHLD
, sigchld
);
1231 iofd
= (!strcmp(opt_io
, "-")) ?
1233 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1235 fprintf(stderr
, "Error opening %s:%s\n",
1236 opt_io
, strerror(errno
));
1246 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1248 fprintf(stderr
, "\n");
1253 static char buf
[BUFSIZ
];
1254 static int buflen
= 0;
1257 int charsize
; /* size of utf8 char in bytes */
1261 /* append read bytes to unprocessed bytes */
1262 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1263 die("Couldn't read from shell: %s\n", SERRNO
);
1265 /* process every complete utf8 char */
1268 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1269 charsize
= utf8decode(ptr
, &utf8c
);
1270 utf8encode(&utf8c
, s
);
1276 /* keep any uncomplete utf8 char for the next call */
1277 memmove(buf
, ptr
, buflen
);
1281 ttywrite(const char *s
, size_t n
) {
1282 if(write(cmdfd
, s
, n
) == -1)
1283 die("write error on tty: %s\n", SERRNO
);
1290 w
.ws_row
= term
.row
;
1291 w
.ws_col
= term
.col
;
1292 w
.ws_xpixel
= xw
.tw
;
1293 w
.ws_ypixel
= xw
.th
;
1294 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1295 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1299 tattrset(int attr
) {
1302 for(i
= 0; i
< term
.row
-1; i
++) {
1303 for(j
= 0; j
< term
.col
-1; j
++) {
1304 if(term
.line
[i
][j
].mode
& attr
)
1313 tsetdirt(int top
, int bot
) {
1316 LIMIT(top
, 0, term
.row
-1);
1317 LIMIT(bot
, 0, term
.row
-1);
1319 for(i
= top
; i
<= bot
; i
++)
1324 tsetdirtattr(int attr
) {
1327 for(i
= 0; i
< term
.row
-1; i
++) {
1328 for(j
= 0; j
< term
.col
-1; j
++) {
1329 if(term
.line
[i
][j
].mode
& attr
) {
1339 tsetdirt(0, term
.row
-1);
1346 if(mode
== CURSOR_SAVE
) {
1348 } else if(mode
== CURSOR_LOAD
) {
1358 term
.c
= (TCursor
){{
1362 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1364 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1365 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1368 term
.bot
= term
.row
- 1;
1369 term
.mode
= MODE_WRAP
;
1371 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1373 tcursor(CURSOR_SAVE
);
1377 tnew(int col
, int row
) {
1378 memset(&term
, 0, sizeof(Term
));
1387 Line
*tmp
= term
.line
;
1389 term
.line
= term
.alt
;
1391 term
.mode
^= MODE_ALTSCREEN
;
1396 tscrolldown(int orig
, int n
) {
1400 LIMIT(n
, 0, term
.bot
-orig
+1);
1402 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1404 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1405 temp
= term
.line
[i
];
1406 term
.line
[i
] = term
.line
[i
-n
];
1407 term
.line
[i
-n
] = temp
;
1410 term
.dirty
[i
-n
] = 1;
1417 tscrollup(int orig
, int n
) {
1420 LIMIT(n
, 0, term
.bot
-orig
+1);
1422 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1424 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1425 temp
= term
.line
[i
];
1426 term
.line
[i
] = term
.line
[i
+n
];
1427 term
.line
[i
+n
] = temp
;
1430 term
.dirty
[i
+n
] = 1;
1433 selscroll(orig
, -n
);
1437 selscroll(int orig
, int n
) {
1441 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1442 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1446 if(sel
.type
== SEL_RECTANGULAR
) {
1447 if(sel
.ob
.y
< term
.top
)
1448 sel
.ob
.y
= term
.top
;
1449 if(sel
.oe
.y
> term
.bot
)
1450 sel
.oe
.y
= term
.bot
;
1452 if(sel
.ob
.y
< term
.top
) {
1453 sel
.ob
.y
= term
.top
;
1456 if(sel
.oe
.y
> term
.bot
) {
1457 sel
.oe
.y
= term
.bot
;
1458 sel
.oe
.x
= term
.col
;
1466 tnewline(int first_col
) {
1470 tscrollup(term
.top
, 1);
1474 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1479 char *p
= csiescseq
.buf
, *np
;
1488 csiescseq
.buf
[csiescseq
.len
] = '\0';
1489 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1491 v
= strtol(p
, &np
, 10);
1494 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1496 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1498 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1502 csiescseq
.mode
= *p
;
1505 /* for absolute user moves, when decom is set */
1507 tmoveato(int x
, int y
) {
1508 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1512 tmoveto(int x
, int y
) {
1515 if(term
.c
.state
& CURSOR_ORIGIN
) {
1520 maxy
= term
.row
- 1;
1522 LIMIT(x
, 0, term
.col
-1);
1523 LIMIT(y
, miny
, maxy
);
1524 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1530 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1531 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1532 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1533 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1534 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1535 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1536 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1537 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1538 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1539 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1543 * The table is proudly stolen from rxvt.
1545 if(attr
->mode
& ATTR_GFX
) {
1546 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1547 && vt100_0
[c
[0] - 0x41]) {
1548 c
= vt100_0
[c
[0] - 0x41];
1553 term
.line
[y
][x
] = *attr
;
1554 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1558 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1562 temp
= x1
, x1
= x2
, x2
= temp
;
1564 temp
= y1
, y1
= y2
, y2
= temp
;
1566 LIMIT(x1
, 0, term
.col
-1);
1567 LIMIT(x2
, 0, term
.col
-1);
1568 LIMIT(y1
, 0, term
.row
-1);
1569 LIMIT(y2
, 0, term
.row
-1);
1571 for(y
= y1
; y
<= y2
; y
++) {
1573 for(x
= x1
; x
<= x2
; x
++) {
1576 term
.line
[y
][x
] = term
.c
.attr
;
1577 memcpy(term
.line
[y
][x
].c
, " ", 2);
1583 tdeletechar(int n
) {
1584 int src
= term
.c
.x
+ n
;
1586 int size
= term
.col
- src
;
1588 term
.dirty
[term
.c
.y
] = 1;
1590 if(src
>= term
.col
) {
1591 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1595 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1596 size
* sizeof(Glyph
));
1597 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1601 tinsertblank(int n
) {
1604 int size
= term
.col
- dst
;
1606 term
.dirty
[term
.c
.y
] = 1;
1608 if(dst
>= term
.col
) {
1609 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1613 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1614 size
* sizeof(Glyph
));
1615 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1619 tinsertblankline(int n
) {
1620 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1623 tscrolldown(term
.c
.y
, n
);
1627 tdeleteline(int n
) {
1628 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1631 tscrollup(term
.c
.y
, n
);
1635 tsetattr(int *attr
, int l
) {
1638 for(i
= 0; i
< l
; i
++) {
1641 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1642 | ATTR_BOLD
| ATTR_ITALIC \
1644 term
.c
.attr
.fg
= defaultfg
;
1645 term
.c
.attr
.bg
= defaultbg
;
1648 term
.c
.attr
.mode
|= ATTR_BOLD
;
1651 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1654 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1656 case 5: /* slow blink */
1657 case 6: /* rapid blink */
1658 term
.c
.attr
.mode
|= ATTR_BLINK
;
1661 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1665 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1668 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1671 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1675 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1678 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1681 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1683 if(BETWEEN(attr
[i
], 0, 255)) {
1684 term
.c
.attr
.fg
= attr
[i
];
1687 "erresc: bad fgcolor %d\n",
1692 "erresc(38): gfx attr %d unknown\n",
1697 term
.c
.attr
.fg
= defaultfg
;
1700 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1702 if(BETWEEN(attr
[i
], 0, 255)) {
1703 term
.c
.attr
.bg
= attr
[i
];
1706 "erresc: bad bgcolor %d\n",
1711 "erresc(48): gfx attr %d unknown\n",
1716 term
.c
.attr
.bg
= defaultbg
;
1719 if(BETWEEN(attr
[i
], 30, 37)) {
1720 term
.c
.attr
.fg
= attr
[i
] - 30;
1721 } else if(BETWEEN(attr
[i
], 40, 47)) {
1722 term
.c
.attr
.bg
= attr
[i
] - 40;
1723 } else if(BETWEEN(attr
[i
], 90, 97)) {
1724 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1725 } else if(BETWEEN(attr
[i
], 100, 107)) {
1726 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1729 "erresc(default): gfx attr %d unknown\n",
1730 attr
[i
]), csidump();
1738 tsetscroll(int t
, int b
) {
1741 LIMIT(t
, 0, term
.row
-1);
1742 LIMIT(b
, 0, term
.row
-1);
1752 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1755 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1759 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1763 case 1: /* DECCKM -- Cursor key */
1764 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1766 case 5: /* DECSCNM -- Reverse video */
1768 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1769 if(mode
!= term
.mode
)
1770 redraw(REDRAW_TIMEOUT
);
1772 case 6: /* DECOM -- Origin */
1773 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1776 case 7: /* DECAWM -- Auto wrap */
1777 MODBIT(term
.mode
, set
, MODE_WRAP
);
1779 case 0: /* Error (IGNORED) */
1780 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1781 case 3: /* DECCOLM -- Column (IGNORED) */
1782 case 4: /* DECSCLM -- Scroll (IGNORED) */
1783 case 8: /* DECARM -- Auto repeat (IGNORED) */
1784 case 18: /* DECPFF -- Printer feed (IGNORED) */
1785 case 19: /* DECPEX -- Printer extent (IGNORED) */
1786 case 42: /* DECNRCM -- National characters (IGNORED) */
1787 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1789 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1790 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1792 case 9: /* X10 mouse compatibility mode */
1793 xsetpointermotion(0);
1794 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1795 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1797 case 1000: /* 1000: report button press */
1798 xsetpointermotion(0);
1799 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1800 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1802 case 1002: /* 1002: report motion on button press */
1803 xsetpointermotion(0);
1804 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1805 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1807 case 1003: /* 1003: enable all mouse motions */
1808 xsetpointermotion(set
);
1809 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1810 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1812 case 1004: /* 1004: send focus events to tty */
1813 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1815 case 1006: /* 1006: extended reporting mode */
1816 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1819 MODBIT(term
.mode
, set
, MODE_8BIT
);
1821 case 1049: /* = 1047 and 1048 */
1824 if (!allowaltscreen
)
1827 alt
= IS_SET(MODE_ALTSCREEN
);
1829 tclearregion(0, 0, term
.col
-1,
1832 if(set
^ alt
) /* set is always 1 or 0 */
1838 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1840 /* Not implemented mouse modes. See comments there. */
1841 case 1001: /* mouse highlight mode; can hang the
1842 terminal by design when implemented. */
1843 case 1005: /* UTF-8 mouse mode; will confuse
1844 applications not supporting UTF-8
1846 case 1015: /* urxvt mangled mouse mode; incompatible
1847 and can be mistaken for other control
1851 "erresc: unknown private set/reset mode %d\n",
1857 case 0: /* Error (IGNORED) */
1859 case 2: /* KAM -- keyboard action */
1860 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1862 case 4: /* IRM -- Insertion-replacement */
1863 MODBIT(term
.mode
, set
, MODE_INSERT
);
1865 case 12: /* SRM -- Send/Receive */
1866 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1868 case 20: /* LNM -- Linefeed/new line */
1869 MODBIT(term
.mode
, set
, MODE_CRLF
);
1873 "erresc: unknown set/reset mode %d\n",
1883 switch(csiescseq
.mode
) {
1886 fprintf(stderr
, "erresc: unknown csi ");
1890 case '@': /* ICH -- Insert <n> blank char */
1891 DEFAULT(csiescseq
.arg
[0], 1);
1892 tinsertblank(csiescseq
.arg
[0]);
1894 case 'A': /* CUU -- Cursor <n> Up */
1895 DEFAULT(csiescseq
.arg
[0], 1);
1896 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1898 case 'B': /* CUD -- Cursor <n> Down */
1899 case 'e': /* VPR --Cursor <n> Down */
1900 DEFAULT(csiescseq
.arg
[0], 1);
1901 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1903 case 'c': /* DA -- Device Attributes */
1904 if(csiescseq
.arg
[0] == 0)
1905 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1907 case 'C': /* CUF -- Cursor <n> Forward */
1908 case 'a': /* HPR -- Cursor <n> Forward */
1909 DEFAULT(csiescseq
.arg
[0], 1);
1910 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1912 case 'D': /* CUB -- Cursor <n> Backward */
1913 DEFAULT(csiescseq
.arg
[0], 1);
1914 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1916 case 'E': /* CNL -- Cursor <n> Down and first col */
1917 DEFAULT(csiescseq
.arg
[0], 1);
1918 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1920 case 'F': /* CPL -- Cursor <n> Up and first col */
1921 DEFAULT(csiescseq
.arg
[0], 1);
1922 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1924 case 'g': /* TBC -- Tabulation clear */
1925 switch(csiescseq
.arg
[0]) {
1926 case 0: /* clear current tab stop */
1927 term
.tabs
[term
.c
.x
] = 0;
1929 case 3: /* clear all the tabs */
1930 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1936 case 'G': /* CHA -- Move to <col> */
1938 DEFAULT(csiescseq
.arg
[0], 1);
1939 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1941 case 'H': /* CUP -- Move to <row> <col> */
1943 DEFAULT(csiescseq
.arg
[0], 1);
1944 DEFAULT(csiescseq
.arg
[1], 1);
1945 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1947 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1948 DEFAULT(csiescseq
.arg
[0], 1);
1949 while(csiescseq
.arg
[0]--)
1952 case 'J': /* ED -- Clear screen */
1954 switch(csiescseq
.arg
[0]) {
1956 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1957 if(term
.c
.y
< term
.row
-1) {
1958 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1964 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1965 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1968 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1974 case 'K': /* EL -- Clear line */
1975 switch(csiescseq
.arg
[0]) {
1977 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1981 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1984 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1988 case 'S': /* SU -- Scroll <n> line up */
1989 DEFAULT(csiescseq
.arg
[0], 1);
1990 tscrollup(term
.top
, csiescseq
.arg
[0]);
1992 case 'T': /* SD -- Scroll <n> line down */
1993 DEFAULT(csiescseq
.arg
[0], 1);
1994 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1996 case 'L': /* IL -- Insert <n> blank lines */
1997 DEFAULT(csiescseq
.arg
[0], 1);
1998 tinsertblankline(csiescseq
.arg
[0]);
2000 case 'l': /* RM -- Reset Mode */
2001 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2003 case 'M': /* DL -- Delete <n> lines */
2004 DEFAULT(csiescseq
.arg
[0], 1);
2005 tdeleteline(csiescseq
.arg
[0]);
2007 case 'X': /* ECH -- Erase <n> char */
2008 DEFAULT(csiescseq
.arg
[0], 1);
2009 tclearregion(term
.c
.x
, term
.c
.y
,
2010 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2012 case 'P': /* DCH -- Delete <n> char */
2013 DEFAULT(csiescseq
.arg
[0], 1);
2014 tdeletechar(csiescseq
.arg
[0]);
2016 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2017 DEFAULT(csiescseq
.arg
[0], 1);
2018 while(csiescseq
.arg
[0]--)
2021 case 'd': /* VPA -- Move to <row> */
2022 DEFAULT(csiescseq
.arg
[0], 1);
2023 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2025 case 'h': /* SM -- Set terminal mode */
2026 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2028 case 'm': /* SGR -- Terminal attribute (color) */
2029 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2031 case 'r': /* DECSTBM -- Set Scrolling Region */
2032 if(csiescseq
.priv
) {
2035 DEFAULT(csiescseq
.arg
[0], 1);
2036 DEFAULT(csiescseq
.arg
[1], term
.row
);
2037 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2041 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2042 tcursor(CURSOR_SAVE
);
2044 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2045 tcursor(CURSOR_LOAD
);
2056 for(i
= 0; i
< csiescseq
.len
; i
++) {
2057 c
= csiescseq
.buf
[i
] & 0xff;
2060 } else if(c
== '\n') {
2062 } else if(c
== '\r') {
2064 } else if(c
== 0x1b) {
2067 printf("(%02x)", c
);
2075 memset(&csiescseq
, 0, sizeof(csiescseq
));
2084 narg
= strescseq
.narg
;
2086 switch(strescseq
.type
) {
2087 case ']': /* OSC -- Operating System Command */
2088 switch(i
= atoi(strescseq
.args
[0])) {
2093 xsettitle(strescseq
.args
[1]);
2095 case 4: /* color set */
2098 p
= strescseq
.args
[2];
2100 case 104: /* color reset, here p = NULL */
2101 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2102 if (!xsetcolorname(j
, p
)) {
2103 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2106 * TODO if defaultbg color is changed, borders
2113 fprintf(stderr
, "erresc: unknown str ");
2118 case 'k': /* old title set compatibility */
2119 xsettitle(strescseq
.args
[0]);
2121 case 'P': /* DSC -- Device Control String */
2122 case '_': /* APC -- Application Program Command */
2123 case '^': /* PM -- Privacy Message */
2125 fprintf(stderr
, "erresc: unknown str ");
2134 char *p
= strescseq
.buf
;
2137 strescseq
.buf
[strescseq
.len
] = '\0';
2138 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2139 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2147 printf("ESC%c", strescseq
.type
);
2148 for(i
= 0; i
< strescseq
.len
; i
++) {
2149 c
= strescseq
.buf
[i
] & 0xff;
2152 } else if(isprint(c
)) {
2154 } else if(c
== '\n') {
2156 } else if(c
== '\r') {
2158 } else if(c
== 0x1b) {
2161 printf("(%02x)", c
);
2169 memset(&strescseq
, 0, sizeof(strescseq
));
2173 tputtab(bool forward
) {
2179 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2184 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2187 tmoveto(x
, term
.c
.y
);
2191 techo(char *buf
, int len
) {
2192 for(; len
> 0; buf
++, len
--) {
2195 if(c
== '\033') { /* escape */
2198 } else if(c
< '\x20') { /* control code */
2199 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2213 tputc(char *c
, int len
) {
2215 bool control
= ascii
< '\x20' || ascii
== 0177;
2218 if(xwrite(iofd
, c
, len
) < 0) {
2219 fprintf(stderr
, "Error writing in %s:%s\n",
2220 opt_io
, strerror(errno
));
2227 * STR sequences must be checked before anything else
2228 * because it can use some control codes as part of the sequence.
2230 if(term
.esc
& ESC_STR
) {
2233 term
.esc
= ESC_START
| ESC_STR_END
;
2235 case '\a': /* backwards compatibility to xterm */
2240 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2241 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2242 strescseq
.len
+= len
;
2245 * Here is a bug in terminals. If the user never sends
2246 * some code to stop the str or esc command, then st
2247 * will stop responding. But this is better than
2248 * silently failing with unknown characters. At least
2249 * then users will report back.
2251 * In the case users ever get fixed, here is the code:
2263 * Actions of control codes must be performed as soon they arrive
2264 * because they can be embedded inside a control sequence, and
2265 * they must not cause conflicts with sequences.
2273 tmoveto(term
.c
.x
-1, term
.c
.y
);
2276 tmoveto(0, term
.c
.y
);
2281 /* go to first col if the mode is set */
2282 tnewline(IS_SET(MODE_CRLF
));
2284 case '\a': /* BEL */
2285 if(!(xw
.state
& WIN_FOCUSED
))
2288 case '\033': /* ESC */
2290 term
.esc
= ESC_START
;
2292 case '\016': /* SO */
2293 case '\017': /* SI */
2295 * Different charsets are hard to handle. Applications
2296 * should use the right alt charset escapes for the
2297 * only reason they still exist: line drawing. The
2298 * rest is incompatible history st should not support.
2301 case '\032': /* SUB */
2302 case '\030': /* CAN */
2305 case '\005': /* ENQ (IGNORED) */
2306 case '\000': /* NUL (IGNORED) */
2307 case '\021': /* XON (IGNORED) */
2308 case '\023': /* XOFF (IGNORED) */
2309 case 0177: /* DEL (IGNORED) */
2312 } else if(term
.esc
& ESC_START
) {
2313 if(term
.esc
& ESC_CSI
) {
2314 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2315 if(BETWEEN(ascii
, 0x40, 0x7E)
2316 || csiescseq
.len
>= \
2317 sizeof(csiescseq
.buf
)-1) {
2322 } else if(term
.esc
& ESC_STR_END
) {
2326 } else if(term
.esc
& ESC_ALTCHARSET
) {
2328 case '0': /* Line drawing set */
2329 term
.c
.attr
.mode
|= ATTR_GFX
;
2331 case 'B': /* USASCII */
2332 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2334 case 'A': /* UK (IGNORED) */
2335 case '<': /* multinational charset (IGNORED) */
2336 case '5': /* Finnish (IGNORED) */
2337 case 'C': /* Finnish (IGNORED) */
2338 case 'K': /* German (IGNORED) */
2341 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2344 } else if(term
.esc
& ESC_TEST
) {
2345 if(ascii
== '8') { /* DEC screen alignment test. */
2346 char E
[UTF_SIZ
] = "E";
2349 for(x
= 0; x
< term
.col
; ++x
) {
2350 for(y
= 0; y
< term
.row
; ++y
)
2351 tsetchar(E
, &term
.c
.attr
, x
, y
);
2358 term
.esc
|= ESC_CSI
;
2361 term
.esc
|= ESC_TEST
;
2363 case 'P': /* DCS -- Device Control String */
2364 case '_': /* APC -- Application Program Command */
2365 case '^': /* PM -- Privacy Message */
2366 case ']': /* OSC -- Operating System Command */
2367 case 'k': /* old title set compatibility */
2369 strescseq
.type
= ascii
;
2370 term
.esc
|= ESC_STR
;
2372 case '(': /* set primary charset G0 */
2373 term
.esc
|= ESC_ALTCHARSET
;
2375 case ')': /* set secondary charset G1 (IGNORED) */
2376 case '*': /* set tertiary charset G2 (IGNORED) */
2377 case '+': /* set quaternary charset G3 (IGNORED) */
2380 case 'D': /* IND -- Linefeed */
2381 if(term
.c
.y
== term
.bot
) {
2382 tscrollup(term
.top
, 1);
2384 tmoveto(term
.c
.x
, term
.c
.y
+1);
2388 case 'E': /* NEL -- Next line */
2389 tnewline(1); /* always go to first col */
2392 case 'H': /* HTS -- Horizontal tab stop */
2393 term
.tabs
[term
.c
.x
] = 1;
2396 case 'M': /* RI -- Reverse index */
2397 if(term
.c
.y
== term
.top
) {
2398 tscrolldown(term
.top
, 1);
2400 tmoveto(term
.c
.x
, term
.c
.y
-1);
2404 case 'Z': /* DECID -- Identify Terminal */
2405 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2408 case 'c': /* RIS -- Reset to inital state */
2413 case '=': /* DECPAM -- Application keypad */
2414 term
.mode
|= MODE_APPKEYPAD
;
2417 case '>': /* DECPNM -- Normal keypad */
2418 term
.mode
&= ~MODE_APPKEYPAD
;
2421 case '7': /* DECSC -- Save Cursor */
2422 tcursor(CURSOR_SAVE
);
2425 case '8': /* DECRC -- Restore Cursor */
2426 tcursor(CURSOR_LOAD
);
2429 case '\\': /* ST -- Stop */
2433 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2434 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2439 * All characters which form part of a sequence are not
2445 * Display control codes only if we are in graphic mode
2447 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2449 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2451 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2452 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2456 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2457 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2458 &term
.line
[term
.c
.y
][term
.c
.x
],
2459 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2462 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2463 if(term
.c
.x
+1 < term
.col
) {
2464 tmoveto(term
.c
.x
+1, term
.c
.y
);
2466 term
.c
.state
|= CURSOR_WRAPNEXT
;
2471 tresize(int col
, int row
) {
2473 int minrow
= MIN(row
, term
.row
);
2474 int mincol
= MIN(col
, term
.col
);
2475 int slide
= term
.c
.y
- row
+ 1;
2479 if(col
< 1 || row
< 1)
2482 /* free unneeded rows */
2486 * slide screen to keep cursor where we expect it -
2487 * tscrollup would work here, but we can optimize to
2488 * memmove because we're freeing the earlier lines
2490 for(/* i = 0 */; i
< slide
; i
++) {
2494 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2495 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2497 for(i
+= row
; i
< term
.row
; i
++) {
2502 /* resize to new height */
2503 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2504 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2505 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2506 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2508 /* resize each row to new width, zero-pad if needed */
2509 for(i
= 0; i
< minrow
; i
++) {
2511 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2512 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2515 /* allocate any new rows */
2516 for(/* i == minrow */; i
< row
; i
++) {
2518 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2519 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2521 if(col
> term
.col
) {
2522 bp
= term
.tabs
+ term
.col
;
2524 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2525 while(--bp
> term
.tabs
&& !*bp
)
2527 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2530 /* update terminal size */
2533 /* reset scrolling region */
2534 tsetscroll(0, row
-1);
2535 /* make use of the LIMIT in tmoveto */
2536 tmoveto(term
.c
.x
, term
.c
.y
);
2537 /* Clearing both screens */
2540 if(mincol
< col
&& 0 < minrow
) {
2541 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2543 if(0 < col
&& minrow
< row
) {
2544 tclearregion(0, minrow
, col
- 1, row
- 1);
2547 } while(orig
!= term
.line
);
2553 xresize(int col
, int row
) {
2554 xw
.tw
= MAX(1, col
* xw
.cw
);
2555 xw
.th
= MAX(1, row
* xw
.ch
);
2557 XFreePixmap(xw
.dpy
, xw
.buf
);
2558 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2559 DefaultDepth(xw
.dpy
, xw
.scr
));
2560 XftDrawChange(xw
.draw
, xw
.buf
);
2561 xclear(0, 0, xw
.w
, xw
.h
);
2564 static inline ushort
2565 sixd_to_16bit(int x
) {
2566 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2572 XRenderColor color
= { .alpha
= 0xffff };
2574 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2575 for(i
= 0; i
< LEN(colorname
); i
++) {
2578 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2579 die("Could not allocate color '%s'\n", colorname
[i
]);
2583 /* load colors [16-255] ; same colors as xterm */
2584 for(i
= 16, r
= 0; r
< 6; r
++) {
2585 for(g
= 0; g
< 6; g
++) {
2586 for(b
= 0; b
< 6; b
++) {
2587 color
.red
= sixd_to_16bit(r
);
2588 color
.green
= sixd_to_16bit(g
);
2589 color
.blue
= sixd_to_16bit(b
);
2590 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2591 die("Could not allocate color %d\n", i
);
2598 for(r
= 0; r
< 24; r
++, i
++) {
2599 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2600 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2602 die("Could not allocate color %d\n", i
);
2608 xsetcolorname(int x
, const char *name
) {
2609 XRenderColor color
= { .alpha
= 0xffff };
2611 if (x
< 0 || x
> LEN(colorname
))
2614 if(16 <= x
&& x
< 16 + 216) {
2615 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2616 color
.red
= sixd_to_16bit(r
);
2617 color
.green
= sixd_to_16bit(g
);
2618 color
.blue
= sixd_to_16bit(b
);
2619 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2620 return 0; /* something went wrong */
2623 } else if (16 + 216 <= x
&& x
< 256) {
2624 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2625 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2626 return 0; /* something went wrong */
2630 name
= colorname
[x
];
2633 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2640 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2641 XftDrawRect(xw
.draw
,
2642 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2643 borderpx
+ col1
* xw
.cw
,
2644 borderpx
+ row1
* xw
.ch
,
2645 (col2
-col1
+1) * xw
.cw
,
2646 (row2
-row1
+1) * xw
.ch
);
2650 * Absolute coordinates.
2653 xclear(int x1
, int y1
, int x2
, int y2
) {
2654 XftDrawRect(xw
.draw
,
2655 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2656 x1
, y1
, x2
-x1
, y2
-y1
);
2661 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2662 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2663 XSizeHints
*sizeh
= NULL
;
2665 sizeh
= XAllocSizeHints();
2666 if(xw
.isfixed
== False
) {
2667 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2668 sizeh
->height
= xw
.h
;
2669 sizeh
->width
= xw
.w
;
2670 sizeh
->height_inc
= xw
.ch
;
2671 sizeh
->width_inc
= xw
.cw
;
2672 sizeh
->base_height
= 2 * borderpx
;
2673 sizeh
->base_width
= 2 * borderpx
;
2675 sizeh
->flags
= PMaxSize
| PMinSize
;
2676 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2677 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2680 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2685 xloadfont(Font
*f
, FcPattern
*pattern
) {
2689 match
= FcFontMatch(NULL
, pattern
, &result
);
2693 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2694 FcPatternDestroy(match
);
2699 f
->pattern
= FcPatternDuplicate(pattern
);
2701 f
->ascent
= f
->match
->ascent
;
2702 f
->descent
= f
->match
->descent
;
2704 f
->rbearing
= f
->match
->max_advance_width
;
2706 f
->height
= f
->ascent
+ f
->descent
;
2707 f
->width
= f
->lbearing
+ f
->rbearing
;
2713 xloadfonts(char *fontstr
, int fontsize
) {
2718 if(fontstr
[0] == '-') {
2719 pattern
= XftXlfdParse(fontstr
, False
, False
);
2721 pattern
= FcNameParse((FcChar8
*)fontstr
);
2725 die("st: can't open font %s\n", fontstr
);
2728 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2729 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2730 usedfontsize
= fontsize
;
2732 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2733 if(result
== FcResultMatch
) {
2734 usedfontsize
= (int)fontval
;
2737 * Default font size is 12, if none given. This is to
2738 * have a known usedfontsize value.
2740 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2745 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2746 FcDefaultSubstitute(pattern
);
2748 if(xloadfont(&dc
.font
, pattern
))
2749 die("st: can't open font %s\n", fontstr
);
2751 /* Setting character width and height. */
2752 xw
.cw
= dc
.font
.width
;
2753 xw
.ch
= dc
.font
.height
;
2755 FcPatternDel(pattern
, FC_SLANT
);
2756 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2757 if(xloadfont(&dc
.ifont
, pattern
))
2758 die("st: can't open font %s\n", fontstr
);
2760 FcPatternDel(pattern
, FC_WEIGHT
);
2761 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2762 if(xloadfont(&dc
.ibfont
, pattern
))
2763 die("st: can't open font %s\n", fontstr
);
2765 FcPatternDel(pattern
, FC_SLANT
);
2766 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2767 if(xloadfont(&dc
.bfont
, pattern
))
2768 die("st: can't open font %s\n", fontstr
);
2770 FcPatternDestroy(pattern
);
2774 xloadfontset(Font
*f
) {
2777 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2783 xunloadfont(Font
*f
) {
2784 XftFontClose(xw
.dpy
, f
->match
);
2785 FcPatternDestroy(f
->pattern
);
2787 FcFontSetDestroy(f
->set
);
2791 xunloadfonts(void) {
2795 * Free the loaded fonts in the font cache. This is done backwards
2798 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2801 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2806 xunloadfont(&dc
.font
);
2807 xunloadfont(&dc
.bfont
);
2808 xunloadfont(&dc
.ifont
);
2809 xunloadfont(&dc
.ibfont
);
2813 xzoom(const Arg
*arg
) {
2815 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2827 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2828 die("Can't open display\n");
2829 xw
.scr
= XDefaultScreen(xw
.dpy
);
2830 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2834 die("Could not init fontconfig.\n");
2836 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2837 xloadfonts(usedfont
, 0);
2840 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2843 /* adjust fixed window geometry */
2845 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2846 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2848 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2850 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2855 /* window - default size */
2856 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2857 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2863 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2864 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2865 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2866 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2867 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2868 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2869 xw
.attrs
.colormap
= xw
.cmap
;
2871 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2872 XRootWindow(xw
.dpy
, xw
.scr
);
2873 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2874 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2875 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2876 | CWEventMask
| CWColormap
, &xw
.attrs
);
2878 memset(&gcvalues
, 0, sizeof(gcvalues
));
2879 gcvalues
.graphics_exposures
= False
;
2880 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2882 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2883 DefaultDepth(xw
.dpy
, xw
.scr
));
2884 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2885 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2887 /* Xft rendering context */
2888 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2891 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2892 XSetLocaleModifiers("@im=local");
2893 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2894 XSetLocaleModifiers("@im=");
2895 if((xw
.xim
= XOpenIM(xw
.dpy
,
2896 NULL
, NULL
, NULL
)) == NULL
) {
2897 die("XOpenIM failed. Could not open input"
2902 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2903 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2904 XNFocusWindow
, xw
.win
, NULL
);
2906 die("XCreateIC failed. Could not obtain input method.\n");
2908 /* white cursor, black outline */
2909 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2910 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2911 XRecolorCursor(xw
.dpy
, cursor
,
2912 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2913 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2915 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2916 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2917 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2920 XMapWindow(xw
.dpy
, xw
.win
);
2926 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2927 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2928 width
= charlen
* xw
.cw
, xp
, i
;
2930 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2933 Font
*font
= &dc
.font
;
2935 FcPattern
*fcpattern
, *fontpattern
;
2936 FcFontSet
*fcsets
[] = { NULL
};
2937 FcCharSet
*fccharset
;
2938 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2939 XRenderColor colfg
, colbg
;
2942 frcflags
= FRC_NORMAL
;
2944 if(base
.mode
& ATTR_ITALIC
) {
2945 if(base
.fg
== defaultfg
)
2946 base
.fg
= defaultitalic
;
2948 frcflags
= FRC_ITALIC
;
2949 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2950 if(base
.fg
== defaultfg
)
2951 base
.fg
= defaultitalic
;
2953 frcflags
= FRC_ITALICBOLD
;
2954 } else if(base
.mode
& ATTR_UNDERLINE
) {
2955 if(base
.fg
== defaultfg
)
2956 base
.fg
= defaultunderline
;
2958 fg
= &dc
.col
[base
.fg
];
2959 bg
= &dc
.col
[base
.bg
];
2961 if(base
.mode
& ATTR_BOLD
) {
2962 if(BETWEEN(base
.fg
, 0, 7)) {
2963 /* basic system colors */
2964 fg
= &dc
.col
[base
.fg
+ 8];
2965 } else if(BETWEEN(base
.fg
, 16, 195)) {
2967 fg
= &dc
.col
[base
.fg
+ 36];
2968 } else if(BETWEEN(base
.fg
, 232, 251)) {
2970 fg
= &dc
.col
[base
.fg
+ 4];
2973 * Those ranges will not be brightened:
2974 * 8 - 15 – bright system colors
2975 * 196 - 231 – highest 256 color cube
2976 * 252 - 255 – brightest colors in greyscale
2979 frcflags
= FRC_BOLD
;
2982 if(IS_SET(MODE_REVERSE
)) {
2983 if(fg
== &dc
.col
[defaultfg
]) {
2984 fg
= &dc
.col
[defaultbg
];
2986 colfg
.red
= ~fg
->color
.red
;
2987 colfg
.green
= ~fg
->color
.green
;
2988 colfg
.blue
= ~fg
->color
.blue
;
2989 colfg
.alpha
= fg
->color
.alpha
;
2990 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2994 if(bg
== &dc
.col
[defaultbg
]) {
2995 bg
= &dc
.col
[defaultfg
];
2997 colbg
.red
= ~bg
->color
.red
;
2998 colbg
.green
= ~bg
->color
.green
;
2999 colbg
.blue
= ~bg
->color
.blue
;
3000 colbg
.alpha
= bg
->color
.alpha
;
3001 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3006 if(base
.mode
& ATTR_REVERSE
) {
3012 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3015 /* Intelligent cleaning up of the borders. */
3017 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3018 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3020 if(x
+ charlen
>= term
.col
) {
3021 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3022 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3025 xclear(winx
, 0, winx
+ width
, borderpx
);
3027 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3029 /* Clean up the region we want to draw to. */
3030 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3032 /* Set the clip region because Xft is sometimes dirty. */
3037 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3039 for(xp
= winx
; bytelen
> 0;) {
3041 * Search for the range in the to be printed string of glyphs
3042 * that are in the main font. Then print that range. If
3043 * some glyph is found that is not in the font, do the
3051 u8cblen
= utf8decode(s
, &u8char
);
3055 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
3056 if(!doesexist
|| bytelen
<= 0) {
3065 XftDrawStringUtf8(xw
.draw
, fg
,
3067 winy
+ font
->ascent
,
3070 xp
+= font
->width
* u8fl
;
3083 /* Search the font cache. */
3084 for(i
= 0; i
< frclen
; i
++, frp
--) {
3088 if(frc
[frp
].c
== u8char
3089 && frc
[frp
].flags
== frcflags
) {
3094 /* Nothing was found. */
3098 fcsets
[0] = font
->set
;
3101 * Nothing was found in the cache. Now use
3102 * some dozen of Fontconfig calls to get the
3103 * font for one single character.
3105 fcpattern
= FcPatternDuplicate(font
->pattern
);
3106 fccharset
= FcCharSetCreate();
3108 FcCharSetAddChar(fccharset
, u8char
);
3109 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3111 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3114 FcConfigSubstitute(0, fcpattern
,
3116 FcDefaultSubstitute(fcpattern
);
3118 fontpattern
= FcFontSetMatch(0, fcsets
,
3119 FcTrue
, fcpattern
, &fcres
);
3122 * Overwrite or create the new cache entry.
3126 if(frccur
>= LEN(frc
))
3128 if(frclen
> LEN(frc
)) {
3130 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
3133 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
3135 frc
[frccur
].c
= u8char
;
3136 frc
[frccur
].flags
= frcflags
;
3138 FcPatternDestroy(fcpattern
);
3139 FcCharSetDestroy(fccharset
);
3144 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
3145 xp
, winy
+ frc
[frp
].font
->ascent
,
3146 (FcChar8
*)u8c
, u8cblen
);
3152 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3153 winy + font->ascent, (FcChar8 *)s, bytelen);
3156 if(base
.mode
& ATTR_UNDERLINE
) {
3157 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3161 /* Reset clip to none. */
3162 XftDrawSetClip(xw
.draw
, 0);
3167 static int oldx
= 0, oldy
= 0;
3169 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3171 LIMIT(oldx
, 0, term
.col
-1);
3172 LIMIT(oldy
, 0, term
.row
-1);
3174 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3176 /* remove the old cursor */
3177 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3178 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3181 /* draw the new one */
3182 if(!(IS_SET(MODE_HIDE
))) {
3183 if(xw
.state
& WIN_FOCUSED
) {
3184 if(IS_SET(MODE_REVERSE
)) {
3185 g
.mode
|= ATTR_REVERSE
;
3191 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3193 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3194 borderpx
+ term
.c
.x
* xw
.cw
,
3195 borderpx
+ term
.c
.y
* xw
.ch
,
3197 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3198 borderpx
+ term
.c
.x
* xw
.cw
,
3199 borderpx
+ term
.c
.y
* xw
.ch
,
3201 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3202 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3203 borderpx
+ term
.c
.y
* xw
.ch
,
3205 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3206 borderpx
+ term
.c
.x
* xw
.cw
,
3207 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3210 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3216 xsettitle(char *p
) {
3219 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3221 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3226 xsettitle(opt_title
? opt_title
: "st");
3230 redraw(int timeout
) {
3231 struct timespec tv
= {0, timeout
* 1000};
3237 nanosleep(&tv
, NULL
);
3238 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3244 drawregion(0, 0, term
.col
, term
.row
);
3245 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3247 XSetForeground(xw
.dpy
, dc
.gc
,
3248 dc
.col
[IS_SET(MODE_REVERSE
)?
3249 defaultfg
: defaultbg
].pixel
);
3253 drawregion(int x1
, int y1
, int x2
, int y2
) {
3254 int ic
, ib
, x
, y
, ox
, sl
;
3256 char buf
[DRAW_BUF_SIZ
];
3257 bool ena_sel
= sel
.ob
.x
!= -1;
3259 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3262 if(!(xw
.state
& WIN_VISIBLE
))
3265 for(y
= y1
; y
< y2
; y
++) {
3269 xtermclear(0, y
, term
.col
, y
);
3271 base
= term
.line
[y
][0];
3273 for(x
= x1
; x
< x2
; x
++) {
3274 new = term
.line
[y
][x
];
3275 if(ena_sel
&& selected(x
, y
))
3276 new.mode
^= ATTR_REVERSE
;
3277 if(ib
> 0 && (ATTRCMP(base
, new)
3278 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3279 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3287 sl
= utf8size(new.c
);
3288 memcpy(buf
+ib
, new.c
, sl
);
3293 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3299 expose(XEvent
*ev
) {
3300 XExposeEvent
*e
= &ev
->xexpose
;
3302 if(xw
.state
& WIN_REDRAW
) {
3304 xw
.state
&= ~WIN_REDRAW
;
3310 visibility(XEvent
*ev
) {
3311 XVisibilityEvent
*e
= &ev
->xvisibility
;
3313 if(e
->state
== VisibilityFullyObscured
) {
3314 xw
.state
&= ~WIN_VISIBLE
;
3315 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3316 /* need a full redraw for next Expose, not just a buf copy */
3317 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3323 xw
.state
&= ~WIN_VISIBLE
;
3327 xsetpointermotion(int set
) {
3328 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3329 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3333 xseturgency(int add
) {
3334 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3336 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3337 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3343 XFocusChangeEvent
*e
= &ev
->xfocus
;
3345 if(e
->mode
== NotifyGrab
)
3348 if(ev
->type
== FocusIn
) {
3349 XSetICFocus(xw
.xic
);
3350 xw
.state
|= WIN_FOCUSED
;
3352 if(IS_SET(MODE_FOCUS
))
3353 ttywrite("\033[I", 3);
3355 XUnsetICFocus(xw
.xic
);
3356 xw
.state
&= ~WIN_FOCUSED
;
3357 if(IS_SET(MODE_FOCUS
))
3358 ttywrite("\033[O", 3);
3363 match(uint mask
, uint state
) {
3364 state
&= ~(ignoremod
);
3366 if(mask
== XK_NO_MOD
&& state
)
3368 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3370 if((state
& mask
) != state
)
3376 numlock(const Arg
*dummy
) {
3381 kmap(KeySym k
, uint state
) {
3386 /* Check for mapped keys out of X11 function keys. */
3387 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3388 if(mappedkeys
[i
] == k
)
3391 if(i
== LEN(mappedkeys
)) {
3392 if((k
& 0xFFFF) < 0xFD00)
3396 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3402 if(!match(mask
, state
))
3405 if(kp
->appkey
> 0) {
3406 if(!IS_SET(MODE_APPKEYPAD
))
3408 if(term
.numlock
&& kp
->appkey
== 2)
3410 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3414 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3416 && !IS_SET(MODE_APPCURSOR
))) {
3420 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3421 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3432 kpress(XEvent
*ev
) {
3433 XKeyEvent
*e
= &ev
->xkey
;
3435 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3441 if(IS_SET(MODE_KBDLOCK
))
3444 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3445 e
->state
&= ~Mod2Mask
;
3447 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3448 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3449 bp
->func(&(bp
->arg
));
3454 /* 2. custom keys from config.h */
3455 if((customkey
= kmap(ksym
, e
->state
))) {
3456 len
= strlen(customkey
);
3457 memcpy(buf
, customkey
, len
);
3458 /* 3. hardcoded (overrides X lookup) */
3463 if(len
== 1 && e
->state
& Mod1Mask
) {
3464 if(IS_SET(MODE_8BIT
)) {
3467 ret
= utf8encode(&c
, cp
);
3476 memcpy(cp
, xstr
, len
);
3477 len
= cp
- buf
+ len
;
3481 if(IS_SET(MODE_ECHO
))
3487 cmessage(XEvent
*e
) {
3490 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3492 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3493 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3494 xw
.state
|= WIN_FOCUSED
;
3496 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3497 xw
.state
&= ~WIN_FOCUSED
;
3499 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3500 /* Send SIGHUP to shell */
3507 cresize(int width
, int height
) {
3515 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3516 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3525 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3528 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3535 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3536 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3538 gettimeofday(&lastblink
, NULL
);
3539 gettimeofday(&last
, NULL
);
3541 for(xev
= actionfps
;;) {
3543 FD_SET(cmdfd
, &rfd
);
3546 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3549 die("select failed: %s\n", SERRNO
);
3551 if(FD_ISSET(cmdfd
, &rfd
)) {
3554 blinkset
= tattrset(ATTR_BLINK
);
3555 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3556 term
.mode
&= ~(MODE_BLINK
);
3560 if(FD_ISSET(xfd
, &rfd
))
3563 gettimeofday(&now
, NULL
);
3564 drawtimeout
.tv_sec
= 0;
3565 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3569 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3570 tsetdirtattr(ATTR_BLINK
);
3571 term
.mode
^= MODE_BLINK
;
3572 gettimeofday(&lastblink
, NULL
);
3575 if(TIMEDIFF(now
, last
) \
3576 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3582 while(XPending(xw
.dpy
)) {
3583 XNextEvent(xw
.dpy
, &ev
);
3584 if(XFilterEvent(&ev
, None
))
3586 if(handler
[ev
.type
])
3587 (handler
[ev
.type
])(&ev
);
3593 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3595 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3597 if(TIMEDIFF(now
, lastblink
) \
3599 drawtimeout
.tv_usec
= 1;
3601 drawtimeout
.tv_usec
= (1000 * \
3616 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3617 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3618 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3622 main(int argc
, char *argv
[]) {
3626 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3631 allowaltscreen
= false;
3634 opt_class
= EARGF(usage());
3637 /* eat all remaining arguments */
3642 opt_font
= EARGF(usage());
3645 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3650 if(bitm
& WidthValue
)
3652 if(bitm
& HeightValue
)
3654 if(bitm
& XNegative
&& xw
.fx
== 0)
3656 if(bitm
& XNegative
&& xw
.fy
== 0)
3659 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3663 opt_io
= EARGF(usage());
3666 opt_title
= EARGF(usage());
3669 opt_embed
= EARGF(usage());
3677 setlocale(LC_CTYPE
, "");
3678 XSetLocaleModifiers("");
3684 cresize(xw
.h
, xw
.w
);