dpms.c revision 684baedf
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/dpmsproto.h>
50#include "dpmsproc.h"
51#include "modinit.h"
52
53static DISPATCH_PROC(ProcDPMSDispatch);
54static DISPATCH_PROC(SProcDPMSDispatch);
55static DISPATCH_PROC(ProcDPMSGetVersion);
56static DISPATCH_PROC(SProcDPMSGetVersion);
57static DISPATCH_PROC(ProcDPMSGetTimeouts);
58static DISPATCH_PROC(SProcDPMSGetTimeouts);
59static DISPATCH_PROC(ProcDPMSSetTimeouts);
60static DISPATCH_PROC(SProcDPMSSetTimeouts);
61static DISPATCH_PROC(ProcDPMSEnable);
62static DISPATCH_PROC(SProcDPMSEnable);
63static DISPATCH_PROC(ProcDPMSDisable);
64static DISPATCH_PROC(SProcDPMSDisable);
65static DISPATCH_PROC(ProcDPMSForceLevel);
66static DISPATCH_PROC(SProcDPMSForceLevel);
67static DISPATCH_PROC(ProcDPMSInfo);
68static DISPATCH_PROC(SProcDPMSInfo);
69static DISPATCH_PROC(ProcDPMSCapable);
70static DISPATCH_PROC(SProcDPMSCapable);
71
72void
73DPMSExtensionInit(INITARGS)
74{
75    AddExtension(DPMSExtensionName, 0, 0,
76		 ProcDPMSDispatch, SProcDPMSDispatch,
77		 NULL, StandardMinorOpcode);
78}
79
80static int
81ProcDPMSGetVersion(client)
82    ClientPtr client;
83{
84    /* REQUEST(xDPMSGetVersionReq); */
85    xDPMSGetVersionReply rep;
86    int n;
87
88    REQUEST_SIZE_MATCH(xDPMSGetVersionReq);
89
90    rep.type = X_Reply;
91    rep.length = 0;
92    rep.sequenceNumber = client->sequence;
93    rep.majorVersion = DPMSMajorVersion;
94    rep.minorVersion = DPMSMinorVersion;
95    if (client->swapped) {
96    	swaps(&rep.sequenceNumber, n);
97	swaps(&rep.majorVersion, n);
98	swaps(&rep.minorVersion, n);
99    }
100    WriteToClient(client, sizeof(xDPMSGetVersionReply), (char *)&rep);
101    return(client->noClientException);
102}
103
104static int
105ProcDPMSCapable(ClientPtr client)
106{
107    /* REQUEST(xDPMSCapableReq); */
108    xDPMSCapableReply rep;
109    int n;
110
111    REQUEST_SIZE_MATCH(xDPMSCapableReq);
112
113    rep.type = X_Reply;
114    rep.length = 0;
115    rep.sequenceNumber = client->sequence;
116    rep.capable = DPMSCapableFlag;
117
118    if (client->swapped) {
119	swaps(&rep.sequenceNumber, n);
120    }
121    WriteToClient(client, sizeof(xDPMSCapableReply), (char *)&rep);
122    return(client->noClientException);
123}
124
125static int
126ProcDPMSGetTimeouts(client)
127    ClientPtr client;
128{
129    /* REQUEST(xDPMSGetTimeoutsReq); */
130    xDPMSGetTimeoutsReply rep;
131    int n;
132
133    REQUEST_SIZE_MATCH(xDPMSGetTimeoutsReq);
134
135    rep.type = X_Reply;
136    rep.length = 0;
137    rep.sequenceNumber = client->sequence;
138    rep.standby = DPMSStandbyTime / MILLI_PER_SECOND;
139    rep.suspend = DPMSSuspendTime / MILLI_PER_SECOND;
140    rep.off = DPMSOffTime / MILLI_PER_SECOND;
141
142    if (client->swapped) {
143    	swaps(&rep.sequenceNumber, n);
144	swaps(&rep.standby, n);
145	swaps(&rep.suspend, n);
146	swaps(&rep.off, n);
147    }
148    WriteToClient(client, sizeof(xDPMSGetTimeoutsReply), (char *)&rep);
149    return(client->noClientException);
150}
151
152static int
153ProcDPMSSetTimeouts(client)
154    ClientPtr client;
155{
156    REQUEST(xDPMSSetTimeoutsReq);
157
158    REQUEST_SIZE_MATCH(xDPMSSetTimeoutsReq);
159
160    if ((stuff->off != 0)&&(stuff->off < stuff->suspend))
161    {
162	client->errorValue = stuff->off;
163	return BadValue;
164    }
165    if ((stuff->suspend != 0)&&(stuff->suspend < stuff->standby))
166    {
167	client->errorValue = stuff->suspend;
168	return BadValue;
169    }
170
171    DPMSStandbyTime = stuff->standby * MILLI_PER_SECOND;
172    DPMSSuspendTime = stuff->suspend * MILLI_PER_SECOND;
173    DPMSOffTime = stuff->off * MILLI_PER_SECOND;
174    SetScreenSaverTimer();
175
176    return(client->noClientException);
177}
178
179static int
180ProcDPMSEnable(client)
181    ClientPtr client;
182{
183    Bool was_enabled = DPMSEnabled;
184
185    REQUEST_SIZE_MATCH(xDPMSEnableReq);
186
187    if (DPMSCapableFlag) {
188	DPMSEnabled = TRUE;
189	if (!was_enabled)
190	    SetScreenSaverTimer();
191    }
192
193    return(client->noClientException);
194}
195
196static int
197ProcDPMSDisable(client)
198    ClientPtr client;
199{
200    /* REQUEST(xDPMSDisableReq); */
201
202    REQUEST_SIZE_MATCH(xDPMSDisableReq);
203
204    DPMSSet(client, DPMSModeOn);
205
206    DPMSEnabled = FALSE;
207
208    return(client->noClientException);
209}
210
211static int
212ProcDPMSForceLevel(client)
213    ClientPtr client;
214{
215    REQUEST(xDPMSForceLevelReq);
216
217    REQUEST_SIZE_MATCH(xDPMSForceLevelReq);
218
219    if (!DPMSEnabled)
220	return BadMatch;
221
222    if (stuff->level != DPMSModeOn &&
223        stuff->level != DPMSModeStandby &&
224        stuff->level != DPMSModeSuspend &&
225        stuff->level != DPMSModeOff) {
226	client->errorValue = stuff->level;
227	return BadValue;
228    }
229
230    DPMSSet(client, stuff->level);
231
232    return(client->noClientException);
233}
234
235static int
236ProcDPMSInfo(ClientPtr client)
237{
238    /* REQUEST(xDPMSInfoReq); */
239    xDPMSInfoReply rep;
240    int n;
241
242    REQUEST_SIZE_MATCH(xDPMSInfoReq);
243
244    rep.type = X_Reply;
245    rep.length = 0;
246    rep.sequenceNumber = client->sequence;
247    rep.power_level = DPMSPowerLevel;
248    rep.state = DPMSEnabled;
249
250    if (client->swapped) {
251    	swaps(&rep.sequenceNumber, n);
252	swaps(&rep.power_level, n);
253    }
254    WriteToClient(client, sizeof(xDPMSInfoReply), (char *)&rep);
255    return(client->noClientException);
256}
257
258static int
259ProcDPMSDispatch (client)
260    ClientPtr	client;
261{
262    REQUEST(xReq);
263
264    switch (stuff->data)
265    {
266    case X_DPMSGetVersion:
267	return ProcDPMSGetVersion(client);
268    case X_DPMSCapable:
269	return ProcDPMSCapable(client);
270    case X_DPMSGetTimeouts:
271	return ProcDPMSGetTimeouts(client);
272    case X_DPMSSetTimeouts:
273	return ProcDPMSSetTimeouts(client);
274    case X_DPMSEnable:
275	return ProcDPMSEnable(client);
276    case X_DPMSDisable:
277	return ProcDPMSDisable(client);
278    case X_DPMSForceLevel:
279	return ProcDPMSForceLevel(client);
280    case X_DPMSInfo:
281	return ProcDPMSInfo(client);
282    default:
283	return BadRequest;
284    }
285}
286
287static int
288SProcDPMSGetVersion(client)
289    ClientPtr	client;
290{
291    int n;
292    REQUEST(xDPMSGetVersionReq);
293
294    swaps(&stuff->length, n);
295    REQUEST_SIZE_MATCH(xDPMSGetVersionReq);
296    swaps(&stuff->majorVersion, n);
297    swaps(&stuff->minorVersion, n);
298    return ProcDPMSGetVersion(client);
299}
300
301static int
302SProcDPMSCapable(ClientPtr client)
303{
304    REQUEST(xDPMSCapableReq);
305    int n;
306
307    swaps(&stuff->length, n);
308    REQUEST_SIZE_MATCH(xDPMSCapableReq);
309
310    return ProcDPMSCapable(client);
311}
312
313static int
314SProcDPMSGetTimeouts(client)
315    ClientPtr client;
316{
317    REQUEST(xDPMSGetTimeoutsReq);
318    int n;
319
320    swaps(&stuff->length, n);
321    REQUEST_SIZE_MATCH(xDPMSGetTimeoutsReq);
322
323    return ProcDPMSGetTimeouts(client);
324}
325
326static int
327SProcDPMSSetTimeouts(client)
328    ClientPtr client;
329{
330    REQUEST(xDPMSSetTimeoutsReq);
331    int n;
332
333    swaps(&stuff->length, n);
334    REQUEST_SIZE_MATCH(xDPMSSetTimeoutsReq);
335
336    swaps(&stuff->standby, n);
337    swaps(&stuff->suspend, n);
338    swaps(&stuff->off, n);
339    return ProcDPMSSetTimeouts(client);
340}
341
342static int
343SProcDPMSEnable(client)
344    ClientPtr client;
345{
346    REQUEST(xDPMSEnableReq);
347    int n;
348
349    swaps(&stuff->length, n);
350    REQUEST_SIZE_MATCH(xDPMSEnableReq);
351
352    return ProcDPMSEnable(client);
353}
354
355static int
356SProcDPMSDisable(client)
357    ClientPtr client;
358{
359    REQUEST(xDPMSDisableReq);
360    int n;
361
362    swaps(&stuff->length, n);
363    REQUEST_SIZE_MATCH(xDPMSDisableReq);
364
365    return ProcDPMSDisable(client);
366}
367
368static int
369SProcDPMSForceLevel(client)
370    ClientPtr client;
371{
372    REQUEST(xDPMSForceLevelReq);
373    int n;
374
375    swaps(&stuff->length, n);
376    REQUEST_SIZE_MATCH(xDPMSForceLevelReq);
377
378    swaps(&stuff->level, n);
379
380    return ProcDPMSForceLevel(client);
381}
382
383static int
384SProcDPMSInfo(client)
385    ClientPtr client;
386{
387    REQUEST(xDPMSInfoReq);
388    int n;
389
390    swaps(&stuff->length, n);
391    REQUEST_SIZE_MATCH(xDPMSInfoReq);
392
393    return ProcDPMSInfo(client);
394}
395
396static int
397SProcDPMSDispatch (client)
398    ClientPtr	client;
399{
400    REQUEST(xReq);
401    switch (stuff->data)
402    {
403    case X_DPMSGetVersion:
404	return SProcDPMSGetVersion(client);
405    case X_DPMSCapable:
406	return SProcDPMSCapable(client);
407    case X_DPMSGetTimeouts:
408	return SProcDPMSGetTimeouts(client);
409    case X_DPMSSetTimeouts:
410	return SProcDPMSSetTimeouts(client);
411    case X_DPMSEnable:
412	return SProcDPMSEnable(client);
413    case X_DPMSDisable:
414	return SProcDPMSDisable(client);
415    case X_DPMSForceLevel:
416	return SProcDPMSForceLevel(client);
417    case X_DPMSInfo:
418	return SProcDPMSInfo(client);
419    default:
420	return BadRequest;
421    }
422}
423