Xinqi Bao's Git
9b77a0b6fe0710d2f075bb20dda948e61af5430f
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 */
73 /* CSI Escape sequence structs */
74 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
76 char buf
[ESC_BUF_SIZ
]; /* raw string */
77 int len
; /* raw string length */
80 int narg
; /* nb of args */
84 /* Internal representation of the screen */
88 Line
* line
; /* screen */
89 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"); /* if getenv() failed */
225 putenv("TERM=" TNAME
);
226 execvp(args
[0], args
);
231 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
232 XFillRectangle(xw
.dis
, xw
.win
, dc
.gc
, BORDER
, BORDER
, xw
.bufw
, xw
.bufh
);
240 if(waitpid(pid
, &stat
, 0) < 0)
241 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
243 exit(WEXITSTATUS(stat
));
252 /* seems to work fine on linux, openbsd and freebsd */
253 struct winsize w
= {term
.row
, term
.col
, 0, 0};
254 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
255 die("openpty failed: %s\n", SERRNO
);
257 switch(pid
= fork()) {
259 die("fork failed\n");
262 setsid(); /* create a new process group */
263 dup2(s
, STDIN_FILENO
);
264 dup2(s
, STDOUT_FILENO
);
265 dup2(s
, STDERR_FILENO
);
266 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
267 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
275 signal(SIGCHLD
, sigchld
);
282 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
284 fprintf(stderr
, "\n");
289 char buf
[BUFSIZ
] = {0};
292 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
293 die("Couldn't read from shell: %s\n", SERRNO
);
299 ttywrite(const char *s
, size_t n
) {
300 if(write(cmdfd
, s
, n
) == -1)
301 die("write error on tty: %s\n", SERRNO
);
305 ttyresize(int x
, int y
) {
310 w
.ws_xpixel
= w
.ws_ypixel
= 0;
311 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
312 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
319 if(mode
== CURSOR_SAVE
)
321 else if(mode
== CURSOR_LOAD
)
322 term
.c
= c
, tmoveto(c
.x
, c
.y
);
331 }, .x
= 0, .y
= 0, .hide
= 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
) {
340 /* set screen size */
341 term
.row
= row
, term
.col
= col
;
342 term
.line
= malloc(term
.row
* sizeof(Line
));
343 for(row
= 0 ; row
< term
.row
; row
++)
344 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
350 tscrolldown (int n
) {
354 LIMIT(n
, 0, term
.bot
-term
.top
+1);
356 for(i
= 0; i
< n
; i
++)
357 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
359 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
361 term
.line
[i
] = term
.line
[i
-n
];
362 term
.line
[i
-n
] = temp
;
370 LIMIT(n
, 0, term
.bot
-term
.top
+1);
372 for(i
= 0; i
< n
; i
++)
373 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
375 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
377 term
.line
[i
] = term
.line
[i
+n
];
378 term
.line
[i
+n
] = temp
;
384 int y
= term
.c
.y
+ 1;
386 tscrollup(1), y
= term
.bot
;
393 char *p
= escseq
.buf
;
397 escseq
.priv
= 1, p
++;
399 while(p
< escseq
.buf
+escseq
.len
) {
401 escseq
.arg
[escseq
.narg
] *= 10;
402 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
404 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
415 tmoveto(int x
, int y
) {
416 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
417 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
422 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
423 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
424 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
428 tclearregion(int x1
, int y1
, int x2
, int y2
) {
432 temp
= x1
, x1
= x2
, x2
= temp
;
434 temp
= y1
, y1
= y2
, y2
= temp
;
436 LIMIT(x1
, 0, term
.col
-1);
437 LIMIT(x2
, 0, term
.col
-1);
438 LIMIT(y1
, 0, term
.row
-1);
439 LIMIT(y2
, 0, term
.row
-1);
441 for(y
= y1
; y
<= y2
; y
++)
442 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
447 int src
= term
.c
.x
+ n
;
449 int size
= term
.col
- src
;
451 if(src
>= term
.col
) {
452 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
455 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
456 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
460 tinsertblank(int n
) {
463 int size
= term
.col
- dst
;
465 if(dst
>= term
.col
) {
466 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
469 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
470 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
474 tinsertblankline(int n
) {
479 if(term
.c
.y
> term
.bot
)
481 else if(term
.c
.y
< term
.top
)
483 if(term
.c
.y
+ n
>= bot
) {
484 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
487 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
488 /* swap deleted line <-> blanked line */
489 blank
= term
.line
[i
];
490 term
.line
[i
] = term
.line
[i
-n
];
491 term
.line
[i
-n
] = blank
;
493 memset(blank
, 0, term
.col
* sizeof(Glyph
));
503 if(term
.c
.y
> term
.bot
)
505 else if(term
.c
.y
< term
.top
)
507 if(term
.c
.y
+ n
>= bot
) {
508 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
511 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
512 /* swap deleted line <-> blanked line */
513 blank
= term
.line
[i
];
514 term
.line
[i
] = term
.line
[i
+n
];
515 term
.line
[i
+n
] = blank
;
517 memset(blank
, 0, term
.col
* sizeof(Glyph
));
522 tsetattr(int *attr
, int l
) {
525 for(i
= 0; i
< l
; i
++) {
528 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
529 term
.c
.attr
.fg
= DefaultFG
;
530 term
.c
.attr
.bg
= DefaultBG
;
533 term
.c
.attr
.mode
|= ATTR_BOLD
;
536 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
539 term
.c
.attr
.mode
|= ATTR_REVERSE
;
542 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
545 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
548 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
551 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
553 if (BETWEEN(attr
[i
], 0, 255))
554 term
.c
.attr
.fg
= attr
[i
];
556 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
559 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
562 term
.c
.attr
.fg
= DefaultFG
;
565 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
567 if (BETWEEN(attr
[i
], 0, 255))
568 term
.c
.attr
.bg
= attr
[i
];
570 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
573 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
576 term
.c
.attr
.bg
= DefaultBG
;
579 if(BETWEEN(attr
[i
], 30, 37))
580 term
.c
.attr
.fg
= attr
[i
] - 30;
581 else if(BETWEEN(attr
[i
], 40, 47))
582 term
.c
.attr
.bg
= attr
[i
] - 40;
583 else if(BETWEEN(attr
[i
], 90, 97))
584 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
585 else if(BETWEEN(attr
[i
], 100, 107))
586 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
588 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
595 tsetscroll(int t
, int b
) {
598 LIMIT(t
, 0, term
.row
-1);
599 LIMIT(b
, 0, term
.row
-1);
611 switch(escseq
.mode
) {
614 printf("erresc: unknown csi ");
618 case '@': /* ICH -- Insert <n> blank char */
619 DEFAULT(escseq
.arg
[0], 1);
620 tinsertblank(escseq
.arg
[0]);
622 case 'A': /* CUU -- Cursor <n> Up */
624 DEFAULT(escseq
.arg
[0], 1);
625 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
627 case 'B': /* CUD -- Cursor <n> Down */
628 DEFAULT(escseq
.arg
[0], 1);
629 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
631 case 'C': /* CUF -- Cursor <n> Forward */
633 DEFAULT(escseq
.arg
[0], 1);
634 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
636 case 'D': /* CUB -- Cursor <n> Backward */
637 DEFAULT(escseq
.arg
[0], 1);
638 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
640 case 'E': /* CNL -- Cursor <n> Down and first col */
641 DEFAULT(escseq
.arg
[0], 1);
642 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
644 case 'F': /* CPL -- Cursor <n> Up and first col */
645 DEFAULT(escseq
.arg
[0], 1);
646 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
648 case 'G': /* CHA -- Move to <col> */
649 case '`': /* XXX: HPA -- same? */
650 DEFAULT(escseq
.arg
[0], 1);
651 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
653 case 'H': /* CUP -- Move to <row> <col> */
654 case 'f': /* XXX: HVP -- same? */
655 DEFAULT(escseq
.arg
[0], 1);
656 DEFAULT(escseq
.arg
[1], 1);
657 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
659 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
660 case 'J': /* ED -- Clear screen */
661 switch(escseq
.arg
[0]) {
663 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
666 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
669 tclearregion(0, 0, term
.col
-1, term
.row
-1);
671 case 3: /* XXX: erase saved lines (xterm) */
676 case 'K': /* EL -- Clear line */
677 switch(escseq
.arg
[0]) {
679 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
682 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
685 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
689 case 'S': /* SU -- Scroll <n> line up */
690 DEFAULT(escseq
.arg
[0], 1);
691 tscrollup(escseq
.arg
[0]);
693 case 'T': /* SD -- Scroll <n> line down */
694 DEFAULT(escseq
.arg
[0], 1);
695 tscrolldown(escseq
.arg
[0]);
697 case 'L': /* IL -- Insert <n> blank lines */
698 DEFAULT(escseq
.arg
[0], 1);
699 tinsertblankline(escseq
.arg
[0]);
701 case 'l': /* RM -- Reset Mode */
703 switch(escseq
.arg
[0]) {
705 term
.mode
&= ~MODE_APPKEYPAD
;
708 term
.mode
&= ~MODE_WRAP
;
710 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
715 case 1048: /* XXX: no alt. screen to erase/save */
717 tcursor(CURSOR_LOAD
);
718 tclearregion(0, 0, term
.col
-1, term
.row
-1);
724 switch(escseq
.arg
[0]) {
726 term
.mode
&= ~MODE_INSERT
;
733 case 'M': /* DL -- Delete <n> lines */
734 DEFAULT(escseq
.arg
[0], 1);
735 tdeleteline(escseq
.arg
[0]);
737 case 'X': /* ECH -- Erase <n> char */
738 DEFAULT(escseq
.arg
[0], 1);
739 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
741 case 'P': /* DCH -- Delete <n> char */
742 DEFAULT(escseq
.arg
[0], 1);
743 tdeletechar(escseq
.arg
[0]);
745 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
746 case 'd': /* VPA -- Move to <row> */
747 DEFAULT(escseq
.arg
[0], 1);
748 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
750 case 'h': /* SM -- Set terminal mode */
752 switch(escseq
.arg
[0]) {
754 term
.mode
|= MODE_APPKEYPAD
;
757 term
.mode
|= MODE_WRAP
;
759 case 12: /* att610 -- Start blinking cursor (IGNORED) */
765 case 1049: /* XXX: no alt. screen to erase/save */
766 tcursor(CURSOR_SAVE
);
767 tclearregion(0, 0, term
.col
-1, term
.row
-1);
769 default: goto unknown
;
772 switch(escseq
.arg
[0]) {
774 term
.mode
|= MODE_INSERT
;
776 default: goto unknown
;
780 case 'm': /* SGR -- Terminal attribute (color) */
781 tsetattr(escseq
.arg
, escseq
.narg
);
783 case 'r': /* DECSTBM -- Set Scrolling Region */
787 DEFAULT(escseq
.arg
[0], 1);
788 DEFAULT(escseq
.arg
[1], term
.row
);
789 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
792 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
793 tcursor(CURSOR_SAVE
);
795 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
796 tcursor(CURSOR_LOAD
);
804 printf("ESC [ %s", escseq
.priv
? "? " : "");
806 for(i
= 0; i
< escseq
.narg
; i
++)
807 printf("%d ", escseq
.arg
[i
]);
809 putchar(escseq
.mode
);
815 memset(&escseq
, 0, sizeof(escseq
));
820 int space
= TAB
- term
.c
.x
% TAB
;
821 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
826 if(term
.esc
& ESC_START
) {
827 if(term
.esc
& ESC_CSI
) {
828 escseq
.buf
[escseq
.len
++] = c
;
829 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
831 csiparse(), csihandle();
833 } else if(term
.esc
& ESC_OSC
) {
836 term
.esc
= ESC_START
| ESC_TITLE
;
838 } else if(term
.esc
& ESC_TITLE
) {
839 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
841 term
.title
[term
.titlelen
] = '\0';
842 XStoreName(xw
.dis
, xw
.win
, term
.title
);
844 term
.title
[term
.titlelen
++] = c
;
846 } else if(term
.esc
& ESC_ALTCHARSET
) {
848 case '0': /* Line drawing crap */
849 term
.c
.attr
.mode
|= ATTR_GFX
;
851 case 'B': /* Back to regular text */
852 term
.c
.attr
.mode
&= ~ATTR_GFX
;
855 printf("esc unhandled charset: ESC ( %c\n", c
);
867 term
.esc
|= ESC_ALTCHARSET
;
870 tmoveto(term
.c
.x
, term
.c
.y
-1);
874 tmoveto(term
.c
.x
, term
.c
.y
+1);
878 tmoveto(term
.c
.x
+1, term
.c
.y
);
881 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
882 tmoveto(term
.c
.x
-1, term
.c
.y
);
885 case 'E': /* NEL -- Next line */
889 case 'M': /* RI -- Reverse index */
890 if(term
.c
.y
== term
.top
)
893 tmoveto(term
.c
.x
, term
.c
.y
-1);
896 case 'c': /* RIS -- Reset to inital state */
900 case '=': /* DECPAM */
901 term
.mode
|= MODE_APPKEYPAD
;
904 case '>': /* DECPNM */
905 term
.mode
&= ~MODE_APPKEYPAD
;
909 tcursor(CURSOR_SAVE
);
913 tcursor(CURSOR_LOAD
);
917 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
927 tmoveto(term
.c
.x
-1, term
.c
.y
);
930 tmoveto(0, term
.c
.y
);
940 term
.esc
= ESC_START
;
944 if(term
.c
.x
+1 < term
.col
) {
945 tmoveto(term
.c
.x
+1, term
.c
.y
);
946 } else if(IS_SET(MODE_WRAP
))
954 tputs(char *s
, int len
) {
955 for(; len
> 0; len
--)
960 tresize(int col
, int row
) {
962 int minrow
= MIN(row
, term
.row
);
963 int mincol
= MIN(col
, term
.col
);
965 if(col
< 1 || row
< 1)
968 /* free uneeded rows */
969 for(i
= row
; i
< term
.row
; i
++)
972 /* resize to new height */
973 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
975 /* resize each row to new width, zero-pad if needed */
976 for(i
= 0; i
< minrow
; i
++) {
977 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
978 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
981 /* allocate any new rows */
982 for(/* i == minrow */; i
< row
; i
++)
983 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
985 LIMIT(term
.c
.x
, 0, col
-1);
986 LIMIT(term
.c
.y
, 0, row
-1);
987 LIMIT(term
.top
, 0, row
-1);
988 LIMIT(term
.bot
, 0, row
-1);
991 term
.col
= col
, term
.row
= row
;
998 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
999 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1001 for(i
= 0; i
< 16; i
++) {
1002 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1004 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1006 dc
.col
[i
] = color
.pixel
;
1009 /* same colors as xterm */
1010 for(r
= 0; r
< 6; r
++)
1011 for(g
= 0; g
< 6; g
++)
1012 for(b
= 0; b
< 6; b
++) {
1013 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1014 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1015 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1016 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1018 fprintf(stderr
, "Could not allocate color %d\n", i
);
1020 dc
.col
[i
] = color
.pixel
;
1024 for(r
= 0; r
< 24; r
++, i
++) {
1025 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1026 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1028 fprintf(stderr
, "Could not allocate color %d\n", i
);
1030 dc
.col
[i
] = color
.pixel
;
1035 xclear(int x1
, int y1
, int x2
, int y2
) {
1036 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1037 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1038 x1
* xw
.cw
, y1
* xw
.ch
,
1039 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1045 XClassHint
class = {TNAME
, TNAME
};
1046 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1048 .flags
= PSize
| PResizeInc
| PBaseSize
,
1051 .height_inc
= xw
.ch
,
1053 .base_height
= 2*BORDER
,
1054 .base_width
= 2*BORDER
,
1056 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1061 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1062 die("Can't open display\n");
1063 xw
.scr
= XDefaultScreen(xw
.dis
);
1066 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1067 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1069 /* XXX: Assuming same size for bold font */
1070 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1071 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1076 term
.c
.attr
.fg
= DefaultFG
;
1077 term
.c
.attr
.bg
= DefaultBG
;
1078 term
.c
.attr
.mode
= ATTR_NULL
;
1080 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1081 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1082 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1086 xw
.bufw
= xw
.w
- 2*BORDER
;
1087 xw
.bufh
= xw
.h
- 2*BORDER
;
1088 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1090 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1091 XMapWindow(xw
.dis
, xw
.win
);
1093 XStoreName(xw
.dis
, xw
.win
, "st");
1098 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1099 unsigned long xfg
, xbg
;
1100 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1103 if(base
.mode
& ATTR_REVERSE
)
1104 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1106 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1108 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1109 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1111 if(base
.mode
& ATTR_GFX
)
1112 for(i
= 0; i
< len
; i
++)
1113 s
[i
] = gfx
[(int)s
[i
]];
1115 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1116 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1118 if(base
.mode
& ATTR_UNDERLINE
)
1119 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1124 static int oldx
= 0;
1125 static int oldy
= 0;
1126 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1128 LIMIT(oldx
, 0, term
.col
-1);
1129 LIMIT(oldy
, 0, term
.row
-1);
1131 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1132 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1134 /* remove the old cursor */
1135 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1136 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1138 xclear(oldx
, oldy
, oldx
, oldy
);
1140 /* draw the new one */
1141 if(mode
== CURSOR_DRAW
) {
1142 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1143 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1148 /* basic drawing routines */
1150 xdrawc(int x
, int y
, Glyph g
) {
1151 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1152 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1153 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1154 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1155 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1162 xclear(0, 0, term
.col
-1, term
.row
-1);
1163 for(y
= 0; y
< term
.row
; y
++)
1164 for(x
= 0; x
< term
.col
; x
++)
1165 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1166 xdrawc(x
, y
, term
.line
[y
][x
]);
1169 xcursor(CURSOR_DRAW
);
1170 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1175 /* optimized drawing routine */
1177 draw(int redraw_all
) {
1180 char buf
[DRAW_BUF_SIZ
];
1182 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1183 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
1184 for(y
= 0; y
< term
.row
; y
++) {
1185 base
= term
.line
[y
][0];
1187 for(x
= 0; x
< term
.col
; x
++) {
1188 new = term
.line
[y
][x
];
1189 if(!ATTRCMP(base
, new) && i
< DRAW_BUF_SIZ
)
1192 xdraws(buf
, base
, ox
, y
, i
);
1199 xdraws(buf
, base
, ox
, y
, i
);
1201 xcursor(term
.c
.hide
? CURSOR_HIDE
: CURSOR_DRAW
);
1202 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1209 expose(XEvent
*ev
) {
1210 draw(SCREEN_REDRAW
);
1216 for(i
= 0; i
< LEN(key
); i
++)
1218 return (char*)key
[i
].s
;
1223 kpress(XEvent
*ev
) {
1224 XKeyEvent
*e
= &ev
->xkey
;
1232 meta
= e
->state
& Mod1Mask
;
1233 shift
= e
->state
& ShiftMask
;
1234 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1236 if((customkey
= kmap(ksym
)))
1237 ttywrite(customkey
, strlen(customkey
));
1239 buf
[sizeof(buf
)-1] = '\0';
1240 if(meta
&& len
== 1)
1241 ttywrite("\033", 1);
1249 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1254 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1257 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1266 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1269 xw
.w
= e
->xconfigure
.width
;
1270 xw
.h
= e
->xconfigure
.height
;
1271 xw
.bufw
= xw
.w
- 2*BORDER
;
1272 xw
.bufh
= xw
.h
- 2*BORDER
;
1273 col
= xw
.bufw
/ xw
.cw
;
1274 row
= xw
.bufh
/ xw
.ch
;
1276 ttyresize(col
, row
);
1277 XFreePixmap(xw
.dis
, xw
.buf
);
1278 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1279 draw(SCREEN_REDRAW
);
1286 int xfd
= XConnectionNumber(xw
.dis
);
1289 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1290 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1294 FD_SET(cmdfd
, &rfd
);
1296 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1299 die("select failed: %s\n", SERRNO
);
1301 if(FD_ISSET(cmdfd
, &rfd
)) {
1303 draw(SCREEN_UPDATE
);
1305 while(XPending(xw
.dis
)) {
1306 XNextEvent(xw
.dis
, &ev
);
1307 if(handler
[ev
.type
])
1308 (handler
[ev
.type
])(&ev
);
1314 main(int argc
, char *argv
[]) {
1315 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1316 die("st-" VERSION
", (c) 2010 st engineers\n");
1318 die("usage: st [-v]\n");
1319 setlocale(LC_CTYPE
, "");