[gtk/gtk-3-24: 1/2] Centralize DESKTOP_STARTUP/AUTOSTART_ID handling



commit 2d3936cbe6bf19bc39e400e1d9660538e42f4341
Author: Jan Alexander Steffens (heftig) <jan steffens gmail com>
Date:   Wed Mar 20 20:04:51 2019 +0100

    Centralize DESKTOP_STARTUP/AUTOSTART_ID handling
    
    Add private API to GDK to move these variables from the environment into
    static scope. Also move the DESKTOP_STARTUP_ID validation here to reduce
    code duplication.
    
    Use constructors to read them as early as possible; however, do not
    unset them until first requested. This avoids breaking gnome-shell and
    gnome-settings-daemon, which want to use the DESKTOP_AUTOSTART_ID in
    their own gnome-session clients.
    
    Fixes https://gitlab.gnome.org/GNOME/gtk/issues/1761

 gdk/gdk-private.c                |  2 +
 gdk/gdk-private.h                |  6 +++
 gdk/gdk.c                        | 80 ++++++++++++++++++++++++++++++++++++++++
 gdk/wayland/gdkdisplay-wayland.c | 17 ++-------
 gdk/x11/gdkdisplay-x11.c         | 16 ++------
 gtk/gtkapplication-dbus.c        | 30 +--------------
 gtk/gtkapplication.c             | 26 +++----------
 7 files changed, 103 insertions(+), 74 deletions(-)
---
diff --git a/gdk/gdk-private.c b/gdk/gdk-private.c
index 98a0b15b0d..cf679b0c7e 100644
--- a/gdk/gdk-private.c
+++ b/gdk/gdk-private.c
@@ -17,6 +17,8 @@ gdk__private__ (void)
     gdk_display_set_rendering_mode,
     gdk_display_get_debug_updates,
     gdk_display_set_debug_updates,
+    gdk_get_desktop_startup_id,
+    gdk_get_desktop_autostart_id,
   };
 
   return &table;
diff --git a/gdk/gdk-private.h b/gdk/gdk-private.h
index 9ec724bced..e38d7b1548 100644
--- a/gdk/gdk-private.h
+++ b/gdk/gdk-private.h
@@ -31,6 +31,9 @@ gboolean         gdk_display_get_debug_updates (GdkDisplay *display);
 void             gdk_display_set_debug_updates (GdkDisplay *display,
                                                 gboolean    debug_updates);
 
+const gchar *   gdk_get_desktop_startup_id   (void);
+const gchar *   gdk_get_desktop_autostart_id (void);
+
 typedef struct {
   /* add all private functions here, initialize them in gdk-private.c */
   gboolean (* gdk_device_grab_info) (GdkDisplay  *display,
@@ -56,6 +59,9 @@ typedef struct {
   gboolean         (* gdk_display_get_debug_updates) (GdkDisplay *display);
   void             (* gdk_display_set_debug_updates) (GdkDisplay *display,
                                                       gboolean    debug_updates);
+
+  const gchar * (* gdk_get_desktop_startup_id)   (void);
+  const gchar * (* gdk_get_desktop_autostart_id) (void);
 } GdkPrivateVTable;
 
 GDK_AVAILABLE_IN_ALL
diff --git a/gdk/gdk.c b/gdk/gdk.c
index 7d8727bf17..38da23aa21 100644
--- a/gdk/gdk.c
+++ b/gdk/gdk.c
@@ -38,6 +38,8 @@
 #include "gdkkeysyms.h"
 #endif
 
+#include "gdkconstructor.h"
+
 #include <string.h>
 #include <stdlib.h>
 
@@ -1132,3 +1134,81 @@ gdk_unichar_direction (gunichar ch)
   else
     return PANGO_DIRECTION_LTR;
 }
+
+#ifdef G_HAS_CONSTRUCTORS
+#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(stash_startup_id)
+#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(stash_autostart_id)
+#endif
+G_DEFINE_CONSTRUCTOR(stash_startup_id)
+G_DEFINE_CONSTRUCTOR(stash_autostart_id)
+#endif
+
+static char *desktop_startup_id = NULL;
+static char *desktop_autostart_id = NULL;
+
+static void
+stash_startup_id (void)
+{
+  const char *startup_id = g_getenv ("DESKTOP_STARTUP_ID");
+
+  if (startup_id == NULL || startup_id[0] == '\0')
+    return;
+
+  if (!g_utf8_validate (startup_id, -1, NULL))
+    {
+      g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
+      return;
+    }
+
+  desktop_startup_id = g_strdup (startup_id);
+}
+
+static void
+stash_autostart_id (void)
+{
+  const char *autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
+  desktop_autostart_id = g_strdup (autostart_id ? autostart_id : "");
+}
+
+const gchar *
+gdk_get_desktop_startup_id (void)
+{
+  static gsize init = 0;
+
+  if (g_once_init_enter (&init))
+    {
+#ifndef G_HAS_CONSTRUCTORS
+      stash_startup_id ();
+#endif
+      /* Clear the environment variable so it won't be inherited by
+       * child processes and confuse things.
+       */
+      g_unsetenv ("DESKTOP_STARTUP_ID");
+
+      g_once_init_leave (&init, 1);
+    }
+
+  return desktop_startup_id;
+}
+
+const gchar *
+gdk_get_desktop_autostart_id (void)
+{
+  static gsize init = 0;
+
+  if (g_once_init_enter (&init))
+    {
+#ifndef G_HAS_CONSTRUCTORS
+      stash_autostart_id ();
+#endif
+      /* Clear the environment variable so it won't be inherited by
+       * child processes and confuse things.
+       */
+      g_unsetenv ("DESKTOP_AUTOSTART_ID");
+
+      g_once_init_leave (&init, 1);
+    }
+
+  return desktop_autostart_id;
+}
diff --git a/gdk/wayland/gdkdisplay-wayland.c b/gdk/wayland/gdkdisplay-wayland.c
index 762ebfb61f..d35dddf329 100644
--- a/gdk/wayland/gdkdisplay-wayland.c
+++ b/gdk/wayland/gdkdisplay-wayland.c
@@ -42,6 +42,7 @@
 #include "gdkprivate-wayland.h"
 #include "gdkglcontext-wayland.h"
 #include "gdkwaylandmonitor.h"
+#include "gdk-private.h"
 #include "pointer-gestures-unstable-v1-client-protocol.h"
 #include "tablet-unstable-v2-client-protocol.h"
 #include "xdg-shell-unstable-v6-client-protocol.h"
@@ -805,19 +806,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_desktop_startup_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 4d145a9956..5b95196a54 100644
--- a/gdk/x11/gdkdisplay-x11.c
+++ b/gdk/x11/gdkdisplay-x11.c
@@ -2229,19 +2229,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_desktop_startup_id ();
+  if (startup_id)
+    gdk_x11_display_set_startup_notification_id (display, startup_id);
 }
 
 static void
