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>
19 #include <X11/Xatom.h>
21 #include <X11/Xutil.h>
22 #include <X11/cursorfont.h>
23 #include <X11/keysym.h>
27 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
29 #elif defined(__FreeBSD__) || defined(__DragonFly__)
34 "st-" VERSION ", (c) 2010 st engineers\n" \
35 "usage: st [-t title] [-c class] [-e cmd] [-v]\n"
38 #define ESC_TITLE_SIZ 256
39 #define ESC_BUF_SIZ 256
40 #define ESC_ARG_SIZ 16
41 #define DRAW_BUF_SIZ 1024
44 #define SERRNO strerror(errno)
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #define MAX(a, b) ((a) < (b) ? (b) : (a))
47 #define LEN(a) (sizeof(a) / sizeof(a[0]))
48 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
49 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
50 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
51 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
52 #define IS_SET(flag) (term.mode & (flag))
54 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
55 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
56 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
57 CURSOR_SAVE
, CURSOR_LOAD
};
58 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
59 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
60 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
62 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
63 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
64 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
67 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
70 char c
[UTF_SIZ
]; /* character code */
71 char mode
; /* attribute flags */
72 int fg
; /* foreground */
73 int bg
; /* background */
74 char state
; /* state flags */
80 Glyph attr
; /* current char attributes */
86 /* CSI Escape sequence structs */
87 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
89 char buf
[ESC_BUF_SIZ
]; /* raw string */
90 int len
; /* raw string length */
93 int narg
; /* nb of args */
97 /* Internal representation of the screen */
100 int col
; /* nb col */
101 Line
* line
; /* screen */
102 Line
* alt
; /* alternate screen */
103 TCursor c
; /* cursor */
104 int top
; /* top scroll limit */
105 int bot
; /* bottom scroll limit */
106 int mode
; /* terminal mode flags */
107 int esc
; /* escape state flags */
108 char title
[ESC_TITLE_SIZ
];
112 /* Purely graphic info */
121 int w
; /* window width */
122 int h
; /* window height */
123 int bufw
; /* pixmap width */
124 int bufh
; /* pixmap height */
125 int ch
; /* char height */
126 int cw
; /* char width */
127 char state
; /* focus, redraw, visible */
135 /* Drawing Context */
137 unsigned long col
[256];
148 /* TODO: use better name for vars... */
153 struct {int x
, y
;} b
, e
;
159 static void die(const char *errstr
, ...);
160 static void draw(int);
161 static void execsh(void);
162 static void sigchld(int);
163 static void run(void);
165 static void csidump(void);
166 static void csihandle(void);
167 static void csiparse(void);
168 static void csireset(void);
170 static void tclearregion(int, int, int, int);
171 static void tcursor(int);
172 static void tdeletechar(int);
173 static void tdeleteline(int);
174 static void tinsertblank(int);
175 static void tinsertblankline(int);
176 static void tmoveto(int, int);
177 static void tnew(int, int);
178 static void tnewline(int);
179 static void tputtab(void);
180 static void tputc(char*);
181 static void treset(void);
182 static int tresize(int, int);
183 static void tscrollup(int, int);
184 static void tscrolldown(int, int);
185 static void tsetattr(int*, int);
186 static void tsetchar(char*);
187 static void tsetscroll(int, int);
188 static void tswapscreen(void);
190 static void ttynew(void);
191 static void ttyread(void);
192 static void ttyresize(int, int);
193 static void ttywrite(const char *, size_t);
195 static void xdraws(char *, Glyph
, int, int, int, int);
196 static void xhints(void);
197 static void xclear(int, int, int, int);
198 static void xdrawcursor(void);
199 static void xinit(void);
200 static void xloadcols(void);
201 static void xseturgency(int);
202 static void xsetsel(char*);
203 static void xresize(int, int);
205 static void expose(XEvent
*);
206 static void visibility(XEvent
*);
207 static void unmap(XEvent
*);
208 static char* kmap(KeySym
);
209 static void kpress(XEvent
*);
210 static void resize(XEvent
*);
211 static void focus(XEvent
*);
212 static void brelease(XEvent
*);
213 static void bpress(XEvent
*);
214 static void bmotion(XEvent
*);
215 static void selnotify(XEvent
*);
216 static void selrequest(XEvent
*);
218 static void selinit(void);
219 static inline int selected(int, int);
220 static void selcopy(void);
221 static void selpaste(void);
223 static int utf8decode(char *, long *);
224 static int utf8encode(long *, char *);
225 static int utf8size(char *);
226 static int isfullutf8(char *, int);
228 static void (*handler
[LASTEvent
])(XEvent
*) = {
230 [ConfigureNotify
] = resize
,
231 [VisibilityNotify
] = visibility
,
232 [UnmapNotify
] = unmap
,
236 [MotionNotify
] = bmotion
,
237 [ButtonPress
] = bpress
,
238 [ButtonRelease
] = brelease
,
239 [SelectionNotify
] = selnotify
,
240 [SelectionRequest
] = selrequest
,
247 static CSIEscape escseq
;
250 static Selection sel
;
251 static char **opt_cmd
= NULL
;
252 static char *opt_title
= NULL
;
253 static char *opt_class
= NULL
;
256 utf8decode(char *s
, long *u
) {
262 if(~c
&B7
) { /* 0xxxxxxx */
265 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
266 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
268 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
269 *u
= c
&(B3
|B2
|B1
|B0
);
271 } else if((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
276 for(i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
278 if((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
281 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
283 if((n
== 1 && *u
< 0x80) ||
284 (n
== 2 && *u
< 0x800) ||
285 (n
== 3 && *u
< 0x10000) ||
286 (*u
>= 0xD800 && *u
<= 0xDFFF))
295 utf8encode(long *u
, char *s
) {
300 sp
= (unsigned char*) s
;
303 *sp
= uc
; /* 0xxxxxxx */
305 } else if(*u
< 0x800) {
306 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
308 } else if(uc
< 0x10000) {
309 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
311 } else if(uc
<= 0x10FFFF) {
312 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
317 for(i
=n
,++sp
; i
>0; --i
,++sp
)
318 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
328 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
329 UTF-8 otherwise return 0 */
331 isfullutf8(char *s
, int b
) {
332 unsigned char *c1
, *c2
, *c3
;
334 c1
= (unsigned char *) s
;
335 c2
= (unsigned char *) ++s
;
336 c3
= (unsigned char *) ++s
;
339 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
341 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
343 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
345 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
347 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
348 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
356 unsigned char c
= *s
;
360 else if ((c
&(B7
|B6
|B5
)) == (B7
|B6
))
362 else if ((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
376 selected(int x
, int y
) {
377 if(sel
.ey
== y
&& sel
.by
== y
) {
378 int bx
= MIN(sel
.bx
, sel
.ex
);
379 int ex
= MAX(sel
.bx
, sel
.ex
);
380 return BETWEEN(x
, bx
, ex
);
382 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
383 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
387 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
389 *b
= e
->xbutton
.button
;
391 *x
= e
->xbutton
.x
/xw
.cw
;
392 *y
= e
->xbutton
.y
/xw
.ch
;
393 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
394 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
395 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
396 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
402 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
403 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
409 int x
, y
, sz
, sl
, ls
= 0;
414 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
415 ptr
= str
= malloc(sz
);
416 for(y
= 0; y
< term
.row
; y
++) {
417 for(x
= 0; x
< term
.col
; x
++)
418 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
419 sl
= utf8size(term
.line
[y
][x
].c
);
420 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
423 if(ls
&& y
< sel
.e
.y
)
432 selnotify(XEvent
*e
) {
433 unsigned long nitems
;
434 unsigned long ofs
, rem
;
441 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
442 False
, AnyPropertyType
, &type
, &format
,
443 &nitems
, &rem
, &data
)) {
444 fprintf(stderr
, "Clipboard allocation failed\n");
447 ttywrite((const char *) data
, nitems
* format
/ 8);
449 /* number of 32-bit chunks returned */
450 ofs
+= nitems
* format
/ 32;
456 XConvertSelection(xw
.dpy
, XA_PRIMARY
, XA_STRING
, XA_PRIMARY
, xw
.win
, CurrentTime
);
460 selrequest(XEvent
*e
) {
461 XSelectionRequestEvent
*xsre
;
465 xsre
= (XSelectionRequestEvent
*) e
;
466 xev
.type
= SelectionNotify
;
467 xev
.requestor
= xsre
->requestor
;
468 xev
.selection
= xsre
->selection
;
469 xev
.target
= xsre
->target
;
470 xev
.time
= xsre
->time
;
474 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
475 if(xsre
->target
== xa_targets
) {
476 /* respond with the supported type */
477 Atom string
= XA_STRING
;
478 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
479 XA_ATOM
, 32, PropModeReplace
,
480 (unsigned char *) &string
, 1);
481 xev
.property
= xsre
->property
;
482 } else if(xsre
->target
== XA_STRING
) {
483 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
484 xsre
->target
, 8, PropModeReplace
,
485 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
486 xev
.property
= xsre
->property
;
489 /* all done, send a notification to the listener */
490 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
491 fprintf(stderr
, "Error sending SelectionNotify event\n");
496 /* register the selection for both the clipboard and the primary */
502 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
504 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
505 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
510 /* TODO: doubleclick to select word */
512 brelease(XEvent
*e
) {
515 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
516 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
530 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
531 /* XXX: draw() can't keep up, disabled for now.
532 selection is visible on button release.
538 die(const char *errstr
, ...) {
541 va_start(ap
, errstr
);
542 vfprintf(stderr
, errstr
, ap
);
550 char *envshell
= getenv("SHELL");
552 DEFAULT(envshell
, "sh");
553 putenv("TERM="TNAME
);
554 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
555 execvp(args
[0], args
);
562 if(waitpid(pid
, &stat
, 0) < 0)
563 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
565 exit(WEXITSTATUS(stat
));
574 /* seems to work fine on linux, openbsd and freebsd */
575 struct winsize w
= {term
.row
, term
.col
, 0, 0};
576 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
577 die("openpty failed: %s\n", SERRNO
);
579 switch(pid
= fork()) {
581 die("fork failed\n");
584 setsid(); /* create a new process group */
585 dup2(s
, STDIN_FILENO
);
586 dup2(s
, STDOUT_FILENO
);
587 dup2(s
, STDERR_FILENO
);
588 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
589 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
597 signal(SIGCHLD
, sigchld
);
604 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
606 fprintf(stderr
, "\n");
611 static char buf
[BUFSIZ
];
612 static int buflen
= 0;
615 int charsize
; /* size of utf8 char in bytes */
619 /* append read bytes to unprocessed bytes */
620 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
621 die("Couldn't read from shell: %s\n", SERRNO
);
623 /* process every complete utf8 char */
626 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
627 charsize
= utf8decode(ptr
, &utf8c
);
628 utf8encode(&utf8c
, s
);
634 /* keep any uncomplete utf8 char for the next call */
635 memmove(buf
, ptr
, buflen
);
639 ttywrite(const char *s
, size_t n
) {
640 if(write(cmdfd
, s
, n
) == -1)
641 die("write error on tty: %s\n", SERRNO
);
645 ttyresize(int x
, int y
) {
650 w
.ws_xpixel
= w
.ws_ypixel
= 0;
651 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
652 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
659 if(mode
== CURSOR_SAVE
)
661 else if(mode
== CURSOR_LOAD
)
662 term
.c
= c
, tmoveto(c
.x
, c
.y
);
671 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
673 term
.top
= 0, term
.bot
= term
.row
- 1;
674 term
.mode
= MODE_WRAP
;
675 tclearregion(0, 0, term
.col
-1, term
.row
-1);
679 tnew(int col
, int row
) {
680 /* set screen size */
681 term
.row
= row
, term
.col
= col
;
682 term
.line
= malloc(term
.row
* sizeof(Line
));
683 term
.alt
= malloc(term
.row
* sizeof(Line
));
684 for(row
= 0 ; row
< term
.row
; row
++) {
685 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
686 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
694 Line
* tmp
= term
.line
;
695 term
.line
= term
.alt
;
697 term
.mode
^= MODE_ALTSCREEN
;
701 tscrolldown(int orig
, int n
) {
705 LIMIT(n
, 0, term
.bot
-orig
+1);
707 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
709 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
711 term
.line
[i
] = term
.line
[i
-n
];
712 term
.line
[i
-n
] = temp
;
717 tscrollup(int orig
, int n
) {
720 LIMIT(n
, 0, term
.bot
-orig
+1);
722 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
724 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
726 term
.line
[i
] = term
.line
[i
+n
];
727 term
.line
[i
+n
] = temp
;
732 tnewline(int first_col
) {
735 tscrollup(term
.top
, 1);
738 tmoveto(first_col
? 0 : term
.c
.x
, y
);
744 char *p
= escseq
.buf
;
748 escseq
.priv
= 1, p
++;
750 while(p
< escseq
.buf
+escseq
.len
) {
752 escseq
.arg
[escseq
.narg
] *= 10;
753 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
755 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
766 tmoveto(int x
, int y
) {
767 LIMIT(x
, 0, term
.col
-1);
768 LIMIT(y
, 0, term
.row
-1);
769 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
776 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
777 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
778 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
782 tclearregion(int x1
, int y1
, int x2
, int y2
) {
786 temp
= x1
, x1
= x2
, x2
= temp
;
788 temp
= y1
, y1
= y2
, y2
= temp
;
790 LIMIT(x1
, 0, term
.col
-1);
791 LIMIT(x2
, 0, term
.col
-1);
792 LIMIT(y1
, 0, term
.row
-1);
793 LIMIT(y2
, 0, term
.row
-1);
795 for(y
= y1
; y
<= y2
; y
++)
796 for(x
= x1
; x
<= x2
; x
++)
797 term
.line
[y
][x
].state
= 0;
802 int src
= term
.c
.x
+ n
;
804 int size
= term
.col
- src
;
806 if(src
>= term
.col
) {
807 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
810 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
811 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
815 tinsertblank(int n
) {
818 int size
= term
.col
- dst
;
820 if(dst
>= term
.col
) {
821 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
824 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
825 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
829 tinsertblankline(int n
) {
830 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
833 tscrolldown(term
.c
.y
, n
);
838 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
841 tscrollup(term
.c
.y
, n
);
845 tsetattr(int *attr
, int l
) {
848 for(i
= 0; i
< l
; i
++) {
851 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
852 term
.c
.attr
.fg
= DefaultFG
;
853 term
.c
.attr
.bg
= DefaultBG
;
856 term
.c
.attr
.mode
|= ATTR_BOLD
;
859 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
862 term
.c
.attr
.mode
|= ATTR_REVERSE
;
865 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
868 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
871 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
874 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
876 if (BETWEEN(attr
[i
], 0, 255))
877 term
.c
.attr
.fg
= attr
[i
];
879 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
882 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
885 term
.c
.attr
.fg
= DefaultFG
;
888 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
890 if (BETWEEN(attr
[i
], 0, 255))
891 term
.c
.attr
.bg
= attr
[i
];
893 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
896 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
899 term
.c
.attr
.bg
= DefaultBG
;
902 if(BETWEEN(attr
[i
], 30, 37))
903 term
.c
.attr
.fg
= attr
[i
] - 30;
904 else if(BETWEEN(attr
[i
], 40, 47))
905 term
.c
.attr
.bg
= attr
[i
] - 40;
906 else if(BETWEEN(attr
[i
], 90, 97))
907 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
908 else if(BETWEEN(attr
[i
], 100, 107))
909 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
911 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
919 tsetscroll(int t
, int b
) {
922 LIMIT(t
, 0, term
.row
-1);
923 LIMIT(b
, 0, term
.row
-1);
935 switch(escseq
.mode
) {
938 fprintf(stderr
, "erresc: unknown csi ");
942 case '@': /* ICH -- Insert <n> blank char */
943 DEFAULT(escseq
.arg
[0], 1);
944 tinsertblank(escseq
.arg
[0]);
946 case 'A': /* CUU -- Cursor <n> Up */
948 DEFAULT(escseq
.arg
[0], 1);
949 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
951 case 'B': /* CUD -- Cursor <n> Down */
952 DEFAULT(escseq
.arg
[0], 1);
953 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
955 case 'C': /* CUF -- Cursor <n> Forward */
957 DEFAULT(escseq
.arg
[0], 1);
958 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
960 case 'D': /* CUB -- Cursor <n> Backward */
961 DEFAULT(escseq
.arg
[0], 1);
962 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
964 case 'E': /* CNL -- Cursor <n> Down and first col */
965 DEFAULT(escseq
.arg
[0], 1);
966 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
968 case 'F': /* CPL -- Cursor <n> Up and first col */
969 DEFAULT(escseq
.arg
[0], 1);
970 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
972 case 'G': /* CHA -- Move to <col> */
973 case '`': /* XXX: HPA -- same? */
974 DEFAULT(escseq
.arg
[0], 1);
975 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
977 case 'H': /* CUP -- Move to <row> <col> */
978 case 'f': /* XXX: HVP -- same? */
979 DEFAULT(escseq
.arg
[0], 1);
980 DEFAULT(escseq
.arg
[1], 1);
981 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
983 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
984 case 'J': /* ED -- Clear screen */
985 switch(escseq
.arg
[0]) {
987 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
988 if(term
.c
.y
< term
.row
-1)
989 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
993 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
994 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
997 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1003 case 'K': /* EL -- Clear line */
1004 switch(escseq
.arg
[0]) {
1006 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1009 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1012 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1016 case 'S': /* SU -- Scroll <n> line up */
1017 DEFAULT(escseq
.arg
[0], 1);
1018 tscrollup(term
.top
, escseq
.arg
[0]);
1020 case 'T': /* SD -- Scroll <n> line down */
1021 DEFAULT(escseq
.arg
[0], 1);
1022 tscrolldown(term
.top
, escseq
.arg
[0]);
1024 case 'L': /* IL -- Insert <n> blank lines */
1025 DEFAULT(escseq
.arg
[0], 1);
1026 tinsertblankline(escseq
.arg
[0]);
1028 case 'l': /* RM -- Reset Mode */
1030 switch(escseq
.arg
[0]) {
1032 term
.mode
&= ~MODE_APPKEYPAD
;
1034 case 5: /* TODO: DECSCNM -- Remove reverse video */
1037 term
.mode
&= ~MODE_WRAP
;
1039 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1042 term
.mode
&= ~MODE_CRLF
;
1045 term
.c
.state
|= CURSOR_HIDE
;
1047 case 1049: /* = 1047 and 1048 */
1049 if(IS_SET(MODE_ALTSCREEN
)) {
1050 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1053 if(escseq
.arg
[0] == 1047)
1056 tcursor(CURSOR_LOAD
);
1062 switch(escseq
.arg
[0]) {
1064 term
.mode
&= ~MODE_INSERT
;
1071 case 'M': /* DL -- Delete <n> lines */
1072 DEFAULT(escseq
.arg
[0], 1);
1073 tdeleteline(escseq
.arg
[0]);
1075 case 'X': /* ECH -- Erase <n> char */
1076 DEFAULT(escseq
.arg
[0], 1);
1077 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1079 case 'P': /* DCH -- Delete <n> char */
1080 DEFAULT(escseq
.arg
[0], 1);
1081 tdeletechar(escseq
.arg
[0]);
1083 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1084 case 'd': /* VPA -- Move to <row> */
1085 DEFAULT(escseq
.arg
[0], 1);
1086 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1088 case 'h': /* SM -- Set terminal mode */
1090 switch(escseq
.arg
[0]) {
1092 term
.mode
|= MODE_APPKEYPAD
;
1094 case 5: /* DECSCNM -- Reverve video */
1095 /* TODO: set REVERSE on the whole screen (f) */
1098 term
.mode
|= MODE_WRAP
;
1101 term
.mode
|= MODE_CRLF
;
1103 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1104 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1105 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1108 term
.c
.state
&= ~CURSOR_HIDE
;
1110 case 1049: /* = 1047 and 1048 */
1112 if(IS_SET(MODE_ALTSCREEN
))
1113 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1116 if(escseq
.arg
[0] == 1047)
1119 tcursor(CURSOR_SAVE
);
1121 default: goto unknown
;
1124 switch(escseq
.arg
[0]) {
1126 term
.mode
|= MODE_INSERT
;
1128 default: goto unknown
;
1132 case 'm': /* SGR -- Terminal attribute (color) */
1133 tsetattr(escseq
.arg
, escseq
.narg
);
1135 case 'r': /* DECSTBM -- Set Scrolling Region */
1139 DEFAULT(escseq
.arg
[0], 1);
1140 DEFAULT(escseq
.arg
[1], term
.row
);
1141 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1145 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1146 tcursor(CURSOR_SAVE
);
1148 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1149 tcursor(CURSOR_LOAD
);
1157 printf("ESC [ %s", escseq
.priv
? "? " : "");
1159 for(i
= 0; i
< escseq
.narg
; i
++)
1160 printf("%d ", escseq
.arg
[i
]);
1162 putchar(escseq
.mode
);
1168 memset(&escseq
, 0, sizeof(escseq
));
1173 int space
= TAB
- term
.c
.x
% TAB
;
1174 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1180 if(term
.esc
& ESC_START
) {
1181 if(term
.esc
& ESC_CSI
) {
1182 escseq
.buf
[escseq
.len
++] = ascii
;
1183 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1185 csiparse(), csihandle();
1187 /* TODO: handle other OSC */
1188 } else if(term
.esc
& ESC_OSC
) {
1191 term
.esc
= ESC_START
| ESC_TITLE
;
1193 } else if(term
.esc
& ESC_TITLE
) {
1194 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1196 term
.title
[term
.titlelen
] = '\0';
1197 XStoreName(xw
.dpy
, xw
.win
, term
.title
);
1199 term
.title
[term
.titlelen
++] = ascii
;
1201 } else if(term
.esc
& ESC_ALTCHARSET
) {
1203 case '0': /* Line drawing crap */
1204 term
.c
.attr
.mode
|= ATTR_GFX
;
1206 case 'B': /* Back to regular text */
1207 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1210 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1216 term
.esc
|= ESC_CSI
;
1219 term
.esc
|= ESC_OSC
;
1222 term
.esc
|= ESC_ALTCHARSET
;
1224 case 'D': /* IND -- Linefeed */
1225 if(term
.c
.y
== term
.bot
)
1226 tscrollup(term
.top
, 1);
1228 tmoveto(term
.c
.x
, term
.c
.y
+1);
1231 case 'E': /* NEL -- Next line */
1232 tnewline(1); /* always go to first col */
1235 case 'M': /* RI -- Reverse index */
1236 if(term
.c
.y
== term
.top
)
1237 tscrolldown(term
.top
, 1);
1239 tmoveto(term
.c
.x
, term
.c
.y
-1);
1242 case 'c': /* RIS -- Reset to inital state */
1246 case '=': /* DECPAM -- Application keypad */
1247 term
.mode
|= MODE_APPKEYPAD
;
1250 case '>': /* DECPNM -- Normal keypad */
1251 term
.mode
&= ~MODE_APPKEYPAD
;
1254 case '7': /* DECSC -- Save Cursor */
1255 tcursor(CURSOR_SAVE
);
1258 case '8': /* DECRC -- Restore Cursor */
1259 tcursor(CURSOR_LOAD
);
1263 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1264 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1274 tmoveto(term
.c
.x
-1, term
.c
.y
);
1277 tmoveto(0, term
.c
.y
);
1282 /* go to first col if the mode is set */
1283 tnewline(IS_SET(MODE_CRLF
));
1286 if(!(xw
.state
& WIN_FOCUSED
))
1291 term
.esc
= ESC_START
;
1294 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1295 tnewline(1); /* always go to first col */
1297 if(term
.c
.x
+1 < term
.col
)
1298 tmoveto(term
.c
.x
+1, term
.c
.y
);
1300 term
.c
.state
|= CURSOR_WRAPNEXT
;
1307 tresize(int col
, int row
) {
1309 int minrow
= MIN(row
, term
.row
);
1310 int mincol
= MIN(col
, term
.col
);
1311 int slide
= term
.c
.y
- row
+ 1;
1313 if(col
< 1 || row
< 1)
1316 /* free unneeded rows */
1319 /* slide screen to keep cursor where we expect it -
1320 * tscrollup would work here, but we can optimize to
1321 * memmove because we're freeing the earlier lines */
1322 for(/* i = 0 */; i
< slide
; i
++) {
1326 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1327 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1329 for(i
+= row
; i
< term
.row
; i
++) {
1334 /* resize to new height */
1335 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1336 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1338 /* resize each row to new width, zero-pad if needed */
1339 for(i
= 0; i
< minrow
; i
++) {
1340 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1341 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1342 for(x
= mincol
; x
< col
; x
++) {
1343 term
.line
[i
][x
].state
= 0;
1344 term
.alt
[i
][x
].state
= 0;
1348 /* allocate any new rows */
1349 for(/* i == minrow */; i
< row
; i
++) {
1350 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1351 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1354 /* update terminal size */
1355 term
.col
= col
, term
.row
= row
;
1356 /* make use of the LIMIT in tmoveto */
1357 tmoveto(term
.c
.x
, term
.c
.y
);
1358 /* reset scrolling region */
1359 tsetscroll(0, row
-1);
1364 xresize(int col
, int row
) {
1370 xw
.bufw
= MAX(1, col
* xw
.cw
);
1371 xw
.bufh
= MAX(1, row
* xw
.ch
);
1372 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1373 XCopyArea(xw
.dpy
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1374 XFreePixmap(xw
.dpy
, xw
.buf
);
1375 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1377 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0,
1378 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1379 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1380 XClearArea(xw
.dpy
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1381 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1384 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
,
1385 xw
.bufw
, xw
.bufh
-oldh
);
1386 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1387 XClearArea(xw
.dpy
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1388 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1397 unsigned long white
= WhitePixel(xw
.dpy
, xw
.scr
);
1399 for(i
= 0; i
< 16; i
++) {
1400 if (!XAllocNamedColor(xw
.dpy
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1402 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1404 dc
.col
[i
] = color
.pixel
;
1407 /* same colors as xterm */
1408 for(r
= 0; r
< 6; r
++)
1409 for(g
= 0; g
< 6; g
++)
1410 for(b
= 0; b
< 6; b
++) {
1411 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1412 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1413 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1414 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1416 fprintf(stderr
, "Could not allocate color %d\n", i
);
1418 dc
.col
[i
] = color
.pixel
;
1422 for(r
= 0; r
< 24; r
++, i
++) {
1423 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1424 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
1426 fprintf(stderr
, "Could not allocate color %d\n", i
);
1428 dc
.col
[i
] = color
.pixel
;
1433 xclear(int x1
, int y1
, int x2
, int y2
) {
1434 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[DefaultBG
]);
1435 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
,
1436 x1
* xw
.cw
, y1
* xw
.ch
,
1437 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1443 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1444 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1446 .flags
= PSize
| PResizeInc
| PBaseSize
,
1449 .height_inc
= xw
.ch
,
1451 .base_height
= 2*BORDER
,
1452 .base_width
= 2*BORDER
,
1454 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1458 xinitfont(char *fontstr
)
1461 char *def
, **missing
;
1465 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
1468 fprintf(stderr
, "st: missing fontset: %s\n", missing
[n
]);
1469 XFreeStringList(missing
);
1475 xgetfontinfo(XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
)
1477 XFontStruct
**xfonts
;
1481 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
1482 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
1483 for(i
= 0; i
< n
; i
++) {
1484 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
1485 *descent
= MAX(*descent
, (*xfonts
)->descent
);
1486 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
1487 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
1493 initfonts(char *fontstr
, char *bfontstr
)
1495 if((dc
.font
.set
= xinitfont(fontstr
)) == NULL
||
1496 (dc
.bfont
.set
= xinitfont(bfontstr
)) == NULL
)
1497 die("Can't load font %s\n", dc
.font
.set
? BOLDFONT
: FONT
);
1498 xgetfontinfo(dc
.font
.set
, &dc
.font
.ascent
, &dc
.font
.descent
,
1499 &dc
.font
.lbearing
, &dc
.font
.rbearing
);
1500 xgetfontinfo(dc
.bfont
.set
, &dc
.bfont
.ascent
, &dc
.bfont
.descent
,
1501 &dc
.bfont
.lbearing
, &dc
.bfont
.rbearing
);
1506 XSetWindowAttributes attrs
;
1509 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
1510 die("Can't open display\n");
1511 xw
.scr
= XDefaultScreen(xw
.dpy
);
1514 initfonts(FONT
, BOLDFONT
);
1516 /* XXX: Assuming same size for bold font */
1517 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1518 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1521 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1524 /* window - default size */
1525 xw
.bufh
= 24 * xw
.ch
;
1526 xw
.bufw
= 80 * xw
.cw
;
1527 xw
.h
= xw
.bufh
+ 2*BORDER
;
1528 xw
.w
= xw
.bufw
+ 2*BORDER
;
1530 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1531 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1532 attrs
.bit_gravity
= NorthWestGravity
;
1533 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1534 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1535 | PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1536 attrs
.colormap
= xw
.cmap
;
1538 xw
.win
= XCreateWindow(xw
.dpy
, XRootWindow(xw
.dpy
, xw
.scr
), 0, 0,
1539 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1540 XDefaultVisual(xw
.dpy
, xw
.scr
),
1541 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1544 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
1548 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1549 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1550 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1551 XNFocusWindow
, xw
.win
, NULL
);
1553 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
1555 /* white cursor, black outline */
1556 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
1557 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1558 XRecolorCursor(xw
.dpy
, cursor
,
1559 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
1560 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
1562 XMapWindow(xw
.dpy
, xw
.win
);
1564 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
1569 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
1570 unsigned long xfg
, xbg
;
1571 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= charlen
*xw
.cw
;
1574 if(base
.mode
& ATTR_REVERSE
)
1575 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1577 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1579 XSetBackground(xw
.dpy
, dc
.gc
, xbg
);
1580 XSetForeground(xw
.dpy
, dc
.gc
, xfg
);
1582 if(base
.mode
& ATTR_GFX
) {
1583 for(i
= 0; i
< bytelen
; i
++) {
1584 char c
= gfx
[(unsigned int)s
[i
] % 256];
1587 else if(s
[i
] > 0x5f)
1592 XmbDrawImageString(xw
.dpy
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.set
: dc
.font
.set
,
1593 dc
.gc
, winx
, winy
, s
, bytelen
);
1595 if(base
.mode
& ATTR_UNDERLINE
)
1596 XDrawLine(xw
.dpy
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1601 static int oldx
= 0;
1602 static int oldy
= 0;
1604 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1606 LIMIT(oldx
, 0, term
.col
-1);
1607 LIMIT(oldy
, 0, term
.row
-1);
1609 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1610 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1612 /* remove the old cursor */
1613 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1614 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
1615 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1617 xclear(oldx
, oldy
, oldx
, oldy
);
1619 /* draw the new one */
1620 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1622 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1623 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1628 /* basic drawing routines */
1630 xdrawc(int x
, int y
, Glyph g
) {
1631 int sl
= utf8size(g
.c
);
1632 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1633 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[g
.bg
]);
1634 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[g
.fg
]);
1635 XmbDrawImageString(xw
.dpy
, xw
.buf
, g
.mode
&ATTR_BOLD
?dc
.bfont
.fs
:dc
.font
.fs
,
1636 dc
.gc
, r
.x
, r
.y
+dc
.font
.ascent
, g
.c
, sl
);
1643 xclear(0, 0, term
.col
-1, term
.row
-1);
1644 for(y
= 0; y
< term
.row
; y
++)
1645 for(x
= 0; x
< term
.col
; x
++)
1646 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1647 xdrawc(x
, y
, term
.line
[y
][x
]);
1650 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1655 /* optimized drawing routine */
1657 draw(int redraw_all
) {
1658 int ic
, ib
, x
, y
, ox
, sl
;
1660 char buf
[DRAW_BUF_SIZ
];
1662 if(!(xw
.state
& WIN_VISIBLE
))
1665 xclear(0, 0, term
.col
-1, term
.row
-1);
1666 for(y
= 0; y
< term
.row
; y
++) {
1667 base
= term
.line
[y
][0];
1669 for(x
= 0; x
< term
.col
; x
++) {
1670 new = term
.line
[y
][x
];
1671 if(sel
.bx
!=-1 && *(new.c
) && selected(x
, y
))
1672 new.mode
^= ATTR_REVERSE
;
1673 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1674 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1675 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1678 if(new.state
& GLYPH_SET
) {
1683 sl
= utf8size(new.c
);
1684 memcpy(buf
+ib
, new.c
, sl
);
1690 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1693 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1699 expose(XEvent
*ev
) {
1700 XExposeEvent
*e
= &ev
->xexpose
;
1701 if(xw
.state
& WIN_REDRAW
) {
1703 xw
.state
&= ~WIN_REDRAW
;
1704 draw(SCREEN_REDRAW
);
1707 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1708 e
->width
, e
->height
, e
->x
, e
->y
);
1712 visibility(XEvent
*ev
) {
1713 XVisibilityEvent
*e
= &ev
->xvisibility
;
1714 if(e
->state
== VisibilityFullyObscured
)
1715 xw
.state
&= ~WIN_VISIBLE
;
1716 else if(!(xw
.state
& WIN_VISIBLE
))
1717 /* need a full redraw for next Expose, not just a buf copy */
1718 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1723 xw
.state
&= ~WIN_VISIBLE
;
1727 xseturgency(int add
) {
1728 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1729 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1730 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1736 if(ev
->type
== FocusIn
) {
1737 xw
.state
|= WIN_FOCUSED
;
1740 xw
.state
&= ~WIN_FOCUSED
;
1741 draw(SCREEN_UPDATE
);
1747 for(i
= 0; i
< LEN(key
); i
++)
1749 return (char*)key
[i
].s
;
1754 kpress(XEvent
*ev
) {
1755 XKeyEvent
*e
= &ev
->xkey
;
1764 meta
= e
->state
& Mod1Mask
;
1765 shift
= e
->state
& ShiftMask
;
1766 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1768 /* 1. custom keys from config.h */
1769 if((customkey
= kmap(ksym
)))
1770 ttywrite(customkey
, strlen(customkey
));
1771 /* 2. hardcoded (overrides X lookup) */
1778 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1786 if(IS_SET(MODE_CRLF
))
1787 ttywrite("\r\n", 2);
1794 if(meta
&& len
== 1)
1795 ttywrite("\033", 1);
1806 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1809 xw
.w
= e
->xconfigure
.width
;
1810 xw
.h
= e
->xconfigure
.height
;
1811 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
1812 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
1813 if(col
== term
.col
&& row
== term
.row
)
1815 if(tresize(col
, row
))
1816 draw(SCREEN_REDRAW
);
1817 ttyresize(col
, row
);
1825 int xfd
= XConnectionNumber(xw
.dpy
);
1829 FD_SET(cmdfd
, &rfd
);
1831 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1834 die("select failed: %s\n", SERRNO
);
1836 if(FD_ISSET(cmdfd
, &rfd
)) {
1838 draw(SCREEN_UPDATE
);
1840 while(XPending(xw
.dpy
)) {
1841 XNextEvent(xw
.dpy
, &ev
);
1842 if (XFilterEvent(&ev
, xw
.win
))
1844 if(handler
[ev
.type
])
1845 (handler
[ev
.type
])(&ev
);
1851 main(int argc
, char *argv
[]) {
1854 for(i
= 1; i
< argc
; i
++) {
1855 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1857 if(++i
< argc
) opt_title
= argv
[i
];
1860 if(++i
< argc
) opt_class
= argv
[i
];
1863 if(++i
< argc
) opt_cmd
= &argv
[i
];
1869 /* -e eats every remaining arguments */
1873 setlocale(LC_CTYPE
, "");