Rip out and burn the HASH_* stuff. We don't need a SPLAY tree for one font.

makes the code a lot simpler. While here rearrange the font handling functions
to be less shit.

ok and help okan@.
This commit is contained in:
oga 2008-06-15 02:47:46 +00:00
parent 96d7310b4a
commit 07cd0b1ac5
8 changed files with 56 additions and 222 deletions

View File

@ -38,7 +38,6 @@ struct client_ctx_q Clientq;
int Doshape, Shape_ev; int Doshape, Shape_ev;
int Starting; int Starting;
struct conf Conf; struct conf Conf;
struct fontdesc *DefaultFont = NULL;
/* From TWM */ /* From TWM */
#define gray_width 2 #define gray_width 2
@ -149,6 +148,8 @@ x_setupscreen(struct screen_ctx *sc, u_int which)
XSetWindowAttributes rootattr; XSetWindowAttributes rootattr;
struct keybinding *kb; struct keybinding *kb;
Curscreen = sc;
sc->display = x_screenname(which); sc->display = x_screenname(which);
sc->which = which; sc->which = which;
sc->rootwin = RootWindow(X_Dpy, which); sc->rootwin = RootWindow(X_Dpy, which);
@ -203,9 +204,7 @@ x_setupscreen(struct screen_ctx *sc, u_int which)
GCLineWidth|GCSubwindowMode, &gv); GCLineWidth|GCSubwindowMode, &gv);
font_init(sc); font_init(sc);
DefaultFont = font_getx(sc, Conf.DefaultFontName); conf_font(&Conf);
sc->fontheight = font_ascent(DefaultFont) +
font_descent(DefaultFont) + 1;
/* /*
* XXX - this should *really* be in screen_init(). ordering * XXX - this should *really* be in screen_init(). ordering
@ -231,7 +230,6 @@ x_setupscreen(struct screen_ctx *sc, u_int which)
} }
XFree(wins); XFree(wins);
Curscreen = sc; /* XXX */
screen_init(); screen_init();
screen_updatestackingorder(); screen_updatestackingorder();

View File

@ -23,8 +23,6 @@
#define CALMWM_MAXNAMELEN 256 #define CALMWM_MAXNAMELEN 256
#include "hash.h"
#undef MIN #undef MIN
#undef MAX #undef MAX
#define MIN(x, y) ((x) < (y) ? (x) : (y)) #define MIN(x, y) ((x) < (y) ? (x) : (y))
@ -40,20 +38,6 @@ struct client_ctx;
TAILQ_HEAD(cycle_entry_q, client_ctx); TAILQ_HEAD(cycle_entry_q, client_ctx);
struct screen_ctx;
struct fontdesc {
const char *name;
XftFont *fn;
struct screen_ctx *sc;
HASH_ENTRY(fontdesc) node;
};
int fontdesc_cmp(struct fontdesc *a, struct fontdesc *b);
HASH_HEAD(fonthash, fontdesc, 16);
HASH_PROTOTYPE(fonthash, fontdesc, node, fontdesc_cmp);
struct screen_ctx { struct screen_ctx {
TAILQ_ENTRY(screen_ctx) entry; TAILQ_ENTRY(screen_ctx) entry;
@ -76,8 +60,6 @@ struct screen_ctx {
struct cycle_entry_q mruq; struct cycle_entry_q mruq;
struct fonthash fonthash;
u_int fontheight;
XftDraw *xftdraw; XftDraw *xftdraw;
XftColor xftcolor; XftColor xftcolor;
}; };
@ -281,6 +263,8 @@ struct conf {
#define DEFAULTFONTNAME "sans-serif:pixelsize=14:bold" #define DEFAULTFONTNAME "sans-serif:pixelsize=14:bold"
char *DefaultFontName; char *DefaultFontName;
XftFont *DefaultFont;
u_int FontHeight;
int gap_top, gap_bottom, gap_left, gap_right; int gap_top, gap_bottom, gap_left, gap_right;
}; };
@ -442,6 +426,7 @@ void conf_mousebind(struct conf *, char *, char *);
void conf_mouseunbind(struct conf *, struct mousebinding *); void conf_mouseunbind(struct conf *, struct mousebinding *);
int conf_changed(char *); int conf_changed(char *);
void conf_reload(struct conf *); void conf_reload(struct conf *);
void conf_font(struct conf *);
void kbfunc_client_lower(struct client_ctx *, void *); void kbfunc_client_lower(struct client_ctx *, void *);
void kbfunc_client_raise(struct client_ctx *, void *); void kbfunc_client_raise(struct client_ctx *, void *);
@ -495,13 +480,14 @@ void group_sticky_toggle_exit(struct client_ctx *);
void group_autogroup(struct client_ctx *); void group_autogroup(struct client_ctx *);
void font_init(struct screen_ctx *); void font_init(struct screen_ctx *);
struct fontdesc *font_get(struct screen_ctx *, const char *); int font_width(const char *, int);
int font_width(struct fontdesc *, const char *, int); void font_draw(struct screen_ctx *, const char *, int,
void font_draw(struct fontdesc *, const char *, int,
Drawable, int, int); Drawable, int, int);
int font_ascent(struct fontdesc *); XftFont *font_make(struct screen_ctx *, const char *);
int font_descent(struct fontdesc *);
struct fontdesc *font_getx(struct screen_ctx *, const char *); #define font_ascent() Conf.DefaultFont->ascent
#define font_descent() Conf.DefaultFont->descent
#define font_height() Conf.FontHeight
#define CCTOSC(cc) (cc->sc) #define CCTOSC(cc) (cc->sc)
@ -524,7 +510,4 @@ extern struct client_ctx_q Clientq;
extern int Doshape, Shape_ev; extern int Doshape, Shape_ev;
extern struct conf Conf; extern struct conf Conf;
extern struct fontdesc *DefaultFont;
#endif /* _CALMWM_H_ */ #endif /* _CALMWM_H_ */

