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>
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 tscrollup(int);
153 static void tscrolldown(int);
154 static void tsetattr(int*, int);
155 static void tsetchar(char);
156 static void tsetscroll(int, int);
158 static void ttynew(void);
159 static void ttyread(void);
160 static void ttyresize(int, int);
161 static void ttywrite(const char *, size_t);
163 static void xbell(void);
164 static void xdraws(char *, Glyph
, int, int, int);
165 static void xhints(void);
166 static void xclear(int, int, int, int);
167 static void xcursor(int);
168 static void xinit(void);
169 static void xloadcols(void);
171 static void expose(XEvent
*);
172 static char* kmap(KeySym
);
173 static void kpress(XEvent
*);
174 static void resize(XEvent
*);
176 static void (*handler
[LASTEvent
])(XEvent
*) = {
179 [ConfigureNotify
] = resize
186 static CSIEscape escseq
;
197 for(row
= 0; row
< term
.row
; row
++) {
198 for(col
= 0; col
< term
.col
; col
++) {
199 if(col
== term
.c
.x
&& row
== term
.c
.y
)
202 c
= term
.line
[row
][col
];
203 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
212 die(const char *errstr
, ...) {
215 va_start(ap
, errstr
);
216 vfprintf(stderr
, errstr
, ap
);
223 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
224 DEFAULT(args
[0], "/bin/sh"); /* default shell if getenv() failed */
225 putenv("TERM=" TNAME
);
226 execvp(args
[0], args
);
230 xbell(void) { /* visual bell */
231 XRectangle r
= { BORDER
, BORDER
, xw
.bufw
, xw
.bufh
};
232 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
233 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
241 if(waitpid(pid
, &stat
, 0) < 0)
242 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
244 exit(WEXITSTATUS(stat
));
253 /* seems to work fine on linux, openbsd and freebsd */
254 struct winsize w
= {term
.row
, term
.col
, 0, 0};
255 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
256 die("openpty failed: %s\n", SERRNO
);
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
) {
341 term
.row
= row
, term
.col
= col
;
342 term
.top
= 0, term
.bot
= term
.row
- 1;
344 term
.mode
= MODE_WRAP
;
346 term
.c
.attr
.mode
= ATTR_NULL
;
347 term
.c
.attr
.fg
= DefaultFG
;
348 term
.c
.attr
.bg
= DefaultBG
;
349 term
.c
.x
= term
.c
.y
= 0;
351 /* allocate screen */
352 term
.line
= calloc(term
.row
, sizeof(Line
));
353 for(row
= 0 ; row
< term
.row
; row
++)
354 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
358 tscrolldown (int n
) {
362 LIMIT(n
, 0, term
.bot
-term
.top
+1);
364 for(i
= 0; i
< n
; i
++)
365 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
367 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
369 term
.line
[i
] = term
.line
[i
-n
];
370 term
.line
[i
-n
] = temp
;
378 LIMIT(n
, 0, term
.bot
-term
.top
+1);
380 for(i
= 0; i
< n
; i
++)
381 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
383 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
385 term
.line
[i
] = term
.line
[i
+n
];
386 term
.line
[i
+n
] = temp
;
392 int y
= term
.c
.y
+ 1;
394 tscrollup(1), y
= term
.bot
;
401 char *p
= escseq
.buf
;
405 escseq
.priv
= 1, p
++;
407 while(p
< escseq
.buf
+escseq
.len
) {
409 escseq
.arg
[escseq
.narg
] *= 10;
410 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
412 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
423 tmoveto(int x
, int y
) {
424 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
425 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
430 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
431 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
432 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
436 tclearregion(int x1
, int y1
, int x2
, int y2
) {
440 temp
= x1
, x1
= x2
, x2
= temp
;
442 temp
= y1
, y1
= y2
, y2
= temp
;
444 LIMIT(x1
, 0, term
.col
-1);
445 LIMIT(x2
, 0, term
.col
-1);
446 LIMIT(y1
, 0, term
.row
-1);
447 LIMIT(y2
, 0, term
.row
-1);
449 for(y
= y1
; y
<= y2
; y
++)
450 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
455 int src
= term
.c
.x
+ n
;
457 int size
= term
.col
- src
;
459 if(src
>= term
.col
) {
460 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
463 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
464 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
468 tinsertblank(int n
) {
471 int size
= term
.col
- dst
;
473 if(dst
>= term
.col
) {
474 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
477 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
478 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
482 tinsertblankline(int n
) {
487 if(term
.c
.y
> term
.bot
)
489 else if(term
.c
.y
< term
.top
)
491 if(term
.c
.y
+ n
>= bot
) {
492 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
495 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
496 /* swap deleted line <-> blanked line */
497 blank
= term
.line
[i
];
498 term
.line
[i
] = term
.line
[i
-n
];
499 term
.line
[i
-n
] = blank
;
501 memset(blank
, 0, term
.col
* sizeof(Glyph
));
511 if(term
.c
.y
> term
.bot
)
513 else if(term
.c
.y
< term
.top
)
515 if(term
.c
.y
+ n
>= bot
) {
516 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
519 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
520 /* swap deleted line <-> blanked line */
521 blank
= term
.line
[i
];
522 term
.line
[i
] = term
.line
[i
+n
];
523 term
.line
[i
+n
] = blank
;
525 memset(blank
, 0, term
.col
* sizeof(Glyph
));
530 tsetattr(int *attr
, int l
) {
533 for(i
= 0; i
< l
; i
++) {
536 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
537 term
.c
.attr
.fg
= DefaultFG
;
538 term
.c
.attr
.bg
= DefaultBG
;
541 term
.c
.attr
.mode
|= ATTR_BOLD
;
544 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
547 term
.c
.attr
.mode
|= ATTR_REVERSE
;
550 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
553 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
556 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
559 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
561 if (BETWEEN(attr
[i
], 0, 255))
562 term
.c
.attr
.fg
= attr
[i
];
564 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
567 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
570 term
.c
.attr
.fg
= DefaultFG
;
573 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
575 if (BETWEEN(attr
[i
], 0, 255))
576 term
.c
.attr
.bg
= attr
[i
];
578 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
581 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
584 term
.c
.attr
.bg
= DefaultBG
;
587 if(BETWEEN(attr
[i
], 30, 37))
588 term
.c
.attr
.fg
= attr
[i
] - 30;
589 else if(BETWEEN(attr
[i
], 40, 47))
590 term
.c
.attr
.bg
= attr
[i
] - 40;
591 else if(BETWEEN(attr
[i
], 90, 97))
592 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
593 else if(BETWEEN(attr
[i
], 100, 107))
594 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
596 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
603 tsetscroll(int t
, int b
) {
606 LIMIT(t
, 0, term
.row
-1);
607 LIMIT(b
, 0, term
.row
-1);
619 switch(escseq
.mode
) {
622 printf("erresc: unknown csi ");
626 case '@': /* ICH -- Insert <n> blank char */
627 DEFAULT(escseq
.arg
[0], 1);
628 tinsertblank(escseq
.arg
[0]);
630 case 'A': /* CUU -- Cursor <n> Up */
632 DEFAULT(escseq
.arg
[0], 1);
633 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
635 case 'B': /* CUD -- Cursor <n> Down */
636 DEFAULT(escseq
.arg
[0], 1);
637 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
639 case 'C': /* CUF -- Cursor <n> Forward */
641 DEFAULT(escseq
.arg
[0], 1);
642 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
644 case 'D': /* CUB -- Cursor <n> Backward */
645 DEFAULT(escseq
.arg
[0], 1);
646 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
648 case 'E': /* CNL -- Cursor <n> Down and first col */
649 DEFAULT(escseq
.arg
[0], 1);
650 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
652 case 'F': /* CPL -- Cursor <n> Up and first col */
653 DEFAULT(escseq
.arg
[0], 1);
654 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
656 case 'G': /* CHA -- Move to <col> */
657 case '`': /* XXX: HPA -- same? */
658 DEFAULT(escseq
.arg
[0], 1);
659 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
661 case 'H': /* CUP -- Move to <row> <col> */
662 case 'f': /* XXX: HVP -- same? */
663 DEFAULT(escseq
.arg
[0], 1);
664 DEFAULT(escseq
.arg
[1], 1);
665 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
667 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
668 case 'J': /* ED -- Clear screen */
669 switch(escseq
.arg
[0]) {
671 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
674 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
677 tclearregion(0, 0, term
.col
-1, term
.row
-1);
679 case 3: /* XXX: erase saved lines (xterm) */
684 case 'K': /* EL -- Clear line */
685 switch(escseq
.arg
[0]) {
687 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
690 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
693 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
697 case 'S': /* SU -- Scroll <n> line up */
698 DEFAULT(escseq
.arg
[0], 1);
699 tscrollup(escseq
.arg
[0]);
701 case 'T': /* SD -- Scroll <n> line down */
702 DEFAULT(escseq
.arg
[0], 1);
703 tscrolldown(escseq
.arg
[0]);
705 case 'L': /* IL -- Insert <n> blank lines */
706 DEFAULT(escseq
.arg
[0], 1);
707 tinsertblankline(escseq
.arg
[0]);
709 case 'l': /* RM -- Reset Mode */
711 switch(escseq
.arg
[0]) {
713 term
.mode
&= ~MODE_APPKEYPAD
;
716 term
.mode
&= ~MODE_WRAP
;
718 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
723 case 1048: /* XXX: no alt. screen to erase/save */
725 tcursor(CURSOR_LOAD
);
726 tclearregion(0, 0, term
.col
-1, term
.row
-1);
732 switch(escseq
.arg
[0]) {
734 term
.mode
&= ~MODE_INSERT
;
741 case 'M': /* DL -- Delete <n> lines */
742 DEFAULT(escseq
.arg
[0], 1);
743 tdeleteline(escseq
.arg
[0]);
745 case 'X': /* ECH -- Erase <n> char */
746 DEFAULT(escseq
.arg
[0], 1);
747 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
749 case 'P': /* DCH -- Delete <n> char */
750 DEFAULT(escseq
.arg
[0], 1);
751 tdeletechar(escseq
.arg
[0]);
753 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
754 case 'd': /* VPA -- Move to <row> */
755 DEFAULT(escseq
.arg
[0], 1);
756 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
758 case 'h': /* SM -- Set terminal mode */
760 switch(escseq
.arg
[0]) {
762 term
.mode
|= MODE_APPKEYPAD
;
765 term
.mode
|= MODE_WRAP
;
767 case 12: /* att610 -- Start blinking cursor (IGNORED) */
773 case 1049: /* XXX: no alt. screen to erase/save */
774 tcursor(CURSOR_SAVE
);
775 tclearregion(0, 0, term
.col
-1, term
.row
-1);
777 default: goto unknown
;
780 switch(escseq
.arg
[0]) {
782 term
.mode
|= MODE_INSERT
;
784 default: goto unknown
;
788 case 'm': /* SGR -- Terminal attribute (color) */
789 tsetattr(escseq
.arg
, escseq
.narg
);
791 case 'r': /* DECSTBM -- Set Scrolling Region */
795 DEFAULT(escseq
.arg
[0], 1);
796 DEFAULT(escseq
.arg
[1], term
.row
);
797 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
800 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
801 tcursor(CURSOR_SAVE
);
803 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
804 tcursor(CURSOR_LOAD
);
812 printf("ESC [ %s", escseq
.priv
? "? " : "");
814 for(i
= 0; i
< escseq
.narg
; i
++)
815 printf("%d ", escseq
.arg
[i
]);
817 putchar(escseq
.mode
);
823 memset(&escseq
, 0, sizeof(escseq
));
828 int space
= TAB
- term
.c
.x
% TAB
;
829 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
834 if(term
.esc
& ESC_START
) {
835 if(term
.esc
& ESC_CSI
) {
836 escseq
.buf
[escseq
.len
++] = c
;
837 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
839 csiparse(), csihandle();
841 } else if(term
.esc
& ESC_OSC
) {
844 term
.esc
= ESC_START
| ESC_TITLE
;
846 } else if(term
.esc
& ESC_TITLE
) {
847 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
849 term
.title
[term
.titlelen
] = '\0';
850 XStoreName(xw
.dis
, xw
.win
, term
.title
);
852 term
.title
[term
.titlelen
++] = c
;
854 } else if(term
.esc
& ESC_ALTCHARSET
) {
856 case '0': /* Line drawing crap */
857 term
.c
.attr
.mode
|= ATTR_GFX
;
859 case 'B': /* Back to regular text */
860 term
.c
.attr
.mode
&= ~ATTR_GFX
;
863 printf("esc unhandled charset: ESC ( %c\n", c
);
875 term
.esc
|= ESC_ALTCHARSET
;
878 tmoveto(term
.c
.x
, term
.c
.y
-1);
882 tmoveto(term
.c
.x
, term
.c
.y
+1);
886 tmoveto(term
.c
.x
+1, term
.c
.y
);
889 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
890 tmoveto(term
.c
.x
-1, term
.c
.y
);
893 case 'E': /* NEL -- Next line */
897 case 'M': /* RI -- Reverse index */
898 if(term
.c
.y
== term
.top
)
901 tmoveto(term
.c
.x
, term
.c
.y
-1);
904 case 'c': /* RIS -- Reset to inital state */
908 case '=': /* DECPAM */
909 term
.mode
|= MODE_APPKEYPAD
;
912 case '>': /* DECPNM */
913 term
.mode
&= ~MODE_APPKEYPAD
;
917 tcursor(CURSOR_SAVE
);
921 tcursor(CURSOR_LOAD
);
925 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
935 tmoveto(term
.c
.x
-1, term
.c
.y
);
938 tmoveto(0, term
.c
.y
);
948 term
.esc
= ESC_START
;
952 if(term
.c
.x
+1 < term
.col
) {
953 tmoveto(term
.c
.x
+1, term
.c
.y
);
954 } else if(IS_SET(MODE_WRAP
))
962 tputs(char *s
, int len
) {
963 for(; len
> 0; len
--)
968 tresize(int col
, int row
) {
970 int minrow
= MIN(row
, term
.row
);
971 int mincol
= MIN(col
, term
.col
);
973 if(col
< 1 || row
< 1)
976 /* free uneeded rows */
977 for(i
= row
; i
< term
.row
; i
++)
980 /* resize to new height */
981 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
983 /* resize each row to new width, zero-pad if needed */
984 for(i
= 0; i
< minrow
; i
++) {
985 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
986 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
989 /* allocate any new rows */
990 for(/* i == minrow */; i
< row
; i
++)
991 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
993 LIMIT(term
.c
.x
, 0, col
-1);
994 LIMIT(term
.c
.y
, 0, row
-1);
995 LIMIT(term
.top
, 0, row
-1);
996 LIMIT(term
.bot
, 0, row
-1);
999 term
.col
= col
, term
.row
= row
;
1006 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1007 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1009 for(i
= 0; i
< 16; i
++) {
1010 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1012 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1014 dc
.col
[i
] = color
.pixel
;
1017 /* same colors as xterm */
1018 for(r
= 0; r
< 6; r
++)
1019 for(g
= 0; g
< 6; g
++)
1020 for(b
= 0; b
< 6; b
++) {
1021 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1022 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1023 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1024 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1026 fprintf(stderr
, "Could not allocate color %d\n", i
);
1028 dc
.col
[i
] = color
.pixel
;
1032 for(r
= 0; r
< 24; r
++, i
++) {
1033 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1034 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1036 fprintf(stderr
, "Could not allocate color %d\n", i
);
1038 dc
.col
[i
] = color
.pixel
;
1043 xclear(int x1
, int y1
, int x2
, int y2
) {
1044 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1045 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1046 x1
* xw
.cw
, y1
* xw
.ch
,
1047 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1053 XClassHint
class = {TNAME
, TNAME
};
1054 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1056 .flags
= PSize
| PResizeInc
| PBaseSize
,
1059 .height_inc
= xw
.ch
,
1061 .base_height
= 2*BORDER
,
1062 .base_width
= 2*BORDER
,
1064 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1069 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1070 die("Can't open display\n");
1071 xw
.scr
= XDefaultScreen(xw
.dis
);
1074 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1075 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1077 /* XXX: Assuming same size for bold font */
1078 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1079 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1084 term
.c
.attr
.fg
= DefaultFG
;
1085 term
.c
.attr
.bg
= DefaultBG
;
1086 term
.c
.attr
.mode
= ATTR_NULL
;
1088 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1089 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1090 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1094 xw
.bufw
= xw
.w
- 2*BORDER
;
1095 xw
.bufh
= xw
.h
- 2*BORDER
;
1096 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1098 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1099 XMapWindow(xw
.dis
, xw
.win
);
1101 XStoreName(xw
.dis
, xw
.win
, "st");
1106 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1107 unsigned long xfg
, xbg
;
1108 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1111 if(base
.mode
& ATTR_REVERSE
)
1112 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1114 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1116 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1117 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1119 if(base
.mode
& ATTR_GFX
)
1120 for(i
= 0; i
< len
; i
++)
1121 s
[i
] = gfx
[(int)s
[i
]];
1123 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1124 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1126 if(base
.mode
& ATTR_UNDERLINE
)
1127 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1132 static int oldx
= 0;
1133 static int oldy
= 0;
1134 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1136 LIMIT(oldx
, 0, term
.col
-1);
1137 LIMIT(oldy
, 0, term
.row
-1);
1139 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1140 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1142 /* remove the old cursor */
1143 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1144 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1146 xclear(oldx
, oldy
, oldx
, oldy
);
1148 /* draw the new one */
1149 if(mode
== CURSOR_DRAW
) {
1150 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1151 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1156 /* basic drawing routines */
1158 xdrawc(int x
, int y
, Glyph g
) {
1159 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1160 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1161 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1162 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1163 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1170 xclear(0, 0, term
.col
-1, term
.row
-1);
1171 for(y
= 0; y
< term
.row
; y
++)
1172 for(x
= 0; x
< term
.col
; x
++)
1173 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1174 xdrawc(x
, y
, term
.line
[y
][x
]);
1177 xcursor(CURSOR_DRAW
);
1178 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1183 /* optimized drawing routine */
1185 draw(int redraw_all
) {
1188 char buf
[DRAW_BUF_SIZ
];
1190 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1191 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1192 for(y
= 0; y
< term
.row
; y
++) {
1193 base
= term
.line
[y
][0];
1195 for(x
= 0; x
< term
.col
; x
++) {
1196 new = term
.line
[y
][x
];
1197 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1200 xdraws(buf
, base
, ox
, y
, i
);
1207 xdraws(buf
, base
, ox
, y
, i
);
1209 xcursor(term
.hidec
? CURSOR_HIDE
: CURSOR_DRAW
);
1210 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1217 expose(XEvent
*ev
) {
1218 draw(SCREEN_REDRAW
);
1224 for(i
= 0; i
< LEN(key
); i
++)
1226 return (char*)key
[i
].s
;
1231 kpress(XEvent
*ev
) {
1232 XKeyEvent
*e
= &ev
->xkey
;
1240 meta
= e
->state
& Mod1Mask
;
1241 shift
= e
->state
& ShiftMask
;
1242 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1244 if((customkey
= kmap(ksym
)))
1245 ttywrite(customkey
, strlen(customkey
));
1247 buf
[sizeof(buf
)-1] = '\0';
1248 if(meta
&& len
== 1)
1249 ttywrite("\033", 1);
1257 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1262 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1265 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1274 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1277 xw
.w
= e
->xconfigure
.width
;
1278 xw
.h
= e
->xconfigure
.height
;
1279 xw
.bufw
= xw
.w
- 2*BORDER
;
1280 xw
.bufh
= xw
.h
- 2*BORDER
;
1281 col
= xw
.bufw
/ xw
.cw
;
1282 row
= xw
.bufh
/ xw
.ch
;
1284 ttyresize(col
, row
);
1285 XFreePixmap(xw
.dis
, xw
.buf
);
1286 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1287 draw(SCREEN_REDRAW
);
1294 int xfd
= XConnectionNumber(xw
.dis
);
1297 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1298 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1302 FD_SET(cmdfd
, &rfd
);
1304 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1307 die("select failed: %s\n", SERRNO
);
1309 if(FD_ISSET(cmdfd
, &rfd
)) {
1311 draw(SCREEN_UPDATE
);
1313 while(XPending(xw
.dis
)) {
1314 XNextEvent(xw
.dis
, &ev
);
1315 if(handler
[ev
.type
])
1316 (handler
[ev
.type
])(&ev
);
1322 main(int argc
, char *argv
[]) {
1323 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1324 die("st-" VERSION
", (c) 2010 st engineers\n");
1326 die("usage: st [-v]\n");
1327 setlocale(LC_CTYPE
, "");