Xinqi Bao's Git
f288f2a1695c71d68c214360b79afb9fe968e611
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>
26 #define ESC_TITLE_SIZ 256
27 #define ESC_BUF_SIZ 256
28 #define ESC_ARG_SIZ 16
29 #define DRAW_BUF_SIZ 1024
31 #define SERRNO strerror(errno)
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33 #define MAX(a, b) ((a) < (b) ? (b) : (a))
34 #define LEN(a) (sizeof(a) / sizeof(a[0]))
35 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
36 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
37 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
38 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
40 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
41 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
42 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
, CURSOR_HIDE
, CURSOR_DRAW
, CURSOR_SAVE
, CURSOR_LOAD
};
43 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
44 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4 };
45 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
46 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
49 char c
; /* character code */
50 char mode
; /* attribute flags */
51 int fg
; /* foreground */
52 int bg
; /* background */
53 char state
; /* state flags */
59 Glyph attr
; /* current char attributes */
64 /* CSI Escape sequence structs */
65 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
67 char buf
[ESC_BUF_SIZ
]; /* raw string */
68 int len
; /* raw string length */
71 int narg
; /* nb of args */
75 /* Internal representation of the screen */
79 Line
* line
; /* screen */
80 TCursor c
; /* cursor */
82 int top
; /* top scroll limit */
83 int bot
; /* bottom scroll limit */
84 int mode
; /* terminal mode flags */
85 int esc
; /* escape state flags */
86 char title
[ESC_TITLE_SIZ
];
90 /* Purely graphic info */
95 int w
; /* window width */
96 int h
; /* window height */
97 int ch
; /* char height */
98 int cw
; /* char width */
108 /* Drawing Context */
110 unsigned long col
[LEN(colorname
)];
116 static void die(const char *errstr
, ...);
117 static void draw(int);
118 static void execsh(void);
119 static void sigchld(int);
120 static void run(void);
122 static void csidump(void);
123 static void csihandle(void);
124 static void csiparse(void);
125 static void csireset(void);
127 static void tclearregion(int, int, int, int);
128 static void tcursor(int);
129 static void tmovecursor(int);
130 static void tdeletechar(int);
131 static void tdeleteline(int);
132 static void tinsertblank(int);
133 static void tinsertblankline(int);
134 static void tmoveto(int, int);
135 static void tnew(int, int);
136 static void tnewline(void);
137 static void tputc(char);
138 static void tputs(char*, int);
139 static void treset(void);
140 static void tresize(int, int);
141 static void tscroll(void);
142 static void tscrollup(int);
143 static void tscrolldown(int);
144 static void tsetattr(int*, int);
145 static void tsetchar(char);
146 static void tsetscroll(int, int);
148 static void ttynew(void);
149 static void ttyread(void);
150 static void ttyresize(int, int);
151 static void ttywrite(const char *, size_t);
153 static unsigned long xgetcol(const char *);
154 static void xclear(int, int, int, int);
155 static void xcursor(int);
156 static void xinit(void);
157 static void xscroll(void);
159 static void expose(XEvent
*);
160 static char* kmap(KeySym
);
161 static void kpress(XEvent
*);
162 static void resize(XEvent
*);
164 static void (*handler
[LASTEvent
])(XEvent
*) = {
167 [ConfigureNotify
] = resize
174 static CSIEscape escseq
;
185 for(row
= 0; row
< term
.row
; row
++) {
186 for(col
= 0; col
< term
.col
; col
++) {
187 if(col
== term
.c
.x
&& row
== term
.c
.y
)
190 c
= term
.line
[row
][col
];
191 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
200 die(const char *errstr
, ...) {
203 va_start(ap
, errstr
);
204 vfprintf(stderr
, errstr
, ap
);
211 char *args
[3] = {SHELL
, "-i", NULL
};
212 putenv("TERM=" TNAME
);
217 xbell(void) { /* visual bell */
218 XRectangle r
= { 0, 0, xw
.w
, xw
.h
};
219 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
220 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
228 if(waitpid(pid
, &stat
, 0) < 0)
229 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
231 exit(WEXITSTATUS(stat
));
241 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
242 die("openpt failed: %s\n", SERRNO
);
244 die("grandpt failed: %s\n", SERRNO
);
246 die("unlockpt failed: %s\n", SERRNO
);
247 if(!(pts
= ptsname(m
)))
248 die("ptsname failed: %s\n", SERRNO
);
249 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
250 die("Couldn't open slave: %s\n", SERRNO
);
251 fcntl(s
, F_SETFL
, O_NDELAY
);
252 switch(pid
= fork()) {
254 die("fork failed\n");
257 setsid(); /* create a new process group */
258 dup2(s
, STDIN_FILENO
);
259 dup2(s
, STDOUT_FILENO
);
260 dup2(s
, STDERR_FILENO
);
261 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
262 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
268 signal(SIGCHLD
, sigchld
);
275 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
277 fprintf(stderr
, "\n");
282 char buf
[BUFSIZ
] = {0};
285 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
286 die("Couldn't read from shell: %s\n", SERRNO
);
292 ttywrite(const char *s
, size_t n
) {
293 if(write(cmdfd
, s
, n
) == -1)
294 die("write error on tty: %s\n", SERRNO
);
298 ttyresize(int x
, int y
) {
303 w
.ws_xpixel
= w
.ws_ypixel
= 0;
304 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
305 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
312 if(mode
== CURSOR_SAVE
)
314 else if(mode
== CURSOR_LOAD
)
315 term
.c
= c
, tmoveto(c
.x
, c
.y
);
320 term
.c
.attr
.mode
= ATTR_NULL
;
321 term
.c
.attr
.fg
= DefaultFG
;
322 term
.c
.attr
.bg
= DefaultBG
;
323 term
.c
.x
= term
.c
.y
= 0;
325 term
.top
= 0, term
.bot
= term
.row
- 1;
326 term
.mode
= MODE_WRAP
;
327 tclearregion(0, 0, term
.col
-1, term
.row
-1);
331 tnew(int col
, int row
) { /* screen size */
332 term
.row
= row
, term
.col
= col
;
333 term
.top
= 0, term
.bot
= term
.row
- 1;
335 term
.mode
= MODE_WRAP
;
337 term
.c
.attr
.mode
= ATTR_NULL
;
338 term
.c
.attr
.fg
= DefaultFG
;
339 term
.c
.attr
.bg
= DefaultBG
;
340 term
.c
.x
= term
.c
.y
= 0;
342 /* allocate screen */
343 term
.line
= calloc(term
.row
, sizeof(Line
));
344 for(row
= 0 ; row
< term
.row
; row
++)
345 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
348 /* TODO: Replace with scrollup/scolldown */
351 Line temp
= term
.line
[term
.top
];
353 /* No dirty flag to set because of xscroll */
354 /* X stuff _before_ the line swapping (results in wrong line index) */
356 for(i
= term
.top
; i
< term
.bot
; i
++)
357 term
.line
[i
] = term
.line
[i
+1];
358 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
359 term
.line
[term
.bot
] = temp
;
363 tscrolldown (int n
) {
367 /* TODO: set dirty flag or scroll with some X func */
368 LIMIT(n
, 0, term
.bot
-term
.top
+1);
370 for(i
= 0; i
< n
; i
++)
371 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
373 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
375 term
.line
[i
] = term
.line
[i
-n
];
376 term
.line
[i
-n
] = temp
;
384 LIMIT(n
, 0, term
.bot
-term
.top
+1);
386 /* TODO: set dirty flag or scroll with some X func */
387 for(i
= 0; i
< n
; i
++)
388 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
390 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
392 term
.line
[i
] = term
.line
[i
+n
];
393 term
.line
[i
+n
] = temp
;
399 int y
= term
.c
.y
+ 1;
401 tscroll(), y
= term
.bot
;
408 char *p
= escseq
.buf
;
412 escseq
.priv
= 1, p
++;
414 while(p
< escseq
.buf
+escseq
.len
) {
416 escseq
.arg
[escseq
.narg
] *= 10;
417 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
419 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
430 tmoveto(int x
, int y
) {
431 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
432 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
436 tmovecursor(int dir
) {
437 int xf
= term
.c
.x
, yf
= term
.c
.y
;
448 if(term
.mode
& MODE_WRAP
&& xf
< 0) {
449 xf
= term
.col
-1, yf
--;
451 yf
= term
.top
, xf
= 0;
456 if(term
.mode
& MODE_WRAP
&& xf
>= term
.col
) {
459 yf
= term
.bot
, tscroll();
468 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
469 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
470 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
| GLYPH_DIRTY
;
474 tclearregion(int x1
, int y1
, int x2
, int y2
) {
478 temp
= x1
, x1
= x2
, x2
= temp
;
480 temp
= y1
, y1
= y2
, y2
= temp
;
482 LIMIT(x1
, 0, term
.col
-1);
483 LIMIT(x2
, 0, term
.col
-1);
484 LIMIT(y1
, 0, term
.row
-1);
485 LIMIT(y2
, 0, term
.row
-1);
487 for(y
= y1
; y
<= y2
; y
++)
488 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
490 xclear(x1
, y1
, x2
, y2
);
495 int src
= term
.c
.x
+ n
;
497 int size
= term
.col
- src
;
499 if(src
>= term
.col
) {
500 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
503 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
504 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
508 tinsertblank(int n
) {
511 int size
= term
.col
- n
- src
;
513 if(dst
>= term
.col
) {
514 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
517 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
518 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
522 tsetlinestate(int n
, int state
) {
524 for(i
= 0; i
< term
.col
; i
++)
525 term
.line
[n
][i
].state
|= state
;
529 tinsertblankline(int n
) {
534 if(term
.c
.y
> term
.bot
)
536 else if(term
.c
.y
< term
.top
)
538 if(term
.c
.y
+ n
>= bot
) {
539 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
542 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
543 /* swap deleted line <-> blanked line */
544 blank
= term
.line
[i
];
545 term
.line
[i
] = term
.line
[i
-n
];
546 term
.line
[i
-n
] = blank
;
548 memset(blank
, 0, term
.col
* sizeof(Glyph
));
549 tsetlinestate(i
, GLYPH_DIRTY
);
550 tsetlinestate(i
-n
, GLYPH_DIRTY
);
560 if(term
.c
.y
> term
.bot
)
562 else if(term
.c
.y
< term
.top
)
564 if(term
.c
.y
+ n
>= bot
) {
565 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
568 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
569 /* swap deleted line <-> blanked line */
570 blank
= term
.line
[i
];
571 term
.line
[i
] = term
.line
[i
+n
];
572 term
.line
[i
+n
] = blank
;
574 memset(blank
, 0, term
.col
* sizeof(Glyph
));
575 tsetlinestate(i
, GLYPH_DIRTY
);
576 tsetlinestate(i
-n
, GLYPH_DIRTY
);
581 tsetattr(int *attr
, int l
) {
584 for(i
= 0; i
< l
; i
++) {
587 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
588 term
.c
.attr
.fg
= DefaultFG
;
589 term
.c
.attr
.bg
= DefaultBG
;
592 term
.c
.attr
.mode
|= ATTR_BOLD
;
595 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
598 term
.c
.attr
.mode
|= ATTR_REVERSE
;
601 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
604 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
607 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
610 term
.c
.attr
.fg
= DefaultFG
;
613 term
.c
.attr
.fg
= DefaultBG
;
616 if(BETWEEN(attr
[i
], 30, 37))
617 term
.c
.attr
.fg
= attr
[i
] - 30;
618 else if(BETWEEN(attr
[i
], 40, 47))
619 term
.c
.attr
.bg
= attr
[i
] - 40;
621 fprintf(stderr
, "erresc: gfx attr %d unkown\n", attr
[i
]);
628 tsetscroll(int t
, int b
) {
631 LIMIT(t
, 0, term
.row
-1);
632 LIMIT(b
, 0, term
.row
-1);
644 switch(escseq
.mode
) {
647 printf("erresc: unknown csi ");
651 case '@': /* ICH -- Insert <n> blank char */
652 DEFAULT(escseq
.arg
[0], 1);
653 tinsertblank(escseq
.arg
[0]);
655 case 'A': /* CUU -- Cursor <n> Up */
657 DEFAULT(escseq
.arg
[0], 1);
658 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
660 case 'B': /* CUD -- Cursor <n> Down */
661 DEFAULT(escseq
.arg
[0], 1);
662 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
664 case 'C': /* CUF -- Cursor <n> Forward */
666 DEFAULT(escseq
.arg
[0], 1);
667 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
669 case 'D': /* CUB -- Cursor <n> Backward */
670 DEFAULT(escseq
.arg
[0], 1);
671 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
673 case 'E': /* CNL -- Cursor <n> Down and first col */
674 DEFAULT(escseq
.arg
[0], 1);
675 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
677 case 'F': /* CPL -- Cursor <n> Up and first col */
678 DEFAULT(escseq
.arg
[0], 1);
679 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
681 case 'G': /* CHA -- Move to <col> */
682 case '`': /* XXX: HPA -- same? */
683 DEFAULT(escseq
.arg
[0], 1);
684 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
686 case 'H': /* CUP -- Move to <row> <col> */
687 case 'f': /* XXX: HVP -- same? */
688 DEFAULT(escseq
.arg
[0], 1);
689 DEFAULT(escseq
.arg
[1], 1);
690 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
692 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
693 case 'J': /* ED -- Clear screen */
694 switch(escseq
.arg
[0]) {
696 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
699 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
702 tclearregion(0, 0, term
.col
-1, term
.row
-1);
704 case 3: /* XXX: erase saved lines (xterm) */
709 case 'K': /* EL -- Clear line */
710 switch(escseq
.arg
[0]) {
712 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
715 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
718 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
722 case 'S': /* SU -- Scroll <n> line up */
723 DEFAULT(escseq
.arg
[0], 1);
724 tscrollup(escseq
.arg
[0]);
726 case 'T': /* SD -- Scroll <n> line down */
727 DEFAULT(escseq
.arg
[0], 1);
728 tscrolldown(escseq
.arg
[0]);
730 case 'L': /* IL -- Insert <n> blank lines */
731 DEFAULT(escseq
.arg
[0], 1);
732 tinsertblankline(escseq
.arg
[0]);
734 case 'l': /* RM -- Reset Mode */
736 switch(escseq
.arg
[0]) {
738 term
.mode
&= ~MODE_APPKEYPAD
;
741 term
.mode
&= ~MODE_WRAP
;
743 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
748 case 1048: /* XXX: no alt. screen to erase/save */
750 tcursor(CURSOR_LOAD
);
751 tclearregion(0, 0, term
.col
-1, term
.row
-1);
757 switch(escseq
.arg
[0]) {
759 term
.mode
&= ~MODE_INSERT
;
766 case 'M': /* DL -- Delete <n> lines */
767 DEFAULT(escseq
.arg
[0], 1);
768 tdeleteline(escseq
.arg
[0]);
770 case 'X': /* ECH -- Erase <n> char */
771 DEFAULT(escseq
.arg
[0], 1);
772 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
774 case 'P': /* DCH -- Delete <n> char */
775 DEFAULT(escseq
.arg
[0], 1);
776 tdeletechar(escseq
.arg
[0]);
778 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
779 case 'd': /* VPA -- Move to <row> */
780 DEFAULT(escseq
.arg
[0], 1);
781 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
783 case 'h': /* SM -- Set terminal mode */
785 switch(escseq
.arg
[0]) {
787 term
.mode
|= MODE_APPKEYPAD
;
790 term
.mode
|= MODE_WRAP
;
792 case 12: /* att610 -- Start blinking cursor (IGNORED) */
798 case 1049: /* XXX: no alt. screen to erase/save */
799 tcursor(CURSOR_SAVE
);
800 tclearregion(0, 0, term
.col
-1, term
.row
-1);
802 default: goto unknown
;
805 switch(escseq
.arg
[0]) {
807 term
.mode
|= MODE_INSERT
;
809 default: goto unknown
;
813 case 'm': /* SGR -- Terminal attribute (color) */
814 tsetattr(escseq
.arg
, escseq
.narg
);
816 case 'r': /* DECSTBM -- Set Scrolling Region */
820 DEFAULT(escseq
.arg
[0], 1);
821 DEFAULT(escseq
.arg
[1], term
.row
);
822 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
825 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
826 tcursor(CURSOR_SAVE
);
828 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
829 tcursor(CURSOR_LOAD
);
837 printf("ESC [ %s", escseq
.priv
? "? " : "");
839 for(i
= 0; i
< escseq
.narg
; i
++)
840 printf("%d ", escseq
.arg
[i
]);
842 putchar(escseq
.mode
);
848 memset(&escseq
, 0, sizeof(escseq
));
853 int space
= TAB
- term
.c
.x
% TAB
;
855 if(term
.c
.x
+ space
>= term
.col
)
858 for(; space
> 0; space
--)
859 tmovecursor(CURSOR_RIGHT
);
865 if(term
.esc
& ESC_START
) {
866 if(term
.esc
& ESC_CSI
) {
867 escseq
.buf
[escseq
.len
++] = c
;
868 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
870 csiparse(), csihandle();
872 } else if(term
.esc
& ESC_OSC
) {
875 term
.esc
= ESC_START
| ESC_TITLE
;
877 } else if(term
.esc
& ESC_TITLE
) {
878 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
880 term
.title
[term
.titlelen
] = '\0';
881 XStoreName(xw
.dis
, xw
.win
, term
.title
);
883 term
.title
[term
.titlelen
++] = c
;
885 } else if(term
.esc
& ESC_ALTCHARSET
) {
887 case '0': /* Line drawing crap */
888 term
.c
.attr
.mode
|= ATTR_GFX
;
890 case 'B': /* Back to regular text */
891 term
.c
.attr
.mode
&= ~ATTR_GFX
;
894 printf("esc unhandled charset: ESC ( %c\n", c
);
906 term
.esc
|= ESC_ALTCHARSET
;
909 tmoveto(term
.c
.x
, term
.c
.y
-1);
913 tmoveto(term
.c
.x
, term
.c
.y
+1);
917 tmoveto(term
.c
.x
+1, term
.c
.y
);
920 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
921 tmoveto(term
.c
.x
-1, term
.c
.y
);
924 case 'E': /* NEL -- Next line */
928 case 'M': /* RI -- Reverse index */
929 if(term
.c
.y
== term
.top
)
932 tmoveto(term
.c
.x
, term
.c
.y
-1);
935 case 'c': /* RIS -- Reset to inital state */
939 case '=': /* DECPAM */
940 term
.mode
|= MODE_APPKEYPAD
;
943 case '>': /* DECPNM */
944 term
.mode
&= ~MODE_APPKEYPAD
;
948 tcursor(CURSOR_SAVE
);
952 tcursor(CURSOR_LOAD
);
956 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
966 tmovecursor(CURSOR_LEFT
);
969 tmoveto(0, term
.c
.y
);
979 term
.esc
= ESC_START
;
983 tmovecursor(CURSOR_RIGHT
);
990 tputs(char *s
, int len
) {
991 for(; len
> 0; len
--)
996 tresize(int col
, int row
) {
999 int minrow
= MIN(row
, term
.row
);
1000 int mincol
= MIN(col
, term
.col
);
1002 if(col
< 1 || row
< 1)
1005 line
= calloc(row
, sizeof(Line
));
1006 for(i
= 0 ; i
< row
; i
++)
1007 line
[i
] = calloc(col
, sizeof(Glyph
));
1009 for(i
= 0 ; i
< minrow
; i
++)
1010 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
1012 for(i
= 0; i
< term
.row
; i
++)
1016 LIMIT(term
.c
.x
, 0, col
-1);
1017 LIMIT(term
.c
.y
, 0, row
-1);
1018 LIMIT(term
.top
, 0, row
-1);
1019 LIMIT(term
.bot
, 0, row
-1);
1023 term
.col
= col
, term
.row
= row
;
1027 xgetcol(const char *s
) {
1029 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1031 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
1032 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
1033 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
1039 xclear(int x1
, int y1
, int x2
, int y2
) {
1040 XClearArea(xw
.dis
, xw
.win
,
1041 x1
* xw
.cw
, y1
* xw
.ch
,
1042 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
,
1048 int srcy
= (term
.top
+1) * xw
.ch
;
1049 int dsty
= term
.top
* xw
.ch
;
1050 int height
= (term
.bot
-term
.top
) * xw
.ch
;
1052 xcursor(CURSOR_HIDE
);
1053 XCopyArea(xw
.dis
, xw
.win
, xw
.win
, dc
.gc
, 0, srcy
, xw
.w
, height
, 0, dsty
);
1054 xclear(0, term
.bot
, term
.col
-1, term
.bot
);
1062 char *args
[] = {NULL
};
1065 xw
.dis
= XOpenDisplay(NULL
);
1066 xw
.scr
= XDefaultScreen(xw
.dis
);
1068 die("Can't open display\n");
1071 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1072 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1074 /* XXX: Assuming same size for bold font */
1075 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1076 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
+ LINESPACE
;
1079 for(i
= 0; i
< LEN(colorname
); i
++)
1080 dc
.col
[i
] = xgetcol(colorname
[i
]);
1082 term
.c
.attr
.fg
= DefaultFG
;
1083 term
.c
.attr
.bg
= DefaultBG
;
1084 term
.c
.attr
.mode
= ATTR_NULL
;
1086 xw
.h
= term
.row
* xw
.ch
;
1087 xw
.w
= term
.col
* xw
.cw
;
1088 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
1089 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1094 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1095 XMapWindow(xw
.dis
, xw
.win
);
1097 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
1098 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
1099 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
1100 shint
.height
= xw
.h
, shint
.width
= xw
.w
;
1101 shint
.flags
= PSize
| PResizeInc
;
1102 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
1103 XStoreName(xw
.dis
, xw
.win
, TNAME
);
1108 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1109 unsigned long xfg
, xbg
;
1110 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1113 if(base
.mode
& ATTR_REVERSE
)
1114 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1116 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1118 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1119 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1121 if(base
.mode
& ATTR_GFX
)
1122 for(i
= 0; i
< len
; i
++)
1125 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1126 XDrawImageString(xw
.dis
, xw
.win
, dc
.gc
, winx
, winy
, s
, len
);
1128 if(base
.mode
& ATTR_UNDERLINE
)
1129 XDrawLine(xw
.dis
, xw
.win
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1134 static int oldx
= 0;
1135 static int oldy
= 0;
1136 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1138 LIMIT(oldx
, 0, term
.col
-1);
1139 LIMIT(oldy
, 0, term
.row
-1);
1141 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1142 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1144 /* remove the old cursor */
1145 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1146 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1148 xclear(oldx
, oldy
, oldx
, oldy
);
1150 /* draw the new one */
1151 if(mode
== CURSOR_DRAW
) {
1152 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1153 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1159 /* basic drawing routines */
1161 xdrawc(int x
, int y
, Glyph g
) {
1162 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1163 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1164 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1165 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1166 XDrawImageString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1173 xclear(0, 0, term
.col
-1, term
.row
-1);
1174 for(y
= 0; y
< term
.row
; y
++)
1175 for(x
= 0; x
< term
.col
; x
++)
1176 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1177 xdrawc(x
, y
, term
.line
[y
][x
]);
1180 xcursor(CURSOR_DRAW
);
1185 draw(int redraw_all
) {
1188 char buf
[DRAW_BUF_SIZ
];
1190 /* XXX: optimize with GLYPH_DIRTY hint */
1191 for(y
= 0; y
< term
.row
; y
++) {
1192 base
= term
.line
[y
][0];
1194 for(x
= 0; x
< term
.col
; x
++) {
1195 new = term
.line
[y
][x
];
1196 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1199 xdraws(buf
, base
, ox
, y
, i
);
1206 xdraws(buf
, base
, ox
, y
, i
);
1208 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1212 expose(XEvent
*ev
) {
1213 draw(SCREEN_REDRAW
);
1219 for(i
= 0; i
< LEN(key
); i
++)
1221 return (char*)key
[i
].s
;
1226 kpress(XEvent
*ev
) {
1227 XKeyEvent
*e
= &ev
->xkey
;
1235 meta
= e
->state
& Mod1Mask
;
1236 shift
= e
->state
& ShiftMask
;
1237 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1239 if(customkey
= kmap(ksym
))
1240 ttywrite(customkey
, strlen(customkey
));
1242 buf
[sizeof(buf
)-1] = '\0';
1243 if(meta
&& len
== 1)
1244 ttywrite("\033", 1);
1252 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1257 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1260 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1268 col
= e
->xconfigure
.width
/ xw
.cw
;
1269 row
= e
->xconfigure
.height
/ xw
.ch
;
1271 if(term
.col
!= col
|| term
.row
!= row
) {
1273 ttyresize(col
, row
);
1274 xw
.w
= e
->xconfigure
.width
;
1275 xw
.h
= e
->xconfigure
.height
;
1276 draw(SCREEN_REDRAW
);
1284 int xfd
= XConnectionNumber(xw
.dis
);
1287 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1288 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* fix resize bug in wmii (?) */
1292 FD_SET(cmdfd
, &rfd
);
1294 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1297 die("select failed: %s\n", SERRNO
);
1299 if(FD_ISSET(cmdfd
, &rfd
)) {
1301 draw(SCREEN_UPDATE
);
1303 while(XPending(xw
.dis
)) {
1304 XNextEvent(xw
.dis
, &ev
);
1305 if(handler
[ev
.type
])
1306 (handler
[ev
.type
])(&ev
);
1312 main(int argc
, char *argv
[]) {
1313 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1314 die("st-" VERSION
", © 2009 st engineers\n");
1316 die("usage: st [-v]\n");
1317 setlocale(LC_CTYPE
, "");