[download]

local/src/dwm/drw.c

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