Xinqi Bao's Git
ee79a33759fd23cbbd8e15c064c664974d7a29a8
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 *shell
= getenv("SHELL");
214 char *args
[3] = {shell
, "-i", NULL
};
215 putenv("TERM=" TNAME
);
220 xbell(void) { /* visual bell */
221 XRectangle r
= { BORDER
, BORDER
, xw
.w
, xw
.h
};
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
);
1036 char *args
[] = {NULL
};
1039 xw
.dis
= XOpenDisplay(NULL
);
1040 xw
.scr
= XDefaultScreen(xw
.dis
);
1042 die("Can't open display\n");
1045 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1046 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1048 /* XXX: Assuming same size for bold font */
1049 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1050 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1053 for(i
= 0; i
< LEN(colorname
); i
++)
1054 dc
.col
[i
] = xgetcol(colorname
[i
]);
1056 term
.c
.attr
.fg
= DefaultFG
;
1057 term
.c
.attr
.bg
= DefaultBG
;
1058 term
.c
.attr
.mode
= ATTR_NULL
;
1060 xw
.h
= term
.row
* xw
.ch
;
1061 xw
.w
= term
.col
* xw
.cw
;
1062 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1063 xw
.w
+ 2*BORDER
, xw
.h
+ 2*BORDER
, 0,
1066 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.w
, xw
.h
, XDefaultDepth(xw
.dis
, xw
.scr
));
1068 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1069 XMapWindow(xw
.dis
, xw
.win
);
1071 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
1072 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
1073 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
1074 shint
.height
= xw
.h
+ 2*BORDER
, shint
.width
= xw
.w
+ 2*BORDER
;
1075 shint
.flags
= PSize
| PResizeInc
;
1076 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
1077 XStoreName(xw
.dis
, xw
.win
, TNAME
);
1078 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1084 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1085 unsigned long xfg
, xbg
;
1086 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1089 if(base
.mode
& ATTR_REVERSE
)
1090 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1092 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1094 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1095 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1097 if(base
.mode
& ATTR_GFX
)
1098 for(i
= 0; i
< len
; i
++)
1101 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1102 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1104 if(base
.mode
& ATTR_UNDERLINE
)
1105 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1110 static int oldx
= 0;
1111 static int oldy
= 0;
1112 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1114 LIMIT(oldx
, 0, term
.col
-1);
1115 LIMIT(oldy
, 0, term
.row
-1);
1117 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1118 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1120 /* remove the old cursor */
1121 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1122 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1124 xclear(oldx
, oldy
, oldx
, oldy
);
1126 /* draw the new one */
1127 if(mode
== CURSOR_DRAW
) {
1128 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1129 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1135 /* basic drawing routines */
1137 xdrawc(int x
, int y
, Glyph g
) {
1138 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1139 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1140 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1141 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1142 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1149 xclear(0, 0, term
.col
-1, term
.row
-1);
1150 for(y
= 0; y
< term
.row
; y
++)
1151 for(x
= 0; x
< term
.col
; x
++)
1152 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1153 xdrawc(x
, y
, term
.line
[y
][x
]);
1156 xcursor(CURSOR_DRAW
);
1157 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
, xw
.h
, BORDER
, BORDER
);
1163 draw(int redraw_all
) {
1166 char buf
[DRAW_BUF_SIZ
];
1168 for(y
= 0; y
< term
.row
; y
++) {
1169 base
= term
.line
[y
][0];
1171 for(x
= 0; x
< term
.col
; x
++) {
1172 new = term
.line
[y
][x
];
1173 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1176 xdraws(buf
, base
, ox
, y
, i
);
1183 xdraws(buf
, base
, ox
, y
, i
);
1185 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1186 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
, xw
.h
, BORDER
, BORDER
);
1191 expose(XEvent
*ev
) {
1192 draw(SCREEN_REDRAW
);
1198 for(i
= 0; i
< LEN(key
); i
++)
1200 return (char*)key
[i
].s
;
1205 kpress(XEvent
*ev
) {
1206 XKeyEvent
*e
= &ev
->xkey
;
1214 meta
= e
->state
& Mod1Mask
;
1215 shift
= e
->state
& ShiftMask
;
1216 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1218 if(customkey
= kmap(ksym
))
1219 ttywrite(customkey
, strlen(customkey
));
1221 buf
[sizeof(buf
)-1] = '\0';
1222 if(meta
&& len
== 1)
1223 ttywrite("\033", 1);
1231 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1236 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1239 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1247 col
= e
->xconfigure
.width
/ xw
.cw
;
1248 row
= e
->xconfigure
.height
/ xw
.ch
;
1250 if(term
.col
!= col
|| term
.row
!= row
) {
1252 ttyresize(col
, row
);
1253 xw
.w
= e
->xconfigure
.width
;
1254 xw
.h
= e
->xconfigure
.height
;
1255 XFreePixmap(xw
.dis
, xw
.buf
);
1256 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.w
, xw
.h
, XDefaultDepth(xw
.dis
, xw
.scr
));
1257 draw(SCREEN_REDRAW
);
1265 int xfd
= XConnectionNumber(xw
.dis
);
1268 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1269 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
+2*BORDER
, xw
.h
+2*BORDER
); /* fix resize bug in wmii (?) */
1273 FD_SET(cmdfd
, &rfd
);
1275 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1278 die("select failed: %s\n", SERRNO
);
1280 if(FD_ISSET(cmdfd
, &rfd
)) {
1282 draw(SCREEN_UPDATE
);
1284 while(XPending(xw
.dis
)) {
1285 XNextEvent(xw
.dis
, &ev
);
1286 if(handler
[ev
.type
])
1287 (handler
[ev
.type
])(&ev
);
1293 main(int argc
, char *argv
[]) {
1294 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1295 die("st-" VERSION
", © 2009 st engineers\n");
1297 die("usage: st [-v]\n");
1298 setlocale(LC_CTYPE
, "");