1 /* The Spiral Generator, Copyright (c) 2000
2 * by Rohit Singh <rohit_singh@hotmail.com>
4 * Contains code from / To be used with:
5 * xscreensaver, Copyright (c) 1992, 1995, 1996, 1997
6 * Jamie Zawinski <jwz@jwz.org>
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notices appear in all copies and that both that
11 * copyright notices and this permission notice appear in supporting
12 * documentation. No representations are made about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
18 #include "screenhack.h"
22 static int sleep_time;
23 static int num_layers;
24 static unsigned int default_fg_pixel;
27 init_tsg (Display *dpy, Window window)
31 XWindowAttributes xgwa;
33 XGetWindowAttributes (dpy, window, &xgwa);
35 gcv.foreground = default_fg_pixel =
36 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
37 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
38 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
43 go (Display *dpy, Window window,
44 int radius1, int radius2,
47 XWindowAttributes xgwa;
54 XGetWindowAttributes (dpy, window, &xgwa);
61 x1 = xmid + radius1 - radius2 + d;
65 for (theta = 1; theta < ( 360 * 100 ); theta++)
67 x2 = xmid + (( radius1 /* * * * * */
68 - radius2 ) /* This algo simulates */
69 * cos(( theta /* the rotation of a */
70 * M_PI ) /* circular disk inside */
71 / 180 )) /* a hollow circular */
72 + ( d /* rim. A point on the */
73 * cos(((( radius1 /* disk dist d from the */
74 * theta ) /* centre, traces the */
75 - delta ) /* path given by this */
76 / radius2 ) /* equation. */
77 * M_PI /* A deviation (error) */
78 / 180 ) /* of delta needs to be */
79 ); /* given, which greatly */
80 /* adds to the beauty */
81 y2 = ymid + ( /* of the figure. */
82 ( radius1 - radius2 /* */
83 ) * sin /* Imperfection adds to */
84 ( /* beauty, symbolically */
85 ( theta * M_PI /* ... */
86 ) / 180 /* Algo deduced by */
87 ) /* Rohit Singh, Jan'00 */
88 ) + /* based on a toy he */
89 ( d * sin /* used to play with */
90 ( /* when he was a kid. */
101 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
110 #define min(a,b) ((a)<(b)?(a):(b))
113 getset (Display *dpy, Window window, XColor *color, Bool *got_color)
117 int radius, radius1, radius2;
119 XWindowAttributes xgwa;
123 XGetWindowAttributes (dpy, window, &xgwa);
125 height = xgwa.height;
126 cmap = xgwa.colormap;
128 radius = min (width, height) / 2;
130 XClearWindow (dpy, window);
133 for(counter = 0; counter < num_layers; counter ++)
135 divisor = ((frand (3.0) + 1) * (((random() & 1) * 2) - 1));
138 radius2 = radius / divisor + 5;
139 distance = 100 + (random() % 200);
142 XSetForeground (dpy, draw_gc, default_fg_pixel);
145 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
146 &color->red, &color->green, &color->blue);
147 if ((*got_color = XAllocColor (dpy, cmap, color)))
148 XSetForeground (dpy, draw_gc, color->pixel);
150 XSetForeground (dpy, draw_gc, default_fg_pixel);
152 go (dpy, window, radius1, -radius2, distance);
154 /* once again, with a parameter negated, just for kicks */
156 XSetForeground (dpy, draw_gc, default_fg_pixel);
159 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
160 &color->red, &color->green, &color->blue);
161 if ((*got_color = XAllocColor (dpy, cmap, color)))
162 XSetForeground (dpy, draw_gc, color->pixel);
164 XSetForeground (dpy, draw_gc, default_fg_pixel);
166 go (dpy, window, radius1, radius2, distance);
171 getset_go (Display *dpy, Window window)
173 Bool free_color = False;
177 XWindowAttributes xgwa;
180 XGetWindowAttributes (dpy, window, &xgwa);
183 height = xgwa.height;
184 cmap = xgwa.colormap;
186 getset(dpy, window, &color, &free_color);
189 screenhack_handle_events (dpy);
190 sleep ( sleep_time );
192 screenhack_handle_events (dpy);
193 erase_full_window(dpy, window);
195 if (free_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
197 screenhack_handle_events (dpy);
201 char *progclass = "XSpiroGraph";
203 char *defaults [] = {
204 ".background: black",
210 XrmOptionDescRec options [] = {
211 { "-delay", ".delay", XrmoptionSepArg, 0 },
212 { "-layers", ".layers", XrmoptionSepArg, 0 },
215 int options_size = (sizeof (options) / sizeof (options[0]));
218 screenhack (Display *dpy, Window window)
220 sleep_time = get_integer_resource("delay", "Integer");
221 num_layers = get_integer_resource("layers", "Integer");
222 init_tsg (dpy, window);
224 getset_go (dpy, window);