11
conf.c
View File

@ -50,6 +50,15 @@ conf_cmd_add(struct conf *c, char *image, char *label, int flags)
} }
} }
void
conf_font(struct conf *c)
{
struct screen_ctx *sc = screen_current();
c->DefaultFont = font_make(sc, Conf.DefaultFontName);
c->FontHeight = font_ascent() + font_descent() + 1;
}
int int
conf_changed(char *path) conf_changed(char *path)
{ {
@ -78,7 +87,7 @@ conf_reload(struct conf *c)
return; return;
} }
DefaultFont = font_getx(Curscreen, c->DefaultFontName); conf_font(c);
} }
void void

97
font.c
View File

@ -16,51 +16,14 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "hash.h"
#include "headers.h" #include "headers.h"
#include "calmwm.h" #include "calmwm.h"
static XftFont *_make_font(struct screen_ctx *sc, struct fontdesc *fdp);
HASH_GENERATE(fonthash, fontdesc, node, fontdesc_cmp);
int
fontdesc_cmp(struct fontdesc *a, struct fontdesc *b)
{
return (strcmp(a->name, b->name));
}
/*
* Fowler/Noll/Vo hash
* http://www.isthe.com/chongo/tech/comp/fnv/
*/
#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
unsigned int
fontdesc_hash(struct fontdesc *fdp)
{
const unsigned char *p, *end, *start;
unsigned int hash = FNV_1_32;
start = fdp->name;
end = (const unsigned char *)fdp->name + strlen(fdp->name);
for (p = start; p < end; p++) {
hash *= FNV_P_32;
hash ^= (unsigned int)*p;
}
return (hash);
}
void void
font_init(struct screen_ctx *sc) font_init(struct screen_ctx *sc)
{ {
XColor xcolor, tmp; XColor xcolor, tmp;
HASH_INIT(&sc->fonthash, fontdesc_hash);
sc->xftdraw = XftDrawCreate(X_Dpy, sc->rootwin, sc->xftdraw = XftDrawCreate(X_Dpy, sc->rootwin,
DefaultVisual(X_Dpy, sc->which), DefaultColormap(X_Dpy, sc->which)); DefaultVisual(X_Dpy, sc->which), DefaultColormap(X_Dpy, sc->which));
if (sc->xftdraw == NULL) if (sc->xftdraw == NULL)
@ -77,76 +40,34 @@ font_init(struct screen_ctx *sc)
sc->xftcolor.pixel = xcolor.pixel; sc->xftcolor.pixel = xcolor.pixel;
} }
struct fontdesc *
font_getx(struct screen_ctx *sc, const char *name)
{
struct fontdesc *fdp;
if ((fdp = font_get(sc, name)) == NULL)
errx(1, "font_get()");
return (fdp);
}
struct fontdesc *
font_get(struct screen_ctx *sc, const char *name)
{
struct fontdesc fd, *fdp;
XftFont *fn;
fd.name = name;
if ((fdp = HASH_FIND(fonthash, &sc->fonthash, &fd)) == NULL
&& (fn = _make_font(sc, &fd)) != NULL) {
fdp = xmalloc(sizeof(*fdp));
fdp->name = xstrdup(fd.name);
fdp->fn = fn;
fdp->sc = sc;
HASH_INSERT(fonthash, &sc->fonthash, fdp);
}
return (fdp);
}
int int
font_width(struct fontdesc *fdp, const char *text, int len) font_width(const char *text, int len)
{ {
XGlyphInfo extents; XGlyphInfo extents;
XftTextExtents8(X_Dpy, fdp->fn, (const XftChar8*)text, len, &extents); XftTextExtents8(X_Dpy, Conf.DefaultFont, (const XftChar8*)text,
len, &extents);
return (extents.xOff); return (extents.xOff);
} }
void void
font_draw(struct fontdesc *fdp, const char *text, int len, font_draw(struct screen_ctx *sc, const char *text, int len,
Drawable d, int x, int y) Drawable d, int x, int y)
{ {
XftDrawChange(fdp->sc->xftdraw, d); XftDrawChange(sc->xftdraw, d);
/* Really needs to be UTF8'd. */ /* Really needs to be UTF8'd. */
XftDrawString8(fdp->sc->xftdraw, &fdp->sc->xftcolor, fdp->fn, x, y, XftDrawString8(sc->xftdraw, &sc->xftcolor, Conf.DefaultFont, x, y,
(const FcChar8*)text, len); (const FcChar8*)text, len);
} }
int XftFont *
font_ascent(struct fontdesc *fdp) font_make(struct screen_ctx *sc, const char *name)
{
return (fdp->fn->ascent);
}
int
font_descent(struct fontdesc *fdp)
{
return (fdp->fn->descent);
}
static XftFont *
_make_font(struct screen_ctx *sc, struct fontdesc *fdp)
{ {
XftFont *fn = NULL; XftFont *fn = NULL;
FcPattern *pat, *patx; FcPattern *pat, *patx;
XftResult res; XftResult res;
if ((pat = FcNameParse(fdp->name)) == NULL) if ((pat = FcNameParse(name)) == NULL)
return (NULL); return (NULL);
if ((patx = XftFontMatch(X_Dpy, sc->which, pat, &res)) != NULL) if ((patx = XftFontMatch(X_Dpy, sc->which, pat, &res)) != NULL)

15
grab.c
View File

@ -33,23 +33,22 @@ grab_sweep_draw(struct client_ctx *cc, int dx, int dy)
int x0 = cc->geom.x, y0 = cc->geom.y; int x0 = cc->geom.x, y0 = cc->geom.y;
char asize[10]; /* fits "nnnnxnnnn\0" */ char asize[10]; /* fits "nnnnxnnnn\0" */
int wide, height, wide_size, wide_name; int wide, height, wide_size, wide_name;
struct fontdesc *font = DefaultFont;
snprintf(asize, sizeof(asize), "%dx%d", snprintf(asize, sizeof(asize), "%dx%d",
ADJUST_WIDTH(cc, dx), ADJUST_HEIGHT(cc, dy)); ADJUST_WIDTH(cc, dx), ADJUST_HEIGHT(cc, dy));
wide_size = font_width(font, asize, strlen(asize)) + 4; wide_size = font_width(asize, strlen(asize)) + 4;
wide_name = font_width(font, cc->name, strlen(cc->name)) + 4; wide_name = font_width(cc->name, strlen(cc->name)) + 4;
wide = MAX(wide_size, wide_name); wide = MAX(wide_size, wide_name);
height = font_ascent(font) + font_descent(font) + 1; height = font_ascent() + font_descent() + 1;
XMoveResizeWindow(X_Dpy, sc->menuwin, x0, y0, wide, height * 2); XMoveResizeWindow(X_Dpy, sc->menuwin, x0, y0, wide, height * 2);
XMapWindow(X_Dpy, sc->menuwin); XMapWindow(X_Dpy, sc->menuwin);
XReparentWindow(X_Dpy, sc->menuwin, cc->win, 0, 0); XReparentWindow(X_Dpy, sc->menuwin, cc->win, 0, 0);
XClearWindow(X_Dpy, sc->menuwin); XClearWindow(X_Dpy, sc->menuwin);
font_draw(font, cc->name, strlen(cc->name), sc->menuwin, font_draw(sc, cc->name, strlen(cc->name), sc->menuwin,
2, font_ascent(font) + 1); 2, font_ascent() + 1);
font_draw(font, asize, strlen(asize), sc->menuwin, font_draw(sc, asize, strlen(asize), sc->menuwin,
wide/2 - wide_size/2, height + font_ascent(font) + 1); wide/2 - wide_size/2, height + font_ascent() + 1);
} }
void void

