First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / common / xf86DPMS.c
blob3aa83e882bc179df15347b92fb86617464d53b75
2 /*
3 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Except as contained in this notice, the name of the copyright holder(s)
24 * and author(s) shall not be used in advertising or otherwise to promote
25 * the sale, use or other dealings in this Software without prior written
26 * authorization from the copyright holder(s) and author(s).
30 * This file contains the DPMS functions required by the extension.
33 #ifdef HAVE_XORG_CONFIG_H
34 #include <xorg-config.h>
35 #endif
37 #include <X11/X.h>
38 #include "os.h"
39 #include "globals.h"
40 #include "xf86.h"
41 #include "xf86Priv.h"
42 #ifdef DPMSExtension
43 #define DPMS_SERVER
44 #include <X11/extensions/dpms.h>
45 #include "dpmsproc.h"
46 #endif
49 #ifdef DPMSExtension
50 static int DPMSGeneration = 0;
51 static int DPMSIndex = -1;
52 static Bool DPMSClose(int i, ScreenPtr pScreen);
53 static int DPMSCount = 0;
54 #endif
57 _X_EXPORT Bool
58 xf86DPMSInit(ScreenPtr pScreen, DPMSSetProcPtr set, int flags)
60 #ifdef DPMSExtension
61 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
62 DPMSPtr pDPMS;
63 pointer DPMSOpt;
65 if (serverGeneration != DPMSGeneration) {
66 if ((DPMSIndex = AllocateScreenPrivateIndex()) < 0)
67 return FALSE;
68 DPMSGeneration = serverGeneration;
71 if (DPMSDisabledSwitch)
72 DPMSEnabled = FALSE;
73 if (!(pScreen->devPrivates[DPMSIndex].ptr = xcalloc(sizeof(DPMSRec), 1)))
74 return FALSE;
76 pDPMS = (DPMSPtr)pScreen->devPrivates[DPMSIndex].ptr;
77 pScrn->DPMSSet = set;
78 pDPMS->Flags = flags;
79 DPMSOpt = xf86FindOption(pScrn->options, "dpms");
80 if (DPMSOpt) {
81 if ((pDPMS->Enabled
82 = xf86SetBoolOption(pScrn->options, "dpms", FALSE))
83 && !DPMSDisabledSwitch)
84 DPMSEnabled = TRUE;
85 xf86MarkOptionUsed(DPMSOpt);
86 xf86DrvMsg(pScreen->myNum, X_CONFIG, "DPMS enabled\n");
87 } else if (DPMSEnabledSwitch) {
88 if (!DPMSDisabledSwitch)
89 DPMSEnabled = TRUE;
90 pDPMS->Enabled = TRUE;
92 else {
93 pDPMS->Enabled = defaultDPMSEnabled;
95 pDPMS->CloseScreen = pScreen->CloseScreen;
96 pScreen->CloseScreen = DPMSClose;
97 DPMSCount++;
98 return TRUE;
99 #else
100 return FALSE;
101 #endif
105 #ifdef DPMSExtension
107 static Bool
108 DPMSClose(int i, ScreenPtr pScreen)
110 DPMSPtr pDPMS;
112 /* This shouldn't happen */
113 if (DPMSIndex < 0)
114 return FALSE;
116 pDPMS = (DPMSPtr)pScreen->devPrivates[DPMSIndex].ptr;
118 /* This shouldn't happen */
119 if (!pDPMS)
120 return FALSE;
122 pScreen->CloseScreen = pDPMS->CloseScreen;
125 * Turn on DPMS when shutting down. If this function can be used
126 * depends on the order the driver wraps things. If this is called
127 * after the driver has shut down everything the driver will have
128 * to deal with this internally.
130 if (xf86Screens[i]->vtSema && xf86Screens[i]->DPMSSet) {
131 xf86Screens[i]->DPMSSet(xf86Screens[i],DPMSModeOn,0);
134 xfree((pointer)pDPMS);
135 pScreen->devPrivates[DPMSIndex].ptr = NULL;
136 if (--DPMSCount == 0)
137 DPMSIndex = -1;
138 return pScreen->CloseScreen(i, pScreen);
143 * DPMSSet --
144 * Device dependent DPMS mode setting hook. This is called whenever
145 * the DPMS mode is to be changed.
147 _X_EXPORT void
148 DPMSSet(int level)
150 int i;
151 DPMSPtr pDPMS;
152 ScrnInfoPtr pScrn;
154 DPMSPowerLevel = level;
156 if (DPMSIndex < 0)
157 return;
159 if (level != DPMSModeOn)
160 SaveScreens(SCREEN_SAVER_FORCER, ScreenSaverActive);
162 /* For each screen, set the DPMS level */
163 for (i = 0; i < xf86NumScreens; i++) {
164 pScrn = xf86Screens[i];
165 pDPMS = (DPMSPtr)screenInfo.screens[i]->devPrivates[DPMSIndex].ptr;
166 if (pDPMS && pScrn->DPMSSet && pDPMS->Enabled && pScrn->vtSema) {
167 xf86EnableAccess(pScrn);
168 pScrn->DPMSSet(pScrn, level, 0);
175 * DPMSSupported --
176 * Return TRUE if any screen supports DPMS.
178 _X_EXPORT Bool
179 DPMSSupported(void)
181 int i;
182 DPMSPtr pDPMS;
183 ScrnInfoPtr pScrn;
185 if (DPMSIndex < 0) {
186 return FALSE;
189 /* For each screen, check if DPMS is supported */
190 for (i = 0; i < xf86NumScreens; i++) {
191 pScrn = xf86Screens[i];
192 pDPMS = (DPMSPtr)screenInfo.screens[i]->devPrivates[DPMSIndex].ptr;
193 if (pDPMS && pScrn->DPMSSet)
194 return TRUE;
196 return FALSE;
201 * DPMSGet --
202 * Device dependent DPMS mode getting hook. This returns the current
203 * DPMS mode, or -1 if DPMS is not supported.
205 * This should hook in to the appropriate driver-level function, which
206 * will be added to the ScrnInfoRec.
208 * NOTES:
209 * 1. the calling interface should be changed to specify which
210 * screen to check.
211 * 2. It isn't clear that this function is ever used or what it should
212 * return.
214 _X_EXPORT int
215 DPMSGet(int *level)
217 return DPMSPowerLevel;
220 #endif /* DPMSExtension */