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>
28 #include <X11/Xft/Xft.h>
34 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
36 #elif defined(__FreeBSD__) || defined(__DragonFly__)
41 "st " VERSION " (c) 2010-2012 st engineers\n" \
42 "usage: st [-t title] [-c class] [-g geometry]" \
43 " [-w windowid] [-v] [-f file] [-e command...]\n"
46 #define XEMBED_FOCUS_IN 4
47 #define XEMBED_FOCUS_OUT 5
50 #define ESC_BUF_SIZ 256
51 #define ESC_ARG_SIZ 16
52 #define STR_BUF_SIZ 256
53 #define STR_ARG_SIZ 16
54 #define DRAW_BUF_SIZ 20*1024
56 #define XK_NO_MOD UINT_MAX
59 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
61 #define SERRNO strerror(errno)
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63 #define MAX(a, b) ((a) < (b) ? (b) : (a))
64 #define LEN(a) (sizeof(a) / sizeof(a[0]))
65 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
66 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
67 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
68 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
69 #define IS_SET(flag) (term.mode & (flag))
70 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
71 #define X2COL(x) (((x) - BORDER)/xw.cw)
72 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
74 enum glyph_attribute
{
84 enum cursor_movement
{
111 MODE_MOUSEMOTION
= 64,
120 ESC_STR
= 4, /* DSC, OSC, PM, APC */
122 ESC_STR_END
= 16, /* a final string was encountered */
133 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
135 typedef unsigned char uchar
;
136 typedef unsigned int uint
;
137 typedef unsigned long ulong
;
138 typedef unsigned short ushort
;
141 char c
[UTF_SIZ
]; /* character code */
142 uchar mode
; /* attribute flags */
143 ushort fg
; /* foreground */
144 ushort bg
; /* background */
145 uchar state
; /* state flags */
151 Glyph attr
; /* current char attributes */
157 /* CSI Escape sequence structs */
158 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
160 char buf
[ESC_BUF_SIZ
]; /* raw string */
161 int len
; /* raw string length */
163 int arg
[ESC_ARG_SIZ
];
164 int narg
; /* nb of args */
168 /* STR Escape sequence structs */
169 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
171 char type
; /* ESC type ... */
172 char buf
[STR_BUF_SIZ
]; /* raw string */
173 int len
; /* raw string length */
174 char *args
[STR_ARG_SIZ
];
175 int narg
; /* nb of args */
178 /* Internal representation of the screen */
180 int row
; /* nb row */
181 int col
; /* nb col */
182 Line
* line
; /* screen */
183 Line
* alt
; /* alternate screen */
184 bool* dirty
; /* dirtyness of lines */
185 TCursor c
; /* cursor */
186 int top
; /* top scroll limit */
187 int bot
; /* bottom scroll limit */
188 int mode
; /* terminal mode flags */
189 int esc
; /* escape state flags */
193 /* Purely graphic info */
205 bool isfixed
; /* is fixed geometry? */
206 int fx
, fy
, fw
, fh
; /* fixed geometry */
207 int tw
, th
; /* tty width and height */
208 int w
; /* window width */
209 int h
; /* window height */
210 int ch
; /* char height */
211 int cw
; /* char width */
212 char state
; /* focus, redraw, visible */
221 /* TODO: use better name for vars... */
226 struct {int x
, y
;} b
, e
;
230 struct timeval tclick1
;
231 struct timeval tclick2
;
246 /* Drawing Context */
248 ulong col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
249 XftColor xft_col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
251 Font font
, bfont
, ifont
, ibfont
;
254 static void die(const char*, ...);
255 static void draw(void);
256 static void redraw(void);
257 static void drawregion(int, int, int, int);
258 static void execsh(void);
259 static void sigchld(int);
260 static void run(void);
262 static void csidump(void);
263 static void csihandle(void);
264 static void csiparse(void);
265 static void csireset(void);
266 static void strdump(void);
267 static void strhandle(void);
268 static void strparse(void);
269 static void strreset(void);
271 static void tclearregion(int, int, int, int);
272 static void tcursor(int);
273 static void tdeletechar(int);
274 static void tdeleteline(int);
275 static void tinsertblank(int);
276 static void tinsertblankline(int);
277 static void tmoveto(int, int);
278 static void tnew(int, int);
279 static void tnewline(int);
280 static void tputtab(bool);
281 static void tputc(char*, int);
282 static void treset(void);
283 static int tresize(int, int);
284 static void tscrollup(int, int);
285 static void tscrolldown(int, int);
286 static void tsetattr(int*, int);
287 static void tsetchar(char*);
288 static void tsetscroll(int, int);
289 static void tswapscreen(void);
290 static void tsetdirt(int, int);
291 static void tsetmode(bool, bool, int *, int);
292 static void tfulldirt(void);
294 static void ttynew(void);
295 static void ttyread(void);
296 static void ttyresize(void);
297 static void ttywrite(const char *, size_t);
299 static void xdraws(char *, Glyph
, int, int, int, int);
300 static void xhints(void);
301 static void xclear(int, int, int, int);
302 static void xdrawcursor(void);
303 static void xinit(void);
304 static void xloadcols(void);
305 static void xresettitle(void);
306 static void xseturgency(int);
307 static void xsetsel(char*);
308 static void xresize(int, int);
310 static void expose(XEvent
*);
311 static void visibility(XEvent
*);
312 static void unmap(XEvent
*);
313 static char* kmap(KeySym
, uint
);
314 static void kpress(XEvent
*);
315 static void cmessage(XEvent
*);
316 static void resize(XEvent
*);
317 static void focus(XEvent
*);
318 static void brelease(XEvent
*);
319 static void bpress(XEvent
*);
320 static void bmotion(XEvent
*);
321 static void selnotify(XEvent
*);
322 static void selclear(XEvent
*);
323 static void selrequest(XEvent
*);
325 static void selinit(void);
326 static inline bool selected(int, int);
327 static void selcopy(void);
328 static void selpaste(void);
329 static void selscroll(int, int);
331 static int utf8decode(char *, long *);
332 static int utf8encode(long *, char *);
333 static int utf8size(char *);
334 static int isfullutf8(char *, int);
336 static void *xmalloc(size_t);
337 static void *xrealloc(void *, size_t);
338 static void *xcalloc(size_t nmemb
, size_t size
);
340 static void (*handler
[LASTEvent
])(XEvent
*) = {
342 [ClientMessage
] = cmessage
,
343 [ConfigureNotify
] = resize
,
344 [VisibilityNotify
] = visibility
,
345 [UnmapNotify
] = unmap
,
349 [MotionNotify
] = bmotion
,
350 [ButtonPress
] = bpress
,
351 [ButtonRelease
] = brelease
,
352 [SelectionClear
] = selclear
,
353 [SelectionNotify
] = selnotify
,
354 [SelectionRequest
] = selrequest
,
361 static CSIEscape csiescseq
;
362 static STREscape strescseq
;
365 static Selection sel
;
366 static int iofd
= -1;
367 static char **opt_cmd
= NULL
;
368 static char *opt_io
= NULL
;
369 static char *opt_title
= NULL
;
370 static char *opt_embed
= NULL
;
371 static char *opt_class
= NULL
;
374 xmalloc(size_t len
) {
375 void *p
= malloc(len
);
377 die("Out of memory\n");
382 xrealloc(void *p
, size_t len
) {
383 if((p
= realloc(p
, len
)) == NULL
)
384 die("Out of memory\n");
389 xcalloc(size_t nmemb
, size_t size
) {
390 void *p
= calloc(nmemb
, size
);
392 die("Out of memory\n");
397 utf8decode(char *s
, long *u
) {
403 if(~c
& B7
) { /* 0xxxxxxx */
406 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
407 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
409 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
410 *u
= c
&(B3
|B2
|B1
|B0
);
412 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
417 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
419 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
422 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
424 if((n
== 1 && *u
< 0x80) ||
425 (n
== 2 && *u
< 0x800) ||
426 (n
== 3 && *u
< 0x10000) ||
427 (*u
>= 0xD800 && *u
<= 0xDFFF))
436 utf8encode(long *u
, char *s
) {
444 *sp
= uc
; /* 0xxxxxxx */
446 } else if(*u
< 0x800) {
447 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
449 } else if(uc
< 0x10000) {
450 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
452 } else if(uc
<= 0x10FFFF) {
453 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
458 for(i
=n
,++sp
; i
>0; --i
,++sp
)
459 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
469 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
470 UTF-8 otherwise return 0 */
472 isfullutf8(char *s
, int b
) {
480 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
482 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
484 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
486 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
488 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
489 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
501 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
503 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
511 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
512 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
516 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
517 if(sel
.xtarget
== None
)
518 sel
.xtarget
= XA_STRING
;
522 selected(int x
, int y
) {
523 if(sel
.ey
== y
&& sel
.by
== y
) {
524 int bx
= MIN(sel
.bx
, sel
.ex
);
525 int ex
= MAX(sel
.bx
, sel
.ex
);
526 return BETWEEN(x
, bx
, ex
);
528 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
529 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
533 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
535 *b
= e
->xbutton
.button
;
537 *x
= X2COL(e
->xbutton
.x
);
538 *y
= Y2ROW(e
->xbutton
.y
);
539 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
540 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
541 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
542 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
546 mousereport(XEvent
*e
) {
547 int x
= X2COL(e
->xbutton
.x
);
548 int y
= Y2ROW(e
->xbutton
.y
);
549 int button
= e
->xbutton
.button
;
550 int state
= e
->xbutton
.state
;
551 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
552 static int ob
, ox
, oy
;
555 if(e
->xbutton
.type
== MotionNotify
) {
556 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
560 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
566 if(e
->xbutton
.type
== ButtonPress
) {
572 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
573 + (state
& Mod4Mask
? 8 : 0)
574 + (state
& ControlMask
? 16 : 0);
576 ttywrite(buf
, sizeof(buf
));
581 if(IS_SET(MODE_MOUSE
))
583 else if(e
->xbutton
.button
== Button1
) {
586 tsetdirt(sel
.b
.y
, sel
.e
.y
);
590 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
591 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
598 int x
, y
, bufsize
, is_selected
= 0;
604 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
605 ptr
= str
= xmalloc(bufsize
);
607 /* append every set & selected glyph to the selection */
608 for(y
= 0; y
< term
.row
; y
++) {
609 for(x
= 0; x
< term
.col
; x
++) {
612 Glyph
*gp
= &term
.line
[y
][x
];
614 if(!(is_selected
= selected(x
, y
)))
616 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
618 memcpy(ptr
, p
, size
);
621 /* \n at the end of every selected line except for the last one */
622 if(is_selected
&& y
< sel
.e
.y
)
627 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
632 selnotify(XEvent
*e
) {
633 ulong nitems
, ofs
, rem
;
640 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
641 False
, AnyPropertyType
, &type
, &format
,
642 &nitems
, &rem
, &data
)) {
643 fprintf(stderr
, "Clipboard allocation failed\n");
646 ttywrite((const char *) data
, nitems
* format
/ 8);
648 /* number of 32-bit chunks returned */
649 ofs
+= nitems
* format
/ 32;
655 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
658 void selclear(XEvent
*e
) {
662 tsetdirt(sel
.b
.y
, sel
.e
.y
);
666 selrequest(XEvent
*e
) {
667 XSelectionRequestEvent
*xsre
;
671 xsre
= (XSelectionRequestEvent
*) e
;
672 xev
.type
= SelectionNotify
;
673 xev
.requestor
= xsre
->requestor
;
674 xev
.selection
= xsre
->selection
;
675 xev
.target
= xsre
->target
;
676 xev
.time
= xsre
->time
;
680 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
681 if(xsre
->target
== xa_targets
) {
682 /* respond with the supported type */
683 Atom string
= sel
.xtarget
;
684 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
685 XA_ATOM
, 32, PropModeReplace
,
686 (uchar
*) &string
, 1);
687 xev
.property
= xsre
->property
;
688 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
689 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
690 xsre
->target
, 8, PropModeReplace
,
691 (uchar
*) sel
.clip
, strlen(sel
.clip
));
692 xev
.property
= xsre
->property
;
695 /* all done, send a notification to the listener */
696 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
697 fprintf(stderr
, "Error sending SelectionNotify event\n");
702 /* register the selection for both the clipboard and the primary */
708 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
710 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
711 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
715 brelease(XEvent
*e
) {
716 if(IS_SET(MODE_MOUSE
)) {
720 if(e
->xbutton
.button
== Button2
)
722 else if(e
->xbutton
.button
== Button1
) {
724 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
725 term
.dirty
[sel
.ey
] = 1;
726 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
729 gettimeofday(&now
, NULL
);
731 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
732 /* triple click on the line */
733 sel
.b
.x
= sel
.bx
= 0;
734 sel
.e
.x
= sel
.ex
= term
.col
;
735 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
737 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
738 /* double click to select word */
740 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
741 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
743 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
744 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
746 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
752 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
753 gettimeofday(&sel
.tclick1
, NULL
);
758 if(IS_SET(MODE_MOUSE
)) {
763 int oldey
= sel
.ey
, oldex
= sel
.ex
;
764 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
766 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
767 int starty
= MIN(oldey
, sel
.ey
);
768 int endy
= MAX(oldey
, sel
.ey
);
769 tsetdirt(starty
, endy
);
775 die(const char *errstr
, ...) {
778 va_start(ap
, errstr
);
779 vfprintf(stderr
, errstr
, ap
);
787 char *envshell
= getenv("SHELL");
793 signal(SIGCHLD
, SIG_DFL
);
794 signal(SIGHUP
, SIG_DFL
);
795 signal(SIGINT
, SIG_DFL
);
796 signal(SIGQUIT
, SIG_DFL
);
797 signal(SIGTERM
, SIG_DFL
);
798 signal(SIGALRM
, SIG_DFL
);
800 DEFAULT(envshell
, SHELL
);
801 putenv("TERM="TNAME
);
802 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
803 execvp(args
[0], args
);
810 if(waitpid(pid
, &stat
, 0) < 0)
811 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
813 exit(WEXITSTATUS(stat
));
822 /* seems to work fine on linux, openbsd and freebsd */
823 struct winsize w
= {term
.row
, term
.col
, 0, 0};
824 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
825 die("openpty failed: %s\n", SERRNO
);
827 switch(pid
= fork()) {
829 die("fork failed\n");
832 setsid(); /* create a new process group */
833 dup2(s
, STDIN_FILENO
);
834 dup2(s
, STDOUT_FILENO
);
835 dup2(s
, STDERR_FILENO
);
836 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
837 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
845 signal(SIGCHLD
, sigchld
);
847 if(!strcmp(opt_io
, "-")) {
848 iofd
= STDOUT_FILENO
;
850 if((iofd
= open(opt_io
, O_WRONLY
| O_CREAT
, 0666)) < 0) {
851 fprintf(stderr
, "Error opening %s:%s\n",
852 opt_io
, strerror(errno
));
862 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
864 fprintf(stderr
, "\n");
869 static char buf
[BUFSIZ
];
870 static int buflen
= 0;
873 int charsize
; /* size of utf8 char in bytes */
877 /* append read bytes to unprocessed bytes */
878 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
879 die("Couldn't read from shell: %s\n", SERRNO
);
881 /* process every complete utf8 char */
884 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
885 charsize
= utf8decode(ptr
, &utf8c
);
886 utf8encode(&utf8c
, s
);
892 /* keep any uncomplete utf8 char for the next call */
893 memmove(buf
, ptr
, buflen
);
897 ttywrite(const char *s
, size_t n
) {
898 if(write(cmdfd
, s
, n
) == -1)
899 die("write error on tty: %s\n", SERRNO
);
910 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
911 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
915 tsetdirt(int top
, int bot
)
919 LIMIT(top
, 0, term
.row
-1);
920 LIMIT(bot
, 0, term
.row
-1);
922 for(i
= top
; i
<= bot
; i
++)
929 tsetdirt(0, term
.row
-1);
936 if(mode
== CURSOR_SAVE
)
938 else if(mode
== CURSOR_LOAD
)
939 term
.c
= c
, tmoveto(c
.x
, c
.y
);
949 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
951 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
952 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
954 term
.top
= 0, term
.bot
= term
.row
- 1;
955 term
.mode
= MODE_WRAP
;
957 tclearregion(0, 0, term
.col
-1, term
.row
-1);
961 tnew(int col
, int row
) {
962 /* set screen size */
963 term
.row
= row
, term
.col
= col
;
964 term
.line
= xmalloc(term
.row
* sizeof(Line
));
965 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
966 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
967 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
969 for(row
= 0; row
< term
.row
; row
++) {
970 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
971 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
974 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
981 Line
* tmp
= term
.line
;
982 term
.line
= term
.alt
;
984 term
.mode
^= MODE_ALTSCREEN
;
989 tscrolldown(int orig
, int n
) {
993 LIMIT(n
, 0, term
.bot
-orig
+1);
995 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
997 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
999 term
.line
[i
] = term
.line
[i
-n
];
1000 term
.line
[i
-n
] = temp
;
1003 term
.dirty
[i
-n
] = 1;
1010 tscrollup(int orig
, int n
) {
1013 LIMIT(n
, 0, term
.bot
-orig
+1);
1015 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1017 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1018 temp
= term
.line
[i
];
1019 term
.line
[i
] = term
.line
[i
+n
];
1020 term
.line
[i
+n
] = temp
;
1023 term
.dirty
[i
+n
] = 1;
1026 selscroll(orig
, -n
);
1030 selscroll(int orig
, int n
) {
1034 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1035 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1039 if(sel
.by
< term
.top
) {
1043 if(sel
.ey
> term
.bot
) {
1047 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1048 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1053 tnewline(int first_col
) {
1056 tscrollup(term
.top
, 1);
1059 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1064 /* int noarg = 1; */
1065 char *p
= csiescseq
.buf
;
1069 csiescseq
.priv
= 1, p
++;
1071 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1072 while(isdigit(*p
)) {
1073 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1074 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1076 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
1077 csiescseq
.narg
++, p
++;
1079 csiescseq
.mode
= *p
;
1087 tmoveto(int x
, int y
) {
1088 LIMIT(x
, 0, term
.col
-1);
1089 LIMIT(y
, 0, term
.row
-1);
1090 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1098 * The table is proudly stolen from rxvt.
1100 if(term
.c
.attr
.mode
& ATTR_GFX
) {
1101 char *vt100_0
[62] = { /* 0x41 - 0x7e */
1102 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1103 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1104 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1105 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1106 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1107 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1108 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1109 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1112 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1113 && vt100_0
[c
[0] - 0x41]) {
1114 c
= vt100_0
[c
[0] - 0x41];
1118 term
.dirty
[term
.c
.y
] = 1;
1119 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1120 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1121 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1125 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1129 temp
= x1
, x1
= x2
, x2
= temp
;
1131 temp
= y1
, y1
= y2
, y2
= temp
;
1133 LIMIT(x1
, 0, term
.col
-1);
1134 LIMIT(x2
, 0, term
.col
-1);
1135 LIMIT(y1
, 0, term
.row
-1);
1136 LIMIT(y2
, 0, term
.row
-1);
1138 for(y
= y1
; y
<= y2
; y
++) {
1140 for(x
= x1
; x
<= x2
; x
++)
1141 term
.line
[y
][x
].state
= 0;
1146 tdeletechar(int n
) {
1147 int src
= term
.c
.x
+ n
;
1149 int size
= term
.col
- src
;
1151 term
.dirty
[term
.c
.y
] = 1;
1153 if(src
>= term
.col
) {
1154 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1157 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1158 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1162 tinsertblank(int n
) {
1165 int size
= term
.col
- dst
;
1167 term
.dirty
[term
.c
.y
] = 1;
1169 if(dst
>= term
.col
) {
1170 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1173 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1174 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1178 tinsertblankline(int n
) {
1179 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1182 tscrolldown(term
.c
.y
, n
);
1186 tdeleteline(int n
) {
1187 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1190 tscrollup(term
.c
.y
, n
);
1194 tsetattr(int *attr
, int l
) {
1197 for(i
= 0; i
< l
; i
++) {
1200 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1201 | ATTR_ITALIC
| ATTR_BLINK
);
1202 term
.c
.attr
.fg
= DefaultFG
;
1203 term
.c
.attr
.bg
= DefaultBG
;
1206 term
.c
.attr
.mode
|= ATTR_BOLD
;
1208 case 3: /* enter standout (highlight) */
1209 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1212 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1215 term
.c
.attr
.mode
|= ATTR_BLINK
;
1218 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1222 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1224 case 23: /* leave standout (highlight) mode */
1225 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1228 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1231 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1234 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1237 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1239 if(BETWEEN(attr
[i
], 0, 255))
1240 term
.c
.attr
.fg
= attr
[i
];
1242 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1245 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1248 term
.c
.attr
.fg
= DefaultFG
;
1251 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1253 if(BETWEEN(attr
[i
], 0, 255))
1254 term
.c
.attr
.bg
= attr
[i
];
1256 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1259 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1262 term
.c
.attr
.bg
= DefaultBG
;
1265 if(BETWEEN(attr
[i
], 30, 37))
1266 term
.c
.attr
.fg
= attr
[i
] - 30;
1267 else if(BETWEEN(attr
[i
], 40, 47))
1268 term
.c
.attr
.bg
= attr
[i
] - 40;
1269 else if(BETWEEN(attr
[i
], 90, 97))
1270 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1271 else if(BETWEEN(attr
[i
], 100, 107))
1272 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1274 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1281 tsetscroll(int t
, int b
) {
1284 LIMIT(t
, 0, term
.row
-1);
1285 LIMIT(b
, 0, term
.row
-1);
1295 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1298 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1301 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1305 case 1: /* DECCKM -- Cursor key */
1306 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1308 case 5: /* DECSCNM -- Reverve video */
1310 MODBIT(term
.mode
,set
, MODE_REVERSE
);
1311 if(mode
!= term
.mode
)
1314 case 6: /* XXX: DECOM -- Origin */
1316 case 7: /* DECAWM -- Auto wrap */
1317 MODBIT(term
.mode
, set
, MODE_WRAP
);
1319 case 8: /* XXX: DECARM -- Auto repeat */
1321 case 0: /* Error (IGNORED) */
1322 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1325 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1327 case 1000: /* 1000,1002: enable xterm mouse report */
1328 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1331 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1333 case 1049: /* = 1047 and 1048 */
1336 if(IS_SET(MODE_ALTSCREEN
))
1337 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1338 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1339 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1346 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1349 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1350 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1351 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1352 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1353 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1354 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1356 "erresc: unknown private set/reset mode %d\n",
1362 case 0: /* Error (IGNORED) */
1364 case 2: /* KAM -- keyboard action */
1365 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1367 case 4: /* IRM -- Insertion-replacement */
1368 MODBIT(term
.mode
, set
, MODE_INSERT
);
1370 case 12: /* XXX: SRM -- Send/Receive */
1372 case 20: /* LNM -- Linefeed/new line */
1373 MODBIT(term
.mode
, set
, MODE_CRLF
);
1377 "erresc: unknown set/reset mode %d\n",
1389 switch(csiescseq
.mode
) {
1392 fprintf(stderr
, "erresc: unknown csi ");
1396 case '@': /* ICH -- Insert <n> blank char */
1397 DEFAULT(csiescseq
.arg
[0], 1);
1398 tinsertblank(csiescseq
.arg
[0]);
1400 case 'A': /* CUU -- Cursor <n> Up */
1402 DEFAULT(csiescseq
.arg
[0], 1);
1403 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1405 case 'B': /* CUD -- Cursor <n> Down */
1406 DEFAULT(csiescseq
.arg
[0], 1);
1407 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1409 case 'C': /* CUF -- Cursor <n> Forward */
1411 DEFAULT(csiescseq
.arg
[0], 1);
1412 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1414 case 'D': /* CUB -- Cursor <n> Backward */
1415 DEFAULT(csiescseq
.arg
[0], 1);
1416 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1418 case 'E': /* CNL -- Cursor <n> Down and first col */
1419 DEFAULT(csiescseq
.arg
[0], 1);
1420 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1422 case 'F': /* CPL -- Cursor <n> Up and first col */
1423 DEFAULT(csiescseq
.arg
[0], 1);
1424 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1426 case 'g': /* TBC -- Tabulation clear */
1427 switch (csiescseq
.arg
[0]) {
1428 case 0: /* clear current tab stop */
1429 term
.tabs
[term
.c
.x
] = 0;
1431 case 3: /* clear all the tabs */
1432 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1438 case 'G': /* CHA -- Move to <col> */
1440 DEFAULT(csiescseq
.arg
[0], 1);
1441 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1443 case 'H': /* CUP -- Move to <row> <col> */
1445 DEFAULT(csiescseq
.arg
[0], 1);
1446 DEFAULT(csiescseq
.arg
[1], 1);
1447 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1449 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1450 DEFAULT(csiescseq
.arg
[0], 1);
1451 while(csiescseq
.arg
[0]--)
1454 case 'J': /* ED -- Clear screen */
1456 switch(csiescseq
.arg
[0]) {
1458 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1459 if(term
.c
.y
< term
.row
-1)
1460 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1464 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1465 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1468 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1474 case 'K': /* EL -- Clear line */
1475 switch(csiescseq
.arg
[0]) {
1477 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1480 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1483 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1487 case 'S': /* SU -- Scroll <n> line up */
1488 DEFAULT(csiescseq
.arg
[0], 1);
1489 tscrollup(term
.top
, csiescseq
.arg
[0]);
1491 case 'T': /* SD -- Scroll <n> line down */
1492 DEFAULT(csiescseq
.arg
[0], 1);
1493 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1495 case 'L': /* IL -- Insert <n> blank lines */
1496 DEFAULT(csiescseq
.arg
[0], 1);
1497 tinsertblankline(csiescseq
.arg
[0]);
1499 case 'l': /* RM -- Reset Mode */
1500 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1502 case 'M': /* DL -- Delete <n> lines */
1503 DEFAULT(csiescseq
.arg
[0], 1);
1504 tdeleteline(csiescseq
.arg
[0]);
1506 case 'X': /* ECH -- Erase <n> char */
1507 DEFAULT(csiescseq
.arg
[0], 1);
1508 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1510 case 'P': /* DCH -- Delete <n> char */
1511 DEFAULT(csiescseq
.arg
[0], 1);
1512 tdeletechar(csiescseq
.arg
[0]);
1514 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1515 DEFAULT(csiescseq
.arg
[0], 1);
1516 while(csiescseq
.arg
[0]--)
1519 case 'd': /* VPA -- Move to <row> */
1520 DEFAULT(csiescseq
.arg
[0], 1);
1521 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1523 case 'h': /* SM -- Set terminal mode */
1524 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1526 case 'm': /* SGR -- Terminal attribute (color) */
1527 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1529 case 'r': /* DECSTBM -- Set Scrolling Region */
1533 DEFAULT(csiescseq
.arg
[0], 1);
1534 DEFAULT(csiescseq
.arg
[1], term
.row
);
1535 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1539 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1540 tcursor(CURSOR_SAVE
);
1542 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1543 tcursor(CURSOR_LOAD
);
1552 for(i
= 0; i
< csiescseq
.len
; i
++) {
1553 uint c
= csiescseq
.buf
[i
] & 0xff;
1554 if(isprint(c
)) putchar(c
);
1555 else if(c
== '\n') printf("(\\n)");
1556 else if(c
== '\r') printf("(\\r)");
1557 else if(c
== 0x1b) printf("(\\e)");
1558 else printf("(%02x)", c
);
1565 memset(&csiescseq
, 0, sizeof(csiescseq
));
1573 * TODO: make this being useful in case of color palette change.
1579 switch(strescseq
.type
) {
1580 case ']': /* OSC -- Operating System Command */
1586 * TODO: Handle special chars in string, like umlauts.
1589 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1593 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1595 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1598 fprintf(stderr
, "erresc: unknown str ");
1603 case 'k': /* old title set compatibility */
1604 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1606 case 'P': /* DSC -- Device Control String */
1607 case '_': /* APC -- Application Program Command */
1608 case '^': /* PM -- Privacy Message */
1610 fprintf(stderr
, "erresc: unknown str ");
1620 * TODO: Implement parsing like for CSI when required.
1621 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1629 printf("ESC%c", strescseq
.type
);
1630 for(i
= 0; i
< strescseq
.len
; i
++) {
1631 uint c
= strescseq
.buf
[i
] & 0xff;
1632 if(isprint(c
)) putchar(c
);
1633 else if(c
== '\n') printf("(\\n)");
1634 else if(c
== '\r') printf("(\\r)");
1635 else if(c
== 0x1b) printf("(\\e)");
1636 else printf("(%02x)", c
);
1643 memset(&strescseq
, 0, sizeof(strescseq
));
1647 tputtab(bool forward
) {
1653 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1658 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1661 tmoveto(x
, term
.c
.y
);
1665 tputc(char *c
, int len
) {
1669 write(iofd
, c
, len
);
1671 if(term
.esc
& ESC_START
) {
1672 if(term
.esc
& ESC_CSI
) {
1673 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1674 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1676 csiparse(), csihandle();
1678 } else if(term
.esc
& ESC_STR
) {
1681 term
.esc
= ESC_START
| ESC_STR_END
;
1683 case '\a': /* backwards compatibility to xterm */
1688 strescseq
.buf
[strescseq
.len
++] = ascii
;
1689 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1694 } else if(term
.esc
& ESC_STR_END
) {
1698 } else if(term
.esc
& ESC_ALTCHARSET
) {
1700 case '0': /* Line drawing set */
1701 term
.c
.attr
.mode
|= ATTR_GFX
;
1703 case 'B': /* USASCII */
1704 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1706 case 'A': /* UK (IGNORED) */
1707 case '<': /* multinational charset (IGNORED) */
1708 case '5': /* Finnish (IGNORED) */
1709 case 'C': /* Finnish (IGNORED) */
1710 case 'K': /* German (IGNORED) */
1713 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1719 term
.esc
|= ESC_CSI
;
1721 case 'P': /* DCS -- Device Control String */
1722 case '_': /* APC -- Application Program Command */
1723 case '^': /* PM -- Privacy Message */
1724 case ']': /* OSC -- Operating System Command */
1725 case 'k': /* old title set compatibility */
1727 strescseq
.type
= ascii
;
1728 term
.esc
|= ESC_STR
;
1730 case '(': /* set primary charset G0 */
1731 term
.esc
|= ESC_ALTCHARSET
;
1733 case ')': /* set secondary charset G1 (IGNORED) */
1734 case '*': /* set tertiary charset G2 (IGNORED) */
1735 case '+': /* set quaternary charset G3 (IGNORED) */
1738 case 'D': /* IND -- Linefeed */
1739 if(term
.c
.y
== term
.bot
)
1740 tscrollup(term
.top
, 1);
1742 tmoveto(term
.c
.x
, term
.c
.y
+1);
1745 case 'E': /* NEL -- Next line */
1746 tnewline(1); /* always go to first col */
1749 case 'H': /* HTS -- Horizontal tab stop */
1750 term
.tabs
[term
.c
.x
] = 1;
1753 case 'M': /* RI -- Reverse index */
1754 if(term
.c
.y
== term
.top
)
1755 tscrolldown(term
.top
, 1);
1757 tmoveto(term
.c
.x
, term
.c
.y
-1);
1760 case 'c': /* RIS -- Reset to inital state */
1763 xclear(0, 0, xw
.w
, xw
.h
);
1766 case '=': /* DECPAM -- Application keypad */
1767 term
.mode
|= MODE_APPKEYPAD
;
1770 case '>': /* DECPNM -- Normal keypad */
1771 term
.mode
&= ~MODE_APPKEYPAD
;
1774 case '7': /* DECSC -- Save Cursor */
1775 tcursor(CURSOR_SAVE
);
1778 case '8': /* DECRC -- Restore Cursor */
1779 tcursor(CURSOR_LOAD
);
1782 case '\\': /* ST -- Stop */
1786 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1787 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1792 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1795 case '\0': /* padding character, do nothing */
1801 tmoveto(term
.c
.x
-1, term
.c
.y
);
1804 tmoveto(0, term
.c
.y
);
1809 /* go to first col if the mode is set */
1810 tnewline(IS_SET(MODE_CRLF
));
1813 if(!(xw
.state
& WIN_FOCUSED
))
1818 term
.esc
= ESC_START
;
1821 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1822 tnewline(1); /* always go to first col */
1824 if(term
.c
.x
+1 < term
.col
)
1825 tmoveto(term
.c
.x
+1, term
.c
.y
);
1827 term
.c
.state
|= CURSOR_WRAPNEXT
;
1833 tresize(int col
, int row
) {
1835 int minrow
= MIN(row
, term
.row
);
1836 int mincol
= MIN(col
, term
.col
);
1837 int slide
= term
.c
.y
- row
+ 1;
1839 if(col
< 1 || row
< 1)
1842 /* free unneeded rows */
1845 /* slide screen to keep cursor where we expect it -
1846 * tscrollup would work here, but we can optimize to
1847 * memmove because we're freeing the earlier lines */
1848 for(/* i = 0 */; i
< slide
; i
++) {
1852 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1853 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1855 for(i
+= row
; i
< term
.row
; i
++) {
1860 /* resize to new height */
1861 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
1862 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
1863 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1864 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1866 /* resize each row to new width, zero-pad if needed */
1867 for(i
= 0; i
< minrow
; i
++) {
1869 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
1870 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
1871 for(x
= mincol
; x
< col
; x
++) {
1872 term
.line
[i
][x
].state
= 0;
1873 term
.alt
[i
][x
].state
= 0;
1877 /* allocate any new rows */
1878 for(/* i == minrow */; i
< row
; i
++) {
1880 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
1881 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
1883 if(col
> term
.col
) {
1884 bool *bp
= term
.tabs
+ term
.col
;
1886 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1887 while(--bp
> term
.tabs
&& !*bp
)
1889 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1892 /* update terminal size */
1893 term
.col
= col
, term
.row
= row
;
1894 /* make use of the LIMIT in tmoveto */
1895 tmoveto(term
.c
.x
, term
.c
.y
);
1896 /* reset scrolling region */
1897 tsetscroll(0, row
-1);
1903 xresize(int col
, int row
) {
1904 xw
.tw
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1905 xw
.th
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1907 XftDrawChange(xw
.xft_draw
, xw
.buf
);
1913 XRenderColor xft_color
= { .alpha
= 0 };
1914 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1916 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1917 for(i
= 0; i
< LEN(colorname
); i
++) {
1920 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.xft_col
[i
])) {
1922 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1924 dc
.col
[i
] = dc
.xft_col
[i
].pixel
;
1927 /* load colors [16-255] ; same colors as xterm */
1928 for(i
= 16, r
= 0; r
< 6; r
++)
1929 for(g
= 0; g
< 6; g
++)
1930 for(b
= 0; b
< 6; b
++) {
1931 xft_color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1932 xft_color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1933 xft_color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1934 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
1936 fprintf(stderr
, "Could not allocate color %d\n", i
);
1938 dc
.col
[i
] = dc
.xft_col
[i
].pixel
;
1942 for(r
= 0; r
< 24; r
++, i
++) {
1943 xft_color
.red
= xft_color
.green
= xft_color
.blue
= 0x0808 + 0x0a0a * r
;
1944 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
1946 fprintf(stderr
, "Could not allocate color %d\n", i
);
1948 dc
.col
[i
] = dc
.xft_col
[i
].pixel
;
1953 xclear(int x1
, int y1
, int x2
, int y2
) {
1954 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1955 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1956 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1957 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1962 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1963 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1964 XSizeHints
*sizeh
= NULL
;
1966 sizeh
= XAllocSizeHints();
1967 if(xw
.isfixed
== False
) {
1968 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
1969 sizeh
->height
= xw
.h
;
1970 sizeh
->width
= xw
.w
;
1971 sizeh
->height_inc
= xw
.ch
;
1972 sizeh
->width_inc
= xw
.cw
;
1973 sizeh
->base_height
= 2*BORDER
;
1974 sizeh
->base_width
= 2*BORDER
;
1976 sizeh
->flags
= PMaxSize
| PMinSize
;
1977 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
1978 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
1981 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
1986 xinitfont(Font
*f
, char *fontstr
) {
1987 f
->xft_set
= XftFontOpenName(xw
.dpy
, xw
.scr
, fontstr
);
1990 die("st: can't open font %s.\n", fontstr
);
1992 f
->ascent
= f
->xft_set
->ascent
;
1993 f
->descent
= f
->xft_set
->descent
;
1995 f
->rbearing
= f
->xft_set
->max_advance_width
;
1999 initfonts(char *fontstr
, char *bfontstr
, char *ifontstr
, char *ibfontstr
) {
2000 xinitfont(&dc
.font
, fontstr
);
2001 xinitfont(&dc
.bfont
, bfontstr
);
2002 xinitfont(&dc
.ifont
, ifontstr
);
2003 xinitfont(&dc
.ibfont
, ibfontstr
);
2008 XSetWindowAttributes attrs
;
2011 int sw
, sh
, major
, minor
;
2013 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2014 die("Can't open display\n");
2015 xw
.scr
= XDefaultScreen(xw
.dpy
);
2016 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2019 initfonts(FONT
, BOLDFONT
, ITALICFONT
, ITALICBOLDFONT
);
2021 /* XXX: Assuming same size for bold font */
2022 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
2023 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
2026 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2029 /* adjust fixed window geometry */
2031 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2032 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2034 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2036 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2041 /* window - default size */
2042 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
2043 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
2048 attrs
.background_pixel
= dc
.col
[DefaultBG
];
2049 attrs
.border_pixel
= dc
.col
[DefaultBG
];
2050 attrs
.bit_gravity
= NorthWestGravity
;
2051 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2052 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2053 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2054 attrs
.colormap
= xw
.cmap
;
2056 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2057 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2058 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2060 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2064 /* double buffering */
2065 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2066 die("Xdbe extension is not present\n");
2067 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2069 /* Xft rendering context */
2070 xw
.xft_draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2073 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2074 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2075 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2076 XNFocusWindow
, xw
.win
, NULL
);
2078 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2080 /* white cursor, black outline */
2081 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2082 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2083 XRecolorCursor(xw
.dpy
, cursor
,
2084 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2085 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2087 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2090 XMapWindow(xw
.dpy
, xw
.win
);
2096 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2097 int fg
= base
.fg
, bg
= base
.bg
, temp
;
2098 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
2099 Font
*font
= &dc
.font
;
2102 /* only switch default fg/bg if term is in RV mode */
2103 if(IS_SET(MODE_REVERSE
)) {
2110 if(base
.mode
& ATTR_REVERSE
)
2111 temp
= fg
, fg
= bg
, bg
= temp
;
2113 if(base
.mode
& ATTR_BOLD
) {
2118 if(base
.mode
& ATTR_ITALIC
)
2120 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2123 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
2124 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
2126 XftTextExtentsUtf8(xw
.dpy
, font
->xft_set
, (FcChar8
*)s
, bytelen
, &extents
);
2127 width
= extents
.xOff
;
2128 XftDrawRect(xw
.xft_draw
, &dc
.xft_col
[bg
], winx
, winy
- font
->ascent
, width
, xw
.ch
);
2129 XftDrawStringUtf8(xw
.xft_draw
, &dc
.xft_col
[fg
], font
->xft_set
, winx
, winy
, (FcChar8
*)s
, bytelen
);
2131 if(base
.mode
& ATTR_UNDERLINE
)
2132 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
2137 static int oldx
= 0;
2138 static int oldy
= 0;
2140 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2142 LIMIT(oldx
, 0, term
.col
-1);
2143 LIMIT(oldy
, 0, term
.row
-1);
2145 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2146 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2148 /* remove the old cursor */
2149 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2150 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2151 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2153 xclear(oldx
, oldy
, oldx
, oldy
);
2155 /* draw the new one */
2156 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2157 if(!(xw
.state
& WIN_FOCUSED
))
2160 if(IS_SET(MODE_REVERSE
))
2161 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2164 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2165 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2171 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2176 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2178 xclear(0, 0, xw
.w
, xw
.h
);
2181 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2182 nanosleep(&tv
, NULL
);
2187 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2189 drawregion(0, 0, term
.col
, term
.row
);
2190 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2194 drawregion(int x1
, int y1
, int x2
, int y2
) {
2195 int ic
, ib
, x
, y
, ox
, sl
;
2197 char buf
[DRAW_BUF_SIZ
];
2198 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2200 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2202 if(!(xw
.state
& WIN_VISIBLE
))
2205 for(y
= y1
; y
< y2
; y
++) {
2208 xclear(0, y
, term
.col
, y
);
2210 base
= term
.line
[y
][0];
2212 for(x
= x1
; x
< x2
; x
++) {
2213 new = term
.line
[y
][x
];
2214 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2215 new.mode
^= ATTR_REVERSE
;
2216 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2217 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2218 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2221 if(new.state
& GLYPH_SET
) {
2226 sl
= utf8size(new.c
);
2227 memcpy(buf
+ib
, new.c
, sl
);
2233 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2239 expose(XEvent
*ev
) {
2240 XExposeEvent
*e
= &ev
->xexpose
;
2241 if(xw
.state
& WIN_REDRAW
) {
2243 xw
.state
&= ~WIN_REDRAW
;
2248 visibility(XEvent
*ev
) {
2249 XVisibilityEvent
*e
= &ev
->xvisibility
;
2250 if(e
->state
== VisibilityFullyObscured
)
2251 xw
.state
&= ~WIN_VISIBLE
;
2252 else if(!(xw
.state
& WIN_VISIBLE
))
2253 /* need a full redraw for next Expose, not just a buf copy */
2254 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2259 xw
.state
&= ~WIN_VISIBLE
;
2263 xseturgency(int add
) {
2264 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2265 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2266 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2272 if(ev
->type
== FocusIn
) {
2273 xw
.state
|= WIN_FOCUSED
;
2276 xw
.state
&= ~WIN_FOCUSED
;
2280 kmap(KeySym k
, uint state
) {
2283 for(i
= 0; i
< LEN(key
); i
++) {
2284 uint mask
= key
[i
].mask
;
2285 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2286 return (char*)key
[i
].s
;
2292 kpress(XEvent
*ev
) {
2293 XKeyEvent
*e
= &ev
->xkey
;
2302 if (IS_SET(MODE_KBDLOCK
))
2304 meta
= e
->state
& Mod1Mask
;
2305 shift
= e
->state
& ShiftMask
;
2306 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2308 /* 1. custom keys from config.h */
2309 if((customkey
= kmap(ksym
, e
->state
)))
2310 ttywrite(customkey
, strlen(customkey
));
2311 /* 2. hardcoded (overrides X lookup) */
2318 /* XXX: shift up/down doesn't work */
2319 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2327 if(IS_SET(MODE_CRLF
))
2328 ttywrite("\r\n", 2);
2335 if(meta
&& len
== 1)
2336 ttywrite("\033", 1);
2344 cmessage(XEvent
*e
) {
2346 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2347 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2348 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2349 xw
.state
|= WIN_FOCUSED
;
2351 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2352 xw
.state
&= ~WIN_FOCUSED
;
2361 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2364 xw
.w
= e
->xconfigure
.width
;
2365 xw
.h
= e
->xconfigure
.height
;
2366 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2367 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2368 if(col
== term
.col
&& row
== term
.row
)
2371 xclear(0, 0, xw
.w
, xw
.h
);
2381 int xfd
= XConnectionNumber(xw
.dpy
), i
;
2382 struct timeval drawtimeout
, *tv
= NULL
;
2386 FD_SET(cmdfd
, &rfd
);
2388 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
2391 die("select failed: %s\n", SERRNO
);
2395 * Stop after a certain number of reads so the user does not
2396 * feel like the system is stuttering.
2398 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
2402 * Just wait a bit so it isn't disturbing the
2403 * user and the system is able to write something.
2405 drawtimeout
.tv_sec
= 0;
2406 drawtimeout
.tv_usec
= 5;
2413 while(XPending(xw
.dpy
)) {
2414 XNextEvent(xw
.dpy
, &ev
);
2415 if(XFilterEvent(&ev
, xw
.win
))
2417 if(handler
[ev
.type
])
2418 (handler
[ev
.type
])(&ev
);
2427 main(int argc
, char *argv
[]) {
2428 int i
, bitm
, xr
, yr
;
2431 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2434 for(i
= 1; i
< argc
; i
++) {
2435 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2437 if(++i
< argc
) opt_title
= argv
[i
];
2440 if(++i
< argc
) opt_class
= argv
[i
];
2443 if(++i
< argc
) opt_embed
= argv
[i
];
2446 if(++i
< argc
) opt_io
= argv
[i
];
2449 /* eat every remaining arguments */
2450 if(++i
< argc
) opt_cmd
= &argv
[i
];
2456 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2461 if(bitm
& WidthValue
)
2463 if(bitm
& HeightValue
)
2465 if(bitm
& XNegative
&& xw
.fx
== 0)
2467 if(bitm
& XNegative
&& xw
.fy
== 0)
2470 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2480 setlocale(LC_CTYPE
, "");