http://www.jwz.org/xscreensaver/xscreensaver-5.12.tar.gz
[xscreensaver] / hacks / slip.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* slip --- lots of slipping blits */
3
4 #if 0
5 static const char sccsid[] = "@(#)slip.c        5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1992 by Scott Draves <spot@cs.cmu.edu>
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * 01-Nov-2000: Allocation checks
24  * 10-May-1997: Jamie Zawinski <jwz@jwz.org> compatible with xscreensaver
25  * 01-Dec-1995: Patched for VMS <joukj@hrem.stm.tudelft.nl>
26  */
27
28 #ifdef STANDALONE
29 # define MODE_slip
30 # define DEFAULTS       "*delay: 50000 \n" \
31                                         "*count: 35 \n" \
32                                         "*cycles: 50 \n" \
33                                         "*ncolors: 200 \n" \
34                                         "*fpsSolid:     true \n"
35
36 # define refresh_slip 0
37 # define slip_handle_event 0
38 # include "xlockmore.h"         /* in xscreensaver distribution */
39 #else /* STANDALONE */
40 # include "xlock.h"             /* in xlockmore distribution */
41 #endif /* STANDALONE */
42
43 #ifdef MODE_slip
44
45 ENTRYPOINT ModeSpecOpt slip_opts =
46 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
47
48 #ifdef USE_MODULES
49 ModStruct   slip_description =
50 {"slip", "init_slip", "draw_slip", "release_slip",
51  "init_slip", "init_slip", (char *) NULL, &slip_opts,
52  50000, 35, 50, 1, 64, 1.0, "",
53  "Shows slipping blits", 0, NULL};
54
55 #endif
56
57 typedef struct {
58         int         width, height;
59         int         nblits_remaining;
60         int         blit_width, blit_height;
61         int         mode;
62         int         first_time;
63         int         backwards;
64         short       lasthalf;
65         int         stage;
66         unsigned long r;
67     Bool image_loading_p;
68 } slipstruct;
69 static slipstruct *slips = (slipstruct *) NULL;
70
71 static short
72 halfrandom(slipstruct *sp, int mv)
73 {
74         unsigned long r;
75
76         if (sp->lasthalf) {
77                 r = sp->lasthalf;
78                 sp->lasthalf = 0;
79         } else {
80                 r = LRAND();
81                 sp->lasthalf = (short) (r >> 16);
82         }
83         return r % mv;
84 }
85
86 static int
87 erandom(slipstruct *sp, int mv)
88 {
89         int         res;
90
91         if (0 == sp->stage) {
92                 sp->r = LRAND();
93                 sp->stage = 7;
94         }
95         res = (int) (sp->r & 0xf);
96         sp->r = sp->r >> 4;
97         sp->stage--;
98         if (res & 8)
99                 return res & mv;
100         else
101                 return -(res & mv);
102 }
103
104 #ifdef STANDALONE
105 static void
106 image_loaded_cb (Screen *screen, Window w, Drawable d,
107                  const char *name, XRectangle *geom,
108                  void *closure)
109 {
110   ModeInfo *mi = (ModeInfo *) closure;
111   slipstruct *sp = &slips[MI_SCREEN(mi)];
112   Display *dpy = DisplayOfScreen (screen);
113   XCopyArea (dpy, d, w, mi->gc, 0, 0,
114              sp->width, sp->height, 0, 0);
115   XFreePixmap (dpy, d);
116   sp->image_loading_p = False;
117 }
118 #endif /* STANDALONE */
119
120 static void
121 prepare_screen(ModeInfo * mi, slipstruct * sp)
122 {
123
124         Display    *display = MI_DISPLAY(mi);
125         GC          gc = MI_GC(mi);
126         int         i, n, w = sp->width / 20;
127         int         not_solid = halfrandom(sp, 10);
128
129         sp->backwards = (int) (LRAND() & 1);    /* jwz: go the other way sometimes */
130
131         if (sp->first_time || !halfrandom(sp, 10)) {
132                 MI_CLEARWINDOW(mi);
133                 n = 300;
134         } else {
135                 if (halfrandom(sp, 5))
136                         return;
137                 if (halfrandom(sp, 5))
138                         n = 100;
139                 else
140                         n = 2000;
141         }
142
143         if (MI_NPIXELS(mi) > 2)
144                 XSetForeground(display, gc, MI_PIXEL(mi, halfrandom(sp, MI_NPIXELS(mi))));
145         else if (halfrandom(sp, 2))
146                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
147         else
148                 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
149
150         for (i = 0; i < n; i++) {
151                 int         ww = ((w / 2) + halfrandom(sp, MAX(w, 1)));
152
153                 if (not_solid) {
154                         if (MI_NPIXELS(mi) > 2)
155                                 XSetForeground(display, gc, MI_PIXEL(mi, halfrandom(sp, MI_NPIXELS(mi))));
156                         else if (halfrandom(sp, 2))
157                                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
158                         else
159                                 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
160                 }
161                 XFillRectangle(display, MI_WINDOW(mi), gc,
162                                halfrandom(sp, MAX(sp->width - ww, 1)),
163                                halfrandom(sp, MAX(sp->height - ww, 1)),
164                                ww, ww);
165         }
166         sp->first_time = 0;
167
168
169 #ifdef STANDALONE                         /* jwz -- sometimes hack the desktop image! */
170         if (!sp->image_loading_p &&
171         (1||halfrandom(sp, 2) == 0)) {
172       /* Load it into a pixmap so that the "Loading" message and checkerboard
173          don't show up on the window -- we keep running while the image is
174          in progress... */
175       Pixmap p = XCreatePixmap (MI_DISPLAY(mi), MI_WINDOW(mi),
176                                 sp->width, sp->height, mi->xgwa.depth);
177       sp->image_loading_p = True;
178       load_image_async (ScreenOfDisplay (MI_DISPLAY(mi), 0/*####MI_SCREEN(mi)*/),
179                         MI_WINDOW(mi), p, image_loaded_cb, mi);
180         }
181 #endif
182 }
183
184 static int
185 quantize(double d)
186 {
187         int         i = (int) floor(d);
188         double      f = d - i;
189
190         if ((LRAND() & 0xff) < f * 0xff)
191                 i++;
192         return i;
193 }
194
195 ENTRYPOINT void
196 reshape_slip (ModeInfo * mi, int w, int h)
197 {
198   slipstruct *sp = &slips[MI_SCREEN(mi)];
199   sp->width = w;
200   sp->height = h;
201   sp->blit_width = sp->width / 25;
202   sp->blit_height = sp->height / 25;
203
204   sp->mode = 0;
205   sp->nblits_remaining = 0;
206 }
207
208 ENTRYPOINT void
209 init_slip (ModeInfo * mi)
210 {
211         slipstruct *sp;
212
213         if (slips == NULL) {
214                 if ((slips = (slipstruct *) calloc(MI_NUM_SCREENS(mi),
215                                                sizeof (slipstruct))) == NULL)
216                         return;
217         }
218         sp = &slips[MI_SCREEN(mi)];
219
220         sp->nblits_remaining = 0;
221         sp->mode = 0;
222         sp->first_time = 1;
223
224         /* no "NoExpose" events from XCopyArea wanted */
225         XSetGraphicsExposures(MI_DISPLAY(mi), MI_GC(mi), False);
226
227     reshape_slip (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
228 }
229
230 ENTRYPOINT void
231 draw_slip (ModeInfo * mi)
232 {
233         Display    *display = MI_DISPLAY(mi);
234         Window      window = MI_WINDOW(mi);
235         GC          gc = MI_GC(mi);
236         int         timer;
237         slipstruct *sp;
238
239         if (slips == NULL)
240                 return;
241         sp = &slips[MI_SCREEN(mi)];
242
243         timer = MI_COUNT(mi) * MI_CYCLES(mi);
244
245         MI_IS_DRAWN(mi) = True;
246
247         while (timer--) {
248                 int         xi = halfrandom(sp, MAX(sp->width - sp->blit_width, 1));
249                 int         yi = halfrandom(sp, MAX(sp->height - sp->blit_height, 1));
250                 double      x, y, dx = 0, dy = 0, t, s1, s2;
251
252                 if (0 == sp->nblits_remaining--) {
253                         static const int lut[] = {0, 0, 0, 1, 1, 1, 2};
254
255                         prepare_screen(mi, sp);
256                         sp->nblits_remaining = MI_COUNT(mi) *
257                                 (2000 + halfrandom(sp, 1000) + halfrandom(sp, 1000));
258                         if (sp->mode == 2)
259                                 sp->mode = halfrandom(sp, 2);
260                         else
261                                 sp->mode = lut[halfrandom(sp, 7)];
262                 }
263                 x = (2 * xi + sp->blit_width) / (double) sp->width - 1;
264                 y = (2 * yi + sp->blit_height) / (double) sp->height - 1;
265
266                 /* (x,y) is in biunit square */
267                 switch (sp->mode) {
268                         case 0: /* rotor */
269                                 dx = x;
270                                 dy = y;
271
272                                 if (dy < 0) {
273                                         dy += 0.04;
274                                         if (dy > 0)
275                                                 dy = 0.00;
276                                 }
277                                 if (dy > 0) {
278                                         dy -= 0.04;
279                                         if (dy < 0)
280                                                 dy = 0.00;
281                                 }
282                                 t = dx * dx + dy * dy + 1e-10;
283                                 s1 = 2 * dx * dx / t - 1;
284                                 s2 = 2 * dx * dy / t;
285                                 dx = s1 * 5;
286                                 dy = s2 * 5;
287
288                                 if (sp->backwards) {    /* jwz: go the other way sometimes */
289                                         dx = -dx;
290                                         dy = -dy;
291                                 }
292                                 break;
293                         case 1: /* shuffle */
294                                 dx = erandom(sp, 3);
295                                 dy = erandom(sp, 3);
296                                 break;
297                         case 2: /* explode */
298                                 dx = x * 3;
299                                 dy = y * 3;
300                                 break;
301                 }
302                 {
303                         int         qx = xi + quantize(dx), qy = yi + quantize(dy);
304                         int         wrap;
305
306                         if (qx < 0 || qy < 0 ||
307                             qx >= sp->width - sp->blit_width ||
308                             qy >= sp->height - sp->blit_height)
309                                 continue;
310
311 /*-
312 Seems to cause problems using Exceed
313 with PseudoColor
314 X Error of failed request:  BadGC (invalid GC parameter)
315 with TrueColor
316 X Error of failed request:  BadDrawable (invalid Pixmap or Window parameter)
317   Major opcode of failed request:  62 (X_CopyArea)
318  */
319                         XCopyArea(display, window, window, gc, xi, yi,
320                                   sp->blit_width, sp->blit_height,
321                                   qx, qy);
322                         switch (sp->mode) {
323                                 case 0:
324                                         /* wrap */
325                                         wrap = sp->width - (2 * sp->blit_width);
326                                         if (qx > wrap ) {
327                                                 XCopyArea(display, window, window, gc, qx, qy,
328                                                 sp->blit_width, sp->blit_height,
329                                                           qx - wrap, qy);
330                                         }
331                                         if (qx < 2 * sp->blit_width) {
332                                                 XCopyArea(display, window, window, gc, qx, qy,
333                                                 sp->blit_width, sp->blit_height,
334                                                           qx + wrap, qy);
335                                         }
336                                         wrap = sp->height - (2 * sp->blit_height);
337                                         if (qy > wrap) {
338                                                 XCopyArea(display, window, window, gc, qx, qy,
339                                                 sp->blit_width, sp->blit_height,
340                                                           qx, qy - wrap);
341                                         }
342                                         if (qy < 2 * sp->blit_height) {
343                                                 XCopyArea(display, window, window, gc, qx, qy,
344                                                 sp->blit_width, sp->blit_height,
345                                                           qx, qy + wrap);
346                                         }
347                                         break;
348                                 case 1:
349                                 case 2:
350                                         break;
351                         }
352                 }
353         }
354 }
355
356 ENTRYPOINT void
357 release_slip (ModeInfo * mi)
358 {
359         if (slips != NULL) {
360                 (void) free((void *) slips);
361                 slips = (slipstruct *) NULL;
362         }
363 }
364
365 XSCREENSAVER_MODULE ("Slip", slip)
366
367 #endif /* MODE_slip */