From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / crystal.c
index 80bcca5b8ebb3de4841e459f80dc7894d3cef5be..9f33529c5ebdb52f96c5a621394cdd8ab43da2e6 100644 (file)
@@ -69,17 +69,17 @@ static const char sccsid[] = "@(#)crystal.c 4.12 98/09/10 xlockmore";
  */
 
 #ifdef STANDALONE
-# define PROGCLASS             "Crystal"
-# define HACK_INIT             init_crystal
-# define HACK_DRAW             draw_crystal
-# define crystal_opts  xlockmore_opts
 # define DEFAULTS              "*delay:                60000   \n" \
                                                 "*count:                -500   \n" \
                                                 "*cycles:                200   \n" \
                                                 "*size:                  -15   \n" \
                                                 "*ncolors:               100   \n" \
-                                                "*fullrandom:   True   \n" \
-                                                "*verbose:             False   \n"
+                                                "*fpsSolid:       True \n" \
+                                                "*ignoreRotation: True \n" \
+
+# define release_crystal 0
+# define reshape_crystal 0
+# define crystal_handle_event 0
 # include "xlockmore.h"                /* in xscreensaver distribution */
 #else /* STANDALONE */
 # include "xlock.h"                    /* in xlockmore distribution */
@@ -96,11 +96,10 @@ static const char sccsid[] = "@(#)crystal.c 4.12 98/09/10 xlockmore";
 #define DEF_MAXSIZE "False"
 #define DEF_CYCLE "True"
 
-#define min(a,b) ((a) <= (b) ? (a) : (b))
+#undef NRAND
+#define NRAND(n)           ( (n) ? (int) (LRAND() % (n)) : 0)
 
-#ifdef STANDALONE
-void release_crystal(ModeInfo * mi);
-#endif
+#define min(a,b) ((a) <= (b) ? (a) : (b))
 
 static int  nx, ny;
 
@@ -108,29 +107,29 @@ static Bool unit_cell, grid_cell, centre, maxsize, cycle_p;
 
 static XrmOptionDescRec opts[] =
 {
-       {"-nx", "crystal.nx", XrmoptionSepArg, (caddr_t) NULL},
-       {"-ny", "crystal.ny", XrmoptionSepArg, (caddr_t) NULL},
-       {"-centre", ".crystal.centre", XrmoptionNoArg, (caddr_t) "on"},
-       {"+centre", ".crystal.centre", XrmoptionNoArg, (caddr_t) "off"},
-       {"-maxsize", ".crystal.maxsize", XrmoptionNoArg, (caddr_t) "on"},
-       {"+maxsize", ".crystal.maxsize", XrmoptionNoArg, (caddr_t) "off"},
-       {"-cell", ".crystal.cell", XrmoptionNoArg, (caddr_t) "on"},
-       {"+cell", ".crystal.cell", XrmoptionNoArg, (caddr_t) "off"},
-       {"-grid", ".crystal.grid", XrmoptionNoArg, (caddr_t) "on"},
-       {"+grid", ".crystal.grid", XrmoptionNoArg, (caddr_t) "off"},
-       {"-shift", ".crystal.shift", XrmoptionNoArg, (caddr_t) "on"},
-       {"+shift", ".crystal.shift", XrmoptionNoArg, (caddr_t) "off"}
+       {"-nx", "crystal.nx", XrmoptionSepArg, 0},
+       {"-ny", "crystal.ny", XrmoptionSepArg, 0},
+       {"-centre", ".crystal.centre", XrmoptionNoArg, "on"},
+       {"+centre", ".crystal.centre", XrmoptionNoArg, "off"},
+       {"-maxsize", ".crystal.maxsize", XrmoptionNoArg, "on"},
+       {"+maxsize", ".crystal.maxsize", XrmoptionNoArg, "off"},
+       {"-cell", ".crystal.cell", XrmoptionNoArg, "on"},
+       {"+cell", ".crystal.cell", XrmoptionNoArg, "off"},
+       {"-grid", ".crystal.grid", XrmoptionNoArg, "on"},
+       {"+grid", ".crystal.grid", XrmoptionNoArg, "off"},
+       {"-shift", ".crystal.shift", XrmoptionNoArg, "on"},
+       {"+shift", ".crystal.shift", XrmoptionNoArg, "off"}
 };
 
 static argtype vars[] =
 {
-       {(caddr_t *) & nx, "nx", "nx", DEF_NX, t_Int},
-       {(caddr_t *) & ny, "ny", "ny", DEF_NY, t_Int},
-       {(caddr_t *) & centre, "centre", "Centre", DEF_CENTRE, t_Bool},
-       {(caddr_t *) & maxsize, "maxsize", "Maxsize", DEF_MAXSIZE, t_Bool},
-       {(caddr_t *) & unit_cell, "cell", "Cell", DEF_CELL, t_Bool},
-       {(caddr_t *) & grid_cell, "grid", "Grid", DEF_GRID, t_Bool},
-       {(caddr_t *) & cycle_p, "shift", "Shift", DEF_CYCLE, t_Bool}
+       {&nx, "nx", "nx", DEF_NX, t_Int},
+       {&ny, "ny", "ny", DEF_NY, t_Int},
+       {&centre, "centre", "Centre", DEF_CENTRE, t_Bool},
+       {&maxsize, "maxsize", "Maxsize", DEF_MAXSIZE, t_Bool},
+       {&unit_cell, "cell", "Cell", DEF_CELL, t_Bool},
+       {&grid_cell, "grid", "Grid", DEF_GRID, t_Bool},
+       {&cycle_p, "shift", "Shift", DEF_CYCLE, t_Bool}
 };
 static OptionStruct desc[] =
 {
@@ -143,13 +142,13 @@ static OptionStruct desc[] =
        {"-/+shift", "turn on/off colour cycling"}
 };
 
