[PATCH] Use cookie events for XI2 events.

Peter Hutterer peter.hutterer at who-t.net
Wed Jul 1 18:41:40 PDT 2009


---
 src/XExtInt.c |  295 +++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 204 insertions(+), 91 deletions(-)

diff --git a/src/XExtInt.c b/src/XExtInt.c
index 6eba270..0851251 100644
--- a/src/XExtInt.c
+++ b/src/XExtInt.c
@@ -124,19 +124,24 @@ static Bool XInputWireToEvent(
     XEvent *		/* re */,
     xEvent *		/* event */
 );
+static Bool XInputWireToCookie(
+    Display*	        /* display */,
+    XGenericEventCookie*	/* re */,
+    xEvent*	        /* event */
+);
 
 static int
-wireToDeviceEvent(xXIDeviceEvent *in, XIDeviceEvent* out);
+wireToDeviceEvent(xXIDeviceEvent *in, XGenericEventCookie* out);
 static int
-wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XIDeviceChangedEvent* out);
+wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XGenericEventCookie *cookie);
 static int
-wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XIHierarchyEvent* out);
+wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XGenericEventCookie *cookie);
 static int
-wireToRawEvent(xXIRawEvent *in, XIRawEvent *out);
+wireToRawEvent(xXIRawEvent *in, XGenericEventCookie *cookie);
 static int
-wireToEnterLeave(xXIEnterEvent *in, XIEnterEvent *out);
+wireToEnterLeave(xXIEnterEvent *in, XGenericEventCookie *cookie);
 static int
-wireToPropertyEvent(xXIPropertyEvent *in, XIPropertyEvent *out);
+wireToPropertyEvent(xXIPropertyEvent *in, XGenericEventCookie *cookie);
 
 static /* const */ XEvent emptyevent;
 
@@ -163,9 +168,20 @@ static char *XInputErrorList[] = {
 };
 
 _X_HIDDEN
-XEXT_GENERATE_FIND_DISPLAY(XInput_find_display, xinput_info,
-			   xinput_extension_name, &xinput_extension_hooks,
-			   IEVENTS, NULL)
+XExtDisplayInfo *XInput_find_display (Display *dpy)
+{
+    XExtDisplayInfo *dpyinfo;
+    if (!xinput_info) { if (!(xinput_info = XextCreateExtension())) return NULL; }
+    if (!(dpyinfo = XextFindDisplay (xinput_info, dpy)))
+    {
+      dpyinfo = XextAddDisplay (xinput_info, dpy,
+                                xinput_extension_name,
+                                &xinput_extension_hooks,
+                                IEVENTS, NULL);
+      XESetWireToEventCookie(dpy, dpyinfo->codes->major_opcode, XInputWireToCookie);
+    }
+    return dpyinfo;
+}
 
 static XEXT_GENERATE_ERROR_STRING(XInputError, xinput_extension_name,
                                   IERRORS, XInputErrorList)
@@ -780,86 +796,111 @@ XInputWireToEvent(
                 printf("XInputWireToEvent: UNKNOWN WIRE EVENT! type=%d\n", type);
                 break;
         }
-    } else /* if type == GenericEvent */
+    }
+    return (DONT_ENQUEUE);
+}
+
+static void xge_copy_to_cookie(xGenericEvent* ev,
+                               XGenericEventCookie *cookie)
+{
+    cookie->type = ev->type;
+    cookie->evtype = ev->evtype;
+    cookie->extension = ev->extension;
+}
+
+static Bool
+XInputWireToCookie(
+    Display	*dpy,
+    XGenericEventCookie *cookie,
+    xEvent	*event)
+{
+    XExtDisplayInfo *info = XInput_find_display(dpy);
+    XEvent *save = (XEvent *) info->data;
+    xGenericEvent* ge = (xGenericEvent*)event;
+
+    if (ge->extension != info->codes->major_opcode)
     {
-        xGenericEvent* ge = (xGenericEvent*)event;
-        if (ge->extension == info->codes->major_opcode)
-        {
-            switch(ge->evtype)
-            {
-                case XI_Motion:
-                case XI_ButtonPress:
-                case XI_ButtonRelease:
-                case XI_KeyPress:
-                case XI_KeyRelease:
-                    *re = *save;
-                    if (!wireToDeviceEvent((xXIDeviceEvent*)event, (XIDeviceEvent*)re))
-                    {
-                        printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
-                                ge->evtype);
-                        break;
-                    }
-                    return ENQUEUE_EVENT;
-                case XI_DeviceChanged:
-                    *re = *save;
-                    if (!wireToDeviceChangedEvent((xXIDeviceChangedEvent*)event,
-                                                   (XIDeviceChangedEvent*)re))
-                    {
-                        printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
-                                ge->evtype);
-                        break;
-                    }
-                    return ENQUEUE_EVENT;
-                case XI_HierarchyChanged:
-                    *re = *save;
-                    if (!wireToHierarchyChangedEvent((xXIHierarchyEvent*)event,
-                                                      (XIHierarchyEvent*)re))
-                    {
-                        printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
-                                ge->evtype);
-                        break;
-                    }
-                    return ENQUEUE_EVENT;
+        printf("XInputWireToEventCookie: wrong extension opcode %d\n",
+                ge->extension);
+        return DONT_ENQUEUE;
+    }
 
