st

my customized st build
git clone git://git.hanetzok.net/st
Log | Files | Refs | README | LICENSE

x.c (50307B)


      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 /* Xresources preferences */
     50 enum resource_type {
     51 	STRING = 0,
     52 	INTEGER = 1,
     53 	FLOAT = 2
     54 };
     55 
     56 typedef struct {
     57 	char *name;
     58 	enum resource_type type;
     59 	void *dst;
     60 } ResourcePref;
     61 
     62 /* X modifiers */
     63 #define XK_ANY_MOD    UINT_MAX
     64 #define XK_NO_MOD     0
     65 #define XK_SWITCH_MOD (1<<13|1<<14)
     66 
     67 /* function definitions used in config.h */
     68 static void clipcopy(const Arg *);
     69 static void clippaste(const Arg *);
     70 static void numlock(const Arg *);
     71 static void selpaste(const Arg *);
     72 static void zoom(const Arg *);
     73 static void zoomabs(const Arg *);
     74 static void zoomreset(const Arg *);
     75 static void ttysend(const Arg *);
     76 
     77 /* config.h for applying patches and the configuration. */
     78 #include "config.h"
     79 
     80 /* XEMBED messages */
     81 #define XEMBED_FOCUS_IN  4
     82 #define XEMBED_FOCUS_OUT 5
     83 
     84 /* macros */
     85 #define IS_SET(flag)		((win.mode & (flag)) != 0)
     86 #define TRUERED(x)		(((x) & 0xff0000) >> 8)
     87 #define TRUEGREEN(x)		(((x) & 0xff00))
     88 #define TRUEBLUE(x)		(((x) & 0xff) << 8)
     89 
     90 typedef XftDraw *Draw;
     91 typedef XftColor Color;
     92 typedef XftGlyphFontSpec GlyphFontSpec;
     93 
     94 /* Purely graphic info */
     95 typedef struct {
     96 	int tw, th; /* tty width and height */
     97 	int w, h; /* window width and height */
     98 	int ch; /* char height */
     99 	int cw; /* char width  */
    100 	int mode; /* window state/mode flags */
    101 	int cursor; /* cursor style */
    102 } TermWindow;
    103 
    104 typedef struct {
    105 	Display *dpy;
    106 	Colormap cmap;
    107 	Window win;
    108 	Drawable buf;
    109 	GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
    110 	Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid;
    111 	struct {
    112 		XIM xim;
    113 		XIC xic;
    114 		XPoint spot;
    115 		XVaNestedList spotlist;
    116 	} ime;
    117 	Draw draw;
    118 	Visual *vis;
    119 	XSetWindowAttributes attrs;
    120 	int scr;
    121 	int isfixed; /* is fixed geometry? */
    122 	int depth; /* bit depth */
    123 	int l, t; /* left and top offset */
    124 	int gm; /* geometry mask */
    125 } XWindow;
    126 
    127 typedef struct {
    128 	Atom xtarget;
    129 	char *primary, *clipboard;
    130 	struct timespec tclick1;
    131 	struct timespec tclick2;
    132 } XSelection;
    133 
    134 /* Font structure */
    135 #define Font Font_
    136 typedef struct {
    137 	int height;
    138 	int width;
    139 	int ascent;
    140 	int descent;
    141 	int badslant;
    142 	int badweight;
    143 	short lbearing;
    144 	short rbearing;
    145 	XftFont *match;
    146 	FcFontSet *set;
    147 	FcPattern *pattern;
    148 } Font;
    149 
    150 /* Drawing Context */
    151 typedef struct {
    152 	Color *col;
    153 	size_t collen;
    154 	Font font, bfont, ifont, ibfont;
    155 	GC gc;
    156 } DC;
    157 
    158 static inline ushort sixd_to_16bit(int);
    159 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
    160 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
    161 static void xdrawglyph(Glyph, int, int);
    162 static void xclear(int, int, int, int);
    163 static int xgeommasktogravity(int);
    164 static int ximopen(Display *);
    165 static void ximinstantiate(Display *, XPointer, XPointer);
    166 static void ximdestroy(XIM, XPointer, XPointer);
    167 static int xicdestroy(XIC, XPointer, XPointer);
    168 static void xinit(int, int);
    169 static void cresize(int, int);
    170 static void xresize(int, int);
    171 static void xhints(void);
    172 static int xloadcolor(int, const char *, Color *);
    173 static int xloadfont(Font *, FcPattern *);
    174 static void xloadfonts(const char *, double);
    175 static void xunloadfont(Font *);
    176 static void xunloadfonts(void);
    177 static void xsetenv(void);
    178 static void xseturgency(int);
    179 static int evcol(XEvent *);
    180 static int evrow(XEvent *);
    181 
    182 static void expose(XEvent *);
    183 static void visibility(XEvent *);
    184 static void unmap(XEvent *);
    185 static void kpress(XEvent *);
    186 static void cmessage(XEvent *);
    187 static void resize(XEvent *);
    188 static void focus(XEvent *);
    189 static uint buttonmask(uint);
    190 static int mouseaction(XEvent *, uint);
    191 static void brelease(XEvent *);
    192 static void bpress(XEvent *);
    193 static void bmotion(XEvent *);
    194 static void propnotify(XEvent *);
    195 static void selnotify(XEvent *);
    196 static void selclear_(XEvent *);
    197 static void selrequest(XEvent *);
    198 static void setsel(char *, Time);
    199 static void mousesel(XEvent *, int);
    200 static void mousereport(XEvent *);
    201 static char *kmap(KeySym, uint);
    202 static int match(uint, uint);
    203 
    204 static void run(void);
    205 static void usage(void);
    206 
    207 static void (*handler[LASTEvent])(XEvent *) = {
    208 	[KeyPress] = kpress,
    209 	[ClientMessage] = cmessage,
    210 	[ConfigureNotify] = resize,
    211 	[VisibilityNotify] = visibility,
    212 	[UnmapNotify] = unmap,
    213 	[Expose] = expose,
    214 	[FocusIn] = focus,
    215 	[FocusOut] = focus,
    216 	[MotionNotify] = bmotion,
    217 	[ButtonPress] = bpress,
    218 	[ButtonRelease] = brelease,
    219 /*
    220  * Uncomment if you want the selection to disappear when you select something
    221  * different in another window.
    222  */
    223 /*	[SelectionClear] = selclear_, */
    224 	[SelectionNotify] = selnotify,
    225 /*
    226  * PropertyNotify is only turned on when there is some INCR transfer happening
    227  * for the selection retrieval.
    228  */
    229 	[PropertyNotify] = propnotify,
    230 	[SelectionRequest] = selrequest,
    231 };
    232 
    233 /* Globals */
    234 static DC dc;
    235 static XWindow xw;
    236 static XSelection xsel;
    237 static TermWindow win;
    238 
    239 /* Font Ring Cache */
    240 enum {
    241 	FRC_NORMAL,
    242 	FRC_ITALIC,
    243 	FRC_BOLD,
    244 	FRC_ITALICBOLD
    245 };
    246 
    247 typedef struct {
    248 	XftFont *font;
    249 	int flags;
    250 	Rune unicodep;
    251 } Fontcache;
    252 
    253 /* Fontcache is an array now. A new font will be appended to the array. */
    254 static Fontcache *frc = NULL;
    255 static int frclen = 0;
    256 static int frccap = 0;
    257 static char *usedfont = NULL;
    258 static double usedfontsize = 0;
    259 static double defaultfontsize = 0;
    260 
    261 static char *opt_class = NULL;
    262 static char **opt_cmd  = NULL;
    263 static char *opt_embed = NULL;
    264 static char *opt_font  = NULL;
    265 static char *opt_io    = NULL;
    266 static char *opt_line  = NULL;
    267 static char *opt_name  = NULL;
    268 static char *opt_title = NULL;
    269 
    270 static uint buttons; /* bit field of pressed buttons */
    271 
    272 void
    273 clipcopy(const Arg *dummy)
    274 {
    275 	Atom clipboard;
    276 
    277 	free(xsel.clipboard);
    278 	xsel.clipboard = NULL;
    279 
    280 	if (xsel.primary != NULL) {
    281 		xsel.clipboard = xstrdup(xsel.primary);
    282 		clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    283 		XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
    284 	}
    285 }
    286 
    287 void
    288 clippaste(const Arg *dummy)
    289 {
    290 	Atom clipboard;
    291 
    292 	clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
    293 	XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
    294 			xw.win, CurrentTime);
    295 }
    296 
    297 void
    298 selpaste(const Arg *dummy)
    299 {
    300 	XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
    301 			xw.win, CurrentTime);
    302 }
    303 
    304 void
    305 numlock(const Arg *dummy)
    306 {
    307 	win.mode ^= MODE_NUMLOCK;
    308 }
    309 
    310 void
    311 zoom(const Arg *arg)
    312 {
    313 	Arg larg;
    314 
    315 	larg.f = usedfontsize + arg->f;
    316 	zoomabs(&larg);
    317 }
    318 
    319 void
    320 zoomabs(const Arg *arg)
    321 {
    322 	xunloadfonts();
    323 	xloadfonts(usedfont, arg->f);
    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 - borderpx;
    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 - borderpx;
    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 (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
    735 		mousereport(e);
    736 		return;
    737 	}
    738 
    739 	mousesel(e, 0);
    740 }
    741 
    742 void
    743 cresize(int width, int height)
    744 {
    745 	int col, row;
    746 
    747 	if (width != 0)
    748 		win.w = width;
    749 	if (height != 0)
    750 		win.h = height;
    751 
    752 	col = (win.w - 2 * borderpx) / win.cw;
    753 	row = (win.h - 2 * borderpx) / win.ch;
    754 	col = MAX(1, col);
    755 	row = MAX(1, row);
    756 
    757 	tresize(col, row);
    758 	xresize(col, row);
    759 	ttyresize(win.tw, win.th);
    760 }
    761 
    762 void
    763 xresize(int col, int row)
    764 {
    765 	win.tw = col * win.cw;
    766 	win.th = row * win.ch;
    767 
    768 	XFreePixmap(xw.dpy, xw.buf);
    769 	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
    770 			xw.depth);
    771 	XftDrawChange(xw.draw, xw.buf);
    772 	xclear(0, 0, win.w, win.h);
    773 
    774 	/* resize to new width */
    775 	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
    776 }
    777 
    778 ushort
    779 sixd_to_16bit(int x)
    780 {
    781 	return x == 0 ? 0 : 0x3737 + 0x2828 * x;
    782 }
    783 
    784 int
    785 xloadcolor(int i, const char *name, Color *ncolor)
    786 {
    787 	XRenderColor color = { .alpha = 0xffff };
    788 
    789 	if (!name) {
    790 		if (BETWEEN(i, 16, 255)) { /* 256 color */
    791 			if (i < 6*6*6+16) { /* same colors as xterm */
    792 				color.red   = sixd_to_16bit( ((i-16)/36)%6 );
    793 				color.green = sixd_to_16bit( ((i-16)/6) %6 );
    794 				color.blue  = sixd_to_16bit( ((i-16)/1) %6 );
    795 			} else { /* greyscale */
    796 				color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
    797 				color.green = color.blue = color.red;
    798 			}
    799 			return XftColorAllocValue(xw.dpy, xw.vis,
    800 			                          xw.cmap, &color, ncolor);
    801 		} else
    802 			name = colorname[i];
    803 	}
    804 
    805 	return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
    806 }
    807 
    808 void
    809 xloadcols(void)
    810 {
    811 	int i;
    812 	static int loaded;
    813 	Color *cp;
    814 
    815 	if (loaded) {
    816 		for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
    817 			XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
    818 	} else {
    819 		dc.collen = MAX(LEN(colorname), 256);
    820 		dc.col = xmalloc(dc.collen * sizeof(Color));
    821 	}
    822 
    823 	for (i = 0; i < dc.collen; i++)
    824 		if (!xloadcolor(i, NULL, &dc.col[i])) {
    825 			if (colorname[i])
    826 				die("could not allocate color '%s'\n", colorname[i]);
    827 			else
    828 				die("could not allocate color %d\n", i);
    829 		}
    830 
    831 	dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha);
    832 	dc.col[defaultbg].pixel &= 0x00FFFFFF;
    833 	dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24;
    834 	loaded = 1;
    835 }
    836 
    837 int
    838 xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b)
    839 {
    840 	if (!BETWEEN(x, 0, dc.collen - 1))
    841 		return 1;
    842 
    843 	*r = dc.col[x].color.red >> 8;
    844 	*g = dc.col[x].color.green >> 8;
    845 	*b = dc.col[x].color.blue >> 8;
    846 
    847 	return 0;
    848 }
    849 
    850 int
    851 xsetcolorname(int x, const char *name)
    852 {
    853 	Color ncolor;
    854 
    855 	if (!BETWEEN(x, 0, dc.collen - 1))
    856 		return 1;
    857 
    858 	if (!xloadcolor(x, name, &ncolor))
    859 		return 1;
    860 
    861 	XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
    862 	dc.col[x] = ncolor;
    863 
    864 	if (x == defaultbg) {
    865 		dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha);
    866 		dc.col[defaultbg].pixel &= 0x00FFFFFF;
    867 		dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24;
    868 	}
    869 
    870 	return 0;
    871 }
    872 
    873 /*
    874  * Absolute coordinates.
    875  */
    876 void
    877 xclear(int x1, int y1, int x2, int y2)
    878 {
    879 	XftDrawRect(xw.draw,
    880 			&dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
    881 			x1, y1, x2-x1, y2-y1);
    882 }
    883 
    884 void
    885 xhints(void)
    886 {
    887 	XClassHint class = {opt_name ? opt_name : "st",
    888 	                    opt_class ? opt_class : "St"};
    889 	XWMHints wm = {.flags = InputHint, .input = 1};
    890 	XSizeHints *sizeh;
    891 
    892 	sizeh = XAllocSizeHints();
    893 
    894 	sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize;
    895 	sizeh->height = win.h;
    896 	sizeh->width = win.w;
    897 	sizeh->height_inc = win.ch;
    898 	sizeh->width_inc = win.cw;
    899 	sizeh->base_height = 2 * borderpx;
    900 	sizeh->base_width = 2 * borderpx;
    901 	sizeh->min_height = win.ch + 2 * borderpx;
    902 	sizeh->min_width = win.cw + 2 * borderpx;
    903 	if (xw.isfixed) {
    904 		sizeh->flags |= PMaxSize;
    905 		sizeh->min_width = sizeh->max_width = win.w;
    906 		sizeh->min_height = sizeh->max_height = win.h;
    907 	}
    908 	if (xw.gm & (XValue|YValue)) {
    909 		sizeh->flags |= USPosition | PWinGravity;
    910 		sizeh->x = xw.l;
    911 		sizeh->y = xw.t;
    912 		sizeh->win_gravity = xgeommasktogravity(xw.gm);
    913 	}
    914 
    915 	XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
    916 			&class);
    917 	XFree(sizeh);
    918 }
    919 
    920 int
    921 xgeommasktogravity(int mask)
    922 {
    923 	switch (mask & (XNegative|YNegative)) {
    924 	case 0:
    925 		return NorthWestGravity;
    926 	case XNegative:
    927 		return NorthEastGravity;
    928 	case YNegative:
    929 		return SouthWestGravity;
    930 	}
    931 
    932 	return SouthEastGravity;
    933 }
    934 
    935 int
    936 xloadfont(Font *f, FcPattern *pattern)
    937 {
    938 	FcPattern *configured;
    939 	FcPattern *match;
    940 	FcResult result;
    941 	XGlyphInfo extents;
    942 	int wantattr, haveattr;
    943 
    944 	/*
    945 	 * Manually configure instead of calling XftMatchFont
    946 	 * so that we can use the configured pattern for
    947 	 * "missing glyph" lookups.
    948 	 */
    949 	configured = FcPatternDuplicate(pattern);
    950 	if (!configured)
    951 		return 1;
    952 
    953 	FcConfigSubstitute(NULL, configured, FcMatchPattern);
    954 	XftDefaultSubstitute(xw.dpy, xw.scr, configured);
    955 
    956 	match = FcFontMatch(NULL, configured, &result);
    957 	if (!match) {
    958 		FcPatternDestroy(configured);
    959 		return 1;
    960 	}
    961 
    962 	if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
    963 		FcPatternDestroy(configured);
    964 		FcPatternDestroy(match);
    965 		return 1;
    966 	}
    967 
    968 	if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
    969 	    XftResultMatch)) {
    970 		/*
    971 		 * Check if xft was unable to find a font with the appropriate
    972 		 * slant but gave us one anyway. Try to mitigate.
    973 		 */
    974 		if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
    975 		    &haveattr) != XftResultMatch) || haveattr < wantattr) {
    976 			f->badslant = 1;
    977 			fputs("font slant does not match\n", stderr);
    978 		}
    979 	}
    980 
    981 	if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
    982 	    XftResultMatch)) {
    983 		if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
    984 		    &haveattr) != XftResultMatch) || haveattr != wantattr) {
    985 			f->badweight = 1;
    986 			fputs("font weight does not match\n", stderr);
    987 		}
    988 	}
    989 
    990 	XftTextExtentsUtf8(xw.dpy, f->match,
    991 		(const FcChar8 *) ascii_printable,
    992 		strlen(ascii_printable), &extents);
    993 
    994 	f->set = NULL;
    995 	f->pattern = configured;
    996 
    997 	f->ascent = f->match->ascent;
    998 	f->descent = f->match->descent;
    999 	f->lbearing = 0;
   1000 	f->rbearing = f->match->max_advance_width;
   1001 
   1002 	f->height = f->ascent + f->descent;
   1003 	f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
   1004 
   1005 	return 0;
   1006 }
   1007 
   1008 void
   1009 xloadfonts(const char *fontstr, double fontsize)
   1010 {
   1011 	FcPattern *pattern;
   1012 	double fontval;
   1013 
   1014 	if (fontstr[0] == '-')
   1015 		pattern = XftXlfdParse(fontstr, False, False);
   1016 	else
   1017 		pattern = FcNameParse((const FcChar8 *)fontstr);
   1018 
   1019 	if (!pattern)
   1020 		die("can't open font %s\n", fontstr);
   1021 
   1022 	if (fontsize > 1) {
   1023 		FcPatternDel(pattern, FC_PIXEL_SIZE);
   1024 		FcPatternDel(pattern, FC_SIZE);
   1025 		FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
   1026 		usedfontsize = fontsize;
   1027 	} else {
   1028 		if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
   1029 				FcResultMatch) {
   1030 			usedfontsize = fontval;
   1031 		} else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
   1032 				FcResultMatch) {
   1033 			usedfontsize = -1;
   1034 		} else {
   1035 			/*
   1036 			 * Default font size is 12, if none given. This is to
   1037 			 * have a known usedfontsize value.
   1038 			 */
   1039 			FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
   1040 			usedfontsize = 12;
   1041 		}
   1042 		defaultfontsize = usedfontsize;
   1043 	}
   1044 
   1045 	if (xloadfont(&dc.font, pattern))
   1046 		die("can't open font %s\n", fontstr);
   1047 
   1048 	if (usedfontsize < 0) {
   1049 		FcPatternGetDouble(dc.font.match->pattern,
   1050 		                   FC_PIXEL_SIZE, 0, &fontval);
   1051 		usedfontsize = fontval;
   1052 		if (fontsize == 0)
   1053 			defaultfontsize = fontval;
   1054 	}
   1055 
   1056 	/* Setting character width and height. */
   1057 	win.cw = ceilf(dc.font.width * cwscale);
   1058 	win.ch = ceilf(dc.font.height * chscale);
   1059 
   1060 	FcPatternDel(pattern, FC_SLANT);
   1061 	FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
   1062 	if (xloadfont(&dc.ifont, pattern))
   1063 		die("can't open font %s\n", fontstr);
   1064 
   1065 	FcPatternDel(pattern, FC_WEIGHT);
   1066 	FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
   1067 	if (xloadfont(&dc.ibfont, pattern))
   1068 		die("can't open font %s\n", fontstr);
   1069 
   1070 	FcPatternDel(pattern, FC_SLANT);
   1071 	FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
   1072 	if (xloadfont(&dc.bfont, pattern))
   1073 		die("can't open font %s\n", fontstr);
   1074 
   1075 	FcPatternDestroy(pattern);
   1076 }
   1077 
   1078 void
   1079 xunloadfont(Font *f)
   1080 {
   1081 	XftFontClose(xw.dpy, f->match);
   1082 	FcPatternDestroy(f->pattern);
   1083 	if (f->set)
   1084 		FcFontSetDestroy(f->set);
   1085 }
   1086 
   1087 void
   1088 xunloadfonts(void)
   1089 {
   1090 	/* Free the loaded fonts in the font cache.  */
   1091 	while (frclen > 0)
   1092 		XftFontClose(xw.dpy, frc[--frclen].font);
   1093 
   1094 	xunloadfont(&dc.font);
   1095 	xunloadfont(&dc.bfont);
   1096 	xunloadfont(&dc.ifont);
   1097 	xunloadfont(&dc.ibfont);
   1098 }
   1099 
   1100 int
   1101 ximopen(Display *dpy)
   1102 {
   1103 	XIMCallback imdestroy = { .client_data = NULL, .callback = ximdestroy };
   1104 	XICCallback icdestroy = { .client_data = NULL, .callback = xicdestroy };
   1105 
   1106 	xw.ime.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
   1107 	if (xw.ime.xim == NULL)
   1108 		return 0;
   1109 
   1110 	if (XSetIMValues(xw.ime.xim, XNDestroyCallback, &imdestroy, NULL))
   1111 		fprintf(stderr, "XSetIMValues: "
   1112 		                "Could not set XNDestroyCallback.\n");
   1113 
   1114 	xw.ime.spotlist = XVaCreateNestedList(0, XNSpotLocation, &xw.ime.spot,
   1115 	                                      NULL);
   1116 
   1117 	if (xw.ime.xic == NULL) {
   1118 		xw.ime.xic = XCreateIC(xw.ime.xim, XNInputStyle,
   1119 		                       XIMPreeditNothing | XIMStatusNothing,
   1120 		                       XNClientWindow, xw.win,
   1121 		                       XNDestroyCallback, &icdestroy,
   1122 		                       NULL);
   1123 	}
   1124 	if (xw.ime.xic == NULL)
   1125 		fprintf(stderr, "XCreateIC: Could not create input context.\n");
   1126 
   1127 	return 1;
   1128 }
   1129 
   1130 void
   1131 ximinstantiate(Display *dpy, XPointer client, XPointer call)
   1132 {
   1133 	if (ximopen(dpy))
   1134 		XUnregisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1135 		                                 ximinstantiate, NULL);
   1136 }
   1137 
   1138 void
   1139 ximdestroy(XIM xim, XPointer client, XPointer call)
   1140 {
   1141 	xw.ime.xim = NULL;
   1142 	XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1143 	                               ximinstantiate, NULL);
   1144 	XFree(xw.ime.spotlist);
   1145 }
   1146 
   1147 int
   1148 xicdestroy(XIC xim, XPointer client, XPointer call)
   1149 {
   1150 	xw.ime.xic = NULL;
   1151 	return 1;
   1152 }
   1153 
   1154 void
   1155 xinit(int cols, int rows)
   1156 {
   1157 	XGCValues gcvalues;
   1158 	Cursor cursor;
   1159 	Window parent, root;
   1160 	pid_t thispid = getpid();
   1161 	XColor xmousefg, xmousebg;
   1162 	XWindowAttributes attr;
   1163 	XVisualInfo vis;
   1164 
   1165 	xw.scr = XDefaultScreen(xw.dpy);
   1166 
   1167 	root = XRootWindow(xw.dpy, xw.scr);
   1168 	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
   1169 		parent = root;
   1170 
   1171 	if (XMatchVisualInfo(xw.dpy, xw.scr, 32, TrueColor, &vis) != 0) {
   1172 		xw.vis = vis.visual;
   1173 		xw.depth = vis.depth;
   1174 	} else {
   1175 		XGetWindowAttributes(xw.dpy, parent, &attr);
   1176 		xw.vis = attr.visual;
   1177 		xw.depth = attr.depth;
   1178 	}
   1179 
   1180 	/* font */
   1181 	if (!FcInit())
   1182 		die("could not init fontconfig.\n");
   1183 
   1184 	usedfont = (opt_font == NULL)? font : opt_font;
   1185 	xloadfonts(usedfont, 0);
   1186 
   1187 	/* colors */
   1188 	xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
   1189 	xloadcols();
   1190 
   1191 	/* adjust fixed window geometry */
   1192 	win.w = 2 * borderpx + cols * win.cw;
   1193 	win.h = 2 * borderpx + rows * win.ch;
   1194 	if (xw.gm & XNegative)
   1195 		xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
   1196 	if (xw.gm & YNegative)
   1197 		xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
   1198 
   1199 	/* Events */
   1200 	xw.attrs.background_pixel = dc.col[defaultbg].pixel;
   1201 	xw.attrs.border_pixel = dc.col[defaultbg].pixel;
   1202 	xw.attrs.bit_gravity = NorthWestGravity;
   1203 	xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
   1204 		| ExposureMask | VisibilityChangeMask | StructureNotifyMask
   1205 		| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
   1206 	xw.attrs.colormap = xw.cmap;
   1207 
   1208 	xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
   1209 			win.w, win.h, 0, xw.depth, InputOutput,
   1210 			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
   1211 			| CWEventMask | CWColormap, &xw.attrs);
   1212 	if (parent != root)
   1213 		XReparentWindow(xw.dpy, xw.win, parent, xw.l, xw.t);
   1214 
   1215 	memset(&gcvalues, 0, sizeof(gcvalues));
   1216 	gcvalues.graphics_exposures = False;
   1217 	dc.gc = XCreateGC(xw.dpy, xw.win, GCGraphicsExposures,
   1218 			&gcvalues);
   1219 	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
   1220 			xw.depth);
   1221 	XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
   1222 	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
   1223 
   1224 	/* font spec buffer */
   1225 	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
   1226 
   1227 	/* Xft rendering context */
   1228 	xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
   1229 
   1230 	/* input methods */
   1231 	if (!ximopen(xw.dpy)) {
   1232 		XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
   1233 	                                       ximinstantiate, NULL);
   1234 	}
   1235 
   1236 	/* white cursor, black outline */
   1237 	cursor = XCreateFontCursor(xw.dpy, mouseshape);
   1238 	XDefineCursor(xw.dpy, xw.win, cursor);
   1239 
   1240 	if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
   1241 		xmousefg.red   = 0xffff;
   1242 		xmousefg.green = 0xffff;
   1243 		xmousefg.blue  = 0xffff;
   1244 	}
   1245 
   1246 	if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
   1247 		xmousebg.red   = 0x0000;
   1248 		xmousebg.green = 0x0000;
   1249 		xmousebg.blue  = 0x0000;
   1250 	}
   1251 
   1252 	XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
   1253 
   1254 	xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
   1255 	xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
   1256 	xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
   1257 	xw.netwmiconname = XInternAtom(xw.dpy, "_NET_WM_ICON_NAME", False);
   1258 	XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
   1259 
   1260 	xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
   1261 	XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
   1262 			PropModeReplace, (uchar *)&thispid, 1);
   1263 
   1264 	win.mode = MODE_NUMLOCK;
   1265 	resettitle();
   1266 	xhints();
   1267 	XMapWindow(xw.dpy, xw.win);
   1268 	XSync(xw.dpy, False);
   1269 
   1270 	clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
   1271 	clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
   1272 	xsel.primary = NULL;
   1273 	xsel.clipboard = NULL;
   1274 	xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
   1275 	if (xsel.xtarget == None)
   1276 		xsel.xtarget = XA_STRING;
   1277 }
   1278 
   1279 int
   1280 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
   1281 {
   1282 	float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
   1283 	ushort mode, prevmode = USHRT_MAX;
   1284 	Font *font = &dc.font;
   1285 	int frcflags = FRC_NORMAL;
   1286 	float runewidth = win.cw;
   1287 	Rune rune;
   1288 	FT_UInt glyphidx;
   1289 	FcResult fcres;
   1290 	FcPattern *fcpattern, *fontpattern;
   1291 	FcFontSet *fcsets[] = { NULL };
   1292 	FcCharSet *fccharset;
   1293 	int i, f, numspecs = 0;
   1294 
   1295 	for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
   1296 		/* Fetch rune and mode for current glyph. */
   1297 		rune = glyphs[i].u;
   1298 		mode = glyphs[i].mode;
   1299 
   1300 		/* Skip dummy wide-character spacing. */
   1301 		if (mode == ATTR_WDUMMY)
   1302 			continue;
   1303 
   1304 		/* Determine font for glyph if different from previous glyph. */
   1305 		if (prevmode != mode) {
   1306 			prevmode = mode;
   1307 			font = &dc.font;
   1308 			frcflags = FRC_NORMAL;
   1309 			runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
   1310 			if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
   1311 				font = &dc.ibfont;
   1312 				frcflags = FRC_ITALICBOLD;
   1313 			} else if (mode & ATTR_ITALIC) {
   1314 				font = &dc.ifont;
   1315 				frcflags = FRC_ITALIC;
   1316 			} else if (mode & ATTR_BOLD) {
   1317 				font = &dc.bfont;
   1318 				frcflags = FRC_BOLD;
   1319 			}
   1320 			yp = winy + font->ascent;
   1321 		}
   1322 
   1323 		/* Lookup character index with default font. */
   1324 		glyphidx = XftCharIndex(xw.dpy, font->match, rune);
   1325 		if (glyphidx) {
   1326 			specs[numspecs].font = font->match;
   1327 			specs[numspecs].glyph = glyphidx;
   1328 			specs[numspecs].x = (short)xp;
   1329 			specs[numspecs].y = (short)yp;
   1330 			xp += runewidth;
   1331 			numspecs++;
   1332 			continue;
   1333 		}
   1334 
   1335 		/* Fallback on font cache, search the font cache for match. */
   1336 		for (f = 0; f < frclen; f++) {
   1337 			glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
   1338 			/* Everything correct. */
   1339 			if (glyphidx && frc[f].flags == frcflags)
   1340 				break;
   1341 			/* We got a default font for a not found glyph. */
   1342 			if (!glyphidx && frc[f].flags == frcflags
   1343 					&& frc[f].unicodep == rune) {
   1344 				break;
   1345 			}
   1346 		}
   1347 
   1348 		/* Nothing was found. Use fontconfig to find matching font. */
   1349 		if (f >= frclen) {
   1350 			if (!font->set)
   1351 				font->set = FcFontSort(0, font->pattern,
   1352 				                       1, 0, &fcres);
   1353 			fcsets[0] = font->set;
   1354 
   1355 			/*
   1356 			 * Nothing was found in the cache. Now use
   1357 			 * some dozen of Fontconfig calls to get the
   1358 			 * font for one single character.
   1359 			 *
   1360 			 * Xft and fontconfig are design failures.
   1361 			 */
   1362 			fcpattern = FcPatternDuplicate(font->pattern);
   1363 			fccharset = FcCharSetCreate();
   1364 
   1365 			FcCharSetAddChar(fccharset, rune);
   1366 			FcPatternAddCharSet(fcpattern, FC_CHARSET,
   1367 					fccharset);
   1368 			FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
   1369 
   1370 			FcConfigSubstitute(0, fcpattern,
   1371 					FcMatchPattern);
   1372 			FcDefaultSubstitute(fcpattern);
   1373 
   1374 			fontpattern = FcFontSetMatch(0, fcsets, 1,
   1375 					fcpattern, &fcres);
   1376 
   1377 			/* Allocate memory for the new cache entry. */
   1378 			if (frclen >= frccap) {
   1379 				frccap += 16;
   1380 				frc = xrealloc(frc, frccap * sizeof(Fontcache));
   1381 			}
   1382 
   1383 			frc[frclen].font = XftFontOpenPattern(xw.dpy,
   1384 					fontpattern);
   1385 			if (!frc[frclen].font)
   1386 				die("XftFontOpenPattern failed seeking fallback font: %s\n",
   1387 					strerror(errno));
   1388 			frc[frclen].flags = frcflags;
   1389 			frc[frclen].unicodep = rune;
   1390 
   1391 			glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
   1392 
   1393 			f = frclen;
   1394 			frclen++;
   1395 
   1396 			FcPatternDestroy(fcpattern);
   1397 			FcCharSetDestroy(fccharset);
   1398 		}
   1399 
   1400 		specs[numspecs].font = frc[f].font;
   1401 		specs[numspecs].glyph = glyphidx;
   1402 		specs[numspecs].x = (short)xp;
   1403 		specs[numspecs].y = (short)yp;
   1404 		xp += runewidth;
   1405 		numspecs++;
   1406 	}
   1407 
   1408 	return numspecs;
   1409 }
   1410 
   1411 void
   1412 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
   1413 {
   1414 	int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
   1415 	int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
   1416 	    width = charlen * win.cw;
   1417 	Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
   1418 	XRenderColor colfg, colbg;
   1419 	XRectangle r;
   1420 
   1421 	/* Fallback on color display for attributes not supported by the font */
   1422 	if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
   1423 		if (dc.ibfont.badslant || dc.ibfont.badweight)
   1424 			base.fg = defaultattr;
   1425 	} else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
   1426 	    (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
   1427 		base.fg = defaultattr;
   1428 	}
   1429 
   1430 	if (IS_TRUECOL(base.fg)) {
   1431 		colfg.alpha = 0xffff;
   1432 		colfg.red = TRUERED(base.fg);
   1433 		colfg.green = TRUEGREEN(base.fg);
   1434 		colfg.blue = TRUEBLUE(base.fg);
   1435 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
   1436 		fg = &truefg;
   1437 	} else {
   1438 		fg = &dc.col[base.fg];
   1439 	}
   1440 
   1441 	if (IS_TRUECOL(base.bg)) {
   1442 		colbg.alpha = 0xffff;
   1443 		colbg.green = TRUEGREEN(base.bg);
   1444 		colbg.red = TRUERED(base.bg);
   1445 		colbg.blue = TRUEBLUE(base.bg);
   1446 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
   1447 		bg = &truebg;
   1448 	} else {
   1449 		bg = &dc.col[base.bg];
   1450 	}
   1451 
   1452 	/* Change basic system colors [0-7] to bright system colors [8-15] */
   1453 	if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
   1454 		fg = &dc.col[base.fg + 8];
   1455 
   1456 	if (IS_SET(MODE_REVERSE)) {
   1457 		if (fg == &dc.col[defaultfg]) {
   1458 			fg = &dc.col[defaultbg];
   1459 		} else {
   1460 			colfg.red = ~fg->color.red;
   1461 			colfg.green = ~fg->color.green;
   1462 			colfg.blue = ~fg->color.blue;
   1463 			colfg.alpha = fg->color.alpha;
   1464 			XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
   1465 					&revfg);
   1466 			fg = &revfg;
   1467 		}
   1468 
   1469 		if (bg == &dc.col[defaultbg]) {
   1470 			bg = &dc.col[defaultfg];
   1471 		} else {
   1472 			colbg.red = ~bg->color.red;
   1473 			colbg.green = ~bg->color.green;
   1474 			colbg.blue = ~bg->color.blue;
   1475 			colbg.alpha = bg->color.alpha;
   1476 			XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
   1477 					&revbg);
   1478 			bg = &revbg;
   1479 		}
   1480 	}
   1481 
   1482 	if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
   1483 		colfg.red = fg->color.red / 2;
   1484 		colfg.green = fg->color.green / 2;
   1485 		colfg.blue = fg->color.blue / 2;
   1486 		colfg.alpha = fg->color.alpha;
   1487 		XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
   1488 		fg = &revfg;
   1489 	}
   1490 
   1491 	if (base.mode & ATTR_REVERSE) {
   1492 		temp = fg;
   1493 		fg = bg;
   1494 		bg = temp;
   1495 	}
   1496 
   1497 	if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
   1498 		fg = bg;
   1499 
   1500 	if (base.mode & ATTR_INVISIBLE)
   1501 		fg = bg;
   1502 
   1503 	/* Intelligent cleaning up of the borders. */
   1504 	if (x == 0) {
   1505 		xclear(0, (y == 0)? 0 : winy, borderpx,
   1506 			winy + win.ch +
   1507 			((winy + win.ch >= borderpx + win.th)? win.h : 0));
   1508 	}
   1509 	if (winx + width >= borderpx + win.tw) {
   1510 		xclear(winx + width, (y == 0)? 0 : winy, win.w,
   1511 			((winy + win.ch >= borderpx + win.th)? win.h : (winy + win.ch)));
   1512 	}
   1513 	if (y == 0)
   1514 		xclear(winx, 0, winx + width, borderpx);
   1515 	if (winy + win.ch >= borderpx + win.th)
   1516 		xclear(winx, winy + win.ch, winx + width, win.h);
   1517 
   1518 	/* Clean up the region we want to draw to. */
   1519 	XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
   1520 
   1521 	/* Set the clip region because Xft is sometimes dirty. */
   1522 	r.x = 0;
   1523 	r.y = 0;
   1524 	r.height = win.ch;
   1525 	r.width = width;
   1526 	XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
   1527 
   1528 	/* Render the glyphs. */
   1529 	XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
   1530 
   1531 	/* Render underline and strikethrough. */
   1532 	if (base.mode & ATTR_UNDERLINE) {
   1533 		XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent * chscale + 1,
   1534 				width, 1);
   1535 	}
   1536 
   1537 	if (base.mode & ATTR_STRUCK) {
   1538 		XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent * chscale / 3,
   1539 				width, 1);
   1540 	}
   1541 
   1542 	/* Reset clip to none. */
   1543 	XftDrawSetClip(xw.draw, 0);
   1544 }
   1545 
   1546 void
   1547 xdrawglyph(Glyph g, int x, int y)
   1548 {
   1549 	int numspecs;
   1550 	XftGlyphFontSpec spec;
   1551 
   1552 	numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
   1553 	xdrawglyphfontspecs(&spec, g, numspecs, x, y);
   1554 }
   1555 
   1556 void
   1557 xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
   1558 {
   1559 	Color drawcol;
   1560 
   1561 	/* remove the old cursor */
   1562 	if (selected(ox, oy))
   1563 		og.mode ^= ATTR_REVERSE;
   1564 	xdrawglyph(og, ox, oy);
   1565 
   1566 	if (IS_SET(MODE_HIDE))
   1567 		return;
   1568 
   1569 	/*
   1570 	 * Select the right color for the right mode.
   1571 	 */
   1572 	g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE;
   1573 
   1574 	if (IS_SET(MODE_REVERSE)) {
   1575 		g.mode |= ATTR_REVERSE;
   1576 		g.bg = defaultfg;
   1577 		if (selected(cx, cy)) {
   1578 			drawcol = dc.col[defaultcs];
   1579 			g.fg = defaultrcs;
   1580 		} else {
   1581 			drawcol = dc.col[defaultrcs];
   1582 			g.fg = defaultcs;
   1583 		}
   1584 	} else {
   1585 		if (selected(cx, cy)) {
   1586 			g.fg = defaultfg;
   1587 			g.bg = defaultrcs;
   1588 		} else {
   1589 			g.fg = defaultbg;
   1590 			g.bg = defaultcs;
   1591 		}
   1592 		drawcol = dc.col[g.bg];
   1593 	}
   1594 
   1595 	/* draw the new one */
   1596 	if (IS_SET(MODE_FOCUSED)) {
   1597 		switch (win.cursor) {
   1598 		case 7: /* st extension */
   1599 			g.u = 0x2603; /* snowman (U+2603) */
   1600 			/* FALLTHROUGH */
   1601 		case 0: /* Blinking Block */
   1602 		case 1: /* Blinking Block (Default) */
   1603 		case 2: /* Steady Block */
   1604 			xdrawglyph(g, cx, cy);
   1605 			break;
   1606 		case 3: /* Blinking Underline */
   1607 		case 4: /* Steady Underline */
   1608 			XftDrawRect(xw.draw, &drawcol,
   1609 					borderpx + cx * win.cw,
   1610 					borderpx + (cy + 1) * win.ch - \
   1611 						cursorthickness,
   1612 					win.cw, cursorthickness);
   1613 			break;
   1614 		case 5: /* Blinking bar */
   1615 		case 6: /* Steady bar */
   1616 			XftDrawRect(xw.draw, &drawcol,
   1617 					borderpx + cx * win.cw,
   1618 					borderpx + cy * win.ch,
   1619 					cursorthickness, win.ch);
   1620 			break;
   1621 		}
   1622 	} else {
   1623 		XftDrawRect(xw.draw, &drawcol,
   1624 				borderpx + cx * win.cw,
   1625 				borderpx + cy * win.ch,
   1626 				win.cw - 1, 1);
   1627 		XftDrawRect(xw.draw, &drawcol,
   1628 				borderpx + cx * win.cw,
   1629 				borderpx + cy * win.ch,
   1630 				1, win.ch - 1);
   1631 		XftDrawRect(xw.draw, &drawcol,
   1632 				borderpx + (cx + 1) * win.cw - 1,
   1633 				borderpx + cy * win.ch,
   1634 				1, win.ch - 1);
   1635 		XftDrawRect(xw.draw, &drawcol,
   1636 				borderpx + cx * win.cw,
   1637 				borderpx + (cy + 1) * win.ch - 1,
   1638 				win.cw, 1);
   1639 	}
   1640 }
   1641 
   1642 void
   1643 xsetenv(void)
   1644 {
   1645 	char buf[sizeof(long) * 8 + 1];
   1646 
   1647 	snprintf(buf, sizeof(buf), "%lu", xw.win);
   1648 	setenv("WINDOWID", buf, 1);
   1649 }
   1650 
   1651 void
   1652 xseticontitle(char *p)
   1653 {
   1654 	XTextProperty prop;
   1655 	DEFAULT(p, opt_title);
   1656 
   1657 	if (p[0] == '\0')
   1658 		p = opt_title;
   1659 
   1660 	if (Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
   1661 	                                &prop) != Success)
   1662 		return;
   1663 	XSetWMIconName(xw.dpy, xw.win, &prop);
   1664 	XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmiconname);
   1665 	XFree(prop.value);
   1666 }
   1667 
   1668 void
   1669 xsettitle(char *p)
   1670 {
   1671 	XTextProperty prop;
   1672 	DEFAULT(p, opt_title);
   1673 
   1674 	if (p[0] == '\0')
   1675 		p = opt_title;
   1676 
   1677 	if (Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
   1678 	                                &prop) != Success)
   1679 		return;
   1680 	XSetWMName(xw.dpy, xw.win, &prop);
   1681 	XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
   1682 	XFree(prop.value);
   1683 }
   1684 
   1685 int
   1686 xstartdraw(void)
   1687 {
   1688 	return IS_SET(MODE_VISIBLE);
   1689 }
   1690 
   1691 void
   1692 xdrawline(Line line, int x1, int y1, int x2)
   1693 {
   1694 	int i, x, ox, numspecs;
   1695 	Glyph base, new;
   1696 	XftGlyphFontSpec *specs = xw.specbuf;
   1697 
   1698 	numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
   1699 	i = ox = 0;
   1700 	for (x = x1; x < x2 && i < numspecs; x++) {
   1701 		new = line[x];
   1702 		if (new.mode == ATTR_WDUMMY)
   1703 			continue;
   1704 		if (selected(x, y1))
   1705 			new.mode ^= ATTR_REVERSE;
   1706 		if (i > 0 && ATTRCMP(base, new)) {
   1707 			xdrawglyphfontspecs(specs, base, i, ox, y1);
   1708 			specs += i;
   1709 			numspecs -= i;
   1710 			i = 0;
   1711 		}
   1712 		if (i == 0) {
   1713 			ox = x;
   1714 			base = new;
   1715 		}
   1716 		i++;
   1717 	}
   1718 	if (i > 0)
   1719 		xdrawglyphfontspecs(specs, base, i, ox, y1);
   1720 }
   1721 
   1722 void
   1723 xfinishdraw(void)
   1724 {
   1725 	XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
   1726 			win.h, 0, 0);
   1727 	XSetForeground(xw.dpy, dc.gc,
   1728 			dc.col[IS_SET(MODE_REVERSE)?
   1729 				defaultfg : defaultbg].pixel);
   1730 }
   1731 
   1732 void
   1733 xximspot(int x, int y)
   1734 {
   1735 	if (xw.ime.xic == NULL)
   1736 		return;
   1737 
   1738 	xw.ime.spot.x = borderpx + x * win.cw;
   1739 	xw.ime.spot.y = borderpx + (y + 1) * win.ch;
   1740 
   1741 	XSetICValues(xw.ime.xic, XNPreeditAttributes, xw.ime.spotlist, NULL);
   1742 }
   1743 
   1744 void
   1745 expose(XEvent *ev)
   1746 {
   1747 	redraw();
   1748 }
   1749 
   1750 void
   1751 visibility(XEvent *ev)
   1752 {
   1753 	XVisibilityEvent *e = &ev->xvisibility;
   1754 
   1755 	MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
   1756 }
   1757 
   1758 void
   1759 unmap(XEvent *ev)
   1760 {
   1761 	win.mode &= ~MODE_VISIBLE;
   1762 }
   1763 
   1764 void
   1765 xsetpointermotion(int set)
   1766 {
   1767 	MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
   1768 	XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
   1769 }
   1770 
   1771 void
   1772 xsetmode(int set, unsigned int flags)
   1773 {
   1774 	int mode = win.mode;
   1775 	MODBIT(win.mode, set, flags);
   1776 	if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
   1777 		redraw();
   1778 }
   1779 
   1780 int
   1781 xsetcursor(int cursor)
   1782 {
   1783 	if (!BETWEEN(cursor, 0, 7)) /* 7: st extension */
   1784 		return 1;
   1785 	win.cursor = cursor;
   1786 	return 0;
   1787 }
   1788 
   1789 void
   1790 xseturgency(int add)
   1791 {
   1792 	XWMHints *h = XGetWMHints(xw.dpy, xw.win);
   1793 
   1794 	MODBIT(h->flags, add, XUrgencyHint);
   1795 	XSetWMHints(xw.dpy, xw.win, h);
   1796 	XFree(h);
   1797 }
   1798 
   1799 void
   1800 xbell(void)
   1801 {
   1802 	if (!(IS_SET(MODE_FOCUSED)))
   1803 		xseturgency(1);
   1804 	if (bellvolume)
   1805 		XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
   1806 }
   1807 
   1808 void
   1809 focus(XEvent *ev)
   1810 {
   1811 	XFocusChangeEvent *e = &ev->xfocus;
   1812 
   1813 	if (e->mode == NotifyGrab)
   1814 		return;
   1815 
   1816 	if (ev->type == FocusIn) {
   1817 		if (xw.ime.xic)
   1818 			XSetICFocus(xw.ime.xic);
   1819 		win.mode |= MODE_FOCUSED;
   1820 		xseturgency(0);
   1821 		if (IS_SET(MODE_FOCUS))
   1822 			ttywrite("\033[I", 3, 0);
   1823 	} else {
   1824 		if (xw.ime.xic)
   1825 			XUnsetICFocus(xw.ime.xic);
   1826 		win.mode &= ~MODE_FOCUSED;
   1827 		if (IS_SET(MODE_FOCUS))
   1828 			ttywrite("\033[O", 3, 0);
   1829 	}
   1830 }
   1831 
   1832 int
   1833 match(uint mask, uint state)
   1834 {
   1835 	return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
   1836 }
   1837 
   1838 char*
   1839 kmap(KeySym k, uint state)
   1840 {
   1841 	Key *kp;
   1842 	int i;
   1843 
   1844 	/* Check for mapped keys out of X11 function keys. */
   1845 	for (i = 0; i < LEN(mappedkeys); i++) {
   1846 		if (mappedkeys[i] == k)
   1847 			break;
   1848 	}
   1849 	if (i == LEN(mappedkeys)) {
   1850 		if ((k & 0xFFFF) < 0xFD00)
   1851 			return NULL;
   1852 	}
   1853 
   1854 	for (kp = key; kp < key + LEN(key); kp++) {
   1855 		if (kp->k != k)
   1856 			continue;
   1857 
   1858 		if (!match(kp->mask, state))
   1859 			continue;
   1860 
   1861 		if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
   1862 			continue;
   1863 		if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
   1864 			continue;
   1865 
   1866 		if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
   1867 			continue;
   1868 
   1869 		return kp->s;
   1870 	}
   1871 
   1872 	return NULL;
   1873 }
   1874 
   1875 void
   1876 kpress(XEvent *ev)
   1877 {
   1878 	XKeyEvent *e = &ev->xkey;
   1879 	KeySym ksym = NoSymbol;
   1880 	char buf[64], *customkey;
   1881 	int len;
   1882 	Rune c;
   1883 	Status status;
   1884 	Shortcut *bp;
   1885 
   1886 	if (IS_SET(MODE_KBDLOCK))
   1887 		return;
   1888 
   1889 	if (xw.ime.xic) {
   1890 		len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status);
   1891 		if (status == XBufferOverflow)
   1892 			return;
   1893 	} else {
   1894 		len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
   1895 	}
   1896 	/* 1. shortcuts */
   1897 	for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
   1898 		if (ksym == bp->keysym && match(bp->mod, e->state)) {
   1899 			bp->func(&(bp->arg));
   1900 			return;
   1901 		}
   1902 	}
   1903 
   1904 	/* 2. custom keys from config.h */
   1905 	if ((customkey = kmap(ksym, e->state))) {
   1906 		ttywrite(customkey, strlen(customkey), 1);
   1907 		return;
   1908 	}
   1909 
   1910 	/* 3. composed string from input method */
   1911 	if (len == 0)
   1912 		return;
   1913 	if (len == 1 && e->state & Mod1Mask) {
   1914 		if (IS_SET(MODE_8BIT)) {
   1915 			if (*buf < 0177) {
   1916 				c = *buf | 0x80;
   1917 				len = utf8encode(c, buf);
   1918 			}
   1919 		} else {
   1920 			buf[1] = buf[0];
   1921 			buf[0] = '\033';
   1922 			len = 2;
   1923 		}
   1924 	}
   1925 	ttywrite(buf, len, 1);
   1926 }
   1927 
   1928 void
   1929 cmessage(XEvent *e)
   1930 {
   1931 	/*
   1932 	 * See xembed specs
   1933 	 *  http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
   1934 	 */
   1935 	if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
   1936 		if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
   1937 			win.mode |= MODE_FOCUSED;
   1938 			xseturgency(0);
   1939 		} else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
   1940 			win.mode &= ~MODE_FOCUSED;
   1941 		}
   1942 	} else if (e->xclient.data.l[0] == xw.wmdeletewin) {
   1943 		ttyhangup();
   1944 		exit(0);
   1945 	}
   1946 }
   1947 
   1948 void
   1949 resize(XEvent *e)
   1950 {
   1951 	if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
   1952 		return;
   1953 
   1954 	cresize(e->xconfigure.width, e->xconfigure.height);
   1955 }
   1956 
   1957 void
   1958 run(void)
   1959 {
   1960 	XEvent ev;
   1961 	int w = win.w, h = win.h;
   1962 	fd_set rfd;
   1963 	int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
   1964 	struct timespec seltv, *tv, now, lastblink, trigger;
   1965 	double timeout;
   1966 
   1967 	/* Waiting for window mapping */
   1968 	do {
   1969 		XNextEvent(xw.dpy, &ev);
   1970 		/*
   1971 		 * This XFilterEvent call is required because of XOpenIM. It
   1972 		 * does filter out the key event and some client message for
   1973 		 * the input method too.
   1974 		 */
   1975 		if (XFilterEvent(&ev, None))
   1976 			continue;
   1977 		if (ev.type == ConfigureNotify) {
   1978 			w = ev.xconfigure.width;
   1979 			h = ev.xconfigure.height;
   1980 		}
   1981 	} while (ev.type != MapNotify);
   1982 
   1983 	ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
   1984 	cresize(w, h);
   1985 
   1986 	for (timeout = -1, drawing = 0, lastblink = (struct timespec){0};;) {
   1987 		FD_ZERO(&rfd);
   1988 		FD_SET(ttyfd, &rfd);
   1989 		FD_SET(xfd, &rfd);
   1990 
   1991 		if (XPending(xw.dpy))
   1992 			timeout = 0;  /* existing events might not set xfd */
   1993 
   1994 		seltv.tv_sec = timeout / 1E3;
   1995 		seltv.tv_nsec = 1E6 * (timeout - 1E3 * seltv.tv_sec);
   1996 		tv = timeout >= 0 ? &seltv : NULL;
   1997 
   1998 		if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
   1999 			if (errno == EINTR)
   2000 				continue;
   2001 			die("select failed: %s\n", strerror(errno));
   2002 		}
   2003 		clock_gettime(CLOCK_MONOTONIC, &now);
   2004 
   2005 		if (FD_ISSET(ttyfd, &rfd))
   2006 			ttyread();
   2007 
   2008 		xev = 0;
   2009 		while (XPending(xw.dpy)) {
   2010 			xev = 1;
   2011 			XNextEvent(xw.dpy, &ev);
   2012 			if (XFilterEvent(&ev, None))
   2013 				continue;
   2014 			if (handler[ev.type])
   2015 				(handler[ev.type])(&ev);
   2016 		}
   2017 
   2018 		/*
   2019 		 * To reduce flicker and tearing, when new content or event
   2020 		 * triggers drawing, we first wait a bit to ensure we got
   2021 		 * everything, and if nothing new arrives - we draw.
   2022 		 * We start with trying to wait minlatency ms. If more content
   2023 		 * arrives sooner, we retry with shorter and shorter periods,
   2024 		 * and eventually draw even without idle after maxlatency ms.
   2025 		 * Typically this results in low latency while interacting,
   2026 		 * maximum latency intervals during `cat huge.txt`, and perfect
   2027 		 * sync with periodic updates from animations/key-repeats/etc.
   2028 		 */
   2029 		if (FD_ISSET(ttyfd, &rfd) || xev) {
   2030 			if (!drawing) {
   2031 				trigger = now;
   2032 				drawing = 1;
   2033 			}
   2034 			timeout = (maxlatency - TIMEDIFF(now, trigger)) \
   2035 			          / maxlatency * minlatency;
   2036 			if (timeout > 0)
   2037 				continue;  /* we have time, try to find idle */
   2038 		}
   2039 
   2040 		/* idle detected or maxlatency exhausted -> draw */
   2041 		timeout = -1;
   2042 		if (blinktimeout && tattrset(ATTR_BLINK)) {
   2043 			timeout = blinktimeout - TIMEDIFF(now, lastblink);
   2044 			if (timeout <= 0) {
   2045 				if (-timeout > blinktimeout) /* start visible */
   2046 					win.mode |= MODE_BLINK;
   2047 				win.mode ^= MODE_BLINK;
   2048 				tsetdirtattr(ATTR_BLINK);
   2049 				lastblink = now;
   2050 				timeout = blinktimeout;
   2051 			}
   2052 		}
   2053 
   2054 		draw();
   2055 		XFlush(xw.dpy);
   2056 		drawing = 0;
   2057 	}
   2058 }
   2059 
   2060 int
   2061 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
   2062 {
   2063 	char **sdst = dst;
   2064 	int *idst = dst;
   2065 	float *fdst = dst;
   2066 
   2067 	char fullname[256];
   2068 	char fullclass[256];
   2069 	char *type;
   2070 	XrmValue ret;
   2071 
   2072 	snprintf(fullname, sizeof(fullname), "%s.%s",
   2073 			opt_name ? opt_name : "st", name);
   2074 	snprintf(fullclass, sizeof(fullclass), "%s.%s",
   2075 			opt_class ? opt_class : "St", name);
   2076 	fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0';
   2077 
   2078 	XrmGetResource(db, fullname, fullclass, &type, &ret);
   2079 	if (ret.addr == NULL || strncmp("String", type, 64))
   2080 		return 1;
   2081 
   2082 	switch (rtype) {
   2083 	case STRING:
   2084 		*sdst = ret.addr;
   2085 		break;
   2086 	case INTEGER:
   2087 		*idst = strtoul(ret.addr, NULL, 10);
   2088 		break;
   2089 	case FLOAT:
   2090 		*fdst = strtof(ret.addr, NULL);
   2091 		break;
   2092 	}
   2093 	return 0;
   2094 }
   2095 
   2096 void
   2097 config_init(void)
   2098 {
   2099 	char *resm;
   2100 	XrmDatabase db;
   2101 	ResourcePref *p;
   2102 
   2103 	XrmInitialize();
   2104 	resm = XResourceManagerString(xw.dpy);
   2105 	if (!resm)
   2106 		return;
   2107 
   2108 	db = XrmGetStringDatabase(resm);
   2109 	for (p = resources; p < resources + LEN(resources); p++)
   2110 		resource_load(db, p->name, p->type, p->dst);
   2111 }
   2112 
   2113 void
   2114 usage(void)
   2115 {
   2116 	die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
   2117 	    " [-n name] [-o file]\n"
   2118 	    "          [-T title] [-t title] [-w windowid]"
   2119 	    " [[-e] command [args ...]]\n"
   2120 	    "       %s [-aiv] [-c class] [-f font] [-g geometry]"
   2121 	    " [-n name] [-o file]\n"
   2122 	    "          [-T title] [-t title] [-w windowid] -l line"
   2123 	    " [stty_args ...]\n", argv0, argv0);
   2124 }
   2125 
   2126 int
   2127 main(int argc, char *argv[])
   2128 {
   2129 	xw.l = xw.t = 0;
   2130 	xw.isfixed = False;
   2131 	xsetcursor(cursorshape);
   2132 
   2133 	ARGBEGIN {
   2134 	case 'a':
   2135 		allowaltscreen = 0;
   2136 		break;
   2137 	case 'A':
   2138 		alpha = strtof(EARGF(usage()), NULL);
   2139 		LIMIT(alpha, 0.0, 1.0);
   2140 		break;
   2141 	case 'c':
   2142 		opt_class = EARGF(usage());
   2143 		break;
   2144 	case 'e':
   2145 		if (argc > 0)
   2146 			--argc, ++argv;
   2147 		goto run;
   2148 	case 'f':
   2149 		opt_font = EARGF(usage());
   2150 		break;
   2151 	case 'g':
   2152 		xw.gm = XParseGeometry(EARGF(usage()),
   2153 				&xw.l, &xw.t, &cols, &rows);
   2154 		break;
   2155 	case 'i':
   2156 		xw.isfixed = 1;
   2157 		break;
   2158 	case 'o':
   2159 		opt_io = EARGF(usage());
   2160 		break;
   2161 	case 'l':
   2162 		opt_line = EARGF(usage());
   2163 		break;
   2164 	case 'n':
   2165 		opt_name = EARGF(usage());
   2166 		break;
   2167 	case 't':
   2168 	case 'T':
   2169 		opt_title = EARGF(usage());
   2170 		break;
   2171 	case 'w':
   2172 		opt_embed = EARGF(usage());
   2173 		break;
   2174 	case 'v':
   2175 		die("%s " VERSION "\n", argv0);
   2176 		break;
   2177 	default:
   2178 		usage();
   2179 	} ARGEND;
   2180 
   2181 run:
   2182 	if (argc > 0) /* eat all remaining arguments */
   2183 		opt_cmd = argv;
   2184 
   2185 	if (!opt_title)
   2186 		opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0];
   2187 
   2188 	setlocale(LC_CTYPE, "");
   2189 	XSetLocaleModifiers("");
   2190 
   2191 	if(!(xw.dpy = XOpenDisplay(NULL)))
   2192 		die("Can't open display\n");
   2193 
   2194 	config_init();
   2195 	cols = MAX(cols, 1);
   2196 	rows = MAX(rows, 1);
   2197 	tnew(cols, rows);
   2198 	xinit(cols, rows);
   2199 	xsetenv();
   2200 	selinit();
   2201 	run();
   2202 
   2203 	return 0;
   2204 }