http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.23.tar.gz
[xscreensaver] / hacks / xlyap.c
index 908d8d82374207bac9991675d9691cae807e6706..3af8850725c2fa655225f6d943fd857461aa2f08 100644 (file)
@@ -39,7 +39,6 @@
 #include "screenhack.h"
 #include "yarandom.h"
 #include "hsv.h"
-#include "vroot.h"
 
 #include <X11/cursorfont.h> 
 #include <X11/Xutil.h> 
@@ -218,7 +217,7 @@ static double  minexp, maxexp, prob=0.5;
 static int     expind[MAXFRAMES]={0}, resized[MAXFRAMES]={0};
 static int     numwheels=MAXWHEELS, force=0, Force=0, negative=1;
 static int     rgb_max=65000, nostart=1, stripe_interval=7;
-static int     save=1, show=0, useprod=1, spinlength=256, savefile=0;
+static int     save=1, show=0, useprod=1, spinlength=256/*, savefile=0*/;
 static int     maxframe=0, frame=0, dorecalc=0, mapindex=0, run=1;
 static char    *outname="lyap.out";
 
@@ -247,7 +246,7 @@ static void main_event(void);
 static int complyap(void);
 static void Getkey(XKeyEvent *event);
 static int sendpoint(double expo);
-static void save_to_file(void);
+/*static void save_to_file(void);*/
 static void setforcing(void);
 static void check_params(int mapnum, int parnum);
 static void usage(void);
@@ -534,17 +533,13 @@ screenhack (Display *d, Window window)
   canvas = window;
   init_canvas();
 
-  if (window != DefaultRootWindow(dpy))
-    XSelectInput(dpy,canvas,KeyPressMask|ButtonPressMask|ButtonMotionMask|
-                ButtonReleaseMask|ExposureMask|StructureNotifyMask);
   if (displayplanes > 1) {
     init_color();
   } else {
     XQueryColors(dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
        Colors, numcolors);
   }
-  pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy),
-                        width, height, DefaultDepth(dpy, screen));
+  pixmap = XCreatePixmap(dpy, window, width, height, xgwa.depth);
   rubber_data.band_cursor = XCreateFontCursor(dpy, XC_hand2);
   CreateXorGC();
   Clear();
@@ -582,6 +577,9 @@ main_event(void)
          break;
            case ButtonRelease:
     EndRubberBand(canvas, &rubber_data, &event);
+         break;
+            default: 
+    screenhack_handle_event (dpy, &event);
          break;
            }
        }
