]> diplodocus.org Git - xorg-xinput/blob - src/test_xi2.c
713bc1a0f3ba0f9f59b243dfd5e155e149d1ed6d
[xorg-xinput] / src / test_xi2.c
1 /*
2 * Copyright © 2009 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25
26 #include "xinput.h"
27 #include <string.h>
28
29 extern void print_classes_xi2(Display*, XIAnyClassInfo **classes,
30 int num_classes);
31
32 #define BitIsOn(ptr, bit) (((unsigned char *) (ptr))[(bit)>>3] & (1 << ((bit) & 7)))
33 #define SetBit(ptr, bit) (((unsigned char *) (ptr))[(bit)>>3] |= (1 << ((bit) & 7)))
34
35 static Window create_win(Display *dpy)
36 {
37 Window win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 200,
38 200, 0, 0, WhitePixel(dpy, 0));
39 Window subwindow = XCreateSimpleWindow(dpy, win, 50, 50, 50, 50, 0, 0,
40 BlackPixel(dpy, 0));
41
42 XSelectInput(dpy, win, ExposureMask);
43 XMapWindow(dpy, subwindow);
44 XMapWindow(dpy, win);
45 XFlush(dpy);
46 return win;
47 }
48
49 static void print_deviceevent(XIDeviceEvent* event)
50 {
51 double *val;
52 int i;
53
54 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
55 printf(" detail: %d\n", event->detail);
56
57 printf(" root: %.2f/%.2f\n", event->root_x, event->root_y);
58 printf(" event: %.2f/%.2f\n", event->event_x, event->event_x);
59
60 printf(" buttons:");
61 for (i = 0; i < event->buttons->mask_len * 8; i++)
62 if (BitIsOn(event->buttons->mask, i))
63 printf(" %d", i);
64 printf("\n");
65
66 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
67 event->mods->locked, event->mods->latched,
68 event->mods->base);
69 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
70 event->group->locked, event->group->latched,
71 event->group->base);
72 printf(" valuators:");
73
74 val = event->valuators->values;
75 for (i = 0; i < event->valuators->mask_len * 8; i++)
76 if (BitIsOn(event->valuators->mask, i))
77 printf(" %.2f", *val++);
78 printf("\n");
79
80 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
81 event->root, event->event, event->child);
82 }
83
84 static void print_devicechangedevent(Display *dpy, XIDeviceChangedEvent *event)
85 {
86 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
87 printf(" reason: %s\n", (event->reason == SlaveSwitch) ? "SlaveSwitch" :
88 "DeviceChanged");
89 print_classes_xi2(dpy, event->classes, event->num_classes);
90 }
91
92 static void print_hierarchychangedevent(XIDeviceHierarchyEvent *event)
93 {
94 int i;
95 printf(" Changes happened: %s %s %s %s %s %s %s\n",
96 (event->flags & HF_MasterAdded) ? "[new master]" : "",
97 (event->flags & HF_MasterRemoved) ? "[master removed]" : "",
98 (event->flags & HF_SlaveAdded) ? "[new slave]" : "",
99 (event->flags & HF_SlaveRemoved) ? "[slave removed]" : "",
100 (event->flags & HF_SlaveAttached) ? "[slave attached]" : "",
101 (event->flags & HF_DeviceEnabled) ? "[device enabled]" : "",
102 (event->flags & HF_DeviceDisabled) ? "[device disabled]" : "");
103
104 for (i = 0; i < event->num_devices; i++)
105 {
106 char *use;
107 switch(event->info[i].use)
108 {
109 case MasterPointer: use = "master pointer";
110 case MasterKeyboard: use = "master keyboard"; break;
111 case SlavePointer: use = "slave pointer";
112 case SlaveKeyboard: use = "slave keyboard"; break;
113 case FloatingSlave: use = "floating slave"; break;
114 break;
115 }
116
117 printf(" device %d [%s (%d)] is %s\n",
118 event->info[i].deviceid,
119 use,
120 event->info[i].attachment,
121 (event->info[i].enabled) ? "enabled" : "disabled");
122 }
123 }
124
125 static void print_rawevent(XIRawDeviceEvent *event)
126 {
127 int i;
128 double *val, *raw_val;
129
130 printf(" device: %d\n", event->deviceid);
131 printf(" detail: %d\n", event->detail);
132 printf(" valuators:\n");
133
134 val = event->valuators->values;
135 raw_val = event->raw_values;
136 for (i = 0; i < event->valuators->mask_len * 8; i++)
137 if (BitIsOn(event->valuators->mask, i))
138 printf(" %2d: %.2f (%.2f)\n", i, *val++, *raw_val++);
139 printf("\n");
140 }
141
142 static void print_enterleave(XILeaveEvent* event)
143 {
144 char *mode, *detail;
145 int i;
146
147 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
148 event->root, event->event, event->child);
149 switch(event->mode)
150 {
151 case NotifyNormal: mode = "NotifyNormal"; break;
152 case NotifyGrab: mode = "NotifyGrab"; break;
153 case NotifyUngrab: mode = "NotifyUngrab"; break;
154 case NotifyWhileGrabbed: mode = "NotifyWhileGrabbed"; break;
155 }
156 switch (event->detail)
157 {
158 case NotifyAncestor: detail = "NotifyAncestor"; break;
159 case NotifyVirtual: detail = "NotifyVirtual"; break;
160 case NotifyInferior: detail = "NotifyInferior"; break;
161 case NotifyNonlinear: detail = "NotifyNonlinear"; break;
162 case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
163 case NotifyPointer: detail = "NotifyPointer"; break;
164 case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
165 case NotifyDetailNone: detail = "NotifyDetailNone"; break;
166 }
167 printf(" mode: %s (detail %s)\n", mode, detail);
168 printf(" flags: %s %s\n", event->focus ? "[focus]" : "",
169 event->same_screen ? "[same screen]" : "");
170 printf(" buttons:");
171 for (i = 0; i < event->buttons->mask_len * 8; i++)
172 if (BitIsOn(event->buttons->mask, i))
173 printf(" %d", i);
174 printf("\n");
175
176 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
177 event->mods->locked, event->mods->latched,
178 event->mods->base);
179 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
180 event->group->locked, event->group->latched,
181 event->group->base);
182
183 printf(" root x/y: %.2f / %.2f\n", event->root_x, event->root_y);
184 printf(" event x/y: %.2f / %.2f\n", event->event_x, event->event_y);
185
186 }
187
188 void
189 test_sync_grab(Display *display, Window win)
190 {
191 int loop = 3;
192 int rc;
193 XIDeviceEventMask mask;
194
195 /* Select for motion events */
196 mask.deviceid = AllDevices;
197 mask.mask_len = 2;
198 mask.mask = calloc(2, sizeof(char));
199 SetBit(mask.mask, XI_ButtonPress);
200
201 if ((rc = XIGrabDevice(display, 2, win, CurrentTime, None, GrabModeSync,
202 GrabModeAsync, False, &mask)) != GrabSuccess)
203 {
204 fprintf(stderr, "Grab failed with %d\n", rc);
205 return;
206 }
207 free(mask.mask);
208
209 XSync(display, True);
210 XIAllowEvents(display, 2, SyncPointer, CurrentTime);
211 XFlush(display);
212
213 printf("Holding sync grab for %d button presses.\n", loop);
214
215 while(loop--)
216 {
217 XIEvent ev;
218
219 XNextEvent(display, (XEvent*)&ev);
220 if (ev.type == GenericEvent)
221 {
222 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
223 print_deviceevent(event);
224 XIAllowEvents(display, 2, SyncPointer, CurrentTime);
225 XIFreeEventData(&ev);
226 }
227 }
228
229 XIUngrabDevice(display, 2, CurrentTime);
230 printf("Done\n");
231 }
232
233 int
234 test_xi2(Display *display,
235 int argc,
236 char *argv[],
237 char *name,
238 char *desc)
239 {
240 XIDeviceEventMask mask;
241 Window win;
242
243 list(display, argc, argv, name, desc);
244 win = create_win(display);
245
246 XSync(display, False);
247
248 /* Select for motion events */
249 mask.deviceid = AllDevices;
250 mask.mask_len = 2;
251 mask.mask = calloc(2, sizeof(char));
252 SetBit(mask.mask, XI_ButtonPress);
253 SetBit(mask.mask, XI_ButtonRelease);
254 SetBit(mask.mask, XI_Motion);
255 SetBit(mask.mask, XI_KeyPress);
256 SetBit(mask.mask, XI_KeyPress);
257 SetBit(mask.mask, XI_DeviceChanged);
258 SetBit(mask.mask, XI_Enter);
259 SetBit(mask.mask, XI_Leave);
260 SetBit(mask.mask, XI_FocusIn);
261 SetBit(mask.mask, XI_FocusOut);
262 SetBit(mask.mask, XI_HierarchyChanged);
263 XISelectEvent(display, win, &mask, 1);
264
265 mask.deviceid = AllMasterDevices;
266 memset(mask.mask, 0, 2);
267 SetBit(mask.mask, XI_RawEvent);
268 XISelectEvent(display, DefaultRootWindow(display), &mask, 1);
269
270 free(mask.mask);
271
272 {
273 XEvent event;
274 XMaskEvent(display, ExposureMask, &event);
275 XSelectInput(display, win, 0);
276 }
277
278 test_sync_grab(display, win);
279
280 while(1)
281 {
282 XIEvent ev;
283 XNextEvent(display, (XEvent*)&ev);
284 if (ev.type == GenericEvent)
285 {
286 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
287
288 printf("EVENT type %d\n", event->evtype);
289 switch (event->evtype)
290 {
291 case XI_DeviceChanged:
292 print_devicechangedevent(display,
293 (XIDeviceChangedEvent*)event);
294 break;
295 case XI_HierarchyChanged:
296 print_hierarchychangedevent((XIDeviceHierarchyEvent*)event);
297 break;
298 case XI_RawEvent:
299 print_rawevent((XIRawDeviceEvent*)event);
300 break;
301 case XI_Enter:
302 case XI_Leave:
303 case XI_FocusIn:
304 case XI_FocusOut:
305 print_enterleave((XILeaveEvent*)event);
306 break;
307 default:
308 print_deviceevent(event);
309 break;
310 }
311 }
312
313 XIFreeEventData(&ev);
314 }
315
316 XDestroyWindow(display, win);
317
318 return EXIT_SUCCESS;
319 }