Xinqi Bao's Git
deb161055009db9f6fd2dccb94b535fa648d9ecf
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 ch
; /* char height */
99 int cw
; /* char width */
109 /* Drawing Context */
111 unsigned long col
[LEN(colorname
)];
117 static void die(const char *errstr
, ...);
118 static void draw(int);
119 static void execsh(void);
120 static void sigchld(int);
121 static void run(void);
123 static void csidump(void);
124 static void csihandle(void);
125 static void csiparse(void);
126 static void csireset(void);
128 static void tclearregion(int, int, int, int);
129 static void tcursor(int);
130 static void tmovecursor(int);
131 static void tdeletechar(int);
132 static void tdeleteline(int);
133 static void tinsertblank(int);
134 static void tinsertblankline(int);
135 static void tmoveto(int, int);
136 static void tnew(int, int);
137 static void tnewline(void);
138 static void tputc(char);
139 static void tputs(char*, int);
140 static void treset(void);
141 static void tresize(int, int);
142 static void tscroll(void);
143 static void tscrollup(int);
144 static void tscrolldown(int);
145 static void tsetattr(int*, int);
146 static void tsetchar(char);
147 static void tsetscroll(int, int);
149 static void ttynew(void);
150 static void ttyread(void);
151 static void ttyresize(int, int);
152 static void ttywrite(const char *, size_t);
154 static unsigned long xgetcol(const char *);
155 static void xclear(int, int, int, int);
156 static void xcursor(int);
157 static void xinit(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
= { BORDER
, BORDER
, 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
];
354 for(i
= term
.top
; i
< term
.bot
; i
++)
355 term
.line
[i
] = term
.line
[i
+1];
356 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
357 term
.line
[term
.bot
] = temp
;
361 tscrolldown (int n
) {
365 LIMIT(n
, 0, term
.bot
-term
.top
+1);
367 for(i
= 0; i
< n
; i
++)
368 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
370 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
372 term
.line
[i
] = term
.line
[i
-n
];
373 term
.line
[i
-n
] = temp
;
381 LIMIT(n
, 0, term
.bot
-term
.top
+1);
383 for(i
= 0; i
< n
; i
++)
384 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
386 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
388 term
.line
[i
] = term
.line
[i
+n
];
389 term
.line
[i
+n
] = temp
;
395 int y
= term
.c
.y
+ 1;
397 tscroll(), y
= term
.bot
;
404 char *p
= escseq
.buf
;
408 escseq
.priv
= 1, p
++;
410 while(p
< escseq
.buf
+escseq
.len
) {
412 escseq
.arg
[escseq
.narg
] *= 10;
413 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
415 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
426 tmoveto(int x
, int y
) {
427 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
428 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
432 tmovecursor(int dir
) {
433 int xf
= term
.c
.x
, yf
= term
.c
.y
;
444 if(term
.mode
& MODE_WRAP
&& xf
< 0) {
445 xf
= term
.col
-1, yf
--;
447 yf
= term
.top
, xf
= 0;
452 if(term
.mode
& MODE_WRAP
&& xf
>= term
.col
) {
455 yf
= term
.bot
, tscroll();
464 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
465 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
466 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
470 tclearregion(int x1
, int y1
, int x2
, int y2
) {
474 temp
= x1
, x1
= x2
, x2
= temp
;
476 temp
= y1
, y1
= y2
, y2
= temp
;
478 LIMIT(x1
, 0, term
.col
-1);
479 LIMIT(x2
, 0, term
.col
-1);
480 LIMIT(y1
, 0, term
.row
-1);
481 LIMIT(y2
, 0, term
.row
-1);
483 for(y
= y1
; y
<= y2
; y
++)
484 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
489 int src
= term
.c
.x
+ n
;
491 int size
= term
.col
- src
;
493 if(src
>= term
.col
) {
494 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
497 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
498 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
502 tinsertblank(int n
) {
505 int size
= term
.col
- n
- src
;
507 if(dst
>= term
.col
) {
508 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
511 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
512 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
516 tinsertblankline(int n
) {
521 if(term
.c
.y
> term
.bot
)
523 else if(term
.c
.y
< term
.top
)
525 if(term
.c
.y
+ n
>= bot
) {
526 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
529 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
530 /* swap deleted line <-> blanked line */
531 blank
= term
.line
[i
];
532 term
.line
[i
] = term
.line
[i
-n
];
533 term
.line
[i
-n
] = blank
;
535 memset(blank
, 0, term
.col
* sizeof(Glyph
));
545 if(term
.c
.y
> term
.bot
)
547 else if(term
.c
.y
< term
.top
)
549 if(term
.c
.y
+ n
>= bot
) {
550 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
553 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
554 /* swap deleted line <-> blanked line */
555 blank
= term
.line
[i
];
556 term
.line
[i
] = term
.line
[i
+n
];
557 term
.line
[i
+n
] = blank
;
559 memset(blank
, 0, term
.col
* sizeof(Glyph
));
564 tsetattr(int *attr
, int l
) {
567 for(i
= 0; i
< l
; i
++) {
570 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
571 term
.c
.attr
.fg
= DefaultFG
;
572 term
.c
.attr
.bg
= DefaultBG
;
575 term
.c
.attr
.mode
|= ATTR_BOLD
;
578 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
581 term
.c
.attr
.mode
|= ATTR_REVERSE
;
584 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
587 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
590 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
593 term
.c
.attr
.fg
= DefaultFG
;
596 term
.c
.attr
.fg
= DefaultBG
;
599 if(BETWEEN(attr
[i
], 30, 37))
600 term
.c
.attr
.fg
= attr
[i
] - 30;
601 else if(BETWEEN(attr
[i
], 40, 47))
602 term
.c
.attr
.bg
= attr
[i
] - 40;
604 fprintf(stderr
, "erresc: gfx attr %d unkown\n", attr
[i
]);
611 tsetscroll(int t
, int b
) {
614 LIMIT(t
, 0, term
.row
-1);
615 LIMIT(b
, 0, term
.row
-1);
627 switch(escseq
.mode
) {
630 printf("erresc: unknown csi ");
634 case '@': /* ICH -- Insert <n> blank char */
635 DEFAULT(escseq
.arg
[0], 1);
636 tinsertblank(escseq
.arg
[0]);
638 case 'A': /* CUU -- Cursor <n> Up */
640 DEFAULT(escseq
.arg
[0], 1);
641 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
643 case 'B': /* CUD -- Cursor <n> Down */
644 DEFAULT(escseq
.arg
[0], 1);
645 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
647 case 'C': /* CUF -- Cursor <n> Forward */
649 DEFAULT(escseq
.arg
[0], 1);
650 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
652 case 'D': /* CUB -- Cursor <n> Backward */
653 DEFAULT(escseq
.arg
[0], 1);
654 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
656 case 'E': /* CNL -- Cursor <n> Down and first col */
657 DEFAULT(escseq
.arg
[0], 1);
658 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
660 case 'F': /* CPL -- Cursor <n> Up and first col */
661 DEFAULT(escseq
.arg
[0], 1);
662 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
664 case 'G': /* CHA -- Move to <col> */
665 case '`': /* XXX: HPA -- same? */
666 DEFAULT(escseq
.arg
[0], 1);
667 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
669 case 'H': /* CUP -- Move to <row> <col> */
670 case 'f': /* XXX: HVP -- same? */
671 DEFAULT(escseq
.arg
[0], 1);
672 DEFAULT(escseq
.arg
[1], 1);
673 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
675 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
676 case 'J': /* ED -- Clear screen */
677 switch(escseq
.arg
[0]) {
679 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
682 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
685 tclearregion(0, 0, term
.col
-1, term
.row
-1);
687 case 3: /* XXX: erase saved lines (xterm) */
692 case 'K': /* EL -- Clear line */
693 switch(escseq
.arg
[0]) {
695 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
698 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
701 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
705 case 'S': /* SU -- Scroll <n> line up */
706 DEFAULT(escseq
.arg
[0], 1);
707 tscrollup(escseq
.arg
[0]);
709 case 'T': /* SD -- Scroll <n> line down */
710 DEFAULT(escseq
.arg
[0], 1);
711 tscrolldown(escseq
.arg
[0]);
713 case 'L': /* IL -- Insert <n> blank lines */
714 DEFAULT(escseq
.arg
[0], 1);
715 tinsertblankline(escseq
.arg
[0]);
717 case 'l': /* RM -- Reset Mode */
719 switch(escseq
.arg
[0]) {
721 term
.mode
&= ~MODE_APPKEYPAD
;
724 term
.mode
&= ~MODE_WRAP
;
726 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
731 case 1048: /* XXX: no alt. screen to erase/save */
733 tcursor(CURSOR_LOAD
);
734 tclearregion(0, 0, term
.col
-1, term
.row
-1);
740 switch(escseq
.arg
[0]) {
742 term
.mode
&= ~MODE_INSERT
;
749 case 'M': /* DL -- Delete <n> lines */
750 DEFAULT(escseq
.arg
[0], 1);
751 tdeleteline(escseq
.arg
[0]);
753 case 'X': /* ECH -- Erase <n> char */
754 DEFAULT(escseq
.arg
[0], 1);
755 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
757 case 'P': /* DCH -- Delete <n> char */
758 DEFAULT(escseq
.arg
[0], 1);
759 tdeletechar(escseq
.arg
[0]);
761 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
762 case 'd': /* VPA -- Move to <row> */
763 DEFAULT(escseq
.arg
[0], 1);
764 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
766 case 'h': /* SM -- Set terminal mode */
768 switch(escseq
.arg
[0]) {
770 term
.mode
|= MODE_APPKEYPAD
;
773 term
.mode
|= MODE_WRAP
;
775 case 12: /* att610 -- Start blinking cursor (IGNORED) */
781 case 1049: /* XXX: no alt. screen to erase/save */
782 tcursor(CURSOR_SAVE
);
783 tclearregion(0, 0, term
.col
-1, term
.row
-1);
785 default: goto unknown
;
788 switch(escseq
.arg
[0]) {
790 term
.mode
|= MODE_INSERT
;
792 default: goto unknown
;
796 case 'm': /* SGR -- Terminal attribute (color) */
797 tsetattr(escseq
.arg
, escseq
.narg
);
799 case 'r': /* DECSTBM -- Set Scrolling Region */
803 DEFAULT(escseq
.arg
[0], 1);
804 DEFAULT(escseq
.arg
[1], term
.row
);
805 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
808 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
809 tcursor(CURSOR_SAVE
);
811 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
812 tcursor(CURSOR_LOAD
);
820 printf("ESC [ %s", escseq
.priv
? "? " : "");
822 for(i
= 0; i
< escseq
.narg
; i
++)
823 printf("%d ", escseq
.arg
[i
]);
825 putchar(escseq
.mode
);
831 memset(&escseq
, 0, sizeof(escseq
));
836 int space
= TAB
- term
.c
.x
% TAB
;
838 if(term
.c
.x
+ space
>= term
.col
)
841 for(; space
> 0; space
--)
842 tmovecursor(CURSOR_RIGHT
);
848 if(term
.esc
& ESC_START
) {
849 if(term
.esc
& ESC_CSI
) {
850 escseq
.buf
[escseq
.len
++] = c
;
851 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
853 csiparse(), csihandle();
855 } else if(term
.esc
& ESC_OSC
) {
858 term
.esc
= ESC_START
| ESC_TITLE
;
860 } else if(term
.esc
& ESC_TITLE
) {
861 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
863 term
.title
[term
.titlelen
] = '\0';
864 XStoreName(xw
.dis
, xw
.win
, term
.title
);
866 term
.title
[term
.titlelen
++] = c
;
868 } else if(term
.esc
& ESC_ALTCHARSET
) {
870 case '0': /* Line drawing crap */
871 term
.c
.attr
.mode
|= ATTR_GFX
;
873 case 'B': /* Back to regular text */
874 term
.c
.attr
.mode
&= ~ATTR_GFX
;
877 printf("esc unhandled charset: ESC ( %c\n", c
);
889 term
.esc
|= ESC_ALTCHARSET
;
892 tmoveto(term
.c
.x
, term
.c
.y
-1);
896 tmoveto(term
.c
.x
, term
.c
.y
+1);
900 tmoveto(term
.c
.x
+1, term
.c
.y
);
903 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
904 tmoveto(term
.c
.x
-1, term
.c
.y
);
907 case 'E': /* NEL -- Next line */
911 case 'M': /* RI -- Reverse index */
912 if(term
.c
.y
== term
.top
)
915 tmoveto(term
.c
.x
, term
.c
.y
-1);
918 case 'c': /* RIS -- Reset to inital state */
922 case '=': /* DECPAM */
923 term
.mode
|= MODE_APPKEYPAD
;
926 case '>': /* DECPNM */
927 term
.mode
&= ~MODE_APPKEYPAD
;
931 tcursor(CURSOR_SAVE
);
935 tcursor(CURSOR_LOAD
);
939 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
949 tmovecursor(CURSOR_LEFT
);
952 tmoveto(0, term
.c
.y
);
962 term
.esc
= ESC_START
;
966 tmovecursor(CURSOR_RIGHT
);
973 tputs(char *s
, int len
) {
974 for(; len
> 0; len
--)
979 tresize(int col
, int row
) {
982 int minrow
= MIN(row
, term
.row
);
983 int mincol
= MIN(col
, term
.col
);
985 if(col
< 1 || row
< 1)
988 line
= calloc(row
, sizeof(Line
));
989 for(i
= 0 ; i
< row
; i
++)
990 line
[i
] = calloc(col
, sizeof(Glyph
));
992 for(i
= 0 ; i
< minrow
; i
++)
993 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
995 for(i
= 0; i
< term
.row
; i
++)
999 LIMIT(term
.c
.x
, 0, col
-1);
1000 LIMIT(term
.c
.y
, 0, row
-1);
1001 LIMIT(term
.top
, 0, row
-1);
1002 LIMIT(term
.bot
, 0, row
-1);
1006 term
.col
= col
, term
.row
= row
;
1010 xgetcol(const char *s
) {
1012 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1014 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
1015 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
1016 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
1022 xclear(int x1
, int y1
, int x2
, int y2
) {
1023 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1024 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1025 x1
* xw
.cw
, y1
* xw
.ch
,
1026 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1034 char *args
[] = {NULL
};
1037 xw
.dis
= XOpenDisplay(NULL
);
1038 xw
.scr
= XDefaultScreen(xw
.dis
);
1040 die("Can't open display\n");
1043 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1044 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1046 /* XXX: Assuming same size for bold font */
1047 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1048 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1051 for(i
= 0; i
< LEN(colorname
); i
++)
1052 dc
.col
[i
] = xgetcol(colorname
[i
]);
1054 term
.c
.attr
.fg
= DefaultFG
;
1055 term
.c
.attr
.bg
= DefaultBG
;
1056 term
.c
.attr
.mode
= ATTR_NULL
;
1058 xw
.h
= term
.row
* xw
.ch
;
1059 xw
.w
= term
.col
* xw
.cw
;
1060 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1061 xw
.w
+ 2*BORDER
, xw
.h
+ 2*BORDER
, 0,
1064 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.w
, xw
.h
, XDefaultDepth(xw
.dis
, xw
.scr
));
1066 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1067 XMapWindow(xw
.dis
, xw
.win
);
1069 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
1070 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
1071 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
1072 shint
.height
= xw
.h
+ 2*BORDER
, shint
.width
= xw
.w
+ 2*BORDER
;
1073 shint
.flags
= PSize
| PResizeInc
;
1074 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
1075 XStoreName(xw
.dis
, xw
.win
, TNAME
);
1076 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1082 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1083 unsigned long xfg
, xbg
;
1084 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1087 if(base
.mode
& ATTR_REVERSE
)
1088 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1090 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1092 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1093 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1095 if(base
.mode
& ATTR_GFX
)
1096 for(i
= 0; i
< len
; i
++)
1099 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1100 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1102 if(base
.mode
& ATTR_UNDERLINE
)
1103 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1108 static int oldx
= 0;
1109 static int oldy
= 0;
1110 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1112 LIMIT(oldx
, 0, term
.col
-1);
1113 LIMIT(oldy
, 0, term
.row
-1);
1115 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1116 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1118 /* remove the old cursor */
1119 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1120 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1122 xclear(oldx
, oldy
, oldx
, oldy
);
1124 /* draw the new one */
1125 if(mode
== CURSOR_DRAW
) {
1126 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1127 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1133 /* basic drawing routines */
1135 xdrawc(int x
, int y
, Glyph g
) {
1136 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1137 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1138 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1139 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1140 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1147 xclear(0, 0, term
.col
-1, term
.row
-1);
1148 for(y
= 0; y
< term
.row
; y
++)
1149 for(x
= 0; x
< term
.col
; x
++)
1150 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1151 xdrawc(x
, y
, term
.line
[y
][x
]);
1154 xcursor(CURSOR_DRAW
);
1155 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
, xw
.h
, BORDER
, BORDER
);
1161 draw(int redraw_all
) {
1164 char buf
[DRAW_BUF_SIZ
];
1166 for(y
= 0; y
< term
.row
; y
++) {
1167 base
= term
.line
[y
][0];
1169 for(x
= 0; x
< term
.col
; x
++) {
1170 new = term
.line
[y
][x
];
1171 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1174 xdraws(buf
, base
, ox
, y
, i
);
1181 xdraws(buf
, base
, ox
, y
, i
);
1183 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1184 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
, xw
.h
, BORDER
, BORDER
);
1189 expose(XEvent
*ev
) {
1190 draw(SCREEN_REDRAW
);
1196 for(i
= 0; i
< LEN(key
); i
++)
1198 return (char*)key
[i
].s
;
1203 kpress(XEvent
*ev
) {
1204 XKeyEvent
*e
= &ev
->xkey
;
1212 meta
= e
->state
& Mod1Mask
;
1213 shift
= e
->state
& ShiftMask
;
1214 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1216 if(customkey
= kmap(ksym
))
1217 ttywrite(customkey
, strlen(customkey
));
1219 buf
[sizeof(buf
)-1] = '\0';
1220 if(meta
&& len
== 1)
1221 ttywrite("\033", 1);
1229 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1234 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1237 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1245 col
= e
->xconfigure
.width
/ xw
.cw
;
1246 row
= e
->xconfigure
.height
/ xw
.ch
;
1248 if(term
.col
!= col
|| term
.row
!= row
) {
1250 ttyresize(col
, row
);
1251 xw
.w
= e
->xconfigure
.width
;
1252 xw
.h
= e
->xconfigure
.height
;
1253 XFreePixmap(xw
.dis
, xw
.buf
);
1254 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.w
, xw
.h
, XDefaultDepth(xw
.dis
, xw
.scr
));
1255 draw(SCREEN_REDRAW
);
1263 int xfd
= XConnectionNumber(xw
.dis
);
1266 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1267 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
+2*BORDER
, xw
.h
+2*BORDER
); /* fix resize bug in wmii (?) */
1271 FD_SET(cmdfd
, &rfd
);
1273 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1276 die("select failed: %s\n", SERRNO
);
1278 if(FD_ISSET(cmdfd
, &rfd
)) {
1280 draw(SCREEN_UPDATE
);
1282 while(XPending(xw
.dis
)) {
1283 XNextEvent(xw
.dis
, &ev
);
1284 if(handler
[ev
.type
])
1285 (handler
[ev
.type
])(&ev
);
1291 main(int argc
, char *argv
[]) {
1292 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1293 die("st-" VERSION
", © 2009 st engineers\n");
1295 die("usage: st [-v]\n");
1296 setlocale(LC_CTYPE
, "");