st

Personal fork of the suckless terminal.
git clone git://git.zepp.club/st.git
Log | Files | Refs | README | LICENSE

x.c (49274B)


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