Xinqi Bao's Git
d928f0d6c58d0115711d69d5cb5330c8bd450ca6
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__) || defined(__NetBSD__)
27 #elif defined(__FreeBSD__) || defined(__DragonFly__)
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
,
50 CURSOR_SAVE
, CURSOR_LOAD
};
51 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
52 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
53 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8 };
54 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
55 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
58 char c
; /* character code */
59 char mode
; /* attribute flags */
60 int fg
; /* foreground */
61 int bg
; /* background */
62 char state
; /* state flags */
68 Glyph attr
; /* current char attributes */
74 /* CSI Escape sequence structs */
75 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
77 char buf
[ESC_BUF_SIZ
]; /* raw string */
78 int len
; /* raw string length */
81 int narg
; /* nb of args */
85 /* Internal representation of the screen */
89 Line
* line
; /* screen */
90 Line
* alt
; /* alternate screen */
91 TCursor c
; /* cursor */
92 int top
; /* top scroll limit */
93 int bot
; /* bottom scroll limit */
94 int mode
; /* terminal mode flags */
95 int esc
; /* escape state flags */
96 char title
[ESC_TITLE_SIZ
];
100 /* Purely graphic info */
106 int w
; /* window width */
107 int h
; /* window height */
108 int bufw
; /* pixmap width */
109 int bufh
; /* pixmap height */
110 int ch
; /* char height */
111 int cw
; /* char width */
120 /* Drawing Context */
122 unsigned long col
[256];
130 static void die(const char *errstr
, ...);
131 static void draw(int);
132 static void execsh(void);
133 static void sigchld(int);
134 static void run(void);
136 static void csidump(void);
137 static void csihandle(void);
138 static void csiparse(void);
139 static void csireset(void);
141 static void tclearregion(int, int, int, int);
142 static void tcursor(int);
143 static void tdeletechar(int);
144 static void tdeleteline(int);
145 static void tinsertblank(int);
146 static void tinsertblankline(int);
147 static void tmoveto(int, int);
148 static void tnew(int, int);
149 static void tnewline(void);
150 static void tputtab(void);
151 static void tputc(char);
152 static void tputs(char*, int);
153 static void treset(void);
154 static void tresize(int, int);
155 static void tscrollup(int);
156 static void tscrolldown(int);
157 static void tsetattr(int*, int);
158 static void tsetchar(char);
159 static void tsetscroll(int, int);
160 static void tswapscreen(void);
162 static void ttynew(void);
163 static void ttyread(void);
164 static void ttyresize(int, int);
165 static void ttywrite(const char *, size_t);
167 static void xdraws(char *, Glyph
, int, int, int);
168 static void xhints(void);
169 static void xclear(int, int, int, int);
170 static void xdrawcursor(void);
171 static void xinit(void);
172 static void xloadcols(void);
173 static void xseturgency(int);
175 static void expose(XEvent
*);
176 static char* kmap(KeySym
);
177 static void kpress(XEvent
*);
178 static void resize(XEvent
*);
179 static void focus(XEvent
*);
182 static void (*handler
[LASTEvent
])(XEvent
*) = {
185 [ConfigureNotify
] = resize
,
194 static CSIEscape escseq
;
204 for(row
= 0; row
< term
.row
; row
++) {
205 for(col
= 0; col
< term
.col
; col
++) {
206 if(col
== term
.c
.x
&& row
== term
.c
.y
)
209 c
= term
.line
[row
][col
];
210 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
219 die(const char *errstr
, ...) {
222 va_start(ap
, errstr
);
223 vfprintf(stderr
, errstr
, ap
);
230 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
231 DEFAULT(args
[0], "/bin/sh"); /* if getenv() failed */
232 putenv("TERM=" TNAME
);
233 execvp(args
[0], args
);
239 if(waitpid(pid
, &stat
, 0) < 0)
240 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
242 exit(WEXITSTATUS(stat
));
251 /* seems to work fine on linux, openbsd and freebsd */
252 struct winsize w
= {term
.row
, term
.col
, 0, 0};
253 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
254 die("openpty failed: %s\n", SERRNO
);
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 TIOCSCTTY failed: %s\n", SERRNO
);
274 signal(SIGCHLD
, sigchld
);
281 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
283 fprintf(stderr
, "\n");
288 char buf
[BUFSIZ
] = {0};
291 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
292 die("Couldn't read from shell: %s\n", SERRNO
);
298 ttywrite(const char *s
, size_t n
) {
299 if(write(cmdfd
, s
, n
) == -1)
300 die("write error on tty: %s\n", SERRNO
);
304 ttyresize(int x
, int y
) {
309 w
.ws_xpixel
= w
.ws_ypixel
= 0;
310 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
311 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
318 if(mode
== CURSOR_SAVE
)
320 else if(mode
== CURSOR_LOAD
)
321 term
.c
= c
, tmoveto(c
.x
, c
.y
);
330 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
332 term
.top
= 0, term
.bot
= term
.row
- 1;
333 term
.mode
= MODE_WRAP
;
334 tclearregion(0, 0, term
.col
-1, term
.row
-1);
338 tnew(int col
, int row
) {
339 /* set screen size */
340 term
.row
= row
, term
.col
= col
;
341 term
.line
= malloc(term
.row
* sizeof(Line
));
342 term
.alt
= malloc(term
.row
* sizeof(Line
));
343 for(row
= 0 ; row
< term
.row
; row
++) {
344 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
345 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
353 Line
* tmp
= term
.line
;
354 term
.line
= term
.alt
;
356 term
.mode
^= MODE_ALTSCREEN
;
360 tscrolldown (int n
) {
364 LIMIT(n
, 0, term
.bot
-term
.top
+1);
366 for(i
= 0; i
< n
; i
++)
367 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
369 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
371 term
.line
[i
] = term
.line
[i
-n
];
372 term
.line
[i
-n
] = temp
;
380 LIMIT(n
, 0, term
.bot
-term
.top
+1);
382 for(i
= 0; i
< n
; i
++)
383 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
385 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
387 term
.line
[i
] = term
.line
[i
+n
];
388 term
.line
[i
+n
] = temp
;
394 int y
= term
.c
.y
+ 1;
396 tscrollup(1), y
= term
.bot
;
403 char *p
= escseq
.buf
;
407 escseq
.priv
= 1, p
++;
409 while(p
< escseq
.buf
+escseq
.len
) {
411 escseq
.arg
[escseq
.narg
] *= 10;
412 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
414 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
425 tmoveto(int x
, int y
) {
426 LIMIT(x
, 0, term
.col
-1);
427 LIMIT(y
, 0, term
.row
-1);
428 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
435 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
436 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
437 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
441 tclearregion(int x1
, int y1
, int x2
, int y2
) {
445 temp
= x1
, x1
= x2
, x2
= temp
;
447 temp
= y1
, y1
= y2
, y2
= temp
;
449 LIMIT(x1
, 0, term
.col
-1);
450 LIMIT(x2
, 0, term
.col
-1);
451 LIMIT(y1
, 0, term
.row
-1);
452 LIMIT(y2
, 0, term
.row
-1);
454 for(y
= y1
; y
<= y2
; y
++)
455 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
460 int src
= term
.c
.x
+ n
;
462 int size
= term
.col
- src
;
464 if(src
>= term
.col
) {
465 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
468 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
469 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
473 tinsertblank(int n
) {
476 int size
= term
.col
- dst
;
478 if(dst
>= term
.col
) {
479 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
482 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
483 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
487 tinsertblankline(int n
) {
492 if(term
.c
.y
> term
.bot
)
494 else if(term
.c
.y
< term
.top
)
496 if(term
.c
.y
+ n
>= bot
) {
497 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
500 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
501 /* swap deleted line <-> blanked line */
502 blank
= term
.line
[i
];
503 term
.line
[i
] = term
.line
[i
-n
];
504 term
.line
[i
-n
] = blank
;
506 memset(blank
, 0, term
.col
* sizeof(Glyph
));
516 if(term
.c
.y
> term
.bot
)
518 else if(term
.c
.y
< term
.top
)
520 if(term
.c
.y
+ n
>= bot
) {
521 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
524 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
525 /* swap deleted line <-> blanked line */
526 blank
= term
.line
[i
];
527 term
.line
[i
] = term
.line
[i
+n
];
528 term
.line
[i
+n
] = blank
;
530 memset(blank
, 0, term
.col
* sizeof(Glyph
));
535 tsetattr(int *attr
, int l
) {
538 for(i
= 0; i
< l
; i
++) {
541 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
542 term
.c
.attr
.fg
= DefaultFG
;
543 term
.c
.attr
.bg
= DefaultBG
;
546 term
.c
.attr
.mode
|= ATTR_BOLD
;
549 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
552 term
.c
.attr
.mode
|= ATTR_REVERSE
;
555 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
558 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
561 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
564 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
566 if (BETWEEN(attr
[i
], 0, 255))
567 term
.c
.attr
.fg
= attr
[i
];
569 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
572 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
575 term
.c
.attr
.fg
= DefaultFG
;
578 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
580 if (BETWEEN(attr
[i
], 0, 255))
581 term
.c
.attr
.bg
= attr
[i
];
583 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
586 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
589 term
.c
.attr
.bg
= DefaultBG
;
592 if(BETWEEN(attr
[i
], 30, 37))
593 term
.c
.attr
.fg
= attr
[i
] - 30;
594 else if(BETWEEN(attr
[i
], 40, 47))
595 term
.c
.attr
.bg
= attr
[i
] - 40;
596 else if(BETWEEN(attr
[i
], 90, 97))
597 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
598 else if(BETWEEN(attr
[i
], 100, 107))
599 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
601 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
608 tsetscroll(int t
, int b
) {
611 LIMIT(t
, 0, term
.row
-1);
612 LIMIT(b
, 0, term
.row
-1);
624 switch(escseq
.mode
) {
627 printf("erresc: unknown csi ");
631 case '@': /* ICH -- Insert <n> blank char */
632 DEFAULT(escseq
.arg
[0], 1);
633 tinsertblank(escseq
.arg
[0]);
635 case 'A': /* CUU -- Cursor <n> Up */
637 DEFAULT(escseq
.arg
[0], 1);
638 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
640 case 'B': /* CUD -- Cursor <n> Down */
641 DEFAULT(escseq
.arg
[0], 1);
642 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
644 case 'C': /* CUF -- Cursor <n> Forward */
646 DEFAULT(escseq
.arg
[0], 1);
647 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
649 case 'D': /* CUB -- Cursor <n> Backward */
650 DEFAULT(escseq
.arg
[0], 1);
651 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
653 case 'E': /* CNL -- Cursor <n> Down and first col */
654 DEFAULT(escseq
.arg
[0], 1);
655 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
657 case 'F': /* CPL -- Cursor <n> Up and first col */
658 DEFAULT(escseq
.arg
[0], 1);
659 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
661 case 'G': /* CHA -- Move to <col> */
662 case '`': /* XXX: HPA -- same? */
663 DEFAULT(escseq
.arg
[0], 1);
664 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
666 case 'H': /* CUP -- Move to <row> <col> */
667 case 'f': /* XXX: HVP -- same? */
668 DEFAULT(escseq
.arg
[0], 1);
669 DEFAULT(escseq
.arg
[1], 1);
670 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
672 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
673 case 'J': /* ED -- Clear screen */
674 switch(escseq
.arg
[0]) {
676 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
679 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
682 tclearregion(0, 0, term
.col
-1, term
.row
-1);
684 case 3: /* XXX: erase saved lines (xterm) */
689 case 'K': /* EL -- Clear line */
690 switch(escseq
.arg
[0]) {
692 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
695 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
698 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
702 case 'S': /* SU -- Scroll <n> line up */
703 DEFAULT(escseq
.arg
[0], 1);
704 tscrollup(escseq
.arg
[0]);
706 case 'T': /* SD -- Scroll <n> line down */
707 DEFAULT(escseq
.arg
[0], 1);
708 tscrolldown(escseq
.arg
[0]);
710 case 'L': /* IL -- Insert <n> blank lines */
711 DEFAULT(escseq
.arg
[0], 1);
712 tinsertblankline(escseq
.arg
[0]);
714 case 'l': /* RM -- Reset Mode */
716 switch(escseq
.arg
[0]) {
718 term
.mode
&= ~MODE_APPKEYPAD
;
721 term
.mode
&= ~MODE_WRAP
;
723 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
726 term
.c
.state
|= CURSOR_HIDE
;
729 if(IS_SET(MODE_ALTSCREEN
)) {
730 tclearregion(0, 0, term
.col
-1, term
.row
-1);
735 tcursor(CURSOR_LOAD
);
738 tcursor(CURSOR_LOAD
);
739 if(IS_SET(MODE_ALTSCREEN
)) {
740 tclearregion(0, 0, term
.col
-1, term
.row
-1);
748 switch(escseq
.arg
[0]) {
750 term
.mode
&= ~MODE_INSERT
;
757 case 'M': /* DL -- Delete <n> lines */
758 DEFAULT(escseq
.arg
[0], 1);
759 tdeleteline(escseq
.arg
[0]);
761 case 'X': /* ECH -- Erase <n> char */
762 DEFAULT(escseq
.arg
[0], 1);
763 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
765 case 'P': /* DCH -- Delete <n> char */
766 DEFAULT(escseq
.arg
[0], 1);
767 tdeletechar(escseq
.arg
[0]);
769 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
770 case 'd': /* VPA -- Move to <row> */
771 DEFAULT(escseq
.arg
[0], 1);
772 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
774 case 'h': /* SM -- Set terminal mode */
776 switch(escseq
.arg
[0]) {
778 term
.mode
|= MODE_APPKEYPAD
;
781 term
.mode
|= MODE_WRAP
;
783 case 12: /* att610 -- Start blinking cursor (IGNORED) */
786 term
.c
.state
&= ~CURSOR_HIDE
;
789 if(IS_SET(MODE_ALTSCREEN
))
790 tclearregion(0, 0, term
.col
-1, term
.row
-1);
795 tcursor(CURSOR_SAVE
);
798 tcursor(CURSOR_SAVE
);
799 if(IS_SET(MODE_ALTSCREEN
))
800 tclearregion(0, 0, term
.col
-1, term
.row
-1);
804 default: goto unknown
;
807 switch(escseq
.arg
[0]) {
809 term
.mode
|= MODE_INSERT
;
811 default: goto unknown
;
815 case 'm': /* SGR -- Terminal attribute (color) */
816 tsetattr(escseq
.arg
, escseq
.narg
);
818 case 'r': /* DECSTBM -- Set Scrolling Region */
822 DEFAULT(escseq
.arg
[0], 1);
823 DEFAULT(escseq
.arg
[1], term
.row
);
824 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
828 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
829 tcursor(CURSOR_SAVE
);
831 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
832 tcursor(CURSOR_LOAD
);
840 printf("ESC [ %s", escseq
.priv
? "? " : "");
842 for(i
= 0; i
< escseq
.narg
; i
++)
843 printf("%d ", escseq
.arg
[i
]);
845 putchar(escseq
.mode
);
851 memset(&escseq
, 0, sizeof(escseq
));
856 int space
= TAB
- term
.c
.x
% TAB
;
857 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
862 if(term
.esc
& ESC_START
) {
863 if(term
.esc
& ESC_CSI
) {
864 escseq
.buf
[escseq
.len
++] = c
;
865 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
867 csiparse(), csihandle();
869 } else if(term
.esc
& ESC_OSC
) {
872 term
.esc
= ESC_START
| ESC_TITLE
;
874 } else if(term
.esc
& ESC_TITLE
) {
875 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
877 term
.title
[term
.titlelen
] = '\0';
878 XStoreName(xw
.dis
, xw
.win
, term
.title
);
880 term
.title
[term
.titlelen
++] = c
;
882 } else if(term
.esc
& ESC_ALTCHARSET
) {
884 case '0': /* Line drawing crap */
885 term
.c
.attr
.mode
|= ATTR_GFX
;
887 case 'B': /* Back to regular text */
888 term
.c
.attr
.mode
&= ~ATTR_GFX
;
891 printf("esc unhandled charset: ESC ( %c\n", c
);
903 term
.esc
|= ESC_ALTCHARSET
;
905 case 'D': /* IND -- Linefeed */
906 if(term
.c
.y
== term
.bot
)
909 tmoveto(term
.c
.x
, term
.c
.y
+1);
912 case 'E': /* NEL -- Next line */
916 case 'M': /* RI -- Reverse index */
917 if(term
.c
.y
== term
.top
)
920 tmoveto(term
.c
.x
, term
.c
.y
-1);
923 case 'c': /* RIS -- Reset to inital state */
927 case '=': /* DECPAM -- Application keypad */
928 term
.mode
|= MODE_APPKEYPAD
;
931 case '>': /* DECPNM -- Normal keypad */
932 term
.mode
&= ~MODE_APPKEYPAD
;
935 case '7': /* DECSC -- Save Cursor*/
936 tcursor(CURSOR_SAVE
);
939 case '8': /* DECRC -- Restore Cursor */
940 tcursor(CURSOR_LOAD
);
944 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
954 tmoveto(term
.c
.x
-1, term
.c
.y
);
957 tmoveto(0, term
.c
.y
);
968 term
.esc
= ESC_START
;
971 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
974 if(term
.c
.x
+1 < term
.col
)
975 tmoveto(term
.c
.x
+1, term
.c
.y
);
977 term
.c
.state
|= CURSOR_WRAPNEXT
;
984 tputs(char *s
, int len
) {
985 for(; len
> 0; len
--)
990 tresize(int col
, int row
) {
992 int minrow
= MIN(row
, term
.row
);
993 int mincol
= MIN(col
, term
.col
);
995 if(col
< 1 || row
< 1)
998 /* free uneeded rows */
999 for(i
= row
; i
< term
.row
; i
++) {
1004 /* resize to new height */
1005 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1006 term
.line
= realloc(term
.alt
, row
* sizeof(Line
));
1008 /* resize each row to new width, zero-pad if needed */
1009 for(i
= 0; i
< minrow
; i
++) {
1010 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1011 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1012 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1013 memset(term
.alt
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1016 /* allocate any new rows */
1017 for(/* i == minrow */; i
< row
; i
++) {
1018 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1019 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1022 /* update terminal size */
1023 term
.col
= col
, term
.row
= row
;
1024 /* make use of the LIMIT in tmoveto */
1025 tmoveto(term
.c
.x
, term
.c
.y
);
1026 /* reset scrolling region */
1027 tsetscroll(0, row
-1);
1034 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1035 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1037 for(i
= 0; i
< 16; i
++) {
1038 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1040 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1042 dc
.col
[i
] = color
.pixel
;
1045 /* same colors as xterm */
1046 for(r
= 0; r
< 6; r
++)
1047 for(g
= 0; g
< 6; g
++)
1048 for(b
= 0; b
< 6; b
++) {
1049 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1050 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1051 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1052 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1054 fprintf(stderr
, "Could not allocate color %d\n", i
);
1056 dc
.col
[i
] = color
.pixel
;
1060 for(r
= 0; r
< 24; r
++, i
++) {
1061 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1062 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1064 fprintf(stderr
, "Could not allocate color %d\n", i
);
1066 dc
.col
[i
] = color
.pixel
;
1071 xclear(int x1
, int y1
, int x2
, int y2
) {
1072 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1073 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1074 x1
* xw
.cw
, y1
* xw
.ch
,
1075 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1081 XClassHint
class = {TNAME
, TNAME
};
1082 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1084 .flags
= PSize
| PResizeInc
| PBaseSize
,
1087 .height_inc
= xw
.ch
,
1089 .base_height
= 2*BORDER
,
1090 .base_width
= 2*BORDER
,
1092 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1097 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1098 die("Can't open display\n");
1099 xw
.scr
= XDefaultScreen(xw
.dis
);
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
;
1113 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1114 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1115 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1119 xw
.bufw
= xw
.w
- 2*BORDER
;
1120 xw
.bufh
= xw
.h
- 2*BORDER
;
1121 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1123 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1124 XMapWindow(xw
.dis
, xw
.win
);
1126 XStoreName(xw
.dis
, xw
.win
, "st");
1131 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1132 unsigned long xfg
, xbg
;
1133 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1136 if(base
.mode
& ATTR_REVERSE
)
1137 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1139 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1141 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1142 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1144 if(base
.mode
& ATTR_GFX
)
1145 for(i
= 0; i
< len
; i
++)
1146 s
[i
] = gfx
[(int)s
[i
]];
1148 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1149 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1151 if(base
.mode
& ATTR_UNDERLINE
)
1152 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1157 static int oldx
= 0;
1158 static int oldy
= 0;
1159 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1161 LIMIT(oldx
, 0, term
.col
-1);
1162 LIMIT(oldy
, 0, term
.row
-1);
1164 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1165 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1167 /* remove the old cursor */
1168 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1169 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1171 xclear(oldx
, oldy
, oldx
, oldy
);
1173 /* draw the new one */
1174 if(!(term
.c
.state
& CURSOR_HIDE
) && xw
.hasfocus
) {
1175 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1176 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1181 /* basic drawing routines */
1183 xdrawc(int x
, int y
, Glyph g
) {
1184 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1185 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1186 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1187 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1188 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1195 xclear(0, 0, term
.col
-1, term
.row
-1);
1196 for(y
= 0; y
< term
.row
; y
++)
1197 for(x
= 0; x
< term
.col
; x
++)
1198 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1199 xdrawc(x
, y
, term
.line
[y
][x
]);
1202 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1207 /* optimized drawing routine */
1209 draw(int redraw_all
) {
1212 char buf
[DRAW_BUF_SIZ
];
1214 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1215 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
);
1216 for(y
= 0; y
< term
.row
; y
++) {
1217 base
= term
.line
[y
][0];
1219 for(x
= 0; x
< term
.col
; x
++) {
1220 new = term
.line
[y
][x
];
1221 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1222 i
>= DRAW_BUF_SIZ
)) {
1223 xdraws(buf
, base
, ox
, y
, i
);
1226 if(new.state
& GLYPH_SET
) {
1235 xdraws(buf
, base
, ox
, y
, i
);
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
);
1250 xseturgency(int add
) {
1251 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1252 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1253 XSetWMHints(xw
.dis
, xw
.win
, h
);
1259 if((xw
.hasfocus
= ev
->type
== FocusIn
))
1261 draw(SCREEN_UPDATE
);
1267 for(i
= 0; i
< LEN(key
); i
++)
1269 return (char*)key
[i
].s
;
1274 kpress(XEvent
*ev
) {
1275 XKeyEvent
*e
= &ev
->xkey
;
1283 meta
= e
->state
& Mod1Mask
;
1284 shift
= e
->state
& ShiftMask
;
1285 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1287 if((customkey
= kmap(ksym
)))
1288 ttywrite(customkey
, strlen(customkey
));
1290 buf
[sizeof(buf
)-1] = '\0';
1291 if(meta
&& len
== 1)
1292 ttywrite("\033", 1);
1300 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1305 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1308 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1317 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1320 xw
.w
= e
->xconfigure
.width
;
1321 xw
.h
= e
->xconfigure
.height
;
1322 xw
.bufw
= xw
.w
- 2*BORDER
;
1323 xw
.bufh
= xw
.h
- 2*BORDER
;
1324 col
= xw
.bufw
/ xw
.cw
;
1325 row
= xw
.bufh
/ xw
.ch
;
1327 ttyresize(col
, row
);
1328 XFreePixmap(xw
.dis
, xw
.buf
);
1329 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1330 draw(SCREEN_REDRAW
);
1337 int xfd
= XConnectionNumber(xw
.dis
);
1338 long mask
= ExposureMask
| KeyPressMask
| StructureNotifyMask
| FocusChangeMask
;
1340 XSelectInput(xw
.dis
, xw
.win
, mask
);
1341 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1345 FD_SET(cmdfd
, &rfd
);
1347 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1350 die("select failed: %s\n", SERRNO
);
1352 if(FD_ISSET(cmdfd
, &rfd
)) {
1354 draw(SCREEN_UPDATE
);
1356 while(XPending(xw
.dis
)) {
1357 XNextEvent(xw
.dis
, &ev
);
1358 if(handler
[ev
.type
])
1359 (handler
[ev
.type
])(&ev
);
1365 main(int argc
, char *argv
[]) {
1366 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1367 die("st-" VERSION
", (c) 2010 st engineers\n");
1369 die("usage: st [-v]\n");
1370 setlocale(LC_CTYPE
, "");