+#if 0
+static void
+check_for_leaks (void)
+{
+#ifdef HAVE_SBRK
+ static unsigned long early_brk = 0;
+ unsigned long max = 30 * 1024 * 1024; /* 30 MB */
+ int b = (unsigned long) sbrk(0);
+ if (early_brk == 0)
+ early_brk = b;
+ else if (b > early_brk + max)
+ {
+ fprintf (stderr, "%s: leaked %lu MB -- aborting!\n",
+ progname, ((b - early_brk) >> 20));
+ exit (1);
+ }
+#endif /* HAVE_SBRK */
+}
+#endif
+
+static void *
+piecewise_init (Display *dd, Window ww)
+{
+ struct state *st = (struct state *) calloc (1, sizeof(*st));
+ st->dpy = dd;
+ st->window = ww;
+
+ st->count = get_integer_resource(st->dpy, "count", "Integer");
+ st->delay = get_integer_resource(st->dpy, "delay", "Integer");
+ st->ncolors = get_integer_resource(st->dpy, "ncolors", "Integer");
+ st->colorspeed = get_integer_resource(st->dpy, "colorspeed", "Integer");
+ st->dbuf = get_boolean_resource(st->dpy, "doubleBuffer", "Boolean");
+
+# ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
+ st->dbuf = False;
+# endif
+
+ st->color_iterations = st->colorspeed ? 100 / st->colorspeed : 100000;
+ if (!st->color_iterations)
+ st->color_iterations = 1;
+
+ XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
+ st->colors = calloc(sizeof(XColor), st->ncolors);
+
+ if (get_boolean_resource(st->dpy, "mono", "Boolean")) {
+ MONO:
+ st->ncolors = 1;
+ st->colors[0].pixel = get_pixel_resource(st->dpy, st->xgwa.colormap, "foreground", "Foreground");
+ }
+ else {
+ make_color_loop(st->xgwa.screen, st->xgwa.visual, st->xgwa.colormap,
+ 0, 1, 1, 120, 1, 1, 240, 1, 1,
+ st->colors, &st->ncolors, True, False);
+ if (st->ncolors < 2)
+ goto MONO;
+ }
+
+ if (st->dbuf) {
+#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
+ st->b = st->backb = xdbe_get_backbuffer(st->dpy, st->window, XdbeUndefined);
+#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
+
+ if (!st->b) {
+ 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;
+ }
+ }
+ else
+ st->b = st->window;
+
+ /* erasure gc */
+ st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap, "background", "Background");
+ st->erase_gc = XCreateGC (st->dpy, st->b, GCForeground, &st->gcv);
+
+ /* drawing gc */
+ st->flags = GCForeground;
+ st->color_index = random() % st->ncolors;
+ st->gcv.foreground = st->colors[st->color_index].pixel;
+ st->draw_gc = XCreateGC(st->dpy, st->b, st->flags, &st->gcv);
+
+ /* initialize circles */
+ st->circles = init_circles(st, st->count, st->xgwa.width, st->xgwa.height);
+
+ st->iterations = 0;
+
+ return st;
+}
+
+static unsigned long
+piecewise_draw (Display *dpy, Window window, void *closure)
+{
+ struct state *st = (struct state *) closure;
+ int i;
+
+ XFillRectangle (st->dpy, st->b, st->erase_gc, 0, 0, st->xgwa.width, st->xgwa.height);
+
+ sweep(st, st->count, st->circles);
+ for (i=0;i<st->count;i++) {
+ draw_circle(st, st->b, st->draw_gc, st->circles+i);
+ move_circle(st->circles+i, st->xgwa.width, st->xgwa.height);
+ }
+ flush_arc_buffer(st, st->b, st->draw_gc);
+
+ if (++st->iterations % st->color_iterations == 0) {
+ st->color_index = (st->color_index + 1) % st->ncolors;
+ XSetForeground(st->dpy, st->draw_gc, st->colors[st->color_index].pixel);
+ }
+
+#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
+ if (st->backb) {
+ XdbeSwapInfo info[1];
+ info[0].swap_window = st->window;
+ info[0].swap_action = XdbeUndefined;
+ XdbeSwapBuffers (st->dpy, info, 1);
+ }
+ else
+#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
+ if (st->dbuf) {
+ XCopyArea (st->dpy, st->b, st->window, st->erase_gc, 0, 0, st->xgwa.width, st->xgwa.height, 0, 0);
+ st->b = (st->b == st->ba ? st->bb : st->ba);
+ }
+
+/* check_for_leaks(); */
+ return st->delay;
+}
+
+static void
+piecewise_reshape (Display *dpy, Window window, void *closure,
+ unsigned int w, unsigned int h)
+{
+ struct state *st = (struct state *) closure;
+ XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
+}
+
+static Bool
+piecewise_event (Display *dpy, Window window, void *closure, XEvent *event)
+{
+ return False;
+}
+
+static void
+piecewise_free (Display *dpy, Window window, void *closure)
+{
+ struct state *st = (struct state *) closure;
+ free (st);
+}
+
+