1 /* xscreensaver, Copyright (c) 1997-2015 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 "screenhack.h"
16 #define SCALE 1000 /* fixed-point math, for sub-pixel motion */
19 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
20 #define RANDSIGN() ((random() & 1) ? 1 : -1)
28 enum starfish_mode mode;
31 long x, y; /* position of midpoint */
32 double th; /* angle of rotation */
33 double rotv; /* rotational velocity */
34 double rota; /* rotational acceleration */
35 long elasticity; /* how fast it deforms: radial velocity */
37 long min_r, max_r; /* radius range */
38 int npoints; /* control points */
56 int delay, delay2, duration, direction, blob_p;
60 unsigned long black, white;
65 struct starfish *starfish;
69 static struct starfish *
70 make_starfish (struct state *st, int maxx, int maxy, int size)
72 struct starfish *s = (struct starfish *) calloc(1, sizeof(*s));
75 s->blob_p = st->blob_p;
76 s->elasticity = SCALE * get_float_resource (st->dpy, "thickness", "Thickness");
78 if (s->elasticity == 0)
79 /* bell curve from 0-15, avg 7.5 */
80 s->elasticity = RAND(5*SCALE) + RAND(5*SCALE) + RAND(5*SCALE);
82 s->rotv = get_float_resource (st->dpy, "rotation", "Rotation");
84 /* bell curve from 0-12 degrees, avg 6 */
85 s->rotv = frand(4) + frand(4) + frand(4);
87 s->rotv /= 360; /* convert degrees to ratio */
95 s->rot_max = s->rotv * 2;
96 s->rota = 0.0004 + frand(0.0002);
99 if (! (random() % 20))
100 size *= frand(0.35) + frand(0.35) + 0.3;
103 static const char skips[] = { 2, 2, 2, 2,
107 s->skip = skips[random() % sizeof(skips)];
110 if (! (random() % (s->skip == 2 ? 3 : 12)))
122 if (s->min_r < (5*SCALE)) s->min_r = (5*SCALE);
127 s->th = frand(M_PI+M_PI) * RANDSIGN();
130 static const char sizes[] = { 3, 3, 3, 3, 3,
136 int nsizes = sizeof(sizes);
139 s->npoints = s->skip * sizes[random() % nsizes];
142 s->spline = make_spline (s->npoints);
143 s->r = (long *) malloc (sizeof(*s->r) * s->npoints);
145 for (i = 0; i < s->npoints; i++)
146 s->r[i] = ((i % s->skip) == 0) ? 0 : size;
153 free_starfish (struct starfish *s)
155 if (s->r) free (s->r);
156 if (s->prev) free (s->prev);
159 if (s->spline->control_x) free (s->spline->control_x);
160 if (s->spline->control_y) free (s->spline->control_y);
161 if (s->spline->points) free (s->spline->points);
169 throb_starfish (struct starfish *s)
172 double frac = ((M_PI+M_PI) / s->npoints);
174 for (i = 0; i < s->npoints; i++)
177 long ra = (r > 0 ? r : -r);
178 double th = (s->th > 0 ? s->th : -s->th);
180 long elasticity = s->elasticity;
182 /* place control points evenly around perimiter, shifted by theta */
183 x = s->x + ra * cos (i * frac + th);
184 y = s->y + ra * sin (i * frac + th);
186 s->spline->control_x[i] = x / SCALE;
187 s->spline->control_y[i] = y / SCALE;
189 if (s->mode == zoom && ((i % s->skip) == 0))
192 /* Slow down near the end points: move fastest in the middle. */
194 double ratio = (double)ra / (double)(s->max_r - s->min_r);
195 if (ratio > 0.5) ratio = 1-ratio; /* flip */
196 ratio *= 2; /* normalize */
197 ratio = (ratio * 0.9) + 0.1; /* fudge */
202 /* Increase/decrease radius by elasticity */
203 ra += (r >= 0 ? elasticity : -elasticity);
204 if ((i % s->skip) == 0)
205 ra += (elasticity / 2);
207 r = ra * (r >= 0 ? 1 : -1);
209 /* If we've reached the end (too long or too short) reverse direction. */
210 if ((ra > s->max_r && r >= 0) ||
211 (ra < s->min_r && r < 0))
220 spin_starfish (struct starfish *s)
224 th = -(th + s->rotv);
228 if (th > (M_PI+M_PI))
233 s->th = (s->th > 0 ? th : -th);
237 if (s->rotv > s->rot_max ||
238 s->rotv < -s->rot_max)
242 /* If it stops, start it going in the other direction. */
243 else if (s->rotv < 0)
247 /* keep going in the same direction */
262 /* Alter direction of rotational acceleration randomly. */
263 if (! (random() % 120))
266 /* Change acceleration very occasionally. */
267 if (! (random() % 200))
278 draw_starfish (struct state *st, Drawable drawable, GC this_gc, struct starfish *s,
281 compute_closed_spline (s->spline);
284 XPoint *points = (XPoint *)
285 malloc (sizeof(XPoint) * (s->n_prev + s->spline->n_points + 2));
286 int i = s->spline->n_points;
288 memcpy (points, s->spline->points, (i * sizeof(*points)));
289 memcpy (points+i, s->prev, (j * sizeof(*points)));
292 XClearWindow (st->dpy, drawable);
293 XFillPolygon (st->dpy, drawable, this_gc, points, i+j, Complex, CoordModeOrigin);
300 s->prev = (XPoint *) malloc (s->spline->n_points * sizeof(XPoint));
301 memcpy (s->prev, s->spline->points, s->spline->n_points * sizeof(XPoint));
302 s->n_prev = s->spline->n_points;
308 for (i = 0; i < s->npoints; i++)
309 XDrawLine (st->dpy, drawable, this_gc, s->x/SCALE, s->y/SCALE,
310 s->spline->control_x[i], s->spline->control_y[i]);
316 static struct starfish *
317 make_window_starfish (struct state *st)
319 XWindowAttributes xgwa;
321 XGetWindowAttributes (st->dpy, st->window, &xgwa);
322 size = (xgwa.width < xgwa.height ? xgwa.width : xgwa.height);
323 if (st->blob_p) size /= 2;
326 if (xgwa.width < 100 || xgwa.height < 100) /* tiny window */
328 size = (xgwa.width > xgwa.height ? xgwa.width : xgwa.height);
329 if (size < 100) size = 100;
332 return make_starfish (st, xgwa.width, xgwa.height, size);
336 static struct starfish *
337 reset_starfish (struct state *st)
340 unsigned int flags = 0;
341 XWindowAttributes xgwa;
342 XGetWindowAttributes (st->dpy, st->window, &xgwa);
344 st->cmap = xgwa.colormap;
348 if (st->colors && st->ncolors)
349 free_colors (xgwa.screen, st->cmap, st->colors, st->ncolors);
353 XFreeGC (st->dpy, st->gc);
357 st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
358 if (st->ncolors < 2) st->ncolors = 2;
359 if (st->ncolors <= 2) mono_p = True;
364 st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1));
368 else if (random() % 3)
369 make_smooth_colormap (xgwa.screen, xgwa.visual, st->cmap,
370 st->colors, &st->ncolors,
373 make_uniform_colormap (xgwa.screen, xgwa.visual, st->cmap,
374 st->colors, &st->ncolors,
377 if (st->ncolors < 2) st->ncolors = 2;
378 if (st->ncolors <= 2) mono_p = True;
382 if (!mono_p && !st->blob_p)
384 flags |= GCForeground;
385 gcv.foreground = st->colors[st->fg_index].pixel;
386 XSetWindowBackground (st->dpy, st->window, gcv.foreground);
391 XClearWindow (st->dpy, st->window);
396 gcv.fill_rule = EvenOddRule;
397 st->gc = XCreateGC (st->dpy, st->window, flags, &gcv);
400 jwxyz_XSetAntiAliasing (st->dpy, st->gc, False);
403 return make_window_starfish (st);
409 run_starfish (struct state *st, struct starfish *s)
413 draw_starfish (st, st->window, st->gc, s, False);
419 st->black = get_pixel_resource (st->dpy, st->cmap, "background", "Background");
420 st->white = get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
422 st->fg_index = st->white;
423 XSetForeground (st->dpy, st->gc, st->fg_index);
427 st->fg_index = (st->fg_index == st->black ? st->white : st->black);
428 XSetForeground (st->dpy, st->gc, st->fg_index);
433 st->fg_index = (st->fg_index + 1) % st->ncolors;
434 XSetForeground (st->dpy, st->gc, st->colors [st->fg_index].pixel);
440 starfish_init (Display *dpy, Window window)
442 struct state *st = (struct state *) calloc (1, sizeof(*st));
446 st->delay = get_integer_resource (st->dpy, "delay", "Delay");
447 st->delay2 = get_integer_resource (st->dpy, "delay2", "Delay") * 1000000;
448 /* st->duration = get_seconds_resource (st->dpy, "duration", "Seconds");*/
449 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
450 st->direction = (random() & 1) ? 1 : -1;
452 s = get_string_resource (st->dpy, "mode", "Mode");
453 if (s && !strcasecmp (s, "blob"))
455 else if (s && !strcasecmp (s, "zoom"))
457 else if (!s || !*s || !strcasecmp (s, "random"))
458 st->blob_p = !(random() % 3);
460 fprintf (stderr, "%s: mode must be blob, zoom, or random", progname);
465 st->starfish = reset_starfish (st);
470 starfish_draw (Display *dpy, Window window, void *closure)
472 struct state *st = (struct state *) closure;
473 struct starfish *s = st->starfish;
476 run_starfish (st, s);
478 if (st->duration > 0)
480 if (st->start_time == 0)
481 st->start_time = time ((time_t *) 0);
482 now = time ((time_t *) 0);
483 if (st->start_time + st->duration < now)
485 st->start_time = now;
489 /* Every now and then, pick new colors; otherwise, just build
490 a new starfish with the current colors. */
491 if (! (random () % 10))
492 s = reset_starfish (st);
494 s = make_window_starfish (st);
504 starfish_reshape (Display *dpy, Window window, void *closure,
505 unsigned int w, unsigned int h)
507 struct state *st = (struct state *) closure;
508 free_starfish (st->starfish);
510 st->starfish = reset_starfish (st);
514 starfish_event (Display *dpy, Window window, void *closure, XEvent *event)
520 starfish_free (Display *dpy, Window window, void *closure)
522 struct state *st = (struct state *) closure;
529 static const char *starfish_defaults [] = {
530 ".background: black",
531 ".foreground: white",
534 "*thickness: 0", /* pixels, 0 = random */
535 "*rotation: -1", /* degrees, -1 = "random" */
541 "*ignoreRotation: True",
546 static XrmOptionDescRec starfish_options [] = {
547 { "-delay", ".delay", XrmoptionSepArg, 0 },
548 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
549 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
550 { "-rotation", ".rotation", XrmoptionSepArg, 0 },
551 { "-colors", ".colors", XrmoptionSepArg, 0 },
552 { "-duration", ".duration", XrmoptionSepArg, 0 },
553 { "-mode", ".mode", XrmoptionSepArg, 0 },
554 { "-blob", ".mode", XrmoptionNoArg, "blob" },
555 { "-zoom", ".mode", XrmoptionNoArg, "zoom" },
559 XSCREENSAVER_MODULE ("Starfish", starfish)