x.c (48554B) - raw


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