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);
113 st->backgroundImage =
114 XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
117 st->start_time = time ((time_t) 0);
118 st->img_loader = load_image_async_simple (0, xwa->screen, st->window,
123 grabImage_done (struct state *st)
125 XWindowAttributes xwa;
126 XGetWindowAttributes (st->dpy, st->window, &xwa);
128 st->start_time = time ((time_t) 0);
129 if (st->sourceImage) XDestroyImage (st->sourceImage);
130 st->sourceImage = XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
133 if (st->workImage) XDestroyImage (st->workImage);
134 st->workImage = NULL;
136 #ifdef HAVE_XSHM_EXTENSION
139 st->workImage = create_xshm_image (st->dpy, xwa.visual, xwa.depth,
140 ZPixmap, 0, &st->shmInfo,
141 st->windowWidth, st->windowHeight);
145 fprintf (stderr, "create_xshm_image failed\n");
149 if (st->workImage == NULL)
150 #endif /* HAVE_XSHM_EXTENSION */
152 /* just use XSubImage to acquire the right visual, depth, etc;
153 * easier than the other alternatives */
154 st->workImage = XSubImage (st->sourceImage, 0, 0, st->windowWidth, st->windowHeight);
157 /* set up the system */
158 static void setup (struct state *st)
160 XWindowAttributes xgwa;
163 XGetWindowAttributes (st->dpy, st->window, &xgwa);
165 st->screen = xgwa.screen;
166 st->windowWidth = xgwa.width;
167 st->windowHeight = xgwa.height;
169 gcv.line_width = st->borderWidth;
170 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
171 "borderColor", "BorderColor");
172 st->borderPixel = gcv.foreground;
173 st->borderGC = XCreateGC (st->dpy, st->window, GCForeground | GCLineWidth,
176 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
177 "background", "Background");
178 st->backgroundPixel = gcv.foreground;
179 st->backgroundGC = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
181 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
182 "foreground", "Foreground");
183 st->foregroundGC = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
185 grabImage_start (st, &xgwa);
194 /* event: randomize all the angular velocities */
195 static void randomizeAllAngularVelocities (struct state *st)
200 for (r = 0; r < st->rows; r++)
202 for (c = 0; c < st->columns; c++)
204 TILE_AT (c, r)->vAngle = RAND_VANGLE;
209 /* event: randomize all the zoomular velocities */
210 static void randomizeAllZoomularVelocities (struct state *st)
215 for (r = 0; r < st->rows; r++)
217 for (c = 0; c < st->columns; c++)
219 TILE_AT (c, r)->vZoom = RAND_VZOOM;
224 /* event: randomize all the velocities */
225 static void randomizeAllVelocities (struct state *st)
227 randomizeAllAngularVelocities (st);
228 randomizeAllZoomularVelocities (st);
231 /* event: randomize all the angular orientations */
232 static void randomizeAllAngularOrientations (struct state *st)
237 for (r = 0; r < st->rows; r++)
239 for (c = 0; c < st->columns; c++)
241 TILE_AT (c, r)->angle = RAND_ANGLE;
246 /* event: randomize all the zoomular orientations */
247 static void randomizeAllZoomularOrientations (struct state *st)
252 for (r = 0; r < st->rows; r++)
254 for (c = 0; c < st->columns; c++)
256 TILE_AT (c, r)->zoom = RAND_ZOOM;
261 /* event: randomize all the orientations */
262 static void randomizeAllOrientations (struct state *st)
264 randomizeAllAngularOrientations (st);
265 randomizeAllZoomularOrientations (st);
268 /* event: randomize everything */
269 static void randomizeEverything (struct state *st)
271 randomizeAllVelocities (st);
272 randomizeAllOrientations (st);
275 /* event: pick one tile and randomize all its stats */
276 static void randomizeOneTile (struct state *st)
278 int c = RAND_FLOAT_01 * st->columns;
279 int r = RAND_FLOAT_01 * st->rows;
281 Tile *t = TILE_AT (c, r);
282 t->angle = RAND_ANGLE;
284 t->vAngle = RAND_VANGLE;
285 t->vZoom = RAND_VZOOM;
288 /* event: pick one row and randomize everything about each of its tiles */
289 static void randomizeOneRow (struct state *st)
292 int r = RAND_FLOAT_01 * st->rows;
294 for (c = 0; c < st->columns; c++)
296 Tile *t = TILE_AT (c, r);
297 t->angle = RAND_ANGLE;
299 t->vAngle = RAND_VANGLE;
300 t->vZoom = RAND_VZOOM;
304 /* event: pick one column and randomize everything about each of its tiles */
305 static void randomizeOneColumn (struct state *st)
307 int c = RAND_FLOAT_01 * st->columns;
310 for (r = 0; r < st->rows; r++)
312 Tile *t = TILE_AT (c, r);
313 t->angle = RAND_ANGLE;
315 t->vAngle = RAND_VANGLE;
316 t->vZoom = RAND_VZOOM;
320 /* do model event processing */
321 static void modelEvents (struct state *st)
325 if (RAND_FLOAT_01 > st->eventChance)
330 which = RAND_FLOAT_01 * 10;
334 case 0: randomizeAllAngularVelocities (st); break;
335 case 1: randomizeAllZoomularVelocities (st); break;
336 case 2: randomizeAllVelocities (st); break;
337 case 3: randomizeAllAngularOrientations (st); break;
338 case 4: randomizeAllZoomularOrientations (st); break;
339 case 5: randomizeAllOrientations (st); break;
340 case 6: randomizeEverything (st); break;
341 case 7: randomizeOneTile (st); break;
342 case 8: randomizeOneColumn (st); break;
343 case 9: randomizeOneRow (st); break;
347 /* update the model for one iteration */
348 static void updateModel (struct state *st)
353 /* for each tile, decrease its velocities according to the friction,
354 * and increase them based on its current orientation and the orientations
355 * of its orthogonal neighbors */
356 for (r = 0; r < st->rows; r++)
358 for (c = 0; c < st->columns; c++)
360 Tile *t = TILE_AT (c, r);
363 FLOAT va = t->vAngle;
366 va -= t->angle * st->springiness;
367 vz -= t->zoom * st->springiness;
371 Tile *t2 = TILE_AT (c - 1, r);
372 va += (t2->angle - a) * st->transference;
373 vz += (t2->zoom - z) * st->transference;
376 if (c < (st->columns - 1))
378 Tile *t2 = TILE_AT (c + 1, r);
379 va += (t2->angle - a) * st->transference;
380 vz += (t2->zoom - z) * st->transference;
385 Tile *t2 = TILE_AT (c, r - 1);
386 va += (t2->angle - a) * st->transference;
387 vz += (t2->zoom - z) * st->transference;
390 if (r < (st->rows - 1))
392 Tile *t2 = TILE_AT (c, r + 1);
393 va += (t2->angle - a) * st->transference;
394 vz += (t2->zoom - z) * st->transference;
397 va *= (1.0 - st->friction);
398 vz *= (1.0 - st->friction);
400 if (va > MAX_VANGLE) va = MAX_VANGLE;
401 else if (va < -MAX_VANGLE) va = -MAX_VANGLE;
404 if (vz > MAX_VZOOM) vz = MAX_VZOOM;
405 else if (vz < -MAX_VZOOM) vz = -MAX_VZOOM;
410 /* for each tile, update its orientation based on its velocities */
411 for (r = 0; r < st->rows; r++)
413 for (c = 0; c < st->columns; c++)
415 Tile *t = TILE_AT (c, r);
416 FLOAT a = t->angle + t->vAngle;
417 FLOAT z = t->zoom + t->vZoom;
419 if (a > M_PI) a = M_PI;
420 else if (a < -M_PI) a = -M_PI;
423 if (z > 1.0) z = 1.0;
424 else if (z < -1.0) z = -1.0;
430 /* the comparator to us to sort the tiles (used immediately below); it'd
431 * sure be nice if C allowed inner functions (or jeebus-forbid *real
433 static int sortTilesComparator (const void *v1, const void *v2)
435 Tile *t1 = *(Tile **) v1;
436 Tile *t2 = *(Tile **) v2;
438 if (t1->zoom < t2->zoom)
443 if (t1->zoom > t2->zoom)
451 /* sort the tiles in sortedTiles by zoom */
452 static void sortTiles (struct state *st)
454 qsort (st->sortedTiles, st->tileCount, sizeof (Tile *), sortTilesComparator);
457 /* render the given tile */
458 static void renderTile (struct state *st, Tile *t)
460 /* note: the zoom as stored per tile is log-based (centered on 0, with
461 * 0 being no zoom, but the range for zoom-as-drawn is 0.4..2.5,
462 * hence the alteration of t->zoom, below */
469 FLOAT zoom = pow (2.5, t->zoom);
470 FLOAT ang = -t->angle;
471 FLOAT sinAng = sin (ang);
472 FLOAT cosAng = cos (ang);
474 FLOAT innerBorder = (st->tileSize - st->borderWidth) / 2.0;
475 FLOAT outerBorder = innerBorder + st->borderWidth;
477 int maxCoord = outerBorder * zoom * (fabs (sinAng) + fabs (cosAng));
478 int minX = tx - maxCoord;
479 int maxX = tx + maxCoord;
480 int minY = ty - maxCoord;
481 int maxY = ty + maxCoord;
485 if (minX < 0) minX = 0;
486 if (maxX > st->windowWidth) maxX = st->windowWidth;
487 if (minY < 0) minY = 0;
488 if (maxY > st->windowHeight) maxY = st->windowHeight;
493 for (y = minY, prey = y - ty; y < maxY; y++, prey++)
495 FLOAT prex = minX - tx;
496 FLOAT srcx = prex * cosAng - prey * sinAng;
497 FLOAT srcy = prex * sinAng + prey * cosAng;
501 x++, srcx += cosAng, srcy += sinAng)
503 if ((srcx < -innerBorder) || (srcx >= innerBorder) ||
504 (srcy < -innerBorder) || (srcy >= innerBorder))
506 if ((srcx < -outerBorder) || (srcx >= outerBorder) ||
507 (srcy < -outerBorder) || (srcy >= outerBorder))
511 XPutPixel (st->workImage, x, y, st->borderPixel);
516 XGetPixel (st->sourceImage, srcx + tx, srcy + ty);
517 XPutPixel (st->workImage, x, y, p);
523 /* render and display the current model */
524 static void renderFrame (struct state *st)
528 memcpy (st->workImage->data, st->backgroundImage->data,
529 st->workImage->bytes_per_line * st->workImage->height);
533 for (n = 0; n < st->tileCount; n++)
535 renderTile (st, st->sortedTiles[n]);
538 #ifdef HAVE_XSHM_EXTENSION
540 XShmPutImage (st->dpy, st->window, st->backgroundGC, st->workImage, 0, 0, 0, 0,
541 st->windowWidth, st->windowHeight, False);
543 #endif /* HAVE_XSHM_EXTENSION */
544 XPutImage (st->dpy, st->window, st->backgroundGC, st->workImage,
545 0, 0, 0, 0, st->windowWidth, st->windowHeight);
548 /* set up the model */
549 static void setupModel (struct state *st)
554 int leftX; /* x of the center of the top-left tile */
555 int topY; /* y of the center of the top-left tile */
557 if (st->tileSize > (st->windowWidth / 2))
559 st->tileSize = st->windowWidth / 2;
562 if (st->tileSize > (st->windowHeight / 2))
564 st->tileSize = st->windowHeight / 2;
567 st->columns = st->tileSize ? st->windowWidth / st->tileSize : 0;
568 st->rows = st->tileSize ? st->windowHeight / st->tileSize : 0;
570 if ((st->maxColumns != 0) && (st->columns > st->maxColumns))
572 st->columns = st->maxColumns;
575 if ((st->maxRows != 0) && (st->rows > st->maxRows))
577 st->rows = st->maxRows;
580 st->tileCount = st->rows * st->columns;
582 leftX = (st->windowWidth - (st->columns * st->tileSize) + st->tileSize) / 2;
583 topY = (st->windowHeight - (st->rows * st->tileSize) + st->tileSize) / 2;
585 st->tiles = calloc (st->tileCount, sizeof (Tile));
586 st->sortedTiles = calloc (st->tileCount, sizeof (Tile *));
588 for (r = 0; r < st->rows; r++)
590 for (c = 0; c < st->columns; c++)
592 Tile *t = TILE_AT (c, r);
593 t->x = leftX + c * st->tileSize;
594 t->y = topY + r * st->tileSize;
595 st->sortedTiles[c + r * st->columns] = t;
599 randomizeEverything (st);
602 /* do one iteration */
605 twang_draw (Display *dpy, Window window, void *closure)
607 struct state *st = (struct state *) closure;
609 if (st->img_loader) /* still loading */
611 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
612 if (! st->img_loader) { /* just finished */
618 if (!st->img_loader &&
619 st->start_time + st->duration < time ((time_t) 0)) {
620 XWindowAttributes xgwa;
621 XGetWindowAttributes (st->dpy, st->window, &xgwa);
622 grabImage_start (st, &xgwa);
634 twang_reshape (Display *dpy, Window window, void *closure,
635 unsigned int w, unsigned int h)
640 twang_event (Display *dpy, Window window, void *closure, XEvent *event)
642 struct state *st = (struct state *) closure;
643 if (screenhack_event_helper (dpy, window, event))
652 twang_free (Display *dpy, Window window, void *closure)
654 struct state *st = (struct state *) closure;
660 /* main and options and stuff */
662 /* initialize the user-specifiable params */
663 static void initParams (struct state *st)
667 st->borderWidth = get_integer_resource (st->dpy, "borderWidth", "Integer");
668 if (st->borderWidth < 0)
670 fprintf (stderr, "error: border width must be at least 0\n");
674 st->delay = get_integer_resource (st->dpy, "delay", "Delay");
677 fprintf (stderr, "error: delay must be at least 0\n");
681 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
682 if (st->duration < 1) st->duration = 1;
684 st->eventChance = get_float_resource (st->dpy, "eventChance", "Double");
685 if ((st->eventChance < 0.0) || (st->eventChance > 1.0))
687 fprintf (stderr, "error: eventChance must be in the range 0..1\n");
691 st->friction = get_float_resource (st->dpy, "friction", "Double");
692 if ((st->friction < 0.0) || (st->friction > 1.0))
694 fprintf (stderr, "error: friction must be in the range 0..1\n");
698 st->maxColumns = get_integer_resource (st->dpy, "maxColumns", "Integer");
699 if (st->maxColumns < 0)
701 fprintf (stderr, "error: max columns must be at least 0\n");
705 st->maxRows = get_integer_resource (st->dpy, "maxRows", "Integer");
708 fprintf (stderr, "error: max rows must be at least 0\n");
712 st->springiness = get_float_resource (st->dpy, "springiness", "Double");
713 if ((st->springiness < 0.0) || (st->springiness > 1.0))
715 fprintf (stderr, "error: springiness must be in the range 0..1\n");
719 st->tileSize = get_integer_resource (st->dpy, "tileSize", "Integer");
720 if (st->tileSize < 1)
722 fprintf (stderr, "error: tile size must be at least 1\n");
726 st->transference = get_float_resource (st->dpy, "transference", "Double");
727 if ((st->transference < 0.0) || (st->transference > 1.0))
729 fprintf (stderr, "error: transference must be in the range 0..1\n");
733 #ifdef HAVE_XSHM_EXTENSION
734 st->useShm = get_boolean_resource (st->dpy, "useSHM", "Boolean");
744 twang_init (Display *dpy, Window win)
746 struct state *st = (struct state *) calloc (1, sizeof(*st));
758 static const char *twang_defaults [] = {
759 ".background: black",
760 ".foreground: white",
761 "*borderColor: blue",
765 "*eventChance: 0.01",
771 "*transference: 0.025",
772 #ifdef HAVE_XSHM_EXTENSION
778 "*ignoreRotation: True",
779 "*rotateImages: True",
784 static XrmOptionDescRec twang_options [] = {
785 { "-border-color", ".borderColor", XrmoptionSepArg, 0 },
786 { "-border-width", ".borderWidth", XrmoptionSepArg, 0 },
787 { "-delay", ".delay", XrmoptionSepArg, 0 },
788 { "-duration", ".duration", XrmoptionSepArg, 0 },
789 { "-event-chance", ".eventChance", XrmoptionSepArg, 0 },
790 { "-friction", ".friction", XrmoptionSepArg, 0 },
791 { "-max-columns", ".maxColumns", XrmoptionSepArg, 0 },
792 { "-max-rows", ".maxRows", XrmoptionSepArg, 0 },
793 { "-springiness", ".springiness", XrmoptionSepArg, 0 },
794 { "-tile-size", ".tileSize", XrmoptionSepArg, 0 },
795 { "-transference", ".transference", XrmoptionSepArg, 0 },
796 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
797 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
802 XSCREENSAVER_MODULE ("Twang", twang)