st

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

x.c (52511B)


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