st

fork of st
git clone git://popovic.xyz/st.git
Log | Files | Refs | README | LICENSE

x.c (53012B)


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