Xft support

This commit is contained in:
Julien Danjou 2007-10-01 19:22:57 +02:00
parent 7204cbdc46
commit 272f4b1c2a
9 changed files with 76 additions and 129 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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;
}

View File

@ -25,6 +25,7 @@
#define AWESOME_CONFIG_FILE ".awesomerc"
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
/** 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

View File

@ -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}\"

54
draw.c
View File

@ -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;
}

7
draw.h
View File

@ -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

View File

@ -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)

View File

@ -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);