From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / loop.c
index 04419801919d5681feb0379b49a574964022e3aa..fd27fd167c63c84a96f66e9efde8c593d461ae8d 100644 (file)
@@ -1,9 +1,8 @@
 /* -*- Mode: C; tab-width: 4 -*- */
 /* loop --- Chris Langton's self-producing loops */
 
-#if !defined( lint ) && !defined( SABER )
+#if 0
 static const char sccsid[] = "@(#)loop.c       5.01 2000/03/15 xlockmore";
-
 #endif
 
 /*-
@@ -80,22 +79,24 @@ static const char sccsid[] = "@(#)loop.c    5.01 2000/03/15 xlockmore";
    there is handedness at both the initial condition and the transition rule.
  */
 
+#ifndef HAVE_JWXYZ
+# define DO_STIPPLE
+#endif
+
 #ifdef STANDALONE
-#define MODE_loop
-#define PROGCLASS "loop"
-#define HACK_INIT init_loop
-#define HACK_DRAW draw_loop
-#define loop_opts xlockmore_opts
-#define DEFAULTS "*delay: 100000 \n" \
- "*count: -5 \n" \
- "*cycles: 1600 \n" \
- "*size: -12 \n" \
- "*ncolors: 15 \n" \
- "*neighbors: 0 \n"
-#define UNIFORM_COLORS
-#include "xlockmore.h"         /* in xscreensaver distribution */
+# define MODE_loop
+# define DEFAULTS      "*delay:   100000 \n" \
+                                       "*count:   -5     \n" \
+                                       "*cycles:  1600   \n" \
+                                       "*size:    -12    \n" \
+                                       "*ncolors: 15     \n" \
+                                       "*fpsSolid: true     \n" \
+                                       "*ignoreRotation: True \n" \
+
+# define UNIFORM_COLORS
+# include "xlockmore.h"                /* in xscreensaver distribution */
 #else /* STANDALONE */
-#include "xlock.h"             /* in xlockmore distribution */
+# include "xlock.h"            /* in xlockmore distribution */
 #endif /* STANDALONE */
 #include "automata.h"
 
@@ -110,20 +111,20 @@ static int  neighbors;
 
 static XrmOptionDescRec opts[] =
 {
-       {(char *) "-neighbors", (char *) ".loop.neighbors", XrmoptionSepArg, (caddr_t) NULL}
+       {"-neighbors", ".loop.neighbors", XrmoptionSepArg, 0}
 };
 
 static argtype vars[] =
 {
-       {(caddr_t *) & neighbors, (char *) "neighbors", (char *) "Neighbors", (char *) DEF_NEIGHBORS, t_Int}
+       {&neighbors, "neighbors", "Neighbors", DEF_NEIGHBORS, t_Int}
 };
 
 static OptionStruct desc[] =
 {
-       {(char *) "-neighbors num", (char *) "squares 4 or hexagons 6"}
+       {"-neighbors num", "squares 4 or hexagons 6"}
 };
 
-ModeSpecOpt loop_opts =
+ENTRYPOINT ModeSpecOpt loop_opts =
 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
 
 
@@ -138,8 +139,8 @@ ModStruct   loop_description =
 
 #define LOOPBITS(n,w,h)\
   if ((lp->pixmaps[lp->init_bits]=\
-  XCreatePixmapFromBitmapData(display,window,(char *)n,w,h,1,0,1))==None){\
-  free_loop(display,lp); return;} else {lp->init_bits++;}
+  XCreatePixmapFromBitmapData(MI_DISPLAY(mi),window,(char *)n,w,h,1,0,1))==None){\
+  free_loop(mi); return;} else {lp->init_bits++;}
 
 static int  local_neighbors = 0;
 
