2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: swarm.c /main/3 1995/11/02 16:08:42 rswiston $ */
27 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
28 * (c) Copyright 1993, 1994 International Business Machines Corp. *
29 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
30 * (c) Copyright 1993, 1994 Novell, Inc. *
33 * swarm.c - swarm of bees for dtscreen, the X Window System lockscreen.
35 * Copyright (c) 1991 by Patrick J. Naughton.
38 * 31-Aug-90: Adapted from xswarm by Jeff Butterworth. (butterwo@ncsc.org)
44 #define TIMES 4 /* number of time positions recorded */
45 #define BEEACC 3 /* acceleration of bees */
46 #define WASPACC 5 /* maximum acceleration of wasp */
47 #define BEEVEL 11 /* maximum bee velocity */
48 #define WASPVEL 12 /* maximum wasp velocity */
49 #define BORDER 50 /* wasp won't go closer than this to the edge */
52 #define X(t,b) (sp->x[(t)*sp->beecount+(b)])
53 #define Y(t,b) (sp->y[(t)*sp->beecount+(b)])
54 #define RAND(v) ((random()%(v))-((v)/2)) /* random number around 0 */
61 int beecount; /* number of bees */
62 XSegment *segs; /* bee lines */
63 XSegment *old_segs; /* old bee lines */
65 short *y; /* bee positions x[time][bee#] */
67 short *yv; /* bee velocities xv[bee#] */
78 XWindowAttributes xgwa;
82 if (pwin->data) free(pwin->data);
83 pwin->data = (void *)malloc(sizeof(swarmstruct));
84 memset(pwin->data, '\0', sizeof(swarmstruct));
85 sp = (swarmstruct *)pwin->data;
86 sp->startTime = seconds();
87 sp->beecount = batchcount;
89 XGetWindowAttributes(dsp, pwin->w, &xgwa);
90 sp->width = xgwa.width;
91 sp->height = xgwa.height;
93 /* Clear the background. */
94 XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
95 XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0, sp->width, sp->height);
97 /* Allocate memory. */
99 sp->segs = (XSegment *) malloc(sizeof(XSegment) * sp->beecount);
100 sp->old_segs = (XSegment *) malloc(sizeof(XSegment) * sp->beecount);
101 sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES);
102 sp->y = (short *) malloc(sizeof(short) * sp->beecount * TIMES);
103 sp->xv = (short *) malloc(sizeof(short) * sp->beecount);
104 sp->yv = (short *) malloc(sizeof(short) * sp->beecount);
106 /* Initialize point positions, velocities, etc. */
109 sp->wx[0] = BORDER + random() % (sp->width - 2 * BORDER);
110 sp->wy[0] = BORDER + random() % (sp->height - 2 * BORDER);
111 sp->wx[1] = sp->wx[0];
112 sp->wy[1] = sp->wy[0];
117 for (b = 0; b < sp->beecount; b++) {
118 X(0, b) = random() % sp->width;
120 Y(0, b) = random() % sp->height;
136 sp = (swarmstruct *)pwin->data;
138 /* Age the arrays. */
139 sp->wx[2] = sp->wx[1];
140 sp->wx[1] = sp->wx[0];
141 sp->wy[2] = sp->wy[1];
142 sp->wy[1] = sp->wy[0];
144 sp->wxv += RAND(WASPACC);
145 sp->wyv += RAND(WASPACC);
147 /* Speed Limit Checks */
148 if (sp->wxv > WASPVEL)
150 if (sp->wxv < -WASPVEL)
152 if (sp->wyv > WASPVEL)
154 if (sp->wyv < -WASPVEL)
158 sp->wx[0] = sp->wx[1] + sp->wxv;
159 sp->wy[0] = sp->wy[1] + sp->wyv;
162 if ((sp->wx[0] < BORDER) || (sp->wx[0] > sp->width - BORDER - 1)) {
164 sp->wx[0] += sp->wxv;
166 if ((sp->wy[0] < BORDER) || (sp->wy[0] > sp->height - BORDER - 1)) {
168 sp->wy[0] += sp->wyv;
170 /* Don't let things settle down. */
171 sp->xv[random() % sp->beecount] += RAND(3);
172 sp->yv[random() % sp->beecount] += RAND(3);
175 for (b = 0; b < sp->beecount; b++) {
179 /* Age the arrays. */
186 dx = sp->wx[1] - X(1, b);
187 dy = sp->wy[1] - Y(1, b);
188 distance = abs(dx) + abs(dy); /* approximation */
191 sp->xv[b] += (dx * BEEACC) / distance;
192 sp->yv[b] += (dy * BEEACC) / distance;
194 /* Speed Limit Checks */
195 if (sp->xv[b] > BEEVEL)
197 if (sp->xv[b] < -BEEVEL)
199 if (sp->yv[b] > BEEVEL)
201 if (sp->yv[b] < -BEEVEL)
205 X(0, b) = X(1, b) + sp->xv[b];
206 Y(0, b) = Y(1, b) + sp->yv[b];
208 /* Fill the segment lists. */
209 sp->segs[b].x1 = X(0, b);
210 sp->segs[b].y1 = Y(0, b);
211 sp->segs[b].x2 = X(1, b);
212 sp->segs[b].y2 = Y(1, b);
213 sp->old_segs[b].x1 = X(1, b);
214 sp->old_segs[b].y1 = Y(1, b);
215 sp->old_segs[b].x2 = X(2, b);
216 sp->old_segs[b].y2 = Y(2, b);
219 XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
220 XDrawLine(dsp, pwin->w, pwin->gc,
221 sp->wx[1], sp->wy[1], sp->wx[2], sp->wy[2]);
222 XDrawSegments(dsp, pwin->w, pwin->gc, sp->old_segs, sp->beecount);
224 XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
225 XDrawLine(dsp, pwin->w, pwin->gc,
226 sp->wx[0], sp->wy[0], sp->wx[1], sp->wy[1]);
227 if (!mono && pwin->perscreen->npixels > 2) {
228 XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[sp->pix]);
229 if (++sp->pix >= pwin->perscreen->npixels)
232 XDrawSegments(dsp, pwin->w, pwin->gc, sp->segs, sp->beecount);