[PATCH] Add XESetEventCookieToWire() handler

carlosg at gnome.org carlosg at gnome.org
Fri Nov 26 15:00:43 PST 2010


From: Carlos Garnacho <carlosg at gnome.org>

This handler transforms XI2 events to the wire protocol, enabling
XSendEvent() to work with these.

All device and raw events that make sense to be sent from one
client to another are translatable, other events should return
BadValue.

Signed-off-by: Carlos Garnacho <carlosg at gnome.org>
---
 src/XExtInt.c |  173 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 170 insertions(+), 3 deletions(-)

diff --git a/src/XExtInt.c b/src/XExtInt.c
index eed6637..05b4ff2 100644
--- a/src/XExtInt.c
+++ b/src/XExtInt.c
@@ -68,6 +68,11 @@ SOFTWARE.
 #define ENQUEUE_EVENT	True
 #define DONT_ENQUEUE	False
 #define FP1616toDBL(x) ((x) * 1.0 / (1 << 16))
+#define DBLtoFP1616(x) ((FP1616) ((x) * ((1 << 16) + 0.5)))
+
+#define DBLtoFP3232(x) ((int64_t) ((x) * (1 << 16) * (1 << 16) + 0.5))
+#define DBLtoFP3232frac(x) (DBLtoFP3232(x) & 0xffffffff);
+#define DBLtoFP3232integral(x) (DBLtoFP3232(x) >> 32);
 
 extern void _xibaddevice(
     Display *		/* dpy */,
@@ -127,6 +132,11 @@ static Bool XInputWireToCookie(
     XGenericEventCookie*	/* re */,
     xEvent*	        /* event */
 );
+static Bool XInputCookieToWire(
+    Display*	        /* display */,
+    XGenericEventCookie*	/* re */,
+    xGenericEvent**	        /* event */
+);
 
 static Bool XInputCopyCookie(
     Display*	        /* display */,
@@ -244,6 +254,7 @@ XExtDisplayInfo *XInput_find_display (Display *dpy)
       if (dpyinfo->codes) /* NULL if XI doesn't exist on the server */
       {
           XESetWireToEventCookie(dpy, dpyinfo->codes->major_opcode, XInputWireToCookie);
+          XESetEventCookieToWire(dpy, dpyinfo->codes->major_opcode, XInputCookieToWire);
           XESetCopyEventCookie(dpy, dpyinfo->codes->major_opcode, XInputCopyCookie);
       }
     }
@@ -888,7 +899,7 @@ XInputWireToEvent(
 static void xge_copy_to_cookie(xGenericEvent* ev,
                                XGenericEventCookie *cookie)
 {
-    cookie->type = ev->type;
+    cookie->type = ev->type & 0x7f;
     cookie->evtype = ev->evtype;
     cookie->extension = ev->extension;
 }
@@ -911,12 +922,11 @@ XInputWireToCookie(
     }
 
     *save = emptyevent;
-    save->type = event->u.u.type;
+    xge_copy_to_cookie((xGenericEvent*)event, (XGenericEventCookie*)save);
     ((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:
@@ -992,6 +1002,162 @@ XInputWireToCookie(
     return DONT_ENQUEUE;
 }
 
+static Bool
+XInputCookieToWire(
+    Display	*dpy,
+    XGenericEventCookie *re,
+    xGenericEvent **event)
+{
+    XExtDisplayInfo *info = XInput_find_display(dpy);
+    XGenericEventCookie *xcookie = (XGenericEventCookie *) re;
+
+    switch (((XGenericEvent*)re)->evtype)
+    {
+    case XI_Motion:
+    case XI_ButtonPress:
+    case XI_ButtonRelease:
+    case XI_KeyPress:
+    case XI_KeyRelease: {
+        register XIDeviceEvent *xev = (XIDeviceEvent *) xcookie->data;
+        register xXIDeviceEvent *wev = (xXIDeviceEvent *) event;
+        int buttons_len = (xev->buttons.mask_len + 3) >> 2;
+        int valuators_len = (xev->valuators.mask_len + 3) >> 2;
+        int i, n_valuators, len;
+        unsigned char *ptr;
+        FP3232 *values;
+
+        n_valuators = count_bits (xev->valuators.mask, xev->valuators.mask_len);
+
+        len = sizeof (xXIDeviceEvent);
+        len += buttons_len << 2;
+        len += valuators_len << 2;
+        len += n_valuators * sizeof (FP3232);
+
+        wev = (xXIDeviceEvent *) Xmalloc(len);
+
+        if (!wev)
+            return (_XUnknownEventCookie(dpy, re, event));
+
+        *event = (xGenericEvent *) wev;
+
+        wev->type = GenericEvent;
+        wev->extension = info->codes->major_opcode;
+        wev->sequenceNumber = xev->serial & 0xFFFF;
+        wev->length = (len - sizeof (xEvent) + 3) >> 2;
+        wev->evtype = xev->evtype;
+        wev->deviceid = xev->deviceid;
+        wev->time = xev->time;
+        wev->detail = xev->detail;
+        wev->root = xev->root;
+        wev->event = xev->event;
+        wev->child = xev->child;
+
+        if (xev->sourceid != 0)
+            wev->sourceid = xev->sourceid;
+        else
+            wev->sourceid = xev->deviceid;
+
+        wev->flags = xev->flags;
+
+        wev->root_x = DBLtoFP1616(xev->root_x);
+        wev->root_y = DBLtoFP1616(xev->root_y);
+        wev->event_x = DBLtoFP1616(xev->event_x);
+        wev->event_y = DBLtoFP1616(xev->event_y);
+
+        wev->mods.base_mods = xev->mods.base;
+        wev->mods.latched_mods = xev->mods.latched;
+        wev->mods.locked_mods = xev->mods.locked;
+        wev->mods.effective_mods = xev->mods.effective;
+
+        wev->group.base_group = xev->group.base;
+        wev->group.latched_group = xev->group.latched;
+        wev->group.locked_group = xev->group.locked;
+        wev->group.effective_group = xev->group.effective;
+
+        wev->buttons_len = (xev->buttons.mask_len + 3) >> 2;
+        wev->valuators_len = (xev->valuators.mask_len + 3) >> 2;
+
+        /* Fill in trailing button mask */
+        ptr = (unsigned char *) &wev[1];
+        memcpy (ptr, xev->buttons.mask, xev->buttons.mask_len);
+        ptr += wev->buttons_len << 2;
+
+        /* Fill in valuator mask and values */
+        memcpy (ptr, xev->valuators.mask, xev->valuators.mask_len);
+        ptr += wev->valuators_len << 2;
+
+        values = (FP3232 *) ptr;
+
+        for (i = 0; i < n_valuators; i++, values++) {
+            values->frac = DBLtoFP3232frac(xev->valuators.values[i]);
+            values->integral = DBLtoFP3232integral(xev->valuators.values[i]);
+        }
+
+        return True;
+    }
+    case XI_RawKeyPress:
+    case XI_RawKeyRelease:
+    case XI_RawButtonPress:
+    case XI_RawButtonRelease:
+    case XI_RawMotion: {
+        register XIRawEvent *rev = (XIRawEvent *) xcookie->data;
+        register xXIRawEvent *wev = (xXIRawEvent *) event;
+        int valuators_len = (rev->valuators.mask_len + 3) >> 2;
+        int i, n_valuators, len;
+        unsigned char *ptr;
+        FP3232 *values;
+
+        n_valuators = count_bits (rev->valuators.mask, rev->valuators.mask_len);
+
+        len = sizeof (xXIRawEvent);
+        len += valuators_len << 2;
+        len += n_valuators * sizeof (FP3232) * 2; /* raw + normal */
+
+        wev = (xXIRawEvent *) Xmalloc(len);
+
+        if (!wev)
+            return (_XUnknownEventCookie(dpy, re, event));
+
+        *event = (xGenericEvent *) wev;
+
+        wev->type = GenericEvent;
+        wev->extension = info->codes->major_opcode;
+        wev->sequenceNumber = rev->serial & 0xFFFF;
+        wev->length = (len - sizeof (xEvent) + 3) >> 2;
+        wev->evtype = rev->evtype;
+        wev->deviceid = rev->deviceid;
+        wev->time = rev->time;
+        wev->detail = rev->detail;
+        wev->flags = rev->flags;
+
+        wev->valuators_len = (rev->valuators.mask_len + 3) >> 2;
+
+        /* Fill in trailing valuator mask and values */
+        ptr = (unsigned char *) &wev[1];
+        memcpy (ptr, rev->valuators.mask, rev->valuators.mask_len);
+        ptr += wev->valuators_len << 2;
+
+        values = (FP3232 *) ptr;
+
+        /* Normal values */
+        for (i = 0; i < n_valuators; i++, values++) {
+            values->frac = DBLtoFP3232frac(rev->valuators.values[i]);
+            values->integral = DBLtoFP3232integral(rev->valuators.values[i]);
+        }
+
+        /* Raw values */
+        for (i = 0; i < n_valuators; i++, values++) {
+            values->frac = DBLtoFP3232frac(rev->raw_values[i]);
+            values->integral = DBLtoFP3232integral(rev->raw_values[i]);
+        }
+
+        return True;
+    }
+    }
+
+    return False;
+}
+
 /**
  * Calculate length in bytes needed for the device event with the given
  * button mask length, valuator mask length + valuator mask. All parameters
@@ -1711,3 +1877,4 @@ wireToPropertyEvent(xXIPropertyEvent *in, XGenericEventCookie *cookie)
 
     return 1;
 }
+
-- 
1.7.3.2



More information about the xorg-devel mailing list