Merge branch 'master' of https://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / programs / dtscreen / hopalong.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: hopalong.c /main/3 1995/11/02 16:07:11 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  * hopalong.c - Real Plane Fractals for dtscreen, the X Window System lockscreen.
34  *
35  * Copyright (c) 1991 by Patrick J. Naughton.
36  *
37  * See dtscreen.c for copying information.
38  *
39  * Revision History:
40  * 29-Oct-90: fix bad (int) cast.
41  * 29-Jul-90: support for multiple screens.
42  * 08-Jul-90: new timing and colors and new algorithm for fractals.
43  * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors.
44  * 08-Oct-89: Fixed long standing typo bug in RandomInitHop();
45  *            Fixed bug in memory allocation in inithop();
46  *            Moved seconds() to an extern.
47  *            Got rid of the % mod since .mod is slow on a sparc.
48  * 20-Sep-89: Lint.
49  * 31-Aug-88: Forked from dtscreen.c for modularity.
50  * 23-Mar-88: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
51  */
52
53 #include "dtscreen.h"
54 #include <math.h>
55
56 typedef struct {
57     int         centerx;
58     int         centery;        /* center of the screen */
59     double      a;
60     double      b;
61     double      c;
62     double      i;
63     double      j;              /* hopalong parameters */
64     int         inc;
65     int         pix;
66     long        startTime;
67 }           hopstruct;
68
69 static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
70
71 #define TIMEOUT 30
72
73 void
74 inithop(pwin)
75     perwindow *pwin;
76 {
77     double      range;
78     XWindowAttributes xgwa;
79     hopstruct  *hp;
80
81     if (pwin->data) free(pwin->data);
82     pwin->data = (void *)malloc(sizeof(hopstruct));
83     memset(pwin->data, '\0', sizeof(hopstruct));
84     hp = (hopstruct *)pwin->data;
85     XGetWindowAttributes(dsp, pwin->w, &xgwa);
86     hp->centerx = xgwa.width / 2;
87     hp->centery = xgwa.height / 2;
88     range = sqrt((double) hp->centerx * hp->centerx +
89                  (double) hp->centery * hp->centery) /
90         (10.0 + random() % 10);
91
92     hp->pix = 0;
93     hp->inc = (int) ((random() / MAXRAND) * 200) - 100;
94     hp->a = (random() / MAXRAND) * range - range / 2.0;
95     hp->b = (random() / MAXRAND) * range - range / 2.0;
96     hp->c = (random() / MAXRAND) * range - range / 2.0;
97     if (!(random() % 2))
98         hp->c = 0.0;
99
100     hp->i = hp->j = 0.0;
101
102     if (!pointBuffer)
103         pointBuffer = (XPoint *) malloc(batchcount * sizeof(XPoint));
104
105     XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
106     XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0,
107                    hp->centerx * 2, hp->centery * 2);
108     XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
109     hp->startTime = seconds();
110 }
111
112
113 void
114 drawhop(pwin)
115     perwindow *pwin;
116 {
117     double      oldj;
118     int         k = batchcount;
119     XPoint     *xp = pointBuffer;
120     hopstruct  *hp = (hopstruct *)pwin->data;
121
122     hp->inc++;
123     if (!mono && pwin->perscreen->npixels > 2) {
124         XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[hp->pix]);
125         if (++hp->pix >= pwin->perscreen->npixels)
126             hp->pix = 0;
127     }
128     while (k--) {
129         oldj = hp->j;
130         hp->j = hp->a - hp->i;
131         hp->i = oldj + (hp->i < 0
132                         ? sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c))
133                         : -sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c)));
134         xp->x = hp->centerx + (int) (hp->i + hp->j);
135         xp->y = hp->centery - (int) (hp->i - hp->j);
136         xp++;
137     }
138     XDrawPoints(dsp, pwin->w, pwin->gc,
139                 pointBuffer, batchcount, CoordModeOrigin);
140     if (seconds() - hp->startTime > TIMEOUT)
141         inithop(pwin);
142 }