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>
This commit is contained in:
Jonas Ådahl 2021-03-03 14:31:56 +01:00
parent 1972d4ec90
commit b1c643eeaa
7 changed files with 126 additions and 40 deletions

View File

@ -107,11 +107,7 @@ struct _MetaBackendClass
MetaPointerConstraint *constraint); MetaPointerConstraint *constraint);
}; };
void meta_init_backend (GType backend_gtype, void meta_backend_destroy (MetaBackend *backend);
unsigned int n_properties,
const char *names[],
const GValue *values);
void meta_release_backend (void);
void meta_backend_prepare_shutdown (MetaBackend *backend); void meta_backend_prepare_shutdown (MetaBackend *backend);

View File

@ -68,6 +68,7 @@
#include "clutter/clutter-seat-private.h" #include "clutter/clutter-seat-private.h"
#include "meta/main.h" #include "meta/main.h"
#include "meta/meta-backend.h" #include "meta/meta-backend.h"
#include "meta/meta-context.h"
#include "meta/util.h" #include "meta/util.h"
#ifdef HAVE_PROFILER #ifdef HAVE_PROFILER
@ -89,6 +90,17 @@
#include "wayland/meta-wayland.h" #include "wayland/meta-wayland.h"
#endif #endif
enum
{
PROP_0,
PROP_CONTEXT,
N_PROPS
};
static GParamSpec *obj_props[N_PROPS];
enum enum
{ {
KEYMAP_CHANGED, KEYMAP_CHANGED,
@ -122,6 +134,8 @@ meta_get_backend (void)
struct _MetaBackendPrivate struct _MetaBackendPrivate
{ {
MetaContext *context;
MetaMonitorManager *monitor_manager; MetaMonitorManager *monitor_manager;
MetaOrientationManager *orientation_manager; MetaOrientationManager *orientation_manager;
MetaCursorTracker *cursor_tracker; MetaCursorTracker *cursor_tracker;
@ -199,6 +213,8 @@ meta_backend_dispose (GObject *object)
MetaBackend *backend = META_BACKEND (object); MetaBackend *backend = META_BACKEND (object);
MetaBackendPrivate *priv = meta_backend_get_instance_private (backend); MetaBackendPrivate *priv = meta_backend_get_instance_private (backend);
_backend = NULL;
g_clear_pointer (&priv->cursor_tracker, meta_cursor_tracker_destroy); g_clear_pointer (&priv->cursor_tracker, meta_cursor_tracker_destroy);
g_clear_object (&priv->current_device); g_clear_object (&priv->current_device);
g_clear_object (&priv->monitor_manager); g_clear_object (&priv->monitor_manager);
@ -249,7 +265,7 @@ meta_backend_dispose (GObject *object)
G_OBJECT_CLASS (meta_backend_parent_class)->dispose (object); G_OBJECT_CLASS (meta_backend_parent_class)->dispose (object);
} }
static void void
meta_backend_destroy (MetaBackend *backend) meta_backend_destroy (MetaBackend *backend)
{ {
g_object_run_dispose (G_OBJECT (backend)); g_object_run_dispose (G_OBJECT (backend));
@ -737,6 +753,8 @@ meta_backend_constructed (GObject *object)
MetaBackendClass *backend_class = MetaBackendClass *backend_class =
META_BACKEND_GET_CLASS (backend); META_BACKEND_GET_CLASS (backend);
g_assert (priv->context);
#ifdef HAVE_LIBWACOM #ifdef HAVE_LIBWACOM
priv->wacom_db = libwacom_database_new (); priv->wacom_db = libwacom_database_new ();
if (!priv->wacom_db) if (!priv->wacom_db)
@ -758,6 +776,46 @@ meta_backend_constructed (GObject *object)
NULL); 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 static void
meta_backend_class_init (MetaBackendClass *klass) meta_backend_class_init (MetaBackendClass *klass)
{ {
@ -766,6 +824,8 @@ meta_backend_class_init (MetaBackendClass *klass)
object_class->dispose = meta_backend_dispose; object_class->dispose = meta_backend_dispose;
object_class->constructed = meta_backend_constructed; 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->post_init = meta_backend_real_post_init;
klass->grab_device = meta_backend_real_grab_device; 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->is_lid_closed = meta_backend_real_is_lid_closed;
klass->create_cursor_tracker = meta_backend_real_create_cursor_tracker; 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] = signals[KEYMAP_CHANGED] =
g_signal_new ("keymap-changed", g_signal_new ("keymap-changed",
G_TYPE_FROM_CLASS (object_class), 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); 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) * meta_backend_ungrab_device: (skip)
*/ */
@ -1458,34 +1542,6 @@ meta_backend_get_clutter_backend (MetaBackend *backend)
return priv->clutter_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 void
meta_backend_prepare_shutdown (MetaBackend *backend) meta_backend_prepare_shutdown (MetaBackend *backend)
{ {

View File

@ -381,6 +381,7 @@ create_x11_cm_backend (MetaContext *context,
return g_initable_new (META_TYPE_BACKEND_X11_CM, return g_initable_new (META_TYPE_BACKEND_X11_CM,
NULL, error, NULL, error,
"context", context,
"display-name", context_main->options.x11.display_name, "display-name", context_main->options.x11.display_name,
NULL); NULL);
} }
@ -392,6 +393,7 @@ create_nested_backend (MetaContext *context,
{ {
return g_initable_new (META_TYPE_BACKEND_X11_NESTED, return g_initable_new (META_TYPE_BACKEND_X11_NESTED,
NULL, error, NULL, error,
"context", context,
NULL); NULL);
} }
@ -402,6 +404,7 @@ create_headless_backend (MetaContext *context,
{ {
return g_initable_new (META_TYPE_BACKEND_NATIVE, return g_initable_new (META_TYPE_BACKEND_NATIVE,
NULL, error, NULL, error,
"context", context,
"headless", TRUE, "headless", TRUE,
NULL); NULL);
} }
@ -412,6 +415,7 @@ create_native_backend (MetaContext *context,
{ {
return g_initable_new (META_TYPE_BACKEND_NATIVE, return g_initable_new (META_TYPE_BACKEND_NATIVE,
NULL, error, NULL, error,
"context", context,
NULL); NULL);
} }
#endif /* HAVE_NATIVE_BACKEND */ #endif /* HAVE_NATIVE_BACKEND */

View File

@ -54,6 +54,8 @@ typedef struct _MetaContextPrivate
GOptionContext *option_context; GOptionContext *option_context;
MetaBackend *backend;
GMainLoop *main_loop; GMainLoop *main_loop;
GError *termination_error; GError *termination_error;
} MetaContextPrivate; } MetaContextPrivate;
@ -111,6 +113,20 @@ meta_context_notify_ready (MetaContext *context)
META_CONTEXT_GET_CLASS (context)->notify_ready (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 MetaCompositorType
meta_context_get_compositor_type (MetaContext *context) meta_context_get_compositor_type (MetaContext *context)
{ {
@ -231,7 +247,15 @@ static gboolean
meta_context_real_setup (MetaContext *context, meta_context_real_setup (MetaContext *context,
GError **error) 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 gboolean
@ -381,15 +405,13 @@ meta_context_finalize (GObject *object)
MetaContext *context = META_CONTEXT (object); MetaContext *context = META_CONTEXT (object);
MetaContextPrivate *priv = meta_context_get_instance_private (context); MetaContextPrivate *priv = meta_context_get_instance_private (context);
MetaDisplay *display; MetaDisplay *display;
MetaBackend *backend;
#ifdef HAVE_WAYLAND #ifdef HAVE_WAYLAND
MetaWaylandCompositor *compositor; MetaWaylandCompositor *compositor;
MetaCompositorType compositor_type; MetaCompositorType compositor_type;
#endif #endif
backend = meta_get_backend (); if (priv->backend)
if (backend) meta_backend_prepare_shutdown (priv->backend);
meta_backend_prepare_shutdown (backend);
#ifdef HAVE_WAYLAND #ifdef HAVE_WAYLAND
compositor = meta_wayland_compositor_get_default (); compositor = meta_wayland_compositor_get_default ();
@ -407,7 +429,7 @@ meta_context_finalize (GObject *object)
meta_wayland_finalize (); meta_wayland_finalize ();
#endif #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->option_context, g_option_context_free);
g_clear_pointer (&priv->main_loop, g_main_loop_unref); g_clear_pointer (&priv->main_loop, g_main_loop_unref);

View File

@ -49,6 +49,9 @@ META_EXPORT
void meta_backend_lock_layout_group (MetaBackend *backend, void meta_backend_lock_layout_group (MetaBackend *backend,
guint idx); guint idx);
META_EXPORT
MetaContext * meta_backend_get_context (MetaBackend *backend);
META_EXPORT META_EXPORT
ClutterActor *meta_backend_get_stage (MetaBackend *backend); ClutterActor *meta_backend_get_stage (MetaBackend *backend);

View File

@ -81,4 +81,7 @@ void meta_context_terminate_with_error (MetaContext *context,
META_EXPORT META_EXPORT
MetaCompositorType meta_context_get_compositor_type (MetaContext *context); MetaCompositorType meta_context_get_compositor_type (MetaContext *context);
META_EXPORT
MetaBackend * meta_context_get_backend (MetaContext *context);
#endif /* META_CONTEXT_H */ #endif /* META_CONTEXT_H */

View File

@ -121,6 +121,7 @@ create_nested_backend (MetaContext *context,
{ {
return g_initable_new (META_TYPE_BACKEND_TEST, return g_initable_new (META_TYPE_BACKEND_TEST,
NULL, error, NULL, error,
"context", context,
NULL); NULL);
} }
@ -131,6 +132,7 @@ create_headless_backend (MetaContext *context,
{ {
return g_initable_new (META_TYPE_BACKEND_NATIVE, return g_initable_new (META_TYPE_BACKEND_NATIVE,
NULL, error, NULL, error,
"context", context,
"headless", TRUE, "headless", TRUE,
NULL); NULL);
} }