dwm-meillo

changeset 3:e969f3575b7a

several new changes, made gridmenu working
author Anselm R. Garbe <garbeam@wmii.de>
date Mon, 10 Jul 2006 19:46:24 +0200
parents a79188fe4a40
children 991bd8b0771e
files Makefile config.h draw.c draw.h gridmenu.c util.c util.h wm.c wm.h
diffstat 9 files changed, 256 insertions(+), 206 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Mon Jul 10 18:35:39 2006 +0200
     1.2 +++ b/Makefile	Mon Jul 10 19:46:24 2006 +0200
     1.3 @@ -5,10 +5,12 @@
     1.4  
     1.5  WMSRC = wm.c draw.c util.c
     1.6  WMOBJ = ${WMSRC:.c=.o}
     1.7 +MENSRC = gridmenu.c draw.c util.c
     1.8 +MENOBJ = ${MENSRC:.c=.o}
     1.9  MAN = gridwm.1
    1.10  BIN = gridwm gridmenu     
    1.11  
    1.12 -all: config gridwm
    1.13 +all: config gridwm gridmenu
    1.14  	@echo finished
    1.15  
    1.16  config:
    1.17 @@ -22,18 +24,22 @@
    1.18  	@echo CC $<
    1.19  	@${CC} -c ${CFLAGS} $<
    1.20  
    1.21 -${WMOBJ}: wm.h draw.h config.h
    1.22 +${WMOBJ}: wm.h draw.h config.h util.h
    1.23 +
    1.24 +gridmenu: ${MENOBJ}
    1.25 +	@echo LD $@
    1.26 +	@${CC} -o $@ ${MENOBJ} ${LDFLAGS}
    1.27  
    1.28  gridwm: ${WMOBJ}
    1.29  	@echo LD $@
    1.30  	@${CC} -o $@ ${WMOBJ} ${LDFLAGS}
    1.31  
    1.32  clean:
    1.33 -	rm -f gridwm *.o
    1.34 +	rm -f gridwm gridmenu *.o core
    1.35  
    1.36  dist: clean
    1.37  	mkdir -p gridwm-${VERSION}
    1.38 -	cp -R Makefile README LICENSE config.mk ${WMSRC} ${MAN} gridwm-${VERSION}
    1.39 +	cp -R Makefile README LICENSE config.mk *.h *.c ${MAN} gridwm-${VERSION}
    1.40  	tar -cf gridwm-${VERSION}.tar gridwm-${VERSION}
    1.41  	gzip gridwm-${VERSION}.tar
    1.42  	rm -rf gridwm-${VERSION}
     2.1 --- a/config.h	Mon Jul 10 18:35:39 2006 +0200
     2.2 +++ b/config.h	Mon Jul 10 19:46:24 2006 +0200
     2.3 @@ -3,7 +3,7 @@
     2.4   * See LICENSE file for license details.
     2.5   */
     2.6  
     2.7 -#define FONT	"fixed"
     2.8 -#define FGCOLOR	"#000000"
     2.9 -#define BGCOLOR	"#ffaa00"
    2.10 -#define BOCOLOR	"#000000"
    2.11 +#define FONT		"-*-terminus-medium-*-*-*-14-*-*-*-*-*-iso10646-*"
    2.12 +#define FGCOLOR		"#000000"
    2.13 +#define BGCOLOR		"#ffaa00"
    2.14 +#define BORDERCOLOR	"#000000"
     3.1 --- a/draw.c	Mon Jul 10 18:35:39 2006 +0200
     3.2 +++ b/draw.c	Mon Jul 10 19:46:24 2006 +0200
     3.3 @@ -14,7 +14,7 @@
     3.4  {
     3.5  	XPoint points[5];
     3.6  	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
     3.7 -	XSetForeground(dpy, b->gc, b->color.border);
     3.8 +	XSetForeground(dpy, b->gc, b->border);
     3.9  	points[0].x = b->rect.x;
    3.10  	points[0].y = b->rect.y;
    3.11  	points[1].x = b->rect.width - 1;
    3.12 @@ -29,54 +29,54 @@
    3.13  }
    3.14  
    3.15  void
    3.16 -draw(Display *dpy, Brush *b)
    3.17 +draw(Display *dpy, Brush *b, Bool border, const char *text)
    3.18  {
    3.19  	unsigned int x, y, w, h, len;
    3.20  	static char buf[256];
    3.21  	XGCValues gcv;
    3.22  
    3.23 -	XSetForeground(dpy, b->gc, b->color.bg);
    3.24 +	XSetForeground(dpy, b->gc, b->bg);
    3.25  	XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
    3.26  
    3.27 -	if(b->border)
    3.28 +	if(border)
    3.29  		drawborder(dpy, b);
    3.30  
    3.31 -	if(!b->text)
    3.32 +	if(!text)
    3.33  		return;
    3.34  
    3.35 -	len = strlen(b->text);
    3.36 +	len = strlen(text);
    3.37  	if(len >= sizeof(buf))
    3.38  		len = sizeof(buf) - 1;
    3.39 -	memcpy(buf, b->text, len);
    3.40 +	memcpy(buf, text, len);
    3.41  	buf[len] = 0;
    3.42  
    3.43 -	h = b->font->ascent + b->font->descent;
    3.44 -	y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent;
    3.45 +	h = b->font.ascent + b->font.descent;
    3.46 +	y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent;
    3.47  	x = b->rect.x + (h / 2);
    3.48  
    3.49  	/* shorten text if necessary */
    3.50 -	while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h)
    3.51 +	while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h)
    3.52  		buf[--len] = 0;
    3.53  
    3.54  	if(w > b->rect.width)
    3.55  		return; /* too long */
    3.56  
    3.57 -	gcv.foreground = b->color.fg;
    3.58 -	gcv.background = b->color.bg;
    3.59 -	if(b->font->set) {
    3.60 +	gcv.foreground = b->fg;
    3.61 +	gcv.background = b->bg;
    3.62 +	if(b->font.set) {
    3.63  		XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
    3.64 -		XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc,
    3.65 +		XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
    3.66  				x, y, buf, len);
    3.67  	}
    3.68  	else {
    3.69 -		gcv.font = b->font->xfont->fid;
    3.70 +		gcv.font = b->font.xfont->fid;
    3.71  		XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
    3.72  		XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
    3.73  	}
    3.74  }
    3.75  
    3.76  static unsigned long
    3.77 -xloadcolor(Display *dpy, Colormap cmap, const char *colstr)
    3.78 +xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
    3.79  {
    3.80  	XColor color;
    3.81  	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    3.82 @@ -84,13 +84,13 @@
    3.83  }
    3.84  
    3.85  void
    3.86 -loadcolor(Display *dpy, int screen, Color *c,
    3.87 +loadcolors(Display *dpy, int screen, Brush *b,
    3.88  		const char *bg, const char *fg, const char *border)
    3.89  {
    3.90  	Colormap cmap = DefaultColormap(dpy, screen);
    3.91 -	c->bg = xloadcolor(dpy, cmap, bg);
    3.92 -	c->fg = xloadcolor(dpy, cmap, fg);
    3.93 -	c->border = xloadcolor(dpy, cmap, border);
    3.94 +	b->bg = xloadcolors(dpy, cmap, bg);
    3.95 +	b->fg = xloadcolors(dpy, cmap, fg);
    3.96 +	b->border = xloadcolors(dpy, cmap, border);
    3.97  }
    3.98  
    3.99  unsigned int
   3.100 @@ -160,4 +160,5 @@
   3.101  		font->ascent = font->xfont->ascent;
   3.102  		font->descent = font->xfont->descent;
   3.103  	}
   3.104 +	font->height = font->ascent + font->descent;
   3.105  }
     4.1 --- a/draw.h	Mon Jul 10 18:35:39 2006 +0200
     4.2 +++ b/draw.h	Mon Jul 10 19:46:24 2006 +0200
     4.3 @@ -3,4 +3,33 @@
     4.4   * See LICENSE file for license details.
     4.5   */
     4.6  
     4.7 -extern void error(char *errstr, ...);
     4.8 +#include <X11/Xlib.h>
     4.9 +#include <X11/Xlocale.h>
    4.10 +
    4.11 +typedef struct Brush Brush;
    4.12 +typedef struct Fnt Fnt;
    4.13 +
    4.14 +struct Fnt {
    4.15 +	XFontStruct *xfont;
    4.16 +	XFontSet set;
    4.17 +	int ascent;
    4.18 +	int descent;
    4.19 +	int height;
    4.20 +};
    4.21 +
    4.22 +struct Brush {
    4.23 +	GC gc;
    4.24 +	Drawable drawable;
    4.25 +	XRectangle rect;
    4.26 +	Fnt font;
    4.27 +	unsigned long bg;
    4.28 +	unsigned long fg;
    4.29 +	unsigned long border;
    4.30 +};
    4.31 +
    4.32 +extern void draw(Display *dpy, Brush *b, Bool border, const char *text);
    4.33 +extern void loadcolors(Display *dpy, int screen, Brush *b,
    4.34 +		const char *bg, const char *fg, const char *bo);
    4.35 +extern void loadfont(Display *dpy, Fnt *font, const char *fontstr);
    4.36 +extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len);
    4.37 +extern unsigned int textwidth(Fnt *font, char *text);
     5.1 --- a/gridmenu.c	Mon Jul 10 18:35:39 2006 +0200
     5.2 +++ b/gridmenu.c	Mon Jul 10 19:46:24 2006 +0200
     5.3 @@ -4,6 +4,10 @@
     5.4   * See LICENSE file for license details.
     5.5   */
     5.6  
     5.7 +#include "config.h"
     5.8 +#include "draw.h"
     5.9 +#include "util.h"
    5.10 +
    5.11  #include <ctype.h>
    5.12  #include <stdlib.h>
    5.13  #include <stdio.h>
    5.14 @@ -12,14 +16,10 @@
    5.15  #include <sys/wait.h>
    5.16  #include <time.h>
    5.17  #include <unistd.h>
    5.18 -#include <X11/Xlib.h>
    5.19  #include <X11/cursorfont.h>
    5.20  #include <X11/Xutil.h>
    5.21  #include <X11/keysym.h>
    5.22  
    5.23 -#include <blitz.h>
    5.24 -#include <cext.h>
    5.25 -
    5.26  typedef struct Item Item;
    5.27  
    5.28  struct Item {
    5.29 @@ -28,37 +28,40 @@
    5.30  	char *text;
    5.31  };
    5.32  
    5.33 -static char *title = nil;
    5.34 +static Display *dpy;
    5.35 +static Window root;
    5.36 +static Window win;
    5.37 +static XRectangle rect;
    5.38  static Bool done = False;
    5.39 +
    5.40 +static Item *allitem = 0;	/* first of all items */
    5.41 +static Item *item = 0;	/* first of pattern matching items */
    5.42 +static Item *sel = 0;
    5.43 +static Item *nextoff = 0;
    5.44 +static Item *prevoff = 0;
    5.45 +static Item *curroff = 0;
    5.46 +
    5.47 +static int screen;
    5.48 +static char *title = 0;
    5.49 +static char text[4096];
    5.50  static int ret = 0;
    5.51 -static char text[4096];
    5.52 -static BlitzColor selcolor;
    5.53 -static BlitzColor normcolor;
    5.54 -static Window win;
    5.55 -static XRectangle mrect;
    5.56 -static Item *allitem = nil;	/* first of all items */
    5.57 -static Item *item = nil;	/* first of pattern matching items */
    5.58 -static Item *sel = nil;
    5.59 -static Item *nextoff = nil;
    5.60 -static Item *prevoff = nil;
    5.61 -static Item *curroff = nil;
    5.62  static int nitem = 0;
    5.63  static unsigned int cmdw = 0;
    5.64  static unsigned int twidth = 0;
    5.65  static unsigned int cwidth = 0;
    5.66 -static Blitz blz = {0};
    5.67 -static BlitzBrush brush = {0};
    5.68  static const int seek = 30;		/* 30px */
    5.69  
    5.70 +static Brush brush = {0};
    5.71 +
    5.72  static void draw_menu(void);
    5.73 -static void handle_kpress(XKeyEvent * e);
    5.74 +static void kpress(XKeyEvent * e);
    5.75  
    5.76 -static char version[] = "wmiimenu - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n";
    5.77 +static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
    5.78  
    5.79  static void
    5.80  usage()
    5.81  {
    5.82 -	fprintf(stderr, "%s", "usage: wmiimenu [-v] [-t <title>]\n");
    5.83 +	fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n");
    5.84  	exit(1);
    5.85  }
    5.86  
    5.87 @@ -71,21 +74,21 @@
    5.88  		return;
    5.89  
    5.90  	for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
    5.91 -		tw = blitz_textwidth(brush.font, nextoff->text);
    5.92 -		if(tw > mrect.width / 3)
    5.93 -			tw = mrect.width / 3;
    5.94 -		w += tw + mrect.height;
    5.95 -		if(w > mrect.width)
    5.96 +		tw = textwidth(&brush.font, nextoff->text);
    5.97 +		if(tw > rect.width / 3)
    5.98 +			tw = rect.width / 3;
    5.99 +		w += tw + brush.font.height;
   5.100 +		if(w > rect.width)
   5.101  			break;
   5.102  	}
   5.103  
   5.104  	w = cmdw + 2 * seek;
   5.105  	for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
   5.106 -		tw = blitz_textwidth(brush.font, prevoff->left->text);
   5.107 -		if(tw > mrect.width / 3)
   5.108 -			tw = mrect.width / 3;
   5.109 -		w += tw + mrect.height;
   5.110 -		if(w > mrect.width)
   5.111 +		tw = textwidth(&brush.font, prevoff->left->text);
   5.112 +		if(tw > rect.width / 3)
   5.113 +			tw = rect.width / 3;
   5.114 +		w += tw + brush.font.height;
   5.115 +		if(w > rect.width)
   5.116  			break;
   5.117  	}
   5.118  }
   5.119 @@ -104,7 +107,7 @@
   5.120  	else
   5.121  		cmdw = twidth;
   5.122  
   5.123 -	item = j = nil;
   5.124 +	item = j = 0;
   5.125  	nitem = 0;
   5.126  
   5.127  	for(i = allitem; i; i=i->next)
   5.128 @@ -114,7 +117,7 @@
   5.129  			else
   5.130  				j->right = i;
   5.131  			i->left = j;
   5.132 -			i->right = nil;
   5.133 +			i->right = 0;
   5.134  			j = i;
   5.135  			nitem++;
   5.136  		}
   5.137 @@ -126,7 +129,7 @@
   5.138  			else
   5.139  				j->right = i;
   5.140  			i->left = j;
   5.141 -			i->right = nil;
   5.142 +			i->right = 0;
   5.143  			j = i;
   5.144  			nitem++;
   5.145  		}
   5.146 @@ -141,72 +144,62 @@
   5.147  draw_menu()
   5.148  {
   5.149  	unsigned int offx = 0;
   5.150 -
   5.151  	Item *i;
   5.152  
   5.153 -	brush.align = WEST;
   5.154 -
   5.155 -	brush.rect = mrect;
   5.156 +	brush.rect = rect;
   5.157  	brush.rect.x = 0;
   5.158  	brush.rect.y = 0;
   5.159 -	brush.color = normcolor;
   5.160 -	brush.border = False;
   5.161 -	blitz_draw_tile(&brush);
   5.162 +	draw(dpy, &brush, False, 0);
   5.163  
   5.164  	/* print command */
   5.165  	if(!title || text[0]) {
   5.166 -		brush.color = normcolor;
   5.167  		cmdw = cwidth;
   5.168  		if(cmdw && item)
   5.169  			brush.rect.width = cmdw;
   5.170 -		blitz_draw_label(&brush, text);
   5.171 +		draw(dpy, &brush, False, text);
   5.172  	}
   5.173  	else {
   5.174  		cmdw = twidth;
   5.175 -		brush.color = selcolor;
   5.176  		brush.rect.width = cmdw;
   5.177 -		blitz_draw_label(&brush, title);
   5.178 +		draw(dpy, &brush, False, title);
   5.179  	}
   5.180  	offx += brush.rect.width;
   5.181  
   5.182 -	brush.align = CENTER;
   5.183  	if(curroff) {
   5.184 -		brush.color = normcolor;
   5.185  		brush.rect.x = offx;
   5.186  		brush.rect.width = seek;
   5.187  		offx += brush.rect.width;
   5.188 -		blitz_draw_label(&brush, (curroff && curroff->left) ? "<" : nil);
   5.189 +		draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
   5.190  
   5.191  		/* determine maximum items */
   5.192  		for(i = curroff; i != nextoff; i=i->right) {
   5.193 -			brush.color = normcolor;
   5.194  			brush.border = False;
   5.195  			brush.rect.x = offx;
   5.196 -			brush.rect.width = blitz_textwidth(brush.font, i->text);
   5.197 -			if(brush.rect.width > mrect.width / 3)
   5.198 -				brush.rect.width = mrect.width / 3;
   5.199 -			brush.rect.width += mrect.height;
   5.200 +			brush.rect.width = textwidth(&brush.font, i->text);
   5.201 +			if(brush.rect.width > rect.width / 3)
   5.202 +				brush.rect.width = rect.width / 3;
   5.203 +			brush.rect.width += brush.font.height;
   5.204  			if(sel == i) {
   5.205 -				brush.color = selcolor;
   5.206 -				brush.border = True;
   5.207 +				swap((void **)&brush.fg, (void **)&brush.bg);
   5.208 +				draw(dpy, &brush, True, i->text);
   5.209 +				swap((void **)&brush.fg, (void **)&brush.bg);
   5.210  			}
   5.211 -			blitz_draw_label(&brush, i->text);
   5.212 +			else
   5.213 +				draw(dpy, &brush, False, i->text);
   5.214  			offx += brush.rect.width;
   5.215  		}
   5.216  
   5.217 -		brush.color = normcolor;
   5.218 -		brush.border = False;
   5.219 -		brush.rect.x = mrect.width - seek;
   5.220 +		brush.rect.x = rect.width - seek;
   5.221  		brush.rect.width = seek;
   5.222 -		blitz_draw_label(&brush, nextoff ? ">" : nil);
   5.223 +		draw(dpy, &brush, False, nextoff ? ">" : 0);
   5.224  	}
   5.225 -	XCopyArea(blz.dpy, brush.drawable, win, brush.gc, 0, 0, mrect.width,
   5.226 -			mrect.height, 0, 0);
   5.227 -	XSync(blz.dpy, False);
   5.228 +	XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, rect.width,
   5.229 +			rect.height, 0, 0);
   5.230 +	XFlush(dpy);
   5.231  }
   5.232  
   5.233  static void
   5.234 -handle_kpress(XKeyEvent * e)
   5.235 +kpress(XKeyEvent * e)
   5.236  {
   5.237  	KeySym ksym;
   5.238  	char buf[32];
   5.239 @@ -272,7 +265,7 @@
   5.240  	case XK_Tab:
   5.241  		if(!sel)
   5.242  			return;
   5.243 -		cext_strlcpy(text, sel->text, sizeof(text));
   5.244 +		strncpy(text, sel->text, sizeof(text));
   5.245  		update_items(text);
   5.246  		break;
   5.247  	case XK_Right:
   5.248 @@ -314,9 +307,9 @@
   5.249  		if((num == 1) && !iscntrl((int) buf[0])) {
   5.250  			buf[num] = 0;
   5.251  			if(len > 0)
   5.252 -				cext_strlcat(text, buf, sizeof(text));
   5.253 +				strncat(text, buf, sizeof(text));
   5.254  			else
   5.255 -				cext_strlcpy(text, buf, sizeof(text));
   5.256 +				strncpy(text, buf, sizeof(text));
   5.257  			update_items(text);
   5.258  		}
   5.259  	}
   5.260 @@ -326,24 +319,24 @@
   5.261  static char *
   5.262  read_allitems()
   5.263  {
   5.264 -	static char *maxname = nil;
   5.265 +	static char *maxname = 0;
   5.266  	char *p, buf[1024];
   5.267  	unsigned int len = 0, max = 0;
   5.268  	Item *i, *new;
   5.269  
   5.270 -	i = nil;
   5.271 +	i = 0;
   5.272  	while(fgets(buf, sizeof(buf), stdin)) {
   5.273  		len = strlen(buf);
   5.274  		if (buf[len - 1] == '\n')
   5.275  			buf[len - 1] = 0;
   5.276 -		p = cext_estrdup(buf);
   5.277 +		p = estrdup(buf);
   5.278  		if(max < len) {
   5.279  			maxname = p;
   5.280  			max = len;
   5.281  		}
   5.282  
   5.283 -		new = cext_emalloc(sizeof(Item));
   5.284 -		new->next = new->left = new->right = nil;
   5.285 +		new = emalloc(sizeof(Item));
   5.286 +		new->next = new->left = new->right = 0;
   5.287  		new->text = p;
   5.288  		if(!i)
   5.289  			allitem = new;
   5.290 @@ -360,10 +353,7 @@
   5.291  {
   5.292  	int i;
   5.293  	XSetWindowAttributes wa;
   5.294 -	char *maxname, *p;
   5.295 -	BlitzFont font = {0};
   5.296 -	GC gc;
   5.297 -	Drawable pmap;
   5.298 +	char *maxname;
   5.299  	XEvent ev;
   5.300  
   5.301  	/* command line args */
   5.302 @@ -388,93 +378,70 @@
   5.303  			usage();
   5.304  	}
   5.305  
   5.306 -	blz.dpy = XOpenDisplay(0);
   5.307 -	if(!blz.dpy) {
   5.308 -		fprintf(stderr, "%s", "wmiimenu: cannot open dpy\n");
   5.309 -		exit(1);
   5.310 -	}
   5.311 -	blz.screen = DefaultScreen(blz.dpy);
   5.312 -	blz.root = RootWindow(blz.dpy, blz.screen);
   5.313 +	dpy = XOpenDisplay(0);
   5.314 +	if(!dpy)
   5.315 +		error("gridmenu: cannot open dpy\n");
   5.316 +	screen = DefaultScreen(dpy);
   5.317 +	root = RootWindow(dpy, screen);
   5.318  
   5.319  	maxname = read_allitems();
   5.320  
   5.321  	/* grab as early as possible, but after reading all items!!! */
   5.322 -	while(XGrabKeyboard
   5.323 -			(blz.dpy, blz.root, True, GrabModeAsync,
   5.324 +	while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
   5.325  			 GrabModeAsync, CurrentTime) != GrabSuccess)
   5.326  		usleep(1000);
   5.327  
   5.328 -	font.fontstr = getenv("WMII_FONT");
   5.329 -	if (!font.fontstr)
   5.330 -		font.fontstr = cext_estrdup(BLITZ_FONT);
   5.331 -	blitz_loadfont(&blz, &font);
   5.332 -
   5.333 -	if((p = getenv("WMII_NORMCOLORS")))
   5.334 -		cext_strlcpy(normcolor.colstr, p, sizeof(normcolor.colstr));
   5.335 -	if(strlen(normcolor.colstr) != 23)
   5.336 -		cext_strlcpy(normcolor.colstr, BLITZ_NORMCOLORS, sizeof(normcolor.colstr));
   5.337 -	blitz_loadcolor(&blz, &normcolor);
   5.338 -
   5.339 -	if((p = getenv("WMII_SELCOLORS")))
   5.340 -		cext_strlcpy(selcolor.colstr, p, sizeof(selcolor.colstr));
   5.341 -	if(strlen(selcolor.colstr) != 23)
   5.342 -		cext_strlcpy(selcolor.colstr, BLITZ_SELCOLORS, sizeof(selcolor.colstr));
   5.343 -	blitz_loadcolor(&blz, &selcolor);
   5.344 +	/* style */
   5.345 +	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
   5.346 +	loadfont(dpy, &brush.font, FONT);
   5.347  
   5.348  	wa.override_redirect = 1;
   5.349  	wa.background_pixmap = ParentRelative;
   5.350  	wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask
   5.351  		| SubstructureRedirectMask | SubstructureNotifyMask;
   5.352  
   5.353 -	mrect.width = DisplayWidth(blz.dpy, blz.screen);
   5.354 -	mrect.height = font.ascent + font.descent + 4;
   5.355 -	mrect.y = DisplayHeight(blz.dpy, blz.screen) - mrect.height;
   5.356 -	mrect.x = 0;
   5.357 +	rect.width = DisplayWidth(dpy, screen);
   5.358 +	rect.height = brush.font.height + 4;
   5.359 +	rect.y = DisplayHeight(dpy, screen) - rect.height;
   5.360 +	rect.x = 0;
   5.361  
   5.362 -	win = XCreateWindow(blz.dpy, blz.root, mrect.x, mrect.y,
   5.363 -			mrect.width, mrect.height, 0, DefaultDepth(blz.dpy, blz.screen),
   5.364 -			CopyFromParent, DefaultVisual(blz.dpy, blz.screen),
   5.365 +	win = XCreateWindow(dpy, root, rect.x, rect.y,
   5.366 +			rect.width, rect.height, 0, DefaultDepth(dpy, screen),
   5.367 +			CopyFromParent, DefaultVisual(dpy, screen),
   5.368  			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
   5.369 -	XDefineCursor(blz.dpy, win, XCreateFontCursor(blz.dpy, XC_xterm));
   5.370 -	XSync(blz.dpy, False);
   5.371 +	XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
   5.372 +	XFlush(dpy);
   5.373  
   5.374  	/* pixmap */
   5.375 -	gc = XCreateGC(blz.dpy, win, 0, 0);
   5.376 -	pmap = XCreatePixmap(blz.dpy, win, mrect.width, mrect.height,
   5.377 -			DefaultDepth(blz.dpy, blz.screen));
   5.378 -
   5.379 -	XSync(blz.dpy, False);
   5.380 -
   5.381 -	brush.blitz = &blz;
   5.382 -	brush.color = normcolor;
   5.383 -	brush.drawable = pmap;
   5.384 -	brush.gc = gc;
   5.385 -	brush.font = &font;
   5.386 +	brush.gc = XCreateGC(dpy, win, 0, 0);
   5.387 +	brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height,
   5.388 +			DefaultDepth(dpy, screen));
   5.389 +	XFlush(dpy);
   5.390  
   5.391  	if(maxname)
   5.392 -		cwidth = blitz_textwidth(brush.font, maxname) + mrect.height;
   5.393 -	if(cwidth > mrect.width / 3)
   5.394 -		cwidth = mrect.width / 3;
   5.395 +		cwidth = textwidth(&brush.font, maxname) + brush.font.height;
   5.396 +	if(cwidth > rect.width / 3)
   5.397 +		cwidth = rect.width / 3;
   5.398  
   5.399  	if(title) {
   5.400 -		twidth = blitz_textwidth(brush.font, title) + mrect.height;
   5.401 -		if(twidth > mrect.width / 3)
   5.402 -			twidth = mrect.width / 3;
   5.403 +		twidth = textwidth(&brush.font, title) + brush.font.height;
   5.404 +		if(twidth > rect.width / 3)
   5.405 +			twidth = rect.width / 3;
   5.406  	}
   5.407  
   5.408  	cmdw = title ? twidth : cwidth;
   5.409  
   5.410  	text[0] = 0;
   5.411  	update_items(text);
   5.412 -	XMapRaised(blz.dpy, win);
   5.413 +	XMapRaised(dpy, win);
   5.414  	draw_menu();
   5.415 -	XSync(blz.dpy, False);
   5.416 +	XFlush(dpy);
   5.417  
   5.418  	/* main event loop */
   5.419 -	while(!XNextEvent(blz.dpy, &ev)) {
   5.420 +	while(!XNextEvent(dpy, &ev)) {
   5.421  		switch (ev.type) {
   5.422  			case KeyPress:
   5.423 -				handle_kpress(&ev.xkey);
   5.424 +				kpress(&ev.xkey);
   5.425  				break;
   5.426  			case Expose:
   5.427  				if(ev.xexpose.count == 0) {
   5.428 @@ -488,11 +455,11 @@
   5.429  			break;
   5.430  	}
   5.431  
   5.432 -	XUngrabKeyboard(blz.dpy, CurrentTime);
   5.433 -	XFreePixmap(blz.dpy, pmap);
   5.434 -	XFreeGC(blz.dpy, gc);
   5.435 -	XDestroyWindow(blz.dpy, win);
   5.436 -	XCloseDisplay(blz.dpy);
   5.437 +	XUngrabKeyboard(dpy, CurrentTime);
   5.438 +	XFreePixmap(dpy, brush.drawable);
   5.439 +	XFreeGC(dpy, brush.gc);
   5.440 +	XDestroyWindow(dpy, win);
   5.441 +	XCloseDisplay(dpy);
   5.442  
   5.443  	return ret;
   5.444  }
     6.1 --- a/util.c	Mon Jul 10 18:35:39 2006 +0200
     6.2 +++ b/util.c	Mon Jul 10 19:46:24 2006 +0200
     6.3 @@ -6,6 +6,7 @@
     6.4  #include <stdarg.h>
     6.5  #include <stdio.h>
     6.6  #include <stdlib.h>
     6.7 +#include <string.h>
     6.8  
     6.9  void
    6.10  error(char *errstr, ...) {
    6.11 @@ -16,3 +17,61 @@
    6.12  	exit(1);
    6.13  }
    6.14  
    6.15 +static void
    6.16 +bad_malloc(unsigned int size)
    6.17 +{
    6.18 +	fprintf(stderr, "fatal: could not malloc() %d bytes\n",
    6.19 +			(int) size);
    6.20 +	exit(1);
    6.21 +}
    6.22 +
    6.23 +void *
    6.24 +emallocz(unsigned int size)
    6.25 +{
    6.26 +	void *res = calloc(1, size);
    6.27 +	if(!res)
    6.28 +		bad_malloc(size);
    6.29 +	return res;
    6.30 +}
    6.31 +
    6.32 +void *
    6.33 +emalloc(unsigned int size)
    6.34 +{
    6.35 +	void *res = malloc(size);
    6.36 +	if(!res)
    6.37 +		bad_malloc(size);
    6.38 +	return res;
    6.39 +}
    6.40 +
    6.41 +void *
    6.42 +erealloc(void *ptr, unsigned int size)
    6.43 +{
    6.44 +	void *res = realloc(ptr, size);
    6.45 +	if(!res)
    6.46 +		bad_malloc(size);
    6.47 +	return res;
    6.48 +}
    6.49 +
    6.50 +char *
    6.51 +estrdup(const char *str)
    6.52 +{
    6.53 +	void *res = strdup(str);
    6.54 +	if(!res)
    6.55 +		bad_malloc(strlen(str));
    6.56 +	return res;
    6.57 +}
    6.58 +
    6.59 +void
    6.60 +failed_assert(char *a, char *file, int line)
    6.61 +{
    6.62 +	fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
    6.63 +	abort();
    6.64 +}
    6.65 +
    6.66 +void
    6.67 +swap(void **p1, void **p2)
    6.68 +{
    6.69 +	void *tmp = *p1;
    6.70 +	*p1 = *p2;
    6.71 +	*p2 = tmp;
    6.72 +}
     7.1 --- a/util.h	Mon Jul 10 18:35:39 2006 +0200
     7.2 +++ b/util.h	Mon Jul 10 19:46:24 2006 +0200
     7.3 @@ -3,39 +3,14 @@
     7.4   * See LICENSE file for license details.
     7.5   */
     7.6  
     7.7 -#include <X11/Xlib.h>
     7.8 -#include <X11/Xlocale.h>
     7.9 -
    7.10 -typedef struct Brush Brush;
    7.11 -typedef struct Color Color;
    7.12 -typedef struct Fnt Fnt;
    7.13 -
    7.14 -struct Color {
    7.15 -	unsigned long bg;
    7.16 -	unsigned long fg;
    7.17 -	unsigned long border;
    7.18 -};
    7.19 -
    7.20 -struct Fnt {
    7.21 -	XFontStruct *xfont;
    7.22 -	XFontSet set;
    7.23 -	int ascent;
    7.24 -	int descent;
    7.25 -};
    7.26 -
    7.27 -struct Brush {
    7.28 -	GC gc;
    7.29 -	Drawable drawable;
    7.30 -	XRectangle rect;
    7.31 -	Bool border;
    7.32 -	Fnt *font;
    7.33 -	Color color;
    7.34 -	const char *text;
    7.35 -};
    7.36 -
    7.37 -extern void draw(Display *dpy, Brush *b);
    7.38 -extern void loadcolor(Display *dpy, int screen, Color *c,
    7.39 -		const char *bg, const char *fg, const char *bo);
    7.40 -extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len);
    7.41 -extern unsigned int textwidth(Fnt *font, char *text);
    7.42 -extern void loadfont(Display *dpy, Fnt *font, const char *fontstr);
    7.43 +extern void error(char *errstr, ...);
    7.44 +extern void *emallocz(unsigned int size);
    7.45 +extern void *emalloc(unsigned int size);
    7.46 +extern void *erealloc(void *ptr, unsigned int size);
    7.47 +extern char *estrdup(const char *str);
    7.48 +#define eassert(a) do { \
    7.49 +		if(!(a)) \
    7.50 +			failed_assert(#a, __FILE__, __LINE__); \
    7.51 +	} while (0)
    7.52 +void failed_assert(char *a, char *file, int line);
    7.53 +void swap(void **p1, void **p2);
     8.1 --- a/wm.c	Mon Jul 10 18:35:39 2006 +0200
     8.2 +++ b/wm.c	Mon Jul 10 19:46:24 2006 +0200
     8.3 @@ -13,15 +13,20 @@
     8.4  
     8.5  #include "wm.h"
     8.6  
     8.7 +/* X structs */
     8.8  Display *dpy;
     8.9  Window root;
    8.10  XRectangle rect;
    8.11 -int screen, sel_screen;
    8.12 +Pixmap pmap;
    8.13  Atom wm_atom[WMLast];
    8.14  Atom net_atom[NetLast];
    8.15  Cursor cursor[CurLast];
    8.16 +
    8.17 +int screen, sel_screen;
    8.18  unsigned int kmask, numlock_mask;
    8.19 -Pixmap pmap;
    8.20 +
    8.21 +/* draw structs */
    8.22 +Brush brush = {0};
    8.23  
    8.24  enum { WM_PROTOCOL_DELWIN = 1 };
    8.25  
    8.26 @@ -208,7 +213,7 @@
    8.27  	XSetErrorHandler(startup_error_handler);
    8.28  	/* this causes an error if some other WM is running */
    8.29  	XSelectInput(dpy, root, SubstructureRedirectMask);
    8.30 -	XSync(dpy, False);
    8.31 +	XFlush(dpy);
    8.32  
    8.33  	if(other_wm_running)
    8.34  		error("gridwm: another window manager is already running\n");
    8.35 @@ -246,6 +251,10 @@
    8.36  	wa.cursor = cursor[CurNormal];
    8.37  	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
    8.38  
    8.39 +	/* style */
    8.40 +	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
    8.41 +	loadfont(dpy, &brush.font, FONT);
    8.42 +
    8.43  	scan_wins();
    8.44  
    8.45  	cleanup();
     9.1 --- a/wm.h	Mon Jul 10 18:35:39 2006 +0200
     9.2 +++ b/wm.h	Mon Jul 10 19:46:24 2006 +0200
     9.3 @@ -3,6 +3,7 @@
     9.4   * See LICENSE file for license details.
     9.5   */
     9.6  
     9.7 +#include "config.h"
     9.8  #include "draw.h"
     9.9  #include "util.h"
    9.10  
    9.11 @@ -48,11 +49,14 @@
    9.12  extern Display *dpy;
    9.13  extern Window root;
    9.14  extern XRectangle rect;
    9.15 -extern int screen, sel_screen;
    9.16 -extern unsigned int kmask, numlock_mask;
    9.17  extern Atom wm_atom[WMLast];
    9.18  extern Atom net_atom[NetLast];
    9.19  extern Cursor cursor[CurLast];
    9.20  extern Pixmap pmap;
    9.21  
    9.22 +extern int screen, sel_screen;
    9.23 +extern unsigned int kmask, numlock_mask;
    9.24 +
    9.25 +extern Brush brush;
    9.26 +
    9.27  /* wm.c */