http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.14.tar.gz
[xscreensaver] / hacks / helix.c
index 758c5557e1a7ef1d57cf44688603a088753576cc..7ca03da7760377aed93d87006bac39bb71ae977d 100644 (file)
@@ -1,5 +1,5 @@
 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997
- *  Jamie Zawinski <jwz@netscape.com>
+ *  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
 
 /* Algorithm from a Mac program by Chris Tate, written in 1988 or so. */
 
-/* 10-May-97: merged ellipse code by Dan Stromberg <strombrg@nis.acs.uci.edu>
+/* 18-Sep-97: Johannes Keukelaar (johannes@nada.kth.se): Improved screen
+ *            eraser.
+ * 10-May-97: merged ellipse code by Dan Stromberg <strombrg@nis.acs.uci.edu>
  *            as found in xlockmore 4.03a10.
  * 1992:      jwz created.
  */
 
+/* 25 April 2002: Matthew Strait <straitm@mathcs.carleton.edu> added
+-subdelay option so the drawing process can be watched */
+
 #include <math.h>
 #include "screenhack.h"
+#include "erase.h"
 
 static double sins [360];
 static double coss [360];
 
-static GC draw_gc, erase_gc;
+static GC draw_gc;
 static unsigned int default_fg_pixel;
+static int sleep_time;
+static int subdelay;
 
 static void
 init_helix (Display *dpy, Window window)
@@ -39,7 +47,6 @@ init_helix (Display *dpy, Window window)
     get_pixel_resource ("foreground", "Foreground", dpy, cmap);
   draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
   gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
-  erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
 
   for (i = 0; i < 360; i++)
     {
@@ -96,9 +103,12 @@ helix (Display *dpy, Window window,
       XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
       x2 = xmid + (((double) radius2) * sins [pmod ((angle * factor3), 360)]);
       y2 = ymid + (((double) radius1) * coss [pmod ((angle * factor4), 360)]);
-
       XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
       angle += d_angle;
+
+      /* if we sleep every time, it's too slow */
+      if(subdelay && i%16 == 0) usleep(subdelay);
+
       XFlush (dpy);
     }
 }
@@ -136,6 +146,12 @@ trig (Display *dpy, Window window,
       if (tmp == 0)    /* Do not want it getting stuck... */
        tmp = 1;        /* Would not need if floating point */
       d_angle += dir * tmp;
+
+      /* this draws faster, so we sleep somewhat more often */
+      if(subdelay && d_angle%4 == 0) usleep(subdelay);
+      
+      /* without this, the subdelay effect is lost */
+      XFlush (dpy);
     }
 }
 
@@ -250,7 +266,6 @@ random_trig (Display *dpy, Window window, XColor *color, Bool *got_color)
 static void
 random_helix_or_trig (Display *dpy, Window window)
 {
-  int i;
   Bool free_color = False;
   XColor color;
   int width, height;
@@ -266,20 +281,16 @@ random_helix_or_trig (Display *dpy, Window window)
   else
     random_trig(dpy, window, &color, &free_color);
 
-  XSync (dpy, True);
-  sleep (5);
+  XSync (dpy, False);
+  screenhack_handle_events (dpy);
+  sleep ( sleep_time );
+
+  screenhack_handle_events (dpy);
+  erase_full_window(dpy, window);
 
-  for (i = 0; i < height; i++)
-    {
-      int y = (random () % height);
-      XDrawLine (dpy, window, erase_gc, 0, y, width, y);
-      XFlush (dpy);
-      if ((i % 50) == 0)
-       usleep (10000);
-    }
-  XClearWindow (dpy, window);
   if (free_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
-  XSync (dpy, True);
+  XSync (dpy, False);
+  screenhack_handle_events (dpy);
   sleep (1);
 }
 
@@ -287,15 +298,24 @@ random_helix_or_trig (Display *dpy, Window window)
 char *progclass = "Helix";
 
 char *defaults [] = {
-  "Helix.background: black",           /* to placate SGI */
+  ".background: black",
+  "*delay:      5",
+  "*subdelay:  0",
   0
 };
 
-XrmOptionDescRec options [] = { { 0, } };
+XrmOptionDescRec options [] = {   
+  { "-delay",           ".delay",               XrmoptionSepArg, 0 },
+  { "-subdelay",        ".subdelay",            XrmoptionSepArg, 0 },
+  { 0,                 0,                      0,               0 },
+};
+int options_size = (sizeof (options) / sizeof (options[0]));
 
 void
 screenhack (Display *dpy, Window window)
 {
+  sleep_time = get_integer_resource("delay", "Integer");
+  subdelay = get_integer_resource("subdelay", "Integer");
   init_helix (dpy, window);
   while (1)
     random_helix_or_trig (dpy, window);