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>
38 #define Draw XftDraw *
39 #define Colour XftColor
40 #define Colourmap Colormap
41 #define Rectangle XRectangle
45 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
47 #elif defined(__FreeBSD__) || defined(__DragonFly__)
53 #define XEMBED_FOCUS_IN 4
54 #define XEMBED_FOCUS_OUT 5
58 #define ESC_BUF_SIZ (128*UTF_SIZ)
59 #define ESC_ARG_SIZ 16
60 #define STR_BUF_SIZ ESC_BUF_SIZ
61 #define STR_ARG_SIZ ESC_ARG_SIZ
62 #define DRAW_BUF_SIZ 20*1024
63 #define XK_ANY_MOD UINT_MAX
65 #define XK_SWITCH_MOD (1<<13)
67 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
70 #define SERRNO strerror(errno)
71 #define MIN(a, b) ((a) < (b) ? (a) : (b))
72 #define MAX(a, b) ((a) < (b) ? (b) : (a))
73 #define LEN(a) (sizeof(a) / sizeof(a[0]))
74 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
75 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
76 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
77 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
78 #define IS_SET(flag) ((term.mode & (flag)) != 0)
79 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
80 #define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
82 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
83 #define IS_TRUECOL(x) (1 << 24 & (x))
84 #define TRUERED(x) (((x) & 0xff0000) >> 8)
85 #define TRUEGREEN(x) (((x) & 0xff00))
86 #define TRUEBLUE(x) (((x) & 0xff) << 8)
89 #define VT102ID "\033[?6c"
91 enum glyph_attribute
{
104 enum cursor_movement
{
122 MODE_MOUSEMOTION
= 64,
127 MODE_APPCURSOR
= 2048,
128 MODE_MOUSESGR
= 4096,
133 MODE_MOUSEX10
= 131072,
134 MODE_MOUSEMANY
= 262144,
135 MODE_BRCKTPASTE
= 524288,
136 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
143 ESC_STR
= 4, /* DSC, OSC, PM, APC */
145 ESC_STR_END
= 16, /* a final string was encountered */
146 ESC_TEST
= 32, /* Enter in test mode */
155 enum selection_type
{
160 enum selection_snap
{
165 typedef unsigned char uchar
;
166 typedef unsigned int uint
;
167 typedef unsigned long ulong
;
168 typedef unsigned short ushort
;
171 char c
[UTF_SIZ
]; /* character code */
172 ushort mode
; /* attribute flags */
173 ulong fg
; /* foreground */
174 ulong bg
; /* background */
180 Glyph attr
; /* current char attributes */
186 /* CSI Escape sequence structs */
187 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
189 char buf
[ESC_BUF_SIZ
]; /* raw string */
190 int len
; /* raw string length */
192 int arg
[ESC_ARG_SIZ
];
193 int narg
; /* nb of args */
197 /* STR Escape sequence structs */
198 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
200 char type
; /* ESC type ... */
201 char buf
[STR_BUF_SIZ
]; /* raw string */
202 int len
; /* raw string length */
203 char *args
[STR_ARG_SIZ
];
204 int narg
; /* nb of args */
207 /* Internal representation of the screen */
209 int row
; /* nb row */
210 int col
; /* nb col */
211 Line
*line
; /* screen */
212 Line
*alt
; /* alternate screen */
213 bool *dirty
; /* dirtyness of lines */
214 TCursor c
; /* cursor */
215 int top
; /* top scroll limit */
216 int bot
; /* bottom scroll limit */
217 int mode
; /* terminal mode flags */
218 int esc
; /* escape state flags */
219 bool numlock
; /* lock numbers in keyboard */
223 /* Purely graphic info */
229 Atom xembed
, wmdeletewin
;
234 XSetWindowAttributes attrs
;
236 bool isfixed
; /* is fixed geometry? */
237 int fx
, fy
, fw
, fh
; /* fixed geometry */
238 int tw
, th
; /* tty width and height */
239 int w
, h
; /* window width and height */
240 int ch
; /* char height */
241 int cw
; /* char width */
242 char state
; /* focus, redraw, visible */
255 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
256 signed char appkey
; /* application keypad */
257 signed char appcursor
; /* application cursor */
258 signed char crlf
; /* crlf mode */
266 * Selection variables:
267 * nb – normalized coordinates of the beginning of the selection
268 * ne – normalized coordinates of the end of the selection
269 * ob – original coordinates of the beginning of the selection
270 * oe – original coordinates of the end of the selection
279 struct timeval tclick1
;
280 struct timeval tclick2
;
293 void (*func
)(const Arg
*);
297 /* function definitions used in config.h */
298 static void clippaste(const Arg
*);
299 static void numlock(const Arg
*);
300 static void selpaste(const Arg
*);
301 static void xzoom(const Arg
*);
303 /* Config.h for applying patches and the configuration. */
319 /* Drawing Context */
321 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
322 Font font
, bfont
, ifont
, ibfont
;
326 static void die(const char *, ...);
327 static void draw(void);
328 static void redraw(int);
329 static void drawregion(int, int, int, int);
330 static void execsh(void);
331 static void sigchld(int);
332 static void run(void);
334 static void csidump(void);
335 static void csihandle(void);
336 static void csiparse(void);
337 static void csireset(void);
338 static void strdump(void);
339 static void strhandle(void);
340 static void strparse(void);
341 static void strreset(void);
343 static int tattrset(int);
344 static void tclearregion(int, int, int, int);
345 static void tcursor(int);
346 static void tdeletechar(int);
347 static void tdeleteline(int);
348 static void tinsertblank(int);
349 static void tinsertblankline(int);
350 static void tmoveto(int, int);
351 static void tmoveato(int x
, int y
);
352 static void tnew(int, int);
353 static void tnewline(int);
354 static void tputtab(bool);
355 static void tputc(char *, int);
356 static void treset(void);
357 static int tresize(int, int);
358 static void tscrollup(int, int);
359 static void tscrolldown(int, int);
360 static void tsetattr(int*, int);
361 static void tsetchar(char *, Glyph
*, int, int);
362 static void tsetscroll(int, int);
363 static void tswapscreen(void);
364 static void tsetdirt(int, int);
365 static void tsetdirtattr(int);
366 static void tsetmode(bool, bool, int *, int);
367 static void tfulldirt(void);
368 static void techo(char *, int);
369 static long tdefcolor(int *, int *, int);
370 static inline bool match(uint
, uint
);
371 static void ttynew(void);
372 static void ttyread(void);
373 static void ttyresize(void);
374 static void ttywrite(const char *, size_t);
376 static void xdraws(char *, Glyph
, int, int, int, int);
377 static void xhints(void);
378 static void xclear(int, int, int, int);
379 static void xdrawcursor(void);
380 static void xinit(void);
381 static void xloadcols(void);
382 static int xsetcolorname(int, const char *);
383 static int xloadfont(Font
*, FcPattern
*);
384 static void xloadfonts(char *, int);
385 static int xloadfontset(Font
*);
386 static void xsettitle(char *);
387 static void xresettitle(void);
388 static void xsetpointermotion(int);
389 static void xseturgency(int);
390 static void xsetsel(char*);
391 static void xtermclear(int, int, int, int);
392 static void xunloadfont(Font
*f
);
393 static void xunloadfonts(void);
394 static void xresize(int, int);
396 static void expose(XEvent
*);
397 static void visibility(XEvent
*);
398 static void unmap(XEvent
*);
399 static char *kmap(KeySym
, uint
);
400 static void kpress(XEvent
*);
401 static void cmessage(XEvent
*);
402 static void cresize(int, int);
403 static void resize(XEvent
*);
404 static void focus(XEvent
*);
405 static void brelease(XEvent
*);
406 static void bpress(XEvent
*);
407 static void bmotion(XEvent
*);
408 static void selnotify(XEvent
*);
409 static void selclear(XEvent
*);
410 static void selrequest(XEvent
*);
412 static void selinit(void);
413 static void selsort(void);
414 static inline bool selected(int, int);
415 static void selcopy(void);
416 static void selscroll(int, int);
417 static void selsnap(int, int *, int *, int);
419 static int utf8decode(char *, long *);
420 static int utf8encode(long *, char *);
421 static int utf8size(char *);
422 static int isfullutf8(char *, int);
424 static ssize_t
xwrite(int, char *, size_t);
425 static void *xmalloc(size_t);
426 static void *xrealloc(void *, size_t);
428 static void (*handler
[LASTEvent
])(XEvent
*) = {
430 [ClientMessage
] = cmessage
,
431 [ConfigureNotify
] = resize
,
432 [VisibilityNotify
] = visibility
,
433 [UnmapNotify
] = unmap
,
437 [MotionNotify
] = bmotion
,
438 [ButtonPress
] = bpress
,
439 [ButtonRelease
] = brelease
,
440 [SelectionClear
] = selclear
,
441 [SelectionNotify
] = selnotify
,
442 [SelectionRequest
] = selrequest
,
449 static CSIEscape csiescseq
;
450 static STREscape strescseq
;
453 static Selection sel
;
454 static int iofd
= -1;
455 static char **opt_cmd
= NULL
;
456 static char *opt_io
= NULL
;
457 static char *opt_title
= NULL
;
458 static char *opt_embed
= NULL
;
459 static char *opt_class
= NULL
;
460 static char *opt_font
= NULL
;
461 static int oldbutton
= 3; /* button event on startup: 3 = release */
463 static char *usedfont
= NULL
;
464 static int usedfontsize
= 0;
466 /* Font Ring Cache */
479 /* Fontcache is an array now. A new font will be appended to the array. */
480 static Fontcache frc
[16];
481 static int frclen
= 0;
484 xwrite(int fd
, char *s
, size_t len
) {
488 ssize_t r
= write(fd
, s
, len
);
498 xmalloc(size_t len
) {
499 void *p
= malloc(len
);
502 die("Out of memory\n");
508 xrealloc(void *p
, size_t len
) {
509 if((p
= realloc(p
, len
)) == NULL
)
510 die("Out of memory\n");
516 utf8decode(char *s
, long *u
) {
522 if(~c
& 0x80) { /* 0xxxxxxx */
525 } else if((c
& 0xE0) == 0xC0) { /* 110xxxxx */
528 } else if((c
& 0xF0) == 0xE0) { /* 1110xxxx */
531 } else if((c
& 0xF8) == 0xF0) { /* 11110xxx */
538 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
540 if((c
& 0xC0) != 0x80) /* 10xxxxxx */
546 if((n
== 1 && *u
< 0x80) ||
547 (n
== 2 && *u
< 0x800) ||
548 (n
== 3 && *u
< 0x10000) ||
549 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
561 utf8encode(long *u
, char *s
) {
569 *sp
= uc
; /* 0xxxxxxx */
571 } else if(*u
< 0x800) {
572 *sp
= (uc
>> 6) | 0xC0; /* 110xxxxx */
574 } else if(uc
< 0x10000) {
575 *sp
= (uc
>> 12) | 0xE0; /* 1110xxxx */
577 } else if(uc
<= 0x10FFFF) {
578 *sp
= (uc
>> 18) | 0xF0; /* 11110xxx */
584 for(i
=n
,++sp
; i
>0; --i
,++sp
)
585 *sp
= ((uc
>> 6*(i
-1)) & 0x3F) | 0x80; /* 10xxxxxx */
597 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
598 UTF-8 otherwise return 0 */
600 isfullutf8(char *s
, int b
) {
608 } else if((*c1
& 0xE0) == 0xC0 && b
== 1) {
610 } else if((*c1
& 0xF0) == 0xE0 &&
612 ((b
== 2) && (*c2
& 0xC0) == 0x80))) {
614 } else if((*c1
& 0xF8) == 0xF0 &&
616 ((b
== 2) && (*c2
& 0xC0) == 0x80) ||
617 ((b
== 3) && (*c2
& 0xC0) == 0x80 && (*c3
& 0xC0) == 0x80))) {
630 } else if((c
& 0xE0) == 0xC0) {
632 } else if((c
& 0xF0) == 0xE0) {
641 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
642 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
646 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
647 if(sel
.xtarget
== None
)
648 sel
.xtarget
= XA_STRING
;
656 return LIMIT(x
, 0, term
.col
-1);
664 return LIMIT(y
, 0, term
.row
-1);
669 if(sel
.ob
.y
== sel
.oe
.y
) {
670 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
671 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
673 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
674 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
676 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
677 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
681 selected(int x
, int y
) {
682 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
683 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
685 if(sel
.type
== SEL_RECTANGULAR
) {
686 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
687 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
690 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
691 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
692 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
693 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
697 selsnap(int mode
, int *x
, int *y
, int direction
) {
703 * Snap around if the word wraps around at the end or
704 * beginning of a line.
707 if(direction
< 0 && *x
<= 0) {
708 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
716 if(direction
> 0 && *x
>= term
.col
-1) {
717 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
726 if(term
.line
[*y
][*x
+direction
].mode
& ATTR_WDUMMY
) {
731 if(strchr(worddelimiters
,
732 term
.line
[*y
][*x
+direction
].c
[0])) {
741 * Snap around if the the previous line or the current one
742 * has set ATTR_WRAP at its end. Then the whole next or
743 * previous line will be selected.
745 *x
= (direction
< 0) ? 0 : term
.col
- 1;
746 if(direction
< 0 && *y
> 0) {
747 for(; *y
> 0; *y
+= direction
) {
748 if(!(term
.line
[*y
-1][term
.col
-1].mode
753 } else if(direction
> 0 && *y
< term
.row
-1) {
754 for(; *y
< term
.row
; *y
+= direction
) {
755 if(!(term
.line
[*y
][term
.col
-1].mode
764 * Select the whole line when the end of line is reached.
768 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
778 getbuttoninfo(XEvent
*e
) {
780 uint state
= e
->xbutton
.state
&~Button1Mask
;
782 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
784 sel
.oe
.x
= x2col(e
->xbutton
.x
);
785 sel
.oe
.y
= y2row(e
->xbutton
.y
);
787 if(sel
.ob
.y
< sel
.oe
.y
788 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
789 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
790 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
792 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
793 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
797 sel
.type
= SEL_REGULAR
;
798 for(type
= 1; type
< LEN(selmasks
); ++type
) {
799 if(match(selmasks
[type
], state
)) {
807 mousereport(XEvent
*e
) {
808 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
809 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
815 if(e
->xbutton
.type
== MotionNotify
) {
816 if(x
== ox
&& y
== oy
)
818 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
820 /* MOUSE_MOTION: no reporting if no button is pressed */
821 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
824 button
= oldbutton
+ 32;
828 if(!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
835 if(e
->xbutton
.type
== ButtonPress
) {
839 } else if(e
->xbutton
.type
== ButtonRelease
) {
841 /* MODE_MOUSEX10: no button release reporting */
842 if(IS_SET(MODE_MOUSEX10
))
847 if(!IS_SET(MODE_MOUSEX10
)) {
848 button
+= (state
& ShiftMask
? 4 : 0)
849 + (state
& Mod4Mask
? 8 : 0)
850 + (state
& ControlMask
? 16 : 0);
854 if(IS_SET(MODE_MOUSESGR
)) {
855 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
857 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
858 } else if(x
< 223 && y
< 223) {
859 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
860 32+button
, 32+x
+1, 32+y
+1);
873 if(IS_SET(MODE_MOUSE
)) {
878 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
879 if(e
->xbutton
.button
== mk
->b
880 && match(mk
->mask
, e
->xbutton
.state
)) {
881 ttywrite(mk
->s
, strlen(mk
->s
));
882 if(IS_SET(MODE_ECHO
))
883 techo(mk
->s
, strlen(mk
->s
));
888 if(e
->xbutton
.button
== Button1
) {
889 gettimeofday(&now
, NULL
);
891 /* Clear previous selection, logically and visually. */
894 sel
.type
= SEL_REGULAR
;
895 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
896 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
899 * If the user clicks below predefined timeouts specific
900 * snapping behaviour is exposed.
902 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
903 sel
.snap
= SNAP_LINE
;
904 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
905 sel
.snap
= SNAP_WORD
;
909 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
910 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
914 * Draw selection, unless it's regular and we don't want to
915 * make clicks visible
919 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
921 sel
.tclick2
= sel
.tclick1
;
929 int x
, y
, bufsize
, size
, i
, ex
;
935 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
936 ptr
= str
= xmalloc(bufsize
);
938 /* append every set & selected glyph to the selection */
939 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
940 gp
= &term
.line
[y
][0];
941 last
= gp
+ term
.col
;
943 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
944 strcmp(last
->c
, " ") != 0))
947 for(x
= 0; gp
<= last
; x
++, ++gp
) {
948 if(!selected(x
, y
) || (gp
->mode
& ATTR_WDUMMY
))
951 size
= utf8size(gp
->c
);
952 memcpy(ptr
, gp
->c
, size
);
957 * Copy and pasting of line endings is inconsistent
958 * in the inconsistent terminal and GUI world.
959 * The best solution seems like to produce '\n' when
960 * something is copied from st and convert '\n' to
961 * '\r', when something to be pasted is received by
963 * FIXME: Fix the computer world.
965 if(y
< sel
.ne
.y
&& x
> 0 && !((gp
-1)->mode
& ATTR_WRAP
))
969 * If the last selected line expands in the selection
970 * after the visible text '\n' is appended.
974 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
977 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
989 selnotify(XEvent
*e
) {
990 ulong nitems
, ofs
, rem
;
992 uchar
*data
, *last
, *repl
;
997 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
998 False
, AnyPropertyType
, &type
, &format
,
999 &nitems
, &rem
, &data
)) {
1000 fprintf(stderr
, "Clipboard allocation failed\n");
1005 * As seen in selcopy:
1006 * Line endings are inconsistent in the terminal and GUI world
1007 * copy and pasting. When receiving some selection data,
1008 * replace all '\n' with '\r'.
1009 * FIXME: Fix the computer world.
1012 last
= data
+ nitems
* format
/ 8;
1013 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1017 if(IS_SET(MODE_BRCKTPASTE
))
1018 ttywrite("\033[200~", 6);
1019 ttywrite((const char *)data
, nitems
* format
/ 8);
1020 if(IS_SET(MODE_BRCKTPASTE
))
1021 ttywrite("\033[201~", 6);
1023 /* number of 32-bit chunks returned */
1024 ofs
+= nitems
* format
/ 32;
1029 selpaste(const Arg
*dummy
) {
1030 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1031 xw
.win
, CurrentTime
);
1035 clippaste(const Arg
*dummy
) {
1038 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1039 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1040 xw
.win
, CurrentTime
);
1044 selclear(XEvent
*e
) {
1048 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1052 selrequest(XEvent
*e
) {
1053 XSelectionRequestEvent
*xsre
;
1054 XSelectionEvent xev
;
1055 Atom xa_targets
, string
;
1057 xsre
= (XSelectionRequestEvent
*) e
;
1058 xev
.type
= SelectionNotify
;
1059 xev
.requestor
= xsre
->requestor
;
1060 xev
.selection
= xsre
->selection
;
1061 xev
.target
= xsre
->target
;
1062 xev
.time
= xsre
->time
;
1064 xev
.property
= None
;
1066 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1067 if(xsre
->target
== xa_targets
) {
1068 /* respond with the supported type */
1069 string
= sel
.xtarget
;
1070 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1071 XA_ATOM
, 32, PropModeReplace
,
1072 (uchar
*) &string
, 1);
1073 xev
.property
= xsre
->property
;
1074 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1075 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1076 xsre
->target
, 8, PropModeReplace
,
1077 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1078 xev
.property
= xsre
->property
;
1081 /* all done, send a notification to the listener */
1082 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1083 fprintf(stderr
, "Error sending SelectionNotify event\n");
1087 xsetsel(char *str
) {
1088 /* register the selection for both the clipboard and the primary */
1094 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1096 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1097 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1101 brelease(XEvent
*e
) {
1102 if(IS_SET(MODE_MOUSE
)) {
1107 if(e
->xbutton
.button
== Button2
) {
1109 } else if(e
->xbutton
.button
== Button1
) {
1117 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1122 bmotion(XEvent
*e
) {
1123 int oldey
, oldex
, oldsby
, oldsey
;
1125 if(IS_SET(MODE_MOUSE
)) {
1140 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1141 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1145 die(const char *errstr
, ...) {
1148 va_start(ap
, errstr
);
1149 vfprintf(stderr
, errstr
, ap
);
1157 char *envshell
= getenv("SHELL");
1158 const struct passwd
*pass
= getpwuid(getuid());
1159 char buf
[sizeof(long) * 8 + 1];
1161 unsetenv("COLUMNS");
1163 unsetenv("TERMCAP");
1166 setenv("LOGNAME", pass
->pw_name
, 1);
1167 setenv("USER", pass
->pw_name
, 1);
1168 setenv("SHELL", pass
->pw_shell
, 0);
1169 setenv("HOME", pass
->pw_dir
, 0);
1172 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1173 setenv("WINDOWID", buf
, 1);
1175 signal(SIGCHLD
, SIG_DFL
);
1176 signal(SIGHUP
, SIG_DFL
);
1177 signal(SIGINT
, SIG_DFL
);
1178 signal(SIGQUIT
, SIG_DFL
);
1179 signal(SIGTERM
, SIG_DFL
);
1180 signal(SIGALRM
, SIG_DFL
);
1182 DEFAULT(envshell
, shell
);
1183 setenv("TERM", termname
, 1);
1184 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1185 execvp(args
[0], args
);
1193 if(waitpid(pid
, &stat
, 0) < 0)
1194 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1196 if(WIFEXITED(stat
)) {
1197 exit(WEXITSTATUS(stat
));
1206 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1208 /* seems to work fine on linux, openbsd and freebsd */
1209 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1210 die("openpty failed: %s\n", SERRNO
);
1212 switch(pid
= fork()) {
1214 die("fork failed\n");
1217 setsid(); /* create a new process group */
1218 dup2(s
, STDIN_FILENO
);
1219 dup2(s
, STDOUT_FILENO
);
1220 dup2(s
, STDERR_FILENO
);
1221 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1222 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1230 signal(SIGCHLD
, sigchld
);
1232 iofd
= (!strcmp(opt_io
, "-")) ?
1234 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1236 fprintf(stderr
, "Error opening %s:%s\n",
1237 opt_io
, strerror(errno
));
1247 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1249 fprintf(stderr
, "\n");
1254 static char buf
[BUFSIZ
];
1255 static int buflen
= 0;
1258 int charsize
; /* size of utf8 char in bytes */
1262 /* append read bytes to unprocessed bytes */
1263 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1264 die("Couldn't read from shell: %s\n", SERRNO
);
1266 /* process every complete utf8 char */
1269 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1270 charsize
= utf8decode(ptr
, &utf8c
);
1271 utf8encode(&utf8c
, s
);
1277 /* keep any uncomplete utf8 char for the next call */
1278 memmove(buf
, ptr
, buflen
);
1282 ttywrite(const char *s
, size_t n
) {
1283 if(write(cmdfd
, s
, n
) == -1)
1284 die("write error on tty: %s\n", SERRNO
);
1291 w
.ws_row
= term
.row
;
1292 w
.ws_col
= term
.col
;
1293 w
.ws_xpixel
= xw
.tw
;
1294 w
.ws_ypixel
= xw
.th
;
1295 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1296 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1300 tattrset(int attr
) {
1303 for(i
= 0; i
< term
.row
-1; i
++) {
1304 for(j
= 0; j
< term
.col
-1; j
++) {
1305 if(term
.line
[i
][j
].mode
& attr
)
1314 tsetdirt(int top
, int bot
) {
1317 LIMIT(top
, 0, term
.row
-1);
1318 LIMIT(bot
, 0, term
.row
-1);
1320 for(i
= top
; i
<= bot
; i
++)
1325 tsetdirtattr(int attr
) {
1328 for(i
= 0; i
< term
.row
-1; i
++) {
1329 for(j
= 0; j
< term
.col
-1; j
++) {
1330 if(term
.line
[i
][j
].mode
& attr
) {
1340 tsetdirt(0, term
.row
-1);
1345 static TCursor c
[2];
1346 bool alt
= IS_SET(MODE_ALTSCREEN
);
1348 if(mode
== CURSOR_SAVE
) {
1350 } else if(mode
== CURSOR_LOAD
) {
1352 tmoveto(c
[alt
].x
, c
[alt
].y
);
1360 term
.c
= (TCursor
){{
1364 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1366 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1367 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1370 term
.bot
= term
.row
- 1;
1371 term
.mode
= MODE_WRAP
;
1373 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1375 tcursor(CURSOR_SAVE
);
1379 tnew(int col
, int row
) {
1380 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1389 Line
*tmp
= term
.line
;
1391 term
.line
= term
.alt
;
1393 term
.mode
^= MODE_ALTSCREEN
;
1398 tscrolldown(int orig
, int n
) {
1402 LIMIT(n
, 0, term
.bot
-orig
+1);
1404 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1406 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1407 temp
= term
.line
[i
];
1408 term
.line
[i
] = term
.line
[i
-n
];
1409 term
.line
[i
-n
] = temp
;
1412 term
.dirty
[i
-n
] = 1;
1419 tscrollup(int orig
, int n
) {
1422 LIMIT(n
, 0, term
.bot
-orig
+1);
1424 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1426 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1427 temp
= term
.line
[i
];
1428 term
.line
[i
] = term
.line
[i
+n
];
1429 term
.line
[i
+n
] = temp
;
1432 term
.dirty
[i
+n
] = 1;
1435 selscroll(orig
, -n
);
1439 selscroll(int orig
, int n
) {
1443 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1444 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1448 if(sel
.type
== SEL_RECTANGULAR
) {
1449 if(sel
.ob
.y
< term
.top
)
1450 sel
.ob
.y
= term
.top
;
1451 if(sel
.oe
.y
> term
.bot
)
1452 sel
.oe
.y
= term
.bot
;
1454 if(sel
.ob
.y
< term
.top
) {
1455 sel
.ob
.y
= term
.top
;
1458 if(sel
.oe
.y
> term
.bot
) {
1459 sel
.oe
.y
= term
.bot
;
1460 sel
.oe
.x
= term
.col
;
1468 tnewline(int first_col
) {
1472 tscrollup(term
.top
, 1);
1476 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1481 char *p
= csiescseq
.buf
, *np
;
1490 csiescseq
.buf
[csiescseq
.len
] = '\0';
1491 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1493 v
= strtol(p
, &np
, 10);
1496 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1498 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1500 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1504 csiescseq
.mode
= *p
;
1507 /* for absolute user moves, when decom is set */
1509 tmoveato(int x
, int y
) {
1510 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1514 tmoveto(int x
, int y
) {
1517 if(term
.c
.state
& CURSOR_ORIGIN
) {
1522 maxy
= term
.row
- 1;
1524 LIMIT(x
, 0, term
.col
-1);
1525 LIMIT(y
, miny
, maxy
);
1526 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1532 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1533 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1534 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1535 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1536 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1537 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1538 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1539 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1540 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1541 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1545 * The table is proudly stolen from rxvt.
1547 if(attr
->mode
& ATTR_GFX
) {
1548 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1549 && vt100_0
[c
[0] - 0x41]) {
1550 c
= vt100_0
[c
[0] - 0x41];
1554 if(term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1555 if(x
+1 < term
.col
) {
1556 term
.line
[y
][x
+1].c
[0] = ' ';
1557 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1559 } else if(term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1560 term
.line
[y
][x
-1].c
[0] = ' ';
1561 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1565 term
.line
[y
][x
] = *attr
;
1566 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1570 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1574 temp
= x1
, x1
= x2
, x2
= temp
;
1576 temp
= y1
, y1
= y2
, y2
= temp
;
1578 LIMIT(x1
, 0, term
.col
-1);
1579 LIMIT(x2
, 0, term
.col
-1);
1580 LIMIT(y1
, 0, term
.row
-1);
1581 LIMIT(y2
, 0, term
.row
-1);
1583 for(y
= y1
; y
<= y2
; y
++) {
1585 for(x
= x1
; x
<= x2
; x
++) {
1588 term
.line
[y
][x
] = term
.c
.attr
;
1589 memcpy(term
.line
[y
][x
].c
, " ", 2);
1595 tdeletechar(int n
) {
1596 int src
= term
.c
.x
+ n
;
1598 int size
= term
.col
- src
;
1600 term
.dirty
[term
.c
.y
] = 1;
1602 if(src
>= term
.col
) {
1603 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1607 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1608 size
* sizeof(Glyph
));
1609 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1613 tinsertblank(int n
) {
1616 int size
= term
.col
- dst
;
1618 term
.dirty
[term
.c
.y
] = 1;
1620 if(dst
>= term
.col
) {
1621 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1625 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1626 size
* sizeof(Glyph
));
1627 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1631 tinsertblankline(int n
) {
1632 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1635 tscrolldown(term
.c
.y
, n
);
1639 tdeleteline(int n
) {
1640 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1643 tscrollup(term
.c
.y
, n
);
1647 tdefcolor(int *attr
, int *npar
, int l
) {
1651 switch (attr
[*npar
+ 1]) {
1652 case 2: /* direct colour in RGB space */
1653 if (*npar
+ 4 >= l
) {
1655 "erresc(38): Incorrect number of parameters (%d)\n",
1659 r
= attr
[*npar
+ 2];
1660 g
= attr
[*npar
+ 3];
1661 b
= attr
[*npar
+ 4];
1663 if(!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1664 fprintf(stderr
, "erresc: bad rgb color (%d,%d,%d)\n",
1667 idx
= TRUECOLOR(r
, g
, b
);
1669 case 5: /* indexed colour */
1670 if (*npar
+ 2 >= l
) {
1672 "erresc(38): Incorrect number of parameters (%d)\n",
1677 if(!BETWEEN(attr
[*npar
], 0, 255))
1678 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1682 case 0: /* implemented defined (only foreground) */
1683 case 1: /* transparent */
1684 case 3: /* direct colour in CMY space */
1685 case 4: /* direct colour in CMYK space */
1688 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1695 tsetattr(int *attr
, int l
) {
1699 for(i
= 0; i
< l
; i
++) {
1702 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1703 | ATTR_BOLD
| ATTR_ITALIC \
1705 term
.c
.attr
.fg
= defaultfg
;
1706 term
.c
.attr
.bg
= defaultbg
;
1709 term
.c
.attr
.mode
|= ATTR_BOLD
;
1712 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1715 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1717 case 5: /* slow blink */
1718 case 6: /* rapid blink */
1719 term
.c
.attr
.mode
|= ATTR_BLINK
;
1722 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1726 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1729 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1732 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1736 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1739 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1742 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1743 term
.c
.attr
.fg
= idx
;
1746 term
.c
.attr
.fg
= defaultfg
;
1749 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1750 term
.c
.attr
.bg
= idx
;
1753 term
.c
.attr
.bg
= defaultbg
;
1756 if(BETWEEN(attr
[i
], 30, 37)) {
1757 term
.c
.attr
.fg
= attr
[i
] - 30;
1758 } else if(BETWEEN(attr
[i
], 40, 47)) {
1759 term
.c
.attr
.bg
= attr
[i
] - 40;
1760 } else if(BETWEEN(attr
[i
], 90, 97)) {
1761 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1762 } else if(BETWEEN(attr
[i
], 100, 107)) {
1763 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1766 "erresc(default): gfx attr %d unknown\n",
1767 attr
[i
]), csidump();
1775 tsetscroll(int t
, int b
) {
1778 LIMIT(t
, 0, term
.row
-1);
1779 LIMIT(b
, 0, term
.row
-1);
1789 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1792 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1796 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1800 case 1: /* DECCKM -- Cursor key */
1801 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1803 case 5: /* DECSCNM -- Reverse video */
1805 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1806 if(mode
!= term
.mode
)
1807 redraw(REDRAW_TIMEOUT
);
1809 case 6: /* DECOM -- Origin */
1810 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1813 case 7: /* DECAWM -- Auto wrap */
1814 MODBIT(term
.mode
, set
, MODE_WRAP
);
1816 case 0: /* Error (IGNORED) */
1817 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1818 case 3: /* DECCOLM -- Column (IGNORED) */
1819 case 4: /* DECSCLM -- Scroll (IGNORED) */
1820 case 8: /* DECARM -- Auto repeat (IGNORED) */
1821 case 18: /* DECPFF -- Printer feed (IGNORED) */
1822 case 19: /* DECPEX -- Printer extent (IGNORED) */
1823 case 42: /* DECNRCM -- National characters (IGNORED) */
1824 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1826 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1827 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1829 case 9: /* X10 mouse compatibility mode */
1830 xsetpointermotion(0);
1831 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1832 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1834 case 1000: /* 1000: report button press */
1835 xsetpointermotion(0);
1836 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1837 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1839 case 1002: /* 1002: report motion on button press */
1840 xsetpointermotion(0);
1841 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1842 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1844 case 1003: /* 1003: enable all mouse motions */
1845 xsetpointermotion(set
);
1846 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1847 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1849 case 1004: /* 1004: send focus events to tty */
1850 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1852 case 1006: /* 1006: extended reporting mode */
1853 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1856 MODBIT(term
.mode
, set
, MODE_8BIT
);
1858 case 1049: /* swap screen & set/restore cursor as xterm */
1859 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1860 case 47: /* swap screen */
1862 if (!allowaltscreen
)
1864 alt
= IS_SET(MODE_ALTSCREEN
);
1866 tclearregion(0, 0, term
.col
-1,
1869 if(set
^ alt
) /* set is always 1 or 0 */
1875 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1877 case 2004: /* 2004: bracketed paste mode */
1878 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
1880 /* Not implemented mouse modes. See comments there. */
1881 case 1001: /* mouse highlight mode; can hang the
1882 terminal by design when implemented. */
1883 case 1005: /* UTF-8 mouse mode; will confuse
1884 applications not supporting UTF-8
1886 case 1015: /* urxvt mangled mouse mode; incompatible
1887 and can be mistaken for other control
1891 "erresc: unknown private set/reset mode %d\n",
1897 case 0: /* Error (IGNORED) */
1899 case 2: /* KAM -- keyboard action */
1900 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1902 case 4: /* IRM -- Insertion-replacement */
1903 MODBIT(term
.mode
, set
, MODE_INSERT
);
1905 case 12: /* SRM -- Send/Receive */
1906 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1908 case 20: /* LNM -- Linefeed/new line */
1909 MODBIT(term
.mode
, set
, MODE_CRLF
);
1913 "erresc: unknown set/reset mode %d\n",
1923 switch(csiescseq
.mode
) {
1926 fprintf(stderr
, "erresc: unknown csi ");
1930 case '@': /* ICH -- Insert <n> blank char */
1931 DEFAULT(csiescseq
.arg
[0], 1);
1932 tinsertblank(csiescseq
.arg
[0]);
1934 case 'A': /* CUU -- Cursor <n> Up */
1935 DEFAULT(csiescseq
.arg
[0], 1);
1936 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1938 case 'B': /* CUD -- Cursor <n> Down */
1939 case 'e': /* VPR --Cursor <n> Down */
1940 DEFAULT(csiescseq
.arg
[0], 1);
1941 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1943 case 'c': /* DA -- Device Attributes */
1944 if(csiescseq
.arg
[0] == 0)
1945 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1947 case 'C': /* CUF -- Cursor <n> Forward */
1948 case 'a': /* HPR -- Cursor <n> Forward */
1949 DEFAULT(csiescseq
.arg
[0], 1);
1950 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1952 case 'D': /* CUB -- Cursor <n> Backward */
1953 DEFAULT(csiescseq
.arg
[0], 1);
1954 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1956 case 'E': /* CNL -- Cursor <n> Down and first col */
1957 DEFAULT(csiescseq
.arg
[0], 1);
1958 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1960 case 'F': /* CPL -- Cursor <n> Up and first col */
1961 DEFAULT(csiescseq
.arg
[0], 1);
1962 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1964 case 'g': /* TBC -- Tabulation clear */
1965 switch(csiescseq
.arg
[0]) {
1966 case 0: /* clear current tab stop */
1967 term
.tabs
[term
.c
.x
] = 0;
1969 case 3: /* clear all the tabs */
1970 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1976 case 'G': /* CHA -- Move to <col> */
1978 DEFAULT(csiescseq
.arg
[0], 1);
1979 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1981 case 'H': /* CUP -- Move to <row> <col> */
1983 DEFAULT(csiescseq
.arg
[0], 1);
1984 DEFAULT(csiescseq
.arg
[1], 1);
1985 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1987 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1988 DEFAULT(csiescseq
.arg
[0], 1);
1989 while(csiescseq
.arg
[0]--)
1992 case 'J': /* ED -- Clear screen */
1994 switch(csiescseq
.arg
[0]) {
1996 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1997 if(term
.c
.y
< term
.row
-1) {
1998 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2004 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2005 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2008 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2014 case 'K': /* EL -- Clear line */
2015 switch(csiescseq
.arg
[0]) {
2017 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2021 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2024 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2028 case 'S': /* SU -- Scroll <n> line up */
2029 DEFAULT(csiescseq
.arg
[0], 1);
2030 tscrollup(term
.top
, csiescseq
.arg
[0]);
2032 case 'T': /* SD -- Scroll <n> line down */
2033 DEFAULT(csiescseq
.arg
[0], 1);
2034 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2036 case 'L': /* IL -- Insert <n> blank lines */
2037 DEFAULT(csiescseq
.arg
[0], 1);
2038 tinsertblankline(csiescseq
.arg
[0]);
2040 case 'l': /* RM -- Reset Mode */
2041 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2043 case 'M': /* DL -- Delete <n> lines */
2044 DEFAULT(csiescseq
.arg
[0], 1);
2045 tdeleteline(csiescseq
.arg
[0]);
2047 case 'X': /* ECH -- Erase <n> char */
2048 DEFAULT(csiescseq
.arg
[0], 1);
2049 tclearregion(term
.c
.x
, term
.c
.y
,
2050 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2052 case 'P': /* DCH -- Delete <n> char */
2053 DEFAULT(csiescseq
.arg
[0], 1);
2054 tdeletechar(csiescseq
.arg
[0]);
2056 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2057 DEFAULT(csiescseq
.arg
[0], 1);
2058 while(csiescseq
.arg
[0]--)
2061 case 'd': /* VPA -- Move to <row> */
2062 DEFAULT(csiescseq
.arg
[0], 1);
2063 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2065 case 'h': /* SM -- Set terminal mode */
2066 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2068 case 'm': /* SGR -- Terminal attribute (color) */
2069 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2071 case 'n': /* DSR – Device Status Report (cursor position) */
2072 if (csiescseq
.arg
[0] == 6) {
2074 int len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR", term
.c
.y
+1, term
.c
.x
+1);
2078 case 'r': /* DECSTBM -- Set Scrolling Region */
2079 if(csiescseq
.priv
) {
2082 DEFAULT(csiescseq
.arg
[0], 1);
2083 DEFAULT(csiescseq
.arg
[1], term
.row
);
2084 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2088 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2089 tcursor(CURSOR_SAVE
);
2091 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2092 tcursor(CURSOR_LOAD
);
2103 for(i
= 0; i
< csiescseq
.len
; i
++) {
2104 c
= csiescseq
.buf
[i
] & 0xff;
2107 } else if(c
== '\n') {
2109 } else if(c
== '\r') {
2111 } else if(c
== 0x1b) {
2114 printf("(%02x)", c
);
2122 memset(&csiescseq
, 0, sizeof(csiescseq
));
2131 narg
= strescseq
.narg
;
2133 switch(strescseq
.type
) {
2134 case ']': /* OSC -- Operating System Command */
2135 switch(i
= atoi(strescseq
.args
[0])) {
2140 xsettitle(strescseq
.args
[1]);
2142 case 4: /* color set */
2145 p
= strescseq
.args
[2];
2147 case 104: /* color reset, here p = NULL */
2148 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2149 if (!xsetcolorname(j
, p
)) {
2150 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2153 * TODO if defaultbg color is changed, borders
2160 fprintf(stderr
, "erresc: unknown str ");
2165 case 'k': /* old title set compatibility */
2166 xsettitle(strescseq
.args
[0]);
2168 case 'P': /* DSC -- Device Control String */
2169 case '_': /* APC -- Application Program Command */
2170 case '^': /* PM -- Privacy Message */
2172 fprintf(stderr
, "erresc: unknown str ");
2181 char *p
= strescseq
.buf
;
2184 strescseq
.buf
[strescseq
.len
] = '\0';
2185 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2186 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2194 printf("ESC%c", strescseq
.type
);
2195 for(i
= 0; i
< strescseq
.len
; i
++) {
2196 c
= strescseq
.buf
[i
] & 0xff;
2199 } else if(isprint(c
)) {
2201 } else if(c
== '\n') {
2203 } else if(c
== '\r') {
2205 } else if(c
== 0x1b) {
2208 printf("(%02x)", c
);
2216 memset(&strescseq
, 0, sizeof(strescseq
));
2220 tputtab(bool forward
) {
2226 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2231 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2234 tmoveto(x
, term
.c
.y
);
2238 techo(char *buf
, int len
) {
2239 for(; len
> 0; buf
++, len
--) {
2242 if(c
== '\033') { /* escape */
2245 } else if(c
< '\x20') { /* control code */
2246 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2260 tputc(char *c
, int len
) {
2262 bool control
= ascii
< '\x20' || ascii
== 0177;
2269 utf8decode(c
, &u8char
);
2270 width
= wcwidth(u8char
);
2274 if(xwrite(iofd
, c
, len
) < 0) {
2275 fprintf(stderr
, "Error writing in %s:%s\n",
2276 opt_io
, strerror(errno
));
2283 * STR sequences must be checked before anything else
2284 * because it can use some control codes as part of the sequence.
2286 if(term
.esc
& ESC_STR
) {
2289 term
.esc
= ESC_START
| ESC_STR_END
;
2291 case '\a': /* backwards compatibility to xterm */
2296 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2297 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2298 strescseq
.len
+= len
;
2301 * Here is a bug in terminals. If the user never sends
2302 * some code to stop the str or esc command, then st
2303 * will stop responding. But this is better than
2304 * silently failing with unknown characters. At least
2305 * then users will report back.
2307 * In the case users ever get fixed, here is the code:
2319 * Actions of control codes must be performed as soon they arrive
2320 * because they can be embedded inside a control sequence, and
2321 * they must not cause conflicts with sequences.
2329 tmoveto(term
.c
.x
-1, term
.c
.y
);
2332 tmoveto(0, term
.c
.y
);
2337 /* go to first col if the mode is set */
2338 tnewline(IS_SET(MODE_CRLF
));
2340 case '\a': /* BEL */
2341 if(!(xw
.state
& WIN_FOCUSED
))
2344 XBell(xw
.dpy
, bellvolume
);
2346 case '\033': /* ESC */
2348 term
.esc
= ESC_START
;
2350 case '\016': /* SO */
2351 case '\017': /* SI */
2353 * Different charsets are hard to handle. Applications
2354 * should use the right alt charset escapes for the
2355 * only reason they still exist: line drawing. The
2356 * rest is incompatible history st should not support.
2359 case '\032': /* SUB */
2360 case '\030': /* CAN */
2363 case '\005': /* ENQ (IGNORED) */
2364 case '\000': /* NUL (IGNORED) */
2365 case '\021': /* XON (IGNORED) */
2366 case '\023': /* XOFF (IGNORED) */
2367 case 0177: /* DEL (IGNORED) */
2370 } else if(term
.esc
& ESC_START
) {
2371 if(term
.esc
& ESC_CSI
) {
2372 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2373 if(BETWEEN(ascii
, 0x40, 0x7E)
2374 || csiescseq
.len
>= \
2375 sizeof(csiescseq
.buf
)-1) {
2380 } else if(term
.esc
& ESC_STR_END
) {
2384 } else if(term
.esc
& ESC_ALTCHARSET
) {
2386 case '0': /* Line drawing set */
2387 term
.c
.attr
.mode
|= ATTR_GFX
;
2389 case 'B': /* USASCII */
2390 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2392 case 'A': /* UK (IGNORED) */
2393 case '<': /* multinational charset (IGNORED) */
2394 case '5': /* Finnish (IGNORED) */
2395 case 'C': /* Finnish (IGNORED) */
2396 case 'K': /* German (IGNORED) */
2399 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2402 } else if(term
.esc
& ESC_TEST
) {
2403 if(ascii
== '8') { /* DEC screen alignment test. */
2404 char E
[UTF_SIZ
] = "E";
2407 for(x
= 0; x
< term
.col
; ++x
) {
2408 for(y
= 0; y
< term
.row
; ++y
)
2409 tsetchar(E
, &term
.c
.attr
, x
, y
);
2416 term
.esc
|= ESC_CSI
;
2419 term
.esc
|= ESC_TEST
;
2421 case 'P': /* DCS -- Device Control String */
2422 case '_': /* APC -- Application Program Command */
2423 case '^': /* PM -- Privacy Message */
2424 case ']': /* OSC -- Operating System Command */
2425 case 'k': /* old title set compatibility */
2427 strescseq
.type
= ascii
;
2428 term
.esc
|= ESC_STR
;
2430 case '(': /* set primary charset G0 */
2431 term
.esc
|= ESC_ALTCHARSET
;
2433 case ')': /* set secondary charset G1 (IGNORED) */
2434 case '*': /* set tertiary charset G2 (IGNORED) */
2435 case '+': /* set quaternary charset G3 (IGNORED) */
2438 case 'D': /* IND -- Linefeed */
2439 if(term
.c
.y
== term
.bot
) {
2440 tscrollup(term
.top
, 1);
2442 tmoveto(term
.c
.x
, term
.c
.y
+1);
2446 case 'E': /* NEL -- Next line */
2447 tnewline(1); /* always go to first col */
2450 case 'H': /* HTS -- Horizontal tab stop */
2451 term
.tabs
[term
.c
.x
] = 1;
2454 case 'M': /* RI -- Reverse index */
2455 if(term
.c
.y
== term
.top
) {
2456 tscrolldown(term
.top
, 1);
2458 tmoveto(term
.c
.x
, term
.c
.y
-1);
2462 case 'Z': /* DECID -- Identify Terminal */
2463 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2466 case 'c': /* RIS -- Reset to inital state */
2472 case '=': /* DECPAM -- Application keypad */
2473 term
.mode
|= MODE_APPKEYPAD
;
2476 case '>': /* DECPNM -- Normal keypad */
2477 term
.mode
&= ~MODE_APPKEYPAD
;
2480 case '7': /* DECSC -- Save Cursor */
2481 tcursor(CURSOR_SAVE
);
2484 case '8': /* DECRC -- Restore Cursor */
2485 tcursor(CURSOR_LOAD
);
2488 case '\\': /* ST -- Stop */
2492 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2493 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2498 * All characters which form part of a sequence are not
2504 * Display control codes only if we are in graphic mode
2506 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2508 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2510 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2511 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2515 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2516 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2517 &term
.line
[term
.c
.y
][term
.c
.x
],
2518 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2521 if(term
.c
.x
+width
> term
.col
)
2524 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2527 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WIDE
;
2528 if(term
.c
.x
+1 < term
.col
) {
2529 term
.line
[term
.c
.y
][term
.c
.x
+1].c
[0] = '\0';
2530 term
.line
[term
.c
.y
][term
.c
.x
+1].mode
= ATTR_WDUMMY
;
2533 if(term
.c
.x
+width
< term
.col
) {
2534 tmoveto(term
.c
.x
+width
, term
.c
.y
);
2536 term
.c
.state
|= CURSOR_WRAPNEXT
;
2541 tresize(int col
, int row
) {
2543 int minrow
= MIN(row
, term
.row
);
2544 int mincol
= MIN(col
, term
.col
);
2545 int slide
= term
.c
.y
- row
+ 1;
2549 if(col
< 1 || row
< 1)
2552 /* free unneeded rows */
2556 * slide screen to keep cursor where we expect it -
2557 * tscrollup would work here, but we can optimize to
2558 * memmove because we're freeing the earlier lines
2560 for(/* i = 0 */; i
< slide
; i
++) {
2564 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2565 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2567 for(i
+= row
; i
< term
.row
; i
++) {
2572 /* resize to new height */
2573 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2574 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2575 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2576 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2578 /* resize each row to new width, zero-pad if needed */
2579 for(i
= 0; i
< minrow
; i
++) {
2581 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2582 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2585 /* allocate any new rows */
2586 for(/* i == minrow */; i
< row
; i
++) {
2588 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
2589 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
2591 if(col
> term
.col
) {
2592 bp
= term
.tabs
+ term
.col
;
2594 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2595 while(--bp
> term
.tabs
&& !*bp
)
2597 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2600 /* update terminal size */
2603 /* reset scrolling region */
2604 tsetscroll(0, row
-1);
2605 /* make use of the LIMIT in tmoveto */
2606 tmoveto(term
.c
.x
, term
.c
.y
);
2607 /* Clearing both screens */
2610 if(mincol
< col
&& 0 < minrow
) {
2611 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2613 if(0 < col
&& minrow
< row
) {
2614 tclearregion(0, minrow
, col
- 1, row
- 1);
2617 } while(orig
!= term
.line
);
2623 xresize(int col
, int row
) {
2624 xw
.tw
= MAX(1, col
* xw
.cw
);
2625 xw
.th
= MAX(1, row
* xw
.ch
);
2627 XFreePixmap(xw
.dpy
, xw
.buf
);
2628 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2629 DefaultDepth(xw
.dpy
, xw
.scr
));
2630 XftDrawChange(xw
.draw
, xw
.buf
);
2631 xclear(0, 0, xw
.w
, xw
.h
);
2634 static inline ushort
2635 sixd_to_16bit(int x
) {
2636 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2642 XRenderColor color
= { .alpha
= 0xffff };
2647 for (cp
= dc
.col
; cp
< dc
.col
+ LEN(dc
.col
); ++cp
)
2648 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
2651 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2652 for(i
= 0; i
< LEN(colorname
); i
++) {
2655 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2656 die("Could not allocate color '%s'\n", colorname
[i
]);
2660 /* load colors [16-255] ; same colors as xterm */
2661 for(i
= 16, r
= 0; r
< 6; r
++) {
2662 for(g
= 0; g
< 6; g
++) {
2663 for(b
= 0; b
< 6; b
++) {
2664 color
.red
= sixd_to_16bit(r
);
2665 color
.green
= sixd_to_16bit(g
);
2666 color
.blue
= sixd_to_16bit(b
);
2667 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2668 die("Could not allocate color %d\n", i
);
2675 for(r
= 0; r
< 24; r
++, i
++) {
2676 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2677 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2679 die("Could not allocate color %d\n", i
);
2686 xsetcolorname(int x
, const char *name
) {
2687 XRenderColor color
= { .alpha
= 0xffff };
2689 if (x
< 0 || x
> LEN(colorname
))
2692 if(16 <= x
&& x
< 16 + 216) {
2693 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2694 color
.red
= sixd_to_16bit(r
);
2695 color
.green
= sixd_to_16bit(g
);
2696 color
.blue
= sixd_to_16bit(b
);
2697 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2698 return 0; /* something went wrong */
2701 } else if (16 + 216 <= x
&& x
< 256) {
2702 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2703 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2704 return 0; /* something went wrong */
2708 name
= colorname
[x
];
2711 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2718 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2719 XftDrawRect(xw
.draw
,
2720 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2721 borderpx
+ col1
* xw
.cw
,
2722 borderpx
+ row1
* xw
.ch
,
2723 (col2
-col1
+1) * xw
.cw
,
2724 (row2
-row1
+1) * xw
.ch
);
2728 * Absolute coordinates.
2731 xclear(int x1
, int y1
, int x2
, int y2
) {
2732 XftDrawRect(xw
.draw
,
2733 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2734 x1
, y1
, x2
-x1
, y2
-y1
);
2739 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2740 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2741 XSizeHints
*sizeh
= NULL
;
2743 sizeh
= XAllocSizeHints();
2744 if(xw
.isfixed
== False
) {
2745 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2746 sizeh
->height
= xw
.h
;
2747 sizeh
->width
= xw
.w
;
2748 sizeh
->height_inc
= xw
.ch
;
2749 sizeh
->width_inc
= xw
.cw
;
2750 sizeh
->base_height
= 2 * borderpx
;
2751 sizeh
->base_width
= 2 * borderpx
;
2753 sizeh
->flags
= PMaxSize
| PMinSize
;
2754 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2755 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2758 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2763 xloadfont(Font
*f
, FcPattern
*pattern
) {
2767 match
= FcFontMatch(NULL
, pattern
, &result
);
2771 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2772 FcPatternDestroy(match
);
2777 f
->pattern
= FcPatternDuplicate(pattern
);
2779 f
->ascent
= f
->match
->ascent
;
2780 f
->descent
= f
->match
->descent
;
2782 f
->rbearing
= f
->match
->max_advance_width
;
2784 f
->height
= f
->ascent
+ f
->descent
;
2785 f
->width
= f
->lbearing
+ f
->rbearing
;
2791 xloadfonts(char *fontstr
, int fontsize
) {
2796 if(fontstr
[0] == '-') {
2797 pattern
= XftXlfdParse(fontstr
, False
, False
);
2799 pattern
= FcNameParse((FcChar8
*)fontstr
);
2803 die("st: can't open font %s\n", fontstr
);
2806 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2807 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2808 usedfontsize
= fontsize
;
2810 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2811 if(result
== FcResultMatch
) {
2812 usedfontsize
= (int)fontval
;
2815 * Default font size is 12, if none given. This is to
2816 * have a known usedfontsize value.
2818 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2823 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2824 FcDefaultSubstitute(pattern
);
2826 if(xloadfont(&dc
.font
, pattern
))
2827 die("st: can't open font %s\n", fontstr
);
2829 /* Setting character width and height. */
2830 xw
.cw
= CEIL(dc
.font
.width
* cwscale
);
2831 xw
.ch
= CEIL(dc
.font
.height
* chscale
);
2833 FcPatternDel(pattern
, FC_SLANT
);
2834 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2835 if(xloadfont(&dc
.ifont
, pattern
))
2836 die("st: can't open font %s\n", fontstr
);
2838 FcPatternDel(pattern
, FC_WEIGHT
);
2839 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2840 if(xloadfont(&dc
.ibfont
, pattern
))
2841 die("st: can't open font %s\n", fontstr
);
2843 FcPatternDel(pattern
, FC_SLANT
);
2844 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2845 if(xloadfont(&dc
.bfont
, pattern
))
2846 die("st: can't open font %s\n", fontstr
);
2848 FcPatternDestroy(pattern
);
2852 xloadfontset(Font
*f
) {
2855 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2861 xunloadfont(Font
*f
) {
2862 XftFontClose(xw
.dpy
, f
->match
);
2863 FcPatternDestroy(f
->pattern
);
2865 FcFontSetDestroy(f
->set
);
2869 xunloadfonts(void) {
2872 /* Free the loaded fonts in the font cache. */
2873 for(i
= 0; i
< frclen
; i
++) {
2874 XftFontClose(xw
.dpy
, frc
[i
].font
);
2878 xunloadfont(&dc
.font
);
2879 xunloadfont(&dc
.bfont
);
2880 xunloadfont(&dc
.ifont
);
2881 xunloadfont(&dc
.ibfont
);
2885 xzoom(const Arg
*arg
) {
2887 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2899 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2900 die("Can't open display\n");
2901 xw
.scr
= XDefaultScreen(xw
.dpy
);
2902 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2906 die("Could not init fontconfig.\n");
2908 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2909 xloadfonts(usedfont
, 0);
2912 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2915 /* adjust fixed window geometry */
2917 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2918 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2920 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2922 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2927 /* window - default size */
2928 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2929 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2935 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2936 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2937 xw
.attrs
.bit_gravity
= NorthWestGravity
;
2938 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2939 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2940 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2941 xw
.attrs
.colormap
= xw
.cmap
;
2943 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2944 XRootWindow(xw
.dpy
, xw
.scr
);
2945 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2946 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2947 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
2948 | CWEventMask
| CWColormap
, &xw
.attrs
);
2950 memset(&gcvalues
, 0, sizeof(gcvalues
));
2951 gcvalues
.graphics_exposures
= False
;
2952 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2954 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2955 DefaultDepth(xw
.dpy
, xw
.scr
));
2956 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2957 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2959 /* Xft rendering context */
2960 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2963 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2964 XSetLocaleModifiers("@im=local");
2965 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2966 XSetLocaleModifiers("@im=");
2967 if((xw
.xim
= XOpenIM(xw
.dpy
,
2968 NULL
, NULL
, NULL
)) == NULL
) {
2969 die("XOpenIM failed. Could not open input"
2974 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2975 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2976 XNFocusWindow
, xw
.win
, NULL
);
2978 die("XCreateIC failed. Could not obtain input method.\n");
2980 /* white cursor, black outline */
2981 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2982 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2983 XRecolorCursor(xw
.dpy
, cursor
,
2984 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2985 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2987 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2988 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2989 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2992 XMapWindow(xw
.dpy
, xw
.win
);
2998 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2999 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3000 width
= charlen
* xw
.cw
, xp
, i
;
3002 int u8fl
, u8fblen
, u8cblen
, doesexist
;
3005 Font
*font
= &dc
.font
;
3007 FcPattern
*fcpattern
, *fontpattern
;
3008 FcFontSet
*fcsets
[] = { NULL
};
3009 FcCharSet
*fccharset
;
3010 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3011 XRenderColor colfg
, colbg
;
3015 frcflags
= FRC_NORMAL
;
3017 if(base
.mode
& ATTR_ITALIC
) {
3018 if(base
.fg
== defaultfg
)
3019 base
.fg
= defaultitalic
;
3021 frcflags
= FRC_ITALIC
;
3022 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
3023 if(base
.fg
== defaultfg
)
3024 base
.fg
= defaultitalic
;
3026 frcflags
= FRC_ITALICBOLD
;
3027 } else if(base
.mode
& ATTR_UNDERLINE
) {
3028 if(base
.fg
== defaultfg
)
3029 base
.fg
= defaultunderline
;
3031 if(IS_TRUECOL(base
.fg
)) {
3032 colfg
.red
= TRUERED(base
.fg
);
3033 colfg
.green
= TRUEGREEN(base
.fg
);
3034 colfg
.blue
= TRUEBLUE(base
.fg
);
3035 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3038 fg
= &dc
.col
[base
.fg
];
3041 if(IS_TRUECOL(base
.bg
)) {
3042 colbg
.green
= TRUEGREEN(base
.bg
);
3043 colbg
.red
= TRUERED(base
.bg
);
3044 colbg
.blue
= TRUEBLUE(base
.bg
);
3045 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3048 bg
= &dc
.col
[base
.bg
];
3053 if(base
.mode
& ATTR_BOLD
) {
3054 if(BETWEEN(base
.fg
, 0, 7)) {
3055 /* basic system colors */
3056 fg
= &dc
.col
[base
.fg
+ 8];
3057 } else if(BETWEEN(base
.fg
, 16, 195)) {
3059 fg
= &dc
.col
[base
.fg
+ 36];
3060 } else if(BETWEEN(base
.fg
, 232, 251)) {
3062 fg
= &dc
.col
[base
.fg
+ 4];
3065 * Those ranges will not be brightened:
3066 * 8 - 15 – bright system colors
3067 * 196 - 231 – highest 256 color cube
3068 * 252 - 255 – brightest colors in greyscale
3071 frcflags
= FRC_BOLD
;
3074 if(IS_SET(MODE_REVERSE
)) {
3075 if(fg
== &dc
.col
[defaultfg
]) {
3076 fg
= &dc
.col
[defaultbg
];
3078 colfg
.red
= ~fg
->color
.red
;
3079 colfg
.green
= ~fg
->color
.green
;
3080 colfg
.blue
= ~fg
->color
.blue
;
3081 colfg
.alpha
= fg
->color
.alpha
;
3082 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3086 if(bg
== &dc
.col
[defaultbg
]) {
3087 bg
= &dc
.col
[defaultfg
];
3089 colbg
.red
= ~bg
->color
.red
;
3090 colbg
.green
= ~bg
->color
.green
;
3091 colbg
.blue
= ~bg
->color
.blue
;
3092 colbg
.alpha
= bg
->color
.alpha
;
3093 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
3098 if(base
.mode
& ATTR_REVERSE
) {
3104 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3107 /* Intelligent cleaning up of the borders. */
3109 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3110 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3112 if(x
+ charlen
>= term
.col
) {
3113 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3114 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3117 xclear(winx
, 0, winx
+ width
, borderpx
);
3119 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3121 /* Clean up the region we want to draw to. */
3122 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3124 /* Set the clip region because Xft is sometimes dirty. */
3129 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3131 for(xp
= winx
; bytelen
> 0;) {
3133 * Search for the range in the to be printed string of glyphs
3134 * that are in the main font. Then print that range. If
3135 * some glyph is found that is not in the font, do the
3141 oneatatime
= font
->width
!= xw
.cw
;
3144 u8cblen
= utf8decode(s
, &u8char
);
3148 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3149 if(oneatatime
|| !doesexist
|| bytelen
<= 0) {
3150 if(oneatatime
|| bytelen
<= 0) {
3158 XftDrawStringUtf8(xw
.draw
, fg
,
3160 winy
+ font
->ascent
,
3178 /* Search the font cache. */
3179 for(i
= 0; i
< frclen
; i
++) {
3180 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3181 && frc
[i
].flags
== frcflags
) {
3186 /* Nothing was found. */
3190 fcsets
[0] = font
->set
;
3193 * Nothing was found in the cache. Now use
3194 * some dozen of Fontconfig calls to get the
3195 * font for one single character.
3197 fcpattern
= FcPatternDuplicate(font
->pattern
);
3198 fccharset
= FcCharSetCreate();
3200 FcCharSetAddChar(fccharset
, u8char
);
3201 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3203 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3206 FcConfigSubstitute(0, fcpattern
,
3208 FcDefaultSubstitute(fcpattern
);
3210 fontpattern
= FcFontSetMatch(0, fcsets
,
3211 FcTrue
, fcpattern
, &fcres
);
3214 * Overwrite or create the new cache entry.
3216 if(frclen
>= LEN(frc
)) {
3217 frclen
= LEN(frc
) - 1;
3218 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3221 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3223 frc
[frclen
].flags
= frcflags
;
3228 FcPatternDestroy(fcpattern
);
3229 FcCharSetDestroy(fccharset
);
3232 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3233 xp
, winy
+ frc
[i
].font
->ascent
,
3234 (FcChar8
*)u8c
, u8cblen
);
3236 xp
+= xw
.cw
* wcwidth(u8char
);
3240 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3241 winy + font->ascent, (FcChar8 *)s, bytelen);
3244 if(base
.mode
& ATTR_UNDERLINE
) {
3245 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3249 /* Reset clip to none. */
3250 XftDrawSetClip(xw
.draw
, 0);
3255 static int oldx
= 0, oldy
= 0;
3256 int sl
, width
, curx
;
3257 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3259 LIMIT(oldx
, 0, term
.col
-1);
3260 LIMIT(oldy
, 0, term
.row
-1);
3264 /* adjust position if in dummy */
3265 if(term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3267 if(term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3270 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3272 /* remove the old cursor */
3273 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3274 width
= (term
.line
[oldy
][oldx
].mode
& ATTR_WIDE
)? 2 : 1;
3275 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3278 /* draw the new one */
3279 if(!(IS_SET(MODE_HIDE
))) {
3280 if(xw
.state
& WIN_FOCUSED
) {
3281 if(IS_SET(MODE_REVERSE
)) {
3282 g
.mode
|= ATTR_REVERSE
;
3288 width
= (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
)\
3290 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, width
, sl
);
3292 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3293 borderpx
+ curx
* xw
.cw
,
3294 borderpx
+ term
.c
.y
* xw
.ch
,
3296 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3297 borderpx
+ curx
* xw
.cw
,
3298 borderpx
+ term
.c
.y
* xw
.ch
,
3300 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3301 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3302 borderpx
+ term
.c
.y
* xw
.ch
,
3304 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3305 borderpx
+ curx
* xw
.cw
,
3306 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3309 oldx
= curx
, oldy
= term
.c
.y
;
3315 xsettitle(char *p
) {
3318 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3320 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3326 xsettitle(opt_title
? opt_title
: "st");
3330 redraw(int timeout
) {
3331 struct timespec tv
= {0, timeout
* 1000};
3337 nanosleep(&tv
, NULL
);
3338 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3344 drawregion(0, 0, term
.col
, term
.row
);
3345 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3347 XSetForeground(xw
.dpy
, dc
.gc
,
3348 dc
.col
[IS_SET(MODE_REVERSE
)?
3349 defaultfg
: defaultbg
].pixel
);
3353 drawregion(int x1
, int y1
, int x2
, int y2
) {
3354 int ic
, ib
, x
, y
, ox
, sl
;
3356 char buf
[DRAW_BUF_SIZ
];
3357 bool ena_sel
= sel
.ob
.x
!= -1;
3360 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3363 if(!(xw
.state
& WIN_VISIBLE
))
3366 for(y
= y1
; y
< y2
; y
++) {
3370 xtermclear(0, y
, term
.col
, y
);
3372 base
= term
.line
[y
][0];
3374 for(x
= x1
; x
< x2
; x
++) {
3375 new = term
.line
[y
][x
];
3376 if(new.mode
== ATTR_WDUMMY
)
3378 if(ena_sel
&& selected(x
, y
))
3379 new.mode
^= ATTR_REVERSE
;
3380 if(ib
> 0 && (ATTRCMP(base
, new)
3381 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3382 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3390 sl
= utf8decode(new.c
, &u8char
);
3391 memcpy(buf
+ib
, new.c
, sl
);
3393 ic
+= (new.mode
& ATTR_WIDE
)? 2 : 1;
3396 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3402 expose(XEvent
*ev
) {
3403 XExposeEvent
*e
= &ev
->xexpose
;
3405 if(xw
.state
& WIN_REDRAW
) {
3407 xw
.state
&= ~WIN_REDRAW
;
3413 visibility(XEvent
*ev
) {
3414 XVisibilityEvent
*e
= &ev
->xvisibility
;
3416 if(e
->state
== VisibilityFullyObscured
) {
3417 xw
.state
&= ~WIN_VISIBLE
;
3418 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3419 /* need a full redraw for next Expose, not just a buf copy */
3420 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3426 xw
.state
&= ~WIN_VISIBLE
;
3430 xsetpointermotion(int set
) {
3431 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3432 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3436 xseturgency(int add
) {
3437 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3439 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3440 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3446 XFocusChangeEvent
*e
= &ev
->xfocus
;
3448 if(e
->mode
== NotifyGrab
)
3451 if(ev
->type
== FocusIn
) {
3452 XSetICFocus(xw
.xic
);
3453 xw
.state
|= WIN_FOCUSED
;
3455 if(IS_SET(MODE_FOCUS
))
3456 ttywrite("\033[I", 3);
3458 XUnsetICFocus(xw
.xic
);
3459 xw
.state
&= ~WIN_FOCUSED
;
3460 if(IS_SET(MODE_FOCUS
))
3461 ttywrite("\033[O", 3);
3466 match(uint mask
, uint state
) {
3467 state
&= ~ignoremod
;
3469 if(mask
== XK_NO_MOD
&& state
)
3471 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3473 if(mask
== XK_ANY_MOD
)
3475 return state
== mask
;
3479 numlock(const Arg
*dummy
) {
3484 kmap(KeySym k
, uint state
) {
3488 /* Check for mapped keys out of X11 function keys. */
3489 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3490 if(mappedkeys
[i
] == k
)
3493 if(i
== LEN(mappedkeys
)) {
3494 if((k
& 0xFFFF) < 0xFD00)
3498 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3502 if(!match(kp
->mask
, state
))
3505 if(kp
->appkey
> 0) {
3506 if(!IS_SET(MODE_APPKEYPAD
))
3508 if(term
.numlock
&& kp
->appkey
== 2)
3510 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3514 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3516 && !IS_SET(MODE_APPCURSOR
))) {
3520 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3521 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3532 kpress(XEvent
*ev
) {
3533 XKeyEvent
*e
= &ev
->xkey
;
3535 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3541 if(IS_SET(MODE_KBDLOCK
))
3544 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3545 e
->state
&= ~Mod2Mask
;
3547 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3548 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3549 bp
->func(&(bp
->arg
));
3554 /* 2. custom keys from config.h */
3555 if((customkey
= kmap(ksym
, e
->state
))) {
3556 len
= strlen(customkey
);
3557 memcpy(buf
, customkey
, len
);
3558 /* 3. hardcoded (overrides X lookup) */
3563 if(len
== 1 && e
->state
& Mod1Mask
) {
3564 if(IS_SET(MODE_8BIT
)) {
3567 ret
= utf8encode(&c
, cp
);
3576 memcpy(cp
, xstr
, len
);
3577 len
= cp
- buf
+ len
;
3581 if(IS_SET(MODE_ECHO
))
3587 cmessage(XEvent
*e
) {
3590 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3592 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3593 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3594 xw
.state
|= WIN_FOCUSED
;
3596 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3597 xw
.state
&= ~WIN_FOCUSED
;
3599 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3600 /* Send SIGHUP to shell */
3607 cresize(int width
, int height
) {
3615 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3616 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3625 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3628 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3634 int w
= xw
.w
, h
= xw
.h
;
3636 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3637 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3639 /* Waiting for window mapping */
3641 XNextEvent(xw
.dpy
, &ev
);
3642 if(ev
.type
== ConfigureNotify
) {
3643 w
= ev
.xconfigure
.width
;
3644 h
= ev
.xconfigure
.height
;
3645 } else if(ev
.type
== MapNotify
) {
3653 cresize(xw
.fw
, xw
.fh
);
3656 gettimeofday(&lastblink
, NULL
);
3657 gettimeofday(&last
, NULL
);
3659 for(xev
= actionfps
;;) {
3661 FD_SET(cmdfd
, &rfd
);
3664 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3667 die("select failed: %s\n", SERRNO
);
3669 if(FD_ISSET(cmdfd
, &rfd
)) {
3672 blinkset
= tattrset(ATTR_BLINK
);
3674 MODBIT(term
.mode
, 0, MODE_BLINK
);
3678 if(FD_ISSET(xfd
, &rfd
))
3681 gettimeofday(&now
, NULL
);
3682 drawtimeout
.tv_sec
= 0;
3683 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3687 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3688 tsetdirtattr(ATTR_BLINK
);
3689 term
.mode
^= MODE_BLINK
;
3690 gettimeofday(&lastblink
, NULL
);
3693 if(TIMEDIFF(now
, last
) \
3694 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3700 while(XPending(xw
.dpy
)) {
3701 XNextEvent(xw
.dpy
, &ev
);
3702 if(XFilterEvent(&ev
, None
))
3704 if(handler
[ev
.type
])
3705 (handler
[ev
.type
])(&ev
);
3711 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3713 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3715 if(TIMEDIFF(now
, lastblink
) \
3717 drawtimeout
.tv_usec
= 1;
3719 drawtimeout
.tv_usec
= (1000 * \
3734 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3735 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3736 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3740 main(int argc
, char *argv
[]) {
3745 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3750 allowaltscreen
= false;
3753 opt_class
= EARGF(usage());
3756 /* eat all remaining arguments */
3759 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3760 titles
= strdup(argv
[1]);
3761 opt_title
= basename(titles
);
3766 opt_font
= EARGF(usage());
3769 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3774 if(bitm
& WidthValue
)
3776 if(bitm
& HeightValue
)
3778 if(bitm
& XNegative
&& xw
.fx
== 0)
3780 if(bitm
& YNegative
&& xw
.fy
== 0)
3783 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3787 opt_io
= EARGF(usage());
3790 opt_title
= EARGF(usage());
3793 opt_embed
= EARGF(usage());
3801 setlocale(LC_CTYPE
, "");
3802 XSetLocaleModifiers("");