From http://www.jwz.org/xscreensaver/xscreensaver-5.22.tar.gz
[xscreensaver] / hacks / wander.c
1 /* wander, by Rick Campbell <rick@campbellcentral.org>, 19 December 1998.
2  *
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 
9  * implied warranty.
10  */
11
12 #include <stdio.h>
13
14 #include "screenhack.h"
15 #include "colors.h"
16 #include "erase.h"
17
18 #define MAXIMUM_COLOR_COUNT (256)
19
20 struct state {
21   Display *dpy;
22   Window window;
23
24    unsigned int advance;
25    Bool         circles;
26    Colormap     color_map;
27    int          color_count;
28    int          color_index;
29    XColor       colors      [MAXIMUM_COLOR_COUNT];
30    GC           context;
31    unsigned int density;
32    int          depth;
33    int          height;
34    unsigned int length;
35    unsigned int reset;
36    unsigned int size;
37    int          width;
38    int delay;
39
40   int x, y, last_x, last_y, width_1, height_1, length_limit, reset_limit;
41   unsigned long color;
42   Pixmap pixmap;
43
44   eraser_state *eraser;
45 };
46
47
48 static void *
49 wander_init (Display *dpy, Window window)
50 {
51     struct state *st = (struct state *) calloc (1, sizeof(*st));
52     XGCValues values;
53     XWindowAttributes attributes;
54
55     st->dpy = dpy;
56     st->window = window;
57     st->delay = get_integer_resource (st->dpy, "delay", "Integer");
58
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;
65     if (st->color_count)
66     {
67         free_colors (attributes.screen, st->color_map,
68                      st->colors, st->color_count);
69         st->color_count = 0;
70     }
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,
74                     0,   1, 1,
75                     120, 1, 1,
76                     240, 1, 1,
77                     st->colors, &st->color_count, True, False);
78     if (st->color_count <= 0)
79     {
80         st->color_count = 2;
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]);
85     }
86     st->color_index = random () % st->color_count;
87     
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);
101
102
103     st->x = random () % st->width;
104     st->y = random () % st->height;
105     st->last_x = st->x;
106     st->last_y = st->y;
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);
115
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);
122
123     return st;
124 }
125
126
127 static unsigned long
128 wander_draw (Display *dpy, Window window, void *closure)
129 {
130   struct state *st = (struct state *) closure;
131   int i;
132
133   if (st->eraser) {
134     st->eraser = erase_window (st->dpy, st->window, st->eraser);
135     goto END;
136   }
137
138   for (i = 0; i < 2000; i++)
139     {
140       if (random () % st->density)
141         {
142           st->x = st->last_x;
143           st->y = st->last_y;
144         }
145       else
146         {
147           st->last_x = st->x;
148           st->last_y = st->y;
149           st->x = (st->x + st->width_1  + (random () % 3)) % st->width;
150           st->y = (st->y + st->height_1 + (random () % 3)) % st->height;
151         }
152
153       if ((random () % st->length_limit) == 0)
154         {
155           if (st->advance == 0)
156             {
157               st->color_index = random () % st->color_count;
158             }
159           else
160             {
161               st->color_index = (st->color_index + st->advance) % st->color_count;
162             }
163           st->color = st->colors [st->color_index].pixel;
164           XSetForeground (st->dpy, st->context, st->color);
165           if (st->circles)
166             {
167               XFillArc (st->dpy, st->pixmap, st->context,
168                         0, 0, st->size, st->size, 0, 360 * 64);
169             }
170         }
171
172       if ((random () % st->reset_limit) == 0)
173         {
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;
178           st->last_x = st->x;
179           st->last_y = st->y;
180           if (st->circles)
181             {
182               XFillArc (st->dpy, st->pixmap, st->context, 0, 0, st->size, st->size, 0, 360*64);
183             }
184         }
185
186       if (st->size == 1)
187         {
188           XDrawPoint (st->dpy, st->window, st->context, st->x, st->y);
189         }
190       else
191         {
192           if (st->circles)
193             {
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);
196             }
197           else
198             {
199               XFillRectangle (st->dpy, st->window, st->context, st->x * st->size, st->y * st->size,
200                               st->size, st->size);
201             }
202         }
203     }
204
205  END:
206   return st->delay;
207 }
208
209
210 static void
211 wander_reshape (Display *dpy, Window window, void *closure, 
212                  unsigned int w, unsigned int h)
213 {
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;
219 }
220
221 static Bool
222 wander_event (Display *dpy, Window window, void *closure, XEvent *event)
223 {
224   return False;
225 }
226
227 static void
228 wander_free (Display *dpy, Window window, void *closure)
229 {
230 }
231
232 static const char *wander_defaults [] =
233 {
234     ".background: black",
235     ".foreground: white",
236     ".fpsSolid:   true",
237     ".advance:    1",
238     ".density:    2",
239     ".length:     25000",
240     ".delay:      20000",
241     ".reset:      2500000",
242     ".circles:    False",
243     ".size:       1",
244 #ifdef USE_IPHONE
245   "*ignoreRotation: True",
246 #endif
247     0
248 };
249
250 static XrmOptionDescRec wander_options [] =
251 {
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 },
260     { 0, 0, 0, 0 }
261 };
262
263
264 XSCREENSAVER_MODULE ("Wander", wander)