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 VT102ID "\033[?6c"
82 enum glyph_attribute
{
93 enum cursor_movement
{
111 MODE_MOUSEMOTION
= 64,
116 MODE_APPCURSOR
= 2048,
117 MODE_MOUSESGR
= 4096,
122 MODE_MOUSEX10
= 131072,
123 MODE_MOUSEMANY
= 262144,
124 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
131 ESC_STR
= 4, /* DSC, OSC, PM, APC */
133 ESC_STR_END
= 16, /* a final string was encountered */
134 ESC_TEST
= 32, /* Enter in test mode */
143 enum selection_type
{
148 enum selection_snap
{
153 typedef unsigned char uchar
;
154 typedef unsigned int uint
;
155 typedef unsigned long ulong
;
156 typedef unsigned short ushort
;
159 char c
[UTF_SIZ
]; /* character code */
160 uchar mode
; /* attribute flags */
161 ushort fg
; /* foreground */
162 ushort bg
; /* background */
168 Glyph attr
; /* current char attributes */
174 /* CSI Escape sequence structs */
175 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
177 char buf
[ESC_BUF_SIZ
]; /* raw string */
178 int len
; /* raw string length */
180 int arg
[ESC_ARG_SIZ
];
181 int narg
; /* nb of args */
185 /* STR Escape sequence structs */
186 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
188 char type
; /* ESC type ... */
189 char buf
[STR_BUF_SIZ
]; /* raw string */
190 int len
; /* raw string length */
191 char *args
[STR_ARG_SIZ
];
192 int narg
; /* nb of args */
195 /* Internal representation of the screen */
197 int row
; /* nb row */
198 int col
; /* nb col */
199 Line
*line
; /* screen */
200 Line
*alt
; /* alternate screen */
201 bool *dirty
; /* dirtyness of lines */
202 TCursor c
; /* cursor */
203 int top
; /* top scroll limit */
204 int bot
; /* bottom scroll limit */
205 int mode
; /* terminal mode flags */
206 int esc
; /* escape state flags */
207 bool numlock
; /* lock numbers in keyboard */
211 /* Purely graphic info */
217 Atom xembed
, wmdeletewin
;
222 XSetWindowAttributes attrs
;
224 bool isfixed
; /* is fixed geometry? */
225 int fx
, fy
, fw
, fh
; /* fixed geometry */
226 int tw
, th
; /* tty width and height */
227 int w
, h
; /* window width and height */
228 int ch
; /* char height */
229 int cw
; /* char width */
230 char state
; /* focus, redraw, visible */
243 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
244 signed char appkey
; /* application keypad */
245 signed char appcursor
; /* application cursor */
246 signed char crlf
; /* crlf mode */
254 * Selection variables:
255 * nb – normalized coordinates of the beginning of the selection
256 * ne – normalized coordinates of the end of the selection
257 * ob – original coordinates of the beginning of the selection
258 * oe – original coordinates of the end of the selection
267 struct timeval tclick1
;
268 struct timeval tclick2
;
281 void (*func
)(const Arg
*);
285 /* function definitions used in config.h */
286 static void clippaste(const Arg
*);
287 static void numlock(const Arg
*);
288 static void selpaste(const Arg
*);
289 static void xzoom(const Arg
*);
291 /* Config.h for applying patches and the configuration. */
307 /* Drawing Context */
309 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
310 Font font
, bfont
, ifont
, ibfont
;
314 static void die(const char *, ...);
315 static void draw(void);
316 static void redraw(int);
317 static void drawregion(int, int, int, int);
318 static void execsh(void);
319 static void sigchld(int);
320 static void run(void);
322 static void csidump(void);
323 static void csihandle(void);
324 static void csiparse(void);
325 static void csireset(void);
326 static void strdump(void);
327 static void strhandle(void);
328 static void strparse(void);
329 static void strreset(void);
331 static int tattrset(int);
332 static void tclearregion(int, int, int, int);
333 static void tcursor(int);
334 static void tdeletechar(int);
335 static void tdeleteline(int);
336 static void tinsertblank(int);
337 static void tinsertblankline(int);
338 static void tmoveto(int, int);
339 static void tmoveato(int x
, int y
);
340 static void tnew(int, int);
341 static void tnewline(int);
342 static void tputtab(bool);
343 static void tputc(char *, int);
344 static void treset(void);
345 static int tresize(int, int);
346 static void tscrollup(int, int);
347 static void tscrolldown(int, int);
348 static void tsetattr(int*, int);
349 static void tsetchar(char *, Glyph
*, int, int);
350 static void tsetscroll(int, int);
351 static void tswapscreen(void);
352 static void tsetdirt(int, int);
353 static void tsetdirtattr(int);
354 static void tsetmode(bool, bool, int *, int);
355 static void tfulldirt(void);
356 static void techo(char *, int);
358 static inline bool match(uint
, uint
);
359 static void ttynew(void);
360 static void ttyread(void);
361 static void ttyresize(void);
362 static void ttywrite(const char *, size_t);
364 static void xdraws(char *, Glyph
, int, int, int, int);
365 static void xhints(void);
366 static void xclear(int, int, int, int);
367 static void xdrawcursor(void);
368 static void xinit(void);
369 static void xloadcols(void);
370 static int xsetcolorname(int, const char *);
371 static int xloadfont(Font
*, FcPattern
*);
372 static void xloadfonts(char *, int);
373 static int xloadfontset(Font
*);
374 static void xsettitle(char *);
375 static void xresettitle(void);
376 static void xsetpointermotion(int);
377 static void xseturgency(int);
378 static void xsetsel(char*);
379 static void xtermclear(int, int, int, int);
380 static void xunloadfont(Font
*f
);
381 static void xunloadfonts(void);
382 static void xresize(int, int);
384 static void expose(XEvent
*);
385 static void visibility(XEvent
*);
386 static void unmap(XEvent
*);
387 static char *kmap(KeySym
, uint
);
388 static void kpress(XEvent
*);
389 static void cmessage(XEvent
*);
390 static void cresize(int, int);
391 static void resize(XEvent
*);
392 static void focus(XEvent
*);
393 static void brelease(XEvent
*);
394 static void bpress(XEvent
*);
395 static void bmotion(XEvent
*);
396 static void selnotify(XEvent
*);
397 static void selclear(XEvent
*);
398 static void selrequest(XEvent
*);
400 static void selinit(void);
401 static void selsort(void);
402 static inline bool selected(int, int);
403 static void selcopy(void);
404 static void selscroll(int, int);
405 static void selsnap(int, int *, int *, int);
407 static int utf8decode(char *, long *);
408 static int utf8encode(long *, char *);
409 static int utf8size(char *);
410 static int isfullutf8(char *, int);
412 static ssize_t
xwrite(int, char *, size_t);
413 static void *xmalloc(size_t);
414 static void *xrealloc(void *, size_t);
415 static void *xcalloc(size_t, size_t);
417 static void (*handler
[LASTEvent
])(XEvent
*) = {
419 [ClientMessage
] = cmessage
,
420 [ConfigureNotify
] = resize
,
421 [VisibilityNotify
] = visibility
,
422 [UnmapNotify
] = unmap
,
426 [MotionNotify
] = bmotion
,
427 [ButtonPress
] = bpress
,
428 [ButtonRelease
] = brelease
,
429 [SelectionClear
] = selclear
,
430 [SelectionNotify
] = selnotify
,
431 [SelectionRequest
] = selrequest
,
438 static CSIEscape csiescseq
;
439 static STREscape strescseq
;
442 static Selection sel
;
443 static int iofd
= -1;
444 static char **opt_cmd
= NULL
;
445 static char *opt_io
= NULL
;
446 static char *opt_title
= NULL
;
447 static char *opt_embed
= NULL
;
448 static char *opt_class
= NULL
;
449 static char *opt_font
= NULL
;
450 static int oldbutton
= 3; /* button event on startup: 3 = release */
452 static char *usedfont
= NULL
;
453 static int usedfontsize
= 0;
455 /* Font Ring Cache */
468 /* Fontcache is an array now. A new font will be appended to the array. */
469 static Fontcache frc
[16];
470 static int frclen
= 0;
473 xwrite(int fd
, char *s
, size_t len
) {
477 ssize_t r
= write(fd
, s
, len
);
487 xmalloc(size_t len
) {
488 void *p
= malloc(len
);
491 die("Out of memory\n");
497 xrealloc(void *p
, size_t len
) {
498 if((p
= realloc(p
, len
)) == NULL
)
499 die("Out of memory\n");
505 xcalloc(size_t nmemb
, size_t size
) {
506 void *p
= calloc(nmemb
, size
);
509 die("Out of memory\n");
515 utf8decode(char *s
, long *u
) {
521 if(~c
& 0x80) { /* 0xxxxxxx */
524 } else if((c
& 0xE0) == 0xC0) { /* 110xxxxx */
527 } else if((c
& 0xF0) == 0xE0) { /* 1110xxxx */
530 } else if((c
& 0xF8) == 0xF0) { /* 11110xxx */
537 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
539 if((c
& 0xC0) != 0x80) /* 10xxxxxx */
545 if((n
== 1 && *u
< 0x80) ||
546 (n
== 2 && *u
< 0x800) ||
547 (n
== 3 && *u
< 0x10000) ||
548 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
560 utf8encode(long *u
, char *s
) {
568 *sp
= uc
; /* 0xxxxxxx */
570 } else if(*u
< 0x800) {
571 *sp
= (uc
>> 6) | 0xC0; /* 110xxxxx */
573 } else if(uc
< 0x10000) {
574 *sp
= (uc
>> 12) | 0xE0; /* 1110xxxx */
576 } else if(uc
<= 0x10FFFF) {
577 *sp
= (uc
>> 18) | 0xF0; /* 11110xxx */
583 for(i
=n
,++sp
; i
>0; --i
,++sp
)
584 *sp
= ((uc
>> 6*(i
-1)) & 0x3F) | 0x80; /* 10xxxxxx */
596 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
597 UTF-8 otherwise return 0 */
599 isfullutf8(char *s
, int b
) {
607 } else if((*c1
& 0xE0) == 0xC0 && b
== 1) {
609 } else if((*c1
& 0xF0) == 0xE0 &&
611 ((b
== 2) && (*c2
& 0xC0) == 0x80))) {
613 } else if((*c1
& 0xF8) == 0xF0 &&
615 ((b
== 2) && (*c2
& 0xC0) == 0x80) ||
616 ((b
== 3) && (*c2
& 0xC0) == 0x80 && (*c3
& 0xC0) == 0x80))) {
629 } else if((c
& 0xE0) == 0xC0) {
631 } else if((c
& 0xF0) == 0xE0) {
640 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
641 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
645 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
646 if(sel
.xtarget
== None
)
647 sel
.xtarget
= XA_STRING
;
655 return LIMIT(x
, 0, term
.col
-1);
663 return LIMIT(y
, 0, term
.row
-1);
668 if(sel
.ob
.y
== sel
.oe
.y
) {
669 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
670 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
672 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
673 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
675 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
676 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
680 selected(int x
, int y
) {
681 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
682 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
684 if(sel
.type
== SEL_RECTANGULAR
) {
685 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
686 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
689 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
690 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
691 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
692 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
696 selsnap(int mode
, int *x
, int *y
, int direction
) {
702 * Snap around if the word wraps around at the end or
703 * beginning of a line.
706 if(direction
< 0 && *x
<= 0) {
707 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
715 if(direction
> 0 && *x
>= term
.col
-1) {
716 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
725 if(strchr(worddelimiters
,
726 term
.line
[*y
][*x
+ direction
].c
[0])) {
735 * Snap around if the the previous line or the current one
736 * has set ATTR_WRAP at its end. Then the whole next or
737 * previous line will be selected.
739 *x
= (direction
< 0) ? 0 : term
.col
- 1;
740 if(direction
< 0 && *y
> 0) {
741 for(; *y
> 0; *y
+= direction
) {
742 if(!(term
.line
[*y
-1][term
.col
-1].mode
747 } else if(direction
> 0 && *y
< term
.row
-1) {
748 for(; *y
< term
.row
; *y
+= direction
) {
749 if(!(term
.line
[*y
][term
.col
-1].mode
758 * Select the whole line when the end of line is reached.
762 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
772 getbuttoninfo(XEvent
*e
) {
774 uint state
= e
->xbutton
.state
&~Button1Mask
;
776 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
778 sel
.oe
.x
= x2col(e
->xbutton
.x
);
779 sel
.oe
.y
= y2row(e
->xbutton
.y
);
781 if(sel
.ob
.y
< sel
.oe
.y
782 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
783 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
784 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
786 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
787 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
791 sel
.type
= SEL_REGULAR
;
792 for(type
= 1; type
< LEN(selmasks
); ++type
) {
793 if(match(selmasks
[type
], state
)) {
801 mousereport(XEvent
*e
) {
802 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
803 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
809 if(e
->xbutton
.type
== MotionNotify
) {
810 if(x
== ox
&& y
== oy
)
812 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
814 /* MOUSE_MOTION: no reporting if no button is pressed */
815 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
818 button
= oldbutton
+ 32;
821 } else if(!IS_SET(MODE_MOUSESGR
)
822 && (e
->xbutton
.type
== ButtonRelease
823 || button
== AnyButton
)) {
829 if(e
->xbutton
.type
== ButtonPress
) {
836 if(!IS_SET(MODE_MOUSEX10
)) {
837 button
+= (state
& ShiftMask
? 4 : 0)
838 + (state
& Mod4Mask
? 8 : 0)
839 + (state
& ControlMask
? 16 : 0);
843 if(IS_SET(MODE_MOUSESGR
)) {
844 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
846 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
847 } else if(x
< 223 && y
< 223) {
848 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
849 IS_SET(MODE_MOUSEX10
)? button
-1 : 32+button
,
863 if(IS_SET(MODE_MOUSE
)) {
868 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
869 if(e
->xbutton
.button
== mk
->b
870 && match(mk
->mask
, e
->xbutton
.state
)) {
871 ttywrite(mk
->s
, strlen(mk
->s
));
872 if(IS_SET(MODE_ECHO
))
873 techo(mk
->s
, strlen(mk
->s
));
878 if(e
->xbutton
.button
== Button1
) {
879 gettimeofday(&now
, NULL
);
881 /* Clear previous selection, logically and visually. */
884 sel
.type
= SEL_REGULAR
;
885 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
886 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
889 * If the user clicks below predefined timeouts specific
890 * snapping behaviour is exposed.
892 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
893 sel
.snap
= SNAP_LINE
;
894 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
895 sel
.snap
= SNAP_WORD
;
899 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
900 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
904 * Draw selection, unless it's regular and we don't want to
905 * make clicks visible
909 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
911 sel
.tclick2
= sel
.tclick1
;
919 int x
, y
, bufsize
, size
, i
, ex
;
925 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
926 ptr
= str
= xmalloc(bufsize
);
928 /* append every set & selected glyph to the selection */
929 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
930 gp
= &term
.line
[y
][0];
931 last
= gp
+ term
.col
;
933 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
934 strcmp(last
->c
, " ") != 0))
937 for(x
= 0; gp
<= last
; x
++, ++gp
) {
941 size
= utf8size(gp
->c
);
942 memcpy(ptr
, gp
->c
, size
);
947 * Copy and pasting of line endings is inconsistent
948 * in the inconsistent terminal and GUI world.
949 * The best solution seems like to produce '\n' when
950 * something is copied from st and convert '\n' to
951 * '\r', when something to be pasted is received by
953 * FIXME: Fix the computer world.
955 if(y
< sel
.ne
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
959 * If the last selected line expands in the selection
960 * after the visible text '\n' is appended.
964 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
967 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
979 selnotify(XEvent
*e
) {
980 ulong nitems
, ofs
, rem
;
982 uchar
*data
, *last
, *repl
;
987 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
988 False
, AnyPropertyType
, &type
, &format
,
989 &nitems
, &rem
, &data
)) {
990 fprintf(stderr
, "Clipboard allocation failed\n");
995 * As seen in selcopy:
996 * Line endings are inconsistent in the terminal and GUI world
997 * copy and pasting. When receiving some selection data,
998 * replace all '\n' with '\r'.
999 * FIXME: Fix the computer world.
1002 last
= data
+ nitems
* format
/ 8;
1003 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1007 ttywrite((const char *)data
, nitems
* format
/ 8);
1009 /* number of 32-bit chunks returned */
1010 ofs
+= nitems
* format
/ 32;
1015 selpaste(const Arg
*dummy
) {
1016 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1017 xw
.win
, CurrentTime
);
1021 clippaste(const Arg
*dummy
) {
1024 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1025 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1026 xw
.win
, CurrentTime
);
1030 selclear(XEvent
*e
) {
1034 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1038 selrequest(XEvent
*e
) {
1039 XSelectionRequestEvent
*xsre
;
1040 XSelectionEvent xev
;
1041 Atom xa_targets
, string
;
1043 xsre
= (XSelectionRequestEvent
*) e
;
1044 xev
.type
= SelectionNotify
;
1045 xev
.requestor
= xsre
->requestor
;
1046 xev
.selection
= xsre
->selection
;
1047 xev
.target
= xsre
->target
;
1048 xev
.time
= xsre
->time
;
1050 xev
.property
= None
;
1052 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1053 if(xsre
->target
== xa_targets
) {
1054 /* respond with the supported type */
1055 string
= sel
.xtarget
;
1056 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1057 XA_ATOM
, 32, PropModeReplace
,
1058 (uchar
*) &string
, 1);
1059 xev
.property
= xsre
->property
;
1060 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1061 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1062 xsre
->target
, 8, PropModeReplace
,
1063 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1064 xev
.property
= xsre
->property
;
1067 /* all done, send a notification to the listener */
1068 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1069 fprintf(stderr
, "Error sending SelectionNotify event\n");
1073 xsetsel(char *str
) {
1074 /* register the selection for both the clipboard and the primary */
1080 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1082 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1083 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1087 brelease(XEvent
*e
) {
1088 if(IS_SET(MODE_MOUSE
)) {
1093 if(e
->xbutton
.button
== Button2
) {
1095 } else if(e
->xbutton
.button
== Button1
) {
1103 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1108 bmotion(XEvent
*e
) {
1109 int oldey
, oldex
, oldsby
, oldsey
;
1111 if(IS_SET(MODE_MOUSE
)) {
1126 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1127 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1131 die(const char *errstr
, ...) {
1134 va_start(ap
, errstr
);
1135 vfprintf(stderr
, errstr
, ap
);
1143 char *envshell
= getenv("SHELL");
1144 const struct passwd
*pass
= getpwuid(getuid());
1145 char buf
[sizeof(long) * 8 + 1];
1147 unsetenv("COLUMNS");
1149 unsetenv("TERMCAP");
1152 setenv("LOGNAME", pass
->pw_name
, 1);
1153 setenv("USER", pass
->pw_name
, 1);
1154 setenv("SHELL", pass
->pw_shell
, 0);
1155 setenv("HOME", pass
->pw_dir
, 0);
1158 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1159 setenv("WINDOWID", buf
, 1);
1161 signal(SIGCHLD
, SIG_DFL
);
1162 signal(SIGHUP
, SIG_DFL
);
1163 signal(SIGINT
, SIG_DFL
);
1164 signal(SIGQUIT
, SIG_DFL
);
1165 signal(SIGTERM
, SIG_DFL
);
1166 signal(SIGALRM
, SIG_DFL
);
1168 DEFAULT(envshell
, shell
);
1169 setenv("TERM", termname
, 1);
1170 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1171 execvp(args
[0], args
);
1179 if(waitpid(pid
, &stat
, 0) < 0)
1180 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1182 if(WIFEXITED(stat
)) {
1183 exit(WEXITSTATUS(stat
));
1192 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1194 /* seems to work fine on linux, openbsd and freebsd */
1195 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1196 die("openpty failed: %s\n", SERRNO
);
1198 switch(pid
= fork()) {
1200 die("fork failed\n");
1203 setsid(); /* create a new process group */
1204 dup2(s
, STDIN_FILENO
);
1205 dup2(s
, STDOUT_FILENO
);
1206 dup2(s
, STDERR_FILENO
);
1207 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1208 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1216 signal(SIGCHLD
, sigchld
);
1218 iofd
= (!strcmp(opt_io
, "-")) ?
1220 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1222 fprintf(stderr
, "Error opening %s:%s\n",
1223 opt_io
, strerror(errno
));
1233 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1235 fprintf(stderr
, "\n");
1240 static char buf
[BUFSIZ
];
1241 static int buflen
= 0;
1244 int charsize
; /* size of utf8 char in bytes */
1248 /* append read bytes to unprocessed bytes */
1249 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1250 die("Couldn't read from shell: %s\n", SERRNO
);
1252 /* process every complete utf8 char */
1255 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1256 charsize
= utf8decode(ptr
, &utf8c
);
1257 utf8encode(&utf8c
, s
);
1263 /* keep any uncomplete utf8 char for the next call */
1264 memmove(buf
, ptr
, buflen
);
1268 ttywrite(const char *s
, size_t n
) {
1269 if(write(cmdfd
, s
, n
) == -1)
1270 die("write error on tty: %s\n", SERRNO
);
1277 w
.ws_row
= term
.row
;
1278 w
.ws_col
= term
.col
;
1279 w
.ws_xpixel
= xw
.tw
;
1280 w
.ws_ypixel
= xw
.th
;
1281 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1282 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1286 tattrset(int attr
) {
1289 for(i
= 0; i
< term
.row
-1; i
++) {
1290 for(j
= 0; j
< term
.col
-1; j
++) {
1291 if(term
.line
[i
][j
].mode
& attr
)
1300 tsetdirt(int top
, int bot
) {
1303 LIMIT(top
, 0, term
.row
-1);
1304 LIMIT(bot
, 0, term
.row
-1);
1306 for(i
= top
; i
<= bot
; i
++)
1311 tsetdirtattr(int attr
) {
1314 for(i
= 0; i
< term
.row
-1; i
++) {
1315 for(j
= 0; j
< term
.col
-1; j
++) {
1316 if(term
.line
[i
][j
].mode
& attr
) {
1326 tsetdirt(0, term
.row
-1);
1333 if(mode
== CURSOR_SAVE
) {
1335 } else if(mode
== CURSOR_LOAD
) {
1345 term
.c
= (TCursor
){{
1349 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1351 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1352 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1355 term
.bot
= term
.row
- 1;
1356 term
.mode
= MODE_WRAP
;
1358 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1360 tcursor(CURSOR_SAVE
);
1364 tnew(int col
, int row
) {
1365 memset(&term
, 0, sizeof(Term
));
1374 Line
*tmp
= term
.line
;
1376 term
.line
= term
.alt
;
1378 term
.mode
^= MODE_ALTSCREEN
;
1383 tscrolldown(int orig
, int n
) {
1387 LIMIT(n
, 0, term
.bot
-orig
+1);
1389 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1391 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1392 temp
= term
.line
[i
];
1393 term
.line
[i
] = term
.line
[i
-n
];
1394 term
.line
[i
-n
] = temp
;
1397 term
.dirty
[i
-n
] = 1;
1404 tscrollup(int orig
, int n
) {
1407 LIMIT(n
, 0, term
.bot
-orig
+1);
1409 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1411 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1412 temp
= term
.line
[i
];
1413 term
.line
[i
] = term
.line
[i
+n
];
1414 term
.line
[i
+n
] = temp
;
1417 term
.dirty
[i
+n
] = 1;
1420 selscroll(orig
, -n
);
1424 selscroll(int orig
, int n
) {
1428 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1429 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1433 if(sel
.type
== SEL_RECTANGULAR
) {
1434 if(sel
.ob
.y
< term
.top
)
1435 sel
.ob
.y
= term
.top
;
1436 if(sel
.oe
.y
> term
.bot
)
1437 sel
.oe
.y
= term
.bot
;
1439 if(sel
.ob
.y
< term
.top
) {
1440 sel
.ob
.y
= term
.top
;
1443 if(sel
.oe
.y
> term
.bot
) {
1444 sel
.oe
.y
= term
.bot
;
1445 sel
.oe
.x
= term
.col
;
1453 tnewline(int first_col
) {
1457 tscrollup(term
.top
, 1);
1461 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1466 char *p
= csiescseq
.buf
, *np
;
1475 csiescseq
.buf
[csiescseq
.len
] = '\0';
1476 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1478 v
= strtol(p
, &np
, 10);
1481 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1483 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1485 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1489 csiescseq
.mode
= *p
;
1492 /* for absolute user moves, when decom is set */
1494 tmoveato(int x
, int y
) {
1495 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1499 tmoveto(int x
, int y
) {
1502 if(term
.c
.state
& CURSOR_ORIGIN
) {
1507 maxy
= term
.row
- 1;
1509 LIMIT(x
, 0, term
.col
-1);
1510 LIMIT(y
, miny
, maxy
);
1511 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1517 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1518 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1519 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1520 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1521 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1522 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1523 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1524 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1525 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1526 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1530 * The table is proudly stolen from rxvt.
1532 if(attr
->mode
& ATTR_GFX
) {
1533 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1534 && vt100_0
[c
[0] - 0x41]) {
1535 c
= vt100_0
[c
[0] - 0x41];
1540 term
.line
[y
][x
] = *attr
;
1541 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1545 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1549 temp
= x1
, x1
= x2
, x2
= temp
;
1551 temp
= y1
, y1
= y2
, y2
= temp
;
1553 LIMIT(x1
, 0, term
.col
-1);
1554 LIMIT(x2
, 0, term
.col
-1);
1555 LIMIT(y1
, 0, term
.row
-1);
1556 LIMIT(y2
, 0, term
.row
-1);
1558 for(y
= y1
; y
<= y2
; y
++) {
1560 for(x
= x1
; x
<= x2
; x
++) {
1563 term
.line
[y
][x
] = term
.c
.attr
;
1564 memcpy(term
.line
[y
][x
].c
, " ", 2);
1570 tdeletechar(int n
) {
1571 int src
= term
.c
.x
+ n
;
1573 int size
= term
.col
- src
;
1575 term
.dirty
[term
.c
.y
] = 1;
1577 if(src
>= term
.col
) {
1578 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1582 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1583 size
* sizeof(Glyph
));
1584 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1588 tinsertblank(int n
) {
1591 int size
= term
.col
- dst
;
1593 term
.dirty
[term
.c
.y
] = 1;
1595 if(dst
>= term
.col
) {
1596 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1600 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1601 size
* sizeof(Glyph
));
1602 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1606 tinsertblankline(int n
) {
1607 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1610 tscrolldown(term
.c
.y
, n
);
1614 tdeleteline(int n
) {
1615 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1618 tscrollup(term
.c
.y
, n
);
1622 tsetattr(int *attr
, int l
) {
1625 for(i
= 0; i
< l
; i
++) {
1628 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1629 | ATTR_BOLD
| ATTR_ITALIC \
1631 term
.c
.attr
.fg
= defaultfg
;
1632 term
.c
.attr
.bg
= defaultbg
;
1635 term
.c
.attr
.mode
|= ATTR_BOLD
;
1638 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1641 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1643 case 5: /* slow blink */
1644 case 6: /* rapid blink */
1645 term
.c
.attr
.mode
|= ATTR_BLINK
;
1648 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1652 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1655 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1658 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1662 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1665 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1668 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1670 if(BETWEEN(attr
[i
], 0, 255)) {
1671 term
.c
.attr
.fg
= attr
[i
];
1674 "erresc: bad fgcolor %d\n",
1679 "erresc(38): gfx attr %d unknown\n",
1684 term
.c
.attr
.fg
= defaultfg
;
1687 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1689 if(BETWEEN(attr
[i
], 0, 255)) {
1690 term
.c
.attr
.bg
= attr
[i
];
1693 "erresc: bad bgcolor %d\n",
1698 "erresc(48): gfx attr %d unknown\n",
1703 term
.c
.attr
.bg
= defaultbg
;
1706 if(BETWEEN(attr
[i
], 30, 37)) {
1707 term
.c
.attr
.fg
= attr
[i
] - 30;
1708 } else if(BETWEEN(attr
[i
], 40, 47)) {
1709 term
.c
.attr
.bg
= attr
[i
] - 40;
1710 } else if(BETWEEN(attr
[i
], 90, 97)) {
1711 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1712 } else if(BETWEEN(attr
[i
], 100, 107)) {
1713 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1716 "erresc(default): gfx attr %d unknown\n",
1717 attr
[i
]), csidump();
1725 tsetscroll(int t
, int b
) {
1728 LIMIT(t
, 0, term
.row
-1);
1729 LIMIT(b
, 0, term
.row
-1);
1739 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1742 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1746 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1750 case 1: /* DECCKM -- Cursor key */
1751 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1753 case 5: /* DECSCNM -- Reverse video */
1755 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1756 if(mode
!= term
.mode
)
1757 redraw(REDRAW_TIMEOUT
);
1759 case 6: /* DECOM -- Origin */
1760 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1763 case 7: /* DECAWM -- Auto wrap */
1764 MODBIT(term
.mode
, set
, MODE_WRAP
);
1766 case 0: /* Error (IGNORED) */
1767 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1768 case 3: /* DECCOLM -- Column (IGNORED) */
1769 case 4: /* DECSCLM -- Scroll (IGNORED) */
1770 case 8: /* DECARM -- Auto repeat (IGNORED) */
1771 case 18: /* DECPFF -- Printer feed (IGNORED) */
1772 case 19: /* DECPEX -- Printer extent (IGNORED) */
1773 case 42: /* DECNRCM -- National characters (IGNORED) */
1774 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1776 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1777 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1779 case 9: /* X10 mouse compatibility mode */
1780 xsetpointermotion(0);
1781 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1782 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1784 case 1000: /* 1000: report button press */
1785 xsetpointermotion(0);
1786 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1787 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1789 case 1002: /* 1002: report motion on button press */
1790 xsetpointermotion(0);
1791 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1792 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1794 case 1003: /* 1003: enable all mouse motions */
1795 xsetpointermotion(set
);
1796 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1797 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1799 case 1004: /* 1004: send focus events to tty */
1800 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1802 case 1006: /* 1006: extended reporting mode */
1803 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1806 MODBIT(term
.mode
, set
, MODE_8BIT
);
1808 case 1049: /* = 1047 and 1048 */
1811 if (!allowaltscreen
)
1814 alt
= IS_SET(MODE_ALTSCREEN
);
1816 tclearregion(0, 0, term
.col
-1,
1819 if(set
^ alt
) /* set is always 1 or 0 */
1825 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1827 /* Not implemented mouse modes. See comments there. */
1828 case 1001: /* mouse highlight mode; can hang the
1829 terminal by design when implemented. */
1830 case 1005: /* UTF-8 mouse mode; will confuse
1831 applications not supporting UTF-8
1833 case 1015: /* urxvt mangled mouse mode; incompatible
1834 and can be mistaken for other control
1838 "erresc: unknown private set/reset mode %d\n",
1844 case 0: /* Error (IGNORED) */
1846 case 2: /* KAM -- keyboard action */
1847 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1849 case 4: /* IRM -- Insertion-replacement */
1850 MODBIT(term
.mode
, set
, MODE_INSERT
);
1852 case 12: /* SRM -- Send/Receive */
1853 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1855 case 20: /* LNM -- Linefeed/new line */
1856 MODBIT(term
.mode
, set
, MODE_CRLF
);
1860 "erresc: unknown set/reset mode %d\n",
1870 switch(csiescseq
.mode
) {
1873 fprintf(stderr
, "erresc: unknown csi ");
1877 case '@': /* ICH -- Insert <n> blank char */
1878 DEFAULT(csiescseq
.arg
[0], 1);
1879 tinsertblank(csiescseq
.arg
[0]);
1881 case 'A': /* CUU -- Cursor <n> Up */
1882 DEFAULT(csiescseq
.arg
[0], 1);
1883 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1885 case 'B': /* CUD -- Cursor <n> Down */
1886 case 'e': /* VPR --Cursor <n> Down */
1887 DEFAULT(csiescseq
.arg
[0], 1);
1888 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1890 case 'c': /* DA -- Device Attributes */
1891 if(csiescseq
.arg
[0] == 0)
1892 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1894 case 'C': /* CUF -- Cursor <n> Forward */
1895 case 'a': /* HPR -- Cursor <n> Forward */
1896 DEFAULT(csiescseq
.arg
[0], 1);
1897 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1899 case 'D': /* CUB -- Cursor <n> Backward */
1900 DEFAULT(csiescseq
.arg
[0], 1);
1901 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1903 case 'E': /* CNL -- Cursor <n> Down and first col */
1904 DEFAULT(csiescseq
.arg
[0], 1);
1905 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1907 case 'F': /* CPL -- Cursor <n> Up and first col */
1908 DEFAULT(csiescseq
.arg
[0], 1);
1909 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1911 case 'g': /* TBC -- Tabulation clear */
1912 switch(csiescseq
.arg
[0]) {
1913 case 0: /* clear current tab stop */
1914 term
.tabs
[term
.c
.x
] = 0;
1916 case 3: /* clear all the tabs */
1917 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1923 case 'G': /* CHA -- Move to <col> */
1925 DEFAULT(csiescseq
.arg
[0], 1);
1926 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1928 case 'H': /* CUP -- Move to <row> <col> */
1930 DEFAULT(csiescseq
.arg
[0], 1);
1931 DEFAULT(csiescseq
.arg
[1], 1);
1932 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1934 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1935 DEFAULT(csiescseq
.arg
[0], 1);
1936 while(csiescseq
.arg
[0]--)
1939 case 'J': /* ED -- Clear screen */
1941 switch(csiescseq
.arg
[0]) {
1943 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1944 if(term
.c
.y
< term
.row
-1) {
1945 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1951 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1952 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1955 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1961 case 'K': /* EL -- Clear line */
1962 switch(csiescseq
.arg
[0]) {
1964 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1968 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1971 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1975 case 'S': /* SU -- Scroll <n> line up */
1976 DEFAULT(csiescseq
.arg
[0], 1);
1977 tscrollup(term
.top
, csiescseq
.arg
[0]);
1979 case 'T': /* SD -- Scroll <n> line down */
1980 DEFAULT(csiescseq
.arg
[0], 1);
1981 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1983 case 'L': /* IL -- Insert <n> blank lines */
1984 DEFAULT(csiescseq
.arg
[0], 1);
1985 tinsertblankline(csiescseq
.arg
[0]);
1987 case 'l': /* RM -- Reset Mode */
1988 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1990 case 'M': /* DL -- Delete <n> lines */
1991 DEFAULT(csiescseq
.arg
[0], 1);
1992 tdeleteline(csiescseq
.arg
[0]);
1994 case 'X': /* ECH -- Erase <n> char */
1995 DEFAULT(csiescseq
.arg
[0], 1);
1996 tclearregion(term
.c
.x
, term
.c
.y
,
1997 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1999 case 'P': /* DCH -- Delete <n> char */
2000 DEFAULT(csiescseq
.arg
[0], 1);
2001 tdeletechar(csiescseq
.arg
[0]);
2003 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2004 DEFAULT(csiescseq
.arg
[0], 1);
2005 while(csiescseq
.arg
[0]--)
2008 case 'd': /* VPA -- Move to <row> */
2009 DEFAULT(csiescseq
.arg
[0], 1);
2010 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2012 case 'h': /* SM -- Set terminal mode */
2013 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2015 case 'm': /* SGR -- Terminal attribute (color) */
2016 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2018 case 'r': /* DECSTBM -- Set Scrolling Region */
2019 if(csiescseq
.priv
) {
2022 DEFAULT(csiescseq
.arg
[0], 1);
2023 DEFAULT(csiescseq
.arg
[1], term
.row
);
2024 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2028 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2029 tcursor(CURSOR_SAVE
);
2031 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2032 tcursor(CURSOR_LOAD
);
2043 for(i
= 0; i
< csiescseq
.len
; i
++) {
2044 c
= csiescseq
.buf
[i
] & 0xff;
2047 } else if(c
== '\n') {
2049 } else if(c
== '\r') {
2051 } else if(c
== 0x1b) {
2054 printf("(%02x)", c
);
2062 memset(&csiescseq
, 0, sizeof(csiescseq
));
2071 narg
= strescseq
.narg
;
2073 switch(strescseq
.type
) {
2074 case ']': /* OSC -- Operating System Command */
2075 switch(i
= atoi(strescseq
.args
[0])) {
2080 xsettitle(strescseq
.args
[1]);
2082 case 4: /* color set */
2085 p
= strescseq
.args
[2];
2087 case 104: /* color reset, here p = NULL */
2088 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2089 if (!xsetcolorname(j
, p
)) {
2090 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2093 * TODO if defaultbg color is changed, borders
2100 fprintf(stderr
, "erresc: unknown str ");
2105 case 'k': /* old title set compatibility */
2106 xsettitle(strescseq
.args
[0]);
2108 case 'P': /* DSC -- Device Control String */
2109 case '_': /* APC -- Application Program Command */
2110 case '^': /* PM -- Privacy Message */
2112 fprintf(stderr
, "erresc: unknown str ");
2121 char *p
= strescseq
.buf
;
2124 strescseq
.buf
[strescseq
.len
] = '\0';
2125 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2126 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2134 printf("ESC%c", strescseq
.type
);
2135 for(i
= 0; i
< strescseq
.len
; i
++) {
2136 c
= strescseq
.buf
[i
] & 0xff;
2139 } else if(isprint(c
)) {
2141 } else if(c
== '\n') {
2143 } else if(c
== '\r') {
2145 } else if(c
== 0x1b) {
2148 printf("(%02x)", c
);
2156 memset(&strescseq
, 0, sizeof(strescseq
));
2160 tputtab(bool forward
) {
2166 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2171 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2174 tmoveto(x
, term
.c
.y
);
2178 techo(char *buf
, int len
) {
2179 for(; len
> 0; buf
++, len
--) {
2182 if(c
== '\033') { /* escape */
2185 } else if(c
< '\x20') { /* control code */
2186 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2200 tputc(char *c
, int len
) {
2202 bool control
= ascii
< '\x20' || ascii
== 0177;
2205 if(xwrite(iofd
, c
, len
) < 0) {
2206 fprintf(stderr
, "Error writing in %s:%s\n",
2207 opt_io
, strerror(errno
));
2214 * STR sequences must be checked before anything else
2215 * because it can use some control codes as part of the sequence.
2217 if(term
.esc
& ESC_STR
) {
2220 term
.esc
= ESC_START
| ESC_STR_END
;
2222 case '\a': /* backwards compatibility to xterm */
2227 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2228 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2229 strescseq
.len
+= len
;
2232 * Here is a bug in terminals. If the user never sends
2233 * some code to stop the str or esc command, then st
2234 * will stop responding. But this is better than
2235 * silently failing with unknown characters. At least
2236 * then users will report back.
2238 * In the case users ever get fixed, here is the code:
2250 * Actions of control codes must be performed as soon they arrive
2251 * because they can be embedded inside a control sequence, and
2252 * they must not cause conflicts with sequences.
2260 tmoveto(term
.c
.x
-1, term
.c
.y
);
2263 tmoveto(0, term
.c
.y
);
2268 /* go to first col if the mode is set */
2269 tnewline(IS_SET(MODE_CRLF
));
2271 case '\a': /* BEL */
2272 if(!(xw
.state
& WIN_FOCUSED
))
2275 case '\033': /* ESC */
2277 term
.esc
= ESC_START
;
2279 case '\016': /* SO */
2280 case '\017': /* SI */
2282 * Different charsets are hard to handle. Applications
2283 * should use the right alt charset escapes for the
2284 * only reason they still exist: line drawing. The
2285 * rest is incompatible history st should not support.
2288 case '\032': /* SUB */
2289 case '\030': /* CAN */
2292 case '\005': /* ENQ (IGNORED) */
2293 case '\000': /* NUL (IGNORED) */
2294 case '\021': /* XON (IGNORED) */
2295 case '\023': /* XOFF (IGNORED) */
2296 case 0177: /* DEL (IGNORED) */
2299 } else if(term
.esc
& ESC_START
) {
2300 if(term
.esc
& ESC_CSI
) {
2301 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2302 if(BETWEEN(ascii
, 0x40, 0x7E)
2303 || csiescseq
.len
>= \
2304 sizeof(csiescseq
.buf
)-1) {
2309 } else if(term
.esc
& ESC_STR_END
) {
2313 } else if(term
.esc
& ESC_ALTCHARSET
) {
2315 case '0': /* Line drawing set */
2316 term
.c
.attr
.mode
|= ATTR_GFX
;
2318 case 'B': /* USASCII */
2319 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2321 case 'A': /* UK (IGNORED) */
2322 case '<': /* multinational charset (IGNORED) */
2323 case '5': /* Finnish (IGNORED) */
2324 case 'C': /* Finnish (IGNORED) */
2325 case 'K': /* German (IGNORED) */
2328 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2331 } else if(term
.esc
& ESC_TEST
) {
2332 if(ascii
== '8') { /* DEC screen alignment test. */
2333 char E
[UTF_SIZ
] = "E";
2336 for(x
= 0; x
< term
.col
; ++x
) {
2337 for(y
= 0; y
< term
.row
; ++y
)
2338 tsetchar(E
, &term
.c
.attr
, x
, y
);
2345 term
.esc
|= ESC_CSI
;
2348 term
.esc
|= ESC_TEST
;
2350 case 'P': /* DCS -- Device Control String */
2351 case '_': /* APC -- Application Program Command */
2352 case '^': /* PM -- Privacy Message */
2353 case ']': /* OSC -- Operating System Command */
2354 case 'k': /* old title set compatibility */
2356 strescseq
.type
= ascii
;
2357 term
.esc
|= ESC_STR
;
2359 case '(': /* set primary charset G0 */
2360 term
.esc
|= ESC_ALTCHARSET
;
2362 case ')': /* set secondary charset G1 (IGNORED) */
2363 case '*': /* set tertiary charset G2 (IGNORED) */
2364 case '+': /* set quaternary charset G3 (IGNORED) */
2367 case 'D': /* IND -- Linefeed */
2368 if(term
.c
.y
== term
.bot
) {
2369 tscrollup(term
.top
, 1);
2371 tmoveto(term
.c
.x
, term
.c
.y
+1);
2375 case 'E': /* NEL -- Next line */
2376 tnewline(1); /* always go to first col */
2379 case 'H': /* HTS -- Horizontal tab stop */
2380 term
.tabs
[term
.c
.x
] = 1;
2383 case 'M': /* RI -- Reverse index */
2384 if(term
.c
.y
== term
.top
) {
2385 tscrolldown(term
.top
, 1);
2387 tmoveto(term
.c
.x
, term
.c
.y
-1);
2391 case 'Z': /* DECID -- Identify Terminal */
2392 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2395 case 'c': /* RIS -- Reset to inital state */
2400 case '=': /* DECPAM -- Application keypad */
2401 term
.mode
|= MODE_APPKEYPAD
;
2404 case '>': /* DECPNM -- Normal keypad */
2405 term
.mode
&= ~MODE_APPKEYPAD
;
2408 case '7': /* DECSC -- Save Cursor */
2409 tcursor(CURSOR_SAVE
);
2412 case '8': /* DECRC -- Restore Cursor */
2413 tcursor(CURSOR_LOAD
);
2416 case '\\': /* ST -- Stop */
2420 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2421 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2426 * All characters which form part of a sequence are not
2432 * Display control codes only if we are in graphic mode
2434 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2436 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2438 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2439 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2443 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2444 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2445 &term
.line
[term
.c
.y
][term
.c
.x
],
2446 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2449 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2450 if(term
.c
.x
+1 < term
.col
) {
2451 tmoveto(term
.c
.x
+1, term
.c
.y
);
2453 term
.c
.state
|= CURSOR_WRAPNEXT
;
2458 tresize(int col
, int row
) {
2460 int minrow
= MIN(row
, term
.row
);
2461 int mincol
= MIN(col
, term
.col
);
2462 int slide
= term
.c
.y
- row
+ 1;
2466 if(col
< 1 || row
< 1)
2469 /* free unneeded rows */
2473 * slide screen to keep cursor where we expect it -
2474 * tscrollup would work here, but we can optimize to
2475 * memmove because we're freeing the earlier lines
2477 for(/* i = 0 */; i
< slide
; i
++) {
2481 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2482 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2484 for(i
+= row
; i
< term
.row
; i
++) {
2489 /* resize to new height */
2490 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2491 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2492 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2493 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2495 /* resize each row to new width, zero-pad if needed */
2496 for(i
= 0; i
< minrow
; i
++) {
2498 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2499 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2502 /* allocate any new rows */
2503 for(/* i == minrow */; i
< row
; i
++) {
2505 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2506 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2508 if(col
> term
.col
) {
2509 bp
= term
.tabs
+ term
.col
;
2511 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2512 while(--bp
> term
.tabs
&& !*bp
)
2514 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2517 /* update terminal size */
2520 /* reset scrolling region */
2521 tsetscroll(0, row
-1);
2522 /* make use of the LIMIT in tmoveto */
2523 tmoveto(term
.c
.x
, term
.c
.y
);
2524 /* Clearing both screens */
2527 if(mincol
< col
&& 0 < minrow
) {
2528 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2530 if(0 < col
&& minrow
< row
) {
2531 tclearregion(0, minrow
, col
- 1, row
- 1);
2534 } while(orig
!= term
.line
);
2540 xresize(int col
, int row
) {
2541 xw
.tw
= MAX(1, col
* xw
.cw
);
2542 xw
.th
= MAX(1, row
* xw
.ch
);
2544 XFreePixmap(xw
.dpy
, xw
.buf
);
2545 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2546 DefaultDepth(xw
.dpy
, xw
.scr
));
2547 XftDrawChange(xw
.draw
, xw
.buf
);
2548 xclear(0, 0, xw
.w
, xw
.h
);
2551 static inline ushort
2552 sixd_to_16bit(int x
) {
2553 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2559 XRenderColor color
= { .alpha
= 0xffff };
2561 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2562 for(i
= 0; i
< LEN(colorname
); i
++) {
2565 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2566 die("Could not allocate color '%s'\n", colorname
[i
]);
2570 /* load colors [16-255] ; same colors as xterm */
2571 for(i
= 16, r
= 0; r
< 6; r
++) {
2572 for(g
= 0; g
< 6; g
++) {
2573 for(b
= 0; b
< 6; b
++) {
2574 color
.red
= sixd_to_16bit(r
);
2575 color
.green
= sixd_to_16bit(g
);
2576 color
.blue
= sixd_to_16bit(b
);
2577 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2578 die("Could not allocate color %d\n", i
);
2585 for(r
= 0; r
< 24; r
++, i
++) {
2586 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2587 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2589 die("Could not allocate color %d\n", i
);
2595 xsetcolorname(int x
, const char *name
) {
2596 XRenderColor color
= { .alpha
= 0xffff };
2598 if (x
< 0 || x
> LEN(colorname
))
2601 if(16 <= x
&& x
< 16 + 216) {
2602 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2603 color
.red
= sixd_to_16bit(r
);
2604 color
.green
= sixd_to_16bit(g
);
2605 color
.blue
= sixd_to_16bit(b
);
2606 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2607 return 0; /* something went wrong */
2610 } else if (16 + 216 <= x
&& x
< 256) {
2611 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2612 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2613 return 0; /* something went wrong */
2617 name
= colorname
[x
];
2620 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2627 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2628 XftDrawRect(xw
.draw
,
2629 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2630 borderpx
+ col1
* xw
.cw
,
2631 borderpx
+ row1
* xw
.ch
,
2632 (col2
-col1
+1) * xw
.cw
,
2633 (row2
-row1
+1) * xw
.ch
);
2637 * Absolute coordinates.
2640 xclear(int x1
, int y1
, int x2
, int y2
) {
2641 XftDrawRect(xw
.draw
,
2642 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2643 x1
, y1
, x2
-x1
, y2
-y1
);
2648 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2649 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2650 XSizeHints
*sizeh
= NULL
;
2652 sizeh
= XAllocSizeHints();
2653 if(xw
.isfixed
== False
) {
2654 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2655 sizeh
->height
= xw
.h
;
2656 sizeh
->width
= xw
.w
;
2657 sizeh
->height_inc
= xw
.ch
;
2658 sizeh
->width_inc
= xw
.cw
;
2659 sizeh
->base_height
= 2 * borderpx
;
2660 sizeh
->base_width
= 2 * borderpx
;
2662 sizeh
->flags
= PMaxSize
| PMinSize
;
2663 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2664 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2667 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2672 xloadfont(Font
*f
, FcPattern
*pattern
) {
2676 match
= FcFontMatch(NULL
, pattern
, &result
);
2680 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2681 FcPatternDestroy(match
);
2686 f
->pattern
= FcPatternDuplicate(pattern
);
2688 f
->ascent
= f
->match
->ascent
;
2689 f
->descent
= f
->match
->descent
;
2691 f
->rbearing
= f
->match
->max_advance_width
;
2693 f
->height
= f
->ascent
+ f
->descent
;
2694 f
->width
= f
->lbearing
+ f
->rbearing
;
2700 xloadfonts(char *fontstr
, int fontsize
) {
2705 if(fontstr
[0] == '-') {
2706 pattern
= XftXlfdParse(fontstr
, False
, False
);
2708 pattern
= FcNameParse((FcChar8
*)fontstr
);
2712 die("st: can't open font %s\n", fontstr
);
2715 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2716 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2717 usedfontsize
= fontsize
;
2719 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2720 if(result
== FcResultMatch
) {
2721 usedfontsize
= (int)fontval
;
2724 * Default font size is 12, if none given. This is to
2725 * have a known usedfontsize value.
2727 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2732 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2733 FcDefaultSubstitute(pattern
);
2735 if(xloadfont(&dc
.font
, pattern
))
2736 die("st: can't open font %s\n", fontstr
);
2738 /* Setting character width and height. */
2739 xw
.cw
= dc
.font
.width
;
2740 xw
.ch
= dc
.font
.height
;
2742 FcPatternDel(pattern
, FC_SLANT
);
2743 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2744 if(xloadfont(&dc
.ifont
, pattern
))
2745 die("st: can't open font %s\n", fontstr
);
2747 FcPatternDel(pattern
, FC_WEIGHT
);
2748 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2749 if(xloadfont(&dc
.ibfont
, pattern
))
2750 die("st: can't open font %s\n", fontstr
);
2752 FcPatternDel(pattern
, FC_SLANT
);
2753 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2754 if(xloadfont(&dc
.bfont
, pattern
))
2755 die("st: can't open font %s\n", fontstr
);
2757 FcPatternDestroy(pattern
);
2761 xloadfontset(Font
*f
) {
2764 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2770 xunloadfont(Font
*f
) {
2771 XftFontClose(xw
.dpy
, f
->match
);
2772 FcPatternDestroy(f
->pattern
);
2774 FcFontSetDestroy(f
->set
);
2778 xunloadfonts(void) {
2781 /* Free the loaded fonts in the font cache. */
2782 for(i
= 0; i
< frclen
; i
++) {
2783 XftFontClose(xw
.dpy
, frc
[i
].font
);
2787 xunloadfont(&dc
.font
);
2788 xunloadfont(&dc
.bfont
);
2789 xunloadfont(&dc
.ifont
);
2790 xunloadfont(&dc
.ibfont
);
2794 xzoom(const Arg
*arg
) {
2796 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2808 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2809 die("Can't open display\n");
2810 xw
.scr
= XDefaultScreen(xw
.dpy
);
2811 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2815 die("Could not init fontconfig.\n");
2817 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2818 xloadfonts(usedfont
, 0);
2821 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2824 /* adjust fixed window geometry */
2826 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2827 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2829 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2831 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2836 /* window - default size */
2837 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2838 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2844 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2845 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2846 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2847 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2848 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2849 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2850 xw
.attrs
.colormap
= xw
.cmap
;
2852 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2853 XRootWindow(xw
.dpy
, xw
.scr
);
2854 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2855 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2856 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2857 | CWEventMask
| CWColormap
, &xw
.attrs
);
2859 memset(&gcvalues
, 0, sizeof(gcvalues
));
2860 gcvalues
.graphics_exposures
= False
;
2861 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2863 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2864 DefaultDepth(xw
.dpy
, xw
.scr
));
2865 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2866 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2868 /* Xft rendering context */
2869 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2872 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2873 XSetLocaleModifiers("@im=local");
2874 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2875 XSetLocaleModifiers("@im=");
2876 if((xw
.xim
= XOpenIM(xw
.dpy
,
2877 NULL
, NULL
, NULL
)) == NULL
) {
2878 die("XOpenIM failed. Could not open input"
2883 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2884 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2885 XNFocusWindow
, xw
.win
, NULL
);
2887 die("XCreateIC failed. Could not obtain input method.\n");
2889 /* white cursor, black outline */
2890 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2891 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2892 XRecolorCursor(xw
.dpy
, cursor
,
2893 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2894 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2896 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2897 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2898 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2901 XMapWindow(xw
.dpy
, xw
.win
);
2907 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2908 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2909 width
= charlen
* xw
.cw
, xp
, i
;
2911 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2914 Font
*font
= &dc
.font
;
2916 FcPattern
*fcpattern
, *fontpattern
;
2917 FcFontSet
*fcsets
[] = { NULL
};
2918 FcCharSet
*fccharset
;
2919 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2920 XRenderColor colfg
, colbg
;
2923 frcflags
= FRC_NORMAL
;
2925 if(base
.mode
& ATTR_ITALIC
) {
2926 if(base
.fg
== defaultfg
)
2927 base
.fg
= defaultitalic
;
2929 frcflags
= FRC_ITALIC
;
2930 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2931 if(base
.fg
== defaultfg
)
2932 base
.fg
= defaultitalic
;
2934 frcflags
= FRC_ITALICBOLD
;
2935 } else if(base
.mode
& ATTR_UNDERLINE
) {
2936 if(base
.fg
== defaultfg
)
2937 base
.fg
= defaultunderline
;
2939 fg
= &dc
.col
[base
.fg
];
2940 bg
= &dc
.col
[base
.bg
];
2942 if(base
.mode
& ATTR_BOLD
) {
2943 if(BETWEEN(base
.fg
, 0, 7)) {
2944 /* basic system colors */
2945 fg
= &dc
.col
[base
.fg
+ 8];
2946 } else if(BETWEEN(base
.fg
, 16, 195)) {
2948 fg
= &dc
.col
[base
.fg
+ 36];
2949 } else if(BETWEEN(base
.fg
, 232, 251)) {
2951 fg
= &dc
.col
[base
.fg
+ 4];
2954 * Those ranges will not be brightened:
2955 * 8 - 15 – bright system colors
2956 * 196 - 231 – highest 256 color cube
2957 * 252 - 255 – brightest colors in greyscale
2960 frcflags
= FRC_BOLD
;
2963 if(IS_SET(MODE_REVERSE
)) {
2964 if(fg
== &dc
.col
[defaultfg
]) {
2965 fg
= &dc
.col
[defaultbg
];
2967 colfg
.red
= ~fg
->color
.red
;
2968 colfg
.green
= ~fg
->color
.green
;
2969 colfg
.blue
= ~fg
->color
.blue
;
2970 colfg
.alpha
= fg
->color
.alpha
;
2971 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2975 if(bg
== &dc
.col
[defaultbg
]) {
2976 bg
= &dc
.col
[defaultfg
];
2978 colbg
.red
= ~bg
->color
.red
;
2979 colbg
.green
= ~bg
->color
.green
;
2980 colbg
.blue
= ~bg
->color
.blue
;
2981 colbg
.alpha
= bg
->color
.alpha
;
2982 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2987 if(base
.mode
& ATTR_REVERSE
) {
2993 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
2996 /* Intelligent cleaning up of the borders. */
2998 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2999 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3001 if(x
+ charlen
>= term
.col
) {
3002 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3003 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3006 xclear(winx
, 0, winx
+ width
, borderpx
);
3008 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3010 /* Clean up the region we want to draw to. */
3011 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3013 /* Set the clip region because Xft is sometimes dirty. */
3018 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3020 for(xp
= winx
; bytelen
> 0;) {
3022 * Search for the range in the to be printed string of glyphs
3023 * that are in the main font. Then print that range. If
3024 * some glyph is found that is not in the font, do the
3032 u8cblen
= utf8decode(s
, &u8char
);
3036 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3037 if(!doesexist
|| bytelen
<= 0) {
3046 XftDrawStringUtf8(xw
.draw
, fg
,
3048 winy
+ font
->ascent
,
3051 xp
+= font
->width
* u8fl
;
3063 /* Search the font cache. */
3064 for(i
= 0; i
< frclen
; i
++) {
3065 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3066 && frc
[i
].flags
== frcflags
) {
3071 /* Nothing was found. */
3075 fcsets
[0] = font
->set
;
3078 * Nothing was found in the cache. Now use
3079 * some dozen of Fontconfig calls to get the
3080 * font for one single character.
3082 fcpattern
= FcPatternDuplicate(font
->pattern
);
3083 fccharset
= FcCharSetCreate();
3085 FcCharSetAddChar(fccharset
, u8char
);
3086 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3088 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3091 FcConfigSubstitute(0, fcpattern
,
3093 FcDefaultSubstitute(fcpattern
);
3095 fontpattern
= FcFontSetMatch(0, fcsets
,
3096 FcTrue
, fcpattern
, &fcres
);
3099 * Overwrite or create the new cache entry.
3101 if(frclen
>= LEN(frc
)) {
3102 frclen
= LEN(frc
) - 1;
3103 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3106 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3108 frc
[frclen
].flags
= frcflags
;
3113 FcPatternDestroy(fcpattern
);
3114 FcCharSetDestroy(fccharset
);
3117 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3118 xp
, winy
+ frc
[i
].font
->ascent
,
3119 (FcChar8
*)u8c
, u8cblen
);
3125 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3126 winy + font->ascent, (FcChar8 *)s, bytelen);
3129 if(base
.mode
& ATTR_UNDERLINE
) {
3130 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3134 /* Reset clip to none. */
3135 XftDrawSetClip(xw
.draw
, 0);
3140 static int oldx
= 0, oldy
= 0;
3142 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3144 LIMIT(oldx
, 0, term
.col
-1);
3145 LIMIT(oldy
, 0, term
.row
-1);
3147 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3149 /* remove the old cursor */
3150 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3151 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3154 /* draw the new one */
3155 if(!(IS_SET(MODE_HIDE
))) {
3156 if(xw
.state
& WIN_FOCUSED
) {
3157 if(IS_SET(MODE_REVERSE
)) {
3158 g
.mode
|= ATTR_REVERSE
;
3164 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3166 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3167 borderpx
+ term
.c
.x
* xw
.cw
,
3168 borderpx
+ term
.c
.y
* xw
.ch
,
3170 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3171 borderpx
+ term
.c
.x
* xw
.cw
,
3172 borderpx
+ term
.c
.y
* xw
.ch
,
3174 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3175 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3176 borderpx
+ term
.c
.y
* xw
.ch
,
3178 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3179 borderpx
+ term
.c
.x
* xw
.cw
,
3180 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3183 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3189 xsettitle(char *p
) {
3192 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3194 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3199 xsettitle(opt_title
? opt_title
: "st");
3203 redraw(int timeout
) {
3204 struct timespec tv
= {0, timeout
* 1000};
3210 nanosleep(&tv
, NULL
);
3211 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3217 drawregion(0, 0, term
.col
, term
.row
);
3218 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3220 XSetForeground(xw
.dpy
, dc
.gc
,
3221 dc
.col
[IS_SET(MODE_REVERSE
)?
3222 defaultfg
: defaultbg
].pixel
);
3226 drawregion(int x1
, int y1
, int x2
, int y2
) {
3227 int ic
, ib
, x
, y
, ox
, sl
;
3229 char buf
[DRAW_BUF_SIZ
];
3230 bool ena_sel
= sel
.ob
.x
!= -1;
3232 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3235 if(!(xw
.state
& WIN_VISIBLE
))
3238 for(y
= y1
; y
< y2
; y
++) {
3242 xtermclear(0, y
, term
.col
, y
);
3244 base
= term
.line
[y
][0];
3246 for(x
= x1
; x
< x2
; x
++) {
3247 new = term
.line
[y
][x
];
3248 if(ena_sel
&& selected(x
, y
))
3249 new.mode
^= ATTR_REVERSE
;
3250 if(ib
> 0 && (ATTRCMP(base
, new)
3251 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3252 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3260 sl
= utf8size(new.c
);
3261 memcpy(buf
+ib
, new.c
, sl
);
3266 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3272 expose(XEvent
*ev
) {
3273 XExposeEvent
*e
= &ev
->xexpose
;
3275 if(xw
.state
& WIN_REDRAW
) {
3277 xw
.state
&= ~WIN_REDRAW
;
3283 visibility(XEvent
*ev
) {
3284 XVisibilityEvent
*e
= &ev
->xvisibility
;
3286 if(e
->state
== VisibilityFullyObscured
) {
3287 xw
.state
&= ~WIN_VISIBLE
;
3288 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3289 /* need a full redraw for next Expose, not just a buf copy */
3290 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3296 xw
.state
&= ~WIN_VISIBLE
;
3300 xsetpointermotion(int set
) {
3301 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3302 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3306 xseturgency(int add
) {
3307 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3309 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3310 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3316 XFocusChangeEvent
*e
= &ev
->xfocus
;
3318 if(e
->mode
== NotifyGrab
)
3321 if(ev
->type
== FocusIn
) {
3322 XSetICFocus(xw
.xic
);
3323 xw
.state
|= WIN_FOCUSED
;
3325 if(IS_SET(MODE_FOCUS
))
3326 ttywrite("\033[I", 3);
3328 XUnsetICFocus(xw
.xic
);
3329 xw
.state
&= ~WIN_FOCUSED
;
3330 if(IS_SET(MODE_FOCUS
))
3331 ttywrite("\033[O", 3);
3336 match(uint mask
, uint state
) {
3337 state
&= ~ignoremod
;
3339 if(mask
== XK_NO_MOD
&& state
)
3341 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3343 if(mask
== XK_ANY_MOD
)
3345 return state
== mask
;
3349 numlock(const Arg
*dummy
) {
3354 kmap(KeySym k
, uint state
) {
3358 /* Check for mapped keys out of X11 function keys. */
3359 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3360 if(mappedkeys
[i
] == k
)
3363 if(i
== LEN(mappedkeys
)) {
3364 if((k
& 0xFFFF) < 0xFD00)
3368 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3372 if(!match(kp
->mask
, state
))
3375 if(kp
->appkey
> 0) {
3376 if(!IS_SET(MODE_APPKEYPAD
))
3378 if(term
.numlock
&& kp
->appkey
== 2)
3380 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3384 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3386 && !IS_SET(MODE_APPCURSOR
))) {
3390 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3391 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3402 kpress(XEvent
*ev
) {
3403 XKeyEvent
*e
= &ev
->xkey
;
3405 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3411 if(IS_SET(MODE_KBDLOCK
))
3414 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3415 e
->state
&= ~Mod2Mask
;
3417 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3418 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3419 bp
->func(&(bp
->arg
));
3424 /* 2. custom keys from config.h */
3425 if((customkey
= kmap(ksym
, e
->state
))) {
3426 len
= strlen(customkey
);
3427 memcpy(buf
, customkey
, len
);
3428 /* 3. hardcoded (overrides X lookup) */
3433 if(len
== 1 && e
->state
& Mod1Mask
) {
3434 if(IS_SET(MODE_8BIT
)) {
3437 ret
= utf8encode(&c
, cp
);
3446 memcpy(cp
, xstr
, len
);
3447 len
= cp
- buf
+ len
;
3451 if(IS_SET(MODE_ECHO
))
3457 cmessage(XEvent
*e
) {
3460 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3462 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3463 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3464 xw
.state
|= WIN_FOCUSED
;
3466 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3467 xw
.state
&= ~WIN_FOCUSED
;
3469 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3470 /* Send SIGHUP to shell */
3477 cresize(int width
, int height
) {
3485 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3486 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3495 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3498 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3504 int w
= xw
.w
, h
= xw
.h
;
3506 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3507 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3509 /* Waiting for window mapping */
3511 XNextEvent(xw
.dpy
, &ev
);
3512 if(ev
.type
== ConfigureNotify
) {
3513 w
= ev
.xconfigure
.width
;
3514 h
= ev
.xconfigure
.height
;
3515 } else if(ev
.type
== MapNotify
) {
3523 cresize(xw
.fw
, xw
.fh
);
3526 gettimeofday(&lastblink
, NULL
);
3527 gettimeofday(&last
, NULL
);
3529 for(xev
= actionfps
;;) {
3531 FD_SET(cmdfd
, &rfd
);
3534 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3537 die("select failed: %s\n", SERRNO
);
3539 if(FD_ISSET(cmdfd
, &rfd
)) {
3542 blinkset
= tattrset(ATTR_BLINK
);
3543 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3544 term
.mode
&= ~(MODE_BLINK
);
3548 if(FD_ISSET(xfd
, &rfd
))
3551 gettimeofday(&now
, NULL
);
3552 drawtimeout
.tv_sec
= 0;
3553 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3557 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3558 tsetdirtattr(ATTR_BLINK
);
3559 term
.mode
^= MODE_BLINK
;
3560 gettimeofday(&lastblink
, NULL
);
3563 if(TIMEDIFF(now
, last
) \
3564 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3570 while(XPending(xw
.dpy
)) {
3571 XNextEvent(xw
.dpy
, &ev
);
3572 if(XFilterEvent(&ev
, None
))
3574 if(handler
[ev
.type
])
3575 (handler
[ev
.type
])(&ev
);
3581 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3583 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3585 if(TIMEDIFF(now
, lastblink
) \
3587 drawtimeout
.tv_usec
= 1;
3589 drawtimeout
.tv_usec
= (1000 * \
3604 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3605 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3606 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3610 main(int argc
, char *argv
[]) {
3615 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3620 allowaltscreen
= false;
3623 opt_class
= EARGF(usage());
3626 /* eat all remaining arguments */
3629 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3630 titles
= strdup(argv
[1]);
3631 opt_title
= basename(titles
);
3636 opt_font
= EARGF(usage());
3639 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3644 if(bitm
& WidthValue
)
3646 if(bitm
& HeightValue
)
3648 if(bitm
& XNegative
&& xw
.fx
== 0)
3650 if(bitm
& XNegative
&& xw
.fy
== 0)
3653 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3657 opt_io
= EARGF(usage());
3660 opt_title
= EARGF(usage());
3663 opt_embed
= EARGF(usage());
3671 setlocale(LC_CTYPE
, "");
3672 XSetLocaleModifiers("");