1 /* xscreensaver, Copyright (c) 2005 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 <X11/Xutil.h>
26 unsigned long fill_color;
34 XWindowAttributes xgwa;
36 unsigned long bg_color;
56 reset_boxes (state *st)
58 static Bool once = False;
63 st->color_horiz_p = random() & 1;
66 free_colors (st->dpy, st->xgwa.colormap, st->colors, st->ncolors);
70 char *s = get_string_resource ("mode", "Mode");
71 if (!s || !*s || !strcasecmp (s, "random"))
73 else if (!strcasecmp (s, "squares") || !strcasecmp (s, "square"))
75 else if (!strcasecmp (s, "circles") || !strcasecmp (s, "circle"))
80 "%s: mode must be random, squares, or circles, not '%s'\n",
87 st->circles_p = random() & 1;
89 st->circles_p = (mode == 1);
93 st->ncolors = get_integer_resource ("colors", "Colors"); /* re-get this */
94 make_smooth_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
95 st->colors, &st->ncolors, True, 0, False);
96 XClearWindow (st->dpy, st->window);
101 init_boxes (Display *dpy, Window window)
104 state *st = (state *) calloc (1, sizeof (*st));
109 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
110 XSelectInput (dpy, window, st->xgwa.your_event_mask | ExposureMask);
112 st->ncolors = get_integer_resource ("colors", "Colors");
113 if (st->ncolors < 1) st->ncolors = 1;
114 st->colors = (XColor *) malloc (sizeof(XColor) * st->ncolors);
116 st->inc = get_integer_resource ("growBy", "GrowBy");
117 st->spacing = get_integer_resource ("spacing", "Spacing");
118 st->border_size = get_integer_resource ("borderSize", "BorderSize");
119 st->bg_color = get_pixel_resource ("background", "Background",
120 st->dpy, st->xgwa.colormap);
121 if (st->inc < 1) st->inc = 1;
122 if (st->border_size < 0) st->border_size = 0;
124 gcv.line_width = st->border_size;
125 gcv.background = st->bg_color;
126 st->gc = XCreateGC (st->dpy, st->window, GCBackground|GCLineWidth, &gcv);
128 st->box_count = get_integer_resource ("boxCount", "BoxCount");
129 if (st->box_count < 1) st->box_count = 1;
132 st->boxes_size = st->box_count * 2;
133 st->boxes = (box *) calloc (st->boxes_size, sizeof(*st->boxes));
141 reshape_boxes (state *st)
144 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
145 for (i = 0; i < st->nboxes; i++)
147 box *b = &st->boxes[i];
154 boxes_overlap_p (box *a, box *b, int pad)
156 /* Two rectangles overlap if the max of the tops is less than the
157 min of the bottoms and the max of the lefts is less than the min
162 # define MAX(A,B) ((A)>(B)?(A):(B))
163 # define MIN(A,B) ((A)<(B)?(A):(B))
165 int maxleft = MAX(a->x - pad, b->x);
166 int maxtop = MAX(a->y - pad, b->y);
167 int minright = MIN(a->x + a->w + pad + pad - 1, b->x + b->w);
168 int minbot = MIN(a->y + a->h + pad + pad - 1, b->y + b->h);
169 return (maxtop < minbot && maxleft < minright);
174 circles_overlap_p (box *a, box *b, int pad)
176 int ar = a->w/2; /* radius */
178 int ax = a->x + ar; /* center */
182 int d2 = (((bx - ax) * (bx - ax)) + /* distance between centers squared */
183 ((by - ay) * (by - ay)));
184 int r2 = ((ar + br + pad) * /* sum of radii squared */
191 box_collides_p (state *st, box *a, int pad)
195 /* collide with wall */
196 if (a->x - pad < 0 ||
198 a->x + a->w + pad + pad >= st->xgwa.width ||
199 a->y + a->h + pad + pad >= st->xgwa.height)
202 /* collide with another box */
203 for (i = 0; i < st->nboxes; i++)
205 box *b = &st->boxes[i];
208 ? circles_overlap_p (a, b, pad)
209 : boxes_overlap_p (a, b, pad)))
218 grow_boxes (state *st)
220 int inc2 = st->inc + st->spacing + st->border_size;
224 /* check box collisions, and grow if none.
226 for (i = 0; i < st->nboxes; i++)
228 box *a = &st->boxes[i];
229 if (!(a->flags & ALIVE)) continue;
231 if (box_collides_p (st, a, inc2))
240 a->w += st->inc + st->inc;
241 a->h += st->inc + st->inc;
247 while (live_count < st->box_count)
251 if (st->boxes_size <= st->nboxes)
253 st->boxes_size = (st->boxes_size * 1.2) + st->nboxes;
255 realloc (st->boxes, st->boxes_size * sizeof(*st->boxes));
258 fprintf (stderr, "%s: out of memory (%d boxes)\n",
259 progname, st->boxes_size);
264 a = &st->boxes[st->nboxes-1];
267 for (i = 0; i < 10000; i++)
269 a->x = inc2 + (random() % (st->xgwa.width - inc2));
270 a->y = inc2 + (random() % (st->xgwa.height - inc2));
274 if (! box_collides_p (st, a, inc2))
282 if (! (a->flags & ALIVE) || /* too many retries; */
283 st->nboxes > 65535) /* that's about 1MB of box structs. */
285 st->nboxes--; /* go into "fade out" mode now. */
286 st->growing_p = False;
288 XSync (st->dpy, False);
294 /* Pick colors for this box */
296 int n = (st->color_horiz_p
297 ? (a->x * st->ncolors / st->xgwa.width)
298 : (a->y * st->ncolors / st->xgwa.height));
299 a->fill_color = st->colors [n % st->ncolors].pixel;
306 shrink_boxes (state *st)
311 for (i = 0; i < st->nboxes; i++)
313 box *a = &st->boxes[i];
315 if (a->w <= 0 || a->h <= 0) continue;
319 a->w -= st->inc + st->inc;
320 a->h -= st->inc + st->inc;
322 if (a->w < 0) a->w = 0;
323 if (a->h < 0) a->h = 0;
325 if (a->w > 0 && a->h > 0)
335 draw_boxes (state *st)
338 for (i = 0; i < st->nboxes; i++)
340 box *b = &st->boxes[i];
344 /* When shrinking, black out an area outside of the border
345 before re-drawing the box.
347 XSetForeground (st->dpy, st->gc, st->bg_color);
348 XSetLineAttributes (st->dpy, st->gc,
349 (st->inc + st->border_size) * 2,
350 LineSolid, CapButt, JoinMiter);
353 XDrawArc (st->dpy, st->window, st->gc,
355 (b->w > 0 ? b->w : 1),
356 (b->h > 0 ? b->h : 1),
359 XDrawRectangle (st->dpy, st->window, st->gc,
361 (b->w > 0 ? b->w : 1),
362 (b->h > 0 ? b->h : 1));
363 XSetLineAttributes (st->dpy, st->gc, st->border_size,
364 LineSolid, CapButt, JoinMiter);
367 if (b->w <= 0 || b->h <= 0) continue;
368 if (! (b->flags & CHANGED)) continue;
369 b->flags &= ~CHANGED;
371 XSetForeground (st->dpy, st->gc, b->fill_color);
374 XFillArc (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h,
377 XFillRectangle (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h);
379 if (st->border_size > 0)
381 unsigned long bd = st->colors [(b->fill_color + st->ncolors/2)
382 % st->ncolors].pixel;
383 XSetForeground (st->dpy, st->gc, bd);
385 XDrawArc (st->dpy, st->window, st->gc, b->x, b->y, b->w, b->h,
388 XDrawRectangle (st->dpy, st->window, st->gc,
389 b->x, b->y, b->w, b->h);
395 handle_events (state *st)
397 XSync (st->dpy, False);
398 while (XPending (st->dpy))
401 XNextEvent (st->dpy, &event);
402 if (event.xany.type == ConfigureNotify ||
403 event.xany.type == Expose)
405 else if (event.xany.type == ButtonPress)
406 st->growing_p = !st->growing_p;
408 screenhack_handle_event (st->dpy, &event);
414 char *progclass = "BoxFit";
416 char *defaults [] = {
417 ".background: black",
428 XrmOptionDescRec options [] = {
429 { "-delay", ".delay", XrmoptionSepArg, 0 },
430 { "-colors", ".colors", XrmoptionSepArg, 0 },
431 { "-count", ".boxCount", XrmoptionSepArg, 0 },
432 { "-growby", ".growBy", XrmoptionSepArg, 0 },
433 { "-spacing", ".spacing", XrmoptionSepArg, 0 },
434 { "-border", ".borderSize", XrmoptionSepArg, 0 },
435 { "-circles", ".mode", XrmoptionNoArg, "circles" },
436 { "-squares", ".mode", XrmoptionNoArg, "squares" },
437 { "-random", ".mode", XrmoptionNoArg, "random" },
443 screenhack (Display *dpy, Window window)
445 state *st = init_boxes (dpy, window);
446 int delay = get_integer_resource ("delay", "Integer");
457 if (delay) usleep (delay);