st

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

x.c (52182B)


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