-ModeSpecOpt crystal_opts =
+ENTRYPOINT ModeSpecOpt crystal_opts =
 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
 
 #ifdef USE_MODULES
 ModStruct   crystal_description =
-{"crystal", "init_crystal", "draw_crystal", "release_crystal",
- "refresh_crystal", "init_crystal", NULL, &crystal_opts,
+{"crystal", "init_crystal", "draw_crystal", NULL,
+ "refresh_crystal", "init_crystal", "free_crystal", &crystal_opts,
  60000, -40, 200, -15, 64, 1.0, "",
  "Shows polygons in 2D plane groups", 0, NULL};
 
@@ -269,6 +268,8 @@ typedef struct {
        Bool        cycle_p, mono_p, no_colors;
        unsigned long blackpixel, whitepixel, fg, bg;
        int         direction, invert;
+       unsigned long grid_pixel;
+       int         inx, iny;
 } crystalstruct;
 
 static crystalstruct *crystals = NULL;
@@ -560,25 +561,173 @@ crystal_drawatom(ModeInfo * mi, crystalatom * atom0)
        }
 }
 
-void
+ENTRYPOINT void init_crystal(ModeInfo * mi);
+
+
+ENTRYPOINT void
 draw_crystal(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
+       Window      window = MI_WINDOW(mi);
        crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
        int         i;
 
+#ifdef HAVE_JWXYZ      /* Don't second-guess Quartz's double-buffering */
+    XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
+#endif
+
        if (cryst->no_colors) {
-               release_crystal(mi);
                init_crystal(mi);
                return;
        }
-       cryst->painted = True;
-       MI_IS_DRAWN(mi) = True;
+
+       /* Moved from init_crystal because you can't draw after MI_CLEARWINDOW in
+          XScreenSaver. - Dave Odell <dmo2118@gmail.com>
+        */
+       if (cryst->unit_cell
+#ifndef HAVE_JWXYZ
+               && !cryst->painted
+#endif
+               ) {
+          int y_coor1 , y_coor2;
+          
+               XSetForeground(display, cryst->gc, cryst->grid_pixel);
+               if (cryst->grid_cell) {
+                       int         inx, iny;
+
+                  if ( cryst->invert )
+                    y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
+                  else
+                    y_coor1 = y_coor2 = cryst->offset_h;
+                       XDrawLine(display, window, cryst->gc, cryst->offset_w,
+                                 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
+                                 y_coor2);
+                  if ( cryst->invert )
+                    {
+                       y_coor1 = cryst->win_height - cryst->offset_h;
+                       y_coor2 = cryst->win_height - (int) (cryst->ny *
+                                                            cryst->b *
+                                        cos((cryst->gamma - 90) * PI_RAD)) -
+                         cryst->offset_h;
+                    }
+                  else
+                    {
+                       y_coor1 = cryst->offset_h;
+                       y_coor2 = (int) (cryst->ny * cryst->b *
+                                        cos((cryst->gamma - 90) * PI_RAD)) +
+                         cryst->offset_h;
+                    }
+                       XDrawLine(display, window, cryst->gc, cryst->offset_w,
+                                 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
+                                         sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor2);
+                       inx = cryst->nx;
+                       for (iny = 1; iny <= cryst->ny; iny++) {
+                  if ( cryst->invert )
+                    {
+                       y_coor1 = cryst->win_height -
+                         (int) (iny * cryst->b * cos((cryst->gamma - 90) *
+                                                 PI_RAD)) - cryst->offset_h;
+                       y_coor2 = cryst->win_height -
+                         (int) (iny * cryst->b * cos((cryst->gamma - 90) *
+                                                     PI_RAD)) -
+                         cryst->offset_h;
+                    }
+                  else
+                    {
+                       y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
+                                                 PI_RAD)) + cryst->offset_h;
+                       y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
+                                         cryst->offset_h;
+                    }
+                               XDrawLine(display, window, cryst->gc,
+                                         (int) (cryst->offset_w +
+                                    inx * cryst->a - (int) (iny * cryst->b *
+                                        sin((cryst->gamma - 90) * PI_RAD))),
+                                         y_coor1,
+                                   (int) (cryst->offset_w - iny * cryst->b *
+                                          sin((cryst->gamma - 90) * PI_RAD)),
+                                         y_coor2);
+                       }
+                       iny = cryst->ny;
+                       for (inx = 1; inx <= cryst->nx; inx++) {
+                          if ( cryst->invert )
+                            {
+                               y_coor1 =cryst->win_height -
+                                 (int) (iny * cryst->b *
+                                               cos((cryst->gamma - 90) *
+                                                   PI_RAD)) - cryst->offset_h;
+                               y_coor2 =cryst->win_height - cryst->offset_h;
+                            }
+                          else
+                            {
+                               y_coor1 =(int) (iny * cryst->b *
+                                               cos((cryst->gamma - 90) *
+                                                   PI_RAD)) + cryst->offset_h;
+                               y_coor2 =cryst->offset_h;
+                            }
+                               XDrawLine(display, window, cryst->gc,
+                                         (int) (cryst->offset_w +
+                                    inx * cryst->a - (int) (iny * cryst->b *
+                                        sin((cryst->gamma - 90) * PI_RAD))),
+                                         y_coor1,
+                                         cryst->offset_w + inx * cryst->a,
+                                         y_coor2);
+                       }
+               } else {
+                  if ( cryst->invert )
+                    {
+                       y_coor1 =cryst->win_height -
+                         (int) (cryst->iny * cryst->b *
+                                                 cos((cryst->gamma - 90) *
+                                                     PI_RAD)) -
+                         cryst->offset_h;
+                       y_coor2 =cryst->win_height -
+                         (int) ( ( cryst->iny + 1 ) * cryst->b *
+                                                 cos((cryst->gamma - 90) *
+                                                     PI_RAD)) -
+                         cryst->offset_h;
+                    }
+                  else
+                    {
+                       y_coor1 =(int) (cryst->iny * cryst->b *
+                                                 cos((cryst->gamma - 90) *
+                                                     PI_RAD)) +
+                         cryst->offset_h;
+                       y_coor2 =(int) (( cryst->iny + 1 ) * cryst->b *
+                                                 cos((cryst->gamma - 90) *
+                                                     PI_RAD)) +
+                         cryst->offset_h;
+                    }
+                       XDrawLine(display, window, cryst->gc,
+                                 cryst->offset_w + cryst->inx * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor1,
+                                 cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor1);
+                       XDrawLine(display, window, cryst->gc,
+                                 cryst->offset_w + cryst->inx * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor1,
+                                 cryst->offset_w + cryst->inx * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor2);
+                       XDrawLine(display, window, cryst->gc,
+                                 cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor1,
+                                 cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor2);
+                       XDrawLine(display, window, cryst->gc,
+                                 cryst->offset_w + cryst->inx * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor2,
+                                 cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
+                                 y_coor2);
+               }
+       }
+
        XSetFunction(display, cryst->gc, GXxor);
 
 /* Rotate colours */
        if (cryst->cycle_p) {
-               rotate_colors(display, cryst->cmap, cryst->colors, cryst->ncolors,
+               rotate_colors(mi->xgwa.screen, cryst->cmap,
+                      cryst->colors, cryst->ncolors,
                              cryst->direction);
                if (!(LRAND() % 1000))
                        cryst->direction = -cryst->direction;
@@ -593,7 +742,10 @@ draw_crystal(ModeInfo * mi)
                } else {
                        XSetForeground(display, cryst->gc, atom0->colour);
                }
-               crystal_drawatom(mi, atom0);
+#ifndef HAVE_JWXYZ
+               if(cryst->painted)
+                       crystal_drawatom(mi, atom0);
+#endif
                atom0->velocity[0] += NRAND(3) - 1;
                atom0->velocity[0] = MAX(-20, MIN(20, atom0->velocity[0]));
                atom0->velocity[1] += NRAND(3) - 1;
@@ -616,9 +768,12 @@ draw_crystal(ModeInfo * mi)
                crystal_drawatom(mi, atom0);
        }
        XSetFunction(display, cryst->gc, GXcopy);
