dpms.c revision 05b261ec
1/*****************************************************************
2
3Copyright (c) 1996 Digital Equipment Corporation, Maynard, Massachusetts.
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
18BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
19WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
20IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of Digital Equipment Corporation
23shall not be used in advertising or otherwise to promote the sale, use or other
24dealings in this Software without prior written authorization from Digital
25Equipment Corporation.
26
27******************************************************************/
28
29/*
30 * HISTORY
31 *
32 * @(#)RCSfile: dpms.c,v Revision: 1.1.4.5  (DEC) Date: 1996/03/04 15:27:00
33 */
34
35
36#ifdef HAVE_DIX_CONFIG_H
37#include <dix-config.h>
38#endif
39
40#include <X11/X.h>
41#include <X11/Xproto.h>
42#include "misc.h"
43#include "os.h"
44#include "dixstruct.h"
45#include "extnsionst.h"
46#include "opaque.h"
47#define DPMS_SERVER
48#include <X11/extensions/dpms.h>
49#include <X11/extensions/dpmsstr.h>
50#include "dpmsproc.h"
51#include "modinit.h"
52
53#if 0
54static unsigned char DPMSCode;
55#endif
56static DISPATCH_PROC(ProcDPMSDispatch);
57static DISPATCH_PROC(SProcDPMSDispatch);
58static DISPATCH_PROC(ProcDPMSGetVersion);
59static DISPATCH_PROC(SProcDPMSGetVersion);
60static DISPATCH_PROC(ProcDPMSGetTimeouts);
61static DISPATCH_PROC(SProcDPMSGetTimeouts);
62static DISPATCH_PROC(ProcDPMSSetTimeouts);
63static DISPATCH_PROC(SProcDPMSSetTimeouts);
64static DISPATCH_PROC(ProcDPMSEnable);
65static DISPATCH_PROC(SProcDPMSEnable);
66static DISPATCH_PROC(ProcDPMSDisable);
67static DISPATCH_PROC(SProcDPMSDisable);
68static DISPATCH_PROC(ProcDPMSForceLevel);
69static DISPATCH_PROC(SProcDPMSForceLevel);
70static DISPATCH_PROC(ProcDPMSInfo);
71static DISPATCH_PROC(SProcDPMSInfo);
72static DISPATCH_PROC(ProcDPMSCapable);
73static DISPATCH_PROC(SProcDPMSCapable);
74static void DPMSResetProc(ExtensionEntry* extEntry);
75
76void
77DPMSExtensionInit(INITARGS)
78{
79#if 0
80    ExtensionEntry *extEntry;
81
82    if ((extEntry = AddExtension(DPMSExtensionName, 0, 0,
83				ProcDPMSDispatch, SProcDPMSDispatch,
84				DPMSResetProc, StandardMinorOpcode)))
85	DPMSCode = (unsigned char)extEntry->base;
86#else
87    (void) AddExtension(DPMSExtensionName, 0, 0,
88			ProcDPMSDispatch, SProcDPMSDispatch,
89			DPMSResetProc, StandardMinorOpcode);
90#endif
91}
92
93/*ARGSUSED*/
94static void
95DPMSResetProc (extEntry)
96    ExtensionEntry	*extEntry;
97{
98}
99
100static int
101ProcDPMSGetVersion(client)
102    register ClientPtr client;
103{
104    /* REQUEST(xDPMSGetVersionReq); */
105    xDPMSGetVersionReply rep;
106    register int n;
107
108    REQUEST_SIZE_MATCH(xDPMSGetVersionReq);
109
110    rep.type = X_Reply;
111    rep.length = 0;
112    rep.sequenceNumber = client->sequence;
113    rep.majorVersion = DPMSMajorVersion;
114    rep.minorVersion = DPMSMinorVersion;
115    if (client->swapped) {
116    	swaps(&rep.sequenceNumber, n);
117	swaps(&rep.majorVersion, n);
118	swaps(&rep.minorVersion, n);
119    }
120    WriteToClient(client, sizeof(xDPMSGetVersionReply), (char *)&rep);
121    return(client->noClientException);
122}
123
124static int
125ProcDPMSCapable(register ClientPtr client)
126{
127    /* REQUEST(xDPMSCapableReq); */
128    xDPMSCapableReply rep;
129    register int n;
130
131    REQUEST_SIZE_MATCH(xDPMSCapableReq);
132
133    rep.type = X_Reply;
134    rep.length = 0;
135    rep.sequenceNumber = client->sequence;
136    rep.capable = DPMSCapableFlag;
137
138    if (client->swapped) {
139	swaps(&rep.sequenceNumber, n);
140    }
141    WriteToClient(client, sizeof(xDPMSCapableReply), (char *)&rep);
142    return(client->noClientException);
143}
144
145static int
146ProcDPMSGetTimeouts(client)
147    register ClientPtr client;
148{
149    /* REQUEST(xDPMSGetTimeoutsReq); */
150    xDPMSGetTimeoutsReply rep;
151    register int n;
152
153    REQUEST_SIZE_MATCH(xDPMSGetTimeoutsReq);
154
155    rep.type = X_Reply;
156    rep.length = 0;
157    rep.sequenceNumber = client->sequence;
158    rep.standby = DPMSStandbyTime / MILLI_PER_SECOND;
159    rep.suspend = DPMSSuspendTime / MILLI_PER_SECOND;
160    rep.off = DPMSOffTime / MILLI_PER_SECOND;
161
162    if (client->swapped) {
163    	swaps(&rep.sequenceNumber, n);
164	swaps(&rep.standby, n);
165	swaps(&rep.suspend, n);
166	swaps(&rep.off, n);
167    }
168    WriteToClient(client, sizeof(xDPMSGetTimeoutsReply), (char *)&rep);
169    return(client->noClientException);
170}
171
172static int
173ProcDPMSSetTimeouts(client)
174    register ClientPtr client;
175{
176    REQUEST(xDPMSSetTimeoutsReq);
177
178    REQUEST_SIZE_MATCH(xDPMSSetTimeoutsReq);
179
180    if ((stuff->off != 0)&&(stuff->off < stuff->suspend))
181    {
182	client->errorValue = stuff->off;
183	return BadValue;
184    }
185    if ((stuff->suspend != 0)&&(stuff->suspend < stuff->standby))
186    {
187	client->errorValue = stuff->suspend;
188	return BadValue;
189    }
190
191    DPMSStandbyTime = stuff->standby * MILLI_PER_SECOND;
192    DPMSSuspendTime = stuff->suspend * MILLI_PER_SECOND;
193    DPMSOffTime = stuff->off * MILLI_PER_SECOND;
194    SetScreenSaverTimer();
195
196    return(client->noClientException);
197}
198
199static int
200ProcDPMSEnable(client)
201    register ClientPtr client;
202{
203    Bool was_enabled = DPMSEnabled;
204
205    REQUEST_SIZE_MATCH(xDPMSEnableReq);
206
207    if (DPMSCapableFlag) {
208	DPMSEnabled = TRUE;
209	if (!was_enabled)
210	    SetScreenSaverTimer();
211    }
212
213    return(client->noClientException);
214}
215
216static int
217ProcDPMSDisable(client)
218    register ClientPtr client;
219{
220    /* REQUEST(xDPMSDisableReq); */
221
222    REQUEST_SIZE_MATCH(xDPMSDisableReq);
223
224    DPMSSet(DPMSModeOn);
225
226    DPMSEnabled = FALSE;
227
228    return(client->noClientException);
229}
230
231static int
232ProcDPMSForceLevel(client)
233    register ClientPtr client;
234{
235    REQUEST(xDPMSForceLevelReq);
236
237    REQUEST_SIZE_MATCH(xDPMSForceLevelReq);
238
239    if (!DPMSEnabled)
240	return BadMatch;
241
242    if (stuff->level == DPMSModeOn) {
243      lastDeviceEventTime.milliseconds =
244          GetTimeInMillis();
245    } else if (stuff->level == DPMSModeStandby) {
246      lastDeviceEventTime.milliseconds =
247          GetTimeInMillis() -  DPMSStandbyTime;
248    } else if (stuff->level == DPMSModeSuspend) {
249      lastDeviceEventTime.milliseconds =
250          GetTimeInMillis() -  DPMSSuspendTime;
251    } else if (stuff->level == DPMSModeOff) {
252      lastDeviceEventTime.milliseconds =
253          GetTimeInMillis() -  DPMSOffTime;
254    } else {
255	client->errorValue = stuff->level;
256	return BadValue;
257    }
258
259    DPMSSet(stuff->level);
260
261    return(client->noClientException);
262}
263
264static int
265ProcDPMSInfo(register ClientPtr client)
266{
267    /* REQUEST(xDPMSInfoReq); */
268    xDPMSInfoReply rep;
269    register int n;
270
271    REQUEST_SIZE_MATCH(xDPMSInfoReq);
272
273    rep.type = X_Reply;
274    rep.length = 0;
275    rep.sequenceNumber = client->sequence;
276    rep.power_level = DPMSPowerLevel;
277    rep.state = DPMSEnabled;
278
279    if (client->swapped) {
280    	swaps(&rep.sequenceNumber, n);
281	swaps(&rep.power_level, n);
282    }
283    WriteToClient(client, sizeof(xDPMSInfoReply), (char *)&rep);
284    return(client->noClientException);
285}
286
287static int
288ProcDPMSDispatch (client)
289    register ClientPtr	client;
290{
291    REQUEST(xReq);
292
293    switch (stuff->data)
294    {
295    case X_DPMSGetVersion:
296	return ProcDPMSGetVersion(client);
297    case X_DPMSCapable:
298	return ProcDPMSCapable(client);
299    case X_DPMSGetTimeouts:
300	return ProcDPMSGetTimeouts(client);
301    case X_DPMSSetTimeouts:
302	return ProcDPMSSetTimeouts(client);
303    case X_DPMSEnable:
304	return ProcDPMSEnable(client);
305    case X_DPMSDisable:
306	return ProcDPMSDisable(client);
307    case X_DPMSForceLevel:
308	return ProcDPMSForceLevel(client);
309    case X_DPMSInfo:
310	return ProcDPMSInfo(client);
311    default:
312	return BadRequest;
313    }
314}
315
316static int
317SProcDPMSGetVersion(client)
318    register ClientPtr	client;
319{
320    register int n;
321    REQUEST(xDPMSGetVersionReq);
322
323    swaps(&stuff->length, n);
324    REQUEST_SIZE_MATCH(xDPMSGetVersionReq);
325    swaps(&stuff->majorVersion, n);
326    swaps(&stuff->minorVersion, n);
327    return ProcDPMSGetVersion(client);
328}
329
330static int
331SProcDPMSCapable(register ClientPtr client)
332{
333    REQUEST(xDPMSCapableReq);
334    register int n;
335
336    swaps(&stuff->length, n);
337    REQUEST_SIZE_MATCH(xDPMSCapableReq);
338
339    return ProcDPMSCapable(client);
340}
341
342static int
343SProcDPMSGetTimeouts(client)
344    register ClientPtr client;
345{
346    REQUEST(xDPMSGetTimeoutsReq);
347    register int n;
348
349    swaps(&stuff->length, n);
350    REQUEST_SIZE_MATCH(xDPMSGetTimeoutsReq);
351
352    return ProcDPMSGetTimeouts(client);
353}
354
355static int
356SProcDPMSSetTimeouts(client)
357    register ClientPtr client;
358{
359    REQUEST(xDPMSSetTimeoutsReq);
360    register int n;
361
362    swaps(&stuff->length, n);
363    REQUEST_SIZE_MATCH(xDPMSSetTimeoutsReq);
364
365    swaps(&stuff->standby, n);
366    swaps(&stuff->suspend, n);
367    swaps(&stuff->off, n);
368    return ProcDPMSSetTimeouts(client);
369}
370
371static int
372SProcDPMSEnable(client)
373    register ClientPtr client;
374{
375    REQUEST(xDPMSEnableReq);
376    register int n;
377
378    swaps(&stuff->length, n);
379    REQUEST_SIZE_MATCH(xDPMSEnableReq);
380
381    return ProcDPMSEnable(client);
382}
383
384static int
385SProcDPMSDisable(client)
386    register ClientPtr client;
387{
388    REQUEST(xDPMSDisableReq);
389    register int n;
390
391    swaps(&stuff->length, n);
392    REQUEST_SIZE_MATCH(xDPMSDisableReq);
393
394    return ProcDPMSDisable(client);
395}
396
397static int
398SProcDPMSForceLevel(client)
399    register ClientPtr client;
400{
401    REQUEST(xDPMSForceLevelReq);
402    register int n;
403
404    swaps(&stuff->length, n);
405    REQUEST_SIZE_MATCH(xDPMSForceLevelReq);
406
407    swaps(&stuff->level, n);
408
409    return ProcDPMSForceLevel(client);
410}
411
412static int
413SProcDPMSInfo(client)
414    register ClientPtr client;
415{
416    REQUEST(xDPMSInfoReq);
417    register int n;
418
419    swaps(&stuff->length, n);
420    REQUEST_SIZE_MATCH(xDPMSInfoReq);
421
422    return ProcDPMSInfo(client);
423}
424
425static int
426SProcDPMSDispatch (client)
427    register ClientPtr	client;
428{
429    REQUEST(xReq);
430    switch (stuff->data)
431    {
432    case X_DPMSGetVersion:
433	return SProcDPMSGetVersion(client);
434    case X_DPMSCapable:
435	return SProcDPMSCapable(client);
436    case X_DPMSGetTimeouts:
437	return SProcDPMSGetTimeouts(client);
438    case X_DPMSSetTimeouts:
439	return SProcDPMSSetTimeouts(client);
440    case X_DPMSEnable:
441	return SProcDPMSEnable(client);
442    case X_DPMSDisable:
443	return SProcDPMSDisable(client);
444    case X_DPMSForceLevel:
445	return SProcDPMSForceLevel(client);
446    case X_DPMSInfo:
447	return SProcDPMSInfo(client);
448    default:
449	return BadRequest;
450    }
451}
452