1 /* xscreensaver, Copyright (c) 2005-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
11 * Boxfit -- fills space with a gradient of growing boxes or circles.
13 * Written by jwz, 21-Feb-2005.
15 * Inspired by http://www.levitated.net/daily/levBoxFitting.html
18 #include "screenhack.h"
20 #include "xpm-pixmap.h"
27 unsigned long fill_color;
35 XWindowAttributes xgwa;
37 unsigned long fg_color, bg_color;
58 async_load_state *img_loader;
59 Pixmap loading_pixmap;
65 reset_boxes (state *st)
71 st->color_horiz_p = random() & 1;
73 if (st->done_once && st->colors)
74 free_colors (st->dpy, st->xgwa.colormap, st->colors, st->ncolors);
78 char *s = get_string_resource (st->dpy, "mode", "Mode");
79 if (!s || !*s || !strcasecmp (s, "random"))
81 else if (!strcasecmp (s, "squares") || !strcasecmp (s, "square"))
83 else if (!strcasecmp (s, "circles") || !strcasecmp (s, "circle"))
88 "%s: mode must be random, squares, or circles, not '%s'\n",
95 st->circles_p = random() & 1;
97 st->circles_p = (mode == 1);
101 if (st->image || get_boolean_resource (st->dpy, "grab", "Boolean"))
103 if (st->image) XDestroyImage (st->image);
106 if (st->loading_pixmap) abort();
107 if (st->img_loader) abort();
108 if (!get_boolean_resource (st->dpy, "peek", "Boolean"))
109 st->loading_pixmap = XCreatePixmap (st->dpy, st->window,
110 st->xgwa.width, st->xgwa.height,
113 XClearWindow (st->dpy, st->window);
114 st->img_loader = load_image_async_simple (0, st->xgwa.screen,
123 st->ncolors = get_integer_resource (st->dpy, "colors", "Colors"); /* re-get */
124 if (st->ncolors < 1) st->ncolors = 1;
125 make_smooth_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
126 st->colors, &st->ncolors, True, 0, False);
127 if (st->ncolors < 1) abort();
128 XClearWindow (st->dpy, st->window);
134 reshape_boxes (state *st)
137 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
138 for (i = 0; i < st->nboxes; i++)
140 box *b = &st->boxes[i];
146 boxfit_init (Display *dpy, Window window)
149 state *st = (state *) calloc (1, sizeof (*st));
153 st->delay = get_integer_resource (dpy, "delay", "Integer");
155 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
156 /* XSelectInput (dpy, window, st->xgwa.your_event_mask | ExposureMask);*/
158 if (! get_boolean_resource (dpy, "grab", "Boolean"))
160 st->ncolors = get_integer_resource (dpy, "colors", "Colors");
161 if (st->ncolors < 1) st->ncolors = 1;
162 st->colors = (XColor *) malloc (sizeof(XColor) * st->ncolors);
165 st->inc = get_integer_resource (dpy, "growBy", "GrowBy");
166 st->spacing = get_integer_resource (dpy, "spacing", "Spacing");
167 st->border_size = get_integer_resource (dpy, "borderSize", "BorderSize");
168 st->fg_color = get_pixel_resource (st->dpy, st->xgwa.colormap,
169 "foreground", "Foreground");
170 st->bg_color = get_pixel_resource (st->dpy, st->xgwa.colormap,
171 "background", "Background");
172 if (st->inc < 1) st->inc = 1;
173 if (st->border_size < 0) st->border_size = 0;
175 gcv.line_width = st->border_size;
176 gcv.background = st->bg_color;
177 st->gc = XCreateGC (st->dpy, st->window, GCBackground|GCLineWidth, &gcv);
179 st->box_count = get_integer_resource (dpy, "boxCount", "BoxCount");
180 if (st->box_count < 1) st->box_count = 1;
183 st->boxes_size = st->box_count * 2;
184 st->boxes = (box *) calloc (st->boxes_size, sizeof(*st->boxes));
195 boxes_overlap_p (box *a, box *b, int pad)
197 /* Two rectangles overlap if the max of the tops is less than the
198 min of the bottoms and the max of the lefts is less than the min
203 # define MAX(A,B) ((A)>(B)?(A):(B))
204 # define MIN(A,B) ((A)<(B)?(A):(B))
206 int maxleft = MAX(a->x - pad, b->x);
207 int maxtop = MAX(a->y - pad, b->y);
208 int minright = MIN(a->x + a->w + pad + pad - 1, b->x + b->w);
209 int minbot = MIN(a->y + a->h + pad + pad - 1, b->y + b->h);
210 return (maxtop < minbot && maxleft < minright);
215 circles_overlap_p (box *a, box *b, int pad)
217 int ar = a->w/2; /* radius */
219 int ax = a->x + ar; /* center */
223 int d2 = (((bx - ax) * (bx - ax)) + /* distance between centers squared */
224 ((by - ay) * (by - ay)));
225 int r2 = ((ar + br + pad) * /* sum of radii squared */
232 box_collides_p (state *st, box *a, int pad)
236 /* collide with wall */
237 if (a->x - pad < 0 ||
239 a->x + a->w + pad + pad >= st->xgwa.width ||
240 a->y + a->h + pad + pad >= st->xgwa.height)
243 /* collide with another box */
244 for (i = 0; i < st->nboxes; i++)
246 box *b = &st->boxes[i];
249 ? circles_overlap_p (a, b, pad)
250 : boxes_overlap_p (a, b, pad)))
259 grow_boxes (state *st)
261 int inc2 = st->inc + st->spacing + st->border_size;
265 /* check box collisions, and grow if none.
267 for (i = 0; i < st->nboxes; i++)
269 box *a = &st->boxes[i];
270 if (!(a->flags & ALIVE)) continue;
272 if (box_collides_p (st, a, inc2))
281 a->w += st->inc + st->inc;
282 a->h += st->inc + st->inc;
288 while (live_count < st->box_count)
292 if (st->boxes_size <= st->nboxes)
294 st->boxes_size = (st->boxes_size * 1.2) + st->nboxes;
296 realloc (st->boxes, st->boxes_size * sizeof(*st->boxes));
299 fprintf (stderr, "%s: out of memory (%d boxes)\n",
300 progname, st->boxes_size);
305 a = &st->boxes[st->nboxes-1];
308 for (i = 0; i < 100; i++)
310 a->x = inc2 + (random() % (st->xgwa.width - inc2));
311 a->y = inc2 + (random() % (st->xgwa.height - inc2));
315 if (! box_collides_p (st, a, inc2))
323 if (! (a->flags & ALIVE) || /* too many retries; */
324 st->nboxes > 65535) /* that's about 1MB of box structs. */
326 st->nboxes--; /* go into "fade out" mode now. */
327 st->growing_p = False;
328 return 2000000; /* make customizable... */
331 /* Pick colors for this box */
334 int w = st->image->width;
335 int h = st->image->height;
336 a->fill_color = XGetPixel (st->image, a->x % w, a->y % h);
340 int n = (st->color_horiz_p
341 ? (a->x * st->ncolors / st->xgwa.width)
342 : (a->y * st->ncolors / st->xgwa.height));
343 a->fill_color = st->colors [n % st->ncolors].pixel;
352 shrink_boxes (state *st)
357 for (i = 0; i < st->nboxes; i++)
359 box *a = &st->boxes[i];
361 if (a->w <= 0 || a->h <= 0) continue;
365 a->w -= st->inc + st->inc;
366 a->h -= st->inc + st->inc;
368 if (a->w < 0) a->w = 0;
369 if (a->h < 0) a->h = 0;
371 if (a->w > 0 && a->h > 0)
375 if (remaining == 0) {
385 draw_boxes (state *st)
388 for (i = 0; i < st->nboxes; i++)
390 box *b = &st->boxes[i];
392 if (b->flags & UNDEAD) continue;
393 if (! (b->flags & CHANGED)) continue;
394 b->flags &= ~CHANGED;
398 /* When shrinking, black out an area outside of the border
399 before re-drawing the box.
401 int margin = st->inc + st->border_size;
403 XSetForeground (st->dpy, st->gc, st->bg_color);
405 XFillArc (st->dpy, st->window, st->gc,
406 b->x - margin, b->y - margin,
407 b->w + (margin*2), b->h + (margin*2),
410 XFillRectangle (st->dpy, st->window, st->gc,
411 b->x - margin, b->y - margin,
412 b->w + (margin*2), b->h + (margin*2));
414 if (b->w <= 0 || b->h <= 0)
415 b->flags |= UNDEAD; /* really very dead now */
418 if (b->w <= 0 || b->h <= 0) continue;
420 XSetForeground (st->dpy, st->gc, b->fill_color);
423 XFillArc (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h,
426 XFillRectangle (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h);
428 if (st->border_size > 0)
430 unsigned int bd = (st->image
432 : st->colors [(b->fill_color + st->ncolors/2)
433 % st->ncolors].pixel);
434 XSetForeground (st->dpy, st->gc, bd);
436 XDrawArc (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h,
439 XDrawRectangle (st->dpy, st->window, st->gc,
440 b->x, b->y, b->w, b->h);
447 boxfit_draw (Display *dpy, Window window, void *closure)
449 state *st = (state *) closure;
452 if (st->img_loader) /* still loading */
454 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
455 if (! st->img_loader) /* just finished */
457 st->image = XGetImage (st->dpy,
458 (st->loading_pixmap ? st->loading_pixmap :
461 st->xgwa.width, st->xgwa.height, ~0L,
463 if (st->loading_pixmap) XFreePixmap (st->dpy, st->loading_pixmap);
464 XSetWindowBackground (st->dpy, st->window, st->bg_color);
465 if (st->loading_pixmap)
466 XClearWindow (st->dpy, st->window);
468 st->countdown = 2000000;
469 st->loading_pixmap = 0;
474 if (st->countdown > 0)
476 st->countdown -= st->delay;
477 if (st->countdown <= 0)
480 XClearWindow (st->dpy, st->window);
487 delay = grow_boxes (st);
489 delay = shrink_boxes (st);
496 boxfit_reshape (Display *dpy, Window window, void *closure,
497 unsigned int w, unsigned int h)
499 state *st = (state *) closure;
504 boxfit_event (Display *dpy, Window window, void *closure, XEvent *event)
506 state *st = (state *) closure;
507 if (event->xany.type == ButtonPress) {
508 st->growing_p = !st->growing_p;
515 boxfit_free (Display *dpy, Window window, void *closure)
520 static const char *boxfit_defaults [] = {
521 ".background: black",
522 ".foreground: #444444",
533 "*grabDesktopImages: False", /* HAVE_COCOA */
534 "*chooseRandomImages: True", /* HAVE_COCOA */
538 static XrmOptionDescRec boxfit_options [] = {
539 { "-delay", ".delay", XrmoptionSepArg, 0 },
540 { "-colors", ".colors", XrmoptionSepArg, 0 },
541 { "-count", ".boxCount", XrmoptionSepArg, 0 },
542 { "-growby", ".growBy", XrmoptionSepArg, 0 },
543 { "-spacing", ".spacing", XrmoptionSepArg, 0 },
544 { "-border", ".borderSize", XrmoptionSepArg, 0 },
545 { "-mode", ".mode", XrmoptionSepArg, 0 },
546 { "-circles", ".mode", XrmoptionNoArg, "circles" },
547 { "-squares", ".mode", XrmoptionNoArg, "squares" },
548 { "-random", ".mode", XrmoptionNoArg, "random" },
549 { "-grab", ".grab", XrmoptionNoArg, "True" },
550 { "-no-grab", ".grab", XrmoptionNoArg, "False" },
551 { "-peek", ".peek", XrmoptionNoArg, "True" },
552 { "-no-peek", ".peek", XrmoptionNoArg, "False" },
557 XSCREENSAVER_MODULE ("BoxFit", boxfit)