Bug Summary

File:test_xi2.c
Location:line 257, column 17
Description:Result of 'calloc' is converted to a pointer of type 'unsigned char', which is incompatible with sizeof operand type 'char'

Annotated Source Code

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
29extern void print_classes_xi2(Display*, XIAnyClassInfo **classes,
30 int num_classes);
31
32static Window create_win(Display *dpy)
33{
34 Window win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy)((&((_XPrivDisplay)(dpy))->screens[(((_XPrivDisplay)(dpy
))->default_screen)])->root)
, 0, 0, 200,
35 200, 0, 0, WhitePixel(dpy, 0)((&((_XPrivDisplay)(dpy))->screens[0])->white_pixel
)
);
36 Window subwindow = XCreateSimpleWindow(dpy, win, 50, 50, 50, 50, 0, 0,
37 BlackPixel(dpy, 0)((&((_XPrivDisplay)(dpy))->screens[0])->black_pixel
)
);
38
39 XMapWindow(dpy, subwindow);
40 XSelectInput(dpy, win, ExposureMask(1L<<15));
41 return win;
42}
43
44static void print_deviceevent(XIDeviceEvent* event)
45{
46 double *val;
47 int i;
48
49 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
50 printf(" detail: %d\n", event->detail);
51 switch(event->evtype) {
52 case XI_KeyPress2:
53 case XI_KeyRelease3:
54 printf(" flags: %s\n", (event->flags & XIKeyRepeat(1 << 16)) ? "repeat" : "");
55 break;
56#if HAVE_XI211
57 case XI_ButtonPress4:
58 case XI_ButtonRelease5:
59 case XI_Motion6:
60 printf(" flags: %s\n", (event->flags & XIPointerEmulated(1 << 16)) ? "emulated" : "");
61 break;
62#endif
63#if HAVE_XI221
64 case XI_TouchBegin18:
65 case XI_TouchUpdate19:
66 case XI_TouchEnd20:
67 printf(" flags:%s%s\n",
68 (event->flags & XITouchPendingEnd(1 << 16)) ? " pending_end" : "",
69 (event->flags & XITouchEmulatingPointer(1 << 17)) ? " emulating" : "");
70 break;
71#endif
72 }
73
74 printf(" root: %.2f/%.2f\n", event->root_x, event->root_y);
75 printf(" event: %.2f/%.2f\n", event->event_x, event->event_y);
76
77 printf(" buttons:");
78 for (i = 0; i < event->buttons.mask_len * 8; i++)
79 if (XIMaskIsSet(event->buttons.mask, i)(((unsigned char*)(event->buttons.mask))[(i)>>3] &
(1 << ((i) & 7)))
)
80 printf(" %d", i);
81 printf("\n");
82
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");
90
91 val = event->valuators.values;
92 for (i = 0; i < event->valuators.mask_len * 8; i++)
93 if (XIMaskIsSet(event->valuators.mask, i)(((unsigned char*)(event->valuators.mask))[(i)>>3] &
(1 << ((i) & 7)))
)
94 printf(" %i: %.2f\n", i, *val++);
95
96 printf(" windows: root 0x%lx event 0x%lx child 0x%lx\n",
97 event->root, event->event, event->child);
98}
99
100static void print_devicechangedevent(Display *dpy, XIDeviceChangedEvent *event)
101{
102 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
103 printf(" reason: %s\n", (event->reason == XISlaveSwitch1) ? "SlaveSwitch" :
104 "DeviceChanged");
105 print_classes_xi2(dpy, event->classes, event->num_classes);
106}
107
108static void print_hierarchychangedevent(XIHierarchyEvent *event)
109{
110 int i;
111 printf(" Changes happened: %s %s %s %s %s %s %s %s\n",
112 (event->flags & XIMasterAdded(1 << 0)) ? "[new master]" : "",
113 (event->flags & XIMasterRemoved(1 << 1)) ? "[master removed]" : "",
114 (event->flags & XISlaveAdded(1 << 2)) ? "[new slave]" : "",
115 (event->flags & XISlaveRemoved(1 << 3)) ? "[slave removed]" : "",
116 (event->flags & XISlaveAttached(1 << 4)) ? "[slave attached]" : "",
117 (event->flags & XISlaveDetached(1 << 5)) ? "[slave detached]" : "",
118 (event->flags & XIDeviceEnabled(1 << 6)) ? "[device enabled]" : "",
119 (event->flags & XIDeviceDisabled(1 << 7)) ? "[device disabled]" : "");
120
121 for (i = 0; i < event->num_info; i++)
122 {
123 char *use = "<undefined>";
124 switch(event->info[i].use)
125 {
126 case XIMasterPointer1: use = "master pointer"; break;
127 case XIMasterKeyboard2: use = "master keyboard"; break;
128 case XISlavePointer3: use = "slave pointer"; break;
129 case XISlaveKeyboard4: use = "slave keyboard"; break;
130 case XIFloatingSlave5: use = "floating slave"; break;
131 break;
132 }
133
134 printf(" device %d [%s (%d)] is %s\n",
135 event->info[i].deviceid,
136 use,
137 event->info[i].attachment,
138 (event->info[i].enabled) ? "enabled" : "disabled");
139 if (event->info[i].flags)
140 {
141 printf(" changes: %s %s %s %s %s %s %s %s\n",
142 (event->info[i].flags & XIMasterAdded(1 << 0)) ? "[new master]" : "",
143 (event->info[i].flags & XIMasterRemoved(1 << 1)) ? "[master removed]" : "",
144 (event->info[i].flags & XISlaveAdded(1 << 2)) ? "[new slave]" : "",
145 (event->info[i].flags & XISlaveRemoved(1 << 3)) ? "[slave removed]" : "",
146 (event->info[i].flags & XISlaveAttached(1 << 4)) ? "[slave attached]" : "",
147 (event->info[i].flags & XISlaveDetached(1 << 5)) ? "[slave detached]" : "",
148 (event->info[i].flags & XIDeviceEnabled(1 << 6)) ? "[device enabled]" : "",
149 (event->info[i].flags & XIDeviceDisabled(1 << 7)) ? "[device disabled]" : "");
150 }
151 }
152}
153
154static void print_rawevent(XIRawEvent *event)
155{
156 int i;
157 double *val, *raw_val;
158
159 printf(" device: %d (%d)\n", event->deviceid, event->sourceid);
160 printf(" detail: %d\n", event->detail);
161#if HAVE_XI211
162 switch(event->evtype) {
163 case XI_RawButtonPress15:
164 case XI_RawButtonRelease16:
165 case XI_RawMotion17:
166 printf(" flags: %s\n", (event->flags & XIPointerEmulated(1 << 16)) ? "emulated" : "");
167 break;
168 }
169#endif
170
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)(((unsigned char*)(event->valuators.mask))[(i)>>3] &
(1 << ((i) & 7)))
)
176 printf(" %2d: %.2f (%.2f)\n", i, *val++, *raw_val++);
177 printf("\n");
178}
179
180static void print_enterleave(XILeaveEvent* event)
181{
182 char *mode = "<undefined>",
183 *detail = "<undefined>";
184 int i;
185
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);
189 switch(event->mode)
190 {
191 case XINotifyNormal0: mode = "NotifyNormal"; break;
192 case XINotifyGrab1: mode = "NotifyGrab"; break;
193 case XINotifyUngrab2: mode = "NotifyUngrab"; break;
194 case XINotifyWhileGrabbed3: mode = "NotifyWhileGrabbed"; break;
195 case XINotifyPassiveGrab4: mode = "NotifyPassiveGrab"; break;
196 case XINotifyPassiveUngrab5:mode = "NotifyPassiveUngrab"; break;
197 }
198 switch (event->detail)
199 {
200 case XINotifyAncestor0: detail = "NotifyAncestor"; break;
201 case XINotifyVirtual1: detail = "NotifyVirtual"; break;
202 case XINotifyInferior2: detail = "NotifyInferior"; break;
203 case XINotifyNonlinear3: detail = "NotifyNonlinear"; break;
204 case XINotifyNonlinearVirtual4: detail = "NotifyNonlinearVirtual"; break;
205 case XINotifyPointer5: detail = "NotifyPointer"; break;
206 case XINotifyPointerRoot6: detail = "NotifyPointerRoot"; break;
207 case XINotifyDetailNone7: detail = "NotifyDetailNone"; break;
208 }
209 printf(" mode: %s (detail %s)\n", mode, detail);
210 printf(" flags: %s %s\n", event->focus ? "[focus]" : "",
211 event->same_screen ? "[same screen]" : "");
212 printf(" buttons:");
213 for (i = 0; i < event->buttons.mask_len * 8; i++)
214 if (XIMaskIsSet(event->buttons.mask, i)(((unsigned char*)(event->buttons.mask))[(i)>>3] &
(1 << ((i) & 7)))
)
215 printf(" %d", i);
216 printf("\n");
217
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);
224
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);
227
228}
229
230static void print_propertyevent(Display *display, XIPropertyEvent* event)
231{
232 char *changed;
233 char *name;
234
235 if (event->what == XIPropertyDeleted0)
236 changed = "deleted";
237 else if (event->what == XIPropertyCreated1)
238 changed = "created";
239 else
240 changed = "modified";
241 name = XGetAtomName(display, event->property);
242 printf(" property: %ld '%s'\n", event->property, name);
243 printf(" changed: %s\n", changed);
244
245 XFree(name);
246}
247void
248test_sync_grab(Display *display, Window win)
249{
250 int loop = 3;
251 int rc;
252 XIEventMask mask;
253
254 /* Select for motion events */
255 mask.deviceid = XIAllDevices0;
256 mask.mask_len = 2;
257 mask.mask = calloc(2, sizeof(char));
Result of 'calloc' is converted to a pointer of type 'unsigned char', which is incompatible with sizeof operand type 'char'
258 XISetMask(mask.mask, XI_ButtonPress)(((unsigned char*)(mask.mask))[(4)>>3] |= (1 << (
(4) & 7)))
;
259
260 if ((rc = XIGrabDevice(display, 2, win, CurrentTime0L, None0L, GrabModeSync0,
261 GrabModeAsync1, False0, &mask)) != GrabSuccess0)
262 {
263 fprintf(stderr__stderrp, "Grab failed with %d\n", rc);
264 return;
265 }
266 free(mask.mask);
267
268 XSync(display, True1);
269 XIAllowEvents(display, 2, SyncPointer1, CurrentTime0L);
270 XFlush(display);
271
272 printf("Holding sync grab for %d button presses.\n", loop);
273
274 while(loop--)
275 {
276 XIEvent ev;
277
278 XNextEvent(display, (XEvent*)&ev);
279 if (ev.type == GenericEvent35 && ev.extension == xi_opcode )
280 {
281 XIDeviceEvent *event = (XIDeviceEvent*)&ev;
282 print_deviceevent(event);
283 XIAllowEvents(display, 2, SyncPointer1, CurrentTime0L);
284 }
285 }
286
287 XIUngrabDevice(display, 2, CurrentTime0L);
288 printf("Done\n");
289}
290
291static const char* type_to_name(int evtype)
292{
293 const char *name;
294
295 switch(evtype) {
296 case XI_DeviceChanged1: name = "DeviceChanged"; break;
297 case XI_KeyPress2: name = "KeyPress"; break;
298 case XI_KeyRelease3: name = "KeyRelease"; break;
299 case XI_ButtonPress4: name = "ButtonPress"; break;
300 case XI_ButtonRelease5: name = "ButtonRelease"; break;
301 case XI_Motion6: name = "Motion"; break;
302 case XI_Enter7: name = "Enter"; break;
303 case XI_Leave8: name = "Leave"; break;
304 case XI_FocusIn9: name = "FocusIn"; break;
305 case XI_FocusOut10: name = "FocusOut"; break;
306 case XI_HierarchyChanged11: name = "HierarchyChanged"; break;
307 case XI_PropertyEvent12: name = "PropertyEvent"; break;
308 case XI_RawKeyPress13: name = "RawKeyPress"; break;
309 case XI_RawKeyRelease14: name = "RawKeyRelease"; break;
310 case XI_RawButtonPress15: name = "RawButtonPress"; break;
311 case XI_RawButtonRelease16: name = "RawButtonRelease"; break;
312 case XI_RawMotion17: name = "RawMotion"; break;
313 case XI_TouchBegin18: name = "TouchBegin"; break;
314 case XI_TouchUpdate19: name = "TouchUpdate"; break;
315 case XI_TouchEnd20: name = "TouchEnd"; break;
316 case XI_RawTouchBegin22: name = "RawTouchBegin"; break;
317 case XI_RawTouchUpdate23: name = "RawTouchUpdate"; break;
318 case XI_RawTouchEnd24: name = "RawTouchEnd"; break;
319 default:
320 name = "unknown event type"; break;
321 }
322 return name;
323}
324
325
326int
327test_xi2(Display *display,
328 int argc,
329 char *argv[],
330 char *name,
331 char *desc)
332{
333 XIEventMask mask[2];
334 XIEventMask *m;
335 Window win;
336 int deviceid = -1;
337 int use_root = 0;
338 int rc;
339
340 setvbuf(stdout__stdoutp, NULL((void*)0), _IOLBF1, 0);
341
342 if (argc >= 1 && strcmp(argv[0], "--root") == 0) {
343 use_root = 1;
344
345 argc--;
346 argv++;
347 }
348
349 rc = list(display, argc, argv, name, desc);
350 if (rc != EXIT_SUCCESS0)
351 return rc;
352
353 if (use_root)
354 win = DefaultRootWindow(display)((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay
)(display))->default_screen)])->root)
;
355 else
356 win = create_win(display);
357
358 if (argc >= 1) {
359 XIDeviceInfo *info;
360 info = xi2_find_device_info(display, argv[0]);
361 deviceid = info->deviceid;
362 }
363
364 /* Select for motion events */
365 m = &mask[0];
366 m->deviceid = (deviceid == -1) ? XIAllDevices0 : deviceid;
367 m->mask_len = XIMaskLen(XI_LASTEVENT)(((26) >> 3) + 1);
368 m->mask = calloc(m->mask_len, sizeof(char));
369 XISetMask(m->mask, XI_ButtonPress)(((unsigned char*)(m->mask))[(4)>>3] |= (1 << (
(4) & 7)))
;
370 XISetMask(m->mask, XI_ButtonRelease)(((unsigned char*)(m->mask))[(5)>>3] |= (1 << (
(5) & 7)))
;
371 XISetMask(m->mask, XI_KeyPress)(((unsigned char*)(m->mask))[(2)>>3] |= (1 << (
(2) & 7)))
;
372 XISetMask(m->mask, XI_KeyRelease)(((unsigned char*)(m->mask))[(3)>>3] |= (1 << (
(3) & 7)))
;
373 XISetMask(m->mask, XI_Motion)(((unsigned char*)(m->mask))[(6)>>3] |= (1 << (
(6) & 7)))
;
374 XISetMask(m->mask, XI_DeviceChanged)(((unsigned char*)(m->mask))[(1)>>3] |= (1 << (
(1) & 7)))
;
375 XISetMask(m->mask, XI_Enter)(((unsigned char*)(m->mask))[(7)>>3] |= (1 << (
(7) & 7)))
;
376 XISetMask(m->mask, XI_Leave)(((unsigned char*)(m->mask))[(8)>>3] |= (1 << (
(8) & 7)))
;
377 XISetMask(m->mask, XI_FocusIn)(((unsigned char*)(m->mask))[(9)>>3] |= (1 << (
(9) & 7)))
;
378 XISetMask(m->mask, XI_FocusOut)(((unsigned char*)(m->mask))[(10)>>3] |= (1 <<
((10) & 7)))
;
379#ifdef HAVE_XI221
380 XISetMask(m->mask, XI_TouchBegin)(((unsigned char*)(m->mask))[(18)>>3] |= (1 <<
((18) & 7)))
;
381 XISetMask(m->mask, XI_TouchUpdate)(((unsigned char*)(m->mask))[(19)>>3] |= (1 <<
((19) & 7)))
;
382 XISetMask(m->mask, XI_TouchEnd)(((unsigned char*)(m->mask))[(20)>>3] |= (1 <<
((20) & 7)))
;
383#endif
384 if (m->deviceid == XIAllDevices0)
385 XISetMask(m->mask, XI_HierarchyChanged)(((unsigned char*)(m->mask))[(11)>>3] |= (1 <<
((11) & 7)))
;
386 XISetMask(m->mask, XI_PropertyEvent)(((unsigned char*)(m->mask))[(12)>>3] |= (1 <<
((12) & 7)))
;
387
388 m = &mask[1];
389 m->deviceid = (deviceid == -1) ? XIAllMasterDevices1 : deviceid;
390 m->mask_len = XIMaskLen(XI_LASTEVENT)(((26) >> 3) + 1);
391 m->mask = calloc(m->mask_len, sizeof(char));
392 XISetMask(m->mask, XI_RawKeyPress)(((unsigned char*)(m->mask))[(13)>>3] |= (1 <<
((13) & 7)))
;
393 XISetMask(m->mask, XI_RawKeyRelease)(((unsigned char*)(m->mask))[(14)>>3] |= (1 <<
((14) & 7)))
;
394 XISetMask(m->mask, XI_RawButtonPress)(((unsigned char*)(m->mask))[(15)>>3] |= (1 <<
((15) & 7)))
;
395 XISetMask(m->mask, XI_RawButtonRelease)(((unsigned char*)(m->mask))[(16)>>3] |= (1 <<
((16) & 7)))
;
396 XISetMask(m->mask, XI_RawMotion)(((unsigned char*)(m->mask))[(17)>>3] |= (1 <<
((17) & 7)))
;
397#ifdef HAVE_XI221
398 XISetMask(m->mask, XI_RawTouchBegin)(((unsigned char*)(m->mask))[(22)>>3] |= (1 <<
((22) & 7)))
;
399 XISetMask(m->mask, XI_RawTouchUpdate)(((unsigned char*)(m->mask))[(23)>>3] |= (1 <<
((23) & 7)))
;
400 XISetMask(m->mask, XI_RawTouchEnd)(((unsigned char*)(m->mask))[(24)>>3] |= (1 <<
((24) & 7)))
;
401#endif
402
403 XISelectEvents(display, win, &mask[0], use_root ? 2 : 1);
404 if (!use_root) {
405 XISelectEvents(display, DefaultRootWindow(display)((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay
)(display))->default_screen)])->root)
, &mask[1], 1);
406 XMapWindow(display, win);
407 }
408 XSync(display, False0);
409
410 free(mask[0].mask);
411 free(mask[1].mask);
412
413 if (!use_root) {
414 XEvent event;
415 XMaskEvent(display, ExposureMask(1L<<15), &event);
416 XSelectInput(display, win, 0);
417 }
418
419 /*
420 test_sync_grab(display, win);
421 */
422
423 while(1)
424 {
425 XEvent ev;
426 XGenericEventCookie *cookie = (XGenericEventCookie*)&ev.xcookie;
427 XNextEvent(display, (XEvent*)&ev);
428
429 if (XGetEventData(display, cookie) &&
430 cookie->type == GenericEvent35 &&
431 cookie->extension == xi_opcode)
432 {
433 printf("EVENT type %d (%s)\n", cookie->evtype, type_to_name(cookie->evtype));
434 switch (cookie->evtype)
435 {
436 case XI_DeviceChanged1:
437 print_devicechangedevent(display, cookie->data);
438 break;
439 case XI_HierarchyChanged11:
440 print_hierarchychangedevent(cookie->data);
441 break;
442 case XI_RawKeyPress13:
443 case XI_RawKeyRelease14:
444 case XI_RawButtonPress15:
445 case XI_RawButtonRelease16:
446 case XI_RawMotion17:
447 case XI_RawTouchBegin22:
448 case XI_RawTouchUpdate23:
449 case XI_RawTouchEnd24:
450 print_rawevent(cookie->data);
451 break;
452 case XI_Enter7:
453 case XI_Leave8:
454 case XI_FocusIn9:
455 case XI_FocusOut10:
456 print_enterleave(cookie->data);
457 break;
458 case XI_PropertyEvent12:
459 print_propertyevent(display, cookie->data);
460 break;
461 default:
462 print_deviceevent(cookie->data);
463 break;
464 }
465 }
466
467 XFreeEventData(display, cookie);
468 }
469
470 XDestroyWindow(display, win);
471
472 return EXIT_SUCCESS0;
473}