birdterm

birdterm - mrgrouse's fork of suckless' st.
Log | Files | Refs | README | LICENSE

x.c (52254B)


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