1 /* xscreensaver, Copyright (c) 2005, 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
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 make_smooth_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
125 st->colors, &st->ncolors, True, 0, False);
126 XClearWindow (st->dpy, st->window);
132 reshape_boxes (state *st)
135 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
136 for (i = 0; i < st->nboxes; i++)
138 box *b = &st->boxes[i];
144 boxfit_init (Display *dpy, Window window)
147 state *st = (state *) calloc (1, sizeof (*st));
151 st->delay = get_integer_resource (dpy, "delay", "Integer");
153 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
154 /* XSelectInput (dpy, window, st->xgwa.your_event_mask | ExposureMask);*/
156 if (! get_boolean_resource (dpy, "grab", "Boolean"))
158 st->ncolors = get_integer_resource (dpy, "colors", "Colors");
159 if (st->ncolors < 1) st->ncolors = 1;
160 st->colors = (XColor *) malloc (sizeof(XColor) * st->ncolors);
163 st->inc = get_integer_resource (dpy, "growBy", "GrowBy");
164 st->spacing = get_integer_resource (dpy, "spacing", "Spacing");
165 st->border_size = get_integer_resource (dpy, "borderSize", "BorderSize");
166 st->fg_color = get_pixel_resource (st->dpy, st->xgwa.colormap,
167 "foreground", "Foreground");
168 st->bg_color = get_pixel_resource (st->dpy, st->xgwa.colormap,
169 "background", "Background");
170 if (st->inc < 1) st->inc = 1;
171 if (st->border_size < 0) st->border_size = 0;
173 gcv.line_width = st->border_size;
174 gcv.background = st->bg_color;
175 st->gc = XCreateGC (st->dpy, st->window, GCBackground|GCLineWidth, &gcv);
177 st->box_count = get_integer_resource (dpy, "boxCount", "BoxCount");
178 if (st->box_count < 1) st->box_count = 1;
181 st->boxes_size = st->box_count * 2;
182 st->boxes = (box *) calloc (st->boxes_size, sizeof(*st->boxes));
193 boxes_overlap_p (box *a, box *b, int pad)
195 /* Two rectangles overlap if the max of the tops is less than the
196 min of the bottoms and the max of the lefts is less than the min
201 # define MAX(A,B) ((A)>(B)?(A):(B))
202 # define MIN(A,B) ((A)<(B)?(A):(B))
204 int maxleft = MAX(a->x - pad, b->x);
205 int maxtop = MAX(a->y - pad, b->y);
206 int minright = MIN(a->x + a->w + pad + pad - 1, b->x + b->w);
207 int minbot = MIN(a->y + a->h + pad + pad - 1, b->y + b->h);
208 return (maxtop < minbot && maxleft < minright);
213 circles_overlap_p (box *a, box *b, int pad)
215 int ar = a->w/2; /* radius */
217 int ax = a->x + ar; /* center */
221 int d2 = (((bx - ax) * (bx - ax)) + /* distance between centers squared */
222 ((by - ay) * (by - ay)));
223 int r2 = ((ar + br + pad) * /* sum of radii squared */
230 box_collides_p (state *st, box *a, int pad)
234 /* collide with wall */
235 if (a->x - pad < 0 ||
237 a->x + a->w + pad + pad >= st->xgwa.width ||
238 a->y + a->h + pad + pad >= st->xgwa.height)
241 /* collide with another box */
242 for (i = 0; i < st->nboxes; i++)
244 box *b = &st->boxes[i];
247 ? circles_overlap_p (a, b, pad)
248 : boxes_overlap_p (a, b, pad)))
257 grow_boxes (state *st)
259 int inc2 = st->inc + st->spacing + st->border_size;
263 /* check box collisions, and grow if none.
265 for (i = 0; i < st->nboxes; i++)
267 box *a = &st->boxes[i];
268 if (!(a->flags & ALIVE)) continue;
270 if (box_collides_p (st, a, inc2))
279 a->w += st->inc + st->inc;
280 a->h += st->inc + st->inc;
286 while (live_count < st->box_count)
290 if (st->boxes_size <= st->nboxes)
292 st->boxes_size = (st->boxes_size * 1.2) + st->nboxes;
294 realloc (st->boxes, st->boxes_size * sizeof(*st->boxes));
297 fprintf (stderr, "%s: out of memory (%d boxes)\n",
298 progname, st->boxes_size);
303 a = &st->boxes[st->nboxes-1];
306 for (i = 0; i < 100; i++)
308 a->x = inc2 + (random() % (st->xgwa.width - inc2));
309 a->y = inc2 + (random() % (st->xgwa.height - inc2));
313 if (! box_collides_p (st, a, inc2))
321 if (! (a->flags & ALIVE) || /* too many retries; */
322 st->nboxes > 65535) /* that's about 1MB of box structs. */
324 st->nboxes--; /* go into "fade out" mode now. */
325 st->growing_p = False;
326 return 2000000; /* make customizable... */
329 /* Pick colors for this box */
332 int w = st->image->width;
333 int h = st->image->height;
334 a->fill_color = XGetPixel (st->image, a->x % w, a->y % h);
338 int n = (st->color_horiz_p
339 ? (a->x * st->ncolors / st->xgwa.width)
340 : (a->y * st->ncolors / st->xgwa.height));
341 a->fill_color = st->colors [n % st->ncolors].pixel;
350 shrink_boxes (state *st)
355 for (i = 0; i < st->nboxes; i++)
357 box *a = &st->boxes[i];
359 if (a->w <= 0 || a->h <= 0) continue;
363 a->w -= st->inc + st->inc;
364 a->h -= st->inc + st->inc;
366 if (a->w < 0) a->w = 0;
367 if (a->h < 0) a->h = 0;
369 if (a->w > 0 && a->h > 0)
373 if (remaining == 0) {
383 draw_boxes (state *st)
386 for (i = 0; i < st->nboxes; i++)
388 box *b = &st->boxes[i];
390 if (b->flags & UNDEAD) continue;
391 if (! (b->flags & CHANGED)) continue;
392 b->flags &= ~CHANGED;
396 /* When shrinking, black out an area outside of the border
397 before re-drawing the box.
399 int margin = st->inc + st->border_size;
401 XSetForeground (st->dpy, st->gc, st->bg_color);
403 XFillArc (st->dpy, st->window, st->gc,
404 b->x - margin, b->y - margin,
405 b->w + (margin*2), b->h + (margin*2),
408 XFillRectangle (st->dpy, st->window, st->gc,
409 b->x - margin, b->y - margin,
410 b->w + (margin*2), b->h + (margin*2));
412 if (b->w <= 0 || b->h <= 0)
413 b->flags |= UNDEAD; /* really very dead now */
416 if (b->w <= 0 || b->h <= 0) continue;
418 XSetForeground (st->dpy, st->gc, b->fill_color);
421 XFillArc (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h,
424 XFillRectangle (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h);
426 if (st->border_size > 0)
428 unsigned int bd = (st->image
430 : st->colors [(b->fill_color + st->ncolors/2)
431 % st->ncolors].pixel);
432 XSetForeground (st->dpy, st->gc, bd);
434 XDrawArc (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h,
437 XDrawRectangle (st->dpy, st->window, st->gc,
438 b->x, b->y, b->w, b->h);
445 boxfit_draw (Display *dpy, Window window, void *closure)
447 state *st = (state *) closure;
450 if (st->img_loader) /* still loading */
452 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
453 if (! st->img_loader) /* just finished */
455 st->image = XGetImage (st->dpy,
456 (st->loading_pixmap ? st->loading_pixmap :
459 st->xgwa.width, st->xgwa.height, ~0L,
461 if (st->loading_pixmap) XFreePixmap (st->dpy, st->loading_pixmap);
462 XSetWindowBackground (st->dpy, st->window, st->bg_color);
463 if (st->loading_pixmap)
464 XClearWindow (st->dpy, st->window);
466 st->countdown = 2000000;
467 st->loading_pixmap = 0;
472 if (st->countdown > 0)
474 st->countdown -= st->delay;
475 if (st->countdown <= 0)
478 XClearWindow (st->dpy, st->window);
485 delay = grow_boxes (st);
487 delay = shrink_boxes (st);
494 boxfit_reshape (Display *dpy, Window window, void *closure,
495 unsigned int w, unsigned int h)
497 state *st = (state *) closure;
502 boxfit_event (Display *dpy, Window window, void *closure, XEvent *event)
504 state *st = (state *) closure;
505 if (event->xany.type == ButtonPress) {
506 st->growing_p = !st->growing_p;
513 boxfit_free (Display *dpy, Window window, void *closure)
518 static const char *boxfit_defaults [] = {
519 ".background: black",
520 ".foreground: #444444",
530 "*grabDesktopImages: False", /* HAVE_COCOA */
531 "*chooseRandomImages: True", /* HAVE_COCOA */
535 static XrmOptionDescRec boxfit_options [] = {
536 { "-delay", ".delay", XrmoptionSepArg, 0 },
537 { "-colors", ".colors", XrmoptionSepArg, 0 },
538 { "-count", ".boxCount", XrmoptionSepArg, 0 },
539 { "-growby", ".growBy", XrmoptionSepArg, 0 },
540 { "-spacing", ".spacing", XrmoptionSepArg, 0 },
541 { "-border", ".borderSize", XrmoptionSepArg, 0 },
542 { "-mode", ".mode", XrmoptionSepArg, 0 },
543 { "-circles", ".mode", XrmoptionNoArg, "circles" },
544 { "-squares", ".mode", XrmoptionNoArg, "squares" },
545 { "-random", ".mode", XrmoptionNoArg, "random" },
546 { "-grab", ".grab", XrmoptionNoArg, "True" },
547 { "-no-grab", ".grab", XrmoptionNoArg, "False" },
548 { "-peek", ".peek", XrmoptionNoArg, "True" },
549 { "-no-peek", ".peek", XrmoptionNoArg, "False" },
554 XSCREENSAVER_MODULE ("Boxfit", boxfit)