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