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 maxColumns; /* the maximum number of columns of tiles */
52 int maxRows; /* the maximum number of rows of tiles */
53 int tileSize; /* the size (width and height) of a tile */
54 int borderWidth; /* the width of the border around each tile */
55 FLOAT eventChance; /* the chance, per iteration, of an interesting event happening */
56 FLOAT friction; /* friction: the fraction (0..1) by which velocity decreased per iteration */
57 FLOAT springiness; /* springiness: the fraction (0..1) of the orientation that turns into velocity towards the center */
58 FLOAT transference; /* transference: the fraction (0..1) of the orientations of orthogonal neighbors that turns into velocity (in the same direction as the orientation) */
59 int windowWidth; /* width and height of the window */
61 Screen *screen; /* the screen to draw on */
62 XImage *sourceImage; /* image source of stuff to draw */
63 XImage *workImage; /* work area image, used when rendering */
64 XImage *backgroundImage; /* image filled with background pixels */
66 GC backgroundGC; /* GC for the background color */
67 GC foregroundGC; /* GC for the foreground color */
68 GC borderGC; /* GC for the border color */
69 unsigned long backgroundPixel; /* background color as a pixel value */
70 unsigned long borderPixel; /* border color as a pixel value */
72 Tile *tiles; /* array of tiles (left->right, top->bottom, row major) */
73 int rows; /* number of rows of tiles */
74 int columns; /* number of columns of tiles */
76 Tile **sortedTiles; /* array of tile pointers, sorted by zoom */
77 int tileCount; /* total number of tiles */
79 async_load_state *img_loader;
81 Bool useShm; /* whether or not to use xshm */
82 #ifdef HAVE_XSHM_EXTENSION
83 XShmSegmentInfo shmInfo;
88 #define TILE_AT(col,row) (&st->tiles[(row) * st->columns + (col)])
90 #define MAX_VANGLE (M_PI / 4.0)
91 #define MAX_VZOOM 0.25
93 #define RAND_ANGLE (RAND_FLOAT_PM1 * M_PI)
94 #define RAND_ZOOM (RAND_FLOAT_PM1)
95 #define RAND_VANGLE (RAND_FLOAT_PM1 * MAX_VANGLE)
96 #define RAND_VZOOM (RAND_FLOAT_PM1 * MAX_VZOOM)
101 * overall setup stuff
104 /* grab the source image */
105 static void grabImage_start (struct state *st, XWindowAttributes *xwa)
107 XFillRectangle (st->dpy, st->window, st->backgroundGC, 0, 0,
108 st->windowWidth, st->windowHeight);
109 st->backgroundImage =
110 XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
113 st->img_loader = load_image_async_simple (0, xwa->screen, st->window,
117 static void grabImage_done (struct state *st)
119 XWindowAttributes xwa;
120 XGetWindowAttributes (st->dpy, st->window, &xwa);
122 st->sourceImage = XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
125 #ifdef HAVE_XSHM_EXTENSION
126 st->workImage = NULL;
129 st->workImage = create_xshm_image (st->dpy, xwa.visual, xwa.depth,
130 ZPixmap, 0, &st->shmInfo,
131 st->windowWidth, st->windowHeight);
135 fprintf (stderr, "create_xshm_image failed\n");
139 if (st->workImage == NULL)
140 #endif /* HAVE_XSHM_EXTENSION */
142 /* just use XSubImage to acquire the right visual, depth, etc;
143 * easier than the other alternatives */
144 st->workImage = XSubImage (st->sourceImage, 0, 0, st->windowWidth, st->windowHeight);
147 /* set up the system */
148 static void setup (struct state *st)
150 XWindowAttributes xgwa;
153 XGetWindowAttributes (st->dpy, st->window, &xgwa);
155 st->screen = xgwa.screen;
156 st->windowWidth = xgwa.width;
157 st->windowHeight = xgwa.height;
159 gcv.line_width = st->borderWidth;
160 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
161 "borderColor", "BorderColor");
162 st->borderPixel = gcv.foreground;
163 st->borderGC = XCreateGC (st->dpy, st->window, GCForeground | GCLineWidth,
166 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
167 "background", "Background");
168 st->backgroundPixel = gcv.foreground;
169 st->backgroundGC = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
171 gcv.foreground = get_pixel_resource (st->dpy, xgwa.colormap,
172 "foreground", "Foreground");
173 st->foregroundGC = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
175 grabImage_start (st, &xgwa);
184 /* event: randomize all the angular velocities */
185 static void randomizeAllAngularVelocities (struct state *st)
190 for (r = 0; r < st->rows; r++)
192 for (c = 0; c < st->columns; c++)
194 TILE_AT (c, r)->vAngle = RAND_VANGLE;
199 /* event: randomize all the zoomular velocities */
200 static void randomizeAllZoomularVelocities (struct state *st)
205 for (r = 0; r < st->rows; r++)
207 for (c = 0; c < st->columns; c++)
209 TILE_AT (c, r)->vZoom = RAND_VZOOM;
214 /* event: randomize all the velocities */
215 static void randomizeAllVelocities (struct state *st)
217 randomizeAllAngularVelocities (st);
218 randomizeAllZoomularVelocities (st);
221 /* event: randomize all the angular orientations */
222 static void randomizeAllAngularOrientations (struct state *st)
227 for (r = 0; r < st->rows; r++)
229 for (c = 0; c < st->columns; c++)
231 TILE_AT (c, r)->angle = RAND_ANGLE;
236 /* event: randomize all the zoomular orientations */
237 static void randomizeAllZoomularOrientations (struct state *st)
242 for (r = 0; r < st->rows; r++)
244 for (c = 0; c < st->columns; c++)
246 TILE_AT (c, r)->zoom = RAND_ZOOM;
251 /* event: randomize all the orientations */
252 static void randomizeAllOrientations (struct state *st)
254 randomizeAllAngularOrientations (st);
255 randomizeAllZoomularOrientations (st);
258 /* event: randomize everything */
259 static void randomizeEverything (struct state *st)
261 randomizeAllVelocities (st);
262 randomizeAllOrientations (st);
265 /* event: pick one tile and randomize all its stats */
266 static void randomizeOneTile (struct state *st)
268 int c = RAND_FLOAT_01 * st->columns;
269 int r = RAND_FLOAT_01 * st->rows;
271 Tile *t = TILE_AT (c, r);
272 t->angle = RAND_ANGLE;
274 t->vAngle = RAND_VANGLE;
275 t->vZoom = RAND_VZOOM;
278 /* event: pick one row and randomize everything about each of its tiles */
279 static void randomizeOneRow (struct state *st)
282 int r = RAND_FLOAT_01 * st->rows;
284 for (c = 0; c < st->columns; c++)
286 Tile *t = TILE_AT (c, r);
287 t->angle = RAND_ANGLE;
289 t->vAngle = RAND_VANGLE;
290 t->vZoom = RAND_VZOOM;
294 /* event: pick one column and randomize everything about each of its tiles */
295 static void randomizeOneColumn (struct state *st)
297 int c = RAND_FLOAT_01 * st->columns;
300 for (r = 0; r < st->rows; r++)
302 Tile *t = TILE_AT (c, r);
303 t->angle = RAND_ANGLE;
305 t->vAngle = RAND_VANGLE;
306 t->vZoom = RAND_VZOOM;
310 /* do model event processing */
311 static void modelEvents (struct state *st)
315 if (RAND_FLOAT_01 > st->eventChance)
320 which = RAND_FLOAT_01 * 10;
324 case 0: randomizeAllAngularVelocities (st); break;
325 case 1: randomizeAllZoomularVelocities (st); break;
326 case 2: randomizeAllVelocities (st); break;
327 case 3: randomizeAllAngularOrientations (st); break;
328 case 4: randomizeAllZoomularOrientations (st); break;
329 case 5: randomizeAllOrientations (st); break;
330 case 6: randomizeEverything (st); break;
331 case 7: randomizeOneTile (st); break;
332 case 8: randomizeOneColumn (st); break;
333 case 9: randomizeOneRow (st); break;
337 /* update the model for one iteration */
338 static void updateModel (struct state *st)
343 /* for each tile, decrease its velocities according to the friction,
344 * and increase them based on its current orientation and the orientations
345 * of its orthogonal neighbors */
346 for (r = 0; r < st->rows; r++)
348 for (c = 0; c < st->columns; c++)
350 Tile *t = TILE_AT (c, r);
353 FLOAT va = t->vAngle;
356 va -= t->angle * st->springiness;
357 vz -= t->zoom * st->springiness;
361 Tile *t2 = TILE_AT (c - 1, r);
362 va += (t2->angle - a) * st->transference;
363 vz += (t2->zoom - z) * st->transference;
366 if (c < (st->columns - 1))
368 Tile *t2 = TILE_AT (c + 1, r);
369 va += (t2->angle - a) * st->transference;
370 vz += (t2->zoom - z) * st->transference;
375 Tile *t2 = TILE_AT (c, r - 1);
376 va += (t2->angle - a) * st->transference;
377 vz += (t2->zoom - z) * st->transference;
380 if (r < (st->rows - 1))
382 Tile *t2 = TILE_AT (c, r + 1);
383 va += (t2->angle - a) * st->transference;
384 vz += (t2->zoom - z) * st->transference;
387 va *= (1.0 - st->friction);
388 vz *= (1.0 - st->friction);
390 if (va > MAX_VANGLE) va = MAX_VANGLE;
391 else if (va < -MAX_VANGLE) va = -MAX_VANGLE;
394 if (vz > MAX_VZOOM) vz = MAX_VZOOM;
395 else if (vz < -MAX_VZOOM) vz = -MAX_VZOOM;
400 /* for each tile, update its orientation based on its velocities */
401 for (r = 0; r < st->rows; r++)
403 for (c = 0; c < st->columns; c++)
405 Tile *t = TILE_AT (c, r);
406 FLOAT a = t->angle + t->vAngle;
407 FLOAT z = t->zoom + t->vZoom;
409 if (a > M_PI) a = M_PI;
410 else if (a < -M_PI) a = -M_PI;
413 if (z > 1.0) z = 1.0;
414 else if (z < -1.0) z = -1.0;
420 /* the comparator to us to sort the tiles (used immediately below); it'd
421 * sure be nice if C allowed inner functions (or jeebus-forbid *real
423 static int sortTilesComparator (const void *v1, const void *v2)
425 Tile *t1 = *(Tile **) v1;
426 Tile *t2 = *(Tile **) v2;
428 if (t1->zoom < t2->zoom)
433 if (t1->zoom > t2->zoom)
441 /* sort the tiles in sortedTiles by zoom */
442 static void sortTiles (struct state *st)
444 qsort (st->sortedTiles, st->tileCount, sizeof (Tile *), sortTilesComparator);
447 /* render the given tile */
448 static void renderTile (struct state *st, Tile *t)
450 /* note: the zoom as stored per tile is log-based (centered on 0, with
451 * 0 being no zoom, but the range for zoom-as-drawn is 0.4..2.5,
452 * hence the alteration of t->zoom, below */
459 FLOAT zoom = pow (2.5, t->zoom);
460 FLOAT ang = -t->angle;
461 FLOAT sinAng = sin (ang);
462 FLOAT cosAng = cos (ang);
464 FLOAT innerBorder = (st->tileSize - st->borderWidth) / 2.0;
465 FLOAT outerBorder = innerBorder + st->borderWidth;
467 int maxCoord = outerBorder * zoom * (fabs (sinAng) + fabs (cosAng));
468 int minX = tx - maxCoord;
469 int maxX = tx + maxCoord;
470 int minY = ty - maxCoord;
471 int maxY = ty + maxCoord;
475 if (minX < 0) minX = 0;
476 if (maxX > st->windowWidth) maxX = st->windowWidth;
477 if (minY < 0) minY = 0;
478 if (maxY > st->windowHeight) maxY = st->windowHeight;
483 for (y = minY, prey = y - ty; y < maxY; y++, prey++)
485 FLOAT prex = minX - tx;
486 FLOAT srcx = prex * cosAng - prey * sinAng;
487 FLOAT srcy = prex * sinAng + prey * cosAng;
491 x++, srcx += cosAng, srcy += sinAng)
493 if ((srcx < -innerBorder) || (srcx >= innerBorder) ||
494 (srcy < -innerBorder) || (srcy >= innerBorder))
496 if ((srcx < -outerBorder) || (srcx >= outerBorder) ||
497 (srcy < -outerBorder) || (srcy >= outerBorder))
501 XPutPixel (st->workImage, x, y, st->borderPixel);
506 XGetPixel (st->sourceImage, srcx + tx, srcy + ty);
507 XPutPixel (st->workImage, x, y, p);
513 /* render and display the current model */
514 static void renderFrame (struct state *st)
518 memcpy (st->workImage->data, st->backgroundImage->data,
519 st->workImage->bytes_per_line * st->workImage->height);
523 for (n = 0; n < st->tileCount; n++)
525 renderTile (st, st->sortedTiles[n]);
528 #ifdef HAVE_XSHM_EXTENSION
530 XShmPutImage (st->dpy, st->window, st->backgroundGC, st->workImage, 0, 0, 0, 0,
531 st->windowWidth, st->windowHeight, False);
533 #endif /* HAVE_XSHM_EXTENSION */
534 XPutImage (st->dpy, st->window, st->backgroundGC, st->workImage,
535 0, 0, 0, 0, st->windowWidth, st->windowHeight);
538 /* set up the model */
539 static void setupModel (struct state *st)
544 int leftX; /* x of the center of the top-left tile */
545 int topY; /* y of the center of the top-left tile */
547 if (st->tileSize > (st->windowWidth / 2))
549 st->tileSize = st->windowWidth / 2;
552 if (st->tileSize > (st->windowHeight / 2))
554 st->tileSize = st->windowHeight / 2;
557 st->columns = st->tileSize ? st->windowWidth / st->tileSize : 0;
558 st->rows = st->tileSize ? st->windowHeight / st->tileSize : 0;
560 if ((st->maxColumns != 0) && (st->columns > st->maxColumns))
562 st->columns = st->maxColumns;
565 if ((st->maxRows != 0) && (st->rows > st->maxRows))
567 st->rows = st->maxRows;
570 st->tileCount = st->rows * st->columns;
572 leftX = (st->windowWidth - (st->columns * st->tileSize) + st->tileSize) / 2;
573 topY = (st->windowHeight - (st->rows * st->tileSize) + st->tileSize) / 2;
575 st->tiles = calloc (st->tileCount, sizeof (Tile));
576 st->sortedTiles = calloc (st->tileCount, sizeof (Tile *));
578 for (r = 0; r < st->rows; r++)
580 for (c = 0; c < st->columns; c++)
582 Tile *t = TILE_AT (c, r);
583 t->x = leftX + c * st->tileSize;
584 t->y = topY + r * st->tileSize;
585 st->sortedTiles[c + r * st->columns] = t;
589 randomizeEverything (st);
592 /* do one iteration */
595 twang_draw (Display *dpy, Window window, void *closure)
597 struct state *st = (struct state *) closure;
599 if (st->img_loader) /* still loading */
601 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
602 if (! st->img_loader) { /* just finished */
617 twang_reshape (Display *dpy, Window window, void *closure,
618 unsigned int w, unsigned int h)
623 twang_event (Display *dpy, Window window, void *closure, XEvent *event)
629 twang_free (Display *dpy, Window window, void *closure)
631 struct state *st = (struct state *) closure;
637 /* main and options and stuff */
639 /* initialize the user-specifiable params */
640 static void initParams (struct state *st)
644 st->borderWidth = get_integer_resource (st->dpy, "borderWidth", "Integer");
645 if (st->borderWidth < 0)
647 fprintf (stderr, "error: border width must be at least 0\n");
651 st->delay = get_integer_resource (st->dpy, "delay", "Delay");
654 fprintf (stderr, "error: delay must be at least 0\n");
658 st->eventChance = get_float_resource (st->dpy, "eventChance", "Double");
659 if ((st->eventChance < 0.0) || (st->eventChance > 1.0))
661 fprintf (stderr, "error: eventChance must be in the range 0..1\n");
665 st->friction = get_float_resource (st->dpy, "friction", "Double");
666 if ((st->friction < 0.0) || (st->friction > 1.0))
668 fprintf (stderr, "error: friction must be in the range 0..1\n");
672 st->maxColumns = get_integer_resource (st->dpy, "maxColumns", "Integer");
673 if (st->maxColumns < 0)
675 fprintf (stderr, "error: max columns must be at least 0\n");
679 st->maxRows = get_integer_resource (st->dpy, "maxRows", "Integer");
682 fprintf (stderr, "error: max rows must be at least 0\n");
686 st->springiness = get_float_resource (st->dpy, "springiness", "Double");
687 if ((st->springiness < 0.0) || (st->springiness > 1.0))
689 fprintf (stderr, "error: springiness must be in the range 0..1\n");
693 st->tileSize = get_integer_resource (st->dpy, "tileSize", "Integer");
694 if (st->tileSize < 1)
696 fprintf (stderr, "error: tile size must be at least 1\n");
700 st->transference = get_float_resource (st->dpy, "transference", "Double");
701 if ((st->transference < 0.0) || (st->transference > 1.0))
703 fprintf (stderr, "error: transference must be in the range 0..1\n");
707 #ifdef HAVE_XSHM_EXTENSION
708 st->useShm = get_boolean_resource (st->dpy, "useSHM", "Boolean");
718 twang_init (Display *dpy, Window win)
720 struct state *st = (struct state *) calloc (1, sizeof(*st));
732 static const char *twang_defaults [] = {
733 ".background: black",
734 ".foreground: white",
735 "*borderColor: blue",
738 "*eventChance: 0.01",
744 "*transference: 0.025",
745 #ifdef HAVE_XSHM_EXTENSION
753 static XrmOptionDescRec twang_options [] = {
754 { "-border-color", ".borderColor", XrmoptionSepArg, 0 },
755 { "-border-width", ".borderWidth", XrmoptionSepArg, 0 },
756 { "-delay", ".delay", XrmoptionSepArg, 0 },
757 { "-event-chance", ".eventChance", XrmoptionSepArg, 0 },
758 { "-friction", ".friction", XrmoptionSepArg, 0 },
759 { "-max-columns", ".maxColumns", XrmoptionSepArg, 0 },
760 { "-max-rows", ".maxRows", XrmoptionSepArg, 0 },
761 { "-springiness", ".springiness", XrmoptionSepArg, 0 },
762 { "-tile-size", ".tileSize", XrmoptionSepArg, 0 },
763 { "-transference", ".transference", XrmoptionSepArg, 0 },
764 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
765 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
770 XSCREENSAVER_MODULE ("Twang", twang)