http://www.jwz.org/xscreensaver/xscreensaver-5.12.tar.gz
[xscreensaver] / utils / erase.c
index 9b58fa9a040f80713942754a628fb4ca1d7488cd..05b213e2aec540a4fac553b57f2e4b0367b560a3 100644 (file)
 /* erase.c: Erase the screen in various more or less interesting ways.
- * (c) 1997 by Johannes Keukelaar <johannes@nada.kth.se>
- * Permission to use in any way granted. Provided "as is" without expressed
- * or implied warranty. NO WARRANTY, NO EXPRESSION OF SUITABILITY FOR ANY
- * PURPOSE. (I.e.: Use in any way, but at your own risk!)
+ * Copyright (c) 1997-2008 Jamie Zawinski <jwz@jwz.org>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation.  No representations are made about the suitability of this
+ * software for any purpose.  It is provided "as is" without express or 
+ * implied warranty.
+ *
+ * Portions (c) 1997 by Johannes Keukelaar <johannes@nada.kth.se>:
+ *   Permission to use in any way granted. Provided "as is" without expressed
+ *   or implied warranty. NO WARRANTY, NO EXPRESSION OF SUITABILITY FOR ANY
+ *   PURPOSE. (I.e.: Use in any way, but at your own risk!)
  */
 
 #include "utils.h"
 #include "yarandom.h"
 #include "usleep.h"
 #include "resources.h"
+#include "erase.h"
+#include <sys/time.h> /* for gettimeofday() */
+
+extern char *progname;
 
 #undef countof
 #define countof(x) (sizeof(x)/sizeof(*(x)))
 
-typedef void (*Eraser) (Display *dpy, Window window, GC gc,
-                       int width, int height, int delay, int granularity);
+typedef void (*Eraser) (eraser_state *);
+
+struct eraser_state {
+  Display *dpy;
+  Window window;
+  GC fg_gc, bg_gc;
+  int width, height;
+  Eraser fn;
+
+  double start_time, stop_time;
+  double ratio, prev_ratio;
+
+  /* data for random_lines, venetian, random_squares */
+  Bool horiz_p;
+  Bool flip_p;
+  int nlines, *lines;
+
+  /* data for triple_wipe, quad_wipe */
+  Bool flip_x, flip_y;
+
+  /* data for circle_wipe, three_circle_wipe */
+  int start;
+
+  /* data for random_squares */
+  int cols;
+
+};
+
+
+static double
+double_time (void)
+{
+  struct timeval now;
+# ifdef GETTIMEOFDAY_TWO_ARGS
+  struct timezone tzp;
+  gettimeofday(&now, &tzp);
+# else
+  gettimeofday(&now);
+# endif
+
+  return (now.tv_sec + ((double) now.tv_usec * 0.000001));
+}
 
 
 static void
