open-src/app/xlock/sun-src/swarm.c
changeset 546 f3f84c886c69
child 907 3c35d611cdaa
equal deleted inserted replaced
545:43240086238f 546:f3f84c886c69
       
     1 /*
       
     2  * Copyright (c) 1988-91 by Patrick J. Naughton.
       
     3  *
       
     4  * Permission to use, copy, modify, and distribute this software and its
       
     5  * documentation for any purpose and without fee is hereby granted,
       
     6  * provided that the above copyright notice appear in all copies and that
       
     7  * both that copyright notice and this permission notice appear in
       
     8  * supporting documentation.
       
     9  *
       
    10  * This file is provided AS IS with no warranties of any kind.  The author
       
    11  * shall have no liability with respect to the infringement of copyrights,
       
    12  * trade secrets or any patents by this file or any part thereof.  In no
       
    13  * event will the author be liable for any lost revenue or profits or
       
    14  * other special, indirect and consequential damages.
       
    15  */
       
    16 
       
    17 /*
       
    18  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
       
    19  * Use is subject to license terms.
       
    20  *
       
    21  * Permission is hereby granted, free of charge, to any person obtaining a
       
    22  * copy of this software and associated documentation files (the
       
    23  * "Software"), to deal in the Software without restriction, including
       
    24  * without limitation the rights to use, copy, modify, merge, publish,
       
    25  * distribute, and/or sell copies of the Software, and to permit persons
       
    26  * to whom the Software is furnished to do so, provided that the above
       
    27  * copyright notice(s) and this permission notice appear in all copies of
       
    28  * the Software and that both the above copyright notice(s) and this
       
    29  * permission notice appear in supporting documentation.
       
    30  *
       
    31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
       
    32  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
       
    34  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
       
    35  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
       
    36  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
       
    37  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
       
    38  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
       
    39  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
       
    40  *
       
    41  * Except as contained in this notice, the name of a copyright holder
       
    42  * shall not be used in advertising or otherwise to promote the sale, use
       
    43  * or other dealings in this Software without prior written authorization
       
    44  * of the copyright holder.
       
    45  */
       
    46 #ifndef lint
       
    47 static char sccsid[] = "@(#)swarm.c	35.3 08/09/18 XLOCK";
       
    48 #endif
       
    49 /*-
       
    50  * swarm.c - swarm of bees for xlock, the X Window System lockscreen.
       
    51  *
       
    52  * Copyright (c) 1991 by Patrick J. Naughton.
       
    53  *
       
    54  * Revision History:
       
    55  * 31-Aug-90: Adapted from xswarm by Jeff Butterworth. ([email protected])
       
    56  */
       
    57 
       
    58 #include "xlock.h"
       
    59 
       
    60 #define TIMES	4		/* number of time positions recorded */
       
    61 #define BEEACC	3		/* acceleration of bees */
       
    62 #define WASPACC 5		/* maximum acceleration of wasp */
       
    63 #define BEEVEL	11		/* maximum bee velocity */
       
    64 #define WASPVEL 12		/* maximum wasp velocity */
       
    65 #define BORDER	50		/* wasp won't go closer than this to the edge */
       
    66 
       
    67 /* Macros */
       
    68 #define X(t,b)	(sp->x[(t)*sp->beecount+(b)])
       
    69 #define Y(t,b)	(sp->y[(t)*sp->beecount+(b)])
       
    70 #define RAND(v)	((random()%(v))-((v)/2))	/* random number around 0 */
       
    71 
       
    72 typedef struct {
       
    73     int         pix;
       
    74     long        startTime;
       
    75     int         width;
       
    76     int         height;
       
    77     int         beecount;	/* number of bees */
       
    78     XSegment   *segs;		/* bee lines */
       
    79     XSegment   *old_segs;	/* old bee lines */
       
    80     short      *x;
       
    81     short      *y;		/* bee positions x[time][bee#] */
       
    82     short      *xv;
       
    83     short      *yv;		/* bee velocities xv[bee#] */
       
    84     short       wx[3];
       
    85     short       wy[3];
       
    86     short       wxv;
       
    87     short       wyv;
       
    88 }           swarmstruct;
       
    89 
       
    90 extern XColor ssblack[];
       
    91 extern XColor sswhite[];
       
    92 
       
    93 static swarmstruct swarms[MAXSCREENS];
       
    94 
       
    95 void
       
    96 initswarm(win)
       
    97     Window      win;
       
    98 {
       
    99     XWindowAttributes xgwa;
       
   100     swarmstruct *sp = &swarms[screen];
       
   101     int         b;
       
   102 
       
   103     sp->startTime = seconds();
       
   104     sp->beecount = batchcount;
       
   105 
       
   106     XGetWindowAttributes(dsp, win, &xgwa);
       
   107     sp->width = xgwa.width;
       
   108     sp->height = xgwa.height;
       
   109 
       
   110     /* Clear the background. */
       
   111     XSetForeground(dsp, Scr[screen].gc, ssblack[screen].pixel);
       
   112     XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, sp->width, sp->height);
       
   113 
       
   114     /* Allocate memory. */
       
   115 
       
   116     if (!sp->segs) {
       
   117 	sp->segs = (XSegment *) malloc(sizeof(XSegment) * sp->beecount);
       
   118 	sp->old_segs = (XSegment *) malloc(sizeof(XSegment) * sp->beecount);
       
   119 	sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES);
       
   120 	sp->y = (short *) malloc(sizeof(short) * sp->beecount * TIMES);
       
   121 	sp->xv = (short *) malloc(sizeof(short) * sp->beecount);
       
   122 	sp->yv = (short *) malloc(sizeof(short) * sp->beecount);
       
   123     }
       
   124     /* Initialize point positions, velocities, etc. */
       
   125 
       
   126     /* wasp */
       
   127     sp->wx[0] = BORDER + random() % (sp->width - 2 * BORDER);
       
   128     sp->wy[0] = BORDER + random() % (sp->height - 2 * BORDER);
       
   129     sp->wx[1] = sp->wx[0];
       
   130     sp->wy[1] = sp->wy[0];
       
   131     sp->wxv = 0;
       
   132     sp->wyv = 0;
       
   133 
       
   134     /* bees */
       
   135     for (b = 0; b < sp->beecount; b++) {
       
   136 	X(0, b) = random() % sp->width;
       
   137 	X(1, b) = X(0, b);
       
   138 	Y(0, b) = random() % sp->height;
       
   139 	Y(1, b) = Y(0, b);
       
   140 	sp->xv[b] = RAND(7);
       
   141 	sp->yv[b] = RAND(7);
       
   142     }
       
   143 }
       
   144 
       
   145 
       
   146 
       
   147 void
       
   148 drawswarm(win)
       
   149     Window      win;
       
   150 {
       
   151     swarmstruct *sp = &swarms[screen];
       
   152     int         b;
       
   153 
       
   154     /* <=- Wasp -=> */
       
   155     /* Age the arrays. */
       
   156     sp->wx[2] = sp->wx[1];
       
   157     sp->wx[1] = sp->wx[0];
       
   158     sp->wy[2] = sp->wy[1];
       
   159     sp->wy[1] = sp->wy[0];
       
   160     /* Accelerate */
       
   161     sp->wxv += RAND(WASPACC);
       
   162     sp->wyv += RAND(WASPACC);
       
   163 
       
   164     /* Speed Limit Checks */
       
   165     if (sp->wxv > WASPVEL)
       
   166 	sp->wxv = WASPVEL;
       
   167     if (sp->wxv < -WASPVEL)
       
   168 	sp->wxv = -WASPVEL;
       
   169     if (sp->wyv > WASPVEL)
       
   170 	sp->wyv = WASPVEL;
       
   171     if (sp->wyv < -WASPVEL)
       
   172 	sp->wyv = -WASPVEL;
       
   173 
       
   174     /* Move */
       
   175     sp->wx[0] = sp->wx[1] + sp->wxv;
       
   176     sp->wy[0] = sp->wy[1] + sp->wyv;
       
   177 
       
   178     /* Bounce Checks */
       
   179     if ((sp->wx[0] < BORDER) || (sp->wx[0] > sp->width - BORDER - 1)) {
       
   180 	sp->wxv = -sp->wxv;
       
   181 	sp->wx[0] += sp->wxv;
       
   182     }
       
   183     if ((sp->wy[0] < BORDER) || (sp->wy[0] > sp->height - BORDER - 1)) {
       
   184 	sp->wyv = -sp->wyv;
       
   185 	sp->wy[0] += sp->wyv;
       
   186     }
       
   187     /* Don't let things settle down. */
       
   188     sp->xv[random() % sp->beecount] += RAND(3);
       
   189     sp->yv[random() % sp->beecount] += RAND(3);
       
   190 
       
   191     /* <=- Bees -=> */
       
   192     for (b = 0; b < sp->beecount; b++) {
       
   193 	int         distance,
       
   194 	            dx,
       
   195 	            dy;
       
   196 	/* Age the arrays. */
       
   197 	X(2, b) = X(1, b);
       
   198 	X(1, b) = X(0, b);
       
   199 	Y(2, b) = Y(1, b);
       
   200 	Y(1, b) = Y(0, b);
       
   201 
       
   202 	/* Accelerate */
       
   203 	dx = sp->wx[1] - X(1, b);
       
   204 	dy = sp->wy[1] - Y(1, b);
       
   205 	distance = abs(dx) + abs(dy);	/* approximation */
       
   206 	if (distance == 0)
       
   207 	    distance = 1;
       
   208 	sp->xv[b] += (dx * BEEACC) / distance;
       
   209 	sp->yv[b] += (dy * BEEACC) / distance;
       
   210 
       
   211 	/* Speed Limit Checks */
       
   212 	if (sp->xv[b] > BEEVEL)
       
   213 	    sp->xv[b] = BEEVEL;
       
   214 	if (sp->xv[b] < -BEEVEL)
       
   215 	    sp->xv[b] = -BEEVEL;
       
   216 	if (sp->yv[b] > BEEVEL)
       
   217 	    sp->yv[b] = BEEVEL;
       
   218 	if (sp->yv[b] < -BEEVEL)
       
   219 	    sp->yv[b] = -BEEVEL;
       
   220 
       
   221 	/* Move */
       
   222 	X(0, b) = X(1, b) + sp->xv[b];
       
   223 	Y(0, b) = Y(1, b) + sp->yv[b];
       
   224 
       
   225 	/* Fill the segment lists. */
       
   226 	sp->segs[b].x1 = X(0, b);
       
   227 	sp->segs[b].y1 = Y(0, b);
       
   228 	sp->segs[b].x2 = X(1, b);
       
   229 	sp->segs[b].y2 = Y(1, b);
       
   230 	sp->old_segs[b].x1 = X(1, b);
       
   231 	sp->old_segs[b].y1 = Y(1, b);
       
   232 	sp->old_segs[b].x2 = X(2, b);
       
   233 	sp->old_segs[b].y2 = Y(2, b);
       
   234     }
       
   235 
       
   236     XSetForeground(dsp, Scr[screen].gc, ssblack[screen].pixel);
       
   237     XDrawLine(dsp, win, Scr[screen].gc,
       
   238 	      sp->wx[1], sp->wy[1], sp->wx[2], sp->wy[2]);
       
   239     XDrawSegments(dsp, win, Scr[screen].gc, sp->old_segs, sp->beecount);
       
   240 
       
   241     XSetForeground(dsp, Scr[screen].gc, sswhite[screen].pixel);
       
   242     XDrawLine(dsp, win, Scr[screen].gc,
       
   243 	      sp->wx[0], sp->wy[0], sp->wx[1], sp->wy[1]);
       
   244     if (!mono && Scr[screen].npixels > 2) {
       
   245 	XSetForeground(dsp, Scr[screen].gc, Scr[screen].pixels[sp->pix]);
       
   246 	if (++sp->pix >= Scr[screen].npixels)
       
   247 	    sp->pix = 0;
       
   248     }
       
   249     XDrawSegments(dsp, win, Scr[screen].gc, sp->segs, sp->beecount);
       
   250 }