dmxdpms.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation on the rights to use, copy, modify, merge,
  10. * publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. /*
  28. * Author:
  29. * Rickard E. (Rik) Faith <faith@redhat.com>
  30. *
  31. */
  32. /** \file
  33. * Provides DPMS support and unifies all DPMS and other screen-saver
  34. * support in one file. If -dpms is given on the command line, or the
  35. * Xdmx server is not compiled with DPMS support, then the DPMS extension
  36. * does not work for clients, but DPMS on the backends is still disables
  37. * (and restored at Xdmx server shutdown time).
  38. */
  39. #ifdef HAVE_DMX_CONFIG_H
  40. #include <dmx-config.h>
  41. #endif
  42. #include "dmx.h"
  43. #include "dmxdpms.h"
  44. #include "dmxlog.h"
  45. #include "dmxsync.h"
  46. #ifdef DPMSExtension
  47. #include "dpmsproc.h"
  48. #endif
  49. #include "windowstr.h" /* For screenIsSaved */
  50. #include <X11/extensions/dpms.h>
  51. static unsigned long dpmsGeneration = 0;
  52. static Bool dpmsSupported = TRUE;
  53. static void
  54. _dmxDPMSInit(DMXScreenInfo * dmxScreen)
  55. {
  56. int event_base, error_base;
  57. int major, minor;
  58. CARD16 level, standby, suspend, off;
  59. BOOL state;
  60. const char *monitor;
  61. if (dpmsGeneration != serverGeneration) {
  62. dpmsSupported = TRUE; /* On unless a backend doesn't support it */
  63. dpmsGeneration = serverGeneration;
  64. }
  65. #ifdef DPMSExtension
  66. if (DPMSDisabledSwitch)
  67. dpmsSupported = FALSE; /* -dpms turns off */
  68. #endif
  69. dmxScreen->dpmsCapable = 0;
  70. if (!dmxScreen->beDisplay) {
  71. dmxLogOutput(dmxScreen,
  72. "Cannot determine if DPMS supported (detached screen)\n");
  73. dpmsSupported = FALSE;
  74. return;
  75. }
  76. if (!DPMSQueryExtension(dmxScreen->beDisplay, &event_base, &error_base)) {
  77. dmxLogOutput(dmxScreen, "DPMS not supported\n");
  78. dpmsSupported = FALSE;
  79. return;
  80. }
  81. if (!DPMSGetVersion(dmxScreen->beDisplay, &major, &minor)) {
  82. dmxLogOutput(dmxScreen, "DPMS not supported\n");
  83. dpmsSupported = FALSE;
  84. return;
  85. }
  86. if (!DPMSCapable(dmxScreen->beDisplay)) {
  87. dmxLogOutput(dmxScreen, "DPMS %d.%d (not DPMS capable)\n",
  88. major, minor);
  89. dpmsSupported = FALSE;
  90. return;
  91. }
  92. DPMSInfo(dmxScreen->beDisplay, &level, &state);
  93. DPMSGetTimeouts(dmxScreen->beDisplay, &standby, &suspend, &off);
  94. DPMSSetTimeouts(dmxScreen->beDisplay, 0, 0, 0);
  95. DPMSEnable(dmxScreen->beDisplay);
  96. DPMSForceLevel(dmxScreen->beDisplay, DPMSModeOn);
  97. dmxScreen->dpmsCapable = 1;
  98. dmxScreen->dpmsEnabled = ! !state;
  99. dmxScreen->dpmsStandby = standby;
  100. dmxScreen->dpmsSuspend = suspend;
  101. dmxScreen->dpmsOff = off;
  102. switch (level) {
  103. case DPMSModeOn:
  104. monitor = "on";
  105. break;
  106. case DPMSModeStandby:
  107. monitor = "standby";
  108. break;
  109. case DPMSModeSuspend:
  110. monitor = "suspend";
  111. break;
  112. case DPMSModeOff:
  113. monitor = "off";
  114. break;
  115. default:
  116. monitor = "unknown";
  117. break;
  118. }
  119. dmxLogOutput(dmxScreen,
  120. "DPMS %d.%d (%s, %s, %d %d %d)\n",
  121. major, minor, monitor, state ? "enabled" : "disabled",
  122. standby, suspend, off);
  123. }
  124. /** Initialize DPMS support. We save the current settings and turn off
  125. * DPMS. The settings are restored in #dmxDPMSTerm. */
  126. void
  127. dmxDPMSInit(DMXScreenInfo * dmxScreen)
  128. {
  129. int interval, preferBlanking, allowExposures;
  130. /* Turn off DPMS */
  131. _dmxDPMSInit(dmxScreen);
  132. if (!dmxScreen->beDisplay)
  133. return;
  134. /* Turn off screen saver */
  135. XGetScreenSaver(dmxScreen->beDisplay, &dmxScreen->savedTimeout, &interval,
  136. &preferBlanking, &allowExposures);
  137. XSetScreenSaver(dmxScreen->beDisplay, 0, interval,
  138. preferBlanking, allowExposures);
  139. XResetScreenSaver(dmxScreen->beDisplay);
  140. dmxSync(dmxScreen, FALSE);
  141. }
  142. /** Terminate DPMS support on \a dmxScreen. We restore the settings
  143. * saved in #dmxDPMSInit. */
  144. void
  145. dmxDPMSTerm(DMXScreenInfo * dmxScreen)
  146. {
  147. int timeout, interval, preferBlanking, allowExposures;
  148. if (!dmxScreen->beDisplay)
  149. return;
  150. XGetScreenSaver(dmxScreen->beDisplay, &timeout, &interval,
  151. &preferBlanking, &allowExposures);
  152. XSetScreenSaver(dmxScreen->beDisplay, dmxScreen->savedTimeout, interval,
  153. preferBlanking, allowExposures);
  154. if (dmxScreen->dpmsCapable) {
  155. /* Restore saved state */
  156. DPMSForceLevel(dmxScreen->beDisplay, DPMSModeOn);
  157. DPMSSetTimeouts(dmxScreen->beDisplay, dmxScreen->dpmsStandby,
  158. dmxScreen->dpmsSuspend, dmxScreen->dpmsOff);
  159. if (dmxScreen->dpmsEnabled)
  160. DPMSEnable(dmxScreen->beDisplay);
  161. else
  162. DPMSDisable(dmxScreen->beDisplay);
  163. }
  164. dmxSync(dmxScreen, FALSE);
  165. }
  166. /** Called when activity is detected so that DPMS power-saving mode can
  167. * be deactivated. */
  168. void
  169. dmxDPMSWakeup(void)
  170. {
  171. if (screenIsSaved == SCREEN_SAVER_ON)
  172. dixSaveScreens(serverClient, SCREEN_SAVER_OFF, ScreenSaverReset);
  173. #ifdef DPMSExtension
  174. if (DPMSPowerLevel)
  175. DPMSSet(serverClient, 0);
  176. #endif
  177. }
  178. #ifdef DPMSExtension
  179. /** This is called on each server generation. It should determine if
  180. * DPMS is supported on all of the backends and, if so, return TRUE. */
  181. Bool
  182. DPMSSupported(void)
  183. {
  184. return dpmsSupported;
  185. }
  186. /** This is used by clients (e.g., xset) to set the DPMS level. */
  187. int
  188. DPMSSet(ClientPtr client, int level)
  189. {
  190. int i;
  191. if (!dpmsSupported)
  192. return Success;
  193. if (level < 0)
  194. level = DPMSModeOn;
  195. if (level > 3)
  196. level = DPMSModeOff;
  197. DPMSPowerLevel = level;
  198. for (i = 0; i < dmxNumScreens; i++) {
  199. DMXScreenInfo *dmxScreen = &dmxScreens[i];
  200. if (dmxScreen->beDisplay) {
  201. DPMSForceLevel(dmxScreen->beDisplay, level);
  202. dmxSync(dmxScreen, FALSE);
  203. }
  204. }
  205. return Success;
  206. }
  207. #endif