-random_lines (Display *dpy, Window window, GC gc,
-             int width, int height, int delay, int granularity)
+random_lines (eraser_state *st)
 {
-  Bool horiz_p = (random() & 1);
-  int max = (horiz_p ? height : width);
-  int *lines = (int *) calloc(max, sizeof(*lines));
   int i;
 
-  for (i = 0; i < max; i++)
-    lines[i] = i;
-
-  for (i = 0; i < max; i++)
+  if (! st->lines)     /* first time */
     {
-      int t, r;
-      t = lines[i];
-      r = random() % max;
-      lines[i] = lines[r];
-      lines[r] = t;
+      st->horiz_p = (random() & 1);
+      st->nlines = (st->horiz_p ? st->height : st->width);
+      st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
+
+      for (i = 0; i < st->nlines; i++)  /* every line */
+        st->lines[i] = i;
+
+      for (i = 0; i < st->nlines; i++)  /* shuffle */
+        {
+          int t, r;
+          t = st->lines[i];
+          r = random() % st->nlines;
+          st->lines[i] = st->lines[r];
+          st->lines[r] = t;
+        }
     }
 
-  for (i = 0; i < max; i++)
-    { 
-      if (horiz_p)
-       XDrawLine (dpy, window, gc, 0, lines[i], width, lines[i]);
+  for (i = st->nlines * st->prev_ratio;
+       i < st->nlines * st->ratio;
+       i++)
+    {
+      if (st->horiz_p)
+        XDrawLine (st->dpy, st->window, st->bg_gc, 
+                   0, st->lines[i], st->width, st->lines[i]);
       else
-       XDrawLine (dpy, window, gc, lines[i], 0, lines[i], height);
+        XDrawLine (st->dpy, st->window, st->bg_gc, 
+                   st->lines[i], 0, st->lines[i], st->height);
+    }
 
-      XSync (dpy, False);
-      if (delay > 0 && ((i % granularity) == 0))
-       usleep (delay * granularity);
+  if (st->ratio >= 1.0)
+    {
+      free (st->lines);
+      st->lines = 0;
     }
-  free(lines);
 }
 
 
 static void
-venetian (Display *dpy, Window window, GC gc,
-         int width, int height, int delay, int granularity)
+venetian (eraser_state *st)
 {
-  Bool horiz_p = (random() & 1);
-  Bool flip_p = (random() & 1);
-  int max = (horiz_p ? height : width);
-  int *lines = (int *) calloc(max, sizeof(*lines));
-  int i, j;
-
-  granularity /= 6;
-
-  j = 0;
-  for (i = 0; i < max*2; i++)
+  int i;
+  if (st->ratio == 0.0)
     {
-      int line = ((i / 16) * 16) - ((i % 16) * 15);
-      if (line >= 0 && line < max)
-       lines[j++] = (flip_p ? max - line : line);
+      int j = 0;
+      st->horiz_p = (random() & 1);
+      st->flip_p = (random() & 1);
+      st->nlines = (st->horiz_p ? st->height : st->width);
+      st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
+
+      for (i = 0; i < st->nlines * 2; i++)
+        {
+          int line = ((i / 16) * 16) - ((i % 16) * 15);
+          if (line >= 0 && line < st->nlines)
+            st->lines[j++] = (st->flip_p ? st->nlines - line : line);
+        }
     }
 
-  for (i = 0; i < max; i++)
-    { 
-      if (horiz_p)
-       XDrawLine (dpy, window, gc, 0, lines[i], width, lines[i]);
+  
+  for (i = st->nlines * st->prev_ratio;
+       i < st->nlines * st->ratio;
+       i++)
+    {
+      if (st->horiz_p)
+        XDrawLine (st->dpy, st->window, st->bg_gc, 
+                   0, st->lines[i], st->width, st->lines[i]);
       else
-       XDrawLine (dpy, window, gc, lines[i], 0, lines[i], height);
+        XDrawLine (st->dpy, st->window, st->bg_gc, 
+                   st->lines[i], 0, st->lines[i], st->height);
+    }
 
-      XSync (dpy, False);
-      if (delay > 0 && ((i % granularity) == 0))
-       usleep (delay * granularity);
+  if (st->ratio >= 1.0)
+    {
+      free (st->lines);
+      st->lines = 0;
     }
-  free(lines);
 }
 
 
 static void
-triple_wipe (Display *dpy, Window window, GC gc,
-            int width, int height, int delay, int granularity)
+triple_wipe (eraser_state *st)
 {
-  Bool flip_x = random() & 1;
-  Bool flip_y = random() & 1;
-  int max = width + (height / 2);
-  int *lines = (int *)calloc(max, sizeof(int));
   int i;
+  if (st->ratio == 0.0)
+    {
+      st->flip_x = random() & 1;
+      st->flip_y = random() & 1;
+      st->nlines = st->width + (st->height / 2);
+      st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
+
+      for (i = 0; i < st->width / 2; i++)
+        st->lines[i] = i * 2 + st->height;
+      for (i = 0; i < st->height / 2; i++)
+        st->lines[i + st->width / 2] = i*2;
+      for (i = 0; i < st->width / 2; i++)
+        st->lines[i + st->width / 2 + st->height / 2] = 
+          st->width - i * 2 - (st->width % 2 ? 0 : 1) + st->height;
+    }
 
-  for(i = 0; i < width/2; i++)
-    lines[i] = i*2+height;
-  for(i = 0; i < height/2; i++)
-    lines[i+width/2] = i*2;
-  for(i = 0; i < width/2; i++)
-    lines[i+width/2+height/2] = width-i*2-(width%2?0:1)+height;
-
-  granularity /= 6;
-
-  for (i = 0; i < max; i++)
-    { 
+  for (i = st->nlines * st->prev_ratio;
+       i < st->nlines * st->ratio;
+       i++)
+    {
       int x, y, x2, y2;
-      if (lines[i] < height)
-       x = 0, y = lines[i], x2 = width, y2 = y;
+
+      if (st->lines[i] < st->height)
+        x = 0, y = st->lines[i], x2 = st->width, y2 = y;
       else
-       x = lines[i]-height, y = 0, x2 = x, y2 = height;
+        x = st->lines[i]-st->height, y = 0, x2 = x, y2 = st->height;
 
-      if (flip_x)
-       x = width-x, x2 = width-x2;
-      if (flip_y)
-       y = height-y, y2 = height-y2;
+      if (st->flip_x)
+        x = st->width - x, x2 = st->width - x2;
+      if (st->flip_y)
+        y = st->height - y, y2 = st->height - y2;
 
-      XDrawLine (dpy, window, gc, x, y, x2, y2);
-      XSync (dpy, False);
-      if (delay > 0 && ((i % granularity) == 0))
-       usleep (delay*granularity);
+      XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2);
+    }
+
+  if (st->ratio >= 1.0)
+    {
+      free (st->lines);
+      st->lines = 0;
     }
-  free(lines);
 }
 
 
 static void
-quad_wipe (Display *dpy, Window window, GC gc,
-          int width, int height, int delay, int granularity)
+quad_wipe (eraser_state *st)
 {
-  Bool flip_x = random() & 1;
-  Bool flip_y = random() & 1;
-  int max = width + height;
-  int *lines = (int *)calloc(max, sizeof(int));
   int i;
-
-  granularity /= 3;
-
-  for (i = 0; i < max/4; i++)
+  if (st->ratio == 0.0)
     {
-      lines[i*4]   = i*2;
-      lines[i*4+1] = height-i*2-(height%2?0:1);
-      lines[i*4+2] = height+i*2;
-      lines[i*4+3] = height+width-i*2-(width%2?0:1);
+      st->flip_x = random() & 1;
+      st->flip_y = random() & 1;
+      st->nlines = st->width + st->height;
+      st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
+
+      for (i = 0; i < st->nlines/4; i++)
+        {
+          st->lines[i*4]   = i*2;
+          st->lines[i*4+1] = st->height - i*2 - (st->height % 2 ? 0 : 1);
+          st->lines[i*4+2] = st->height + i*2;
+          st->lines[i*4+3] = st->height + st->width - i*2
+            - (st->width % 2 ? 0 : 1);
+        }
     }
 
-  for (i = 0; i < max; i++)
-    { 
+  for (i = st->nlines * st->prev_ratio;
+       i < st->nlines * st->ratio;
+       i++)
+    {
       int x, y, x2, y2;
-      if (lines[i] < height)
-       x = 0, y = lines[i], x2 = width, y2 = y;
+      if (st->lines[i] < st->height)
+        x = 0, y = st->lines[i], x2 = st->width, y2 = y;
       else
-       x = lines[i]-height, y = 0, x2 = x, y2 = height;
+        x = st->lines[i] - st->height, y = 0, x2 = x, y2 = st->height;
 
-      if (flip_x)
-       x = width-x, x2 = width-x2;
-      if (flip_y)
-       y = height-y, y2 = height-y2;
+      if (st->flip_x)
+        x = st->width-x, x2 = st->width-x2;
+      if (st->flip_y)
+        y = st->height-y, y2 = st->height-y2;
 
-      XDrawLine (dpy, window, gc, x, y, x2, y2);
-      XSync (dpy, False);
-      if (delay > 0 && ((i % granularity) == 0))
-       usleep (delay*granularity);
+      XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2);
     }
-  free(lines);
-}
 
+  if (st->ratio >= 1.0)
+    {
+      free (st->lines);
+      st->lines = 0;
+    }
+}
 
 
 static void
-circle_wipe (Display *dpy, Window window, GC gc,
-            int width, int height, int delay, int granularity)
+circle_wipe (eraser_state *st)
 {
-  int full = 360 * 64;
-  int inc = full / 64;
-  int start = random() % full;
-  int rad = (width > height ? width : height);
-  int i;
-  if (random() & 1)
-    inc = -inc;
-  for (i = (inc > 0 ? 0 : full);
-       (inc > 0 ? i < full : i > 0);
-       i += inc)
+  int rad = (st->width > st->height ? st->width : st->height);
+  int max = 360 * 64;
+  int th, oth;
+
+  if (st->ratio == 0.0)
+    {
+      st->flip_p = random() & 1;
+      st->start = random() % max;
+    }
+
+  th  = max * st->ratio;
+  oth = max * st->prev_ratio;
+  if (st->flip_p)
     {
-      XFillArc(dpy, window, gc,
-              (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (i+start) % full, inc);
-      XFlush (dpy);
-      usleep (delay*granularity);
+      th  = max - th;
+      oth = max - oth;
     }
+  XFillArc (st->dpy, st->window, st->bg_gc,
+            (st->width  / 2) - rad,
+            (st->height / 2) - rad, 
+            rad*2, rad*2,
+            (st->start + oth) % max,
+            th-oth);
 }
 
 
 static void
-three_circle_wipe (Display *dpy, Window window, GC gc,
-                  int width, int height, int delay, int granularity)
+three_circle_wipe (eraser_state *st)
 {
+  int rad = (st->width > st->height ? st->width : st->height);
+  int max = 360 * 64;
+  int th, oth;
   int i;
-  int full = 360 * 64;
-  int q = full / 6;
-  int q2 = q * 2;
-  int inc = full / 240;
-  int start = random() % q;
-  int rad = (width > height ? width : height);
-
-  for (i = 0; i < q; i += inc)
+
+  if (st->ratio == 0.0)
+    st->start = random() % max;
+
+  th  = max/6 * st->ratio;
+  oth = max/6 * st->prev_ratio;
+
+  for (i = 0; i < 3; i++)
     {
-      XFillArc(dpy, window, gc, (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (start+i) % full, inc);
-      XFillArc(dpy, window, gc, (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (start-i) % full, -inc);
-
-      XFillArc(dpy, window, gc, (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (start+q2+i) % full, inc);
-      XFillArc(dpy, window, gc, (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (start+q2-i) % full, -inc);
-
-      XFillArc(dpy, window, gc, (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (start+q2+q2+i) % full, inc);
-      XFillArc(dpy, window, gc, (width/2)-rad, (height/2)-rad, rad*2, rad*2,
-              (start+q2+q2-i) % full, -inc);
-
-      XSync (dpy, False);
-      usleep (delay*granularity);
+      int off = i * max / 3;
+      XFillArc (st->dpy, st->window, st->bg_gc,
+                (st->width  / 2) - rad,
+                (st->height / 2) - rad, 
+                rad*2, rad*2,
+                (st->start + off + oth) % max,
+                th-oth);
+      XFillArc (st->dpy, st->window, st->bg_gc,
+                (st->width  / 2) - rad,
+                (st->height / 2) - rad, 
+                rad*2, rad*2,
+                (st->start + off - oth) % max,
+                oth-th);
     }
 }
 
 
 static void
-squaretate (Display *dpy, Window window, GC gc,
-           int width, int height, int delay, int granularity)
+squaretate (eraser_state *st)
 {
-  int steps = (((width > height ? width : width) * 2) / granularity);
-  int i;
-  Bool flip = random() & 1;
+  int max = ((st->width > st->height ? st->width : st->height) * 2);
+  XPoint points [3];
+  int i = max * st->ratio;
+
+  if (st->ratio == 0.0)
+    st->flip_p = random() & 1;
+    
+# define DRAW()                                                \
+  if (st->flip_p) {                                    \
+    points[0].x = st->width - points[0].x;             \
+    points[1].x = st->width - points[1].x;             \
+    points[2].x = st->width - points[2].x;             \
+  }                                                    \
+  XFillPolygon (st->dpy, st->window, st->bg_gc,                \
+               points, 3, Convex, CoordModeOrigin)
+
+  points[0].x = 0;
+  points[0].y = 0;
+  points[1].x = st->width;
+  points[1].y = 0;
+  points[2].x = 0;
+  points[2].y = points[0].y + ((i * st->height) / max);
+  DRAW();
+
+  points[0].x = 0;
+  points[0].y = 0;
+  points[1].x = 0;
+  points[1].y = st->height;
+  points[2].x = ((i * st->width) / max);
+  points[2].y = st->height;
+  DRAW();
+
+  points[0].x = st->width;
+  points[0].y = st->height;
+  points[1].x = 0;
+  points[1].y = st->height;
+  points[2].x = st->width;
+  points[2].y = st->height - ((i * st->height) / max);
+  DRAW();
+
+  points[0].x = st->width;
+  points[0].y = st->height;
+  points[1].x = st->width;
+  points[1].y = 0;
+  points[2].x = st->width - ((i * st->width) / max);
+  points[2].y = 0;
+  DRAW();
+# undef DRAW
+}
 
-#define DRAW() \
-      if (flip) { \
-       points[0].x = width-points[0].x; \
-       points[1].x = width-points[1].x; \
-        points[2].x = width-points[2].x; } \
-      XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin)
 
-  for (i = 0; i < steps; i++)
+static void
+fizzle (eraser_state *st)
+{
+  XPoint *points;
+  int chunk = 20000;
+  int npoints = st->width * st->height * 4;
+  npoints *= (st->ratio - st->prev_ratio);
+
+  points = (XPoint *) malloc (chunk * sizeof(*points));
+  if (! points) return;
+
+  while (npoints > 0)
     {
-      XPoint points [3];
-      points[0].x = 0;
-      points[0].y = 0;
-      points[1].x = width;
-      points[1].y = 0;
-      points[2].x = 0;
-      points[2].y = points[0].y + ((i * height) / steps);
-      DRAW();
-
-      points[0].x = 0;
-      points[0].y = 0;
-      points[1].x = 0;
-      points[1].y = height;
-      points[2].x = ((i * width) / steps);
-      points[2].y = height;
-      DRAW();
-
-      points[0].x = width;
-      points[0].y = height;
-      points[1].x = 0;
-      points[1].y = height;
-      points[2].x = width;
-      points[2].y = height - ((i * height) / steps);
-      DRAW();
-
-      points[0].x = width;
-      points[0].y = height;
-      points[1].x = width;
-      points[1].y = 0;
-      points[2].x = width - ((i * width) / steps);
-      points[2].y = 0;
-      DRAW();
-
-      XSync (dpy, True);
-      if (delay > 0)
-       usleep (delay * granularity);
-   }
-#undef DRAW
+      int remain = (chunk > npoints ? npoints : chunk);
+      int i;
+      for (i = 0; i < remain; i++)
+        {
+          int r = random();
+          points[i].x = r % st->width;
+          points[i].y = (r >> 16) % st->height;
+        }
+      XDrawPoints (st->dpy, st->window, st->bg_gc, 
+                   points, remain, CoordModeOrigin);
+      npoints -= remain;
+    }
+  free (points);
 }
 
 
-/* from Frederick Roeber <roeber@netscape.com> */
 static void
-fizzle (Display *dpy, Window window, GC gc,
-           int width, int height, int delay, int granularity)
+spiral (eraser_state *st)
 {
-  /* These dimensions must be prime numbers.  They should be roughly the
-     square root of the width and height. */
-# define BX 31
-# define BY 31
-# define SIZE (BX*BY)
-
-  int array[SIZE];
-  int i, j;
-  XPoint *skews;
-  int nx, ny;
-
-  /* Distribute the numbers [0,SIZE) randomly in the array */
-  {
-    int indices[SIZE];
-
-    for( i = 0; i < SIZE; i++ ) {
-      array[i] = -1;
-      indices[i] = i;
-    } 
-
-    for( i = 0; i < SIZE; i++ ) {
-      j = random()%(SIZE-i);
-      array[indices[j]] = i;
-      indices[j] = indices[SIZE-i-1];
-    }
-  }
-
-  /* nx, ny are the number of cells across and down, rounded up */
-  nx = width  / BX + (0 == (width %BX) ? 0 : 1);
-  ny = height / BY + (0 == (height%BY) ? 0 : 1);
-  skews = (XPoint *)malloc(sizeof(XPoint) * (nx*ny));
-  if( (XPoint *)0 != skews ) {
-    for( i = 0; i < nx; i++ ) {
-      for( j = 0; j < ny; j++ ) {
-        skews[j * nx + i].x = random()%BX;
-        skews[j * nx + i].y = random()%BY;
-      }
-    }
-  }
+  int max_radius = (st->width > st->height ? st->width : st->height) * 0.7;
+  int loops = 10;
+  float max_th = M_PI * 2 * loops;
+  int i;
+  int steps = 360 * loops / 4;
+  float off;
 
-# define SKEWX(cx, cy) (((XPoint *)0 == skews)?0:skews[cy*nx + cx].x)
-# define SKEWY(cx, cy) (((XPoint *)0 == skews)?0:skews[cy*nx + cx].y)
+  if (st->ratio == 0.0)
+    {
+      st->flip_p = random() & 1;
+      st->start = random() % 360;
+    }
 
-  for( i = 0; i < SIZE; i++ ) {
-    int x = array[i] % BX;
-    int y = array[i] / BX;
+  off = st->start * M_PI / 180;
 
+  for (i = steps * st->prev_ratio;
+       i < steps * st->ratio;
+       i++)
     {
-      int iy, cy;
-      for( iy = 0, cy = 0; iy < height; iy += BY, cy++ ) {
-        int ix, cx;
-        for( ix = 0, cx = 0; ix < width; ix += BX, cx++ ) {
-          int xx = ix + (SKEWX(cx, cy) + x*((cx%(BX-1))+1))%BX;
-          int yy = iy + (SKEWY(cx, cy) + y*((cy%(BY-1))+1))%BY;
-          XDrawPoint(dpy, window, gc, xx, yy);
+      float th1 = i     * max_th / steps;
+      float th2 = (i+1) * max_th / steps;
+      int   r1  = i     * max_radius / steps;
+      int   r2  = (i+1) * max_radius / steps;
+      XPoint points[3];
+
+      if (st->flip_p)
+        {
+          th1 = max_th - th1;
+          th2 = max_th - th2;
         }
-      }
+
+      points[0].x = st->width  / 2;
+      points[0].y = st->height / 2;
+      points[1].x = points[0].x + r1 * cos (off + th1);
+      points[1].y = points[0].y + r1 * sin (off + th1);
+      points[2].x = points[0].x + r2 * cos (off + th2);
+      points[2].y = points[0].y + r2 * sin (off + th2);
+/*  XFillRectangle(st->dpy, st->window, st->fg_gc,0,0,st->width, st->height);*/
+      XFillPolygon (st->dpy, st->window, st->bg_gc,
+                    points, 3, Convex, CoordModeOrigin);
     }
+}
+
+
+static void
+random_squares (eraser_state *st)
+{
+  int i, size, rows;
 
-    if( (BX-1) == (i%BX) ) {
-      XSync (dpy, False);
-      usleep (delay*granularity);
+  if (st->ratio == 0.0)
+    {
+      st->cols = 10 + random() % 30;
+      size = st->width / st->cols;
+      rows = (size ? (st->height / size) : 0) + 1;
+      st->nlines = st->cols * rows;
+      st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
+
+      for (i = 0; i < st->nlines; i++)  /* every square */
+        st->lines[i] = i;
+
+      for (i = 0; i < st->nlines; i++)  /* shuffle */
+        {
+          int t, r;
+          t = st->lines[i];
+          r = random() % st->nlines;
+          st->lines[i] = st->lines[r];
+          st->lines[r] = t;
+        }
     }
-  }
 
-# undef SKEWX
-# undef SKEWY
+  size = st->width / st->cols;
+  rows = (size ? (st->height / size) : 0) + 1;
 
-  if( (XPoint *)0 != skews ) {
-    free(skews);
-  }
+  for (i = st->nlines * st->prev_ratio;
+       i < st->nlines * st->ratio;
+       i++)
+    {
+      int x = st->lines[i] % st->cols;
+      int y = st->lines[i] / st->cols;
+      XFillRectangle (st->dpy, st->window, st->bg_gc,
+                      st->width  * x / st->cols,
+                      st->height * y / rows,
+                      size+1, size+1);
+    }
 
-# undef BX
-# undef BY
-# undef SIZE
+  if (st->ratio >= 1.0)
+    {
+      free (st->lines);
+      st->lines = 0;
+    }
 }
 
 
-#undef MAX
-#undef MIN
-#define MAX(a,b) ((a)>(b)?(a):(b))
-#define MIN(a,b) ((a)<(b)?(a):(b))
-
-/* from David Bagley <bagleyd@tux.org> */
+/* I first saw something like this, albeit in reverse, in an early Tetris
+   implementation for the Mac.
+    -- Torbjörn Andersson <torbjorn@dev.eurotime.se>
+ */
 static void
-random_squares(Display * dpy, Window window, GC gc,
-               int width, int height, int delay, int granularity)
+slide_lines (eraser_state *st)
 {
-  int randsize = MAX(1, MIN(width, height) / (16 + (random() % 32)));
-  int max = (height / randsize + 1) * (width / randsize + 1);
-  int *squares = (int *) calloc(max, sizeof (*squares));
-  int i;
-  int columns = width / randsize + 1;  /* Add an extra for roundoff */
+  int max = st->width * 1.1;
+  int nlines = 40;
+  int h = st->height / nlines;
+  int y, step;
+  int tick = 0;
 
-  for (i = 0; i < max; i++)
-    squares[i] = i;
+  if (h < 10)
+    h = 10;
 
-  for (i = 0; i < max; i++)
+  step = (max * st->ratio) - (max * st->prev_ratio);
+  if (step <= 0)
+    step = 1;
+
+  for (y = 0; y < st->height; y += h)
     {
-      int t, r;
-      t = squares[i];
-      r = random() % max;
-      squares[i] = squares[r];
-      squares[r] = t;
+      if (st->width <= step)
+        ;
+      else if (tick & 1)
+        {
+          XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
+                     0, y, st->width-step, h, step, y);
+          XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                          0, y, step, h);
+        }
+      else
+        {
+          XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
+                     step, y, st->width-step, h, 0, y);
+          XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                          st->width-step, y, step, h);
+        }
+
+      tick++;
     }
+}
 
-  for (i = 0; i < max; i++)
+
+/* from Frederick Roeber <roeber@xigo.com> */
+static void
+losira (eraser_state *st)
+{
+  double mode1 = 0.55;
+  double mode2 = mode1 + 0.30;
+  double mode3 = 1.0;
+  int radius = 10;
+
+  if (st->ratio < mode1)               /* squeeze from the sides */
+    {
+      double ratio = st->ratio / mode1;
+      double prev_ratio = st->prev_ratio / mode1;
+      int max = st->width / 2;
+      int step = (max * ratio) - (max * prev_ratio);
+
+      if (step <= 0)
+        step = 1;
+
+      /* pull from left */
+      XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
+                 0, 0, max - step, st->height, step, 0);
+      XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                      0, 0, max * ratio, st->height);
+
+      /* pull from right */
+      XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
+                 max+step, 0, max - step, st->height, max, 0);
+      XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                      max + max*(1-ratio), 0, max, st->height);
+
+      /* expand white from center */
+      XFillRectangle (st->dpy, st->window, st->fg_gc,
+                      max - (radius * ratio), 0,
+                      radius * ratio * 2, st->height);
+    }
+  else if (st->ratio < mode2)          /* squeeze from the top/bottom */
+    {
+      double ratio = (st->ratio - mode1) / (mode2 - mode1);
+      int max = st->height / 2;
+
+      /* fill middle */
+      XFillRectangle (st->dpy, st->window, st->fg_gc,
+                      st->width/2 - radius,
+                      max * ratio,
+                      radius*2, st->height * (1 - ratio));
+
+      /* fill left and right */
+      XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                      0, 0, st->width/2 - radius, st->height);
+      XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                      st->width/2 + radius, 0, st->width/2, st->height);
+
+      /* fill top and bottom */
+      XFillRectangle (st->dpy, st->window, st->bg_gc,
+                      0, 0, st->width, max * ratio);
+      XFillRectangle (st->dpy, st->window, st->bg_gc,
+                      0, st->height - (max * ratio),
+                      st->width, max);
+
+      /* cap top */
+      XFillArc (st->dpy, st->window, st->fg_gc,
+                st->width/2 - radius,
+                max * ratio - radius,
+                radius*2, radius*2,
+                0, 180*64);
+
+      /* cap bottom */
+      XFillArc (st->dpy, st->window, st->fg_gc,
+                st->width/2 - radius,
+                st->height - (max * ratio + radius),
+                radius*2, radius*2,
+                180*64, 180*64);
+    }
+  else                                 /* starburst */
     {
-      XFillRectangle(dpy, window, gc,
-                    (squares[i] % columns) * randsize,
-                    (squares[i] / columns) * randsize,
-                    randsize, randsize);
-
-      XSync(dpy, False);
-      if (delay > 0 && ((i % granularity) == 0))
-      usleep(delay * granularity);
+      double ratio = (st->ratio - mode2) / (mode3 - mode2);
+      double r2 = ratio * radius * 4;
+      XArc arc[9];
+      int i;
+      int angle = 360*64/countof(arc);
+
+      for (i = 0; i < countof(arc); i++)
+        {
+          double th;
+          arc[i].angle1 = angle * i;
+          arc[i].angle2 = angle;
+          arc[i].width  = radius*2 * (1 + ratio);
+          arc[i].height = radius*2 * (1 + ratio);
+          arc[i].x = st->width  / 2 - radius;
+          arc[i].y = st->height / 2 - radius;
+
+          th = ((arc[i].angle1 + (arc[i].angle2 / 2)) / 64.0 / 180 * M_PI);
+
+          arc[i].x += r2 * cos (th);
+          arc[i].y -= r2 * sin (th);
+        }
+
+      XFillRectangle (st->dpy, st->window, st->bg_gc, 
+                      0, 0, st->width, st->height);
+      XFillArcs (st->dpy, st->window, st->fg_gc, arc, countof(arc));
     }
