wayland: Implement on-demand start of Xwayland

The Xwayland manager now has 4 distinct phases:
- Init and shutdown (Happening together with the compositor itself)
- Start and stop

In these last 2 phases, handle orderly initialization and shutdown
of Xwayland. On initialization We will simply find out what is a
proper display name, and set up the envvar and socket so that clients
think there is a X server.

Whenever we detect data on this socket, we enter the start phase
that will launch Xwayland, and plunge the socket directly to it.
In this phase we now also set up the MetaX11Display.

The stop phase is pretty much the opposite, we will shutdown the
MetaX11Display and all related data, terminate the Xwayland
process, and restore the listening sockets. This phase happens
on a timeout whenever the last known X11 MetaWindow is gone. If no
new X clients come back in this timeout, the X server will be
eventually terminated.

The shutdown phase happens on compositor shutdown and is completely
uninteresting. Some bits there moved into the stop phase as might
happen over and over.

This is all controlled by META_DISPLAY_POLICY_ON_DEMAND and
the "autostart-xwayland" experimental setting.

https://gitlab.gnome.org/GNOME/mutter/merge_requests/709
This commit is contained in:
Carlos Garnacho 2019-05-24 21:36:50 +02:00 committed by Carlos Garnacho
parent 0c5866a9e1
commit 141373f0ba
3 changed files with 113 additions and 21 deletions

View File

@ -49,6 +49,7 @@
#include "backends/x11/meta-backend-x11.h"
#include "backends/x11/cm/meta-backend-x11-cm.h"
#include "clutter/x11/clutter-x11.h"
#include "compositor/compositor-private.h"
#include "core/bell.h"
#include "core/boxes-private.h"
#include "core/display-private.h"
@ -643,12 +644,14 @@ meta_display_init_x11 (MetaDisplay *display,
display->x11_display = x11_display;
g_signal_emit (display, display_signals[X11_DISPLAY_OPENED], 0);
meta_x11_display_create_guard_window (x11_display);
if (!display->display_opening)
{
meta_display_manage_all_xwindows (display);
meta_compositor_redirect_x11_windows (display->compositor);
}
return TRUE;
}

View File

@ -48,6 +48,7 @@ typedef struct
char *lock_file;
int abstract_fd;
int unix_fd;
guint xserver_grace_period_id;
struct wl_display *wayland_display;
struct wl_client *client;
struct wl_resource *xserver_resource;

View File

