1706f2543Smrg/*
2706f2543Smrg * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
3706f2543Smrg *
4706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5706f2543Smrg * copy of this software and associated documentation files (the "Software"),
6706f2543Smrg * to deal in the Software without restriction, including without limitation
7706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
9706f2543Smrg * Software is furnished to do so, subject to the following conditions:
10706f2543Smrg *
11706f2543Smrg * The above copyright notice and this permission notice shall be included in
12706f2543Smrg * all copies or substantial portions of the Software.
13706f2543Smrg *
14706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17706f2543Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18706f2543Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19706f2543Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20706f2543Smrg * OTHER DEALINGS IN THE SOFTWARE.
21706f2543Smrg *
22706f2543Smrg * Except as contained in this notice, the name of the copyright holder(s)
23706f2543Smrg * and author(s) shall not be used in advertising or otherwise to promote
24706f2543Smrg * the sale, use or other dealings in this Software without prior written
25706f2543Smrg * authorization from the copyright holder(s) and author(s).
26706f2543Smrg */
27706f2543Smrg
28706f2543Smrg/*
29706f2543Smrg * This file contains the DPMS functions required by the extension.
30706f2543Smrg */
31706f2543Smrg
32706f2543Smrg#ifdef HAVE_XORG_CONFIG_H
33706f2543Smrg#include <xorg-config.h>
34706f2543Smrg#endif
35706f2543Smrg
36706f2543Smrg#include <X11/X.h>
37706f2543Smrg#include "os.h"
38706f2543Smrg#include "globals.h"
39706f2543Smrg#include "xf86.h"
40706f2543Smrg#include "xf86Priv.h"
41706f2543Smrg#ifdef DPMSExtension
42706f2543Smrg#include <X11/extensions/dpmsconst.h>
43706f2543Smrg#include "dpmsproc.h"
44706f2543Smrg#endif
45706f2543Smrg#include "xf86VGAarbiter.h"
46706f2543Smrg
47706f2543Smrg
48706f2543Smrg#ifdef DPMSExtension
49706f2543Smrgstatic DevPrivateKeyRec DPMSKeyRec;
50706f2543Smrgstatic DevPrivateKey DPMSKey;
51706f2543Smrgstatic Bool DPMSClose(int i, ScreenPtr pScreen);
52706f2543Smrgstatic int DPMSCount = 0;
53706f2543Smrg#endif
54706f2543Smrg
55706f2543Smrg
56706f2543SmrgBool
57706f2543Smrgxf86DPMSInit(ScreenPtr pScreen, DPMSSetProcPtr set, int flags)
58706f2543Smrg{
59706f2543Smrg#ifdef DPMSExtension
60706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
61706f2543Smrg    DPMSPtr pDPMS;
62706f2543Smrg    pointer DPMSOpt;
63706f2543Smrg    MessageType enabled_from;
64706f2543Smrg
65706f2543Smrg    DPMSKey = &DPMSKeyRec;
66706f2543Smrg
67706f2543Smrg    if (!dixRegisterPrivateKey(&DPMSKeyRec, PRIVATE_SCREEN, sizeof (DPMSRec)))
68706f2543Smrg	return FALSE;
69706f2543Smrg
70706f2543Smrg    pDPMS = dixLookupPrivate(&pScreen->devPrivates, DPMSKey);
71706f2543Smrg    pScrn->DPMSSet = set;
72706f2543Smrg    pDPMS->Flags = flags;
73706f2543Smrg    DPMSOpt = xf86FindOption(pScrn->options, "dpms");
74706f2543Smrg    if (DPMSDisabledSwitch) {
75706f2543Smrg	enabled_from = X_CMDLINE;
76706f2543Smrg	DPMSEnabled = FALSE;
77706f2543Smrg    }
78706f2543Smrg    else if (DPMSOpt) {
79706f2543Smrg	enabled_from = X_CONFIG;
80706f2543Smrg	DPMSEnabled = xf86CheckBoolOption(pScrn->options, "dpms", FALSE);
81706f2543Smrg	xf86MarkOptionUsed(DPMSOpt);
82706f2543Smrg    }
83706f2543Smrg    else {
84706f2543Smrg	enabled_from = X_DEFAULT;
85706f2543Smrg	DPMSEnabled = TRUE;
86706f2543Smrg    }
87706f2543Smrg    if (DPMSEnabled)
88706f2543Smrg	xf86DrvMsg(pScreen->myNum, enabled_from, "DPMS enabled\n");
89706f2543Smrg    pDPMS->Enabled = DPMSEnabled;
90706f2543Smrg    pDPMS->CloseScreen = pScreen->CloseScreen;
91706f2543Smrg    pScreen->CloseScreen = DPMSClose;
92706f2543Smrg    DPMSCount++;
93706f2543Smrg    return TRUE;
94706f2543Smrg#else
95706f2543Smrg    return FALSE;
96706f2543Smrg#endif
97706f2543Smrg}
98706f2543Smrg
99706f2543Smrg
100706f2543Smrg#ifdef DPMSExtension
101706f2543Smrg
102706f2543Smrgstatic Bool
103706f2543SmrgDPMSClose(int i, ScreenPtr pScreen)
104706f2543Smrg{
105706f2543Smrg    DPMSPtr pDPMS;
106706f2543Smrg
107706f2543Smrg    /* This shouldn't happen */
108706f2543Smrg    if (DPMSKey == NULL)
109706f2543Smrg	return FALSE;
110706f2543Smrg
111706f2543Smrg    pDPMS = dixLookupPrivate(&pScreen->devPrivates, DPMSKey);
112706f2543Smrg
113706f2543Smrg    /* This shouldn't happen */
114706f2543Smrg    if (!pDPMS)
115706f2543Smrg	return FALSE;
116706f2543Smrg
117706f2543Smrg    pScreen->CloseScreen = pDPMS->CloseScreen;
118706f2543Smrg
119706f2543Smrg    /*
120706f2543Smrg     * Turn on DPMS when shutting down. If this function can be used
121706f2543Smrg     * depends on the order the driver wraps things. If this is called
122706f2543Smrg     * after the driver has shut down everything the driver will have
123706f2543Smrg     * to deal with this internally.
124706f2543Smrg     */
125706f2543Smrg    if (xf86Screens[i]->vtSema && xf86Screens[i]->DPMSSet) {
126706f2543Smrg 	xf86Screens[i]->DPMSSet(xf86Screens[i],DPMSModeOn,0);
127706f2543Smrg    }
128706f2543Smrg
129706f2543Smrg    if (--DPMSCount == 0)
130706f2543Smrg	DPMSKey = NULL;
131706f2543Smrg    return pScreen->CloseScreen(i, pScreen);
132706f2543Smrg}
133706f2543Smrg
134706f2543Smrg
135706f2543Smrg/*
136706f2543Smrg * DPMSSet --
137706f2543Smrg *	Device dependent DPMS mode setting hook.  This is called whenever
138706f2543Smrg *	the DPMS mode is to be changed.
139706f2543Smrg */
140706f2543Smrgint
141706f2543SmrgDPMSSet(ClientPtr client, int level)
142706f2543Smrg{
143706f2543Smrg    int rc, i;
144706f2543Smrg    DPMSPtr pDPMS;
145706f2543Smrg    ScrnInfoPtr pScrn;
146706f2543Smrg
147706f2543Smrg    DPMSPowerLevel = level;
148706f2543Smrg
149706f2543Smrg    if (DPMSKey == NULL)
150706f2543Smrg	return Success;
151706f2543Smrg
152706f2543Smrg    if (level != DPMSModeOn) {
153706f2543Smrg	rc = dixSaveScreens(client, SCREEN_SAVER_FORCER, ScreenSaverActive);
154706f2543Smrg	if (rc != Success)
155706f2543Smrg	    return rc;
156706f2543Smrg    }
157706f2543Smrg
158706f2543Smrg    /* For each screen, set the DPMS level */
159706f2543Smrg    for (i = 0; i < xf86NumScreens; i++) {
160706f2543Smrg    	pScrn = xf86Screens[i];
161706f2543Smrg	pDPMS = dixLookupPrivate(&screenInfo.screens[i]->devPrivates, DPMSKey);
162706f2543Smrg	if (pDPMS && pScrn->DPMSSet && pDPMS->Enabled && pScrn->vtSema) {
163706f2543Smrg	    xf86VGAarbiterLock(pScrn);
164706f2543Smrg	    pScrn->DPMSSet(pScrn, level, 0);
165706f2543Smrg	    xf86VGAarbiterUnlock(pScrn);
166706f2543Smrg	}
167706f2543Smrg    }
168706f2543Smrg    return Success;
169706f2543Smrg}
170706f2543Smrg
171706f2543Smrg
172706f2543Smrg/*
173706f2543Smrg * DPMSSupported --
174706f2543Smrg *	Return TRUE if any screen supports DPMS.
175706f2543Smrg */
176706f2543SmrgBool
177706f2543SmrgDPMSSupported(void)
178706f2543Smrg{
179706f2543Smrg    int i;
180706f2543Smrg    DPMSPtr pDPMS;
181706f2543Smrg    ScrnInfoPtr pScrn;
182706f2543Smrg
183706f2543Smrg    if (DPMSKey == NULL) {
184706f2543Smrg	return FALSE;
185706f2543Smrg    }
186706f2543Smrg
187706f2543Smrg    /* For each screen, check if DPMS is supported */
188706f2543Smrg    for (i = 0; i < xf86NumScreens; i++) {
189706f2543Smrg    	pScrn = xf86Screens[i];
190706f2543Smrg	pDPMS = dixLookupPrivate(&screenInfo.screens[i]->devPrivates, DPMSKey);
191706f2543Smrg	if (pDPMS && pScrn->DPMSSet)
192706f2543Smrg	    return TRUE;
193706f2543Smrg    }
194706f2543Smrg    return FALSE;
195706f2543Smrg}
196706f2543Smrg
197706f2543Smrg#endif /* DPMSExtension */
198