1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/extensions/Xdbe.h>
31 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
33 #elif defined(__FreeBSD__) || defined(__DragonFly__)
38 "st " VERSION " (c) 2010-2012 st engineers\n" \
39 "usage: st [-t title] [-c class] [-g geometry]" \
40 " [-w windowid] [-v] [-f file] [-e command...]\n"
43 #define XEMBED_FOCUS_IN 4
44 #define XEMBED_FOCUS_OUT 5
47 #define ESC_BUF_SIZ 256
48 #define ESC_ARG_SIZ 16
49 #define STR_BUF_SIZ 256
50 #define STR_ARG_SIZ 16
51 #define DRAW_BUF_SIZ 1024
53 #define XK_NO_MOD UINT_MAX
56 #define SELECT_TIMEOUT (20*1000) /* 20 ms */
57 #define DRAW_TIMEOUT (20*1000) /* 20 ms */
58 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
60 #define SERRNO strerror(errno)
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 #define MAX(a, b) ((a) < (b) ? (b) : (a))
63 #define LEN(a) (sizeof(a) / sizeof(a[0]))
64 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
65 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
66 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
67 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
68 #define IS_SET(flag) (term.mode & (flag))
69 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
70 #define X2COL(x) (((x) - BORDER)/xw.cw)
71 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
73 enum glyph_attribute
{
82 enum cursor_movement
{
109 MODE_MOUSEMOTION
= 64,
117 ESC_STR
= 4, /* DSC, OSC, PM, APC */
119 ESC_STR_END
= 16, /* a final string was encountered */
130 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
132 typedef unsigned char uchar
;
133 typedef unsigned int uint
;
134 typedef unsigned long ulong
;
135 typedef unsigned short ushort
;
138 char c
[UTF_SIZ
]; /* character code */
139 uchar mode
; /* attribute flags */
140 ushort fg
; /* foreground */
141 ushort bg
; /* background */
142 uchar state
; /* state flags */
148 Glyph attr
; /* current char attributes */
154 /* CSI Escape sequence structs */
155 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
157 char buf
[ESC_BUF_SIZ
]; /* raw string */
158 int len
; /* raw string length */
160 int arg
[ESC_ARG_SIZ
];
161 int narg
; /* nb of args */
165 /* STR Escape sequence structs */
166 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
168 char type
; /* ESC type ... */
169 char buf
[STR_BUF_SIZ
]; /* raw string */
170 int len
; /* raw string length */
171 char *args
[STR_ARG_SIZ
];
172 int narg
; /* nb of args */
175 /* Internal representation of the screen */
177 int row
; /* nb row */
178 int col
; /* nb col */
179 Line
* line
; /* screen */
180 Line
* alt
; /* alternate screen */
181 bool* dirty
; /* dirtyness of lines */
182 TCursor c
; /* cursor */
183 int top
; /* top scroll limit */
184 int bot
; /* bottom scroll limit */
185 int mode
; /* terminal mode flags */
186 int esc
; /* escape state flags */
190 /* Purely graphic info */
200 Bool isfixed
; /* is fixed geometry? */
201 int fx
, fy
, fw
, fh
; /* fixed geometry */
202 int w
; /* window width */
203 int h
; /* window height */
204 int ch
; /* char height */
205 int cw
; /* char width */
206 char state
; /* focus, redraw, visible */
207 struct timeval lastdraw
;
217 /* TODO: use better name for vars... */
222 struct {int x
, y
;} b
, e
;
226 struct timeval tclick1
;
227 struct timeval tclick2
;
232 /* Drawing Context */
234 ulong col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
242 } font
, bfont
, ifont
;
245 static void die(const char*, ...);
246 static void draw(void);
247 static void redraw(void);
248 static void drawregion(int, int, int, int);
249 static void execsh(void);
250 static void sigchld(int);
251 static void run(void);
252 static bool last_draw_too_old(void);
254 static void csidump(void);
255 static void csihandle(void);
256 static void csiparse(void);
257 static void csireset(void);
258 static void strdump(void);
259 static void strhandle(void);
260 static void strparse(void);
261 static void strreset(void);
263 static void tclearregion(int, int, int, int);
264 static void tcursor(int);
265 static void tdeletechar(int);
266 static void tdeleteline(int);
267 static void tinsertblank(int);
268 static void tinsertblankline(int);
269 static void tmoveto(int, int);
270 static void tnew(int, int);
271 static void tnewline(int);
272 static void tputtab(bool);
273 static void tputc(char*);
274 static void treset(void);
275 static int tresize(int, int);
276 static void tscrollup(int, int);
277 static void tscrolldown(int, int);
278 static void tsetattr(int*, int);
279 static void tsetchar(char*);
280 static void tsetscroll(int, int);
281 static void tswapscreen(void);
282 static void tsetdirt(int, int);
283 static void tsetmode(bool, bool, int *, int);
284 static void tfulldirt(void);
286 static void ttynew(void);
287 static void ttyread(void);
288 static void ttyresize(int, int);
289 static void ttywrite(const char *, size_t);
291 static void xdraws(char *, Glyph
, int, int, int, int);
292 static void xhints(void);
293 static void xclear(int, int, int, int);
294 static void xcopy(void);
295 static void xdrawcursor(void);
296 static void xinit(void);
297 static void xloadcols(void);
298 static void xseturgency(int);
299 static void xsetsel(char*);
300 static void xresize(int, int);
302 static void expose(XEvent
*);
303 static void visibility(XEvent
*);
304 static void unmap(XEvent
*);
305 static char* kmap(KeySym
, uint
);
306 static void kpress(XEvent
*);
307 static void cmessage(XEvent
*);
308 static void resize(XEvent
*);
309 static void focus(XEvent
*);
310 static void brelease(XEvent
*);
311 static void bpress(XEvent
*);
312 static void bmotion(XEvent
*);
313 static void selnotify(XEvent
*);
314 static void selclear(XEvent
*);
315 static void selrequest(XEvent
*);
317 static void selinit(void);
318 static inline bool selected(int, int);
319 static void selcopy(void);
320 static void selpaste(void);
321 static void selscroll(int, int);
323 static int utf8decode(char *, long *);
324 static int utf8encode(long *, char *);
325 static int utf8size(char *);
326 static int isfullutf8(char *, int);
328 static void (*handler
[LASTEvent
])(XEvent
*) = {
330 [ClientMessage
] = cmessage
,
331 [ConfigureNotify
] = resize
,
332 [VisibilityNotify
] = visibility
,
333 [UnmapNotify
] = unmap
,
337 [MotionNotify
] = bmotion
,
338 [ButtonPress
] = bpress
,
339 [ButtonRelease
] = brelease
,
340 [SelectionClear
] = selclear
,
341 [SelectionNotify
] = selnotify
,
342 [SelectionRequest
] = selrequest
,
349 static CSIEscape csiescseq
;
350 static STREscape strescseq
;
353 static Selection sel
;
355 static char **opt_cmd
= NULL
;
356 static char *opt_io
= NULL
;
357 static char *opt_title
= NULL
;
358 static char *opt_embed
= NULL
;
359 static char *opt_class
= NULL
;
362 utf8decode(char *s
, long *u
) {
368 if(~c
& B7
) { /* 0xxxxxxx */
371 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
372 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
374 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
375 *u
= c
&(B3
|B2
|B1
|B0
);
377 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
382 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
384 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
387 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
389 if((n
== 1 && *u
< 0x80) ||
390 (n
== 2 && *u
< 0x800) ||
391 (n
== 3 && *u
< 0x10000) ||
392 (*u
>= 0xD800 && *u
<= 0xDFFF))
401 utf8encode(long *u
, char *s
) {
409 *sp
= uc
; /* 0xxxxxxx */
411 } else if(*u
< 0x800) {
412 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
414 } else if(uc
< 0x10000) {
415 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
417 } else if(uc
<= 0x10FFFF) {
418 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
423 for(i
=n
,++sp
; i
>0; --i
,++sp
)
424 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
434 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
435 UTF-8 otherwise return 0 */
437 isfullutf8(char *s
, int b
) {
445 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
447 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
449 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
451 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
453 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
454 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
466 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
468 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
476 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
477 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
481 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
482 if(sel
.xtarget
== None
)
483 sel
.xtarget
= XA_STRING
;
487 selected(int x
, int y
) {
488 if(sel
.ey
== y
&& sel
.by
== y
) {
489 int bx
= MIN(sel
.bx
, sel
.ex
);
490 int ex
= MAX(sel
.bx
, sel
.ex
);
491 return BETWEEN(x
, bx
, ex
);
493 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
494 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
498 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
500 *b
= e
->xbutton
.button
;
502 *x
= X2COL(e
->xbutton
.x
);
503 *y
= Y2ROW(e
->xbutton
.y
);
504 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
505 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
506 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
507 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
511 mousereport(XEvent
*e
) {
512 int x
= X2COL(e
->xbutton
.x
);
513 int y
= Y2ROW(e
->xbutton
.y
);
514 int button
= e
->xbutton
.button
;
515 int state
= e
->xbutton
.state
;
516 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
517 static int ob
, ox
, oy
;
520 if(e
->xbutton
.type
== MotionNotify
) {
521 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
525 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
531 if(e
->xbutton
.type
== ButtonPress
) {
537 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
538 + (state
& Mod4Mask
? 8 : 0)
539 + (state
& ControlMask
? 16 : 0);
541 ttywrite(buf
, sizeof(buf
));
546 if(IS_SET(MODE_MOUSE
))
548 else if(e
->xbutton
.button
== Button1
) {
550 tsetdirt(sel
.b
.y
, sel
.e
.y
);
552 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
553 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
560 int x
, y
, bufsize
, is_selected
= 0;
566 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
567 ptr
= str
= malloc(bufsize
);
569 /* append every set & selected glyph to the selection */
570 for(y
= 0; y
< term
.row
; y
++) {
571 for(x
= 0; x
< term
.col
; x
++) {
572 is_selected
= selected(x
, y
);
573 if((term
.line
[y
][x
].state
& GLYPH_SET
) && is_selected
) {
574 int size
= utf8size(term
.line
[y
][x
].c
);
575 memcpy(ptr
, term
.line
[y
][x
].c
, size
);
580 /* \n at the end of every selected line except for the last one */
581 if(is_selected
&& y
< sel
.e
.y
)
586 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
591 selnotify(XEvent
*e
) {
592 ulong nitems
, ofs
, rem
;
599 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
600 False
, AnyPropertyType
, &type
, &format
,
601 &nitems
, &rem
, &data
)) {
602 fprintf(stderr
, "Clipboard allocation failed\n");
605 ttywrite((const char *) data
, nitems
* format
/ 8);
607 /* number of 32-bit chunks returned */
608 ofs
+= nitems
* format
/ 32;
614 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
617 void selclear(XEvent
*e
) {
621 tsetdirt(sel
.b
.y
, sel
.e
.y
);
626 selrequest(XEvent
*e
) {
627 XSelectionRequestEvent
*xsre
;
631 xsre
= (XSelectionRequestEvent
*) e
;
632 xev
.type
= SelectionNotify
;
633 xev
.requestor
= xsre
->requestor
;
634 xev
.selection
= xsre
->selection
;
635 xev
.target
= xsre
->target
;
636 xev
.time
= xsre
->time
;
640 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
641 if(xsre
->target
== xa_targets
) {
642 /* respond with the supported type */
643 Atom string
= sel
.xtarget
;
644 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
645 XA_ATOM
, 32, PropModeReplace
,
646 (uchar
*) &string
, 1);
647 xev
.property
= xsre
->property
;
648 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
649 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
650 xsre
->target
, 8, PropModeReplace
,
651 (uchar
*) sel
.clip
, strlen(sel
.clip
));
652 xev
.property
= xsre
->property
;
655 /* all done, send a notification to the listener */
656 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
657 fprintf(stderr
, "Error sending SelectionNotify event\n");
662 /* register the selection for both the clipboard and the primary */
668 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
670 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
671 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
677 brelease(XEvent
*e
) {
678 if(IS_SET(MODE_MOUSE
)) {
682 if(e
->xbutton
.button
== Button2
)
684 else if(e
->xbutton
.button
== Button1
) {
686 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
687 term
.dirty
[sel
.ey
] = 1;
688 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
691 gettimeofday(&now
, NULL
);
693 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
694 /* triple click on the line */
695 sel
.b
.x
= sel
.bx
= 0;
696 sel
.e
.x
= sel
.ex
= term
.col
;
697 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
699 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
700 /* double click to select word */
702 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
703 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
705 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
706 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
708 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
714 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
715 gettimeofday(&sel
.tclick1
, NULL
);
721 if(IS_SET(MODE_MOUSE
)) {
726 int oldey
= sel
.ey
, oldex
= sel
.ex
;
727 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
729 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
730 int starty
= MIN(oldey
, sel
.ey
);
731 int endy
= MAX(oldey
, sel
.ey
);
732 tsetdirt(starty
, endy
);
739 die(const char *errstr
, ...) {
742 va_start(ap
, errstr
);
743 vfprintf(stderr
, errstr
, ap
);
751 char *envshell
= getenv("SHELL");
757 DEFAULT(envshell
, SHELL
);
758 putenv("TERM="TNAME
);
759 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
760 execvp(args
[0], args
);
767 if(waitpid(pid
, &stat
, 0) < 0)
768 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
770 exit(WEXITSTATUS(stat
));
779 /* seems to work fine on linux, openbsd and freebsd */
780 struct winsize w
= {term
.row
, term
.col
, 0, 0};
781 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
782 die("openpty failed: %s\n", SERRNO
);
784 switch(pid
= fork()) {
786 die("fork failed\n");
789 setsid(); /* create a new process group */
790 dup2(s
, STDIN_FILENO
);
791 dup2(s
, STDOUT_FILENO
);
792 dup2(s
, STDERR_FILENO
);
793 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
794 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
802 signal(SIGCHLD
, sigchld
);
804 if(!strcmp(opt_io
, "-")) {
807 if(!(fileio
= fopen(opt_io
, "w"))) {
808 fprintf(stderr
, "Error opening %s:%s\n",
809 opt_io
, strerror(errno
));
819 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
821 fprintf(stderr
, "\n");
826 static char buf
[BUFSIZ
];
827 static int buflen
= 0;
830 int charsize
; /* size of utf8 char in bytes */
834 /* append read bytes to unprocessed bytes */
835 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
836 die("Couldn't read from shell: %s\n", SERRNO
);
838 /* process every complete utf8 char */
841 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
842 charsize
= utf8decode(ptr
, &utf8c
);
843 utf8encode(&utf8c
, s
);
849 /* keep any uncomplete utf8 char for the next call */
850 memmove(buf
, ptr
, buflen
);
854 ttywrite(const char *s
, size_t n
) {
855 if(write(cmdfd
, s
, n
) == -1)
856 die("write error on tty: %s\n", SERRNO
);
860 ttyresize(int x
, int y
) {
867 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
868 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
872 tsetdirt(int top
, int bot
)
876 LIMIT(top
, 0, term
.row
-1);
877 LIMIT(bot
, 0, term
.row
-1);
879 for(i
= top
; i
<= bot
; i
++)
886 tsetdirt(0, term
.row
-1);
893 if(mode
== CURSOR_SAVE
)
895 else if(mode
== CURSOR_LOAD
)
896 term
.c
= c
, tmoveto(c
.x
, c
.y
);
906 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
908 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
909 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
911 term
.top
= 0, term
.bot
= term
.row
- 1;
912 term
.mode
= MODE_WRAP
;
913 tclearregion(0, 0, term
.col
-1, term
.row
-1);
917 tnew(int col
, int row
) {
918 /* set screen size */
919 term
.row
= row
, term
.col
= col
;
920 term
.line
= malloc(term
.row
* sizeof(Line
));
921 term
.alt
= malloc(term
.row
* sizeof(Line
));
922 term
.dirty
= malloc(term
.row
* sizeof(*term
.dirty
));
923 term
.tabs
= malloc(term
.col
* sizeof(*term
.tabs
));
925 for(row
= 0; row
< term
.row
; row
++) {
926 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
927 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
930 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
937 Line
* tmp
= term
.line
;
938 term
.line
= term
.alt
;
940 term
.mode
^= MODE_ALTSCREEN
;
945 tscrolldown(int orig
, int n
) {
949 LIMIT(n
, 0, term
.bot
-orig
+1);
951 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
953 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
955 term
.line
[i
] = term
.line
[i
-n
];
956 term
.line
[i
-n
] = temp
;
966 tscrollup(int orig
, int n
) {
969 LIMIT(n
, 0, term
.bot
-orig
+1);
971 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
973 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
975 term
.line
[i
] = term
.line
[i
+n
];
976 term
.line
[i
+n
] = temp
;
986 selscroll(int orig
, int n
) {
990 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
991 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
995 if(sel
.by
< term
.top
) {
999 if(sel
.ey
> term
.bot
) {
1003 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1004 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1009 tnewline(int first_col
) {
1012 tscrollup(term
.top
, 1);
1015 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1020 /* int noarg = 1; */
1021 char *p
= csiescseq
.buf
;
1025 csiescseq
.priv
= 1, p
++;
1027 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1028 while(isdigit(*p
)) {
1029 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1030 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1032 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
1033 csiescseq
.narg
++, p
++;
1035 csiescseq
.mode
= *p
;
1043 tmoveto(int x
, int y
) {
1044 LIMIT(x
, 0, term
.col
-1);
1045 LIMIT(y
, 0, term
.row
-1);
1046 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1053 term
.dirty
[term
.c
.y
] = 1;
1054 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1055 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1056 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1060 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1064 temp
= x1
, x1
= x2
, x2
= temp
;
1066 temp
= y1
, y1
= y2
, y2
= temp
;
1068 LIMIT(x1
, 0, term
.col
-1);
1069 LIMIT(x2
, 0, term
.col
-1);
1070 LIMIT(y1
, 0, term
.row
-1);
1071 LIMIT(y2
, 0, term
.row
-1);
1073 for(y
= y1
; y
<= y2
; y
++) {
1075 for(x
= x1
; x
<= x2
; x
++)
1076 term
.line
[y
][x
].state
= 0;
1081 tdeletechar(int n
) {
1082 int src
= term
.c
.x
+ n
;
1084 int size
= term
.col
- src
;
1086 term
.dirty
[term
.c
.y
] = 1;
1088 if(src
>= term
.col
) {
1089 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1092 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1093 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1097 tinsertblank(int n
) {
1100 int size
= term
.col
- dst
;
1102 term
.dirty
[term
.c
.y
] = 1;
1104 if(dst
>= term
.col
) {
1105 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1108 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1109 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1113 tinsertblankline(int n
) {
1114 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1117 tscrolldown(term
.c
.y
, n
);
1121 tdeleteline(int n
) {
1122 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1125 tscrollup(term
.c
.y
, n
);
1129 tsetattr(int *attr
, int l
) {
1132 for(i
= 0; i
< l
; i
++) {
1135 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1137 term
.c
.attr
.fg
= DefaultFG
;
1138 term
.c
.attr
.bg
= DefaultBG
;
1141 term
.c
.attr
.mode
|= ATTR_BOLD
;
1143 case 3: /* enter standout (highlight) */
1144 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1147 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1150 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1153 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1155 case 23: /* leave standout (highlight) mode */
1156 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1159 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1162 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1165 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1167 if(BETWEEN(attr
[i
], 0, 255))
1168 term
.c
.attr
.fg
= attr
[i
];
1170 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1173 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1176 term
.c
.attr
.fg
= DefaultFG
;
1179 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1181 if(BETWEEN(attr
[i
], 0, 255))
1182 term
.c
.attr
.bg
= attr
[i
];
1184 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1187 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1190 term
.c
.attr
.bg
= DefaultBG
;
1193 if(BETWEEN(attr
[i
], 30, 37))
1194 term
.c
.attr
.fg
= attr
[i
] - 30;
1195 else if(BETWEEN(attr
[i
], 40, 47))
1196 term
.c
.attr
.bg
= attr
[i
] - 40;
1197 else if(BETWEEN(attr
[i
], 90, 97))
1198 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1199 else if(BETWEEN(attr
[i
], 100, 107))
1200 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
1202 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1209 tsetscroll(int t
, int b
) {
1212 LIMIT(t
, 0, term
.row
-1);
1213 LIMIT(b
, 0, term
.row
-1);
1223 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1226 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1229 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1233 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1235 case 5: /* DECSCNM -- Reverve video */
1237 MODBIT(term
.mode
,set
, MODE_REVERSE
);
1238 if(mode
!= term
.mode
)
1242 MODBIT(term
.mode
, set
, MODE_WRAP
);
1245 MODBIT(term
.mode
, set
, MODE_CRLF
);
1247 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1250 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1252 case 1000: /* 1000,1002: enable xterm mouse report */
1253 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1256 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1258 case 1049: /* = 1047 and 1048 */
1261 if(IS_SET(MODE_ALTSCREEN
))
1262 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1263 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1264 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1271 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1275 "erresc: unknown private set/reset mode %d\n",
1282 MODBIT(term
.mode
, set
, MODE_INSERT
);
1286 "erresc: unknown set/reset mode %d\n",
1298 switch(csiescseq
.mode
) {
1301 fprintf(stderr
, "erresc: unknown csi ");
1305 case '@': /* ICH -- Insert <n> blank char */
1306 DEFAULT(csiescseq
.arg
[0], 1);
1307 tinsertblank(csiescseq
.arg
[0]);
1309 case 'A': /* CUU -- Cursor <n> Up */
1311 DEFAULT(csiescseq
.arg
[0], 1);
1312 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1314 case 'B': /* CUD -- Cursor <n> Down */
1315 DEFAULT(csiescseq
.arg
[0], 1);
1316 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1318 case 'C': /* CUF -- Cursor <n> Forward */
1320 DEFAULT(csiescseq
.arg
[0], 1);
1321 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1323 case 'D': /* CUB -- Cursor <n> Backward */
1324 DEFAULT(csiescseq
.arg
[0], 1);
1325 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1327 case 'E': /* CNL -- Cursor <n> Down and first col */
1328 DEFAULT(csiescseq
.arg
[0], 1);
1329 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1331 case 'F': /* CPL -- Cursor <n> Up and first col */
1332 DEFAULT(csiescseq
.arg
[0], 1);
1333 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1335 case 'g': /* TBC -- Tabulation clear */
1336 switch (csiescseq
.arg
[0]) {
1337 case 0: /* clear current tab stop */
1338 term
.tabs
[term
.c
.x
] = 0;
1340 case 3: /* clear all the tabs */
1341 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1347 case 'G': /* CHA -- Move to <col> */
1349 DEFAULT(csiescseq
.arg
[0], 1);
1350 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1352 case 'H': /* CUP -- Move to <row> <col> */
1354 DEFAULT(csiescseq
.arg
[0], 1);
1355 DEFAULT(csiescseq
.arg
[1], 1);
1356 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1358 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1359 DEFAULT(csiescseq
.arg
[0], 1);
1360 while(csiescseq
.arg
[0]--)
1363 case 'J': /* ED -- Clear screen */
1365 switch(csiescseq
.arg
[0]) {
1367 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1368 if(term
.c
.y
< term
.row
-1)
1369 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1373 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1374 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1377 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1383 case 'K': /* EL -- Clear line */
1384 switch(csiescseq
.arg
[0]) {
1386 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1389 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1392 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1396 case 'S': /* SU -- Scroll <n> line up */
1397 DEFAULT(csiescseq
.arg
[0], 1);
1398 tscrollup(term
.top
, csiescseq
.arg
[0]);
1400 case 'T': /* SD -- Scroll <n> line down */
1401 DEFAULT(csiescseq
.arg
[0], 1);
1402 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1404 case 'L': /* IL -- Insert <n> blank lines */
1405 DEFAULT(csiescseq
.arg
[0], 1);
1406 tinsertblankline(csiescseq
.arg
[0]);
1408 case 'l': /* RM -- Reset Mode */
1409 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1411 case 'M': /* DL -- Delete <n> lines */
1412 DEFAULT(csiescseq
.arg
[0], 1);
1413 tdeleteline(csiescseq
.arg
[0]);
1415 case 'X': /* ECH -- Erase <n> char */
1416 DEFAULT(csiescseq
.arg
[0], 1);
1417 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1419 case 'P': /* DCH -- Delete <n> char */
1420 DEFAULT(csiescseq
.arg
[0], 1);
1421 tdeletechar(csiescseq
.arg
[0]);
1423 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1424 DEFAULT(csiescseq
.arg
[0], 1);
1425 while(csiescseq
.arg
[0]--)
1428 case 'd': /* VPA -- Move to <row> */
1429 DEFAULT(csiescseq
.arg
[0], 1);
1430 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1432 case 'h': /* SM -- Set terminal mode */
1433 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1435 case 'm': /* SGR -- Terminal attribute (color) */
1436 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1438 case 'r': /* DECSTBM -- Set Scrolling Region */
1442 DEFAULT(csiescseq
.arg
[0], 1);
1443 DEFAULT(csiescseq
.arg
[1], term
.row
);
1444 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1448 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1449 tcursor(CURSOR_SAVE
);
1451 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1452 tcursor(CURSOR_LOAD
);
1461 for(i
= 0; i
< csiescseq
.len
; i
++) {
1462 uint c
= csiescseq
.buf
[i
] & 0xff;
1463 if(isprint(c
)) putchar(c
);
1464 else if(c
== '\n') printf("(\\n)");
1465 else if(c
== '\r') printf("(\\r)");
1466 else if(c
== 0x1b) printf("(\\e)");
1467 else printf("(%02x)", c
);
1474 memset(&csiescseq
, 0, sizeof(csiescseq
));
1482 * TODO: make this being useful in case of color palette change.
1488 switch(strescseq
.type
) {
1489 case ']': /* OSC -- Operating System Command */
1495 * TODO: Handle special chars in string, like umlauts.
1498 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1502 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1504 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1507 fprintf(stderr
, "erresc: unknown str ");
1512 case 'P': /* DSC -- Device Control String */
1513 case '_': /* APC -- Application Program Command */
1514 case '^': /* PM -- Privacy Message */
1516 fprintf(stderr
, "erresc: unknown str ");
1526 * TODO: Implement parsing like for CSI when required.
1527 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1535 printf("ESC%c", strescseq
.type
);
1536 for(i
= 0; i
< strescseq
.len
; i
++) {
1537 uint c
= strescseq
.buf
[i
] & 0xff;
1538 if(isprint(c
)) putchar(c
);
1539 else if(c
== '\n') printf("(\\n)");
1540 else if(c
== '\r') printf("(\\r)");
1541 else if(c
== 0x1b) printf("(\\e)");
1542 else printf("(%02x)", c
);
1549 memset(&strescseq
, 0, sizeof(strescseq
));
1553 tputtab(bool forward
) {
1554 unsigned x
= term
.c
.x
;
1559 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1564 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1567 tmoveto(x
, term
.c
.y
);
1575 putc(ascii
, fileio
);
1579 if(term
.esc
& ESC_START
) {
1580 if(term
.esc
& ESC_CSI
) {
1581 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1582 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1584 csiparse(), csihandle();
1586 } else if(term
.esc
& ESC_STR
) {
1589 term
.esc
= ESC_START
| ESC_STR_END
;
1591 case '\a': /* backwards compatibility to xterm */
1596 strescseq
.buf
[strescseq
.len
++] = ascii
;
1597 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1602 } else if(term
.esc
& ESC_STR_END
) {
1606 } else if(term
.esc
& ESC_ALTCHARSET
) {
1608 case '0': /* Line drawing crap */
1609 term
.c
.attr
.mode
|= ATTR_GFX
;
1611 case 'B': /* Back to regular text */
1612 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1615 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1621 term
.esc
|= ESC_CSI
;
1623 case 'P': /* DCS -- Device Control String */
1624 case '_': /* APC -- Application Program Command */
1625 case '^': /* PM -- Privacy Message */
1626 case ']': /* OSC -- Operating System Command */
1628 strescseq
.type
= ascii
;
1629 term
.esc
|= ESC_STR
;
1632 term
.esc
|= ESC_ALTCHARSET
;
1634 case 'D': /* IND -- Linefeed */
1635 if(term
.c
.y
== term
.bot
)
1636 tscrollup(term
.top
, 1);
1638 tmoveto(term
.c
.x
, term
.c
.y
+1);
1641 case 'E': /* NEL -- Next line */
1642 tnewline(1); /* always go to first col */
1645 case 'H': /* HTS -- Horizontal tab stop */
1646 term
.tabs
[term
.c
.x
] = 1;
1649 case 'M': /* RI -- Reverse index */
1650 if(term
.c
.y
== term
.top
)
1651 tscrolldown(term
.top
, 1);
1653 tmoveto(term
.c
.x
, term
.c
.y
-1);
1656 case 'c': /* RIS -- Reset to inital state */
1660 case '=': /* DECPAM -- Application keypad */
1661 term
.mode
|= MODE_APPKEYPAD
;
1664 case '>': /* DECPNM -- Normal keypad */
1665 term
.mode
&= ~MODE_APPKEYPAD
;
1668 case '7': /* DECSC -- Save Cursor */
1669 tcursor(CURSOR_SAVE
);
1672 case '8': /* DECRC -- Restore Cursor */
1673 tcursor(CURSOR_LOAD
);
1676 case '\\': /* ST -- Stop */
1680 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1681 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1686 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1693 tmoveto(term
.c
.x
-1, term
.c
.y
);
1696 tmoveto(0, term
.c
.y
);
1701 /* go to first col if the mode is set */
1702 tnewline(IS_SET(MODE_CRLF
));
1705 if(!(xw
.state
& WIN_FOCUSED
))
1710 term
.esc
= ESC_START
;
1713 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1714 tnewline(1); /* always go to first col */
1716 if(term
.c
.x
+1 < term
.col
)
1717 tmoveto(term
.c
.x
+1, term
.c
.y
);
1719 term
.c
.state
|= CURSOR_WRAPNEXT
;
1725 tresize(int col
, int row
) {
1727 int minrow
= MIN(row
, term
.row
);
1728 int mincol
= MIN(col
, term
.col
);
1729 int slide
= term
.c
.y
- row
+ 1;
1731 if(col
< 1 || row
< 1)
1734 /* free unneeded rows */
1737 /* slide screen to keep cursor where we expect it -
1738 * tscrollup would work here, but we can optimize to
1739 * memmove because we're freeing the earlier lines */
1740 for(/* i = 0 */; i
< slide
; i
++) {
1744 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1745 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1747 for(i
+= row
; i
< term
.row
; i
++) {
1752 /* resize to new height */
1753 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1754 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1755 term
.dirty
= realloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1756 term
.tabs
= realloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1758 /* resize each row to new width, zero-pad if needed */
1759 for(i
= 0; i
< minrow
; i
++) {
1761 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1762 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1763 for(x
= mincol
; x
< col
; x
++) {
1764 term
.line
[i
][x
].state
= 0;
1765 term
.alt
[i
][x
].state
= 0;
1769 /* allocate any new rows */
1770 for(/* i == minrow */; i
< row
; i
++) {
1772 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1773 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1775 if(col
> term
.col
) {
1776 bool *bp
= term
.tabs
+ term
.col
;
1778 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1779 while(--bp
> term
.tabs
&& !*bp
)
1781 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1784 /* update terminal size */
1785 term
.col
= col
, term
.row
= row
;
1786 /* make use of the LIMIT in tmoveto */
1787 tmoveto(term
.c
.x
, term
.c
.y
);
1788 /* reset scrolling region */
1789 tsetscroll(0, row
-1);
1795 xresize(int col
, int row
) {
1796 xw
.w
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1797 xw
.h
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1804 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1806 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1807 for(i
= 0; i
< LEN(colorname
); i
++) {
1810 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1812 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1814 dc
.col
[i
] = color
.pixel
;
1817 /* load colors [16-255] ; same colors as xterm */
1818 for(i
= 16, r
= 0; r
< 6; r
++)
1819 for(g
= 0; g
< 6; g
++)
1820 for(b
= 0; b
< 6; b
++) {
1821 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1822 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1823 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1824 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1826 fprintf(stderr
, "Could not allocate color %d\n", i
);
1828 dc
.col
[i
] = color
.pixel
;
1832 for(r
= 0; r
< 24; r
++, i
++) {
1833 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1834 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1836 fprintf(stderr
, "Could not allocate color %d\n", i
);
1838 dc
.col
[i
] = color
.pixel
;
1843 xclear(int x1
, int y1
, int x2
, int y2
) {
1844 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1845 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1846 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1847 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1852 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1853 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1854 XSizeHints
*sizeh
= NULL
;
1856 sizeh
= XAllocSizeHints();
1857 if(xw
.isfixed
== False
) {
1858 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
1859 sizeh
->height
= xw
.h
;
1860 sizeh
->width
= xw
.w
;
1861 sizeh
->height_inc
= xw
.ch
;
1862 sizeh
->width_inc
= xw
.cw
;
1863 sizeh
->base_height
= 2*BORDER
;
1864 sizeh
->base_width
= 2*BORDER
;
1866 sizeh
->flags
= PMaxSize
| PMinSize
;
1867 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
1868 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
1871 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
1876 xinitfont(char *fontstr
) {
1878 char *def
, **missing
;
1882 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1885 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1886 XFreeStringList(missing
);
1892 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1893 XFontStruct
**xfonts
;
1897 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1898 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1899 for(i
= 0; i
< n
; i
++) {
1900 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1901 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1902 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1903 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1909 initfonts(char *fontstr
, char *bfontstr
, char *ifontstr
) {
1910 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
)
1911 die("Can't load font %s\n", fontstr
);
1912 if((dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1913 die("Can't load bfont %s\n", bfontstr
);
1914 if((dc
.ifont
.set
= xinitfont(ifontstr
)) == NULL
)
1915 die("Can't load ifont %s\n", ifontstr
);
1917 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1918 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1919 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1920 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1921 xgetfontinfo(dc
.ifont
.set
, &dc
.ifont
.ascent
, &dc
.ifont
.descent
,
1922 &dc
.ifont
.lbearing
, &dc
.ifont
.rbearing
);
1927 XSetWindowAttributes attrs
;
1932 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1933 die("Can't open display\n");
1934 xw
.scr
= XDefaultScreen(xw
.dpy
);
1937 initfonts(FONT
, BOLDFONT
, ITALICFONT
);
1939 /* XXX: Assuming same size for bold font */
1940 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1941 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1944 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1947 /* adjust fixed window geometry */
1949 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
1950 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
1952 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
1954 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
1959 /* window - default size */
1960 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
1961 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
1966 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1967 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1968 attrs
.bit_gravity
= NorthWestGravity
;
1969 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1970 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1971 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1972 attrs
.colormap
= xw
.cmap
;
1974 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
1975 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
1976 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1977 XDefaultVisual(xw
.dpy
, xw
.scr
),
1978 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1981 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
1985 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1986 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1987 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1988 XNFocusWindow
, xw
.win
, NULL
);
1990 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1992 /* white cursor, black outline */
1993 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1994 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1995 XRecolorCursor(xw
.dpy
, cursor
,
1996 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1997 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1999 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2001 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2002 XMapWindow(xw
.dpy
, xw
.win
);
2008 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2009 int fg
= base
.fg
, bg
= base
.bg
, temp
;
2010 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
2011 XFontSet fontset
= dc
.font
.set
;
2014 /* only switch default fg/bg if term is in RV mode */
2015 if(IS_SET(MODE_REVERSE
)) {
2022 if(base
.mode
& ATTR_REVERSE
)
2023 temp
= fg
, fg
= bg
, bg
= temp
;
2025 if(base
.mode
& ATTR_BOLD
) {
2027 fontset
= dc
.bfont
.set
;
2030 if(base
.mode
& ATTR_ITALIC
)
2031 fontset
= dc
.ifont
.set
;
2033 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
2034 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
2036 if(base
.mode
& ATTR_GFX
) {
2037 for(i
= 0; i
< bytelen
; i
++) {
2038 char c
= gfx
[(uint
)s
[i
] % 256];
2041 else if(s
[i
] > 0x5f)
2046 XmbDrawImageString(xw
.dpy
, xw
.buf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
2048 if(base
.mode
& ATTR_UNDERLINE
)
2049 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
2052 /* copy buffer pixmap to screen pixmap */
2055 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2056 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2061 static int oldx
= 0;
2062 static int oldy
= 0;
2064 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2066 LIMIT(oldx
, 0, term
.col
-1);
2067 LIMIT(oldy
, 0, term
.row
-1);
2069 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2070 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2072 /* remove the old cursor */
2073 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2074 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2075 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2077 xclear(oldx
, oldy
, oldx
, oldy
);
2081 /* draw the new one */
2082 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2083 if(!(xw
.state
& WIN_FOCUSED
))
2086 if(IS_SET(MODE_REVERSE
))
2087 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2090 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2091 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2099 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2102 nanosleep(&tv
, NULL
);
2107 drawregion(0, 0, term
.col
, term
.row
);
2109 gettimeofday(&xw
.lastdraw
, NULL
);
2113 drawregion(int x1
, int y1
, int x2
, int y2
) {
2114 int ic
, ib
, x
, y
, ox
, sl
;
2116 char buf
[DRAW_BUF_SIZ
];
2117 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2119 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2121 if(!(xw
.state
& WIN_VISIBLE
))
2124 for(y
= y1
; y
< y2
; y
++) {
2127 xclear(0, y
, term
.col
, y
);
2129 base
= term
.line
[y
][0];
2131 for(x
= x1
; x
< x2
; x
++) {
2132 new = term
.line
[y
][x
];
2133 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2134 new.mode
^= ATTR_REVERSE
;
2135 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2136 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2137 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2140 if(new.state
& GLYPH_SET
) {
2145 sl
= utf8size(new.c
);
2146 memcpy(buf
+ib
, new.c
, sl
);
2152 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2158 expose(XEvent
*ev
) {
2159 XExposeEvent
*e
= &ev
->xexpose
;
2160 if(xw
.state
& WIN_REDRAW
) {
2162 xw
.state
&= ~WIN_REDRAW
;
2168 visibility(XEvent
*ev
) {
2169 XVisibilityEvent
*e
= &ev
->xvisibility
;
2170 if(e
->state
== VisibilityFullyObscured
)
2171 xw
.state
&= ~WIN_VISIBLE
;
2172 else if(!(xw
.state
& WIN_VISIBLE
))
2173 /* need a full redraw for next Expose, not just a buf copy */
2174 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2179 xw
.state
&= ~WIN_VISIBLE
;
2183 xseturgency(int add
) {
2184 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2185 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2186 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2192 if(ev
->type
== FocusIn
) {
2193 xw
.state
|= WIN_FOCUSED
;
2196 xw
.state
&= ~WIN_FOCUSED
;
2201 kmap(KeySym k
, uint state
) {
2204 for(i
= 0; i
< LEN(key
); i
++) {
2205 uint mask
= key
[i
].mask
;
2206 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2207 return (char*)key
[i
].s
;
2213 kpress(XEvent
*ev
) {
2214 XKeyEvent
*e
= &ev
->xkey
;
2223 meta
= e
->state
& Mod1Mask
;
2224 shift
= e
->state
& ShiftMask
;
2225 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2227 /* 1. custom keys from config.h */
2228 if((customkey
= kmap(ksym
, e
->state
)))
2229 ttywrite(customkey
, strlen(customkey
));
2230 /* 2. hardcoded (overrides X lookup) */
2237 /* XXX: shift up/down doesn't work */
2238 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2246 if(IS_SET(MODE_CRLF
))
2247 ttywrite("\r\n", 2);
2254 if(meta
&& len
== 1)
2255 ttywrite("\033", 1);
2263 cmessage(XEvent
*e
) {
2265 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2266 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2267 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2268 xw
.state
|= WIN_FOCUSED
;
2270 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2271 xw
.state
&= ~WIN_FOCUSED
;
2281 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2284 xw
.w
= e
->xconfigure
.width
;
2285 xw
.h
= e
->xconfigure
.height
;
2286 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2287 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2288 if(col
== term
.col
&& row
== term
.row
)
2290 if(tresize(col
, row
))
2293 ttyresize(col
, row
);
2297 last_draw_too_old(void) {
2299 gettimeofday(&now
, NULL
);
2300 return TIMEDIFF(now
, xw
.lastdraw
) >= DRAW_TIMEOUT
/1000;
2307 int xfd
= XConnectionNumber(xw
.dpy
);
2308 struct timeval timeout
= {0};
2309 bool stuff_to_print
= 0;
2313 FD_SET(cmdfd
, &rfd
);
2316 timeout
.tv_usec
= SELECT_TIMEOUT
;
2317 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
2320 die("select failed: %s\n", SERRNO
);
2322 if(FD_ISSET(cmdfd
, &rfd
)) {
2327 if(stuff_to_print
&& last_draw_too_old()) {
2332 while(XPending(xw
.dpy
)) {
2333 XNextEvent(xw
.dpy
, &ev
);
2334 if(XFilterEvent(&ev
, xw
.win
))
2336 if(handler
[ev
.type
])
2337 (handler
[ev
.type
])(&ev
);
2343 main(int argc
, char *argv
[]) {
2344 int i
, bitm
, xr
, yr
;
2345 unsigned int wr
, hr
;
2347 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2350 for(i
= 1; i
< argc
; i
++) {
2351 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2353 if(++i
< argc
) opt_title
= argv
[i
];
2356 if(++i
< argc
) opt_class
= argv
[i
];
2359 if(++i
< argc
) opt_embed
= argv
[i
];
2362 if(++i
< argc
) opt_io
= argv
[i
];
2365 /* eat every remaining arguments */
2366 if(++i
< argc
) opt_cmd
= &argv
[i
];
2372 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2377 if(bitm
& WidthValue
)
2379 if(bitm
& HeightValue
)
2381 if(bitm
& XNegative
&& xw
.fx
== 0)
2383 if(bitm
& XNegative
&& xw
.fy
== 0)
2386 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2396 setlocale(LC_CTYPE
, "");