summaryrefslogtreecommitdiff
blob: 4e3f32c6aabb1705860bcae7d8d7c392c134c6f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
--- a/config.mk
+++ b/config.mk
@@ -12,9 +12,13 @@
 XINERAMALIBS  = -lXinerama
 XINERAMAFLAGS = -DXINERAMA
 
+# Xft, comment if you don't want it
+XFTINC = $(shell pkg-config --cflags xft)
+XFTLIBS = $(shell pkg-config --libs xft)
+
 # includes and libs
-INCS = -I${X11INC}
-LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS}
+INCS = -I${X11INC} ${XFTINC}
+LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${XFTLIBS}
 
 # flags
 CPPFLAGS = -D_BSD_SOURCE -D_POSIX_C_SOURCE=2 -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
--- a/dmenu.1
+++ b/dmenu.1
@@ -53,7 +53,7 @@
 defines the prompt to be displayed to the left of the input field.
 .TP
 .BI \-fn " font"
-defines the font or font set used.
+defines the font or font set used. eg. "fixed" or "Monospace-12:normal" (an xft font)
 .TP
 .BI \-nb " color"
 defines the normal background color.
--- a/dmenu.c
+++ b/dmenu.c
@@ -17,6 +17,7 @@
                              * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
 #define MIN(a,b)              ((a) < (b) ? (a) : (b))
 #define MAX(a,b)              ((a) > (b) ? (a) : (b))
+#define DEFFONT "fixed" /* xft example: "Monospace-11" */
 
 typedef struct Item Item;
 struct Item {
@@ -26,6 +27,7 @@
 
 static void appenditem(Item *item, Item **list, Item **last);
 static void calcoffsets(void);
+static void cleanup(void);
 static char *cistrstr(const char *s, const char *sub);
 static void drawmenu(void);
 static void grabkeyboard(void);
@@ -50,10 +52,12 @@
 static const char *selbgcolor  = "#005577";
 static const char *selfgcolor  = "#eeeeee";
 static unsigned int lines = 0;
-static unsigned long normcol[ColLast];
-static unsigned long selcol[ColLast];
+static ColorSet *normcol;
+static ColorSet *selcol;
 static Atom clip, utf8;
 static Bool topbar = True;
+static Bool running = True;
+static int ret = 0;
 static DC *dc;
 static Item *items = NULL;
 static Item *matches, *matchend;
@@ -104,7 +108,9 @@
 			usage();
 
 	dc = initdc();
-	initfont(dc, font);
+	initfont(dc, font ? font : DEFFONT);
+	normcol = initcolor(dc, normfgcolor, normbgcolor);
+	selcol = initcolor(dc, selfgcolor, selbgcolor);
 
 	if(fast) {
 		grabkeyboard();
@@ -117,7 +123,8 @@
 	setup();
 	run();
 
-	return 1; /* unreachable */
+	cleanup();
+	return ret;
 }
 
 void
@@ -160,6 +167,15 @@
 }
 
 void
