1 /* xscreensaver, Copyright (c) 1997 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
13 #include <sys/time.h> /* for gettimeofday() */
14 #include "screenhack.h"
20 static XColor *colors;
25 #define SCALE 1000 /* fixed-point math, for sub-pixel motion */
28 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
29 #define RANDSIGN() ((random() & 1) ? 1 : -1)
38 enum starfish_mode mode;
41 long x, y; /* position of midpoint */
42 double th; /* angle of rotation */
43 double rotv; /* rotational velocity */
44 double rota; /* rotational acceleration */
45 long elasticity; /* how fast it deforms: radial velocity */
47 long min_r, max_r; /* radius range */
48 int npoints; /* control points */
55 static struct starfish *
56 make_starfish (int maxx, int maxy, int size)
58 struct starfish *s = (struct starfish *) calloc(1, sizeof(*s));
62 s->blob_p = get_boolean_resource ("blob", "Blob");
63 s->elasticity = SCALE * get_float_resource ("thickness", "Thickness");
65 if (s->elasticity == 0)
66 /* bell curve from 0-15, avg 7.5 */
67 s->elasticity = RAND(5*SCALE) + RAND(5*SCALE) + RAND(5*SCALE);
69 s->rotv = get_float_resource ("rotation", "Rotation");
71 /* bell curve from 0-12 degrees, avg 6 */
72 s->rotv = frand(4) + frand(4) + frand(4);
74 s->rotv /= 360; /* convert degrees to ratio */
82 s->rot_max = s->rotv * 2;
83 s->rota = 0.0004 + frand(0.0002);
86 if (! (random() % 20))
87 size *= frand(0.35) + frand(0.35) + 0.3;
90 static char skips[] = { 2, 2, 2, 2,
94 s->skip = skips[random() % sizeof(skips)];
97 if (! (random() % (s->skip == 2 ? 3 : 12)))
109 if (s->min_r < (5*SCALE)) s->min_r = (5*SCALE);
110 mid = ((s->min_r + s->max_r) / 2);
115 s->th = frand(M_PI+M_PI) * RANDSIGN();
118 static char sizes[] = { 3, 3, 3, 3, 3,
124 int nsizes = sizeof(sizes);
127 s->npoints = s->skip * sizes[random() % nsizes];
130 s->spline = make_spline (s->npoints);
131 s->r = (long *) malloc (sizeof(*s->r) * s->npoints);
133 for (i = 0; i < s->npoints; i++)
134 s->r[i] = ((i % s->skip) == 0) ? 0 : size;
141 free_starfish (struct starfish *s)
143 if (s->r) free (s->r);
144 if (s->prev) free (s->prev);
147 if (s->spline->control_x) free (s->spline->control_x);
148 if (s->spline->control_y) free (s->spline->control_y);
149 if (s->spline->points) free (s->spline->points);
157 throb_starfish (struct starfish *s)
160 double frac = ((M_PI+M_PI) / s->npoints);
162 for (i = 0; i < s->npoints; i++)
165 long ra = (r > 0 ? r : -r);
166 double th = (s->th > 0 ? s->th : -s->th);
168 long elasticity = s->elasticity;
170 /* place control points evenly around perimiter, shifted by theta */
171 x = s->x + ra * cos (i * frac + th);
172 y = s->y + ra * sin (i * frac + th);
174 s->spline->control_x[i] = x / SCALE;
175 s->spline->control_y[i] = y / SCALE;
177 if (s->mode == zoom && ((i % s->skip) == 0))
180 /* Slow down near the end points: move fastest in the middle. */
182 double ratio = (double)ra / (double)(s->max_r - s->min_r);
183 if (ratio > 0.5) ratio = 1-ratio; /* flip */
184 ratio *= 2; /* normalize */
185 ratio = (ratio * 0.9) + 0.1; /* fudge */
190 /* Increase/decrease radius by elasticity */
191 ra += (r >= 0 ? elasticity : -elasticity);
192 if ((i % s->skip) == 0)
193 ra += (elasticity / 2);
195 r = ra * (r >= 0 ? 1 : -1);
197 /* If we've reached the end (too long or too short) reverse direction. */
198 if ((ra > s->max_r && r >= 0) ||
199 (ra < s->min_r && r < 0))
208 spin_starfish (struct starfish *s)
212 th = -(th + s->rotv);
216 if (th > (M_PI+M_PI))
221 s->th = (s->th > 0 ? th : -th);
225 if (s->rotv > s->rot_max ||
226 s->rotv < -s->rot_max)
230 /* If it stops, start it going in the other direction. */
231 else if (s->rotv < 0)
235 /* keep going in the same direction */
250 /* Alter direction of rotational acceleration randomly. */
251 if (! (random() % 120))
254 /* Change acceleration very occasionally. */
255 if (! (random() % 200))
266 draw_starfish (Display *dpy, Drawable drawable, GC gc, struct starfish *s,
269 compute_closed_spline (s->spline);
272 XPoint *points = (XPoint *)
273 malloc (sizeof(XPoint) * (s->n_prev + s->spline->n_points + 2));
274 int i = s->spline->n_points;
276 memcpy (points, s->spline->points, (i * sizeof(*points)));
277 memcpy (points+i, s->prev, (j * sizeof(*points)));
280 XClearWindow (dpy, drawable);
281 XFillPolygon (dpy, drawable, gc, points, i+j, Complex, CoordModeOrigin);
288 s->prev = (XPoint *) malloc (s->spline->n_points * sizeof(XPoint));
289 memcpy (s->prev, s->spline->points, s->spline->n_points * sizeof(XPoint));
290 s->n_prev = s->spline->n_points;
296 for (i = 0; i < s->npoints; i++)
297 XDrawLine (dpy, drawable, gc, s->x/SCALE, s->y/SCALE,
298 s->spline->control_x[i], s->spline->control_y[i]);
304 static struct starfish *
305 make_window_starfish (Display *dpy, Window window)
307 XWindowAttributes xgwa;
309 Bool blob_p = get_boolean_resource ("blob", "Blob");
310 XGetWindowAttributes (dpy, window, &xgwa);
311 size = (xgwa.width < xgwa.height ? xgwa.width : xgwa.height);
312 if (blob_p) size /= 2;
314 return make_starfish (xgwa.width, xgwa.height, size);
318 static struct starfish *
319 init_starfish (Display *dpy, Window window)
321 static Bool first_time = True;
323 XWindowAttributes xgwa;
324 Bool blob_p = get_boolean_resource ("blob", "Blob");
325 XGetWindowAttributes (dpy, window, &xgwa);
327 cmap = xgwa.colormap;
328 cycle_p = get_boolean_resource ("cycle", "Cycle");
332 if (colors && ncolors)
333 free_colors (dpy, cmap, colors, ncolors);
339 ncolors = get_integer_resource ("colors", "Colors");
340 if (ncolors < 2) ncolors = 2;
341 if (ncolors <= 2) mono_p = True;
346 colors = (XColor *) malloc(sizeof(*colors) * (ncolors+1));
348 if (mono_p || blob_p)
353 else if (random() % 3)
354 make_smooth_colormap (dpy, xgwa.visual, cmap, colors, &ncolors,
355 True, &cycle_p, True);
357 make_uniform_colormap (dpy, xgwa.visual, cmap, colors, &ncolors,
358 True, &cycle_p, True);
360 if (ncolors < 2) ncolors = 2;
361 if (ncolors <= 2) mono_p = True;
363 if (mono_p) cycle_p = False;
367 if (!mono_p && !blob_p)
369 gcv.foreground = colors[fg_index].pixel;
370 XSetWindowBackground (dpy, window, gcv.foreground);
375 XClearWindow (dpy, window);
379 gcv.fill_rule = EvenOddRule;
380 gc = XCreateGC (dpy, window, GCForeground | GCFillRule, &gcv);
382 return make_window_starfish (dpy, window);
388 run_starfish (Display *dpy, Window window, struct starfish *s)
392 draw_starfish (dpy, window, gc, s, False);
396 static Bool init = False;
397 static unsigned long black, white;
400 black = get_pixel_resource ("background", "Background", dpy, cmap);
401 white = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
404 XSetForeground (dpy, gc, fg_index);
408 fg_index = (fg_index == black ? white : black);
409 XSetForeground (dpy, gc, fg_index);
414 fg_index = (fg_index + 1) % ncolors;
415 XSetForeground (dpy, gc, colors [fg_index].pixel);
422 char *progclass = "Starfish";
424 char *defaults [] = {
425 ".background: black",
426 ".foreground: white",
428 "*cycleDelay: 100000",
429 "*thickness: 0", /* pixels, 0 = random */
430 "*rotation: -1", /* degrees, -1 = "random" */
439 XrmOptionDescRec options [] = {
440 { "-delay", ".delay", XrmoptionSepArg, 0 },
441 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
442 { "-cycle-delay", ".cycleDelay", XrmoptionSepArg, 0 },
443 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
444 { "-rotation", ".rotation", XrmoptionSepArg, 0 },
445 { "-colors", ".colors", XrmoptionSepArg, 0 },
446 { "-cycle", ".cycle", XrmoptionNoArg, "True" },
447 { "-no-cycle", ".cycle", XrmoptionNoArg, "False" },
448 { "-duration", ".duration", XrmoptionSepArg, 0 },
449 { "-blob", ".blob", XrmoptionNoArg, "True" },
450 { "-no-blob", ".blob", XrmoptionNoArg, "False" },
455 screenhack (Display *dpy, Window window)
457 struct starfish *s = init_starfish (dpy, window);
458 int delay = get_integer_resource ("delay", "Delay");
459 int delay2 = get_integer_resource ("delay2", "Delay") * 1000000;
460 int cycle_delay = get_integer_resource ("cycleDelay", "Delay");
461 int duration = get_seconds_resource ("duration", "Seconds");
462 Bool blob_p = get_boolean_resource ("blob", "Blob");
463 time_t start = time ((time_t) 0);
465 int direction = (random() % 1) ? 1 : -1;
472 run_starfish (dpy, window, s);
475 if (cycle_p && cycle_delay)
477 if (cycle_delay <= delay)
482 rotate_colors (dpy, cmap, colors, ncolors, direction);
489 static long tick = 0;
490 if (tick >= cycle_delay)
492 rotate_colors (dpy, cmap, colors, ncolors, direction);
500 if (! (random() % 1000))
501 direction = -direction;
508 now = time ((time_t) 0);
509 if (start + duration < now)
515 if (delay2 && !blob_p && cycle_p)
520 rotate_colors (dpy, cmap, colors, ncolors, direction);
526 /* Every now and then, pick new colors; otherwise, just build
527 a new starfish with the current colors. */
528 if (! (random () % 10))
529 s = init_starfish (dpy, window);
531 s = make_window_starfish(dpy, window);