Xinqi Bao's Git

Adding a more flexible fontstring handling, shortcuts and a zoom function.
[st.git] / st.c
1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
3 #include <ctype.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <locale.h>
8 #include <pwd.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <signal.h>
15 #include <sys/ioctl.h>
16 #include <sys/select.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <X11/Xatom.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/cursorfont.h>
27 #include <X11/keysym.h>
28 #include <X11/extensions/Xdbe.h>
29 #include <X11/Xft/Xft.h>
30 #include <fontconfig/fontconfig.h>
31 #define Glyph Glyph_
32 #define Font Font_
33
34 #if defined(__linux)
35 #include <pty.h>
36 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
37 #include <util.h>
38 #elif defined(__FreeBSD__) || defined(__DragonFly__)
39 #include <libutil.h>
40 #endif
41
42 #define USAGE \
43 "st " VERSION " (c) 2010-2012 st engineers\n" \
44 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
45 " [-t title] [-w windowid] [-e command ...]\n"
46
47 /* XEMBED messages */
48 #define XEMBED_FOCUS_IN 4
49 #define XEMBED_FOCUS_OUT 5
50
51 /* Arbitrary sizes */
52 #define ESC_BUF_SIZ 256
53 #define ESC_ARG_SIZ 16
54 #define STR_BUF_SIZ 256
55 #define STR_ARG_SIZ 16
56 #define DRAW_BUF_SIZ 20*1024
57 #define UTF_SIZ 4
58 #define XK_NO_MOD UINT_MAX
59 #define XK_ANY_MOD 0
60
61 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
62
63 /* macros */
64 #define CLEANMASK(mask) (mask & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
65 #define SERRNO strerror(errno)
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #define MAX(a, b) ((a) < (b) ? (b) : (a))
68 #define LEN(a) (sizeof(a) / sizeof(a[0]))
69 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
70 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
71 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
72 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
73 #define IS_SET(flag) (term.mode & (flag))
74 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
75 #define X2COL(x) (((x) - BORDER)/xw.cw)
76 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
77
78 #define VT102ID "\033[?6c"
79
80 enum glyph_attribute {
81 ATTR_NULL = 0,
82 ATTR_REVERSE = 1,
83 ATTR_UNDERLINE = 2,
84 ATTR_BOLD = 4,
85 ATTR_GFX = 8,
86 ATTR_ITALIC = 16,
87 ATTR_BLINK = 32,
88 };
89
90 enum cursor_movement {
91 CURSOR_UP,
92 CURSOR_DOWN,
93 CURSOR_LEFT,
94 CURSOR_RIGHT,
95 CURSOR_SAVE,
96 CURSOR_LOAD
97 };
98
99 enum cursor_state {
100 CURSOR_DEFAULT = 0,
101 CURSOR_HIDE = 1,
102 CURSOR_WRAPNEXT = 2
103 };
104
105 enum glyph_state {
106 GLYPH_SET = 1,
107 GLYPH_DIRTY = 2
108 };
109
110 enum term_mode {
111 MODE_WRAP = 1,
112 MODE_INSERT = 2,
113 MODE_APPKEYPAD = 4,
114 MODE_ALTSCREEN = 8,
115 MODE_CRLF = 16,
116 MODE_MOUSEBTN = 32,
117 MODE_MOUSEMOTION = 64,
118 MODE_MOUSE = 32|64,
119 MODE_REVERSE = 128,
120 MODE_KBDLOCK = 256
121 };
122
123 enum escape_state {
124 ESC_START = 1,
125 ESC_CSI = 2,
126 ESC_STR = 4, /* DSC, OSC, PM, APC */
127 ESC_ALTCHARSET = 8,
128 ESC_STR_END = 16, /* a final string was encountered */
129 ESC_TEST = 32, /* Enter in test mode */
130 };
131
132 enum window_state {
133 WIN_VISIBLE = 1,
134 WIN_REDRAW = 2,
135 WIN_FOCUSED = 4
136 };
137
138 /* bit macro */
139 #undef B0
140 enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
141
142 typedef unsigned char uchar;
143 typedef unsigned int uint;
144 typedef unsigned long ulong;
145 typedef unsigned short ushort;
146
147 typedef struct {
148 char c[UTF_SIZ]; /* character code */
149 uchar mode; /* attribute flags */
150 ushort fg; /* foreground */
151 ushort bg; /* background */
152 uchar state; /* state flags */
153 } Glyph;
154
155 typedef Glyph* Line;
156
157 typedef struct {
158 Glyph attr; /* current char attributes */
159 int x;
160 int y;
161 char state;
162 } TCursor;
163
164 /* CSI Escape sequence structs */
165 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
166 typedef struct {
167 char buf[ESC_BUF_SIZ]; /* raw string */
168 int len; /* raw string length */
169 char priv;
170 int arg[ESC_ARG_SIZ];
171 int narg; /* nb of args */
172 char mode;
173 } CSIEscape;
174
175 /* STR Escape sequence structs */
176 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
177 typedef struct {
178 char type; /* ESC type ... */
179 char buf[STR_BUF_SIZ]; /* raw string */
180 int len; /* raw string length */
181 char *args[STR_ARG_SIZ];
182 int narg; /* nb of args */
183 } STREscape;
184
185 /* Internal representation of the screen */
186 typedef struct {
187 int row; /* nb row */
188 int col; /* nb col */
189 Line *line; /* screen */
190 Line *alt; /* alternate screen */
191 bool *dirty; /* dirtyness of lines */
192 TCursor c; /* cursor */
193 int top; /* top scroll limit */
194 int bot; /* bottom scroll limit */
195 int mode; /* terminal mode flags */
196 int esc; /* escape state flags */
197 bool *tabs;
198 } Term;
199
200 /* Purely graphic info */
201 typedef struct {
202 Display* dpy;
203 Colormap cmap;
204 Window win;
205 XdbeBackBuffer buf;
206 Atom xembed, wmdeletewin;
207 XIM xim;
208 XIC xic;
209 XftDraw *xft_draw;
210 Visual *vis;
211 int scr;
212 bool isfixed; /* is fixed geometry? */
213 int fx, fy, fw, fh; /* fixed geometry */
214 int tw, th; /* tty width and height */
215 int w; /* window width */
216 int h; /* window height */
217 int ch; /* char height */
218 int cw; /* char width */
219 char state; /* focus, redraw, visible */
220 } XWindow;
221
222 typedef struct {
223 KeySym k;
224 uint mask;
225 char s[ESC_BUF_SIZ];
226 } Key;
227
228 /* TODO: use better name for vars... */
229 typedef struct {
230 int mode;
231 int bx, by;
232 int ex, ey;
233 struct {
234 int x, y;
235 } b, e;
236 char *clip;
237 Atom xtarget;
238 bool alt;
239 struct timeval tclick1;
240 struct timeval tclick2;
241 } Selection;
242
243 typedef union {
244 int i;
245 unsigned int ui;
246 float f;
247 const void *v;
248 } Arg;
249
250 typedef struct {
251 unsigned int mod;
252 KeySym keysym;
253 void (*func)(const Arg *);
254 const Arg arg;
255 } Shortcut;
256
257 /* function definitions used in config.h */
258 static void xzoom(const Arg *);
259
260 /* Config.h for applying patches and the configuration. */
261 #include "config.h"
262
263 /* Font structure */
264 typedef struct {
265 int height;
266 int width;
267 int ascent;
268 int descent;
269 short lbearing;
270 short rbearing;
271 XftFont *xft_set;
272 } Font;
273
274 /* Drawing Context */
275 typedef struct {
276 XftColor xft_col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
277 GC gc;
278 Font font, bfont, ifont, ibfont;
279 } DC;
280
281 static void die(const char *, ...);
282 static void draw(void);
283 static void redraw(void);
284 static void drawregion(int, int, int, int);
285 static void execsh(void);
286 static void sigchld(int);
287 static void run(void);
288
289 static void csidump(void);
290 static void csihandle(void);
291 static void csiparse(void);
292 static void csireset(void);
293 static void strdump(void);
294 static void strhandle(void);
295 static void strparse(void);
296 static void strreset(void);
297
298 static void tclearregion(int, int, int, int);
299 static void tcursor(int);
300 static void tdeletechar(int);
301 static void tdeleteline(int);
302 static void tinsertblank(int);
303 static void tinsertblankline(int);
304 static void tmoveto(int, int);
305 static void tnew(int, int);
306 static void tnewline(int);
307 static void tputtab(bool);
308 static void tputc(char *, int);
309 static void treset(void);
310 static int tresize(int, int);
311 static void tscrollup(int, int);
312 static void tscrolldown(int, int);
313 static void tsetattr(int*, int);
314 static void tsetchar(char *, Glyph *, int, int);
315 static void tsetscroll(int, int);
316 static void tswapscreen(void);
317 static void tsetdirt(int, int);
318 static void tsetmode(bool, bool, int *, int);
319 static void tfulldirt(void);
320
321 static void ttynew(void);
322 static void ttyread(void);
323 static void ttyresize(void);
324 static void ttywrite(const char *, size_t);
325
326 static void xdraws(char *, Glyph, int, int, int, int);
327 static void xhints(void);
328 static void xclear(int, int, int, int);
329 static void xdrawcursor(void);
330 static void xinit(void);
331 static void xloadcols(void);
332 static void xresettitle(void);
333 static void xseturgency(int);
334 static void xsetsel(char*);
335 static void xtermclear(int, int, int, int);
336 static void xresize(int, int);
337
338 static void expose(XEvent *);
339 static void visibility(XEvent *);
340 static void unmap(XEvent *);
341 static char *kmap(KeySym, uint);
342 static void kpress(XEvent *);
343 static void cmessage(XEvent *);
344 static void cresize(int width, int height);
345 static void resize(XEvent *);
346 static void focus(XEvent *);
347 static void brelease(XEvent *);
348 static void bpress(XEvent *);
349 static void bmotion(XEvent *);
350 static void selnotify(XEvent *);
351 static void selclear(XEvent *);
352 static void selrequest(XEvent *);
353
354 static void selinit(void);
355 static inline bool selected(int, int);
356 static void selcopy(void);
357 static void selpaste(void);
358 static void selscroll(int, int);
359
360 static int utf8decode(char *, long *);
361 static int utf8encode(long *, char *);
362 static int utf8size(char *);
363 static int isfullutf8(char *, int);
364
365 static ssize_t xwrite(int, char *, size_t);
366 static void *xmalloc(size_t);
367 static void *xrealloc(void *, size_t);
368 static void *xcalloc(size_t nmemb, size_t size);
369
370 static void (*handler[LASTEvent])(XEvent *) = {
371 [KeyPress] = kpress,
372 [ClientMessage] = cmessage,
373 [ConfigureNotify] = resize,
374 [VisibilityNotify] = visibility,
375 [UnmapNotify] = unmap,
376 [Expose] = expose,
377 [FocusIn] = focus,
378 [FocusOut] = focus,
379 [MotionNotify] = bmotion,
380 [ButtonPress] = bpress,
381 [ButtonRelease] = brelease,
382 [SelectionClear] = selclear,
383 [SelectionNotify] = selnotify,
384 [SelectionRequest] = selrequest,
385 };
386
387 /* Globals */
388 static DC dc;
389 static XWindow xw;
390 static Term term;
391 static CSIEscape csiescseq;
392 static STREscape strescseq;
393 static int cmdfd;
394 static pid_t pid;
395 static Selection sel;
396 static int iofd = -1;
397 static char **opt_cmd = NULL;
398 static char *opt_io = NULL;
399 static char *opt_title = NULL;
400 static char *opt_embed = NULL;
401 static char *opt_class = NULL;
402 static char *opt_font = NULL;
403
404 static char *usedfont = NULL;
405 static int usedfontsize = 0;
406
407 ssize_t
408 xwrite(int fd, char *s, size_t len) {
409 size_t aux = len;
410
411 while(len > 0) {
412 ssize_t r = write(fd, s, len);
413 if(r < 0)
414 return r;
415 len -= r;
416 s += r;
417 }
418 return aux;
419 }
420
421 void *
422 xmalloc(size_t len) {
423 void *p = malloc(len);
424
425 if(!p)
426 die("Out of memory\n");
427
428 return p;
429 }
430
431 void *
432 xrealloc(void *p, size_t len) {
433 if((p = realloc(p, len)) == NULL)
434 die("Out of memory\n");
435
436 return p;
437 }
438
439 void *
440 xcalloc(size_t nmemb, size_t size) {
441 void *p = calloc(nmemb, size);
442
443 if(!p)
444 die("Out of memory\n");
445
446 return p;
447 }
448
449 int
450 utf8decode(char *s, long *u) {
451 uchar c;
452 int i, n, rtn;
453
454 rtn = 1;
455 c = *s;
456 if(~c & B7) { /* 0xxxxxxx */
457 *u = c;
458 return rtn;
459 } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
460 *u = c&(B4|B3|B2|B1|B0);
461 n = 1;
462 } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
463 *u = c&(B3|B2|B1|B0);
464 n = 2;
465 } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
466 *u = c & (B2|B1|B0);
467 n = 3;
468 } else {
469 goto invalid;
470 }
471
472 for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
473 c = *s;
474 if((c & (B7|B6)) != B7) /* 10xxxxxx */
475 goto invalid;
476 *u <<= 6;
477 *u |= c & (B5|B4|B3|B2|B1|B0);
478 }
479
480 if((n == 1 && *u < 0x80) ||
481 (n == 2 && *u < 0x800) ||
482 (n == 3 && *u < 0x10000) ||
483 (*u >= 0xD800 && *u <= 0xDFFF)) {
484 goto invalid;
485 }
486
487 return rtn;
488 invalid:
489 *u = 0xFFFD;
490
491 return rtn;
492 }
493
494 int
495 utf8encode(long *u, char *s) {
496 uchar *sp;
497 ulong uc;
498 int i, n;
499
500 sp = (uchar *)s;
501 uc = *u;
502 if(uc < 0x80) {
503 *sp = uc; /* 0xxxxxxx */
504 return 1;
505 } else if(*u < 0x800) {
506 *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
507 n = 1;
508 } else if(uc < 0x10000) {
509 *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
510 n = 2;
511 } else if(uc <= 0x10FFFF) {
512 *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
513 n = 3;
514 } else {
515 goto invalid;
516 }
517
518 for(i=n,++sp; i>0; --i,++sp)
519 *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
520
521 return n+1;
522 invalid:
523 /* U+FFFD */
524 *s++ = '\xEF';
525 *s++ = '\xBF';
526 *s = '\xBD';
527
528 return 3;
529 }
530
531 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
532 UTF-8 otherwise return 0 */
533 int
534 isfullutf8(char *s, int b) {
535 uchar *c1, *c2, *c3;
536
537 c1 = (uchar *)s;
538 c2 = (uchar *)++s;
539 c3 = (uchar *)++s;
540 if(b < 1) {
541 return 0;
542 } else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) {
543 return 0;
544 } else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
545 ((b == 1) ||
546 ((b == 2) && (*c2&(B7|B6)) == B7))) {
547 return 0;
548 } else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
549 ((b == 1) ||
550 ((b == 2) && (*c2&(B7|B6)) == B7) ||
551 ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7))) {
552 return 0;
553 } else {
554 return 1;
555 }
556 }
557
558 int
559 utf8size(char *s) {
560 uchar c = *s;
561
562 if(~c&B7) {
563 return 1;
564 } else if((c&(B7|B6|B5)) == (B7|B6)) {
565 return 2;
566 } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) {
567 return 3;
568 } else {
569 return 4;
570 }
571 }
572
573 void
574 selinit(void) {
575 memset(&sel.tclick1, 0, sizeof(sel.tclick1));
576 memset(&sel.tclick2, 0, sizeof(sel.tclick2));
577 sel.mode = 0;
578 sel.bx = -1;
579 sel.clip = NULL;
580 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
581 if(sel.xtarget == None)
582 sel.xtarget = XA_STRING;
583 }
584
585 static inline bool
586 selected(int x, int y) {
587 int bx, ex;
588
589 if(sel.ey == y && sel.by == y) {
590 bx = MIN(sel.bx, sel.ex);
591 ex = MAX(sel.bx, sel.ex);
592 return BETWEEN(x, bx, ex);
593 }
594
595 return ((sel.b.y < y && y < sel.e.y)
596 || (y == sel.e.y && x <= sel.e.x))
597 || (y == sel.b.y && x >= sel.b.x
598 && (x <= sel.e.x || sel.b.y != sel.e.y));
599 }
600
601 void
602 getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
603 if(b)
604 *b = e->xbutton.button;
605
606 *x = X2COL(e->xbutton.x);
607 *y = Y2ROW(e->xbutton.y);
608 sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
609 sel.b.y = MIN(sel.by, sel.ey);
610 sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
611 sel.e.y = MAX(sel.by, sel.ey);
612 }
613
614 void
615 mousereport(XEvent *e) {
616 int x = X2COL(e->xbutton.x);
617 int y = Y2ROW(e->xbutton.y);
618 int button = e->xbutton.button;
619 int state = e->xbutton.state;
620 char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
621 static int ob, ox, oy;
622
623 /* from urxvt */
624 if(e->xbutton.type == MotionNotify) {
625 if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
626 return;
627 button = ob + 32;
628 ox = x, oy = y;
629 } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
630 button = 3;
631 } else {
632 button -= Button1;
633 if(button >= 3)
634 button += 64 - 3;
635 if(e->xbutton.type == ButtonPress) {
636 ob = button;
637 ox = x, oy = y;
638 }
639 }
640
641 buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
642 + (state & Mod4Mask ? 8 : 0)
643 + (state & ControlMask ? 16 : 0);
644
645 ttywrite(buf, sizeof(buf));
646 }
647
648 void
649 bpress(XEvent *e) {
650 if(IS_SET(MODE_MOUSE)) {
651 mousereport(e);
652 } else if(e->xbutton.button == Button1) {
653 if(sel.bx != -1) {
654 sel.bx = -1;
655 tsetdirt(sel.b.y, sel.e.y);
656 draw();
657 }
658 sel.mode = 1;
659 sel.ex = sel.bx = X2COL(e->xbutton.x);
660 sel.ey = sel.by = Y2ROW(e->xbutton.y);
661 }
662 }
663
664 void
665 selcopy(void) {
666 char *str, *ptr;
667 int x, y, bufsize, is_selected = 0, size;
668 Glyph *gp;
669
670 if(sel.bx == -1) {
671 str = NULL;
672 } else {
673 bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
674 ptr = str = xmalloc(bufsize);
675
676 /* append every set & selected glyph to the selection */
677 for(y = 0; y < term.row; y++) {
678 for(x = 0; x < term.col; x++) {
679 gp = &term.line[y][x];
680
681 if(!(is_selected = selected(x, y))
682 || !(gp->state & GLYPH_SET)) {
683 continue;
684 }
685 size = utf8size(gp->c);
686 memcpy(ptr, gp->c, size);
687 ptr += size;
688 }
689 /* \n at the end of every selected line except for the last one */
690 if(is_selected && y < sel.e.y)
691 *ptr++ = '\n';
692 }
693 *ptr = 0;
694 }
695 sel.alt = IS_SET(MODE_ALTSCREEN);
696 xsetsel(str);
697 }
698
699 void
700 selnotify(XEvent *e) {
701 ulong nitems, ofs, rem;
702 int format;
703 uchar *data;
704 Atom type;
705
706 ofs = 0;
707 do {
708 if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
709 False, AnyPropertyType, &type, &format,
710 &nitems, &rem, &data)) {
711 fprintf(stderr, "Clipboard allocation failed\n");
712 return;
713 }
714 ttywrite((const char *) data, nitems * format / 8);
715 XFree(data);
716 /* number of 32-bit chunks returned */
717 ofs += nitems * format / 32;
718 } while(rem > 0);
719 }
720
721 void
722 selpaste(void) {
723 XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
724 xw.win, CurrentTime);
725 }
726
727 void selclear(XEvent *e) {
728 if(sel.bx == -1)
729 return;
730 sel.bx = -1;
731 tsetdirt(sel.b.y, sel.e.y);
732 }
733
734 void
735 selrequest(XEvent *e) {
736 XSelectionRequestEvent *xsre;
737 XSelectionEvent xev;
738 Atom xa_targets, string;
739
740 xsre = (XSelectionRequestEvent *) e;
741 xev.type = SelectionNotify;
742 xev.requestor = xsre->requestor;
743 xev.selection = xsre->selection;
744 xev.target = xsre->target;
745 xev.time = xsre->time;
746 /* reject */
747 xev.property = None;
748
749 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
750 if(xsre->target == xa_targets) {
751 /* respond with the supported type */
752 string = sel.xtarget;
753 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
754 XA_ATOM, 32, PropModeReplace,
755 (uchar *) &string, 1);
756 xev.property = xsre->property;
757 } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
758 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
759 xsre->target, 8, PropModeReplace,
760 (uchar *) sel.clip, strlen(sel.clip));
761 xev.property = xsre->property;
762 }
763
764 /* all done, send a notification to the listener */
765 if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
766 fprintf(stderr, "Error sending SelectionNotify event\n");
767 }
768
769 void
770 xsetsel(char *str) {
771 /* register the selection for both the clipboard and the primary */
772 Atom clipboard;
773
774 free(sel.clip);
775 sel.clip = str;
776
777 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
778
779 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
780 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
781 }
782
783 void
784 brelease(XEvent *e) {
785 struct timeval now;
786
787 if(IS_SET(MODE_MOUSE)) {
788 mousereport(e);
789 return;
790 }
791
792 if(e->xbutton.button == Button2) {
793 selpaste();
794 } else if(e->xbutton.button == Button1) {
795 sel.mode = 0;
796 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
797 term.dirty[sel.ey] = 1;
798 if(sel.bx == sel.ex && sel.by == sel.ey) {
799 sel.bx = -1;
800 gettimeofday(&now, NULL);
801
802 if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
803 /* triple click on the line */
804 sel.b.x = sel.bx = 0;
805 sel.e.x = sel.ex = term.col;
806 sel.b.y = sel.e.y = sel.ey;
807 selcopy();
808 } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
809 /* double click to select word */
810 sel.bx = sel.ex;
811 while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
812 term.line[sel.ey][sel.bx-1].c[0] != ' ') {
813 sel.bx--;
814 }
815 sel.b.x = sel.bx;
816 while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
817 term.line[sel.ey][sel.ex+1].c[0] != ' ') {
818 sel.ex++;
819 }
820 sel.e.x = sel.ex;
821 sel.b.y = sel.e.y = sel.ey;
822 selcopy();
823 }
824 } else {
825 selcopy();
826 }
827 }
828
829 memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
830 gettimeofday(&sel.tclick1, NULL);
831 }
832
833 void
834 bmotion(XEvent *e) {
835 int starty, endy, oldey, oldex;
836
837 if(IS_SET(MODE_MOUSE)) {
838 mousereport(e);
839 return;
840 }
841
842 if(sel.mode) {
843 oldey = sel.ey;
844 oldex = sel.ex;
845 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
846
847 if(oldey != sel.ey || oldex != sel.ex) {
848 starty = MIN(oldey, sel.ey);
849 endy = MAX(oldey, sel.ey);
850 tsetdirt(starty, endy);
851 }
852 }
853 }
854
855 void
856 die(const char *errstr, ...) {
857 va_list ap;
858
859 va_start(ap, errstr);
860 vfprintf(stderr, errstr, ap);
861 va_end(ap);
862 exit(EXIT_FAILURE);
863 }
864
865 void
866 execsh(void) {
867 char **args;
868 char *envshell = getenv("SHELL");
869 const struct passwd *pass = getpwuid(getuid());
870 char buf[sizeof(long) * 8 + 1];
871
872 unsetenv("COLUMNS");
873 unsetenv("LINES");
874 unsetenv("TERMCAP");
875
876 if(pass) {
877 setenv("LOGNAME", pass->pw_name, 1);
878 setenv("USER", pass->pw_name, 1);
879 setenv("SHELL", pass->pw_shell, 0);
880 setenv("HOME", pass->pw_dir, 0);
881 }
882
883 snprintf(buf, sizeof(buf), "%lu", xw.win);
884 setenv("WINDOWID", buf, 1);
885
886 signal(SIGCHLD, SIG_DFL);
887 signal(SIGHUP, SIG_DFL);
888 signal(SIGINT, SIG_DFL);
889 signal(SIGQUIT, SIG_DFL);
890 signal(SIGTERM, SIG_DFL);
891 signal(SIGALRM, SIG_DFL);
892
893 DEFAULT(envshell, SHELL);
894 putenv("TERM="TNAME);
895 args = opt_cmd ? opt_cmd : (char *[]){envshell, "-i", NULL};
896 execvp(args[0], args);
897 exit(EXIT_FAILURE);
898 }
899
900 void
901 sigchld(int a) {
902 int stat = 0;
903
904 if(waitpid(pid, &stat, 0) < 0)
905 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
906
907 if(WIFEXITED(stat)) {
908 exit(WEXITSTATUS(stat));
909 } else {
910 exit(EXIT_FAILURE);
911 }
912 }
913
914 void
915 ttynew(void) {
916 int m, s;
917 struct winsize w = {term.row, term.col, 0, 0};
918
919 /* seems to work fine on linux, openbsd and freebsd */
920 if(openpty(&m, &s, NULL, NULL, &w) < 0)
921 die("openpty failed: %s\n", SERRNO);
922
923 switch(pid = fork()) {
924 case -1:
925 die("fork failed\n");
926 break;
927 case 0:
928 setsid(); /* create a new process group */
929 dup2(s, STDIN_FILENO);
930 dup2(s, STDOUT_FILENO);
931 dup2(s, STDERR_FILENO);
932 if(ioctl(s, TIOCSCTTY, NULL) < 0)
933 die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
934 close(s);
935 close(m);
936 execsh();
937 break;
938 default:
939 close(s);
940 cmdfd = m;
941 signal(SIGCHLD, sigchld);
942 if(opt_io) {
943 iofd = (!strcmp(opt_io, "-")) ?
944 STDOUT_FILENO :
945 open(opt_io, O_WRONLY | O_CREAT, 0666);
946 if(iofd < 0) {
947 fprintf(stderr, "Error opening %s:%s\n",
948 opt_io, strerror(errno));
949 }
950 }
951 }
952 }
953
954 void
955 dump(char c) {
956 static int col;
957
958 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
959 if(++col % 10 == 0)
960 fprintf(stderr, "\n");
961 }
962
963 void
964 ttyread(void) {
965 static char buf[BUFSIZ];
966 static int buflen = 0;
967 char *ptr;
968 char s[UTF_SIZ];
969 int charsize; /* size of utf8 char in bytes */
970 long utf8c;
971 int ret;
972
973 /* append read bytes to unprocessed bytes */
974 if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
975 die("Couldn't read from shell: %s\n", SERRNO);
976
977 /* process every complete utf8 char */
978 buflen += ret;
979 ptr = buf;
980 while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
981 charsize = utf8decode(ptr, &utf8c);
982 utf8encode(&utf8c, s);
983 tputc(s, charsize);
984 ptr += charsize;
985 buflen -= charsize;
986 }
987
988 /* keep any uncomplete utf8 char for the next call */
989 memmove(buf, ptr, buflen);
990 }
991
992 void
993 ttywrite(const char *s, size_t n) {
994 if(write(cmdfd, s, n) == -1)
995 die("write error on tty: %s\n", SERRNO);
996 }
997
998 void
999 ttyresize(void) {
1000 struct winsize w;
1001
1002 w.ws_row = term.row;
1003 w.ws_col = term.col;
1004 w.ws_xpixel = xw.tw;
1005 w.ws_ypixel = xw.th;
1006 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
1007 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
1008 }
1009
1010 void
1011 tsetdirt(int top, int bot) {
1012 int i;
1013
1014 LIMIT(top, 0, term.row-1);
1015 LIMIT(bot, 0, term.row-1);
1016
1017 for(i = top; i <= bot; i++)
1018 term.dirty[i] = 1;
1019 }
1020
1021 void
1022 tfulldirt(void) {
1023 tsetdirt(0, term.row-1);
1024 }
1025
1026 void
1027 tcursor(int mode) {
1028 static TCursor c;
1029
1030 if(mode == CURSOR_SAVE) {
1031 c = term.c;
1032 } else if(mode == CURSOR_LOAD) {
1033 term.c = c;
1034 tmoveto(c.x, c.y);
1035 }
1036 }
1037
1038 void
1039 treset(void) {
1040 uint i;
1041
1042 term.c = (TCursor){{
1043 .mode = ATTR_NULL,
1044 .fg = DefaultFG,
1045 .bg = DefaultBG
1046 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
1047
1048 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
1049 for(i = TAB; i < term.col; i += TAB)
1050 term.tabs[i] = 1;
1051 term.top = 0;
1052 term.bot = term.row - 1;
1053 term.mode = MODE_WRAP;
1054
1055 tclearregion(0, 0, term.col-1, term.row-1);
1056 }
1057
1058 void
1059 tnew(int col, int row) {
1060 /* set screen size */
1061 term.row = row;
1062 term.col = col;
1063 term.line = xmalloc(term.row * sizeof(Line));
1064 term.alt = xmalloc(term.row * sizeof(Line));
1065 term.dirty = xmalloc(term.row * sizeof(*term.dirty));
1066 term.tabs = xmalloc(term.col * sizeof(*term.tabs));
1067
1068 for(row = 0; row < term.row; row++) {
1069 term.line[row] = xmalloc(term.col * sizeof(Glyph));
1070 term.alt [row] = xmalloc(term.col * sizeof(Glyph));
1071 term.dirty[row] = 0;
1072 }
1073 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
1074 /* setup screen */
1075 treset();
1076 }
1077
1078 void
1079 tswapscreen(void) {
1080 Line *tmp = term.line;
1081
1082 term.line = term.alt;
1083 term.alt = tmp;
1084 term.mode ^= MODE_ALTSCREEN;
1085 tfulldirt();
1086 }
1087
1088 void
1089 tscrolldown(int orig, int n) {
1090 int i;
1091 Line temp;
1092
1093 LIMIT(n, 0, term.bot-orig+1);
1094
1095 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
1096
1097 for(i = term.bot; i >= orig+n; i--) {
1098 temp = term.line[i];
1099 term.line[i] = term.line[i-n];
1100 term.line[i-n] = temp;
1101
1102 term.dirty[i] = 1;
1103 term.dirty[i-n] = 1;
1104 }
1105
1106 selscroll(orig, n);
1107 }
1108
1109 void
1110 tscrollup(int orig, int n) {
1111 int i;
1112 Line temp;
1113 LIMIT(n, 0, term.bot-orig+1);
1114
1115 tclearregion(0, orig, term.col-1, orig+n-1);
1116
1117 for(i = orig; i <= term.bot-n; i++) {
1118 temp = term.line[i];
1119 term.line[i] = term.line[i+n];
1120 term.line[i+n] = temp;
1121
1122 term.dirty[i] = 1;
1123 term.dirty[i+n] = 1;
1124 }
1125
1126 selscroll(orig, -n);
1127 }
1128
1129 void
1130 selscroll(int orig, int n) {
1131 if(sel.bx == -1)
1132 return;
1133
1134 if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
1135 if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
1136 sel.bx = -1;
1137 return;
1138 }
1139 if(sel.by < term.top) {
1140 sel.by = term.top;
1141 sel.bx = 0;
1142 }
1143 if(sel.ey > term.bot) {
1144 sel.ey = term.bot;
1145 sel.ex = term.col;
1146 }
1147 sel.b.y = sel.by, sel.b.x = sel.bx;
1148 sel.e.y = sel.ey, sel.e.x = sel.ex;
1149 }
1150 }
1151
1152 void
1153 tnewline(int first_col) {
1154 int y = term.c.y;
1155
1156 if(y == term.bot) {
1157 tscrollup(term.top, 1);
1158 } else {
1159 y++;
1160 }
1161 tmoveto(first_col ? 0 : term.c.x, y);
1162 }
1163
1164 void
1165 csiparse(void) {
1166 /* int noarg = 1; */
1167 char *p = csiescseq.buf;
1168
1169 csiescseq.narg = 0;
1170 if(*p == '?')
1171 csiescseq.priv = 1, p++;
1172
1173 while(p < csiescseq.buf+csiescseq.len) {
1174 while(isdigit(*p)) {
1175 csiescseq.arg[csiescseq.narg] *= 10;
1176 csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
1177 }
1178 if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ) {
1179 csiescseq.narg++, p++;
1180 } else {
1181 csiescseq.mode = *p;
1182 csiescseq.narg++;
1183
1184 return;
1185 }
1186 }
1187 }
1188
1189 void
1190 tmoveto(int x, int y) {
1191 LIMIT(x, 0, term.col-1);
1192 LIMIT(y, 0, term.row-1);
1193 term.c.state &= ~CURSOR_WRAPNEXT;
1194 term.c.x = x;
1195 term.c.y = y;
1196 }
1197
1198 void
1199 tsetchar(char *c, Glyph *attr, int x, int y) {
1200 static char *vt100_0[62] = { /* 0x41 - 0x7e */
1201 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1202 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1203 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1204 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1205 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1206 "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1207 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1208 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1209 };
1210
1211 /*
1212 * The table is proudly stolen from rxvt.
1213 */
1214 if(attr->mode & ATTR_GFX) {
1215 if(c[0] >= 0x41 && c[0] <= 0x7e
1216 && vt100_0[c[0] - 0x41]) {
1217 c = vt100_0[c[0] - 0x41];
1218 }
1219 }
1220
1221 term.dirty[y] = 1;
1222 term.line[y][x] = *attr;
1223 memcpy(term.line[y][x].c, c, UTF_SIZ);
1224 term.line[y][x].state |= GLYPH_SET;
1225 }
1226
1227 void
1228 tclearregion(int x1, int y1, int x2, int y2) {
1229 int x, y, temp;
1230
1231 if(x1 > x2)
1232 temp = x1, x1 = x2, x2 = temp;
1233 if(y1 > y2)
1234 temp = y1, y1 = y2, y2 = temp;
1235
1236 LIMIT(x1, 0, term.col-1);
1237 LIMIT(x2, 0, term.col-1);
1238 LIMIT(y1, 0, term.row-1);
1239 LIMIT(y2, 0, term.row-1);
1240
1241 for(y = y1; y <= y2; y++) {
1242 term.dirty[y] = 1;
1243 for(x = x1; x <= x2; x++)
1244 term.line[y][x].state = 0;
1245 }
1246 }
1247
1248 void
1249 tdeletechar(int n) {
1250 int src = term.c.x + n;
1251 int dst = term.c.x;
1252 int size = term.col - src;
1253
1254 term.dirty[term.c.y] = 1;
1255
1256 if(src >= term.col) {
1257 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1258 return;
1259 }
1260
1261 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
1262 size * sizeof(Glyph));
1263 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
1264 }
1265
1266 void
1267 tinsertblank(int n) {
1268 int src = term.c.x;
1269 int dst = src + n;
1270 int size = term.col - dst;
1271
1272 term.dirty[term.c.y] = 1;
1273
1274 if(dst >= term.col) {
1275 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1276 return;
1277 }
1278
1279 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
1280 size * sizeof(Glyph));
1281 tclearregion(src, term.c.y, dst - 1, term.c.y);
1282 }
1283
1284 void
1285 tinsertblankline(int n) {
1286 if(term.c.y < term.top || term.c.y > term.bot)
1287 return;
1288
1289 tscrolldown(term.c.y, n);
1290 }
1291
1292 void
1293 tdeleteline(int n) {
1294 if(term.c.y < term.top || term.c.y > term.bot)
1295 return;
1296
1297 tscrollup(term.c.y, n);
1298 }
1299
1300 void
1301 tsetattr(int *attr, int l) {
1302 int i;
1303
1304 for(i = 0; i < l; i++) {
1305 switch(attr[i]) {
1306 case 0:
1307 term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD \
1308 | ATTR_ITALIC | ATTR_BLINK);
1309 term.c.attr.fg = DefaultFG;
1310 term.c.attr.bg = DefaultBG;
1311 break;
1312 case 1:
1313 term.c.attr.mode |= ATTR_BOLD;
1314 break;
1315 case 3: /* enter standout (highlight) */
1316 term.c.attr.mode |= ATTR_ITALIC;
1317 break;
1318 case 4:
1319 term.c.attr.mode |= ATTR_UNDERLINE;
1320 break;
1321 case 5:
1322 term.c.attr.mode |= ATTR_BLINK;
1323 break;
1324 case 7:
1325 term.c.attr.mode |= ATTR_REVERSE;
1326 break;
1327 case 21:
1328 case 22:
1329 term.c.attr.mode &= ~ATTR_BOLD;
1330 break;
1331 case 23: /* leave standout (highlight) mode */
1332 term.c.attr.mode &= ~ATTR_ITALIC;
1333 break;
1334 case 24:
1335 term.c.attr.mode &= ~ATTR_UNDERLINE;
1336 break;
1337 case 25:
1338 term.c.attr.mode &= ~ATTR_BLINK;
1339 break;
1340 case 27:
1341 term.c.attr.mode &= ~ATTR_REVERSE;
1342 break;
1343 case 38:
1344 if(i + 2 < l && attr[i + 1] == 5) {
1345 i += 2;
1346 if(BETWEEN(attr[i], 0, 255)) {
1347 term.c.attr.fg = attr[i];
1348 } else {
1349 fprintf(stderr,
1350 "erresc: bad fgcolor %d\n",
1351 attr[i]);
1352 }
1353 } else {
1354 fprintf(stderr,
1355 "erresc(38): gfx attr %d unknown\n",
1356 attr[i]);
1357 }
1358 break;
1359 case 39:
1360 term.c.attr.fg = DefaultFG;
1361 break;
1362 case 48:
1363 if(i + 2 < l && attr[i + 1] == 5) {
1364 i += 2;
1365 if(BETWEEN(attr[i], 0, 255)) {
1366 term.c.attr.bg = attr[i];
1367 } else {
1368 fprintf(stderr,
1369 "erresc: bad bgcolor %d\n",
1370 attr[i]);
1371 }
1372 } else {
1373 fprintf(stderr,
1374 "erresc(48): gfx attr %d unknown\n",
1375 attr[i]);
1376 }
1377 break;
1378 case 49:
1379 term.c.attr.bg = DefaultBG;
1380 break;
1381 default:
1382 if(BETWEEN(attr[i], 30, 37)) {
1383 term.c.attr.fg = attr[i] - 30;
1384 } else if(BETWEEN(attr[i], 40, 47)) {
1385 term.c.attr.bg = attr[i] - 40;
1386 } else if(BETWEEN(attr[i], 90, 97)) {
1387 term.c.attr.fg = attr[i] - 90 + 8;
1388 } else if(BETWEEN(attr[i], 100, 107)) {
1389 term.c.attr.bg = attr[i] - 100 + 8;
1390 } else {
1391 fprintf(stderr,
1392 "erresc(default): gfx attr %d unknown\n",
1393 attr[i]), csidump();
1394 }
1395 break;
1396 }
1397 }
1398 }
1399
1400 void
1401 tsetscroll(int t, int b) {
1402 int temp;
1403
1404 LIMIT(t, 0, term.row-1);
1405 LIMIT(b, 0, term.row-1);
1406 if(t > b) {
1407 temp = t;
1408 t = b;
1409 b = temp;
1410 }
1411 term.top = t;
1412 term.bot = b;
1413 }
1414
1415 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1416
1417 void
1418 tsetmode(bool priv, bool set, int *args, int narg) {
1419 int *lim, mode;
1420
1421 for(lim = args + narg; args < lim; ++args) {
1422 if(priv) {
1423 switch(*args) {
1424 break;
1425 case 1: /* DECCKM -- Cursor key */
1426 MODBIT(term.mode, set, MODE_APPKEYPAD);
1427 break;
1428 case 5: /* DECSCNM -- Reverse video */
1429 mode = term.mode;
1430 MODBIT(term.mode, set, MODE_REVERSE);
1431 if(mode != term.mode)
1432 redraw();
1433 break;
1434 case 6: /* XXX: DECOM -- Origin */
1435 break;
1436 case 7: /* DECAWM -- Auto wrap */
1437 MODBIT(term.mode, set, MODE_WRAP);
1438 break;
1439 case 8: /* XXX: DECARM -- Auto repeat */
1440 break;
1441 case 0: /* Error (IGNORED) */
1442 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1443 break;
1444 case 25:
1445 MODBIT(term.c.state, !set, CURSOR_HIDE);
1446 break;
1447 case 1000: /* 1000,1002: enable xterm mouse report */
1448 MODBIT(term.mode, set, MODE_MOUSEBTN);
1449 break;
1450 case 1002:
1451 MODBIT(term.mode, set, MODE_MOUSEMOTION);
1452 break;
1453 case 1049: /* = 1047 and 1048 */
1454 case 47:
1455 case 1047:
1456 if(IS_SET(MODE_ALTSCREEN))
1457 tclearregion(0, 0, term.col-1, term.row-1);
1458 if((set && !IS_SET(MODE_ALTSCREEN)) ||
1459 (!set && IS_SET(MODE_ALTSCREEN))) {
1460 tswapscreen();
1461 }
1462 if(*args != 1049)
1463 break;
1464 /* pass through */
1465 case 1048:
1466 tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
1467 break;
1468 default:
1469 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1470 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1471 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1472 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1473 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1474 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1475 fprintf(stderr,
1476 "erresc: unknown private set/reset mode %d\n",
1477 *args);
1478 break;
1479 }
1480 } else {
1481 switch(*args) {
1482 case 0: /* Error (IGNORED) */
1483 break;
1484 case 2: /* KAM -- keyboard action */
1485 MODBIT(term.mode, set, MODE_KBDLOCK);
1486 break;
1487 case 4: /* IRM -- Insertion-replacement */
1488 MODBIT(term.mode, set, MODE_INSERT);
1489 break;
1490 case 12: /* XXX: SRM -- Send/Receive */
1491 break;
1492 case 20: /* LNM -- Linefeed/new line */
1493 MODBIT(term.mode, set, MODE_CRLF);
1494 break;
1495 default:
1496 fprintf(stderr,
1497 "erresc: unknown set/reset mode %d\n",
1498 *args);
1499 break;
1500 }
1501 }
1502 }
1503 }
1504 #undef MODBIT
1505
1506
1507 void
1508 csihandle(void) {
1509 switch(csiescseq.mode) {
1510 default:
1511 unknown:
1512 fprintf(stderr, "erresc: unknown csi ");
1513 csidump();
1514 /* die(""); */
1515 break;
1516 case '@': /* ICH -- Insert <n> blank char */
1517 DEFAULT(csiescseq.arg[0], 1);
1518 tinsertblank(csiescseq.arg[0]);
1519 break;
1520 case 'A': /* CUU -- Cursor <n> Up */
1521 case 'e':
1522 DEFAULT(csiescseq.arg[0], 1);
1523 tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
1524 break;
1525 case 'B': /* CUD -- Cursor <n> Down */
1526 DEFAULT(csiescseq.arg[0], 1);
1527 tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
1528 break;
1529 case 'c': /* DA -- Device Attributes */
1530 if(csiescseq.arg[0] == 0)
1531 ttywrite(VT102ID, sizeof(VT102ID) - 1);
1532 break;
1533 case 'C': /* CUF -- Cursor <n> Forward */
1534 case 'a':
1535 DEFAULT(csiescseq.arg[0], 1);
1536 tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
1537 break;
1538 case 'D': /* CUB -- Cursor <n> Backward */
1539 DEFAULT(csiescseq.arg[0], 1);
1540 tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
1541 break;
1542 case 'E': /* CNL -- Cursor <n> Down and first col */
1543 DEFAULT(csiescseq.arg[0], 1);
1544 tmoveto(0, term.c.y+csiescseq.arg[0]);
1545 break;
1546 case 'F': /* CPL -- Cursor <n> Up and first col */
1547 DEFAULT(csiescseq.arg[0], 1);
1548 tmoveto(0, term.c.y-csiescseq.arg[0]);
1549 break;
1550 case 'g': /* TBC -- Tabulation clear */
1551 switch (csiescseq.arg[0]) {
1552 case 0: /* clear current tab stop */
1553 term.tabs[term.c.x] = 0;
1554 break;
1555 case 3: /* clear all the tabs */
1556 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
1557 break;
1558 default:
1559 goto unknown;
1560 }
1561 break;
1562 case 'G': /* CHA -- Move to <col> */
1563 case '`': /* HPA */
1564 DEFAULT(csiescseq.arg[0], 1);
1565 tmoveto(csiescseq.arg[0]-1, term.c.y);
1566 break;
1567 case 'H': /* CUP -- Move to <row> <col> */
1568 case 'f': /* HVP */
1569 DEFAULT(csiescseq.arg[0], 1);
1570 DEFAULT(csiescseq.arg[1], 1);
1571 tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
1572 break;
1573 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1574 DEFAULT(csiescseq.arg[0], 1);
1575 while(csiescseq.arg[0]--)
1576 tputtab(1);
1577 break;
1578 case 'J': /* ED -- Clear screen */
1579 sel.bx = -1;
1580 switch(csiescseq.arg[0]) {
1581 case 0: /* below */
1582 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1583 if(term.c.y < term.row-1)
1584 tclearregion(0, term.c.y+1, term.col-1, term.row-1);
1585 break;
1586 case 1: /* above */
1587 if(term.c.y > 1)
1588 tclearregion(0, 0, term.col-1, term.c.y-1);
1589 tclearregion(0, term.c.y, term.c.x, term.c.y);
1590 break;
1591 case 2: /* all */
1592 tclearregion(0, 0, term.col-1, term.row-1);
1593 break;
1594 default:
1595 goto unknown;
1596 }
1597 break;
1598 case 'K': /* EL -- Clear line */
1599 switch(csiescseq.arg[0]) {
1600 case 0: /* right */
1601 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
1602 break;
1603 case 1: /* left */
1604 tclearregion(0, term.c.y, term.c.x, term.c.y);
1605 break;
1606 case 2: /* all */
1607 tclearregion(0, term.c.y, term.col-1, term.c.y);
1608 break;
1609 }
1610 break;
1611 case 'S': /* SU -- Scroll <n> line up */
1612 DEFAULT(csiescseq.arg[0], 1);
1613 tscrollup(term.top, csiescseq.arg[0]);
1614 break;
1615 case 'T': /* SD -- Scroll <n> line down */
1616 DEFAULT(csiescseq.arg[0], 1);
1617 tscrolldown(term.top, csiescseq.arg[0]);
1618 break;
1619 case 'L': /* IL -- Insert <n> blank lines */
1620 DEFAULT(csiescseq.arg[0], 1);
1621 tinsertblankline(csiescseq.arg[0]);
1622 break;
1623 case 'l': /* RM -- Reset Mode */
1624 tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
1625 break;
1626 case 'M': /* DL -- Delete <n> lines */
1627 DEFAULT(csiescseq.arg[0], 1);
1628 tdeleteline(csiescseq.arg[0]);
1629 break;
1630 case 'X': /* ECH -- Erase <n> char */
1631 DEFAULT(csiescseq.arg[0], 1);
1632 tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0], term.c.y);
1633 break;
1634 case 'P': /* DCH -- Delete <n> char */
1635 DEFAULT(csiescseq.arg[0], 1);
1636 tdeletechar(csiescseq.arg[0]);
1637 break;
1638 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1639 DEFAULT(csiescseq.arg[0], 1);
1640 while(csiescseq.arg[0]--)
1641 tputtab(0);
1642 break;
1643 case 'd': /* VPA -- Move to <row> */
1644 DEFAULT(csiescseq.arg[0], 1);
1645 tmoveto(term.c.x, csiescseq.arg[0]-1);
1646 break;
1647 case 'h': /* SM -- Set terminal mode */
1648 tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
1649 break;
1650 case 'm': /* SGR -- Terminal attribute (color) */
1651 tsetattr(csiescseq.arg, csiescseq.narg);
1652 break;
1653 case 'r': /* DECSTBM -- Set Scrolling Region */
1654 if(csiescseq.priv) {
1655 goto unknown;
1656 } else {
1657 DEFAULT(csiescseq.arg[0], 1);
1658 DEFAULT(csiescseq.arg[1], term.row);
1659 tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
1660 tmoveto(0, 0);
1661 }
1662 break;
1663 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1664 tcursor(CURSOR_SAVE);
1665 break;
1666 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1667 tcursor(CURSOR_LOAD);
1668 break;
1669 }
1670 }
1671
1672 void
1673 csidump(void) {
1674 int i;
1675 uint c;
1676
1677 printf("ESC[");
1678 for(i = 0; i < csiescseq.len; i++) {
1679 c = csiescseq.buf[i] & 0xff;
1680 if(isprint(c)) {
1681 putchar(c);
1682 } else if(c == '\n') {
1683 printf("(\\n)");
1684 } else if(c == '\r') {
1685 printf("(\\r)");
1686 } else if(c == 0x1b) {
1687 printf("(\\e)");
1688 } else {
1689 printf("(%02x)", c);
1690 }
1691 }
1692 putchar('\n');
1693 }
1694
1695 void
1696 csireset(void) {
1697 memset(&csiescseq, 0, sizeof(csiescseq));
1698 }
1699
1700 void
1701 strhandle(void) {
1702 char *p;
1703
1704 /*
1705 * TODO: make this being useful in case of color palette change.
1706 */
1707 strparse();
1708
1709 p = strescseq.buf;
1710
1711 switch(strescseq.type) {
1712 case ']': /* OSC -- Operating System Command */
1713 switch(p[0]) {
1714 case '0':
1715 case '1':
1716 case '2':
1717 /*
1718 * TODO: Handle special chars in string, like umlauts.
1719 */
1720 if(p[1] == ';') {
1721 XStoreName(xw.dpy, xw.win, strescseq.buf+2);
1722 }
1723 break;
1724 case ';':
1725 XStoreName(xw.dpy, xw.win, strescseq.buf+1);
1726 break;
1727 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1728 break;
1729 default:
1730 fprintf(stderr, "erresc: unknown str ");
1731 strdump();
1732 break;
1733 }
1734 break;
1735 case 'k': /* old title set compatibility */
1736 XStoreName(xw.dpy, xw.win, strescseq.buf);
1737 break;
1738 case 'P': /* DSC -- Device Control String */
1739 case '_': /* APC -- Application Program Command */
1740 case '^': /* PM -- Privacy Message */
1741 default:
1742 fprintf(stderr, "erresc: unknown str ");
1743 strdump();
1744 /* die(""); */
1745 break;
1746 }
1747 }
1748
1749 void
1750 strparse(void) {
1751 /*
1752 * TODO: Implement parsing like for CSI when required.
1753 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1754 */
1755 return;
1756 }
1757
1758 void
1759 strdump(void) {
1760 int i;
1761 uint c;
1762
1763 printf("ESC%c", strescseq.type);
1764 for(i = 0; i < strescseq.len; i++) {
1765 c = strescseq.buf[i] & 0xff;
1766 if(isprint(c)) {
1767 putchar(c);
1768 } else if(c == '\n') {
1769 printf("(\\n)");
1770 } else if(c == '\r') {
1771 printf("(\\r)");
1772 } else if(c == 0x1b) {
1773 printf("(\\e)");
1774 } else {
1775 printf("(%02x)", c);
1776 }
1777 }
1778 printf("ESC\\\n");
1779 }
1780
1781 void
1782 strreset(void) {
1783 memset(&strescseq, 0, sizeof(strescseq));
1784 }
1785
1786 void
1787 tputtab(bool forward) {
1788 uint x = term.c.x;
1789
1790 if(forward) {
1791 if(x == term.col)
1792 return;
1793 for(++x; x < term.col && !term.tabs[x]; ++x)
1794 /* nothing */ ;
1795 } else {
1796 if(x == 0)
1797 return;
1798 for(--x; x > 0 && !term.tabs[x]; --x)
1799 /* nothing */ ;
1800 }
1801 tmoveto(x, term.c.y);
1802 }
1803
1804 void
1805 tputc(char *c, int len) {
1806 uchar ascii = *c;
1807 bool control = ascii < '\x20' || ascii == 0177;
1808
1809 if(iofd != -1) {
1810 if (xwrite(iofd, c, len) < 0) {
1811 fprintf(stderr, "Error writting in %s:%s\n",
1812 opt_io, strerror(errno));
1813 close(iofd);
1814 iofd = -1;
1815 }
1816 }
1817 /*
1818 * STR sequences must be checked before of anything
1819 * because it can use some control codes as part of the sequence
1820 */
1821 if(term.esc & ESC_STR) {
1822 switch(ascii) {
1823 case '\033':
1824 term.esc = ESC_START | ESC_STR_END;
1825 break;
1826 case '\a': /* backwards compatibility to xterm */
1827 term.esc = 0;
1828 strhandle();
1829 break;
1830 default:
1831 strescseq.buf[strescseq.len++] = ascii;
1832 if(strescseq.len+1 >= STR_BUF_SIZ) {
1833 term.esc = 0;
1834 strhandle();
1835 }
1836 }
1837 return;
1838 }
1839 /*
1840 * Actions of control codes must be performed as soon they arrive
1841 * because they can be embedded inside a control sequence, and
1842 * they must not cause conflicts with sequences.
1843 */
1844 if(control) {
1845 switch(ascii) {
1846 case '\t': /* HT */
1847 tputtab(1);
1848 return;
1849 case '\b': /* BS */
1850 tmoveto(term.c.x-1, term.c.y);
1851 return;
1852 case '\r': /* CR */
1853 tmoveto(0, term.c.y);
1854 return;
1855 case '\f': /* LF */
1856 case '\v': /* VT */
1857 case '\n': /* LF */
1858 /* go to first col if the mode is set */
1859 tnewline(IS_SET(MODE_CRLF));
1860 return;
1861 case '\a': /* BEL */
1862 if(!(xw.state & WIN_FOCUSED))
1863 xseturgency(1);
1864 return;
1865 case '\033': /* ESC */
1866 csireset();
1867 term.esc = ESC_START;
1868 return;
1869 case '\016': /* SO */
1870 term.c.attr.mode |= ATTR_GFX;
1871 return;
1872 case '\017': /* SI */
1873 term.c.attr.mode &= ~ATTR_GFX;
1874 return;
1875 case '\032': /* SUB */
1876 case '\030': /* CAN */
1877 csireset();
1878 return;
1879 case '\005': /* ENQ (IGNORED) */
1880 case '\000': /* NUL (IGNORED) */
1881 case '\021': /* XON (IGNORED) */
1882 case '\023': /* XOFF (IGNORED) */
1883 case 0177: /* DEL (IGNORED) */
1884 return;
1885 }
1886 } else if(term.esc & ESC_START) {
1887 if(term.esc & ESC_CSI) {
1888 csiescseq.buf[csiescseq.len++] = ascii;
1889 if(BETWEEN(ascii, 0x40, 0x7E)
1890 || csiescseq.len >= ESC_BUF_SIZ) {
1891 term.esc = 0;
1892 csiparse(), csihandle();
1893 }
1894 } else if(term.esc & ESC_STR_END) {
1895 term.esc = 0;
1896 if(ascii == '\\')
1897 strhandle();
1898 } else if(term.esc & ESC_ALTCHARSET) {
1899 switch(ascii) {
1900 case '0': /* Line drawing set */
1901 term.c.attr.mode |= ATTR_GFX;
1902 break;
1903 case 'B': /* USASCII */
1904 term.c.attr.mode &= ~ATTR_GFX;
1905 break;
1906 case 'A': /* UK (IGNORED) */
1907 case '<': /* multinational charset (IGNORED) */
1908 case '5': /* Finnish (IGNORED) */
1909 case 'C': /* Finnish (IGNORED) */
1910 case 'K': /* German (IGNORED) */
1911 break;
1912 default:
1913 fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
1914 }
1915 term.esc = 0;
1916 } else if(term.esc & ESC_TEST) {
1917 if(ascii == '8') { /* DEC screen alignment test. */
1918 char E[UTF_SIZ] = "E";
1919 int x, y;
1920
1921 for(x = 0; x < term.col; ++x) {
1922 for(y = 0; y < term.row; ++y)
1923 tsetchar(E, &term.c.attr, x, y);
1924 }
1925 }
1926 term.esc = 0;
1927 } else {
1928 switch(ascii) {
1929 case '[':
1930 term.esc |= ESC_CSI;
1931 break;
1932 case '#':
1933 term.esc |= ESC_TEST;
1934 break;
1935 case 'P': /* DCS -- Device Control String */
1936 case '_': /* APC -- Application Program Command */
1937 case '^': /* PM -- Privacy Message */
1938 case ']': /* OSC -- Operating System Command */
1939 case 'k': /* old title set compatibility */
1940 strreset();
1941 strescseq.type = ascii;
1942 term.esc |= ESC_STR;
1943 break;
1944 case '(': /* set primary charset G0 */
1945 term.esc |= ESC_ALTCHARSET;
1946 break;
1947 case ')': /* set secondary charset G1 (IGNORED) */
1948 case '*': /* set tertiary charset G2 (IGNORED) */
1949 case '+': /* set quaternary charset G3 (IGNORED) */
1950 term.esc = 0;
1951 break;
1952 case 'D': /* IND -- Linefeed */
1953 if(term.c.y == term.bot) {
1954 tscrollup(term.top, 1);
1955 } else {
1956 tmoveto(term.c.x, term.c.y+1);
1957 }
1958 term.esc = 0;
1959 break;
1960 case 'E': /* NEL -- Next line */
1961 tnewline(1); /* always go to first col */
1962 term.esc = 0;
1963 break;
1964 case 'H': /* HTS -- Horizontal tab stop */
1965 term.tabs[term.c.x] = 1;
1966 term.esc = 0;
1967 break;
1968 case 'M': /* RI -- Reverse index */
1969 if(term.c.y == term.top) {
1970 tscrolldown(term.top, 1);
1971 } else {
1972 tmoveto(term.c.x, term.c.y-1);
1973 }
1974 term.esc = 0;
1975 break;
1976 case 'Z': /* DECID -- Identify Terminal */
1977 ttywrite(VT102ID, sizeof(VT102ID) - 1);
1978 term.esc = 0;
1979 break;
1980 case 'c': /* RIS -- Reset to inital state */
1981 treset();
1982 term.esc = 0;
1983 xresettitle();
1984 break;
1985 case '=': /* DECPAM -- Application keypad */
1986 term.mode |= MODE_APPKEYPAD;
1987 term.esc = 0;
1988 break;
1989 case '>': /* DECPNM -- Normal keypad */
1990 term.mode &= ~MODE_APPKEYPAD;
1991 term.esc = 0;
1992 break;
1993 case '7': /* DECSC -- Save Cursor */
1994 tcursor(CURSOR_SAVE);
1995 term.esc = 0;
1996 break;
1997 case '8': /* DECRC -- Restore Cursor */
1998 tcursor(CURSOR_LOAD);
1999 term.esc = 0;
2000 break;
2001 case '\\': /* ST -- Stop */
2002 term.esc = 0;
2003 break;
2004 default:
2005 fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2006 (uchar) ascii, isprint(ascii)? ascii:'.');
2007 term.esc = 0;
2008 }
2009 }
2010 /*
2011 * All characters which forms part of a sequence are not
2012 * printed
2013 */
2014 return;
2015 }
2016 /*
2017 * Display control codes only if we are in graphic mode
2018 */
2019 if(control && !(term.c.attr.mode & ATTR_GFX))
2020 return;
2021 if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
2022 sel.bx = -1;
2023 if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
2024 tnewline(1); /* always go to first col */
2025 tsetchar(c, &term.c.attr, term.c.x, term.c.y);
2026 if(term.c.x+1 < term.col)
2027 tmoveto(term.c.x+1, term.c.y);
2028 else
2029 term.c.state |= CURSOR_WRAPNEXT;
2030 }
2031
2032 int
2033 tresize(int col, int row) {
2034 int i, x;
2035 int minrow = MIN(row, term.row);
2036 int mincol = MIN(col, term.col);
2037 int slide = term.c.y - row + 1;
2038 bool *bp;
2039
2040 if(col < 1 || row < 1)
2041 return 0;
2042
2043 /* free unneeded rows */
2044 i = 0;
2045 if(slide > 0) {
2046 /* slide screen to keep cursor where we expect it -
2047 * tscrollup would work here, but we can optimize to
2048 * memmove because we're freeing the earlier lines */
2049 for(/* i = 0 */; i < slide; i++) {
2050 free(term.line[i]);
2051 free(term.alt[i]);
2052 }
2053 memmove(term.line, term.line + slide, row * sizeof(Line));
2054 memmove(term.alt, term.alt + slide, row * sizeof(Line));
2055 }
2056 for(i += row; i < term.row; i++) {
2057 free(term.line[i]);
2058 free(term.alt[i]);
2059 }
2060
2061 /* resize to new height */
2062 term.line = xrealloc(term.line, row * sizeof(Line));
2063 term.alt = xrealloc(term.alt, row * sizeof(Line));
2064 term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
2065 term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
2066
2067 /* resize each row to new width, zero-pad if needed */
2068 for(i = 0; i < minrow; i++) {
2069 term.dirty[i] = 1;
2070 term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
2071 term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
2072 for(x = mincol; x < col; x++) {
2073 term.line[i][x].state = 0;
2074 term.alt[i][x].state = 0;
2075 }
2076 }
2077
2078 /* allocate any new rows */
2079 for(/* i == minrow */; i < row; i++) {
2080 term.dirty[i] = 1;
2081 term.line[i] = xcalloc(col, sizeof(Glyph));
2082 term.alt [i] = xcalloc(col, sizeof(Glyph));
2083 }
2084 if(col > term.col) {
2085 bp = term.tabs + term.col;
2086
2087 memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
2088 while(--bp > term.tabs && !*bp)
2089 /* nothing */ ;
2090 for(bp += TAB; bp < term.tabs + col; bp += TAB)
2091 *bp = 1;
2092 }
2093 /* update terminal size */
2094 term.col = col;
2095 term.row = row;
2096 /* make use of the LIMIT in tmoveto */
2097 tmoveto(term.c.x, term.c.y);
2098 /* reset scrolling region */
2099 tsetscroll(0, row-1);
2100
2101 return (slide > 0);
2102 }
2103
2104 void
2105 xresize(int col, int row) {
2106 xw.tw = MAX(1, 2*BORDER + col * xw.cw);
2107 xw.th = MAX(1, 2*BORDER + row * xw.ch);
2108
2109 XftDrawChange(xw.xft_draw, xw.buf);
2110 }
2111
2112 void
2113 xloadcols(void) {
2114 int i, r, g, b;
2115 XRenderColor xft_color = { .alpha = 0 };
2116
2117 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2118 for(i = 0; i < LEN(colorname); i++) {
2119 if(!colorname[i])
2120 continue;
2121 if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
2122 die("Could not allocate color '%s'\n", colorname[i]);
2123 }
2124 }
2125
2126 /* load colors [16-255] ; same colors as xterm */
2127 for(i = 16, r = 0; r < 6; r++) {
2128 for(g = 0; g < 6; g++) {
2129 for(b = 0; b < 6; b++) {
2130 xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
2131 xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
2132 xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
2133 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
2134 die("Could not allocate color %d\n", i);
2135 }
2136 i++;
2137 }
2138 }
2139 }
2140
2141 for(r = 0; r < 24; r++, i++) {
2142 xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
2143 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color,
2144 &dc.xft_col[i])) {
2145 die("Could not allocate color %d\n", i);
2146 }
2147 }
2148 }
2149
2150 void
2151 xtermclear(int col1, int row1, int col2, int row2) {
2152 XftDrawRect(xw.xft_draw,
2153 &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
2154 BORDER + col1 * xw.cw,
2155 BORDER + row1 * xw.ch,
2156 (col2-col1+1) * xw.cw,
2157 (row2-row1+1) * xw.ch);
2158 }
2159
2160 /*
2161 * Absolute coordinates.
2162 */
2163 void
2164 xclear(int x1, int y1, int x2, int y2) {
2165 XftDrawRect(xw.xft_draw,
2166 &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
2167 x1, y1, x2-x1, y2-y1);
2168 }
2169
2170 void
2171 xhints(void) {
2172 XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
2173 XWMHints wm = {.flags = InputHint, .input = 1};
2174 XSizeHints *sizeh = NULL;
2175
2176 sizeh = XAllocSizeHints();
2177 if(xw.isfixed == False) {
2178 sizeh->flags = PSize | PResizeInc | PBaseSize;
2179 sizeh->height = xw.h;
2180 sizeh->width = xw.w;
2181 sizeh->height_inc = xw.ch;
2182 sizeh->width_inc = xw.cw;
2183 sizeh->base_height = 2*BORDER;
2184 sizeh->base_width = 2*BORDER;
2185 } else {
2186 sizeh->flags = PMaxSize | PMinSize;
2187 sizeh->min_width = sizeh->max_width = xw.fw;
2188 sizeh->min_height = sizeh->max_height = xw.fh;
2189 }
2190
2191 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
2192 XFree(sizeh);
2193 }
2194
2195 int
2196 xloadfont(Font *f, FcPattern *pattern) {
2197 FcPattern *match;
2198 FcResult result;
2199
2200 match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
2201 if(!match)
2202 return 1;
2203 if(!(f->xft_set = XftFontOpenPattern(xw.dpy, match))) {
2204 FcPatternDestroy(match);
2205 return 1;
2206 }
2207
2208 f->ascent = f->xft_set->ascent;
2209 f->descent = f->xft_set->descent;
2210 f->lbearing = 0;
2211 f->rbearing = f->xft_set->max_advance_width;
2212
2213 f->height = f->xft_set->height;
2214 f->width = f->lbearing + f->rbearing;
2215
2216 return 0;
2217 }
2218
2219 void
2220 xloadfonts(char *fontstr, int fontsize) {
2221 FcPattern *pattern;
2222 FcResult result;
2223 double fontval;
2224
2225 pattern = FcNameParse((FcChar8 *)fontstr);
2226 if(!pattern)
2227 die("st: can't open font %s\n", fontstr);
2228
2229 if(fontsize > 0) {
2230 FcPatternDel(pattern, FC_PIXEL_SIZE);
2231 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
2232 usedfontsize = fontsize;
2233 } else {
2234 result = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
2235 if(result == FcResultMatch) {
2236 usedfontsize = (int)fontval;
2237 } else {
2238 /*
2239 * Default font size is 12, if none given. This is to
2240 * have a known usedfontsize value.
2241 */
2242 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
2243 usedfontsize = 12;
2244 }
2245 }
2246
2247 if(xloadfont(&dc.font, pattern))
2248 die("st: can't open font %s\n", fontstr);
2249
2250 /* Setting character width and height. */
2251 xw.cw = dc.font.width;
2252 xw.ch = dc.font.height;
2253
2254 FcPatternDel(pattern, FC_WEIGHT);
2255 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
2256 if(xloadfont(&dc.bfont, pattern))
2257 die("st: can't open font %s\n", fontstr);
2258
2259 FcPatternDel(pattern, FC_SLANT);
2260 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
2261 if(xloadfont(&dc.ibfont, pattern))
2262 die("st: can't open font %s\n", fontstr);
2263
2264 FcPatternDel(pattern, FC_WEIGHT);
2265 if(xloadfont(&dc.ifont, pattern))
2266 die("st: can't open font %s\n", fontstr);
2267
2268 FcPatternDestroy(pattern);
2269 }
2270
2271 void
2272 xzoom(const Arg *arg)
2273 {
2274 xloadfonts(usedfont, usedfontsize + arg->i);
2275 cresize(0, 0);
2276 draw();
2277 }
2278
2279 void
2280 xinit(void) {
2281 XSetWindowAttributes attrs;
2282 Cursor cursor;
2283 Window parent;
2284 int sw, sh, major, minor;
2285
2286 if(!(xw.dpy = XOpenDisplay(NULL)))
2287 die("Can't open display\n");
2288 xw.scr = XDefaultScreen(xw.dpy);
2289 xw.vis = XDefaultVisual(xw.dpy, xw.scr);
2290
2291 /* font */
2292 usedfont = (opt_font == NULL)? FONT : opt_font;
2293 xloadfonts(usedfont, 0);
2294
2295 /* colors */
2296 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
2297 xloadcols();
2298
2299 /* adjust fixed window geometry */
2300 if(xw.isfixed) {
2301 sw = DisplayWidth(xw.dpy, xw.scr);
2302 sh = DisplayHeight(xw.dpy, xw.scr);
2303 if(xw.fx < 0)
2304 xw.fx = sw + xw.fx - xw.fw - 1;
2305 if(xw.fy < 0)
2306 xw.fy = sh + xw.fy - xw.fh - 1;
2307
2308 xw.h = xw.fh;
2309 xw.w = xw.fw;
2310 } else {
2311 /* window - default size */
2312 xw.h = 2*BORDER + term.row * xw.ch;
2313 xw.w = 2*BORDER + term.col * xw.cw;
2314 xw.fx = 0;
2315 xw.fy = 0;
2316 }
2317
2318 attrs.background_pixel = dc.xft_col[DefaultBG].pixel;
2319 attrs.border_pixel = dc.xft_col[DefaultBG].pixel;
2320 attrs.bit_gravity = NorthWestGravity;
2321 attrs.event_mask = FocusChangeMask | KeyPressMask
2322 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
2323 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
2324 attrs.colormap = xw.cmap;
2325
2326 parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
2327 xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
2328 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
2329 xw.vis,
2330 CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2331 | CWColormap,
2332 &attrs);
2333
2334 /* double buffering */
2335 if(!XdbeQueryExtension(xw.dpy, &major, &minor))
2336 die("Xdbe extension is not present\n");
2337 xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
2338
2339 /* Xft rendering context */
2340 xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
2341
2342 /* input methods */
2343 xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
2344 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
2345 | XIMStatusNothing, XNClientWindow, xw.win,
2346 XNFocusWindow, xw.win, NULL);
2347 /* gc */
2348 dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
2349
2350 /* white cursor, black outline */
2351 cursor = XCreateFontCursor(xw.dpy, XC_xterm);
2352 XDefineCursor(xw.dpy, xw.win, cursor);
2353 XRecolorCursor(xw.dpy, cursor,
2354 &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
2355 &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
2356
2357 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
2358 xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
2359 XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
2360
2361 xresettitle();
2362 XMapWindow(xw.dpy, xw.win);
2363 xhints();
2364 XSync(xw.dpy, 0);
2365 }
2366
2367 void
2368 xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
2369 int winx = BORDER + x * xw.cw, winy = BORDER + y * xw.ch,
2370 width = charlen * xw.cw;
2371 Font *font = &dc.font;
2372 XGlyphInfo extents;
2373 XftColor *fg = &dc.xft_col[base.fg], *bg = &dc.xft_col[base.bg],
2374 *temp, revfg, revbg;
2375 XRenderColor colfg, colbg;
2376
2377 if(base.mode & ATTR_REVERSE)
2378 temp = fg, fg = bg, bg = temp;
2379
2380 if(base.mode & ATTR_BOLD) {
2381 if(BETWEEN(base.fg, 0, 7)) {
2382 /* basic system colors */
2383 fg = &dc.xft_col[base.fg + 8];
2384 } else if(BETWEEN(base.fg, 16, 195)) {
2385 /* 256 colors */
2386 fg = &dc.xft_col[base.fg + 36];
2387 } else if(BETWEEN(base.fg, 232, 251)) {
2388 /* greyscale */
2389 fg = &dc.xft_col[base.fg + 4];
2390 }
2391 /*
2392 * Those ranges will not be brightened:
2393 * 8 - 15 – bright system colors
2394 * 196 - 231 – highest 256 color cube
2395 * 252 - 255 – brightest colors in greyscale
2396 */
2397 font = &dc.bfont;
2398 }
2399
2400 if(base.mode & ATTR_ITALIC)
2401 font = &dc.ifont;
2402 if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
2403 font = &dc.ibfont;
2404
2405 if(IS_SET(MODE_REVERSE)) {
2406 if(fg == &dc.xft_col[DefaultFG]) {
2407 fg = &dc.xft_col[DefaultBG];
2408 } else {
2409 colfg.red = ~fg->color.red;
2410 colfg.green = ~fg->color.green;
2411 colfg.blue = ~fg->color.blue;
2412 colfg.alpha = fg->color.alpha;
2413 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
2414 fg = &revfg;
2415 }
2416
2417 if(bg == &dc.xft_col[DefaultBG]) {
2418 bg = &dc.xft_col[DefaultFG];
2419 } else {
2420 colbg.red = ~bg->color.red;
2421 colbg.green = ~bg->color.green;
2422 colbg.blue = ~bg->color.blue;
2423 colbg.alpha = bg->color.alpha;
2424 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
2425 bg = &revbg;
2426 }
2427 }
2428
2429 XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen,
2430 &extents);
2431 width = extents.xOff;
2432
2433 /* Intelligent cleaning up of the borders. */
2434 if(x == 0) {
2435 xclear(0, (y == 0)? 0 : winy, BORDER,
2436 winy + xw.ch + (y == term.row-1)? xw.h : 0);
2437 }
2438 if(x + charlen >= term.col-1) {
2439 xclear(winx + width, (y == 0)? 0 : winy, xw.w,
2440 (y == term.row-1)? xw.h : (winy + xw.ch));
2441 }
2442 if(y == 0)
2443 xclear(winx, 0, winx + width, BORDER);
2444 if(y == term.row-1)
2445 xclear(winx, winy + xw.ch, winx + width, xw.h);
2446
2447 XftDrawRect(xw.xft_draw, bg, winx, winy, width, xw.ch);
2448 XftDrawStringUtf8(xw.xft_draw, fg, font->xft_set, winx,
2449 winy + font->ascent, (FcChar8 *)s, bytelen);
2450
2451 if(base.mode & ATTR_UNDERLINE) {
2452 XftDrawRect(xw.xft_draw, fg, winx, winy + font->ascent + 1,
2453 width, 1);
2454 }
2455 }
2456
2457 void
2458 xdrawcursor(void) {
2459 static int oldx = 0, oldy = 0;
2460 int sl;
2461 Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
2462
2463 LIMIT(oldx, 0, term.col-1);
2464 LIMIT(oldy, 0, term.row-1);
2465
2466 if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
2467 memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
2468
2469 /* remove the old cursor */
2470 if(term.line[oldy][oldx].state & GLYPH_SET) {
2471 sl = utf8size(term.line[oldy][oldx].c);
2472 xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
2473 oldy, 1, sl);
2474 } else {
2475 xtermclear(oldx, oldy, oldx, oldy);
2476 }
2477
2478 /* draw the new one */
2479 if(!(term.c.state & CURSOR_HIDE)) {
2480 if(!(xw.state & WIN_FOCUSED))
2481 g.bg = DefaultUCS;
2482
2483 if(IS_SET(MODE_REVERSE))
2484 g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
2485
2486 sl = utf8size(g.c);
2487 xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
2488 oldx = term.c.x, oldy = term.c.y;
2489 }
2490 }
2491
2492 void
2493 xresettitle(void) {
2494 XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
2495 }
2496
2497 void
2498 redraw(void) {
2499 struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
2500
2501 tfulldirt();
2502 draw();
2503 XSync(xw.dpy, False); /* necessary for a good tput flash */
2504 nanosleep(&tv, NULL);
2505 }
2506
2507 void
2508 draw(void) {
2509 XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
2510
2511 drawregion(0, 0, term.col, term.row);
2512 XdbeSwapBuffers(xw.dpy, swpinfo, 1);
2513 }
2514
2515 void
2516 drawregion(int x1, int y1, int x2, int y2) {
2517 int ic, ib, x, y, ox, sl;
2518 Glyph base, new;
2519 char buf[DRAW_BUF_SIZ];
2520 bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
2521
2522 if((sel.alt && !alt) || (!sel.alt && alt))
2523 ena_sel = 0;
2524 if(!(xw.state & WIN_VISIBLE))
2525 return;
2526
2527 for(y = y1; y < y2; y++) {
2528 if(!term.dirty[y])
2529 continue;
2530
2531 xtermclear(0, y, term.col, y);
2532 term.dirty[y] = 0;
2533 base = term.line[y][0];
2534 ic = ib = ox = 0;
2535 for(x = x1; x < x2; x++) {
2536 new = term.line[y][x];
2537 if(ena_sel && *(new.c) && selected(x, y))
2538 new.mode ^= ATTR_REVERSE;
2539 if(ib > 0 && (!(new.state & GLYPH_SET)
2540 || ATTRCMP(base, new)
2541 || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
2542 xdraws(buf, base, ox, y, ic, ib);
2543 ic = ib = 0;
2544 }
2545 if(new.state & GLYPH_SET) {
2546 if(ib == 0) {
2547 ox = x;
2548 base = new;
2549 }
2550 sl = utf8size(new.c);
2551 memcpy(buf+ib, new.c, sl);
2552 ib += sl;
2553 ++ic;
2554 }
2555 }
2556 if(ib > 0)
2557 xdraws(buf, base, ox, y, ic, ib);
2558 }
2559 xdrawcursor();
2560 }
2561
2562 void
2563 expose(XEvent *ev) {
2564 XExposeEvent *e = &ev->xexpose;
2565
2566 if(xw.state & WIN_REDRAW) {
2567 if(!e->count)
2568 xw.state &= ~WIN_REDRAW;
2569 }
2570 }
2571
2572 void
2573 visibility(XEvent *ev) {
2574 XVisibilityEvent *e = &ev->xvisibility;
2575
2576 if(e->state == VisibilityFullyObscured) {
2577 xw.state &= ~WIN_VISIBLE;
2578 } else if(!(xw.state & WIN_VISIBLE)) {
2579 /* need a full redraw for next Expose, not just a buf copy */
2580 xw.state |= WIN_VISIBLE | WIN_REDRAW;
2581 }
2582 }
2583
2584 void
2585 unmap(XEvent *ev) {
2586 xw.state &= ~WIN_VISIBLE;
2587 }
2588
2589 void
2590 xseturgency(int add) {
2591 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
2592
2593 h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
2594 XSetWMHints(xw.dpy, xw.win, h);
2595 XFree(h);
2596 }
2597
2598 void
2599 focus(XEvent *ev) {
2600 if(ev->type == FocusIn) {
2601 xw.state |= WIN_FOCUSED;
2602 xseturgency(0);
2603 } else {
2604 xw.state &= ~WIN_FOCUSED;
2605 }
2606 }
2607
2608 char*
2609 kmap(KeySym k, uint state) {
2610 int i;
2611 uint mask;
2612
2613 state &= ~Mod2Mask;
2614 for(i = 0; i < LEN(key); i++) {
2615 mask = key[i].mask;
2616
2617 if(key[i].k == k && ((state & mask) == mask
2618 || (mask == XK_NO_MOD && !state))) {
2619 return (char*)key[i].s;
2620 }
2621 }
2622 return NULL;
2623 }
2624
2625 void
2626 kpress(XEvent *ev) {
2627 XKeyEvent *e = &ev->xkey;
2628 KeySym ksym;
2629 char buf[32], *customkey;
2630 int len, meta, shift, i;
2631 Status status;
2632
2633 if (IS_SET(MODE_KBDLOCK))
2634 return;
2635
2636 meta = e->state & Mod1Mask;
2637 shift = e->state & ShiftMask;
2638 len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
2639
2640 /* 1. shortcuts */
2641 for(i = 0; i < LEN(shortcuts); i++) {
2642 if((ksym == shortcuts[i].keysym)
2643 && (CLEANMASK(shortcuts[i].mod) == \
2644 CLEANMASK(e->state))
2645 && shortcuts[i].func) {
2646 shortcuts[i].func(&(shortcuts[i].arg));
2647 }
2648 }
2649
2650 /* 2. custom keys from config.h */
2651 if((customkey = kmap(ksym, e->state))) {
2652 ttywrite(customkey, strlen(customkey));
2653 /* 2. hardcoded (overrides X lookup) */
2654 } else {
2655 switch(ksym) {
2656 case XK_Up:
2657 case XK_Down:
2658 case XK_Left:
2659 case XK_Right:
2660 /* XXX: shift up/down doesn't work */
2661 sprintf(buf, "\033%c%c",
2662 IS_SET(MODE_APPKEYPAD) ? 'O' : '[',
2663 (shift ? "dacb":"DACB")[ksym - XK_Left]);
2664 ttywrite(buf, 3);
2665 break;
2666 case XK_Insert:
2667 if(shift)
2668 selpaste();
2669 break;
2670 case XK_Return:
2671 if(IS_SET(MODE_CRLF)) {
2672 ttywrite("\r\n", 2);
2673 } else {
2674 ttywrite("\r", 1);
2675 }
2676 break;
2677 /* 3. X lookup */
2678 default:
2679 if(len > 0) {
2680 if(meta && len == 1)
2681 ttywrite("\033", 1);
2682 ttywrite(buf, len);
2683 }
2684 break;
2685 }
2686 }
2687 }
2688
2689 void
2690 cmessage(XEvent *e) {
2691 /* See xembed specs
2692 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2693 if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
2694 if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
2695 xw.state |= WIN_FOCUSED;
2696 xseturgency(0);
2697 } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
2698 xw.state &= ~WIN_FOCUSED;
2699 }
2700 } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
2701 /* Send SIGHUP to shell */
2702 kill(pid, SIGHUP);
2703 exit(EXIT_SUCCESS);
2704 }
2705 }
2706
2707 void
2708 cresize(int width, int height)
2709 {
2710 int col, row;
2711
2712 if(width != 0)
2713 xw.w = width;
2714 if(height != 0)
2715 xw.h = height;
2716
2717 col = (xw.w - 2*BORDER) / xw.cw;
2718 row = (xw.h - 2*BORDER) / xw.ch;
2719 if(col == term.col && row == term.row)
2720 return;
2721
2722 tresize(col, row);
2723 xresize(col, row);
2724 ttyresize();
2725 }
2726
2727 void
2728 resize(XEvent *e) {
2729 if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
2730 return;
2731
2732 cresize(e->xconfigure.width, e->xconfigure.height);
2733 }
2734
2735 void
2736 run(void) {
2737 XEvent ev;
2738 fd_set rfd;
2739 int xfd = XConnectionNumber(xw.dpy), i;
2740 struct timeval drawtimeout, *tv = NULL;
2741
2742 for(i = 0;; i++) {
2743 FD_ZERO(&rfd);
2744 FD_SET(cmdfd, &rfd);
2745 FD_SET(xfd, &rfd);
2746 if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
2747 if(errno == EINTR)
2748 continue;
2749 die("select failed: %s\n", SERRNO);
2750 }
2751
2752 /*
2753 * Stop after a certain number of reads so the user does not
2754 * feel like the system is stuttering.
2755 */
2756 if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
2757 ttyread();
2758
2759 /*
2760 * Just wait a bit so it isn't disturbing the
2761 * user and the system is able to write something.
2762 */
2763 drawtimeout.tv_sec = 0;
2764 drawtimeout.tv_usec = 5;
2765 tv = &drawtimeout;
2766 continue;
2767 }
2768 i = 0;
2769 tv = NULL;
2770
2771 while(XPending(xw.dpy)) {
2772 XNextEvent(xw.dpy, &ev);
2773 if(XFilterEvent(&ev, xw.win))
2774 continue;
2775 if(handler[ev.type])
2776 (handler[ev.type])(&ev);
2777 }
2778
2779 draw();
2780 XFlush(xw.dpy);
2781 }
2782 }
2783
2784 int
2785 main(int argc, char *argv[]) {
2786 int i, bitm, xr, yr;
2787 uint wr, hr;
2788
2789 xw.fw = xw.fh = xw.fx = xw.fy = 0;
2790 xw.isfixed = False;
2791
2792 for(i = 1; i < argc; i++) {
2793 switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
2794 case 'c':
2795 if(++i < argc)
2796 opt_class = argv[i];
2797 break;
2798 case 'e':
2799 /* eat every remaining arguments */
2800 if(++i < argc)
2801 opt_cmd = &argv[i];
2802 goto run;
2803 case 'f':
2804 if(++i < argc)
2805 opt_font = argv[i];
2806 break;
2807 case 'g':
2808 if(++i >= argc)
2809 break;
2810
2811 bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
2812 if(bitm & XValue)
2813 xw.fx = xr;
2814 if(bitm & YValue)
2815 xw.fy = yr;
2816 if(bitm & WidthValue)
2817 xw.fw = (int)wr;
2818 if(bitm & HeightValue)
2819 xw.fh = (int)hr;
2820 if(bitm & XNegative && xw.fx == 0)
2821 xw.fx = -1;
2822 if(bitm & XNegative && xw.fy == 0)
2823 xw.fy = -1;
2824
2825 if(xw.fh != 0 && xw.fw != 0)
2826 xw.isfixed = True;
2827 break;
2828 case 'o':
2829 if(++i < argc)
2830 opt_io = argv[i];
2831 break;
2832 case 't':
2833 if(++i < argc)
2834 opt_title = argv[i];
2835 break;
2836 case 'v':
2837 default:
2838 die(USAGE);
2839 case 'w':
2840 if(++i < argc)
2841 opt_embed = argv[i];
2842 break;
2843 }
2844 }
2845
2846 run:
2847 setlocale(LC_CTYPE, "");
2848 tnew(80, 24);
2849 xinit();
2850 ttynew();
2851 selinit();
2852 run();
2853 return 0;
2854 }
2855