+       cryst->painted = True;
+       MI_IS_DRAWN(mi) = True;
 }
 
-void
+#ifndef STANDALONE
+ENTRYPOINT void
 refresh_crystal(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
@@ -784,48 +939,40 @@ refresh_crystal(ModeInfo * mi)
        }
        XSetFunction(display, cryst->gc, GXcopy);
 }
+#endif
 
-void
-release_crystal(ModeInfo * mi)
+ENTRYPOINT void
+free_crystal(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
+       crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
 
-       if (crystals != NULL) {
-               int         screen;
-
-               for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-                       crystalstruct *cryst = &crystals[screen];
-
-                       if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
-                               MI_WHITE_PIXEL(mi) = cryst->whitepixel;
-                               MI_BLACK_PIXEL(mi) = cryst->blackpixel;
+       if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
+               MI_WHITE_PIXEL(mi) = cryst->whitepixel;
+               MI_BLACK_PIXEL(mi) = cryst->blackpixel;
 #ifndef STANDALONE
-                               MI_FG_PIXEL(mi) = cryst->fg;
-                               MI_BG_PIXEL(mi) = cryst->bg;
+               MI_FG_PIXEL(mi) = cryst->fg;
+               MI_BG_PIXEL(mi) = cryst->bg;
 #endif
-                               if (cryst->colors && cryst->ncolors && !cryst->no_colors)
-                                       free_colors(display, cryst->cmap, cryst->colors, cryst->ncolors);
-                               if (cryst->colors)
-                                       (void) free((void *) cryst->colors);
+               if (cryst->colors && cryst->ncolors && !cryst->no_colors)
+                       free_colors(mi->xgwa.screen, cryst->cmap, cryst->colors,
+                        cryst->ncolors);
+               if (cryst->colors)
+                       (void) free((void *) cryst->colors);
 #if 0 /* #### wrong! -jwz */
-                               XFreeColormap(display, cryst->cmap);
+               XFreeColormap(display, cryst->cmap);
 #endif
-                       }
-                       if (cryst->gc != NULL)
-                               XFreeGC(display, cryst->gc);
-                       if (cryst->atom != NULL)
-                               (void) free((void *) cryst->atom);
-               }
-               (void) free((void *) crystals);
-               crystals = NULL;
        }
