[mutter] context: Make the context owner of the backend



commit b1c643eeaa2f783b6f9b5b29f4fed312bf8f33fb
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Wed Mar 3 14:31:56 2021 +0100

    context: Make the context owner of the backend
    
    There is still the `_backend` singleton still, as there are still the
    `meta_get_backend()` that needs to work for now.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1861>

 src/backends/meta-backend-private.h |   6 +-
 src/backends/meta-backend.c         | 114 +++++++++++++++++++++++++++---------
 src/core/meta-context-main.c        |   4 ++
 src/core/meta-context.c             |  34 +++++++++--
 src/meta/meta-backend.h             |   3 +
 src/meta/meta-context.h             |   3 +
 src/tests/meta-context-test.c       |   2 +
 7 files changed, 126 insertions(+), 40 deletions(-)
---
diff --git a/src/backends/meta-backend-private.h b/src/backends/meta-backend-private.h
index 33a3752112..bce77c2ec4 100644
--- a/src/backends/meta-backend-private.h
+++ b/src/backends/meta-backend-private.h
@@ -107,11 +107,7 @@ struct _MetaBackendClass
                                    MetaPointerConstraint *constraint);
 };
 
-void meta_init_backend (GType         backend_gtype,
-                        unsigned int  n_properties,
-                        const char   *names[],
-                        const GValue *values);
-void meta_release_backend (void);
+void meta_backend_destroy (MetaBackend *backend);
 
 void meta_backend_prepare_shutdown (MetaBackend *backend);
 
diff --git a/src/backends/meta-backend.c b/src/backends/meta-backend.c
index 1dd90a3563..bf3c7d7a6d 100644
--- a/src/backends/meta-backend.c
+++ b/src/backends/meta-backend.c
@@ -68,6 +68,7 @@
 #include "clutter/clutter-seat-private.h"
 #include "meta/main.h"
 #include "meta/meta-backend.h"
+#include "meta/meta-context.h"
 #include "meta/util.h"
 
 #ifdef HAVE_PROFILER
@@ -89,6 +90,17 @@
 #include "wayland/meta-wayland.h"
 #endif
 
+enum
+{
+  PROP_0,
+
+  PROP_CONTEXT,
+
+  N_PROPS
+};
+
+static GParamSpec *obj_props[N_PROPS];
+
 enum
 {
   KEYMAP_CHANGED,
@@ -122,6 +134,8 @@ meta_get_backend (void)
 
 struct _MetaBackendPrivate
 {
+  MetaContext *context;
+
   MetaMonitorManager *monitor_manager;
   MetaOrientationManager *orientation_manager;
   MetaCursorTracker *cursor_tracker;
@@ -199,6 +213,8 @@ meta_backend_dispose (GObject *object)
   MetaBackend *backend = META_BACKEND (object);
   MetaBackendPrivate *priv = meta_backend_get_instance_private (backend);
 
+  _backend = NULL;
+
   g_clear_pointer (&priv->cursor_tracker, meta_cursor_tracker_destroy);
   g_clear_object (&priv->current_device);
   g_clear_object (&priv->monitor_manager);
@@ -249,7 +265,7 @@ meta_backend_dispose (GObject *object)
   G_OBJECT_CLASS (meta_backend_parent_class)->dispose (object);
 }
 
-static void
+void
 meta_backend_destroy (MetaBackend *backend)
 {
   g_object_run_dispose (G_OBJECT (backend));
@@ -737,6 +753,8 @@ meta_backend_constructed (GObject *object)
   MetaBackendClass *backend_class =
    META_BACKEND_GET_CLASS (backend);
 
+  g_assert (priv->context);
+
 #ifdef HAVE_LIBWACOM
   priv->wacom_db = libwacom_database_new ();
   if (!priv->wacom_db)
@@ -758,6 +776,46 @@ meta_backend_constructed (GObject *object)
                                             NULL);
 }
 
