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 tputtab(void);
140 static void tputc(char);
141 static void tputs(char*, int);
142 static void treset(void);
143 static void tresize(int, int);
144 static void tscroll(void);
145 static void tscrollup(int);
146 static void tscrolldown(int);
147 static void tsetattr(int*, int);
148 static void tsetchar(char);
149 static void tsetscroll(int, int);
151 static void ttynew(void);
152 static void ttyread(void);
153 static void ttyresize(int, int);
154 static void ttywrite(const char *, size_t);
156 static void xbell(void);
157 static void xdraws(char *, Glyph
, int, int, int);
158 static void xhints(void);
159 static void xclear(int, int, int, int);
160 static void xcursor(int);
161 static void xinit(void);
162 static void xloadcols(void);
164 static void expose(XEvent
*);
165 static char* kmap(KeySym
);
166 static void kpress(XEvent
*);
167 static void resize(XEvent
*);
169 static void (*handler
[LASTEvent
])(XEvent
*) = {
172 [ConfigureNotify
] = resize
179 static CSIEscape escseq
;
190 for(row
= 0; row
< term
.row
; row
++) {
191 for(col
= 0; col
< term
.col
; col
++) {
192 if(col
== term
.c
.x
&& row
== term
.c
.y
)
195 c
= term
.line
[row
][col
];
196 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
205 die(const char *errstr
, ...) {
208 va_start(ap
, errstr
);
209 vfprintf(stderr
, errstr
, ap
);
216 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
217 DEFAULT(args
[0], "/bin/sh"); /* default shell if getenv() failed */
218 putenv("TERM=" TNAME
);
219 execvp(args
[0], args
);
223 xbell(void) { /* visual bell */
224 XRectangle r
= { BORDER
, BORDER
, xw
.bufw
, xw
.bufh
};
225 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
226 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
234 if(waitpid(pid
, &stat
, 0) < 0)
235 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
237 exit(WEXITSTATUS(stat
));
247 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
248 die("openpt failed: %s\n", SERRNO
);
250 die("grantpt failed: %s\n", SERRNO
);
252 die("unlockpt failed: %s\n", SERRNO
);
253 if(!(pts
= ptsname(m
)))
254 die("ptsname failed: %s\n", SERRNO
);
255 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
256 die("Couldn't open slave: %s\n", SERRNO
);
257 fcntl(s
, F_SETFL
, O_NDELAY
);
258 switch(pid
= fork()) {
260 die("fork failed\n");
263 setsid(); /* create a new process group */
264 dup2(s
, STDIN_FILENO
);
265 dup2(s
, STDOUT_FILENO
);
266 dup2(s
, STDERR_FILENO
);
267 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
268 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
276 signal(SIGCHLD
, sigchld
);
283 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
285 fprintf(stderr
, "\n");
290 char buf
[BUFSIZ
] = {0};
293 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
294 die("Couldn't read from shell: %s\n", SERRNO
);
300 ttywrite(const char *s
, size_t n
) {
301 if(write(cmdfd
, s
, n
) == -1)
302 die("write error on tty: %s\n", SERRNO
);
306 ttyresize(int x
, int y
) {
311 w
.ws_xpixel
= w
.ws_ypixel
= 0;
312 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
313 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
320 if(mode
== CURSOR_SAVE
)
322 else if(mode
== CURSOR_LOAD
)
323 term
.c
= c
, tmoveto(c
.x
, c
.y
);
328 term
.c
.attr
.mode
= ATTR_NULL
;
329 term
.c
.attr
.fg
= DefaultFG
;
330 term
.c
.attr
.bg
= DefaultBG
;
331 term
.c
.x
= term
.c
.y
= 0;
333 term
.top
= 0, term
.bot
= term
.row
- 1;
334 term
.mode
= MODE_WRAP
;
335 tclearregion(0, 0, term
.col
-1, term
.row
-1);
339 tnew(int col
, int row
) { /* screen size */
340 term
.row
= row
, term
.col
= col
;
341 term
.top
= 0, term
.bot
= term
.row
- 1;
343 term
.mode
= MODE_WRAP
;
345 term
.c
.attr
.mode
= ATTR_NULL
;
346 term
.c
.attr
.fg
= DefaultFG
;
347 term
.c
.attr
.bg
= DefaultBG
;
348 term
.c
.x
= term
.c
.y
= 0;
350 /* allocate screen */
351 term
.line
= calloc(term
.row
, sizeof(Line
));
352 for(row
= 0 ; row
< term
.row
; row
++)
353 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
356 /* TODO: Replace with scrollup/scolldown */
359 Line temp
= term
.line
[term
.top
];
362 for(i
= term
.top
; i
< term
.bot
; i
++)
363 term
.line
[i
] = term
.line
[i
+1];
364 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
365 term
.line
[term
.bot
] = temp
;
369 tscrolldown (int n
) {
373 LIMIT(n
, 0, term
.bot
-term
.top
+1);
375 for(i
= 0; i
< n
; i
++)
376 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
378 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
380 term
.line
[i
] = term
.line
[i
-n
];
381 term
.line
[i
-n
] = temp
;
389 LIMIT(n
, 0, term
.bot
-term
.top
+1);
391 for(i
= 0; i
< n
; i
++)
392 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
394 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
396 term
.line
[i
] = term
.line
[i
+n
];
397 term
.line
[i
+n
] = temp
;
403 int y
= term
.c
.y
+ 1;
405 tscroll(), y
= term
.bot
;
412 char *p
= escseq
.buf
;
416 escseq
.priv
= 1, p
++;
418 while(p
< escseq
.buf
+escseq
.len
) {
420 escseq
.arg
[escseq
.narg
] *= 10;
421 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
423 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
434 tmoveto(int x
, int y
) {
435 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
436 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
440 tmovecursor(int dir
) {
441 int xf
= term
.c
.x
, yf
= term
.c
.y
;
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
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
505 tinsertblank(int n
) {
508 int size
= term
.col
- dst
;
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
- 1, 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 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
598 if (BETWEEN(attr
[i
], 0, 255))
599 term
.c
.attr
.fg
= attr
[i
];
601 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
604 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
607 term
.c
.attr
.fg
= DefaultFG
;
610 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
612 if (BETWEEN(attr
[i
], 0, 255))
613 term
.c
.attr
.bg
= attr
[i
];
615 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
618 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
621 term
.c
.attr
.bg
= DefaultBG
;
624 if(BETWEEN(attr
[i
], 30, 37))
625 term
.c
.attr
.fg
= attr
[i
] - 30;
626 else if(BETWEEN(attr
[i
], 40, 47))
627 term
.c
.attr
.bg
= attr
[i
] - 40;
628 else if(BETWEEN(attr
[i
], 90, 97))
629 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
630 else if(BETWEEN(attr
[i
], 100, 107))
631 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
633 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
640 tsetscroll(int t
, int b
) {
643 LIMIT(t
, 0, term
.row
-1);
644 LIMIT(b
, 0, term
.row
-1);
656 switch(escseq
.mode
) {
659 printf("erresc: unknown csi ");
663 case '@': /* ICH -- Insert <n> blank char */
664 DEFAULT(escseq
.arg
[0], 1);
665 tinsertblank(escseq
.arg
[0]);
667 case 'A': /* CUU -- Cursor <n> Up */
669 DEFAULT(escseq
.arg
[0], 1);
670 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
672 case 'B': /* CUD -- Cursor <n> Down */
673 DEFAULT(escseq
.arg
[0], 1);
674 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
676 case 'C': /* CUF -- Cursor <n> Forward */
678 DEFAULT(escseq
.arg
[0], 1);
679 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
681 case 'D': /* CUB -- Cursor <n> Backward */
682 DEFAULT(escseq
.arg
[0], 1);
683 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
685 case 'E': /* CNL -- Cursor <n> Down and first col */
686 DEFAULT(escseq
.arg
[0], 1);
687 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
689 case 'F': /* CPL -- Cursor <n> Up and first col */
690 DEFAULT(escseq
.arg
[0], 1);
691 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
693 case 'G': /* CHA -- Move to <col> */
694 case '`': /* XXX: HPA -- same? */
695 DEFAULT(escseq
.arg
[0], 1);
696 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
698 case 'H': /* CUP -- Move to <row> <col> */
699 case 'f': /* XXX: HVP -- same? */
700 DEFAULT(escseq
.arg
[0], 1);
701 DEFAULT(escseq
.arg
[1], 1);
702 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
704 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
705 case 'J': /* ED -- Clear screen */
706 switch(escseq
.arg
[0]) {
708 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
711 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
714 tclearregion(0, 0, term
.col
-1, term
.row
-1);
716 case 3: /* XXX: erase saved lines (xterm) */
721 case 'K': /* EL -- Clear line */
722 switch(escseq
.arg
[0]) {
724 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
727 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
730 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
734 case 'S': /* SU -- Scroll <n> line up */
735 DEFAULT(escseq
.arg
[0], 1);
736 tscrollup(escseq
.arg
[0]);
738 case 'T': /* SD -- Scroll <n> line down */
739 DEFAULT(escseq
.arg
[0], 1);
740 tscrolldown(escseq
.arg
[0]);
742 case 'L': /* IL -- Insert <n> blank lines */
743 DEFAULT(escseq
.arg
[0], 1);
744 tinsertblankline(escseq
.arg
[0]);
746 case 'l': /* RM -- Reset Mode */
748 switch(escseq
.arg
[0]) {
750 term
.mode
&= ~MODE_APPKEYPAD
;
753 term
.mode
&= ~MODE_WRAP
;
755 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
760 case 1048: /* XXX: no alt. screen to erase/save */
762 tcursor(CURSOR_LOAD
);
763 tclearregion(0, 0, term
.col
-1, term
.row
-1);
769 switch(escseq
.arg
[0]) {
771 term
.mode
&= ~MODE_INSERT
;
778 case 'M': /* DL -- Delete <n> lines */
779 DEFAULT(escseq
.arg
[0], 1);
780 tdeleteline(escseq
.arg
[0]);
782 case 'X': /* ECH -- Erase <n> char */
783 DEFAULT(escseq
.arg
[0], 1);
784 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
786 case 'P': /* DCH -- Delete <n> char */
787 DEFAULT(escseq
.arg
[0], 1);
788 tdeletechar(escseq
.arg
[0]);
790 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
791 case 'd': /* VPA -- Move to <row> */
792 DEFAULT(escseq
.arg
[0], 1);
793 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
795 case 'h': /* SM -- Set terminal mode */
797 switch(escseq
.arg
[0]) {
799 term
.mode
|= MODE_APPKEYPAD
;
802 term
.mode
|= MODE_WRAP
;
804 case 12: /* att610 -- Start blinking cursor (IGNORED) */
810 case 1049: /* XXX: no alt. screen to erase/save */
811 tcursor(CURSOR_SAVE
);
812 tclearregion(0, 0, term
.col
-1, term
.row
-1);
814 default: goto unknown
;
817 switch(escseq
.arg
[0]) {
819 term
.mode
|= MODE_INSERT
;
821 default: goto unknown
;
825 case 'm': /* SGR -- Terminal attribute (color) */
826 tsetattr(escseq
.arg
, escseq
.narg
);
828 case 'r': /* DECSTBM -- Set Scrolling Region */
832 DEFAULT(escseq
.arg
[0], 1);
833 DEFAULT(escseq
.arg
[1], term
.row
);
834 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
837 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
838 tcursor(CURSOR_SAVE
);
840 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
841 tcursor(CURSOR_LOAD
);
849 printf("ESC [ %s", escseq
.priv
? "? " : "");
851 for(i
= 0; i
< escseq
.narg
; i
++)
852 printf("%d ", escseq
.arg
[i
]);
854 putchar(escseq
.mode
);
860 memset(&escseq
, 0, sizeof(escseq
));
865 int space
= TAB
- term
.c
.x
% TAB
;
866 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
871 if(term
.esc
& ESC_START
) {
872 if(term
.esc
& ESC_CSI
) {
873 escseq
.buf
[escseq
.len
++] = c
;
874 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
876 csiparse(), csihandle();
878 } else if(term
.esc
& ESC_OSC
) {
881 term
.esc
= ESC_START
| ESC_TITLE
;
883 } else if(term
.esc
& ESC_TITLE
) {
884 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
886 term
.title
[term
.titlelen
] = '\0';
887 XStoreName(xw
.dis
, xw
.win
, term
.title
);
889 term
.title
[term
.titlelen
++] = c
;
891 } else if(term
.esc
& ESC_ALTCHARSET
) {
893 case '0': /* Line drawing crap */
894 term
.c
.attr
.mode
|= ATTR_GFX
;
896 case 'B': /* Back to regular text */
897 term
.c
.attr
.mode
&= ~ATTR_GFX
;
900 printf("esc unhandled charset: ESC ( %c\n", c
);
912 term
.esc
|= ESC_ALTCHARSET
;
915 tmoveto(term
.c
.x
, term
.c
.y
-1);
919 tmoveto(term
.c
.x
, term
.c
.y
+1);
923 tmoveto(term
.c
.x
+1, term
.c
.y
);
926 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
927 tmoveto(term
.c
.x
-1, term
.c
.y
);
930 case 'E': /* NEL -- Next line */
934 case 'M': /* RI -- Reverse index */
935 if(term
.c
.y
== term
.top
)
938 tmoveto(term
.c
.x
, term
.c
.y
-1);
941 case 'c': /* RIS -- Reset to inital state */
945 case '=': /* DECPAM */
946 term
.mode
|= MODE_APPKEYPAD
;
949 case '>': /* DECPNM */
950 term
.mode
&= ~MODE_APPKEYPAD
;
954 tcursor(CURSOR_SAVE
);
958 tcursor(CURSOR_LOAD
);
962 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
972 tmovecursor(CURSOR_LEFT
);
975 tmoveto(0, term
.c
.y
);
985 term
.esc
= ESC_START
;
989 tmovecursor(CURSOR_RIGHT
);
996 tputs(char *s
, int len
) {
997 for(; len
> 0; len
--)
1002 tresize(int col
, int row
) {
1004 int minrow
= MIN(row
, term
.row
);
1005 int mincol
= MIN(col
, term
.col
);
1007 if(col
< 1 || row
< 1)
1010 for(i
= row
; i
< term
.row
; i
++)
1012 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1013 for(i
= 0; i
< minrow
; i
++) {
1014 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1015 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1017 for(/* i == minrow */; i
< row
; i
++)
1018 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1020 LIMIT(term
.c
.x
, 0, col
-1);
1021 LIMIT(term
.c
.y
, 0, row
-1);
1022 LIMIT(term
.top
, 0, row
-1);
1023 LIMIT(term
.bot
, 0, row
-1);
1026 term
.col
= col
, term
.row
= row
;
1033 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1034 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1036 for(i
= 0; i
< 16; i
++) {
1037 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1039 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1041 dc
.col
[i
] = color
.pixel
;
1044 /* same colors as xterm */
1045 for(r
= 0; r
< 6; r
++)
1046 for(g
= 0; g
< 6; g
++)
1047 for(b
= 0; b
< 6; b
++) {
1048 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1049 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1050 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1051 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1053 fprintf(stderr
, "Could not allocate color %d\n", i
);
1055 dc
.col
[i
] = color
.pixel
;
1059 for(r
= 0; r
< 24; r
++, i
++) {
1060 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1061 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1063 fprintf(stderr
, "Could not allocate color %d\n", i
);
1065 dc
.col
[i
] = color
.pixel
;
1070 xclear(int x1
, int y1
, int x2
, int y2
) {
1071 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1072 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1073 x1
* xw
.cw
, y1
* xw
.ch
,
1074 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1080 XClassHint
class = {TNAME
, TNAME
};
1081 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1083 .flags
= PSize
| PResizeInc
| PBaseSize
,
1086 .height_inc
= xw
.ch
,
1088 .base_height
= 2*BORDER
,
1089 .base_width
= 2*BORDER
,
1091 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1096 xw
.dis
= XOpenDisplay(NULL
);
1097 xw
.scr
= XDefaultScreen(xw
.dis
);
1099 die("Can't open display\n");
1102 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1103 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1105 /* XXX: Assuming same size for bold font */
1106 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1107 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1112 term
.c
.attr
.fg
= DefaultFG
;
1113 term
.c
.attr
.bg
= DefaultBG
;
1114 term
.c
.attr
.mode
= ATTR_NULL
;
1116 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1117 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1118 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1122 xw
.bufw
= xw
.w
- 2*BORDER
;
1123 xw
.bufh
= xw
.h
- 2*BORDER
;
1124 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1126 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1127 XMapWindow(xw
.dis
, xw
.win
);
1129 XStoreName(xw
.dis
, xw
.win
, "st");
1134 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1135 unsigned long xfg
, xbg
;
1136 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1139 if(base
.mode
& ATTR_REVERSE
)
1140 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1142 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1144 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1145 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1147 if(base
.mode
& ATTR_GFX
)
1148 for(i
= 0; i
< len
; i
++)
1149 s
[i
] = gfx
[(int)s
[i
]];
1151 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1152 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1154 if(base
.mode
& ATTR_UNDERLINE
)
1155 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1160 static int oldx
= 0;
1161 static int oldy
= 0;
1162 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1164 LIMIT(oldx
, 0, term
.col
-1);
1165 LIMIT(oldy
, 0, term
.row
-1);
1167 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1168 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1170 /* remove the old cursor */
1171 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1172 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1174 xclear(oldx
, oldy
, oldx
, oldy
);
1176 /* draw the new one */
1177 if(mode
== CURSOR_DRAW
) {
1178 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1179 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1184 /* basic drawing routines */
1186 xdrawc(int x
, int y
, Glyph g
) {
1187 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1188 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1189 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1190 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1191 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1198 xclear(0, 0, term
.col
-1, term
.row
-1);
1199 for(y
= 0; y
< term
.row
; y
++)
1200 for(x
= 0; x
< term
.col
; x
++)
1201 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1202 xdrawc(x
, y
, term
.line
[y
][x
]);
1205 xcursor(CURSOR_DRAW
);
1206 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1211 /* optimized drawing routine */
1213 draw(int redraw_all
) {
1216 char buf
[DRAW_BUF_SIZ
];
1218 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1219 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1220 for(y
= 0; y
< term
.row
; y
++) {
1221 base
= term
.line
[y
][0];
1223 for(x
= 0; x
< term
.col
; x
++) {
1224 new = term
.line
[y
][x
];
1225 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1228 xdraws(buf
, base
, ox
, y
, i
);
1235 xdraws(buf
, base
, ox
, y
, i
);
1237 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1238 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1245 expose(XEvent
*ev
) {
1246 draw(SCREEN_REDRAW
);
1252 for(i
= 0; i
< LEN(key
); i
++)
1254 return (char*)key
[i
].s
;
1259 kpress(XEvent
*ev
) {
1260 XKeyEvent
*e
= &ev
->xkey
;
1268 meta
= e
->state
& Mod1Mask
;
1269 shift
= e
->state
& ShiftMask
;
1270 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1272 if((customkey
= kmap(ksym
)))
1273 ttywrite(customkey
, strlen(customkey
));
1275 buf
[sizeof(buf
)-1] = '\0';
1276 if(meta
&& len
== 1)
1277 ttywrite("\033", 1);
1285 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1290 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1293 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1302 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1305 xw
.w
= e
->xconfigure
.width
;
1306 xw
.h
= e
->xconfigure
.height
;
1307 xw
.bufw
= xw
.w
- 2*BORDER
;
1308 xw
.bufh
= xw
.h
- 2*BORDER
;
1309 col
= xw
.bufw
/ xw
.cw
;
1310 row
= xw
.bufh
/ xw
.ch
;
1312 ttyresize(col
, row
);
1313 XFreePixmap(xw
.dis
, xw
.buf
);
1314 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1315 draw(SCREEN_REDRAW
);
1322 int xfd
= XConnectionNumber(xw
.dis
);
1325 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1326 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1330 FD_SET(cmdfd
, &rfd
);
1332 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1335 die("select failed: %s\n", SERRNO
);
1337 if(FD_ISSET(cmdfd
, &rfd
)) {
1339 draw(SCREEN_UPDATE
);
1341 while(XPending(xw
.dis
)) {
1342 XNextEvent(xw
.dis
, &ev
);
1343 if(handler
[ev
.type
])
1344 (handler
[ev
.type
])(&ev
);
1350 main(int argc
, char *argv
[]) {
1351 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1352 die("st-" VERSION
", © 2009 st engineers\n");
1354 die("usage: st [-v]\n");
1355 setlocale(LC_CTYPE
, "");