Skip to content

Commit

Permalink
Function to probe for Wayland compositor name
Browse files Browse the repository at this point in the history
Maybe needed to workaround #8236
  • Loading branch information
kovidgoyal committed Jan 21, 2025
1 parent 5335d18 commit 7346aca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 additions & 0 deletions glfw/wl_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 //////
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions glfw/wl_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7346aca

Please sign in to comment.