1 /* xscreensaver, Copyright (c) 1992-2006 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
14 * Based on slidescreen program from the xscreensaver application and the
15 * decay program for Sun framebuffers. This is the comment from the decay.c
19 * find the screen bitmap for the console and make it "decay" by
20 * randomly shifting random rectangles by one pixelwidth at a time.
23 * rewritten by Natuerlich!
24 * based on a similar "utility" on the Apollo ring at Yale.
28 * Vivek Khera <khera@cs.duke.edu>
31 * Hacked by jwz, 28-Nov-97 (sped up and added new motion directions)
34 * Added "melt" & "stretch" modes 28-Mar-1999
38 #include "screenhack.h"
51 const int *current_bias;
53 async_load_state *img_loader;
73 decayscreen_init (Display *dpy, Window window)
75 struct state *st = (struct state *) calloc (1, sizeof(*st));
77 XWindowAttributes xgwa;
85 s = get_string_resource(st->dpy, "mode", "Mode");
86 if (s && !strcmp(s, "shuffle")) st->mode = SHUFFLE;
87 else if (s && !strcmp(s, "up")) st->mode = UP;
88 else if (s && !strcmp(s, "left")) st->mode = LEFT;
89 else if (s && !strcmp(s, "right")) st->mode = RIGHT;
90 else if (s && !strcmp(s, "down")) st->mode = DOWN;
91 else if (s && !strcmp(s, "upleft")) st->mode = UPLEFT;
92 else if (s && !strcmp(s, "downleft")) st->mode = DOWNLEFT;
93 else if (s && !strcmp(s, "upright")) st->mode = UPRIGHT;
94 else if (s && !strcmp(s, "downright")) st->mode = DOWNRIGHT;
95 else if (s && !strcmp(s, "in")) st->mode = IN;
96 else if (s && !strcmp(s, "out")) st->mode = OUT;
97 else if (s && !strcmp(s, "melt")) st->mode = MELT;
98 else if (s && !strcmp(s, "stretch")) st->mode = STRETCH;
99 else if (s && !strcmp(s, "fuzz")) st->mode = FUZZ;
101 if (s && *s && !!strcmp(s, "random"))
102 fprintf(stderr, "%s: unknown mode %s\n", progname, s);
103 st->mode = random() % (FUZZ+1);
106 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
108 if (st->delay < 0) st->delay = 0;
110 XGetWindowAttributes (st->dpy, st->window, &xgwa);
112 gcv.function = GXcopy;
113 gcv.subwindow_mode = IncludeInferiors;
114 bg = get_pixel_resource (st->dpy, xgwa.colormap, "background", "Background");
117 gcflags = GCForeground | GCFunction;
118 if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
119 gcflags |= GCSubwindowMode;
120 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
122 st->sizex = xgwa.width;
123 st->sizey = xgwa.height;
125 if (st->mode == MELT || st->mode == STRETCH)
126 st->iterations = 1; /* slow down for smoother melting */
128 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
136 * perform one iteration of decay
139 decayscreen_draw (Display *dpy, Window window, void *closure)
141 struct state *st = (struct state *) closure;
142 int left, top, width, height, toleft, totop;
148 static const int no_bias[] = { L,L,L,L, R,R,R,R, U,U,U,U, D,D,D,D };
149 static const int up_bias[] = { L,L,L,L, R,R,R,R, U,U,U,U, U,U,D,D };
150 static const int down_bias[] = { L,L,L,L, R,R,R,R, U,U,D,D, D,D,D,D };
151 static const int left_bias[] = { L,L,L,L, L,L,R,R, U,U,U,U, D,D,D,D };
152 static const int right_bias[] = { L,L,R,R, R,R,R,R, U,U,U,U, D,D,D,D };
154 static const int upleft_bias[] = { L,L,L,L, L,R,R,R, U,U,U,U, U,D,D,D };
155 static const int downleft_bias[] = { L,L,L,L, L,R,R,R, U,U,U,D, D,D,D,D };
156 static const int upright_bias[] = { L,L,L,R, R,R,R,R, U,U,U,U, U,D,D,D };
157 static const int downright_bias[] = { L,L,L,R, R,R,R,R, U,U,U,D, D,D,D,D };
159 if (st->img_loader) /* still loading */
161 st->img_loader = load_image_async_simple (st->img_loader,
163 if (! st->img_loader) { /* just finished */
164 if (st->mode == MELT || st->mode == STRETCH)
165 /* make sure screen eventually turns background color */
166 XDrawLine (st->dpy, st->window, st->gc, 0, 0, st->sizex, 0);
172 case SHUFFLE: st->current_bias = no_bias; break;
173 case UP: st->current_bias = up_bias; break;
174 case LEFT: st->current_bias = left_bias; break;
175 case RIGHT: st->current_bias = right_bias; break;
176 case DOWN: st->current_bias = down_bias; break;
177 case UPLEFT: st->current_bias = upleft_bias; break;
178 case DOWNLEFT: st->current_bias = downleft_bias; break;
179 case UPRIGHT: st->current_bias = upright_bias; break;
180 case DOWNRIGHT: st->current_bias = downright_bias; break;
181 case IN: st->current_bias = no_bias; break;
182 case OUT: st->current_bias = no_bias; break;
183 case MELT: st->current_bias = no_bias; break;
184 case STRETCH: st->current_bias = no_bias; break;
185 case FUZZ: st->current_bias = no_bias; break;
189 #define nrnd(x) ((x) ? random() % (x) : x)
191 if (st->mode == MELT || st->mode == STRETCH) {
192 left = nrnd(st->sizex/2);
193 top = nrnd(st->sizey);
194 width = nrnd( st->sizex/2 ) + st->sizex/2 - left;
195 height = nrnd(st->sizey - top);
199 } else if (st->mode == FUZZ) { /* By Vince Levey <vincel@vincel.org>;
200 inspired by the "melt" mode of the
201 "scrhack" IrisGL program by Paul Haeberli
203 left = nrnd(st->sizex - 1);
204 top = nrnd(st->sizey - 1);
205 st->fuzz_toggle = !st->fuzz_toggle;
210 toleft = nrnd(st->sizex - 1);
228 totop = nrnd(st->sizey - 1);
245 left = nrnd(st->sizex - 1);
246 top = nrnd(st->sizey);
247 width = nrnd(st->sizex - left);
248 height = nrnd(st->sizey - top);
252 if (st->mode == IN || st->mode == OUT) {
253 int x = left+(width/2);
254 int y = top+(height/2);
255 int cx = st->sizex/2;
256 int cy = st->sizey/2;
257 if (st->mode == IN) {
258 if (x > cx && y > cy) st->current_bias = upleft_bias;
259 else if (x < cx && y > cy) st->current_bias = upright_bias;
260 else if (x < cx && y < cy) st->current_bias = downright_bias;
261 else /* (x > cx && y < cy)*/ st->current_bias = downleft_bias;
263 if (x > cx && y > cy) st->current_bias = downright_bias;
264 else if (x < cx && y > cy) st->current_bias = downleft_bias;
265 else if (x < cx && y < cy) st->current_bias = upleft_bias;
266 else /* (x > cx && y < cy)*/ st->current_bias = upright_bias;
270 switch (st->current_bias[random() % (sizeof(no_bias)/sizeof(*no_bias))]) {
271 case L: toleft = left-1; break;
272 case R: toleft = left+1; break;
273 case U: totop = top-1; break;
274 case D: totop = top+1; break;
275 default: abort(); break;
279 if (st->mode == STRETCH) {
280 XCopyArea (st->dpy, st->window, st->window, st->gc, 0, st->sizey-top-2, st->sizex, top+1,
283 XCopyArea (st->dpy, st->window, st->window, st->gc, left, top, width, height,
293 decayscreen_reshape (Display *dpy, Window window, void *closure,
294 unsigned int w, unsigned int h)
296 struct state *st = (struct state *) closure;
302 decayscreen_event (Display *dpy, Window window, void *closure, XEvent *event)
308 decayscreen_free (Display *dpy, Window window, void *closure)
310 struct state *st = (struct state *) closure;
316 static const char *decayscreen_defaults [] = {
317 ".background: Black",
318 ".foreground: Yellow",
319 "*dontClearRoot: True",
321 #ifdef __sgi /* really, HAVE_READ_DISPLAY_EXTENSION */
330 static XrmOptionDescRec decayscreen_options [] = {
331 { "-delay", ".delay", XrmoptionSepArg, 0 },
332 { "-mode", ".mode", XrmoptionSepArg, 0 },
337 XSCREENSAVER_MODULE ("DecayScreen", decayscreen)