2
0

screensaver.c 6.3 KB

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