Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / xfree86 / common / xf86DPMS.c
CommitLineData
a09e091a
JB
1/*
2 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
26 */
27
28/*
29 * This file contains the DPMS functions required by the extension.
30 */
31
32#ifdef HAVE_XORG_CONFIG_H
33#include <xorg-config.h>
34#endif
35
36#include <X11/X.h>
37#include "os.h"
38#include "globals.h"
39#include "windowstr.h"
40#include "xf86.h"
41#include "xf86Priv.h"
42#ifdef DPMSExtension
43#include <X11/extensions/dpmsconst.h>
44#include "dpmsproc.h"
45#endif
46#ifdef XSERVER_LIBPCIACCESS
47#include "xf86VGAarbiter.h"
48#endif
49
50#ifdef DPMSExtension
51static DevPrivateKeyRec DPMSKeyRec;
52static DevPrivateKey DPMSKey;
53static Bool DPMSClose(ScreenPtr pScreen);
54static int DPMSCount = 0;
55#endif
56
57Bool
58xf86DPMSInit(ScreenPtr pScreen, DPMSSetProcPtr set, int flags)
59{
60#ifdef DPMSExtension
61 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
62 DPMSPtr pDPMS;
63 pointer DPMSOpt;
64 MessageType enabled_from;
65
66 DPMSKey = &DPMSKeyRec;
67
68 if (!dixRegisterPrivateKey(&DPMSKeyRec, PRIVATE_SCREEN, sizeof(DPMSRec)))
69 return FALSE;
70
71 pDPMS = dixLookupPrivate(&pScreen->devPrivates, DPMSKey);
72 pScrn->DPMSSet = set;
73 pDPMS->Flags = flags;
74 DPMSOpt = xf86FindOption(pScrn->options, "dpms");
75 if (DPMSDisabledSwitch) {
76 enabled_from = X_CMDLINE;
77 DPMSEnabled = FALSE;
78 }
79 else if (DPMSOpt) {
80 enabled_from = X_CONFIG;
81 DPMSEnabled = xf86CheckBoolOption(pScrn->options, "dpms", FALSE);
82 xf86MarkOptionUsed(DPMSOpt);
83 }
84 else {
85 enabled_from = X_DEFAULT;
86 DPMSEnabled = TRUE;
87 }
88 if (DPMSEnabled)
89 xf86DrvMsg(pScreen->myNum, enabled_from, "DPMS enabled\n");
90 pDPMS->Enabled = DPMSEnabled;
91 pDPMS->CloseScreen = pScreen->CloseScreen;
92 pScreen->CloseScreen = DPMSClose;
93 DPMSCount++;
94 return TRUE;
95#else
96 return FALSE;
97#endif
98}
99
100#ifdef DPMSExtension
101
102static Bool
103DPMSClose(ScreenPtr pScreen)
104{
105 DPMSPtr pDPMS;
106 ScrnInfoPtr pScrn;
107 /* This shouldn't happen */
108 if (DPMSKey == NULL)
109 return FALSE;
110
111 pDPMS = dixLookupPrivate(&pScreen->devPrivates, DPMSKey);
112
113 /* This shouldn't happen */
114 if (!pDPMS)
115 return FALSE;
116
117 pScreen->CloseScreen = pDPMS->CloseScreen;
118 pScrn = xf86ScreenToScrn(pScreen);
119 /*
120 * Turn on DPMS when shutting down. If this function can be used
121 * depends on the order the driver wraps things. If this is called
122 * after the driver has shut down everything the driver will have
123 * to deal with this internally.
124 */
125 if (pScrn->vtSema && pScrn->DPMSSet) {
126 pScrn->DPMSSet(pScrn, DPMSModeOn, 0);
127 }
128
129 if (--DPMSCount == 0)
130 DPMSKey = NULL;
131 return pScreen->CloseScreen(pScreen);
132}
133
134static void
135DPMSSetScreen(ScrnInfoPtr pScrn, int level)
136{
137 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
138 DPMSPtr pDPMS = dixLookupPrivate(&pScreen->devPrivates, DPMSKey);
139
140 if (pDPMS && pScrn->DPMSSet && pDPMS->Enabled && pScrn->vtSema) {
141 xf86VGAarbiterLock(pScrn);
142 pScrn->DPMSSet(pScrn, level, 0);
143 xf86VGAarbiterUnlock(pScrn);
144 }
145}
146
147/*
148 * DPMSSet --
149 * Device dependent DPMS mode setting hook. This is called whenever
150 * the DPMS mode is to be changed.
151 */
152int
153DPMSSet(ClientPtr client, int level)
154{
155 int rc, i;
156
157 DPMSPowerLevel = level;
158
159 if (DPMSKey == NULL)
160 return Success;
161
162 if (level != DPMSModeOn) {
163 if (xf86IsUnblank(screenIsSaved)) {
164 rc = dixSaveScreens(client, SCREEN_SAVER_FORCER, ScreenSaverActive);
165 if (rc != Success)
166 return rc;
167 }
168 } else if (!xf86IsUnblank(screenIsSaved)) {
169 rc = dixSaveScreens(client, SCREEN_SAVER_FORCER, ScreenSaverReset);
170 if (rc != Success)
171 return rc;
172 }
173
174 /* For each screen, set the DPMS level */
175 for (i = 0; i < xf86NumScreens; i++) {
176 DPMSSetScreen(xf86Screens[i], level);
177 }
178 for (i = 0; i < xf86NumGPUScreens; i++) {
179 DPMSSetScreen(xf86GPUScreens[i], level);
180 }
181 return Success;
182}
183
184static Bool
185DPMSSupportedOnScreen(ScrnInfoPtr pScrn)
186{
187 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
188 DPMSPtr pDPMS = dixLookupPrivate(&pScreen->devPrivates, DPMSKey);
189
190 return pDPMS && pScrn->DPMSSet;
191}
192
193/*
194 * DPMSSupported --
195 * Return TRUE if any screen supports DPMS.
196 */
197Bool
198DPMSSupported(void)
199{
200 int i;
201
202 if (DPMSKey == NULL) {
203 return FALSE;
204 }
205
206 /* For each screen, check if DPMS is supported */
207 for (i = 0; i < xf86NumScreens; i++) {
208 if (DPMSSupportedOnScreen(xf86Screens[i]))
209 return TRUE;
210 }
211 for (i = 0; i < xf86NumGPUScreens; i++) {
212 if (DPMSSupportedOnScreen(xf86GPUScreens[i]))
213 return TRUE;
214 }
215 return FALSE;
216}
217
218#endif /* DPMSExtension */