[mutter/wip/carlosg/input-thread: 11/95] backends/native: Use libinput seat slot API




commit 07e94acf292b582fcde3ceca64917d0c13b5c194
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu May 7 12:41:32 2020 +0200

    backends/native: Use libinput seat slot API
    
    Instead of creating a seat-wide touch slot ID ourselves, rely on libinput
    API doing this for us.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1403

 src/backends/native/meta-seat-native.c             | 97 +++++++++-------------
 src/backends/native/meta-seat-native.h             |  9 +-
 .../native/meta-virtual-input-device-native.c      |  3 +-
 3 files changed, 44 insertions(+), 65 deletions(-)
---
diff --git a/src/backends/native/meta-seat-native.c b/src/backends/native/meta-seat-native.c
index e8e76b5710..3d956ecab2 100644
--- a/src/backends/native/meta-seat-native.c
+++ b/src/backends/native/meta-seat-native.c
@@ -146,79 +146,56 @@ meta_seat_native_sync_leds (MetaSeatNative *seat)
     }
 }
 
-static void
-clutter_touch_state_free (MetaTouchState *touch_state)
+MetaTouchState *
+meta_seat_native_lookup_touch_state (MetaSeatNative *seat,
+                                     int             seat_slot)
 {
-  g_slice_free (MetaTouchState, touch_state);
+  if (!seat->touch_states)
+    return NULL;
+  return g_hash_table_lookup (seat->touch_states, GINT_TO_POINTER (seat_slot));
 }
 
 static void
-ensure_seat_slot_allocated (MetaSeatNative *seat,
-                            int             seat_slot)
-{
-  if (seat_slot >= seat->n_alloc_touch_states)
-    {
-      const int size_increase = 5;
-      int i;
-
-      seat->n_alloc_touch_states += size_increase;
-      seat->touch_states = g_realloc_n (seat->touch_states,
-                                        seat->n_alloc_touch_states,
-                                        sizeof (MetaTouchState *));
-      for (i = 0; i < size_increase; i++)
-        seat->touch_states[seat->n_alloc_touch_states - (i + 1)] = NULL;
-    }
-}
-
-MetaTouchState *
-meta_seat_native_lookup_touch_state (MetaSeatNative *seat,
-                                     int             device_slot)
+meta_touch_state_free (MetaTouchState *state)
 {
-  int seat_slot;
-
-  for (seat_slot = 0; seat_slot < seat->n_alloc_touch_states; seat_slot++)
-    {
-      if (seat->touch_states[seat_slot] &&
-          seat->touch_states[seat_slot]->device_slot == device_slot)
-        return seat->touch_states[seat_slot];
-    }
-
-  return NULL;
+  g_slice_free (MetaTouchState, state);
 }
 
 MetaTouchState *
 meta_seat_native_acquire_touch_state (MetaSeatNative *seat,
-                                      int             device_slot)
+                                      int             seat_slot)
 {
   MetaTouchState *touch_state;
-  int seat_slot;
 
-  for (seat_slot = 0; seat_slot < seat->n_alloc_touch_states; seat_slot++)
+  if (!seat->touch_states)
     {
-      if (!seat->touch_states[seat_slot])
-        break;
+      seat->touch_states =
+        g_hash_table_new_full (NULL, NULL, NULL,
+                               (GDestroyNotify) meta_touch_state_free);
     }
 
-  ensure_seat_slot_allocated (seat, seat_slot);
+  g_assert (!g_hash_table_contains (seat->touch_states,
+                                    GINT_TO_POINTER (seat_slot)));
 
   touch_state = g_slice_new0 (MetaTouchState);
   *touch_state = (MetaTouchState) {
     .seat = seat,
     .seat_slot = seat_slot,
-    .device_slot = device_slot,
   };
 
-  seat->touch_states[seat_slot] = touch_state;
+  g_hash_table_insert (seat->touch_states, GINT_TO_POINTER (seat_slot),
+                       touch_state);
 
   return touch_state;
 }
 
 void
 meta_seat_native_release_touch_state (MetaSeatNative *seat,
-                                      MetaTouchState *touch_state)
+                                      int             seat_slot)
 {
-  g_clear_pointer (&seat->touch_states[touch_state->seat_slot],
-                   clutter_touch_state_free);
+  if (!seat->touch_states)
+    return;
+  g_hash_table_remove (seat->touch_states, GINT_TO_POINTER (seat_slot));
 }
 
 void
