ftp://ftp.uni-heidelberg.de/pub/X11/contrib/applications/xscreensaver-2.07.tar.gz
[xscreensaver] / hacks / decayscreen.c
1 /* xscreensaver, Copyright (c) 1992, 1993, 1994, 1996, 1997 
2  * Jamie Zawinski <jwz@netscape.com>
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
13 /* decayscreen
14  *
15  * Based on slidescreen program from the xscreensaver application and the
16  * decay program for Sun framebuffers.  This is the comment from the decay.c
17  * file:
18
19  * decay.c
20  *   find the screen bitmap for the console and make it "decay" by
21  *   randomly shifting random rectangles by one pixelwidth at a time.
22  *
23  *   by David Wald, 1988
24  *        rewritten by Natuerlich!
25  *   based on a similar "utility" on the Apollo ring at Yale.
26
27  * X version by
28  *
29  *  Vivek Khera <khera@cs.duke.edu>
30  *  5-AUG-1993
31  */
32
33 #include "screenhack.h"
34
35 static int sizex, sizey;
36 static int delay;
37 static GC gc;
38
39 static void
40 init_decay (Display *dpy, Window window)
41 {
42   XGCValues gcv;
43   XWindowAttributes xgwa;
44
45   delay = get_integer_resource ("delay", "Integer");
46
47   if (delay < 0) delay = 0;
48
49   gcv.function = GXcopy;
50   gcv.subwindow_mode = IncludeInferiors;
51   gc = XCreateGC (dpy, window, GCForeground |GCFunction | GCSubwindowMode,
52                   &gcv);
53
54   XGetWindowAttributes (dpy, window, &xgwa);
55   sizex = xgwa.width;
56   sizey = xgwa.height;
57
58   grab_screen_image (xgwa.screen, window);
59 }
60
61
62 /*
63  * perform one iteration of decay
64  */
65 static void
66 decay1 (Display *dpy, Window window)
67 {
68     int left, top, width, height;
69
70 #define nrnd(x) (random() % (x))
71
72     switch (random() % 8) {
73       case 0:                   /* move a block left */
74       case 1:
75         left = nrnd(sizex - 1) + 1;
76         top = nrnd(sizey);
77         width = nrnd(sizex - left);
78         height = nrnd(sizey - top);
79         XCopyArea (dpy, window, window, gc, left, top, width, height,
80                    left - 1, top);
81         break;
82       case 2:                   /* move a block right */
83       case 3:
84         left = nrnd(sizex - 1);
85         top = nrnd(sizey);
86         width = nrnd(sizex - 1 - left);
87         height = nrnd(sizey - top);
88         XCopyArea (dpy, window, window, gc, left, top, width, height,
89                    left + 1, top);
90         break;
91       case 4:                   /* move a block up */
92         left = nrnd(sizex);
93         top = nrnd(sizey - 1) + 1;
94         width = nrnd(sizex - left);
95         height = nrnd(sizey - top);
96         XCopyArea (dpy, window, window, gc, left, top, width, height,
97                    left, top - 1);
98         break;
99       default:                  /* move block down (biased to this) */
100         left = nrnd(sizex);
101         top = nrnd(sizey - 1);
102         width = nrnd(sizex - left);
103         height = nrnd(sizey - 1 - top);
104         XCopyArea (dpy, window, window, gc, left, top, width, height,
105                    left, top + 1);
106         break;
107     }
108     XSync (dpy, True);
109 #undef nrnd
110 }
111
112 \f
113 char *progclass = "DecayScreen";
114
115 char *defaults [] = {
116   "*dontClearRoot:              True",
117
118 #ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
119   "*visualID:                   Best",
120 #endif
121
122   "*delay:                      10",
123   0
124 };
125
126 XrmOptionDescRec options [] = {
127   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
128   { 0, 0, 0, 0 }
129 };
130
131 void
132 screenhack (Display *dpy, Window window)
133 {
134     init_decay (dpy, window);
135     while (1) {
136         decay1 (dpy, window);
137         if (delay) usleep (delay);
138     }
139 }