+static void
+meta_backend_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+  MetaBackend *backend = META_BACKEND (object);
+  MetaBackendPrivate *priv = meta_backend_get_instance_private (backend);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      priv->context = g_value_get_object (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+meta_backend_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  MetaBackend *backend = META_BACKEND (object);
+  MetaBackendPrivate *priv = meta_backend_get_instance_private (backend);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      g_value_set_object (value, priv->context);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
 static void
 meta_backend_class_init (MetaBackendClass *klass)
 {
@@ -766,6 +824,8 @@ meta_backend_class_init (MetaBackendClass *klass)
 
   object_class->dispose = meta_backend_dispose;
   object_class->constructed = meta_backend_constructed;
+  object_class->set_property = meta_backend_set_property;
+  object_class->get_property = meta_backend_get_property;
 
   klass->post_init = meta_backend_real_post_init;
   klass->grab_device = meta_backend_real_grab_device;
@@ -774,6 +834,16 @@ meta_backend_class_init (MetaBackendClass *klass)
   klass->is_lid_closed = meta_backend_real_is_lid_closed;
   klass->create_cursor_tracker = meta_backend_real_create_cursor_tracker;
 
+  obj_props[PROP_CONTEXT] =
+    g_param_spec_object ("context",
+                         "context",
+                         "MetaContext",
+                         META_TYPE_CONTEXT,
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT_ONLY |
+                         G_PARAM_STATIC_STRINGS);
+  g_object_class_install_properties (object_class, N_PROPS, obj_props);
+
   signals[KEYMAP_CHANGED] =
     g_signal_new ("keymap-changed",
                   G_TYPE_FROM_CLASS (object_class),
@@ -1279,6 +1349,20 @@ meta_backend_grab_device (MetaBackend *backend,
   return META_BACKEND_GET_CLASS (backend)->grab_device (backend, device_id, timestamp);
 }
 
+/**
+ * meta_backend_get_context:
+ * @backend: the #MetaBackend
+ *
+ * Returns: (transfer none): The #MetaContext
+ */
+MetaContext *
+meta_backend_get_context (MetaBackend *backend)
+{
+  MetaBackendPrivate *priv = meta_backend_get_instance_private (backend);
+
+  return priv->context;
+}
+
 /**
  * meta_backend_ungrab_device: (skip)
  */
@@ -1458,34 +1542,6 @@ meta_backend_get_clutter_backend (MetaBackend *backend)
   return priv->clutter_backend;
 }
 
-void
-meta_init_backend (GType         backend_gtype,
-                   unsigned int  n_properties,
-                   const char   *names[],
-                   const GValue *values)
-{
-  MetaBackend *backend;
-  GError *error = NULL;
-
-  /* meta_backend_init() above install the backend globally so
-   * so meta_get_backend() works even during initialization. */
-  backend = META_BACKEND (g_object_new_with_properties (backend_gtype,
-                                                        n_properties,
-                                                        names,
-                                                        values));
-  if (!g_initable_init (G_INITABLE (backend), NULL, &error))
-    {
-      g_warning ("Failed to create backend: %s", error->message);
-      meta_exit (META_EXIT_ERROR);
-    }
-}
-
-void
-meta_release_backend (void)
-{
-  g_clear_pointer (&_backend, meta_backend_destroy);
-}
-
 void
 meta_backend_prepare_shutdown (MetaBackend *backend)
 {
diff --git a/src/core/meta-context-main.c b/src/core/meta-context-main.c
index 60e507c247..b8263002d5 100644
--- a/src/core/meta-context-main.c
+++ b/src/core/meta-context-main.c
@@ -381,6 +381,7 @@ create_x11_cm_backend (MetaContext  *context,
 
   return g_initable_new (META_TYPE_BACKEND_X11_CM,
                          NULL, error,
+                         "context", context,
                          "display-name", context_main->options.x11.display_name,
                          NULL);
 }
@@ -392,6 +393,7 @@ create_nested_backend (MetaContext  *context,
 {
   return g_initable_new (META_TYPE_BACKEND_X11_NESTED,
                          NULL, error,
+                         "context", context,
                          NULL);
 }
 
@@ -402,6 +404,7 @@ create_headless_backend (MetaContext  *context,
 {
   return g_initable_new (META_TYPE_BACKEND_NATIVE,
                          NULL, error,
+                         "context", context,
                          "headless", TRUE,
                          NULL);
 }
@@ -412,6 +415,7 @@ create_native_backend (MetaContext  *context,
 {
   return g_initable_new (META_TYPE_BACKEND_NATIVE,
                          NULL, error,
+                         "context", context,
                          NULL);
 }
 #endif /* HAVE_NATIVE_BACKEND */
diff --git a/src/core/meta-context.c b/src/core/meta-context.c
index 60e3543a5f..787d25d8ec 100644
--- a/src/core/meta-context.c
+++ b/src/core/meta-context.c
@@ -54,6 +54,8 @@ typedef struct _MetaContextPrivate
 
   GOptionContext *option_context;
 
+  MetaBackend *backend;
+
   GMainLoop *main_loop;
   GError *termination_error;
 } MetaContextPrivate;
@@ -111,6 +113,20 @@ meta_context_notify_ready (MetaContext *context)
   META_CONTEXT_GET_CLASS (context)->notify_ready (context);
 }
 
+/**
+ * meta_context_get_backend:
+ * @context: The #MetaContext
+ *
+ * Returns: (transfer none): the #MetaBackend
+ */
+MetaBackend *
+meta_context_get_backend (MetaContext *context)
+{
+  MetaContextPrivate *priv = meta_context_get_instance_private (context);
+
+  return priv->backend;
+}
+
 MetaCompositorType
 meta_context_get_compositor_type (MetaContext *context)
 {
@@ -231,7 +247,15 @@ static gboolean
 meta_context_real_setup (MetaContext  *context,
                          GError      **error)
 {
-  return !!META_CONTEXT_GET_CLASS (context)->create_backend (context, error);
+  MetaContextPrivate *priv = meta_context_get_instance_private (context);
+  MetaBackend *backend;
+
+  backend = META_CONTEXT_GET_CLASS (context)->create_backend (context, error);
+  if (!backend)
+    return FALSE;
+
+  priv->backend = backend;
+  return TRUE;
 }
 
 gboolean
@@ -381,15 +405,13 @@ meta_context_finalize (GObject *object)
   MetaContext *context = META_CONTEXT (object);
   MetaContextPrivate *priv = meta_context_get_instance_private (context);
   MetaDisplay *display;
-  MetaBackend *backend;
 #ifdef HAVE_WAYLAND
   MetaWaylandCompositor *compositor;
   MetaCompositorType compositor_type;
 #endif
 
-  backend = meta_get_backend ();
-  if (backend)
-    meta_backend_prepare_shutdown (backend);
+  if (priv->backend)
+    meta_backend_prepare_shutdown (priv->backend);
 
 #ifdef HAVE_WAYLAND
   compositor = meta_wayland_compositor_get_default ();
@@ -407,7 +429,7 @@ meta_context_finalize (GObject *object)
     meta_wayland_finalize ();
 #endif
 
-  meta_release_backend ();
+  g_clear_pointer (&priv->backend, meta_backend_destroy);
 
   g_clear_pointer (&priv->option_context, g_option_context_free);
   g_clear_pointer (&priv->main_loop, g_main_loop_unref);
diff --git a/src/meta/meta-backend.h b/src/meta/meta-backend.h
index 78ad08b349..63a679a1ab 100644
--- a/src/meta/meta-backend.h
+++ b/src/meta/meta-backend.h
@@ -49,6 +49,9 @@ META_EXPORT
 void meta_backend_lock_layout_group (MetaBackend *backend,
                                      guint        idx);
 
+META_EXPORT
+MetaContext * meta_backend_get_context (MetaBackend *backend);
+
 META_EXPORT
 ClutterActor *meta_backend_get_stage (MetaBackend *backend);
 
diff --git a/src/meta/meta-context.h b/src/meta/meta-context.h
index 6c95727572..aa1437efe3 100644
--- a/src/meta/meta-context.h
+++ b/src/meta/meta-context.h
@@ -81,4 +81,7 @@ void meta_context_terminate_with_error (MetaContext *context,
 META_EXPORT
 MetaCompositorType meta_context_get_compositor_type (MetaContext *context);
 
+META_EXPORT
+MetaBackend * meta_context_get_backend (MetaContext *context);
+
 #endif /* META_CONTEXT_H */
diff --git a/src/tests/meta-context-test.c b/src/tests/meta-context-test.c
index e15ac3257c..2f88d8c729 100644
--- a/src/tests/meta-context-test.c
+++ b/src/tests/meta-context-test.c
@@ -121,6 +121,7 @@ create_nested_backend (MetaContext  *context,
 {
   return g_initable_new (META_TYPE_BACKEND_TEST,
                          NULL, error,
+                         "context", context,
                          NULL);
 }
 
@@ -131,6 +132,7 @@ create_headless_backend (MetaContext  *context,
 {
   return g_initable_new (META_TYPE_BACKEND_NATIVE,
                          NULL, error,
+                         "context", context,
                          "headless", TRUE,
                          NULL);
 }


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