From 6710b03d1ebafc15ee4e826a5281204c6441c083 Mon Sep 17 00:00:00 2001
From: "Owen W. Taylor" <otaylor@fishsoup.net>
Date: Thu, 8 May 2014 18:35:49 -0400
Subject: [PATCH 1/4] Add a framework for restarting the compositor with nice
visuals
The current GNOME Shell Alt-F2 restart looks very messy and also
provides no indication to the user what is going on. We need to
restart the compositor to switch in and out of stereo mode, so
add a framework for doing this more cleanly:
Additions:
meta_restart(): restarts the compositor with a message
MetaDisplay::show-restart-message: signal the embedding
shell to show a message
MetaDisplay::restart: signal the embedding shell to restart
itself.
meta_is_restart(): indicates whether the current instance is a
restart so we can suppress login animations.
A helper program meta-restart-helper holds the composite overlay
window up during the restart to avoid visual artifacts.
https://bugzilla.gnome.org/show_bug.cgi?id=733026
---
configure.ac | 1 +
src/Makefile.am | 5 ++
src/compositor/compositor.c | 5 ++
src/core/display-private.h | 7 ++
src/core/display.c | 80 +++++++++++++++++
src/core/main.c | 3 +
src/core/restart-helper.c | 82 +++++++++++++++++
src/core/restart.c | 212 ++++++++++++++++++++++++++++++++++++++++++++
src/meta/main.h | 3 +
9 files changed, 398 insertions(+)
create mode 100644 src/core/restart-helper.c
create mode 100644 src/core/restart.c
diff --git a/configure.ac b/configure.ac
index 6e9ca8c..dcd8a54 100644
--- a/configure.ac
+++ b/configure.ac
@@ -69,6 +69,7 @@ CLUTTER_PACKAGE=clutter-1.0
MUTTER_PC_MODULES="
gtk+-3.0 >= 3.3.7
gio-2.0 >= 2.25.10
+ gio-unix-2.0 >= 2.25.10
pango >= 1.2.0
cairo >= 1.10.0
gsettings-desktop-schemas >= 3.7.3
diff --git a/src/Makefile.am b/src/Makefile.am
index e2cec91..d664b54 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -119,6 +119,7 @@ libmutter_la_SOURCES = \
core/screen-private.h \
meta/screen.h \
meta/types.h \
+ core/restart.c \
core/session.c \
core/session.h \
core/stack.c \
@@ -213,6 +214,10 @@ bin_PROGRAMS=mutter mutter-theme-viewer
mutter_SOURCES = core/mutter.c
mutter_LDADD = $(MUTTER_LIBS) libmutter.la
+libexec_PROGRAMS = mutter-restart-helper
+mutter_restart_helper_SOURCES = core/restart-helper.c
+mutter_restart_helper_LDADD = $(MUTTER_LIBS)
+
if HAVE_INTROSPECTION
include $(INTROSPECTION_MAKEFILE)
diff --git a/src/compositor/compositor.c b/src/compositor/compositor.c
index 91b477f..539a7a6 100644
--- a/src/compositor/compositor.c
+++ b/src/compositor/compositor.c
@@ -86,6 +86,7 @@
#include "meta-window-group.h"
#include "window-private.h" /* to check window->hidden */
#include "display-private.h" /* for meta_display_lookup_x_window() */
+#include "util-private.h"
#include <X11/extensions/shape.h>
#include <X11/extensions/Xcomposite.h>
@@ -216,6 +217,10 @@ get_output_window (MetaScreen *screen)
xroot = meta_screen_get_xroot (screen);
output = XCompositeGetOverlayWindow (xdisplay, xroot);
+ /* Now that we've gotten taken a reference count on the COW, we
+ * can close the helper that is holding on to it */
+ meta_restart_finish ();
+
meta_core_add_old_event_mask (xdisplay, output, &mask);
XISetMask (mask.mask, XI_KeyPress);
diff --git a/src/core/display-private.h b/src/core/display-private.h
index 3423fb1..d227492 100644
--- a/src/core/display-private.h
+++ b/src/core/display-private.h
@@ -470,4 +470,11 @@ gboolean meta_display_process_barrier_event (MetaDisplay *display,
XIBarrierEvent *event);
#endif /* HAVE_XI23 */
+gboolean meta_display_show_restart_message (MetaDisplay *display,
+ const char *message);
+gboolean meta_display_request_restart (MetaDisplay *display);
+
+void meta_restart_init (void);
+void meta_restart_finish (void);
+
#endif
diff --git a/src/core/display.c b/src/core/display.c
index d611314..9ed0f6e 100644
--- a/src/core/display.c
+++ b/src/core/display.c
@@ -146,6 +146,8 @@ enum
WINDOW_MARKED_URGENT,
GRAB_OP_BEGIN,
GRAB_OP_END,
+ SHOW_RESTART_MESSAGE,
+ RESTART,
LAST_SIGNAL
};
@@ -322,6 +324,59 @@ meta_display_class_init (MetaDisplayClass *klass)
META_TYPE_WINDOW,
META_TYPE_GRAB_OP);
+ /**
+ * MetaDisplay::show-restart-message:
+ * @display: the #MetaDisplay instance
+ * @message: (allow-none): The message to display, or %NULL
+ * to clear a previous restart message.
+ *
+ * The ::show-restart-message signal will be emitted to indicate
+ * that the compositor should show a message during restart. This is
+ * emitted when meta_restart() is called, either by Mutter
+ * internally or by the embedding compositor. The message should be
+ * immediately added to the Clutter stage in its final form -
+ * ::restart will be emitted to exit the application and leave the
+ * stage contents frozen as soon as the the stage is painted again.
+ *
+ * On case of failure to restart, this signal will be emitted again
+ * with %NULL for @message.
+ *
+ * Returns: %TRUE means the message was added to the stage; %FALSE
+ * indicates that the compositor did not show the message.
+ */
+ display_signals[SHOW_RESTART_MESSAGE] =
+ g_signal_new ("show-restart-message",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ g_signal_accumulator_true_handled,
+ NULL, NULL,
+ G_TYPE_BOOLEAN, 1,
+ G_TYPE_STRING);
+
+ /**
+ * MetaDisplay::restart:
+ * @display: the #MetaDisplay instance
+ *
+ * The ::restart signal is emitted to indicate that compositor
+ * should reexec the process. This is
+ * emitted when meta_restart() is called, either by Mutter
+ * internally or by the embedding compositor. See also
+ * ::show-restart-message.
+ *
+ * Returns: %FALSE to indicate that the compositor could not
+ * be restarted. When the compositor is restarted, the signal
+ * should not return.
+ */
+ display_signals[RESTART] =
+ g_signal_new ("restart",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ g_signal_accumulator_true_handled,
+ NULL, NULL,
+ G_TYPE_BOOLEAN, 0);
+
g_object_class_install_property (object_class,
PROP_FOCUS_WINDOW,
g_param_spec_object ("focus-window",
@@ -5856,3 +5911,28 @@ meta_display_clear_mouse_mode (MetaDisplay *display)
{
display->mouse_mode = FALSE;
}
+
+gboolean
+meta_display_show_restart_message (MetaDisplay *display,
+ const char *message)
+{
+ gboolean result = FALSE;
+
+ g_signal_emit (display,
+ display_signals[SHOW_RESTART_MESSAGE], 0,
+ message, &result);
+
+ return result;
+}
+
+gboolean
+meta_display_request_restart (MetaDisplay *display)
+{
+ gboolean result = FALSE;
+
+ g_signal_emit (display,
+ display_signals[RESTART], 0,
+ &result);
+
+ return result;
+}
diff --git a/src/core/main.c b/src/core/main.c
index 4bec3d2..d5bde44 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -53,6 +53,7 @@
#include <meta/errors.h>
#include "ui.h"
#include "session.h"
+#include "stereo.h"
#include <meta/prefs.h>
#include <meta/compositor.h>
@@ -441,6 +442,8 @@ meta_init (void)
meta_ui_init ();
+ meta_restart_init ();
+
/*
* Clutter can only be initialized after the UI.
*/
diff --git a/src/core/restart-helper.c b/src/core/restart-helper.c
new file mode 100644
index 0000000..57a19fb
--- /dev/null
+++ b/src/core/restart-helper.c
@@ -0,0 +1,82 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
+/*
+ * SECTION:restart-helper
+ * @short_description: helper program during a restart
+ *
+ * To smoothly restart Mutter, we want to keep the composite
+ * overlay window enabled during the restart. This is done by
+ * spawning this program, which keeps a reference to the the composite
+ * overlay window until Mutter picks it back up.
+ */
+
+/*
+ * Copyright (C) 2014 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <X11/Xlib.h>
+#include <X11/extensions/Xcomposite.h>
+
+int
+main (int argc,
+ char **argv)
+{
+ Display *display = XOpenDisplay (NULL);
+ Window selection_window;
+ XSetWindowAttributes xwa;
+ unsigned long mask = 0;
+
+ xwa.override_redirect = True;
+ mask |= CWOverrideRedirect;
+
+
+ XCompositeGetOverlayWindow (display, DefaultRootWindow (display));
+
+ selection_window = XCreateWindow (display,
+ DefaultRootWindow (display),
+ -100, -100, 1, 1, 0,
+ 0,
+ InputOnly,
+ DefaultVisual (display, DefaultScreen (display)),
+ mask, &xwa);
+
+ XSetSelectionOwner (display,
+ XInternAtom (display, "_MUTTER_RESTART_HELPER", False),
+ selection_window,
+ CurrentTime);
+
+ /* Mutter looks for an (arbitrary) line printed to stdout to know that
+ * we have started and have a reference to the COW. XSync() so that
+ * everything is set on the X server before Mutter starts restarting.
+ */
+ XSync (display, False);
+
+ printf ("STARTED\n");
+ fflush (stdout);
+
+ while (True)
+ {
+ XEvent xev;
+
+ XNextEvent (display, &xev);
+ /* Mutter restarted and unset the selection to indicate that
+ * it has a reference on the COW again */
+ if (xev.xany.type == SelectionClear)
+ return 0;
+ }
+}
diff --git a/src/core/restart.c b/src/core/restart.c
new file mode 100644
index 0000000..04ef7d5
--- /dev/null
+++ b/src/core/restart.c
@@ -0,0 +1,212 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
+/*
+ * Copyright (C) 2014 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 <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * SECTION:restart
+ * @short_description: Smoothly restart the compositor
+ *
+ * There are some cases where we need to restart Mutter in order
+ * to deal with changes in state - the particular case inspiring
+ * this is enabling or disabling stereo output. To make this
+ * fairly smooth for the user, we need to do two things:
+ *
+ * - Display a message to the user and make sure that it is
+ * actually painted before we exit.
+ * - Use a helper program so that the Composite Overlay Window
+ * isn't unmapped and mapped.
+ *
+ * This handles both of these.
+ */
+
+#include <config.h>
+
+#include <clutter/clutter.h>
+#include <gio/gunixinputstream.h>
+
+#include <meta/main.h>
+#include <meta/util.h>
+#include "ui.h"
+#include "display-private.h"
+
+static gboolean restart_helper_started = FALSE;
+static gboolean restart_message_shown = FALSE;
+static gboolean is_restart = FALSE;
+
+void
+meta_restart_init (void)
+{
+ Display *xdisplay = meta_ui_get_display ();
+ Atom atom_restart_helper = XInternAtom (xdisplay, "_MUTTER_RESTART_HELPER", False);
+ Window restart_helper_window = NULL;
+
+ restart_helper_window = XGetSelectionOwner (xdisplay, atom_restart_helper);
+ if (restart_helper_window)
+ is_restart = TRUE;
+}
+
+static void
+restart_check_ready (void)
+{
+ if (restart_helper_started && restart_message_shown)
+ meta_display_request_restart (meta_get_display ());
+}
+
+static void
+restart_helper_read_line_callback (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ gsize length;
+ char *line = g_data_input_stream_read_line_finish_utf8 (G_DATA_INPUT_STREAM (source_object),
+ res,
+ &length, &error);
+ if (line == NULL)
+ {
+ meta_warning ("Failed to read output from restart helper%s%s\n",
+ error ? ": " : NULL,
+ error ? error->message : NULL);
+ }
+ else
+ g_free (line); /* We don't actually care what the restart helper outputs */
+
+ g_object_unref (source_object);
+
+ restart_helper_started = TRUE;
+ restart_check_ready ();
+}
+
+static gboolean
+restart_message_painted (gpointer data)
+{
+ restart_message_shown = TRUE;
+ restart_check_ready ();
+
+ return FALSE;
+}
+
+/**
+ * meta_restart:
+ * @message: message to display to the user.
+ *
+ * Starts the process of restarting the compositor. Note that Mutter's
+ * involvement here is to make the restart visually smooth for the
+ * user - it cannot itself safely reexec a program that embeds libmuttter.
+ * So in order for this to work, the compositor must handle two
+ * signals - MetaDisplay::show-restart-message, to display the
+ * message passed here on the Clutter stage, and ::restart to actually
+ * reexec the compositor.
+ */
+void
+meta_restart (const char *message)
+{
+ MetaDisplay *display = meta_get_display();
+ GInputStream *unix_stream;
+ GDataInputStream *data_stream;
+ GError *error = NULL;
+ int helper_out_fd;
+
+ static const char * const helper_argv[] = {
+ MUTTER_LIBEXECDIR "/mutter-restart-helper", NULL
+ };
+
+ if (meta_display_show_restart_message (display, message))
+ {
+ /* Wait until the stage was painted */
+ clutter_threads_add_repaint_func_full (CLUTTER_REPAINT_FLAGS_POST_PAINT,
+ restart_message_painted,
+ NULL, NULL);
+ }
+ else
+ {
+ /* Can't show the message, show the message as soon as the
+ * restart helper starts
+ */
+ restart_message_painted (NULL);
+ }
+
+ /* We also need to wait for the restart helper to get its
+ * reference to the Composite Overlay Window.
+ */
+ if (!g_spawn_async_with_pipes (NULL, /* working directory */
+ (char **)helper_argv,
+ NULL, /* envp */
+ 0, /* G_SPAWN_DEFAULT */
+ NULL, NULL, /* child_setup */
+ NULL, /* child_pid */
+ NULL, /* standard_input */
+ &helper_out_fd,
+ NULL, /* standard_error */
+ &error))
+ {
+ meta_warning ("Failed to start restart helper: %s\n", error->message);
+ goto error;
+ }
+
+ unix_stream = g_unix_input_stream_new (helper_out_fd, TRUE);
+ data_stream = g_data_input_stream_new (unix_stream);
+ g_object_unref (unix_stream);
+
+ g_data_input_stream_read_line_async (data_stream, G_PRIORITY_DEFAULT,
+ NULL, restart_helper_read_line_callback,
+ &error);
+ if (error != NULL)
+ {
+ meta_warning ("Failed to read from restart helper: %s\n", error->message);
+ g_object_unref (data_stream);
+ goto error;
+ }
+
+ return;
+
+ error:
+ /* If starting the restart helper fails, then we just go ahead and restart
+ * immediately. We won't get a smooth transition, since the overlay window
+ * will be destroyed and recreated, but otherwise it will work fine.
+ */
+ restart_helper_started = TRUE;
+ restart_check_ready ();
+
+ return;
+}
+
+void
+meta_restart_finish (void)
+{
+ if (is_restart)
+ {
+ Display *xdisplay = meta_display_get_xdisplay (meta_get_display ());
+ Atom atom_restart_helper = XInternAtom (xdisplay, "_MUTTER_RESTART_HELPER", False);
+ XSetSelectionOwner (xdisplay, atom_restart_helper, None, CurrentTime);
+ }
+}
+
+/**
+ * meta_is_restart:
+ *
+ * Returns %TRUE if this instance of Mutter comes from Mutter
+ * restarting itself (for example to enable/disable stereo.)
+ * See meta_restart(). If this is the case, any startup visuals
+ * or animations should be suppressed.
+ */
+gboolean
+meta_is_restart (void)
+{
+ return is_restart;
+}
diff --git a/src/meta/main.h b/src/meta/main.h
index dd4e7f1..af7a2c9 100644
--- a/src/meta/main.h
+++ b/src/meta/main.h
@@ -35,6 +35,9 @@ gboolean meta_get_replace_current_wm (void); /* Actually defined in util
void meta_set_wm_name (const char *wm_name);
void meta_set_gnome_wm_keybindings (const char *wm_keybindings);
+void meta_restart (const char *message);
+gboolean meta_is_restart (void);
+
/**
* MetaExitCode:
* @META_EXIT_SUCCESS: Success
--
1.9.3