Function to probe for Wayland compositor name

Maybe needed to workaround #8236
This commit is contained in:
Kovid Goyal 2025-01-21 15:10:07 +05:30
parent 5335d183c1
commit 7346aca56d
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 35 additions and 0 deletions

33
glfw/wl_init.c vendored
View file

@ -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

2
glfw/wl_platform.h vendored
View file

@ -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;