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
,
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 };
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 TCursor c
; /* cursor */
91 int top
; /* top scroll limit */
92 int bot
; /* bottom scroll limit */
93 int mode
; /* terminal mode flags */
94 int esc
; /* escape state flags */
95 char title
[ESC_TITLE_SIZ
];
99 /* Purely graphic info */
105 int w
; /* window width */
106 int h
; /* window height */
107 int bufw
; /* pixmap width */
108 int bufh
; /* pixmap height */
109 int ch
; /* char height */
110 int cw
; /* char width */
119 /* Drawing Context */
121 unsigned long col
[256];
129 static void die(const char *errstr
, ...);
130 static void draw(int);
131 static void execsh(void);
132 static void sigchld(int);
133 static void run(void);
135 static void csidump(void);
136 static void csihandle(void);
137 static void csiparse(void);
138 static void csireset(void);
140 static void tclearregion(int, int, int, int);
141 static void tcursor(int);
142 static void tdeletechar(int);
143 static void tdeleteline(int);
144 static void tinsertblank(int);
145 static void tinsertblankline(int);
146 static void tmoveto(int, int);
147 static void tnew(int, int);
148 static void tnewline(void);
149 static void tputtab(void);
150 static void tputc(char);
151 static void tputs(char*, int);
152 static void treset(void);
153 static void tresize(int, int);
154 static void tscrollup(int);
155 static void tscrolldown(int);
156 static void tsetattr(int*, int);
157 static void tsetchar(char);
158 static void tsetscroll(int, int);
160 static void ttynew(void);
161 static void ttyread(void);
162 static void ttyresize(int, int);
163 static void ttywrite(const char *, size_t);
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 xdrawcursor(void);
169 static void xinit(void);
170 static void xloadcols(void);
171 static void xseturgency(int);
173 static void expose(XEvent
*);
174 static char* kmap(KeySym
);
175 static void kpress(XEvent
*);
176 static void resize(XEvent
*);
177 static void focus(XEvent
*);
180 static void (*handler
[LASTEvent
])(XEvent
*) = {
183 [ConfigureNotify
] = resize
,
192 static CSIEscape escseq
;
202 for(row
= 0; row
< term
.row
; row
++) {
203 for(col
= 0; col
< term
.col
; col
++) {
204 if(col
== term
.c
.x
&& row
== term
.c
.y
)
207 c
= term
.line
[row
][col
];
208 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
217 die(const char *errstr
, ...) {
220 va_start(ap
, errstr
);
221 vfprintf(stderr
, errstr
, ap
);
228 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
229 DEFAULT(args
[0], "/bin/sh"); /* if getenv() failed */
230 putenv("TERM=" TNAME
);
231 execvp(args
[0], args
);
237 if(waitpid(pid
, &stat
, 0) < 0)
238 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
240 exit(WEXITSTATUS(stat
));
249 /* seems to work fine on linux, openbsd and freebsd */
250 struct winsize w
= {term
.row
, term
.col
, 0, 0};
251 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
252 die("openpty failed: %s\n", SERRNO
);
254 switch(pid
= fork()) {
256 die("fork failed\n");
259 setsid(); /* create a new process group */
260 dup2(s
, STDIN_FILENO
);
261 dup2(s
, STDOUT_FILENO
);
262 dup2(s
, STDERR_FILENO
);
263 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
264 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
272 signal(SIGCHLD
, sigchld
);
279 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
281 fprintf(stderr
, "\n");
286 char buf
[BUFSIZ
] = {0};
289 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
290 die("Couldn't read from shell: %s\n", SERRNO
);
296 ttywrite(const char *s
, size_t n
) {
297 if(write(cmdfd
, s
, n
) == -1)
298 die("write error on tty: %s\n", SERRNO
);
302 ttyresize(int x
, int y
) {
307 w
.ws_xpixel
= w
.ws_ypixel
= 0;
308 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
309 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
316 if(mode
== CURSOR_SAVE
)
318 else if(mode
== CURSOR_LOAD
)
319 term
.c
= c
, tmoveto(c
.x
, c
.y
);
328 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
330 term
.top
= 0, term
.bot
= term
.row
- 1;
331 term
.mode
= MODE_WRAP
;
332 tclearregion(0, 0, term
.col
-1, term
.row
-1);
336 tnew(int col
, int row
) {
337 /* set screen size */
338 term
.row
= row
, term
.col
= col
;
339 term
.line
= malloc(term
.row
* sizeof(Line
));
340 for(row
= 0 ; row
< term
.row
; row
++)
341 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
347 tscrolldown (int n
) {
351 LIMIT(n
, 0, term
.bot
-term
.top
+1);
353 for(i
= 0; i
< n
; i
++)
354 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
356 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
358 term
.line
[i
] = term
.line
[i
-n
];
359 term
.line
[i
-n
] = temp
;
367 LIMIT(n
, 0, term
.bot
-term
.top
+1);
369 for(i
= 0; i
< n
; i
++)
370 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
372 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
374 term
.line
[i
] = term
.line
[i
+n
];
375 term
.line
[i
+n
] = temp
;
381 int y
= term
.c
.y
+ 1;
383 tscrollup(1), y
= term
.bot
;
390 char *p
= escseq
.buf
;
394 escseq
.priv
= 1, p
++;
396 while(p
< escseq
.buf
+escseq
.len
) {
398 escseq
.arg
[escseq
.narg
] *= 10;
399 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
401 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
412 tmoveto(int x
, int y
) {
413 LIMIT(x
, 0, term
.col
-1);
414 LIMIT(y
, 0, term
.row
-1);
415 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
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) */
713 term
.c
.state
|= CURSOR_HIDE
;
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) */
762 term
.c
.state
&= ~CURSOR_HIDE
;
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);
793 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
794 tcursor(CURSOR_SAVE
);
796 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
797 tcursor(CURSOR_LOAD
);
805 printf("ESC [ %s", escseq
.priv
? "? " : "");
807 for(i
= 0; i
< escseq
.narg
; i
++)
808 printf("%d ", escseq
.arg
[i
]);
810 putchar(escseq
.mode
);
816 memset(&escseq
, 0, sizeof(escseq
));
821 int space
= TAB
- term
.c
.x
% TAB
;
822 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
827 if(term
.esc
& ESC_START
) {
828 if(term
.esc
& ESC_CSI
) {
829 escseq
.buf
[escseq
.len
++] = c
;
830 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
832 csiparse(), csihandle();
834 } else if(term
.esc
& ESC_OSC
) {
837 term
.esc
= ESC_START
| ESC_TITLE
;
839 } else if(term
.esc
& ESC_TITLE
) {
840 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
842 term
.title
[term
.titlelen
] = '\0';
843 XStoreName(xw
.dis
, xw
.win
, term
.title
);
845 term
.title
[term
.titlelen
++] = c
;
847 } else if(term
.esc
& ESC_ALTCHARSET
) {
849 case '0': /* Line drawing crap */
850 term
.c
.attr
.mode
|= ATTR_GFX
;
852 case 'B': /* Back to regular text */
853 term
.c
.attr
.mode
&= ~ATTR_GFX
;
856 printf("esc unhandled charset: ESC ( %c\n", c
);
868 term
.esc
|= ESC_ALTCHARSET
;
870 case 'D': /* IND -- Linefeed */
871 if(term
.c
.y
== term
.bot
)
874 tmoveto(term
.c
.x
, term
.c
.y
+1);
877 case 'E': /* NEL -- Next line */
881 case 'M': /* RI -- Reverse index */
882 if(term
.c
.y
== term
.top
)
885 tmoveto(term
.c
.x
, term
.c
.y
-1);
888 case 'c': /* RIS -- Reset to inital state */
892 case '=': /* DECPAM */
893 term
.mode
|= MODE_APPKEYPAD
;
896 case '>': /* DECPNM */
897 term
.mode
&= ~MODE_APPKEYPAD
;
901 tcursor(CURSOR_SAVE
);
905 tcursor(CURSOR_LOAD
);
909 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
919 tmoveto(term
.c
.x
-1, term
.c
.y
);
922 tmoveto(0, term
.c
.y
);
933 term
.esc
= ESC_START
;
936 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
939 if(term
.c
.x
+1 < term
.col
)
940 tmoveto(term
.c
.x
+1, term
.c
.y
);
942 term
.c
.state
|= CURSOR_WRAPNEXT
;
949 tputs(char *s
, int len
) {
950 for(; len
> 0; len
--)
955 tresize(int col
, int row
) {
957 int minrow
= MIN(row
, term
.row
);
958 int mincol
= MIN(col
, term
.col
);
960 if(col
< 1 || row
< 1)
963 /* free uneeded rows */
964 for(i
= row
; i
< term
.row
; i
++)
967 /* resize to new height */
968 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
970 /* resize each row to new width, zero-pad if needed */
971 for(i
= 0; i
< minrow
; i
++) {
972 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
973 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
976 /* allocate any new rows */
977 for(/* i == minrow */; i
< row
; i
++)
978 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
980 /* update terminal size */
981 term
.col
= col
, term
.row
= row
;
982 /* make use of the LIMIT in tmoveto */
983 tmoveto(term
.c
.x
, term
.c
.y
);
984 /* reset scrolling region */
985 tsetscroll(0, row
-1);
992 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
993 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
995 for(i
= 0; i
< 16; i
++) {
996 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
998 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1000 dc
.col
[i
] = color
.pixel
;
1003 /* same colors as xterm */
1004 for(r
= 0; r
< 6; r
++)
1005 for(g
= 0; g
< 6; g
++)
1006 for(b
= 0; b
< 6; b
++) {
1007 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1008 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1009 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1010 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1012 fprintf(stderr
, "Could not allocate color %d\n", i
);
1014 dc
.col
[i
] = color
.pixel
;
1018 for(r
= 0; r
< 24; r
++, i
++) {
1019 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1020 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1022 fprintf(stderr
, "Could not allocate color %d\n", i
);
1024 dc
.col
[i
] = color
.pixel
;
1029 xclear(int x1
, int y1
, int x2
, int y2
) {
1030 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1031 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1032 x1
* xw
.cw
, y1
* xw
.ch
,
1033 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1039 XClassHint
class = {TNAME
, TNAME
};
1040 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1042 .flags
= PSize
| PResizeInc
| PBaseSize
,
1045 .height_inc
= xw
.ch
,
1047 .base_height
= 2*BORDER
,
1048 .base_width
= 2*BORDER
,
1050 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1055 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1056 die("Can't open display\n");
1057 xw
.scr
= XDefaultScreen(xw
.dis
);
1060 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1061 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1063 /* XXX: Assuming same size for bold font */
1064 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1065 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1071 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1072 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1073 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1077 xw
.bufw
= xw
.w
- 2*BORDER
;
1078 xw
.bufh
= xw
.h
- 2*BORDER
;
1079 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1081 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1082 XMapWindow(xw
.dis
, xw
.win
);
1084 XStoreName(xw
.dis
, xw
.win
, "st");
1089 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1090 unsigned long xfg
, xbg
;
1091 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1094 if(base
.mode
& ATTR_REVERSE
)
1095 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1097 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1099 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1100 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1102 if(base
.mode
& ATTR_GFX
)
1103 for(i
= 0; i
< len
; i
++)
1104 s
[i
] = gfx
[(int)s
[i
]];
1106 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1107 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1109 if(base
.mode
& ATTR_UNDERLINE
)
1110 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1115 static int oldx
= 0;
1116 static int oldy
= 0;
1117 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1119 LIMIT(oldx
, 0, term
.col
-1);
1120 LIMIT(oldy
, 0, term
.row
-1);
1122 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1123 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1125 /* remove the old cursor */
1126 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1127 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1129 xclear(oldx
, oldy
, oldx
, oldy
);
1131 /* draw the new one */
1132 if(!(term
.c
.state
& CURSOR_HIDE
)) {
1133 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1134 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1139 /* basic drawing routines */
1141 xdrawc(int x
, int y
, Glyph g
) {
1142 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1143 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1144 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1145 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1146 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1153 xclear(0, 0, term
.col
-1, term
.row
-1);
1154 for(y
= 0; y
< term
.row
; y
++)
1155 for(x
= 0; x
< term
.col
; x
++)
1156 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1157 xdrawc(x
, y
, term
.line
[y
][x
]);
1160 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1165 /* optimized drawing routine */
1167 draw(int redraw_all
) {
1170 char buf
[DRAW_BUF_SIZ
];
1172 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1173 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
);
1174 for(y
= 0; y
< term
.row
; y
++) {
1175 base
= term
.line
[y
][0];
1177 for(x
= 0; x
< term
.col
; x
++) {
1178 new = term
.line
[y
][x
];
1179 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1180 i
>= DRAW_BUF_SIZ
)) {
1181 xdraws(buf
, base
, ox
, y
, i
);
1184 if(new.state
& GLYPH_SET
) {
1193 xdraws(buf
, base
, ox
, y
, i
);
1196 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1203 expose(XEvent
*ev
) {
1204 draw(SCREEN_REDRAW
);
1208 xseturgency(int add
) {
1209 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1210 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1211 XSetWMHints(xw
.dis
, xw
.win
, h
);
1217 if((xw
.hasfocus
= ev
->type
== FocusIn
))
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
);
1295 long mask
= ExposureMask
| KeyPressMask
| StructureNotifyMask
| FocusChangeMask
;
1297 XSelectInput(xw
.dis
, xw
.win
, mask
);
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
) < 0) {
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
, "");