suckless

my configuration for dwm, st, dmenu.
git clone https://s.sonu.ch/~rei/suckless.git
Log | Files | Refs | README

x.c (47328B)


      1 /* See LICENSE for license details. */
      2 #include <errno.h>
      3 #include <math.h>
      4 #include <limits.h>
      5 #include <locale.h>
      6 #include <signal.h>
      7 #include <sys/select.h>
      8 #include <time.h>
      9 #include <unistd.h>
     10 #include <libgen.h>
     11 #include <X11/Xatom.h>
     12 #include <X11/Xlib.h>
     13 #include <X11/cursorfont.h>
     14 #include <X11/keysym.h>
     15 #include <X11/Xft/Xft.h>
     16 #include <X11/XKBlib.h>
     17 
     18 char *argv0;
     19 #include "arg.h"
     20 #include "st.h"
     21 #include "win.h"
     22 
     23 /* types used in config.h */
     24 typedef struct {
     25 	uint mod;
     26 	KeySym keysym;
     27 	void (*func)(const Arg *);
     28 	const Arg arg;
     29 } Shortcut;
     30 
     31 typedef struct {
     32 	uint mod;
     33 	uint button;
     34 	void (*func)(const Arg *);
     35 	const Arg arg;
     36 	uint  release;
     37 } MouseShortcut;
     38 
     39 typedef struct {
     40 	KeySym k;
     41 	uint mask;
     42 	char *s;
     43 	/* three-valued logic variables: 0 indifferent, 1 on, -1 off */
     44 	signed char appkey;    /* application keypad */
     45 	signed char appcursor; /* application cursor */
     46 } Key;
     47 
     48 /* X modifiers */
     49 #define XK_ANY_MOD    UINT_MAX
     50 #define XK_NO_MOD     0
     51 #define XK_SWITCH_MOD (1<<13|1<<14)
     52 
     53 /* function definitions used in config.h */
     54 static void clipcopy(const Arg *);
     55 static void clippaste(const Arg *);
     56 static void numlock(const Arg *);
     57 static void selpaste(const Arg *);
     58 static void zoom(const Arg *);
     59 static void zoomabs(const Arg *);
     60 static void zoomreset(const Arg *);
     61 static void ttysend(const Arg *);
     62 
     63 /* config.h for applying patches and the configuration. */
     64 #include "config.h"
     65 
     66 /* XEMBED messages */
     67 #define XEMBED_FOCUS_IN  4
     68 #define XEMBED_FOCUS_OUT 5
     69 
     70 /* macros */
     71 #define IS_SET(flag)		((win.mode & (flag)) != 0)
     72 #define TRUERED(x)		(((x) & 0xff0000) >> 8)
     73 #define TRUEGREEN(x)		(((x) & 0xff00))
     74 #define TRUEBLUE(x)		(((x) & 0xff) << 8)
     75 
     76 typedef XftDraw *Draw;
     77 typedef XftColor Color;
     78 typedef XftGlyphFontSpec GlyphFontSpec;
     79 
     80 /* Purely graphic info */
     81 typedef struct {
     82 	int tw, th; /* tty width and height */
     83 	int w, h; /* window width and height */
     84 	int ch; /* char height */
     85 	int cw; /* char width  */
     86 	int mode; /* window state/mode flags */
     87 	int cursor; /* cursor style */
     88 } TermWindow;
     89 
     90 typedef struct {
     91 	Display *dpy;
     92 	Colormap cmap;
     93 	Window win;
     94 	Drawable buf;
     95 	GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
     96 	Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid;
     97 	struct {
     98 		XIM xim;
     99 		XIC xic;
    100 		XPoint spot;
    101 		XVaNestedList spotlist;
    102 	} ime;
    103 	Draw draw;
    104 	Visual *vis;
    105 	XSetWindowAttributes attrs;
    106 	int scr;
    107 	int isfixed; /* is fixed geometry? */
    108 	int l, t; /* left and top offset */
    109 	int gm; /* geometry mask */
    110 } XWindow;
    111 
    112 typedef struct {
    113 	Atom xtarget;
    114 	char *primary, *clipboard;
    115 	struct timespec tclick1;
    116 	struct timespec tclick2;
    117 } XSelection;
    118 
    119 /* Font structure */
    120 #define Font Font_
    121 typedef struct {
    122 	int height;
    123 	int width;
    124 	int ascent;
    125 	int descent;
    126 	int badslant;
    127 	int badweight;
    128 	short lbearing;
    129 	short rbearing;
    130 	XftFont *match;
    131 	FcFontSet *set;
    132 	FcPattern *pattern;
    133 } Font;
    134 
    135 /* Drawing Context */
    136 typedef struct {
    137 	Color *col;
    138 	size_t collen;
    139 	Font font, bfont, ifont, ibfont;
    140 	GC gc;
    141 } DC;
    142 
    143 static inline ushort sixd_to_16bit(int);
    144 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
    145 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
    146 static void xdrawglyph(Glyph, int, int);
    147 static void xclear(int, int, int, int);
    148 static int xgeommasktogravity(int);
    149 static int ximopen(Display *);
    150 static void ximinstantiate(Display *, XPointer, XPointer);
    151 static void ximdestroy(XIM, XPointer, XPointer);
    152 static int xicdestroy(XIC, XPointer, XPointer);
    153 static void xinit(int, int);
    154 static void cresize(int, int);
    155 static void xresize(int, int);
    156 static void xhints(void);
    157 static int xloadcolor(int, const char *, Color *);
    158 static int xloadfont(Font *, FcPattern *);
    159 static void xloadfonts(const char *, double);
    160 static void xunloadfont(Font *);
    161 static void xunloadfonts(void);
    162 static void xsetenv(void);
    163 static void xseturgency(int);
    164 static int evcol(XEvent *);
    165 static int evrow(XEvent *);
    166 
    167 static void expose(XEvent *);
    168 static void visibility(XEvent *);
    169 static void unmap(XEvent *);
    170 static void kpress(XEvent *);
    171 static void cmessage(XEvent *);
    172 static void resize(XEvent *);
    173 static void focus(XEvent *);
    174 static uint buttonmask(uint);
    175 static int mouseaction(XEvent *, uint);
    176 static void brelease(XEvent *);
    177 static void bpress(XEvent *);
    178 static void bmotion(XEvent *);
    179 static void propnotify(XEvent *);
    180 static void selnotify(XEvent *);
    181 static void selclear_(XEvent *);
    182 static void selrequest(XEvent *);
    183 static void setsel(char *, Time);
    184 static void mousesel(XEvent *, int);
    185 static void mousereport(XEvent *);
    186 static char *kmap(KeySym, uint);
    187 static int match(uint, uint);
    188 
    189 static void run(void);
    190 static void usage(void);
    191 
    192 static void (*handler[LASTEvent])(XEvent *) = {
    193 	[KeyPress] = kpress,
    194 	[ClientMessage] = cmessage,
    195 	[ConfigureNotify] = resize,
    196 	[VisibilityNotify] = visibility,
    197 	[UnmapNotify] = unmap,
    198 	[Expose] = expose,
    199 	[FocusIn] = focus,
    200 	[FocusOut] = focus,
    201 	[MotionNotify] = bmotion,
    202 	[ButtonPress] = bpress,
    203 	[ButtonRelease] = brelease,
    204 /*
    205  * Uncomment if you want the selection to disappear when you select something
    206  * different in another window.
    207  */
    208 /*	[SelectionClear] = selclear_, */
    209 	[SelectionNotify] = selnotify,
    210 /*
    211  * PropertyNotify is only turned on when there is some INCR transfer happening
    212  * for the selection retrieval.
    213  */
    214 	[PropertyNotify] = propnotify,
    215 	[SelectionRequest] = selrequest,
    216 };
    217 
    218 /* Globals */
    219 static DC dc;
    220 static XWindow xw;
    221 static XSelection xsel;
    222 static TermWindow win;
    223 
    224 /* Font Ring Cache */
    225 enum {
    226 	FRC_NORMAL,
    227 	FRC_ITALIC,
    228 	FRC_BOLD,
    229 	FRC_ITALICBOLD
    230 };
    231 
    232 typedef struct {
    233 	XftFont *font;
    234 	int flags;
    235 	Rune unicodep;
    236 } Fontcache;
    237 
    238 /* Fontcache is an array now. A new font will be appended to the array. */
    239 static Fontcache *frc = NULL;
    240 static int frclen = 0;
    241 static int frccap = 0;
    242 static char *usedfont = NULL;
    243 static double usedfontsize = 0;
    244 static double defaultfontsize = 0;
    245 
    246 static char *opt_class = NULL;
    247 static char **opt_cmd  = NULL;
    248 static char *opt_embed = NULL;
    249 static char *opt_font  = NULL;
    250 static char *opt_io    = NULL;
    251 static char *opt_line  = NULL;
    252 static char *opt_name  = NULL;
    253 static char *opt_title = NULL;
    254 
    255 static int oldbutton = 3; /* button event on startup: 3 = release */
    256 
    257 void
    258 clipcopy(const Arg *dummy)
    259 {
    260 	Atom clipboard;
    261 
    262 	free(xsel.clipboard);
    263 	xsel.clipboard = NULL;
    264 
    265 	if (xsel.primary != NULL) {
    266 		xsel.clipboard = xstrdup(xsel.primary);
    267 		clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    268 		XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
    269 	}
    270 }
    271 
    272 void
    273 clippaste(const Arg *dummy)
    274 {
    275 	Atom clipboard;
    276 
    277 	clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    278 	XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
    279 			xw.win, CurrentTime);
    280 }
    281 
    282 void
    283 selpaste(const Arg *dummy)
    284 {
    285 	XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
    286 			xw.win, CurrentTime);
    287 }
    288 
    289 void
    290 numlock(const Arg *dummy)
    291 {
    292 	win.mode ^= MODE_NUMLOCK;
    293 }
    294 
    295 void
    296 zoom(const Arg *arg)
    297 {
    298 	Arg larg;
    299 
    300 	larg.f = usedfontsize + arg->f;
    301 	zoomabs(&larg);
    302 }
    303 
    304 void
    305 zoomabs(const Arg *arg)
    306 {
    307 	xunloadfonts();
    308 	xloadfonts(usedfont, arg->f);
    309 	cresize(0, 0);
    310 	redraw();
    311 	xhints();
    312 }
    313 
    314 void
    315 zoomreset(const Arg *arg)
    316 {
    317 	Arg larg;
    318 
    319 	if (defaultfontsize > 0) {
    320 		larg.f = defaultfontsize;
    321 		zoomabs(&larg);
    322 	}
    323 }
    324 
    325 void
    326 ttysend(const Arg *arg)
    327 {
    328 	ttywrite(arg->s, strlen(arg->s), 1);
    329 }
    330 
    331 int
    332 evcol(XEvent *e)
    333 {
    334 	int x = e->xbutton.x - borderpx;
    335 	LIMIT(x, 0, win.tw - 1);
    336 	return x / win.cw;
    337 }
    338 
    339 int
    340 evrow(XEvent *e)
    341 {
    342 	int y = e->xbutton.y - borderpx;
    343 	LIMIT(y, 0, win.th - 1);
    344 	return y / win.ch;
    345 }
    346 
    347 void
    348 mousesel(XEvent *e, int done)
    349 {
    350 	int type, seltype = SEL_REGULAR;
    351 	uint state = e->xbutton.state & ~(Button1Mask | forcemousemod);
    352 
    353 	for (type = 1; type < LEN(selmasks); ++type) {
    354 		if (match(selmasks[type], state)) {
    355 			seltype = type;
    356 			break;
    357 		}
    358 	}
    359 	selextend(evcol(e), evrow(e), seltype, done);
    360 	if (done)
    361 		setsel(getsel(), e->xbutton.time);
    362 }
    363 
    364 void
    365 mousereport(XEvent *e)
    366 {
    367 	int len, x = evcol(e), y = evrow(e),
    368 	    button = e->xbutton.button, state = e->xbutton.state;
    369 	char buf[40];
    370 	static int ox, oy;
    371 
    372 	/* from urxvt */
    373 	if (e->xbutton.type == MotionNotify) {
    374 		if (x == ox && y == oy)
    375 			return;
    376 		if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
    377 			return;
    378 		/* MOUSE_MOTION: no reporting if no button is pressed */
    379 		if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
    380 			return;
    381 
    382 		button = oldbutton + 32;
    383 		ox = x;
    384 		oy = y;
    385 	} else {
    386 		if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
    387 			button = 3;
    388 		} else {
    389 			button -= Button1;
    390 			if (button >= 7)
    391 				button += 128 - 7;
    392 			else if (button >= 3)
    393 				button += 64 - 3;
    394 		}
    395 		if (e->xbutton.type == ButtonPress) {
    396 			oldbutton = button;
    397 			ox = x;
    398 			oy = y;
    399 		} else if (e->xbutton.type == ButtonRelease) {
    400 			oldbutton = 3;
    401 			/* MODE_MOUSEX10: no button release reporting */
    402 			if (IS_SET(MODE_MOUSEX10))
    403 				return;
    404 			if (button == 64 || button == 65)
    405 				return;
    406 		}
    407 	}
    408 
    409 	if (!IS_SET(MODE_MOUSEX10)) {
    410 		button += ((state & ShiftMask  ) ? 4  : 0)
    411 			+ ((state & Mod4Mask   ) ? 8  : 0)
    412 			+ ((state & ControlMask) ? 16 : 0);
    413 	}
    414 
    415 	if (IS_SET(MODE_MOUSESGR)) {
    416 		len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
    417 				button, x+1, y+1,
    418 				e->xbutton.type == ButtonRelease ? 'm' : 'M');
    419 	} else if (x < 223 && y < 223) {
    420 		len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
    421 				32+button, 32+x+1, 32+y+1);
    422 	} else {
    423 		return;
    424 	}
    425 
    426 	ttywrite(buf, len, 0);
    427 }
    428 
    429 uint
    430 buttonmask(uint button)
    431 {
    432 	return button == Button1 ? Button1Mask
    433 	     : button == Button2 ? Button2Mask
    434 	     : button == Button3 ? Button3Mask
    435 	     : button == Button4 ? Button4Mask
    436 	     : button == Button5 ? Button5Mask
    437 	     : 0;
    438 }
    439 
    440 int
    441 mouseaction(XEvent *e, uint release)
    442 {
    443 	MouseShortcut *ms;
    444 
    445 	/* ignore Button<N>mask for Button<N> - it's set on release */
    446 	uint state = e->xbutton.state & ~buttonmask(e->xbutton.button);
    447 
    448 	for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
    449 		if (ms->release == release &&
    450 		    ms->button == e->xbutton.button &&
    451 		    (match(ms->mod, state) ||  /* exact or forced */
    452 		     match(ms->mod, state & ~forcemousemod))) {
    453 			ms->func(&(ms->arg));
    454 			return 1;
    455 		}
    456 	}
    457 
    458 	return 0;
    459 }
    460 
    461 void
    462 bpress(XEvent *e)
    463 {
    464 	struct timespec now;
    465 	int snap;
    466 
    467 	if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    468 		mousereport(e);
    469 		return;
    470 	}
    471 
    472 	if (mouseaction(e, 0))
    473 		return;
    474 
    475 	if (e->xbutton.button == Button1) {
    476 		/*
    477 		 * If the user clicks below predefined timeouts specific
    478 		 * snapping behaviour is exposed.
    479 		 */
    480 		clock_gettime(CLOCK_MONOTONIC, &now);
    481 		if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
    482 			snap = SNAP_LINE;
    483 		} else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
    484 			snap = SNAP_WORD;
    485 		} else {
    486 			snap = 0;
    487 		}
    488 		xsel.tclick2 = xsel.tclick1;
    489 		xsel.tclick1 = now;
    490 
    491 		selstart(evcol(e), evrow(e), snap);
    492 	}
    493 }
    494 
    495 void
    496 propnotify(XEvent *e)
    497 {
    498 	XPropertyEvent *xpev;
    499 	Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    500 
    501 	xpev = &e->xproperty;
    502 	if (xpev->state == PropertyNewValue &&
    503 			(xpev->atom == XA_PRIMARY ||
    504 			 xpev->atom == clipboard)) {
    505 		selnotify(e);
    506 	}
    507 }
    508 
    509 void
    510 selnotify(XEvent *e)
    511 {
    512 	ulong nitems, ofs, rem;
    513 	int format;
    514 	uchar *data, *last, *repl;
    515 	Atom type, incratom, property = None;
    516 
    517 	incratom = XInternAtom(xw.dpy, "INCR", 0);
    518 
    519 	ofs = 0;
    520 	if (e->type == SelectionNotify)
    521 		property = e->xselection.property;
    522 	else if (e->type == PropertyNotify)
    523 		property = e->xproperty.atom;
    524 
    525 	if (property == None)
    526 		return;
    527 
    528 	do {
    529 		if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
    530 					BUFSIZ/4, False, AnyPropertyType,
    531 					&type, &format, &nitems, &rem,
    532 					&data)) {
    533 			fprintf(stderr, "Clipboard allocation failed\n");
    534 			return;
    535 		}
    536 
    537 		if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
    538 			/*
    539 			 * If there is some PropertyNotify with no data, then
    540 			 * this is the signal of the selection owner that all
    541 			 * data has been transferred. We won't need to receive
    542 			 * PropertyNotify events anymore.
    543 			 */
    544 			MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
    545 			XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
    546 					&xw.attrs);
    547 		}
    548 
    549 		if (type == incratom) {
    550 			/*
    551 			 * Activate the PropertyNotify events so we receive
    552 			 * when the selection owner does send us the next
    553 			 * chunk of data.
    554 			 */
    555 			MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
    556 			XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
    557 					&xw.attrs);
    558 
    559 			/*
    560 			 * Deleting the property is the transfer start signal.
    561 			 */
    562 			XDeleteProperty(xw.dpy, xw.win, (int)property);
    563 			continue;
    564 		}
    565 
    566 		/*
    567 		 * As seen in getsel:
    568 		 * Line endings are inconsistent in the terminal and GUI world
    569 		 * copy and pasting. When receiving some selection data,
    570 		 * replace all '\n' with '\r'.
    571 		 * FIXME: Fix the computer world.
    572 		 */
    573 		repl = data;
    574 		last = data + nitems * format / 8;
    575 		while ((repl = memchr(repl, '\n', last - repl))) {
    576 			*repl++ = '\r';
    577 		}
    578 
    579 		if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
    580 			ttywrite("\033[200~", 6, 0);
    581 		ttywrite((char *)data, nitems * format / 8, 1);
    582 		if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
    583 			ttywrite("\033[201~", 6, 0);
    584 		XFree(data);
    585 		/* number of 32-bit chunks returned */
    586 		ofs += nitems * format / 32;
    587 	} while (rem > 0);
    588 
    589 	/*
    590 	 * Deleting the property again tells the selection owner to send the
    591 	 * next data chunk in the property.
    592 	 */
    593 	XDeleteProperty(xw.dpy, xw.win, (int)property);
    594 }
    595 
    596 void
    597 xclipcopy(void)
    598 {
    599 	clipcopy(NULL);
    600 }
    601 
    602 void
    603 selclear_(XEvent *e)
    604 {
    605 	selclear();
    606 }
    607 
    608 void
    609 selrequest(XEvent *e)
    610 {
    611 	XSelectionRequestEvent *xsre;
    612 	XSelectionEvent xev;
    613 	Atom xa_targets, string, clipboard;
    614 	char *seltext;
    615 
    616 	xsre = (XSelectionRequestEvent *) e;
    617 	xev.type = SelectionNotify;
    618 	xev.requestor = xsre->requestor;
    619 	xev.selection = xsre->selection;
    620 	xev.target = xsre->target;
    621 	xev.time = xsre->time;
    622 	if (xsre->property == None)
    623 		xsre->property = xsre->target;
    624 
    625 	/* reject */
    626 	xev.property = None;
    627 
    628 	xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
    629 	if (xsre->target == xa_targets) {
    630 		/* respond with the supported type */
    631 		string = xsel.xtarget;
    632 		XChangeProperty(xsre->display, xsre->requestor, xsre->property,
    633 				XA_ATOM, 32, PropModeReplace,
    634 				(uchar *) &string, 1);
    635 		xev.property = xsre->property;
    636 	} else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
    637 		/*
    638 		 * xith XA_STRING non ascii characters may be incorrect in the
    639 		 * requestor. It is not our problem, use utf8.
    640 		 */
    641 		clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    642 		if (xsre->selection == XA_PRIMARY) {
    643 			seltext = xsel.primary;
    644 		} else if (xsre->selection == clipboard) {
    645 			seltext = xsel.clipboard;
    646 		} else {
    647 			fprintf(stderr,
    648 				"Unhandled clipboard selection 0x%lx\n",
    649 				xsre->selection);
    650 			return;
    651 		}
    652 		if (seltext != NULL) {
    653 			XChangeProperty(xsre->display, xsre->requestor,
    654 					xsre->property, xsre->target,
    655 					8, PropModeReplace,
    656 					(uchar *)seltext, strlen(seltext));
    657 			xev.property = xsre->property;
    658 		}
    659 	}
    660 
    661 	/* all done, send a notification to the listener */
    662 	if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
    663 		fprintf(stderr, "Error sending SelectionNotify event\n");
    664 }
    665 
    666 void
    667 setsel(char *str, Time t)
    668 {
    669 	if (!str)
    670 		return;
    671 
    672 	free(xsel.primary);
    673 	xsel.primary = str;
    674 
    675 	XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
    676 	if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
    677 		selclear();
    678 	clipcopy(NULL);
    679 }
    680 
    681 void
    682 xsetsel(char *str)
    683 {
    684 	setsel(str, CurrentTime);
    685 }
    686 
    687 void
    688 brelease(XEvent *e)
    689 {
    690 	if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    691 		mousereport(e);
    692 		return;
    693 	}
    694 
    695 	if (mouseaction(e, 1))
    696 		return;
    697 	if (e->xbutton.button == Button1)
    698 		mousesel(e, 1);
    699 }
    700 
    701 void
    702 bmotion(XEvent *e)
    703 {
    704 	if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    705 		mousereport(e);
    706 		return;
    707 	}
    708 
    709 	mousesel(e, 0);
    710 }
    711 
    712 void
    713 cresize(int width, int height)
    714 {
    715 	int col, row;
    716 
    717 	if (width != 0)
    718 		win.w = width;
    719 	if (height != 0)
    720 		win.h = height;
    721 
    722 	col = (win.w - 2 * borderpx) / win.cw;
    723 	row = (win.h - 2 * borderpx) / win.ch;
    724 	col = MAX(1, col);
    725 	row = MAX(1, row);
    726 
    727 	tresize(col, row);
    728 	xresize(col, row);
    729 	ttyresize(win.tw, win.th);
    730 }
    731 
    732 void
    733 xresize(int col, int row)
    734 {
    735 	win.tw = col * win.cw;
    736 	win.th = row * win.ch;
    737 
    738 	XFreePixmap(xw.dpy, xw.buf);
    739 	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
    740 			DefaultDepth(xw.dpy, xw.scr));
    741 	XftDrawChange(xw.draw, xw.buf);
    742 	xclear(0, 0, win.w, win.h);
    743 
    744 	/* resize to new width */
    745 	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
    746 }
    747 
    748 ushort
    749 sixd_to_16bit(int x)
    750 {
    751 	return x == 0 ? 0 : 0x3737 + 0x2828 * x;
    752 }
    753 
    754 int
    755 xloadcolor(int i, const char *name, Color *ncolor)
    756 {
    757 	XRenderColor color = { .alpha = 0xffff };
    758 
    759 	if (!name) {
    760 		if (BETWEEN(i, 16, 255)) { /* 256 color */
    761 			if (i < 6*6*6+16) { /* same colors as xterm */
    762 				color.red   = sixd_to_16bit( ((i-16)/36)%6 );
    763 				color.green = sixd_to_16bit( ((i-16)/6) %6 );
    764 				color.blue  = sixd_to_16bit( ((i-16)/1) %6 );
    765 			} else { /* greyscale */
    766 				color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
    767 				color.green = color.blue = color.red;
    768 			}
    769 			return XftColorAllocValue(xw.dpy, xw.vis,
    770 			                          xw.cmap, &color, ncolor);
    771 		} else
    772 			name = colorname[i];
    773 	}
    774 
    775 	return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
    776 }
    777 
    778 void
    779 xloadcols(void)
    780 {
    781 	int i;
    782 	static int loaded;
    783 	Color *cp;
    784 
    785 	if (loaded) {
    786 		for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
    787 			XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
    788 	} else {
    789 		dc.collen = MAX(LEN(colorname), 256);
    790 		dc.col = xmalloc(dc.collen * sizeof(Color));
    791 	}
    792 
    793 	for (i = 0; i < dc.collen; i++)
    794 		if (!xloadcolor(i, NULL, &dc.col[i])) {
    795 			if (colorname[i])
    796 				die("could not allocate color '%s'\n", colorname[i]);
    797 			else
    798 				die("could not allocate color %d\n", i);
    799 		}
    800 	loaded = 1;
    801 }
    802 
    803 int
    804 xsetcolorname(int x, const char *name)
    805 {
    806 	Color ncolor;
    807 
    808 	if (!BETWEEN(x, 0, dc.collen))
    809 		return 1;
    810 
    811 	if (!xloadcolor(x, name, &ncolor))
    812 		return 1;
    813 
    814 	XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
    815 	dc.col[x] = ncolor;
    816 
    817 	return 0;
    818 }
    819 
    820 /*
    821  * Absolute coordinates.
    822  */
    823 void
    824 xclear(int x1, int y1, int x2, int y2)
    825 {
    826 	XftDrawRect(xw.draw,
    827 			&dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
    828 			x1, y1, x2-x1, y2-y1);
    829 }
    830 
    831 void
    832 xhints(void)
    833 {
    834 	XClassHint class = {opt_name ? opt_name : termname,
    835 	                    opt_class ? opt_class : termname};
    836 	XWMHints wm = {.flags = InputHint, .input = 1};
    837 	XSizeHints *sizeh;
    838 
    839 	sizeh = XAllocSizeHints();
    840 
    841 	sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize;
    842 	sizeh->height = win.h;
    843 	sizeh->width = win.w;
    844 	sizeh->height_inc = win.ch;
    845 	sizeh->width_inc = win.cw;
    846 	sizeh->base_height = 2 * borderpx;
    847 	sizeh->base_width = 2 * borderpx;
    848 	sizeh->min_height = win.ch + 2 * borderpx;
    849 	sizeh->min_width = win.cw + 2 * borderpx;
    850 	if (xw.isfixed) {
    851 		sizeh->flags |= PMaxSize;
    852 		sizeh->min_width = sizeh->max_width = win.w;
    853 		sizeh->min_height = sizeh->max_height = win.h;
    854 	}
    855 	if (xw.gm & (XValue|YValue)) {
    856 		sizeh->flags |= USPosition | PWinGravity;
    857 		sizeh->x = xw.l;
    858 		sizeh->y = xw.t;
    859 		sizeh->win_gravity = xgeommasktogravity(xw.gm);
    860 	}
    861 
    862 	XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
    863 			&class);
    864 	XFree(sizeh);
    865 }
    866 
    867 int
    868 xgeommasktogravity(int mask)
    869 {
    870 	switch (mask & (XNegative|YNegative)) {
    871 	case 0:
    872 		return NorthWestGravity;
    873 	case XNegative:
    874 		return NorthEastGravity;
    875 	case YNegative:
    876 		return SouthWestGravity;
    877 	}
    878 
    879 	return SouthEastGravity;
    880 }
    881 
    882 int
    883 xloadfont(Font *f, FcPattern *pattern)
    884 {
    885 	FcPattern *configured;
    886 	FcPattern *match;
    887 	FcResult result;
    888 	XGlyphInfo extents;
    889 	int wantattr, haveattr;
    890 
    891 	/*
    892 	 * Manually configure instead of calling XftMatchFont
    893 	 * so that we can use the configured pattern for
    894 	 * "missing glyph" lookups.
    895 	 */
    896 	configured = FcPatternDuplicate(pattern);
    897 	if (!configured)
    898 		return 1;
    899 
    900 	FcConfigSubstitute(NULL, configured, FcMatchPattern);
    901 	XftDefaultSubstitute(xw.dpy, xw.scr, configured);
    902 
    903 	match = FcFontMatch(NULL, configured, &result);
    904 	if (!match) {
    905 		FcPatternDestroy(configured);
    906 		return 1;
    907 	}
    908 
    909 	if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
    910 		FcPatternDestroy(configured);
    911 		FcPatternDestroy(match);
    912 		return 1;
    913 	}
    914 
    915 	if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
    916 	    XftResultMatch)) {
    917 		/*
    918 		 * Check if xft was unable to find a font with the appropriate
    919 		 * slant but gave us one anyway. Try to mitigate.
    920 		 */
    921 		if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
    922 		    &haveattr) != XftResultMatch) || haveattr < wantattr) {
    923 			f->badslant = 1;
    924 			fputs("font slant does not match\n", stderr);
    925 		}
    926 	}
    927 
    928 	if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
    929 	    XftResultMatch)) {
    930 		if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
    931 		    &haveattr) != XftResultMatch) || haveattr != wantattr) {
    932 			f->badweight = 1;
    933 			fputs("font weight does not match\n", stderr);
    934 		}
    935 	}
    936 
    937 	XftTextExtentsUtf8(xw.dpy, f->match,
    938 		(const FcChar8 *) ascii_printable,
    939 		strlen(ascii_printable), &extents);
    940 
    941 	f->set = NULL;
    942 	f->pattern = configured;
    943 
    944 	f->ascent = f->match->ascent;
    945 	f->descent = f->match->descent;
    946 	f->lbearing = 0;
    947 	f->rbearing = f->match->max_advance_width;
    948 
    949 	f->height = f->ascent + f->descent;
    950 	f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
    951 
    952 	return 0;
    953 }
    954 
    955 void
    956 xloadfonts(const char *fontstr, double fontsize)
    957 {
    958 	FcPattern *pattern;
    959 	double fontval;
    960 
    961 	if (fontstr[0] == '-')
    962 		pattern = XftXlfdParse(fontstr, False, False);
    963 	else
    964 		pattern = FcNameParse((const FcChar8 *)fontstr);
    965 
    966 	if (!pattern)
    967 		die("can't open font %s\n", fontstr);
    968 
    969 	if (fontsize > 1) {
    970 		FcPatternDel(pattern, FC_PIXEL_SIZE);
    971 		FcPatternDel(pattern, FC_SIZE);
    972 		FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
    973 		usedfontsize = fontsize;
    974 	} else {
    975 		if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
    976 				FcResultMatch) {
    977 			usedfontsize = fontval;
    978 		} else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
    979 				FcResultMatch) {
    980 			usedfontsize = -1;
    981 		} else {
    982 			/*
    983 			 * Default font size is 12, if none given. This is to
    984 			 * have a known usedfontsize value.
    985 			 */
    986 			FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
    987 			usedfontsize = 12;
    988 		}
    989 		defaultfontsize = usedfontsize;
    990 	}
    991 
    992 	if (xloadfont(&dc.font, pattern))
    993 		die("can't open font %s\n", fontstr);
    994 
    995 	if (usedfontsize < 0) {
    996 		FcPatternGetDouble(dc.font.match->pattern,
    997 		                   FC_PIXEL_SIZE, 0, &fontval);
    998 		usedfontsize = fontval;
    999 		if (fontsize == 0)
   1000 			defaultfontsize = fontval;
   1001 	}
   1002 
   1003 	/* Setting character width and height. */
   1004 	win.cw = ceilf(dc.font.width * cwscale);
   1005 	win.ch = ceilf(dc.font.height * chscale);
   1006 
   1007 	FcPatternDel(pattern, FC_SLANT);
   1008 	FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
   1009 	if (xloadfont(&dc.ifont, pattern))
   1010 		die("can't open font %s\n", fontstr);
   1011 
   1012 	FcPatternDel(pattern, FC_WEIGHT);
   1013 	FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
   1014 	if (xloadfont(&dc.ibfont, pattern))
   1015 		die("can't open font %s\n", fontstr);
   1016 
   1017 	FcPatternDel(pattern, FC_SLANT);
   1018 	FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
   1019 	if (xloadfont(&dc.bfont, pattern))
   1020 		die("can't open font %s\n", fontstr);
   1021 
   1022 	FcPatternDestroy(pattern);
   1023 }
   1024 
   1025 void
   1026 xunloadfont(Font *f)
   1027 {
   1028 	XftFontClose(xw.dpy, f->match);
   1029 	FcPatternDestroy(f->pattern);
   1030 	if (f->set)
   1031 		FcFontSetDestroy(f->set);
   1032 }
   1033 
   1034 void
   1035 xunloadfonts(void)
   1036 {
   1037 	/* Free the loaded fonts in the font cache.  */
   1038 	while (frclen > 0)
   1039 		XftFontClose(xw.dpy, frc[--frclen].font);
   1040 
   1041 	xunloadfont(&dc.font);
   1042 	xunloadfont(&dc.bfont);
   1043 	xunloadfont(&dc.ifont);
   1044 	xunloadfont(&dc.ibfont);
   1045 }
   1046 
   1047 int
   1048 ximopen(Display *dpy)
   1049 {
   1050 	XIMCallback imdestroy = { .client_data = NULL, .callback = ximdestroy };
   1051 	XICCallback icdestroy = { .client_data = NULL, .callback = xicdestroy };
   1052 
   1053 	xw.ime.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
   1054 	if (xw.ime.xim == NULL)
   1055 		return 0;
   1056 
   1057 	if (XSetIMValues(xw.ime.xim, XNDestroyCallback, &imdestroy, NULL))
   1058 		fprintf(stderr, "XSetIMValues: "
   1059 		                "Could not set XNDestroyCallback.\n");
   1060 
   1061 	xw.ime.spotlist = XVaCreateNestedList(0, XNSpotLocation, &xw.ime.spot,
   1062 	                                      NULL);
   1063 
   1064 	if (xw.ime.xic == NULL) {
   1065 		xw.ime.xic = XCreateIC(xw.ime.xim, XNInputStyle,
   1066 		                       XIMPreeditNothing | XIMStatusNothing,
   1067 		                       XNClientWindow, xw.win,
   1068 		                       XNDestroyCallback, &icdestroy,
   1069 		                       NULL);
   1070 	}
   1071 	if (xw.ime.xic == NULL)
   1072 		fprintf(stderr, "XCreateIC: Could not create input context.\n");
   1073 
   1074 	return 1;
   1075 }
   1076 
   1077 void
   1078 ximinstantiate(Display *dpy, XPointer client, XPointer call)
   1079 {
   1080 	if (ximopen(dpy))
   1081 		XUnregisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1082 		                                 ximinstantiate, NULL);
   1083 }
   1084 
   1085 void
   1086 ximdestroy(XIM xim, XPointer client, XPointer call)
   1087 {
   1088 	xw.ime.xim = NULL;
   1089 	XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1090 	                               ximinstantiate, NULL);
   1091 	XFree(xw.ime.spotlist);
   1092 }
   1093 
   1094 int
   1095 xicdestroy(XIC xim, XPointer client, XPointer call)
   1096 {
   1097 	xw.ime.xic = NULL;
   1098 	return 1;
   1099 }
   1100 
   1101 void
   1102 xinit(int cols, int rows)
   1103 {
   1104 	XGCValues gcvalues;
   1105 	Cursor cursor;
   1106 	Window parent;
   1107 	pid_t thispid = getpid();
   1108 	XColor xmousefg, xmousebg;
   1109 
   1110 	if (!(xw.dpy = XOpenDisplay(NULL)))
   1111 		die("can't open display\n");
   1112 	xw.scr = XDefaultScreen(xw.dpy);
   1113 	xw.vis = XDefaultVisual(xw.dpy, xw.scr);
   1114 
   1115 	/* font */
   1116 	if (!FcInit())
   1117 		die("could not init fontconfig.\n");
   1118 
   1119 	usedfont = (opt_font == NULL)? font : opt_font;
   1120 	xloadfonts(usedfont, 0);
   1121 
   1122 	/* colors */
   1123 	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
   1124 	xloadcols();
   1125 
   1126 	/* adjust fixed window geometry */
   1127 	win.w = 2 * borderpx + cols * win.cw;
   1128 	win.h = 2 * borderpx + rows * win.ch;
   1129 	if (xw.gm & XNegative)
   1130 		xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
   1131 	if (xw.gm & YNegative)
   1132 		xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
   1133 
   1134 	/* Events */
   1135 	xw.attrs.background_pixel = dc.col[defaultbg].pixel;
   1136 	xw.attrs.border_pixel = dc.col[defaultbg].pixel;
   1137 	xw.attrs.bit_gravity = NorthWestGravity;
   1138 	xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
   1139 		| ExposureMask | VisibilityChangeMask | StructureNotifyMask
   1140 		| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
   1141 	xw.attrs.colormap = xw.cmap;
   1142 
   1143 	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
   1144 		parent = XRootWindow(xw.dpy, xw.scr);
   1145 	xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
   1146 			win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
   1147 			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
   1148 			| CWEventMask | CWColormap, &xw.attrs);
   1149 
   1150 	memset(&gcvalues, 0, sizeof(gcvalues));
   1151 	gcvalues.graphics_exposures = False;
   1152 	dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
   1153 			&gcvalues);
   1154 	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
   1155 			DefaultDepth(xw.dpy, xw.scr));
   1156 	XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
   1157 	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
   1158 
   1159 	/* font spec buffer */
   1160 	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
   1161 
   1162 	/* Xft rendering context */
   1163 	xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
   1164 
   1165 	/* input methods */
   1166 	if (!ximopen(xw.dpy)) {
   1167 		XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1168 	                                       ximinstantiate, NULL);
   1169 	}
   1170 
   1171 	/* white cursor, black outline */
   1172 	cursor = XCreateFontCursor(xw.dpy, mouseshape);
   1173 	XDefineCursor(xw.dpy, xw.win, cursor);
   1174 
   1175 	if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
   1176 		xmousefg.red   = 0xffff;
   1177 		xmousefg.green = 0xffff;
   1178 		xmousefg.blue  = 0xffff;
   1179 	}
   1180 
   1181 	if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
   1182 		xmousebg.red   = 0x0000;
   1183 		xmousebg.green = 0x0000;
   1184 		xmousebg.blue  = 0x0000;
   1185 	}
   1186 
   1187 	XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
   1188 
   1189 	xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
   1190 	xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
   1191 	xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
   1192 	xw.netwmiconname = XInternAtom(xw.dpy, "_NET_WM_ICON_NAME", False);
   1193 	XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
   1194 
   1195 	xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
   1196 	XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
   1197 			PropModeReplace, (uchar *)&thispid, 1);
   1198 
   1199 	win.mode = MODE_NUMLOCK;
   1200 	resettitle();
   1201 	xhints();
   1202 	XMapWindow(xw.dpy, xw.win);
   1203 	XSync(xw.dpy, False);
   1204 
   1205 	clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
   1206 	clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
   1207 	xsel.primary = NULL;
   1208 	xsel.clipboard = NULL;
   1209 	xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
   1210 	if (xsel.xtarget == None)
   1211 		xsel.xtarget = XA_STRING;
   1212 }
   1213 
   1214 int
   1215 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
   1216 {
   1217 	float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
   1218 	ushort mode, prevmode = USHRT_MAX;
   1219 	Font *font = &dc.font;
   1220 	int frcflags = FRC_NORMAL;
   1221 	float runewidth = win.cw;
   1222 	Rune rune;
   1223 	FT_UInt glyphidx;
   1224 	FcResult fcres;
   1225 	FcPattern *fcpattern, *fontpattern;
   1226 	FcFontSet *fcsets[] = { NULL };
   1227 	FcCharSet *fccharset;
   1228 	int i, f, numspecs = 0;
   1229 
   1230 	for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
   1231 		/* Fetch rune and mode for current glyph. */
   1232 		rune = glyphs[i].u;
   1233 		mode = glyphs[i].mode;
   1234 
   1235 		/* Skip dummy wide-character spacing. */
   1236 		if (mode == ATTR_WDUMMY)
   1237 			continue;
   1238 
   1239 		/* Determine font for glyph if different from previous glyph. */
   1240 		if (prevmode != mode) {
   1241 			prevmode = mode;
   1242 			font = &dc.font;
   1243 			frcflags = FRC_NORMAL;
   1244 			runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
   1245 			if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
   1246 				font = &dc.ibfont;
   1247 				frcflags = FRC_ITALICBOLD;
   1248 			} else if (mode & ATTR_ITALIC) {
   1249 				font = &dc.ifont;
   1250 				frcflags = FRC_ITALIC;
   1251 			} else if (mode & ATTR_BOLD) {
   1252 				font = &dc.bfont;
   1253 				frcflags = FRC_BOLD;
   1254 			}
   1255 			yp = winy + font->ascent;
   1256 		}
   1257 
   1258 		/* Lookup character index with default font. */
   1259 		glyphidx = XftCharIndex(xw.dpy, font->match, rune);
   1260 		if (glyphidx) {
   1261 			specs[numspecs].font = font->match;
   1262 			specs[numspecs].glyph = glyphidx;
   1263 			specs[numspecs].x = (short)xp;
   1264 			specs[numspecs].y = (short)yp;
   1265 			xp += runewidth;
   1266 			numspecs++;
   1267 			continue;
   1268 		}
   1269 
   1270 		/* Fallback on font cache, search the font cache for match. */
   1271 		for (f = 0; f < frclen; f++) {
   1272 			glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
   1273 			/* Everything correct. */
   1274 			if (glyphidx && frc[f].flags == frcflags)
   1275 				break;
   1276 			/* We got a default font for a not found glyph. */
   1277 			if (!glyphidx && frc[f].flags == frcflags
   1278 					&& frc[f].unicodep == rune) {
   1279 				break;
   1280 			}
   1281 		}
   1282 
   1283 		/* Nothing was found. Use fontconfig to find matching font. */
   1284 		if (f >= frclen) {
   1285 			if (!font->set)
   1286 				font->set = FcFontSort(0, font->pattern,
   1287 				                       1, 0, &fcres);
   1288 			fcsets[0] = font->set;
   1289 
   1290 			/*
   1291 			 * Nothing was found in the cache. Now use
   1292 			 * some dozen of Fontconfig calls to get the
   1293 			 * font for one single character.
   1294 			 *
   1295 			 * Xft and fontconfig are design failures.
   1296 			 */
   1297 			fcpattern = FcPatternDuplicate(font->pattern);
   1298 			fccharset = FcCharSetCreate();
   1299 
   1300 			FcCharSetAddChar(fccharset, rune);
   1301 			FcPatternAddCharSet(fcpattern, FC_CHARSET,
   1302 					fccharset);
   1303 			FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
   1304 
   1305 			FcConfigSubstitute(0, fcpattern,
   1306 					FcMatchPattern);
   1307 			FcDefaultSubstitute(fcpattern);
   1308 
   1309 			fontpattern = FcFontSetMatch(0, fcsets, 1,
   1310 					fcpattern, &fcres);
   1311 
   1312 			/* Allocate memory for the new cache entry. */
   1313 			if (frclen >= frccap) {
   1314 				frccap += 16;
   1315 				frc = xrealloc(frc, frccap * sizeof(Fontcache));
   1316 			}
   1317 
   1318 			frc[frclen].font = XftFontOpenPattern(xw.dpy,
   1319 					fontpattern);
   1320 			if (!frc[frclen].font)
   1321 				die("XftFontOpenPattern failed seeking fallback font: %s\n",
   1322 					strerror(errno));
   1323 			frc[frclen].flags = frcflags;
   1324 			frc[frclen].unicodep = rune;
   1325 
   1326 			glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
   1327 
   1328 			f = frclen;
   1329 			frclen++;
   1330 
   1331 			FcPatternDestroy(fcpattern);
   1332 			FcCharSetDestroy(fccharset);
   1333 		}
   1334 
   1335 		specs[numspecs].font = frc[f].font;
   1336 		specs[numspecs].glyph = glyphidx;
   1337 		specs[numspecs].x = (short)xp;
   1338 		specs[numspecs].y = (short)yp;
   1339 		xp += runewidth;
   1340 		numspecs++;
   1341 	}
   1342 
   1343 	return numspecs;
   1344 }
   1345 
   1346 void
   1347 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
   1348 {
   1349 	int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
   1350 	int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
   1351 	    width = charlen * win.cw;
   1352 	Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
   1353 	XRenderColor colfg, colbg;
   1354 	XRectangle r;
   1355 
   1356 	/* Fallback on color display for attributes not supported by the font */
   1357 	if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
   1358 		if (dc.ibfont.badslant || dc.ibfont.badweight)
   1359 			base.fg = defaultattr;
   1360 	} else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
   1361 	    (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
   1362 		base.fg = defaultattr;
   1363 	}
   1364 
   1365 	if (IS_TRUECOL(base.fg)) {
   1366 		colfg.alpha = 0xffff;
   1367 		colfg.red = TRUERED(base.fg);
   1368 		colfg.green = TRUEGREEN(base.fg);
   1369 		colfg.blue = TRUEBLUE(base.fg);
   1370 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
   1371 		fg = &truefg;
   1372 	} else {
   1373 		fg = &dc.col[base.fg];
   1374 	}
   1375 
   1376 	if (IS_TRUECOL(base.bg)) {
   1377 		colbg.alpha = 0xffff;
   1378 		colbg.green = TRUEGREEN(base.bg);
   1379 		colbg.red = TRUERED(base.bg);
   1380 		colbg.blue = TRUEBLUE(base.bg);
   1381 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
   1382 		bg = &truebg;
   1383 	} else {
   1384 		bg = &dc.col[base.bg];
   1385 	}
   1386 
   1387 	/* Change basic system colors [0-7] to bright system colors [8-15] */
   1388 	if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
   1389 		fg = &dc.col[base.fg + 8];
   1390 
   1391 	if (IS_SET(MODE_REVERSE)) {
   1392 		if (fg == &dc.col[defaultfg]) {
   1393 			fg = &dc.col[defaultbg];
   1394 		} else {
   1395 			colfg.red = ~fg->color.red;
   1396 			colfg.green = ~fg->color.green;
   1397 			colfg.blue = ~fg->color.blue;
   1398 			colfg.alpha = fg->color.alpha;
   1399 			XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
   1400 					&revfg);
   1401 			fg = &revfg;
   1402 		}
   1403 
   1404 		if (bg == &dc.col[defaultbg]) {
   1405 			bg = &dc.col[defaultfg];
   1406 		} else {
   1407 			colbg.red = ~bg->color.red;
   1408 			colbg.green = ~bg->color.green;
   1409 			colbg.blue = ~bg->color.blue;
   1410 			colbg.alpha = bg->color.alpha;
   1411 			XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
   1412 					&revbg);
   1413 			bg = &revbg;
   1414 		}
   1415 	}
   1416 
   1417 	if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
   1418 		colfg.red = fg->color.red / 2;
   1419 		colfg.green = fg->color.green / 2;
   1420 		colfg.blue = fg->color.blue / 2;
   1421 		colfg.alpha = fg->color.alpha;
   1422 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
   1423 		fg = &revfg;
   1424 	}
   1425 
   1426 	if (base.mode & ATTR_REVERSE) {
   1427 		temp = fg;
   1428 		fg = bg;
   1429 		bg = temp;
   1430 	}
   1431 
   1432 	if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
   1433 		fg = bg;
   1434 
   1435 	if (base.mode & ATTR_INVISIBLE)
   1436 		fg = bg;
   1437 
   1438 	/* Intelligent cleaning up of the borders. */
   1439 	if (x == 0) {
   1440 		xclear(0, (y == 0)? 0 : winy, borderpx,
   1441 			winy + win.ch +
   1442 			((winy + win.ch >= borderpx + win.th)? win.h : 0));
   1443 	}
   1444 	if (winx + width >= borderpx + win.tw) {
   1445 		xclear(winx + width, (y == 0)? 0 : winy, win.w,
   1446 			((winy + win.ch >= borderpx + win.th)? win.h : (winy + win.ch)));
   1447 	}
   1448 	if (y == 0)
   1449 		xclear(winx, 0, winx + width, borderpx);
   1450 	if (winy + win.ch >= borderpx + win.th)
   1451 		xclear(winx, winy + win.ch, winx + width, win.h);
   1452 
   1453 	/* Clean up the region we want to draw to. */
   1454 	XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
   1455 
   1456 	/* Set the clip region because Xft is sometimes dirty. */
   1457 	r.x = 0;
   1458 	r.y = 0;
   1459 	r.height = win.ch;
   1460 	r.width = width;
   1461 	XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
   1462 
   1463 	/* Render the glyphs. */
   1464 	XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
   1465 
   1466 	/* Render underline and strikethrough. */
   1467 	if (base.mode & ATTR_UNDERLINE) {
   1468 		XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
   1469 				width, 1);
   1470 	}
   1471 
   1472 	if (base.mode & ATTR_STRUCK) {
   1473 		XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
   1474 				width, 1);
   1475 	}
   1476 
   1477 	/* Reset clip to none. */
   1478 	XftDrawSetClip(xw.draw, 0);
   1479 }
   1480 
   1481 void
   1482 xdrawglyph(Glyph g, int x, int y)
   1483 {
   1484 	int numspecs;
   1485 	XftGlyphFontSpec spec;
   1486 
   1487 	numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
   1488 	xdrawglyphfontspecs(&spec, g, numspecs, x, y);
   1489 }
   1490 
   1491 void
   1492 xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
   1493 {
   1494 	Color drawcol;
   1495 
   1496 	/* remove the old cursor */
   1497 	if (selected(ox, oy))
   1498 		og.mode ^= ATTR_REVERSE;
   1499 	xdrawglyph(og, ox, oy);
   1500 
   1501 	if (IS_SET(MODE_HIDE))
   1502 		return;
   1503 
   1504 	/*
   1505 	 * Select the right color for the right mode.
   1506 	 */
   1507 	g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE;
   1508 
   1509 	if (IS_SET(MODE_REVERSE)) {
   1510 		g.mode |= ATTR_REVERSE;
   1511 		g.bg = defaultfg;
   1512 		if (selected(cx, cy)) {
   1513 			drawcol = dc.col[defaultcs];
   1514 			g.fg = defaultrcs;
   1515 		} else {
   1516 			drawcol = dc.col[defaultrcs];
   1517 			g.fg = defaultcs;
   1518 		}
   1519 	} else {
   1520 		if (selected(cx, cy)) {
   1521 			g.fg = defaultfg;
   1522 			g.bg = defaultrcs;
   1523 		} else {
   1524 			g.fg = defaultbg;
   1525 			g.bg = defaultcs;
   1526 		}
   1527 		drawcol = dc.col[g.bg];
   1528 	}
   1529 
   1530 	/* draw the new one */
   1531 	if (IS_SET(MODE_FOCUSED)) {
   1532 		switch (win.cursor) {
   1533 		case 7: /* st extension */
   1534 			g.u = 0x2603; /* snowman (U+2603) */
   1535 			/* FALLTHROUGH */
   1536 		case 0: /* Blinking Block */
   1537 		case 1: /* Blinking Block (Default) */
   1538 		case 2: /* Steady Block */
   1539 			xdrawglyph(g, cx, cy);
   1540 			break;
   1541 		case 3: /* Blinking Underline */
   1542 		case 4: /* Steady Underline */
   1543 			XftDrawRect(xw.draw, &drawcol,
   1544 					borderpx + cx * win.cw,
   1545 					borderpx + (cy + 1) * win.ch - \
   1546 						cursorthickness,
   1547 					win.cw, cursorthickness);
   1548 			break;
   1549 		case 5: /* Blinking bar */
   1550 		case 6: /* Steady bar */
   1551 			XftDrawRect(xw.draw, &drawcol,
   1552 					borderpx + cx * win.cw,
   1553 					borderpx + cy * win.ch,
   1554 					cursorthickness, win.ch);
   1555 			break;
   1556 		}
   1557 	} else {
   1558 		XftDrawRect(xw.draw, &drawcol,
   1559 				borderpx + cx * win.cw,
   1560 				borderpx + cy * win.ch,
   1561 				win.cw - 1, 1);
   1562 		XftDrawRect(xw.draw, &drawcol,
   1563 				borderpx + cx * win.cw,
   1564 				borderpx + cy * win.ch,
   1565 				1, win.ch - 1);
   1566 		XftDrawRect(xw.draw, &drawcol,
   1567 				borderpx + (cx + 1) * win.cw - 1,
   1568 				borderpx + cy * win.ch,
   1569 				1, win.ch - 1);
   1570 		XftDrawRect(xw.draw, &drawcol,
   1571 				borderpx + cx * win.cw,
   1572 				borderpx + (cy + 1) * win.ch - 1,
   1573 				win.cw, 1);
   1574 	}
   1575 }
   1576 
   1577 void
   1578 xsetenv(void)
   1579 {
   1580 	char buf[sizeof(long) * 8 + 1];
   1581 
   1582 	snprintf(buf, sizeof(buf), "%lu", xw.win);
   1583 	setenv("WINDOWID", buf, 1);
   1584 }
   1585 
   1586 void
   1587 xseticontitle(char *p)
   1588 {
   1589 	XTextProperty prop;
   1590 	DEFAULT(p, opt_title);
   1591 
   1592 	Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
   1593 			&prop);
   1594 	XSetWMIconName(xw.dpy, xw.win, &prop);
   1595 	XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmiconname);
   1596 	XFree(prop.value);
   1597 }
   1598 
   1599 void
   1600 xsettitle(char *p)
   1601 {
   1602 	XTextProperty prop;
   1603 	DEFAULT(p, opt_title);
   1604 
   1605 	Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
   1606 			&prop);
   1607 	XSetWMName(xw.dpy, xw.win, &prop);
   1608 	XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
   1609 	XFree(prop.value);
   1610 }
   1611 
   1612 int
   1613 xstartdraw(void)
   1614 {
   1615 	return IS_SET(MODE_VISIBLE);
   1616 }
   1617 
   1618 void
   1619 xdrawline(Line line, int x1, int y1, int x2)
   1620 {
   1621 	int i, x, ox, numspecs;
   1622 	Glyph base, new;
   1623 	XftGlyphFontSpec *specs = xw.specbuf;
   1624 
   1625 	numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
   1626 	i = ox = 0;
   1627 	for (x = x1; x < x2 && i < numspecs; x++) {
   1628 		new = line[x];
   1629 		if (new.mode == ATTR_WDUMMY)
   1630 			continue;
   1631 		if (selected(x, y1))
   1632 			new.mode ^= ATTR_REVERSE;
   1633 		if (i > 0 && ATTRCMP(base, new)) {
   1634 			xdrawglyphfontspecs(specs, base, i, ox, y1);
   1635 			specs += i;
   1636 			numspecs -= i;
   1637 			i = 0;
   1638 		}
   1639 		if (i == 0) {
   1640 			ox = x;
   1641 			base = new;
   1642 		}
   1643 		i++;
   1644 	}
   1645 	if (i > 0)
   1646 		xdrawglyphfontspecs(specs, base, i, ox, y1);
   1647 }
   1648 
   1649 void
   1650 xfinishdraw(void)
   1651 {
   1652 	XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
   1653 			win.h, 0, 0);
   1654 	XSetForeground(xw.dpy, dc.gc,
   1655 			dc.col[IS_SET(MODE_REVERSE)?
   1656 				defaultfg : defaultbg].pixel);
   1657 }
   1658 
   1659 void
   1660 xximspot(int x, int y)
   1661 {
   1662 	if (xw.ime.xic == NULL)
   1663 		return;
   1664 
   1665 	xw.ime.spot.x = borderpx + x * win.cw;
   1666 	xw.ime.spot.y = borderpx + (y + 1) * win.ch;
   1667 
   1668 	XSetICValues(xw.ime.xic, XNPreeditAttributes, xw.ime.spotlist, NULL);
   1669 }
   1670 
   1671 void
   1672 expose(XEvent *ev)
   1673 {
   1674 	redraw();
   1675 }
   1676 
   1677 void
   1678 visibility(XEvent *ev)
   1679 {
   1680 	XVisibilityEvent *e = &ev->xvisibility;
   1681 
   1682 	MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
   1683 }
   1684 
   1685 void
   1686 unmap(XEvent *ev)
   1687 {
   1688 	win.mode &= ~MODE_VISIBLE;
   1689 }
   1690 
   1691 void
   1692 xsetpointermotion(int set)
   1693 {
   1694 	MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
   1695 	XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
   1696 }
   1697 
   1698 void
   1699 xsetmode(int set, unsigned int flags)
   1700 {
   1701 	int mode = win.mode;
   1702 	MODBIT(win.mode, set, flags);
   1703 	if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
   1704 		redraw();
   1705 }
   1706 
   1707 int
   1708 xsetcursor(int cursor)
   1709 {
   1710 	if (!BETWEEN(cursor, 0, 7)) /* 7: st extension */
   1711 		return 1;
   1712 	win.cursor = cursor;
   1713 	return 0;
   1714 }
   1715 
   1716 void
   1717 xseturgency(int add)
   1718 {
   1719 	XWMHints *h = XGetWMHints(xw.dpy, xw.win);
   1720 
   1721 	MODBIT(h->flags, add, XUrgencyHint);
   1722 	XSetWMHints(xw.dpy, xw.win, h);
   1723 	XFree(h);
   1724 }
   1725 
   1726 void
   1727 xbell(void)
   1728 {
   1729 	if (!(IS_SET(MODE_FOCUSED)))
   1730 		xseturgency(1);
   1731 	if (bellvolume)
   1732 		XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
   1733 }
   1734 
   1735 void
   1736 focus(XEvent *ev)
   1737 {
   1738 	XFocusChangeEvent *e = &ev->xfocus;
   1739 
   1740 	if (e->mode == NotifyGrab)
   1741 		return;
   1742 
   1743 	if (ev->type == FocusIn) {
   1744 		if (xw.ime.xic)
   1745 			XSetICFocus(xw.ime.xic);
   1746 		win.mode |= MODE_FOCUSED;
   1747 		xseturgency(0);
   1748 		if (IS_SET(MODE_FOCUS))
   1749 			ttywrite("\033[I", 3, 0);
   1750 	} else {
   1751 		if (xw.ime.xic)
   1752 			XUnsetICFocus(xw.ime.xic);
   1753 		win.mode &= ~MODE_FOCUSED;
   1754 		if (IS_SET(MODE_FOCUS))
   1755 			ttywrite("\033[O", 3, 0);
   1756 	}
   1757 }
   1758 
   1759 int
   1760 match(uint mask, uint state)
   1761 {
   1762 	return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
   1763 }
   1764 
   1765 char*
   1766 kmap(KeySym k, uint state)
   1767 {
   1768 	Key *kp;
   1769 	int i;
   1770 
   1771 	/* Check for mapped keys out of X11 function keys. */
   1772 	for (i = 0; i < LEN(mappedkeys); i++) {
   1773 		if (mappedkeys[i] == k)
   1774 			break;
   1775 	}
   1776 	if (i == LEN(mappedkeys)) {
   1777 		if ((k & 0xFFFF) < 0xFD00)
   1778 			return NULL;
   1779 	}
   1780 
   1781 	for (kp = key; kp < key + LEN(key); kp++) {
   1782 		if (kp->k != k)
   1783 			continue;
   1784 
   1785 		if (!match(kp->mask, state))
   1786 			continue;
   1787 
   1788 		if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
   1789 			continue;
   1790 		if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
   1791 			continue;
   1792 
   1793 		if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
   1794 			continue;
   1795 
   1796 		return kp->s;
   1797 	}
   1798 
   1799 	return NULL;
   1800 }
   1801 
   1802 void
   1803 kpress(XEvent *ev)
   1804 {
   1805 	XKeyEvent *e = &ev->xkey;
   1806 	KeySym ksym;
   1807 	char buf[64], *customkey;
   1808 	int len;
   1809 	Rune c;
   1810 	Status status;
   1811 	Shortcut *bp;
   1812 
   1813 	if (IS_SET(MODE_KBDLOCK))
   1814 		return;
   1815 
   1816 	if (xw.ime.xic)
   1817 		len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status);
   1818 	else
   1819 		len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
   1820 	/* 1. shortcuts */
   1821 	for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
   1822 		if (ksym == bp->keysym && match(bp->mod, e->state)) {
   1823 			bp->func(&(bp->arg));
   1824 			return;
   1825 		}
   1826 	}
   1827 
   1828 	/* 2. custom keys from config.h */
   1829 	if ((customkey = kmap(ksym, e->state))) {
   1830 		ttywrite(customkey, strlen(customkey), 1);
   1831 		return;
   1832 	}
   1833 
   1834 	/* 3. composed string from input method */
   1835 	if (len == 0)
   1836 		return;
   1837 	if (len == 1 && e->state & Mod1Mask) {
   1838 		if (IS_SET(MODE_8BIT)) {
   1839 			if (*buf < 0177) {
   1840 				c = *buf | 0x80;
   1841 				len = utf8encode(c, buf);
   1842 			}
   1843 		} else {
   1844 			buf[1] = buf[0];
   1845 			buf[0] = '\033';
   1846 			len = 2;
   1847 		}
   1848 	}
   1849 	ttywrite(buf, len, 1);
   1850 }
   1851 
   1852 void
   1853 cmessage(XEvent *e)
   1854 {
   1855 	/*
   1856 	 * See xembed specs
   1857 	 *  http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
   1858 	 */
   1859 	if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
   1860 		if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
   1861 			win.mode |= MODE_FOCUSED;
   1862 			xseturgency(0);
   1863 		} else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
   1864 			win.mode &= ~MODE_FOCUSED;
   1865 		}
   1866 	} else if (e->xclient.data.l[0] == xw.wmdeletewin) {
   1867 		ttyhangup();
   1868 		exit(0);
   1869 	}
   1870 }
   1871 
   1872 void
   1873 resize(XEvent *e)
   1874 {
   1875 	if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
   1876 		return;
   1877 
   1878 	cresize(e->xconfigure.width, e->xconfigure.height);
   1879 }
   1880 
   1881 void
   1882 run(void)
   1883 {
   1884 	XEvent ev;
   1885 	int w = win.w, h = win.h;
   1886 	fd_set rfd;
   1887 	int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
   1888 	struct timespec seltv, *tv, now, lastblink, trigger;
   1889 	double timeout;
   1890 
   1891 	/* Waiting for window mapping */
   1892 	do {
   1893 		XNextEvent(xw.dpy, &ev);
   1894 		/*
   1895 		 * This XFilterEvent call is required because of XOpenIM. It
   1896 		 * does filter out the key event and some client message for
   1897 		 * the input method too.
   1898 		 */
   1899 		if (XFilterEvent(&ev, None))
   1900 			continue;
   1901 		if (ev.type == ConfigureNotify) {
   1902 			w = ev.xconfigure.width;
   1903 			h = ev.xconfigure.height;
   1904 		}
   1905 	} while (ev.type != MapNotify);
   1906 
   1907 	ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
   1908 	cresize(w, h);
   1909 
   1910 	for (timeout = -1, drawing = 0, lastblink = (struct timespec){0};;) {
   1911 		FD_ZERO(&rfd);
   1912 		FD_SET(ttyfd, &rfd);
   1913 		FD_SET(xfd, &rfd);
   1914 
   1915 		if (XPending(xw.dpy))
   1916 			timeout = 0;  /* existing events might not set xfd */
   1917 
   1918 		seltv.tv_sec = timeout / 1E3;
   1919 		seltv.tv_nsec = 1E6 * (timeout - 1E3 * seltv.tv_sec);
   1920 		tv = timeout >= 0 ? &seltv : NULL;
   1921 
   1922 		if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
   1923 			if (errno == EINTR)
   1924 				continue;
   1925 			die("select failed: %s\n", strerror(errno));
   1926 		}
   1927 		clock_gettime(CLOCK_MONOTONIC, &now);
   1928 
   1929 		if (FD_ISSET(ttyfd, &rfd))
   1930 			ttyread();
   1931 
   1932 		xev = 0;
   1933 		while (XPending(xw.dpy)) {
   1934 			xev = 1;
   1935 			XNextEvent(xw.dpy, &ev);
   1936 			if (XFilterEvent(&ev, None))
   1937 				continue;
   1938 			if (handler[ev.type])
   1939 				(handler[ev.type])(&ev);
   1940 		}
   1941 
   1942 		/*
   1943 		 * To reduce flicker and tearing, when new content or event
   1944 		 * triggers drawing, we first wait a bit to ensure we got
   1945 		 * everything, and if nothing new arrives - we draw.
   1946 		 * We start with trying to wait minlatency ms. If more content
   1947 		 * arrives sooner, we retry with shorter and shorter periods,
   1948 		 * and eventually draw even without idle after maxlatency ms.
   1949 		 * Typically this results in low latency while interacting,
   1950 		 * maximum latency intervals during `cat huge.txt`, and perfect
   1951 		 * sync with periodic updates from animations/key-repeats/etc.
   1952 		 */
   1953 		if (FD_ISSET(ttyfd, &rfd) || xev) {
   1954 			if (!drawing) {
   1955 				trigger = now;
   1956 				drawing = 1;
   1957 			}
   1958 			timeout = (maxlatency - TIMEDIFF(now, trigger)) \
   1959 			          / maxlatency * minlatency;
   1960 			if (timeout > 0)
   1961 				continue;  /* we have time, try to find idle */
   1962 		}
   1963 
   1964 		/* idle detected or maxlatency exhausted -> draw */
   1965 		timeout = -1;
   1966 		if (blinktimeout && tattrset(ATTR_BLINK)) {
   1967 			timeout = blinktimeout - TIMEDIFF(now, lastblink);
   1968 			if (timeout <= 0) {
   1969 				if (-timeout > blinktimeout) /* start visible */
   1970 					win.mode |= MODE_BLINK;
   1971 				win.mode ^= MODE_BLINK;
   1972 				tsetdirtattr(ATTR_BLINK);
   1973 				lastblink = now;
   1974 				timeout = blinktimeout;
   1975 			}
   1976 		}
   1977 
   1978 		draw();
   1979 		XFlush(xw.dpy);
   1980 		drawing = 0;
   1981 	}
   1982 }
   1983 
   1984 void
   1985 usage(void)
   1986 {
   1987 	die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
   1988 	    " [-n name] [-o file]\n"
   1989 	    "          [-T title] [-t title] [-w windowid]"
   1990 	    " [[-e] command [args ...]]\n"
   1991 	    "       %s [-aiv] [-c class] [-f font] [-g geometry]"
   1992 	    " [-n name] [-o file]\n"
   1993 	    "          [-T title] [-t title] [-w windowid] -l line"
   1994 	    " [stty_args ...]\n", argv0, argv0);
   1995 }
   1996 
   1997 int
   1998 main(int argc, char *argv[])
   1999 {
   2000 	xw.l = xw.t = 0;
   2001 	xw.isfixed = False;
   2002 	xsetcursor(cursorshape);
   2003 
   2004 	ARGBEGIN {
   2005 	case 'a':
   2006 		allowaltscreen = 0;
   2007 		break;
   2008 	case 'c':
   2009 		opt_class = EARGF(usage());
   2010 		break;
   2011 	case 'e':
   2012 		if (argc > 0)
   2013 			--argc, ++argv;
   2014 		goto run;
   2015 	case 'f':
   2016 		opt_font = EARGF(usage());
   2017 		break;
   2018 	case 'g':
   2019 		xw.gm = XParseGeometry(EARGF(usage()),
   2020 				&xw.l, &xw.t, &cols, &rows);
   2021 		break;
   2022 	case 'i':
   2023 		xw.isfixed = 1;
   2024 		break;
   2025 	case 'o':
   2026 		opt_io = EARGF(usage());
   2027 		break;
   2028 	case 'l':
   2029 		opt_line = EARGF(usage());
   2030 		break;
   2031 	case 'n':
   2032 		opt_name = EARGF(usage());
   2033 		break;
   2034 	case 't':
   2035 	case 'T':
   2036 		opt_title = EARGF(usage());
   2037 		break;
   2038 	case 'w':
   2039 		opt_embed = EARGF(usage());
   2040 		break;
   2041 	case 'v':
   2042 		die("%s " VERSION "\n", argv0);
   2043 		break;
   2044 	default:
   2045 		usage();
   2046 	} ARGEND;
   2047 
   2048 run:
   2049 	if (argc > 0) /* eat all remaining arguments */
   2050 		opt_cmd = argv;
   2051 
   2052 	if (!opt_title)
   2053 		opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0];
   2054 
   2055 	setlocale(LC_CTYPE, "");
   2056 	XSetLocaleModifiers("");
   2057 	cols = MAX(cols, 1);
   2058 	rows = MAX(rows, 1);
   2059 	tnew(cols, rows);
   2060 	xinit(cols, rows);
   2061 	xsetenv();
   2062 	selinit();
   2063 	run();
   2064 
   2065 	return 0;
   2066 }