st

Suckless terminal
git clone https://git.jamzattack.xyz/st
Log | Files | Refs | README | LICENSE

x.c (48059B)


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