From acf56a41e9ac26a97cfb5c8841bc1707e7dcdf70 Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Tue, 12 Jun 2012 12:16:47 +0200 Subject: [PATCH] xft --- config.mk | 10 ++- draw.c | 254 +++++++++++++++++++++++++++++++----------------------- draw.h | 22 +++-- dunst.c | 62 +++++++------ dunst.h | 8 +- 5 files changed, 212 insertions(+), 144 deletions(-) diff --git a/config.mk b/config.mk index 6f31c20..095bc1c 100644 --- a/config.mk +++ b/config.mk @@ -5,18 +5,22 @@ MANPREFIX = ${PREFIX}/share/man X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib +# Xft, comment if you don't want it +XFTINC = -I/usr/include/freetype2 +XFTLIBS = -lXft -lXrender -lfreetype -lz -lfontconfig + # Xinerama, comment if you don't want it XINERAMALIBS = -lXinerama XINERAMAFLAGS = -DXINERAMA # includes and libs -INCS = -I${X11INC} -I/usr/lib/dbus-1.0/include -I/usr/include/dbus-1.0 -LIBS = -L${X11LIB} -lX11 -ldbus-1 -lpthread -lrt ${XINERAMALIBS} +INCS = -I${X11INC} -I/usr/lib/dbus-1.0/include -I/usr/include/dbus-1.0 ${XFTINC} +LIBS = -L${X11LIB} -lX11 -ldbus-1 ${XFTLIBS} -lpthread -lrt ${XINERAMALIBS} # flags CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} CFLAGS = -g -ansi -pedantic -Wall -Os ${INCS} ${CPPFLAGS} -LDFLAGS = -s ${LIBS} +LDFLAGS = ${LIBS} # compiler and linker CC = cc diff --git a/draw.c b/draw.c index 9995f5a..c89b22a 100644 --- a/draw.c +++ b/draw.c @@ -9,168 +9,210 @@ #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) { - XSetForeground(dc->dpy, dc->gc, color); - if(fill) - XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); - else - XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1); + XSetForeground(dc->dpy, dc->gc, color); + if(fill) + XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); + else + XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1); } void -drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { - char buf[BUFSIZ]; - size_t mn, n = strlen(text); +drawtext(DC *dc, const char *text, ColorSet *col) { + char buf[BUFSIZ]; + size_t mn, n = strlen(text); - /* shorten text if necessary */ - for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--) - if(mn == 0) - return; - memcpy(buf, text, mn); - if(mn < n) - for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.'); + /* shorten text if necessary */ + for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--) + if(mn == 0) + return; + memcpy(buf, text, mn); + 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)); - drawtextn(dc, buf, mn, 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]) { - int x = dc->x + dc->font.height/2; - int y = dc->y + dc->font.ascent+1; +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) - XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n); - else { - XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); - XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n); - } + XSetForeground(dc->dpy, dc->gc, col->FG); + if(dc->font.xft_font) { + if (!dc->xftdraw) + eprintf("error, xft drawable does not exist"); + printf("XftDrawStringUtf8\n"); + XftDrawStringUtf8(dc->xftdraw, &col->FG_xft, + dc->font.xft_font, x, y, (unsigned char*)text, n); + } else if(dc->font.set) { + printf("XmbDrawString\n"); + XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n); + } else { + XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); + XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n); + } } void eprintf(const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); - if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); + if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } + exit(EXIT_FAILURE); +} + +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); } - exit(EXIT_FAILURE); } void freedc(DC *dc) { - if(dc->font.set) - XFreeFontSet(dc->dpy, dc->font.set); + 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) - XFreeFont(dc->dpy, dc->font.xfont); + XFreeFont(dc->dpy, dc->font.xfont); if(dc->canvas) - XFreePixmap(dc->dpy, dc->canvas); - XFreeGC(dc->dpy, dc->gc); - XCloseDisplay(dc->dpy); - free(dc); + XFreePixmap(dc->dpy, dc->canvas); + if(dc->gc) + XFreeGC(dc->dpy, dc->gc); + if(dc->dpy) + XCloseDisplay(dc->dpy); + if(dc) + free(dc); } unsigned long getcolor(DC *dc, const char *colstr) { - Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); - XColor color; + Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); + XColor color; - if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) - eprintf("cannot allocate color '%s'\n", colstr); - return color.pixel; + if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) + eprintf("cannot allocate color '%s'\n", colstr); + 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; + DC *dc; - if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) - fputs("no locale support\n", stderr); - if(!(dc = calloc(1, sizeof *dc))) - eprintf("cannot malloc %u bytes:", sizeof *dc); - if(!(dc->dpy = XOpenDisplay(NULL))) - eprintf("cannot open display\n"); + if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) + fputs("no locale support\n", stderr); + if(!(dc = calloc(1, sizeof *dc))) + eprintf("cannot malloc %u bytes:", sizeof *dc); + if(!(dc->dpy = XOpenDisplay(NULL))) + eprintf("cannot open display\n"); - dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); - XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); - return dc; + dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); + XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); + return dc; } 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; -} + char *def, **missing, **names; + int i, n; + XFontStruct **xfonts; -Bool -loadfont(DC *dc, const char *fontstr) { - char *def, **missing, **names; - int i, n = 1; - XFontStruct **xfonts; - - if(!*fontstr) - return False; - if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) - n = XFontsOfFontSet(dc->font.set, &xfonts, &names); - else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) - xfonts = &dc->font.xfont; - else - n = 0; - - for(i = 0; i < n; i++) { - dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent); - dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent); - dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width); - } - if(missing) - XFreeStringList(missing); - return (dc->font.set || dc->font.xfont); + missing = NULL; + 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.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) { + n = XFontsOfFontSet(dc->font.set, &xfonts, &names); + for(i = 0; i < n; i++) { + dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent); + 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.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); + dc->font.height = dc->font.ascent + dc->font.descent; + return; } void mapdc(DC *dc, Window win, unsigned int w, unsigned int h) { - XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0); + XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0); } void resizedc(DC *dc, unsigned int w, unsigned int h) { - if(dc->canvas) - XFreePixmap(dc->dpy, dc->canvas); + 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))); + dc->w = w; + dc->h = h; + dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h, + DefaultDepth(dc->dpy, screen)); + if(dc->xftdraw) { + XftDrawDestroy(dc->xftdraw); + } + if(dc->font.xft_font) { + 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) { - XRectangle r; - - XmbTextExtents(dc->font.set, text, len, NULL, &r); - return r.width; - } - return XTextWidth(dc->font.xfont, text, len); + 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; + } + return XTextWidth(dc->font.xfont, text, len); } int textw(DC *dc, const char *text) { - return textnw(dc, text, strlen(text)) + dc->font.height; + return textnw(dc, text, strlen(text)) + dc->font.height; } diff --git a/draw.h b/draw.h index a69efd4..b5b139d 100644 --- a/draw.h +++ b/draw.h @@ -1,12 +1,9 @@ /* See LICENSE file for copyright and license details. */ + #ifndef DRAW_H #define DRAW_H - -#define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG]) -#define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG]) - -enum { ColBG, ColFG, ColBorder, ColLast }; +#include typedef struct { int x, y, w, h; @@ -14,6 +11,7 @@ typedef struct { Display *dpy; GC gc; Pixmap canvas; + XftDraw *xftdraw; struct { int ascent; int descent; @@ -21,19 +19,29 @@ typedef struct { 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); void resizedc(DC *dc, unsigned int w, unsigned int h); int textnw(DC *dc, const char *text, size_t len); int textw(DC *dc, const char *text); + #endif diff --git a/dunst.c b/dunst.c index a6ac681..3e23de6 100644 --- a/dunst.c +++ b/dunst.c @@ -25,6 +25,8 @@ #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) #define FONT_HEIGHT_BORDER 2 +#define DEFFONT "Monospace-11" + #define MSG 1 #define INFO 2 #define DEBUG 3 @@ -38,7 +40,8 @@ typedef struct _screen_info { /* global variables */ /* index of colors fit to urgency level */ -static unsigned long colors[3][ColLast]; +static ColorSet *colors[3]; +static const char *color_strings[3][3]; static Atom utf8; static DC *dc; static Window win; @@ -295,13 +298,13 @@ drawmsg(void) { resizedc(dc, width, height*font_h); XResizeWindow(dc->dpy, win, width, height*font_h); - drawrect(dc, 0, 0, width, height*font_h, True, BG(dc, colors[NORM])); + drawrect(dc, 0, 0, width, height*font_h, True, colors[NORM]->BG); for(i = 0; i < drawn_msg_count; i++) { if(cur_msg->start == 0) cur_msg->start = now; - drawrect(dc, 0, dc->y, width, font_h, True, BG(dc, cur_msg->colors)); + drawrect(dc, 0, dc->y, width, font_h, True, cur_msg->colors->BG); drawtext(dc, cur_msg->msg, cur_msg->colors); dc->y += font_h; @@ -310,7 +313,8 @@ drawmsg(void) { } if(hidden_count) { - drawrect(dc, 0, dc->y, width, font_h, True, BG(dc, colors[NORM])); + drawrect(dc, 0, dc->y, width, font_h, True, colors[NORM]->BG); + drawtext(dc, hidden, colors[hidden_color_idx]); dc->y += font_h; } @@ -443,21 +447,10 @@ handleXEvents(void) { } } -static void -_set_color(msg_queue_t *msg, int color_idx) { - Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); - XColor color; - if(msg->color_strings[color_idx] == NULL - || !XAllocNamedColor(dc->dpy, cmap, - msg->color_strings[color_idx], &color, &color)) { - msg->colors[color_idx] = colors[msg->urgency][color_idx]; - } else { - msg->colors[color_idx] = color.pixel; - } -} - void initmsg(msg_queue_t *msg) { + const char *fg = NULL; + const char *bg = NULL; msg->format = format; apply_rules(msg); @@ -471,8 +464,21 @@ initmsg(msg_queue_t *msg) { /* urgency > CRIT -> array out of range */ msg->urgency = msg->urgency > CRIT ? CRIT : msg->urgency; - _set_color(msg, ColFG); - _set_color(msg, ColBG); + + + if (msg->color_strings[ColFG]) { + fg = msg->color_strings[ColFG]; + } else { + fg = color_strings[ColFG][msg->urgency]; + } + + if (msg->color_strings[ColBG]) { + bg = msg->color_strings[ColBG]; + } else { + bg = color_strings[ColBG][msg->urgency]; + } + + msg->colors = initcolor(dc, fg, bg); msg->timeout = msg->timeout == -1 ? timeouts[msg->urgency] : msg->timeout; msg->start = 0; @@ -538,13 +544,6 @@ setup(void) { } root = RootWindow(dc->dpy, DefaultScreen(dc->dpy)); - colors[0][ColBG] = getcolor(dc, lowbgcolor); - colors[0][ColFG] = getcolor(dc, lowfgcolor); - colors[1][ColBG] = getcolor(dc, normbgcolor); - colors[1][ColFG] = getcolor(dc, normfgcolor); - colors[2][ColBG] = getcolor(dc, critbgcolor); - colors[2][ColFG] = getcolor(dc, critfgcolor); - utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); /* menu geometry */ @@ -736,6 +735,17 @@ main(int argc, char *argv[]) { initdbus(); initfont(dc, font); + colors[LOW] = initcolor(dc, lowfgcolor, lowbgcolor); + colors[NORM] = initcolor(dc, normfgcolor, normbgcolor); + colors[CRIT] = initcolor(dc, critfgcolor, critbgcolor); + + color_strings[ColFG][LOW] = lowfgcolor; + color_strings[ColFG][NORM] = normfgcolor; + color_strings[ColFG][LOW] = lowfgcolor; + + color_strings[ColBG][LOW] = lowbgcolor; + color_strings[ColBG][NORM] = normbgcolor; + color_strings[ColBG][CRIT] = critbgcolor; setup(); if(msgqueue != NULL) { show_win(); diff --git a/dunst.h b/dunst.h index 8a4d9f9..5604160 100644 --- a/dunst.h +++ b/dunst.h @@ -7,6 +7,10 @@ #define NORM 1 #define CRIT 2 +#define ColLast 2 +#define ColFG 1 +#define ColBG 0 + typedef struct _rule_t { /* filters */ char *appname; @@ -33,8 +37,8 @@ typedef struct _msg_queue_t { time_t start; int timeout; int urgency; - unsigned long colors[ColLast]; - char *color_strings[ColLast]; + ColorSet *colors; + char *color_strings[2]; } msg_queue_t; typedef struct _dimension_t {