Added xft support

Based on xft tabbed patch from the wiki.

Signed-off-by: Lucas Gabriel Vuotto <l.vuotto92@gmail.com>
Signed-off-by: Christoph Lohmann <20h@r-36.net>
This commit is contained in:
Lucas Gabriel Vuotto 2016-02-13 18:42:14 -03:00 committed by Christoph Lohmann
parent 8920c1ba19
commit 3c79bcb408
3 changed files with 30 additions and 75 deletions

View file

@ -1,7 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
/* appearance */ /* appearance */
static const char font[] = "-*-*-medium-*-*-*-14-*-*-*-*-*-*-*"; static const char font[] = "monospace:size=9";
static const char* normbgcolor = "#222222"; static const char* normbgcolor = "#222222";
static const char* normfgcolor = "#cccccc"; static const char* normfgcolor = "#cccccc";
static const char* selbgcolor = "#555555"; static const char* selbgcolor = "#555555";

View file

@ -8,8 +8,8 @@ PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man MANPREFIX = ${PREFIX}/share/man
# includes and libs # includes and libs
INCS = -I. -I/usr/include INCS = -I. -I/usr/include -I/usr/include/freetype2
LIBS = -L/usr/lib -lc -lX11 LIBS = -L/usr/lib -lc -lX11 -lfontconfig -lXft
# flags # flags
CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE

View file

