1 /* xscreensaver, Copyright (c) 1992-2008 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"
53 const int *current_bias;
55 async_load_state *img_loader;
75 decayscreen_load_image (struct state *st)
77 XWindowAttributes xgwa;
78 XGetWindowAttributes (st->dpy, st->window, &xgwa);
79 st->sizex = xgwa.width;
80 st->sizey = xgwa.height;
81 if (st->img_loader) abort();
82 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
87 decayscreen_init (Display *dpy, Window window)
89 struct state *st = (struct state *) calloc (1, sizeof(*st));
91 XWindowAttributes xgwa;
100 s = get_string_resource(st->dpy, "mode", "Mode");
101 if (s && !strcmp(s, "shuffle")) st->mode = SHUFFLE;
102 else if (s && !strcmp(s, "up")) st->mode = UP;
103 else if (s && !strcmp(s, "left")) st->mode = LEFT;
104 else if (s && !strcmp(s, "right")) st->mode = RIGHT;
105 else if (s && !strcmp(s, "down")) st->mode = DOWN;
106 else if (s && !strcmp(s, "upleft")) st->mode = UPLEFT;
107 else if (s && !strcmp(s, "downleft")) st->mode = DOWNLEFT;
108 else if (s && !strcmp(s, "upright")) st->mode = UPRIGHT;
109 else if (s && !strcmp(s, "downright")) st->mode = DOWNRIGHT;
110 else if (s && !strcmp(s, "in")) st->mode = IN;
111 else if (s && !strcmp(s, "out")) st->mode = OUT;
112 else if (s && !strcmp(s, "melt")) st->mode = MELT;
113 else if (s && !strcmp(s, "stretch")) st->mode = STRETCH;
114 else if (s && !strcmp(s, "fuzz")) st->mode = FUZZ;
116 if (s && *s && !!strcmp(s, "random"))
117 fprintf(stderr, "%s: unknown mode %s\n", progname, s);
119 st->mode = random() % (FUZZ+1);
122 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
123 if (st->delay < 0) st->delay = 0;
125 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
126 if (st->duration < 1) st->duration = 1;
128 XGetWindowAttributes (st->dpy, st->window, &xgwa);
130 gcv.function = GXcopy;
131 gcv.subwindow_mode = IncludeInferiors;
132 bg = get_pixel_resource (st->dpy, xgwa.colormap, "background", "Background");
135 gcflags = GCForeground | GCFunction;
136 if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
137 gcflags |= GCSubwindowMode;
138 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
140 st->start_time = time ((time_t) 0);
141 decayscreen_load_image (st);
148 * perform one iteration of decay
151 decayscreen_draw (Display *dpy, Window window, void *closure)
153 struct state *st = (struct state *) closure;
154 int left, top, width, height, toleft, totop;
160 static const int no_bias[] = { L,L,L,L, R,R,R,R, U,U,U,U, D,D,D,D };
161 static const int up_bias[] = { L,L,L,L, R,R,R,R, U,U,U,U, U,U,D,D };
162 static const int down_bias[] = { L,L,L,L, R,R,R,R, U,U,D,D, D,D,D,D };
163 static const int left_bias[] = { L,L,L,L, L,L,R,R, U,U,U,U, D,D,D,D };
164 static const int right_bias[] = { L,L,R,R, R,R,R,R, U,U,U,U, D,D,D,D };
166 static const int upleft_bias[] = { L,L,L,L, L,R,R,R, U,U,U,U, U,D,D,D };
167 static const int downleft_bias[] = { L,L,L,L, L,R,R,R, U,U,U,D, D,D,D,D };
168 static const int upright_bias[] = { L,L,L,R, R,R,R,R, U,U,U,U, U,D,D,D };
169 static const int downright_bias[] = { L,L,L,R, R,R,R,R, U,U,U,D, D,D,D,D };
171 if (st->img_loader) /* still loading */
173 st->img_loader = load_image_async_simple (st->img_loader,
175 if (! st->img_loader) { /* just finished */
177 st->start_time = time ((time_t) 0);
179 st->mode = random() % (FUZZ+1);
181 if (st->mode == MELT || st->mode == STRETCH)
182 /* make sure screen eventually turns background color */
183 XDrawLine (st->dpy, st->window, st->gc, 0, 0, st->sizex, 0);
188 if (!st->img_loader &&
189 st->start_time + st->duration < time ((time_t) 0)) {
190 decayscreen_load_image (st);
194 case SHUFFLE: st->current_bias = no_bias; break;
195 case UP: st->current_bias = up_bias; break;
196 case LEFT: st->current_bias = left_bias; break;
197 case RIGHT: st->current_bias = right_bias; break;
198 case DOWN: st->current_bias = down_bias; break;
199 case UPLEFT: st->current_bias = upleft_bias; break;
200 case DOWNLEFT: st->current_bias = downleft_bias; break;
201 case UPRIGHT: st->current_bias = upright_bias; break;
202 case DOWNRIGHT: st->current_bias = downright_bias; break;
203 case IN: st->current_bias = no_bias; break;
204 case OUT: st->current_bias = no_bias; break;
205 case MELT: st->current_bias = no_bias; break;
206 case STRETCH: st->current_bias = no_bias; break;
207 case FUZZ: st->current_bias = no_bias; break;
211 #define nrnd(x) ((x) ? random() % (x) : x)
213 if (st->mode == MELT || st->mode == STRETCH) {
214 left = nrnd(st->sizex/2);
215 top = nrnd(st->sizey);
216 width = nrnd( st->sizex/2 ) + st->sizex/2 - left;
217 height = nrnd(st->sizey - top);
221 } else if (st->mode == FUZZ) { /* By Vince Levey <vincel@vincel.org>;
222 inspired by the "melt" mode of the
223 "scrhack" IrisGL program by Paul Haeberli
225 left = nrnd(st->sizex - 1);
226 top = nrnd(st->sizey - 1);
227 st->fuzz_toggle = !st->fuzz_toggle;
232 toleft = nrnd(st->sizex - 1);
250 totop = nrnd(st->sizey - 1);
267 left = nrnd(st->sizex - 1);
268 top = nrnd(st->sizey);
269 width = nrnd(st->sizex - left);
270 height = nrnd(st->sizey - top);
274 if (st->mode == IN || st->mode == OUT) {
275 int x = left+(width/2);
276 int y = top+(height/2);
277 int cx = st->sizex/2;
278 int cy = st->sizey/2;
279 if (st->mode == IN) {
280 if (x > cx && y > cy) st->current_bias = upleft_bias;
281 else if (x < cx && y > cy) st->current_bias = upright_bias;
282 else if (x < cx && y < cy) st->current_bias = downright_bias;
283 else /* (x > cx && y < cy)*/ st->current_bias = downleft_bias;
285 if (x > cx && y > cy) st->current_bias = downright_bias;
286 else if (x < cx && y > cy) st->current_bias = downleft_bias;
287 else if (x < cx && y < cy) st->current_bias = upleft_bias;
288 else /* (x > cx && y < cy)*/ st->current_bias = upright_bias;
292 switch (st->current_bias[random() % (sizeof(no_bias)/sizeof(*no_bias))]) {
293 case L: toleft = left-1; break;
294 case R: toleft = left+1; break;
295 case U: totop = top-1; break;
296 case D: totop = top+1; break;
297 default: abort(); break;
301 if (st->mode == STRETCH) {
302 XCopyArea (st->dpy, st->window, st->window, st->gc, 0, st->sizey-top-2, st->sizex, top+1,
305 XCopyArea (st->dpy, st->window, st->window, st->gc, left, top, width, height,
315 decayscreen_reshape (Display *dpy, Window window, void *closure,
316 unsigned int w, unsigned int h)
318 struct state *st = (struct state *) closure;
324 decayscreen_event (Display *dpy, Window window, void *closure, XEvent *event)
330 decayscreen_free (Display *dpy, Window window, void *closure)
332 struct state *st = (struct state *) closure;
338 static const char *decayscreen_defaults [] = {
339 ".background: Black",
340 ".foreground: Yellow",
341 "*dontClearRoot: True",
344 #ifdef __sgi /* really, HAVE_READ_DISPLAY_EXTENSION */
354 static XrmOptionDescRec decayscreen_options [] = {
355 { "-delay", ".delay", XrmoptionSepArg, 0 },
356 { "-mode", ".mode", XrmoptionSepArg, 0 },
357 { "-duration", ".duration", XrmoptionSepArg, 0 },
362 XSCREENSAVER_MODULE ("DecayScreen", decayscreen)