st

Suckless' st with my personal patches
git clone https://git.instinctive.eu/st.git
Log | Files | Refs | README | LICENSE

x.c (52669B)


      1 /* See LICENSE for license details. */
      2 #include <errno.h>
      3 #include <math.h>
      4 #include <limits.h>
      5 #include <locale.h>
      6 #include <signal.h>
      7 #include <sys/select.h>
      8 #include <time.h>
      9 #include <unistd.h>
     10 #include <libgen.h>
     11 #include <X11/Xatom.h>
     12 #include <X11/Xlib.h>
     13 #include <X11/cursorfont.h>
     14 #include <X11/keysym.h>
     15 #include <X11/Xft/Xft.h>
     16 #include <X11/XKBlib.h>
     17 
     18 char *argv0;
     19 #include "arg.h"
     20 #include "st.h"
     21 #include "win.h"
     22 
     23 /* types used in config.h */
     24 typedef struct {
     25 	uint mod;
     26 	KeySym keysym;
     27 	void (*func)(const Arg *);
     28 	const Arg arg;
     29 } Shortcut;
     30 
     31 typedef struct {
     32 	uint mod;
     33 	uint button;
     34 	void (*func)(const Arg *);
     35 	const Arg arg;
     36 	uint  release;
     37 } MouseShortcut;
     38 
     39 typedef struct {
     40 	KeySym k;
     41 	uint mask;
     42 	char *s;
     43 	/* three-valued logic variables: 0 indifferent, 1 on, -1 off */
     44 	signed char appkey;    /* application keypad */
     45 	signed char appcursor; /* application cursor */
     46 } Key;
     47 
     48 /* X modifiers */
     49 #define XK_ANY_MOD    UINT_MAX
     50 #define XK_NO_MOD     0
     51 #define XK_SWITCH_MOD (1<<13|1<<14)
     52 
     53 /* function definitions used in config.h */
     54 static void clipcopy(const Arg *);
     55 static void clippaste(const Arg *);
     56 static void insert_debug(const Arg *);
     57 static void numlock(const Arg *);
     58 static void selpaste(const Arg *);
     59 static void zoom(const Arg *);
     60 static void zoomabs(const Arg *);
     61 static void zoomreset(const Arg *);
     62 static void ttysend(const Arg *);
     63 static void modscheme(const Arg *);
     64 static void nextscheme(const Arg *);
     65 static void selectscheme(const Arg *);
     66 
     67 /* config.h for applying patches and the configuration. */
     68 #include "config.h"
     69 
     70 /* XEMBED messages */
     71 #define XEMBED_FOCUS_IN  4
     72 #define XEMBED_FOCUS_OUT 5
     73 
     74 /* macros */
     75 #define IS_SET(flag)		((win.mode & (flag)) != 0)
     76 #define TRUERED(x)		(((x) & 0xff0000) >> 8)
     77 #define TRUEGREEN(x)		(((x) & 0xff00))
     78 #define TRUEBLUE(x)		(((x) & 0xff) << 8)
     79 
     80 typedef XftDraw *Draw;
     81 typedef XftColor Color;
     82 typedef XftGlyphFontSpec GlyphFontSpec;
     83 
     84 /* Purely graphic info */
     85 typedef struct {
     86 	int tw, th; /* tty width and height */
     87 	int w, h; /* window width and height */
     88 	int hborderpx, vborderpx;
     89 	int ch; /* char height */
     90 	int cw; /* char width  */
     91 	int mode; /* window state/mode flags */
     92 	int cursor; /* cursor style */
     93 } TermWindow;
     94 
     95 typedef struct {
     96 	Display *dpy;
     97 	Colormap cmap;
     98 	Window win;
     99 	Drawable buf;
    100 	GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
    101 	Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid;
    102 	struct {
    103 		XIM xim;
    104 		XIC xic;
    105 		XPoint spot;
    106 		XVaNestedList spotlist;
    107 	} ime;
    108 	Draw draw;
    109 	Visual *vis;
    110 	XSetWindowAttributes attrs;
    111 	int scr;
    112 	int isfixed; /* is fixed geometry? */
    113 	int l, t; /* left and top offset */
    114 	int gm; /* geometry mask */
    115 } XWindow;
    116 
    117 typedef struct {
    118 	Atom xtarget;
    119 	char *primary, *clipboard;
    120 	struct timespec tclick1;
    121 	struct timespec tclick2;
    122 } XSelection;
    123 
    124 /* Font structure */
    125 #define Font Font_
    126 typedef struct {
    127 	int height;
    128 	int width;
    129 	int ascent;
    130 	int descent;
    131 	int badslant;
    132 	int badweight;
    133 	short lbearing;
    134 	short rbearing;
    135 	XftFont *match;
    136 	FcFontSet *set;
    137 	FcPattern *pattern;
    138 } Font;
    139 
    140 /* Drawing Context */
    141 typedef struct {
    142 	Color *col;
    143 	size_t collen;
    144 	Font font, bfont, ifont, ibfont;
    145 	GC gc;
    146 } DC;
    147 
    148 static inline ushort sixd_to_16bit(int);
    149 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
    150 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
    151 static void xdrawglyph(Glyph, int, int);
    152 static void xclear(int, int, int, int);
    153 static int xgeommasktogravity(int);
    154 static int ximopen(Display *);
    155 static void ximinstantiate(Display *, XPointer, XPointer);
    156 static void ximdestroy(XIM, XPointer, XPointer);
    157 static int xicdestroy(XIC, XPointer, XPointer);
    158 static void xinit(int, int);
    159 static void cresize(int, int);
    160 static void xresize(int, int);
    161 static void xhints(void);
    162 static int xloadcolor(int, const char *, Color *);
    163 static int xloadfont(Font *, FcPattern *);
    164 static void xloadfonts(const char *, double);
    165 static int xloadsparefont(FcPattern *, int);
    166 static void xloadsparefonts(void);
    167 static void xunloadfont(Font *);
    168 static void xunloadfonts(void);
    169 static void xsetenv(void);
    170 static void xseturgency(int);
    171 static int evcol(XEvent *);
    172 static int evrow(XEvent *);
    173 
    174 static void expose(XEvent *);
    175 static void visibility(XEvent *);
    176 static void unmap(XEvent *);
    177 static void kpress(XEvent *);
    178 static void cmessage(XEvent *);
    179 static void resize(XEvent *);
    180 static void focus(XEvent *);
    181 static uint buttonmask(uint);
    182 static int mouseaction(XEvent *, uint);
    183 static void brelease(XEvent *);
    184 static void bpress(XEvent *);
    185 static void bmotion(XEvent *);
    186 static void propnotify(XEvent *);
    187 static void selnotify(XEvent *);
    188 static void selclear_(XEvent *);
    189 static void selrequest(XEvent *);
    190 static void setsel(char *, Time);
    191 static void mousesel(XEvent *, int);
    192 static void mousereport(XEvent *);
    193 static char *kmap(KeySym, uint);
    194 static int match(uint, uint);
    195 static void updatescheme(void);
    196 
    197 static void run(void);
    198 static void usage(void);
    199 
    200 static void (*handler[LASTEvent])(XEvent *) = {
    201 	[KeyPress] = kpress,
    202 	[ClientMessage] = cmessage,
    203 	[ConfigureNotify] = resize,
    204 	[VisibilityNotify] = visibility,
    205 	[UnmapNotify] = unmap,
    206 	[Expose] = expose,
    207 	[FocusIn] = focus,
    208 	[FocusOut] = focus,
    209 	[MotionNotify] = bmotion,
    210 	[ButtonPress] = bpress,
    211 	[ButtonRelease] = brelease,
    212 /*
    213  * Uncomment if you want the selection to disappear when you select something
    214  * different in another window.
    215  */
    216 	[SelectionClear] = selclear_,
    217 	[SelectionNotify] = selnotify,
    218 /*
    219  * PropertyNotify is only turned on when there is some INCR transfer happening
    220  * for the selection retrieval.
    221  */
    222 	[PropertyNotify] = propnotify,
    223 	[SelectionRequest] = selrequest,
    224 };
    225 
    226 /* Globals */
    227 static DC dc;
    228 static XWindow xw;
    229 static XSelection xsel;
    230 static TermWindow win;
    231 
    232 /* Font Ring Cache */
    233 enum {
    234 	FRC_NORMAL,
    235 	FRC_ITALIC,
    236 	FRC_BOLD,
    237 	FRC_ITALICBOLD
    238 };
    239 
    240 typedef struct {
    241 	XftFont *font;
    242 	int flags;
    243 	Rune unicodep;
    244 } Fontcache;
    245 
    246 /* Fontcache is an array now. A new font will be appended to the array. */
    247 static Fontcache *frc = NULL;
    248 static int frclen = 0;
    249 static int frccap = 0;
    250 static char *usedfont = NULL;
    251 static double usedfontsize = 0;
    252 static double defaultfontsize = 0;
    253 
    254 static char *opt_class = NULL;
    255 static char **opt_cmd  = NULL;
    256 static char *opt_embed = NULL;
    257 static char *opt_font  = NULL;
    258 static char *opt_io    = NULL;
    259 static char *opt_line  = NULL;
    260 static char *opt_name  = NULL;
    261 static char *opt_scale = NULL;
    262 static char *opt_title = NULL;
    263 
    264 static uint buttons; /* bit field of pressed buttons */
    265 
    266 void
    267 clipcopy(const Arg *dummy)
    268 {
    269 	Atom clipboard;
    270 
    271 	free(xsel.clipboard);
    272 	xsel.clipboard = NULL;
    273 
    274 	if (xsel.primary != NULL) {
    275 		xsel.clipboard = xstrdup(xsel.primary);
    276 		clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    277 		XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
    278 	}
    279 }
    280 
    281 void
    282 clippaste(const Arg *dummy)
    283 {
    284 	Atom clipboard;
    285 
    286 	clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    287 	XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
    288 			xw.win, CurrentTime);
    289 }
    290 
    291 void
    292 selpaste(const Arg *dummy)
    293 {
    294 	XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
    295 			xw.win, CurrentTime);
    296 }
    297 
    298 void
    299 insert_debug(const Arg *dummy)
    300 {
    301 	char buffer[160];
    302 	int n;
    303 
    304 	n = snprintf(buffer, sizeof buffer,
    305 	    "Cell %dx%d from ascent %d, descent %d, height %d, width %d,"
    306 	    " fontsize %lf, defaultfontsize %lf\n",
    307 	    win.cw, win.ch,
    308 	    dc.font.ascent, dc.font.descent, dc.font.match->height,
    309 	    dc.font.width, usedfontsize, defaultfontsize);
    310 
    311 	if (n > 0)
    312 		ttywrite(buffer, n, 1);
    313 }
    314 
    315 void
    316 numlock(const Arg *dummy)
    317 {
    318 	win.mode ^= MODE_NUMLOCK;
    319 }
    320 
    321 void
    322 zoom(const Arg *arg)
    323 {
    324 	Arg larg;
    325 
    326 	larg.f = usedfontsize + arg->f;
    327 	zoomabs(&larg);
    328 }
    329 
    330 void
    331 zoomabs(const Arg *arg)
    332 {
    333 	xunloadfonts();
    334 	xloadfonts(usedfont, arg->f);
    335 	xloadsparefonts();
    336 	cresize(0, 0);
    337 	redraw();
    338 	xhints();
    339 }
    340 
    341 void
    342 zoomreset(const Arg *arg)
    343 {
    344 	Arg larg;
    345 
    346 	if (defaultfontsize > 0) {
    347 		larg.f = defaultfontsize;
    348 		zoomabs(&larg);
    349 	}
    350 }
    351 
    352 void
    353 ttysend(const Arg *arg)
    354 {
    355 	ttywrite(arg->s, strlen(arg->s), 1);
    356 }
    357 
    358 int
    359 evcol(XEvent *e)
    360 {
    361 	int x = e->xbutton.x - win.hborderpx;
    362 	LIMIT(x, 0, win.tw - 1);
    363 	return x / win.cw;
    364 }
    365 
    366 int
    367 evrow(XEvent *e)
    368 {
    369 	int y = e->xbutton.y - win.vborderpx;
    370 	LIMIT(y, 0, win.th - 1);
    371 	return y / win.ch;
    372 }
    373 
    374 void
    375 mousesel(XEvent *e, int done)
    376 {
    377 	int type, seltype = SEL_REGULAR;
    378 	uint state = e->xbutton.state & ~(Button1Mask | forcemousemod);
    379 
    380 	for (type = 1; type < LEN(selmasks); ++type) {
    381 		if (match(selmasks[type], state)) {
    382 			seltype = type;
    383 			break;
    384 		}
    385 	}
    386 	selextend(evcol(e), evrow(e), seltype, done);
    387 	if (done)
    388 		setsel(getsel(), e->xbutton.time);
    389 }
    390 
    391 void
    392 mousereport(XEvent *e)
    393 {
    394 	int len, btn, code;
    395 	int x = evcol(e), y = evrow(e);
    396 	int state = e->xbutton.state;
    397 	char buf[40];
    398 	static int ox, oy;
    399 
    400 	if (e->type == MotionNotify) {
    401 		if (x == ox && y == oy)
    402 			return;
    403 		if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
    404 			return;
    405 		/* MODE_MOUSEMOTION: no reporting if no button is pressed */
    406 		if (IS_SET(MODE_MOUSEMOTION) && buttons == 0)
    407 			return;
    408 		/* Set btn to lowest-numbered pressed button, or 12 if no
    409 		 * buttons are pressed. */
    410 		for (btn = 1; btn <= 11 && !(buttons & (1<<(btn-1))); btn++)
    411 			;
    412 		code = 32;
    413 	} else {
    414 		btn = e->xbutton.button;
    415 		/* Only buttons 1 through 11 can be encoded */
    416 		if (btn < 1 || btn > 11)
    417 			return;
    418 		if (e->type == ButtonRelease) {
    419 			/* MODE_MOUSEX10: no button release reporting */
    420 			if (IS_SET(MODE_MOUSEX10))
    421 				return;
    422 			/* Don't send release events for the scroll wheel */
    423 			if (btn == 4 || btn == 5)
    424 				return;
    425 		}
    426 		code = 0;
    427 	}
    428 
    429 	ox = x;
    430 	oy = y;
    431 
    432 	/* Encode btn into code. If no button is pressed for a motion event in
    433 	 * MODE_MOUSEMANY, then encode it as a release. */
    434 	if ((!IS_SET(MODE_MOUSESGR) && e->type == ButtonRelease) || btn == 12)
    435 		code += 3;
    436 	else if (btn >= 8)
    437 		code += 128 + btn - 8;
    438 	else if (btn >= 4)
    439 		code += 64 + btn - 4;
    440 	else
    441 		code += btn - 1;
    442 
    443 	if (!IS_SET(MODE_MOUSEX10)) {
    444 		code += ((state & ShiftMask  ) ?  4 : 0)
    445 		      + ((state & Mod1Mask   ) ?  8 : 0) /* meta key: alt */
    446 		      + ((state & ControlMask) ? 16 : 0);
    447 	}
    448 
    449 	if (IS_SET(MODE_MOUSESGR)) {
    450 		len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
    451 				code, x+1, y+1,
    452 				e->type == ButtonRelease ? 'm' : 'M');
    453 	} else if (x < 223 && y < 223) {
    454 		len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
    455 				32+code, 32+x+1, 32+y+1);
    456 	} else {
    457 		return;
    458 	}
    459 
    460 	ttywrite(buf, len, 0);
    461 }
    462 
    463 uint
    464 buttonmask(uint button)
    465 {
    466 	return button == Button1 ? Button1Mask
    467 	     : button == Button2 ? Button2Mask
    468 	     : button == Button3 ? Button3Mask
    469 	     : button == Button4 ? Button4Mask
    470 	     : button == Button5 ? Button5Mask
    471 	     : 0;
    472 }
    473 
    474 int
    475 mouseaction(XEvent *e, uint release)
    476 {
    477 	MouseShortcut *ms;
    478 
    479 	/* ignore Button<N>mask for Button<N> - it's set on release */
    480 	uint state = e->xbutton.state & ~buttonmask(e->xbutton.button);
    481 
    482 	for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
    483 		if (ms->release == release &&
    484 		    ms->button == e->xbutton.button &&
    485 		    (match(ms->mod, state) ||  /* exact or forced */
    486 		     match(ms->mod, state & ~forcemousemod))) {
    487 			ms->func(&(ms->arg));
    488 			return 1;
    489 		}
    490 	}
    491 
    492 	return 0;
    493 }
    494 
    495 void
    496 bpress(XEvent *e)
    497 {
    498 	int btn = e->xbutton.button;
    499 	struct timespec now;
    500 	int snap;
    501 
    502 	if (1 <= btn && btn <= 11)
    503 		buttons |= 1 << (btn-1);
    504 
    505 	if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    506 		mousereport(e);
    507 		return;
    508 	}
    509 
    510 	if (mouseaction(e, 0))
    511 		return;
    512 
    513 	if (btn == Button1) {
    514 		/*
    515 		 * If the user clicks below predefined timeouts specific
    516 		 * snapping behaviour is exposed.
    517 		 */
    518 		clock_gettime(CLOCK_MONOTONIC, &now);
    519 		if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
    520 			snap = SNAP_LINE;
    521 		} else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
    522 			snap = SNAP_WORD;
    523 		} else {
    524 			snap = 0;
    525 		}
    526 		xsel.tclick2 = xsel.tclick1;
    527 		xsel.tclick1 = now;
    528 
    529 		selstart(evcol(e), evrow(e), snap);
    530 	}
    531 }
    532 
    533 void
    534 propnotify(XEvent *e)
    535 {
    536 	XPropertyEvent *xpev;
    537 	Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    538 
    539 	xpev = &e->xproperty;
    540 	if (xpev->state == PropertyNewValue &&
    541 			(xpev->atom == XA_PRIMARY ||
    542 			 xpev->atom == clipboard)) {
    543 		selnotify(e);
    544 	}
    545 }
    546 
    547 void
    548 selnotify(XEvent *e)
    549 {
    550 	ulong nitems, ofs, rem;
    551 	int format;
    552 	uchar *data, *last, *repl;
    553 	Atom type, incratom, property = None;
    554 
    555 	incratom = XInternAtom(xw.dpy, "INCR", 0);
    556 
    557 	ofs = 0;
    558 	if (e->type == SelectionNotify)
    559 		property = e->xselection.property;
    560 	else if (e->type == PropertyNotify)
    561 		property = e->xproperty.atom;
    562 
    563 	if (property == None)
    564 		return;
    565 
    566 	do {
    567 		if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
    568 					BUFSIZ/4, False, AnyPropertyType,
    569 					&type, &format, &nitems, &rem,
    570 					&data)) {
    571 			fprintf(stderr, "Clipboard allocation failed\n");
    572 			return;
    573 		}
    574 
    575 		if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
    576 			/*
    577 			 * If there is some PropertyNotify with no data, then
    578 			 * this is the signal of the selection owner that all
    579 			 * data has been transferred. We won't need to receive
    580 			 * PropertyNotify events anymore.
    581 			 */
    582 			MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
    583 			XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
    584 					&xw.attrs);
    585 		}
    586 
    587 		if (type == incratom) {
    588 			/*
    589 			 * Activate the PropertyNotify events so we receive
    590 			 * when the selection owner does send us the next
    591 			 * chunk of data.
    592 			 */
    593 			MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
    594 			XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
    595 					&xw.attrs);
    596 
    597 			/*
    598 			 * Deleting the property is the transfer start signal.
    599 			 */
    600 			XDeleteProperty(xw.dpy, xw.win, (int)property);
    601 			continue;
    602 		}
    603 
    604 		/*
    605 		 * As seen in getsel:
    606 		 * Line endings are inconsistent in the terminal and GUI world
    607 		 * copy and pasting. When receiving some selection data,
    608 		 * replace all '\n' with '\r'.
    609 		 * FIXME: Fix the computer world.
    610 		 */
    611 		repl = data;
    612 		last = data + nitems * format / 8;
    613 		while ((repl = memchr(repl, '\n', last - repl))) {
    614 			*repl++ = '\r';
    615 		}
    616 
    617 		if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
    618 			ttywrite("\033[200~", 6, 0);
    619 		ttywrite((char *)data, nitems * format / 8, 1);
    620 		if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
    621 			ttywrite("\033[201~", 6, 0);
    622 		XFree(data);
    623 		/* number of 32-bit chunks returned */
    624 		ofs += nitems * format / 32;
    625 	} while (rem > 0);
    626 
    627 	/*
    628 	 * Deleting the property again tells the selection owner to send the
    629 	 * next data chunk in the property.
    630 	 */
    631 	XDeleteProperty(xw.dpy, xw.win, (int)property);
    632 }
    633 
    634 void
    635 xclipcopy(void)
    636 {
    637 	clipcopy(NULL);
    638 }
    639 
    640 void
    641 selclear_(XEvent *e)
    642 {
    643 	selclear();
    644 }
    645 
    646 void
    647 selrequest(XEvent *e)
    648 {
    649 	XSelectionRequestEvent *xsre;
    650 	XSelectionEvent xev;
    651 	Atom xa_targets, string, clipboard;
    652 	char *seltext;
    653 
    654 	xsre = (XSelectionRequestEvent *) e;
    655 	xev.type = SelectionNotify;
    656 	xev.requestor = xsre->requestor;
    657 	xev.selection = xsre->selection;
    658 	xev.target = xsre->target;
    659 	xev.time = xsre->time;
    660 	if (xsre->property == None)
    661 		xsre->property = xsre->target;
    662 
    663 	/* reject */
    664 	xev.property = None;
    665 
    666 	xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
    667 	if (xsre->target == xa_targets) {
    668 		/* respond with the supported type */
    669 		string = xsel.xtarget;
    670 		XChangeProperty(xsre->display, xsre->requestor, xsre->property,
    671 				XA_ATOM, 32, PropModeReplace,
    672 				(uchar *) &string, 1);
    673 		xev.property = xsre->property;
    674 	} else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
    675 		/*
    676 		 * xith XA_STRING non ascii characters may be incorrect in the
    677 		 * requestor. It is not our problem, use utf8.
    678 		 */
    679 		clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    680 		if (xsre->selection == XA_PRIMARY) {
    681 			seltext = xsel.primary;
    682 		} else if (xsre->selection == clipboard) {
    683 			seltext = xsel.clipboard;
    684 		} else {
    685 			fprintf(stderr,
    686 				"Unhandled clipboard selection 0x%lx\n",
    687 				xsre->selection);
    688 			return;
    689 		}
    690 		if (seltext != NULL) {
    691 			XChangeProperty(xsre->display, xsre->requestor,
    692 					xsre->property, xsre->target,
    693 					8, PropModeReplace,
    694 					(uchar *)seltext, strlen(seltext));
    695 			xev.property = xsre->property;
    696 		}
    697 	}
    698 
    699 	/* all done, send a notification to the listener */
    700 	if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
    701 		fprintf(stderr, "Error sending SelectionNotify event\n");
    702 }
    703 
    704 void
    705 setsel(char *str, Time t)
    706 {
    707 	if (!str)
    708 		return;
    709 
    710 	free(xsel.primary);
    711 	xsel.primary = str;
    712 
    713 	XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
    714 	if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
    715 		selclear();
    716 }
    717 
    718 void
    719 xsetsel(char *str)
    720 {
    721 	setsel(str, CurrentTime);
    722 }
    723 
    724 void
    725 brelease(XEvent *e)
    726 {
    727 	int btn = e->xbutton.button;
    728 
    729 	if (1 <= btn && btn <= 11)
    730 		buttons &= ~(1 << (btn-1));
    731 
    732 	if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    733 		mousereport(e);
    734 		return;
    735 	}
    736 
    737 	if (mouseaction(e, 1))
    738 		return;
    739 	if (btn == Button1)
    740 		mousesel(e, 1);
    741 }
    742 
    743 void
    744 bmotion(XEvent *e)
    745 {
    746 	if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    747 		mousereport(e);
    748 		return;
    749 	}
    750 
    751 	mousesel(e, 0);
    752 }
    753 
    754 void
    755 cresize(int width, int height)
    756 {
    757 	int col, row;
    758 
    759 	if (width != 0)
    760 		win.w = width;
    761 	if (height != 0)
    762 		win.h = height;
    763 
    764 	col = (win.w - 2 * borderpx) / win.cw;
    765 	row = (win.h - 2 * borderpx) / win.ch;
    766 	col = MAX(1, col);
    767 	row = MAX(1, row);
    768 
    769 	win.hborderpx = (win.w - col * win.cw) / 2;
    770 	win.vborderpx = (win.h - row * win.ch) / 2;
    771 
    772 	tresize(col, row);
    773 	xresize(col, row);
    774 	ttyresize(win.tw, win.th);
    775 }
    776 
    777 void
    778 xresize(int col, int row)
    779 {
    780 	win.tw = col * win.cw;
    781 	win.th = row * win.ch;
    782 
    783 	XFreePixmap(xw.dpy, xw.buf);
    784 	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
    785 			DefaultDepth(xw.dpy, xw.scr));
    786 	XftDrawChange(xw.draw, xw.buf);
    787 	xclear(0, 0, win.w, win.h);
    788 
    789 	/* resize to new width */
    790 	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
    791 }
    792 
    793 ushort
    794 sixd_to_16bit(int x)
    795 {
    796 	return x == 0 ? 0 : 0x3737 + 0x2828 * x;
    797 }
    798 
    799 int
    800 xloadcolor(int i, const char *name, Color *ncolor)
    801 {
    802 	XRenderColor color = { .alpha = 0xffff };
    803 
    804 	if (!name) {
    805 		if (BETWEEN(i, 16, 255)) { /* 256 color */
    806 			if (i < 6*6*6+16) { /* same colors as xterm */
    807 				color.red   = sixd_to_16bit( ((i-16)/36)%6 );
    808 				color.green = sixd_to_16bit( ((i-16)/6) %6 );
    809 				color.blue  = sixd_to_16bit( ((i-16)/1) %6 );
    810 			} else { /* greyscale */
    811 				color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
    812 				color.green = color.blue = color.red;
    813 			}
    814 			return XftColorAllocValue(xw.dpy, xw.vis,
    815 			                          xw.cmap, &color, ncolor);
    816 		} else
    817 			name = colorname[i];
    818 	}
    819 
    820 	return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
    821 }
    822 
    823 void
    824 xloadcols(void)
    825 {
    826 	int i;
    827 	static int loaded;
    828 	Color *cp;
    829 
    830 	if (loaded) {
    831 		for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
    832 			XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
    833 	} else {
    834 		dc.collen = 260;
    835 		dc.col = xmalloc(dc.collen * sizeof(Color));
    836 	}
    837 
    838 	for (i = 0; i < dc.collen; i++)
    839 		if (!xloadcolor(i, NULL, &dc.col[i])) {
    840 			if (colorname[i])
    841 				die("could not allocate color '%s'\n", colorname[i]);
    842 			else
    843 				die("could not allocate color %d\n", i);
    844 		}
    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 : termname,
    893 	                    opt_class ? opt_class : termname};
    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, root;
   1260 	pid_t thispid = getpid();
   1261 	XColor xmousefg, xmousebg;
   1262 
   1263 	if (!(xw.dpy = XOpenDisplay(NULL)))
   1264 		die("can't open display\n");
   1265 	xw.scr = XDefaultScreen(xw.dpy);
   1266 	xw.vis = XDefaultVisual(xw.dpy, xw.scr);
   1267 
   1268 	/* font */
   1269 	if (!FcInit())
   1270 		die("could not init fontconfig.\n");
   1271 
   1272 	usedfont = (opt_font == NULL)? font : opt_font;
   1273 	xloadfonts(usedfont, 0);
   1274 
   1275 	/* spare fonts */
   1276 	xloadsparefonts();
   1277 
   1278 	/* colors */
   1279 	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
   1280 	xloadcols();
   1281 
   1282 	/* adjust fixed window geometry */
   1283 	win.w = 2 * win.hborderpx + 2 * borderpx + cols * win.cw;
   1284 	win.h = 2 * win.vborderpx + 2 * borderpx + rows * win.ch;
   1285 	if (xw.gm & XNegative)
   1286 		xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
   1287 	if (xw.gm & YNegative)
   1288 		xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
   1289 
   1290 	/* Events */
   1291 	xw.attrs.background_pixel = dc.col[defaultbg].pixel;
   1292 	xw.attrs.border_pixel = dc.col[defaultbg].pixel;
   1293 	xw.attrs.bit_gravity = NorthWestGravity;
   1294 	xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
   1295 		| ExposureMask | VisibilityChangeMask | StructureNotifyMask
   1296 		| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
   1297 	xw.attrs.colormap = xw.cmap;
   1298 
   1299 	root = XRootWindow(xw.dpy, xw.scr);
   1300 	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
   1301 		parent = root;
   1302 	xw.win = XCreateWindow(xw.dpy, root, xw.l, xw.t,
   1303 			win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
   1304 			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
   1305 			| CWEventMask | CWColormap, &xw.attrs);
   1306 	if (parent != root)
   1307 		XReparentWindow(xw.dpy, xw.win, parent, xw.l, xw.t);
   1308 
   1309 	memset(&gcvalues, 0, sizeof(gcvalues));
   1310 	gcvalues.graphics_exposures = False;
   1311 	dc.gc = XCreateGC(xw.dpy, xw.win, GCGraphicsExposures,
   1312 			&gcvalues);
   1313 	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
   1314 			DefaultDepth(xw.dpy, xw.scr));
   1315 	XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
   1316 	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
   1317 
   1318 	/* font spec buffer */
   1319 	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
   1320 
   1321 	/* Xft rendering context */
   1322 	xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
   1323 
   1324 	/* input methods */
   1325 	if (!ximopen(xw.dpy)) {
   1326 		XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1327 	                                       ximinstantiate, NULL);
   1328 	}
   1329 
   1330 	/* white cursor, black outline */
   1331 	cursor = XCreateFontCursor(xw.dpy, mouseshape);
   1332 	XDefineCursor(xw.dpy, xw.win, cursor);
   1333 
   1334 	if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
   1335 		xmousefg.red   = 0xffff;
   1336 		xmousefg.green = 0xffff;
   1337 		xmousefg.blue  = 0xffff;
   1338 	}
   1339 
   1340 	if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
   1341 		xmousebg.red   = 0x0000;
   1342 		xmousebg.green = 0x0000;
   1343 		xmousebg.blue  = 0x0000;
   1344 	}
   1345 
   1346 	XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
   1347 
   1348 	xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
   1349 	xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
   1350 	xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
   1351 	xw.netwmiconname = XInternAtom(xw.dpy, "_NET_WM_ICON_NAME", False);
   1352 	XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
   1353 
   1354 	xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
   1355 	XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
   1356 			PropModeReplace, (uchar *)&thispid, 1);
   1357 
   1358 	win.mode = MODE_NUMLOCK;
   1359 	resettitle();
   1360 	xhints();
   1361 	XMapWindow(xw.dpy, xw.win);
   1362 	XSync(xw.dpy, False);
   1363 
   1364 	clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
   1365 	clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
   1366 	xsel.primary = NULL;
   1367 	xsel.clipboard = NULL;
   1368 	xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
   1369 	if (xsel.xtarget == None)
   1370 		xsel.xtarget = XA_STRING;
   1371 
   1372 	boxdraw_xinit(xw.dpy, xw.cmap, xw.draw, xw.vis);
   1373 }
   1374 
   1375 int
   1376 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
   1377 {
   1378 	float winx = win.hborderpx + x * win.cw, winy = win.vborderpx + y * win.ch, xp, yp;
   1379 	ushort mode, prevmode = USHRT_MAX;
   1380 	Font *font = &dc.font;
   1381 	int frcflags = FRC_NORMAL;
   1382 	float runewidth = win.cw;
   1383 	Rune rune;
   1384 	FT_UInt glyphidx;
   1385 	FcResult fcres;
   1386 	FcPattern *fcpattern, *fontpattern;
   1387 	FcFontSet *fcsets[] = { NULL };
   1388 	FcCharSet *fccharset;
   1389 	int i, f, numspecs = 0;
   1390 
   1391 	for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
   1392 		/* Fetch rune and mode for current glyph. */
   1393 		rune = glyphs[i].u;
   1394 		mode = glyphs[i].mode;
   1395 
   1396 		/* Skip dummy wide-character spacing. */
   1397 		if (mode == ATTR_WDUMMY)
   1398 			continue;
   1399 
   1400 		/* Determine font for glyph if different from previous glyph. */
   1401 		if (prevmode != mode) {
   1402 			prevmode = mode;
   1403 			font = &dc.font;
   1404 			frcflags = FRC_NORMAL;
   1405 			runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
   1406 			if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
   1407 				font = &dc.ibfont;
   1408 				frcflags = FRC_ITALICBOLD;
   1409 			} else if (mode & ATTR_ITALIC) {
   1410 				font = &dc.ifont;
   1411 				frcflags = FRC_ITALIC;
   1412 			} else if (mode & ATTR_BOLD) {
   1413 				font = &dc.bfont;
   1414 				frcflags = FRC_BOLD;
   1415 			}
   1416 			yp = winy + font->ascent;
   1417 		}
   1418 
   1419 		if (mode & ATTR_BOXDRAW) {
   1420 			/* minor shoehorning: boxdraw uses only this ushort */
   1421 			glyphidx = boxdrawindex(&glyphs[i]);
   1422 		} else {
   1423 			/* Lookup character index with default font. */
   1424 			glyphidx = XftCharIndex(xw.dpy, font->match, rune);
   1425 		}
   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 			FcPatternAddBool(fcpattern, FC_COLOR, 0);
   1471 
   1472 			FcConfigSubstitute(0, fcpattern,
   1473 					FcMatchPattern);
   1474 			FcDefaultSubstitute(fcpattern);
   1475 
   1476 			fontpattern = FcFontSetMatch(0, fcsets, 1,
   1477 					fcpattern, &fcres);
   1478 
   1479 			/* Allocate memory for the new cache entry. */
   1480 			if (frclen >= frccap) {
   1481 				frccap += 16;
   1482 				frc = xrealloc(frc, frccap * sizeof(Fontcache));
   1483 			}
   1484 
   1485 			frc[frclen].font = XftFontOpenPattern(xw.dpy,
   1486 					fontpattern);
   1487 			if (!frc[frclen].font)
   1488 				die("XftFontOpenPattern failed seeking fallback font: %s\n",
   1489 					strerror(errno));
   1490 			frc[frclen].flags = frcflags;
   1491 			frc[frclen].unicodep = rune;
   1492 
   1493 			glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
   1494 
   1495 			f = frclen;
   1496 			frclen++;
   1497 
   1498 			FcPatternDestroy(fcpattern);
   1499 			FcCharSetDestroy(fccharset);
   1500 		}
   1501 
   1502 		specs[numspecs].font = frc[f].font;
   1503 		specs[numspecs].glyph = glyphidx;
   1504 		specs[numspecs].x = (short)xp;
   1505 		specs[numspecs].y = (short)yp;
   1506 		xp += runewidth;
   1507 		numspecs++;
   1508 	}
   1509 
   1510 	return numspecs;
   1511 }
   1512 
   1513 void
   1514 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
   1515 {
   1516 	int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
   1517 	int winx = win.hborderpx + x * win.cw, winy = win.vborderpx + y * win.ch,
   1518 	    width = charlen * win.cw;
   1519 	Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
   1520 	XRenderColor colfg, colbg;
   1521 	XRectangle r;
   1522 
   1523 	/* Fallback on color display for attributes not supported by the font */
   1524 	if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
   1525 		if (dc.ibfont.badslant || dc.ibfont.badweight)
   1526 			base.fg = defaultattr;
   1527 	} else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
   1528 	    (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
   1529 		base.fg = defaultattr;
   1530 	}
   1531 
   1532 	if (IS_TRUECOL(base.fg)) {
   1533 		colfg.alpha = 0xffff;
   1534 		colfg.red = TRUERED(base.fg);
   1535 		colfg.green = TRUEGREEN(base.fg);
   1536 		colfg.blue = TRUEBLUE(base.fg);
   1537 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
   1538 		fg = &truefg;
   1539 	} else {
   1540 		fg = &dc.col[base.fg];
   1541 	}
   1542 
   1543 	if (IS_TRUECOL(base.bg)) {
   1544 		colbg.alpha = 0xffff;
   1545 		colbg.green = TRUEGREEN(base.bg);
   1546 		colbg.red = TRUERED(base.bg);
   1547 		colbg.blue = TRUEBLUE(base.bg);
   1548 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
   1549 		bg = &truebg;
   1550 	} else {
   1551 		bg = &dc.col[base.bg];
   1552 	}
   1553 
   1554 	if (IS_SET(MODE_REVERSE)) {
   1555 		if (fg == &dc.col[defaultfg]) {
   1556 			fg = &dc.col[defaultbg];
   1557 		} else {
   1558 			colfg.red = ~fg->color.red;
   1559 			colfg.green = ~fg->color.green;
   1560 			colfg.blue = ~fg->color.blue;
   1561 			colfg.alpha = fg->color.alpha;
   1562 			XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
   1563 					&revfg);
   1564 			fg = &revfg;
   1565 		}
   1566 
   1567 		if (bg == &dc.col[defaultbg]) {
   1568 			bg = &dc.col[defaultfg];
   1569 		} else {
   1570 			colbg.red = ~bg->color.red;
   1571 			colbg.green = ~bg->color.green;
   1572 			colbg.blue = ~bg->color.blue;
   1573 			colbg.alpha = bg->color.alpha;
   1574 			XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
   1575 					&revbg);
   1576 			bg = &revbg;
   1577 		}
   1578 	}
   1579 
   1580 	if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
   1581 		colfg.red = fg->color.red / 2;
   1582 		colfg.green = fg->color.green / 2;
   1583 		colfg.blue = fg->color.blue / 2;
   1584 		colfg.alpha = fg->color.alpha;
   1585 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
   1586 		fg = &revfg;
   1587 	}
   1588 
   1589 	if (base.mode & ATTR_REVERSE) {
   1590 		temp = fg;
   1591 		fg = bg;
   1592 		bg = temp;
   1593 	}
   1594 
   1595 	if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
   1596 		fg = bg;
   1597 
   1598 	if (base.mode & ATTR_INVISIBLE)
   1599 		fg = bg;
   1600 
   1601 	/* Intelligent cleaning up of the borders. */
   1602 	if (x == 0) {
   1603 		xclear(0, (y == 0)? 0 : winy, win.hborderpx,
   1604 			winy + win.ch +
   1605 			((winy + win.ch >= win.vborderpx + win.th)? win.h : 0));
   1606 	}
   1607 	if (winx + width >= win.hborderpx + win.tw) {
   1608 		xclear(winx + width, (y == 0)? 0 : winy, win.w,
   1609 			((winy + win.ch >= win.vborderpx + win.th)? win.h : (winy + win.ch)));
   1610 	}
   1611 	if (y == 0)
   1612 		xclear(winx, 0, winx + width, win.vborderpx);
   1613 	if (winy + win.ch >= win.vborderpx + win.th)
   1614 		xclear(winx, winy + win.ch, winx + width, win.h);
   1615 
   1616 	/* Clean up the region we want to draw to. */
   1617 	XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
   1618 
   1619 	/* Set the clip region because Xft is sometimes dirty. */
   1620 	r.x = 0;
   1621 	r.y = 0;
   1622 	r.height = win.ch;
   1623 	r.width = width;
   1624 	XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
   1625 
   1626 	if (base.mode & ATTR_BOXDRAW) {
   1627 		drawboxes(winx, winy, width / len, win.ch, fg, bg, specs, len);
   1628 	} else {
   1629 		/* Render the glyphs. */
   1630 		XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
   1631 	}
   1632 
   1633 	/* Render underline and strikethrough. */
   1634 	if (base.mode & ATTR_UNDERLINE) {
   1635 		XftDrawRect(xw.draw, fg, winx, winy + ceilf(dc.font.ascent * chscale) + 1,
   1636 				width, 1);
   1637 	}
   1638 
   1639 	if (base.mode & ATTR_STRUCK) {
   1640 		XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent * chscale / 3,
   1641 				width, 1);
   1642 	}
   1643 
   1644 	/* Reset clip to none. */
   1645 	XftDrawSetClip(xw.draw, 0);
   1646 }
   1647 
   1648 void
   1649 xdrawglyph(Glyph g, int x, int y)
   1650 {
   1651 	int numspecs;
   1652 	XftGlyphFontSpec spec;
   1653 
   1654 	numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
   1655 	xdrawglyphfontspecs(&spec, g, numspecs, x, y);
   1656 }
   1657 
   1658 void
   1659 xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
   1660 {
   1661 	Color drawcol;
   1662 
   1663 	/* remove the old cursor */
   1664 	if (selected(ox, oy))
   1665 		og.mode ^= ATTR_REVERSE;
   1666 	xdrawglyph(og, ox, oy);
   1667 
   1668 	if (IS_SET(MODE_HIDE))
   1669 		return;
   1670 
   1671 	/*
   1672 	 * Select the right color for the right mode.
   1673 	 */
   1674 	g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE|ATTR_BOXDRAW;
   1675 
   1676 	if (IS_SET(MODE_REVERSE)) {
   1677 		g.mode |= ATTR_REVERSE;
   1678 		g.bg = defaultfg;
   1679 		if (selected(cx, cy)) {
   1680 			drawcol = dc.col[defaultcs];
   1681 			g.fg = defaultrcs;
   1682 		} else {
   1683 			drawcol = dc.col[defaultrcs];
   1684 			g.fg = defaultcs;
   1685 		}
   1686 	} else {
   1687 		if (selected(cx, cy)) {
   1688 			g.fg = defaultfg;
   1689 			g.bg = defaultrcs;
   1690 		} else {
   1691 			g.fg = defaultbg;
   1692 			g.bg = defaultcs;
   1693 		}
   1694 		drawcol = dc.col[g.bg];
   1695 	}
   1696 
   1697 	/* draw the new one */
   1698 	if (IS_SET(MODE_FOCUSED)) {
   1699 		switch (win.cursor) {
   1700 		case 7: /* st extension */
   1701 			g.u = 0x2603; /* snowman (U+2603) */
   1702 			/* FALLTHROUGH */
   1703 		case 0: /* Blinking Block */
   1704 		case 1: /* Blinking Block (Default) */
   1705 		case 2: /* Steady Block */
   1706 			xdrawglyph(g, cx, cy);
   1707 			break;
   1708 		case 3: /* Blinking Underline */
   1709 		case 4: /* Steady Underline */
   1710 			XftDrawRect(xw.draw, &drawcol,
   1711 					win.hborderpx + cx * win.cw,
   1712 					win.vborderpx + (cy + 1) * win.ch - \
   1713 						cursorthickness,
   1714 					win.cw, cursorthickness);
   1715 			break;
   1716 		case 5: /* Blinking bar */
   1717 		case 6: /* Steady bar */
   1718 			XftDrawRect(xw.draw, &drawcol,
   1719 					win.hborderpx + cx * win.cw,
   1720 					win.vborderpx + cy * win.ch,
   1721 					cursorthickness, win.ch);
   1722 			break;
   1723 		}
   1724 	} else {
   1725 		XftDrawRect(xw.draw, &drawcol,
   1726 				win.hborderpx + cx * win.cw,
   1727 				win.vborderpx + cy * win.ch,
   1728 				win.cw - 1, 1);
   1729 		XftDrawRect(xw.draw, &drawcol,
   1730 				win.hborderpx + cx * win.cw,
   1731 				win.vborderpx + cy * win.ch,
   1732 				1, win.ch - 1);
   1733 		XftDrawRect(xw.draw, &drawcol,
   1734 				win.hborderpx + (cx + 1) * win.cw - 1,
   1735 				win.vborderpx + cy * win.ch,
   1736 				1, win.ch - 1);
   1737 		XftDrawRect(xw.draw, &drawcol,
   1738 				win.hborderpx + cx * win.cw,
   1739 				win.vborderpx + (cy + 1) * win.ch - 1,
   1740 				win.cw, 1);
   1741 	}
   1742 }
   1743 
   1744 void
   1745 xsetenv(void)
   1746 {
   1747 	char buf[sizeof(long) * 8 + 1];
   1748 
   1749 	snprintf(buf, sizeof(buf), "%lu", xw.win);
   1750 	setenv("WINDOWID", buf, 1);
   1751 }
   1752 
   1753 void
   1754 xseticontitle(char *p)
   1755 {
   1756 	XTextProperty prop;
   1757 	DEFAULT(p, opt_title);
   1758 
   1759 	if (p[0] == '\0')
   1760 		p = opt_title;
   1761 
   1762 	if (Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
   1763 	                                &prop) != Success)
   1764 		return;
   1765 	XSetWMIconName(xw.dpy, xw.win, &prop);
   1766 	XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmiconname);
   1767 	XFree(prop.value);
   1768 }
   1769 
   1770 void
   1771 xsettitle(char *p)
   1772 {
   1773 	XTextProperty prop;
   1774 	DEFAULT(p, opt_title);
   1775 
   1776 	if (p[0] == '\0')
   1777 		p = opt_title;
   1778 
   1779 	if (Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
   1780 	                                &prop) != Success)
   1781 		return;
   1782 	XSetWMName(xw.dpy, xw.win, &prop);
   1783 	XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
   1784 	XFree(prop.value);
   1785 }
   1786 
   1787 int
   1788 xstartdraw(void)
   1789 {
   1790 	return IS_SET(MODE_VISIBLE);
   1791 }
   1792 
   1793 void
   1794 xdrawline(Line line, int x1, int y1, int x2)
   1795 {
   1796 	int i, x, ox, numspecs;
   1797 	Glyph base, new;
   1798 	XftGlyphFontSpec *specs = xw.specbuf;
   1799 
   1800 	numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
   1801 	i = ox = 0;
   1802 	for (x = x1; x < x2 && i < numspecs; x++) {
   1803 		new = line[x];
   1804 		if (new.mode == ATTR_WDUMMY)
   1805 			continue;
   1806 		if (selected(x, y1))
   1807 			new.mode ^= ATTR_REVERSE;
   1808 		if (i > 0 && ATTRCMP(base, new)) {
   1809 			xdrawglyphfontspecs(specs, base, i, ox, y1);
   1810 			specs += i;
   1811 			numspecs -= i;
   1812 			i = 0;
   1813 		}
   1814 		if (i == 0) {
   1815 			ox = x;
   1816 			base = new;
   1817 		}
   1818 		i++;
   1819 	}
   1820 	if (i > 0)
   1821 		xdrawglyphfontspecs(specs, base, i, ox, y1);
   1822 }
   1823 
   1824 void
   1825 xfinishdraw(void)
   1826 {
   1827 	XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
   1828 			win.h, 0, 0);
   1829 	XSetForeground(xw.dpy, dc.gc,
   1830 			dc.col[IS_SET(MODE_REVERSE)?
   1831 				defaultfg : defaultbg].pixel);
   1832 }
   1833 
   1834 void
   1835 xximspot(int x, int y)
   1836 {
   1837 	if (xw.ime.xic == NULL)
   1838 		return;
   1839 
   1840 	xw.ime.spot.x = borderpx + x * win.cw;
   1841 	xw.ime.spot.y = borderpx + (y + 1) * win.ch;
   1842 
   1843 	XSetICValues(xw.ime.xic, XNPreeditAttributes, xw.ime.spotlist, NULL);
   1844 }
   1845 
   1846 void
   1847 expose(XEvent *ev)
   1848 {
   1849 	redraw();
   1850 }
   1851 
   1852 void
   1853 visibility(XEvent *ev)
   1854 {
   1855 	XVisibilityEvent *e = &ev->xvisibility;
   1856 
   1857 	MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
   1858 }
   1859 
   1860 void
   1861 unmap(XEvent *ev)
   1862 {
   1863 	win.mode &= ~MODE_VISIBLE;
   1864 }
   1865 
   1866 void
   1867 xsetpointermotion(int set)
   1868 {
   1869 	MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
   1870 	XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
   1871 }
   1872 
   1873 void
   1874 xsetmode(int set, unsigned int flags)
   1875 {
   1876 	int mode = win.mode;
   1877 	MODBIT(win.mode, set, flags);
   1878 	if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
   1879 		redraw();
   1880 }
   1881 
   1882 int
   1883 xsetcursor(int cursor)
   1884 {
   1885 	if (!BETWEEN(cursor, 0, 7)) /* 7: st extension */
   1886 		return 1;
   1887 	win.cursor = cursor;
   1888 	return 0;
   1889 }
   1890 
   1891 void
   1892 xseturgency(int add)
   1893 {
   1894 	XWMHints *h = XGetWMHints(xw.dpy, xw.win);
   1895 
   1896 	MODBIT(h->flags, add, XUrgencyHint);
   1897 	XSetWMHints(xw.dpy, xw.win, h);
   1898 	XFree(h);
   1899 }
   1900 
   1901 void
   1902 xbell(void)
   1903 {
   1904 	if (!(IS_SET(MODE_FOCUSED)))
   1905 		xseturgency(1);
   1906 	if (bellvolume)
   1907 		XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
   1908 }
   1909 
   1910 void
   1911 focus(XEvent *ev)
   1912 {
   1913 	XFocusChangeEvent *e = &ev->xfocus;
   1914 
   1915 	if (e->mode == NotifyGrab)
   1916 		return;
   1917 
   1918 	if (ev->type == FocusIn) {
   1919 		if (xw.ime.xic)
   1920 			XSetICFocus(xw.ime.xic);
   1921 		win.mode |= MODE_FOCUSED;
   1922 		xseturgency(0);
   1923 		if (IS_SET(MODE_FOCUS))
   1924 			ttywrite("\033[I", 3, 0);
   1925 	} else {
   1926 		if (xw.ime.xic)
   1927 			XUnsetICFocus(xw.ime.xic);
   1928 		win.mode &= ~MODE_FOCUSED;
   1929 		if (IS_SET(MODE_FOCUS))
   1930 			ttywrite("\033[O", 3, 0);
   1931 	}
   1932 }
   1933 
   1934 int
   1935 match(uint mask, uint state)
   1936 {
   1937 	return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
   1938 }
   1939 
   1940 char*
   1941 kmap(KeySym k, uint state)
   1942 {
   1943 	Key *kp;
   1944 	int i;
   1945 
   1946 	/* Check for mapped keys out of X11 function keys. */
   1947 	for (i = 0; i < LEN(mappedkeys); i++) {
   1948 		if (mappedkeys[i] == k)
   1949 			break;
   1950 	}
   1951 	if (i == LEN(mappedkeys)) {
   1952 		if ((k & 0xFFFF) < 0xFD00)
   1953 			return NULL;
   1954 	}
   1955 
   1956 	for (kp = key; kp < key + LEN(key); kp++) {
   1957 		if (kp->k != k)
   1958 			continue;
   1959 
   1960 		if (!match(kp->mask, state))
   1961 			continue;
   1962 
   1963 		if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
   1964 			continue;
   1965 		if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
   1966 			continue;
   1967 
   1968 		if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
   1969 			continue;
   1970 
   1971 		return kp->s;
   1972 	}
   1973 
   1974 	return NULL;
   1975 }
   1976 
   1977 void
   1978 kpress(XEvent *ev)
   1979 {
   1980 	XKeyEvent *e = &ev->xkey;
   1981 	KeySym ksym = NoSymbol;
   1982 	char buf[64], *customkey;
   1983 	int len;
   1984 	Rune c;
   1985 	Status status;
   1986 	Shortcut *bp;
   1987 
   1988 	if (IS_SET(MODE_KBDLOCK))
   1989 		return;
   1990 
   1991 	if (xw.ime.xic) {
   1992 		len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status);
   1993 		if (status == XBufferOverflow)
   1994 			return;
   1995 	} else {
   1996 		len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
   1997 	}
   1998 	/* 1. shortcuts */
   1999 	for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
   2000 		if (ksym == bp->keysym && match(bp->mod, e->state)) {
   2001 			bp->func(&(bp->arg));
   2002 			return;
   2003 		}
   2004 	}
   2005 
   2006 	/* 2. custom keys from config.h */
   2007 	if ((customkey = kmap(ksym, e->state))) {
   2008 		ttywrite(customkey, strlen(customkey), 1);
   2009 		return;
   2010 	}
   2011 
   2012 	/* 3. composed string from input method */
   2013 	if (len == 0)
   2014 		return;
   2015 	if (len == 1 && e->state & Mod1Mask) {
   2016 		if (IS_SET(MODE_8BIT)) {
   2017 			if (*buf < 0177) {
   2018 				c = *buf | 0x80;
   2019 				len = utf8encode(c, buf);
   2020 			}
   2021 		} else {
   2022 			buf[1] = buf[0];
   2023 			buf[0] = '\033';
   2024 			len = 2;
   2025 		}
   2026 	}
   2027 	ttywrite(buf, len, 1);
   2028 }
   2029 
   2030 void
   2031 cmessage(XEvent *e)
   2032 {
   2033 	/*
   2034 	 * See xembed specs
   2035 	 *  http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
   2036 	 */
   2037 	if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
   2038 		if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
   2039 			win.mode |= MODE_FOCUSED;
   2040 			xseturgency(0);
   2041 		} else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
   2042 			win.mode &= ~MODE_FOCUSED;
   2043 		}
   2044 	} else if (e->xclient.data.l[0] == xw.wmdeletewin) {
   2045 		ttyhangup();
   2046 		exit(0);
   2047 	}
   2048 }
   2049 
   2050 void
   2051 resize(XEvent *e)
   2052 {
   2053 	if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
   2054 		return;
   2055 
   2056 	cresize(e->xconfigure.width, e->xconfigure.height);
   2057 }
   2058 
   2059 void
   2060 run(void)
   2061 {
   2062 	XEvent ev;
   2063 	int w = win.w, h = win.h;
   2064 	fd_set rfd;
   2065 	int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
   2066 	struct timespec seltv, *tv, now, lastblink, trigger;
   2067 	double timeout;
   2068 
   2069 	/* Waiting for window mapping */
   2070 	do {
   2071 		XNextEvent(xw.dpy, &ev);
   2072 		/*
   2073 		 * This XFilterEvent call is required because of XOpenIM. It
   2074 		 * does filter out the key event and some client message for
   2075 		 * the input method too.
   2076 		 */
   2077 		if (XFilterEvent(&ev, None))
   2078 			continue;
   2079 		if (ev.type == ConfigureNotify) {
   2080 			w = ev.xconfigure.width;
   2081 			h = ev.xconfigure.height;
   2082 		}
   2083 	} while (ev.type != MapNotify);
   2084 
   2085 	ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
   2086 	cresize(w, h);
   2087 
   2088 	for (timeout = -1, drawing = 0, lastblink = (struct timespec){0};;) {
   2089 		FD_ZERO(&rfd);
   2090 		FD_SET(ttyfd, &rfd);
   2091 		FD_SET(xfd, &rfd);
   2092 
   2093 		if (XPending(xw.dpy))
   2094 			timeout = 0;  /* existing events might not set xfd */
   2095 
   2096 		seltv.tv_sec = timeout / 1E3;
   2097 		seltv.tv_nsec = 1E6 * (timeout - 1E3 * seltv.tv_sec);
   2098 		tv = timeout >= 0 ? &seltv : NULL;
   2099 
   2100 		if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
   2101 			if (errno == EINTR)
   2102 				continue;
   2103 			die("select failed: %s\n", strerror(errno));
   2104 		}
   2105 		clock_gettime(CLOCK_MONOTONIC, &now);
   2106 
   2107 		if (FD_ISSET(ttyfd, &rfd))
   2108 			ttyread();
   2109 
   2110 		xev = 0;
   2111 		while (XPending(xw.dpy)) {
   2112 			xev = 1;
   2113 			XNextEvent(xw.dpy, &ev);
   2114 			if (XFilterEvent(&ev, None))
   2115 				continue;
   2116 			if (handler[ev.type])
   2117 				(handler[ev.type])(&ev);
   2118 		}
   2119 
   2120 		/*
   2121 		 * To reduce flicker and tearing, when new content or event
   2122 		 * triggers drawing, we first wait a bit to ensure we got
   2123 		 * everything, and if nothing new arrives - we draw.
   2124 		 * We start with trying to wait minlatency ms. If more content
   2125 		 * arrives sooner, we retry with shorter and shorter periods,
   2126 		 * and eventually draw even without idle after maxlatency ms.
   2127 		 * Typically this results in low latency while interacting,
   2128 		 * maximum latency intervals during `cat huge.txt`, and perfect
   2129 		 * sync with periodic updates from animations/key-repeats/etc.
   2130 		 */
   2131 		if (FD_ISSET(ttyfd, &rfd) || xev) {
   2132 			if (!drawing) {
   2133 				trigger = now;
   2134 				drawing = 1;
   2135 			}
   2136 			timeout = (maxlatency - TIMEDIFF(now, trigger)) \
   2137 			          / maxlatency * minlatency;
   2138 			if (timeout > 0)
   2139 				continue;  /* we have time, try to find idle */
   2140 		}
   2141 
   2142 		/* idle detected or maxlatency exhausted -> draw */
   2143 		timeout = -1;
   2144 		if (blinktimeout && tattrset(ATTR_BLINK)) {
   2145 			timeout = blinktimeout - TIMEDIFF(now, lastblink);
   2146 			if (timeout <= 0) {
   2147 				if (-timeout > blinktimeout) /* start visible */
   2148 					win.mode |= MODE_BLINK;
   2149 				win.mode ^= MODE_BLINK;
   2150 				tsetdirtattr(ATTR_BLINK);
   2151 				lastblink = now;
   2152 				timeout = blinktimeout;
   2153 			}
   2154 		}
   2155 
   2156 		draw();
   2157 		XFlush(xw.dpy);
   2158 		drawing = 0;
   2159 	}
   2160 }
   2161 
   2162 void
   2163 usage(void)
   2164 {
   2165 	die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
   2166 	    " [-n name] [-o file]\n"
   2167 	    "          [-T title] [-t title] [-w windowid]"
   2168 	    " [[-e] command [args ...]]\n"
   2169 	    "       %s [-aiv] [-c class] [-f font] [-g geometry]"
   2170 	    " [-n name] [-o file]\n"
   2171 	    "          [-T title] [-t title] [-w windowid] -l line"
   2172 	    " [stty_args ...]\n", argv0, argv0);
   2173 }
   2174 
   2175 void
   2176 modscheme(const Arg *arg) {
   2177 	colorscheme = (colorscheme + LEN(schemes) + arg->i) % LEN(schemes);
   2178 	updatescheme();
   2179 }
   2180 
   2181 void
   2182 nextscheme(const Arg *arg)
   2183 {
   2184 	colorscheme += arg->i;
   2185 	if (colorscheme >= (int)LEN(schemes))
   2186 		colorscheme = 0;
   2187 	else if (colorscheme < 0)
   2188 		colorscheme = LEN(schemes) - 1;
   2189 	updatescheme();
   2190 }
   2191 
   2192 void
   2193 selectscheme(const Arg *arg)
   2194 {
   2195 	if (BETWEEN(arg->i, 0, LEN(schemes)-1)) {
   2196 		colorscheme = arg->i;
   2197 		updatescheme();
   2198 	}
   2199 }
   2200 
   2201 void
   2202 updatescheme(void)
   2203 {
   2204 	int oldbg, oldfg;
   2205 
   2206 	oldbg = defaultbg;
   2207 	oldfg = defaultfg;
   2208 	colorname = schemes[colorscheme];
   2209 	xloadcols();
   2210 	cresize(win.w, win.h);
   2211 	redraw();
   2212 }
   2213 
   2214 int
   2215 main(int argc, char *argv[])
   2216 {
   2217 	char *endptr;
   2218 	xw.l = xw.t = 0;
   2219 	xw.isfixed = False;
   2220 	xsetcursor(cursorshape);
   2221 
   2222 	ARGBEGIN {
   2223 	case 'a':
   2224 		allowaltscreen = 0;
   2225 		break;
   2226 	case 'c':
   2227 		opt_class = EARGF(usage());
   2228 		break;
   2229 	case 'e':
   2230 		if (argc > 0)
   2231 			--argc, ++argv;
   2232 		goto run;
   2233 	case 'f':
   2234 		opt_font = EARGF(usage());
   2235 		break;
   2236 	case 'g':
   2237 		xw.gm = XParseGeometry(EARGF(usage()),
   2238 				&xw.l, &xw.t, &cols, &rows);
   2239 		break;
   2240 	case 'i':
   2241 		xw.isfixed = 1;
   2242 		break;
   2243 	case 'o':
   2244 		opt_io = EARGF(usage());
   2245 		break;
   2246 	case 'l':
   2247 		opt_line = EARGF(usage());
   2248 		break;
   2249 	case 'n':
   2250 		opt_name = EARGF(usage());
   2251 		break;
   2252 	case 's':
   2253 		opt_scale = EARGF(usage());
   2254 		cwscale = strtof(opt_scale, &endptr);
   2255 		if (endptr && endptr[0] && endptr[1]) {
   2256 			chscale = strtof(endptr + 1,&endptr);
   2257 			if (endptr && endptr[0]) {
   2258 				usage();
   2259 				abort();
   2260 			}
   2261 		}
   2262 		break;
   2263 	case 't':
   2264 	case 'T':
   2265 		opt_title = EARGF(usage());
   2266 		break;
   2267 	case 'w':
   2268 		opt_embed = EARGF(usage());
   2269 		break;
   2270 	case 'v':
   2271 		die("%s " VERSION "\n", argv0);
   2272 		break;
   2273 	default:
   2274 		usage();
   2275 	} ARGEND;
   2276 
   2277 run:
   2278 	colorname = schemes[colorscheme];
   2279 
   2280 	if (argc > 0) /* eat all remaining arguments */
   2281 		opt_cmd = argv;
   2282 
   2283 	if (!opt_title)
   2284 		opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0];
   2285 
   2286 	setlocale(LC_CTYPE, "");
   2287 	XSetLocaleModifiers("");
   2288 	cols = MAX(cols, 1);
   2289 	rows = MAX(rows, 1);
   2290 	tnew(cols, rows);
   2291 	xinit(cols, rows);
   2292 	xsetenv();
   2293 	selinit();
   2294 	run();
   2295 
   2296 	return 0;
   2297 }