summaryrefslogtreecommitdiff
path: root/lib/libXi/src/XIProperties.c
blob: 045cc715f17c58ab9fb35bfb1fe379f29f7cf40e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright © 2009 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

/***********************************************************************
 * XI2 property requests, list, change, delete and get properties.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XI2.h>
#include <X11/extensions/XI2proto.h>
#include <X11/extensions/XInput2.h>
#include <X11/extensions/extutil.h>
#include "XIint.h"

Atom*
XIListProperties(Display* dpy, int deviceid, int *num_props_return)
{
    xXIListPropertiesReq        *req;
    xXIListPropertiesReply      rep;
    XExtDisplayInfo             *info = XInput_find_display(dpy);
    Atom                        *props = NULL;

    LockDisplay(dpy);
    *num_props_return = 0;
    if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
	goto cleanup;

    GetReq(XIListProperties, req);
    req->reqType = info->codes->major_opcode;
    req->ReqType = X_XIListProperties;
    req->deviceid = deviceid;

    if (!_XReply(dpy, (xReply*)&rep, 0, xFalse))
        goto cleanup;

    if (rep.num_properties) {
        props = (Atom*)Xmalloc(rep.num_properties * sizeof(Atom));
        if (!props)
        {
            _XEatData(dpy, rep.num_properties << 2);
            goto cleanup;
        }

        _XRead32(dpy, (long*)props, rep.num_properties << 2);
    }

    *num_props_return = rep.num_properties;

cleanup:
    UnlockDisplay(dpy);
    SyncHandle();
    return props;
}


void
XIDeleteProperty(Display* dpy, int deviceid, Atom property)
{
    xXIDeletePropertyReq   *req;

    XExtDisplayInfo *info = XInput_find_display(dpy);

    LockDisplay(dpy);
    if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
	return;

    GetReq(XIDeleteProperty, req);
    req->reqType    = info->codes->major_opcode;
    req->ReqType    = X_XIDeleteProperty;
    req->deviceid   = deviceid;
    req->property   = property;

    UnlockDisplay(dpy);
    SyncHandle();
}

void
XIChangeProperty(Display* dpy, int deviceid, Atom property, Atom type,
                 int format, int mode, unsigned char *data, int num_items)
{
    xXIChangePropertyReq *req;
    int len;

    XExtDisplayInfo *info = XInput_find_display(dpy);

    LockDisplay(dpy);
    if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
	return;

    GetReq(XIChangeProperty, req);
    req->reqType    = info->codes->major_opcode;
    req->ReqType    = X_XIChangeProperty;
    req->deviceid   = deviceid;
    req->property   = property;
    req->type       = type;
    req->mode       = mode;
    if (num_items < 0) {
	req->num_items = 0;
	req->format = 0; /* ask for garbage, get garbage */
    } else {
	req->num_items = num_items;
	req->format = format;
    }

    switch (req->format) {
    case 8:
	len = (num_items + 3)/4;
        SetReqLen(req, len, len);
        len = num_items;
	break;

    case 16:
	len = (num_items + 1)/2;
        SetReqLen(req, len, len);
        len = num_items * 2;
	break;

    case 32:
	len = num_items;
        SetReqLen(req, len, len);
        len = num_items * 4;
	break;

    default:
	/* BadValue will be generated */ ;
    }

    /* we use data instead of Data32 and friends to avoid Xlib's braindead
     * 64 bit handling.*/

    Data(dpy, (const char*)data, len);
    UnlockDisplay(dpy);
    SyncHandle();
}

Status
XIGetProperty(Display* dpy, int deviceid, Atom property, long offset,
              long length, Bool delete_property, Atom type,
              Atom *type_return, int *format_return,
              unsigned long *num_items_return,unsigned long *bytes_after_return,
              unsigned char **data)
{
    xXIGetPropertyReq   *req;
    xXIGetPropertyReply rep;
    long                    nbytes, rbytes;

    XExtDisplayInfo *info = XInput_find_display(dpy);

    LockDisplay(dpy);
    if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
	return 1;

    GetReq(XIGetProperty, req);
    req->reqType    = info->codes->major_opcode;
    req->ReqType    = X_XIGetProperty;
    req->deviceid   = deviceid;
    req->property   = property;
    req->type       = type;
    req->offset     = offset;
    req->len        = length;
    req->delete     = delete_property;

    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
    {
	UnlockDisplay (dpy);
	SyncHandle ();
	return 1;
    }

    *data = NULL;

    if (rep.type != None) {
        if (rep.format != 8 && rep.format != 16 && rep.format != 32) {
	    /*
	     * This part of the code should never be reached.  If it is,
	     * the server sent back a property with an invalid format.
	     */
	    nbytes = rep.length << 2;
	    _XEatData(dpy, nbytes);
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return(BadImplementation);
        }

	/*
	 * One extra byte is malloced than is needed to contain the property
	 * data, but this last byte is null terminated and convenient for
	 * returning string properties, so the client doesn't then have to
	 * recopy the string to make it null terminated.
	 */

        nbytes = rep.num_items * rep.format/8;
        rbytes = nbytes + 1;
        *data = Xmalloc(rbytes);

	if (!(*data)) {
	    _XEatData(dpy, nbytes);
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return(BadAlloc);
	}

        _XReadPad (dpy, (char *)*data, nbytes);
	(*data)[rbytes - 1] = '\0';
    }

    *type_return        = rep.type;
    *format_return      = rep.format;
    *num_items_return   = rep.num_items;
    *bytes_after_return = rep.bytes_after;
    UnlockDisplay (dpy);
    SyncHandle();
    return Success;
}