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,
126 ESC_STR
= 4, /* DSC, OSC, PM, APC */
128 ESC_STR_END
= 16, /* a final string was encountered */
129 ESC_TEST
= 32, /* Enter in test mode */
138 enum selection_type
{
143 enum selection_snap
{
150 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
152 typedef unsigned char uchar
;
153 typedef unsigned int uint
;
154 typedef unsigned long ulong
;
155 typedef unsigned short ushort
;
158 char c
[UTF_SIZ
]; /* character code */
159 uchar mode
; /* attribute flags */
160 ushort fg
; /* foreground */
161 ushort bg
; /* background */
167 Glyph attr
; /* current char attributes */
173 /* CSI Escape sequence structs */
174 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
176 char buf
[ESC_BUF_SIZ
]; /* raw string */
177 int len
; /* raw string length */
179 int arg
[ESC_ARG_SIZ
];
180 int narg
; /* nb of args */
184 /* STR Escape sequence structs */
185 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
187 char type
; /* ESC type ... */
188 char buf
[STR_BUF_SIZ
]; /* raw string */
189 int len
; /* raw string length */
190 char *args
[STR_ARG_SIZ
];
191 int narg
; /* nb of args */
194 /* Internal representation of the screen */
196 int row
; /* nb row */
197 int col
; /* nb col */
198 Line
*line
; /* screen */
199 Line
*alt
; /* alternate screen */
200 bool *dirty
; /* dirtyness of lines */
201 TCursor c
; /* cursor */
202 int top
; /* top scroll limit */
203 int bot
; /* bottom scroll limit */
204 int mode
; /* terminal mode flags */
205 int esc
; /* escape state flags */
206 bool numlock
; /* lock numbers in keyboard */
210 /* Purely graphic info */
216 Atom xembed
, wmdeletewin
;
222 bool isfixed
; /* is fixed geometry? */
223 int fx
, fy
, fw
, fh
; /* fixed geometry */
224 int tw
, th
; /* tty width and height */
225 int w
, h
; /* window width and height */
226 int ch
; /* char height */
227 int cw
; /* char width */
228 char state
; /* focus, redraw, visible */
241 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
242 signed char appkey
; /* application keypad */
243 signed char appcursor
; /* application cursor */
244 signed char crlf
; /* crlf mode */
247 /* TODO: use better name for vars... */
253 * Selection variables:
254 * nb – normalized coordinates of the beginning of the selection
255 * ne – normalized coordinates of the end of the selection
256 * ob – original coordinates of the beginning of the selection
257 * oe – original coordinates of the end of the selection
266 struct timeval tclick1
;
267 struct timeval tclick2
;
280 void (*func
)(const Arg
*);
284 /* function definitions used in config.h */
285 static void clippaste(const Arg
*);
286 static void numlock(const Arg
*);
287 static void selpaste(const Arg
*);
288 static void xzoom(const Arg
*);
290 /* Config.h for applying patches and the configuration. */
306 /* Drawing Context */
308 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
309 Font font
, bfont
, ifont
, ibfont
;
313 static void die(const char *, ...);
314 static void draw(void);
315 static void redraw(int);
316 static void drawregion(int, int, int, int);
317 static void execsh(void);
318 static void sigchld(int);
319 static void run(void);
321 static void csidump(void);
322 static void csihandle(void);
323 static void csiparse(void);
324 static void csireset(void);
325 static void strdump(void);
326 static void strhandle(void);
327 static void strparse(void);
328 static void strreset(void);
330 static int tattrset(int);
331 static void tclearregion(int, int, int, int);
332 static void tcursor(int);
333 static void tdeletechar(int);
334 static void tdeleteline(int);
335 static void tinsertblank(int);
336 static void tinsertblankline(int);
337 static void tmoveto(int, int);
338 static void tmoveato(int x
, int y
);
339 static void tnew(int, int);
340 static void tnewline(int);
341 static void tputtab(bool);
342 static void tputc(char *, int);
343 static void treset(void);
344 static int tresize(int, int);
345 static void tscrollup(int, int);
346 static void tscrolldown(int, int);
347 static void tsetattr(int*, int);
348 static void tsetchar(char *, Glyph
*, int, int);
349 static void tsetscroll(int, int);
350 static void tswapscreen(void);
351 static void tsetdirt(int, int);
352 static void tsetdirtattr(int);
353 static void tsetmode(bool, bool, int *, int);
354 static void tfulldirt(void);
355 static void techo(char *, int);
357 static inline bool match(uint
, uint
);
358 static void ttynew(void);
359 static void ttyread(void);
360 static void ttyresize(void);
361 static void ttywrite(const char *, size_t);
363 static void xdraws(char *, Glyph
, int, int, int, int);
364 static void xhints(void);
365 static void xclear(int, int, int, int);
366 static void xdrawcursor(void);
367 static void xinit(void);
368 static void xloadcols(void);
369 static int xsetcolorname(int, const char *);
370 static int xloadfont(Font
*, FcPattern
*);
371 static void xloadfonts(char *, int);
372 static int xloadfontset(Font
*);
373 static void xsettitle(char *);
374 static void xresettitle(void);
375 static void xseturgency(int);
376 static void xsetsel(char*);
377 static void xtermclear(int, int, int, int);
378 static void xunloadfont(Font
*f
);
379 static void xunloadfonts(void);
380 static void xresize(int, int);
382 static void expose(XEvent
*);
383 static void visibility(XEvent
*);
384 static void unmap(XEvent
*);
385 static char *kmap(KeySym
, uint
);
386 static void kpress(XEvent
*);
387 static void cmessage(XEvent
*);
388 static void cresize(int, int);
389 static void resize(XEvent
*);
390 static void focus(XEvent
*);
391 static void brelease(XEvent
*);
392 static void bpress(XEvent
*);
393 static void bmotion(XEvent
*);
394 static void selnotify(XEvent
*);
395 static void selclear(XEvent
*);
396 static void selrequest(XEvent
*);
398 static void selinit(void);
399 static void selsort(void);
400 static inline bool selected(int, int);
401 static void selcopy(void);
402 static void selscroll(int, int);
403 static void selsnap(int, int *, int *, int);
405 static int utf8decode(char *, long *);
406 static int utf8encode(long *, char *);
407 static int utf8size(char *);
408 static int isfullutf8(char *, int);
410 static ssize_t
xwrite(int, char *, size_t);
411 static void *xmalloc(size_t);
412 static void *xrealloc(void *, size_t);
413 static void *xcalloc(size_t, size_t);
415 static void (*handler
[LASTEvent
])(XEvent
*) = {
417 [ClientMessage
] = cmessage
,
418 [ConfigureNotify
] = resize
,
419 [VisibilityNotify
] = visibility
,
420 [UnmapNotify
] = unmap
,
424 [MotionNotify
] = bmotion
,
425 [ButtonPress
] = bpress
,
426 [ButtonRelease
] = brelease
,
427 [SelectionClear
] = selclear
,
428 [SelectionNotify
] = selnotify
,
429 [SelectionRequest
] = selrequest
,
436 static CSIEscape csiescseq
;
437 static STREscape strescseq
;
440 static Selection sel
;
441 static int iofd
= -1;
442 static char **opt_cmd
= NULL
;
443 static char *opt_io
= NULL
;
444 static char *opt_title
= NULL
;
445 static char *opt_embed
= NULL
;
446 static char *opt_class
= NULL
;
447 static char *opt_font
= NULL
;
449 static char *usedfont
= NULL
;
450 static int usedfontsize
= 0;
452 /* Font Ring Cache */
467 * Fontcache is a ring buffer, with frccur as current position and frclen as
468 * the current length of used elements.
471 static Fontcache frc
[1024];
472 static int frccur
= -1, frclen
= 0;
475 xwrite(int fd
, char *s
, size_t len
) {
479 ssize_t r
= write(fd
, s
, len
);
489 xmalloc(size_t len
) {
490 void *p
= malloc(len
);
493 die("Out of memory\n");
499 xrealloc(void *p
, size_t len
) {
500 if((p
= realloc(p
, len
)) == NULL
)
501 die("Out of memory\n");
507 xcalloc(size_t nmemb
, size_t size
) {
508 void *p
= calloc(nmemb
, size
);
511 die("Out of memory\n");
517 utf8decode(char *s
, long *u
) {
523 if(~c
& B7
) { /* 0xxxxxxx */
526 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
527 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
529 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
530 *u
= c
&(B3
|B2
|B1
|B0
);
532 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
539 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
541 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
544 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
547 if((n
== 1 && *u
< 0x80) ||
548 (n
== 2 && *u
< 0x800) ||
549 (n
== 3 && *u
< 0x10000) ||
550 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
562 utf8encode(long *u
, char *s
) {
570 *sp
= uc
; /* 0xxxxxxx */
572 } else if(*u
< 0x800) {
573 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
575 } else if(uc
< 0x10000) {
576 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
578 } else if(uc
<= 0x10FFFF) {
579 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
585 for(i
=n
,++sp
; i
>0; --i
,++sp
)
586 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
598 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
599 UTF-8 otherwise return 0 */
601 isfullutf8(char *s
, int b
) {
609 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
611 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
613 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
615 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
617 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
618 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
631 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
633 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
642 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
643 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
647 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
648 if(sel
.xtarget
== None
)
649 sel
.xtarget
= XA_STRING
;
657 return LIMIT(x
, 0, term
.col
-1);
665 return LIMIT(y
, 0, term
.row
-1);
670 if(sel
.ob
.y
== sel
.oe
.y
) {
671 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
672 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
674 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
675 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
677 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
678 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
682 selected(int x
, int y
) {
683 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
684 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
686 if(sel
.type
== SEL_RECTANGULAR
) {
687 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
688 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
691 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
692 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
693 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
694 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
698 selsnap(int mode
, int *x
, int *y
, int direction
) {
704 * Snap around if the word wraps around at the end or
705 * beginning of a line.
708 if(direction
< 0 && *x
<= 0) {
709 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
717 if(direction
> 0 && *x
>= term
.col
-1) {
718 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
727 if(strchr(worddelimiters
,
728 term
.line
[*y
][*x
+ direction
].c
[0])) {
737 * Snap around if the the previous line or the current one
738 * has set ATTR_WRAP at its end. Then the whole next or
739 * previous line will be selected.
741 *x
= (direction
< 0) ? 0 : term
.col
- 1;
742 if(direction
< 0 && *y
> 0) {
743 for(; *y
> 0; *y
+= direction
) {
744 if(!(term
.line
[*y
-1][term
.col
-1].mode
749 } else if(direction
> 0 && *y
< term
.row
-1) {
750 for(; *y
< term
.row
; *y
+= direction
) {
751 if(!(term
.line
[*y
][term
.col
-1].mode
760 * Select the whole line when the end of line is reached.
764 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
774 getbuttoninfo(XEvent
*e
) {
776 uint state
= e
->xbutton
.state
&~Button1Mask
;
778 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
780 sel
.oe
.x
= x2col(e
->xbutton
.x
);
781 sel
.oe
.y
= y2row(e
->xbutton
.y
);
783 if(sel
.ob
.y
< sel
.oe
.y
784 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
785 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
786 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
788 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
789 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
793 sel
.type
= SEL_REGULAR
;
794 for(type
= 1; type
< LEN(selmasks
); ++type
) {
795 if(match(selmasks
[type
], state
)) {
803 mousereport(XEvent
*e
) {
804 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
805 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
808 static int ob
, ox
, oy
;
811 if(e
->xbutton
.type
== MotionNotify
) {
812 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
817 } else if(!IS_SET(MODE_MOUSESGR
)
818 && (e
->xbutton
.type
== ButtonRelease
819 || button
== AnyButton
)) {
825 if(e
->xbutton
.type
== ButtonPress
) {
832 button
+= (state
& ShiftMask
? 4 : 0)
833 + (state
& Mod4Mask
? 8 : 0)
834 + (state
& ControlMask
? 16 : 0);
837 if(IS_SET(MODE_MOUSESGR
)) {
838 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
840 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
841 } else if(x
< 223 && y
< 223) {
842 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
843 32+button
, 32+x
+1, 32+y
+1);
856 if(IS_SET(MODE_MOUSE
)) {
861 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
862 if(e
->xbutton
.button
== mk
->b
863 && match(mk
->mask
, e
->xbutton
.state
)) {
864 ttywrite(mk
->s
, strlen(mk
->s
));
865 if(IS_SET(MODE_ECHO
))
866 techo(mk
->s
, strlen(mk
->s
));
871 if(e
->xbutton
.button
== Button1
) {
872 gettimeofday(&now
, NULL
);
874 /* Clear previous selection, logically and visually. */
877 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
881 sel
.type
= SEL_REGULAR
;
882 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
883 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
886 * If the user clicks below predefined timeouts specific
887 * snapping behaviour is exposed.
889 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
890 sel
.snap
= SNAP_LINE
;
891 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
892 sel
.snap
= SNAP_WORD
;
896 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
897 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
901 * Draw selection, unless it's regular and we don't want to
902 * make clicks visible
906 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
909 sel
.tclick2
= sel
.tclick1
;
917 int x
, y
, bufsize
, size
, i
, ex
;
923 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
924 ptr
= str
= xmalloc(bufsize
);
926 /* append every set & selected glyph to the selection */
927 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
928 gp
= &term
.line
[y
][0];
929 last
= gp
+ term
.col
;
931 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
932 strcmp(last
->c
, " ") != 0))
935 for(x
= 0; gp
<= last
; x
++, ++gp
) {
939 size
= utf8size(gp
->c
);
940 memcpy(ptr
, gp
->c
, size
);
945 * Copy and pasting of line endings is inconsistent
946 * in the inconsistent terminal and GUI world.
947 * The best solution seems like to produce '\n' when
948 * something is copied from st and convert '\n' to
949 * '\r', when something to be pasted is received by
951 * FIXME: Fix the computer world.
953 if(y
< sel
.ne
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
957 * If the last selected line expands in the selection
958 * after the visible text '\n' is appended.
962 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
965 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
977 selnotify(XEvent
*e
) {
978 ulong nitems
, ofs
, rem
;
980 uchar
*data
, *last
, *repl
;
985 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
986 False
, AnyPropertyType
, &type
, &format
,
987 &nitems
, &rem
, &data
)) {
988 fprintf(stderr
, "Clipboard allocation failed\n");
993 * As seen in selcopy:
994 * Line endings are inconsistent in the terminal and GUI world
995 * copy and pasting. When receiving some selection data,
996 * replace all '\n' with '\r'.
997 * FIXME: Fix the computer world.
1000 last
= data
+ nitems
* format
/ 8;
1001 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1005 ttywrite((const char *)data
, nitems
* format
/ 8);
1007 /* number of 32-bit chunks returned */
1008 ofs
+= nitems
* format
/ 32;
1013 selpaste(const Arg
*dummy
) {
1014 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1015 xw
.win
, CurrentTime
);
1019 clippaste(const Arg
*dummy
) {
1022 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1023 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1024 xw
.win
, CurrentTime
);
1028 selclear(XEvent
*e
) {
1032 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1036 selrequest(XEvent
*e
) {
1037 XSelectionRequestEvent
*xsre
;
1038 XSelectionEvent xev
;
1039 Atom xa_targets
, string
;
1041 xsre
= (XSelectionRequestEvent
*) e
;
1042 xev
.type
= SelectionNotify
;
1043 xev
.requestor
= xsre
->requestor
;
1044 xev
.selection
= xsre
->selection
;
1045 xev
.target
= xsre
->target
;
1046 xev
.time
= xsre
->time
;
1048 xev
.property
= None
;
1050 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1051 if(xsre
->target
== xa_targets
) {
1052 /* respond with the supported type */
1053 string
= sel
.xtarget
;
1054 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1055 XA_ATOM
, 32, PropModeReplace
,
1056 (uchar
*) &string
, 1);
1057 xev
.property
= xsre
->property
;
1058 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1059 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1060 xsre
->target
, 8, PropModeReplace
,
1061 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1062 xev
.property
= xsre
->property
;
1065 /* all done, send a notification to the listener */
1066 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1067 fprintf(stderr
, "Error sending SelectionNotify event\n");
1071 xsetsel(char *str
) {
1072 /* register the selection for both the clipboard and the primary */
1078 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1080 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1081 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1085 brelease(XEvent
*e
) {
1086 if(IS_SET(MODE_MOUSE
)) {
1091 if(e
->xbutton
.button
== Button2
) {
1093 } else if(e
->xbutton
.button
== Button1
) {
1101 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1106 bmotion(XEvent
*e
) {
1107 int oldey
, oldex
, oldsby
, oldsey
;
1109 if(IS_SET(MODE_MOUSE
)) {
1124 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1125 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1129 die(const char *errstr
, ...) {
1132 va_start(ap
, errstr
);
1133 vfprintf(stderr
, errstr
, ap
);
1141 char *envshell
= getenv("SHELL");
1142 const struct passwd
*pass
= getpwuid(getuid());
1143 char buf
[sizeof(long) * 8 + 1];
1145 unsetenv("COLUMNS");
1147 unsetenv("TERMCAP");
1150 setenv("LOGNAME", pass
->pw_name
, 1);
1151 setenv("USER", pass
->pw_name
, 1);
1152 setenv("SHELL", pass
->pw_shell
, 0);
1153 setenv("HOME", pass
->pw_dir
, 0);
1156 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1157 setenv("WINDOWID", buf
, 1);
1159 signal(SIGCHLD
, SIG_DFL
);
1160 signal(SIGHUP
, SIG_DFL
);
1161 signal(SIGINT
, SIG_DFL
);
1162 signal(SIGQUIT
, SIG_DFL
);
1163 signal(SIGTERM
, SIG_DFL
);
1164 signal(SIGALRM
, SIG_DFL
);
1166 DEFAULT(envshell
, shell
);
1167 setenv("TERM", termname
, 1);
1168 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1169 execvp(args
[0], args
);
1177 if(waitpid(pid
, &stat
, 0) < 0)
1178 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1180 if(WIFEXITED(stat
)) {
1181 exit(WEXITSTATUS(stat
));
1190 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1192 /* seems to work fine on linux, openbsd and freebsd */
1193 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1194 die("openpty failed: %s\n", SERRNO
);
1196 switch(pid
= fork()) {
1198 die("fork failed\n");
1201 setsid(); /* create a new process group */
1202 dup2(s
, STDIN_FILENO
);
1203 dup2(s
, STDOUT_FILENO
);
1204 dup2(s
, STDERR_FILENO
);
1205 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1206 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1214 signal(SIGCHLD
, sigchld
);
1216 iofd
= (!strcmp(opt_io
, "-")) ?
1218 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1220 fprintf(stderr
, "Error opening %s:%s\n",
1221 opt_io
, strerror(errno
));
1231 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1233 fprintf(stderr
, "\n");
1238 static char buf
[BUFSIZ
];
1239 static int buflen
= 0;
1242 int charsize
; /* size of utf8 char in bytes */
1246 /* append read bytes to unprocessed bytes */
1247 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1248 die("Couldn't read from shell: %s\n", SERRNO
);
1250 /* process every complete utf8 char */
1253 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1254 charsize
= utf8decode(ptr
, &utf8c
);
1255 utf8encode(&utf8c
, s
);
1261 /* keep any uncomplete utf8 char for the next call */
1262 memmove(buf
, ptr
, buflen
);
1266 ttywrite(const char *s
, size_t n
) {
1267 if(write(cmdfd
, s
, n
) == -1)
1268 die("write error on tty: %s\n", SERRNO
);
1275 w
.ws_row
= term
.row
;
1276 w
.ws_col
= term
.col
;
1277 w
.ws_xpixel
= xw
.tw
;
1278 w
.ws_ypixel
= xw
.th
;
1279 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1280 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1284 tattrset(int attr
) {
1287 for(i
= 0; i
< term
.row
-1; i
++) {
1288 for(j
= 0; j
< term
.col
-1; j
++) {
1289 if(term
.line
[i
][j
].mode
& attr
)
1298 tsetdirt(int top
, int bot
) {
1301 LIMIT(top
, 0, term
.row
-1);
1302 LIMIT(bot
, 0, term
.row
-1);
1304 for(i
= top
; i
<= bot
; i
++)
1309 tsetdirtattr(int attr
) {
1312 for(i
= 0; i
< term
.row
-1; i
++) {
1313 for(j
= 0; j
< term
.col
-1; j
++) {
1314 if(term
.line
[i
][j
].mode
& attr
) {
1324 tsetdirt(0, term
.row
-1);
1331 if(mode
== CURSOR_SAVE
) {
1333 } else if(mode
== CURSOR_LOAD
) {
1343 term
.c
= (TCursor
){{
1347 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1349 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1350 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1353 term
.bot
= term
.row
- 1;
1354 term
.mode
= MODE_WRAP
;
1356 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1358 tcursor(CURSOR_SAVE
);
1362 tnew(int col
, int row
) {
1363 memset(&term
, 0, sizeof(Term
));
1372 Line
*tmp
= term
.line
;
1374 term
.line
= term
.alt
;
1376 term
.mode
^= MODE_ALTSCREEN
;
1381 tscrolldown(int orig
, int n
) {
1385 LIMIT(n
, 0, term
.bot
-orig
+1);
1387 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1389 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1390 temp
= term
.line
[i
];
1391 term
.line
[i
] = term
.line
[i
-n
];
1392 term
.line
[i
-n
] = temp
;
1395 term
.dirty
[i
-n
] = 1;
1402 tscrollup(int orig
, int n
) {
1405 LIMIT(n
, 0, term
.bot
-orig
+1);
1407 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1409 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1410 temp
= term
.line
[i
];
1411 term
.line
[i
] = term
.line
[i
+n
];
1412 term
.line
[i
+n
] = temp
;
1415 term
.dirty
[i
+n
] = 1;
1418 selscroll(orig
, -n
);
1422 selscroll(int orig
, int n
) {
1426 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1427 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1431 if(sel
.type
== SEL_RECTANGULAR
) {
1432 if(sel
.ob
.y
< term
.top
)
1433 sel
.ob
.y
= term
.top
;
1434 if(sel
.oe
.y
> term
.bot
)
1435 sel
.oe
.y
= term
.bot
;
1437 if(sel
.ob
.y
< term
.top
) {
1438 sel
.ob
.y
= term
.top
;
1441 if(sel
.oe
.y
> term
.bot
) {
1442 sel
.oe
.y
= term
.bot
;
1443 sel
.oe
.x
= term
.col
;
1451 tnewline(int first_col
) {
1455 tscrollup(term
.top
, 1);
1459 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1464 char *p
= csiescseq
.buf
, *np
;
1473 csiescseq
.buf
[csiescseq
.len
] = '\0';
1474 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1476 v
= strtol(p
, &np
, 10);
1479 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1481 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1483 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1487 csiescseq
.mode
= *p
;
1490 /* for absolute user moves, when decom is set */
1492 tmoveato(int x
, int y
) {
1493 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1497 tmoveto(int x
, int y
) {
1500 if(term
.c
.state
& CURSOR_ORIGIN
) {
1505 maxy
= term
.row
- 1;
1507 LIMIT(x
, 0, term
.col
-1);
1508 LIMIT(y
, miny
, maxy
);
1509 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1515 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1516 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1517 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1518 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1519 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1520 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1521 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1522 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1523 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1524 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1528 * The table is proudly stolen from rxvt.
1530 if(attr
->mode
& ATTR_GFX
) {
1531 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1532 && vt100_0
[c
[0] - 0x41]) {
1533 c
= vt100_0
[c
[0] - 0x41];
1538 term
.line
[y
][x
] = *attr
;
1539 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1543 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1547 temp
= x1
, x1
= x2
, x2
= temp
;
1549 temp
= y1
, y1
= y2
, y2
= temp
;
1551 LIMIT(x1
, 0, term
.col
-1);
1552 LIMIT(x2
, 0, term
.col
-1);
1553 LIMIT(y1
, 0, term
.row
-1);
1554 LIMIT(y2
, 0, term
.row
-1);
1556 for(y
= y1
; y
<= y2
; y
++) {
1558 for(x
= x1
; x
<= x2
; x
++) {
1561 term
.line
[y
][x
] = term
.c
.attr
;
1562 memcpy(term
.line
[y
][x
].c
, " ", 2);
1568 tdeletechar(int n
) {
1569 int src
= term
.c
.x
+ n
;
1571 int size
= term
.col
- src
;
1573 term
.dirty
[term
.c
.y
] = 1;
1575 if(src
>= term
.col
) {
1576 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1580 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1581 size
* sizeof(Glyph
));
1582 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1586 tinsertblank(int n
) {
1589 int size
= term
.col
- dst
;
1591 term
.dirty
[term
.c
.y
] = 1;
1593 if(dst
>= term
.col
) {
1594 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1598 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1599 size
* sizeof(Glyph
));
1600 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1604 tinsertblankline(int n
) {
1605 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1608 tscrolldown(term
.c
.y
, n
);
1612 tdeleteline(int n
) {
1613 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1616 tscrollup(term
.c
.y
, n
);
1620 tsetattr(int *attr
, int l
) {
1623 for(i
= 0; i
< l
; i
++) {
1626 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1627 | ATTR_BOLD
| ATTR_ITALIC \
1629 term
.c
.attr
.fg
= defaultfg
;
1630 term
.c
.attr
.bg
= defaultbg
;
1633 term
.c
.attr
.mode
|= ATTR_BOLD
;
1636 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1639 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1641 case 5: /* slow blink */
1642 case 6: /* rapid blink */
1643 term
.c
.attr
.mode
|= ATTR_BLINK
;
1646 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1650 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1653 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1656 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1660 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1663 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1666 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1668 if(BETWEEN(attr
[i
], 0, 255)) {
1669 term
.c
.attr
.fg
= attr
[i
];
1672 "erresc: bad fgcolor %d\n",
1677 "erresc(38): gfx attr %d unknown\n",
1682 term
.c
.attr
.fg
= defaultfg
;
1685 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1687 if(BETWEEN(attr
[i
], 0, 255)) {
1688 term
.c
.attr
.bg
= attr
[i
];
1691 "erresc: bad bgcolor %d\n",
1696 "erresc(48): gfx attr %d unknown\n",
1701 term
.c
.attr
.bg
= defaultbg
;
1704 if(BETWEEN(attr
[i
], 30, 37)) {
1705 term
.c
.attr
.fg
= attr
[i
] - 30;
1706 } else if(BETWEEN(attr
[i
], 40, 47)) {
1707 term
.c
.attr
.bg
= attr
[i
] - 40;
1708 } else if(BETWEEN(attr
[i
], 90, 97)) {
1709 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1710 } else if(BETWEEN(attr
[i
], 100, 107)) {
1711 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1714 "erresc(default): gfx attr %d unknown\n",
1715 attr
[i
]), csidump();
1723 tsetscroll(int t
, int b
) {
1726 LIMIT(t
, 0, term
.row
-1);
1727 LIMIT(b
, 0, term
.row
-1);
1737 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1740 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1744 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1748 case 1: /* DECCKM -- Cursor key */
1749 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1751 case 5: /* DECSCNM -- Reverse video */
1753 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1754 if(mode
!= term
.mode
)
1755 redraw(REDRAW_TIMEOUT
);
1757 case 6: /* DECOM -- Origin */
1758 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1761 case 7: /* DECAWM -- Auto wrap */
1762 MODBIT(term
.mode
, set
, MODE_WRAP
);
1764 case 0: /* Error (IGNORED) */
1765 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1766 case 3: /* DECCOLM -- Column (IGNORED) */
1767 case 4: /* DECSCLM -- Scroll (IGNORED) */
1768 case 8: /* DECARM -- Auto repeat (IGNORED) */
1769 case 18: /* DECPFF -- Printer feed (IGNORED) */
1770 case 19: /* DECPEX -- Printer extent (IGNORED) */
1771 case 42: /* DECNRCM -- National characters (IGNORED) */
1772 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1774 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1775 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1777 case 1000: /* 1000,1002: enable xterm mouse report */
1778 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1779 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1782 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1783 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1786 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1789 MODBIT(term
.mode
, set
, MODE_8BIT
);
1791 case 1049: /* = 1047 and 1048 */
1794 if (!allowaltscreen
)
1797 alt
= IS_SET(MODE_ALTSCREEN
);
1799 tclearregion(0, 0, term
.col
-1,
1802 if(set
^ alt
) /* set is always 1 or 0 */
1808 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1812 "erresc: unknown private set/reset mode %d\n",
1818 case 0: /* Error (IGNORED) */
1820 case 2: /* KAM -- keyboard action */
1821 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1823 case 4: /* IRM -- Insertion-replacement */
1824 MODBIT(term
.mode
, set
, MODE_INSERT
);
1826 case 12: /* SRM -- Send/Receive */
1827 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1829 case 20: /* LNM -- Linefeed/new line */
1830 MODBIT(term
.mode
, set
, MODE_CRLF
);
1834 "erresc: unknown set/reset mode %d\n",
1846 switch(csiescseq
.mode
) {
1849 fprintf(stderr
, "erresc: unknown csi ");
1853 case '@': /* ICH -- Insert <n> blank char */
1854 DEFAULT(csiescseq
.arg
[0], 1);
1855 tinsertblank(csiescseq
.arg
[0]);
1857 case 'A': /* CUU -- Cursor <n> Up */
1858 DEFAULT(csiescseq
.arg
[0], 1);
1859 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1861 case 'B': /* CUD -- Cursor <n> Down */
1862 case 'e': /* VPR --Cursor <n> Down */
1863 DEFAULT(csiescseq
.arg
[0], 1);
1864 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1866 case 'c': /* DA -- Device Attributes */
1867 if(csiescseq
.arg
[0] == 0)
1868 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1870 case 'C': /* CUF -- Cursor <n> Forward */
1871 case 'a': /* HPR -- Cursor <n> Forward */
1872 DEFAULT(csiescseq
.arg
[0], 1);
1873 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1875 case 'D': /* CUB -- Cursor <n> Backward */
1876 DEFAULT(csiescseq
.arg
[0], 1);
1877 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1879 case 'E': /* CNL -- Cursor <n> Down and first col */
1880 DEFAULT(csiescseq
.arg
[0], 1);
1881 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1883 case 'F': /* CPL -- Cursor <n> Up and first col */
1884 DEFAULT(csiescseq
.arg
[0], 1);
1885 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1887 case 'g': /* TBC -- Tabulation clear */
1888 switch(csiescseq
.arg
[0]) {
1889 case 0: /* clear current tab stop */
1890 term
.tabs
[term
.c
.x
] = 0;
1892 case 3: /* clear all the tabs */
1893 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1899 case 'G': /* CHA -- Move to <col> */
1901 DEFAULT(csiescseq
.arg
[0], 1);
1902 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1904 case 'H': /* CUP -- Move to <row> <col> */
1906 DEFAULT(csiescseq
.arg
[0], 1);
1907 DEFAULT(csiescseq
.arg
[1], 1);
1908 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1910 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1911 DEFAULT(csiescseq
.arg
[0], 1);
1912 while(csiescseq
.arg
[0]--)
1915 case 'J': /* ED -- Clear screen */
1917 switch(csiescseq
.arg
[0]) {
1919 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1920 if(term
.c
.y
< term
.row
-1) {
1921 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1927 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1928 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1931 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1937 case 'K': /* EL -- Clear line */
1938 switch(csiescseq
.arg
[0]) {
1940 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1944 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1947 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1951 case 'S': /* SU -- Scroll <n> line up */
1952 DEFAULT(csiescseq
.arg
[0], 1);
1953 tscrollup(term
.top
, csiescseq
.arg
[0]);
1955 case 'T': /* SD -- Scroll <n> line down */
1956 DEFAULT(csiescseq
.arg
[0], 1);
1957 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1959 case 'L': /* IL -- Insert <n> blank lines */
1960 DEFAULT(csiescseq
.arg
[0], 1);
1961 tinsertblankline(csiescseq
.arg
[0]);
1963 case 'l': /* RM -- Reset Mode */
1964 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1966 case 'M': /* DL -- Delete <n> lines */
1967 DEFAULT(csiescseq
.arg
[0], 1);
1968 tdeleteline(csiescseq
.arg
[0]);
1970 case 'X': /* ECH -- Erase <n> char */
1971 DEFAULT(csiescseq
.arg
[0], 1);
1972 tclearregion(term
.c
.x
, term
.c
.y
,
1973 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1975 case 'P': /* DCH -- Delete <n> char */
1976 DEFAULT(csiescseq
.arg
[0], 1);
1977 tdeletechar(csiescseq
.arg
[0]);
1979 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1980 DEFAULT(csiescseq
.arg
[0], 1);
1981 while(csiescseq
.arg
[0]--)
1984 case 'd': /* VPA -- Move to <row> */
1985 DEFAULT(csiescseq
.arg
[0], 1);
1986 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1988 case 'h': /* SM -- Set terminal mode */
1989 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1991 case 'm': /* SGR -- Terminal attribute (color) */
1992 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1994 case 'r': /* DECSTBM -- Set Scrolling Region */
1995 if(csiescseq
.priv
) {
1998 DEFAULT(csiescseq
.arg
[0], 1);
1999 DEFAULT(csiescseq
.arg
[1], term
.row
);
2000 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2004 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2005 tcursor(CURSOR_SAVE
);
2007 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2008 tcursor(CURSOR_LOAD
);
2019 for(i
= 0; i
< csiescseq
.len
; i
++) {
2020 c
= csiescseq
.buf
[i
] & 0xff;
2023 } else if(c
== '\n') {
2025 } else if(c
== '\r') {
2027 } else if(c
== 0x1b) {
2030 printf("(%02x)", c
);
2038 memset(&csiescseq
, 0, sizeof(csiescseq
));
2047 narg
= strescseq
.narg
;
2049 switch(strescseq
.type
) {
2050 case ']': /* OSC -- Operating System Command */
2051 switch(i
= atoi(strescseq
.args
[0])) {
2056 xsettitle(strescseq
.args
[1]);
2058 case 4: /* color set */
2061 p
= strescseq
.args
[2];
2063 case 104: /* color reset, here p = NULL */
2064 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2065 if (!xsetcolorname(j
, p
)) {
2066 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2069 * TODO if defaultbg color is changed, borders
2076 fprintf(stderr
, "erresc: unknown str ");
2081 case 'k': /* old title set compatibility */
2082 xsettitle(strescseq
.args
[0]);
2084 case 'P': /* DSC -- Device Control String */
2085 case '_': /* APC -- Application Program Command */
2086 case '^': /* PM -- Privacy Message */
2088 fprintf(stderr
, "erresc: unknown str ");
2097 char *p
= strescseq
.buf
;
2100 strescseq
.buf
[strescseq
.len
] = '\0';
2101 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2102 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2110 printf("ESC%c", strescseq
.type
);
2111 for(i
= 0; i
< strescseq
.len
; i
++) {
2112 c
= strescseq
.buf
[i
] & 0xff;
2115 } else if(isprint(c
)) {
2117 } else if(c
== '\n') {
2119 } else if(c
== '\r') {
2121 } else if(c
== 0x1b) {
2124 printf("(%02x)", c
);
2132 memset(&strescseq
, 0, sizeof(strescseq
));
2136 tputtab(bool forward
) {
2142 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2147 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2150 tmoveto(x
, term
.c
.y
);
2154 techo(char *buf
, int len
) {
2155 for(; len
> 0; buf
++, len
--) {
2158 if(c
== '\033') { /* escape */
2161 } else if(c
< '\x20') { /* control code */
2162 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2176 tputc(char *c
, int len
) {
2178 bool control
= ascii
< '\x20' || ascii
== 0177;
2181 if(xwrite(iofd
, c
, len
) < 0) {
2182 fprintf(stderr
, "Error writing in %s:%s\n",
2183 opt_io
, strerror(errno
));
2190 * STR sequences must be checked before anything else
2191 * because it can use some control codes as part of the sequence.
2193 if(term
.esc
& ESC_STR
) {
2196 term
.esc
= ESC_START
| ESC_STR_END
;
2198 case '\a': /* backwards compatibility to xterm */
2203 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2204 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2205 strescseq
.len
+= len
;
2208 * Here is a bug in terminals. If the user never sends
2209 * some code to stop the str or esc command, then st
2210 * will stop responding. But this is better than
2211 * silently failing with unknown characters. At least
2212 * then users will report back.
2214 * In the case users ever get fixed, here is the code:
2226 * Actions of control codes must be performed as soon they arrive
2227 * because they can be embedded inside a control sequence, and
2228 * they must not cause conflicts with sequences.
2236 tmoveto(term
.c
.x
-1, term
.c
.y
);
2239 tmoveto(0, term
.c
.y
);
2244 /* go to first col if the mode is set */
2245 tnewline(IS_SET(MODE_CRLF
));
2247 case '\a': /* BEL */
2248 if(!(xw
.state
& WIN_FOCUSED
))
2251 case '\033': /* ESC */
2253 term
.esc
= ESC_START
;
2255 case '\016': /* SO */
2256 case '\017': /* SI */
2258 * Different charsets are hard to handle. Applications
2259 * should use the right alt charset escapes for the
2260 * only reason they still exist: line drawing. The
2261 * rest is incompatible history st should not support.
2264 case '\032': /* SUB */
2265 case '\030': /* CAN */
2268 case '\005': /* ENQ (IGNORED) */
2269 case '\000': /* NUL (IGNORED) */
2270 case '\021': /* XON (IGNORED) */
2271 case '\023': /* XOFF (IGNORED) */
2272 case 0177: /* DEL (IGNORED) */
2275 } else if(term
.esc
& ESC_START
) {
2276 if(term
.esc
& ESC_CSI
) {
2277 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2278 if(BETWEEN(ascii
, 0x40, 0x7E)
2279 || csiescseq
.len
>= \
2280 sizeof(csiescseq
.buf
)-1) {
2285 } else if(term
.esc
& ESC_STR_END
) {
2289 } else if(term
.esc
& ESC_ALTCHARSET
) {
2291 case '0': /* Line drawing set */
2292 term
.c
.attr
.mode
|= ATTR_GFX
;
2294 case 'B': /* USASCII */
2295 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2297 case 'A': /* UK (IGNORED) */
2298 case '<': /* multinational charset (IGNORED) */
2299 case '5': /* Finnish (IGNORED) */
2300 case 'C': /* Finnish (IGNORED) */
2301 case 'K': /* German (IGNORED) */
2304 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2307 } else if(term
.esc
& ESC_TEST
) {
2308 if(ascii
== '8') { /* DEC screen alignment test. */
2309 char E
[UTF_SIZ
] = "E";
2312 for(x
= 0; x
< term
.col
; ++x
) {
2313 for(y
= 0; y
< term
.row
; ++y
)
2314 tsetchar(E
, &term
.c
.attr
, x
, y
);
2321 term
.esc
|= ESC_CSI
;
2324 term
.esc
|= ESC_TEST
;
2326 case 'P': /* DCS -- Device Control String */
2327 case '_': /* APC -- Application Program Command */
2328 case '^': /* PM -- Privacy Message */
2329 case ']': /* OSC -- Operating System Command */
2330 case 'k': /* old title set compatibility */
2332 strescseq
.type
= ascii
;
2333 term
.esc
|= ESC_STR
;
2335 case '(': /* set primary charset G0 */
2336 term
.esc
|= ESC_ALTCHARSET
;
2338 case ')': /* set secondary charset G1 (IGNORED) */
2339 case '*': /* set tertiary charset G2 (IGNORED) */
2340 case '+': /* set quaternary charset G3 (IGNORED) */
2343 case 'D': /* IND -- Linefeed */
2344 if(term
.c
.y
== term
.bot
) {
2345 tscrollup(term
.top
, 1);
2347 tmoveto(term
.c
.x
, term
.c
.y
+1);
2351 case 'E': /* NEL -- Next line */
2352 tnewline(1); /* always go to first col */
2355 case 'H': /* HTS -- Horizontal tab stop */
2356 term
.tabs
[term
.c
.x
] = 1;
2359 case 'M': /* RI -- Reverse index */
2360 if(term
.c
.y
== term
.top
) {
2361 tscrolldown(term
.top
, 1);
2363 tmoveto(term
.c
.x
, term
.c
.y
-1);
2367 case 'Z': /* DECID -- Identify Terminal */
2368 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2371 case 'c': /* RIS -- Reset to inital state */
2376 case '=': /* DECPAM -- Application keypad */
2377 term
.mode
|= MODE_APPKEYPAD
;
2380 case '>': /* DECPNM -- Normal keypad */
2381 term
.mode
&= ~MODE_APPKEYPAD
;
2384 case '7': /* DECSC -- Save Cursor */
2385 tcursor(CURSOR_SAVE
);
2388 case '8': /* DECRC -- Restore Cursor */
2389 tcursor(CURSOR_LOAD
);
2392 case '\\': /* ST -- Stop */
2396 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2397 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2402 * All characters which form part of a sequence are not
2408 * Display control codes only if we are in graphic mode
2410 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2412 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2414 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2415 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2419 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2420 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2421 &term
.line
[term
.c
.y
][term
.c
.x
],
2422 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2425 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2426 if(term
.c
.x
+1 < term
.col
) {
2427 tmoveto(term
.c
.x
+1, term
.c
.y
);
2429 term
.c
.state
|= CURSOR_WRAPNEXT
;
2434 tresize(int col
, int row
) {
2436 int minrow
= MIN(row
, term
.row
);
2437 int mincol
= MIN(col
, term
.col
);
2438 int slide
= term
.c
.y
- row
+ 1;
2442 if(col
< 1 || row
< 1)
2445 /* free unneeded rows */
2449 * slide screen to keep cursor where we expect it -
2450 * tscrollup would work here, but we can optimize to
2451 * memmove because we're freeing the earlier lines
2453 for(/* i = 0 */; i
< slide
; i
++) {
2457 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2458 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2460 for(i
+= row
; i
< term
.row
; i
++) {
2465 /* resize to new height */
2466 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2467 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2468 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2469 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2471 /* resize each row to new width, zero-pad if needed */
2472 for(i
= 0; i
< minrow
; i
++) {
2474 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2475 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2478 /* allocate any new rows */
2479 for(/* i == minrow */; i
< row
; i
++) {
2481 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2482 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2484 if(col
> term
.col
) {
2485 bp
= term
.tabs
+ term
.col
;
2487 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2488 while(--bp
> term
.tabs
&& !*bp
)
2490 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2493 /* update terminal size */
2496 /* reset scrolling region */
2497 tsetscroll(0, row
-1);
2498 /* make use of the LIMIT in tmoveto */
2499 tmoveto(term
.c
.x
, term
.c
.y
);
2500 /* Clearing both screens */
2503 if(mincol
< col
&& 0 < minrow
) {
2504 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2506 if(0 < col
&& minrow
< row
) {
2507 tclearregion(0, minrow
, col
- 1, row
- 1);
2510 } while(orig
!= term
.line
);
2516 xresize(int col
, int row
) {
2517 xw
.tw
= MAX(1, col
* xw
.cw
);
2518 xw
.th
= MAX(1, row
* xw
.ch
);
2520 XFreePixmap(xw
.dpy
, xw
.buf
);
2521 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2522 DefaultDepth(xw
.dpy
, xw
.scr
));
2523 XftDrawChange(xw
.draw
, xw
.buf
);
2524 xclear(0, 0, xw
.w
, xw
.h
);
2527 static inline ushort
2528 sixd_to_16bit(int x
) {
2529 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2535 XRenderColor color
= { .alpha
= 0xffff };
2537 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2538 for(i
= 0; i
< LEN(colorname
); i
++) {
2541 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2542 die("Could not allocate color '%s'\n", colorname
[i
]);
2546 /* load colors [16-255] ; same colors as xterm */
2547 for(i
= 16, r
= 0; r
< 6; r
++) {
2548 for(g
= 0; g
< 6; g
++) {
2549 for(b
= 0; b
< 6; b
++) {
2550 color
.red
= sixd_to_16bit(r
);
2551 color
.green
= sixd_to_16bit(g
);
2552 color
.blue
= sixd_to_16bit(b
);
2553 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2554 die("Could not allocate color %d\n", i
);
2561 for(r
= 0; r
< 24; r
++, i
++) {
2562 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2563 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2565 die("Could not allocate color %d\n", i
);
2571 xsetcolorname(int x
, const char *name
) {
2572 XRenderColor color
= { .alpha
= 0xffff };
2574 if (x
< 0 || x
> LEN(colorname
))
2577 if(16 <= x
&& x
< 16 + 216) {
2578 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2579 color
.red
= sixd_to_16bit(r
);
2580 color
.green
= sixd_to_16bit(g
);
2581 color
.blue
= sixd_to_16bit(b
);
2582 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2583 return 0; /* something went wrong */
2586 } else if (16 + 216 <= x
&& x
< 256) {
2587 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2588 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2589 return 0; /* something went wrong */
2593 name
= colorname
[x
];
2596 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2603 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2604 XftDrawRect(xw
.draw
,
2605 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2606 borderpx
+ col1
* xw
.cw
,
2607 borderpx
+ row1
* xw
.ch
,
2608 (col2
-col1
+1) * xw
.cw
,
2609 (row2
-row1
+1) * xw
.ch
);
2613 * Absolute coordinates.
2616 xclear(int x1
, int y1
, int x2
, int y2
) {
2617 XftDrawRect(xw
.draw
,
2618 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2619 x1
, y1
, x2
-x1
, y2
-y1
);
2624 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2625 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2626 XSizeHints
*sizeh
= NULL
;
2628 sizeh
= XAllocSizeHints();
2629 if(xw
.isfixed
== False
) {
2630 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2631 sizeh
->height
= xw
.h
;
2632 sizeh
->width
= xw
.w
;
2633 sizeh
->height_inc
= xw
.ch
;
2634 sizeh
->width_inc
= xw
.cw
;
2635 sizeh
->base_height
= 2 * borderpx
;
2636 sizeh
->base_width
= 2 * borderpx
;
2638 sizeh
->flags
= PMaxSize
| PMinSize
;
2639 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2640 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2643 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2648 xloadfont(Font
*f
, FcPattern
*pattern
) {
2652 match
= FcFontMatch(NULL
, pattern
, &result
);
2656 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2657 FcPatternDestroy(match
);
2662 f
->pattern
= FcPatternDuplicate(pattern
);
2664 f
->ascent
= f
->match
->ascent
;
2665 f
->descent
= f
->match
->descent
;
2667 f
->rbearing
= f
->match
->max_advance_width
;
2669 f
->height
= f
->ascent
+ f
->descent
;
2670 f
->width
= f
->lbearing
+ f
->rbearing
;
2676 xloadfonts(char *fontstr
, int fontsize
) {
2681 if(fontstr
[0] == '-') {
2682 pattern
= XftXlfdParse(fontstr
, False
, False
);
2684 pattern
= FcNameParse((FcChar8
*)fontstr
);
2688 die("st: can't open font %s\n", fontstr
);
2691 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2692 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2693 usedfontsize
= fontsize
;
2695 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2696 if(result
== FcResultMatch
) {
2697 usedfontsize
= (int)fontval
;
2700 * Default font size is 12, if none given. This is to
2701 * have a known usedfontsize value.
2703 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2708 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2709 FcDefaultSubstitute(pattern
);
2711 if(xloadfont(&dc
.font
, pattern
))
2712 die("st: can't open font %s\n", fontstr
);
2714 /* Setting character width and height. */
2715 xw
.cw
= dc
.font
.width
;
2716 xw
.ch
= dc
.font
.height
;
2718 FcPatternDel(pattern
, FC_SLANT
);
2719 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2720 if(xloadfont(&dc
.ifont
, pattern
))
2721 die("st: can't open font %s\n", fontstr
);
2723 FcPatternDel(pattern
, FC_WEIGHT
);
2724 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2725 if(xloadfont(&dc
.ibfont
, pattern
))
2726 die("st: can't open font %s\n", fontstr
);
2728 FcPatternDel(pattern
, FC_SLANT
);
2729 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2730 if(xloadfont(&dc
.bfont
, pattern
))
2731 die("st: can't open font %s\n", fontstr
);
2733 FcPatternDestroy(pattern
);
2737 xloadfontset(Font
*f
) {
2740 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
2746 xunloadfont(Font
*f
) {
2747 XftFontClose(xw
.dpy
, f
->match
);
2748 FcPatternDestroy(f
->pattern
);
2750 FcFontSetDestroy(f
->set
);
2754 xunloadfonts(void) {
2758 * Free the loaded fonts in the font cache. This is done backwards
2761 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2764 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2769 xunloadfont(&dc
.font
);
2770 xunloadfont(&dc
.bfont
);
2771 xunloadfont(&dc
.ifont
);
2772 xunloadfont(&dc
.ibfont
);
2776 xzoom(const Arg
*arg
) {
2778 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2785 XSetWindowAttributes attrs
;
2791 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2792 die("Can't open display\n");
2793 xw
.scr
= XDefaultScreen(xw
.dpy
);
2794 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2798 die("Could not init fontconfig.\n");
2800 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2801 xloadfonts(usedfont
, 0);
2804 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2807 /* adjust fixed window geometry */
2809 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2810 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2812 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2814 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2819 /* window - default size */
2820 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2821 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2827 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2828 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2829 attrs
.bit_gravity
= NorthWestGravity
;
2830 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2831 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2832 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2833 attrs
.colormap
= xw
.cmap
;
2835 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2836 XRootWindow(xw
.dpy
, xw
.scr
);
2837 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2838 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2840 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2844 memset(&gcvalues
, 0, sizeof(gcvalues
));
2845 gcvalues
.graphics_exposures
= False
;
2846 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2848 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2849 DefaultDepth(xw
.dpy
, xw
.scr
));
2850 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2851 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2853 /* Xft rendering context */
2854 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2857 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2858 XSetLocaleModifiers("@im=local");
2859 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2860 XSetLocaleModifiers("@im=");
2861 if((xw
.xim
= XOpenIM(xw
.dpy
,
2862 NULL
, NULL
, NULL
)) == NULL
) {
2863 die("XOpenIM failed. Could not open input"
2868 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2869 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2870 XNFocusWindow
, xw
.win
, NULL
);
2872 die("XCreateIC failed. Could not obtain input method.\n");
2874 /* white cursor, black outline */
2875 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2876 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2877 XRecolorCursor(xw
.dpy
, cursor
,
2878 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2879 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2881 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2882 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2883 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2886 XMapWindow(xw
.dpy
, xw
.win
);
2892 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2893 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2894 width
= charlen
* xw
.cw
, xp
, i
;
2896 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2899 Font
*font
= &dc
.font
;
2901 FcPattern
*fcpattern
, *fontpattern
;
2902 FcFontSet
*fcsets
[] = { NULL
};
2903 FcCharSet
*fccharset
;
2904 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2905 XRenderColor colfg
, colbg
;
2908 frcflags
= FRC_NORMAL
;
2910 if(base
.mode
& ATTR_ITALIC
) {
2911 if(base
.fg
== defaultfg
)
2912 base
.fg
= defaultitalic
;
2914 frcflags
= FRC_ITALIC
;
2915 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2916 if(base
.fg
== defaultfg
)
2917 base
.fg
= defaultitalic
;
2919 frcflags
= FRC_ITALICBOLD
;
2920 } else if(base
.mode
& ATTR_UNDERLINE
) {
2921 if(base
.fg
== defaultfg
)
2922 base
.fg
= defaultunderline
;
2924 fg
= &dc
.col
[base
.fg
];
2925 bg
= &dc
.col
[base
.bg
];
2927 if(base
.mode
& ATTR_BOLD
) {
2928 if(BETWEEN(base
.fg
, 0, 7)) {
2929 /* basic system colors */
2930 fg
= &dc
.col
[base
.fg
+ 8];
2931 } else if(BETWEEN(base
.fg
, 16, 195)) {
2933 fg
= &dc
.col
[base
.fg
+ 36];
2934 } else if(BETWEEN(base
.fg
, 232, 251)) {
2936 fg
= &dc
.col
[base
.fg
+ 4];
2939 * Those ranges will not be brightened:
2940 * 8 - 15 – bright system colors
2941 * 196 - 231 – highest 256 color cube
2942 * 252 - 255 – brightest colors in greyscale
2945 frcflags
= FRC_BOLD
;
2948 if(IS_SET(MODE_REVERSE
)) {
2949 if(fg
== &dc
.col
[defaultfg
]) {
2950 fg
= &dc
.col
[defaultbg
];
2952 colfg
.red
= ~fg
->color
.red
;
2953 colfg
.green
= ~fg
->color
.green
;
2954 colfg
.blue
= ~fg
->color
.blue
;
2955 colfg
.alpha
= fg
->color
.alpha
;
2956 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2960 if(bg
== &dc
.col
[defaultbg
]) {
2961 bg
= &dc
.col
[defaultfg
];
2963 colbg
.red
= ~bg
->color
.red
;
2964 colbg
.green
= ~bg
->color
.green
;
2965 colbg
.blue
= ~bg
->color
.blue
;
2966 colbg
.alpha
= bg
->color
.alpha
;
2967 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2972 if(base
.mode
& ATTR_REVERSE
) {
2978 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
2981 /* Intelligent cleaning up of the borders. */
2983 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2984 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2986 if(x
+ charlen
>= term
.col
) {
2987 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2988 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2991 xclear(winx
, 0, winx
+ width
, borderpx
);
2993 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2995 /* Clean up the region we want to draw to. */
2996 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2998 /* Set the clip region because Xft is sometimes dirty. */
3003 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3005 for(xp
= winx
; bytelen
> 0;) {
3007 * Search for the range in the to be printed string of glyphs
3008 * that are in the main font. Then print that range. If
3009 * some glyph is found that is not in the font, do the
3017 u8cblen
= utf8decode(s
, &u8char
);
3021 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
3022 if(!doesexist
|| bytelen
<= 0) {
3031 XftDrawStringUtf8(xw
.draw
, fg
,
3033 winy
+ font
->ascent
,
3036 xp
+= font
->width
* u8fl
;
3049 /* Search the font cache. */
3050 for(i
= 0; i
< frclen
; i
++, frp
--) {
3054 if(frc
[frp
].c
== u8char
3055 && frc
[frp
].flags
== frcflags
) {
3060 /* Nothing was found. */
3064 fcsets
[0] = font
->set
;
3067 * Nothing was found in the cache. Now use
3068 * some dozen of Fontconfig calls to get the
3069 * font for one single character.
3071 fcpattern
= FcPatternDuplicate(font
->pattern
);
3072 fccharset
= FcCharSetCreate();
3074 FcCharSetAddChar(fccharset
, u8char
);
3075 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3077 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3080 FcConfigSubstitute(0, fcpattern
,
3082 FcDefaultSubstitute(fcpattern
);
3084 fontpattern
= FcFontSetMatch(0, fcsets
,
3085 FcTrue
, fcpattern
, &fcres
);
3088 * Overwrite or create the new cache entry.
3092 if(frccur
>= LEN(frc
))
3094 if(frclen
> LEN(frc
)) {
3096 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
3099 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
3101 frc
[frccur
].c
= u8char
;
3102 frc
[frccur
].flags
= frcflags
;
3104 FcPatternDestroy(fcpattern
);
3105 FcCharSetDestroy(fccharset
);
3110 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
3111 xp
, winy
+ frc
[frp
].font
->ascent
,
3112 (FcChar8
*)u8c
, u8cblen
);
3118 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3119 winy + font->ascent, (FcChar8 *)s, bytelen);
3122 if(base
.mode
& ATTR_UNDERLINE
) {
3123 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3127 /* Reset clip to none. */
3128 XftDrawSetClip(xw
.draw
, 0);
3133 static int oldx
= 0, oldy
= 0;
3135 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3137 LIMIT(oldx
, 0, term
.col
-1);
3138 LIMIT(oldy
, 0, term
.row
-1);
3140 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3142 /* remove the old cursor */
3143 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3144 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3147 /* draw the new one */
3148 if(!(IS_SET(MODE_HIDE
))) {
3149 if(xw
.state
& WIN_FOCUSED
) {
3150 if(IS_SET(MODE_REVERSE
)) {
3151 g
.mode
|= ATTR_REVERSE
;
3157 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3159 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3160 borderpx
+ term
.c
.x
* xw
.cw
,
3161 borderpx
+ term
.c
.y
* xw
.ch
,
3163 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3164 borderpx
+ term
.c
.x
* xw
.cw
,
3165 borderpx
+ term
.c
.y
* xw
.ch
,
3167 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3168 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3169 borderpx
+ term
.c
.y
* xw
.ch
,
3171 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3172 borderpx
+ term
.c
.x
* xw
.cw
,
3173 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3176 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3182 xsettitle(char *p
) {
3185 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3187 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3192 xsettitle(opt_title
? opt_title
: "st");
3196 redraw(int timeout
) {
3197 struct timespec tv
= {0, timeout
* 1000};
3203 nanosleep(&tv
, NULL
);
3204 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3210 drawregion(0, 0, term
.col
, term
.row
);
3211 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3213 XSetForeground(xw
.dpy
, dc
.gc
,
3214 dc
.col
[IS_SET(MODE_REVERSE
)?
3215 defaultfg
: defaultbg
].pixel
);
3219 drawregion(int x1
, int y1
, int x2
, int y2
) {
3220 int ic
, ib
, x
, y
, ox
, sl
;
3222 char buf
[DRAW_BUF_SIZ
];
3223 bool ena_sel
= sel
.ob
.x
!= -1;
3225 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3228 if(!(xw
.state
& WIN_VISIBLE
))
3231 for(y
= y1
; y
< y2
; y
++) {
3235 xtermclear(0, y
, term
.col
, y
);
3237 base
= term
.line
[y
][0];
3239 for(x
= x1
; x
< x2
; x
++) {
3240 new = term
.line
[y
][x
];
3241 if(ena_sel
&& selected(x
, y
))
3242 new.mode
^= ATTR_REVERSE
;
3243 if(ib
> 0 && (ATTRCMP(base
, new)
3244 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3245 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3253 sl
= utf8size(new.c
);
3254 memcpy(buf
+ib
, new.c
, sl
);
3259 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3265 expose(XEvent
*ev
) {
3266 XExposeEvent
*e
= &ev
->xexpose
;
3268 if(xw
.state
& WIN_REDRAW
) {
3270 xw
.state
&= ~WIN_REDRAW
;
3276 visibility(XEvent
*ev
) {
3277 XVisibilityEvent
*e
= &ev
->xvisibility
;
3279 if(e
->state
== VisibilityFullyObscured
) {
3280 xw
.state
&= ~WIN_VISIBLE
;
3281 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3282 /* need a full redraw for next Expose, not just a buf copy */
3283 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3289 xw
.state
&= ~WIN_VISIBLE
;
3293 xseturgency(int add
) {
3294 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3296 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3297 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3303 XFocusChangeEvent
*e
= &ev
->xfocus
;
3305 if(e
->mode
== NotifyGrab
)
3308 if(ev
->type
== FocusIn
) {
3309 XSetICFocus(xw
.xic
);
3310 xw
.state
|= WIN_FOCUSED
;
3313 XUnsetICFocus(xw
.xic
);
3314 xw
.state
&= ~WIN_FOCUSED
;
3319 match(uint mask
, uint state
) {
3320 state
&= ~(ignoremod
);
3322 if(mask
== XK_NO_MOD
&& state
)
3324 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3326 if((state
& mask
) != state
)
3332 numlock(const Arg
*dummy
) {
3337 kmap(KeySym k
, uint state
) {
3342 /* Check for mapped keys out of X11 function keys. */
3343 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3344 if(mappedkeys
[i
] == k
)
3347 if(i
== LEN(mappedkeys
)) {
3348 if((k
& 0xFFFF) < 0xFD00)
3352 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3358 if(!match(mask
, state
))
3361 if(kp
->appkey
> 0) {
3362 if(!IS_SET(MODE_APPKEYPAD
))
3364 if(term
.numlock
&& kp
->appkey
== 2)
3366 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3370 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3372 && !IS_SET(MODE_APPCURSOR
))) {
3376 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3377 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3388 kpress(XEvent
*ev
) {
3389 XKeyEvent
*e
= &ev
->xkey
;
3391 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3397 if(IS_SET(MODE_KBDLOCK
))
3400 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3401 e
->state
&= ~Mod2Mask
;
3403 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3404 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3405 bp
->func(&(bp
->arg
));
3410 /* 2. custom keys from config.h */
3411 if((customkey
= kmap(ksym
, e
->state
))) {
3412 len
= strlen(customkey
);
3413 memcpy(buf
, customkey
, len
);
3414 /* 3. hardcoded (overrides X lookup) */
3419 if(len
== 1 && e
->state
& Mod1Mask
) {
3420 if(IS_SET(MODE_8BIT
)) {
3423 ret
= utf8encode(&c
, cp
);
3432 memcpy(cp
, xstr
, len
);
3433 len
= cp
- buf
+ len
;
3437 if(IS_SET(MODE_ECHO
))
3443 cmessage(XEvent
*e
) {
3446 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3448 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3449 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3450 xw
.state
|= WIN_FOCUSED
;
3452 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3453 xw
.state
&= ~WIN_FOCUSED
;
3455 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3456 /* Send SIGHUP to shell */
3463 cresize(int width
, int height
) {
3471 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3472 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3481 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3484 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3491 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3492 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3494 gettimeofday(&lastblink
, NULL
);
3495 gettimeofday(&last
, NULL
);
3497 for(xev
= actionfps
;;) {
3499 FD_SET(cmdfd
, &rfd
);
3502 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3505 die("select failed: %s\n", SERRNO
);
3507 if(FD_ISSET(cmdfd
, &rfd
)) {
3510 blinkset
= tattrset(ATTR_BLINK
);
3511 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3512 term
.mode
&= ~(MODE_BLINK
);
3516 if(FD_ISSET(xfd
, &rfd
))
3519 gettimeofday(&now
, NULL
);
3520 drawtimeout
.tv_sec
= 0;
3521 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3525 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3526 tsetdirtattr(ATTR_BLINK
);
3527 term
.mode
^= MODE_BLINK
;
3528 gettimeofday(&lastblink
, NULL
);
3531 if(TIMEDIFF(now
, last
) \
3532 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3538 while(XPending(xw
.dpy
)) {
3539 XNextEvent(xw
.dpy
, &ev
);
3540 if(XFilterEvent(&ev
, None
))
3542 if(handler
[ev
.type
])
3543 (handler
[ev
.type
])(&ev
);
3549 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3551 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3553 if(TIMEDIFF(now
, lastblink
) \
3555 drawtimeout
.tv_usec
= 1;
3557 drawtimeout
.tv_usec
= (1000 * \
3572 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3573 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3574 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3578 main(int argc
, char *argv
[]) {
3582 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3587 allowaltscreen
= false;
3590 opt_class
= EARGF(usage());
3593 /* eat all remaining arguments */
3598 opt_font
= EARGF(usage());
3601 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3606 if(bitm
& WidthValue
)
3608 if(bitm
& HeightValue
)
3610 if(bitm
& XNegative
&& xw
.fx
== 0)
3612 if(bitm
& XNegative
&& xw
.fy
== 0)
3615 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3619 opt_io
= EARGF(usage());
3622 opt_title
= EARGF(usage());
3625 opt_embed
= EARGF(usage());
3633 setlocale(LC_CTYPE
, "");
3634 XSetLocaleModifiers("");
3640 cresize(xw
.h
, xw
.w
);