DPMS.c revision caade7cc
1/* $Xorg: DPMS.c,v 1.3 2000/08/17 19:45:50 cpqbld Exp $ */
2/*****************************************************************
3
4Copyright (c) 1996 Digital Equipment Corporation, Maynard, Massachusetts.
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
19BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
21IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of Digital Equipment Corporation
24shall not be used in advertising or otherwise to promote the sale, use or other
25dealings in this Software without prior written authorization from Digital
26Equipment Corporation.
27
28******************************************************************/
29/* $XFree86: xc/lib/Xext/DPMS.c,v 3.5 2002/10/16 00:37:27 dawes Exp $ */
30
31/*
32 * HISTORY
33 */
34
35#define NEED_REPLIES
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39#include <X11/Xlibint.h>
40#include <X11/extensions/dpms.h>
41#include <X11/extensions/dpmsstr.h>
42#include <X11/extensions/Xext.h>
43#include <X11/extensions/extutil.h>
44#include <stdio.h>
45
46static XExtensionInfo _dpms_info_data;
47static XExtensionInfo *dpms_info = &_dpms_info_data;
48static char *dpms_extension_name = DPMSExtensionName;
49
50#define DPMSCheckExtension(dpy,i,val) \
51  XextCheckExtension (dpy, i, dpms_extension_name, val)
52
53/*****************************************************************************
54 *                                                                           *
55 *                         private utility routines                          *
56 *                                                                           *
57 *****************************************************************************/
58
59static int close_display(Display *dpy, XExtCodes *codes);
60static /* const */ XExtensionHooks dpms_extension_hooks = {
61    NULL,                               /* create_gc */
62    NULL,                               /* copy_gc */
63    NULL,                               /* flush_gc */
64    NULL,                               /* free_gc */
65    NULL,                               /* create_font */
66    NULL,                               /* free_font */
67    close_display,                      /* close_display */
68    NULL,                               /* wire_to_event */
69    NULL,                               /* event_to_wire */
70    NULL,                               /* error */
71    NULL                                /* error_string */
72};
73
74static XEXT_GENERATE_FIND_DISPLAY (find_display, dpms_info,
75				   dpms_extension_name,
76                                   &dpms_extension_hooks, DPMSNumberEvents,
77                                   NULL)
78
79static XEXT_GENERATE_CLOSE_DISPLAY (close_display, dpms_info)
80
81/*****************************************************************************
82 *                                                                           *
83 *                  public routines                                          *
84 *                                                                           *
85 *****************************************************************************/
86
87Bool
88DPMSQueryExtension (Display *dpy, int *event_basep, int *error_basep)
89{
90    XExtDisplayInfo *info = find_display (dpy);
91
92    if (XextHasExtension(info)) {
93	*event_basep = info->codes->first_event;
94	*error_basep = info->codes->first_error;
95	return True;
96    } else {
97	return False;
98    }
99}
100
101Status
102DPMSGetVersion(Display *dpy, int *major_versionp, int *minor_versionp)
103{
104    XExtDisplayInfo *info = find_display (dpy);
105    xDPMSGetVersionReply	    rep;
106    register xDPMSGetVersionReq  *req;
107
108    DPMSCheckExtension (dpy, info, 0);
109
110    LockDisplay (dpy);
111    GetReq (DPMSGetVersion, req);
112    req->reqType = info->codes->major_opcode;
113    req->dpmsReqType = X_DPMSGetVersion;
114    if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
115	UnlockDisplay (dpy);
116	SyncHandle ();
117	return 0;
118    }
119    *major_versionp = rep.majorVersion;
120    *minor_versionp = rep.minorVersion;
121    UnlockDisplay (dpy);
122    SyncHandle ();
123    return 1;
124}
125
126Bool
127DPMSCapable(Display *dpy)
128{
129    XExtDisplayInfo *info = find_display (dpy);
130    register xDPMSCapableReq *req;
131    xDPMSCapableReply rep;
132
133    DPMSCheckExtension (dpy, info, 0);
134
135    LockDisplay(dpy);
136    GetReq(DPMSCapable, req);
137    req->reqType = info->codes->major_opcode;
138    req->dpmsReqType = X_DPMSCapable;
139
140    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
141	UnlockDisplay(dpy);
142	SyncHandle();
143	return False;
144    }
145    UnlockDisplay(dpy);
146    SyncHandle();
147    return rep.capable;
148}
149
150Status
151DPMSSetTimeouts(Display *dpy, CARD16 standby, CARD16 suspend, CARD16 off)
152{
153    XExtDisplayInfo *info = find_display (dpy);
154    register xDPMSSetTimeoutsReq *req;
155
156    if ((off != 0)&&(off < suspend))
157    {
158	return BadValue;
159    }
160    if ((suspend != 0)&&(suspend < standby))
161    {
162	return BadValue;
163    }
164
165    DPMSCheckExtension (dpy, info, 0);
166    LockDisplay(dpy);
167    GetReq(DPMSSetTimeouts, req);
168    req->reqType = info->codes->major_opcode;
169    req->dpmsReqType = X_DPMSSetTimeouts;
170    req->standby = standby;
171    req->suspend = suspend;
172    req->off = off;
173
174    UnlockDisplay(dpy);
175    SyncHandle();
176    return 1;
177}
178
179Bool
180DPMSGetTimeouts(Display *dpy, CARD16 *standby, CARD16 *suspend, CARD16 *off)
181{
182    XExtDisplayInfo *info = find_display (dpy);
183    register xDPMSGetTimeoutsReq *req;
184    xDPMSGetTimeoutsReply rep;
185
186    DPMSCheckExtension (dpy, info, 0);
187
188    LockDisplay(dpy);
189    GetReq(DPMSGetTimeouts, req);
190    req->reqType = info->codes->major_opcode;
191    req->dpmsReqType = X_DPMSGetTimeouts;
192
193    if (!_XReply(dpy, (xReply *)&rep, 0, xTrue)) {
194	UnlockDisplay(dpy);
195	SyncHandle();
196	return False;
197    }
198    UnlockDisplay(dpy);
199    SyncHandle();
200    *standby = rep.standby;
201    *suspend = rep.suspend;
202    *off = rep.off;
203    return 1;
204}
205
206Status
207DPMSEnable(Display *dpy)
208{
209    XExtDisplayInfo *info = find_display (dpy);
210    register xDPMSEnableReq *req;
211
212    DPMSCheckExtension (dpy, info, 0);
213    LockDisplay(dpy);
214    GetReq(DPMSEnable, req);
215    req->reqType = info->codes->major_opcode;
216    req->dpmsReqType = X_DPMSEnable;
217
218    UnlockDisplay(dpy);
219    SyncHandle();
220    return 1;
221}
222
223Status
224DPMSDisable(Display *dpy)
225{
226    XExtDisplayInfo *info = find_display (dpy);
227    register xDPMSDisableReq *req;
228
229    DPMSCheckExtension (dpy, info, 0);
230    LockDisplay(dpy);
231    GetReq(DPMSDisable, req);
232    req->reqType = info->codes->major_opcode;
233    req->dpmsReqType = X_DPMSDisable;
234
235    UnlockDisplay(dpy);
236    SyncHandle();
237    return 1;
238}
239
240
241Status
242DPMSForceLevel(Display *dpy, CARD16 level)
243{
244    XExtDisplayInfo *info = find_display (dpy);
245    register xDPMSForceLevelReq *req;
246
247    DPMSCheckExtension (dpy, info, 0);
248
249    if ((level != DPMSModeOn) &&
250	(level != DPMSModeStandby) &&
251	(level != DPMSModeSuspend) &&
252	(level != DPMSModeOff))
253	return BadValue;
254
255    LockDisplay(dpy);
256    GetReq(DPMSForceLevel, req);
257    req->reqType = info->codes->major_opcode;
258    req->dpmsReqType = X_DPMSForceLevel;
259    req->level = level;
260
261    UnlockDisplay(dpy);
262    SyncHandle();
263    return 1;
264}
265
266Status
267DPMSInfo(Display *dpy, CARD16 *power_level, BOOL *state)
268{
269    XExtDisplayInfo *info = find_display (dpy);
270    register xDPMSInfoReq *req;
271    xDPMSInfoReply rep;
272
273    DPMSCheckExtension (dpy, info, 0);
274
275    LockDisplay(dpy);
276    GetReq(DPMSInfo, req);
277    req->reqType = info->codes->major_opcode;
278    req->dpmsReqType = X_DPMSInfo;
279
280    if (!_XReply(dpy, (xReply *)&rep, 0, xTrue)) {
281	UnlockDisplay(dpy);
282	SyncHandle();
283	return False;
284    }
285    UnlockDisplay(dpy);
286    SyncHandle();
287    *power_level = rep.power_level;
288    *state = rep.state;
289    return 1;
290}
291
292
293
294