+static void
+popsquares_reshape (Display *dpy, Window window, void *closure,
+ unsigned int w, unsigned int h)
+{
+ struct state *st = (struct state *) closure;
+ int s = get_integer_resource(st->dpy, "subdivision", "Integer");
+ int x, y;
+ XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
+
+ if (st->xgwa.width < 100 || st->xgwa.height < 100) /* tiny window */
+ {
+ int ss = (st->xgwa.width < st->xgwa.height
+ ? st->xgwa.width : st->xgwa.height);
+ s = ss / 15;
+ if (s < 1) s = 1;
+ }
+
+ if (st->xgwa.width > st->xgwa.height * 5 || /* weird aspect ratio */
+ st->xgwa.height > st->xgwa.width * 5)
+ {
+ double r = st->xgwa.width / (double) st->xgwa.height;
+ if (r > 1)
+ {
+ st->subdivisiony = s;
+ st->subdivisionx = s * r;
+ }
+ else
+ {
+ st->subdivisionx = s;
+ st->subdivisiony = s / r;
+ }
+ }
+ else
+ {
+ st->subdivisionx = st->subdivisiony = s;
+ }
+
+ st->sw = st->xgwa.width / st->subdivisionx;
+ st->sh = st->xgwa.height / st->subdivisiony;
+ st->gw = st->sw ? st->xgwa.width / st->sw : 0;
+ st->gh = st->sh ? st->xgwa.height / st->sh : 0;
+ st->nsquares = st->gw * st->gh;
+ free (st->squares);
+ if (st->nsquares < 1) st->nsquares = 1;
+ st->squares = (square *) calloc (st->nsquares, sizeof(square));
+
+ for (y = 0; y < st->gh; y++)
+ for (x = 0; x < st->gw; x++)
+ {
+ square *s = (square *) &st->squares[st->gw * y + x];
+ s->w = st->sw;
+ s->h = st->sh;
+ s->x = x * st->sw;
+ s->y = y * st->sh;
+ }
+
+ randomize_square_colors(st->squares, st->nsquares, st->ncolors);
+
+ if (st->dbuf) {
+ XFreePixmap (dpy, st->ba);
+ XFreePixmap (dpy, st->bb);
+ st->ba = XCreatePixmap (st->dpy, st->window, st->xgwa.width, st->xgwa.height, st->xgwa.depth);
+ st->bb = XCreatePixmap (st->dpy, st->window, st->xgwa.width, st->xgwa.height, st->xgwa.depth);
+ st->b = st->ba;
+ }
+}
+