+cleanup(void) {
+    freecol(dc, normcol);
+    freecol(dc, selcol);
+    XDestroyWindow(dc->dpy, win);
+    XUngrabKeyboard(dc->dpy, CurrentTime);
+    freedc(dc);
+}
+
+void
 drawmenu(void) {
 	int curpos;
 	Item *item;
@@ -167,7 +183,7 @@
 	dc->x = 0;
 	dc->y = 0;
 	dc->h = bh;
-	drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
+	drawrect(dc, 0, 0, mw, mh, True, normcol->BG);
 
 	if(prompt) {
 		dc->w = promptw;
@@ -178,7 +194,7 @@
 	dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
 	drawtext(dc, text, normcol);
 	if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
-		drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
+		drawrect(dc, curpos, 2, 1, dc->h - 4, True, normcol->FG);
 
 	if(lines > 0) {
 		/* draw vertical list */
@@ -321,7 +337,8 @@
 		sel = matchend;
 		break;
 	case XK_Escape:
-		exit(EXIT_FAILURE);
+        ret = EXIT_FAILURE;
+        running = False;
 	case XK_Home:
 		if(sel == matches) {
 			cursor = 0;
@@ -359,7 +376,8 @@
 	case XK_Return:
 	case XK_KP_Enter:
 		puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
-		exit(EXIT_SUCCESS);
+		ret = EXIT_SUCCESS;
+		running = False;
 	case XK_Right:
 		if(text[cursor] != '\0') {
 			cursor = nextrune(+1);
@@ -490,7 +508,7 @@
 run(void) {
 	XEvent ev;
 
-	while(!XNextEvent(dc->dpy, &ev)) {
+	while(running && !XNextEvent(dc->dpy, &ev)) {
 		if(XFilterEvent(&ev, win))
 			continue;
 		switch(ev.type) {
@@ -524,11 +542,6 @@
 	XineramaScreenInfo *info;
 #endif
 
-	normcol[ColBG] = getcolor(dc, normbgcolor);
-	normcol[ColFG] = getcolor(dc, normfgcolor);
-	selcol[ColBG]  = getcolor(dc, selbgcolor);
-	selcol[ColFG]  = getcolor(dc, selfgcolor);
-
 	clip = XInternAtom(dc->dpy, "CLIPBOARD",   False);
 	utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
 
@@ -582,7 +595,7 @@
 
 	/* create menu window */
 	swa.override_redirect = True;
-	swa.background_pixel = normcol[ColBG];
+	swa.background_pixel = normcol->BG;
 	swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
 	win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
 	                    DefaultDepth(dc->dpy, screen), CopyFromParent,
--- a/draw.c
+++ b/draw.c
@@ -9,9 +9,6 @@
 
 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
-#define DEFAULTFN  "fixed"
-
-static Bool loadfont(DC *dc, const char *fontstr);
 
 void
 drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
@@ -23,7 +20,7 @@
 }
 
 void
-drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
+drawtext(DC *dc, const char *text, ColorSet *col) {
 	char buf[BUFSIZ];
 	size_t mn, n = strlen(text);
 
@@ -35,19 +32,24 @@
 	if(mn < n)
 		for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
 
-	drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
+	drawrect(dc, 0, 0, dc->w, dc->h, True, col->BG);
 	drawtextn(dc, buf, mn, col);
 }
 
 void
-drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
+drawtextn(DC *dc, const char *text, size_t n, ColorSet *col) {
 	int x = dc->x + dc->font.height/2;
 	int y = dc->y + dc->font.ascent+1;
 
-	XSetForeground(dc->dpy, dc->gc, FG(dc, col));
-	if(dc->font.set)
+	XSetForeground(dc->dpy, dc->gc, col->FG);
+	if(dc->font.xft_font) {
+		if (!dc->xftdraw)
+			eprintf("error, xft drawable does not exist");
+		XftDrawStringUtf8(dc->xftdraw, &col->FG_xft,
+			dc->font.xft_font, x, y, (unsigned char*)text, n);
+	} else if(dc->font.set) {
 		XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
-	else {
+	} else {
 		XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
 		XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
 	}
@@ -69,16 +71,33 @@
 }
 
 void
+freecol(DC *dc, ColorSet *col) {
+    if(col) {
+        if(&col->FG_xft)
+            XftColorFree(dc->dpy, DefaultVisual(dc->dpy, DefaultScreen(dc->dpy)),
+                DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)), &col->FG_xft);
+        free(col); 
+    }
+}
+
+void
 freedc(DC *dc) {
+    if(dc->font.xft_font) {
+        XftFontClose(dc->dpy, dc->font.xft_font);
+        XftDrawDestroy(dc->xftdraw);
+    }
 	if(dc->font.set)
 		XFreeFontSet(dc->dpy, dc->font.set);
-	if(dc->font.xfont)
+    if(dc->font.xfont)
 		XFreeFont(dc->dpy, dc->font.xfont);
-	if(dc->canvas)
+    if(dc->canvas)
 		XFreePixmap(dc->dpy, dc->canvas);
-	XFreeGC(dc->dpy, dc->gc);
-	XCloseDisplay(dc->dpy);
-	free(dc);
+	if(dc->gc)
+        XFreeGC(dc->dpy, dc->gc);
+	if(dc->dpy)
+        XCloseDisplay(dc->dpy);
+	if(dc)
+        free(dc);
 }
 
 unsigned long
@@ -91,6 +110,20 @@
 	return color.pixel;
 }
 
+ColorSet *
+initcolor(DC *dc, const char * foreground, const char * background) {
+	ColorSet * col = (ColorSet *)malloc(sizeof(ColorSet));
+	if(!col)
+		eprintf("error, cannot allocate memory for color set");
+	col->BG = getcolor(dc, background);
+	col->FG = getcolor(dc, foreground);
+	if(dc->font.xft_font)
+		if(!XftColorAllocName(dc->dpy, DefaultVisual(dc->dpy, DefaultScreen(dc->dpy)),
+			DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)), foreground, &col->FG_xft))
+			eprintf("error, cannot allocate xft font color '%s'\n", foreground);
+	return col;
+}
+
 DC *
 initdc(void) {
 	DC *dc;
@@ -109,23 +142,11 @@
 
 void
 initfont(DC *dc, const char *fontstr) {
-	if(!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) {
-		if(fontstr != NULL)
-			fprintf(stderr, "cannot load font '%s'\n", fontstr);
-		if(fontstr == NULL || !loadfont(dc, DEFAULTFN))
-			eprintf("cannot load font '%s'\n", DEFAULTFN);
-	}
-	dc->font.height = dc->font.ascent + dc->font.descent;
-}
-
-Bool
-loadfont(DC *dc, const char *fontstr) {
 	char *def, **missing, **names;
 	int i, n;
 	XFontStruct **xfonts;
 
-	if(!*fontstr)
-		return False;
+	missing = NULL;
 	if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
 		n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
 		for(i = 0; i < n; i++) {
@@ -133,15 +154,21 @@
 			dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
 			dc->font.width   = MAX(dc->font.width,   xfonts[i]->max_bounds.width);
 		}
-	}
-	else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
-		dc->font.ascent  = dc->font.xfont->ascent;
+    } else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
+		dc->font.ascent = dc->font.xfont->ascent;
 		dc->font.descent = dc->font.xfont->descent;
 		dc->font.width   = dc->font.xfont->max_bounds.width;
+	} else if((dc->font.xft_font = XftFontOpenName(dc->dpy, DefaultScreen(dc->dpy), fontstr))) {
+		dc->font.ascent = dc->font.xft_font->ascent;
+		dc->font.descent = dc->font.xft_font->descent;
+		dc->font.width = dc->font.xft_font->max_advance_width;
+	} else {
+		eprintf("cannot load font '%s'\n", fontstr);
 	}
 	if(missing)
 		XFreeStringList(missing);
-	return dc->font.set || dc->font.xfont;
+	dc->font.height = dc->font.ascent + dc->font.descent;
+	return;
 }
 
 void
@@ -151,20 +178,29 @@
 
 void
 resizedc(DC *dc, unsigned int w, unsigned int h) {
+	int screen = DefaultScreen(dc->dpy);
 	if(dc->canvas)
 		XFreePixmap(dc->dpy, dc->canvas);
 
 	dc->w = w;
 	dc->h = h;
 	dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
-	                           DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
+	                           DefaultDepth(dc->dpy, screen));
+	if(dc->font.xft_font && !(dc->xftdraw)) {
+		dc->xftdraw = XftDrawCreate(dc->dpy, dc->canvas, DefaultVisual(dc->dpy,screen), DefaultColormap(dc->dpy,screen));
+		if(!(dc->xftdraw))
+			eprintf("error, cannot create xft drawable\n");
+	}
 }
 
 int
 textnw(DC *dc, const char *text, size_t len) {
-	if(dc->font.set) {
+	if(dc->font.xft_font) {
+		XGlyphInfo gi;
+		XftTextExtentsUtf8(dc->dpy, dc->font.xft_font, (const FcChar8*)text, len, &gi);
+		return gi.width;
+	} else if(dc->font.set) {
 		XRectangle r;
-
 		XmbTextExtents(dc->font.set, text, len, NULL, &r);
 		return r.width;
 	}
--- a/draw.h
+++ b/draw.h
@@ -1,9 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 
-#define FG(dc, col)  ((col)[(dc)->invert ? ColBG : ColFG])
-#define BG(dc, col)  ((col)[(dc)->invert ? ColFG : ColBG])
-
-enum { ColBG, ColFG, ColBorder, ColLast };
+#include <X11/Xft/Xft.h>
 
 typedef struct {
 	int x, y, w, h;
@@ -11,6 +8,7 @@
 	Display *dpy;
 	GC gc;
 	Pixmap canvas;
+	XftDraw *xftdraw;
 	struct {
 		int ascent;
 		int descent;
@@ -18,15 +16,24 @@
 		int width;
 		XFontSet set;
 		XFontStruct *xfont;
+		XftFont *xft_font;
 	} font;
 } DC;  /* draw context */
 
+typedef struct {
+	unsigned long FG;
+	XftColor FG_xft;
+	unsigned long BG;
+} ColorSet;
+
 void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color);
-void drawtext(DC *dc, const char *text, unsigned long col[ColLast]);
-void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]);
+void drawtext(DC *dc, const char *text, ColorSet *col);
+void drawtextn(DC *dc, const char *text, size_t n, ColorSet *col);
+void freecol(DC *dc, ColorSet *col);
 void eprintf(const char *fmt, ...);
 void freedc(DC *dc);
 unsigned long getcolor(DC *dc, const char *colstr);
+ColorSet *initcolor(DC *dc, const char *foreground, const char *background);
 DC *initdc(void);
 void initfont(DC *dc, const char *fontstr);
 void mapdc(DC *dc, Window win, unsigned int w, unsigned int h);