Fixes from Axel Belinfante.

Fix 9term New border flash too.
diff --git a/src/cmd/rio/color.c b/src/cmd/rio/color.c
index e3410bb..d585472 100644
--- a/src/cmd/rio/color.c
+++ b/src/cmd/rio/color.c
@@ -8,7 +8,7 @@
 #include "fns.h"
 
 unsigned long
-colorpixel(Display *dpy, int depth, unsigned long rgb, unsigned long def)
+colorpixel(Display *dpy, ScreenInfo *s, int depth, unsigned long rgb, unsigned long def)
 {
 	int r, g, b;
 
@@ -36,6 +36,9 @@
 		return (r<<11) | (g<<5) | b;
 	case 24:
 	case 32:
+		/* try to find byte order */
+		if (s->vis->red_mask & 0xff)
+			return (r) | (g<<8) | (b<<16); /* OK on Sun */
 		return rgb;
 	}
 }
diff --git a/src/cmd/rio/fns.h b/src/cmd/rio/fns.h
index e3c214d..90cfb27 100644
--- a/src/cmd/rio/fns.h
+++ b/src/cmd/rio/fns.h
@@ -10,7 +10,7 @@
 
 
 /* color.c */
-unsigned long colorpixel(Display*, int, unsigned long, unsigned long);
+unsigned long colorpixel(Display*, ScreenInfo*, int, unsigned long, unsigned long);
 
 /* main.c */
 void	usage();
diff --git a/src/cmd/rio/grab.c b/src/cmd/rio/grab.c
index 9b987ea..8838ed4 100644
--- a/src/cmd/rio/grab.c
+++ b/src/cmd/rio/grab.c
@@ -420,8 +420,10 @@
 	c->y = py;
 	
 	/* compensate position for size changed due to size hints */
-	c->x -= spx*(c->dx - rdx);
-	c->y -= spy*(c->dy - rdy);
+	if(spx)
+		c->x -= c->dx - rdx;
+	if(spy)
+		c->y -= c->dy - rdy;
 
 	return init;
 }
@@ -441,7 +443,7 @@
 	int x, y, dx, dy;
 	ScreenInfo *s;
 
-	if (debug) fprintf(stderr, "drawbound %dx%d +%d+%d\n", 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;
@@ -523,12 +525,12 @@
 	c->y -= BORDER;
 	c->dx += 2*BORDER;
 	c->dy += 2*BORDER;
-	if (bl || e0 == 0)
+	if (bl != BorderUnknown || e0 == 0)
 		getmouse(&cx, &cy, c->screen);
 	else
 		getmouse(&c->x, &c->y, c->screen);
 	XGrabServer(dpy);
-	if (bl) {
+	if (bl != BorderUnknown) {
 		notmoved = recalc(c, cx, cy, bl, notmoved);
 	}
 	drawbound(c, 1);
@@ -545,7 +547,7 @@
 					XGrabServer(dpy);
 					idle = 0;
 				}
-				if(e0 || bl)
+				if(e0 || bl != BorderUnknown)
 					notmoved = recalc(c, rx, ry, bl, notmoved);
 				else
 					notmoved = recalc(c, rx-cx, ry-cy, bl, notmoved);
@@ -615,7 +617,7 @@
 		return 0;
 	}
 	XChangeActivePointerGrab(dpy, ButtonMask, s->boxcurs, e->time);
-	return sweepdrag(c, but, e, 0, sweepcalc);
+	return sweepdrag(c, but, e, BorderUnknown, sweepcalc);
 }
 
 int
@@ -650,7 +652,7 @@
 		graberror("drag", status); /* */
 		return 0;
 	}
-	return sweepdrag(c, but, 0, 0, dragcalc);
+	return sweepdrag(c, but, 0, BorderUnknown, dragcalc);
 }
 
 void
diff --git a/src/cmd/rio/main.c b/src/cmd/rio/main.c
index c25f8a4..c79b9db 100644
--- a/src/cmd/rio/main.c
+++ b/src/cmd/rio/main.c
@@ -284,11 +284,11 @@
 
 	s->black = BlackPixel(dpy, i);
 	s->white = WhitePixel(dpy, i);
-	s->activeholdborder = colorpixel(dpy, s->depth, 0x000099, s->white);
-	s->inactiveholdborder = colorpixel(dpy, s->depth, 0x005DBB, s->black);
-	s->activeborder = colorpixel(dpy, s->depth ,0x55AAAA, s->black);
-	s->inactiveborder = colorpixel(dpy, s->depth, 0x9EEEEE, s->white);
-	s->red = colorpixel(dpy, s->depth, 0xDD0000, s->white);
+	s->activeholdborder = colorpixel(dpy, s, s->depth, 0x000099, s->white);
+	s->inactiveholdborder = colorpixel(dpy, s, s->depth, 0x005DBB, s->black);
+	s->activeborder = colorpixel(dpy, s, s->depth, 0x55AAAA, s->black);
+	s->inactiveborder = colorpixel(dpy, s, s->depth, 0x9EEEEE, s->white);
+	s->red = colorpixel(dpy, s, s->depth, 0xDD0000, s->white);
 	s->width = WidthOfScreen(ScreenOfDisplay(dpy, i));
 	s->height = HeightOfScreen(ScreenOfDisplay(dpy, i));
 	s->bkup[0] = XCreatePixmap(dpy, s->root, 2*s->width, BORDER, DefaultDepth(dpy, i));
