[mutter/wip/carlosg/input-thread: 97/97] backends/native: Add input thread inside MetaSeatImpl




commit 1c8aefbc71bc816ee39b12412dcb6d5eefcbb5c0
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Aug 12 18:04:34 2020 +0200

    backends/native: Add input thread inside MetaSeatImpl
    
    This (now) doesn't change anything in regards to the API that the UI
    thread should access from the MetaSeatImpl. The MetaInputDeviceNative,
    MetaInputSettings and MetaKeymap objects are now considered owned by
    the input thread, as well as all of libinput objects.
    
    The MetaEventSource now dispatches events in a GMainContext that is
    the thread default to this thread, and all UI-thread-accessible API
    (seat and virtual input device API) will be handled in a serialized
    manner by that same input thread.
    
    The MetaSeatImpl itself is still considered to be owned by the caller
    thread, and all the signals that this object emits will be emitted in
    the GMainContext that is default at the time of calling
    meta_seat_impl_new().
    
    The MetaInputSettings configuration changes will likewise be handled
    in the input thread, close to libinput devices.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1403

 src/backends/native/meta-seat-impl.c | 245 ++++++++++++++++++++++++-----------
 src/backends/native/meta-seat-impl.h |   9 +-
 2 files changed, 175 insertions(+), 79 deletions(-)
---
diff --git a/src/backends/native/meta-seat-impl.c b/src/backends/native/meta-seat-impl.c
index 99655dd867..567345a55c 100644
--- a/src/backends/native/meta-seat-impl.c
+++ b/src/backends/native/meta-seat-impl.c
@@ -119,7 +119,11 @@ enum
 
 static guint signals[N_SIGNALS] = { 0 };
 
