From 27d991f046bdebb12bfd58f7c05a19f135979c29 Mon Sep 17 00:00:00 2001
From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
Date: Fri, 14 Feb 2025 17:12:39 +0100
Subject: [PATCH] Update bundled libdecor to last upstream version (21 jan
 2025)

---
 documentation/src/bundled-libs.dox      |  4 +--
 libdecor/src/libdecor.c                 | 46 ++++++++++++++-----------
 libdecor/src/plugins/gtk/libdecor-gtk.c |  6 ++++
 3 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/documentation/src/bundled-libs.dox b/documentation/src/bundled-libs.dox
index 18237b9ea..8ec6d02eb 100644
--- a/documentation/src/bundled-libs.dox
+++ b/documentation/src/bundled-libs.dox
@@ -23,14 +23,14 @@ The nanosvg library is not affected.
 
 \section bundled-status Current status
 \code
-Current versions of bundled libraries (as of December 5, 2024):
+Current versions of bundled libraries (as of February 14, 2025):
   Library       Version/git commit Release date         FLTK Version
   --------------------------------------------------------------------------
   jpeg          jpeg-9f            2024-01-14           1.4.0
   nanosvg       7aeda550a8 [1]     2023-12-02           1.4.0
   png           libpng-1.6.44      2024-09-12           1.4.1
   zlib          zlib-1.3.1         2024-01-22           1.4.0
-  libdecor      c2bd8ad6   [2]     2024-05-31           1.4.0
+  libdecor      f7cd7ffd   [2]     2025-01-21           1.4.2
   --------------------------------------------------------------------------
 
 Previous versions of bundled libraries (FLTK 1.3.x):
diff --git a/libdecor/src/libdecor.c b/libdecor/src/libdecor.c
index 3ede71280..75033afec 100644
--- a/libdecor/src/libdecor.c
+++ b/libdecor/src/libdecor.c
@@ -124,6 +124,7 @@ struct libdecor_frame_private {
 
 	enum libdecor_window_state window_state;
 
+	bool has_decoration_mode;
 	enum zxdg_toplevel_decoration_v1_mode decoration_mode;
 
 	enum libdecor_capabilities capabilities;
@@ -504,7 +505,13 @@ toplevel_decoration_configure(
 		struct zxdg_toplevel_decoration_v1 *zxdg_toplevel_decoration_v1,
 		uint32_t mode)
 {
-	((struct libdecor_frame_private *)(data))->decoration_mode = mode;
+	struct libdecor_frame_private *frame_priv = (struct libdecor_frame_private *)data;
+	/* Ignore any _configure calls after the first, they will be
+	 * from our set_mode call. */
+	if (!frame_priv->has_decoration_mode) {
+		frame_priv->has_decoration_mode = true;
+		frame_priv->decoration_mode = mode;
+	}
 }
 
 static const struct zxdg_toplevel_decoration_v1_listener
@@ -640,9 +647,9 @@ libdecor_frame_unref(struct libdecor_frame *frame)
 		struct libdecor_plugin *plugin = context->plugin;
 
 		if (context->decoration_manager && frame_priv->toplevel_decoration) {
-        		zxdg_toplevel_decoration_v1_destroy(frame_priv->toplevel_decoration);
-        		frame_priv->toplevel_decoration = NULL;
-        	}
+			zxdg_toplevel_decoration_v1_destroy(frame_priv->toplevel_decoration);
+			frame_priv->toplevel_decoration = NULL;
+		}
 
 		wl_list_remove(&frame->link);
 
@@ -684,24 +691,21 @@ libdecor_frame_set_visibility(struct libdecor_frame *frame,
 
 	frame_priv->visible = visible;
 
-	/* enable/disable decorations that are managed by the compositor,
-	 * only xdg-decoration version 2 and above allows to toggle decoration */
+	/* enable/disable decorations that are managed by the compositor.
+	 * Note that, as of xdg_decoration v1, this is just a hint and there is
+	 * no reliable way of disabling all decorations. In practice this should
+	 * work but per spec this is not guaranteed.
+	 *
+	 * See also: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/17
+	 */
 	if (context->decoration_manager &&
-	    zxdg_decoration_manager_v1_get_version(context->decoration_manager) > 1) {
-		if (frame_priv->visible &&
-		    frame_priv->toplevel_decoration == NULL) {
-			/* - request to SHOW decorations
-			 * - decorations are NOT HANDLED
-			 * => create new decorations for already mapped surface */
-			libdecor_frame_create_xdg_decoration(frame_priv);
-		} else if (!frame_priv->visible &&
-			 frame_priv->toplevel_decoration != NULL) {
-			/* - request to HIDE decorations
-			 * - decorations are HANDLED
-			 * => destroy decorations */
-			zxdg_toplevel_decoration_v1_destroy(frame_priv->toplevel_decoration);
-			frame_priv->toplevel_decoration = NULL;
-		}
+	    frame_priv->toplevel_decoration &&
+	    frame_priv->has_decoration_mode &&
+	    frame_priv->decoration_mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE) {
+		zxdg_toplevel_decoration_v1_set_mode(frame_priv->toplevel_decoration,
+						     frame->priv->visible
+						     ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE
+						     : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);
 	}
 
 	/* enable/disable decorations that are managed by a plugin */
diff --git a/libdecor/src/plugins/gtk/libdecor-gtk.c b/libdecor/src/plugins/gtk/libdecor-gtk.c
index 7ce38ddc6..813b764e3 100644
--- a/libdecor/src/plugins/gtk/libdecor-gtk.c
+++ b/libdecor/src/plugins/gtk/libdecor-gtk.c
@@ -2915,6 +2915,12 @@ libdecor_plugin_new(struct libdecor *context)
 	struct libdecor_plugin_gtk *plugin_gtk;
 	struct wl_display *wl_display;
 
+#ifdef HAVE_GETTID
+	/* Only support running on the main thread. */
+	if (getpid () != gettid ())
+		return NULL;
+#endif
+
 	plugin_gtk = zalloc(sizeof *plugin_gtk);
 	libdecor_plugin_init(&plugin_gtk->plugin,
 			     context,
-- 
GitLab