1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
15 #include <sys/ioctl.h>
16 #include <sys/select.h>
19 #include <sys/types.h>
23 #include <X11/Xatom.h>
25 #include <X11/Xutil.h>
26 #include <X11/cursorfont.h>
27 #include <X11/keysym.h>
28 #include <X11/extensions/Xdbe.h>
29 #include <X11/Xft/Xft.h>
30 #include <fontconfig/fontconfig.h>
34 #define Draw XftDraw *
35 #define Colour XftColor
36 #define Colourmap Colormap
40 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
42 #elif defined(__FreeBSD__) || defined(__DragonFly__)
47 "st " VERSION " (c) 2010-2012 st engineers\n" \
48 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
49 " [-t title] [-w windowid] [-e command ...]\n"
52 #define XEMBED_FOCUS_IN 4
53 #define XEMBED_FOCUS_OUT 5
56 #define ESC_BUF_SIZ 256
57 #define ESC_ARG_SIZ 16
58 #define STR_BUF_SIZ 256
59 #define STR_ARG_SIZ 16
60 #define DRAW_BUF_SIZ 20*1024
62 #define XK_ANY_MOD UINT_MAX
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
{
91 enum cursor_movement
{
114 MODE_MOUSEMOTION
= 64,
120 MODE_APPCURSOR
= 2048
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 */
140 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
142 typedef unsigned char uchar
;
143 typedef unsigned int uint
;
144 typedef unsigned long ulong
;
145 typedef unsigned short ushort
;
148 char c
[UTF_SIZ
]; /* character code */
149 uchar mode
; /* attribute flags */
150 ushort fg
; /* foreground */
151 ushort bg
; /* background */
152 uchar state
; /* state flags */
158 Glyph attr
; /* current char attributes */
164 /* CSI Escape sequence structs */
165 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
167 char buf
[ESC_BUF_SIZ
]; /* raw string */
168 int len
; /* raw string length */
170 int arg
[ESC_ARG_SIZ
];
171 int narg
; /* nb of args */
175 /* STR Escape sequence structs */
176 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
178 char type
; /* ESC type ... */
179 char buf
[STR_BUF_SIZ
]; /* raw string */
180 int len
; /* raw string length */
181 char *args
[STR_ARG_SIZ
];
182 int narg
; /* nb of args */
185 /* Internal representation of the screen */
187 int row
; /* nb row */
188 int col
; /* nb col */
189 Line
*line
; /* screen */
190 Line
*alt
; /* alternate screen */
191 bool *dirty
; /* dirtyness of lines */
192 TCursor c
; /* cursor */
193 int top
; /* top scroll limit */
194 int bot
; /* bottom scroll limit */
195 int mode
; /* terminal mode flags */
196 int esc
; /* escape state flags */
197 bool numlock
; /* lock numbers in keyboard */
201 /* Purely graphic info */
207 Atom xembed
, wmdeletewin
;
213 bool isfixed
; /* is fixed geometry? */
214 int fx
, fy
, fw
, fh
; /* fixed geometry */
215 int tw
, th
; /* tty width and height */
216 int w
; /* window width */
217 int h
; /* window height */
218 int ch
; /* char height */
219 int cw
; /* char width */
220 char state
; /* focus, redraw, visible */
227 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
228 signed char appkey
; /* application keypad */
229 signed char appcursor
; /* application cursor */
230 signed char crlf
; /* crlf mode */
233 /* TODO: use better name for vars... */
244 struct timeval tclick1
;
245 struct timeval tclick2
;
258 void (*func
)(const Arg
*);
262 /* function definitions used in config.h */
263 static void xzoom(const Arg
*);
264 static void selpaste(const Arg
*);
265 static void numlock(const Arg
*);
267 /* Config.h for applying patches and the configuration. */
283 /* Drawing Context */
285 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
286 Font font
, bfont
, ifont
, ibfont
;
289 static void die(const char *, ...);
290 static void draw(void);
291 static void redraw(void);
292 static void drawregion(int, int, int, int);
293 static void execsh(void);
294 static void sigchld(int);
295 static void run(void);
297 static void csidump(void);
298 static void csihandle(void);
299 static void csiparse(void);
300 static void csireset(void);
301 static void strdump(void);
302 static void strhandle(void);
303 static void strparse(void);
304 static void strreset(void);
306 static void tclearregion(int, int, int, int);
307 static void tcursor(int);
308 static void tdeletechar(int);
309 static void tdeleteline(int);
310 static void tinsertblank(int);
311 static void tinsertblankline(int);
312 static void tmoveto(int, int);
313 static void tmoveato(int x
, int y
);
314 static void tnew(int, int);
315 static void tnewline(int);
316 static void tputtab(bool);
317 static void tputc(char *, int);
318 static void treset(void);
319 static int tresize(int, int);
320 static void tscrollup(int, int);
321 static void tscrolldown(int, int);
322 static void tsetattr(int*, int);
323 static void tsetchar(char *, Glyph
*, int, int);
324 static void tsetscroll(int, int);
325 static void tswapscreen(void);
326 static void tsetdirt(int, int);
327 static void tsetmode(bool, bool, int *, int);
328 static void tfulldirt(void);
329 static void techo(char *, int);
331 static inline bool match(uint
, uint
);
332 static void ttynew(void);
333 static void ttyread(void);
334 static void ttyresize(void);
335 static void ttywrite(const char *, size_t);
337 static void xdraws(char *, Glyph
, int, int, int, int);
338 static void xhints(void);
339 static void xclear(int, int, int, int);
340 static void xdrawcursor(void);
341 static void xinit(void);
342 static void xloadcols(void);
343 static int xloadfont(Font
*, FcPattern
*);
344 static void xloadfonts(char *, int);
345 static void xresettitle(void);
346 static void xseturgency(int);
347 static void xsetsel(char*);
348 static void xtermclear(int, int, int, int);
349 static void xunloadfonts(void);
350 static void xresize(int, int);
352 static void expose(XEvent
*);
353 static void visibility(XEvent
*);
354 static void unmap(XEvent
*);
355 static char *kmap(KeySym
, uint
);
356 static void kpress(XEvent
*);
357 static void cmessage(XEvent
*);
358 static void cresize(int, int);
359 static void resize(XEvent
*);
360 static void focus(XEvent
*);
361 static void brelease(XEvent
*);
362 static void bpress(XEvent
*);
363 static void bmotion(XEvent
*);
364 static void selnotify(XEvent
*);
365 static void selclear(XEvent
*);
366 static void selrequest(XEvent
*);
368 static void selinit(void);
369 static inline bool selected(int, int);
370 static void selcopy(void);
371 static void selscroll(int, int);
373 static int utf8decode(char *, long *);
374 static int utf8encode(long *, char *);
375 static int utf8size(char *);
376 static int isfullutf8(char *, int);
378 static ssize_t
xwrite(int, char *, size_t);
379 static void *xmalloc(size_t);
380 static void *xrealloc(void *, size_t);
381 static void *xcalloc(size_t, size_t);
383 static void (*handler
[LASTEvent
])(XEvent
*) = {
385 [ClientMessage
] = cmessage
,
386 [ConfigureNotify
] = resize
,
387 [VisibilityNotify
] = visibility
,
388 [UnmapNotify
] = unmap
,
392 [MotionNotify
] = bmotion
,
393 [ButtonPress
] = bpress
,
394 [ButtonRelease
] = brelease
,
395 [SelectionClear
] = selclear
,
396 [SelectionNotify
] = selnotify
,
397 [SelectionRequest
] = selrequest
,
404 static CSIEscape csiescseq
;
405 static STREscape strescseq
;
408 static Selection sel
;
409 static int iofd
= -1;
410 static char **opt_cmd
= NULL
;
411 static char *opt_io
= NULL
;
412 static char *opt_title
= NULL
;
413 static char *opt_embed
= NULL
;
414 static char *opt_class
= NULL
;
415 static char *opt_font
= NULL
;
417 static char *usedfont
= NULL
;
418 static int usedfontsize
= 0;
420 /* Font Ring Cache */
435 * Fontcache is a ring buffer, with frccur as current position and frclen as
436 * the current length of used elements.
439 static Fontcache frc
[256];
440 static int frccur
= -1, frclen
= 0;
443 xwrite(int fd
, char *s
, size_t len
) {
447 ssize_t r
= write(fd
, s
, len
);
457 xmalloc(size_t len
) {
458 void *p
= malloc(len
);
461 die("Out of memory\n");
467 xrealloc(void *p
, size_t len
) {
468 if((p
= realloc(p
, len
)) == NULL
)
469 die("Out of memory\n");
475 xcalloc(size_t nmemb
, size_t size
) {
476 void *p
= calloc(nmemb
, size
);
479 die("Out of memory\n");
485 utf8decode(char *s
, long *u
) {
491 if(~c
& B7
) { /* 0xxxxxxx */
494 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
495 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
497 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
498 *u
= c
&(B3
|B2
|B1
|B0
);
500 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
507 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
509 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
512 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
515 if((n
== 1 && *u
< 0x80) ||
516 (n
== 2 && *u
< 0x800) ||
517 (n
== 3 && *u
< 0x10000) ||
518 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
530 utf8encode(long *u
, char *s
) {
538 *sp
= uc
; /* 0xxxxxxx */
540 } else if(*u
< 0x800) {
541 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
543 } else if(uc
< 0x10000) {
544 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
546 } else if(uc
<= 0x10FFFF) {
547 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
553 for(i
=n
,++sp
; i
>0; --i
,++sp
)
554 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
566 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
567 UTF-8 otherwise return 0 */
569 isfullutf8(char *s
, int b
) {
577 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
579 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
581 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
583 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
585 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
586 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
599 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
601 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
610 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
611 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
615 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
616 if(sel
.xtarget
== None
)
617 sel
.xtarget
= XA_STRING
;
625 return LIMIT(x
, 0, term
.col
-1);
633 return LIMIT(y
, 0, term
.row
-1);
637 selected(int x
, int y
) {
640 if(sel
.ey
== y
&& sel
.by
== y
) {
641 bx
= MIN(sel
.bx
, sel
.ex
);
642 ex
= MAX(sel
.bx
, sel
.ex
);
643 return BETWEEN(x
, bx
, ex
);
646 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
647 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
648 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
649 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
653 getbuttoninfo(XEvent
*e
) {
654 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
656 sel
.ex
= x2col(e
->xbutton
.x
);
657 sel
.ey
= y2row(e
->xbutton
.y
);
659 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
660 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
661 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
662 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
666 mousereport(XEvent
*e
) {
667 int x
= x2col(e
->xbutton
.x
);
668 int y
= y2row(e
->xbutton
.y
);
669 int button
= e
->xbutton
.button
;
670 int state
= e
->xbutton
.state
;
671 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
672 static int ob
, ox
, oy
;
675 if(e
->xbutton
.type
== MotionNotify
) {
676 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
680 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
686 if(e
->xbutton
.type
== ButtonPress
) {
692 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
693 + (state
& Mod4Mask
? 8 : 0)
694 + (state
& ControlMask
? 16 : 0);
696 ttywrite(buf
, sizeof(buf
));
701 if(IS_SET(MODE_MOUSE
)) {
703 } else if(e
->xbutton
.button
== Button1
) {
706 tsetdirt(sel
.b
.y
, sel
.e
.y
);
710 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
711 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
712 } else if(e
->xbutton
.button
== Button4
) {
714 } else if(e
->xbutton
.button
== Button5
) {
722 int x
, y
, bufsize
, is_selected
= 0, size
;
728 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
729 ptr
= str
= xmalloc(bufsize
);
731 /* append every set & selected glyph to the selection */
732 for(y
= 0; y
< term
.row
; y
++) {
733 gp
= &term
.line
[y
][0];
734 last
= gp
+ term
.col
;
736 while(--last
>= gp
&& !(last
->state
& GLYPH_SET
))
739 for(x
= 0; gp
<= last
; x
++, ++gp
) {
740 if(!(is_selected
= selected(x
, y
)))
743 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
745 memcpy(ptr
, p
, size
);
748 /* \n at the end of every selected line except for the last one */
749 if(is_selected
&& y
< sel
.e
.y
)
758 selnotify(XEvent
*e
) {
759 ulong nitems
, ofs
, rem
;
766 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
767 False
, AnyPropertyType
, &type
, &format
,
768 &nitems
, &rem
, &data
)) {
769 fprintf(stderr
, "Clipboard allocation failed\n");
772 ttywrite((const char *) data
, nitems
* format
/ 8);
774 /* number of 32-bit chunks returned */
775 ofs
+= nitems
* format
/ 32;
780 selpaste(const Arg
*dummy
) {
781 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
782 xw
.win
, CurrentTime
);
785 void selclear(XEvent
*e
) {
789 tsetdirt(sel
.b
.y
, sel
.e
.y
);
793 selrequest(XEvent
*e
) {
794 XSelectionRequestEvent
*xsre
;
796 Atom xa_targets
, string
;
798 xsre
= (XSelectionRequestEvent
*) e
;
799 xev
.type
= SelectionNotify
;
800 xev
.requestor
= xsre
->requestor
;
801 xev
.selection
= xsre
->selection
;
802 xev
.target
= xsre
->target
;
803 xev
.time
= xsre
->time
;
807 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
808 if(xsre
->target
== xa_targets
) {
809 /* respond with the supported type */
810 string
= sel
.xtarget
;
811 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
812 XA_ATOM
, 32, PropModeReplace
,
813 (uchar
*) &string
, 1);
814 xev
.property
= xsre
->property
;
815 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
816 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
817 xsre
->target
, 8, PropModeReplace
,
818 (uchar
*) sel
.clip
, strlen(sel
.clip
));
819 xev
.property
= xsre
->property
;
822 /* all done, send a notification to the listener */
823 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
824 fprintf(stderr
, "Error sending SelectionNotify event\n");
829 /* register the selection for both the clipboard and the primary */
835 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
837 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
838 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
842 brelease(XEvent
*e
) {
845 if(IS_SET(MODE_MOUSE
)) {
850 if(e
->xbutton
.button
== Button2
) {
852 } else if(e
->xbutton
.button
== Button1
) {
855 term
.dirty
[sel
.ey
] = 1;
856 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
858 gettimeofday(&now
, NULL
);
860 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
861 /* triple click on the line */
862 sel
.b
.x
= sel
.bx
= 0;
863 sel
.e
.x
= sel
.ex
= term
.col
;
864 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
866 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
867 /* double click to select word */
869 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
870 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
874 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
875 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
879 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
887 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
888 gettimeofday(&sel
.tclick1
, NULL
);
893 int starty
, endy
, oldey
, oldex
;
895 if(IS_SET(MODE_MOUSE
)) {
907 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
908 starty
= MIN(oldey
, sel
.ey
);
909 endy
= MAX(oldey
, sel
.ey
);
910 tsetdirt(starty
, endy
);
915 die(const char *errstr
, ...) {
918 va_start(ap
, errstr
);
919 vfprintf(stderr
, errstr
, ap
);
927 char *envshell
= getenv("SHELL");
928 const struct passwd
*pass
= getpwuid(getuid());
929 char buf
[sizeof(long) * 8 + 1];
936 setenv("LOGNAME", pass
->pw_name
, 1);
937 setenv("USER", pass
->pw_name
, 1);
938 setenv("SHELL", pass
->pw_shell
, 0);
939 setenv("HOME", pass
->pw_dir
, 0);
942 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
943 setenv("WINDOWID", buf
, 1);
945 signal(SIGCHLD
, SIG_DFL
);
946 signal(SIGHUP
, SIG_DFL
);
947 signal(SIGINT
, SIG_DFL
);
948 signal(SIGQUIT
, SIG_DFL
);
949 signal(SIGTERM
, SIG_DFL
);
950 signal(SIGALRM
, SIG_DFL
);
952 DEFAULT(envshell
, shell
);
953 setenv("TERM", termname
, 1);
954 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
955 execvp(args
[0], args
);
963 if(waitpid(pid
, &stat
, 0) < 0)
964 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
966 if(WIFEXITED(stat
)) {
967 exit(WEXITSTATUS(stat
));
976 struct winsize w
= {term
.row
, term
.col
, 0, 0};
978 /* seems to work fine on linux, openbsd and freebsd */
979 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
980 die("openpty failed: %s\n", SERRNO
);
982 switch(pid
= fork()) {
984 die("fork failed\n");
987 setsid(); /* create a new process group */
988 dup2(s
, STDIN_FILENO
);
989 dup2(s
, STDOUT_FILENO
);
990 dup2(s
, STDERR_FILENO
);
991 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
992 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1000 signal(SIGCHLD
, sigchld
);
1002 iofd
= (!strcmp(opt_io
, "-")) ?
1004 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1006 fprintf(stderr
, "Error opening %s:%s\n",
1007 opt_io
, strerror(errno
));
1017 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1019 fprintf(stderr
, "\n");
1024 static char buf
[BUFSIZ
];
1025 static int buflen
= 0;
1028 int charsize
; /* size of utf8 char in bytes */
1032 /* append read bytes to unprocessed bytes */
1033 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1034 die("Couldn't read from shell: %s\n", SERRNO
);
1036 /* process every complete utf8 char */
1039 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1040 charsize
= utf8decode(ptr
, &utf8c
);
1041 utf8encode(&utf8c
, s
);
1047 /* keep any uncomplete utf8 char for the next call */
1048 memmove(buf
, ptr
, buflen
);
1052 ttywrite(const char *s
, size_t n
) {
1053 if(write(cmdfd
, s
, n
) == -1)
1054 die("write error on tty: %s\n", SERRNO
);
1061 w
.ws_row
= term
.row
;
1062 w
.ws_col
= term
.col
;
1063 w
.ws_xpixel
= xw
.tw
;
1064 w
.ws_ypixel
= xw
.th
;
1065 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1066 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1070 tsetdirt(int top
, int bot
) {
1073 LIMIT(top
, 0, term
.row
-1);
1074 LIMIT(bot
, 0, term
.row
-1);
1076 for(i
= top
; i
<= bot
; i
++)
1082 tsetdirt(0, term
.row
-1);
1089 if(mode
== CURSOR_SAVE
) {
1091 } else if(mode
== CURSOR_LOAD
) {
1101 term
.c
= (TCursor
){{
1105 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1107 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1108 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1111 term
.bot
= term
.row
- 1;
1112 term
.mode
= MODE_WRAP
;
1114 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1116 tcursor(CURSOR_SAVE
);
1120 tnew(int col
, int row
) {
1121 /* set screen size */
1124 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1125 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1126 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1127 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1129 for(row
= 0; row
< term
.row
; row
++) {
1130 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1131 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1132 term
.dirty
[row
] = 0;
1136 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1143 Line
*tmp
= term
.line
;
1145 term
.line
= term
.alt
;
1147 term
.mode
^= MODE_ALTSCREEN
;
1152 tscrolldown(int orig
, int n
) {
1156 LIMIT(n
, 0, term
.bot
-orig
+1);
1158 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1160 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1161 temp
= term
.line
[i
];
1162 term
.line
[i
] = term
.line
[i
-n
];
1163 term
.line
[i
-n
] = temp
;
1166 term
.dirty
[i
-n
] = 1;
1173 tscrollup(int orig
, int n
) {
1176 LIMIT(n
, 0, term
.bot
-orig
+1);
1178 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1180 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1181 temp
= term
.line
[i
];
1182 term
.line
[i
] = term
.line
[i
+n
];
1183 term
.line
[i
+n
] = temp
;
1186 term
.dirty
[i
+n
] = 1;
1189 selscroll(orig
, -n
);
1193 selscroll(int orig
, int n
) {
1197 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1198 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1202 if(sel
.by
< term
.top
) {
1206 if(sel
.ey
> term
.bot
) {
1210 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1211 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1216 tnewline(int first_col
) {
1220 tscrollup(term
.top
, 1);
1224 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1229 /* int noarg = 1; */
1230 char *p
= csiescseq
.buf
;
1234 csiescseq
.priv
= 1, p
++;
1236 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1237 while(isdigit(*p
)) {
1238 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1239 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1241 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1242 csiescseq
.narg
++, p
++;
1244 csiescseq
.mode
= *p
;
1252 /* for absolute user moves, when decom is set */
1254 tmoveato(int x
, int y
) {
1255 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1259 tmoveto(int x
, int y
) {
1262 if(term
.c
.state
& CURSOR_ORIGIN
) {
1267 maxy
= term
.row
- 1;
1269 LIMIT(x
, 0, term
.col
-1);
1270 LIMIT(y
, miny
, maxy
);
1271 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1277 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1278 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1279 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1280 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1281 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1282 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1283 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1284 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1285 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1286 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1290 * The table is proudly stolen from rxvt.
1292 if(attr
->mode
& ATTR_GFX
) {
1293 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1294 && vt100_0
[c
[0] - 0x41]) {
1295 c
= vt100_0
[c
[0] - 0x41];
1300 term
.line
[y
][x
] = *attr
;
1301 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1302 term
.line
[y
][x
].state
|= GLYPH_SET
;
1306 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1310 temp
= x1
, x1
= x2
, x2
= temp
;
1312 temp
= y1
, y1
= y2
, y2
= temp
;
1314 LIMIT(x1
, 0, term
.col
-1);
1315 LIMIT(x2
, 0, term
.col
-1);
1316 LIMIT(y1
, 0, term
.row
-1);
1317 LIMIT(y2
, 0, term
.row
-1);
1319 for(y
= y1
; y
<= y2
; y
++) {
1321 for(x
= x1
; x
<= x2
; x
++)
1322 term
.line
[y
][x
].state
= 0;
1327 tdeletechar(int n
) {
1328 int src
= term
.c
.x
+ n
;
1330 int size
= term
.col
- src
;
1332 term
.dirty
[term
.c
.y
] = 1;
1334 if(src
>= term
.col
) {
1335 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1339 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1340 size
* sizeof(Glyph
));
1341 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1345 tinsertblank(int n
) {
1348 int size
= term
.col
- dst
;
1350 term
.dirty
[term
.c
.y
] = 1;
1352 if(dst
>= term
.col
) {
1353 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1357 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1358 size
* sizeof(Glyph
));
1359 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1363 tinsertblankline(int n
) {
1364 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1367 tscrolldown(term
.c
.y
, n
);
1371 tdeleteline(int n
) {
1372 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1375 tscrollup(term
.c
.y
, n
);
1379 tsetattr(int *attr
, int l
) {
1382 for(i
= 0; i
< l
; i
++) {
1385 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1386 | ATTR_ITALIC
| ATTR_BLINK
);
1387 term
.c
.attr
.fg
= defaultfg
;
1388 term
.c
.attr
.bg
= defaultbg
;
1391 term
.c
.attr
.mode
|= ATTR_BOLD
;
1393 case 3: /* enter standout (highlight) */
1394 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1397 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1400 term
.c
.attr
.mode
|= ATTR_BLINK
;
1403 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1407 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1409 case 23: /* leave standout (highlight) mode */
1410 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1413 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1416 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1419 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1422 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1424 if(BETWEEN(attr
[i
], 0, 255)) {
1425 term
.c
.attr
.fg
= attr
[i
];
1428 "erresc: bad fgcolor %d\n",
1433 "erresc(38): gfx attr %d unknown\n",
1438 term
.c
.attr
.fg
= defaultfg
;
1441 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1443 if(BETWEEN(attr
[i
], 0, 255)) {
1444 term
.c
.attr
.bg
= attr
[i
];
1447 "erresc: bad bgcolor %d\n",
1452 "erresc(48): gfx attr %d unknown\n",
1457 term
.c
.attr
.bg
= defaultbg
;
1460 if(BETWEEN(attr
[i
], 30, 37)) {
1461 term
.c
.attr
.fg
= attr
[i
] - 30;
1462 } else if(BETWEEN(attr
[i
], 40, 47)) {
1463 term
.c
.attr
.bg
= attr
[i
] - 40;
1464 } else if(BETWEEN(attr
[i
], 90, 97)) {
1465 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1466 } else if(BETWEEN(attr
[i
], 100, 107)) {
1467 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1470 "erresc(default): gfx attr %d unknown\n",
1471 attr
[i
]), csidump();
1479 tsetscroll(int t
, int b
) {
1482 LIMIT(t
, 0, term
.row
-1);
1483 LIMIT(b
, 0, term
.row
-1);
1493 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1496 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1500 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1504 case 1: /* DECCKM -- Cursor key */
1505 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1507 case 5: /* DECSCNM -- Reverse video */
1509 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1510 if(mode
!= term
.mode
)
1513 case 6: /* DECOM -- Origin */
1514 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1517 case 7: /* DECAWM -- Auto wrap */
1518 MODBIT(term
.mode
, set
, MODE_WRAP
);
1520 case 0: /* Error (IGNORED) */
1521 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1522 case 3: /* DECCOLM -- Column (IGNORED) */
1523 case 4: /* DECSCLM -- Scroll (IGNORED) */
1524 case 8: /* DECARM -- Auto repeat (IGNORED) */
1525 case 18: /* DECPFF -- Printer feed (IGNORED) */
1526 case 19: /* DECPEX -- Printer extent (IGNORED) */
1527 case 42: /* DECNRCM -- National characters (IGNORED) */
1528 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1530 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1531 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1533 case 1000: /* 1000,1002: enable xterm mouse report */
1534 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1537 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1539 case 1049: /* = 1047 and 1048 */
1542 alt
= IS_SET(MODE_ALTSCREEN
);
1544 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1545 if(set
^ alt
) /* set is always 1 or 0 */
1552 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1556 "erresc: unknown private set/reset mode %d\n",
1562 case 0: /* Error (IGNORED) */
1564 case 2: /* KAM -- keyboard action */
1565 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1567 case 4: /* IRM -- Insertion-replacement */
1568 MODBIT(term
.mode
, set
, MODE_INSERT
);
1570 case 12: /* SRM -- Send/Receive */
1571 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1573 case 20: /* LNM -- Linefeed/new line */
1574 MODBIT(term
.mode
, set
, MODE_CRLF
);
1578 "erresc: unknown set/reset mode %d\n",
1590 switch(csiescseq
.mode
) {
1593 fprintf(stderr
, "erresc: unknown csi ");
1597 case '@': /* ICH -- Insert <n> blank char */
1598 DEFAULT(csiescseq
.arg
[0], 1);
1599 tinsertblank(csiescseq
.arg
[0]);
1601 case 'A': /* CUU -- Cursor <n> Up */
1602 DEFAULT(csiescseq
.arg
[0], 1);
1603 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1605 case 'B': /* CUD -- Cursor <n> Down */
1606 case 'e': /* VPR --Cursor <n> Down */
1607 DEFAULT(csiescseq
.arg
[0], 1);
1608 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1610 case 'c': /* DA -- Device Attributes */
1611 if(csiescseq
.arg
[0] == 0)
1612 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1614 case 'C': /* CUF -- Cursor <n> Forward */
1615 case 'a': /* HPR -- Cursor <n> Forward */
1616 DEFAULT(csiescseq
.arg
[0], 1);
1617 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1619 case 'D': /* CUB -- Cursor <n> Backward */
1620 DEFAULT(csiescseq
.arg
[0], 1);
1621 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1623 case 'E': /* CNL -- Cursor <n> Down and first col */
1624 DEFAULT(csiescseq
.arg
[0], 1);
1625 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1627 case 'F': /* CPL -- Cursor <n> Up and first col */
1628 DEFAULT(csiescseq
.arg
[0], 1);
1629 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1631 case 'g': /* TBC -- Tabulation clear */
1632 switch (csiescseq
.arg
[0]) {
1633 case 0: /* clear current tab stop */
1634 term
.tabs
[term
.c
.x
] = 0;
1636 case 3: /* clear all the tabs */
1637 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1643 case 'G': /* CHA -- Move to <col> */
1645 DEFAULT(csiescseq
.arg
[0], 1);
1646 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1648 case 'H': /* CUP -- Move to <row> <col> */
1650 DEFAULT(csiescseq
.arg
[0], 1);
1651 DEFAULT(csiescseq
.arg
[1], 1);
1652 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1654 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1655 DEFAULT(csiescseq
.arg
[0], 1);
1656 while(csiescseq
.arg
[0]--)
1659 case 'J': /* ED -- Clear screen */
1661 switch(csiescseq
.arg
[0]) {
1663 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1664 if(term
.c
.y
< term
.row
-1)
1665 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1669 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1670 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1673 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1679 case 'K': /* EL -- Clear line */
1680 switch(csiescseq
.arg
[0]) {
1682 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1685 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1688 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1692 case 'S': /* SU -- Scroll <n> line up */
1693 DEFAULT(csiescseq
.arg
[0], 1);
1694 tscrollup(term
.top
, csiescseq
.arg
[0]);
1696 case 'T': /* SD -- Scroll <n> line down */
1697 DEFAULT(csiescseq
.arg
[0], 1);
1698 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1700 case 'L': /* IL -- Insert <n> blank lines */
1701 DEFAULT(csiescseq
.arg
[0], 1);
1702 tinsertblankline(csiescseq
.arg
[0]);
1704 case 'l': /* RM -- Reset Mode */
1705 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1707 case 'M': /* DL -- Delete <n> lines */
1708 DEFAULT(csiescseq
.arg
[0], 1);
1709 tdeleteline(csiescseq
.arg
[0]);
1711 case 'X': /* ECH -- Erase <n> char */
1712 DEFAULT(csiescseq
.arg
[0], 1);
1713 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1715 case 'P': /* DCH -- Delete <n> char */
1716 DEFAULT(csiescseq
.arg
[0], 1);
1717 tdeletechar(csiescseq
.arg
[0]);
1719 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1720 DEFAULT(csiescseq
.arg
[0], 1);
1721 while(csiescseq
.arg
[0]--)
1724 case 'd': /* VPA -- Move to <row> */
1725 DEFAULT(csiescseq
.arg
[0], 1);
1726 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1728 case 'h': /* SM -- Set terminal mode */
1729 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1731 case 'm': /* SGR -- Terminal attribute (color) */
1732 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1734 case 'r': /* DECSTBM -- Set Scrolling Region */
1735 if(csiescseq
.priv
) {
1738 DEFAULT(csiescseq
.arg
[0], 1);
1739 DEFAULT(csiescseq
.arg
[1], term
.row
);
1740 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1744 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1745 tcursor(CURSOR_SAVE
);
1747 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1748 tcursor(CURSOR_LOAD
);
1759 for(i
= 0; i
< csiescseq
.len
; i
++) {
1760 c
= csiescseq
.buf
[i
] & 0xff;
1763 } else if(c
== '\n') {
1765 } else if(c
== '\r') {
1767 } else if(c
== 0x1b) {
1770 printf("(%02x)", c
);
1778 memset(&csiescseq
, 0, sizeof(csiescseq
));
1786 * TODO: make this being useful in case of color palette change.
1792 switch(strescseq
.type
) {
1793 case ']': /* OSC -- Operating System Command */
1799 * TODO: Handle special chars in string, like umlauts.
1802 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1806 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1808 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1811 fprintf(stderr
, "erresc: unknown str ");
1816 case 'k': /* old title set compatibility */
1817 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1819 case 'P': /* DSC -- Device Control String */
1820 case '_': /* APC -- Application Program Command */
1821 case '^': /* PM -- Privacy Message */
1823 fprintf(stderr
, "erresc: unknown str ");
1833 * TODO: Implement parsing like for CSI when required.
1834 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1844 printf("ESC%c", strescseq
.type
);
1845 for(i
= 0; i
< strescseq
.len
; i
++) {
1846 c
= strescseq
.buf
[i
] & 0xff;
1849 } else if(c
== '\n') {
1851 } else if(c
== '\r') {
1853 } else if(c
== 0x1b) {
1856 printf("(%02x)", c
);
1864 memset(&strescseq
, 0, sizeof(strescseq
));
1868 tputtab(bool forward
) {
1874 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1879 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1882 tmoveto(x
, term
.c
.y
);
1886 techo(char *buf
, int len
) {
1887 for(; len
> 0; buf
++, len
--) {
1890 if(c
== '\033') { /* escape */
1893 } else if (c
< '\x20') { /* control code */
1894 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
1908 tputc(char *c
, int len
) {
1910 bool control
= ascii
< '\x20' || ascii
== 0177;
1913 if (xwrite(iofd
, c
, len
) < 0) {
1914 fprintf(stderr
, "Error writting in %s:%s\n",
1915 opt_io
, strerror(errno
));
1921 * STR sequences must be checked before anything else
1922 * because it can use some control codes as part of the sequence.
1924 if(term
.esc
& ESC_STR
) {
1927 term
.esc
= ESC_START
| ESC_STR_END
;
1929 case '\a': /* backwards compatibility to xterm */
1934 strescseq
.buf
[strescseq
.len
++] = ascii
;
1935 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1944 * Actions of control codes must be performed as soon they arrive
1945 * because they can be embedded inside a control sequence, and
1946 * they must not cause conflicts with sequences.
1954 tmoveto(term
.c
.x
-1, term
.c
.y
);
1957 tmoveto(0, term
.c
.y
);
1962 /* go to first col if the mode is set */
1963 tnewline(IS_SET(MODE_CRLF
));
1965 case '\a': /* BEL */
1966 if(!(xw
.state
& WIN_FOCUSED
))
1969 case '\033': /* ESC */
1971 term
.esc
= ESC_START
;
1973 case '\016': /* SO */
1974 term
.c
.attr
.mode
|= ATTR_GFX
;
1976 case '\017': /* SI */
1977 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1979 case '\032': /* SUB */
1980 case '\030': /* CAN */
1983 case '\005': /* ENQ (IGNORED) */
1984 case '\000': /* NUL (IGNORED) */
1985 case '\021': /* XON (IGNORED) */
1986 case '\023': /* XOFF (IGNORED) */
1987 case 0177: /* DEL (IGNORED) */
1990 } else if(term
.esc
& ESC_START
) {
1991 if(term
.esc
& ESC_CSI
) {
1992 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1993 if(BETWEEN(ascii
, 0x40, 0x7E)
1994 || csiescseq
.len
>= ESC_BUF_SIZ
) {
1996 csiparse(), csihandle();
1998 } else if(term
.esc
& ESC_STR_END
) {
2002 } else if(term
.esc
& ESC_ALTCHARSET
) {
2004 case '0': /* Line drawing set */
2005 term
.c
.attr
.mode
|= ATTR_GFX
;
2007 case 'B': /* USASCII */
2008 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2010 case 'A': /* UK (IGNORED) */
2011 case '<': /* multinational charset (IGNORED) */
2012 case '5': /* Finnish (IGNORED) */
2013 case 'C': /* Finnish (IGNORED) */
2014 case 'K': /* German (IGNORED) */
2017 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2020 } else if(term
.esc
& ESC_TEST
) {
2021 if(ascii
== '8') { /* DEC screen alignment test. */
2022 char E
[UTF_SIZ
] = "E";
2025 for(x
= 0; x
< term
.col
; ++x
) {
2026 for(y
= 0; y
< term
.row
; ++y
)
2027 tsetchar(E
, &term
.c
.attr
, x
, y
);
2034 term
.esc
|= ESC_CSI
;
2037 term
.esc
|= ESC_TEST
;
2039 case 'P': /* DCS -- Device Control String */
2040 case '_': /* APC -- Application Program Command */
2041 case '^': /* PM -- Privacy Message */
2042 case ']': /* OSC -- Operating System Command */
2043 case 'k': /* old title set compatibility */
2045 strescseq
.type
= ascii
;
2046 term
.esc
|= ESC_STR
;
2048 case '(': /* set primary charset G0 */
2049 term
.esc
|= ESC_ALTCHARSET
;
2051 case ')': /* set secondary charset G1 (IGNORED) */
2052 case '*': /* set tertiary charset G2 (IGNORED) */
2053 case '+': /* set quaternary charset G3 (IGNORED) */
2056 case 'D': /* IND -- Linefeed */
2057 if(term
.c
.y
== term
.bot
) {
2058 tscrollup(term
.top
, 1);
2060 tmoveto(term
.c
.x
, term
.c
.y
+1);
2064 case 'E': /* NEL -- Next line */
2065 tnewline(1); /* always go to first col */
2068 case 'H': /* HTS -- Horizontal tab stop */
2069 term
.tabs
[term
.c
.x
] = 1;
2072 case 'M': /* RI -- Reverse index */
2073 if(term
.c
.y
== term
.top
) {
2074 tscrolldown(term
.top
, 1);
2076 tmoveto(term
.c
.x
, term
.c
.y
-1);
2080 case 'Z': /* DECID -- Identify Terminal */
2081 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2084 case 'c': /* RIS -- Reset to inital state */
2089 case '=': /* DECPAM -- Application keypad */
2090 term
.mode
|= MODE_APPKEYPAD
;
2093 case '>': /* DECPNM -- Normal keypad */
2094 term
.mode
&= ~MODE_APPKEYPAD
;
2097 case '7': /* DECSC -- Save Cursor */
2098 tcursor(CURSOR_SAVE
);
2101 case '8': /* DECRC -- Restore Cursor */
2102 tcursor(CURSOR_LOAD
);
2105 case '\\': /* ST -- Stop */
2109 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2110 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2115 * All characters which form part of a sequence are not
2121 * Display control codes only if we are in graphic mode
2123 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2125 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2127 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2128 tnewline(1); /* always go to first col */
2130 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2131 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2132 &term
.line
[term
.c
.y
][term
.c
.x
],
2133 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2136 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2137 if(term
.c
.x
+1 < term
.col
) {
2138 tmoveto(term
.c
.x
+1, term
.c
.y
);
2140 term
.c
.state
|= CURSOR_WRAPNEXT
;
2145 tresize(int col
, int row
) {
2147 int minrow
= MIN(row
, term
.row
);
2148 int mincol
= MIN(col
, term
.col
);
2149 int slide
= term
.c
.y
- row
+ 1;
2152 if(col
< 1 || row
< 1)
2155 /* free unneeded rows */
2158 /* slide screen to keep cursor where we expect it -
2159 * tscrollup would work here, but we can optimize to
2160 * memmove because we're freeing the earlier lines */
2161 for(/* i = 0 */; i
< slide
; i
++) {
2165 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2166 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2168 for(i
+= row
; i
< term
.row
; i
++) {
2173 /* resize to new height */
2174 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2175 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2176 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2177 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2179 /* resize each row to new width, zero-pad if needed */
2180 for(i
= 0; i
< minrow
; i
++) {
2182 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2183 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2184 for(x
= mincol
; x
< col
; x
++) {
2185 term
.line
[i
][x
].state
= 0;
2186 term
.alt
[i
][x
].state
= 0;
2190 /* allocate any new rows */
2191 for(/* i == minrow */; i
< row
; i
++) {
2193 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2194 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2196 if(col
> term
.col
) {
2197 bp
= term
.tabs
+ term
.col
;
2199 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2200 while(--bp
> term
.tabs
&& !*bp
)
2202 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2205 /* update terminal size */
2208 /* reset scrolling region */
2209 tsetscroll(0, row
-1);
2210 /* make use of the LIMIT in tmoveto */
2211 tmoveto(term
.c
.x
, term
.c
.y
);
2217 xresize(int col
, int row
) {
2218 xw
.tw
= MAX(1, col
* xw
.cw
);
2219 xw
.th
= MAX(1, row
* xw
.ch
);
2221 XftDrawChange(xw
.draw
, xw
.buf
);
2227 XRenderColor color
= { .alpha
= 0 };
2229 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2230 for(i
= 0; i
< LEN(colorname
); i
++) {
2233 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2234 die("Could not allocate color '%s'\n", colorname
[i
]);
2238 /* load colors [16-255] ; same colors as xterm */
2239 for(i
= 16, r
= 0; r
< 6; r
++) {
2240 for(g
= 0; g
< 6; g
++) {
2241 for(b
= 0; b
< 6; b
++) {
2242 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2243 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2244 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2245 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2246 die("Could not allocate color %d\n", i
);
2253 for(r
= 0; r
< 24; r
++, i
++) {
2254 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2255 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2257 die("Could not allocate color %d\n", i
);
2263 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2264 XftDrawRect(xw
.draw
,
2265 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2266 borderpx
+ col1
* xw
.cw
,
2267 borderpx
+ row1
* xw
.ch
,
2268 (col2
-col1
+1) * xw
.cw
,
2269 (row2
-row1
+1) * xw
.ch
);
2273 * Absolute coordinates.
2276 xclear(int x1
, int y1
, int x2
, int y2
) {
2277 XftDrawRect(xw
.draw
,
2278 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2279 x1
, y1
, x2
-x1
, y2
-y1
);
2284 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2285 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2286 XSizeHints
*sizeh
= NULL
;
2288 sizeh
= XAllocSizeHints();
2289 if(xw
.isfixed
== False
) {
2290 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2291 sizeh
->height
= xw
.h
;
2292 sizeh
->width
= xw
.w
;
2293 sizeh
->height_inc
= xw
.ch
;
2294 sizeh
->width_inc
= xw
.cw
;
2295 sizeh
->base_height
= 2 * borderpx
;
2296 sizeh
->base_width
= 2 * borderpx
;
2298 sizeh
->flags
= PMaxSize
| PMinSize
;
2299 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2300 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2303 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2308 xloadfont(Font
*f
, FcPattern
*pattern
) {
2312 match
= FcFontMatch(NULL
, pattern
, &result
);
2316 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2317 FcPatternDestroy(match
);
2321 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2322 FcPatternDestroy(match
);
2326 f
->pattern
= FcPatternDuplicate(pattern
);
2328 f
->ascent
= f
->match
->ascent
;
2329 f
->descent
= f
->match
->descent
;
2331 f
->rbearing
= f
->match
->max_advance_width
;
2333 f
->height
= f
->match
->height
;
2334 f
->width
= f
->lbearing
+ f
->rbearing
;
2340 xloadfonts(char *fontstr
, int fontsize
) {
2345 if(fontstr
[0] == '-') {
2346 pattern
= XftXlfdParse(fontstr
, False
, False
);
2348 pattern
= FcNameParse((FcChar8
*)fontstr
);
2352 die("st: can't open font %s\n", fontstr
);
2355 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2356 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2357 usedfontsize
= fontsize
;
2359 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2360 if(result
== FcResultMatch
) {
2361 usedfontsize
= (int)fontval
;
2364 * Default font size is 12, if none given. This is to
2365 * have a known usedfontsize value.
2367 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2372 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2373 FcDefaultSubstitute(pattern
);
2375 if(xloadfont(&dc
.font
, pattern
))
2376 die("st: can't open font %s\n", fontstr
);
2378 /* Setting character width and height. */
2379 xw
.cw
= dc
.font
.width
;
2380 xw
.ch
= dc
.font
.height
;
2382 FcPatternDel(pattern
, FC_WEIGHT
);
2383 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2384 if(xloadfont(&dc
.bfont
, pattern
))
2385 die("st: can't open font %s\n", fontstr
);
2387 FcPatternDel(pattern
, FC_SLANT
);
2388 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2389 if(xloadfont(&dc
.ibfont
, pattern
))
2390 die("st: can't open font %s\n", fontstr
);
2392 FcPatternDel(pattern
, FC_WEIGHT
);
2393 if(xloadfont(&dc
.ifont
, pattern
))
2394 die("st: can't open font %s\n", fontstr
);
2396 FcPatternDestroy(pattern
);
2405 * Free the loaded fonts in the font cache. This is done backwards
2408 for (i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2411 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2416 XftFontClose(xw
.dpy
, dc
.font
.match
);
2417 FcPatternDestroy(dc
.font
.pattern
);
2418 FcFontSetDestroy(dc
.font
.set
);
2419 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2420 FcPatternDestroy(dc
.bfont
.pattern
);
2421 FcFontSetDestroy(dc
.bfont
.set
);
2422 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2423 FcPatternDestroy(dc
.ifont
.pattern
);
2424 FcFontSetDestroy(dc
.ifont
.set
);
2425 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2426 FcPatternDestroy(dc
.ibfont
.pattern
);
2427 FcFontSetDestroy(dc
.ibfont
.set
);
2431 xzoom(const Arg
*arg
)
2434 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2441 XSetWindowAttributes attrs
;
2444 int sw
, sh
, major
, minor
;
2446 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2447 die("Can't open display\n");
2448 xw
.scr
= XDefaultScreen(xw
.dpy
);
2449 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2453 die("Could not init fontconfig.\n");
2455 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2456 xloadfonts(usedfont
, 0);
2459 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2462 /* adjust fixed window geometry */
2464 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2465 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2467 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2469 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2474 /* window - default size */
2475 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2476 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2481 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2482 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2483 attrs
.bit_gravity
= NorthWestGravity
;
2484 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2485 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2486 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2487 attrs
.colormap
= xw
.cmap
;
2489 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2490 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2491 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2493 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2497 /* double buffering */
2498 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2499 die("Xdbe extension is not present\n");
2500 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2502 /* Xft rendering context */
2503 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2506 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2508 die("XOpenIM failed. Could not open input device.\n");
2509 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2510 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2511 XNFocusWindow
, xw
.win
, NULL
);
2513 die("XCreateIC failed. Could not obtain input method.\n");
2515 /* white cursor, black outline */
2516 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2517 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2518 XRecolorCursor(xw
.dpy
, cursor
,
2519 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2520 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2522 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2523 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2524 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2527 XMapWindow(xw
.dpy
, xw
.win
);
2533 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2534 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2535 width
= charlen
* xw
.cw
, xp
, i
;
2537 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2540 Font
*font
= &dc
.font
;
2542 FcPattern
*fcpattern
, *fontpattern
;
2543 FcFontSet
*fcsets
[] = { NULL
};
2544 FcCharSet
*fccharset
;
2545 Colour
*fg
= &dc
.col
[base
.fg
], *bg
= &dc
.col
[base
.bg
],
2546 *temp
, revfg
, revbg
;
2547 XRenderColor colfg
, colbg
;
2549 frcflags
= FRC_NORMAL
;
2551 if(base
.mode
& ATTR_BOLD
) {
2552 if(BETWEEN(base
.fg
, 0, 7)) {
2553 /* basic system colors */
2554 fg
= &dc
.col
[base
.fg
+ 8];
2555 } else if(BETWEEN(base
.fg
, 16, 195)) {
2557 fg
= &dc
.col
[base
.fg
+ 36];
2558 } else if(BETWEEN(base
.fg
, 232, 251)) {
2560 fg
= &dc
.col
[base
.fg
+ 4];
2563 * Those ranges will not be brightened:
2564 * 8 - 15 – bright system colors
2565 * 196 - 231 – highest 256 color cube
2566 * 252 - 255 – brightest colors in greyscale
2569 frcflags
= FRC_BOLD
;
2572 if(base
.mode
& ATTR_ITALIC
) {
2574 frcflags
= FRC_ITALIC
;
2576 if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2578 frcflags
= FRC_ITALICBOLD
;
2581 if(IS_SET(MODE_REVERSE
)) {
2582 if(fg
== &dc
.col
[defaultfg
]) {
2583 fg
= &dc
.col
[defaultbg
];
2585 colfg
.red
= ~fg
->color
.red
;
2586 colfg
.green
= ~fg
->color
.green
;
2587 colfg
.blue
= ~fg
->color
.blue
;
2588 colfg
.alpha
= fg
->color
.alpha
;
2589 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2593 if(bg
== &dc
.col
[defaultbg
]) {
2594 bg
= &dc
.col
[defaultfg
];
2596 colbg
.red
= ~bg
->color
.red
;
2597 colbg
.green
= ~bg
->color
.green
;
2598 colbg
.blue
= ~bg
->color
.blue
;
2599 colbg
.alpha
= bg
->color
.alpha
;
2600 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2605 if(base
.mode
& ATTR_REVERSE
) {
2611 /* Intelligent cleaning up of the borders. */
2613 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2614 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2616 if(x
+ charlen
>= term
.col
) {
2617 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2618 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2621 xclear(winx
, 0, winx
+ width
, borderpx
);
2623 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2625 /* Clean up the region we want to draw to. */
2626 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2628 fcsets
[0] = font
->set
;
2629 for (xp
= winx
; bytelen
> 0;) {
2631 * Search for the range in the to be printed string of glyphs
2632 * that are in the main font. Then print that range. If
2633 * some glyph is found that is not in the font, do the
2641 u8cblen
= utf8decode(s
, &u8char
);
2645 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2646 if (!doesexist
|| bytelen
<= 0) {
2655 XftDrawStringUtf8(xw
.draw
, fg
,
2657 winy
+ font
->ascent
,
2660 xp
+= font
->width
* u8fl
;
2672 /* Search the font cache. */
2673 for (i
= 0; i
< frclen
; i
++, frp
--) {
2677 if (frc
[frp
].c
== u8char
2678 && frc
[frp
].flags
== frcflags
) {
2683 /* Nothing was found. */
2686 * Nothing was found in the cache. Now use
2687 * some dozen of Fontconfig calls to get the
2688 * font for one single character.
2690 fcpattern
= FcPatternDuplicate(font
->pattern
);
2691 fccharset
= FcCharSetCreate();
2693 FcCharSetAddChar(fccharset
, u8char
);
2694 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2696 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2699 FcConfigSubstitute(0, fcpattern
,
2701 FcDefaultSubstitute(fcpattern
);
2703 fontpattern
= FcFontSetMatch(0, fcsets
,
2704 FcTrue
, fcpattern
, &fcres
);
2707 * Overwrite or create the new cache entry
2712 if (frccur
>= LEN(frc
))
2714 if (frclen
> LEN(frc
)) {
2716 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2719 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2721 frc
[frccur
].c
= u8char
;
2722 frc
[frccur
].flags
= frcflags
;
2724 FcPatternDestroy(fcpattern
);
2725 FcCharSetDestroy(fccharset
);
2730 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2731 xp
, winy
+ frc
[frp
].font
->ascent
,
2732 (FcChar8
*)u8c
, u8cblen
);
2738 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2739 winy + font->ascent, (FcChar8 *)s, bytelen);
2742 if(base
.mode
& ATTR_UNDERLINE
) {
2743 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2750 static int oldx
= 0, oldy
= 0;
2752 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
, 0};
2754 LIMIT(oldx
, 0, term
.col
-1);
2755 LIMIT(oldy
, 0, term
.row
-1);
2757 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2758 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2760 /* remove the old cursor */
2761 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2762 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2763 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2766 xtermclear(oldx
, oldy
, oldx
, oldy
);
2769 /* draw the new one */
2770 if(!(IS_SET(MODE_HIDE
))) {
2771 if(!(xw
.state
& WIN_FOCUSED
))
2774 if(IS_SET(MODE_REVERSE
))
2775 g
.mode
|= ATTR_REVERSE
, g
.fg
= defaultcs
, g
.bg
= defaultfg
;
2778 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2779 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2785 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2790 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2794 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2795 nanosleep(&tv
, NULL
);
2800 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2802 drawregion(0, 0, term
.col
, term
.row
);
2803 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2807 drawregion(int x1
, int y1
, int x2
, int y2
) {
2808 int ic
, ib
, x
, y
, ox
, sl
;
2810 char buf
[DRAW_BUF_SIZ
];
2811 bool ena_sel
= sel
.bx
!= -1;
2813 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
2816 if(!(xw
.state
& WIN_VISIBLE
))
2819 for(y
= y1
; y
< y2
; y
++) {
2823 xtermclear(0, y
, term
.col
, y
);
2825 base
= term
.line
[y
][0];
2827 for(x
= x1
; x
< x2
; x
++) {
2828 new = term
.line
[y
][x
];
2829 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2830 new.mode
^= ATTR_REVERSE
;
2831 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2832 || ATTRCMP(base
, new)
2833 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2834 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2837 if(new.state
& GLYPH_SET
) {
2842 sl
= utf8size(new.c
);
2843 memcpy(buf
+ib
, new.c
, sl
);
2849 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2855 expose(XEvent
*ev
) {
2856 XExposeEvent
*e
= &ev
->xexpose
;
2858 if(xw
.state
& WIN_REDRAW
) {
2860 xw
.state
&= ~WIN_REDRAW
;
2865 visibility(XEvent
*ev
) {
2866 XVisibilityEvent
*e
= &ev
->xvisibility
;
2868 if(e
->state
== VisibilityFullyObscured
) {
2869 xw
.state
&= ~WIN_VISIBLE
;
2870 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2871 /* need a full redraw for next Expose, not just a buf copy */
2872 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2878 xw
.state
&= ~WIN_VISIBLE
;
2882 xseturgency(int add
) {
2883 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2885 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2886 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2892 if(ev
->type
== FocusIn
) {
2893 XSetICFocus(xw
.xic
);
2894 xw
.state
|= WIN_FOCUSED
;
2897 XUnsetICFocus(xw
.xic
);
2898 xw
.state
&= ~WIN_FOCUSED
;
2903 match(uint mask
, uint state
) {
2904 if(mask
== XK_NO_MOD
&& state
)
2906 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
2908 if((state
& mask
) != state
)
2914 numlock(const Arg
*dummy
) {
2919 kmap(KeySym k
, uint state
) {
2924 /* Check for mapped keys out of X11 function keys. */
2925 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
2926 if(mappedkeys
[i
] == k
)
2929 if(i
== LEN(mappedkeys
)) {
2930 if((k
& 0xFFFF) < 0xFD00)
2934 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
2940 if(!match(mask
, state
))
2943 if(kp
->appkey
> 0) {
2944 if(!IS_SET(MODE_APPKEYPAD
))
2946 if(term
.numlock
&& kp
->appkey
== 2)
2948 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
2952 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
2954 && !IS_SET(MODE_APPCURSOR
))) {
2958 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
2959 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
2970 kpress(XEvent
*ev
) {
2971 XKeyEvent
*e
= &ev
->xkey
;
2973 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
2978 if (IS_SET(MODE_KBDLOCK
))
2981 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
2982 e
->state
&= ~Mod2Mask
;
2984 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
2985 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
2986 bp
->func(&(bp
->arg
));
2991 /* 2. custom keys from config.h */
2992 if((customkey
= kmap(ksym
, e
->state
))) {
2993 len
= strlen(customkey
);
2994 memcpy(buf
, customkey
, len
);
2995 /* 2. hardcoded (overrides X lookup) */
3000 if (len
== 1 && e
->state
& Mod1Mask
)
3003 memcpy(cp
, xstr
, len
);
3004 len
= cp
- buf
+ len
;
3008 if(IS_SET(MODE_ECHO
))
3014 cmessage(XEvent
*e
) {
3016 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
3017 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3018 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3019 xw
.state
|= WIN_FOCUSED
;
3021 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3022 xw
.state
&= ~WIN_FOCUSED
;
3024 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3025 /* Send SIGHUP to shell */
3032 cresize(int width
, int height
)
3041 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3042 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3051 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3054 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3061 int xfd
= XConnectionNumber(xw
.dpy
), i
;
3062 struct timeval drawtimeout
, *tv
= NULL
;
3066 FD_SET(cmdfd
, &rfd
);
3068 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3071 die("select failed: %s\n", SERRNO
);
3075 * Stop after a certain number of reads so the user does not
3076 * feel like the system is stuttering.
3078 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
3082 * Just wait a bit so it isn't disturbing the
3083 * user and the system is able to write something.
3085 drawtimeout
.tv_sec
= 0;
3086 drawtimeout
.tv_usec
= 5;
3093 while(XPending(xw
.dpy
)) {
3094 XNextEvent(xw
.dpy
, &ev
);
3095 if(XFilterEvent(&ev
, None
))
3097 if(handler
[ev
.type
])
3098 (handler
[ev
.type
])(&ev
);
3107 main(int argc
, char *argv
[]) {
3108 int i
, bitm
, xr
, yr
;
3111 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3114 for(i
= 1; i
< argc
; i
++) {
3115 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
3118 opt_class
= argv
[i
];
3121 /* eat all remaining arguments */
3133 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
3138 if(bitm
& WidthValue
)
3140 if(bitm
& HeightValue
)
3142 if(bitm
& XNegative
&& xw
.fx
== 0)
3144 if(bitm
& XNegative
&& xw
.fy
== 0)
3147 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3156 opt_title
= argv
[i
];
3163 opt_embed
= argv
[i
];
3169 setlocale(LC_CTYPE
, "");
3170 XSetLocaleModifiers("");