st

simple terminal configuration
git clone https://riazj.com/git/st
Log | Files | Refs | README | LICENSE

x.c (49407B)


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