ksh: fix up shipin for more modern systems WRT test and wc
[oweals/cde.git] / cde / examples / dtscreen / screensaver.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: screensaver.c /main/3 1995/10/27 10:40:15 rswiston $ */
24 /************************************/
25 /** Sample CDE Screen Saver Client **/
26 /************************************/
27
28 #include <locale.h>
29 #include <stdio.h>
30 #include <sys/time.h>
31 #include <math.h>
32 #include <X11/Xlib.h>
33 #include <X11/Intrinsic.h>
34 #include <Dt/Saver.h>
35
36 #define SS_MAX_COLORS 6
37
38 typedef struct {
39     Window      id;
40     int         width;
41     int         height;
42     unsigned long ssPixels[SS_MAX_COLORS];
43     Colormap    cmap;
44     GC          gc;
45 } WinDataStruct;
46
47 int
48 usleep(usec)
49     unsigned long usec;
50 {
51     poll((struct poll *) 0, (size_t) 0, usec / 1000);   /* milliseconds */
52     return 0;
53 }
54
55 int main(int argc, char *argv[])
56 {
57 Display *dpy;                   /* Display connection      */
58 Window *winprop = NULL;         /* dtsession cover windows */
59 int nWindows;                   /* number of windows       */
60 WinDataStruct *perWindow;       /* per-window information  */
61 int i, j;                       /* index variable          */
62 XColor actual, exact;           /* color structs           */
63 int colorRotate = 0;            /* color rotation counter  */
64
65     setlocale(LC_ALL, "");
66
67     /*******************************/
68     /** open a display connection **/
69     /*******************************/
70     if (!(dpy = XOpenDisplay(NULL)) ) {
71         fprintf(stderr, "Unable to open the Display.\n");
72         exit(1);
73     }
74
75     /***********************************************************/
76     /** Get the list of screen saver windows from the desktop **/
77     /***********************************************************/
78     if (!DtSaverGetWindows(dpy, &winprop, &nWindows)) {
79         fprintf(stderr, "Unable to get screen saver info.\n");
80         XCloseDisplay(dpy);
81         exit(1);
82     }
83
84     /******************************************************/
85     /** allocate an array to hold per window information **/
86     /******************************************************/
87     if ( (perWindow = (WinDataStruct *)malloc(nWindows * sizeof(WinDataStruct)))
88         == NULL) {
89         fprintf(stderr, "Out of memory.\n");
90         XCloseDisplay(dpy);
91         exit(1);
92     }
93
94     /*****************************************************/
95     /** get things set up for each window we were given **/
96     /*****************************************************/
97     for (i = 0; i < nWindows; i++) {
98         XWindowAttributes attr;
99
100         perWindow[i].id = winprop[i];
101         /*************************************/
102         /** get information for each window **/
103         /*************************************/
104         if (! XGetWindowAttributes(dpy, perWindow[i].id, &attr)) {
105             fprintf(stderr, "Unable to get window %d attributes.\n",
106                     perWindow[i].id);
107             free((void *)perWindow);
108             XCloseDisplay(dpy);
109             exit(1);
110         }
111
112         /***************************************/
113         /** save the window info we will need **/
114         /***************************************/
115         perWindow[i].width = attr.width;
116         perWindow[i].height = attr.height;
117         perWindow[i].cmap = DefaultColormapOfScreen(attr.screen);
118         perWindow[i].gc = DefaultGCOfScreen(attr.screen);
119
120         /***********************************************/
121         /** Allocate the colors we will use, fallback **/
122         /** to black and white if necessary           **/
123         /***********************************************/
124         if (XAllocNamedColor(dpy,perWindow[i].cmap,"red",&actual,&exact)){
125             perWindow[i].ssPixels[0] = actual.pixel;
126         } else {
127             perWindow[i].ssPixels[0] = BlackPixelOfScreen(attr.screen);
128         }
129         if (XAllocNamedColor(dpy,perWindow[i].cmap,"orange",&actual,&exact)){
130             perWindow[i].ssPixels[1] = actual.pixel;
131         } else {
132             perWindow[i].ssPixels[1] = WhitePixelOfScreen(attr.screen);
133         }
134         if (XAllocNamedColor(dpy,perWindow[i].cmap,"yellow",&actual,&exact)){
135             perWindow[i].ssPixels[2] = actual.pixel;
136         } else {
137             perWindow[i].ssPixels[2] = BlackPixelOfScreen(attr.screen);
138         }
139         if (XAllocNamedColor(dpy,perWindow[i].cmap,"green",&actual,&exact)){
140                 perWindow[i].ssPixels[3] = actual.pixel;
141         } else {
142                 perWindow[i].ssPixels[3] = WhitePixelOfScreen(attr.screen);
143         }
144         if (XAllocNamedColor(dpy,perWindow[i].cmap,"blue",&actual,&exact)){
145                 perWindow[i].ssPixels[4] = actual.pixel;
146         } else {
147                 perWindow[i].ssPixels[4] = BlackPixelOfScreen(attr.screen);;
148         }
149         if (XAllocNamedColor(dpy,perWindow[i].cmap,"purple",&actual,&exact)){
150             perWindow[i].ssPixels[5] = actual.pixel;
151         } else {
152             perWindow[i].ssPixels[5] = WhitePixelOfScreen(attr.screen);
153         }
154     }
155
156     /************************************/
157     /** OK, now enter our drawing loop **/
158     /************************************/
159     while (1) {
160         /************************/
161         /** update each window **/
162         /************************/
163         for (i = 0; i < nWindows; i++) {
164             /***************************/
165             /** for each color we use **/
166             /***************************/
167             for (j = 0; j < SS_MAX_COLORS; j++) {
168                 int x, y, width, height;
169                 unsigned long curColor;
170
171                 curColor = perWindow[i].ssPixels[
172                                 (int)fmod((j + colorRotate),SS_MAX_COLORS)];
173                 XSetBackground(dpy, perWindow[i].gc, curColor);
174                 XSetForeground(dpy, perWindow[i].gc, curColor);
175                 x = j * ((perWindow[i].width/2) / (SS_MAX_COLORS));
176                 y = j * ((perWindow[i].height/2) / (SS_MAX_COLORS));
177                 width = perWindow[i].width - (2 * x);
178                 height = perWindow[i].height - (2 * y);
179                 XFillRectangle(dpy, perWindow[i].id, perWindow[i].gc,
180                                x, y, width, height);
181             }
182         }
183         /**XFlush(dpy); **/
184         XSync(dpy, False);
185         colorRotate = (int) fmod ((colorRotate + 1), SS_MAX_COLORS);
186
187         usleep(200000);
188     }
189 }