@@ -596,25 +594,26 @@ main_event(void)
 static int
 complyap(void)
 {
-  register i, bindex;
-  double total, prod, x, r;
+  int i, bindex;
+  double total, prod, x, dx, r;
 
   if (!run)
     return TRUE;
   a += a_inc;
-  if (a >= max_a)
+  if (a >= max_a) {
     if (sendpoint(lyapunov) == TRUE)
       return FALSE;
     else {
       FlushBuffer();
-      if (savefile)
-       save_to_file();
+/*      if (savefile)
+       save_to_file(); */
       return TRUE;
     }
+  }
   if (b >= max_b) {
     FlushBuffer();
-    if (savefile)
-      save_to_file();
+/*    if (savefile)
+      save_to_file();*/
     return TRUE;
   }
   prod = 1.0;
@@ -646,7 +645,14 @@ complyap(void)
   if (useprod) {      /* using log(a*b) */
     for (i=0;i<dwell;i++) {
       x = (*map)(x, r);
-      prod *= ABS((*deriv)(x, r));
+      dx = (*deriv)(x, r); /* ABS is a macro, so don't be fancy */
+      dx = ABS(dx);
+      if (dx == 0.0) /* log(0) is nasty so break out. */
+      {
+        i++;
+        break;
+      }
+      prod *= dx;
       /* we need to prevent overflow and underflow */
       if ((prod > 1.0e12) || (prod < 1.0e-12)) {
        total += log(prod);
@@ -666,12 +672,19 @@ complyap(void)
 #endif
     }
     total += log(prod);
-    lyapunov = (total * M_LOG2E) / (double)dwell;
+    lyapunov = (total * M_LOG2E) / (double)i;   
   }
   else {       /* use log(a) + log(b) */
     for (i=0;i<dwell;i++) {
       x = (*map)(x, r);
-      total += log(ABS((*deriv)(x, r)));
+      dx = (*deriv)(x, r); /* ABS is a macro, so don't be fancy */
+      dx = ABS(dx);
+      if (x == 0.0)  /* log(0) check */
+      {
+        i++;
+        break;
+      }
+      total += log(dx);
       if (++bindex >= maxindex) {
        bindex = 0;
        if (Rflag)
@@ -685,14 +698,15 @@ complyap(void)
       deriv = Derivs[Forcing[findex]];
 #endif
     }
-    lyapunov = (total * M_LOG2E) / (double)dwell;
+    lyapunov = (total * M_LOG2E) / (double)i;
   }
+
   if (sendpoint(lyapunov) == TRUE)
     return FALSE;
   else {
     FlushBuffer();
-    if (savefile)
-      save_to_file();
+/*    if (savefile)
+      save_to_file();*/
     return TRUE;
   }
 }
@@ -797,7 +811,7 @@ init_canvas(void)
   * create default, writable, graphics contexts for the canvas.
   */
        for (i=0; i<maxcolor; i++) {
-           Data_GC[i] = XCreateGC(dpy, DefaultRootWindow(dpy),
+           Data_GC[i] = XCreateGC(dpy, canvas,
                (unsigned long) NULL, (XGCValues *) NULL);
            /* set the background to black */
            XSetBackground(dpy,Data_GC[i],BlackPixel(dpy,XDefaultScreen(dpy)));
@@ -1302,7 +1316,7 @@ Getkey(XKeyEvent *event)
       redraw(exponents[frame], expind[frame], 1);
       break;
   case 'f':
-  case 'F': save_to_file(); break;
+/*  case 'F': save_to_file(); break;*/
   case 'i': if (stripe_interval > 0) {
       stripe_interval--;
        if (displayplanes > 1) {
@@ -1422,6 +1436,15 @@ sendpoint(double expo)
   static int index;
   static double tmpexpo;
 
+#if 0
+/* The relationship minexp <= expo <= maxexp should always be true. This test
+   enforces that. But maybe not enforcing it makes better pictures. */
+  if (expo < minexp)
+    expo = minexp;
+  else if (expo > maxexp)
+    expo = maxexp;
+#endif
+
   point.x++;
   tmpexpo = (negative) ? expo : -1.0 * expo;
   if (tmpexpo > 0) {
@@ -1484,8 +1507,7 @@ resize(void)
   XClearWindow(dpy, canvas);
   if (pixmap)
     XFreePixmap(dpy, pixmap);
-  pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy),
-      width, height, DefaultDepth(dpy, screen));
+  pixmap = XCreatePixmap(dpy, canvas, width, height, d);
   a_inc = a_range / (double)width;
   b_inc = b_range / (double)height;
   point.x = -1;
@@ -1546,6 +1568,7 @@ Redraw(void)
   resized[frame] = 0;
 }
 
+#if 0
 /* Store color pics in PPM format and monochrome in PGM */
 static void
 save_to_file(void)
@@ -1621,6 +1644,7 @@ save_to_file(void)
       }
   fclose(outfile);
 }
+#endif
 
 static void
 recalc(void)
@@ -1666,7 +1690,7 @@ CreateXorGC(void)
   values.foreground = foreground;
   values.line_style = LineSolid;
   values.function = GXxor;
-  RubberGC = XCreateGC(dpy, DefaultRootWindow(dpy),
+  RubberGC = XCreateGC(dpy, canvas,
        GCForeground | GCBackground | GCFunction | GCLineStyle, &values);
 }
 
@@ -1854,6 +1878,14 @@ InitBuffer(void)
 static void
 BufferPoint(Display *display, Window window, int color, int x, int y)
 {
+
+/* Guard against bogus color values. Shouldn't be necessary but paranoia
+   is good. */
+  if (color < 0)
+    color = 0;
+  else if (color >= maxcolor)
+    color = maxcolor - 1;
+
   if (Points.npoints[color] == MAXPOINTS)
   {
     XDrawPoints(display, window, Data_GC[color],