summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-05-22 22:33:56 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2016-06-26 13:52:36 +0200
commit7af4d439bdb5a2e40aca69446a3367bd71431c45 (patch)
treed5608cf1398098a1b8ab5aa5d8887d5fec5b6af7
parentConfigure geometry before applying rules (diff)
downloaddwm-7af4d439bdb5a2e40aca69446a3367bd71431c45.tar.gz
dwm-7af4d439bdb5a2e40aca69446a3367bd71431c45.zip
import new drw from libsl and minor fixes.
- better scaling for occupied tag squares. - draw statusline first to omitt some complicated calculations.
-rw-r--r--config.def.h24
-rw-r--r--drw.c241
-rw-r--r--drw.h63
-rw-r--r--dwm.c85
-rw-r--r--util.h4
5 files changed, 209 insertions, 208 deletions
diff --git a/config.def.h b/config.def.h
index 7054c06..13ea4b9 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,20 +1,22 @@
1/* See LICENSE file for copyright and license details. */ 1/* See LICENSE file for copyright and license details. */
2 2
3/* appearance */ 3/* appearance */
4static const char *fonts[] = {
5 "monospace:size=10"
6};
7static const char dmenufont[] = "monospace:size=10";
8static const char normbordercolor[] = "#444444";
9static const char normbgcolor[] = "#222222";
10static const char normfgcolor[] = "#bbbbbb";
11static const char selbordercolor[] = "#005577";
12static const char selbgcolor[] = "#005577";
13static const char selfgcolor[] = "#eeeeee";
14static const unsigned int borderpx = 1; /* border pixel of windows */ 4static const unsigned int borderpx = 1; /* border pixel of windows */
15static const unsigned int snap = 32; /* snap pixel */ 5static const unsigned int snap = 32; /* snap pixel */
16static const int showbar = 1; /* 0 means no bar */ 6static const int showbar = 1; /* 0 means no bar */
17static const int topbar = 1; /* 0 means bottom bar */ 7static const int topbar = 1; /* 0 means bottom bar */
8static const char *fonts[] = { "monospace:size=10" };
9static const char dmenufont[] = "monospace:size=10";
10static const char col_gray1[] = "#222222";
11static const char col_gray2[] = "#444444";
12static const char col_gray3[] = "#bbbbbb";
13static const char col_gray4[] = "#eeeeee";
14static const char col_cyan[] = "#005577";
15static const char *colors[][3] = {
16 /* fg bg border */
17 { col_gray3, col_gray1, col_gray2}, /* normal */
18 { col_gray4, col_cyan, col_cyan }, /* selected */
19};
18 20
19/* tagging */ 21/* tagging */
20static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; 22static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
@@ -54,7 +56,7 @@ static const Layout layouts[] = {
54 56
55/* commands */ 57/* commands */
56static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ 58static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
57static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL }; 59static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
58static const char *termcmd[] = { "st", NULL }; 60static const char *termcmd[] = { "st", NULL };
59 61
60static Key keys[] = { 62static Key keys[] = {
diff --git a/drw.c b/drw.c
index f49200b..987e53b 100644
--- a/drw.c
+++ b/drw.c
@@ -63,9 +63,8 @@ utf8decode(const char *c, long *u, size_t clen)
63Drw * 63Drw *
64drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) 64drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
65{ 65{
66 Drw *drw; 66 Drw *drw = ecalloc(1, sizeof(Drw));
67 67
68 drw = ecalloc(1, sizeof(Drw));
69 drw->dpy = dpy; 68 drw->dpy = dpy;
70 drw->screen = screen; 69 drw->screen = screen;
71 drw->root = root; 70 drw->root = root;
@@ -73,7 +72,6 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
73 drw->h = h; 72 drw->h = h;
74 drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); 73 drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
75 drw->gc = XCreateGC(dpy, root, 0, NULL); 74 drw->gc = XCreateGC(dpy, root, 0, NULL);
76 drw->fontcount = 0;
77 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); 75 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
78 76
79 return drw; 77 return drw;
@@ -82,6 +80,9 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
82void 80void
83drw_resize(Drw *drw, unsigned int w, unsigned int h) 81drw_resize(Drw *drw, unsigned int w, unsigned int h)
84{ 82{
83 if (!drw)
84 return;
85
85 drw->w = w; 86 drw->w = w;
86 drw->h = h; 87 drw->h = h;
87 if (drw->drawable) 88 if (drw->drawable)
@@ -92,44 +93,39 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h)
92void 93void
93drw_free(Drw *drw) 94drw_free(Drw *drw)
94{ 95{
95 size_t i;
96
97 for (i = 0; i < drw->fontcount; i++)
98 drw_font_free(drw->fonts[i]);
99 XFreePixmap(drw->dpy, drw->drawable); 96 XFreePixmap(drw->dpy, drw->drawable);
100 XFreeGC(drw->dpy, drw->gc); 97 XFreeGC(drw->dpy, drw->gc);
101 free(drw); 98 free(drw);
102} 99}
103 100
104/* This function is an implementation detail. Library users should use 101/* This function is an implementation detail. Library users should use
105 * drw_font_create instead. 102 * drw_fontset_create instead.
106 */ 103 */
107static Fnt * 104static Fnt *
108drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) 105xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
109{ 106{
110 Fnt *font; 107 Fnt *font;
111 XftFont *xfont = NULL; 108 XftFont *xfont = NULL;
112 FcPattern *pattern = NULL; 109 FcPattern *pattern = NULL;
113 110
114 if (fontname) { 111 if (fontname) {
115 /* Using the pattern found at font->xfont->pattern does not yield same 112 /* Using the pattern found at font->xfont->pattern does not yield the
116 * the same substitution results as using the pattern returned by 113 * same substitution results as using the pattern returned by
117 * FcNameParse; using the latter results in the desired fallback 114 * FcNameParse; using the latter results in the desired fallback
118 * behaviour whereas the former just results in 115 * behaviour whereas the former just results in missing-character
119 * missing-character-rectangles being drawn, at least with some fonts. 116 * rectangles being drawn, at least with some fonts. */
120 */
121 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) { 117 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
122 fprintf(stderr, "error, cannot load font: '%s'\n", fontname); 118 fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
123 return NULL; 119 return NULL;
124 } 120 }
125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) { 121 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
126 fprintf(stderr, "error, cannot load font: '%s'\n", fontname); 122 fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
127 XftFontClose(drw->dpy, xfont); 123 XftFontClose(drw->dpy, xfont);
128 return NULL; 124 return NULL;
129 } 125 }
130 } else if (fontpattern) { 126 } else if (fontpattern) {
131 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) { 127 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
132 fprintf(stderr, "error, cannot load font pattern.\n"); 128 fprintf(stderr, "error, cannot load font from pattern.\n");
133 return NULL; 129 return NULL;
134 } 130 }
135 } else { 131 } else {
@@ -139,95 +135,115 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
139 font = ecalloc(1, sizeof(Fnt)); 135 font = ecalloc(1, sizeof(Fnt));
140 font->xfont = xfont; 136 font->xfont = xfont;
141 font->pattern = pattern; 137 font->pattern = pattern;
142 font->ascent = xfont->ascent; 138 font->h = xfont->ascent + xfont->descent;
143 font->descent = xfont->descent;
144 font->h = font->ascent + font->descent;
145 font->dpy = drw->dpy; 139 font->dpy = drw->dpy;
146 140
147 return font; 141 return font;
148} 142}
149 143
150Fnt* 144static void
151drw_font_create(Drw *drw, const char *fontname) 145xfont_free(Fnt *font)
152{ 146{
153 return drw_font_xcreate(drw, fontname, NULL); 147 if (!font)
148 return;
149 if (font->pattern)
150 FcPatternDestroy(font->pattern);
151 XftFontClose(font->dpy, font->xfont);
152 free(font);
154} 153}
155 154
156void 155Fnt*
157drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount) 156drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
158{ 157{
158 Fnt *cur, *ret = NULL;
159 size_t i; 159 size_t i;
160 Fnt *font;
161 160
162 for (i = 0; i < fontcount; i++) { 161 if (!drw || !fonts)
163 if (drw->fontcount >= DRW_FONT_CACHE_SIZE) { 162 return NULL;
164 die("font cache exhausted.\n"); 163
165 } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) { 164 for (i = 1; i <= fontcount; i++) {
166 drw->fonts[drw->fontcount++] = font; 165 if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
166 cur->next = ret;
167 ret = cur;
167 } 168 }
168 } 169 }
170 return (drw->fonts = ret);
169} 171}
170 172
171void 173void
172drw_font_free(Fnt *font) 174drw_fontset_free(Fnt *font)
173{ 175{
174 if (!font) 176 if (font) {
175 return; 177 drw_fontset_free(font->next);
176 if (font->pattern) 178 xfont_free(font);
177 FcPatternDestroy(font->pattern); 179 }
178 XftFontClose(font->dpy, font->xfont);
179 free(font);
180} 180}
181 181
182Clr * 182void
183drw_clr_create(Drw *drw, const char *clrname) 183drw_clr_create(Drw *drw, XftColor *dest, const char *clrname)
184{ 184{
185 Clr *clr; 185 if (!drw || !dest || !clrname)
186 return;
186 187
187 clr = ecalloc(1, sizeof(Clr));
188 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen), 188 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
189 DefaultColormap(drw->dpy, drw->screen), 189 DefaultColormap(drw->dpy, drw->screen),
190 clrname, &clr->rgb)) 190 clrname, dest))
191 die("error, cannot allocate color '%s'\n", clrname); 191 die("error, cannot allocate color '%s'\n", clrname);
192 clr->pix = clr->rgb.pixel; 192}
193
194/* Wrapper to create color schemes. The caller has to call free(3) on the
195 * returned color scheme when done using it. */
196Scm
197drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
198{
199 size_t i;
200 Scm ret;
201
202 /* need at least two colors for a scheme */
203 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
204 return NULL;
193 205
194 return clr; 206 for (i = 0; i < clrcount; i++)
207 drw_clr_create(drw, &ret[i], clrnames[i]);
208 return ret;
195} 209}
196 210
197void 211void
198drw_clr_free(Clr *clr) 212drw_setfontset(Drw *drw, Fnt *set)
199{ 213{
200 free(clr); 214 if (drw)
215 drw->fonts = set;
201} 216}
202 217
203void 218void
204drw_setscheme(Drw *drw, ClrScheme *scheme) 219drw_setscheme(Drw *drw, Scm scm)
205{ 220{
206 drw->scheme = scheme; 221 if (drw)
222 drw->scheme = scm;
207} 223}
208 224
209void 225void
210drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) 226drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
211{ 227{
212 if (!drw->scheme) 228 if (!drw || !drw->scheme)
213 return; 229 return;
214 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix); 230 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
215 if (filled) 231 if (filled)
216 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1); 232 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
217 else if (empty) 233 else
218 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 234 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
219} 235}
220 236
221int 237int
222drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) 238drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
223{ 239{
224 char buf[1024]; 240 char buf[1024];
225 int tx, ty, th; 241 int ty;
226 Extnts tex; 242 unsigned int ew;
227 XftDraw *d = NULL; 243 XftDraw *d = NULL;
228 Fnt *curfont, *nextfont; 244 Fnt *usedfont, *curfont, *nextfont;
229 size_t i, len; 245 size_t i, len;
230 int utf8strlen, utf8charlen, render; 246 int utf8strlen, utf8charlen, render = x || y || w || h;
231 long utf8codepoint = 0; 247 long utf8codepoint = 0;
232 const char *utf8str; 248 const char *utf8str;
233 FcCharSet *fccharset; 249 FcCharSet *fccharset;
@@ -236,66 +252,67 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
236 XftResult result; 252 XftResult result;
237 int charexists = 0; 253 int charexists = 0;
238 254
239 if (!drw->scheme || !drw->fontcount) 255 if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
240 return 0; 256 return 0;
241 257
242 if (!(render = x || y || w || h)) { 258 if (!render) {
243 w = ~w; 259 w = ~w;
244 } else { 260 } else {
245 XSetForeground(drw->dpy, drw->gc, invert ? 261 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
246 drw->scheme->fg->pix : drw->scheme->bg->pix);
247 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 262 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
248 d = XftDrawCreate(drw->dpy, drw->drawable, 263 d = XftDrawCreate(drw->dpy, drw->drawable,
249 DefaultVisual(drw->dpy, drw->screen), 264 DefaultVisual(drw->dpy, drw->screen),
250 DefaultColormap(drw->dpy, drw->screen)); 265 DefaultColormap(drw->dpy, drw->screen));
266 x += lpad;
267 w -= lpad;
251 } 268 }
252 269
253 curfont = drw->fonts[0]; 270 usedfont = drw->fonts;
254 while (1) { 271 while (1) {
255 utf8strlen = 0; 272 utf8strlen = 0;
256 utf8str = text; 273 utf8str = text;
257 nextfont = NULL; 274 nextfont = NULL;
258 while (*text) { 275 while (*text) {
259 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ); 276 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
260 for (i = 0; i < drw->fontcount; i++) { 277 for (curfont = drw->fonts; curfont; curfont = curfont->next) {
261 charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint); 278 charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
262 if (charexists) { 279 if (charexists) {
263 if (drw->fonts[i] == curfont) { 280 if (curfont == usedfont) {
264 utf8strlen += utf8charlen; 281 utf8strlen += utf8charlen;
265 text += utf8charlen; 282 text += utf8charlen;
266 } else { 283 } else {
267 nextfont = drw->fonts[i]; 284 nextfont = curfont;
268 } 285 }
269 break; 286 break;
270 } 287 }
271 } 288 }
272 289
273 if (!charexists || (nextfont && nextfont != curfont)) 290 if (!charexists || nextfont)
274 break; 291 break;
275 else 292 else
276 charexists = 0; 293 charexists = 0;
277 } 294 }
278 295
279 if (utf8strlen) { 296 if (utf8strlen) {
280 drw_font_getexts(curfont, utf8str, utf8strlen, &tex); 297 drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
281 /* shorten text if necessary */ 298 /* 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--) 299 for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
283 drw_font_getexts(curfont, utf8str, len, &tex); 300 drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
284 301
285 if (len) { 302 if (len) {
286 memcpy(buf, utf8str, len); 303 memcpy(buf, utf8str, len);
287 buf[len] = '\0'; 304 buf[len] = '\0';
288 if (len < utf8strlen) 305 if (len < utf8strlen)
289 for (i = len; i && i > len - 3; buf[--i] = '.'); 306 for (i = len; i && i > len - 3; buf[--i] = '.')
307 ; /* NOP */
290 308
291 if (render) { 309 if (render) {
292 th = curfont->ascent + curfont->descent; 310 ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
293 ty = y + (h / 2) - (th / 2) + curfont->ascent; 311 XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
294 tx = x + (h / 2); 312 usedfont->xfont, x, ty, (XftChar8 *)buf, len);
295 XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
296 } 313 }
297 x += tex.w; 314 x += ew;
298 w -= tex.w; 315 w -= ew;
299 } 316 }
300 } 317 }
301 318
@@ -303,26 +320,21 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
303 break; 320 break;
304 } else if (nextfont) { 321 } else if (nextfont) {
305 charexists = 0; 322 charexists = 0;
306 curfont = nextfont; 323 usedfont = nextfont;
307 } else { 324 } else {
308 /* Regardless of whether or not a fallback font is found, the 325 /* Regardless of whether or not a fallback font is found, the
309 * character must be drawn. 326 * character must be drawn. */
310 */
311 charexists = 1; 327 charexists = 1;
312 328
313 if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
314 continue;
315
316 fccharset = FcCharSetCreate(); 329 fccharset = FcCharSetCreate();
317 FcCharSetAddChar(fccharset, utf8codepoint); 330 FcCharSetAddChar(fccharset, utf8codepoint);
318 331
319 if (!drw->fonts[0]->pattern) { 332 if (!drw->fonts->pattern) {
320 /* Refer to the comment in drw_font_xcreate for more 333 /* Refer to the comment in xfont_create for more information. */
321 * information. */
322 die("the first font in the cache must be loaded from a font string.\n"); 334 die("the first font in the cache must be loaded from a font string.\n");
323 } 335 }
324 336
325 fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern); 337 fcpattern = FcPatternDuplicate(drw->fonts->pattern);
326 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); 338 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
327 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); 339 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
328 340
@@ -334,12 +346,14 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
334 FcPatternDestroy(fcpattern); 346 FcPatternDestroy(fcpattern);
335 347
336 if (match) { 348 if (match) {
337 curfont = drw_font_xcreate(drw, NULL, match); 349 usedfont = xfont_create(drw, NULL, match);
338 if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) { 350 if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
339 drw->fonts[drw->fontcount++] = curfont; 351 for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
352 ; /* NOP */
353 curfont->next = usedfont;
340 } else { 354 } else {
341 drw_font_free(curfont); 355 xfont_free(usedfont);
342 curfont = drw->fonts[0]; 356 usedfont = drw->fonts;
343 } 357 }
344 } 358 }
345 } 359 }
@@ -347,34 +361,40 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
347 if (d) 361 if (d)
348 XftDrawDestroy(d); 362 XftDrawDestroy(d);
349 363
350 return x; 364 return x + (render ? w : 0);
351} 365}
352 366
353void 367void
354drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) 368drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
355{ 369{
370 if (!drw)
371 return;
372
356 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); 373 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
357 XSync(drw->dpy, False); 374 XSync(drw->dpy, False);
358} 375}
359 376
360void 377unsigned int
361drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) 378drw_fontset_getwidth(Drw *drw, const char *text)
362{ 379{
363 XGlyphInfo ext; 380 if (!drw || !drw->fonts || !text)
364 381 return 0;
365 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); 382 return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
366 tex->h = font->h;
367 tex->w = ext.xOff;
368} 383}
369 384
370unsigned int 385void
371drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) 386drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
372{ 387{
373 Extnts tex; 388 XGlyphInfo ext;
374 389
375 drw_font_getexts(font, text, len, &tex); 390 if (!font || !text)
391 return;
376 392
377 return tex.w; 393 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
394 if (w)
395 *w = ext.xOff;
396 if (h)
397 *h = font->h;
378} 398}
379 399
380Cur * 400Cur *
@@ -382,7 +402,9 @@ drw_cur_create(Drw *drw, int shape)
382{ 402{
383 Cur *cur; 403 Cur *cur;
384 404
385 cur = ecalloc(1, sizeof(Cur)); 405 if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
406 return NULL;
407
386 cur->cursor = XCreateFontCursor(drw->dpy, shape); 408 cur->cursor = XCreateFontCursor(drw->dpy, shape);
387 409
388 return cur; 410 return cur;
@@ -393,6 +415,7 @@ drw_cur_free(Drw *drw, Cur *cursor)
393{ 415{
394 if (!cursor) 416 if (!cursor)
395 return; 417 return;
418
396 XFreeCursor(drw->dpy, cursor->cursor); 419 XFreeCursor(drw->dpy, cursor->cursor);
397 free(cursor); 420 free(cursor);
398} 421}
diff --git a/drw.h b/drw.h
index e3b8515..ff4355b 100644
--- a/drw.h
+++ b/drw.h
@@ -1,29 +1,19 @@
1/* See LICENSE file for copyright and license details. */ 1/* See LICENSE file for copyright and license details. */
2#define DRW_FONT_CACHE_SIZE 32
3
4typedef struct {
5 unsigned long pix;
6 XftColor rgb;
7} Clr;
8 2
9typedef struct { 3typedef struct {
10 Cursor cursor; 4 Cursor cursor;
11} Cur; 5} Cur;
12 6
13typedef struct { 7typedef struct Fnt {
14 Display *dpy; 8 Display *dpy;
15 int ascent;
16 int descent;
17 unsigned int h; 9 unsigned int h;
18 XftFont *xfont; 10 XftFont *xfont;
19 FcPattern *pattern; 11 FcPattern *pattern;
12 struct Fnt *next;
20} Fnt; 13} Fnt;
21 14
22typedef struct { 15enum { ColFg, ColBg, ColCount }; /* Scm index */
23 Clr *fg; 16typedef XftColor *Scm;
24 Clr *bg;
25 Clr *border;
26} ClrScheme;
27 17
28typedef struct { 18typedef struct {
29 unsigned int w, h; 19 unsigned int w, h;
@@ -32,43 +22,36 @@ typedef struct {
32 Window root; 22 Window root;
33 Drawable drawable; 23 Drawable drawable;
34 GC gc; 24 GC gc;
35 ClrScheme *scheme; 25 Scm scheme;
36 size_t fontcount; 26 Fnt *fonts;
37 Fnt *fonts[DRW_FONT_CACHE_SIZE];
38} Drw; 27} Drw;
39 28
40typedef struct {
41 unsigned int w;
42 unsigned int h;
43} Extnts;
44
45/* Drawable abstraction */ 29/* Drawable abstraction */
46Drw *drw_create(Display *, int, Window, unsigned int, unsigned int); 30Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
47void drw_resize(Drw *, unsigned int, unsigned int); 31void drw_resize(Drw *drw, unsigned int w, unsigned int h);
48void drw_free(Drw *); 32void drw_free(Drw *drw);
49 33
50/* Fnt abstraction */ 34/* Fnt abstraction */
51Fnt *drw_font_create(Drw *, const char *); 35Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount);
52void drw_load_fonts(Drw *, const char *[], size_t); 36void drw_fontset_free(Fnt* set);
53void drw_font_free(Fnt *); 37unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
54void drw_font_getexts(Fnt *, const char *, unsigned int, Extnts *); 38void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
55unsigned int drw_font_getexts_width(Fnt *, const char *, unsigned int);
56 39
57/* Colour abstraction */ 40/* Colorscheme abstraction */
58Clr *drw_clr_create(Drw *, const char *); 41void drw_clr_create(Drw *drw, XftColor *dest, const char *clrname);
59void drw_clr_free(Clr *); 42Scm drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
60 43
61/* Cursor abstraction */ 44/* Cursor abstraction */
62Cur *drw_cur_create(Drw *, int); 45Cur *drw_cur_create(Drw *drw, int shape);
63void drw_cur_free(Drw *, Cur *); 46void drw_cur_free(Drw *drw, Cur *cursor);
64 47
65/* Drawing context manipulation */ 48/* Drawing context manipulation */
66void drw_setfont(Drw *, Fnt *); 49void drw_setfontset(Drw *drw, Fnt *set);
67void drw_setscheme(Drw *, ClrScheme *); 50void drw_setscheme(Drw *drw, Scm scm);
68 51
69/* Drawing functions */ 52/* Drawing functions */
70void drw_rect(Drw *, int, int, unsigned int, unsigned int, int, int, int); 53void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert);
71int drw_text(Drw *, int, int, unsigned int, unsigned int, const char *, int); 54int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert);
72 55
73/* Map functions */ 56/* Map functions */
74void drw_map(Drw *, Window, int, int, unsigned int, unsigned int); 57void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);
diff --git a/dwm.c b/dwm.c
index 9585683..b2bc9bd 100644
--- a/dwm.c
+++ b/dwm.c
@@ -55,7 +55,8 @@
55#define WIDTH(X) ((X)->w + 2 * (X)->bw) 55#define WIDTH(X) ((X)->w + 2 * (X)->bw)
56#define HEIGHT(X) ((X)->h + 2 * (X)->bw) 56#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57#define TAGMASK ((1 << LENGTH(tags)) - 1) 57#define TAGMASK ((1 << LENGTH(tags)) - 1)
58#define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h) 58#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
59#define ColBorder 2
59 60
60/* enums */ 61/* enums */
61enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ 62enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
@@ -240,6 +241,7 @@ static char stext[256];
240static int screen; 241static int screen;
241static int sw, sh; /* X display screen geometry width, height */ 242static int sw, sh; /* X display screen geometry width, height */
242static int bh, blw = 0; /* bar geometry */ 243static int bh, blw = 0; /* bar geometry */
244static int lrpad; /* sum of left and right padding for text */
243static int (*xerrorxlib)(Display *, XErrorEvent *); 245static int (*xerrorxlib)(Display *, XErrorEvent *);
244static unsigned int numlockmask = 0; 246static unsigned int numlockmask = 0;
245static void (*handler[LASTEvent]) (XEvent *) = { 247static void (*handler[LASTEvent]) (XEvent *) = {
@@ -261,7 +263,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
261static Atom wmatom[WMLast], netatom[NetLast]; 263static Atom wmatom[WMLast], netatom[NetLast];
262static int running = 1; 264static int running = 1;
263static Cur *cursor[CurLast]; 265static Cur *cursor[CurLast];
264static ClrScheme scheme[SchemeLast]; 266static Scm scheme[SchemeLast];
265static Display *dpy; 267static Display *dpy;
266static Drw *drw; 268static Drw *drw;
267static Monitor *mons, *selmon; 269static Monitor *mons, *selmon;
@@ -481,11 +483,8 @@ cleanup(void)
481 cleanupmon(mons); 483 cleanupmon(mons);
482 for (i = 0; i < CurLast; i++) 484 for (i = 0; i < CurLast; i++)
483 drw_cur_free(drw, cursor[i]); 485 drw_cur_free(drw, cursor[i]);
484 for (i = 0; i < SchemeLast; i++) { 486 for (i = 0; i < SchemeLast; i++)
485 drw_clr_free(scheme[i].border); 487 free(scheme[i]);
486 drw_clr_free(scheme[i].bg);
487 drw_clr_free(scheme[i].fg);
488 }
489 drw_free(drw); 488 drw_free(drw);
490 XSync(dpy, False); 489 XSync(dpy, False);
491 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); 490 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
@@ -709,11 +708,18 @@ dirtomon(int dir)
709void 708void
710drawbar(Monitor *m) 709drawbar(Monitor *m)
711{ 710{
712 int x, xx, w, dx; 711 int x, w, sw = 0;
712 int boxs = drw->fonts->h / 9;
713 int boxw = drw->fonts->h / 6 + 2;
713 unsigned int i, occ = 0, urg = 0; 714 unsigned int i, occ = 0, urg = 0;
714 Client *c; 715 Client *c;
715 716
716 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4; 717 /* draw status first so it can be overdrawn by tags later */
718 if (m == selmon) { /* status is only drawn on selected monitor */
719 drw_setscheme(drw, scheme[SchemeNorm]);
720 sw = TEXTW(stext) - lrpad / 2; /* no right padding so status text hugs the corner */
721 drw_text(drw, m->ww - sw, 0, sw, bh, lrpad / 2 - 2, stext, 0);
722 }
717 723
718 for (c = m->clients; c; c = c->next) { 724 for (c = m->clients; c; c = c->next) {
719 occ |= c->tags; 725 occ |= c->tags;
@@ -723,36 +729,27 @@ drawbar(Monitor *m)
723 x = 0; 729 x = 0;
724 for (i = 0; i < LENGTH(tags); i++) { 730 for (i = 0; i < LENGTH(tags); i++) {
725 w = TEXTW(tags[i]); 731 w = TEXTW(tags[i]);
726 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]); 732 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
727 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i); 733 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
728 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i, 734 if (occ & 1 << i)
729 occ & 1 << i, urg & 1 << i); 735 drw_rect(drw, x + boxs, boxs, boxw, boxw,
736 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
737 urg & 1 << i);
730 x += w; 738 x += w;
731 } 739 }
732 w = blw = TEXTW(m->ltsymbol); 740 w = blw = TEXTW(m->ltsymbol);
733 drw_setscheme(drw, &scheme[SchemeNorm]); 741 drw_setscheme(drw, scheme[SchemeNorm]);
734 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0); 742 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
735 x += w; 743
736 xx = x; 744 if ((w = m->ww - sw - x) > bh) {
737 if (m == selmon) { /* status is only drawn on selected monitor */
738 w = TEXTW(stext);
739 x = m->ww - w;
740 if (x < xx) {
741 x = xx;
742 w = m->ww - xx;
743 }
744 drw_text(drw, x, 0, w, bh, stext, 0);
745 } else
746 x = m->ww;
747 if ((w = x - xx) > bh) {
748 x = xx;
749 if (m->sel) { 745 if (m->sel) {
750 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]); 746 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
751 drw_text(drw, x, 0, w, bh, m->sel->name, 0); 747 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
752 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0); 748 if (m->sel->isfloating)
749 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
753 } else { 750 } else {
754 drw_setscheme(drw, &scheme[SchemeNorm]); 751 drw_setscheme(drw, scheme[SchemeNorm]);
755 drw_rect(drw, x, 0, w, bh, 1, 0, 1); 752 drw_rect(drw, x, 0, w, bh, 1, 1);
756 } 753 }
757 } 754 }
758 drw_map(drw, m->barwin, 0, 0, m->ww, bh); 755 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
@@ -812,7 +809,7 @@ focus(Client *c)
812 detachstack(c); 809 detachstack(c);
813 attachstack(c); 810 attachstack(c);
814 grabbuttons(c, 1); 811 grabbuttons(c, 1);
815 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix); 812 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
816 setfocus(c); 813 setfocus(c);
817 } else { 814 } else {
818 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 815 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
@@ -1071,7 +1068,7 @@ manage(Window w, XWindowAttributes *wa)
1071 1068
1072 wc.border_width = c->bw; 1069 wc.border_width = c->bw;
1073 XConfigureWindow(dpy, w, CWBorderWidth, &wc); 1070 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1074 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix); 1071 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1075 configure(c); /* propagates border_width, if size doesn't change */ 1072 configure(c); /* propagates border_width, if size doesn't change */
1076 updatewindowtype(c); 1073 updatewindowtype(c);
1077 updatesizehints(c); 1074 updatesizehints(c);
@@ -1563,10 +1560,10 @@ setup(void)
1563 sh = DisplayHeight(dpy, screen); 1560 sh = DisplayHeight(dpy, screen);
1564 root = RootWindow(dpy, screen); 1561 root = RootWindow(dpy, screen);
1565 drw = drw_create(dpy, screen, root, sw, sh); 1562 drw = drw_create(dpy, screen, root, sw, sh);
1566 drw_load_fonts(drw, fonts, LENGTH(fonts)); 1563 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1567 if (!drw->fontcount)
1568 die("no fonts could be loaded.\n"); 1564 die("no fonts could be loaded.\n");
1569 bh = drw->fonts[0]->h + 2; 1565 lrpad = drw->fonts->h;
1566 bh = drw->fonts->h + 2;
1570 updategeom(); 1567 updategeom();
1571 /* init atoms */ 1568 /* init atoms */
1572 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1569 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
@@ -1586,12 +1583,8 @@ setup(void)
1586 cursor[CurResize] = drw_cur_create(drw, XC_sizing); 1583 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1587 cursor[CurMove] = drw_cur_create(drw, XC_fleur); 1584 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1588 /* init appearance */ 1585 /* init appearance */
1589 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor); 1586 scheme[SchemeNorm] = drw_scm_create(drw, colors[SchemeNorm], 3);
1590 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor); 1587 scheme[SchemeSel] = drw_scm_create(drw, colors[SchemeSel], 3);
1591 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1592 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1593 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1594 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1595 /* init bars */ 1588 /* init bars */
1596 updatebars(); 1589 updatebars();
1597 updatestatus(); 1590 updatestatus();
@@ -1751,7 +1744,7 @@ unfocus(Client *c, int setfocus)
1751 if (!c) 1744 if (!c)
1752 return; 1745 return;
1753 grabbuttons(c, 0); 1746 grabbuttons(c, 0);
1754 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix); 1747 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1755 if (setfocus) { 1748 if (setfocus) {
1756 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 1749 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1757 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 1750 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
diff --git a/util.h b/util.h
index cded043..f633b51 100644
--- a/util.h
+++ b/util.h
@@ -4,5 +4,5 @@
4#define MIN(A, B) ((A) < (B) ? (A) : (B)) 4#define MIN(A, B) ((A) < (B) ? (A) : (B))
5#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) 5#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
6 6
7void die(const char *errstr, ...); 7void die(const char *fmt, ...);
8void *ecalloc(size_t, size_t); 8void *ecalloc(size_t nmemb, size_t size);