1 /************************************************************
3 Copyright 2008 Peter Hutterer
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of the author shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from the author.
27 /***********************************************************************
28 * XGetDeviceProperties - get an input device's properties.
35 #include <X11/Xlibint.h>
36 #include <X11/extensions/XI.h>
37 #include <X11/extensions/XIproto.h>
38 #include <X11/extensions/XInput.h>
39 #include <X11/extensions/extutil.h>
44 XGetDeviceProperty(Display* dpy, XDevice* dev,
45 Atom property, long offset, long length, Bool delete,
46 Atom req_type, Atom *actual_type, int *actual_format,
47 unsigned long *nitems, unsigned long *bytes_after,
50 xGetDevicePropertyReq *req;
51 xGetDevicePropertyReply rep;
52 unsigned long nbytes, rbytes;
55 XExtDisplayInfo *info = XInput_find_display(dpy);
58 if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
61 GetReq(GetDeviceProperty, req);
62 req->reqType = info->codes->major_opcode;
63 req->ReqType = X_GetDeviceProperty;
64 req->deviceid = dev->device_id;
65 req->property = property;
67 req->longOffset = offset;
68 req->longLength = length;
71 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
78 *prop = (unsigned char *) NULL;
80 if (rep.propertyType != None) {
82 * One extra byte is malloced than is needed to contain the property
83 * data, but this last byte is null terminated and convenient for
84 * returning string properties, so the client doesn't then have to
85 * recopy the string to make it null terminated.
87 * Maximum item limits are set to both prevent integer overflow when
88 * calculating the amount of memory to malloc, and to limit how much
89 * memory will be used if a server provides an insanely high count.
93 if (rep.nItems < INT_MAX) {
95 rbytes = rep.nItems + 1;
96 if ((*prop = Xmalloc (rbytes)))
97 _XReadPad (dpy, (char *) *prop, nbytes);
104 if (rep.nItems < (INT_MAX / sizeof (short))) {
105 nbytes = rep.nItems << 1;
106 rbytes = rep.nItems * sizeof (short) + 1;
107 if ((*prop = Xmalloc (rbytes)))
108 _XRead16Pad (dpy, (short *) *prop, nbytes);
115 if (rep.nItems < (INT_MAX / sizeof (long))) {
116 nbytes = rep.nItems << 2;
117 rbytes = rep.nItems * sizeof (long) + 1;
118 if ((*prop = Xmalloc (rbytes)))
119 _XRead32 (dpy, (long *) *prop, nbytes);
127 * This part of the code should never be reached. If it is,
128 * the server sent back a property with an invalid format.
130 ret = BadImplementation;
133 _XEatDataWords(dpy, rep.length);
138 (*prop)[rbytes - 1] = '\0';
141 *actual_type = rep.propertyType;
142 *actual_format = rep.format;
143 *nitems = rep.nItems;
144 *bytes_after = rep.bytesAfter;