1/************************************************************
2
3Copyright 2008 Peter Hutterer
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of the author shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from the author.
24
25*/
26
27/***********************************************************************
28 * XGetDeviceProperties - get an input device's properties.
29 */
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
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>
40#include "XIint.h"
41#include <limits.h>
42
43int
44XGetDeviceProperty(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,
48                         unsigned char **prop)
49{
50    xGetDevicePropertyReq   *req;
51    xGetDevicePropertyReply rep;
52    unsigned long           nbytes, rbytes;
53    int                     ret = Success;
54
55    XExtDisplayInfo *info = XInput_find_display(dpy);
56
57    LockDisplay(dpy);
58    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
59	return 1;
60
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;
66    req->type       = req_type;
67    req->longOffset = offset;
68    req->longLength = length;
69    req->delete     = delete;
70
71    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
72    {
73	UnlockDisplay (dpy);
74	SyncHandle ();
75	return 1;
76    }
77
78    *prop = (unsigned char *) NULL;
79
80    if (rep.propertyType != None) {
81	/*
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.
86	 *
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.
90	 */
91	switch (rep.format) {
92	case 8:
93	    if (rep.nItems < INT_MAX) {
94		nbytes = rep.nItems;
95		rbytes = rep.nItems + 1;
96		if ((*prop = Xmalloc (rbytes)))
97		    _XReadPad (dpy, (char *) *prop, nbytes);
98		else
99		    ret = BadAlloc;
100	    }
101	    break;
102
103	case 16:
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);
109		else
110		    ret = BadAlloc;
111	    }
112	    break;
113
114	case 32:
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);
120		else
121		    ret = BadAlloc;
122	    }
123	    break;
124
125	default:
126	    /*
127	     * This part of the code should never be reached.  If it is,
128	     * the server sent back a property with an invalid format.
129	     */
130	    ret = BadImplementation;
131	}
132	if (! *prop) {
133	    _XEatDataWords(dpy, rep.length);
134	    if (ret == Success)
135		ret = BadAlloc;
136	    goto out;
137	}
138	(*prop)[rbytes - 1] = '\0';
139    }
140
141    *actual_type = rep.propertyType;
142    *actual_format = rep.format;
143    *nitems = rep.nItems;
144    *bytes_after = rep.bytesAfter;
145  out:
146    UnlockDisplay (dpy);
147    SyncHandle ();
148
149    return ret;
150}
151
152