dotfiles

personal configuration files and scripts
git clone https://tongong.net/git/dotfiles.git
Log | Files | Refs | README

x.c (47639B)


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