ebcecfbcc1ee3e0aaf73c8aa4ae753fac0b93640
[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  *  Hacked by jwz, 28-Nov-97 (sped up and added new motion directions)
33  */
34
35 #include "screenhack.h"
36
37 static int sizex, sizey;
38 static int delay;
39 static GC gc;
40 static int mode;
41
42 #define SHUFFLE 0
43 #define UP 1
44 #define LEFT 2
45 #define RIGHT 3
46 #define DOWN 4
47 #define UPLEFT 5
48 #define DOWNLEFT 6
49 #define UPRIGHT 7
50 #define DOWNRIGHT 8
51 #define IN 9
52 #define OUT 10
53
54
55 static void
56 init_decay (Display *dpy, Window window)
57 {
58   XGCValues gcv;
59   XWindowAttributes xgwa;
60   long gcflags;
61
62   char *s = get_string_resource("mode", "Mode");
63   if      (s && !strcmp(s, "shuffle")) mode = SHUFFLE;
64   else if (s && !strcmp(s, "up")) mode = UP;
65   else if (s && !strcmp(s, "left")) mode = LEFT;
66   else if (s && !strcmp(s, "right")) mode = RIGHT;
67   else if (s && !strcmp(s, "down")) mode = DOWN;
68   else if (s && !strcmp(s, "upleft")) mode = UPLEFT;
69   else if (s && !strcmp(s, "downleft")) mode = DOWNLEFT;
70   else if (s && !strcmp(s, "upright")) mode = UPRIGHT;
71   else if (s && !strcmp(s, "downright")) mode = DOWNRIGHT;
72   else if (s && !strcmp(s, "in")) mode = IN;
73   else if (s && !strcmp(s, "out")) mode = OUT;
74   else {
75     if (s && *s && !!strcmp(s, "random"))
76       fprintf(stderr, "%s: unknown mode %s\n", progname, s);
77     mode = random() % (OUT+1);
78   }
79
80   delay = get_integer_resource ("delay", "Integer");
81
82   if (delay < 0) delay = 0;
83
84   XGetWindowAttributes (dpy, window, &xgwa);
85
86   gcv.function = GXcopy;
87   gcv.subwindow_mode = IncludeInferiors;
88   gcflags = GCForeground |GCFunction;
89   if (use_subwindow_mode_p(xgwa.screen, window)) /* see grabscreen.c */
90     gcflags |= GCSubwindowMode;
91   gc = XCreateGC (dpy, window, gcflags, &gcv);
92
93   sizex = xgwa.width;
94   sizey = xgwa.height;
95
96   grab_screen_image (xgwa.screen, window);
97 }
98
99
100 /*
101  * perform one iteration of decay
102  */
103 static void
104 decay1 (Display *dpy, Window window)
105 {
106     int left, top, width, height, toleft, totop;
107
108 #define L 101
109 #define R 102
110 #define U 103
111 #define D 104
112     static int no_bias[]        = { L,L,L,L, R,R,R,R, U,U,U,U, D,D,D,D };
113     static int up_bias[]        = { L,L,L,L, R,R,R,R, U,U,U,U, U,U,D,D };
114     static int down_bias[]      = { L,L,L,L, R,R,R,R, U,U,D,D, D,D,D,D };
115     static int left_bias[]      = { L,L,L,L, L,L,R,R, U,U,U,U, D,D,D,D };
116     static int right_bias[]     = { L,L,R,R, R,R,R,R, U,U,U,U, D,D,D,D };
117
118     static int upleft_bias[]    = { L,L,L,L, L,R,R,R, U,U,U,U, U,D,D,D };
119     static int downleft_bias[]  = { L,L,L,L, L,R,R,R, U,U,U,D, D,D,D,D };
120     static int upright_bias[]   = { L,L,L,R, R,R,R,R, U,U,U,U, U,D,D,D };
121     static int downright_bias[] = { L,L,L,R, R,R,R,R, U,U,U,D, D,D,D,D };
122     static int *bias;
123
124     switch (mode) {
125       case SHUFFLE:     bias = no_bias; break;
126       case UP:          bias = up_bias; break;
127       case LEFT:        bias = left_bias; break;
128       case RIGHT:       bias = right_bias; break;
129       case DOWN:        bias = down_bias; break;
130       case UPLEFT:      bias = upleft_bias; break;
131       case DOWNLEFT:    bias = downleft_bias; break;
132       case UPRIGHT:     bias = upright_bias; break;
133       case DOWNRIGHT:   bias = downright_bias; break;
134       case IN:          bias = no_bias; break;
135       case OUT:         bias = no_bias; break;
136       default: abort();
137     }
138
139 #define nrnd(x) (random() % (x))
140
141     left = nrnd(sizex - 1);
142     top = nrnd(sizey);
143     width = nrnd(sizex - left);
144     height = nrnd(sizey - top);
145
146     toleft = left;
147     totop = top;
148
149     if (mode == IN || mode == OUT) {
150       int x = left+(width/2);
151       int y = top+(height/2);
152       int cx = sizex/2;
153       int cy = sizey/2;
154       if (mode == IN) {
155         if      (x > cx && y > cy)   bias = upleft_bias;
156         else if (x < cx && y > cy)   bias = upright_bias;
157         else if (x < cx && y < cy)   bias = downright_bias;
158         else /* (x > cx && y < cy)*/ bias = downleft_bias;
159       } else {
160         if      (x > cx && y > cy)   bias = downright_bias;
161         else if (x < cx && y > cy)   bias = downleft_bias;
162         else if (x < cx && y < cy)   bias = upleft_bias;
163         else /* (x > cx && y < cy)*/ bias = upright_bias;
164       }
165     }
166
167     switch (bias[random() % (sizeof(no_bias)/sizeof(*no_bias))]) {
168       case L: toleft = left-1; break;
169       case R: toleft = left+1; break;
170       case U: totop = top-1; break;
171       case D: totop = top+1; break;
172       default: abort(); break;
173     }
174
175     XCopyArea (dpy, window, window, gc, left, top, width, height,
176                toleft, totop);
177 #undef nrnd
178 }
179
180 \f
181 char *progclass = "DecayScreen";
182
183 char *defaults [] = {
184   "*dontClearRoot:              True",
185
186 #ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
187   "*visualID:                   Best",
188 #endif
189
190   "*delay:                      10000",
191   "*mode:                       random",
192   0
193 };
194
195 XrmOptionDescRec options [] = {
196   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
197   { "-mode",            ".mode",                XrmoptionSepArg, 0 },
198   { 0, 0, 0, 0 }
199 };
200
201 void
202 screenhack (Display *dpy, Window window)
203 {
204     init_decay (dpy, window);
205     while (1) {
206       int i;
207       for (i = 0; i < 100; i++)
208         decay1 (dpy, window);
209       XSync(dpy, False);
210       if (delay) usleep (delay);
211     }
212 }