-G_DEFINE_TYPE (MetaSeatImpl, meta_seat_impl, G_TYPE_OBJECT)
+static void meta_seat_impl_initable_iface_init (GInitableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (MetaSeatImpl, meta_seat_impl, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                                                meta_seat_impl_initable_iface_init))
 
 static void process_events (MetaSeatImpl *seat);
 void meta_seat_impl_constrain_pointer (MetaSeatImpl       *seat,
@@ -135,7 +139,7 @@ void meta_seat_impl_filter_relative_motion (MetaSeatImpl       *seat,
                                             float               y,
                                             float              *dx,
                                             float              *dy);
-void meta_seat_impl_clear_repeat_timer (MetaSeatImpl *seat);
+void meta_seat_impl_clear_repeat_source (MetaSeatImpl *seat);
 
 void
 meta_seat_impl_run_input_task (MetaSeatImpl *impl,
@@ -243,11 +247,11 @@ meta_seat_impl_release_touch_state (MetaSeatImpl *seat,
 }
 
 void
-meta_seat_impl_clear_repeat_timer (MetaSeatImpl *seat)
+meta_seat_impl_clear_repeat_source (MetaSeatImpl *seat)
 {
-  if (seat->repeat_timer)
+  if (seat->repeat_source)
     {
-      g_clear_handle_id (&seat->repeat_timer, g_source_remove);
+      g_clear_pointer (&seat->repeat_source, g_source_destroy);
       g_clear_object (&seat->repeat_device);
     }
 }
@@ -263,20 +267,18 @@ static gboolean
 keyboard_repeat (gpointer data)
 {
   MetaSeatImpl *seat = data;
-  GSource *source;
 
   /* There might be events queued in libinput that could cancel the
      repeat timer. */
   dispatch_libinput (seat);
-  if (!seat->repeat_timer)
+  if (!seat->repeat_source)
     return G_SOURCE_REMOVE;
 
   g_return_val_if_fail (seat->repeat_device != NULL, G_SOURCE_REMOVE);
-  source = g_main_context_find_source_by_id (NULL, seat->repeat_timer);
 
   meta_seat_impl_notify_key (seat,
                              seat->repeat_device,
-                             g_source_get_time (source),
+                             g_source_get_time (seat->repeat_source),
                              seat->repeat_key,
                              AUTOREPEAT_VALUE,
                              FALSE);
@@ -315,6 +317,50 @@ update_button_count (MetaSeatImpl *seat,
     }
 }
 
+typedef struct
+{
+  MetaSeatImpl *seat;
+  guint signal_id;
+  guint arg1;
+  guint arg2;
+} MetaSeatSignalData;
+
+static gboolean
+emit_signal_main (MetaSeatSignalData *data)
+{
+  g_signal_emit (data->seat,
+                 data->signal_id,
+                 0,
+                 data->arg1,
+                 data->arg2);
+  return G_SOURCE_REMOVE;
+}
+
+static void
+emit_signal (MetaSeatImpl *seat,
+             guint         signal_id,
+             guint         arg1,
+             guint         arg2)
+{
+  MetaSeatSignalData *emit_signal_data;
+  GSource *source;
+
+  emit_signal_data = g_new0 (MetaSeatSignalData, 1);
+  emit_signal_data->seat = seat;
+  emit_signal_data->signal_id = signal_id;
+  emit_signal_data->arg1 = arg1;
+  emit_signal_data->arg2 = arg2;
+
+  source = g_idle_source_new ();
+  g_source_set_priority (source, G_PRIORITY_HIGH);
+  g_source_set_callback (source,
+                         (GSourceFunc) emit_signal_main,
+                         emit_signal_data,
+                         g_free);
+
+  g_source_attach (source, seat->caller_context);
+}
+
 void
 meta_seat_impl_notify_key (MetaSeatImpl       *seat,
                            ClutterInputDevice *device,
@@ -365,7 +411,7 @@ meta_seat_impl_notify_key (MetaSeatImpl       *seat,
 
   if (update_keys && (changed_state & XKB_STATE_LEDS))
     {
-      g_signal_emit (seat, signals[MODS_STATE_CHANGED], 0);
+      emit_signal (seat, signals[MODS_STATE_CHANGED], 0, 0);
       meta_seat_impl_sync_leds (seat);
       meta_input_device_native_a11y_maybe_notify_toggle_keys (META_INPUT_DEVICE_NATIVE 
(seat->core_keyboard));
     }
@@ -375,7 +421,8 @@ meta_seat_impl_notify_key (MetaSeatImpl       *seat,
       !xkb_keymap_key_repeats (xkb_state_get_keymap (seat->xkb),
                                event->key.hardware_keycode))
     {
-      meta_seat_impl_clear_repeat_timer (seat);
+      seat->repeat_count = 0;
+      meta_seat_impl_clear_repeat_source (seat);
       return;
     }
 
@@ -392,7 +439,7 @@ meta_seat_impl_notify_key (MetaSeatImpl       *seat,
       {
         uint32_t interval;
 
-        meta_seat_impl_clear_repeat_timer (seat);
+        meta_seat_impl_clear_repeat_source (seat);
         seat->repeat_device = g_object_ref (device);
 
         if (seat->repeat_count == 1)
@@ -400,12 +447,11 @@ meta_seat_impl_notify_key (MetaSeatImpl       *seat,
         else
           interval = seat->repeat_interval;
 
-        seat->repeat_timer =
-          clutter_threads_add_timeout_full (CLUTTER_PRIORITY_EVENTS,
-                                            interval,
-                                            keyboard_repeat,
-                                            seat,
-                                            NULL);
+        seat->repeat_source = g_timeout_source_new (interval);
+        g_source_set_priority (seat->repeat_source, CLUTTER_PRIORITY_EVENTS);
+        g_source_set_callback (seat->repeat_source,
+                               keyboard_repeat, seat, NULL);
+        g_source_attach (seat->repeat_source, seat->input_context);
         return;
       }
     default:
@@ -874,26 +920,13 @@ meta_seat_impl_notify_touch_event (MetaSeatImpl       *seat,
 /*
  * MetaEventSource for reading input devices
  */
-static gboolean
-meta_event_prepare (GSource *source,
-                    int     *timeout)
-{
-  gboolean retval;
-
-  *timeout = -1;
-  retval = clutter_events_pending ();
-
-  return retval;
-}
-
 static gboolean
 meta_event_check (GSource *source)
 {
   MetaEventSource *event_source = (MetaEventSource *) source;
   gboolean retval;
 
-  retval = ((event_source->event_poll_fd.revents & G_IO_IN) ||
-            clutter_events_pending ());
+  retval = !!(event_source->event_poll_fd.revents & G_IO_IN);
 
   return retval;
 }
@@ -1373,19 +1406,13 @@ meta_event_dispatch (GSource     *g_source,
 
   seat = source->seat_impl;
 
-  /* Don't queue more events if we haven't finished handling the previous batch
-   */
-  if (clutter_events_pending ())
-    goto queue_event;
-
   dispatch_libinput (seat);
 
- queue_event:
-
   return TRUE;
 }
+
 static GSourceFuncs event_funcs = {
-  meta_event_prepare,
+  NULL,
   meta_event_check,
   meta_event_dispatch,
   NULL
@@ -1412,7 +1439,7 @@ meta_event_source_new (MetaSeatImpl *seat)
   g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS);
   g_source_add_poll (source, &event_source->event_poll_fd);
   g_source_set_can_recurse (source, TRUE);
-  g_source_attach (source, NULL);
+  g_source_attach (source, seat->input_context);
 
   return event_source;
 }
@@ -1496,7 +1523,7 @@ update_touch_mode (MetaSeatImpl *seat)
   if (seat->touch_mode != touch_mode)
     {
       seat->touch_mode = touch_mode;
-      g_signal_emit (seat, signals[TOUCH_MODE], 0, touch_mode);
+      emit_signal (seat, signals[TOUCH_MODE], touch_mode, 0);
     }
 }
 
@@ -1564,8 +1591,8 @@ evdev_remove_device (MetaSeatImpl          *seat,
   if (is_touchscreen || is_tablet_switch)
     update_touch_mode (seat);
 
-  if (seat->repeat_timer && seat->repeat_device == device)
-    meta_seat_impl_clear_repeat_timer (seat);
+  if (seat->repeat_source && seat->repeat_device == device)
+    meta_seat_impl_clear_repeat_source (seat);
 
   g_object_run_dispose (G_OBJECT (device));
   g_object_unref (device);
@@ -2496,36 +2523,21 @@ static const struct libinput_interface libinput_interface = {
   close_restricted
 };
 
-static void
-meta_seat_impl_constructed (GObject *object)
+static gpointer
+input_thread (MetaSeatImpl *seat)
 {
-  MetaSeatImpl *seat = META_SEAT_IMPL (object);
-  ClutterInputDevice *device;
   MetaEventSource *source;
   struct udev *udev;
   struct xkb_keymap *xkb_keymap;
 
-  g_rw_lock_writer_lock (&seat->state_lock);
-
-  device = meta_input_device_native_new_virtual (
-      seat, CLUTTER_POINTER_DEVICE,
-      CLUTTER_INPUT_MODE_LOGICAL);
-  seat->pointer_x = INITIAL_POINTER_X;
-  seat->pointer_y = INITIAL_POINTER_Y;
-  meta_input_device_native_update_coords (META_INPUT_DEVICE_NATIVE (device),
-                                          seat->pointer_x, seat->pointer_y);
-  seat->core_pointer = device;
-
-  device = meta_input_device_native_new_virtual (
-      seat, CLUTTER_KEYBOARD_DEVICE,
-      CLUTTER_INPUT_MODE_LOGICAL);
-  seat->core_keyboard = device;
+  g_main_context_push_thread_default (seat->input_context);
 
   udev = udev_new ();
   if (G_UNLIKELY (udev == NULL))
     {
       g_warning ("Failed to create udev object");
-      return;
+      seat->input_thread_initialized = TRUE;
+      return NULL;
     }
 
   seat->libinput = libinput_udev_create_context (&libinput_interface,
@@ -2533,7 +2545,8 @@ meta_seat_impl_constructed (GObject *object)
   if (seat->libinput == NULL)
     {
       g_critical ("Failed to create the libinput object.");
-      return;
+      seat->input_thread_initialized = TRUE;
+      return NULL;
     }
 
   if (libinput_udev_assign_seat (seat->libinput, seat->seat_id) == -1)
@@ -2541,19 +2554,20 @@ meta_seat_impl_constructed (GObject *object)
       g_critical ("Failed to assign a seat to the libinput object.");
       libinput_unref (seat->libinput);
       seat->libinput = NULL;
-      return;
+      seat->input_thread_initialized = TRUE;
+      return NULL;
     }
 
   udev_unref (udev);
 
   seat->input_settings = meta_input_settings_native_new (seat);
 
-  seat->udev_client = g_udev_client_new ((const char *[]) { "input", NULL });
-
   source = meta_event_source_new (seat);
   seat->event_source = source;
 
   seat->keymap = g_object_new (META_TYPE_KEYMAP_NATIVE, NULL);
+
+  g_rw_lock_writer_lock (&seat->state_lock);
   xkb_keymap = meta_keymap_native_get_keyboard_map (seat->keymap);
 
   if (xkb_keymap)
@@ -2574,6 +2588,69 @@ meta_seat_impl_constructed (GObject *object)
   seat->has_tablet_switch = has_tablet_switch (seat);
   update_touch_mode (seat);
 
+  g_mutex_lock (&seat->init_mutex);
+  seat->input_thread_initialized = TRUE;
+  g_cond_signal (&seat->init_cond);
+  g_mutex_unlock (&seat->init_mutex);
+
+  seat->input_loop = g_main_loop_new (seat->input_context, FALSE);
+  g_main_loop_run (seat->input_loop);
+  g_main_loop_unref (seat->input_loop);
+
+  g_main_context_pop_thread_default (seat->input_context);
+
+  return NULL;
+}
+
+static gboolean
+meta_seat_impl_initable_init (GInitable     *initable,
+                              GCancellable  *cancellable,
+                              GError       **error)
+{
+  MetaSeatImpl *seat = META_SEAT_IMPL (initable);
+
+  seat->input_context = g_main_context_new ();
+  seat->caller_context = g_main_context_ref_thread_default ();
+
+  seat->input_thread =
+    g_thread_try_new ("Mutter Input Thread",
+                      (GThreadFunc) input_thread,
+                      initable,
+                      error);
+  if (!seat->input_thread)
+    return FALSE;
+
+  /* Initialize thread synchronously */
+  g_mutex_lock (&seat->init_mutex);
+  while (!seat->input_thread_initialized)
+    g_cond_wait (&seat->init_cond, &seat->init_mutex);
+  g_mutex_unlock (&seat->init_mutex);
+
+  return TRUE;
+}
+
+static void
+meta_seat_impl_constructed (GObject *object)
+{
+  MetaSeatImpl *seat = META_SEAT_IMPL (object);
+  ClutterInputDevice *device;
+
+  device = meta_input_device_native_new_virtual (
+      seat, CLUTTER_POINTER_DEVICE,
+      CLUTTER_INPUT_MODE_LOGICAL);
+  seat->pointer_x = INITIAL_POINTER_X;
+  seat->pointer_y = INITIAL_POINTER_Y;
+  meta_input_device_native_update_coords (META_INPUT_DEVICE_NATIVE (device),
+                                          seat->pointer_x, seat->pointer_y);
+  seat->core_pointer = device;
+
+  device = meta_input_device_native_new_virtual (
+      seat, CLUTTER_KEYBOARD_DEVICE,
+      CLUTTER_INPUT_MODE_LOGICAL);
+  seat->core_keyboard = device;
+
+  seat->udev_client = g_udev_client_new ((const char *[]) { "input", NULL });
+
   if (G_OBJECT_CLASS (meta_seat_impl_parent_class)->constructed)
     G_OBJECT_CLASS (meta_seat_impl_parent_class)->constructed (object);
 }
@@ -2640,6 +2717,9 @@ meta_seat_impl_finalize (GObject *object)
   MetaSeatImpl *seat = META_SEAT_IMPL (object);
   GSList *iter;
 
+  g_main_loop_quit (seat->input_loop);
+  g_thread_join (seat->input_thread);
+
   for (iter = seat->devices; iter; iter = g_slist_next (iter))
     {
       ClutterInputDevice *device = iter->data;
@@ -2657,7 +2737,7 @@ meta_seat_impl_finalize (GObject *object)
 
   xkb_state_unref (seat->xkb);
 
-  meta_seat_impl_clear_repeat_timer (seat);
+  meta_seat_impl_clear_repeat_source (seat);
 
   if (seat->libinput_seat)
     libinput_seat_unref (seat->libinput_seat);
@@ -2802,6 +2882,12 @@ meta_seat_impl_query_state (MetaSeatImpl         *seat,
   return retval;
 }
 
+static void
+meta_seat_impl_initable_iface_init (GInitableIface *iface)
+{
+  iface->init = meta_seat_impl_initable_init;
+}
+
 static void
 meta_seat_impl_class_init (MetaSeatImplClass *klass)
 {
@@ -2878,6 +2964,9 @@ meta_seat_impl_init (MetaSeatImpl *seat)
   seat->repeat_delay = 250;     /* ms */
   seat->repeat_interval = 33;   /* ms */
 
+  g_mutex_init (&seat->init_mutex);
+  g_cond_init (&seat->init_cond);
+
   seat->barrier_manager = meta_barrier_manager_native_new ();
 }
 
@@ -3337,10 +3426,11 @@ MetaSeatImpl *
 meta_seat_impl_new (MetaSeatNative *seat,
                     const char     *seat_id)
 {
-  return g_object_new (META_TYPE_SEAT_IMPL,
-                       "seat", seat,
-                       "seat-id", seat_id,
-                       NULL);
+  return g_initable_new (META_TYPE_SEAT_IMPL,
+                         NULL, NULL,
+                         "seat", seat,
+                         "seat-id", seat_id,
+                         NULL);
 }
 
 void
@@ -3353,8 +3443,7 @@ meta_seat_impl_notify_kbd_a11y_flags_changed (MetaSeatImpl          *impl,
   input_settings = impl->input_settings;
   meta_input_settings_notify_kbd_a11y_change (input_settings,
                                               new_flags, what_changed);
-  g_signal_emit (impl, signals[KBD_A11Y_FLAGS_CHANGED], 0,
-                 new_flags, what_changed);
+  emit_signal (impl, signals[KBD_A11Y_FLAGS_CHANGED], new_flags, what_changed);
 }
 
 void
@@ -3362,14 +3451,14 @@ meta_seat_impl_notify_kbd_a11y_mods_state_changed (MetaSeatImpl   *impl,
                                                    xkb_mod_mask_t  new_latched_mods,
                                                    xkb_mod_mask_t  new_locked_mods)
 {
-  g_signal_emit (impl, signals[KBD_A11Y_MODS_STATE_CHANGED], 0,
-                 new_latched_mods, new_locked_mods);
+  emit_signal (impl, signals[KBD_A11Y_MODS_STATE_CHANGED],
+               new_latched_mods, new_locked_mods);
 }
 
 void
 meta_seat_impl_notify_bell (MetaSeatImpl *impl)
 {
-  g_signal_emit (impl, signals[BELL], 0);
+  emit_signal (impl, signals[BELL], 0, 0);
 }
 
 MetaInputSettings *
diff --git a/src/backends/native/meta-seat-impl.h b/src/backends/native/meta-seat-impl.h
index 2576c08dfa..3c6fe094c2 100644
--- a/src/backends/native/meta-seat-impl.h
+++ b/src/backends/native/meta-seat-impl.h
@@ -54,7 +54,13 @@ struct _MetaSeatImpl
 {
   GObject parent_instance;
 
+  GMainContext *caller_context;
   GMainContext *input_context;
+  GMainLoop *input_loop;
+  GThread *input_thread;
+  GMutex init_mutex;
+  GCond init_cond;
+
   MetaSeatNative *seat;
   char *seat_id;
   MetaEventSource *event_source;
@@ -94,6 +100,7 @@ struct _MetaSeatImpl
   guint has_touchscreen          : 1;
   guint has_tablet_switch        : 1;
   guint touch_mode               : 1;
+  guint input_thread_initialized : 1;
 
   /* keyboard repeat */
   gboolean repeat;
@@ -101,8 +108,8 @@ struct _MetaSeatImpl
   uint32_t repeat_interval;
   uint32_t repeat_key;
   uint32_t repeat_count;
-  uint32_t repeat_timer;
   ClutterInputDevice *repeat_device;
+  GSource *repeat_source;
 
   float pointer_x;
   float pointer_y;


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