Commit dfd84f9

Anselm R. Garbe <garbeam@wmii.de>
2006-07-12 09:17:22
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
1 parent 7696c89
bar.c
@@ -8,23 +8,22 @@
 void
 draw_bar()
 {
-	brush.rect = barrect;
-	brush.rect.x = brush.rect.y = 0;
+	brush.x = brush.y = 0;
+	brush.w = bw;
+	brush.h = bh;
 	draw(dpy, &brush, False, NULL);
 
 	if(stack) {
-		brush.rect.width = textwidth(&brush.font, stack->name) + labelheight(&brush.font);
+		brush.w = textw(&brush.font, stack->name) + bh;
 		swap((void **)&brush.fg, (void **)&brush.bg);
 		draw(dpy, &brush, True, stack->name);
 		swap((void **)&brush.fg, (void **)&brush.bg);
-		brush.rect.x += brush.rect.width;
+		brush.x += brush.w;
 	}
 
-	brush.rect.width = textwidth(&brush.font, statustext) + labelheight(&brush.font);
-	brush.rect.x = barrect.x + barrect.width - brush.rect.width;
+	brush.w = textw(&brush.font, statustext) + bh;
+	brush.x = bx + bw - brush.w;
 	draw(dpy, &brush, False, statustext);
-
-	XCopyArea(dpy, brush.drawable, barwin, brush.gc, 0, 0, barrect.width,
-			barrect.height, 0, 0);
+	XCopyArea(dpy, brush.drawable, barwin, brush.gc, 0, 0, bw, bh, 0, 0);
 	XFlush(dpy);
 }
client.c
@@ -10,7 +10,16 @@
 #include "util.h"
 #include "wm.h"
 
-#define CLIENT_MASK		(StructureNotifyMask | PropertyChangeMask | EnterWindowMask)
+static void
+resize_title(Client *c)
+{
+	c->tw = textw(&brush.font, c->name) + bh;
+	if(c->tw > c->w)
+		c->tw = c->w + 2;
+	c->tx = c->x + c->w - c->tw + 2;
+	c->ty = c->y;
+	XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
+}
 
 void
 update_name(Client *c)
@@ -37,6 +46,7 @@ update_name(Client *c)
 		}
 	}
 	XFree(name.value);
+	resize_title(c);
 }
 
 void
@@ -73,27 +83,38 @@ update_size(Client *c)
 		c->minw = c->minh = 0;
 }
 
+void
+raise(Client *c)
+{
+	XRaiseWindow(dpy, c->win);
+	XRaiseWindow(dpy, c->title);
+}
+
+void
+lower(Client *c)
+{
+	XLowerWindow(dpy, c->title);
+	XLowerWindow(dpy, c->win);
+}
+
 void
 focus(Client *c)
 {
 	Client **l, *old;
 
 	old = stack;
-	for(l=&stack; *l && *l != c; l=&(*l)->snext);
+	for(l = &stack; *l && *l != c; l = &(*l)->snext);
 	eassert(*l == c);
 	*l = c->snext;
 	c->snext = stack;
 	stack = c;
-	XRaiseWindow(dpy, c->win);
-	XRaiseWindow(dpy, c->title);
-	XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
 	if(old && old != c) {
 		XMapWindow(dpy, old->title);
 		draw_client(old);
 	}
 	XUnmapWindow(dpy, c->title);
-	draw_bar();
-	discard_events(EnterWindowMask);
+	draw_client(old);
+	XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
 	XFlush(dpy);
 }
 
@@ -107,13 +128,16 @@ manage(Window w, XWindowAttributes *wa)
 	c->win = w;
 	c->tx = c->x = wa->x;
 	c->ty = c->y = wa->y;
+	if(c->y < bh)
+		c->ty = c->y += bh;
 	c->tw = c->w = wa->width;
 	c->h = wa->height;
-	c->th = barrect.height;
+	c->th = bh;
 	update_size(c);
 	XSetWindowBorderWidth(dpy, c->win, 1);
 	XSetWindowBorder(dpy, c->win, brush.border);
