1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.com>
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
12 /* This file was ported from xlock for use in xscreensaver (and standalone)
13 * by jwz on 12-Aug-92. Original copyright reads:
15 * hopalong.c - Real Plane Fractals for xlock, the X Window System lockscreen.
17 * Copyright (c) 1991 by Patrick J. Naughton.
19 * Permission to use, copy, modify, and distribute this software and its
20 * documentation for any purpose and without fee is hereby granted,
21 * provided that the above copyright notice appear in all copies and that
22 * both that copyright notice and this permission notice appear in
23 * supporting documentation.
25 * This file is provided AS IS with no warranties of any kind. The author
26 * shall have no liability with respect to the infringement of copyrights,
27 * trade secrets or any patents by this file or any part thereof. In no
28 * event will the author be liable for any lost revenue or profits or
29 * other special, indirect and consequential damages.
31 * Comments and additions should be sent to the author:
33 * naughton@eng.sun.com
37 * Sun Laboritories, Inc.
39 * Mountain View, CA 94043
42 * 29-Oct-90: fix bad (int) cast.
43 * 29-Jul-90: support for multiple screens.
44 * 08-Jul-90: new timing and colors and new algorithm for fractals.
45 * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors.
46 * 08-Oct-89: Fixed long standing typo bug in RandomInitHop();
47 * Fixed bug in memory allocation in inithop();
48 * Moved seconds() to an extern.
49 * Got rid of the % mod since .mod is slow on a sparc.
51 * 31-Aug-88: Forked from xlock.c for modularity.
52 * 23-Mar-88: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
55 #include "screenhack.h"
59 static int batchcount = 1000;
61 static unsigned int *pixels = 0, fg_pixel, bg_pixel;
63 static unsigned int delay;
68 int centery; /* center of the screen */
73 double j; /* hopalong parameters */
80 static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
88 XWindowAttributes xgwa;
92 XGetWindowAttributes (dsp, win, &xgwa);
98 int i = get_integer_resource ("ncolors", "Integer");
100 if (i <= 2) i = 2, mono_p = True;
102 pixels = (unsigned int *) calloc (i, sizeof (unsigned int));
103 fg_pixel = get_pixel_resource ("foreground", "Foreground", dsp, cmap);
104 bg_pixel = get_pixel_resource ("background", "Background", dsp, cmap);
107 hsv_to_rgb (random () % 360, 1.0, 1.0,
108 &color.red, &color.green, &color.blue);
109 for (npixels = 0; npixels < i; npixels++)
111 if (! XAllocColor (dsp, cmap, &color))
113 pixels[npixels] = color.pixel;
114 cycle_hue (&color, shift);
117 timeout = get_integer_resource ("timeout", "Seconds");
118 if (timeout <= 0) timeout = 30;
119 delay = get_integer_resource ("delay", "Usecs");
121 gcv.foreground = fg_pixel;
122 gc = XCreateGC (dsp, win, GCForeground, &gcv);
125 XClearWindow (dsp, win);
127 hp->centerx = xgwa.width / 2;
128 hp->centery = xgwa.height / 2;
129 range = sqrt((double) hp->centerx * hp->centerx +
130 (double) hp->centery * hp->centery) /
131 (10.0 + random() % 10);
134 #define frand0() (((double) random()) / ((unsigned int) (~0)))
135 hp->inc = (int) (frand0() * 200) - 100;
136 hp->a = frand0() * range - range / 2.0;
137 hp->b = frand0() * range - range / 2.0;
138 hp->c = frand0() * range - range / 2.0;
145 pointBuffer = (XPoint *) malloc(batchcount * sizeof(XPoint));
147 XSetForeground(dsp, gc, bg_pixel);
148 XFillRectangle(dsp, win, gc, 0, 0,
149 hp->centerx * 2, hp->centery * 2);
150 XSetForeground(dsp, gc, fg_pixel);
151 hp->startTime = time ((time_t *) 0);
162 XPoint *xp = pointBuffer;
163 hopstruct *hp = &hop;
167 XSetForeground(dsp, gc, pixels[hp->pix]);
168 if (++hp->pix >= npixels)
173 hp->j = hp->a - hp->i;
174 hp->i = oldj + (hp->i < 0
175 ? sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c))
176 : -sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c)));
177 xp->x = hp->centerx + (int) (hp->i + hp->j);
178 xp->y = hp->centery - (int) (hp->i - hp->j);
181 XDrawPoints(dsp, win, gc,
182 pointBuffer, batchcount, CoordModeOrigin);
184 if ((time ((time_t *) 0) - hp->startTime) > timeout)
187 XSetForeground(dsp, gc, bg_pixel);
188 for (i = 0; i < hp->centery; i++)
190 int y = (random () % (hp->centery << 1));
191 XDrawLine (dsp, win, gc, 0, y, hp->centerx << 1, y);
196 XClearWindow (dsp, win);
204 char *progclass = "Hopalong";
206 char *defaults [] = {
207 "*background: black",
208 "*foreground: white",
216 XrmOptionDescRec options [] = {
217 { "-count", ".count", XrmoptionSepArg, 0 },
218 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
219 { "-timeout", ".timeout", XrmoptionSepArg, 0 },
220 { "-delay", ".delay", XrmoptionSepArg, 0 },
222 int options_size = (sizeof (options) / sizeof (options[0]));
225 screenhack (dpy, window)
229 inithop (dpy, window);
232 drawhop (dpy, window);
234 if (delay) usleep (delay);