formatting changes; attempt at keyboard-based window switching
diff --git a/src/cmd/rio/client.c b/src/cmd/rio/client.c
index 077106b..02db0e9 100644
--- a/src/cmd/rio/client.c
+++ b/src/cmd/rio/client.c
@@ -15,18 +15,19 @@
 void
 setactive(Client *c, int on)
 {
-	if (c->parent == c->screen->root) {
-		fprintf(stderr, "rio: bad parent in setactive; dumping core\n");
-		abort();
-	}
-	if (on) {
+//	dbg("setactive client %x %d", c->window, c->on);
+
+	if(c->parent == c->screen->root)
+		return;
+	
+	if(on){
 		XUngrabButton(dpy, AnyButton, AnyModifier, c->parent);
 		XSetInputFocus(dpy, c->window, RevertToPointerRoot, timestamp());
-		if (c->proto & Ptakefocus)
+		if(c->proto & Ptakefocus)
 			sendcmessage(c->window, wm_protocols, wm_take_focus, 0, 1);
 		cmapfocus(c);
-	} else {
-		if (c->proto & Plosefocus)
+	}else{
+		if(c->proto & Plosefocus)
 			sendcmessage(c->window, wm_protocols, wm_lose_focus, 0, 1);
 		XGrabButton(dpy, AnyButton, AnyModifier, c->parent, False,
 			ButtonMask, GrabModeAsync, GrabModeSync, None, None);
@@ -51,7 +52,7 @@
 			pixel = c->screen->inactiveborder;
 	}
 
-	if (debug) fprintf(stderr, "draw_border %p pixel %ld active %d hold %d\n", c, pixel, active, c->hold);
+	if(debug) fprintf(stderr, "draw_border %p pixel %ld active %d hold %d\n", c, pixel, active, c->hold);
 	XSetWindowBackground(dpy, c->parent, pixel);
 	XClearWindow(dpy, c->parent);
 }
@@ -61,27 +62,27 @@
 {
 	Client *cc;
 
-	if (c == 0) {
+	if(c == 0){
 		fprintf(stderr, "rio: active(c==0)\n");
 		return;
 	}
-	if (c == current)
+	if(c == current)
 		return;
-	if (current) {
+	if(current){
 		setactive(current, 0);
-		if (current->screen != c->screen)
+		if(current->screen != c->screen)
 			cmapnofocus(current->screen);
 	}
 	setactive(c, 1);
-	for (cc = clients; cc; cc = cc->next)
-		if (cc->revert == c)
+	for(cc = clients; cc; cc = cc->next)
+		if(cc->revert == c)
 			cc->revert = c->revert;
 	c->revert = current;
-	while (c->revert && !normal(c->revert))
+	while(c->revert && !normal(c->revert))
 		c->revert = c->revert->revert;
 	current = c;
 #ifdef	DEBUG
-	if (debug)
+	if(debug)
 		dump_revert();
 #endif
 }
@@ -94,10 +95,10 @@
 	XSetWindowAttributes attr;
 	Client *c;
 
-	if (current) {
+	if(current){
 		setactive(current, 0);
-		for (c = current->revert; c; c = c->revert)
-			if (normal(c)) {
+		for(c = current->revert; c; c = c->revert)
+			if(normal(c)){
 				active(c);
 				return;
 			}
@@ -105,7 +106,7 @@
 		/* if no candidates to revert to, fall through */
 	}
 	current = 0;
-	if (w == 0) {
+	if(w == 0){
 		mask = CWOverrideRedirect/*|CWColormap*/;
 		attr.override_redirect = 1;
 		/* attr.colormap = screens[0].def_cmap;*/
@@ -122,8 +123,8 @@
 	Client **l, *cc;
 
 	l = &clients;
-	for (cc = *l; cc; cc = *l) {
-		if (cc == c) {
+	for(cc = *l; cc; cc = *l){
+		if(cc == c){
 			*l = c->next;
 			c->next = clients;
 			clients = c;
@@ -139,14 +140,14 @@
 {
 	Client *c;
 
-	if (w == 0 || getscreen(w))
+	if(w == 0 || getscreen(w))
 		return 0;
 
-	for (c = clients; c; c = c->next)
-		if (c->window == w || c->parent == w)
+	for(c = clients; c; c = c->next)
+		if(c->window == w || c->parent == w)
 			return c;
 
-	if (!create)
+	if(!create)
 		return 0;
 
 	c = (Client *)malloc(sizeof(Client));
@@ -176,44 +177,44 @@
 {
 	Client *cc;
 
-	for (cc = current; cc && cc->revert; cc = cc->revert)
-		if (cc->revert == c)
+	for(cc = current; cc && cc->revert; cc = cc->revert)
+		if(cc->revert == c)
 			cc->revert = cc->revert->revert;
 
-	if (c == clients)
+	if(c == clients)
 		clients = c->next;
-	for (cc = clients; cc && cc->next; cc = cc->next)
-		if (cc->next == c)
+	for(cc = clients; cc && cc->next; cc = cc->next)
+		if(cc->next == c)
 			cc->next = cc->next->next;
 
-	if (hidden(c))
+	if(hidden(c))
 		unhidec(c, 0);
 
-	if (c->parent != c->screen->root)
+	if(c->parent != c->screen->root)
 		XDestroyWindow(dpy, c->parent);
 
 	c->parent = c->window = None;		/* paranoia */
-	if (current == c) {
+	if(current == c){
 		current = c->revert;
-		if (current == 0)
+		if(current == 0)
 			nofocus();
 		else {
-			if (current->screen != c->screen)
+			if(current->screen != c->screen)
 				cmapnofocus(c->screen);
 			setactive(current, 1);
 		}
 	}
-	if (c->ncmapwins != 0) {
+	if(c->ncmapwins != 0){
 		XFree((char *)c->cmapwins);
 		free((char *)c->wmcmaps);
 	}
-	if (c->iconname != 0)
+	if(c->iconname != 0)
 		XFree((char*) c->iconname);
-	if (c->name != 0)
+	if(c->name != 0)
 		XFree((char*) c->name);
-	if (c->instance != 0)
+	if(c->instance != 0)
 		XFree((char*) c->instance);
-	if (c->class != 0)
+	if(c->class != 0)
 		XFree((char*) c->class);
 	memset(c, 0, sizeof(Client));		/* paranoia */
 	free(c);
@@ -227,14 +228,14 @@
 	int i;
 
 	i = 0;
-	for (c = current; c; c = c->revert) {
+	for(c = current; c; c = c->revert){
 		fprintf(stderr, "%s(%x:%d)", c->label ? c->label : "?", c->window, c->state);
-		if (i++ > 100)
+		if(i++ > 100)
 			break;
-		if (c->revert)
+		if(c->revert)
 			fprintf(stderr, " -> ");
 	}
-	if (current == 0)
+	if(current == 0)
 		fprintf(stderr, "empty");
 	fprintf(stderr, "\n");
 }
@@ -244,7 +245,41 @@
 {
 	Client *c;
 
-	for (c = clients; c; c = c->next)
+	for(c = clients; c; c = c->next)
 		fprintf(stderr, "w 0x%x parent 0x%x @ (%d, %d)\n", c->window, c->parent, c->x, c->y);
 }
 #endif
+
+void
+shuffle(int up)
+{
+	Client **l, *c;
+	
+	if(clients == 0 || clients->next == 0)
+		return;
+	if(up){
+		//for(c=clients; c->next; c=c->next)
+		//	;
+		for(l=&clients; (*l)->next; l=&(*l)->next)
+			;
+		c = *l;
+		*l = nil;
+		c->next = clients;
+		clients = c;
+		XMapRaised(dpy, c->parent);
+		top(c);
+		active(c);
+	}else{
+		c = clients;
+		for(l=&clients; *l; l=&(*l)->next)
+			;
+		clients = c->next;
+		*l = c;
+		c->next = 0;
+		XLowerWindow(dpy, c->window);
+	}
+//	XMapRaised(dpy, clients->parent);
+//	top(clients);	
+//	active(clients);
+}
+
diff --git a/src/cmd/rio/color.c b/src/cmd/rio/color.c
index d585472..f1fff54 100644
--- a/src/cmd/rio/color.c
+++ b/src/cmd/rio/color.c
@@ -37,7 +37,7 @@
 	case 24:
 	case 32:
 		/* try to find byte order */
-		if (s->vis->red_mask & 0xff)
+		if(s->vis->red_mask & 0xff)
 			return (r) | (g<<8) | (b<<16); /* OK on Sun */
 		return rgb;
 	}
diff --git a/src/cmd/rio/cursor.c b/src/cmd/rio/cursor.c
index f1991ba..9acab51 100644
--- a/src/cmd/rio/cursor.c
+++ b/src/cmd/rio/cursor.c
@@ -182,7 +182,7 @@
 	XAllocNamedColor(dpy, DefaultColormap(dpy, s->num),
 			"white", &wh, &dummy);
 
-	if (nostalgia) {
+	if(nostalgia){
 		s->arrow = getcursor(&blitarrow, s);
 		s->target = getcursor(&blittarget, s);
 		s->sweep0 = getcursor(&blitsweep, s);
diff --git a/src/cmd/rio/error.c b/src/cmd/rio/error.c
index d4751ef..1aeb7cd 100644
--- a/src/cmd/rio/error.c
+++ b/src/cmd/rio/error.c
@@ -24,23 +24,23 @@
 {
 	char msg[80], req[80], number[80];
 
-	if (initting && (e->request_code == X_ChangeWindowAttributes) && (e->error_code == BadAccess)) {
+	if(initting && (e->request_code == X_ChangeWindowAttributes) && (e->error_code == BadAccess)){
 		fprintf(stderr, "rio: it looks like there's already a window manager running;  rio not started\n");
 		exit(1);
 	}
 
-	if (ignore_badwindow && (e->error_code == BadWindow || e->error_code == BadColor))
+	if(ignore_badwindow && (e->error_code == BadWindow || e->error_code == BadColor))
 		return 0;
 
 	XGetErrorText(d, e->error_code, msg, sizeof(msg));
 	sprintf(number, "%d", e->request_code);
 	XGetErrorDatabaseText(d, "XRequest", number, "", req, sizeof(req));
-	if (req[0] == '\0')
+	if(req[0] == '\0')
 		sprintf(req, "<request-code-%d>", (int)e->request_code);
 
 	fprintf(stderr, "rio: %s(0x%x): %s\n", req, (int)e->resourceid, msg);
 
-	if (initting) {
+	if(initting){
 		fprintf(stderr, "rio: failure during initialisation; aborting\n");
 		exit(1);
 	}
@@ -53,7 +53,7 @@
 #ifdef	DEBUG	/* sick of "bug" reports; grab errors "just happen" */
 	char *s;
 
-	switch (err) {
+	switch (err){
 	case GrabNotViewable:
 		s = "not viewable";
 		break;
@@ -86,10 +86,10 @@
 dotrace(char *s, Client *c, XEvent *e)
 {
 	fprintf(stderr, "rio: %s: c=0x%x", s, c);
-	if (c)
+	if(c)
 		fprintf(stderr, " x %d y %d dx %d dy %d w 0x%x parent 0x%x", c->x, c->y, c->dx, c->dy, c->window, c->parent);
 #ifdef	DEBUG_EV
-	if (e) {
+	if(e){
 		fprintf(stderr, "\n\t");
 		ShowEvent(e);
 	}
diff --git a/src/cmd/rio/event.c b/src/cmd/rio/event.c
index 2e72889..e4f3f69 100644
--- a/src/cmd/rio/event.c
+++ b/src/cmd/rio/event.c
@@ -15,25 +15,33 @@
 mainloop(int shape_event)
 {
 	XEvent ev;
-
-	for (;;) {
+	XKeyEvent *ke;
+	
+Top:
+	for(;;){
 		getevent(&ev);
 
 #ifdef	DEBUG_EV
-		if (debug) {
+		if(debug){
 			ShowEvent(&ev);
 			printf("\n");
 		}
 #endif
-		switch (ev.type) {
+		switch (ev.type){
 		default:
 #ifdef	SHAPE
-			if (shape && ev.type == shape_event)
+			if(shape && ev.type == shape_event)
 				shapenotify((XShapeEvent *)&ev);
 			else
 #endif
 				fprintf(stderr, "rio: unknown ev.type %d\n", ev.type);
 			break;
+		case KeyPress:
+			keypress(&ev.xkey);
+			break;
+		case KeyRelease:
+			keyrelease(&ev.xkey);
+			break;
 		case ButtonPress:
 			button(&ev.xbutton);
 			break;
@@ -117,26 +125,26 @@
 
 	e->value_mask &= ~CWSibling;
 
-	if (c) {
+	if(c){
 		gravitate(c, 1);
-		if (e->value_mask & CWX)
+		if(e->value_mask & CWX)
 			c->x = e->x;
-		if (e->value_mask & CWY)
+		if(e->value_mask & CWY)
 			c->y = e->y;
-		if (e->value_mask & CWWidth)
+		if(e->value_mask & CWWidth)
 			c->dx = e->width;
-		if (e->value_mask & CWHeight)
+		if(e->value_mask & CWHeight)
 			c->dy = e->height;
-		if (e->value_mask & CWBorderWidth)
+		if(e->value_mask & CWBorderWidth)
 			c->border = e->border_width;
 		gravitate(c, 0);
-		if (e->value_mask & CWStackMode) {
-			if (e->detail == Above)
+		if(e->value_mask & CWStackMode){
+			if(e->detail == Above)
 				top(c);
 			else
 				e->value_mask &= ~CWStackMode;
 		}
-		if (c->parent != c->screen->root && c->window == e->window) {
+		if(c->parent != c->screen->root && c->window == e->window){
 			wc.x = c->x-BORDER;
 			wc.y = c->y-BORDER;
 			wc.width = c->dx+2*BORDER;
@@ -146,14 +154,14 @@
 			wc.stack_mode = e->detail;
 			XConfigureWindow(dpy, c->parent, e->value_mask, &wc);
 			sendconfig(c);
-			if (e->value_mask & CWStackMode) {
+			if(e->value_mask & CWStackMode){
 				top(c);
 				active(c);
 			}
 		}
 	}
 
-	if (c && c->init) {
+	if(c && c->init){
 		wc.x = BORDER;
 		wc.y = BORDER;
 	}
@@ -182,23 +190,23 @@
 	c = getclient(e->window, 0);
 	trace("mapreq", c, e);
 
-	if (c == 0 || c->window != e->window) {
+	if(c == 0 || c->window != e->window){
 		/* workaround for stupid NCDware */
 		fprintf(stderr, "rio: bad mapreq c %p w %x, rescanning\n",
 			c, (int)e->window);
-		for (i = 0; i < num_screens; i++)
+		for(i = 0; i < num_screens; i++)
 			scanwins(&screens[i]);
 		c = getclient(e->window, 0);
-		if (c == 0 || c->window != e->window) {
+		if(c == 0 || c->window != e->window){
 			fprintf(stderr, "rio: window not found after rescan\n");
 			return;
 		}
 	}
 
-	switch (c->state) {
+	switch (c->state){
 	case WithdrawnState:
-		if (c->parent == c->screen->root) {
-			if (!manage(c, 0))
+		if(c->parent == c->screen->root){
+			if(!manage(c, 0))
 				return;
 			break;
 		}
@@ -210,7 +218,7 @@
 		XMapRaised(dpy, c->parent);
 		top(c);
 		setstate(c, NormalState);
-		if (c->trans != None && current && c->trans == current->window)
+		if(c->trans != None && current && c->trans == current->window)
 				active(c);
 		break;
 	case IconicState:
@@ -226,18 +234,18 @@
 
 	curtime = CurrentTime;
 	c = getclient(e->window, 0);
-	if (c) {
-		switch (c->state) {
+	if(c){
+		switch (c->state){
 		case IconicState:
-			if (e->send_event) {
+			if(e->send_event){
 				unhidec(c, 0);
 				withdraw(c);
 			}
 			break;
 		case NormalState:
-			if (c == current)
+			if(c == current)
 				nofocus();
-			if (!c->reparenting)
+			if(!c->reparenting)
 				withdraw(c);
 			break;
 		}
@@ -258,17 +266,17 @@
 	ScreenInfo *s;
 
 	/* we don't set curtime as nothing here uses it */
-	if (e->override_redirect)
+	if(e->override_redirect)
 		return;
 	c = getclient(e->window, 1);
-	if (c && c->window == e->window && (s = getscreen(e->parent))) {
+	if(c && c->window == e->window && (s = getscreen(e->parent))){
 		c->x = e->x;
 		c->y = e->y;
 		c->dx = e->width;
 		c->dy = e->height;
 		c->border = e->border_width;
 		c->screen = s;
-		if (c->parent == None)
+		if(c->parent == None)
 			c->parent = c->screen->root;
 	}
 }
@@ -280,7 +288,7 @@
 
 	curtime = CurrentTime;
 	c = getclient(w, 0);
-	if (c == 0)
+	if(c == 0)
 		return;
 
 	rmclient(c);
@@ -297,21 +305,21 @@
 	Client *c;
 
 	curtime = CurrentTime;
-	if (e->message_type == exit_rio) {
+	if(e->message_type == exit_rio){
 		cleanup();
 		exit(0);
 	}
-	if (e->message_type == restart_rio) {
+	if(e->message_type == restart_rio){
 		fprintf(stderr, "*** rio restarting ***\n");
 		cleanup();
 		execvp(myargv[0], myargv);
 		perror("rio: exec failed");
 		exit(1);
 	}
-	if (e->message_type == wm_change_state) {
+	if(e->message_type == wm_change_state){
 		c = getclient(e->window, 0);
-		if (e->format == 32 && e->data.l[0] == IconicState && c != 0) {
-			if (normal(c))
+		if(e->format == 32 && e->data.l[0] == IconicState && c != 0){
+			if(normal(c))
 				hide(c);
 		}
 		else
@@ -330,19 +338,19 @@
 	int i;
 
 	/* we don't set curtime as nothing here uses it */
-	if (e->new) {
+	if(e->new){
 		c = getclient(e->window, 0);
-		if (c) {
+		if(c){
 			c->cmap = e->colormap;
-			if (c == current)
+			if(c == current)
 				cmapfocus(c);
 		}
 		else
-			for (c = clients; c; c = c->next) {
-				for (i = 0; i < c->ncmapwins; i++)
-					if (c->cmapwins[i] == e->window) {
+			for(c = clients; c; c = c->next){
+				for(i = 0; i < c->ncmapwins; i++)
+					if(c->cmapwins[i] == e->window){
 						c->wmcmaps[i] = e->colormap;
-						if (c == current)
+						if(c == current)
 							cmapfocus(c);
 						return;
 					}
@@ -362,19 +370,19 @@
 	a = e->atom;
 	delete = (e->state == PropertyDelete);
 	c = getclient(e->window, 0);
-	if (c == 0)
+	if(c == 0)
 		return;
 
-	switch (a) {
+	switch (a){
 	case XA_WM_ICON_NAME:
-		if (c->iconname != 0)
+		if(c->iconname != 0)
 			XFree((char*) c->iconname);
 		c->iconname = delete ? 0 : getprop(c->window, a);
 		setlabel(c);
 		renamec(c, c->label);
 		return;
 	case XA_WM_NAME:
-		if (c->name != 0)
+		if(c->name != 0)
 			XFree((char*) c->name);
 		c->name = delete ? 0 : getprop(c->window, a);
 		setlabel(c);
@@ -389,18 +397,18 @@
 		/* placeholders to not forget.  ignore for now.  -Axel */
 		return;
 	case XA_WM_NORMAL_HINTS:
-		if (XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 || c->size.flags == 0)
+		if(XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 || c->size.flags == 0)
 			c->size.flags = PSize;	/* not specified - punt */
 		return;
 	}
-	if (a == _rio_hold_mode) {
+	if(a == _rio_hold_mode){
 		c->hold = getiprop(c->window, _rio_hold_mode);
-		if (c == current)
+		if(c == current)
 			draw_border(c, 1);
 	}
-	else if (a == wm_colormaps) {
+	else if(a == wm_colormaps){
 		getcmaps(c);
-		if (c == current)
+		if(c == current)
 			cmapfocus(c);
 	}
 }
@@ -413,11 +421,11 @@
 	ScreenInfo *s;
 
 	/* we don't set curtime as nothing here uses it */
-	if (!getscreen(e->event) || e->override_redirect)
+	if(!getscreen(e->event) || e->override_redirect)
 		return;
-	if ((s = getscreen(e->parent)) != 0) {
+	if((s = getscreen(e->parent)) != 0){
 		c = getclient(e->window, 1);
-		if (c != 0 && (c->dx == 0 || c->dy == 0)) {
+		if(c != 0 && (c->dx == 0 || c->dy == 0)){
 			/* flush any errors */
 			ignore_badwindow = 1;
 			XGetWindowAttributes(dpy, c->window, &attr);
@@ -430,13 +438,13 @@
 			c->dy = attr.height;
 			c->border = attr.border_width;
 			c->screen = s;
-			if (c->parent == None)
+			if(c->parent == None)
 				c->parent = c->screen->root;
 		}
 	}
 	else {
 		c = getclient(e->window, 0);
-		if (c != 0 && (c->parent == c->screen->root || withdrawn(c)))
+		if(c != 0 && (c->parent == c->screen->root || withdrawn(c)))
 			rmclient(c);
 	}
 }
@@ -449,7 +457,7 @@
 
 	/* we don't set curtime as nothing here uses it */
 	c = getclient(e->window, 0);
-	if (c == 0)
+	if(c == 0)
 		return;
 
 	setshape(c);
@@ -462,10 +470,10 @@
 	Client *c;
 
 	curtime = e->time;
-	if (e->mode != NotifyGrab || e->detail != NotifyNonlinearVirtual)
+	if(e->mode != NotifyGrab || e->detail != NotifyNonlinearVirtual)
 		return;
 	c = getclient(e->window, 0);
-	if (c != 0 && c != current) {
+	if(c != 0 && c != current){
 		/* someone grabbed the pointer; make them current */
 		XMapRaised(dpy, c->parent);
 		top(c);
@@ -479,7 +487,7 @@
 	Client *c;
 
 	c = getclient(e->window, 0);
-	if (c)
+	if(c)
 		XUndefineCursor(dpy, c->parent);
 /* 	XDefineCursor(dpy, c->parent, c->screen->arrow); */
 }
@@ -490,10 +498,10 @@
 	Client *c;
 
 	curtime = CurrentTime;
-	if (e->detail != NotifyNonlinearVirtual)
+	if(e->detail != NotifyNonlinearVirtual)
 		return;
 	c = getclient(e->window, 0);
-	if (c != 0 && c->window == e->window && c != current) {
+	if(c != 0 && c->window == e->window && c != current){
 		/* someone grabbed keyboard or seized focus; make them current */
 		XMapRaised(dpy, c->parent);
 		top(c);
@@ -504,60 +512,60 @@
 BorderOrient
 borderorient(Client *c, int x, int y)
 {
-	if (x <= BORDER) {
-		if (y <= CORNER) {
-			if (debug) fprintf(stderr, "topleft\n");
+	if(x <= BORDER){
+		if(y <= CORNER){
+			if(debug) fprintf(stderr, "topleft\n");
 			return BorderWNW;
 		}
-		if (y >= (c->dy + 2*BORDER) - CORNER) {
-			if (debug) fprintf(stderr, "botleft\n");
+		if(y >= (c->dy + 2*BORDER) - CORNER){
+			if(debug) fprintf(stderr, "botleft\n");
 			return BorderWSW;
 		}
-		if (y > CORNER &&
-			y < (c->dy + 2*BORDER) - CORNER) {
-			if (debug) fprintf(stderr, "left\n");
+		if(y > CORNER &&
+			y < (c->dy + 2*BORDER) - CORNER){
+			if(debug) fprintf(stderr, "left\n");
 			return BorderW;
 		}
-	} else if (x <= CORNER) {
-		if (y <= BORDER) {
-			if (debug) fprintf(stderr, "topleft\n");
+	} else if(x <= CORNER){
+		if(y <= BORDER){
+			if(debug) fprintf(stderr, "topleft\n");
 			return BorderNNW;
 		}
-		if  (y >= (c->dy + BORDER)) {
-			if (debug) fprintf(stderr, "botleft\n");
+		if  (y >= (c->dy + BORDER)){
+			if(debug) fprintf(stderr, "botleft\n");
 			return BorderSSW;
 		}
-	} else if (x >= (c->dx + BORDER)) {
-		if (y <= CORNER) {
-			if (debug) fprintf(stderr, "topright\n");
+	} else if(x >= (c->dx + BORDER)){
+		if(y <= CORNER){
+			if(debug) fprintf(stderr, "topright\n");
 			return BorderENE;
 		}
-		if (y >= (c->dy + 2*BORDER) - CORNER) {
-			if (debug) fprintf(stderr, "botright\n");
+		if(y >= (c->dy + 2*BORDER) - CORNER){
+			if(debug) fprintf(stderr, "botright\n");
 			return BorderESE;
 		}
-		if (y > CORNER &&
-			y < (c->dy + 2*BORDER) - CORNER) {
-			if (debug) fprintf(stderr, "right\n");
+		if(y > CORNER &&
+			y < (c->dy + 2*BORDER) - CORNER){
+			if(debug) fprintf(stderr, "right\n");
 			return BorderE;
 		}
-	} else if (x >= (c->dx + 2*BORDER) - CORNER) {
-		if (y <= BORDER) {
-			if (debug) fprintf(stderr, "topright\n");
+	} else if(x >= (c->dx + 2*BORDER) - CORNER){
+		if(y <= BORDER){
+			if(debug) fprintf(stderr, "topright\n");
 			return BorderNNE;
 		}
-		if  (y >= (c->dy + BORDER)) {
-			if (debug) fprintf(stderr, "botright\n");
+		if  (y >= (c->dy + BORDER)){
+			if(debug) fprintf(stderr, "botright\n");
 			return BorderSSE;
 		}
-	} else if (x > CORNER &&
-			x < (c->dx + 2*BORDER) - CORNER) {
-		if (y <= BORDER) {
-			if (debug) fprintf(stderr, "top\n");
+	} else if(x > CORNER &&
+			x < (c->dx + 2*BORDER) - CORNER){
+		if(y <= BORDER){
+			if(debug) fprintf(stderr, "top\n");
 			return BorderN;
 		}
-		if (y >= (c->dy + BORDER)) {
-			if (debug) fprintf(stderr, "bot\n");
+		if(y >= (c->dy + BORDER)){
+			if(debug) fprintf(stderr, "bot\n");
 			return BorderS;
 		}
 	}
@@ -571,9 +579,9 @@
 	BorderOrient bl;
 
 	c = getclient(e->window, 0);
-	if (c) {
+	if(c){
 		bl = borderorient(c, e->x, e->y);
-		if (bl == BorderUnknown)
+		if(bl == BorderUnknown)
 			XUndefineCursor(dpy, c->parent);
 		else
 			XDefineCursor(dpy, c->parent, c->screen->bordcurs[bl]);
diff --git a/src/cmd/rio/fns.h b/src/cmd/rio/fns.h
index a9693f4..cbe7f23 100644
--- a/src/cmd/rio/fns.h
+++ b/src/cmd/rio/fns.h
@@ -61,6 +61,11 @@
 void	getproto();
 void	gettrans();
 
+/* key.c */
+void keypress();
+void keyrelease();
+void keysetup();
+
 /* menu.c */
 void	button();
 void	spawn();
@@ -88,6 +93,7 @@
 void	rmclient();
 void	dump_revert();
 void	dump_clients();
+void shuffle(int);
 
 /* grab.c */
 int 	menuhit();
diff --git a/src/cmd/rio/grab.c b/src/cmd/rio/grab.c
index 3a1f031..5aff69c 100644
--- a/src/cmd/rio/grab.c
+++ b/src/cmd/rio/grab.c
@@ -21,7 +21,7 @@
 {
 	int status;
 
-	if (t == 0)
+	if(t == 0)
 		t = timestamp();
 	status = XGrabPointer(dpy, w, False, mask,
 		GrabModeAsync, GrabModeAsync, constrain, curs, t);
@@ -33,13 +33,13 @@
 {
 	XEvent ev;
 
-	if (!nobuttons(e))
-		for (;;) {
+	if(!nobuttons(e))
+		for(;;){
 			XMaskEvent(dpy, ButtonMask | ButtonMotionMask, &ev);
-			if (ev.type == MotionNotify)
+			if(ev.type == MotionNotify)
 				continue;
 			e = &ev.xbutton;
-			if (nobuttons(e))
+			if(nobuttons(e))
 				break;
 		}
 	XUngrabPointer(dpy, e->time);
@@ -65,21 +65,21 @@
 	int x, y, dx, dy, xmax, ymax;
 	ScreenInfo *s;
 
-	if (font == 0)
+	if(font == 0)
 		return -1;
 	s = getscreen(e->root);
-	if (s == 0 || e->window == s->menuwin)	   /* ugly event mangling */
+	if(s == 0 || e->window == s->menuwin)	   /* ugly event mangling */
 		return -1;
 
 	dx = 0;
-	for (n = 0; m->item[n]; n++) {
+	for(n = 0; m->item[n]; n++){
 		wide = XTextWidth(font, m->item[n], strlen(m->item[n])) + 4;
-		if (wide > dx)
+		if(wide > dx)
 			dx = wide;
 	}
 	wide = dx;
 	cur = m->lasthit;
-	if (cur >= n)
+	if(cur >= n)
 		cur = n - 1;
 
 	high = font->ascent + font->descent + 1;
@@ -89,88 +89,88 @@
 	warp = 0;
 	xmax = DisplayWidth(dpy, s->num);
 	ymax = DisplayHeight(dpy, s->num);
-	if (x < 0) {
+	if(x < 0){
 		e->x -= x;
 		x = 0;
 		warp++;
 	}
-	if (x+wide >= xmax) {
+	if(x+wide >= xmax){
 		e->x -= x+wide-xmax;
 		x = xmax-wide;
 		warp++;
 	}
-	if (y < 0) {
+	if(y < 0){
 		e->y -= y;
 		y = 0;
 		warp++;
 	}
-	if (y+dy >= ymax) {
+	if(y+dy >= ymax){
 		e->y -= y+dy-ymax;
 		y = ymax-dy;
 		warp++;
 	}
-	if (warp)
+	if(warp)
 		setmouse(e->x, e->y, s);
 	XMoveResizeWindow(dpy, s->menuwin, x, y, dx, dy);
 	XSelectInput(dpy, s->menuwin, MenuMask);
 	XMapRaised(dpy, s->menuwin);
 	status = grab(s->menuwin, None, MenuGrabMask, None, e->time);
-	if (status != GrabSuccess) {
+	if(status != GrabSuccess){
 		/* graberror("menuhit", status); */
 		XUnmapWindow(dpy, s->menuwin);
 		return -1;
 	}
 	drawn = 0;
-	for (;;) {
+	for(;;){
 		XMaskEvent(dpy, MenuMask, &ev);
-		switch (ev.type) {
+		switch (ev.type){
 		default:
 			fprintf(stderr, "rio: menuhit: unknown ev.type %d\n", ev.type);
 			break;
 		case ButtonPress:
 			break;
 		case ButtonRelease:
-			if (ev.xbutton.button != e->button)
+			if(ev.xbutton.button != e->button)
 				break;
 			x = ev.xbutton.x;
 			y = ev.xbutton.y;
 			i = y/high;
-			if (cur >= 0 && y >= cur*high-3 && y < (cur+1)*high+3)
+			if(cur >= 0 && y >= cur*high-3 && y < (cur+1)*high+3)
 				i = cur;
-			if (x < 0 || x > wide || y < -3)
+			if(x < 0 || x > wide || y < -3)
 				i = -1;
-			else if (i < 0 || i >= n)
+			else if(i < 0 || i >= n)
 				i = -1;
 			else
 				m->lasthit = i;
-			if (!nobuttons(&ev.xbutton))
+			if(!nobuttons(&ev.xbutton))
 				i = -1;
 			ungrab(&ev.xbutton);
 			XUnmapWindow(dpy, s->menuwin);
 			return i;
 		case MotionNotify:
-			if (!drawn)
+			if(!drawn)
 				break;
 			x = ev.xbutton.x;
 			y = ev.xbutton.y;
 			old = cur;
 			cur = y/high;
-			if (old >= 0 && y >= old*high-3 && y < (old+1)*high+3)
+			if(old >= 0 && y >= old*high-3 && y < (old+1)*high+3)
 				cur = old;
-			if (x < 0 || x > wide || y < -3)
+			if(x < 0 || x > wide || y < -3)
 				cur = -1;
-			else if (cur < 0 || cur >= n)
+			else if(cur < 0 || cur >= n)
 				cur = -1;
-			if (cur == old)
+			if(cur == old)
 				break;
-			if (old >= 0 && old < n)
+			if(old >= 0 && old < n)
 				drawstring(dpy, s, m, wide, high, old, 0);
-			if (cur >= 0 && cur < n)
+			if(cur >= 0 && cur < n)
 				drawstring(dpy, s, m, wide, high, cur, 1);
 			break;
 		case Expose:
 			XClearWindow(dpy, s->menuwin);
-			for (i = 0; i < n; i++)
+			for(i = 0; i < n; i++)
 				drawstring(dpy, s, m, wide, high, i, cur==i);
 			drawn = 1;
 		}
@@ -187,35 +187,35 @@
 	Client *c;
 
 	status = grab(s->root, s->root, ButtonMask, s->target, 0);
-	if (status != GrabSuccess) {
+	if(status != GrabSuccess){
 		graberror("selectwin", status); /* */
 		return 0;
 	}
 	w = None;
-	for (;;) {
+	for(;;){
 		XMaskEvent(dpy, ButtonMask, &ev);
 		e = &ev.xbutton;
-		switch (ev.type) {
+		switch (ev.type){
 		case ButtonPress:
-			if (e->button != Button3) {
+			if(e->button != Button3){
 				ungrab(e);
 				return 0;
 			}
 			w = e->subwindow;
-			if (!release) {
+			if(!release){
 				c = getclient(w, 0);
-				if (c == 0)
+				if(c == 0)
 					ungrab(e);
-				if (shift != 0)
+				if(shift != 0)
 					*shift = (e->state&ShiftMask) != 0;
 				return c;
 			}
 			break;
 		case ButtonRelease:
 			ungrab(e);
-			if (e->button != Button3 || e->subwindow != w)
+			if(e->button != Button3 || e->subwindow != w)
 				return 0;
-			if (shift != 0)
+			if(shift != 0)
 				*shift = (e->state&ShiftMask) != 0;
 			return getclient(w, 0);
 		}
@@ -231,12 +231,12 @@
 	dy = y - c->y;
 	sx = sy = 1;
 	x += dx;
-	if (dx < 0) {
+	if(dx < 0){
 		dx = -dx;
 		sx = -1;
 	}
 	y += dy;
-	if (dy < 0) {
+	if(dy < 0){
 		dy = -dy;
 		sy = -1;
 	}
@@ -244,22 +244,22 @@
 	dx -= 2*BORDER;
 	dy -= 2*BORDER;
 
-	if (!c->is9term) {
-		if (dx < c->min_dx)
+	if(!c->is9term){
+		if(dx < c->min_dx)
 			dx = c->min_dx;
-		if (dy < c->min_dy)
+		if(dy < c->min_dy)
 			dy = c->min_dy;
 	}
 
-	if (c->size.flags & PResizeInc) {
+	if(c->size.flags & PResizeInc){
 		dx = c->min_dx + (dx-c->min_dx)/c->size.width_inc*c->size.width_inc;
 		dy = c->min_dy + (dy-c->min_dy)/c->size.height_inc*c->size.height_inc;
 	}
 
-	if (c->size.flags & PMaxSize) {
-		if (dx > c->size.max_width)
+	if(c->size.flags & PMaxSize){
+		if(dx > c->size.max_width)
 			dx = c->size.max_width;
-		if (dy > c->size.max_height)
+		if(dy > c->size.max_height)
 			dy = c->size.max_height;
 	}
 	c->dx = sx*(dx + 2*BORDER);
@@ -291,7 +291,7 @@
  	xoff = yoff = 0;
 	xcorn = ycorn = 0;
 
-	switch(bl) {
+	switch(bl){
 	case BorderN:
 		py = y;
 		dy = (c->y + c->dy)  - y;
@@ -351,7 +351,7 @@
 	default:
 		break;
 	}
-	switch(bl) {
+	switch(bl){
 	case BorderNNW:
 	case BorderNNE:
 	case BorderSSW:
@@ -365,21 +365,21 @@
 		ycorn = 1;
 		break;
 	}
-	if (!init
+	if(!init
 		|| xoff < 0 || (xcorn && xoff > CORNER) || (!xcorn && xoff > BORDER)
-		|| yoff < 0 || (ycorn && yoff > CORNER) || (!ycorn && yoff > BORDER)) {
+		|| yoff < 0 || (ycorn && yoff > CORNER) || (!ycorn && yoff > BORDER)){
 		xoff = 0;
 		yoff = 0;
 		init = 0;
 	}
 
-	if (debug) fprintf(stderr, "c %dx%d+%d+%d m +%d+%d r  %dx%d+%d+%d sp (%d,%d) bl %d\n",
+	if(debug) fprintf(stderr, "c %dx%d+%d+%d m +%d+%d r  %dx%d+%d+%d sp (%d,%d) bl %d\n",
 				c->dx, c->dy, c->x, c->y, x, y, dx, dy, px, py, spx, spy, bl);
-	if (dx < 0) {
+	if(dx < 0){
 		dx = -dx;
 		sx = -1;
 	}
-	if (dy < 0) {
+	if(dy < 0){
 		dy = -dy;
 		sy = -1;
 	}
@@ -394,22 +394,22 @@
 	dx -= (2*BORDER - xoff);
 	dy -= (2*BORDER - yoff);
 
-	if (!c->is9term) {
-		if (dx < c->min_dx)
+	if(!c->is9term){
+		if(dx < c->min_dx)
 			dx = c->min_dx;
-		if (dy < c->min_dy)
+		if(dy < c->min_dy)
 			dy = c->min_dy;
 	}
 
-	if (c->size.flags & PResizeInc) {
+	if(c->size.flags & PResizeInc){
 		dx = c->min_dx + (dx-c->min_dx)/c->size.width_inc*c->size.width_inc;
 		dy = c->min_dy + (dy-c->min_dy)/c->size.height_inc*c->size.height_inc;
 	}
 
-	if (c->size.flags & PMaxSize) {
-		if (dx > c->size.max_width)
+	if(c->size.flags & PMaxSize){
+		if(dx > c->size.max_width)
 			dx = c->size.max_width;
-		if (dy > c->size.max_height)
+		if(dy > c->size.max_height)
 			dy = c->size.max_height;
 	}
 
@@ -443,22 +443,22 @@
 	int x, y, dx, dy;
 	ScreenInfo *s;
 
-	if (debug) fprintf(stderr, "drawbound %d %dx%d+%d+%d\n", drawing, c->dx, c->dy, c->x, c->y);
+	if(debug) fprintf(stderr, "drawbound %d %dx%d+%d+%d\n", drawing, c->dx, c->dy, c->x, c->y);
 	
 	s = c->screen;
 	x = c->x;
 	y = c->y;
 	dx = c->dx;
 	dy = c->dy;
-	if (dx < 0) {
+	if(dx < 0){
 		x += dx;
 		dx = -dx;
 	}
-	if (dy < 0) {
+	if(dy < 0){
 		y += dy;
 		dy = -dy;
 	}
-	if (dx <= 2 || dy <= 2)
+	if(dx <= 2 || dy <= 2)
 		return;
 
 	if(solidsweep){
@@ -525,22 +525,22 @@
 	c->y -= BORDER;
 	c->dx += 2*BORDER;
 	c->dy += 2*BORDER;
-	if (bl != BorderUnknown || e0 == 0)
+	if(bl != BorderUnknown || e0 == 0)
 		getmouse(&cx, &cy, c->screen);
 	else
 		getmouse(&c->x, &c->y, c->screen);
 	XGrabServer(dpy);
-	if (bl != BorderUnknown) {
+	if(bl != BorderUnknown){
 		notmoved = recalc(c, cx, cy, bl, notmoved);
 	}
 	drawbound(c, 1);
 	idle = 0;
-	for (;;) {
-		if (XCheckMaskEvent(dpy, ButtonMask, &ev) == 0) {
+	for(;;){
+		if(XCheckMaskEvent(dpy, ButtonMask, &ev) == 0){
 			getmouse(&rx, &ry, c->screen);
-			if (rx != cx || ry != cy || ++idle > 300) {
+			if(rx != cx || ry != cy || ++idle > 300){
 				drawbound(c, 0);
-				if (rx == cx && ry == cy) {
+				if(rx == cx && ry == cy){
 					XUngrabServer(dpy);
 					XFlush(dpy);
 					misleep(500);
@@ -560,19 +560,19 @@
 			continue;
 		}
 		e = &ev.xbutton;
-		switch (ev.type) {
+		switch (ev.type){
 		case ButtonPress:
 		case ButtonRelease:
 			drawbound(c, 0);
 			ungrab(e);
 			XUngrabServer(dpy);
-			if (e->button != but && c->init)
+			if(e->button != but && c->init)
 				goto bad;
-			if (c->dx < 0) {
+			if(c->dx < 0){
 				c->x += c->dx;
 				c->dx = -c->dx;
 			}
-			if (c->dy < 0) {
+			if(c->dy < 0){
 				c->y += c->dy;
 				c->dy = -c->dy;
 			}
@@ -580,13 +580,13 @@
 			c->y += BORDER;
 			c->dx -= 2*BORDER;
 			c->dy -= 2*BORDER;
-			if (c->dx < 4 || c->dy < 4 || c->dx < c->min_dx || c->dy < c->min_dy)
+			if(c->dx < 4 || c->dy < 4 || c->dx < c->min_dx || c->dy < c->min_dy)
 				goto bad;
 			return 1;
 		}
 	}
 bad:
-	if (debug) fprintf(stderr, "sweepdrag bad\n");
+	if(debug) fprintf(stderr, "sweepdrag bad\n");
 	c->x = ox;
 	c->y = oy;
 	c->dx = odx;
@@ -607,14 +607,14 @@
 	c->dx = 0;
 	c->dy = 0;
 	status = grab(s->root, s->root, ButtonMask, s->sweep0, 0);
-	if (status != GrabSuccess) {
+	if(status != GrabSuccess){
 		graberror("sweep", status); /* */
 		return 0;
 	}
 
 	XMaskEvent(dpy, ButtonMask, &ev);
 	e = &ev.xbutton;
-	if (e->button != but) {
+	if(e->button != but){
 		ungrab(e);
 		return 0;
 	}
@@ -634,7 +634,7 @@
 
 	s = c->screen;
 	status = grab(s->root, s->root, ButtonMask, s->bordcurs[bl], 0);
-	if (status != GrabSuccess) {
+	if(status != GrabSuccess){
 		graberror("pull", status); /* */
 		return 0;
 	}
@@ -650,7 +650,7 @@
 
 	s = c->screen;
 	status = grab(s->root, s->root, ButtonMask, s->boxcurs, 0);
-	if (status != GrabSuccess) {
+	if(status != GrabSuccess){
 		graberror("drag", status); /* */
 		return 0;
 	}
@@ -665,7 +665,7 @@
 	unsigned int t3;
 
 	XQueryPointer(dpy, s->root, &dw1, &dw2, x, y, &t1, &t2, &t3);
-	if (debug) fprintf(stderr, "getmouse: %d %d\n", *x, *y);
+	if(debug) fprintf(stderr, "getmouse: %d %d\n", *x, *y);
 }
 
 void
diff --git a/src/cmd/rio/main.c b/src/cmd/rio/main.c
index f8251cd..71f85fb 100644
--- a/src/cmd/rio/main.c
+++ b/src/cmd/rio/main.c
@@ -88,83 +88,83 @@
 	background = 0;
 	font = 0;
 	fname = 0;
-	for (i = 1; i < argc; i++)
-		if (strcmp(argv[i], "-nostalgia") == 0)
+	for(i = 1; i < argc; i++)
+		if(strcmp(argv[i], "-nostalgia") == 0)
 			nostalgia++;
-		else if (strcmp(argv[i], "-grey") == 0)
+		else if(strcmp(argv[i], "-grey") == 0)
 			background = 1;
-		else if (strcmp(argv[i], "-debug") == 0)
+		else if(strcmp(argv[i], "-debug") == 0)
 			debug++;
-		else if (strcmp(argv[i], "-font") == 0 && i+1<argc) {
+		else if(strcmp(argv[i], "-font") == 0 && i+1<argc){
 			i++;
 			fname = argv[i];
 		}
-		else if (strcmp(argv[i], "-term") == 0 && i+1<argc)
+		else if(strcmp(argv[i], "-term") == 0 && i+1<argc)
 			termprog = argv[++i];
-		else if (strcmp(argv[i], "-virtuals") == 0 && i+1<argc) {
+		else if(strcmp(argv[i], "-virtuals") == 0 && i+1<argc){
 			numvirtuals = atoi(argv[++i]);
-			if(numvirtuals < 0 || numvirtuals > 12) {
+			if(numvirtuals < 0 || numvirtuals > 12){
 				fprintf(stderr, "rio: wrong number of virtual displays, defaulting to 4\n");
 				numvirtuals = 4;
 			}
-		} else if (strcmp(argv[i], "-version") == 0) {
+		} else if(strcmp(argv[i], "-version") == 0){
 			fprintf(stderr, "%s", version[0]);
-			if (PATCHLEVEL > 0)
+			if(PATCHLEVEL > 0)
 				fprintf(stderr, "; patch level %d", PATCHLEVEL);
 			fprintf(stderr, "\n");
 			exit(0);
 		}
-		else if (strcmp(argv[i], "-s") == 0) {
+		else if(strcmp(argv[i], "-s") == 0){
 			scrolling = 1;
 		}
-		else if (argv[i][0] == '-')
+		else if(argv[i][0] == '-')
 			usage();
 		else
 			break;
-	for (; i < argc; i++)
-		if (strcmp(argv[i], "exit") == 0)
+	for(; i < argc; i++)
+		if(strcmp(argv[i], "exit") == 0)
 			do_exit++;
-		else if (strcmp(argv[i], "restart") == 0)
+		else if(strcmp(argv[i], "restart") == 0)
 			do_restart++;
 		else
 			usage();
 
-	if (do_exit && do_restart)
+	if(do_exit && do_restart)
 		usage();
 
 	shell = (char *)getenv("SHELL");
-	if (shell == NULL)
+	if(shell == NULL)
 		shell = DEFSHELL;
 
 	dpy = XOpenDisplay("");
-	if (dpy == 0)
+	if(dpy == 0)
 		fatal("can't open display");
 
 	initting = 1;
 	XSetErrorHandler(handler);
-	if (signal(SIGTERM, sighandler) == SIG_IGN)
+	if(signal(SIGTERM, sighandler) == SIG_IGN)
 		signal(SIGTERM, SIG_IGN);
-	if (signal(SIGINT, sighandler) == SIG_IGN)
+	if(signal(SIGINT, sighandler) == SIG_IGN)
 		signal(SIGINT, SIG_IGN);
-	if (signal(SIGHUP, sighandler) == SIG_IGN)
+	if(signal(SIGHUP, sighandler) == SIG_IGN)
 		signal(SIGHUP, SIG_IGN);
 
 	exit_rio = XInternAtom(dpy, "9WM_EXIT", False);
 	restart_rio = XInternAtom(dpy, "9WM_RESTART", False);
 
 	curtime = -1;		/* don't care */
-	if (do_exit) {
+	if(do_exit){
 		sendcmessage(DefaultRootWindow(dpy), exit_rio, 0L, 1, 1);
 		XSync(dpy, False);
 		exit(0);
 	}
-	if (do_restart) {
+	if(do_restart){
 		sendcmessage(DefaultRootWindow(dpy), restart_rio, 0L, 1, 1);
 		XSync(dpy, False);
 		exit(0);
 	}
 
-	if (0) XSynchronize(dpy, True);
+	if(0) XSynchronize(dpy, True);
 
 	wm_state = XInternAtom(dpy, "WM_STATE", False);
 	wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
@@ -176,24 +176,24 @@
 	_rio_running = XInternAtom(dpy, "_9WM_RUNNING", False);
 	_rio_hold_mode = XInternAtom(dpy, "_9WM_HOLD_MODE", False);
 
-	if (fname != 0)
-		if ((font = XLoadQueryFont(dpy, fname)) == 0)
+	if(fname != 0)
+		if((font = XLoadQueryFont(dpy, fname)) == 0)
 			fprintf(stderr, "rio: warning: can't load font %s\n", fname);
 
-	if (font == 0) {
+	if(font == 0){
 		i = 0;
-		for (;;) {
+		for(;;){
 			fname = fontlist[i++];
-			if (fname == 0) {
+			if(fname == 0){
 				fprintf(stderr, "rio: warning: can't find a font\n");
 				break;
 			}
 			font = XLoadQueryFont(dpy, fname);
-			if (font != 0)
+			if(font != 0)
 				break;
 		}
 	}
-	if (nostalgia) {
+	if(nostalgia){
 		_border--;
 		_inset--;
 	}
@@ -205,7 +205,7 @@
 	num_screens = ScreenCount(dpy);
 	screens = (ScreenInfo *)malloc(sizeof(ScreenInfo) * num_screens);
 
-	for (i = 0; i < num_screens; i++)
+	for(i = 0; i < num_screens; i++)
 		initscreen(&screens[i], i, background);
 
 	initb2menu(numvirtuals);
@@ -219,9 +219,10 @@
 
 	nofocus();
 
-	for (i = 0; i < num_screens; i++)
+	for(i = 0; i < num_screens; i++)
 		scanwins(&screens[i]);
 
+	keysetup();
 	mainloop(shape_event);
 	return 0;
 }
@@ -277,18 +278,18 @@
 		}
 		s->vis = DefaultVisual(dpy, i);
 	}
-	if(DefaultDepth(dpy, i) != s->depth) {
+	if(DefaultDepth(dpy, i) != s->depth){
 		s->def_cmap = XCreateColormap(dpy, s->root, s->vis, AllocNone); 
 	}
 
 	ds = DisplayString(dpy);
 	colon = rindex(ds, ':');
-	if (colon && num_screens > 1) {
+	if(colon && num_screens > 1){
 		strcpy(s->display, "DISPLAY=");
 		strcat(s->display, ds);
 		colon = s->display + 8 + (colon - ds);	/* use version in buf */
 		dot1 = index(colon, '.');	/* first period after colon */
-		if (!dot1)
+		if(!dot1)
 			dot1 = colon + strlen(colon);	/* if not there, append */
 		sprintf(dot1, ".%d", i);
 	}
@@ -314,7 +315,7 @@
 	gv.subwindow_mode = IncludeInferiors;
 	gmask = GCForeground | GCBackground | GCFunction | GCLineWidth
 		| GCSubwindowMode;
-	if (font != 0) {
+	if(font != 0){
 		gv.font = font->fid;
 		gmask |= GCFont;
 	}
@@ -339,7 +340,7 @@
 	XChangeWindowAttributes(dpy, s->root, mask, &attr);
 	XSync(dpy, False);
 
-	if (background) {
+	if(background){
 		XSetWindowBackgroundPixmap(dpy, s->root, s->root_pixmap);
 		XClearWindow(dpy, s->root);
 	} else
@@ -389,8 +390,8 @@
 {
 	int i;
 
-	for (i = 0; i < num_screens; i++)
-		if (screens[i].root == w)
+	for(i = 0; i < num_screens; i++)
+		if(screens[i].root == w)
 			return &screens[i];
 
 	return 0;
@@ -401,7 +402,7 @@
 {
 	XEvent ev;
 
-	if (curtime == CurrentTime) {
+	if(curtime == CurrentTime){
 		XChangeProperty(dpy, screens[0].root, _rio_running, _rio_running, 8,
 				PropModeAppend, (unsigned char *)"", 0);
 		XMaskEvent(dpy, PropertyChangeMask, &ev);
@@ -427,13 +428,13 @@
 	mask = 0;
 	if(usemask){
 		mask |= KeyPressMask;	/* seems to be necessary */
-		if (isroot)
+		if(isroot)
 			mask |= SubstructureRedirectMask;		/* magic! */
 		else
 			mask |= ExposureMask;	/* not really correct but so be it */
 	}
 	status = XSendEvent(dpy, w, False, mask, &ev);
-	if (status == 0)
+	if(status == 0)
 		fprintf(stderr, "rio: sendcmessage failed\n");
 }
 
@@ -468,8 +469,8 @@
 	fd_set rfds;
 	struct timeval t;
 
-	if (!signalled) {
-		if (QLength(dpy) > 0) {
+	if(!signalled){
+		if(QLength(dpy) > 0){
 			XNextEvent(dpy, e);
 			return;
 		}
@@ -477,17 +478,17 @@
 		FD_ZERO(&rfds);
 		FD_SET(fd, &rfds);
 		t.tv_sec = t.tv_usec = 0;
-		if (select(fd+1, &rfds, NULL, NULL, &t) == 1) {
+		if(select(fd+1, &rfds, NULL, NULL, &t) == 1){
 			XNextEvent(dpy, e);
 			return;
 		}
 		XFlush(dpy);
 		FD_SET(fd, &rfds);
-		if (select(fd+1, &rfds, NULL, NULL, NULL) == 1) {
+		if(select(fd+1, &rfds, NULL, NULL, NULL) == 1){
 			XNextEvent(dpy, e);
 			return;
 		}
-		if (errno != EINTR || !signalled) {
+		if(errno != EINTR || !signalled){
 			perror("rio: select failed");
 			exit(1);
 		}
@@ -506,16 +507,16 @@
 
 	/* order of un-reparenting determines final stacking order... */
 	cc[0] = cc[1] = 0;
-	for (c = clients; c; c = next) {
+	for(c = clients; c; c = next){
 		next = c->next;
 		i = normal(c);
 		c->next = cc[i];
 		cc[i] = c;
 	}
 
-	for (i = 0; i < 2; i++) {
-		for (c = cc[i]; c; c = c->next) {
-			if (!withdrawn(c)) {
+	for(i = 0; i < 2; i++){
+		for(c = cc[i]; c; c = c->next){
+			if(!withdrawn(c)){
 				gravitate(c, 1);
 				XReparentWindow(dpy, c->window, c->screen->root,
 						c->x, c->y);
@@ -526,7 +527,7 @@
 	}
 
 	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, timestamp());
-	for (i = 0; i < num_screens; i++)
+	for(i = 0; i < num_screens; i++)
 		cmapnofocus(&screens[i]);
 	XCloseDisplay(dpy);
 }
diff --git a/src/cmd/rio/manage.c b/src/cmd/rio/manage.c
index 11892a0..86c23e8 100644
--- a/src/cmd/rio/manage.c
+++ b/src/cmd/rio/manage.c
@@ -1,3 +1,7 @@
+/*
+ * Window management.
+ */
+
 /* Copyright (c) 1994-1996 David Hogan, see README for licence details */
 #include <stdio.h>
 #include <stdlib.h>
@@ -26,7 +30,7 @@
 
 	/* Get loads of hints */
 
-	if (XGetClassHint(dpy, c->window, &class) != 0) {	/* ``Success'' */
+	if(XGetClassHint(dpy, c->window, &class) != 0){	/* ``Success'' */
 		c->instance = class.res_name;
 		c->class = class.res_class;
 		c->is9term = 0;
@@ -45,63 +49,63 @@
 	setlabel(c);
 
 	hints = XGetWMHints(dpy, c->window);
-	if (XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 || c->size.flags == 0)
+	if(XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 || c->size.flags == 0)
 		c->size.flags = PSize;		/* not specified - punt */
 
 	getcmaps(c);
 	getproto(c);
 	gettrans(c);
-	if (c->is9term)
+	if(c->is9term)
 		c->hold = getiprop(c->window, _rio_hold_mode);
 
 	/* Figure out what to do with the window from hints */
 
-	if (!getstate(c->window, &state))
+	if(!getstate(c->window, &state))
 		state = hints ? hints->initial_state : NormalState;
 	dohide = (state == IconicState);
 
 	fixsize = 0;
-	if ((c->size.flags & (USSize|PSize)))
+	if((c->size.flags & (USSize|PSize)))
 		fixsize = 1;
-	if ((c->size.flags & (PMinSize|PMaxSize)) == (PMinSize|PMaxSize) && c->size.min_width == c->size.max_width && c->size.min_height == c->size.max_height)
+	if((c->size.flags & (PMinSize|PMaxSize)) == (PMinSize|PMaxSize) && c->size.min_width == c->size.max_width && c->size.min_height == c->size.max_height)
 		fixsize = 1;
 	doreshape = !mapped;
-	if (fixsize) {
-		if (c->size.flags & USPosition)
+	if(fixsize){
+		if(c->size.flags & USPosition)
 			doreshape = 0;
-		if (dohide && (c->size.flags & PPosition))
+		if(dohide && (c->size.flags & PPosition))
 			doreshape = 0;
-		if (c->trans != None)
+		if(c->trans != None)
 			doreshape = 0;
 	}
-	if (c->is9term)
+	if(c->is9term)
 		fixsize = 0;
-	if (c->size.flags & PBaseSize) {
+	if(c->size.flags & PBaseSize){
 		c->min_dx = c->size.base_width;
 		c->min_dy = c->size.base_height;
 	}
-	else if (c->size.flags & PMinSize) {
+	else if(c->size.flags & PMinSize){
 		c->min_dx = c->size.min_width;
 		c->min_dy = c->size.min_height;
 	}
-	else if (c->is9term) {
+	else if(c->is9term){
 		c->min_dx = 100;
 		c->min_dy = 50;
 	}
 	else
 		c->min_dx = c->min_dy = 0;
 
-	if (hints)
+	if(hints)
 		XFree(hints);
 
 	/* Now do it!!! */
 
-	if (doreshape) {
+	if(doreshape){
 		if(0) fprintf(stderr, "in doreshape is9term=%d fixsize=%d, x=%d, y=%d, min_dx=%d, min_dy=%d, dx=%d, dy=%d\n",
 				c->is9term, fixsize, c->x, c->y, c->min_dx, c->min_dy, c->dx, c->dy);
-		if (current && current->screen == c->screen)
+		if(current && current->screen == c->screen)
 			cmapnofocus(c->screen);
-		if (!c->is9term && c->x==0 && c->y==0) {
+		if(!c->is9term && c->x==0 && c->y==0){
 			static int nwin;
 
 			c->x = 20*nwin+BORDER;
@@ -110,10 +114,10 @@
 			nwin %= 10;
 		}
 
-		if (c->is9term && !(fixsize ? drag(c, Button3) : sweep(c, Button3))) {
+		if(c->is9term && !(fixsize ? drag(c, Button3) : sweep(c, Button3))){
 			XKillClient(dpy, c->window);
 			rmclient(c);
-			if (current && current->screen == c->screen)
+			if(current && current->screen == c->screen)
 				cmapfocus(current);
 			return 0;
 		}
@@ -135,9 +139,9 @@
 			&attrs);
 
 	XSelectInput(dpy, c->parent, SubstructureRedirectMask | SubstructureNotifyMask|ButtonPressMask| PointerMotionMask|LeaveWindowMask);
-	if (mapped)
+	if(mapped)
 		c->reparenting = 1;
-	if (doreshape && !fixsize)
+	if(doreshape && !fixsize)
 		XResizeWindow(dpy, c->window, c->dx, c->dy);
 	XSetWindowBorderWidth(dpy, c->window, 0);
 
@@ -149,13 +153,13 @@
 	  * (black (or white)  border around black (or white) window
 	  *  is not very helpful.
 	  */
-	if (c->screen->depth <= 8) {
+	if(c->screen->depth <= 8){
 		XSetWindowBorderWidth(dpy, c->parent, 1);
 	}
 
 	XReparentWindow(dpy, c->window, c->parent, BORDER, BORDER);
 #ifdef	SHAPE
-	if (shape) {
+	if(shape){
 		XShapeSelectInput(dpy, c->window, ShapeNotifyMask);
 		ignore_badwindow = 1;		/* magic */
 		setshape(c);
@@ -163,21 +167,21 @@
 	}
 #endif
 	XAddToSaveSet(dpy, c->window);
-	if (dohide)
+	if(dohide)
 		hide(c);
 	else {
 		XMapWindow(dpy, c->window);
 		XMapWindow(dpy, c->parent);
 		XUnmapWindow(dpy, c->screen->sweepwin);
-		if (nostalgia || doreshape)
+		if(nostalgia || doreshape)
 			active(c);
-		else if (c->trans != None && current && current->window == c->trans)
+		else if(c->trans != None && current && current->window == c->trans)
 			active(c);
 		else
 			setactive(c, 0);
 		setstate(c, NormalState);
 	}
-	if (current && (current != c))
+	if(current && (current != c))
 		cmapfocus(current);
 	c->init = 1;
 
@@ -202,12 +206,12 @@
 	XWindowAttributes attr;
 
 	XQueryTree(dpy, s->root, &dw1, &dw2, &wins, &nwins);
-	for (i = 0; i < nwins; i++) {
+	for(i = 0; i < nwins; i++){
 		XGetWindowAttributes(dpy, wins[i], &attr);
-		if (attr.override_redirect || wins[i] == s->menuwin)
+		if(attr.override_redirect || wins[i] == s->menuwin)
 			continue;
 		c = getclient(wins[i], 1);
-		if (c != 0 && c->window == wins[i] && !c->init) {
+		if(c != 0 && c->window == wins[i] && !c->init){
 			c->x = attr.x;
 			c->y = attr.y;
 			c->dx = attr.width;
@@ -215,7 +219,7 @@
 			c->border = attr.border_width;
 			c->screen = s;
 			c->parent = s->root;
-			if (attr.map_state == IsViewable)
+			if(attr.map_state == IsViewable)
 				manage(c, 1);
 		}
 	}
@@ -228,7 +232,7 @@
 	Window trans;
 
 	trans = None;
-	if (XGetTransientForHint(dpy, c->window, &trans) != 0)
+	if(XGetTransientForHint(dpy, c->window, &trans) != 0)
 		c->trans = trans;
 	else
 		c->trans = None;
@@ -256,11 +260,11 @@
 	int gravity, dx, dy, delta;
 
 	gravity = NorthWestGravity;
-	if (c->size.flags & PWinGravity)
+	if(c->size.flags & PWinGravity)
 		gravity = c->size.win_gravity;
 
 	delta = c->border-BORDER;
-	switch (gravity) {
+	switch (gravity){
 	case NorthWestGravity:
 		dx = 0;
 		dy = 0;
@@ -304,7 +308,7 @@
 	}
 	dx += BORDER;
 	dy += BORDER;
-	if (invert) {
+	if(invert){
 		dx = -dx;
 		dy = -dy;
 	}
@@ -315,7 +319,7 @@
 static void
 installcmap(ScreenInfo *s, Colormap cmap)
 {
-	if (cmap == None)
+	if(cmap == None)
 		XInstallColormap(dpy, s->def_cmap);
 	else
 		XInstallColormap(dpy, cmap);
@@ -327,19 +331,19 @@
 	int i, found;
 	Client *cc;
 
-	if (c == 0)
+	if(c == 0)
 		return;
-	else if (c->ncmapwins != 0) {
+	else if(c->ncmapwins != 0){
 		found = 0;
-		for (i = c->ncmapwins-1; i >= 0; i--) {
+		for(i = c->ncmapwins-1; i >= 0; i--){
 			installcmap(c->screen, c->wmcmaps[i]);
-			if (c->cmapwins[i] == c->window)
+			if(c->cmapwins[i] == c->window)
 				found++;
 		}
-		if (!found)
+		if(!found)
 			installcmap(c->screen, c->cmap);
 	}
-	else if (c->trans != None && (cc = getclient(c->trans, 0)) != 0 && cc->ncmapwins != 0)
+	else if(c->trans != None && (cc = getclient(c->trans, 0)) != 0 && cc->ncmapwins != 0)
 		cmapfocus(cc);
 	else
 		installcmap(c->screen, c->cmap);
@@ -358,7 +362,7 @@
 	Window *cw;
 	XWindowAttributes attr;
 
-	if (!c->init) {
+	if(!c->init){
 		ignore_badwindow = 1;
 		XGetWindowAttributes(dpy, c->window, &attr);
 		c->cmap = attr.colormap;
@@ -367,11 +371,11 @@
 	}
 
 	n = _getprop(c->window, wm_colormaps, XA_WINDOW, 100L, (void*)&cw);
-	if (c->ncmapwins != 0) {
+	if(c->ncmapwins != 0){
 		XFree((char *)c->cmapwins);
 		free((char *)c->wmcmaps);
 	}
-	if (n <= 0) {
+	if(n <= 0){
 		c->ncmapwins = 0;
 		return;
 	}
@@ -380,8 +384,8 @@
 	c->cmapwins = cw;
 
 	c->wmcmaps = (Colormap*)malloc(n*sizeof(Colormap));
-	for (i = 0; i < n; i++) {
-		if (cw[i] == c->window)
+	for(i = 0; i < n; i++){
+		if(cw[i] == c->window)
 			c->wmcmaps[i] = c->cmap;
 		else {
 			/* flush any errors (e.g., caused by mozilla tabs) */
@@ -400,17 +404,17 @@
 {
 	char *label, *p;
 
-	if (c->iconname != 0)
+	if(c->iconname != 0)
 		label = c->iconname;
-	else if (c->name != 0)
+	else if(c->name != 0)
 		label = c->name;
-	else if (c->instance != 0)
+	else if(c->instance != 0)
 		label = c->instance;
-	else if (c->class != 0)
+	else if(c->class != 0)
 		label = c->class;
 	else
 		label = "no label";
-	if ((p = index(label, ':')) != 0)
+	if((p = index(label, ':')) != 0)
 		*p = '\0';
 	c->label = label;
 }
@@ -424,7 +428,7 @@
 
 	/* don't try to add a border if the window is non-rectangular */
 	rect = XShapeGetRectangles(dpy, c->window, ShapeBounding, &n, &order);
-	if (n > 1)
+	if(n > 1)
 		XShapeCombineShape(dpy, c->parent, ShapeBounding, BORDER, BORDER,
 			c->window, ShapeBounding, ShapeSet);
 	XFree((void*)rect);
@@ -440,9 +444,9 @@
 	int status;
 
 	status = XGetWindowProperty(dpy, w, a, 0L, len, False, type, &real_type, &format, &n, &extra, p);
-	if (status != Success || *p == 0)
+	if(status != Success || *p == 0)
 		return -1;
-	if (n == 0)
+	if(n == 0)
 		XFree((void*) *p);
 	/* could check real_type, format, extra here... */
 	return n;
@@ -453,7 +457,7 @@
 {
 	unsigned char *p;
 
-	if (_getprop(w, a, XA_STRING, 100L, &p) <= 0)
+	if(_getprop(w, a, XA_STRING, 100L, &p) <= 0)
 		return 0;
 	return (char *)p;
 }
@@ -463,7 +467,7 @@
 {
 	char **p, *x;
 
-	if (_getprop(w, a, type, 1L, (void*)&p) <= 0)
+	if(_getprop(w, a, type, 1L, (void*)&p) <= 0)
 		return 0;
 	x = *p;
 	XFree((void*) p);
@@ -500,7 +504,7 @@
 {
 	long *p = 0;
 
-	if (_getprop(w, wm_state, wm_state, 2L, (void*)&p) <= 0)
+	if(_getprop(w, wm_state, wm_state, 2L, (void*)&p) <= 0)
 		return 0;
 
 	*state = (int) *p;
@@ -518,15 +522,15 @@
 
 	w = c->window;
 	c->proto = 0;
-	if ((n = _getprop(w, wm_protocols, XA_ATOM, 20L, (void*)&p)) <= 0)
+	if((n = _getprop(w, wm_protocols, XA_ATOM, 20L, (void*)&p)) <= 0)
 		return;
 
-	for (i = 0; i < n; i++)
-		if (p[i] == wm_delete)
+	for(i = 0; i < n; i++)
+		if(p[i] == wm_delete)
 			c->proto |= Pdelete;
-		else if (p[i] == wm_take_focus)
+		else if(p[i] == wm_take_focus)
 			c->proto |= Ptakefocus;
-		else if (p[i] == wm_lose_focus)
+		else if(p[i] == wm_lose_focus)
 			c->proto |= Plosefocus;
 
 	XFree((char *) p);
diff --git a/src/cmd/rio/menu.c b/src/cmd/rio/menu.c
index 37d5016..9c321e5 100644
--- a/src/cmd/rio/menu.c
+++ b/src/cmd/rio/menu.c
@@ -1,8 +1,13 @@
+/*
+ * Pop-up menus.
+ */
+
 /* Copyright (c) 1994-1996 David Hogan, see README for licence details */
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/wait.h>
 #include <X11/X.h>
 #include <X11/Xlib.h>
@@ -15,6 +20,7 @@
 int	numhidden;
 
 int virt;
+int reversehide = 1;
 
 Client * currents[NUMVIRTUALS] =
 {
@@ -53,6 +59,15 @@
 	0,
 };
 
+enum
+{
+	New,
+	Reshape,
+	Move,
+	Delete,
+	Hide
+};
+
 Menu	b3menu =
 {
 	b3items,
@@ -73,14 +88,14 @@
 
 	curtime = e->time;
 	s = getscreen(e->root);
-	if (s == 0)
+	if(s == 0)
 		return;
 	c = getclient(e->window, 0);
 	if(c){
-		if (debug) fprintf(stderr, "but: e x=%d y=%d c x=%d y=%d dx=%d dy=%d BORDR %d\n",
+		if(debug) fprintf(stderr, "but: e x=%d y=%d c x=%d y=%d dx=%d dy=%d BORDR %d\n",
 				e->x, e->y, c->x, c->y, c->dx, c->dy, BORDER);
 		if(borderorient(c, e->x, e->y) != BorderUnknown){
-			switch (e->button) {
+			switch (e->button){
 			case Button1:
 			case Button2:
 				reshape(c, e->button, pull, e);
@@ -94,26 +109,26 @@
 		}
 		e->x += c->x - BORDER;
 		e->y += c->y - BORDER;
-	} else if (e->window != e->root) {
-		if (debug) fprintf(stderr, "but no client: e x=%d y=%d\n",
+	} else if(e->window != e->root){
+		if(debug) fprintf(stderr, "but no client: e x=%d y=%d\n",
 				e->x, e->y);
 		XTranslateCoordinates(dpy, e->window, s->root, e->x, e->y,
 				&e->x, &e->y, &dw);
 	}		
-	switch (e->button) {
+	switch (e->button){
 	case Button1:
-		if (c) {
+		if(c){
 			XMapRaised(dpy, c->parent);
 			top(c);
 			active(c);
 		}
 		return;
 	case Button2:
-		if (c) {
+		if(c){
 			XMapRaised(dpy, c->parent);
 			active(c);
 			XAllowEvents (dpy, ReplayPointer, curtime);
-		} else if ((e->state&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) {
+		} else if((e->state&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)){
 			menuhit(e, &egg);
 		} else if(numvirtuals > 1 && (n = menuhit(e, &b2menu)) > -1) 
 				button2(n);
@@ -136,24 +151,24 @@
 		return;
 	}
 
-	if (current && current->screen == s)
+	if(current && current->screen == s)
 		cmapnofocus(s);
-	switch (n = menuhit(e, &b3menu)) {
-	case 0: 	/* New */
+	switch (n = menuhit(e, &b3menu)){
+	case New:
 		spawn(s);
 		break;
-	case 1: 	/* Reshape */
+	case Reshape:
 		reshape(selectwin(1, 0, s), Button3, sweep, 0);
 		break;
-	case 2: 	/* Move */
+	case Move:
 		move(selectwin(0, 0, s), Button3);
 		break;
-	case 3: 	/* Delete */
+	case Delete:
 		shift = 0;
 		c = selectwin(1, &shift, s);
 		delete(c, shift);
 		break;
-	case 4: 	/* Hide */
+	case Hide:
 		hide(selectwin(1, 0, s));
 		break;
 	default:	/* unhide window */
@@ -162,7 +177,7 @@
 	case -1:	/* nothing */
 		break;
 	}
-	if (current && current->screen == s)
+	if(current && current->screen == s)
 		cmapfocus(current);
 }
 
@@ -180,15 +195,15 @@
 	 * ugly dance to avoid leaving zombies. Could use SIGCHLD,
 	 * but it's not very portable.
 	 */
-	if (fork() == 0) {
-		if (fork() == 0) {
+	if(fork() == 0){
+		if(fork() == 0){
 			close(ConnectionNumber(dpy));
-			if (s->display[0] != '\0')
+			if(s->display[0] != '\0')
 				putenv(s->display);
 			signal(SIGINT, SIG_DFL);
 			signal(SIGTERM, SIG_DFL);
 			signal(SIGHUP, SIG_DFL);
-			if (termprog != NULL) {
+			if(termprog != NULL){
 				execl(shell, shell, "-c", termprog, 0);
 				fprintf(stderr, "rio: exec %s", shell);
 				perror(" failed");
@@ -208,18 +223,18 @@
 {
 	int odx, ody;
 
-	if (c == 0)
+	if(c == 0)
 		return;
 	odx = c->dx;
 	ody = c->dy;
-	if (fn(c, but, e) == 0)
+	if(fn(c, but, e) == 0)
 		return;
 	active(c);
 	top(c);
 	XRaiseWindow(dpy, c->parent);
 	XMoveResizeWindow(dpy, c->parent, c->x-BORDER, c->y-BORDER,
 					c->dx+2*BORDER, c->dy+2*BORDER);
-	if (c->dx == odx && c->dy == ody)
+	if(c->dx == odx && c->dy == ody)
 		sendconfig(c);
 	else
 		XMoveResizeWindow(dpy, c->window, BORDER, BORDER, c->dx, c->dy);
@@ -228,9 +243,9 @@
 void
 move(Client *c, int but)
 {
-	if (c == 0)
+	if(c == 0)
 		return;
-	if (drag(c, but) == 0)
+	if(drag(c, but) == 0)
 		return;
 	active(c);
 	top(c);
@@ -242,9 +257,9 @@
 void
 delete(Client *c, int shift)
 {
-	if (c == 0)
+	if(c == 0)
 		return;
-	if ((c->proto & Pdelete) && !shift)
+	if((c->proto & Pdelete) && !shift)
 		sendcmessage(c->window, wm_protocols, wm_delete, 0, 0);
 	else
 		XKillClient(dpy, c->window);		/* let event clean up */
@@ -253,19 +268,26 @@
 void
 hide(Client *c)
 {
-	if (c == 0 || numhidden == MAXHIDDEN)
+	if(c == 0 || numhidden == MAXHIDDEN)
 		return;
-	if (hidden(c)) {
+	if(hidden(c)){
 		fprintf(stderr, "rio: already hidden: %s\n", c->label);
 		return;
 	}
 	XUnmapWindow(dpy, c->parent);
 	XUnmapWindow(dpy, c->window);
 	setstate(c, IconicState);
-	if (c == current)
+	if(c == current)
 		nofocus();
-	hiddenc[numhidden] = c;
-	b3items[B3FIXED+numhidden] = c->label;
+	if(reversehide){
+		memmove(hiddenc+1, hiddenc, numhidden*sizeof hiddenc[0]);
+		memmove(b3items+B3FIXED+1, b3items+B3FIXED, numhidden*sizeof b3items[0]);
+		hiddenc[0] = c;
+		b3items[B3FIXED] = c->label;
+	}else{
+		hiddenc[numhidden] = c;
+		b3items[B3FIXED+numhidden] = c->label;
+	}
 	numhidden++;
 	b3items[B3FIXED+numhidden] = 0;
 }
@@ -276,19 +298,19 @@
 	Client *c;
 	int i;
 
-	if (n >= numhidden) {
+	if(n >= numhidden){
 		fprintf(stderr, "rio: unhide: n %d numhidden %d\n", n, numhidden);
 		return;
 	}
 	c = hiddenc[n];
-	if (!hidden(c)) {
+	if(!hidden(c)){
 		fprintf(stderr, "rio: unhide: not hidden: %s(0x%x)\n",
 			c->label, (int)c->window);
 		return;
 	}
 	c->virt = virt;
 
-	if (map) {
+	if(map){
 		XMapWindow(dpy, c->window);
 		XMapRaised(dpy, c->parent);
 		setstate(c, NormalState);
@@ -297,7 +319,7 @@
 	}
 
 	numhidden--;
-	for (i = n; i < numhidden; i ++) {
+	for(i = n; i < numhidden; i ++){
 		hiddenc[i] = hiddenc[i+1];
 		b3items[B3FIXED+i] = b3items[B3FIXED+i+1];
 	}
@@ -309,8 +331,8 @@
 {
 	int i;
 
-	for (i = 0; i < numhidden; i++)
-		if (c == hiddenc[i]) {
+	for(i = 0; i < numhidden; i++)
+		if(c == hiddenc[i]){
 			unhide(i, map);
 			return;
 		}
@@ -323,13 +345,13 @@
 {
 	int i;
 
-	if (name == 0)
+	if(name == 0)
 		name = "???";
 	c->label = name;
-	if (!hidden(c))
+	if(!hidden(c))
 		return;
-	for (i = 0; i < numhidden; i++)
-		if (c == hiddenc[i]) {
+	for(i = 0; i < numhidden; i++)
+		if(c == hiddenc[i]){
 			b3items[B3FIXED+i] = name;
 			return;
 		}
@@ -339,37 +361,37 @@
 button2(int n)
 {
 	switch_to(n);
-	if (current)
+	if(current)
 		cmapfocus(current);
 }
 
 void
 switch_to_c(int n, Client *c)
 {
-	if (c && c->next)
+	if(c && c->next)
 		switch_to_c(n,c->next);
 
-	if (c->parent == DefaultRootWindow(dpy))
+	if(c->parent == DefaultRootWindow(dpy))
 		return;
 
-	if (c->virt != virt && c->state == NormalState) {
+	if(c->virt != virt && c->state == NormalState){
 		XUnmapWindow(dpy, c->parent);
 		XUnmapWindow(dpy, c->window);
 		setstate(c, IconicState);
-		if (c == current)
+		if(c == current)
 			nofocus();
-	} else if (c->virt == virt && c->state == IconicState) {
+	} else if(c->virt == virt && c->state == IconicState){
 		int i;
 
-		for (i = 0; i < numhidden; i++)
-		if (c == hiddenc[i]) 
+		for(i = 0; i < numhidden; i++)
+		if(c == hiddenc[i]) 
 			break;
 
-		if (i == numhidden) {
+		if(i == numhidden){
 			XMapWindow(dpy, c->window);
 			XMapWindow(dpy, c->parent);
 			setstate(c, NormalState);
-			if (currents[virt] == c)
+			if(currents[virt] == c)
 				active(c); 
 		}
 	}
@@ -378,7 +400,7 @@
 void
 switch_to(int n)
 {
-	if (n == virt)
+	if(n == virt)
 		return;
 	currents[virt] = current;
 	virt = n;
diff --git a/src/cmd/rio/mkfile b/src/cmd/rio/mkfile
index 775597d..754a26c 100644
--- a/src/cmd/rio/mkfile
+++ b/src/cmd/rio/mkfile
@@ -1,4 +1,5 @@
 <$PLAN9/src/mkhdr
+<|sh $PLAN9/src/libdraw/mkwsysrules.sh	# for X11
 
 OFILES=\
 	client.$O\
@@ -7,10 +8,12 @@
 	error.$O\
 	event.$O\
 	grab.$O\
+	key.$O\
 	main.$O\
 	manage.$O\
 	menu.$O\
 
+CFLAGS=$CFLAGS -DDEBUG
 HFILES=dat.h fns.h
 
 TARG=rio
@@ -18,10 +21,14 @@
 # need to add lib64 when it exists (on x86-64), but
 # Darwin complains about the nonexistant directory
 L64=`[ -d $X11/lib64 ] && echo 64; echo`
-LDFLAGS=-L$X11/lib$L64\ -lXext -lX11
+LDFLAGS=-L$X11/lib$L64 -lXext -lX11
 
 <$PLAN9/src/mkone
 
-CFLAGS=$CFLAGS -DSHAPE -I$X11/include
+CFLAGS=$CFLAGS -DSHAPE
 
+$O.xevents: xevents.$O printevent.$O
+	$LD -o $target $prereq $LDFLAGS
+
+xevents.$O printevent.$O: printevent.h