Xinqi Bao's Git
1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
20 #include <X11/keysym.h>
21 #include <X11/Xutil.h>
26 #define ESC_TITLE_SIZ 256
27 #define ESC_BUF_SIZ 256
28 #define ESC_ARG_SIZ 16
29 #define DRAW_BUF_SIZ 1024
31 #define SERRNO strerror(errno)
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33 #define MAX(a, b) ((a) < (b) ? (b) : (a))
34 #define LEN(a) (sizeof(a) / sizeof(a[0]))
35 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
36 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
37 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
38 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
40 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
41 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
42 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
, CURSOR_HIDE
, CURSOR_DRAW
,
43 CURSOR_SAVE
, CURSOR_LOAD
};
44 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
45 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4 };
46 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
47 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
50 char c
; /* character code */
51 char mode
; /* attribute flags */
52 int fg
; /* foreground */
53 int bg
; /* background */
54 char state
; /* state flags */
60 Glyph attr
; /* current char attributes */
65 /* CSI Escape sequence structs */
66 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
68 char buf
[ESC_BUF_SIZ
]; /* raw string */
69 int len
; /* raw string length */
72 int narg
; /* nb of args */
76 /* Internal representation of the screen */
80 Line
* line
; /* screen */
81 TCursor c
; /* cursor */
83 int top
; /* top scroll limit */
84 int bot
; /* bottom scroll limit */
85 int mode
; /* terminal mode flags */
86 int esc
; /* escape state flags */
87 char title
[ESC_TITLE_SIZ
];
91 /* Purely graphic info */
97 int w
; /* window width */
98 int h
; /* window height */
99 int bufw
; /* pixmap width */
100 int bufh
; /* pixmap height */
101 int ch
; /* char height */
102 int cw
; /* char width */
112 /* Drawing Context */
114 unsigned long col
[LEN(colorname
)];
120 static void die(const char *errstr
, ...);
121 static void draw(int);
122 static void execsh(void);
123 static void sigchld(int);
124 static void run(void);
126 static void csidump(void);
127 static void csihandle(void);
128 static void csiparse(void);
129 static void csireset(void);
131 static void tclearregion(int, int, int, int);
132 static void tcursor(int);
133 static void tmovecursor(int);
134 static void tdeletechar(int);
135 static void tdeleteline(int);
136 static void tinsertblank(int);
137 static void tinsertblankline(int);
138 static void tmoveto(int, int);
139 static void tnew(int, int);
140 static void tnewline(void);
141 static void tputc(char);
142 static void tputs(char*, int);
143 static void treset(void);
144 static void tresize(int, int);
145 static void tscroll(void);
146 static void tscrollup(int);
147 static void tscrolldown(int);
148 static void tsetattr(int*, int);
149 static void tsetchar(char);
150 static void tsetscroll(int, int);
152 static void ttynew(void);
153 static void ttyread(void);
154 static void ttyresize(int, int);
155 static void ttywrite(const char *, size_t);
157 static unsigned long xgetcol(const char *);
158 static void xclear(int, int, int, int);
159 static void xcursor(int);
160 static void xinit(void);
162 static void expose(XEvent
*);
163 static char* kmap(KeySym
);
164 static void kpress(XEvent
*);
165 static void resize(XEvent
*);
167 static void (*handler
[LASTEvent
])(XEvent
*) = {
170 [ConfigureNotify
] = resize
177 static CSIEscape escseq
;
188 for(row
= 0; row
< term
.row
; row
++) {
189 for(col
= 0; col
< term
.col
; col
++) {
190 if(col
== term
.c
.x
&& row
== term
.c
.y
)
193 c
= term
.line
[row
][col
];
194 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
203 die(const char *errstr
, ...) {
206 va_start(ap
, errstr
);
207 vfprintf(stderr
, errstr
, ap
);
214 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
215 DEFAULT(args
[0], "/bin/sh"); /* default shell if getenv() failed */
216 putenv("TERM=" TNAME
);
217 execvp(args
[0], args
);
221 xbell(void) { /* visual bell */
222 XRectangle r
= { BORDER
, BORDER
, xw
.bufw
, xw
.bufh
};
223 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
224 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
232 if(waitpid(pid
, &stat
, 0) < 0)
233 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
235 exit(WEXITSTATUS(stat
));
245 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
246 die("openpt failed: %s\n", SERRNO
);
248 die("grandpt failed: %s\n", SERRNO
);
250 die("unlockpt failed: %s\n", SERRNO
);
251 if(!(pts
= ptsname(m
)))
252 die("ptsname failed: %s\n", SERRNO
);
253 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
254 die("Couldn't open slave: %s\n", SERRNO
);
255 fcntl(s
, F_SETFL
, O_NDELAY
);
256 switch(pid
= fork()) {
258 die("fork failed\n");
261 setsid(); /* create a new process group */
262 dup2(s
, STDIN_FILENO
);
263 dup2(s
, STDOUT_FILENO
);
264 dup2(s
, STDERR_FILENO
);
265 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
266 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
272 signal(SIGCHLD
, sigchld
);
279 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
281 fprintf(stderr
, "\n");
286 char buf
[BUFSIZ
] = {0};
289 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
290 die("Couldn't read from shell: %s\n", SERRNO
);
296 ttywrite(const char *s
, size_t n
) {
297 if(write(cmdfd
, s
, n
) == -1)
298 die("write error on tty: %s\n", SERRNO
);
302 ttyresize(int x
, int y
) {
307 w
.ws_xpixel
= w
.ws_ypixel
= 0;
308 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
309 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
316 if(mode
== CURSOR_SAVE
)
318 else if(mode
== CURSOR_LOAD
)
319 term
.c
= c
, tmoveto(c
.x
, c
.y
);
324 term
.c
.attr
.mode
= ATTR_NULL
;
325 term
.c
.attr
.fg
= DefaultFG
;
326 term
.c
.attr
.bg
= DefaultBG
;
327 term
.c
.x
= term
.c
.y
= 0;
329 term
.top
= 0, term
.bot
= term
.row
- 1;
330 term
.mode
= MODE_WRAP
;
331 tclearregion(0, 0, term
.col
-1, term
.row
-1);
335 tnew(int col
, int row
) { /* screen size */
336 term
.row
= row
, term
.col
= col
;
337 term
.top
= 0, term
.bot
= term
.row
- 1;
339 term
.mode
= MODE_WRAP
;
341 term
.c
.attr
.mode
= ATTR_NULL
;
342 term
.c
.attr
.fg
= DefaultFG
;
343 term
.c
.attr
.bg
= DefaultBG
;
344 term
.c
.x
= term
.c
.y
= 0;
346 /* allocate screen */
347 term
.line
= calloc(term
.row
, sizeof(Line
));
348 for(row
= 0 ; row
< term
.row
; row
++)
349 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
352 /* TODO: Replace with scrollup/scolldown */
355 Line temp
= term
.line
[term
.top
];
358 for(i
= term
.top
; i
< term
.bot
; i
++)
359 term
.line
[i
] = term
.line
[i
+1];
360 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
361 term
.line
[term
.bot
] = temp
;
365 tscrolldown (int n
) {
369 LIMIT(n
, 0, term
.bot
-term
.top
+1);
371 for(i
= 0; i
< n
; i
++)
372 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
374 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
376 term
.line
[i
] = term
.line
[i
-n
];
377 term
.line
[i
-n
] = temp
;
385 LIMIT(n
, 0, term
.bot
-term
.top
+1);
387 for(i
= 0; i
< n
; i
++)
388 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
390 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
392 term
.line
[i
] = term
.line
[i
+n
];
393 term
.line
[i
+n
] = temp
;
399 int y
= term
.c
.y
+ 1;
401 tscroll(), y
= term
.bot
;
408 char *p
= escseq
.buf
;
412 escseq
.priv
= 1, p
++;
414 while(p
< escseq
.buf
+escseq
.len
) {
416 escseq
.arg
[escseq
.narg
] *= 10;
417 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
419 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
430 tmoveto(int x
, int y
) {
431 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
432 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
436 tmovecursor(int dir
) {
437 int xf
= term
.c
.x
, yf
= term
.c
.y
;
448 if(term
.mode
& MODE_WRAP
&& xf
< 0) {
449 xf
= term
.col
-1, yf
--;
451 yf
= term
.top
, xf
= 0;
456 if(term
.mode
& MODE_WRAP
&& xf
>= term
.col
) {
459 yf
= term
.bot
, tscroll();
468 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
469 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
470 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
474 tclearregion(int x1
, int y1
, int x2
, int y2
) {
478 temp
= x1
, x1
= x2
, x2
= temp
;
480 temp
= y1
, y1
= y2
, y2
= temp
;
482 LIMIT(x1
, 0, term
.col
-1);
483 LIMIT(x2
, 0, term
.col
-1);
484 LIMIT(y1
, 0, term
.row
-1);
485 LIMIT(y2
, 0, term
.row
-1);
487 for(y
= y1
; y
<= y2
; y
++)
488 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
493 int src
= term
.c
.x
+ n
;
495 int size
= term
.col
- src
;
497 if(src
>= term
.col
) {
498 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
501 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
502 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
506 tinsertblank(int n
) {
509 int size
= term
.col
- n
- src
;
511 if(dst
>= term
.col
) {
512 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
515 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
516 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
520 tinsertblankline(int n
) {
525 if(term
.c
.y
> term
.bot
)
527 else if(term
.c
.y
< term
.top
)
529 if(term
.c
.y
+ n
>= bot
) {
530 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
533 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
534 /* swap deleted line <-> blanked line */
535 blank
= term
.line
[i
];
536 term
.line
[i
] = term
.line
[i
-n
];
537 term
.line
[i
-n
] = blank
;
539 memset(blank
, 0, term
.col
* sizeof(Glyph
));
549 if(term
.c
.y
> term
.bot
)
551 else if(term
.c
.y
< term
.top
)
553 if(term
.c
.y
+ n
>= bot
) {
554 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
557 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
558 /* swap deleted line <-> blanked line */
559 blank
= term
.line
[i
];
560 term
.line
[i
] = term
.line
[i
+n
];
561 term
.line
[i
+n
] = blank
;
563 memset(blank
, 0, term
.col
* sizeof(Glyph
));
568 tsetattr(int *attr
, int l
) {
571 for(i
= 0; i
< l
; i
++) {
574 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
575 term
.c
.attr
.fg
= DefaultFG
;
576 term
.c
.attr
.bg
= DefaultBG
;
579 term
.c
.attr
.mode
|= ATTR_BOLD
;
582 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
585 term
.c
.attr
.mode
|= ATTR_REVERSE
;
588 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
591 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
594 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
597 term
.c
.attr
.fg
= DefaultFG
;
600 term
.c
.attr
.bg
= DefaultBG
;
603 if(BETWEEN(attr
[i
], 30, 37))
604 term
.c
.attr
.fg
= attr
[i
] - 30;
605 else if(BETWEEN(attr
[i
], 40, 47))
606 term
.c
.attr
.bg
= attr
[i
] - 40;
608 fprintf(stderr
, "erresc: gfx attr %d unkown\n", attr
[i
]);
615 tsetscroll(int t
, int b
) {
618 LIMIT(t
, 0, term
.row
-1);
619 LIMIT(b
, 0, term
.row
-1);
631 switch(escseq
.mode
) {
634 printf("erresc: unknown csi ");
638 case '@': /* ICH -- Insert <n> blank char */
639 DEFAULT(escseq
.arg
[0], 1);
640 tinsertblank(escseq
.arg
[0]);
642 case 'A': /* CUU -- Cursor <n> Up */
644 DEFAULT(escseq
.arg
[0], 1);
645 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
647 case 'B': /* CUD -- Cursor <n> Down */
648 DEFAULT(escseq
.arg
[0], 1);
649 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
651 case 'C': /* CUF -- Cursor <n> Forward */
653 DEFAULT(escseq
.arg
[0], 1);
654 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
656 case 'D': /* CUB -- Cursor <n> Backward */
657 DEFAULT(escseq
.arg
[0], 1);
658 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
660 case 'E': /* CNL -- Cursor <n> Down and first col */
661 DEFAULT(escseq
.arg
[0], 1);
662 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
664 case 'F': /* CPL -- Cursor <n> Up and first col */
665 DEFAULT(escseq
.arg
[0], 1);
666 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
668 case 'G': /* CHA -- Move to <col> */
669 case '`': /* XXX: HPA -- same? */
670 DEFAULT(escseq
.arg
[0], 1);
671 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
673 case 'H': /* CUP -- Move to <row> <col> */
674 case 'f': /* XXX: HVP -- same? */
675 DEFAULT(escseq
.arg
[0], 1);
676 DEFAULT(escseq
.arg
[1], 1);
677 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
679 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
680 case 'J': /* ED -- Clear screen */
681 switch(escseq
.arg
[0]) {
683 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
686 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
689 tclearregion(0, 0, term
.col
-1, term
.row
-1);
691 case 3: /* XXX: erase saved lines (xterm) */
696 case 'K': /* EL -- Clear line */
697 switch(escseq
.arg
[0]) {
699 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
702 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
705 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
709 case 'S': /* SU -- Scroll <n> line up */
710 DEFAULT(escseq
.arg
[0], 1);
711 tscrollup(escseq
.arg
[0]);
713 case 'T': /* SD -- Scroll <n> line down */
714 DEFAULT(escseq
.arg
[0], 1);
715 tscrolldown(escseq
.arg
[0]);
717 case 'L': /* IL -- Insert <n> blank lines */
718 DEFAULT(escseq
.arg
[0], 1);
719 tinsertblankline(escseq
.arg
[0]);
721 case 'l': /* RM -- Reset Mode */
723 switch(escseq
.arg
[0]) {
725 term
.mode
&= ~MODE_APPKEYPAD
;
728 term
.mode
&= ~MODE_WRAP
;
730 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
735 case 1048: /* XXX: no alt. screen to erase/save */
737 tcursor(CURSOR_LOAD
);
738 tclearregion(0, 0, term
.col
-1, term
.row
-1);
744 switch(escseq
.arg
[0]) {
746 term
.mode
&= ~MODE_INSERT
;
753 case 'M': /* DL -- Delete <n> lines */
754 DEFAULT(escseq
.arg
[0], 1);
755 tdeleteline(escseq
.arg
[0]);
757 case 'X': /* ECH -- Erase <n> char */
758 DEFAULT(escseq
.arg
[0], 1);
759 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
761 case 'P': /* DCH -- Delete <n> char */
762 DEFAULT(escseq
.arg
[0], 1);
763 tdeletechar(escseq
.arg
[0]);
765 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
766 case 'd': /* VPA -- Move to <row> */
767 DEFAULT(escseq
.arg
[0], 1);
768 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
770 case 'h': /* SM -- Set terminal mode */
772 switch(escseq
.arg
[0]) {
774 term
.mode
|= MODE_APPKEYPAD
;
777 term
.mode
|= MODE_WRAP
;
779 case 12: /* att610 -- Start blinking cursor (IGNORED) */
785 case 1049: /* XXX: no alt. screen to erase/save */
786 tcursor(CURSOR_SAVE
);
787 tclearregion(0, 0, term
.col
-1, term
.row
-1);
789 default: goto unknown
;
792 switch(escseq
.arg
[0]) {
794 term
.mode
|= MODE_INSERT
;
796 default: goto unknown
;
800 case 'm': /* SGR -- Terminal attribute (color) */
801 tsetattr(escseq
.arg
, escseq
.narg
);
803 case 'r': /* DECSTBM -- Set Scrolling Region */
807 DEFAULT(escseq
.arg
[0], 1);
808 DEFAULT(escseq
.arg
[1], term
.row
);
809 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
812 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
813 tcursor(CURSOR_SAVE
);
815 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
816 tcursor(CURSOR_LOAD
);
824 printf("ESC [ %s", escseq
.priv
? "? " : "");
826 for(i
= 0; i
< escseq
.narg
; i
++)
827 printf("%d ", escseq
.arg
[i
]);
829 putchar(escseq
.mode
);
835 memset(&escseq
, 0, sizeof(escseq
));
840 int space
= TAB
- term
.c
.x
% TAB
;
842 if(term
.c
.x
+ space
>= term
.col
)
845 for(; space
> 0; space
--)
846 tmovecursor(CURSOR_RIGHT
);
851 if(term
.esc
& ESC_START
) {
852 if(term
.esc
& ESC_CSI
) {
853 escseq
.buf
[escseq
.len
++] = c
;
854 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
856 csiparse(), csihandle();
858 } else if(term
.esc
& ESC_OSC
) {
861 term
.esc
= ESC_START
| ESC_TITLE
;
863 } else if(term
.esc
& ESC_TITLE
) {
864 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
866 term
.title
[term
.titlelen
] = '\0';
867 XStoreName(xw
.dis
, xw
.win
, term
.title
);
869 term
.title
[term
.titlelen
++] = c
;
871 } else if(term
.esc
& ESC_ALTCHARSET
) {
873 case '0': /* Line drawing crap */
874 term
.c
.attr
.mode
|= ATTR_GFX
;
876 case 'B': /* Back to regular text */
877 term
.c
.attr
.mode
&= ~ATTR_GFX
;
880 printf("esc unhandled charset: ESC ( %c\n", c
);
892 term
.esc
|= ESC_ALTCHARSET
;
895 tmoveto(term
.c
.x
, term
.c
.y
-1);
899 tmoveto(term
.c
.x
, term
.c
.y
+1);
903 tmoveto(term
.c
.x
+1, term
.c
.y
);
906 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
907 tmoveto(term
.c
.x
-1, term
.c
.y
);
910 case 'E': /* NEL -- Next line */
914 case 'M': /* RI -- Reverse index */
915 if(term
.c
.y
== term
.top
)
918 tmoveto(term
.c
.x
, term
.c
.y
-1);
921 case 'c': /* RIS -- Reset to inital state */
925 case '=': /* DECPAM */
926 term
.mode
|= MODE_APPKEYPAD
;
929 case '>': /* DECPNM */
930 term
.mode
&= ~MODE_APPKEYPAD
;
934 tcursor(CURSOR_SAVE
);
938 tcursor(CURSOR_LOAD
);
942 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
952 tmovecursor(CURSOR_LEFT
);
955 tmoveto(0, term
.c
.y
);
965 term
.esc
= ESC_START
;
969 tmovecursor(CURSOR_RIGHT
);
976 tputs(char *s
, int len
) {
977 for(; len
> 0; len
--)
982 tresize(int col
, int row
) {
985 int minrow
= MIN(row
, term
.row
);
986 int mincol
= MIN(col
, term
.col
);
988 if(col
< 1 || row
< 1)
991 line
= calloc(row
, sizeof(Line
));
992 for(i
= 0 ; i
< row
; i
++)
993 line
[i
] = calloc(col
, sizeof(Glyph
));
995 for(i
= 0 ; i
< minrow
; i
++)
996 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
998 for(i
= 0; i
< term
.row
; i
++)
1002 LIMIT(term
.c
.x
, 0, col
-1);
1003 LIMIT(term
.c
.y
, 0, row
-1);
1004 LIMIT(term
.top
, 0, row
-1);
1005 LIMIT(term
.bot
, 0, row
-1);
1009 term
.col
= col
, term
.row
= row
;
1013 xgetcol(const char *s
) {
1015 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1017 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
1018 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
1019 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
1025 xclear(int x1
, int y1
, int x2
, int y2
) {
1026 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1027 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1028 x1
* xw
.cw
, y1
* xw
.ch
,
1029 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1035 XClassHint
class = {TNAME
, TNAME
};
1036 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1038 .flags
= PSize
| PResizeInc
| PBaseSize
,
1041 .height_inc
= xw
.ch
,
1043 .base_height
= 2*BORDER
,
1044 .base_width
= 2*BORDER
,
1046 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1053 xw
.dis
= XOpenDisplay(NULL
);
1054 xw
.scr
= XDefaultScreen(xw
.dis
);
1056 die("Can't open display\n");
1059 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1060 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1062 /* XXX: Assuming same size for bold font */
1063 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1064 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1067 for(i
= 0; i
< LEN(colorname
); i
++)
1068 dc
.col
[i
] = xgetcol(colorname
[i
]);
1070 term
.c
.attr
.fg
= DefaultFG
;
1071 term
.c
.attr
.bg
= DefaultBG
;
1072 term
.c
.attr
.mode
= ATTR_NULL
;
1074 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1075 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1076 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1080 xw
.bufw
= xw
.w
- 2*BORDER
;
1081 xw
.bufh
= xw
.h
- 2*BORDER
;
1082 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1084 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1085 XMapWindow(xw
.dis
, xw
.win
);
1087 XStoreName(xw
.dis
, xw
.win
, "st");
1092 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1093 unsigned long xfg
, xbg
;
1094 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1097 if(base
.mode
& ATTR_REVERSE
)
1098 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1100 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1102 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1103 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1105 if(base
.mode
& ATTR_GFX
)
1106 for(i
= 0; i
< len
; i
++)
1109 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1110 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1112 if(base
.mode
& ATTR_UNDERLINE
)
1113 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1118 static int oldx
= 0;
1119 static int oldy
= 0;
1120 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1122 LIMIT(oldx
, 0, term
.col
-1);
1123 LIMIT(oldy
, 0, term
.row
-1);
1125 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1126 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1128 /* remove the old cursor */
1129 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1130 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1132 xclear(oldx
, oldy
, oldx
, oldy
);
1134 /* draw the new one */
1135 if(mode
== CURSOR_DRAW
) {
1136 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1137 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1142 /* basic drawing routines */
1144 xdrawc(int x
, int y
, Glyph g
) {
1145 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1146 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1147 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1148 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1149 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1156 xclear(0, 0, term
.col
-1, term
.row
-1);
1157 for(y
= 0; y
< term
.row
; y
++)
1158 for(x
= 0; x
< term
.col
; x
++)
1159 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1160 xdrawc(x
, y
, term
.line
[y
][x
]);
1163 xcursor(CURSOR_DRAW
);
1164 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1169 /* optimized drawing routine */
1171 draw(int redraw_all
) {
1174 char buf
[DRAW_BUF_SIZ
];
1176 for(y
= 0; y
< term
.row
; y
++) {
1177 base
= term
.line
[y
][0];
1179 for(x
= 0; x
< term
.col
; x
++) {
1180 new = term
.line
[y
][x
];
1181 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1184 xdraws(buf
, base
, ox
, y
, i
);
1191 xdraws(buf
, base
, ox
, y
, i
);
1193 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1194 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1201 expose(XEvent
*ev
) {
1202 draw(SCREEN_REDRAW
);
1208 for(i
= 0; i
< LEN(key
); i
++)
1210 return (char*)key
[i
].s
;
1215 kpress(XEvent
*ev
) {
1216 XKeyEvent
*e
= &ev
->xkey
;
1224 meta
= e
->state
& Mod1Mask
;
1225 shift
= e
->state
& ShiftMask
;
1226 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1228 if(customkey
= kmap(ksym
))
1229 ttywrite(customkey
, strlen(customkey
));
1231 buf
[sizeof(buf
)-1] = '\0';
1232 if(meta
&& len
== 1)
1233 ttywrite("\033", 1);
1241 sprintf(buf
, "\033%c%c", term
.mode
& MODE_APPKEYPAD
? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1246 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1249 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1258 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1261 xw
.w
= e
->xconfigure
.width
;
1262 xw
.h
= e
->xconfigure
.height
;
1263 xw
.bufw
= xw
.w
- 2*BORDER
;
1264 xw
.bufh
= xw
.h
- 2*BORDER
;
1265 col
= xw
.bufw
/ xw
.cw
;
1266 row
= xw
.bufh
/ xw
.ch
;
1268 ttyresize(col
, row
);
1269 XFreePixmap(xw
.dis
, xw
.buf
);
1270 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1271 draw(SCREEN_REDRAW
);
1278 int xfd
= XConnectionNumber(xw
.dis
);
1281 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1282 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1286 FD_SET(cmdfd
, &rfd
);
1288 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1291 die("select failed: %s\n", SERRNO
);
1293 if(FD_ISSET(cmdfd
, &rfd
)) {
1295 draw(SCREEN_UPDATE
);
1297 while(XPending(xw
.dis
)) {
1298 XNextEvent(xw
.dis
, &ev
);
1299 if(handler
[ev
.type
])
1300 (handler
[ev
.type
])(&ev
);
1306 main(int argc
, char *argv
[]) {
1307 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1308 die("st-" VERSION
", © 2009 st engineers\n");
1310 die("usage: st [-v]\n");
1311 setlocale(LC_CTYPE
, "");