1 /* xscreensaver, Copyright (c) 1992, 1993, 1994 Jamie Zawinski <jwz@mcom.com>
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
12 #include "screenhack.h"
16 static int hole_x, hole_y;
17 static int bitmap_w, bitmap_h;
18 static int xoff, yoff;
19 static int grid_w, grid_h;
20 static int delay, delay2;
24 init_slide (dpy, window)
30 XWindowAttributes xgwa;
39 XGetWindowAttributes (dpy, window, &xgwa);
43 copy_default_colormap_contents (dpy, cmap, visual);
45 delay = get_integer_resource ("delay", "Integer");
46 delay2 = get_integer_resource ("delay2", "Integer");
47 grid_size = get_integer_resource ("gridSize", "Integer");
48 pix_inc = get_integer_resource ("pixelIncrement", "Integer");
49 border = get_integer_resource ("internalBorderWidth", "InternalBorderWidth");
50 fg = get_pixel_resource ("background", "Background", dpy,
51 /* Pixels always come out of default map. */
52 XDefaultColormapOfScreen (xgwa.screen));
53 root_p = get_boolean_resource ("root", "Boolean");
55 if (delay < 0) delay = 0;
56 if (delay2 < 0) delay2 = 0;
57 if (pix_inc < 1) pix_inc = 1;
58 if (grid_size < 1) grid_size = 1;
61 gcv.function = GXcopy;
62 gcv.subwindow_mode = IncludeInferiors;
63 gc = XCreateGC (dpy, window, GCForeground |GCFunction | GCSubwindowMode,
66 pixmap = grab_screen_image (dpy, window, root_p);
68 XGetWindowAttributes (dpy, window, &xgwa);
69 bitmap_w = xgwa.width;
70 bitmap_h = xgwa.height;
72 grid_w = bitmap_w / grid_size;
73 grid_h = bitmap_h / grid_size;
74 hole_x = random () % grid_w;
75 hole_y = random () % grid_h;
76 xoff = (bitmap_w - (grid_w * grid_size)) / 2;
77 yoff = (bitmap_h - (grid_h * grid_size)) / 2;
79 d = (pixmap ? pixmap : window);
84 for (i = 0; i <= bitmap_w; i += grid_size)
85 XFillRectangle (dpy, d, gc, xoff+i-border/2, yoff, border, bitmap_h);
86 for (i = 0; i <= bitmap_h; i += grid_size)
87 XFillRectangle (dpy, d, gc, xoff, yoff+i-border/2, bitmap_w, border);
92 XFillRectangle (dpy, d, gc, 0, 0, xoff, bitmap_h);
93 XFillRectangle (dpy, d, gc, bitmap_w - xoff, 0, xoff, bitmap_h);
97 XFillRectangle (dpy, d, gc, 0, 0, bitmap_w, yoff);
98 XFillRectangle (dpy, d, gc, 0, bitmap_h - yoff, bitmap_w, yoff);
101 if (pixmap) XClearWindow (dpy, window);
103 if (delay2) usleep (delay2 * 2);
104 for (i = 0; i < grid_size; i += pix_inc)
107 points[0].x = xoff + grid_size * hole_x;
108 points[0].y = yoff + grid_size * hole_y;
109 points[1].x = points[0].x + grid_size;
110 points[1].y = points[0].y;
111 points[2].x = points[0].x;
112 points[2].y = points[0].y + i;
113 XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin);
115 points[1].x = points[0].x;
116 points[1].y = points[0].y + grid_size;
117 points[2].x = points[0].x + i;
118 points[2].y = points[0].y + grid_size;
119 XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin);
121 points[0].x = points[1].x + grid_size;
122 points[0].y = points[1].y;
123 points[2].x = points[0].x;
124 points[2].y = points[0].y - i;
125 XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin);
127 points[1].x = points[0].x;
128 points[1].y = points[0].y - grid_size;
129 points[2].x = points[1].x - i;
130 points[2].y = points[1].y;
131 XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin);
134 if (delay) usleep (delay);
137 XFillRectangle (dpy, window, gc,
138 xoff + grid_size * hole_x,
139 yoff + grid_size * hole_y,
140 grid_size, grid_size);
148 /* this code is a total kludge, but who cares, it works... */
149 int i, x, y, ix, iy, dx, dy, dir, w, h, size, inc;
150 static int last = -1;
155 case 0: dx = 0, dy = 1; break;
156 case 1: dx = -1, dy = 0; break;
157 case 2: dx = 0, dy = -1; break;
158 case 3: dx = 1, dy = 0; break;
161 } while (dir == last ||
162 hole_x + dx < 0 || hole_x + dx >= grid_w ||
163 hole_y + dy < 0 || hole_y + dy >= grid_h);
164 if (grid_w > 1 && grid_h > 1)
165 last = (dir == 0 ? 2 : dir == 2 ? 0 : dir == 1 ? 3 : 1);
169 case 0: size = 1 + (random()%(grid_h - hole_y - 1)); h = size; w = 1; break;
170 case 1: size = 1 + (random()%hole_x); w = size; h = 1; break;
171 case 2: size = 1 + (random()%hole_y); h = size; w = 1; break;
172 case 3: size = 1 + (random()%(grid_w - hole_x - 1)); w = size; h = 1; break;
176 if (dx == -1) hole_x -= (size - 1);
177 else if (dy == -1) hole_y -= (size - 1);
179 ix = x = xoff + (hole_x + dx) * grid_size;
180 iy = y = yoff + (hole_y + dy) * grid_size;
182 for (i = 0; i < grid_size; i += inc)
184 if (inc + i > grid_size)
186 XCopyArea (dpy, window, window, gc, x, y, grid_size * w, grid_size * h,
187 x - dx * inc, y - dy * inc);
192 case 0: XFillRectangle (dpy, window, gc,
193 ix, y + grid_size * h, grid_size * w, iy - y);
195 case 1: XFillRectangle (dpy, window, gc, ix, iy, x - ix, grid_size * h);
197 case 2: XFillRectangle (dpy, window, gc, ix, iy, grid_size * w, y - iy);
199 case 3: XFillRectangle (dpy, window, gc,
200 x + grid_size * w, iy, ix - x, grid_size * h);
205 if (delay) usleep (delay);
209 case 0: hole_y += size; break;
210 case 1: hole_x--; break;
211 case 2: hole_y--; break;
212 case 3: hole_x += size; break;
217 char *progclass = "SlidePuzzle";
219 char *defaults [] = {
220 "SlidePuzzle.mappedWhenManaged:false",
221 "SlidePuzzle.dontClearWindow: true",
222 "*background: black",
224 "*pixelIncrement: 10",
225 "*internalBorderWidth: 1",
231 XrmOptionDescRec options [] = {
232 { "-grid-size", ".gridSize", XrmoptionSepArg, 0 },
233 { "-ibw", ".internalBorderWidth", XrmoptionSepArg, 0 },
234 { "-increment", ".pixelIncrement", XrmoptionSepArg, 0 },
235 { "-delay", ".delay", XrmoptionSepArg, 0 },
236 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
239 int options_size = (sizeof (options) / sizeof (options[0]));
242 screenhack (dpy, window)
246 init_slide (dpy, window);
249 slide1 (dpy, window);
250 if (delay2) usleep (delay2);