+       if (cryst->gc != NULL)
+               XFreeGC(display, cryst->gc);
+       if (cryst->atom != NULL)
+               (void) free((void *) cryst->atom);
 }
 
-void
+ENTRYPOINT void
 init_crystal(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
-       Window      window = MI_WINDOW(mi);
        crystalstruct *cryst;
        int         i, max_atoms, size_atom, neqv;
        int         cell_min;
@@ -833,11 +980,7 @@ init_crystal(ModeInfo * mi)
 #define MIN_CELL 200
 
 /* initialize */
-       if (crystals == NULL) {
-               if ((crystals = (crystalstruct *) calloc(MI_NUM_SCREENS(mi),
-                                           sizeof (crystalstruct))) == NULL)
-                       return;
-       }
+       MI_INIT (mi, crystals);
        cryst = &crystals[MI_SCREEN(mi)];
 
        if (!cryst->gc) {
@@ -980,9 +1123,12 @@ init_crystal(ModeInfo * mi)
                        cryst->offset_w = (int) (cryst->b * 0.5);
                }
        } else {
+               int max_repeat = 10;
                cryst->offset_w = -1;
-               while (cryst->offset_w < 4 || (int) (cryst->offset_w - cryst->b *
-                                   sin((cryst->gamma - 90) * PI_RAD)) < 4) {
+               while (max_repeat-- && 
+                 (cryst->offset_w < 4 || (int) (cryst->offset_w - cryst->b *
+                                   sin((cryst->gamma - 90) * PI_RAD)) < 4)
+                          ) {
                        cryst->b = NRAND((int) (cryst->win_height / (cos((cryst->gamma - 90) *
                                            PI_RAD))) - cell_min) + cell_min;
                        if (cryst->planegroup > 8)
@@ -1025,150 +1171,11 @@ init_crystal(ModeInfo * mi)
        }
        cryst->a = cryst->a / cryst->nx;
        cryst->b = cryst->b / cryst->ny;
-       if (cryst->unit_cell) {
-          int y_coor1 , y_coor2;
-          
-               if (MI_NPIXELS(mi) > 2)
-                       XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
-               else
-                       XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
-               if (cryst->grid_cell) {
-                       int         inx, iny;
-
-                  if ( cryst->invert )
-                    y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
-                  else
-                    y_coor1 = y_coor2 = cryst->offset_h;
-                       XDrawLine(display, window, cryst->gc, cryst->offset_w,
-                                 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
-                                 y_coor2);
-                  if ( cryst->invert )
-                    {
-                       y_coor1 = cryst->win_height - cryst->offset_h;
-                       y_coor2 = cryst->win_height - (int) (cryst->ny *
-                                                            cryst->b *
-                                        cos((cryst->gamma - 90) * PI_RAD)) -
-                         cryst->offset_h;
-                    }
-                  else
-                    {
-                       y_coor1 = cryst->offset_h;
-                       y_coor2 = (int) (cryst->ny * cryst->b *
-                                        cos((cryst->gamma - 90) * PI_RAD)) +
-                         cryst->offset_h;
-                    }
-                       XDrawLine(display, window, cryst->gc, cryst->offset_w,
-                                 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
-                                         sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor2);
-                       inx = cryst->nx;
-                       for (iny = 1; iny <= cryst->ny; iny++) {
-                  if ( cryst->invert )
-                    {
-                       y_coor1 = cryst->win_height -
-                         (int) (iny * cryst->b * cos((cryst->gamma - 90) *
-                                                 PI_RAD)) - cryst->offset_h;
-                       y_coor2 = cryst->win_height -
-                         (int) (iny * cryst->b * cos((cryst->gamma - 90) *
-                                                     PI_RAD)) -
-                         cryst->offset_h;
-                    }
-                  else
-                    {
-                       y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
-                                                 PI_RAD)) + cryst->offset_h;
-                       y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
-                                         cryst->offset_h;
-                    }
-                               XDrawLine(display, window, cryst->gc,
-                                         (int) (cryst->offset_w +
-                                    inx * cryst->a - (int) (iny * cryst->b *
-                                        sin((cryst->gamma - 90) * PI_RAD))),
-                                         y_coor1,
-                                   (int) (cryst->offset_w - iny * cryst->b *
-                                          sin((cryst->gamma - 90) * PI_RAD)),
-                                         y_coor2);
-                       }
-                       iny = cryst->ny;
-                       for (inx = 1; inx <= cryst->nx; inx++) {
-                          if ( cryst->invert )
-                            {
-                               y_coor1 =cryst->win_height -
-                                 (int) (iny * cryst->b *
-                                               cos((cryst->gamma - 90) *
-                                                   PI_RAD)) - cryst->offset_h;
-                               y_coor2 =cryst->win_height - cryst->offset_h;
-                            }
-                          else
-                            {
-                               y_coor1 =(int) (iny * cryst->b *
-                                               cos((cryst->gamma - 90) *
-                                                   PI_RAD)) + cryst->offset_h;
-                               y_coor2 =cryst->offset_h;
-                            }
-                               XDrawLine(display, window, cryst->gc,
-                                         (int) (cryst->offset_w +
-                                    inx * cryst->a - (int) (iny * cryst->b *
-                                        sin((cryst->gamma - 90) * PI_RAD))),
-                                         y_coor1,
-                                         cryst->offset_w + inx * cryst->a,
-                                         y_coor2);
-                       }
-               } else {
-                       int         inx, iny;
-
-                       inx = NRAND(cryst->nx);
-                       iny = NRAND(cryst->ny);
-                  if ( cryst->invert )
-                    {
-                       y_coor1 =cryst->win_height -
-                         (int) (iny * cryst->b *
-                                                 cos((cryst->gamma - 90) *
-                                                     PI_RAD)) -
-                         cryst->offset_h;
-                       y_coor2 =cryst->win_height -
-                         (int) ( ( iny + 1 ) * cryst->b *
-                                                 cos((cryst->gamma - 90) *
-                                                     PI_RAD)) -
-                         cryst->offset_h;
-                    }
-                  else
-                    {
-                       y_coor1 =(int) (iny * cryst->b *
-                                                 cos((cryst->gamma - 90) *
-                                                     PI_RAD)) +
-                         cryst->offset_h;
-                       y_coor2 =(int) (( iny + 1 ) * cryst->b *
-                                                 cos((cryst->gamma - 90) *
-                                                     PI_RAD)) +
-                         cryst->offset_h;
-                    }
-                       XDrawLine(display, window, cryst->gc,
-                                 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor1,
-                                 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor1);
-                       XDrawLine(display, window, cryst->gc,
-                                 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor1,
-                                 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor2);
-                       XDrawLine(display, window, cryst->gc,
-                                 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor1,
-                                 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor2);
-                       XDrawLine(display, window, cryst->gc,
-                                 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor2,
-                                 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
-                                 y_coor2);
-               }
-       }
        if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
 /* Set up colour map */
                if (cryst->colors && cryst->ncolors && !cryst->no_colors)
