st

My fork of st.
git clone git://git.alex.balgavy.eu/st.git
Log | Files | Refs | README | LICENSE

x.c (51357B)


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