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>
21 #include <X11/keysym.h>
22 #include <X11/Xutil.h>
26 #elif defined(OPENBSD)
28 #elif defined(FREEBSD)
33 #define ESC_TITLE_SIZ 256
34 #define ESC_BUF_SIZ 256
35 #define ESC_ARG_SIZ 16
36 #define DRAW_BUF_SIZ 1024
38 #define SERRNO strerror(errno)
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 #define MAX(a, b) ((a) < (b) ? (b) : (a))
41 #define LEN(a) (sizeof(a) / sizeof(a[0]))
42 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
43 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
44 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
45 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
46 #define IS_SET(flag) (term.mode & (flag))
48 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
49 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
50 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
, CURSOR_HIDE
= 1,
51 CURSOR_DRAW
= 0, CURSOR_SAVE
, CURSOR_LOAD
};
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 */
118 /* Drawing Context */
120 unsigned long col
[256];
128 static void die(const char *errstr
, ...);
129 static void draw(int);
130 static void execsh(void);
131 static void sigchld(int);
132 static void run(void);
134 static void csidump(void);
135 static void csihandle(void);
136 static void csiparse(void);
137 static void csireset(void);
139 static void tclearregion(int, int, int, int);
140 static void tcursor(int);
141 static void tdeletechar(int);
142 static void tdeleteline(int);
143 static void tinsertblank(int);
144 static void tinsertblankline(int);
145 static void tmoveto(int, int);
146 static void tnew(int, int);
147 static void tnewline(void);
148 static void tputtab(void);
149 static void tputc(char);
150 static void tputs(char*, int);
151 static void treset(void);
152 static void tresize(int, int);
153 static void tscrollup(int);
154 static void tscrolldown(int);
155 static void tsetattr(int*, int);
156 static void tsetchar(char);
157 static void tsetscroll(int, int);
159 static void ttynew(void);
160 static void ttyread(void);
161 static void ttyresize(int, int);
162 static void ttywrite(const char *, size_t);
164 static void xbell(void);
165 static void xdraws(char *, Glyph
, int, int, int);
166 static void xhints(void);
167 static void xclear(int, int, int, int);
168 static void xcursor(int);
169 static void xinit(void);
170 static void xloadcols(void);
172 static void expose(XEvent
*);
173 static char* kmap(KeySym
);
174 static void kpress(XEvent
*);
175 static void resize(XEvent
*);
177 static void (*handler
[LASTEvent
])(XEvent
*) = {
180 [ConfigureNotify
] = resize
187 static CSIEscape escseq
;
198 for(row
= 0; row
< term
.row
; row
++) {
199 for(col
= 0; col
< term
.col
; col
++) {
200 if(col
== term
.c
.x
&& row
== term
.c
.y
)
203 c
= term
.line
[row
][col
];
204 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
213 die(const char *errstr
, ...) {
216 va_start(ap
, errstr
);
217 vfprintf(stderr
, errstr
, ap
);
224 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
225 DEFAULT(args
[0], "/bin/sh"); /* if getenv() failed */
226 putenv("TERM=" TNAME
);
227 execvp(args
[0], args
);
232 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
233 XFillRectangle(xw
.dis
, xw
.win
, dc
.gc
, BORDER
, BORDER
, xw
.bufw
, xw
.bufh
);
242 if(waitpid(pid
, &stat
, 0) < 0)
243 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
245 exit(WEXITSTATUS(stat
));
254 /* seems to work fine on linux, openbsd and freebsd */
255 struct winsize w
= {term
.row
, term
.col
, 0, 0};
256 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
257 die("openpty failed: %s\n", SERRNO
);
259 switch(pid
= fork()) {
261 die("fork failed\n");
264 setsid(); /* create a new process group */
265 dup2(s
, STDIN_FILENO
);
266 dup2(s
, STDOUT_FILENO
);
267 dup2(s
, STDERR_FILENO
);
268 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
269 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
277 signal(SIGCHLD
, sigchld
);
284 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
286 fprintf(stderr
, "\n");
291 char buf
[BUFSIZ
] = {0};
294 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
295 die("Couldn't read from shell: %s\n", SERRNO
);
301 ttywrite(const char *s
, size_t n
) {
302 if(write(cmdfd
, s
, n
) == -1)
303 die("write error on tty: %s\n", SERRNO
);
307 ttyresize(int x
, int y
) {
312 w
.ws_xpixel
= w
.ws_ypixel
= 0;
313 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
314 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
321 if(mode
== CURSOR_SAVE
)
323 else if(mode
== CURSOR_LOAD
)
324 term
.c
= c
, tmoveto(c
.x
, c
.y
);
333 }, .x
= 0, .y
= 0, .hide
= 0};
335 term
.top
= 0, term
.bot
= term
.row
- 1;
336 term
.mode
= MODE_WRAP
;
337 tclearregion(0, 0, term
.col
-1, term
.row
-1);
341 tnew(int col
, int row
) {
342 /* set screen size */
343 term
.row
= row
, term
.col
= col
;
344 term
.line
= malloc(term
.row
* sizeof(Line
));
345 for(row
= 0 ; row
< term
.row
; row
++)
346 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
352 tscrolldown (int n
) {
356 LIMIT(n
, 0, term
.bot
-term
.top
+1);
358 for(i
= 0; i
< n
; i
++)
359 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
361 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
363 term
.line
[i
] = term
.line
[i
-n
];
364 term
.line
[i
-n
] = temp
;
372 LIMIT(n
, 0, term
.bot
-term
.top
+1);
374 for(i
= 0; i
< n
; i
++)
375 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
377 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
379 term
.line
[i
] = term
.line
[i
+n
];
380 term
.line
[i
+n
] = temp
;
386 int y
= term
.c
.y
+ 1;
388 tscrollup(1), y
= term
.bot
;
395 char *p
= escseq
.buf
;
399 escseq
.priv
= 1, p
++;
401 while(p
< escseq
.buf
+escseq
.len
) {
403 escseq
.arg
[escseq
.narg
] *= 10;
404 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
406 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
417 tmoveto(int x
, int y
) {
418 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
419 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
424 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
425 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
426 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
430 tclearregion(int x1
, int y1
, int x2
, int y2
) {
434 temp
= x1
, x1
= x2
, x2
= temp
;
436 temp
= y1
, y1
= y2
, y2
= temp
;
438 LIMIT(x1
, 0, term
.col
-1);
439 LIMIT(x2
, 0, term
.col
-1);
440 LIMIT(y1
, 0, term
.row
-1);
441 LIMIT(y2
, 0, term
.row
-1);
443 for(y
= y1
; y
<= y2
; y
++)
444 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
449 int src
= term
.c
.x
+ n
;
451 int size
= term
.col
- src
;
453 if(src
>= term
.col
) {
454 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
457 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
458 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
462 tinsertblank(int n
) {
465 int size
= term
.col
- dst
;
467 if(dst
>= term
.col
) {
468 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
471 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
472 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
476 tinsertblankline(int n
) {
481 if(term
.c
.y
> term
.bot
)
483 else if(term
.c
.y
< term
.top
)
485 if(term
.c
.y
+ n
>= bot
) {
486 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
489 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
490 /* swap deleted line <-> blanked line */
491 blank
= term
.line
[i
];
492 term
.line
[i
] = term
.line
[i
-n
];
493 term
.line
[i
-n
] = blank
;
495 memset(blank
, 0, term
.col
* sizeof(Glyph
));
505 if(term
.c
.y
> term
.bot
)
507 else if(term
.c
.y
< term
.top
)
509 if(term
.c
.y
+ n
>= bot
) {
510 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
513 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
514 /* swap deleted line <-> blanked line */
515 blank
= term
.line
[i
];
516 term
.line
[i
] = term
.line
[i
+n
];
517 term
.line
[i
+n
] = blank
;
519 memset(blank
, 0, term
.col
* sizeof(Glyph
));
524 tsetattr(int *attr
, int l
) {
527 for(i
= 0; i
< l
; i
++) {
530 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
531 term
.c
.attr
.fg
= DefaultFG
;
532 term
.c
.attr
.bg
= DefaultBG
;
535 term
.c
.attr
.mode
|= ATTR_BOLD
;
538 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
541 term
.c
.attr
.mode
|= ATTR_REVERSE
;
544 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
547 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
550 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
553 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
555 if (BETWEEN(attr
[i
], 0, 255))
556 term
.c
.attr
.fg
= attr
[i
];
558 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
561 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
564 term
.c
.attr
.fg
= DefaultFG
;
567 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
569 if (BETWEEN(attr
[i
], 0, 255))
570 term
.c
.attr
.bg
= attr
[i
];
572 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
575 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
578 term
.c
.attr
.bg
= DefaultBG
;
581 if(BETWEEN(attr
[i
], 30, 37))
582 term
.c
.attr
.fg
= attr
[i
] - 30;
583 else if(BETWEEN(attr
[i
], 40, 47))
584 term
.c
.attr
.bg
= attr
[i
] - 40;
585 else if(BETWEEN(attr
[i
], 90, 97))
586 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
587 else if(BETWEEN(attr
[i
], 100, 107))
588 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
590 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
597 tsetscroll(int t
, int b
) {
600 LIMIT(t
, 0, term
.row
-1);
601 LIMIT(b
, 0, term
.row
-1);
613 switch(escseq
.mode
) {
616 printf("erresc: unknown csi ");
620 case '@': /* ICH -- Insert <n> blank char */
621 DEFAULT(escseq
.arg
[0], 1);
622 tinsertblank(escseq
.arg
[0]);
624 case 'A': /* CUU -- Cursor <n> Up */
626 DEFAULT(escseq
.arg
[0], 1);
627 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
629 case 'B': /* CUD -- Cursor <n> Down */
630 DEFAULT(escseq
.arg
[0], 1);
631 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
633 case 'C': /* CUF -- Cursor <n> Forward */
635 DEFAULT(escseq
.arg
[0], 1);
636 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
638 case 'D': /* CUB -- Cursor <n> Backward */
639 DEFAULT(escseq
.arg
[0], 1);
640 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
642 case 'E': /* CNL -- Cursor <n> Down and first col */
643 DEFAULT(escseq
.arg
[0], 1);
644 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
646 case 'F': /* CPL -- Cursor <n> Up and first col */
647 DEFAULT(escseq
.arg
[0], 1);
648 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
650 case 'G': /* CHA -- Move to <col> */
651 case '`': /* XXX: HPA -- same? */
652 DEFAULT(escseq
.arg
[0], 1);
653 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
655 case 'H': /* CUP -- Move to <row> <col> */
656 case 'f': /* XXX: HVP -- same? */
657 DEFAULT(escseq
.arg
[0], 1);
658 DEFAULT(escseq
.arg
[1], 1);
659 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
661 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
662 case 'J': /* ED -- Clear screen */
663 switch(escseq
.arg
[0]) {
665 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
668 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
671 tclearregion(0, 0, term
.col
-1, term
.row
-1);
673 case 3: /* XXX: erase saved lines (xterm) */
678 case 'K': /* EL -- Clear line */
679 switch(escseq
.arg
[0]) {
681 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
684 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
687 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
691 case 'S': /* SU -- Scroll <n> line up */
692 DEFAULT(escseq
.arg
[0], 1);
693 tscrollup(escseq
.arg
[0]);
695 case 'T': /* SD -- Scroll <n> line down */
696 DEFAULT(escseq
.arg
[0], 1);
697 tscrolldown(escseq
.arg
[0]);
699 case 'L': /* IL -- Insert <n> blank lines */
700 DEFAULT(escseq
.arg
[0], 1);
701 tinsertblankline(escseq
.arg
[0]);
703 case 'l': /* RM -- Reset Mode */
705 switch(escseq
.arg
[0]) {
707 term
.mode
&= ~MODE_APPKEYPAD
;
710 term
.mode
&= ~MODE_WRAP
;
712 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
715 term
.c
.hide
= CURSOR_HIDE
;
717 case 1048: /* XXX: no alt. screen to erase/save */
719 tcursor(CURSOR_LOAD
);
720 tclearregion(0, 0, term
.col
-1, term
.row
-1);
726 switch(escseq
.arg
[0]) {
728 term
.mode
&= ~MODE_INSERT
;
735 case 'M': /* DL -- Delete <n> lines */
736 DEFAULT(escseq
.arg
[0], 1);
737 tdeleteline(escseq
.arg
[0]);
739 case 'X': /* ECH -- Erase <n> char */
740 DEFAULT(escseq
.arg
[0], 1);
741 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
743 case 'P': /* DCH -- Delete <n> char */
744 DEFAULT(escseq
.arg
[0], 1);
745 tdeletechar(escseq
.arg
[0]);
747 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
748 case 'd': /* VPA -- Move to <row> */
749 DEFAULT(escseq
.arg
[0], 1);
750 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
752 case 'h': /* SM -- Set terminal mode */
754 switch(escseq
.arg
[0]) {
756 term
.mode
|= MODE_APPKEYPAD
;
759 term
.mode
|= MODE_WRAP
;
761 case 12: /* att610 -- Start blinking cursor (IGNORED) */
764 term
.c
.hide
= CURSOR_DRAW
;
767 case 1049: /* XXX: no alt. screen to erase/save */
768 tcursor(CURSOR_SAVE
);
769 tclearregion(0, 0, term
.col
-1, term
.row
-1);
771 default: goto unknown
;
774 switch(escseq
.arg
[0]) {
776 term
.mode
|= MODE_INSERT
;
778 default: goto unknown
;
782 case 'm': /* SGR -- Terminal attribute (color) */
783 tsetattr(escseq
.arg
, escseq
.narg
);
785 case 'r': /* DECSTBM -- Set Scrolling Region */
789 DEFAULT(escseq
.arg
[0], 1);
790 DEFAULT(escseq
.arg
[1], term
.row
);
791 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
794 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
795 tcursor(CURSOR_SAVE
);
797 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
798 tcursor(CURSOR_LOAD
);
806 printf("ESC [ %s", escseq
.priv
? "? " : "");
808 for(i
= 0; i
< escseq
.narg
; i
++)
809 printf("%d ", escseq
.arg
[i
]);
811 putchar(escseq
.mode
);
817 memset(&escseq
, 0, sizeof(escseq
));
822 int space
= TAB
- term
.c
.x
% TAB
;
823 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
828 if(term
.esc
& ESC_START
) {
829 if(term
.esc
& ESC_CSI
) {
830 escseq
.buf
[escseq
.len
++] = c
;
831 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
833 csiparse(), csihandle();
835 } else if(term
.esc
& ESC_OSC
) {
838 term
.esc
= ESC_START
| ESC_TITLE
;
840 } else if(term
.esc
& ESC_TITLE
) {
841 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
843 term
.title
[term
.titlelen
] = '\0';
844 XStoreName(xw
.dis
, xw
.win
, term
.title
);
846 term
.title
[term
.titlelen
++] = c
;
848 } else if(term
.esc
& ESC_ALTCHARSET
) {
850 case '0': /* Line drawing crap */
851 term
.c
.attr
.mode
|= ATTR_GFX
;
853 case 'B': /* Back to regular text */
854 term
.c
.attr
.mode
&= ~ATTR_GFX
;
857 printf("esc unhandled charset: ESC ( %c\n", c
);
869 term
.esc
|= ESC_ALTCHARSET
;
872 tmoveto(term
.c
.x
, term
.c
.y
-1);
876 tmoveto(term
.c
.x
, term
.c
.y
+1);
880 tmoveto(term
.c
.x
+1, term
.c
.y
);
883 case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
884 tmoveto(term
.c
.x
-1, term
.c
.y
);
887 case 'E': /* NEL -- Next line */
891 case 'M': /* RI -- Reverse index */
892 if(term
.c
.y
== term
.top
)
895 tmoveto(term
.c
.x
, term
.c
.y
-1);
898 case 'c': /* RIS -- Reset to inital state */
902 case '=': /* DECPAM */
903 term
.mode
|= MODE_APPKEYPAD
;
906 case '>': /* DECPNM */
907 term
.mode
&= ~MODE_APPKEYPAD
;
911 tcursor(CURSOR_SAVE
);
915 tcursor(CURSOR_LOAD
);
919 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
929 tmoveto(term
.c
.x
-1, term
.c
.y
);
932 tmoveto(0, term
.c
.y
);
942 term
.esc
= ESC_START
;
946 if(term
.c
.x
+1 < term
.col
) {
947 tmoveto(term
.c
.x
+1, term
.c
.y
);
948 } else if(IS_SET(MODE_WRAP
))
956 tputs(char *s
, int len
) {
957 for(; len
> 0; len
--)
962 tresize(int col
, int row
) {
964 int minrow
= MIN(row
, term
.row
);
965 int mincol
= MIN(col
, term
.col
);
967 if(col
< 1 || row
< 1)
970 /* free uneeded rows */
971 for(i
= row
; i
< term
.row
; i
++)
974 /* resize to new height */
975 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
977 /* resize each row to new width, zero-pad if needed */
978 for(i
= 0; i
< minrow
; i
++) {
979 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
980 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
983 /* allocate any new rows */
984 for(/* i == minrow */; i
< row
; i
++)
985 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
987 LIMIT(term
.c
.x
, 0, col
-1);
988 LIMIT(term
.c
.y
, 0, row
-1);
989 LIMIT(term
.top
, 0, row
-1);
990 LIMIT(term
.bot
, 0, row
-1);
993 term
.col
= col
, term
.row
= row
;
1000 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1001 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1003 for(i
= 0; i
< 16; i
++) {
1004 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1006 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1008 dc
.col
[i
] = color
.pixel
;
1011 /* same colors as xterm */
1012 for(r
= 0; r
< 6; r
++)
1013 for(g
= 0; g
< 6; g
++)
1014 for(b
= 0; b
< 6; b
++) {
1015 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1016 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1017 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1018 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1020 fprintf(stderr
, "Could not allocate color %d\n", i
);
1022 dc
.col
[i
] = color
.pixel
;
1026 for(r
= 0; r
< 24; r
++, i
++) {
1027 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1028 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1030 fprintf(stderr
, "Could not allocate color %d\n", i
);
1032 dc
.col
[i
] = color
.pixel
;
1037 xclear(int x1
, int y1
, int x2
, int y2
) {
1038 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1039 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1040 x1
* xw
.cw
, y1
* xw
.ch
,
1041 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1047 XClassHint
class = {TNAME
, TNAME
};
1048 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1050 .flags
= PSize
| PResizeInc
| PBaseSize
,
1053 .height_inc
= xw
.ch
,
1055 .base_height
= 2*BORDER
,
1056 .base_width
= 2*BORDER
,
1058 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1063 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1064 die("Can't open display\n");
1065 xw
.scr
= XDefaultScreen(xw
.dis
);
1068 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1069 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1071 /* XXX: Assuming same size for bold font */
1072 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1073 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1078 term
.c
.attr
.fg
= DefaultFG
;
1079 term
.c
.attr
.bg
= DefaultBG
;
1080 term
.c
.attr
.mode
= ATTR_NULL
;
1082 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1083 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1084 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1088 xw
.bufw
= xw
.w
- 2*BORDER
;
1089 xw
.bufh
= xw
.h
- 2*BORDER
;
1090 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1092 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1093 XMapWindow(xw
.dis
, xw
.win
);
1095 XStoreName(xw
.dis
, xw
.win
, "st");
1100 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1101 unsigned long xfg
, xbg
;
1102 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1105 if(base
.mode
& ATTR_REVERSE
)
1106 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1108 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1110 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1111 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1113 if(base
.mode
& ATTR_GFX
)
1114 for(i
= 0; i
< len
; i
++)
1115 s
[i
] = gfx
[(int)s
[i
]];
1117 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1118 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1120 if(base
.mode
& ATTR_UNDERLINE
)
1121 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1126 static int oldx
= 0;
1127 static int oldy
= 0;
1128 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1130 LIMIT(oldx
, 0, term
.col
-1);
1131 LIMIT(oldy
, 0, term
.row
-1);
1133 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1134 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1136 /* remove the old cursor */
1137 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1138 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1140 xclear(oldx
, oldy
, oldx
, oldy
);
1142 /* draw the new one */
1143 if(mode
== CURSOR_DRAW
) {
1144 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1145 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1150 /* basic drawing routines */
1152 xdrawc(int x
, int y
, Glyph g
) {
1153 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1154 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1155 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1156 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1157 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1164 xclear(0, 0, term
.col
-1, term
.row
-1);
1165 for(y
= 0; y
< term
.row
; y
++)
1166 for(x
= 0; x
< term
.col
; x
++)
1167 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1168 xdrawc(x
, y
, term
.line
[y
][x
]);
1170 xcursor(term
.c
.hide
);
1171 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1176 /* optimized drawing routine */
1178 draw(int redraw_all
) {
1181 char buf
[DRAW_BUF_SIZ
];
1183 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1184 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
);
1185 for(y
= 0; y
< term
.row
; y
++) {
1186 base
= term
.line
[y
][0];
1188 for(x
= 0; x
< term
.col
; x
++) {
1189 new = term
.line
[y
][x
];
1190 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1191 i
>= DRAW_BUF_SIZ
)) {
1192 xdraws(buf
, base
, ox
, y
, i
);
1195 if(new.state
& GLYPH_SET
) {
1204 xdraws(buf
, base
, ox
, y
, i
);
1206 xcursor(term
.c
.hide
);
1207 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1214 expose(XEvent
*ev
) {
1215 draw(SCREEN_REDRAW
);
1221 for(i
= 0; i
< LEN(key
); i
++)
1223 return (char*)key
[i
].s
;
1228 kpress(XEvent
*ev
) {
1229 XKeyEvent
*e
= &ev
->xkey
;
1237 meta
= e
->state
& Mod1Mask
;
1238 shift
= e
->state
& ShiftMask
;
1239 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1241 if((customkey
= kmap(ksym
)))
1242 ttywrite(customkey
, strlen(customkey
));
1244 buf
[sizeof(buf
)-1] = '\0';
1245 if(meta
&& len
== 1)
1246 ttywrite("\033", 1);
1254 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1259 draw(1), puts("draw!")/* XXX: paste X clipboard */;
1262 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1271 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1274 xw
.w
= e
->xconfigure
.width
;
1275 xw
.h
= e
->xconfigure
.height
;
1276 xw
.bufw
= xw
.w
- 2*BORDER
;
1277 xw
.bufh
= xw
.h
- 2*BORDER
;
1278 col
= xw
.bufw
/ xw
.cw
;
1279 row
= xw
.bufh
/ xw
.ch
;
1281 ttyresize(col
, row
);
1282 XFreePixmap(xw
.dis
, xw
.buf
);
1283 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1284 draw(SCREEN_REDRAW
);
1291 int xfd
= XConnectionNumber(xw
.dis
);
1294 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1295 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* XXX: fix resize bug in wmii (?) */
1299 FD_SET(cmdfd
, &rfd
);
1301 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1304 die("select failed: %s\n", SERRNO
);
1306 if(FD_ISSET(cmdfd
, &rfd
)) {
1308 draw(SCREEN_UPDATE
);
1310 while(XPending(xw
.dis
)) {
1311 XNextEvent(xw
.dis
, &ev
);
1312 if(handler
[ev
.type
])
1313 (handler
[ev
.type
])(&ev
);
1319 main(int argc
, char *argv
[]) {
1320 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1321 die("st-" VERSION
", (c) 2010 st engineers\n");
1323 die("usage: st [-v]\n");
1324 setlocale(LC_CTYPE
, "");