st

my build of st
Log | Files | Refs | README | LICENSE

x.c.orig (50450B)


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