dotfiles

Configs for programs I use!
git clone git://shipwreckt.co.uk/dotfiles.git
Log | Files | Refs | README | LICENSE

x.c (48665B)


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