dwm

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

drw.c (11357B)


      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 		if (w < lpad)
    253 			return x + w;
    254 		d = XftDrawCreate(drw->dpy, drw->drawable,
    255 		                  DefaultVisual(drw->dpy, drw->screen),
    256 		                  DefaultColormap(drw->dpy, drw->screen));
    257 		x += lpad;
    258 		w -= lpad;
    259 	}
    260 
    261 	usedfont = drw->fonts;
    262 	if (!ellipsis_width && render)
    263 		ellipsis_width = drw_fontset_getwidth(drw, "...");
    264 	if (!invalid_width && render)
    265 		invalid_width = drw_fontset_getwidth(drw, invalid);
    266 	while (1) {
    267 		ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0;
    268 		utf8str = text;
    269 		nextfont = NULL;
    270 		while (*text) {
    271 			utf8charlen = utf8decode(text, &utf8codepoint, &utf8err);
    272 			for (curfont = drw->fonts; curfont; curfont = curfont->next) {
    273 				charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
    274 				if (charexists) {
    275 					if (iscntrl(*text))
    276 						tmpw = 0;
    277 					else
    278 						drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
    279 					if (ew + ellipsis_width <= w) {
    280 						/* keep track where the ellipsis still fits */
    281 						ellipsis_x = x + ew;
    282 						ellipsis_w = w - ew;
    283 						ellipsis_len = utf8strlen;
    284 					}
    285 
    286 					if (ew + tmpw > w) {
    287 						overflow = 1;
    288 						/* called from drw_fontset_getwidth_clamp():
    289 						 * it wants the width AFTER the overflow
    290 						 */
    291 						if (!render)
    292 							x += tmpw;
    293 						else
    294 							utf8strlen = ellipsis_len;
    295 					} else if (curfont == usedfont) {
    296 						text += utf8charlen;
    297 						utf8strlen += utf8err ? 0 : utf8charlen;
    298 						ew += utf8err ? 0 : tmpw;
    299 					} else {
    300 						nextfont = curfont;
    301 					}
    302 					break;
    303 				}
    304 			}
    305 
    306 			if (overflow || !charexists || nextfont || utf8err)
    307 				break;
    308 			else
    309 				charexists = 0;
    310 		}
    311 
    312 		if (utf8strlen) {
    313 			if (render) {
    314 				ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
    315 				XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
    316 				                  usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
    317 			}
    318 			x += ew;
    319 			w -= ew;
    320 		}
    321 		if (utf8err && (!render || invalid_width < w)) {
    322 			if (render)
    323 				drw_text(drw, x, y, w, h, 0, invalid, invert);
    324 			x += invalid_width;
    325 			w -= invalid_width;
    326 		}
    327 		if (render && overflow)
    328 			drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
    329 
    330 		if (!*text || overflow) {
    331 			break;
    332 		} else if (nextfont) {
    333 			charexists = 0;
    334 			usedfont = nextfont;
    335 		} else {
    336 			/* Regardless of whether or not a fallback font is found, the
    337 			 * character must be drawn. */
    338 			charexists = 1;
    339 
    340 			hash = (unsigned int)utf8codepoint;
    341 			hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
    342 			hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
    343 			h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
    344 			h1 = (hash >> 17) % LENGTH(nomatches);
    345 			/* avoid expensive XftFontMatch call when we know we won't find a match */
    346 			if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
    347 				goto no_match;
    348 
    349 			fccharset = FcCharSetCreate();
    350 			FcCharSetAddChar(fccharset, utf8codepoint);
    351 
    352 			if (!drw->fonts->pattern) {
    353 				/* Refer to the comment in xfont_create for more information. */
    354 				die("the first font in the cache must be loaded from a font string.");
    355 			}
    356 
    357 			fcpattern = FcPatternDuplicate(drw->fonts->pattern);
    358 			FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
    359 			FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
    360 
    361 			FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
    362 			FcDefaultSubstitute(fcpattern);
    363 			match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
    364 
    365 			FcCharSetDestroy(fccharset);
    366 			FcPatternDestroy(fcpattern);
    367 
    368 			if (match) {
    369 				usedfont = xfont_create(drw, NULL, match);
    370 				if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
    371 					for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
    372 						; /* NOP */
    373 					curfont->next = usedfont;
    374 				} else {
    375 					xfont_free(usedfont);
    376 					nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
    377 no_match:
    378 					usedfont = drw->fonts;
    379 				}
    380 			}
    381 		}
    382 	}
    383 	if (d)
    384 		XftDrawDestroy(d);
    385 
    386 	return x + (render ? w : 0);
    387 }
    388 
    389 void
    390 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
    391 {
    392 	if (!drw)
    393 		return;
    394 
    395 	XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
    396 	XSync(drw->dpy, False);
    397 }
    398 
    399 unsigned int
    400 drw_fontset_getwidth(Drw *drw, const char *text)
    401 {
    402 	if (!drw || !drw->fonts || !text)
    403 		return 0;
    404 	return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
    405 }
    406 
    407 unsigned int
    408 drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
    409 {
    410 	unsigned int tmp = 0;
    411 	if (drw && drw->fonts && text && n)
    412 		tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
    413 	return MIN(n, tmp);
    414 }
    415 
    416 void
    417 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
    418 {
    419 	XGlyphInfo ext;
    420 
    421 	if (!font || !text)
    422 		return;
    423 
    424 	XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
    425 	if (w)
    426 		*w = ext.xOff;
    427 	if (h)
    428 		*h = font->h;
    429 }
    430 
    431 Cur *
    432 drw_cur_create(Drw *drw, int shape)
    433 {
    434 	Cur *cur;
    435 
    436 	if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
    437 		return NULL;
    438 
    439 	cur->cursor = XCreateFontCursor(drw->dpy, shape);
    440 
    441 	return cur;
    442 }
    443 
    444 void
    445 drw_cur_free(Drw *drw, Cur *cursor)
    446 {
    447 	if (!cursor)
    448 		return;
    449 
    450 	XFreeCursor(drw->dpy, cursor->cursor);
    451 	free(cursor);
    452 }