@@ -313,23 +313,9 @@
 	gv.foreground = s->red;
 	s->gcred = XCreateGC(dpy, s->root, gmask, &gv);
 
-	gv.foreground = colorpixel(dpy, s->depth, 0xEEEEEE, s->black);
+	gv.foreground = colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
 	s->gcsweep = XCreateGC(dpy, s->root, gmask, &gv);
 
-	gv.foreground = colorpixel(dpy, s->depth, 0xE9FFE9, s->white);
-	s->gcmenubg = XCreateGC(dpy, s->root, gmask, &gv);
-
-	gv.foreground = colorpixel(dpy, s->depth, 0x448844, s->black);
-	s->gcmenubgs = XCreateGC(dpy, s->root, gmask, &gv);
-
-	gv.foreground = s->black;
-	gv.background = colorpixel(dpy, s->depth, 0xE9FFE9, s->white);
-	s->gcmenufg = XCreateGC(dpy, s->root, gmask, &gv);
-
-	gv.foreground = colorpixel(dpy, s->depth, 0xE9FFE9, s->white);
-	gv.background = colorpixel(dpy, s->depth, 0x448844, s->black);
-	s->gcmenufgs = XCreateGC(dpy, s->root, gmask, &gv);
-
 	initcurs(s);
 
 	attr.cursor = s->arrow;
@@ -346,28 +332,41 @@
 	} else
 		system("xsetroot -solid grey30");
 
-	attrs.border_pixel =  colorpixel(dpy, s->depth, 0x88CC88, s->black);
-	attrs.background_pixel =  colorpixel(dpy, s->depth, 0xE9FFE9, s->white);
-	attrs.save_under = True; /* Does this help us in anyway? */
+	attrs.border_pixel =  colorpixel(dpy, s, s->depth, 0x88CC88, s->black);
+	attrs.background_pixel =  colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
 	attrs.colormap = s->def_cmap;
 
 	s->menuwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 2,
 						s->depth,
 						CopyFromParent,
 						s->vis,
-						CWBackPixel | CWBorderPixel | CWSaveUnder|CWColormap,
+						CWBackPixel | CWBorderPixel | CWColormap,
 						&attrs
 						);
 
+
+	gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
+	s->gcmenubg = XCreateGC(dpy, s->menuwin, gmask, &gv);
+
+	gv.foreground = colorpixel(dpy, s, s->depth, 0x448844, s->black);
+	s->gcmenubgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
+
+	gv.foreground = s->black;
+	gv.background = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
+	s->gcmenufg = XCreateGC(dpy, s->menuwin, gmask, &gv);
+
+	gv.foreground = colorpixel(dpy, s, s->depth, 0xE9FFE9, s->white);
+	gv.background = colorpixel(dpy, s, s->depth, 0x448844, s->black);
+	s->gcmenufgs = XCreateGC(dpy, s->menuwin, gmask, &gv);
+
 	attrs.border_pixel =  s->red;
-	attrs.background_pixel =  colorpixel(dpy, s->depth, 0xEEEEEE, s->black);
-	attrs.save_under = True; /* Does this help us in anyway? */
+	attrs.background_pixel =  colorpixel(dpy, s, s->depth, 0xEEEEEE, s->black);
 	attrs.colormap = s->def_cmap;
 	s->sweepwin = XCreateWindow(dpy, s->root, 0, 0, 1, 1, 4,
 						s->depth,
 						CopyFromParent,
 						s->vis,
-						CWBackPixel | CWBorderPixel | CWSaveUnder|CWColormap,
+						CWBackPixel | CWBorderPixel | CWColormap,
 						&attrs
 						);
 }
diff --git a/src/cmd/rio/manage.c b/src/cmd/rio/manage.c
index 8fc0836..1478fda 100644
--- a/src/cmd/rio/manage.c
+++ b/src/cmd/rio/manage.c
@@ -32,6 +32,10 @@
 		c->is9term = 0;
 		if(isNew){
 			c->is9term = strstr(c->class, "term") || strstr(c->class, "Term");
+			if(c->is9term){
+				c->dx = 0;
+				c->dy = 0;
+			}
 			isNew = 0;
 		}
 	}