dwm

my heavily-patched and customized dwm build
git clone git://git.kocotian.pl/dwm.git
Log | Files | Refs | README | LICENSE

drw.c (10728B)


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