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>
36 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
38 #elif defined(__FreeBSD__) || defined(__DragonFly__)
43 "st " VERSION " (c) 2010-2012 st engineers\n" \
44 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
45 " [-t title] [-w windowid] [-e command ...]\n"
48 #define XEMBED_FOCUS_IN 4
49 #define XEMBED_FOCUS_OUT 5
52 #define ESC_BUF_SIZ 256
53 #define ESC_ARG_SIZ 16
54 #define STR_BUF_SIZ 256
55 #define STR_ARG_SIZ 16
56 #define DRAW_BUF_SIZ 20*1024
58 #define XK_NO_MOD UINT_MAX
61 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
64 #define CLEANMASK(mask) (mask & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
65 #define SERRNO strerror(errno)
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #define MAX(a, b) ((a) < (b) ? (b) : (a))
68 #define LEN(a) (sizeof(a) / sizeof(a[0]))
69 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
70 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
71 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
72 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
73 #define IS_SET(flag) (term.mode & (flag))
74 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
76 #define VT102ID "\033[?6c"
78 enum glyph_attribute
{
88 enum cursor_movement
{
114 MODE_MOUSEMOTION
= 64,
124 ESC_STR
= 4, /* DSC, OSC, PM, APC */
126 ESC_STR_END
= 16, /* a final string was encountered */
127 ESC_TEST
= 32, /* Enter in test mode */
138 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
140 typedef unsigned char uchar
;
141 typedef unsigned int uint
;
142 typedef unsigned long ulong
;
143 typedef unsigned short ushort
;
146 char c
[UTF_SIZ
]; /* character code */
147 uchar mode
; /* attribute flags */
148 ushort fg
; /* foreground */
149 ushort bg
; /* background */
150 uchar state
; /* state flags */
156 Glyph attr
; /* current char attributes */
162 /* CSI Escape sequence structs */
163 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
165 char buf
[ESC_BUF_SIZ
]; /* raw string */
166 int len
; /* raw string length */
168 int arg
[ESC_ARG_SIZ
];
169 int narg
; /* nb of args */
173 /* STR Escape sequence structs */
174 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
176 char type
; /* ESC type ... */
177 char buf
[STR_BUF_SIZ
]; /* raw string */
178 int len
; /* raw string length */
179 char *args
[STR_ARG_SIZ
];
180 int narg
; /* nb of args */
183 /* Internal representation of the screen */
185 int row
; /* nb row */
186 int col
; /* nb col */
187 Line
*line
; /* screen */
188 Line
*alt
; /* alternate screen */
189 bool *dirty
; /* dirtyness of lines */
190 TCursor c
; /* cursor */
191 int top
; /* top scroll limit */
192 int bot
; /* bottom scroll limit */
193 int mode
; /* terminal mode flags */
194 int esc
; /* escape state flags */
198 /* Purely graphic info */
204 Atom xembed
, wmdeletewin
;
210 bool isfixed
; /* is fixed geometry? */
211 int fx
, fy
, fw
, fh
; /* fixed geometry */
212 int tw
, th
; /* tty width and height */
213 int w
; /* window width */
214 int h
; /* window height */
215 int ch
; /* char height */
216 int cw
; /* char width */
217 char state
; /* focus, redraw, visible */
226 /* TODO: use better name for vars... */
237 struct timeval tclick1
;
238 struct timeval tclick2
;
251 void (*func
)(const Arg
*);
255 /* function definitions used in config.h */
256 static void xzoom(const Arg
*);
258 /* Config.h for applying patches and the configuration. */
272 /* Drawing Context */
274 XftColor xft_col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
276 Font font
, bfont
, ifont
, ibfont
;
279 static void die(const char *, ...);
280 static void draw(void);
281 static void redraw(void);
282 static void drawregion(int, int, int, int);
283 static void execsh(void);
284 static void sigchld(int);
285 static void run(void);
287 static void csidump(void);
288 static void csihandle(void);
289 static void csiparse(void);
290 static void csireset(void);
291 static void strdump(void);
292 static void strhandle(void);
293 static void strparse(void);
294 static void strreset(void);
296 static void tclearregion(int, int, int, int);
297 static void tcursor(int);
298 static void tdeletechar(int);
299 static void tdeleteline(int);
300 static void tinsertblank(int);
301 static void tinsertblankline(int);
302 static void tmoveto(int, int);
303 static void tnew(int, int);
304 static void tnewline(int);
305 static void tputtab(bool);
306 static void tputc(char *, int);
307 static void treset(void);
308 static int tresize(int, int);
309 static void tscrollup(int, int);
310 static void tscrolldown(int, int);
311 static void tsetattr(int*, int);
312 static void tsetchar(char *, Glyph
*, int, int);
313 static void tsetscroll(int, int);
314 static void tswapscreen(void);
315 static void tsetdirt(int, int);
316 static void tsetmode(bool, bool, int *, int);
317 static void tfulldirt(void);
319 static void ttynew(void);
320 static void ttyread(void);
321 static void ttyresize(void);
322 static void ttywrite(const char *, size_t);
324 static void xdraws(char *, Glyph
, int, int, int, int);
325 static void xhints(void);
326 static void xclear(int, int, int, int);
327 static void xdrawcursor(void);
328 static void xinit(void);
329 static void xloadcols(void);
330 static void xresettitle(void);
331 static void xseturgency(int);
332 static void xsetsel(char*);
333 static void xtermclear(int, int, int, int);
334 static void xresize(int, int);
336 static void expose(XEvent
*);
337 static void visibility(XEvent
*);
338 static void unmap(XEvent
*);
339 static char *kmap(KeySym
, uint
);
340 static void kpress(XEvent
*);
341 static void cmessage(XEvent
*);
342 static void cresize(int width
, int height
);
343 static void resize(XEvent
*);
344 static void focus(XEvent
*);
345 static void brelease(XEvent
*);
346 static void bpress(XEvent
*);
347 static void bmotion(XEvent
*);
348 static void selnotify(XEvent
*);
349 static void selclear(XEvent
*);
350 static void selrequest(XEvent
*);
352 static void selinit(void);
353 static inline bool selected(int, int);
354 static void selcopy(void);
355 static void selpaste(void);
356 static void selscroll(int, int);
358 static int utf8decode(char *, long *);
359 static int utf8encode(long *, char *);
360 static int utf8size(char *);
361 static int isfullutf8(char *, int);
363 static ssize_t
xwrite(int, char *, size_t);
364 static void *xmalloc(size_t);
365 static void *xrealloc(void *, size_t);
366 static void *xcalloc(size_t nmemb
, size_t size
);
368 static void (*handler
[LASTEvent
])(XEvent
*) = {
370 [ClientMessage
] = cmessage
,
371 [ConfigureNotify
] = resize
,
372 [VisibilityNotify
] = visibility
,
373 [UnmapNotify
] = unmap
,
377 [MotionNotify
] = bmotion
,
378 [ButtonPress
] = bpress
,
379 [ButtonRelease
] = brelease
,
380 [SelectionClear
] = selclear
,
381 [SelectionNotify
] = selnotify
,
382 [SelectionRequest
] = selrequest
,
389 static CSIEscape csiescseq
;
390 static STREscape strescseq
;
393 static Selection sel
;
394 static int iofd
= -1;
395 static char **opt_cmd
= NULL
;
396 static char *opt_io
= NULL
;
397 static char *opt_title
= NULL
;
398 static char *opt_embed
= NULL
;
399 static char *opt_class
= NULL
;
400 static char *opt_font
= NULL
;
402 static char *usedfont
= NULL
;
403 static int usedfontsize
= 0;
406 xwrite(int fd
, char *s
, size_t len
) {
410 ssize_t r
= write(fd
, s
, len
);
420 xmalloc(size_t len
) {
421 void *p
= malloc(len
);
424 die("Out of memory\n");
430 xrealloc(void *p
, size_t len
) {
431 if((p
= realloc(p
, len
)) == NULL
)
432 die("Out of memory\n");
438 xcalloc(size_t nmemb
, size_t size
) {
439 void *p
= calloc(nmemb
, size
);
442 die("Out of memory\n");
448 utf8decode(char *s
, long *u
) {
454 if(~c
& B7
) { /* 0xxxxxxx */
457 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
458 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
460 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
461 *u
= c
&(B3
|B2
|B1
|B0
);
463 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
470 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
472 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
475 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
478 if((n
== 1 && *u
< 0x80) ||
479 (n
== 2 && *u
< 0x800) ||
480 (n
== 3 && *u
< 0x10000) ||
481 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
493 utf8encode(long *u
, char *s
) {
501 *sp
= uc
; /* 0xxxxxxx */
503 } else if(*u
< 0x800) {
504 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
506 } else if(uc
< 0x10000) {
507 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
509 } else if(uc
<= 0x10FFFF) {
510 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
516 for(i
=n
,++sp
; i
>0; --i
,++sp
)
517 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
529 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
530 UTF-8 otherwise return 0 */
532 isfullutf8(char *s
, int b
) {
540 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
542 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
544 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
546 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
548 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
549 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
562 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
564 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
573 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
574 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
578 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
579 if(sel
.xtarget
== None
)
580 sel
.xtarget
= XA_STRING
;
588 return LIMIT(x
, 0, term
.col
-1);
596 return LIMIT(y
, 0, term
.row
-1);
600 selected(int x
, int y
) {
603 if(sel
.ey
== y
&& sel
.by
== y
) {
604 bx
= MIN(sel
.bx
, sel
.ex
);
605 ex
= MAX(sel
.bx
, sel
.ex
);
606 return BETWEEN(x
, bx
, ex
);
609 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
610 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
611 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
612 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
616 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
618 *b
= e
->xbutton
.button
;
620 *x
= x2col(e
->xbutton
.x
);
621 *y
= y2row(e
->xbutton
.y
);
623 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
624 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
625 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
626 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
630 mousereport(XEvent
*e
) {
631 int x
= x2col(e
->xbutton
.x
);
632 int y
= y2row(e
->xbutton
.y
);
633 int button
= e
->xbutton
.button
;
634 int state
= e
->xbutton
.state
;
635 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
636 static int ob
, ox
, oy
;
639 if(e
->xbutton
.type
== MotionNotify
) {
640 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
644 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
650 if(e
->xbutton
.type
== ButtonPress
) {
656 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
657 + (state
& Mod4Mask
? 8 : 0)
658 + (state
& ControlMask
? 16 : 0);
660 ttywrite(buf
, sizeof(buf
));
665 if(IS_SET(MODE_MOUSE
)) {
667 } else if(e
->xbutton
.button
== Button1
) {
670 tsetdirt(sel
.b
.y
, sel
.e
.y
);
674 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
675 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
676 } else if(e
->xbutton
.button
== Button4
) {
678 } else if(e
->xbutton
.button
== Button5
) {
686 int x
, y
, bufsize
, is_selected
= 0, size
;
692 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
693 ptr
= str
= xmalloc(bufsize
);
695 /* append every set & selected glyph to the selection */
696 for(y
= 0; y
< term
.row
; y
++) {
697 gp
= &term
.line
[y
][0];
698 last
= gp
+ term
.col
;
700 while(--last
>= gp
&& !(last
->state
& GLYPH_SET
))
703 for(x
= 0; gp
<= last
; x
++, ++gp
) {
704 if(!(is_selected
= selected(x
, y
)))
707 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
709 memcpy(ptr
, p
, size
);
712 /* \n at the end of every selected line except for the last one */
713 if(is_selected
&& y
< sel
.e
.y
)
718 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
723 selnotify(XEvent
*e
) {
724 ulong nitems
, ofs
, rem
;
731 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
732 False
, AnyPropertyType
, &type
, &format
,
733 &nitems
, &rem
, &data
)) {
734 fprintf(stderr
, "Clipboard allocation failed\n");
737 ttywrite((const char *) data
, nitems
* format
/ 8);
739 /* number of 32-bit chunks returned */
740 ofs
+= nitems
* format
/ 32;
746 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
747 xw
.win
, CurrentTime
);
750 void selclear(XEvent
*e
) {
754 tsetdirt(sel
.b
.y
, sel
.e
.y
);
758 selrequest(XEvent
*e
) {
759 XSelectionRequestEvent
*xsre
;
761 Atom xa_targets
, string
;
763 xsre
= (XSelectionRequestEvent
*) e
;
764 xev
.type
= SelectionNotify
;
765 xev
.requestor
= xsre
->requestor
;
766 xev
.selection
= xsre
->selection
;
767 xev
.target
= xsre
->target
;
768 xev
.time
= xsre
->time
;
772 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
773 if(xsre
->target
== xa_targets
) {
774 /* respond with the supported type */
775 string
= sel
.xtarget
;
776 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
777 XA_ATOM
, 32, PropModeReplace
,
778 (uchar
*) &string
, 1);
779 xev
.property
= xsre
->property
;
780 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
781 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
782 xsre
->target
, 8, PropModeReplace
,
783 (uchar
*) sel
.clip
, strlen(sel
.clip
));
784 xev
.property
= xsre
->property
;
787 /* all done, send a notification to the listener */
788 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
789 fprintf(stderr
, "Error sending SelectionNotify event\n");
794 /* register the selection for both the clipboard and the primary */
800 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
802 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
803 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
807 brelease(XEvent
*e
) {
810 if(IS_SET(MODE_MOUSE
)) {
815 if(e
->xbutton
.button
== Button2
) {
817 } else if(e
->xbutton
.button
== Button1
) {
819 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
820 term
.dirty
[sel
.ey
] = 1;
821 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
823 gettimeofday(&now
, NULL
);
825 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
826 /* triple click on the line */
827 sel
.b
.x
= sel
.bx
= 0;
828 sel
.e
.x
= sel
.ex
= term
.col
;
829 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
831 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
832 /* double click to select word */
834 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
835 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
839 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
840 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
844 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
852 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
853 gettimeofday(&sel
.tclick1
, NULL
);
858 int starty
, endy
, oldey
, oldex
;
860 if(IS_SET(MODE_MOUSE
)) {
868 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
870 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
871 starty
= MIN(oldey
, sel
.ey
);
872 endy
= MAX(oldey
, sel
.ey
);
873 tsetdirt(starty
, endy
);
879 die(const char *errstr
, ...) {
882 va_start(ap
, errstr
);
883 vfprintf(stderr
, errstr
, ap
);
891 char *envshell
= getenv("SHELL");
892 const struct passwd
*pass
= getpwuid(getuid());
893 char buf
[sizeof(long) * 8 + 1];
900 setenv("LOGNAME", pass
->pw_name
, 1);
901 setenv("USER", pass
->pw_name
, 1);
902 setenv("SHELL", pass
->pw_shell
, 0);
903 setenv("HOME", pass
->pw_dir
, 0);
906 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
907 setenv("WINDOWID", buf
, 1);
909 signal(SIGCHLD
, SIG_DFL
);
910 signal(SIGHUP
, SIG_DFL
);
911 signal(SIGINT
, SIG_DFL
);
912 signal(SIGQUIT
, SIG_DFL
);
913 signal(SIGTERM
, SIG_DFL
);
914 signal(SIGALRM
, SIG_DFL
);
916 DEFAULT(envshell
, shell
);
917 setenv("TERM", termname
, 1);
918 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
919 execvp(args
[0], args
);
927 if(waitpid(pid
, &stat
, 0) < 0)
928 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
930 if(WIFEXITED(stat
)) {
931 exit(WEXITSTATUS(stat
));
940 struct winsize w
= {term
.row
, term
.col
, 0, 0};
942 /* seems to work fine on linux, openbsd and freebsd */
943 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
944 die("openpty failed: %s\n", SERRNO
);
946 switch(pid
= fork()) {
948 die("fork failed\n");
951 setsid(); /* create a new process group */
952 dup2(s
, STDIN_FILENO
);
953 dup2(s
, STDOUT_FILENO
);
954 dup2(s
, STDERR_FILENO
);
955 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
956 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
964 signal(SIGCHLD
, sigchld
);
966 iofd
= (!strcmp(opt_io
, "-")) ?
968 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
970 fprintf(stderr
, "Error opening %s:%s\n",
971 opt_io
, strerror(errno
));
981 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
983 fprintf(stderr
, "\n");
988 static char buf
[BUFSIZ
];
989 static int buflen
= 0;
992 int charsize
; /* size of utf8 char in bytes */
996 /* append read bytes to unprocessed bytes */
997 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
998 die("Couldn't read from shell: %s\n", SERRNO
);
1000 /* process every complete utf8 char */
1003 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1004 charsize
= utf8decode(ptr
, &utf8c
);
1005 utf8encode(&utf8c
, s
);
1011 /* keep any uncomplete utf8 char for the next call */
1012 memmove(buf
, ptr
, buflen
);
1016 ttywrite(const char *s
, size_t n
) {
1017 if(write(cmdfd
, s
, n
) == -1)
1018 die("write error on tty: %s\n", SERRNO
);
1025 w
.ws_row
= term
.row
;
1026 w
.ws_col
= term
.col
;
1027 w
.ws_xpixel
= xw
.tw
;
1028 w
.ws_ypixel
= xw
.th
;
1029 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1030 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1034 tsetdirt(int top
, int bot
) {
1037 LIMIT(top
, 0, term
.row
-1);
1038 LIMIT(bot
, 0, term
.row
-1);
1040 for(i
= top
; i
<= bot
; i
++)
1046 tsetdirt(0, term
.row
-1);
1053 if(mode
== CURSOR_SAVE
) {
1055 } else if(mode
== CURSOR_LOAD
) {
1065 term
.c
= (TCursor
){{
1069 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1071 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1072 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1075 term
.bot
= term
.row
- 1;
1076 term
.mode
= MODE_WRAP
;
1078 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1082 tnew(int col
, int row
) {
1083 /* set screen size */
1086 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1087 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1088 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1089 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1091 for(row
= 0; row
< term
.row
; row
++) {
1092 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1093 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1094 term
.dirty
[row
] = 0;
1096 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1103 Line
*tmp
= term
.line
;
1105 term
.line
= term
.alt
;
1107 term
.mode
^= MODE_ALTSCREEN
;
1112 tscrolldown(int orig
, int n
) {
1116 LIMIT(n
, 0, term
.bot
-orig
+1);
1118 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1120 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1121 temp
= term
.line
[i
];
1122 term
.line
[i
] = term
.line
[i
-n
];
1123 term
.line
[i
-n
] = temp
;
1126 term
.dirty
[i
-n
] = 1;
1133 tscrollup(int orig
, int n
) {
1136 LIMIT(n
, 0, term
.bot
-orig
+1);
1138 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1140 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1141 temp
= term
.line
[i
];
1142 term
.line
[i
] = term
.line
[i
+n
];
1143 term
.line
[i
+n
] = temp
;
1146 term
.dirty
[i
+n
] = 1;
1149 selscroll(orig
, -n
);
1153 selscroll(int orig
, int n
) {
1157 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1158 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1162 if(sel
.by
< term
.top
) {
1166 if(sel
.ey
> term
.bot
) {
1170 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1171 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1176 tnewline(int first_col
) {
1180 tscrollup(term
.top
, 1);
1184 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1189 /* int noarg = 1; */
1190 char *p
= csiescseq
.buf
;
1194 csiescseq
.priv
= 1, p
++;
1196 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1197 while(isdigit(*p
)) {
1198 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1199 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1201 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1202 csiescseq
.narg
++, p
++;
1204 csiescseq
.mode
= *p
;
1213 tmoveto(int x
, int y
) {
1214 LIMIT(x
, 0, term
.col
-1);
1215 LIMIT(y
, 0, term
.row
-1);
1216 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1222 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1223 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1224 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1225 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1226 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1227 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1228 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1229 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1230 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1231 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1235 * The table is proudly stolen from rxvt.
1237 if(attr
->mode
& ATTR_GFX
) {
1238 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1239 && vt100_0
[c
[0] - 0x41]) {
1240 c
= vt100_0
[c
[0] - 0x41];
1245 term
.line
[y
][x
] = *attr
;
1246 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1247 term
.line
[y
][x
].state
|= GLYPH_SET
;
1251 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1255 temp
= x1
, x1
= x2
, x2
= temp
;
1257 temp
= y1
, y1
= y2
, y2
= temp
;
1259 LIMIT(x1
, 0, term
.col
-1);
1260 LIMIT(x2
, 0, term
.col
-1);
1261 LIMIT(y1
, 0, term
.row
-1);
1262 LIMIT(y2
, 0, term
.row
-1);
1264 for(y
= y1
; y
<= y2
; y
++) {
1266 for(x
= x1
; x
<= x2
; x
++)
1267 term
.line
[y
][x
].state
= 0;
1272 tdeletechar(int n
) {
1273 int src
= term
.c
.x
+ n
;
1275 int size
= term
.col
- src
;
1277 term
.dirty
[term
.c
.y
] = 1;
1279 if(src
>= term
.col
) {
1280 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1284 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1285 size
* sizeof(Glyph
));
1286 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1290 tinsertblank(int n
) {
1293 int size
= term
.col
- dst
;
1295 term
.dirty
[term
.c
.y
] = 1;
1297 if(dst
>= term
.col
) {
1298 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1302 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1303 size
* sizeof(Glyph
));
1304 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1308 tinsertblankline(int n
) {
1309 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1312 tscrolldown(term
.c
.y
, n
);
1316 tdeleteline(int n
) {
1317 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1320 tscrollup(term
.c
.y
, n
);
1324 tsetattr(int *attr
, int l
) {
1327 for(i
= 0; i
< l
; i
++) {
1330 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1331 | ATTR_ITALIC
| ATTR_BLINK
);
1332 term
.c
.attr
.fg
= defaultfg
;
1333 term
.c
.attr
.bg
= defaultbg
;
1336 term
.c
.attr
.mode
|= ATTR_BOLD
;
1338 case 3: /* enter standout (highlight) */
1339 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1342 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1345 term
.c
.attr
.mode
|= ATTR_BLINK
;
1348 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1352 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1354 case 23: /* leave standout (highlight) mode */
1355 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1358 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1361 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1364 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1367 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1369 if(BETWEEN(attr
[i
], 0, 255)) {
1370 term
.c
.attr
.fg
= attr
[i
];
1373 "erresc: bad fgcolor %d\n",
1378 "erresc(38): gfx attr %d unknown\n",
1383 term
.c
.attr
.fg
= defaultfg
;
1386 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1388 if(BETWEEN(attr
[i
], 0, 255)) {
1389 term
.c
.attr
.bg
= attr
[i
];
1392 "erresc: bad bgcolor %d\n",
1397 "erresc(48): gfx attr %d unknown\n",
1402 term
.c
.attr
.bg
= defaultbg
;
1405 if(BETWEEN(attr
[i
], 30, 37)) {
1406 term
.c
.attr
.fg
= attr
[i
] - 30;
1407 } else if(BETWEEN(attr
[i
], 40, 47)) {
1408 term
.c
.attr
.bg
= attr
[i
] - 40;
1409 } else if(BETWEEN(attr
[i
], 90, 97)) {
1410 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1411 } else if(BETWEEN(attr
[i
], 100, 107)) {
1412 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1415 "erresc(default): gfx attr %d unknown\n",
1416 attr
[i
]), csidump();
1424 tsetscroll(int t
, int b
) {
1427 LIMIT(t
, 0, term
.row
-1);
1428 LIMIT(b
, 0, term
.row
-1);
1438 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1441 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1444 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1448 case 1: /* DECCKM -- Cursor key */
1449 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1451 case 5: /* DECSCNM -- Reverse video */
1453 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1454 if(mode
!= term
.mode
)
1457 case 6: /* XXX: DECOM -- Origin */
1459 case 7: /* DECAWM -- Auto wrap */
1460 MODBIT(term
.mode
, set
, MODE_WRAP
);
1462 case 8: /* XXX: DECARM -- Auto repeat */
1464 case 0: /* Error (IGNORED) */
1465 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1467 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1468 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1470 case 1000: /* 1000,1002: enable xterm mouse report */
1471 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1474 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1476 case 1049: /* = 1047 and 1048 */
1479 if(IS_SET(MODE_ALTSCREEN
))
1480 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1481 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1482 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1489 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1492 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1493 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1494 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1495 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1496 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1497 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1499 "erresc: unknown private set/reset mode %d\n",
1505 case 0: /* Error (IGNORED) */
1507 case 2: /* KAM -- keyboard action */
1508 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1510 case 4: /* IRM -- Insertion-replacement */
1511 MODBIT(term
.mode
, set
, MODE_INSERT
);
1513 case 12: /* XXX: SRM -- Send/Receive */
1515 case 20: /* LNM -- Linefeed/new line */
1516 MODBIT(term
.mode
, set
, MODE_CRLF
);
1520 "erresc: unknown set/reset mode %d\n",
1532 switch(csiescseq
.mode
) {
1535 fprintf(stderr
, "erresc: unknown csi ");
1539 case '@': /* ICH -- Insert <n> blank char */
1540 DEFAULT(csiescseq
.arg
[0], 1);
1541 tinsertblank(csiescseq
.arg
[0]);
1543 case 'A': /* CUU -- Cursor <n> Up */
1545 DEFAULT(csiescseq
.arg
[0], 1);
1546 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1548 case 'B': /* CUD -- Cursor <n> Down */
1549 DEFAULT(csiescseq
.arg
[0], 1);
1550 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1552 case 'c': /* DA -- Device Attributes */
1553 if(csiescseq
.arg
[0] == 0)
1554 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1556 case 'C': /* CUF -- Cursor <n> Forward */
1558 DEFAULT(csiescseq
.arg
[0], 1);
1559 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1561 case 'D': /* CUB -- Cursor <n> Backward */
1562 DEFAULT(csiescseq
.arg
[0], 1);
1563 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1565 case 'E': /* CNL -- Cursor <n> Down and first col */
1566 DEFAULT(csiescseq
.arg
[0], 1);
1567 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1569 case 'F': /* CPL -- Cursor <n> Up and first col */
1570 DEFAULT(csiescseq
.arg
[0], 1);
1571 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1573 case 'g': /* TBC -- Tabulation clear */
1574 switch (csiescseq
.arg
[0]) {
1575 case 0: /* clear current tab stop */
1576 term
.tabs
[term
.c
.x
] = 0;
1578 case 3: /* clear all the tabs */
1579 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1585 case 'G': /* CHA -- Move to <col> */
1587 DEFAULT(csiescseq
.arg
[0], 1);
1588 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1590 case 'H': /* CUP -- Move to <row> <col> */
1592 DEFAULT(csiescseq
.arg
[0], 1);
1593 DEFAULT(csiescseq
.arg
[1], 1);
1594 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1596 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1597 DEFAULT(csiescseq
.arg
[0], 1);
1598 while(csiescseq
.arg
[0]--)
1601 case 'J': /* ED -- Clear screen */
1603 switch(csiescseq
.arg
[0]) {
1605 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1606 if(term
.c
.y
< term
.row
-1)
1607 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1611 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1612 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1615 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1621 case 'K': /* EL -- Clear line */
1622 switch(csiescseq
.arg
[0]) {
1624 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1627 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1630 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1634 case 'S': /* SU -- Scroll <n> line up */
1635 DEFAULT(csiescseq
.arg
[0], 1);
1636 tscrollup(term
.top
, csiescseq
.arg
[0]);
1638 case 'T': /* SD -- Scroll <n> line down */
1639 DEFAULT(csiescseq
.arg
[0], 1);
1640 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1642 case 'L': /* IL -- Insert <n> blank lines */
1643 DEFAULT(csiescseq
.arg
[0], 1);
1644 tinsertblankline(csiescseq
.arg
[0]);
1646 case 'l': /* RM -- Reset Mode */
1647 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1649 case 'M': /* DL -- Delete <n> lines */
1650 DEFAULT(csiescseq
.arg
[0], 1);
1651 tdeleteline(csiescseq
.arg
[0]);
1653 case 'X': /* ECH -- Erase <n> char */
1654 DEFAULT(csiescseq
.arg
[0], 1);
1655 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1657 case 'P': /* DCH -- Delete <n> char */
1658 DEFAULT(csiescseq
.arg
[0], 1);
1659 tdeletechar(csiescseq
.arg
[0]);
1661 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1662 DEFAULT(csiescseq
.arg
[0], 1);
1663 while(csiescseq
.arg
[0]--)
1666 case 'd': /* VPA -- Move to <row> */
1667 DEFAULT(csiescseq
.arg
[0], 1);
1668 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1670 case 'h': /* SM -- Set terminal mode */
1671 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1673 case 'm': /* SGR -- Terminal attribute (color) */
1674 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1676 case 'r': /* DECSTBM -- Set Scrolling Region */
1677 if(csiescseq
.priv
) {
1680 DEFAULT(csiescseq
.arg
[0], 1);
1681 DEFAULT(csiescseq
.arg
[1], term
.row
);
1682 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1686 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1687 tcursor(CURSOR_SAVE
);
1689 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1690 tcursor(CURSOR_LOAD
);
1701 for(i
= 0; i
< csiescseq
.len
; i
++) {
1702 c
= csiescseq
.buf
[i
] & 0xff;
1705 } else if(c
== '\n') {
1707 } else if(c
== '\r') {
1709 } else if(c
== 0x1b) {
1712 printf("(%02x)", c
);
1720 memset(&csiescseq
, 0, sizeof(csiescseq
));
1728 * TODO: make this being useful in case of color palette change.
1734 switch(strescseq
.type
) {
1735 case ']': /* OSC -- Operating System Command */
1741 * TODO: Handle special chars in string, like umlauts.
1744 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1748 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1750 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1753 fprintf(stderr
, "erresc: unknown str ");
1758 case 'k': /* old title set compatibility */
1759 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1761 case 'P': /* DSC -- Device Control String */
1762 case '_': /* APC -- Application Program Command */
1763 case '^': /* PM -- Privacy Message */
1765 fprintf(stderr
, "erresc: unknown str ");
1775 * TODO: Implement parsing like for CSI when required.
1776 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1786 printf("ESC%c", strescseq
.type
);
1787 for(i
= 0; i
< strescseq
.len
; i
++) {
1788 c
= strescseq
.buf
[i
] & 0xff;
1791 } else if(c
== '\n') {
1793 } else if(c
== '\r') {
1795 } else if(c
== 0x1b) {
1798 printf("(%02x)", c
);
1806 memset(&strescseq
, 0, sizeof(strescseq
));
1810 tputtab(bool forward
) {
1816 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1821 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1824 tmoveto(x
, term
.c
.y
);
1828 tputc(char *c
, int len
) {
1830 bool control
= ascii
< '\x20' || ascii
== 0177;
1833 if (xwrite(iofd
, c
, len
) < 0) {
1834 fprintf(stderr
, "Error writting in %s:%s\n",
1835 opt_io
, strerror(errno
));
1841 * STR sequences must be checked before anything else
1842 * because it can use some control codes as part of the sequence.
1844 if(term
.esc
& ESC_STR
) {
1847 term
.esc
= ESC_START
| ESC_STR_END
;
1849 case '\a': /* backwards compatibility to xterm */
1854 strescseq
.buf
[strescseq
.len
++] = ascii
;
1855 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1864 * Actions of control codes must be performed as soon they arrive
1865 * because they can be embedded inside a control sequence, and
1866 * they must not cause conflicts with sequences.
1874 tmoveto(term
.c
.x
-1, term
.c
.y
);
1877 tmoveto(0, term
.c
.y
);
1882 /* go to first col if the mode is set */
1883 tnewline(IS_SET(MODE_CRLF
));
1885 case '\a': /* BEL */
1886 if(!(xw
.state
& WIN_FOCUSED
))
1889 case '\033': /* ESC */
1891 term
.esc
= ESC_START
;
1893 case '\016': /* SO */
1894 term
.c
.attr
.mode
|= ATTR_GFX
;
1896 case '\017': /* SI */
1897 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1899 case '\032': /* SUB */
1900 case '\030': /* CAN */
1903 case '\005': /* ENQ (IGNORED) */
1904 case '\000': /* NUL (IGNORED) */
1905 case '\021': /* XON (IGNORED) */
1906 case '\023': /* XOFF (IGNORED) */
1907 case 0177: /* DEL (IGNORED) */
1910 } else if(term
.esc
& ESC_START
) {
1911 if(term
.esc
& ESC_CSI
) {
1912 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1913 if(BETWEEN(ascii
, 0x40, 0x7E)
1914 || csiescseq
.len
>= ESC_BUF_SIZ
) {
1916 csiparse(), csihandle();
1918 } else if(term
.esc
& ESC_STR_END
) {
1922 } else if(term
.esc
& ESC_ALTCHARSET
) {
1924 case '0': /* Line drawing set */
1925 term
.c
.attr
.mode
|= ATTR_GFX
;
1927 case 'B': /* USASCII */
1928 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1930 case 'A': /* UK (IGNORED) */
1931 case '<': /* multinational charset (IGNORED) */
1932 case '5': /* Finnish (IGNORED) */
1933 case 'C': /* Finnish (IGNORED) */
1934 case 'K': /* German (IGNORED) */
1937 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1940 } else if(term
.esc
& ESC_TEST
) {
1941 if(ascii
== '8') { /* DEC screen alignment test. */
1942 char E
[UTF_SIZ
] = "E";
1945 for(x
= 0; x
< term
.col
; ++x
) {
1946 for(y
= 0; y
< term
.row
; ++y
)
1947 tsetchar(E
, &term
.c
.attr
, x
, y
);
1954 term
.esc
|= ESC_CSI
;
1957 term
.esc
|= ESC_TEST
;
1959 case 'P': /* DCS -- Device Control String */
1960 case '_': /* APC -- Application Program Command */
1961 case '^': /* PM -- Privacy Message */
1962 case ']': /* OSC -- Operating System Command */
1963 case 'k': /* old title set compatibility */
1965 strescseq
.type
= ascii
;
1966 term
.esc
|= ESC_STR
;
1968 case '(': /* set primary charset G0 */
1969 term
.esc
|= ESC_ALTCHARSET
;
1971 case ')': /* set secondary charset G1 (IGNORED) */
1972 case '*': /* set tertiary charset G2 (IGNORED) */
1973 case '+': /* set quaternary charset G3 (IGNORED) */
1976 case 'D': /* IND -- Linefeed */
1977 if(term
.c
.y
== term
.bot
) {
1978 tscrollup(term
.top
, 1);
1980 tmoveto(term
.c
.x
, term
.c
.y
+1);
1984 case 'E': /* NEL -- Next line */
1985 tnewline(1); /* always go to first col */
1988 case 'H': /* HTS -- Horizontal tab stop */
1989 term
.tabs
[term
.c
.x
] = 1;
1992 case 'M': /* RI -- Reverse index */
1993 if(term
.c
.y
== term
.top
) {
1994 tscrolldown(term
.top
, 1);
1996 tmoveto(term
.c
.x
, term
.c
.y
-1);
2000 case 'Z': /* DECID -- Identify Terminal */
2001 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2004 case 'c': /* RIS -- Reset to inital state */
2009 case '=': /* DECPAM -- Application keypad */
2010 term
.mode
|= MODE_APPKEYPAD
;
2013 case '>': /* DECPNM -- Normal keypad */
2014 term
.mode
&= ~MODE_APPKEYPAD
;
2017 case '7': /* DECSC -- Save Cursor */
2018 tcursor(CURSOR_SAVE
);
2021 case '8': /* DECRC -- Restore Cursor */
2022 tcursor(CURSOR_LOAD
);
2025 case '\\': /* ST -- Stop */
2029 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2030 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2035 * All characters which forms part of a sequence are not
2041 * Display control codes only if we are in graphic mode
2043 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2045 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2047 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2048 tnewline(1); /* always go to first col */
2049 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2050 if(term
.c
.x
+1 < term
.col
)
2051 tmoveto(term
.c
.x
+1, term
.c
.y
);
2053 term
.c
.state
|= CURSOR_WRAPNEXT
;
2057 tresize(int col
, int row
) {
2059 int minrow
= MIN(row
, term
.row
);
2060 int mincol
= MIN(col
, term
.col
);
2061 int slide
= term
.c
.y
- row
+ 1;
2064 if(col
< 1 || row
< 1)
2067 /* free unneeded rows */
2070 /* slide screen to keep cursor where we expect it -
2071 * tscrollup would work here, but we can optimize to
2072 * memmove because we're freeing the earlier lines */
2073 for(/* i = 0 */; i
< slide
; i
++) {
2077 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2078 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2080 for(i
+= row
; i
< term
.row
; i
++) {
2085 /* resize to new height */
2086 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2087 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2088 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2089 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2091 /* resize each row to new width, zero-pad if needed */
2092 for(i
= 0; i
< minrow
; i
++) {
2094 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2095 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2096 for(x
= mincol
; x
< col
; x
++) {
2097 term
.line
[i
][x
].state
= 0;
2098 term
.alt
[i
][x
].state
= 0;
2102 /* allocate any new rows */
2103 for(/* i == minrow */; i
< row
; i
++) {
2105 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2106 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2108 if(col
> term
.col
) {
2109 bp
= term
.tabs
+ term
.col
;
2111 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2112 while(--bp
> term
.tabs
&& !*bp
)
2114 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2117 /* update terminal size */
2120 /* make use of the LIMIT in tmoveto */
2121 tmoveto(term
.c
.x
, term
.c
.y
);
2122 /* reset scrolling region */
2123 tsetscroll(0, row
-1);
2129 xresize(int col
, int row
) {
2130 xw
.tw
= MAX(1, 2*borderpx
+ col
* xw
.cw
);
2131 xw
.th
= MAX(1, 2*borderpx
+ row
* xw
.ch
);
2133 XftDrawChange(xw
.xft_draw
, xw
.buf
);
2139 XRenderColor xft_color
= { .alpha
= 0 };
2141 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2142 for(i
= 0; i
< LEN(colorname
); i
++) {
2145 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.xft_col
[i
])) {
2146 die("Could not allocate color '%s'\n", colorname
[i
]);
2150 /* load colors [16-255] ; same colors as xterm */
2151 for(i
= 16, r
= 0; r
< 6; r
++) {
2152 for(g
= 0; g
< 6; g
++) {
2153 for(b
= 0; b
< 6; b
++) {
2154 xft_color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2155 xft_color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2156 xft_color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2157 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
2158 die("Could not allocate color %d\n", i
);
2165 for(r
= 0; r
< 24; r
++, i
++) {
2166 xft_color
.red
= xft_color
.green
= xft_color
.blue
= 0x0808 + 0x0a0a * r
;
2167 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
,
2169 die("Could not allocate color %d\n", i
);
2175 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2176 XftDrawRect(xw
.xft_draw
,
2177 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2178 borderpx
+ col1
* xw
.cw
,
2179 borderpx
+ row1
* xw
.ch
,
2180 (col2
-col1
+1) * xw
.cw
,
2181 (row2
-row1
+1) * xw
.ch
);
2185 * Absolute coordinates.
2188 xclear(int x1
, int y1
, int x2
, int y2
) {
2189 XftDrawRect(xw
.xft_draw
,
2190 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2191 x1
, y1
, x2
-x1
, y2
-y1
);
2196 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2197 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2198 XSizeHints
*sizeh
= NULL
;
2200 sizeh
= XAllocSizeHints();
2201 if(xw
.isfixed
== False
) {
2202 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2203 sizeh
->height
= xw
.h
;
2204 sizeh
->width
= xw
.w
;
2205 sizeh
->height_inc
= xw
.ch
;
2206 sizeh
->width_inc
= xw
.cw
;
2207 sizeh
->base_height
= 2*borderpx
;
2208 sizeh
->base_width
= 2*borderpx
;
2210 sizeh
->flags
= PMaxSize
| PMinSize
;
2211 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2212 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2215 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2220 xloadfont(Font
*f
, FcPattern
*pattern
) {
2224 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
2227 if(!(f
->xft_set
= XftFontOpenPattern(xw
.dpy
, match
))) {
2228 FcPatternDestroy(match
);
2232 f
->ascent
= f
->xft_set
->ascent
;
2233 f
->descent
= f
->xft_set
->descent
;
2235 f
->rbearing
= f
->xft_set
->max_advance_width
;
2237 f
->height
= f
->xft_set
->height
;
2238 f
->width
= f
->lbearing
+ f
->rbearing
;
2244 xloadfonts(char *fontstr
, int fontsize
) {
2249 if(fontstr
[0] == '-') {
2250 pattern
= XftXlfdParse(fontstr
, False
, False
);
2252 pattern
= FcNameParse((FcChar8
*)fontstr
);
2256 die("st: can't open font %s\n", fontstr
);
2259 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2260 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2261 usedfontsize
= fontsize
;
2263 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2264 if(result
== FcResultMatch
) {
2265 usedfontsize
= (int)fontval
;
2268 * Default font size is 12, if none given. This is to
2269 * have a known usedfontsize value.
2271 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2276 if(xloadfont(&dc
.font
, pattern
))
2277 die("st: can't open font %s\n", fontstr
);
2279 /* Setting character width and height. */
2280 xw
.cw
= dc
.font
.width
;
2281 xw
.ch
= dc
.font
.height
;
2283 FcPatternDel(pattern
, FC_WEIGHT
);
2284 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2285 if(xloadfont(&dc
.bfont
, pattern
))
2286 die("st: can't open font %s\n", fontstr
);
2288 FcPatternDel(pattern
, FC_SLANT
);
2289 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2290 if(xloadfont(&dc
.ibfont
, pattern
))
2291 die("st: can't open font %s\n", fontstr
);
2293 FcPatternDel(pattern
, FC_WEIGHT
);
2294 if(xloadfont(&dc
.ifont
, pattern
))
2295 die("st: can't open font %s\n", fontstr
);
2297 FcPatternDestroy(pattern
);
2301 xzoom(const Arg
*arg
)
2303 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2310 XSetWindowAttributes attrs
;
2313 int sw
, sh
, major
, minor
;
2315 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2316 die("Can't open display\n");
2317 xw
.scr
= XDefaultScreen(xw
.dpy
);
2318 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2321 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2322 xloadfonts(usedfont
, 0);
2325 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2328 /* adjust fixed window geometry */
2330 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2331 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2333 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2335 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2340 /* window - default size */
2341 xw
.h
= 2*borderpx
+ term
.row
* xw
.ch
;
2342 xw
.w
= 2*borderpx
+ term
.col
* xw
.cw
;
2347 attrs
.background_pixel
= dc
.xft_col
[defaultbg
].pixel
;
2348 attrs
.border_pixel
= dc
.xft_col
[defaultbg
].pixel
;
2349 attrs
.bit_gravity
= NorthWestGravity
;
2350 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2351 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2352 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2353 attrs
.colormap
= xw
.cmap
;
2355 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2356 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2357 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2359 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2363 /* double buffering */
2364 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2365 die("Xdbe extension is not present\n");
2366 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2368 /* Xft rendering context */
2369 xw
.xft_draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2372 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2373 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2374 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2375 XNFocusWindow
, xw
.win
, NULL
);
2377 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2379 /* white cursor, black outline */
2380 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2381 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2382 XRecolorCursor(xw
.dpy
, cursor
,
2383 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2384 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2386 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2387 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2388 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2391 XMapWindow(xw
.dpy
, xw
.win
);
2397 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2398 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2399 width
= charlen
* xw
.cw
;
2400 Font
*font
= &dc
.font
;
2402 XftColor
*fg
= &dc
.xft_col
[base
.fg
], *bg
= &dc
.xft_col
[base
.bg
],
2403 *temp
, revfg
, revbg
;
2404 XRenderColor colfg
, colbg
;
2406 if(base
.mode
& ATTR_BOLD
) {
2407 if(BETWEEN(base
.fg
, 0, 7)) {
2408 /* basic system colors */
2409 fg
= &dc
.xft_col
[base
.fg
+ 8];
2410 } else if(BETWEEN(base
.fg
, 16, 195)) {
2412 fg
= &dc
.xft_col
[base
.fg
+ 36];
2413 } else if(BETWEEN(base
.fg
, 232, 251)) {
2415 fg
= &dc
.xft_col
[base
.fg
+ 4];
2418 * Those ranges will not be brightened:
2419 * 8 - 15 – bright system colors
2420 * 196 - 231 – highest 256 color cube
2421 * 252 - 255 – brightest colors in greyscale
2426 if(base
.mode
& ATTR_ITALIC
)
2428 if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
2431 if(IS_SET(MODE_REVERSE
)) {
2432 if(fg
== &dc
.xft_col
[defaultfg
]) {
2433 fg
= &dc
.xft_col
[defaultbg
];
2435 colfg
.red
= ~fg
->color
.red
;
2436 colfg
.green
= ~fg
->color
.green
;
2437 colfg
.blue
= ~fg
->color
.blue
;
2438 colfg
.alpha
= fg
->color
.alpha
;
2439 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2443 if(bg
== &dc
.xft_col
[defaultbg
]) {
2444 bg
= &dc
.xft_col
[defaultfg
];
2446 colbg
.red
= ~bg
->color
.red
;
2447 colbg
.green
= ~bg
->color
.green
;
2448 colbg
.blue
= ~bg
->color
.blue
;
2449 colbg
.alpha
= bg
->color
.alpha
;
2450 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2455 if(base
.mode
& ATTR_REVERSE
)
2456 temp
= fg
, fg
= bg
, bg
= temp
;
2458 XftTextExtentsUtf8(xw
.dpy
, font
->xft_set
, (FcChar8
*)s
, bytelen
,
2460 width
= extents
.xOff
;
2462 /* Intelligent cleaning up of the borders. */
2464 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2465 winy
+ xw
.ch
+ (y
== term
.row
-1)? xw
.h
: 0);
2467 if(x
+ charlen
>= term
.col
-1) {
2468 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2469 (y
== term
.row
-1)? xw
.h
: (winy
+ xw
.ch
));
2472 xclear(winx
, 0, winx
+ width
, borderpx
);
2474 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2476 XftDrawRect(xw
.xft_draw
, bg
, winx
, winy
, width
, xw
.ch
);
2477 XftDrawStringUtf8(xw
.xft_draw
, fg
, font
->xft_set
, winx
,
2478 winy
+ font
->ascent
, (FcChar8
*)s
, bytelen
);
2480 if(base
.mode
& ATTR_UNDERLINE
) {
2481 XftDrawRect(xw
.xft_draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2488 static int oldx
= 0, oldy
= 0;
2490 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
, 0};
2492 LIMIT(oldx
, 0, term
.col
-1);
2493 LIMIT(oldy
, 0, term
.row
-1);
2495 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2496 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2498 /* remove the old cursor */
2499 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2500 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2501 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2504 xtermclear(oldx
, oldy
, oldx
, oldy
);
2507 /* draw the new one */
2508 if(!(IS_SET(MODE_HIDE
))) {
2509 if(!(xw
.state
& WIN_FOCUSED
))
2512 if(IS_SET(MODE_REVERSE
))
2513 g
.mode
|= ATTR_REVERSE
, g
.fg
= defaultcs
, g
.bg
= defaultfg
;
2516 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2517 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2523 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2528 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2532 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2533 nanosleep(&tv
, NULL
);
2538 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2540 drawregion(0, 0, term
.col
, term
.row
);
2541 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2545 drawregion(int x1
, int y1
, int x2
, int y2
) {
2546 int ic
, ib
, x
, y
, ox
, sl
;
2548 char buf
[DRAW_BUF_SIZ
];
2549 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2551 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2553 if(!(xw
.state
& WIN_VISIBLE
))
2556 for(y
= y1
; y
< y2
; y
++) {
2560 xtermclear(0, y
, term
.col
, y
);
2562 base
= term
.line
[y
][0];
2564 for(x
= x1
; x
< x2
; x
++) {
2565 new = term
.line
[y
][x
];
2566 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2567 new.mode
^= ATTR_REVERSE
;
2568 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2569 || ATTRCMP(base
, new)
2570 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2571 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2574 if(new.state
& GLYPH_SET
) {
2579 sl
= utf8size(new.c
);
2580 memcpy(buf
+ib
, new.c
, sl
);
2586 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2592 expose(XEvent
*ev
) {
2593 XExposeEvent
*e
= &ev
->xexpose
;
2595 if(xw
.state
& WIN_REDRAW
) {
2597 xw
.state
&= ~WIN_REDRAW
;
2602 visibility(XEvent
*ev
) {
2603 XVisibilityEvent
*e
= &ev
->xvisibility
;
2605 if(e
->state
== VisibilityFullyObscured
) {
2606 xw
.state
&= ~WIN_VISIBLE
;
2607 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2608 /* need a full redraw for next Expose, not just a buf copy */
2609 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2615 xw
.state
&= ~WIN_VISIBLE
;
2619 xseturgency(int add
) {
2620 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2622 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2623 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2629 if(ev
->type
== FocusIn
) {
2630 XSetICFocus(xw
.xic
);
2631 xw
.state
|= WIN_FOCUSED
;
2634 XUnsetICFocus(xw
.xic
);
2635 xw
.state
&= ~WIN_FOCUSED
;
2640 kmap(KeySym k
, uint state
) {
2645 for(i
= 0; i
< LEN(key
); i
++) {
2648 if(key
[i
].k
== k
&& ((state
& mask
) == mask
2649 || (mask
== XK_NO_MOD
&& !state
))) {
2650 return (char*)key
[i
].s
;
2657 kpress(XEvent
*ev
) {
2658 XKeyEvent
*e
= &ev
->xkey
;
2660 char buf
[32], *customkey
;
2661 int len
, meta
, shift
, i
;
2664 if (IS_SET(MODE_KBDLOCK
))
2667 meta
= e
->state
& Mod1Mask
;
2668 shift
= e
->state
& ShiftMask
;
2669 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2672 for(i
= 0; i
< LEN(shortcuts
); i
++) {
2673 if((ksym
== shortcuts
[i
].keysym
)
2674 && (CLEANMASK(shortcuts
[i
].mod
) == \
2675 CLEANMASK(e
->state
))
2676 && shortcuts
[i
].func
) {
2677 shortcuts
[i
].func(&(shortcuts
[i
].arg
));
2681 /* 2. custom keys from config.h */
2682 if((customkey
= kmap(ksym
, e
->state
))) {
2683 ttywrite(customkey
, strlen(customkey
));
2684 /* 2. hardcoded (overrides X lookup) */
2691 /* XXX: shift up/down doesn't work */
2692 sprintf(buf
, "\033%c%c",
2693 IS_SET(MODE_APPKEYPAD
) ? 'O' : '[',
2694 (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2703 ttywrite("\033", 1);
2705 if(IS_SET(MODE_CRLF
)) {
2706 ttywrite("\r\n", 2);
2714 if(meta
&& len
== 1)
2715 ttywrite("\033", 1);
2724 cmessage(XEvent
*e
) {
2726 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2727 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2728 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2729 xw
.state
|= WIN_FOCUSED
;
2731 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2732 xw
.state
&= ~WIN_FOCUSED
;
2734 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
2735 /* Send SIGHUP to shell */
2742 cresize(int width
, int height
)
2751 col
= (xw
.w
- 2*borderpx
) / xw
.cw
;
2752 row
= (xw
.h
- 2*borderpx
) / xw
.ch
;
2761 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2764 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
2771 int xfd
= XConnectionNumber(xw
.dpy
), i
;
2772 struct timeval drawtimeout
, *tv
= NULL
;
2776 FD_SET(cmdfd
, &rfd
);
2778 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
2781 die("select failed: %s\n", SERRNO
);
2785 * Stop after a certain number of reads so the user does not
2786 * feel like the system is stuttering.
2788 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
2792 * Just wait a bit so it isn't disturbing the
2793 * user and the system is able to write something.
2795 drawtimeout
.tv_sec
= 0;
2796 drawtimeout
.tv_usec
= 5;
2803 while(XPending(xw
.dpy
)) {
2804 XNextEvent(xw
.dpy
, &ev
);
2805 if(XFilterEvent(&ev
, None
))
2807 if(handler
[ev
.type
])
2808 (handler
[ev
.type
])(&ev
);
2817 main(int argc
, char *argv
[]) {
2818 int i
, bitm
, xr
, yr
;
2821 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2824 for(i
= 1; i
< argc
; i
++) {
2825 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2828 opt_class
= argv
[i
];
2831 /* eat every remaining arguments */
2843 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2848 if(bitm
& WidthValue
)
2850 if(bitm
& HeightValue
)
2852 if(bitm
& XNegative
&& xw
.fx
== 0)
2854 if(bitm
& XNegative
&& xw
.fy
== 0)
2857 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2866 opt_title
= argv
[i
];
2873 opt_embed
= argv
[i
];
2879 setlocale(LC_CTYPE
, "");
2880 XSetLocaleModifiers("");