1 /* xscreensaver, Copyright (c) 1997-2013 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 * Concept snarfed from Michael D. Bayne in
12 * http://www.go2net.com/internet/deep/1997/04/16/body.html
14 * Changes by Lars Huttar, http://www.huttar.net:
15 * - allow use of golden ratio for dividing rectangles instead of 1/2.
16 * - allow smooth colors instead of random
17 * - added line thickness setting
18 * - added "Mondrian" mode
20 * - allow recomputing the colormap on each new frame (especially useful
21 * when ncolors is low)
24 #include "screenhack.h"
40 XWindowAttributes xgwa;
46 * Suppose you're dividing a rectangle of length A+B
47 * into two parts, of length A and B respectively. You want the ratio of
48 * A to B to be the same as the ratio of the whole (A+B) to A. The golden
49 * ratio (phi) is that ratio. Supposed to be visually pleasing. */
51 #define PHI1 (1.0/PHI)
52 #define PHI2 (1.0 - PHI1)
54 /* copied from make_random_colormap in colors.c */
56 make_mondrian_colormap (Screen *screen, Visual *visual, Colormap cmap,
57 XColor *colors, int *ncolorsP,
62 Display *dpy = DisplayOfScreen (screen);
63 Bool wanted_writable = (allocate_p && writable_pP && *writable_pP);
67 if (*ncolorsP <= 0) return;
69 /* If this visual doesn't support writable cells, don't bother trying. */
70 if (wanted_writable && !has_writable_cells(screen, visual))
73 for (i = 0; i < ncolors; i++)
75 colors[i].flags = DoRed|DoGreen|DoBlue;
81 case 0: case 1: case 2: case 3: case 7: /* white */
82 colors[i].red = 0xE800;
83 colors[i].green = 0xE800;
84 colors[i].blue = 0xE800;
87 colors[i].red = 0xCFFF; break; /* red */
89 colors[i].red = 0x2000;
90 colors[i].blue = 0xCFFF; break; /* blue */
92 colors[i].red = 0xDFFF; /* yellow */
93 colors[i].green = 0xCFFF; break;
101 if (writable_pP && *writable_pP)
103 unsigned long *pixels = (unsigned long *)
104 malloc(sizeof(*pixels) * (ncolors + 1));
106 allocate_writable_colors (screen, cmap, pixels, &ncolors);
108 for (i = 0; i < ncolors; i++)
109 colors[i].pixel = pixels[i];
112 XStoreColors (dpy, cmap, colors, ncolors);
116 for (i = 0; i < ncolors; i++)
120 if (!XAllocColor (dpy, cmap, &color))
122 colors[i].pixel = color.pixel;
127 /* If we tried for writable cells and got none, try for non-writable. */
128 if (allocate_p && ncolors == 0 && writable_pP && *writable_pP)
131 *writable_pP = False;
132 goto RETRY_NON_WRITABLE;
136 /* I don't think we need to bother copying or linking to the complain
139 complain(*ncolorsP, ncolors, wanted_writable,
140 wanted_writable && *writable_pP);
147 mondrian_set_sizes (struct state *st, int w, int h)
150 st->line_width = w/50;
151 st->min_height = st->min_width = w/8;
153 st->line_width = h/50;
154 st->min_height = st->min_width = h/8;
159 deco (Display *dpy, Window window, struct state *st,
160 int x, int y, int w, int h, int depth)
162 if (((random() % st->max_depth) < depth) || (w < st->min_width) || (h < st->min_height))
166 if (++st->current_color >= st->ncolors)
167 st->current_color = 0;
168 XSetForeground(dpy, st->bgc, st->colors[st->current_color].pixel);
170 XFillRectangle (dpy, window, st->bgc, x, y, w, h);
171 XDrawRectangle (dpy, window, st->fgc, x, y, w, h);
175 if ((st->goldenRatio || st->mondrian) ? (w > h) : (random() & 1))
176 { /* Divide the rectangle side-by-side */
177 int wnew = (st->goldenRatio ? (w * (random() & 1 ? PHI1 : PHI2)) : w/2);
178 deco (dpy, window, st, x, y, wnew, h, depth+1);
179 deco (dpy, window, st, x+wnew, y, w-wnew, h, depth+1);
182 { /* Divide the rectangle top-to-bottom */
183 int hnew = (st->goldenRatio ? (h * (random() & 1 ? PHI1 : PHI2)) : h/2);
184 deco (dpy, window, st, x, y, w, hnew, depth+1);
185 deco (dpy, window, st, x, y+hnew, w, h-hnew, depth+1);
191 deco_init (Display *dpy, Window window)
193 struct state *st = (struct state *) calloc (1, sizeof(*st));
196 st->delay = get_integer_resource (dpy, "delay", "Integer");
198 st->smoothColors = get_boolean_resource(dpy, "smoothColors", "Boolean");
199 st->old_line_width = 1;
201 st->goldenRatio = get_boolean_resource (dpy, "goldenRatio", "Boolean");
203 st->max_depth = get_integer_resource (dpy, "maxDepth", "Integer");
204 if (st->max_depth < 1) st->max_depth = 1;
205 else if (st->max_depth > 1000) st->max_depth = 1000;
207 st->min_width = get_integer_resource (dpy, "minWidth", "Integer");
208 if (st->min_width < 2) st->min_width = 2;
209 st->min_height = get_integer_resource (dpy, "minHeight", "Integer");
210 if (st->min_height < 2) st->min_height = 2;
212 st->line_width = get_integer_resource (dpy, "lineWidth", "Integer");
214 XGetWindowAttributes (dpy, window, &st->xgwa);
216 st->ncolors = get_integer_resource (dpy, "ncolors", "Integer");
218 gcv.foreground = get_pixel_resource(dpy, st->xgwa.colormap,
219 "foreground", "Foreground");
220 st->fgc = XCreateGC (dpy, window, GCForeground, &gcv);
222 gcv.foreground = get_pixel_resource(dpy, st->xgwa.colormap,
223 "background", "Background");
224 st->bgc = XCreateGC (dpy, window, GCForeground, &gcv);
226 if (st->ncolors <= 2)
236 st->mondrian = get_boolean_resource(dpy, "mondrian", "Boolean");
238 /* Mondrian, if true, overrides several other options. */
239 mondrian_set_sizes(st, st->xgwa.width, st->xgwa.height);
241 /** set up red-yellow-blue-black-white colormap and fgc **/
242 make_mondrian_colormap(st->xgwa.screen, st->xgwa.visual,
244 st->colors, &st->ncolors, True, 0, True);
246 /** put white in several cells **/
247 /** set min-height and min-width to about 10% of total w/h **/
249 else if (st->smoothColors)
250 make_smooth_colormap (st->xgwa.screen, st->xgwa.visual,
252 st->colors, &st->ncolors, True, 0, True);
254 make_random_colormap (st->xgwa.screen, st->xgwa.visual,
256 st->colors, &st->ncolors, False, True, 0, True);
258 gcv.line_width = st->old_line_width = st->line_width;
259 XChangeGC(dpy, st->fgc, GCLineWidth, &gcv);
265 deco_draw (Display *dpy, Window window, void *closure)
267 struct state *st = (struct state *) closure;
268 XFillRectangle (dpy, window, st->bgc, 0, 0, st->xgwa.width, st->xgwa.height);
270 mondrian_set_sizes(st, st->xgwa.width, st->xgwa.height);
271 if (st->line_width != st->old_line_width) {
272 XSetLineAttributes(dpy, st->fgc, st->line_width,
273 LineSolid, CapButt, JoinBevel);
274 st->old_line_width = st->line_width;
277 deco (dpy, window, st, 0, 0, st->xgwa.width, st->xgwa.height, 0);
278 return 1000000 * st->delay;
282 deco_reshape (Display *dpy, Window window, void *closure,
283 unsigned int w, unsigned int h)
285 struct state *st = (struct state *) closure;
291 deco_event (Display *dpy, Window window, void *closure, XEvent *event)
297 deco_free (Display *dpy, Window window, void *closure)
299 struct state *st = (struct state *) closure;
304 static const char *deco_defaults [] = {
305 ".background: black",
306 ".foreground: white",
313 "*goldenRatio: False",
314 "*smoothColors: False",
317 "*ignoreRotation: True",
322 static XrmOptionDescRec deco_options [] = {
323 { "-max-depth", ".maxDepth", XrmoptionSepArg, 0 },
324 { "-min-width", ".minWidth", XrmoptionSepArg, 0 },
325 { "-min-height", ".minHeight", XrmoptionSepArg, 0 },
326 { "-line-width", ".lineWidth", XrmoptionSepArg, 0 },
327 { "-delay", ".delay", XrmoptionSepArg, 0 },
328 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
329 { "-golden-ratio", ".goldenRatio", XrmoptionNoArg, "True" },
330 { "-no-golden-ratio", ".goldenRatio", XrmoptionNoArg, "False" },
331 { "-smooth-colors", ".smoothColors",XrmoptionNoArg, "True" },
332 { "-no-smooth-colors",".smoothColors",XrmoptionNoArg, "False" },
333 { "-mondrian", ".mondrian", XrmoptionNoArg, "True" },
334 { "-no-mondrian", ".mondrian", XrmoptionNoArg, "False" },
338 XSCREENSAVER_MODULE ("Deco", deco)