dtscreen: Resolve a -Wformat-security warning.
[oweals/cde.git] / cde / programs / dtscreen / qix.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: qix.c /main/3 1995/11/02 16:08: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  * qix.c - Vector swirl 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-Jul-90: support for multiple screens.
41  *            made check_bounds_?() a macro.
42  *            fixed initial parameter setup.
43  * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors.
44  * 08-Oct-89: Fixed bug in memory allocation in initqix().
45  *            Moved seconds() to an extern.
46  * 23-Sep-89: Switch to random() and fixed bug w/ less than 4 lines.
47  * 20-Sep-89: Lint.
48  * 24-Mar-89: Written.
49  */
50
51 #include "dtscreen.h"
52 #include <stdlib.h>
53
54 typedef struct {
55     int         x;
56     int         y;
57 }           point;
58
59 typedef struct {
60     int         pix;
61     long        startTime;
62     int         first;
63     int         last;
64     int         dx1;
65     int         dy1;
66     int         dx2;
67     int         dy2;
68     int         x1;
69     int         y1;
70     int         x2;
71     int         y2;
72     int         offset;
73     int         delta;
74     int         width;
75     int         height;
76     int         nlines;
77     point      *lineq;
78 }           qixstruct;
79
80 void
81 initqix(pwin)
82     perwindow *pwin;
83 {
84     XWindowAttributes xgwa;
85     qixstruct  *qp;
86
87     if (pwin->data) free(pwin->data);
88     pwin->data = (void *)malloc(sizeof(qixstruct));
89     memset(pwin->data, '\0', sizeof(qixstruct));
90     qp = (qixstruct *)pwin->data;
91     qp->startTime = seconds();
92     qp->nlines = (batchcount + 1) * 2;
93
94     qp->lineq = (point *) malloc(qp->nlines * sizeof(point));
95     memset(qp->lineq, '\0', qp->nlines * sizeof(point));
96
97     XGetWindowAttributes(dsp, pwin->w, &xgwa);
98     qp->width = xgwa.width;
99     qp->height = xgwa.height;
100     qp->delta = 16;
101
102     if (qp->width < 100) {      /* icon window */
103         qp->nlines /= 4;
104         qp->delta /= 4;
105     }
106     qp->offset = qp->delta / 3;
107     qp->last = 0;
108     qp->pix = 0;
109     qp->dx1 = random() % qp->delta + qp->offset;
110     qp->dy1 = random() % qp->delta + qp->offset;
111     qp->dx2 = random() % qp->delta + qp->offset;
112     qp->dy2 = random() % qp->delta + qp->offset;
113     qp->x1 = random() % qp->width;
114     qp->y1 = random() % qp->height;
115     qp->x2 = random() % qp->width;
116     qp->y2 = random() % qp->height;
117     XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
118     XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0, qp->width, qp->height);
119 }
120
121 #define check_bounds(qp, val, del, max)                         \
122 {                                                               \
123     if ((val) < 0) {                                            \
124         *(del) = (random() % (qp)->delta) + (qp)->offset;       \
125     } else if ((val) > (max)) {                                 \
126         *(del) = -(random() % (qp)->delta) - (qp)->offset;      \
127     }                                                           \
128 }
129
130 void
131 drawqix(pwin)
132     perwindow *pwin;
133 {
134     qixstruct  *qp = (qixstruct  *)pwin->data;
135
136     qp->first = (qp->last + 2) % qp->nlines;
137
138     qp->x1 += qp->dx1;
139     qp->y1 += qp->dy1;
140     qp->x2 += qp->dx2;
141     qp->y2 += qp->dy2;
142     check_bounds(qp, qp->x1, &qp->dx1, qp->width);
143     check_bounds(qp, qp->y1, &qp->dy1, qp->height);
144     check_bounds(qp, qp->x2, &qp->dx2, qp->width);
145     check_bounds(qp, qp->y2, &qp->dy2, qp->height);
146     XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
147     XDrawLine(dsp, pwin->w, pwin->gc,
148               qp->lineq[qp->first].x, qp->lineq[qp->first].y,
149               qp->lineq[qp->first + 1].x, qp->lineq[qp->first + 1].y);
150     if (!mono && pwin->perscreen->npixels > 2) {
151         XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[qp->pix]);
152         if (++qp->pix >= pwin->perscreen->npixels)
153             qp->pix = 0;
154     } else
155         XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
156
157     XDrawLine(dsp, pwin->w, pwin->gc, qp->x1, qp->y1, qp->x2, qp->y2);
158
159     qp->lineq[qp->last].x = qp->x1;
160     qp->lineq[qp->last].y = qp->y1;
161     qp->last++;
162     if (qp->last >= qp->nlines)
163         qp->last = 0;
164
165     qp->lineq[qp->last].x = qp->x2;
166     qp->lineq[qp->last].y = qp->y2;
167     qp->last++;
168     if (qp->last >= qp->nlines)
169         qp->last = 0;
170 }