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.
38 #include <X11/Xlibint.h>
39 #include <X11/extensions/XI2proto.h>
40 #include <X11/extensions/XInput2.h>
41 #include <X11/extensions/extutil.h>
42 #include <X11/extensions/ge.h>
43 #include <X11/extensions/geproto.h>
47 XISelectEvents(Display* dpy, Window win, XIEventMask* masks, int num_masks)
50 xXISelectEventsReq *req;
56 XExtDisplayInfo *info = XInput_find_display(dpy);
58 if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1) {
62 GetReq(XISelectEvents, req);
64 req->reqType = info->codes->major_opcode;
65 req->ReqType = X_XISelectEvents;
67 req->num_masks = num_masks;
69 /* get the right length */
70 for (i = 0; i < num_masks; i++)
74 len += (current->mask_len + 3)/4;
77 SetReqLen(req, len, len);
79 for (i = 0; i < num_masks; i++)
83 mask.deviceid = current->deviceid;
84 mask.mask_len = (current->mask_len + 3)/4;
85 /* masks.mask_len is in bytes, but we need 4-byte units on the wire,
86 * and they need to be padded with 0 */
87 buff = calloc(1, mask.mask_len * 4);
88 memcpy(buff, current->mask, current->mask_len);
89 Data(dpy, (char*)&mask, sizeof(xXIEventMask));
90 Data(dpy, buff, mask.mask_len * 4);
102 XIGetSelectedEvents(Display* dpy, Window win, int *num_masks_return)
106 XIEventMask *mask_out = NULL;
107 xXIEventMask *mask_in = NULL, *mi;
108 xXIGetSelectedEventsReq *req;
109 xXIGetSelectedEventsReply reply;
110 XExtDisplayInfo *info = XInput_find_display(dpy);
112 *num_masks_return = -1;
114 if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
117 GetReq(XIGetSelectedEvents, req);
119 req->reqType = info->codes->major_opcode;
120 req->ReqType = X_XIGetSelectedEvents;
123 if (!_XReply(dpy, (xReply *) &reply, 0, xFalse))
126 if (reply.num_masks == 0)
128 *num_masks_return = 0;
132 mask_in = Xmalloc(reply.length * 4);
136 _XRead(dpy, (char*)mask_in, reply.length * 4);
138 /* Memory layout of the XIEventMask for a 3 mask reply:
139 * [struct a][struct b][struct c][masks a][masks b][masks c]
141 len = reply.num_masks * sizeof(XIEventMask);
143 for (i = 0, mi = mask_in; i < reply.num_masks; i++)
145 len += mi->mask_len * 4;
146 mi = (xXIEventMask*)((char*)mi + mi->mask_len * 4);
150 mask_out = Xmalloc(len);
155 mask = (unsigned char*)&mask_out[reply.num_masks];
156 for (i = 0; i < reply.num_masks; i++)
158 mask_out[i].deviceid = mi->deviceid;
159 mask_out[i].mask_len = mi->mask_len * 4;
160 mask_out[i].mask = mask;
161 memcpy(mask_out[i].mask, &mi[1], mask_out[i].mask_len);
162 mask += mask_out[i].mask_len;
163 mi = (xXIEventMask*)((char*)mi + mi->mask_len * 4);
167 *num_masks_return = reply.num_masks;