1 /* wander, by Rick Campbell <rick@campbellcentral.org>, 19 December 1998.
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
14 #include "screenhack.h"
18 #define MAXIMUM_COLOR_COUNT (256)
29 XColor colors [MAXIMUM_COLOR_COUNT];
40 int x, y, last_x, last_y, width_1, height_1, length_limit, reset_limit;
49 wander_init (Display *dpy, Window window)
51 struct state *st = (struct state *) calloc (1, sizeof(*st));
53 XWindowAttributes attributes;
57 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
59 XClearWindow (st->dpy, st->window);
60 XGetWindowAttributes (st->dpy, st->window, &attributes);
61 st->width = attributes.width;
62 st->height = attributes.height;
63 st->depth = attributes.depth;
64 st->color_map = attributes.colormap;
67 free_colors (attributes.screen, st->color_map,
68 st->colors, st->color_count);
71 st->context = XCreateGC (st->dpy, st->window, 0, &values);
72 st->color_count = MAXIMUM_COLOR_COUNT;
73 make_color_loop (attributes.screen, attributes.visual, st->color_map,
77 st->colors, &st->color_count, True, False);
78 if (st->color_count <= 0)
81 st->colors [0].red = st->colors [0].green = st->colors [0].blue = 0;
82 st->colors [1].red = st->colors [1].green = st->colors [1].blue = 0xFFFF;
83 XAllocColor (st->dpy, st->color_map, &st->colors [0]);
84 XAllocColor (st->dpy, st->color_map, &st->colors [1]);
86 st->color_index = random () % st->color_count;
88 st->advance = get_integer_resource (st->dpy, "advance", "Integer");
89 st->density = get_integer_resource (st->dpy, "density", "Integer");
90 if (st->density < 1) st->density = 1;
91 st->reset = get_integer_resource (st->dpy, "reset", "Integer");
92 if (st->reset < 100) st->reset = 100;
93 st->circles = get_boolean_resource (st->dpy, "circles", "Boolean");
94 st->size = get_integer_resource (st->dpy, "size", "Integer");
95 if (st->size < 1) st->size = 1;
96 st->width = st->width / st->size;
97 st->height = st->height / st->size;
98 st->length = get_integer_resource (st->dpy, "length", "Integer");
99 if (st->length < 1) st->length = 1;
100 XSetForeground (st->dpy, st->context, st->colors [st->color_index].pixel);
103 st->x = random () % st->width;
104 st->y = random () % st->height;
107 st->width_1 = st->width - 1;
108 st->height_1 = st->height - 1;
109 st->length_limit = st->length;
110 st->reset_limit = st->reset;
111 st->color_index = random () % st->color_count;
112 st->color = st->colors [random () % st->color_count].pixel;
113 st->pixmap = XCreatePixmap (st->dpy, window, st->size,
114 st->size, st->depth);
116 XSetForeground (st->dpy, st->context,
117 BlackPixel (st->dpy, DefaultScreen (st->dpy)));
118 XFillRectangle (st->dpy, st->pixmap, st->context, 0, 0,
119 st->width * st->size, st->height * st->size);
120 XSetForeground (st->dpy, st->context, st->color);
121 XFillArc (st->dpy, st->pixmap, st->context, 0, 0, st->size, st->size, 0, 360*64);
128 wander_draw (Display *dpy, Window window, void *closure)
130 struct state *st = (struct state *) closure;
134 st->eraser = erase_window (st->dpy, st->window, st->eraser);
138 for (i = 0; i < 2000; i++)
140 if (random () % st->density)
149 st->x = (st->x + st->width_1 + (random () % 3)) % st->width;
150 st->y = (st->y + st->height_1 + (random () % 3)) % st->height;
153 if ((random () % st->length_limit) == 0)
155 if (st->advance == 0)
157 st->color_index = random () % st->color_count;
161 st->color_index = (st->color_index + st->advance) % st->color_count;
163 st->color = st->colors [st->color_index].pixel;
164 XSetForeground (st->dpy, st->context, st->color);
167 XFillArc (st->dpy, st->pixmap, st->context,
168 0, 0, st->size, st->size, 0, 360 * 64);
172 if ((random () % st->reset_limit) == 0)
174 st->eraser = erase_window (st->dpy, st->window, st->eraser);
175 st->color = st->colors [random () % st->color_count].pixel;
176 st->x = random () % st->width;
177 st->y = random () % st->height;
182 XFillArc (st->dpy, st->pixmap, st->context, 0, 0, st->size, st->size, 0, 360*64);
188 XDrawPoint (st->dpy, st->window, st->context, st->x, st->y);
194 XCopyArea (st->dpy, st->pixmap, st->window, st->context, 0, 0, st->size, st->size,
195 st->x * st->size, st->y * st->size);
199 XFillRectangle (st->dpy, st->window, st->context, st->x * st->size, st->y * st->size,
211 wander_reshape (Display *dpy, Window window, void *closure,
212 unsigned int w, unsigned int h)
214 struct state *st = (struct state *) closure;
215 st->width = w / st->size;
216 st->height = h / st->size;
217 st->width_1 = st->width - 1;
218 st->height_1 = st->height - 1;
222 wander_event (Display *dpy, Window window, void *closure, XEvent *event)
228 wander_free (Display *dpy, Window window, void *closure)
232 static const char *wander_defaults [] =
234 ".background: black",
235 ".foreground: white",
245 "*ignoreRotation: True",
250 static XrmOptionDescRec wander_options [] =
252 { "-advance", ".advance", XrmoptionSepArg, 0 },
253 { "-circles", ".circles", XrmoptionNoArg, "True" },
254 { "-no-circles",".circles", XrmoptionNoArg, "False" },
255 { "-density", ".density", XrmoptionSepArg, 0 },
256 { "-length", ".length", XrmoptionSepArg, 0 },
257 { "-delay", ".delay", XrmoptionSepArg, 0 },
258 { "-reset", ".reset", XrmoptionSepArg, 0 },
259 { "-size", ".size", XrmoptionSepArg, 0 },
264 XSCREENSAVER_MODULE ("Wander", wander)