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 */
65 /* CSI Escape sequence structs */
66 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
68 char buf
[ESC_BUF_SIZ
]; /* raw string */
69 int len
; /* raw string length */
72 int narg
; /* nb of args */
76 /* Internal representation of the screen */
80 Line
* line
; /* screen */
81 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
)];
115 static void die(const char *errstr
, ...);
116 static void draw(int);
117 static void execsh(void);
118 static void sigchld(int);
119 static void run(void);
121 static void csidump(void);
122 static void csihandle(void);
123 static void csiparse(void);
124 static void csireset(void);
126 static void tclearregion(int, int, int, int);
127 static void tcursor(int);
128 static void tmovecursor(int);
129 static void tdeletechar(int);
130 static void tdeleteline(int);
131 static void tinsertblank(int);
132 static void tinsertblankline(int);
133 static void tmoveto(int, int);
134 static void tnew(int, int);
135 static void tnewline(void);
136 static void tputc(char);
137 static void tputs(char*, int);
138 static void tresize(int, int);
139 static void tscroll(void);
140 static void tsetattr(int*, int);
141 static void tsetchar(char);
142 static void tsetscroll(int, int);
144 static void ttynew(void);
145 static void ttyread(void);
146 static void ttyresize(int, int);
147 static void ttywrite(const char *, size_t);
149 static unsigned long xgetcol(const char *);
150 static void xclear(int, int, int, int);
151 static void xcursor(int);
152 static void xdrawc(int, int, Glyph
);
153 static void xinit(void);
154 static void xscroll(void);
156 static void expose(XEvent
*);
157 static char * kmap(KeySym
);
158 static void kpress(XEvent
*);
159 static void resize(XEvent
*);
161 static void (*handler
[LASTEvent
])(XEvent
*) = {
164 [ConfigureNotify
] = resize
171 static CSIEscape escseq
;
182 for(row
= 0; row
< term
.row
; row
++) {
183 for(col
= 0; col
< term
.col
; col
++) {
184 if(col
== term
.c
.x
&& row
== term
.c
.y
)
187 c
= term
.line
[row
][col
];
188 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
197 die(const char *errstr
, ...) {
200 va_start(ap
, errstr
);
201 vfprintf(stderr
, errstr
, ap
);
208 char *args
[3] = {SHELL
, "-i", NULL
};
209 putenv("TERM=" TNAME
);
214 xbell(void) { /* visual bell */
215 XRectangle r
= { 0, 0, xw
.w
, xw
.h
};
216 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
217 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
225 if(waitpid(pid
, &stat
, 0) < 0)
226 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
228 exit(WEXITSTATUS(stat
));
238 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
239 die("openpt failed: %s\n", SERRNO
);
241 die("grandpt failed: %s\n", SERRNO
);
243 die("unlockpt failed: %s\n", SERRNO
);
244 if(!(pts
= ptsname(m
)))
245 die("ptsname failed: %s\n", SERRNO
);
246 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
247 die("Couldn't open slave: %s\n", SERRNO
);
248 fcntl(s
, F_SETFL
, O_NDELAY
);
249 switch(pid
= fork()) {
251 die("fork failed\n");
254 setsid(); /* create a new process group */
255 dup2(s
, STDIN_FILENO
);
256 dup2(s
, STDOUT_FILENO
);
257 dup2(s
, STDERR_FILENO
);
258 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
259 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
265 signal(SIGCHLD
, sigchld
);
272 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
274 fprintf(stderr
, "\n");
279 char buf
[BUFSIZ
] = {0};
282 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
283 die("Couldn't read from shell: %s\n", SERRNO
);
289 ttywrite(const char *s
, size_t n
) {
290 if(write(cmdfd
, s
, n
) == -1)
291 die("write error on tty: %s\n", SERRNO
);
295 ttyresize(int x
, int y
) {
300 w
.ws_xpixel
= w
.ws_ypixel
= 0;
301 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
302 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
309 if(mode
== CURSOR_SAVE
)
311 else if(mode
== CURSOR_LOAD
)
312 term
.c
= c
, tmoveto(c
.x
, c
.y
);
316 tnew(int col
, int row
) { /* screen size */
317 term
.row
= row
, term
.col
= col
;
318 term
.top
= 0, term
.bot
= term
.row
- 1;
320 term
.mode
= MODE_WRAP
;
322 term
.c
.attr
.mode
= ATTR_NULL
;
323 term
.c
.attr
.fg
= DefaultFG
;
324 term
.c
.attr
.bg
= DefaultBG
;
325 term
.c
.x
= term
.c
.y
= 0;
327 /* allocate screen */
328 term
.line
= calloc(term
.row
, sizeof(Line
));
329 for(row
= 0 ; row
< term
.row
; row
++)
330 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
335 Line temp
= term
.line
[term
.top
];
337 /* X stuff _before_ the line swapping (results in wrong line index) */
339 for(i
= term
.top
; i
< term
.bot
; i
++)
340 term
.line
[i
] = term
.line
[i
+1];
341 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
342 term
.line
[term
.bot
] = temp
;
347 int y
= term
.c
.y
+ 1;
349 tscroll(), y
= term
.bot
;
356 char *p
= escseq
.buf
;
360 escseq
.priv
= 1, p
++;
362 while(p
< escseq
.buf
+escseq
.len
) {
364 escseq
.arg
[escseq
.narg
] *= 10;
365 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
367 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
378 tmoveto(int x
, int y
) {
379 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
380 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
384 tmovecursor(int dir
) {
385 int xf
= term
.c
.x
, yf
= term
.c
.y
;
396 if(term
.mode
& MODE_WRAP
&& xf
< 0) {
397 xf
= term
.col
-1, yf
--;
399 yf
= term
.top
, xf
= 0;
404 if(term
.mode
& MODE_WRAP
&& xf
>= term
.col
) {
407 yf
= term
.bot
, tscroll();
416 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
417 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
418 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
| GLYPH_DIRTY
;
422 tclearregion(int x1
, int y1
, int x2
, int y2
) {
425 LIMIT(x1
, 0, term
.col
-1);
426 LIMIT(x2
, 0, term
.col
-1);
427 LIMIT(y1
, 0, term
.row
-1);
428 LIMIT(y2
, 0, term
.row
-1);
430 /* XXX: could be optimized */
431 for(x
= x1
; x
<= x2
; x
++)
432 for(y
= y1
; y
<= y2
; y
++)
433 memset(&term
.line
[y
][x
], 0, sizeof(Glyph
));
435 xclear(x1
, y1
, x2
, y2
);
440 int src
= term
.c
.x
+ n
;
442 int size
= term
.col
- src
;
444 if(src
>= term
.col
) {
445 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
448 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
449 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
453 tinsertblank(int n
) {
456 int size
= term
.col
- n
- src
;
458 if(dst
>= term
.col
) {
459 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
462 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
463 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
467 tsetlinestate(int n
, int state
) {
469 for(i
= 0; i
< term
.col
; i
++)
470 term
.line
[n
][i
].state
|= state
;
474 tinsertblankline(int n
) {
479 if(term
.c
.y
> term
.bot
)
481 else if(term
.c
.y
< term
.top
)
483 if(term
.c
.y
+ n
>= bot
) {
484 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
487 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
488 /* swap deleted line <-> blanked line */
489 blank
= term
.line
[i
];
490 term
.line
[i
] = term
.line
[i
-n
];
491 term
.line
[i
-n
] = blank
;
493 memset(blank
, 0, term
.col
* sizeof(Glyph
));
494 tsetlinestate(i
, GLYPH_DIRTY
);
495 tsetlinestate(i
-n
, GLYPH_DIRTY
);
505 if(term
.c
.y
> term
.bot
)
507 else if(term
.c
.y
< term
.top
)
509 if(term
.c
.y
+ n
>= bot
) {
510 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
513 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
514 /* swap deleted line <-> blanked line */
515 blank
= term
.line
[i
];
516 term
.line
[i
] = term
.line
[i
+n
];
517 term
.line
[i
+n
] = blank
;
519 memset(blank
, 0, term
.col
* sizeof(Glyph
));
520 tsetlinestate(i
, GLYPH_DIRTY
);
521 tsetlinestate(i
-n
, GLYPH_DIRTY
);
526 tsetattr(int *attr
, int l
) {
529 for(i
= 0; i
< l
; i
++) {
532 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
533 term
.c
.attr
.fg
= DefaultFG
;
534 term
.c
.attr
.bg
= DefaultBG
;
537 term
.c
.attr
.mode
|= ATTR_BOLD
;
540 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
543 term
.c
.attr
.mode
|= ATTR_REVERSE
;
546 term
.c
.hidden
= CURSOR_HIDE
;
549 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
552 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
555 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
558 term
.c
.attr
.fg
= DefaultFG
;
561 term
.c
.attr
.fg
= DefaultBG
;
564 if(BETWEEN(attr
[i
], 30, 37))
565 term
.c
.attr
.fg
= attr
[i
] - 30;
566 else if(BETWEEN(attr
[i
], 40, 47))
567 term
.c
.attr
.bg
= attr
[i
] - 40;
574 tsetscroll(int t
, int b
) {
577 LIMIT(t
, 0, term
.row
-1);
578 LIMIT(b
, 0, term
.row
-1);
590 switch(escseq
.mode
) {
593 printf("erresc: unknown sequence -- ");
597 case '@': /* ICH -- Insert <n> blank char */
598 DEFAULT(escseq
.arg
[0], 1);
599 tinsertblank(escseq
.arg
[0]);
601 case 'A': /* CUU -- Cursor <n> Up */
603 DEFAULT(escseq
.arg
[0], 1);
604 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
606 case 'B': /* CUD -- Cursor <n> Down */
607 DEFAULT(escseq
.arg
[0], 1);
608 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
610 case 'C': /* CUF -- Cursor <n> Forward */
612 DEFAULT(escseq
.arg
[0], 1);
613 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
615 case 'D': /* CUB -- Cursor <n> Backward */
616 DEFAULT(escseq
.arg
[0], 1);
617 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
619 case 'E': /* CNL -- Cursor <n> Down and first col */
620 DEFAULT(escseq
.arg
[0], 1);
621 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
623 case 'F': /* CPL -- Cursor <n> Up and first col */
624 DEFAULT(escseq
.arg
[0], 1);
625 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
627 case 'G': /* CHA -- Move to <col> */
628 case '`': /* XXX: HPA -- same? */
629 DEFAULT(escseq
.arg
[0], 1);
630 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
632 case 'H': /* CUP -- Move to <row> <col> */
633 case 'f': /* XXX: HVP -- same? */
634 DEFAULT(escseq
.arg
[0], 1);
635 DEFAULT(escseq
.arg
[1], 1);
636 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
638 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
639 case 'J': /* ED -- Clear screen */
640 switch(escseq
.arg
[0]) {
642 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
645 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
648 tclearregion(0, 0, term
.col
-1, term
.row
-1);
650 case 3: /* XXX: erase saved lines (xterm) */
655 case 'K': /* EL -- Clear line */
656 switch(escseq
.arg
[0]) {
658 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
661 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
664 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
668 case 'S': /* XXX: SU -- Scroll <n> line up (faked) */
669 case 'L': /* IL -- Insert <n> blank lines */
670 DEFAULT(escseq
.arg
[0], 1);
671 tinsertblankline(escseq
.arg
[0]);
673 case 'l': /* RM -- Reset Mode */
675 switch(escseq
.arg
[0]) {
677 term
.mode
&= ~MODE_APPKEYPAD
;
680 term
.mode
&= ~MODE_WRAP
;
682 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
687 case 1048: /* XXX: no alt. screen to erase/save */
689 tcursor(CURSOR_LOAD
);
690 tclearregion(0, 0, term
.col
-1, term
.row
-1);
696 switch(escseq
.arg
[0]) {
698 term
.mode
&= ~MODE_INSERT
;
705 case 'M': /* DL -- Delete <n> lines */
706 DEFAULT(escseq
.arg
[0], 1);
707 tdeleteline(escseq
.arg
[0]);
709 case 'X': /* ECH -- Erase <n> char */
710 DEFAULT(escseq
.arg
[0], 1);
711 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
713 case 'P': /* DCH -- Delete <n> char */
714 DEFAULT(escseq
.arg
[0], 1);
715 tdeletechar(escseq
.arg
[0]);
717 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
718 case 'd': /* VPA -- Move to <row> */
719 DEFAULT(escseq
.arg
[0], 1);
720 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
722 case 'h': /* SM -- Set terminal mode */
724 switch(escseq
.arg
[0]) {
726 term
.mode
|= MODE_APPKEYPAD
;
729 term
.mode
|= MODE_WRAP
;
731 case 12: /* att610 -- Start blinking cursor (IGNORED) */
737 case 1049: /* XXX: no alt. screen to erase/save */
738 tcursor(CURSOR_SAVE
);
739 tclearregion(0, 0, term
.col
-1, term
.row
-1);
741 default: goto unknown
;
744 switch(escseq
.arg
[0]) {
746 term
.mode
|= MODE_INSERT
;
748 default: goto unknown
;
752 case 'm': /* SGR -- Terminal attribute (color) */
753 tsetattr(escseq
.arg
, escseq
.narg
);
755 case 'r': /* DECSTBM -- Set Scrolling Region */
759 DEFAULT(escseq
.arg
[0], 1);
760 DEFAULT(escseq
.arg
[1], term
.row
);
761 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
764 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
765 tcursor(CURSOR_SAVE
);
767 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
768 tcursor(CURSOR_LOAD
);
776 printf("ESC [ %s", escseq
.priv
? "? " : "");
778 for(i
= 0; i
< escseq
.narg
; i
++)
779 printf("%d ", escseq
.arg
[i
]);
781 putchar(escseq
.mode
);
787 memset(&escseq
, 0, sizeof(escseq
));
792 int space
= TAB
- term
.c
.x
% TAB
;
794 if(term
.c
.x
+ space
>= term
.col
)
797 for(; space
> 0; space
--)
798 tmovecursor(CURSOR_RIGHT
);
804 if(term
.esc
& ESC_START
) {
805 if(term
.esc
& ESC_CSI
) {
806 escseq
.buf
[escseq
.len
++] = c
;
807 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
809 csiparse(), csihandle();
811 } else if(term
.esc
& ESC_OSC
) {
814 term
.esc
= ESC_START
| ESC_TITLE
;
816 } else if(term
.esc
& ESC_TITLE
) {
817 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
819 term
.title
[term
.titlelen
] = '\0';
820 XStoreName(xw
.dis
, xw
.win
, term
.title
);
822 term
.title
[term
.titlelen
++] = c
;
824 } else if(term
.esc
& ESC_ALTCHARSET
) {
826 case '0': /* Line drawing crap */
827 term
.c
.attr
.mode
|= ATTR_GFX
;
829 case 'B': /* Back to regular text */
830 term
.c
.attr
.mode
&= ~ATTR_GFX
;
833 printf("esc unhandled charset: ESC ( %c\n", c
);
845 term
.esc
|= ESC_ALTCHARSET
;
848 tmoveto(term
.c
.x
, term
.c
.y
-1);
852 tmoveto(term
.c
.x
, term
.c
.y
+1);
856 tmoveto(term
.c
.x
+1, term
.c
.y
);
860 tmoveto(term
.c
.x
-1, term
.c
.y
);
863 case 'M': /* RI -- Reverse index */
864 if(term
.c
.y
== term
.top
)
867 tmoveto(term
.c
.x
, term
.c
.y
-1);
870 case '=': /* DECPAM */
871 term
.mode
|= MODE_APPKEYPAD
;
874 case '>': /* DECPNM */
875 term
.mode
&= ~MODE_APPKEYPAD
;
879 tcursor(CURSOR_SAVE
);
883 tcursor(CURSOR_LOAD
);
887 fprintf(stderr
, "erresc: unknown sequence ESC %02X '%c'\n", c
, isprint(c
)?c
:'.');
897 tmovecursor(CURSOR_LEFT
);
900 tmoveto(0, term
.c
.y
);
910 term
.esc
= ESC_START
;
914 tmovecursor(CURSOR_RIGHT
);
921 tputs(char *s
, int len
) {
922 for(; len
> 0; len
--)
927 tresize(int col
, int row
) {
930 int minrow
= MIN(row
, term
.row
);
931 int mincol
= MIN(col
, term
.col
);
933 if(col
< 1 || row
< 1)
936 line
= calloc(row
, sizeof(Line
));
937 for(i
= 0 ; i
< row
; i
++)
938 line
[i
] = calloc(col
, sizeof(Glyph
));
940 for(i
= 0 ; i
< minrow
; i
++)
941 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
943 for(i
= 0; i
< term
.row
; i
++)
947 LIMIT(term
.c
.x
, 0, col
-1);
948 LIMIT(term
.c
.y
, 0, row
-1);
949 LIMIT(term
.top
, 0, row
-1);
950 LIMIT(term
.bot
, 0, row
-1);
954 term
.col
= col
, term
.row
= row
;
958 xgetcol(const char *s
) {
960 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
962 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
963 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
964 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
970 xclear(int x1
, int y1
, int x2
, int y2
) {
971 XClearArea(xw
.dis
, xw
.win
,
972 x1
* xw
.cw
, y1
* xw
.ch
,
973 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
,
979 int srcy
= (term
.top
+1) * xw
.ch
;
980 int dsty
= term
.top
* xw
.ch
;
981 int height
= (term
.bot
-term
.top
) * xw
.ch
;
983 xcursor(CURSOR_HIDE
);
984 XCopyArea(xw
.dis
, xw
.win
, xw
.win
, dc
.gc
, 0, srcy
, xw
.w
, height
, 0, dsty
);
985 xclear(0, term
.bot
, term
.col
-1, term
.bot
);
991 unsigned long valuemask
;
995 char *args
[] = {NULL
};
998 xw
.dis
= XOpenDisplay(NULL
);
999 xw
.scr
= XDefaultScreen(xw
.dis
);
1001 die("Can't open display\n");
1004 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)))
1005 die("Can't load font %s\n", FONT
);
1007 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1008 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
+ LINESPACE
;
1011 for(i
= 0; i
< LEN(colorname
); i
++)
1012 dc
.col
[i
] = xgetcol(colorname
[i
]);
1014 term
.c
.attr
.fg
= DefaultFG
;
1015 term
.c
.attr
.bg
= DefaultBG
;
1016 term
.c
.attr
.mode
= ATTR_NULL
;
1018 xw
.h
= term
.row
* xw
.ch
;
1019 xw
.w
= term
.col
* xw
.cw
;
1020 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
1021 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1026 values
.foreground
= XWhitePixel(xw
.dis
, xw
.scr
);
1027 values
.font
= dc
.font
->fid
;
1028 valuemask
= GCForeground
| GCFont
;
1029 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, valuemask
, &values
);
1030 XMapWindow(xw
.dis
, xw
.win
);
1032 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
1033 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
1034 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
1035 shint
.height
= xw
.h
, shint
.width
= xw
.w
;
1036 shint
.flags
= PSize
| PResizeInc
;
1037 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
1038 XStoreName(xw
.dis
, xw
.win
, TNAME
);
1043 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1044 unsigned long xfg
, xbg
;
1045 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1048 if(base
.mode
& ATTR_REVERSE
)
1049 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1051 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1053 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1054 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1056 if(base
.mode
& ATTR_GFX
)
1057 for(i
= 0; i
< len
; i
++)
1060 XDrawImageString(xw
.dis
, xw
.win
, dc
.gc
, winx
, winy
, s
, len
);
1062 if(base
.mode
& ATTR_UNDERLINE
)
1063 XDrawLine(xw
.dis
, xw
.win
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1067 xdrawc(int x
, int y
, Glyph g
) {
1068 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1069 unsigned long xfg
, xbg
;
1072 if(g
.mode
& ATTR_REVERSE
)
1073 xfg
= dc
.col
[g
.bg
], xbg
= dc
.col
[g
.fg
];
1075 xfg
= dc
.col
[g
.fg
], xbg
= dc
.col
[g
.bg
];
1077 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1078 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1079 XDrawImageString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1084 static int oldx
= 0;
1085 static int oldy
= 0;
1086 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1088 LIMIT(oldx
, 0, term
.col
-1);
1089 LIMIT(oldy
, 0, term
.row
-1);
1091 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1092 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1093 /* remove the old cursor */
1094 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1095 xdrawc(oldx
, oldy
, term
.line
[oldy
][oldx
]);
1097 xclear(oldx
, oldy
, oldx
, oldy
);
1098 /* draw the new one */
1099 if(mode
== CURSOR_DRAW
) {
1100 xdrawc(term
.c
.x
, term
.c
.y
, g
);
1101 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1106 draw(int redraw_all
) {
1109 char buf
[DRAW_BUF_SIZ
];
1111 for(y
= 0; y
< term
.row
; y
++) {
1112 base
= term
.line
[y
][0];
1114 for(x
= 0; x
< term
.col
; x
++) {
1115 new = term
.line
[y
][x
];
1116 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1119 xdraws(buf
, base
, ox
, y
, i
);
1126 xdraws(buf
, base
, ox
, y
, i
);
1129 xcursor(CURSOR_DRAW
);
1133 expose(XEvent
*ev
) {
1134 draw(SCREEN_REDRAW
);
1140 for(i
= 0; i
< LEN(key
); i
++)
1142 return (char*)key
[i
].s
;
1147 kpress(XEvent
*ev
) {
1148 XKeyEvent
*e
= &ev
->xkey
;
1156 meta
= e
->state
& Mod1Mask
;
1157 shift
= e
->state
& ShiftMask
;
1158 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1160 if(customkey
= kmap(ksym
))
1161 ttywrite(customkey
, strlen(customkey
));
1163 buf
[sizeof(buf
)-1] = '\0';
1164 if(meta
&& len
== 1)
1165 ttywrite("\033", 1);
1173 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1178 /* XXX: paste X clipboard */;
1181 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1189 col
= e
->xconfigure
.width
/ xw
.cw
;
1190 row
= e
->xconfigure
.height
/ xw
.ch
;
1192 if(term
.col
!= col
|| term
.row
!= row
) {
1194 ttyresize(col
, row
);
1195 xw
.w
= e
->xconfigure
.width
;
1196 xw
.h
= e
->xconfigure
.height
;
1197 draw(SCREEN_REDRAW
);
1205 int xfd
= XConnectionNumber(xw
.dis
);
1208 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1209 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* fix resize bug in wmii (?) */
1213 FD_SET(cmdfd
, &rfd
);
1215 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1218 die("select failed: %s\n", SERRNO
);
1220 if(FD_ISSET(cmdfd
, &rfd
)) {
1222 draw(SCREEN_UPDATE
);
1224 while(XPending(xw
.dis
)) {
1225 XNextEvent(xw
.dis
, &ev
);
1226 if(handler
[ev
.type
])
1227 (handler
[ev
.type
])(&ev
);
1233 main(int argc
, char *argv
[]) {
1234 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1235 die("st-" VERSION
", © 2009 st engineers\n");
1237 die("usage: st [-v]\n");
1238 setlocale(LC_CTYPE
, "");