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