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