Skip to content

Commit

Permalink
Centralize DESKTOP_STARTUP/AUTOSTART_ID handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
heftig committed Apr 2, 2019
1 parent 892abdd commit 2d3936c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 74 deletions.
2 changes: 2 additions & 0 deletions gdk/gdk-private.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions gdk/gdk-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
80 changes: 80 additions & 0 deletions gdk/gdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "gdkkeysyms.h"
#endif

#include "gdkconstructor.h"

#include <string.h>
#include <stdlib.h>

Expand Down Expand Up @@ -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;
}
17 changes: 4 additions & 13 deletions gdk/wayland/gdkdisplay-wayland.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
16 changes: 3 additions & 13 deletions gdk/x11/gdkdisplay-x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 2 additions & 28 deletions gtk/gtkapplication-dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));

Expand Down
26 changes: 6 additions & 20 deletions gtk/gtkapplication.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <unistd.h>
#endif

#include "gdk/gdkconstructor.h"
#include "gdk/gdk-private.h"

#include "gtkapplicationprivate.h"
#include "gtkclipboardprivate.h"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2d3936c

Please sign in to comment.