Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtscreen / image.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: image.c /main/3 1995/11/02 16:07:35 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  * image.c - image bouncer 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: Written.
41  */
42
43 #include "dtscreen.h"
44 #include "xlogo.bit"
45
46 static XImage logo = {
47     0, 0,                       /* width, height */
48     0, XYBitmap, 0,             /* xoffset, format, data */
49     LSBFirst, 8,                /* byte-order, bitmap-unit */
50     LSBFirst, 8, 1              /* bitmap-bit-order, bitmap-pad, depth */
51 };
52
53 #define MAXICONS 256
54
55 typedef struct {
56     int         x;
57     int         y;
58 }           point;
59
60 typedef struct {
61     int         width;
62     int         height;
63     int         nrows;
64     int         ncols;
65     int         xb;
66     int         yb;
67     int         iconmode;
68     int         iconcount;
69     point       icons[MAXICONS];
70     long        startTime;
71 }           imagestruct;
72
73 void
74 drawimage(pwin)
75     perwindow *pwin;
76 {
77     imagestruct *ip;
78     int         i;
79
80     ip = (imagestruct *)pwin->data;
81     XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
82     for (i = 0; i < ip->iconcount; i++) {
83         if (!ip->iconmode)
84             XFillRectangle(dsp, pwin->w, pwin->gc,
85                            ip->xb + xlogo_width * ip->icons[i].x,
86                            ip->yb + xlogo_height * ip->icons[i].y,
87                            xlogo_width, xlogo_height);
88
89         ip->icons[i].x = random() % ip->ncols;
90         ip->icons[i].y = random() % ip->nrows;
91     }
92     if (pwin->perscreen->npixels == 2)
93       XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
94     for (i = 0; i < ip->iconcount; i++) {
95         if (pwin->perscreen->npixels > 2)
96             XSetForeground(dsp, pwin->gc,
97                  pwin->perscreen->pixels[random() % pwin->perscreen->npixels]);
98
99         XPutImage(dsp, pwin->w, pwin->gc, &logo,
100                   0, 0,
101                   ip->xb + xlogo_width * ip->icons[i].x,
102                   ip->yb + xlogo_height * ip->icons[i].y,
103                   xlogo_width, xlogo_height);
104     }
105 }
106
107 void
108 initimage(pwin)
109     perwindow *pwin;
110 {
111     XWindowAttributes xgwa;
112     imagestruct *ip;
113
114     if (pwin->data) free(pwin->data);
115     pwin->data = (void *)malloc(sizeof(imagestruct));
116     memset(pwin->data, '\0', sizeof(imagestruct));
117     ip = (imagestruct *)pwin->data;
118     ip->startTime = seconds();
119
120     logo.data = (char *) xlogo_bits;
121     logo.width = xlogo_width;
122     logo.height = xlogo_height;
123     logo.bytes_per_line = (xlogo_width + 7) / 8;
124
125     XGetWindowAttributes(dsp, pwin->w, &xgwa);
126     ip->width = xgwa.width;
127     ip->height = xgwa.height;
128     ip->ncols = ip->width / xlogo_width;
129     ip->nrows = ip->height / xlogo_height;
130     ip->iconmode = (ip->ncols < 2 || ip->nrows < 2);
131     if (ip->iconmode) {
132         ip->xb = 0;
133         ip->yb = 0;
134         ip->iconcount = 1;      /* icon mode */
135     } else {
136         ip->xb = (ip->width - xlogo_width * ip->ncols) / 2;
137         ip->yb = (ip->height - xlogo_height * ip->nrows) / 2;
138         ip->iconcount = batchcount;
139     }
140     XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
141     XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0, ip->width, ip->height);
142 }