@ -15,6 +15,7 @@
#include <X11/Xproto.h> #include <X11/Xproto.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/XKBlib.h> #include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include "arg.h" #include "arg.h"
@ -64,17 +65,16 @@ typedef struct {
typedef struct { typedef struct {
int x, y, w, h; int x, y, w, h;
unsigned long norm[ColLast]; XftColor norm[ColLast];
unsigned long sel[ColLast]; XftColor sel[ColLast];
unsigned long urg[ColLast]; XftColor urg[ColLast];
Drawable drawable; Drawable drawable;
GC gc; GC gc;
struct { struct {
int ascent; int ascent;
int descent; int descent;
int height; int height;
XFontSet set; XftFont *xfont;
XFontStruct *xfont;
} font; } font;
} DC; /* draw context */ } DC; /* draw context */
@ -96,7 +96,7 @@ static void createnotify(const XEvent *e);
static void destroynotify(const XEvent *e); static void destroynotify(const XEvent *e);
static void die(const char *errstr, ...); static void die(const char *errstr, ...);
static void drawbar(void); static void drawbar(void);
static void drawtext(const char *text, unsigned long col[ColLast]); static void drawtext(const char *text, XftColor col[ColLast]);
static void *ecalloc(size_t n, size_t size); static void *ecalloc(size_t n, size_t size);
static void *erealloc(void *o, size_t size); static void *erealloc(void *o, size_t size);
static void expose(const XEvent *e); static void expose(const XEvent *e);
@ -107,7 +107,7 @@ static void focusurgent(const Arg *arg);
static void fullscreen(const Arg *arg); static void fullscreen(const Arg *arg);
static char *getatom(int a); static char *getatom(int a);
static int getclient(Window w); static int getclient(Window w);
static unsigned long getcolor(const char *colstr); static XftColor getcolor(const char *colstr);
static int getfirsttab(void); static int getfirsttab(void);
static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
static void initfont(const char *fontstr); static void initfont(const char *fontstr);
@ -223,11 +223,6 @@ cleanup(void)
free(clients); free(clients);
clients = NULL; clients = NULL;
if (dc.font.set)
XFreeFontSet(dpy, dc.font.set);
else
XFreeFont(dpy, dc.font.xfont);
XFreePixmap(dpy, dc.drawable); XFreePixmap(dpy, dc.drawable);
XFreeGC(dpy, dc.gc); XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win); XDestroyWindow(dpy, win);
@ -314,7 +309,7 @@ die(const char *errstr, ...)
void void
drawbar(void) drawbar(void)
{ {
unsigned long *col; XftColor *col;
int c, cc, fc, width; int c, cc, fc, width;
char *name = NULL; char *name = NULL;
@ -367,13 +362,14 @@ drawbar(void)
} }
void void
drawtext(const char *text, unsigned long col[ColLast]) drawtext(const char *text, XftColor col[ColLast])
{ {
int i, j, x, y, h, len, olen; int i, j, x, y, h, len, olen;
char buf[256]; char buf[256];
XftDraw *d;
XRectangle r = { dc.x, dc.y, dc.w, dc.h }; XRectangle r = { dc.x, dc.y, dc.w, dc.h };
XSetForeground(dpy, dc.gc, col[ColBG]); XSetForeground(dpy, dc.gc, col[ColBG].pixel);
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
if (!text) if (!text)
return; return;
@ -397,12 +393,9 @@ drawtext(const char *text, unsigned long col[ColLast])
; ;
} }
XSetForeground(dpy, dc.gc, col[ColFG]); d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen));
if (dc.font.set) XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len);
XmbDrawString(dpy, dc.drawable, dc.font.set, XftDrawDestroy(d);
dc.gc, x, y, buf, len);
else
XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
} }
void * void *
@ -560,16 +553,15 @@ getclient(Window w)
return -1; return -1;
} }
unsigned long XftColor
getcolor(const char *colstr) getcolor(const char *colstr)
{ {
Colormap cmap = DefaultColormap(dpy, screen); XftColor color;
XColor color;
if (!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) if (!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color))
die("%s: cannot allocate color '%s'\n", argv0, colstr); die("%s: cannot allocate color '%s'\n", argv0, colstr);
return color.pixel; return color;
} }
int int
@ -621,42 +613,12 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size)
void void
initfont(const char *fontstr) initfont(const char *fontstr)
{ {
char *def, **missing, **font_names; if (!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr))
XFontStruct **xfonts; && !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed")))
int i, n; die("error, cannot load font: '%s'\n", fontstr);
missing = NULL; dc.font.ascent = dc.font.xfont->ascent;
if (dc.font.set) dc.font.descent = dc.font.xfont->descent;
XFreeFontSet(dpy, dc.font.set);
dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
if (missing) {
while (n--)
fprintf(stderr, "%s: missing fontset: %s\n",
argv0, missing[n]);
XFreeStringList(missing);
}
if (dc.font.set) {
dc.font.ascent = dc.font.descent = 0;
n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
for (i = 0, dc.font.ascent = 0, dc.font.descent = 0;
i < n; i++) {
dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
xfonts++;
}
} else {
if (dc.font.xfont)
XFreeFont(dpy, dc.font.xfont);
dc.font.xfont = NULL;
if (!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)) &&
!(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
die("%s: cannot load font: '%s'\n", argv0, fontstr);
dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent;
}
dc.font.height = dc.font.ascent + dc.font.descent; dc.font.height = dc.font.ascent + dc.font.descent;
} }
@ -1057,11 +1019,9 @@ setup(void)
dc.drawable = XCreatePixmap(dpy, root, ww, wh, dc.drawable = XCreatePixmap(dpy, root, ww, wh,
DefaultDepth(dpy, screen)); DefaultDepth(dpy, screen));
dc.gc = XCreateGC(dpy, root, 0, 0); dc.gc = XCreateGC(dpy, root, 0, 0);
if (!dc.font.set)
XSetFont(dpy, dc.gc, dc.font.xfont->fid);
win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0, win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0,
dc.norm[ColFG], dc.norm[ColBG]); dc.norm[ColFG].pixel, dc.norm[ColBG].pixel);
XMapRaised(dpy, win); XMapRaised(dpy, win);
XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask | XSelectInput(dpy, win, SubstructureNotifyMask | FocusChangeMask |
ButtonPressMask | ExposureMask | KeyPressMask | ButtonPressMask | ExposureMask | KeyPressMask |
@ -1131,14 +1091,9 @@ spawn(const Arg *arg)
int int
textnw(const char *text, unsigned int len) textnw(const char *text, unsigned int len)
{ {
XRectangle r; XGlyphInfo ext;
XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
if (dc.font.set) { return ext.xOff;
XmbTextExtents(dc.font.set, text, len, NULL, &r);
return r.width;
}
return XTextWidth(dc.font.xfont, text, len);
} }
void void