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
);
52 printf(" root: %.2f/%.2f\n", event
->root_x
, event
->root_y
);
53 printf(" event: %.2f/%.2f\n", event
->event_x
, event
->event_y
);
56 for (i
= 0; i
< event
->buttons
.mask_len
* 8; i
++)
57 if (XIMaskIsSet(event
->buttons
.mask
, i
))
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:");
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
++);
75 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
76 event
->root
, event
->event
, event
->child
);
79 static void print_devicechangedevent(Display
*dpy
, XIDeviceChangedEvent
*event
)
81 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
82 printf(" reason: %s\n", (event
->reason
== XISlaveSwitch
) ? "SlaveSwitch" :
84 print_classes_xi2(dpy
, event
->classes
, event
->num_classes
);
87 static void print_hierarchychangedevent(XIHierarchyEvent
*event
)
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]" : "");
100 for (i
= 0; i
< event
->num_info
; i
++)
103 switch(event
->info
[i
].use
)
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;
113 printf(" device %d [%s (%d)] is %s\n",
114 event
->info
[i
].deviceid
,
116 event
->info
[i
].attachment
,
117 (event
->info
[i
].enabled
) ? "enabled" : "disabled");
118 if (event
->info
[i
].flags
)
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]" : "");
133 static void print_rawevent(XIRawEvent
*event
)
136 double *val
, *raw_val
;
138 printf(" device: %d\n", event
->deviceid
);
139 printf(" detail: %d\n", event
->detail
);
140 printf(" valuators:\n");
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
++);
150 static void print_enterleave(XILeaveEvent
* event
)
155 printf(" device: %d\n", event
->deviceid
);
156 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
157 event
->root
, event
->event
, event
->child
);
160 case XINotifyNormal
: mode
= "NotifyNormal"; break;
161 case XINotifyGrab
: mode
= "NotifyGrab"; break;
162 case XINotifyUngrab
: mode
= "NotifyUngrab"; break;
163 case XINotifyWhileGrabbed
: mode
= "NotifyWhileGrabbed"; break;
164 case XINotifyPassiveGrab
: mode
= "NotifyPassiveGrab"; break;
165 case XINotifyPassiveUngrab
:mode
= "NotifyPassiveUngrab"; break;
167 switch (event
->detail
)
169 case XINotifyAncestor
: detail
= "NotifyAncestor"; break;
170 case XINotifyVirtual
: detail
= "NotifyVirtual"; break;
171 case XINotifyInferior
: detail
= "NotifyInferior"; break;
172 case XINotifyNonlinear
: detail
= "NotifyNonlinear"; break;
173 case XINotifyNonlinearVirtual
: detail
= "NotifyNonlinearVirtual"; break;
174 case XINotifyPointer
: detail
= "NotifyPointer"; break;
175 case XINotifyPointerRoot
: detail
= "NotifyPointerRoot"; break;
176 case XINotifyDetailNone
: detail
= "NotifyDetailNone"; break;
178 printf(" mode: %s (detail %s)\n", mode
, detail
);
179 printf(" flags: %s %s\n", event
->focus
? "[focus]" : "",
180 event
->same_screen
? "[same screen]" : "");
182 for (i
= 0; i
< event
->buttons
.mask_len
* 8; i
++)
183 if (XIMaskIsSet(event
->buttons
.mask
, i
))
187 printf(" modifiers: locked %#x latched %#x base %#x effective: %#x\n",
188 event
->mods
.locked
, event
->mods
.latched
,
189 event
->mods
.base
, event
->mods
.effective
);
190 printf(" group: locked %#x latched %#x base %#x effective: %#x\n",
191 event
->group
.locked
, event
->group
.latched
,
192 event
->group
.base
, event
->group
.effective
);
194 printf(" root x/y: %.2f / %.2f\n", event
->root_x
, event
->root_y
);
195 printf(" event x/y: %.2f / %.2f\n", event
->event_x
, event
->event_y
);
199 static void print_propertyevent(Display
*display
, XIPropertyEvent
* event
)
204 if (event
->what
== XIPropertyDeleted
)
206 else if (event
->what
== XIPropertyCreated
)
209 changed
= "modified";
210 name
= XGetAtomName(display
, event
->property
);
211 printf(" property: %ld '%s'\n", event
->property
, name
);
212 printf(" changed: %s\n", changed
);
217 test_sync_grab(Display
*display
, Window win
)
223 /* Select for motion events */
224 mask
.deviceid
= XIAllDevices
;
226 mask
.mask
= calloc(2, sizeof(char));
227 XISetMask(mask
.mask
, XI_ButtonPress
);
229 if ((rc
= XIGrabDevice(display
, 2, win
, CurrentTime
, None
, GrabModeSync
,
230 GrabModeAsync
, False
, &mask
)) != GrabSuccess
)
232 fprintf(stderr
, "Grab failed with %d\n", rc
);
237 XSync(display
, True
);
238 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
241 printf("Holding sync grab for %d button presses.\n", loop
);
247 XNextEvent(display
, (XEvent
*)&ev
);
248 if (ev
.type
== GenericEvent
&& ev
.extension
== xi_opcode
)
250 XIDeviceEvent
*event
= (XIDeviceEvent
*)&ev
;
251 print_deviceevent(event
);
252 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
256 XIUngrabDevice(display
, 2, CurrentTime
);
261 test_xi2(Display
*display
,
270 list(display
, argc
, argv
, name
, desc
);
271 win
= create_win(display
);
273 /* Select for motion events */
274 mask
.deviceid
= XIAllDevices
;
275 mask
.mask_len
= XIMaskLen(XI_RawMotion
);
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 XMapWindow(display
, win
);
291 XSync(display
, False
);
294 XIGrabModifiers modifiers
[] = {{0, 0}, {0, 0x10}, {0, 0x1}, {0, 0x11}};
295 int nmods
= sizeof(modifiers
)/sizeof(modifiers
[0]);
298 memset(mask
.mask
, 0, 2);
299 XISetMask(mask
.mask
, XI_KeyPress
);
300 XISetMask(mask
.mask
, XI_KeyRelease
);
301 XISetMask(mask
.mask
, XI_ButtonPress
);
302 XISetMask(mask
.mask
, XI_ButtonRelease
);
303 XISetMask(mask
.mask
, XI_Motion
);
304 XIGrabButton(display
, 2, 1, win
, None
, GrabModeAsync
, GrabModeAsync
,
305 False
, &mask
, nmods
, modifiers
);
306 XIGrabKeysym(display
, 3, 0x71, win
, GrabModeAsync
, GrabModeAsync
,
307 False
, &mask
, nmods
, modifiers
);
308 XIUngrabButton(display
, 3, 1, win
, nmods
- 2, &modifiers
[2]);
309 XIUngrabKeysym(display
, 3, 0x71, win
, nmods
- 2, &modifiers
[2]);
312 mask
.deviceid
= XIAllMasterDevices
;
313 memset(mask
.mask
, 0, 2);
314 XISetMask(mask
.mask
, XI_RawKeyPress
);
315 XISetMask(mask
.mask
, XI_RawKeyRelease
);
316 XISetMask(mask
.mask
, XI_RawButtonPress
);
317 XISetMask(mask
.mask
, XI_RawButtonRelease
);
318 XISetMask(mask
.mask
, XI_RawMotion
);
319 XISelectEvents(display
, DefaultRootWindow(display
), &mask
, 1);
325 XMaskEvent(display
, ExposureMask
, &event
);
326 XSelectInput(display
, win
, 0);
330 test_sync_grab(display, win);
336 XGenericEventCookie
*cookie
= (XGenericEventCookie
*)&ev
.xcookie
;
337 XNextEvent(display
, (XEvent
*)&ev
);
339 if (XGetEventData(display
, cookie
) &&
340 cookie
->type
== GenericEvent
&&
341 cookie
->extension
== xi_opcode
)
343 printf("EVENT type %d\n", cookie
->evtype
);
344 switch (cookie
->evtype
)
346 case XI_DeviceChanged
:
347 print_devicechangedevent(display
, cookie
->data
);
349 case XI_HierarchyChanged
:
350 print_hierarchychangedevent(cookie
->data
);
353 case XI_RawKeyRelease
:
354 case XI_RawButtonPress
:
355 case XI_RawButtonRelease
:
357 print_rawevent(cookie
->data
);
363 print_enterleave(cookie
->data
);
365 case XI_PropertyEvent
:
366 print_propertyevent(display
, cookie
->data
);
369 print_deviceevent(cookie
->data
);
374 XFreeEventData(display
, cookie
);
377 XDestroyWindow(display
, win
);