From ef3427575fe49c19f35defd292d647c0792e2fc4 Mon Sep 17 00:00:00 2001 From: Mario Sanchez Prada Date: Mon, 11 Jun 2018 16:09:06 +0100 Subject: [PATCH] display: Avoid unsetting the DESKTOP_STARTUP_ID variable too late Similar to what has been done recently for DESKTOP_AUTOSTART_ID [1], we need to get rid of this call to g_unsetenv() in the displays' backends for X11 and Wayland, so that it's guarantee to happen any thread is created, while still being accessible when needed. Let's stash the value of this environment variable when loading the GDK library, and provide a private method so that it can be retrieved from the displays' backend when implementing gdk_display_make_default(). [1] https://gitlab.gnome.org/GNOME/gtk/commit/22269902 Closes: https://gitlab.gnome.org/GNOME/gtk/issues/979 --- gdk/gdk-private.h | 2 ++ gdk/gdk.c | 50 ++++++++++++++++++++++++++++++++ gdk/wayland/gdkdisplay-wayland.c | 18 ++++-------- gdk/x11/gdkdisplay-x11.c | 16 ++-------- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/gdk/gdk-private.h b/gdk/gdk-private.h index b4a16c5659..c6a45ed48f 100644 --- a/gdk/gdk-private.h +++ b/gdk/gdk-private.h @@ -40,4 +40,6 @@ void gdk_display_set_cursor_theme (GdkDisplay *display, const char *theme, int size); +const gchar * gdk_get_startup_notification_id (void); + #endif /* __GDK__PRIVATE_H__ */ diff --git a/gdk/gdk.c b/gdk/gdk.c index 72d1478f69..94d27ee87e 100644 --- a/gdk/gdk.c +++ b/gdk/gdk.c @@ -154,6 +154,36 @@ static const GDebugKey gdk_debug_keys[] = { }; #endif + +#ifdef G_HAS_CONSTRUCTORS +#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA +#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(stash_desktop_startup_notification_id) +#endif +G_DEFINE_CONSTRUCTOR(stash_desktop_startup_notification_id) +#endif + +static gchar *startup_notification_id = NULL; + +static void +stash_desktop_startup_notification_id (void) +{ + const char *desktop_startup_id; + + desktop_startup_id = g_getenv ("DESKTOP_STARTUP_ID"); + if (desktop_startup_id && *desktop_startup_id != '\0') + { + if (!g_utf8_validate (desktop_startup_id, -1, NULL)) + g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8"); + else + startup_notification_id = g_strdup (desktop_startup_id ? desktop_startup_id : ""); + } + + /* Clear the environment variable so it won't be inherited by + * child processes and confuse things. + */ + g_unsetenv ("DESKTOP_STARTUP_ID"); +} + static gpointer register_resources (gpointer dummy G_GNUC_UNUSED) { @@ -186,6 +216,10 @@ gdk_pre_parse (void) G_N_ELEMENTS (gdk_debug_keys)); } #endif /* G_ENABLE_DEBUG */ + +#ifndef G_HAS_CONSTRUCTORS + stash_desktop_startup_notification_id (); +#endif } /*< private > @@ -216,6 +250,22 @@ gdk_display_open_default (void) return display; } +/*< private > + * + * gdk_get_startup_notification_id + * + * Returns the original value of the DESKTOP_STARTUP_ID environment + * variable if it was defined and valid, or %NULL otherwise. + * + * Returns: (nullable) (transfer none): the original value of the + * DESKTOP_STARTUP_ID environment variable, or %NULL. + */ +const gchar * +gdk_get_startup_notification_id (void) +{ + return startup_notification_id; +} + /** * SECTION:threads * @Short_description: Functions for using GDK in multi-threaded programs diff --git a/gdk/wayland/gdkdisplay-wayland.c b/gdk/wayland/gdkdisplay-wayland.c index 3fb9fda050..2f46817be4 100644 --- a/gdk/wayland/gdkdisplay-wayland.c +++ b/gdk/wayland/gdkdisplay-wayland.c @@ -54,6 +54,8 @@ #include "wm-button-layout-translation.h" +#include "gdk/gdk-private.h" + /** * SECTION:wayland_interaction * @Short_description: Wayland backend-specific functions @@ -817,19 +819,9 @@ gdk_wayland_display_make_default (GdkDisplay *display) g_free (display_wayland->startup_notification_id); display_wayland->startup_notification_id = NULL; - startup_id = g_getenv ("DESKTOP_STARTUP_ID"); - if (startup_id && *startup_id != '\0') - { - if (!g_utf8_validate (startup_id, -1, NULL)) - g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8"); - else - display_wayland->startup_notification_id = g_strdup (startup_id); - - /* Clear the environment variable so it won't be inherited by - * child processes and confuse things. - */ - g_unsetenv ("DESKTOP_STARTUP_ID"); - } + startup_id = gdk_get_startup_notification_id (); + if (startup_id) + display_wayland->startup_notification_id = g_strdup (startup_id); } static gboolean diff --git a/gdk/x11/gdkdisplay-x11.c b/gdk/x11/gdkdisplay-x11.c index 4ac989d3de..eaa457dcd7 100644 --- a/gdk/x11/gdkdisplay-x11.c +++ b/gdk/x11/gdkdisplay-x11.c @@ -2153,19 +2153,9 @@ gdk_x11_display_make_default (GdkDisplay *display) g_free (display_x11->startup_notification_id); display_x11->startup_notification_id = NULL; - startup_id = g_getenv ("DESKTOP_STARTUP_ID"); - if (startup_id && *startup_id != '\0') - { - if (!g_utf8_validate (startup_id, -1, NULL)) - g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8"); - else - gdk_x11_display_set_startup_notification_id (display, startup_id); - - /* Clear the environment variable so it won't be inherited by - * child processes and confuse things. - */ - g_unsetenv ("DESKTOP_STARTUP_ID"); - } + startup_id = gdk_get_startup_notification_id (); + if (startup_id) + gdk_x11_display_set_startup_notification_id (display, startup_id); } static void