XIProperties.c revision 190694da
1c27c18e8Smrg/*
2c27c18e8Smrg * Copyright © 2009 Red Hat, Inc.
3c27c18e8Smrg *
4c27c18e8Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5c27c18e8Smrg * copy of this software and associated documentation files (the "Software"),
6c27c18e8Smrg * to deal in the Software without restriction, including without limitation
7c27c18e8Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c27c18e8Smrg * and/or sell copies of the Software, and to permit persons to whom the
9c27c18e8Smrg * Software is furnished to do so, subject to the following conditions:
10c27c18e8Smrg *
11c27c18e8Smrg * The above copyright notice and this permission notice (including the next
12c27c18e8Smrg * paragraph) shall be included in all copies or substantial portions of the
13c27c18e8Smrg * Software.
14c27c18e8Smrg *
15c27c18e8Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c27c18e8Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c27c18e8Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18c27c18e8Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c27c18e8Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c27c18e8Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21c27c18e8Smrg * DEALINGS IN THE SOFTWARE.
22c27c18e8Smrg *
23c27c18e8Smrg */
24c27c18e8Smrg
25c27c18e8Smrg/***********************************************************************
26c27c18e8Smrg * XI2 property requests, list, change, delete and get properties.
27c27c18e8Smrg */
28c27c18e8Smrg
29c27c18e8Smrg
30c27c18e8Smrg#ifdef HAVE_CONFIG_H
31c27c18e8Smrg#include <config.h>
32c27c18e8Smrg#endif
33c27c18e8Smrg
34c27c18e8Smrg#include <stdint.h>
35c27c18e8Smrg#include <X11/Xlibint.h>
36c27c18e8Smrg#include <X11/extensions/XI2.h>
37c27c18e8Smrg#include <X11/extensions/XI2proto.h>
38c27c18e8Smrg#include <X11/extensions/XInput2.h>
39c27c18e8Smrg#include <X11/extensions/extutil.h>
40c27c18e8Smrg#include "XIint.h"
41190694daSmrg#include <limits.h>
42c27c18e8Smrg
43c27c18e8SmrgAtom*
44c27c18e8SmrgXIListProperties(Display* dpy, int deviceid, int *num_props_return)
45c27c18e8Smrg{
46c27c18e8Smrg    xXIListPropertiesReq        *req;
47c27c18e8Smrg    xXIListPropertiesReply      rep;
48c27c18e8Smrg    XExtDisplayInfo             *info = XInput_find_display(dpy);
49c27c18e8Smrg    Atom                        *props = NULL;
50c27c18e8Smrg
51c27c18e8Smrg    LockDisplay(dpy);
52c27c18e8Smrg    *num_props_return = 0;
53b789ec8aSmrg    if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
54c27c18e8Smrg	goto cleanup;
55c27c18e8Smrg
56c27c18e8Smrg    GetReq(XIListProperties, req);
57c27c18e8Smrg    req->reqType = info->codes->major_opcode;
58c27c18e8Smrg    req->ReqType = X_XIListProperties;
59c27c18e8Smrg    req->deviceid = deviceid;
60c27c18e8Smrg
61c27c18e8Smrg    if (!_XReply(dpy, (xReply*)&rep, 0, xFalse))
62c27c18e8Smrg        goto cleanup;
63c27c18e8Smrg
64c27c18e8Smrg    if (rep.num_properties) {
65c27c18e8Smrg        props = (Atom*)Xmalloc(rep.num_properties * sizeof(Atom));
66c27c18e8Smrg        if (!props)
67c27c18e8Smrg        {
68c27c18e8Smrg            _XEatData(dpy, rep.num_properties << 2);
69c27c18e8Smrg            goto cleanup;
70c27c18e8Smrg        }
71c27c18e8Smrg
72c27c18e8Smrg        _XRead32(dpy, (long*)props, rep.num_properties << 2);
73c27c18e8Smrg    }
74c27c18e8Smrg
75c27c18e8Smrg    *num_props_return = rep.num_properties;
76c27c18e8Smrg
77c27c18e8Smrgcleanup:
78c27c18e8Smrg    UnlockDisplay(dpy);
79c27c18e8Smrg    SyncHandle();
80c27c18e8Smrg    return props;
81c27c18e8Smrg}
82c27c18e8Smrg
83c27c18e8Smrg
84c27c18e8Smrgvoid
85c27c18e8SmrgXIDeleteProperty(Display* dpy, int deviceid, Atom property)
86c27c18e8Smrg{
87c27c18e8Smrg    xXIDeletePropertyReq   *req;
88c27c18e8Smrg
89c27c18e8Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
90c27c18e8Smrg
91c27c18e8Smrg    LockDisplay(dpy);
92b789ec8aSmrg    if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
93c27c18e8Smrg	return;
94c27c18e8Smrg
95c27c18e8Smrg    GetReq(XIDeleteProperty, req);
96c27c18e8Smrg    req->reqType    = info->codes->major_opcode;
97c27c18e8Smrg    req->ReqType    = X_XIDeleteProperty;
98c27c18e8Smrg    req->deviceid   = deviceid;
99c27c18e8Smrg    req->property   = property;
100c27c18e8Smrg
101c27c18e8Smrg    UnlockDisplay(dpy);
102c27c18e8Smrg    SyncHandle();
103c27c18e8Smrg}
104c27c18e8Smrg
105c27c18e8Smrgvoid
106c27c18e8SmrgXIChangeProperty(Display* dpy, int deviceid, Atom property, Atom type,
107c27c18e8Smrg                 int format, int mode, unsigned char *data, int num_items)
108c27c18e8Smrg{
109c27c18e8Smrg    xXIChangePropertyReq *req;
110c27c18e8Smrg    int len;
111c27c18e8Smrg
112c27c18e8Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
113c27c18e8Smrg
114c27c18e8Smrg    LockDisplay(dpy);
115b789ec8aSmrg    if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
116c27c18e8Smrg	return;
117c27c18e8Smrg
118c27c18e8Smrg    GetReq(XIChangeProperty, req);
119c27c18e8Smrg    req->reqType    = info->codes->major_opcode;
120c27c18e8Smrg    req->ReqType    = X_XIChangeProperty;
121c27c18e8Smrg    req->deviceid   = deviceid;
122c27c18e8Smrg    req->property   = property;
123c27c18e8Smrg    req->type       = type;
124c27c18e8Smrg    req->mode       = mode;
125c27c18e8Smrg    if (num_items < 0) {
126c27c18e8Smrg	req->num_items = 0;
127c27c18e8Smrg	req->format = 0; /* ask for garbage, get garbage */
128c27c18e8Smrg    } else {
129c27c18e8Smrg	req->num_items = num_items;
130c27c18e8Smrg	req->format = format;
131c27c18e8Smrg    }
132c27c18e8Smrg
133c27c18e8Smrg    switch (req->format) {
134c27c18e8Smrg    case 8:
135c27c18e8Smrg	len = (num_items + 3)/4;
136c27c18e8Smrg        SetReqLen(req, len, len);
137c27c18e8Smrg        len = num_items;
138c27c18e8Smrg	break;
139c27c18e8Smrg
140c27c18e8Smrg    case 16:
141c27c18e8Smrg	len = (num_items + 1)/2;
142c27c18e8Smrg        SetReqLen(req, len, len);
143c27c18e8Smrg        len = num_items * 2;
144c27c18e8Smrg	break;
145c27c18e8Smrg
146c27c18e8Smrg    case 32:
147c27c18e8Smrg	len = num_items;
148c27c18e8Smrg        SetReqLen(req, len, len);
149c27c18e8Smrg        len = num_items * 4;
150c27c18e8Smrg	break;
151c27c18e8Smrg
152c27c18e8Smrg    default:
153c27c18e8Smrg	/* BadValue will be generated */ ;
1543e256790Smrg        len = 0;
155c27c18e8Smrg    }
156c27c18e8Smrg
157c27c18e8Smrg    /* we use data instead of Data32 and friends to avoid Xlib's braindead
158c27c18e8Smrg     * 64 bit handling.*/
159c27c18e8Smrg
160c27c18e8Smrg    Data(dpy, (const char*)data, len);
161c27c18e8Smrg    UnlockDisplay(dpy);
162c27c18e8Smrg    SyncHandle();
163c27c18e8Smrg}
164c27c18e8Smrg
165c27c18e8SmrgStatus
166c27c18e8SmrgXIGetProperty(Display* dpy, int deviceid, Atom property, long offset,
167c27c18e8Smrg              long length, Bool delete_property, Atom type,
168c27c18e8Smrg              Atom *type_return, int *format_return,
169c27c18e8Smrg              unsigned long *num_items_return,unsigned long *bytes_after_return,
170c27c18e8Smrg              unsigned char **data)
171c27c18e8Smrg{
172c27c18e8Smrg    xXIGetPropertyReq   *req;
173c27c18e8Smrg    xXIGetPropertyReply rep;
174190694daSmrg    unsigned long       nbytes, rbytes;
175c27c18e8Smrg
176c27c18e8Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
177c27c18e8Smrg
178c27c18e8Smrg    LockDisplay(dpy);
179b789ec8aSmrg    if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
180c27c18e8Smrg	return 1;
181c27c18e8Smrg
182c27c18e8Smrg    GetReq(XIGetProperty, req);
183c27c18e8Smrg    req->reqType    = info->codes->major_opcode;
184c27c18e8Smrg    req->ReqType    = X_XIGetProperty;
185c27c18e8Smrg    req->deviceid   = deviceid;
186c27c18e8Smrg    req->property   = property;
187c27c18e8Smrg    req->type       = type;
188c27c18e8Smrg    req->offset     = offset;
189c27c18e8Smrg    req->len        = length;
190c27c18e8Smrg    req->delete     = delete_property;
191c27c18e8Smrg
192c27c18e8Smrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
193c27c18e8Smrg    {
194c27c18e8Smrg	UnlockDisplay (dpy);
195c27c18e8Smrg	SyncHandle ();
196c27c18e8Smrg	return 1;
197c27c18e8Smrg    }
198c27c18e8Smrg
199c27c18e8Smrg    *data = NULL;
200c27c18e8Smrg
201c27c18e8Smrg    if (rep.type != None) {
202c27c18e8Smrg        if (rep.format != 8 && rep.format != 16 && rep.format != 32) {
203c27c18e8Smrg	    /*
204c27c18e8Smrg	     * This part of the code should never be reached.  If it is,
205c27c18e8Smrg	     * the server sent back a property with an invalid format.
206c27c18e8Smrg	     */
207c27c18e8Smrg	    nbytes = rep.length << 2;
208c27c18e8Smrg	    _XEatData(dpy, nbytes);
209c27c18e8Smrg	    UnlockDisplay(dpy);
210c27c18e8Smrg	    SyncHandle();
211c27c18e8Smrg	    return(BadImplementation);
212c27c18e8Smrg        }
213c27c18e8Smrg
214c27c18e8Smrg	/*
215c27c18e8Smrg	 * One extra byte is malloced than is needed to contain the property
216c27c18e8Smrg	 * data, but this last byte is null terminated and convenient for
217c27c18e8Smrg	 * returning string properties, so the client doesn't then have to
218c27c18e8Smrg	 * recopy the string to make it null terminated.
219c27c18e8Smrg	 */
220c27c18e8Smrg
221190694daSmrg	if (rep.num_items < (INT_MAX / (rep.format/8))) {
222190694daSmrg	    nbytes = rep.num_items * rep.format/8;
223190694daSmrg	    rbytes = nbytes + 1;
224190694daSmrg	    *data = Xmalloc(rbytes);
225190694daSmrg	}
226c27c18e8Smrg
227c27c18e8Smrg	if (!(*data)) {
228c27c18e8Smrg	    _XEatData(dpy, nbytes);
229c27c18e8Smrg	    UnlockDisplay(dpy);
230c27c18e8Smrg	    SyncHandle();
231c27c18e8Smrg	    return(BadAlloc);
232c27c18e8Smrg	}
233c27c18e8Smrg
234c27c18e8Smrg        _XReadPad (dpy, (char *)*data, nbytes);
235c27c18e8Smrg	(*data)[rbytes - 1] = '\0';
236c27c18e8Smrg    }
237c27c18e8Smrg
238c27c18e8Smrg    *type_return        = rep.type;
239c27c18e8Smrg    *format_return      = rep.format;
240c27c18e8Smrg    *num_items_return   = rep.num_items;
241c27c18e8Smrg    *bytes_after_return = rep.bytes_after;
242c27c18e8Smrg    UnlockDisplay (dpy);
243c27c18e8Smrg    SyncHandle();
244c27c18e8Smrg    return Success;
245c27c18e8Smrg}
246