]> diplodocus.org Git - xorg-xinput/blob - src/test_xi2.c
32fa3f06f6c420e7e272d4c8e3fbd7b3ed874c52
[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 static Window create_win(Display *dpy)
33 {
34 Window win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 200,
35 200, 0, 0, WhitePixel(dpy, 0));
36 Window subwindow = XCreateSimpleWindow(dpy, win, 50, 50, 50, 50, 0, 0,
37 BlackPixel(dpy, 0));
38
39 XSelectInput(dpy, win, ExposureMask);
40 XMapWindow(dpy, subwindow);
41 XMapWindow(dpy, win);
42 XFlush(dpy);
43 return win;
44 }
45
46 static void print_deviceevent(XIDeviceEvent* event)
47 {
48 double *val;
49 int i;
50
51 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
52 printf(" detail: %d\n", event->detail);
53
54 printf(" root: %.2f/%.2f\n", event->root_x, event->root_y);
55 printf(" event: %.2f/%.2f\n", event->event_x, event->event_x);
56
57 printf(" buttons:");
58 for (i = 0; i < event->buttons->mask_len * 8; i++)
59 if (XIMaskIsSet(event->buttons->mask, i))
60 printf(" %d", i);
61 printf("\n");
62
63 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
64 event->mods->locked, event->mods->latched,
65 event->mods->base);
66 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
67 event->group->locked, event->group->latched,
68 event->group->base);
69 printf(" valuators:");
70
71 val = event->valuators->values;
72 for (i = 0; i < event->valuators->mask_len * 8; i++)
73 if (XIMaskIsSet(event->valuators->mask, i))
74 printf(" %.2f", *val++);
75 printf("\n");
76
77 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
78 event->root, event->event, event->child);
79 }
80
81 static void print_devicechangedevent(Display *dpy, XIDeviceChangedEvent *event)
82 {
83 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
84 printf(" reason: %s\n", (event->reason == XISlaveSwitch) ? "SlaveSwitch" :
85 "DeviceChanged");
86 print_classes_xi2(dpy, event->classes, event->num_classes);
87 }
88
89 static void print_hierarchychangedevent(XIHierarchyEvent *event)
90 {
91 int i;
92 printf(" Changes happened: %s %s %s %s %s %s %s %s\n",
93 (event->flags & XIMasterAdded) ? "[new master]" : "",
94 (event->flags & XIMasterRemoved) ? "[master removed]" : "",
95 (event->flags & XISlaveAdded) ? "[new slave]" : "",
96 (event->flags & XISlaveRemoved) ? "[slave removed]" : "",
97 (event->flags & XISlaveAttached) ? "[slave attached]" : "",
98 (event->flags & XISlaveDetached) ? "[slave detached]" : "",
99 (event->flags & XIDeviceEnabled) ? "[device enabled]" : "",
100 (event->flags & XIDeviceDisabled) ? "[device disabled]" : "");
101
102 for (i = 0; i < event->num_info; i++)
103 {
104 char *use;
105 switch(event->info[i].use)
106 {
107 case XIMasterPointer: use = "master pointer"; break;
108 case XIMasterKeyboard: use = "master keyboard"; break;
109 case XISlavePointer: use = "slave pointer"; break;
110 case XISlaveKeyboard: use = "slave keyboard"; break;
111 case XIFloatingSlave: use = "floating slave"; break;
112 break;
113 }
114
115 printf(" device %d [%s (%d)] is %s\n",
116 event->info[i].deviceid,
117 use,
118 event->info[i].attachment,
119 (event->info[i].enabled) ? "enabled" : "disabled");
120 if (event->info[i].flags)
121 {
122 printf(" changes: %s %s %s %s %s %s %s %s\n",
123 (event->info[i].flags & XIMasterAdded) ? "[new master]" : "",
124 (event->info[i].flags & XIMasterRemoved) ? "[master removed]" : "",
125 (event->info[i].flags & XISlaveAdded) ? "[new slave]" : "",
126 (event->info[i].flags & XISlaveRemoved) ? "[slave removed]" : "",
127 (event->info[i].flags & XISlaveAttached) ? "[slave attached]" : "",
128 (event->info[i].flags & XISlaveDetached) ? "[slave detached]" : "",
129 (event->info[i].flags & XIDeviceEnabled) ? "[device enabled]" : "",
130 (event->info[i].flags & XIDeviceDisabled) ? "[device disabled]" : "");
131 }
132 }
133 }
134
135 static void print_rawevent(XIRawEvent *event)
136 {
137 int i;
138 double *val, *raw_val;
139
140 printf(" device: %d\n", event->deviceid);
141 printf(" detail: %d\n", event->detail);
142 printf(" valuators:\n");
143
144 val = event->valuators->values;
145 raw_val = event->raw_values;
146 for (i = 0; i < event->valuators->mask_len * 8; i++)
147 if (XIMaskIsSet(event->valuators->mask, i))
148 printf(" %2d: %.2f (%.2f)\n", i, *val++, *raw_val++);
149 printf("\n");
150 }
151
152 static void print_enterleave(XILeaveEvent* event)
153 {
154 char *mode, *detail;
155 int i;
156
157 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
158 event->root, event->event, event->child);
159 switch(event->mode)
160 {
161 case NotifyNormal: mode = "NotifyNormal"; break;
162 case NotifyGrab: mode = "NotifyGrab"; break;
163 case NotifyUngrab: mode = "NotifyUngrab"; break;
164 case NotifyWhileGrabbed: mode = "NotifyWhileGrabbed"; break;
165 }
166 switch (event->detail)
167 {
168 case NotifyAncestor: detail = "NotifyAncestor"; break;
169 case NotifyVirtual: detail = "NotifyVirtual"; break;
170 case NotifyInferior: detail = "NotifyInferior"; break;
171 case NotifyNonlinear: detail = "NotifyNonlinear"; break;
172 case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
173 case NotifyPointer: detail = "NotifyPointer"; break;
174 case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
175 case NotifyDetailNone: detail = "NotifyDetailNone"; break;
176 }
177 printf(" mode: %s (detail %s)\n", mode, detail);
178 printf(" flags: %s %s\n", event->focus ? "[focus]" : "",
179 event->same_screen ? "[same screen]" : "");
180 printf(" buttons:");
181 for (i = 0; i < event->buttons->mask_len * 8; i++)
182 if (XIMaskIsSet(event->buttons->mask, i))
183 printf(" %d", i);
184 printf("\n");
185
186 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
187 event->mods->locked, event->mods->latched,
188 event->mods->base);
189 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
190 event->group->locked, event->group->latched,
191 event->group->base);
192
193 printf(" root x/y: %.2f / %.2f\n", event->root_x, event->root_y);
194 printf(" event x/y: %.2f / %.2f\n", event->event_x, event->event_y);
195
196 }
197
198 static void print_propertyevent(Display *display, XIPropertyEvent* event)
199 {
200 char *changed;
201
202 if (event->what == XIPropertyDeleted)
203 changed = "deleted";
204 else if (event->what == XIPropertyCreated)
205 changed = "created";
206 else
207 changed = "modified";
208
209 printf(" property: %ld '%s'\n", event->property, XGetAtomName(display, event->property));
210 printf(" changed: %s\n", changed);
211
212 }
213 void
214 test_sync_grab(Display *display, Window win)
215 {
216 int loop = 3;
217 int rc;
218 XIEventMask mask;
219
220 /* Select for motion events */
221 mask.deviceid = XIAllDevices;
222 mask.mask_len = 2;
223 mask.mask = calloc(2, sizeof(char));
224 XISetMask(mask.mask, XI_ButtonPress);
225
226 if ((rc = XIGrabDevice(display, 2, win, CurrentTime, None, GrabModeSync,
227 GrabModeAsync, False, &mask)) != GrabSuccess)
228 {
229 fprintf(stderr, "Grab failed with %d\n", rc);
230 return;
231 }
232 free(mask.mask);
233
234 XSync(display, True);
235 XIAllowEvents(display, 2, SyncPointer, CurrentTime);
236 XFlush(display);
237
238 printf("Holding sync grab for %d button presses.\n", loop);
239
240 while(loop--)
241 {
242 XIEvent ev;
243
244 XNextEvent(display, (XEvent*)&ev);
245 if (ev.type == GenericEvent)
246 {
247 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
248 print_deviceevent(event);
249 XIAllowEvents(display, 2, SyncPointer, CurrentTime);
250 XIFreeEventData(&ev);
251 }
252 }
253
254 XIUngrabDevice(display, 2, CurrentTime);
255 printf("Done\n");
256 }
257
258 int
259 test_xi2(Display *display,
260 int argc,
261 char *argv[],
262 char *name,
263 char *desc)
264 {
265 XIEventMask mask;
266 Window win;
267
268 list(display, argc, argv, name, desc);
269 win = create_win(display);
270
271 XSync(display, False);
272
273 /* Select for motion events */
274 mask.deviceid = XIAllDevices;
275 mask.mask_len = 2;
276 mask.mask = calloc(mask.mask_len, sizeof(char));
277 XISetMask(mask.mask, XI_ButtonPress);
278 XISetMask(mask.mask, XI_ButtonRelease);
279 XISetMask(mask.mask, XI_KeyPress);
280 XISetMask(mask.mask, XI_KeyRelease);
281 XISetMask(mask.mask, XI_Motion);
282 XISetMask(mask.mask, XI_DeviceChanged);
283 XISetMask(mask.mask, XI_Enter);
284 XISetMask(mask.mask, XI_Leave);
285 XISetMask(mask.mask, XI_FocusIn);
286 XISetMask(mask.mask, XI_FocusOut);
287 XISetMask(mask.mask, XI_HierarchyChanged);
288 XISetMask(mask.mask, XI_PropertyEvent);
289 XISelectEvents(display, win, &mask, 1);
290 XSync(display, False);
291
292 {
293 XIGrabModifiers modifiers[] = {{0, 0}, {0, 0x10}, {0, 0x1}, {0, 0x11}};
294 int nmods = sizeof(modifiers)/sizeof(modifiers[0]);
295
296 mask.deviceid = 2;
297 memset(mask.mask, 0, 2);
298 XISetMask(mask.mask, XI_KeyPress);
299 XISetMask(mask.mask, XI_KeyRelease);
300 XISetMask(mask.mask, XI_ButtonPress);
301 XISetMask(mask.mask, XI_Motion);
302 XIGrabButton(display, 2, 1, win, None, GrabModeAsync, GrabModeAsync,
303 False, &mask, nmods, modifiers);
304 XIGrabKeysym(display, 3, 0x71, win, GrabModeAsync, GrabModeAsync,
305 False, &mask, nmods, modifiers);
306 XIUngrabButton(display, 3, 1, win, nmods - 2, &modifiers[2]);
307 XIUngrabKeysym(display, 3, 0x71, win, nmods - 2, &modifiers[2]);
308 }
309
310 mask.deviceid = XIAllMasterDevices;
311 memset(mask.mask, 0, 2);
312 XISetMask(mask.mask, XI_RawEvent);
313 XISelectEvents(display, DefaultRootWindow(display), &mask, 1);
314
315 free(mask.mask);
316
317 {
318 XEvent event;
319 XMaskEvent(display, ExposureMask, &event);
320 XSelectInput(display, win, 0);
321 }
322
323 /*
324 test_sync_grab(display, win);
325 */
326
327 while(1)
328 {
329 XIEvent ev;
330 XNextEvent(display, (XEvent*)&ev);
331 if (ev.type == GenericEvent)
332 {
333 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
334
335 printf("EVENT type %d\n", event->evtype);
336 switch (event->evtype)
337 {
338 case XI_DeviceChanged:
339 print_devicechangedevent(display,
340 (XIDeviceChangedEvent*)event);
341 break;
342 case XI_HierarchyChanged:
343 print_hierarchychangedevent((XIHierarchyEvent*)event);
344 break;
345 case XI_RawEvent:
346 print_rawevent((XIRawEvent*)event);
347 break;
348 case XI_Enter:
349 case XI_Leave:
350 case XI_FocusIn:
351 case XI_FocusOut:
352 print_enterleave((XILeaveEvent*)event);
353 break;
354 case XI_PropertyEvent:
355 print_propertyevent(display, (XIPropertyEvent*)event);
356 break;
357 default:
358 print_deviceevent(event);
359 break;
360 }
361 }
362
363 XIFreeEventData(&ev);
364 }
365
366 XDestroyWindow(display, win);
367
368 return EXIT_SUCCESS;
369 }