dwm

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

drw.c (11112B)


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