-	XSelectInput(dpy, c->win, CLIENT_MASK);
+	XSelectInput(dpy, c->win,
+			StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
 	XGetTransientForHint(dpy, c->win, &c->trans);
 	twa.override_redirect = 1;
 	twa.background_pixmap = ParentRelative;
@@ -130,8 +154,8 @@ manage(Window w, XWindowAttributes *wa)
 	*l = c;
 	c->snext = stack;
 	stack = c;
-	XMapWindow(dpy, c->win);
-	XMapWindow(dpy, c->title);
+	XMapRaised(dpy, c->win);
+	XMapRaised(dpy, c->title);
 	XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
 			GrabModeAsync, GrabModeSync, None, None);
 	XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
@@ -147,6 +171,7 @@ resize(Client *c)
 {
 	XConfigureEvent e;
 
+	resize_title(c);
 	XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
 	e.type = ConfigureNotify;
 	e.event = c->win;
@@ -158,9 +183,7 @@ resize(Client *c)
 	e.border_width = 0;
 	e.above = None;
 	e.override_redirect = False;
-	XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
 	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
-	XSelectInput(dpy, c->win, CLIENT_MASK);
 	XFlush(dpy);
 }
 
@@ -219,17 +242,14 @@ getclient(Window w)
 void
 draw_client(Client *c)
 {
-	if(c == stack)
+	if(c == stack) {
 		draw_bar();
+		return;
+	}
 
-	c->tw = textwidth(&brush.font, c->name) + labelheight(&brush.font);
-	c->tx = c->x + c->w - c->tw + 2;
-	c->ty = c->y;
-	XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
-
-	brush.rect.x = brush.rect.y = 0;
-	brush.rect.width = c->tw;
-	brush.rect.height = c->th;
+	brush.x = brush.y = 0;
+	brush.w = c->tw;
+	brush.h = c->th;
 
 	draw(dpy, &brush, True, c->name);
 	XCopyArea(dpy, brush.drawable, c->title, brush.gc,
cmd.c
@@ -23,16 +23,18 @@ void
 sel(void *aux)
 {
 	const char *arg = aux;
-	Client *c;
+	Client *c = NULL;
 
 	if(!arg || !stack)
 		return;
 	if(!strncmp(arg, "next", 5))
-		focus(stack->snext ? stack->snext : stack);
-	else if(!strncmp(arg, "prev", 5)) {
+		c = stack->snext ? stack->snext : stack;
+	else if(!strncmp(arg, "prev", 5))
 		for(c = stack; c && c->snext; c = c->snext);
-		focus(c ? c : stack);
-	}
+	if(!c)
+		c = stack;
+	raise(c);
+	focus(c);
 }
 
 void
config.h
@@ -4,7 +4,7 @@
  */
 
 #define FONT		"-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*"
-#define BGCOLOR		"#0c0c33"
-#define FGCOLOR		"#b2c8cd"
-#define BORDERCOLOR	"#3030c0"
-#define STATUSDELAY	10 /* milliseconds */
+#define BGCOLOR		"#666699"
+#define FGCOLOR		"#ffffff"
+#define BORDERCOLOR	"#9999CC"
+#define STATUSDELAY	10 /* seconds */
draw.c
@@ -15,16 +15,16 @@ drawborder(Display *dpy, Brush *b)
 	XPoint points[5];
 	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
 	XSetForeground(dpy, b->gc, b->border);
-	points[0].x = b->rect.x;
-	points[0].y = b->rect.y;
-	points[1].x = b->rect.width - 1;
+	points[0].x = b->x;
+	points[0].y = b->y;
+	points[1].x = b->w - 1;
 	points[1].y = 0;
 	points[2].x = 0;
-	points[2].y = b->rect.height - 1;
-	points[3].x = -(b->rect.width - 1);
+	points[2].y = b->h - 1;
+	points[3].x = -(b->w - 1);
 	points[3].y = 0;
 	points[4].x = 0;
-	points[4].y = -(b->rect.height - 1);
+	points[4].y = -(b->h - 1);
 	XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
 }
 
@@ -34,9 +34,10 @@ draw(Display *dpy, Brush *b, Bool border, const char *text)
 	unsigned int x, y, w, h, len;
 	static char buf[256];
 	XGCValues gcv;
+	XRectangle r = { b->x, b->y, b->w, b->h };
 
 	XSetForeground(dpy, b->gc, b->bg);
-	XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
+	XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
 
 	if(border)
 		drawborder(dpy, b);
@@ -51,14 +52,14 @@ draw(Display *dpy, Brush *b, Bool border, const char *text)
 	buf[len] = 0;
 
 	h = b->font.ascent + b->font.descent;
-	y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent;
-	x = b->rect.x + (h / 2);
+	y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
+	x = b->x + (h / 2);
 
 	/* shorten text if necessary */
-	while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h)
+	while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
 		buf[--len] = 0;
 
