st

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

x.c (49401B)


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