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
51 #define SERRNO strerror(errno)
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 #define MAX(a, b) ((a) < (b) ? (b) : (a))
54 #define LEN(a) (sizeof(a) / sizeof(a[0]))
55 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
56 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
57 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
58 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
59 #define IS_SET(flag) (term.mode & (flag))
60 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
61 #define X2COL(x) (((x) - BORDER)/xw.cw)
62 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
64 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
65 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
66 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
67 CURSOR_SAVE
, CURSOR_LOAD
};
68 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
69 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
70 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
71 MODE_CRLF
=16, MODE_MOUSEBTN
=32, MODE_MOUSEMOTION
=64, MODE_MOUSE
=32|64, MODE_REVERSE
=128 };
72 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
73 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
76 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
79 char c
[UTF_SIZ
]; /* character code */
80 char mode
; /* attribute flags */
81 int fg
; /* foreground */
82 int bg
; /* background */
83 char state
; /* state flags */
89 Glyph attr
; /* current char attributes */
95 /* CSI Escape sequence structs */
96 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
98 char buf
[ESC_BUF_SIZ
]; /* raw string */
99 int len
; /* raw string length */
101 int arg
[ESC_ARG_SIZ
];
102 int narg
; /* nb of args */
106 /* Internal representation of the screen */
108 int row
; /* nb row */
109 int col
; /* nb col */
110 Line
* line
; /* screen */
111 Line
* alt
; /* alternate screen */
112 TCursor c
; /* cursor */
113 int top
; /* top scroll limit */
114 int bot
; /* bottom scroll limit */
115 int mode
; /* terminal mode flags */
116 int esc
; /* escape state flags */
117 char title
[ESC_TITLE_SIZ
];
121 /* Purely graphic info */
130 int w
; /* window width */
131 int h
; /* window height */
132 int bufw
; /* pixmap width */
133 int bufh
; /* pixmap height */
134 int ch
; /* char height */
135 int cw
; /* char width */
136 char state
; /* focus, redraw, visible */
145 /* Drawing Context */
147 unsigned long col
[256];
158 /* TODO: use better name for vars... */
163 struct {int x
, y
;} b
, e
;
166 struct timeval tclick1
;
167 struct timeval tclick2
;
172 static void die(const char*, ...);
173 static void draw(void);
174 static void drawregion(int, int, int, int);
175 static void execsh(void);
176 static void sigchld(int);
177 static void run(void);
179 static void csidump(void);
180 static void csihandle(void);
181 static void csiparse(void);
182 static void csireset(void);
184 static void tclearregion(int, int, int, int);
185 static void tcursor(int);
186 static void tdeletechar(int);
187 static void tdeleteline(int);
188 static void tinsertblank(int);
189 static void tinsertblankline(int);
190 static void tmoveto(int, int);
191 static void tnew(int, int);
192 static void tnewline(int);
193 static void tputtab(void);
194 static void tputc(char*);
195 static void treset(void);
196 static int tresize(int, int);
197 static void tscrollup(int, int);
198 static void tscrolldown(int, int);
199 static void tsetattr(int*, int);
200 static void tsetchar(char*);
201 static void tsetscroll(int, int);
202 static void tswapscreen(void);
204 static void ttynew(void);
205 static void ttyread(void);
206 static void ttyresize(int, int);
207 static void ttywrite(const char *, size_t);
209 static void xdraws(char *, Glyph
, int, int, int, int);
210 static void xhints(void);
211 static void xclear(int, int, int, int);
212 static void xdrawcursor(void);
213 static void xinit(void);
214 static void xloadcols(void);
215 static void xseturgency(int);
216 static void xsetsel(char*);
217 static void xresize(int, int);
219 static void expose(XEvent
*);
220 static void visibility(XEvent
*);
221 static void unmap(XEvent
*);
222 static char* kmap(KeySym
, unsigned int state
);
223 static void kpress(XEvent
*);
224 static void cmessage(XEvent
*);
225 static void resize(XEvent
*);
226 static void focus(XEvent
*);
227 static void brelease(XEvent
*);
228 static void bpress(XEvent
*);
229 static void bmotion(XEvent
*);
230 static void selnotify(XEvent
*);
231 static void selrequest(XEvent
*);
233 static void selinit(void);
234 static inline int selected(int, int);
235 static void selcopy(void);
236 static void selpaste();
238 static int utf8decode(char *, long *);
239 static int utf8encode(long *, char *);
240 static int utf8size(char *);
241 static int isfullutf8(char *, int);
243 static void (*handler
[LASTEvent
])(XEvent
*) = {
245 [ClientMessage
] = cmessage
,
246 [ConfigureNotify
] = resize
,
247 [VisibilityNotify
] = visibility
,
248 [UnmapNotify
] = unmap
,
252 [MotionNotify
] = bmotion
,
253 [ButtonPress
] = bpress
,
254 [ButtonRelease
] = brelease
,
255 [SelectionNotify
] = selnotify
,
256 [SelectionRequest
] = selrequest
,
263 static CSIEscape escseq
;
266 static Selection sel
;
267 static char **opt_cmd
= NULL
;
268 static char *opt_title
= NULL
;
269 static char *opt_embed
= NULL
;
270 static char *opt_class
= NULL
;
271 static Atom xembedatom
;
274 utf8decode(char *s
, long *u
) {
280 if(~c
&B7
) { /* 0xxxxxxx */
283 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
284 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
286 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
287 *u
= c
&(B3
|B2
|B1
|B0
);
289 } else if((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
294 for(i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
296 if((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
299 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
301 if((n
== 1 && *u
< 0x80) ||
302 (n
== 2 && *u
< 0x800) ||
303 (n
== 3 && *u
< 0x10000) ||
304 (*u
>= 0xD800 && *u
<= 0xDFFF))
313 utf8encode(long *u
, char *s
) {
318 sp
= (unsigned char*) s
;
321 *sp
= uc
; /* 0xxxxxxx */
323 } else if(*u
< 0x800) {
324 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
326 } else if(uc
< 0x10000) {
327 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
329 } else if(uc
<= 0x10FFFF) {
330 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
335 for(i
=n
,++sp
; i
>0; --i
,++sp
)
336 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
346 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
347 UTF-8 otherwise return 0 */
349 isfullutf8(char *s
, int b
) {
350 unsigned char *c1
, *c2
, *c3
;
352 c1
= (unsigned char *) s
;
353 c2
= (unsigned char *) ++s
;
354 c3
= (unsigned char *) ++s
;
357 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
359 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
361 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
363 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
365 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
366 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
374 unsigned char c
= *s
;
378 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
380 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
388 sel
.tclick1
.tv_sec
= 0;
389 sel
.tclick1
.tv_usec
= 0;
393 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
394 if(sel
.xtarget
== None
)
395 sel
.xtarget
= XA_STRING
;
399 selected(int x
, int y
) {
400 if(sel
.ey
== y
&& sel
.by
== y
) {
401 int bx
= MIN(sel
.bx
, sel
.ex
);
402 int ex
= MAX(sel
.bx
, sel
.ex
);
403 return BETWEEN(x
, bx
, ex
);
405 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
406 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
410 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
412 *b
= e
->xbutton
.button
;
414 *x
= X2COL(e
->xbutton
.x
);
415 *y
= Y2ROW(e
->xbutton
.y
);
416 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
417 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
418 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
419 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
423 mousereport(XEvent
*e
) {
424 int x
= X2COL(e
->xbutton
.x
);
425 int y
= Y2ROW(e
->xbutton
.y
);
426 int button
= e
->xbutton
.button
;
427 int state
= e
->xbutton
.state
;
428 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
429 static int ob
, ox
, oy
;
432 if(e
->xbutton
.type
== MotionNotify
) {
433 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
437 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
443 if(e
->xbutton
.type
== ButtonPress
) {
449 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
450 + (state
& Mod4Mask
? 8 : 0)
451 + (state
& ControlMask
? 16 : 0);
453 ttywrite(buf
, sizeof(buf
));
458 if(IS_SET(MODE_MOUSE
))
460 else if(e
->xbutton
.button
== Button1
) {
462 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
463 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
470 int x
, y
, sz
, sl
, ls
= 0;
475 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
476 ptr
= str
= malloc(sz
);
477 for(y
= 0; y
< term
.row
; y
++) {
478 for(x
= 0; x
< term
.col
; x
++)
479 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
480 sl
= utf8size(term
.line
[y
][x
].c
);
481 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
484 if(ls
&& y
< sel
.e
.y
)
493 selnotify(XEvent
*e
) {
494 unsigned long nitems
, ofs
, rem
;
501 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
502 False
, AnyPropertyType
, &type
, &format
,
503 &nitems
, &rem
, &data
)) {
504 fprintf(stderr
, "Clipboard allocation failed\n");
507 ttywrite((const char *) data
, nitems
* format
/ 8);
509 /* number of 32-bit chunks returned */
510 ofs
+= nitems
* format
/ 32;
516 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
520 selrequest(XEvent
*e
) {
521 XSelectionRequestEvent
*xsre
;
525 xsre
= (XSelectionRequestEvent
*) e
;
526 xev
.type
= SelectionNotify
;
527 xev
.requestor
= xsre
->requestor
;
528 xev
.selection
= xsre
->selection
;
529 xev
.target
= xsre
->target
;
530 xev
.time
= xsre
->time
;
534 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
535 if(xsre
->target
== xa_targets
) {
536 /* respond with the supported type */
537 Atom string
= sel
.xtarget
;
538 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
539 XA_ATOM
, 32, PropModeReplace
,
540 (unsigned char *) &string
, 1);
541 xev
.property
= xsre
->property
;
542 } else if(xsre
->target
== sel
.xtarget
) {
543 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
544 xsre
->target
, 8, PropModeReplace
,
545 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
546 xev
.property
= xsre
->property
;
549 /* all done, send a notification to the listener */
550 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
551 fprintf(stderr
, "Error sending SelectionNotify event\n");
556 /* register the selection for both the clipboard and the primary */
562 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
564 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
565 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
571 brelease(XEvent
*e
) {
572 if(IS_SET(MODE_MOUSE
)) {
576 if(e
->xbutton
.button
== Button2
)
578 else if(e
->xbutton
.button
== Button1
) {
580 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
581 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
584 gettimeofday(&now
, NULL
);
586 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
587 /* triple click on the line */
588 sel
.b
.x
= sel
.bx
= 0;
589 sel
.e
.x
= sel
.ex
= term
.col
;
590 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
592 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
593 /* double click to select word */
595 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
596 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
598 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
599 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
601 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
607 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
608 gettimeofday(&sel
.tclick1
, NULL
);
614 if(IS_SET(MODE_MOUSE
)) {
619 int oldey
= sel
.ey
, oldex
= sel
.ex
;
620 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
622 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
623 int starty
= MIN(oldey
, sel
.ey
);
624 int endy
= MAX(oldey
, sel
.ey
);
625 drawregion(0, (starty
> 0 ? starty
: 0), term
.col
, (endy
< term
.row
? endy
+1 : term
.row
));
631 die(const char *errstr
, ...) {
634 va_start(ap
, errstr
);
635 vfprintf(stderr
, errstr
, ap
);
643 char *envshell
= getenv("SHELL");
645 DEFAULT(envshell
, "sh");
646 putenv("TERM="TNAME
);
647 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
648 execvp(args
[0], args
);
655 if(waitpid(pid
, &stat
, 0) < 0)
656 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
658 exit(WEXITSTATUS(stat
));
667 /* seems to work fine on linux, openbsd and freebsd */
668 struct winsize w
= {term
.row
, term
.col
, 0, 0};
669 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
670 die("openpty failed: %s\n", SERRNO
);
672 switch(pid
= fork()) {
674 die("fork failed\n");
677 setsid(); /* create a new process group */
678 dup2(s
, STDIN_FILENO
);
679 dup2(s
, STDOUT_FILENO
);
680 dup2(s
, STDERR_FILENO
);
681 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
682 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
690 signal(SIGCHLD
, sigchld
);
697 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
699 fprintf(stderr
, "\n");
704 static char buf
[BUFSIZ
];
705 static int buflen
= 0;
708 int charsize
; /* size of utf8 char in bytes */
712 /* append read bytes to unprocessed bytes */
713 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
714 die("Couldn't read from shell: %s\n", SERRNO
);
716 /* process every complete utf8 char */
719 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
720 charsize
= utf8decode(ptr
, &utf8c
);
721 utf8encode(&utf8c
, s
);
727 /* keep any uncomplete utf8 char for the next call */
728 memmove(buf
, ptr
, buflen
);
732 ttywrite(const char *s
, size_t n
) {
733 if(write(cmdfd
, s
, n
) == -1)
734 die("write error on tty: %s\n", SERRNO
);
738 ttyresize(int x
, int y
) {
743 w
.ws_xpixel
= w
.ws_ypixel
= 0;
744 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
745 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
752 if(mode
== CURSOR_SAVE
)
754 else if(mode
== CURSOR_LOAD
)
755 term
.c
= c
, tmoveto(c
.x
, c
.y
);
764 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
766 term
.top
= 0, term
.bot
= term
.row
- 1;
767 term
.mode
= MODE_WRAP
;
768 tclearregion(0, 0, term
.col
-1, term
.row
-1);
772 tnew(int col
, int row
) {
773 /* set screen size */
774 term
.row
= row
, term
.col
= col
;
775 term
.line
= malloc(term
.row
* sizeof(Line
));
776 term
.alt
= malloc(term
.row
* sizeof(Line
));
777 for(row
= 0; row
< term
.row
; row
++) {
778 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
779 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
787 Line
* tmp
= term
.line
;
788 term
.line
= term
.alt
;
790 term
.mode
^= MODE_ALTSCREEN
;
794 tscrolldown(int orig
, int n
) {
798 LIMIT(n
, 0, term
.bot
-orig
+1);
800 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
802 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
804 term
.line
[i
] = term
.line
[i
-n
];
805 term
.line
[i
-n
] = temp
;
810 tscrollup(int orig
, int n
) {
813 LIMIT(n
, 0, term
.bot
-orig
+1);
815 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
817 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
819 term
.line
[i
] = term
.line
[i
+n
];
820 term
.line
[i
+n
] = temp
;
825 tnewline(int first_col
) {
828 tscrollup(term
.top
, 1);
831 tmoveto(first_col
? 0 : term
.c
.x
, y
);
837 char *p
= escseq
.buf
;
841 escseq
.priv
= 1, p
++;
843 while(p
< escseq
.buf
+escseq
.len
) {
845 escseq
.arg
[escseq
.narg
] *= 10;
846 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
848 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
859 tmoveto(int x
, int y
) {
860 LIMIT(x
, 0, term
.col
-1);
861 LIMIT(y
, 0, term
.row
-1);
862 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
869 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
870 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
871 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
875 tclearregion(int x1
, int y1
, int x2
, int y2
) {
879 temp
= x1
, x1
= x2
, x2
= temp
;
881 temp
= y1
, y1
= y2
, y2
= temp
;
883 LIMIT(x1
, 0, term
.col
-1);
884 LIMIT(x2
, 0, term
.col
-1);
885 LIMIT(y1
, 0, term
.row
-1);
886 LIMIT(y2
, 0, term
.row
-1);
888 for(y
= y1
; y
<= y2
; y
++)
889 for(x
= x1
; x
<= x2
; x
++)
890 term
.line
[y
][x
].state
= 0;
895 int src
= term
.c
.x
+ n
;
897 int size
= term
.col
- src
;
899 if(src
>= term
.col
) {
900 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
903 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
904 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
908 tinsertblank(int n
) {
911 int size
= term
.col
- dst
;
913 if(dst
>= term
.col
) {
914 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
917 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
918 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
922 tinsertblankline(int n
) {
923 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
926 tscrolldown(term
.c
.y
, n
);
931 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
934 tscrollup(term
.c
.y
, n
);
938 tsetattr(int *attr
, int l
) {
941 for(i
= 0; i
< l
; i
++) {
944 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
945 term
.c
.attr
.fg
= DefaultFG
;
946 term
.c
.attr
.bg
= DefaultBG
;
949 term
.c
.attr
.mode
|= ATTR_BOLD
;
952 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
955 term
.c
.attr
.mode
|= ATTR_REVERSE
;
958 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
961 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
964 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
967 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
969 if(BETWEEN(attr
[i
], 0, 255))
970 term
.c
.attr
.fg
= attr
[i
];
972 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
975 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
978 term
.c
.attr
.fg
= DefaultFG
;
981 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
983 if(BETWEEN(attr
[i
], 0, 255))
984 term
.c
.attr
.bg
= attr
[i
];
986 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
989 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
992 term
.c
.attr
.bg
= DefaultBG
;
995 if(BETWEEN(attr
[i
], 30, 37))
996 term
.c
.attr
.fg
= attr
[i
] - 30;
997 else if(BETWEEN(attr
[i
], 40, 47))
998 term
.c
.attr
.bg
= attr
[i
] - 40;
999 else if(BETWEEN(attr
[i
], 90, 97))
1000 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1001 else if(BETWEEN(attr
[i
], 100, 107))
1002 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1004 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
1012 tsetscroll(int t
, int b
) {
1015 LIMIT(t
, 0, term
.row
-1);
1016 LIMIT(b
, 0, term
.row
-1);
1028 switch(escseq
.mode
) {
1031 fprintf(stderr
, "erresc: unknown csi ");
1035 case '@': /* ICH -- Insert <n> blank char */
1036 DEFAULT(escseq
.arg
[0], 1);
1037 tinsertblank(escseq
.arg
[0]);
1039 case 'A': /* CUU -- Cursor <n> Up */
1041 DEFAULT(escseq
.arg
[0], 1);
1042 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
1044 case 'B': /* CUD -- Cursor <n> Down */
1045 DEFAULT(escseq
.arg
[0], 1);
1046 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
1048 case 'C': /* CUF -- Cursor <n> Forward */
1050 DEFAULT(escseq
.arg
[0], 1);
1051 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
1053 case 'D': /* CUB -- Cursor <n> Backward */
1054 DEFAULT(escseq
.arg
[0], 1);
1055 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
1057 case 'E': /* CNL -- Cursor <n> Down and first col */
1058 DEFAULT(escseq
.arg
[0], 1);
1059 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
1061 case 'F': /* CPL -- Cursor <n> Up and first col */
1062 DEFAULT(escseq
.arg
[0], 1);
1063 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
1065 case 'G': /* CHA -- Move to <col> */
1066 case '`': /* XXX: HPA -- same? */
1067 DEFAULT(escseq
.arg
[0], 1);
1068 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
1070 case 'H': /* CUP -- Move to <row> <col> */
1071 case 'f': /* XXX: HVP -- same? */
1072 DEFAULT(escseq
.arg
[0], 1);
1073 DEFAULT(escseq
.arg
[1], 1);
1074 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
1076 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1077 case 'J': /* ED -- Clear screen */
1078 switch(escseq
.arg
[0]) {
1080 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1081 if(term
.c
.y
< term
.row
-1)
1082 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1086 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1087 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1090 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1096 case 'K': /* EL -- Clear line */
1097 switch(escseq
.arg
[0]) {
1099 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1102 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1105 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1109 case 'S': /* SU -- Scroll <n> line up */
1110 DEFAULT(escseq
.arg
[0], 1);
1111 tscrollup(term
.top
, escseq
.arg
[0]);
1113 case 'T': /* SD -- Scroll <n> line down */
1114 DEFAULT(escseq
.arg
[0], 1);
1115 tscrolldown(term
.top
, escseq
.arg
[0]);
1117 case 'L': /* IL -- Insert <n> blank lines */
1118 DEFAULT(escseq
.arg
[0], 1);
1119 tinsertblankline(escseq
.arg
[0]);
1121 case 'l': /* RM -- Reset Mode */
1123 switch(escseq
.arg
[0]) {
1125 term
.mode
&= ~MODE_APPKEYPAD
;
1127 case 5: /* DECSCNM -- Remove reverse video */
1128 if(IS_SET(MODE_REVERSE
)) {
1129 term
.mode
&= ~MODE_REVERSE
;
1134 term
.mode
&= ~MODE_WRAP
;
1136 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1139 term
.mode
&= ~MODE_CRLF
;
1142 term
.c
.state
|= CURSOR_HIDE
;
1144 case 1000: /* disable X11 xterm mouse reporting */
1145 term
.mode
&= ~MODE_MOUSEBTN
;
1148 term
.mode
&= ~MODE_MOUSEMOTION
;
1150 case 1049: /* = 1047 and 1048 */
1153 if(IS_SET(MODE_ALTSCREEN
)) {
1154 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1157 if(escseq
.arg
[0] != 1049)
1160 tcursor(CURSOR_LOAD
);
1166 switch(escseq
.arg
[0]) {
1168 term
.mode
&= ~MODE_INSERT
;
1175 case 'M': /* DL -- Delete <n> lines */
1176 DEFAULT(escseq
.arg
[0], 1);
1177 tdeleteline(escseq
.arg
[0]);
1179 case 'X': /* ECH -- Erase <n> char */
1180 DEFAULT(escseq
.arg
[0], 1);
1181 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1183 case 'P': /* DCH -- Delete <n> char */
1184 DEFAULT(escseq
.arg
[0], 1);
1185 tdeletechar(escseq
.arg
[0]);
1187 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1188 case 'd': /* VPA -- Move to <row> */
1189 DEFAULT(escseq
.arg
[0], 1);
1190 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1192 case 'h': /* SM -- Set terminal mode */
1194 switch(escseq
.arg
[0]) {
1196 term
.mode
|= MODE_APPKEYPAD
;
1198 case 5: /* DECSCNM -- Reverve video */
1199 if(!IS_SET(MODE_REVERSE
)) {
1200 term
.mode
|= MODE_REVERSE
;
1205 term
.mode
|= MODE_WRAP
;
1208 term
.mode
|= MODE_CRLF
;
1210 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1211 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1212 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1215 term
.c
.state
&= ~CURSOR_HIDE
;
1217 case 1000: /* 1000,1002: enable xterm mouse report */
1218 term
.mode
|= MODE_MOUSEBTN
;
1221 term
.mode
|= MODE_MOUSEMOTION
;
1223 case 1049: /* = 1047 and 1048 */
1226 if(IS_SET(MODE_ALTSCREEN
))
1227 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1230 if(escseq
.arg
[0] != 1049)
1233 tcursor(CURSOR_SAVE
);
1235 default: goto unknown
;
1238 switch(escseq
.arg
[0]) {
1240 term
.mode
|= MODE_INSERT
;
1242 default: goto unknown
;
1246 case 'm': /* SGR -- Terminal attribute (color) */
1247 tsetattr(escseq
.arg
, escseq
.narg
);
1249 case 'r': /* DECSTBM -- Set Scrolling Region */
1253 DEFAULT(escseq
.arg
[0], 1);
1254 DEFAULT(escseq
.arg
[1], term
.row
);
1255 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1259 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1260 tcursor(CURSOR_SAVE
);
1262 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1263 tcursor(CURSOR_LOAD
);
1271 printf("ESC [ %s", escseq
.priv
? "? " : "");
1273 for(i
= 0; i
< escseq
.narg
; i
++)
1274 printf("%d ", escseq
.arg
[i
]);
1276 putchar(escseq
.mode
);
1282 memset(&escseq
, 0, sizeof(escseq
));
1287 int space
= TAB
- term
.c
.x
% TAB
;
1288 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1294 if(term
.esc
& ESC_START
) {
1295 if(term
.esc
& ESC_CSI
) {
1296 escseq
.buf
[escseq
.len
++] = ascii
;
1297 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1299 csiparse(), csihandle();
1301 /* TODO: handle other OSC */
1302 } else if(term
.esc
& ESC_OSC
) {
1305 term
.esc
= ESC_START
| ESC_TITLE
;
1307 } else if(term
.esc
& ESC_TITLE
) {
1308 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1310 term
.title
[term
.titlelen
] = '\0';
1311 XStoreName(xw
.dpy
, xw
.win
, term
.title
);
1313 term
.title
[term
.titlelen
++] = ascii
;
1315 } else if(term
.esc
& ESC_ALTCHARSET
) {
1317 case '0': /* Line drawing crap */
1318 term
.c
.attr
.mode
|= ATTR_GFX
;
1320 case 'B': /* Back to regular text */
1321 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1324 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1330 term
.esc
|= ESC_CSI
;
1333 term
.esc
|= ESC_OSC
;
1336 term
.esc
|= ESC_ALTCHARSET
;
1338 case 'D': /* IND -- Linefeed */
1339 if(term
.c
.y
== term
.bot
)
1340 tscrollup(term
.top
, 1);
1342 tmoveto(term
.c
.x
, term
.c
.y
+1);
1345 case 'E': /* NEL -- Next line */
1346 tnewline(1); /* always go to first col */
1349 case 'M': /* RI -- Reverse index */
1350 if(term
.c
.y
== term
.top
)
1351 tscrolldown(term
.top
, 1);
1353 tmoveto(term
.c
.x
, term
.c
.y
-1);
1356 case 'c': /* RIS -- Reset to inital state */
1360 case '=': /* DECPAM -- Application keypad */
1361 term
.mode
|= MODE_APPKEYPAD
;
1364 case '>': /* DECPNM -- Normal keypad */
1365 term
.mode
&= ~MODE_APPKEYPAD
;
1368 case '7': /* DECSC -- Save Cursor */
1369 tcursor(CURSOR_SAVE
);
1372 case '8': /* DECRC -- Restore Cursor */
1373 tcursor(CURSOR_LOAD
);
1377 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1378 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1388 tmoveto(term
.c
.x
-1, term
.c
.y
);
1391 tmoveto(0, term
.c
.y
);
1396 /* go to first col if the mode is set */
1397 tnewline(IS_SET(MODE_CRLF
));
1400 if(!(xw
.state
& WIN_FOCUSED
))
1405 term
.esc
= ESC_START
;
1408 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1409 tnewline(1); /* always go to first col */
1411 if(term
.c
.x
+1 < term
.col
)
1412 tmoveto(term
.c
.x
+1, term
.c
.y
);
1414 term
.c
.state
|= CURSOR_WRAPNEXT
;
1420 tresize(int col
, int row
) {
1422 int minrow
= MIN(row
, term
.row
);
1423 int mincol
= MIN(col
, term
.col
);
1424 int slide
= term
.c
.y
- row
+ 1;
1426 if(col
< 1 || row
< 1)
1429 /* free unneeded rows */
1432 /* slide screen to keep cursor where we expect it -
1433 * tscrollup would work here, but we can optimize to
1434 * memmove because we're freeing the earlier lines */
1435 for(/* i = 0 */; i
< slide
; i
++) {
1439 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1440 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1442 for(i
+= row
; i
< term
.row
; i
++) {
1447 /* resize to new height */
1448 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1449 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1451 /* resize each row to new width, zero-pad if needed */
1452 for(i
= 0; i
< minrow
; i
++) {
1453 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1454 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1455 for(x
= mincol
; x
< col
; x
++) {
1456 term
.line
[i
][x
].state
= 0;
1457 term
.alt
[i
][x
].state
= 0;
1461 /* allocate any new rows */
1462 for(/* i == minrow */; i
< row
; i
++) {
1463 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1464 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1467 /* update terminal size */
1468 term
.col
= col
, term
.row
= row
;
1469 /* make use of the LIMIT in tmoveto */
1470 tmoveto(term
.c
.x
, term
.c
.y
);
1471 /* reset scrolling region */
1472 tsetscroll(0, row
-1);
1477 xresize(int col
, int row
) {
1483 xw
.bufw
= MAX(1, col
* xw
.cw
);
1484 xw
.bufh
= MAX(1, row
* xw
.ch
);
1485 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1486 XCopyArea(xw
.dpy
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1487 XFreePixmap(xw
.dpy
, xw
.buf
);
1488 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1490 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0,
1491 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1492 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1493 XClearArea(xw
.dpy
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1494 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1497 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
,
1498 xw
.bufw
, xw
.bufh
-oldh
);
1499 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1500 XClearArea(xw
.dpy
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1501 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1510 unsigned long white
= WhitePixel(xw
.dpy
, xw
.scr
);
1512 for(i
= 0; i
< 16; i
++) {
1513 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1515 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1517 dc
.col
[i
] = color
.pixel
;
1520 /* same colors as xterm */
1521 for(r
= 0; r
< 6; r
++)
1522 for(g
= 0; g
< 6; g
++)
1523 for(b
= 0; b
< 6; b
++) {
1524 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1525 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1526 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1527 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1529 fprintf(stderr
, "Could not allocate color %d\n", i
);
1531 dc
.col
[i
] = color
.pixel
;
1535 for(r
= 0; r
< 24; r
++, i
++) {
1536 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1537 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1539 fprintf(stderr
, "Could not allocate color %d\n", i
);
1541 dc
.col
[i
] = color
.pixel
;
1546 xclear(int x1
, int y1
, int x2
, int y2
) {
1547 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1548 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1549 x1
* xw
.cw
, y1
* xw
.ch
,
1550 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1555 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1556 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1558 .flags
= PSize
| PResizeInc
| PBaseSize
,
1561 .height_inc
= xw
.ch
,
1563 .base_height
= 2*BORDER
,
1564 .base_width
= 2*BORDER
,
1566 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1570 xinitfont(char *fontstr
) {
1572 char *def
, **missing
;
1576 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1579 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1580 XFreeStringList(missing
);
1586 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1587 XFontStruct
**xfonts
;
1591 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1592 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1593 for(i
= 0; i
< n
; i
++) {
1594 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1595 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1596 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1597 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1603 initfonts(char *fontstr
, char *bfontstr
) {
1604 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1605 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1606 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1607 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1608 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1609 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1610 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1615 XSetWindowAttributes attrs
;
1619 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1620 die("Can't open display\n");
1621 xw
.scr
= XDefaultScreen(xw
.dpy
);
1624 initfonts(FONT
, BOLDFONT
);
1626 /* XXX: Assuming same size for bold font */
1627 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1628 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1631 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1634 /* window - default size */
1635 xw
.bufh
= term
.row
* xw
.ch
;
1636 xw
.bufw
= term
.col
* xw
.cw
;
1637 xw
.h
= xw
.bufh
+ 2*BORDER
;
1638 xw
.w
= xw
.bufw
+ 2*BORDER
;
1640 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1641 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1642 attrs
.bit_gravity
= NorthWestGravity
;
1643 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1644 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1645 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1646 | EnterWindowMask
| LeaveWindowMask
;
1647 attrs
.colormap
= xw
.cmap
;
1649 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1650 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1651 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1652 XDefaultVisual(xw
.dpy
, xw
.scr
),
1653 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1656 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1660 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1661 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1662 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1663 XNFocusWindow
, xw
.win
, NULL
);
1665 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1667 /* white cursor, black outline */
1668 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1669 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1670 XRecolorCursor(xw
.dpy
, cursor
,
1671 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1672 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1674 xembedatom
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1676 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1677 XMapWindow(xw
.dpy
, xw
.win
);
1683 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1684 unsigned long xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
], temp
;
1685 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1688 /* only switch default fg/bg if term is in RV mode */
1689 if(IS_SET(MODE_REVERSE
)) {
1690 if(base
.fg
== DefaultFG
)
1691 xfg
= dc
.col
[DefaultBG
];
1692 if(base
.bg
== DefaultBG
)
1693 xbg
= dc
.col
[DefaultFG
];
1696 if(base
.mode
& ATTR_REVERSE
)
1697 temp
= xfg
, xfg
= xbg
, xbg
= temp
;
1699 XSetBackground(xw
.dpy
, dc
.gc
, xbg
);
1700 XSetForeground(xw
.dpy
, dc
.gc
, xfg
);
1702 if(base
.mode
& ATTR_GFX
) {
1703 for(i
= 0; i
< bytelen
; i
++) {
1704 char c
= gfx
[(unsigned int)s
[i
] % 256];
1707 else if(s
[i
] > 0x5f)
1712 XmbDrawImageString(xw
.dpy
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.set
: dc
.font
.set
,
1713 dc
.gc
, winx
, winy
, s
, bytelen
);
1715 if(base
.mode
& ATTR_UNDERLINE
)
1716 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1721 static int oldx
= 0;
1722 static int oldy
= 0;
1724 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1726 LIMIT(oldx
, 0, term
.col
-1);
1727 LIMIT(oldy
, 0, term
.row
-1);
1729 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1730 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1732 /* remove the old cursor */
1733 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1734 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1735 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1737 xclear(oldx
, oldy
, oldx
, oldy
);
1739 /* draw the new one */
1740 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1742 if(IS_SET(MODE_REVERSE
))
1743 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
1744 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1745 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1751 drawregion(0, 0, term
.col
, term
.row
);
1755 drawregion(int x1
, int y1
, int x2
, int y2
) {
1756 int ic
, ib
, x
, y
, ox
, sl
;
1758 char buf
[DRAW_BUF_SIZ
];
1760 if(!(xw
.state
& WIN_VISIBLE
))
1763 xclear(x1
, y1
, x2
-1, y2
-1);
1764 for(y
= y1
; y
< y2
; y
++) {
1765 base
= term
.line
[y
][0];
1767 for(x
= x1
; x
< x2
; x
++) {
1768 new = term
.line
[y
][x
];
1769 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
1770 new.mode
^= ATTR_REVERSE
;
1771 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1772 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1773 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1776 if(new.state
& GLYPH_SET
) {
1781 sl
= utf8size(new.c
);
1782 memcpy(buf
+ib
, new.c
, sl
);
1788 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1791 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1795 expose(XEvent
*ev
) {
1796 XExposeEvent
*e
= &ev
->xexpose
;
1797 if(xw
.state
& WIN_REDRAW
) {
1799 xw
.state
&= ~WIN_REDRAW
;
1803 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1804 e
->width
, e
->height
, e
->x
, e
->y
);
1808 visibility(XEvent
*ev
) {
1809 XVisibilityEvent
*e
= &ev
->xvisibility
;
1810 if(e
->state
== VisibilityFullyObscured
)
1811 xw
.state
&= ~WIN_VISIBLE
;
1812 else if(!(xw
.state
& WIN_VISIBLE
))
1813 /* need a full redraw for next Expose, not just a buf copy */
1814 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1819 xw
.state
&= ~WIN_VISIBLE
;
1823 xseturgency(int add
) {
1824 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1825 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1826 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1832 if(ev
->type
== FocusIn
) {
1833 xw
.state
|= WIN_FOCUSED
;
1836 xw
.state
&= ~WIN_FOCUSED
;
1841 kmap(KeySym k
, unsigned int state
) {
1843 for(i
= 0; i
< LEN(key
); i
++)
1844 if(key
[i
].k
== k
&& (key
[i
].mask
== 0 || key
[i
].mask
& state
))
1845 return (char*)key
[i
].s
;
1850 kpress(XEvent
*ev
) {
1851 XKeyEvent
*e
= &ev
->xkey
;
1860 meta
= e
->state
& Mod1Mask
;
1861 shift
= e
->state
& ShiftMask
;
1862 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1864 /* 1. custom keys from config.h */
1865 if((customkey
= kmap(ksym
, e
->state
)))
1866 ttywrite(customkey
, strlen(customkey
));
1867 /* 2. hardcoded (overrides X lookup) */
1874 /* XXX: shift up/down doesn't work */
1875 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
1883 if(IS_SET(MODE_CRLF
))
1884 ttywrite("\r\n", 2);
1891 if(meta
&& len
== 1)
1892 ttywrite("\033", 1);
1900 cmessage(XEvent
*e
) {
1901 if (e
->xclient
.message_type
== xembedatom
&& e
->xclient
.format
== 32) {
1902 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1903 xw
.state
|= WIN_FOCUSED
;
1905 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1906 xw
.state
&= ~WIN_FOCUSED
;
1916 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1919 xw
.w
= e
->xconfigure
.width
;
1920 xw
.h
= e
->xconfigure
.height
;
1921 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
1922 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
1923 if(col
== term
.col
&& row
== term
.row
)
1925 if(tresize(col
, row
))
1927 ttyresize(col
, row
);
1935 int xfd
= XConnectionNumber(xw
.dpy
);
1939 FD_SET(cmdfd
, &rfd
);
1941 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1944 die("select failed: %s\n", SERRNO
);
1946 if(FD_ISSET(cmdfd
, &rfd
)) {
1950 while(XPending(xw
.dpy
)) {
1951 XNextEvent(xw
.dpy
, &ev
);
1952 if(XFilterEvent(&ev
, xw
.win
))
1954 if(handler
[ev
.type
])
1955 (handler
[ev
.type
])(&ev
);
1961 main(int argc
, char *argv
[]) {
1964 for(i
= 1; i
< argc
; i
++) {
1965 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1967 if(++i
< argc
) opt_title
= argv
[i
];
1970 if(++i
< argc
) opt_class
= argv
[i
];
1973 if(++i
< argc
) opt_embed
= argv
[i
];
1976 /* eat every remaining arguments */
1977 if(++i
< argc
) opt_cmd
= &argv
[i
];
1986 setlocale(LC_CTYPE
, "");