st

my st build
git clone https://s.sonu.ch/~roket1428/st.git
Log | Files | Refs | README | LICENSE

x.c (49236B)


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