1
/************************************************************
3
Copyright 2009 Red Hat, Inc.
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
/***********************************************************************
29
* XISelectEvent - Select for XI2 events.
35
#include <X11/Xlibint.h>
36
#include <X11/extensions/XI2proto.h>
37
#include <X11/extensions/XInput2.h>
38
#include <X11/extensions/extutil.h>
39
#include <X11/extensions/ge.h>
40
#include <X11/extensions/geproto.h>
44
XISelectEvents(Display* dpy, Window win, XIEventMask* masks, int num_masks)
47
xXISelectEventsReq *req;
52
XExtDisplayInfo *info = XInput_find_display(dpy);
54
if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
55
return (NoSuchExtension);
56
GetReq(XISelectEvents, req);
58
req->reqType = info->codes->major_opcode;
59
req->ReqType = X_XISelectEvents;
61
req->num_masks = num_masks;
63
/* get the right length */
64
for (i = 0; i < num_masks; i++)
68
len += (current->mask_len + 3)/4;
71
SetReqLen(req, len, len);
73
for (i = 0; i < num_masks; i++)
77
mask.deviceid = current->deviceid;
78
mask.mask_len = (current->mask_len + 3)/4;
79
/* masks.mask_len is in bytes, but we need 4-byte units on the wire,
80
* and they need to be padded with 0 */
81
buff = calloc(1, mask.mask_len * 4);
82
memcpy(buff, current->mask, current->mask_len);
83
Data32(dpy, &mask, sizeof(xXIEventMask));
84
Data(dpy, buff, mask.mask_len * 4);
95
XIGetSelectedEvents(Display* dpy, Window win, int *num_masks_return)
99
XIEventMask *mask_out = NULL;
100
xXIEventMask *mask_in = NULL, *mi;
101
xXIGetSelectedEventsReq *req;
102
xXIGetSelectedEventsReply reply;
104
XExtDisplayInfo *info = XInput_find_display(dpy);
106
if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
108
*num_masks_return = -1;
112
GetReq(XIGetSelectedEvents, req);
114
req->reqType = info->codes->major_opcode;
115
req->ReqType = X_XIGetSelectedEvents;
118
if (!_XReply(dpy, (xReply *) &reply, 0, xFalse))
121
if (reply.num_masks == 0)
123
*num_masks_return = 0;
127
mask_in = Xmalloc(reply.length * 4);
131
_XRead(dpy, (char*)mask_in, reply.length * 4);
133
/* Memory layout of the XIEventMask for a 3 mask reply:
134
* [struct a][struct b][struct c][masks a][masks b][masks c]
136
len = reply.num_masks * sizeof(XIEventMask);
138
for (i = 0, mi = mask_in; i < reply.num_masks; i++)
140
len += mi->mask_len * 4;
141
mi = (xXIEventMask*)((char*)mi + mi->mask_len * 4);
145
mask_out = Xmalloc(len);
150
mask = (unsigned char*)&mask_out[reply.num_masks];
151
for (i = 0; i < reply.num_masks; i++)
153
mask_out[i].deviceid = mi->deviceid;
154
mask_out[i].mask_len = mi->mask_len * 4;
155
mask_out[i].mask = mask;
156
memcpy(mask_out[i].mask, &mi[1], mask_out[i].mask_len);
157
mask += mask_out[i].mask_len;
158
mi = (xXIEventMask*)((char*)mi + mi->mask_len * 4);
162
*num_masks_return = reply.num_masks;
169
*num_masks_return = -1;