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