dtscreen: Resolve a -Wformat-security warning.
[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 #include <stdlib.h>
56
57 typedef struct {
58     int         centerx;
59     int         centery;        /* center of the screen */
60     double      a;
61     double      b;
62     double      c;
63     double      i;
64     double      j;              /* hopalong parameters */
65     int         inc;
66     int         pix;
67     long        startTime;
68 }           hopstruct;
69
70 static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
71
72 #define TIMEOUT 30
73
74 void
75 inithop(pwin)
76     perwindow *pwin;
77 {
78     double      range;
79     XWindowAttributes xgwa;
80     hopstruct  *hp;
81
82     if (pwin->data) free(pwin->data);
83     pwin->data = (void *)malloc(sizeof(hopstruct));
84     memset(pwin->data, '\0', sizeof(hopstruct));
85     hp = (hopstruct *)pwin->data;
86     XGetWindowAttributes(dsp, pwin->w, &xgwa);
87     hp->centerx = xgwa.width / 2;
88     hp->centery = xgwa.height / 2;
89     range = sqrt((double) hp->centerx * hp->centerx +
90                  (double) hp->centery * hp->centery) /
91         (10.0 + random() % 10);
92
93     hp->pix = 0;
94     hp->inc = (int) ((random() / MAXRAND) * 200) - 100;
95     hp->a = (random() / MAXRAND) * range - range / 2.0;
96     hp->b = (random() / MAXRAND) * range - range / 2.0;
97     hp->c = (random() / MAXRAND) * range - range / 2.0;
98     if (!(random() % 2))
99         hp->c = 0.0;
100
101     hp->i = hp->j = 0.0;
102
103     if (!pointBuffer)
104         pointBuffer = (XPoint *) malloc(batchcount * sizeof(XPoint));
105
106     XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
107     XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0,
108                    hp->centerx * 2, hp->centery * 2);
109     XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
110     hp->startTime = seconds();
111 }
112
113
114 void
115 drawhop(pwin)
116     perwindow *pwin;
117 {
118     double      oldj;
119     int         k = batchcount;
120     XPoint     *xp = pointBuffer;
121     hopstruct  *hp = (hopstruct *)pwin->data;
122
123     hp->inc++;
124     if (!mono && pwin->perscreen->npixels > 2) {
125         XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[hp->pix]);
126         if (++hp->pix >= pwin->perscreen->npixels)
127             hp->pix = 0;
128     }
129     while (k--) {
130         oldj = hp->j;
131         hp->j = hp->a - hp->i;
132         hp->i = oldj + (hp->i < 0
133                         ? sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c))
134                         : -sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c)));
135         xp->x = hp->centerx + (int) (hp->i + hp->j);
136         xp->y = hp->centery - (int) (hp->i - hp->j);
137         xp++;
138     }
139     XDrawPoints(dsp, pwin->w, pwin->gc,
140                 pointBuffer, batchcount, CoordModeOrigin);
141     if (seconds() - hp->startTime > TIMEOUT)
142         inithop(pwin);
143 }