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 st engineers\n" \
38 "usage: st [-t title] [-c class] [-v] [-e cmd]\n"
41 #define ESC_TITLE_SIZ 256
42 #define ESC_BUF_SIZ 256
43 #define ESC_ARG_SIZ 16
44 #define DRAW_BUF_SIZ 1024
47 #define SERRNO strerror(errno)
48 #define MIN(a, b) ((a) < (b) ? (a) : (b))
49 #define MAX(a, b) ((a) < (b) ? (b) : (a))
50 #define LEN(a) (sizeof(a) / sizeof(a[0]))
51 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
52 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
53 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
54 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
55 #define IS_SET(flag) (term.mode & (flag))
56 #define TIMEDIFFERENCE(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
58 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
59 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
60 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
61 CURSOR_SAVE
, CURSOR_LOAD
};
62 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
63 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
64 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
66 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
67 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
70 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
73 char c
[UTF_SIZ
]; /* character code */
74 char mode
; /* attribute flags */
75 int fg
; /* foreground */
76 int bg
; /* background */
77 char state
; /* state flags */
83 Glyph attr
; /* current char attributes */
89 /* CSI Escape sequence structs */
90 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
92 char buf
[ESC_BUF_SIZ
]; /* raw string */
93 int len
; /* raw string length */
96 int narg
; /* nb of args */
100 /* Internal representation of the screen */
102 int row
; /* nb row */
103 int col
; /* nb col */
104 Line
* line
; /* screen */
105 Line
* alt
; /* alternate screen */
106 TCursor c
; /* cursor */
107 int top
; /* top scroll limit */
108 int bot
; /* bottom scroll limit */
109 int mode
; /* terminal mode flags */
110 int esc
; /* escape state flags */
111 char title
[ESC_TITLE_SIZ
];
115 /* Purely graphic info */
124 int w
; /* window width */
125 int h
; /* window height */
126 int bufw
; /* pixmap width */
127 int bufh
; /* pixmap height */
128 int ch
; /* char height */
129 int cw
; /* char width */
130 char state
; /* focus, redraw, visible */
139 /* Drawing Context */
141 unsigned long col
[256];
152 /* TODO: use better name for vars... */
157 struct {int x
, y
;} b
, e
;
160 struct timeval tclick1
;
161 struct timeval tclick2
;
166 static void die(const char*, ...);
167 static void draw(void);
168 static void drawregion(int, int, int, int);
169 static void execsh(void);
170 static void sigchld(int);
171 static void run(void);
173 static void csidump(void);
174 static void csihandle(void);
175 static void csiparse(void);
176 static void csireset(void);
178 static void tclearregion(int, int, int, int);
179 static void tcursor(int);
180 static void tdeletechar(int);
181 static void tdeleteline(int);
182 static void tinsertblank(int);
183 static void tinsertblankline(int);
184 static void tmoveto(int, int);
185 static void tnew(int, int);
186 static void tnewline(int);
187 static void tputtab(void);
188 static void tputc(char*);
189 static void treset(void);
190 static int tresize(int, int);
191 static void tscrollup(int, int);
192 static void tscrolldown(int, int);
193 static void tsetattr(int*, int);
194 static void tsetchar(char*);
195 static void tsetscroll(int, int);
196 static void tswapscreen(void);
198 static void ttynew(void);
199 static void ttyread(void);
200 static void ttyresize(int, int);
201 static void ttywrite(const char *, size_t);
203 static void xdraws(char *, Glyph
, int, int, int, int);
204 static void xhints(void);
205 static void xclear(int, int, int, int);
206 static void xdrawcursor(void);
207 static void xinit(void);
208 static void xloadcols(void);
209 static void xseturgency(int);
210 static void xsetsel(char*);
211 static void xresize(int, int);
213 static void expose(XEvent
*);
214 static void visibility(XEvent
*);
215 static void unmap(XEvent
*);
216 static char* kmap(KeySym
, unsigned int state
);
217 static void kpress(XEvent
*);
218 static void resize(XEvent
*);
219 static void focus(XEvent
*);
220 static void brelease(XEvent
*);
221 static void bpress(XEvent
*);
222 static void bmotion(XEvent
*);
223 static void selnotify(XEvent
*);
224 static void selrequest(XEvent
*);
226 static void selinit(void);
227 static inline int selected(int, int);
228 static void selcopy(void);
229 static void selpaste();
231 static int utf8decode(char *, long *);
232 static int utf8encode(long *, char *);
233 static int utf8size(char *);
234 static int isfullutf8(char *, int);
236 static void (*handler
[LASTEvent
])(XEvent
*) = {
238 [ConfigureNotify
] = resize
,
239 [VisibilityNotify
] = visibility
,
240 [UnmapNotify
] = unmap
,
244 [MotionNotify
] = bmotion
,
245 [ButtonPress
] = bpress
,
246 [ButtonRelease
] = brelease
,
247 [SelectionNotify
] = selnotify
,
248 [SelectionRequest
] = selrequest
,
255 static CSIEscape escseq
;
258 static Selection sel
;
259 static char **opt_cmd
= NULL
;
260 static char *opt_title
= NULL
;
261 static char *opt_class
= NULL
;
264 utf8decode(char *s
, long *u
) {
270 if(~c
&B7
) { /* 0xxxxxxx */
273 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
274 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
276 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
277 *u
= c
&(B3
|B2
|B1
|B0
);
279 } else if((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
284 for(i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
286 if((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
289 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
291 if((n
== 1 && *u
< 0x80) ||
292 (n
== 2 && *u
< 0x800) ||
293 (n
== 3 && *u
< 0x10000) ||
294 (*u
>= 0xD800 && *u
<= 0xDFFF))
303 utf8encode(long *u
, char *s
) {
308 sp
= (unsigned char*) s
;
311 *sp
= uc
; /* 0xxxxxxx */
313 } else if(*u
< 0x800) {
314 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
316 } else if(uc
< 0x10000) {
317 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
319 } else if(uc
<= 0x10FFFF) {
320 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
325 for(i
=n
,++sp
; i
>0; --i
,++sp
)
326 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
336 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
337 UTF-8 otherwise return 0 */
339 isfullutf8(char *s
, int b
) {
340 unsigned char *c1
, *c2
, *c3
;
342 c1
= (unsigned char *) s
;
343 c2
= (unsigned char *) ++s
;
344 c3
= (unsigned char *) ++s
;
347 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
349 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
351 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
353 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
355 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
356 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
364 unsigned char c
= *s
;
368 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
370 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
378 sel
.tclick1
.tv_sec
= 0;
379 sel
.tclick1
.tv_usec
= 0;
383 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
384 if(sel
.xtarget
== None
)
385 sel
.xtarget
= XA_STRING
;
389 selected(int x
, int y
) {
390 if(sel
.ey
== y
&& sel
.by
== y
) {
391 int bx
= MIN(sel
.bx
, sel
.ex
);
392 int ex
= MAX(sel
.bx
, sel
.ex
);
393 return BETWEEN(x
, bx
, ex
);
395 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
396 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
400 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
402 *b
= e
->xbutton
.button
;
404 *x
= (e
->xbutton
.x
- BORDER
)/xw
.cw
;
405 *y
= (e
->xbutton
.y
- BORDER
)/xw
.ch
;
406 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
407 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
408 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
409 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
415 sel
.ex
= sel
.bx
= (e
->xbutton
.x
- BORDER
)/xw
.cw
;
416 sel
.ey
= sel
.by
= (e
->xbutton
.y
- BORDER
)/xw
.ch
;
422 int x
, y
, sz
, sl
, ls
= 0;
427 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
428 ptr
= str
= malloc(sz
);
429 for(y
= 0; y
< term
.row
; y
++) {
430 for(x
= 0; x
< term
.col
; x
++)
431 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
432 sl
= utf8size(term
.line
[y
][x
].c
);
433 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
436 if(ls
&& y
< sel
.e
.y
)
445 selnotify(XEvent
*e
) {
446 unsigned long nitems
;
447 unsigned long ofs
, rem
;
454 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
455 False
, AnyPropertyType
, &type
, &format
,
456 &nitems
, &rem
, &data
)) {
457 fprintf(stderr
, "Clipboard allocation failed\n");
460 ttywrite((const char *) data
, nitems
* format
/ 8);
462 /* number of 32-bit chunks returned */
463 ofs
+= nitems
* format
/ 32;
469 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
473 selrequest(XEvent
*e
) {
474 XSelectionRequestEvent
*xsre
;
478 xsre
= (XSelectionRequestEvent
*) e
;
479 xev
.type
= SelectionNotify
;
480 xev
.requestor
= xsre
->requestor
;
481 xev
.selection
= xsre
->selection
;
482 xev
.target
= xsre
->target
;
483 xev
.time
= xsre
->time
;
487 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
488 if(xsre
->target
== xa_targets
) {
489 /* respond with the supported type */
490 Atom string
= sel
.xtarget
;
491 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
492 XA_ATOM
, 32, PropModeReplace
,
493 (unsigned char *) &string
, 1);
494 xev
.property
= xsre
->property
;
495 } else if(xsre
->target
== sel
.xtarget
) {
496 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
497 xsre
->target
, 8, PropModeReplace
,
498 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
499 xev
.property
= xsre
->property
;
502 /* all done, send a notification to the listener */
503 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
504 fprintf(stderr
, "Error sending SelectionNotify event\n");
509 /* register the selection for both the clipboard and the primary */
515 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
517 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
518 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
524 brelease(XEvent
*e
) {
528 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
529 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
535 gettimeofday(&now
, NULL
);
537 if(TIMEDIFFERENCE(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
538 /* triple click on the line */
539 sel
.b
.x
= sel
.bx
= 0;
540 sel
.e
.x
= sel
.ex
= term
.col
;
541 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
543 } else if(TIMEDIFFERENCE(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
544 /* double click to select word */
546 while(term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
547 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
549 while(term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
550 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
552 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
558 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
559 gettimeofday(&sel
.tclick1
, NULL
);
568 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
570 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
571 int starty
= MIN(oldey
, sel
.ey
);
572 int endy
= MAX(oldey
, sel
.ey
);
573 drawregion(0, (starty
> 0 ? starty
: 0), term
.col
, (sel
.ey
< term
.row
? endy
+1 : term
.row
));
579 die(const char *errstr
, ...) {
582 va_start(ap
, errstr
);
583 vfprintf(stderr
, errstr
, ap
);
591 char *envshell
= getenv("SHELL");
593 DEFAULT(envshell
, "sh");
594 putenv("TERM="TNAME
);
595 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
596 execvp(args
[0], args
);
603 if(waitpid(pid
, &stat
, 0) < 0)
604 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
606 exit(WEXITSTATUS(stat
));
615 /* seems to work fine on linux, openbsd and freebsd */
616 struct winsize w
= {term
.row
, term
.col
, 0, 0};
617 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
618 die("openpty failed: %s\n", SERRNO
);
620 switch(pid
= fork()) {
622 die("fork failed\n");
625 setsid(); /* create a new process group */
626 dup2(s
, STDIN_FILENO
);
627 dup2(s
, STDOUT_FILENO
);
628 dup2(s
, STDERR_FILENO
);
629 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
630 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
638 signal(SIGCHLD
, sigchld
);
645 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
647 fprintf(stderr
, "\n");
652 static char buf
[BUFSIZ
];
653 static int buflen
= 0;
656 int charsize
; /* size of utf8 char in bytes */
660 /* append read bytes to unprocessed bytes */
661 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
662 die("Couldn't read from shell: %s\n", SERRNO
);
664 /* process every complete utf8 char */
667 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
668 charsize
= utf8decode(ptr
, &utf8c
);
669 utf8encode(&utf8c
, s
);
675 /* keep any uncomplete utf8 char for the next call */
676 memmove(buf
, ptr
, buflen
);
680 ttywrite(const char *s
, size_t n
) {
681 if(write(cmdfd
, s
, n
) == -1)
682 die("write error on tty: %s\n", SERRNO
);
686 ttyresize(int x
, int y
) {
691 w
.ws_xpixel
= w
.ws_ypixel
= 0;
692 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
693 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
700 if(mode
== CURSOR_SAVE
)
702 else if(mode
== CURSOR_LOAD
)
703 term
.c
= c
, tmoveto(c
.x
, c
.y
);
712 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
714 term
.top
= 0, term
.bot
= term
.row
- 1;
715 term
.mode
= MODE_WRAP
;
716 tclearregion(0, 0, term
.col
-1, term
.row
-1);
720 tnew(int col
, int row
) {
721 /* set screen size */
722 term
.row
= row
, term
.col
= col
;
723 term
.line
= malloc(term
.row
* sizeof(Line
));
724 term
.alt
= malloc(term
.row
* sizeof(Line
));
725 for(row
= 0 ; row
< term
.row
; row
++) {
726 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
727 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
735 Line
* tmp
= term
.line
;
736 term
.line
= term
.alt
;
738 term
.mode
^= MODE_ALTSCREEN
;
742 tscrolldown(int orig
, int n
) {
746 LIMIT(n
, 0, term
.bot
-orig
+1);
748 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
750 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
752 term
.line
[i
] = term
.line
[i
-n
];
753 term
.line
[i
-n
] = temp
;
758 tscrollup(int orig
, int n
) {
761 LIMIT(n
, 0, term
.bot
-orig
+1);
763 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
765 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
767 term
.line
[i
] = term
.line
[i
+n
];
768 term
.line
[i
+n
] = temp
;
773 tnewline(int first_col
) {
776 tscrollup(term
.top
, 1);
779 tmoveto(first_col
? 0 : term
.c
.x
, y
);
785 char *p
= escseq
.buf
;
789 escseq
.priv
= 1, p
++;
791 while(p
< escseq
.buf
+escseq
.len
) {
793 escseq
.arg
[escseq
.narg
] *= 10;
794 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
796 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
807 tmoveto(int x
, int y
) {
808 LIMIT(x
, 0, term
.col
-1);
809 LIMIT(y
, 0, term
.row
-1);
810 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
817 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
818 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
819 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
823 tclearregion(int x1
, int y1
, int x2
, int y2
) {
827 temp
= x1
, x1
= x2
, x2
= temp
;
829 temp
= y1
, y1
= y2
, y2
= temp
;
831 LIMIT(x1
, 0, term
.col
-1);
832 LIMIT(x2
, 0, term
.col
-1);
833 LIMIT(y1
, 0, term
.row
-1);
834 LIMIT(y2
, 0, term
.row
-1);
836 for(y
= y1
; y
<= y2
; y
++)
837 for(x
= x1
; x
<= x2
; x
++)
838 term
.line
[y
][x
].state
= 0;
843 int src
= term
.c
.x
+ n
;
845 int size
= term
.col
- src
;
847 if(src
>= term
.col
) {
848 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
851 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
852 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
856 tinsertblank(int n
) {
859 int size
= term
.col
- dst
;
861 if(dst
>= term
.col
) {
862 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
865 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
866 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
870 tinsertblankline(int n
) {
871 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
874 tscrolldown(term
.c
.y
, n
);
879 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
882 tscrollup(term
.c
.y
, n
);
886 tsetattr(int *attr
, int l
) {
889 for(i
= 0; i
< l
; i
++) {
892 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
893 term
.c
.attr
.fg
= DefaultFG
;
894 term
.c
.attr
.bg
= DefaultBG
;
897 term
.c
.attr
.mode
|= ATTR_BOLD
;
900 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
903 term
.c
.attr
.mode
|= ATTR_REVERSE
;
906 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
909 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
912 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
915 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
917 if(BETWEEN(attr
[i
], 0, 255))
918 term
.c
.attr
.fg
= attr
[i
];
920 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
923 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
926 term
.c
.attr
.fg
= DefaultFG
;
929 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
931 if(BETWEEN(attr
[i
], 0, 255))
932 term
.c
.attr
.bg
= attr
[i
];
934 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
937 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
940 term
.c
.attr
.bg
= DefaultBG
;
943 if(BETWEEN(attr
[i
], 30, 37))
944 term
.c
.attr
.fg
= attr
[i
] - 30;
945 else if(BETWEEN(attr
[i
], 40, 47))
946 term
.c
.attr
.bg
= attr
[i
] - 40;
947 else if(BETWEEN(attr
[i
], 90, 97))
948 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
949 else if(BETWEEN(attr
[i
], 100, 107))
950 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
952 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
960 tsetscroll(int t
, int b
) {
963 LIMIT(t
, 0, term
.row
-1);
964 LIMIT(b
, 0, term
.row
-1);
976 switch(escseq
.mode
) {
979 fprintf(stderr
, "erresc: unknown csi ");
983 case '@': /* ICH -- Insert <n> blank char */
984 DEFAULT(escseq
.arg
[0], 1);
985 tinsertblank(escseq
.arg
[0]);
987 case 'A': /* CUU -- Cursor <n> Up */
989 DEFAULT(escseq
.arg
[0], 1);
990 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
992 case 'B': /* CUD -- Cursor <n> Down */
993 DEFAULT(escseq
.arg
[0], 1);
994 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
996 case 'C': /* CUF -- Cursor <n> Forward */
998 DEFAULT(escseq
.arg
[0], 1);
999 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
1001 case 'D': /* CUB -- Cursor <n> Backward */
1002 DEFAULT(escseq
.arg
[0], 1);
1003 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
1005 case 'E': /* CNL -- Cursor <n> Down and first col */
1006 DEFAULT(escseq
.arg
[0], 1);
1007 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
1009 case 'F': /* CPL -- Cursor <n> Up and first col */
1010 DEFAULT(escseq
.arg
[0], 1);
1011 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
1013 case 'G': /* CHA -- Move to <col> */
1014 case '`': /* XXX: HPA -- same? */
1015 DEFAULT(escseq
.arg
[0], 1);
1016 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
1018 case 'H': /* CUP -- Move to <row> <col> */
1019 case 'f': /* XXX: HVP -- same? */
1020 DEFAULT(escseq
.arg
[0], 1);
1021 DEFAULT(escseq
.arg
[1], 1);
1022 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
1024 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1025 case 'J': /* ED -- Clear screen */
1026 switch(escseq
.arg
[0]) {
1028 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1029 if(term
.c
.y
< term
.row
-1)
1030 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1034 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1035 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1038 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1044 case 'K': /* EL -- Clear line */
1045 switch(escseq
.arg
[0]) {
1047 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1050 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1053 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1057 case 'S': /* SU -- Scroll <n> line up */
1058 DEFAULT(escseq
.arg
[0], 1);
1059 tscrollup(term
.top
, escseq
.arg
[0]);
1061 case 'T': /* SD -- Scroll <n> line down */
1062 DEFAULT(escseq
.arg
[0], 1);
1063 tscrolldown(term
.top
, escseq
.arg
[0]);
1065 case 'L': /* IL -- Insert <n> blank lines */
1066 DEFAULT(escseq
.arg
[0], 1);
1067 tinsertblankline(escseq
.arg
[0]);
1069 case 'l': /* RM -- Reset Mode */
1071 switch(escseq
.arg
[0]) {
1073 term
.mode
&= ~MODE_APPKEYPAD
;
1075 case 5: /* TODO: DECSCNM -- Remove reverse video */
1078 term
.mode
&= ~MODE_WRAP
;
1080 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1083 term
.mode
&= ~MODE_CRLF
;
1086 term
.c
.state
|= CURSOR_HIDE
;
1088 case 1049: /* = 1047 and 1048 */
1090 if(IS_SET(MODE_ALTSCREEN
)) {
1091 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1094 if(escseq
.arg
[0] == 1047)
1097 tcursor(CURSOR_LOAD
);
1103 switch(escseq
.arg
[0]) {
1105 term
.mode
&= ~MODE_INSERT
;
1112 case 'M': /* DL -- Delete <n> lines */
1113 DEFAULT(escseq
.arg
[0], 1);
1114 tdeleteline(escseq
.arg
[0]);
1116 case 'X': /* ECH -- Erase <n> char */
1117 DEFAULT(escseq
.arg
[0], 1);
1118 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1120 case 'P': /* DCH -- Delete <n> char */
1121 DEFAULT(escseq
.arg
[0], 1);
1122 tdeletechar(escseq
.arg
[0]);
1124 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1125 case 'd': /* VPA -- Move to <row> */
1126 DEFAULT(escseq
.arg
[0], 1);
1127 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1129 case 'h': /* SM -- Set terminal mode */
1131 switch(escseq
.arg
[0]) {
1133 term
.mode
|= MODE_APPKEYPAD
;
1135 case 5: /* DECSCNM -- Reverve video */
1136 /* TODO: set REVERSE on the whole screen (f) */
1139 term
.mode
|= MODE_WRAP
;
1142 term
.mode
|= MODE_CRLF
;
1144 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1145 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1146 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1149 term
.c
.state
&= ~CURSOR_HIDE
;
1151 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] == 1047)
1160 tcursor(CURSOR_SAVE
);
1162 default: goto unknown
;
1165 switch(escseq
.arg
[0]) {
1167 term
.mode
|= MODE_INSERT
;
1169 default: goto unknown
;
1173 case 'm': /* SGR -- Terminal attribute (color) */
1174 tsetattr(escseq
.arg
, escseq
.narg
);
1176 case 'r': /* DECSTBM -- Set Scrolling Region */
1180 DEFAULT(escseq
.arg
[0], 1);
1181 DEFAULT(escseq
.arg
[1], term
.row
);
1182 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1186 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1187 tcursor(CURSOR_SAVE
);
1189 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1190 tcursor(CURSOR_LOAD
);
1198 printf("ESC [ %s", escseq
.priv
? "? " : "");
1200 for(i
= 0; i
< escseq
.narg
; i
++)
1201 printf("%d ", escseq
.arg
[i
]);
1203 putchar(escseq
.mode
);
1209 memset(&escseq
, 0, sizeof(escseq
));
1214 int space
= TAB
- term
.c
.x
% TAB
;
1215 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1221 if(term
.esc
& ESC_START
) {
1222 if(term
.esc
& ESC_CSI
) {
1223 escseq
.buf
[escseq
.len
++] = ascii
;
1224 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1226 csiparse(), csihandle();
1228 /* TODO: handle other OSC */
1229 } else if(term
.esc
& ESC_OSC
) {
1232 term
.esc
= ESC_START
| ESC_TITLE
;
1234 } else if(term
.esc
& ESC_TITLE
) {
1235 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1237 term
.title
[term
.titlelen
] = '\0';
1238 XStoreName(xw
.dpy
, xw
.win
, term
.title
);
1240 term
.title
[term
.titlelen
++] = ascii
;
1242 } else if(term
.esc
& ESC_ALTCHARSET
) {
1244 case '0': /* Line drawing crap */
1245 term
.c
.attr
.mode
|= ATTR_GFX
;
1247 case 'B': /* Back to regular text */
1248 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1251 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1257 term
.esc
|= ESC_CSI
;
1260 term
.esc
|= ESC_OSC
;
1263 term
.esc
|= ESC_ALTCHARSET
;
1265 case 'D': /* IND -- Linefeed */
1266 if(term
.c
.y
== term
.bot
)
1267 tscrollup(term
.top
, 1);
1269 tmoveto(term
.c
.x
, term
.c
.y
+1);
1272 case 'E': /* NEL -- Next line */
1273 tnewline(1); /* always go to first col */
1276 case 'M': /* RI -- Reverse index */
1277 if(term
.c
.y
== term
.top
)
1278 tscrolldown(term
.top
, 1);
1280 tmoveto(term
.c
.x
, term
.c
.y
-1);
1283 case 'c': /* RIS -- Reset to inital state */
1287 case '=': /* DECPAM -- Application keypad */
1288 term
.mode
|= MODE_APPKEYPAD
;
1291 case '>': /* DECPNM -- Normal keypad */
1292 term
.mode
&= ~MODE_APPKEYPAD
;
1295 case '7': /* DECSC -- Save Cursor */
1296 tcursor(CURSOR_SAVE
);
1299 case '8': /* DECRC -- Restore Cursor */
1300 tcursor(CURSOR_LOAD
);
1304 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1305 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1315 tmoveto(term
.c
.x
-1, term
.c
.y
);
1318 tmoveto(0, term
.c
.y
);
1323 /* go to first col if the mode is set */
1324 tnewline(IS_SET(MODE_CRLF
));
1327 if(!(xw
.state
& WIN_FOCUSED
))
1332 term
.esc
= ESC_START
;
1335 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1336 tnewline(1); /* always go to first col */
1338 if(term
.c
.x
+1 < term
.col
)
1339 tmoveto(term
.c
.x
+1, term
.c
.y
);
1341 term
.c
.state
|= CURSOR_WRAPNEXT
;
1347 tresize(int col
, int row
) {
1349 int minrow
= MIN(row
, term
.row
);
1350 int mincol
= MIN(col
, term
.col
);
1351 int slide
= term
.c
.y
- row
+ 1;
1353 if(col
< 1 || row
< 1)
1356 /* free unneeded rows */
1359 /* slide screen to keep cursor where we expect it -
1360 * tscrollup would work here, but we can optimize to
1361 * memmove because we're freeing the earlier lines */
1362 for(/* i = 0 */; i
< slide
; i
++) {
1366 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1367 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1369 for(i
+= row
; i
< term
.row
; i
++) {
1374 /* resize to new height */
1375 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1376 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1378 /* resize each row to new width, zero-pad if needed */
1379 for(i
= 0; i
< minrow
; i
++) {
1380 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1381 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1382 for(x
= mincol
; x
< col
; x
++) {
1383 term
.line
[i
][x
].state
= 0;
1384 term
.alt
[i
][x
].state
= 0;
1388 /* allocate any new rows */
1389 for(/* i == minrow */; i
< row
; i
++) {
1390 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1391 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1394 /* update terminal size */
1395 term
.col
= col
, term
.row
= row
;
1396 /* make use of the LIMIT in tmoveto */
1397 tmoveto(term
.c
.x
, term
.c
.y
);
1398 /* reset scrolling region */
1399 tsetscroll(0, row
-1);
1404 xresize(int col
, int row
) {
1410 xw
.bufw
= MAX(1, col
* xw
.cw
);
1411 xw
.bufh
= MAX(1, row
* xw
.ch
);
1412 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1413 XCopyArea(xw
.dpy
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1414 XFreePixmap(xw
.dpy
, xw
.buf
);
1415 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1417 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0,
1418 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1419 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1420 XClearArea(xw
.dpy
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1421 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1424 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
,
1425 xw
.bufw
, xw
.bufh
-oldh
);
1426 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1427 XClearArea(xw
.dpy
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1428 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1437 unsigned long white
= WhitePixel(xw
.dpy
, xw
.scr
);
1439 for(i
= 0; i
< 16; i
++) {
1440 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1442 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1444 dc
.col
[i
] = color
.pixel
;
1447 /* same colors as xterm */
1448 for(r
= 0; r
< 6; r
++)
1449 for(g
= 0; g
< 6; g
++)
1450 for(b
= 0; b
< 6; b
++) {
1451 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1452 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1453 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1454 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1456 fprintf(stderr
, "Could not allocate color %d\n", i
);
1458 dc
.col
[i
] = color
.pixel
;
1462 for(r
= 0; r
< 24; r
++, i
++) {
1463 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1464 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1466 fprintf(stderr
, "Could not allocate color %d\n", i
);
1468 dc
.col
[i
] = color
.pixel
;
1473 xclear(int x1
, int y1
, int x2
, int y2
) {
1474 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1475 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1476 x1
* xw
.cw
, y1
* xw
.ch
,
1477 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1482 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1483 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1485 .flags
= PSize
| PResizeInc
| PBaseSize
,
1488 .height_inc
= xw
.ch
,
1490 .base_height
= 2*BORDER
,
1491 .base_width
= 2*BORDER
,
1493 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1497 xinitfont(char *fontstr
) {
1499 char *def
, **missing
;
1503 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1506 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1507 XFreeStringList(missing
);
1513 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1514 XFontStruct
**xfonts
;
1518 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1519 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1520 for(i
= 0; i
< n
; i
++) {
1521 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1522 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1523 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1524 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1530 initfonts(char *fontstr
, char *bfontstr
) {
1531 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1532 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1533 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1534 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1535 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1536 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1537 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1542 XSetWindowAttributes attrs
;
1545 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1546 die("Can't open display\n");
1547 xw
.scr
= XDefaultScreen(xw
.dpy
);
1550 initfonts(FONT
, BOLDFONT
);
1552 /* XXX: Assuming same size for bold font */
1553 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1554 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1557 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1560 /* window - default size */
1561 xw
.bufh
= 24 * xw
.ch
;
1562 xw
.bufw
= 80 * xw
.cw
;
1563 xw
.h
= xw
.bufh
+ 2*BORDER
;
1564 xw
.w
= xw
.bufw
+ 2*BORDER
;
1566 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1567 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1568 attrs
.bit_gravity
= NorthWestGravity
;
1569 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1570 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1571 | PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1572 attrs
.colormap
= xw
.cmap
;
1574 xw
.win
= XCreateWindow(xw
.dpy
, XRootWindow(xw
.dpy
, xw
.scr
), 0, 0,
1575 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1576 XDefaultVisual(xw
.dpy
, xw
.scr
),
1577 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1580 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1584 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1585 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1586 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1587 XNFocusWindow
, xw
.win
, NULL
);
1589 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1591 /* white cursor, black outline */
1592 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1593 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1594 XRecolorCursor(xw
.dpy
, cursor
,
1595 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1596 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1598 XMapWindow(xw
.dpy
, xw
.win
);
1600 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1605 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1606 unsigned long xfg
, xbg
;
1607 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1610 if(base
.mode
& ATTR_REVERSE
)
1611 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1613 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1615 XSetBackground(xw
.dpy
, dc
.gc
, xbg
);
1616 XSetForeground(xw
.dpy
, dc
.gc
, xfg
);
1618 if(base
.mode
& ATTR_GFX
) {
1619 for(i
= 0; i
< bytelen
; i
++) {
1620 char c
= gfx
[(unsigned int)s
[i
] % 256];
1623 else if(s
[i
] > 0x5f)
1628 XmbDrawImageString(xw
.dpy
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.set
: dc
.font
.set
,
1629 dc
.gc
, winx
, winy
, s
, bytelen
);
1631 if(base
.mode
& ATTR_UNDERLINE
)
1632 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1637 static int oldx
= 0;
1638 static int oldy
= 0;
1640 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1642 LIMIT(oldx
, 0, term
.col
-1);
1643 LIMIT(oldy
, 0, term
.row
-1);
1645 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1646 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1648 /* remove the old cursor */
1649 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1650 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1651 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1653 xclear(oldx
, oldy
, oldx
, oldy
);
1655 /* draw the new one */
1656 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1658 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1659 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1665 drawregion(0, 0, term
.col
, term
.row
);
1669 drawregion(int x1
, int y1
, int x2
, int y2
) {
1670 int ic
, ib
, x
, y
, ox
, sl
;
1672 char buf
[DRAW_BUF_SIZ
];
1674 if(!(xw
.state
& WIN_VISIBLE
))
1677 xclear(x1
, y1
, x2
-1, y2
-1);
1678 for(y
= y1
; y
< y2
; y
++) {
1679 base
= term
.line
[y
][0];
1681 for(x
= x1
; x
< x2
; x
++) {
1682 new = term
.line
[y
][x
];
1683 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
1684 new.mode
^= ATTR_REVERSE
;
1685 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1686 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1687 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1690 if(new.state
& GLYPH_SET
) {
1695 sl
= utf8size(new.c
);
1696 memcpy(buf
+ib
, new.c
, sl
);
1702 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1705 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1709 expose(XEvent
*ev
) {
1710 XExposeEvent
*e
= &ev
->xexpose
;
1711 if(xw
.state
& WIN_REDRAW
) {
1713 xw
.state
&= ~WIN_REDRAW
;
1717 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1718 e
->width
, e
->height
, e
->x
, e
->y
);
1722 visibility(XEvent
*ev
) {
1723 XVisibilityEvent
*e
= &ev
->xvisibility
;
1724 if(e
->state
== VisibilityFullyObscured
)
1725 xw
.state
&= ~WIN_VISIBLE
;
1726 else if(!(xw
.state
& WIN_VISIBLE
))
1727 /* need a full redraw for next Expose, not just a buf copy */
1728 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1733 xw
.state
&= ~WIN_VISIBLE
;
1737 xseturgency(int add
) {
1738 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1739 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1740 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1746 if(ev
->type
== FocusIn
) {
1747 xw
.state
|= WIN_FOCUSED
;
1750 xw
.state
&= ~WIN_FOCUSED
;
1755 kmap(KeySym k
, unsigned int state
) {
1757 for(i
= 0; i
< LEN(key
); i
++)
1758 if(key
[i
].k
== k
&& (key
[i
].mask
== 0 || key
[i
].mask
& state
))
1759 return (char*)key
[i
].s
;
1764 kpress(XEvent
*ev
) {
1765 XKeyEvent
*e
= &ev
->xkey
;
1774 meta
= e
->state
& Mod1Mask
;
1775 shift
= e
->state
& ShiftMask
;
1776 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1778 /* 1. custom keys from config.h */
1779 if((customkey
= kmap(ksym
, e
->state
)))
1780 ttywrite(customkey
, strlen(customkey
));
1781 /* 2. hardcoded (overrides X lookup) */
1788 /* XXX: shift up/down doesn't work */
1789 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
1797 if(IS_SET(MODE_CRLF
))
1798 ttywrite("\r\n", 2);
1805 if(meta
&& len
== 1)
1806 ttywrite("\033", 1);
1817 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1820 xw
.w
= e
->xconfigure
.width
;
1821 xw
.h
= e
->xconfigure
.height
;
1822 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
1823 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
1824 if(col
== term
.col
&& row
== term
.row
)
1826 if(tresize(col
, row
))
1828 ttyresize(col
, row
);
1836 int xfd
= XConnectionNumber(xw
.dpy
);
1840 FD_SET(cmdfd
, &rfd
);
1842 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1845 die("select failed: %s\n", SERRNO
);
1847 if(FD_ISSET(cmdfd
, &rfd
)) {
1851 while(XPending(xw
.dpy
)) {
1852 XNextEvent(xw
.dpy
, &ev
);
1853 if(XFilterEvent(&ev
, xw
.win
))
1855 if(handler
[ev
.type
])
1856 (handler
[ev
.type
])(&ev
);
1862 main(int argc
, char *argv
[]) {
1865 for(i
= 1; i
< argc
; i
++) {
1866 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1868 if(++i
< argc
) opt_title
= argv
[i
];
1871 if(++i
< argc
) opt_class
= argv
[i
];
1874 if(++i
< argc
) opt_cmd
= &argv
[i
];
1880 /* -e eats every remaining arguments */
1884 setlocale(LC_CTYPE
, "");