-  free(squares);
 }
 
 
@@ -424,100 +649,110 @@ static Eraser erasers[] = {
   three_circle_wipe,
   squaretate,
   fizzle,
+  spiral,
   random_squares,
+  slide_lines,
+  losira,
 };
 
 
-void
-erase_window(Display *dpy, Window window, GC gc,
-            int width, int height, int mode, int delay)
-{
-  int granularity = 25;
-
-  if (mode < 0 || mode >= countof(erasers))
-    mode = random() % countof(erasers);
-  (*(erasers[mode])) (dpy, window, gc, width, height, delay, granularity);
-  XClearWindow (dpy, window);
-  XSync(dpy, False);
-}
-
-
-void
-erase_full_window(Display *dpy, Window window)
+static eraser_state *
+eraser_init (Display *dpy, Window window)
 {
+  eraser_state *st = (eraser_state *) calloc (1, sizeof(*st));
   XWindowAttributes xgwa;
   XGCValues gcv;
-  GC erase_gc;
-  XColor black;
-  int erase_speed, erase_mode;
+  unsigned long fg, bg;
+  double duration;
+  int which;
   char *s;
 
-  s = get_string_resource("eraseSpeed", "Integer");
-  if (s && *s)
-    erase_speed = get_integer_resource("eraseSpeed", "Integer");
-  else
-    erase_speed = 400;
-  if (s) free(s);
+  st->dpy = dpy;
+  st->window = window;
 
-  s = get_string_resource("eraseMode", "Integer");
-  if (s && *s)
-    erase_mode = get_integer_resource("eraseMode", "Integer");
+  XGetWindowAttributes (dpy, window, &xgwa);
+  st->width = xgwa.width;
+  st->height = xgwa.height;
+
+  bg = get_pixel_resource (dpy, xgwa.colormap, "background", "Background");
+  fg = get_pixel_resource (dpy, xgwa.colormap, "foreground", "Foreground");
+
+  gcv.foreground = fg;
+  gcv.background = bg;
+  st->fg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
+  gcv.foreground = bg;
+  gcv.background = fg;
+  st->bg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
+
+# ifdef HAVE_COCOA
+  /* Pretty much all of these leave turds if AA is on. */
+  jwxyz_XSetAntiAliasing (st->dpy, st->fg_gc, False);
+  jwxyz_XSetAntiAliasing (st->dpy, st->bg_gc, False);
+# endif
+
+  s = get_string_resource (dpy, "eraseMode", "Integer");
+  if (!s || !*s)
+    which = -1;
   else
-    erase_mode = -1;
-  if (s) free(s);
+    which = get_integer_resource(dpy, "eraseMode", "Integer");
 
-  XGetWindowAttributes (dpy, window, &xgwa);
-  black.flags = DoRed|DoGreen|DoBlue;
-  black.red = black.green = black.blue = 0;
-  XAllocColor(dpy, xgwa.colormap, &black);
-  gcv.foreground = black.pixel;
-  erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
-  erase_window (dpy, window, erase_gc, xgwa.width, xgwa.height,
-               erase_mode, erase_speed);
-  XFreeColors(dpy, xgwa.colormap, &black.pixel, 1, 0);
-  XFreeGC(dpy, erase_gc);
+  if (which < 0 || which >= countof(erasers))
+    which = random() % countof(erasers);
+  st->fn = erasers[which];
+
+  duration = get_float_resource (dpy, "eraseSeconds", "Float");
+  if (duration < 0.1 || duration > 10)
+    duration = 1;
+
+  st->start_time = double_time();
+  st->stop_time = st->start_time + duration;
+
+  XSync (st->dpy, False);
+
+  return st;
 }
 
 
