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/keysym.h>
21 #include <X11/Xutil.h>
25 #elif defined(__OpenBSD__) || defined(__NetBSD__)
27 #elif defined(__FreeBSD__) || defined(__DragonFly__)
32 "st-" VERSION ", (c) 2010 st engineers\n" \
33 "usage: st [-t title] [-e cmd] [-v]\n"
36 #define ESC_TITLE_SIZ 256
37 #define ESC_BUF_SIZ 256
38 #define ESC_ARG_SIZ 16
39 #define DRAW_BUF_SIZ 1024
41 #define SERRNO strerror(errno)
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 #define MAX(a, b) ((a) < (b) ? (b) : (a))
44 #define LEN(a) (sizeof(a) / sizeof(a[0]))
45 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
46 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
47 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
48 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
49 #define IS_SET(flag) (term.mode & (flag))
51 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
52 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
53 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
54 CURSOR_SAVE
, CURSOR_LOAD
};
55 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
56 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
57 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8 };
58 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
59 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
62 char c
; /* character code */
63 char mode
; /* attribute flags */
64 int fg
; /* foreground */
65 int bg
; /* background */
66 char state
; /* state flags */
72 Glyph attr
; /* current char attributes */
78 /* CSI Escape sequence structs */
79 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
81 char buf
[ESC_BUF_SIZ
]; /* raw string */
82 int len
; /* raw string length */
85 int narg
; /* nb of args */
89 /* Internal representation of the screen */
93 Line
* line
; /* screen */
94 Line
* alt
; /* alternate screen */
95 TCursor c
; /* cursor */
96 int top
; /* top scroll limit */
97 int bot
; /* bottom scroll limit */
98 int mode
; /* terminal mode flags */
99 int esc
; /* escape state flags */
100 char title
[ESC_TITLE_SIZ
];
104 /* Purely graphic info */
113 int w
; /* window width */
114 int h
; /* window height */
115 int bufw
; /* pixmap width */
116 int bufh
; /* pixmap height */
117 int ch
; /* char height */
118 int cw
; /* char width */
120 int vis
; /* is visible */
128 /* Drawing Context */
130 unsigned long col
[256];
136 /* TODO: use better name for vars... */
141 struct {int x
, y
;} b
, e
;
147 static void die(const char *errstr
, ...);
148 static void draw(int);
149 static void execsh(void);
150 static void sigchld(int);
151 static void run(void);
153 static void csidump(void);
154 static void csihandle(void);
155 static void csiparse(void);
156 static void csireset(void);
158 static void tclearregion(int, int, int, int);
159 static void tcursor(int);
160 static void tdeletechar(int);
161 static void tdeleteline(int);
162 static void tinsertblank(int);
163 static void tinsertblankline(int);
164 static void tmoveto(int, int);
165 static void tnew(int, int);
166 static void tnewline(void);
167 static void tputtab(void);
168 static void tputc(char);
169 static void tputs(char*, int);
170 static void treset(void);
171 static void tresize(int, int);
172 static void tscrollup(int, int);
173 static void tscrolldown(int, int);
174 static void tsetattr(int*, int);
175 static void tsetchar(char);
176 static void tsetscroll(int, int);
177 static void tswapscreen(void);
179 static void ttynew(void);
180 static void ttyread(void);
181 static void ttyresize(int, int);
182 static void ttywrite(const char *, size_t);
184 static void xdraws(char *, Glyph
, int, int, int);
185 static void xhints(void);
186 static void xclear(int, int, int, int);
187 static void xdrawcursor(void);
188 static void xinit(void);
189 static void xloadcols(void);
190 static void xseturgency(int);
192 static void expose(XEvent
*);
193 static void visibility(XEvent
*);
194 static void unmap(XEvent
*);
195 static char* kmap(KeySym
);
196 static void kpress(XEvent
*);
197 static void resize(XEvent
*);
198 static void focus(XEvent
*);
199 static void brelease(XEvent
*);
200 static void bpress(XEvent
*);
201 static void bmotion(XEvent
*);
204 static void (*handler
[LASTEvent
])(XEvent
*) = {
206 [ConfigureNotify
] = resize
,
207 [VisibilityNotify
] = visibility
,
208 [UnmapNotify
] = unmap
,
212 [MotionNotify
] = bmotion
,
213 [ButtonPress
] = bpress
,
214 [ButtonRelease
] = brelease
,
221 static CSIEscape escseq
;
224 static Selection sel
;
225 static char *opt_cmd
= NULL
;
226 static char *opt_title
= NULL
;
235 static inline int selected(int x
, int y
) {
236 if(sel
.ey
== y
&& sel
.by
== y
) {
237 int bx
= MIN(sel
.bx
, sel
.ex
);
238 int ex
= MAX(sel
.bx
, sel
.ex
);
239 return BETWEEN(x
, bx
, ex
);
241 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
242 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
245 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
247 *b
= e
->xbutton
.button
;
249 *x
= e
->xbutton
.x
/xw
.cw
;
250 *y
= e
->xbutton
.y
/xw
.ch
;
251 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
252 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
253 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
254 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
257 static void bpress(XEvent
*e
) {
259 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
260 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
263 static char *getseltext() {
268 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1);
269 ptr
= str
= malloc(sz
);
270 for(y
= 0; y
< term
.row
; y
++) {
271 for(x
= 0; x
< term
.col
; x
++)
272 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
)))
273 *ptr
= term
.line
[y
][x
].c
, ptr
++;
281 /* TODO: use X11 clipboard */
282 static void selcopy(char *str
) {
287 static void selpaste() {
289 ttywrite(sel
.clip
, strlen(sel
.clip
));
292 /* TODO: doubleclick to select word */
293 static void brelease(XEvent
*e
) {
296 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
297 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
303 selcopy(getseltext());
308 static void bmotion(XEvent
*e
) {
310 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
321 for(row
= 0; row
< term
.row
; row
++) {
322 for(col
= 0; col
< term
.col
; col
++) {
323 if(col
== term
.c
.x
&& row
== term
.c
.y
)
326 c
= term
.line
[row
][col
];
327 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
336 die(const char *errstr
, ...) {
339 va_start(ap
, errstr
);
340 vfprintf(stderr
, errstr
, ap
);
347 char *args
[] = {getenv("SHELL"), "-i", NULL
};
349 args
[0] = opt_cmd
, args
[1] = NULL
;
351 DEFAULT(args
[0], SHELL
);
352 putenv("TERM="TNAME
);
353 execvp(args
[0], args
);
359 if(waitpid(pid
, &stat
, 0) < 0)
360 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
362 exit(WEXITSTATUS(stat
));
371 /* seems to work fine on linux, openbsd and freebsd */
372 struct winsize w
= {term
.row
, term
.col
, 0, 0};
373 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
374 die("openpty failed: %s\n", SERRNO
);
376 switch(pid
= fork()) {
378 die("fork failed\n");
381 setsid(); /* create a new process group */
382 dup2(s
, STDIN_FILENO
);
383 dup2(s
, STDOUT_FILENO
);
384 dup2(s
, STDERR_FILENO
);
385 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
386 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
394 signal(SIGCHLD
, sigchld
);
401 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
403 fprintf(stderr
, "\n");
411 if((ret
= read(cmdfd
, buf
, LEN(buf
))) < 0)
412 die("Couldn't read from shell: %s\n", SERRNO
);
418 ttywrite(const char *s
, size_t n
) {
419 if(write(cmdfd
, s
, n
) == -1)
420 die("write error on tty: %s\n", SERRNO
);
424 ttyresize(int x
, int y
) {
429 w
.ws_xpixel
= w
.ws_ypixel
= 0;
430 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
431 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
438 if(mode
== CURSOR_SAVE
)
440 else if(mode
== CURSOR_LOAD
)
441 term
.c
= c
, tmoveto(c
.x
, c
.y
);
450 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
452 term
.top
= 0, term
.bot
= term
.row
- 1;
453 term
.mode
= MODE_WRAP
;
454 tclearregion(0, 0, term
.col
-1, term
.row
-1);
458 tnew(int col
, int row
) {
459 /* set screen size */
460 term
.row
= row
, term
.col
= col
;
461 term
.line
= malloc(term
.row
* sizeof(Line
));
462 term
.alt
= malloc(term
.row
* sizeof(Line
));
463 for(row
= 0 ; row
< term
.row
; row
++) {
464 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
465 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
473 Line
* tmp
= term
.line
;
474 term
.line
= term
.alt
;
476 term
.mode
^= MODE_ALTSCREEN
;
480 tscrolldown(int orig
, int n
) {
484 LIMIT(n
, 0, term
.bot
-orig
+1);
486 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
488 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
490 term
.line
[i
] = term
.line
[i
-n
];
491 term
.line
[i
-n
] = temp
;
496 tscrollup(int orig
, int n
) {
499 LIMIT(n
, 0, term
.bot
-orig
+1);
501 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
503 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
505 term
.line
[i
] = term
.line
[i
+n
];
506 term
.line
[i
+n
] = temp
;
513 if(term
.c
.y
== term
.bot
)
514 tscrollup(term
.top
, 1);
523 char *p
= escseq
.buf
;
527 escseq
.priv
= 1, p
++;
529 while(p
< escseq
.buf
+escseq
.len
) {
531 escseq
.arg
[escseq
.narg
] *= 10;
532 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
534 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
545 tmoveto(int x
, int y
) {
546 LIMIT(x
, 0, term
.col
-1);
547 LIMIT(y
, 0, term
.row
-1);
548 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
555 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
556 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
557 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
561 tclearregion(int x1
, int y1
, int x2
, int y2
) {
565 temp
= x1
, x1
= x2
, x2
= temp
;
567 temp
= y1
, y1
= y2
, y2
= temp
;
569 LIMIT(x1
, 0, term
.col
-1);
570 LIMIT(x2
, 0, term
.col
-1);
571 LIMIT(y1
, 0, term
.row
-1);
572 LIMIT(y2
, 0, term
.row
-1);
574 for(y
= y1
; y
<= y2
; y
++)
575 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
580 int src
= term
.c
.x
+ n
;
582 int size
= term
.col
- src
;
584 if(src
>= term
.col
) {
585 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
588 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
589 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
593 tinsertblank(int n
) {
596 int size
= term
.col
- dst
;
598 if(dst
>= term
.col
) {
599 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
602 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
603 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
607 tinsertblankline(int n
) {
608 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
611 tscrolldown(term
.c
.y
, n
);
616 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
619 tscrollup(term
.c
.y
, n
);
623 tsetattr(int *attr
, int l
) {
626 for(i
= 0; i
< l
; i
++) {
629 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
630 term
.c
.attr
.fg
= DefaultFG
;
631 term
.c
.attr
.bg
= DefaultBG
;
634 term
.c
.attr
.mode
|= ATTR_BOLD
;
637 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
640 term
.c
.attr
.mode
|= ATTR_REVERSE
;
643 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
646 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
649 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
652 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
654 if (BETWEEN(attr
[i
], 0, 255))
655 term
.c
.attr
.fg
= attr
[i
];
657 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
660 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
663 term
.c
.attr
.fg
= DefaultFG
;
666 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
668 if (BETWEEN(attr
[i
], 0, 255))
669 term
.c
.attr
.bg
= attr
[i
];
671 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
674 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
677 term
.c
.attr
.bg
= DefaultBG
;
680 if(BETWEEN(attr
[i
], 30, 37))
681 term
.c
.attr
.fg
= attr
[i
] - 30;
682 else if(BETWEEN(attr
[i
], 40, 47))
683 term
.c
.attr
.bg
= attr
[i
] - 40;
684 else if(BETWEEN(attr
[i
], 90, 97))
685 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
686 else if(BETWEEN(attr
[i
], 100, 107))
687 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
689 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
697 tsetscroll(int t
, int b
) {
700 LIMIT(t
, 0, term
.row
-1);
701 LIMIT(b
, 0, term
.row
-1);
713 switch(escseq
.mode
) {
716 printf("erresc: unknown csi ");
720 case '@': /* ICH -- Insert <n> blank char */
721 DEFAULT(escseq
.arg
[0], 1);
722 tinsertblank(escseq
.arg
[0]);
724 case 'A': /* CUU -- Cursor <n> Up */
726 DEFAULT(escseq
.arg
[0], 1);
727 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
729 case 'B': /* CUD -- Cursor <n> Down */
730 DEFAULT(escseq
.arg
[0], 1);
731 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
733 case 'C': /* CUF -- Cursor <n> Forward */
735 DEFAULT(escseq
.arg
[0], 1);
736 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
738 case 'D': /* CUB -- Cursor <n> Backward */
739 DEFAULT(escseq
.arg
[0], 1);
740 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
742 case 'E': /* CNL -- Cursor <n> Down and first col */
743 DEFAULT(escseq
.arg
[0], 1);
744 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
746 case 'F': /* CPL -- Cursor <n> Up and first col */
747 DEFAULT(escseq
.arg
[0], 1);
748 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
750 case 'G': /* CHA -- Move to <col> */
751 case '`': /* XXX: HPA -- same? */
752 DEFAULT(escseq
.arg
[0], 1);
753 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
755 case 'H': /* CUP -- Move to <row> <col> */
756 case 'f': /* XXX: HVP -- same? */
757 DEFAULT(escseq
.arg
[0], 1);
758 DEFAULT(escseq
.arg
[1], 1);
759 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
761 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
762 case 'J': /* ED -- Clear screen */
763 switch(escseq
.arg
[0]) {
765 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
768 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
771 tclearregion(0, 0, term
.col
-1, term
.row
-1);
777 case 'K': /* EL -- Clear line */
778 switch(escseq
.arg
[0]) {
780 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
783 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
786 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
790 case 'S': /* SU -- Scroll <n> line up */
791 DEFAULT(escseq
.arg
[0], 1);
792 tscrollup(term
.top
, escseq
.arg
[0]);
794 case 'T': /* SD -- Scroll <n> line down */
795 DEFAULT(escseq
.arg
[0], 1);
796 tscrolldown(term
.top
, escseq
.arg
[0]);
798 case 'L': /* IL -- Insert <n> blank lines */
799 DEFAULT(escseq
.arg
[0], 1);
800 tinsertblankline(escseq
.arg
[0]);
802 case 'l': /* RM -- Reset Mode */
804 switch(escseq
.arg
[0]) {
806 term
.mode
&= ~MODE_APPKEYPAD
;
808 case 5: /* TODO: DECSCNM -- Remove reverse video */
811 term
.mode
&= ~MODE_WRAP
;
813 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
816 term
.c
.state
|= CURSOR_HIDE
;
818 case 1049: /* = 1047 and 1048 */
820 if(IS_SET(MODE_ALTSCREEN
)) {
821 tclearregion(0, 0, term
.col
-1, term
.row
-1);
824 if(escseq
.arg
[0] == 1047)
827 tcursor(CURSOR_LOAD
);
833 switch(escseq
.arg
[0]) {
835 term
.mode
&= ~MODE_INSERT
;
842 case 'M': /* DL -- Delete <n> lines */
843 DEFAULT(escseq
.arg
[0], 1);
844 tdeleteline(escseq
.arg
[0]);
846 case 'X': /* ECH -- Erase <n> char */
847 DEFAULT(escseq
.arg
[0], 1);
848 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
850 case 'P': /* DCH -- Delete <n> char */
851 DEFAULT(escseq
.arg
[0], 1);
852 tdeletechar(escseq
.arg
[0]);
854 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
855 case 'd': /* VPA -- Move to <row> */
856 DEFAULT(escseq
.arg
[0], 1);
857 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
859 case 'h': /* SM -- Set terminal mode */
861 switch(escseq
.arg
[0]) {
863 term
.mode
|= MODE_APPKEYPAD
;
865 case 5: /* DECSCNM -- Reverve video */
866 /* TODO: set REVERSE on the whole screen (f) */
869 term
.mode
|= MODE_WRAP
;
871 case 12: /* att610 -- Start blinking cursor (IGNORED) */
872 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
873 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
876 term
.c
.state
&= ~CURSOR_HIDE
;
878 case 1049: /* = 1047 and 1048 */
880 if(IS_SET(MODE_ALTSCREEN
))
881 tclearregion(0, 0, term
.col
-1, term
.row
-1);
884 if(escseq
.arg
[0] == 1047)
887 tcursor(CURSOR_SAVE
);
889 default: goto unknown
;
892 switch(escseq
.arg
[0]) {
894 term
.mode
|= MODE_INSERT
;
896 default: goto unknown
;
900 case 'm': /* SGR -- Terminal attribute (color) */
901 tsetattr(escseq
.arg
, escseq
.narg
);
903 case 'r': /* DECSTBM -- Set Scrolling Region */
907 DEFAULT(escseq
.arg
[0], 1);
908 DEFAULT(escseq
.arg
[1], term
.row
);
909 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
913 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
914 tcursor(CURSOR_SAVE
);
916 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
917 tcursor(CURSOR_LOAD
);
925 printf("ESC [ %s", escseq
.priv
? "? " : "");
927 for(i
= 0; i
< escseq
.narg
; i
++)
928 printf("%d ", escseq
.arg
[i
]);
930 putchar(escseq
.mode
);
936 memset(&escseq
, 0, sizeof(escseq
));
941 int space
= TAB
- term
.c
.x
% TAB
;
942 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
947 if(term
.esc
& ESC_START
) {
948 if(term
.esc
& ESC_CSI
) {
949 escseq
.buf
[escseq
.len
++] = c
;
950 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
952 csiparse(), csihandle();
954 /* TODO: handle other OSC */
955 } else if(term
.esc
& ESC_OSC
) {
958 term
.esc
= ESC_START
| ESC_TITLE
;
960 } else if(term
.esc
& ESC_TITLE
) {
961 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
963 term
.title
[term
.titlelen
] = '\0';
964 XStoreName(xw
.dis
, xw
.win
, term
.title
);
966 term
.title
[term
.titlelen
++] = c
;
968 } else if(term
.esc
& ESC_ALTCHARSET
) {
970 case '0': /* Line drawing crap */
971 term
.c
.attr
.mode
|= ATTR_GFX
;
973 case 'B': /* Back to regular text */
974 term
.c
.attr
.mode
&= ~ATTR_GFX
;
977 printf("esc unhandled charset: ESC ( %c\n", c
);
989 term
.esc
|= ESC_ALTCHARSET
;
991 case 'D': /* IND -- Linefeed */
992 if(term
.c
.y
== term
.bot
)
993 tscrollup(term
.top
, 1);
995 tmoveto(term
.c
.x
, term
.c
.y
+1);
998 case 'E': /* NEL -- Next line */
1002 case 'M': /* RI -- Reverse index */
1003 if(term
.c
.y
== term
.top
)
1004 tscrolldown(term
.top
, 1);
1006 tmoveto(term
.c
.x
, term
.c
.y
-1);
1009 case 'c': /* RIS -- Reset to inital state */
1013 case '=': /* DECPAM -- Application keypad */
1014 term
.mode
|= MODE_APPKEYPAD
;
1017 case '>': /* DECPNM -- Normal keypad */
1018 term
.mode
&= ~MODE_APPKEYPAD
;
1021 case '7': /* DECSC -- Save Cursor */
1022 tcursor(CURSOR_SAVE
);
1025 case '8': /* DECRC -- Restore Cursor */
1026 tcursor(CURSOR_LOAD
);
1030 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
1040 tmoveto(term
.c
.x
-1, term
.c
.y
);
1043 tmoveto(0, term
.c
.y
);
1054 term
.esc
= ESC_START
;
1057 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1060 if(term
.c
.x
+1 < term
.col
)
1061 tmoveto(term
.c
.x
+1, term
.c
.y
);
1063 term
.c
.state
|= CURSOR_WRAPNEXT
;
1070 tputs(char *s
, int len
) {
1071 for(; len
> 0; len
--)
1076 tresize(int col
, int row
) {
1078 int minrow
= MIN(row
, term
.row
);
1079 int mincol
= MIN(col
, term
.col
);
1080 int slide
= term
.c
.y
- row
+ 1;
1082 if(col
< 1 || row
< 1)
1085 /* free unneeded rows */
1088 /* slide screen to keep cursor where we expect it -
1089 * tscrollup would work here, but we can optimize to
1090 * memmove because we're freeing the earlier lines */
1091 for(/* i = 0 */; i
< slide
; i
++) {
1095 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1096 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1098 for(i
+= row
; i
< term
.row
; i
++) {
1103 /* resize to new height */
1104 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1105 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1107 /* resize each row to new width, zero-pad if needed */
1108 for(i
= 0; i
< minrow
; i
++) {
1109 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1110 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1111 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1112 memset(term
.alt
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1115 /* allocate any new rows */
1116 for(/* i == minrow */; i
< row
; i
++) {
1117 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1118 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1121 /* update terminal size */
1122 term
.col
= col
, term
.row
= row
;
1123 /* make use of the LIMIT in tmoveto */
1124 tmoveto(term
.c
.x
, term
.c
.y
);
1125 /* reset scrolling region */
1126 tsetscroll(0, row
-1);
1133 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1135 for(i
= 0; i
< 16; i
++) {
1136 if (!XAllocNamedColor(xw
.dis
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1138 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1140 dc
.col
[i
] = color
.pixel
;
1143 /* same colors as xterm */
1144 for(r
= 0; r
< 6; r
++)
1145 for(g
= 0; g
< 6; g
++)
1146 for(b
= 0; b
< 6; b
++) {
1147 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1148 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1149 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1150 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1152 fprintf(stderr
, "Could not allocate color %d\n", i
);
1154 dc
.col
[i
] = color
.pixel
;
1158 for(r
= 0; r
< 24; r
++, i
++) {
1159 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1160 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1162 fprintf(stderr
, "Could not allocate color %d\n", i
);
1164 dc
.col
[i
] = color
.pixel
;
1169 xclear(int x1
, int y1
, int x2
, int y2
) {
1170 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1171 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1172 x1
* xw
.cw
, y1
* xw
.ch
,
1173 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1179 XClassHint
class = {TNAME
, TNAME
};
1180 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1182 .flags
= PSize
| PResizeInc
| PBaseSize
,
1185 .height_inc
= xw
.ch
,
1187 .base_height
= 2*BORDER
,
1188 .base_width
= 2*BORDER
,
1190 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1195 XSetWindowAttributes attrs
;
1197 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1198 die("Can't open display\n");
1199 xw
.scr
= XDefaultScreen(xw
.dis
);
1202 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1203 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1205 /* XXX: Assuming same size for bold font */
1206 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1207 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1210 xw
.cmap
= XDefaultColormap(xw
.dis
, xw
.scr
);
1213 /* window - default size */
1214 xw
.bufh
= 24 * xw
.ch
;
1215 xw
.bufw
= 80 * xw
.cw
;
1216 xw
.h
= xw
.bufh
+ 2*BORDER
;
1217 xw
.w
= xw
.bufw
+ 2*BORDER
;
1219 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1220 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1221 attrs
.bit_gravity
= NorthWestGravity
;
1222 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1223 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1224 | PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1225 attrs
.colormap
= xw
.cmap
;
1227 xw
.win
= XCreateWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1228 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dis
, xw
.scr
), InputOutput
,
1229 XDefaultVisual(xw
.dis
, xw
.scr
),
1230 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1233 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1237 xw
.xim
= XOpenIM(xw
.dis
, NULL
, NULL
, NULL
);
1238 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1239 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1240 XNFocusWindow
, xw
.win
, NULL
);
1242 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1244 XMapWindow(xw
.dis
, xw
.win
);
1246 XStoreName(xw
.dis
, xw
.win
, opt_title
? opt_title
: "st");
1251 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1252 unsigned long xfg
, xbg
;
1253 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1256 if(base
.mode
& ATTR_REVERSE
)
1257 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1259 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1261 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1262 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1264 if(base
.mode
& ATTR_GFX
)
1265 for(i
= 0; i
< len
; i
++) {
1266 char c
= gfx
[(unsigned int)s
[i
] % 256];
1269 else if(s
[i
] > 0x5f)
1273 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1274 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1276 if(base
.mode
& ATTR_UNDERLINE
)
1277 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1282 static int oldx
= 0;
1283 static int oldy
= 0;
1284 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1286 LIMIT(oldx
, 0, term
.col
-1);
1287 LIMIT(oldy
, 0, term
.row
-1);
1289 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1290 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1292 /* remove the old cursor */
1293 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1294 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1296 xclear(oldx
, oldy
, oldx
, oldy
);
1298 /* draw the new one */
1299 if(!(term
.c
.state
& CURSOR_HIDE
) && xw
.focus
) {
1300 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1301 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1306 /* basic drawing routines */
1308 xdrawc(int x
, int y
, Glyph g
) {
1309 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1310 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1311 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1312 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1313 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1320 xclear(0, 0, term
.col
-1, term
.row
-1);
1321 for(y
= 0; y
< term
.row
; y
++)
1322 for(x
= 0; x
< term
.col
; x
++)
1323 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1324 xdrawc(x
, y
, term
.line
[y
][x
]);
1327 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1332 /* optimized drawing routine */
1334 draw(int redraw_all
) {
1337 char buf
[DRAW_BUF_SIZ
];
1342 xclear(0, 0, term
.col
-1, term
.row
-1);
1343 for(y
= 0; y
< term
.row
; y
++) {
1344 base
= term
.line
[y
][0];
1346 for(x
= 0; x
< term
.col
; x
++) {
1347 new = term
.line
[y
][x
];
1348 if(sel
.bx
!=-1 && new.c
&& selected(x
, y
))
1349 new.mode
^= ATTR_REVERSE
;
1350 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1351 i
>= DRAW_BUF_SIZ
)) {
1352 xdraws(buf
, base
, ox
, y
, i
);
1355 if(new.state
& GLYPH_SET
) {
1364 xdraws(buf
, base
, ox
, y
, i
);
1367 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1374 expose(XEvent
*ev
) {
1375 draw(SCREEN_REDRAW
);
1379 visibility(XEvent
*ev
) {
1380 XVisibilityEvent
*e
= &ev
->xvisibility
;
1381 /* XXX if this goes from 0 to 1, need a full redraw for next Expose,
1382 * not just a buf copy */
1383 xw
.vis
= e
->state
!= VisibilityFullyObscured
;
1392 xseturgency(int add
) {
1393 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1394 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1395 XSetWMHints(xw
.dis
, xw
.win
, h
);
1401 if((xw
.focus
= ev
->type
== FocusIn
))
1403 draw(SCREEN_UPDATE
);
1409 for(i
= 0; i
< LEN(key
); i
++)
1411 return (char*)key
[i
].s
;
1416 kpress(XEvent
*ev
) {
1417 XKeyEvent
*e
= &ev
->xkey
;
1426 meta
= e
->state
& Mod1Mask
;
1427 shift
= e
->state
& ShiftMask
;
1428 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1430 if((customkey
= kmap(ksym
)))
1431 ttywrite(customkey
, strlen(customkey
));
1433 buf
[sizeof(buf
)-1] = '\0';
1434 if(meta
&& len
== 1)
1435 ttywrite("\033", 1);
1443 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1448 selpaste(), draw(1);
1451 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1460 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1463 xw
.w
= e
->xconfigure
.width
;
1464 xw
.h
= e
->xconfigure
.height
;
1465 xw
.bufw
= xw
.w
- 2*BORDER
;
1466 xw
.bufh
= xw
.h
- 2*BORDER
;
1467 col
= xw
.bufw
/ xw
.cw
;
1468 row
= xw
.bufh
/ xw
.ch
;
1470 ttyresize(col
, row
);
1471 xw
.bufh
= MAX(1, xw
.bufh
);
1472 xw
.bufw
= MAX(1, xw
.bufw
);
1473 XFreePixmap(xw
.dis
, xw
.buf
);
1474 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1481 int xfd
= XConnectionNumber(xw
.dis
);
1485 FD_SET(cmdfd
, &rfd
);
1487 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1490 die("select failed: %s\n", SERRNO
);
1492 if(FD_ISSET(cmdfd
, &rfd
)) {
1494 draw(SCREEN_UPDATE
);
1496 while(XPending(xw
.dis
)) {
1497 XNextEvent(xw
.dis
, &ev
);
1498 if (XFilterEvent(&ev
, xw
.win
))
1500 if(handler
[ev
.type
])
1501 (handler
[ev
.type
])(&ev
);
1507 main(int argc
, char *argv
[]) {
1510 for(i
= 1; i
< argc
; i
++) {
1511 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1513 if(++i
< argc
) opt_title
= argv
[i
];
1516 if(++i
< argc
) opt_cmd
= argv
[i
];
1523 setlocale(LC_CTYPE
, "");