diff --git a/awesome.c b/awesome.c index 4e7c8bd6..75788b0b 100644 --- a/awesome.c +++ b/awesome.c @@ -67,12 +67,9 @@ cleanup(DC *drawcontext, awesome_config *awesomeconf) for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++) { - if(drawcontext[screen].font.set) - XFreeFontSet(awesomeconf->display, drawcontext[screen].font.set); - else - XFreeFont(awesomeconf->display, drawcontext[screen].font.xfont); + XftFontClose(awesomeconf->display, drawcontext->font); - XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, screen)); + XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, screen)); XFreePixmap(awesomeconf->display, awesomeconf[screen].statusbar.drawable); XFreeGC(awesomeconf->display, drawcontext[screen].gc); @@ -206,14 +203,11 @@ setup(DC *drawcontext, awesome_config *awesomeconf) compileregs(awesomeconf->rules, awesomeconf->nrules); /* bar */ - drawcontext->h = awesomeconf->statusbar.height = drawcontext->font.height + 2; + drawcontext->h = awesomeconf->statusbar.height = drawcontext->font->height + 2; initstatusbar(awesomeconf->display, awesomeconf->screen, drawcontext, &awesomeconf->statusbar); drawcontext->gc = XCreateGC(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), 0, 0); XSetLineAttributes(awesomeconf->display, drawcontext->gc, 1, LineSolid, CapButt, JoinMiter); - if(!drawcontext->font.set) - XSetFont(awesomeconf->display, drawcontext->gc, drawcontext->font.xfont->fid); - loadawesomeprops(awesomeconf->display, awesomeconf); } diff --git a/awesomerc b/awesomerc index 0492ceed..50e7734a 100644 --- a/awesomerc +++ b/awesomerc @@ -11,7 +11,7 @@ awesome: # Master window width factor (used by tile layouts) mwfact = 0.6; # Font - font = "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*"; + font = "fixed-12"; # Tags tags = ( "1", "2", "3", "4", "5", "6", "7", "8", "9" ); # Colors diff --git a/config.c b/config.c index fa78dbab..8729f991 100644 --- a/config.c +++ b/config.c @@ -38,8 +38,7 @@ #include "layouts/max.h" #include "layouts/floating.h" -static void initfont(Display *, DC *, const char *); -static unsigned long initcolor(Display *, int, const char *); +static XColor initxcolor(Display *, int, const char *); static unsigned int get_numlockmask(Display *); /** Link a name to a function */ @@ -180,6 +179,7 @@ parse_config(Display * disp, int scr, DC * drawcontext, const char *confpatharg, char *confpath; KeySym tmp_key; ssize_t confpath_len; + XColor colorbuf; if(confpatharg) confpath = a_strdup(confpatharg); @@ -211,7 +211,10 @@ parse_config(Display * disp, int scr, DC * drawcontext, const char *confpatharg, /* font */ tmp = config_lookup_string(&awesomelibconf, "awesome.font"); - initfont(disp, drawcontext, tmp ? tmp : "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*"); + drawcontext->font = XftFontOpenName(disp, awesomeconf->phys_screen, tmp ? tmp : "sans-12"); + + if(!drawcontext->font) + eprint("awesome: cannot init font\n"); /* layouts */ conflayouts = config_lookup(&awesomelibconf, "awesome.layouts"); @@ -253,7 +256,9 @@ parse_config(Display * disp, int scr, DC * drawcontext, const char *confpatharg, for(i = 0; i < awesomeconf->nlayouts; i++) { - j = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->layouts[i].symbol, drawcontext->font.height); + j = drawcontext->font->height + + textwidth(disp, drawcontext->font, + awesomeconf->layouts[i].symbol, a_strlen(awesomeconf->layouts[i].symbol)); if(j > awesomeconf->statusbar.width) awesomeconf->statusbar.width = j; } @@ -399,79 +404,35 @@ parse_config(Display * disp, int scr, DC * drawcontext, const char *confpatharg, /* colors */ tmp = config_lookup_string(&awesomelibconf, "awesome.normal_border_color"); - drawcontext->norm[ColBorder] = initcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#dddddd"); + colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#dddddd"); + drawcontext->norm[ColBorder] = colorbuf.pixel; tmp = config_lookup_string(&awesomelibconf, "awesome.normal_bg_color"); - drawcontext->norm[ColBG] = initcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#000000"); + colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#000000"); + drawcontext->norm[ColBG] = colorbuf.pixel; tmp = config_lookup_string(&awesomelibconf, "awesome.normal_fg_color"); - drawcontext->norm[ColFG] = initcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#ffffff"); + colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#ffffff"); + drawcontext->norm[ColFG] = colorbuf.pixel; + drawcontext->text_normal = colorbuf; tmp = config_lookup_string(&awesomelibconf, "awesome.focus_border_color"); - drawcontext->sel[ColBorder] = initcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#008b8b"); + colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#008b8b"); + drawcontext->sel[ColBorder] = colorbuf.pixel; tmp = config_lookup_string(&awesomelibconf, "awesome.focus_bg_color"); - drawcontext->sel[ColBG] = initcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#008b8b"); + colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#008b8b"); + drawcontext->sel[ColBG] = colorbuf.pixel; tmp = config_lookup_string(&awesomelibconf, "awesome.focus_fg_color"); - drawcontext->sel[ColFG] = initcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#ffffff"); + colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#ffffff"); + drawcontext->sel[ColFG] = colorbuf.pixel; + drawcontext->text_selected = colorbuf; config_destroy(&awesomelibconf); p_delete(&confpath); } -/** Initialize font from X side and store in draw context - * \param fontstr Font name - * \param disp Display ref - * \param drawcontext Draw context - */ -static void -initfont(Display *disp, DC *drawcontext, const char *fontstr) -{ - char *def, **missing; - int i, n; - - missing = NULL; - if(drawcontext->font.set) - XFreeFontSet(disp, drawcontext->font.set); - drawcontext->font.set = XCreateFontSet(disp, fontstr, &missing, &n, &def); - if(missing) - { - while(n--) - fprintf(stderr, "awesome: missing fontset: %s\n", missing[n]); - XFreeStringList(missing); - } - if(drawcontext->font.set) - { - XFontSetExtents *font_extents; - XFontStruct **xfonts; - char **font_names; - drawcontext->font.ascent = drawcontext->font.descent = 0; - font_extents = XExtentsOfFontSet(drawcontext->font.set); - n = XFontsOfFontSet(drawcontext->font.set, &xfonts, &font_names); - for(i = 0, drawcontext->font.ascent = 0, drawcontext->font.descent = 0; i < n; i++) - { - if(drawcontext->font.ascent < (*xfonts)->ascent) - drawcontext->font.ascent = (*xfonts)->ascent; - if(drawcontext->font.descent < (*xfonts)->descent) - drawcontext->font.descent = (*xfonts)->descent; - xfonts++; - } - } - else - { - if(drawcontext->font.xfont) - XFreeFont(disp, drawcontext->font.xfont); - drawcontext->font.xfont = NULL; - if(!(drawcontext->font.xfont = XLoadQueryFont(disp, fontstr)) - && !(drawcontext->font.xfont = XLoadQueryFont(disp, "fixed"))) - die("awesome: error, cannot load font: '%s'\n", fontstr); - drawcontext->font.ascent = drawcontext->font.xfont->ascent; - drawcontext->font.descent = drawcontext->font.xfont->descent; - } - drawcontext->font.height = drawcontext->font.ascent + drawcontext->font.descent; -} - static unsigned int get_numlockmask(Display *disp) { @@ -499,12 +460,11 @@ get_numlockmask(Display *disp) * \param scr Screen number * \return XColor pixel */ -static unsigned long -initcolor(Display *disp, int scr, const char *colstr) +static XColor +initxcolor(Display *disp, int scr, const char *colstr) { - Colormap cmap = DefaultColormap(disp, scr); XColor color; - if(!XAllocNamedColor(disp, cmap, colstr, &color, &color)) + if(!XAllocNamedColor(disp, DefaultColormap(disp, scr), colstr, &color, &color)) die("awesome: error, cannot allocate color '%s'\n", colstr); - return color.pixel; + return color; } diff --git a/config.h b/config.h index b950163e..8c9e3635 100644 --- a/config.h +++ b/config.h @@ -25,6 +25,7 @@ #define AWESOME_CONFIG_FILE ".awesomerc" #include +#include /** Bar possible position */ enum @@ -41,16 +42,11 @@ typedef struct int x, y, w, h; unsigned long norm[ColLast]; unsigned long sel[ColLast]; + XColor text_normal; + XColor text_selected; GC gc; Cursor cursor[CurLast]; - struct - { - int ascent; - int descent; - int height; - XFontSet set; - XFontStruct *xfont; - } font; + XftFont *font; } DC; typedef struct diff --git a/config.mk b/config.mk index 8bcfb804..54d411ee 100644 --- a/config.mk +++ b/config.mk @@ -14,8 +14,8 @@ X11INC = /usr/include/X11 X11LIB = /usr/lib/X11 # includes and libs -INCS = -I. -I/usr/include -I${X11INC} `pkg-config --cflags libconfig` -LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 `pkg-config --libs libconfig` -lXext -lXrandr -lXinerama +INCS = -I. -I/usr/include -I${X11INC} `pkg-config --cflags libconfig xft` +LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 `pkg-config --libs libconfig xft` -lXext -lXrandr -lXinerama # flags CFLAGS = -fgnu89-inline -std=gnu99 -ggdb3 -pipe -Wall -Wextra -W -Wchar-subscripts -Wundef -Wshadow -Wcast-align -Wwrite-strings -Wsign-compare -Wunused -Wuninitialized -Winit-self -Wpointer-arith -Wredundant-decls -Wno-format-zero-length -Wmissing-prototypes -Wmissing-format-attribute -Wmissing-noreturn -O3 ${INCS} -DVERSION=\"${VERSION}\" diff --git a/draw.c b/draw.c index 67142486..23b39e79 100644 --- a/draw.c +++ b/draw.c @@ -28,15 +28,18 @@ extern Client *clients, *sel, *stack; /* global client list and stack */ /* static */ void -drawtext(Display *disp, DC drawcontext, Drawable drawable, const char *text, unsigned long col[ColLast]) +drawtext(Display *disp, int screen, DC *drawcontext, Drawable drawable, const char *text, unsigned long col[ColLast], XColor textcolor) { int x, y, w, h; static char buf[256]; size_t len, olen; - XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h }; + XRectangle r = { drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h }; + XRenderColor xrcolor; + XftColor xftcolor; + XftDraw *xftdrawable; - XSetForeground(disp, drawcontext.gc, col[ColBG]); - XFillRectangles(disp, drawable, drawcontext.gc, &r, 1); + XSetForeground(disp, drawcontext->gc, col[ColBG]); + XFillRectangles(disp, drawable, drawcontext->gc, &r, 1); if(!text) return; w = 0; @@ -45,13 +48,12 @@ drawtext(Display *disp, DC drawcontext, Drawable drawable, const char *text, uns len = sizeof(buf) - 1; memcpy(buf, text, len); buf[len] = 0; - h = drawcontext.font.ascent + drawcontext.font.descent; - y = drawcontext.y + (drawcontext.h / 2) - (h / 2) + drawcontext.font.ascent; - x = drawcontext.x + (h / 2); - /* shorten text if necessary */ - while(len && (w = textnw(drawcontext.font.set, drawcontext.font.xfont, buf, len)) > drawcontext.w - h) + h = drawcontext->font->ascent + drawcontext->font->descent; + y = drawcontext->y + (drawcontext->h / 2) - (h / 2) + drawcontext->font->ascent; + x = drawcontext->x + (h / 2); + while(len && (w = textwidth(disp, drawcontext->font, buf, len)) > drawcontext->w - h) buf[--len] = 0; - if(w > drawcontext.w) + if(w > drawcontext->w) return; /* too long */ if(len < olen) { @@ -62,11 +64,14 @@ drawtext(Display *disp, DC drawcontext, Drawable drawable, const char *text, uns if(len > 3) buf[len - 3] = '.'; } - XSetForeground(disp, drawcontext.gc, col[ColFG]); - if(drawcontext.font.set) - XmbDrawString(disp, drawable, drawcontext.font.set, drawcontext.gc, x, y, buf, len); - else - XDrawString(disp, drawable, drawcontext.gc, x, y, buf, len); + xrcolor.red = textcolor.red; + xrcolor.green = textcolor.green; + xrcolor.blue = textcolor.blue; + xrcolor.alpha = 255; + XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xrcolor, &xftcolor); + xftdrawable = XftDrawCreate(disp, drawable, DefaultVisual(disp, screen), DefaultColormap(disp, screen)); + XftDrawStringUtf8(xftdrawable, &xftcolor, drawcontext->font, x, y, (FcChar8 *) buf, len); + XftColorFree(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xftcolor); } void @@ -76,9 +81,9 @@ drawsquare(Display *disp, DC drawcontext, Drawable drawable, Bool filled, unsign XGCValues gcv; XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h }; + x = (drawcontext.font->ascent + drawcontext.font->descent + 2) / 4; gcv.foreground = col; XChangeGC(disp, drawcontext.gc, GCForeground, &gcv); - x = (drawcontext.font.ascent + drawcontext.font.descent + 2) / 4; r.x = drawcontext.x + 1; r.y = drawcontext.y + 1; r.width = r.height = x; @@ -91,16 +96,11 @@ drawsquare(Display *disp, DC drawcontext, Drawable drawable, Bool filled, unsign XDrawRectangles(disp, drawable, drawcontext.gc, &r, 1); } -int -textnw(XFontSet set, XFontStruct *xfont, const char *text, int len) + +unsigned short +textwidth(Display *disp, XftFont *font, char *text, ssize_t len) { - XRectangle r; - - if(set) - { - XmbTextExtents(set, text, len, NULL, &r); - return r.width; - } - return XTextWidth(xfont, text, len); + XGlyphInfo gi; + XftTextExtentsUtf8(disp, font, (FcChar8 *) text, len, &gi); + return gi.width; } - diff --git a/draw.h b/draw.h index b36d9d7f..cc6831b9 100644 --- a/draw.h +++ b/draw.h @@ -24,10 +24,7 @@ #include "config.h" -#define textw(set, xfont, text, height) (textnw(set, xfont, text, a_strlen(text)) + height) - void drawsquare(Display *, DC, Drawable, Bool, unsigned long); -void drawtext(Display *, DC, Drawable, const char *, unsigned long *); -int textnw(XFontSet, XFontStruct *, const char *, int); - +void drawtext(Display *, int, DC *, Drawable, const char *, unsigned long *, XColor); +inline unsigned short textwidth(Display *, XftFont *, char *, ssize_t); #endif diff --git a/event.c b/event.c index 8be0fa42..04ca64cc 100644 --- a/event.c +++ b/event.c @@ -154,7 +154,7 @@ handle_event_buttonpress(XEvent * e, awesome_config *awesomeconf) int x = 0; for(i = 0; i < awesomeconf[screen].ntags; i++) { - x += textw(dc[screen].font.set, dc[screen].font.xfont, awesomeconf[screen].tags[i].name, dc[screen].font.height); + x += textwidth(e->xany.display, dc[screen].font, awesomeconf[screen].tags[i].name, a_strlen(awesomeconf[screen].tags[i].name)) + dc[screen].font->height; if(ev->x < x) { if(ev->button == Button1) diff --git a/statusbar.c b/statusbar.c index 25ce2d67..05211f10 100644 --- a/statusbar.c +++ b/statusbar.c @@ -56,38 +56,38 @@ drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf) drawcontext->x = drawcontext->y = 0; for(i = 0; i < awesomeconf->ntags; i++) { - drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->tags[i].name, drawcontext->font.height); + drawcontext->w = textwidth(disp, drawcontext->font, awesomeconf->tags[i].name, a_strlen(awesomeconf->tags[i].name)) + drawcontext->font->height; if(awesomeconf->tags[i].selected) { - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->sel); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->sel, drawcontext->text_selected); if(isoccupied(i, awesomeconf->screen)) drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->sel[ColFG]); } else { - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->norm); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->norm, drawcontext->text_normal); if(isoccupied(i, awesomeconf->screen)) drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->norm[ColFG]); } drawcontext->x += drawcontext->w; } drawcontext->w = awesomeconf->statusbar.width; - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->current_layout->symbol, drawcontext->norm); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->current_layout->symbol, drawcontext->norm, drawcontext->text_normal); x = drawcontext->x + drawcontext->w; - drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->statustext, drawcontext->font.height); + drawcontext->w = textwidth(disp, drawcontext->font, awesomeconf->statustext, a_strlen(awesomeconf->statustext)) + drawcontext->font->height; drawcontext->x = si[awesomeconf->screen].width - drawcontext->w; if(drawcontext->x < x) { drawcontext->x = x; drawcontext->w = si[awesomeconf->screen].width - x; } - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->statustext, drawcontext->norm); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->statustext, drawcontext->norm, drawcontext->text_normal); if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height) { drawcontext->x = x; if(sel) { - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->name, drawcontext->sel); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, sel->name, drawcontext->sel, drawcontext->text_selected); if(sel->isfloating) drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->ismax, drawcontext->sel[ColFG]); } @@ -95,10 +95,10 @@ drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf) { char buf[256]; snprintf(buf, sizeof(buf), "nmaster: %d ncols: %d mwfact: %.2lf", awesomeconf->nmaster, awesomeconf->ncols, awesomeconf->mwfact); - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, buf, drawcontext->norm); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, buf, drawcontext->norm, drawcontext->text_normal); } else - drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, NULL, drawcontext->norm); + drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, NULL, drawcontext->norm, drawcontext->text_normal); } XCopyArea(disp, awesomeconf->statusbar.drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, si[awesomeconf->screen].width, awesomeconf->statusbar.height, 0, 0); XSync(disp, False);