-                       free_colors(display, cryst->cmap, cryst->colors, cryst->ncolors);
+                       free_colors(mi->xgwa.screen, cryst->cmap,
+                        cryst->colors, cryst->ncolors);
                if (cryst->colors)
                        (void) free((void *) cryst->colors);
                cryst->colors = 0;
@@ -1197,14 +1204,20 @@ init_crystal(ModeInfo * mi)
                }
                if (!cryst->mono_p) {
                        if (!(LRAND() % 10))
-                               make_random_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
-                                               True, True, &cryst->cycle_p, True);
+                               make_random_colormap(mi->xgwa.screen, MI_VISUAL(mi),
+                                     cryst->cmap, cryst->colors,
+                                     &cryst->ncolors,
+                                     True, True, &cryst->cycle_p, True);
                        else if (!(LRAND() % 2))
-                               make_uniform_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
-                                                     True, &cryst->cycle_p, True);
+                               make_uniform_colormap(mi->xgwa.screen, MI_VISUAL(mi),
+                                      cryst->cmap, cryst->colors,
+                                      &cryst->ncolors, True,
+                                      &cryst->cycle_p, True);
                        else
-                               make_smooth_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
-                                                    True, &cryst->cycle_p, True);
+                               make_smooth_colormap(mi->xgwa.screen, MI_VISUAL(mi),
+                                     cryst->cmap, cryst->colors,
+                                     &cryst->ncolors,
+                                     True, &cryst->cycle_p, True);
                }
 #if 0 /* #### wrong! -jwz */
                XInstallColormap(display, cryst->cmap);
