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 REDRAW_TIMEOUT (80*1000) /* 80 ms */
58 #define SERRNO strerror(errno)
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 #define LEN(a) (sizeof(a) / sizeof(a[0]))
62 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
63 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
64 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
65 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
66 #define IS_SET(flag) (term.mode & (flag))
67 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
68 #define X2COL(x) (((x) - BORDER)/xw.cw)
69 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
71 enum glyph_attribute
{
81 enum cursor_movement
{
108 MODE_MOUSEMOTION
= 64,
116 ESC_STR
= 4, /* DSC, OSC, PM, APC */
118 ESC_STR_END
= 16, /* a final string was encountered */
129 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
131 typedef unsigned char uchar
;
132 typedef unsigned int uint
;
133 typedef unsigned long ulong
;
134 typedef unsigned short ushort
;
137 char c
[UTF_SIZ
]; /* character code */
138 uchar mode
; /* attribute flags */
139 ushort fg
; /* foreground */
140 ushort bg
; /* background */
141 uchar state
; /* state flags */
147 Glyph attr
; /* current char attributes */
153 /* CSI Escape sequence structs */
154 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
156 char buf
[ESC_BUF_SIZ
]; /* raw string */
157 int len
; /* raw string length */
159 int arg
[ESC_ARG_SIZ
];
160 int narg
; /* nb of args */
164 /* STR Escape sequence structs */
165 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
167 char type
; /* ESC type ... */
168 char buf
[STR_BUF_SIZ
]; /* raw string */
169 int len
; /* raw string length */
170 char *args
[STR_ARG_SIZ
];
171 int narg
; /* nb of args */
174 /* Internal representation of the screen */
176 int row
; /* nb row */
177 int col
; /* nb col */
178 Line
* line
; /* screen */
179 Line
* alt
; /* alternate screen */
180 bool* dirty
; /* dirtyness of lines */
181 TCursor c
; /* cursor */
182 int top
; /* top scroll limit */
183 int bot
; /* bottom scroll limit */
184 int mode
; /* terminal mode flags */
185 int esc
; /* escape state flags */
189 /* Purely graphic info */
199 Bool isfixed
; /* is fixed geometry? */
200 int fx
, fy
, fw
, fh
; /* fixed geometry */
201 int w
; /* window width */
202 int h
; /* window height */
203 int ch
; /* char height */
204 int cw
; /* char width */
205 char state
; /* focus, redraw, visible */
215 /* TODO: use better name for vars... */
220 struct {int x
, y
;} b
, e
;
224 struct timeval tclick1
;
225 struct timeval tclick2
;
230 /* Drawing Context */
232 ulong col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
240 } font
, bfont
, ifont
, ibfont
;
243 static void die(const char*, ...);
244 static void draw(void);
245 static void redraw(void);
246 static void drawregion(int, int, int, int);
247 static void execsh(void);
248 static void sigchld(int);
249 static void run(void);
251 static void csidump(void);
252 static void csihandle(void);
253 static void csiparse(void);
254 static void csireset(void);
255 static void strdump(void);
256 static void strhandle(void);
257 static void strparse(void);
258 static void strreset(void);
260 static void tclearregion(int, int, int, int);
261 static void tcursor(int);
262 static void tdeletechar(int);
263 static void tdeleteline(int);
264 static void tinsertblank(int);
265 static void tinsertblankline(int);
266 static void tmoveto(int, int);
267 static void tnew(int, int);
268 static void tnewline(int);
269 static void tputtab(bool);
270 static void tputc(char*);
271 static void treset(void);
272 static int tresize(int, int);
273 static void tscrollup(int, int);
274 static void tscrolldown(int, int);
275 static void tsetattr(int*, int);
276 static void tsetchar(char*);
277 static void tsetscroll(int, int);
278 static void tswapscreen(void);
279 static void tsetdirt(int, int);
280 static void tsetmode(bool, bool, int *, int);
281 static void tfulldirt(void);
283 static void ttynew(void);
284 static void ttyread(void);
285 static void ttyresize(int, int);
286 static void ttywrite(const char *, size_t);
288 static void xdraws(char *, Glyph
, int, int, int, int);
289 static void xhints(void);
290 static void xclear(int, int, int, int);
291 static void xdrawcursor(void);
292 static void xinit(void);
293 static void xloadcols(void);
294 static void xresettitle(void);
295 static void xseturgency(int);
296 static void xsetsel(char*);
297 static void xresize(int, int);
299 static void expose(XEvent
*);
300 static void visibility(XEvent
*);
301 static void unmap(XEvent
*);
302 static char* kmap(KeySym
, uint
);
303 static void kpress(XEvent
*);
304 static void cmessage(XEvent
*);
305 static void resize(XEvent
*);
306 static void focus(XEvent
*);
307 static void brelease(XEvent
*);
308 static void bpress(XEvent
*);
309 static void bmotion(XEvent
*);
310 static void selnotify(XEvent
*);
311 static void selclear(XEvent
*);
312 static void selrequest(XEvent
*);
314 static void selinit(void);
315 static inline bool selected(int, int);
316 static void selcopy(void);
317 static void selpaste(void);
318 static void selscroll(int, int);
320 static int utf8decode(char *, long *);
321 static int utf8encode(long *, char *);
322 static int utf8size(char *);
323 static int isfullutf8(char *, int);
325 static void *xmalloc(size_t);
326 static void *xrealloc(void *, size_t);
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
;
354 static int iofd
= -1;
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 xmalloc(size_t len
) {
363 void *p
= malloc(len
);
365 die("Out of memory");
370 xrealloc(void *p
, size_t len
) {
371 if((p
= realloc(p
, len
)) == NULL
)
372 die("Out of memory");
377 utf8decode(char *s
, long *u
) {
383 if(~c
& B7
) { /* 0xxxxxxx */
386 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
387 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
389 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
390 *u
= c
&(B3
|B2
|B1
|B0
);
392 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
397 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
399 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
402 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
404 if((n
== 1 && *u
< 0x80) ||
405 (n
== 2 && *u
< 0x800) ||
406 (n
== 3 && *u
< 0x10000) ||
407 (*u
>= 0xD800 && *u
<= 0xDFFF))
416 utf8encode(long *u
, char *s
) {
424 *sp
= uc
; /* 0xxxxxxx */
426 } else if(*u
< 0x800) {
427 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
429 } else if(uc
< 0x10000) {
430 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
432 } else if(uc
<= 0x10FFFF) {
433 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
438 for(i
=n
,++sp
; i
>0; --i
,++sp
)
439 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
449 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
450 UTF-8 otherwise return 0 */
452 isfullutf8(char *s
, int b
) {
460 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
462 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
464 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
466 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
468 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
469 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
481 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
483 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
491 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
492 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
496 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
497 if(sel
.xtarget
== None
)
498 sel
.xtarget
= XA_STRING
;
502 selected(int x
, int y
) {
503 if(sel
.ey
== y
&& sel
.by
== y
) {
504 int bx
= MIN(sel
.bx
, sel
.ex
);
505 int ex
= MAX(sel
.bx
, sel
.ex
);
506 return BETWEEN(x
, bx
, ex
);
508 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
509 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
513 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
515 *b
= e
->xbutton
.button
;
517 *x
= X2COL(e
->xbutton
.x
);
518 *y
= Y2ROW(e
->xbutton
.y
);
519 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
520 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
521 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
522 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
526 mousereport(XEvent
*e
) {
527 int x
= X2COL(e
->xbutton
.x
);
528 int y
= Y2ROW(e
->xbutton
.y
);
529 int button
= e
->xbutton
.button
;
530 int state
= e
->xbutton
.state
;
531 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
532 static int ob
, ox
, oy
;
535 if(e
->xbutton
.type
== MotionNotify
) {
536 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
540 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
546 if(e
->xbutton
.type
== ButtonPress
) {
552 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
553 + (state
& Mod4Mask
? 8 : 0)
554 + (state
& ControlMask
? 16 : 0);
556 ttywrite(buf
, sizeof(buf
));
561 if(IS_SET(MODE_MOUSE
))
563 else if(e
->xbutton
.button
== Button1
) {
565 tsetdirt(sel
.b
.y
, sel
.e
.y
);
567 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
568 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
575 int x
, y
, bufsize
, is_selected
= 0;
581 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
582 ptr
= str
= xmalloc(bufsize
);
584 /* append every set & selected glyph to the selection */
585 for(y
= 0; y
< term
.row
; y
++) {
586 for(x
= 0; x
< term
.col
; x
++) {
587 is_selected
= selected(x
, y
);
588 if((term
.line
[y
][x
].state
& GLYPH_SET
) && is_selected
) {
589 int size
= utf8size(term
.line
[y
][x
].c
);
590 memcpy(ptr
, term
.line
[y
][x
].c
, size
);
595 /* \n at the end of every selected line except for the last one */
596 if(is_selected
&& y
< sel
.e
.y
)
601 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
606 selnotify(XEvent
*e
) {
607 ulong nitems
, ofs
, rem
;
614 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
615 False
, AnyPropertyType
, &type
, &format
,
616 &nitems
, &rem
, &data
)) {
617 fprintf(stderr
, "Clipboard allocation failed\n");
620 ttywrite((const char *) data
, nitems
* format
/ 8);
622 /* number of 32-bit chunks returned */
623 ofs
+= nitems
* format
/ 32;
629 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
632 void selclear(XEvent
*e
) {
636 tsetdirt(sel
.b
.y
, sel
.e
.y
);
640 selrequest(XEvent
*e
) {
641 XSelectionRequestEvent
*xsre
;
645 xsre
= (XSelectionRequestEvent
*) e
;
646 xev
.type
= SelectionNotify
;
647 xev
.requestor
= xsre
->requestor
;
648 xev
.selection
= xsre
->selection
;
649 xev
.target
= xsre
->target
;
650 xev
.time
= xsre
->time
;
654 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
655 if(xsre
->target
== xa_targets
) {
656 /* respond with the supported type */
657 Atom string
= sel
.xtarget
;
658 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
659 XA_ATOM
, 32, PropModeReplace
,
660 (uchar
*) &string
, 1);
661 xev
.property
= xsre
->property
;
662 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
663 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
664 xsre
->target
, 8, PropModeReplace
,
665 (uchar
*) sel
.clip
, strlen(sel
.clip
));
666 xev
.property
= xsre
->property
;
669 /* all done, send a notification to the listener */
670 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
671 fprintf(stderr
, "Error sending SelectionNotify event\n");
676 /* register the selection for both the clipboard and the primary */
682 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
684 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
685 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
689 brelease(XEvent
*e
) {
690 if(IS_SET(MODE_MOUSE
)) {
694 if(e
->xbutton
.button
== Button2
)
696 else if(e
->xbutton
.button
== Button1
) {
698 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
699 term
.dirty
[sel
.ey
] = 1;
700 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
703 gettimeofday(&now
, NULL
);
705 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
706 /* triple click on the line */
707 sel
.b
.x
= sel
.bx
= 0;
708 sel
.e
.x
= sel
.ex
= term
.col
;
709 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
711 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
712 /* double click to select word */
714 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
715 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
717 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
718 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
720 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
726 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
727 gettimeofday(&sel
.tclick1
, NULL
);
732 if(IS_SET(MODE_MOUSE
)) {
737 int oldey
= sel
.ey
, oldex
= sel
.ex
;
738 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
740 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
741 int starty
= MIN(oldey
, sel
.ey
);
742 int endy
= MAX(oldey
, sel
.ey
);
743 tsetdirt(starty
, endy
);
749 die(const char *errstr
, ...) {
752 va_start(ap
, errstr
);
753 vfprintf(stderr
, errstr
, ap
);
761 char *envshell
= getenv("SHELL");
767 signal(SIGCHLD
, SIG_DFL
);
768 signal(SIGHUP
, SIG_DFL
);
769 signal(SIGINT
, SIG_DFL
);
770 signal(SIGQUIT
, SIG_DFL
);
771 signal(SIGTERM
, SIG_DFL
);
772 signal(SIGALRM
, SIG_DFL
);
774 DEFAULT(envshell
, SHELL
);
775 putenv("TERM="TNAME
);
776 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
777 execvp(args
[0], args
);
784 if(waitpid(pid
, &stat
, 0) < 0)
785 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
787 exit(WEXITSTATUS(stat
));
796 /* seems to work fine on linux, openbsd and freebsd */
797 struct winsize w
= {term
.row
, term
.col
, 0, 0};
798 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
799 die("openpty failed: %s\n", SERRNO
);
801 switch(pid
= fork()) {
803 die("fork failed\n");
806 setsid(); /* create a new process group */
807 dup2(s
, STDIN_FILENO
);
808 dup2(s
, STDOUT_FILENO
);
809 dup2(s
, STDERR_FILENO
);
810 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
811 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
819 signal(SIGCHLD
, sigchld
);
821 if(!strcmp(opt_io
, "-")) {
822 iofd
= STDOUT_FILENO
;
824 if((iofd
= open(opt_io
, O_WRONLY
| O_CREAT
, 0666)) < 0) {
825 fprintf(stderr
, "Error opening %s:%s\n",
826 opt_io
, strerror(errno
));
836 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
838 fprintf(stderr
, "\n");
843 static char buf
[BUFSIZ
];
844 static int buflen
= 0;
847 int charsize
; /* size of utf8 char in bytes */
851 /* append read bytes to unprocessed bytes */
852 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
853 die("Couldn't read from shell: %s\n", SERRNO
);
855 /* process every complete utf8 char */
858 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
859 charsize
= utf8decode(ptr
, &utf8c
);
860 utf8encode(&utf8c
, s
);
866 /* keep any uncomplete utf8 char for the next call */
867 memmove(buf
, ptr
, buflen
);
871 ttywrite(const char *s
, size_t n
) {
872 if(write(cmdfd
, s
, n
) == -1)
873 die("write error on tty: %s\n", SERRNO
);
877 ttyresize(int x
, int y
) {
884 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
885 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
889 tsetdirt(int top
, int bot
)
893 LIMIT(top
, 0, term
.row
-1);
894 LIMIT(bot
, 0, term
.row
-1);
896 for(i
= top
; i
<= bot
; i
++)
903 tsetdirt(0, term
.row
-1);
910 if(mode
== CURSOR_SAVE
)
912 else if(mode
== CURSOR_LOAD
)
913 term
.c
= c
, tmoveto(c
.x
, c
.y
);
923 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
925 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
926 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
928 term
.top
= 0, term
.bot
= term
.row
- 1;
929 term
.mode
= MODE_WRAP
;
930 tclearregion(0, 0, term
.col
-1, term
.row
-1);
934 tnew(int col
, int row
) {
935 /* set screen size */
936 term
.row
= row
, term
.col
= col
;
937 term
.line
= xmalloc(term
.row
* sizeof(Line
));
938 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
939 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
940 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
942 for(row
= 0; row
< term
.row
; row
++) {
943 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
944 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
947 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
954 Line
* tmp
= term
.line
;
955 term
.line
= term
.alt
;
957 term
.mode
^= MODE_ALTSCREEN
;
962 tscrolldown(int orig
, int n
) {
966 LIMIT(n
, 0, term
.bot
-orig
+1);
968 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
970 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
972 term
.line
[i
] = term
.line
[i
-n
];
973 term
.line
[i
-n
] = temp
;
983 tscrollup(int orig
, int n
) {
986 LIMIT(n
, 0, term
.bot
-orig
+1);
988 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
990 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
992 term
.line
[i
] = term
.line
[i
+n
];
993 term
.line
[i
+n
] = temp
;
1003 selscroll(int orig
, int n
) {
1007 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1008 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1012 if(sel
.by
< term
.top
) {
1016 if(sel
.ey
> term
.bot
) {
1020 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1021 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1026 tnewline(int first_col
) {
1029 tscrollup(term
.top
, 1);
1032 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1037 /* int noarg = 1; */
1038 char *p
= csiescseq
.buf
;
1042 csiescseq
.priv
= 1, p
++;
1044 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1045 while(isdigit(*p
)) {
1046 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1047 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1049 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
1050 csiescseq
.narg
++, p
++;
1052 csiescseq
.mode
= *p
;
1060 tmoveto(int x
, int y
) {
1061 LIMIT(x
, 0, term
.col
-1);
1062 LIMIT(y
, 0, term
.row
-1);
1063 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1070 term
.dirty
[term
.c
.y
] = 1;
1071 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1072 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1073 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1077 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1081 temp
= x1
, x1
= x2
, x2
= temp
;
1083 temp
= y1
, y1
= y2
, y2
= temp
;
1085 LIMIT(x1
, 0, term
.col
-1);
1086 LIMIT(x2
, 0, term
.col
-1);
1087 LIMIT(y1
, 0, term
.row
-1);
1088 LIMIT(y2
, 0, term
.row
-1);
1090 for(y
= y1
; y
<= y2
; y
++) {
1092 for(x
= x1
; x
<= x2
; x
++)
1093 term
.line
[y
][x
].state
= 0;
1098 tdeletechar(int n
) {
1099 int src
= term
.c
.x
+ n
;
1101 int size
= term
.col
- src
;
1103 term
.dirty
[term
.c
.y
] = 1;
1105 if(src
>= term
.col
) {
1106 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1109 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1110 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1114 tinsertblank(int n
) {
1117 int size
= term
.col
- dst
;
1119 term
.dirty
[term
.c
.y
] = 1;
1121 if(dst
>= term
.col
) {
1122 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1125 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1126 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1130 tinsertblankline(int n
) {
1131 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1134 tscrolldown(term
.c
.y
, n
);
1138 tdeleteline(int n
) {
1139 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1142 tscrollup(term
.c
.y
, n
);
1146 tsetattr(int *attr
, int l
) {
1149 for(i
= 0; i
< l
; i
++) {
1152 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1153 | ATTR_ITALIC
| ATTR_BLINK
);
1154 term
.c
.attr
.fg
= DefaultFG
;
1155 term
.c
.attr
.bg
= DefaultBG
;
1158 term
.c
.attr
.mode
|= ATTR_BOLD
;
1160 case 3: /* enter standout (highlight) */
1161 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1164 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1167 term
.c
.attr
.mode
|= ATTR_BLINK
;
1170 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1174 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1176 case 23: /* leave standout (highlight) mode */
1177 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1180 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1183 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1186 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1189 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1191 if(BETWEEN(attr
[i
], 0, 255))
1192 term
.c
.attr
.fg
= attr
[i
];
1194 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1197 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1200 term
.c
.attr
.fg
= DefaultFG
;
1203 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1205 if(BETWEEN(attr
[i
], 0, 255))
1206 term
.c
.attr
.bg
= attr
[i
];
1208 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1211 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1214 term
.c
.attr
.bg
= DefaultBG
;
1217 if(BETWEEN(attr
[i
], 30, 37))
1218 term
.c
.attr
.fg
= attr
[i
] - 30;
1219 else if(BETWEEN(attr
[i
], 40, 47))
1220 term
.c
.attr
.bg
= attr
[i
] - 40;
1221 else if(BETWEEN(attr
[i
], 90, 97))
1222 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1223 else if(BETWEEN(attr
[i
], 100, 107))
1224 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1226 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1233 tsetscroll(int t
, int b
) {
1236 LIMIT(t
, 0, term
.row
-1);
1237 LIMIT(b
, 0, term
.row
-1);
1247 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1250 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1253 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1257 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1259 case 5: /* DECSCNM -- Reverve video */
1261 MODBIT(term
.mode
,set
, MODE_REVERSE
);
1262 if(mode
!= term
.mode
)
1266 MODBIT(term
.mode
, set
, MODE_WRAP
);
1269 MODBIT(term
.mode
, set
, MODE_CRLF
);
1271 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1274 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1276 case 1000: /* 1000,1002: enable xterm mouse report */
1277 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1280 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1282 case 1049: /* = 1047 and 1048 */
1285 if(IS_SET(MODE_ALTSCREEN
))
1286 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1287 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1288 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1295 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1299 "erresc: unknown private set/reset mode %d\n",
1306 MODBIT(term
.mode
, set
, MODE_INSERT
);
1310 "erresc: unknown set/reset mode %d\n",
1322 switch(csiescseq
.mode
) {
1325 fprintf(stderr
, "erresc: unknown csi ");
1329 case '@': /* ICH -- Insert <n> blank char */
1330 DEFAULT(csiescseq
.arg
[0], 1);
1331 tinsertblank(csiescseq
.arg
[0]);
1333 case 'A': /* CUU -- Cursor <n> Up */
1335 DEFAULT(csiescseq
.arg
[0], 1);
1336 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1338 case 'B': /* CUD -- Cursor <n> Down */
1339 DEFAULT(csiescseq
.arg
[0], 1);
1340 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1342 case 'C': /* CUF -- Cursor <n> Forward */
1344 DEFAULT(csiescseq
.arg
[0], 1);
1345 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1347 case 'D': /* CUB -- Cursor <n> Backward */
1348 DEFAULT(csiescseq
.arg
[0], 1);
1349 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1351 case 'E': /* CNL -- Cursor <n> Down and first col */
1352 DEFAULT(csiescseq
.arg
[0], 1);
1353 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1355 case 'F': /* CPL -- Cursor <n> Up and first col */
1356 DEFAULT(csiescseq
.arg
[0], 1);
1357 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1359 case 'g': /* TBC -- Tabulation clear */
1360 switch (csiescseq
.arg
[0]) {
1361 case 0: /* clear current tab stop */
1362 term
.tabs
[term
.c
.x
] = 0;
1364 case 3: /* clear all the tabs */
1365 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1371 case 'G': /* CHA -- Move to <col> */
1373 DEFAULT(csiescseq
.arg
[0], 1);
1374 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1376 case 'H': /* CUP -- Move to <row> <col> */
1378 DEFAULT(csiescseq
.arg
[0], 1);
1379 DEFAULT(csiescseq
.arg
[1], 1);
1380 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1382 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1383 DEFAULT(csiescseq
.arg
[0], 1);
1384 while(csiescseq
.arg
[0]--)
1387 case 'J': /* ED -- Clear screen */
1389 switch(csiescseq
.arg
[0]) {
1391 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1392 if(term
.c
.y
< term
.row
-1)
1393 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1397 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1398 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1401 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1407 case 'K': /* EL -- Clear line */
1408 switch(csiescseq
.arg
[0]) {
1410 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1413 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1416 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1420 case 'S': /* SU -- Scroll <n> line up */
1421 DEFAULT(csiescseq
.arg
[0], 1);
1422 tscrollup(term
.top
, csiescseq
.arg
[0]);
1424 case 'T': /* SD -- Scroll <n> line down */
1425 DEFAULT(csiescseq
.arg
[0], 1);
1426 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1428 case 'L': /* IL -- Insert <n> blank lines */
1429 DEFAULT(csiescseq
.arg
[0], 1);
1430 tinsertblankline(csiescseq
.arg
[0]);
1432 case 'l': /* RM -- Reset Mode */
1433 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1435 case 'M': /* DL -- Delete <n> lines */
1436 DEFAULT(csiescseq
.arg
[0], 1);
1437 tdeleteline(csiescseq
.arg
[0]);
1439 case 'X': /* ECH -- Erase <n> char */
1440 DEFAULT(csiescseq
.arg
[0], 1);
1441 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1443 case 'P': /* DCH -- Delete <n> char */
1444 DEFAULT(csiescseq
.arg
[0], 1);
1445 tdeletechar(csiescseq
.arg
[0]);
1447 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1448 DEFAULT(csiescseq
.arg
[0], 1);
1449 while(csiescseq
.arg
[0]--)
1452 case 'd': /* VPA -- Move to <row> */
1453 DEFAULT(csiescseq
.arg
[0], 1);
1454 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1456 case 'h': /* SM -- Set terminal mode */
1457 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1459 case 'm': /* SGR -- Terminal attribute (color) */
1460 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1462 case 'r': /* DECSTBM -- Set Scrolling Region */
1466 DEFAULT(csiescseq
.arg
[0], 1);
1467 DEFAULT(csiescseq
.arg
[1], term
.row
);
1468 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1472 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1473 tcursor(CURSOR_SAVE
);
1475 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1476 tcursor(CURSOR_LOAD
);
1485 for(i
= 0; i
< csiescseq
.len
; i
++) {
1486 uint c
= csiescseq
.buf
[i
] & 0xff;
1487 if(isprint(c
)) putchar(c
);
1488 else if(c
== '\n') printf("(\\n)");
1489 else if(c
== '\r') printf("(\\r)");
1490 else if(c
== 0x1b) printf("(\\e)");
1491 else printf("(%02x)", c
);
1498 memset(&csiescseq
, 0, sizeof(csiescseq
));
1506 * TODO: make this being useful in case of color palette change.
1512 switch(strescseq
.type
) {
1513 case ']': /* OSC -- Operating System Command */
1519 * TODO: Handle special chars in string, like umlauts.
1522 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1526 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1528 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1531 fprintf(stderr
, "erresc: unknown str ");
1536 case 'k': /* old title set compatibility */
1537 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1539 case 'P': /* DSC -- Device Control String */
1540 case '_': /* APC -- Application Program Command */
1541 case '^': /* PM -- Privacy Message */
1543 fprintf(stderr
, "erresc: unknown str ");
1553 * TODO: Implement parsing like for CSI when required.
1554 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1562 printf("ESC%c", strescseq
.type
);
1563 for(i
= 0; i
< strescseq
.len
; i
++) {
1564 uint c
= strescseq
.buf
[i
] & 0xff;
1565 if(isprint(c
)) putchar(c
);
1566 else if(c
== '\n') printf("(\\n)");
1567 else if(c
== '\r') printf("(\\r)");
1568 else if(c
== 0x1b) printf("(\\e)");
1569 else printf("(%02x)", c
);
1576 memset(&strescseq
, 0, sizeof(strescseq
));
1580 tputtab(bool forward
) {
1581 unsigned x
= term
.c
.x
;
1586 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1591 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1594 tmoveto(x
, term
.c
.y
);
1604 if(term
.esc
& ESC_START
) {
1605 if(term
.esc
& ESC_CSI
) {
1606 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1607 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1609 csiparse(), csihandle();
1611 } else if(term
.esc
& ESC_STR
) {
1614 term
.esc
= ESC_START
| ESC_STR_END
;
1616 case '\a': /* backwards compatibility to xterm */
1621 strescseq
.buf
[strescseq
.len
++] = ascii
;
1622 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1627 } else if(term
.esc
& ESC_STR_END
) {
1631 } else if(term
.esc
& ESC_ALTCHARSET
) {
1633 case '0': /* Line drawing crap */
1634 term
.c
.attr
.mode
|= ATTR_GFX
;
1636 case 'B': /* Back to regular text */
1637 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1640 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1646 term
.esc
|= ESC_CSI
;
1648 case 'P': /* DCS -- Device Control String */
1649 case '_': /* APC -- Application Program Command */
1650 case '^': /* PM -- Privacy Message */
1651 case ']': /* OSC -- Operating System Command */
1652 case 'k': /* old title set compatibility */
1654 strescseq
.type
= ascii
;
1655 term
.esc
|= ESC_STR
;
1658 term
.esc
|= ESC_ALTCHARSET
;
1660 case 'D': /* IND -- Linefeed */
1661 if(term
.c
.y
== term
.bot
)
1662 tscrollup(term
.top
, 1);
1664 tmoveto(term
.c
.x
, term
.c
.y
+1);
1667 case 'E': /* NEL -- Next line */
1668 tnewline(1); /* always go to first col */
1671 case 'H': /* HTS -- Horizontal tab stop */
1672 term
.tabs
[term
.c
.x
] = 1;
1675 case 'M': /* RI -- Reverse index */
1676 if(term
.c
.y
== term
.top
)
1677 tscrolldown(term
.top
, 1);
1679 tmoveto(term
.c
.x
, term
.c
.y
-1);
1682 case 'c': /* RIS -- Reset to inital state */
1687 case '=': /* DECPAM -- Application keypad */
1688 term
.mode
|= MODE_APPKEYPAD
;
1691 case '>': /* DECPNM -- Normal keypad */
1692 term
.mode
&= ~MODE_APPKEYPAD
;
1695 case '7': /* DECSC -- Save Cursor */
1696 tcursor(CURSOR_SAVE
);
1699 case '8': /* DECRC -- Restore Cursor */
1700 tcursor(CURSOR_LOAD
);
1703 case '\\': /* ST -- Stop */
1707 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1708 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1713 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1716 case '\0': /* padding character, do nothing */
1722 tmoveto(term
.c
.x
-1, term
.c
.y
);
1725 tmoveto(0, term
.c
.y
);
1730 /* go to first col if the mode is set */
1731 tnewline(IS_SET(MODE_CRLF
));
1734 if(!(xw
.state
& WIN_FOCUSED
))
1739 term
.esc
= ESC_START
;
1742 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1743 tnewline(1); /* always go to first col */
1745 if(term
.c
.x
+1 < term
.col
)
1746 tmoveto(term
.c
.x
+1, term
.c
.y
);
1748 term
.c
.state
|= CURSOR_WRAPNEXT
;
1754 tresize(int col
, int row
) {
1756 int minrow
= MIN(row
, term
.row
);
1757 int mincol
= MIN(col
, term
.col
);
1758 int slide
= term
.c
.y
- row
+ 1;
1760 if(col
< 1 || row
< 1)
1763 /* free unneeded rows */
1766 /* slide screen to keep cursor where we expect it -
1767 * tscrollup would work here, but we can optimize to
1768 * memmove because we're freeing the earlier lines */
1769 for(/* i = 0 */; i
< slide
; i
++) {
1773 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1774 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1776 for(i
+= row
; i
< term
.row
; i
++) {
1781 /* resize to new height */
1782 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
1783 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
1784 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1785 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1787 /* resize each row to new width, zero-pad if needed */
1788 for(i
= 0; i
< minrow
; i
++) {
1790 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
1791 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
1792 for(x
= mincol
; x
< col
; x
++) {
1793 term
.line
[i
][x
].state
= 0;
1794 term
.alt
[i
][x
].state
= 0;
1798 /* allocate any new rows */
1799 for(/* i == minrow */; i
< row
; i
++) {
1801 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1802 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1804 if(col
> term
.col
) {
1805 bool *bp
= term
.tabs
+ term
.col
;
1807 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1808 while(--bp
> term
.tabs
&& !*bp
)
1810 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1813 /* update terminal size */
1814 term
.col
= col
, term
.row
= row
;
1815 /* make use of the LIMIT in tmoveto */
1816 tmoveto(term
.c
.x
, term
.c
.y
);
1817 /* reset scrolling region */
1818 tsetscroll(0, row
-1);
1824 xresize(int col
, int row
) {
1825 xw
.w
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1826 xw
.h
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1833 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1835 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1836 for(i
= 0; i
< LEN(colorname
); i
++) {
1839 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1841 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1843 dc
.col
[i
] = color
.pixel
;
1846 /* load colors [16-255] ; same colors as xterm */
1847 for(i
= 16, r
= 0; r
< 6; r
++)
1848 for(g
= 0; g
< 6; g
++)
1849 for(b
= 0; b
< 6; b
++) {
1850 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1851 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1852 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1853 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1855 fprintf(stderr
, "Could not allocate color %d\n", i
);
1857 dc
.col
[i
] = color
.pixel
;
1861 for(r
= 0; r
< 24; r
++, i
++) {
1862 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1863 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1865 fprintf(stderr
, "Could not allocate color %d\n", i
);
1867 dc
.col
[i
] = color
.pixel
;
1872 xclear(int x1
, int y1
, int x2
, int y2
) {
1873 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1874 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1875 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1876 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1881 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1882 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1883 XSizeHints
*sizeh
= NULL
;
1885 sizeh
= XAllocSizeHints();
1886 if(xw
.isfixed
== False
) {
1887 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
1888 sizeh
->height
= xw
.h
;
1889 sizeh
->width
= xw
.w
;
1890 sizeh
->height_inc
= xw
.ch
;
1891 sizeh
->width_inc
= xw
.cw
;
1892 sizeh
->base_height
= 2*BORDER
;
1893 sizeh
->base_width
= 2*BORDER
;
1895 sizeh
->flags
= PMaxSize
| PMinSize
;
1896 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
1897 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
1900 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
1905 xinitfont(char *fontstr
) {
1907 char *def
, **missing
;
1911 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1914 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1915 XFreeStringList(missing
);
1921 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1922 XFontStruct
**xfonts
;
1926 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1927 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1928 for(i
= 0; i
< n
; i
++) {
1929 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1930 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1931 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1932 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1938 initfonts(char *fontstr
, char *bfontstr
, char *ifontstr
, char *ibfontstr
) {
1939 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
)
1940 die("Can't load font %s\n", fontstr
);
1941 if((dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1942 die("Can't load bfont %s\n", bfontstr
);
1943 if((dc
.ifont
.set
= xinitfont(ifontstr
)) == NULL
)
1944 die("Can't load ifont %s\n", ifontstr
);
1945 if((dc
.ibfont
.set
= xinitfont(ibfontstr
)) == NULL
)
1946 die("Can't load ibfont %s\n", ibfontstr
);
1948 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1949 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1950 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1951 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1952 xgetfontinfo(dc
.ifont
.set
, &dc
.ifont
.ascent
, &dc
.ifont
.descent
,
1953 &dc
.ifont
.lbearing
, &dc
.ifont
.rbearing
);
1954 xgetfontinfo(dc
.ibfont
.set
, &dc
.ibfont
.ascent
, &dc
.ibfont
.descent
,
1955 &dc
.ibfont
.lbearing
, &dc
.ibfont
.rbearing
);
1960 XSetWindowAttributes attrs
;
1963 int sw
, sh
, major
, minor
;
1965 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1966 die("Can't open display\n");
1967 xw
.scr
= XDefaultScreen(xw
.dpy
);
1970 initfonts(FONT
, BOLDFONT
, ITALICFONT
, ITALICBOLDFONT
);
1972 /* XXX: Assuming same size for bold font */
1973 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1974 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1977 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1980 /* adjust fixed window geometry */
1982 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
1983 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
1985 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
1987 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
1992 /* window - default size */
1993 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
1994 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
1999 attrs
.background_pixel
= dc
.col
[DefaultBG
];
2000 attrs
.border_pixel
= dc
.col
[DefaultBG
];
2001 attrs
.bit_gravity
= NorthWestGravity
;
2002 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2003 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2004 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2005 attrs
.colormap
= xw
.cmap
;
2007 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2008 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2009 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2010 XDefaultVisual(xw
.dpy
, xw
.scr
),
2011 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2014 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2015 die("Xdbe extension is not present\n");
2016 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2019 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2020 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2021 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2022 XNFocusWindow
, xw
.win
, NULL
);
2024 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2026 /* white cursor, black outline */
2027 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2028 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2029 XRecolorCursor(xw
.dpy
, cursor
,
2030 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2031 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2033 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2036 XMapWindow(xw
.dpy
, xw
.win
);
2042 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2043 int fg
= base
.fg
, bg
= base
.bg
, temp
;
2044 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
2045 XFontSet fontset
= dc
.font
.set
;
2048 /* only switch default fg/bg if term is in RV mode */
2049 if(IS_SET(MODE_REVERSE
)) {
2056 if(base
.mode
& ATTR_REVERSE
)
2057 temp
= fg
, fg
= bg
, bg
= temp
;
2059 if(base
.mode
& ATTR_BOLD
) {
2061 fontset
= dc
.bfont
.set
;
2064 if(base
.mode
& ATTR_ITALIC
)
2065 fontset
= dc
.ifont
.set
;
2066 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2067 fontset
= dc
.ibfont
.set
;
2069 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
2070 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
2072 if(base
.mode
& ATTR_GFX
) {
2073 for(i
= 0; i
< bytelen
; i
++) {
2074 char c
= gfx
[(uint
)s
[i
] % 256];
2077 else if(s
[i
] > 0x5f)
2082 XmbDrawImageString(xw
.dpy
, xw
.buf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
2084 if(base
.mode
& ATTR_UNDERLINE
)
2085 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
2090 static int oldx
= 0;
2091 static int oldy
= 0;
2093 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2095 LIMIT(oldx
, 0, term
.col
-1);
2096 LIMIT(oldy
, 0, term
.row
-1);
2098 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2099 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2101 /* remove the old cursor */
2102 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2103 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2104 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2106 xclear(oldx
, oldy
, oldx
, oldy
);
2108 /* draw the new one */
2109 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2110 if(!(xw
.state
& WIN_FOCUSED
))
2113 if(IS_SET(MODE_REVERSE
))
2114 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2117 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2118 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2124 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2129 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2132 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2133 nanosleep(&tv
, NULL
);
2138 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2140 drawregion(0, 0, term
.col
, term
.row
);
2141 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2145 drawregion(int x1
, int y1
, int x2
, int y2
) {
2146 int ic
, ib
, x
, y
, ox
, sl
;
2148 char buf
[DRAW_BUF_SIZ
];
2149 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2151 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2153 if(!(xw
.state
& WIN_VISIBLE
))
2156 for(y
= y1
; y
< y2
; y
++) {
2159 xclear(0, y
, term
.col
, y
);
2161 base
= term
.line
[y
][0];
2163 for(x
= x1
; x
< x2
; x
++) {
2164 new = term
.line
[y
][x
];
2165 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2166 new.mode
^= ATTR_REVERSE
;
2167 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2168 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2169 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2172 if(new.state
& GLYPH_SET
) {
2177 sl
= utf8size(new.c
);
2178 memcpy(buf
+ib
, new.c
, sl
);
2184 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2190 expose(XEvent
*ev
) {
2191 XExposeEvent
*e
= &ev
->xexpose
;
2192 if(xw
.state
& WIN_REDRAW
) {
2194 xw
.state
&= ~WIN_REDRAW
;
2199 visibility(XEvent
*ev
) {
2200 XVisibilityEvent
*e
= &ev
->xvisibility
;
2201 if(e
->state
== VisibilityFullyObscured
)
2202 xw
.state
&= ~WIN_VISIBLE
;
2203 else if(!(xw
.state
& WIN_VISIBLE
))
2204 /* need a full redraw for next Expose, not just a buf copy */
2205 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2210 xw
.state
&= ~WIN_VISIBLE
;
2214 xseturgency(int add
) {
2215 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2216 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2217 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2223 if(ev
->type
== FocusIn
) {
2224 xw
.state
|= WIN_FOCUSED
;
2227 xw
.state
&= ~WIN_FOCUSED
;
2231 kmap(KeySym k
, uint state
) {
2234 for(i
= 0; i
< LEN(key
); i
++) {
2235 uint mask
= key
[i
].mask
;
2236 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2237 return (char*)key
[i
].s
;
2243 kpress(XEvent
*ev
) {
2244 XKeyEvent
*e
= &ev
->xkey
;
2253 meta
= e
->state
& Mod1Mask
;
2254 shift
= e
->state
& ShiftMask
;
2255 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2257 /* 1. custom keys from config.h */
2258 if((customkey
= kmap(ksym
, e
->state
)))
2259 ttywrite(customkey
, strlen(customkey
));
2260 /* 2. hardcoded (overrides X lookup) */
2267 /* XXX: shift up/down doesn't work */
2268 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2276 if(IS_SET(MODE_CRLF
))
2277 ttywrite("\r\n", 2);
2284 if(meta
&& len
== 1)
2285 ttywrite("\033", 1);
2293 cmessage(XEvent
*e
) {
2295 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2296 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2297 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2298 xw
.state
|= WIN_FOCUSED
;
2300 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2301 xw
.state
&= ~WIN_FOCUSED
;
2310 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2313 xw
.w
= e
->xconfigure
.width
;
2314 xw
.h
= e
->xconfigure
.height
;
2315 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2316 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2317 if(col
== term
.col
&& row
== term
.row
)
2319 if(tresize(col
, row
))
2322 ttyresize(col
, row
);
2329 int xfd
= XConnectionNumber(xw
.dpy
);
2333 FD_SET(cmdfd
, &rfd
);
2335 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
2338 die("select failed: %s\n", SERRNO
);
2340 if(FD_ISSET(cmdfd
, &rfd
))
2343 while(XPending(xw
.dpy
)) {
2344 XNextEvent(xw
.dpy
, &ev
);
2345 if(XFilterEvent(&ev
, xw
.win
))
2347 if(handler
[ev
.type
])
2348 (handler
[ev
.type
])(&ev
);
2357 main(int argc
, char *argv
[]) {
2358 int i
, bitm
, xr
, yr
;
2359 unsigned int wr
, hr
;
2361 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2364 for(i
= 1; i
< argc
; i
++) {
2365 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2367 if(++i
< argc
) opt_title
= argv
[i
];
2370 if(++i
< argc
) opt_class
= argv
[i
];
2373 if(++i
< argc
) opt_embed
= argv
[i
];
2376 if(++i
< argc
) opt_io
= argv
[i
];
2379 /* eat every remaining arguments */
2380 if(++i
< argc
) opt_cmd
= &argv
[i
];
2386 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2391 if(bitm
& WidthValue
)
2393 if(bitm
& HeightValue
)
2395 if(bitm
& XNegative
&& xw
.fx
== 0)
2397 if(bitm
& XNegative
&& xw
.fy
== 0)
2400 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2410 setlocale(LC_CTYPE
, "");