dwm-meillo

diff gridmenu.c @ 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 f10194d4b76d
children
line diff
     1.1 --- a/gridmenu.c	Mon Jul 10 18:35:39 2006 +0200
     1.2 +++ b/gridmenu.c	Mon Jul 10 19:46:24 2006 +0200
     1.3 @@ -4,6 +4,10 @@
     1.4   * See LICENSE file for license details.
     1.5   */
     1.6  
     1.7 +#include "config.h"
     1.8 +#include "draw.h"
     1.9 +#include "util.h"
    1.10 +
    1.11  #include <ctype.h>
    1.12  #include <stdlib.h>
    1.13  #include <stdio.h>
    1.14 @@ -12,14 +16,10 @@
    1.15  #include <sys/wait.h>
    1.16  #include <time.h>
    1.17  #include <unistd.h>
    1.18 -#include <X11/Xlib.h>
    1.19  #include <X11/cursorfont.h>
    1.20  #include <X11/Xutil.h>
    1.21  #include <X11/keysym.h>
    1.22  
    1.23 -#include <blitz.h>
    1.24 -#include <cext.h>
    1.25 -
    1.26  typedef struct Item Item;
    1.27  
    1.28  struct Item {
    1.29 @@ -28,37 +28,40 @@
    1.30  	char *text;
    1.31  };
    1.32  
    1.33 -static char *title = nil;
    1.34 +static Display *dpy;
    1.35 +static Window root;
    1.36 +static Window win;
    1.37 +static XRectangle rect;
    1.38  static Bool done = False;
    1.39 +
    1.40 +static Item *allitem = 0;	/* first of all items */
    1.41 +static Item *item = 0;	/* first of pattern matching items */
    1.42 +static Item *sel = 0;
    1.43 +static Item *nextoff = 0;
    1.44 +static Item *prevoff = 0;
    1.45 +static Item *curroff = 0;
    1.46 +
    1.47 +static int screen;
    1.48 +static char *title = 0;
    1.49 +static char text[4096];
    1.50  static int ret = 0;
    1.51 -static char text[4096];
    1.52 -static BlitzColor selcolor;
    1.53 -static BlitzColor normcolor;
    1.54 -static Window win;
    1.55 -static XRectangle mrect;
    1.56 -static Item *allitem = nil;	/* first of all items */
    1.57 -static Item *item = nil;	/* first of pattern matching items */
    1.58 -static Item *sel = nil;
    1.59 -static Item *nextoff = nil;
    1.60 -static Item *prevoff = nil;
    1.61 -static Item *curroff = nil;
    1.62  static int nitem = 0;
    1.63  static unsigned int cmdw = 0;
    1.64  static unsigned int twidth = 0;
    1.65  static unsigned int cwidth = 0;
    1.66 -static Blitz blz = {0};
    1.67 -static BlitzBrush brush = {0};
    1.68  static const int seek = 30;		/* 30px */
    1.69  
    1.70 +static Brush brush = {0};
    1.71 +
    1.72  static void draw_menu(void);
    1.73 -static void handle_kpress(XKeyEvent * e);
    1.74 +static void kpress(XKeyEvent * e);
    1.75  
    1.76 -static char version[] = "wmiimenu - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n";
    1.77 +static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
    1.78  
    1.79  static void
    1.80  usage()
    1.81  {
    1.82 -	fprintf(stderr, "%s", "usage: wmiimenu [-v] [-t <title>]\n");
    1.83 +	fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n");
    1.84  	exit(1);
    1.85  }
    1.86  
    1.87 @@ -71,21 +74,21 @@
    1.88  		return;
    1.89  
    1.90  	for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
    1.91 -		tw = blitz_textwidth(brush.font, nextoff->text);
    1.92 -		if(tw > mrect.width / 3)
    1.93 -			tw = mrect.width / 3;
    1.94 -		w += tw + mrect.height;
    1.95 -		if(w > mrect.width)
    1.96 +		tw = textwidth(&brush.font, nextoff->text);
    1.97 +		if(tw > rect.width / 3)
    1.98 +			tw = rect.width / 3;
    1.99 +		w += tw + brush.font.height;
   1.100 +		if(w > rect.width)
   1.101  			break;
   1.102  	}
   1.103  
   1.104  	w = cmdw + 2 * seek;
   1.105  	for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
   1.106 -		tw = blitz_textwidth(brush.font, prevoff->left->text);
   1.107 -		if(tw > mrect.width / 3)
   1.108 -			tw = mrect.width / 3;
   1.109 -		w += tw + mrect.height;
   1.110 -		if(w > mrect.width)
   1.111 +		tw = textwidth(&brush.font, prevoff->left->text);
   1.112 +		if(tw > rect.width / 3)
   1.113 +			tw = rect.width / 3;
   1.114 +		w += tw + brush.font.height;
   1.115 +		if(w > rect.width)
   1.116  			break;
   1.117  	}
   1.118  }
   1.119 @@ -104,7 +107,7 @@
   1.120  	else
   1.121  		cmdw = twidth;
   1.122  
   1.123 -	item = j = nil;
   1.124 +	item = j = 0;
   1.125  	nitem = 0;
   1.126  
   1.127  	for(i = allitem; i; i=i->next)
   1.128 @@ -114,7 +117,7 @@
   1.129  			else
   1.130  				j->right = i;
   1.131  			i->left = j;
   1.132 -			i->right = nil;
   1.133 +			i->right = 0;
   1.134  			j = i;
   1.135  			nitem++;
   1.136  		}
   1.137 @@ -126,7 +129,7 @@
   1.138  			else
   1.139  				j->right = i;
   1.140  			i->left = j;
   1.141 -			i->right = nil;
   1.142 +			i->right = 0;
   1.143  			j = i;
   1.144  			nitem++;
   1.145  		}
   1.146 @@ -141,72 +144,62 @@
   1.147  draw_menu()
   1.148  {
   1.149  	unsigned int offx = 0;
   1.150 -
   1.151  	Item *i;
   1.152  
   1.153 -	brush.align = WEST;
   1.154 -
   1.155 -	brush.rect = mrect;
   1.156 +	brush.rect = rect;
   1.157  	brush.rect.x = 0;
   1.158  	brush.rect.y = 0;
   1.159 -	brush.color = normcolor;
   1.160 -	brush.border = False;
   1.161 -	blitz_draw_tile(&brush);
   1.162 +	draw(dpy, &brush, False, 0);
   1.163  
   1.164  	/* print command */
   1.165  	if(!title || text[0]) {
   1.166 -		brush.color = normcolor;
   1.167  		cmdw = cwidth;
   1.168  		if(cmdw && item)
   1.169  			brush.rect.width = cmdw;
   1.170 -		blitz_draw_label(&brush, text);
   1.171 +		draw(dpy, &brush, False, text);
   1.172  	}
   1.173  	else {
   1.174  		cmdw = twidth;
   1.175 -		brush.color = selcolor;
   1.176  		brush.rect.width = cmdw;
   1.177 -		blitz_draw_label(&brush, title);
   1.178 +		draw(dpy, &brush, False, title);
   1.179  	}
   1.180  	offx += brush.rect.width;
   1.181  
   1.182 -	brush.align = CENTER;
   1.183  	if(curroff) {
   1.184 -		brush.color = normcolor;
   1.185  		brush.rect.x = offx;
   1.186  		brush.rect.width = seek;
   1.187  		offx += brush.rect.width;
   1.188 -		blitz_draw_label(&brush, (curroff && curroff->left) ? "<" : nil);
   1.189 +		draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
   1.190  
   1.191  		/* determine maximum items */
   1.192  		for(i = curroff; i != nextoff; i=i->right) {
   1.193 -			brush.color = normcolor;
   1.194  			brush.border = False;
   1.195  			brush.rect.x = offx;
   1.196 -			brush.rect.width = blitz_textwidth(brush.font, i->text);
   1.197 -			if(brush.rect.width > mrect.width / 3)
   1.198 -				brush.rect.width = mrect.width / 3;
   1.199 -			brush.rect.width += mrect.height;
   1.200 +			brush.rect.width = textwidth(&brush.font, i->text);
   1.201 +			if(brush.rect.width > rect.width / 3)
   1.202 +				brush.rect.width = rect.width / 3;
   1.203 +			brush.rect.width += brush.font.height;
   1.204  			if(sel == i) {
   1.205 -				brush.color = selcolor;
   1.206 -				brush.border = True;
   1.207 +				swap((void **)&brush.fg, (void **)&brush.bg);
   1.208 +				draw(dpy, &brush, True, i->text);
   1.209 +				swap((void **)&brush.fg, (void **)&brush.bg);
   1.210  			}
   1.211 -			blitz_draw_label(&brush, i->text);
   1.212 +			else
   1.213 +				draw(dpy, &brush, False, i->text);
   1.214  			offx += brush.rect.width;
   1.215  		}
   1.216  
   1.217 -		brush.color = normcolor;
   1.218 -		brush.border = False;
   1.219 -		brush.rect.x = mrect.width - seek;
   1.220 +		brush.rect.x = rect.width - seek;
   1.221  		brush.rect.width = seek;
   1.222 -		blitz_draw_label(&brush, nextoff ? ">" : nil);
   1.223 +		draw(dpy, &brush, False, nextoff ? ">" : 0);
   1.224  	}
   1.225 -	XCopyArea(blz.dpy, brush.drawable, win, brush.gc, 0, 0, mrect.width,
   1.226 -			mrect.height, 0, 0);
   1.227 -	XSync(blz.dpy, False);
   1.228 +	XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, rect.width,
   1.229 +			rect.height, 0, 0);
   1.230 +	XFlush(dpy);
   1.231  }
   1.232  
   1.233  static void
   1.234 -handle_kpress(XKeyEvent * e)
   1.235 +kpress(XKeyEvent * e)
   1.236  {
   1.237  	KeySym ksym;
   1.238  	char buf[32];
   1.239 @@ -272,7 +265,7 @@
   1.240  	case XK_Tab:
   1.241  		if(!sel)
   1.242  			return;
   1.243 -		cext_strlcpy(text, sel->text, sizeof(text));
   1.244 +		strncpy(text, sel->text, sizeof(text));
   1.245  		update_items(text);
   1.246  		break;
   1.247  	case XK_Right:
   1.248 @@ -314,9 +307,9 @@
   1.249  		if((num == 1) && !iscntrl((int) buf[0])) {
   1.250  			buf[num] = 0;
   1.251  			if(len > 0)
   1.252 -				cext_strlcat(text, buf, sizeof(text));
   1.253 +				strncat(text, buf, sizeof(text));
   1.254  			else
   1.255 -				cext_strlcpy(text, buf, sizeof(text));
   1.256 +				strncpy(text, buf, sizeof(text));
   1.257  			update_items(text);
   1.258  		}
   1.259  	}
   1.260 @@ -326,24 +319,24 @@
   1.261  static char *
   1.262  read_allitems()
   1.263  {
   1.264 -	static char *maxname = nil;
   1.265 +	static char *maxname = 0;
   1.266  	char *p, buf[1024];
   1.267  	unsigned int len = 0, max = 0;
   1.268  	Item *i, *new;
   1.269  
   1.270 -	i = nil;
   1.271 +	i = 0;
   1.272  	while(fgets(buf, sizeof(buf), stdin)) {
   1.273  		len = strlen(buf);
   1.274  		if (buf[len - 1] == '\n')
   1.275  			buf[len - 1] = 0;
   1.276 -		p = cext_estrdup(buf);
   1.277 +		p = estrdup(buf);
   1.278  		if(max < len) {
   1.279  			maxname = p;
   1.280  			max = len;
   1.281  		}
   1.282  
   1.283 -		new = cext_emalloc(sizeof(Item));
   1.284 -		new->next = new->left = new->right = nil;
   1.285 +		new = emalloc(sizeof(Item));
   1.286 +		new->next = new->left = new->right = 0;
   1.287  		new->text = p;
   1.288  		if(!i)
   1.289  			allitem = new;
   1.290 @@ -360,10 +353,7 @@
   1.291  {
   1.292  	int i;
   1.293  	XSetWindowAttributes wa;
   1.294 -	char *maxname, *p;
   1.295 -	BlitzFont font = {0};
   1.296 -	GC gc;
   1.297 -	Drawable pmap;
   1.298 +	char *maxname;
   1.299  	XEvent ev;
   1.300  
   1.301  	/* command line args */
   1.302 @@ -388,93 +378,70 @@
   1.303  			usage();
   1.304  	}
   1.305  
   1.306 -	blz.dpy = XOpenDisplay(0);
   1.307 -	if(!blz.dpy) {
   1.308 -		fprintf(stderr, "%s", "wmiimenu: cannot open dpy\n");
   1.309 -		exit(1);
   1.310 -	}
   1.311 -	blz.screen = DefaultScreen(blz.dpy);
   1.312 -	blz.root = RootWindow(blz.dpy, blz.screen);
   1.313 +	dpy = XOpenDisplay(0);
   1.314 +	if(!dpy)
   1.315 +		error("gridmenu: cannot open dpy\n");
   1.316 +	screen = DefaultScreen(dpy);
   1.317 +	root = RootWindow(dpy, screen);
   1.318  
   1.319  	maxname = read_allitems();
   1.320  
   1.321  	/* grab as early as possible, but after reading all items!!! */
   1.322 -	while(XGrabKeyboard
   1.323 -			(blz.dpy, blz.root, True, GrabModeAsync,
   1.324 +	while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
   1.325  			 GrabModeAsync, CurrentTime) != GrabSuccess)
   1.326  		usleep(1000);
   1.327  
   1.328 -	font.fontstr = getenv("WMII_FONT");
   1.329 -	if (!font.fontstr)
   1.330 -		font.fontstr = cext_estrdup(BLITZ_FONT);
   1.331 -	blitz_loadfont(&blz, &font);
   1.332 -
   1.333 -	if((p = getenv("WMII_NORMCOLORS")))
   1.334 -		cext_strlcpy(normcolor.colstr, p, sizeof(normcolor.colstr));
   1.335 -	if(strlen(normcolor.colstr) != 23)
   1.336 -		cext_strlcpy(normcolor.colstr, BLITZ_NORMCOLORS, sizeof(normcolor.colstr));
   1.337 -	blitz_loadcolor(&blz, &normcolor);
   1.338 -
   1.339 -	if((p = getenv("WMII_SELCOLORS")))
   1.340 -		cext_strlcpy(selcolor.colstr, p, sizeof(selcolor.colstr));
   1.341 -	if(strlen(selcolor.colstr) != 23)
   1.342 -		cext_strlcpy(selcolor.colstr, BLITZ_SELCOLORS, sizeof(selcolor.colstr));
   1.343 -	blitz_loadcolor(&blz, &selcolor);
   1.344 +	/* style */
   1.345 +	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
   1.346 +	loadfont(dpy, &brush.font, FONT);
   1.347  
   1.348  	wa.override_redirect = 1;
   1.349  	wa.background_pixmap = ParentRelative;
   1.350  	wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask
   1.351  		| SubstructureRedirectMask | SubstructureNotifyMask;
   1.352  
   1.353 -	mrect.width = DisplayWidth(blz.dpy, blz.screen);
   1.354 -	mrect.height = font.ascent + font.descent + 4;
   1.355 -	mrect.y = DisplayHeight(blz.dpy, blz.screen) - mrect.height;
   1.356 -	mrect.x = 0;
   1.357 +	rect.width = DisplayWidth(dpy, screen);
   1.358 +	rect.height = brush.font.height + 4;
   1.359 +	rect.y = DisplayHeight(dpy, screen) - rect.height;
   1.360 +	rect.x = 0;
   1.361  
   1.362 -	win = XCreateWindow(blz.dpy, blz.root, mrect.x, mrect.y,
   1.363 -			mrect.width, mrect.height, 0, DefaultDepth(blz.dpy, blz.screen),
   1.364 -			CopyFromParent, DefaultVisual(blz.dpy, blz.screen),
   1.365 +	win = XCreateWindow(dpy, root, rect.x, rect.y,
   1.366 +			rect.width, rect.height, 0, DefaultDepth(dpy, screen),
   1.367 +			CopyFromParent, DefaultVisual(dpy, screen),
   1.368  			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
   1.369 -	XDefineCursor(blz.dpy, win, XCreateFontCursor(blz.dpy, XC_xterm));
   1.370 -	XSync(blz.dpy, False);
   1.371 +	XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
   1.372 +	XFlush(dpy);
   1.373  
   1.374  	/* pixmap */
   1.375 -	gc = XCreateGC(blz.dpy, win, 0, 0);
   1.376 -	pmap = XCreatePixmap(blz.dpy, win, mrect.width, mrect.height,
   1.377 -			DefaultDepth(blz.dpy, blz.screen));
   1.378 -
   1.379 -	XSync(blz.dpy, False);
   1.380 -
   1.381 -	brush.blitz = &blz;
   1.382 -	brush.color = normcolor;
   1.383 -	brush.drawable = pmap;
   1.384 -	brush.gc = gc;
   1.385 -	brush.font = &font;
   1.386 +	brush.gc = XCreateGC(dpy, win, 0, 0);
   1.387 +	brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height,
   1.388 +			DefaultDepth(dpy, screen));
   1.389 +	XFlush(dpy);
   1.390  
   1.391  	if(maxname)
   1.392 -		cwidth = blitz_textwidth(brush.font, maxname) + mrect.height;
   1.393 -	if(cwidth > mrect.width / 3)
   1.394 -		cwidth = mrect.width / 3;
   1.395 +		cwidth = textwidth(&brush.font, maxname) + brush.font.height;
   1.396 +	if(cwidth > rect.width / 3)
   1.397 +		cwidth = rect.width / 3;
   1.398  
   1.399  	if(title) {
   1.400 -		twidth = blitz_textwidth(brush.font, title) + mrect.height;
   1.401 -		if(twidth > mrect.width / 3)
   1.402 -			twidth = mrect.width / 3;
   1.403 +		twidth = textwidth(&brush.font, title) + brush.font.height;
   1.404 +		if(twidth > rect.width / 3)
   1.405 +			twidth = rect.width / 3;
   1.406  	}
   1.407  
   1.408  	cmdw = title ? twidth : cwidth;
   1.409  
   1.410  	text[0] = 0;
   1.411  	update_items(text);
   1.412 -	XMapRaised(blz.dpy, win);
   1.413 +	XMapRaised(dpy, win);
   1.414  	draw_menu();
   1.415 -	XSync(blz.dpy, False);
   1.416 +	XFlush(dpy);
   1.417  
   1.418  	/* main event loop */
   1.419 -	while(!XNextEvent(blz.dpy, &ev)) {
   1.420 +	while(!XNextEvent(dpy, &ev)) {
   1.421  		switch (ev.type) {
   1.422  			case KeyPress:
   1.423 -				handle_kpress(&ev.xkey);
   1.424 +				kpress(&ev.xkey);
   1.425  				break;
   1.426  			case Expose:
   1.427  				if(ev.xexpose.count == 0) {
   1.428 @@ -488,11 +455,11 @@
   1.429  			break;
   1.430  	}
   1.431  
   1.432 -	XUngrabKeyboard(blz.dpy, CurrentTime);
   1.433 -	XFreePixmap(blz.dpy, pmap);
   1.434 -	XFreeGC(blz.dpy, gc);
   1.435 -	XDestroyWindow(blz.dpy, win);
   1.436 -	XCloseDisplay(blz.dpy);
   1.437 +	XUngrabKeyboard(dpy, CurrentTime);
   1.438 +	XFreePixmap(dpy, brush.drawable);
   1.439 +	XFreeGC(dpy, brush.gc);
   1.440 +	XDestroyWindow(dpy, win);
   1.441 +	XCloseDisplay(dpy);
   1.442  
   1.443  	return ret;
   1.444  }