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");
868 if (envshell
== NULL
)
875 signal(SIGCHLD
, SIG_DFL
);
876 signal(SIGHUP
, SIG_DFL
);
877 signal(SIGINT
, SIG_DFL
);
878 signal(SIGQUIT
, SIG_DFL
);
879 signal(SIGTERM
, SIG_DFL
);
880 signal(SIGALRM
, SIG_DFL
);
882 DEFAULT(envshell
, SHELL
);
883 putenv("TERM="TNAME
);
884 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
885 execvp(args
[0], args
);
893 if(waitpid(pid
, &stat
, 0) < 0)
894 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
896 if(WIFEXITED(stat
)) {
897 exit(WEXITSTATUS(stat
));
906 struct winsize w
= {term
.row
, term
.col
, 0, 0};
908 /* seems to work fine on linux, openbsd and freebsd */
909 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
910 die("openpty failed: %s\n", SERRNO
);
912 switch(pid
= fork()) {
914 die("fork failed\n");
917 setsid(); /* create a new process group */
918 dup2(s
, STDIN_FILENO
);
919 dup2(s
, STDOUT_FILENO
);
920 dup2(s
, STDERR_FILENO
);
921 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
922 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
930 signal(SIGCHLD
, sigchld
);
932 if(!strcmp(opt_io
, "-")) {
933 iofd
= STDOUT_FILENO
;
935 if((iofd
= open(opt_io
, O_WRONLY
| O_CREAT
, 0666)) < 0) {
936 fprintf(stderr
, "Error opening %s:%s\n",
937 opt_io
, strerror(errno
));
948 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
950 fprintf(stderr
, "\n");
955 static char buf
[BUFSIZ
];
956 static int buflen
= 0;
959 int charsize
; /* size of utf8 char in bytes */
963 /* append read bytes to unprocessed bytes */
964 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
965 die("Couldn't read from shell: %s\n", SERRNO
);
967 /* process every complete utf8 char */
970 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
971 charsize
= utf8decode(ptr
, &utf8c
);
972 utf8encode(&utf8c
, s
);
978 /* keep any uncomplete utf8 char for the next call */
979 memmove(buf
, ptr
, buflen
);
983 ttywrite(const char *s
, size_t n
) {
984 if(write(cmdfd
, s
, n
) == -1)
985 die("write error on tty: %s\n", SERRNO
);
996 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
997 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1001 tsetdirt(int top
, int bot
) {
1004 LIMIT(top
, 0, term
.row
-1);
1005 LIMIT(bot
, 0, term
.row
-1);
1007 for(i
= top
; i
<= bot
; i
++)
1013 tsetdirt(0, term
.row
-1);
1020 if(mode
== CURSOR_SAVE
) {
1022 } else if(mode
== CURSOR_LOAD
) {
1032 term
.c
= (TCursor
){{
1036 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1038 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1039 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
1042 term
.bot
= term
.row
- 1;
1043 term
.mode
= MODE_WRAP
;
1045 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1049 tnew(int col
, int row
) {
1050 /* set screen size */
1053 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1054 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1055 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1056 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1058 for(row
= 0; row
< term
.row
; row
++) {
1059 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1060 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1061 term
.dirty
[row
] = 0;
1063 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1070 Line
*tmp
= term
.line
;
1072 term
.line
= term
.alt
;
1074 term
.mode
^= MODE_ALTSCREEN
;
1079 tscrolldown(int orig
, int n
) {
1083 LIMIT(n
, 0, term
.bot
-orig
+1);
1085 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1087 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1088 temp
= term
.line
[i
];
1089 term
.line
[i
] = term
.line
[i
-n
];
1090 term
.line
[i
-n
] = temp
;
1093 term
.dirty
[i
-n
] = 1;
1100 tscrollup(int orig
, int n
) {
1103 LIMIT(n
, 0, term
.bot
-orig
+1);
1105 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1107 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1108 temp
= term
.line
[i
];
1109 term
.line
[i
] = term
.line
[i
+n
];
1110 term
.line
[i
+n
] = temp
;
1113 term
.dirty
[i
+n
] = 1;
1116 selscroll(orig
, -n
);
1120 selscroll(int orig
, int n
) {
1124 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1125 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1129 if(sel
.by
< term
.top
) {
1133 if(sel
.ey
> term
.bot
) {
1137 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1138 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1143 tnewline(int first_col
) {
1147 tscrollup(term
.top
, 1);
1151 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1156 /* int noarg = 1; */
1157 char *p
= csiescseq
.buf
;
1161 csiescseq
.priv
= 1, p
++;
1163 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1164 while(isdigit(*p
)) {
1165 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1166 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1168 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1169 csiescseq
.narg
++, p
++;
1171 csiescseq
.mode
= *p
;
1180 tmoveto(int x
, int y
) {
1181 LIMIT(x
, 0, term
.col
-1);
1182 LIMIT(y
, 0, term
.row
-1);
1183 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1189 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1190 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1191 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1192 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1193 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1194 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1195 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1196 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1197 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1198 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1202 * The table is proudly stolen from rxvt.
1204 if(attr
->mode
& ATTR_GFX
) {
1205 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1206 && vt100_0
[c
[0] - 0x41]) {
1207 c
= vt100_0
[c
[0] - 0x41];
1212 term
.line
[y
][x
] = *attr
;
1213 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1214 term
.line
[y
][x
].state
|= GLYPH_SET
;
1218 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1222 temp
= x1
, x1
= x2
, x2
= temp
;
1224 temp
= y1
, y1
= y2
, y2
= temp
;
1226 LIMIT(x1
, 0, term
.col
-1);
1227 LIMIT(x2
, 0, term
.col
-1);
1228 LIMIT(y1
, 0, term
.row
-1);
1229 LIMIT(y2
, 0, term
.row
-1);
1231 for(y
= y1
; y
<= y2
; y
++) {
1233 for(x
= x1
; x
<= x2
; x
++)
1234 term
.line
[y
][x
].state
= 0;
1239 tdeletechar(int n
) {
1240 int src
= term
.c
.x
+ n
;
1242 int size
= term
.col
- src
;
1244 term
.dirty
[term
.c
.y
] = 1;
1246 if(src
>= term
.col
) {
1247 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1251 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1252 size
* sizeof(Glyph
));
1253 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1257 tinsertblank(int n
) {
1260 int size
= term
.col
- dst
;
1262 term
.dirty
[term
.c
.y
] = 1;
1264 if(dst
>= term
.col
) {
1265 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1269 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1270 size
* sizeof(Glyph
));
1271 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1275 tinsertblankline(int n
) {
1276 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1279 tscrolldown(term
.c
.y
, n
);
1283 tdeleteline(int n
) {
1284 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1287 tscrollup(term
.c
.y
, n
);
1291 tsetattr(int *attr
, int l
) {
1294 for(i
= 0; i
< l
; i
++) {
1297 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1298 | ATTR_ITALIC
| ATTR_BLINK
);
1299 term
.c
.attr
.fg
= DefaultFG
;
1300 term
.c
.attr
.bg
= DefaultBG
;
1303 term
.c
.attr
.mode
|= ATTR_BOLD
;
1305 case 3: /* enter standout (highlight) */
1306 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1309 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1312 term
.c
.attr
.mode
|= ATTR_BLINK
;
1315 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1319 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1321 case 23: /* leave standout (highlight) mode */
1322 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1325 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1328 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1331 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1334 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1336 if(BETWEEN(attr
[i
], 0, 255)) {
1337 term
.c
.attr
.fg
= attr
[i
];
1340 "erresc: bad fgcolor %d\n",
1345 "erresc(38): gfx attr %d unknown\n",
1350 term
.c
.attr
.fg
= DefaultFG
;
1353 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1355 if(BETWEEN(attr
[i
], 0, 255)) {
1356 term
.c
.attr
.bg
= attr
[i
];
1359 "erresc: bad bgcolor %d\n",
1364 "erresc(48): gfx attr %d unknown\n",
1369 term
.c
.attr
.bg
= DefaultBG
;
1372 if(BETWEEN(attr
[i
], 30, 37)) {
1373 term
.c
.attr
.fg
= attr
[i
] - 30;
1374 } else if(BETWEEN(attr
[i
], 40, 47)) {
1375 term
.c
.attr
.bg
= attr
[i
] - 40;
1376 } else if(BETWEEN(attr
[i
], 90, 97)) {
1377 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1378 } else if(BETWEEN(attr
[i
], 100, 107)) {
1379 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1382 "erresc(default): gfx attr %d unknown\n",
1383 attr
[i
]), csidump();
1391 tsetscroll(int t
, int b
) {
1394 LIMIT(t
, 0, term
.row
-1);
1395 LIMIT(b
, 0, term
.row
-1);
1405 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1408 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1411 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1415 case 1: /* DECCKM -- Cursor key */
1416 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1418 case 5: /* DECSCNM -- Reverse video */
1420 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1421 if(mode
!= term
.mode
)
1424 case 6: /* XXX: DECOM -- Origin */
1426 case 7: /* DECAWM -- Auto wrap */
1427 MODBIT(term
.mode
, set
, MODE_WRAP
);
1429 case 8: /* XXX: DECARM -- Auto repeat */
1431 case 0: /* Error (IGNORED) */
1432 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1435 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1437 case 1000: /* 1000,1002: enable xterm mouse report */
1438 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1441 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1443 case 1049: /* = 1047 and 1048 */
1446 if(IS_SET(MODE_ALTSCREEN
))
1447 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1448 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1449 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1456 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1459 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1460 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1461 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1462 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1463 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1464 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1466 "erresc: unknown private set/reset mode %d\n",
1472 case 0: /* Error (IGNORED) */
1474 case 2: /* KAM -- keyboard action */
1475 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1477 case 4: /* IRM -- Insertion-replacement */
1478 MODBIT(term
.mode
, set
, MODE_INSERT
);
1480 case 12: /* XXX: SRM -- Send/Receive */
1482 case 20: /* LNM -- Linefeed/new line */
1483 MODBIT(term
.mode
, set
, MODE_CRLF
);
1487 "erresc: unknown set/reset mode %d\n",
1499 switch(csiescseq
.mode
) {
1502 fprintf(stderr
, "erresc: unknown csi ");
1506 case '@': /* ICH -- Insert <n> blank char */
1507 DEFAULT(csiescseq
.arg
[0], 1);
1508 tinsertblank(csiescseq
.arg
[0]);
1510 case 'A': /* CUU -- Cursor <n> Up */
1512 DEFAULT(csiescseq
.arg
[0], 1);
1513 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1515 case 'B': /* CUD -- Cursor <n> Down */
1516 DEFAULT(csiescseq
.arg
[0], 1);
1517 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1519 case 'c': /* DA -- Device Attributes */
1520 if(csiescseq
.arg
[0] == 0)
1521 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1523 case 'C': /* CUF -- Cursor <n> Forward */
1525 DEFAULT(csiescseq
.arg
[0], 1);
1526 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1528 case 'D': /* CUB -- Cursor <n> Backward */
1529 DEFAULT(csiescseq
.arg
[0], 1);
1530 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1532 case 'E': /* CNL -- Cursor <n> Down and first col */
1533 DEFAULT(csiescseq
.arg
[0], 1);
1534 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1536 case 'F': /* CPL -- Cursor <n> Up and first col */
1537 DEFAULT(csiescseq
.arg
[0], 1);
1538 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1540 case 'g': /* TBC -- Tabulation clear */
1541 switch (csiescseq
.arg
[0]) {
1542 case 0: /* clear current tab stop */
1543 term
.tabs
[term
.c
.x
] = 0;
1545 case 3: /* clear all the tabs */
1546 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1552 case 'G': /* CHA -- Move to <col> */
1554 DEFAULT(csiescseq
.arg
[0], 1);
1555 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1557 case 'H': /* CUP -- Move to <row> <col> */
1559 DEFAULT(csiescseq
.arg
[0], 1);
1560 DEFAULT(csiescseq
.arg
[1], 1);
1561 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1563 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1564 DEFAULT(csiescseq
.arg
[0], 1);
1565 while(csiescseq
.arg
[0]--)
1568 case 'J': /* ED -- Clear screen */
1570 switch(csiescseq
.arg
[0]) {
1572 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1573 if(term
.c
.y
< term
.row
-1)
1574 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1578 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1579 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1582 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1588 case 'K': /* EL -- Clear line */
1589 switch(csiescseq
.arg
[0]) {
1591 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1594 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1597 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1601 case 'S': /* SU -- Scroll <n> line up */
1602 DEFAULT(csiescseq
.arg
[0], 1);
1603 tscrollup(term
.top
, csiescseq
.arg
[0]);
1605 case 'T': /* SD -- Scroll <n> line down */
1606 DEFAULT(csiescseq
.arg
[0], 1);
1607 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1609 case 'L': /* IL -- Insert <n> blank lines */
1610 DEFAULT(csiescseq
.arg
[0], 1);
1611 tinsertblankline(csiescseq
.arg
[0]);
1613 case 'l': /* RM -- Reset Mode */
1614 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1616 case 'M': /* DL -- Delete <n> lines */
1617 DEFAULT(csiescseq
.arg
[0], 1);
1618 tdeleteline(csiescseq
.arg
[0]);
1620 case 'X': /* ECH -- Erase <n> char */
1621 DEFAULT(csiescseq
.arg
[0], 1);
1622 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1624 case 'P': /* DCH -- Delete <n> char */
1625 DEFAULT(csiescseq
.arg
[0], 1);
1626 tdeletechar(csiescseq
.arg
[0]);
1628 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1629 DEFAULT(csiescseq
.arg
[0], 1);
1630 while(csiescseq
.arg
[0]--)
1633 case 'd': /* VPA -- Move to <row> */
1634 DEFAULT(csiescseq
.arg
[0], 1);
1635 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1637 case 'h': /* SM -- Set terminal mode */
1638 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1640 case 'm': /* SGR -- Terminal attribute (color) */
1641 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1643 case 'r': /* DECSTBM -- Set Scrolling Region */
1644 if(csiescseq
.priv
) {
1647 DEFAULT(csiescseq
.arg
[0], 1);
1648 DEFAULT(csiescseq
.arg
[1], term
.row
);
1649 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1653 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1654 tcursor(CURSOR_SAVE
);
1656 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1657 tcursor(CURSOR_LOAD
);
1668 for(i
= 0; i
< csiescseq
.len
; i
++) {
1669 c
= csiescseq
.buf
[i
] & 0xff;
1672 } else if(c
== '\n') {
1674 } else if(c
== '\r') {
1676 } else if(c
== 0x1b) {
1679 printf("(%02x)", c
);
1687 memset(&csiescseq
, 0, sizeof(csiescseq
));
1695 * TODO: make this being useful in case of color palette change.
1701 switch(strescseq
.type
) {
1702 case ']': /* OSC -- Operating System Command */
1708 * TODO: Handle special chars in string, like umlauts.
1711 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1715 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1717 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1720 fprintf(stderr
, "erresc: unknown str ");
1725 case 'k': /* old title set compatibility */
1726 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1728 case 'P': /* DSC -- Device Control String */
1729 case '_': /* APC -- Application Program Command */
1730 case '^': /* PM -- Privacy Message */
1732 fprintf(stderr
, "erresc: unknown str ");
1742 * TODO: Implement parsing like for CSI when required.
1743 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1753 printf("ESC%c", strescseq
.type
);
1754 for(i
= 0; i
< strescseq
.len
; i
++) {
1755 c
= strescseq
.buf
[i
] & 0xff;
1758 } else if(c
== '\n') {
1760 } else if(c
== '\r') {
1762 } else if(c
== 0x1b) {
1765 printf("(%02x)", c
);
1773 memset(&strescseq
, 0, sizeof(strescseq
));
1777 tputtab(bool forward
) {
1783 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1788 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1791 tmoveto(x
, term
.c
.y
);
1795 tputc(char *c
, int len
) {
1797 bool control
= ascii
< '\x20' || ascii
== 0177;
1800 write(iofd
, c
, len
);
1802 * STR sequences must be checked before of anything
1803 * because it can use some control codes as part of the sequence
1805 if(term
.esc
& ESC_STR
) {
1808 term
.esc
= ESC_START
| ESC_STR_END
;
1810 case '\a': /* backwards compatibility to xterm */
1815 strescseq
.buf
[strescseq
.len
++] = ascii
;
1816 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1824 * Actions of control codes must be performed as soon they arrive
1825 * because they can be embedded inside a control sequence, and
1826 * they must not cause conflicts with sequences.
1834 tmoveto(term
.c
.x
-1, term
.c
.y
);
1837 tmoveto(0, term
.c
.y
);
1842 /* go to first col if the mode is set */
1843 tnewline(IS_SET(MODE_CRLF
));
1845 case '\a': /* BEL */
1846 if(!(xw
.state
& WIN_FOCUSED
))
1849 case '\033': /* ESC */
1851 term
.esc
= ESC_START
;
1853 case '\016': /* SO */
1854 term
.c
.attr
.mode
|= ATTR_GFX
;
1856 case '\017': /* SI */
1857 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1859 case '\032': /* SUB */
1860 case '\030': /* CAN */
1863 case '\005': /* ENQ (IGNORED) */
1864 case '\000': /* NUL (IGNORED) */
1865 case '\021': /* XON (IGNORED) */
1866 case '\023': /* XOFF (IGNORED) */
1867 case 0177: /* DEL (IGNORED) */
1870 } else if(term
.esc
& ESC_START
) {
1871 if(term
.esc
& ESC_CSI
) {
1872 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1873 if(BETWEEN(ascii
, 0x40, 0x7E)
1874 || csiescseq
.len
>= ESC_BUF_SIZ
) {
1876 csiparse(), csihandle();
1878 } else if(term
.esc
& ESC_STR_END
) {
1882 } else if(term
.esc
& ESC_ALTCHARSET
) {
1884 case '0': /* Line drawing set */
1885 term
.c
.attr
.mode
|= ATTR_GFX
;
1887 case 'B': /* USASCII */
1888 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1890 case 'A': /* UK (IGNORED) */
1891 case '<': /* multinational charset (IGNORED) */
1892 case '5': /* Finnish (IGNORED) */
1893 case 'C': /* Finnish (IGNORED) */
1894 case 'K': /* German (IGNORED) */
1897 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1900 } else if(term
.esc
& ESC_TEST
) {
1901 if(ascii
== '8') { /* DEC screen alignment test. */
1902 char E
[UTF_SIZ
] = "E";
1905 for(x
= 0; x
< term
.col
; ++x
) {
1906 for(y
= 0; y
< term
.row
; ++y
)
1907 tsetchar(E
, &term
.c
.attr
, x
, y
);
1914 term
.esc
|= ESC_CSI
;
1917 term
.esc
|= ESC_TEST
;
1919 case 'P': /* DCS -- Device Control String */
1920 case '_': /* APC -- Application Program Command */
1921 case '^': /* PM -- Privacy Message */
1922 case ']': /* OSC -- Operating System Command */
1923 case 'k': /* old title set compatibility */
1925 strescseq
.type
= ascii
;
1926 term
.esc
|= ESC_STR
;
1928 case '(': /* set primary charset G0 */
1929 term
.esc
|= ESC_ALTCHARSET
;
1931 case ')': /* set secondary charset G1 (IGNORED) */
1932 case '*': /* set tertiary charset G2 (IGNORED) */
1933 case '+': /* set quaternary charset G3 (IGNORED) */
1936 case 'D': /* IND -- Linefeed */
1937 if(term
.c
.y
== term
.bot
) {
1938 tscrollup(term
.top
, 1);
1940 tmoveto(term
.c
.x
, term
.c
.y
+1);
1944 case 'E': /* NEL -- Next line */
1945 tnewline(1); /* always go to first col */
1948 case 'H': /* HTS -- Horizontal tab stop */
1949 term
.tabs
[term
.c
.x
] = 1;
1952 case 'M': /* RI -- Reverse index */
1953 if(term
.c
.y
== term
.top
) {
1954 tscrolldown(term
.top
, 1);
1956 tmoveto(term
.c
.x
, term
.c
.y
-1);
1960 case 'Z': /* DECID -- Identify Terminal */
1961 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1964 case 'c': /* RIS -- Reset to inital state */
1969 case '=': /* DECPAM -- Application keypad */
1970 term
.mode
|= MODE_APPKEYPAD
;
1973 case '>': /* DECPNM -- Normal keypad */
1974 term
.mode
&= ~MODE_APPKEYPAD
;
1977 case '7': /* DECSC -- Save Cursor */
1978 tcursor(CURSOR_SAVE
);
1981 case '8': /* DECRC -- Restore Cursor */
1982 tcursor(CURSOR_LOAD
);
1985 case '\\': /* ST -- Stop */
1989 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1990 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
1995 * All characters which forms part of a sequence are not
2001 * Display control codes only if we are in graphic mode
2003 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2005 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2007 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2008 tnewline(1); /* always go to first col */
2009 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2010 if(term
.c
.x
+1 < term
.col
)
2011 tmoveto(term
.c
.x
+1, term
.c
.y
);
2013 term
.c
.state
|= CURSOR_WRAPNEXT
;
2017 tresize(int col
, int row
) {
2019 int minrow
= MIN(row
, term
.row
);
2020 int mincol
= MIN(col
, term
.col
);
2021 int slide
= term
.c
.y
- row
+ 1;
2024 if(col
< 1 || row
< 1)
2027 /* free unneeded rows */
2030 /* slide screen to keep cursor where we expect it -
2031 * tscrollup would work here, but we can optimize to
2032 * memmove because we're freeing the earlier lines */
2033 for(/* i = 0 */; i
< slide
; i
++) {
2037 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2038 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2040 for(i
+= row
; i
< term
.row
; i
++) {
2045 /* resize to new height */
2046 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2047 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2048 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2049 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2051 /* resize each row to new width, zero-pad if needed */
2052 for(i
= 0; i
< minrow
; i
++) {
2054 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2055 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2056 for(x
= mincol
; x
< col
; x
++) {
2057 term
.line
[i
][x
].state
= 0;
2058 term
.alt
[i
][x
].state
= 0;
2062 /* allocate any new rows */
2063 for(/* i == minrow */; i
< row
; i
++) {
2065 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2066 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2068 if(col
> term
.col
) {
2069 bp
= term
.tabs
+ term
.col
;
2071 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2072 while(--bp
> term
.tabs
&& !*bp
)
2074 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
2077 /* update terminal size */
2078 term
.col
= col
, term
.row
= row
;
2079 /* make use of the LIMIT in tmoveto */
2080 tmoveto(term
.c
.x
, term
.c
.y
);
2081 /* reset scrolling region */
2082 tsetscroll(0, row
-1);
2088 xresize(int col
, int row
) {
2089 xw
.tw
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
2090 xw
.th
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
2092 XftDrawChange(xw
.xft_draw
, xw
.buf
);
2098 XRenderColor xft_color
= { .alpha
= 0 };
2100 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2101 for(i
= 0; i
< LEN(colorname
); i
++) {
2104 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.xft_col
[i
])) {
2105 die("Could not allocate color '%s'\n", colorname
[i
]);
2109 /* load colors [16-255] ; same colors as xterm */
2110 for(i
= 16, r
= 0; r
< 6; r
++) {
2111 for(g
= 0; g
< 6; g
++) {
2112 for(b
= 0; b
< 6; b
++) {
2113 xft_color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2114 xft_color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2115 xft_color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2116 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
2117 die("Could not allocate color %d\n", i
);
2124 for(r
= 0; r
< 24; r
++, i
++) {
2125 xft_color
.red
= xft_color
.green
= xft_color
.blue
= 0x0808 + 0x0a0a * r
;
2126 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
,
2128 die("Could not allocate color %d\n", i
);
2134 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2135 XftDrawRect(xw
.xft_draw
,
2136 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2137 BORDER
+ col1
* xw
.cw
,
2138 BORDER
+ row1
* xw
.ch
,
2139 (col2
-col1
+1) * xw
.cw
,
2140 (row2
-row1
+1) * xw
.ch
);
2144 * Absolute coordinates.
2147 xclear(int x1
, int y1
, int x2
, int y2
) {
2148 XftDrawRect(xw
.xft_draw
,
2149 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2150 x1
, y1
, x2
-x1
, y2
-y1
);
2155 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
2156 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2157 XSizeHints
*sizeh
= NULL
;
2159 sizeh
= XAllocSizeHints();
2160 if(xw
.isfixed
== False
) {
2161 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2162 sizeh
->height
= xw
.h
;
2163 sizeh
->width
= xw
.w
;
2164 sizeh
->height_inc
= xw
.ch
;
2165 sizeh
->width_inc
= xw
.cw
;
2166 sizeh
->base_height
= 2*BORDER
;
2167 sizeh
->base_width
= 2*BORDER
;
2169 sizeh
->flags
= PMaxSize
| PMinSize
;
2170 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2171 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2174 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2179 xinitfont(Font
*f
, char *fontstr
) {
2180 FcPattern
*pattern
, *match
;
2183 pattern
= FcNameParse((FcChar8
*)fontstr
);
2185 die("st: can't open font %s\n", fontstr
);
2187 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
2188 FcPatternDestroy(pattern
);
2190 die("st: can't open font %s\n", fontstr
);
2191 if(!(f
->xft_set
= XftFontOpenPattern(xw
.dpy
, match
))) {
2192 FcPatternDestroy(match
);
2193 die("st: can't open font %s.\n", fontstr
);
2196 f
->ascent
= f
->xft_set
->ascent
;
2197 f
->descent
= f
->xft_set
->descent
;
2199 f
->rbearing
= f
->xft_set
->max_advance_width
;
2201 f
->height
= f
->xft_set
->height
;
2202 f
->width
= f
->lbearing
+ f
->rbearing
;
2206 initfonts(char *fontstr
) {
2209 xinitfont(&dc
.font
, fontstr
);
2210 xw
.cw
= dc
.font
.width
;
2211 xw
.ch
= dc
.font
.height
;
2213 fstr
= smstrcat(fontstr
, ":weight=bold", NULL
);
2214 xinitfont(&dc
.bfont
, fstr
);
2217 fstr
= smstrcat(fontstr
, ":slant=italic,oblique", NULL
);
2218 xinitfont(&dc
.ifont
, fstr
);
2221 fstr
= smstrcat(fontstr
, ":weight=bold:slant=italic,oblique", NULL
);
2222 xinitfont(&dc
.ibfont
, fstr
);
2228 XSetWindowAttributes attrs
;
2231 int sw
, sh
, major
, minor
;
2233 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2234 die("Can't open display\n");
2235 xw
.scr
= XDefaultScreen(xw
.dpy
);
2236 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2239 initfonts((opt_font
!= NULL
)? opt_font
: FONT
);
2242 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2245 /* adjust fixed window geometry */
2247 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2248 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2250 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2252 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2257 /* window - default size */
2258 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
2259 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
2264 attrs
.background_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2265 attrs
.border_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2266 attrs
.bit_gravity
= NorthWestGravity
;
2267 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2268 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2269 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2270 attrs
.colormap
= xw
.cmap
;
2272 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2273 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2274 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2276 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2280 /* double buffering */
2281 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2282 die("Xdbe extension is not present\n");
2283 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2285 /* Xft rendering context */
2286 xw
.xft_draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2289 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2290 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2291 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2292 XNFocusWindow
, xw
.win
, NULL
);
2294 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2296 /* white cursor, black outline */
2297 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2298 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2299 XRecolorCursor(xw
.dpy
, cursor
,
2300 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2301 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2303 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2304 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2305 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2308 XMapWindow(xw
.dpy
, xw
.win
);
2314 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2315 int winx
= BORDER
+ x
* xw
.cw
, winy
= BORDER
+ y
* xw
.ch
,
2316 width
= charlen
* xw
.cw
;
2317 Font
*font
= &dc
.font
;
2319 XftColor
*fg
= &dc
.xft_col
[base
.fg
], *bg
= &dc
.xft_col
[base
.bg
],
2320 *temp
, revfg
, revbg
;
2321 XRenderColor colfg
, colbg
;
2323 if(base
.mode
& ATTR_REVERSE
)
2324 temp
= fg
, fg
= bg
, bg
= temp
;
2326 if(base
.mode
& ATTR_BOLD
) {
2327 if(BETWEEN(base
.fg
, 0, 7)) {
2328 /* basic system colors */
2329 fg
= &dc
.xft_col
[base
.fg
+ 8];
2330 } else if(BETWEEN(base
.fg
, 16, 195)) {
2332 fg
= &dc
.xft_col
[base
.fg
+ 36];
2333 } else if(BETWEEN(base
.fg
, 232, 251)) {
2335 fg
= &dc
.xft_col
[base
.fg
+ 4];
2338 * Those ranges will not be brightened:
2339 * 8 - 15 – bright system colors
2340 * 196 - 231 – highest 256 color cube
2341 * 252 - 255 – brightest colors in greyscale
2346 if(base
.mode
& ATTR_ITALIC
)
2348 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2351 if(IS_SET(MODE_REVERSE
)) {
2352 if(fg
== &dc
.xft_col
[DefaultFG
]) {
2353 fg
= &dc
.xft_col
[DefaultBG
];
2355 colfg
.red
= ~fg
->color
.red
;
2356 colfg
.green
= ~fg
->color
.green
;
2357 colfg
.blue
= ~fg
->color
.blue
;
2358 colfg
.alpha
= fg
->color
.alpha
;
2359 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2363 if(bg
== &dc
.xft_col
[DefaultBG
]) {
2364 bg
= &dc
.xft_col
[DefaultFG
];
2366 colbg
.red
= ~bg
->color
.red
;
2367 colbg
.green
= ~bg
->color
.green
;
2368 colbg
.blue
= ~bg
->color
.blue
;
2369 colbg
.alpha
= bg
->color
.alpha
;
2370 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2375 XftTextExtentsUtf8(xw
.dpy
, font
->xft_set
, (FcChar8
*)s
, bytelen
,
2377 width
= extents
.xOff
;
2379 /* Intelligent cleaning up of the borders. */
2381 xclear(0, (y
== 0)? 0 : winy
, BORDER
,
2382 winy
+ xw
.ch
+ (y
== term
.row
-1)? xw
.h
: 0);
2384 if(x
+ charlen
>= term
.col
-1) {
2385 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2386 (y
== term
.row
-1)? xw
.h
: (winy
+ xw
.ch
));
2389 xclear(winx
, 0, winx
+ width
, BORDER
);
2391 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2393 XftDrawRect(xw
.xft_draw
, bg
, winx
, winy
, width
, xw
.ch
);
2394 XftDrawStringUtf8(xw
.xft_draw
, fg
, font
->xft_set
, winx
,
2395 winy
+ font
->ascent
, (FcChar8
*)s
, bytelen
);
2397 if(base
.mode
& ATTR_UNDERLINE
) {
2398 XftDrawRect(xw
.xft_draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2405 static int oldx
= 0, oldy
= 0;
2407 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2409 LIMIT(oldx
, 0, term
.col
-1);
2410 LIMIT(oldy
, 0, term
.row
-1);
2412 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2413 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2415 /* remove the old cursor */
2416 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2417 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2418 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2421 xtermclear(oldx
, oldy
, oldx
, oldy
);
2424 /* draw the new one */
2425 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2426 if(!(xw
.state
& WIN_FOCUSED
))
2429 if(IS_SET(MODE_REVERSE
))
2430 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2433 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2434 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2440 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2445 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2449 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2450 nanosleep(&tv
, NULL
);
2455 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2457 drawregion(0, 0, term
.col
, term
.row
);
2458 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2462 drawregion(int x1
, int y1
, int x2
, int y2
) {
2463 int ic
, ib
, x
, y
, ox
, sl
;
2465 char buf
[DRAW_BUF_SIZ
];
2466 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2468 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2470 if(!(xw
.state
& WIN_VISIBLE
))
2473 for(y
= y1
; y
< y2
; y
++) {
2477 xtermclear(0, y
, term
.col
, y
);
2479 base
= term
.line
[y
][0];
2481 for(x
= x1
; x
< x2
; x
++) {
2482 new = term
.line
[y
][x
];
2483 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2484 new.mode
^= ATTR_REVERSE
;
2485 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2486 || ATTRCMP(base
, new)
2487 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2488 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2491 if(new.state
& GLYPH_SET
) {
2496 sl
= utf8size(new.c
);
2497 memcpy(buf
+ib
, new.c
, sl
);
2503 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2509 expose(XEvent
*ev
) {
2510 XExposeEvent
*e
= &ev
->xexpose
;
2512 if(xw
.state
& WIN_REDRAW
) {
2514 xw
.state
&= ~WIN_REDRAW
;
2519 visibility(XEvent
*ev
) {
2520 XVisibilityEvent
*e
= &ev
->xvisibility
;
2522 if(e
->state
== VisibilityFullyObscured
) {
2523 xw
.state
&= ~WIN_VISIBLE
;
2524 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2525 /* need a full redraw for next Expose, not just a buf copy */
2526 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2532 xw
.state
&= ~WIN_VISIBLE
;
2536 xseturgency(int add
) {
2537 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2539 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2540 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2546 if(ev
->type
== FocusIn
) {
2547 xw
.state
|= WIN_FOCUSED
;
2550 xw
.state
&= ~WIN_FOCUSED
;
2555 kmap(KeySym k
, uint state
) {
2560 for(i
= 0; i
< LEN(key
); i
++) {
2563 if(key
[i
].k
== k
&& ((state
& mask
) == mask
2564 || (mask
== XK_NO_MOD
&& !state
))) {
2565 return (char*)key
[i
].s
;
2572 kpress(XEvent
*ev
) {
2573 XKeyEvent
*e
= &ev
->xkey
;
2582 if (IS_SET(MODE_KBDLOCK
))
2585 meta
= e
->state
& Mod1Mask
;
2586 shift
= e
->state
& ShiftMask
;
2587 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2589 /* 1. custom keys from config.h */
2590 if((customkey
= kmap(ksym
, e
->state
))) {
2591 ttywrite(customkey
, strlen(customkey
));
2592 /* 2. hardcoded (overrides X lookup) */
2599 /* XXX: shift up/down doesn't work */
2600 sprintf(buf
, "\033%c%c",
2601 IS_SET(MODE_APPKEYPAD
) ? 'O' : '[',
2602 (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2610 if(IS_SET(MODE_CRLF
)) {
2611 ttywrite("\r\n", 2);
2619 if(meta
&& len
== 1)
2620 ttywrite("\033", 1);
2629 cmessage(XEvent
*e
) {
2631 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2632 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2633 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2634 xw
.state
|= WIN_FOCUSED
;
2636 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2637 xw
.state
&= ~WIN_FOCUSED
;
2639 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
2640 /* Send SIGHUP to shell */
2650 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2653 xw
.w
= e
->xconfigure
.width
;
2654 xw
.h
= e
->xconfigure
.height
;
2655 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2656 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2657 if(col
== term
.col
&& row
== term
.row
)
2669 int xfd
= XConnectionNumber(xw
.dpy
), i
;
2670 struct timeval drawtimeout
, *tv
= NULL
;
2674 FD_SET(cmdfd
, &rfd
);
2676 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
2679 die("select failed: %s\n", SERRNO
);
2683 * Stop after a certain number of reads so the user does not
2684 * feel like the system is stuttering.
2686 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
2690 * Just wait a bit so it isn't disturbing the
2691 * user and the system is able to write something.
2693 drawtimeout
.tv_sec
= 0;
2694 drawtimeout
.tv_usec
= 5;
2701 while(XPending(xw
.dpy
)) {
2702 XNextEvent(xw
.dpy
, &ev
);
2703 if(XFilterEvent(&ev
, xw
.win
))
2705 if(handler
[ev
.type
])
2706 (handler
[ev
.type
])(&ev
);
2715 main(int argc
, char *argv
[]) {
2716 int i
, bitm
, xr
, yr
;
2719 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2722 for(i
= 1; i
< argc
; i
++) {
2723 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2726 opt_class
= argv
[i
];
2729 /* eat every remaining arguments */
2741 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2746 if(bitm
& WidthValue
)
2748 if(bitm
& HeightValue
)
2750 if(bitm
& XNegative
&& xw
.fx
== 0)
2752 if(bitm
& XNegative
&& xw
.fy
== 0)
2755 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2764 opt_title
= argv
[i
];
2771 opt_embed
= argv
[i
];
2777 setlocale(LC_CTYPE
, "");