1 /* -*- Mode: C; tab-width: 4 -*-
2 * swirl --- swirly color-cycling patterns.
5 static const char sccsid[] = "@(#)swirl.c 4.00 97/01/01 xlockmore";
8 /* Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
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.
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.
22 * 09-Oct-2016: dmo2118@gmail.com: Updated for new xshm.c.
23 * 13-May-1997: jwz@jwz.org: turned into a standalone program.
24 * 21-Apr-1995: improved startup time for TrueColour displays
25 * (limited to 16bpp to save memory) S.Early <sde1000@cam.ac.uk>
26 * 09-Jan-1995: fixed colour maps (more colourful) and the image now spirals
27 * outwards from the centre with a fixed number of points drawn
28 * every iteration. Thanks to M.Dobie <mrd@ecs.soton.ac.uk>.
29 * 1994: written. Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
30 * based on original code by R.Taylor
34 # define DEFAULTS "*count: 5 \n" \
38 "*fpsSolid: true \n" \
39 "*ignoreRotation: True \n" \
42 # define SMOOTH_COLORS
43 # define WRITABLE_COLORS
44 # define release_swirl 0
45 # define reshape_swirl 0
46 # define swirl_handle_event 0
47 # include "xlockmore.h" /* from the xscreensaver distribution */
49 #else /* !STANDALONE */
50 # include "xlock.h" /* from the xlockmore distribution */
51 # undef HAVE_XSHM_EXTENSION
52 #endif /* !STANDALONE */
54 ENTRYPOINT ModeSpecOpt swirl_opts = {
55 0, NULL, 0, NULL, NULL };
59 /****************************************************************/
61 #define MASS 4 /* maximum mass of a knot */
62 #define MIN_RES 5 /* minimim resolution (>= MIN_RES) */
63 #define MAX_RES 1 /* maximum resolution (>0) */
64 #define TWO_PLANE_PCNT 30 /* probability for two plane mode (0-100) */
65 #define RESTART 2500 /* number of cycles before restart */
66 #define BATCH_DRAW 100 /* points to draw per iteration */
81 int x, y; /* position */
83 KNOT_T t; /* type in the first (or only) plane */
84 KNOT_T T; /* type in second plane if there is one */
85 int M; /* mass in second plane if there is one */
88 /* a colour specification */
89 typedef struct Colour {
90 unsigned short r, g, b;
93 /* drawing direction */
95 DRAW_RIGHT, DRAW_DOWN, DRAW_LEFT, DRAW_UP
98 /****************************************************************/
100 /* data associated with a swirl window */
101 typedef struct swirl_data {
102 /* window paramaters */
103 Window win; /* the window */
104 int width, height; /* window size */
105 int depth; /* depth */
106 int rdepth; /* real depth (for XImage) */
107 Visual *visual; /* visual */
109 /* swirl drawing parameters */
110 int n_knots; /* number of knots */
111 KNOT_P knots; /* knot details */
112 KNOT_T knot_type; /* general type of knots */
113 int resolution; /* drawing resolution, 1..5 */
114 int max_resolution; /* maximum resolution, MAX_RES */
115 int r; /* pixel step */
116 Bool two_plane; /* two plane mode? */
117 Bool first_plane; /* doing first plane? */
118 int start_again; /* when to restart */
120 /* spiral drawing parameters */
121 int x, y; /* current point */
122 DIR_T direction; /* current direction */
123 int dir_todo, dir_done; /* how many points in current direction? */
124 int batch_todo, batch_done; /* how many points in this batch */
125 Bool started, drawing; /* are we drawing? */
128 unsigned char *image; /* image data */
130 XShmSegmentInfo shm_info;
133 int colours; /* how many colours possible */
134 int dcolours; /* how many colours for shading */
136 Bool fixed_colourmap; /* fixed colourmap? */
137 #endif /* !STANDALONE */
138 Bool monochrome; /* monochrome? */
139 Colormap cmap; /* colour map for the window */
140 XColor *rgb_values; /* colour definitions array */
142 int current_map; /* current colour map, 0..dcolours-1 */
143 unsigned long fg, bg, white, black; /* black and white pixel values */
144 int shift; /* colourmap shift */
145 int dshift; /* colourmap shift while drawing */
146 XColor fgcol, bgcol; /* foreground and background colour specs */
147 #endif /* !STANDALONE */
151 #define SWIRLCOLOURS 13
155 static COLOUR basic_colours[SWIRLCOLOURS];
156 #endif /* !STANDALONE */
158 /* an array of swirls for each screen */
159 static SWIRL_P swirls = NULL;
164 Return a random integer between 0 and n inclusive
166 - n is the maximum number
168 Returns a random integer */
171 random_no(unsigned int n)
173 return ((int) ((n + 1) * (double) LRAND() / MAXRAND));
176 /****************************************************************/
181 Initialise all the swirl data
183 - swirl is the swirl data */
186 initialise_swirl(ModeInfo * mi, SWIRL_P swirl)
189 Display *display = MI_DISPLAY(mi);
190 #endif /* !STANDALONE */
192 swirl->width = 0; /* width and height of window */
196 swirl->visual = NULL;
197 swirl->resolution = MIN_RES + 1; /* current resolution */
198 swirl->max_resolution = MAX_RES; /* maximum resolution */
199 swirl->n_knots = 0; /* number of knots */
200 swirl->knot_type = ALL; /* general type of knots */
201 swirl->two_plane = False; /* two plane mode? */
202 swirl->first_plane = False; /* doing first plane? */
203 swirl->start_again = -1; /* restart counter */
205 /* drawing parameters */
208 swirl->started = False;
209 swirl->drawing = False;
212 swirl->image = NULL; /* image data */
213 swirl->ximage = NULL;
216 swirl->colours = 0; /* how many colours possible */
217 swirl->dcolours = 0; /* how many colours for shading */
218 swirl->cmap = (Colormap) NULL;
219 swirl->rgb_values = NULL; /* colour definitions array */
221 swirl->current_map = 0; /* current colour map, 0..dcolours-1 */
223 /* set up fg fb colour specs */
224 swirl->white = MI_WIN_WHITE_PIXEL(mi);
225 swirl->black = MI_WIN_BLACK_PIXEL(mi);
226 #endif /* !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 */
239 /****************************************************************/
244 * Initialise the image for drawing to
246 * - swirl is the swirl data
249 initialise_image(ModeInfo * mi, SWIRL_P swirl)
251 Display *dpy = MI_DISPLAY(mi);
253 if (swirl->ximage != NULL)
254 destroy_xshm_image(dpy, swirl->ximage, &swirl->shm_info);
256 swirl->ximage = create_xshm_image(dpy, swirl->visual, swirl->rdepth,
257 ZPixmap, &swirl->shm_info,
258 swirl->width, swirl->height);
261 /****************************************************************/
267 * Initialise the list of colours from which the colourmaps are derived
269 * - colours is the array to initialise
270 * - saturation is the saturation value to use 0->grey,
271 * 1.0->full saturation
274 initialise_colours(COLOUR * colours, float saturate)
278 /* start off fully saturated, medium and bright colours */
279 colours[0].r = 0xA000;
280 colours[0].g = 0x0000;
281 colours[0].b = 0x0000;
282 colours[1].r = 0xD000;
283 colours[1].g = 0x0000;
284 colours[1].b = 0x0000;
285 colours[2].r = 0x0000;
286 colours[2].g = 0x6000;
287 colours[2].b = 0x0000;
288 colours[3].r = 0x0000;
289 colours[3].g = 0x9000;
290 colours[3].b = 0x0000;
291 colours[4].r = 0x0000;
292 colours[4].g = 0x0000;
293 colours[4].b = 0xC000;
294 colours[5].r = 0x0000;
295 colours[5].g = 0x0000;
296 colours[5].b = 0xF000;
297 colours[6].r = 0xA000;
298 colours[6].g = 0x6000;
299 colours[6].b = 0x0000;
300 colours[7].r = 0xD000;
301 colours[7].g = 0x9000;
302 colours[7].b = 0x0000;
303 colours[8].r = 0xA000;
304 colours[8].g = 0x0000;
305 colours[8].b = 0xC000;
306 colours[9].r = 0xD000;
307 colours[9].g = 0x0000;
308 colours[9].b = 0xF000;
309 colours[10].r = 0x0000;
310 colours[10].g = 0x6000;
311 colours[10].b = 0xC000;
312 colours[11].r = 0x0000;
313 colours[11].g = 0x9000;
314 colours[11].b = 0xF000;
315 colours[12].r = 0xA000;
316 colours[12].g = 0xA000;
317 colours[12].b = 0xA000;
319 /* add white for low saturation */
320 for (i = 0; i < SWIRLCOLOURS - 1; i++) {
321 unsigned short max_rg, max;
323 /* what is the max intensity for this colour? */
324 max_rg = (colours[i].r > colours[i].g) ? colours[i].r : colours[i].g;
325 max = (max_rg > colours[i].b) ? max_rg : colours[i].b;
327 /* bring elements up to max as saturation approaches 0.0 */
328 colours[i].r += (unsigned short) ((float) (1.0 - saturate) *
329 ((float) max - colours[i].r));
330 colours[i].g += (unsigned short) ((float) (1.0 - saturate) *
331 ((float) max - colours[i].g));
332 colours[i].b += (unsigned short) ((float) (1.0 - saturate) *
333 ((float) max - colours[i].b));
336 #endif /* !STANDALONE */
338 /****************************************************************/
342 * set_black_and_white
344 * Set the entries for foreground & background pixels and
345 * WhitePixel & BlackPixel in an array of colour specifications.
347 * - swirl is the swirl data
348 * - values is the array of specifications
351 set_black_and_white(SWIRL_P swirl, XColor * values)
353 unsigned long white, black;
355 /* where is black and white? */
356 white = swirl->white;
357 black = swirl->black;
359 /* set black and white up */
360 values[white].flags = DoRed | DoGreen | DoBlue;
361 values[white].pixel = white;
362 values[white].red = 0xFFFF;
363 values[white].green = 0xFFFF;
364 values[white].blue = 0xFFFF;
365 values[black].flags = DoRed | DoGreen | DoBlue;
366 values[black].pixel = black;
367 values[black].red = 0;
368 values[black].green = 0;
369 values[black].blue = 0;
371 /* copy the colour specs from the original entries */
372 values[swirl->fg] = swirl->fgcol;
373 values[swirl->bg] = swirl->bgcol;
376 /****************************************************************/
381 * Set an entry in an array of XColor specifications. The given entry will be
382 * set to the given colour. If the entry corresponds to the foreground,
383 * background, WhitePixel, or BlackPixel it is ignored and the given colour
384 * is is put in the next entry.
386 * Therefore, the given colour may be placed up to four places after the
387 * specified entry in the array, if foreground, background, white, or black
390 * - swirl is the swirl data
391 * - value points to a pointer to the array entry. It gets updated to
392 * point to the next free entry.
393 * - pixel points to the current pixel number. It gets updated.
394 * - c points to the colour to add
397 set_colour(SWIRL_P swirl, XColor ** value, unsigned long *pixel, COLOUR_P c)
400 unsigned long fg, bg, white, black;
402 /* where are foreground, background, white, and black? */
405 white = swirl->white;
406 black = swirl->black;
408 /* haven't set it yet */
411 /* try and set the colour */
413 (**value).flags = DoRed | DoGreen | DoBlue;
414 (**value).pixel = *pixel;
416 /* white, black, fg, bg, or a colour? */
417 if ((*pixel != fg) && (*pixel != bg) &&
418 (*pixel != white) && (*pixel != black)) {
419 (**value).red = c->r;
420 (**value).green = c->g;
421 (**value).blue = c->b;
423 /* now we've done it */
432 /****************************************************************/
437 * Get an entry from an array of XColor specifications. The next colour from
438 * the array will be returned. Foreground, background, WhitePixel, or
439 * BlackPixel will be ignored.
441 * - swirl is the swirl data
442 * - value points the array entry. It is updated to point to the entry
443 * following the one returned.
444 * - c is set to the colour found
447 get_colour(SWIRL_P swirl, XColor ** value, COLOUR_P c)
450 unsigned long fg, bg, white, black;
452 /* where is white and black? */
455 white = swirl->white;
456 black = swirl->black;
458 /* haven't set it yet */
461 /* try and set the colour */
463 /* black, white or a colour? */
464 if (((*value)->pixel != fg) && ((*value)->pixel != bg) &&
465 ((*value)->pixel != white) && ((*value)->pixel != black)) {
466 c->r = (*value)->red;
467 c->g = (*value)->green;
468 c->b = (*value)->blue;
470 /* now we've done it */
477 #endif /* !STANDALONE */
479 /****************************************************************/
485 * Generate n colours between c1 and c2. n XColors at *value are set up with
486 * ascending pixel values.
488 * If the pixel range includes BlackPixel or WhitePixel they are set to black
489 * and white respectively but otherwise ignored. Therefore, up to n+2 colours
490 * may actually be set by this function.
492 * - swirl is the swirl data
493 * - values points a pointer to an array of XColors to update
494 * - pixel points to the pixel number to start at
495 * - k n is the number of colours to generate
496 * - c1, c2 are the colours to interpolate between
499 interpolate(SWIRL_P swirl, XColor ** values, unsigned long *pixel, int n, COLOUR_P c1, COLOUR_P c2)
508 for (i = 0; i < n / 2 && (int) *pixel < swirl->colours; i++) {
509 /* work out the colour */
510 r = c1->r + 2 * i * ((int) c2->r) / n;
511 c.r = (r > (int) maxv) ? maxv : r;
512 g = c1->g + 2 * i * ((int) c2->g) / n;
513 c.g = (g > (int) maxv) ? maxv : g;
514 b = c1->b + 2 * i * ((int) c2->b) / n;
515 c.b = (b > (int) maxv) ? maxv : b;
518 set_colour(swirl, values, pixel, &c);
520 for (i = n / 2; i >= 0 && (int) *pixel < swirl->colours; i--) {
521 r = c2->r + 2 * i * ((int) c1->r) / n;
522 c.r = (r > (int) maxv) ? maxv : r;
523 g = c2->g + 2 * i * ((int) c1->g) / n;
524 c.g = (g > (int) maxv) ? maxv : g;
525 b = c2->b + 2 * i * ((int) c1->b) / n;
526 c.b = (b > (int) maxv) ? maxv : b;
529 set_colour(swirl, values, pixel, &c);
533 /****************************************************************/
538 * Generate a `random' closed loop colourmap that occupies the whole colour
541 * - swirl is the swirl data
542 * - values is the array of colour definitions to set up
545 basic_map(SWIRL_P swirl, XColor * values)
549 unsigned short r1, g1, b1, r2, g2, b2, r3, g3, b3;
554 /* start at the beginning of the colour map */
558 /* choose 3 different basic colours at random */
559 for (i = 0; i < 3;) {
563 /* choose colour i */
564 c[i] = basic_colours[random_no(SWIRLCOLOURS - 1)];
566 /* assume different */
569 /* different from the rest? */
570 for (j = 0; j < i; j++)
571 if ((c[i].r == c[j].r) &&
572 (c[i].g == c[j].g) &&
576 /* ready for the next colour? */
581 /* extract components into variables */
592 /* work out the lengths of each side of the triangle */
593 L1 = (int) sqrt((((double) r1 - r2) * ((double) r1 - r2) +
594 ((double) g1 - g2) * ((double) g1 - g2) +
595 ((double) b1 - b2) * ((double) b1 - b2)));
597 L2 = (int) sqrt((((double) r3 - r2) * ((double) r3 - r2) +
598 ((double) g3 - g2) * ((double) g3 - g2) +
599 ((double) b3 - b2) * ((double) b3 - b2)));
601 L3 = (int) sqrt((((double) r1 - r3) * ((double) r1 - r3) +
602 ((double) g1 - g3) * ((double) g1 - g3) +
603 ((double) b1 - b3) * ((double) b1 - b3)));
607 /* allocate colours in proportion to the lengths of the sides */
608 interpolate(swirl, &value, &pixel,
609 (int) ((double) swirl->dcolours * ((double) L1 / (double) L)) + 1, c, c + 1);
610 interpolate(swirl, &value, &pixel,
611 (int) ((double) swirl->dcolours * ((double) L2 / (double) L)) + 1, c + 1, c + 2);
612 interpolate(swirl, &value, &pixel,
613 (int) ((double) swirl->dcolours * ((double) L3 / (double) L)) + 1, c + 2, c);
615 /* fill up any remaining slots (due to rounding) */
616 while ((int) pixel < swirl->colours) {
617 /* repeat the last colour */
618 set_colour(swirl, &value, &pixel, c);
621 /* ensure black and white are correct */
622 if (!swirl->fixed_colourmap)
623 set_black_and_white(swirl, values);
626 /****************************************************************/
631 * Generate pre-rotated versions of the colour specifications
633 * - swirl is the swirl data
634 * - values is an array of colour specifications
637 pre_rotate(SWIRL_P swirl, XColor * values)
644 /* how many colours to display? */
645 dcolours = swirl->dcolours;
647 /* start at the first map */
649 dest = values + swirl->colours;
651 /* generate dcolours-1 rotated maps */
652 for (i = 0; i < dcolours - 1; i++) {
655 /* start at the first pixel */
658 /* remember the first one and skip it */
659 get_colour(swirl, &src, &first);
661 /* put a rotated version of src at dest */
662 for (j = 0; j < dcolours - 1; j++) {
665 /* get the source colour */
666 get_colour(swirl, &src, &c);
669 set_colour(swirl, &dest, &pixel, &c);
672 /* put the first one at the end */
673 set_colour(swirl, &dest, &pixel, &first);
675 /* NB: src and dest should now be ready for the next table */
677 /* ensure black and white are properly set */
678 set_black_and_white(swirl, src);
682 /****************************************************************/
687 * Create a read/write colourmap to use
689 * - swirl is the swirl data
693 create_colourmap(ModeInfo * mi, SWIRL_P swirl)
695 Display *display = MI_DISPLAY(mi);
700 unsigned long redmask, greenmask, bluemask;
702 swirl->fixed_colourmap = !setupColormap(mi, &(swirl->colours),
703 &truecolor, &redmask, &greenmask, &bluemask);
704 preserve = preserveColors(swirl->fg, swirl->bg, swirl->white, swirl->black);
706 /* how many colours should we animate? */
707 swirl->dcolours = (swirl->colours > preserve + 1) ?
708 swirl->colours - preserve : swirl->colours;
710 if (MI_NPIXELS(mi) < 2)
713 /* how fast to shift the colourmap? */
714 swirl->shift = (swirl->colours > 64) ? swirl->colours / 64 : 1;
715 swirl->dshift = (swirl->shift > 1) ? swirl->shift * 2 : 1;
717 /* how may colour map rotations are there? */
718 n_rotations = (swirl->fixed_colourmap) ? 1 : swirl->dcolours;
720 /* allocate space for colour definitions (if not already there) */
721 if (swirl->rgb_values == NULL) {
722 swirl->rgb_values = (XColor *) calloc((swirl->colours + 3) * n_rotations,
725 /* create a colour map */
726 if (!swirl->fixed_colourmap)
728 XCreateColormap(display, swirl->win, swirl->visual, AllocAll);
730 /* select a set of colours for the colour map */
731 basic_map(swirl, swirl->rgb_values);
733 /* are we rotating them? */
734 if (!swirl->fixed_colourmap) {
735 /* generate rotations of the colour maps */
736 pre_rotate(swirl, swirl->rgb_values);
738 /* store the colours in the colour map */
739 XStoreColors(display, swirl->cmap, swirl->rgb_values, swirl->colours);
746 for (i = 0; (int) t > 0; i++, t >>= 1);
749 for (i = 0; (int) t > 0; i++, t >>= 1);
752 for (i = 0; (int) t > 0; i++, t >>= 1);
754 for (i = 0; i < swirl->colours; i++)
755 swirl->rgb_values[i].pixel =
756 ((rsh > 0 ? (swirl->rgb_values[i].red) >> rsh :
757 (swirl->rgb_values[i].red) << (-rsh)) & redmask) |
758 ((gsh > 0 ? (swirl->rgb_values[i].green) >> gsh :
759 (swirl->rgb_values[i].green) << (-gsh)) & greenmask) |
760 ((bsh > 0 ? (swirl->rgb_values[i].blue) >> bsh :
761 (swirl->rgb_values[i].blue) << (-bsh)) & bluemask);
763 /* lookup the colours in the fixed colour map */
764 for (i = 0; i < swirl->colours; i++)
765 (void) XAllocColor(display, MI_COLORMAP(mi),
766 &(swirl->rgb_values[i]));
771 /****************************************************************/
776 * Install a new set of colours into the colour map
778 * - dpy is the display
779 * - swirl is the swirl data
780 * - shift is the amount to rotate the colour map by
783 install_map(Display * dpy, SWIRL_P swirl, int shift)
785 if (!swirl->fixed_colourmap) {
786 /* shift the colour map */
787 swirl->current_map = (swirl->current_map + shift) %
791 XStoreColors(dpy, swirl->cmap,
793 swirl->current_map * swirl->colours,
797 #endif /* !STANDALONE */
799 /****************************************************************/
804 * Initialise the array of knot
806 * swirl is the swirl data
809 create_knots(SWIRL_P swirl)
812 Bool orbit, wheel, picasso, ray, hook;
815 /* create array for knots */
817 (void) free((void *) swirl->knots);
818 swirl->knots = (KNOT_P) calloc(swirl->n_knots, sizeof (KNOT));
821 orbit = wheel = picasso = ray = hook = False;
823 /* what types do we have? */
824 if ((int) swirl->knot_type & (int) ALL) {
825 orbit = wheel = ray = hook = True;
827 if ((int) swirl->knot_type & (int) ORBIT)
829 if ((int) swirl->knot_type & (int) WHEEL)
831 if ((int) swirl->knot_type & (int) PICASSO)
833 if ((int) swirl->knot_type & (int) RAY)
835 if ((int) swirl->knot_type & (int) HOOK)
839 /* initialise each knot */
841 for (k = 0; k < swirl->n_knots; k++) {
843 knot->x = random_no((unsigned int) swirl->width);
844 knot->y = random_no((unsigned int) swirl->height);
847 knot->m = random_no(MASS) + 1;
849 /* can be negative */
850 if (random_no(100) > 50)
855 while (knot->t == NONE) {
856 /* choose a random one from the types available */
857 switch (random_no(4)) {
881 /* if two planes, do same for second plane */
882 if (swirl->two_plane) {
884 while (knot->T == NONE || knot->T == knot->t) {
885 /* choose a different type */
886 switch (random_no(4)) {
915 /****************************************************************/
920 * Work out the pixel value at i, j. Ensure it does not clash with BlackPixel
923 * - swirl is the swirl data
924 * - i, j is the point to calculate
926 * Returns the value of the point
929 do_point(SWIRL_P swirl, int i, int j)
931 int tT, k, value, add;
932 double dx, dy, theta, dist;
933 int dcolours, qcolours;
937 /* how many colours? */
938 dcolours = swirl->dcolours;
939 qcolours = dcolours / 4;
941 /* colour step round a circle */
942 rads = (double) dcolours / (2.0 * M_PI);
947 /* go through all the knots */
949 for (k = 0; k < swirl->n_knots; k++) {
953 /* in two_plane mode get the appropriate knot type */
954 if (swirl->two_plane)
955 tT = (int) ((swirl->first_plane) ? knot->t : knot->T);
959 /* distance from knot */
960 dist = sqrt(dx * dx + dy * dy);
962 /* nothing to add at first */
965 /* work out the contribution (if close enough) */
969 add = (int) (dcolours / (1.0 + 0.01 * abs(knot->m) * dist));
972 /* Avoid atan2: DOMAIN error message */
973 if (dy == 0.0 && dx == 0.0)
976 theta = (atan2(dy, dx) + M_PI) / M_PI;
978 add = (int) (dcolours * theta +
979 sin(0.1 * knot->m * dist) *
980 qcolours * exp(-0.01 * dist));
982 add = (int) (dcolours * (theta - 1.0) +
983 sin(0.1 * knot->m * dist) *
984 qcolours * exp(-0.01 * dist));
987 add = (int) (dcolours *
988 fabs(cos(0.002 * knot->m * dist)));
991 /* Avoid atan2: DOMAIN error message */
992 if (dy == 0.0 && dx == 0.0)
995 add = (int) (dcolours * fabs(sin(2.0 * atan2(dy, dx))));
999 /* Avoid atan2: DOMAIN error message */
1000 if (dy == 0.0 && dx == 0.0)
1001 add = (int) (0.05 * (abs(knot->m) - 1) * dist);
1003 add = (int) (rads * atan2(dy, dx) +
1004 0.05 * (abs(knot->m) - 1) * dist);
1007 /* for a +ve mass add on the contribution else take it off */
1018 swirl->first_plane = (!swirl->first_plane);
1020 /* make sure we handle -ve values properly */
1022 value = (value % dcolours) + 2;
1024 value = dcolours - (abs(value) % (dcolours - 1));
1027 /* if fg and bg are 1 and 0 we should be OK, but just in case */
1028 while ((dcolours > 2) &&
1029 (((value % swirl->colours) == (int) swirl->fg) ||
1030 ((value % swirl->colours) == (int) swirl->bg) ||
1031 ((value % swirl->colours) == (int) swirl->white) ||
1032 ((value % swirl->colours) == (int) swirl->black))) {
1035 #endif /* !STANDALONE */
1037 /* definitely make sure it is in range */
1038 value = value % swirl->colours;
1040 /* lookup the pixel value if necessary */
1042 if (swirl->fixed_colourmap && swirl->dcolours > 2)
1044 value = swirl->rgb_values[value].pixel;
1047 return ((unsigned long) value);
1050 /****************************************************************/
1055 * Draw a square block of points with the same value.
1057 * - ximage is the XImage to draw on.
1058 * - x, y is the top left corner
1059 * - s is the length of each side
1063 draw_block(XImage * ximage, int x, int y, int s, unsigned long v)
1067 for (a = 0; a < s; a++)
1068 for (b = 0; b < s; b++) {
1069 XPutPixel(ximage, x + b, y + a, v);
1073 /****************************************************************/
1076 * draw_point Draw the current point in a swirl pattern onto the XImage
1078 * - swirl is the swirl
1079 * - win is the window to update
1082 draw_point(ModeInfo * mi, SWIRL_P swirl)
1087 /* get current point coordinates and resolution */
1092 /* check we are within the window */
1093 if ((x < 0) || (x > swirl->width - r) || (y < 0) || (y > swirl->height - r))
1096 /* what style are we drawing? */
1097 if (swirl->two_plane) {
1100 /* halve the block size */
1103 /* interleave blocks at half r */
1104 draw_block(swirl->ximage, x, y, r2, do_point(swirl, x, y));
1105 draw_block(swirl->ximage, x + r2, y, r2, do_point(swirl, x + r2, y));
1106 draw_block(swirl->ximage, x + r2, y + r2, r2, do_point(swirl,
1108 draw_block(swirl->ximage, x, y + r2, r2, do_point(swirl, x, y + r2));
1110 draw_block(swirl->ximage, x, y, r, do_point(swirl, x, y));
1112 /* update the screen */
1114 put_xshm_image(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1115 x, y, x, y, r, r, &swirl->shm_info);
1118 /****************************************************************/
1121 * next_point Move to the next point in the spiral pattern
1122 * - swirl is the swirl
1123 * - win is the window to update
1126 next_point(SWIRL_P swirl)
1128 /* more to do in this direction? */
1129 if (swirl->dir_done < swirl->dir_todo) {
1130 /* move in the current direction */
1131 switch (swirl->direction) {
1133 swirl->x += swirl->r;
1136 swirl->y += swirl->r;
1139 swirl->x -= swirl->r;
1142 swirl->y -= swirl->r;
1146 /* done another point */
1149 /* none drawn yet */
1150 swirl->dir_done = 0;
1152 /* change direction - check and record if off screen */
1153 switch (swirl->direction) {
1155 swirl->direction = DRAW_DOWN;
1156 if (swirl->x > swirl->width - swirl->r) {
1157 /* skip these points */
1158 swirl->dir_done = swirl->dir_todo;
1159 swirl->y += (swirl->dir_todo * swirl->r);
1161 /* check for finish */
1162 if (swirl->off_screen)
1163 swirl->drawing = False;
1164 swirl->off_screen = True;
1166 swirl->off_screen = False;
1169 swirl->direction = DRAW_LEFT;
1171 if (swirl->y > swirl->height - swirl->r) {
1172 /* skip these points */
1173 swirl->dir_done = swirl->dir_todo;
1174 swirl->x -= (swirl->dir_todo * swirl->r);
1176 /* check for finish */
1177 if (swirl->off_screen)
1178 swirl->drawing = False;
1179 swirl->off_screen = True;
1181 swirl->off_screen = False;
1184 swirl->direction = DRAW_UP;
1186 /* skip these points */
1187 swirl->dir_done = swirl->dir_todo;
1188 swirl->y -= (swirl->dir_todo * swirl->r);
1190 /* check for finish */
1191 if (swirl->off_screen)
1192 swirl->drawing = False;
1193 swirl->off_screen = True;
1195 swirl->off_screen = False;
1198 swirl->direction = DRAW_RIGHT;
1201 /* skip these points */
1202 swirl->dir_done = swirl->dir_todo;
1203 swirl->x += (swirl->dir_todo * swirl->r);
1205 /* check for finish */
1206 if (swirl->off_screen)
1207 swirl->drawing = False;
1208 swirl->off_screen = True;
1210 swirl->off_screen = False;
1216 /****************************************************************/
1221 * Initialise things for swirling
1223 * - win is the window to draw in
1226 init_swirl(ModeInfo * mi)
1228 Display *display = MI_DISPLAY(mi);
1229 Window window = MI_WINDOW(mi);
1232 MI_INIT (mi, swirls);
1233 swirl = &(swirls[MI_SCREEN(mi)]);
1234 initialise_swirl(mi, swirl);
1236 /* get window parameters */
1237 swirl->win = window;
1238 swirl->width = MI_WIN_WIDTH(mi);
1239 swirl->height = MI_WIN_HEIGHT(mi);
1240 swirl->depth = MI_WIN_DEPTH(mi);
1241 swirl->rdepth = swirl->depth;
1242 swirl->visual = MI_VISUAL(mi);
1244 if (swirl->depth > 16)
1247 /* initialise image for speeding up drawing */
1248 initialise_image(mi, swirl);
1250 /* clear the window (before setting the colourmap) */
1251 XClearWindow(display, MI_WINDOW(mi));
1255 swirl->rgb_values = mi->colors;
1256 swirl->colours = mi->npixels;
1257 swirl->dcolours = swirl->colours;
1258 /* swirl->fixed_colourmap = !mi->writable_p;*/
1260 #else /* !STANDALONE */
1262 /* initialise the colours from which the colourmap is derived */
1263 initialise_colours(basic_colours, MI_SATURATION(mi));
1265 /* set up the colour map */
1266 create_colourmap(mi, swirl);
1268 /* attach the colour map to the window (if we have one) */
1269 if (!swirl->fixed_colourmap) {
1271 setColormap(display, window, swirl->cmap, MI_WIN_IS_INWINDOW(mi));
1273 XSetWindowColormap(display, window, swirl->cmap);
1274 (void) XSetWMColormapWindows(display, window, &window, 1);
1275 XInstallColormap(display, swirl->cmap);
1278 #endif /* STANDALONE */
1280 /* resolution starts off chunky */
1281 swirl->resolution = MIN_RES + 1;
1283 /* calculate the pixel step for this resulution */
1284 swirl->r = (1 << (swirl->resolution - 1));
1286 /* how many knots? */
1287 swirl->n_knots = random_no((unsigned int) MI_BATCHCOUNT(mi) / 2) +
1288 MI_BATCHCOUNT(mi) + 1;
1290 /* what type of knots? */
1291 swirl->knot_type = ALL; /* for now */
1293 /* use two_plane mode occaisionally */
1294 if (random_no(100) <= TWO_PLANE_PCNT) {
1295 swirl->two_plane = swirl->first_plane = True;
1296 swirl->max_resolution = 2;
1298 swirl->two_plane = False;
1300 /* fix the knot values */
1301 create_knots(swirl);
1304 swirl->started = True;
1305 swirl->drawing = False;
1308 /****************************************************************/
1313 * Draw one iteration of swirling
1315 * - win is the window to draw in
1318 draw_swirl(ModeInfo * mi)
1320 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1323 if (swirl->started) {
1324 /* in the middle of drawing? */
1325 if (swirl->drawing) {
1328 rotate_colors(mi->xgwa.screen, MI_COLORMAP(mi),
1329 swirl->rgb_values, swirl->colours, 1);
1330 #else /* !STANDALONE */
1331 /* rotate the colours */
1332 install_map(MI_DISPLAY(mi), swirl, swirl->dshift);
1333 #endif /* !STANDALONE */
1335 /* draw a batch of points */
1336 swirl->batch_todo = BATCH_DRAW;
1337 while ((swirl->batch_todo > 0) && swirl->drawing) {
1339 draw_point(mi, swirl);
1341 /* move to the next point */
1345 swirl->batch_todo--;
1350 rotate_colors(mi->xgwa.screen, MI_COLORMAP(mi),
1351 swirl->rgb_values, swirl->colours, 1);
1352 #else /* !STANDALONE */
1353 /* rotate the colours */
1354 install_map(MI_DISPLAY(mi), swirl, swirl->shift);
1355 #endif /* !STANDALONE */
1357 /* time for a higher resolution? */
1358 if (swirl->resolution > swirl->max_resolution) {
1359 /* move to higher resolution */
1360 swirl->resolution--;
1362 /* calculate the pixel step for this resulution */
1363 swirl->r = (1 << (swirl->resolution - 1));
1365 /* start drawing again */
1366 swirl->drawing = True;
1368 /* start in the middle of the screen */
1369 swirl->x = (swirl->width - swirl->r) / 2;
1370 swirl->y = (swirl->height - swirl->r) / 2;
1372 /* initialise spiral drawing parameters */
1373 swirl->direction = DRAW_RIGHT;
1374 swirl->dir_todo = 1;
1375 swirl->dir_done = 0;
1377 /* all done, decide when to restart */
1378 if (swirl->start_again == -1) {
1379 /* start the counter */
1380 swirl->start_again = RESTART;
1381 } else if (swirl->start_again == 0) {
1382 /* reset the counter */
1383 swirl->start_again = -1;
1386 /* Pick a new colormap! */
1387 XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi));
1388 free_colors (mi->xgwa.screen, MI_COLORMAP(mi),
1389 mi->colors, mi->npixels);
1390 make_smooth_colormap (mi->xgwa.screen, MI_VISUAL(mi),
1392 mi->colors, &mi->npixels, True,
1393 &mi->writable_p, True);
1394 swirl->colours = mi->npixels;
1395 #endif /* STANDALONE */
1400 /* decrement the counter */
1401 swirl->start_again--;
1407 /****************************************************************/
1410 free_swirl (ModeInfo * mi)
1412 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1415 if (swirl->cmap != (Colormap) NULL)
1416 XFreeColormap(MI_DISPLAY(mi), swirl->cmap);
1417 #endif /* STANDALONE */
1419 if (swirl->rgb_values != NULL)
1420 XFree((void *) swirl->rgb_values);
1421 #endif /* !STANDALONE */
1422 if (swirl->ximage != NULL)
1423 destroy_xshm_image(MI_DISPLAY(mi), swirl->ximage,
1426 (void) free((void *) swirl->knots);
1429 /****************************************************************/
1433 refresh_swirl (ModeInfo * mi)
1435 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1437 if (swirl->started) {
1439 swirl->resolution = swirl->resolution + 1;
1440 swirl->drawing = False;
1445 XSCREENSAVER_MODULE ("Swirl", swirl)