-                case XI_RawEvent:
-                    *re = *save;
-                    if (!wireToRawEvent((xXIRawEvent*)event, (XIRawEvent*)re))
-                    {
-                        printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
-                                ge->evtype);
-                        break;
-                    }
-                    return ENQUEUE_EVENT;
-                case XI_Enter:
-                case XI_Leave:
-                case XI_FocusIn:
-                case XI_FocusOut:
-                    *re = *save;
-                    if (!wireToEnterLeave((xXIEnterEvent*)event,
-                                          (XIEnterEvent*)re))
-                    {
-                        printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
-                                ge->evtype);
-                        break;
-                    }
-                    return ENQUEUE_EVENT;
-                case XI_PropertyEvent:
-                    *re = *save;
-                    if (!wireToPropertyEvent((xXIPropertyEvent*)event,
-                                            (XIPropertyEvent*)re))
-                    {
-                        printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
-                                ge->evtype);
-                        break;
-                    }
-                    return ENQUEUE_EVENT;
-                default:
-                    printf("XInputWireToEvent: Unknown generic event. type %d\n", ge->evtype);
+    *save = emptyevent;
+    save->type = event->u.u.type;
+    ((XAnyEvent*)save)->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
+    ((XAnyEvent*)save)->send_event = ((event->u.u.type & 0x80) != 0);
+    ((XAnyEvent*)save)->display = dpy;
 
+    xge_copy_to_cookie((xGenericEvent*)event, (XGenericEventCookie*)save);
+    switch(ge->evtype)
+    {
+        case XI_Motion:
+        case XI_ButtonPress:
+        case XI_ButtonRelease:
+        case XI_KeyPress:
+        case XI_KeyRelease:
+            *cookie = *(XGenericEventCookie*)save;
+            if (!wireToDeviceEvent((xXIDeviceEvent*)event, cookie))
+            {
+                printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
+                        ge->evtype);
+                break;
             }
-        }
+            return ENQUEUE_EVENT;
+        case XI_DeviceChanged:
+            *cookie = *(XGenericEventCookie*)save;
+            if (!wireToDeviceChangedEvent((xXIDeviceChangedEvent*)event, cookie))
+            {
+                printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
+                        ge->evtype);
+                break;
+            }
+            return ENQUEUE_EVENT;
+        case XI_HierarchyChanged:
+            *cookie = *(XGenericEventCookie*)save;
+            if (!wireToHierarchyChangedEvent((xXIHierarchyEvent*)event, cookie))
+            {
+                printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
+                        ge->evtype);
+                break;
+            }
+            return ENQUEUE_EVENT;
+
+        case XI_RawEvent:
+            *cookie = *(XGenericEventCookie*)save;
+            if (!wireToRawEvent((xXIRawEvent*)event, cookie))
+            {
+                printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
+                        ge->evtype);
+                break;
+            }
+            return ENQUEUE_EVENT;
+        case XI_Enter:
+        case XI_Leave:
+        case XI_FocusIn:
+        case XI_FocusOut:
+            *cookie = *(XGenericEventCookie*)save;
+            if (!wireToEnterLeave((xXIEnterEvent*)event, cookie))
+            {
+                printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
+                        ge->evtype);
+                break;
+            }
+            return ENQUEUE_EVENT;
+        case XI_PropertyEvent:
+            *cookie = *(XGenericEventCookie*)save;
+            if (!wireToPropertyEvent((xXIPropertyEvent*)event, cookie))
+            {
+                printf("XInputWireToEvent: CONVERSION FAILURE!  evtype=%d\n",
+                        ge->evtype);
+                break;
+            }
+            return ENQUEUE_EVENT;
+        default:
+            printf("XInputWireToEvent: Unknown generic event. type %d\n", ge->evtype);
+
     }
-    return (DONT_ENQUEUE);
+    return DONT_ENQUEUE;
 }
 
 static int count_bits(unsigned char* ptr, int len)
@@ -880,13 +921,27 @@ static int count_bits(unsigned char* ptr, int len)
     return bits;
 }
 
-/* Keep this in sync with XIFreeEventData() */
+static void
+freeDeviceEvent(void *ev)
+{
+    XIDeviceEvent *event = (XIDeviceEvent*)ev;
+    free(event->valuators);
+    free(event->buttons);
+    free(event->mods);
+    free(event->group);
+    free(event);
+}
+
 static int
