From 50ed027b6f63e6a1b7cc2850c5e68fec1ee4c890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Fri, 16 Apr 2021 20:34:29 +0200 Subject: [PATCH] context: Move X11 display policy under the context The context implementations already effectively dictate the policy, so let them do it more directly instead of going indirectly via global variables. Part-of: --- src/core/display.c | 6 ++- src/core/main-private.h | 32 ----------- src/core/main.c | 95 --------------------------------- src/core/meta-context-main.c | 38 ++++++++++--- src/core/meta-context-private.h | 5 ++ src/core/meta-context.c | 6 +++ src/meson.build | 2 - src/tests/meta-context-test.c | 19 ++++--- src/wayland/meta-wayland.c | 8 +-- src/wayland/meta-xwayland.c | 27 +++++++--- 10 files changed, 84 insertions(+), 154 deletions(-) delete mode 100644 src/core/main-private.h delete mode 100644 src/core/main.c diff --git a/src/core/display.c b/src/core/display.c index 12dacc20d..47205effc 100644 --- a/src/core/display.c +++ b/src/core/display.c @@ -60,7 +60,6 @@ #include "core/events.h" #include "core/frame.h" #include "core/keybindings-private.h" -#include "core/main-private.h" #include "core/meta-clipboard-manager.h" #include "core/meta-workspace-manager-private.h" #include "core/util-private.h" @@ -890,7 +889,10 @@ meta_display_new (MetaContext *context, #ifdef HAVE_WAYLAND if (meta_is_wayland_compositor ()) { - if (meta_get_x11_display_policy () == META_X11_DISPLAY_POLICY_MANDATORY) + MetaX11DisplayPolicy x11_display_policy; + + x11_display_policy = meta_context_get_x11_display_policy (context); + if (x11_display_policy == META_X11_DISPLAY_POLICY_MANDATORY) { meta_display_init_x11 (display, NULL, (GAsyncReadyCallback) on_x11_initialized, diff --git a/src/core/main-private.h b/src/core/main-private.h deleted file mode 100644 index e35efe8fe..000000000 --- a/src/core/main-private.h +++ /dev/null @@ -1,32 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ - -/* - * Copyright (C) 2016 Red Hat, Inc. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ - -#ifndef META_MAIN_PRIVATE_H -#define META_MAIN_PRIVATE_H - -#include "core/util-private.h" -#include "core/meta-private-enums.h" - -META_EXPORT_TEST -MetaX11DisplayPolicy meta_get_x11_display_policy (void); - -META_EXPORT_TEST -void meta_override_x11_display_policy (MetaX11DisplayPolicy x11_display_policy); - -#endif /* META_MAIN_PRIVATE_H */ diff --git a/src/core/main.c b/src/core/main.c deleted file mode 100644 index cd5075936..000000000 --- a/src/core/main.c +++ /dev/null @@ -1,95 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ - -/* Mutter main() */ - -/* - * Copyright (C) 2001 Havoc Pennington - * Copyright (C) 2006 Elijah Newren - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ - -/** - * SECTION:main - * @title: Main - * @short_description: Program startup. - * - * Functions which parse the command-line arguments, create the display, - * kick everything off and then close down Mutter when it's time to go. - * - * - * - * Mutter - a boring window manager for the adult in you - * - * Many window managers are like Marshmallow Froot Loops; Mutter - * is like Frosted Flakes: it's still plain old corn, but dusted - * with some sugar. - * - * The best way to get a handle on how the whole system fits together - * is discussed in doc/code-overview.txt; if you're looking for functions - * to investigate, read main(), meta_display_open(), and event_callback(). - */ - -#include "config.h" - -#include "meta/main.h" - -#include "backends/x11/cm/meta-backend-x11-cm.h" -#include "core/meta-context-private.h" -#include "core/main-private.h" - -#if defined(HAVE_NATIVE_BACKEND) && defined(HAVE_WAYLAND) -#include -#endif /* HAVE_WAYLAND && HAVE_NATIVE_BACKEND */ - -MetaContext * -meta_get_context_temporary (void); - -static MetaX11DisplayPolicy x11_display_policy_override = -1; - -void -meta_override_x11_display_policy (MetaX11DisplayPolicy x11_display_policy) -{ - x11_display_policy_override = x11_display_policy; -} - -MetaX11DisplayPolicy -meta_get_x11_display_policy (void) -{ - MetaBackend *backend = meta_get_backend (); - - if (META_IS_BACKEND_X11_CM (backend)) - return META_X11_DISPLAY_POLICY_MANDATORY; - - if (x11_display_policy_override != -1) - return x11_display_policy_override; - -#ifdef HAVE_WAYLAND - if (meta_is_wayland_compositor ()) - { -#ifdef HAVE_XWAYLAND_INITFD - g_autofree char *unit = NULL; -#endif - -#ifdef HAVE_XWAYLAND_INITFD - if (sd_pid_get_user_unit (0, &unit) < 0) - return META_X11_DISPLAY_POLICY_MANDATORY; - else - return META_X11_DISPLAY_POLICY_ON_DEMAND; -#endif - } -#endif - - return META_X11_DISPLAY_POLICY_MANDATORY; -} diff --git a/src/core/meta-context-main.c b/src/core/meta-context-main.c index d3b2f80c8..6c40ba526 100644 --- a/src/core/meta-context-main.c +++ b/src/core/meta-context-main.c @@ -34,7 +34,6 @@ #include "backends/meta-monitor-manager-private.h" #include "backends/meta-virtual-monitor.h" #include "backends/x11/cm/meta-backend-x11-cm.h" -#include "core/main-private.h" #include "meta/meta-backend.h" #include "wayland/meta-wayland.h" #include "x11/session.h" @@ -279,11 +278,6 @@ meta_context_main_configure (MetaContext *context, if (!check_configuration (context_main, error)) return FALSE; -#ifdef HAVE_WAYLAND - if (context_main->options.no_x11) - meta_override_x11_display_policy (META_X11_DISPLAY_POLICY_DISABLED); -#endif - context_main->compositor_type = determine_compositor_type (context_main, error); if (context_main->compositor_type == -1) @@ -305,6 +299,36 @@ meta_context_main_get_compositor_type (MetaContext *context) return context_main->compositor_type; } +static MetaX11DisplayPolicy +meta_context_main_get_x11_display_policy (MetaContext *context) +{ + MetaCompositorType compositor_type; +#ifdef HAVE_WAYLAND + MetaContextMain *context_main = META_CONTEXT_MAIN (context); + char *unit; +#endif + + compositor_type = meta_context_get_compositor_type (context); + switch (compositor_type) + { + case META_COMPOSITOR_TYPE_X11: + return META_X11_DISPLAY_POLICY_MANDATORY; + case META_COMPOSITOR_TYPE_WAYLAND: +#ifdef HAVE_WAYLAND + if (context_main->options.no_x11) + return META_X11_DISPLAY_POLICY_DISABLED; + else if (sd_pid_get_user_unit (0, &unit) < 0) + return META_X11_DISPLAY_POLICY_MANDATORY; + else + return META_X11_DISPLAY_POLICY_ON_DEMAND; +#else /* HAVE_WAYLAND */ + g_assert_not_reached (); +#endif /* HAVE_WAYLAND */ + } + + g_assert_not_reached (); +} + #ifdef HAVE_NATIVE_BACKEND static gboolean add_persistent_virtual_monitors (MetaContextMain *context_main, @@ -656,6 +680,8 @@ meta_context_main_class_init (MetaContextMainClass *klass) context_class->configure = meta_context_main_configure; context_class->get_compositor_type = meta_context_main_get_compositor_type; + context_class->get_x11_display_policy = + meta_context_main_get_x11_display_policy; context_class->setup = meta_context_main_setup; context_class->create_backend = meta_context_main_create_backend; context_class->notify_ready = meta_context_main_notify_ready; diff --git a/src/core/meta-context-private.h b/src/core/meta-context-private.h index 03c9455a5..422316cb5 100644 --- a/src/core/meta-context-private.h +++ b/src/core/meta-context-private.h @@ -21,6 +21,7 @@ #ifndef META_CONTEXT_PRIVATE_H #define META_CONTEXT_PRIVATE_H +#include "core/meta-private-enums.h" #include "meta/meta-backend.h" #include "meta/meta-context.h" #include "wayland/meta-wayland-types.h" @@ -36,6 +37,8 @@ struct _MetaContextClass MetaCompositorType (* get_compositor_type) (MetaContext *context); + MetaX11DisplayPolicy (* get_x11_display_policy) (MetaContext *context); + gboolean (* setup) (MetaContext *context, GError **error); @@ -51,4 +54,6 @@ const char * meta_context_get_gnome_wm_keybindings (MetaContext *context); MetaWaylandCompositor * meta_context_get_wayland_compositor (MetaContext *context); +MetaX11DisplayPolicy meta_context_get_x11_display_policy (MetaContext *context); + #endif /* META_CONTEXT_PRIVATE_H */ diff --git a/src/core/meta-context.c b/src/core/meta-context.c index 82e8c90e7..79c3ff9d7 100644 --- a/src/core/meta-context.c +++ b/src/core/meta-context.c @@ -212,6 +212,12 @@ meta_context_get_compositor_type (MetaContext *context) return META_CONTEXT_GET_CLASS (context)->get_compositor_type (context); } +MetaX11DisplayPolicy +meta_context_get_x11_display_policy (MetaContext *context) +{ + return META_CONTEXT_GET_CLASS (context)->get_x11_display_policy (context); +} + static gboolean meta_context_real_configure (MetaContext *context, int *argc, diff --git a/src/meson.build b/src/meson.build index 4a810fb8d..36a339960 100644 --- a/src/meson.build +++ b/src/meson.build @@ -358,8 +358,6 @@ mutter_sources = [ 'core/frame.h', 'core/keybindings.c', 'core/keybindings-private.h', - 'core/main-private.h', - 'core/main.c', 'core/meta-accel-parse.c', 'core/meta-accel-parse.h', 'core/meta-anonymous-file.c', diff --git a/src/tests/meta-context-test.c b/src/tests/meta-context-test.c index 2f88d8c72..db544c04d 100644 --- a/src/tests/meta-context-test.c +++ b/src/tests/meta-context-test.c @@ -25,7 +25,6 @@ #include #include -#include "core/main-private.h" #include "tests/meta-backend-test.h" #include "tests/test-utils.h" #include "wayland/meta-wayland.h" @@ -92,6 +91,17 @@ meta_context_test_get_compositor_type (MetaContext *context) return META_COMPOSITOR_TYPE_WAYLAND; } +static MetaX11DisplayPolicy +meta_context_test_get_x11_display_policy (MetaContext *context) +{ + MetaContextTest *context_test = META_CONTEXT_TEST (context); + + if (context_test->flags & META_CONTEXT_TEST_FLAG_NO_X11) + return META_X11_DISPLAY_POLICY_DISABLED; + else + return META_X11_DISPLAY_POLICY_ON_DEMAND; +} + static gboolean meta_context_test_setup (MetaContext *context, GError **error) @@ -241,11 +251,6 @@ meta_create_test_context (MetaContextTestType type, context_test->type = type; context_test->flags = flags; - /* NOTE: This will be removed in a follow up commit, but is needed - * until the override method is replaced. */ - if (flags & META_CONTEXT_TEST_FLAG_NO_X11) - meta_override_x11_display_policy (META_X11_DISPLAY_POLICY_DISABLED); - return META_CONTEXT (context_test); } @@ -256,6 +261,8 @@ meta_context_test_class_init (MetaContextTestClass *klass) context_class->configure = meta_context_test_configure; context_class->get_compositor_type = meta_context_test_get_compositor_type; + context_class->get_x11_display_policy = + meta_context_test_get_x11_display_policy; context_class->setup = meta_context_test_setup; context_class->create_backend = meta_context_test_create_backend; context_class->notify_ready = meta_context_test_notify_ready; diff --git a/src/wayland/meta-wayland.c b/src/wayland/meta-wayland.c index be59fc149..e2832bbc4 100644 --- a/src/wayland/meta-wayland.c +++ b/src/wayland/meta-wayland.c @@ -31,7 +31,6 @@ #include "clutter/clutter.h" #include "cogl/cogl-egl.h" #include "compositor/meta-surface-actor-wayland.h" -#include "core/main-private.h" #include "core/meta-context-private.h" #include "wayland/meta-wayland-activation.h" #include "wayland/meta-wayland-buffer.h" @@ -518,6 +517,7 @@ meta_wayland_compositor_new (MetaContext *context) ClutterActor *stage = meta_backend_get_stage (backend); MetaWaylandCompositor *compositor; GSource *wayland_event_source; + MetaX11DisplayPolicy x11_display_policy; compositor = g_object_new (META_TYPE_WAYLAND_COMPOSITOR, NULL); compositor->context = context; @@ -577,7 +577,9 @@ meta_wayland_compositor_new (MetaContext *context) meta_wayland_eglstream_controller_init (compositor); #endif - if (meta_get_x11_display_policy () != META_X11_DISPLAY_POLICY_DISABLED) + x11_display_policy = + meta_context_get_x11_display_policy (compositor->context); + if (x11_display_policy != META_X11_DISPLAY_POLICY_DISABLED) { g_autoptr (GError) error = NULL; @@ -609,7 +611,7 @@ meta_wayland_compositor_new (MetaContext *context) g_message ("Using Wayland display name '%s'", compositor->display_name); - if (meta_get_x11_display_policy () != META_X11_DISPLAY_POLICY_DISABLED) + if (x11_display_policy != META_X11_DISPLAY_POLICY_DISABLED) { set_gnome_env ("GNOME_SETUP_DISPLAY", compositor->xwayland_manager.private_connection.name); set_gnome_env ("DISPLAY", compositor->xwayland_manager.public_connection.name); diff --git a/src/wayland/meta-xwayland.c b/src/wayland/meta-xwayland.c index 345ade297..dce4facd0 100644 --- a/src/wayland/meta-xwayland.c +++ b/src/wayland/meta-xwayland.c @@ -47,7 +47,6 @@ #include "backends/meta-monitor-manager-private.h" #include "backends/meta-settings-private.h" -#include "core/main-private.h" #include "meta/main.h" #include "meta/meta-backend.h" #include "wayland/meta-xwayland-surface.h" @@ -536,10 +535,14 @@ xserver_died (GObject *source, GAsyncResult *result, gpointer user_data) { + MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default (); GSubprocess *proc = G_SUBPROCESS (source); MetaDisplay *display = meta_get_display (); g_autoptr (GError) error = NULL; + MetaX11DisplayPolicy x11_display_policy; + x11_display_policy = + meta_context_get_x11_display_policy (compositor->context); if (!g_subprocess_wait_finish (proc, result, &error)) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) @@ -549,19 +552,18 @@ xserver_died (GObject *source, } else if (!g_subprocess_get_successful (proc)) { - if (meta_get_x11_display_policy () == META_X11_DISPLAY_POLICY_MANDATORY) + if (x11_display_policy == META_X11_DISPLAY_POLICY_MANDATORY) g_warning ("X Wayland crashed; exiting"); else g_warning ("X Wayland crashed; attempting to recover"); } - if (meta_get_x11_display_policy () == META_X11_DISPLAY_POLICY_MANDATORY) + if (x11_display_policy == META_X11_DISPLAY_POLICY_MANDATORY) { meta_exit (META_EXIT_ERROR); } - else if (meta_get_x11_display_policy () == META_X11_DISPLAY_POLICY_ON_DEMAND) + else if (x11_display_policy == META_X11_DISPLAY_POLICY_ON_DEMAND) { - MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default (); g_autoptr (GError) error = NULL; if (display->x11_display) @@ -609,9 +611,14 @@ shutdown_xwayland_cb (gpointer data) static int x_io_error (Display *display) { + MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default (); + MetaX11DisplayPolicy x11_display_policy; + g_warning ("Connection to xwayland lost"); - if (meta_get_x11_display_policy () == META_X11_DISPLAY_POLICY_MANDATORY) + x11_display_policy = + meta_context_get_x11_display_policy (compositor->context); + if (x11_display_policy == META_X11_DISPLAY_POLICY_MANDATORY) meta_exit (META_EXIT_ERROR); return 0; @@ -1143,6 +1150,7 @@ meta_xwayland_init (MetaXWaylandManager *manager, struct wl_display *wl_display, GError **error) { + MetaContext *context = compositor->context; MetaX11DisplayPolicy policy; int display = 0; @@ -1186,7 +1194,7 @@ meta_xwayland_init (MetaXWaylandManager *manager, manager->private_connection.name); manager->wayland_display = wl_display; - policy = meta_get_x11_display_policy (); + policy = meta_context_get_x11_display_policy (context); if (policy == META_X11_DISPLAY_POLICY_ON_DEMAND) { @@ -1253,6 +1261,7 @@ meta_xwayland_complete_init (MetaDisplay *display, { MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default (); MetaXWaylandManager *manager = &compositor->xwayland_manager; + MetaX11DisplayPolicy x11_display_policy; /* We install an X IO error handler in addition to the child watch, because after Xlib connects our child watch may not be called soon @@ -1270,7 +1279,9 @@ meta_xwayland_complete_init (MetaDisplay *display, add_local_user_to_xhost (xdisplay); meta_xwayland_init_xrandr (manager, xdisplay); - if (meta_get_x11_display_policy () == META_X11_DISPLAY_POLICY_ON_DEMAND) + x11_display_policy = + meta_context_get_x11_display_policy (compositor->context); + if (x11_display_policy == META_X11_DISPLAY_POLICY_ON_DEMAND) { meta_xwayland_stop_xserver_timeout (manager); g_signal_connect (meta_get_display (), "window-created",