1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
20 #include <X11/Xatom.h>
21 #include <X11/keysym.h>
22 #include <X11/Xutil.h>
26 #elif defined(__OpenBSD__) || defined(__NetBSD__)
28 #elif defined(__FreeBSD__) || defined(__DragonFly__)
33 "st-" VERSION ", (c) 2010 st engineers\n" \
34 "usage: st [-t title] [-e cmd] [-v]\n"
37 #define ESC_TITLE_SIZ 256
38 #define ESC_BUF_SIZ 256
39 #define ESC_ARG_SIZ 16
40 #define DRAW_BUF_SIZ 1024
42 #define SERRNO strerror(errno)
43 #define MIN(a, b) ((a) < (b) ? (a) : (b))
44 #define MAX(a, b) ((a) < (b) ? (b) : (a))
45 #define LEN(a) (sizeof(a) / sizeof(a[0]))
46 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
47 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
48 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
49 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
50 #define IS_SET(flag) (term.mode & (flag))
52 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
53 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
54 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
55 CURSOR_SAVE
, CURSOR_LOAD
};
56 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
57 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
58 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
60 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
61 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
62 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
65 char c
; /* character code */
66 char mode
; /* attribute flags */
67 int fg
; /* foreground */
68 int bg
; /* background */
69 char state
; /* state flags */
75 Glyph attr
; /* current char attributes */
81 /* CSI Escape sequence structs */
82 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
84 char buf
[ESC_BUF_SIZ
]; /* raw string */
85 int len
; /* raw string length */
88 int narg
; /* nb of args */
92 /* Internal representation of the screen */
96 Line
* line
; /* screen */
97 Line
* alt
; /* alternate screen */
98 TCursor c
; /* cursor */
99 int top
; /* top scroll limit */
100 int bot
; /* bottom scroll limit */
101 int mode
; /* terminal mode flags */
102 int esc
; /* escape state flags */
103 char title
[ESC_TITLE_SIZ
];
107 /* Purely graphic info */
116 int w
; /* window width */
117 int h
; /* window height */
118 int bufw
; /* pixmap width */
119 int bufh
; /* pixmap height */
120 int ch
; /* char height */
121 int cw
; /* char width */
122 char state
; /* focus, redraw, visible */
130 /* Drawing Context */
132 unsigned long col
[256];
138 /* TODO: use better name for vars... */
143 struct {int x
, y
;} b
, e
;
149 static void die(const char *errstr
, ...);
150 static void draw(int);
151 static void execsh(void);
152 static void sigchld(int);
153 static void run(void);
155 static void csidump(void);
156 static void csihandle(void);
157 static void csiparse(void);
158 static void csireset(void);
160 static void tclearregion(int, int, int, int);
161 static void tcursor(int);
162 static void tdeletechar(int);
163 static void tdeleteline(int);
164 static void tinsertblank(int);
165 static void tinsertblankline(int);
166 static void tmoveto(int, int);
167 static void tnew(int, int);
168 static void tnewline(int);
169 static void tputtab(void);
170 static void tputc(char);
171 static void tputs(char*, int);
172 static void treset(void);
173 static void tresize(int, int);
174 static void tscrollup(int, int);
175 static void tscrolldown(int, int);
176 static void tsetattr(int*, int);
177 static void tsetchar(char);
178 static void tsetscroll(int, int);
179 static void tswapscreen(void);
181 static void ttynew(void);
182 static void ttyread(void);
183 static void ttyresize(int, int);
184 static void ttywrite(const char *, size_t);
186 static void xdraws(char *, Glyph
, int, int, int);
187 static void xhints(void);
188 static void xclear(int, int, int, int);
189 static void xdrawcursor(void);
190 static void xinit(void);
191 static void xloadcols(void);
192 static void xseturgency(int);
193 static void xresize(int, int);
195 static void expose(XEvent
*);
196 static void visibility(XEvent
*);
197 static void unmap(XEvent
*);
198 static char* kmap(KeySym
);
199 static void kpress(XEvent
*);
200 static void resize(XEvent
*);
201 static void focus(XEvent
*);
202 static void brelease(XEvent
*);
203 static void bpress(XEvent
*);
204 static void bmotion(XEvent
*);
205 static void selection_notify(XEvent
*);
206 static void selection_request(XEvent
*);
208 static void (*handler
[LASTEvent
])(XEvent
*) = {
210 [ConfigureNotify
] = resize
,
211 [VisibilityNotify
] = visibility
,
212 [UnmapNotify
] = unmap
,
216 [MotionNotify
] = bmotion
,
217 [ButtonPress
] = bpress
,
218 [ButtonRelease
] = brelease
,
219 [SelectionNotify
] = selection_notify
,
220 [SelectionRequest
] = selection_request
,
227 static CSIEscape escseq
;
230 static Selection sel
;
231 static char *opt_cmd
= NULL
;
232 static char *opt_title
= NULL
;
241 static inline int selected(int x
, int y
) {
242 if(sel
.ey
== y
&& sel
.by
== y
) {
243 int bx
= MIN(sel
.bx
, sel
.ex
);
244 int ex
= MAX(sel
.bx
, sel
.ex
);
245 return BETWEEN(x
, bx
, ex
);
247 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
248 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
251 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
253 *b
= e
->xbutton
.button
;
255 *x
= e
->xbutton
.x
/xw
.cw
;
256 *y
= e
->xbutton
.y
/xw
.ch
;
257 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
258 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
259 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
260 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
263 static void bpress(XEvent
*e
) {
265 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
266 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
269 static char *getseltext() {
274 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1);
275 ptr
= str
= malloc(sz
);
276 for(y
= 0; y
< term
.row
; y
++) {
277 for(x
= 0; x
< term
.col
; x
++)
278 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
)))
279 *ptr
= term
.line
[y
][x
].c
, ptr
++;
287 static void selection_notify(XEvent
*e
) {
288 unsigned long nitems
;
289 unsigned long length
;
294 res
= XGetWindowProperty(xw
.dis
, xw
.win
, XA_PRIMARY
, 0, 0, False
,
295 AnyPropertyType
, &type
, &format
, &nitems
, &length
, &data
);
300 fprintf(stderr
, "Invalid paste, XGetWindowProperty0");
304 res
= XGetWindowProperty(xw
.dis
, xw
.win
, XA_PRIMARY
, 0, length
, False
,
305 AnyPropertyType
, &type
, &format
, &nitems
, &length
, &data
);
310 fprintf(stderr
, "Invalid paste, XGetWindowProperty0");
315 ttywrite((const char *) data
, nitems
* format
/ 8);
320 static void selpaste() {
321 XConvertSelection(xw
.dis
, XA_PRIMARY
, XA_STRING
, XA_PRIMARY
, xw
.win
, CurrentTime
);
324 static void selection_request(XEvent
*e
)
326 XSelectionRequestEvent
*xsre
;
331 xsre
= (XSelectionRequestEvent
*) e
;
332 xev
.type
= SelectionNotify
;
333 xev
.requestor
= xsre
->requestor
;
334 xev
.selection
= xsre
->selection
;
335 xev
.target
= xsre
->target
;
336 xev
.time
= xsre
->time
;
340 xa_targets
= XInternAtom(xw
.dis
, "TARGETS", 0);
341 if(xsre
->target
== xa_targets
) {
342 /* respond with the supported type */
343 Atom string
= XA_STRING
;
344 res
= XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
, XA_ATOM
, 32,
345 PropModeReplace
, (unsigned char *) &string
, 1);
352 fprintf(stderr
, "Error in selection_request, TARGETS");
355 xev
.property
= xsre
->property
;
357 } else if(xsre
->target
== XA_STRING
) {
358 res
= XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
359 xsre
->target
, 8, PropModeReplace
, (unsigned char *) sel
.clip
,
367 fprintf(stderr
, "Error in selection_request, XA_STRING");
370 xev
.property
= xsre
->property
;
374 /* all done, send a notification to the listener */
375 res
= XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
);
380 fprintf(stderr
, "Error in selection_requested, XSendEvent");
384 static void selcopy(char *str
) {
385 /* register the selection for both the clipboard and the primary */
392 res
= XSetSelectionOwner(xw
.dis
, XA_PRIMARY
, xw
.win
, CurrentTime
);
396 fprintf(stderr
, "Invalid copy, XSetSelectionOwner");
400 clipboard
= XInternAtom(xw
.dis
, "CLIPBOARD", 0);
401 res
= XSetSelectionOwner(xw
.dis
, clipboard
, xw
.win
, CurrentTime
);
405 fprintf(stderr
, "Invalid copy, XSetSelectionOwner");
412 /* TODO: doubleclick to select word */
413 static void brelease(XEvent
*e
) {
416 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
417 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
423 selcopy(getseltext());
428 static void bmotion(XEvent
*e
) {
430 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
441 for(row
= 0; row
< term
.row
; row
++) {
442 for(col
= 0; col
< term
.col
; col
++) {
443 if(col
== term
.c
.x
&& row
== term
.c
.y
)
446 c
= term
.line
[row
][col
];
447 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
456 die(const char *errstr
, ...) {
459 va_start(ap
, errstr
);
460 vfprintf(stderr
, errstr
, ap
);
467 char *args
[] = {getenv("SHELL"), "-i", NULL
};
469 args
[0] = opt_cmd
, args
[1] = NULL
;
471 DEFAULT(args
[0], SHELL
);
472 putenv("TERM="TNAME
);
473 execvp(args
[0], args
);
479 if(waitpid(pid
, &stat
, 0) < 0)
480 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
482 exit(WEXITSTATUS(stat
));
491 /* seems to work fine on linux, openbsd and freebsd */
492 struct winsize w
= {term
.row
, term
.col
, 0, 0};
493 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
494 die("openpty failed: %s\n", SERRNO
);
496 switch(pid
= fork()) {
498 die("fork failed\n");
501 setsid(); /* create a new process group */
502 dup2(s
, STDIN_FILENO
);
503 dup2(s
, STDOUT_FILENO
);
504 dup2(s
, STDERR_FILENO
);
505 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
506 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
514 signal(SIGCHLD
, sigchld
);
521 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
523 fprintf(stderr
, "\n");
531 if((ret
= read(cmdfd
, buf
, LEN(buf
))) < 0)
532 die("Couldn't read from shell: %s\n", SERRNO
);
538 ttywrite(const char *s
, size_t n
) {
539 if(write(cmdfd
, s
, n
) == -1)
540 die("write error on tty: %s\n", SERRNO
);
544 ttyresize(int x
, int y
) {
549 w
.ws_xpixel
= w
.ws_ypixel
= 0;
550 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
551 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
558 if(mode
== CURSOR_SAVE
)
560 else if(mode
== CURSOR_LOAD
)
561 term
.c
= c
, tmoveto(c
.x
, c
.y
);
570 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
572 term
.top
= 0, term
.bot
= term
.row
- 1;
573 term
.mode
= MODE_WRAP
;
574 tclearregion(0, 0, term
.col
-1, term
.row
-1);
578 tnew(int col
, int row
) {
579 /* set screen size */
580 term
.row
= row
, term
.col
= col
;
581 term
.line
= malloc(term
.row
* sizeof(Line
));
582 term
.alt
= malloc(term
.row
* sizeof(Line
));
583 for(row
= 0 ; row
< term
.row
; row
++) {
584 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
585 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
593 Line
* tmp
= term
.line
;
594 term
.line
= term
.alt
;
596 term
.mode
^= MODE_ALTSCREEN
;
600 tscrolldown(int orig
, int n
) {
604 LIMIT(n
, 0, term
.bot
-orig
+1);
606 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
608 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
610 term
.line
[i
] = term
.line
[i
-n
];
611 term
.line
[i
-n
] = temp
;
616 tscrollup(int orig
, int n
) {
619 LIMIT(n
, 0, term
.bot
-orig
+1);
621 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
623 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
625 term
.line
[i
] = term
.line
[i
+n
];
626 term
.line
[i
+n
] = temp
;
631 tnewline(int first_col
) {
634 tscrollup(term
.top
, 1);
637 tmoveto(first_col
? 0 : term
.c
.x
, y
);
643 char *p
= escseq
.buf
;
647 escseq
.priv
= 1, p
++;
649 while(p
< escseq
.buf
+escseq
.len
) {
651 escseq
.arg
[escseq
.narg
] *= 10;
652 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
654 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
665 tmoveto(int x
, int y
) {
666 LIMIT(x
, 0, term
.col
-1);
667 LIMIT(y
, 0, term
.row
-1);
668 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
675 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
676 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
677 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
681 tclearregion(int x1
, int y1
, int x2
, int y2
) {
685 temp
= x1
, x1
= x2
, x2
= temp
;
687 temp
= y1
, y1
= y2
, y2
= temp
;
689 LIMIT(x1
, 0, term
.col
-1);
690 LIMIT(x2
, 0, term
.col
-1);
691 LIMIT(y1
, 0, term
.row
-1);
692 LIMIT(y2
, 0, term
.row
-1);
694 for(y
= y1
; y
<= y2
; y
++)
695 for(x
= x1
; x
<= x2
; x
++)
696 term
.line
[y
][x
].state
= 0;
701 int src
= term
.c
.x
+ n
;
703 int size
= term
.col
- src
;
705 if(src
>= term
.col
) {
706 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
709 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
710 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
714 tinsertblank(int n
) {
717 int size
= term
.col
- dst
;
719 if(dst
>= term
.col
) {
720 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
723 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
724 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
728 tinsertblankline(int n
) {
729 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
732 tscrolldown(term
.c
.y
, n
);
737 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
740 tscrollup(term
.c
.y
, n
);
744 tsetattr(int *attr
, int l
) {
747 for(i
= 0; i
< l
; i
++) {
750 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
751 term
.c
.attr
.fg
= DefaultFG
;
752 term
.c
.attr
.bg
= DefaultBG
;
755 term
.c
.attr
.mode
|= ATTR_BOLD
;
758 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
761 term
.c
.attr
.mode
|= ATTR_REVERSE
;
764 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
767 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
770 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
773 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
775 if (BETWEEN(attr
[i
], 0, 255))
776 term
.c
.attr
.fg
= attr
[i
];
778 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
781 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
784 term
.c
.attr
.fg
= DefaultFG
;
787 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
789 if (BETWEEN(attr
[i
], 0, 255))
790 term
.c
.attr
.bg
= attr
[i
];
792 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
795 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
798 term
.c
.attr
.bg
= DefaultBG
;
801 if(BETWEEN(attr
[i
], 30, 37))
802 term
.c
.attr
.fg
= attr
[i
] - 30;
803 else if(BETWEEN(attr
[i
], 40, 47))
804 term
.c
.attr
.bg
= attr
[i
] - 40;
805 else if(BETWEEN(attr
[i
], 90, 97))
806 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
807 else if(BETWEEN(attr
[i
], 100, 107))
808 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
810 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
818 tsetscroll(int t
, int b
) {
821 LIMIT(t
, 0, term
.row
-1);
822 LIMIT(b
, 0, term
.row
-1);
834 switch(escseq
.mode
) {
837 printf("erresc: unknown csi ");
841 case '@': /* ICH -- Insert <n> blank char */
842 DEFAULT(escseq
.arg
[0], 1);
843 tinsertblank(escseq
.arg
[0]);
845 case 'A': /* CUU -- Cursor <n> Up */
847 DEFAULT(escseq
.arg
[0], 1);
848 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
850 case 'B': /* CUD -- Cursor <n> Down */
851 DEFAULT(escseq
.arg
[0], 1);
852 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
854 case 'C': /* CUF -- Cursor <n> Forward */
856 DEFAULT(escseq
.arg
[0], 1);
857 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
859 case 'D': /* CUB -- Cursor <n> Backward */
860 DEFAULT(escseq
.arg
[0], 1);
861 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
863 case 'E': /* CNL -- Cursor <n> Down and first col */
864 DEFAULT(escseq
.arg
[0], 1);
865 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
867 case 'F': /* CPL -- Cursor <n> Up and first col */
868 DEFAULT(escseq
.arg
[0], 1);
869 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
871 case 'G': /* CHA -- Move to <col> */
872 case '`': /* XXX: HPA -- same? */
873 DEFAULT(escseq
.arg
[0], 1);
874 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
876 case 'H': /* CUP -- Move to <row> <col> */
877 case 'f': /* XXX: HVP -- same? */
878 DEFAULT(escseq
.arg
[0], 1);
879 DEFAULT(escseq
.arg
[1], 1);
880 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
882 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
883 case 'J': /* ED -- Clear screen */
884 switch(escseq
.arg
[0]) {
886 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
889 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
892 tclearregion(0, 0, term
.col
-1, term
.row
-1);
898 case 'K': /* EL -- Clear line */
899 switch(escseq
.arg
[0]) {
901 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
904 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
907 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
911 case 'S': /* SU -- Scroll <n> line up */
912 DEFAULT(escseq
.arg
[0], 1);
913 tscrollup(term
.top
, escseq
.arg
[0]);
915 case 'T': /* SD -- Scroll <n> line down */
916 DEFAULT(escseq
.arg
[0], 1);
917 tscrolldown(term
.top
, escseq
.arg
[0]);
919 case 'L': /* IL -- Insert <n> blank lines */
920 DEFAULT(escseq
.arg
[0], 1);
921 tinsertblankline(escseq
.arg
[0]);
923 case 'l': /* RM -- Reset Mode */
925 switch(escseq
.arg
[0]) {
927 term
.mode
&= ~MODE_APPKEYPAD
;
929 case 5: /* TODO: DECSCNM -- Remove reverse video */
932 term
.mode
&= ~MODE_WRAP
;
934 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
937 term
.mode
&= ~MODE_CRLF
;
940 term
.c
.state
|= CURSOR_HIDE
;
942 case 1049: /* = 1047 and 1048 */
944 if(IS_SET(MODE_ALTSCREEN
)) {
945 tclearregion(0, 0, term
.col
-1, term
.row
-1);
948 if(escseq
.arg
[0] == 1047)
951 tcursor(CURSOR_LOAD
);
957 switch(escseq
.arg
[0]) {
959 term
.mode
&= ~MODE_INSERT
;
966 case 'M': /* DL -- Delete <n> lines */
967 DEFAULT(escseq
.arg
[0], 1);
968 tdeleteline(escseq
.arg
[0]);
970 case 'X': /* ECH -- Erase <n> char */
971 DEFAULT(escseq
.arg
[0], 1);
972 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
974 case 'P': /* DCH -- Delete <n> char */
975 DEFAULT(escseq
.arg
[0], 1);
976 tdeletechar(escseq
.arg
[0]);
978 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
979 case 'd': /* VPA -- Move to <row> */
980 DEFAULT(escseq
.arg
[0], 1);
981 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
983 case 'h': /* SM -- Set terminal mode */
985 switch(escseq
.arg
[0]) {
987 term
.mode
|= MODE_APPKEYPAD
;
989 case 5: /* DECSCNM -- Reverve video */
990 /* TODO: set REVERSE on the whole screen (f) */
993 term
.mode
|= MODE_WRAP
;
996 term
.mode
|= MODE_CRLF
;
998 case 12: /* att610 -- Start blinking cursor (IGNORED) */
999 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1000 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1003 term
.c
.state
&= ~CURSOR_HIDE
;
1005 case 1049: /* = 1047 and 1048 */
1007 if(IS_SET(MODE_ALTSCREEN
))
1008 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1011 if(escseq
.arg
[0] == 1047)
1014 tcursor(CURSOR_SAVE
);
1016 default: goto unknown
;
1019 switch(escseq
.arg
[0]) {
1021 term
.mode
|= MODE_INSERT
;
1023 default: goto unknown
;
1027 case 'm': /* SGR -- Terminal attribute (color) */
1028 tsetattr(escseq
.arg
, escseq
.narg
);
1030 case 'r': /* DECSTBM -- Set Scrolling Region */
1034 DEFAULT(escseq
.arg
[0], 1);
1035 DEFAULT(escseq
.arg
[1], term
.row
);
1036 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1040 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1041 tcursor(CURSOR_SAVE
);
1043 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1044 tcursor(CURSOR_LOAD
);
1052 printf("ESC [ %s", escseq
.priv
? "? " : "");
1054 for(i
= 0; i
< escseq
.narg
; i
++)
1055 printf("%d ", escseq
.arg
[i
]);
1057 putchar(escseq
.mode
);
1063 memset(&escseq
, 0, sizeof(escseq
));
1068 int space
= TAB
- term
.c
.x
% TAB
;
1069 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1074 if(term
.esc
& ESC_START
) {
1075 if(term
.esc
& ESC_CSI
) {
1076 escseq
.buf
[escseq
.len
++] = c
;
1077 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1079 csiparse(), csihandle();
1081 /* TODO: handle other OSC */
1082 } else if(term
.esc
& ESC_OSC
) {
1085 term
.esc
= ESC_START
| ESC_TITLE
;
1087 } else if(term
.esc
& ESC_TITLE
) {
1088 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1090 term
.title
[term
.titlelen
] = '\0';
1091 XStoreName(xw
.dis
, xw
.win
, term
.title
);
1093 term
.title
[term
.titlelen
++] = c
;
1095 } else if(term
.esc
& ESC_ALTCHARSET
) {
1097 case '0': /* Line drawing crap */
1098 term
.c
.attr
.mode
|= ATTR_GFX
;
1100 case 'B': /* Back to regular text */
1101 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1104 printf("esc unhandled charset: ESC ( %c\n", c
);
1110 term
.esc
|= ESC_CSI
;
1113 term
.esc
|= ESC_OSC
;
1116 term
.esc
|= ESC_ALTCHARSET
;
1118 case 'D': /* IND -- Linefeed */
1119 if(term
.c
.y
== term
.bot
)
1120 tscrollup(term
.top
, 1);
1122 tmoveto(term
.c
.x
, term
.c
.y
+1);
1125 case 'E': /* NEL -- Next line */
1126 tnewline(1); /* always go to first col */
1129 case 'M': /* RI -- Reverse index */
1130 if(term
.c
.y
== term
.top
)
1131 tscrolldown(term
.top
, 1);
1133 tmoveto(term
.c
.x
, term
.c
.y
-1);
1136 case 'c': /* RIS -- Reset to inital state */
1140 case '=': /* DECPAM -- Application keypad */
1141 term
.mode
|= MODE_APPKEYPAD
;
1144 case '>': /* DECPNM -- Normal keypad */
1145 term
.mode
&= ~MODE_APPKEYPAD
;
1148 case '7': /* DECSC -- Save Cursor */
1149 tcursor(CURSOR_SAVE
);
1152 case '8': /* DECRC -- Restore Cursor */
1153 tcursor(CURSOR_LOAD
);
1157 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
1167 tmoveto(term
.c
.x
-1, term
.c
.y
);
1170 tmoveto(0, term
.c
.y
);
1175 /* go to first col if the mode is set */
1176 tnewline(IS_SET(MODE_CRLF
));
1179 if(!(xw
.state
& WIN_FOCUSED
))
1184 term
.esc
= ESC_START
;
1187 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1188 tnewline(1); /* always go to first col */
1190 if(term
.c
.x
+1 < term
.col
)
1191 tmoveto(term
.c
.x
+1, term
.c
.y
);
1193 term
.c
.state
|= CURSOR_WRAPNEXT
;
1200 tputs(char *s
, int len
) {
1201 for(; len
> 0; len
--)
1206 tresize(int col
, int row
) {
1208 int minrow
= MIN(row
, term
.row
);
1209 int mincol
= MIN(col
, term
.col
);
1210 int slide
= term
.c
.y
- row
+ 1;
1212 if(col
< 1 || row
< 1)
1215 /* free unneeded rows */
1218 /* slide screen to keep cursor where we expect it -
1219 * tscrollup would work here, but we can optimize to
1220 * memmove because we're freeing the earlier lines */
1221 for(/* i = 0 */; i
< slide
; i
++) {
1225 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1226 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1228 for(i
+= row
; i
< term
.row
; i
++) {
1233 /* resize to new height */
1234 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1235 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1237 /* resize each row to new width, zero-pad if needed */
1238 for(i
= 0; i
< minrow
; i
++) {
1239 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1240 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1241 for(x
= mincol
; x
< col
; x
++) {
1242 term
.line
[i
][x
].state
= 0;
1243 term
.alt
[i
][x
].state
= 0;
1247 /* allocate any new rows */
1248 for(/* i == minrow */; i
< row
; i
++) {
1249 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1250 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1253 /* update terminal size */
1254 term
.col
= col
, term
.row
= row
;
1255 /* make use of the LIMIT in tmoveto */
1256 tmoveto(term
.c
.x
, term
.c
.y
);
1257 /* reset scrolling region */
1258 tsetscroll(0, row
-1);
1262 xresize(int col
, int row
) {
1263 xw
.bufw
= MAX(1, col
* xw
.cw
);
1264 xw
.bufh
= MAX(1, row
* xw
.ch
);
1265 XFreePixmap(xw
.dis
, xw
.buf
);
1266 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1273 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1275 for(i
= 0; i
< 16; i
++) {
1276 if (!XAllocNamedColor(xw
.dis
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1278 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1280 dc
.col
[i
] = color
.pixel
;
1283 /* same colors as xterm */
1284 for(r
= 0; r
< 6; r
++)
1285 for(g
= 0; g
< 6; g
++)
1286 for(b
= 0; b
< 6; b
++) {
1287 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1288 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1289 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1290 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1292 fprintf(stderr
, "Could not allocate color %d\n", i
);
1294 dc
.col
[i
] = color
.pixel
;
1298 for(r
= 0; r
< 24; r
++, i
++) {
1299 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1300 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1302 fprintf(stderr
, "Could not allocate color %d\n", i
);
1304 dc
.col
[i
] = color
.pixel
;
1309 xclear(int x1
, int y1
, int x2
, int y2
) {
1310 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1311 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1312 x1
* xw
.cw
, y1
* xw
.ch
,
1313 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1319 XClassHint
class = {TNAME
, TNAME
};
1320 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1322 .flags
= PSize
| PResizeInc
| PBaseSize
,
1325 .height_inc
= xw
.ch
,
1327 .base_height
= 2*BORDER
,
1328 .base_width
= 2*BORDER
,
1330 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1335 XSetWindowAttributes attrs
;
1337 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1338 die("Can't open display\n");
1339 xw
.scr
= XDefaultScreen(xw
.dis
);
1342 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1343 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1345 /* XXX: Assuming same size for bold font */
1346 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1347 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1350 xw
.cmap
= XDefaultColormap(xw
.dis
, xw
.scr
);
1353 /* window - default size */
1354 xw
.bufh
= 24 * xw
.ch
;
1355 xw
.bufw
= 80 * xw
.cw
;
1356 xw
.h
= xw
.bufh
+ 2*BORDER
;
1357 xw
.w
= xw
.bufw
+ 2*BORDER
;
1359 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1360 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1361 attrs
.bit_gravity
= NorthWestGravity
;
1362 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1363 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1364 | PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1365 attrs
.colormap
= xw
.cmap
;
1367 xw
.win
= XCreateWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1368 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dis
, xw
.scr
), InputOutput
,
1369 XDefaultVisual(xw
.dis
, xw
.scr
),
1370 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1373 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1377 xw
.xim
= XOpenIM(xw
.dis
, NULL
, NULL
, NULL
);
1378 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1379 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1380 XNFocusWindow
, xw
.win
, NULL
);
1382 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1384 XMapWindow(xw
.dis
, xw
.win
);
1386 XStoreName(xw
.dis
, xw
.win
, opt_title
? opt_title
: "st");
1391 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1392 unsigned long xfg
, xbg
;
1393 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1396 if(base
.mode
& ATTR_REVERSE
)
1397 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1399 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1401 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1402 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1404 if(base
.mode
& ATTR_GFX
)
1405 for(i
= 0; i
< len
; i
++) {
1406 char c
= gfx
[(unsigned int)s
[i
] % 256];
1409 else if(s
[i
] > 0x5f)
1413 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1414 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1416 if(base
.mode
& ATTR_UNDERLINE
)
1417 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1422 static int oldx
= 0;
1423 static int oldy
= 0;
1424 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1426 LIMIT(oldx
, 0, term
.col
-1);
1427 LIMIT(oldy
, 0, term
.row
-1);
1429 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1430 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1432 /* remove the old cursor */
1433 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1434 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1436 xclear(oldx
, oldy
, oldx
, oldy
);
1438 /* draw the new one */
1439 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1440 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1441 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1446 /* basic drawing routines */
1448 xdrawc(int x
, int y
, Glyph g
) {
1449 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1450 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1451 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1452 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1453 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1460 xclear(0, 0, term
.col
-1, term
.row
-1);
1461 for(y
= 0; y
< term
.row
; y
++)
1462 for(x
= 0; x
< term
.col
; x
++)
1463 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1464 xdrawc(x
, y
, term
.line
[y
][x
]);
1467 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1472 /* optimized drawing routine */
1474 draw(int redraw_all
) {
1477 char buf
[DRAW_BUF_SIZ
];
1479 if(!(xw
.state
& WIN_VISIBLE
))
1482 xclear(0, 0, term
.col
-1, term
.row
-1);
1483 for(y
= 0; y
< term
.row
; y
++) {
1484 base
= term
.line
[y
][0];
1486 for(x
= 0; x
< term
.col
; x
++) {
1487 new = term
.line
[y
][x
];
1488 if(sel
.bx
!=-1 && new.c
&& selected(x
, y
))
1489 new.mode
^= ATTR_REVERSE
;
1490 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1491 i
>= DRAW_BUF_SIZ
)) {
1492 xdraws(buf
, base
, ox
, y
, i
);
1495 if(new.state
& GLYPH_SET
) {
1504 xdraws(buf
, base
, ox
, y
, i
);
1507 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1513 expose(XEvent
*ev
) {
1514 XExposeEvent
*e
= &ev
->xexpose
;
1515 if(xw
.state
& WIN_REDRAW
) {
1517 xw
.state
&= ~WIN_REDRAW
;
1518 draw(SCREEN_REDRAW
);
1521 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1522 e
->width
, e
->height
, e
->x
, e
->y
);
1526 visibility(XEvent
*ev
) {
1527 XVisibilityEvent
*e
= &ev
->xvisibility
;
1528 if(e
->state
== VisibilityFullyObscured
)
1529 xw
.state
&= ~WIN_VISIBLE
;
1530 else if(!(xw
.state
& WIN_VISIBLE
))
1531 /* need a full redraw for next Expose, not just a buf copy */
1532 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1537 xw
.state
&= ~WIN_VISIBLE
;
1541 xseturgency(int add
) {
1542 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1543 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1544 XSetWMHints(xw
.dis
, xw
.win
, h
);
1550 if(ev
->type
== FocusIn
) {
1551 xw
.state
|= WIN_FOCUSED
;
1554 xw
.state
&= ~WIN_FOCUSED
;
1555 draw(SCREEN_UPDATE
);
1561 for(i
= 0; i
< LEN(key
); i
++)
1563 return (char*)key
[i
].s
;
1568 kpress(XEvent
*ev
) {
1569 XKeyEvent
*e
= &ev
->xkey
;
1578 meta
= e
->state
& Mod1Mask
;
1579 shift
= e
->state
& ShiftMask
;
1580 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1582 /* 1. custom keys from config.h */
1583 if((customkey
= kmap(ksym
)))
1584 ttywrite(customkey
, strlen(customkey
));
1585 /* 2. hardcoded (overrides X lookup) */
1592 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1600 if(IS_SET(MODE_CRLF
))
1601 ttywrite("\r\n", 2);
1608 buf
[sizeof(buf
)-1] = '\0';
1609 if(meta
&& len
== 1)
1610 ttywrite("\033", 1);
1612 } else /* 4. nothing to send */
1613 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1622 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1625 xw
.w
= e
->xconfigure
.width
;
1626 xw
.h
= e
->xconfigure
.height
;
1627 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
1628 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
1629 if(col
== term
.col
&& row
== term
.row
)
1632 ttyresize(col
, row
);
1640 int xfd
= XConnectionNumber(xw
.dis
);
1644 FD_SET(cmdfd
, &rfd
);
1646 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1649 die("select failed: %s\n", SERRNO
);
1651 if(FD_ISSET(cmdfd
, &rfd
)) {
1653 draw(SCREEN_UPDATE
);
1655 while(XPending(xw
.dis
)) {
1656 XNextEvent(xw
.dis
, &ev
);
1657 if (XFilterEvent(&ev
, xw
.win
))
1659 if(handler
[ev
.type
])
1660 (handler
[ev
.type
])(&ev
);
1666 main(int argc
, char *argv
[]) {
1669 for(i
= 1; i
< argc
; i
++) {
1670 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1672 if(++i
< argc
) opt_title
= argv
[i
];
1675 if(++i
< argc
) opt_cmd
= argv
[i
];
1682 setlocale(LC_CTYPE
, "");