2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: screensaver.c /main/3 1995/10/27 10:40:15 rswiston $ */
24 /************************************/
25 /** Sample CDE Screen Saver Client **/
26 /************************************/
33 #include <X11/Intrinsic.h>
36 #define SS_MAX_COLORS 6
42 unsigned long ssPixels[SS_MAX_COLORS];
51 poll((struct poll *) 0, (size_t) 0, usec / 1000); /* milliseconds */
55 int main(int argc, char *argv[])
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 */
65 setlocale(LC_ALL, "");
67 /*******************************/
68 /** open a display connection **/
69 /*******************************/
70 if (!(dpy = XOpenDisplay(NULL)) ) {
71 fprintf(stderr, "Unable to open the Display.\n");
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");
84 /******************************************************/
85 /** allocate an array to hold per window information **/
86 /******************************************************/
87 if ( (perWindow = (WinDataStruct *)malloc(nWindows * sizeof(WinDataStruct)))
89 fprintf(stderr, "Out of memory.\n");
94 /*****************************************************/
95 /** get things set up for each window we were given **/
96 /*****************************************************/
97 for (i = 0; i < nWindows; i++) {
98 XWindowAttributes attr;
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",
107 free((void *)perWindow);
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);
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;
127 perWindow[i].ssPixels[0] = BlackPixelOfScreen(attr.screen);
129 if (XAllocNamedColor(dpy,perWindow[i].cmap,"orange",&actual,&exact)){
130 perWindow[i].ssPixels[1] = actual.pixel;
132 perWindow[i].ssPixels[1] = WhitePixelOfScreen(attr.screen);
134 if (XAllocNamedColor(dpy,perWindow[i].cmap,"yellow",&actual,&exact)){
135 perWindow[i].ssPixels[2] = actual.pixel;
137 perWindow[i].ssPixels[2] = BlackPixelOfScreen(attr.screen);
139 if (XAllocNamedColor(dpy,perWindow[i].cmap,"green",&actual,&exact)){
140 perWindow[i].ssPixels[3] = actual.pixel;
142 perWindow[i].ssPixels[3] = WhitePixelOfScreen(attr.screen);
144 if (XAllocNamedColor(dpy,perWindow[i].cmap,"blue",&actual,&exact)){
145 perWindow[i].ssPixels[4] = actual.pixel;
147 perWindow[i].ssPixels[4] = BlackPixelOfScreen(attr.screen);;
149 if (XAllocNamedColor(dpy,perWindow[i].cmap,"purple",&actual,&exact)){
150 perWindow[i].ssPixels[5] = actual.pixel;
152 perWindow[i].ssPixels[5] = WhitePixelOfScreen(attr.screen);
156 /************************************/
157 /** OK, now enter our drawing loop **/
158 /************************************/
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;
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);
185 colorRotate = (int) fmod ((colorRotate + 1), SS_MAX_COLORS);