Xinqi Bao's Git
9ca032f5a34a221787ca53235fc5021e9ffd2cb0
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>
25 #elif defined(OPENBSD)
27 #elif defined(FREEBSD)
32 #define ESC_TITLE_SIZ 256
33 #define ESC_BUF_SIZ 256
34 #define ESC_ARG_SIZ 16
35 #define DRAW_BUF_SIZ 1024
37 #define SERRNO strerror(errno)
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #define MAX(a, b) ((a) < (b) ? (b) : (a))
40 #define LEN(a) (sizeof(a) / sizeof(a[0]))
41 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
42 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
43 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
44 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
45 #define IS_SET(flag) (term.mode & (flag))
47 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
48 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
49 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
, CURSOR_HIDE
, CURSOR_DRAW
,
50 CURSOR_SAVE
, CURSOR_LOAD
};
51 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
52 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4 };
53 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
54 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
57 char c
; /* character code */
58 char mode
; /* attribute flags */
59 int fg
; /* foreground */
60 int bg
; /* background */
61 char state
; /* state flags */
67 Glyph attr
; /* current char attributes */
72 /* CSI Escape sequence structs */
73 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
75 char buf
[ESC_BUF_SIZ
]; /* raw string */
76 int len
; /* raw string length */
79 int narg
; /* nb of args */
83 /* Internal representation of the screen */
87 Line
* line
; /* screen */
88 TCursor c
; /* cursor */
90 int top
; /* top scroll limit */
91 int bot
; /* bottom scroll limit */
92 int mode
; /* terminal mode flags */
93 int esc
; /* escape state flags */
94 char title
[ESC_TITLE_SIZ
];
98 /* Purely graphic info */
104 int w
; /* window width */
105 int h
; /* window height */
106 int bufw
; /* pixmap width */
107 int bufh
; /* pixmap height */
108 int ch
; /* char height */
109 int cw
; /* char width */
117 /* Drawing Context */
119 unsigned long col
[256];
127 static void die(const char *errstr
, ...);
128 static void draw(int);
129 static void execsh(void);
130 static void sigchld(int);
131 static void run(void);
133 static void csidump(void);
134 static void csihandle(void);
135 static void csiparse(void);
136 static void csireset(void);
138 static void tclearregion(int, int, int, int);
139 static void tcursor(int);
140 static void tdeletechar(int);
141 static void tdeleteline(int);
142 static void tinsertblank(int);
143 static void tinsertblankline(int);
144 static void tmoveto(int, int);
145 static void tnew(int, int);
146 static void tnewline(void);
147 static void tputtab(void);
148 static void tputc(char);
149 static void tputs(char*, int);
150 static void treset(void);
151 static void tresize(int, int);
152 static void tscroll(void);
153 static void tscrollup(int);
154 static void tscrolldown(int);
155 static void tsetattr(int*, int);
156 static void tsetchar(char);
157 static void tsetscroll(int, int);
159 static void ttynew(void);
160 static void ttyread(void);
161 static void ttyresize(int, int);
162 static void ttywrite(const char *, size_t);
164 static void xbell(void);
165 static void xdraws(char *, Glyph
, int, int, int);
166 static void xhints(void);
167 static void xclear(int, int, int, int);
168 static void xcursor(int);
169 static void xinit(void);
170 static void xloadcols(void);
172 static void expose(XEvent
*);
173 static char* kmap(KeySym
);
174 static void kpress(XEvent
*);
175 static void resize(XEvent
*);
177 static void (*handler
[LASTEvent
])(XEvent
*) = {
180 [ConfigureNotify
] = resize
187 static CSIEscape escseq
;
198 for(row
= 0; row
< term
.row
; row
++) {
199 for(col
= 0; col
< term
.col
; col
++) {
200 if(col
== term
.c
.x
&& row
== term
.c
.y
)
203 c
= term
.line
[row
][col
];
204 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
213 die(const char *errstr
, ...) {
216 va_start(ap
, errstr
);
217 vfprintf(stderr
, errstr
, ap
);
224 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
225 DEFAULT(args
[0], "/bin/sh"); /* default shell if getenv() failed */
226 putenv("TERM=" TNAME
);
227 execvp(args
[0], args
);
231 xbell(void) { /* visual bell */
232 XRectangle r
= { BORDER
, BORDER
, xw
.bufw
, xw
.bufh
};
233 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
234 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
242 if(waitpid(pid
, &stat
, 0) < 0)
243 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
245 exit(WEXITSTATUS(stat
));
254 /* seems to work fine on linux, openbsd and freebsd */
255 struct winsize w
= {term
.row
, term
.col
, 0, 0};
256 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
257 die("openpty failed: %s\n", SERRNO
);
259 switch(pid
= fork()) {
261 die("fork failed\n");
264 setsid(); /* create a new process group */
265 dup2(s
, STDIN_FILENO
);
266 dup2(s
, STDOUT_FILENO
);
267 dup2(s
, STDERR_FILENO
);
268 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
269 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
277 signal(SIGCHLD
, sigchld
);
284 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
286 fprintf(stderr
, "\n");
291 char buf
[BUFSIZ
] = {0};
294 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
295 die("Couldn't read from shell: %s\n", SERRNO
);
301 ttywrite(const char *s
, size_t n
) {
302 if(write(cmdfd
, s
, n
) == -1)
303 die("write error on tty: %s\n", SERRNO
);
307 ttyresize(int x
, int y
) {
312 w
.ws_xpixel
= w
.ws_ypixel
= 0;
313 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
314 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
321 if(mode
== CURSOR_SAVE
)
323 else if(mode
== CURSOR_LOAD
)
324 term
.c
= c
, tmoveto(c
.x
, c
.y
);
329 term
.c
.attr
.mode
= ATTR_NULL
;
330 term
.c
.attr
.fg
= DefaultFG
;
331 term
.c
.attr
.bg
= DefaultBG
;
332 term
.c
.x
= term
.c
.y
= 0;
334 term
.top
= 0, term
.bot
= term
.row
- 1;
335 term
.mode
= MODE_WRAP
;
336 tclearregion(0, 0, term
.col
-1, term
.row
-1);
340 tnew(int col
, int row
) {
342 term
.row
= row
, term
.col
= col
;
343 term
.top
= 0, term
.bot
= term
.row
- 1;
345 term
.mode
= MODE_WRAP
;
347 term
.c
.attr
.mode
= ATTR_NULL
;
348 term
.c
.attr
.fg
= DefaultFG
;
349 term
.c
.attr
.bg
= DefaultBG
;
350 term
.c
.x
= term
.c
.y
= 0;
352 /* allocate screen */
353 term
.line
= calloc(term
.row
, sizeof(Line
));
354 for(row
= 0 ; row
< term
.row
; row
++)
355 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
358 /* TODO: Replace with scrollup/scolldown */
361 Line temp
= term
.line
[term
.top
];
364 for(i
= term
.top
; i
< term
.bot
; i
++)
365 term
.line
[i
] = term
.line
[i
+1];
366 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
367 term
.line
[term
.bot
] = temp
;
371 tscrolldown (int n
) {
375 LIMIT(n
, 0, term
.bot
-term
.top
+1);
377 for(i
= 0; i
< n
; i
++)
378 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
380 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
382 term
.line
[i
] = term
.line
[i
-n
];
383 term
.line
[i
-n
] = temp
;
391 LIMIT(n
, 0, term
.bot
-term
.top
+1);
393 for(i
= 0; i
< n
; i
++)
394 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
396 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
398 term
.line
[i
] = term
.line
[i
+n
];
399 term
.line
[i
+n
] = temp
;
405 int y
= term
.c
.y
+ 1;
407 tscroll(), y
= term
.bot
;
414 char *p
= escseq
.buf
;
418 escseq
.priv
= 1, p
++;
420 while(p
< escseq
.buf
+escseq
.len
) {
422 escseq
.arg
[escseq
.narg
] *= 10;
423 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
425 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
436 tmoveto(int x
, int y
) {
437 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
438 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
443 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
444 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
445 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
449 tclearregion(int x1
, int y1
, int x2
, int y2
) {
453 temp
= x1
, x1
= x2
, x2
= temp
;
455 temp
= y1
, y1
= y2
, y2
= temp
;
457 LIMIT(x1
, 0, term
.col
-1);
458 LIMIT(x2
, 0, term
.col
-1);
459 LIMIT(y1
, 0, term
.row
-1);
460 LIMIT(y2
, 0, term
.row
-1);
462 for(y
= y1
; y
<= y2
; y
++)
463 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
468 int src
= term
.c
.x
+ n
;
470 int size
= term
.col
- src
;
472 if(src
>= term
.col
) {
473 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
476 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
477 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
481 tinsertblank(int n
) {
484 int size
= term
.col
- dst
;
486 if(dst
>= term
.col
) {
487 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
490 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
491 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
495 tinsertblankline(int n
) {
500 if(term
.c
.y
> term
.bot
)
502 else if(term
.c
.y
< term
.top
)
504 if(term
.c
.y
+ n
>= bot
) {
505 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
508 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
509 /* swap deleted line <-> blanked line */
510 blank
= term
.line
[i
];
511 term
.line
[i
] = term
.line
[i
-n
];
512 term
.line
[i
-n
] = blank
;
514 memset(blank
, 0, term
.col
* sizeof(Glyph
));
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
= term
.c
.y
; i
<= bot
-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
));
543 tsetattr(int *attr
, int l
) {
546 for(i
= 0; i
< l
; i
++) {
549 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
550 term
.c
.attr
.fg
= DefaultFG
;
551 term
.c
.attr
.bg
= DefaultBG
;
554 term
.c
.attr
.mode
|= ATTR_BOLD
;
557 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
560 term
.c
.attr
.mode
|= ATTR_REVERSE
;
563 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
566 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
569 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
572 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
574 if (BETWEEN(attr
[i
], 0, 255))
575 term
.c
.attr
.fg
= attr
[i
];
577 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
580 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
583 term
.c
.attr
.fg
= DefaultFG
;
586 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
588 if (BETWEEN(attr
[i
], 0, 255))
589 term
.c
.attr
.bg
= attr
[i
];
591 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
594 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
597 term
.c
.attr
.bg
= DefaultBG
;
600 if(BETWEEN(attr
[i
], 30, 37))
601 term
.c
.attr
.fg
= attr
[i
] - 30;
602 else if(BETWEEN(attr
[i
], 40, 47))
603 term
.c
.attr
.bg
= attr
[i
] - 40;
604 else if(BETWEEN(attr
[i
], 90, 97))
605 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
606 else if(BETWEEN(attr
[i
], 100, 107))
607 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
609 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
616 tsetscroll(int t
, int b
) {
619 LIMIT(t
, 0, term
.row
-1);
620 LIMIT(b
, 0, term
.row
-1);
632 switch(escseq
.mode
) {
635 printf("erresc: unknown csi ");
639 case '@': /* ICH -- Insert <n> blank char */
640 DEFAULT(escseq
.arg
[0], 1);
641 tinsertblank(escseq
.arg
[0]);
643 case 'A': /* CUU -- Cursor <n> Up */
645 DEFAULT(escseq
.arg
[0], 1);
646 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
648 case 'B': /* CUD -- Cursor <n> Down */
649 DEFAULT(escseq
.arg
[0], 1);
650 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
652 case 'C': /* CUF -- Cursor <n> Forward */
654 DEFAULT(escseq
.arg
[0], 1);
655 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
657 case 'D': /* CUB -- Cursor <n> Backward */
658 DEFAULT(escseq
.arg
[0], 1);
659 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
661 case 'E': /* CNL -- Cursor <n> Down and first col */
662 DEFAULT(escseq
.arg
[0], 1);
663 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
665 case 'F': /* CPL -- Cursor <n> Up and first col */
666 DEFAULT(escseq
.arg
[0], 1);
667 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
669 case 'G': /* CHA -- Move to <col> */
670 case '`': /* XXX: HPA -- same? */
671 DEFAULT(escseq
.arg
[0], 1);
672 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
674 case 'H': /* CUP -- Move to <row> <col> */
675 case 'f': /* XXX: HVP -- same? */
676 DEFAULT(escseq
.arg
[0], 1);
677 DEFAULT(escseq
.arg
[1], 1);
678 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
680 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
681 case 'J': /* ED -- Clear screen */
682 switch(escseq
.arg
[0]) {
684 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
687 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
690 tclearregion(0, 0, term
.col
-1, term
.row
-1);
692 case 3: /* XXX: erase saved lines (xterm) */
697 case 'K': /* EL -- Clear line */
698 switch(escseq
.arg
[0]) {
700 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
703 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
706 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
710 case 'S': /* SU -- Scroll <n> line up */
711 DEFAULT(escseq
.arg
[0], 1);
712 tscrollup(escseq
.arg
[0]);
714 case 'T': /* SD -- Scroll <n> line down */
715 DEFAULT(escseq
.arg
[0], 1);
716 tscrolldown(escseq
.arg
[0]);
718 case 'L': /* IL -- Insert <n> blank lines */
719 DEFAULT(escseq
.arg
[0], 1);
720 tinsertblankline(escseq
.arg
[0]);
722 case 'l': /* RM -- Reset Mode */
724 switch(escseq
.arg
[0]) {
726 term
.mode
&= ~MODE_APPKEYPAD
;
729 term
.mode
&= ~MODE_WRAP
;
731 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
736 case 1048: /* XXX: no alt. screen to erase/save */
738 tcursor(CURSOR_LOAD
);
739 tclearregion(0, 0, term
.col
-1, term
.row
-1);
745 switch(escseq
.arg
[0]) {
747 term
.mode
&= ~MODE_INSERT
;
754 case 'M': /* DL -- Delete <n> lines */
755 DEFAULT(escseq
.arg
[0], 1);
756 tdeleteline(escseq
.arg
[0]);
758 case 'X': /* ECH -- Erase <n> char */
759 DEFAULT(escseq
.arg
[0], 1);
760 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
762 case 'P': /* DCH -- Delete <n> char */
763 DEFAULT(escseq
.arg
[0], 1);
764 tdeletechar(escseq
.arg
[0]);
766 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
767 case 'd': /* VPA -- Move to <row> */
768 DEFAULT(escseq
.arg
[0], 1);
769 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
771 case 'h': /* SM -- Set terminal mode */
773 switch(escseq
.arg
[0]) {
775 term
.mode
|= MODE_APPKEYPAD
;
778 term
.mode
|= MODE_WRAP
;
780 case 12: /* att610 -- Start blinking cursor (IGNORED) */
786 case 1049: /* XXX: no alt. screen to erase/save */
787 tcursor(CURSOR_SAVE
);
788 tclearregion(0, 0, term
.col
-1, term
.row
-1);
790 default: goto unknown
;
793 switch(escseq
.arg
[0]) {
795 term
.mode
|= MODE_INSERT
;
797 default: goto unknown
;
801 case 'm': /* SGR -- Terminal attribute (color) */
802 tsetattr(escseq
.arg
, escseq
.narg
);
804 case 'r': /* DECSTBM -- Set Scrolling Region */
808 DEFAULT(escseq
.arg
[0], 1);
809 DEFAULT(escseq
.arg
[1], term
.row
);
810 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
813 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
814 tcursor(CURSOR_SAVE
);
816 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
817 tcursor(CURSOR_LOAD
);
825 printf("ESC [ %s", escseq
.priv
? "? " : "");
827 for(i
= 0; i
< escseq
.narg
; i
++)
828 printf("%d ", escseq
.arg
[i
]);
830 putchar(escseq
.mode
);
836 memset(&escseq
, 0, sizeof(escseq
));
841 int space
= TAB
- term
.c
.x
% TAB
;
842 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
847 if(term
.esc
& ESC_START
) {
848 if(term
.esc
& ESC_CSI
) {
849 escseq
.buf
[escseq
.len
++] = c
;
850 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
852 csiparse(), csihandle();
854 } else if(term
.esc
& ESC_OSC
) {
857 term
.esc
= ESC_START
| ESC_TITLE
;
859 } else if(term
.esc
& ESC_TITLE
) {
860 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
862 term
.title
[term
.titlelen
] = '\0';
863 XStoreName(xw
.dis
, xw
.win
, term
.title
);
865 term
.title
[term
.titlelen
++] = c
;
867 } else if(term
.esc
& ESC_ALTCHARSET
) {
869 case '0': /* Line drawing crap */
870 term
.c
.attr
.mode
|= ATTR_GFX
;
872 case 'B': /* Back to regular text */
873 term
.c
.attr
.mode
&= ~ATTR_GFX
;
876 printf("esc unhandled charset: ESC ( %c\n", c
);
888 term
.esc
|= ESC_ALTCHARSET
;
891 tmoveto(term
.c
.x
, term
.c
.y
-1);
895 tmoveto(term
.c
.x
, term
.c
.y
+1);
899 tmoveto(term
.c
.x
+1, term
.c
.y
);
902 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
903 tmoveto(term
.c
.x
-1, term
.c
.y
);
906 case 'E': /* NEL -- Next line */
910 case 'M': /* RI -- Reverse index */
911 if(term
.c
.y
== term
.top
)
914 tmoveto(term
.c
.x
, term
.c
.y
-1);
917 case 'c': /* RIS -- Reset to inital state */
921 case '=': /* DECPAM */
922 term
.mode
|= MODE_APPKEYPAD
;
925 case '>': /* DECPNM */
926 term
.mode
&= ~MODE_APPKEYPAD
;
930 tcursor(CURSOR_SAVE
);
934 tcursor(CURSOR_LOAD
);
938 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
948 tmoveto(term
.c
.x
-1, term
.c
.y
);
951 tmoveto(0, term
.c
.y
);
961 term
.esc
= ESC_START
;
965 if(term
.c
.x
+1 < term
.col
) {
966 tmoveto(term
.c
.x
+1, term
.c
.y
);
967 } else if(IS_SET(MODE_WRAP
))
975 tputs(char *s
, int len
) {
976 for(; len
> 0; len
--)
981 tresize(int col
, int row
) {
983 int minrow
= MIN(row
, term
.row
);
984 int mincol
= MIN(col
, term
.col
);
986 if(col
< 1 || row
< 1)
989 /* free uneeded rows */
990 for(i
= row
; i
< term
.row
; i
++)
993 /* resize to new height */
994 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
996 /* resize each row to new width, zero-pad if needed */
997 for(i
= 0; i
< minrow
; i
++) {
998 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
999 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1002 /* allocate any new rows */
1003 for(/* i == minrow */; i
< row
; i
++)
1004 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1006 LIMIT(term
.c
.x
, 0, col
-1);
1007 LIMIT(term
.c
.y
, 0, row
-1);
1008 LIMIT(term
.top
, 0, row
-1);
1009 LIMIT(term
.bot
, 0, row
-1);
1012 term
.col
= col
, term
.row
= row
;
1019 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1020 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1022 for(i
= 0; i
< 16; i
++) {
1023 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1025 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1027 dc
.col
[i
] = color
.pixel
;
1030 /* same colors as xterm */
1031 for(r
= 0; r
< 6; r
++)
1032 for(g
= 0; g
< 6; g
++)
1033 for(b
= 0; b
< 6; b
++) {
1034 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1035 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1036 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1037 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1039 fprintf(stderr
, "Could not allocate color %d\n", i
);
1041 dc
.col
[i
] = color
.pixel
;
1045 for(r
= 0; r
< 24; r
++, i
++) {
1046 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1047 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1049 fprintf(stderr
, "Could not allocate color %d\n", i
);
1051 dc
.col
[i
] = color
.pixel
;
1056 xclear(int x1
, int y1
, int x2
, int y2
) {
1057 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1058 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1059 x1
* xw
.cw
, y1
* xw
.ch
,
1060 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1066 XClassHint
class = {TNAME
, TNAME
};
1067 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1069 .flags
= PSize
| PResizeInc
| PBaseSize
,
1072 .height_inc
= xw
.ch
,
1074 .base_height
= 2*BORDER
,
1075 .base_width
= 2*BORDER
,
1077 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1082 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1083 die("Can't open display\n");
1084 xw
.scr
= XDefaultScreen(xw
.dis
);
1087 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1088 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1090 /* XXX: Assuming same size for bold font */
1091 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1092 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1097 term
.c
.attr
.fg
= DefaultFG
;
1098 term
.c
.attr
.bg
= DefaultBG
;
1099 term
.c
.attr
.mode
= ATTR_NULL
;
1101 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1102 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1103 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1107 xw
.bufw
= xw
.w
- 2*BORDER
;
1108 xw
.bufh
= xw
.h
- 2*BORDER
;
1109 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1111 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1112 XMapWindow(xw
.dis
, xw
.win
);
1114 XStoreName(xw
.dis
, xw
.win
, "st");
1119 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1120 unsigned long xfg
, xbg
;
1121 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1124 if(base
.mode
& ATTR_REVERSE
)
1125 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1127 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1129 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1130 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1132 if(base
.mode
& ATTR_GFX
)
1133 for(i
= 0; i
< len
; i
++)
1134 s
[i
] = gfx
[(int)s
[i
]];
1136 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1137 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1139 if(base
.mode
& ATTR_UNDERLINE
)
1140 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1145 static int oldx
= 0;
1146 static int oldy
= 0;
1147 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1149 LIMIT(oldx
, 0, term
.col
-1);
1150 LIMIT(oldy
, 0, term
.row
-1);
1152 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1153 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1155 /* remove the old cursor */
1156 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1157 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1159 xclear(oldx
, oldy
, oldx
, oldy
);
1161 /* draw the new one */
1162 if(mode
== CURSOR_DRAW
) {
1163 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1164 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1169 /* basic drawing routines */
1171 xdrawc(int x
, int y
, Glyph g
) {
1172 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1173 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1174 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1175 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1176 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1183 xclear(0, 0, term
.col
-1, term
.row
-1);
1184 for(y
= 0; y
< term
.row
; y
++)
1185 for(x
= 0; x
< term
.col
; x
++)
1186 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1187 xdrawc(x
, y
, term
.line
[y
][x
]);
1190 xcursor(CURSOR_DRAW
);
1191 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1196 /* optimized drawing routine */
1198 draw(int redraw_all
) {
1201 char buf
[DRAW_BUF_SIZ
];
1203 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1204 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1205 for(y
= 0; y
< term
.row
; y
++) {
1206 base
= term
.line
[y
][0];
1208 for(x
= 0; x
< term
.col
; x
++) {
1209 new = term
.line
[y
][x
];
1210 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1213 xdraws(buf
, base
, ox
, y
, i
);
1220 xdraws(buf
, base
, ox
, y
, i
);
1222 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1223 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1230 expose(XEvent
*ev
) {
1231 draw(SCREEN_REDRAW
);
1237 for(i
= 0; i
< LEN(key
); i
++)
1239 return (char*)key
[i
].s
;
1244 kpress(XEvent
*ev
) {
1245 XKeyEvent
*e
= &ev
->xkey
;
1253 meta
= e
->state
& Mod1Mask
;
1254 shift
= e
->state
& ShiftMask
;
1255 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1257 if((customkey
= kmap(ksym
)))
1258 ttywrite(customkey
, strlen(customkey
));
1260 buf
[sizeof(buf
)-1] = '\0';
1261 if(meta
&& len
== 1)
1262 ttywrite("\033", 1);
1270 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1275 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1278 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1287 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1290 xw
.w
= e
->xconfigure
.width
;
1291 xw
.h
= e
->xconfigure
.height
;
1292 xw
.bufw
= xw
.w
- 2*BORDER
;
1293 xw
.bufh
= xw
.h
- 2*BORDER
;
1294 col
= xw
.bufw
/ xw
.cw
;
1295 row
= xw
.bufh
/ xw
.ch
;
1297 ttyresize(col
, row
);
1298 XFreePixmap(xw
.dis
, xw
.buf
);
1299 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1300 draw(SCREEN_REDRAW
);
1307 int xfd
= XConnectionNumber(xw
.dis
);
1310 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1311 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1315 FD_SET(cmdfd
, &rfd
);
1317 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1320 die("select failed: %s\n", SERRNO
);
1322 if(FD_ISSET(cmdfd
, &rfd
)) {
1324 draw(SCREEN_UPDATE
);
1326 while(XPending(xw
.dis
)) {
1327 XNextEvent(xw
.dis
, &ev
);
1328 if(handler
[ev
.type
])
1329 (handler
[ev
.type
])(&ev
);
1335 main(int argc
, char *argv
[]) {
1336 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1337 die("st-" VERSION
", (c) 2010 st engineers\n");
1339 die("usage: st [-v]\n");
1340 setlocale(LC_CTYPE
, "");