]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/sys/linux/libXNVCtrl/NVCtrl.c
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / sys / linux / libXNVCtrl / NVCtrl.c
1 #define NEED_EVENTS
2 #define NEED_REPLIES
3 #include <X11/Xlibint.h>
4 #include <X11/Xutil.h>
5 #include <X11/extensions/Xext.h>
6 #include "extutil.h"
7 #include "NVCtrlLib.h"
8 #include "nv_control.h"
9
10 #if !defined(XTRHEADS)
11 #warning XTRHEADS not defined -- this libXNVCtrl.a will not be thread safe!
12 #endif
13
14 static XExtensionInfo _nvctrl_ext_info_data;
15 static XExtensionInfo *nvctrl_ext_info = &_nvctrl_ext_info_data;
16 static /* const */ char *nvctrl_extension_name = NV_CONTROL_NAME;
17
18 #define XNVCTRLCheckExtension(dpy,i,val) \
19   XextCheckExtension (dpy, i, nvctrl_extension_name, val)
20 #define XNVCTRLSimpleCheckExtension(dpy,i) \
21   XextSimpleCheckExtension (dpy, i, nvctrl_extension_name)
22
23 static int close_display();
24 static Bool wire_to_event();
25 static /* const */ XExtensionHooks nvctrl_extension_hooks = {
26     NULL,                               /* create_gc */
27     NULL,                               /* copy_gc */
28     NULL,                               /* flush_gc */
29     NULL,                               /* free_gc */
30     NULL,                               /* create_font */
31     NULL,                               /* free_font */
32     close_display,                      /* close_display */
33     wire_to_event,                      /* wire_to_event */
34     NULL,                               /* event_to_wire */
35     NULL,                               /* error */
36     NULL,                               /* error_string */
37 };
38
39 static XEXT_GENERATE_FIND_DISPLAY (find_display, nvctrl_ext_info,
40                                    nvctrl_extension_name, 
41                                    &nvctrl_extension_hooks,
42                                    NV_CONTROL_EVENTS, NULL)
43
44 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, nvctrl_ext_info)
45
46 Bool XNVCTRLQueryExtension (
47     Display *dpy,
48     int *event_basep,
49     int *error_basep
50 ){
51     XExtDisplayInfo *info = find_display (dpy);
52
53     if (XextHasExtension(info)) {
54         if (event_basep) *event_basep = info->codes->first_event;
55         if (error_basep) *error_basep = info->codes->first_error;
56         return True;
57     } else {
58         return False;
59     }
60 }
61
62
63 Bool XNVCTRLQueryVersion (
64     Display *dpy,
65     int *major,
66     int *minor
67 ){
68     XExtDisplayInfo *info = find_display (dpy);
69     xnvCtrlQueryExtensionReply rep;
70     xnvCtrlQueryExtensionReq   *req;
71
72     if(!XextHasExtension(info))
73         return False;
74
75     XNVCTRLCheckExtension (dpy, info, False);
76
77     LockDisplay (dpy);
78     GetReq (nvCtrlQueryExtension, req);
79     req->reqType = info->codes->major_opcode;
80     req->nvReqType = X_nvCtrlQueryExtension;
81     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
82         UnlockDisplay (dpy);
83         SyncHandle ();
84         return False;
85     }
86     if (major) *major = rep.major;
87     if (minor) *minor = rep.minor;
88     UnlockDisplay (dpy);
89     SyncHandle ();
90     return True;
91 }
92
93
94 Bool XNVCTRLIsNvScreen (
95     Display *dpy,
96     int screen
97 ){
98     XExtDisplayInfo *info = find_display (dpy);
99     xnvCtrlIsNvReply rep;
100     xnvCtrlIsNvReq   *req;
101     Bool isnv;
102
103     if(!XextHasExtension(info))
104         return False;
105
106     XNVCTRLCheckExtension (dpy, info, False);
107
108     LockDisplay (dpy);
109     GetReq (nvCtrlIsNv, req);
110     req->reqType = info->codes->major_opcode;
111     req->nvReqType = X_nvCtrlIsNv;
112     req->screen = screen;
113     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
114         UnlockDisplay (dpy);
115         SyncHandle ();
116         return False;
117     }
118     isnv = rep.isnv;
119     UnlockDisplay (dpy);
120     SyncHandle ();
121     return isnv;
122 }
123
124
125 void XNVCTRLSetAttribute (
126     Display *dpy,
127     int screen,
128     unsigned int display_mask,
129     unsigned int attribute,
130     int value
131 ){
132     XExtDisplayInfo *info = find_display (dpy);
133     xnvCtrlSetAttributeReq *req;
134
135     XNVCTRLSimpleCheckExtension (dpy, info);
136
137     LockDisplay (dpy);
138     GetReq (nvCtrlSetAttribute, req);
139     req->reqType = info->codes->major_opcode;
140     req->nvReqType = X_nvCtrlSetAttribute;
141     req->screen = screen;
142     req->display_mask = display_mask;
143     req->attribute = attribute;
144     req->value = value;
145     UnlockDisplay (dpy);
146     SyncHandle ();
147 }
148
149
150 Bool XNVCTRLQueryAttribute (
151     Display *dpy,
152     int screen,
153     unsigned int display_mask,
154     unsigned int attribute,
155     int *value
156 ){
157     XExtDisplayInfo *info = find_display (dpy);
158     xnvCtrlQueryAttributeReply rep;
159     xnvCtrlQueryAttributeReq   *req;
160     Bool exists;
161
162     if(!XextHasExtension(info))
163         return False;
164
165     XNVCTRLCheckExtension (dpy, info, False);
166
167     LockDisplay (dpy);
168     GetReq (nvCtrlQueryAttribute, req);
169     req->reqType = info->codes->major_opcode;
170     req->nvReqType = X_nvCtrlQueryAttribute;
171     req->screen = screen;
172     req->display_mask = display_mask;
173     req->attribute = attribute;
174     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
175         UnlockDisplay (dpy);
176         SyncHandle ();
177         return False;
178     }
179     if (value) *value = rep.value;
180     exists = rep.flags;
181     UnlockDisplay (dpy);
182     SyncHandle ();
183     return exists;
184 }
185
186
187 Bool XNVCTRLQueryStringAttribute (
188     Display *dpy,
189     int screen,
190     unsigned int display_mask,
191     unsigned int attribute,
192     char **ptr
193 ){
194     XExtDisplayInfo *info = find_display (dpy);
195     xnvCtrlQueryStringAttributeReply rep;
196     xnvCtrlQueryStringAttributeReq   *req;
197     Bool exists;
198     int length, numbytes, slop;
199
200     if (!ptr) return False;
201
202     if(!XextHasExtension(info))
203         return False;
204
205     XNVCTRLCheckExtension (dpy, info, False);
206
207     LockDisplay (dpy);
208     GetReq (nvCtrlQueryStringAttribute, req);
209     req->reqType = info->codes->major_opcode;
210     req->nvReqType = X_nvCtrlQueryStringAttribute;
211     req->screen = screen;
212     req->display_mask = display_mask;
213     req->attribute = attribute;
214     if (!_XReply (dpy, (xReply *) &rep, 0, False)) {
215         UnlockDisplay (dpy);
216         SyncHandle ();
217         return False;
218     }
219     length = rep.length;
220     numbytes = rep.n;
221     slop = numbytes & 3;
222     *ptr = (char *) Xmalloc(numbytes);
223     if (! *ptr) {
224         _XEatData(dpy, length);
225         UnlockDisplay (dpy);
226         SyncHandle ();
227         return False;
228     } else {
229         _XRead(dpy, (char *) *ptr, numbytes);
230         if (slop) _XEatData(dpy, 4-slop);
231     }
232     exists = rep.flags;
233     UnlockDisplay (dpy);
234     SyncHandle ();
235     return exists;
236 }
237
238 Bool XNVCTRLQueryValidAttributeValues (
239     Display *dpy,
240     int screen,
241     unsigned int display_mask,
242     unsigned int attribute,                                 
243     NVCTRLAttributeValidValuesRec *values
244 ){
245     XExtDisplayInfo *info = find_display (dpy);
246     xnvCtrlQueryValidAttributeValuesReply rep;
247     xnvCtrlQueryValidAttributeValuesReq   *req;
248     Bool exists;
249     
250     if (!values) return False;
251
252     if(!XextHasExtension(info))
253         return False;
254
255     XNVCTRLCheckExtension (dpy, info, False);
256
257     LockDisplay (dpy);
258     GetReq (nvCtrlQueryValidAttributeValues, req);
259     req->reqType = info->codes->major_opcode;
260     req->nvReqType = X_nvCtrlQueryValidAttributeValues;
261     req->screen = screen;
262     req->display_mask = display_mask;
263     req->attribute = attribute;
264     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
265         UnlockDisplay (dpy);
266         SyncHandle ();
267         return False;
268     }
269     exists = rep.flags;
270     values->type = rep.attr_type;
271     if (rep.attr_type == ATTRIBUTE_TYPE_RANGE) {
272         values->u.range.min = rep.min;
273         values->u.range.max = rep.max;
274     }
275     if (rep.attr_type == ATTRIBUTE_TYPE_INT_BITS) {
276         values->u.bits.ints = rep.bits;
277     }
278     values->permissions = rep.perms;
279     UnlockDisplay (dpy);
280     SyncHandle ();
281     return exists;
282 }
283
284 Bool XNVCtrlSelectNotify (
285     Display *dpy,
286     int screen,
287     int type,
288     Bool onoff
289 ){
290     XExtDisplayInfo *info = find_display (dpy);
291     xnvCtrlSelectNotifyReq *req;
292
293     if(!XextHasExtension (info))
294         return False;
295
296     XNVCTRLCheckExtension (dpy, info, False);
297
298     LockDisplay (dpy);
299     GetReq (nvCtrlSelectNotify, req);
300     req->reqType = info->codes->major_opcode;
301     req->nvReqType = X_nvCtrlSelectNotify;
302     req->screen = screen;
303     req->notifyType = type;
304     req->onoff = onoff;
305     UnlockDisplay (dpy);
306     SyncHandle ();
307
308     return True;
309 }
310
311 static Bool wire_to_event (Display *dpy, XEvent *host, xEvent *wire)
312 {
313     XExtDisplayInfo *info = find_display (dpy);
314     XNVCtrlEvent *re = (XNVCtrlEvent *) host;
315     xnvctrlEvent *event = (xnvctrlEvent *) wire;
316
317     XNVCTRLCheckExtension (dpy, info, False);
318     
319     switch ((event->u.u.type & 0x7F) - info->codes->first_event) {
320     case ATTRIBUTE_CHANGED_EVENT:
321         re->attribute_changed.type = event->u.u.type & 0x7F;
322         re->attribute_changed.serial = 
323             _XSetLastRequestRead(dpy, (xGenericReply*) event);
324         re->attribute_changed.send_event = ((event->u.u.type & 0x80) != 0);
325         re->attribute_changed.display = dpy;
326         re->attribute_changed.time = event->u.attribute_changed.time;
327         re->attribute_changed.screen = event->u.attribute_changed.screen;
328         re->attribute_changed.display_mask =
329             event->u.attribute_changed.display_mask;
330         re->attribute_changed.attribute = event->u.attribute_changed.attribute;
331         re->attribute_changed.value = event->u.attribute_changed.value;
332         break;
333     default:
334         return False;
335     }
336     
337     return True;
338 }
339