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>
24 #define ESC_TITLE_SIZ 256
25 #define ESC_BUF_SIZ 256
26 #define ESC_ARG_SIZ 16
27 #define DRAW_BUF_SIZ 1024
29 #define SERRNO strerror(errno)
30 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 #define MAX(a, b) ((a) < (b) ? (b) : (a))
32 #define LEN(a) (sizeof(a) / sizeof(a[0]))
33 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
34 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
35 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
36 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
38 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
39 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
40 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
, CURSOR_HIDE
, CURSOR_DRAW
,
41 CURSOR_SAVE
, CURSOR_LOAD
};
42 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
43 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4 };
44 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
45 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
48 char c
; /* character code */
49 char mode
; /* attribute flags */
50 int fg
; /* foreground */
51 int bg
; /* background */
52 char state
; /* state flags */
58 Glyph attr
; /* current char attributes */
63 /* CSI Escape sequence structs */
64 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
66 char buf
[ESC_BUF_SIZ
]; /* raw string */
67 int len
; /* raw string length */
70 int narg
; /* nb of args */
74 /* Internal representation of the screen */
78 Line
* line
; /* screen */
79 TCursor c
; /* cursor */
81 int top
; /* top scroll limit */
82 int bot
; /* bottom scroll limit */
83 int mode
; /* terminal mode flags */
84 int esc
; /* escape state flags */
85 char title
[ESC_TITLE_SIZ
];
89 /* Purely graphic info */
95 int w
; /* window width */
96 int h
; /* window height */
97 int bufw
; /* pixmap width */
98 int bufh
; /* pixmap height */
99 int ch
; /* char height */
100 int cw
; /* char width */
108 /* Drawing Context */
110 unsigned long col
[256];
118 static void die(const char *errstr
, ...);
119 static void draw(int);
120 static void execsh(void);
121 static void sigchld(int);
122 static void run(void);
124 static void csidump(void);
125 static void csihandle(void);
126 static void csiparse(void);
127 static void csireset(void);
129 static void tclearregion(int, int, int, int);
130 static void tcursor(int);
131 static void tmovecursor(int);
132 static void tdeletechar(int);
133 static void tdeleteline(int);
134 static void tinsertblank(int);
135 static void tinsertblankline(int);
136 static void tmoveto(int, int);
137 static void tnew(int, int);
138 static void tnewline(void);
139 static void tputc(char);
140 static void tputs(char*, int);
141 static void treset(void);
142 static void tresize(int, int);
143 static void tscroll(void);
144 static void tscrollup(int);
145 static void tscrolldown(int);
146 static void tsetattr(int*, int);
147 static void tsetchar(char);
148 static void tsetscroll(int, int);
150 static void ttynew(void);
151 static void ttyread(void);
152 static void ttyresize(int, int);
153 static void ttywrite(const char *, size_t);
155 static void xclear(int, int, int, int);
156 static void xcursor(int);
157 static void xinit(void);
158 static void xloadcols(void);
160 static void expose(XEvent
*);
161 static char* kmap(KeySym
);
162 static void kpress(XEvent
*);
163 static void resize(XEvent
*);
165 static void (*handler
[LASTEvent
])(XEvent
*) = {
168 [ConfigureNotify
] = resize
175 static CSIEscape escseq
;
186 for(row
= 0; row
< term
.row
; row
++) {
187 for(col
= 0; col
< term
.col
; col
++) {
188 if(col
== term
.c
.x
&& row
== term
.c
.y
)
191 c
= term
.line
[row
][col
];
192 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
201 die(const char *errstr
, ...) {
204 va_start(ap
, errstr
);
205 vfprintf(stderr
, errstr
, ap
);
212 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
213 DEFAULT(args
[0], "/bin/sh"); /* default shell if getenv() failed */
214 putenv("TERM=" TNAME
);
215 execvp(args
[0], args
);
219 xbell(void) { /* visual bell */
220 XRectangle r
= { BORDER
, BORDER
, xw
.bufw
, xw
.bufh
};
221 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
222 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
230 if(waitpid(pid
, &stat
, 0) < 0)
231 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
233 exit(WEXITSTATUS(stat
));
243 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
244 die("openpt failed: %s\n", SERRNO
);
246 die("grandpt failed: %s\n", SERRNO
);
248 die("unlockpt failed: %s\n", SERRNO
);
249 if(!(pts
= ptsname(m
)))
250 die("ptsname failed: %s\n", SERRNO
);
251 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
252 die("Couldn't open slave: %s\n", SERRNO
);
253 fcntl(s
, F_SETFL
, O_NDELAY
);
254 switch(pid
= fork()) {
256 die("fork failed\n");
259 setsid(); /* create a new process group */
260 dup2(s
, STDIN_FILENO
);
261 dup2(s
, STDOUT_FILENO
);
262 dup2(s
, STDERR_FILENO
);
263 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
264 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
270 signal(SIGCHLD
, sigchld
);
277 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
279 fprintf(stderr
, "\n");
284 char buf
[BUFSIZ
] = {0};
287 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
288 die("Couldn't read from shell: %s\n", SERRNO
);
294 ttywrite(const char *s
, size_t n
) {
295 if(write(cmdfd
, s
, n
) == -1)
296 die("write error on tty: %s\n", SERRNO
);
300 ttyresize(int x
, int y
) {
305 w
.ws_xpixel
= w
.ws_ypixel
= 0;
306 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
307 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
314 if(mode
== CURSOR_SAVE
)
316 else if(mode
== CURSOR_LOAD
)
317 term
.c
= c
, tmoveto(c
.x
, c
.y
);
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 term
.top
= 0, term
.bot
= term
.row
- 1;
328 term
.mode
= MODE_WRAP
;
329 tclearregion(0, 0, term
.col
-1, term
.row
-1);
333 tnew(int col
, int row
) { /* screen size */
334 term
.row
= row
, term
.col
= col
;
335 term
.top
= 0, term
.bot
= term
.row
- 1;
337 term
.mode
= MODE_WRAP
;
339 term
.c
.attr
.mode
= ATTR_NULL
;
340 term
.c
.attr
.fg
= DefaultFG
;
341 term
.c
.attr
.bg
= DefaultBG
;
342 term
.c
.x
= term
.c
.y
= 0;
344 /* allocate screen */
345 term
.line
= calloc(term
.row
, sizeof(Line
));
346 for(row
= 0 ; row
< term
.row
; row
++)
347 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
350 /* TODO: Replace with scrollup/scolldown */
353 Line temp
= term
.line
[term
.top
];
356 for(i
= term
.top
; i
< term
.bot
; i
++)
357 term
.line
[i
] = term
.line
[i
+1];
358 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
359 term
.line
[term
.bot
] = temp
;
363 tscrolldown (int n
) {
367 LIMIT(n
, 0, term
.bot
-term
.top
+1);
369 for(i
= 0; i
< n
; i
++)
370 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
372 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
374 term
.line
[i
] = term
.line
[i
-n
];
375 term
.line
[i
-n
] = temp
;
383 LIMIT(n
, 0, term
.bot
-term
.top
+1);
385 for(i
= 0; i
< n
; i
++)
386 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
388 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
390 term
.line
[i
] = term
.line
[i
+n
];
391 term
.line
[i
+n
] = temp
;
397 int y
= term
.c
.y
+ 1;
399 tscroll(), y
= term
.bot
;
406 char *p
= escseq
.buf
;
410 escseq
.priv
= 1, p
++;
412 while(p
< escseq
.buf
+escseq
.len
) {
414 escseq
.arg
[escseq
.narg
] *= 10;
415 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
417 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
428 tmoveto(int x
, int y
) {
429 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
430 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
434 tmovecursor(int dir
) {
435 int xf
= term
.c
.x
, yf
= term
.c
.y
;
446 if(term
.mode
& MODE_WRAP
&& xf
< 0) {
447 xf
= term
.col
-1, yf
--;
449 yf
= term
.top
, xf
= 0;
454 if(term
.mode
& MODE_WRAP
&& xf
>= term
.col
) {
457 yf
= term
.bot
, tscroll();
466 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
467 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
468 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
472 tclearregion(int x1
, int y1
, int x2
, int y2
) {
476 temp
= x1
, x1
= x2
, x2
= temp
;
478 temp
= y1
, y1
= y2
, y2
= temp
;
480 LIMIT(x1
, 0, term
.col
-1);
481 LIMIT(x2
, 0, term
.col
-1);
482 LIMIT(y1
, 0, term
.row
-1);
483 LIMIT(y2
, 0, term
.row
-1);
485 for(y
= y1
; y
<= y2
; y
++)
486 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
491 int src
= term
.c
.x
+ n
;
493 int size
= term
.col
- src
;
495 if(src
>= term
.col
) {
496 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
499 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
500 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
504 tinsertblank(int n
) {
507 int size
= term
.col
- n
- src
;
509 if(dst
>= term
.col
) {
510 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
513 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
514 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
518 tinsertblankline(int n
) {
523 if(term
.c
.y
> term
.bot
)
525 else if(term
.c
.y
< term
.top
)
527 if(term
.c
.y
+ n
>= bot
) {
528 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
531 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
532 /* swap deleted line <-> blanked line */
533 blank
= term
.line
[i
];
534 term
.line
[i
] = term
.line
[i
-n
];
535 term
.line
[i
-n
] = blank
;
537 memset(blank
, 0, term
.col
* sizeof(Glyph
));
547 if(term
.c
.y
> term
.bot
)
549 else if(term
.c
.y
< term
.top
)
551 if(term
.c
.y
+ n
>= bot
) {
552 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
555 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
556 /* swap deleted line <-> blanked line */
557 blank
= term
.line
[i
];
558 term
.line
[i
] = term
.line
[i
+n
];
559 term
.line
[i
+n
] = blank
;
561 memset(blank
, 0, term
.col
* sizeof(Glyph
));
566 tsetattr(int *attr
, int l
) {
569 for(i
= 0; i
< l
; i
++) {
572 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
573 term
.c
.attr
.fg
= DefaultFG
;
574 term
.c
.attr
.bg
= DefaultBG
;
577 term
.c
.attr
.mode
|= ATTR_BOLD
;
580 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
583 term
.c
.attr
.mode
|= ATTR_REVERSE
;
586 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
589 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
592 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
595 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
597 if (BETWEEN(attr
[i
], 0, 255))
598 term
.c
.attr
.fg
= attr
[i
];
600 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
603 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
606 term
.c
.attr
.fg
= DefaultFG
;
609 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
611 if (BETWEEN(attr
[i
], 0, 255))
612 term
.c
.attr
.bg
= attr
[i
];
614 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
617 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
620 term
.c
.attr
.bg
= DefaultBG
;
623 if(BETWEEN(attr
[i
], 30, 37))
624 term
.c
.attr
.fg
= attr
[i
] - 30;
625 else if(BETWEEN(attr
[i
], 40, 47))
626 term
.c
.attr
.bg
= attr
[i
] - 40;
627 else if(BETWEEN(attr
[i
], 90, 97))
628 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
629 else if(BETWEEN(attr
[i
], 100, 107))
630 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
632 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
639 tsetscroll(int t
, int b
) {
642 LIMIT(t
, 0, term
.row
-1);
643 LIMIT(b
, 0, term
.row
-1);
655 switch(escseq
.mode
) {
658 printf("erresc: unknown csi ");
662 case '@': /* ICH -- Insert <n> blank char */
663 DEFAULT(escseq
.arg
[0], 1);
664 tinsertblank(escseq
.arg
[0]);
666 case 'A': /* CUU -- Cursor <n> Up */
668 DEFAULT(escseq
.arg
[0], 1);
669 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
671 case 'B': /* CUD -- Cursor <n> Down */
672 DEFAULT(escseq
.arg
[0], 1);
673 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
675 case 'C': /* CUF -- Cursor <n> Forward */
677 DEFAULT(escseq
.arg
[0], 1);
678 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
680 case 'D': /* CUB -- Cursor <n> Backward */
681 DEFAULT(escseq
.arg
[0], 1);
682 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
684 case 'E': /* CNL -- Cursor <n> Down and first col */
685 DEFAULT(escseq
.arg
[0], 1);
686 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
688 case 'F': /* CPL -- Cursor <n> Up and first col */
689 DEFAULT(escseq
.arg
[0], 1);
690 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
692 case 'G': /* CHA -- Move to <col> */
693 case '`': /* XXX: HPA -- same? */
694 DEFAULT(escseq
.arg
[0], 1);
695 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
697 case 'H': /* CUP -- Move to <row> <col> */
698 case 'f': /* XXX: HVP -- same? */
699 DEFAULT(escseq
.arg
[0], 1);
700 DEFAULT(escseq
.arg
[1], 1);
701 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
703 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
704 case 'J': /* ED -- Clear screen */
705 switch(escseq
.arg
[0]) {
707 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
710 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
713 tclearregion(0, 0, term
.col
-1, term
.row
-1);
715 case 3: /* XXX: erase saved lines (xterm) */
720 case 'K': /* EL -- Clear line */
721 switch(escseq
.arg
[0]) {
723 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
726 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
729 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
733 case 'S': /* SU -- Scroll <n> line up */
734 DEFAULT(escseq
.arg
[0], 1);
735 tscrollup(escseq
.arg
[0]);
737 case 'T': /* SD -- Scroll <n> line down */
738 DEFAULT(escseq
.arg
[0], 1);
739 tscrolldown(escseq
.arg
[0]);
741 case 'L': /* IL -- Insert <n> blank lines */
742 DEFAULT(escseq
.arg
[0], 1);
743 tinsertblankline(escseq
.arg
[0]);
745 case 'l': /* RM -- Reset Mode */
747 switch(escseq
.arg
[0]) {
749 term
.mode
&= ~MODE_APPKEYPAD
;
752 term
.mode
&= ~MODE_WRAP
;
754 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
759 case 1048: /* XXX: no alt. screen to erase/save */
761 tcursor(CURSOR_LOAD
);
762 tclearregion(0, 0, term
.col
-1, term
.row
-1);
768 switch(escseq
.arg
[0]) {
770 term
.mode
&= ~MODE_INSERT
;
777 case 'M': /* DL -- Delete <n> lines */
778 DEFAULT(escseq
.arg
[0], 1);
779 tdeleteline(escseq
.arg
[0]);
781 case 'X': /* ECH -- Erase <n> char */
782 DEFAULT(escseq
.arg
[0], 1);
783 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
785 case 'P': /* DCH -- Delete <n> char */
786 DEFAULT(escseq
.arg
[0], 1);
787 tdeletechar(escseq
.arg
[0]);
789 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
790 case 'd': /* VPA -- Move to <row> */
791 DEFAULT(escseq
.arg
[0], 1);
792 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
794 case 'h': /* SM -- Set terminal mode */
796 switch(escseq
.arg
[0]) {
798 term
.mode
|= MODE_APPKEYPAD
;
801 term
.mode
|= MODE_WRAP
;
803 case 12: /* att610 -- Start blinking cursor (IGNORED) */
809 case 1049: /* XXX: no alt. screen to erase/save */
810 tcursor(CURSOR_SAVE
);
811 tclearregion(0, 0, term
.col
-1, term
.row
-1);
813 default: goto unknown
;
816 switch(escseq
.arg
[0]) {
818 term
.mode
|= MODE_INSERT
;
820 default: goto unknown
;
824 case 'm': /* SGR -- Terminal attribute (color) */
825 tsetattr(escseq
.arg
, escseq
.narg
);
827 case 'r': /* DECSTBM -- Set Scrolling Region */
831 DEFAULT(escseq
.arg
[0], 1);
832 DEFAULT(escseq
.arg
[1], term
.row
);
833 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
836 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
837 tcursor(CURSOR_SAVE
);
839 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
840 tcursor(CURSOR_LOAD
);
848 printf("ESC [ %s", escseq
.priv
? "? " : "");
850 for(i
= 0; i
< escseq
.narg
; i
++)
851 printf("%d ", escseq
.arg
[i
]);
853 putchar(escseq
.mode
);
859 memset(&escseq
, 0, sizeof(escseq
));
864 int space
= TAB
- term
.c
.x
% TAB
;
866 if(term
.c
.x
+ space
>= term
.col
)
869 for(; space
> 0; space
--)
870 tmovecursor(CURSOR_RIGHT
);
875 if(term
.esc
& ESC_START
) {
876 if(term
.esc
& ESC_CSI
) {
877 escseq
.buf
[escseq
.len
++] = c
;
878 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
880 csiparse(), csihandle();
882 } else if(term
.esc
& ESC_OSC
) {
885 term
.esc
= ESC_START
| ESC_TITLE
;
887 } else if(term
.esc
& ESC_TITLE
) {
888 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
890 term
.title
[term
.titlelen
] = '\0';
891 XStoreName(xw
.dis
, xw
.win
, term
.title
);
893 term
.title
[term
.titlelen
++] = c
;
895 } else if(term
.esc
& ESC_ALTCHARSET
) {
897 case '0': /* Line drawing crap */
898 term
.c
.attr
.mode
|= ATTR_GFX
;
900 case 'B': /* Back to regular text */
901 term
.c
.attr
.mode
&= ~ATTR_GFX
;
904 printf("esc unhandled charset: ESC ( %c\n", c
);
916 term
.esc
|= ESC_ALTCHARSET
;
919 tmoveto(term
.c
.x
, term
.c
.y
-1);
923 tmoveto(term
.c
.x
, term
.c
.y
+1);
927 tmoveto(term
.c
.x
+1, term
.c
.y
);
930 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
931 tmoveto(term
.c
.x
-1, term
.c
.y
);
934 case 'E': /* NEL -- Next line */
938 case 'M': /* RI -- Reverse index */
939 if(term
.c
.y
== term
.top
)
942 tmoveto(term
.c
.x
, term
.c
.y
-1);
945 case 'c': /* RIS -- Reset to inital state */
949 case '=': /* DECPAM */
950 term
.mode
|= MODE_APPKEYPAD
;
953 case '>': /* DECPNM */
954 term
.mode
&= ~MODE_APPKEYPAD
;
958 tcursor(CURSOR_SAVE
);
962 tcursor(CURSOR_LOAD
);
966 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
976 tmovecursor(CURSOR_LEFT
);
979 tmoveto(0, term
.c
.y
);
989 term
.esc
= ESC_START
;
993 tmovecursor(CURSOR_RIGHT
);
1000 tputs(char *s
, int len
) {
1001 for(; len
> 0; len
--)
1006 tresize(int col
, int row
) {
1009 int minrow
= MIN(row
, term
.row
);
1010 int mincol
= MIN(col
, term
.col
);
1012 if(col
< 1 || row
< 1)
1015 line
= calloc(row
, sizeof(Line
));
1016 for(i
= 0 ; i
< row
; i
++)
1017 line
[i
] = calloc(col
, sizeof(Glyph
));
1019 for(i
= 0 ; i
< minrow
; i
++)
1020 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
1022 for(i
= 0; i
< term
.row
; i
++)
1026 LIMIT(term
.c
.x
, 0, col
-1);
1027 LIMIT(term
.c
.y
, 0, row
-1);
1028 LIMIT(term
.top
, 0, row
-1);
1029 LIMIT(term
.bot
, 0, row
-1);
1033 term
.col
= col
, term
.row
= row
;
1040 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1041 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1043 for(i
= 0; i
< 16; i
++) {
1044 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1046 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1048 dc
.col
[i
] = color
.pixel
;
1051 /* same colors as xterm */
1052 for(r
= 0; r
< 6; r
++)
1053 for(g
= 0; g
< 6; g
++)
1054 for(b
= 0; b
< 6; b
++) {
1055 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1056 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1057 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1058 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1060 fprintf(stderr
, "Could not allocate color %d\n", i
);
1062 dc
.col
[i
] = color
.pixel
;
1066 for(r
= 0; r
< 24; r
++, i
++) {
1067 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1068 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1070 fprintf(stderr
, "Could not allocate color %d\n", i
);
1072 dc
.col
[i
] = color
.pixel
;
1077 xclear(int x1
, int y1
, int x2
, int y2
) {
1078 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1079 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1080 x1
* xw
.cw
, y1
* xw
.ch
,
1081 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1087 XClassHint
class = {TNAME
, TNAME
};
1088 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1090 .flags
= PSize
| PResizeInc
| PBaseSize
,
1093 .height_inc
= xw
.ch
,
1095 .base_height
= 2*BORDER
,
1096 .base_width
= 2*BORDER
,
1098 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1103 xw
.dis
= XOpenDisplay(NULL
);
1104 xw
.scr
= XDefaultScreen(xw
.dis
);
1106 die("Can't open display\n");
1109 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1110 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1112 /* XXX: Assuming same size for bold font */
1113 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1114 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1119 term
.c
.attr
.fg
= DefaultFG
;
1120 term
.c
.attr
.bg
= DefaultBG
;
1121 term
.c
.attr
.mode
= ATTR_NULL
;
1123 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1124 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1125 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1129 xw
.bufw
= xw
.w
- 2*BORDER
;
1130 xw
.bufh
= xw
.h
- 2*BORDER
;
1131 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1133 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1134 XMapWindow(xw
.dis
, xw
.win
);
1136 XStoreName(xw
.dis
, xw
.win
, "st");
1141 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1142 unsigned long xfg
, xbg
;
1143 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1146 if(base
.mode
& ATTR_REVERSE
)
1147 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1149 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1151 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1152 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1154 if(base
.mode
& ATTR_GFX
)
1155 for(i
= 0; i
< len
; i
++)
1156 s
[i
] = gfx
[(int)s
[i
]];
1158 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1159 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1161 if(base
.mode
& ATTR_UNDERLINE
)
1162 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1167 static int oldx
= 0;
1168 static int oldy
= 0;
1169 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1171 LIMIT(oldx
, 0, term
.col
-1);
1172 LIMIT(oldy
, 0, term
.row
-1);
1174 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1175 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1177 /* remove the old cursor */
1178 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1179 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1181 xclear(oldx
, oldy
, oldx
, oldy
);
1183 /* draw the new one */
1184 if(mode
== CURSOR_DRAW
) {
1185 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1186 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1191 /* basic drawing routines */
1193 xdrawc(int x
, int y
, Glyph g
) {
1194 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1195 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1196 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1197 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1198 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1205 xclear(0, 0, term
.col
-1, term
.row
-1);
1206 for(y
= 0; y
< term
.row
; y
++)
1207 for(x
= 0; x
< term
.col
; x
++)
1208 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1209 xdrawc(x
, y
, term
.line
[y
][x
]);
1212 xcursor(CURSOR_DRAW
);
1213 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1218 /* optimized drawing routine */
1220 draw(int redraw_all
) {
1223 char buf
[DRAW_BUF_SIZ
];
1225 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1226 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1227 for(y
= 0; y
< term
.row
; y
++) {
1228 base
= term
.line
[y
][0];
1230 for(x
= 0; x
< term
.col
; x
++) {
1231 new = term
.line
[y
][x
];
1232 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1235 xdraws(buf
, base
, ox
, y
, i
);
1242 xdraws(buf
, base
, ox
, y
, i
);
1244 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1245 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1252 expose(XEvent
*ev
) {
1253 draw(SCREEN_REDRAW
);
1259 for(i
= 0; i
< LEN(key
); i
++)
1261 return (char*)key
[i
].s
;
1266 kpress(XEvent
*ev
) {
1267 XKeyEvent
*e
= &ev
->xkey
;
1275 meta
= e
->state
& Mod1Mask
;
1276 shift
= e
->state
& ShiftMask
;
1277 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1279 if((customkey
= kmap(ksym
)))
1280 ttywrite(customkey
, strlen(customkey
));
1282 buf
[sizeof(buf
)-1] = '\0';
1283 if(meta
&& len
== 1)
1284 ttywrite("\033", 1);
1292 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1297 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1300 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1309 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1312 xw
.w
= e
->xconfigure
.width
;
1313 xw
.h
= e
->xconfigure
.height
;
1314 xw
.bufw
= xw
.w
- 2*BORDER
;
1315 xw
.bufh
= xw
.h
- 2*BORDER
;
1316 col
= xw
.bufw
/ xw
.cw
;
1317 row
= xw
.bufh
/ xw
.ch
;
1319 ttyresize(col
, row
);
1320 XFreePixmap(xw
.dis
, xw
.buf
);
1321 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1322 draw(SCREEN_REDRAW
);
1329 int xfd
= XConnectionNumber(xw
.dis
);
1332 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1333 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1337 FD_SET(cmdfd
, &rfd
);
1339 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1342 die("select failed: %s\n", SERRNO
);
1344 if(FD_ISSET(cmdfd
, &rfd
)) {
1346 draw(SCREEN_UPDATE
);
1348 while(XPending(xw
.dis
)) {
1349 XNextEvent(xw
.dis
, &ev
);
1350 if(handler
[ev
.type
])
1351 (handler
[ev
.type
])(&ev
);
1357 main(int argc
, char *argv
[]) {
1358 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1359 die("st-" VERSION
", © 2009 st engineers\n");
1361 die("usage: st [-v]\n");
1362 setlocale(LC_CTYPE
, "");