From 4ef73665d3cc270683973cde09857611e89b931b Mon Sep 17 00:00:00 2001 From: oga Date: Sun, 15 Jun 2008 02:47:46 +0000 Subject: [PATCH] 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@. --- app/cwm/calmwm.c | 10 ++--- app/cwm/calmwm.h | 39 ++++++------------- app/cwm/conf.c | 13 ++++++- app/cwm/font.c | 97 +++++------------------------------------------- app/cwm/grab.c | 17 ++++----- app/cwm/hash.h | 68 --------------------------------- app/cwm/menu.c | 36 ++++++++---------- app/cwm/parse.y | 8 +--- 8 files changed, 61 insertions(+), 227 deletions(-) delete mode 100644 app/cwm/hash.h diff --git a/app/cwm/calmwm.c b/app/cwm/calmwm.c index 3a0f18ca5..08d3e362a 100644 --- a/app/cwm/calmwm.c +++ b/app/cwm/calmwm.c @@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $Id: calmwm.c,v 1.20 2008/05/21 14:11:19 oga Exp $ + * $Id: calmwm.c,v 1.21 2008/06/15 02:47:46 oga Exp $ */ #include "headers.h" @@ -38,7 +38,6 @@ struct client_ctx_q Clientq; int Doshape, Shape_ev; int Starting; struct conf Conf; -struct fontdesc *DefaultFont = NULL; /* From TWM */ #define gray_width 2 @@ -149,6 +148,8 @@ x_setupscreen(struct screen_ctx *sc, u_int which) XSetWindowAttributes rootattr; struct keybinding *kb; + Curscreen = sc; + sc->display = x_screenname(which); sc->which = which; sc->rootwin = RootWindow(X_Dpy, which); @@ -203,9 +204,7 @@ x_setupscreen(struct screen_ctx *sc, u_int which) GCLineWidth|GCSubwindowMode, &gv); font_init(sc); - DefaultFont = font_getx(sc, Conf.DefaultFontName); - sc->fontheight = font_ascent(DefaultFont) + - font_descent(DefaultFont) + 1; + conf_font(&Conf); /* * XXX - this should *really* be in screen_init(). ordering @@ -231,7 +230,6 @@ x_setupscreen(struct screen_ctx *sc, u_int which) } XFree(wins); - Curscreen = sc; /* XXX */ screen_init(); screen_updatestackingorder(); diff --git a/app/cwm/calmwm.h b/app/cwm/calmwm.h index 41f4c94b6..bb38b04b2 100644 --- a/app/cwm/calmwm.h +++ b/app/cwm/calmwm.h @@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $Id: calmwm.h,v 1.53 2008/06/14 22:04:11 okan Exp $ + * $Id: calmwm.h,v 1.54 2008/06/15 02:47:46 oga Exp $ */ #ifndef _CALMWM_H_ @@ -23,8 +23,6 @@ #define CALMWM_MAXNAMELEN 256 -#include "hash.h" - #undef MIN #undef MAX #define MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -40,20 +38,6 @@ struct 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 { TAILQ_ENTRY(screen_ctx) entry; @@ -76,8 +60,6 @@ struct screen_ctx { struct cycle_entry_q mruq; - struct fonthash fonthash; - u_int fontheight; XftDraw *xftdraw; XftColor xftcolor; }; @@ -281,6 +263,8 @@ struct conf { #define DEFAULTFONTNAME "sans-serif:pixelsize=14:bold" char *DefaultFontName; + XftFont *DefaultFont; + u_int FontHeight; 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 *); int conf_changed(char *); void conf_reload(struct conf *); +void conf_font(struct conf *); void kbfunc_client_lower(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 font_init(struct screen_ctx *); -struct fontdesc *font_get(struct screen_ctx *, const char *); -int font_width(struct fontdesc *, const char *, int); -void font_draw(struct fontdesc *, const char *, int, +int font_width(const char *, int); +void font_draw(struct screen_ctx *, const char *, int, Drawable, int, int); -int font_ascent(struct fontdesc *); -int font_descent(struct fontdesc *); -struct fontdesc *font_getx(struct screen_ctx *, const char *); +XftFont *font_make(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) @@ -524,7 +510,4 @@ extern struct client_ctx_q Clientq; extern int Doshape, Shape_ev; extern struct conf Conf; -extern struct fontdesc *DefaultFont; - - #endif /* _CALMWM_H_ */ diff --git a/app/cwm/conf.c b/app/cwm/conf.c index 90e8e0c2d..69b422686 100644 --- a/app/cwm/conf.c +++ b/app/cwm/conf.c @@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $Id: conf.c,v 1.39 2008/06/14 21:59:09 okan Exp $ + * $Id: conf.c,v 1.40 2008/06/15 02:47:46 oga Exp $ */ #include "headers.h" @@ -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 conf_changed(char *path) { @@ -78,7 +87,7 @@ conf_reload(struct conf *c) return; } - DefaultFont = font_getx(Curscreen, c->DefaultFontName); + conf_font(c); } void diff --git a/app/cwm/font.c b/app/cwm/font.c index 38670e59a..85f54e597 100644 --- a/app/cwm/font.c +++ b/app/cwm/font.c @@ -16,51 +16,14 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "hash.h" #include "headers.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 font_init(struct screen_ctx *sc) { XColor xcolor, tmp; - HASH_INIT(&sc->fonthash, fontdesc_hash); sc->xftdraw = XftDrawCreate(X_Dpy, sc->rootwin, DefaultVisual(X_Dpy, sc->which), DefaultColormap(X_Dpy, sc->which)); if (sc->xftdraw == NULL) @@ -77,76 +40,34 @@ font_init(struct screen_ctx *sc) 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 -font_width(struct fontdesc *fdp, const char *text, int len) +font_width(const char *text, int len) { 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); } 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) { - XftDrawChange(fdp->sc->xftdraw, d); + XftDrawChange(sc->xftdraw, 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); } -int -font_ascent(struct fontdesc *fdp) -{ - 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 * +font_make(struct screen_ctx *sc, const char *name) { XftFont *fn = NULL; FcPattern *pat, *patx; XftResult res; - if ((pat = FcNameParse(fdp->name)) == NULL) + if ((pat = FcNameParse(name)) == NULL) return (NULL); if ((patx = XftFontMatch(X_Dpy, sc->which, pat, &res)) != NULL) diff --git a/app/cwm/grab.c b/app/cwm/grab.c index 46f9133d6..1c5ef2d70 100644 --- a/app/cwm/grab.c +++ b/app/cwm/grab.c @@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $Id: grab.c,v 1.18 2008/06/14 22:04:11 okan Exp $ + * $Id: grab.c,v 1.19 2008/06/15 02:47:46 oga Exp $ */ #include "headers.h" @@ -33,23 +33,22 @@ grab_sweep_draw(struct client_ctx *cc, int dx, int dy) int x0 = cc->geom.x, y0 = cc->geom.y; char asize[10]; /* fits "nnnnxnnnn\0" */ int wide, height, wide_size, wide_name; - struct fontdesc *font = DefaultFont; snprintf(asize, sizeof(asize), "%dx%d", ADJUST_WIDTH(cc, dx), ADJUST_HEIGHT(cc, dy)); - wide_size = font_width(font, asize, strlen(asize)) + 4; - wide_name = font_width(font, cc->name, strlen(cc->name)) + 4; + wide_size = font_width(asize, strlen(asize)) + 4; + wide_name = font_width(cc->name, strlen(cc->name)) + 4; 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); XMapWindow(X_Dpy, sc->menuwin); XReparentWindow(X_Dpy, sc->menuwin, cc->win, 0, 0); XClearWindow(X_Dpy, sc->menuwin); - font_draw(font, cc->name, strlen(cc->name), sc->menuwin, - 2, font_ascent(font) + 1); - font_draw(font, asize, strlen(asize), sc->menuwin, - wide/2 - wide_size/2, height + font_ascent(font) + 1); + font_draw(sc, cc->name, strlen(cc->name), sc->menuwin, + 2, font_ascent() + 1); + font_draw(sc, asize, strlen(asize), sc->menuwin, + wide/2 - wide_size/2, height + font_ascent() + 1); } void diff --git a/app/cwm/hash.h b/app/cwm/hash.h deleted file mode 100644 index 6dcac2cff..000000000 --- a/app/cwm/hash.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * hash.h - generic hash template, akin to queue.h & tree.h - * - * Copyright (c) 2005 Marius Eriksen - * - * 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 - -#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_ */ diff --git a/app/cwm/menu.c b/app/cwm/menu.c index df870fdc7..6db9a535f 100644 --- a/app/cwm/menu.c +++ b/app/cwm/menu.c @@ -74,7 +74,6 @@ menu_filter(struct menu_q *menuq, char *prompt, char *initial, int dummy, XEvent e; Window focuswin; int Mask, focusrevert; - struct fontdesc *font = DefaultFont; TAILQ_INIT(&resultq); @@ -92,7 +91,7 @@ menu_filter(struct menu_q *menuq, char *prompt, char *initial, int dummy, PROMPT_SCHAR); snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%c", 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)); mc.hasprompt = 1; } @@ -106,7 +105,7 @@ menu_filter(struct menu_q *menuq, char *prompt, char *initial, int dummy, mc.entry = mc.prev = -1; XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width, - sc->fontheight); + font_height()); XSelectInput(X_Dpy, sc->menuwin, Mask); 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 xsave, ysave; int warp; - struct fontdesc *font = DefaultFont; if (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) { snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%c", mc->promptstr, mc->searchstr, PROMPT_ECHAR); - mc->width = font_width(font, mc->dispstr, strlen(mc->dispstr)); - dy = sc->fontheight; + mc->width = font_width(mc->dispstr, strlen(mc->dispstr)); + dy = font_height(); mc->num = 1; } @@ -296,9 +294,9 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq, 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))); - dy += sc->fontheight; + dy += font_height(); 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); if (mc->hasprompt) { - font_draw(font, mc->dispstr, strlen(mc->dispstr), sc->menuwin, - 0, font_ascent(font) + 1); + font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin, + 0, font_ascent() + 1); n = 1; } else 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' ? mi->print : mi->text; - font_draw(font, text, - MIN(strlen(text), MENU_MAXENTRY), - sc->menuwin, - 0, n*sc->fontheight + font_ascent(font) + 1); + font_draw(sc, text, MIN(strlen(text), MENU_MAXENTRY), + sc->menuwin, 0, n*font_height() + font_ascent() + 1); n++; } if (mc->hasprompt && n > 1) XFillRectangle(X_Dpy, sc->menuwin, sc->gc, - 0, sc->fontheight, mc->width, sc->fontheight); + 0, font_height(), mc->width, font_height()); if (mc->noresult) XFillRectangle(X_Dpy, sc->menuwin, sc->gc, - 0, 0, mc->width, sc->fontheight); + 0, 0, mc->width, font_height()); } void @@ -356,11 +352,11 @@ menu_handle_move(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc) if (mc->prev != -1) 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) { xu_ptr_regrab(MenuGrabMask, Cursor_select); 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 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 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? */ - 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 = -1; diff --git a/app/cwm/parse.y b/app/cwm/parse.y index e344e1100..c67ddbe79 100644 --- a/app/cwm/parse.y +++ b/app/cwm/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.11 2008/06/14 21:51:00 okan Exp $ */ +/* $OpenBSD: parse.y,v 1.12 2008/06/15 02:47:46 oga Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -103,11 +103,7 @@ main : FONTNAME STRING { if (conf->DefaultFontName != NULL && conf->DefaultFontName != DEFAULTFONTNAME) free(conf->DefaultFontName); - if ((conf->DefaultFontName = xstrdup($2)) == NULL) { - free($2); - yyerror("string: asprintf"); - YYERROR; - } + conf->DefaultFontName = xstrdup($2); free($2); } | STICKY yesno {