[clutter/wip/wayland-compositor-help: 2/5] evdev: add callback to constrain the pointer position



commit d3736da8bd4bea2d096fcab4e7e70fd47b84b7dc
Author: Giovanni Campagna <gcampagn redhat com>
Date:   Fri Aug 23 12:15:40 2013 +0200

    evdev: add callback to constrain the pointer position
    
    Add a new callback that is called prior to emitting pointer
    motion events and that can modify the new pointer position.
    The main purpose is allowing multiscreen apps to prevent the
    pointer for entering the dead area that exists when the screens
    are not the same size, but it could also used to implement
    pointer barriers.
    A callback is needed to make sure that the hook is called early
    enough and the Clutter state is always consistent.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=706652

 clutter/evdev/clutter-device-manager-evdev.c |   44 ++++++++++++++++++++++++++
 clutter/evdev/clutter-evdev.h                |   24 ++++++++++++++
 2 files changed, 68 insertions(+), 0 deletions(-)
---
diff --git a/clutter/evdev/clutter-device-manager-evdev.c b/clutter/evdev/clutter-device-manager-evdev.c
index dd503bb..a2994fe 100644
--- a/clutter/evdev/clutter-device-manager-evdev.c
+++ b/clutter/evdev/clutter-device-manager-evdev.c
@@ -73,6 +73,10 @@ struct _ClutterDeviceManagerEvdevPrivate
   ClutterInputDevice *core_pointer;
   ClutterInputDevice *core_keyboard;
 
+  ClutterPointerConstrainCallback constrain_callback;
+  gpointer                        constrain_data;
+  GDestroyNotify                  constrain_data_notify;
+
   ClutterStageManager *stage_manager;
   guint stage_added_handler;
   guint stage_removed_handler;
@@ -262,6 +266,10 @@ notify_relative_motion (ClutterEventSource *source,
   new_x = CLAMP (new_x, 0.f, stage_width - 1);
   new_y = CLAMP (new_y, 0.f, stage_height - 1);
 
+  if (manager_evdev->priv->constrain_callback)
+    manager_evdev->priv->constrain_callback (manager_evdev->priv->core_pointer, &new_x, &new_y,
+                                            manager_evdev->priv->constrain_data);
+
   event->motion.time = time_;
   event->motion.stage = stage;
   event->motion.device = manager_evdev->priv->core_pointer;
@@ -1029,6 +1037,9 @@ clutter_device_manager_evdev_finalize (GObject *object)
     }
   g_slist_free (priv->event_sources);
 
+  if (priv->constrain_data_notify)
+    priv->constrain_data_notify (priv->constrain_data);
+
   G_OBJECT_CLASS (clutter_device_manager_evdev_parent_class)->finalize (object);
 }
 
@@ -1313,3 +1324,36 @@ clutter_evdev_set_keyboard_map (ClutterDeviceManager *evdev,
   for (i = 0; i < priv->keys->len; i++)
     xkb_state_update_key (priv->xkb, g_array_index (priv->keys, guint32, i), XKB_KEY_DOWN);
 }
+
+/**
+ * clutter_evdev_set_pointer_constrain_callback:
+ * @evdev: the #ClutterDeviceManager created by the evdev backend
+ * @callback: the callback
+ * @user_data:
+ * @user_data_notify:
+ *
+ * Sets a callback to be invoked for every pointer motion. The callback
+ * can then modify the new pointer coordinates to constrain movement within
+ * a specific region.
+ */
+void
+clutter_evdev_set_pointer_constrain_callback (ClutterDeviceManager            *evdev,
+                                             ClutterPointerConstrainCallback  callback,
+                                             gpointer                         user_data,
+                                             GDestroyNotify                   user_data_notify)
+{
+  ClutterDeviceManagerEvdev *manager_evdev;
+  ClutterDeviceManagerEvdevPrivate *priv;
+
+  g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (evdev));
+
+  manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (evdev);
+  priv = manager_evdev->priv;
+
+  if (priv->constrain_data_notify)
+    priv->constrain_data_notify (priv->constrain_data);
+
+  priv->constrain_callback = callback;
+  priv->constrain_data = user_data;
+  priv->constrain_data_notify = user_data_notify;
+}
diff --git a/clutter/evdev/clutter-evdev.h b/clutter/evdev/clutter-evdev.h
index fea26cb..aea19b9 100644
--- a/clutter/evdev/clutter-evdev.h
+++ b/clutter/evdev/clutter-evdev.h
@@ -51,6 +51,30 @@ void  clutter_evdev_set_open_callback (ClutterOpenDeviceCallback callback,
 void  clutter_evdev_release_devices (void);
 void  clutter_evdev_reclaim_devices (void);
 
+/**
+ * ClutterPointerConstrainCallback:
+ * @device: the core pointer device
+ * @x: (inout): the new X coordinate
+ * @y: (inout): the new Y coordinate
+ * @user_data:
+ *
+ * This callback will be called for all pointer motion events, and should
+ * update (@x, @y) to constrain the pointer position appropriately.
+ * The subsequent motion event will use the updated values as the new coordinates.
+ * Note that the coordinates are already clamped to the stage size.
+ * Also note that the event will be emitted even if the pointer is constrained
+ * to be in the same position.
+ */
+typedef void (*ClutterPointerConstrainCallback) (ClutterInputDevice *device,
+                                                float              *x,
+                                                float              *y,
+                                                gpointer            user_data);
+
+void  clutter_evdev_set_pointer_constrain_callback (ClutterDeviceManager            *evdev,
+                                                   ClutterPointerConstrainCallback  callback,
+                                                   gpointer                         user_data,
+                                                   GDestroyNotify                   user_data_notify);
+
 struct xkb_state * clutter_evdev_get_keyboard_state (ClutterDeviceManager *evdev);
 void               clutter_evdev_set_keyboard_map   (ClutterDeviceManager *evdev,
                                                     struct xkb_keymap    *keymap);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]