st

my build of st
git clone git://git.ckyln.com/st
Log | Files | Refs | README | LICENSE

x.c (48178B)


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