XGetDProp.c revision 21e67964
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
42int
43XGetDeviceProperty(Display* dpy, XDevice* dev,
44			 Atom property, long offset, long length, Bool delete,
45                         Atom req_type, Atom *actual_type, int *actual_format,
46                         unsigned long *nitems, unsigned long *bytes_after,
47                         unsigned char **prop)
48{
49    xGetDevicePropertyReq   *req;
50    xGetDevicePropertyReply rep;
51    long                    nbytes, rbytes;
52
53    XExtDisplayInfo *info = XInput_find_display(dpy);
54
55    LockDisplay(dpy);
56    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
57	return 1;
58
59    GetReq(GetDeviceProperty, req);
60    req->reqType    = info->codes->major_opcode;
61    req->ReqType    = X_GetDeviceProperty;
62    req->deviceid   = dev->device_id;
63    req->property   = property;
64    req->type       = req_type;
65    req->longOffset = offset;
66    req->longLength = length;
67    req->delete     = delete;
68
69    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
70    {
71	UnlockDisplay (dpy);
72	SyncHandle ();
73	return 1;
74    }
75
76    *prop = (unsigned char *) NULL;
77
78    if (rep.propertyType != None) {
79	/*
80	 * One extra byte is malloced than is needed to contain the property
81	 * data, but this last byte is null terminated and convenient for
82	 * returning string properties, so the client doesn't then have to
83	 * recopy the string to make it null terminated.
84	 */
85	switch (rep.format) {
86	case 8:
87	    nbytes = rep.nItems;
88	    rbytes = rep.nItems + 1;
89	    if (rbytes > 0 &&
90		(*prop = (unsigned char *) Xmalloc ((unsigned)rbytes)))
91		_XReadPad (dpy, (char *) *prop, nbytes);
92	    break;
93
94	case 16:
95	    nbytes = rep.nItems << 1;
96	    rbytes = rep.nItems * sizeof (short) + 1;
97	    if (rbytes > 0 &&
98		(*prop = (unsigned char *) Xmalloc ((unsigned)rbytes)))
99		_XRead16Pad (dpy, (short *) *prop, nbytes);
100	    break;
101
102	case 32:
103	    nbytes = rep.nItems << 2;
104	    rbytes = rep.nItems * sizeof (long) + 1;
105	    if (rbytes > 0 &&
106		(*prop = (unsigned char *) Xmalloc ((unsigned)rbytes)))
107		_XRead32 (dpy, (long *) *prop, nbytes);
108	    break;
109
110	default:
111	    /*
112	     * This part of the code should never be reached.  If it is,
113	     * the server sent back a property with an invalid format.
114	     */
115	    nbytes = rep.length << 2;
116	    _XEatData(dpy, (unsigned long) nbytes);
117	    UnlockDisplay(dpy);
118	    SyncHandle();
119	    return(BadImplementation);
120	}
121	if (! *prop) {
122	    _XEatData(dpy, (unsigned long) nbytes);
123	    UnlockDisplay(dpy);
124	    SyncHandle();
125	    return(BadAlloc);
126	}
127	(*prop)[rbytes - 1] = '\0';
128    }
129
130    *actual_type = rep.propertyType;
131    *actual_format = rep.format;
132    *nitems = rep.nItems;
133    *bytes_after = rep.bytesAfter;
134    UnlockDisplay (dpy);
135    SyncHandle ();
136
137    return Success;
138}
139
140