http://ftp.aanet.ru/pub/Linux/X11/apps/xscreensaver-2.31.tar.gz
[xscreensaver] / hacks / swirl.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * swirl --- swirly color-cycling patterns.
3  */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)swirl.c       4.00 97/01/01 xlockmore";
6 #endif
7
8 /* Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * 13-May-97: jwz@jwz.org: turned into a standalone program.
23  * 21-Apr-95: improved startup time for TrueColour displays
24  *            (limited to 16bpp to save memory) S.Early <sde1000@cam.ac.uk>
25  * 09-Jan-95: fixed colour maps (more colourful) and the image now spirals
26  *            outwards from the centre with a fixed number of points drawn
27  *            every iteration. Thanks to M.Dobie <mrd@ecs.soton.ac.uk>.
28  * 1994:      written.   Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
29  *            based on original code by R.Taylor
30  */
31
32 #ifdef STANDALONE
33 # define PROGCLASS                                      "Swirl"
34 # define HACK_INIT                                      init_swirl
35 # define HACK_DRAW                                      draw_swirl
36 # define swirl_opts                                     xlockmore_opts
37 # define DEFAULTS       "*count:                5       \n"                     \
38                                         "*delay:                10000   \n"                     \
39                                         "*ncolors:              200     \n"                     \
40                                         "*useSHM:               True    \n"
41 # define SMOOTH_COLORS
42 # define WRITABLE_COLORS
43 # include "xlockmore.h"                         /* from the xscreensaver distribution */
44 # include <X11/Xutil.h>
45 # ifdef HAVE_XSHM_EXTENSION
46 #  include "xshm.h"
47 # endif /* HAVE_XSHM_EXTENSION */
48 #else  /* !STANDALONE */
49 # include "xlock.h"                                     /* from the xlockmore distribution */
50 # undef HAVE_XSHM_EXTENSION
51 #endif /* !STANDALONE */
52
53 ModeSpecOpt swirl_opts = {
54   0, NULL, 0, NULL, NULL };
55
56 #include <time.h>
57
58 /****************************************************************/
59
60 #define MASS            4       /* maximum mass of a knot */
61 #define MIN_RES         5       /* minimim resolution (>= MIN_RES) */
62 #define MAX_RES         1       /* maximum resolution (>0) */
63 #define TWO_PLANE_PCNT  30      /* probability for two plane mode (0-100) */
64 #define RESTART         2500    /* number of cycles before restart */
65 #define BATCH_DRAW      100     /* points to draw per iteration */
66
67 /* knot types */
68 typedef enum {
69         NONE = 0,
70         ORBIT = (1 << 0),
71         WHEEL = (1 << 1),
72         PICASSO = (1 << 2),
73         RAY = (1 << 3),
74         HOOK = (1 << 4),
75         ALL = (1 << 5)
76 } KNOT_T;
77
78 /* a knot */
79 typedef struct Knot {
80         int         x, y;       /* position */
81         int         m;          /* mass */
82         KNOT_T      t;          /* type in the first (or only) plane */
83         KNOT_T      T;          /* type in second plane if there is one */
84         int         M;          /* mass in second plane if there is one */
85 } KNOT     , *KNOT_P;
86
87 /* a colour specification */
88 typedef struct Colour {
89         unsigned short r, g, b;
90 } COLOUR   , *COLOUR_P;
91
92 /* drawing direction */
93 typedef enum {
94         DRAW_RIGHT, DRAW_DOWN, DRAW_LEFT, DRAW_UP
95 } DIR_T;
96
97 /****************************************************************/
98
99 /* data associated with a swirl window */
100 typedef struct swirl_data {
101         /* window paramaters */
102         Window      win;        /* the window */
103         int         width, height;      /* window size */
104         int         depth;      /* depth */
105         int         rdepth;     /* real depth (for XImage) */
106         Visual     *visual;     /* visual */
107
108         /* swirl drawing parameters */
109         int         n_knots;    /* number of knots */
110         KNOT_P      knots;      /* knot details */
111         KNOT_T      knot_type;  /* general type of knots */
112         int         resolution; /* drawing resolution, 1..5 */
113         int         max_resolution;     /* maximum resolution, MAX_RES */
114         int         r;          /* pixel step */
115         Bool        two_plane;  /* two plane mode? */
116         Bool        first_plane;        /* doing first plane? */
117         int         start_again;        /* when to restart */
118
119         /* spiral drawing parameters */
120         int         x, y;       /* current point */
121         DIR_T       direction;  /* current direction */
122         int         dir_todo, dir_done;         /* how many points in current direction? */
123         int         batch_todo, batch_done;     /* how many points in this batch */
124         Bool        started, drawing;   /* are we drawing? */
125
126         /* image stuff */
127         unsigned char *image;   /* image data */
128         XImage     *ximage;
129
130         /* colours stuff */
131         int         colours;    /* how many colours possible */
132         int         dcolours;   /* how many colours for shading */
133 #ifndef STANDALONE
134         Bool        fixed_colourmap;    /* fixed colourmap? */
135 #endif /* !STANDALONE */
136         Bool        monochrome; /* monochrome? */
137         Colormap    cmap;       /* colour map for the window */
138         XColor     *rgb_values; /* colour definitions array */
139 #ifndef STANDALONE
140         int         current_map;        /* current colour map, 0..dcolours-1 */
141         unsigned long fg, bg, white, black;     /* black and white pixel values */
142         int         shift;      /* colourmap shift */
143         int         dshift;     /* colourmap shift while drawing */
144         XColor      fgcol, bgcol;       /* foreground and background colour specs */
145 #endif /* !STANDALONE */
146         Bool       off_screen;
147 } SWIRL    , *SWIRL_P;
148
149 #define SWIRLCOLOURS 13
150
151 #ifndef STANDALONE
152 /* basic colours */
153 static COLOUR basic_colours[SWIRLCOLOURS];
154 #endif /* !STANDALONE */
155
156 /* an array of swirls for each screen */
157 static SWIRL_P swirls = NULL;
158
159 /* 
160    random_no
161
162    Return a random integer between 0 and n inclusive
163
164    -      n is the maximum number
165
166    Returns a random integer */
167
168 static int
169 random_no(unsigned int n)
170 {
171         return ((int) ((n + 1) * (double) LRAND() / MAXRAND));
172 }
173
174 /****************************************************************/
175
176 /* 
177    initialise_swirl
178
179    Initialise all the swirl data
180
181    -      swirl is the swirl data */
182
183 static void
184 initialise_swirl(ModeInfo * mi, SWIRL_P swirl)
185 {
186 #ifndef STANDALONE
187         Display    *display = MI_DISPLAY(mi);
188 #endif /* !STANDALONE */
189
190         swirl->width = 0;       /* width and height of window */
191         swirl->height = 0;
192         swirl->depth = 1;
193         swirl->rdepth = 1;
194         swirl->visual = NULL;
195         swirl->resolution = MIN_RES + 1;        /* current resolution */
196         swirl->max_resolution = MAX_RES;        /* maximum resolution */
197         swirl->n_knots = 0;     /* number of knots */
198         swirl->knot_type = ALL; /* general type of knots */
199         swirl->two_plane = False;       /* two plane mode? */
200         swirl->first_plane = False;     /* doing first plane? */
201         swirl->start_again = -1;        /* restart counter */
202
203         /* drawing parameters */
204         swirl->x = 0;
205         swirl->y = 0;
206         swirl->started = False;
207         swirl->drawing = False;
208
209         /* image stuff */
210         swirl->image = NULL;    /* image data */
211         swirl->ximage = NULL;
212
213         /* colours stuff */
214         swirl->colours = 0;     /* how many colours possible */
215         swirl->dcolours = 0;    /* how many colours for shading */
216         swirl->cmap = (Colormap) NULL;
217         swirl->rgb_values = NULL;       /* colour definitions array */
218 #ifndef STANDALONE
219         swirl->current_map = 0; /* current colour map, 0..dcolours-1 */
220
221         /* set up fg fb colour specs */
222         swirl->white = MI_WIN_WHITE_PIXEL(mi);
223         swirl->black = MI_WIN_BLACK_PIXEL(mi);
224 #endif /* !STANDALONE */
225
226
227 #ifdef STANDALONE
228 # define MI_COLORMAP MI_WIN_COLORMAP
229 #else /* !STANDALONE */
230         swirl->fg = MI_FG_COLOR(mi);
231         swirl->bg = MI_BG_COLOR(mi);
232         swirl->fgcol.pixel = swirl->fg;
233         swirl->bgcol.pixel = swirl->bg;
234         XQueryColor(display, MI_COLORMAP(mi), &(swirl->fgcol));
235         XQueryColor(display, MI_COLORMAP(mi), &(swirl->bgcol));
236 #endif /* !STANDALONE */
237 }
238
239 /****************************************************************/
240
241 /* 
242  * initialise_image
243  *
244  * Initialise the image for drawing to
245  *
246  * -      swirl is the swirl data
247  */
248 static void
249 initialise_image(ModeInfo * mi, SWIRL_P swirl)
250 {
251   Display *dpy = MI_DISPLAY(mi);
252
253   if (swirl->ximage != NULL)
254         XDestroyImage(swirl->ximage);
255
256   swirl->ximage = 0;
257 #ifdef HAVE_XSHM_EXTENSION
258   if (mi->use_shm)
259         {
260           swirl->ximage = create_xshm_image(dpy, swirl->visual, swirl->rdepth,
261                                                                                 ZPixmap, 0, &mi->shm_info,
262                                                                                 swirl->width, swirl->height);
263           if (!swirl->ximage)
264                 mi->use_shm = False;
265         }
266 #endif /* HAVE_XSHM_EXTENSION */
267
268   if (!swirl->ximage)
269         {
270           swirl->ximage = XCreateImage(dpy, swirl->visual, swirl->rdepth, ZPixmap,
271                                                                    0, 0, swirl->width, swirl->height,
272                                                                    8, 0);
273           swirl->ximage->data = swirl->image =
274                 (char *) calloc(swirl->height, swirl->ximage->bytes_per_line);
275         }
276 }
277
278 /****************************************************************/
279
280 #ifndef STANDALONE
281 /* 
282  * initialise_colours
283  *
284  * Initialise the list of colours from which the colourmaps are derived
285  *
286  * -      colours is the array to initialise
287  * -      saturation is the saturation value to use 0->grey,
288  *            1.0->full saturation
289  */
290 static void
291 initialise_colours(COLOUR * colours, float saturate)
292 {
293         int         i;
294
295         /* start off fully saturated, medium and bright colours */
296         colours[0].r = 0xA000;
297         colours[0].g = 0x0000;
298         colours[0].b = 0x0000;
299         colours[1].r = 0xD000;
300         colours[1].g = 0x0000;
301         colours[1].b = 0x0000;
302         colours[2].r = 0x0000;
303         colours[2].g = 0x6000;
304         colours[2].b = 0x0000;
305         colours[3].r = 0x0000;
306         colours[3].g = 0x9000;
307         colours[3].b = 0x0000;
308         colours[4].r = 0x0000;
309         colours[4].g = 0x0000;
310         colours[4].b = 0xC000;
311         colours[5].r = 0x0000;
312         colours[5].g = 0x0000;
313         colours[5].b = 0xF000;
314         colours[6].r = 0xA000;
315         colours[6].g = 0x6000;
316         colours[6].b = 0x0000;
317         colours[7].r = 0xD000;
318         colours[7].g = 0x9000;
319         colours[7].b = 0x0000;
320         colours[8].r = 0xA000;
321         colours[8].g = 0x0000;
322         colours[8].b = 0xC000;
323         colours[9].r = 0xD000;
324         colours[9].g = 0x0000;
325         colours[9].b = 0xF000;
326         colours[10].r = 0x0000;
327         colours[10].g = 0x6000;
328         colours[10].b = 0xC000;
329         colours[11].r = 0x0000;
330         colours[11].g = 0x9000;
331         colours[11].b = 0xF000;
332         colours[12].r = 0xA000;
333         colours[12].g = 0xA000;
334         colours[12].b = 0xA000;
335
336         /* add white for low saturation */
337         for (i = 0; i < SWIRLCOLOURS - 1; i++) {
338                 unsigned short max_rg, max;
339
340                 /* what is the max intensity for this colour? */
341                 max_rg = (colours[i].r > colours[i].g) ? colours[i].r : colours[i].g;
342                 max = (max_rg > colours[i].b) ? max_rg : colours[i].b;
343
344                 /* bring elements up to max as saturation approaches 0.0 */
345                 colours[i].r += (unsigned short) ((float) (1.0 - saturate) *
346                                                ((float) max - colours[i].r));
347                 colours[i].g += (unsigned short) ((float) (1.0 - saturate) *
348                                                ((float) max - colours[i].g));
349                 colours[i].b += (unsigned short) ((float) (1.0 - saturate) *
350                                                ((float) max - colours[i].b));
351         }
352 }
353 #endif /* !STANDALONE */
354
355 /****************************************************************/
356
357 #ifndef STANDALONE
358 /* 
359  * set_black_and_white
360  *
361  * Set the entries for foreground & background pixels and
362  * WhitePixel & BlackPixel in an array of colour specifications.
363  *
364  * -      swirl is the swirl data
365  * -      values is the array of specifications 
366  */
367 static void
368 set_black_and_white(SWIRL_P swirl, XColor * values)
369 {
370         unsigned long white, black;
371
372         /* where is black and white? */
373         white = swirl->white;
374         black = swirl->black;
375
376         /* set black and white up */
377         values[white].flags = DoRed | DoGreen | DoBlue;
378         values[white].pixel = white;
379         values[white].red = 0xFFFF;
380         values[white].green = 0xFFFF;
381         values[white].blue = 0xFFFF;
382         values[black].flags = DoRed | DoGreen | DoBlue;
383         values[black].pixel = black;
384         values[black].red = 0;
385         values[black].green = 0;
386         values[black].blue = 0;
387
388         /* copy the colour specs from the original entries */
389         values[swirl->fg] = swirl->fgcol;
390         values[swirl->bg] = swirl->bgcol;
391 }
392
393 /****************************************************************/
394
395 /* 
396  * set_colour
397  *
398  * Set an entry in an array of XColor specifications. The given entry will be
399  * set to the given colour. If the entry corresponds to the foreground,
400  * background, WhitePixel, or BlackPixel it is ignored and the given colour
401  * is is put in the next entry.
402  *
403  * Therefore, the given colour may be placed up to four places after the
404  * specified entry in the array, if foreground, background, white, or black
405  * intervene.
406  *
407  * -      swirl is the swirl data
408  * -      value points to a pointer to the array entry. It gets updated to
409  *            point to the next free entry.
410  * -      pixel points to the current pixel number. It gets updated.
411  * -      c points to the colour to add
412  */
413 static void
414 set_colour(SWIRL_P swirl, XColor ** value, unsigned long *pixel, COLOUR_P c)
415 {
416         Bool        done;
417         unsigned long fg, bg, white, black;
418
419         /* where are foreground, background, white, and black? */
420         fg = swirl->fg;
421         bg = swirl->bg;
422         white = swirl->white;
423         black = swirl->black;
424
425         /* haven't set it yet */
426         done = False;
427
428         /* try and set the colour */
429         while (!done) {
430                 (**value).flags = DoRed | DoGreen | DoBlue;
431                 (**value).pixel = *pixel;
432
433                 /* white, black, fg, bg, or a colour? */
434                 if ((*pixel != fg) && (*pixel != bg) &&
435                     (*pixel != white) && (*pixel != black)) {
436                         (**value).red = c->r;
437                         (**value).green = c->g;
438                         (**value).blue = c->b;
439
440                         /* now we've done it */
441                         done = True;
442                 }
443                 /* next pixel */
444                 (*value)++;
445                 (*pixel)++;
446         }
447 }
448
449 /****************************************************************/
450
451 /* 
452  * get_colour
453  *
454  * Get an entry from an array of XColor specifications. The next colour from
455  * the array will be returned. Foreground, background, WhitePixel, or
456  * BlackPixel will be ignored.
457  *
458  * -      swirl is the swirl data
459  * -      value points the array entry. It is updated to point to the entry
460  *            following the one returned.
461  * -      c is set to the colour found
462  */
463 static void
464 get_colour(SWIRL_P swirl, XColor ** value, COLOUR_P c)
465 {
466         Bool        done;
467         unsigned long fg, bg, white, black;
468
469         /* where is white and black? */
470         fg = swirl->fg;
471         bg = swirl->bg;
472         white = swirl->white;
473         black = swirl->black;
474
475         /* haven't set it yet */
476         done = False;
477
478         /* try and set the colour */
479         while (!done) {
480                 /* black, white or a colour? */
481                 if (((*value)->pixel != fg) && ((*value)->pixel != bg) &&
482                   ((*value)->pixel != white) && ((*value)->pixel != black)) {
483                         c->r = (*value)->red;
484                         c->g = (*value)->green;
485                         c->b = (*value)->blue;
486
487                         /* now we've done it */
488                         done = True;
489                 }
490                 /* next value */
491                 (*value)++;
492         }
493 }
494 #endif /* !STANDALONE */
495
496 /****************************************************************/
497
498 #ifndef STANDALONE
499 /* 
500  *  interpolate
501  *
502  * Generate n colours between c1 and c2.  n XColors at *value are set up with
503  * ascending pixel values.
504  *
505  * If the pixel range includes BlackPixel or WhitePixel they are set to black
506  * and white respectively but otherwise ignored. Therefore, up to n+2 colours
507  * may actually be set by this function.
508  *
509  * -      swirl is the swirl data
510  * -      values points a pointer to an array of XColors to update
511  * -      pixel points to the pixel number to start at
512  * -      k n is the number of colours to generate
513  * -      c1, c2 are the colours to interpolate between
514  */
515 static void
516 interpolate(SWIRL_P swirl, XColor ** values, unsigned long *pixel, int n, COLOUR_P c1, COLOUR_P c2)
517 {
518         int         i, r, g, b;
519         COLOUR      c;
520         unsigned short maxv;
521
522         /* maximum value */
523         maxv = (255 << 8);
524
525         for (i = 0; i < n / 2 && (int) *pixel < swirl->colours; i++) {
526                 /* work out the colour */
527                 r = c1->r + 2 * i * ((int) c2->r) / n;
528                 c.r = (r > (int) maxv) ? maxv : r;
529                 g = c1->g + 2 * i * ((int) c2->g) / n;
530                 c.g = (g > (int) maxv) ? maxv : g;
531                 b = c1->b + 2 * i * ((int) c2->b) / n;
532                 c.b = (b > (int) maxv) ? maxv : b;
533
534                 /* set it up */
535                 set_colour(swirl, values, pixel, &c);
536         }
537         for (i = n / 2; i >= 0 && (int) *pixel < swirl->colours; i--) {
538                 r = c2->r + 2 * i * ((int) c1->r) / n;
539                 c.r = (r > (int) maxv) ? maxv : r;
540                 g = c2->g + 2 * i * ((int) c1->g) / n;
541                 c.g = (g > (int) maxv) ? maxv : g;
542                 b = c2->b + 2 * i * ((int) c1->b) / n;
543                 c.b = (b > (int) maxv) ? maxv : b;
544
545                 /* set it up */
546                 set_colour(swirl, values, pixel, &c);
547         }
548 }
549
550 /****************************************************************/
551
552 /* 
553  * basic_map
554  *
555  * Generate a `random' closed loop colourmap that occupies the whole colour
556  * map.
557  *
558  * -      swirl is the swirl data
559  * -      values is the array of colour definitions to set up
560  */
561 static void
562 basic_map(SWIRL_P swirl, XColor * values)
563 {
564         COLOUR      c[3];
565         int         i;
566         unsigned short r1, g1, b1, r2, g2, b2, r3, g3, b3;
567         int         L1, L2, L3, L;
568         unsigned long pixel;
569         XColor     *value;
570
571         /* start at the beginning of the colour map */
572         pixel = 0;
573         value = values;
574
575         /* choose 3 different basic colours at random */
576         for (i = 0; i < 3;) {
577                 int         j;
578                 Bool        same;
579
580                 /* choose colour i */
581                 c[i] = basic_colours[random_no(SWIRLCOLOURS - 1)];
582
583                 /* assume different */
584                 same = False;
585
586                 /* different from the rest? */
587                 for (j = 0; j < i; j++)
588                         if ((c[i].r == c[j].r) &&
589                             (c[i].g == c[j].g) &&
590                             (c[i].b == c[j].b))
591                                 same = True;
592
593                 /* ready for the next colour? */
594                 if (!same)
595                         i++;
596         }
597
598         /* extract components into variables */
599         r1 = c[0].r;
600         g1 = c[0].g;
601         b1 = c[0].b;
602         r2 = c[1].r;
603         g2 = c[1].g;
604         b2 = c[1].b;
605         r3 = c[2].r;
606         g3 = c[2].g;
607         b3 = c[2].b;
608
609         /* work out the lengths of each side of the triangle */
610         L1 = (int) sqrt((((double) r1 - r2) * ((double) r1 - r2) +
611                          ((double) g1 - g2) * ((double) g1 - g2) +
612                          ((double) b1 - b2) * ((double) b1 - b2)));
613
614         L2 = (int) sqrt((((double) r3 - r2) * ((double) r3 - r2) +
615                          ((double) g3 - g2) * ((double) g3 - g2) +
616                          ((double) b3 - b2) * ((double) b3 - b2)));
617
618         L3 = (int) sqrt((((double) r1 - r3) * ((double) r1 - r3) +
619                          ((double) g1 - g3) * ((double) g1 - g3) +
620                          ((double) b1 - b3) * ((double) b1 - b3)));
621
622         L = L1 + L2 + L3;
623
624         /* allocate colours in proportion to the lengths of the sides */
625         interpolate(swirl, &value, &pixel,
626                     (int) ((double) swirl->dcolours * ((double) L1 / (double) L)) + 1, c, c + 1);
627         interpolate(swirl, &value, &pixel,
628                     (int) ((double) swirl->dcolours * ((double) L2 / (double) L)) + 1, c + 1, c + 2);
629         interpolate(swirl, &value, &pixel,
630                     (int) ((double) swirl->dcolours * ((double) L3 / (double) L)) + 1, c + 2, c);
631
632         /* fill up any remaining slots (due to rounding) */
633         while ((int) pixel < swirl->colours) {
634                 /* repeat the last colour */
635                 set_colour(swirl, &value, &pixel, c);
636         }
637
638         /* ensure black and white are correct */
639         if (!swirl->fixed_colourmap)
640                 set_black_and_white(swirl, values);
641 }
642
643 /****************************************************************/
644
645 /* 
646  * pre_rotate
647  *
648  * Generate pre-rotated versions of the colour specifications
649  *
650  * -      swirl is the swirl data
651  * -      values is an array of colour specifications
652  */
653 static void
654 pre_rotate(SWIRL_P swirl, XColor * values)
655 {
656         int         i, j;
657         XColor     *src, *dest;
658         int         dcolours;
659         unsigned long pixel;
660
661         /* how many colours to display? */
662         dcolours = swirl->dcolours;
663
664         /* start at the first map */
665         src = values;
666         dest = values + swirl->colours;
667
668         /* generate dcolours-1 rotated maps */
669         for (i = 0; i < dcolours - 1; i++) {
670                 COLOUR      first;
671
672                 /* start at the first pixel */
673                 pixel = 0;
674
675                 /* remember the first one and skip it */
676                 get_colour(swirl, &src, &first);
677
678                 /* put a rotated version of src at dest */
679                 for (j = 0; j < dcolours - 1; j++) {
680                         COLOUR      c;
681
682                         /* get the source colour */
683                         get_colour(swirl, &src, &c);
684
685                         /* set the colour */
686                         set_colour(swirl, &dest, &pixel, &c);
687                 }
688
689                 /* put the first one at the end */
690                 set_colour(swirl, &dest, &pixel, &first);
691
692                 /* NB: src and dest should now be ready for the next table */
693
694                 /* ensure black and white are properly set */
695                 set_black_and_white(swirl, src);
696         }
697 }
698
699 /****************************************************************/
700
701 /* 
702  * create_colourmap
703  *
704  * Create a read/write colourmap to use
705  *
706  * -      swirl is the swirl data
707  */
708
709 static void
710 create_colourmap(ModeInfo * mi, SWIRL_P swirl)
711 {
712         Display    *display = MI_DISPLAY(mi);
713         int         preserve;
714         int         n_rotations;
715         int         i;
716         Bool        truecolor;
717   unsigned long redmask, greenmask, bluemask;
718
719         swirl->fixed_colourmap = !setupColormap(mi, &(swirl->colours),
720     &truecolor, &redmask, &greenmask, &bluemask);
721         preserve = preserveColors(swirl->fg, swirl->bg, swirl->white, swirl->black);
722
723         /* how many colours should we animate? */
724         swirl->dcolours = (swirl->colours > preserve + 1) ?
725                 swirl->colours - preserve : swirl->colours;
726
727         if (MI_NPIXELS(mi) < 2)
728                 return;
729
730         /* how fast to shift the colourmap? */
731         swirl->shift = (swirl->colours > 64) ? swirl->colours / 64 : 1;
732         swirl->dshift = (swirl->shift > 1) ? swirl->shift * 2 : 1;
733
734         /* how may colour map rotations are there? */
735         n_rotations = (swirl->fixed_colourmap) ? 1 : swirl->dcolours;
736
737         /* allocate space for colour definitions (if not already there) */
738         if (swirl->rgb_values == NULL) {
739                 swirl->rgb_values = (XColor *) calloc((swirl->colours + 3) * n_rotations,
740                                                       sizeof (XColor));
741
742                 /* create a colour map */
743                 if (!swirl->fixed_colourmap)
744                         swirl->cmap =
745                                 XCreateColormap(display, swirl->win, swirl->visual, AllocAll);
746         }
747         /* select a set of colours for the colour map */
748         basic_map(swirl, swirl->rgb_values);
749
750         /* are we rotating them? */
751         if (!swirl->fixed_colourmap) {
752                 /* generate rotations of the colour maps */
753                 pre_rotate(swirl, swirl->rgb_values);
754
755                 /* store the colours in the colour map */
756                 XStoreColors(display, swirl->cmap, swirl->rgb_values, swirl->colours);
757         } else {
758                 if (truecolor) {
759                         int         rsh, gsh, bsh;
760                         unsigned long int t;
761
762                         t = redmask;
763                         for (i = 0; (int) t > 0; i++, t >>= 1);
764                         rsh = 16 - i;
765                         t = greenmask;
766                         for (i = 0; (int) t > 0; i++, t >>= 1);
767                         gsh = 16 - i;
768                         t = bluemask;
769                         for (i = 0; (int) t > 0; i++, t >>= 1);
770                         bsh = 16 - i;
771                         for (i = 0; i < swirl->colours; i++)
772                                 swirl->rgb_values[i].pixel =
773                                         ((rsh > 0 ? (swirl->rgb_values[i].red) >> rsh :
774                                           (swirl->rgb_values[i].red) << (-rsh)) & redmask) |
775                                         ((gsh > 0 ? (swirl->rgb_values[i].green) >> gsh :
776                                           (swirl->rgb_values[i].green) << (-gsh)) & greenmask) |
777                                         ((bsh > 0 ? (swirl->rgb_values[i].blue) >> bsh :
778                                           (swirl->rgb_values[i].blue) << (-bsh)) & bluemask);
779                 } else {
780                         /* lookup the colours in the fixed colour map */
781                         for (i = 0; i < swirl->colours; i++)
782                                 (void) XAllocColor(display, MI_COLORMAP(mi),
783                                                    &(swirl->rgb_values[i]));
784                 }
785         }
786 }
787
788 /****************************************************************/
789
790 /* 
791  * install_map
792  *
793  * Install a new set of colours into the colour map
794  *
795  * -      dpy is the display
796  * -      swirl is the swirl data
797  * -      shift is the amount to rotate the colour map by
798  */
799 static void
800 install_map(Display * dpy, SWIRL_P swirl, int shift)
801 {
802         if (!swirl->fixed_colourmap) {
803                 /* shift the colour map */
804                 swirl->current_map = (swirl->current_map + shift) %
805                         swirl->dcolours;
806
807                 /* store it */
808                 XStoreColors(dpy, swirl->cmap,
809                              swirl->rgb_values +
810                              swirl->current_map * swirl->colours,
811                              swirl->colours);
812         }
813 }
814 #endif /* !STANDALONE */
815
816 /****************************************************************/
817
818 /* 
819  * create_knots
820  *
821  * Initialise the array of knot
822  *
823  * swirl is the swirl data
824  */
825 static void
826 create_knots(SWIRL_P swirl)
827 {
828         int         k;
829         Bool        orbit, wheel, picasso, ray, hook;
830         KNOT_P      knot;
831
832         /* create array for knots */
833         if (swirl->knots)
834                 (void) free((void *) swirl->knots);
835         swirl->knots = (KNOT_P) calloc(swirl->n_knots, sizeof (KNOT));
836
837         /* no knots yet */
838         orbit = wheel = picasso = ray = hook = False;
839
840         /* what types do we have? */
841         if ((int) swirl->knot_type & (int) ALL) {
842                 orbit = wheel = ray = hook = True;
843         } else {
844                 if ((int) swirl->knot_type & (int) ORBIT)
845                         orbit = True;
846                 if ((int) swirl->knot_type & (int) WHEEL)
847                         wheel = True;
848                 if ((int) swirl->knot_type & (int) PICASSO)
849                         picasso = True;
850                 if ((int) swirl->knot_type & (int) RAY)
851                         ray = True;
852                 if ((int) swirl->knot_type & (int) HOOK)
853                         hook = True;
854         }
855
856         /* initialise each knot */
857         knot = swirl->knots;
858         for (k = 0; k < swirl->n_knots; k++) {
859                 /* position */
860                 knot->x = random_no((unsigned int) swirl->width);
861                 knot->y = random_no((unsigned int) swirl->height);
862
863                 /* mass */
864                 knot->m = random_no(MASS) + 1;
865
866                 /* can be negative */
867                 if (random_no(100) > 50)
868                         knot->m *= -1;
869
870                 /* type */
871                 knot->t = NONE;
872                 while (knot->t == NONE) {
873                         /* choose a random one from the types available */
874                         switch (random_no(4)) {
875                                 case 0:
876                                         if (orbit)
877                                                 knot->t = ORBIT;
878                                         break;
879                                 case 1:
880                                         if (wheel)
881                                                 knot->t = WHEEL;
882                                         break;
883                                 case 2:
884                                         if (picasso)
885                                                 knot->t = PICASSO;
886                                         break;
887                                 case 3:
888                                         if (ray)
889                                                 knot->t = RAY;
890                                         break;
891                                 case 4:
892                                         if (hook)
893                                                 knot->t = HOOK;
894                                         break;
895                         }
896                 }
897
898                 /* if two planes, do same for second plane */
899                 if (swirl->two_plane) {
900                         knot->T = NONE;
901                         while (knot->T == NONE || knot->T == knot->t) {
902                                 /* choose a different type */
903                                 switch (random_no(4)) {
904                                         case 0:
905                                                 if (orbit)
906                                                         knot->T = ORBIT;
907                                                 break;
908                                         case 1:
909                                                 if (wheel)
910                                                         knot->T = WHEEL;
911                                                 break;
912                                         case 2:
913                                                 if (picasso)
914                                                         knot->T = PICASSO;
915                                                 break;
916                                         case 3:
917                                                 if (ray)
918                                                         knot->T = RAY;
919                                                 break;
920                                         case 4:
921                                                 if (hook)
922                                                         knot->T = HOOK;
923                                                 break;
924                                 }
925                         }
926                 }
927                 /* next knot */
928                 knot++;
929         }
930 }
931
932 /****************************************************************/
933
934 /* 
935  * do_point
936  *
937  * Work out the pixel value at i, j. Ensure it does not clash with BlackPixel
938  * or WhitePixel.
939  *
940  * -      swirl is the swirl data
941  * -      i, j is the point to calculate
942  *
943  * Returns the value of the point
944  */
945 static unsigned long
946 do_point(SWIRL_P swirl, int i, int j)
947 {
948         int         tT, k, value, add;
949         double      dx, dy, theta, dist;
950         int         dcolours, qcolours;
951         double      rads;
952         KNOT_P      knot;
953
954         /* how many colours? */
955         dcolours = swirl->dcolours;
956         qcolours = dcolours / 4;
957
958         /* colour step round a circle */
959         rads = (double) dcolours / (2.0 * M_PI);
960
961         /* start at zero */
962         value = 0;
963
964         /* go through all the knots */
965         knot = swirl->knots;
966         for (k = 0; k < swirl->n_knots; k++) {
967                 dx = i - knot->x;
968                 dy = j - knot->y;
969
970                 /* in two_plane mode get the appropriate knot type */
971                 if (swirl->two_plane)
972                         tT = (int) ((swirl->first_plane) ? knot->t : knot->T);
973                 else
974                         tT = (int) knot->t;
975
976                 /* distance from knot */
977                 dist = sqrt(dx * dx + dy * dy);
978
979                 /* nothing to add at first */
980                 add = 0;
981
982                 /* work out the contribution (if close enough) */
983                 if (dist > 0.1)
984                         switch (tT) {
985                                 case ORBIT:
986                                         add = (int) (dcolours / (1.0 + 0.01 * abs(knot->m) * dist));
987                                         break;
988                                 case WHEEL:
989                                         /* Avoid atan2: DOMAIN error message */
990                                         if (dy == 0.0 && dx == 0.0)
991                                                 theta = 1.0;
992                                         else
993                                                 theta = (atan2(dy, dx) + M_PI) / M_PI;
994                                         if (theta < 1.0)
995                                                 add = (int) (dcolours * theta +
996                                                   sin(0.1 * knot->m * dist) *
997                                                 qcolours * exp(-0.01 * dist));
998                                         else
999                                                 add = (int) (dcolours * (theta - 1.0) +
1000                                                   sin(0.1 * knot->m * dist) *
1001                                                 qcolours * exp(-0.01 * dist));
1002                                         break;
1003                                 case PICASSO:
1004                                         add = (int) (dcolours *
1005                                           fabs(cos(0.002 * knot->m * dist)));
1006                                         break;
1007                                 case RAY:
1008                                         /* Avoid atan2: DOMAIN error message */
1009                                         if (dy == 0.0 && dx == 0.0)
1010                                                 add = 0;
1011                                         else
1012                                                 add = (int) (dcolours * fabs(sin(2.0 * atan2(dy, dx))));
1013
1014                                         break;
1015                                 case HOOK:
1016                                         /* Avoid atan2: DOMAIN error message */
1017                                         if (dy == 0.0 && dx == 0.0)
1018                                                 add = (int) (0.05 * (abs(knot->m) - 1) * dist);
1019                                         else
1020                                                 add = (int) (rads * atan2(dy, dx) +
1021                                                              0.05 * (abs(knot->m) - 1) * dist);
1022                                         break;
1023                         }
1024                 /* for a +ve mass add on the contribution else take it off */
1025                 if (knot->m > 0)
1026                         value += add;
1027                 else
1028                         value -= add;
1029
1030                 /* next knot */
1031                 knot++;
1032         }
1033
1034         /* toggle plane */
1035         swirl->first_plane = (!swirl->first_plane);
1036
1037         /* make sure we handle -ve values properly */
1038         if (value >= 0)
1039                 value = (value % dcolours) + 2;
1040         else
1041                 value = dcolours - (abs(value) % (dcolours - 1));
1042
1043 #ifndef STANDALONE
1044         /* if fg and bg are 1 and 0 we should be OK, but just in case */
1045         while ((dcolours > 2) &&
1046                (((value % swirl->colours) == (int) swirl->fg) ||
1047                 ((value % swirl->colours) == (int) swirl->bg) ||
1048                 ((value % swirl->colours) == (int) swirl->white) ||
1049                 ((value % swirl->colours) == (int) swirl->black))) {
1050                 value++;
1051         }
1052 #endif /* !STANDALONE */
1053
1054         /* definitely make sure it is in range */
1055         value = value % swirl->colours;
1056
1057         /* lookup the pixel value if necessary */
1058 #ifndef STANDALONE
1059         if (swirl->fixed_colourmap && swirl->dcolours > 2)
1060 #endif
1061                 value = swirl->rgb_values[value].pixel;
1062
1063         /* return it */
1064         return ((unsigned long) value);
1065 }
1066
1067 /****************************************************************/
1068
1069 /* 
1070  * draw_block
1071  *
1072  * Draw a square block of points with the same value.
1073  *
1074  * -      ximage is the XImage to draw on.
1075  * -      x, y is the top left corner
1076  * -      s is the length of each side
1077  * -      v is the value
1078  */
1079 static void
1080 draw_block(XImage * ximage, int x, int y, int s, unsigned long v)
1081 {
1082         int         a, b;
1083
1084         for (a = 0; a < s; a++)
1085                 for (b = 0; b < s; b++) {
1086                         XPutPixel(ximage, x + b, y + a, v);
1087                 }
1088 }
1089
1090 /****************************************************************/
1091
1092 /* 
1093  * draw_point  Draw the current point in a swirl pattern onto the XImage
1094  *
1095  * -    swirl is the swirl
1096  * -    win is the window to update
1097  */
1098 static void
1099 draw_point(ModeInfo * mi, SWIRL_P swirl)
1100 {
1101         int         r;
1102         int         x, y;
1103
1104         /* get current point coordinates and resolution */
1105         x = swirl->x;
1106         y = swirl->y;
1107         r = swirl->r;
1108
1109         /* check we are within the window */
1110         if ((x < 0) || (x > swirl->width - r) || (y < 0) || (y > swirl->height - r))
1111                 return;
1112
1113         /* what style are we drawing? */
1114         if (swirl->two_plane) {
1115                 int         r2;
1116
1117                 /* halve the block size */
1118                 r2 = r / 2;
1119
1120                 /* interleave blocks at half r */
1121                 draw_block(swirl->ximage, x, y, r2, do_point(swirl, x, y));
1122                 draw_block(swirl->ximage, x + r2, y, r2, do_point(swirl, x + r2, y));
1123                 draw_block(swirl->ximage, x + r2, y + r2, r2, do_point(swirl,
1124                         x + r2, y + r2));
1125                 draw_block(swirl->ximage, x, y + r2, r2, do_point(swirl, x, y + r2));
1126         } else
1127                 draw_block(swirl->ximage, x, y, r, do_point(swirl, x, y));
1128
1129         /* update the screen */
1130
1131 #ifdef HAVE_XSHM_EXTENSION
1132         if (mi->use_shm)
1133           XShmPutImage(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1134                                    x, y, x, y, r, r, False);
1135         else
1136 #endif /* !HAVE_XSHM_EXTENSION */
1137           /* PURIFY 4.0.1 on SunOS4 and on Solaris 2 reports a 256 byte memory
1138                  leak on the next line. */
1139           XPutImage(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1140                                 x, y, x, y, r, r);
1141 }
1142
1143 /****************************************************************/
1144
1145 /* 
1146  * next_point  Move to the next point in the spiral pattern
1147  *  -    swirl is the swirl
1148  *  -    win is the window to update
1149  */
1150 static void
1151 next_point(SWIRL_P swirl)
1152 {
1153         /* more to do in this direction? */
1154         if (swirl->dir_done < swirl->dir_todo) {
1155                 /* move in the current direction */
1156                 switch (swirl->direction) {
1157                         case DRAW_RIGHT:
1158                                 swirl->x += swirl->r;
1159                                 break;
1160                         case DRAW_DOWN:
1161                                 swirl->y += swirl->r;
1162                                 break;
1163                         case DRAW_LEFT:
1164                                 swirl->x -= swirl->r;
1165                                 break;
1166                         case DRAW_UP:
1167                                 swirl->y -= swirl->r;
1168                                 break;
1169                 }
1170
1171                 /* done another point */
1172                 swirl->dir_done++;
1173         } else {
1174                 /* none drawn yet */
1175                 swirl->dir_done = 0;
1176
1177                 /* change direction - check and record if off screen */
1178                 switch (swirl->direction) {
1179                         case DRAW_RIGHT:
1180                                 swirl->direction = DRAW_DOWN;
1181                                 if (swirl->x > swirl->width - swirl->r) {
1182                                         /* skip these points */
1183                                         swirl->dir_done = swirl->dir_todo;
1184                                         swirl->y += (swirl->dir_todo * swirl->r);
1185
1186                                         /* check for finish */
1187                                         if (swirl->off_screen)
1188                                                 swirl->drawing = False;
1189                                         swirl->off_screen = True;
1190                                 } else
1191                                         swirl->off_screen = False;
1192                                 break;
1193                         case DRAW_DOWN:
1194                                 swirl->direction = DRAW_LEFT;
1195                                 swirl->dir_todo++;
1196                                 if (swirl->y > swirl->height - swirl->r) {
1197                                         /* skip these points */
1198                                         swirl->dir_done = swirl->dir_todo;
1199                                         swirl->x -= (swirl->dir_todo * swirl->r);
1200
1201                                         /* check for finish */
1202                                         if (swirl->off_screen)
1203                                                 swirl->drawing = False;
1204                                         swirl->off_screen = True;
1205                                 } else
1206                                         swirl->off_screen = False;
1207                                 break;
1208                         case DRAW_LEFT:
1209                                 swirl->direction = DRAW_UP;
1210                                 if (swirl->x < 0) {
1211                                         /* skip these points */
1212                                         swirl->dir_done = swirl->dir_todo;
1213                                         swirl->y -= (swirl->dir_todo * swirl->r);
1214
1215                                         /* check for finish */
1216                                         if (swirl->off_screen)
1217                                                 swirl->drawing = False;
1218                                         swirl->off_screen = True;
1219                                 } else
1220                                         swirl->off_screen = False;
1221                                 break;
1222                         case DRAW_UP:
1223                                 swirl->direction = DRAW_RIGHT;
1224                                 swirl->dir_todo++;
1225                                 if (swirl->y < 0) {
1226                                         /* skip these points */
1227                                         swirl->dir_done = swirl->dir_todo;
1228                                         swirl->x += (swirl->dir_todo * swirl->r);
1229
1230                                         /* check for finish */
1231                                         if (swirl->off_screen)
1232                                                 swirl->drawing = False;
1233                                         swirl->off_screen = True;
1234                                 } else
1235                                         swirl->off_screen = False;
1236                                 break;
1237                 }
1238         }
1239 }
1240
1241 /****************************************************************/
1242
1243 /* 
1244  * init_swirl
1245  *
1246  * Initialise things for swirling
1247  *
1248  * -      win is the window to draw in
1249  */
1250 void
1251 init_swirl(ModeInfo * mi)
1252 {
1253         Display    *display = MI_DISPLAY(mi);
1254         Window      window = MI_WINDOW(mi);
1255         SWIRL_P     swirl;
1256
1257         /* does the swirls array exist? */
1258         if (swirls == NULL) {
1259                 int         i;
1260
1261                 /* allocate an array, one entry for each screen */
1262                 swirls = (SWIRL_P) calloc(ScreenCount(display), sizeof (SWIRL));
1263
1264                 /* initialise them all */
1265                 for (i = 0; i < ScreenCount(display); i++)
1266                         initialise_swirl(mi, &swirls[i]);
1267         }
1268         /* get a pointer to this swirl */
1269         swirl = &(swirls[MI_SCREEN(mi)]);
1270
1271         /* get window parameters */
1272         swirl->win = window;
1273         swirl->width = MI_WIN_WIDTH(mi);
1274         swirl->height = MI_WIN_HEIGHT(mi);
1275         swirl->depth = MI_WIN_DEPTH(mi);
1276         swirl->rdepth = swirl->depth;
1277         swirl->visual = MI_VISUAL(mi);
1278
1279         if (swirl->depth > 16)
1280                 swirl->depth = 16;
1281
1282         /* initialise image for speeding up drawing */
1283         initialise_image(mi, swirl);
1284
1285         /* clear the window (before setting the colourmap) */
1286         XClearWindow(display, MI_WINDOW(mi));
1287
1288 #ifdef STANDALONE
1289
1290         swirl->rgb_values = mi->colors;
1291         swirl->colours = mi->npixels;
1292         swirl->dcolours = swirl->colours;
1293 /*      swirl->fixed_colourmap = !mi->writable_p;*/
1294
1295 #else /* !STANDALONE */
1296
1297         /* initialise the colours from which the colourmap is derived */
1298         initialise_colours(basic_colours, MI_SATURATION(mi));
1299
1300         /* set up the colour map */
1301         create_colourmap(mi, swirl);
1302
1303         /* attach the colour map to the window (if we have one) */
1304         if (!swirl->fixed_colourmap) {
1305 #if 1
1306                 setColormap(display, window, swirl->cmap, MI_WIN_IS_INWINDOW(mi));
1307 #else
1308                 XSetWindowColormap(display, window, swirl->cmap);
1309                 (void) XSetWMColormapWindows(display, window, &window, 1);
1310                 XInstallColormap(display, swirl->cmap);
1311 #endif
1312         }
1313 #endif /* STANDALONE */
1314
1315         /* resolution starts off chunky */
1316         swirl->resolution = MIN_RES + 1;
1317
1318         /* calculate the pixel step for this resulution */
1319         swirl->r = (1 << (swirl->resolution - 1));
1320
1321         /* how many knots? */
1322         swirl->n_knots = random_no((unsigned int) MI_BATCHCOUNT(mi) / 2) +
1323                 MI_BATCHCOUNT(mi) + 1;
1324
1325         /* what type of knots? */
1326         swirl->knot_type = ALL; /* for now */
1327
1328         /* use two_plane mode occaisionally */
1329         if (random_no(100) <= TWO_PLANE_PCNT) {
1330                 swirl->two_plane = swirl->first_plane = True;
1331                 swirl->max_resolution = 2;
1332         } else
1333                 swirl->two_plane = False;
1334
1335         /* fix the knot values */
1336         create_knots(swirl);
1337
1338         /* we are off */
1339         swirl->started = True;
1340         swirl->drawing = False;
1341 }
1342
1343 /****************************************************************/
1344
1345 /* 
1346  * draw_swirl
1347  *
1348  * Draw one iteration of swirling
1349  *
1350  * -      win is the window to draw in
1351  */
1352 void
1353 draw_swirl(ModeInfo * mi)
1354 {
1355         SWIRL_P     swirl = &(swirls[MI_SCREEN(mi)]);
1356
1357         /* are we going? */
1358         if (swirl->started) {
1359                 /* in the middle of drawing? */
1360                 if (swirl->drawing) {
1361 #ifdef STANDALONE
1362                   if (mi->writable_p)
1363                         rotate_colors(MI_DISPLAY(mi), MI_COLORMAP(mi),
1364                                                   swirl->rgb_values, swirl->colours, 1);
1365 #else  /* !STANDALONE */
1366                         /* rotate the colours */
1367                         install_map(MI_DISPLAY(mi), swirl, swirl->dshift);
1368 #endif /* !STANDALONE */
1369
1370                         /* draw a batch of points */
1371                         swirl->batch_todo = BATCH_DRAW;
1372                         while ((swirl->batch_todo > 0) && swirl->drawing) {
1373                                 /* draw a point */
1374                                 draw_point(mi, swirl);
1375
1376                                 /* move to the next point */
1377                                 next_point(swirl);
1378
1379                                 /* done a point */
1380                                 swirl->batch_todo--;
1381                         }
1382                 } else {
1383 #ifdef STANDALONE
1384                   if (mi->writable_p)
1385                         rotate_colors(MI_DISPLAY(mi), MI_COLORMAP(mi),
1386                                                   swirl->rgb_values, swirl->colours, 1);
1387 #else  /* !STANDALONE */
1388                         /* rotate the colours */
1389                         install_map(MI_DISPLAY(mi), swirl, swirl->shift);
1390 #endif /* !STANDALONE */
1391
1392                         /* time for a higher resolution? */
1393                         if (swirl->resolution > swirl->max_resolution) {
1394                                 /* move to higher resolution */
1395                                 swirl->resolution--;
1396
1397                                 /* calculate the pixel step for this resulution */
1398                                 swirl->r = (1 << (swirl->resolution - 1));
1399
1400                                 /* start drawing again */
1401                                 swirl->drawing = True;
1402
1403                                 /* start in the middle of the screen */
1404                                 swirl->x = (swirl->width - swirl->r) / 2;
1405                                 swirl->y = (swirl->height - swirl->r) / 2;
1406
1407                                 /* initialise spiral drawing parameters */
1408                                 swirl->direction = DRAW_RIGHT;
1409                                 swirl->dir_todo = 1;
1410                                 swirl->dir_done = 0;
1411                         } else {
1412                                 /* all done, decide when to restart */
1413                                 if (swirl->start_again == -1) {
1414                                         /* start the counter */
1415                                         swirl->start_again = RESTART;
1416                                 } else if (swirl->start_again == 0) {
1417                                         /* reset the counter */
1418                                         swirl->start_again = -1;
1419
1420 #ifdef STANDALONE
1421                                         /* Pick a new colormap! */
1422                                         XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi));
1423                                         free_colors (MI_DISPLAY(mi), MI_COLORMAP(mi),
1424                                                                  mi->colors, mi->npixels);
1425                                         make_smooth_colormap (MI_DISPLAY(mi),
1426                                                                                   MI_VISUAL(mi),
1427                                                                                   MI_COLORMAP(mi),
1428                                                                                   mi->colors, &mi->npixels, True,
1429                                                                                   &mi->writable_p, True);
1430                                         swirl->colours = mi->npixels;
1431 #endif /* STANDALONE */
1432
1433                                         /* start again */
1434                                         init_swirl(mi);
1435                                 } else
1436                                         /* decrement the counter */
1437                                         swirl->start_again--;
1438                         }
1439                 }
1440         }
1441 }
1442
1443 /****************************************************************/
1444
1445 void
1446 release_swirl(ModeInfo * mi)
1447 {
1448         /* does the swirls array exist? */
1449         if (swirls != NULL) {
1450                 int         i;
1451
1452                 /* free them all */
1453                 for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
1454                         SWIRL_P     swirl = &(swirls[i]);
1455
1456                         if (swirl->cmap != (Colormap) NULL)
1457                                 XFreeColormap(MI_DISPLAY(mi), swirl->cmap);
1458                         if (swirl->rgb_values != NULL)
1459                                 XFree((void *) swirl->rgb_values);
1460                         if (swirl->ximage != NULL)
1461                                 XDestroyImage(swirl->ximage);
1462                         if (swirl->knots)
1463                                 (void) free((void *) swirl->knots);
1464                 }
1465                 /* deallocate an array, one entry for each screen */
1466                 (void) free((void *) swirls);
1467                 swirls = NULL;
1468         }
1469 }
1470
1471 /****************************************************************/
1472
1473 void
1474 refresh_swirl(ModeInfo * mi)
1475 {
1476         SWIRL_P     swirl = &(swirls[MI_SCREEN(mi)]);
1477
1478         if (swirl->started) {
1479                 if (swirl->drawing)
1480                         swirl->resolution = swirl->resolution + 1;
1481                 swirl->drawing = False;
1482         }
1483 }