[mutter] seat-impl: Open/close files via device pool



commit 7ce266628e13b5c30cac05b4d75af927e86b0bdc
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Tue Apr 6 17:53:27 2021 +0200

    seat-impl: Open/close files via device pool
    
    This replaces going through MetaLauncher to open/close restricted files.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1828>

 src/backends/native/meta-backend-native.c |  1 +
 src/backends/native/meta-seat-impl.c      | 65 ++++++++++++++++++++-----------
 src/backends/native/meta-seat-native.c    | 21 ++++++++++
 src/backends/native/meta-seat-native.h    |  4 ++
 4 files changed, 69 insertions(+), 22 deletions(-)
---
diff --git a/src/backends/native/meta-backend-native.c b/src/backends/native/meta-backend-native.c
index 0c1eebcc9b..d5dd148d38 100644
--- a/src/backends/native/meta-backend-native.c
+++ b/src/backends/native/meta-backend-native.c
@@ -147,6 +147,7 @@ meta_backend_native_create_default_seat (MetaBackend  *backend,
     flags = META_SEAT_NATIVE_FLAG_NONE;
 
   return CLUTTER_SEAT (g_object_new (META_TYPE_SEAT_NATIVE,
+                                     "backend", backend,
                                      "seat-id", seat_id,
                                      "flags", flags,
                                      NULL));
diff --git a/src/backends/native/meta-seat-impl.c b/src/backends/native/meta-seat-impl.c
index a7613627f3..4d1e7c2ebb 100644
--- a/src/backends/native/meta-seat-impl.c
+++ b/src/backends/native/meta-seat-impl.c
@@ -34,7 +34,9 @@
 #include <math.h>
 
 #include "backends/meta-cursor-tracker-private.h"
+#include "backends/native/meta-backend-native-private.h"
 #include "backends/native/meta-barrier-native.h"
+#include "backends/native/meta-device-pool.h"
 #include "backends/native/meta-input-thread.h"
 #include "backends/native/meta-virtual-input-device-native.h"
 #include "clutter/clutter-mutter.h"
@@ -116,11 +118,17 @@ enum
 
 static guint signals[N_SIGNALS] = { 0 };
 
+typedef struct _MetaSeatImplPrivate
+{
+  GHashTable *device_files;
+} MetaSeatImplPrivate;
+
 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))