68
hash.h
View File

@ -1,68 +0,0 @@
/*
* hash.h - generic hash template, akin to queue.h & tree.h
*
* Copyright (c) 2005 Marius Eriksen <marius@monkey.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _HASH_H_ /* Possibly this is too generic. */
#define _HASH_H_
#include <sys/tree.h>
#define HASH_ENTRY SPLAY_ENTRY
#define HASH_HEAD(name, type, nbuckets) \
SPLAY_HEAD(name##_HASH_TREE, type); \
struct name { \
struct name##_HASH_TREE buckets[nbuckets]; \
unsigned int (*hashfn)(struct type *elm); \
};
#define HASH_NBUCKETS(head) \
(sizeof((head)->buckets)/sizeof((head)->buckets[0]))
#define HASH_INIT(head, fn) do { \
int i; \
for (i = 0; i < HASH_NBUCKETS(head); i++) { \
SPLAY_INIT(&(head)->buckets[i]); \
} \
(head)->hashfn = fn; \
} while (0)
#define HASH_PROTOTYPE(name, type, field, cmp) \
SPLAY_PROTOTYPE(name##_HASH_TREE, type, field, cmp) \
struct type *name##_HASH_TREE_FIND(struct name *head, struct type *find); \
void name##_HASH_TREE_INSERT(struct name *head, struct type *insert);
#define HASH_GENERATE(name, type, field, cmp) \
SPLAY_GENERATE(name##_HASH_TREE, type, field, cmp) \
struct type *name##_HASH_TREE_FIND(struct name *head, struct type *find) \
{ \
struct name##_HASH_TREE *bucket = \
&head->buckets[(*head->hashfn)(find) % HASH_NBUCKETS(head)]; \
return (SPLAY_FIND(name##_HASH_TREE, bucket, find)); \
} \
void name##_HASH_TREE_INSERT(struct name *head, struct type *insert) \
{ \
struct name##_HASH_TREE *bucket = \
&head->buckets[(*head->hashfn)(insert) % HASH_NBUCKETS(head)]; \
\
SPLAY_INSERT(name##_HASH_TREE, bucket, insert); \
}
#define HASH_FIND(name, head, find) name##_HASH_TREE_FIND((head), (find))
#define HASH_INSERT(name, head, insert) name##_HASH_TREE_INSERT((head), (insert))
#endif /* _HASH_H_ */

36
menu.c
View File

@ -74,7 +74,6 @@ menu_filter(struct menu_q *menuq, char *prompt, char *initial, int dummy,
XEvent e; XEvent e;
Window focuswin; Window focuswin;
int Mask, focusrevert; int Mask, focusrevert;
struct fontdesc *font = DefaultFont;
TAILQ_INIT(&resultq); TAILQ_INIT(&resultq);
@ -92,7 +91,7 @@ menu_filter(struct menu_q *menuq, char *prompt, char *initial, int dummy,
PROMPT_SCHAR); PROMPT_SCHAR);
snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%c", mc.promptstr, snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%c", mc.promptstr,
mc.searchstr, PROMPT_ECHAR); mc.searchstr, PROMPT_ECHAR);
mc.width = font_width(font, mc.dispstr, strlen(mc.dispstr)); mc.width = font_width(mc.dispstr, strlen(mc.dispstr));
mc.hasprompt = 1; mc.hasprompt = 1;
} }
@ -106,7 +105,7 @@ menu_filter(struct menu_q *menuq, char *prompt, char *initial, int dummy,
mc.entry = mc.prev = -1; mc.entry = mc.prev = -1;
XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width, XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width,
sc->fontheight); font_height());
XSelectInput(X_Dpy, sc->menuwin, Mask); XSelectInput(X_Dpy, sc->menuwin, Mask);
XMapRaised(X_Dpy, sc->menuwin); XMapRaised(X_Dpy, sc->menuwin);
@ -260,7 +259,6 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
int dy; int dy;
int xsave, ysave; int xsave, ysave;
int warp; int warp;
struct fontdesc *font = DefaultFont;
if (mc->list) { if (mc->list) {
if (TAILQ_EMPTY(resultq) && mc->list) { if (TAILQ_EMPTY(resultq) && mc->list) {
@ -280,8 +278,8 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
if (mc->hasprompt) { if (mc->hasprompt) {
snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%c", snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%c",
mc->promptstr, mc->searchstr, PROMPT_ECHAR); mc->promptstr, mc->searchstr, PROMPT_ECHAR);
mc->width = font_width(font, mc->dispstr, strlen(mc->dispstr)); mc->width = font_width(mc->dispstr, strlen(mc->dispstr));
dy = sc->fontheight; dy = font_height();
mc->num = 1; mc->num = 1;
} }
@ -296,9 +294,9 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
text = mi->text; text = mi->text;
} }
mc->width = MAX(mc->width, font_width(font, text, mc->width = MAX(mc->width, font_width(text,
MIN(strlen(text), MENU_MAXENTRY))); MIN(strlen(text), MENU_MAXENTRY)));
dy += sc->fontheight; dy += font_height();
mc->num++; mc->num++;
} }
@ -322,8 +320,8 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
XMoveResizeWindow(X_Dpy, sc->menuwin, mc->x, mc->y, mc->width, dy); XMoveResizeWindow(X_Dpy, sc->menuwin, mc->x, mc->y, mc->width, dy);
if (mc->hasprompt) { if (mc->hasprompt) {
font_draw(font, mc->dispstr, strlen(mc->dispstr), sc->menuwin, font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin,
0, font_ascent(font) + 1); 0, font_ascent() + 1);
n = 1; n = 1;
} else } else
n = 0; n = 0;
@ -332,20 +330,18 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
char *text = mi->print[0] != '\0' ? char *text = mi->print[0] != '\0' ?
mi->print : mi->text; mi->print : mi->text;
font_draw(font, text, font_draw(sc, text, MIN(strlen(text), MENU_MAXENTRY),
MIN(strlen(text), MENU_MAXENTRY), sc->menuwin, 0, n*font_height() + font_ascent() + 1);
sc->menuwin,
0, n*sc->fontheight + font_ascent(font) + 1);
n++; n++;
} }
if (mc->hasprompt && n > 1) if (mc->hasprompt && n > 1)
XFillRectangle(X_Dpy, sc->menuwin, sc->gc, XFillRectangle(X_Dpy, sc->menuwin, sc->gc,
0, sc->fontheight, mc->width, sc->fontheight); 0, font_height(), mc->width, font_height());
if (mc->noresult) if (mc->noresult)
XFillRectangle(X_Dpy, sc->menuwin, sc->gc, XFillRectangle(X_Dpy, sc->menuwin, sc->gc,
0, 0, mc->width, sc->fontheight); 0, 0, mc->width, font_height());
} }
void void
@ -356,11 +352,11 @@ menu_handle_move(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc)
if (mc->prev != -1) if (mc->prev != -1)
XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0, XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0,
sc->fontheight * mc->prev, mc->width, sc->fontheight); font_height() * mc->prev, mc->width, font_height());
if (mc->entry != -1) { if (mc->entry != -1) {
xu_ptr_regrab(MenuGrabMask, Cursor_select); xu_ptr_regrab(MenuGrabMask, Cursor_select);
XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0, XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0,
sc->fontheight * mc->entry, mc->width, sc->fontheight); font_height() * mc->entry, mc->width, font_height());
} else } else
xu_ptr_regrab(MenuGrabMask, Cursor_default); xu_ptr_regrab(MenuGrabMask, Cursor_default);
} }
@ -392,10 +388,10 @@ menu_handle_release(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc,
static int static int
menu_calc_entry(struct screen_ctx *sc, struct menu_ctx *mc, int x, int y) menu_calc_entry(struct screen_ctx *sc, struct menu_ctx *mc, int x, int y)
{ {
int entry = y / sc->fontheight; int entry = y / font_height();
/* in bounds? */ /* in bounds? */
if (x < 0 || x > mc->width || y < 0 || y > sc->fontheight*mc->num || if (x < 0 || x > mc->width || y < 0 || y > font_height()*mc->num ||
entry < 0 || entry >= mc->num) entry < 0 || entry >= mc->num)
entry = -1; entry = -1;

View File

@ -103,11 +103,7 @@ main : FONTNAME STRING {
if (conf->DefaultFontName != NULL && if (conf->DefaultFontName != NULL &&
conf->DefaultFontName != DEFAULTFONTNAME) conf->DefaultFontName != DEFAULTFONTNAME)
free(conf->DefaultFontName); free(conf->DefaultFontName);
if ((conf->DefaultFontName = xstrdup($2)) == NULL) { conf->DefaultFontName = xstrdup($2);
free($2);
yyerror("string: asprintf");
YYERROR;
}
free($2); free($2);
} }
| STICKY yesno { | STICKY yesno {