1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
19 #include <X11/Xatom.h>
21 #include <X11/Xutil.h>
22 #include <X11/cursorfont.h>
23 #include <X11/keysym.h>
30 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
32 #elif defined(__FreeBSD__) || defined(__DragonFly__)
37 "st-" VERSION ", (c) 2010-2011 st engineers\n" \
38 "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
41 #define XEMBED_FOCUS_IN 4
42 #define XEMBED_FOCUS_OUT 5
45 #define ESC_TITLE_SIZ 256
46 #define ESC_BUF_SIZ 256
47 #define ESC_ARG_SIZ 16
48 #define DRAW_BUF_SIZ 1024
50 #define XK_NO_MOD UINT_MAX
53 #define SERRNO strerror(errno)
54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 #define MAX(a, b) ((a) < (b) ? (b) : (a))
56 #define LEN(a) (sizeof(a) / sizeof(a[0]))
57 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
58 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
59 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
60 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
61 #define IS_SET(flag) (term.mode & (flag))
62 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
63 #define X2COL(x) (((x) - BORDER)/xw.cw)
64 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
66 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
67 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
68 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
69 CURSOR_SAVE
, CURSOR_LOAD
};
70 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
71 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
72 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
73 MODE_CRLF
=16, MODE_MOUSEBTN
=32, MODE_MOUSEMOTION
=64, MODE_MOUSE
=32|64, MODE_REVERSE
=128 };
74 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
75 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
78 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
81 char c
[UTF_SIZ
]; /* character code */
82 char mode
; /* attribute flags */
83 int fg
; /* foreground */
84 int bg
; /* background */
85 char state
; /* state flags */
91 Glyph attr
; /* current char attributes */
97 /* CSI Escape sequence structs */
98 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
100 char buf
[ESC_BUF_SIZ
]; /* raw string */
101 int len
; /* raw string length */
103 int arg
[ESC_ARG_SIZ
];
104 int narg
; /* nb of args */
108 /* Internal representation of the screen */
110 int row
; /* nb row */
111 int col
; /* nb col */
112 Line
* line
; /* screen */
113 Line
* alt
; /* alternate screen */
114 TCursor c
; /* cursor */
115 int top
; /* top scroll limit */
116 int bot
; /* bottom scroll limit */
117 int mode
; /* terminal mode flags */
118 int esc
; /* escape state flags */
119 char title
[ESC_TITLE_SIZ
];
123 /* Purely graphic info */
133 int w
; /* window width */
134 int h
; /* window height */
135 int bufw
; /* pixmap width */
136 int bufh
; /* pixmap height */
137 int ch
; /* char height */
138 int cw
; /* char width */
139 char state
; /* focus, redraw, visible */
148 /* Drawing Context */
150 unsigned long col
[256];
161 /* TODO: use better name for vars... */
166 struct {int x
, y
;} b
, e
;
169 struct timeval tclick1
;
170 struct timeval tclick2
;
175 static void die(const char*, ...);
176 static void draw(void);
177 static void drawregion(int, int, int, int);
178 static void execsh(void);
179 static void sigchld(int);
180 static void run(void);
182 static void csidump(void);
183 static void csihandle(void);
184 static void csiparse(void);
185 static void csireset(void);
187 static void tclearregion(int, int, int, int);
188 static void tcursor(int);
189 static void tdeletechar(int);
190 static void tdeleteline(int);
191 static void tinsertblank(int);
192 static void tinsertblankline(int);
193 static void tmoveto(int, int);
194 static void tnew(int, int);
195 static void tnewline(int);
196 static void tputtab(void);
197 static void tputc(char*);
198 static void treset(void);
199 static int tresize(int, int);
200 static void tscrollup(int, int);
201 static void tscrolldown(int, int);
202 static void tsetattr(int*, int);
203 static void tsetchar(char*);
204 static void tsetscroll(int, int);
205 static void tswapscreen(void);
207 static void ttynew(void);
208 static void ttyread(void);
209 static void ttyresize(int, int);
210 static void ttywrite(const char *, size_t);
212 static void xdraws(char *, Glyph
, int, int, int, int);
213 static void xhints(void);
214 static void xclear(int, int, int, int);
215 static void xdrawcursor(void);
216 static void xinit(void);
217 static void xloadcols(void);
218 static void xseturgency(int);
219 static void xsetsel(char*);
220 static void xresize(int, int);
222 static void expose(XEvent
*);
223 static void visibility(XEvent
*);
224 static void unmap(XEvent
*);
225 static char* kmap(KeySym
, unsigned int state
);
226 static void kpress(XEvent
*);
227 static void cmessage(XEvent
*);
228 static void resize(XEvent
*);
229 static void focus(XEvent
*);
230 static void brelease(XEvent
*);
231 static void bpress(XEvent
*);
232 static void bmotion(XEvent
*);
233 static void selnotify(XEvent
*);
234 static void selrequest(XEvent
*);
236 static void selinit(void);
237 static inline int selected(int, int);
238 static void selcopy(void);
239 static void selpaste();
240 static void selscroll(int, int);
242 static int utf8decode(char *, long *);
243 static int utf8encode(long *, char *);
244 static int utf8size(char *);
245 static int isfullutf8(char *, int);
247 static void (*handler
[LASTEvent
])(XEvent
*) = {
249 [ClientMessage
] = cmessage
,
250 [ConfigureNotify
] = resize
,
251 [VisibilityNotify
] = visibility
,
252 [UnmapNotify
] = unmap
,
256 [MotionNotify
] = bmotion
,
257 [ButtonPress
] = bpress
,
258 [ButtonRelease
] = brelease
,
259 [SelectionNotify
] = selnotify
,
260 [SelectionRequest
] = selrequest
,
267 static CSIEscape escseq
;
270 static Selection sel
;
271 static char **opt_cmd
= NULL
;
272 static char *opt_title
= NULL
;
273 static char *opt_embed
= NULL
;
274 static char *opt_class
= NULL
;
277 utf8decode(char *s
, long *u
) {
283 if(~c
&B7
) { /* 0xxxxxxx */
286 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
287 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
289 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
290 *u
= c
&(B3
|B2
|B1
|B0
);
292 } else if((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
297 for(i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
299 if((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
302 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
304 if((n
== 1 && *u
< 0x80) ||
305 (n
== 2 && *u
< 0x800) ||
306 (n
== 3 && *u
< 0x10000) ||
307 (*u
>= 0xD800 && *u
<= 0xDFFF))
316 utf8encode(long *u
, char *s
) {
321 sp
= (unsigned char*) s
;
324 *sp
= uc
; /* 0xxxxxxx */
326 } else if(*u
< 0x800) {
327 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
329 } else if(uc
< 0x10000) {
330 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
332 } else if(uc
<= 0x10FFFF) {
333 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
338 for(i
=n
,++sp
; i
>0; --i
,++sp
)
339 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
349 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
350 UTF-8 otherwise return 0 */
352 isfullutf8(char *s
, int b
) {
353 unsigned char *c1
, *c2
, *c3
;
355 c1
= (unsigned char *) s
;
356 c2
= (unsigned char *) ++s
;
357 c3
= (unsigned char *) ++s
;
360 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
362 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
364 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
366 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
368 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
369 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
377 unsigned char c
= *s
;
381 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
383 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
391 sel
.tclick1
.tv_sec
= 0;
392 sel
.tclick1
.tv_usec
= 0;
396 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
397 if(sel
.xtarget
== None
)
398 sel
.xtarget
= XA_STRING
;
402 selected(int x
, int y
) {
403 if(sel
.ey
== y
&& sel
.by
== y
) {
404 int bx
= MIN(sel
.bx
, sel
.ex
);
405 int ex
= MAX(sel
.bx
, sel
.ex
);
406 return BETWEEN(x
, bx
, ex
);
408 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
409 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
413 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
415 *b
= e
->xbutton
.button
;
417 *x
= X2COL(e
->xbutton
.x
);
418 *y
= Y2ROW(e
->xbutton
.y
);
419 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
420 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
421 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
422 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
426 mousereport(XEvent
*e
) {
427 int x
= X2COL(e
->xbutton
.x
);
428 int y
= Y2ROW(e
->xbutton
.y
);
429 int button
= e
->xbutton
.button
;
430 int state
= e
->xbutton
.state
;
431 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
432 static int ob
, ox
, oy
;
435 if(e
->xbutton
.type
== MotionNotify
) {
436 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
440 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
446 if(e
->xbutton
.type
== ButtonPress
) {
452 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
453 + (state
& Mod4Mask
? 8 : 0)
454 + (state
& ControlMask
? 16 : 0);
456 ttywrite(buf
, sizeof(buf
));
461 if(IS_SET(MODE_MOUSE
))
463 else if(e
->xbutton
.button
== Button1
) {
465 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
466 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
473 int x
, y
, sz
, sl
, ls
= 0;
478 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
479 ptr
= str
= malloc(sz
);
480 for(y
= 0; y
< term
.row
; y
++) {
481 for(x
= 0; x
< term
.col
; x
++)
482 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
483 sl
= utf8size(term
.line
[y
][x
].c
);
484 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
487 if(ls
&& y
< sel
.e
.y
)
496 selnotify(XEvent
*e
) {
497 unsigned long nitems
, ofs
, rem
;
504 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
505 False
, AnyPropertyType
, &type
, &format
,
506 &nitems
, &rem
, &data
)) {
507 fprintf(stderr
, "Clipboard allocation failed\n");
510 ttywrite((const char *) data
, nitems
* format
/ 8);
512 /* number of 32-bit chunks returned */
513 ofs
+= nitems
* format
/ 32;
519 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
523 selrequest(XEvent
*e
) {
524 XSelectionRequestEvent
*xsre
;
528 xsre
= (XSelectionRequestEvent
*) e
;
529 xev
.type
= SelectionNotify
;
530 xev
.requestor
= xsre
->requestor
;
531 xev
.selection
= xsre
->selection
;
532 xev
.target
= xsre
->target
;
533 xev
.time
= xsre
->time
;
537 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
538 if(xsre
->target
== xa_targets
) {
539 /* respond with the supported type */
540 Atom string
= sel
.xtarget
;
541 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
542 XA_ATOM
, 32, PropModeReplace
,
543 (unsigned char *) &string
, 1);
544 xev
.property
= xsre
->property
;
545 } else if(xsre
->target
== sel
.xtarget
) {
546 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
547 xsre
->target
, 8, PropModeReplace
,
548 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
549 xev
.property
= xsre
->property
;
552 /* all done, send a notification to the listener */
553 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
554 fprintf(stderr
, "Error sending SelectionNotify event\n");
559 /* register the selection for both the clipboard and the primary */
565 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
567 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
568 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
574 brelease(XEvent
*e
) {
575 if(IS_SET(MODE_MOUSE
)) {
579 if(e
->xbutton
.button
== Button2
)
581 else if(e
->xbutton
.button
== Button1
) {
583 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
584 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
587 gettimeofday(&now
, NULL
);
589 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
590 /* triple click on the line */
591 sel
.b
.x
= sel
.bx
= 0;
592 sel
.e
.x
= sel
.ex
= term
.col
;
593 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
595 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
596 /* double click to select word */
598 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
599 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
601 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
602 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
604 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
610 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
611 gettimeofday(&sel
.tclick1
, NULL
);
617 if(IS_SET(MODE_MOUSE
)) {
622 int oldey
= sel
.ey
, oldex
= sel
.ex
;
623 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
625 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
626 int starty
= MIN(oldey
, sel
.ey
);
627 int endy
= MAX(oldey
, sel
.ey
);
628 drawregion(0, (starty
> 0 ? starty
: 0), term
.col
, (endy
< term
.row
? endy
+1 : term
.row
));
634 die(const char *errstr
, ...) {
637 va_start(ap
, errstr
);
638 vfprintf(stderr
, errstr
, ap
);
646 char *envshell
= getenv("SHELL");
648 DEFAULT(envshell
, "sh");
649 putenv("TERM="TNAME
);
650 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
651 execvp(args
[0], args
);
658 if(waitpid(pid
, &stat
, 0) < 0)
659 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
661 exit(WEXITSTATUS(stat
));
670 /* seems to work fine on linux, openbsd and freebsd */
671 struct winsize w
= {term
.row
, term
.col
, 0, 0};
672 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
673 die("openpty failed: %s\n", SERRNO
);
675 switch(pid
= fork()) {
677 die("fork failed\n");
680 setsid(); /* create a new process group */
681 dup2(s
, STDIN_FILENO
);
682 dup2(s
, STDOUT_FILENO
);
683 dup2(s
, STDERR_FILENO
);
684 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
685 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
693 signal(SIGCHLD
, sigchld
);
700 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
702 fprintf(stderr
, "\n");
707 static char buf
[BUFSIZ
];
708 static int buflen
= 0;
711 int charsize
; /* size of utf8 char in bytes */
715 /* append read bytes to unprocessed bytes */
716 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
717 die("Couldn't read from shell: %s\n", SERRNO
);
719 /* process every complete utf8 char */
722 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
723 charsize
= utf8decode(ptr
, &utf8c
);
724 utf8encode(&utf8c
, s
);
730 /* keep any uncomplete utf8 char for the next call */
731 memmove(buf
, ptr
, buflen
);
735 ttywrite(const char *s
, size_t n
) {
736 if(write(cmdfd
, s
, n
) == -1)
737 die("write error on tty: %s\n", SERRNO
);
741 ttyresize(int x
, int y
) {
746 w
.ws_xpixel
= w
.ws_ypixel
= 0;
747 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
748 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
755 if(mode
== CURSOR_SAVE
)
757 else if(mode
== CURSOR_LOAD
)
758 term
.c
= c
, tmoveto(c
.x
, c
.y
);
767 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
769 term
.top
= 0, term
.bot
= term
.row
- 1;
770 term
.mode
= MODE_WRAP
;
771 tclearregion(0, 0, term
.col
-1, term
.row
-1);
775 tnew(int col
, int row
) {
776 /* set screen size */
777 term
.row
= row
, term
.col
= col
;
778 term
.line
= malloc(term
.row
* sizeof(Line
));
779 term
.alt
= malloc(term
.row
* sizeof(Line
));
780 for(row
= 0; row
< term
.row
; row
++) {
781 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
782 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
790 Line
* tmp
= term
.line
;
791 term
.line
= term
.alt
;
793 term
.mode
^= MODE_ALTSCREEN
;
797 tscrolldown(int orig
, int n
) {
801 LIMIT(n
, 0, term
.bot
-orig
+1);
803 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
805 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
807 term
.line
[i
] = term
.line
[i
-n
];
808 term
.line
[i
-n
] = temp
;
815 tscrollup(int orig
, int n
) {
818 LIMIT(n
, 0, term
.bot
-orig
+1);
820 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
822 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
824 term
.line
[i
] = term
.line
[i
+n
];
825 term
.line
[i
+n
] = temp
;
832 selscroll(int orig
, int n
) {
836 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
837 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
841 if(sel
.by
< term
.top
) {
845 if(sel
.ey
> term
.bot
) {
849 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
850 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
855 tnewline(int first_col
) {
858 tscrollup(term
.top
, 1);
861 tmoveto(first_col
? 0 : term
.c
.x
, y
);
867 char *p
= escseq
.buf
;
871 escseq
.priv
= 1, p
++;
873 while(p
< escseq
.buf
+escseq
.len
) {
875 escseq
.arg
[escseq
.narg
] *= 10;
876 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
878 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
889 tmoveto(int x
, int y
) {
890 LIMIT(x
, 0, term
.col
-1);
891 LIMIT(y
, 0, term
.row
-1);
892 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
899 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
900 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
901 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
905 tclearregion(int x1
, int y1
, int x2
, int y2
) {
909 temp
= x1
, x1
= x2
, x2
= temp
;
911 temp
= y1
, y1
= y2
, y2
= temp
;
913 LIMIT(x1
, 0, term
.col
-1);
914 LIMIT(x2
, 0, term
.col
-1);
915 LIMIT(y1
, 0, term
.row
-1);
916 LIMIT(y2
, 0, term
.row
-1);
918 for(y
= y1
; y
<= y2
; y
++)
919 for(x
= x1
; x
<= x2
; x
++)
920 term
.line
[y
][x
].state
= 0;
925 int src
= term
.c
.x
+ n
;
927 int size
= term
.col
- src
;
929 if(src
>= term
.col
) {
930 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
933 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
934 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
938 tinsertblank(int n
) {
941 int size
= term
.col
- dst
;
943 if(dst
>= term
.col
) {
944 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
947 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
948 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
952 tinsertblankline(int n
) {
953 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
956 tscrolldown(term
.c
.y
, n
);
961 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
964 tscrollup(term
.c
.y
, n
);
968 tsetattr(int *attr
, int l
) {
971 for(i
= 0; i
< l
; i
++) {
974 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
975 term
.c
.attr
.fg
= DefaultFG
;
976 term
.c
.attr
.bg
= DefaultBG
;
979 term
.c
.attr
.mode
|= ATTR_BOLD
;
982 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
985 term
.c
.attr
.mode
|= ATTR_REVERSE
;
988 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
991 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
994 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
997 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
999 if(BETWEEN(attr
[i
], 0, 255))
1000 term
.c
.attr
.fg
= attr
[i
];
1002 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1005 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1008 term
.c
.attr
.fg
= DefaultFG
;
1011 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1013 if(BETWEEN(attr
[i
], 0, 255))
1014 term
.c
.attr
.bg
= attr
[i
];
1016 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1019 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1022 term
.c
.attr
.bg
= DefaultBG
;
1025 if(BETWEEN(attr
[i
], 30, 37))
1026 term
.c
.attr
.fg
= attr
[i
] - 30;
1027 else if(BETWEEN(attr
[i
], 40, 47))
1028 term
.c
.attr
.bg
= attr
[i
] - 40;
1029 else if(BETWEEN(attr
[i
], 90, 97))
1030 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1031 else if(BETWEEN(attr
[i
], 100, 107))
1032 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1034 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
1042 tsetscroll(int t
, int b
) {
1045 LIMIT(t
, 0, term
.row
-1);
1046 LIMIT(b
, 0, term
.row
-1);
1058 switch(escseq
.mode
) {
1061 fprintf(stderr
, "erresc: unknown csi ");
1065 case '@': /* ICH -- Insert <n> blank char */
1066 DEFAULT(escseq
.arg
[0], 1);
1067 tinsertblank(escseq
.arg
[0]);
1069 case 'A': /* CUU -- Cursor <n> Up */
1071 DEFAULT(escseq
.arg
[0], 1);
1072 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
1074 case 'B': /* CUD -- Cursor <n> Down */
1075 DEFAULT(escseq
.arg
[0], 1);
1076 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
1078 case 'C': /* CUF -- Cursor <n> Forward */
1080 DEFAULT(escseq
.arg
[0], 1);
1081 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
1083 case 'D': /* CUB -- Cursor <n> Backward */
1084 DEFAULT(escseq
.arg
[0], 1);
1085 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
1087 case 'E': /* CNL -- Cursor <n> Down and first col */
1088 DEFAULT(escseq
.arg
[0], 1);
1089 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
1091 case 'F': /* CPL -- Cursor <n> Up and first col */
1092 DEFAULT(escseq
.arg
[0], 1);
1093 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
1095 case 'G': /* CHA -- Move to <col> */
1096 case '`': /* XXX: HPA -- same? */
1097 DEFAULT(escseq
.arg
[0], 1);
1098 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
1100 case 'H': /* CUP -- Move to <row> <col> */
1101 case 'f': /* XXX: HVP -- same? */
1102 DEFAULT(escseq
.arg
[0], 1);
1103 DEFAULT(escseq
.arg
[1], 1);
1104 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
1106 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1107 case 'J': /* ED -- Clear screen */
1109 switch(escseq
.arg
[0]) {
1111 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1112 if(term
.c
.y
< term
.row
-1)
1113 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1117 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1118 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1121 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1127 case 'K': /* EL -- Clear line */
1128 switch(escseq
.arg
[0]) {
1130 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1133 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1136 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1140 case 'S': /* SU -- Scroll <n> line up */
1141 DEFAULT(escseq
.arg
[0], 1);
1142 tscrollup(term
.top
, escseq
.arg
[0]);
1144 case 'T': /* SD -- Scroll <n> line down */
1145 DEFAULT(escseq
.arg
[0], 1);
1146 tscrolldown(term
.top
, escseq
.arg
[0]);
1148 case 'L': /* IL -- Insert <n> blank lines */
1149 DEFAULT(escseq
.arg
[0], 1);
1150 tinsertblankline(escseq
.arg
[0]);
1152 case 'l': /* RM -- Reset Mode */
1154 switch(escseq
.arg
[0]) {
1156 term
.mode
&= ~MODE_APPKEYPAD
;
1158 case 5: /* DECSCNM -- Remove reverse video */
1159 if(IS_SET(MODE_REVERSE
)) {
1160 term
.mode
&= ~MODE_REVERSE
;
1165 term
.mode
&= ~MODE_WRAP
;
1167 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1170 term
.mode
&= ~MODE_CRLF
;
1173 term
.c
.state
|= CURSOR_HIDE
;
1175 case 1000: /* disable X11 xterm mouse reporting */
1176 term
.mode
&= ~MODE_MOUSEBTN
;
1179 term
.mode
&= ~MODE_MOUSEMOTION
;
1181 case 1049: /* = 1047 and 1048 */
1184 if(IS_SET(MODE_ALTSCREEN
)) {
1185 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1188 if(escseq
.arg
[0] != 1049)
1191 tcursor(CURSOR_LOAD
);
1197 switch(escseq
.arg
[0]) {
1199 term
.mode
&= ~MODE_INSERT
;
1206 case 'M': /* DL -- Delete <n> lines */
1207 DEFAULT(escseq
.arg
[0], 1);
1208 tdeleteline(escseq
.arg
[0]);
1210 case 'X': /* ECH -- Erase <n> char */
1211 DEFAULT(escseq
.arg
[0], 1);
1212 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1214 case 'P': /* DCH -- Delete <n> char */
1215 DEFAULT(escseq
.arg
[0], 1);
1216 tdeletechar(escseq
.arg
[0]);
1218 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1219 case 'd': /* VPA -- Move to <row> */
1220 DEFAULT(escseq
.arg
[0], 1);
1221 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1223 case 'h': /* SM -- Set terminal mode */
1225 switch(escseq
.arg
[0]) {
1227 term
.mode
|= MODE_APPKEYPAD
;
1229 case 5: /* DECSCNM -- Reverve video */
1230 if(!IS_SET(MODE_REVERSE
)) {
1231 term
.mode
|= MODE_REVERSE
;
1236 term
.mode
|= MODE_WRAP
;
1239 term
.mode
|= MODE_CRLF
;
1241 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1242 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1243 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1246 term
.c
.state
&= ~CURSOR_HIDE
;
1248 case 1000: /* 1000,1002: enable xterm mouse report */
1249 term
.mode
|= MODE_MOUSEBTN
;
1252 term
.mode
|= MODE_MOUSEMOTION
;
1254 case 1049: /* = 1047 and 1048 */
1257 if(IS_SET(MODE_ALTSCREEN
))
1258 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1261 if(escseq
.arg
[0] != 1049)
1264 tcursor(CURSOR_SAVE
);
1266 default: goto unknown
;
1269 switch(escseq
.arg
[0]) {
1271 term
.mode
|= MODE_INSERT
;
1273 default: goto unknown
;
1277 case 'm': /* SGR -- Terminal attribute (color) */
1278 tsetattr(escseq
.arg
, escseq
.narg
);
1280 case 'r': /* DECSTBM -- Set Scrolling Region */
1284 DEFAULT(escseq
.arg
[0], 1);
1285 DEFAULT(escseq
.arg
[1], term
.row
);
1286 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1290 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1291 tcursor(CURSOR_SAVE
);
1293 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1294 tcursor(CURSOR_LOAD
);
1302 printf("ESC [ %s", escseq
.priv
? "? " : "");
1304 for(i
= 0; i
< escseq
.narg
; i
++)
1305 printf("%d ", escseq
.arg
[i
]);
1307 putchar(escseq
.mode
);
1313 memset(&escseq
, 0, sizeof(escseq
));
1318 int space
= TAB
- term
.c
.x
% TAB
;
1319 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1325 if(term
.esc
& ESC_START
) {
1326 if(term
.esc
& ESC_CSI
) {
1327 escseq
.buf
[escseq
.len
++] = ascii
;
1328 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1330 csiparse(), csihandle();
1332 /* TODO: handle other OSC */
1333 } else if(term
.esc
& ESC_OSC
) {
1336 term
.esc
= ESC_START
| ESC_TITLE
;
1338 } else if(term
.esc
& ESC_TITLE
) {
1339 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1341 term
.title
[term
.titlelen
] = '\0';
1342 XStoreName(xw
.dpy
, xw
.win
, term
.title
);
1344 term
.title
[term
.titlelen
++] = ascii
;
1346 } else if(term
.esc
& ESC_ALTCHARSET
) {
1348 case '0': /* Line drawing crap */
1349 term
.c
.attr
.mode
|= ATTR_GFX
;
1351 case 'B': /* Back to regular text */
1352 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1355 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1361 term
.esc
|= ESC_CSI
;
1364 term
.esc
|= ESC_OSC
;
1367 term
.esc
|= ESC_ALTCHARSET
;
1369 case 'D': /* IND -- Linefeed */
1370 if(term
.c
.y
== term
.bot
)
1371 tscrollup(term
.top
, 1);
1373 tmoveto(term
.c
.x
, term
.c
.y
+1);
1376 case 'E': /* NEL -- Next line */
1377 tnewline(1); /* always go to first col */
1380 case 'M': /* RI -- Reverse index */
1381 if(term
.c
.y
== term
.top
)
1382 tscrolldown(term
.top
, 1);
1384 tmoveto(term
.c
.x
, term
.c
.y
-1);
1387 case 'c': /* RIS -- Reset to inital state */
1391 case '=': /* DECPAM -- Application keypad */
1392 term
.mode
|= MODE_APPKEYPAD
;
1395 case '>': /* DECPNM -- Normal keypad */
1396 term
.mode
&= ~MODE_APPKEYPAD
;
1399 case '7': /* DECSC -- Save Cursor */
1400 tcursor(CURSOR_SAVE
);
1403 case '8': /* DECRC -- Restore Cursor */
1404 tcursor(CURSOR_LOAD
);
1408 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1409 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1414 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
)) sel
.bx
= -1;
1420 tmoveto(term
.c
.x
-1, term
.c
.y
);
1423 tmoveto(0, term
.c
.y
);
1428 /* go to first col if the mode is set */
1429 tnewline(IS_SET(MODE_CRLF
));
1432 if(!(xw
.state
& WIN_FOCUSED
))
1437 term
.esc
= ESC_START
;
1440 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1441 tnewline(1); /* always go to first col */
1443 if(term
.c
.x
+1 < term
.col
)
1444 tmoveto(term
.c
.x
+1, term
.c
.y
);
1446 term
.c
.state
|= CURSOR_WRAPNEXT
;
1452 tresize(int col
, int row
) {
1454 int minrow
= MIN(row
, term
.row
);
1455 int mincol
= MIN(col
, term
.col
);
1456 int slide
= term
.c
.y
- row
+ 1;
1458 if(col
< 1 || row
< 1)
1461 /* free unneeded rows */
1464 /* slide screen to keep cursor where we expect it -
1465 * tscrollup would work here, but we can optimize to
1466 * memmove because we're freeing the earlier lines */
1467 for(/* i = 0 */; i
< slide
; i
++) {
1471 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1472 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1474 for(i
+= row
; i
< term
.row
; i
++) {
1479 /* resize to new height */
1480 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1481 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1483 /* resize each row to new width, zero-pad if needed */
1484 for(i
= 0; i
< minrow
; i
++) {
1485 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1486 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1487 for(x
= mincol
; x
< col
; x
++) {
1488 term
.line
[i
][x
].state
= 0;
1489 term
.alt
[i
][x
].state
= 0;
1493 /* allocate any new rows */
1494 for(/* i == minrow */; i
< row
; i
++) {
1495 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1496 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1499 /* update terminal size */
1500 term
.col
= col
, term
.row
= row
;
1501 /* make use of the LIMIT in tmoveto */
1502 tmoveto(term
.c
.x
, term
.c
.y
);
1503 /* reset scrolling region */
1504 tsetscroll(0, row
-1);
1509 xresize(int col
, int row
) {
1515 xw
.bufw
= MAX(1, col
* xw
.cw
);
1516 xw
.bufh
= MAX(1, row
* xw
.ch
);
1517 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1518 XCopyArea(xw
.dpy
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1519 XFreePixmap(xw
.dpy
, xw
.buf
);
1520 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1522 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0,
1523 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1524 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1525 XClearArea(xw
.dpy
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1526 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1529 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
,
1530 xw
.bufw
, xw
.bufh
-oldh
);
1531 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1532 XClearArea(xw
.dpy
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1533 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1542 unsigned long white
= WhitePixel(xw
.dpy
, xw
.scr
);
1544 for(i
= 0; i
< 16; i
++) {
1545 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1547 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1549 dc
.col
[i
] = color
.pixel
;
1552 /* same colors as xterm */
1553 for(r
= 0; r
< 6; r
++)
1554 for(g
= 0; g
< 6; g
++)
1555 for(b
= 0; b
< 6; b
++) {
1556 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1557 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1558 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1559 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1561 fprintf(stderr
, "Could not allocate color %d\n", i
);
1563 dc
.col
[i
] = color
.pixel
;
1567 for(r
= 0; r
< 24; r
++, i
++) {
1568 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1569 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1571 fprintf(stderr
, "Could not allocate color %d\n", i
);
1573 dc
.col
[i
] = color
.pixel
;
1578 xclear(int x1
, int y1
, int x2
, int y2
) {
1579 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1580 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1581 x1
* xw
.cw
, y1
* xw
.ch
,
1582 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1587 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1588 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1590 .flags
= PSize
| PResizeInc
| PBaseSize
,
1593 .height_inc
= xw
.ch
,
1595 .base_height
= 2*BORDER
,
1596 .base_width
= 2*BORDER
,
1598 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1602 xinitfont(char *fontstr
) {
1604 char *def
, **missing
;
1608 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1611 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1612 XFreeStringList(missing
);
1618 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1619 XFontStruct
**xfonts
;
1623 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1624 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1625 for(i
= 0; i
< n
; i
++) {
1626 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1627 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1628 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1629 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1635 initfonts(char *fontstr
, char *bfontstr
) {
1636 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1637 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1638 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1639 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1640 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1641 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1642 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1647 XSetWindowAttributes attrs
;
1651 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1652 die("Can't open display\n");
1653 xw
.scr
= XDefaultScreen(xw
.dpy
);
1656 initfonts(FONT
, BOLDFONT
);
1658 /* XXX: Assuming same size for bold font */
1659 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1660 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1663 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1666 /* window - default size */
1667 xw
.bufh
= term
.row
* xw
.ch
;
1668 xw
.bufw
= term
.col
* xw
.cw
;
1669 xw
.h
= xw
.bufh
+ 2*BORDER
;
1670 xw
.w
= xw
.bufw
+ 2*BORDER
;
1672 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1673 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1674 attrs
.bit_gravity
= NorthWestGravity
;
1675 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1676 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1677 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1678 | EnterWindowMask
| LeaveWindowMask
;
1679 attrs
.colormap
= xw
.cmap
;
1681 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1682 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1683 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1684 XDefaultVisual(xw
.dpy
, xw
.scr
),
1685 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1688 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1692 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1693 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1694 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1695 XNFocusWindow
, xw
.win
, NULL
);
1697 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1699 /* white cursor, black outline */
1700 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1701 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1702 XRecolorCursor(xw
.dpy
, cursor
,
1703 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1704 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1706 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1708 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1709 XMapWindow(xw
.dpy
, xw
.win
);
1715 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1716 unsigned long xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
], temp
;
1717 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1720 /* only switch default fg/bg if term is in RV mode */
1721 if(IS_SET(MODE_REVERSE
)) {
1722 if(base
.fg
== DefaultFG
)
1723 xfg
= dc
.col
[DefaultBG
];
1724 if(base
.bg
== DefaultBG
)
1725 xbg
= dc
.col
[DefaultFG
];
1728 if(base
.mode
& ATTR_REVERSE
)
1729 temp
= xfg
, xfg
= xbg
, xbg
= temp
;
1731 XSetBackground(xw
.dpy
, dc
.gc
, xbg
);
1732 XSetForeground(xw
.dpy
, dc
.gc
, xfg
);
1734 if(base
.mode
& ATTR_GFX
) {
1735 for(i
= 0; i
< bytelen
; i
++) {
1736 char c
= gfx
[(unsigned int)s
[i
] % 256];
1739 else if(s
[i
] > 0x5f)
1744 XmbDrawImageString(xw
.dpy
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.set
: dc
.font
.set
,
1745 dc
.gc
, winx
, winy
, s
, bytelen
);
1747 if(base
.mode
& ATTR_UNDERLINE
)
1748 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1753 static int oldx
= 0;
1754 static int oldy
= 0;
1756 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1758 LIMIT(oldx
, 0, term
.col
-1);
1759 LIMIT(oldy
, 0, term
.row
-1);
1761 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1762 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1764 /* remove the old cursor */
1765 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1766 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1767 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1769 xclear(oldx
, oldy
, oldx
, oldy
);
1771 /* draw the new one */
1772 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1774 if(IS_SET(MODE_REVERSE
))
1775 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
1776 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1777 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1783 drawregion(0, 0, term
.col
, term
.row
);
1787 drawregion(int x1
, int y1
, int x2
, int y2
) {
1788 int ic
, ib
, x
, y
, ox
, sl
;
1790 char buf
[DRAW_BUF_SIZ
];
1792 if(!(xw
.state
& WIN_VISIBLE
))
1795 xclear(x1
, y1
, x2
-1, y2
-1);
1796 for(y
= y1
; y
< y2
; y
++) {
1797 base
= term
.line
[y
][0];
1799 for(x
= x1
; x
< x2
; x
++) {
1800 new = term
.line
[y
][x
];
1801 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
1802 new.mode
^= ATTR_REVERSE
;
1803 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1804 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1805 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1808 if(new.state
& GLYPH_SET
) {
1813 sl
= utf8size(new.c
);
1814 memcpy(buf
+ib
, new.c
, sl
);
1820 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1823 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1827 expose(XEvent
*ev
) {
1828 XExposeEvent
*e
= &ev
->xexpose
;
1829 if(xw
.state
& WIN_REDRAW
) {
1831 xw
.state
&= ~WIN_REDRAW
;
1835 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1836 e
->width
, e
->height
, e
->x
, e
->y
);
1840 visibility(XEvent
*ev
) {
1841 XVisibilityEvent
*e
= &ev
->xvisibility
;
1842 if(e
->state
== VisibilityFullyObscured
)
1843 xw
.state
&= ~WIN_VISIBLE
;
1844 else if(!(xw
.state
& WIN_VISIBLE
))
1845 /* need a full redraw for next Expose, not just a buf copy */
1846 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1851 xw
.state
&= ~WIN_VISIBLE
;
1855 xseturgency(int add
) {
1856 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1857 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1858 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1864 if(ev
->type
== FocusIn
) {
1865 xw
.state
|= WIN_FOCUSED
;
1868 xw
.state
&= ~WIN_FOCUSED
;
1873 kmap(KeySym k
, unsigned int state
) {
1876 for(i
= 0; i
< LEN(key
); i
++) {
1877 unsigned int mask
= key
[i
].mask
;
1878 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
1879 return (char*)key
[i
].s
;
1885 kpress(XEvent
*ev
) {
1886 XKeyEvent
*e
= &ev
->xkey
;
1895 meta
= e
->state
& Mod1Mask
;
1896 shift
= e
->state
& ShiftMask
;
1897 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1899 /* 1. custom keys from config.h */
1900 if((customkey
= kmap(ksym
, e
->state
)))
1901 ttywrite(customkey
, strlen(customkey
));
1902 /* 2. hardcoded (overrides X lookup) */
1909 /* XXX: shift up/down doesn't work */
1910 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
1918 if(IS_SET(MODE_CRLF
))
1919 ttywrite("\r\n", 2);
1926 if(meta
&& len
== 1)
1927 ttywrite("\033", 1);
1935 cmessage(XEvent
*e
) {
1937 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
1938 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1939 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1940 xw
.state
|= WIN_FOCUSED
;
1942 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1943 xw
.state
&= ~WIN_FOCUSED
;
1953 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1956 xw
.w
= e
->xconfigure
.width
;
1957 xw
.h
= e
->xconfigure
.height
;
1958 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
1959 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
1960 if(col
== term
.col
&& row
== term
.row
)
1962 if(tresize(col
, row
))
1964 ttyresize(col
, row
);
1972 int xfd
= XConnectionNumber(xw
.dpy
);
1976 FD_SET(cmdfd
, &rfd
);
1978 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1981 die("select failed: %s\n", SERRNO
);
1983 if(FD_ISSET(cmdfd
, &rfd
)) {
1987 while(XPending(xw
.dpy
)) {
1988 XNextEvent(xw
.dpy
, &ev
);
1989 if(XFilterEvent(&ev
, xw
.win
))
1991 if(handler
[ev
.type
])
1992 (handler
[ev
.type
])(&ev
);
1998 main(int argc
, char *argv
[]) {
2001 for(i
= 1; i
< argc
; i
++) {
2002 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2004 if(++i
< argc
) opt_title
= argv
[i
];
2007 if(++i
< argc
) opt_class
= argv
[i
];
2010 if(++i
< argc
) opt_embed
= argv
[i
];
2013 /* eat every remaining arguments */
2014 if(++i
< argc
) opt_cmd
= &argv
[i
];
2023 setlocale(LC_CTYPE
, "");