From 7346aca56d6ec7a9a1aa6f83fd0c3e0e2e0d3f33 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 21 Jan 2025 15:10:07 +0530 Subject: [PATCH] Function to probe for Wayland compositor name Maybe needed to workaround #8236 --- glfw/wl_init.c | 33 +++++++++++++++++++++++++++++++++ glfw/wl_platform.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/glfw/wl_init.c b/glfw/wl_init.c index 4a0202aa40c..584bab18470 100644 --- a/glfw/wl_init.c +++ b/glfw/wl_init.c @@ -668,6 +668,35 @@ GLFWAPI pid_t glfwWaylandCompositorPID(void) { return get_socket_peer_pid(fd); } +const char* +_glfwWaylandCompositorName(void) { + static bool probed = false; + if (!probed) { + probed = true; + static const size_t sz = 1024; + _glfw.wl.compositor_name = malloc(sz); + if (!_glfw.wl.compositor_name) return ""; + char *ans = _glfw.wl.compositor_name; ans[0] = 0; + pid_t cpid = glfwWaylandCompositorPID(); + if (cpid < 0) return ans; + snprintf(ans, sz, "/proc/%d/cmdline", cpid); + int fd = open(ans, O_RDONLY | O_CLOEXEC); + if (fd < 0) { + ans[0] = 0; + } else { + ssize_t n; + while (true) { + n = read(fd, ans, sz-1); + if (n < 0 && errno == EINTR) continue; + close(fd); break; + } + ans[n < 0 ? 0 : n] = 0; + } + } + return _glfw.wl.compositor_name ? _glfw.wl.compositor_name : ""; +} + + ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -876,6 +905,10 @@ void _glfwPlatformTerminate(void) wl_display_disconnect(_glfw.wl.display); } finalizePollData(&_glfw.wl.eventLoopData); + if (_glfw.wl.compositor_name) { + free(_glfw.wl.compositor_name); + _glfw.wl.compositor_name = NULL; + } } #define GLFW_LOOP_BACKEND wl diff --git a/glfw/wl_platform.h b/glfw/wl_platform.h index d54e58ad00c..1011ba91552 100644 --- a/glfw/wl_platform.h +++ b/glfw/wl_platform.h @@ -389,6 +389,7 @@ typedef struct _GLFWlibraryWayland size_t dataOffersCounter; _GLFWWaylandDataOffer dataOffers[8]; bool has_preferred_buffer_scale; + char *compositor_name; } _GLFWlibraryWayland; // Wayland-specific per-monitor data @@ -432,6 +433,7 @@ int _glfwWaylandIntegerWindowScale(_GLFWwindow*); void animateCursorImage(id_type timer_id, void *data); struct wl_cursor* _glfwLoadCursor(GLFWCursorShape, struct wl_cursor_theme*); void destroy_data_offer(_GLFWWaylandDataOffer*); +const char* _glfwWaylandCompositorName(void); typedef struct wayland_cursor_shape { int which; const char *name;