dwm

personal fork of dwm (rnpnr branch)
git clone anongit@rnpnr.xyz:dwm.git
Log | Files | Refs | Feed | README | LICENSE

drw.c (11324B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <ctype.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <X11/Xlib.h>
      7 #include <X11/Xft/Xft.h>
      8 
      9 #include "drw.h"
     10 #include "util.h"
     11 
     12 #define UTF_INVALID 0xFFFD
     13 
     14 static int
     15 utf8decode(const char *s_in, long *u, int *err)
     16 {
     17 	static const unsigned char lens[] = {
     18 		/* 0XXXX */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     19 		/* 10XXX */ 0, 0, 0, 0, 0, 0, 0, 0,  /* invalid */
     20 		/* 110XX */ 2, 2, 2, 2,
     21 		/* 1110X */ 3, 3,
     22 		/* 11110 */ 4,
     23 		/* 11111 */ 0,  /* invalid */
     24 	};
     25 	static const unsigned char leading_mask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
     26 	static const unsigned int overlong[] = { 0x0, 0x80, 0x0800, 0x10000 };
     27 
     28 	const unsigned char *s = (const unsigned char *)s_in;
     29 	int len = lens[*s >> 3];
     30 	*u = UTF_INVALID;
     31 	*err = 1;
     32 	if (len == 0)
     33 		return 1;
     34 
     35 	long cp = s[0] & leading_mask[len - 1];
     36 	for (int i = 1; i < len; ++i) {
     37 		if (s[i] == '\0' || (s[i] & 0xC0) != 0x80)
     38 			return i;
     39 		cp = (cp << 6) | (s[i] & 0x3F);
     40 	}
     41 	/* out of range, surrogate, overlong encoding */
     42 	if (cp > 0x10FFFF || (cp >> 11) == 0x1B || cp < overlong[len - 1])
     43 		return len;
     44 
     45 	*err = 0;
     46 	*u = cp;
     47 	return len;
     48 }
     49 
     50 Drw *
     51 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
     52 {
     53 	Drw *drw = ecalloc(1, sizeof(Drw));
     54 
     55 	drw->dpy = dpy;
     56 	drw->screen = screen;
     57 	drw->root = root;
     58 	drw->w = w;
     59 	drw->h = h;
     60 	drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
     61 	drw->gc = XCreateGC(dpy, root, 0, NULL);
     62 	XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
     63 
     64 	return drw;
     65 }
     66 
     67 void
     68 drw_resize(Drw *drw, unsigned int w, unsigned int h)
     69 {
     70 	if (!drw)
     71 		return;
     72 
     73 	drw->w = w;
     74 	drw->h = h;
     75 	if (drw->drawable)
     76 		XFreePixmap(drw->dpy, drw->drawable);
     77 	drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
     78 }
     79 
     80 void
     81 drw_free(Drw *drw)
     82 {
     83 	XFreePixmap(drw->dpy, drw->drawable);
     84 	XFreeGC(drw->dpy, drw->gc);
     85 	drw_fontset_free(drw->fonts);
     86 	free(drw);
     87 }
     88 
     89 /* This function is an implementation detail. Library users should use
     90  * drw_fontset_create instead.
     91  */
     92 static Fnt *
     93 xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
     94 {
     95 	Fnt *font;
     96 	XftFont *xfont = NULL;
     97 	FcPattern *pattern = NULL;
     98 
     99 	if (fontname) {
    100 		/* Using the pattern found at font->xfont->pattern does not yield the
    101 		 * same substitution results as using the pattern returned by
    102 		 * FcNameParse; using the latter results in the desired fallback
    103 		 * behaviour whereas the former just results in missing-character
    104 		 * rectangles being drawn, at least with some fonts. */
    105 		if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
    106 			fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
    107 			return NULL;
    108 		}
    109 		if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
    110 			fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
    111 			XftFontClose(drw->dpy, xfont);
    112 			return NULL;
    113 		}
    114 	} else if (fontpattern) {
    115 		if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
    116 			fprintf(stderr, "error, cannot load font from pattern.\n");
    117 			return NULL;
    118 		}
    119 	} else {
    120 		die("no font specified.");
    121 	}
    122 
    123 	font = ecalloc(1, sizeof(Fnt));
    124 	font->xfont = xfont;
    125 	font->pattern = pattern;
    126 	font->h = xfont->ascent + xfont->descent;
    127 	font->dpy = drw->dpy;
    128 
    129 	return font;
    130 }
    131 
    132 static void
    133 xfont_free(Fnt *font)
    134 {
    135 	if (!font)
    136 		return;
    137 	if (font->pattern)
    138 		FcPatternDestroy(font->pattern);
    139 	XftFontClose(font->dpy, font->xfont);
    140 	free(font);
    141 }
    142 
    143 Fnt*
    144 drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
    145 {
    146 	Fnt *cur, *ret = NULL;
    147 	size_t i;
    148 
    149 	if (!drw || !fonts)
    150 		return NULL;
    151 
    152 	for (i = 1; i <= fontcount; i++) {
    153 		if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
    154 			cur->next = ret;
    155 			ret = cur;
    156 		}
    157 	}
    158 	return (drw->fonts = ret);
    159 }
    160 
    161 void
    162 drw_fontset_free(Fnt *font)
    163 {
    164 	if (font) {
    165 		drw_fontset_free(font->next);
    166 		xfont_free(font);
    167 	}
    168 }
    169 
    170 void
    171 drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
    172 {
    173 	if (!drw || !dest || !clrname)
    174 		return;
    175 
    176 	if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
    177 	                       DefaultColormap(drw->dpy, drw->screen),
    178 	                       clrname, dest))
    179 		die("error, cannot allocate color '%s'", clrname);
    180 }
    181 
    182 /* Wrapper to create color schemes. The caller has to call free(3) on the
    183  * returned color scheme when done using it. */
    184 Clr *
    185 drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
    186 {
    187 	size_t i;
    188 	Clr *ret;
    189 
    190 	/* need at least two colors for a scheme */
    191 	if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
    192 		return NULL;
    193 
    194 	for (i = 0; i < clrcount; i++)
    195 		drw_clr_create(drw, &ret[i], clrnames[i]);
    196 	return ret;
    197 }
    198 
    199 void
    200 drw_setfontset(Drw *drw, Fnt *set)
    201 {
    202 	if (drw)
    203 		drw->fonts = set;
    204 }
    205 
    206 void
    207 drw_setscheme(Drw *drw, Clr *scm)
    208 {
    209 	if (drw)
    210 		drw->scheme = scm;
    211 }
    212 
    213 void
    214 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
    215 {
    216 	if (!drw || !drw->scheme)
    217 		return;
    218 	XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
    219 	if (filled)
    220 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
    221 	else
    222 		XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
    223 }
    224 
    225 int
    226 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
    227 {
    228 	int ty, ellipsis_x = 0;
    229 	unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
    230 	XftDraw *d = NULL;
    231 	Fnt *usedfont, *curfont, *nextfont;
    232 	int utf8strlen, utf8charlen, utf8err, render = x || y || w || h;
    233 	long utf8codepoint = 0;
    234 	const char *utf8str;
    235 	FcCharSet *fccharset;
    236 	FcPattern *fcpattern;
    237 	FcPattern *match;
    238 	XftResult result;
    239 	int charexists = 0, overflow = 0;
    240 	/* keep track of a couple codepoints for which we have no match. */
    241 	static unsigned int nomatches[128], ellipsis_width, invalid_width;
    242 	static const char invalid[] = "�";
    243 
    244 	if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
    245 		return 0;
    246 
    247 	if (!render) {
    248 		w = invert ? invert : ~invert;
    249 	} else {
    250 		XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
    251 		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
    252 		d = XftDrawCreate(drw->dpy, drw->drawable,
    253 		                  DefaultVisual(drw->dpy, drw->screen),
    254 		                  DefaultColormap(drw->dpy, drw->screen));
    255 		x += lpad;
    256 		w -= lpad;
    257 	}
    258 
    259 	usedfont = drw->fonts;
    260 	if (!ellipsis_width && render)
    261 		ellipsis_width = drw_fontset_getwidth(drw, "...");
    262 	if (!invalid_width && render)
    263 		invalid_width = drw_fontset_getwidth(drw, invalid);
    264 	while (1) {
    265 		ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0;
    266 		utf8str = text;
    267 		nextfont = NULL;
    268 		while (*text) {
    269 			utf8charlen = utf8decode(text, &utf8codepoint, &utf8err);
    270 			for (curfont = drw->fonts; curfont; curfont = curfont->next) {
    271 				charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
    272 				if (charexists) {
    273 					if (iscntrl(*text))
    274 						tmpw = 0;
    275 					else
    276 						drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
    277 					if (ew + ellipsis_width <= w) {
    278 						/* keep track where the ellipsis still fits */
    279 						ellipsis_x = x + ew;
    280 						ellipsis_w = w - ew;
    281 						ellipsis_len = utf8strlen;
    282 					}
    283 
    284 					if (ew + tmpw > w) {
    285 						overflow = 1;
    286 						/* called from drw_fontset_getwidth_clamp():
    287 						 * it wants the width AFTER the overflow
    288 						 */
    289 						if (!render)
    290 							x += tmpw;
    291 						else
    292 							utf8strlen = ellipsis_len;
    293 					} else if (curfont == usedfont) {
    294 						text += utf8charlen;
    295 						utf8strlen += utf8err ? 0 : utf8charlen;
    296 						ew += utf8err ? 0 : tmpw;
    297 					} else {
    298 						nextfont = curfont;
    299 					}
    300 					break;
    301 				}
    302 			}
    303 
    304 			if (overflow || !charexists || nextfont || utf8err)
    305 				break;
    306 			else
    307 				charexists = 0;
    308 		}
    309 
    310 		if (utf8strlen) {
    311 			if (render) {
    312 				ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
    313 				XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
    314 				                  usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
    315 			}
    316 			x += ew;
    317 			w -= ew;
    318 		}
    319 		if (utf8err && (!render || invalid_width < w)) {
    320 			if (render)
    321 				drw_text(drw, x, y, w, h, 0, invalid, invert);
    322 			x += invalid_width;
    323 			w -= invalid_width;
    324 		}
    325 		if (render && overflow)
    326 			drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
    327 
    328 		if (!*text || overflow) {
    329 			break;
    330 		} else if (nextfont) {
    331 			charexists = 0;
    332 			usedfont = nextfont;
    333 		} else {
    334 			/* Regardless of whether or not a fallback font is found, the
    335 			 * character must be drawn. */
    336 			charexists = 1;
    337 
    338 			hash = (unsigned int)utf8codepoint;
    339 			hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
    340 			hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
    341 			h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
    342 			h1 = (hash >> 17) % LENGTH(nomatches);
    343 			/* avoid expensive XftFontMatch call when we know we won't find a match */
    344 			if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
    345 				goto no_match;
    346 
    347 			fccharset = FcCharSetCreate();
    348 			FcCharSetAddChar(fccharset, utf8codepoint);
    349 
    350 			if (!drw->fonts->pattern) {
    351 				/* Refer to the comment in xfont_create for more information. */
    352 				die("the first font in the cache must be loaded from a font string.");
    353 			}
    354 
    355 			fcpattern = FcPatternDuplicate(drw->fonts->pattern);
    356 			FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
    357 			FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
    358 
    359 			FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
    360 			FcDefaultSubstitute(fcpattern);
    361 			match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
    362 
    363 			FcCharSetDestroy(fccharset);
    364 			FcPatternDestroy(fcpattern);
    365 
    366 			if (match) {
    367 				usedfont = xfont_create(drw, NULL, match);
    368 				if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
    369 					for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
    370 						; /* NOP */
    371 					curfont->next = usedfont;
    372 				} else {
    373 					xfont_free(usedfont);
    374 					nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
    375 no_match:
    376 					usedfont = drw->fonts;
    377 				}
    378 			}
    379 		}
    380 	}
    381 	if (d)
    382 		XftDrawDestroy(d);
    383 
    384 	return x + (render ? w : 0);
    385 }
    386 
    387 void
    388 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
    389 {
    390 	if (!drw)
    391 		return;
    392 
    393 	XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
    394 	XSync(drw->dpy, False);
    395 }
    396 
    397 unsigned int
    398 drw_fontset_getwidth(Drw *drw, const char *text)
    399 {
    400 	if (!drw || !drw->fonts || !text)
    401 		return 0;
    402 	return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
    403 }
    404 
    405 unsigned int
    406 drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
    407 {
    408 	unsigned int tmp = 0;
    409 	if (drw && drw->fonts && text && n)
    410 		tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
    411 	return MIN(n, tmp);
    412 }
    413 
    414 void
    415 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
    416 {
    417 	XGlyphInfo ext;
    418 
    419 	if (!font || !text)
    420 		return;
    421 
    422 	XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
    423 	if (w)
    424 		*w = ext.xOff;
    425 	if (h)
    426 		*h = font->h;
    427 }
    428 
    429 Cur *
    430 drw_cur_create(Drw *drw, int shape)
    431 {
    432 	Cur *cur;
    433 
    434 	if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
    435 		return NULL;
    436 
    437 	cur->cursor = XCreateFontCursor(drw->dpy, shape);
    438 
    439 	return cur;
    440 }
    441 
    442 void
    443 drw_cur_free(Drw *drw, Cur *cursor)
    444 {
    445 	if (!cursor)
    446 		return;
    447 
    448 	XFreeCursor(drw->dpy, cursor->cursor);
    449 	free(cursor);
    450 }