@@ -1230,13 +1243,11 @@ init_crystal(ModeInfo * mi)
                                atom0->colour = NRAND(cryst->ncolors - 2) + 2;
                        else
                                atom0->colour = 1;      /* Just in case */
-                       XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
                } else {
                        if (MI_NPIXELS(mi) > 2)
                                atom0->colour = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
                        else
                                atom0->colour = 1;      /*Xor'red so WHITE may not be appropriate */
-                       XSetForeground(display, cryst->gc, atom0->colour);
                }
                atom0->x0 = NRAND(cryst->a);
                atom0->y0 = NRAND(cryst->b);
@@ -1257,8 +1268,17 @@ init_crystal(ModeInfo * mi)
                else
                        atom0->num_point = 4;
                crystal_setupatom(atom0, cryst->gamma);
-               crystal_drawatom(mi, atom0);
        }
-       XSync(display, False);
        XSetFunction(display, cryst->gc, GXcopy);
+
+       if (MI_NPIXELS(mi) > 2)
+               cryst->grid_pixel = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
+       else
+               cryst->grid_pixel = MI_WHITE_PIXEL(mi);
+
+       cryst->inx = NRAND(cryst->nx);
+       cryst->iny = NRAND(cryst->ny);
+
 }
+
+XSCREENSAVER_MODULE ("Crystal", crystal)