From http://www.jwz.org/xscreensaver/xscreensaver-5.18.tar.gz
[xscreensaver] / hacks / interference.c
index 7f66b01b0c99b01bafbce687878b79eb3cabd789..6060798181928ddc2482e2a95a5e8a8fa95ea2a2 100644 (file)
@@ -24,6 +24,9 @@
  *
  * Created      : Wed Apr 22 09:30:30 1998, hmallat
  * Last modified: Wed Apr 22 09:30:30 1998, hmallat
+ * Last modified: Sun Aug 31 23:40:14 2003, 
+ *              david slimp <rock808@DavidSlimp.com>
+ *             added -hue option to specify base color hue
  *
  * TODO:
  *
@@ -48,8 +51,6 @@
 
 #include "screenhack.h"
 
-# include <X11/Xutil.h>
-
 /* I thought it would be faster this way, but it turns out not to be... -jwz */
 #undef USE_XIMAGE
 
 # include "xshm.h"
 #endif /* HAVE_XSHM_EXTENSION */
 
-char *progclass="Interference";
-
-char *defaults [] = {
+static const char *interference_defaults [] = {
+  ".background:  black",
+  ".foreground:  white",
   "*count:       3",     /* number of waves */
   "*gridsize:    4",     /* pixel size, smaller values for better resolution */
   "*ncolors:     128",   /* number of colours used */
+  "*hue:         0",     /* hue to use for base color (0-360) */
   "*speed:       30",    /* speed of wave origins moving around */
   "*delay:       30000", /* or something */
   "*color-shift: 60",    /* h in hsv space, smaller values for smaller
@@ -91,10 +93,11 @@ char *defaults [] = {
   0
 };
 
-XrmOptionDescRec options [] = {
+static XrmOptionDescRec interference_options [] = {
   { "-count",       ".count",       XrmoptionSepArg, 0 }, 
   { "-ncolors",     ".ncolors",     XrmoptionSepArg, 0 }, 
   { "-gridsize",    ".gridsize",    XrmoptionSepArg, 0 }, 
+  { "-hue",         ".hue",         XrmoptionSepArg, 0 },
   { "-speed",       ".speed",       XrmoptionSepArg, 0 },
   { "-delay",       ".delay",       XrmoptionSepArg, 0 },
   { "-color-shift", ".color-shift", XrmoptionSepArg, 0 },
@@ -110,8 +113,6 @@ XrmOptionDescRec options [] = {
   { 0, 0, 0, 0 }
 };
 
-int options_size = (sizeof (options) / sizeof (XrmOptionDescRec));
-
 struct inter_source {
   int x; 
   int y;
@@ -147,6 +148,7 @@ struct inter_context {
   int count;
   int grid_size;
   int colors;
+  float hue;
   int speed;
   int delay;
   int shift;
@@ -179,7 +181,7 @@ struct inter_context {
 # define TARGET(c) ((c)->pix_buf ? (c)->pix_buf : (c)->win)
 #endif /* !HAVE_DOUBLE_BUFFER_EXTENSION */
 
-void inter_init(Display* dpy, Window win, struct inter_context* c) 
+static void inter_init(Display* dpy, Window win, struct inter_context* c) 
 {
   XWindowAttributes xgwa;
   double H[3], S[3], V[3];
@@ -188,20 +190,27 @@ void inter_init(Display* dpy, Window win, struct inter_context* c)
   int gray;
   XGCValues val;
   unsigned long valmask = 0;
-  Bool dbuf = get_boolean_resource ("doubleBuffer", "Boolean");
+  Bool dbuf = get_boolean_resource (dpy, "doubleBuffer", "Boolean");
+
+# ifdef HAVE_COCOA     /* Don't second-guess Quartz's double-buffering */
+  dbuf = False;
+# endif
 
   memset (c, 0, sizeof(*c));
 
   c->dpy = dpy;
   c->win = win;
 
+  c->delay = get_integer_resource(dpy, "delay", "Integer");
+
+
   XGetWindowAttributes(c->dpy, c->win, &xgwa);
   c->w = xgwa.width;
   c->h = xgwa.height;
   c->cmap = xgwa.colormap;
 
 #ifdef HAVE_XSHM_EXTENSION
-  c->use_shm = get_boolean_resource("useSHM", "Boolean");
+  c->use_shm = get_boolean_resource(dpy, "useSHM", "Boolean");
 #endif /*  HAVE_XSHM_EXTENSION */
 
   if (dbuf)
@@ -220,25 +229,28 @@ void inter_init(Display* dpy, Window win, struct inter_context* c)
   val.function = GXcopy;
   c->copy_gc = XCreateGC(c->dpy, TARGET(c), GCFunction, &val);
 
-  c->count = get_integer_resource("count", "Integer");
+  c->count = get_integer_resource(dpy, "count", "Integer");
   if(c->count < 1)
     c->count = 1;
-  c->grid_size = get_integer_resource("gridsize", "Integer");
+  c->grid_size = get_integer_resource(dpy, "gridsize", "Integer");
   if(c->grid_size < 1)
     c->grid_size = 1;
-  mono = get_boolean_resource("mono", "Boolean");
+  mono = get_boolean_resource(dpy, "mono", "Boolean");
   if(!mono) {
-    c->colors = get_integer_resource("ncolors", "Integer");
+    c->colors = get_integer_resource(dpy, "ncolors", "Integer");
     if(c->colors < 2)
       c->colors = 2;
   }
-  c->speed = get_integer_resource("speed", "Integer");
-  c->shift = get_float_resource("color-shift", "Float");
+  c->hue = get_integer_resource(dpy, "hue", "Float");
+  while (c->hue < 0 || c->hue >= 360)
+    c->hue = frand(360.0);
+  c->speed = get_integer_resource(dpy, "speed", "Integer");
+  c->shift = get_float_resource(dpy, "color-shift", "Float");
   while(c->shift >= 360.0)
     c->shift -= 360.0;
   while(c->shift <= -360.0)
     c->shift += 360.0;
-  c->radius = get_integer_resource("radius", "Integer");;
+  c->radius = get_integer_resource(dpy, "radius", "Integer");;
   if(c->radius < 1)
     c->radius = 1;
 
@@ -272,9 +284,9 @@ void inter_init(Display* dpy, Window win, struct inter_context* c)
   if(!mono) {
     c->pal = calloc(c->colors, sizeof(XColor));
 
-    gray = get_boolean_resource("gray", "Boolean");
+    gray = get_boolean_resource(dpy, "gray", "Boolean");
     if(!gray) {
-      H[0] = frand(360.0); 
+      H[0] = c->hue;
       H[1] = H[0] + c->shift < 360.0 ? H[0]+c->shift : H[0] + c->shift-360.0; 
       H[2] = H[1] + c->shift < 360.0 ? H[1]+c->shift : H[1] + c->shift-360.0; 
       S[0] = S[1] = S[2] = 1.0;
@@ -342,7 +354,7 @@ void inter_init(Display* dpy, Window win, struct inter_context* c)
  * it, go ahead! 
  */
 
-void do_inter(struct inter_context* c) 
+static void do_inter(struct inter_context* c) 
 {
   int i, j, k;
   int result;
@@ -371,7 +383,7 @@ void do_inter(struct inter_context* c)
        dx = i*g + g/2 - c->source[k].x;
        dy = j*g + g/2 - c->source[k].y;
        dist = sqrt(dx*dx + dy*dy); /* what's the performance penalty here? */
-       result += (dist > c->radius ? 0 : c->wave_height[dist]);
+       result += (dist >= c->radius ? 0 : c->wave_height[dist]);
       }
       result %= c->colors;
 
@@ -423,21 +435,42 @@ void do_inter(struct inter_context* c)
         XCopyArea (c->dpy, c->pix_buf, c->win, c->copy_gc,
                    0, 0, c->w, c->h, 0, 0);
       }
+}
 
-  XSync(c->dpy, False);
+static void *
+interference_init (Display *dpy, Window win)
+{
+  struct inter_context *c = (struct inter_context *) calloc (1, sizeof(*c));
+  inter_init(dpy, win, c);
+  return c;
 }
 
-void screenhack(Display *dpy, Window win) 
+static unsigned long
+interference_draw (Display *dpy, Window win, void *closure)
 {
-  struct inter_context c;
-  int delay;
+  struct inter_context *c = (struct inter_context *) closure;
+  do_inter(c);
+  return c->delay;
+}
 
-  delay = get_integer_resource("delay", "Integer");
+static void
+interference_reshape (Display *dpy, Window window, void *closure, 
+                 unsigned int w, unsigned int h)
+{
+  struct inter_context *c = (struct inter_context *) closure;
+  c->w = w;
+  c->h = h;
+}
 
-  inter_init(dpy, win, &c);
-  while(1) {
-    do_inter(&c); 
-    screenhack_handle_events (dpy);
-    if(delay) usleep(delay);
-  }
+static Bool
+interference_event (Display *dpy, Window window, void *closure, XEvent *event)
+{
+  return False;
 }
+
+static void
+interference_free (Display *dpy, Window window, void *closure)
+{
+}
+
+XSCREENSAVER_MODULE ("Interference", interference)