]>
diplodocus.org Git - xorg-xinput/blob - src/test_xi2.c
b9ac1cc7e7423229091ea59fc811b497206f7adb
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 #define BitIsOn(ptr, bit) (((unsigned char *) (ptr))[(bit)>>3] & (1 << ((bit) & 7)))
33 #define SetBit(ptr, bit) (((unsigned char *) (ptr))[(bit)>>3] |= (1 << ((bit) & 7)))
35 static Window
create_win(Display
*dpy
)
37 Window win
= XCreateSimpleWindow(dpy
, DefaultRootWindow(dpy
), 0, 0, 200,
38 200, 0, 0, WhitePixel(dpy
, 0));
39 Window subwindow
= XCreateSimpleWindow(dpy
, win
, 50, 50, 50, 50, 0, 0,
42 XSelectInput(dpy
, win
, ExposureMask
);
43 XMapWindow(dpy
, subwindow
);
49 static void print_deviceevent(XIDeviceEvent
* event
)
54 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
55 printf(" detail: %d\n", event
->detail
);
57 printf(" root: %.2f/%.2f\n", event
->root_x
, event
->root_y
);
58 printf(" event: %.2f/%.2f\n", event
->event_x
, event
->event_x
);
61 for (i
= 0; i
< event
->buttons
->mask_len
* 8; i
++)
62 if (BitIsOn(event
->buttons
->mask
, i
))
66 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
67 event
->mods
->locked
, event
->mods
->latched
,
69 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
70 event
->group
->locked
, event
->group
->latched
,
72 printf(" valuators:");
74 val
= event
->valuators
->values
;
75 for (i
= 0; i
< event
->valuators
->mask_len
* 8; i
++)
76 if (BitIsOn(event
->valuators
->mask
, i
))
77 printf(" %.2f", *val
++);
80 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
81 event
->root
, event
->event
, event
->child
);
84 static void print_devicechangedevent(Display
*dpy
, XIDeviceChangedEvent
*event
)
86 printf(" device: %d (%d)\n", event
->deviceid
, event
->sourceid
);
87 printf(" reason: %s\n", (event
->reason
== SlaveSwitch
) ? "SlaveSwitch" :
89 print_classes_xi2(dpy
, event
->classes
, event
->num_classes
);
92 static void print_hierarchychangedevent(XIDeviceHierarchyEvent
*event
)
95 printf(" Changes happened: %s %s %s %s %s %s %s\n",
96 (event
->flags
& HF_MasterAdded
) ? "[new master]" : "",
97 (event
->flags
& HF_MasterRemoved
) ? "[master removed]" : "",
98 (event
->flags
& HF_SlaveAdded
) ? "[new slave]" : "",
99 (event
->flags
& HF_SlaveRemoved
) ? "[slave removed]" : "",
100 (event
->flags
& HF_SlaveAttached
) ? "[slave attached]" : "",
101 (event
->flags
& HF_DeviceEnabled
) ? "[device enabled]" : "",
102 (event
->flags
& HF_DeviceDisabled
) ? "[device disabled]" : "");
104 for (i
= 0; i
< event
->num_devices
; i
++)
107 switch(event
->info
[i
].use
)
109 case MasterPointer
: use
= "master pointer";
110 case MasterKeyboard
: use
= "master keyboard"; break;
111 case SlavePointer
: use
= "slave pointer";
112 case SlaveKeyboard
: use
= "slave keyboard"; break;
113 case FloatingSlave
: use
= "floating slave"; break;
117 printf(" device %d [%s (%d)] is %s\n",
118 event
->info
[i
].deviceid
,
120 event
->info
[i
].attachment
,
121 (event
->info
[i
].enabled
) ? "enabled" : "disabled");
125 static void print_rawevent(XIRawDeviceEvent
*event
)
128 double *val
, *raw_val
;
130 printf(" device: %d\n", event
->deviceid
);
131 printf(" detail: %d\n", event
->detail
);
132 printf(" valuators:\n");
134 val
= event
->valuators
->values
;
135 raw_val
= event
->raw_values
;
136 for (i
= 0; i
< event
->valuators
->mask_len
* 8; i
++)
137 if (BitIsOn(event
->valuators
->mask
, i
))
138 printf(" %2d: %.2f (%.2f)\n", i
, *val
++, *raw_val
++);
142 static void print_enterleave(XILeaveEvent
* event
)
147 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
148 event
->root
, event
->event
, event
->child
);
151 case NotifyNormal
: mode
= "NotifyNormal"; break;
152 case NotifyGrab
: mode
= "NotifyGrab"; break;
153 case NotifyUngrab
: mode
= "NotifyUngrab"; break;
154 case NotifyWhileGrabbed
: mode
= "NotifyWhileGrabbed"; break;
156 switch (event
->detail
)
158 case NotifyAncestor
: detail
= "NotifyAncestor"; break;
159 case NotifyVirtual
: detail
= "NotifyVirtual"; break;
160 case NotifyInferior
: detail
= "NotifyInferior"; break;
161 case NotifyNonlinear
: detail
= "NotifyNonlinear"; break;
162 case NotifyNonlinearVirtual
: detail
= "NotifyNonlinearVirtual"; break;
163 case NotifyPointer
: detail
= "NotifyPointer"; break;
164 case NotifyPointerRoot
: detail
= "NotifyPointerRoot"; break;
165 case NotifyDetailNone
: detail
= "NotifyDetailNone"; break;
167 printf(" mode: %s (detail %s)\n", mode
, detail
);
168 printf(" flags: %s %s\n", event
->focus
? "[focus]" : "",
169 event
->same_screen
? "[same screen]" : "");
171 for (i
= 0; i
< event
->buttons
->mask_len
* 8; i
++)
172 if (BitIsOn(event
->buttons
->mask
, i
))
176 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
177 event
->mods
->locked
, event
->mods
->latched
,
179 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
180 event
->group
->locked
, event
->group
->latched
,
183 printf(" root x/y: %.2f / %.2f\n", event
->root_x
, event
->root_y
);
184 printf(" event x/y: %.2f / %.2f\n", event
->event_x
, event
->event_y
);
188 static void print_propertyevent(Display
*display
, XIPropertyEvent
* event
)
192 if (event
->what
== XIPropertyDeleted
)
194 else if (event
->what
== XIPropertyCreated
)
197 changed
= "modified";
199 printf(" property: %ld '%s'\n", event
->property
, XGetAtomName(display
, event
->property
));
200 printf(" changed: %s\n", changed
);
204 test_sync_grab(Display
*display
, Window win
)
208 XIDeviceEventMask mask
;
210 /* Select for motion events */
211 mask
.deviceid
= AllDevices
;
213 mask
.mask
= calloc(2, sizeof(char));
214 SetBit(mask
.mask
, XI_ButtonPress
);
216 if ((rc
= XIGrabDevice(display
, 2, win
, CurrentTime
, None
, GrabModeSync
,
217 GrabModeAsync
, False
, &mask
)) != GrabSuccess
)
219 fprintf(stderr
, "Grab failed with %d\n", rc
);
224 XSync(display
, True
);
225 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
228 printf("Holding sync grab for %d button presses.\n", loop
);
234 XNextEvent(display
, (XEvent
*)&ev
);
235 if (ev
.type
== GenericEvent
)
237 XIDeviceEvent
*event
= (XIDeviceEvent
*)&ev
;
238 print_deviceevent(event
);
239 XIAllowEvents(display
, 2, SyncPointer
, CurrentTime
);
240 XIFreeEventData(&ev
);
244 XIUngrabDevice(display
, 2, CurrentTime
);
249 test_xi2(Display
*display
,
255 XIDeviceEventMask mask
;
258 list(display
, argc
, argv
, name
, desc
);
259 win
= create_win(display
);
261 XSync(display
, False
);
263 /* Select for motion events */
264 mask
.deviceid
= AllDevices
;
266 mask
.mask
= calloc(mask
.mask_len
, sizeof(char));
267 SetBit(mask
.mask
, XI_ButtonPress
);
268 SetBit(mask
.mask
, XI_ButtonRelease
);
269 SetBit(mask
.mask
, XI_KeyPress
);
270 SetBit(mask
.mask
, XI_KeyRelease
);
271 SetBit(mask
.mask
, XI_Motion
);
272 SetBit(mask
.mask
, XI_DeviceChanged
);
273 SetBit(mask
.mask
, XI_Enter
);
274 SetBit(mask
.mask
, XI_Leave
);
275 SetBit(mask
.mask
, XI_FocusIn
);
276 SetBit(mask
.mask
, XI_FocusOut
);
277 SetBit(mask
.mask
, XI_HierarchyChanged
);
278 SetBit(mask
.mask
, XI_PropertyEvent
);
279 XISelectEvent(display
, win
, &mask
, 1);
280 XSync(display
, False
);
283 int modifiers
[] = {0, 0x10, 0x1, 0x11};
284 int nmods
= sizeof(modifiers
)/sizeof(modifiers
[0]);
287 memset(mask
.mask
, 0, 2);
288 SetBit(mask
.mask
, XI_KeyPress
);
289 SetBit(mask
.mask
, XI_KeyRelease
);
290 SetBit(mask
.mask
, XI_ButtonPress
);
291 SetBit(mask
.mask
, XI_Motion
);
292 XIGrabButton(display
, 2, 1, win
, None
, GrabModeAsync
, GrabModeAsync
,
293 False
, &mask
, nmods
, modifiers
);
294 XIGrabKeysym(display
, 3, 0x71, win
, GrabModeAsync
, GrabModeAsync
,
295 False
, &mask
, nmods
, modifiers
);
296 XIUngrabButton(display
, 3, 1, win
, nmods
- 2, &modifiers
[2]);
297 XIUngrabKeysym(display
, 3, 0x71, win
, nmods
- 2, &modifiers
[2]);
300 mask
.deviceid
= AllMasterDevices
;
301 memset(mask
.mask
, 0, 2);
302 SetBit(mask
.mask
, XI_RawEvent
);
303 XISelectEvent(display
, DefaultRootWindow(display
), &mask
, 1);
309 XMaskEvent(display
, ExposureMask
, &event
);
310 XSelectInput(display
, win
, 0);
314 test_sync_grab(display, win);
320 XNextEvent(display
, (XEvent
*)&ev
);
321 if (ev
.type
== GenericEvent
)
323 XIDeviceEvent
*event
= (XIDeviceEvent
*)&ev
;
325 printf("EVENT type %d\n", event
->evtype
);
326 switch (event
->evtype
)
328 case XI_DeviceChanged
:
329 print_devicechangedevent(display
,
330 (XIDeviceChangedEvent
*)event
);
332 case XI_HierarchyChanged
:
333 print_hierarchychangedevent((XIDeviceHierarchyEvent
*)event
);
336 print_rawevent((XIRawDeviceEvent
*)event
);
342 print_enterleave((XILeaveEvent
*)event
);
344 case XI_PropertyEvent
:
345 print_propertyevent(display
, (XIPropertyEvent
*)event
);
348 print_deviceevent(event
);
353 XIFreeEventData(&ev
);
356 XDestroyWindow(display
, win
);