1 /* -*- mode: C; tab-width: 4 -*-
2 * xscreensaver, Copyright (c) 1992-2005 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
14 * by Jonas Munsin (jmunsin@iki.fi) and Jamie Zawinski <jwz@jwz.org>
16 * -check the allocations in init_round_lense again, maybe make it possible again
17 * to use swamp without pre-allocating/calculating (although that
18 * makes it slower) - -swamp is memory hungry
19 * -more distortion matrices (fortunately, I'm out of ideas :)
20 * Stuff that would be cool but probably too much of a resource hog:
21 * -some kind of interpolation to avoid jaggies
22 * -large speed values leaves the image distorted
23 * program idea borrowed from a screensaver on a non-*NIX OS,
25 * 28 Sep 1999 Jonas Munsin (jmunsin@iki.fi)
26 * Added about 10x faster algortim for 8, 16 and 32 bpp (modifies pixels
27 * directly avoiding costly XPutPixle(XGetPixel()) calls, inspired by
28 * xwhirl made by horvai@clipper.ens.fr (Peter Horvai) and the XFree86
30 * This piece of code is really horrible, but it works, and at the moment
31 * I don't have time or inspiration to fix something that works (knock
33 * 08 Oct 1999 Jonas Munsin (jmunsin@iki.fi)
34 * Corrected several bugs causing references beyond allocated memory.
38 #include "screenhack.h"
39 #include <X11/Xutil.h>
42 #ifdef HAVE_XSHM_EXTENSION
44 static Bool use_shm = False;
45 static XShmSegmentInfo shm_info;
46 #endif /* HAVE_XSHM_EXTENSION */
54 static struct coo xy_coo[10];
56 static int delay, radius, speed, number, blackhole, vortex, magnify, reflect, slow;
57 static XWindowAttributes xgwa;
59 static Window g_window;
60 static Display *g_dpy;
61 static unsigned long black_pixel;
63 static XImage *orig_map, *buffer_map;
64 static unsigned long *buffer_map_cache;
67 static int ****from_array;
68 static int *fast_from = NULL;
69 static void (*effect) (int) = NULL;
70 static void move_lense(int);
71 static void swamp_thing(int);
72 static void new_rnd_coo(int);
73 static void init_round_lense(void);
74 static void (*draw) (int) = NULL;
75 static void reflect_draw(int);
76 static void plain_draw(int);
78 static void (*draw_routine)(XImage *, XImage *, int, int, int *) = NULL;
79 static void fast_draw_8(XImage *, XImage *, int, int, int *);
80 static void fast_draw_16(XImage *, XImage *, int, int, int *);
81 static void fast_draw_32(XImage *, XImage *, int, int, int *);
82 static void generic_draw(XImage *, XImage *, int, int, int *);
83 static int bpp_size = 0;
86 static void init_distort(Display *dpy, Window window)
95 delay = get_integer_resource("delay", "Integer");
96 radius = get_integer_resource("radius", "Integer");
97 speed = get_integer_resource("speed", "Integer");
98 number = get_integer_resource("number", "Integer");
100 #ifdef HAVE_XSHM_EXTENSION
101 use_shm = get_boolean_resource("useSHM", "Boolean");
102 #endif /* HAVE_XSHM_EXTENSION */
104 blackhole = get_boolean_resource("blackhole", "Boolean");
105 vortex = get_boolean_resource("vortex", "Boolean");
106 magnify = get_boolean_resource("magnify", "Boolean");
107 reflect = get_boolean_resource("reflect", "Boolean");
108 slow = get_boolean_resource("slow", "Boolean");
110 if (get_boolean_resource("swamp", "Boolean"))
111 effect = &swamp_thing;
112 if (get_boolean_resource("bounce", "Boolean"))
113 effect = &move_lense;
115 XGetWindowAttributes (dpy, window, &xgwa);
117 if (effect == NULL && radius == 0 && speed == 0 && number == 0
118 && !blackhole && !vortex && !magnify && !reflect) {
119 /* if no cmdline options are given, randomly choose one of:
120 * -radius 50 -number 4 -speed 1 -bounce
121 * -radius 50 -number 4 -speed 1 -blackhole
122 * -radius 50 -number 4 -speed 1 -vortex
123 * -radius 50 -number 4 -speed 1 -vortex -magnify
124 * -radius 50 -number 4 -speed 1 -vortex -magnify -blackhole
125 * -radius 100 -number 1 -speed 2 -bounce
126 * -radius 100 -number 1 -speed 2 -blackhole
127 * -radius 100 -number 1 -speed 2 -vortex
128 * -radius 100 -number 1 -speed 2 -vortex -magnify
129 * -radius 100 -number 1 -speed 2 -vortex -magnify -blackhole
130 * -radius 80 -number 1 -speed 2 -reflect
131 * -radius 50 -number 3 -speed 2 -reflect
133 * -radius 50 -number 4 -speed 2 -swamp
134 * -radius 50 -number 4 -speed 2 -swamp -blackhole
135 * -radius 50 -number 4 -speed 2 -swamp -vortex
136 * -radius 50 -number 4 -speed 2 -swamp -vortex -magnify
137 * -radius 50 -number 4 -speed 2 -swamp -vortex -magnify -blackhole
140 i = (random() % 12 /* 17 */);
146 radius=50;number=4;speed=1;
147 effect=&move_lense;break;
149 radius=50;number=4;speed=1;blackhole=1;
150 effect=&move_lense;break;
152 radius=50;number=4;speed=1;vortex=1;
153 effect=&move_lense;break;
155 radius=50;number=4;speed=1;vortex=1;magnify=1;
156 effect=&move_lense;break;
158 radius=50;number=4;speed=1;vortex=1;magnify=1;blackhole=1;
159 effect=&move_lense;break;
161 radius=100;number=1;speed=2;
162 effect=&move_lense;break;
164 radius=100;number=1;speed=2;blackhole=1;
165 effect=&move_lense;break;
167 radius=100;number=1;speed=2;vortex=1;
168 effect=&move_lense;break;
170 radius=100;number=1;speed=2;vortex=1;magnify=1;
171 effect=&move_lense;break;
173 radius=100;number=1;speed=2;vortex=1;magnify=1;blackhole=1;
174 effect=&move_lense;break;
177 radius=80;number=1;speed=2;reflect=1;
178 draw = &reflect_draw;effect = &move_lense;break;
180 radius=50;number=4;speed=2;reflect=1;
181 draw = &reflect_draw;effect = &move_lense;break;
183 #if 0 /* jwz: not these */
185 radius=50;number=4;speed=2;
186 effect=&swamp_thing;break;
188 radius=50;number=4;speed=2;blackhole=1;
189 effect=&swamp_thing;break;
191 radius=50;number=4;speed=2;vortex=1;
192 effect=&swamp_thing;break;
194 radius=50;number=4;speed=2;vortex=1;magnify=1;
195 effect=&swamp_thing;break;
197 radius=50;number=4;speed=2;vortex=1;magnify=1;blackhole=1;
198 effect=&swamp_thing;break;
205 /* but if the window is small, reduce default radius */
206 if (xgwa.width < radius * 8)
207 radius = xgwa.width/8;
210 /* never allow the radius to be too close to the min window dimension
212 if (radius >= xgwa.width * 0.45) radius = xgwa.width * 0.45;
213 if (radius >= xgwa.height * 0.45) radius = xgwa.height * 0.45;
216 /* -swamp mode consumes vast amounts of memory, proportional to radius --
217 so throttle radius to a small-ish value (60 => ~30MB.)
219 if (effect == &swamp_thing && radius > 60)
233 effect = &move_lense;
235 draw = &reflect_draw;
236 effect = &move_lense;
241 black_pixel = BlackPixelOfScreen( xgwa.screen );
243 gcv.function = GXcopy;
244 gcv.subwindow_mode = IncludeInferiors;
245 gcflags = GCForeground |GCFunction;
246 if (use_subwindow_mode_p(xgwa.screen, window)) /* see grabscreen.c */
247 gcflags |= GCSubwindowMode;
248 gc = XCreateGC (dpy, window, gcflags, &gcv);
250 load_random_image (xgwa.screen, window, window, NULL, NULL);
253 orig_map = XGetImage(dpy, window, 0, 0, xgwa.width, xgwa.height,
255 buffer_map_cache = malloc(sizeof(unsigned long)*(2*radius+speed+2)*(2*radius+speed+2));
257 if (buffer_map_cache == NULL) {
262 # ifdef HAVE_XSHM_EXTENSION
266 buffer_map = create_xshm_image(dpy, xgwa.visual, orig_map->depth,
267 ZPixmap, 0, &shm_info,
268 2*radius + speed + 2,
269 2*radius + speed + 2);
273 # endif /* HAVE_XSHM_EXTENSION */
277 buffer_map = XCreateImage(dpy, xgwa.visual,
278 orig_map->depth, ZPixmap, 0, 0,
279 2*radius + speed + 2, 2*radius + speed + 2,
281 buffer_map->data = (char *)
282 calloc(buffer_map->height, buffer_map->bytes_per_line);
285 if ((buffer_map->byte_order == orig_map->byte_order)
286 && (buffer_map->depth == orig_map->depth)
287 && (buffer_map->format == ZPixmap)
288 && (orig_map->format == ZPixmap)
290 switch (orig_map->bits_per_pixel) {
292 draw_routine = &fast_draw_32;
293 bpp_size = sizeof(CARD32);
296 draw_routine = &fast_draw_16;
297 bpp_size = sizeof(CARD16);
300 draw_routine = &fast_draw_8;
301 bpp_size = sizeof(CARD8);
304 draw_routine = &generic_draw;
308 draw_routine = &generic_draw;
312 for (i = 0; i < number; i++) {
315 xy_coo[i].r = (i*radius)/(number-1); /* "randomize" initial */
318 xy_coo[i].r_change = speed + (i%2)*2*(-speed); /* values a bit */
319 xy_coo[i].xmove = speed + (i%2)*2*(-speed);
320 xy_coo[i].ymove = speed + (i%2)*2*(-speed);
325 /* example: initializes a "see-trough" matrix */
326 /* static void make_null_lense(void)
329 for (i = 0; i < 2*radius+speed+2; i++) {
330 for (j = 0 ; j < 2*radius+speed+2 ; j++) {
337 static void convert(void) {
340 fast_from = calloc(1, sizeof(int)*((buffer_map->bytes_per_line/bpp_size)*(2*radius+speed+2) + 2*radius+speed+2));
341 if (fast_from == NULL) {
346 for (i = 0; i < 2*radius+speed+2; i++) {
347 for (j = 0; j < 2*radius+speed+2; j++) {
348 *(p + i + j*buffer_map->bytes_per_line/bpp_size)
349 = from[i][j][0] + xgwa.width*from[i][j][1];
350 if (*(p + i + j*buffer_map->bytes_per_line/bpp_size) < 0
351 || *(p + i + j*buffer_map->bytes_per_line/bpp_size) >= orig_map->height*orig_map->width) {
352 *(p + i + j*buffer_map->bytes_per_line/bpp_size) = 0;
358 /* makes a lense with the Radius=loop and centred in
359 * the point (radius, radius)
361 static void make_round_lense(int radius, int loop)
365 for (i = 0; i < 2*radius+speed+2; i++) {
366 for(j = 0; j < ((0 == bpp_size) ? (2*radius+speed+2) : (buffer_map->bytes_per_line/bpp_size)); j++) {
368 r = sqrt ((i-radius)*(i-radius)+(j-radius)*(j-radius));
376 if (vortex) { /* vortex-twist effect */
378 /* this one-line formula for getting a nice rotation angle is borrowed
379 * (with permission) from the whirl plugin for gimp,
380 * Copyright (C) 1996 Federico Mena Quintero
382 /* 5 is just a constant used because it looks good :) */
383 angle = 5*(1-d)*(1-d);
385 /* Avoid atan2: DOMAIN error message */
386 if ((radius-j) == 0.0 && (radius-i) == 0.0) {
387 from[i][j][0] = radius + cos(angle)*r;
388 from[i][j][1] = radius + sin(angle)*r;
390 from[i][j][0] = radius +
391 cos(angle - atan2(radius-j, -(radius-i)))*r;
392 from[i][j][1] = radius +
393 sin(angle - atan2(radius-j, -(radius-i)))*r;
397 if (blackhole && r != 0) /* blackhole effect */
399 from[i][j][0] = radius + (from[i][j][0]-radius)*r;
400 from[i][j][1] = radius + (from[i][j][1]-radius)*r;
402 } else { /* default is to magnify */
405 /* raising r to different power here gives different amounts of
406 * distortion, a negative value sucks everything into a black hole
409 if (blackhole && r != 0) /* blackhole effect */
411 /* bubble effect (and blackhole) */
412 from[i][j][0] = radius + (i-radius)*r;
413 from[i][j][1] = radius + (j-radius)*r;
415 } else { /* not inside loop */
422 /* this is really just a quick hack to keep both the compability mode with all depths and still
423 * allow the custom optimized draw routines with the minimum amount of work */
430 # define EXIT_FAILURE -1
433 static void allocate_lense(void)
436 int s = ((0 != bpp_size) ? (buffer_map->bytes_per_line/bpp_size) : (2*radius+speed+2));
437 /* maybe this should be redone so that from[][][] is in one block;
438 * then pointers could be used instead of arrays in some places (and
439 * maybe give a speedup - maybe also consume less memory)
441 from = (int ***)malloc(s*sizeof(int **));
446 for (i = 0; i < s; i++) {
447 from[i] = (int **)malloc((2*radius+speed+2) * sizeof(int *));
448 if (from[i] == NULL) {
452 for (j = 0; j < s; j++) {
453 from[i][j] = (int *)malloc(2 * sizeof(int));
454 if (from[i][j] == NULL) {
462 /* from_array in an array containing precalculated from matrices,
463 * this is a double faced mem vs speed trade, it's faster, but eats
464 * _a lot_ of mem for large radius (is there a bug here? I can't see it)
466 static void init_round_lense(void)
470 if (effect == &swamp_thing) {
471 from_array = (int ****)malloc((radius+1)*sizeof(int ***));
472 for (k=0; k <= radius; k++) {
474 make_round_lense(radius, k);
475 from_array[k] = from;
477 } else { /* just allocate one from[][][] */
479 make_round_lense(radius,radius);
483 /* If fast_draw_8, fast_draw_16 or fast_draw_32 are to be used, the following properties
484 * of the src and dest XImages must hold (otherwise the generic, slooow, method provided
485 * by X is to be used):
486 * src->byte_order == dest->byte_order
487 * src->format == ZPixmap && dest->format == ZPixmap
488 * src->depth == dest->depth == the depth the function in question asumes
489 * x and y is the coordinates in src from where to cut out the image from,
490 * distort_matrix is a precalculated array of how to distort the matrix
493 static void fast_draw_8(XImage *src, XImage *dest, int x, int y, int *distort_matrix) {
494 CARD8 *u = (CARD8 *)dest->data;
495 CARD8 *t = (CARD8 *)src->data + x + y*src->bytes_per_line/sizeof(CARD8);
497 while (u < (CARD8 *)(dest->data + sizeof(CARD8)*dest->height
498 *dest->bytes_per_line/sizeof(CARD8))) {
499 *u++ = t[*distort_matrix++];
503 static void fast_draw_16(XImage *src, XImage *dest, int x, int y, int *distort_matrix) {
504 CARD16 *u = (CARD16 *)dest->data;
505 CARD16 *t = (CARD16 *)src->data + x + y*src->bytes_per_line/sizeof(CARD16);
507 while (u < (CARD16 *)(dest->data + sizeof(CARD16)*dest->height
508 *dest->bytes_per_line/sizeof(CARD16))) {
509 *u++ = t[*distort_matrix++];
513 static void fast_draw_32(XImage *src, XImage *dest, int x, int y, int *distort_matrix) {
514 CARD32 *u = (CARD32 *)dest->data;
515 CARD32 *t = (CARD32 *)src->data + x + y*src->bytes_per_line/sizeof(CARD32);
517 while (u < (CARD32 *)(dest->data + sizeof(CARD32)*dest->height
518 *dest->bytes_per_line/sizeof(CARD32))) {
519 *u++ = t[*distort_matrix++];
523 static void generic_draw(XImage *src, XImage *dest, int x, int y, int *distort_matrix) {
525 for (i = 0; i < dest->width; i++)
526 for (j = 0; j < dest->height; j++)
527 if (from[i][j][0] + x >= 0 &&
528 from[i][j][0] + x < src->width &&
529 from[i][j][1] + y >= 0 &&
530 from[i][j][1] + y < src->height)
531 XPutPixel(dest, i, j,
537 /* generate an XImage of from[][][] and draw it on the screen */
538 static void plain_draw(int k)
540 if (xy_coo[k].x+2*radius+speed+2 > orig_map->width ||
541 xy_coo[k].y+2*radius+speed+2 > orig_map->height)
544 draw_routine(orig_map, buffer_map, xy_coo[k].x, xy_coo[k].y, fast_from);
546 # ifdef HAVE_XSHM_EXTENSION
548 XShmPutImage(g_dpy, g_window, gc, buffer_map, 0, 0, xy_coo[k].x, xy_coo[k].y,
549 2*radius+speed+2, 2*radius+speed+2, False);
554 XPutImage(g_dpy, g_window, gc, buffer_map, 0, 0, xy_coo[k].x, xy_coo[k].y,
555 2*radius+speed+2, 2*radius+speed+2);
560 /* generate an XImage from the reflect algoritm submitted by
561 * Randy Zack <randy@acucorp.com>
562 * draw really got too big and ugly so I split it up
563 * it should be possible to use the from[][] to speed it up
564 * (once I figure out the algorithm used :)
566 static void reflect_draw(int k)
570 int ly, lysq, lx, ny, dist, rsq = radius * radius;
573 if (xy_coo[k].ymove > 0)
575 if (xy_coo[k].xmove > 0)
578 for(i = 0 ; i < 2*radius+speed+2; i++) {
581 ny = xy_coo[k].y + i;
582 if (ny >= orig_map->height) ny = orig_map->height-1;
583 for(j = 0 ; j < 2*radius+speed+2 ; j++) {
585 dist = lx * lx + lysq;
587 ly < -radius || ly > radius ||
588 lx < -radius || lx > radius)
589 XPutPixel( buffer_map, j, i,
590 XGetPixel( orig_map, xy_coo[k].x + j, ny ));
592 XPutPixel( buffer_map, j, i, black_pixel );
594 int x = xy_coo[k].x + cx + (lx * rsq / dist);
595 int y = xy_coo[k].y + cy + (ly * rsq / dist);
596 if (x < 0 || x >= xgwa.width ||
597 y < 0 || y >= xgwa.height)
598 XPutPixel( buffer_map, j, i, black_pixel );
600 XPutPixel( buffer_map, j, i,
601 XGetPixel( orig_map, x, y ));
606 XPutImage(g_dpy, g_window, gc, buffer_map, 0, 0, xy_coo[k].x, xy_coo[k].y,
607 2*radius+speed+2, 2*radius+speed+2);
610 /* create a new, random coordinate, that won't interfer with any other
611 * coordinates, as the drawing routines would be significantly slowed
612 * down if they were to handle serveral layers of distortions
614 static void new_rnd_coo(int k)
618 xy_coo[k].x = (random() % (xgwa.width-2*radius));
619 xy_coo[k].y = (random() % (xgwa.height-2*radius));
621 for (i = 0; i < number; i++) {
623 if ((abs(xy_coo[k].x - xy_coo[i].x) <= 2*radius+speed+2)
624 && (abs(xy_coo[k].y - xy_coo[i].y) <= 2*radius+speed+2)) {
625 xy_coo[k].x = (random() % (xgwa.width-2*radius));
626 xy_coo[k].y = (random() % (xgwa.height-2*radius));
633 /* move lens and handle bounces with walls and other lenses */
634 static void move_lense(int k)
638 if (xy_coo[k].x + 2*radius + speed + 2 >= xgwa.width)
639 xy_coo[k].xmove = -abs(xy_coo[k].xmove);
640 if (xy_coo[k].x <= speed)
641 xy_coo[k].xmove = abs(xy_coo[k].xmove);
642 if (xy_coo[k].y + 2*radius + speed + 2 >= xgwa.height)
643 xy_coo[k].ymove = -abs(xy_coo[k].ymove);
644 if (xy_coo[k].y <= speed)
645 xy_coo[k].ymove = abs(xy_coo[k].ymove);
647 xy_coo[k].x = xy_coo[k].x + xy_coo[k].xmove;
648 xy_coo[k].y = xy_coo[k].y + xy_coo[k].ymove;
650 /* bounce against othe lenses */
651 for (i = 0; i < number; i++) {
654 /* This commented test is for rectangular lenses (not currently used) and
655 * the one used is for circular ones
656 && (abs(xy_coo[k].x - xy_coo[i].x) <= 2*radius)
657 && (abs(xy_coo[k].y - xy_coo[i].y) <= 2*radius)) { */
659 && ((xy_coo[k].x - xy_coo[i].x)*(xy_coo[k].x - xy_coo[i].x)
660 + (xy_coo[k].y - xy_coo[i].y)*(xy_coo[k].y - xy_coo[i].y)
661 <= 2*radius*2*radius)) {
666 xy_coo[k].xmove = xy_coo[i].xmove;
667 xy_coo[k].ymove = xy_coo[i].ymove;
675 /* make xy_coo[k] grow/shrink */
676 static void swamp_thing(int k)
678 if (xy_coo[k].r >= radius)
679 xy_coo[k].r_change = -abs(xy_coo[k].r_change);
681 if (xy_coo[k].r <= 0) {
682 from = from_array[0];
684 xy_coo[k].r_change = abs(xy_coo[k].r_change);
686 xy_coo[k].r=xy_coo[k].r_change;
690 xy_coo[k].r = xy_coo[k].r + xy_coo[k].r_change;
692 if (xy_coo[k].r >= radius)
693 xy_coo[k].r = radius;
694 if (xy_coo[k].r <= 0)
697 from = from_array[xy_coo[k].r];
703 char *progclass = "Distort";
705 char *defaults [] = {
706 "*dontClearRoot: True",
707 #ifdef __sgi /* really, HAVE_READ_DISPLAY_EXTENSION */
721 #ifdef HAVE_XSHM_EXTENSION
722 "*useSHM: False", /* xshm turns out not to help. */
723 #endif /* HAVE_XSHM_EXTENSION */
727 XrmOptionDescRec options [] = {
728 { "-delay", ".delay", XrmoptionSepArg, 0 },
729 { "-radius", ".radius", XrmoptionSepArg, 0 },
730 { "-speed", ".speed", XrmoptionSepArg, 0 },
731 { "-number", ".number", XrmoptionSepArg, 0 },
732 { "-swamp", ".swamp", XrmoptionNoArg, "True" },
733 { "-bounce", ".bounce", XrmoptionNoArg, "True" },
734 { "-reflect", ".reflect", XrmoptionNoArg, "True" },
735 { "-vortex", ".vortex", XrmoptionNoArg, "True" },
736 { "-magnify", ".magnify", XrmoptionNoArg, "True" },
737 { "-blackhole", ".blackhole", XrmoptionNoArg, "True" },
738 { "-slow", ".slow", XrmoptionNoArg, "True" },
739 #ifdef HAVE_XSHM_EXTENSION
740 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
741 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
742 #endif /* HAVE_XSHM_EXTENSION */
747 void screenhack(Display *dpy, Window window)
751 init_distort (dpy, window);
753 for (k = 0; k < number; k++) {
759 screenhack_handle_events (dpy);
760 if (delay) usleep(delay);