1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/extensions/Xdbe.h>
28 #include <X11/Xft/Xft.h>
29 #include <fontconfig/fontconfig.h>
35 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
37 #elif defined(__FreeBSD__) || defined(__DragonFly__)
42 "st " VERSION " (c) 2010-2012 st engineers\n" \
43 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
44 " [-t title] [-w windowid] [-e command ...]\n"
47 #define XEMBED_FOCUS_IN 4
48 #define XEMBED_FOCUS_OUT 5
51 #define ESC_BUF_SIZ 256
52 #define ESC_ARG_SIZ 16
53 #define STR_BUF_SIZ 256
54 #define STR_ARG_SIZ 16
55 #define DRAW_BUF_SIZ 20*1024
57 #define XK_NO_MOD UINT_MAX
60 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
62 #define SERRNO strerror(errno)
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define MAX(a, b) ((a) < (b) ? (b) : (a))
65 #define LEN(a) (sizeof(a) / sizeof(a[0]))
66 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
67 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
68 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
69 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
70 #define IS_SET(flag) (term.mode & (flag))
71 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
72 #define X2COL(x) (((x) - BORDER)/xw.cw)
73 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
75 #define VT102ID "\033[?6c"
77 enum glyph_attribute
{
87 enum cursor_movement
{
114 MODE_MOUSEMOTION
= 64,
123 ESC_STR
= 4, /* DSC, OSC, PM, APC */
125 ESC_STR_END
= 16, /* a final string was encountered */
126 ESC_TEST
= 32, /* Enter in test mode */
137 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
139 typedef unsigned char uchar
;
140 typedef unsigned int uint
;
141 typedef unsigned long ulong
;
142 typedef unsigned short ushort
;
145 char c
[UTF_SIZ
]; /* character code */
146 uchar mode
; /* attribute flags */
147 ushort fg
; /* foreground */
148 ushort bg
; /* background */
149 uchar state
; /* state flags */
155 Glyph attr
; /* current char attributes */
161 /* CSI Escape sequence structs */
162 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
164 char buf
[ESC_BUF_SIZ
]; /* raw string */
165 int len
; /* raw string length */
167 int arg
[ESC_ARG_SIZ
];
168 int narg
; /* nb of args */
172 /* STR Escape sequence structs */
173 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
175 char type
; /* ESC type ... */
176 char buf
[STR_BUF_SIZ
]; /* raw string */
177 int len
; /* raw string length */
178 char *args
[STR_ARG_SIZ
];
179 int narg
; /* nb of args */
182 /* Internal representation of the screen */
184 int row
; /* nb row */
185 int col
; /* nb col */
186 Line
*line
; /* screen */
187 Line
*alt
; /* alternate screen */
188 bool *dirty
; /* dirtyness of lines */
189 TCursor c
; /* cursor */
190 int top
; /* top scroll limit */
191 int bot
; /* bottom scroll limit */
192 int mode
; /* terminal mode flags */
193 int esc
; /* escape state flags */
197 /* Purely graphic info */
203 Atom xembed
, wmdeletewin
;
209 bool isfixed
; /* is fixed geometry? */
210 int fx
, fy
, fw
, fh
; /* fixed geometry */
211 int tw
, th
; /* tty width and height */
212 int w
; /* window width */
213 int h
; /* window height */
214 int ch
; /* char height */
215 int cw
; /* char width */
216 char state
; /* focus, redraw, visible */
225 /* TODO: use better name for vars... */
236 struct timeval tclick1
;
237 struct timeval tclick2
;
253 /* Drawing Context */
255 XftColor xft_col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
257 Font font
, bfont
, ifont
, ibfont
;
260 static void die(const char *, ...);
261 static void draw(void);
262 static void redraw(void);
263 static void drawregion(int, int, int, int);
264 static void execsh(void);
265 static void sigchld(int);
266 static void run(void);
268 static void csidump(void);
269 static void csihandle(void);
270 static void csiparse(void);
271 static void csireset(void);
272 static void strdump(void);
273 static void strhandle(void);
274 static void strparse(void);
275 static void strreset(void);
277 static void tclearregion(int, int, int, int);
278 static void tcursor(int);
279 static void tdeletechar(int);
280 static void tdeleteline(int);
281 static void tinsertblank(int);
282 static void tinsertblankline(int);
283 static void tmoveto(int, int);
284 static void tnew(int, int);
285 static void tnewline(int);
286 static void tputtab(bool);
287 static void tputc(char *, int);
288 static void treset(void);
289 static int tresize(int, int);
290 static void tscrollup(int, int);
291 static void tscrolldown(int, int);
292 static void tsetattr(int*, int);
293 static void tsetchar(char *, Glyph
*, int, int);
294 static void tsetscroll(int, int);
295 static void tswapscreen(void);
296 static void tsetdirt(int, int);
297 static void tsetmode(bool, bool, int *, int);
298 static void tfulldirt(void);
300 static void ttynew(void);
301 static void ttyread(void);
302 static void ttyresize(void);
303 static void ttywrite(const char *, size_t);
305 static void xdraws(char *, Glyph
, int, int, int, int);
306 static void xhints(void);
307 static void xclear(int, int, int, int);
308 static void xdrawcursor(void);
309 static void xinit(void);
310 static void xloadcols(void);
311 static void xresettitle(void);
312 static void xseturgency(int);
313 static void xsetsel(char*);
314 static void xtermclear(int, int, int, int);
315 static void xresize(int, int);
317 static void expose(XEvent
*);
318 static void visibility(XEvent
*);
319 static void unmap(XEvent
*);
320 static char *kmap(KeySym
, uint
);
321 static void kpress(XEvent
*);
322 static void cmessage(XEvent
*);
323 static void resize(XEvent
*);
324 static void focus(XEvent
*);
325 static void brelease(XEvent
*);
326 static void bpress(XEvent
*);
327 static void bmotion(XEvent
*);
328 static void selnotify(XEvent
*);
329 static void selclear(XEvent
*);
330 static void selrequest(XEvent
*);
332 static void selinit(void);
333 static inline bool selected(int, int);
334 static void selcopy(void);
335 static void selpaste(void);
336 static void selscroll(int, int);
338 static int utf8decode(char *, long *);
339 static int utf8encode(long *, char *);
340 static int utf8size(char *);
341 static int isfullutf8(char *, int);
343 static void *xmalloc(size_t);
344 static void *xrealloc(void *, size_t);
345 static void *xcalloc(size_t nmemb
, size_t size
);
346 static char *smstrcat(char *, ...);
348 static void (*handler
[LASTEvent
])(XEvent
*) = {
350 [ClientMessage
] = cmessage
,
351 [ConfigureNotify
] = resize
,
352 [VisibilityNotify
] = visibility
,
353 [UnmapNotify
] = unmap
,
357 [MotionNotify
] = bmotion
,
358 [ButtonPress
] = bpress
,
359 [ButtonRelease
] = brelease
,
360 [SelectionClear
] = selclear
,
361 [SelectionNotify
] = selnotify
,
362 [SelectionRequest
] = selrequest
,
369 static CSIEscape csiescseq
;
370 static STREscape strescseq
;
373 static Selection sel
;
374 static int iofd
= -1;
375 static char **opt_cmd
= NULL
;
376 static char *opt_io
= NULL
;
377 static char *opt_title
= NULL
;
378 static char *opt_embed
= NULL
;
379 static char *opt_class
= NULL
;
380 static char *opt_font
= NULL
;
383 xmalloc(size_t len
) {
384 void *p
= malloc(len
);
387 die("Out of memory\n");
393 xrealloc(void *p
, size_t len
) {
394 if((p
= realloc(p
, len
)) == NULL
)
395 die("Out of memory\n");
401 xcalloc(size_t nmemb
, size_t size
) {
402 void *p
= calloc(nmemb
, size
);
405 die("Out of memory\n");
411 smstrcat(char *src
, ...)
417 len
= slen
= strlen(src
);
419 va_start(fmtargs
, src
);
421 v
= va_arg(fmtargs
, char *);
428 p
= ret
= xmalloc(len
+1);
429 memmove(p
, src
, slen
);
432 va_start(fmtargs
, src
);
434 v
= va_arg(fmtargs
, char *);
449 utf8decode(char *s
, long *u
) {
455 if(~c
& B7
) { /* 0xxxxxxx */
458 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
459 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
461 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
462 *u
= c
&(B3
|B2
|B1
|B0
);
464 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
471 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
473 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
476 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
479 if((n
== 1 && *u
< 0x80) ||
480 (n
== 2 && *u
< 0x800) ||
481 (n
== 3 && *u
< 0x10000) ||
482 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
494 utf8encode(long *u
, char *s
) {
502 *sp
= uc
; /* 0xxxxxxx */
504 } else if(*u
< 0x800) {
505 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
507 } else if(uc
< 0x10000) {
508 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
510 } else if(uc
<= 0x10FFFF) {
511 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
517 for(i
=n
,++sp
; i
>0; --i
,++sp
)
518 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
530 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
531 UTF-8 otherwise return 0 */
533 isfullutf8(char *s
, int b
) {
541 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
543 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
545 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
547 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
549 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
550 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
563 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
565 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
574 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
575 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
579 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
580 if(sel
.xtarget
== None
)
581 sel
.xtarget
= XA_STRING
;
585 selected(int x
, int y
) {
588 if(sel
.ey
== y
&& sel
.by
== y
) {
589 bx
= MIN(sel
.bx
, sel
.ex
);
590 ex
= MAX(sel
.bx
, sel
.ex
);
591 return BETWEEN(x
, bx
, ex
);
594 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
595 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
596 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
597 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
601 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
603 *b
= e
->xbutton
.button
;
605 *x
= X2COL(e
->xbutton
.x
);
606 *y
= Y2ROW(e
->xbutton
.y
);
607 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
608 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
609 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
610 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
614 mousereport(XEvent
*e
) {
615 int x
= X2COL(e
->xbutton
.x
);
616 int y
= Y2ROW(e
->xbutton
.y
);
617 int button
= e
->xbutton
.button
;
618 int state
= e
->xbutton
.state
;
619 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
620 static int ob
, ox
, oy
;
623 if(e
->xbutton
.type
== MotionNotify
) {
624 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
628 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
634 if(e
->xbutton
.type
== ButtonPress
) {
640 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
641 + (state
& Mod4Mask
? 8 : 0)
642 + (state
& ControlMask
? 16 : 0);
644 ttywrite(buf
, sizeof(buf
));
649 if(IS_SET(MODE_MOUSE
)) {
651 } else if(e
->xbutton
.button
== Button1
) {
654 tsetdirt(sel
.b
.y
, sel
.e
.y
);
658 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
659 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
666 int x
, y
, bufsize
, is_selected
= 0, size
;
672 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
673 ptr
= str
= xmalloc(bufsize
);
675 /* append every set & selected glyph to the selection */
676 for(y
= 0; y
< term
.row
; y
++) {
677 for(x
= 0; x
< term
.col
; x
++) {
678 gp
= &term
.line
[y
][x
];
680 if(!(is_selected
= selected(x
, y
)))
682 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
684 memcpy(ptr
, p
, size
);
687 /* \n at the end of every selected line except for the last one */
688 if(is_selected
&& y
< sel
.e
.y
)
693 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
698 selnotify(XEvent
*e
) {
699 ulong nitems
, ofs
, rem
;
706 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
707 False
, AnyPropertyType
, &type
, &format
,
708 &nitems
, &rem
, &data
)) {
709 fprintf(stderr
, "Clipboard allocation failed\n");
712 ttywrite((const char *) data
, nitems
* format
/ 8);
714 /* number of 32-bit chunks returned */
715 ofs
+= nitems
* format
/ 32;
721 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
722 xw
.win
, CurrentTime
);
725 void selclear(XEvent
*e
) {
729 tsetdirt(sel
.b
.y
, sel
.e
.y
);
733 selrequest(XEvent
*e
) {
734 XSelectionRequestEvent
*xsre
;
736 Atom xa_targets
, string
;
738 xsre
= (XSelectionRequestEvent
*) e
;
739 xev
.type
= SelectionNotify
;
740 xev
.requestor
= xsre
->requestor
;
741 xev
.selection
= xsre
->selection
;
742 xev
.target
= xsre
->target
;
743 xev
.time
= xsre
->time
;
747 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
748 if(xsre
->target
== xa_targets
) {
749 /* respond with the supported type */
750 string
= sel
.xtarget
;
751 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
752 XA_ATOM
, 32, PropModeReplace
,
753 (uchar
*) &string
, 1);
754 xev
.property
= xsre
->property
;
755 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
756 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
757 xsre
->target
, 8, PropModeReplace
,
758 (uchar
*) sel
.clip
, strlen(sel
.clip
));
759 xev
.property
= xsre
->property
;
762 /* all done, send a notification to the listener */
763 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
764 fprintf(stderr
, "Error sending SelectionNotify event\n");
769 /* register the selection for both the clipboard and the primary */
775 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
777 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
778 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
782 brelease(XEvent
*e
) {
785 if(IS_SET(MODE_MOUSE
)) {
790 if(e
->xbutton
.button
== Button2
) {
792 } else if(e
->xbutton
.button
== Button1
) {
794 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
795 term
.dirty
[sel
.ey
] = 1;
796 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
798 gettimeofday(&now
, NULL
);
800 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
801 /* triple click on the line */
802 sel
.b
.x
= sel
.bx
= 0;
803 sel
.e
.x
= sel
.ex
= term
.col
;
804 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
806 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
807 /* double click to select word */
809 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
810 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
814 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
815 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
819 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
827 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
828 gettimeofday(&sel
.tclick1
, NULL
);
833 int starty
, endy
, oldey
, oldex
;
835 if(IS_SET(MODE_MOUSE
)) {
843 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
845 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
846 starty
= MIN(oldey
, sel
.ey
);
847 endy
= MAX(oldey
, sel
.ey
);
848 tsetdirt(starty
, endy
);
854 die(const char *errstr
, ...) {
857 va_start(ap
, errstr
);
858 vfprintf(stderr
, errstr
, ap
);
866 char *envshell
= getenv("SHELL");
872 signal(SIGCHLD
, SIG_DFL
);
873 signal(SIGHUP
, SIG_DFL
);
874 signal(SIGINT
, SIG_DFL
);
875 signal(SIGQUIT
, SIG_DFL
);
876 signal(SIGTERM
, SIG_DFL
);
877 signal(SIGALRM
, SIG_DFL
);
879 DEFAULT(envshell
, SHELL
);
880 putenv("TERM="TNAME
);
881 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
882 execvp(args
[0], args
);
890 if(waitpid(pid
, &stat
, 0) < 0)
891 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
893 if(WIFEXITED(stat
)) {
894 exit(WEXITSTATUS(stat
));
903 struct winsize w
= {term
.row
, term
.col
, 0, 0};
905 /* seems to work fine on linux, openbsd and freebsd */
906 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
907 die("openpty failed: %s\n", SERRNO
);
909 switch(pid
= fork()) {
911 die("fork failed\n");
914 setsid(); /* create a new process group */
915 dup2(s
, STDIN_FILENO
);
916 dup2(s
, STDOUT_FILENO
);
917 dup2(s
, STDERR_FILENO
);
918 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
919 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
927 signal(SIGCHLD
, sigchld
);
929 if(!strcmp(opt_io
, "-")) {
930 iofd
= STDOUT_FILENO
;
932 if((iofd
= open(opt_io
, O_WRONLY
| O_CREAT
, 0666)) < 0) {
933 fprintf(stderr
, "Error opening %s:%s\n",
934 opt_io
, strerror(errno
));
945 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
947 fprintf(stderr
, "\n");
952 static char buf
[BUFSIZ
];
953 static int buflen
= 0;
956 int charsize
; /* size of utf8 char in bytes */
960 /* append read bytes to unprocessed bytes */
961 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
962 die("Couldn't read from shell: %s\n", SERRNO
);
964 /* process every complete utf8 char */
967 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
968 charsize
= utf8decode(ptr
, &utf8c
);
969 utf8encode(&utf8c
, s
);
975 /* keep any uncomplete utf8 char for the next call */
976 memmove(buf
, ptr
, buflen
);
980 ttywrite(const char *s
, size_t n
) {
981 if(write(cmdfd
, s
, n
) == -1)
982 die("write error on tty: %s\n", SERRNO
);
993 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
994 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
998 tsetdirt(int top
, int bot
) {
1001 LIMIT(top
, 0, term
.row
-1);
1002 LIMIT(bot
, 0, term
.row
-1);
1004 for(i
= top
; i
<= bot
; i
++)
1010 tsetdirt(0, term
.row
-1);
1017 if(mode
== CURSOR_SAVE
) {
1019 } else if(mode
== CURSOR_LOAD
) {
1029 term
.c
= (TCursor
){{
1033 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1035 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1036 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
1039 term
.bot
= term
.row
- 1;
1040 term
.mode
= MODE_WRAP
;
1042 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1046 tnew(int col
, int row
) {
1047 /* set screen size */
1050 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1051 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1052 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1053 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1055 for(row
= 0; row
< term
.row
; row
++) {
1056 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1057 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1058 term
.dirty
[row
] = 0;
1060 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1067 Line
*tmp
= term
.line
;
1069 term
.line
= term
.alt
;
1071 term
.mode
^= MODE_ALTSCREEN
;
1076 tscrolldown(int orig
, int n
) {
1080 LIMIT(n
, 0, term
.bot
-orig
+1);
1082 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1084 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1085 temp
= term
.line
[i
];
1086 term
.line
[i
] = term
.line
[i
-n
];
1087 term
.line
[i
-n
] = temp
;
1090 term
.dirty
[i
-n
] = 1;
1097 tscrollup(int orig
, int n
) {
1100 LIMIT(n
, 0, term
.bot
-orig
+1);
1102 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1104 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1105 temp
= term
.line
[i
];
1106 term
.line
[i
] = term
.line
[i
+n
];
1107 term
.line
[i
+n
] = temp
;
1110 term
.dirty
[i
+n
] = 1;
1113 selscroll(orig
, -n
);
1117 selscroll(int orig
, int n
) {
1121 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1122 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1126 if(sel
.by
< term
.top
) {
1130 if(sel
.ey
> term
.bot
) {
1134 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1135 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1140 tnewline(int first_col
) {
1144 tscrollup(term
.top
, 1);
1148 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1153 /* int noarg = 1; */
1154 char *p
= csiescseq
.buf
;
1158 csiescseq
.priv
= 1, p
++;
1160 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1161 while(isdigit(*p
)) {
1162 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1163 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1165 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1166 csiescseq
.narg
++, p
++;
1168 csiescseq
.mode
= *p
;
1177 tmoveto(int x
, int y
) {
1178 LIMIT(x
, 0, term
.col
-1);
1179 LIMIT(y
, 0, term
.row
-1);
1180 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1186 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1187 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1188 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1189 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1190 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1191 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1192 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1193 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1194 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1195 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1199 * The table is proudly stolen from rxvt.
1201 if(attr
->mode
& ATTR_GFX
) {
1202 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1203 && vt100_0
[c
[0] - 0x41]) {
1204 c
= vt100_0
[c
[0] - 0x41];
1209 term
.line
[y
][x
] = *attr
;
1210 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1211 term
.line
[y
][x
].state
|= GLYPH_SET
;
1215 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1219 temp
= x1
, x1
= x2
, x2
= temp
;
1221 temp
= y1
, y1
= y2
, y2
= temp
;
1223 LIMIT(x1
, 0, term
.col
-1);
1224 LIMIT(x2
, 0, term
.col
-1);
1225 LIMIT(y1
, 0, term
.row
-1);
1226 LIMIT(y2
, 0, term
.row
-1);
1228 for(y
= y1
; y
<= y2
; y
++) {
1230 for(x
= x1
; x
<= x2
; x
++)
1231 term
.line
[y
][x
].state
= 0;
1236 tdeletechar(int n
) {
1237 int src
= term
.c
.x
+ n
;
1239 int size
= term
.col
- src
;
1241 term
.dirty
[term
.c
.y
] = 1;
1243 if(src
>= term
.col
) {
1244 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1248 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1249 size
* sizeof(Glyph
));
1250 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1254 tinsertblank(int n
) {
1257 int size
= term
.col
- dst
;
1259 term
.dirty
[term
.c
.y
] = 1;
1261 if(dst
>= term
.col
) {
1262 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1266 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1267 size
* sizeof(Glyph
));
1268 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1272 tinsertblankline(int n
) {
1273 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1276 tscrolldown(term
.c
.y
, n
);
1280 tdeleteline(int n
) {
1281 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1284 tscrollup(term
.c
.y
, n
);
1288 tsetattr(int *attr
, int l
) {
1291 for(i
= 0; i
< l
; i
++) {
1294 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1295 | ATTR_ITALIC
| ATTR_BLINK
);
1296 term
.c
.attr
.fg
= DefaultFG
;
1297 term
.c
.attr
.bg
= DefaultBG
;
1300 term
.c
.attr
.mode
|= ATTR_BOLD
;
1302 case 3: /* enter standout (highlight) */
1303 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1306 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1309 term
.c
.attr
.mode
|= ATTR_BLINK
;
1312 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1316 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1318 case 23: /* leave standout (highlight) mode */
1319 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1322 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1325 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1328 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1331 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1333 if(BETWEEN(attr
[i
], 0, 255)) {
1334 term
.c
.attr
.fg
= attr
[i
];
1337 "erresc: bad fgcolor %d\n",
1342 "erresc(38): gfx attr %d unknown\n",
1347 term
.c
.attr
.fg
= DefaultFG
;
1350 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1352 if(BETWEEN(attr
[i
], 0, 255)) {
1353 term
.c
.attr
.bg
= attr
[i
];
1356 "erresc: bad bgcolor %d\n",
1361 "erresc(48): gfx attr %d unknown\n",
1366 term
.c
.attr
.bg
= DefaultBG
;
1369 if(BETWEEN(attr
[i
], 30, 37)) {
1370 term
.c
.attr
.fg
= attr
[i
] - 30;
1371 } else if(BETWEEN(attr
[i
], 40, 47)) {
1372 term
.c
.attr
.bg
= attr
[i
] - 40;
1373 } else if(BETWEEN(attr
[i
], 90, 97)) {
1374 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1375 } else if(BETWEEN(attr
[i
], 100, 107)) {
1376 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1379 "erresc(default): gfx attr %d unknown\n",
1380 attr
[i
]), csidump();
1388 tsetscroll(int t
, int b
) {
1391 LIMIT(t
, 0, term
.row
-1);
1392 LIMIT(b
, 0, term
.row
-1);
1402 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1405 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1408 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1412 case 1: /* DECCKM -- Cursor key */
1413 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1415 case 5: /* DECSCNM -- Reverse video */
1417 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1418 if(mode
!= term
.mode
)
1421 case 6: /* XXX: DECOM -- Origin */
1423 case 7: /* DECAWM -- Auto wrap */
1424 MODBIT(term
.mode
, set
, MODE_WRAP
);
1426 case 8: /* XXX: DECARM -- Auto repeat */
1428 case 0: /* Error (IGNORED) */
1429 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1432 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1434 case 1000: /* 1000,1002: enable xterm mouse report */
1435 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1438 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1440 case 1049: /* = 1047 and 1048 */
1443 if(IS_SET(MODE_ALTSCREEN
))
1444 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1445 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1446 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1453 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1456 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1457 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1458 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1459 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1460 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1461 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1463 "erresc: unknown private set/reset mode %d\n",
1469 case 0: /* Error (IGNORED) */
1471 case 2: /* KAM -- keyboard action */
1472 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1474 case 4: /* IRM -- Insertion-replacement */
1475 MODBIT(term
.mode
, set
, MODE_INSERT
);
1477 case 12: /* XXX: SRM -- Send/Receive */
1479 case 20: /* LNM -- Linefeed/new line */
1480 MODBIT(term
.mode
, set
, MODE_CRLF
);
1484 "erresc: unknown set/reset mode %d\n",
1496 switch(csiescseq
.mode
) {
1499 fprintf(stderr
, "erresc: unknown csi ");
1503 case '@': /* ICH -- Insert <n> blank char */
1504 DEFAULT(csiescseq
.arg
[0], 1);
1505 tinsertblank(csiescseq
.arg
[0]);
1507 case 'A': /* CUU -- Cursor <n> Up */
1509 DEFAULT(csiescseq
.arg
[0], 1);
1510 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1512 case 'B': /* CUD -- Cursor <n> Down */
1513 DEFAULT(csiescseq
.arg
[0], 1);
1514 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1516 case 'c': /* DA -- Device Attributes */
1517 if(csiescseq
.arg
[0] == 0)
1518 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1520 case 'C': /* CUF -- Cursor <n> Forward */
1522 DEFAULT(csiescseq
.arg
[0], 1);
1523 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1525 case 'D': /* CUB -- Cursor <n> Backward */
1526 DEFAULT(csiescseq
.arg
[0], 1);
1527 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1529 case 'E': /* CNL -- Cursor <n> Down and first col */
1530 DEFAULT(csiescseq
.arg
[0], 1);
1531 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1533 case 'F': /* CPL -- Cursor <n> Up and first col */
1534 DEFAULT(csiescseq
.arg
[0], 1);
1535 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1537 case 'g': /* TBC -- Tabulation clear */
1538 switch (csiescseq
.arg
[0]) {
1539 case 0: /* clear current tab stop */
1540 term
.tabs
[term
.c
.x
] = 0;
1542 case 3: /* clear all the tabs */
1543 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1549 case 'G': /* CHA -- Move to <col> */
1551 DEFAULT(csiescseq
.arg
[0], 1);
1552 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1554 case 'H': /* CUP -- Move to <row> <col> */
1556 DEFAULT(csiescseq
.arg
[0], 1);
1557 DEFAULT(csiescseq
.arg
[1], 1);
1558 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1560 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1561 DEFAULT(csiescseq
.arg
[0], 1);
1562 while(csiescseq
.arg
[0]--)
1565 case 'J': /* ED -- Clear screen */
1567 switch(csiescseq
.arg
[0]) {
1569 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1570 if(term
.c
.y
< term
.row
-1)
1571 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1575 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1576 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1579 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1585 case 'K': /* EL -- Clear line */
1586 switch(csiescseq
.arg
[0]) {
1588 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1591 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1594 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1598 case 'S': /* SU -- Scroll <n> line up */
1599 DEFAULT(csiescseq
.arg
[0], 1);
1600 tscrollup(term
.top
, csiescseq
.arg
[0]);
1602 case 'T': /* SD -- Scroll <n> line down */
1603 DEFAULT(csiescseq
.arg
[0], 1);
1604 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1606 case 'L': /* IL -- Insert <n> blank lines */
1607 DEFAULT(csiescseq
.arg
[0], 1);
1608 tinsertblankline(csiescseq
.arg
[0]);
1610 case 'l': /* RM -- Reset Mode */
1611 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1613 case 'M': /* DL -- Delete <n> lines */
1614 DEFAULT(csiescseq
.arg
[0], 1);
1615 tdeleteline(csiescseq
.arg
[0]);
1617 case 'X': /* ECH -- Erase <n> char */
1618 DEFAULT(csiescseq
.arg
[0], 1);
1619 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1621 case 'P': /* DCH -- Delete <n> char */
1622 DEFAULT(csiescseq
.arg
[0], 1);
1623 tdeletechar(csiescseq
.arg
[0]);
1625 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1626 DEFAULT(csiescseq
.arg
[0], 1);
1627 while(csiescseq
.arg
[0]--)
1630 case 'd': /* VPA -- Move to <row> */
1631 DEFAULT(csiescseq
.arg
[0], 1);
1632 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1634 case 'h': /* SM -- Set terminal mode */
1635 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1637 case 'm': /* SGR -- Terminal attribute (color) */
1638 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1640 case 'r': /* DECSTBM -- Set Scrolling Region */
1641 if(csiescseq
.priv
) {
1644 DEFAULT(csiescseq
.arg
[0], 1);
1645 DEFAULT(csiescseq
.arg
[1], term
.row
);
1646 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1650 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1651 tcursor(CURSOR_SAVE
);
1653 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1654 tcursor(CURSOR_LOAD
);
1665 for(i
= 0; i
< csiescseq
.len
; i
++) {
1666 c
= csiescseq
.buf
[i
] & 0xff;
1669 } else if(c
== '\n') {
1671 } else if(c
== '\r') {
1673 } else if(c
== 0x1b) {
1676 printf("(%02x)", c
);
1684 memset(&csiescseq
, 0, sizeof(csiescseq
));
1692 * TODO: make this being useful in case of color palette change.
1698 switch(strescseq
.type
) {
1699 case ']': /* OSC -- Operating System Command */
1705 * TODO: Handle special chars in string, like umlauts.
1708 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1712 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1714 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1717 fprintf(stderr
, "erresc: unknown str ");
1722 case 'k': /* old title set compatibility */
1723 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1725 case 'P': /* DSC -- Device Control String */
1726 case '_': /* APC -- Application Program Command */
1727 case '^': /* PM -- Privacy Message */
1729 fprintf(stderr
, "erresc: unknown str ");
1739 * TODO: Implement parsing like for CSI when required.
1740 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1750 printf("ESC%c", strescseq
.type
);
1751 for(i
= 0; i
< strescseq
.len
; i
++) {
1752 c
= strescseq
.buf
[i
] & 0xff;
1755 } else if(c
== '\n') {
1757 } else if(c
== '\r') {
1759 } else if(c
== 0x1b) {
1762 printf("(%02x)", c
);
1770 memset(&strescseq
, 0, sizeof(strescseq
));
1774 tputtab(bool forward
) {
1780 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1785 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1788 tmoveto(x
, term
.c
.y
);
1792 tputc(char *c
, int len
) {
1794 bool control
= ascii
< '\x20' || ascii
== 0177;
1797 write(iofd
, c
, len
);
1799 * STR sequences must be checked before of anything
1800 * because it can use some control codes as part of the sequence
1802 if(term
.esc
& ESC_STR
) {
1805 term
.esc
= ESC_START
| ESC_STR_END
;
1807 case '\a': /* backwards compatibility to xterm */
1812 strescseq
.buf
[strescseq
.len
++] = ascii
;
1813 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1821 * Actions of control codes must be performed as soon they arrive
1822 * because they can be embedded inside a control sequence, and
1823 * they must not cause conflicts with sequences.
1831 tmoveto(term
.c
.x
-1, term
.c
.y
);
1834 tmoveto(0, term
.c
.y
);
1839 /* go to first col if the mode is set */
1840 tnewline(IS_SET(MODE_CRLF
));
1842 case '\a': /* BEL */
1843 if(!(xw
.state
& WIN_FOCUSED
))
1846 case '\033': /* ESC */
1848 term
.esc
= ESC_START
;
1850 case '\016': /* SO */
1851 term
.c
.attr
.mode
|= ATTR_GFX
;
1853 case '\017': /* SI */
1854 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1856 case '\032': /* SUB */
1857 case '\030': /* CAN */
1860 case '\005': /* ENQ (IGNORED) */
1861 case '\000': /* NUL (IGNORED) */
1862 case '\021': /* XON (IGNORED) */
1863 case '\023': /* XOFF (IGNORED) */
1864 case 0177: /* DEL (IGNORED) */
1867 } else if(term
.esc
& ESC_START
) {
1868 if(term
.esc
& ESC_CSI
) {
1869 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1870 if(BETWEEN(ascii
, 0x40, 0x7E)
1871 || csiescseq
.len
>= ESC_BUF_SIZ
) {
1873 csiparse(), csihandle();
1875 } else if(term
.esc
& ESC_STR_END
) {
1879 } else if(term
.esc
& ESC_ALTCHARSET
) {
1881 case '0': /* Line drawing set */
1882 term
.c
.attr
.mode
|= ATTR_GFX
;
1884 case 'B': /* USASCII */
1885 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1887 case 'A': /* UK (IGNORED) */
1888 case '<': /* multinational charset (IGNORED) */
1889 case '5': /* Finnish (IGNORED) */
1890 case 'C': /* Finnish (IGNORED) */
1891 case 'K': /* German (IGNORED) */
1894 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1897 } else if(term
.esc
& ESC_TEST
) {
1898 if(ascii
== '8') { /* DEC screen alignment test. */
1899 char E
[UTF_SIZ
] = "E";
1902 for(x
= 0; x
< term
.col
; ++x
) {
1903 for(y
= 0; y
< term
.row
; ++y
)
1904 tsetchar(E
, &term
.c
.attr
, x
, y
);
1911 term
.esc
|= ESC_CSI
;
1914 term
.esc
|= ESC_TEST
;
1916 case 'P': /* DCS -- Device Control String */
1917 case '_': /* APC -- Application Program Command */
1918 case '^': /* PM -- Privacy Message */
1919 case ']': /* OSC -- Operating System Command */
1920 case 'k': /* old title set compatibility */
1922 strescseq
.type
= ascii
;
1923 term
.esc
|= ESC_STR
;
1925 case '(': /* set primary charset G0 */
1926 term
.esc
|= ESC_ALTCHARSET
;
1928 case ')': /* set secondary charset G1 (IGNORED) */
1929 case '*': /* set tertiary charset G2 (IGNORED) */
1930 case '+': /* set quaternary charset G3 (IGNORED) */
1933 case 'D': /* IND -- Linefeed */
1934 if(term
.c
.y
== term
.bot
) {
1935 tscrollup(term
.top
, 1);
1937 tmoveto(term
.c
.x
, term
.c
.y
+1);
1941 case 'E': /* NEL -- Next line */
1942 tnewline(1); /* always go to first col */
1945 case 'H': /* HTS -- Horizontal tab stop */
1946 term
.tabs
[term
.c
.x
] = 1;
1949 case 'M': /* RI -- Reverse index */
1950 if(term
.c
.y
== term
.top
) {
1951 tscrolldown(term
.top
, 1);
1953 tmoveto(term
.c
.x
, term
.c
.y
-1);
1957 case 'Z': /* DECID -- Identify Terminal */
1958 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1961 case 'c': /* RIS -- Reset to inital state */
1966 case '=': /* DECPAM -- Application keypad */
1967 term
.mode
|= MODE_APPKEYPAD
;
1970 case '>': /* DECPNM -- Normal keypad */
1971 term
.mode
&= ~MODE_APPKEYPAD
;
1974 case '7': /* DECSC -- Save Cursor */
1975 tcursor(CURSOR_SAVE
);
1978 case '8': /* DECRC -- Restore Cursor */
1979 tcursor(CURSOR_LOAD
);
1982 case '\\': /* ST -- Stop */
1986 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1987 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
1992 * All characters which forms part of a sequence are not
1998 * Display control codes only if we are in graphic mode
2000 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2002 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2004 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2005 tnewline(1); /* always go to first col */
2006 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2007 if(term
.c
.x
+1 < term
.col
)
2008 tmoveto(term
.c
.x
+1, term
.c
.y
);
2010 term
.c
.state
|= CURSOR_WRAPNEXT
;
2014 tresize(int col
, int row
) {
2016 int minrow
= MIN(row
, term
.row
);
2017 int mincol
= MIN(col
, term
.col
);
2018 int slide
= term
.c
.y
- row
+ 1;
2021 if(col
< 1 || row
< 1)
2024 /* free unneeded rows */
2027 /* slide screen to keep cursor where we expect it -
2028 * tscrollup would work here, but we can optimize to
2029 * memmove because we're freeing the earlier lines */
2030 for(/* i = 0 */; i
< slide
; i
++) {
2034 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2035 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2037 for(i
+= row
; i
< term
.row
; i
++) {
2042 /* resize to new height */
2043 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2044 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2045 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2046 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2048 /* resize each row to new width, zero-pad if needed */
2049 for(i
= 0; i
< minrow
; i
++) {
2051 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2052 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2053 for(x
= mincol
; x
< col
; x
++) {
2054 term
.line
[i
][x
].state
= 0;
2055 term
.alt
[i
][x
].state
= 0;
2059 /* allocate any new rows */
2060 for(/* i == minrow */; i
< row
; i
++) {
2062 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2063 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2065 if(col
> term
.col
) {
2066 bp
= term
.tabs
+ term
.col
;
2068 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2069 while(--bp
> term
.tabs
&& !*bp
)
2071 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
2074 /* update terminal size */
2075 term
.col
= col
, term
.row
= row
;
2076 /* make use of the LIMIT in tmoveto */
2077 tmoveto(term
.c
.x
, term
.c
.y
);
2078 /* reset scrolling region */
2079 tsetscroll(0, row
-1);
2085 xresize(int col
, int row
) {
2086 xw
.tw
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
2087 xw
.th
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
2089 XftDrawChange(xw
.xft_draw
, xw
.buf
);
2095 XRenderColor xft_color
= { .alpha
= 0 };
2097 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2098 for(i
= 0; i
< LEN(colorname
); i
++) {
2101 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.xft_col
[i
])) {
2102 die("Could not allocate color '%s'\n", colorname
[i
]);
2106 /* load colors [16-255] ; same colors as xterm */
2107 for(i
= 16, r
= 0; r
< 6; r
++) {
2108 for(g
= 0; g
< 6; g
++) {
2109 for(b
= 0; b
< 6; b
++) {
2110 xft_color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2111 xft_color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2112 xft_color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2113 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
2114 die("Could not allocate color %d\n", i
);
2121 for(r
= 0; r
< 24; r
++, i
++) {
2122 xft_color
.red
= xft_color
.green
= xft_color
.blue
= 0x0808 + 0x0a0a * r
;
2123 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
,
2125 die("Could not allocate color %d\n", i
);
2131 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2132 XftDrawRect(xw
.xft_draw
,
2133 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2134 BORDER
+ col1
* xw
.cw
,
2135 BORDER
+ row1
* xw
.ch
,
2136 (col2
-col1
+1) * xw
.cw
,
2137 (row2
-row1
+1) * xw
.ch
);
2141 * Absolute coordinates.
2144 xclear(int x1
, int y1
, int x2
, int y2
) {
2145 XftDrawRect(xw
.xft_draw
,
2146 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2147 x1
, y1
, x2
-x1
, y2
-y1
);
2152 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
2153 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2154 XSizeHints
*sizeh
= NULL
;
2156 sizeh
= XAllocSizeHints();
2157 if(xw
.isfixed
== False
) {
2158 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2159 sizeh
->height
= xw
.h
;
2160 sizeh
->width
= xw
.w
;
2161 sizeh
->height_inc
= xw
.ch
;
2162 sizeh
->width_inc
= xw
.cw
;
2163 sizeh
->base_height
= 2*BORDER
;
2164 sizeh
->base_width
= 2*BORDER
;
2166 sizeh
->flags
= PMaxSize
| PMinSize
;
2167 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2168 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2171 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2176 xinitfont(Font
*f
, char *fontstr
) {
2177 FcPattern
*pattern
, *match
;
2180 pattern
= FcNameParse((FcChar8
*)fontstr
);
2182 die("st: can't open font %s\n", fontstr
);
2184 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
2185 FcPatternDestroy(pattern
);
2187 die("st: can't open font %s\n", fontstr
);
2188 if(!(f
->xft_set
= XftFontOpenPattern(xw
.dpy
, match
))) {
2189 FcPatternDestroy(match
);
2190 die("st: can't open font %s.\n", fontstr
);
2193 f
->ascent
= f
->xft_set
->ascent
;
2194 f
->descent
= f
->xft_set
->descent
;
2196 f
->rbearing
= f
->xft_set
->max_advance_width
;
2198 f
->height
= f
->xft_set
->height
;
2199 f
->width
= f
->lbearing
+ f
->rbearing
;
2203 initfonts(char *fontstr
) {
2206 xinitfont(&dc
.font
, fontstr
);
2207 xw
.cw
= dc
.font
.width
;
2208 xw
.ch
= dc
.font
.height
;
2210 fstr
= smstrcat(fontstr
, ":weight=bold", NULL
);
2211 xinitfont(&dc
.bfont
, fstr
);
2214 fstr
= smstrcat(fontstr
, ":slant=italic,oblique", NULL
);
2215 xinitfont(&dc
.ifont
, fstr
);
2218 fstr
= smstrcat(fontstr
, ":weight=bold:slant=italic,oblique", NULL
);
2219 xinitfont(&dc
.ibfont
, fstr
);
2225 XSetWindowAttributes attrs
;
2228 int sw
, sh
, major
, minor
;
2230 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2231 die("Can't open display\n");
2232 xw
.scr
= XDefaultScreen(xw
.dpy
);
2233 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2236 initfonts((opt_font
!= NULL
)? opt_font
: FONT
);
2239 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2242 /* adjust fixed window geometry */
2244 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2245 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2247 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2249 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2254 /* window - default size */
2255 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
2256 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
2261 attrs
.background_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2262 attrs
.border_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2263 attrs
.bit_gravity
= NorthWestGravity
;
2264 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2265 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2266 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2267 attrs
.colormap
= xw
.cmap
;
2269 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2270 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2271 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2273 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2277 /* double buffering */
2278 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2279 die("Xdbe extension is not present\n");
2280 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2282 /* Xft rendering context */
2283 xw
.xft_draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2286 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2287 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2288 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2289 XNFocusWindow
, xw
.win
, NULL
);
2291 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2293 /* white cursor, black outline */
2294 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2295 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2296 XRecolorCursor(xw
.dpy
, cursor
,
2297 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2298 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2300 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2301 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2302 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2305 XMapWindow(xw
.dpy
, xw
.win
);
2311 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2312 int winx
= BORDER
+ x
* xw
.cw
, winy
= BORDER
+ y
* xw
.ch
,
2313 width
= charlen
* xw
.cw
;
2314 Font
*font
= &dc
.font
;
2316 XftColor
*fg
= &dc
.xft_col
[base
.fg
], *bg
= &dc
.xft_col
[base
.bg
],
2317 *temp
, revfg
, revbg
;
2318 XRenderColor colfg
, colbg
;
2320 if(base
.mode
& ATTR_REVERSE
)
2321 temp
= fg
, fg
= bg
, bg
= temp
;
2323 if(base
.mode
& ATTR_BOLD
) {
2324 if(BETWEEN(base
.fg
, 0, 7)) {
2325 /* basic system colors */
2326 fg
= &dc
.xft_col
[base
.fg
+ 8];
2327 } else if(BETWEEN(base
.fg
, 16, 195)) {
2329 fg
= &dc
.xft_col
[base
.fg
+ 36];
2330 } else if(BETWEEN(base
.fg
, 232, 251)) {
2332 fg
= &dc
.xft_col
[base
.fg
+ 4];
2335 * Those ranges will not be brightened:
2336 * 8 - 15 – bright system colors
2337 * 196 - 231 – highest 256 color cube
2338 * 252 - 255 – brightest colors in greyscale
2343 if(base
.mode
& ATTR_ITALIC
)
2345 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2348 if(IS_SET(MODE_REVERSE
)) {
2349 if(fg
== &dc
.xft_col
[DefaultFG
]) {
2350 fg
= &dc
.xft_col
[DefaultBG
];
2352 colfg
.red
= ~fg
->color
.red
;
2353 colfg
.green
= ~fg
->color
.green
;
2354 colfg
.blue
= ~fg
->color
.blue
;
2355 colfg
.alpha
= fg
->color
.alpha
;
2356 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2360 if(bg
== &dc
.xft_col
[DefaultBG
]) {
2361 bg
= &dc
.xft_col
[DefaultFG
];
2363 colbg
.red
= ~bg
->color
.red
;
2364 colbg
.green
= ~bg
->color
.green
;
2365 colbg
.blue
= ~bg
->color
.blue
;
2366 colbg
.alpha
= bg
->color
.alpha
;
2367 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2372 XftTextExtentsUtf8(xw
.dpy
, font
->xft_set
, (FcChar8
*)s
, bytelen
,
2374 width
= extents
.xOff
;
2376 /* Intelligent cleaning up of the borders. */
2378 xclear(0, (y
== 0)? 0 : winy
, BORDER
,
2379 winy
+ xw
.ch
+ (y
== term
.row
-1)? xw
.h
: 0);
2381 if(x
+ charlen
>= term
.col
-1) {
2382 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2383 (y
== term
.row
-1)? xw
.h
: (winy
+ xw
.ch
));
2386 xclear(winx
, 0, winx
+ width
, BORDER
);
2388 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2390 XftDrawRect(xw
.xft_draw
, bg
, winx
, winy
, width
, xw
.ch
);
2391 XftDrawStringUtf8(xw
.xft_draw
, fg
, font
->xft_set
, winx
,
2392 winy
+ font
->ascent
, (FcChar8
*)s
, bytelen
);
2394 if(base
.mode
& ATTR_UNDERLINE
) {
2395 XftDrawRect(xw
.xft_draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2402 static int oldx
= 0, oldy
= 0;
2404 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2406 LIMIT(oldx
, 0, term
.col
-1);
2407 LIMIT(oldy
, 0, term
.row
-1);
2409 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2410 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2412 /* remove the old cursor */
2413 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2414 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2415 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2418 xtermclear(oldx
, oldy
, oldx
, oldy
);
2421 /* draw the new one */
2422 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2423 if(!(xw
.state
& WIN_FOCUSED
))
2426 if(IS_SET(MODE_REVERSE
))
2427 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2430 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2431 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2437 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2442 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2446 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2447 nanosleep(&tv
, NULL
);
2452 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2454 drawregion(0, 0, term
.col
, term
.row
);
2455 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2459 drawregion(int x1
, int y1
, int x2
, int y2
) {
2460 int ic
, ib
, x
, y
, ox
, sl
;
2462 char buf
[DRAW_BUF_SIZ
];
2463 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2465 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2467 if(!(xw
.state
& WIN_VISIBLE
))
2470 for(y
= y1
; y
< y2
; y
++) {
2474 xtermclear(0, y
, term
.col
, y
);
2476 base
= term
.line
[y
][0];
2478 for(x
= x1
; x
< x2
; x
++) {
2479 new = term
.line
[y
][x
];
2480 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2481 new.mode
^= ATTR_REVERSE
;
2482 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2483 || ATTRCMP(base
, new)
2484 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2485 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2488 if(new.state
& GLYPH_SET
) {
2493 sl
= utf8size(new.c
);
2494 memcpy(buf
+ib
, new.c
, sl
);
2500 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2506 expose(XEvent
*ev
) {
2507 XExposeEvent
*e
= &ev
->xexpose
;
2509 if(xw
.state
& WIN_REDRAW
) {
2511 xw
.state
&= ~WIN_REDRAW
;
2516 visibility(XEvent
*ev
) {
2517 XVisibilityEvent
*e
= &ev
->xvisibility
;
2519 if(e
->state
== VisibilityFullyObscured
) {
2520 xw
.state
&= ~WIN_VISIBLE
;
2521 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2522 /* need a full redraw for next Expose, not just a buf copy */
2523 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2529 xw
.state
&= ~WIN_VISIBLE
;
2533 xseturgency(int add
) {
2534 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2536 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2537 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2543 if(ev
->type
== FocusIn
) {
2544 xw
.state
|= WIN_FOCUSED
;
2547 xw
.state
&= ~WIN_FOCUSED
;
2552 kmap(KeySym k
, uint state
) {
2557 for(i
= 0; i
< LEN(key
); i
++) {
2560 if(key
[i
].k
== k
&& ((state
& mask
) == mask
2561 || (mask
== XK_NO_MOD
&& !state
))) {
2562 return (char*)key
[i
].s
;
2569 kpress(XEvent
*ev
) {
2570 XKeyEvent
*e
= &ev
->xkey
;
2579 if (IS_SET(MODE_KBDLOCK
))
2582 meta
= e
->state
& Mod1Mask
;
2583 shift
= e
->state
& ShiftMask
;
2584 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2586 /* 1. custom keys from config.h */
2587 if((customkey
= kmap(ksym
, e
->state
))) {
2588 ttywrite(customkey
, strlen(customkey
));
2589 /* 2. hardcoded (overrides X lookup) */
2596 /* XXX: shift up/down doesn't work */
2597 sprintf(buf
, "\033%c%c",
2598 IS_SET(MODE_APPKEYPAD
) ? 'O' : '[',
2599 (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2607 if(IS_SET(MODE_CRLF
)) {
2608 ttywrite("\r\n", 2);
2616 if(meta
&& len
== 1)
2617 ttywrite("\033", 1);
2626 cmessage(XEvent
*e
) {
2628 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2629 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2630 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2631 xw
.state
|= WIN_FOCUSED
;
2633 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2634 xw
.state
&= ~WIN_FOCUSED
;
2636 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
2637 /* Send SIGHUP to shell */
2647 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2650 xw
.w
= e
->xconfigure
.width
;
2651 xw
.h
= e
->xconfigure
.height
;
2652 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2653 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2654 if(col
== term
.col
&& row
== term
.row
)
2666 int xfd
= XConnectionNumber(xw
.dpy
), i
;
2667 struct timeval drawtimeout
, *tv
= NULL
;
2671 FD_SET(cmdfd
, &rfd
);
2673 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
2676 die("select failed: %s\n", SERRNO
);
2680 * Stop after a certain number of reads so the user does not
2681 * feel like the system is stuttering.
2683 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
2687 * Just wait a bit so it isn't disturbing the
2688 * user and the system is able to write something.
2690 drawtimeout
.tv_sec
= 0;
2691 drawtimeout
.tv_usec
= 5;
2698 while(XPending(xw
.dpy
)) {
2699 XNextEvent(xw
.dpy
, &ev
);
2700 if(XFilterEvent(&ev
, xw
.win
))
2702 if(handler
[ev
.type
])
2703 (handler
[ev
.type
])(&ev
);
2712 main(int argc
, char *argv
[]) {
2713 int i
, bitm
, xr
, yr
;
2716 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2719 for(i
= 1; i
< argc
; i
++) {
2720 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2723 opt_class
= argv
[i
];
2726 /* eat every remaining arguments */
2738 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2743 if(bitm
& WidthValue
)
2745 if(bitm
& HeightValue
)
2747 if(bitm
& XNegative
&& xw
.fx
== 0)
2749 if(bitm
& XNegative
&& xw
.fy
== 0)
2752 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2761 opt_title
= argv
[i
];
2768 opt_embed
= argv
[i
];
2774 setlocale(LC_CTYPE
, "");