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,
116 MODE_APPCURSOR
= 2048,
117 MODE_MOUSESGR
= 4096,
127 ESC_STR
= 4, /* DSC, OSC, PM, APC */
129 ESC_STR_END
= 16, /* a final string was encountered */
130 ESC_TEST
= 32, /* Enter in test mode */
139 enum selection_type
{
144 enum selection_snap
{
151 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
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
;
223 bool isfixed
; /* is fixed geometry? */
224 int fx
, fy
, fw
, fh
; /* fixed geometry */
225 int tw
, th
; /* tty width and height */
226 int w
, h
; /* window width and height */
227 int ch
; /* char height */
228 int cw
; /* char width */
229 char state
; /* focus, redraw, visible */
242 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
243 signed char appkey
; /* application keypad */
244 signed char appcursor
; /* application cursor */
245 signed char crlf
; /* crlf mode */
248 /* TODO: use better name for vars... */
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 xseturgency(int);
377 static void xsetsel(char*);
378 static void xtermclear(int, int, int, int);
379 static void xunloadfont(Font
*f
);
380 static void xunloadfonts(void);
381 static void xresize(int, int);
383 static void expose(XEvent
*);
384 static void visibility(XEvent
*);
385 static void unmap(XEvent
*);
386 static char *kmap(KeySym
, uint
);
387 static void kpress(XEvent
*);
388 static void cmessage(XEvent
*);
389 static void cresize(int, int);
390 static void resize(XEvent
*);
391 static void focus(XEvent
*);
392 static void brelease(XEvent
*);
393 static void bpress(XEvent
*);
394 static void bmotion(XEvent
*);
395 static void selnotify(XEvent
*);
396 static void selclear(XEvent
*);
397 static void selrequest(XEvent
*);
399 static void selinit(void);
400 static void selsort(void);
401 static inline bool selected(int, int);
402 static void selcopy(void);
403 static void selscroll(int, int);
404 static void selsnap(int, int *, int *, int);
406 static int utf8decode(char *, long *);
407 static int utf8encode(long *, char *);
408 static int utf8size(char *);
409 static int isfullutf8(char *, int);
411 static ssize_t
xwrite(int, char *, size_t);
412 static void *xmalloc(size_t);
413 static void *xrealloc(void *, size_t);
414 static void *xcalloc(size_t, size_t);
416 static void (*handler
[LASTEvent
])(XEvent
*) = {
418 [ClientMessage
] = cmessage
,
419 [ConfigureNotify
] = resize
,
420 [VisibilityNotify
] = visibility
,
421 [UnmapNotify
] = unmap
,
425 [MotionNotify
] = bmotion
,
426 [ButtonPress
] = bpress
,
427 [ButtonRelease
] = brelease
,
428 [SelectionClear
] = selclear
,
429 [SelectionNotify
] = selnotify
,
430 [SelectionRequest
] = selrequest
,
437 static CSIEscape csiescseq
;
438 static STREscape strescseq
;
441 static Selection sel
;
442 static int iofd
= -1;
443 static char **opt_cmd
= NULL
;
444 static char *opt_io
= NULL
;
445 static char *opt_title
= NULL
;
446 static char *opt_embed
= NULL
;
447 static char *opt_class
= NULL
;
448 static char *opt_font
= NULL
;
450 static char *usedfont
= NULL
;
451 static int usedfontsize
= 0;
453 /* Font Ring Cache */
468 * Fontcache is a ring buffer, with frccur as current position and frclen as
469 * the current length of used elements.
472 static Fontcache frc
[1024];
473 static int frccur
= -1, frclen
= 0;
476 xwrite(int fd
, char *s
, size_t len
) {
480 ssize_t r
= write(fd
, s
, len
);
490 xmalloc(size_t len
) {
491 void *p
= malloc(len
);
494 die("Out of memory\n");
500 xrealloc(void *p
, size_t len
) {
501 if((p
= realloc(p
, len
)) == NULL
)
502 die("Out of memory\n");
508 xcalloc(size_t nmemb
, size_t size
) {
509 void *p
= calloc(nmemb
, size
);
512 die("Out of memory\n");
518 utf8decode(char *s
, long *u
) {
524 if(~c
& B7
) { /* 0xxxxxxx */
527 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
528 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
530 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
531 *u
= c
&(B3
|B2
|B1
|B0
);
533 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
540 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
542 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
545 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
548 if((n
== 1 && *u
< 0x80) ||
549 (n
== 2 && *u
< 0x800) ||
550 (n
== 3 && *u
< 0x10000) ||
551 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
563 utf8encode(long *u
, char *s
) {
571 *sp
= uc
; /* 0xxxxxxx */
573 } else if(*u
< 0x800) {
574 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
576 } else if(uc
< 0x10000) {
577 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
579 } else if(uc
<= 0x10FFFF) {
580 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
586 for(i
=n
,++sp
; i
>0; --i
,++sp
)
587 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
599 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
600 UTF-8 otherwise return 0 */
602 isfullutf8(char *s
, int b
) {
610 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
612 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
614 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
616 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
618 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
619 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
632 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
634 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
643 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
644 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
648 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
649 if(sel
.xtarget
== None
)
650 sel
.xtarget
= XA_STRING
;
658 return LIMIT(x
, 0, term
.col
-1);
666 return LIMIT(y
, 0, term
.row
-1);
671 if(sel
.ob
.y
== sel
.oe
.y
) {
672 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
673 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
675 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
676 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
678 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
679 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
683 selected(int x
, int y
) {
684 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
685 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
687 if(sel
.type
== SEL_RECTANGULAR
) {
688 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
689 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
692 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
693 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
694 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
695 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
699 selsnap(int mode
, int *x
, int *y
, int direction
) {
705 * Snap around if the word wraps around at the end or
706 * beginning of a line.
709 if(direction
< 0 && *x
<= 0) {
710 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
718 if(direction
> 0 && *x
>= term
.col
-1) {
719 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
728 if(strchr(worddelimiters
,
729 term
.line
[*y
][*x
+ direction
].c
[0])) {
738 * Snap around if the the previous line or the current one
739 * has set ATTR_WRAP at its end. Then the whole next or
740 * previous line will be selected.
742 *x
= (direction
< 0) ? 0 : term
.col
- 1;
743 if(direction
< 0 && *y
> 0) {
744 for(; *y
> 0; *y
+= direction
) {
745 if(!(term
.line
[*y
-1][term
.col
-1].mode
750 } else if(direction
> 0 && *y
< term
.row
-1) {
751 for(; *y
< term
.row
; *y
+= direction
) {
752 if(!(term
.line
[*y
][term
.col
-1].mode
761 * Select the whole line when the end of line is reached.
765 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
775 getbuttoninfo(XEvent
*e
) {
777 uint state
= e
->xbutton
.state
&~Button1Mask
;
779 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
781 sel
.oe
.x
= x2col(e
->xbutton
.x
);
782 sel
.oe
.y
= y2row(e
->xbutton
.y
);
784 if(sel
.ob
.y
< sel
.oe
.y
785 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
786 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
787 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
789 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
790 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
794 sel
.type
= SEL_REGULAR
;
795 for(type
= 1; type
< LEN(selmasks
); ++type
) {
796 if(match(selmasks
[type
], state
)) {
804 mousereport(XEvent
*e
) {
805 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
806 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
809 static int ob
, ox
, oy
;
812 if(e
->xbutton
.type
== MotionNotify
) {
813 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
818 } else if(!IS_SET(MODE_MOUSESGR
)
819 && (e
->xbutton
.type
== ButtonRelease
820 || button
== AnyButton
)) {
826 if(e
->xbutton
.type
== ButtonPress
) {
833 button
+= (state
& ShiftMask
? 4 : 0)
834 + (state
& Mod4Mask
? 8 : 0)
835 + (state
& ControlMask
? 16 : 0);
838 if(IS_SET(MODE_MOUSESGR
)) {
839 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
841 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
842 } else if(x
< 223 && y
< 223) {
843 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
844 32+button
, 32+x
+1, 32+y
+1);
857 if(IS_SET(MODE_MOUSE
)) {
862 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
863 if(e
->xbutton
.button
== mk
->b
864 && match(mk
->mask
, e
->xbutton
.state
)) {
865 ttywrite(mk
->s
, strlen(mk
->s
));
866 if(IS_SET(MODE_ECHO
))
867 techo(mk
->s
, strlen(mk
->s
));
872 if(e
->xbutton
.button
== Button1
) {
873 gettimeofday(&now
, NULL
);
875 /* Clear previous selection, logically and visually. */
878 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
882 sel
.type
= SEL_REGULAR
;
883 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
884 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
887 * If the user clicks below predefined timeouts specific
888 * snapping behaviour is exposed.
890 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
891 sel
.snap
= SNAP_LINE
;
892 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
893 sel
.snap
= SNAP_WORD
;
897 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
898 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
902 * Draw selection, unless it's regular and we don't want to
903 * make clicks visible
907 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
910 sel
.tclick2
= sel
.tclick1
;
918 int x
, y
, bufsize
, size
, i
, ex
;
924 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
925 ptr
= str
= xmalloc(bufsize
);
927 /* append every set & selected glyph to the selection */
928 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
929 gp
= &term
.line
[y
][0];
930 last
= gp
+ term
.col
;
932 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
933 strcmp(last
->c
, " ") != 0))
936 for(x
= 0; gp
<= last
; x
++, ++gp
) {
940 size
= utf8size(gp
->c
);
941 memcpy(ptr
, gp
->c
, size
);
946 * Copy and pasting of line endings is inconsistent
947 * in the inconsistent terminal and GUI world.
948 * The best solution seems like to produce '\n' when
949 * something is copied from st and convert '\n' to
950 * '\r', when something to be pasted is received by
952 * FIXME: Fix the computer world.
954 if(y
< sel
.ne
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
958 * If the last selected line expands in the selection
959 * after the visible text '\n' is appended.
963 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
966 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
978 selnotify(XEvent
*e
) {
979 ulong nitems
, ofs
, rem
;
981 uchar
*data
, *last
, *repl
;
986 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
987 False
, AnyPropertyType
, &type
, &format
,
988 &nitems
, &rem
, &data
)) {
989 fprintf(stderr
, "Clipboard allocation failed\n");
994 * As seen in selcopy:
995 * Line endings are inconsistent in the terminal and GUI world
996 * copy and pasting. When receiving some selection data,
997 * replace all '\n' with '\r'.
998 * FIXME: Fix the computer world.
1001 last
= data
+ nitems
* format
/ 8;
1002 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1006 ttywrite((const char *)data
, nitems
* format
/ 8);
1008 /* number of 32-bit chunks returned */
1009 ofs
+= nitems
* format
/ 32;
1014 selpaste(const Arg
*dummy
) {
1015 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1016 xw
.win
, CurrentTime
);
1020 clippaste(const Arg
*dummy
) {
1023 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1024 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1025 xw
.win
, CurrentTime
);
1029 selclear(XEvent
*e
) {
1033 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1037 selrequest(XEvent
*e
) {
1038 XSelectionRequestEvent
*xsre
;
1039 XSelectionEvent xev
;
1040 Atom xa_targets
, string
;
1042 xsre
= (XSelectionRequestEvent
*) e
;
1043 xev
.type
= SelectionNotify
;
1044 xev
.requestor
= xsre
->requestor
;
1045 xev
.selection
= xsre
->selection
;
1046 xev
.target
= xsre
->target
;
1047 xev
.time
= xsre
->time
;
1049 xev
.property
= None
;
1051 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1052 if(xsre
->target
== xa_targets
) {
1053 /* respond with the supported type */
1054 string
= sel
.xtarget
;
1055 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1056 XA_ATOM
, 32, PropModeReplace
,
1057 (uchar
*) &string
, 1);
1058 xev
.property
= xsre
->property
;
1059 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1060 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1061 xsre
->target
, 8, PropModeReplace
,
1062 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1063 xev
.property
= xsre
->property
;
1066 /* all done, send a notification to the listener */
1067 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1068 fprintf(stderr
, "Error sending SelectionNotify event\n");
1072 xsetsel(char *str
) {
1073 /* register the selection for both the clipboard and the primary */
1079 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1081 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1082 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1086 brelease(XEvent
*e
) {
1087 if(IS_SET(MODE_MOUSE
)) {
1092 if(e
->xbutton
.button
== Button2
) {
1094 } else if(e
->xbutton
.button
== Button1
) {
1102 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1107 bmotion(XEvent
*e
) {
1108 int oldey
, oldex
, oldsby
, oldsey
;
1110 if(IS_SET(MODE_MOUSE
)) {
1125 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1126 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1130 die(const char *errstr
, ...) {
1133 va_start(ap
, errstr
);
1134 vfprintf(stderr
, errstr
, ap
);
1142 char *envshell
= getenv("SHELL");
1143 const struct passwd
*pass
= getpwuid(getuid());
1144 char buf
[sizeof(long) * 8 + 1];
1146 unsetenv("COLUMNS");
1148 unsetenv("TERMCAP");
1151 setenv("LOGNAME", pass
->pw_name
, 1);
1152 setenv("USER", pass
->pw_name
, 1);
1153 setenv("SHELL", pass
->pw_shell
, 0);
1154 setenv("HOME", pass
->pw_dir
, 0);
1157 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1158 setenv("WINDOWID", buf
, 1);
1160 signal(SIGCHLD
, SIG_DFL
);
1161 signal(SIGHUP
, SIG_DFL
);
1162 signal(SIGINT
, SIG_DFL
);
1163 signal(SIGQUIT
, SIG_DFL
);
1164 signal(SIGTERM
, SIG_DFL
);
1165 signal(SIGALRM
, SIG_DFL
);
1167 DEFAULT(envshell
, shell
);
1168 setenv("TERM", termname
, 1);
1169 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1170 execvp(args
[0], args
);
1178 if(waitpid(pid
, &stat
, 0) < 0)
1179 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1181 if(WIFEXITED(stat
)) {
1182 exit(WEXITSTATUS(stat
));
1191 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1193 /* seems to work fine on linux, openbsd and freebsd */
1194 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1195 die("openpty failed: %s\n", SERRNO
);
1197 switch(pid
= fork()) {
1199 die("fork failed\n");
1202 setsid(); /* create a new process group */
1203 dup2(s
, STDIN_FILENO
);
1204 dup2(s
, STDOUT_FILENO
);
1205 dup2(s
, STDERR_FILENO
);
1206 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1207 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1215 signal(SIGCHLD
, sigchld
);
1217 iofd
= (!strcmp(opt_io
, "-")) ?
1219 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1221 fprintf(stderr
, "Error opening %s:%s\n",
1222 opt_io
, strerror(errno
));
1232 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1234 fprintf(stderr
, "\n");
1239 static char buf
[BUFSIZ
];
1240 static int buflen
= 0;
1243 int charsize
; /* size of utf8 char in bytes */
1247 /* append read bytes to unprocessed bytes */
1248 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1249 die("Couldn't read from shell: %s\n", SERRNO
);
1251 /* process every complete utf8 char */
1254 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1255 charsize
= utf8decode(ptr
, &utf8c
);
1256 utf8encode(&utf8c
, s
);
1262 /* keep any uncomplete utf8 char for the next call */
1263 memmove(buf
, ptr
, buflen
);
1267 ttywrite(const char *s
, size_t n
) {
1268 if(write(cmdfd
, s
, n
) == -1)
1269 die("write error on tty: %s\n", SERRNO
);
1276 w
.ws_row
= term
.row
;
1277 w
.ws_col
= term
.col
;
1278 w
.ws_xpixel
= xw
.tw
;
1279 w
.ws_ypixel
= xw
.th
;
1280 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1281 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1285 tattrset(int attr
) {
1288 for(i
= 0; i
< term
.row
-1; i
++) {
1289 for(j
= 0; j
< term
.col
-1; j
++) {
1290 if(term
.line
[i
][j
].mode
& attr
)
1299 tsetdirt(int top
, int bot
) {
1302 LIMIT(top
, 0, term
.row
-1);
1303 LIMIT(bot
, 0, term
.row
-1);
1305 for(i
= top
; i
<= bot
; i
++)
1310 tsetdirtattr(int attr
) {
1313 for(i
= 0; i
< term
.row
-1; i
++) {
1314 for(j
= 0; j
< term
.col
-1; j
++) {
1315 if(term
.line
[i
][j
].mode
& attr
) {
1325 tsetdirt(0, term
.row
-1);
1332 if(mode
== CURSOR_SAVE
) {
1334 } else if(mode
== CURSOR_LOAD
) {
1344 term
.c
= (TCursor
){{
1348 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1350 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1351 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1354 term
.bot
= term
.row
- 1;
1355 term
.mode
= MODE_WRAP
;
1357 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1359 tcursor(CURSOR_SAVE
);
1363 tnew(int col
, int row
) {
1364 memset(&term
, 0, sizeof(Term
));
1373 Line
*tmp
= term
.line
;
1375 term
.line
= term
.alt
;
1377 term
.mode
^= MODE_ALTSCREEN
;
1382 tscrolldown(int orig
, int n
) {
1386 LIMIT(n
, 0, term
.bot
-orig
+1);
1388 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1390 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1391 temp
= term
.line
[i
];
1392 term
.line
[i
] = term
.line
[i
-n
];
1393 term
.line
[i
-n
] = temp
;
1396 term
.dirty
[i
-n
] = 1;
1403 tscrollup(int orig
, int n
) {
1406 LIMIT(n
, 0, term
.bot
-orig
+1);
1408 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1410 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1411 temp
= term
.line
[i
];
1412 term
.line
[i
] = term
.line
[i
+n
];
1413 term
.line
[i
+n
] = temp
;
1416 term
.dirty
[i
+n
] = 1;
1419 selscroll(orig
, -n
);
1423 selscroll(int orig
, int n
) {
1427 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1428 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1432 if(sel
.type
== SEL_RECTANGULAR
) {
1433 if(sel
.ob
.y
< term
.top
)
1434 sel
.ob
.y
= term
.top
;
1435 if(sel
.oe
.y
> term
.bot
)
1436 sel
.oe
.y
= term
.bot
;
1438 if(sel
.ob
.y
< term
.top
) {
1439 sel
.ob
.y
= term
.top
;
1442 if(sel
.oe
.y
> term
.bot
) {
1443 sel
.oe
.y
= term
.bot
;
1444 sel
.oe
.x
= term
.col
;
1452 tnewline(int first_col
) {
1456 tscrollup(term
.top
, 1);
1460 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1465 char *p
= csiescseq
.buf
, *np
;
1474 csiescseq
.buf
[csiescseq
.len
] = '\0';
1475 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1477 v
= strtol(p
, &np
, 10);
1480 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1482 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1484 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1488 csiescseq
.mode
= *p
;
1491 /* for absolute user moves, when decom is set */
1493 tmoveato(int x
, int y
) {
1494 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1498 tmoveto(int x
, int y
) {
1501 if(term
.c
.state
& CURSOR_ORIGIN
) {
1506 maxy
= term
.row
- 1;
1508 LIMIT(x
, 0, term
.col
-1);
1509 LIMIT(y
, miny
, maxy
);
1510 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1516 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1517 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1518 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1519 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1520 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1521 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1522 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1523 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1524 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1525 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1529 * The table is proudly stolen from rxvt.
1531 if(attr
->mode
& ATTR_GFX
) {
1532 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1533 && vt100_0
[c
[0] - 0x41]) {
1534 c
= vt100_0
[c
[0] - 0x41];
1539 term
.line
[y
][x
] = *attr
;
1540 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1544 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1548 temp
= x1
, x1
= x2
, x2
= temp
;
1550 temp
= y1
, y1
= y2
, y2
= temp
;
1552 LIMIT(x1
, 0, term
.col
-1);
1553 LIMIT(x2
, 0, term
.col
-1);
1554 LIMIT(y1
, 0, term
.row
-1);
1555 LIMIT(y2
, 0, term
.row
-1);
1557 for(y
= y1
; y
<= y2
; y
++) {
1559 for(x
= x1
; x
<= x2
; x
++) {
1562 term
.line
[y
][x
] = term
.c
.attr
;
1563 memcpy(term
.line
[y
][x
].c
, " ", 2);
1569 tdeletechar(int n
) {
1570 int src
= term
.c
.x
+ n
;
1572 int size
= term
.col
- src
;
1574 term
.dirty
[term
.c
.y
] = 1;
1576 if(src
>= term
.col
) {
1577 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1581 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1582 size
* sizeof(Glyph
));
1583 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1587 tinsertblank(int n
) {
1590 int size
= term
.col
- dst
;
1592 term
.dirty
[term
.c
.y
] = 1;
1594 if(dst
>= term
.col
) {
1595 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1599 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1600 size
* sizeof(Glyph
));
1601 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1605 tinsertblankline(int n
) {
1606 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1609 tscrolldown(term
.c
.y
, n
);
1613 tdeleteline(int n
) {
1614 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1617 tscrollup(term
.c
.y
, n
);
1621 tsetattr(int *attr
, int l
) {
1624 for(i
= 0; i
< l
; i
++) {
1627 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1628 | ATTR_BOLD
| ATTR_ITALIC \
1630 term
.c
.attr
.fg
= defaultfg
;
1631 term
.c
.attr
.bg
= defaultbg
;
1634 term
.c
.attr
.mode
|= ATTR_BOLD
;
1637 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1640 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1642 case 5: /* slow blink */
1643 case 6: /* rapid blink */
1644 term
.c
.attr
.mode
|= ATTR_BLINK
;
1647 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1651 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1654 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1657 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1661 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1664 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1667 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1669 if(BETWEEN(attr
[i
], 0, 255)) {
1670 term
.c
.attr
.fg
= attr
[i
];
1673 "erresc: bad fgcolor %d\n",
1678 "erresc(38): gfx attr %d unknown\n",
1683 term
.c
.attr
.fg
= defaultfg
;
1686 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1688 if(BETWEEN(attr
[i
], 0, 255)) {
1689 term
.c
.attr
.bg
= attr
[i
];
1692 "erresc: bad bgcolor %d\n",
1697 "erresc(48): gfx attr %d unknown\n",
1702 term
.c
.attr
.bg
= defaultbg
;
1705 if(BETWEEN(attr
[i
], 30, 37)) {
1706 term
.c
.attr
.fg
= attr
[i
] - 30;
1707 } else if(BETWEEN(attr
[i
], 40, 47)) {
1708 term
.c
.attr
.bg
= attr
[i
] - 40;
1709 } else if(BETWEEN(attr
[i
], 90, 97)) {
1710 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1711 } else if(BETWEEN(attr
[i
], 100, 107)) {
1712 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1715 "erresc(default): gfx attr %d unknown\n",
1716 attr
[i
]), csidump();
1724 tsetscroll(int t
, int b
) {
1727 LIMIT(t
, 0, term
.row
-1);
1728 LIMIT(b
, 0, term
.row
-1);
1738 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1741 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1745 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1749 case 1: /* DECCKM -- Cursor key */
1750 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1752 case 5: /* DECSCNM -- Reverse video */
1754 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1755 if(mode
!= term
.mode
)
1756 redraw(REDRAW_TIMEOUT
);
1758 case 6: /* DECOM -- Origin */
1759 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1762 case 7: /* DECAWM -- Auto wrap */
1763 MODBIT(term
.mode
, set
, MODE_WRAP
);
1765 case 0: /* Error (IGNORED) */
1766 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1767 case 3: /* DECCOLM -- Column (IGNORED) */
1768 case 4: /* DECSCLM -- Scroll (IGNORED) */
1769 case 8: /* DECARM -- Auto repeat (IGNORED) */
1770 case 18: /* DECPFF -- Printer feed (IGNORED) */
1771 case 19: /* DECPEX -- Printer extent (IGNORED) */
1772 case 42: /* DECNRCM -- National characters (IGNORED) */
1773 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1775 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1776 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1778 case 1000: /* 1000,1002: enable xterm mouse report */
1779 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1780 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1783 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1784 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1786 case 1003: /* 1003: enable all mouse reports */
1787 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1788 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1791 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1794 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1797 MODBIT(term
.mode
, set
, MODE_8BIT
);
1799 case 1049: /* = 1047 and 1048 */
1802 if (!allowaltscreen
)
1805 alt
= IS_SET(MODE_ALTSCREEN
);
1807 tclearregion(0, 0, term
.col
-1,
1810 if(set
^ alt
) /* set is always 1 or 0 */
1816 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1818 /* Not implemented mouse modes. See comments there. */
1819 case 9: /* X10 compatibility mode */
1820 case 1001: /* mouse highlight mode; can hang the
1821 terminal by design when implemented. */
1822 case 1005: /* UTF-8 mouse mode; will confuse
1823 applications not supporting UTF-8
1825 case 1015: /* urxvt mangled mouse mode; incompatible
1826 and can be mistaken for other control
1830 "erresc: unknown private set/reset mode %d\n",
1836 case 0: /* Error (IGNORED) */
1838 case 2: /* KAM -- keyboard action */
1839 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1841 case 4: /* IRM -- Insertion-replacement */
1842 MODBIT(term
.mode
, set
, MODE_INSERT
);
1844 case 12: /* SRM -- Send/Receive */
1845 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1847 case 20: /* LNM -- Linefeed/new line */
1848 MODBIT(term
.mode
, set
, MODE_CRLF
);
1852 "erresc: unknown set/reset mode %d\n",
1864 switch(csiescseq
.mode
) {
1867 fprintf(stderr
, "erresc: unknown csi ");
1871 case '@': /* ICH -- Insert <n> blank char */
1872 DEFAULT(csiescseq
.arg
[0], 1);
1873 tinsertblank(csiescseq
.arg
[0]);
1875 case 'A': /* CUU -- Cursor <n> Up */
1876 DEFAULT(csiescseq
.arg
[0], 1);
1877 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1879 case 'B': /* CUD -- Cursor <n> Down */
1880 case 'e': /* VPR --Cursor <n> Down */
1881 DEFAULT(csiescseq
.arg
[0], 1);
1882 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1884 case 'c': /* DA -- Device Attributes */
1885 if(csiescseq
.arg
[0] == 0)
1886 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1888 case 'C': /* CUF -- Cursor <n> Forward */
1889 case 'a': /* HPR -- Cursor <n> Forward */
1890 DEFAULT(csiescseq
.arg
[0], 1);
1891 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1893 case 'D': /* CUB -- Cursor <n> Backward */
1894 DEFAULT(csiescseq
.arg
[0], 1);
1895 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1897 case 'E': /* CNL -- Cursor <n> Down and first col */
1898 DEFAULT(csiescseq
.arg
[0], 1);
1899 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1901 case 'F': /* CPL -- Cursor <n> Up and first col */
1902 DEFAULT(csiescseq
.arg
[0], 1);
1903 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1905 case 'g': /* TBC -- Tabulation clear */
1906 switch(csiescseq
.arg
[0]) {
1907 case 0: /* clear current tab stop */
1908 term
.tabs
[term
.c
.x
] = 0;
1910 case 3: /* clear all the tabs */
1911 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1917 case 'G': /* CHA -- Move to <col> */
1919 DEFAULT(csiescseq
.arg
[0], 1);
1920 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1922 case 'H': /* CUP -- Move to <row> <col> */
1924 DEFAULT(csiescseq
.arg
[0], 1);
1925 DEFAULT(csiescseq
.arg
[1], 1);
1926 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1928 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1929 DEFAULT(csiescseq
.arg
[0], 1);
1930 while(csiescseq
.arg
[0]--)
1933 case 'J': /* ED -- Clear screen */
1935 switch(csiescseq
.arg
[0]) {
1937 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1938 if(term
.c
.y
< term
.row
-1) {
1939 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1945 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1946 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1949 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1955 case 'K': /* EL -- Clear line */
1956 switch(csiescseq
.arg
[0]) {
1958 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1962 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1965 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1969 case 'S': /* SU -- Scroll <n> line up */
1970 DEFAULT(csiescseq
.arg
[0], 1);
1971 tscrollup(term
.top
, csiescseq
.arg
[0]);
1973 case 'T': /* SD -- Scroll <n> line down */
1974 DEFAULT(csiescseq
.arg
[0], 1);
1975 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1977 case 'L': /* IL -- Insert <n> blank lines */
1978 DEFAULT(csiescseq
.arg
[0], 1);
1979 tinsertblankline(csiescseq
.arg
[0]);
1981 case 'l': /* RM -- Reset Mode */
1982 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1984 case 'M': /* DL -- Delete <n> lines */
1985 DEFAULT(csiescseq
.arg
[0], 1);
1986 tdeleteline(csiescseq
.arg
[0]);
1988 case 'X': /* ECH -- Erase <n> char */
1989 DEFAULT(csiescseq
.arg
[0], 1);
1990 tclearregion(term
.c
.x
, term
.c
.y
,
1991 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1993 case 'P': /* DCH -- Delete <n> char */
1994 DEFAULT(csiescseq
.arg
[0], 1);
1995 tdeletechar(csiescseq
.arg
[0]);
1997 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1998 DEFAULT(csiescseq
.arg
[0], 1);
1999 while(csiescseq
.arg
[0]--)
2002 case 'd': /* VPA -- Move to <row> */
2003 DEFAULT(csiescseq
.arg
[0], 1);
2004 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2006 case 'h': /* SM -- Set terminal mode */
2007 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2009 case 'm': /* SGR -- Terminal attribute (color) */
2010 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2012 case 'r': /* DECSTBM -- Set Scrolling Region */
2013 if(csiescseq
.priv
) {
2016 DEFAULT(csiescseq
.arg
[0], 1);
2017 DEFAULT(csiescseq
.arg
[1], term
.row
);
2018 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2022 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2023 tcursor(CURSOR_SAVE
);
2025 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2026 tcursor(CURSOR_LOAD
);
2037 for(i
= 0; i
< csiescseq
.len
; i
++) {
2038 c
= csiescseq
.buf
[i
] & 0xff;
2041 } else if(c
== '\n') {
2043 } else if(c
== '\r') {
2045 } else if(c
== 0x1b) {
2048 printf("(%02x)", c
);
2056 memset(&csiescseq
, 0, sizeof(csiescseq
));
2065 narg
= strescseq
.narg
;
2067 switch(strescseq
.type
) {
2068 case ']': /* OSC -- Operating System Command */
2069 switch(i
= atoi(strescseq
.args
[0])) {
2074 xsettitle(strescseq
.args
[1]);
2076 case 4: /* color set */
2079 p
= strescseq
.args
[2];
2081 case 104: /* color reset, here p = NULL */
2082 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2083 if (!xsetcolorname(j
, p
)) {
2084 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2087 * TODO if defaultbg color is changed, borders
2094 fprintf(stderr
, "erresc: unknown str ");
2099 case 'k': /* old title set compatibility */
2100 xsettitle(strescseq
.args
[0]);
2102 case 'P': /* DSC -- Device Control String */
2103 case '_': /* APC -- Application Program Command */
2104 case '^': /* PM -- Privacy Message */
2106 fprintf(stderr
, "erresc: unknown str ");
2115 char *p
= strescseq
.buf
;
2118 strescseq
.buf
[strescseq
.len
] = '\0';
2119 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2120 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2128 printf("ESC%c", strescseq
.type
);
2129 for(i
= 0; i
< strescseq
.len
; i
++) {
2130 c
= strescseq
.buf
[i
] & 0xff;
2133 } else if(isprint(c
)) {
2135 } else if(c
== '\n') {
2137 } else if(c
== '\r') {
2139 } else if(c
== 0x1b) {
2142 printf("(%02x)", c
);
2150 memset(&strescseq
, 0, sizeof(strescseq
));
2154 tputtab(bool forward
) {
2160 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2165 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2168 tmoveto(x
, term
.c
.y
);
2172 techo(char *buf
, int len
) {
2173 for(; len
> 0; buf
++, len
--) {
2176 if(c
== '\033') { /* escape */
2179 } else if(c
< '\x20') { /* control code */
2180 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2194 tputc(char *c
, int len
) {
2196 bool control
= ascii
< '\x20' || ascii
== 0177;
2199 if(xwrite(iofd
, c
, len
) < 0) {
2200 fprintf(stderr
, "Error writing in %s:%s\n",
2201 opt_io
, strerror(errno
));
2208 * STR sequences must be checked before anything else
2209 * because it can use some control codes as part of the sequence.
2211 if(term
.esc
& ESC_STR
) {
2214 term
.esc
= ESC_START
| ESC_STR_END
;
2216 case '\a': /* backwards compatibility to xterm */
2221 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2222 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2223 strescseq
.len
+= len
;
2226 * Here is a bug in terminals. If the user never sends
2227 * some code to stop the str or esc command, then st
2228 * will stop responding. But this is better than
2229 * silently failing with unknown characters. At least
2230 * then users will report back.
2232 * In the case users ever get fixed, here is the code:
2244 * Actions of control codes must be performed as soon they arrive
2245 * because they can be embedded inside a control sequence, and
2246 * they must not cause conflicts with sequences.
2254 tmoveto(term
.c
.x
-1, term
.c
.y
);
2257 tmoveto(0, term
.c
.y
);
2262 /* go to first col if the mode is set */
2263 tnewline(IS_SET(MODE_CRLF
));
2265 case '\a': /* BEL */
2266 if(!(xw
.state
& WIN_FOCUSED
))
2269 case '\033': /* ESC */
2271 term
.esc
= ESC_START
;
2273 case '\016': /* SO */
2274 case '\017': /* SI */
2276 * Different charsets are hard to handle. Applications
2277 * should use the right alt charset escapes for the
2278 * only reason they still exist: line drawing. The
2279 * rest is incompatible history st should not support.
2282 case '\032': /* SUB */
2283 case '\030': /* CAN */
2286 case '\005': /* ENQ (IGNORED) */
2287 case '\000': /* NUL (IGNORED) */
2288 case '\021': /* XON (IGNORED) */
2289 case '\023': /* XOFF (IGNORED) */
2290 case 0177: /* DEL (IGNORED) */
2293 } else if(term
.esc
& ESC_START
) {
2294 if(term
.esc
& ESC_CSI
) {
2295 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2296 if(BETWEEN(ascii
, 0x40, 0x7E)
2297 || csiescseq
.len
>= \
2298 sizeof(csiescseq
.buf
)-1) {
2303 } else if(term
.esc
& ESC_STR_END
) {
2307 } else if(term
.esc
& ESC_ALTCHARSET
) {
2309 case '0': /* Line drawing set */
2310 term
.c
.attr
.mode
|= ATTR_GFX
;
2312 case 'B': /* USASCII */
2313 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2315 case 'A': /* UK (IGNORED) */
2316 case '<': /* multinational charset (IGNORED) */
2317 case '5': /* Finnish (IGNORED) */
2318 case 'C': /* Finnish (IGNORED) */
2319 case 'K': /* German (IGNORED) */
2322 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2325 } else if(term
.esc
& ESC_TEST
) {
2326 if(ascii
== '8') { /* DEC screen alignment test. */
2327 char E
[UTF_SIZ
] = "E";
2330 for(x
= 0; x
< term
.col
; ++x
) {
2331 for(y
= 0; y
< term
.row
; ++y
)
2332 tsetchar(E
, &term
.c
.attr
, x
, y
);
2339 term
.esc
|= ESC_CSI
;
2342 term
.esc
|= ESC_TEST
;
2344 case 'P': /* DCS -- Device Control String */
2345 case '_': /* APC -- Application Program Command */
2346 case '^': /* PM -- Privacy Message */
2347 case ']': /* OSC -- Operating System Command */
2348 case 'k': /* old title set compatibility */
2350 strescseq
.type
= ascii
;
2351 term
.esc
|= ESC_STR
;
2353 case '(': /* set primary charset G0 */
2354 term
.esc
|= ESC_ALTCHARSET
;
2356 case ')': /* set secondary charset G1 (IGNORED) */
2357 case '*': /* set tertiary charset G2 (IGNORED) */
2358 case '+': /* set quaternary charset G3 (IGNORED) */
2361 case 'D': /* IND -- Linefeed */
2362 if(term
.c
.y
== term
.bot
) {
2363 tscrollup(term
.top
, 1);
2365 tmoveto(term
.c
.x
, term
.c
.y
+1);
2369 case 'E': /* NEL -- Next line */
2370 tnewline(1); /* always go to first col */
2373 case 'H': /* HTS -- Horizontal tab stop */
2374 term
.tabs
[term
.c
.x
] = 1;
2377 case 'M': /* RI -- Reverse index */
2378 if(term
.c
.y
== term
.top
) {
2379 tscrolldown(term
.top
, 1);
2381 tmoveto(term
.c
.x
, term
.c
.y
-1);
2385 case 'Z': /* DECID -- Identify Terminal */
2386 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2389 case 'c': /* RIS -- Reset to inital state */
2394 case '=': /* DECPAM -- Application keypad */
2395 term
.mode
|= MODE_APPKEYPAD
;
2398 case '>': /* DECPNM -- Normal keypad */
2399 term
.mode
&= ~MODE_APPKEYPAD
;
2402 case '7': /* DECSC -- Save Cursor */
2403 tcursor(CURSOR_SAVE
);
2406 case '8': /* DECRC -- Restore Cursor */
2407 tcursor(CURSOR_LOAD
);
2410 case '\\': /* ST -- Stop */
2414 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2415 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2420 * All characters which form part of a sequence are not
2426 * Display control codes only if we are in graphic mode
2428 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2430 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2432 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2433 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2437 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2438 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2439 &term
.line
[term
.c
.y
][term
.c
.x
],
2440 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2443 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2444 if(term
.c
.x
+1 < term
.col
) {
2445 tmoveto(term
.c
.x
+1, term
.c
.y
);
2447 term
.c
.state
|= CURSOR_WRAPNEXT
;
2452 tresize(int col
, int row
) {
2454 int minrow
= MIN(row
, term
.row
);
2455 int mincol
= MIN(col
, term
.col
);
2456 int slide
= term
.c
.y
- row
+ 1;
2460 if(col
< 1 || row
< 1)
2463 /* free unneeded rows */
2467 * slide screen to keep cursor where we expect it -
2468 * tscrollup would work here, but we can optimize to
2469 * memmove because we're freeing the earlier lines
2471 for(/* i = 0 */; i
< slide
; i
++) {
2475 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2476 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2478 for(i
+= row
; i
< term
.row
; i
++) {
2483 /* resize to new height */
2484 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2485 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2486 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2487 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2489 /* resize each row to new width, zero-pad if needed */
2490 for(i
= 0; i
< minrow
; i
++) {
2492 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2493 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2496 /* allocate any new rows */
2497 for(/* i == minrow */; i
< row
; i
++) {
2499 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2500 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2502 if(col
> term
.col
) {
2503 bp
= term
.tabs
+ term
.col
;
2505 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2506 while(--bp
> term
.tabs
&& !*bp
)
2508 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2511 /* update terminal size */
2514 /* reset scrolling region */
2515 tsetscroll(0, row
-1);
2516 /* make use of the LIMIT in tmoveto */
2517 tmoveto(term
.c
.x
, term
.c
.y
);
2518 /* Clearing both screens */
2521 if(mincol
< col
&& 0 < minrow
) {
2522 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2524 if(0 < col
&& minrow
< row
) {
2525 tclearregion(0, minrow
, col
- 1, row
- 1);
2528 } while(orig
!= term
.line
);
2534 xresize(int col
, int row
) {
2535 xw
.tw
= MAX(1, col
* xw
.cw
);
2536 xw
.th
= MAX(1, row
* xw
.ch
);
2538 XFreePixmap(xw
.dpy
, xw
.buf
);
2539 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2540 DefaultDepth(xw
.dpy
, xw
.scr
));
2541 XftDrawChange(xw
.draw
, xw
.buf
);
2542 xclear(0, 0, xw
.w
, xw
.h
);
2545 static inline ushort
2546 sixd_to_16bit(int x
) {
2547 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2553 XRenderColor color
= { .alpha
= 0xffff };
2555 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2556 for(i
= 0; i
< LEN(colorname
); i
++) {
2559 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2560 die("Could not allocate color '%s'\n", colorname
[i
]);
2564 /* load colors [16-255] ; same colors as xterm */
2565 for(i
= 16, r
= 0; r
< 6; r
++) {
2566 for(g
= 0; g
< 6; g
++) {
2567 for(b
= 0; b
< 6; b
++) {
2568 color
.red
= sixd_to_16bit(r
);
2569 color
.green
= sixd_to_16bit(g
);
2570 color
.blue
= sixd_to_16bit(b
);
2571 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2572 die("Could not allocate color %d\n", i
);
2579 for(r
= 0; r
< 24; r
++, i
++) {
2580 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2581 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2583 die("Could not allocate color %d\n", i
);
2589 xsetcolorname(int x
, const char *name
) {
2590 XRenderColor color
= { .alpha
= 0xffff };
2592 if (x
< 0 || x
> LEN(colorname
))
2595 if(16 <= x
&& x
< 16 + 216) {
2596 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2597 color
.red
= sixd_to_16bit(r
);
2598 color
.green
= sixd_to_16bit(g
);
2599 color
.blue
= sixd_to_16bit(b
);
2600 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2601 return 0; /* something went wrong */
2604 } else if (16 + 216 <= x
&& x
< 256) {
2605 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2606 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2607 return 0; /* something went wrong */
2611 name
= colorname
[x
];
2614 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2621 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2622 XftDrawRect(xw
.draw
,
2623 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2624 borderpx
+ col1
* xw
.cw
,
2625 borderpx
+ row1
* xw
.ch
,
2626 (col2
-col1
+1) * xw
.cw
,
2627 (row2
-row1
+1) * xw
.ch
);
2631 * Absolute coordinates.
2634 xclear(int x1
, int y1
, int x2
, int y2
) {
2635 XftDrawRect(xw
.draw
,
2636 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2637 x1
, y1
, x2
-x1
, y2
-y1
);
2642 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2643 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2644 XSizeHints
*sizeh
= NULL
;
2646 sizeh
= XAllocSizeHints();
2647 if(xw
.isfixed
== False
) {
2648 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2649 sizeh
->height
= xw
.h
;
2650 sizeh
->width
= xw
.w
;
2651 sizeh
->height_inc
= xw
.ch
;
2652 sizeh
->width_inc
= xw
.cw
;
2653 sizeh
->base_height
= 2 * borderpx
;
2654 sizeh
->base_width
= 2 * borderpx
;
2656 sizeh
->flags
= PMaxSize
| PMinSize
;
2657 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2658 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2661 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2666 xloadfont(Font
*f
, FcPattern
*pattern
) {
2670 match
= FcFontMatch(NULL
, pattern
, &result
);
2674 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2675 FcPatternDestroy(match
);
2680 f
->pattern
= FcPatternDuplicate(pattern
);
2682 f
->ascent
= f
->match
->ascent
;
2683 f
->descent
= f
->match
->descent
;
2685 f
->rbearing
= f
->match
->max_advance_width
;
2687 f
->height
= f
->ascent
+ f
->descent
;
2688 f
->width
= f
->lbearing
+ f
->rbearing
;
2694 xloadfonts(char *fontstr
, int fontsize
) {
2699 if(fontstr
[0] == '-') {
2700 pattern
= XftXlfdParse(fontstr
, False
, False
);
2702 pattern
= FcNameParse((FcChar8
*)fontstr
);
2706 die("st: can't open font %s\n", fontstr
);
2709 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2710 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2711 usedfontsize
= fontsize
;
2713 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2714 if(result
== FcResultMatch
) {
2715 usedfontsize
= (int)fontval
;
2718 * Default font size is 12, if none given. This is to
2719 * have a known usedfontsize value.
2721 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2726 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2727 FcDefaultSubstitute(pattern
);
2729 if(xloadfont(&dc
.font
, pattern
))
2730 die("st: can't open font %s\n", fontstr
);
2732 /* Setting character width and height. */
2733 xw
.cw
= dc
.font
.width
;
2734 xw
.ch
= dc
.font
.height
;
2736 FcPatternDel(pattern
, FC_SLANT
);
2737 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2738 if(xloadfont(&dc
.ifont
, pattern
))
2739 die("st: can't open font %s\n", fontstr
);
2741 FcPatternDel(pattern
, FC_WEIGHT
);
2742 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2743 if(xloadfont(&dc
.ibfont
, pattern
))
2744 die("st: can't open font %s\n", fontstr
);
2746 FcPatternDel(pattern
, FC_SLANT
);
2747 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2748 if(xloadfont(&dc
.bfont
, pattern
))
2749 die("st: can't open font %s\n", fontstr
);
2751 FcPatternDestroy(pattern
);
2755 xloadfontset(Font
*f
) {
2758 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2764 xunloadfont(Font
*f
) {
2765 XftFontClose(xw
.dpy
, f
->match
);
2766 FcPatternDestroy(f
->pattern
);
2768 FcFontSetDestroy(f
->set
);
2772 xunloadfonts(void) {
2776 * Free the loaded fonts in the font cache. This is done backwards
2779 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2782 XftFontClose(xw
.dpy
, frc
[ip
].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
);
2803 XSetWindowAttributes attrs
;
2809 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2810 die("Can't open display\n");
2811 xw
.scr
= XDefaultScreen(xw
.dpy
);
2812 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2816 die("Could not init fontconfig.\n");
2818 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2819 xloadfonts(usedfont
, 0);
2822 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2825 /* adjust fixed window geometry */
2827 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2828 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2830 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2832 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2837 /* window - default size */
2838 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2839 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2845 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2846 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2847 attrs
.bit_gravity
= NorthWestGravity
;
2848 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2849 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2850 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2851 attrs
.colormap
= xw
.cmap
;
2853 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2854 XRootWindow(xw
.dpy
, xw
.scr
);
2855 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2856 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2858 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2862 memset(&gcvalues
, 0, sizeof(gcvalues
));
2863 gcvalues
.graphics_exposures
= False
;
2864 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2866 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2867 DefaultDepth(xw
.dpy
, xw
.scr
));
2868 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2869 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2871 /* Xft rendering context */
2872 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2875 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2876 XSetLocaleModifiers("@im=local");
2877 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2878 XSetLocaleModifiers("@im=");
2879 if((xw
.xim
= XOpenIM(xw
.dpy
,
2880 NULL
, NULL
, NULL
)) == NULL
) {
2881 die("XOpenIM failed. Could not open input"
2886 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2887 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2888 XNFocusWindow
, xw
.win
, NULL
);
2890 die("XCreateIC failed. Could not obtain input method.\n");
2892 /* white cursor, black outline */
2893 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2894 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2895 XRecolorCursor(xw
.dpy
, cursor
,
2896 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2897 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2899 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2900 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2901 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2904 XMapWindow(xw
.dpy
, xw
.win
);
2910 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2911 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2912 width
= charlen
* xw
.cw
, xp
, i
;
2914 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2917 Font
*font
= &dc
.font
;
2919 FcPattern
*fcpattern
, *fontpattern
;
2920 FcFontSet
*fcsets
[] = { NULL
};
2921 FcCharSet
*fccharset
;
2922 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2923 XRenderColor colfg
, colbg
;
2926 frcflags
= FRC_NORMAL
;
2928 if(base
.mode
& ATTR_ITALIC
) {
2929 if(base
.fg
== defaultfg
)
2930 base
.fg
= defaultitalic
;
2932 frcflags
= FRC_ITALIC
;
2933 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2934 if(base
.fg
== defaultfg
)
2935 base
.fg
= defaultitalic
;
2937 frcflags
= FRC_ITALICBOLD
;
2938 } else if(base
.mode
& ATTR_UNDERLINE
) {
2939 if(base
.fg
== defaultfg
)
2940 base
.fg
= defaultunderline
;
2942 fg
= &dc
.col
[base
.fg
];
2943 bg
= &dc
.col
[base
.bg
];
2945 if(base
.mode
& ATTR_BOLD
) {
2946 if(BETWEEN(base
.fg
, 0, 7)) {
2947 /* basic system colors */
2948 fg
= &dc
.col
[base
.fg
+ 8];
2949 } else if(BETWEEN(base
.fg
, 16, 195)) {
2951 fg
= &dc
.col
[base
.fg
+ 36];
2952 } else if(BETWEEN(base
.fg
, 232, 251)) {
2954 fg
= &dc
.col
[base
.fg
+ 4];
2957 * Those ranges will not be brightened:
2958 * 8 - 15 – bright system colors
2959 * 196 - 231 – highest 256 color cube
2960 * 252 - 255 – brightest colors in greyscale
2963 frcflags
= FRC_BOLD
;
2966 if(IS_SET(MODE_REVERSE
)) {
2967 if(fg
== &dc
.col
[defaultfg
]) {
2968 fg
= &dc
.col
[defaultbg
];
2970 colfg
.red
= ~fg
->color
.red
;
2971 colfg
.green
= ~fg
->color
.green
;
2972 colfg
.blue
= ~fg
->color
.blue
;
2973 colfg
.alpha
= fg
->color
.alpha
;
2974 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2978 if(bg
== &dc
.col
[defaultbg
]) {
2979 bg
= &dc
.col
[defaultfg
];
2981 colbg
.red
= ~bg
->color
.red
;
2982 colbg
.green
= ~bg
->color
.green
;
2983 colbg
.blue
= ~bg
->color
.blue
;
2984 colbg
.alpha
= bg
->color
.alpha
;
2985 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2990 if(base
.mode
& ATTR_REVERSE
) {
2996 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
2999 /* Intelligent cleaning up of the borders. */
3001 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3002 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3004 if(x
+ charlen
>= term
.col
) {
3005 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3006 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3009 xclear(winx
, 0, winx
+ width
, borderpx
);
3011 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3013 /* Clean up the region we want to draw to. */
3014 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3016 /* Set the clip region because Xft is sometimes dirty. */
3021 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3023 for(xp
= winx
; bytelen
> 0;) {
3025 * Search for the range in the to be printed string of glyphs
3026 * that are in the main font. Then print that range. If
3027 * some glyph is found that is not in the font, do the
3035 u8cblen
= utf8decode(s
, &u8char
);
3039 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
3040 if(!doesexist
|| bytelen
<= 0) {
3049 XftDrawStringUtf8(xw
.draw
, fg
,
3051 winy
+ font
->ascent
,
3054 xp
+= font
->width
* u8fl
;
3067 /* Search the font cache. */
3068 for(i
= 0; i
< frclen
; i
++, frp
--) {
3072 if(frc
[frp
].c
== u8char
3073 && frc
[frp
].flags
== frcflags
) {
3078 /* Nothing was found. */
3082 fcsets
[0] = font
->set
;
3085 * Nothing was found in the cache. Now use
3086 * some dozen of Fontconfig calls to get the
3087 * font for one single character.
3089 fcpattern
= FcPatternDuplicate(font
->pattern
);
3090 fccharset
= FcCharSetCreate();
3092 FcCharSetAddChar(fccharset
, u8char
);
3093 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3095 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3098 FcConfigSubstitute(0, fcpattern
,
3100 FcDefaultSubstitute(fcpattern
);
3102 fontpattern
= FcFontSetMatch(0, fcsets
,
3103 FcTrue
, fcpattern
, &fcres
);
3106 * Overwrite or create the new cache entry.
3110 if(frccur
>= LEN(frc
))
3112 if(frclen
> LEN(frc
)) {
3114 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
3117 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
3119 frc
[frccur
].c
= u8char
;
3120 frc
[frccur
].flags
= frcflags
;
3122 FcPatternDestroy(fcpattern
);
3123 FcCharSetDestroy(fccharset
);
3128 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
3129 xp
, winy
+ frc
[frp
].font
->ascent
,
3130 (FcChar8
*)u8c
, u8cblen
);
3136 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3137 winy + font->ascent, (FcChar8 *)s, bytelen);
3140 if(base
.mode
& ATTR_UNDERLINE
) {
3141 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3145 /* Reset clip to none. */
3146 XftDrawSetClip(xw
.draw
, 0);
3151 static int oldx
= 0, oldy
= 0;
3153 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3155 LIMIT(oldx
, 0, term
.col
-1);
3156 LIMIT(oldy
, 0, term
.row
-1);
3158 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3160 /* remove the old cursor */
3161 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3162 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3165 /* draw the new one */
3166 if(!(IS_SET(MODE_HIDE
))) {
3167 if(xw
.state
& WIN_FOCUSED
) {
3168 if(IS_SET(MODE_REVERSE
)) {
3169 g
.mode
|= ATTR_REVERSE
;
3175 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3177 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3178 borderpx
+ term
.c
.x
* xw
.cw
,
3179 borderpx
+ term
.c
.y
* xw
.ch
,
3181 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3182 borderpx
+ term
.c
.x
* xw
.cw
,
3183 borderpx
+ term
.c
.y
* xw
.ch
,
3185 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3186 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3187 borderpx
+ term
.c
.y
* xw
.ch
,
3189 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3190 borderpx
+ term
.c
.x
* xw
.cw
,
3191 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3194 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3200 xsettitle(char *p
) {
3203 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3205 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3210 xsettitle(opt_title
? opt_title
: "st");
3214 redraw(int timeout
) {
3215 struct timespec tv
= {0, timeout
* 1000};
3221 nanosleep(&tv
, NULL
);
3222 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3228 drawregion(0, 0, term
.col
, term
.row
);
3229 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3231 XSetForeground(xw
.dpy
, dc
.gc
,
3232 dc
.col
[IS_SET(MODE_REVERSE
)?
3233 defaultfg
: defaultbg
].pixel
);
3237 drawregion(int x1
, int y1
, int x2
, int y2
) {
3238 int ic
, ib
, x
, y
, ox
, sl
;
3240 char buf
[DRAW_BUF_SIZ
];
3241 bool ena_sel
= sel
.ob
.x
!= -1;
3243 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3246 if(!(xw
.state
& WIN_VISIBLE
))
3249 for(y
= y1
; y
< y2
; y
++) {
3253 xtermclear(0, y
, term
.col
, y
);
3255 base
= term
.line
[y
][0];
3257 for(x
= x1
; x
< x2
; x
++) {
3258 new = term
.line
[y
][x
];
3259 if(ena_sel
&& selected(x
, y
))
3260 new.mode
^= ATTR_REVERSE
;
3261 if(ib
> 0 && (ATTRCMP(base
, new)
3262 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3263 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3271 sl
= utf8size(new.c
);
3272 memcpy(buf
+ib
, new.c
, sl
);
3277 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3283 expose(XEvent
*ev
) {
3284 XExposeEvent
*e
= &ev
->xexpose
;
3286 if(xw
.state
& WIN_REDRAW
) {
3288 xw
.state
&= ~WIN_REDRAW
;
3294 visibility(XEvent
*ev
) {
3295 XVisibilityEvent
*e
= &ev
->xvisibility
;
3297 if(e
->state
== VisibilityFullyObscured
) {
3298 xw
.state
&= ~WIN_VISIBLE
;
3299 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3300 /* need a full redraw for next Expose, not just a buf copy */
3301 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3307 xw
.state
&= ~WIN_VISIBLE
;
3311 xseturgency(int add
) {
3312 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3314 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3315 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3321 XFocusChangeEvent
*e
= &ev
->xfocus
;
3323 if(e
->mode
== NotifyGrab
)
3326 if(ev
->type
== FocusIn
) {
3327 XSetICFocus(xw
.xic
);
3328 xw
.state
|= WIN_FOCUSED
;
3330 if(IS_SET(MODE_FOCUS
))
3331 ttywrite("\033[I", 3);
3333 XUnsetICFocus(xw
.xic
);
3334 xw
.state
&= ~WIN_FOCUSED
;
3335 if(IS_SET(MODE_FOCUS
))
3336 ttywrite("\033[O", 3);
3341 match(uint mask
, uint state
) {
3342 state
&= ~(ignoremod
);
3344 if(mask
== XK_NO_MOD
&& state
)
3346 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3348 if((state
& mask
) != state
)
3354 numlock(const Arg
*dummy
) {
3359 kmap(KeySym k
, uint state
) {
3364 /* Check for mapped keys out of X11 function keys. */
3365 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3366 if(mappedkeys
[i
] == k
)
3369 if(i
== LEN(mappedkeys
)) {
3370 if((k
& 0xFFFF) < 0xFD00)
3374 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3380 if(!match(mask
, state
))
3383 if(kp
->appkey
> 0) {
3384 if(!IS_SET(MODE_APPKEYPAD
))
3386 if(term
.numlock
&& kp
->appkey
== 2)
3388 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3392 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3394 && !IS_SET(MODE_APPCURSOR
))) {
3398 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3399 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3410 kpress(XEvent
*ev
) {
3411 XKeyEvent
*e
= &ev
->xkey
;
3413 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3419 if(IS_SET(MODE_KBDLOCK
))
3422 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3423 e
->state
&= ~Mod2Mask
;
3425 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3426 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3427 bp
->func(&(bp
->arg
));
3432 /* 2. custom keys from config.h */
3433 if((customkey
= kmap(ksym
, e
->state
))) {
3434 len
= strlen(customkey
);
3435 memcpy(buf
, customkey
, len
);
3436 /* 3. hardcoded (overrides X lookup) */
3441 if(len
== 1 && e
->state
& Mod1Mask
) {
3442 if(IS_SET(MODE_8BIT
)) {
3445 ret
= utf8encode(&c
, cp
);
3454 memcpy(cp
, xstr
, len
);
3455 len
= cp
- buf
+ len
;
3459 if(IS_SET(MODE_ECHO
))
3465 cmessage(XEvent
*e
) {
3468 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3470 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3471 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3472 xw
.state
|= WIN_FOCUSED
;
3474 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3475 xw
.state
&= ~WIN_FOCUSED
;
3477 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3478 /* Send SIGHUP to shell */
3485 cresize(int width
, int height
) {
3493 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3494 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3503 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3506 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3513 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3514 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3516 gettimeofday(&lastblink
, NULL
);
3517 gettimeofday(&last
, NULL
);
3519 for(xev
= actionfps
;;) {
3521 FD_SET(cmdfd
, &rfd
);
3524 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3527 die("select failed: %s\n", SERRNO
);
3529 if(FD_ISSET(cmdfd
, &rfd
)) {
3532 blinkset
= tattrset(ATTR_BLINK
);
3533 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3534 term
.mode
&= ~(MODE_BLINK
);
3538 if(FD_ISSET(xfd
, &rfd
))
3541 gettimeofday(&now
, NULL
);
3542 drawtimeout
.tv_sec
= 0;
3543 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3547 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3548 tsetdirtattr(ATTR_BLINK
);
3549 term
.mode
^= MODE_BLINK
;
3550 gettimeofday(&lastblink
, NULL
);
3553 if(TIMEDIFF(now
, last
) \
3554 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3560 while(XPending(xw
.dpy
)) {
3561 XNextEvent(xw
.dpy
, &ev
);
3562 if(XFilterEvent(&ev
, None
))
3564 if(handler
[ev
.type
])
3565 (handler
[ev
.type
])(&ev
);
3571 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3573 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3575 if(TIMEDIFF(now
, lastblink
) \
3577 drawtimeout
.tv_usec
= 1;
3579 drawtimeout
.tv_usec
= (1000 * \
3594 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3595 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3596 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3600 main(int argc
, char *argv
[]) {
3604 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3609 allowaltscreen
= false;
3612 opt_class
= EARGF(usage());
3615 /* eat all remaining arguments */
3620 opt_font
= EARGF(usage());
3623 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3628 if(bitm
& WidthValue
)
3630 if(bitm
& HeightValue
)
3632 if(bitm
& XNegative
&& xw
.fx
== 0)
3634 if(bitm
& XNegative
&& xw
.fy
== 0)
3637 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3641 opt_io
= EARGF(usage());
3644 opt_title
= EARGF(usage());
3647 opt_embed
= EARGF(usage());
3655 setlocale(LC_CTYPE
, "");
3656 XSetLocaleModifiers("");
3662 cresize(xw
.h
, xw
.w
);