@@ -859,11 +860,16 @@ drawcell(ModeInfo * mi, int col, int row, int state)
                gc = MI_GC(mi);
                XSetForeground(MI_DISPLAY(mi), gc, lp->colors[state]);
        } else {
+#ifdef DO_STIPPLE
                gcv.stipple = lp->pixmaps[state];
+#endif /* DO_STIPPLE */
                gcv.foreground = MI_WHITE_PIXEL(mi);
                gcv.background = MI_BLACK_PIXEL(mi);
                XChangeGC(MI_DISPLAY(mi), lp->stippledGC,
-                         GCStipple | GCForeground | GCBackground, &gcv);
+#ifdef DO_STIPPLE
+                         GCStipple |
+#endif /* DO_STIPPLE */
+                          GCForeground | GCBackground, &gcv);
                gc = lp->stippledGC;
        }
        fillcell(mi, gc, col, row);
@@ -911,8 +917,10 @@ free_list(loopstruct * lp)
 }
 
 static void
-free_loop(Display *display, loopstruct * lp)
+free_loop(ModeInfo * mi)
 {
+       Display    *display = MI_DISPLAY(mi);
+       loopstruct *lp = &loops[MI_SCREEN(mi)];
        int         shade;
 
        for (shade = 0; shade < lp->init_bits; shade++)
@@ -944,7 +952,7 @@ addtolist(ModeInfo * mi, int col, int row, unsigned char state)
        if ((lp->cellList[state] = (CellList *) malloc(sizeof (CellList))) ==
                        NULL) {
                lp->cellList[state] = current;
-               free_loop(MI_DISPLAY(mi), lp);
+               free_loop(mi);
                return False;
        }
        lp->cellList[state]->pt.x = col;
@@ -967,11 +975,16 @@ draw_state(ModeInfo * mi, int state)
                gc = MI_GC(mi);
                XSetForeground(display, gc, lp->colors[state]);
        } else {
+#ifdef DO_STIPPLE
                gcv.stipple = lp->pixmaps[state];
+#endif /* DO_STIPPLE */
                gcv.foreground = MI_WHITE_PIXEL(mi);
                gcv.background = MI_BLACK_PIXEL(mi);
                XChangeGC(display, lp->stippledGC,
-                         GCStipple | GCForeground | GCBackground, &gcv);
+#ifdef DO_STIPPLE
+                         GCStipple |
+#endif /* DO_STIPPLE */
+                          GCForeground | GCBackground, &gcv);
                gc = lp->stippledGC;
        }
 
@@ -1017,7 +1030,6 @@ draw_state(ModeInfo * mi, int state)
                (void) free((void *) rects);
        }
        free_state(lp, state);
-       XFlush(display);
        return True;
 }
 
@@ -1141,7 +1153,7 @@ static void
 init_adam(ModeInfo * mi)
 {
        loopstruct *lp = &loops[MI_SCREEN(mi)];
-       XPoint      start, dirx, diry;
+       XPoint      start = { 0, 0 }, dirx = { 0, 0 }, diry = { 0, 0 };
        int         i, j, dir;
 
 #ifdef DELAYDEBUGLOOP
@@ -1409,47 +1421,40 @@ do_gen(loopstruct * lp)
        }
 }
 
-void
-release_loop(ModeInfo * mi)
+ENTRYPOINT void
+release_loop (ModeInfo * mi)
 {
-       if (loops != NULL) {
-               int         screen;
-
-               for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
-                       free_loop(MI_DISPLAY(mi), &loops[screen]);
-               (void) free((void *) loops);
-               loops = (loopstruct *) NULL;
-       }
        if (table != NULL) {
                (void) free((void *) table);
                table = (unsigned int *) NULL;
        }
 }
 
