]> diplodocus.org Git - xorg-xinput/blob - src/test_xi2.c
b9ac1cc7e7423229091ea59fc811b497206f7adb
[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 #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)))
34
35 static Window create_win(Display *dpy)
36 {
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,
40 BlackPixel(dpy, 0));
41
42 XSelectInput(dpy, win, ExposureMask);
43 XMapWindow(dpy, subwindow);
44 XMapWindow(dpy, win);
45 XFlush(dpy);
46 return win;
47 }
48
49 static void print_deviceevent(XIDeviceEvent* event)
50 {
51 double *val;
52 int i;
53
54 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
55 printf(" detail: %d\n", event->detail);
56
57 printf(" root: %.2f/%.2f\n", event->root_x, event->root_y);
58 printf(" event: %.2f/%.2f\n", event->event_x, event->event_x);
59
60 printf(" buttons:");
61 for (i = 0; i < event->buttons->mask_len * 8; i++)
62 if (BitIsOn(event->buttons->mask, i))
63 printf(" %d", i);
64 printf("\n");
65
66 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
67 event->mods->locked, event->mods->latched,
68 event->mods->base);
69 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
70 event->group->locked, event->group->latched,
71 event->group->base);
72 printf(" valuators:");
73
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++);
78 printf("\n");
79
80 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
81 event->root, event->event, event->child);
82 }
83
84 static void print_devicechangedevent(Display *dpy, XIDeviceChangedEvent *event)
85 {
86 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
87 printf(" reason: %s\n", (event->reason == SlaveSwitch) ? "SlaveSwitch" :
88 "DeviceChanged");
89 print_classes_xi2(dpy, event->classes, event->num_classes);
90 }
91
92 static void print_hierarchychangedevent(XIDeviceHierarchyEvent *event)
93 {
94 int i;
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]" : "");
103
104 for (i = 0; i < event->num_devices; i++)
105 {
106 char *use;
107 switch(event->info[i].use)
108 {
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;
114 break;
115 }
116
117 printf(" device %d [%s (%d)] is %s\n",
118 event->info[i].deviceid,
119 use,
120 event->info[i].attachment,
121 (event->info[i].enabled) ? "enabled" : "disabled");
122 }
123 }
124
125 static void print_rawevent(XIRawDeviceEvent *event)
126 {
127 int i;
128 double *val, *raw_val;
129
130 printf(" device: %d\n", event->deviceid);
131 printf(" detail: %d\n", event->detail);
132 printf(" valuators:\n");
133
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++);
139 printf("\n");
140 }
141
142 static void print_enterleave(XILeaveEvent* event)
143 {
144 char *mode, *detail;
145 int i;
146
147 printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n",
148 event->root, event->event, event->child);
149 switch(event->mode)
150 {
151 case NotifyNormal: mode = "NotifyNormal"; break;
152 case NotifyGrab: mode = "NotifyGrab"; break;
153 case NotifyUngrab: mode = "NotifyUngrab"; break;
154 case NotifyWhileGrabbed: mode = "NotifyWhileGrabbed"; break;
155 }
156 switch (event->detail)
157 {
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;
166 }
167 printf(" mode: %s (detail %s)\n", mode, detail);
168 printf(" flags: %s %s\n", event->focus ? "[focus]" : "",
169 event->same_screen ? "[same screen]" : "");
170 printf(" buttons:");
171 for (i = 0; i < event->buttons->mask_len * 8; i++)
172 if (BitIsOn(event->buttons->mask, i))
173 printf(" %d", i);
174 printf("\n");
175
176 printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n",
177 event->mods->locked, event->mods->latched,
178 event->mods->base);
179 printf(" group: locked 0x%x latched 0x%x base 0x%x\n",
180 event->group->locked, event->group->latched,
181 event->group->base);
182
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);
185
186 }
187
188 static void print_propertyevent(Display *display, XIPropertyEvent* event)
189 {
190 char *changed;
191
192 if (event->what == XIPropertyDeleted)
193 changed = "deleted";
194 else if (event->what == XIPropertyCreated)
195 changed = "created";
196 else
197 changed = "modified";
198
199 printf(" property: %ld '%s'\n", event->property, XGetAtomName(display, event->property));
200 printf(" changed: %s\n", changed);
201
202 }
203 void
204 test_sync_grab(Display *display, Window win)
205 {
206 int loop = 3;
207 int rc;
208 XIDeviceEventMask mask;
209
210 /* Select for motion events */
211 mask.deviceid = AllDevices;
212 mask.mask_len = 2;
213 mask.mask = calloc(2, sizeof(char));
214 SetBit(mask.mask, XI_ButtonPress);
215
216 if ((rc = XIGrabDevice(display, 2, win, CurrentTime, None, GrabModeSync,
217 GrabModeAsync, False, &mask)) != GrabSuccess)
218 {
219 fprintf(stderr, "Grab failed with %d\n", rc);
220 return;
221 }
222 free(mask.mask);
223
224 XSync(display, True);
225 XIAllowEvents(display, 2, SyncPointer, CurrentTime);
226 XFlush(display);
227
228 printf("Holding sync grab for %d button presses.\n", loop);
229
230 while(loop--)
231 {
232 XIEvent ev;
233
234 XNextEvent(display, (XEvent*)&ev);
235 if (ev.type == GenericEvent)
236 {
237 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
238 print_deviceevent(event);
239 XIAllowEvents(display, 2, SyncPointer, CurrentTime);
240 XIFreeEventData(&ev);
241 }
242 }
243
244 XIUngrabDevice(display, 2, CurrentTime);
245 printf("Done\n");
246 }
247
248 int
249 test_xi2(Display *display,
250 int argc,
251 char *argv[],
252 char *name,
253 char *desc)
254 {
255 XIDeviceEventMask mask;
256 Window win;
257
258 list(display, argc, argv, name, desc);
259 win = create_win(display);
260
261 XSync(display, False);
262
263 /* Select for motion events */
264 mask.deviceid = AllDevices;
265 mask.mask_len = 2;
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);
281
282 {
283 int modifiers[] = {0, 0x10, 0x1, 0x11};
284 int nmods = sizeof(modifiers)/sizeof(modifiers[0]);
285
286 mask.deviceid = 2;
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]);
298 }
299
300 mask.deviceid = AllMasterDevices;
301 memset(mask.mask, 0, 2);
302 SetBit(mask.mask, XI_RawEvent);
303 XISelectEvent(display, DefaultRootWindow(display), &mask, 1);
304
305 free(mask.mask);
306
307 {
308 XEvent event;
309 XMaskEvent(display, ExposureMask, &event);
310 XSelectInput(display, win, 0);
311 }
312
313 /*
314 test_sync_grab(display, win);
315 */
316
317 while(1)
318 {
319 XIEvent ev;
320 XNextEvent(display, (XEvent*)&ev);
321 if (ev.type == GenericEvent)
322 {
323 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
324
325 printf("EVENT type %d\n", event->evtype);
326 switch (event->evtype)
327 {
328 case XI_DeviceChanged:
329 print_devicechangedevent(display,
330 (XIDeviceChangedEvent*)event);
331 break;
332 case XI_HierarchyChanged:
333 print_hierarchychangedevent((XIDeviceHierarchyEvent*)event);
334 break;
335 case XI_RawEvent:
336 print_rawevent((XIRawDeviceEvent*)event);
337 break;
338 case XI_Enter:
339 case XI_Leave:
340 case XI_FocusIn:
341 case XI_FocusOut:
342 print_enterleave((XILeaveEvent*)event);
343 break;
344 case XI_PropertyEvent:
345 print_propertyevent(display, (XIPropertyEvent*)event);
346 break;
347 default:
348 print_deviceevent(event);
349 break;
350 }
351 }
352
353 XIFreeEventData(&ev);
354 }
355
356 XDestroyWindow(display, win);
357
358 return EXIT_SUCCESS;
359 }