1 /************************************************************
3 Copyright 1989, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
25 Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Hewlett-Packard not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ********************************************************/
47 /***********************************************************************
49 * Input Extension library internal functions.
59 #include <X11/extensions/XI.h>
60 #include <X11/extensions/XI2.h>
61 #include <X11/extensions/XIproto.h>
62 #include <X11/extensions/XI2proto.h>
63 #include <X11/Xlibint.h>
64 #include <X11/extensions/XInput.h>
65 #include <X11/extensions/XInput2.h>
66 #include <X11/extensions/extutil.h>
67 #include <X11/extensions/geproto.h>
68 #include <X11/extensions/ge.h>
69 #include <X11/extensions/Xge.h>
72 #define ENQUEUE_EVENT True
73 #define DONT_ENQUEUE False
74 #define FP1616toDBL(x) ((x) * 1.0 / (1 << 16))
76 extern void _xibaddevice(
81 extern void _xibadclass(
86 extern void _xibadevent(
91 extern void _xibadmode(
96 extern void _xidevicebusy(
101 extern int _XiGetDevicePresenceNotifyEvent(
105 int copy_classes(XIDeviceInfo *to, xXIAnyInfo* from, int *nclasses);
106 int size_classes(xXIAnyInfo* from, int nclasses);
108 static XExtensionInfo *xinput_info;
109 static /* const */ char *xinput_extension_name = INAME;
111 static int XInputClose(
113 XExtCodes * /* codes */
116 static char *XInputError(
119 XExtCodes * /* codes */,
124 static Bool XInputWireToEvent(
129 static Bool XInputWireToCookie(
130 Display* /* display */,
131 XGenericEventCookie* /* re */,
135 static Bool XInputCopyCookie(
136 Display* /* display */,
137 XGenericEventCookie* /* in */,
138 XGenericEventCookie* /* out */
142 wireToDeviceEvent(xXIDeviceEvent *in, XGenericEventCookie* out);
144 wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XGenericEventCookie *cookie);
146 wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XGenericEventCookie *cookie);
148 wireToRawEvent(XExtDisplayInfo *info, xXIRawEvent *in, XGenericEventCookie *cookie);
150 wireToEnterLeave(xXIEnterEvent *in, XGenericEventCookie *cookie);
152 wireToPropertyEvent(xXIPropertyEvent *in, XGenericEventCookie *cookie);
154 wireToTouchOwnershipEvent(xXITouchOwnershipEvent *in,
155 XGenericEventCookie *cookie);
157 static /* const */ XEvent emptyevent;
159 typedef Status (*core_event_to_wire)(Display*, XEvent*, xEvent*);
161 static /* const */ XExtensionHooks xinput_extension_hooks = {
162 NULL, /* create_gc */
166 NULL, /* create_font */
167 NULL, /* free_font */
168 XInputClose, /* close_display */
169 XInputWireToEvent, /* wire_to_event */
170 (core_event_to_wire)_XiEventToWire, /* event_to_wire */
172 XInputError, /* error_string */
175 static char *XInputErrorList[] = {
176 "BadDevice, invalid or uninitialized input device", /* BadDevice */
177 "BadEvent, invalid event type", /* BadEvent */
178 "BadMode, invalid mode parameter", /* BadMode */
179 "DeviceBusy, device is busy", /* DeviceBusy */
180 "BadClass, invalid event class", /* BadClass */
183 /* Get the version supported by the server to know which number of
184 * events are support. Otherwise, a wrong number of events may smash
185 * the Xlib-internal event processing vector.
187 * Since the extension hasn't been initialized yet, we need to
188 * manually get the opcode, then the version.
191 _XiFindEventsSupported(Display *dpy)
194 XExtensionVersion *extversion = NULL;
197 if (!XQueryExtension(dpy, INAME, &codes.major_opcode,
198 &codes.first_event, &codes.first_error))
202 extversion = _XiGetExtensionVersionRequest(dpy, INAME, codes.major_opcode);
206 if (!extversion || !extversion->present)
209 if (extversion->major_version >= 2)
210 nevents = IEVENTS; /* number is fixed, XI2 adds GenericEvents only */
211 else if (extversion->major_version <= 0)
213 printf("XInput_find_display: invalid extension version %d.%d\n",
214 extversion->major_version, extversion->minor_version);
219 switch(extversion->minor_version)
221 case XI_Add_DeviceProperties_Minor:
222 nevents = XI_DevicePropertyNotify + 1;
224 case XI_Add_DevicePresenceNotify_Minor:
225 nevents = XI_DevicePresenceNotify + 1;
228 nevents = XI_DeviceButtonstateNotify + 1;
241 XExtDisplayInfo *XInput_find_display (Display *dpy)
243 XExtDisplayInfo *dpyinfo;
244 if (!xinput_info) { if (!(xinput_info = XextCreateExtension())) return NULL; }
245 if (!(dpyinfo = XextFindDisplay (xinput_info, dpy)))
247 int nevents = _XiFindEventsSupported(dpy);
249 dpyinfo = XextAddDisplay (xinput_info, dpy,
250 xinput_extension_name,
251 &xinput_extension_hooks,
253 if (dpyinfo->codes) /* NULL if XI doesn't exist on the server */
255 XESetWireToEventCookie(dpy, dpyinfo->codes->major_opcode, XInputWireToCookie);
256 XESetCopyEventCookie(dpy, dpyinfo->codes->major_opcode, XInputCopyCookie);
262 static XEXT_GENERATE_ERROR_STRING(XInputError, xinput_extension_name,
263 IERRORS, XInputErrorList)
264 /*******************************************************************
266 * Input extension versions.
269 static XExtensionVersion versions[] = { {XI_Absent, 0, 0},
270 {XI_Present, XI_Initial_Release_Major, XI_Initial_Release_Minor},
271 {XI_Present, XI_Add_XDeviceBell_Major, XI_Add_XDeviceBell_Minor},
272 {XI_Present, XI_Add_XSetDeviceValuators_Major,
273 XI_Add_XSetDeviceValuators_Minor},
274 {XI_Present, XI_Add_XChangeDeviceControl_Major,
275 XI_Add_XChangeDeviceControl_Minor},
276 {XI_Present, XI_Add_DevicePresenceNotify_Major,
277 XI_Add_DevicePresenceNotify_Minor},
278 {XI_Present, XI_Add_DeviceProperties_Major,
279 XI_Add_DeviceProperties_Minor},
285 /***********************************************************************
287 * Return errors reported by this extension.
296 XExtDisplayInfo *info = XInput_find_display(dpy);
298 *error = info->codes->first_error + XI_BadDevice;
306 XExtDisplayInfo *info = XInput_find_display(dpy);
308 *error = info->codes->first_error + XI_BadClass;
316 XExtDisplayInfo *info = XInput_find_display(dpy);
318 *error = info->codes->first_error + XI_BadEvent;
326 XExtDisplayInfo *info = XInput_find_display(dpy);
328 *error = info->codes->first_error + XI_BadMode;
336 XExtDisplayInfo *info = XInput_find_display(dpy);
338 *error = info->codes->first_error + XI_DeviceBusy;
341 static int XInputCheckExtension(Display *dpy, XExtDisplayInfo *info)
343 XextCheckExtension (dpy, info, xinput_extension_name, 0);
347 /*****************************************************************
348 * Compare version numbers between info and the built-in version table.
350 * -1 if info's version is less than version_index's version,
351 * 0 if equal (or DontCheck),
352 * 1 if info's version is greater than version_index's version.
353 * Returns -2 on initialization errors which shouldn't happen if you call it
357 _XiCheckVersion(XExtDisplayInfo *info,
360 XExtensionVersion *ext;
362 if (versions[version_index].major_version == Dont_Check)
368 ext = ((XInputData *) info->data)->vers;
372 if (ext->major_version == versions[version_index].major_version &&
373 ext->minor_version == versions[version_index].minor_version)
376 if (ext->major_version < versions[version_index].major_version ||
377 (ext->major_version == versions[version_index].major_version &&
378 ext->minor_version < versions[version_index].minor_version))
384 /***********************************************************************
386 * Check to see if the input extension is installed in the server.
387 * Also check to see if the version is >= the requested version.
393 register Display *dpy,
394 register int version_index,
395 XExtDisplayInfo *info)
397 if (!XInputCheckExtension(dpy, info)) {
402 if (info->data == NULL) {
403 info->data = (XPointer) Xmalloc(sizeof(XInputData));
408 ((XInputData *) info->data)->vers =
409 _XiGetExtensionVersion(dpy, "XInputExtension", info);
412 if (_XiCheckVersion(info, version_index) < 0) {
420 /***********************************************************************
422 * Close display routine.
431 XExtDisplayInfo *info = XInput_find_display(dpy);
433 if (info->data != NULL) {
434 XFree((char *)((XInputData *) info->data)->vers);
435 XFree((char *)info->data);
438 if (!XextRemoveDisplay(xinput_info, dpy))
441 if (xinput_info->ndisplays == 0) {
442 XextDestroyExtension(xinput_info);
454 y = (mask >> 1) & 033333333333;
455 y = mask - y - ((y >> 1) & 033333333333);
456 return (((y + (y >> 3)) & 030707070707) % 077);
459 static int count_bits(unsigned char* ptr, int len)
465 for (i = 0; i < len; i++)
478 _XiGetDevicePresenceNotifyEvent(Display * dpy)
480 XExtDisplayInfo *info = XInput_find_display(dpy);
482 return info->codes->first_event + XI_DevicePresenceNotify;
485 /***********************************************************************
487 * Handle Input extension events.
488 * Reformat a wire event into an XEvent structure of the right type.
498 unsigned int type, reltype;
500 XExtDisplayInfo *info = XInput_find_display(dpy);
501 XEvent *save = (XEvent *) info->data;
503 type = event->u.u.type & 0x7f;
504 reltype = (type - info->codes->first_event);
506 if (type == GenericEvent ||
507 (reltype != XI_DeviceValuator &&
508 reltype != XI_DeviceKeystateNotify &&
509 reltype != XI_DeviceButtonstateNotify)) {
512 ((XAnyEvent *) save)->serial = _XSetLastRequestRead(dpy,
515 ((XAnyEvent *) save)->send_event = ((event->u.u.type & 0x80) != 0);
516 ((XAnyEvent *) save)->display = dpy;
519 /* Process traditional events */
520 if (type != GenericEvent)
523 case XI_DeviceMotionNotify:
525 register XDeviceMotionEvent *ev = (XDeviceMotionEvent *) save;
526 deviceKeyButtonPointer *ev2 = (deviceKeyButtonPointer *) event;
528 ev->root = ev2->root;
529 ev->window = ev2->event;
530 ev->subwindow = ev2->child;
531 ev->time = ev2->time;
532 ev->x_root = ev2->root_x;
533 ev->y_root = ev2->root_y;
534 ev->x = ev2->event_x;
535 ev->y = ev2->event_y;
536 ev->state = ev2->state;
537 ev->same_screen = ev2->same_screen;
538 ev->is_hint = ev2->detail;
539 ev->deviceid = ev2->deviceid & DEVICE_BITS;
540 return (DONT_ENQUEUE);
543 case XI_DeviceKeyPress:
544 case XI_DeviceKeyRelease:
546 register XDeviceKeyEvent *ev = (XDeviceKeyEvent *) save;
547 deviceKeyButtonPointer *ev2 = (deviceKeyButtonPointer *) event;
549 ev->root = ev2->root;
550 ev->window = ev2->event;
551 ev->subwindow = ev2->child;
552 ev->time = ev2->time;
553 ev->x_root = ev2->root_x;
554 ev->y_root = ev2->root_y;
555 ev->x = ev2->event_x;
556 ev->y = ev2->event_y;
557 ev->state = ev2->state;
558 ev->same_screen = ev2->same_screen;
559 ev->keycode = ev2->detail;
560 ev->deviceid = ev2->deviceid & DEVICE_BITS;
561 if (ev2->deviceid & MORE_EVENTS)
562 return (DONT_ENQUEUE);
565 return (ENQUEUE_EVENT);
569 case XI_DeviceButtonPress:
570 case XI_DeviceButtonRelease:
572 register XDeviceButtonEvent *ev = (XDeviceButtonEvent *) save;
573 deviceKeyButtonPointer *ev2 = (deviceKeyButtonPointer *) event;
575 ev->root = ev2->root;
576 ev->window = ev2->event;
577 ev->subwindow = ev2->child;
578 ev->time = ev2->time;
579 ev->x_root = ev2->root_x;
580 ev->y_root = ev2->root_y;
581 ev->x = ev2->event_x;
582 ev->y = ev2->event_y;
583 ev->state = ev2->state;
584 ev->same_screen = ev2->same_screen;
585 ev->button = ev2->detail;
586 ev->deviceid = ev2->deviceid & DEVICE_BITS;
587 if (ev2->deviceid & MORE_EVENTS)
588 return (DONT_ENQUEUE);
591 return (ENQUEUE_EVENT);
596 case XI_ProximityOut:
598 register XProximityNotifyEvent *ev = (XProximityNotifyEvent *) save;
599 deviceKeyButtonPointer *ev2 = (deviceKeyButtonPointer *) event;
601 ev->root = ev2->root;
602 ev->window = ev2->event;
603 ev->subwindow = ev2->child;
604 ev->time = ev2->time;
605 ev->x_root = ev2->root_x;
606 ev->y_root = ev2->root_y;
607 ev->x = ev2->event_x;
608 ev->y = ev2->event_y;
609 ev->state = ev2->state;
610 ev->same_screen = ev2->same_screen;
611 ev->deviceid = ev2->deviceid & DEVICE_BITS;
612 if (ev2->deviceid & MORE_EVENTS)
613 return (DONT_ENQUEUE);
616 return (ENQUEUE_EVENT);
620 case XI_DeviceValuator:
622 deviceValuator *xev = (deviceValuator *) event;
623 int save_type = save->type - info->codes->first_event;
625 if (save_type == XI_DeviceKeyPress || save_type == XI_DeviceKeyRelease) {
626 XDeviceKeyEvent *kev = (XDeviceKeyEvent *) save;
628 kev->device_state = xev->device_state;
629 kev->axes_count = xev->num_valuators;
630 kev->first_axis = xev->first_valuator;
631 i = xev->num_valuators;
636 kev->axis_data[5] = xev->valuator5;
638 kev->axis_data[4] = xev->valuator4;
640 kev->axis_data[3] = xev->valuator3;
642 kev->axis_data[2] = xev->valuator2;
644 kev->axis_data[1] = xev->valuator1;
646 kev->axis_data[0] = xev->valuator0;
648 } else if (save_type == XI_DeviceButtonPress ||
649 save_type == XI_DeviceButtonRelease) {
650 XDeviceButtonEvent *bev = (XDeviceButtonEvent *) save;
652 bev->device_state = xev->device_state;
653 bev->axes_count = xev->num_valuators;
654 bev->first_axis = xev->first_valuator;
655 i = xev->num_valuators;
660 bev->axis_data[5] = xev->valuator5;
662 bev->axis_data[4] = xev->valuator4;
664 bev->axis_data[3] = xev->valuator3;
666 bev->axis_data[2] = xev->valuator2;
668 bev->axis_data[1] = xev->valuator1;
670 bev->axis_data[0] = xev->valuator0;
672 } else if (save_type == XI_DeviceMotionNotify) {
673 XDeviceMotionEvent *mev = (XDeviceMotionEvent *) save;
675 mev->device_state = xev->device_state;
676 mev->axes_count = xev->num_valuators;
677 mev->first_axis = xev->first_valuator;
678 i = xev->num_valuators;
683 mev->axis_data[5] = xev->valuator5;
685 mev->axis_data[4] = xev->valuator4;
687 mev->axis_data[3] = xev->valuator3;
689 mev->axis_data[2] = xev->valuator2;
691 mev->axis_data[1] = xev->valuator1;
693 mev->axis_data[0] = xev->valuator0;
695 } else if (save_type == XI_ProximityIn || save_type == XI_ProximityOut) {
696 XProximityNotifyEvent *pev = (XProximityNotifyEvent *) save;
698 pev->device_state = xev->device_state;
699 pev->axes_count = xev->num_valuators;
700 pev->first_axis = xev->first_valuator;
701 i = xev->num_valuators;
706 pev->axis_data[5] = xev->valuator5;
708 pev->axis_data[4] = xev->valuator4;
710 pev->axis_data[3] = xev->valuator3;
712 pev->axis_data[2] = xev->valuator2;
714 pev->axis_data[1] = xev->valuator1;
716 pev->axis_data[0] = xev->valuator0;
718 } else if (save_type == XI_DeviceStateNotify) {
719 XDeviceStateNotifyEvent *sev = (XDeviceStateNotifyEvent *) save;
720 XInputClass *any = (XInputClass *) & sev->data[0];
723 for (i = 0; i < sev->num_classes; i++)
724 if (any->class != ValuatorClass)
725 any = (XInputClass *) ((char *)any + any->length);
726 v = (XValuatorStatus *) any;
727 i = v->num_valuators;
728 j = xev->num_valuators;
733 v->valuators[i + 2] = xev->valuator2;
735 v->valuators[i + 1] = xev->valuator1;
737 v->valuators[i + 0] = xev->valuator0;
739 v->num_valuators += j;
743 return (ENQUEUE_EVENT);
746 case XI_DeviceFocusIn:
747 case XI_DeviceFocusOut:
749 register XDeviceFocusChangeEvent *ev = (XDeviceFocusChangeEvent *) re;
750 deviceFocus *fev = (deviceFocus *) event;
752 *ev = *((XDeviceFocusChangeEvent *) save);
753 ev->window = fev->window;
754 ev->time = fev->time;
755 ev->mode = fev->mode;
756 ev->detail = fev->detail;
757 ev->deviceid = fev->deviceid & DEVICE_BITS;
758 return (ENQUEUE_EVENT);
761 case XI_DeviceStateNotify:
763 XDeviceStateNotifyEvent *stev = (XDeviceStateNotifyEvent *) save;
764 deviceStateNotify *sev = (deviceStateNotify *) event;
768 stev->deviceid = sev->deviceid & DEVICE_BITS;
769 stev->time = sev->time;
770 stev->num_classes = Ones((Mask) sev->classes_reported & InputClassBits);
771 data = (char *)&stev->data[0];
772 if (sev->classes_reported & (1 << KeyClass)) {
773 register XKeyStatus *kstev = (XKeyStatus *) data;
775 kstev->class = KeyClass;
776 kstev->length = sizeof(XKeyStatus);
777 kstev->num_keys = sev->num_keys;
778 memcpy((char *)&kstev->keys[0], (char *)&sev->keys[0], 4);
779 data += sizeof(XKeyStatus);
781 if (sev->classes_reported & (1 << ButtonClass)) {
782 register XButtonStatus *bev = (XButtonStatus *) data;
784 bev->class = ButtonClass;
785 bev->length = sizeof(XButtonStatus);
786 bev->num_buttons = sev->num_buttons;
787 memcpy((char *)bev->buttons, (char *)sev->buttons, 4);
788 data += sizeof(XButtonStatus);
790 if (sev->classes_reported & (1 << ValuatorClass)) {
791 register XValuatorStatus *vev = (XValuatorStatus *) data;
793 vev->class = ValuatorClass;
794 vev->length = sizeof(XValuatorStatus);
795 vev->num_valuators = sev->num_valuators;
796 vev->mode = sev->classes_reported >> ModeBitsShift;
797 j = sev->num_valuators;
802 vev->valuators[2] = sev->valuator2;
804 vev->valuators[1] = sev->valuator1;
806 vev->valuators[0] = sev->valuator0;
808 data += sizeof(XValuatorStatus);
810 if (sev->deviceid & MORE_EVENTS)
811 return (DONT_ENQUEUE);
814 return (ENQUEUE_EVENT);
818 case XI_DeviceKeystateNotify:
821 XInputClass *anyclass;
822 register XKeyStatus *kv;
823 deviceKeyStateNotify *ksev = (deviceKeyStateNotify *) event;
824 XDeviceStateNotifyEvent *kstev = (XDeviceStateNotifyEvent *) save;
826 anyclass = (XInputClass *) & kstev->data[0];
827 for (i = 0; i < kstev->num_classes; i++)
828 if (anyclass->class == KeyClass)
831 anyclass = (XInputClass *) ((char *)anyclass +
834 kv = (XKeyStatus *) anyclass;
836 memcpy((char *)&kv->keys[4], (char *)ksev->keys, 28);
837 if (ksev->deviceid & MORE_EVENTS)
838 return (DONT_ENQUEUE);
841 return (ENQUEUE_EVENT);
845 case XI_DeviceButtonstateNotify:
848 XInputClass *anyclass;
849 register XButtonStatus *bv;
850 deviceButtonStateNotify *bsev = (deviceButtonStateNotify *) event;
851 XDeviceStateNotifyEvent *bstev = (XDeviceStateNotifyEvent *) save;
853 anyclass = (XInputClass *) & bstev->data[0];
854 for (i = 0; i < bstev->num_classes; i++)
855 if (anyclass->class == ButtonClass)
858 anyclass = (XInputClass *) ((char *)anyclass +
861 bv = (XButtonStatus *) anyclass;
862 bv->num_buttons = 256;
863 memcpy((char *)&bv->buttons[4], (char *)bsev->buttons, 28);
864 if (bsev->deviceid & MORE_EVENTS)
865 return (DONT_ENQUEUE);
868 return (ENQUEUE_EVENT);
872 case XI_DeviceMappingNotify:
874 register XDeviceMappingEvent *ev = (XDeviceMappingEvent *) re;
875 deviceMappingNotify *ev2 = (deviceMappingNotify *) event;
877 *ev = *((XDeviceMappingEvent *) save);
879 ev->first_keycode = ev2->firstKeyCode;
880 ev->request = ev2->request;
881 ev->count = ev2->count;
882 ev->time = ev2->time;
883 ev->deviceid = ev2->deviceid & DEVICE_BITS;
884 return (ENQUEUE_EVENT);
887 case XI_ChangeDeviceNotify:
889 register XChangeDeviceNotifyEvent *ev = (XChangeDeviceNotifyEvent *) re;
890 changeDeviceNotify *ev2 = (changeDeviceNotify *) event;
892 *ev = *((XChangeDeviceNotifyEvent *) save);
894 ev->request = ev2->request;
895 ev->time = ev2->time;
896 ev->deviceid = ev2->deviceid & DEVICE_BITS;
897 return (ENQUEUE_EVENT);
901 case XI_DevicePresenceNotify:
903 XDevicePresenceNotifyEvent *ev = (XDevicePresenceNotifyEvent *) re;
904 devicePresenceNotify *ev2 = (devicePresenceNotify *) event;
906 *ev = *(XDevicePresenceNotifyEvent *) save;
908 ev->time = ev2->time;
909 ev->devchange = ev2->devchange;
910 ev->deviceid = ev2->deviceid;
911 ev->control = ev2->control;
912 return (ENQUEUE_EVENT);
915 case XI_DevicePropertyNotify:
917 XDevicePropertyNotifyEvent* ev = (XDevicePropertyNotifyEvent*)re;
918 devicePropertyNotify *ev2 = (devicePropertyNotify*)event;
920 *ev = *(XDevicePropertyNotifyEvent*)save;
921 ev->time = ev2->time;
922 ev->deviceid = ev2->deviceid;
923 ev->atom = ev2->atom;
924 ev->state = ev2->state;
925 return ENQUEUE_EVENT;
929 printf("XInputWireToEvent: UNKNOWN WIRE EVENT! type=%d\n", type);
933 return (DONT_ENQUEUE);
936 static void xge_copy_to_cookie(xGenericEvent* ev,
937 XGenericEventCookie *cookie)
939 cookie->type = ev->type;
940 cookie->evtype = ev->evtype;
941 cookie->extension = ev->extension;
947 XGenericEventCookie *cookie,
950 XExtDisplayInfo *info = XInput_find_display(dpy);
951 XEvent *save = (XEvent *) info->data;
952 xGenericEvent* ge = (xGenericEvent*)event;
954 if (ge->extension != info->codes->major_opcode)
956 printf("XInputWireToCookie: wrong extension opcode %d\n",
962 save->type = event->u.u.type;
963 ((XAnyEvent*)save)->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
964 ((XAnyEvent*)save)->send_event = ((event->u.u.type & 0x80) != 0);
965 ((XAnyEvent*)save)->display = dpy;
967 xge_copy_to_cookie((xGenericEvent*)event, (XGenericEventCookie*)save);
972 case XI_ButtonRelease:
978 *cookie = *(XGenericEventCookie*)save;
979 if (!wireToDeviceEvent((xXIDeviceEvent*)event, cookie))
981 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
985 return ENQUEUE_EVENT;
986 case XI_DeviceChanged:
987 *cookie = *(XGenericEventCookie*)save;
988 if (!wireToDeviceChangedEvent((xXIDeviceChangedEvent*)event, cookie))
990 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
994 return ENQUEUE_EVENT;
995 case XI_HierarchyChanged:
996 *cookie = *(XGenericEventCookie*)save;
997 if (!wireToHierarchyChangedEvent((xXIHierarchyEvent*)event, cookie))
999 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
1003 return ENQUEUE_EVENT;
1004 case XI_TouchOwnership:
1005 *cookie = *(XGenericEventCookie*)save;
1006 if (!wireToTouchOwnershipEvent((xXITouchOwnershipEvent*)event,
1009 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
1013 return ENQUEUE_EVENT;
1015 case XI_RawKeyPress:
1016 case XI_RawKeyRelease:
1017 case XI_RawButtonPress:
1018 case XI_RawButtonRelease:
1020 case XI_RawTouchBegin:
1021 case XI_RawTouchUpdate:
1022 case XI_RawTouchEnd:
1023 *cookie = *(XGenericEventCookie*)save;
1024 if (!wireToRawEvent(info, (xXIRawEvent*)event, cookie))
1026 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
1030 return ENQUEUE_EVENT;
1035 *cookie = *(XGenericEventCookie*)save;
1036 if (!wireToEnterLeave((xXIEnterEvent*)event, cookie))
1038 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
1042 return ENQUEUE_EVENT;
1043 case XI_PropertyEvent:
1044 *cookie = *(XGenericEventCookie*)save;
1045 if (!wireToPropertyEvent((xXIPropertyEvent*)event, cookie))
1047 printf("XInputWireToCookie: CONVERSION FAILURE! evtype=%d\n",
1051 return ENQUEUE_EVENT;
1053 printf("XInputWireToCookie: Unknown generic event. type %d\n", ge->evtype);
1056 return DONT_ENQUEUE;
1060 * Calculate length in bytes needed for the device event with the given
1061 * button mask length, valuator mask length + valuator mask. All parameters
1065 sizeDeviceEvent(int buttons_len, int valuators_len,
1066 unsigned char *valuators_mask)
1070 len = sizeof(XIDeviceEvent);
1071 len += sizeof(XIButtonState) + buttons_len;
1072 len += sizeof(XIValuatorState) + valuators_len;
1073 len += count_bits(valuators_mask, valuators_len) * sizeof(double);
1074 len += sizeof(XIModifierState) + sizeof(XIGroupState);
1079 /* Return the size with added padding so next element would be
1080 double-aligned unless the architecture is known to allow unaligned
1081 data accesses. Not doing this can cause a bus error on
1084 pad_to_double(int size)
1086 #if !defined(__i386__) && !defined(__sh__)
1087 if (size % sizeof(double) != 0)
1088 size += sizeof(double) - size % sizeof(double);
1094 * Set structure and atoms to size in bytes of XIButtonClassInfo, its
1095 * button state mask and labels array.
1098 sizeXIButtonClassType(int num_buttons, int* structure, int* state, int* atoms)
1103 *structure = pad_to_double(sizeof(XIButtonClassInfo));
1104 size = ((((num_buttons + 7)/8) + 3)/4);
1106 /* Force mask alignment with longs to avoid unaligned
1107 * access when accessing the atoms. */
1108 *state = pad_to_double(size * 4);
1109 labels = num_buttons * sizeof(Atom);
1111 /* Force mask alignment with longs to avoid
1112 * unaligned access when accessing the atoms. */
1113 labels += ((((num_buttons + 7)/8) + 3)/4) * sizeof(Atom);
1114 *atoms = pad_to_double(labels);
1118 * Set structure and keycodes to size in bytes of XIKeyClassInfo and
1119 * its keycodes array.
1122 sizeXIKeyClassType(int num_keycodes, int* structure, int* keycodes)
1124 *structure = pad_to_double(sizeof(XIKeyClassInfo));
1125 *keycodes = pad_to_double(num_keycodes * sizeof(int));
1129 * Return the size in bytes required to store the matching class type
1130 * num_elements is num_buttons for XIButtonClass or num_keycodes for
1133 * Also used from copy_classes in XIQueryDevice.c
1136 sizeDeviceClassType(int type, int num_elements)
1144 sizeXIButtonClassType(num_elements, &l, &extra1, &extra2);
1145 l += extra1 + extra2;
1148 sizeXIKeyClassType(num_elements, &l, &extra1);
1151 case XIValuatorClass:
1152 l = pad_to_double(sizeof(XIValuatorClassInfo));
1155 l = pad_to_double(sizeof(XIScrollClassInfo));
1158 l = pad_to_double(sizeof(XITouchClassInfo));
1161 printf("sizeDeviceClassType: unknown type %d\n", type);
1168 copyHierarchyEvent(XGenericEventCookie *cookie_in,
1169 XGenericEventCookie *cookie_out)
1171 XIHierarchyEvent *in, *out;
1174 in = cookie_in->data;
1176 ptr = cookie_out->data = malloc(sizeof(XIHierarchyEvent) +
1177 in->num_info * sizeof(XIHierarchyInfo));
1181 out = next_block(&ptr, sizeof(XIHierarchyEvent));
1183 out->info = next_block(&ptr, in->num_info * sizeof(XIHierarchyInfo));
1184 memcpy(out->info, in->info, in->num_info * sizeof(XIHierarchyInfo));
1190 copyDeviceChangedEvent(XGenericEventCookie *in_cookie,
1191 XGenericEventCookie *out_cookie)
1194 XIDeviceChangedEvent *in, *out;
1195 XIAnyClassInfo *any;
1198 in = in_cookie->data;
1200 len = sizeof(XIDeviceChangedEvent);
1201 len += in->num_classes * sizeof(XIAnyClassInfo*);
1203 for (i = 0; i < in->num_classes; i++)
1205 any = in->classes[i];
1209 len += sizeDeviceClassType(XIButtonClass,
1210 ((XIButtonClassInfo*)any)->num_buttons);
1213 len += sizeDeviceClassType(XIKeyClass,
1214 ((XIKeyClassInfo*)any)->num_keycodes);
1216 case XIValuatorClass:
1217 len += sizeDeviceClassType(XIValuatorClass, 0);
1220 len += sizeDeviceClassType(XIScrollClass, 0);
1223 printf("copyDeviceChangedEvent: unknown type %d\n",
1230 ptr = out_cookie->data = malloc(len);
1233 out = next_block(&ptr, sizeof(XIDeviceChangedEvent));
1236 out->classes = next_block(&ptr,
1237 out->num_classes * sizeof(XIAnyClassInfo*));
1239 for (i = 0; i < in->num_classes; i++)
1241 any = in->classes[i];
1250 XIButtonClassInfo *bin, *bout;
1251 bin = (XIButtonClassInfo*)any;
1252 sizeXIButtonClassType(bin->num_buttons, &struct_size,
1253 &state_size, &labels_size);
1254 bout = next_block(&ptr, struct_size);
1257 bout->state.mask = next_block(&ptr, state_size);
1258 memcpy(bout->state.mask, bin->state.mask,
1259 bout->state.mask_len);
1261 bout->labels = next_block(&ptr, labels_size);
1262 memcpy(bout->labels, bin->labels, bout->num_buttons * sizeof(Atom));
1263 out->classes[i] = (XIAnyClassInfo*)bout;
1268 XIKeyClassInfo *kin, *kout;
1271 kin = (XIKeyClassInfo*)any;
1272 sizeXIKeyClassType(kin->num_keycodes, &struct_size,
1275 kout = next_block(&ptr, struct_size);
1277 kout->keycodes = next_block(&ptr, keycodes_size);
1278 memcpy(kout->keycodes, kin->keycodes, kout->num_keycodes * sizeof(int));
1279 out->classes[i] = (XIAnyClassInfo*)kout;
1282 case XIValuatorClass:
1284 XIValuatorClassInfo *vin, *vout;
1285 vin = (XIValuatorClassInfo*)any;
1286 vout = next_block(&ptr,
1287 sizeDeviceClassType(XIValuatorClass, 0));
1289 out->classes[i] = (XIAnyClassInfo*)vout;
1294 XIScrollClassInfo *sin, *sout;
1295 sin = (XIScrollClassInfo*)any;
1296 sout = next_block(&ptr,
1297 sizeDeviceClassType(XIScrollClass, 0));
1299 out->classes[i] = (XIAnyClassInfo*)sout;
1309 copyDeviceEvent(XGenericEventCookie *cookie_in,
1310 XGenericEventCookie *cookie_out)
1313 XIDeviceEvent *in, *out;
1314 int bits; /* valuator bits */
1317 in = cookie_in->data;
1318 bits = count_bits(in->valuators.mask, in->valuators.mask_len);
1320 len = sizeDeviceEvent(in->buttons.mask_len, in->valuators.mask_len,
1321 in->valuators.mask);
1323 ptr = cookie_out->data = malloc(len);
1327 out = next_block(&ptr, sizeof(XIDeviceEvent));
1330 out->buttons.mask = next_block(&ptr, in->buttons.mask_len);
1331 memcpy(out->buttons.mask, in->buttons.mask,
1332 out->buttons.mask_len);
1333 out->valuators.mask = next_block(&ptr, in->valuators.mask_len);
1334 memcpy(out->valuators.mask, in->valuators.mask,
1335 out->valuators.mask_len);
1336 out->valuators.values = next_block(&ptr, bits * sizeof(double));
1337 memcpy(out->valuators.values, in->valuators.values,
1338 bits * sizeof(double));
1344 copyEnterEvent(XGenericEventCookie *cookie_in,
1345 XGenericEventCookie *cookie_out)
1348 XIEnterEvent *in, *out;
1351 in = cookie_in->data;
1353 len = sizeof(XIEnterEvent) + in->buttons.mask_len;
1355 ptr = cookie_out->data = malloc(len);
1359 out = next_block(&ptr, sizeof(XIEnterEvent));
1362 out->buttons.mask = next_block(&ptr, in->buttons.mask_len);
1363 memcpy(out->buttons.mask, in->buttons.mask, out->buttons.mask_len);
1369 copyPropertyEvent(XGenericEventCookie *cookie_in,
1370 XGenericEventCookie *cookie_out)
1372 XIPropertyEvent *in, *out;
1374 in = cookie_in->data;
1376 out = cookie_out->data = malloc(sizeof(XIPropertyEvent));
1385 copyTouchOwnershipEvent(XGenericEventCookie *cookie_in,
1386 XGenericEventCookie *cookie_out)
1388 XITouchOwnershipEvent *in, *out;
1390 in = cookie_in->data;
1392 out = cookie_out->data = malloc(sizeof(XITouchOwnershipEvent));
1401 copyRawEvent(XGenericEventCookie *cookie_in,
1402 XGenericEventCookie *cookie_out)
1404 XIRawEvent *in, *out;
1409 in = cookie_in->data;
1411 bits = count_bits(in->valuators.mask, in->valuators.mask_len);
1412 len = sizeof(XIRawEvent) + in->valuators.mask_len;
1413 len += bits * sizeof(double) * 2;
1415 ptr = cookie_out->data = malloc(len);
1419 out = next_block(&ptr, sizeof(XIRawEvent));
1421 out->valuators.mask = next_block(&ptr, out->valuators.mask_len);
1422 memcpy(out->valuators.mask, in->valuators.mask, out->valuators.mask_len);
1424 out->valuators.values = next_block(&ptr, bits * sizeof(double));
1425 memcpy(out->valuators.values, in->valuators.values, bits * sizeof(double));
1427 out->raw_values = next_block(&ptr, bits * sizeof(double));
1428 memcpy(out->raw_values, in->raw_values, bits * sizeof(double));
1436 XInputCopyCookie(Display *dpy, XGenericEventCookie *in, XGenericEventCookie *out)
1440 XExtDisplayInfo *info = XInput_find_display(dpy);
1442 if (in->extension != info->codes->major_opcode)
1444 printf("XInputCopyCookie: wrong extension opcode %d\n",
1453 switch(in->evtype) {
1455 case XI_ButtonPress:
1456 case XI_ButtonRelease:
1460 case XI_TouchUpdate:
1462 ret = copyDeviceEvent(in, out);
1464 case XI_DeviceChanged:
1465 ret = copyDeviceChangedEvent(in, out);
1467 case XI_HierarchyChanged:
1468 ret = copyHierarchyEvent(in, out);
1474 ret = copyEnterEvent(in, out);
1476 case XI_PropertyEvent:
1477 ret = copyPropertyEvent(in, out);
1479 case XI_TouchOwnership:
1480 ret = copyTouchOwnershipEvent(in, out);
1482 case XI_RawKeyPress:
1483 case XI_RawKeyRelease:
1484 case XI_RawButtonPress:
1485 case XI_RawButtonRelease:
1487 ret = copyRawEvent(in, out);
1490 printf("XInputCopyCookie: unknown evtype %d\n", in->evtype);
1495 printf("XInputCopyCookie: Failed to copy evtype %d", in->evtype);
1500 wireToDeviceEvent(xXIDeviceEvent *in, XGenericEventCookie* cookie)
1508 ptr = (unsigned char*)&in[1] + in->buttons_len * 4;
1510 len = sizeDeviceEvent(in->buttons_len * 4, in->valuators_len * 4, ptr);
1512 cookie->data = ptr_lib = malloc(len);
1514 out = next_block(&ptr_lib, sizeof(XIDeviceEvent));
1515 out->display = cookie->display;
1516 out->type = in->type;
1517 out->extension = in->extension;
1518 out->evtype = in->evtype;
1519 out->send_event = ((in->type & 0x80) != 0);
1520 out->time = in->time;
1521 out->deviceid = in->deviceid;
1522 out->sourceid = in->sourceid;
1523 out->detail = in->detail;
1524 out->root = in->root;
1525 out->event = in->event;
1526 out->child = in->child;
1527 out->root_x = FP1616toDBL(in->root_x);
1528 out->root_y = FP1616toDBL(in->root_y);
1529 out->event_x = FP1616toDBL(in->event_x);
1530 out->event_y = FP1616toDBL(in->event_y);
1531 out->flags = in->flags;
1532 out->mods.base = in->mods.base_mods;
1533 out->mods.locked = in->mods.locked_mods;
1534 out->mods.latched = in->mods.latched_mods;
1535 out->mods.effective = in->mods.effective_mods;
1536 out->group.base = in->group.base_group;
1537 out->group.locked = in->group.locked_group;
1538 out->group.latched = in->group.latched_group;
1539 out->group.effective = in->group.effective_group;
1540 out->buttons.mask_len = in->buttons_len * 4;
1541 out->valuators.mask_len = in->valuators_len * 4;
1543 out->buttons.mask = next_block(&ptr_lib, out->buttons.mask_len);
1546 ptr = (unsigned char*)&in[1];
1547 memcpy(out->buttons.mask, ptr, out->buttons.mask_len);
1548 ptr += in->buttons_len * 4;
1551 out->valuators.mask = next_block(&ptr_lib, out->valuators.mask_len);
1552 memcpy(out->valuators.mask, ptr, out->valuators.mask_len);
1553 ptr += in->valuators_len * 4;
1555 len = count_bits(out->valuators.mask, out->valuators.mask_len);
1556 out->valuators.values = next_block(&ptr_lib, len * sizeof(double));
1558 values = (FP3232*)ptr;
1559 for (i = 0; i < len; i++, values++)
1561 out->valuators.values[i] = values->integral;
1562 out->valuators.values[i] += ((double)values->frac / (1 << 16) / (1 << 16));
1570 size_classes(xXIAnyInfo* from, int nclasses)
1573 xXIAnyInfo *any_wire;
1576 /* len for to->classes */
1577 len = pad_to_double(nclasses * sizeof(XIAnyClassInfo*));
1578 ptr_wire = (char*)from;
1579 for (i = 0; i < nclasses; i++)
1582 any_wire = (xXIAnyInfo*)ptr_wire;
1583 switch(any_wire->type)
1586 l = sizeDeviceClassType(XIButtonClass,
1587 ((xXIButtonInfo*)any_wire)->num_buttons);
1590 l = sizeDeviceClassType(XIKeyClass,
1591 ((xXIKeyInfo*)any_wire)->num_keycodes);
1593 case XIValuatorClass:
1594 l = sizeDeviceClassType(XIValuatorClass, 0);
1597 l = sizeDeviceClassType(XIScrollClass, 0);
1600 l = sizeDeviceClassType(XITouchClass, 0);
1605 ptr_wire += any_wire->length * 4;
1611 /* Copy classes from any into to->classes and return the number of bytes
1612 * copied. Memory layout of to->classes is
1613 * [clsptr][clsptr][clsptr][classinfo][classinfo]...
1614 * |________|___________^
1615 * |______________________^
1618 copy_classes(XIDeviceInfo* to, xXIAnyInfo* from, int *nclasses)
1620 XIAnyClassInfo *any_lib;
1621 xXIAnyInfo *any_wire;
1630 ptr_wire = (char*)from;
1631 ptr_lib = to->classes;
1632 to->classes = next_block(&ptr_lib,
1633 pad_to_double((*nclasses) * sizeof(XIAnyClassInfo*)));
1634 memset(to->classes, 0, (*nclasses) * sizeof(XIAnyClassInfo*));
1635 len = 0; /* count wire length */
1637 for (i = 0; i < *nclasses; i++)
1639 any_lib = (XIAnyClassInfo*)ptr_lib;
1640 any_wire = (xXIAnyInfo*)ptr_wire;
1642 switch(any_wire->type)
1646 XIButtonClassInfo *cls_lib;
1647 xXIButtonInfo *cls_wire;
1655 cls_wire = (xXIButtonInfo*)any_wire;
1656 sizeXIButtonClassType(cls_wire->num_buttons,
1657 &struct_size, &state_size,
1659 cls_lib = next_block(&ptr_lib, struct_size);
1660 wire_mask_size = ((cls_wire->num_buttons + 7)/8 + 3)/4 * 4;
1662 cls_lib->type = cls_wire->type;
1663 cls_lib->sourceid = cls_wire->sourceid;
1664 cls_lib->num_buttons = cls_wire->num_buttons;
1665 cls_lib->state.mask_len = state_size;
1666 cls_lib->state.mask = next_block(&ptr_lib, state_size);
1667 memcpy(cls_lib->state.mask, &cls_wire[1],
1669 if (state_size != wire_mask_size)
1670 memset(&cls_lib->state.mask[wire_mask_size], 0,
1671 state_size - wire_mask_size);
1673 cls_lib->labels = next_block(&ptr_lib, labels_size);
1675 atoms =(uint32_t*)((char*)&cls_wire[1] + wire_mask_size);
1676 for (j = 0; j < cls_lib->num_buttons; j++)
1677 cls_lib->labels[j] = *atoms++;
1679 to->classes[cls_idx++] = any_lib;
1684 XIKeyClassInfo *cls_lib;
1685 xXIKeyInfo *cls_wire;
1689 cls_wire = (xXIKeyInfo*)any_wire;
1690 sizeXIKeyClassType(cls_wire->num_keycodes,
1691 &struct_size, &keycodes_size);
1692 cls_lib = next_block(&ptr_lib, struct_size);
1694 cls_lib->type = cls_wire->type;
1695 cls_lib->sourceid = cls_wire->sourceid;
1696 cls_lib->num_keycodes = cls_wire->num_keycodes;
1697 cls_lib->keycodes = next_block(&ptr_lib, keycodes_size);
1698 memcpy(cls_lib->keycodes, &cls_wire[1],
1699 cls_lib->num_keycodes);
1701 to->classes[cls_idx++] = any_lib;
1704 case XIValuatorClass:
1706 XIValuatorClassInfo *cls_lib;
1707 xXIValuatorInfo *cls_wire;
1710 next_block(&ptr_lib,
1711 sizeDeviceClassType(XIValuatorClass, 0));
1712 cls_wire = (xXIValuatorInfo*)any_wire;
1714 cls_lib->type = cls_wire->type;
1715 cls_lib->sourceid = cls_wire->sourceid;
1716 cls_lib->number = cls_wire->number;
1717 cls_lib->label = cls_wire->label;
1718 cls_lib->resolution = cls_wire->resolution;
1719 cls_lib->min = cls_wire->min.integral;
1720 cls_lib->max = cls_wire->max.integral;
1721 cls_lib->value = cls_wire->value.integral;
1722 /* FIXME: fractional parts */
1723 cls_lib->mode = cls_wire->mode;
1725 to->classes[cls_idx++] = any_lib;
1730 XIScrollClassInfo *cls_lib;
1731 xXIScrollInfo *cls_wire;
1734 next_block(&ptr_lib,
1735 sizeDeviceClassType(XIScrollClass, 0));
1736 cls_wire = (xXIScrollInfo*)any_wire;
1738 cls_lib->type = cls_wire->type;
1739 cls_lib->sourceid = cls_wire->sourceid;
1740 cls_lib->number = cls_wire->number;
1741 cls_lib->scroll_type= cls_wire->scroll_type;
1742 cls_lib->flags = cls_wire->flags;
1743 cls_lib->increment = cls_wire->increment.integral;
1744 cls_lib->increment += (unsigned int)cls_wire->increment.frac/(double)(1ULL << 32);
1746 to->classes[cls_idx++] = any_lib;
1751 XITouchClassInfo *cls_lib;
1752 xXITouchInfo *cls_wire;
1754 cls_wire = (xXITouchInfo*)any_wire;
1755 cls_lib = next_block(&ptr_lib, sizeof(XITouchClassInfo));
1757 cls_lib->type = cls_wire->type;
1758 cls_lib->sourceid = cls_wire->sourceid;
1759 cls_lib->mode = cls_wire->mode;
1760 cls_lib->num_touches = cls_wire->num_touches;
1762 to->classes[cls_idx++] = any_lib;
1766 len += any_wire->length * 4;
1767 ptr_wire += any_wire->length * 4;
1770 /* we may have skipped unknown classes, reset nclasses */
1771 *nclasses = cls_idx;
1777 wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XGenericEventCookie *cookie)
1779 XIDeviceChangedEvent *out;
1782 int nclasses = in->num_classes;
1784 len = size_classes((xXIAnyInfo*)&in[1], in->num_classes);
1786 cookie->data = out = malloc(sizeof(XIDeviceChangedEvent) + len);
1788 out->type = in->type;
1789 out->display = cookie->display;
1790 out->extension = in->extension;
1791 out->evtype = in->evtype;
1792 out->send_event = ((in->type & 0x80) != 0);
1793 out->time = in->time;
1794 out->deviceid = in->deviceid;
1795 out->sourceid = in->sourceid;
1796 out->reason = in->reason;
1798 out->classes = (XIAnyClassInfo**)&out[1];
1800 info.classes = out->classes;
1802 copy_classes(&info, (xXIAnyInfo*)&in[1], &nclasses);
1803 out->num_classes = nclasses;
1809 wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XGenericEventCookie *cookie)
1812 XIHierarchyInfo *info_out;
1813 xXIHierarchyInfo *info_in;
1814 XIHierarchyEvent *out;
1816 cookie->data = out = malloc(sizeof(XIHierarchyEvent) + in->num_info * sizeof(XIHierarchyInfo));;
1818 out->info = (XIHierarchyInfo*)&out[1];
1819 out->display = cookie->display;
1820 out->type = in->type;
1821 out->extension = in->extension;
1822 out->evtype = in->evtype;
1823 out->send_event = ((in->type & 0x80) != 0);
1824 out->time = in->time;
1825 out->flags = in->flags;
1826 out->num_info = in->num_info;
1828 info_out = out->info;
1829 info_in = (xXIHierarchyInfo*)&in[1];
1831 for (i = 0; i < out->num_info; i++, info_out++, info_in++)
1833 info_out->deviceid = info_in->deviceid;
1834 info_out->attachment = info_in->attachment;
1835 info_out->use = info_in->use;
1836 info_out->enabled = info_in->enabled;
1837 info_out->flags = info_in->flags;
1844 wireToRawEvent(XExtDisplayInfo *info, xXIRawEvent *in, XGenericEventCookie *cookie)
1851 len = sizeof(XIRawEvent) + in->valuators_len * 4;
1852 bits = count_bits((unsigned char*)&in[1], in->valuators_len * 4);
1853 len += bits * sizeof(double) * 2; /* raw + normal */
1855 cookie->data = ptr = calloc(1, len);
1859 out = next_block(&ptr, sizeof(XIRawEvent));
1860 out->type = in->type;
1861 out->display = cookie->display;
1862 out->extension = in->extension;
1863 out->evtype = in->evtype;
1864 out->send_event = ((in->type & 0x80) != 0);
1865 out->time = in->time;
1866 out->detail = in->detail;
1867 out->deviceid = in->deviceid;
1868 out->flags = in->flags;
1870 /* https://bugs.freedesktop.org/show_bug.cgi?id=34240 */
1871 if (_XiCheckVersion(info, XInput_2_2) >= 0)
1872 out->sourceid = in->sourceid;
1876 out->valuators.mask_len = in->valuators_len * 4;
1877 out->valuators.mask = next_block(&ptr, out->valuators.mask_len);
1878 memcpy(out->valuators.mask, &in[1], out->valuators.mask_len);
1880 out->valuators.values = next_block(&ptr, bits * sizeof(double));
1881 out->raw_values = next_block(&ptr, bits * sizeof(double));
1883 values = (FP3232*)(((char*)&in[1]) + in->valuators_len * 4);
1884 for (i = 0; i < bits; i++)
1886 out->valuators.values[i] = values->integral;
1887 out->valuators.values[i] += ((double)values->frac / (1 << 16) / (1 << 16));
1888 out->raw_values[i] = (values + bits)->integral;
1889 out->raw_values[i] += ((double)(values + bits)->frac / (1 << 16) / (1 << 16));
1896 /* Memory layout of XIEnterEvents:
1897 [event][modifiers][group][button]
1900 wireToEnterLeave(xXIEnterEvent *in, XGenericEventCookie *cookie)
1905 len = sizeof(XIEnterEvent) + in->buttons_len * 4;
1907 cookie->data = out = malloc(len);
1908 out->buttons.mask = (unsigned char*)&out[1];
1910 out->type = in->type;
1911 out->display = cookie->display;
1912 out->extension = in->extension;
1913 out->evtype = in->evtype;
1914 out->send_event = ((in->type & 0x80) != 0);
1915 out->time = in->time;
1916 out->detail = in->detail;
1917 out->deviceid = in->deviceid;
1918 out->root = in->root;
1919 out->event = in->event;
1920 out->child = in->child;
1921 out->sourceid = in->sourceid;
1922 out->root_x = FP1616toDBL(in->root_x);
1923 out->root_y = FP1616toDBL(in->root_y);
1924 out->event_x = FP1616toDBL(in->event_x);
1925 out->event_y = FP1616toDBL(in->event_y);
1926 out->mode = in->mode;
1927 out->focus = in->focus;
1928 out->same_screen = in->same_screen;
1930 out->mods.base = in->mods.base_mods;
1931 out->mods.locked = in->mods.locked_mods;
1932 out->mods.latched = in->mods.latched_mods;
1933 out->mods.effective = in->mods.effective_mods;
1934 out->group.base = in->group.base_group;
1935 out->group.locked = in->group.locked_group;
1936 out->group.latched = in->group.latched_group;
1937 out->group.effective = in->group.effective_group;
1939 out->buttons.mask_len = in->buttons_len * 4;
1940 memcpy(out->buttons.mask, &in[1], out->buttons.mask_len);
1946 wireToPropertyEvent(xXIPropertyEvent *in, XGenericEventCookie *cookie)
1948 XIPropertyEvent *out = malloc(sizeof(XIPropertyEvent));
1952 out->type = in->type;
1953 out->extension = in->extension;
1954 out->evtype = in->evtype;
1955 out->send_event = ((in->type & 0x80) != 0);
1956 out->time = in->time;
1957 out->property = in->property;
1958 out->what = in->what;
1959 out->deviceid = in->deviceid;
1965 wireToTouchOwnershipEvent(xXITouchOwnershipEvent *in,
1966 XGenericEventCookie *cookie)
1968 XITouchOwnershipEvent *out = malloc(sizeof(XITouchOwnershipEvent));
1972 out->type = in->type;
1973 out->display = cookie->display;
1974 out->extension = in->extension;
1975 out->evtype = in->evtype;
1976 out->send_event = ((in->type & 0x80) != 0);
1977 out->time = in->time;
1978 out->deviceid = in->deviceid;
1979 out->sourceid = in->sourceid;
1980 out->touchid = in->touchid;
1981 out->root = in->root;
1982 out->event = in->event;
1983 out->child = in->child;
1984 out->flags = in->flags;