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
12 #include "screenhack.h"
14 enum { DOWN = 0, LEFT, UP, RIGHT };
15 enum { VERTICAL, HORIZONTAL };
24 int bitmap_w, bitmap_h;
30 int max_width, max_height;
34 int draw_x, draw_y, draw_ix, draw_iy, draw_dx, draw_dy;
35 int draw_dir, draw_w, draw_h, draw_size, draw_inc;
39 async_load_state *img_loader;
44 slidescreen_init (Display *dpy, Window window)
46 struct state *st = (struct state *) calloc (1, sizeof(*st));
47 XWindowAttributes xgwa;
54 XGetWindowAttributes (st->dpy, st->window, &xgwa);
55 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
59 st->max_width = xgwa.width;
60 st->max_height = xgwa.height;
62 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
63 st->delay2 = get_integer_resource (st->dpy, "delay2", "Integer");
64 st->grid_size = get_integer_resource (st->dpy, "gridSize", "Integer");
65 st->pix_inc = get_integer_resource (st->dpy, "pixelIncrement", "Integer");
67 /* Don't let the grid be smaller than 5x5 */
68 while (st->grid_size > xgwa.width / 5)
70 while (st->grid_size > xgwa.height / 5)
73 if (st->delay < 0) st->delay = 0;
74 if (st->delay2 < 0) st->delay2 = 0;
75 if (st->pix_inc < 1) st->pix_inc = 1;
76 if (st->grid_size < 1) st->grid_size = 1;
81 char *fgs = get_string_resource(st->dpy, "background", "Background");
82 char *bgs = get_string_resource(st->dpy, "foreground", "Foreground");
84 if (!XParseColor (st->dpy, xgwa.colormap, fgs, &fgc))
85 XParseColor (st->dpy, xgwa.colormap, "black", &bgc);
86 if (!XParseColor (st->dpy, xgwa.colormap, bgs, &bgc))
87 XParseColor (st->dpy, xgwa.colormap, "gray", &fgc);
89 fg_ok = XAllocColor (st->dpy, xgwa.colormap, &fgc);
90 bg_ok = XAllocColor (st->dpy, xgwa.colormap, &bgc);
92 /* If we weren't able to allocate the two colors we want from the
93 colormap (which is likely if the screen has been grabbed on an
94 8-bit SGI visual -- don't ask) then just go through the map
95 and find the closest color to the ones we wanted, and use those
96 pixels without actually allocating them.
112 unsigned long fgd = ~0;
113 unsigned long bgd = ~0;
114 int max = visual_cells (xgwa.screen, xgwa.visual);
115 XColor *all = (XColor *) calloc(sizeof (*all), max);
116 for (i = 0; i < max; i++)
118 all[i].flags = DoRed|DoGreen|DoBlue;
121 XQueryColors (st->dpy, xgwa.colormap, all, max);
122 for(i = 0; i < max; i++)
128 rd = (all[i].red >> 8) - (fgc.red >> 8);
129 gd = (all[i].green >> 8) - (fgc.green >> 8);
130 bd = (all[i].blue >> 8) - (fgc.blue >> 8);
131 if (rd < 0) rd = -rd;
132 if (gd < 0) gd = -gd;
133 if (bd < 0) bd = -bd;
134 dd = (rd << 1) + (gd << 2) + bd;
138 st->fg = all[i].pixel;
146 rd = (all[i].red >> 8) - (bgc.red >> 8);
147 gd = (all[i].green >> 8) - (bgc.green >> 8);
148 bd = (all[i].blue >> 8) - (bgc.blue >> 8);
149 if (rd < 0) rd = -rd;
150 if (gd < 0) gd = -gd;
151 if (bd < 0) bd = -bd;
152 dd = (rd << 1) + (gd << 2) + bd;
156 st->bg = all[i].pixel;
167 #endif /* !HAVE_COCOA */
170 gcv.foreground = st->fg;
171 gcv.function = GXcopy;
172 gcv.subwindow_mode = IncludeInferiors;
173 gcflags = GCForeground |GCFunction;
174 if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
175 gcflags |= GCSubwindowMode;
176 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
182 draw_grid (struct state *st)
187 XWindowAttributes xgwa;
189 XGetWindowAttributes (st->dpy, st->window, &xgwa);
191 border = get_integer_resource (st->dpy, "internalBorderWidth",
192 "InternalBorderWidth");
194 XGetWindowAttributes (st->dpy, st->window, &xgwa);
195 st->bitmap_w = xgwa.width;
196 st->bitmap_h = xgwa.height;
198 st->grid_w = st->bitmap_w / st->grid_size;
199 st->grid_h = st->bitmap_h / st->grid_size;
200 st->hole_x = random () % st->grid_w;
201 st->hole_y = random () % st->grid_h;
202 st->xoff = (st->bitmap_w - (st->grid_w * st->grid_size)) / 2;
203 st->yoff = (st->bitmap_h - (st->grid_h * st->grid_size)) / 2;
213 int half2 = (border & 1 ? half+1 : half);
214 XSetForeground(st->dpy, st->gc, st->bg);
215 for (i = 0; i < st->bitmap_w; i += st->grid_size)
218 for (j = 0; j < st->bitmap_h; j += st->grid_size)
219 XDrawRectangle (st->dpy, d, st->gc,
220 st->xoff+i+half2, st->yoff+j+half2,
221 st->grid_size-border-1, st->grid_size-border-1);
224 XSetForeground(st->dpy, st->gc, st->fg);
225 for (i = 0; i <= st->bitmap_w; i += st->grid_size)
226 XFillRectangle (st->dpy, d, st->gc, st->xoff+i-half, st->yoff, border, st->bitmap_h);
227 for (i = 0; i <= st->bitmap_h; i += st->grid_size)
228 XFillRectangle (st->dpy, d, st->gc, st->xoff, st->yoff+i-half, st->bitmap_w, border);
233 XFillRectangle (st->dpy, d, st->gc, 0, 0, st->xoff, st->bitmap_h);
234 XFillRectangle (st->dpy, d, st->gc, st->bitmap_w - st->xoff, 0, st->xoff, st->bitmap_h);
238 XFillRectangle (st->dpy, d, st->gc, 0, 0, st->bitmap_w, st->yoff);
239 XFillRectangle (st->dpy, d, st->gc, 0, st->bitmap_h - st->yoff, st->bitmap_w, st->yoff);
245 slidescreen_draw_early (struct state *st)
247 while (st->early_i < 0)
253 /* for (early_i = 0; early_i < grid_size; early_i += pix_inc) */
256 points[0].x = st->xoff + st->grid_size * st->hole_x;
257 points[0].y = st->yoff + st->grid_size * st->hole_y;
258 points[1].x = points[0].x + st->grid_size;
259 points[1].y = points[0].y;
260 points[2].x = points[0].x;
261 points[2].y = points[0].y + st->early_i;
262 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
264 points[1].x = points[0].x;
265 points[1].y = points[0].y + st->grid_size;
266 points[2].x = points[0].x + st->early_i;
267 points[2].y = points[0].y + st->grid_size;
268 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
270 points[0].x = points[1].x + st->grid_size;
271 points[0].y = points[1].y;
272 points[2].x = points[0].x;
273 points[2].y = points[0].y - st->early_i;
274 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
276 points[1].x = points[0].x;
277 points[1].y = points[0].y - st->grid_size;
278 points[2].x = points[1].x - st->early_i;
279 points[2].y = points[1].y;
280 XFillPolygon (st->dpy, st->window, st->gc, points, 3, Convex, CoordModeOrigin);
283 st->early_i += st->pix_inc;
284 if (st->early_i < st->grid_size)
287 XFillRectangle (st->dpy, st->window, st->gc,
288 st->xoff + st->grid_size * st->hole_x,
289 st->yoff + st->grid_size * st->hole_y,
290 st->grid_size, st->grid_size);
296 slidescreen_draw (Display *dpy, Window window, void *closure)
298 struct state *st = (struct state *) closure;
299 int this_delay = st->delay;
301 /* this code is a total kludge, but who cares, it works... */
303 if (st->img_loader) /* still loading */
305 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
306 if (! st->img_loader) /* just finished */
311 if (! st->draw_initted)
313 if (!slidescreen_draw_early (st))
315 st->draw_initted = 1;
324 if (st->draw_last == -1) st->draw_last = random () % 2;
326 /* alternate between horizontal and vertical slides */
327 /* note that draw_dir specifies the direction the _hole_ moves, not the tiles */
328 if (st->draw_last == VERTICAL) {
329 if (((st->grid_w > 1) ? st->draw_rnd = random () % (st->grid_w - 1) : 0)
331 st->draw_dx = -1; st->draw_dir = LEFT; st->hole_x -= st->draw_rnd;
333 st->draw_dx = 1; st->draw_dir = RIGHT; st->draw_rnd -= st->hole_x;
335 st->draw_dy = 0; st->draw_w = st->draw_size = st->draw_rnd + 1; st->draw_h = 1;
336 st->draw_last = HORIZONTAL;
338 if (((st->grid_h > 1) ? st->draw_rnd = random () % (st->grid_h - 1) : 0)
340 st->draw_dy = -1; st->draw_dir = UP; st->hole_y -= st->draw_rnd;
342 st->draw_dy = 1; st->draw_dir = DOWN; st->draw_rnd -= st->hole_y;
344 st->draw_dx = 0; st->draw_h = st->draw_size = st->draw_rnd + 1; st->draw_w = 1;
345 st->draw_last = VERTICAL;
348 st->draw_ix = st->draw_x = st->xoff + (st->hole_x + st->draw_dx) * st->grid_size;
349 st->draw_iy = st->draw_y = st->yoff + (st->hole_y + st->draw_dy) * st->grid_size;
350 st->draw_inc = st->pix_inc;
354 /* for (draw_i = 0; draw_i < grid_size; draw_i += draw_inc) */
356 int fx, fy, tox, toy;
357 if (st->draw_inc + st->draw_i > st->grid_size)
358 st->draw_inc = st->grid_size - st->draw_i;
359 tox = st->draw_x - st->draw_dx * st->draw_inc;
360 toy = st->draw_y - st->draw_dy * st->draw_inc;
362 fx = (st->draw_x < 0 ? 0 : st->draw_x > st->max_width ? st->max_width : st->draw_x);
363 fy = (st->draw_y < 0 ? 0 : st->draw_y > st->max_height ? st->max_height : st->draw_y);
364 tox = (tox < 0 ? 0 : tox > st->max_width ? st->max_width : tox);
365 toy = (toy < 0 ? 0 : toy > st->max_height ? st->max_height : toy);
367 XCopyArea (st->dpy, st->window, st->window, st->gc,
369 st->grid_size * st->draw_w, st->grid_size * st->draw_h,
372 st->draw_x -= st->draw_dx * st->draw_inc;
373 st->draw_y -= st->draw_dy * st->draw_inc;
374 switch (st->draw_dir)
376 case DOWN: XFillRectangle (st->dpy, st->window, st->gc,
377 st->draw_ix, st->draw_y + st->grid_size * st->draw_h, st->grid_size * st->draw_w, st->draw_iy - st->draw_y);
379 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);
381 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);
383 case RIGHT: XFillRectangle (st->dpy, st->window, st->gc,
384 st->draw_x + st->grid_size * st->draw_w, st->draw_iy, st->draw_ix - st->draw_x, st->grid_size * st->draw_h);
389 st->draw_i += st->draw_inc;
390 if (st->draw_i >= st->grid_size)
394 switch (st->draw_dir)
396 case DOWN: st->hole_y += st->draw_size; break;
397 case LEFT: st->hole_x--; break;
398 case UP: st->hole_y--; break;
399 case RIGHT: st->hole_x += st->draw_size; break;
402 this_delay = st->delay2;
409 slidescreen_reshape (Display *dpy, Window window, void *closure,
410 unsigned int w, unsigned int h)
415 slidescreen_event (Display *dpy, Window window, void *closure, XEvent *event)
421 slidescreen_free (Display *dpy, Window window, void *closure)
423 struct state *st = (struct state *) closure;
424 XFreeGC (dpy, st->gc);
430 static const char *slidescreen_defaults [] = {
431 "*dontClearRoot: True",
433 #ifdef __sgi /* really, HAVE_READ_DISPLAY_EXTENSION */
437 ".background: Black",
438 ".foreground: #BEBEBE",
440 "*pixelIncrement: 10",
441 "*internalBorderWidth: 4",
447 static XrmOptionDescRec slidescreen_options [] = {
448 { "-grid-size", ".gridSize", XrmoptionSepArg, 0 },
449 { "-ibw", ".internalBorderWidth", XrmoptionSepArg, 0 },
450 { "-increment", ".pixelIncrement", XrmoptionSepArg, 0 },
451 { "-delay", ".delay", XrmoptionSepArg, 0 },
452 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
456 XSCREENSAVER_MODULE ("Slidescreen", slidescreen)