1 /* xscreensaver, Copyright (c) 1992-2014 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
12 #include "screenhack.h"
14 enum { DOWN = 0, LEFT, UP, RIGHT };
15 enum { VERTICAL, HORIZONTAL };
24 int bitmap_w, bitmap_h;
31 int max_width, max_height;
35 int draw_x, draw_y, draw_ix, draw_iy, draw_dx, draw_dy;
36 int draw_dir, draw_w, draw_h, draw_size, draw_inc;
41 async_load_state *img_loader;
46 slidescreen_init (Display *dpy, Window window)
48 struct state *st = (struct state *) calloc (1, sizeof(*st));
49 XWindowAttributes xgwa;
55 XGetWindowAttributes (st->dpy, st->window, &xgwa);
56 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
58 st->start_time = time ((time_t) 0);
60 st->max_width = xgwa.width;
61 st->max_height = xgwa.height;
63 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
64 st->delay2 = get_integer_resource (st->dpy, "delay2", "Integer");
65 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
66 st->grid_size = get_integer_resource (st->dpy, "gridSize", "Integer");
67 st->pix_inc = get_integer_resource (st->dpy, "pixelIncrement", "Integer");
69 /* Don't let the grid be smaller than 5x5 */
70 while (st->grid_size > xgwa.width / 5)
72 while (st->grid_size > xgwa.height / 5)
75 if (st->delay < 0) st->delay = 0;
76 if (st->delay2 < 0) st->delay2 = 0;
77 if (st->duration < 1) st->duration = 1;
78 if (st->pix_inc < 1) st->pix_inc = 1;
79 if (st->grid_size < 1) st->grid_size = 1;
84 char *fgs = get_string_resource(st->dpy, "background", "Background");
85 char *bgs = get_string_resource(st->dpy, "foreground", "Foreground");
87 if (!XParseColor (st->dpy, xgwa.colormap, fgs, &fgc))
88 XParseColor (st->dpy, xgwa.colormap, "black", &bgc);
89 if (!XParseColor (st->dpy, xgwa.colormap, bgs, &bgc))
90 XParseColor (st->dpy, xgwa.colormap, "gray", &fgc);
92 fg_ok = XAllocColor (st->dpy, xgwa.colormap, &fgc);
93 bg_ok = XAllocColor (st->dpy, xgwa.colormap, &bgc);
95 /* If we weren't able to allocate the two colors we want from the
96 colormap (which is likely if the screen has been grabbed on an
97 8-bit SGI visual -- don't ask) then just go through the map
98 and find the closest color to the ones we wanted, and use those
99 pixels without actually allocating them.
115 unsigned long fgd = ~0;
116 unsigned long bgd = ~0;
117 int max = visual_cells (xgwa.screen, xgwa.visual);
118 XColor *all = (XColor *) calloc(sizeof (*all), max);
119 for (i = 0; i < max; i++)
121 all[i].flags = DoRed|DoGreen|DoBlue;
124 XQueryColors (st->dpy, xgwa.colormap, all, max);
125 for(i = 0; i < max; i++)
131 rd = (all[i].red >> 8) - (fgc.red >> 8);
132 gd = (all[i].green >> 8) - (fgc.green >> 8);
133 bd = (all[i].blue >> 8) - (fgc.blue >> 8);
134 if (rd < 0) rd = -rd;
135 if (gd < 0) gd = -gd;
136 if (bd < 0) bd = -bd;
137 dd = (rd << 1) + (gd << 2) + bd;
141 st->fg = all[i].pixel;
149 rd = (all[i].red >> 8) - (bgc.red >> 8);
150 gd = (all[i].green >> 8) - (bgc.green >> 8);
151 bd = (all[i].blue >> 8) - (bgc.blue >> 8);
152 if (rd < 0) rd = -rd;
153 if (gd < 0) gd = -gd;
154 if (bd < 0) bd = -bd;
155 dd = (rd << 1) + (gd << 2) + bd;
159 st->bg = all[i].pixel;
170 #endif /* !HAVE_COCOA */
173 gcv.foreground = st->fg;
174 gcv.function = GXcopy;
175 gcv.subwindow_mode = IncludeInferiors;
176 gcflags = GCForeground |GCFunction;
177 if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
178 gcflags |= GCSubwindowMode;
179 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
185 draw_grid (struct state *st)
190 XWindowAttributes xgwa;
192 XGetWindowAttributes (st->dpy, st->window, &xgwa);
194 border = get_integer_resource (st->dpy, "internalBorderWidth",
195 "InternalBorderWidth");
197 XGetWindowAttributes (st->dpy, st->window, &xgwa);
198 st->bitmap_w = xgwa.width;
199 st->bitmap_h = xgwa.height;
201 st->grid_w = st->bitmap_w / st->grid_size;
202 st->grid_h = st->bitmap_h / st->grid_size;
203 st->hole_x = random () % st->grid_w;
204 st->hole_y = random () % st->grid_h;
205 st->xoff = (st->bitmap_w - (st->grid_w * st->grid_size)) / 2;
206 st->yoff = (st->bitmap_h - (st->grid_h * st->grid_size)) / 2;
216 int half2 = (border & 1 ? half+1 : half);
217 XSetForeground(st->dpy, st->gc, st->bg);
218 for (i = 0; i < st->bitmap_w; i += st->grid_size)
221 for (j = 0; j < st->bitmap_h; j += st->grid_size)
222 XDrawRectangle (st->dpy, d, st->gc,
223 st->xoff+i+half2, st->yoff+j+half2,
224 st->grid_size-border-1, st->grid_size-border-1);
227 XSetForeground(st->dpy, st->gc, st->fg);
228 for (i = 0; i <= st->bitmap_w; i += st->grid_size)
229 XFillRectangle (st->dpy, d, st->gc, st->xoff+i-half, st->yoff, border, st->bitmap_h);
230 for (i = 0; i <= st->bitmap_h; i += st->grid_size)
231 XFillRectangle (st->dpy, d, st->gc, st->xoff, st->yoff+i-half, st->bitmap_w, border);
236 XFillRectangle (st->dpy, d, st->gc, 0, 0, st->xoff, st->bitmap_h);
237 XFillRectangle (st->dpy, d, st->gc, st->bitmap_w - st->xoff, 0, st->xoff, st->bitmap_h);
241 XFillRectangle (st->dpy, d, st->gc, 0, 0, st->bitmap_w, st->yoff);
242 XFillRectangle (st->dpy, d, st->gc, 0, st->bitmap_h - st->yoff, st->bitmap_w, st->yoff);
248 slidescreen_draw_early (struct state *st)
250 while (st->early_i < 0)
256 /* for (early_i = 0; early_i < grid_size; early_i += pix_inc) */
259 points[0].x = st->xoff + st->grid_size * st->hole_x;
260 points[0].y = st->yoff + st->grid_size * st->hole_y;
261 points[1].x = points[0].x + st->grid_size;
262 points[1].y = points[0].y;
263 points[2].x = points[0].x;
264 points[2].y = points[0].y + st->early_i;
265 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
267 points[1].x = points[0].x;
268 points[1].y = points[0].y + st->grid_size;
269 points[2].x = points[0].x + st->early_i;
270 points[2].y = points[0].y + st->grid_size;
271 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
273 points[0].x = points[1].x + st->grid_size;
274 points[0].y = points[1].y;
275 points[2].x = points[0].x;
276 points[2].y = points[0].y - st->early_i;
277 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
279 points[1].x = points[0].x;
280 points[1].y = points[0].y - st->grid_size;
281 points[2].x = points[1].x - st->early_i;
282 points[2].y = points[1].y;
283 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
286 st->early_i += st->pix_inc;
287 if (st->early_i < st->grid_size)
290 XFillRectangle (st->dpy, st->window, st->gc,
291 st->xoff + st->grid_size * st->hole_x,
292 st->yoff + st->grid_size * st->hole_y,
293 st->grid_size, st->grid_size);
299 slidescreen_draw (Display *dpy, Window window, void *closure)
301 struct state *st = (struct state *) closure;
302 int this_delay = st->delay;
304 /* this code is a total kludge, but who cares, it works... */
306 if (st->img_loader) /* still loading */
308 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
309 if (! st->img_loader) { /* just finished */
310 st->start_time = time ((time_t) 0);
316 if (!st->img_loader &&
317 st->start_time + st->duration < time ((time_t) 0)) {
318 XWindowAttributes xgwa;
319 XGetWindowAttributes(st->dpy, st->window, &xgwa);
320 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
322 st->start_time = time ((time_t) 0);
323 st->draw_initted = 0;
327 if (! st->draw_initted)
329 if (!slidescreen_draw_early (st))
331 st->draw_initted = 1;
340 if (st->draw_last == -1) st->draw_last = random () % 2;
342 /* alternate between horizontal and vertical slides */
343 /* note that draw_dir specifies the direction the _hole_ moves, not the tiles */
344 if (st->draw_last == VERTICAL) {
345 if (((st->grid_w > 1) ? st->draw_rnd = random () % (st->grid_w - 1) : 0)
347 st->draw_dx = -1; st->draw_dir = LEFT; st->hole_x -= st->draw_rnd;
349 st->draw_dx = 1; st->draw_dir = RIGHT; st->draw_rnd -= st->hole_x;
351 st->draw_dy = 0; st->draw_w = st->draw_size = st->draw_rnd + 1; st->draw_h = 1;
352 st->draw_last = HORIZONTAL;
354 if (((st->grid_h > 1) ? st->draw_rnd = random () % (st->grid_h - 1) : 0)
356 st->draw_dy = -1; st->draw_dir = UP; st->hole_y -= st->draw_rnd;
358 st->draw_dy = 1; st->draw_dir = DOWN; st->draw_rnd -= st->hole_y;
360 st->draw_dx = 0; st->draw_h = st->draw_size = st->draw_rnd + 1; st->draw_w = 1;
361 st->draw_last = VERTICAL;
364 st->draw_ix = st->draw_x = st->xoff + (st->hole_x + st->draw_dx) * st->grid_size;
365 st->draw_iy = st->draw_y = st->yoff + (st->hole_y + st->draw_dy) * st->grid_size;
366 st->draw_inc = st->pix_inc;
370 /* for (draw_i = 0; draw_i < grid_size; draw_i += draw_inc) */
372 int fx, fy, tox, toy;
373 if (st->draw_inc + st->draw_i > st->grid_size)
374 st->draw_inc = st->grid_size - st->draw_i;
375 tox = st->draw_x - st->draw_dx * st->draw_inc;
376 toy = st->draw_y - st->draw_dy * st->draw_inc;
378 fx = (st->draw_x < 0 ? 0 : st->draw_x > st->max_width ? st->max_width : st->draw_x);
379 fy = (st->draw_y < 0 ? 0 : st->draw_y > st->max_height ? st->max_height : st->draw_y);
380 tox = (tox < 0 ? 0 : tox > st->max_width ? st->max_width : tox);
381 toy = (toy < 0 ? 0 : toy > st->max_height ? st->max_height : toy);
383 XCopyArea (st->dpy, st->window, st->window, st->gc,
385 st->grid_size * st->draw_w, st->grid_size * st->draw_h,
388 st->draw_x -= st->draw_dx * st->draw_inc;
389 st->draw_y -= st->draw_dy * st->draw_inc;
390 switch (st->draw_dir)
392 case DOWN: XFillRectangle (st->dpy, st->window, st->gc,
393 st->draw_ix, st->draw_y + st->grid_size * st->draw_h, st->grid_size * st->draw_w, st->draw_iy - st->draw_y);
395 case LEFT: XFillRectangle (st->dpy, st->window, st->gc, st->draw_ix, st->draw_iy, st->draw_x - st->draw_ix, st->grid_size * st->draw_h);
397 case UP: XFillRectangle (st->dpy, st->window, st->gc, st->draw_ix, st->draw_iy, st->grid_size * st->draw_w, st->draw_y - st->draw_iy);
399 case RIGHT: XFillRectangle (st->dpy, st->window, st->gc,
400 st->draw_x + st->grid_size * st->draw_w, st->draw_iy, st->draw_ix - st->draw_x, st->grid_size * st->draw_h);
405 st->draw_i += st->draw_inc;
406 if (st->draw_i >= st->grid_size)
410 switch (st->draw_dir)
412 case DOWN: st->hole_y += st->draw_size; break;
413 case LEFT: st->hole_x--; break;
414 case UP: st->hole_y--; break;
415 case RIGHT: st->hole_x += st->draw_size; break;
418 this_delay = st->delay2;
425 slidescreen_reshape (Display *dpy, Window window, void *closure,
426 unsigned int w, unsigned int h)
428 struct state *st = (struct state *) closure;
431 if (! st->img_loader) {
432 XWindowAttributes xgwa;
433 XGetWindowAttributes (st->dpy, st->window, &xgwa);
434 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
436 st->start_time = time ((time_t) 0);
441 slidescreen_event (Display *dpy, Window window, void *closure, XEvent *event)
443 struct state *st = (struct state *) closure;
444 if (screenhack_event_helper (dpy, window, event))
453 slidescreen_free (Display *dpy, Window window, void *closure)
455 struct state *st = (struct state *) closure;
456 XFreeGC (dpy, st->gc);
462 static const char *slidescreen_defaults [] = {
463 "*dontClearRoot: True",
466 #ifdef __sgi /* really, HAVE_READ_DISPLAY_EXTENSION */
470 ".background: Black",
471 ".foreground: #BEBEBE",
473 "*pixelIncrement: 10",
474 "*internalBorderWidth: 4",
479 "*ignoreRotation: True",
480 "*rotateImages: True",
485 static XrmOptionDescRec slidescreen_options [] = {
486 { "-grid-size", ".gridSize", XrmoptionSepArg, 0 },
487 { "-ibw", ".internalBorderWidth", XrmoptionSepArg, 0 },
488 { "-increment", ".pixelIncrement", XrmoptionSepArg, 0 },
489 { "-delay", ".delay", XrmoptionSepArg, 0 },
490 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
491 {"-duration", ".duration", XrmoptionSepArg, 0 },
495 XSCREENSAVER_MODULE ("SlideScreen", slidescreen)