From e5908f575298bdb7aa0999a9dd5b76eb1549f226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Tue, 31 May 2022 12:50:48 +0200 Subject: [PATCH] startup-notification/x11: Let the libsn user handle API annoyances The API has no concept of user data, and requires the user to some how get an instance without context, i.e. via static globals. Limit this to the file where this is needed. Part-of: --- src/core/display-private.h | 2 -- src/core/display.c | 25 ------------- src/x11/meta-startup-notification-x11.c | 47 +++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/core/display-private.h b/src/core/display-private.h index 575744476..f82fb8ba2 100644 --- a/src/core/display-private.h +++ b/src/core/display-private.h @@ -298,8 +298,6 @@ META_EXPORT_TEST GSList* meta_display_list_windows (MetaDisplay *display, MetaListWindowsFlags flags); -MetaDisplay* meta_display_for_x_display (Display *xdisplay); - META_EXPORT_TEST MetaDisplay* meta_get_display (void); diff --git a/src/core/display.c b/src/core/display.c index c5a353369..10e273237 100644 --- a/src/core/display.c +++ b/src/core/display.c @@ -1205,31 +1205,6 @@ meta_display_close (MetaDisplay *display, the_display = NULL; } -/** - * meta_display_for_x_display: - * @xdisplay: An X display - * - * Returns the singleton MetaDisplay if @xdisplay matches the X display it's - * managing; otherwise gives a warning and returns %NULL. When we were claiming - * to be able to manage multiple displays, this was supposed to find the - * display out of the list which matched that display. Now it's merely an - * extra sanity check. - * - * Returns: The singleton X display, or %NULL if @xdisplay isn't the one - * we're managing. - */ -MetaDisplay* -meta_display_for_x_display (Display *xdisplay) -{ - if (the_display->x11_display->xdisplay == xdisplay) - return the_display; - - meta_warning ("Could not find display for X display %p, probably going to crash", - xdisplay); - - return NULL; -} - /** * meta_get_display: * diff --git a/src/x11/meta-startup-notification-x11.c b/src/x11/meta-startup-notification-x11.c index 27ad8bd0a..d92639bea 100644 --- a/src/x11/meta-startup-notification-x11.c +++ b/src/x11/meta-startup-notification-x11.c @@ -51,6 +51,8 @@ struct _MetaX11StartupNotification static GParamSpec *seq_x11_props[N_SEQ_X11_PROPS]; +static GList *displays; + G_DEFINE_TYPE (MetaStartupSequenceX11, meta_startup_sequence_x11, META_TYPE_STARTUP_SEQUENCE) @@ -168,13 +170,36 @@ meta_startup_sequence_x11_new (MetaDisplay *display, NULL); } +static MetaDisplay * +find_display (Display *xdisplay) +{ + GList *l; + + for (l = displays; l; l = l->next) + { + MetaDisplay *display = l->data; + MetaX11Display *x11_display; + + x11_display = display->x11_display; + if (!x11_display) + continue; + + if (x11_display->xdisplay != xdisplay) + continue; + + return display; + } + + return NULL; +} + static void sn_error_trap_push (SnDisplay *sn_display, Display *xdisplay) { MetaDisplay *display; - display = meta_display_for_x_display (xdisplay); + display = find_display (xdisplay); if (display != NULL) meta_x11_error_trap_push (display->x11_display); } @@ -185,7 +210,7 @@ sn_error_trap_pop (SnDisplay *sn_display, { MetaDisplay *display; - display = meta_display_for_x_display (xdisplay); + display = find_display (xdisplay); if (display != NULL) meta_x11_error_trap_pop (display->x11_display); } @@ -253,13 +278,21 @@ meta_startup_notification_sn_event (SnMonitorEvent *event, sn_startup_sequence_unref (sequence); } -#endif + +static void +on_x11_display_closing (MetaDisplay *display) +{ + g_signal_handlers_disconnect_by_func (display, on_x11_display_closing, NULL); + displays = g_list_remove (displays, display); +} +#endif /* HAVE_STARTUP_NOTIFICATION */ void meta_x11_startup_notification_init (MetaX11Display *x11_display) { #ifdef HAVE_STARTUP_NOTIFICATION MetaX11StartupNotification *x11_sn; + MetaDisplay *display; x11_sn = g_new0 (MetaX11StartupNotification, 1); x11_sn->sn_display = sn_display_new (x11_display->xdisplay, @@ -273,6 +306,14 @@ meta_x11_startup_notification_init (MetaX11Display *x11_display) NULL); x11_display->startup_notification = x11_sn; + + display = meta_x11_display_get_display (x11_display); + if (!g_list_find (displays, display)) + { + displays = g_list_prepend (displays, display); + g_signal_connect (display, "x11-display-closing", + G_CALLBACK (on_x11_display_closing), NULL); + } #endif }