diff --git a/gtk/gtkapplication-dbus.c b/gtk/gtkapplication-dbus.c
index aee9cb09fb..dc141cfa5f 100644
--- a/gtk/gtkapplication-dbus.c
+++ b/gtk/gtkapplication-dbus.c
@@ -26,7 +26,7 @@
 #include "gtksettings.h"
 #include "gtkprivate.h"
 
-#include "gdk/gdkconstructor.h"
+#include "gdk/gdk-private.h"
 
 G_DEFINE_TYPE (GtkApplicationImplDBus, gtk_application_impl_dbus, GTK_TYPE_APPLICATION_IMPL)
 
@@ -155,29 +155,6 @@ gtk_application_get_proxy_if_service_present (GDBusConnection *connection,
   return proxy;
 }
 
-#ifdef G_HAS_CONSTRUCTORS
-#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
-#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(stash_desktop_autostart_id)
-#endif
-G_DEFINE_CONSTRUCTOR(stash_desktop_autostart_id)
-#endif
-
-static char *client_id = NULL;
-
-static void
-stash_desktop_autostart_id (void)
-{
-  const char *desktop_autostart_id;
-
-  desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
-  client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
-
-  /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
-   * use the same client id.
-   */
-  g_unsetenv ("DESKTOP_AUTOSTART_ID");
-}
-
 static void
 screensaver_signal_session (GDBusProxy     *proxy,
                             const char     *sender_name,
@@ -280,10 +257,7 @@ gtk_application_impl_dbus_startup (GtkApplicationImpl *impl,
   gboolean same_bus;
   const char *bus_name;
   const char *client_interface;
-
-#ifndef G_HAS_CONSTRUCTORS
-  stash_desktop_autostart_id ();
-#endif
+  const char *client_id = GDK_PRIVATE_CALL (gdk_get_desktop_autostart_id) ();
 
   dbus->session = g_application_get_dbus_connection (G_APPLICATION (impl->application));
 
diff --git a/gtk/gtkapplication.c b/gtk/gtkapplication.c
index 3bb51d499a..7b45c06145 100644
--- a/gtk/gtkapplication.c
+++ b/gtk/gtkapplication.c
@@ -28,7 +28,7 @@
 #include <unistd.h>
 #endif
 
-#include "gdk/gdkconstructor.h"
+#include "gdk/gdk-private.h"
 
 #include "gtkapplicationprivate.h"
 #include "gtkclipboardprivate.h"
@@ -169,21 +169,6 @@ struct _GtkApplicationPrivate
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
 
-#ifdef G_HAS_CONSTRUCTORS
-#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
-#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(stash_desktop_startup_id)
-#endif
-G_DEFINE_CONSTRUCTOR(stash_desktop_startup_id)
-#endif
-
-static const char *desktop_startup_id = NULL;
-
-static void
-stash_desktop_startup_id (void)
-{
-  desktop_startup_id = g_getenv ("DESKTOP_STARTUP_ID");
-}
-
 static gboolean
 gtk_application_focus_in_event_cb (GtkWindow      *window,
                                    GdkEventFocus  *event,
@@ -363,7 +348,9 @@ gtk_application_add_platform_data (GApplication    *application,
    *
    * So we do all the things... which currently is just one thing.
    */
-  if (desktop_startup_id && g_utf8_validate (desktop_startup_id, -1, NULL))
+  const gchar *desktop_startup_id =
+    GDK_PRIVATE_CALL (gdk_get_desktop_startup_id) ();
+  if (desktop_startup_id)
     g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
                            g_variant_new_string (desktop_startup_id));
 }
@@ -395,9 +382,8 @@ gtk_application_init (GtkApplication *application)
 
   application->priv->accels = gtk_application_accels_new ();
 
-#ifndef G_HAS_CONSTRUCTORS
-  stash_desktop_startup_id ();
-#endif
+  /* getenv now at the latest */
+  GDK_PRIVATE_CALL (gdk_get_desktop_startup_id) ();
 }
 
 static void


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