XF86Misc.c revision 11245024
1/* $XFree86: xc/lib/Xxf86misc/XF86Misc.c,v 3.12 2002/11/20 04:04:57 dawes Exp $ */
2
3/*
4 * Copyright (c) 1995, 1996  The XFree86 Project, Inc
5 */
6
7/* THIS IS NOT AN X CONSORTIUM STANDARD */
8
9#define NEED_EVENTS
10#define NEED_REPLIES
11#include <X11/Xlibint.h>
12#include <X11/extensions/xf86mscstr.h>
13#include <X11/extensions/Xext.h>
14#include <X11/extensions/extutil.h>
15
16static XExtensionInfo _xf86misc_info_data;
17static XExtensionInfo *xf86misc_info = &_xf86misc_info_data;
18static char *xf86misc_extension_name = XF86MISCNAME;
19
20#define XF86MiscCheckExtension(dpy,i,val) \
21  XextCheckExtension (dpy, i, xf86misc_extension_name, val)
22
23/*****************************************************************************
24 *                                                                           *
25 *			   private utility routines                          *
26 *                                                                           *
27 *****************************************************************************/
28
29static int close_display(Display *dpy, XExtCodes *codes);
30
31static /* const */ XExtensionHooks xf86misc_extension_hooks = {
32    NULL,				/* create_gc */
33    NULL,				/* copy_gc */
34    NULL,				/* flush_gc */
35    NULL,				/* free_gc */
36    NULL,				/* create_font */
37    NULL,				/* free_font */
38    close_display,			/* close_display */
39    NULL,				/* wire_to_event */
40    NULL,				/* event_to_wire */
41    NULL,				/* error */
42    NULL,				/* error_string */
43};
44
45static XEXT_GENERATE_FIND_DISPLAY (find_display, xf86misc_info,
46				   xf86misc_extension_name,
47				   &xf86misc_extension_hooks,
48				   0, NULL)
49
50static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xf86misc_info)
51
52
53/*****************************************************************************
54 *                                                                           *
55 *		    public XFree86-Misc Extension routines                *
56 *                                                                           *
57 *****************************************************************************/
58
59Bool XF86MiscQueryExtension (Display *dpy, int *event_basep, int *error_basep)
60{
61    XExtDisplayInfo *info = find_display (dpy);
62
63    if (XextHasExtension(info)) {
64	*event_basep = info->codes->first_event;
65	*error_basep = info->codes->first_error;
66	return True;
67    } else {
68	return False;
69    }
70}
71
72Bool XF86MiscQueryVersion(Display* dpy, int* majorVersion, int* minorVersion)
73{
74    XExtDisplayInfo *info = find_display (dpy);
75    xXF86MiscQueryVersionReply rep;
76    xXF86MiscQueryVersionReq *req;
77
78    XF86MiscCheckExtension (dpy, info, False);
79    LockDisplay(dpy);
80    GetReq(XF86MiscQueryVersion, req);
81    req->reqType = info->codes->major_opcode;
82    req->xf86miscReqType = X_XF86MiscQueryVersion;
83    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
84	UnlockDisplay(dpy);
85	SyncHandle();
86	return False;
87    }
88    *majorVersion = rep.majorVersion;
89    *minorVersion = rep.minorVersion;
90    UnlockDisplay(dpy);
91    SyncHandle();
92    if (*majorVersion > 0 || *minorVersion > 5)
93	XF86MiscSetClientVersion(dpy);
94
95    return True;
96}
97
98Bool
99XF86MiscSetClientVersion(Display *dpy)
100{
101    XExtDisplayInfo *info = find_display(dpy);
102    xXF86MiscSetClientVersionReq *req;
103
104    XF86MiscCheckExtension(dpy, info, False);
105
106    LockDisplay(dpy);
107    GetReq(XF86MiscSetClientVersion, req);
108    req->reqType = info->codes->major_opcode;
109    req->xf86miscReqType = X_XF86MiscSetClientVersion;
110    req->major = XF86MISC_MAJOR_VERSION;
111    req->minor = XF86MISC_MINOR_VERSION;
112    UnlockDisplay(dpy);
113    SyncHandle();
114    return True;
115}
116
117Bool XF86MiscGetMouseSettings(Display* dpy, XF86MiscMouseSettings *mouseinfo)
118{
119    XExtDisplayInfo *info = find_display (dpy);
120    xXF86MiscGetMouseSettingsReply rep;
121    xXF86MiscGetMouseSettingsReq *req;
122
123    XF86MiscCheckExtension (dpy, info, False);
124
125    LockDisplay(dpy);
126    GetReq(XF86MiscGetMouseSettings, req);
127    req->reqType = info->codes->major_opcode;
128    req->xf86miscReqType = X_XF86MiscGetMouseSettings;
129    if (!_XReply(dpy, (xReply *)&rep,
130		(SIZEOF(xXF86MiscGetMouseSettingsReply) - SIZEOF(xReply))>>2,
131		xFalse)) {
132	UnlockDisplay(dpy);
133	SyncHandle();
134	return False;
135    }
136
137    mouseinfo->type = rep.mousetype;
138    mouseinfo->baudrate = rep.baudrate;
139    mouseinfo->samplerate = rep.samplerate;
140    mouseinfo->resolution = rep.resolution;
141    mouseinfo->buttons = rep.buttons;
142    mouseinfo->emulate3buttons = rep.emulate3buttons;
143    mouseinfo->emulate3timeout = rep.emulate3timeout;
144    mouseinfo->chordmiddle = rep.chordmiddle;
145    mouseinfo->flags = rep.flags;
146    if (rep.devnamelen > 0) {
147        if (!(mouseinfo->device = Xcalloc(rep.devnamelen + 1, 1))) {
148            _XEatData(dpy, (rep.devnamelen+3) & ~3);
149            Xfree(mouseinfo->device);
150            return False;
151        }
152        _XReadPad(dpy, mouseinfo->device, rep.devnamelen);
153    } else
154	mouseinfo->device = NULL;
155
156    UnlockDisplay(dpy);
157    SyncHandle();
158    return True;
159}
160
161Bool XF86MiscGetKbdSettings(Display* dpy, XF86MiscKbdSettings *kbdinfo)
162{
163    XExtDisplayInfo *info = find_display (dpy);
164    xXF86MiscGetKbdSettingsReply rep;
165    xXF86MiscGetKbdSettingsReq *req;
166
167    XF86MiscCheckExtension (dpy, info, False);
168
169    LockDisplay(dpy);
170    GetReq(XF86MiscGetKbdSettings, req);
171    req->reqType = info->codes->major_opcode;
172    req->xf86miscReqType = X_XF86MiscGetKbdSettings;
173    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
174	UnlockDisplay(dpy);
175	SyncHandle();
176	return False;
177    }
178
179    kbdinfo->type = rep.kbdtype;
180    kbdinfo->rate = rep.rate;
181    kbdinfo->delay = rep.delay;
182    kbdinfo->servnumlock = rep.servnumlock;
183
184    UnlockDisplay(dpy);
185    SyncHandle();
186    return True;
187}
188
189Bool XF86MiscSetMouseSettings(Display* dpy, XF86MiscMouseSettings *mouseinfo)
190{
191    XExtDisplayInfo *info = find_display (dpy);
192    xXF86MiscSetMouseSettingsReq *req;
193    int majorVersion, minorVersion;
194
195    XF86MiscCheckExtension (dpy, info, False);
196    XF86MiscQueryVersion(dpy, &majorVersion, &minorVersion);
197
198    LockDisplay(dpy);
199    GetReq(XF86MiscSetMouseSettings, req);
200
201    req->reqType = info->codes->major_opcode;
202    req->xf86miscReqType = X_XF86MiscSetMouseSettings;
203    req->mousetype = mouseinfo->type;
204    req->baudrate = mouseinfo->baudrate;
205    req->samplerate = mouseinfo->samplerate;
206    req->resolution = mouseinfo->resolution;
207    req->buttons = mouseinfo->buttons;
208    req->emulate3buttons = mouseinfo->emulate3buttons;
209    req->emulate3timeout = mouseinfo->emulate3timeout;
210    req->chordmiddle = mouseinfo->chordmiddle;
211    req->flags = mouseinfo->flags;
212    if (majorVersion > 0 || minorVersion > 5) {
213	int len;
214	if ((len = strlen(mouseinfo->device))) {
215	req->devnamelen =  len + 1;
216	len = (req->devnamelen + 3) >> 2;
217	SetReqLen(req,len,len);
218	Data(dpy, mouseinfo->device, req->devnamelen);
219	}
220    }
221
222    UnlockDisplay(dpy);
223    SyncHandle();
224    return True;
225}
226
227Bool XF86MiscSetKbdSettings(Display* dpy, XF86MiscKbdSettings *kbdinfo)
228{
229    XExtDisplayInfo *info = find_display (dpy);
230    xXF86MiscSetKbdSettingsReq *req;
231
232    XF86MiscCheckExtension (dpy, info, False);
233
234    LockDisplay(dpy);
235    GetReq(XF86MiscSetKbdSettings, req);
236    req->reqType = info->codes->major_opcode;
237    req->xf86miscReqType = X_XF86MiscSetKbdSettings;
238    req->kbdtype = kbdinfo->type;
239    req->rate = kbdinfo->rate;
240    req->delay = kbdinfo->delay;
241    req->servnumlock = kbdinfo->servnumlock;
242
243    UnlockDisplay(dpy);
244    SyncHandle();
245    return True;
246}
247
248int XF86MiscSetGrabKeysState(Display* dpy, Bool enable)
249{
250    XExtDisplayInfo *info = find_display (dpy);
251    xXF86MiscSetGrabKeysStateReply rep;
252    xXF86MiscSetGrabKeysStateReq *req;
253
254    XF86MiscCheckExtension (dpy, info, False);
255
256    LockDisplay(dpy);
257    GetReq(XF86MiscSetGrabKeysState, req);
258    req->reqType = info->codes->major_opcode;
259    req->xf86miscReqType = X_XF86MiscSetGrabKeysState;
260    req->enable = enable;
261    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
262	UnlockDisplay(dpy);
263	SyncHandle();
264	return 0;
265    }
266
267    UnlockDisplay(dpy);
268    SyncHandle();
269    return rep.status;
270}
271
272Bool XF86MiscGetFilePaths(Display* dpy, XF86MiscFilePaths *filpaths)
273{
274    XExtDisplayInfo *info = find_display (dpy);
275    xXF86MiscGetFilePathsReply rep;
276    xXF86MiscGetFilePathsReq *req;
277
278    XF86MiscCheckExtension (dpy, info, False);
279
280    LockDisplay(dpy);
281    GetReq(XF86MiscGetFilePaths, req);
282    req->reqType = info->codes->major_opcode;
283    req->xf86miscReqType = X_XF86MiscGetFilePaths;
284    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
285	UnlockDisplay(dpy);
286	SyncHandle();
287	return False;
288    }
289
290    if (rep.configlen) {
291        if (!(filpaths->configfile = Xcalloc(rep.configlen + 1, 1))) {
292            _XEatData(dpy, ((rep.configlen+3) & ~3) + ((rep.modulelen+3) & ~3)
293			    + ((rep.loglen+3) & ~3));
294            return False;
295        }
296    }
297
298    if (rep.modulelen) {
299        if (!(filpaths->modulepath = Xcalloc(rep.modulelen + 1, 1))) {
300            _XEatData(dpy, ((rep.configlen+3) & ~3) + ((rep.modulelen+3) & ~3)
301			    + ((rep.loglen+3) & ~3));
302            if (filpaths->configfile)
303		    Xfree(filpaths->configfile);
304            return False;
305        }
306    }
307
308    if (rep.loglen) {
309        if (!(filpaths->logfile = Xcalloc(rep.loglen + 1, 1))) {
310            _XEatData(dpy, ((rep.configlen+3) & ~3) + ((rep.modulelen+3) & ~3)
311			    + ((rep.loglen+3) & ~3));
312            if (filpaths->configfile)
313		    Xfree(filpaths->configfile);
314            if (filpaths->modulepath)
315		    Xfree(filpaths->modulepath);
316            return False;
317        }
318    }
319
320    if (rep.configlen)
321        _XReadPad(dpy, filpaths->configfile, rep.configlen);
322    else
323	filpaths->configfile = "";
324
325    if (rep.modulelen)
326        _XReadPad(dpy, filpaths->modulepath, rep.modulelen);
327    else
328	filpaths->modulepath = "";
329
330    if (rep.loglen)
331        _XReadPad(dpy, filpaths->logfile, rep.loglen);
332    else
333	filpaths->logfile = "";
334
335    UnlockDisplay(dpy);
336    SyncHandle();
337    return True;
338}
339
340Status XF86MiscPassMessage(Display* dpy, int screen,
341			   const char* msgtype, const char* msgval,
342			   char** retmsg)
343{
344    XExtDisplayInfo *info = find_display (dpy);
345    xXF86MiscPassMessageReply rep;
346    xXF86MiscPassMessageReq *req;
347    int len;
348
349    XF86MiscCheckExtension (dpy, info, False);
350
351    LockDisplay(dpy);
352    GetReq(XF86MiscPassMessage, req);
353    req->reqType = info->codes->major_opcode;
354    req->xf86miscReqType = X_XF86MiscPassMessage;
355    req->screen = screen;
356    if ((len = strlen(msgtype))) {
357	req->typelen =  len + 1;
358	len = (req->typelen + 3) >> 2;
359	SetReqLen(req,len,len);
360	Data(dpy, msgtype, req->typelen);
361    }
362    if ((len = strlen(msgval))) {
363	req->vallen =  len + 1;
364	len = (req->vallen + 3) >> 2;
365	SetReqLen(req,len,len);
366	Data(dpy, msgval, req->vallen);
367    }
368    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
369	UnlockDisplay(dpy);
370	SyncHandle();
371	return BadImplementation;
372    }
373
374    if (rep.mesglen) {
375        if (!(*retmsg = Xcalloc(rep.mesglen + 1, 1))) {
376            _XEatData(dpy, ((rep.mesglen+3) & ~3));
377            return BadAlloc;
378        }
379        _XReadPad(dpy, *retmsg, rep.mesglen);
380    }
381
382    UnlockDisplay(dpy);
383    SyncHandle();
384    return rep.status;
385}
386
387