-\f
-#if 0
-#include "screenhack.h"
+static Bool
+eraser_draw (eraser_state *st, Bool first_p)
+{
+  double now = (first_p ? st->start_time : double_time());
+  double duration = st->stop_time - st->start_time;
 
-char *progclass = "Erase";
-char *defaults [] = {
-  0
-};
+  st->prev_ratio = st->ratio;
+  st->ratio = (now - st->start_time) / duration;
+
+  if (st->ratio > 1.0)
+    st->ratio = 1.0;
+
+  st->fn (st);
+  XSync (st->dpy, False);
 
-XrmOptionDescRec options [] = {{0}};
-int options_size = 0;
+  return (st->ratio < 1.0);
+}
 
-void
-screenhack (dpy, window)
-     Display *dpy;
-     Window window;
+static void
+eraser_free (eraser_state *st)
 {
-  int delay = 500000;
-  XGCValues gcv;
-  GC gc;
-  XColor white;
-  XWindowAttributes xgwa;
-  XGetWindowAttributes (dpy, window, &xgwa);
-  white.flags = DoRed|DoGreen|DoBlue;
-  white.red = white.green = white.blue = 0xFFFF;
-  XAllocColor(dpy, xgwa.colormap, &white);
-  gcv.foreground = white.pixel;
-  gc = XCreateGC (dpy, window, GCForeground, &gcv);
+  XClearWindow (st->dpy, st->window);
+  XFreeGC (st->dpy, st->fg_gc);
+  XFreeGC (st->dpy, st->bg_gc);
+  free (st);
+}
 
-  while (1)
+eraser_state *
+erase_window (Display *dpy, Window window, eraser_state *st)
+{
+  Bool first_p = False;
+  if (! st)
     {
-      XFillRectangle(dpy, window, gc, 0, 0, 1280, 1024);
-      XSync (dpy, False);
-      usleep (delay);
-      erase_full_window(dpy, window);
-      XSync (dpy, False);
-      usleep (delay);
-
+      first_p = True;
+      st = eraser_init (dpy, window);
     }
+  if (! eraser_draw (st, first_p)) 
+    {
+      eraser_free (st);
+      st = 0;
+    }
+  return st;
 }
-
-#endif