-	if(w > b->rect.width)
+	if(w > b->w)
 		return; /* too long */
 
 	gcv.foreground = b->fg;
@@ -94,20 +95,26 @@ loadcolors(Display *dpy, int screen, Brush *b,
 }
 
 unsigned int
-textwidth_l(Fnt *font, char *text, unsigned int len)
+textnw(Fnt *font, char *text, unsigned int len)
 {
+	XRectangle r;
 	if(font->set) {
-		XRectangle r;
-		XmbTextExtents(font->set, text, len, 0, &r);
+		XmbTextExtents(font->set, text, len, NULL, &r);
 		return r.width;
 	}
 	return XTextWidth(font->xfont, text, len);
 }
 
 unsigned int
-textwidth(Fnt *font, char *text)
+textw(Fnt *font, char *text)
 {
-	return textwidth_l(font, text, strlen(text));
+	return textnw(font, text, strlen(text));
+}
+
+unsigned int
+texth(Fnt *font)
+{
+	return font->height + 4;
 }
 
 void
@@ -162,9 +169,3 @@ loadfont(Display *dpy, Fnt *font, const char *fontstr)
 	}
 	font->height = font->ascent + font->descent;
 }
-
-unsigned int
-labelheight(Fnt *font)
-{
-	return font->height + 4;
-}
draw.h
@@ -20,7 +20,7 @@ struct Fnt {
 struct Brush {
 	GC gc;
 	Drawable drawable;
-	XRectangle rect;
+	int x, y, w, h;
 	Fnt font;
 	unsigned long bg;
 	unsigned long fg;
@@ -31,6 +31,6 @@ extern void draw(Display *dpy, Brush *b, Bool border, const char *text);
 extern void loadcolors(Display *dpy, int screen, Brush *b,
 		const char *bg, const char *fg, const char *bo);
 extern void loadfont(Display *dpy, Fnt *font, const char *fontstr);
-extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len);
-extern unsigned int textwidth(Fnt *font, char *text);
-extern unsigned int labelheight(Fnt *font);
+extern unsigned int textnw(Fnt *font, char *text, unsigned int len);
+extern unsigned int textw(Fnt *font, char *text);
+extern unsigned int texth(Fnt *font);
event.c
@@ -37,13 +37,11 @@ void (*handler[LASTEvent]) (XEvent *) = {
 	[UnmapNotify] = unmapnotify
 };
 
-unsigned int
+void
 discard_events(long even_mask)
 {
 	XEvent ev;
-	unsigned int n = 0;
-	while(XCheckMaskEvent(dpy, even_mask, &ev)) n++;
-	return n;
+	while(XCheckMaskEvent(dpy, even_mask, &ev));
 }
 
 static void
@@ -53,6 +51,7 @@ buttonpress(XEvent *e)
 	Client *c;
 
 	if((c = getclient(ev->window))) {
+		raise(c);
 		switch(ev->button) {
 		default:
 			break;
@@ -60,7 +59,7 @@ buttonpress(XEvent *e)
 			mmove(c);
 			break;
 		case Button2:
-			XLowerWindow(dpy, c->win);
+			lower(c);
 			break;
 		case Button3:
 			mresize(c);
@@ -122,10 +121,8 @@ enternotify(XEvent *e)
 
 	if((c = getclient(ev->window)))
 		focus(c);
-	else if(ev->window == root) {
+	else if(ev->window == root)
 		sel_screen = True;
-		/*draw_frames();*/
-	}
 }
 
 static void
@@ -133,10 +130,8 @@ leavenotify(XEvent *e)
 {
 	XCrossingEvent *ev = &e->xcrossing;
 
-	if((ev->window == root) && !ev->same_screen) {
+	if((ev->window == root) && !ev->same_screen)
 		sel_screen = True;
-		/*draw_frames();*/
-	}
 }
 
 static void
font.c
@@ -1,81 +0,0 @@
-/*
- * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
- * See LICENSE file for license details.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <locale.h>
-
-unsigned int
-textwidth_l(BlitzFont *font, char *text, unsigned int len)
-{
-	if(font->set) {
-		XRectangle r;
-		XmbTextExtents(font->set, text, len, nil, &r);
-		return r.width;
-	}
-	return XTextWidth(font->xfont, text, len);
-}
-
-unsigned int
-textwidth(BlitzFont *font, char *text)
-{
-	return blitz_textwidth_l(font, text, strlen(text));
-}
-
-void
-loadfont(Blitz *blitz, BlitzFont *font)
-{
-	char *fontname = font->fontstr;
-	char **missing = nil, *def = "?";
-	int n;
-
-	setlocale(LC_ALL, "");
-	if(font->set)
-		XFreeFontSet(blitz->dpy, font->set);
-	font->set = XCreateFontSet(blitz->dpy, fontname, &missing, &n, &def);
-	if(missing) {
-		while(n--)
-			fprintf(stderr, "missing fontset: %s\n", missing[n]);
-		XFreeStringList(missing);
-		if(font->set) {
-			XFreeFontSet(blitz->dpy, font->set);
-			font->set = nil;
-		}
-	}
-	if(font->set) {
-		XFontSetExtents *font_extents;
-		XFontStruct **xfonts;
-		char **font_names;
-		unsigned int i;
-
-		font->ascent = font->descent = 0;
-		font_extents = XExtentsOfFontSet(font->set);
-		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
-		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
-			if(font->ascent < (*xfonts)->ascent)
-				font->ascent = (*xfonts)->ascent;
-			if(font->descent < (*xfonts)->descent)
-				font->descent = (*xfonts)->descent;
-			xfonts++;
-		}
-	}
-	else {
-		if(font->xfont)
-			XFreeFont(blitz->dpy, font->xfont);
-		font->xfont = nil;
-		font->xfont = XLoadQueryFont(blitz->dpy, fontname);
-		if (!font->xfont) {
-			fontname = "fixed";
-			font->xfont = XLoadQueryFont(blitz->dpy, fontname);
-		}
-		if (!font->xfont) {
-			fprintf(stderr, "%s", "error, cannot load 'fixed' font\n");
-			exit(1);
-		}
-		font->ascent = font->xfont->ascent;
-		font->descent = font->xfont->descent;
-	}
-}
mouse.c
@@ -45,21 +45,21 @@ mresize(Client *c)
 	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
 				None, cursor[CurResize], CurrentTime) != GrabSuccess)
 		return;
-	XGrabServer(dpy);
 	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
 	for(;;) {
-		XMaskEvent(dpy, MouseMask, &ev);
+		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
 		switch(ev.type) {
 		default: break;
+		case Expose:
+			handler[Expose](&ev);
+			break;
 		case MotionNotify:
-			XUngrabServer(dpy);
+			XFlush(dpy);
 			mmatch(c, old_cx, old_cy, ev.xmotion.x, ev.xmotion.y);
 			XResizeWindow(dpy, c->win, c->w, c->h);
-			XGrabServer(dpy);
 			break;
 		case ButtonRelease:
 			resize(c);
-			XUngrabServer(dpy);
 			XUngrabPointer(dpy, CurrentTime);
 			return;
 		}
@@ -80,21 +80,21 @@ mmove(Client *c)
 				None, cursor[CurMove], CurrentTime) != GrabSuccess)
 		return;
 	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
-	XGrabServer(dpy);
 	for(;;) {
-		XMaskEvent(dpy, MouseMask, &ev);
+		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
 		switch (ev.type) {
 		default: break;
+		case Expose:
+			handler[Expose](&ev);
+			break;
 		case MotionNotify:
-			XUngrabServer(dpy);
+			XFlush(dpy);
 			c->x = old_cx + (ev.xmotion.x - x1);
 			c->y = old_cy + (ev.xmotion.y - y1);
 			XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
-			XGrabServer(dpy);
 			break;
 		case ButtonRelease:
 			resize(c);
-			XUngrabServer(dpy);
 			XUngrabPointer(dpy, CurrentTime);
 			return;
 		}
wm.c
@@ -23,12 +23,11 @@ Display *dpy;
 Window root, barwin;
 Atom wm_atom[WMLast], net_atom[NetLast];
 Cursor cursor[CurLast];
-XRectangle rect, barrect;
 Bool running = True;
 Bool sel_screen;
 
 char statustext[1024], tag[256];
-int screen;
+int screen, sx, sy, sw, sh, bx, by, bw, bh;
 
 Brush brush = {0};
 Client *clients = NULL;
@@ -39,7 +38,7 @@ static const char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R.
 static int (*x_error_handler) (Display *, XErrorEvent *);
 
 static const char *status[] = {
-	"sh", "-c", "echo -n `date '+%Y/%m/%d %H:%M'`" 
+	"sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`" 
 	" `uptime | sed 's/.*://; s/,//g'`"
 	" `acpi | awk '{print $4}' | sed 's/,//'`", 0
 };
@@ -220,9 +219,9 @@ main(int argc, char *argv[])
 	if(other_wm_running)
 		error("gridwm: another window manager is already running\n");
 
-	rect.x = rect.y = 0;
-	rect.width = DisplayWidth(dpy, screen);
-	rect.height = DisplayHeight(dpy, screen);
+	sx = sy = 0;
+	sw = DisplayWidth(dpy, screen);
+	sh = DisplayHeight(dpy, screen);
 	sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
 
 	XSetErrorHandler(0);
@@ -253,18 +252,16 @@ main(int argc, char *argv[])
 	wa.background_pixmap = ParentRelative;
 	wa.event_mask = ExposureMask;
 
-	barrect = rect;
-	barrect.height = labelheight(&brush.font);
-	barrect.y = rect.height - barrect.height;
-	barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
-			barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
+	bx = by = 0;
+	bw = sw;
+	bh = texth(&brush.font);
+	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
 			CopyFromParent, DefaultVisual(dpy, screen),
 			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 	XDefineCursor(dpy, barwin, cursor[CurNormal]);
 	XMapRaised(dpy, barwin);
 
-	brush.drawable = XCreatePixmap(dpy, root, rect.width, barrect.height,
-			DefaultDepth(dpy, screen));
+	brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
 	brush.gc = XCreateGC(dpy, root, 0, 0);
 
 	pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
wm.h
@@ -46,11 +46,10 @@ extern Display *dpy;
 extern Window root, barwin;
 extern Atom wm_atom[WMLast], net_atom[NetLast];
 extern Cursor cursor[CurLast];
-extern XRectangle rect, barrect;
 extern Bool running, sel_screen, grid;
 extern void (*handler[LASTEvent]) (XEvent *);
 
-extern int screen;
+extern int screen, sx, sy, sw, sh, bx, by, bw, bh;
 extern char statustext[1024], tag[256];
 
 extern Brush brush;
@@ -75,9 +74,11 @@ extern void draw_client(Client *c);
 extern void resize(Client *c);
 extern void update_size(Client *c);
 extern Client *gettitle(Window w);
+extern void raise(Client *c);
+extern void lower(Client *c);
 
 /* event.c */
-extern unsigned int discard_events(long even_mask);
+extern void discard_events(long even_mask);
 
 /* key.c */
 extern void update_keys();