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 ssize_t
xwrite(int, char *, size_t);
344 static void *xmalloc(size_t);
345 static void *xrealloc(void *, size_t);
346 static void *xcalloc(size_t nmemb
, size_t size
);
347 static char *smstrcat(char *, ...);
349 static void (*handler
[LASTEvent
])(XEvent
*) = {
351 [ClientMessage
] = cmessage
,
352 [ConfigureNotify
] = resize
,
353 [VisibilityNotify
] = visibility
,
354 [UnmapNotify
] = unmap
,
358 [MotionNotify
] = bmotion
,
359 [ButtonPress
] = bpress
,
360 [ButtonRelease
] = brelease
,
361 [SelectionClear
] = selclear
,
362 [SelectionNotify
] = selnotify
,
363 [SelectionRequest
] = selrequest
,
370 static CSIEscape csiescseq
;
371 static STREscape strescseq
;
374 static Selection sel
;
375 static int iofd
= -1;
376 static char **opt_cmd
= NULL
;
377 static char *opt_io
= NULL
;
378 static char *opt_title
= NULL
;
379 static char *opt_embed
= NULL
;
380 static char *opt_class
= NULL
;
381 static char *opt_font
= NULL
;
385 xwrite(int fd
, char *s
, size_t len
) {
389 ssize_t r
= write(fd
, s
, len
);
399 xmalloc(size_t len
) {
400 void *p
= malloc(len
);
403 die("Out of memory\n");
409 xrealloc(void *p
, size_t len
) {
410 if((p
= realloc(p
, len
)) == NULL
)
411 die("Out of memory\n");
417 xcalloc(size_t nmemb
, size_t size
) {
418 void *p
= calloc(nmemb
, size
);
421 die("Out of memory\n");
427 smstrcat(char *src
, ...)
433 len
= slen
= strlen(src
);
435 va_start(fmtargs
, src
);
437 v
= va_arg(fmtargs
, char *);
444 p
= ret
= xmalloc(len
+1);
445 memmove(p
, src
, slen
);
448 va_start(fmtargs
, src
);
450 v
= va_arg(fmtargs
, char *);
465 utf8decode(char *s
, long *u
) {
471 if(~c
& B7
) { /* 0xxxxxxx */
474 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
475 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
477 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
478 *u
= c
&(B3
|B2
|B1
|B0
);
480 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
487 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
489 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
492 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
495 if((n
== 1 && *u
< 0x80) ||
496 (n
== 2 && *u
< 0x800) ||
497 (n
== 3 && *u
< 0x10000) ||
498 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
510 utf8encode(long *u
, char *s
) {
518 *sp
= uc
; /* 0xxxxxxx */
520 } else if(*u
< 0x800) {
521 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
523 } else if(uc
< 0x10000) {
524 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
526 } else if(uc
<= 0x10FFFF) {
527 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
533 for(i
=n
,++sp
; i
>0; --i
,++sp
)
534 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
546 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
547 UTF-8 otherwise return 0 */
549 isfullutf8(char *s
, int b
) {
557 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
559 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
561 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
563 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
565 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
566 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
579 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
581 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
590 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
591 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
595 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
596 if(sel
.xtarget
== None
)
597 sel
.xtarget
= XA_STRING
;
601 selected(int x
, int y
) {
604 if(sel
.ey
== y
&& sel
.by
== y
) {
605 bx
= MIN(sel
.bx
, sel
.ex
);
606 ex
= MAX(sel
.bx
, sel
.ex
);
607 return BETWEEN(x
, bx
, ex
);
610 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
611 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
612 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
613 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
617 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
619 *b
= e
->xbutton
.button
;
621 *x
= X2COL(e
->xbutton
.x
);
622 *y
= Y2ROW(e
->xbutton
.y
);
623 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
624 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
625 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
626 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
630 mousereport(XEvent
*e
) {
631 int x
= X2COL(e
->xbutton
.x
);
632 int y
= Y2ROW(e
->xbutton
.y
);
633 int button
= e
->xbutton
.button
;
634 int state
= e
->xbutton
.state
;
635 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
636 static int ob
, ox
, oy
;
639 if(e
->xbutton
.type
== MotionNotify
) {
640 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
644 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
650 if(e
->xbutton
.type
== ButtonPress
) {
656 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
657 + (state
& Mod4Mask
? 8 : 0)
658 + (state
& ControlMask
? 16 : 0);
660 ttywrite(buf
, sizeof(buf
));
665 if(IS_SET(MODE_MOUSE
)) {
667 } else if(e
->xbutton
.button
== Button1
) {
670 tsetdirt(sel
.b
.y
, sel
.e
.y
);
674 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
675 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
682 int x
, y
, bufsize
, is_selected
= 0, size
;
688 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
689 ptr
= str
= xmalloc(bufsize
);
691 /* append every set & selected glyph to the selection */
692 for(y
= 0; y
< term
.row
; y
++) {
693 for(x
= 0; x
< term
.col
; x
++) {
694 gp
= &term
.line
[y
][x
];
696 if(!(is_selected
= selected(x
, y
)))
698 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
700 memcpy(ptr
, p
, size
);
703 /* \n at the end of every selected line except for the last one */
704 if(is_selected
&& y
< sel
.e
.y
)
709 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
714 selnotify(XEvent
*e
) {
715 ulong nitems
, ofs
, rem
;
722 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
723 False
, AnyPropertyType
, &type
, &format
,
724 &nitems
, &rem
, &data
)) {
725 fprintf(stderr
, "Clipboard allocation failed\n");
728 ttywrite((const char *) data
, nitems
* format
/ 8);
730 /* number of 32-bit chunks returned */
731 ofs
+= nitems
* format
/ 32;
737 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
738 xw
.win
, CurrentTime
);
741 void selclear(XEvent
*e
) {
745 tsetdirt(sel
.b
.y
, sel
.e
.y
);
749 selrequest(XEvent
*e
) {
750 XSelectionRequestEvent
*xsre
;
752 Atom xa_targets
, string
;
754 xsre
= (XSelectionRequestEvent
*) e
;
755 xev
.type
= SelectionNotify
;
756 xev
.requestor
= xsre
->requestor
;
757 xev
.selection
= xsre
->selection
;
758 xev
.target
= xsre
->target
;
759 xev
.time
= xsre
->time
;
763 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
764 if(xsre
->target
== xa_targets
) {
765 /* respond with the supported type */
766 string
= sel
.xtarget
;
767 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
768 XA_ATOM
, 32, PropModeReplace
,
769 (uchar
*) &string
, 1);
770 xev
.property
= xsre
->property
;
771 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
772 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
773 xsre
->target
, 8, PropModeReplace
,
774 (uchar
*) sel
.clip
, strlen(sel
.clip
));
775 xev
.property
= xsre
->property
;
778 /* all done, send a notification to the listener */
779 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
780 fprintf(stderr
, "Error sending SelectionNotify event\n");
785 /* register the selection for both the clipboard and the primary */
791 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
793 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
794 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
798 brelease(XEvent
*e
) {
801 if(IS_SET(MODE_MOUSE
)) {
806 if(e
->xbutton
.button
== Button2
) {
808 } else if(e
->xbutton
.button
== Button1
) {
810 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
811 term
.dirty
[sel
.ey
] = 1;
812 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
814 gettimeofday(&now
, NULL
);
816 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
817 /* triple click on the line */
818 sel
.b
.x
= sel
.bx
= 0;
819 sel
.e
.x
= sel
.ex
= term
.col
;
820 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
822 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
823 /* double click to select word */
825 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
826 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') {
830 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
831 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') {
835 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
843 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
844 gettimeofday(&sel
.tclick1
, NULL
);
849 int starty
, endy
, oldey
, oldex
;
851 if(IS_SET(MODE_MOUSE
)) {
859 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
861 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
862 starty
= MIN(oldey
, sel
.ey
);
863 endy
= MAX(oldey
, sel
.ey
);
864 tsetdirt(starty
, endy
);
870 die(const char *errstr
, ...) {
873 va_start(ap
, errstr
);
874 vfprintf(stderr
, errstr
, ap
);
882 char *envshell
= getenv("SHELL");
888 signal(SIGCHLD
, SIG_DFL
);
889 signal(SIGHUP
, SIG_DFL
);
890 signal(SIGINT
, SIG_DFL
);
891 signal(SIGQUIT
, SIG_DFL
);
892 signal(SIGTERM
, SIG_DFL
);
893 signal(SIGALRM
, SIG_DFL
);
895 DEFAULT(envshell
, SHELL
);
896 putenv("TERM="TNAME
);
897 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
898 execvp(args
[0], args
);
906 if(waitpid(pid
, &stat
, 0) < 0)
907 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
909 if(WIFEXITED(stat
)) {
910 exit(WEXITSTATUS(stat
));
919 struct winsize w
= {term
.row
, term
.col
, 0, 0};
921 /* seems to work fine on linux, openbsd and freebsd */
922 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
923 die("openpty failed: %s\n", SERRNO
);
925 switch(pid
= fork()) {
927 die("fork failed\n");
930 setsid(); /* create a new process group */
931 dup2(s
, STDIN_FILENO
);
932 dup2(s
, STDOUT_FILENO
);
933 dup2(s
, STDERR_FILENO
);
934 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
935 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
943 signal(SIGCHLD
, sigchld
);
945 iofd
= (!strcmp(opt_io
, "-")) ?
947 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
949 fprintf(stderr
, "Error opening %s:%s\n",
950 opt_io
, strerror(errno
));
960 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
962 fprintf(stderr
, "\n");
967 static char buf
[BUFSIZ
];
968 static int buflen
= 0;
971 int charsize
; /* size of utf8 char in bytes */
975 /* append read bytes to unprocessed bytes */
976 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
977 die("Couldn't read from shell: %s\n", SERRNO
);
979 /* process every complete utf8 char */
982 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
983 charsize
= utf8decode(ptr
, &utf8c
);
984 utf8encode(&utf8c
, s
);
990 /* keep any uncomplete utf8 char for the next call */
991 memmove(buf
, ptr
, buflen
);
995 ttywrite(const char *s
, size_t n
) {
996 if(write(cmdfd
, s
, n
) == -1)
997 die("write error on tty: %s\n", SERRNO
);
1004 w
.ws_row
= term
.row
;
1005 w
.ws_col
= term
.col
;
1006 w
.ws_xpixel
= xw
.tw
;
1007 w
.ws_ypixel
= xw
.th
;
1008 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1009 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1013 tsetdirt(int top
, int bot
) {
1016 LIMIT(top
, 0, term
.row
-1);
1017 LIMIT(bot
, 0, term
.row
-1);
1019 for(i
= top
; i
<= bot
; i
++)
1025 tsetdirt(0, term
.row
-1);
1032 if(mode
== CURSOR_SAVE
) {
1034 } else if(mode
== CURSOR_LOAD
) {
1044 term
.c
= (TCursor
){{
1048 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1050 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1051 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
1054 term
.bot
= term
.row
- 1;
1055 term
.mode
= MODE_WRAP
;
1057 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1061 tnew(int col
, int row
) {
1062 /* set screen size */
1065 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1066 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1067 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1068 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1070 for(row
= 0; row
< term
.row
; row
++) {
1071 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1072 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1073 term
.dirty
[row
] = 0;
1075 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1082 Line
*tmp
= term
.line
;
1084 term
.line
= term
.alt
;
1086 term
.mode
^= MODE_ALTSCREEN
;
1091 tscrolldown(int orig
, int n
) {
1095 LIMIT(n
, 0, term
.bot
-orig
+1);
1097 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1099 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1100 temp
= term
.line
[i
];
1101 term
.line
[i
] = term
.line
[i
-n
];
1102 term
.line
[i
-n
] = temp
;
1105 term
.dirty
[i
-n
] = 1;
1112 tscrollup(int orig
, int n
) {
1115 LIMIT(n
, 0, term
.bot
-orig
+1);
1117 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1119 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1120 temp
= term
.line
[i
];
1121 term
.line
[i
] = term
.line
[i
+n
];
1122 term
.line
[i
+n
] = temp
;
1125 term
.dirty
[i
+n
] = 1;
1128 selscroll(orig
, -n
);
1132 selscroll(int orig
, int n
) {
1136 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1137 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1141 if(sel
.by
< term
.top
) {
1145 if(sel
.ey
> term
.bot
) {
1149 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1150 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1155 tnewline(int first_col
) {
1159 tscrollup(term
.top
, 1);
1163 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1168 /* int noarg = 1; */
1169 char *p
= csiescseq
.buf
;
1173 csiescseq
.priv
= 1, p
++;
1175 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1176 while(isdigit(*p
)) {
1177 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1178 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1180 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
) {
1181 csiescseq
.narg
++, p
++;
1183 csiescseq
.mode
= *p
;
1192 tmoveto(int x
, int y
) {
1193 LIMIT(x
, 0, term
.col
-1);
1194 LIMIT(y
, 0, term
.row
-1);
1195 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1201 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1202 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1203 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1204 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1205 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1206 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1207 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1208 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1209 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1210 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1214 * The table is proudly stolen from rxvt.
1216 if(attr
->mode
& ATTR_GFX
) {
1217 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1218 && vt100_0
[c
[0] - 0x41]) {
1219 c
= vt100_0
[c
[0] - 0x41];
1224 term
.line
[y
][x
] = *attr
;
1225 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1226 term
.line
[y
][x
].state
|= GLYPH_SET
;
1230 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1234 temp
= x1
, x1
= x2
, x2
= temp
;
1236 temp
= y1
, y1
= y2
, y2
= temp
;
1238 LIMIT(x1
, 0, term
.col
-1);
1239 LIMIT(x2
, 0, term
.col
-1);
1240 LIMIT(y1
, 0, term
.row
-1);
1241 LIMIT(y2
, 0, term
.row
-1);
1243 for(y
= y1
; y
<= y2
; y
++) {
1245 for(x
= x1
; x
<= x2
; x
++)
1246 term
.line
[y
][x
].state
= 0;
1251 tdeletechar(int n
) {
1252 int src
= term
.c
.x
+ n
;
1254 int size
= term
.col
- src
;
1256 term
.dirty
[term
.c
.y
] = 1;
1258 if(src
>= term
.col
) {
1259 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1263 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1264 size
* sizeof(Glyph
));
1265 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1269 tinsertblank(int n
) {
1272 int size
= term
.col
- dst
;
1274 term
.dirty
[term
.c
.y
] = 1;
1276 if(dst
>= term
.col
) {
1277 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1281 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1282 size
* sizeof(Glyph
));
1283 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1287 tinsertblankline(int n
) {
1288 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1291 tscrolldown(term
.c
.y
, n
);
1295 tdeleteline(int n
) {
1296 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1299 tscrollup(term
.c
.y
, n
);
1303 tsetattr(int *attr
, int l
) {
1306 for(i
= 0; i
< l
; i
++) {
1309 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1310 | ATTR_ITALIC
| ATTR_BLINK
);
1311 term
.c
.attr
.fg
= DefaultFG
;
1312 term
.c
.attr
.bg
= DefaultBG
;
1315 term
.c
.attr
.mode
|= ATTR_BOLD
;
1317 case 3: /* enter standout (highlight) */
1318 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1321 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1324 term
.c
.attr
.mode
|= ATTR_BLINK
;
1327 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1331 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1333 case 23: /* leave standout (highlight) mode */
1334 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1337 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1340 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1343 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1346 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1348 if(BETWEEN(attr
[i
], 0, 255)) {
1349 term
.c
.attr
.fg
= attr
[i
];
1352 "erresc: bad fgcolor %d\n",
1357 "erresc(38): gfx attr %d unknown\n",
1362 term
.c
.attr
.fg
= DefaultFG
;
1365 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1367 if(BETWEEN(attr
[i
], 0, 255)) {
1368 term
.c
.attr
.bg
= attr
[i
];
1371 "erresc: bad bgcolor %d\n",
1376 "erresc(48): gfx attr %d unknown\n",
1381 term
.c
.attr
.bg
= DefaultBG
;
1384 if(BETWEEN(attr
[i
], 30, 37)) {
1385 term
.c
.attr
.fg
= attr
[i
] - 30;
1386 } else if(BETWEEN(attr
[i
], 40, 47)) {
1387 term
.c
.attr
.bg
= attr
[i
] - 40;
1388 } else if(BETWEEN(attr
[i
], 90, 97)) {
1389 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1390 } else if(BETWEEN(attr
[i
], 100, 107)) {
1391 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1394 "erresc(default): gfx attr %d unknown\n",
1395 attr
[i
]), csidump();
1403 tsetscroll(int t
, int b
) {
1406 LIMIT(t
, 0, term
.row
-1);
1407 LIMIT(b
, 0, term
.row
-1);
1417 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1420 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1423 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1427 case 1: /* DECCKM -- Cursor key */
1428 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1430 case 5: /* DECSCNM -- Reverse video */
1432 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1433 if(mode
!= term
.mode
)
1436 case 6: /* XXX: DECOM -- Origin */
1438 case 7: /* DECAWM -- Auto wrap */
1439 MODBIT(term
.mode
, set
, MODE_WRAP
);
1441 case 8: /* XXX: DECARM -- Auto repeat */
1443 case 0: /* Error (IGNORED) */
1444 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1447 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1449 case 1000: /* 1000,1002: enable xterm mouse report */
1450 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1453 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1455 case 1049: /* = 1047 and 1048 */
1458 if(IS_SET(MODE_ALTSCREEN
))
1459 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1460 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1461 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1468 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1471 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1472 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1473 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1474 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1475 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1476 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1478 "erresc: unknown private set/reset mode %d\n",
1484 case 0: /* Error (IGNORED) */
1486 case 2: /* KAM -- keyboard action */
1487 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1489 case 4: /* IRM -- Insertion-replacement */
1490 MODBIT(term
.mode
, set
, MODE_INSERT
);
1492 case 12: /* XXX: SRM -- Send/Receive */
1494 case 20: /* LNM -- Linefeed/new line */
1495 MODBIT(term
.mode
, set
, MODE_CRLF
);
1499 "erresc: unknown set/reset mode %d\n",
1511 switch(csiescseq
.mode
) {
1514 fprintf(stderr
, "erresc: unknown csi ");
1518 case '@': /* ICH -- Insert <n> blank char */
1519 DEFAULT(csiescseq
.arg
[0], 1);
1520 tinsertblank(csiescseq
.arg
[0]);
1522 case 'A': /* CUU -- Cursor <n> Up */
1524 DEFAULT(csiescseq
.arg
[0], 1);
1525 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1527 case 'B': /* CUD -- Cursor <n> Down */
1528 DEFAULT(csiescseq
.arg
[0], 1);
1529 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1531 case 'c': /* DA -- Device Attributes */
1532 if(csiescseq
.arg
[0] == 0)
1533 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1535 case 'C': /* CUF -- Cursor <n> Forward */
1537 DEFAULT(csiescseq
.arg
[0], 1);
1538 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1540 case 'D': /* CUB -- Cursor <n> Backward */
1541 DEFAULT(csiescseq
.arg
[0], 1);
1542 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1544 case 'E': /* CNL -- Cursor <n> Down and first col */
1545 DEFAULT(csiescseq
.arg
[0], 1);
1546 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1548 case 'F': /* CPL -- Cursor <n> Up and first col */
1549 DEFAULT(csiescseq
.arg
[0], 1);
1550 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1552 case 'g': /* TBC -- Tabulation clear */
1553 switch (csiescseq
.arg
[0]) {
1554 case 0: /* clear current tab stop */
1555 term
.tabs
[term
.c
.x
] = 0;
1557 case 3: /* clear all the tabs */
1558 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1564 case 'G': /* CHA -- Move to <col> */
1566 DEFAULT(csiescseq
.arg
[0], 1);
1567 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1569 case 'H': /* CUP -- Move to <row> <col> */
1571 DEFAULT(csiescseq
.arg
[0], 1);
1572 DEFAULT(csiescseq
.arg
[1], 1);
1573 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1575 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1576 DEFAULT(csiescseq
.arg
[0], 1);
1577 while(csiescseq
.arg
[0]--)
1580 case 'J': /* ED -- Clear screen */
1582 switch(csiescseq
.arg
[0]) {
1584 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1585 if(term
.c
.y
< term
.row
-1)
1586 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1590 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1591 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1594 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1600 case 'K': /* EL -- Clear line */
1601 switch(csiescseq
.arg
[0]) {
1603 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1606 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1609 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1613 case 'S': /* SU -- Scroll <n> line up */
1614 DEFAULT(csiescseq
.arg
[0], 1);
1615 tscrollup(term
.top
, csiescseq
.arg
[0]);
1617 case 'T': /* SD -- Scroll <n> line down */
1618 DEFAULT(csiescseq
.arg
[0], 1);
1619 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1621 case 'L': /* IL -- Insert <n> blank lines */
1622 DEFAULT(csiescseq
.arg
[0], 1);
1623 tinsertblankline(csiescseq
.arg
[0]);
1625 case 'l': /* RM -- Reset Mode */
1626 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1628 case 'M': /* DL -- Delete <n> lines */
1629 DEFAULT(csiescseq
.arg
[0], 1);
1630 tdeleteline(csiescseq
.arg
[0]);
1632 case 'X': /* ECH -- Erase <n> char */
1633 DEFAULT(csiescseq
.arg
[0], 1);
1634 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1636 case 'P': /* DCH -- Delete <n> char */
1637 DEFAULT(csiescseq
.arg
[0], 1);
1638 tdeletechar(csiescseq
.arg
[0]);
1640 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1641 DEFAULT(csiescseq
.arg
[0], 1);
1642 while(csiescseq
.arg
[0]--)
1645 case 'd': /* VPA -- Move to <row> */
1646 DEFAULT(csiescseq
.arg
[0], 1);
1647 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1649 case 'h': /* SM -- Set terminal mode */
1650 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1652 case 'm': /* SGR -- Terminal attribute (color) */
1653 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1655 case 'r': /* DECSTBM -- Set Scrolling Region */
1656 if(csiescseq
.priv
) {
1659 DEFAULT(csiescseq
.arg
[0], 1);
1660 DEFAULT(csiescseq
.arg
[1], term
.row
);
1661 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1665 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1666 tcursor(CURSOR_SAVE
);
1668 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1669 tcursor(CURSOR_LOAD
);
1680 for(i
= 0; i
< csiescseq
.len
; i
++) {
1681 c
= csiescseq
.buf
[i
] & 0xff;
1684 } else if(c
== '\n') {
1686 } else if(c
== '\r') {
1688 } else if(c
== 0x1b) {
1691 printf("(%02x)", c
);
1699 memset(&csiescseq
, 0, sizeof(csiescseq
));
1707 * TODO: make this being useful in case of color palette change.
1713 switch(strescseq
.type
) {
1714 case ']': /* OSC -- Operating System Command */
1720 * TODO: Handle special chars in string, like umlauts.
1723 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1727 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1729 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1732 fprintf(stderr
, "erresc: unknown str ");
1737 case 'k': /* old title set compatibility */
1738 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1740 case 'P': /* DSC -- Device Control String */
1741 case '_': /* APC -- Application Program Command */
1742 case '^': /* PM -- Privacy Message */
1744 fprintf(stderr
, "erresc: unknown str ");
1754 * TODO: Implement parsing like for CSI when required.
1755 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1765 printf("ESC%c", strescseq
.type
);
1766 for(i
= 0; i
< strescseq
.len
; i
++) {
1767 c
= strescseq
.buf
[i
] & 0xff;
1770 } else if(c
== '\n') {
1772 } else if(c
== '\r') {
1774 } else if(c
== 0x1b) {
1777 printf("(%02x)", c
);
1785 memset(&strescseq
, 0, sizeof(strescseq
));
1789 tputtab(bool forward
) {
1795 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1800 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1803 tmoveto(x
, term
.c
.y
);
1807 tputc(char *c
, int len
) {
1809 bool control
= ascii
< '\x20' || ascii
== 0177;
1812 if (xwrite(iofd
, c
, len
) < 0) {
1813 fprintf(stderr
, "Error writting in %s:%s\n",
1814 opt_io
, strerror(errno
));
1820 * STR sequences must be checked before of anything
1821 * because it can use some control codes as part of the sequence
1823 if(term
.esc
& ESC_STR
) {
1826 term
.esc
= ESC_START
| ESC_STR_END
;
1828 case '\a': /* backwards compatibility to xterm */
1833 strescseq
.buf
[strescseq
.len
++] = ascii
;
1834 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1842 * Actions of control codes must be performed as soon they arrive
1843 * because they can be embedded inside a control sequence, and
1844 * they must not cause conflicts with sequences.
1852 tmoveto(term
.c
.x
-1, term
.c
.y
);
1855 tmoveto(0, term
.c
.y
);
1860 /* go to first col if the mode is set */
1861 tnewline(IS_SET(MODE_CRLF
));
1863 case '\a': /* BEL */
1864 if(!(xw
.state
& WIN_FOCUSED
))
1867 case '\033': /* ESC */
1869 term
.esc
= ESC_START
;
1871 case '\016': /* SO */
1872 term
.c
.attr
.mode
|= ATTR_GFX
;
1874 case '\017': /* SI */
1875 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1877 case '\032': /* SUB */
1878 case '\030': /* CAN */
1881 case '\005': /* ENQ (IGNORED) */
1882 case '\000': /* NUL (IGNORED) */
1883 case '\021': /* XON (IGNORED) */
1884 case '\023': /* XOFF (IGNORED) */
1885 case 0177: /* DEL (IGNORED) */
1888 } else if(term
.esc
& ESC_START
) {
1889 if(term
.esc
& ESC_CSI
) {
1890 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1891 if(BETWEEN(ascii
, 0x40, 0x7E)
1892 || csiescseq
.len
>= ESC_BUF_SIZ
) {
1894 csiparse(), csihandle();
1896 } else if(term
.esc
& ESC_STR_END
) {
1900 } else if(term
.esc
& ESC_ALTCHARSET
) {
1902 case '0': /* Line drawing set */
1903 term
.c
.attr
.mode
|= ATTR_GFX
;
1905 case 'B': /* USASCII */
1906 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1908 case 'A': /* UK (IGNORED) */
1909 case '<': /* multinational charset (IGNORED) */
1910 case '5': /* Finnish (IGNORED) */
1911 case 'C': /* Finnish (IGNORED) */
1912 case 'K': /* German (IGNORED) */
1915 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1918 } else if(term
.esc
& ESC_TEST
) {
1919 if(ascii
== '8') { /* DEC screen alignment test. */
1920 char E
[UTF_SIZ
] = "E";
1923 for(x
= 0; x
< term
.col
; ++x
) {
1924 for(y
= 0; y
< term
.row
; ++y
)
1925 tsetchar(E
, &term
.c
.attr
, x
, y
);
1932 term
.esc
|= ESC_CSI
;
1935 term
.esc
|= ESC_TEST
;
1937 case 'P': /* DCS -- Device Control String */
1938 case '_': /* APC -- Application Program Command */
1939 case '^': /* PM -- Privacy Message */
1940 case ']': /* OSC -- Operating System Command */
1941 case 'k': /* old title set compatibility */
1943 strescseq
.type
= ascii
;
1944 term
.esc
|= ESC_STR
;
1946 case '(': /* set primary charset G0 */
1947 term
.esc
|= ESC_ALTCHARSET
;
1949 case ')': /* set secondary charset G1 (IGNORED) */
1950 case '*': /* set tertiary charset G2 (IGNORED) */
1951 case '+': /* set quaternary charset G3 (IGNORED) */
1954 case 'D': /* IND -- Linefeed */
1955 if(term
.c
.y
== term
.bot
) {
1956 tscrollup(term
.top
, 1);
1958 tmoveto(term
.c
.x
, term
.c
.y
+1);
1962 case 'E': /* NEL -- Next line */
1963 tnewline(1); /* always go to first col */
1966 case 'H': /* HTS -- Horizontal tab stop */
1967 term
.tabs
[term
.c
.x
] = 1;
1970 case 'M': /* RI -- Reverse index */
1971 if(term
.c
.y
== term
.top
) {
1972 tscrolldown(term
.top
, 1);
1974 tmoveto(term
.c
.x
, term
.c
.y
-1);
1978 case 'Z': /* DECID -- Identify Terminal */
1979 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1982 case 'c': /* RIS -- Reset to inital state */
1987 case '=': /* DECPAM -- Application keypad */
1988 term
.mode
|= MODE_APPKEYPAD
;
1991 case '>': /* DECPNM -- Normal keypad */
1992 term
.mode
&= ~MODE_APPKEYPAD
;
1995 case '7': /* DECSC -- Save Cursor */
1996 tcursor(CURSOR_SAVE
);
1999 case '8': /* DECRC -- Restore Cursor */
2000 tcursor(CURSOR_LOAD
);
2003 case '\\': /* ST -- Stop */
2007 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2008 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2013 * All characters which forms part of a sequence are not
2019 * Display control codes only if we are in graphic mode
2021 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2023 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2025 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2026 tnewline(1); /* always go to first col */
2027 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2028 if(term
.c
.x
+1 < term
.col
)
2029 tmoveto(term
.c
.x
+1, term
.c
.y
);
2031 term
.c
.state
|= CURSOR_WRAPNEXT
;
2035 tresize(int col
, int row
) {
2037 int minrow
= MIN(row
, term
.row
);
2038 int mincol
= MIN(col
, term
.col
);
2039 int slide
= term
.c
.y
- row
+ 1;
2042 if(col
< 1 || row
< 1)
2045 /* free unneeded rows */
2048 /* slide screen to keep cursor where we expect it -
2049 * tscrollup would work here, but we can optimize to
2050 * memmove because we're freeing the earlier lines */
2051 for(/* i = 0 */; i
< slide
; i
++) {
2055 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2056 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2058 for(i
+= row
; i
< term
.row
; i
++) {
2063 /* resize to new height */
2064 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2065 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2066 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2067 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2069 /* resize each row to new width, zero-pad if needed */
2070 for(i
= 0; i
< minrow
; i
++) {
2072 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2073 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2074 for(x
= mincol
; x
< col
; x
++) {
2075 term
.line
[i
][x
].state
= 0;
2076 term
.alt
[i
][x
].state
= 0;
2080 /* allocate any new rows */
2081 for(/* i == minrow */; i
< row
; i
++) {
2083 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2084 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2086 if(col
> term
.col
) {
2087 bp
= term
.tabs
+ term
.col
;
2089 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2090 while(--bp
> term
.tabs
&& !*bp
)
2092 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
2095 /* update terminal size */
2096 term
.col
= col
, term
.row
= row
;
2097 /* make use of the LIMIT in tmoveto */
2098 tmoveto(term
.c
.x
, term
.c
.y
);
2099 /* reset scrolling region */
2100 tsetscroll(0, row
-1);
2106 xresize(int col
, int row
) {
2107 xw
.tw
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
2108 xw
.th
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
2110 XftDrawChange(xw
.xft_draw
, xw
.buf
);
2116 XRenderColor xft_color
= { .alpha
= 0 };
2118 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2119 for(i
= 0; i
< LEN(colorname
); i
++) {
2122 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.xft_col
[i
])) {
2123 die("Could not allocate color '%s'\n", colorname
[i
]);
2127 /* load colors [16-255] ; same colors as xterm */
2128 for(i
= 16, r
= 0; r
< 6; r
++) {
2129 for(g
= 0; g
< 6; g
++) {
2130 for(b
= 0; b
< 6; b
++) {
2131 xft_color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
2132 xft_color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
2133 xft_color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
2134 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
2135 die("Could not allocate color %d\n", i
);
2142 for(r
= 0; r
< 24; r
++, i
++) {
2143 xft_color
.red
= xft_color
.green
= xft_color
.blue
= 0x0808 + 0x0a0a * r
;
2144 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
,
2146 die("Could not allocate color %d\n", i
);
2152 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2153 XftDrawRect(xw
.xft_draw
,
2154 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2155 BORDER
+ col1
* xw
.cw
,
2156 BORDER
+ row1
* xw
.ch
,
2157 (col2
-col1
+1) * xw
.cw
,
2158 (row2
-row1
+1) * xw
.ch
);
2162 * Absolute coordinates.
2165 xclear(int x1
, int y1
, int x2
, int y2
) {
2166 XftDrawRect(xw
.xft_draw
,
2167 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2168 x1
, y1
, x2
-x1
, y2
-y1
);
2173 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
2174 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2175 XSizeHints
*sizeh
= NULL
;
2177 sizeh
= XAllocSizeHints();
2178 if(xw
.isfixed
== False
) {
2179 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2180 sizeh
->height
= xw
.h
;
2181 sizeh
->width
= xw
.w
;
2182 sizeh
->height_inc
= xw
.ch
;
2183 sizeh
->width_inc
= xw
.cw
;
2184 sizeh
->base_height
= 2*BORDER
;
2185 sizeh
->base_width
= 2*BORDER
;
2187 sizeh
->flags
= PMaxSize
| PMinSize
;
2188 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2189 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2192 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2197 xinitfont(Font
*f
, char *fontstr
) {
2198 FcPattern
*pattern
, *match
;
2201 pattern
= FcNameParse((FcChar8
*)fontstr
);
2203 die("st: can't open font %s\n", fontstr
);
2205 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
2206 FcPatternDestroy(pattern
);
2208 die("st: can't open font %s\n", fontstr
);
2209 if(!(f
->xft_set
= XftFontOpenPattern(xw
.dpy
, match
))) {
2210 FcPatternDestroy(match
);
2211 die("st: can't open font %s.\n", fontstr
);
2214 f
->ascent
= f
->xft_set
->ascent
;
2215 f
->descent
= f
->xft_set
->descent
;
2217 f
->rbearing
= f
->xft_set
->max_advance_width
;
2219 f
->height
= f
->xft_set
->height
;
2220 f
->width
= f
->lbearing
+ f
->rbearing
;
2224 initfonts(char *fontstr
) {
2227 xinitfont(&dc
.font
, fontstr
);
2228 xw
.cw
= dc
.font
.width
;
2229 xw
.ch
= dc
.font
.height
;
2231 fstr
= smstrcat(fontstr
, ":weight=bold", NULL
);
2232 xinitfont(&dc
.bfont
, fstr
);
2235 fstr
= smstrcat(fontstr
, ":slant=italic,oblique", NULL
);
2236 xinitfont(&dc
.ifont
, fstr
);
2239 fstr
= smstrcat(fontstr
, ":weight=bold:slant=italic,oblique", NULL
);
2240 xinitfont(&dc
.ibfont
, fstr
);
2246 XSetWindowAttributes attrs
;
2249 int sw
, sh
, major
, minor
;
2251 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2252 die("Can't open display\n");
2253 xw
.scr
= XDefaultScreen(xw
.dpy
);
2254 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2257 initfonts((opt_font
!= NULL
)? opt_font
: FONT
);
2260 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2263 /* adjust fixed window geometry */
2265 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2266 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2268 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2270 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2275 /* window - default size */
2276 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
2277 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
2282 attrs
.background_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2283 attrs
.border_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2284 attrs
.bit_gravity
= NorthWestGravity
;
2285 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2286 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2287 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2288 attrs
.colormap
= xw
.cmap
;
2290 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2291 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2292 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2294 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2298 /* double buffering */
2299 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2300 die("Xdbe extension is not present\n");
2301 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2303 /* Xft rendering context */
2304 xw
.xft_draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2307 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2308 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2309 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2310 XNFocusWindow
, xw
.win
, NULL
);
2312 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2314 /* white cursor, black outline */
2315 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2316 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2317 XRecolorCursor(xw
.dpy
, cursor
,
2318 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2319 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2321 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2322 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2323 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2326 XMapWindow(xw
.dpy
, xw
.win
);
2332 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2333 int winx
= BORDER
+ x
* xw
.cw
, winy
= BORDER
+ y
* xw
.ch
,
2334 width
= charlen
* xw
.cw
;
2335 Font
*font
= &dc
.font
;
2337 XftColor
*fg
= &dc
.xft_col
[base
.fg
], *bg
= &dc
.xft_col
[base
.bg
],
2338 *temp
, revfg
, revbg
;
2339 XRenderColor colfg
, colbg
;
2341 if(base
.mode
& ATTR_REVERSE
)
2342 temp
= fg
, fg
= bg
, bg
= temp
;
2344 if(base
.mode
& ATTR_BOLD
) {
2345 if(BETWEEN(base
.fg
, 0, 7)) {
2346 /* basic system colors */
2347 fg
= &dc
.xft_col
[base
.fg
+ 8];
2348 } else if(BETWEEN(base
.fg
, 16, 195)) {
2350 fg
= &dc
.xft_col
[base
.fg
+ 36];
2351 } else if(BETWEEN(base
.fg
, 232, 251)) {
2353 fg
= &dc
.xft_col
[base
.fg
+ 4];
2356 * Those ranges will not be brightened:
2357 * 8 - 15 – bright system colors
2358 * 196 - 231 – highest 256 color cube
2359 * 252 - 255 – brightest colors in greyscale
2364 if(base
.mode
& ATTR_ITALIC
)
2366 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2369 if(IS_SET(MODE_REVERSE
)) {
2370 if(fg
== &dc
.xft_col
[DefaultFG
]) {
2371 fg
= &dc
.xft_col
[DefaultBG
];
2373 colfg
.red
= ~fg
->color
.red
;
2374 colfg
.green
= ~fg
->color
.green
;
2375 colfg
.blue
= ~fg
->color
.blue
;
2376 colfg
.alpha
= fg
->color
.alpha
;
2377 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2381 if(bg
== &dc
.xft_col
[DefaultBG
]) {
2382 bg
= &dc
.xft_col
[DefaultFG
];
2384 colbg
.red
= ~bg
->color
.red
;
2385 colbg
.green
= ~bg
->color
.green
;
2386 colbg
.blue
= ~bg
->color
.blue
;
2387 colbg
.alpha
= bg
->color
.alpha
;
2388 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2393 XftTextExtentsUtf8(xw
.dpy
, font
->xft_set
, (FcChar8
*)s
, bytelen
,
2395 width
= extents
.xOff
;
2397 /* Intelligent cleaning up of the borders. */
2399 xclear(0, (y
== 0)? 0 : winy
, BORDER
,
2400 winy
+ xw
.ch
+ (y
== term
.row
-1)? xw
.h
: 0);
2402 if(x
+ charlen
>= term
.col
-1) {
2403 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2404 (y
== term
.row
-1)? xw
.h
: (winy
+ xw
.ch
));
2407 xclear(winx
, 0, winx
+ width
, BORDER
);
2409 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2411 XftDrawRect(xw
.xft_draw
, bg
, winx
, winy
, width
, xw
.ch
);
2412 XftDrawStringUtf8(xw
.xft_draw
, fg
, font
->xft_set
, winx
,
2413 winy
+ font
->ascent
, (FcChar8
*)s
, bytelen
);
2415 if(base
.mode
& ATTR_UNDERLINE
) {
2416 XftDrawRect(xw
.xft_draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2423 static int oldx
= 0, oldy
= 0;
2425 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2427 LIMIT(oldx
, 0, term
.col
-1);
2428 LIMIT(oldy
, 0, term
.row
-1);
2430 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2431 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2433 /* remove the old cursor */
2434 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2435 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2436 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2439 xtermclear(oldx
, oldy
, oldx
, oldy
);
2442 /* draw the new one */
2443 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2444 if(!(xw
.state
& WIN_FOCUSED
))
2447 if(IS_SET(MODE_REVERSE
))
2448 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2451 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2452 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2458 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2463 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2467 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2468 nanosleep(&tv
, NULL
);
2473 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2475 drawregion(0, 0, term
.col
, term
.row
);
2476 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2480 drawregion(int x1
, int y1
, int x2
, int y2
) {
2481 int ic
, ib
, x
, y
, ox
, sl
;
2483 char buf
[DRAW_BUF_SIZ
];
2484 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2486 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2488 if(!(xw
.state
& WIN_VISIBLE
))
2491 for(y
= y1
; y
< y2
; y
++) {
2495 xtermclear(0, y
, term
.col
, y
);
2497 base
= term
.line
[y
][0];
2499 for(x
= x1
; x
< x2
; x
++) {
2500 new = term
.line
[y
][x
];
2501 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2502 new.mode
^= ATTR_REVERSE
;
2503 if(ib
> 0 && (!(new.state
& GLYPH_SET
)
2504 || ATTRCMP(base
, new)
2505 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2506 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2509 if(new.state
& GLYPH_SET
) {
2514 sl
= utf8size(new.c
);
2515 memcpy(buf
+ib
, new.c
, sl
);
2521 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2527 expose(XEvent
*ev
) {
2528 XExposeEvent
*e
= &ev
->xexpose
;
2530 if(xw
.state
& WIN_REDRAW
) {
2532 xw
.state
&= ~WIN_REDRAW
;
2537 visibility(XEvent
*ev
) {
2538 XVisibilityEvent
*e
= &ev
->xvisibility
;
2540 if(e
->state
== VisibilityFullyObscured
) {
2541 xw
.state
&= ~WIN_VISIBLE
;
2542 } else if(!(xw
.state
& WIN_VISIBLE
)) {
2543 /* need a full redraw for next Expose, not just a buf copy */
2544 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2550 xw
.state
&= ~WIN_VISIBLE
;
2554 xseturgency(int add
) {
2555 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2557 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2558 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2564 if(ev
->type
== FocusIn
) {
2565 xw
.state
|= WIN_FOCUSED
;
2568 xw
.state
&= ~WIN_FOCUSED
;
2573 kmap(KeySym k
, uint state
) {
2578 for(i
= 0; i
< LEN(key
); i
++) {
2581 if(key
[i
].k
== k
&& ((state
& mask
) == mask
2582 || (mask
== XK_NO_MOD
&& !state
))) {
2583 return (char*)key
[i
].s
;
2590 kpress(XEvent
*ev
) {
2591 XKeyEvent
*e
= &ev
->xkey
;
2600 if (IS_SET(MODE_KBDLOCK
))
2603 meta
= e
->state
& Mod1Mask
;
2604 shift
= e
->state
& ShiftMask
;
2605 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2607 /* 1. custom keys from config.h */
2608 if((customkey
= kmap(ksym
, e
->state
))) {
2609 ttywrite(customkey
, strlen(customkey
));
2610 /* 2. hardcoded (overrides X lookup) */
2617 /* XXX: shift up/down doesn't work */
2618 sprintf(buf
, "\033%c%c",
2619 IS_SET(MODE_APPKEYPAD
) ? 'O' : '[',
2620 (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2628 if(IS_SET(MODE_CRLF
)) {
2629 ttywrite("\r\n", 2);
2637 if(meta
&& len
== 1)
2638 ttywrite("\033", 1);
2647 cmessage(XEvent
*e
) {
2649 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2650 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2651 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2652 xw
.state
|= WIN_FOCUSED
;
2654 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2655 xw
.state
&= ~WIN_FOCUSED
;
2657 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
2658 /* Send SIGHUP to shell */
2668 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2671 xw
.w
= e
->xconfigure
.width
;
2672 xw
.h
= e
->xconfigure
.height
;
2673 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2674 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2675 if(col
== term
.col
&& row
== term
.row
)
2687 int xfd
= XConnectionNumber(xw
.dpy
), i
;
2688 struct timeval drawtimeout
, *tv
= NULL
;
2692 FD_SET(cmdfd
, &rfd
);
2694 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
2697 die("select failed: %s\n", SERRNO
);
2701 * Stop after a certain number of reads so the user does not
2702 * feel like the system is stuttering.
2704 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
2708 * Just wait a bit so it isn't disturbing the
2709 * user and the system is able to write something.
2711 drawtimeout
.tv_sec
= 0;
2712 drawtimeout
.tv_usec
= 5;
2719 while(XPending(xw
.dpy
)) {
2720 XNextEvent(xw
.dpy
, &ev
);
2721 if(XFilterEvent(&ev
, xw
.win
))
2723 if(handler
[ev
.type
])
2724 (handler
[ev
.type
])(&ev
);
2733 main(int argc
, char *argv
[]) {
2734 int i
, bitm
, xr
, yr
;
2737 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2740 for(i
= 1; i
< argc
; i
++) {
2741 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2744 opt_class
= argv
[i
];
2747 /* eat every remaining arguments */
2759 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2764 if(bitm
& WidthValue
)
2766 if(bitm
& HeightValue
)
2768 if(bitm
& XNegative
&& xw
.fx
== 0)
2770 if(bitm
& XNegative
&& xw
.fy
== 0)
2773 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2782 opt_title
= argv
[i
];
2789 opt_embed
= argv
[i
];
2795 setlocale(LC_CTYPE
, "");