Xinqi Bao's Git
c2f294a94f37fba153da80f3c68184a996b3bfa0
1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
20 #include <X11/keysym.h>
21 #include <X11/Xutil.h>
25 #elif defined(__OpenBSD__) || defined(__NetBSD__)
27 #elif defined(__FreeBSD__) || defined(__DragonFly__)
32 #define ESC_TITLE_SIZ 256
33 #define ESC_BUF_SIZ 256
34 #define ESC_ARG_SIZ 16
35 #define DRAW_BUF_SIZ 1024
37 #define SERRNO strerror(errno)
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #define MAX(a, b) ((a) < (b) ? (b) : (a))
40 #define LEN(a) (sizeof(a) / sizeof(a[0]))
41 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
42 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
43 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
44 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
45 #define IS_SET(flag) (term.mode & (flag))
47 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
48 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
49 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
50 CURSOR_SAVE
, CURSOR_LOAD
};
51 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
52 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
53 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8 };
54 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
55 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
58 char c
; /* character code */
59 char mode
; /* attribute flags */
60 int fg
; /* foreground */
61 int bg
; /* background */
62 char state
; /* state flags */
68 Glyph attr
; /* current char attributes */
74 /* CSI Escape sequence structs */
75 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
77 char buf
[ESC_BUF_SIZ
]; /* raw string */
78 int len
; /* raw string length */
81 int narg
; /* nb of args */
85 /* Internal representation of the screen */
89 Line
* line
; /* screen */
90 Line
* alt
; /* alternate screen */
91 TCursor c
; /* cursor */
92 int top
; /* top scroll limit */
93 int bot
; /* bottom scroll limit */
94 int mode
; /* terminal mode flags */
95 int esc
; /* escape state flags */
96 char title
[ESC_TITLE_SIZ
];
100 /* Purely graphic info */
106 int w
; /* window width */
107 int h
; /* window height */
108 int bufw
; /* pixmap width */
109 int bufh
; /* pixmap height */
110 int ch
; /* char height */
111 int cw
; /* char width */
120 /* Drawing Context */
122 unsigned long col
[256];
138 static void die(const char *errstr
, ...);
139 static void draw(int);
140 static void execsh(void);
141 static void sigchld(int);
142 static void run(void);
144 static void csidump(void);
145 static void csihandle(void);
146 static void csiparse(void);
147 static void csireset(void);
149 static void tclearregion(int, int, int, int);
150 static void tcursor(int);
151 static void tdeletechar(int);
152 static void tdeleteline(int);
153 static void tinsertblank(int);
154 static void tinsertblankline(int);
155 static void tmoveto(int, int);
156 static void tnew(int, int);
157 static void tnewline(void);
158 static void tputtab(void);
159 static void tputc(char);
160 static void tputs(char*, int);
161 static void treset(void);
162 static void tresize(int, int);
163 static void tscrollup(int);
164 static void tscrolldown(int);
165 static void tsetattr(int*, int);
166 static void tsetchar(char);
167 static void tsetscroll(int, int);
168 static void tswapscreen(void);
170 static void ttynew(void);
171 static void ttyread(void);
172 static void ttyresize(int, int);
173 static void ttywrite(const char *, size_t);
175 static void xdraws(char *, Glyph
, int, int, int);
176 static void xhints(void);
177 static void xclear(int, int, int, int);
178 static void xdrawcursor(void);
179 static void xinit(void);
180 static void xloadcols(void);
181 static void xseturgency(int);
183 static void expose(XEvent
*);
184 static char* kmap(KeySym
);
185 static void kpress(XEvent
*);
186 static void resize(XEvent
*);
187 static void focus(XEvent
*);
188 static void brelease(XEvent
*);
189 static void bpress(XEvent
*);
190 static void bmotion(XEvent
*);
193 static void (*handler
[LASTEvent
])(XEvent
*) = {
196 [ConfigureNotify
] = resize
,
199 [MotionNotify
] = bmotion
,
200 [ButtonPress
] = bpress
,
201 [ButtonRelease
] = brelease
,
208 static CSIEscape escseq
;
211 static Selection sel
;
220 static inline int selected(int x
, int y
) {
221 if ((sel
.ey
==y
&& sel
.by
==y
)) {
222 int bx
= MIN(sel
.bx
, sel
.ex
);
223 int ex
= MAX(sel
.bx
, sel
.ex
);
224 return (x
>=bx
&& x
<=ex
);
226 return (((y
>sel
.b
[1] && y
<sel
.e
[1]) || (y
==sel
.e
[1] && x
<=sel
.e
[0])) || \
227 (y
==sel
.b
[1] && x
>=sel
.b
[0] && (x
<=sel
.e
[0] || sel
.b
[1]!=sel
.e
[1])));
230 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
231 if(b
) *b
= e
->xbutton
.state
,
232 *b
=*b
==4096?5:*b
==2048?4:*b
==1024?3:*b
==512?2:*b
==256?1:-1;
233 *x
= e
->xbutton
.x
/xw
.cw
;
234 *y
= e
->xbutton
.y
/xw
.ch
;
235 sel
.b
[0] = sel
.by
<sel
.ey
?sel
.bx
:sel
.ex
;
236 sel
.b
[1] = MIN(sel
.by
, sel
.ey
);
237 sel
.e
[0] = sel
.by
<sel
.ey
?sel
.ex
:sel
.bx
;
238 sel
.e
[1] = MAX(sel
.by
, sel
.ey
);
241 static void bpress(XEvent
*e
) {
243 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
244 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
247 static char *getseltext() {
252 sz
= ((term
.col
+1) * (sel
.e
[1]-sel
.b
[1]+1));
253 ptr
= str
= malloc (sz
);
254 for(y
= 0; y
< term
.row
; y
++) {
255 for(x
= 0; x
< term
.col
; x
++) {
256 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
=selected(x
, y
)))
257 *ptr
= term
.line
[y
][x
].c
, ptr
++;
266 /* TODO: use X11 clipboard */
267 static void selcopy(char *str
) {
272 static void selpaste() {
274 ttywrite(sel
.clip
, strlen(sel
.clip
));
277 /* TODO: doubleclick to select word */
278 static void brelease(XEvent
*e
) {
281 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
282 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
288 selcopy(getseltext());
293 static void bmotion(XEvent
*e
) {
295 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
306 for(row
= 0; row
< term
.row
; row
++) {
307 for(col
= 0; col
< term
.col
; col
++) {
308 if(col
== term
.c
.x
&& row
== term
.c
.y
)
311 c
= term
.line
[row
][col
];
312 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
321 die(const char *errstr
, ...) {
324 va_start(ap
, errstr
);
325 vfprintf(stderr
, errstr
, ap
);
332 char *args
[3] = {getenv("SHELL"), "-i", NULL
};
333 DEFAULT(args
[0], SHELL
); /* if getenv() failed */
334 putenv("TERM=" TNAME
);
335 execvp(args
[0], args
);
341 if(waitpid(pid
, &stat
, 0) < 0)
342 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
344 exit(WEXITSTATUS(stat
));
353 /* seems to work fine on linux, openbsd and freebsd */
354 struct winsize w
= {term
.row
, term
.col
, 0, 0};
355 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
356 die("openpty failed: %s\n", SERRNO
);
358 switch(pid
= fork()) {
360 die("fork failed\n");
363 setsid(); /* create a new process group */
364 dup2(s
, STDIN_FILENO
);
365 dup2(s
, STDOUT_FILENO
);
366 dup2(s
, STDERR_FILENO
);
367 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
368 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
376 signal(SIGCHLD
, sigchld
);
383 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
385 fprintf(stderr
, "\n");
390 char buf
[BUFSIZ
] = {0};
393 if((ret
= read(cmdfd
, buf
, BUFSIZ
)) < 0)
394 die("Couldn't read from shell: %s\n", SERRNO
);
400 ttywrite(const char *s
, size_t n
) {
401 if(write(cmdfd
, s
, n
) == -1)
402 die("write error on tty: %s\n", SERRNO
);
406 ttyresize(int x
, int y
) {
411 w
.ws_xpixel
= w
.ws_ypixel
= 0;
412 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
413 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
420 if(mode
== CURSOR_SAVE
)
422 else if(mode
== CURSOR_LOAD
)
423 term
.c
= c
, tmoveto(c
.x
, c
.y
);
432 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
434 term
.top
= 0, term
.bot
= term
.row
- 1;
435 term
.mode
= MODE_WRAP
;
436 tclearregion(0, 0, term
.col
-1, term
.row
-1);
440 tnew(int col
, int row
) {
441 /* set screen size */
442 term
.row
= row
, term
.col
= col
;
443 term
.line
= malloc(term
.row
* sizeof(Line
));
444 term
.alt
= malloc(term
.row
* sizeof(Line
));
445 for(row
= 0 ; row
< term
.row
; row
++) {
446 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
447 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
455 Line
* tmp
= term
.line
;
456 term
.line
= term
.alt
;
458 term
.mode
^= MODE_ALTSCREEN
;
462 tscrolldown (int n
) {
466 LIMIT(n
, 0, term
.bot
-term
.top
+1);
468 for(i
= 0; i
< n
; i
++)
469 memset(term
.line
[term
.bot
-i
], 0, term
.col
*sizeof(Glyph
));
471 for(i
= term
.bot
; i
>= term
.top
+n
; i
--) {
473 term
.line
[i
] = term
.line
[i
-n
];
474 term
.line
[i
-n
] = temp
;
482 LIMIT(n
, 0, term
.bot
-term
.top
+1);
484 for(i
= 0; i
< n
; i
++)
485 memset(term
.line
[term
.top
+i
], 0, term
.col
*sizeof(Glyph
));
487 for(i
= term
.top
; i
<= term
.bot
-n
; i
++) {
489 term
.line
[i
] = term
.line
[i
+n
];
490 term
.line
[i
+n
] = temp
;
496 int y
= term
.c
.y
+ 1;
498 tscrollup(1), y
= term
.bot
;
505 char *p
= escseq
.buf
;
509 escseq
.priv
= 1, p
++;
511 while(p
< escseq
.buf
+escseq
.len
) {
513 escseq
.arg
[escseq
.narg
] *= 10;
514 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
516 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
527 tmoveto(int x
, int y
) {
528 LIMIT(x
, 0, term
.col
-1);
529 LIMIT(y
, 0, term
.row
-1);
530 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
537 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
538 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
539 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
543 tclearregion(int x1
, int y1
, int x2
, int y2
) {
547 temp
= x1
, x1
= x2
, x2
= temp
;
549 temp
= y1
, y1
= y2
, y2
= temp
;
551 LIMIT(x1
, 0, term
.col
-1);
552 LIMIT(x2
, 0, term
.col
-1);
553 LIMIT(y1
, 0, term
.row
-1);
554 LIMIT(y2
, 0, term
.row
-1);
556 for(y
= y1
; y
<= y2
; y
++)
557 memset(&term
.line
[y
][x1
], 0, sizeof(Glyph
)*(x2
-x1
+1));
562 int src
= term
.c
.x
+ n
;
564 int size
= term
.col
- src
;
566 if(src
>= term
.col
) {
567 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
570 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
571 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
575 tinsertblank(int n
) {
578 int size
= term
.col
- dst
;
580 if(dst
>= term
.col
) {
581 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
584 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
585 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
589 tinsertblankline(int n
) {
594 if(term
.c
.y
> term
.bot
)
596 else if(term
.c
.y
< term
.top
)
598 if(term
.c
.y
+ n
>= bot
) {
599 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
602 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
603 /* swap deleted line <-> blanked line */
604 blank
= term
.line
[i
];
605 term
.line
[i
] = term
.line
[i
-n
];
606 term
.line
[i
-n
] = blank
;
608 memset(blank
, 0, term
.col
* sizeof(Glyph
));
618 if(term
.c
.y
> term
.bot
)
620 else if(term
.c
.y
< term
.top
)
622 if(term
.c
.y
+ n
>= bot
) {
623 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
626 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
627 /* swap deleted line <-> blanked line */
628 blank
= term
.line
[i
];
629 term
.line
[i
] = term
.line
[i
+n
];
630 term
.line
[i
+n
] = blank
;
632 memset(blank
, 0, term
.col
* sizeof(Glyph
));
637 tsetattr(int *attr
, int l
) {
640 for(i
= 0; i
< l
; i
++) {
643 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
644 term
.c
.attr
.fg
= DefaultFG
;
645 term
.c
.attr
.bg
= DefaultBG
;
648 term
.c
.attr
.mode
|= ATTR_BOLD
;
651 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
654 term
.c
.attr
.mode
|= ATTR_REVERSE
;
657 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
660 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
663 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
666 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
668 if (BETWEEN(attr
[i
], 0, 255))
669 term
.c
.attr
.fg
= attr
[i
];
671 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
674 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
677 term
.c
.attr
.fg
= DefaultFG
;
680 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
682 if (BETWEEN(attr
[i
], 0, 255))
683 term
.c
.attr
.bg
= attr
[i
];
685 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
688 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
691 term
.c
.attr
.bg
= DefaultBG
;
694 if(BETWEEN(attr
[i
], 30, 37))
695 term
.c
.attr
.fg
= attr
[i
] - 30;
696 else if(BETWEEN(attr
[i
], 40, 47))
697 term
.c
.attr
.bg
= attr
[i
] - 40;
698 else if(BETWEEN(attr
[i
], 90, 97))
699 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
700 else if(BETWEEN(attr
[i
], 100, 107))
701 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
703 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
710 tsetscroll(int t
, int b
) {
713 LIMIT(t
, 0, term
.row
-1);
714 LIMIT(b
, 0, term
.row
-1);
726 switch(escseq
.mode
) {
729 printf("erresc: unknown csi ");
733 case '@': /* ICH -- Insert <n> blank char */
734 DEFAULT(escseq
.arg
[0], 1);
735 tinsertblank(escseq
.arg
[0]);
737 case 'A': /* CUU -- Cursor <n> Up */
739 DEFAULT(escseq
.arg
[0], 1);
740 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
742 case 'B': /* CUD -- Cursor <n> Down */
743 DEFAULT(escseq
.arg
[0], 1);
744 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
746 case 'C': /* CUF -- Cursor <n> Forward */
748 DEFAULT(escseq
.arg
[0], 1);
749 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
751 case 'D': /* CUB -- Cursor <n> Backward */
752 DEFAULT(escseq
.arg
[0], 1);
753 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
755 case 'E': /* CNL -- Cursor <n> Down and first col */
756 DEFAULT(escseq
.arg
[0], 1);
757 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
759 case 'F': /* CPL -- Cursor <n> Up and first col */
760 DEFAULT(escseq
.arg
[0], 1);
761 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
763 case 'G': /* CHA -- Move to <col> */
764 case '`': /* XXX: HPA -- same? */
765 DEFAULT(escseq
.arg
[0], 1);
766 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
768 case 'H': /* CUP -- Move to <row> <col> */
769 case 'f': /* XXX: HVP -- same? */
770 DEFAULT(escseq
.arg
[0], 1);
771 DEFAULT(escseq
.arg
[1], 1);
772 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
774 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
775 case 'J': /* ED -- Clear screen */
776 switch(escseq
.arg
[0]) {
778 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
781 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
784 tclearregion(0, 0, term
.col
-1, term
.row
-1);
790 case 'K': /* EL -- Clear line */
791 switch(escseq
.arg
[0]) {
793 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
796 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
799 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
803 case 'S': /* SU -- Scroll <n> line up */
804 DEFAULT(escseq
.arg
[0], 1);
805 tscrollup(escseq
.arg
[0]);
807 case 'T': /* SD -- Scroll <n> line down */
808 DEFAULT(escseq
.arg
[0], 1);
809 tscrolldown(escseq
.arg
[0]);
811 case 'L': /* IL -- Insert <n> blank lines */
812 DEFAULT(escseq
.arg
[0], 1);
813 tinsertblankline(escseq
.arg
[0]);
815 case 'l': /* RM -- Reset Mode */
817 switch(escseq
.arg
[0]) {
819 term
.mode
&= ~MODE_APPKEYPAD
;
822 term
.mode
&= ~MODE_WRAP
;
824 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
827 term
.c
.state
|= CURSOR_HIDE
;
829 case 1049: /* = 1047 and 1048 */
831 if(IS_SET(MODE_ALTSCREEN
)) {
832 tclearregion(0, 0, term
.col
-1, term
.row
-1);
835 if(escseq
.arg
[0] == 1047)
838 tcursor(CURSOR_LOAD
);
844 switch(escseq
.arg
[0]) {
846 term
.mode
&= ~MODE_INSERT
;
853 case 'M': /* DL -- Delete <n> lines */
854 DEFAULT(escseq
.arg
[0], 1);
855 tdeleteline(escseq
.arg
[0]);
857 case 'X': /* ECH -- Erase <n> char */
858 DEFAULT(escseq
.arg
[0], 1);
859 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
861 case 'P': /* DCH -- Delete <n> char */
862 DEFAULT(escseq
.arg
[0], 1);
863 tdeletechar(escseq
.arg
[0]);
865 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
866 case 'd': /* VPA -- Move to <row> */
867 DEFAULT(escseq
.arg
[0], 1);
868 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
870 case 'h': /* SM -- Set terminal mode */
872 switch(escseq
.arg
[0]) {
874 term
.mode
|= MODE_APPKEYPAD
;
877 term
.mode
|= MODE_WRAP
;
879 case 12: /* att610 -- Start blinking cursor (IGNORED) */
882 term
.c
.state
&= ~CURSOR_HIDE
;
884 case 1049: /* = 1047 and 1048 */
886 if(IS_SET(MODE_ALTSCREEN
))
887 tclearregion(0, 0, term
.col
-1, term
.row
-1);
890 if(escseq
.arg
[0] == 1047)
893 tcursor(CURSOR_SAVE
);
895 default: goto unknown
;
898 switch(escseq
.arg
[0]) {
900 term
.mode
|= MODE_INSERT
;
902 default: goto unknown
;
906 case 'm': /* SGR -- Terminal attribute (color) */
907 tsetattr(escseq
.arg
, escseq
.narg
);
909 case 'r': /* DECSTBM -- Set Scrolling Region */
913 DEFAULT(escseq
.arg
[0], 1);
914 DEFAULT(escseq
.arg
[1], term
.row
);
915 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
919 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
920 tcursor(CURSOR_SAVE
);
922 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
923 tcursor(CURSOR_LOAD
);
931 printf("ESC [ %s", escseq
.priv
? "? " : "");
933 for(i
= 0; i
< escseq
.narg
; i
++)
934 printf("%d ", escseq
.arg
[i
]);
936 putchar(escseq
.mode
);
942 memset(&escseq
, 0, sizeof(escseq
));
947 int space
= TAB
- term
.c
.x
% TAB
;
948 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
953 if(term
.esc
& ESC_START
) {
954 if(term
.esc
& ESC_CSI
) {
955 escseq
.buf
[escseq
.len
++] = c
;
956 if(BETWEEN(c
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
958 csiparse(), csihandle();
960 } else if(term
.esc
& ESC_OSC
) {
963 term
.esc
= ESC_START
| ESC_TITLE
;
965 } else if(term
.esc
& ESC_TITLE
) {
966 if(c
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
968 term
.title
[term
.titlelen
] = '\0';
969 XStoreName(xw
.dis
, xw
.win
, term
.title
);
971 term
.title
[term
.titlelen
++] = c
;
973 } else if(term
.esc
& ESC_ALTCHARSET
) {
975 case '0': /* Line drawing crap */
976 term
.c
.attr
.mode
|= ATTR_GFX
;
978 case 'B': /* Back to regular text */
979 term
.c
.attr
.mode
&= ~ATTR_GFX
;
982 printf("esc unhandled charset: ESC ( %c\n", c
);
994 term
.esc
|= ESC_ALTCHARSET
;
996 case 'D': /* IND -- Linefeed */
997 if(term
.c
.y
== term
.bot
)
1000 tmoveto(term
.c
.x
, term
.c
.y
+1);
1003 case 'E': /* NEL -- Next line */
1007 case 'M': /* RI -- Reverse index */
1008 if(term
.c
.y
== term
.top
)
1011 tmoveto(term
.c
.x
, term
.c
.y
-1);
1014 case 'c': /* RIS -- Reset to inital state */
1018 case '=': /* DECPAM -- Application keypad */
1019 term
.mode
|= MODE_APPKEYPAD
;
1022 case '>': /* DECPNM -- Normal keypad */
1023 term
.mode
&= ~MODE_APPKEYPAD
;
1026 case '7': /* DECSC -- Save Cursor */
1027 tcursor(CURSOR_SAVE
);
1030 case '8': /* DECRC -- Restore Cursor */
1031 tcursor(CURSOR_LOAD
);
1035 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n", c
, isprint(c
)?c
:'.');
1045 tmoveto(term
.c
.x
-1, term
.c
.y
);
1048 tmoveto(0, term
.c
.y
);
1059 term
.esc
= ESC_START
;
1062 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1065 if(term
.c
.x
+1 < term
.col
)
1066 tmoveto(term
.c
.x
+1, term
.c
.y
);
1068 term
.c
.state
|= CURSOR_WRAPNEXT
;
1075 tputs(char *s
, int len
) {
1076 for(; len
> 0; len
--)
1081 tresize(int col
, int row
) {
1083 int minrow
= MIN(row
, term
.row
);
1084 int mincol
= MIN(col
, term
.col
);
1086 if(col
< 1 || row
< 1)
1089 /* free uneeded rows */
1090 for(i
= row
; i
< term
.row
; i
++) {
1095 /* resize to new height */
1096 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1097 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1099 /* resize each row to new width, zero-pad if needed */
1100 for(i
= 0; i
< minrow
; i
++) {
1101 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1102 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1103 memset(term
.line
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1104 memset(term
.alt
[i
] + mincol
, 0, (col
- mincol
) * sizeof(Glyph
));
1107 /* allocate any new rows */
1108 for(/* i == minrow */; i
< row
; i
++) {
1109 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1110 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1113 /* update terminal size */
1114 term
.col
= col
, term
.row
= row
;
1115 /* make use of the LIMIT in tmoveto */
1116 tmoveto(term
.c
.x
, term
.c
.y
);
1117 /* reset scrolling region */
1118 tsetscroll(0, row
-1);
1125 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
1126 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1128 for(i
= 0; i
< 16; i
++) {
1129 if (!XAllocNamedColor(xw
.dis
, cmap
, colorname
[i
], &color
, &color
)) {
1131 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1133 dc
.col
[i
] = color
.pixel
;
1136 /* same colors as xterm */
1137 for(r
= 0; r
< 6; r
++)
1138 for(g
= 0; g
< 6; g
++)
1139 for(b
= 0; b
< 6; b
++) {
1140 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1141 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1142 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1143 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1145 fprintf(stderr
, "Could not allocate color %d\n", i
);
1147 dc
.col
[i
] = color
.pixel
;
1151 for(r
= 0; r
< 24; r
++, i
++) {
1152 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1153 if (!XAllocColor(xw
.dis
, cmap
, &color
)) {
1155 fprintf(stderr
, "Could not allocate color %d\n", i
);
1157 dc
.col
[i
] = color
.pixel
;
1162 xclear(int x1
, int y1
, int x2
, int y2
) {
1163 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1164 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1165 x1
* xw
.cw
, y1
* xw
.ch
,
1166 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1172 XClassHint
class = {TNAME
, TNAME
};
1173 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1175 .flags
= PSize
| PResizeInc
| PBaseSize
,
1178 .height_inc
= xw
.ch
,
1180 .base_height
= 2*BORDER
,
1181 .base_width
= 2*BORDER
,
1183 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1188 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1189 die("Can't open display\n");
1190 xw
.scr
= XDefaultScreen(xw
.dis
);
1193 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)) || !(dc
.bfont
= XLoadQueryFont(xw
.dis
, BOLDFONT
)))
1194 die("Can't load font %s\n", dc
.font
? BOLDFONT
: FONT
);
1196 /* XXX: Assuming same size for bold font */
1197 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
1198 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
;
1204 xw
.h
= term
.row
* xw
.ch
+ 2*BORDER
;
1205 xw
.w
= term
.col
* xw
.cw
+ 2*BORDER
;
1206 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1210 xw
.bufw
= xw
.w
- 2*BORDER
;
1211 xw
.bufh
= xw
.h
- 2*BORDER
;
1212 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1214 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1217 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
1218 | StructureNotifyMask
| FocusChangeMask
| PointerMotionMask
1219 | ButtonPressMask
| ButtonReleaseMask
);
1221 XMapWindow(xw
.dis
, xw
.win
);
1223 XStoreName(xw
.dis
, xw
.win
, "st");
1228 xdraws(char *s
, Glyph base
, int x
, int y
, int len
) {
1229 unsigned long xfg
, xbg
;
1230 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
->ascent
, width
= len
*xw
.cw
;
1233 if(base
.mode
& ATTR_REVERSE
)
1234 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1236 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1238 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1239 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1241 if(base
.mode
& ATTR_GFX
)
1242 for(i
= 0; i
< len
; i
++)
1243 s
[i
] = gfx
[(int)s
[i
]];
1245 XSetFont(xw
.dis
, dc
.gc
, base
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1246 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
, s
, len
);
1248 if(base
.mode
& ATTR_UNDERLINE
)
1249 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1254 static int oldx
= 0;
1255 static int oldy
= 0;
1256 Glyph g
= {' ', ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1258 LIMIT(oldx
, 0, term
.col
-1);
1259 LIMIT(oldy
, 0, term
.row
-1);
1261 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1262 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
1264 /* remove the old cursor */
1265 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
)
1266 xdraws(&term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1);
1268 xclear(oldx
, oldy
, oldx
, oldy
);
1270 /* draw the new one */
1271 if(!(term
.c
.state
& CURSOR_HIDE
) && xw
.hasfocus
) {
1272 xdraws(&g
.c
, g
, term
.c
.x
, term
.c
.y
, 1);
1273 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1278 /* basic drawing routines */
1280 xdrawc(int x
, int y
, Glyph g
) {
1281 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1282 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1283 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1284 XSetFont(xw
.dis
, dc
.gc
, g
.mode
& ATTR_BOLD
? dc
.bfont
->fid
: dc
.font
->fid
);
1285 XDrawImageString(xw
.dis
, xw
.buf
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &g
.c
, 1);
1292 xclear(0, 0, term
.col
-1, term
.row
-1);
1293 for(y
= 0; y
< term
.row
; y
++)
1294 for(x
= 0; x
< term
.col
; x
++)
1295 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1296 xdrawc(x
, y
, term
.line
[y
][x
]);
1299 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1304 /* optimized drawing routine */
1306 draw(int redraw_all
) {
1309 char buf
[DRAW_BUF_SIZ
];
1311 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1312 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
);
1313 for(y
= 0; y
< term
.row
; y
++) {
1314 base
= term
.line
[y
][0];
1316 for(x
= 0; x
< term
.col
; x
++) {
1317 new = term
.line
[y
][x
];
1318 if(sel
.bx
!=-1 && new.c
&& selected(x
, y
))
1319 new.mode
^= ATTR_REVERSE
;
1320 if(i
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1321 i
>= DRAW_BUF_SIZ
)) {
1322 xdraws(buf
, base
, ox
, y
, i
);
1325 if(new.state
& GLYPH_SET
) {
1334 xdraws(buf
, base
, ox
, y
, i
);
1337 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1344 expose(XEvent
*ev
) {
1345 draw(SCREEN_REDRAW
);
1349 xseturgency(int add
) {
1350 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1351 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1352 XSetWMHints(xw
.dis
, xw
.win
, h
);
1358 if((xw
.hasfocus
= ev
->type
== FocusIn
))
1360 draw(SCREEN_UPDATE
);
1366 for(i
= 0; i
< LEN(key
); i
++)
1368 return (char*)key
[i
].s
;
1373 kpress(XEvent
*ev
) {
1374 XKeyEvent
*e
= &ev
->xkey
;
1382 meta
= e
->state
& Mod1Mask
;
1383 shift
= e
->state
& ShiftMask
;
1384 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1386 if((customkey
= kmap(ksym
)))
1387 ttywrite(customkey
, strlen(customkey
));
1389 buf
[sizeof(buf
)-1] = '\0';
1390 if(meta
&& len
== 1)
1391 ttywrite("\033", 1);
1399 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1404 selpaste(), draw(1);
1407 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1416 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1419 xw
.w
= e
->xconfigure
.width
;
1420 xw
.h
= e
->xconfigure
.height
;
1421 xw
.bufw
= xw
.w
- 2*BORDER
;
1422 xw
.bufh
= xw
.h
- 2*BORDER
;
1423 col
= xw
.bufw
/ xw
.cw
;
1424 row
= xw
.bufh
/ xw
.ch
;
1426 ttyresize(col
, row
);
1427 xw
.bufh
= MAX(1, xw
.bufh
);
1428 xw
.bufw
= MAX(1, xw
.bufw
);
1429 XFreePixmap(xw
.dis
, xw
.buf
);
1430 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1431 draw(SCREEN_REDRAW
);
1438 int xfd
= XConnectionNumber(xw
.dis
);
1442 FD_SET(cmdfd
, &rfd
);
1444 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1447 die("select failed: %s\n", SERRNO
);
1449 if(FD_ISSET(cmdfd
, &rfd
)) {
1451 draw(SCREEN_UPDATE
);
1453 while(XPending(xw
.dis
)) {
1454 XNextEvent(xw
.dis
, &ev
);
1455 if(handler
[ev
.type
])
1456 (handler
[ev
.type
])(&ev
);
1462 main(int argc
, char *argv
[]) {
1463 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1464 die("st-" VERSION
", (c) 2010 st engineers\n");
1466 die("usage: st [-v]\n");
1467 setlocale(LC_CTYPE
, "");