st

Mahdi's build of st
git clone git://mahdi.pw/st.git
Log | Files | Refs | README | LICENSE

x.c (56851B)


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