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 (st->dpy, st->color_map, st->colors, st->color_count);
70 st->context = XCreateGC (st->dpy, st->window, 0, &values);
71 st->color_count = MAXIMUM_COLOR_COUNT;
72 make_color_loop (st->dpy, st->color_map,
76 st->colors, &st->color_count, True, False);
77 if (st->color_count <= 0)
80 st->colors [0].red = st->colors [0].green = st->colors [0].blue = 0;
81 st->colors [1].red = st->colors [1].green = st->colors [1].blue = 0xFFFF;
82 XAllocColor (st->dpy, st->color_map, &st->colors [0]);
83 XAllocColor (st->dpy, st->color_map, &st->colors [1]);
85 st->color_index = random () % st->color_count;
87 st->advance = get_integer_resource (st->dpy, "advance", "Integer");
88 st->density = get_integer_resource (st->dpy, "density", "Integer");
89 if (st->density < 1) st->density = 1;
90 st->reset = get_integer_resource (st->dpy, "reset", "Integer");
91 if (st->reset < 100) st->reset = 100;
92 st->circles = get_boolean_resource (st->dpy, "circles", "Boolean");
93 st->size = get_integer_resource (st->dpy, "size", "Integer");
94 if (st->size < 1) st->size = 1;
95 st->width = st->width / st->size;
96 st->height = st->height / st->size;
97 st->length = get_integer_resource (st->dpy, "length", "Integer");
98 if (st->length < 1) st->length = 1;
99 XSetForeground (st->dpy, st->context, st->colors [st->color_index].pixel);
102 st->x = random () % st->width;
103 st->y = random () % st->height;
106 st->width_1 = st->width - 1;
107 st->height_1 = st->height - 1;
108 st->length_limit = st->length;
109 st->reset_limit = st->reset;
110 st->color_index = random () % st->color_count;
111 st->color = st->colors [random () % st->color_count].pixel;
112 st->pixmap = XCreatePixmap (st->dpy, window, st->size,
113 st->size, st->depth);
115 XSetForeground (st->dpy, st->context,
116 BlackPixel (st->dpy, DefaultScreen (st->dpy)));
117 XFillRectangle (st->dpy, st->pixmap, st->context, 0, 0,
118 st->width * st->size, st->height * st->size);
119 XSetForeground (st->dpy, st->context, st->color);
120 XFillArc (st->dpy, st->pixmap, st->context, 0, 0, st->size, st->size, 0, 360*64);
127 wander_draw (Display *dpy, Window window, void *closure)
129 struct state *st = (struct state *) closure;
133 st->eraser = erase_window (st->dpy, st->window, st->eraser);
137 for (i = 0; i < 2000; i++)
139 if (random () % st->density)
148 st->x = (st->x + st->width_1 + (random () % 3)) % st->width;
149 st->y = (st->y + st->height_1 + (random () % 3)) % st->height;
152 if ((random () % st->length_limit) == 0)
154 if (st->advance == 0)
156 st->color_index = random () % st->color_count;
160 st->color_index = (st->color_index + st->advance) % st->color_count;
162 st->color = st->colors [st->color_index].pixel;
163 XSetForeground (st->dpy, st->context, st->color);
166 XFillArc (st->dpy, st->pixmap, st->context,
167 0, 0, st->size, st->size, 0, 360 * 64);
171 if ((random () % st->reset_limit) == 0)
173 st->eraser = erase_window (st->dpy, st->window, st->eraser);
174 st->color = st->colors [random () % st->color_count].pixel;
175 st->x = random () % st->width;
176 st->y = random () % st->height;
181 XFillArc (st->dpy, st->pixmap, st->context, 0, 0, st->size, st->size, 0, 360*64);
187 XDrawPoint (st->dpy, st->window, st->context, st->x, st->y);
193 XCopyArea (st->dpy, st->pixmap, st->window, st->context, 0, 0, st->size, st->size,
194 st->x * st->size, st->y * st->size);
198 XFillRectangle (st->dpy, st->window, st->context, st->x * st->size, st->y * st->size,
210 wander_reshape (Display *dpy, Window window, void *closure,
211 unsigned int w, unsigned int h)
216 wander_event (Display *dpy, Window window, void *closure, XEvent *event)
222 wander_free (Display *dpy, Window window, void *closure)
226 static const char *wander_defaults [] =
228 ".background: black",
229 ".foreground: white",
241 static XrmOptionDescRec wander_options [] =
243 { "-advance", ".advance", XrmoptionSepArg, 0 },
244 { "-circles", ".circles", XrmoptionNoArg, "True" },
245 { "-no-circles",".circles", XrmoptionNoArg, "False" },
246 { "-density", ".density", XrmoptionSepArg, 0 },
247 { "-length", ".length", XrmoptionSepArg, 0 },
248 { "-delay", ".delay", XrmoptionSepArg, 0 },
249 { "-reset", ".reset", XrmoptionSepArg, 0 },
250 { "-size", ".size", XrmoptionSepArg, 0 },
255 XSCREENSAVER_MODULE ("Wander", wander)