-void
-init_loop(ModeInfo * mi)
+static void *stop_warning_about_triangleUnit_already;
+
+
+ENTRYPOINT void
+init_loop (ModeInfo * mi)
 {
-       Display    *display = MI_DISPLAY(mi);
-       Window      window = MI_WINDOW(mi);
        int         i, size = MI_SIZE(mi);
        loopstruct *lp;
-       XGCValues   gcv;
 
-       if (loops == NULL) {
-               if ((loops = (loopstruct *) calloc(MI_NUM_SCREENS(mi),
-                                              sizeof (loopstruct))) == NULL)
-                       return;
-       }
+    stop_warning_about_triangleUnit_already = (void *) &triangleUnit;
+
+       MI_INIT (mi, loops, free_loop);
        lp = &loops[MI_SCREEN(mi)];
 
        lp->redrawing = 0;
 
+#ifdef DO_STIPPLE
        if ((MI_NPIXELS(mi) < COLORS) && (lp->init_bits == 0)) {
-               if (lp->stippledGC == None) {
+          Window      window = MI_WINDOW(mi);
+          XGCValues   gcv;
+        if (lp->stippledGC == None) {
                        gcv.fill_style = FillOpaqueStippled;
-                       if ((lp->stippledGC = XCreateGC(display, window,
+                       if ((lp->stippledGC = XCreateGC(MI_DISPLAY(mi), window,
                                 GCFillStyle, &gcv)) == None) {
-                               free_loop(display, lp);
+                               free_loop(mi);
                                return;
                        }
                }
@@ -1462,6 +1467,7 @@ init_loop(ModeInfo * mi)
                LOOPBITS(stipples[8], STIPPLESIZE, STIPPLESIZE);
                LOOPBITS(stipples[10], STIPPLESIZE, STIPPLESIZE);
        }
+#endif /* DO_STIPPLE */
        if (MI_NPIXELS(mi) >= COLORS) {
                /* Maybe these colors should be randomized */
                lp->colors[0] = MI_BLACK_PIXEL(mi);
@@ -1557,7 +1563,7 @@ init_loop(ModeInfo * mi)
        }
        if ((lp->oldcells = (unsigned char *) calloc(lp->bncols * lp->bnrows,
                        sizeof (unsigned char))) == NULL) {
-               free_loop(display, lp);
+               free_loop(mi);
                return;
        }
        if (lp->newcells != NULL) {
@@ -1566,7 +1572,7 @@ init_loop(ModeInfo * mi)
        }
        if ((lp->newcells = (unsigned char *) calloc(lp->bncols * lp->bnrows,
                        sizeof (unsigned char))) == NULL) {
-               free_loop(display, lp);
+               free_loop(mi);
                return;
        }
        if (!init_table()) {
@@ -1592,8 +1598,8 @@ init_loop(ModeInfo * mi)
        init_adam(mi);
 }
 
-void
-draw_loop(ModeInfo * mi)
+ENTRYPOINT void
+draw_loop (ModeInfo * mi)
 {
        int         offset, i, j;
        unsigned char *z, *znew;
@@ -1636,7 +1642,7 @@ draw_loop(ModeInfo * mi)
        }
        for (i = 0; i < COLORS; i++)
                if (!draw_state(mi, i)) {
-                       free_loop(MI_DISPLAY(mi), lp);
+                       free_loop(mi);
                        return;
                }
        if (++lp->generation > MI_CYCLES(mi) || lp->dead) {
@@ -1660,8 +1666,15 @@ draw_loop(ModeInfo * mi)
        }
 }
 
-void
-refresh_loop(ModeInfo * mi)
+ENTRYPOINT void
+reshape_loop(ModeInfo * mi, int width, int height)
+{
+  XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
+  init_loop (mi);
+}
+
+ENTRYPOINT void
+refresh_loop (ModeInfo * mi)
 {
        loopstruct *lp;
 
@@ -1674,4 +1687,17 @@ refresh_loop(ModeInfo * mi)
        lp->redrawpos = lp->by * lp->ncols + lp->bx;
 }
 
+ENTRYPOINT Bool
+loop_handle_event (ModeInfo *mi, XEvent *event)
+{
+  if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
+    {
+      reshape_loop (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+      return True;
+    }
+  return False;
+}
+
+XSCREENSAVER_MODULE ("Loop", loop)
+
 #endif /* MODE_loop */