1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
17 #include <sys/types.h>
21 #include <X11/Xatom.h>
23 #include <X11/Xutil.h>
24 #include <X11/cursorfont.h>
25 #include <X11/keysym.h>
29 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
31 #elif defined(__FreeBSD__) || defined(__DragonFly__)
36 "st " VERSION " (c) 2010-2011 st engineers\n" \
37 "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
40 #define XEMBED_FOCUS_IN 4
41 #define XEMBED_FOCUS_OUT 5
44 #define ESC_TITLE_SIZ 256
45 #define ESC_BUF_SIZ 256
46 #define ESC_ARG_SIZ 16
47 #define DRAW_BUF_SIZ 1024
49 #define XK_NO_MOD UINT_MAX
52 #define SELECT_TIMEOUT (20*1000) /* 20 ms */
53 #define DRAW_TIMEOUT (20*1000) /* 20 ms */
55 #define SERRNO strerror(errno)
56 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 #define MAX(a, b) ((a) < (b) ? (b) : (a))
58 #define LEN(a) (sizeof(a) / sizeof(a[0]))
59 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
60 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
61 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
62 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
63 #define IS_SET(flag) (term.mode & (flag))
64 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
65 #define X2COL(x) (((x) - BORDER)/xw.cw)
66 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
68 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
69 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
70 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
71 CURSOR_SAVE
, CURSOR_LOAD
};
72 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
73 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
74 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
75 MODE_CRLF
=16, MODE_MOUSEBTN
=32, MODE_MOUSEMOTION
=64, MODE_MOUSE
=32|64, MODE_REVERSE
=128 };
76 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
77 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
80 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
83 char c
[UTF_SIZ
]; /* character code */
84 char mode
; /* attribute flags */
85 int fg
; /* foreground */
86 int bg
; /* background */
87 char state
; /* state flags */
93 Glyph attr
; /* current char attributes */
99 /* CSI Escape sequence structs */
100 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
102 char buf
[ESC_BUF_SIZ
]; /* raw string */
103 int len
; /* raw string length */
105 int arg
[ESC_ARG_SIZ
];
106 int narg
; /* nb of args */
110 /* Internal representation of the screen */
112 int row
; /* nb row */
113 int col
; /* nb col */
114 Line
* line
; /* screen */
115 Line
* alt
; /* alternate screen */
116 char* dirty
; /* dirtyness of lines */
117 TCursor c
; /* cursor */
118 int top
; /* top scroll limit */
119 int bot
; /* bottom scroll limit */
120 int mode
; /* terminal mode flags */
121 int esc
; /* escape state flags */
122 char title
[ESC_TITLE_SIZ
];
126 /* Purely graphic info */
136 int w
; /* window width */
137 int h
; /* window height */
138 int bufw
; /* pixmap width */
139 int bufh
; /* pixmap height */
140 int ch
; /* char height */
141 int cw
; /* char width */
142 char state
; /* focus, redraw, visible */
143 struct timeval lastdraw
;
152 /* Drawing Context */
154 unsigned long col
[256];
165 /* TODO: use better name for vars... */
170 struct {int x
, y
;} b
, e
;
173 struct timeval tclick1
;
174 struct timeval tclick2
;
179 static void die(const char*, ...);
180 static void draw(void);
181 static void drawregion(int, int, int, int);
182 static void execsh(void);
183 static void sigchld(int);
184 static void run(void);
185 static int last_draw_too_old(void);
187 static void csidump(void);
188 static void csihandle(void);
189 static void csiparse(void);
190 static void csireset(void);
192 static void tclearregion(int, int, int, int);
193 static void tcursor(int);
194 static void tdeletechar(int);
195 static void tdeleteline(int);
196 static void tinsertblank(int);
197 static void tinsertblankline(int);
198 static void tmoveto(int, int);
199 static void tnew(int, int);
200 static void tnewline(int);
201 static void tputtab(void);
202 static void tputc(char*);
203 static void treset(void);
204 static int tresize(int, int);
205 static void tscrollup(int, int);
206 static void tscrolldown(int, int);
207 static void tsetattr(int*, int);
208 static void tsetchar(char*);
209 static void tsetscroll(int, int);
210 static void tswapscreen(void);
211 static void tfulldirt(void);
213 static void ttynew(void);
214 static void ttyread(void);
215 static void ttyresize(int, int);
216 static void ttywrite(const char *, size_t);
218 static void xdraws(char *, Glyph
, int, int, int, int);
219 static void xhints(void);
220 static void xclear(int, int, int, int);
221 static void xcopy(int, int, int, int);
222 static void xdrawcursor(void);
223 static void xinit(void);
224 static void xloadcols(void);
225 static void xseturgency(int);
226 static void xsetsel(char*);
227 static void xresize(int, int);
229 static void expose(XEvent
*);
230 static void visibility(XEvent
*);
231 static void unmap(XEvent
*);
232 static char* kmap(KeySym
, unsigned int);
233 static void kpress(XEvent
*);
234 static void cmessage(XEvent
*);
235 static void resize(XEvent
*);
236 static void focus(XEvent
*);
237 static void brelease(XEvent
*);
238 static void bpress(XEvent
*);
239 static void bmotion(XEvent
*);
240 static void selnotify(XEvent
*);
241 static void selrequest(XEvent
*);
243 static void selinit(void);
244 static inline int selected(int, int);
245 static void selcopy(void);
246 static void selpaste();
247 static void selscroll(int, int);
249 static int utf8decode(char *, long *);
250 static int utf8encode(long *, char *);
251 static int utf8size(char *);
252 static int isfullutf8(char *, int);
254 static void (*handler
[LASTEvent
])(XEvent
*) = {
256 [ClientMessage
] = cmessage
,
257 [ConfigureNotify
] = resize
,
258 [VisibilityNotify
] = visibility
,
259 [UnmapNotify
] = unmap
,
263 [MotionNotify
] = bmotion
,
264 [ButtonPress
] = bpress
,
265 [ButtonRelease
] = brelease
,
266 [SelectionNotify
] = selnotify
,
267 [SelectionRequest
] = selrequest
,
274 static CSIEscape escseq
;
277 static Selection sel
;
278 static char **opt_cmd
= NULL
;
279 static char *opt_title
= NULL
;
280 static char *opt_embed
= NULL
;
281 static char *opt_class
= NULL
;
284 utf8decode(char *s
, long *u
) {
290 if(~c
&B7
) { /* 0xxxxxxx */
293 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
294 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
296 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
297 *u
= c
&(B3
|B2
|B1
|B0
);
299 } else if((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
304 for(i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
306 if((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
309 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
311 if((n
== 1 && *u
< 0x80) ||
312 (n
== 2 && *u
< 0x800) ||
313 (n
== 3 && *u
< 0x10000) ||
314 (*u
>= 0xD800 && *u
<= 0xDFFF))
323 utf8encode(long *u
, char *s
) {
328 sp
= (unsigned char*) s
;
331 *sp
= uc
; /* 0xxxxxxx */
333 } else if(*u
< 0x800) {
334 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
336 } else if(uc
< 0x10000) {
337 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
339 } else if(uc
<= 0x10FFFF) {
340 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
345 for(i
=n
,++sp
; i
>0; --i
,++sp
)
346 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
356 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
357 UTF-8 otherwise return 0 */
359 isfullutf8(char *s
, int b
) {
360 unsigned char *c1
, *c2
, *c3
;
362 c1
= (unsigned char *) s
;
363 c2
= (unsigned char *) ++s
;
364 c3
= (unsigned char *) ++s
;
367 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
369 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
371 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
373 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
375 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
376 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
384 unsigned char c
= *s
;
388 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
390 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
398 sel
.tclick1
.tv_sec
= 0;
399 sel
.tclick1
.tv_usec
= 0;
403 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
404 if(sel
.xtarget
== None
)
405 sel
.xtarget
= XA_STRING
;
409 selected(int x
, int y
) {
410 if(sel
.ey
== y
&& sel
.by
== y
) {
411 int bx
= MIN(sel
.bx
, sel
.ex
);
412 int ex
= MAX(sel
.bx
, sel
.ex
);
413 return BETWEEN(x
, bx
, ex
);
415 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
416 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
420 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
422 *b
= e
->xbutton
.button
;
424 *x
= X2COL(e
->xbutton
.x
);
425 *y
= Y2ROW(e
->xbutton
.y
);
426 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
427 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
428 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
429 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
433 mousereport(XEvent
*e
) {
434 int x
= X2COL(e
->xbutton
.x
);
435 int y
= Y2ROW(e
->xbutton
.y
);
436 int button
= e
->xbutton
.button
;
437 int state
= e
->xbutton
.state
;
438 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
439 static int ob
, ox
, oy
;
442 if(e
->xbutton
.type
== MotionNotify
) {
443 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
447 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
453 if(e
->xbutton
.type
== ButtonPress
) {
459 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
460 + (state
& Mod4Mask
? 8 : 0)
461 + (state
& ControlMask
? 16 : 0);
463 ttywrite(buf
, sizeof(buf
));
468 if(IS_SET(MODE_MOUSE
))
470 else if(e
->xbutton
.button
== Button1
) {
472 for(int i
=sel
.b
.y
; i
<=sel
.e
.y
; i
++)
475 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
476 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
483 int x
, y
, sz
, sl
, ls
= 0;
488 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
489 ptr
= str
= malloc(sz
);
490 for(y
= 0; y
< term
.row
; y
++) {
491 for(x
= 0; x
< term
.col
; x
++)
492 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
493 sl
= utf8size(term
.line
[y
][x
].c
);
494 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
497 if(ls
&& y
< sel
.e
.y
)
506 selnotify(XEvent
*e
) {
507 unsigned long nitems
, ofs
, rem
;
514 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
515 False
, AnyPropertyType
, &type
, &format
,
516 &nitems
, &rem
, &data
)) {
517 fprintf(stderr
, "Clipboard allocation failed\n");
520 ttywrite((const char *) data
, nitems
* format
/ 8);
522 /* number of 32-bit chunks returned */
523 ofs
+= nitems
* format
/ 32;
529 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
533 selrequest(XEvent
*e
) {
534 XSelectionRequestEvent
*xsre
;
538 xsre
= (XSelectionRequestEvent
*) e
;
539 xev
.type
= SelectionNotify
;
540 xev
.requestor
= xsre
->requestor
;
541 xev
.selection
= xsre
->selection
;
542 xev
.target
= xsre
->target
;
543 xev
.time
= xsre
->time
;
547 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
548 if(xsre
->target
== xa_targets
) {
549 /* respond with the supported type */
550 Atom string
= sel
.xtarget
;
551 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
552 XA_ATOM
, 32, PropModeReplace
,
553 (unsigned char *) &string
, 1);
554 xev
.property
= xsre
->property
;
555 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
556 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
557 xsre
->target
, 8, PropModeReplace
,
558 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
559 xev
.property
= xsre
->property
;
562 /* all done, send a notification to the listener */
563 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
564 fprintf(stderr
, "Error sending SelectionNotify event\n");
569 /* register the selection for both the clipboard and the primary */
575 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
577 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
578 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
584 brelease(XEvent
*e
) {
585 if(IS_SET(MODE_MOUSE
)) {
589 if(e
->xbutton
.button
== Button2
)
591 else if(e
->xbutton
.button
== Button1
) {
593 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
594 term
.dirty
[sel
.ey
] = 1;
595 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
598 gettimeofday(&now
, NULL
);
600 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
601 /* triple click on the line */
602 sel
.b
.x
= sel
.bx
= 0;
603 sel
.e
.x
= sel
.ex
= term
.col
;
604 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
606 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
607 /* double click to select word */
609 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
610 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
612 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
613 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
615 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
621 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
622 gettimeofday(&sel
.tclick1
, NULL
);
628 if(IS_SET(MODE_MOUSE
)) {
633 int oldey
= sel
.ey
, oldex
= sel
.ex
;
634 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
636 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
637 int starty
= MIN(oldey
, sel
.ey
);
638 int endy
= MAX(oldey
, sel
.ey
);
639 for(int i
=starty
; i
<=endy
; i
++)
647 die(const char *errstr
, ...) {
650 va_start(ap
, errstr
);
651 vfprintf(stderr
, errstr
, ap
);
659 char *envshell
= getenv("SHELL");
661 DEFAULT(envshell
, "sh");
662 putenv("TERM="TNAME
);
663 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
664 execvp(args
[0], args
);
671 if(waitpid(pid
, &stat
, 0) < 0)
672 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
674 exit(WEXITSTATUS(stat
));
683 /* seems to work fine on linux, openbsd and freebsd */
684 struct winsize w
= {term
.row
, term
.col
, 0, 0};
685 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
686 die("openpty failed: %s\n", SERRNO
);
688 switch(pid
= fork()) {
690 die("fork failed\n");
693 setsid(); /* create a new process group */
694 dup2(s
, STDIN_FILENO
);
695 dup2(s
, STDOUT_FILENO
);
696 dup2(s
, STDERR_FILENO
);
697 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
698 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
706 signal(SIGCHLD
, sigchld
);
713 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
715 fprintf(stderr
, "\n");
720 static char buf
[BUFSIZ
];
721 static int buflen
= 0;
724 int charsize
; /* size of utf8 char in bytes */
728 /* append read bytes to unprocessed bytes */
729 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
730 die("Couldn't read from shell: %s\n", SERRNO
);
732 /* process every complete utf8 char */
735 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
736 charsize
= utf8decode(ptr
, &utf8c
);
737 utf8encode(&utf8c
, s
);
743 /* keep any uncomplete utf8 char for the next call */
744 memmove(buf
, ptr
, buflen
);
748 ttywrite(const char *s
, size_t n
) {
749 if(write(cmdfd
, s
, n
) == -1)
750 die("write error on tty: %s\n", SERRNO
);
754 ttyresize(int x
, int y
) {
759 w
.ws_xpixel
= w
.ws_ypixel
= 0;
760 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
761 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
768 for(i
= 0; i
< term
.row
; i
++)
776 if(mode
== CURSOR_SAVE
)
778 else if(mode
== CURSOR_LOAD
)
779 term
.c
= c
, tmoveto(c
.x
, c
.y
);
788 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
790 term
.top
= 0, term
.bot
= term
.row
- 1;
791 term
.mode
= MODE_WRAP
;
792 tclearregion(0, 0, term
.col
-1, term
.row
-1);
796 tnew(int col
, int row
) {
797 /* set screen size */
798 term
.row
= row
, term
.col
= col
;
799 term
.line
= malloc(term
.row
* sizeof(Line
));
800 term
.alt
= malloc(term
.row
* sizeof(Line
));
801 term
.dirty
= malloc(term
.row
* sizeof(*term
.dirty
));
803 for(row
= 0; row
< term
.row
; row
++) {
804 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
805 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
814 Line
* tmp
= term
.line
;
815 term
.line
= term
.alt
;
817 term
.mode
^= MODE_ALTSCREEN
;
822 tscrolldown(int orig
, int n
) {
826 LIMIT(n
, 0, term
.bot
-orig
+1);
828 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
830 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
832 term
.line
[i
] = term
.line
[i
-n
];
833 term
.line
[i
-n
] = temp
;
843 tscrollup(int orig
, int n
) {
846 LIMIT(n
, 0, term
.bot
-orig
+1);
848 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
850 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
852 term
.line
[i
] = term
.line
[i
+n
];
853 term
.line
[i
+n
] = temp
;
863 selscroll(int orig
, int n
) {
867 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
868 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
872 if(sel
.by
< term
.top
) {
876 if(sel
.ey
> term
.bot
) {
880 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
881 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
886 tnewline(int first_col
) {
889 tscrollup(term
.top
, 1);
892 tmoveto(first_col
? 0 : term
.c
.x
, y
);
898 char *p
= escseq
.buf
;
902 escseq
.priv
= 1, p
++;
904 while(p
< escseq
.buf
+escseq
.len
) {
906 escseq
.arg
[escseq
.narg
] *= 10;
907 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
909 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
920 tmoveto(int x
, int y
) {
921 LIMIT(x
, 0, term
.col
-1);
922 LIMIT(y
, 0, term
.row
-1);
923 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
930 term
.dirty
[term
.c
.y
] = 1;
931 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
932 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
933 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
937 tclearregion(int x1
, int y1
, int x2
, int y2
) {
941 temp
= x1
, x1
= x2
, x2
= temp
;
943 temp
= y1
, y1
= y2
, y2
= temp
;
945 LIMIT(x1
, 0, term
.col
-1);
946 LIMIT(x2
, 0, term
.col
-1);
947 LIMIT(y1
, 0, term
.row
-1);
948 LIMIT(y2
, 0, term
.row
-1);
950 for(y
= y1
; y
<= y2
; y
++) {
952 for(x
= x1
; x
<= x2
; x
++)
953 term
.line
[y
][x
].state
= 0;
959 int src
= term
.c
.x
+ n
;
961 int size
= term
.col
- src
;
963 term
.dirty
[term
.c
.y
] = 1;
965 if(src
>= term
.col
) {
966 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
969 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
970 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
974 tinsertblank(int n
) {
977 int size
= term
.col
- dst
;
979 term
.dirty
[term
.c
.y
] = 1;
981 if(dst
>= term
.col
) {
982 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
985 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
986 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
990 tinsertblankline(int n
) {
991 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
994 tscrolldown(term
.c
.y
, n
);
999 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1002 tscrollup(term
.c
.y
, n
);
1006 tsetattr(int *attr
, int l
) {
1009 for(i
= 0; i
< l
; i
++) {
1012 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
1013 term
.c
.attr
.fg
= DefaultFG
;
1014 term
.c
.attr
.bg
= DefaultBG
;
1017 term
.c
.attr
.mode
|= ATTR_BOLD
;
1020 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1023 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1026 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1029 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1032 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1035 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1037 if(BETWEEN(attr
[i
], 0, 255))
1038 term
.c
.attr
.fg
= attr
[i
];
1040 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1043 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1046 term
.c
.attr
.fg
= DefaultFG
;
1049 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1051 if(BETWEEN(attr
[i
], 0, 255))
1052 term
.c
.attr
.bg
= attr
[i
];
1054 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1057 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1060 term
.c
.attr
.bg
= DefaultBG
;
1063 if(BETWEEN(attr
[i
], 30, 37))
1064 term
.c
.attr
.fg
= attr
[i
] - 30;
1065 else if(BETWEEN(attr
[i
], 40, 47))
1066 term
.c
.attr
.bg
= attr
[i
] - 40;
1067 else if(BETWEEN(attr
[i
], 90, 97))
1068 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1069 else if(BETWEEN(attr
[i
], 100, 107))
1070 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1072 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
1080 tsetscroll(int t
, int b
) {
1083 LIMIT(t
, 0, term
.row
-1);
1084 LIMIT(b
, 0, term
.row
-1);
1096 switch(escseq
.mode
) {
1099 fprintf(stderr
, "erresc: unknown csi ");
1103 case '@': /* ICH -- Insert <n> blank char */
1104 DEFAULT(escseq
.arg
[0], 1);
1105 tinsertblank(escseq
.arg
[0]);
1107 case 'A': /* CUU -- Cursor <n> Up */
1109 DEFAULT(escseq
.arg
[0], 1);
1110 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
1112 case 'B': /* CUD -- Cursor <n> Down */
1113 DEFAULT(escseq
.arg
[0], 1);
1114 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
1116 case 'C': /* CUF -- Cursor <n> Forward */
1118 DEFAULT(escseq
.arg
[0], 1);
1119 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
1121 case 'D': /* CUB -- Cursor <n> Backward */
1122 DEFAULT(escseq
.arg
[0], 1);
1123 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
1125 case 'E': /* CNL -- Cursor <n> Down and first col */
1126 DEFAULT(escseq
.arg
[0], 1);
1127 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
1129 case 'F': /* CPL -- Cursor <n> Up and first col */
1130 DEFAULT(escseq
.arg
[0], 1);
1131 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
1133 case 'G': /* CHA -- Move to <col> */
1134 case '`': /* XXX: HPA -- same? */
1135 DEFAULT(escseq
.arg
[0], 1);
1136 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
1138 case 'H': /* CUP -- Move to <row> <col> */
1139 case 'f': /* XXX: HVP -- same? */
1140 DEFAULT(escseq
.arg
[0], 1);
1141 DEFAULT(escseq
.arg
[1], 1);
1142 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
1144 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1145 case 'J': /* ED -- Clear screen */
1147 switch(escseq
.arg
[0]) {
1149 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1150 if(term
.c
.y
< term
.row
-1)
1151 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1155 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1156 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1159 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1165 case 'K': /* EL -- Clear line */
1166 switch(escseq
.arg
[0]) {
1168 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1171 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1174 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1178 case 'S': /* SU -- Scroll <n> line up */
1179 DEFAULT(escseq
.arg
[0], 1);
1180 tscrollup(term
.top
, escseq
.arg
[0]);
1182 case 'T': /* SD -- Scroll <n> line down */
1183 DEFAULT(escseq
.arg
[0], 1);
1184 tscrolldown(term
.top
, escseq
.arg
[0]);
1186 case 'L': /* IL -- Insert <n> blank lines */
1187 DEFAULT(escseq
.arg
[0], 1);
1188 tinsertblankline(escseq
.arg
[0]);
1190 case 'l': /* RM -- Reset Mode */
1192 switch(escseq
.arg
[0]) {
1194 term
.mode
&= ~MODE_APPKEYPAD
;
1196 case 5: /* DECSCNM -- Remove reverse video */
1197 if(IS_SET(MODE_REVERSE
)) {
1198 term
.mode
&= ~MODE_REVERSE
;
1203 term
.mode
&= ~MODE_WRAP
;
1205 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1208 term
.mode
&= ~MODE_CRLF
;
1211 term
.c
.state
|= CURSOR_HIDE
;
1213 case 1000: /* disable X11 xterm mouse reporting */
1214 term
.mode
&= ~MODE_MOUSEBTN
;
1217 term
.mode
&= ~MODE_MOUSEMOTION
;
1219 case 1049: /* = 1047 and 1048 */
1222 if(IS_SET(MODE_ALTSCREEN
)) {
1223 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1226 if(escseq
.arg
[0] != 1049)
1229 tcursor(CURSOR_LOAD
);
1235 switch(escseq
.arg
[0]) {
1237 term
.mode
&= ~MODE_INSERT
;
1244 case 'M': /* DL -- Delete <n> lines */
1245 DEFAULT(escseq
.arg
[0], 1);
1246 tdeleteline(escseq
.arg
[0]);
1248 case 'X': /* ECH -- Erase <n> char */
1249 DEFAULT(escseq
.arg
[0], 1);
1250 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1252 case 'P': /* DCH -- Delete <n> char */
1253 DEFAULT(escseq
.arg
[0], 1);
1254 tdeletechar(escseq
.arg
[0]);
1256 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1257 case 'd': /* VPA -- Move to <row> */
1258 DEFAULT(escseq
.arg
[0], 1);
1259 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1261 case 'h': /* SM -- Set terminal mode */
1263 switch(escseq
.arg
[0]) {
1265 term
.mode
|= MODE_APPKEYPAD
;
1267 case 5: /* DECSCNM -- Reverve video */
1268 if(!IS_SET(MODE_REVERSE
)) {
1269 term
.mode
|= MODE_REVERSE
;
1274 term
.mode
|= MODE_WRAP
;
1277 term
.mode
|= MODE_CRLF
;
1279 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1280 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1281 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1284 term
.c
.state
&= ~CURSOR_HIDE
;
1286 case 1000: /* 1000,1002: enable xterm mouse report */
1287 term
.mode
|= MODE_MOUSEBTN
;
1290 term
.mode
|= MODE_MOUSEMOTION
;
1292 case 1049: /* = 1047 and 1048 */
1295 if(IS_SET(MODE_ALTSCREEN
))
1296 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1299 if(escseq
.arg
[0] != 1049)
1302 tcursor(CURSOR_SAVE
);
1304 default: goto unknown
;
1307 switch(escseq
.arg
[0]) {
1309 term
.mode
|= MODE_INSERT
;
1311 default: goto unknown
;
1315 case 'm': /* SGR -- Terminal attribute (color) */
1316 tsetattr(escseq
.arg
, escseq
.narg
);
1318 case 'r': /* DECSTBM -- Set Scrolling Region */
1322 DEFAULT(escseq
.arg
[0], 1);
1323 DEFAULT(escseq
.arg
[1], term
.row
);
1324 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1328 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1329 tcursor(CURSOR_SAVE
);
1331 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1332 tcursor(CURSOR_LOAD
);
1340 printf("ESC [ %s", escseq
.priv
? "? " : "");
1342 for(i
= 0; i
< escseq
.narg
; i
++)
1343 printf("%d ", escseq
.arg
[i
]);
1345 putchar(escseq
.mode
);
1351 memset(&escseq
, 0, sizeof(escseq
));
1356 int space
= TAB
- term
.c
.x
% TAB
;
1357 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1363 if(term
.esc
& ESC_START
) {
1364 if(term
.esc
& ESC_CSI
) {
1365 escseq
.buf
[escseq
.len
++] = ascii
;
1366 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1368 csiparse(), csihandle();
1370 /* TODO: handle other OSC */
1371 } else if(term
.esc
& ESC_OSC
) {
1374 term
.esc
= ESC_START
| ESC_TITLE
;
1376 } else if(term
.esc
& ESC_TITLE
) {
1377 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1379 term
.title
[term
.titlelen
] = '\0';
1380 XStoreName(xw
.dpy
, xw
.win
, term
.title
);
1382 term
.title
[term
.titlelen
++] = ascii
;
1384 } else if(term
.esc
& ESC_ALTCHARSET
) {
1386 case '0': /* Line drawing crap */
1387 term
.c
.attr
.mode
|= ATTR_GFX
;
1389 case 'B': /* Back to regular text */
1390 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1393 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1399 term
.esc
|= ESC_CSI
;
1402 term
.esc
|= ESC_OSC
;
1405 term
.esc
|= ESC_ALTCHARSET
;
1407 case 'D': /* IND -- Linefeed */
1408 if(term
.c
.y
== term
.bot
)
1409 tscrollup(term
.top
, 1);
1411 tmoveto(term
.c
.x
, term
.c
.y
+1);
1414 case 'E': /* NEL -- Next line */
1415 tnewline(1); /* always go to first col */
1418 case 'M': /* RI -- Reverse index */
1419 if(term
.c
.y
== term
.top
)
1420 tscrolldown(term
.top
, 1);
1422 tmoveto(term
.c
.x
, term
.c
.y
-1);
1425 case 'c': /* RIS -- Reset to inital state */
1429 case '=': /* DECPAM -- Application keypad */
1430 term
.mode
|= MODE_APPKEYPAD
;
1433 case '>': /* DECPNM -- Normal keypad */
1434 term
.mode
&= ~MODE_APPKEYPAD
;
1437 case '7': /* DECSC -- Save Cursor */
1438 tcursor(CURSOR_SAVE
);
1441 case '8': /* DECRC -- Restore Cursor */
1442 tcursor(CURSOR_LOAD
);
1446 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1447 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1452 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1459 tmoveto(term
.c
.x
-1, term
.c
.y
);
1462 tmoveto(0, term
.c
.y
);
1467 /* go to first col if the mode is set */
1468 tnewline(IS_SET(MODE_CRLF
));
1471 if(!(xw
.state
& WIN_FOCUSED
))
1476 term
.esc
= ESC_START
;
1479 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1480 tnewline(1); /* always go to first col */
1482 if(term
.c
.x
+1 < term
.col
)
1483 tmoveto(term
.c
.x
+1, term
.c
.y
);
1485 term
.c
.state
|= CURSOR_WRAPNEXT
;
1491 tresize(int col
, int row
) {
1493 int minrow
= MIN(row
, term
.row
);
1494 int mincol
= MIN(col
, term
.col
);
1495 int slide
= term
.c
.y
- row
+ 1;
1497 if(col
< 1 || row
< 1)
1500 /* free unneeded rows */
1503 /* slide screen to keep cursor where we expect it -
1504 * tscrollup would work here, but we can optimize to
1505 * memmove because we're freeing the earlier lines */
1506 for(/* i = 0 */; i
< slide
; i
++) {
1510 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1511 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1513 for(i
+= row
; i
< term
.row
; i
++) {
1518 /* resize to new height */
1519 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1520 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1521 term
.dirty
= realloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1523 /* resize each row to new width, zero-pad if needed */
1524 for(i
= 0; i
< minrow
; i
++) {
1526 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1527 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1528 for(x
= mincol
; x
< col
; x
++) {
1529 term
.line
[i
][x
].state
= 0;
1530 term
.alt
[i
][x
].state
= 0;
1534 /* allocate any new rows */
1535 for(/* i == minrow */; i
< row
; i
++) {
1537 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1538 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1541 /* update terminal size */
1542 term
.col
= col
, term
.row
= row
;
1543 /* make use of the LIMIT in tmoveto */
1544 tmoveto(term
.c
.x
, term
.c
.y
);
1545 /* reset scrolling region */
1546 tsetscroll(0, row
-1);
1552 xresize(int col
, int row
) {
1558 xw
.bufw
= MAX(1, col
* xw
.cw
);
1559 xw
.bufh
= MAX(1, row
* xw
.ch
);
1560 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1561 XCopyArea(xw
.dpy
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1562 XFreePixmap(xw
.dpy
, xw
.buf
);
1563 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1565 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0,
1566 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1567 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1568 XClearArea(xw
.dpy
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1569 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1572 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
,
1573 xw
.bufw
, xw
.bufh
-oldh
);
1574 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1575 XClearArea(xw
.dpy
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1576 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1585 unsigned long white
= WhitePixel(xw
.dpy
, xw
.scr
);
1587 for(i
= 0; i
< LEN(colorname
); i
++) {
1588 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1590 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1592 dc
.col
[i
] = color
.pixel
;
1595 /* same colors as xterm */
1596 for(r
= 0; r
< 6; r
++)
1597 for(g
= 0; g
< 6; g
++)
1598 for(b
= 0; b
< 6; b
++) {
1599 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1600 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1601 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1602 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1604 fprintf(stderr
, "Could not allocate color %d\n", i
);
1606 dc
.col
[i
] = color
.pixel
;
1610 for(r
= 0; r
< 24; r
++, i
++) {
1611 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1612 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1614 fprintf(stderr
, "Could not allocate color %d\n", i
);
1616 dc
.col
[i
] = color
.pixel
;
1621 xclear(int x1
, int y1
, int x2
, int y2
) {
1622 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1623 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1624 x1
* xw
.cw
, y1
* xw
.ch
,
1625 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1630 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1631 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1633 .flags
= PSize
| PResizeInc
| PBaseSize
,
1636 .height_inc
= xw
.ch
,
1638 .base_height
= 2*BORDER
,
1639 .base_width
= 2*BORDER
,
1641 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1645 xinitfont(char *fontstr
) {
1647 char *def
, **missing
;
1651 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1654 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1655 XFreeStringList(missing
);
1661 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1662 XFontStruct
**xfonts
;
1666 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1667 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1668 for(i
= 0; i
< n
; i
++) {
1669 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1670 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1671 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1672 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1678 initfonts(char *fontstr
, char *bfontstr
) {
1679 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1680 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1681 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1682 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1683 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1684 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1685 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1690 XSetWindowAttributes attrs
;
1694 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1695 die("Can't open display\n");
1696 xw
.scr
= XDefaultScreen(xw
.dpy
);
1699 initfonts(FONT
, BOLDFONT
);
1701 /* XXX: Assuming same size for bold font */
1702 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1703 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1706 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1709 /* window - default size */
1710 xw
.bufh
= term
.row
* xw
.ch
;
1711 xw
.bufw
= term
.col
* xw
.cw
;
1712 xw
.h
= xw
.bufh
+ 2*BORDER
;
1713 xw
.w
= xw
.bufw
+ 2*BORDER
;
1715 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1716 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1717 attrs
.bit_gravity
= NorthWestGravity
;
1718 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1719 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1720 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1721 | EnterWindowMask
| LeaveWindowMask
;
1722 attrs
.colormap
= xw
.cmap
;
1724 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1725 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1726 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1727 XDefaultVisual(xw
.dpy
, xw
.scr
),
1728 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1731 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1735 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1736 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1737 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1738 XNFocusWindow
, xw
.win
, NULL
);
1740 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1742 /* white cursor, black outline */
1743 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1744 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1745 XRecolorCursor(xw
.dpy
, cursor
,
1746 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1747 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1749 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1751 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1752 XMapWindow(xw
.dpy
, xw
.win
);
1758 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1759 unsigned long xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
], temp
;
1760 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1763 /* only switch default fg/bg if term is in RV mode */
1764 if(IS_SET(MODE_REVERSE
)) {
1765 if(base
.fg
== DefaultFG
)
1766 xfg
= dc
.col
[DefaultBG
];
1767 if(base
.bg
== DefaultBG
)
1768 xbg
= dc
.col
[DefaultFG
];
1771 if(base
.mode
& ATTR_REVERSE
)
1772 temp
= xfg
, xfg
= xbg
, xbg
= temp
;
1774 XSetBackground(xw
.dpy
, dc
.gc
, xbg
);
1775 XSetForeground(xw
.dpy
, dc
.gc
, xfg
);
1777 if(base
.mode
& ATTR_GFX
) {
1778 for(i
= 0; i
< bytelen
; i
++) {
1779 char c
= gfx
[(unsigned int)s
[i
] % 256];
1782 else if(s
[i
] > 0x5f)
1787 XmbDrawImageString(xw
.dpy
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.set
: dc
.font
.set
,
1788 dc
.gc
, winx
, winy
, s
, bytelen
);
1790 if(base
.mode
& ATTR_UNDERLINE
)
1791 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1794 /* copy buffer pixmap to screen pixmap */
1796 xcopy(int x
, int y
, int cols
, int rows
) {
1797 int src_x
= x
*xw
.cw
, src_y
= y
*xw
.ch
, src_w
= cols
*xw
.cw
, src_h
= rows
*xw
.ch
;
1798 int dst_x
= BORDER
+src_x
, dst_y
= BORDER
+src_y
;
1799 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, src_x
, src_y
, src_w
, src_h
, dst_x
, dst_y
);
1804 static int oldx
= 0;
1805 static int oldy
= 0;
1807 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1809 LIMIT(oldx
, 0, term
.col
-1);
1810 LIMIT(oldy
, 0, term
.row
-1);
1812 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1813 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1815 /* remove the old cursor */
1816 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1817 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1818 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1820 xclear(oldx
, oldy
, oldx
, oldy
);
1822 xcopy(oldx
, oldy
, 1, 1);
1824 /* draw the new one */
1825 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1827 if(IS_SET(MODE_REVERSE
))
1828 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
1829 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1830 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1833 xcopy(term
.c
.x
, term
.c
.y
, 1, 1);
1838 drawregion(0, 0, term
.col
, term
.row
);
1839 gettimeofday(&xw
.lastdraw
, NULL
);
1843 drawregion(int x1
, int y1
, int x2
, int y2
) {
1844 int ic
, ib
, x
, y
, ox
, sl
;
1846 char buf
[DRAW_BUF_SIZ
];
1848 if(!(xw
.state
& WIN_VISIBLE
))
1851 for(y
= y1
; y
< y2
; y
++) {
1854 xclear(0, y
, term
.col
, y
);
1856 base
= term
.line
[y
][0];
1858 for(x
= x1
; x
< x2
; x
++) {
1859 new = term
.line
[y
][x
];
1860 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
1861 new.mode
^= ATTR_REVERSE
;
1862 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1863 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1864 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1867 if(new.state
& GLYPH_SET
) {
1872 sl
= utf8size(new.c
);
1873 memcpy(buf
+ib
, new.c
, sl
);
1879 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1880 xcopy(0, y
, term
.col
, 1);
1886 expose(XEvent
*ev
) {
1887 XExposeEvent
*e
= &ev
->xexpose
;
1888 if(xw
.state
& WIN_REDRAW
) {
1890 xw
.state
&= ~WIN_REDRAW
;
1894 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1895 e
->width
, e
->height
, e
->x
, e
->y
);
1899 visibility(XEvent
*ev
) {
1900 XVisibilityEvent
*e
= &ev
->xvisibility
;
1901 if(e
->state
== VisibilityFullyObscured
)
1902 xw
.state
&= ~WIN_VISIBLE
;
1903 else if(!(xw
.state
& WIN_VISIBLE
))
1904 /* need a full redraw for next Expose, not just a buf copy */
1905 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1910 xw
.state
&= ~WIN_VISIBLE
;
1914 xseturgency(int add
) {
1915 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1916 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1917 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1923 if(ev
->type
== FocusIn
) {
1924 xw
.state
|= WIN_FOCUSED
;
1927 xw
.state
&= ~WIN_FOCUSED
;
1932 kmap(KeySym k
, unsigned int state
) {
1935 for(i
= 0; i
< LEN(key
); i
++) {
1936 unsigned int mask
= key
[i
].mask
;
1937 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
1938 return (char*)key
[i
].s
;
1944 kpress(XEvent
*ev
) {
1945 XKeyEvent
*e
= &ev
->xkey
;
1954 meta
= e
->state
& Mod1Mask
;
1955 shift
= e
->state
& ShiftMask
;
1956 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1958 /* 1. custom keys from config.h */
1959 if((customkey
= kmap(ksym
, e
->state
)))
1960 ttywrite(customkey
, strlen(customkey
));
1961 /* 2. hardcoded (overrides X lookup) */
1968 /* XXX: shift up/down doesn't work */
1969 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
1977 if(IS_SET(MODE_CRLF
))
1978 ttywrite("\r\n", 2);
1985 if(meta
&& len
== 1)
1986 ttywrite("\033", 1);
1994 cmessage(XEvent
*e
) {
1996 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
1997 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1998 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1999 xw
.state
|= WIN_FOCUSED
;
2001 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2002 xw
.state
&= ~WIN_FOCUSED
;
2012 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2015 xw
.w
= e
->xconfigure
.width
;
2016 xw
.h
= e
->xconfigure
.height
;
2017 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2018 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2019 if(col
== term
.col
&& row
== term
.row
)
2021 if(tresize(col
, row
))
2023 ttyresize(col
, row
);
2028 last_draw_too_old(void) {
2030 gettimeofday(&now
, NULL
);
2031 return TIMEDIFF(now
, xw
.lastdraw
) >= PRINT_TIMEOUT
/1000;
2038 int xfd
= XConnectionNumber(xw
.dpy
);
2039 struct timeval timeout
= {0};
2040 int stuff_to_print
= 0;
2044 FD_SET(cmdfd
, &rfd
);
2047 timeout
.tv_usec
= SELECT_TIMEOUT
;
2048 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
2051 die("select failed: %s\n", SERRNO
);
2053 if(FD_ISSET(cmdfd
, &rfd
)) {
2058 if(stuff_to_print
&& last_draw_too_old()) {
2063 while(XPending(xw
.dpy
)) {
2064 XNextEvent(xw
.dpy
, &ev
);
2065 if(XFilterEvent(&ev
, xw
.win
))
2067 if(handler
[ev
.type
])
2068 (handler
[ev
.type
])(&ev
);
2074 main(int argc
, char *argv
[]) {
2077 for(i
= 1; i
< argc
; i
++) {
2078 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2080 if(++i
< argc
) opt_title
= argv
[i
];
2083 if(++i
< argc
) opt_class
= argv
[i
];
2086 if(++i
< argc
) opt_embed
= argv
[i
];
2089 /* eat every remaining arguments */
2090 if(++i
< argc
) opt_cmd
= &argv
[i
];
2099 setlocale(LC_CTYPE
, "");