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 XSelectInput(dpy
, win
, ExposureMask
);
40 XMapWindow(dpy
, subwindow
);
46 static void print_deviceevent(XIDeviceEvent
* event
)
51 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
52 printf(" detail: %d\n", event
->detail
);
54 printf(" root: %.2f/%.2f\n", event
->root_x
, event
->root_y
);
55 printf(" event: %.2f/%.2f\n", event
->event_x
, event
->event_x
);
58 for (i
= 0; i
< event
->buttons
->mask_len
* 8; i
++)
59 if (XIMaskIsSet(event
->buttons
->mask
, i
))
63 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
64 event
->mods
->locked
, event
->mods
->latched
,
66 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
67 event
->group
->locked
, event
->group
->latched
,
69 printf(" valuators:");
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
++);
77 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
78 event
->root
, event
->event
, event
->child
);
81 static void print_devicechangedevent(Display
*dpy
, XIDeviceChangedEvent
*event
)
83 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
84 printf(" reason: %s\n", (event
->reason
== XISlaveSwitch
) ? "SlaveSwitch" :
86 print_classes_xi2(dpy
, event
->classes
, event
->num_classes
);
89 static void print_hierarchychangedevent(XIHierarchyEvent
*event
)
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]" : "");
102 for (i
= 0; i
< event
->num_info
; i
++)
105 switch(event
->info
[i
].use
)
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;
115 printf(" device %d [%s (%d)] is %s\n",
116 event
->info
[i
].deviceid
,
118 event
->info
[i
].attachment
,
119 (event
->info
[i
].enabled
) ? "enabled" : "disabled");
120 if (event
->info
[i
].flags
)
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]" : "");
135 static void print_rawevent(XIRawEvent
*event
)
138 double *val
, *raw_val
;
140 printf(" device: %d\n", event
->deviceid
);
141 printf(" detail: %d\n", event
->detail
);
142 printf(" valuators:\n");
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
++);
152 static void print_enterleave(XILeaveEvent
* event
)
157 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
158 event
->root
, event
->event
, event
->child
);
161 case NotifyNormal
: mode
= "NotifyNormal"; break;
162 case NotifyGrab
: mode
= "NotifyGrab"; break;
163 case NotifyUngrab
: mode
= "NotifyUngrab"; break;
164 case NotifyWhileGrabbed
: mode
= "NotifyWhileGrabbed"; break;
166 switch (event
->detail
)
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;
177 printf(" mode: %s (detail %s)\n", mode
, detail
);
178 printf(" flags: %s %s\n", event
->focus
? "[focus]" : "",
179 event
->same_screen
? "[same screen]" : "");
181 for (i
= 0; i
< event
->buttons
->mask_len
* 8; i
++)
182 if (XIMaskIsSet(event
->buttons
->mask
, i
))
186 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
187 event
->mods
->locked
, event
->mods
->latched
,
189 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
190 event
->group
->locked
, event
->group
->latched
,
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
);
198 static void print_propertyevent(Display
*display
, XIPropertyEvent
* event
)
202 if (event
->what
== XIPropertyDeleted
)
204 else if (event
->what
== XIPropertyCreated
)
207 changed
= "modified";
209 printf(" property: %ld '%s'\n", event
->property
, XGetAtomName(display
, event
->property
));
210 printf(" changed: %s\n", changed
);
214 test_sync_grab(Display
*display
, Window win
)
220 /* Select for motion events */
221 mask
.deviceid
= XIAllDevices
;
223 mask
.mask
= calloc(2, sizeof(char));
224 XISetMask(mask
.mask
, XI_ButtonPress
);
226 if ((rc
= XIGrabDevice(display
, 2, win
, CurrentTime
, None
, GrabModeSync
,
227 GrabModeAsync
, False
, &mask
)) != GrabSuccess
)
229 fprintf(stderr
, "Grab failed with %d\n", rc
);
234 XSync(display
, True
);
235 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
238 printf("Holding sync grab for %d button presses.\n", loop
);
244 XNextEvent(display
, (XEvent
*)&ev
);
245 if (ev
.type
== GenericEvent
)
247 XIDeviceEvent
*event
= (XIDeviceEvent
*)&ev
;
248 print_deviceevent(event
);
249 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
250 XIFreeEventData(&ev
);
254 XIUngrabDevice(display
, 2, CurrentTime
);
259 test_xi2(Display
*display
,
268 list(display
, argc
, argv
, name
, desc
);
269 win
= create_win(display
);
271 XSync(display
, False
);
273 /* Select for motion events */
274 mask
.deviceid
= XIAllDevices
;
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
);
293 XIGrabModifiers modifiers
[] = {{0, 0}, {0, 0x10}, {0, 0x1}, {0, 0x11}};
294 int nmods
= sizeof(modifiers
)/sizeof(modifiers
[0]);
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]);
310 mask
.deviceid
= XIAllMasterDevices
;
311 memset(mask
.mask
, 0, 2);
312 XISetMask(mask
.mask
, XI_RawEvent
);
313 XISelectEvents(display
, DefaultRootWindow(display
), &mask
, 1);
319 XMaskEvent(display
, ExposureMask
, &event
);
320 XSelectInput(display
, win
, 0);
324 test_sync_grab(display, win);
330 XNextEvent(display
, (XEvent
*)&ev
);
331 if (ev
.type
== GenericEvent
)
333 XIDeviceEvent
*event
= (XIDeviceEvent
*)&ev
;
335 printf("EVENT type %d\n", event
->evtype
);
336 switch (event
->evtype
)
338 case XI_DeviceChanged
:
339 print_devicechangedevent(display
,
340 (XIDeviceChangedEvent
*)event
);
342 case XI_HierarchyChanged
:
343 print_hierarchychangedevent((XIHierarchyEvent
*)event
);
346 print_rawevent((XIRawEvent
*)event
);
352 print_enterleave((XILeaveEvent
*)event
);
354 case XI_PropertyEvent
:
355 print_propertyevent(display
, (XIPropertyEvent
*)event
);
358 print_deviceevent(event
);
363 XIFreeEventData(&ev
);
366 XDestroyWindow(display
, win
);