st

my customized branch of st
git clone git://git.jakekoroman.com/st
Log | Files | Refs | README | LICENSE

x.c (49335B)


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