Merge branch 'xft' into next

Conflicts:
	config.mk
This commit is contained in:
Sascha Kruse 2012-06-12 13:59:51 +02:00
commit 9b4d9ec205
5 changed files with 212 additions and 144 deletions

View File

@ -5,18 +5,22 @@ MANPREFIX = ${PREFIX}/share/man
X11INC = /usr/X11R6/include X11INC = /usr/X11R6/include
X11LIB = /usr/X11R6/lib 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 # Xinerama, comment if you don't want it
XINERAMALIBS = -lXinerama XINERAMALIBS = -lXinerama
XINERAMAFLAGS = -DXINERAMA XINERAMAFLAGS = -DXINERAMA
# includes and libs # includes and libs
INCS = -I${X11INC} -I/usr/lib/dbus-1.0/include -I/usr/include/dbus-1.0 INCS = -I${X11INC} -I/usr/lib/dbus-1.0/include -I/usr/include/dbus-1.0 ${XFTINC}
LIBS = -L${X11LIB} -lX11 -lXext -lXss -ldbus-1 -lpthread -lrt -liniparser ${XINERAMALIBS} LIBS = -L${X11LIB} -lX11 -lXext -lXss -ldbus-1 ${XFTLIBS} -lpthread -liniparser lrt ${XINERAMALIBS}
# flags # flags
CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
CFLAGS = -g -ansi -pedantic -Wall -Os ${INCS} ${CPPFLAGS} CFLAGS = -g -ansi -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
LDFLAGS = -s ${LIBS} LDFLAGS = ${LIBS}
# compiler and linker # compiler and linker
CC = cc CC = cc

254
draw.c
View File

