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
14static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
15static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
16static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
17static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
18
19static long
20utf8decodebyte(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
28static size_t
29utf8validate(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
38static size_t
39utf8decode(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
63Drw *
64drw_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
80void
81drw_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
93void
94drw_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 */
105static Fnt *
106xfont_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 font = ecalloc(1, sizeof(Fnt));
137 font->xfont = xfont;
138 font->pattern = pattern;
139 font->h = xfont->ascent + xfont->descent;
140 font->dpy = drw->dpy;
141
142 return font;
143}
144
145static void
146xfont_free(Fnt *font)
147{
148 if (!font)
149 return;
150 if (font->pattern)
151 FcPatternDestroy(font->pattern);
152 XftFontClose(font->dpy, font->xfont);
153 free(font);
154}
155
156Fnt*
157drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
158{
159 Fnt *cur, *ret = NULL;
160 size_t i;
161
162 if (!drw || !fonts)
163 return NULL;
164
165 for (i = 1; i <= fontcount; i++) {
166 if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
167 cur->next = ret;
168 ret = cur;
169 }
170 }
171 return (drw->fonts = ret);
172}
173
174void
175drw_fontset_free(Fnt *font)
176{
177 if (font) {
178 drw_fontset_free(font->next);
179 xfont_free(font);
180 }
181}
182
183void
184drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
185{
186 if (!drw || !dest || !clrname)
187 return;
188
189 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
190 DefaultColormap(drw->dpy, drw->screen),
191 clrname, dest))
192 die("error, cannot allocate color '%s'", clrname);
193}
194
195/* Wrapper to create color schemes. The caller has to call free(3) on the
196 * returned color scheme when done using it. */
197Clr *
198drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
199{
200 size_t i;
201 Clr *ret;
202
203 /* need at least two colors for a scheme */
204 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
205 return NULL;
206
207 for (i = 0; i < clrcount; i++)
208 drw_clr_create(drw, &ret[i], clrnames[i]);
209 return ret;
210}
211
212void
213drw_setfontset(Drw *drw, Fnt *set)
214{
215 if (drw)
216 drw->fonts = set;
217}
218
219void
220drw_setscheme(Drw *drw, Clr *scm)
221{
222 if (drw)
223 drw->scheme = scm;
224}
225
226void
227drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
228{
229 if (!drw || !drw->scheme)
230 return;
231 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
232 if (filled)
233 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
234 else
235 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
236}
237
238int
239drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
240{
241 int ty, ellipsis_x = 0;
242 unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
243 XftDraw *d = NULL;
244 Fnt *usedfont, *curfont, *nextfont;
245 int utf8strlen, utf8charlen, render = x || y || w || h;
246 long utf8codepoint = 0;
247 const char *utf8str;
248 FcCharSet *fccharset;
249 FcPattern *fcpattern;
250 FcPattern *match;
251 XftResult result;
252 int charexists = 0, overflow = 0;
253 /* keep track of a couple codepoints for which we have no match. */
254 static unsigned int nomatches[128], ellipsis_width;
255
256 if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
257 return 0;
258
259 if (!render) {
260 w = invert ? invert : ~invert;
261 } else {
262 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
263 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
264 d = XftDrawCreate(drw->dpy, drw->drawable,
265 DefaultVisual(drw->dpy, drw->screen),
266 DefaultColormap(drw->dpy, drw->screen));
267 x += lpad;
268 w -= lpad;
269 }
270
271 usedfont = drw->fonts;
272 if (!ellipsis_width && render)
273 ellipsis_width = drw_fontset_getwidth(drw, "...");
274 while (1) {
275 utf8strlen = 0;
276 ew = ellipsis_len = utf8strlen = 0;
277 utf8str = text;
278 nextfont = NULL;
279 while (*text) {
280 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
281 for (curfont = drw->fonts; curfont; curfont = curfont->next) {
282 charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
283 if (charexists) {
284 drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
285 if (ew + ellipsis_width <= w) {
286 /* keep track where the ellipsis still fits */
287 ellipsis_x = x + ew;
288 ellipsis_w = w - ew;
289 ellipsis_len = utf8strlen;
290 }
291
292 if (ew + tmpw > w) {
293 overflow = 1;
294 /* called from drw_fontset_getwidth_clamp():
295 * it wants the width AFTER the overflow
296 */
297 if (!render)
298 x += tmpw;
299 else
300 utf8strlen = ellipsis_len;
301 } else if (curfont == usedfont) {
302 utf8strlen += utf8charlen;
303 text += utf8charlen;
304 ew += tmpw;
305 } else {
306 nextfont = curfont;
307 }
308 break;
309 }
310 }
311
312 if (overflow || !charexists || nextfont)
313 break;
314 else
315 charexists = 0;
316 }
317
318 if (utf8strlen) {
319 if (render) {
320 ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
321 XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
322 usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
323 }
324 x += ew;
325 w -= ew;
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;
377no_match:
378 usedfont = drw->fonts;
379 }
380 }
381 }
382 }
383 if (d)
384 XftDrawDestroy(d);
385
386 return x + (render ? w : 0);
387}
388
389void
390drw_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
399unsigned int
400drw_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
407unsigned int
408drw_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
416void
417drw_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
431Cur *
432drw_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
444void
445drw_cur_free(Drw *drw, Cur *cursor)
446{
447 if (!cursor)
448 return;
449
450 XFreeCursor(drw->dpy, cursor->cursor);
451 free(cursor);
452}