ftp://ftp.uni-heidelberg.de/pub/X11/contrib/applications/xscreensaver-2.07.tar.gz
[xscreensaver] / hacks / slip.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * slip --- lots of blits.
3  */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)slip.c        4.00 97/01/01 xlockmore";
6 #endif
7
8 /* Copyright (c) 1992 by Scott Draves (spot@cs.cmu.edu)
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * Revision History:
23  * 12-May-97: jwz@netscape.com: turned into a standalone program.
24  * 01-Dec-95: Patched for VMS <joukj@alpha.chem.uva.nl>.
25  */
26
27 #ifdef STANDALONE
28 # define PROGCLASS                                      "Slip"
29 # define HACK_INIT                                      init_slip
30 # define HACK_DRAW                                      draw_slip
31 # define slip_opts                                      xlockmore_opts
32 # define DEFAULTS       "*count:                35    \n"                       \
33                                         "*cycles:               50    \n"                       \
34                                         "*delay:                50000 \n"                       \
35                                         "*ncolors:              200   \n"
36 # include "xlockmore.h"                         /* from the xscreensaver distribution */
37 #else  /* !STANDALONE */
38 # include "xlock.h"                                     /* from the xlockmore distribution */
39 #endif /* !STANDALONE */
40
41 ModeSpecOpt slip_opts = {
42   0, NULL, 0, NULL, NULL };
43
44 typedef struct {
45         int         width, height;
46         int         nblits_remaining;
47         int         blit_width, blit_height;
48         int         mode;
49         int         first_time;
50     int         backwards;
51 } slipstruct;
52 static slipstruct *slips = NULL;
53
54 static short
55 halfrandom(int mv)
56 {
57         static short lasthalf = 0;
58         unsigned long r;
59
60         if (lasthalf) {
61                 r = lasthalf;
62                 lasthalf = 0;
63         } else {
64                 r = LRAND();
65                 lasthalf = r >> 16;
66         }
67         return r % mv;
68 }
69
70 static int
71 erandom(int mv)
72 {
73         static int  stage = 0;
74         static unsigned long r;
75         int         res;
76
77         if (0 == stage) {
78                 r = LRAND();
79                 stage = 7;
80         }
81         res = r & 0xf;
82         r = r >> 4;
83         stage--;
84         if (res & 8)
85                 return res & mv;
86         else
87                 return -(res & mv);
88 }
89
90 static void
91 prepare_screen(ModeInfo * mi, slipstruct * s)
92 {
93
94         Display    *display = MI_DISPLAY(mi);
95         GC          gc = MI_GC(mi);
96         int         i, n, w = s->width / 20;
97         int         not_solid = halfrandom(10);
98
99 #ifdef STANDALONE                         /* jwz -- sometimes hack the desktop image! */
100         if (halfrandom(5) == 0)
101           {
102                 grab_screen_image(DefaultScreenOfDisplay (MI_DISPLAY(mi)),
103                                                   MI_WINDOW(mi));
104                 return;
105           }
106 #endif
107
108         s->backwards = LRAND() & 1;             /* jwz: go the other way sometimes */
109
110         if (s->first_time || !halfrandom(5)) {
111                 XClearWindow(display, MI_WINDOW(mi));
112                 n = 300;
113         } else {
114                 if (halfrandom(5))
115                         return;
116                 if (halfrandom(5))
117                         n = 100;
118                 else
119                         n = 2000;
120         }
121
122         if (MI_NPIXELS(mi) > 2)
123                 XSetForeground(display, gc, MI_PIXEL(mi, halfrandom(MI_NPIXELS(mi))));
124         else if (halfrandom(2))
125                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
126         else
127                 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
128
129         for (i = 0; i < n; i++) {
130                 if (not_solid)
131                         if (MI_NPIXELS(mi) > 2)
132                                 XSetForeground(display, gc, MI_PIXEL(mi, halfrandom(MI_NPIXELS(mi))));
133                         else if (halfrandom(2))
134                                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
135                         else
136                                 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
137                 XFillRectangle(display, MI_WINDOW(mi), gc,
138                                halfrandom(s->width - w),
139                                halfrandom(s->height - w),
140                                w, w);
141         }
142         s->first_time = 0;
143 }
144
145 static int
146 quantize(double d)
147 {
148         int         i = (int) floor(d);
149         double      f = d - i;
150
151         if ((LRAND() & 0xff) < f * 0xff)
152                 i++;
153         return i;
154 }
155
156 void
157 init_slip(ModeInfo * mi)
158 {
159         slipstruct *sp;
160
161         if (slips == NULL) {
162                 if ((slips = (slipstruct *) calloc(MI_NUM_SCREENS(mi),
163                                                sizeof (slipstruct))) == NULL)
164                         return;
165         }
166         sp = &slips[MI_SCREEN(mi)];
167
168         sp->width = MI_WIN_WIDTH(mi);
169         sp->height = MI_WIN_HEIGHT(mi);
170
171         sp->blit_width = sp->width / 25;
172         sp->blit_height = sp->height / 25;
173         sp->nblits_remaining = 0;
174         sp->mode = 0;
175         sp->first_time = 1;
176
177         /* no "NoExpose" events from XCopyArea wanted */
178         XSetGraphicsExposures(MI_DISPLAY(mi), MI_GC(mi), False);
179 }
180
181 void
182 draw_slip(ModeInfo * mi)
183 {
184         Display    *display = MI_DISPLAY(mi);
185         Window      window = MI_WINDOW(mi);
186         GC          gc = MI_GC(mi);
187         slipstruct *s = &slips[MI_SCREEN(mi)];
188         int         timer;
189
190         timer = MI_BATCHCOUNT(mi) * MI_CYCLES(mi);
191
192         while (timer--) {
193                 int         xi = halfrandom(s->width - s->blit_width);
194                 int         yi = halfrandom(s->height - s->blit_height);
195                 double      x, y, dx = 0, dy = 0, t, s1, s2;
196
197                 if (0 == s->nblits_remaining--) {
198                         static      lut[] =
199                         {0, 0, 0, 1, 1, 1, 2};
200
201                         prepare_screen(mi, s);
202                         s->nblits_remaining = MI_BATCHCOUNT(mi) *
203                                 (2000 + halfrandom(1000) + halfrandom(1000));
204                         if (s->mode == 2)
205                                 s->mode = halfrandom(2);
206                         else
207                                 s->mode = lut[halfrandom(7)];
208                 }
209                 x = (2 * xi + s->blit_width) / (double) s->width - 1;
210                 y = (2 * yi + s->blit_height) / (double) s->height - 1;
211
212                 /* (x,y) is in biunit square */
213                 switch (s->mode) {
214                         case 0:
215                                 dx = x;
216                                 dy = y;
217
218                                 if (dy < 0) {
219                                         dy += 0.04;
220                                         if (dy > 0)
221                                                 dy = 0.00;
222                                 }
223                                 if (dy > 0) {
224                                         dy -= 0.04;
225                                         if (dy < 0)
226                                                 dy = 0.00;
227                                 }
228                                 t = dx * dx + dy * dy + 1e-10;
229                                 s1 = 2 * dx * dx / t - 1;
230                                 s2 = 2 * dx * dy / t;
231                                 dx = s1 * 5;
232                                 dy = s2 * 5;
233                                 if (s->backwards) {     /* jwz: go the other way sometimes */
234                                         dx = -dx;
235                                         dy = -dy;
236                                 }
237                                 break;
238                         case 1:
239                                 dx = erandom(3);
240                                 dy = erandom(3);
241                                 break;
242                         case 2:
243                                 dx = x * 3;
244                                 dy = y * 3;
245                                 break;
246                 }
247                 {
248                         int         qx = xi + quantize(dx), qy = yi + quantize(dy);
249                         int         wrap;
250
251                         if (qx < 0 || qy < 0 ||
252                             qx >= s->width - s->blit_width ||
253                             qy >= s->height - s->blit_height)
254                                 continue;
255
256                         XCopyArea(display, window, window, gc, xi, yi,
257                                   s->blit_width, s->blit_height,
258                                   qx, qy);
259
260                         switch (s->mode) {
261                                 case 0:
262                                         /* wrap */
263                                         wrap = s->width - (2 * s->blit_width);
264                                         if (qx > wrap)
265                                                 XCopyArea(display, window, window, gc, qx, qy,
266                                                 s->blit_width, s->blit_height,
267                                                           qx - wrap, qy);
268
269                                         if (qx < 2 * s->blit_width)
270                                                 XCopyArea(display, window, window, gc, qx, qy,
271                                                 s->blit_width, s->blit_height,
272                                                           qx + wrap, qy);
273
274                                         wrap = s->height - (2 * s->blit_height);
275                                         if (qy > wrap)
276                                                 XCopyArea(display, window, window, gc, qx, qy,
277                                                 s->blit_width, s->blit_height,
278                                                           qx, qy - wrap);
279
280                                         if (qy < 2 * s->blit_height)
281                                                 XCopyArea(display, window, window, gc, qx, qy,
282                                                 s->blit_width, s->blit_height,
283                                                           qx, qy + wrap);
284                                         break;
285                                 case 1:
286                                 case 2:
287                                         break;
288                         }
289                 }
290         }
291 }
292
293 void
294 release_slip(ModeInfo * mi)
295 {
296         if (slips != NULL) {
297                 (void) free((void *) slips);
298                 slips = NULL;
299         }
300 }