ftp://updates.redhat.com/enterprise/2.1AS/en/os/SRPMS/xscreensaver-3.33-4.rhel21...
[xscreensaver] / utils / erase.c
1 /* erase.c: Erase the screen in various more or less interesting ways.
2  * Copyright (c) 1997-2001 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  *
12  * Portions (c) 1997 by Johannes Keukelaar <johannes@nada.kth.se>:
13  *   Permission to use in any way granted. Provided "as is" without expressed
14  *   or implied warranty. NO WARRANTY, NO EXPRESSION OF SUITABILITY FOR ANY
15  *   PURPOSE. (I.e.: Use in any way, but at your own risk!)
16  */
17
18 #include "utils.h"
19 #include "yarandom.h"
20 #include "usleep.h"
21 #include "resources.h"
22
23 extern char *progname;
24
25 #undef countof
26 #define countof(x) (sizeof(x)/sizeof(*(x)))
27
28 #define LITTLE_NAP 5000   /* 1/200th sec */
29
30 typedef void (*Eraser) (Display *dpy, Window window, GC gc,
31                         int width, int height, int total_msecs);
32
33
34 static unsigned long
35 millitime (void)
36 {
37   struct timeval tt;
38 # ifdef GETTIMEOFDAY_TWO_ARGS
39   struct timezone tz;
40   gettimeofday (&tt, &tz);
41 # else
42   gettimeofday (&tt);
43 # endif
44   return (tt.tv_sec * 1000) + (tt.tv_usec / 1000);
45 }
46
47
48 static void
49 random_lines (Display *dpy, Window window, GC gc,
50               int width, int height, int total_msecs)
51 {
52   int granularity = 50;
53
54   Bool horiz_p = (random() & 1);
55   int max = (horiz_p ? height : width);
56   int *lines = (int *) calloc(max, sizeof(*lines));
57   int oi = -1;
58   int i;
59   unsigned long start_tick = millitime();
60   unsigned long end_tick = start_tick + total_msecs;
61   unsigned long tick = start_tick;
62   int hits = 0;
63   int nonhits = 0;
64
65   for (i = 0; i < max; i++)
66     lines[i] = i;
67
68   for (i = 0; i < max; i++)
69     {
70       int t, r;
71       t = lines[i];
72       r = random() % max;
73       lines[i] = lines[r];
74       lines[r] = t;
75     }
76
77   while (tick < end_tick)
78     {
79       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
80
81       i /= granularity;
82
83       if (i == oi)
84         {
85           usleep (LITTLE_NAP);
86           nonhits++;
87         }
88       else
89         {
90           int j;
91           for (j = 0; j < granularity; j++)
92             {
93               int ii = i * granularity + j;
94               if (horiz_p)
95                 XDrawLine (dpy, window, gc, 0, lines[ii], width, lines[ii]);
96               else
97                 XDrawLine (dpy, window, gc, lines[ii], 0, lines[ii], height);
98               hits++;
99             }
100           XSync (dpy, False);
101         }
102
103       oi = i;
104       tick = millitime();
105     }
106
107   free(lines);
108 }
109
110
111 static void
112 venetian (Display *dpy, Window window, GC gc,
113           int width, int height, int total_msecs)
114 {
115   Bool horiz_p = (random() & 1);
116   Bool flip_p = (random() & 1);
117   int max = (horiz_p ? height : width);
118   int *lines = (int *) calloc(max, sizeof(*lines));
119   int i, j;
120   int oi = -1;
121
122   unsigned long start_tick = millitime();
123   unsigned long end_tick = (start_tick +
124                             (1.5 * total_msecs));  /* this one needs more */
125   unsigned long tick = start_tick;
126   int hits = 0;
127   int nonhits = 0;
128
129   j = 0;
130   for (i = 0; i < max*2; i++)
131     {
132       int line = ((i / 16) * 16) - ((i % 16) * 15);
133       if (line >= 0 && line < max)
134         lines[j++] = (flip_p ? max - line : line);
135     }
136
137   while (tick < end_tick)
138     {
139       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
140
141       if (i == oi)
142         {
143           usleep (LITTLE_NAP);
144           nonhits++;
145         }
146       else
147         {
148           int k;
149           for (k = oi; k <= i; k++)
150             {
151               if (horiz_p)
152                 XDrawLine(dpy,window, gc, 0, lines[k], width, lines[k]);
153               else
154                 XDrawLine(dpy,window, gc, lines[k], 0, lines[k], height);
155               hits++;
156             }
157           XSync (dpy, False);
158         }
159
160       oi = i;
161       tick = millitime();
162     }
163   free(lines);
164 }
165
166
167 static void
168 triple_wipe (Display *dpy, Window window, GC gc,
169              int width, int height, int total_msecs)
170 {
171   Bool flip_x = random() & 1;
172   Bool flip_y = random() & 1;
173   int max = width + (height / 2);
174   int *lines = (int *)calloc(max, sizeof(int));
175   int i;
176   int oi = -1;
177
178   unsigned long start_tick = millitime();
179   unsigned long end_tick = start_tick + total_msecs;
180   unsigned long tick = start_tick;
181   int hits = 0;
182   int nonhits = 0;
183
184   for(i = 0; i < width/2; i++)
185     lines[i] = i*2+height;
186   for(i = 0; i < height/2; i++)
187     lines[i+width/2] = i*2;
188   for(i = 0; i < width/2; i++)
189     lines[i+width/2+height/2] = width-i*2-(width%2?0:1)+height;
190
191   while (tick < end_tick)
192     {
193       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
194       int x, y, x2, y2;
195
196       if (i == oi)
197         {
198           usleep (LITTLE_NAP);
199           nonhits++;
200         }
201       else
202         {
203           int k;
204           for (k = oi; k <= i; k++)
205             {
206               if (lines[k] < height)
207                 x = 0, y = lines[k], x2 = width, y2 = y;
208               else
209                 x = lines[k]-height, y = 0, x2 = x, y2 = height;
210
211               if (flip_x)
212                 x = width-x, x2 = width-x2;
213               if (flip_y)
214                 y = height-y, y2 = height-y2;
215
216               XDrawLine (dpy, window, gc, x, y, x2, y2);
217               hits++;
218             }
219           XSync (dpy, False);
220         }
221
222       oi = i;
223       tick = millitime();
224     }
225   free(lines);
226 }
227
228
229 static void
230 quad_wipe (Display *dpy, Window window, GC gc,
231            int width, int height, int total_msecs)
232 {
233   Bool flip_x = random() & 1;
234   Bool flip_y = random() & 1;
235   int max = width + height;
236   int *lines = (int *)calloc(max, sizeof(int));
237   int i;
238   int oi = -1;
239
240   unsigned long start_tick = millitime();
241   unsigned long end_tick = start_tick + total_msecs;
242   unsigned long tick = start_tick;
243   int hits = 0;
244   int nonhits = 0;
245
246   for (i = 0; i < max/4; i++)
247     {
248       lines[i*4]   = i*2;
249       lines[i*4+1] = height-i*2-(height%2?0:1);
250       lines[i*4+2] = height+i*2;
251       lines[i*4+3] = height+width-i*2-(width%2?0:1);
252     }
253
254   while (tick < end_tick)
255     {
256       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
257
258       if (i == oi)
259         {
260           usleep (LITTLE_NAP);
261           nonhits++;
262         }
263       else
264         {
265           int k;
266           for (k = oi; k <= i; k++)
267             {
268               int x, y, x2, y2;
269               if (lines[k] < height)
270                 x = 0, y = lines[k], x2 = width, y2 = y;
271               else
272                 x = lines[k]-height, y = 0, x2 = x, y2 = height;
273
274               if (flip_x)
275                 x = width-x, x2 = width-x2;
276               if (flip_y)
277                 y = height-y, y2 = height-y2;
278
279               XDrawLine (dpy, window, gc, x, y, x2, y2);
280               hits++;
281             }
282           XSync (dpy, False);
283         }
284
285       oi = i;
286       tick = millitime();
287     }
288
289   free(lines);
290 }
291
292
293
294 static void
295 circle_wipe (Display *dpy, Window window, GC gc,
296              int width, int height, int total_msecs)
297 {
298   int max = 360 * 64;
299   int start = random() % max;
300   int rad = (width > height ? width : height);
301   int flip_p = random() & 1;
302   int oth;
303
304   unsigned long start_tick = millitime();
305   unsigned long end_tick = start_tick + total_msecs;
306   unsigned long tick = start_tick;
307
308   int hits = 0;
309   int nonhits = 0;
310
311   oth = (flip_p ? max : 0);
312   while (tick < end_tick)
313     {
314       int th = (max * (tick - start_tick)) / (end_tick - start_tick);
315       if (flip_p)
316         th = (360 * 64) - th;
317
318       if (th == oth)
319         {
320           usleep (LITTLE_NAP);
321           nonhits++;
322         }
323       else
324         {
325           XFillArc(dpy, window, gc,
326                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
327                    (start+oth)%(360*64),
328                    (th-oth));
329           hits++;
330           XSync (dpy, False);
331         }
332
333       oth = th;
334       tick = millitime();
335     }
336 }
337
338
339 static void
340 three_circle_wipe (Display *dpy, Window window, GC gc,
341                    int width, int height, int total_msecs)
342 {
343   int max = (360 * 64) / 6;
344   int start = random() % max;
345   int rad = (width > height ? width : height);
346   int oth;
347
348   unsigned long start_tick = millitime();
349   unsigned long end_tick = start_tick + total_msecs;
350   unsigned long tick = start_tick;
351
352   int hits = 0;
353   int nonhits = 0;
354
355   oth = 0;
356   while (tick < end_tick)
357     {
358       int th = (max * (tick - start_tick)) / (end_tick - start_tick);
359
360       if (th == oth)
361         {
362           usleep (LITTLE_NAP);
363           nonhits++;
364         }
365       else
366         {
367           int off = 0;
368           XFillArc(dpy, window, gc,
369                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
370                    (start+off+oth)%(360*64),
371                    (th-oth));
372           XFillArc(dpy, window, gc,
373                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
374                    ((start+off-oth))%(360*64),
375                    -(th-oth));
376
377           off += max + max;
378           XFillArc(dpy, window, gc,
379                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
380                    (start+off+oth)%(360*64),
381                    (th-oth));
382           XFillArc(dpy, window, gc,
383                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
384                    ((start+off-oth))%(360*64),
385                    -(th-oth));
386
387           off += max + max;
388           XFillArc(dpy, window, gc,
389                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
390                    (start+off+oth)%(360*64),
391                    (th-oth));
392           XFillArc(dpy, window, gc,
393                    (width/2)-rad, (height/2)-rad, rad*2, rad*2,
394                    ((start+off-oth))%(360*64),
395                    -(th-oth));
396
397           hits++;
398           XSync (dpy, False);
399         }
400
401       oth = th;
402       tick = millitime();
403     }
404 }
405
406
407 static void
408 squaretate (Display *dpy, Window window, GC gc,
409             int width, int height, int total_msecs)
410 {
411   int max = ((width > height ? width : width) * 2);
412   int oi = -1;
413   Bool flip = random() & 1;
414
415   unsigned long start_tick = millitime();
416   unsigned long end_tick = start_tick + total_msecs;
417   unsigned long tick = start_tick;
418   int hits = 0;
419   int nonhits = 0;
420
421 #define DRAW() \
422       if (flip) { \
423         points[0].x = width-points[0].x; \
424         points[1].x = width-points[1].x; \
425         points[2].x = width-points[2].x; } \
426       XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin)
427
428   while (tick < end_tick)
429     {
430       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
431
432       if (i == oi)
433         {
434           usleep (LITTLE_NAP);
435           nonhits++;
436         }
437       else
438         {
439           XPoint points [3];
440           points[0].x = 0;
441           points[0].y = 0;
442           points[1].x = width;
443           points[1].y = 0;
444           points[2].x = 0;
445           points[2].y = points[0].y + ((i * height) / max);
446           DRAW();
447
448           points[0].x = 0;
449           points[0].y = 0;
450           points[1].x = 0;
451           points[1].y = height;
452           points[2].x = ((i * width) / max);
453           points[2].y = height;
454           DRAW();
455
456           points[0].x = width;
457           points[0].y = height;
458           points[1].x = 0;
459           points[1].y = height;
460           points[2].x = width;
461           points[2].y = height - ((i * height) / max);
462           DRAW();
463
464           points[0].x = width;
465           points[0].y = height;
466           points[1].x = width;
467           points[1].y = 0;
468           points[2].x = width - ((i * width) / max);
469           points[2].y = 0;
470           DRAW();
471           hits++;
472           XSync (dpy, True);
473         }
474
475       oi = i;
476       tick = millitime();
477     }
478 #undef DRAW
479 }
480
481
482 /* from Frederick Roeber <roeber@netscape.com> */
483 static void
484 fizzle (Display *dpy, Window window, GC gc,
485         int width, int height, int total_msecs)
486 {
487   /* These dimensions must be prime numbers.  They should be roughly the
488      square root of the width and height. */
489 # define BX 41
490 # define BY 31
491 # define SIZE (BX*BY)
492
493   int array[SIZE];
494   int i, j;
495   int oi = -1;
496   XPoint *skews;
497   XPoint points[250];
498   int npoints = 0;
499   int nx, ny;
500
501   unsigned long start_tick = millitime();
502   unsigned long end_tick = (start_tick +
503                             (2.5 * total_msecs));  /* this one needs more */
504   unsigned long tick = start_tick;
505   int hits = 0;
506   int nonhits = 0;
507
508   /* Distribute the numbers [0,SIZE) randomly in the array */
509   {
510     int indices[SIZE];
511
512     for( i = 0; i < SIZE; i++ ) {
513       array[i] = -1;
514       indices[i] = i;
515     } 
516
517     for( i = 0; i < SIZE; i++ ) {
518       j = random()%(SIZE-i);
519       array[indices[j]] = i;
520       indices[j] = indices[SIZE-i-1];
521     }
522   }
523
524   /* nx, ny are the number of cells across and down, rounded up */
525   nx = width  / BX + (0 == (width %BX) ? 0 : 1);
526   ny = height / BY + (0 == (height%BY) ? 0 : 1);
527   skews = (XPoint *)malloc(sizeof(XPoint) * (nx*ny));
528   if( (XPoint *)0 != skews ) {
529     for( i = 0; i < nx; i++ ) {
530       for( j = 0; j < ny; j++ ) {
531         skews[j * nx + i].x = random()%BX;
532         skews[j * nx + i].y = random()%BY;
533       }
534     }
535   }
536
537 # define SKEWX(cx, cy) (((XPoint *)0 == skews)?0:skews[cy*nx + cx].x)
538 # define SKEWY(cx, cy) (((XPoint *)0 == skews)?0:skews[cy*nx + cx].y)
539
540   while (tick < end_tick)
541     {
542       int i = (SIZE * (tick - start_tick)) / (end_tick - start_tick);
543
544       if (i == oi)
545         {
546           usleep (LITTLE_NAP);
547           nonhits++;
548         }
549       else
550         {
551           int j;
552           for (j = oi; j < i; j++)
553             {
554               int x = array[j] % BX;
555               int y = array[j] / BX;
556               int iy, cy;
557               for (iy = 0, cy = 0; iy < height; iy += BY, cy++)
558                 {
559                   int ix, cx;
560                   for( ix = 0, cx = 0; ix < width; ix += BX, cx++ ) {
561                     int xx = ix + (SKEWX(cx, cy) + x*((cx%(BX-1))+1))%BX;
562                     int yy = iy + (SKEWY(cx, cy) + y*((cy%(BY-1))+1))%BY;
563                     if (xx < width && yy < height)
564                       {
565                         points[npoints].x = xx;
566                         points[npoints].y = yy;
567                         if (++npoints == countof(points))
568                           {
569                             XDrawPoints(dpy, window, gc, points, npoints,
570                                         CoordModeOrigin);
571                             XSync (dpy, False);
572                             npoints = 0;
573                           }
574                       }
575                   }
576                 }
577             }
578           hits++;
579         }
580
581       oi = i;
582       tick = millitime();
583     }
584
585   if (npoints > 100)
586     {
587       XDrawPoints(dpy, window, gc, points, npoints, CoordModeOrigin);
588       XSync (dpy, False);
589       usleep (10000);
590     }
591
592 # undef SKEWX
593 # undef SKEWY
594 # undef BX
595 # undef BY
596 # undef SIZE
597
598   if (skews) free(skews);
599 }
600
601
602 /* from Rick Campbell <rick@campbellcentral.org> */
603 static void
604 spiral (Display *dpy, Window window, GC context,
605         int width, int height, int total_msecs)
606 {
607   int granularity = 1; /* #### */
608
609   double pi2 = (M_PI + M_PI);
610   int loop_count = 10;
611   int angle_step = 1000 / 8;  /* disc granularity is 8 degrees */
612   int max = pi2 * angle_step;
613   double angle;
614   int arc_limit;
615   int arc_max_limit;
616   int length_step;
617   XPoint points [3];
618
619   total_msecs *= 2.5;  /* this one needs more */
620
621   angle = 0.0;
622   arc_limit = 1;
623   arc_max_limit = (ceil (sqrt ((width * width) + (height * height))) / 2.0);
624   length_step = ((arc_max_limit + loop_count - 1) / loop_count);
625   arc_max_limit += length_step;
626   points [0].x = width / 2;
627   points [0].y = height / 2;
628   points [1].x = points [0].x + length_step;
629   points [1].y = points [0].y;
630   points [2].x = points [1].x;
631   points [2].y = points [1].y;
632
633   for (arc_limit = length_step;
634        arc_limit < arc_max_limit;
635        arc_limit += length_step)
636     {
637       int arc_length = length_step;
638       int length_base = arc_limit;
639
640       unsigned long start_tick = millitime();
641       unsigned long end_tick = start_tick + (total_msecs /
642                                              (arc_max_limit / length_step));
643       unsigned long tick = start_tick;
644       int hits = 0;
645       int nonhits = 0;
646       int i = 0;
647       int oi = -1;
648
649 #if 0
650       int max2 = max / granularity;
651       while (i < max2)
652 #else
653       while (tick < end_tick)
654 #endif
655         {
656           i = (max * (tick - start_tick)) / (end_tick - start_tick);
657           if (i > max) i = max;
658
659           i /= granularity;
660
661           if (i == oi)
662             {
663               usleep (LITTLE_NAP);
664               nonhits++;
665             }
666           else
667             {
668               int j, k;
669 #if 0
670               for (k = oi; k <= i; k++)
671 #else
672               k = i;
673 #endif
674                 {
675                   for (j = 0; j < granularity; j++)
676                     {
677                       int ii = k * granularity + j;
678                       angle = ii / (double) angle_step;
679                       arc_length = length_base + ((length_step * angle) / pi2);
680                       points [1].x = points [2].x;
681                       points [1].y = points [2].y;
682                       points [2].x = points [0].x +
683                         (int)(cos(angle) * arc_length);
684                       points [2].y = points [0].y +
685                         (int)(sin(angle) * arc_length);
686                       XFillPolygon (dpy, window, context, points, 3, Convex,
687                                     CoordModeOrigin);
688                       hits++;
689                     }
690                 }
691               XSync (dpy, False);
692             }
693
694           oi = i;
695           tick = millitime();
696         }
697     }
698 }
699
700
701 #undef MAX
702 #undef MIN
703 #define MAX(a,b) ((a)>(b)?(a):(b))
704 #define MIN(a,b) ((a)<(b)?(a):(b))
705
706 /* from David Bagley <bagleyd@tux.org> */
707 static void
708 random_squares(Display * dpy, Window window, GC gc,
709                int width, int height, int total_msecs)
710 {
711   int granularity = 20;
712
713   int randsize = MAX(1, MIN(width, height) / (16 + (random() % 32)));
714   int max = (height / randsize + 1) * (width / randsize + 1);
715   int *squares = (int *) calloc(max, sizeof (*squares));
716   int i;
717   int columns = width / randsize + 1;  /* Add an extra for roundoff */
718
719   int oi = -1;
720   unsigned long start_tick = millitime();
721   unsigned long end_tick = start_tick + total_msecs;
722   unsigned long tick = start_tick;
723   int hits = 0;
724   int nonhits = 0;
725
726   for (i = 0; i < max; i++)
727     squares[i] = i;
728
729   for (i = 0; i < max; i++)
730     {
731       int t, r;
732       t = squares[i];
733       r = random() % max;
734       squares[i] = squares[r];
735       squares[r] = t;
736     }
737
738   while (tick < end_tick)
739     {
740       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
741
742       i /= granularity;
743
744       if (i == oi)
745         {
746           usleep (LITTLE_NAP);
747           nonhits++;
748         }
749       else
750         {
751           int j;
752           for (j = 0; j < granularity; j++)
753             {
754               int ii = i * granularity + j;
755
756               XFillRectangle(dpy, window, gc,
757                              (squares[ii] % columns) * randsize,
758                              (squares[ii] / columns) * randsize,
759                              randsize, randsize);
760               hits++;
761             }
762         }
763       XSync (dpy, False);
764
765       oi = i;
766       tick = millitime();
767     }
768   free(squares);
769 }
770
771 /* I first saw something like this, albeit in reverse, in an early Tetris
772    implementation for the Mac.
773     -- Torbjörn Andersson <torbjorn@dev.eurotime.se>
774  */
775 static void
776 slide_lines (Display *dpy, Window window, GC gc,
777              int width, int height, int total_msecs)
778 {
779   int max = width;
780   int dy = MAX (10, height/40);
781
782   int oi = 0;
783   unsigned long start_tick = millitime();
784   unsigned long end_tick = start_tick + total_msecs;
785   unsigned long tick = start_tick;
786   int hits = 0;
787   int nonhits = 0;
788
789   while (tick < end_tick)
790     {
791       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
792
793       if (i == oi)
794         {
795           usleep (LITTLE_NAP);
796           nonhits++;
797         }
798       else
799         {
800           int y;
801           int tick = 0;
802           int from1 = oi;
803           int to1 = i;
804           int w = width-to1;
805           int from2 = width - oi - w;
806           int to2 = width - i - w;
807
808           for (y = 0; y < height; y += dy)
809             {
810               if (++tick & 1)
811                 {
812                   XCopyArea (dpy, window, window, gc, from1, y, w, dy, to1, y);
813                   XFillRectangle (dpy, window, gc, from1, y, to1-from1, dy);
814                 }
815               else
816                 {
817                   XCopyArea (dpy, window, window, gc, from2, y, w, dy, to2, y);
818                   XFillRectangle (dpy, window, gc, from2+w, y, to2-from2, dy);
819                 }
820             }
821
822           hits++;
823           XSync (dpy, False);
824         }
825
826       oi = i;
827       tick = millitime();
828     }
829 }
830
831
832 /* from Frederick Roeber <roeber@xigo.com> */
833 static void
834 losira (Display * dpy, Window window, GC gc,
835         int width, int height, int total_msecs)
836 {
837   XGCValues gcv;
838   XWindowAttributes wa;
839   XColor white;
840   GC white_gc; 
841   XArc arc[2][8];
842   double xx[8], yy[8], dx[8], dy[8];
843
844   int i;
845   int oi = 0;
846
847   int max = width/2;
848   int max_off = MAX(1, max / 12);
849
850   int msecs1 = (0.55 * total_msecs);
851   int msecs2 = (0.30 * total_msecs);
852   int msecs3 = (0.15 * total_msecs);
853
854   unsigned long start_tick = millitime();
855   unsigned long end_tick = start_tick + msecs1;
856   unsigned long tick = start_tick;
857   int hits = 0;
858   int nonhits = 0;
859
860   XGetWindowAttributes(dpy, window, &wa);
861   white.flags = DoRed|DoGreen|DoBlue;
862   white.red = white.green = white.blue = 65535;
863   XAllocColor(dpy, wa.colormap, &white);
864   gcv.foreground = white.pixel;
865   white_gc = XCreateGC(dpy, window, GCForeground, &gcv);
866
867   /* Squeeze in from the sides */
868   while (tick < end_tick)
869     {
870       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
871
872       if (i == oi)
873         {
874           usleep (LITTLE_NAP);
875           nonhits++;
876         }
877       else
878         {
879           int off = (max_off * (tick - start_tick)) / (end_tick - start_tick);
880
881           int from1 = oi;
882           int to1 = i;
883           int w = max - to1 - off/2 + 1;
884           int from2 = max+(to1-from1)+off/2;
885           int to2 = max+off/2;
886
887           if (w < 0)
888             break;
889
890           XCopyArea (dpy, window, window, gc, from1, 0, w, height, to1, 0);
891           XCopyArea (dpy, window, window, gc, from2, 0, w, height, to2, 0);
892           XFillRectangle (dpy, window, gc, from1, 0, (to1-from1), height);
893           XFillRectangle (dpy, window, gc, to2+w, 0, from2+w,     height);
894           XFillRectangle (dpy, window, white_gc, max-off/2, 0, off, height);
895           hits++;
896           XSync(dpy, False);
897         }
898
899       oi = i;
900       tick = millitime();
901     }
902
903
904   XFillRectangle(dpy, window, white_gc, max-max_off/2, 0, max_off, height);
905
906   /* Cap the top and bottom of the line */
907   XFillRectangle(dpy, window, gc, max-max_off/2, 0, max_off, max_off/2);
908   XFillRectangle(dpy, window, gc, max-max_off/2, height-max_off/2,
909                  max_off, max_off/2);
910   XFillArc(dpy, window, white_gc, max-max_off/2-1, 0,
911            max_off-1, max_off-1, 0, 180*64);
912   XFillArc(dpy, window, white_gc, max-max_off/2-1, height-max_off,
913            max_off-1, max_off-1,
914            180*64, 360*64);
915
916   XFillRectangle(dpy, window, gc, 0,               0, max-max_off/2, height);
917   XFillRectangle(dpy, window, gc, max+max_off/2-1, 0, max-max_off/2, height);
918   XSync(dpy, False);
919
920   /* Collapse vertically */
921   start_tick = millitime();
922   end_tick = start_tick + msecs2;
923   tick = start_tick;
924
925   max = height/2;
926   oi = 0;
927   while (tick < end_tick)
928     {
929       int i = (max * (tick - start_tick)) / (end_tick - start_tick);
930       int x = (width-max_off)/2;
931       int w = max_off;
932
933       if (i == oi)
934         {
935           usleep (LITTLE_NAP);
936           nonhits++;
937         }
938       else
939         {
940           int off = (max_off * (tick - start_tick)) / (end_tick - start_tick);
941
942           int from1 = oi;
943           int to1 = i;
944           int h = max - to1 - off/2;
945           int from2 = max+(to1-from1)+off/2;
946           int to2 = max+off/2;
947
948           if (h < max_off/2)
949             break;
950
951           XCopyArea (dpy, window, window, gc, x, from1, w, h, x, to1);
952           XCopyArea (dpy, window, window, gc, x, from2, w, h, x, to2);
953           XFillRectangle(dpy, window, gc, x, from1, w, (to1 - from1));
954           XFillRectangle(dpy, window, gc, x, to2+h, w, (to2 - from2));
955           hits++;
956           XSync(dpy, False);
957         }
958
959       oi = i;
960       tick = millitime();
961     }
962
963   /* "This is Sci-Fi" */
964   for( i = 0; i < 8; i++ ) {
965     arc[0][i].width = arc[0][i].height = max_off;
966     arc[1][i].width = arc[1][i].height = max_off;
967     arc[0][i].x = arc[1][i].x = width/2;
968     arc[0][i].y = arc[1][i].y = height/2;
969     xx[i] = (double)(width/2)  - max_off/2;
970     yy[i] = (double)(height/2) - max_off/2;
971   }
972
973   arc[0][0].angle1 = arc[1][0].angle1 =   0*64; arc[0][0].angle2 = arc[1][0].angle2 =  45*64;
974   arc[0][1].angle1 = arc[1][1].angle1 =  45*64; arc[0][1].angle2 = arc[1][1].angle2 =  45*64;
975   arc[0][2].angle1 = arc[1][2].angle1 =  90*64; arc[0][2].angle2 = arc[1][2].angle2 =  45*64;
976   arc[0][3].angle1 = arc[1][3].angle1 = 135*64; arc[0][3].angle2 = arc[1][3].angle2 =  45*64;
977   arc[0][4].angle1 = arc[1][4].angle1 = 180*64; arc[0][4].angle2 = arc[1][4].angle2 =  45*64;
978   arc[0][5].angle1 = arc[1][5].angle1 = 225*64; arc[0][5].angle2 = arc[1][5].angle2 =  45*64;
979   arc[0][6].angle1 = arc[1][6].angle1 = 270*64; arc[0][6].angle2 = arc[1][6].angle2 =  45*64;
980   arc[0][7].angle1 = arc[1][7].angle1 = 315*64; arc[0][7].angle2 = arc[1][7].angle2 =  45*64;
981   
982   for( i = 0; i < 8; i++ ) {
983     dx[i] =  cos((i*45 + 22.5)/360 * 2*M_PI);
984     dy[i] = -sin((i*45 + 22.5)/360 * 2*M_PI);
985   }
986
987   gcv.line_width = 3;  
988   XChangeGC(dpy, gc, GCLineWidth, &gcv);
989
990   XClearWindow (dpy, window);
991   XFillArc(dpy, window, white_gc,
992            width/2-max_off/2-1, height/2-max_off/2-1,
993            max_off-1, max_off-1,
994            0, 360*64);
995   XDrawLine(dpy, window, gc, 0, height/2-1, width, height/2-1);
996   XDrawLine(dpy, window, gc, width/2-1, 0, width/2-1, height);
997   XDrawLine(dpy, window, gc, width/2-1-max_off, height/2-1-max_off,
998             width/2+max_off, height/2+max_off);
999   XDrawLine(dpy, window, gc, width/2+max_off, height/2-1-max_off,
1000             width/2-1-max_off, height/2+max_off);
1001
1002   XSync(dpy, False);
1003
1004
1005   /* Fan out */
1006   start_tick = millitime();
1007   end_tick = start_tick + msecs3;
1008   tick = start_tick;
1009   oi = 0;
1010   while (tick < end_tick)
1011     {
1012       int i = (max_off * (tick - start_tick)) / (end_tick - start_tick);
1013
1014       if (i == oi)
1015         {
1016           usleep (LITTLE_NAP);
1017           nonhits++;
1018         }
1019       else
1020         {
1021           int j;
1022           for (j = 0; j < 8; j++)
1023             {
1024               xx[j] += 2*dx[j];
1025               yy[j] += 2*dy[j];
1026               arc[(i+1)%2][j].x = xx[j];
1027               arc[(i+1)%2][j].y = yy[j];
1028             }
1029
1030           XFillRectangle (dpy, window, gc,
1031                           (width-max_off*5)/2, (height-max_off*5)/2,
1032                           max_off*5, max_off*5);
1033           XFillArcs(dpy, window, white_gc, arc[(i+1)%2], 8);
1034           XSync(dpy, False);
1035           hits++;
1036         }
1037
1038       oi = i;
1039       tick = millitime();
1040     }
1041   
1042   XSync (dpy, False);
1043
1044   /*XFreeColors(dpy, wa.colormap, &white.pixel, 1, 0);*/
1045   XFreeGC(dpy, white_gc);
1046 }
1047
1048
1049
1050 static Eraser erasers[] = {
1051   random_lines,
1052   venetian,
1053   triple_wipe,
1054   quad_wipe,
1055   circle_wipe,
1056   three_circle_wipe,
1057   squaretate,
1058   fizzle,
1059   spiral,
1060   random_squares,
1061   slide_lines,
1062   losira,
1063 };
1064
1065
1066 static void
1067 erase_window (Display *dpy, Window window, GC gc,
1068               int width, int height, int mode, int total_msecs)
1069 {
1070   Bool verbose_p = False;
1071   unsigned long start = millitime();
1072
1073   if (mode < 0 || mode >= countof(erasers))
1074     mode = random() % countof(erasers);
1075
1076   (*(erasers[mode])) (dpy, window, gc, width, height, total_msecs);
1077
1078   if (verbose_p)
1079     fprintf(stderr, "%s: eraser %d time: %4.2f sec\n",
1080             progname, mode, (millitime() - start) / 1000.0);
1081
1082   XClearWindow (dpy, window);
1083   XSync(dpy, False);
1084   usleep (333333);  /* 1/3 sec */
1085 }
1086
1087
1088 void
1089 erase_full_window(Display *dpy, Window window)
1090 {
1091   XWindowAttributes xgwa;
1092   XGCValues gcv;
1093   GC erase_gc;
1094   XColor black;
1095   int erase_msecs, erase_mode;
1096   char *s;
1097
1098   s = get_string_resource("eraseSeconds", "Integer");
1099   if (s && *s)
1100     erase_msecs = 1000 * get_float_resource("eraseSeconds", "Float");
1101   else
1102     erase_msecs = 1000;
1103
1104   if (erase_msecs < 10 || erase_msecs > 10000)
1105     erase_msecs = 1000;
1106
1107   if (s) free(s);
1108
1109   s = get_string_resource("eraseMode", "Integer");
1110   if (s && *s)
1111     erase_mode = get_integer_resource("eraseMode", "Integer");
1112   else
1113     erase_mode = -1;
1114   if (s) free(s);
1115
1116   XGetWindowAttributes (dpy, window, &xgwa);
1117   black.flags = DoRed|DoGreen|DoBlue;
1118   black.red = black.green = black.blue = 0;
1119   XAllocColor(dpy, xgwa.colormap, &black);
1120   gcv.foreground = black.pixel;
1121   erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1122   erase_window (dpy, window, erase_gc, xgwa.width, xgwa.height,
1123                 erase_mode, erase_msecs);
1124   XFreeColors(dpy, xgwa.colormap, &black.pixel, 1, 0);
1125   XFreeGC(dpy, erase_gc);
1126 }