st

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

x.c (51872B)


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