@ -9,168 +9,210 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(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 void
drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) { drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
XSetForeground(dc->dpy, dc->gc, color); XSetForeground(dc->dpy, dc->gc, color);
if(fill) if(fill)
XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h);
else else
XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1); XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1);
} }
void void
drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { drawtext(DC *dc, const char *text, ColorSet *col) {
char buf[BUFSIZ]; char buf[BUFSIZ];
size_t mn, n = strlen(text); size_t mn, n = strlen(text);
/* shorten text if necessary */ /* shorten text if necessary */
for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--) for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--)
if(mn == 0) if(mn == 0)
return; return;
memcpy(buf, text, mn); memcpy(buf, text, mn);
if(mn < n) if(mn < n)
for(n = MAX(mn-3, 0); n < mn; buf[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); drawtextn(dc, buf, mn, col);
} }
void 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 x = dc->x + dc->font.height/2;
int y = dc->y + dc->font.ascent+1; int y = dc->y + dc->font.ascent+1;
XSetForeground(dc->dpy, dc->gc, FG(dc, col)); XSetForeground(dc->dpy, dc->gc, col->FG);
if(dc->font.set) if(dc->font.xft_font) {
XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n); if (!dc->xftdraw)
else { eprintf("error, xft drawable does not exist");
XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); printf("XftDrawStringUtf8\n");
XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, 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 void
eprintf(const char *fmt, ...) { eprintf(const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr); fputc(' ', stderr);
perror(NULL); 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 void
freedc(DC *dc) { freedc(DC *dc) {
if(dc->font.set) if(dc->font.xft_font) {
XFreeFontSet(dc->dpy, dc->font.set); 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); XFreeFont(dc->dpy, dc->font.xfont);
if(dc->canvas) if(dc->canvas)
XFreePixmap(dc->dpy, dc->canvas); XFreePixmap(dc->dpy, dc->canvas);
XFreeGC(dc->dpy, dc->gc); if(dc->gc)
XCloseDisplay(dc->dpy); XFreeGC(dc->dpy, dc->gc);
free(dc); if(dc->dpy)
XCloseDisplay(dc->dpy);
if(dc)
free(dc);
} }
unsigned long unsigned long
getcolor(DC *dc, const char *colstr) { getcolor(DC *dc, const char *colstr) {
Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
XColor color; XColor color;
if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
eprintf("cannot allocate color '%s'\n", colstr); eprintf("cannot allocate color '%s'\n", colstr);
return color.pixel; 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 * DC *
initdc(void) { initdc(void) {
DC *dc; DC *dc;
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fputs("no locale support\n", stderr); fputs("no locale support\n", stderr);
if(!(dc = calloc(1, sizeof *dc))) if(!(dc = calloc(1, sizeof *dc)))
eprintf("cannot malloc %u bytes:", sizeof *dc); eprintf("cannot malloc %u bytes:", sizeof *dc);
if(!(dc->dpy = XOpenDisplay(NULL))) if(!(dc->dpy = XOpenDisplay(NULL)))
eprintf("cannot open display\n"); eprintf("cannot open display\n");
dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
return dc; return dc;
} }
void void
initfont(DC *dc, const char *fontstr) { initfont(DC *dc, const char *fontstr) {
if(!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) { char *def, **missing, **names;
if(fontstr != NULL) int i, n;
fprintf(stderr, "cannot load font '%s'\n", fontstr); XFontStruct **xfonts;
if(fontstr == NULL || !loadfont(dc, DEFAULTFN))
eprintf("cannot load font '%s'\n", DEFAULTFN);
}
dc->font.height = dc->font.ascent + dc->font.descent;
}
Bool missing = NULL;
loadfont(DC *dc, const char *fontstr) { if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
char *def, **missing, **names; dc->font.ascent = dc->font.xfont->ascent;
int i, n = 1; dc->font.descent = dc->font.xfont->descent;
XFontStruct **xfonts; dc->font.width = dc->font.xfont->max_bounds.width;
} else if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
if(!*fontstr) n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
return False; for(i = 0; i < n; i++) {
if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
n = XFontsOfFontSet(dc->font.set, &xfonts, &names); dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width);
xfonts = &dc->font.xfont; }
else } else if((dc->font.xft_font = XftFontOpenName(dc->dpy, DefaultScreen(dc->dpy), fontstr))) {
n = 0; dc->font.ascent = dc->font.xft_font->ascent;
dc->font.descent = dc->font.xft_font->descent;
for(i = 0; i < n; i++) { dc->font.width = dc->font.xft_font->max_advance_width;
dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent); } else {
dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent); eprintf("cannot load font '%s'\n", fontstr);
dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width); }
} if(missing)
if(missing) XFreeStringList(missing);
XFreeStringList(missing); dc->font.height = dc->font.ascent + dc->font.descent;
return (dc->font.set || dc->font.xfont); return;
} }
void void
mapdc(DC *dc, Window win, unsigned int w, unsigned int h) { 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 void
resizedc(DC *dc, unsigned int w, unsigned int h) { resizedc(DC *dc, unsigned int w, unsigned int h) {
if(dc->canvas) int screen = DefaultScreen(dc->dpy);
XFreePixmap(dc->dpy, dc->canvas); if(dc->canvas)
XFreePixmap(dc->dpy, dc->canvas);
dc->w = w; dc->w = w;
dc->h = h; dc->h = h;
dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h, dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
DefaultDepth(dc->dpy, DefaultScreen(dc->dpy))); 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 int
textnw(DC *dc, const char *text, size_t len) { textnw(DC *dc, const char *text, size_t len) {
if(dc->font.set) { if(dc->font.xft_font) {
XRectangle r; XGlyphInfo gi;
XftTextExtentsUtf8(dc->dpy, dc->font.xft_font, (const FcChar8*)text, len, &gi);
XmbTextExtents(dc->font.set, text, len, NULL, &r); return gi.width;
return r.width; } else if(dc->font.set) {
} XRectangle r;
return XTextWidth(dc->font.xfont, text, len); XmbTextExtents(dc->font.set, text, len, NULL, &r);
return r.width;
}
return XTextWidth(dc->font.xfont, text, len);
} }
int int
textw(DC *dc, const char *text) { textw(DC *dc, const char *text) {
return textnw(dc, text, strlen(text)) + dc->font.height; return textnw(dc, text, strlen(text)) + dc->font.height;
} }

22
draw.h
View File

@ -1,12 +1,9 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#ifndef DRAW_H #ifndef DRAW_H
#define DRAW_H #define DRAW_H
#include <X11/Xft/Xft.h>
#define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG])
#define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG])
enum { ColBG, ColFG, ColBorder, ColLast };
typedef struct { typedef struct {
int x, y, w, h; int x, y, w, h;
@ -14,6 +11,7 @@ typedef struct {
Display *dpy; Display *dpy;
GC gc; GC gc;
Pixmap canvas; Pixmap canvas;
XftDraw *xftdraw;
struct { struct {
int ascent; int ascent;
int descent; int descent;
@ -21,19 +19,29 @@ typedef struct {
int width; int width;
XFontSet set; XFontSet set;
XFontStruct *xfont; XFontStruct *xfont;
XftFont *xft_font;
} font; } font;
} DC; /* draw context */ } 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 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 drawtext(DC *dc, const char *text, ColorSet *col);
void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]); void drawtextn(DC *dc, const char *text, size_t n, ColorSet *col);
void freecol(DC *dc, ColorSet *col);
void eprintf(const char *fmt, ...); void eprintf(const char *fmt, ...);
void freedc(DC *dc); void freedc(DC *dc);
unsigned long getcolor(DC *dc, const char *colstr); unsigned long getcolor(DC *dc, const char *colstr);
ColorSet *initcolor(DC *dc, const char *foreground, const char *background);
DC *initdc(void); DC *initdc(void);
void initfont(DC *dc, const char *fontstr); void initfont(DC *dc, const char *fontstr);
void mapdc(DC *dc, Window win, unsigned int w, unsigned int h); void mapdc(DC *dc, Window win, unsigned int w, unsigned int h);
void resizedc(DC *dc, 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 textnw(DC *dc, const char *text, size_t len);
int textw(DC *dc, const char *text); int textw(DC *dc, const char *text);
#endif #endif

62
dunst.c
View File

@ -27,6 +27,8 @@
#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
#define FONT_HEIGHT_BORDER 2 #define FONT_HEIGHT_BORDER 2
#define DEFFONT "Monospace-11"
#define MSG 1 #define MSG 1
#define INFO 2 #define INFO 2
#define DEBUG 3 #define DEBUG 3
@ -60,7 +62,8 @@ int verbosity = 0;
rule_t *rules = NULL; rule_t *rules = NULL;
/* index of colors fit to urgency level */ /* 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 Atom utf8;
static DC *dc; static DC *dc;
static Window win; static Window win;
@ -358,13 +361,13 @@ drawmsg(void) {
resizedc(dc, width, height*font_h); resizedc(dc, width, height*font_h);
XResizeWindow(dc->dpy, win, 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++) { for(i = 0; i < drawn_msg_count; i++) {
if(cur_msg->start == 0) if(cur_msg->start == 0)
cur_msg->start = now; 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); drawtext(dc, cur_msg->msg, cur_msg->colors);
dc->y += font_h; dc->y += font_h;
@ -373,7 +376,8 @@ drawmsg(void) {
} }
if(hidden_count) { 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]); drawtext(dc, hidden, colors[hidden_color_idx]);
dc->y += font_h; dc->y += font_h;
} }
@ -506,21 +510,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 void
initmsg(msg_queue_t *msg) { initmsg(msg_queue_t *msg) {
const char *fg = NULL;
const char *bg = NULL;
msg->format = format; msg->format = format;
apply_rules(msg); apply_rules(msg);
@ -534,8 +527,21 @@ initmsg(msg_queue_t *msg) {
/* urgency > CRIT -> array out of range */ /* urgency > CRIT -> array out of range */
msg->urgency = msg->urgency > CRIT ? CRIT : msg->urgency; 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->timeout = msg->timeout == -1 ? timeouts[msg->urgency] : msg->timeout;
msg->start = 0; msg->start = 0;
@ -630,13 +636,6 @@ setup(void) {
} }
root = RootWindow(dc->dpy, DefaultScreen(dc->dpy)); 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); utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
/* menu geometry */ /* menu geometry */
@ -1010,6 +1009,17 @@ main(int argc, char *argv[]) {
initdbus(); initdbus();
initfont(dc, font); 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(); setup();
if(msgqueue != NULL) { if(msgqueue != NULL) {
show_win(); show_win();

View File

@ -7,6 +7,10 @@
#define NORM 1 #define NORM 1
#define CRIT 2 #define CRIT 2
#define ColLast 2
#define ColFG 1
#define ColBG 0
typedef struct _rule_t { typedef struct _rule_t {
char *name; char *name;
/* filters */ /* filters */
@ -36,8 +40,8 @@ typedef struct _msg_queue_t {
time_t start; time_t start;
int timeout; int timeout;
int urgency; int urgency;
unsigned long colors[ColLast]; ColorSet *colors;
char *color_strings[ColLast]; char *color_strings[2];
} msg_queue_t; } msg_queue_t;
typedef struct _dimension_t { typedef struct _dimension_t {