+                                                meta_seat_impl_initable_iface_init)
+                         G_ADD_PRIVATE (MetaSeatImpl))
 
 static void process_events (MetaSeatImpl *seat_impl);
 void meta_seat_impl_constrain_pointer (MetaSeatImpl       *seat_impl,
@@ -2565,32 +2573,36 @@ process_events (MetaSeatImpl *seat_impl)
 
 static int
 open_restricted (const char *path,
-                 int         flags,
+                 int         open_flags,
                  void       *user_data)
 {
+  MetaSeatImpl *seat_impl = user_data;
+  MetaSeatImplPrivate *priv = meta_seat_impl_get_instance_private (seat_impl);
+  MetaBackend *backend = meta_seat_native_get_backend (seat_impl->seat_native);
+  MetaDevicePool *device_pool =
+    meta_backend_native_get_device_pool (META_BACKEND_NATIVE (backend));
+  MetaDeviceFileFlags flags;
+  g_autoptr (GError) error = NULL;
+  MetaDeviceFile *device_file;
   int fd;
 
-  if (device_open_callback)
-    {
-      GError *error = NULL;
+  flags = META_DEVICE_FILE_FLAG_NONE;
+  if (!(open_flags & (O_RDWR | O_WRONLY)))
+    flags |= META_DEVICE_FILE_FLAG_READ_ONLY;
 
-      fd = device_open_callback (path, flags, device_callback_data, &error);
+  if (!g_str_has_prefix (path, "/sys/"))
+    flags |= META_DEVICE_FILE_FLAG_TAKE_CONTROL;
 
-      if (fd < 0)
-        {
-          g_warning ("Could not open device %s: %s", path, error->message);
-          g_error_free (error);
-        }
-    }
-  else
+  device_file = meta_device_pool_open (device_pool, path, flags, &error);
+  if (!device_file)
     {
-      fd = open (path, O_RDWR | O_NONBLOCK);
-      if (fd < 0)
-        {
-          g_warning ("Could not open device %s: %s", path, strerror (errno));
-        }
+      g_warning ("Could not open device %s: %s", path, error->message);
+      return -1;
     }
 
+  fd = meta_device_file_get_fd (device_file);
+  g_hash_table_insert (priv->device_files, GINT_TO_POINTER (fd), device_file);
+
   return fd;
 }
 
@@ -2598,10 +2610,10 @@ static void
 close_restricted (int   fd,
                   void *user_data)
 {
-  if (device_close_callback)
-    device_close_callback (fd, device_callback_data);
-  else
-    close (fd);
+  MetaSeatImpl *seat_impl = user_data;
+  MetaSeatImplPrivate *priv = meta_seat_impl_get_instance_private (seat_impl);
+
+  g_hash_table_remove (priv->device_files, GINT_TO_POINTER (fd));
 }
 
 static const struct libinput_interface libinput_interface = {
@@ -2709,10 +2721,16 @@ init_libinput (MetaSeatImpl  *seat_impl,
 static gpointer
 input_thread (MetaSeatImpl *seat_impl)
 {
+  MetaSeatImplPrivate *priv = meta_seat_impl_get_instance_private (seat_impl);
   struct xkb_keymap *xkb_keymap;
 
   g_main_context_push_thread_default (seat_impl->input_context);
 
+  priv->device_files =
+    g_hash_table_new_full (NULL, NULL,
+                           NULL,
+                           (GDestroyNotify) meta_device_file_release);
+
   if (!(seat_impl->flags & META_SEAT_NATIVE_FLAG_NO_LIBINPUT))
     {
       g_autoptr (GError) error = NULL;
@@ -2868,6 +2886,7 @@ static gboolean
 destroy_in_impl (GTask *task)
 {
   MetaSeatImpl *seat_impl = g_task_get_source_object (task);
+  MetaSeatImplPrivate *priv = meta_seat_impl_get_instance_private (seat_impl);
   gboolean numlock_active;
 
   g_slist_foreach (seat_impl->devices,
@@ -2892,6 +2911,8 @@ destroy_in_impl (GTask *task)
 
   meta_seat_impl_clear_repeat_source (seat_impl);
 
+  g_clear_pointer (&priv->device_files, g_hash_table_destroy);
+
   g_main_loop_quit (seat_impl->input_loop);
   g_task_return_boolean (task, TRUE);
 
diff --git a/src/backends/native/meta-seat-native.c b/src/backends/native/meta-seat-native.c
index 6dcc284ef2..12fead1fb2 100644
--- a/src/backends/native/meta-seat-native.c
+++ b/src/backends/native/meta-seat-native.c
@@ -44,6 +44,7 @@ enum
   PROP_0,
   PROP_SEAT_ID,
   PROP_FLAGS,
+  PROP_BACKEND,
   N_PROPS,
 
   /* This property is overridden */
@@ -193,6 +194,9 @@ meta_seat_native_set_property (GObject      *object,
     case PROP_FLAGS:
       seat_native->flags = g_value_get_flags (value);
       break;
+    case PROP_BACKEND:
+      seat_native->backend = g_value_get_object (value);
+      break;
     case PROP_TOUCH_MODE:
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -218,6 +222,9 @@ meta_seat_native_get_property (GObject    *object,
     case PROP_FLAGS:
       g_value_set_flags (value, seat_native->flags);
       break;
+    case PROP_BACKEND:
+      g_value_set_object (value, seat_native->backend);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -400,6 +407,14 @@ meta_seat_native_class_init (MetaSeatNativeClass *klass)
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY);
 
+  props[PROP_BACKEND] =
+    g_param_spec_object ("backend",
+                         "Backend",
+                         "Backend",
+                         META_TYPE_BACKEND,
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT_ONLY);
+
   g_object_class_install_properties (object_class, N_PROPS, props);
 
   g_object_class_override_property (object_class, PROP_TOUCH_MODE,
@@ -571,6 +586,12 @@ meta_seat_native_get_barrier_manager (MetaSeatNative *seat)
   return meta_seat_impl_get_barrier_manager (seat->impl);
 }
 
+MetaBackend *
+meta_seat_native_get_backend (MetaSeatNative *seat_native)
+{
+  return seat_native->backend;
+}
+
 void
 meta_seat_native_set_pointer_constraint (MetaSeatNative            *seat,
                                          MetaPointerConstraintImpl *constraint_impl)
diff --git a/src/backends/native/meta-seat-native.h b/src/backends/native/meta-seat-native.h
index 661d2df3b4..53ba00f514 100644
--- a/src/backends/native/meta-seat-native.h
+++ b/src/backends/native/meta-seat-native.h
@@ -42,6 +42,8 @@ struct _MetaSeatNative
 {
   ClutterSeat parent_instance;
 
+  MetaBackend *backend;
+
   MetaSeatImpl *impl;
   char *seat_id;
   MetaSeatNativeFlag flags;
@@ -118,6 +120,8 @@ void meta_seat_native_release_touch_slots (MetaSeatNative *seat,
 
 MetaBarrierManagerNative * meta_seat_native_get_barrier_manager (MetaSeatNative *seat);
 
+MetaBackend * meta_seat_native_get_backend (MetaSeatNative *seat);
+
 void meta_seat_native_set_pointer_constraint (MetaSeatNative            *seat,
                                               MetaPointerConstraintImpl *constraint_impl);
 MetaCursorRenderer * meta_seat_native_maybe_ensure_cursor_renderer (MetaSeatNative     *seat,


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