@@ -1963,7 +1940,7 @@ process_device_event (MetaSeatNative        *seat,
 
     case LIBINPUT_EVENT_TOUCH_DOWN:
       {
-        int device_slot;
+        int seat_slot;
         uint64_t time_us;
         double x, y;
         float stage_width, stage_height;
@@ -1984,14 +1961,14 @@ process_device_event (MetaSeatNative        *seat,
         stage_width = clutter_actor_get_width (CLUTTER_ACTOR (stage));
         stage_height = clutter_actor_get_height (CLUTTER_ACTOR (stage));
 
-        device_slot = libinput_event_touch_get_slot (touch_event);
+        seat_slot = libinput_event_touch_get_seat_slot (touch_event);
         time_us = libinput_event_touch_get_time_usec (touch_event);
         x = libinput_event_touch_get_x_transformed (touch_event,
                                                     stage_width);
         y = libinput_event_touch_get_y_transformed (touch_event,
                                                     stage_height);
 
-        touch_state = meta_seat_native_acquire_touch_state (seat, device_slot);
+        touch_state = meta_seat_native_acquire_touch_state (seat, seat_slot);
         touch_state->coords.x = x;
         touch_state->coords.y = y;
 
@@ -2006,7 +1983,7 @@ process_device_event (MetaSeatNative        *seat,
 
     case LIBINPUT_EVENT_TOUCH_UP:
       {
-        int device_slot;
+        int seat_slot;
         uint64_t time_us;
         MetaSeatNative *seat;
         MetaTouchState *touch_state;
@@ -2017,9 +1994,9 @@ process_device_event (MetaSeatNative        *seat,
         device_evdev = META_INPUT_DEVICE_NATIVE (device);
         seat = meta_input_device_native_get_seat (device_evdev);
 
-        device_slot = libinput_event_touch_get_slot (touch_event);
+        seat_slot = libinput_event_touch_get_seat_slot (touch_event);
         time_us = libinput_event_touch_get_time_usec (touch_event);
-        touch_state = meta_seat_native_lookup_touch_state (seat, device_slot);
+        touch_state = meta_seat_native_lookup_touch_state (seat, seat_slot);
         if (!touch_state)
           break;
 
@@ -2028,13 +2005,13 @@ process_device_event (MetaSeatNative        *seat,
                                              touch_state->seat_slot,
                                              touch_state->coords.x,
                                              touch_state->coords.y);
-        meta_seat_native_release_touch_state (seat, touch_state);
+        meta_seat_native_release_touch_state (seat, seat_slot);
         break;
       }
 
     case LIBINPUT_EVENT_TOUCH_MOTION:
       {
-        int device_slot;
+        int seat_slot;
         uint64_t time_us;
         double x, y;
         float stage_width, stage_height;
@@ -2055,14 +2032,14 @@ process_device_event (MetaSeatNative        *seat,
         stage_width = clutter_actor_get_width (CLUTTER_ACTOR (stage));
         stage_height = clutter_actor_get_height (CLUTTER_ACTOR (stage));
 
-        device_slot = libinput_event_touch_get_slot (touch_event);
+        seat_slot = libinput_event_touch_get_seat_slot (touch_event);
         time_us = libinput_event_touch_get_time_usec (touch_event);
         x = libinput_event_touch_get_x_transformed (touch_event,
                                                     stage_width);
         y = libinput_event_touch_get_y_transformed (touch_event,
                                                     stage_height);
 
-        touch_state = meta_seat_native_lookup_touch_state (seat, device_slot);
+        touch_state = meta_seat_native_lookup_touch_state (seat, seat_slot);
         if (!touch_state)
           break;
 
@@ -2079,7 +2056,7 @@ process_device_event (MetaSeatNative        *seat,
       }
     case LIBINPUT_EVENT_TOUCH_CANCEL:
       {
-        int device_slot;
+        int seat_slot;
         MetaTouchState *touch_state;
         uint64_t time_us;
         MetaSeatNative *seat;
@@ -2091,8 +2068,8 @@ process_device_event (MetaSeatNative        *seat,
         seat = meta_input_device_native_get_seat (device_evdev);
         time_us = libinput_event_touch_get_time_usec (touch_event);
 
-        device_slot = libinput_event_touch_get_slot (touch_event);
-        touch_state = meta_seat_native_lookup_touch_state (seat, device_slot);
+        seat_slot = libinput_event_touch_get_seat_slot (touch_event);
+        touch_state = meta_seat_native_lookup_touch_state (seat, seat_slot);
         if (!touch_state)
           break;
 
@@ -2104,7 +2081,7 @@ process_device_event (MetaSeatNative        *seat,
                                              touch_state->coords.x,
                                              touch_state->coords.y);
 
-        meta_seat_native_release_touch_state (touch_state->seat, touch_state);
+        meta_seat_native_release_touch_state (seat, seat_slot);
         break;
       }
     case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN:
@@ -2575,7 +2552,9 @@ meta_seat_native_finalize (GObject *object)
       g_object_unref (device);
     }
   g_slist_free (seat->devices);
-  g_free (seat->touch_states);
+
+  if (seat->touch_states)
+    g_hash_table_destroy (seat->touch_states);
 
   g_object_unref (seat->udev_client);
 
diff --git a/src/backends/native/meta-seat-native.h b/src/backends/native/meta-seat-native.h
index befc5a041f..1c4ee182c3 100644
--- a/src/backends/native/meta-seat-native.h
+++ b/src/backends/native/meta-seat-native.h
@@ -88,8 +88,7 @@ struct _MetaSeatNative
   ClutterInputDevice *core_pointer;
   ClutterInputDevice *core_keyboard;
 
-  MetaTouchState **touch_states;
-  int n_alloc_touch_states;
+  GHashTable *touch_states;
 
   struct xkb_state *xkb;
   xkb_led_index_t caps_lock_led;
@@ -202,12 +201,12 @@ void meta_seat_native_set_libinput_seat (MetaSeatNative       *seat,
 void meta_seat_native_sync_leds (MetaSeatNative *seat);
 
 MetaTouchState * meta_seat_native_acquire_touch_state (MetaSeatNative *seat,
-                                                       int             device_slot);
+                                                       int             seat_slot);
 MetaTouchState * meta_seat_native_lookup_touch_state  (MetaSeatNative *seat,
-                                                       int             device_slot);
+                                                       int             seat_slot);
 
 void meta_seat_native_release_touch_state (MetaSeatNative *seat,
-                                           MetaTouchState *touch_state);
+                                           int             seat_slot);
 
 void meta_seat_native_set_stage (MetaSeatNative *seat,
                                  ClutterStage   *stage);
diff --git a/src/backends/native/meta-virtual-input-device-native.c 
b/src/backends/native/meta-virtual-input-device-native.c
index be25038d36..390d83cc59 100644
--- a/src/backends/native/meta-virtual-input-device-native.c
+++ b/src/backends/native/meta-virtual-input-device-native.c
@@ -628,7 +628,8 @@ meta_virtual_input_device_native_notify_touch_up (ClutterVirtualInputDevice *vir
                                        touch_state->coords.x,
                                        touch_state->coords.y);
 
-  meta_seat_native_release_touch_state (virtual_evdev->seat, touch_state);
+  meta_seat_native_release_touch_state (virtual_evdev->seat,
+                                        touch_state->seat_slot);
 }
 
 static void


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