1 /* twang, twist around screen bits, v1.3
2 * by Dan Bornstein, danfuzz@milk.com
3 * Copyright (c) 2003 Dan Bornstein. All rights reserved.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation. No representations are made about the suitability of this
10 * software for any purpose. It is provided "as is" without express or
13 * See the included man page for more details.
17 #include "screenhack.h"
19 #ifdef HAVE_XSHM_EXTENSION
25 /* random float in the range (-1..1) */
26 #define RAND_FLOAT_PM1 \
27 (((FLOAT) ((random() >> 8) & 0xffff)) / ((FLOAT) 0x10000) * 2 - 1)
29 /* random float in the range (0..1) */
30 #define RAND_FLOAT_01 \
31 (((FLOAT) ((random() >> 8) & 0xffff)) / ((FLOAT) 0x10000))
36 int x; /* x coordinate of the center of the tile */
37 int y; /* y coordinate of the center of the tile */
38 FLOAT angle; /* angle of the tile (-pi..pi) */
39 FLOAT zoom; /* log of the zoom of the tile (-1..1) */
40 FLOAT vAngle; /* angular velocity (-pi/4..pi/4) */
41 FLOAT vZoom; /* zoomular velocity (-0.25..0.25) */
50 int delay; /* delay (usec) between iterations */
51 int duration; /* time (sec) before loading new image */
52 int maxColumns; /* the maximum number of columns of tiles */
53 int maxRows; /* the maximum number of rows of tiles */
54 int tileSize; /* the size (width and height) of a tile */
55 int borderWidth; /* the width of the border around each tile */
56 FLOAT eventChance; /* the chance, per iteration, of an interesting event happening */
57 FLOAT friction; /* friction: the fraction (0..1) by which velocity decreased per iteration */
58 FLOAT springiness; /* springiness: the fraction (0..1) of the orientation that turns into velocity towards the center */
59 FLOAT transference; /* transference: the fraction (0..1) of the orientations of orthogonal neighbors that turns into velocity (in the same direction as the orientation) */
60 int windowWidth; /* width and height of the window */
62 Screen *screen; /* the screen to draw on */
63 XImage *sourceImage; /* image source of stuff to draw */
64 XImage *workImage; /* work area image, used when rendering */
65 XImage *backgroundImage; /* image filled with background pixels */
67 GC backgroundGC; /* GC for the background color */
68 GC foregroundGC; /* GC for the foreground color */
69 GC borderGC; /* GC for the border color */
70 unsigned long backgroundPixel; /* background color as a pixel value */
71 unsigned long borderPixel; /* border color as a pixel value */
73 Tile *tiles; /* array of tiles (left->right, top->bottom, row major) */
74 int rows; /* number of rows of tiles */
75 int columns; /* number of columns of tiles */
77 Tile **sortedTiles; /* array of tile pointers, sorted by zoom */
78 int tileCount; /* total number of tiles */
81 async_load_state *img_loader;
83 Bool useShm; /* whether or not to use xshm */
84 #ifdef HAVE_XSHM_EXTENSION
85 XShmSegmentInfo shmInfo;
90 #define TILE_AT(col,row) (&st->tiles[(row) * st->columns + (col)])
92 #define MAX_VANGLE (M_PI / 4.0)
93 #define MAX_VZOOM 0.25
95 #define RAND_ANGLE (RAND_FLOAT_PM1 * M_PI)
96 #define RAND_ZOOM (RAND_FLOAT_PM1)
97 #define RAND_VANGLE (RAND_FLOAT_PM1 * MAX_VANGLE)
98 #define RAND_VZOOM (RAND_FLOAT_PM1 * MAX_VZOOM)
103 * overall setup stuff
106 /* grab the source image */
108 grabImage_start (struct state *st, XWindowAttributes *xwa)
110 XFillRectangle (st->dpy, st->window, st->backgroundGC, 0, 0,
111 st->windowWidth, st->windowHeight);
112 st->backgroundImage =
113 XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
116 st->start_time = time ((time_t) 0);
117 st->img_loader = load_image_async_simple (0, xwa->screen, st->window,
122 grabImage_done (struct state *st)
124 XWindowAttributes xwa;
125 XGetWindowAttributes (st->dpy, st->window, &xwa);
127 st->start_time = time ((time_t) 0);
128 if (st->sourceImage) XDestroyImage (st->sourceImage);
129 st->sourceImage = XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
132 if (st->workImage) XDestroyImage (st->workImage);
133 st->workImage = NULL;
135 #ifdef HAVE_XSHM_EXTENSION
138 st->workImage = create_xshm_image (st->dpy, xwa.visual, xwa.depth,
139 ZPixmap, 0, &st->shmInfo,
140 st->windowWidth, st->windowHeight);
144 fprintf (stderr, "create_xshm_image failed\n");
148 if (st->workImage == NULL)
149 #endif /* HAVE_XSHM_EXTENSION */
151 /* just use XSubImage to acquire the right visual, depth, etc;
152 * easier than the other alternatives */
153 st->workImage = XSubImage (st->sourceImage, 0, 0, st->windowWidth, st->windowHeight);
156 /* set up the system */
157 static void setup (struct state *st)
159 XWindowAttributes xgwa;
162 XGetWindowAttributes (st->dpy, st->window, &xgwa);
164 st->screen = xgwa.screen;
165 st->windowWidth = xgwa.width;
166 st->windowHeight = xgwa.height;
168 gcv.line_width = st->borderWidth;
169 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
170 "borderColor", "BorderColor");
171 st->borderPixel = gcv.foreground;
172 st->borderGC = XCreateGC (st->dpy, st->window, GCForeground | GCLineWidth,
175 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
176 "background", "Background");
177 st->backgroundPixel = gcv.foreground;
178 st->backgroundGC = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
180 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
181 "foreground", "Foreground");
182 st->foregroundGC = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
184 grabImage_start (st, &xgwa);
193 /* event: randomize all the angular velocities */
194 static void randomizeAllAngularVelocities (struct state *st)
199 for (r = 0; r < st->rows; r++)
201 for (c = 0; c < st->columns; c++)
203 TILE_AT (c, r)->vAngle = RAND_VANGLE;
208 /* event: randomize all the zoomular velocities */
209 static void randomizeAllZoomularVelocities (struct state *st)
214 for (r = 0; r < st->rows; r++)
216 for (c = 0; c < st->columns; c++)
218 TILE_AT (c, r)->vZoom = RAND_VZOOM;
223 /* event: randomize all the velocities */
224 static void randomizeAllVelocities (struct state *st)
226 randomizeAllAngularVelocities (st);
227 randomizeAllZoomularVelocities (st);
230 /* event: randomize all the angular orientations */
231 static void randomizeAllAngularOrientations (struct state *st)
236 for (r = 0; r < st->rows; r++)
238 for (c = 0; c < st->columns; c++)
240 TILE_AT (c, r)->angle = RAND_ANGLE;
245 /* event: randomize all the zoomular orientations */
246 static void randomizeAllZoomularOrientations (struct state *st)
251 for (r = 0; r < st->rows; r++)
253 for (c = 0; c < st->columns; c++)
255 TILE_AT (c, r)->zoom = RAND_ZOOM;
260 /* event: randomize all the orientations */
261 static void randomizeAllOrientations (struct state *st)
263 randomizeAllAngularOrientations (st);
264 randomizeAllZoomularOrientations (st);
267 /* event: randomize everything */
268 static void randomizeEverything (struct state *st)
270 randomizeAllVelocities (st);
271 randomizeAllOrientations (st);
274 /* event: pick one tile and randomize all its stats */
275 static void randomizeOneTile (struct state *st)
277 int c = RAND_FLOAT_01 * st->columns;
278 int r = RAND_FLOAT_01 * st->rows;
280 Tile *t = TILE_AT (c, r);
281 t->angle = RAND_ANGLE;
283 t->vAngle = RAND_VANGLE;
284 t->vZoom = RAND_VZOOM;
287 /* event: pick one row and randomize everything about each of its tiles */
288 static void randomizeOneRow (struct state *st)
291 int r = RAND_FLOAT_01 * st->rows;
293 for (c = 0; c < st->columns; c++)
295 Tile *t = TILE_AT (c, r);
296 t->angle = RAND_ANGLE;
298 t->vAngle = RAND_VANGLE;
299 t->vZoom = RAND_VZOOM;
303 /* event: pick one column and randomize everything about each of its tiles */
304 static void randomizeOneColumn (struct state *st)
306 int c = RAND_FLOAT_01 * st->columns;
309 for (r = 0; r < st->rows; r++)
311 Tile *t = TILE_AT (c, r);
312 t->angle = RAND_ANGLE;
314 t->vAngle = RAND_VANGLE;
315 t->vZoom = RAND_VZOOM;
319 /* do model event processing */
320 static void modelEvents (struct state *st)
324 if (RAND_FLOAT_01 > st->eventChance)
329 which = RAND_FLOAT_01 * 10;
333 case 0: randomizeAllAngularVelocities (st); break;
334 case 1: randomizeAllZoomularVelocities (st); break;
335 case 2: randomizeAllVelocities (st); break;
336 case 3: randomizeAllAngularOrientations (st); break;
337 case 4: randomizeAllZoomularOrientations (st); break;
338 case 5: randomizeAllOrientations (st); break;
339 case 6: randomizeEverything (st); break;
340 case 7: randomizeOneTile (st); break;
341 case 8: randomizeOneColumn (st); break;
342 case 9: randomizeOneRow (st); break;
346 /* update the model for one iteration */
347 static void updateModel (struct state *st)
352 /* for each tile, decrease its velocities according to the friction,
353 * and increase them based on its current orientation and the orientations
354 * of its orthogonal neighbors */
355 for (r = 0; r < st->rows; r++)
357 for (c = 0; c < st->columns; c++)
359 Tile *t = TILE_AT (c, r);
362 FLOAT va = t->vAngle;
365 va -= t->angle * st->springiness;
366 vz -= t->zoom * st->springiness;
370 Tile *t2 = TILE_AT (c - 1, r);
371 va += (t2->angle - a) * st->transference;
372 vz += (t2->zoom - z) * st->transference;
375 if (c < (st->columns - 1))
377 Tile *t2 = TILE_AT (c + 1, r);
378 va += (t2->angle - a) * st->transference;
379 vz += (t2->zoom - z) * st->transference;
384 Tile *t2 = TILE_AT (c, r - 1);
385 va += (t2->angle - a) * st->transference;
386 vz += (t2->zoom - z) * st->transference;
389 if (r < (st->rows - 1))
391 Tile *t2 = TILE_AT (c, r + 1);
392 va += (t2->angle - a) * st->transference;
393 vz += (t2->zoom - z) * st->transference;
396 va *= (1.0 - st->friction);
397 vz *= (1.0 - st->friction);
399 if (va > MAX_VANGLE) va = MAX_VANGLE;
400 else if (va < -MAX_VANGLE) va = -MAX_VANGLE;
403 if (vz > MAX_VZOOM) vz = MAX_VZOOM;
404 else if (vz < -MAX_VZOOM) vz = -MAX_VZOOM;
409 /* for each tile, update its orientation based on its velocities */
410 for (r = 0; r < st->rows; r++)
412 for (c = 0; c < st->columns; c++)
414 Tile *t = TILE_AT (c, r);
415 FLOAT a = t->angle + t->vAngle;
416 FLOAT z = t->zoom + t->vZoom;
418 if (a > M_PI) a = M_PI;
419 else if (a < -M_PI) a = -M_PI;
422 if (z > 1.0) z = 1.0;
423 else if (z < -1.0) z = -1.0;
429 /* the comparator to us to sort the tiles (used immediately below); it'd
430 * sure be nice if C allowed inner functions (or jeebus-forbid *real
432 static int sortTilesComparator (const void *v1, const void *v2)
434 Tile *t1 = *(Tile **) v1;
435 Tile *t2 = *(Tile **) v2;
437 if (t1->zoom < t2->zoom)
442 if (t1->zoom > t2->zoom)
450 /* sort the tiles in sortedTiles by zoom */
451 static void sortTiles (struct state *st)
453 qsort (st->sortedTiles, st->tileCount, sizeof (Tile *), sortTilesComparator);
456 /* render the given tile */
457 static void renderTile (struct state *st, Tile *t)
459 /* note: the zoom as stored per tile is log-based (centered on 0, with
460 * 0 being no zoom, but the range for zoom-as-drawn is 0.4..2.5,
461 * hence the alteration of t->zoom, below */
468 FLOAT zoom = pow (2.5, t->zoom);
469 FLOAT ang = -t->angle;
470 FLOAT sinAng = sin (ang);
471 FLOAT cosAng = cos (ang);
473 FLOAT innerBorder = (st->tileSize - st->borderWidth) / 2.0;
474 FLOAT outerBorder = innerBorder + st->borderWidth;
476 int maxCoord = outerBorder * zoom * (fabs (sinAng) + fabs (cosAng));
477 int minX = tx - maxCoord;
478 int maxX = tx + maxCoord;
479 int minY = ty - maxCoord;
480 int maxY = ty + maxCoord;
484 if (minX < 0) minX = 0;
485 if (maxX > st->windowWidth) maxX = st->windowWidth;
486 if (minY < 0) minY = 0;
487 if (maxY > st->windowHeight) maxY = st->windowHeight;
492 for (y = minY, prey = y - ty; y < maxY; y++, prey++)
494 FLOAT prex = minX - tx;
495 FLOAT srcx = prex * cosAng - prey * sinAng;
496 FLOAT srcy = prex * sinAng + prey * cosAng;
500 x++, srcx += cosAng, srcy += sinAng)
502 if ((srcx < -innerBorder) || (srcx >= innerBorder) ||
503 (srcy < -innerBorder) || (srcy >= innerBorder))
505 if ((srcx < -outerBorder) || (srcx >= outerBorder) ||
506 (srcy < -outerBorder) || (srcy >= outerBorder))
510 XPutPixel (st->workImage, x, y, st->borderPixel);
515 XGetPixel (st->sourceImage, srcx + tx, srcy + ty);
516 XPutPixel (st->workImage, x, y, p);
522 /* render and display the current model */
523 static void renderFrame (struct state *st)
527 memcpy (st->workImage->data, st->backgroundImage->data,
528 st->workImage->bytes_per_line * st->workImage->height);
532 for (n = 0; n < st->tileCount; n++)
534 renderTile (st, st->sortedTiles[n]);
537 #ifdef HAVE_XSHM_EXTENSION
539 XShmPutImage (st->dpy, st->window, st->backgroundGC, st->workImage, 0, 0, 0, 0,
540 st->windowWidth, st->windowHeight, False);
542 #endif /* HAVE_XSHM_EXTENSION */
543 XPutImage (st->dpy, st->window, st->backgroundGC, st->workImage,
544 0, 0, 0, 0, st->windowWidth, st->windowHeight);
547 /* set up the model */
548 static void setupModel (struct state *st)
553 int leftX; /* x of the center of the top-left tile */
554 int topY; /* y of the center of the top-left tile */
556 if (st->tileSize > (st->windowWidth / 2))
558 st->tileSize = st->windowWidth / 2;
561 if (st->tileSize > (st->windowHeight / 2))
563 st->tileSize = st->windowHeight / 2;
566 st->columns = st->tileSize ? st->windowWidth / st->tileSize : 0;
567 st->rows = st->tileSize ? st->windowHeight / st->tileSize : 0;
569 if ((st->maxColumns != 0) && (st->columns > st->maxColumns))
571 st->columns = st->maxColumns;
574 if ((st->maxRows != 0) && (st->rows > st->maxRows))
576 st->rows = st->maxRows;
579 st->tileCount = st->rows * st->columns;
581 leftX = (st->windowWidth - (st->columns * st->tileSize) + st->tileSize) / 2;
582 topY = (st->windowHeight - (st->rows * st->tileSize) + st->tileSize) / 2;
584 st->tiles = calloc (st->tileCount, sizeof (Tile));
585 st->sortedTiles = calloc (st->tileCount, sizeof (Tile *));
587 for (r = 0; r < st->rows; r++)
589 for (c = 0; c < st->columns; c++)
591 Tile *t = TILE_AT (c, r);
592 t->x = leftX + c * st->tileSize;
593 t->y = topY + r * st->tileSize;
594 st->sortedTiles[c + r * st->columns] = t;
598 randomizeEverything (st);
601 /* do one iteration */
604 twang_draw (Display *dpy, Window window, void *closure)
606 struct state *st = (struct state *) closure;
608 if (st->img_loader) /* still loading */
610 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
611 if (! st->img_loader) { /* just finished */
617 if (!st->img_loader &&
618 st->start_time + st->duration < time ((time_t) 0)) {
619 XWindowAttributes xgwa;
620 XGetWindowAttributes (st->dpy, st->window, &xgwa);
621 grabImage_start (st, &xgwa);
633 twang_reshape (Display *dpy, Window window, void *closure,
634 unsigned int w, unsigned int h)
639 twang_event (Display *dpy, Window window, void *closure, XEvent *event)
645 twang_free (Display *dpy, Window window, void *closure)
647 struct state *st = (struct state *) closure;
653 /* main and options and stuff */
655 /* initialize the user-specifiable params */
656 static void initParams (struct state *st)
660 st->borderWidth = get_integer_resource (st->dpy, "borderWidth", "Integer");
661 if (st->borderWidth < 0)
663 fprintf (stderr, "error: border width must be at least 0\n");
667 st->delay = get_integer_resource (st->dpy, "delay", "Delay");
670 fprintf (stderr, "error: delay must be at least 0\n");
674 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
675 if (st->duration < 1) st->duration = 1;
677 st->eventChance = get_float_resource (st->dpy, "eventChance", "Double");
678 if ((st->eventChance < 0.0) || (st->eventChance > 1.0))
680 fprintf (stderr, "error: eventChance must be in the range 0..1\n");
684 st->friction = get_float_resource (st->dpy, "friction", "Double");
685 if ((st->friction < 0.0) || (st->friction > 1.0))
687 fprintf (stderr, "error: friction must be in the range 0..1\n");
691 st->maxColumns = get_integer_resource (st->dpy, "maxColumns", "Integer");
692 if (st->maxColumns < 0)
694 fprintf (stderr, "error: max columns must be at least 0\n");
698 st->maxRows = get_integer_resource (st->dpy, "maxRows", "Integer");
701 fprintf (stderr, "error: max rows must be at least 0\n");
705 st->springiness = get_float_resource (st->dpy, "springiness", "Double");
706 if ((st->springiness < 0.0) || (st->springiness > 1.0))
708 fprintf (stderr, "error: springiness must be in the range 0..1\n");
712 st->tileSize = get_integer_resource (st->dpy, "tileSize", "Integer");
713 if (st->tileSize < 1)
715 fprintf (stderr, "error: tile size must be at least 1\n");
719 st->transference = get_float_resource (st->dpy, "transference", "Double");
720 if ((st->transference < 0.0) || (st->transference > 1.0))
722 fprintf (stderr, "error: transference must be in the range 0..1\n");
726 #ifdef HAVE_XSHM_EXTENSION
727 st->useShm = get_boolean_resource (st->dpy, "useSHM", "Boolean");
737 twang_init (Display *dpy, Window win)
739 struct state *st = (struct state *) calloc (1, sizeof(*st));
751 static const char *twang_defaults [] = {
752 ".background: black",
753 ".foreground: white",
754 "*borderColor: blue",
758 "*eventChance: 0.01",
764 "*transference: 0.025",
765 #ifdef HAVE_XSHM_EXTENSION
773 static XrmOptionDescRec twang_options [] = {
774 { "-border-color", ".borderColor", XrmoptionSepArg, 0 },
775 { "-border-width", ".borderWidth", XrmoptionSepArg, 0 },
776 { "-delay", ".delay", XrmoptionSepArg, 0 },
777 { "-duration", ".duration", XrmoptionSepArg, 0 },
778 { "-event-chance", ".eventChance", XrmoptionSepArg, 0 },
779 { "-friction", ".friction", XrmoptionSepArg, 0 },
780 { "-max-columns", ".maxColumns", XrmoptionSepArg, 0 },
781 { "-max-rows", ".maxRows", XrmoptionSepArg, 0 },
782 { "-springiness", ".springiness", XrmoptionSepArg, 0 },
783 { "-tile-size", ".tileSize", XrmoptionSepArg, 0 },
784 { "-transference", ".transference", XrmoptionSepArg, 0 },
785 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
786 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
791 XSCREENSAVER_MODULE ("Twang", twang)