xf86DPMS.c revision 05b261ec
1
2/*
3 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
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).
27 */
28
29/*
30 * This file contains the DPMS functions required by the extension.
31 */
32
33#ifdef HAVE_XORG_CONFIG_H
34#include <xorg-config.h>
35#endif
36
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
47
48
49#ifdef DPMSExtension
50static int DPMSGeneration = 0;
51static int DPMSIndex = -1;
52static Bool DPMSClose(int i, ScreenPtr pScreen);
53static int DPMSCount = 0;
54#endif
55
56
57_X_EXPORT Bool
58xf86DPMSInit(ScreenPtr pScreen, DPMSSetProcPtr set, int flags)
59{
60#ifdef DPMSExtension
61    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
62    DPMSPtr pDPMS;
63    pointer DPMSOpt;
64
65    if (serverGeneration != DPMSGeneration) {
66	if ((DPMSIndex = AllocateScreenPrivateIndex()) < 0)
67	    return FALSE;
68	DPMSGeneration = serverGeneration;
69    }
70
71    if (DPMSDisabledSwitch)
72	DPMSEnabled = FALSE;
73    if (!(pScreen->devPrivates[DPMSIndex].ptr = xcalloc(sizeof(DPMSRec), 1)))
74	return FALSE;
75
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;
91    }
92    else {
93	pDPMS->Enabled = defaultDPMSEnabled;
94    }
95    pDPMS->CloseScreen = pScreen->CloseScreen;
96    pScreen->CloseScreen = DPMSClose;
97    DPMSCount++;
98    return TRUE;
99#else
100    return FALSE;
101#endif
102}
103
104
105#ifdef DPMSExtension
106
107static Bool
108DPMSClose(int i, ScreenPtr pScreen)
109{
110    DPMSPtr pDPMS;
111
112    /* This shouldn't happen */
113    if (DPMSIndex < 0)
114	return FALSE;
115
116    pDPMS = (DPMSPtr)pScreen->devPrivates[DPMSIndex].ptr;
117
118    /* This shouldn't happen */
119    if (!pDPMS)
120	return FALSE;
121
122    pScreen->CloseScreen = pDPMS->CloseScreen;
123
124    /*
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.
129     */
130    if (xf86Screens[i]->vtSema && xf86Screens[i]->DPMSSet) {
131 	xf86Screens[i]->DPMSSet(xf86Screens[i],DPMSModeOn,0);
132    }
133
134    xfree((pointer)pDPMS);
135    pScreen->devPrivates[DPMSIndex].ptr = NULL;
136    if (--DPMSCount == 0)
137	DPMSIndex = -1;
138    return pScreen->CloseScreen(i, pScreen);
139}
140
141
142/*
143 * DPMSSet --
144 *	Device dependent DPMS mode setting hook.  This is called whenever
145 *	the DPMS mode is to be changed.
146 */
147_X_EXPORT void
148DPMSSet(int level)
149{
150    int i;
151    DPMSPtr pDPMS;
152    ScrnInfoPtr pScrn;
153
154    DPMSPowerLevel = level;
155
156    if (DPMSIndex < 0)
157	return;
158
159    if (level != DPMSModeOn)
160	SaveScreens(SCREEN_SAVER_FORCER, ScreenSaverActive);
161
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);
169	}
170    }
171}
172
173
174/*
175 * DPMSSupported --
176 *	Return TRUE if any screen supports DPMS.
177 */
178_X_EXPORT Bool
179DPMSSupported(void)
180{
181    int i;
182    DPMSPtr pDPMS;
183    ScrnInfoPtr pScrn;
184
185    if (DPMSIndex < 0) {
186	return FALSE;
187    }
188
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;
195    }
196    return FALSE;
197}
198
199
200/*
201 * DPMSGet --
202 *	Device dependent DPMS mode getting hook.  This returns the current
203 *	DPMS mode, or -1 if DPMS is not supported.
204 *
205 *	This should hook in to the appropriate driver-level function, which
206 *	will be added to the ScrnInfoRec.
207 *
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.
213 */
214_X_EXPORT int
215DPMSGet(int *level)
216{
217    return DPMSPowerLevel;
218}
219
220#endif /* DPMSExtension */
221