st

my st build
Log | Files | Refs | README | LICENSE

x.c (48840B)


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