-wireToDeviceEvent(xXIDeviceEvent *in, XIDeviceEvent* out)
+wireToDeviceEvent(xXIDeviceEvent *in, XGenericEventCookie* cookie)
 {
     int len, i;
     unsigned char *ptr;
     FP3232 *values;
+    XIDeviceEvent *out = malloc(sizeof(XIDeviceEvent));
+
+    cookie->data = out;
+    cookie->free_event = freeDeviceEvent;
 
     out->type = in->type;
     out->extension = in->extension;
@@ -946,11 +1001,23 @@ wireToDeviceEvent(xXIDeviceEvent *in, XIDeviceEvent* out)
     return 1;
 }
 
+static void
+freeDeviceChangedEvent(void *ev)
+{
+    XIDeviceChangedEvent *event = (XIDeviceChangedEvent*)ev;
+    free(event->classes);
+    free(event);
+}
 
 static int
-wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XIDeviceChangedEvent* out)
+wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XGenericEventCookie *cookie)
 {
     XIDeviceInfo info;
+
+    XIDeviceChangedEvent *out = malloc(sizeof(XIDeviceChangedEvent));
+    cookie->data = out;
+    cookie->free_event = freeDeviceChangedEvent;
+
     out->type = in->type;
     out->extension = in->extension;
     out->evtype = in->evtype;
@@ -966,12 +1033,24 @@ wireToDeviceChangedEvent(xXIDeviceChangedEvent *in, XIDeviceChangedEvent* out)
     return 1;
 }
 
+static void
+freeHierarchyEvent(void *ev)
+{
+    XIHierarchyEvent *event = (XIHierarchyEvent*)ev;
+    free(event->info);
+    free(event);
+}
+
 static int
-wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XIHierarchyEvent* out)
+wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XGenericEventCookie *cookie)
 {
     int i;
     XIHierarchyInfo *info_out;
     xXIHierarchyInfo *info_in;
+    XIHierarchyEvent *out = malloc(sizeof(XIHierarchyEvent));
+
+    cookie->data = out;
+    cookie->free_event = freeHierarchyEvent;
 
     out->info = Xmalloc(in->num_info * sizeof(XIHierarchyInfo));
     out->type           = in->type;
@@ -996,11 +1075,25 @@ wireToHierarchyChangedEvent(xXIHierarchyEvent *in, XIHierarchyEvent* out)
     return 1;
 }
 
+static void
+freeRawEvent(void *ev)
+{
+    XIRawEvent *event = (XIRawEvent*)ev;
+    free(event->valuators->values);
+    free(event->valuators);
+    free(event->raw_values);
+    free(event);
+}
+
 static int
-wireToRawEvent(xXIRawEvent *in, XIRawEvent *out)
+wireToRawEvent(xXIRawEvent *in, XGenericEventCookie *cookie)
 {
     int len, i;
     FP3232 *values;
+    XIRawEvent *out = malloc(sizeof(XIRawEvent));
+
+    cookie->data = out;
+    cookie->free_event = freeRawEvent;
 
     out->type           = in->type;
     out->extension      = in->extension;
@@ -1030,9 +1123,24 @@ wireToRawEvent(xXIRawEvent *in, XIRawEvent *out)
     return 1;
 }
 
+static void
+freeEnterEvent(void *ev)
+{
+    XIEnterEvent *event = (XIEnterEvent*)ev;
+    free(event->mods);
+    free(event->group);
+    free(event->buttons);
+    free(event);
+}
+
 static int
-wireToEnterLeave(xXIEnterEvent *in, XIEnterEvent *out)
+wireToEnterLeave(xXIEnterEvent *in, XGenericEventCookie *cookie)
 {
+    XIEnterEvent *out = malloc(sizeof(XIEnterEvent));
+
+    cookie->data = out;
+    cookie->free_event = freeEnterEvent;
+
     out->type           = in->type;
     out->extension      = in->extension;
     out->evtype         = in->evtype;
@@ -1070,8 +1178,13 @@ wireToEnterLeave(xXIEnterEvent *in, XIEnterEvent *out)
 }
 
 static int
-wireToPropertyEvent(xXIPropertyEvent *in, XIPropertyEvent *out)
+wireToPropertyEvent(xXIPropertyEvent *in, XGenericEventCookie *cookie)
 {
+    XIPropertyEvent *out = malloc(sizeof(XIPropertyEvent));
+
+    cookie->data = out;
+    cookie->free_event = free;
+
     out->type           = in->type;
     out->extension      = in->extension;
     out->evtype         = in->evtype;
-- 
1.6.3.rc1.2.g0164.dirty



More information about the xorg-devel mailing list