add new layouts
[azarus-dwm.git] / drw.c
blob12e3ebc7f923971332d92e5be14be53b3b7b5a04
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>
8 #include "drw.h"
9 #include "util.h"
11 #define UTF_INVALID 0xFFFD
12 #define UTF_SIZ 4
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};
19 static long
20 utf8decodebyte(const char c, size_t *i)
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;
28 static size_t
29 utf8validate(long *u, size_t i)
31 if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
32 *u = UTF_INVALID;
33 for (i = 1; *u > utfmax[i]; ++i)
35 return i;
38 static size_t
39 utf8decode(const char *c, long *u, size_t clen)
41 size_t i, j, len, type;
42 long udecoded;
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;
55 if (j < len)
56 return 0;
57 *u = udecoded;
58 utf8validate(u, len);
60 return len;
63 Drw *
64 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap)
66 Drw *drw;
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->visual = visual;
75 drw->depth = depth;
76 drw->cmap = cmap;
77 drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
78 drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
79 drw->fontcount = 0;
80 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
82 return drw;
85 void
86 drw_resize(Drw *drw, unsigned int w, unsigned int h)
88 drw->w = w;
89 drw->h = h;
90 if (drw->drawable)
91 XFreePixmap(drw->dpy, drw->drawable);
92 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
95 void
96 drw_free(Drw *drw)
98 size_t i;
100 for (i = 0; i < drw->fontcount; i++)
101 drw_font_free(drw->fonts[i]);
102 XFreePixmap(drw->dpy, drw->drawable);
103 XFreeGC(drw->dpy, drw->gc);
104 free(drw);
107 /* This function is an implementation detail. Library users should use
108 * drw_font_create instead.
110 static Fnt *
111 drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
113 Fnt *font;
114 XftFont *xfont = NULL;
115 FcPattern *pattern = NULL;
117 if (fontname) {
118 /* Using the pattern found at font->xfont->pattern does not yield same
119 * the same substitution results as using the pattern returned by
120 * FcNameParse; using the latter results in the desired fallback
121 * behaviour whereas the former just results in
122 * missing-character-rectangles being drawn, at least with some fonts.
124 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
125 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
126 return NULL;
128 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
129 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
130 XftFontClose(drw->dpy, xfont);
131 return NULL;
133 } else if (fontpattern) {
134 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
135 fprintf(stderr, "error, cannot load font pattern.\n");
136 return NULL;
138 } else {
139 die("no font specified.\n");
142 font = ecalloc(1, sizeof(Fnt));
143 font->xfont = xfont;
144 font->pattern = pattern;
145 font->ascent = xfont->ascent;
146 font->descent = xfont->descent;
147 font->h = font->ascent + font->descent;
148 font->dpy = drw->dpy;
150 return font;
153 Fnt*
154 drw_font_create(Drw *drw, const char *fontname)
156 return drw_font_xcreate(drw, fontname, NULL);
159 void
160 drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount)
162 size_t i;
163 Fnt *font;
165 for (i = 0; i < fontcount; i++) {
166 if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
167 die("font cache exhausted.\n");
168 } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
169 drw->fonts[drw->fontcount++] = font;
174 void
175 drw_font_free(Fnt *font)
177 if (!font)
178 return;
179 if (font->pattern)
180 FcPatternDestroy(font->pattern);
181 XftFontClose(font->dpy, font->xfont);
182 free(font);
185 Clr *
186 drw_clr_create(Drw *drw, const char *clrname, unsigned int alpha)
188 Clr *clr;
190 clr = ecalloc(1, sizeof(Clr));
191 if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
192 clrname, &clr->rgb))
193 die("error, cannot allocate color '%s'\n", clrname);
194 clr->pix = (clr->rgb.pixel & 0x00ffffffU) | (alpha << 24);
196 return clr;
199 void
200 drw_clr_free(Clr *clr)
202 free(clr);
205 void
206 drw_setscheme(Drw *drw, ClrScheme *scheme)
208 drw->scheme = scheme;
211 void
212 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
214 if (!drw->scheme)
215 return;
216 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
217 if (filled)
218 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
219 else if (empty)
220 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
224 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
226 char buf[1024];
227 int tx, ty, th;
228 Extnts tex;
229 XftDraw *d = NULL;
230 Fnt *curfont, *nextfont;
231 size_t i, len;
232 int utf8strlen, utf8charlen, render;
233 long utf8codepoint = 0;
234 const char *utf8str;
235 FcCharSet *fccharset;
236 FcPattern *fcpattern;
237 FcPattern *match;
238 XftResult result;
239 int charexists = 0;
241 if (!drw->scheme || !drw->fontcount)
242 return 0;
244 if (!(render = x || y || w || h)) {
245 w = ~w;
246 } else {
247 XSetForeground(drw->dpy, drw->gc, invert ?
248 drw->scheme->fg->pix : drw->scheme->bg->pix);
249 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
250 d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
253 curfont = drw->fonts[0];
254 while (1) {
255 utf8strlen = 0;
256 utf8str = text;
257 nextfont = NULL;
258 while (*text) {
259 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
260 for (i = 0; i < drw->fontcount; i++) {
261 charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
262 if (charexists) {
263 if (drw->fonts[i] == curfont) {
264 utf8strlen += utf8charlen;
265 text += utf8charlen;
266 } else {
267 nextfont = drw->fonts[i];
269 break;
273 if (!charexists || (nextfont && nextfont != curfont))
274 break;
275 else
276 charexists = 0;
279 if (utf8strlen) {
280 drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
281 /* shorten text if necessary */
282 for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
283 drw_font_getexts(curfont, utf8str, len, &tex);
285 if (len) {
286 memcpy(buf, utf8str, len);
287 buf[len] = '\0';
288 if (len < utf8strlen)
289 for (i = len; i && i > len - 3; buf[--i] = '.');
291 if (render) {
292 th = curfont->ascent + curfont->descent;
293 ty = y + (h / 2) - (th / 2) + curfont->ascent;
294 tx = x + (h / 2);
295 XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
297 x += tex.w;
298 w -= tex.w;
302 if (!*text) {
303 break;
304 } else if (nextfont) {
305 charexists = 0;
306 curfont = nextfont;
307 } else {
308 /* Regardless of whether or not a fallback font is found, the
309 * character must be drawn.
311 charexists = 1;
313 if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
314 continue;
316 fccharset = FcCharSetCreate();
317 FcCharSetAddChar(fccharset, utf8codepoint);
319 if (!drw->fonts[0]->pattern) {
320 /* Refer to the comment in drw_font_xcreate for more
321 * information. */
322 die("the first font in the cache must be loaded from a font string.\n");
325 fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
326 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
327 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
329 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
330 FcDefaultSubstitute(fcpattern);
331 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
333 FcCharSetDestroy(fccharset);
334 FcPatternDestroy(fcpattern);
336 if (match) {
337 curfont = drw_font_xcreate(drw, NULL, match);
338 if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
339 drw->fonts[drw->fontcount++] = curfont;
340 } else {
341 drw_font_free(curfont);
342 curfont = drw->fonts[0];
347 if (d)
348 XftDrawDestroy(d);
350 return x;
353 void
354 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
356 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
357 XSync(drw->dpy, False);
360 void
361 drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
363 XGlyphInfo ext;
365 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
366 tex->h = font->h;
367 tex->w = ext.xOff;
370 unsigned int
371 drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
373 Extnts tex;
375 drw_font_getexts(font, text, len, &tex);
377 return tex.w;
380 Cur *
381 drw_cur_create(Drw *drw, int shape)
383 Cur *cur;
385 cur = ecalloc(1, sizeof(Cur));
386 cur->cursor = XCreateFontCursor(drw->dpy, shape);
388 return cur;
391 void
392 drw_cur_free(Drw *drw, Cur *cursor)
394 if (!cursor)
395 return;
396 XFreeCursor(drw->dpy, cursor->cursor);
397 free(cursor);