ftp://ftp.uni-heidelberg.de/pub/X11/contrib/applications/xscreensaver-1.25.tar.Z
[xscreensaver] / hacks / slidescreen.c
1 /* xscreensaver, Copyright (c) 1992, 1993, 1994 Jamie Zawinski <jwz@mcom.com>
2  *
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 
9  * implied warranty.
10  */
11
12 #include "screenhack.h"
13
14 static int grid_size;
15 static int pix_inc;
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;
21 static GC gc;
22
23 static void
24 init_slide (dpy, window)
25      Display *dpy;
26      Window window;
27 {
28   int i;
29   XGCValues gcv;
30   XWindowAttributes xgwa;
31   int border;
32   int root_p;
33   unsigned long fg;
34   Pixmap pixmap;
35   Drawable d;
36   Colormap cmap;
37   Visual *visual;
38
39   XGetWindowAttributes (dpy, window, &xgwa);
40   cmap = xgwa.colormap;
41   visual = xgwa.visual;
42
43   copy_default_colormap_contents (dpy, cmap, visual);
44
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");
54
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;
59
60   gcv.foreground = fg;
61   gcv.function = GXcopy;
62   gcv.subwindow_mode = IncludeInferiors;
63   gc = XCreateGC (dpy, window, GCForeground |GCFunction | GCSubwindowMode,
64                   &gcv);
65
66   pixmap = grab_screen_image (dpy, window, root_p);
67
68   XGetWindowAttributes (dpy, window, &xgwa);
69   bitmap_w = xgwa.width;
70   bitmap_h = xgwa.height;
71
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;
78
79   d = (pixmap ? pixmap : window);
80
81   if (border)
82     {
83       int i;
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);
88     }
89
90   if (xoff)
91     {
92       XFillRectangle (dpy, d, gc, 0, 0, xoff, bitmap_h);
93       XFillRectangle (dpy, d, gc, bitmap_w - xoff, 0, xoff, bitmap_h);
94     }
95   if (yoff)
96     {
97       XFillRectangle (dpy, d, gc, 0, 0, bitmap_w, yoff);
98       XFillRectangle (dpy, d, gc, 0, bitmap_h - yoff, bitmap_w, yoff);
99     }
100
101   if (pixmap) XClearWindow (dpy, window);
102   XSync (dpy, True);
103   if (delay2) usleep (delay2 * 2);
104  for (i = 0; i < grid_size; i += pix_inc)
105    {
106      XPoint points [3];
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);
114
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);
120
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);
126
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);
132
133      XSync (dpy, True);
134      if (delay) usleep (delay);
135    }
136
137   XFillRectangle (dpy, window, gc,
138                   xoff + grid_size * hole_x,
139                   yoff + grid_size * hole_y,
140                   grid_size, grid_size);
141 }
142
143 static void
144 slide1 (dpy, window)
145      Display *dpy;
146      Window window;
147 {
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;
151  do {
152    dir = random () % 4;
153    switch (dir)
154      {
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;
159      default: abort ();
160      }
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);
166
167  switch (dir)
168    {
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;
173    default: abort ();
174    }
175
176  if (dx == -1) hole_x -= (size - 1);
177  else if (dy == -1) hole_y -= (size - 1);
178
179  ix = x = xoff + (hole_x + dx) * grid_size;
180  iy = y = yoff + (hole_y + dy) * grid_size;
181  inc = pix_inc;
182  for (i = 0; i < grid_size; i += inc)
183    {
184      if (inc + i > grid_size)
185        inc = grid_size - i;
186      XCopyArea (dpy, window, window, gc, x, y, grid_size * w, grid_size * h,
187                 x - dx * inc, y - dy * inc);
188      x -= dx * inc;
189      y -= dy * inc;
190      switch (dir)
191        {
192        case 0: XFillRectangle (dpy, window, gc,
193                                ix, y + grid_size * h, grid_size * w, iy - y);
194          break;
195        case 1: XFillRectangle (dpy, window, gc, ix, iy, x - ix, grid_size * h);
196          break;
197        case 2: XFillRectangle (dpy, window, gc, ix, iy, grid_size * w, y - iy);
198          break;
199        case 3: XFillRectangle (dpy, window, gc,
200                                x + grid_size * w, iy, ix - x, grid_size * h);
201          break;
202        }
203
204      XSync (dpy, True);
205      if (delay) usleep (delay);
206    }
207  switch (dir)
208    {
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;
213    }
214 }
215
216 \f
217 char *progclass = "SlidePuzzle";
218
219 char *defaults [] = {
220   "SlidePuzzle.mappedWhenManaged:false",
221   "SlidePuzzle.dontClearWindow:  true",
222   "*background:                 black",
223   "*gridSize:                   70",
224   "*pixelIncrement:             10",
225   "*internalBorderWidth:        1",
226   "*delay:                      50000",
227   "*delay2:                     1000000",
228   0
229 };
230
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 },
237 };
238
239 int options_size = (sizeof (options) / sizeof (options[0]));
240
241 void
242 screenhack (dpy, window)
243      Display *dpy;
244      Window window;
245 {
246   init_slide (dpy, window);
247   while (1)
248     {
249       slide1 (dpy, window);
250       if (delay2) usleep (delay2);
251     }
252 }