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;
325 return make_starfish (st, xgwa.width, xgwa.height, size);
329 static struct starfish *
330 reset_starfish (struct state *st)
333 unsigned int flags = 0;
334 XWindowAttributes xgwa;
335 XGetWindowAttributes (st->dpy, st->window, &xgwa);
337 st->cmap = xgwa.colormap;
341 if (st->colors && st->ncolors)
342 free_colors (xgwa.screen, st->cmap, st->colors, st->ncolors);
346 XFreeGC (st->dpy, st->gc);
350 st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
351 if (st->ncolors < 2) st->ncolors = 2;
352 if (st->ncolors <= 2) mono_p = True;
357 st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1));
361 else if (random() % 3)
362 make_smooth_colormap (xgwa.screen, xgwa.visual, st->cmap,
363 st->colors, &st->ncolors,
366 make_uniform_colormap (xgwa.screen, xgwa.visual, st->cmap,
367 st->colors, &st->ncolors,
370 if (st->ncolors < 2) st->ncolors = 2;
371 if (st->ncolors <= 2) mono_p = True;
375 if (!mono_p && !st->blob_p)
377 flags |= GCForeground;
378 gcv.foreground = st->colors[st->fg_index].pixel;
379 XSetWindowBackground (st->dpy, st->window, gcv.foreground);
384 XClearWindow (st->dpy, st->window);
389 gcv.fill_rule = EvenOddRule;
390 st->gc = XCreateGC (st->dpy, st->window, flags, &gcv);
393 jwxyz_XSetAntiAliasing (st->dpy, st->gc, False);
396 return make_window_starfish (st);
402 run_starfish (struct state *st, struct starfish *s)
406 draw_starfish (st, st->window, st->gc, s, False);
412 st->black = get_pixel_resource (st->dpy, st->cmap, "background", "Background");
413 st->white = get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
415 st->fg_index = st->white;
416 XSetForeground (st->dpy, st->gc, st->fg_index);
420 st->fg_index = (st->fg_index == st->black ? st->white : st->black);
421 XSetForeground (st->dpy, st->gc, st->fg_index);
426 st->fg_index = (st->fg_index + 1) % st->ncolors;
427 XSetForeground (st->dpy, st->gc, st->colors [st->fg_index].pixel);
433 starfish_init (Display *dpy, Window window)
435 struct state *st = (struct state *) calloc (1, sizeof(*st));
439 st->delay = get_integer_resource (st->dpy, "delay", "Delay");
440 st->delay2 = get_integer_resource (st->dpy, "delay2", "Delay") * 1000000;
441 /* st->duration = get_seconds_resource (st->dpy, "duration", "Seconds");*/
442 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
443 st->direction = (random() & 1) ? 1 : -1;
445 s = get_string_resource (st->dpy, "mode", "Mode");
446 if (s && !strcasecmp (s, "blob"))
448 else if (s && !strcasecmp (s, "zoom"))
450 else if (!s || !*s || !strcasecmp (s, "random"))
451 st->blob_p = !(random() % 3);
453 fprintf (stderr, "%s: mode must be blob, zoom, or random", progname);
458 st->starfish = reset_starfish (st);
463 starfish_draw (Display *dpy, Window window, void *closure)
465 struct state *st = (struct state *) closure;
466 struct starfish *s = st->starfish;
469 run_starfish (st, s);
471 if (st->duration > 0)
473 if (st->start_time == 0)
474 st->start_time = time ((time_t *) 0);
475 now = time ((time_t *) 0);
476 if (st->start_time + st->duration < now)
478 st->start_time = now;
482 /* Every now and then, pick new colors; otherwise, just build
483 a new starfish with the current colors. */
484 if (! (random () % 10))
485 s = reset_starfish (st);
487 s = make_window_starfish (st);
497 starfish_reshape (Display *dpy, Window window, void *closure,
498 unsigned int w, unsigned int h)
500 struct state *st = (struct state *) closure;
501 free_starfish (st->starfish);
503 st->starfish = reset_starfish (st);
507 starfish_event (Display *dpy, Window window, void *closure, XEvent *event)
513 starfish_free (Display *dpy, Window window, void *closure)
515 struct state *st = (struct state *) closure;
522 static const char *starfish_defaults [] = {
523 ".background: black",
524 ".foreground: white",
527 "*thickness: 0", /* pixels, 0 = random */
528 "*rotation: -1", /* degrees, -1 = "random" */
534 "*ignoreRotation: True",
539 static XrmOptionDescRec starfish_options [] = {
540 { "-delay", ".delay", XrmoptionSepArg, 0 },
541 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
542 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
543 { "-rotation", ".rotation", XrmoptionSepArg, 0 },
544 { "-colors", ".colors", XrmoptionSepArg, 0 },
545 { "-duration", ".duration", XrmoptionSepArg, 0 },
546 { "-mode", ".mode", XrmoptionSepArg, 0 },
547 { "-blob", ".mode", XrmoptionNoArg, "blob" },
548 { "-zoom", ".mode", XrmoptionNoArg, "zoom" },
552 XSCREENSAVER_MODULE ("Starfish", starfish)