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 char* dirty
; /* dirtyness of lines */
115 TCursor c
; /* cursor */
116 int top
; /* top scroll limit */
117 int bot
; /* bottom scroll limit */
118 int mode
; /* terminal mode flags */
119 int esc
; /* escape state flags */
120 char title
[ESC_TITLE_SIZ
];
124 /* Purely graphic info */
134 int w
; /* window width */
135 int h
; /* window height */
136 int bufw
; /* pixmap width */
137 int bufh
; /* pixmap height */
138 int ch
; /* char height */
139 int cw
; /* char width */
140 char state
; /* focus, redraw, visible */
149 /* Drawing Context */
151 unsigned long col
[256];
162 /* TODO: use better name for vars... */
167 struct {int x
, y
;} b
, e
;
170 struct timeval tclick1
;
171 struct timeval tclick2
;
176 static void die(const char*, ...);
177 static void draw(void);
178 static void drawregion(int, int, int, int);
179 static void execsh(void);
180 static void sigchld(int);
181 static void run(void);
183 static void csidump(void);
184 static void csihandle(void);
185 static void csiparse(void);
186 static void csireset(void);
188 static void tclearregion(int, int, int, int);
189 static void tcursor(int);
190 static void tdeletechar(int);
191 static void tdeleteline(int);
192 static void tinsertblank(int);
193 static void tinsertblankline(int);
194 static void tmoveto(int, int);
195 static void tnew(int, int);
196 static void tnewline(int);
197 static void tputtab(void);
198 static void tputc(char*);
199 static void treset(void);
200 static int tresize(int, int);
201 static void tscrollup(int, int);
202 static void tscrolldown(int, int);
203 static void tsetattr(int*, int);
204 static void tsetchar(char*);
205 static void tsetscroll(int, int);
206 static void tswapscreen(void);
207 static void tfulldirt(void);
209 static void ttynew(void);
210 static void ttyread(void);
211 static void ttyresize(int, int);
212 static void ttywrite(const char *, size_t);
214 static void xdraws(char *, Glyph
, int, int, int, int);
215 static void xhints(void);
216 static void xclear(int, int, int, int);
217 static void xdrawcursor(void);
218 static void xinit(void);
219 static void xloadcols(void);
220 static void xseturgency(int);
221 static void xsetsel(char*);
222 static void xresize(int, int);
224 static void expose(XEvent
*);
225 static void visibility(XEvent
*);
226 static void unmap(XEvent
*);
227 static char* kmap(KeySym
, unsigned int state
);
228 static void kpress(XEvent
*);
229 static void cmessage(XEvent
*);
230 static void resize(XEvent
*);
231 static void focus(XEvent
*);
232 static void brelease(XEvent
*);
233 static void bpress(XEvent
*);
234 static void bmotion(XEvent
*);
235 static void selnotify(XEvent
*);
236 static void selrequest(XEvent
*);
238 static void selinit(void);
239 static inline int selected(int, int);
240 static void selcopy(void);
241 static void selpaste();
242 static void selscroll(int, int);
244 static int utf8decode(char *, long *);
245 static int utf8encode(long *, char *);
246 static int utf8size(char *);
247 static int isfullutf8(char *, int);
249 static void (*handler
[LASTEvent
])(XEvent
*) = {
251 [ClientMessage
] = cmessage
,
252 [ConfigureNotify
] = resize
,
253 [VisibilityNotify
] = visibility
,
254 [UnmapNotify
] = unmap
,
258 [MotionNotify
] = bmotion
,
259 [ButtonPress
] = bpress
,
260 [ButtonRelease
] = brelease
,
261 [SelectionNotify
] = selnotify
,
262 [SelectionRequest
] = selrequest
,
269 static CSIEscape escseq
;
272 static Selection sel
;
273 static char **opt_cmd
= NULL
;
274 static char *opt_title
= NULL
;
275 static char *opt_embed
= NULL
;
276 static char *opt_class
= NULL
;
279 utf8decode(char *s
, long *u
) {
285 if(~c
&B7
) { /* 0xxxxxxx */
288 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
289 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
291 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
292 *u
= c
&(B3
|B2
|B1
|B0
);
294 } else if((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
299 for(i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
301 if((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
304 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
306 if((n
== 1 && *u
< 0x80) ||
307 (n
== 2 && *u
< 0x800) ||
308 (n
== 3 && *u
< 0x10000) ||
309 (*u
>= 0xD800 && *u
<= 0xDFFF))
318 utf8encode(long *u
, char *s
) {
323 sp
= (unsigned char*) s
;
326 *sp
= uc
; /* 0xxxxxxx */
328 } else if(*u
< 0x800) {
329 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
331 } else if(uc
< 0x10000) {
332 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
334 } else if(uc
<= 0x10FFFF) {
335 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
340 for(i
=n
,++sp
; i
>0; --i
,++sp
)
341 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
351 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
352 UTF-8 otherwise return 0 */
354 isfullutf8(char *s
, int b
) {
355 unsigned char *c1
, *c2
, *c3
;
357 c1
= (unsigned char *) s
;
358 c2
= (unsigned char *) ++s
;
359 c3
= (unsigned char *) ++s
;
362 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
364 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
366 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
368 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
370 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
371 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
379 unsigned char c
= *s
;
383 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
385 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
393 sel
.tclick1
.tv_sec
= 0;
394 sel
.tclick1
.tv_usec
= 0;
398 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
399 if(sel
.xtarget
== None
)
400 sel
.xtarget
= XA_STRING
;
404 selected(int x
, int y
) {
405 if(sel
.ey
== y
&& sel
.by
== y
) {
406 int bx
= MIN(sel
.bx
, sel
.ex
);
407 int ex
= MAX(sel
.bx
, sel
.ex
);
408 return BETWEEN(x
, bx
, ex
);
410 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
411 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
415 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
417 *b
= e
->xbutton
.button
;
419 *x
= X2COL(e
->xbutton
.x
);
420 *y
= Y2ROW(e
->xbutton
.y
);
421 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
422 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
423 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
424 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
428 mousereport(XEvent
*e
) {
429 int x
= X2COL(e
->xbutton
.x
);
430 int y
= Y2ROW(e
->xbutton
.y
);
431 int button
= e
->xbutton
.button
;
432 int state
= e
->xbutton
.state
;
433 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
434 static int ob
, ox
, oy
;
437 if(e
->xbutton
.type
== MotionNotify
) {
438 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
442 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
448 if(e
->xbutton
.type
== ButtonPress
) {
454 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
455 + (state
& Mod4Mask
? 8 : 0)
456 + (state
& ControlMask
? 16 : 0);
458 ttywrite(buf
, sizeof(buf
));
463 if(IS_SET(MODE_MOUSE
))
465 else if(e
->xbutton
.button
== Button1
) {
467 for(int i
=sel
.b
.y
; i
<=sel
.e
.y
; i
++)
470 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
471 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
478 int x
, y
, sz
, sl
, ls
= 0;
483 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
484 ptr
= str
= malloc(sz
);
485 for(y
= 0; y
< term
.row
; y
++) {
486 for(x
= 0; x
< term
.col
; x
++)
487 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
488 sl
= utf8size(term
.line
[y
][x
].c
);
489 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
492 if(ls
&& y
< sel
.e
.y
)
501 selnotify(XEvent
*e
) {
502 unsigned long nitems
, ofs
, rem
;
509 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
510 False
, AnyPropertyType
, &type
, &format
,
511 &nitems
, &rem
, &data
)) {
512 fprintf(stderr
, "Clipboard allocation failed\n");
515 ttywrite((const char *) data
, nitems
* format
/ 8);
517 /* number of 32-bit chunks returned */
518 ofs
+= nitems
* format
/ 32;
524 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
528 selrequest(XEvent
*e
) {
529 XSelectionRequestEvent
*xsre
;
533 xsre
= (XSelectionRequestEvent
*) e
;
534 xev
.type
= SelectionNotify
;
535 xev
.requestor
= xsre
->requestor
;
536 xev
.selection
= xsre
->selection
;
537 xev
.target
= xsre
->target
;
538 xev
.time
= xsre
->time
;
542 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
543 if(xsre
->target
== xa_targets
) {
544 /* respond with the supported type */
545 Atom string
= sel
.xtarget
;
546 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
547 XA_ATOM
, 32, PropModeReplace
,
548 (unsigned char *) &string
, 1);
549 xev
.property
= xsre
->property
;
550 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
551 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
552 xsre
->target
, 8, PropModeReplace
,
553 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
554 xev
.property
= xsre
->property
;
557 /* all done, send a notification to the listener */
558 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
559 fprintf(stderr
, "Error sending SelectionNotify event\n");
564 /* register the selection for both the clipboard and the primary */
570 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
572 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
573 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
579 brelease(XEvent
*e
) {
580 if(IS_SET(MODE_MOUSE
)) {
584 if(e
->xbutton
.button
== Button2
)
586 else if(e
->xbutton
.button
== Button1
) {
588 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
589 term
.dirty
[sel
.ey
] = 1;
590 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
593 gettimeofday(&now
, NULL
);
595 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
596 /* triple click on the line */
597 sel
.b
.x
= sel
.bx
= 0;
598 sel
.e
.x
= sel
.ex
= term
.col
;
599 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
601 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
602 /* double click to select word */
604 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
605 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
607 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
608 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
610 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
616 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
617 gettimeofday(&sel
.tclick1
, NULL
);
623 if(IS_SET(MODE_MOUSE
)) {
628 int oldey
= sel
.ey
, oldex
= sel
.ex
;
629 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
631 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
632 int starty
= MIN(oldey
, sel
.ey
);
633 int endy
= MAX(oldey
, sel
.ey
);
634 for(int i
=starty
; i
<=endy
; i
++)
642 die(const char *errstr
, ...) {
645 va_start(ap
, errstr
);
646 vfprintf(stderr
, errstr
, ap
);
654 char *envshell
= getenv("SHELL");
656 DEFAULT(envshell
, "sh");
657 putenv("TERM="TNAME
);
658 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
659 execvp(args
[0], args
);
666 if(waitpid(pid
, &stat
, 0) < 0)
667 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
669 exit(WEXITSTATUS(stat
));
678 /* seems to work fine on linux, openbsd and freebsd */
679 struct winsize w
= {term
.row
, term
.col
, 0, 0};
680 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
681 die("openpty failed: %s\n", SERRNO
);
683 switch(pid
= fork()) {
685 die("fork failed\n");
688 setsid(); /* create a new process group */
689 dup2(s
, STDIN_FILENO
);
690 dup2(s
, STDOUT_FILENO
);
691 dup2(s
, STDERR_FILENO
);
692 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
693 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
701 signal(SIGCHLD
, sigchld
);
708 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
710 fprintf(stderr
, "\n");
715 static char buf
[BUFSIZ
];
716 static int buflen
= 0;
719 int charsize
; /* size of utf8 char in bytes */
723 /* append read bytes to unprocessed bytes */
724 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
725 die("Couldn't read from shell: %s\n", SERRNO
);
727 /* process every complete utf8 char */
730 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
731 charsize
= utf8decode(ptr
, &utf8c
);
732 utf8encode(&utf8c
, s
);
738 /* keep any uncomplete utf8 char for the next call */
739 memmove(buf
, ptr
, buflen
);
743 ttywrite(const char *s
, size_t n
) {
744 if(write(cmdfd
, s
, n
) == -1)
745 die("write error on tty: %s\n", SERRNO
);
749 ttyresize(int x
, int y
) {
754 w
.ws_xpixel
= w
.ws_ypixel
= 0;
755 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
756 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
763 for(i
= 0; i
< term
.row
; i
++)
771 if(mode
== CURSOR_SAVE
)
773 else if(mode
== CURSOR_LOAD
)
774 term
.c
= c
, tmoveto(c
.x
, c
.y
);
783 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
785 term
.top
= 0, term
.bot
= term
.row
- 1;
786 term
.mode
= MODE_WRAP
;
787 tclearregion(0, 0, term
.col
-1, term
.row
-1);
791 tnew(int col
, int row
) {
792 /* set screen size */
793 term
.row
= row
, term
.col
= col
;
794 term
.line
= malloc(term
.row
* sizeof(Line
));
795 term
.alt
= malloc(term
.row
* sizeof(Line
));
796 term
.dirty
= malloc(term
.row
* sizeof(*term
.dirty
));
798 for(row
= 0; row
< term
.row
; row
++) {
799 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
800 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
809 Line
* tmp
= term
.line
;
810 term
.line
= term
.alt
;
812 term
.mode
^= MODE_ALTSCREEN
;
817 tscrolldown(int orig
, int n
) {
821 LIMIT(n
, 0, term
.bot
-orig
+1);
823 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
825 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
827 term
.line
[i
] = term
.line
[i
-n
];
828 term
.line
[i
-n
] = temp
;
838 tscrollup(int orig
, int n
) {
841 LIMIT(n
, 0, term
.bot
-orig
+1);
843 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
845 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
847 term
.line
[i
] = term
.line
[i
+n
];
848 term
.line
[i
+n
] = temp
;
858 selscroll(int orig
, int n
) {
862 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
863 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
867 if(sel
.by
< term
.top
) {
871 if(sel
.ey
> term
.bot
) {
875 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
876 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
881 tnewline(int first_col
) {
884 tscrollup(term
.top
, 1);
887 tmoveto(first_col
? 0 : term
.c
.x
, y
);
893 char *p
= escseq
.buf
;
897 escseq
.priv
= 1, p
++;
899 while(p
< escseq
.buf
+escseq
.len
) {
901 escseq
.arg
[escseq
.narg
] *= 10;
902 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
904 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
915 tmoveto(int x
, int y
) {
916 LIMIT(x
, 0, term
.col
-1);
917 LIMIT(y
, 0, term
.row
-1);
918 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
925 term
.dirty
[term
.c
.y
] = 1;
926 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
927 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
928 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
932 tclearregion(int x1
, int y1
, int x2
, int y2
) {
936 temp
= x1
, x1
= x2
, x2
= temp
;
938 temp
= y1
, y1
= y2
, y2
= temp
;
940 LIMIT(x1
, 0, term
.col
-1);
941 LIMIT(x2
, 0, term
.col
-1);
942 LIMIT(y1
, 0, term
.row
-1);
943 LIMIT(y2
, 0, term
.row
-1);
945 for(y
= y1
; y
<= y2
; y
++) {
947 for(x
= x1
; x
<= x2
; x
++)
948 term
.line
[y
][x
].state
= 0;
954 int src
= term
.c
.x
+ n
;
956 int size
= term
.col
- src
;
958 term
.dirty
[term
.c
.y
] = 1;
960 if(src
>= term
.col
) {
961 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
964 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
965 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
969 tinsertblank(int n
) {
972 int size
= term
.col
- dst
;
974 term
.dirty
[term
.c
.y
] = 1;
976 if(dst
>= term
.col
) {
977 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
980 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
981 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
985 tinsertblankline(int n
) {
986 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
989 tscrolldown(term
.c
.y
, n
);
994 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
997 tscrollup(term
.c
.y
, n
);
1001 tsetattr(int *attr
, int l
) {
1004 for(i
= 0; i
< l
; i
++) {
1007 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
1008 term
.c
.attr
.fg
= DefaultFG
;
1009 term
.c
.attr
.bg
= DefaultBG
;
1012 term
.c
.attr
.mode
|= ATTR_BOLD
;
1015 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1018 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1021 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1024 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1027 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1030 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1032 if(BETWEEN(attr
[i
], 0, 255))
1033 term
.c
.attr
.fg
= attr
[i
];
1035 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1038 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1041 term
.c
.attr
.fg
= DefaultFG
;
1044 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1046 if(BETWEEN(attr
[i
], 0, 255))
1047 term
.c
.attr
.bg
= attr
[i
];
1049 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1052 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
1055 term
.c
.attr
.bg
= DefaultBG
;
1058 if(BETWEEN(attr
[i
], 30, 37))
1059 term
.c
.attr
.fg
= attr
[i
] - 30;
1060 else if(BETWEEN(attr
[i
], 40, 47))
1061 term
.c
.attr
.bg
= attr
[i
] - 40;
1062 else if(BETWEEN(attr
[i
], 90, 97))
1063 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1064 else if(BETWEEN(attr
[i
], 100, 107))
1065 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1067 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
1075 tsetscroll(int t
, int b
) {
1078 LIMIT(t
, 0, term
.row
-1);
1079 LIMIT(b
, 0, term
.row
-1);
1091 switch(escseq
.mode
) {
1094 fprintf(stderr
, "erresc: unknown csi ");
1098 case '@': /* ICH -- Insert <n> blank char */
1099 DEFAULT(escseq
.arg
[0], 1);
1100 tinsertblank(escseq
.arg
[0]);
1102 case 'A': /* CUU -- Cursor <n> Up */
1104 DEFAULT(escseq
.arg
[0], 1);
1105 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
1107 case 'B': /* CUD -- Cursor <n> Down */
1108 DEFAULT(escseq
.arg
[0], 1);
1109 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
1111 case 'C': /* CUF -- Cursor <n> Forward */
1113 DEFAULT(escseq
.arg
[0], 1);
1114 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
1116 case 'D': /* CUB -- Cursor <n> Backward */
1117 DEFAULT(escseq
.arg
[0], 1);
1118 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
1120 case 'E': /* CNL -- Cursor <n> Down and first col */
1121 DEFAULT(escseq
.arg
[0], 1);
1122 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
1124 case 'F': /* CPL -- Cursor <n> Up and first col */
1125 DEFAULT(escseq
.arg
[0], 1);
1126 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
1128 case 'G': /* CHA -- Move to <col> */
1129 case '`': /* XXX: HPA -- same? */
1130 DEFAULT(escseq
.arg
[0], 1);
1131 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
1133 case 'H': /* CUP -- Move to <row> <col> */
1134 case 'f': /* XXX: HVP -- same? */
1135 DEFAULT(escseq
.arg
[0], 1);
1136 DEFAULT(escseq
.arg
[1], 1);
1137 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
1139 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
1140 case 'J': /* ED -- Clear screen */
1142 switch(escseq
.arg
[0]) {
1144 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1145 if(term
.c
.y
< term
.row
-1)
1146 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1150 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1151 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1154 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1160 case 'K': /* EL -- Clear line */
1161 switch(escseq
.arg
[0]) {
1163 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1166 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1169 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1173 case 'S': /* SU -- Scroll <n> line up */
1174 DEFAULT(escseq
.arg
[0], 1);
1175 tscrollup(term
.top
, escseq
.arg
[0]);
1177 case 'T': /* SD -- Scroll <n> line down */
1178 DEFAULT(escseq
.arg
[0], 1);
1179 tscrolldown(term
.top
, escseq
.arg
[0]);
1181 case 'L': /* IL -- Insert <n> blank lines */
1182 DEFAULT(escseq
.arg
[0], 1);
1183 tinsertblankline(escseq
.arg
[0]);
1185 case 'l': /* RM -- Reset Mode */
1187 switch(escseq
.arg
[0]) {
1189 term
.mode
&= ~MODE_APPKEYPAD
;
1191 case 5: /* DECSCNM -- Remove reverse video */
1192 if(IS_SET(MODE_REVERSE
)) {
1193 term
.mode
&= ~MODE_REVERSE
;
1198 term
.mode
&= ~MODE_WRAP
;
1200 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1203 term
.mode
&= ~MODE_CRLF
;
1206 term
.c
.state
|= CURSOR_HIDE
;
1208 case 1000: /* disable X11 xterm mouse reporting */
1209 term
.mode
&= ~MODE_MOUSEBTN
;
1212 term
.mode
&= ~MODE_MOUSEMOTION
;
1214 case 1049: /* = 1047 and 1048 */
1217 if(IS_SET(MODE_ALTSCREEN
)) {
1218 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1221 if(escseq
.arg
[0] != 1049)
1224 tcursor(CURSOR_LOAD
);
1230 switch(escseq
.arg
[0]) {
1232 term
.mode
&= ~MODE_INSERT
;
1239 case 'M': /* DL -- Delete <n> lines */
1240 DEFAULT(escseq
.arg
[0], 1);
1241 tdeleteline(escseq
.arg
[0]);
1243 case 'X': /* ECH -- Erase <n> char */
1244 DEFAULT(escseq
.arg
[0], 1);
1245 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1247 case 'P': /* DCH -- Delete <n> char */
1248 DEFAULT(escseq
.arg
[0], 1);
1249 tdeletechar(escseq
.arg
[0]);
1251 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1252 case 'd': /* VPA -- Move to <row> */
1253 DEFAULT(escseq
.arg
[0], 1);
1254 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1256 case 'h': /* SM -- Set terminal mode */
1258 switch(escseq
.arg
[0]) {
1260 term
.mode
|= MODE_APPKEYPAD
;
1262 case 5: /* DECSCNM -- Reverve video */
1263 if(!IS_SET(MODE_REVERSE
)) {
1264 term
.mode
|= MODE_REVERSE
;
1269 term
.mode
|= MODE_WRAP
;
1272 term
.mode
|= MODE_CRLF
;
1274 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1275 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1276 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1279 term
.c
.state
&= ~CURSOR_HIDE
;
1281 case 1000: /* 1000,1002: enable xterm mouse report */
1282 term
.mode
|= MODE_MOUSEBTN
;
1285 term
.mode
|= MODE_MOUSEMOTION
;
1287 case 1049: /* = 1047 and 1048 */
1290 if(IS_SET(MODE_ALTSCREEN
))
1291 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1294 if(escseq
.arg
[0] != 1049)
1297 tcursor(CURSOR_SAVE
);
1299 default: goto unknown
;
1302 switch(escseq
.arg
[0]) {
1304 term
.mode
|= MODE_INSERT
;
1306 default: goto unknown
;
1310 case 'm': /* SGR -- Terminal attribute (color) */
1311 tsetattr(escseq
.arg
, escseq
.narg
);
1313 case 'r': /* DECSTBM -- Set Scrolling Region */
1317 DEFAULT(escseq
.arg
[0], 1);
1318 DEFAULT(escseq
.arg
[1], term
.row
);
1319 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1323 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1324 tcursor(CURSOR_SAVE
);
1326 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1327 tcursor(CURSOR_LOAD
);
1335 printf("ESC [ %s", escseq
.priv
? "? " : "");
1337 for(i
= 0; i
< escseq
.narg
; i
++)
1338 printf("%d ", escseq
.arg
[i
]);
1340 putchar(escseq
.mode
);
1346 memset(&escseq
, 0, sizeof(escseq
));
1351 int space
= TAB
- term
.c
.x
% TAB
;
1352 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1358 if(term
.esc
& ESC_START
) {
1359 if(term
.esc
& ESC_CSI
) {
1360 escseq
.buf
[escseq
.len
++] = ascii
;
1361 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1363 csiparse(), csihandle();
1365 /* TODO: handle other OSC */
1366 } else if(term
.esc
& ESC_OSC
) {
1369 term
.esc
= ESC_START
| ESC_TITLE
;
1371 } else if(term
.esc
& ESC_TITLE
) {
1372 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1374 term
.title
[term
.titlelen
] = '\0';
1375 XStoreName(xw
.dpy
, xw
.win
, term
.title
);
1377 term
.title
[term
.titlelen
++] = ascii
;
1379 } else if(term
.esc
& ESC_ALTCHARSET
) {
1381 case '0': /* Line drawing crap */
1382 term
.c
.attr
.mode
|= ATTR_GFX
;
1384 case 'B': /* Back to regular text */
1385 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1388 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1394 term
.esc
|= ESC_CSI
;
1397 term
.esc
|= ESC_OSC
;
1400 term
.esc
|= ESC_ALTCHARSET
;
1402 case 'D': /* IND -- Linefeed */
1403 if(term
.c
.y
== term
.bot
)
1404 tscrollup(term
.top
, 1);
1406 tmoveto(term
.c
.x
, term
.c
.y
+1);
1409 case 'E': /* NEL -- Next line */
1410 tnewline(1); /* always go to first col */
1413 case 'M': /* RI -- Reverse index */
1414 if(term
.c
.y
== term
.top
)
1415 tscrolldown(term
.top
, 1);
1417 tmoveto(term
.c
.x
, term
.c
.y
-1);
1420 case 'c': /* RIS -- Reset to inital state */
1424 case '=': /* DECPAM -- Application keypad */
1425 term
.mode
|= MODE_APPKEYPAD
;
1428 case '>': /* DECPNM -- Normal keypad */
1429 term
.mode
&= ~MODE_APPKEYPAD
;
1432 case '7': /* DECSC -- Save Cursor */
1433 tcursor(CURSOR_SAVE
);
1436 case '8': /* DECRC -- Restore Cursor */
1437 tcursor(CURSOR_LOAD
);
1441 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1442 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1447 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1454 tmoveto(term
.c
.x
-1, term
.c
.y
);
1457 tmoveto(0, term
.c
.y
);
1462 /* go to first col if the mode is set */
1463 tnewline(IS_SET(MODE_CRLF
));
1466 if(!(xw
.state
& WIN_FOCUSED
))
1471 term
.esc
= ESC_START
;
1474 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1475 tnewline(1); /* always go to first col */
1477 if(term
.c
.x
+1 < term
.col
)
1478 tmoveto(term
.c
.x
+1, term
.c
.y
);
1480 term
.c
.state
|= CURSOR_WRAPNEXT
;
1486 tresize(int col
, int row
) {
1488 int minrow
= MIN(row
, term
.row
);
1489 int mincol
= MIN(col
, term
.col
);
1490 int slide
= term
.c
.y
- row
+ 1;
1492 if(col
< 1 || row
< 1)
1495 /* free unneeded rows */
1498 /* slide screen to keep cursor where we expect it -
1499 * tscrollup would work here, but we can optimize to
1500 * memmove because we're freeing the earlier lines */
1501 for(/* i = 0 */; i
< slide
; i
++) {
1505 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1506 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1508 for(i
+= row
; i
< term
.row
; i
++) {
1513 /* resize to new height */
1514 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1515 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1516 term
.dirty
= realloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1518 /* resize each row to new width, zero-pad if needed */
1519 for(i
= 0; i
< minrow
; i
++) {
1521 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1522 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1523 for(x
= mincol
; x
< col
; x
++) {
1524 term
.line
[i
][x
].state
= 0;
1525 term
.alt
[i
][x
].state
= 0;
1529 /* allocate any new rows */
1530 for(/* i == minrow */; i
< row
; i
++) {
1532 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1533 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1536 /* update terminal size */
1537 term
.col
= col
, term
.row
= row
;
1538 /* make use of the LIMIT in tmoveto */
1539 tmoveto(term
.c
.x
, term
.c
.y
);
1540 /* reset scrolling region */
1541 tsetscroll(0, row
-1);
1547 xresize(int col
, int row
) {
1553 xw
.bufw
= MAX(1, col
* xw
.cw
);
1554 xw
.bufh
= MAX(1, row
* xw
.ch
);
1555 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1556 XCopyArea(xw
.dpy
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1557 XFreePixmap(xw
.dpy
, xw
.buf
);
1558 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1560 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0,
1561 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1562 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1563 XClearArea(xw
.dpy
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1564 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1567 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
,
1568 xw
.bufw
, xw
.bufh
-oldh
);
1569 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1570 XClearArea(xw
.dpy
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1571 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1580 unsigned long white
= WhitePixel(xw
.dpy
, xw
.scr
);
1582 for(i
= 0; i
< LEN(colorname
); i
++) {
1583 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1585 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1587 dc
.col
[i
] = color
.pixel
;
1590 /* same colors as xterm */
1591 for(r
= 0; r
< 6; r
++)
1592 for(g
= 0; g
< 6; g
++)
1593 for(b
= 0; b
< 6; b
++) {
1594 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1595 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1596 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1597 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1599 fprintf(stderr
, "Could not allocate color %d\n", i
);
1601 dc
.col
[i
] = color
.pixel
;
1605 for(r
= 0; r
< 24; r
++, i
++) {
1606 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1607 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1609 fprintf(stderr
, "Could not allocate color %d\n", i
);
1611 dc
.col
[i
] = color
.pixel
;
1616 xclear(int x1
, int y1
, int x2
, int y2
) {
1617 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1618 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1619 x1
* xw
.cw
, y1
* xw
.ch
,
1620 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1625 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1626 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1628 .flags
= PSize
| PResizeInc
| PBaseSize
,
1631 .height_inc
= xw
.ch
,
1633 .base_height
= 2*BORDER
,
1634 .base_width
= 2*BORDER
,
1636 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1640 xinitfont(char *fontstr
) {
1642 char *def
, **missing
;
1646 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1649 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1650 XFreeStringList(missing
);
1656 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1657 XFontStruct
**xfonts
;
1661 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1662 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1663 for(i
= 0; i
< n
; i
++) {
1664 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1665 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1666 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1667 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1673 initfonts(char *fontstr
, char *bfontstr
) {
1674 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1675 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1676 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1677 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1678 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1679 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1680 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1685 XSetWindowAttributes attrs
;
1689 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1690 die("Can't open display\n");
1691 xw
.scr
= XDefaultScreen(xw
.dpy
);
1694 initfonts(FONT
, BOLDFONT
);
1696 /* XXX: Assuming same size for bold font */
1697 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1698 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1701 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1704 /* window - default size */
1705 xw
.bufh
= term
.row
* xw
.ch
;
1706 xw
.bufw
= term
.col
* xw
.cw
;
1707 xw
.h
= xw
.bufh
+ 2*BORDER
;
1708 xw
.w
= xw
.bufw
+ 2*BORDER
;
1710 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1711 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1712 attrs
.bit_gravity
= NorthWestGravity
;
1713 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1714 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1715 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
1716 | EnterWindowMask
| LeaveWindowMask
;
1717 attrs
.colormap
= xw
.cmap
;
1719 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1720 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
1721 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1722 XDefaultVisual(xw
.dpy
, xw
.scr
),
1723 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1726 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1730 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1731 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1732 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1733 XNFocusWindow
, xw
.win
, NULL
);
1735 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1737 /* white cursor, black outline */
1738 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1739 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1740 XRecolorCursor(xw
.dpy
, cursor
,
1741 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1742 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1744 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1746 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1747 XMapWindow(xw
.dpy
, xw
.win
);
1753 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1754 unsigned long xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
], temp
;
1755 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1758 /* only switch default fg/bg if term is in RV mode */
1759 if(IS_SET(MODE_REVERSE
)) {
1760 if(base
.fg
== DefaultFG
)
1761 xfg
= dc
.col
[DefaultBG
];
1762 if(base
.bg
== DefaultBG
)
1763 xbg
= dc
.col
[DefaultFG
];
1766 if(base
.mode
& ATTR_REVERSE
)
1767 temp
= xfg
, xfg
= xbg
, xbg
= temp
;
1769 XSetBackground(xw
.dpy
, dc
.gc
, xbg
);
1770 XSetForeground(xw
.dpy
, dc
.gc
, xfg
);
1772 if(base
.mode
& ATTR_GFX
) {
1773 for(i
= 0; i
< bytelen
; i
++) {
1774 char c
= gfx
[(unsigned int)s
[i
] % 256];
1777 else if(s
[i
] > 0x5f)
1782 XmbDrawImageString(xw
.dpy
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.set
: dc
.font
.set
,
1783 dc
.gc
, winx
, winy
, s
, bytelen
);
1785 if(base
.mode
& ATTR_UNDERLINE
)
1786 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1791 static int oldx
= 0;
1792 static int oldy
= 0;
1794 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1796 LIMIT(oldx
, 0, term
.col
-1);
1797 LIMIT(oldy
, 0, term
.row
-1);
1799 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1800 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1802 /* remove the old cursor */
1803 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1804 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1805 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1807 xclear(oldx
, oldy
, oldx
, oldy
);
1809 /* draw the new one */
1810 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1812 if(IS_SET(MODE_REVERSE
))
1813 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
1814 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1815 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1821 drawregion(0, 0, term
.col
, term
.row
);
1825 drawregion(int x1
, int y1
, int x2
, int y2
) {
1826 int ic
, ib
, x
, y
, ox
, sl
;
1828 char buf
[DRAW_BUF_SIZ
];
1830 if(!(xw
.state
& WIN_VISIBLE
))
1833 for(y
= y1
; y
< y2
; y
++) {
1836 xclear(0, y
, term
.col
, y
);
1838 base
= term
.line
[y
][0];
1840 for(x
= x1
; x
< x2
; x
++) {
1841 new = term
.line
[y
][x
];
1842 if(sel
.bx
!= -1 && *(new.c
) && selected(x
, y
))
1843 new.mode
^= ATTR_REVERSE
;
1844 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1845 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1846 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1849 if(new.state
& GLYPH_SET
) {
1854 sl
= utf8size(new.c
);
1855 memcpy(buf
+ib
, new.c
, sl
);
1861 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1864 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, x1
*xw
.cw
, y1
*xw
.ch
, x2
*xw
.cw
, y2
*xw
.ch
, BORDER
, BORDER
);
1868 expose(XEvent
*ev
) {
1869 XExposeEvent
*e
= &ev
->xexpose
;
1870 if(xw
.state
& WIN_REDRAW
) {
1872 xw
.state
&= ~WIN_REDRAW
;
1876 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1877 e
->width
, e
->height
, e
->x
, e
->y
);
1881 visibility(XEvent
*ev
) {
1882 XVisibilityEvent
*e
= &ev
->xvisibility
;
1883 if(e
->state
== VisibilityFullyObscured
)
1884 xw
.state
&= ~WIN_VISIBLE
;
1885 else if(!(xw
.state
& WIN_VISIBLE
))
1886 /* need a full redraw for next Expose, not just a buf copy */
1887 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1892 xw
.state
&= ~WIN_VISIBLE
;
1896 xseturgency(int add
) {
1897 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1898 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1899 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1905 if(ev
->type
== FocusIn
) {
1906 xw
.state
|= WIN_FOCUSED
;
1909 xw
.state
&= ~WIN_FOCUSED
;
1914 kmap(KeySym k
, unsigned int state
) {
1917 for(i
= 0; i
< LEN(key
); i
++) {
1918 unsigned int mask
= key
[i
].mask
;
1919 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
1920 return (char*)key
[i
].s
;
1926 kpress(XEvent
*ev
) {
1927 XKeyEvent
*e
= &ev
->xkey
;
1936 meta
= e
->state
& Mod1Mask
;
1937 shift
= e
->state
& ShiftMask
;
1938 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1940 /* 1. custom keys from config.h */
1941 if((customkey
= kmap(ksym
, e
->state
)))
1942 ttywrite(customkey
, strlen(customkey
));
1943 /* 2. hardcoded (overrides X lookup) */
1950 /* XXX: shift up/down doesn't work */
1951 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
1959 if(IS_SET(MODE_CRLF
))
1960 ttywrite("\r\n", 2);
1967 if(meta
&& len
== 1)
1968 ttywrite("\033", 1);
1976 cmessage(XEvent
*e
) {
1978 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
1979 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1980 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1981 xw
.state
|= WIN_FOCUSED
;
1983 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1984 xw
.state
&= ~WIN_FOCUSED
;
1994 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1997 xw
.w
= e
->xconfigure
.width
;
1998 xw
.h
= e
->xconfigure
.height
;
1999 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2000 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2001 if(col
== term
.col
&& row
== term
.row
)
2003 if(tresize(col
, row
))
2005 ttyresize(col
, row
);
2013 int xfd
= XConnectionNumber(xw
.dpy
);
2017 FD_SET(cmdfd
, &rfd
);
2019 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
2022 die("select failed: %s\n", SERRNO
);
2024 if(FD_ISSET(cmdfd
, &rfd
)) {
2028 while(XPending(xw
.dpy
)) {
2029 XNextEvent(xw
.dpy
, &ev
);
2030 if(XFilterEvent(&ev
, xw
.win
))
2032 if(handler
[ev
.type
])
2033 (handler
[ev
.type
])(&ev
);
2039 main(int argc
, char *argv
[]) {
2042 for(i
= 1; i
< argc
; i
++) {
2043 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2045 if(++i
< argc
) opt_title
= argv
[i
];
2048 if(++i
< argc
) opt_class
= argv
[i
];
2051 if(++i
< argc
) opt_embed
= argv
[i
];
2054 /* eat every remaining arguments */
2055 if(++i
< argc
) opt_cmd
= &argv
[i
];
2064 setlocale(LC_CTYPE
, "");