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/Xatom.h>
21 #include <X11/keysym.h>
22 #include <X11/Xutil.h>
26 #elif defined(__OpenBSD__) || defined(__NetBSD__)
28 #elif defined(__FreeBSD__) || defined(__DragonFly__)
33 "st-" VERSION ", (c) 2010 st engineers\n" \
34 "usage: st [-t title] [-c class] [-e cmd] [-v]\n"
37 #define ESC_TITLE_SIZ 256
38 #define ESC_BUF_SIZ 256
39 #define ESC_ARG_SIZ 16
40 #define DRAW_BUF_SIZ 1024
43 #define SERRNO strerror(errno)
44 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #define MAX(a, b) ((a) < (b) ? (b) : (a))
46 #define LEN(a) (sizeof(a) / sizeof(a[0]))
47 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
48 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
49 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
50 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
51 #define IS_SET(flag) (term.mode & (flag))
53 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
54 enum { ATTR_NULL
=0 , ATTR_REVERSE
=1 , ATTR_UNDERLINE
=2, ATTR_BOLD
=4, ATTR_GFX
=8 };
55 enum { CURSOR_UP
, CURSOR_DOWN
, CURSOR_LEFT
, CURSOR_RIGHT
,
56 CURSOR_SAVE
, CURSOR_LOAD
};
57 enum { CURSOR_DEFAULT
= 0, CURSOR_HIDE
= 1, CURSOR_WRAPNEXT
= 2 };
58 enum { GLYPH_SET
=1, GLYPH_DIRTY
=2 };
59 enum { MODE_WRAP
=1, MODE_INSERT
=2, MODE_APPKEYPAD
=4, MODE_ALTSCREEN
=8,
61 enum { ESC_START
=1, ESC_CSI
=2, ESC_OSC
=4, ESC_TITLE
=8, ESC_ALTCHARSET
=16 };
62 enum { SCREEN_UPDATE
, SCREEN_REDRAW
};
63 enum { WIN_VISIBLE
=1, WIN_REDRAW
=2, WIN_FOCUSED
=4 };
66 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
69 char c
[UTF_SIZ
]; /* character code */
70 char mode
; /* attribute flags */
71 int fg
; /* foreground */
72 int bg
; /* background */
73 char state
; /* state flags */
79 Glyph attr
; /* current char attributes */
85 /* CSI Escape sequence structs */
86 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
88 char buf
[ESC_BUF_SIZ
]; /* raw string */
89 int len
; /* raw string length */
92 int narg
; /* nb of args */
96 /* Internal representation of the screen */
100 Line
* line
; /* screen */
101 Line
* alt
; /* alternate screen */
102 TCursor c
; /* cursor */
103 int top
; /* top scroll limit */
104 int bot
; /* bottom scroll limit */
105 int mode
; /* terminal mode flags */
106 int esc
; /* escape state flags */
107 char title
[ESC_TITLE_SIZ
];
111 /* Purely graphic info */
120 int w
; /* window width */
121 int h
; /* window height */
122 int bufw
; /* pixmap width */
123 int bufh
; /* pixmap height */
124 int ch
; /* char height */
125 int cw
; /* char width */
126 char state
; /* focus, redraw, visible */
142 /* Drawing Context */
144 unsigned long col
[256];
150 /* TODO: use better name for vars... */
155 struct {int x
, y
;} b
, e
;
161 static void die(const char *errstr
, ...);
162 static void draw(int);
163 static void execsh(void);
164 static void sigchld(int);
165 static void run(void);
167 static void csidump(void);
168 static void csihandle(void);
169 static void csiparse(void);
170 static void csireset(void);
172 static void tclearregion(int, int, int, int);
173 static void tcursor(int);
174 static void tdeletechar(int);
175 static void tdeleteline(int);
176 static void tinsertblank(int);
177 static void tinsertblankline(int);
178 static void tmoveto(int, int);
179 static void tnew(int, int);
180 static void tnewline(int);
181 static void tputtab(void);
182 static void tputc(char*);
183 static void treset(void);
184 static int tresize(int, int);
185 static void tscrollup(int, int);
186 static void tscrolldown(int, int);
187 static void tsetattr(int*, int);
188 static void tsetchar(char*);
189 static void tsetscroll(int, int);
190 static void tswapscreen(void);
192 static void ttynew(void);
193 static void ttyread(void);
194 static void ttyresize(int, int);
195 static void ttywrite(const char *, size_t);
197 static void xdraws(char *, Glyph
, int, int, int, int);
198 static void xhints(void);
199 static void xclear(int, int, int, int);
200 static void xdrawcursor(void);
201 static void xinit(void);
202 static void xloadcols(void);
203 static void xseturgency(int);
204 static void xsetsel(char*);
205 static void xresize(int, int);
207 static void expose(XEvent
*);
208 static void visibility(XEvent
*);
209 static void unmap(XEvent
*);
210 static char* kmap(KeySym
);
211 static void kpress(XEvent
*);
212 static void resize(XEvent
*);
213 static void focus(XEvent
*);
214 static void brelease(XEvent
*);
215 static void bpress(XEvent
*);
216 static void bmotion(XEvent
*);
217 static void selnotify(XEvent
*);
218 static void selrequest(XEvent
*);
220 static void selinit(void);
221 static inline int selected(int, int);
222 static void selcopy(void);
223 static void selpaste(void);
225 static int stou(char *, long *);
226 static int utos(long *, char *);
227 static int slen(char *);
228 static int canstou(char *, int);
230 static void (*handler
[LASTEvent
])(XEvent
*) = {
232 [ConfigureNotify
] = resize
,
233 [VisibilityNotify
] = visibility
,
234 [UnmapNotify
] = unmap
,
238 [MotionNotify
] = bmotion
,
239 [ButtonPress
] = bpress
,
240 [ButtonRelease
] = brelease
,
241 [SelectionNotify
] = selnotify
,
242 [SelectionRequest
] = selrequest
,
249 static CSIEscape escseq
;
252 static Selection sel
;
253 static char *opt_cmd
= NULL
;
254 static char *opt_title
= NULL
;
255 static char *opt_class
= NULL
;
258 static int stou(char *s
, long *u
) {
264 if(~c
&B7
) { /* 0xxxxxxx */
267 } else if ((c
&(B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
268 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
270 } else if ((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
271 *u
= c
&(B3
|B2
|B1
|B0
);
273 } else if ((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
278 for (i
=n
,++s
; i
>0; --i
,++rtn
,++s
) {
280 if ((c
&(B7
|B6
)) != B7
) /* 10xxxxxx */
283 *u
|= c
&(B5
|B4
|B3
|B2
|B1
|B0
);
285 if ((n
== 1 && *u
< 0x80) ||
286 (n
== 2 && *u
< 0x800) ||
287 (n
== 3 && *u
< 0x10000) ||
288 (*u
>= 0xD800 && *u
<= 0xDFFF))
297 static int utos(long *u
, char *s
) {
302 sp
= (unsigned char*) s
;
305 *sp
= uc
; /* 0xxxxxxx */
307 } else if (*u
< 0x800) {
308 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
310 } else if (uc
< 0x10000) {
311 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
313 } else if (uc
<= 0x10FFFF) {
314 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
319 for (i
=n
,++sp
; i
>0; --i
,++sp
)
320 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
330 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
331 UTF-8 otherwise return 0 */
332 static int canstou(char *s
, int b
) {
333 unsigned char c
= *s
;
340 else if ((c
&(B7
|B6
|B5
)) == (B7
|B6
))
342 else if ((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
344 else if ((c
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
))
348 for (--b
,++s
; n
>0&&b
>0; --n
,--b
,++s
) {
350 if ((c
&(B7
|B6
)) != B7
)
359 static int slen(char *s
) {
360 unsigned char c
= *s
;
364 else if ((c
&(B7
|B6
|B5
)) == (B7
|B6
))
366 else if ((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
372 static void selinit(void) {
378 static inline int selected(int x
, int y
) {
379 if(sel
.ey
== y
&& sel
.by
== y
) {
380 int bx
= MIN(sel
.bx
, sel
.ex
);
381 int ex
= MAX(sel
.bx
, sel
.ex
);
382 return BETWEEN(x
, bx
, ex
);
384 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
385 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
388 static void getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
390 *b
= e
->xbutton
.button
;
392 *x
= e
->xbutton
.x
/xw
.cw
;
393 *y
= e
->xbutton
.y
/xw
.ch
;
394 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
395 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
396 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
397 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
400 static void bpress(XEvent
*e
) {
402 sel
.ex
= sel
.bx
= e
->xbutton
.x
/xw
.cw
;
403 sel
.ey
= sel
.by
= e
->xbutton
.y
/xw
.ch
;
406 static void selcopy() {
408 int ls
, x
, y
, sz
, sl
;
413 sz
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
414 ptr
= str
= malloc(sz
);
415 for(y
= 0; y
< term
.row
; y
++) {
416 for(x
= 0; x
< term
.col
; x
++)
417 if(term
.line
[y
][x
].state
& GLYPH_SET
&& (ls
= selected(x
, y
))) {
418 sl
= slen(term
.line
[y
][x
].c
);
419 memcpy(ptr
, term
.line
[y
][x
].c
, sl
);
430 static void selnotify(XEvent
*e
) {
431 unsigned long nitems
;
432 unsigned long ofs
, rem
;
439 if(XGetWindowProperty(xw
.dis
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
440 False
, AnyPropertyType
, &type
, &format
,
441 &nitems
, &rem
, &data
)) {
442 fprintf(stderr
, "Clipboard allocation failed\n");
445 ttywrite((const char *) data
, nitems
* format
/ 8);
447 /* number of 32-bit chunks returned */
448 ofs
+= nitems
* format
/ 32;
452 static void selpaste() {
453 XConvertSelection(xw
.dis
, XA_PRIMARY
, XA_STRING
, XA_PRIMARY
, xw
.win
, CurrentTime
);
456 static void selrequest(XEvent
*e
)
458 XSelectionRequestEvent
*xsre
;
462 xsre
= (XSelectionRequestEvent
*) e
;
463 xev
.type
= SelectionNotify
;
464 xev
.requestor
= xsre
->requestor
;
465 xev
.selection
= xsre
->selection
;
466 xev
.target
= xsre
->target
;
467 xev
.time
= xsre
->time
;
471 xa_targets
= XInternAtom(xw
.dis
, "TARGETS", 0);
472 if(xsre
->target
== xa_targets
) {
473 /* respond with the supported type */
474 Atom string
= XA_STRING
;
475 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
476 XA_ATOM
, 32, PropModeReplace
,
477 (unsigned char *) &string
, 1);
478 xev
.property
= xsre
->property
;
479 } else if(xsre
->target
== XA_STRING
) {
480 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
481 xsre
->target
, 8, PropModeReplace
,
482 (unsigned char *) sel
.clip
, strlen(sel
.clip
));
483 xev
.property
= xsre
->property
;
486 /* all done, send a notification to the listener */
487 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
488 fprintf(stderr
, "Error sending SelectionNotify event\n");
491 static void xsetsel(char *str
) {
492 /* register the selection for both the clipboard and the primary */
498 XSetSelectionOwner(xw
.dis
, XA_PRIMARY
, xw
.win
, CurrentTime
);
500 clipboard
= XInternAtom(xw
.dis
, "CLIPBOARD", 0);
501 XSetSelectionOwner(xw
.dis
, clipboard
, xw
.win
, CurrentTime
);
506 /* TODO: doubleclick to select word */
507 static void brelease(XEvent
*e
) {
510 getbuttoninfo(e
, &b
, &sel
.ex
, &sel
.ey
);
511 if(sel
.bx
==sel
.ex
&& sel
.by
==sel
.ey
) {
522 static void bmotion(XEvent
*e
) {
524 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
535 for(row
= 0; row
< term
.row
; row
++) {
536 for(col
= 0; col
< term
.col
; col
++) {
537 if(col
== term
.c
.x
&& row
== term
.c
.y
)
540 c
= term
.line
[row
][col
];
541 putchar(c
.state
& GLYPH_SET
? c
.c
: '.');
550 die(const char *errstr
, ...) {
553 va_start(ap
, errstr
);
554 vfprintf(stderr
, errstr
, ap
);
561 char *args
[] = {getenv("SHELL"), "-i", NULL
};
563 args
[0] = opt_cmd
, args
[1] = NULL
;
565 DEFAULT(args
[0], SHELL
);
566 putenv("TERM="TNAME
);
567 execvp(args
[0], args
);
573 if(waitpid(pid
, &stat
, 0) < 0)
574 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
576 exit(WEXITSTATUS(stat
));
585 /* seems to work fine on linux, openbsd and freebsd */
586 struct winsize w
= {term
.row
, term
.col
, 0, 0};
587 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
588 die("openpty failed: %s\n", SERRNO
);
590 switch(pid
= fork()) {
592 die("fork failed\n");
595 setsid(); /* create a new process group */
596 dup2(s
, STDIN_FILENO
);
597 dup2(s
, STDOUT_FILENO
);
598 dup2(s
, STDERR_FILENO
);
599 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
600 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
608 signal(SIGCHLD
, sigchld
);
615 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
617 fprintf(stderr
, "\n");
622 char buf
[BUFSIZ
], *ptr
;
625 static int buflen
= 0;
628 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
629 die("Couldn't read from shell: %s\n", SERRNO
);
632 for(ptr
=buf
; buflen
>=UTF_SIZ
||canstou(ptr
,buflen
); buflen
-=br
) {
638 memcpy(buf
, ptr
, buflen
);
643 ttywrite(const char *s
, size_t n
) {
644 if(write(cmdfd
, s
, n
) == -1)
645 die("write error on tty: %s\n", SERRNO
);
649 ttyresize(int x
, int y
) {
654 w
.ws_xpixel
= w
.ws_ypixel
= 0;
655 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
656 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
663 if(mode
== CURSOR_SAVE
)
665 else if(mode
== CURSOR_LOAD
)
666 term
.c
= c
, tmoveto(c
.x
, c
.y
);
675 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
677 term
.top
= 0, term
.bot
= term
.row
- 1;
678 term
.mode
= MODE_WRAP
;
679 tclearregion(0, 0, term
.col
-1, term
.row
-1);
683 tnew(int col
, int row
) {
684 /* set screen size */
685 term
.row
= row
, term
.col
= col
;
686 term
.line
= malloc(term
.row
* sizeof(Line
));
687 term
.alt
= malloc(term
.row
* sizeof(Line
));
688 for(row
= 0 ; row
< term
.row
; row
++) {
689 term
.line
[row
] = malloc(term
.col
* sizeof(Glyph
));
690 term
.alt
[row
] = malloc(term
.col
* sizeof(Glyph
));
698 Line
* tmp
= term
.line
;
699 term
.line
= term
.alt
;
701 term
.mode
^= MODE_ALTSCREEN
;
705 tscrolldown(int orig
, int n
) {
709 LIMIT(n
, 0, term
.bot
-orig
+1);
711 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
713 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
715 term
.line
[i
] = term
.line
[i
-n
];
716 term
.line
[i
-n
] = temp
;
721 tscrollup(int orig
, int n
) {
724 LIMIT(n
, 0, term
.bot
-orig
+1);
726 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
728 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
730 term
.line
[i
] = term
.line
[i
+n
];
731 term
.line
[i
+n
] = temp
;
736 tnewline(int first_col
) {
739 tscrollup(term
.top
, 1);
742 tmoveto(first_col
? 0 : term
.c
.x
, y
);
748 char *p
= escseq
.buf
;
752 escseq
.priv
= 1, p
++;
754 while(p
< escseq
.buf
+escseq
.len
) {
756 escseq
.arg
[escseq
.narg
] *= 10;
757 escseq
.arg
[escseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
759 if(*p
== ';' && escseq
.narg
+1 < ESC_ARG_SIZ
)
770 tmoveto(int x
, int y
) {
771 LIMIT(x
, 0, term
.col
-1);
772 LIMIT(y
, 0, term
.row
-1);
773 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
780 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
781 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
782 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
786 tclearregion(int x1
, int y1
, int x2
, int y2
) {
790 temp
= x1
, x1
= x2
, x2
= temp
;
792 temp
= y1
, y1
= y2
, y2
= temp
;
794 LIMIT(x1
, 0, term
.col
-1);
795 LIMIT(x2
, 0, term
.col
-1);
796 LIMIT(y1
, 0, term
.row
-1);
797 LIMIT(y2
, 0, term
.row
-1);
799 for(y
= y1
; y
<= y2
; y
++)
800 for(x
= x1
; x
<= x2
; x
++)
801 term
.line
[y
][x
].state
= 0;
806 int src
= term
.c
.x
+ n
;
808 int size
= term
.col
- src
;
810 if(src
>= term
.col
) {
811 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
814 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
815 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
819 tinsertblank(int n
) {
822 int size
= term
.col
- dst
;
824 if(dst
>= term
.col
) {
825 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
828 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
829 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
833 tinsertblankline(int n
) {
834 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
837 tscrolldown(term
.c
.y
, n
);
842 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
845 tscrollup(term
.c
.y
, n
);
849 tsetattr(int *attr
, int l
) {
852 for(i
= 0; i
< l
; i
++) {
855 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
856 term
.c
.attr
.fg
= DefaultFG
;
857 term
.c
.attr
.bg
= DefaultBG
;
860 term
.c
.attr
.mode
|= ATTR_BOLD
;
863 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
866 term
.c
.attr
.mode
|= ATTR_REVERSE
;
869 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
872 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
875 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
878 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
880 if (BETWEEN(attr
[i
], 0, 255))
881 term
.c
.attr
.fg
= attr
[i
];
883 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
886 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
889 term
.c
.attr
.fg
= DefaultFG
;
892 if (i
+ 2 < l
&& attr
[i
+ 1] == 5) {
894 if (BETWEEN(attr
[i
], 0, 255))
895 term
.c
.attr
.bg
= attr
[i
];
897 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
900 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]);
903 term
.c
.attr
.bg
= DefaultBG
;
906 if(BETWEEN(attr
[i
], 30, 37))
907 term
.c
.attr
.fg
= attr
[i
] - 30;
908 else if(BETWEEN(attr
[i
], 40, 47))
909 term
.c
.attr
.bg
= attr
[i
] - 40;
910 else if(BETWEEN(attr
[i
], 90, 97))
911 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
912 else if(BETWEEN(attr
[i
], 100, 107))
913 term
.c
.attr
.fg
= attr
[i
] - 100 + 8;
915 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[i
]), csidump();
923 tsetscroll(int t
, int b
) {
926 LIMIT(t
, 0, term
.row
-1);
927 LIMIT(b
, 0, term
.row
-1);
939 switch(escseq
.mode
) {
942 printf("erresc: unknown csi ");
946 case '@': /* ICH -- Insert <n> blank char */
947 DEFAULT(escseq
.arg
[0], 1);
948 tinsertblank(escseq
.arg
[0]);
950 case 'A': /* CUU -- Cursor <n> Up */
952 DEFAULT(escseq
.arg
[0], 1);
953 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
955 case 'B': /* CUD -- Cursor <n> Down */
956 DEFAULT(escseq
.arg
[0], 1);
957 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
959 case 'C': /* CUF -- Cursor <n> Forward */
961 DEFAULT(escseq
.arg
[0], 1);
962 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
964 case 'D': /* CUB -- Cursor <n> Backward */
965 DEFAULT(escseq
.arg
[0], 1);
966 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
968 case 'E': /* CNL -- Cursor <n> Down and first col */
969 DEFAULT(escseq
.arg
[0], 1);
970 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
972 case 'F': /* CPL -- Cursor <n> Up and first col */
973 DEFAULT(escseq
.arg
[0], 1);
974 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
976 case 'G': /* CHA -- Move to <col> */
977 case '`': /* XXX: HPA -- same? */
978 DEFAULT(escseq
.arg
[0], 1);
979 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
981 case 'H': /* CUP -- Move to <row> <col> */
982 case 'f': /* XXX: HVP -- same? */
983 DEFAULT(escseq
.arg
[0], 1);
984 DEFAULT(escseq
.arg
[1], 1);
985 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
987 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
988 case 'J': /* ED -- Clear screen */
989 switch(escseq
.arg
[0]) {
991 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
992 if(term
.c
.y
< term
.row
-1)
993 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
997 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
998 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1001 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1007 case 'K': /* EL -- Clear line */
1008 switch(escseq
.arg
[0]) {
1010 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1013 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1016 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1020 case 'S': /* SU -- Scroll <n> line up */
1021 DEFAULT(escseq
.arg
[0], 1);
1022 tscrollup(term
.top
, escseq
.arg
[0]);
1024 case 'T': /* SD -- Scroll <n> line down */
1025 DEFAULT(escseq
.arg
[0], 1);
1026 tscrolldown(term
.top
, escseq
.arg
[0]);
1028 case 'L': /* IL -- Insert <n> blank lines */
1029 DEFAULT(escseq
.arg
[0], 1);
1030 tinsertblankline(escseq
.arg
[0]);
1032 case 'l': /* RM -- Reset Mode */
1034 switch(escseq
.arg
[0]) {
1036 term
.mode
&= ~MODE_APPKEYPAD
;
1038 case 5: /* TODO: DECSCNM -- Remove reverse video */
1041 term
.mode
&= ~MODE_WRAP
;
1043 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
1046 term
.mode
&= ~MODE_CRLF
;
1049 term
.c
.state
|= CURSOR_HIDE
;
1051 case 1049: /* = 1047 and 1048 */
1053 if(IS_SET(MODE_ALTSCREEN
)) {
1054 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1057 if(escseq
.arg
[0] == 1047)
1060 tcursor(CURSOR_LOAD
);
1066 switch(escseq
.arg
[0]) {
1068 term
.mode
&= ~MODE_INSERT
;
1075 case 'M': /* DL -- Delete <n> lines */
1076 DEFAULT(escseq
.arg
[0], 1);
1077 tdeleteline(escseq
.arg
[0]);
1079 case 'X': /* ECH -- Erase <n> char */
1080 DEFAULT(escseq
.arg
[0], 1);
1081 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ escseq
.arg
[0], term
.c
.y
);
1083 case 'P': /* DCH -- Delete <n> char */
1084 DEFAULT(escseq
.arg
[0], 1);
1085 tdeletechar(escseq
.arg
[0]);
1087 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
1088 case 'd': /* VPA -- Move to <row> */
1089 DEFAULT(escseq
.arg
[0], 1);
1090 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
1092 case 'h': /* SM -- Set terminal mode */
1094 switch(escseq
.arg
[0]) {
1096 term
.mode
|= MODE_APPKEYPAD
;
1098 case 5: /* DECSCNM -- Reverve video */
1099 /* TODO: set REVERSE on the whole screen (f) */
1102 term
.mode
|= MODE_WRAP
;
1105 term
.mode
|= MODE_CRLF
;
1107 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1108 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
1109 if(escseq
.narg
> 1 && escseq
.arg
[1] != 25)
1112 term
.c
.state
&= ~CURSOR_HIDE
;
1114 case 1049: /* = 1047 and 1048 */
1116 if(IS_SET(MODE_ALTSCREEN
))
1117 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1120 if(escseq
.arg
[0] == 1047)
1123 tcursor(CURSOR_SAVE
);
1125 default: goto unknown
;
1128 switch(escseq
.arg
[0]) {
1130 term
.mode
|= MODE_INSERT
;
1132 default: goto unknown
;
1136 case 'm': /* SGR -- Terminal attribute (color) */
1137 tsetattr(escseq
.arg
, escseq
.narg
);
1139 case 'r': /* DECSTBM -- Set Scrolling Region */
1143 DEFAULT(escseq
.arg
[0], 1);
1144 DEFAULT(escseq
.arg
[1], term
.row
);
1145 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
1149 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1150 tcursor(CURSOR_SAVE
);
1152 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1153 tcursor(CURSOR_LOAD
);
1161 printf("ESC [ %s", escseq
.priv
? "? " : "");
1163 for(i
= 0; i
< escseq
.narg
; i
++)
1164 printf("%d ", escseq
.arg
[i
]);
1166 putchar(escseq
.mode
);
1172 memset(&escseq
, 0, sizeof(escseq
));
1177 int space
= TAB
- term
.c
.x
% TAB
;
1178 tmoveto(term
.c
.x
+ space
, term
.c
.y
);
1184 if(term
.esc
& ESC_START
) {
1185 if(term
.esc
& ESC_CSI
) {
1186 escseq
.buf
[escseq
.len
++] = ascii
;
1187 if(BETWEEN(ascii
, 0x40, 0x7E) || escseq
.len
>= ESC_BUF_SIZ
) {
1189 csiparse(), csihandle();
1191 /* TODO: handle other OSC */
1192 } else if(term
.esc
& ESC_OSC
) {
1195 term
.esc
= ESC_START
| ESC_TITLE
;
1197 } else if(term
.esc
& ESC_TITLE
) {
1198 if(ascii
== '\a' || term
.titlelen
+1 >= ESC_TITLE_SIZ
) {
1200 term
.title
[term
.titlelen
] = '\0';
1201 XStoreName(xw
.dis
, xw
.win
, term
.title
);
1203 term
.title
[term
.titlelen
++] = ascii
;
1205 } else if(term
.esc
& ESC_ALTCHARSET
) {
1207 case '0': /* Line drawing crap */
1208 term
.c
.attr
.mode
|= ATTR_GFX
;
1210 case 'B': /* Back to regular text */
1211 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1214 printf("esc unhandled charset: ESC ( %c\n", ascii
);
1220 term
.esc
|= ESC_CSI
;
1223 term
.esc
|= ESC_OSC
;
1226 term
.esc
|= ESC_ALTCHARSET
;
1228 case 'D': /* IND -- Linefeed */
1229 if(term
.c
.y
== term
.bot
)
1230 tscrollup(term
.top
, 1);
1232 tmoveto(term
.c
.x
, term
.c
.y
+1);
1235 case 'E': /* NEL -- Next line */
1236 tnewline(1); /* always go to first col */
1239 case 'M': /* RI -- Reverse index */
1240 if(term
.c
.y
== term
.top
)
1241 tscrolldown(term
.top
, 1);
1243 tmoveto(term
.c
.x
, term
.c
.y
-1);
1246 case 'c': /* RIS -- Reset to inital state */
1250 case '=': /* DECPAM -- Application keypad */
1251 term
.mode
|= MODE_APPKEYPAD
;
1254 case '>': /* DECPNM -- Normal keypad */
1255 term
.mode
&= ~MODE_APPKEYPAD
;
1258 case '7': /* DECSC -- Save Cursor */
1259 tcursor(CURSOR_SAVE
);
1262 case '8': /* DECRC -- Restore Cursor */
1263 tcursor(CURSOR_LOAD
);
1267 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1268 (unsigned char) ascii
, isprint(ascii
)?ascii
:'.');
1278 tmoveto(term
.c
.x
-1, term
.c
.y
);
1281 tmoveto(0, term
.c
.y
);
1286 /* go to first col if the mode is set */
1287 tnewline(IS_SET(MODE_CRLF
));
1290 if(!(xw
.state
& WIN_FOCUSED
))
1295 term
.esc
= ESC_START
;
1298 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1299 tnewline(1); /* always go to first col */
1301 if(term
.c
.x
+1 < term
.col
)
1302 tmoveto(term
.c
.x
+1, term
.c
.y
);
1304 term
.c
.state
|= CURSOR_WRAPNEXT
;
1311 tresize(int col
, int row
) {
1313 int minrow
= MIN(row
, term
.row
);
1314 int mincol
= MIN(col
, term
.col
);
1315 int slide
= term
.c
.y
- row
+ 1;
1317 if(col
< 1 || row
< 1)
1320 /* free unneeded rows */
1323 /* slide screen to keep cursor where we expect it -
1324 * tscrollup would work here, but we can optimize to
1325 * memmove because we're freeing the earlier lines */
1326 for(/* i = 0 */; i
< slide
; i
++) {
1330 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1331 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1333 for(i
+= row
; i
< term
.row
; i
++) {
1338 /* resize to new height */
1339 term
.line
= realloc(term
.line
, row
* sizeof(Line
));
1340 term
.alt
= realloc(term
.alt
, row
* sizeof(Line
));
1342 /* resize each row to new width, zero-pad if needed */
1343 for(i
= 0; i
< minrow
; i
++) {
1344 term
.line
[i
] = realloc(term
.line
[i
], col
* sizeof(Glyph
));
1345 term
.alt
[i
] = realloc(term
.alt
[i
], col
* sizeof(Glyph
));
1346 for(x
= mincol
; x
< col
; x
++) {
1347 term
.line
[i
][x
].state
= 0;
1348 term
.alt
[i
][x
].state
= 0;
1352 /* allocate any new rows */
1353 for(/* i == minrow */; i
< row
; i
++) {
1354 term
.line
[i
] = calloc(col
, sizeof(Glyph
));
1355 term
.alt
[i
] = calloc(col
, sizeof(Glyph
));
1358 /* update terminal size */
1359 term
.col
= col
, term
.row
= row
;
1360 /* make use of the LIMIT in tmoveto */
1361 tmoveto(term
.c
.x
, term
.c
.y
);
1362 /* reset scrolling region */
1363 tsetscroll(0, row
-1);
1368 xresize(int col
, int row
) {
1374 xw
.bufw
= MAX(1, col
* xw
.cw
);
1375 xw
.bufh
= MAX(1, row
* xw
.ch
);
1376 newbuf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1377 XCopyArea(xw
.dis
, xw
.buf
, newbuf
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, 0, 0);
1378 XFreePixmap(xw
.dis
, xw
.buf
);
1379 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1381 XFillRectangle(xw
.dis
, newbuf
, dc
.gc
, oldw
, 0,
1382 xw
.bufw
-oldw
, MIN(xw
.bufh
, oldh
));
1383 else if(xw
.bufw
< oldw
&& (BORDER
> 0 || xw
.w
> xw
.bufw
))
1384 XClearArea(xw
.dis
, xw
.win
, BORDER
+xw
.bufw
, BORDER
,
1385 xw
.w
-xw
.bufh
-BORDER
, BORDER
+MIN(xw
.bufh
, oldh
),
1388 XFillRectangle(xw
.dis
, newbuf
, dc
.gc
, 0, oldh
,
1389 xw
.bufw
, xw
.bufh
-oldh
);
1390 else if(xw
.bufh
< oldh
&& (BORDER
> 0 || xw
.h
> xw
.bufh
))
1391 XClearArea(xw
.dis
, xw
.win
, BORDER
, BORDER
+xw
.bufh
,
1392 xw
.w
-2*BORDER
, xw
.h
-xw
.bufh
-BORDER
,
1401 unsigned long white
= WhitePixel(xw
.dis
, xw
.scr
);
1403 for(i
= 0; i
< 16; i
++) {
1404 if (!XAllocNamedColor(xw
.dis
, xw
.cmap
, colorname
[i
], &color
, &color
)) {
1406 fprintf(stderr
, "Could not allocate color '%s'\n", colorname
[i
]);
1408 dc
.col
[i
] = color
.pixel
;
1411 /* same colors as xterm */
1412 for(r
= 0; r
< 6; r
++)
1413 for(g
= 0; g
< 6; g
++)
1414 for(b
= 0; b
< 6; b
++) {
1415 color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1416 color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1417 color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1418 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1420 fprintf(stderr
, "Could not allocate color %d\n", i
);
1422 dc
.col
[i
] = color
.pixel
;
1426 for(r
= 0; r
< 24; r
++, i
++) {
1427 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
1428 if (!XAllocColor(xw
.dis
, xw
.cmap
, &color
)) {
1430 fprintf(stderr
, "Could not allocate color %d\n", i
);
1432 dc
.col
[i
] = color
.pixel
;
1437 xclear(int x1
, int y1
, int x2
, int y2
) {
1438 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[DefaultBG
]);
1439 XFillRectangle(xw
.dis
, xw
.buf
, dc
.gc
,
1440 x1
* xw
.cw
, y1
* xw
.ch
,
1441 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
);
1447 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
1448 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
1450 .flags
= PSize
| PResizeInc
| PBaseSize
,
1453 .height_inc
= xw
.ch
,
1455 .base_height
= 2*BORDER
,
1456 .base_width
= 2*BORDER
,
1458 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
1462 xsetfontinfo(FontInfo
*fi
)
1464 XFontStruct
**xfonts
;
1473 fnum
= XFontsOfFontSet(fi
->fs
, &xfonts
, &fontnames
);
1474 for(i
=0; i
<fnum
; i
++,xfonts
++,fontnames
++) {
1476 if(fi
->ascent
< (*xfonts
)->ascent
)
1477 fi
->ascent
= (*xfonts
)->ascent
;
1478 if(fi
->descent
< (*xfonts
)->descent
)
1479 fi
->descent
= (*xfonts
)->descent
;
1480 if(fi
->rbearing
< (*xfonts
)->max_bounds
.rbearing
)
1481 fi
->rbearing
= (*xfonts
)->max_bounds
.rbearing
;
1482 if(fi
->lbearing
< (*xfonts
)->min_bounds
.lbearing
)
1483 fi
->lbearing
= (*xfonts
)->min_bounds
.lbearing
;
1489 XSetWindowAttributes attrs
;
1494 if(!(xw
.dis
= XOpenDisplay(NULL
)))
1495 die("Can't open display\n");
1496 xw
.scr
= XDefaultScreen(xw
.dis
);
1499 if ((dc
.font
.fs
= XCreateFontSet(xw
.dis
, FONT
, &mc
, &nmc
, &ds
)) == NULL
||
1500 (dc
.bfont
.fs
= XCreateFontSet(xw
.dis
, BOLDFONT
, &mc
, &nmc
, &ds
)) == NULL
)
1501 die("Can't load font %s\n", dc
.font
.fs
? BOLDFONT
: FONT
);
1502 xsetfontinfo(&dc
.font
);
1503 xsetfontinfo(&dc
.bfont
);
1505 /* XXX: Assuming same size for bold font */
1506 xw
.cw
= dc
.font
.rbearing
- dc
.font
.lbearing
;
1507 xw
.ch
= dc
.font
.ascent
+ dc
.font
.descent
;
1510 xw
.cmap
= XDefaultColormap(xw
.dis
, xw
.scr
);
1513 /* window - default size */
1514 xw
.bufh
= 24 * xw
.ch
;
1515 xw
.bufw
= 80 * xw
.cw
;
1516 xw
.h
= xw
.bufh
+ 2*BORDER
;
1517 xw
.w
= xw
.bufw
+ 2*BORDER
;
1519 attrs
.background_pixel
= dc
.col
[DefaultBG
];
1520 attrs
.border_pixel
= dc
.col
[DefaultBG
];
1521 attrs
.bit_gravity
= NorthWestGravity
;
1522 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1523 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1524 | PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1525 attrs
.colormap
= xw
.cmap
;
1527 xw
.win
= XCreateWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
1528 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dis
, xw
.scr
), InputOutput
,
1529 XDefaultVisual(xw
.dis
, xw
.scr
),
1530 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
1533 xw
.buf
= XCreatePixmap(xw
.dis
, xw
.win
, xw
.bufw
, xw
.bufh
, XDefaultDepth(xw
.dis
, xw
.scr
));
1537 xw
.xim
= XOpenIM(xw
.dis
, NULL
, NULL
, NULL
);
1538 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1539 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1540 XNFocusWindow
, xw
.win
, NULL
);
1542 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, 0, NULL
);
1544 XMapWindow(xw
.dis
, xw
.win
);
1546 XStoreName(xw
.dis
, xw
.win
, opt_title
? opt_title
: "st");
1551 xdraws(char *s
, Glyph base
, int x
, int y
, int cl
, int sl
) {
1552 unsigned long xfg
, xbg
;
1553 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+ dc
.font
.ascent
, width
= cl
*xw
.cw
;
1556 if(base
.mode
& ATTR_REVERSE
)
1557 xfg
= dc
.col
[base
.bg
], xbg
= dc
.col
[base
.fg
];
1559 xfg
= dc
.col
[base
.fg
], xbg
= dc
.col
[base
.bg
];
1561 XSetBackground(xw
.dis
, dc
.gc
, xbg
);
1562 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
1564 if(base
.mode
& ATTR_GFX
)
1565 for(i
= 0; i
< cl
; i
++) {
1566 char c
= gfx
[(unsigned int)s
[i
] % 256];
1569 else if(s
[i
] > 0x5f)
1573 XmbDrawImageString(xw
.dis
, xw
.buf
, base
.mode
& ATTR_BOLD
? dc
.bfont
.fs
: dc
.font
.fs
,
1574 dc
.gc
, winx
, winy
, s
, sl
);
1576 if(base
.mode
& ATTR_UNDERLINE
)
1577 XDrawLine(xw
.dis
, xw
.buf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
1582 static int oldx
= 0;
1583 static int oldy
= 0;
1585 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
1587 LIMIT(oldx
, 0, term
.col
-1);
1588 LIMIT(oldy
, 0, term
.row
-1);
1590 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
1591 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
1593 /* remove the old cursor */
1594 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
1595 sl
= slen(term
.line
[oldy
][oldx
].c
);
1596 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
1598 xclear(oldx
, oldy
, oldx
, oldy
);
1600 /* draw the new one */
1601 if(!(term
.c
.state
& CURSOR_HIDE
) && (xw
.state
& WIN_FOCUSED
)) {
1603 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
1604 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
1609 /* basic drawing routines */
1611 xdrawc(int x
, int y
, Glyph g
) {
1613 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
1614 XSetBackground(xw
.dis
, dc
.gc
, dc
.col
[g
.bg
]);
1615 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[g
.fg
]);
1616 XmbDrawImageString(xw
.dis
, xw
.buf
, g
.mode
&ATTR_BOLD
?dc
.bfont
.fs
:dc
.font
.fs
,
1617 dc
.gc
, r
.x
, r
.y
+dc
.font
.ascent
, g
.c
, sl
);
1624 xclear(0, 0, term
.col
-1, term
.row
-1);
1625 for(y
= 0; y
< term
.row
; y
++)
1626 for(x
= 0; x
< term
.col
; x
++)
1627 if(term
.line
[y
][x
].state
& GLYPH_SET
)
1628 xdrawc(x
, y
, term
.line
[y
][x
]);
1631 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1636 /* optimized drawing routine */
1638 draw(int redraw_all
) {
1639 int ic
, ib
, x
, y
, ox
, sl
;
1641 char buf
[DRAW_BUF_SIZ
];
1643 if(!(xw
.state
& WIN_VISIBLE
))
1646 xclear(0, 0, term
.col
-1, term
.row
-1);
1647 for(y
= 0; y
< term
.row
; y
++) {
1648 base
= term
.line
[y
][0];
1650 for(x
= 0; x
< term
.col
; x
++) {
1651 new = term
.line
[y
][x
];
1652 if(sel
.bx
!=-1 && *(new.c
) && selected(x
, y
))
1653 new.mode
^= ATTR_REVERSE
;
1654 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
1655 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
1656 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1659 if(new.state
& GLYPH_SET
) {
1665 memcpy(buf
+ib
, new.c
, sl
);
1671 xdraws(buf
, base
, ox
, y
, ic
, ib
);
1674 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.bufw
, xw
.bufh
, BORDER
, BORDER
);
1680 expose(XEvent
*ev
) {
1681 XExposeEvent
*e
= &ev
->xexpose
;
1682 if(xw
.state
& WIN_REDRAW
) {
1684 xw
.state
&= ~WIN_REDRAW
;
1685 draw(SCREEN_REDRAW
);
1688 XCopyArea(xw
.dis
, xw
.buf
, xw
.win
, dc
.gc
, e
->x
-BORDER
, e
->y
-BORDER
,
1689 e
->width
, e
->height
, e
->x
, e
->y
);
1693 visibility(XEvent
*ev
) {
1694 XVisibilityEvent
*e
= &ev
->xvisibility
;
1695 if(e
->state
== VisibilityFullyObscured
)
1696 xw
.state
&= ~WIN_VISIBLE
;
1697 else if(!(xw
.state
& WIN_VISIBLE
))
1698 /* need a full redraw for next Expose, not just a buf copy */
1699 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
1704 xw
.state
&= ~WIN_VISIBLE
;
1708 xseturgency(int add
) {
1709 XWMHints
*h
= XGetWMHints(xw
.dis
, xw
.win
);
1710 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
1711 XSetWMHints(xw
.dis
, xw
.win
, h
);
1717 if(ev
->type
== FocusIn
) {
1718 xw
.state
|= WIN_FOCUSED
;
1721 xw
.state
&= ~WIN_FOCUSED
;
1722 draw(SCREEN_UPDATE
);
1728 for(i
= 0; i
< LEN(key
); i
++)
1730 return (char*)key
[i
].s
;
1735 kpress(XEvent
*ev
) {
1736 XKeyEvent
*e
= &ev
->xkey
;
1745 meta
= e
->state
& Mod1Mask
;
1746 shift
= e
->state
& ShiftMask
;
1747 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
1749 /* 1. custom keys from config.h */
1750 if((customkey
= kmap(ksym
)))
1751 ttywrite(customkey
, strlen(customkey
));
1752 /* 2. hardcoded (overrides X lookup) */
1759 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', "DACB"[ksym
- XK_Left
]);
1767 if(IS_SET(MODE_CRLF
))
1768 ttywrite("\r\n", 2);
1775 buf
[sizeof(buf
)-1] = '\0';
1776 if(meta
&& len
== 1)
1777 ttywrite("\033", 1);
1779 } else /* 4. nothing to send */
1780 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1789 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
1792 xw
.w
= e
->xconfigure
.width
;
1793 xw
.h
= e
->xconfigure
.height
;
1794 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
1795 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
1796 if(col
== term
.col
&& row
== term
.row
)
1798 if(tresize(col
, row
))
1799 draw(SCREEN_REDRAW
);
1800 ttyresize(col
, row
);
1808 int xfd
= XConnectionNumber(xw
.dis
);
1812 FD_SET(cmdfd
, &rfd
);
1814 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) < 0) {
1817 die("select failed: %s\n", SERRNO
);
1819 if(FD_ISSET(cmdfd
, &rfd
)) {
1821 draw(SCREEN_UPDATE
);
1823 while(XPending(xw
.dis
)) {
1824 XNextEvent(xw
.dis
, &ev
);
1825 if (XFilterEvent(&ev
, xw
.win
))
1827 if(handler
[ev
.type
])
1828 (handler
[ev
.type
])(&ev
);
1834 main(int argc
, char *argv
[]) {
1837 for(i
= 1; i
< argc
; i
++) {
1838 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
1840 if(++i
< argc
) opt_title
= argv
[i
];
1843 if(++i
< argc
) opt_class
= argv
[i
];
1846 if(++i
< argc
) opt_cmd
= argv
[i
];
1853 setlocale(LC_CTYPE
, "");