]>
diplodocus.org Git - xorg-xinput/blob - src/test_xi2.c
402acacf36b7eb11a8d6db96c4dbab59fd24de56
2 * Copyright © 2009 Red Hat, Inc.
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:
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
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.
29 extern void print_classes_xi2(Display
*, XIAnyClassInfo
**classes
,
32 static Window
create_win(Display
*dpy
)
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,
39 XMapWindow(dpy
, subwindow
);
40 XSelectInput(dpy
, win
, ExposureMask
);
44 static void print_deviceevent(XIDeviceEvent
* event
)
49 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
50 printf(" detail: %d\n", event
->detail
);
51 switch(event
->evtype
) {
54 printf(" flags: %s\n", (event
->flags
& XIKeyRepeat
) ? "repeat" : "");
58 case XI_ButtonRelease
:
60 printf(" flags: %s\n", (event
->flags
& XIPointerEmulated
) ? "emulated" : "");
67 printf(" flags:%s%s\n",
68 (event
->flags
& XITouchPendingEnd
) ? " pending_end" : "",
69 (event
->flags
& XITouchEmulatingPointer
) ? " emulating" : "");
74 printf(" root: %.2f/%.2f\n", event
->root_x
, event
->root_y
);
75 printf(" event: %.2f/%.2f\n", event
->event_x
, event
->event_y
);
78 for (i
= 0; i
< event
->buttons
.mask_len
* 8; i
++)
79 if (XIMaskIsSet(event
->buttons
.mask
, i
))
83 printf(" modifiers: locked %#x latched %#x base %#x effective: %#x\n",
84 event
->mods
.locked
, event
->mods
.latched
,
85 event
->mods
.base
, event
->mods
.effective
);
86 printf(" group: locked %#x latched %#x base %#x effective: %#x\n",
87 event
->group
.locked
, event
->group
.latched
,
88 event
->group
.base
, event
->group
.effective
);
89 printf(" valuators:\n");
91 val
= event
->valuators
.values
;
92 for (i
= 0; i
< event
->valuators
.mask_len
* 8; i
++)
93 if (XIMaskIsSet(event
->valuators
.mask
, i
))
94 printf(" %i: %.2f\n", i
, *val
++);
96 printf(" windows: root 0x%lx event 0x%lx child 0x%lx\n",
97 event
->root
, event
->event
, event
->child
);
100 static void print_devicechangedevent(Display
*dpy
, XIDeviceChangedEvent
*event
)
102 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
103 printf(" reason: %s\n", (event
->reason
== XISlaveSwitch
) ? "SlaveSwitch" :
105 print_classes_xi2(dpy
, event
->classes
, event
->num_classes
);
108 static void print_hierarchychangedevent(XIHierarchyEvent
*event
)
111 printf(" Changes happened: %s %s %s %s %s %s %s %s\n",
112 (event
->flags
& XIMasterAdded
) ? "[new master]" : "",
113 (event
->flags
& XIMasterRemoved
) ? "[master removed]" : "",
114 (event
->flags
& XISlaveAdded
) ? "[new slave]" : "",
115 (event
->flags
& XISlaveRemoved
) ? "[slave removed]" : "",
116 (event
->flags
& XISlaveAttached
) ? "[slave attached]" : "",
117 (event
->flags
& XISlaveDetached
) ? "[slave detached]" : "",
118 (event
->flags
& XIDeviceEnabled
) ? "[device enabled]" : "",
119 (event
->flags
& XIDeviceDisabled
) ? "[device disabled]" : "");
121 for (i
= 0; i
< event
->num_info
; i
++)
123 char *use
= "<undefined>";
124 switch(event
->info
[i
].use
)
126 case XIMasterPointer
: use
= "master pointer"; break;
127 case XIMasterKeyboard
: use
= "master keyboard"; break;
128 case XISlavePointer
: use
= "slave pointer"; break;
129 case XISlaveKeyboard
: use
= "slave keyboard"; break;
130 case XIFloatingSlave
: use
= "floating slave"; break;
134 printf(" device %d [%s (%d)] is %s\n",
135 event
->info
[i
].deviceid
,
137 event
->info
[i
].attachment
,
138 (event
->info
[i
].enabled
) ? "enabled" : "disabled");
139 if (event
->info
[i
].flags
)
141 printf(" changes: %s %s %s %s %s %s %s %s\n",
142 (event
->info
[i
].flags
& XIMasterAdded
) ? "[new master]" : "",
143 (event
->info
[i
].flags
& XIMasterRemoved
) ? "[master removed]" : "",
144 (event
->info
[i
].flags
& XISlaveAdded
) ? "[new slave]" : "",
145 (event
->info
[i
].flags
& XISlaveRemoved
) ? "[slave removed]" : "",
146 (event
->info
[i
].flags
& XISlaveAttached
) ? "[slave attached]" : "",
147 (event
->info
[i
].flags
& XISlaveDetached
) ? "[slave detached]" : "",
148 (event
->info
[i
].flags
& XIDeviceEnabled
) ? "[device enabled]" : "",
149 (event
->info
[i
].flags
& XIDeviceDisabled
) ? "[device disabled]" : "");
154 static void print_rawevent(XIRawEvent
*event
)
157 double *val
, *raw_val
;
159 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
160 printf(" detail: %d\n", event
->detail
);
162 switch(event
->evtype
) {
163 case XI_RawButtonPress
:
164 case XI_RawButtonRelease
:
166 printf(" flags: %s\n", (event
->flags
& XIPointerEmulated
) ? "emulated" : "");
171 printf(" valuators:\n");
172 val
= event
->valuators
.values
;
173 raw_val
= event
->raw_values
;
174 for (i
= 0; i
< event
->valuators
.mask_len
* 8; i
++)
175 if (XIMaskIsSet(event
->valuators
.mask
, i
))
176 printf(" %2d: %.2f (%.2f)\n", i
, *val
++, *raw_val
++);
180 static void print_enterleave(XILeaveEvent
* event
)
182 char *mode
= "<undefined>",
183 *detail
= "<undefined>";
186 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
187 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
188 event
->root
, event
->event
, event
->child
);
191 case XINotifyNormal
: mode
= "NotifyNormal"; break;
192 case XINotifyGrab
: mode
= "NotifyGrab"; break;
193 case XINotifyUngrab
: mode
= "NotifyUngrab"; break;
194 case XINotifyWhileGrabbed
: mode
= "NotifyWhileGrabbed"; break;
195 case XINotifyPassiveGrab
: mode
= "NotifyPassiveGrab"; break;
196 case XINotifyPassiveUngrab
:mode
= "NotifyPassiveUngrab"; break;
198 switch (event
->detail
)
200 case XINotifyAncestor
: detail
= "NotifyAncestor"; break;
201 case XINotifyVirtual
: detail
= "NotifyVirtual"; break;
202 case XINotifyInferior
: detail
= "NotifyInferior"; break;
203 case XINotifyNonlinear
: detail
= "NotifyNonlinear"; break;
204 case XINotifyNonlinearVirtual
: detail
= "NotifyNonlinearVirtual"; break;
205 case XINotifyPointer
: detail
= "NotifyPointer"; break;
206 case XINotifyPointerRoot
: detail
= "NotifyPointerRoot"; break;
207 case XINotifyDetailNone
: detail
= "NotifyDetailNone"; break;
209 printf(" mode: %s (detail %s)\n", mode
, detail
);
210 printf(" flags: %s %s\n", event
->focus
? "[focus]" : "",
211 event
->same_screen
? "[same screen]" : "");
213 for (i
= 0; i
< event
->buttons
.mask_len
* 8; i
++)
214 if (XIMaskIsSet(event
->buttons
.mask
, i
))
218 printf(" modifiers: locked %#x latched %#x base %#x effective: %#x\n",
219 event
->mods
.locked
, event
->mods
.latched
,
220 event
->mods
.base
, event
->mods
.effective
);
221 printf(" group: locked %#x latched %#x base %#x effective: %#x\n",
222 event
->group
.locked
, event
->group
.latched
,
223 event
->group
.base
, event
->group
.effective
);
225 printf(" root x/y: %.2f / %.2f\n", event
->root_x
, event
->root_y
);
226 printf(" event x/y: %.2f / %.2f\n", event
->event_x
, event
->event_y
);
230 static void print_propertyevent(Display
*display
, XIPropertyEvent
* event
)
235 if (event
->what
== XIPropertyDeleted
)
237 else if (event
->what
== XIPropertyCreated
)
240 changed
= "modified";
241 name
= XGetAtomName(display
, event
->property
);
242 printf(" property: %ld '%s'\n", event
->property
, name
);
243 printf(" changed: %s\n", changed
);
248 test_sync_grab(Display
*display
, Window win
)
254 /* Select for motion events */
255 mask
.deviceid
= XIAllDevices
;
257 mask
.mask
= calloc(2, sizeof(char));
258 XISetMask(mask
.mask
, XI_ButtonPress
);
260 if ((rc
= XIGrabDevice(display
, 2, win
, CurrentTime
, None
, GrabModeSync
,
261 GrabModeAsync
, False
, &mask
)) != GrabSuccess
)
263 fprintf(stderr
, "Grab failed with %d\n", rc
);
268 XSync(display
, True
);
269 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
272 printf("Holding sync grab for %d button presses.\n", loop
);
278 XNextEvent(display
, (XEvent
*)&ev
);
279 if (ev
.type
== GenericEvent
&& ev
.extension
== xi_opcode
)
281 XIDeviceEvent
*event
= (XIDeviceEvent
*)&ev
;
282 print_deviceevent(event
);
283 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
287 XIUngrabDevice(display
, 2, CurrentTime
);
291 static const char* type_to_name(int evtype
)
296 case XI_DeviceChanged
: name
= "DeviceChanged"; break;
297 case XI_KeyPress
: name
= "KeyPress"; break;
298 case XI_KeyRelease
: name
= "KeyRelease"; break;
299 case XI_ButtonPress
: name
= "ButtonPress"; break;
300 case XI_ButtonRelease
: name
= "ButtonRelease"; break;
301 case XI_Motion
: name
= "Motion"; break;
302 case XI_Enter
: name
= "Enter"; break;
303 case XI_Leave
: name
= "Leave"; break;
304 case XI_FocusIn
: name
= "FocusIn"; break;
305 case XI_FocusOut
: name
= "FocusOut"; break;
306 case XI_HierarchyChanged
: name
= "HierarchyChanged"; break;
307 case XI_PropertyEvent
: name
= "PropertyEvent"; break;
308 case XI_RawKeyPress
: name
= "RawKeyPress"; break;
309 case XI_RawKeyRelease
: name
= "RawKeyRelease"; break;
310 case XI_RawButtonPress
: name
= "RawButtonPress"; break;
311 case XI_RawButtonRelease
: name
= "RawButtonRelease"; break;
312 case XI_RawMotion
: name
= "RawMotion"; break;
313 case XI_TouchBegin
: name
= "TouchBegin"; break;
314 case XI_TouchUpdate
: name
= "TouchUpdate"; break;
315 case XI_TouchEnd
: name
= "TouchEnd"; break;
316 case XI_RawTouchBegin
: name
= "RawTouchBegin"; break;
317 case XI_RawTouchUpdate
: name
= "RawTouchUpdate"; break;
318 case XI_RawTouchEnd
: name
= "RawTouchEnd"; break;
320 name
= "unknown event type"; break;
327 test_xi2(Display
*display
,
340 setvbuf(stdout
, NULL
, _IOLBF
, 0);
342 if (argc
>= 1 && strcmp(argv
[0], "--root") == 0) {
349 rc
= list(display
, argc
, argv
, name
, desc
);
350 if (rc
!= EXIT_SUCCESS
)
354 win
= DefaultRootWindow(display
);
356 win
= create_win(display
);
360 info
= xi2_find_device_info(display
, argv
[0]);
361 deviceid
= info
->deviceid
;
364 /* Select for motion events */
366 m
->deviceid
= (deviceid
== -1) ? XIAllDevices
: deviceid
;
367 m
->mask_len
= XIMaskLen(XI_LASTEVENT
);
368 m
->mask
= calloc(m
->mask_len
, sizeof(char));
369 XISetMask(m
->mask
, XI_ButtonPress
);
370 XISetMask(m
->mask
, XI_ButtonRelease
);
371 XISetMask(m
->mask
, XI_KeyPress
);
372 XISetMask(m
->mask
, XI_KeyRelease
);
373 XISetMask(m
->mask
, XI_Motion
);
374 XISetMask(m
->mask
, XI_DeviceChanged
);
375 XISetMask(m
->mask
, XI_Enter
);
376 XISetMask(m
->mask
, XI_Leave
);
377 XISetMask(m
->mask
, XI_FocusIn
);
378 XISetMask(m
->mask
, XI_FocusOut
);
380 XISetMask(m
->mask
, XI_TouchBegin
);
381 XISetMask(m
->mask
, XI_TouchUpdate
);
382 XISetMask(m
->mask
, XI_TouchEnd
);
384 if (m
->deviceid
== XIAllDevices
)
385 XISetMask(m
->mask
, XI_HierarchyChanged
);
386 XISetMask(m
->mask
, XI_PropertyEvent
);
389 m
->deviceid
= (deviceid
== -1) ? XIAllMasterDevices
: deviceid
;
390 m
->mask_len
= XIMaskLen(XI_LASTEVENT
);
391 m
->mask
= calloc(m
->mask_len
, sizeof(char));
392 XISetMask(m
->mask
, XI_RawKeyPress
);
393 XISetMask(m
->mask
, XI_RawKeyRelease
);
394 XISetMask(m
->mask
, XI_RawButtonPress
);
395 XISetMask(m
->mask
, XI_RawButtonRelease
);
396 XISetMask(m
->mask
, XI_RawMotion
);
398 XISetMask(m
->mask
, XI_RawTouchBegin
);
399 XISetMask(m
->mask
, XI_RawTouchUpdate
);
400 XISetMask(m
->mask
, XI_RawTouchEnd
);
403 XISelectEvents(display
, win
, &mask
[0], use_root
? 2 : 1);
405 XISelectEvents(display
, DefaultRootWindow(display
), &mask
[1], 1);
406 XMapWindow(display
, win
);
408 XSync(display
, False
);
415 XMaskEvent(display
, ExposureMask
, &event
);
416 XSelectInput(display
, win
, 0);
420 test_sync_grab(display, win);
426 XGenericEventCookie
*cookie
= (XGenericEventCookie
*)&ev
.xcookie
;
427 XNextEvent(display
, (XEvent
*)&ev
);
429 if (XGetEventData(display
, cookie
) &&
430 cookie
->type
== GenericEvent
&&
431 cookie
->extension
== xi_opcode
)
433 printf("EVENT type %d (%s)\n", cookie
->evtype
, type_to_name(cookie
->evtype
));
434 switch (cookie
->evtype
)
436 case XI_DeviceChanged
:
437 print_devicechangedevent(display
, cookie
->data
);
439 case XI_HierarchyChanged
:
440 print_hierarchychangedevent(cookie
->data
);
443 case XI_RawKeyRelease
:
444 case XI_RawButtonPress
:
445 case XI_RawButtonRelease
:
447 case XI_RawTouchBegin
:
448 case XI_RawTouchUpdate
:
450 print_rawevent(cookie
->data
);
456 print_enterleave(cookie
->data
);
458 case XI_PropertyEvent
:
459 print_propertyevent(display
, cookie
->data
);
462 print_deviceevent(cookie
->data
);
467 XFreeEventData(display
, cookie
);
470 XDestroyWindow(display
, win
);