1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/extensions/Xdbe.h>
31 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
33 #elif defined(__FreeBSD__) || defined(__DragonFly__)
38 "st " VERSION " (c) 2010-2012 st engineers\n" \
39 "usage: st [-t title] [-c class] [-g geometry]" \
40 " [-w windowid] [-v] [-f file] [-e command...]\n"
43 #define XEMBED_FOCUS_IN 4
44 #define XEMBED_FOCUS_OUT 5
47 #define ESC_BUF_SIZ 256
48 #define ESC_ARG_SIZ 16
49 #define STR_BUF_SIZ 256
50 #define STR_ARG_SIZ 16
51 #define DRAW_BUF_SIZ 1024
53 #define XK_NO_MOD UINT_MAX
56 #define SELECT_TIMEOUT (20*1000) /* 20 ms */
57 #define DRAW_TIMEOUT (20*1000) /* 20 ms */
58 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
60 #define SERRNO strerror(errno)
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 #define MAX(a, b) ((a) < (b) ? (b) : (a))
63 #define LEN(a) (sizeof(a) / sizeof(a[0]))
64 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
65 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
66 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
67 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
68 #define IS_SET(flag) (term.mode & (flag))
69 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
70 #define X2COL(x) (((x) - BORDER)/xw.cw)
71 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
73 enum glyph_attribute
{
83 enum cursor_movement
{
110 MODE_MOUSEMOTION
= 64,
118 ESC_STR
= 4, /* DSC, OSC, PM, APC */
120 ESC_STR_END
= 16, /* a final string was encountered */
131 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
133 typedef unsigned char uchar
;
134 typedef unsigned int uint
;
135 typedef unsigned long ulong
;
136 typedef unsigned short ushort
;
139 char c
[UTF_SIZ
]; /* character code */
140 uchar mode
; /* attribute flags */
141 ushort fg
; /* foreground */
142 ushort bg
; /* background */
143 uchar state
; /* state flags */
149 Glyph attr
; /* current char attributes */
155 /* CSI Escape sequence structs */
156 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
158 char buf
[ESC_BUF_SIZ
]; /* raw string */
159 int len
; /* raw string length */
161 int arg
[ESC_ARG_SIZ
];
162 int narg
; /* nb of args */
166 /* STR Escape sequence structs */
167 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
169 char type
; /* ESC type ... */
170 char buf
[STR_BUF_SIZ
]; /* raw string */
171 int len
; /* raw string length */
172 char *args
[STR_ARG_SIZ
];
173 int narg
; /* nb of args */
176 /* Internal representation of the screen */
178 int row
; /* nb row */
179 int col
; /* nb col */
180 Line
* line
; /* screen */
181 Line
* alt
; /* alternate screen */
182 bool* dirty
; /* dirtyness of lines */
183 TCursor c
; /* cursor */
184 int top
; /* top scroll limit */
185 int bot
; /* bottom scroll limit */
186 int mode
; /* terminal mode flags */
187 int esc
; /* escape state flags */
191 /* Purely graphic info */
201 Bool isfixed
; /* is fixed geometry? */
202 int fx
, fy
, fw
, fh
; /* fixed geometry */
203 int w
; /* window width */
204 int h
; /* window height */
205 int ch
; /* char height */
206 int cw
; /* char width */
207 char state
; /* focus, redraw, visible */
208 struct timeval lastdraw
;
218 /* TODO: use better name for vars... */
223 struct {int x
, y
;} b
, e
;
227 struct timeval tclick1
;
228 struct timeval tclick2
;
233 /* Drawing Context */
235 ulong col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
243 } font
, bfont
, ifont
, ibfont
;
246 static void die(const char*, ...);
247 static void draw(void);
248 static void redraw(void);
249 static void drawregion(int, int, int, int);
250 static void execsh(void);
251 static void sigchld(int);
252 static void run(void);
253 static bool last_draw_too_old(void);
255 static void csidump(void);
256 static void csihandle(void);
257 static void csiparse(void);
258 static void csireset(void);
259 static void strdump(void);
260 static void strhandle(void);
261 static void strparse(void);
262 static void strreset(void);
264 static void tclearregion(int, int, int, int);
265 static void tcursor(int);
266 static void tdeletechar(int);
267 static void tdeleteline(int);
268 static void tinsertblank(int);
269 static void tinsertblankline(int);
270 static void tmoveto(int, int);
271 static void tnew(int, int);
272 static void tnewline(int);
273 static void tputtab(bool);
274 static void tputc(char*);
275 static void treset(void);
276 static int tresize(int, int);
277 static void tscrollup(int, int);
278 static void tscrolldown(int, int);
279 static void tsetattr(int*, int);
280 static void tsetchar(char*);
281 static void tsetscroll(int, int);
282 static void tswapscreen(void);
283 static void tsetdirt(int, int);
284 static void tsetmode(bool, bool, int *, int);
285 static void tfulldirt(void);
287 static void ttynew(void);
288 static void ttyread(void);
289 static void ttyresize(int, int);
290 static void ttywrite(const char *, size_t);
292 static void xdraws(char *, Glyph
, int, int, int, int);
293 static void xhints(void);
294 static void xclear(int, int, int, int);
295 static void xcopy(void);
296 static void xdrawcursor(void);
297 static void xinit(void);
298 static void xloadcols(void);
299 static void xresettitle(void);
300 static void xseturgency(int);
301 static void xsetsel(char*);
302 static void xresize(int, int);
304 static void expose(XEvent
*);
305 static void visibility(XEvent
*);
306 static void unmap(XEvent
*);
307 static char* kmap(KeySym
, uint
);
308 static void kpress(XEvent
*);
309 static void cmessage(XEvent
*);
310 static void resize(XEvent
*);
311 static void focus(XEvent
*);
312 static void brelease(XEvent
*);
313 static void bpress(XEvent
*);
314 static void bmotion(XEvent
*);
315 static void selnotify(XEvent
*);
316 static void selclear(XEvent
*);
317 static void selrequest(XEvent
*);
319 static void selinit(void);
320 static inline bool selected(int, int);
321 static void selcopy(void);
322 static void selpaste(void);
323 static void selscroll(int, int);
325 static int utf8decode(char *, long *);
326 static int utf8encode(long *, char *);
327 static int utf8size(char *);
328 static int isfullutf8(char *, int);
330 static void *xmalloc(size_t);
331 static void *xrealloc(void *, size_t);
333 static void (*handler
[LASTEvent
])(XEvent
*) = {
335 [ClientMessage
] = cmessage
,
336 [ConfigureNotify
] = resize
,
337 [VisibilityNotify
] = visibility
,
338 [UnmapNotify
] = unmap
,
342 [MotionNotify
] = bmotion
,
343 [ButtonPress
] = bpress
,
344 [ButtonRelease
] = brelease
,
345 [SelectionClear
] = selclear
,
346 [SelectionNotify
] = selnotify
,
347 [SelectionRequest
] = selrequest
,
354 static CSIEscape csiescseq
;
355 static STREscape strescseq
;
358 static Selection sel
;
359 static int iofd
= -1;
360 static char **opt_cmd
= NULL
;
361 static char *opt_io
= NULL
;
362 static char *opt_title
= NULL
;
363 static char *opt_embed
= NULL
;
364 static char *opt_class
= NULL
;
367 xmalloc(size_t len
) {
368 void *p
= malloc(len
);
370 die("Out of memory");
375 xrealloc(void *p
, size_t len
) {
376 if((p
= realloc(p
, len
)) == NULL
)
377 die("Out of memory");
382 utf8decode(char *s
, long *u
) {
388 if(~c
& B7
) { /* 0xxxxxxx */
391 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
392 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
394 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
395 *u
= c
&(B3
|B2
|B1
|B0
);
397 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
402 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
404 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
407 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
409 if((n
== 1 && *u
< 0x80) ||
410 (n
== 2 && *u
< 0x800) ||
411 (n
== 3 && *u
< 0x10000) ||
412 (*u
>= 0xD800 && *u
<= 0xDFFF))
421 utf8encode(long *u
, char *s
) {
429 *sp
= uc
; /* 0xxxxxxx */
431 } else if(*u
< 0x800) {
432 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
434 } else if(uc
< 0x10000) {
435 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
437 } else if(uc
<= 0x10FFFF) {
438 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
443 for(i
=n
,++sp
; i
>0; --i
,++sp
)
444 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
454 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
455 UTF-8 otherwise return 0 */
457 isfullutf8(char *s
, int b
) {
465 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
467 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
469 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
471 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
473 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
474 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
486 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
488 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
496 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
497 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
501 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
502 if(sel
.xtarget
== None
)
503 sel
.xtarget
= XA_STRING
;
507 selected(int x
, int y
) {
508 if(sel
.ey
== y
&& sel
.by
== y
) {
509 int bx
= MIN(sel
.bx
, sel
.ex
);
510 int ex
= MAX(sel
.bx
, sel
.ex
);
511 return BETWEEN(x
, bx
, ex
);
513 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
514 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
518 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
520 *b
= e
->xbutton
.button
;
522 *x
= X2COL(e
->xbutton
.x
);
523 *y
= Y2ROW(e
->xbutton
.y
);
524 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
525 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
526 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
527 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
531 mousereport(XEvent
*e
) {
532 int x
= X2COL(e
->xbutton
.x
);
533 int y
= Y2ROW(e
->xbutton
.y
);
534 int button
= e
->xbutton
.button
;
535 int state
= e
->xbutton
.state
;
536 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
537 static int ob
, ox
, oy
;
540 if(e
->xbutton
.type
== MotionNotify
) {
541 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
545 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
551 if(e
->xbutton
.type
== ButtonPress
) {
557 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
558 + (state
& Mod4Mask
? 8 : 0)
559 + (state
& ControlMask
? 16 : 0);
561 ttywrite(buf
, sizeof(buf
));
566 if(IS_SET(MODE_MOUSE
))
568 else if(e
->xbutton
.button
== Button1
) {
570 tsetdirt(sel
.b
.y
, sel
.e
.y
);
572 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
573 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
580 int x
, y
, bufsize
, is_selected
= 0;
586 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
587 ptr
= str
= xmalloc(bufsize
);
589 /* append every set & selected glyph to the selection */
590 for(y
= 0; y
< term
.row
; y
++) {
591 for(x
= 0; x
< term
.col
; x
++) {
592 is_selected
= selected(x
, y
);
593 if((term
.line
[y
][x
].state
& GLYPH_SET
) && is_selected
) {
594 int size
= utf8size(term
.line
[y
][x
].c
);
595 memcpy(ptr
, term
.line
[y
][x
].c
, size
);
600 /* \n at the end of every selected line except for the last one */
601 if(is_selected
&& y
< sel
.e
.y
)
606 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
611 selnotify(XEvent
*e
) {
612 ulong nitems
, ofs
, rem
;
619 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
620 False
, AnyPropertyType
, &type
, &format
,
621 &nitems
, &rem
, &data
)) {
622 fprintf(stderr
, "Clipboard allocation failed\n");
625 ttywrite((const char *) data
, nitems
* format
/ 8);
627 /* number of 32-bit chunks returned */
628 ofs
+= nitems
* format
/ 32;
634 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
637 void selclear(XEvent
*e
) {
641 tsetdirt(sel
.b
.y
, sel
.e
.y
);
646 selrequest(XEvent
*e
) {
647 XSelectionRequestEvent
*xsre
;
651 xsre
= (XSelectionRequestEvent
*) e
;
652 xev
.type
= SelectionNotify
;
653 xev
.requestor
= xsre
->requestor
;
654 xev
.selection
= xsre
->selection
;
655 xev
.target
= xsre
->target
;
656 xev
.time
= xsre
->time
;
660 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
661 if(xsre
->target
== xa_targets
) {
662 /* respond with the supported type */
663 Atom string
= sel
.xtarget
;
664 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
665 XA_ATOM
, 32, PropModeReplace
,
666 (uchar
*) &string
, 1);
667 xev
.property
= xsre
->property
;
668 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
669 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
670 xsre
->target
, 8, PropModeReplace
,
671 (uchar
*) sel
.clip
, strlen(sel
.clip
));
672 xev
.property
= xsre
->property
;
675 /* all done, send a notification to the listener */
676 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
677 fprintf(stderr
, "Error sending SelectionNotify event\n");
682 /* register the selection for both the clipboard and the primary */
688 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
690 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
691 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
697 brelease(XEvent
*e
) {
698 if(IS_SET(MODE_MOUSE
)) {
702 if(e
->xbutton
.button
== Button2
)
704 else if(e
->xbutton
.button
== Button1
) {
706 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
707 term
.dirty
[sel
.ey
] = 1;
708 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
711 gettimeofday(&now
, NULL
);
713 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
714 /* triple click on the line */
715 sel
.b
.x
= sel
.bx
= 0;
716 sel
.e
.x
= sel
.ex
= term
.col
;
717 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
719 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
720 /* double click to select word */
722 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
723 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
725 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
726 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
728 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
734 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
735 gettimeofday(&sel
.tclick1
, NULL
);
741 if(IS_SET(MODE_MOUSE
)) {
746 int oldey
= sel
.ey
, oldex
= sel
.ex
;
747 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
749 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
750 int starty
= MIN(oldey
, sel
.ey
);
751 int endy
= MAX(oldey
, sel
.ey
);
752 tsetdirt(starty
, endy
);
759 die(const char *errstr
, ...) {
762 va_start(ap
, errstr
);
763 vfprintf(stderr
, errstr
, ap
);
771 char *envshell
= getenv("SHELL");
777 signal(SIGCHLD
, SIG_DFL
);
778 signal(SIGHUP
, SIG_DFL
);
779 signal(SIGINT
, SIG_DFL
);
780 signal(SIGQUIT
, SIG_DFL
);
781 signal(SIGTERM
, SIG_DFL
);
782 signal(SIGALRM
, SIG_DFL
);
784 DEFAULT(envshell
, SHELL
);
785 putenv("TERM="TNAME
);
786 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
787 execvp(args
[0], args
);
794 if(waitpid(pid
, &stat
, 0) < 0)
795 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
797 exit(WEXITSTATUS(stat
));
806 /* seems to work fine on linux, openbsd and freebsd */
807 struct winsize w
= {term
.row
, term
.col
, 0, 0};
808 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
809 die("openpty failed: %s\n", SERRNO
);
811 switch(pid
= fork()) {
813 die("fork failed\n");
816 setsid(); /* create a new process group */
817 dup2(s
, STDIN_FILENO
);
818 dup2(s
, STDOUT_FILENO
);
819 dup2(s
, STDERR_FILENO
);
820 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
821 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
829 signal(SIGCHLD
, sigchld
);
831 if(!strcmp(opt_io
, "-")) {
832 iofd
= STDOUT_FILENO
;
834 if((iofd
= open(opt_io
, O_WRONLY
| O_CREAT
, 0666)) < 0) {
835 fprintf(stderr
, "Error opening %s:%s\n",
836 opt_io
, strerror(errno
));
846 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
848 fprintf(stderr
, "\n");
853 static char buf
[BUFSIZ
];
854 static int buflen
= 0;
857 int charsize
; /* size of utf8 char in bytes */
861 /* append read bytes to unprocessed bytes */
862 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
863 die("Couldn't read from shell: %s\n", SERRNO
);
865 /* process every complete utf8 char */
868 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
869 charsize
= utf8decode(ptr
, &utf8c
);
870 utf8encode(&utf8c
, s
);
876 /* keep any uncomplete utf8 char for the next call */
877 memmove(buf
, ptr
, buflen
);
881 ttywrite(const char *s
, size_t n
) {
882 if(write(cmdfd
, s
, n
) == -1)
883 die("write error on tty: %s\n", SERRNO
);
887 ttyresize(int x
, int y
) {
894 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
895 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
899 tsetdirt(int top
, int bot
)
903 LIMIT(top
, 0, term
.row
-1);
904 LIMIT(bot
, 0, term
.row
-1);
906 for(i
= top
; i
<= bot
; i
++)
913 tsetdirt(0, term
.row
-1);
920 if(mode
== CURSOR_SAVE
)
922 else if(mode
== CURSOR_LOAD
)
923 term
.c
= c
, tmoveto(c
.x
, c
.y
);
933 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
935 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
936 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
938 term
.top
= 0, term
.bot
= term
.row
- 1;
939 term
.mode
= MODE_WRAP
;
940 tclearregion(0, 0, term
.col
-1, term
.row
-1);
944 tnew(int col
, int row
) {
945 /* set screen size */
946 term
.row
= row
, term
.col
= col
;
947 term
.line
= xmalloc(term
.row
* sizeof(Line
));
948 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
949 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
950 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
952 for(row
= 0; row
< term
.row
; row
++) {
953 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
954 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
957 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
964 Line
* tmp
= term
.line
;
965 term
.line
= term
.alt
;
967 term
.mode
^= MODE_ALTSCREEN
;
972 tscrolldown(int orig
, int n
) {
976 LIMIT(n
, 0, term
.bot
-orig
+1);
978 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
980 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
982 term
.line
[i
] = term
.line
[i
-n
];
983 term
.line
[i
-n
] = temp
;
993 tscrollup(int orig
, int n
) {
996 LIMIT(n
, 0, term
.bot
-orig
+1);
998 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1000 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1001 temp
= term
.line
[i
];
1002 term
.line
[i
] = term
.line
[i
+n
];
1003 term
.line
[i
+n
] = temp
;
1006 term
.dirty
[i
+n
] = 1;
1009 selscroll(orig
, -n
);
1013 selscroll(int orig
, int n
) {
1017 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1018 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1022 if(sel
.by
< term
.top
) {
1026 if(sel
.ey
> term
.bot
) {
1030 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1031 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1036 tnewline(int first_col
) {
1039 tscrollup(term
.top
, 1);
1042 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1047 /* int noarg = 1; */
1048 char *p
= csiescseq
.buf
;
1052 csiescseq
.priv
= 1, p
++;
1054 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1055 while(isdigit(*p
)) {
1056 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1057 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1059 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
1060 csiescseq
.narg
++, p
++;
1062 csiescseq
.mode
= *p
;
1070 tmoveto(int x
, int y
) {
1071 LIMIT(x
, 0, term
.col
-1);
1072 LIMIT(y
, 0, term
.row
-1);
1073 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1080 term
.dirty
[term
.c
.y
] = 1;
1081 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1082 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1083 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1087 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1091 temp
= x1
, x1
= x2
, x2
= temp
;
1093 temp
= y1
, y1
= y2
, y2
= temp
;
1095 LIMIT(x1
, 0, term
.col
-1);
1096 LIMIT(x2
, 0, term
.col
-1);
1097 LIMIT(y1
, 0, term
.row
-1);
1098 LIMIT(y2
, 0, term
.row
-1);
1100 for(y
= y1
; y
<= y2
; y
++) {
1102 for(x
= x1
; x
<= x2
; x
++)
1103 term
.line
[y
][x
].state
= 0;
1108 tdeletechar(int n
) {
1109 int src
= term
.c
.x
+ n
;
1111 int size
= term
.col
- src
;
1113 term
.dirty
[term
.c
.y
] = 1;
1115 if(src
>= term
.col
) {
1116 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1119 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1120 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1124 tinsertblank(int n
) {
1127 int size
= term
.col
- dst
;
1129 term
.dirty
[term
.c
.y
] = 1;
1131 if(dst
>= term
.col
) {
1132 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1135 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1136 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1140 tinsertblankline(int n
) {
1141 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1144 tscrolldown(term
.c
.y
, n
);
1148 tdeleteline(int n
) {
1149 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1152 tscrollup(term
.c
.y
, n
);
1156 tsetattr(int *attr
, int l
) {
1159 for(i
= 0; i
< l
; i
++) {
1162 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1163 | ATTR_ITALIC
| ATTR_BLINK
);
1164 term
.c
.attr
.fg
= DefaultFG
;
1165 term
.c
.attr
.bg
= DefaultBG
;
1168 term
.c
.attr
.mode
|= ATTR_BOLD
;
1170 case 3: /* enter standout (highlight) */
1171 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1174 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1177 term
.c
.attr
.mode
|= ATTR_BLINK
;
1180 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1184 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1186 case 23: /* leave standout (highlight) mode */
1187 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1190 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1193 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1196 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1199 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1201 if(BETWEEN(attr
[i
], 0, 255))
1202 term
.c
.attr
.fg
= attr
[i
];
1204 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1207 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1210 term
.c
.attr
.fg
= DefaultFG
;
1213 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1215 if(BETWEEN(attr
[i
], 0, 255))
1216 term
.c
.attr
.bg
= attr
[i
];
1218 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1221 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1224 term
.c
.attr
.bg
= DefaultBG
;
1227 if(BETWEEN(attr
[i
], 30, 37))
1228 term
.c
.attr
.fg
= attr
[i
] - 30;
1229 else if(BETWEEN(attr
[i
], 40, 47))
1230 term
.c
.attr
.bg
= attr
[i
] - 40;
1231 else if(BETWEEN(attr
[i
], 90, 97))
1232 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1233 else if(BETWEEN(attr
[i
], 100, 107))
1234 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1236 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1243 tsetscroll(int t
, int b
) {
1246 LIMIT(t
, 0, term
.row
-1);
1247 LIMIT(b
, 0, term
.row
-1);
1257 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1260 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1263 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1267 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1269 case 5: /* DECSCNM -- Reverve video */
1271 MODBIT(term
.mode
,set
, MODE_REVERSE
);
1272 if(mode
!= term
.mode
)
1276 MODBIT(term
.mode
, set
, MODE_WRAP
);
1279 MODBIT(term
.mode
, set
, MODE_CRLF
);
1281 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1284 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1286 case 1000: /* 1000,1002: enable xterm mouse report */
1287 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1290 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1292 case 1049: /* = 1047 and 1048 */
1295 if(IS_SET(MODE_ALTSCREEN
))
1296 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1297 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1298 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1305 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1309 "erresc: unknown private set/reset mode %d\n",
1316 MODBIT(term
.mode
, set
, MODE_INSERT
);
1320 "erresc: unknown set/reset mode %d\n",
1332 switch(csiescseq
.mode
) {
1335 fprintf(stderr
, "erresc: unknown csi ");
1339 case '@': /* ICH -- Insert <n> blank char */
1340 DEFAULT(csiescseq
.arg
[0], 1);
1341 tinsertblank(csiescseq
.arg
[0]);
1343 case 'A': /* CUU -- Cursor <n> Up */
1345 DEFAULT(csiescseq
.arg
[0], 1);
1346 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1348 case 'B': /* CUD -- Cursor <n> Down */
1349 DEFAULT(csiescseq
.arg
[0], 1);
1350 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1352 case 'C': /* CUF -- Cursor <n> Forward */
1354 DEFAULT(csiescseq
.arg
[0], 1);
1355 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1357 case 'D': /* CUB -- Cursor <n> Backward */
1358 DEFAULT(csiescseq
.arg
[0], 1);
1359 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1361 case 'E': /* CNL -- Cursor <n> Down and first col */
1362 DEFAULT(csiescseq
.arg
[0], 1);
1363 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1365 case 'F': /* CPL -- Cursor <n> Up and first col */
1366 DEFAULT(csiescseq
.arg
[0], 1);
1367 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1369 case 'g': /* TBC -- Tabulation clear */
1370 switch (csiescseq
.arg
[0]) {
1371 case 0: /* clear current tab stop */
1372 term
.tabs
[term
.c
.x
] = 0;
1374 case 3: /* clear all the tabs */
1375 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1381 case 'G': /* CHA -- Move to <col> */
1383 DEFAULT(csiescseq
.arg
[0], 1);
1384 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1386 case 'H': /* CUP -- Move to <row> <col> */
1388 DEFAULT(csiescseq
.arg
[0], 1);
1389 DEFAULT(csiescseq
.arg
[1], 1);
1390 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1392 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1393 DEFAULT(csiescseq
.arg
[0], 1);
1394 while(csiescseq
.arg
[0]--)
1397 case 'J': /* ED -- Clear screen */
1399 switch(csiescseq
.arg
[0]) {
1401 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1402 if(term
.c
.y
< term
.row
-1)
1403 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1407 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1408 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1411 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1417 case 'K': /* EL -- Clear line */
1418 switch(csiescseq
.arg
[0]) {
1420 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1423 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1426 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1430 case 'S': /* SU -- Scroll <n> line up */
1431 DEFAULT(csiescseq
.arg
[0], 1);
1432 tscrollup(term
.top
, csiescseq
.arg
[0]);
1434 case 'T': /* SD -- Scroll <n> line down */
1435 DEFAULT(csiescseq
.arg
[0], 1);
1436 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1438 case 'L': /* IL -- Insert <n> blank lines */
1439 DEFAULT(csiescseq
.arg
[0], 1);
1440 tinsertblankline(csiescseq
.arg
[0]);
1442 case 'l': /* RM -- Reset Mode */
1443 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1445 case 'M': /* DL -- Delete <n> lines */
1446 DEFAULT(csiescseq
.arg
[0], 1);
1447 tdeleteline(csiescseq
.arg
[0]);
1449 case 'X': /* ECH -- Erase <n> char */
1450 DEFAULT(csiescseq
.arg
[0], 1);
1451 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1453 case 'P': /* DCH -- Delete <n> char */
1454 DEFAULT(csiescseq
.arg
[0], 1);
1455 tdeletechar(csiescseq
.arg
[0]);
1457 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1458 DEFAULT(csiescseq
.arg
[0], 1);
1459 while(csiescseq
.arg
[0]--)
1462 case 'd': /* VPA -- Move to <row> */
1463 DEFAULT(csiescseq
.arg
[0], 1);
1464 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1466 case 'h': /* SM -- Set terminal mode */
1467 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1469 case 'm': /* SGR -- Terminal attribute (color) */
1470 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1472 case 'r': /* DECSTBM -- Set Scrolling Region */
1476 DEFAULT(csiescseq
.arg
[0], 1);
1477 DEFAULT(csiescseq
.arg
[1], term
.row
);
1478 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1482 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1483 tcursor(CURSOR_SAVE
);
1485 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1486 tcursor(CURSOR_LOAD
);
1495 for(i
= 0; i
< csiescseq
.len
; i
++) {
1496 uint c
= csiescseq
.buf
[i
] & 0xff;
1497 if(isprint(c
)) putchar(c
);
1498 else if(c
== '\n') printf("(\\n)");
1499 else if(c
== '\r') printf("(\\r)");
1500 else if(c
== 0x1b) printf("(\\e)");
1501 else printf("(%02x)", c
);
1508 memset(&csiescseq
, 0, sizeof(csiescseq
));
1516 * TODO: make this being useful in case of color palette change.
1522 switch(strescseq
.type
) {
1523 case ']': /* OSC -- Operating System Command */
1529 * TODO: Handle special chars in string, like umlauts.
1532 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1536 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1538 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1541 fprintf(stderr
, "erresc: unknown str ");
1546 case 'k': /* old title set compatibility */
1547 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1549 case 'P': /* DSC -- Device Control String */
1550 case '_': /* APC -- Application Program Command */
1551 case '^': /* PM -- Privacy Message */
1553 fprintf(stderr
, "erresc: unknown str ");
1563 * TODO: Implement parsing like for CSI when required.
1564 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1572 printf("ESC%c", strescseq
.type
);
1573 for(i
= 0; i
< strescseq
.len
; i
++) {
1574 uint c
= strescseq
.buf
[i
] & 0xff;
1575 if(isprint(c
)) putchar(c
);
1576 else if(c
== '\n') printf("(\\n)");
1577 else if(c
== '\r') printf("(\\r)");
1578 else if(c
== 0x1b) printf("(\\e)");
1579 else printf("(%02x)", c
);
1586 memset(&strescseq
, 0, sizeof(strescseq
));
1590 tputtab(bool forward
) {
1591 unsigned x
= term
.c
.x
;
1596 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1601 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1604 tmoveto(x
, term
.c
.y
);
1614 if(term
.esc
& ESC_START
) {
1615 if(term
.esc
& ESC_CSI
) {
1616 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1617 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1619 csiparse(), csihandle();
1621 } else if(term
.esc
& ESC_STR
) {
1624 term
.esc
= ESC_START
| ESC_STR_END
;
1626 case '\a': /* backwards compatibility to xterm */
1631 strescseq
.buf
[strescseq
.len
++] = ascii
;
1632 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1637 } else if(term
.esc
& ESC_STR_END
) {
1641 } else if(term
.esc
& ESC_ALTCHARSET
) {
1643 case '0': /* Line drawing crap */
1644 term
.c
.attr
.mode
|= ATTR_GFX
;
1646 case 'B': /* Back to regular text */
1647 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1650 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1656 term
.esc
|= ESC_CSI
;
1658 case 'P': /* DCS -- Device Control String */
1659 case '_': /* APC -- Application Program Command */
1660 case '^': /* PM -- Privacy Message */
1661 case ']': /* OSC -- Operating System Command */
1662 case 'k': /* old title set compatibility */
1664 strescseq
.type
= ascii
;
1665 term
.esc
|= ESC_STR
;
1668 term
.esc
|= ESC_ALTCHARSET
;
1670 case 'D': /* IND -- Linefeed */
1671 if(term
.c
.y
== term
.bot
)
1672 tscrollup(term
.top
, 1);
1674 tmoveto(term
.c
.x
, term
.c
.y
+1);
1677 case 'E': /* NEL -- Next line */
1678 tnewline(1); /* always go to first col */
1681 case 'H': /* HTS -- Horizontal tab stop */
1682 term
.tabs
[term
.c
.x
] = 1;
1685 case 'M': /* RI -- Reverse index */
1686 if(term
.c
.y
== term
.top
)
1687 tscrolldown(term
.top
, 1);
1689 tmoveto(term
.c
.x
, term
.c
.y
-1);
1692 case 'c': /* RIS -- Reset to inital state */
1697 case '=': /* DECPAM -- Application keypad */
1698 term
.mode
|= MODE_APPKEYPAD
;
1701 case '>': /* DECPNM -- Normal keypad */
1702 term
.mode
&= ~MODE_APPKEYPAD
;
1705 case '7': /* DECSC -- Save Cursor */
1706 tcursor(CURSOR_SAVE
);
1709 case '8': /* DECRC -- Restore Cursor */
1710 tcursor(CURSOR_LOAD
);
1713 case '\\': /* ST -- Stop */
1717 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1718 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1723 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1726 case '\0': /* padding character, do nothing */
1732 tmoveto(term
.c
.x
-1, term
.c
.y
);
1735 tmoveto(0, term
.c
.y
);
1740 /* go to first col if the mode is set */
1741 tnewline(IS_SET(MODE_CRLF
));
1744 if(!(xw
.state
& WIN_FOCUSED
))
1749 term
.esc
= ESC_START
;
1752 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1753 tnewline(1); /* always go to first col */
1755 if(term
.c
.x
+1 < term
.col
)
1756 tmoveto(term
.c
.x
+1, term
.c
.y
);
1758 term
.c
.state
|= CURSOR_WRAPNEXT
;
1764 tresize(int col
, int row
) {
1766 int minrow
= MIN(row
, term
.row
);
1767 int mincol
= MIN(col
, term
.col
);
1768 int slide
= term
.c
.y
- row
+ 1;
1770 if(col
< 1 || row
< 1)
1773 /* free unneeded rows */
1776 /* slide screen to keep cursor where we expect it -
1777 * tscrollup would work here, but we can optimize to
1778 * memmove because we're freeing the earlier lines */
1779 for(/* i = 0 */; i
< slide
; i
++) {
1783 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1784 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1786 for(i
+= row
; i
< term
.row
; i
++) {
1791 /* resize to new height */
1792 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
1793 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
1794 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1795 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1797 /* resize each row to new width, zero-pad if needed */
1798 for(i
= 0; i
< minrow
; i
++) {
1800 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
1801 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
1802 for(x
= mincol
; x
< col
; x
++) {
1803 term
.line
[i
][x
].state
= 0;
1804 term
.alt
[i
][x
].state
= 0;
1808 /* allocate any new rows */
1809 for(/* i == minrow */; i
< row
; i
++) {
1811 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1812 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1814 if(col
> term
.col
) {
1815 bool *bp
= term
.tabs
+ term
.col
;
1817 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1818 while(--bp
> term
.tabs
&& !*bp
)
1820 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1823 /* update terminal size */
1824 term
.col
= col
, term
.row
= row
;
1825 /* make use of the LIMIT in tmoveto */
1826 tmoveto(term
.c
.x
, term
.c
.y
);
1827 /* reset scrolling region */
1828 tsetscroll(0, row
-1);
1834 xresize(int col
, int row
) {
1835 xw
.w
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1836 xw
.h
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1843 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
1845 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1846 for(i
= 0; i
< LEN(colorname
); i
++) {
1849 if(!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1851 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1853 dc
.col
[i
] = color
.pixel
;
1856 /* load colors [16-255] ; same colors as xterm */
1857 for(i
= 16, r
= 0; r
< 6; r
++)
1858 for(g
= 0; g
< 6; g
++)
1859 for(b
= 0; b
< 6; b
++) {
1860 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1861 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1862 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1863 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1865 fprintf(stderr
, "Could not allocate color %d\n", i
);
1867 dc
.col
[i
] = color
.pixel
;
1871 for(r
= 0; r
< 24; r
++, i
++) {
1872 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1873 if(!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1875 fprintf(stderr
, "Could not allocate color %d\n", i
);
1877 dc
.col
[i
] = color
.pixel
;
1882 xclear(int x1
, int y1
, int x2
, int y2
) {
1883 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
]);
1884 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1885 BORDER
+ x1
* xw
.cw
, BORDER
+ y1
* xw
.ch
,
1886 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1891 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1892 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1893 XSizeHints
*sizeh
= NULL
;
1895 sizeh
= XAllocSizeHints();
1896 if(xw
.isfixed
== False
) {
1897 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
1898 sizeh
->height
= xw
.h
;
1899 sizeh
->width
= xw
.w
;
1900 sizeh
->height_inc
= xw
.ch
;
1901 sizeh
->width_inc
= xw
.cw
;
1902 sizeh
->base_height
= 2*BORDER
;
1903 sizeh
->base_width
= 2*BORDER
;
1905 sizeh
->flags
= PMaxSize
| PMinSize
;
1906 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
1907 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
1910 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
1915 xinitfont(char *fontstr
) {
1917 char *def
, **missing
;
1921 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1924 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1925 XFreeStringList(missing
);
1931 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
) {
1932 XFontStruct
**xfonts
;
1936 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1937 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1938 for(i
= 0; i
< n
; i
++) {
1939 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1940 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1941 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1942 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1948 initfonts(char *fontstr
, char *bfontstr
, char *ifontstr
, char *ibfontstr
) {
1949 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
)
1950 die("Can't load font %s\n", fontstr
);
1951 if((dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1952 die("Can't load bfont %s\n", bfontstr
);
1953 if((dc
.ifont
.set
= xinitfont(ifontstr
)) == NULL
)
1954 die("Can't load ifont %s\n", ifontstr
);
1955 if((dc
.ibfont
.set
= xinitfont(ibfontstr
)) == NULL
)
1956 die("Can't load ibfont %s\n", ibfontstr
);
1958 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1959 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1960 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1961 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1962 xgetfontinfo(dc
.ifont
.set
, &dc
.ifont
.ascent
, &dc
.ifont
.descent
,
1963 &dc
.ifont
.lbearing
, &dc
.ifont
.rbearing
);
1964 xgetfontinfo(dc
.ibfont
.set
, &dc
.ibfont
.ascent
, &dc
.ibfont
.descent
,
1965 &dc
.ibfont
.lbearing
, &dc
.ibfont
.rbearing
);
1970 XSetWindowAttributes attrs
;
1973 int sw
, sh
, major
, minor
;
1975 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1976 die("Can't open display\n");
1977 xw
.scr
= XDefaultScreen(xw
.dpy
);
1980 initfonts(FONT
, BOLDFONT
, ITALICFONT
, ITALICBOLDFONT
);
1982 /* XXX: Assuming same size for bold font */
1983 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1984 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1987 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1990 /* adjust fixed window geometry */
1992 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
1993 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
1995 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
1997 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2002 /* window - default size */
2003 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
2004 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
2009 attrs
.background_pixel
= dc
.col
[DefaultBG
];
2010 attrs
.border_pixel
= dc
.col
[DefaultBG
];
2011 attrs
.bit_gravity
= NorthWestGravity
;
2012 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2013 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2014 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2015 attrs
.colormap
= xw
.cmap
;
2017 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2018 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2019 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2020 XDefaultVisual(xw
.dpy
, xw
.scr
),
2021 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2024 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2025 die("Xdbe extension is not present\n");
2026 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2029 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2030 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2031 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2032 XNFocusWindow
, xw
.win
, NULL
);
2034 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2036 /* white cursor, black outline */
2037 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2038 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2039 XRecolorCursor(xw
.dpy
, cursor
,
2040 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2041 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2043 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2046 XMapWindow(xw
.dpy
, xw
.win
);
2052 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2053 int fg
= base
.fg
, bg
= base
.bg
, temp
;
2054 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
2055 XFontSet fontset
= dc
.font
.set
;
2058 /* only switch default fg/bg if term is in RV mode */
2059 if(IS_SET(MODE_REVERSE
)) {
2066 if(base
.mode
& ATTR_REVERSE
)
2067 temp
= fg
, fg
= bg
, bg
= temp
;
2069 if(base
.mode
& ATTR_BOLD
) {
2071 fontset
= dc
.bfont
.set
;
2074 if(base
.mode
& ATTR_ITALIC
)
2075 fontset
= dc
.ifont
.set
;
2076 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2077 fontset
= dc
.ibfont
.set
;
2079 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
2080 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
2082 if(base
.mode
& ATTR_GFX
) {
2083 for(i
= 0; i
< bytelen
; i
++) {
2084 char c
= gfx
[(uint
)s
[i
] % 256];
2087 else if(s
[i
] > 0x5f)
2092 XmbDrawImageString(xw
.dpy
, xw
.buf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
2094 if(base
.mode
& ATTR_UNDERLINE
)
2095 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
2098 /* copy buffer pixmap to screen pixmap */
2101 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2102 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2107 static int oldx
= 0;
2108 static int oldy
= 0;
2110 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2112 LIMIT(oldx
, 0, term
.col
-1);
2113 LIMIT(oldy
, 0, term
.row
-1);
2115 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2116 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2118 /* remove the old cursor */
2119 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2120 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2121 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2123 xclear(oldx
, oldy
, oldx
, oldy
);
2127 /* draw the new one */
2128 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2129 if(!(xw
.state
& WIN_FOCUSED
))
2132 if(IS_SET(MODE_REVERSE
))
2133 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2136 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2137 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2145 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2150 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2153 nanosleep(&tv
, NULL
);
2158 drawregion(0, 0, term
.col
, term
.row
);
2160 gettimeofday(&xw
.lastdraw
, NULL
);
2164 drawregion(int x1
, int y1
, int x2
, int y2
) {
2165 int ic
, ib
, x
, y
, ox
, sl
;
2167 char buf
[DRAW_BUF_SIZ
];
2168 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2170 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2172 if(!(xw
.state
& WIN_VISIBLE
))
2175 for(y
= y1
; y
< y2
; y
++) {
2178 xclear(0, y
, term
.col
, y
);
2180 base
= term
.line
[y
][0];
2182 for(x
= x1
; x
< x2
; x
++) {
2183 new = term
.line
[y
][x
];
2184 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2185 new.mode
^= ATTR_REVERSE
;
2186 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2187 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2188 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2191 if(new.state
& GLYPH_SET
) {
2196 sl
= utf8size(new.c
);
2197 memcpy(buf
+ib
, new.c
, sl
);
2203 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2209 expose(XEvent
*ev
) {
2210 XExposeEvent
*e
= &ev
->xexpose
;
2211 if(xw
.state
& WIN_REDRAW
) {
2213 xw
.state
&= ~WIN_REDRAW
;
2219 visibility(XEvent
*ev
) {
2220 XVisibilityEvent
*e
= &ev
->xvisibility
;
2221 if(e
->state
== VisibilityFullyObscured
)
2222 xw
.state
&= ~WIN_VISIBLE
;
2223 else if(!(xw
.state
& WIN_VISIBLE
))
2224 /* need a full redraw for next Expose, not just a buf copy */
2225 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2230 xw
.state
&= ~WIN_VISIBLE
;
2234 xseturgency(int add
) {
2235 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2236 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2237 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2243 if(ev
->type
== FocusIn
) {
2244 xw
.state
|= WIN_FOCUSED
;
2247 xw
.state
&= ~WIN_FOCUSED
;
2252 kmap(KeySym k
, uint state
) {
2255 for(i
= 0; i
< LEN(key
); i
++) {
2256 uint mask
= key
[i
].mask
;
2257 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2258 return (char*)key
[i
].s
;
2264 kpress(XEvent
*ev
) {
2265 XKeyEvent
*e
= &ev
->xkey
;
2274 meta
= e
->state
& Mod1Mask
;
2275 shift
= e
->state
& ShiftMask
;
2276 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2278 /* 1. custom keys from config.h */
2279 if((customkey
= kmap(ksym
, e
->state
)))
2280 ttywrite(customkey
, strlen(customkey
));
2281 /* 2. hardcoded (overrides X lookup) */
2288 /* XXX: shift up/down doesn't work */
2289 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2297 if(IS_SET(MODE_CRLF
))
2298 ttywrite("\r\n", 2);
2305 if(meta
&& len
== 1)
2306 ttywrite("\033", 1);
2314 cmessage(XEvent
*e
) {
2316 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2317 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2318 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2319 xw
.state
|= WIN_FOCUSED
;
2321 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2322 xw
.state
&= ~WIN_FOCUSED
;
2332 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2335 xw
.w
= e
->xconfigure
.width
;
2336 xw
.h
= e
->xconfigure
.height
;
2337 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2338 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2339 if(col
== term
.col
&& row
== term
.row
)
2341 if(tresize(col
, row
))
2344 ttyresize(col
, row
);
2348 last_draw_too_old(void) {
2350 gettimeofday(&now
, NULL
);
2351 return TIMEDIFF(now
, xw
.lastdraw
) >= DRAW_TIMEOUT
/1000;
2358 int xfd
= XConnectionNumber(xw
.dpy
);
2359 struct timeval timeout
= {0};
2360 bool stuff_to_print
= 0;
2364 FD_SET(cmdfd
, &rfd
);
2367 timeout
.tv_usec
= SELECT_TIMEOUT
;
2368 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
2371 die("select failed: %s\n", SERRNO
);
2373 if(FD_ISSET(cmdfd
, &rfd
)) {
2378 if(stuff_to_print
&& last_draw_too_old()) {
2383 while(XPending(xw
.dpy
)) {
2384 XNextEvent(xw
.dpy
, &ev
);
2385 if(XFilterEvent(&ev
, xw
.win
))
2387 if(handler
[ev
.type
])
2388 (handler
[ev
.type
])(&ev
);
2394 main(int argc
, char *argv
[]) {
2395 int i
, bitm
, xr
, yr
;
2396 unsigned int wr
, hr
;
2398 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2401 for(i
= 1; i
< argc
; i
++) {
2402 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2404 if(++i
< argc
) opt_title
= argv
[i
];
2407 if(++i
< argc
) opt_class
= argv
[i
];
2410 if(++i
< argc
) opt_embed
= argv
[i
];
2413 if(++i
< argc
) opt_io
= argv
[i
];
2416 /* eat every remaining arguments */
2417 if(++i
< argc
) opt_cmd
= &argv
[i
];
2423 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2428 if(bitm
& WidthValue
)
2430 if(bitm
& HeightValue
)
2432 if(bitm
& XNegative
&& xw
.fx
== 0)
2434 if(bitm
& XNegative
&& xw
.fy
== 0)
2437 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2447 setlocale(LC_CTYPE
, "");