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
|
/************************************************************
Copyright 2009 Red Hat, Inc.
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice 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
AUTHOR 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.
Except as contained in this notice, the name of the author shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the author.
*/
/***********************************************************************
*
* XISelectEvent - Select for XI2 events.
*
*/
#include <stdint.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XI.h>
#include <X11/extensions/XI2proto.h>
#include <X11/extensions/XInput2.h>
#include <X11/extensions/extutil.h>
#include <X11/extensions/ge.h>
#include <X11/extensions/geproto.h>
#include "XIint.h"
int
XISelectEvents(Display* dpy, Window win, XIEventMask* masks, int num_masks)
{
XIEventMask *current;
xXISelectEventsReq *req;
xXIEventMask mask;
int i;
int len = 0;
XExtDisplayInfo *info = XInput_find_display(dpy);
LockDisplay(dpy);
if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
return (NoSuchExtension);
GetReq(XISelectEvents, req);
req->reqType = info->codes->major_opcode;
req->ReqType = X_XISelectEvents;
req->window = win;
req->num_masks = num_masks;
/* get the right length */
for (i = 0; i < num_masks; i++)
{
len++;
current = &masks[i];
len += (current->mask_len + 3)/4;
}
SetReqLen(req, len, len);
for (i = 0; i < num_masks; i++)
{
char *buff;
current = &masks[i];
mask.deviceid = current->deviceid;
mask.mask_len = (current->mask_len + 3)/4;
/* masks.mask_len is in bytes, but we need 4-byte units on the wire,
* and they need to be padded with 0 */
buff = calloc(1, mask.mask_len * 4);
memcpy(buff, current->mask, current->mask_len);
Data32(dpy, &mask, sizeof(xXIEventMask));
Data(dpy, buff, mask.mask_len * 4);
free(buff);
}
UnlockDisplay(dpy);
SyncHandle();
return Success;
}
XIEventMask*
XIGetSelectedEvents(Display* dpy, Window win, int *num_masks_return)
{
int i, len = 0;
unsigned char *mask;
XIEventMask *mask_out = NULL;
xXIEventMask *mask_in = NULL, *mi;
xXIGetSelectedEventsReq *req;
xXIGetSelectedEventsReply reply;
XExtDisplayInfo *info = XInput_find_display(dpy);
LockDisplay(dpy);
if (_XiCheckExtInit(dpy, Dont_Check, info) == -1)
{
*num_masks_return = -1;
return NULL;
}
GetReq(XIGetSelectedEvents, req);
req->reqType = info->codes->major_opcode;
req->ReqType = X_XIGetSelectedEvents;
req->window = win;
if (!_XReply(dpy, (xReply *) &reply, 0, xFalse))
goto error;
if (reply.num_masks == 0)
{
*num_masks_return = 0;
return NULL;
}
mask_in = Xmalloc(reply.length * 4);
if (!mask_in)
goto error;
_XRead(dpy, (char*)mask_in, reply.length * 4);
/* Memory layout of the XIEventMask for a 3 mask reply:
* [struct a][struct b][struct c][masks a][masks b][masks c]
*/
len = reply.num_masks * sizeof(XIEventMask);
for (i = 0, mi = mask_in; i < reply.num_masks; i++)
{
len += mi->mask_len * 4;
mi = (xXIEventMask*)((char*)mi + mi->mask_len * 4);
mi++;
}
mask_out = Xmalloc(len);
if (!mask_out)
goto error;
mi = mask_in;
mask = (unsigned char*)&mask_out[reply.num_masks];
for (i = 0; i < reply.num_masks; i++)
{
mask_out[i].deviceid = mi->deviceid;
mask_out[i].mask_len = mi->mask_len * 4;
mask_out[i].mask = mask;
memcpy(mask_out[i].mask, &mi[1], mask_out[i].mask_len);
mask += mask_out[i].mask_len;
mi = (xXIEventMask*)((char*)mi + mi->mask_len * 4);
mi++;
}
*num_masks_return = reply.num_masks;
return mask_out;
error:
if (mask_in)
Xfree(mask_in);
*num_masks_return = -1;
UnlockDisplay(dpy);
SyncHandle();
return NULL;
}
|