[mutter] context: Move X11 display policy under the context



commit 50ed027b6f63e6a1b7cc2850c5e68fec1ee4c890
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Fri Apr 16 20:34:29 2021 +0200

    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: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1861>

 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(-)
---
diff --git a/src/core/display.c b/src/core/display.c
index 12dacc20d1..47205effcc 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/meta-context-main.c b/src/core/meta-context-main.c
index d3b2f80c86..6c40ba526f 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 03c9455a51..422316cb59 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 82e8c90e75..79c3ff9d7f 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 4a810fb8d5..36a339960f 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 2f88d8c729..db544c04d4 100644
--- a/src/tests/meta-context-test.c
+++ b/src/tests/meta-context-test.c
@@ -25,7 +25,6 @@
 #include <glib.h>
 #include <gio/gio.h>
 
-#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 be59fc1493..e2832bbc41 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 345ade297e..dce4facd01 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",


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