@ -41,6 +41,7 @@
#include "compositor/meta-surface-actor-wayland.h"
#include "compositor/meta-window-actor-private.h"
#include "core/main-private.h"
#include "meta/main.h"
#include "wayland/meta-wayland-actor-surface.h"
#include "x11/meta-x11-display-private.h"
@ -71,6 +72,8 @@ G_DEFINE_TYPE (MetaWaylandSurfaceRoleXWayland,
static int display_number_override = -1;
static void meta_xwayland_stop_xserver (MetaXWaylandManager *manager);
void
meta_xwayland_associate_window_with_surface (MetaWindow *window,
MetaWaylandSurface *surface)
@ -369,23 +372,49 @@ xserver_died (GObject *source,
g_warning ("Failed to finish waiting for Xwayland: %s", error->message);
}
else if (!g_subprocess_get_successful (proc))
{
if (meta_get_x11_display_policy () == META_DISPLAY_POLICY_MANDATORY)
g_warning ("X Wayland crashed; exiting");
else
{
/* For now we simply abort if we see the server exit.
*
* In the future X will only be loaded lazily for legacy X support
* but for now it's a hard requirement. */
g_warning ("Spurious exit of X Wayland server");
g_warning ("X Wayland crashed; attempting to recover");
}
if (meta_get_x11_display_policy () == META_DISPLAY_POLICY_MANDATORY)
{
meta_exit (META_EXIT_ERROR);
}
else if (meta_get_x11_display_policy () == META_DISPLAY_POLICY_ON_DEMAND)
{
MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default ();
MetaDisplay *display = meta_get_display ();
if (display->x11_display)
meta_display_shutdown_x11 (display);
if (!meta_xwayland_init (&compositor->xwayland_manager,
compositor->wayland_display))
g_warning ("Failed to init X sockets");
}
}
static gboolean
shutdown_xwayland_cb (gpointer data)
{
MetaXWaylandManager *manager = data;
meta_verbose ("Shutting down Xwayland");
manager->xserver_grace_period_id = 0;
meta_display_shutdown_x11 (meta_get_display ());
meta_xwayland_stop_xserver (manager);
return G_SOURCE_REMOVE;
}
static int
x_io_error (Display *display)
{
g_warning ("Connection to xwayland lost");
if (meta_get_x11_display_policy () == META_DISPLAY_POLICY_MANDATORY)
meta_exit (META_EXIT_ERROR);
return 0;
@ -553,6 +582,7 @@ on_displayfd_ready (int fd,
gpointer user_data)
{
MetaXWaylandManager *manager = user_data;
MetaDisplay *display = meta_get_display ();
/* The server writes its display name to the displayfd
* socket when it's ready. We don't care about the data
@ -560,6 +590,9 @@ on_displayfd_ready (int fd,
* that means it's ready. */
xserver_finished_init (manager);
if (meta_get_x11_display_policy () == META_DISPLAY_POLICY_ON_DEMAND)
meta_display_init_x11 (display, NULL);
return G_SOURCE_REMOVE;
}
@ -637,6 +670,19 @@ meta_xwayland_start_xserver (MetaXWaylandManager *manager)
return TRUE;
}
static gboolean
xdisplay_connection_activity_cb (gint fd,
GIOCondition cond,
gpointer user_data)
{
MetaXWaylandManager *manager = user_data;
if (!meta_xwayland_start_xserver (manager))
g_critical ("Could not start Xserver");
return G_SOURCE_REMOVE;
}
static void
window_unmanaged_cb (MetaWindow *window,
MetaXWaylandManager *manager)
@ -645,6 +691,12 @@ window_unmanaged_cb (MetaWindow *window,
g_signal_handlers_disconnect_by_func (window,
window_unmanaged_cb,
manager);
if (!manager->x11_windows)
{
meta_verbose ("All X11 windows gone, setting shutdown timeout");
manager->xserver_grace_period_id =
g_timeout_add_seconds (10, shutdown_xwayland_cb, manager);
}
}
static void
@ -660,21 +712,63 @@ window_created_cb (MetaDisplay *display,
manager->x11_windows = g_list_prepend (manager->x11_windows, window);
g_signal_connect (window, "unmanaged",
G_CALLBACK (window_unmanaged_cb), manager);
if (manager->xserver_grace_period_id)
{
g_source_remove (manager->xserver_grace_period_id);
manager->xserver_grace_period_id = 0;
}
}
static void
meta_xwayland_stop_xserver (MetaXWaylandManager *manager)
{
if (manager->proc)
g_subprocess_send_signal (manager->proc, SIGTERM);
g_signal_handlers_disconnect_by_func (meta_get_display (),
window_created_cb,
manager);
g_clear_object (&manager->xserver_died_cancellable);
g_clear_object (&manager->proc);
}
gboolean
meta_xwayland_init (MetaXWaylandManager *manager,
struct wl_display *wl_display)
{
MetaDisplayPolicy policy;
gboolean fatal;
if (!manager->display_name)
{
if (!choose_xdisplay (manager))
return FALSE;
}
else
{
if (!open_display_sockets (manager, manager->display_index, &fatal))
return FALSE;
}
if (!prepare_auth_file (manager))
return FALSE;
manager->wayland_display = wl_display;
policy = meta_get_x11_display_policy ();
if (policy == META_DISPLAY_POLICY_MANDATORY)
{
return meta_xwayland_start_xserver (manager);
}
else if (policy == META_DISPLAY_POLICY_ON_DEMAND)
{
g_unix_fd_add (manager->abstract_fd, G_IO_IN,
xdisplay_connection_activity_cb, manager);
return TRUE;
}
return FALSE;
}
static void
on_x11_display_closing (MetaDisplay *display)
@ -712,13 +806,7 @@ meta_xwayland_shutdown (MetaXWaylandManager *manager)
{
char path[256];
g_signal_handlers_disconnect_by_func (meta_get_display (),
window_created_cb,
manager);
g_cancellable_cancel (manager->xserver_died_cancellable);
g_clear_object (&manager->proc);
g_clear_object (&manager->xserver_died_cancellable);
snprintf (path, sizeof path, "/tmp/.X11-unix/X%d", manager->display_index);
unlink (path);