http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / hacks / coral.c
index 74b5120b2609eed4f23c165f9476fb2e5092594f..e17d4951f471db1634077d4ca7a7c6a93bb1507b 100644 (file)
@@ -21,12 +21,12 @@ static int ncolors = 0;
 static int colorindex = 0;
 static int colorsloth;
 
-static XPoint *walkers;
+static XPoint *walkers = 0;
 static int nwalkers;
 static int width, widthb;
 static int height;
 
-static unsigned int *board;
+static unsigned int *board = 0;
 #define getdot(x,y) (board[(y*widthb)+(x>>5)] &  (1<<(x & 31)))
 #define setdot(x,y) (board[(y*widthb)+(x>>5)] |= (1<<(x & 31)))
 
@@ -47,6 +47,7 @@ init_coral(Display *dpy, Window window)
     width = xgwa.width;
     widthb = ((xgwa.width + 31) >> 5);
     height = xgwa.height;
+    if (board) free(board);
     board = (unsigned int *)calloc(widthb * xgwa.height, sizeof(unsigned int));
     if(!board) exit(1);
     cmap = xgwa.colormap;
@@ -73,6 +74,7 @@ init_coral(Display *dpy, Window window)
     if( density < 1 ) density = 1;
     if( density > 100 ) density = 90; /* more like mold than coral */
     nwalkers = (width*height*density)/100;
+    if (walkers) free(walkers);
     walkers = (XPoint *)calloc(nwalkers, sizeof(XPoint));
     if( (XPoint *)0 == walkers ) exit(1);
 
@@ -86,8 +88,8 @@ init_coral(Display *dpy, Window window)
     for( i = 0; i < seeds; i++ ) {
         int x, y;
         do {
-            x = random() % width;
-            y = random() % height;
+          x = 1 + random() % (width - 2);
+          y = 1 + random() % (height - 2);
         } while( getdot(x, y) );
 
         setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
@@ -168,7 +170,7 @@ coral(Display *dpy, Window window)
                  XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
                              CoordModeOrigin);
                  npoints = 0;
-                 XSync(dpy, True);
+                 XSync(dpy, False);
                }
 
                if (color) {
@@ -179,7 +181,7 @@ coral(Display *dpy, Window window)
                 }
 
                 if( 0 == nwalkers ) {
-                    XSync(dpy, True);
+                    XSync(dpy, False);
                    free(pointbuf);
                     return;
                 }
@@ -215,8 +217,9 @@ coral(Display *dpy, Window window)
            XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
                        CoordModeOrigin);
            npoints = 0;
-           XSync(dpy, True);
+           XSync(dpy, False);
          }
+          screenhack_handle_events (dpy);
          usleep(delay2);
        }
     }
@@ -225,13 +228,13 @@ coral(Display *dpy, Window window)
 char *progclass = "Coral";
 
 char *defaults[] = {
-    "Coral.background: black",
-    "Coral.foreground: white",
-    "*density: 25",
-    "*seeds: 20", /* too many for 640x480, too few for 1280x1024 */
-    "*delay: 5",
-    "*delay2: 1000",
-    0
+  ".background:        black",
+  ".foreground:        white",
+  "*density:   25",
+  "*seeds:     20", /* too many for 640x480, too few for 1280x1024 */
+  "*delay:     5",
+  "*delay2:    1000",
+  0
 };
 
 XrmOptionDescRec options[] = {
@@ -251,6 +254,7 @@ Window window;
     while( 1 ) {
         init_coral(dpy, window);
         coral(dpy, window);
+        screenhack_handle_events (dpy);
         if( delay ) sleep(delay);
        erase_full_window(dpy, window);
     }