+static unsigned long
+xspirograph_draw (Display *dpy, Window window, void *closure)
+{
+ struct state *st = (struct state *) closure;
+ Bool free_color = False;
+ Bool flip_p = (st->counter & 1);
+ int i;
+
+ switch (st->drawstate) {
+ case ERASE1:
+ /* 5 sec delay before starting the erase */
+ st->drawstate = ERASE2;
+ /* shouldn't this use the configured long_delay value??? */
+ return (st->long_delay == 0 ? 0 : 5000000);
+
+ case ERASE2:
+ /* erase, delaying 1/50th sec between frames */
+ st->eraser = erase_window(st->dpy, st->window, st->eraser);
+ if (st->eraser)
+ /* shouldn't this be a configured pause??? */
+ return 20000;
+ st->drawstate = NEW_LAYER;
+ /* just finished erasing -- leave screen black for 1 sec */
+ return (st->long_delay == 0 ? 0 : 1000000);
+
+ case DRAW:
+ /* most common case put in front */
+ for (i = 0; i < 1000; i++) {
+ if (go(st, st->radius1, (flip_p ? st->radius2 : -st->radius2),
+ st->distance)) {
+ st->drawstate = NEW_LAYER;
+ break;
+ }
+ }
+ /* Next draw is delayed sleep_time (initialized value)*/
+ return st->sub_sleep_time;
+
+ case NEW_LAYER:
+ /* Increment counter */
+ st->counter++;
+ if (st->counter > (2 * st->num_layers)) {
+ /* reset to zero, free, and erase next time through */
+ st->counter = 0;
+ if (free_color)
+ XFreeColors (st->dpy, st->xgwa.colormap, &st->color.pixel, 1, 0);
+ st->drawstate = ERASE1;
+ } else {
+ /* first, third, fifth, ... time through */
+ if (!flip_p)
+ pick_new (st);
+
+ new_colors (st);
+ st->drawstate = DRAW;
+ }
+ /* No delay to the next draw */
+ return 0;
+
+ default:
+ /* OOPS!! */
+ fprintf(stderr, "%s: invalid state\n", progname);
+ exit(1);