Xinqi Bao's Git

add sbase-style ecalloc(), calloc: or die
[dmenu.git] / 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;
67
68 drw = ecalloc(1, sizeof(Drw));
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 drw->fontcount = 0;
77 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
78
79 return drw;
80 }
81
82 void
83 drw_resize(Drw *drw, unsigned int w, unsigned int h)
84 {
85 if (!drw)
86 return;
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 size_t i;
98
99 for (i = 0; i < drw->fontcount; i++)
100 drw_font_free(drw->fonts[i]);
101 XFreePixmap(drw->dpy, drw->drawable);
102 XFreeGC(drw->dpy, drw->gc);
103 free(drw);
104 }
105
106 /* This function is an implementation detail. Library users should use
107 * drw_font_create instead.
108 */
109 static Fnt *
110 drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
111 {
112 Fnt *font;
113
114 if (!(fontname || fontpattern))
115 die("No font specified.\n");
116
117 if (!(font = calloc(1, sizeof(Fnt))))
118 return NULL;
119
120 if (fontname) {
121 /* Using the pattern found at font->xfont->pattern does not yield same
122 * the same substitution results as using the pattern returned by
123 * FcNameParse; using the latter results in the desired fallback
124 * behaviour whereas the former just results in
125 * missing-character-rectangles being drawn, at least with some fonts.
126 */
127 if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) ||
128 !(font->pattern = FcNameParse((FcChar8 *) fontname))) {
129 if (font->xfont) {
130 XftFontClose(drw->dpy, font->xfont);
131 font->xfont = NULL;
132 }
133 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
134 }
135 } else if (fontpattern) {
136 if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern)))
137 fprintf(stderr, "error, cannot load font pattern.\n");
138 else
139 font->pattern = NULL;
140 }
141
142 if (!font->xfont) {
143 free(font);
144 return NULL;
145 }
146
147 font->ascent = font->xfont->ascent;
148 font->descent = font->xfont->descent;
149 font->h = font->ascent + font->descent;
150 font->dpy = drw->dpy;
151
152 return font;
153 }
154
155 Fnt*
156 drw_font_create(Drw *drw, const char *fontname)
157 {
158 return drw_font_xcreate(drw, fontname, NULL);
159 }
160
161 void
162 drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount)
163 {
164 size_t i;
165 Fnt *font;
166
167 for (i = 0; i < fontcount; i++) {
168 if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
169 die("Font cache exhausted.\n");
170 } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
171 drw->fonts[drw->fontcount++] = font;
172 }
173 }
174 }
175
176 void
177 drw_font_free(Fnt *font)
178 {
179 if (!font)
180 return;
181 if (font->pattern)
182 FcPatternDestroy(font->pattern);
183 XftFontClose(font->dpy, font->xfont);
184 free(font);
185 }
186
187 Clr *
188 drw_clr_create(Drw *drw, const char *clrname)
189 {
190 Clr *clr;
191 if (!drw)
192 return NULL;
193
194 clr = ecalloc(1, sizeof(Clr));
195 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
196 DefaultColormap(drw->dpy, drw->screen),
197 clrname, &clr->rgb))
198 die("error, cannot allocate color '%s'\n", clrname);
199 clr->pix = clr->rgb.pixel;
200
201 return clr;
202 }
203
204 void
205 drw_clr_free(Clr *clr)
206 {
207 free(clr);
208 }
209
210 void
211 drw_setscheme(Drw *drw, ClrScheme *scheme)
212 {
213 if (!drw)
214 return;
215 drw->scheme = scheme;
216 }
217
218 void
219 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
220 {
221 if (!drw || !drw->scheme)
222 return;
223 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
224 if (filled)
225 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
226 else if (empty)
227 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
228 }
229
230 int
231 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
232 {
233 char buf[1024];
234 int tx, ty, th;
235 Extnts tex;
236 Colormap cmap;
237 Visual *vis;
238 XftDraw *d;
239 Fnt *curfont, *nextfont;
240 size_t i, len;
241 int utf8strlen, utf8charlen, render;
242 long utf8codepoint = 0;
243 const char *utf8str;
244 FcCharSet *fccharset;
245 FcPattern *fcpattern;
246 FcPattern *match;
247 XftResult result;
248 int charexists = 0;
249
250 if (!(render = x || y || w || h))
251 w = ~w;
252
253 if (!drw || !drw->scheme) {
254 return 0;
255 } else if (render) {
256 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->pix : drw->scheme->bg->pix);
257 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
258 }
259
260 if (!text || !drw->fontcount) {
261 return 0;
262 } else if (render) {
263 cmap = DefaultColormap(drw->dpy, drw->screen);
264 vis = DefaultVisual(drw->dpy, drw->screen);
265 d = XftDrawCreate(drw->dpy, drw->drawable, vis, cmap);
266 }
267
268 curfont = drw->fonts[0];
269 while (1) {
270 utf8strlen = 0;
271 utf8str = text;
272 nextfont = NULL;
273 while (*text) {
274 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
275 for (i = 0; i < drw->fontcount; i++) {
276 charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
277 if (charexists) {
278 if (drw->fonts[i] == curfont) {
279 utf8strlen += utf8charlen;
280 text += utf8charlen;
281 } else {
282 nextfont = drw->fonts[i];
283 }
284 break;
285 }
286 }
287
288 if (!charexists || (nextfont && nextfont != curfont))
289 break;
290 else
291 charexists = 0;
292 }
293
294 if (utf8strlen) {
295 drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
296 /* shorten text if necessary */
297 for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
298 drw_font_getexts(curfont, utf8str, len, &tex);
299
300 if (len) {
301 memcpy(buf, utf8str, len);
302 buf[len] = '\0';
303 if (len < utf8strlen)
304 for (i = len; i && i > len - 3; buf[--i] = '.');
305
306 if (render) {
307 th = curfont->ascent + curfont->descent;
308 ty = y + (h / 2) - (th / 2) + curfont->ascent;
309 tx = x + (h / 2);
310 XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
311 }
312 x += tex.w;
313 w -= tex.w;
314 }
315 }
316
317 if (!*text) {
318 break;
319 } else if (nextfont) {
320 charexists = 0;
321 curfont = nextfont;
322 } else {
323 /* Regardless of whether or not a fallback font is found, the
324 * character must be drawn.
325 */
326 charexists = 1;
327
328 if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
329 continue;
330
331 fccharset = FcCharSetCreate();
332 FcCharSetAddChar(fccharset, utf8codepoint);
333
334 if (!drw->fonts[0]->pattern) {
335 /* Refer to the comment in drw_font_xcreate for more
336 * information. */
337 die("The first font in the cache must be loaded from a font string.\n");
338 }
339
340 fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
341 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
342 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
343
344 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
345 FcDefaultSubstitute(fcpattern);
346 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
347
348 FcCharSetDestroy(fccharset);
349 FcPatternDestroy(fcpattern);
350
351 if (match) {
352 curfont = drw_font_xcreate(drw, NULL, match);
353 if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
354 drw->fonts[drw->fontcount++] = curfont;
355 } else {
356 if (curfont)
357 drw_font_free(curfont);
358 curfont = drw->fonts[0];
359 }
360 }
361 }
362 }
363 if (render)
364 XftDrawDestroy(d);
365
366 return x;
367 }
368
369 void
370 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
371 {
372 if (!drw)
373 return;
374 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
375 XSync(drw->dpy, False);
376 }
377
378 void
379 drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
380 {
381 XGlyphInfo ext;
382
383 if (!font || !text)
384 return;
385 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
386 tex->h = font->h;
387 tex->w = ext.xOff;
388 }
389
390 unsigned int
391 drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
392 {
393 Extnts tex;
394
395 if (!font)
396 return -1;
397 drw_font_getexts(font, text, len, &tex);
398 return tex.w;
399 }
400
401 Cur *
402 drw_cur_create(Drw *drw, int shape)
403 {
404 Cur *cur;
405
406 if (!drw)
407 return NULL;
408 cur = ecalloc(1, sizeof(Cur));
409 cur->cursor = XCreateFontCursor(drw->dpy, shape);
410
411 return cur;
412 }
413
414 void
415 drw_cur_free(Drw *drw, Cur *cursor)
416 {
417 if (!drw || !cursor)
418 return;
419 XFreeCursor(drw->dpy, cursor->cursor);
420 free(cursor);
421 }