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 */
235 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
236 signed char appkey
; /* application keypad */
237 signed char appcursor
; /* application cursor */
238 signed char crlf
; /* crlf mode */
241 /* TODO: use better name for vars... */
254 struct timeval tclick1
;
255 struct timeval tclick2
;
268 void (*func
)(const Arg
*);
272 /* function definitions used in config.h */
273 static void clippaste(const Arg
*);
274 static void numlock(const Arg
*);
275 static void selpaste(const Arg
*);
276 static void xzoom(const Arg
*);
278 /* Config.h for applying patches and the configuration. */
294 /* Drawing Context */
296 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
297 Font font
, bfont
, ifont
, ibfont
;
301 static void die(const char *, ...);
302 static void draw(void);
303 static void redraw(int);
304 static void drawregion(int, int, int, int);
305 static void execsh(void);
306 static void sigchld(int);
307 static void run(void);
309 static void csidump(void);
310 static void csihandle(void);
311 static void csiparse(void);
312 static void csireset(void);
313 static void strdump(void);
314 static void strhandle(void);
315 static void strparse(void);
316 static void strreset(void);
318 static int tattrset(int);
319 static void tclearregion(int, int, int, int);
320 static void tcursor(int);
321 static void tdeletechar(int);
322 static void tdeleteline(int);
323 static void tinsertblank(int);
324 static void tinsertblankline(int);
325 static void tmoveto(int, int);
326 static void tmoveato(int x
, int y
);
327 static void tnew(int, int);
328 static void tnewline(int);
329 static void tputtab(bool);
330 static void tputc(char *, int);
331 static void treset(void);
332 static int tresize(int, int);
333 static void tscrollup(int, int);
334 static void tscrolldown(int, int);
335 static void tsetattr(int*, int);
336 static void tsetchar(char *, Glyph
*, int, int);
337 static void tsetscroll(int, int);
338 static void tswapscreen(void);
339 static void tsetdirt(int, int);
340 static void tsetdirtattr(int);
341 static void tsetmode(bool, bool, int *, int);
342 static void tfulldirt(void);
343 static void techo(char *, int);
345 static inline bool match(uint
, uint
);
346 static void ttynew(void);
347 static void ttyread(void);
348 static void ttyresize(void);
349 static void ttywrite(const char *, size_t);
351 static void xdraws(char *, Glyph
, int, int, int, int);
352 static void xhints(void);
353 static void xclear(int, int, int, int);
354 static void xdrawcursor(void);
355 static void xinit(void);
356 static void xloadcols(void);
357 static int xsetcolorname(int, const char *);
358 static int xloadfont(Font
*, FcPattern
*);
359 static void xloadfonts(char *, int);
360 static void xsettitle(char *);
361 static void xresettitle(void);
362 static void xseturgency(int);
363 static void xsetsel(char*);
364 static void xtermclear(int, int, int, int);
365 static void xunloadfonts(void);
366 static void xresize(int, int);
368 static void expose(XEvent
*);
369 static void visibility(XEvent
*);
370 static void unmap(XEvent
*);
371 static char *kmap(KeySym
, uint
);
372 static void kpress(XEvent
*);
373 static void cmessage(XEvent
*);
374 static void cresize(int, int);
375 static void resize(XEvent
*);
376 static void focus(XEvent
*);
377 static void brelease(XEvent
*);
378 static void bpress(XEvent
*);
379 static void bmotion(XEvent
*);
380 static void selnotify(XEvent
*);
381 static void selclear(XEvent
*);
382 static void selrequest(XEvent
*);
384 static void selinit(void);
385 static inline bool selected(int, int);
386 static void selcopy(void);
387 static void selscroll(int, int);
388 static void selsnap(int, int *, int *, int);
390 static int utf8decode(char *, long *);
391 static int utf8encode(long *, char *);
392 static int utf8size(char *);
393 static int isfullutf8(char *, int);
395 static ssize_t
xwrite(int, char *, size_t);
396 static void *xmalloc(size_t);
397 static void *xrealloc(void *, size_t);
398 static void *xcalloc(size_t, size_t);
400 static void (*handler
[LASTEvent
])(XEvent
*) = {
402 [ClientMessage
] = cmessage
,
403 [ConfigureNotify
] = resize
,
404 [VisibilityNotify
] = visibility
,
405 [UnmapNotify
] = unmap
,
409 [MotionNotify
] = bmotion
,
410 [ButtonPress
] = bpress
,
411 [ButtonRelease
] = brelease
,
412 [SelectionClear
] = selclear
,
413 [SelectionNotify
] = selnotify
,
414 [SelectionRequest
] = selrequest
,
421 static CSIEscape csiescseq
;
422 static STREscape strescseq
;
425 static Selection sel
;
426 static int iofd
= -1;
427 static char **opt_cmd
= NULL
;
428 static char *opt_io
= NULL
;
429 static char *opt_title
= NULL
;
430 static char *opt_embed
= NULL
;
431 static char *opt_class
= NULL
;
432 static char *opt_font
= NULL
;
434 static char *usedfont
= NULL
;
435 static int usedfontsize
= 0;
437 /* Font Ring Cache */
452 * Fontcache is a ring buffer, with frccur as current position and frclen as
453 * the current length of used elements.
456 static Fontcache frc
[1024];
457 static int frccur
= -1, frclen
= 0;
460 xwrite(int fd
, char *s
, size_t len
) {
464 ssize_t r
= write(fd
, s
, len
);
474 xmalloc(size_t len
) {
475 void *p
= malloc(len
);
478 die("Out of memory\n");
484 xrealloc(void *p
, size_t len
) {
485 if((p
= realloc(p
, len
)) == NULL
)
486 die("Out of memory\n");
492 xcalloc(size_t nmemb
, size_t size
) {
493 void *p
= calloc(nmemb
, size
);
496 die("Out of memory\n");
502 utf8decode(char *s
, long *u
) {
508 if(~c
& B7
) { /* 0xxxxxxx */
511 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
512 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
514 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
515 *u
= c
&(B3
|B2
|B1
|B0
);
517 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
524 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
526 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
529 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
532 if((n
== 1 && *u
< 0x80) ||
533 (n
== 2 && *u
< 0x800) ||
534 (n
== 3 && *u
< 0x10000) ||
535 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
547 utf8encode(long *u
, char *s
) {
555 *sp
= uc
; /* 0xxxxxxx */
557 } else if(*u
< 0x800) {
558 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
560 } else if(uc
< 0x10000) {
561 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
563 } else if(uc
<= 0x10FFFF) {
564 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
570 for(i
=n
,++sp
; i
>0; --i
,++sp
)
571 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
583 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
584 UTF-8 otherwise return 0 */
586 isfullutf8(char *s
, int b
) {
594 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
596 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
598 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
600 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
602 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
603 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
616 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
618 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
627 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
628 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
632 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
633 if(sel
.xtarget
== None
)
634 sel
.xtarget
= XA_STRING
;
642 return LIMIT(x
, 0, term
.col
-1);
650 return LIMIT(y
, 0, term
.row
-1);
654 selected(int x
, int y
) {
657 if(sel
.ey
== y
&& sel
.by
== y
) {
658 bx
= MIN(sel
.bx
, sel
.ex
);
659 ex
= MAX(sel
.bx
, sel
.ex
);
661 return BETWEEN(x
, bx
, ex
);
664 if(sel
.type
== SEL_RECTANGULAR
) {
665 return ((sel
.b
.y
<= y
&& y
<= sel
.e
.y
)
666 && (sel
.b
.x
<= x
&& x
<= sel
.e
.x
));
668 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
669 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
670 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
671 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
675 selsnap(int mode
, int *x
, int *y
, int direction
) {
678 while(*x
> 0 && *x
< term
.col
-1
679 && term
.line
[*y
][*x
+ direction
].c
[0] != ' ') {
684 *x
= (direction
< 0) ? 0 : term
.col
- 1;
692 getbuttoninfo(XEvent
*e
) {
694 uint state
= e
->xbutton
.state
&~Button1Mask
;
696 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
698 sel
.ex
= x2col(e
->xbutton
.x
);
699 sel
.ey
= y2row(e
->xbutton
.y
);
702 || (sel
.by
== sel
.ey
&& sel
.bx
< sel
.ex
)) {
703 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, -1);
704 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, +1);
706 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, -1);
707 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, +1);
710 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
711 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
712 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
713 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
715 sel
.type
= SEL_REGULAR
;
716 for(type
= 1; type
< LEN(selmasks
); ++type
) {
717 if(match(selmasks
[type
], state
)) {
725 mousereport(XEvent
*e
) {
726 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
727 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
730 static int ob
, ox
, oy
;
733 if(e
->xbutton
.type
== MotionNotify
) {
734 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
738 } else if(!IS_SET(MODE_MOUSESGR
)
739 && (e
->xbutton
.type
== ButtonRelease
740 || button
== AnyButton
)) {
746 if(e
->xbutton
.type
== ButtonPress
) {
752 button
+= (state
& ShiftMask
? 4 : 0)
753 + (state
& Mod4Mask
? 8 : 0)
754 + (state
& ControlMask
? 16 : 0);
757 if(IS_SET(MODE_MOUSESGR
)) {
758 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
760 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
761 } else if(x
< 223 && y
< 223) {
762 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
763 32+button
, 32+x
+1, 32+y
+1);
775 if(IS_SET(MODE_MOUSE
)) {
777 } else if(e
->xbutton
.button
== Button1
) {
778 gettimeofday(&now
, NULL
);
780 /* Clear previous selection, logically and visually. */
783 tsetdirt(sel
.b
.y
, sel
.e
.y
);
787 sel
.type
= SEL_REGULAR
;
788 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
789 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
792 * If the user clicks below predefined timeouts specific
793 * snapping behaviour is exposed.
795 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
796 sel
.snap
= SNAP_LINE
;
797 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
798 sel
.snap
= SNAP_WORD
;
802 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, -1);
803 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, 1);
810 * Draw selection, unless it's regular and we don't want to
811 * make clicks visible
815 tsetdirt(sel
.b
.y
, sel
.e
.y
);
818 sel
.tclick2
= sel
.tclick1
;
820 } else if(e
->xbutton
.button
== Button4
) {
822 } else if(e
->xbutton
.button
== Button5
) {
830 int x
, y
, bufsize
, size
;
836 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
837 ptr
= str
= xmalloc(bufsize
);
839 /* append every set & selected glyph to the selection */
840 for(y
= sel
.b
.y
; y
< sel
.e
.y
+ 1; y
++) {
841 gp
= &term
.line
[y
][0];
842 last
= gp
+ term
.col
;
844 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
845 strcmp(last
->c
, " ") != 0))
848 for(x
= 0; gp
<= last
; x
++, ++gp
) {
852 size
= utf8size(gp
->c
);
853 memcpy(ptr
, gp
->c
, size
);
858 * Copy and pasting of line endings is inconsistent
859 * in the inconsistent terminal and GUI world.
860 * The best solution seems like to produce '\n' when
861 * something is copied from st and convert '\n' to
862 * '\r', when something to be pasted is received by
864 * FIXME: Fix the computer world.
866 if(y
< sel
.e
.y
&& !((gp
-1)->mode
& ATTR_WRAP
))
875 selnotify(XEvent
*e
) {
876 ulong nitems
, ofs
, rem
;
878 uchar
*data
, *last
, *repl
;
883 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
884 False
, AnyPropertyType
, &type
, &format
,
885 &nitems
, &rem
, &data
)) {
886 fprintf(stderr
, "Clipboard allocation failed\n");
891 * As seen in selcopy:
892 * Line endings are inconsistent in the terminal and GUI world
893 * copy and pasting. When receiving some selection data,
894 * replace all '\n' with '\r'.
895 * FIXME: Fix the computer world.
898 last
= data
+ nitems
* format
/ 8;
899 while((repl
= memchr(repl
, '\n', last
- repl
))) {
903 ttywrite((const char *)data
, nitems
* format
/ 8);
905 /* number of 32-bit chunks returned */
906 ofs
+= nitems
* format
/ 32;
911 selpaste(const Arg
*dummy
) {
912 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
913 xw
.win
, CurrentTime
);
917 clippaste(const Arg
*dummy
) {
920 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
921 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
922 xw
.win
, CurrentTime
);
926 selclear(XEvent
*e
) {
930 tsetdirt(sel
.b
.y
, sel
.e
.y
);
934 selrequest(XEvent
*e
) {
935 XSelectionRequestEvent
*xsre
;
937 Atom xa_targets
, string
;
939 xsre
= (XSelectionRequestEvent
*) e
;
940 xev
.type
= SelectionNotify
;
941 xev
.requestor
= xsre
->requestor
;
942 xev
.selection
= xsre
->selection
;
943 xev
.target
= xsre
->target
;
944 xev
.time
= xsre
->time
;
948 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
949 if(xsre
->target
== xa_targets
) {
950 /* respond with the supported type */
951 string
= sel
.xtarget
;
952 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
953 XA_ATOM
, 32, PropModeReplace
,
954 (uchar
*) &string
, 1);
955 xev
.property
= xsre
->property
;
956 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
957 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
958 xsre
->target
, 8, PropModeReplace
,
959 (uchar
*) sel
.clip
, strlen(sel
.clip
));
960 xev
.property
= xsre
->property
;
963 /* all done, send a notification to the listener */
964 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
965 fprintf(stderr
, "Error sending SelectionNotify event\n");
970 /* register the selection for both the clipboard and the primary */
976 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
978 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
979 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
983 brelease(XEvent
*e
) {
984 if(IS_SET(MODE_MOUSE
)) {
989 if(e
->xbutton
.button
== Button2
) {
991 } else if(e
->xbutton
.button
== Button1
) {
999 term
.dirty
[sel
.ey
] = 1;
1004 bmotion(XEvent
*e
) {
1005 int oldey
, oldex
, oldsby
, oldsey
;
1007 if(IS_SET(MODE_MOUSE
)) {
1022 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
1023 tsetdirt(MIN(sel
.b
.y
, oldsby
), MAX(sel
.e
.y
, oldsey
));
1028 die(const char *errstr
, ...) {
1031 va_start(ap
, errstr
);
1032 vfprintf(stderr
, errstr
, ap
);
1040 char *envshell
= getenv("SHELL");
1041 const struct passwd
*pass
= getpwuid(getuid());
1042 char buf
[sizeof(long) * 8 + 1];
1044 unsetenv("COLUMNS");
1046 unsetenv("TERMCAP");
1049 setenv("LOGNAME", pass
->pw_name
, 1);
1050 setenv("USER", pass
->pw_name
, 1);
1051 setenv("SHELL", pass
->pw_shell
, 0);
1052 setenv("HOME", pass
->pw_dir
, 0);
1055 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1056 setenv("WINDOWID", buf
, 1);
1058 signal(SIGCHLD
, SIG_DFL
);
1059 signal(SIGHUP
, SIG_DFL
);
1060 signal(SIGINT
, SIG_DFL
);
1061 signal(SIGQUIT
, SIG_DFL
);
1062 signal(SIGTERM
, SIG_DFL
);
1063 signal(SIGALRM
, SIG_DFL
);
1065 DEFAULT(envshell
, shell
);
1066 setenv("TERM", termname
, 1);
1067 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1068 execvp(args
[0], args
);
1076 if(waitpid(pid
, &stat
, 0) < 0)
1077 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1079 if(WIFEXITED(stat
)) {
1080 exit(WEXITSTATUS(stat
));
1089 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1091 /* seems to work fine on linux, openbsd and freebsd */
1092 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1093 die("openpty failed: %s\n", SERRNO
);
1095 switch(pid
= fork()) {
1097 die("fork failed\n");
1100 setsid(); /* create a new process group */
1101 dup2(s
, STDIN_FILENO
);
1102 dup2(s
, STDOUT_FILENO
);
1103 dup2(s
, STDERR_FILENO
);
1104 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1105 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1113 signal(SIGCHLD
, sigchld
);
1115 iofd
= (!strcmp(opt_io
, "-")) ?
1117 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1119 fprintf(stderr
, "Error opening %s:%s\n",
1120 opt_io
, strerror(errno
));
1130 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1132 fprintf(stderr
, "\n");
1137 static char buf
[BUFSIZ
];
1138 static int buflen
= 0;
1141 int charsize
; /* size of utf8 char in bytes */
1145 /* append read bytes to unprocessed bytes */
1146 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1147 die("Couldn't read from shell: %s\n", SERRNO
);
1149 /* process every complete utf8 char */
1152 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1153 charsize
= utf8decode(ptr
, &utf8c
);
1154 utf8encode(&utf8c
, s
);
1160 /* keep any uncomplete utf8 char for the next call */
1161 memmove(buf
, ptr
, buflen
);
1165 ttywrite(const char *s
, size_t n
) {
1166 if(write(cmdfd
, s
, n
) == -1)
1167 die("write error on tty: %s\n", SERRNO
);
1174 w
.ws_row
= term
.row
;
1175 w
.ws_col
= term
.col
;
1176 w
.ws_xpixel
= xw
.tw
;
1177 w
.ws_ypixel
= xw
.th
;
1178 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1179 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1183 tattrset(int attr
) {
1186 for(i
= 0; i
< term
.row
-1; i
++) {
1187 for(j
= 0; j
< term
.col
-1; j
++) {
1188 if(term
.line
[i
][j
].mode
& attr
)
1197 tsetdirt(int top
, int bot
) {
1200 LIMIT(top
, 0, term
.row
-1);
1201 LIMIT(bot
, 0, term
.row
-1);
1203 for(i
= top
; i
<= bot
; i
++)
1208 tsetdirtattr(int attr
) {
1211 for(i
= 0; i
< term
.row
-1; i
++) {
1212 for(j
= 0; j
< term
.col
-1; j
++) {
1213 if(term
.line
[i
][j
].mode
& attr
) {
1223 tsetdirt(0, term
.row
-1);
1230 if(mode
== CURSOR_SAVE
) {
1232 } else if(mode
== CURSOR_LOAD
) {
1242 term
.c
= (TCursor
){{
1246 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1248 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1249 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1252 term
.bot
= term
.row
- 1;
1253 term
.mode
= MODE_WRAP
;
1255 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1257 tcursor(CURSOR_SAVE
);
1261 tnew(int col
, int row
) {
1262 memset(&term
, 0, sizeof(Term
));
1271 Line
*tmp
= term
.line
;
1273 term
.line
= term
.alt
;
1275 term
.mode
^= MODE_ALTSCREEN
;
1280 tscrolldown(int orig
, int n
) {
1284 LIMIT(n
, 0, term
.bot
-orig
+1);
1286 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1288 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1289 temp
= term
.line
[i
];
1290 term
.line
[i
] = term
.line
[i
-n
];
1291 term
.line
[i
-n
] = temp
;
1294 term
.dirty
[i
-n
] = 1;
1301 tscrollup(int orig
, int n
) {
1304 LIMIT(n
, 0, term
.bot
-orig
+1);
1306 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1308 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1309 temp
= term
.line
[i
];
1310 term
.line
[i
] = term
.line
[i
+n
];
1311 term
.line
[i
+n
] = temp
;
1314 term
.dirty
[i
+n
] = 1;
1317 selscroll(orig
, -n
);
1321 selscroll(int orig
, int n
) {
1325 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1326 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1330 if(sel
.type
== SEL_RECTANGULAR
) {
1331 if(sel
.by
< term
.top
)
1333 if(sel
.ey
> term
.bot
)
1336 if(sel
.by
< term
.top
) {
1340 if(sel
.ey
> term
.bot
) {
1345 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1346 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1351 tnewline(int first_col
) {
1355 tscrollup(term
.top
, 1);
1359 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1364 char *p
= csiescseq
.buf
, *np
;
1373 csiescseq
.buf
[csiescseq
.len
] = '\0';
1374 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1376 v
= strtol(p
, &np
, 10);
1379 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1381 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1383 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1387 csiescseq
.mode
= *p
;
1390 /* for absolute user moves, when decom is set */
1392 tmoveato(int x
, int y
) {
1393 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1397 tmoveto(int x
, int y
) {
1400 if(term
.c
.state
& CURSOR_ORIGIN
) {
1405 maxy
= term
.row
- 1;
1407 LIMIT(x
, 0, term
.col
-1);
1408 LIMIT(y
, miny
, maxy
);
1409 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1415 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1416 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1417 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1418 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1419 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1420 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1421 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1422 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1423 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1424 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1428 * The table is proudly stolen from rxvt.
1430 if(attr
->mode
& ATTR_GFX
) {
1431 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1432 && vt100_0
[c
[0] - 0x41]) {
1433 c
= vt100_0
[c
[0] - 0x41];
1438 term
.line
[y
][x
] = *attr
;
1439 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1443 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1447 temp
= x1
, x1
= x2
, x2
= temp
;
1449 temp
= y1
, y1
= y2
, y2
= temp
;
1451 LIMIT(x1
, 0, term
.col
-1);
1452 LIMIT(x2
, 0, term
.col
-1);
1453 LIMIT(y1
, 0, term
.row
-1);
1454 LIMIT(y2
, 0, term
.row
-1);
1456 for(y
= y1
; y
<= y2
; y
++) {
1458 for(x
= x1
; x
<= x2
; x
++) {
1461 term
.line
[y
][x
] = term
.c
.attr
;
1462 memcpy(term
.line
[y
][x
].c
, " ", 2);
1468 tdeletechar(int n
) {
1469 int src
= term
.c
.x
+ n
;
1471 int size
= term
.col
- src
;
1473 term
.dirty
[term
.c
.y
] = 1;
1475 if(src
>= term
.col
) {
1476 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1480 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1481 size
* sizeof(Glyph
));
1482 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1486 tinsertblank(int n
) {
1489 int size
= term
.col
- dst
;
1491 term
.dirty
[term
.c
.y
] = 1;
1493 if(dst
>= term
.col
) {
1494 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1498 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1499 size
* sizeof(Glyph
));
1500 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1504 tinsertblankline(int n
) {
1505 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1508 tscrolldown(term
.c
.y
, n
);
1512 tdeleteline(int n
) {
1513 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1516 tscrollup(term
.c
.y
, n
);
1520 tsetattr(int *attr
, int l
) {
1523 for(i
= 0; i
< l
; i
++) {
1526 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1527 | ATTR_BOLD
| ATTR_ITALIC \
1529 term
.c
.attr
.fg
= defaultfg
;
1530 term
.c
.attr
.bg
= defaultbg
;
1533 term
.c
.attr
.mode
|= ATTR_BOLD
;
1536 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1539 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1541 case 5: /* slow blink */
1542 case 6: /* rapid blink */
1543 term
.c
.attr
.mode
|= ATTR_BLINK
;
1546 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1550 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1553 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1556 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1560 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1563 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1566 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1568 if(BETWEEN(attr
[i
], 0, 255)) {
1569 term
.c
.attr
.fg
= attr
[i
];
1572 "erresc: bad fgcolor %d\n",
1577 "erresc(38): gfx attr %d unknown\n",
1582 term
.c
.attr
.fg
= defaultfg
;
1585 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1587 if(BETWEEN(attr
[i
], 0, 255)) {
1588 term
.c
.attr
.bg
= attr
[i
];
1591 "erresc: bad bgcolor %d\n",
1596 "erresc(48): gfx attr %d unknown\n",
1601 term
.c
.attr
.bg
= defaultbg
;
1604 if(BETWEEN(attr
[i
], 30, 37)) {
1605 term
.c
.attr
.fg
= attr
[i
] - 30;
1606 } else if(BETWEEN(attr
[i
], 40, 47)) {
1607 term
.c
.attr
.bg
= attr
[i
] - 40;
1608 } else if(BETWEEN(attr
[i
], 90, 97)) {
1609 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1610 } else if(BETWEEN(attr
[i
], 100, 107)) {
1611 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1614 "erresc(default): gfx attr %d unknown\n",
1615 attr
[i
]), csidump();
1623 tsetscroll(int t
, int b
) {
1626 LIMIT(t
, 0, term
.row
-1);
1627 LIMIT(b
, 0, term
.row
-1);
1637 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1640 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1644 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1648 case 1: /* DECCKM -- Cursor key */
1649 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1651 case 5: /* DECSCNM -- Reverse video */
1653 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1654 if(mode
!= term
.mode
)
1655 redraw(REDRAW_TIMEOUT
);
1657 case 6: /* DECOM -- Origin */
1658 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1661 case 7: /* DECAWM -- Auto wrap */
1662 MODBIT(term
.mode
, set
, MODE_WRAP
);
1664 case 0: /* Error (IGNORED) */
1665 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1666 case 3: /* DECCOLM -- Column (IGNORED) */
1667 case 4: /* DECSCLM -- Scroll (IGNORED) */
1668 case 8: /* DECARM -- Auto repeat (IGNORED) */
1669 case 18: /* DECPFF -- Printer feed (IGNORED) */
1670 case 19: /* DECPEX -- Printer extent (IGNORED) */
1671 case 42: /* DECNRCM -- National characters (IGNORED) */
1672 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1674 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1675 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1677 case 1000: /* 1000,1002: enable xterm mouse report */
1678 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1679 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1682 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1683 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1686 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1689 MODBIT(term
.mode
, set
, MODE_8BIT
);
1691 case 1049: /* = 1047 and 1048 */
1694 if (!allowaltscreen
)
1697 alt
= IS_SET(MODE_ALTSCREEN
);
1699 tclearregion(0, 0, term
.col
-1,
1702 if(set
^ alt
) /* set is always 1 or 0 */
1708 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1712 "erresc: unknown private set/reset mode %d\n",
1718 case 0: /* Error (IGNORED) */
1720 case 2: /* KAM -- keyboard action */
1721 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1723 case 4: /* IRM -- Insertion-replacement */
1724 MODBIT(term
.mode
, set
, MODE_INSERT
);
1726 case 12: /* SRM -- Send/Receive */
1727 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1729 case 20: /* LNM -- Linefeed/new line */
1730 MODBIT(term
.mode
, set
, MODE_CRLF
);
1734 "erresc: unknown set/reset mode %d\n",
1746 switch(csiescseq
.mode
) {
1749 fprintf(stderr
, "erresc: unknown csi ");
1753 case '@': /* ICH -- Insert <n> blank char */
1754 DEFAULT(csiescseq
.arg
[0], 1);
1755 tinsertblank(csiescseq
.arg
[0]);
1757 case 'A': /* CUU -- Cursor <n> Up */
1758 DEFAULT(csiescseq
.arg
[0], 1);
1759 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1761 case 'B': /* CUD -- Cursor <n> Down */
1762 case 'e': /* VPR --Cursor <n> Down */
1763 DEFAULT(csiescseq
.arg
[0], 1);
1764 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1766 case 'c': /* DA -- Device Attributes */
1767 if(csiescseq
.arg
[0] == 0)
1768 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1770 case 'C': /* CUF -- Cursor <n> Forward */
1771 case 'a': /* HPR -- Cursor <n> Forward */
1772 DEFAULT(csiescseq
.arg
[0], 1);
1773 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1775 case 'D': /* CUB -- Cursor <n> Backward */
1776 DEFAULT(csiescseq
.arg
[0], 1);
1777 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1779 case 'E': /* CNL -- Cursor <n> Down and first col */
1780 DEFAULT(csiescseq
.arg
[0], 1);
1781 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1783 case 'F': /* CPL -- Cursor <n> Up and first col */
1784 DEFAULT(csiescseq
.arg
[0], 1);
1785 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1787 case 'g': /* TBC -- Tabulation clear */
1788 switch(csiescseq
.arg
[0]) {
1789 case 0: /* clear current tab stop */
1790 term
.tabs
[term
.c
.x
] = 0;
1792 case 3: /* clear all the tabs */
1793 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1799 case 'G': /* CHA -- Move to <col> */
1801 DEFAULT(csiescseq
.arg
[0], 1);
1802 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1804 case 'H': /* CUP -- Move to <row> <col> */
1806 DEFAULT(csiescseq
.arg
[0], 1);
1807 DEFAULT(csiescseq
.arg
[1], 1);
1808 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1810 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1811 DEFAULT(csiescseq
.arg
[0], 1);
1812 while(csiescseq
.arg
[0]--)
1815 case 'J': /* ED -- Clear screen */
1817 switch(csiescseq
.arg
[0]) {
1819 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1820 if(term
.c
.y
< term
.row
-1) {
1821 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1827 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1828 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1831 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1837 case 'K': /* EL -- Clear line */
1838 switch(csiescseq
.arg
[0]) {
1840 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1844 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1847 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1851 case 'S': /* SU -- Scroll <n> line up */
1852 DEFAULT(csiescseq
.arg
[0], 1);
1853 tscrollup(term
.top
, csiescseq
.arg
[0]);
1855 case 'T': /* SD -- Scroll <n> line down */
1856 DEFAULT(csiescseq
.arg
[0], 1);
1857 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1859 case 'L': /* IL -- Insert <n> blank lines */
1860 DEFAULT(csiescseq
.arg
[0], 1);
1861 tinsertblankline(csiescseq
.arg
[0]);
1863 case 'l': /* RM -- Reset Mode */
1864 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1866 case 'M': /* DL -- Delete <n> lines */
1867 DEFAULT(csiescseq
.arg
[0], 1);
1868 tdeleteline(csiescseq
.arg
[0]);
1870 case 'X': /* ECH -- Erase <n> char */
1871 DEFAULT(csiescseq
.arg
[0], 1);
1872 tclearregion(term
.c
.x
, term
.c
.y
,
1873 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1875 case 'P': /* DCH -- Delete <n> char */
1876 DEFAULT(csiescseq
.arg
[0], 1);
1877 tdeletechar(csiescseq
.arg
[0]);
1879 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1880 DEFAULT(csiescseq
.arg
[0], 1);
1881 while(csiescseq
.arg
[0]--)
1884 case 'd': /* VPA -- Move to <row> */
1885 DEFAULT(csiescseq
.arg
[0], 1);
1886 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1888 case 'h': /* SM -- Set terminal mode */
1889 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1891 case 'm': /* SGR -- Terminal attribute (color) */
1892 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1894 case 'r': /* DECSTBM -- Set Scrolling Region */
1895 if(csiescseq
.priv
) {
1898 DEFAULT(csiescseq
.arg
[0], 1);
1899 DEFAULT(csiescseq
.arg
[1], term
.row
);
1900 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1904 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1905 tcursor(CURSOR_SAVE
);
1907 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1908 tcursor(CURSOR_LOAD
);
1919 for(i
= 0; i
< csiescseq
.len
; i
++) {
1920 c
= csiescseq
.buf
[i
] & 0xff;
1923 } else if(c
== '\n') {
1925 } else if(c
== '\r') {
1927 } else if(c
== 0x1b) {
1930 printf("(%02x)", c
);
1938 memset(&csiescseq
, 0, sizeof(csiescseq
));
1947 narg
= strescseq
.narg
;
1949 switch(strescseq
.type
) {
1950 case ']': /* OSC -- Operating System Command */
1951 switch(i
= atoi(strescseq
.args
[0])) {
1956 xsettitle(strescseq
.args
[1]);
1958 case 4: /* color set */
1961 p
= strescseq
.args
[2];
1963 case 104: /* color reset, here p = NULL */
1964 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
1965 if (!xsetcolorname(j
, p
)) {
1966 fprintf(stderr
, "erresc: invalid color %s\n", p
);
1969 * TODO if defaultbg color is changed, borders
1976 fprintf(stderr
, "erresc: unknown str ");
1981 case 'k': /* old title set compatibility */
1982 xsettitle(strescseq
.args
[0]);
1984 case 'P': /* DSC -- Device Control String */
1985 case '_': /* APC -- Application Program Command */
1986 case '^': /* PM -- Privacy Message */
1988 fprintf(stderr
, "erresc: unknown str ");
1997 char *p
= strescseq
.buf
;
2000 strescseq
.buf
[strescseq
.len
] = '\0';
2001 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2002 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2010 printf("ESC%c", strescseq
.type
);
2011 for(i
= 0; i
< strescseq
.len
; i
++) {
2012 c
= strescseq
.buf
[i
] & 0xff;
2015 } else if(isprint(c
)) {
2017 } else if(c
== '\n') {
2019 } else if(c
== '\r') {
2021 } else if(c
== 0x1b) {
2024 printf("(%02x)", c
);
2032 memset(&strescseq
, 0, sizeof(strescseq
));
2036 tputtab(bool forward
) {
2042 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2047 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2050 tmoveto(x
, term
.c
.y
);
2054 techo(char *buf
, int len
) {
2055 for(; len
> 0; buf
++, len
--) {
2058 if(c
== '\033') { /* escape */
2061 } else if(c
< '\x20') { /* control code */
2062 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2076 tputc(char *c
, int len
) {
2078 bool control
= ascii
< '\x20' || ascii
== 0177;
2081 if(xwrite(iofd
, c
, len
) < 0) {
2082 fprintf(stderr
, "Error writing in %s:%s\n",
2083 opt_io
, strerror(errno
));
2090 * STR sequences must be checked before anything else
2091 * because it can use some control codes as part of the sequence.
2093 if(term
.esc
& ESC_STR
) {
2096 term
.esc
= ESC_START
| ESC_STR_END
;
2098 case '\a': /* backwards compatibility to xterm */
2103 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2104 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2105 strescseq
.len
+= len
;
2108 * Here is a bug in terminals. If the user never sends
2109 * some code to stop the str or esc command, then st
2110 * will stop responding. But this is better than
2111 * silently failing with unknown characters. At least
2112 * then users will report back.
2114 * In the case users ever get fixed, here is the code:
2126 * Actions of control codes must be performed as soon they arrive
2127 * because they can be embedded inside a control sequence, and
2128 * they must not cause conflicts with sequences.
2136 tmoveto(term
.c
.x
-1, term
.c
.y
);
2139 tmoveto(0, term
.c
.y
);
2144 /* go to first col if the mode is set */
2145 tnewline(IS_SET(MODE_CRLF
));
2147 case '\a': /* BEL */
2148 if(!(xw
.state
& WIN_FOCUSED
))
2151 case '\033': /* ESC */
2153 term
.esc
= ESC_START
;
2155 case '\016': /* SO */
2156 case '\017': /* SI */
2158 * Different charsets are hard to handle. Applications
2159 * should use the right alt charset escapes for the
2160 * only reason they still exist: line drawing. The
2161 * rest is incompatible history st should not support.
2164 case '\032': /* SUB */
2165 case '\030': /* CAN */
2168 case '\005': /* ENQ (IGNORED) */
2169 case '\000': /* NUL (IGNORED) */
2170 case '\021': /* XON (IGNORED) */
2171 case '\023': /* XOFF (IGNORED) */
2172 case 0177: /* DEL (IGNORED) */
2175 } else if(term
.esc
& ESC_START
) {
2176 if(term
.esc
& ESC_CSI
) {
2177 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2178 if(BETWEEN(ascii
, 0x40, 0x7E)
2179 || csiescseq
.len
>= \
2180 sizeof(csiescseq
.buf
)-1) {
2185 } else if(term
.esc
& ESC_STR_END
) {
2189 } else if(term
.esc
& ESC_ALTCHARSET
) {
2191 case '0': /* Line drawing set */
2192 term
.c
.attr
.mode
|= ATTR_GFX
;
2194 case 'B': /* USASCII */
2195 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2197 case 'A': /* UK (IGNORED) */
2198 case '<': /* multinational charset (IGNORED) */
2199 case '5': /* Finnish (IGNORED) */
2200 case 'C': /* Finnish (IGNORED) */
2201 case 'K': /* German (IGNORED) */
2204 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2207 } else if(term
.esc
& ESC_TEST
) {
2208 if(ascii
== '8') { /* DEC screen alignment test. */
2209 char E
[UTF_SIZ
] = "E";
2212 for(x
= 0; x
< term
.col
; ++x
) {
2213 for(y
= 0; y
< term
.row
; ++y
)
2214 tsetchar(E
, &term
.c
.attr
, x
, y
);
2221 term
.esc
|= ESC_CSI
;
2224 term
.esc
|= ESC_TEST
;
2226 case 'P': /* DCS -- Device Control String */
2227 case '_': /* APC -- Application Program Command */
2228 case '^': /* PM -- Privacy Message */
2229 case ']': /* OSC -- Operating System Command */
2230 case 'k': /* old title set compatibility */
2232 strescseq
.type
= ascii
;
2233 term
.esc
|= ESC_STR
;
2235 case '(': /* set primary charset G0 */
2236 term
.esc
|= ESC_ALTCHARSET
;
2238 case ')': /* set secondary charset G1 (IGNORED) */
2239 case '*': /* set tertiary charset G2 (IGNORED) */
2240 case '+': /* set quaternary charset G3 (IGNORED) */
2243 case 'D': /* IND -- Linefeed */
2244 if(term
.c
.y
== term
.bot
) {
2245 tscrollup(term
.top
, 1);
2247 tmoveto(term
.c
.x
, term
.c
.y
+1);
2251 case 'E': /* NEL -- Next line */
2252 tnewline(1); /* always go to first col */
2255 case 'H': /* HTS -- Horizontal tab stop */
2256 term
.tabs
[term
.c
.x
] = 1;
2259 case 'M': /* RI -- Reverse index */
2260 if(term
.c
.y
== term
.top
) {
2261 tscrolldown(term
.top
, 1);
2263 tmoveto(term
.c
.x
, term
.c
.y
-1);
2267 case 'Z': /* DECID -- Identify Terminal */
2268 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2271 case 'c': /* RIS -- Reset to inital state */
2276 case '=': /* DECPAM -- Application keypad */
2277 term
.mode
|= MODE_APPKEYPAD
;
2280 case '>': /* DECPNM -- Normal keypad */
2281 term
.mode
&= ~MODE_APPKEYPAD
;
2284 case '7': /* DECSC -- Save Cursor */
2285 tcursor(CURSOR_SAVE
);
2288 case '8': /* DECRC -- Restore Cursor */
2289 tcursor(CURSOR_LOAD
);
2292 case '\\': /* ST -- Stop */
2296 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2297 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2302 * All characters which form part of a sequence are not
2308 * Display control codes only if we are in graphic mode
2310 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2312 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2314 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2315 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2319 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2320 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2321 &term
.line
[term
.c
.y
][term
.c
.x
],
2322 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2325 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2326 if(term
.c
.x
+1 < term
.col
) {
2327 tmoveto(term
.c
.x
+1, term
.c
.y
);
2329 term
.c
.state
|= CURSOR_WRAPNEXT
;
2334 tresize(int col
, int row
) {
2336 int minrow
= MIN(row
, term
.row
);
2337 int mincol
= MIN(col
, term
.col
);
2338 int slide
= term
.c
.y
- row
+ 1;
2342 if(col
< 1 || row
< 1)
2345 /* free unneeded rows */
2349 * slide screen to keep cursor where we expect it -
2350 * tscrollup would work here, but we can optimize to
2351 * memmove because we're freeing the earlier lines
2353 for(/* i = 0 */; i
< slide
; i
++) {
2357 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2358 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2360 for(i
+= row
; i
< term
.row
; i
++) {
2365 /* resize to new height */
2366 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2367 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2368 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2369 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2371 /* resize each row to new width, zero-pad if needed */
2372 for(i
= 0; i
< minrow
; i
++) {
2374 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2375 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2378 /* allocate any new rows */
2379 for(/* i == minrow */; i
< row
; i
++) {
2381 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2382 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2384 if(col
> term
.col
) {
2385 bp
= term
.tabs
+ term
.col
;
2387 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2388 while(--bp
> term
.tabs
&& !*bp
)
2390 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2393 /* update terminal size */
2396 /* reset scrolling region */
2397 tsetscroll(0, row
-1);
2398 /* make use of the LIMIT in tmoveto */
2399 tmoveto(term
.c
.x
, term
.c
.y
);
2400 /* Clearing both screens */
2403 if(mincol
< col
&& 0 < minrow
) {
2404 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2406 if(0 < col
&& minrow
< row
) {
2407 tclearregion(0, minrow
, col
- 1, row
- 1);
2410 } while(orig
!= term
.line
);
2416 xresize(int col
, int row
) {
2417 xw
.tw
= MAX(1, col
* xw
.cw
);
2418 xw
.th
= MAX(1, row
* xw
.ch
);
2420 XFreePixmap(xw
.dpy
, xw
.buf
);
2421 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2422 DefaultDepth(xw
.dpy
, xw
.scr
));
2423 XftDrawChange(xw
.draw
, xw
.buf
);
2424 xclear(0, 0, xw
.w
, xw
.h
);
2427 static inline ushort
2428 sixd_to_16bit(int x
) {
2429 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2435 XRenderColor color
= { .alpha
= 0xffff };
2437 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2438 for(i
= 0; i
< LEN(colorname
); i
++) {
2441 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2442 die("Could not allocate color '%s'\n", colorname
[i
]);
2446 /* load colors [16-255] ; same colors as xterm */
2447 for(i
= 16, r
= 0; r
< 6; r
++) {
2448 for(g
= 0; g
< 6; g
++) {
2449 for(b
= 0; b
< 6; b
++) {
2450 color
.red
= sixd_to_16bit(r
);
2451 color
.green
= sixd_to_16bit(g
);
2452 color
.blue
= sixd_to_16bit(b
);
2453 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2454 die("Could not allocate color %d\n", i
);
2461 for(r
= 0; r
< 24; r
++, i
++) {
2462 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2463 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2465 die("Could not allocate color %d\n", i
);
2471 xsetcolorname(int x
, const char *name
) {
2472 XRenderColor color
= { .alpha
= 0xffff };
2474 if (x
< 0 || x
> LEN(colorname
))
2477 if(16 <= x
&& x
< 16 + 216) {
2478 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2479 color
.red
= sixd_to_16bit(r
);
2480 color
.green
= sixd_to_16bit(g
);
2481 color
.blue
= sixd_to_16bit(b
);
2482 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2483 return 0; /* something went wrong */
2486 } else if (16 + 216 <= x
&& x
< 256) {
2487 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2488 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2489 return 0; /* something went wrong */
2493 name
= colorname
[x
];
2496 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2503 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2504 XftDrawRect(xw
.draw
,
2505 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2506 borderpx
+ col1
* xw
.cw
,
2507 borderpx
+ row1
* xw
.ch
,
2508 (col2
-col1
+1) * xw
.cw
,
2509 (row2
-row1
+1) * xw
.ch
);
2513 * Absolute coordinates.
2516 xclear(int x1
, int y1
, int x2
, int y2
) {
2517 XftDrawRect(xw
.draw
,
2518 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2519 x1
, y1
, x2
-x1
, y2
-y1
);
2524 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2525 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2526 XSizeHints
*sizeh
= NULL
;
2528 sizeh
= XAllocSizeHints();
2529 if(xw
.isfixed
== False
) {
2530 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2531 sizeh
->height
= xw
.h
;
2532 sizeh
->width
= xw
.w
;
2533 sizeh
->height_inc
= xw
.ch
;
2534 sizeh
->width_inc
= xw
.cw
;
2535 sizeh
->base_height
= 2 * borderpx
;
2536 sizeh
->base_width
= 2 * borderpx
;
2538 sizeh
->flags
= PMaxSize
| PMinSize
;
2539 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2540 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2543 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2548 xloadfont(Font
*f
, FcPattern
*pattern
) {
2552 match
= FcFontMatch(NULL
, pattern
, &result
);
2556 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2557 FcPatternDestroy(match
);
2561 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2562 FcPatternDestroy(match
);
2566 f
->pattern
= FcPatternDuplicate(pattern
);
2568 f
->ascent
= f
->match
->ascent
;
2569 f
->descent
= f
->match
->descent
;
2571 f
->rbearing
= f
->match
->max_advance_width
;
2573 f
->height
= f
->ascent
+ f
->descent
;
2574 f
->width
= f
->lbearing
+ f
->rbearing
;
2580 xloadfonts(char *fontstr
, int fontsize
) {
2585 if(fontstr
[0] == '-') {
2586 pattern
= XftXlfdParse(fontstr
, False
, False
);
2588 pattern
= FcNameParse((FcChar8
*)fontstr
);
2592 die("st: can't open font %s\n", fontstr
);
2595 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2596 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2597 usedfontsize
= fontsize
;
2599 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2600 if(result
== FcResultMatch
) {
2601 usedfontsize
= (int)fontval
;
2604 * Default font size is 12, if none given. This is to
2605 * have a known usedfontsize value.
2607 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2612 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2613 FcDefaultSubstitute(pattern
);
2615 if(xloadfont(&dc
.font
, pattern
))
2616 die("st: can't open font %s\n", fontstr
);
2618 /* Setting character width and height. */
2619 xw
.cw
= dc
.font
.width
;
2620 xw
.ch
= dc
.font
.height
;
2622 FcPatternDel(pattern
, FC_SLANT
);
2623 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2624 if(xloadfont(&dc
.ifont
, pattern
))
2625 die("st: can't open font %s\n", fontstr
);
2627 FcPatternDel(pattern
, FC_WEIGHT
);
2628 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2629 if(xloadfont(&dc
.ibfont
, pattern
))
2630 die("st: can't open font %s\n", fontstr
);
2632 FcPatternDel(pattern
, FC_SLANT
);
2633 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2634 if(xloadfont(&dc
.bfont
, pattern
))
2635 die("st: can't open font %s\n", fontstr
);
2637 FcPatternDestroy(pattern
);
2641 xunloadfonts(void) {
2645 * Free the loaded fonts in the font cache. This is done backwards
2648 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2651 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2656 XftFontClose(xw
.dpy
, dc
.font
.match
);
2657 FcPatternDestroy(dc
.font
.pattern
);
2658 FcFontSetDestroy(dc
.font
.set
);
2659 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2660 FcPatternDestroy(dc
.bfont
.pattern
);
2661 FcFontSetDestroy(dc
.bfont
.set
);
2662 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2663 FcPatternDestroy(dc
.ifont
.pattern
);
2664 FcFontSetDestroy(dc
.ifont
.set
);
2665 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2666 FcPatternDestroy(dc
.ibfont
.pattern
);
2667 FcFontSetDestroy(dc
.ibfont
.set
);
2671 xzoom(const Arg
*arg
) {
2673 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2680 XSetWindowAttributes attrs
;
2686 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2687 die("Can't open display\n");
2688 xw
.scr
= XDefaultScreen(xw
.dpy
);
2689 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2693 die("Could not init fontconfig.\n");
2695 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2696 xloadfonts(usedfont
, 0);
2699 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2702 /* adjust fixed window geometry */
2704 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2705 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2707 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2709 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2714 /* window - default size */
2715 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2716 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2722 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2723 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2724 attrs
.bit_gravity
= NorthWestGravity
;
2725 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2726 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2727 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2728 attrs
.colormap
= xw
.cmap
;
2730 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2731 XRootWindow(xw
.dpy
, xw
.scr
);
2732 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2733 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2735 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2739 memset(&gcvalues
, 0, sizeof(gcvalues
));
2740 gcvalues
.graphics_exposures
= False
;
2741 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2743 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2744 DefaultDepth(xw
.dpy
, xw
.scr
));
2745 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2746 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2748 /* Xft rendering context */
2749 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2752 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2753 XSetLocaleModifiers("@im=local");
2754 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2755 XSetLocaleModifiers("@im=");
2756 if((xw
.xim
= XOpenIM(xw
.dpy
,
2757 NULL
, NULL
, NULL
)) == NULL
) {
2758 die("XOpenIM failed. Could not open input"
2763 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2764 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2765 XNFocusWindow
, xw
.win
, NULL
);
2767 die("XCreateIC failed. Could not obtain input method.\n");
2769 /* white cursor, black outline */
2770 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2771 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2772 XRecolorCursor(xw
.dpy
, cursor
,
2773 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2774 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2776 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2777 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2778 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2781 XMapWindow(xw
.dpy
, xw
.win
);
2787 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2788 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2789 width
= charlen
* xw
.cw
, xp
, i
;
2791 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2794 Font
*font
= &dc
.font
;
2796 FcPattern
*fcpattern
, *fontpattern
;
2797 FcFontSet
*fcsets
[] = { NULL
};
2798 FcCharSet
*fccharset
;
2799 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2800 XRenderColor colfg
, colbg
;
2803 frcflags
= FRC_NORMAL
;
2805 if(base
.mode
& ATTR_ITALIC
) {
2806 if(base
.fg
== defaultfg
)
2807 base
.fg
= defaultitalic
;
2809 frcflags
= FRC_ITALIC
;
2810 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2811 if(base
.fg
== defaultfg
)
2812 base
.fg
= defaultitalic
;
2814 frcflags
= FRC_ITALICBOLD
;
2815 } else if(base
.mode
& ATTR_UNDERLINE
) {
2816 if(base
.fg
== defaultfg
)
2817 base
.fg
= defaultunderline
;
2819 fg
= &dc
.col
[base
.fg
];
2820 bg
= &dc
.col
[base
.bg
];
2822 if(base
.mode
& ATTR_BOLD
) {
2823 if(BETWEEN(base
.fg
, 0, 7)) {
2824 /* basic system colors */
2825 fg
= &dc
.col
[base
.fg
+ 8];
2826 } else if(BETWEEN(base
.fg
, 16, 195)) {
2828 fg
= &dc
.col
[base
.fg
+ 36];
2829 } else if(BETWEEN(base
.fg
, 232, 251)) {
2831 fg
= &dc
.col
[base
.fg
+ 4];
2834 * Those ranges will not be brightened:
2835 * 8 - 15 – bright system colors
2836 * 196 - 231 – highest 256 color cube
2837 * 252 - 255 – brightest colors in greyscale
2840 frcflags
= FRC_BOLD
;
2843 if(IS_SET(MODE_REVERSE
)) {
2844 if(fg
== &dc
.col
[defaultfg
]) {
2845 fg
= &dc
.col
[defaultbg
];
2847 colfg
.red
= ~fg
->color
.red
;
2848 colfg
.green
= ~fg
->color
.green
;
2849 colfg
.blue
= ~fg
->color
.blue
;
2850 colfg
.alpha
= fg
->color
.alpha
;
2851 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2855 if(bg
== &dc
.col
[defaultbg
]) {
2856 bg
= &dc
.col
[defaultfg
];
2858 colbg
.red
= ~bg
->color
.red
;
2859 colbg
.green
= ~bg
->color
.green
;
2860 colbg
.blue
= ~bg
->color
.blue
;
2861 colbg
.alpha
= bg
->color
.alpha
;
2862 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2867 if(base
.mode
& ATTR_REVERSE
) {
2873 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
2876 /* Intelligent cleaning up of the borders. */
2878 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2879 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2881 if(x
+ charlen
>= term
.col
) {
2882 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2883 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2886 xclear(winx
, 0, winx
+ width
, borderpx
);
2888 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2890 /* Clean up the region we want to draw to. */
2891 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2893 /* Set the clip region because Xft is sometimes dirty. */
2898 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
2900 fcsets
[0] = font
->set
;
2901 for(xp
= winx
; bytelen
> 0;) {
2903 * Search for the range in the to be printed string of glyphs
2904 * that are in the main font. Then print that range. If
2905 * some glyph is found that is not in the font, do the
2913 u8cblen
= utf8decode(s
, &u8char
);
2917 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2918 if(!doesexist
|| bytelen
<= 0) {
2927 XftDrawStringUtf8(xw
.draw
, fg
,
2929 winy
+ font
->ascent
,
2932 xp
+= font
->width
* u8fl
;
2945 /* Search the font cache. */
2946 for(i
= 0; i
< frclen
; i
++, frp
--) {
2950 if(frc
[frp
].c
== u8char
2951 && frc
[frp
].flags
== frcflags
) {
2956 /* Nothing was found. */
2959 * Nothing was found in the cache. Now use
2960 * some dozen of Fontconfig calls to get the
2961 * font for one single character.
2963 fcpattern
= FcPatternDuplicate(font
->pattern
);
2964 fccharset
= FcCharSetCreate();
2966 FcCharSetAddChar(fccharset
, u8char
);
2967 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2969 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2972 FcConfigSubstitute(0, fcpattern
,
2974 FcDefaultSubstitute(fcpattern
);
2976 fontpattern
= FcFontSetMatch(0, fcsets
,
2977 FcTrue
, fcpattern
, &fcres
);
2980 * Overwrite or create the new cache entry.
2984 if(frccur
>= LEN(frc
))
2986 if(frclen
> LEN(frc
)) {
2988 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2991 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2993 frc
[frccur
].c
= u8char
;
2994 frc
[frccur
].flags
= frcflags
;
2996 FcPatternDestroy(fcpattern
);
2997 FcCharSetDestroy(fccharset
);
3002 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
3003 xp
, winy
+ frc
[frp
].font
->ascent
,
3004 (FcChar8
*)u8c
, u8cblen
);
3010 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3011 winy + font->ascent, (FcChar8 *)s, bytelen);
3014 if(base
.mode
& ATTR_UNDERLINE
) {
3015 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3019 /* Reset clip to none. */
3020 XftDrawSetClip(xw
.draw
, 0);
3025 static int oldx
= 0, oldy
= 0;
3027 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3029 LIMIT(oldx
, 0, term
.col
-1);
3030 LIMIT(oldy
, 0, term
.row
-1);
3032 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3034 /* remove the old cursor */
3035 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3036 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3039 /* draw the new one */
3040 if(!(IS_SET(MODE_HIDE
))) {
3041 if(xw
.state
& WIN_FOCUSED
) {
3042 if(IS_SET(MODE_REVERSE
)) {
3043 g
.mode
|= ATTR_REVERSE
;
3049 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
3051 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3052 borderpx
+ term
.c
.x
* xw
.cw
,
3053 borderpx
+ term
.c
.y
* xw
.ch
,
3055 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3056 borderpx
+ term
.c
.x
* xw
.cw
,
3057 borderpx
+ term
.c
.y
* xw
.ch
,
3059 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3060 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
3061 borderpx
+ term
.c
.y
* xw
.ch
,
3063 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3064 borderpx
+ term
.c
.x
* xw
.cw
,
3065 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3068 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3074 xsettitle(char *p
) {
3077 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3079 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3084 xsettitle(opt_title
? opt_title
: "st");
3088 redraw(int timeout
) {
3089 struct timespec tv
= {0, timeout
* 1000};
3095 nanosleep(&tv
, NULL
);
3096 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3102 drawregion(0, 0, term
.col
, term
.row
);
3103 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3105 XSetForeground(xw
.dpy
, dc
.gc
,
3106 dc
.col
[IS_SET(MODE_REVERSE
)?
3107 defaultfg
: defaultbg
].pixel
);
3111 drawregion(int x1
, int y1
, int x2
, int y2
) {
3112 int ic
, ib
, x
, y
, ox
, sl
;
3114 char buf
[DRAW_BUF_SIZ
];
3115 bool ena_sel
= sel
.bx
!= -1;
3117 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3120 if(!(xw
.state
& WIN_VISIBLE
))
3123 for(y
= y1
; y
< y2
; y
++) {
3127 xtermclear(0, y
, term
.col
, y
);
3129 base
= term
.line
[y
][0];
3131 for(x
= x1
; x
< x2
; x
++) {
3132 new = term
.line
[y
][x
];
3133 if(ena_sel
&& selected(x
, y
))
3134 new.mode
^= ATTR_REVERSE
;
3135 if(ib
> 0 && (ATTRCMP(base
, new)
3136 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3137 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3145 sl
= utf8size(new.c
);
3146 memcpy(buf
+ib
, new.c
, sl
);
3151 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3157 expose(XEvent
*ev
) {
3158 XExposeEvent
*e
= &ev
->xexpose
;
3160 if(xw
.state
& WIN_REDRAW
) {
3162 xw
.state
&= ~WIN_REDRAW
;
3168 visibility(XEvent
*ev
) {
3169 XVisibilityEvent
*e
= &ev
->xvisibility
;
3171 if(e
->state
== VisibilityFullyObscured
) {
3172 xw
.state
&= ~WIN_VISIBLE
;
3173 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3174 /* need a full redraw for next Expose, not just a buf copy */
3175 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3181 xw
.state
&= ~WIN_VISIBLE
;
3185 xseturgency(int add
) {
3186 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3188 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3189 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3195 XFocusChangeEvent
*e
= &ev
->xfocus
;
3197 if(e
->mode
== NotifyGrab
)
3200 if(ev
->type
== FocusIn
) {
3201 XSetICFocus(xw
.xic
);
3202 xw
.state
|= WIN_FOCUSED
;
3205 XUnsetICFocus(xw
.xic
);
3206 xw
.state
&= ~WIN_FOCUSED
;
3211 match(uint mask
, uint state
) {
3212 state
&= ~(ignoremod
);
3214 if(mask
== XK_NO_MOD
&& state
)
3216 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3218 if((state
& mask
) != state
)
3224 numlock(const Arg
*dummy
) {
3229 kmap(KeySym k
, uint state
) {
3234 /* Check for mapped keys out of X11 function keys. */
3235 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3236 if(mappedkeys
[i
] == k
)
3239 if(i
== LEN(mappedkeys
)) {
3240 if((k
& 0xFFFF) < 0xFD00)
3244 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3250 if(!match(mask
, state
))
3253 if(kp
->appkey
> 0) {
3254 if(!IS_SET(MODE_APPKEYPAD
))
3256 if(term
.numlock
&& kp
->appkey
== 2)
3258 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3262 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3264 && !IS_SET(MODE_APPCURSOR
))) {
3268 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3269 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3280 kpress(XEvent
*ev
) {
3281 XKeyEvent
*e
= &ev
->xkey
;
3283 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3289 if(IS_SET(MODE_KBDLOCK
))
3292 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3293 e
->state
&= ~Mod2Mask
;
3295 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3296 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3297 bp
->func(&(bp
->arg
));
3302 /* 2. custom keys from config.h */
3303 if((customkey
= kmap(ksym
, e
->state
))) {
3304 len
= strlen(customkey
);
3305 memcpy(buf
, customkey
, len
);
3306 /* 3. hardcoded (overrides X lookup) */
3311 if(len
== 1 && e
->state
& Mod1Mask
) {
3312 if(IS_SET(MODE_8BIT
)) {
3315 ret
= utf8encode(&c
, cp
);
3324 memcpy(cp
, xstr
, len
);
3325 len
= cp
- buf
+ len
;
3329 if(IS_SET(MODE_ECHO
))
3335 cmessage(XEvent
*e
) {
3338 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3340 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3341 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3342 xw
.state
|= WIN_FOCUSED
;
3344 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3345 xw
.state
&= ~WIN_FOCUSED
;
3347 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3348 /* Send SIGHUP to shell */
3355 cresize(int width
, int height
) {
3363 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3364 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3373 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3376 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3383 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3384 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3386 gettimeofday(&lastblink
, NULL
);
3387 gettimeofday(&last
, NULL
);
3389 for(xev
= actionfps
;;) {
3391 FD_SET(cmdfd
, &rfd
);
3394 switch(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3398 die("select failed: %s\n", SERRNO
);
3400 if(FD_ISSET(cmdfd
, &rfd
)) {
3403 blinkset
= tattrset(ATTR_BLINK
);
3404 if(!blinkset
&& term
.mode
& ATTR_BLINK
)
3405 term
.mode
&= ~(MODE_BLINK
);
3409 if(FD_ISSET(xfd
, &rfd
))
3413 gettimeofday(&now
, NULL
);
3414 drawtimeout
.tv_sec
= 0;
3415 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3419 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3420 tsetdirtattr(ATTR_BLINK
);
3421 term
.mode
^= MODE_BLINK
;
3422 gettimeofday(&lastblink
, NULL
);
3425 if(TIMEDIFF(now
, last
) \
3426 > (xev
? (1000/xfps
) : (1000/actionfps
))) {
3432 while(XPending(xw
.dpy
)) {
3433 XNextEvent(xw
.dpy
, &ev
);
3434 if(XFilterEvent(&ev
, None
))
3436 if(handler
[ev
.type
])
3437 (handler
[ev
.type
])(&ev
);
3443 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3445 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3447 drawtimeout
.tv_usec
= 1000 * \
3459 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3460 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3461 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3465 main(int argc
, char *argv
[]) {
3469 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3474 allowaltscreen
= false;
3477 opt_class
= EARGF(usage());
3480 /* eat all remaining arguments */
3485 opt_font
= EARGF(usage());
3488 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3493 if(bitm
& WidthValue
)
3495 if(bitm
& HeightValue
)
3497 if(bitm
& XNegative
&& xw
.fx
== 0)
3499 if(bitm
& XNegative
&& xw
.fy
== 0)
3502 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3506 opt_io
= EARGF(usage());
3509 opt_title
= EARGF(usage());
3512 opt_embed
= EARGF(usage());
3520 setlocale(LC_CTYPE
, "");
3521 XSetLocaleModifiers("");
3527 cresize(xw
.h
, xw
.w
);