Linux has the same value for ENOTSUP as another var, therefore protect one
[oweals/cde.git] / cde / programs / dtscreen / worm.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $XConsortium: worm.c /main/3 1995/11/02 16:09:04 rswiston $ */
24 /*
25  */
26 /*                                                                      *
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.                                *
31  */
32 /*-
33  * worm.c - draw wiggly worms.
34  *
35  * Copyright (c) 1991 by Patrick J. Naughton.
36  *
37  * See dtscreen.c for copying information.
38  *
39  * Revision History:
40  * 27-Sep-91: got rid of all malloc calls since there were no calls to free().
41  * 25-Sep-91: Integrated into X11R5 contrib dtscreen.
42  *
43  * Adapted from a concept in the Dec 87 issue of Scientific American.
44  *
45  * SunView version: Brad Taylor (brad@sun.com)
46  * X11 version: Dave Lemke (lemke@ncd.com)
47  * xlock version: Boris Putanec (bp@cs.brown.edu)
48  *
49  * This code is a static memory pig... like almost 200K... but as contributed
50  * it leaked at a massive rate, so I made everything static up front... feel
51  * free to contribute the proper memory management code.
52  * 
53  */
54
55 #include "dtscreen.h"
56 #include        <math.h>
57
58 #define MAXCOLORS 64
59 #define MAXWORMS 64
60 #define CIRCSIZE 2
61 #define MAXWORMLEN 50
62
63 #define PI 3.14159265358979323844
64 #define SEGMENTS  36
65 static int  sintab[SEGMENTS];
66 static int  costab[SEGMENTS];
67 static int  init_table = 0;
68
69 typedef struct {
70     int         xcirc[MAXWORMLEN];
71     int         ycirc[MAXWORMLEN];
72     int         dir;
73     int         tail;
74     int         x;
75     int         y;
76 }           wormstuff;
77
78 typedef struct {
79     int         xsize;
80     int         ysize;
81     int         wormlength;
82     int         monopix;
83     int         nc;
84     int         nw;
85     wormstuff   worm[MAXWORMS];
86     XRectangle  rects[MAXCOLORS][MAXWORMS];
87     int         size[MAXCOLORS];
88 }           wormstruct;
89
90 int
91 round(x)
92     float       x;
93 {
94     return ((int) floor((double) x));
95 }
96
97
98 void
99 worm_doit(pwin, wp, which, color)
100     perwindow *pwin;
101     wormstruct *wp;
102     int         which;
103     unsigned long color;
104 {
105     wormstuff  *ws = &wp->worm[which];
106     int         x, y;
107
108     ws->tail++;
109     if (ws->tail == wp->wormlength)
110         ws->tail = 0;
111
112     x = ws->xcirc[ws->tail];
113     y = ws->ycirc[ws->tail];
114     XClearArea(dsp, pwin->w, x, y, CIRCSIZE, CIRCSIZE, False);
115
116     if (random() & 1) {
117         ws->dir = (ws->dir + 1) % SEGMENTS;
118     } else {
119         ws->dir = (ws->dir + SEGMENTS - 1) % SEGMENTS;
120     }
121
122     x = (ws->x + costab[ws->dir] + wp->xsize) % wp->xsize;
123     y = (ws->y + sintab[ws->dir] + wp->ysize) % wp->ysize;
124
125     ws->xcirc[ws->tail] = x;
126     ws->ycirc[ws->tail] = y;
127     ws->x = x;
128     ws->y = y;
129
130     wp->rects[color][wp->size[color]].x = x;
131     wp->rects[color][wp->size[color]].y = y;
132     wp->size[color]++;
133 }
134
135
136 void
137 initworm(pwin)
138     perwindow *pwin;
139 {
140     int         i, j;
141     wormstruct *wp;
142     XWindowAttributes xwa;
143
144     if (pwin->data) free(pwin->data);
145     pwin->data = (void *)malloc(sizeof(wormstruct));
146     memset(pwin->data, '\0', sizeof(wormstruct));
147     wp = (wormstruct *)pwin->data;
148     wp->nc = pwin->perscreen->npixels;
149     if (wp->nc > MAXCOLORS)
150         wp->nc = MAXCOLORS;
151
152     wp->nw = batchcount;
153     if (wp->nw > MAXWORMS)
154         wp->nw = MAXWORMS;
155
156     if (!init_table) {
157         init_table = 1;
158         for (i = 0; i < SEGMENTS; i++) {
159             sintab[i] = round(CIRCSIZE * sin(i * 2 * PI / SEGMENTS));
160             costab[i] = round(CIRCSIZE * cos(i * 2 * PI / SEGMENTS));
161         }
162     }
163     XGetWindowAttributes(dsp, pwin->w, &xwa);
164     wp->xsize = xwa.width;
165     wp->ysize = xwa.height;
166
167     if (xwa.width < 100) {
168         wp->monopix = BlackPixelOfScreen(pwin->perscreen->screen);
169         wp->wormlength = MAXWORMLEN / 10;
170     } else {
171         wp->monopix = WhitePixelOfScreen(pwin->perscreen->screen);
172         wp->wormlength = MAXWORMLEN;
173     }
174
175     for (i = 0; i < wp->nc; i++) {
176         for (j = 0; j < wp->nw / wp->nc + 1; j++) {
177             wp->rects[i][j].width = CIRCSIZE;
178             wp->rects[i][j].height = CIRCSIZE;
179         }
180     }
181     memset(wp->size, '\0', wp->nc * sizeof(int));
182
183     for (i = 0; i < wp->nw; i++) {
184         for (j = 0; j < wp->wormlength; j++) {
185             wp->worm[i].xcirc[j] = wp->xsize / 2;
186             wp->worm[i].ycirc[j] = wp->ysize / 2;
187         }
188         wp->worm[i].dir = (unsigned) random() % SEGMENTS;
189         wp->worm[i].tail = 0;
190         wp->worm[i].x = wp->xsize / 2;
191         wp->worm[i].y = wp->ysize / 2;
192     }
193
194     XClearWindow(dsp, pwin->w);
195 }
196
197
198 void
199 drawworm(pwin)
200     perwindow *pwin;
201 {
202     int         i;
203     wormstruct *wp = (wormstruct *)pwin->data;
204     unsigned int wcolor;
205     static unsigned int chromo = 0;
206
207     memset(wp->size, '\0', wp->nc * sizeof(int));
208
209     for (i = 0; i < wp->nw; i++) {
210         if (!mono && wp->nc > 2) {
211             wcolor = (i + chromo) % wp->nc;
212
213             worm_doit(pwin, wp, i, wcolor);
214         } else
215             worm_doit(pwin, wp, i, 0);
216     }
217
218     if (!mono && wp->nc > 2) {
219         for (i = 0; i < wp->nc; i++) {
220             XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[i]);
221             XFillRectangles(dsp, pwin->w, pwin->gc, wp->rects[i],
222                             wp->size[i]);
223         }
224     } else {
225         XSetForeground(dsp, pwin->gc, wp->monopix);
226         XFillRectangles(dsp, pwin->w, pwin->gc, wp->rects[0],
227                         wp->size[0]);
228     }
229
230     if (++chromo == wp->nc)
231         chromo = 0;
232 }