Xinqi Bao's Git
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 */
96 int w
; /* window width */
97 int h
; /* window height */
98 int bufw
; /* pixmap width */
99 int bufh
; /* pixmap height */
100 int ch
; /* char height */
101 int cw
; /* char width */
111 /* Drawing Context */
113 unsigned long col
[LEN(colorname
)];
119 static void die(const char *errstr
, ...);
120 static void draw(int);
121 static void execsh(void);
122 static void sigchld(int);
123 static void run(void);
125 static void csidump(void);
126 static void csihandle(void);
127 static void csiparse(void);
128 static void csireset(void);
130 static void tclearregion(int, int, int, int);
131 static void tcursor(int);
132 static void tmovecursor(int);
133 static void tdeletechar(int);
134 static void tdeleteline(int);
135 static void tinsertblank(int);
136 static void tinsertblankline(int);
137 static void tmoveto(int, int);
138 static void tnew(int, int);
139 static void tnewline(void);
140 static void tputc(char);
141 static void tputs(char*, int);
142 static void treset(void);
143 static void tresize(int, int);
144 static void tscroll(void);
145 static void tscrollup(int);
146 static void tscrolldown(int);
147 static void tsetattr(int*, int);
148 static void tsetchar(char);
149 static void tsetscroll(int, int);
151 static void ttynew(void);
152 static void ttyread(void);
153 static void ttyresize(int, int);
154 static void ttywrite(const char *, size_t);
156 static unsigned long xgetcol(const char *);
157 static void xclear(int, int, int, int);
158 static void xcursor(int);
159 static void xinit(void);
161 static void expose(XEvent
*);
162 static char* kmap(KeySym
);
163 static void kpress(XEvent
*);
164 static void resize(XEvent
*);
166 static void (*handler
[LASTEvent
])(XEvent
*) = {
169 [ConfigureNotify
] = resize
176 static CSIEscape escseq
;
187 for(row
= 0; row
< term
.row
; row
++) {
188 for(col
= 0; col
< term
.col
; col
++) {
189 if(col
== term
.c
.x
&& row
== term
.c
.y
)
192 c
= term
.line
[row
][col
];
193 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
202 die(const char *errstr
, ...) {
205 va_start(ap
, errstr
);
206 vfprintf(stderr
, errstr
, ap
);
213 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
214 DEFAULT(args
[0], "/bin/sh"); /* default shell if getenv() failed */
215 putenv("TERM=" TNAME
);
216 execvp(args
[0], args
);
220 xbell(void) { /* visual bell */
221 XRectangle r
= { BORDER
, BORDER
, xw
.bufw
, xw
.bufh
};
222 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
223 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
231 if(waitpid(pid
, &stat
, 0) < 0)
232 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
234 exit(WEXITSTATUS(stat
));
244 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
245 die("openpt failed: %s\n", SERRNO
);
247 die("grandpt failed: %s\n", SERRNO
);
249 die("unlockpt failed: %s\n", SERRNO
);
250 if(!(pts
= ptsname(m
)))
251 die("ptsname failed: %s\n", SERRNO
);
252 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
253 die("Couldn't open slave: %s\n", SERRNO
);
254 fcntl(s
, F_SETFL
, O_NDELAY
);
255 switch(pid
= fork()) {
257 die("fork failed\n");
260 setsid(); /* create a new process group */
261 dup2(s
, STDIN_FILENO
);
262 dup2(s
, STDOUT_FILENO
);
263 dup2(s
, STDERR_FILENO
);
264 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
265 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
271 signal(SIGCHLD
, sigchld
);
278 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
280 fprintf(stderr
, "\n");
285 char buf
[BUFSIZ
] = {0};
288 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
289 die("Couldn't read from shell: %s\n", SERRNO
);
295 ttywrite(const char *s
, size_t n
) {
296 if(write(cmdfd
, s
, n
) == -1)
297 die("write error on tty: %s\n", SERRNO
);
301 ttyresize(int x
, int y
) {
306 w
.ws_xpixel
= w
.ws_ypixel
= 0;
307 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
308 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
315 if(mode
== CURSOR_SAVE
)
317 else if(mode
== CURSOR_LOAD
)
318 term
.c
= c
, tmoveto(c
.x
, c
.y
);
323 term
.c
.attr
.mode
= ATTR_NULL
;
324 term
.c
.attr
.fg
= DefaultFG
;
325 term
.c
.attr
.bg
= DefaultBG
;
326 term
.c
.x
= term
.c
.y
= 0;
328 term
.top
= 0, term
.bot
= term
.row
- 1;
329 term
.mode
= MODE_WRAP
;
330 tclearregion(0, 0, term
.col
-1, term
.row
-1);
334 tnew(int col
, int row
) { /* screen size */
335 term
.row
= row
, term
.col
= col
;
336 term
.top
= 0, term
.bot
= term
.row
- 1;
338 term
.mode
= MODE_WRAP
;
340 term
.c
.attr
.mode
= ATTR_NULL
;
341 term
.c
.attr
.fg
= DefaultFG
;
342 term
.c
.attr
.bg
= DefaultBG
;
343 term
.c
.x
= term
.c
.y
= 0;
345 /* allocate screen */
346 term
.line
= calloc(term
.row
, sizeof(Line
));
347 for(row
= 0 ; row
< term
.row
; row
++)
348 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
351 /* TODO: Replace with scrollup/scolldown */
354 Line temp
= term
.line
[term
.top
];
357 for(i
= term
.top
; i
< term
.bot
; i
++)
358 term
.line
[i
] = term
.line
[i
+1];
359 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
360 term
.line
[term
.bot
] = temp
;
364 tscrolldown (int n
) {
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 for(i
= 0; i
< n
; i
++)
387 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
389 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
391 term
.line
[i
] = term
.line
[i
+n
];
392 term
.line
[i
+n
] = temp
;
398 int y
= term
.c
.y
+ 1;
400 tscroll(), y
= term
.bot
;
407 char *p
= escseq
.buf
;
411 escseq
.priv
= 1, p
++;
413 while(p
< escseq
.buf
+escseq
.len
) {
415 escseq
.arg
[escseq
.narg
] *= 10;
416 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
418 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
429 tmoveto(int x
, int y
) {
430 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
431 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
435 tmovecursor(int dir
) {
436 int xf
= term
.c
.x
, yf
= term
.c
.y
;
447 if(term
.mode
& MODE_WRAP
&& xf
< 0) {
448 xf
= term
.col
-1, yf
--;
450 yf
= term
.top
, xf
= 0;
455 if(term
.mode
& MODE_WRAP
&& xf
>= term
.col
) {
458 yf
= term
.bot
, tscroll();
467 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
468 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
469 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
473 tclearregion(int x1
, int y1
, int x2
, int y2
) {
477 temp
= x1
, x1
= x2
, x2
= temp
;
479 temp
= y1
, y1
= y2
, y2
= temp
;
481 LIMIT(x1
, 0, term
.col
-1);
482 LIMIT(x2
, 0, term
.col
-1);
483 LIMIT(y1
, 0, term
.row
-1);
484 LIMIT(y2
, 0, term
.row
-1);
486 for(y
= y1
; y
<= y2
; y
++)
487 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
492 int src
= term
.c
.x
+ n
;
494 int size
= term
.col
- src
;
496 if(src
>= term
.col
) {
497 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
500 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
501 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
505 tinsertblank(int n
) {
508 int size
= term
.col
- n
- src
;
510 if(dst
>= term
.col
) {
511 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
514 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
515 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
519 tinsertblankline(int n
) {
524 if(term
.c
.y
> term
.bot
)
526 else if(term
.c
.y
< term
.top
)
528 if(term
.c
.y
+ n
>= bot
) {
529 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
532 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
533 /* swap deleted line <-> blanked line */
534 blank
= term
.line
[i
];
535 term
.line
[i
] = term
.line
[i
-n
];
536 term
.line
[i
-n
] = blank
;
538 memset(blank
, 0, term
.col
* sizeof(Glyph
));
548 if(term
.c
.y
> term
.bot
)
550 else if(term
.c
.y
< term
.top
)
552 if(term
.c
.y
+ n
>= bot
) {
553 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
556 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
557 /* swap deleted line <-> blanked line */
558 blank
= term
.line
[i
];
559 term
.line
[i
] = term
.line
[i
+n
];
560 term
.line
[i
+n
] = blank
;
562 memset(blank
, 0, term
.col
* sizeof(Glyph
));
567 tsetattr(int *attr
, int l
) {
570 for(i
= 0; i
< l
; i
++) {
573 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
574 term
.c
.attr
.fg
= DefaultFG
;
575 term
.c
.attr
.bg
= DefaultBG
;
578 term
.c
.attr
.mode
|= ATTR_BOLD
;
581 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
584 term
.c
.attr
.mode
|= ATTR_REVERSE
;
587 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
590 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
593 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
596 term
.c
.attr
.fg
= DefaultFG
;
599 term
.c
.attr
.fg
= DefaultBG
;
602 if(BETWEEN(attr
[i
], 30, 37))
603 term
.c
.attr
.fg
= attr
[i
] - 30;
604 else if(BETWEEN(attr
[i
], 40, 47))
605 term
.c
.attr
.bg
= attr
[i
] - 40;
607 fprintf(stderr
, "erresc: gfx attr %d unkown\n", attr
[i
]);
614 tsetscroll(int t
, int b
) {
617 LIMIT(t
, 0, term
.row
-1);
618 LIMIT(b
, 0, term
.row
-1);
630 switch(escseq
.mode
) {
633 printf("erresc: unknown csi ");
637 case '@': /* ICH -- Insert <n> blank char */
638 DEFAULT(escseq
.arg
[0], 1);
639 tinsertblank(escseq
.arg
[0]);
641 case 'A': /* CUU -- Cursor <n> Up */
643 DEFAULT(escseq
.arg
[0], 1);
644 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
646 case 'B': /* CUD -- Cursor <n> Down */
647 DEFAULT(escseq
.arg
[0], 1);
648 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
650 case 'C': /* CUF -- Cursor <n> Forward */
652 DEFAULT(escseq
.arg
[0], 1);
653 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
655 case 'D': /* CUB -- Cursor <n> Backward */
656 DEFAULT(escseq
.arg
[0], 1);
657 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
659 case 'E': /* CNL -- Cursor <n> Down and first col */
660 DEFAULT(escseq
.arg
[0], 1);
661 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
663 case 'F': /* CPL -- Cursor <n> Up and first col */
664 DEFAULT(escseq
.arg
[0], 1);
665 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
667 case 'G': /* CHA -- Move to <col> */
668 case '`': /* XXX: HPA -- same? */
669 DEFAULT(escseq
.arg
[0], 1);
670 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
672 case 'H': /* CUP -- Move to <row> <col> */
673 case 'f': /* XXX: HVP -- same? */
674 DEFAULT(escseq
.arg
[0], 1);
675 DEFAULT(escseq
.arg
[1], 1);
676 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
678 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
679 case 'J': /* ED -- Clear screen */
680 switch(escseq
.arg
[0]) {
682 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
685 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
688 tclearregion(0, 0, term
.col
-1, term
.row
-1);
690 case 3: /* XXX: erase saved lines (xterm) */
695 case 'K': /* EL -- Clear line */
696 switch(escseq
.arg
[0]) {
698 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
701 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
704 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
708 case 'S': /* SU -- Scroll <n> line up */
709 DEFAULT(escseq
.arg
[0], 1);
710 tscrollup(escseq
.arg
[0]);
712 case 'T': /* SD -- Scroll <n> line down */
713 DEFAULT(escseq
.arg
[0], 1);
714 tscrolldown(escseq
.arg
[0]);
716 case 'L': /* IL -- Insert <n> blank lines */
717 DEFAULT(escseq
.arg
[0], 1);
718 tinsertblankline(escseq
.arg
[0]);
720 case 'l': /* RM -- Reset Mode */
722 switch(escseq
.arg
[0]) {
724 term
.mode
&= ~MODE_APPKEYPAD
;
727 term
.mode
&= ~MODE_WRAP
;
729 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
734 case 1048: /* XXX: no alt. screen to erase/save */
736 tcursor(CURSOR_LOAD
);
737 tclearregion(0, 0, term
.col
-1, term
.row
-1);
743 switch(escseq
.arg
[0]) {
745 term
.mode
&= ~MODE_INSERT
;
752 case 'M': /* DL -- Delete <n> lines */
753 DEFAULT(escseq
.arg
[0], 1);
754 tdeleteline(escseq
.arg
[0]);
756 case 'X': /* ECH -- Erase <n> char */
757 DEFAULT(escseq
.arg
[0], 1);
758 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
760 case 'P': /* DCH -- Delete <n> char */
761 DEFAULT(escseq
.arg
[0], 1);
762 tdeletechar(escseq
.arg
[0]);
764 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
765 case 'd': /* VPA -- Move to <row> */
766 DEFAULT(escseq
.arg
[0], 1);
767 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
769 case 'h': /* SM -- Set terminal mode */
771 switch(escseq
.arg
[0]) {
773 term
.mode
|= MODE_APPKEYPAD
;
776 term
.mode
|= MODE_WRAP
;
778 case 12: /* att610 -- Start blinking cursor (IGNORED) */
784 case 1049: /* XXX: no alt. screen to erase/save */
785 tcursor(CURSOR_SAVE
);
786 tclearregion(0, 0, term
.col
-1, term
.row
-1);
788 default: goto unknown
;
791 switch(escseq
.arg
[0]) {
793 term
.mode
|= MODE_INSERT
;
795 default: goto unknown
;
799 case 'm': /* SGR -- Terminal attribute (color) */
800 tsetattr(escseq
.arg
, escseq
.narg
);
802 case 'r': /* DECSTBM -- Set Scrolling Region */
806 DEFAULT(escseq
.arg
[0], 1);
807 DEFAULT(escseq
.arg
[1], term
.row
);
808 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
811 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
812 tcursor(CURSOR_SAVE
);
814 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
815 tcursor(CURSOR_LOAD
);
823 printf("ESC [ %s", escseq
.priv
? "? " : "");
825 for(i
= 0; i
< escseq
.narg
; i
++)
826 printf("%d ", escseq
.arg
[i
]);
828 putchar(escseq
.mode
);
834 memset(&escseq
, 0, sizeof(escseq
));
839 int space
= TAB
- term
.c
.x
% TAB
;
841 if(term
.c
.x
+ space
>= term
.col
)
844 for(; space
> 0; space
--)
845 tmovecursor(CURSOR_RIGHT
);
850 if(term
.esc
& ESC_START
) {
851 if(term
.esc
& ESC_CSI
) {
852 escseq
.buf
[escseq
.len
++] = c
;
853 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
855 csiparse(), csihandle();
857 } else if(term
.esc
& ESC_OSC
) {
860 term
.esc
= ESC_START
| ESC_TITLE
;
862 } else if(term
.esc
& ESC_TITLE
) {
863 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
865 term
.title
[term
.titlelen
] = '\0';
866 XStoreName(xw
.dis
, xw
.win
, term
.title
);
868 term
.title
[term
.titlelen
++] = c
;
870 } else if(term
.esc
& ESC_ALTCHARSET
) {
872 case '0': /* Line drawing crap */
873 term
.c
.attr
.mode
|= ATTR_GFX
;
875 case 'B': /* Back to regular text */
876 term
.c
.attr
.mode
&= ~ATTR_GFX
;
879 printf("esc unhandled charset: ESC ( %c\n", c
);
891 term
.esc
|= ESC_ALTCHARSET
;
894 tmoveto(term
.c
.x
, term
.c
.y
-1);
898 tmoveto(term
.c
.x
, term
.c
.y
+1);
902 tmoveto(term
.c
.x
+1, term
.c
.y
);
905 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
906 tmoveto(term
.c
.x
-1, term
.c
.y
);
909 case 'E': /* NEL -- Next line */
913 case 'M': /* RI -- Reverse index */
914 if(term
.c
.y
== term
.top
)
917 tmoveto(term
.c
.x
, term
.c
.y
-1);
920 case 'c': /* RIS -- Reset to inital state */
924 case '=': /* DECPAM */
925 term
.mode
|= MODE_APPKEYPAD
;
928 case '>': /* DECPNM */
929 term
.mode
&= ~MODE_APPKEYPAD
;
933 tcursor(CURSOR_SAVE
);
937 tcursor(CURSOR_LOAD
);
941 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
951 tmovecursor(CURSOR_LEFT
);
954 tmoveto(0, term
.c
.y
);
964 term
.esc
= ESC_START
;
968 tmovecursor(CURSOR_RIGHT
);
975 tputs(char *s
, int len
) {
976 for(; len
> 0; len
--)
981 tresize(int col
, int row
) {
984 int minrow
= MIN(row
, term
.row
);
985 int mincol
= MIN(col
, term
.col
);
987 if(col
< 1 || row
< 1)
990 line
= calloc(row
, sizeof(Line
));
991 for(i
= 0 ; i
< row
; i
++)
992 line
[i
] = calloc(col
, sizeof(Glyph
));
994 for(i
= 0 ; i
< minrow
; i
++)
995 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
997 for(i
= 0; i
< term
.row
; i
++)
1001 LIMIT(term
.c
.x
, 0, col
-1);
1002 LIMIT(term
.c
.y
, 0, row
-1);
1003 LIMIT(term
.top
, 0, row
-1);
1004 LIMIT(term
.bot
, 0, row
-1);
1008 term
.col
= col
, term
.row
= row
;
1012 xgetcol(const char *s
) {
1014 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1016 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
1017 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
1018 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
1024 xclear(int x1
, int y1
, int x2
, int y2
) {
1025 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1026 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1027 x1
* xw
.cw
, y1
* xw
.ch
,
1028 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1034 XClassHint chint
= {TNAME
, TNAME
};
1035 XWMHints wmhint
= {.flags
= InputHint
, .input
= 1};
1036 XSizeHints shint
= {
1037 .flags
= PSize
| PResizeInc
,
1038 .height
= xw
.h
, /* XXX: doesn't seem to work, see run() */
1040 .height_inc
= xw
.ch
,
1043 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &shint
, &wmhint
, &chint
);
1050 xw
.dis
= XOpenDisplay(NULL
);
1051 xw
.scr
= XDefaultScreen(xw
.dis
);
1053 die("Can't open display\n");
1056 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1057 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1059 /* XXX: Assuming same size for bold font */
1060 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1061 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1064 for(i
= 0; i
< LEN(colorname
); i
++)
1065 dc
.col
[i
] = xgetcol(colorname
[i
]);
1067 term
.c
.attr
.fg
= DefaultFG
;
1068 term
.c
.attr
.bg
= DefaultBG
;
1069 term
.c
.attr
.mode
= ATTR_NULL
;
1071 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1072 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1073 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1077 xw
.bufw
= xw
.w
- 2*BORDER
;
1078 xw
.bufh
= xw
.h
- 2*BORDER
;
1079 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1081 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1082 XMapWindow(xw
.dis
, xw
.win
);
1084 XStoreName(xw
.dis
, xw
.win
, TNAME
);
1089 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1090 unsigned long xfg
, xbg
;
1091 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1094 if(base
.mode
& ATTR_REVERSE
)
1095 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1097 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1099 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1100 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1102 if(base
.mode
& ATTR_GFX
)
1103 for(i
= 0; i
< len
; i
++)
1106 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1107 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1109 if(base
.mode
& ATTR_UNDERLINE
)
1110 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1115 static int oldx
= 0;
1116 static int oldy
= 0;
1117 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1119 LIMIT(oldx
, 0, term
.col
-1);
1120 LIMIT(oldy
, 0, term
.row
-1);
1122 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1123 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1125 /* remove the old cursor */
1126 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1127 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1129 xclear(oldx
, oldy
, oldx
, oldy
);
1131 /* draw the new one */
1132 if(mode
== CURSOR_DRAW
) {
1133 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1134 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1140 /* basic drawing routines */
1142 xdrawc(int x
, int y
, Glyph g
) {
1143 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1144 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1145 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1146 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1147 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1154 xclear(0, 0, term
.col
-1, term
.row
-1);
1155 for(y
= 0; y
< term
.row
; y
++)
1156 for(x
= 0; x
< term
.col
; x
++)
1157 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1158 xdrawc(x
, y
, term
.line
[y
][x
]);
1161 xcursor(CURSOR_DRAW
);
1162 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1168 draw(int redraw_all
) {
1171 char buf
[DRAW_BUF_SIZ
];
1173 for(y
= 0; y
< term
.row
; y
++) {
1174 base
= term
.line
[y
][0];
1176 for(x
= 0; x
< term
.col
; x
++) {
1177 new = term
.line
[y
][x
];
1178 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1181 xdraws(buf
, base
, ox
, y
, i
);
1188 xdraws(buf
, base
, ox
, y
, i
);
1190 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1191 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1196 expose(XEvent
*ev
) {
1197 draw(SCREEN_REDRAW
);
1203 for(i
= 0; i
< LEN(key
); i
++)
1205 return (char*)key
[i
].s
;
1210 kpress(XEvent
*ev
) {
1211 XKeyEvent
*e
= &ev
->xkey
;
1219 meta
= e
->state
& Mod1Mask
;
1220 shift
= e
->state
& ShiftMask
;
1221 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1223 if(customkey
= kmap(ksym
))
1224 ttywrite(customkey
, strlen(customkey
));
1226 buf
[sizeof(buf
)-1] = '\0';
1227 if(meta
&& len
== 1)
1228 ttywrite("\033", 1);
1236 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1241 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1244 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1253 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1256 xw
.w
= e
->xconfigure
.width
;
1257 xw
.h
= e
->xconfigure
.height
;
1258 xw
.bufw
= xw
.w
- 2*BORDER
;
1259 xw
.bufh
= xw
.h
- 2*BORDER
;
1260 col
= xw
.bufw
/ xw
.cw
;
1261 row
= xw
.bufh
/ xw
.ch
;
1263 ttyresize(col
, row
);
1264 XFreePixmap(xw
.dis
, xw
.buf
);
1265 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1266 draw(SCREEN_REDRAW
);
1273 int xfd
= XConnectionNumber(xw
.dis
);
1276 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1277 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1281 FD_SET(cmdfd
, &rfd
);
1283 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1286 die("select failed: %s\n", SERRNO
);
1288 if(FD_ISSET(cmdfd
, &rfd
)) {
1290 draw(SCREEN_UPDATE
);
1292 while(XPending(xw
.dis
)) {
1293 XNextEvent(xw
.dis
, &ev
);
1294 if(handler
[ev
.type
])
1295 (handler
[ev
.type
])(&ev
);
1301 main(int argc
, char *argv
[]) {
1302 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1303 die("st-" VERSION
", © 2009 st engineers\n");
1305 die("usage: st [-v]\n");
1306 setlocale(LC_CTYPE
, "");