Xinqi Bao's Git
9e66d09bf3e94ffd4dc783a6dd7f4ad5187301db
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 */
110 int w
; /* window width */
111 int h
; /* window height */
112 int bufw
; /* pixmap width */
113 int bufh
; /* pixmap height */
114 int ch
; /* char height */
115 int cw
; /* char width */
124 /* Drawing Context */
126 unsigned long col
[256];
132 /* TODO: use better name for vars... */
137 struct {int x
, y
;} b
, e
;
143 static void die(const char *errstr
, ...);
144 static void draw(int);
145 static void execsh(void);
146 static void sigchld(int);
147 static void run(void);
149 static void csidump(void);
150 static void csihandle(void);
151 static void csiparse(void);
152 static void csireset(void);
154 static void tclearregion(int, int, int, int);
155 static void tcursor(int);
156 static void tdeletechar(int);
157 static void tdeleteline(int);
158 static void tinsertblank(int);
159 static void tinsertblankline(int);
160 static void tmoveto(int, int);
161 static void tnew(int, int);
162 static void tnewline(void);
163 static void tputtab(void);
164 static void tputc(char);
165 static void tputs(char*, int);
166 static void treset(void);
167 static void tresize(int, int);
168 static void tscrollup(int, int);
169 static void tscrolldown(int, int);
170 static void tsetattr(int*, int);
171 static void tsetchar(char);
172 static void tsetscroll(int, int);
173 static void tswapscreen(void);
175 static void ttynew(void);
176 static void ttyread(void);
177 static void ttyresize(int, int);
178 static void ttywrite(const char *, size_t);
180 static void xdraws(char *, Glyph
, int, int, int);
181 static void xhints(void);
182 static void xclear(int, int, int, int);
183 static void xdrawcursor(void);
184 static void xinit(void);
185 static void xloadcols(void);
186 static void xseturgency(int);
188 static void expose(XEvent
*);
189 static char* kmap(KeySym
);
190 static void kpress(XEvent
*);
191 static void resize(XEvent
*);
192 static void focus(XEvent
*);
193 static void brelease(XEvent
*);
194 static void bpress(XEvent
*);
195 static void bmotion(XEvent
*);
198 static void (*handler
[LASTEvent
])(XEvent
*) = {
201 [ConfigureNotify
] = resize
,
204 [MotionNotify
] = bmotion
,
205 [ButtonPress
] = bpress
,
206 [ButtonRelease
] = brelease
,
213 static CSIEscape escseq
;
216 static Selection sel
;
217 static char *opt_cmd
= NULL
;
218 static char *opt_title
= NULL
;
227 static inline int selected(int x
, int y
) {
228 if(sel
.ey
== y
&& sel
.by
== y
) {
229 int bx
= MIN(sel
.bx
, sel
.ex
);
230 int ex
= MAX(sel
.bx
, sel
.ex
);
231 return BETWEEN(x
, bx
, ex
);
233 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
234 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
237 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
238 if(b
) *b
= e
->xbutton
.state
,
239 *b
=*b
==4096?5:*b
==2048?4:*b
==1024?3:*b
==512?2:*b
==256?1:-1;
240 *x
= e
->xbutton
.x
/xw
.cw
;
241 *y
= e
->xbutton
.y
/xw
.ch
;
242 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
243 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
244 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
245 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
248 static void bpress(XEvent
*e
) {
250 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
251 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
254 static char *getseltext() {
259 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1);
260 ptr
= str
= malloc(sz
);
261 for(y
= 0; y
< term
.row
; y
++) {
262 for(x
= 0; x
< term
.col
; x
++)
263 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
)))
264 *ptr
= term
.line
[y
][x
].c
, ptr
++;
272 /* TODO: use X11 clipboard */
273 static void selcopy(char *str
) {
278 static void selpaste() {
280 ttywrite(sel
.clip
, strlen(sel
.clip
));
283 /* TODO: doubleclick to select word */
284 static void brelease(XEvent
*e
) {
287 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
288 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
294 selcopy(getseltext());
299 static void bmotion(XEvent
*e
) {
301 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
312 for(row
= 0; row
< term
.row
; row
++) {
313 for(col
= 0; col
< term
.col
; col
++) {
314 if(col
== term
.c
.x
&& row
== term
.c
.y
)
317 c
= term
.line
[row
][col
];
318 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
327 die(const char *errstr
, ...) {
330 va_start(ap
, errstr
);
331 vfprintf(stderr
, errstr
, ap
);
338 char *args
[] = {getenv("SHELL"), "-i", NULL
};
340 args
[0] = opt_cmd
, args
[1] = NULL
;
342 DEFAULT(args
[0], SHELL
);
343 putenv("TERM="TNAME
);
344 execvp(args
[0], args
);
350 if(waitpid(pid
, &stat
, 0) < 0)
351 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
353 exit(WEXITSTATUS(stat
));
362 /* seems to work fine on linux, openbsd and freebsd */
363 struct winsize w
= {term
.row
, term
.col
, 0, 0};
364 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
365 die("openpty failed: %s\n", SERRNO
);
367 switch(pid
= fork()) {
369 die("fork failed\n");
372 setsid(); /* create a new process group */
373 dup2(s
, STDIN_FILENO
);
374 dup2(s
, STDOUT_FILENO
);
375 dup2(s
, STDERR_FILENO
);
376 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
377 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
385 signal(SIGCHLD
, sigchld
);
392 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
394 fprintf(stderr
, "\n");
402 if((ret
= read(cmdfd
, buf
, LEN(buf
))) < 0)
403 die("Couldn't read from shell: %s\n", SERRNO
);
409 ttywrite(const char *s
, size_t n
) {
410 if(write(cmdfd
, s
, n
) == -1)
411 die("write error on tty: %s\n", SERRNO
);
415 ttyresize(int x
, int y
) {
420 w
.ws_xpixel
= w
.ws_ypixel
= 0;
421 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
422 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
429 if(mode
== CURSOR_SAVE
)
431 else if(mode
== CURSOR_LOAD
)
432 term
.c
= c
, tmoveto(c
.x
, c
.y
);
441 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
443 term
.top
= 0, term
.bot
= term
.row
- 1;
444 term
.mode
= MODE_WRAP
;
445 tclearregion(0, 0, term
.col
-1, term
.row
-1);
449 tnew(int col
, int row
) {
450 /* set screen size */
451 term
.row
= row
, term
.col
= col
;
452 term
.line
= malloc(term
.row
* sizeof(Line
));
453 term
.alt
= malloc(term
.row
* sizeof(Line
));
454 for(row
= 0 ; row
< term
.row
; row
++) {
455 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
456 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
464 Line
* tmp
= term
.line
;
465 term
.line
= term
.alt
;
467 term
.mode
^= MODE_ALTSCREEN
;
471 tscrolldown(int orig
, int n
) {
475 LIMIT(n
, 0, term
.bot
-orig
+1);
477 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
479 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
481 term
.line
[i
] = term
.line
[i
-n
];
482 term
.line
[i
-n
] = temp
;
487 tscrollup(int orig
, int n
) {
490 LIMIT(n
, 0, term
.bot
-orig
+1);
492 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
494 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
496 term
.line
[i
] = term
.line
[i
+n
];
497 term
.line
[i
+n
] = temp
;
504 if(term
.c
.y
== term
.bot
)
505 tscrollup(term
.top
, 1);
514 char *p
= escseq
.buf
;
518 escseq
.priv
= 1, p
++;
520 while(p
< escseq
.buf
+escseq
.len
) {
522 escseq
.arg
[escseq
.narg
] *= 10;
523 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
525 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
536 tmoveto(int x
, int y
) {
537 LIMIT(x
, 0, term
.col
-1);
538 LIMIT(y
, 0, term
.row
-1);
539 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
546 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
547 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
548 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
552 tclearregion(int x1
, int y1
, int x2
, int y2
) {
556 temp
= x1
, x1
= x2
, x2
= temp
;
558 temp
= y1
, y1
= y2
, y2
= temp
;
560 LIMIT(x1
, 0, term
.col
-1);
561 LIMIT(x2
, 0, term
.col
-1);
562 LIMIT(y1
, 0, term
.row
-1);
563 LIMIT(y2
, 0, term
.row
-1);
565 for(y
= y1
; y
<= y2
; y
++)
566 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
571 int src
= term
.c
.x
+ n
;
573 int size
= term
.col
- src
;
575 if(src
>= term
.col
) {
576 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
579 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
580 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
584 tinsertblank(int n
) {
587 int size
= term
.col
- dst
;
589 if(dst
>= term
.col
) {
590 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
593 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
594 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
598 tinsertblankline(int n
) {
599 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
602 tscrolldown(term
.c
.y
, n
);
607 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
610 tscrollup(term
.c
.y
, n
);
614 tsetattr(int *attr
, int l
) {
617 for(i
= 0; i
< l
; i
++) {
620 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
621 term
.c
.attr
.fg
= DefaultFG
;
622 term
.c
.attr
.bg
= DefaultBG
;
625 term
.c
.attr
.mode
|= ATTR_BOLD
;
628 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
631 term
.c
.attr
.mode
|= ATTR_REVERSE
;
634 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
637 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
640 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
643 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
645 if (BETWEEN(attr
[i
], 0, 255))
646 term
.c
.attr
.fg
= attr
[i
];
648 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
651 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
654 term
.c
.attr
.fg
= DefaultFG
;
657 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
659 if (BETWEEN(attr
[i
], 0, 255))
660 term
.c
.attr
.bg
= attr
[i
];
662 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
665 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
668 term
.c
.attr
.bg
= DefaultBG
;
671 if(BETWEEN(attr
[i
], 30, 37))
672 term
.c
.attr
.fg
= attr
[i
] - 30;
673 else if(BETWEEN(attr
[i
], 40, 47))
674 term
.c
.attr
.bg
= attr
[i
] - 40;
675 else if(BETWEEN(attr
[i
], 90, 97))
676 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
677 else if(BETWEEN(attr
[i
], 100, 107))
678 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
680 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
688 tsetscroll(int t
, int b
) {
691 LIMIT(t
, 0, term
.row
-1);
692 LIMIT(b
, 0, term
.row
-1);
704 switch(escseq
.mode
) {
707 printf("erresc: unknown csi ");
711 case '@': /* ICH -- Insert <n> blank char */
712 DEFAULT(escseq
.arg
[0], 1);
713 tinsertblank(escseq
.arg
[0]);
715 case 'A': /* CUU -- Cursor <n> Up */
717 DEFAULT(escseq
.arg
[0], 1);
718 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
720 case 'B': /* CUD -- Cursor <n> Down */
721 DEFAULT(escseq
.arg
[0], 1);
722 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
724 case 'C': /* CUF -- Cursor <n> Forward */
726 DEFAULT(escseq
.arg
[0], 1);
727 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
729 case 'D': /* CUB -- Cursor <n> Backward */
730 DEFAULT(escseq
.arg
[0], 1);
731 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
733 case 'E': /* CNL -- Cursor <n> Down and first col */
734 DEFAULT(escseq
.arg
[0], 1);
735 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
737 case 'F': /* CPL -- Cursor <n> Up and first col */
738 DEFAULT(escseq
.arg
[0], 1);
739 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
741 case 'G': /* CHA -- Move to <col> */
742 case '`': /* XXX: HPA -- same? */
743 DEFAULT(escseq
.arg
[0], 1);
744 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
746 case 'H': /* CUP -- Move to <row> <col> */
747 case 'f': /* XXX: HVP -- same? */
748 DEFAULT(escseq
.arg
[0], 1);
749 DEFAULT(escseq
.arg
[1], 1);
750 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
752 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
753 case 'J': /* ED -- Clear screen */
754 switch(escseq
.arg
[0]) {
756 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
759 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
762 tclearregion(0, 0, term
.col
-1, term
.row
-1);
768 case 'K': /* EL -- Clear line */
769 switch(escseq
.arg
[0]) {
771 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
774 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
777 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
781 case 'S': /* SU -- Scroll <n> line up */
782 DEFAULT(escseq
.arg
[0], 1);
783 tscrollup(term
.top
, escseq
.arg
[0]);
785 case 'T': /* SD -- Scroll <n> line down */
786 DEFAULT(escseq
.arg
[0], 1);
787 tscrolldown(term
.top
, escseq
.arg
[0]);
789 case 'L': /* IL -- Insert <n> blank lines */
790 DEFAULT(escseq
.arg
[0], 1);
791 tinsertblankline(escseq
.arg
[0]);
793 case 'l': /* RM -- Reset Mode */
795 switch(escseq
.arg
[0]) {
797 term
.mode
&= ~MODE_APPKEYPAD
;
800 term
.mode
&= ~MODE_WRAP
;
802 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
805 term
.c
.state
|= CURSOR_HIDE
;
807 case 1049: /* = 1047 and 1048 */
809 if(IS_SET(MODE_ALTSCREEN
)) {
810 tclearregion(0, 0, term
.col
-1, term
.row
-1);
813 if(escseq
.arg
[0] == 1047)
816 tcursor(CURSOR_LOAD
);
822 switch(escseq
.arg
[0]) {
824 term
.mode
&= ~MODE_INSERT
;
831 case 'M': /* DL -- Delete <n> lines */
832 DEFAULT(escseq
.arg
[0], 1);
833 tdeleteline(escseq
.arg
[0]);
835 case 'X': /* ECH -- Erase <n> char */
836 DEFAULT(escseq
.arg
[0], 1);
837 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
839 case 'P': /* DCH -- Delete <n> char */
840 DEFAULT(escseq
.arg
[0], 1);
841 tdeletechar(escseq
.arg
[0]);
843 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
844 case 'd': /* VPA -- Move to <row> */
845 DEFAULT(escseq
.arg
[0], 1);
846 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
848 case 'h': /* SM -- Set terminal mode */
850 switch(escseq
.arg
[0]) {
852 term
.mode
|= MODE_APPKEYPAD
;
855 term
.mode
|= MODE_WRAP
;
857 case 12: /* att610 -- Start blinking cursor (IGNORED) */
860 term
.c
.state
&= ~CURSOR_HIDE
;
862 case 1049: /* = 1047 and 1048 */
864 if(IS_SET(MODE_ALTSCREEN
))
865 tclearregion(0, 0, term
.col
-1, term
.row
-1);
868 if(escseq
.arg
[0] == 1047)
871 tcursor(CURSOR_SAVE
);
873 default: goto unknown
;
876 switch(escseq
.arg
[0]) {
878 term
.mode
|= MODE_INSERT
;
880 default: goto unknown
;
884 case 'm': /* SGR -- Terminal attribute (color) */
885 tsetattr(escseq
.arg
, escseq
.narg
);
887 case 'r': /* DECSTBM -- Set Scrolling Region */
891 DEFAULT(escseq
.arg
[0], 1);
892 DEFAULT(escseq
.arg
[1], term
.row
);
893 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
897 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
898 tcursor(CURSOR_SAVE
);
900 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
901 tcursor(CURSOR_LOAD
);
909 printf("ESC [ %s", escseq
.priv
? "? " : "");
911 for(i
= 0; i
< escseq
.narg
; i
++)
912 printf("%d ", escseq
.arg
[i
]);
914 putchar(escseq
.mode
);
920 memset(&escseq
, 0, sizeof(escseq
));
925 int space
= TAB
- term
.c
.x
% TAB
;
926 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
931 if(term
.esc
& ESC_START
) {
932 if(term
.esc
& ESC_CSI
) {
933 escseq
.buf
[escseq
.len
++] = c
;
934 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
936 csiparse(), csihandle();
938 /* TODO: handle other OSC */
939 } else if(term
.esc
& ESC_OSC
) {
942 term
.esc
= ESC_START
| ESC_TITLE
;
944 } else if(term
.esc
& ESC_TITLE
) {
945 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
947 term
.title
[term
.titlelen
] = '\0';
948 XStoreName(xw
.dis
, xw
.win
, term
.title
);
950 term
.title
[term
.titlelen
++] = c
;
952 } else if(term
.esc
& ESC_ALTCHARSET
) {
954 case '0': /* Line drawing crap */
955 term
.c
.attr
.mode
|= ATTR_GFX
;
957 case 'B': /* Back to regular text */
958 term
.c
.attr
.mode
&= ~ATTR_GFX
;
961 printf("esc unhandled charset: ESC ( %c\n", c
);
973 term
.esc
|= ESC_ALTCHARSET
;
975 case 'D': /* IND -- Linefeed */
976 if(term
.c
.y
== term
.bot
)
977 tscrollup(term
.top
, 1);
979 tmoveto(term
.c
.x
, term
.c
.y
+1);
982 case 'E': /* NEL -- Next line */
986 case 'M': /* RI -- Reverse index */
987 if(term
.c
.y
== term
.top
)
988 tscrolldown(term
.top
, 1);
990 tmoveto(term
.c
.x
, term
.c
.y
-1);
993 case 'c': /* RIS -- Reset to inital state */
997 case '=': /* DECPAM -- Application keypad */
998 term
.mode
|= MODE_APPKEYPAD
;
1001 case '>': /* DECPNM -- Normal keypad */
1002 term
.mode
&= ~MODE_APPKEYPAD
;
1005 case '7': /* DECSC -- Save Cursor */
1006 tcursor(CURSOR_SAVE
);
1009 case '8': /* DECRC -- Restore Cursor */
1010 tcursor(CURSOR_LOAD
);
1014 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
1024 tmoveto(term
.c
.x
-1, term
.c
.y
);
1027 tmoveto(0, term
.c
.y
);
1038 term
.esc
= ESC_START
;
1041 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1044 if(term
.c
.x
+1 < term
.col
)
1045 tmoveto(term
.c
.x
+1, term
.c
.y
);
1047 term
.c
.state
|= CURSOR_WRAPNEXT
;
1054 tputs(char *s
, int len
) {
1055 for(; len
> 0; len
--)
1060 tresize(int col
, int row
) {
1062 int minrow
= MIN(row
, term
.row
);
1063 int mincol
= MIN(col
, term
.col
);
1064 int slide
= term
.c
.y
- row
+ 1;
1066 if(col
< 1 || row
< 1)
1069 /* free unneeded rows */
1072 /* slide screen to keep cursor where we expect it -
1073 * tscrollup would work here, but we can optimize to
1074 * memmove because we're freeing the earlier lines */
1075 for(/* i = 0 */; i
< slide
; i
++) {
1079 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1080 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1082 for(i
+= row
; i
< term
.row
; i
++) {
1087 /* resize to new height */
1088 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1089 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1091 /* resize each row to new width, zero-pad if needed */
1092 for(i
= 0; i
< minrow
; i
++) {
1093 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1094 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1095 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1096 memset(term
.alt
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1099 /* allocate any new rows */
1100 for(/* i == minrow */; i
< row
; i
++) {
1101 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1102 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1105 /* update terminal size */
1106 term
.col
= col
, term
.row
= row
;
1107 /* make use of the LIMIT in tmoveto */
1108 tmoveto(term
.c
.x
, term
.c
.y
);
1109 /* reset scrolling region */
1110 tsetscroll(0, row
-1);
1117 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1118 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1120 for(i
= 0; i
< 16; i
++) {
1121 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1123 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1125 dc
.col
[i
] = color
.pixel
;
1128 /* same colors as xterm */
1129 for(r
= 0; r
< 6; r
++)
1130 for(g
= 0; g
< 6; g
++)
1131 for(b
= 0; b
< 6; b
++) {
1132 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1133 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1134 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1135 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1137 fprintf(stderr
, "Could not allocate color %d\n", i
);
1139 dc
.col
[i
] = color
.pixel
;
1143 for(r
= 0; r
< 24; r
++, i
++) {
1144 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1145 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1147 fprintf(stderr
, "Could not allocate color %d\n", i
);
1149 dc
.col
[i
] = color
.pixel
;
1154 xclear(int x1
, int y1
, int x2
, int y2
) {
1155 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1156 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1157 x1
* xw
.cw
, y1
* xw
.ch
,
1158 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1164 XClassHint
class = {TNAME
, TNAME
};
1165 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1167 .flags
= PSize
| PResizeInc
| PBaseSize
,
1170 .height_inc
= xw
.ch
,
1172 .base_height
= 2*BORDER
,
1173 .base_width
= 2*BORDER
,
1175 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1180 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1181 die("Can't open display\n");
1182 xw
.scr
= XDefaultScreen(xw
.dis
);
1185 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1186 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1188 /* XXX: Assuming same size for bold font */
1189 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1190 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1196 xw
.bufh
= term
.row
* xw
.ch
;
1197 xw
.bufw
= term
.col
* xw
.cw
;
1198 xw
.h
= xw
.bufh
+ 2*BORDER
;
1199 xw
.w
= xw
.bufw
+ 2*BORDER
;
1200 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1204 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1206 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1209 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
1210 | StructureNotifyMask
| FocusChangeMask
| PointerMotionMask
1211 | ButtonPressMask
| ButtonReleaseMask
);
1213 XMapWindow(xw
.dis
, xw
.win
);
1215 XStoreName(xw
.dis
, xw
.win
, opt_title
? opt_title
: "st");
1220 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1221 unsigned long xfg
, xbg
;
1222 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1225 if(base
.mode
& ATTR_REVERSE
)
1226 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1228 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1230 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1231 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1233 if(base
.mode
& ATTR_GFX
)
1234 for(i
= 0; i
< len
; i
++) {
1235 char c
= gfx
[(unsigned int)s
[i
] % 256];
1238 else if(s
[i
] > 0x5f)
1242 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1243 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1245 if(base
.mode
& ATTR_UNDERLINE
)
1246 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1251 static int oldx
= 0;
1252 static int oldy
= 0;
1253 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1255 LIMIT(oldx
, 0, term
.col
-1);
1256 LIMIT(oldy
, 0, term
.row
-1);
1258 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1259 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1261 /* remove the old cursor */
1262 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1263 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1265 xclear(oldx
, oldy
, oldx
, oldy
);
1267 /* draw the new one */
1268 if(!(term
.c
.state
& CURSOR_HIDE
) && xw
.hasfocus
) {
1269 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1270 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1275 /* basic drawing routines */
1277 xdrawc(int x
, int y
, Glyph g
) {
1278 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1279 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1280 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1281 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1282 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1289 xclear(0, 0, term
.col
-1, term
.row
-1);
1290 for(y
= 0; y
< term
.row
; y
++)
1291 for(x
= 0; x
< term
.col
; x
++)
1292 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1293 xdrawc(x
, y
, term
.line
[y
][x
]);
1296 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1301 /* optimized drawing routine */
1303 draw(int redraw_all
) {
1306 char buf
[DRAW_BUF_SIZ
];
1308 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1309 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
);
1310 for(y
= 0; y
< term
.row
; y
++) {
1311 base
= term
.line
[y
][0];
1313 for(x
= 0; x
< term
.col
; x
++) {
1314 new = term
.line
[y
][x
];
1315 if(sel
.bx
!=-1 && new.c
&& selected(x
, y
))
1316 new.mode
^= ATTR_REVERSE
;
1317 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1318 i
>= DRAW_BUF_SIZ
)) {
1319 xdraws(buf
, base
, ox
, y
, i
);
1322 if(new.state
& GLYPH_SET
) {
1331 xdraws(buf
, base
, ox
, y
, i
);
1334 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1341 expose(XEvent
*ev
) {
1342 draw(SCREEN_REDRAW
);
1346 xseturgency(int add
) {
1347 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1348 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1349 XSetWMHints(xw
.dis
, xw
.win
, h
);
1355 if((xw
.hasfocus
= ev
->type
== FocusIn
))
1357 draw(SCREEN_UPDATE
);
1363 for(i
= 0; i
< LEN(key
); i
++)
1365 return (char*)key
[i
].s
;
1370 kpress(XEvent
*ev
) {
1371 XKeyEvent
*e
= &ev
->xkey
;
1379 meta
= e
->state
& Mod1Mask
;
1380 shift
= e
->state
& ShiftMask
;
1381 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1383 if((customkey
= kmap(ksym
)))
1384 ttywrite(customkey
, strlen(customkey
));
1386 buf
[sizeof(buf
)-1] = '\0';
1387 if(meta
&& len
== 1)
1388 ttywrite("\033", 1);
1396 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1401 selpaste(), draw(1);
1404 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1413 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1416 xw
.w
= e
->xconfigure
.width
;
1417 xw
.h
= e
->xconfigure
.height
;
1418 xw
.bufw
= xw
.w
- 2*BORDER
;
1419 xw
.bufh
= xw
.h
- 2*BORDER
;
1420 col
= xw
.bufw
/ xw
.cw
;
1421 row
= xw
.bufh
/ xw
.ch
;
1423 ttyresize(col
, row
);
1424 xw
.bufh
= MAX(1, xw
.bufh
);
1425 xw
.bufw
= MAX(1, xw
.bufw
);
1426 XFreePixmap(xw
.dis
, xw
.buf
);
1427 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1428 draw(SCREEN_REDRAW
);
1435 int xfd
= XConnectionNumber(xw
.dis
);
1439 FD_SET(cmdfd
, &rfd
);
1441 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1444 die("select failed: %s\n", SERRNO
);
1446 if(FD_ISSET(cmdfd
, &rfd
)) {
1448 draw(SCREEN_UPDATE
);
1450 while(XPending(xw
.dis
)) {
1451 XNextEvent(xw
.dis
, &ev
);
1452 if(handler
[ev
.type
])
1453 (handler
[ev
.type
])(&ev
);
1459 main(int argc
, char *argv
[]) {
1462 for(i
= 1; i
< argc
; i
++) {
1463 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1465 if(++i
< argc
) opt_title
= argv
[i
];
1468 if(++i
< argc
) opt_cmd
= argv
[i
];
1475 setlocale(LC_CTYPE
, "");