From 71be34a6c30825aef502b652c86cc31fdcbb5457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Fri, 15 Jul 2022 13:12:58 +0200 Subject: [PATCH] Wayland: Fix crash if mouse connected after init The cursor theme was only loaded if the chosen seat had a mouse (wl_pointer) during initialization. If a mouse was connected only after glfwInit, there would be no cursor theme but the rest of the cursor related code assumed one had already been loaded. This also moves the details of cursor theme loading out into a separate function to declutter platform init. Because the original cursor theme loading code checked whether we got a wl_shm, and because the rest of the code just assumes we have a wl_shm, initialization will now fail if there isn't one. Fixes #1450 --- CONTRIBUTORS.md | 1 + README.md | 1 + src/wl_init.c | 73 ++++++++++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 153c38d2..673db319 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -187,6 +187,7 @@ video tutorials. - Philip Rideout - Eddie Ringle - Max Risuhin + - Joe Roback - Jorge Rodriguez - Jari Ronkainen - Luca Rood diff --git a/README.md b/README.md index b0405480..5b56b3eb 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,7 @@ information on what to include when reporting a bug. wlroots compositors (#1268) - [Wayland] Bugfix: `GLFW_DECORATED` was ignored when showing a window with XDG decorations + - [Wayland] Bugfix: Connecting a mouse after `glfwInit` would segfault (#1450) - [POSIX] Removed use of deprecated function `gettimeofday` - [POSIX] Bugfix: `CLOCK_MONOTONIC` was not correctly tested for or enabled - [Linux] Bugfix: Joysticks without buttons were ignored (#2042,#2043) diff --git a/src/wl_init.c b/src/wl_init.c index 06928a04..3883b405 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -335,6 +335,41 @@ static void createKeyTables(void) } } +static GLFWbool loadCursorTheme(void) +{ + const char* cursorTheme; + const char* cursorSizeStr; + char* cursorSizeEnd; + long cursorSizeLong; + int cursorSize; + + cursorTheme = getenv("XCURSOR_THEME"); + cursorSizeStr = getenv("XCURSOR_SIZE"); + cursorSize = 32; + if (cursorSizeStr) + { + errno = 0; + cursorSizeLong = strtol(cursorSizeStr, &cursorSizeEnd, 10); + if (!*cursorSizeEnd && !errno && cursorSizeLong > 0 && cursorSizeLong <= INT_MAX) + cursorSize = (int)cursorSizeLong; + } + _glfw.wl.cursorTheme = + wl_cursor_theme_load(cursorTheme, cursorSize, _glfw.wl.shm); + if (!_glfw.wl.cursorTheme) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "Wayland: Failed to load default cursor theme"); + return GLFW_FALSE; + } + // If this happens to be NULL, we just fallback to the scale=1 version. + _glfw.wl.cursorThemeHiDPI = + wl_cursor_theme_load(cursorTheme, 2 * cursorSize, _glfw.wl.shm); + _glfw.wl.cursorSurface = + wl_compositor_create_surface(_glfw.wl.compositor); + _glfw.wl.cursorTimerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); + return GLFW_TRUE; +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// @@ -471,12 +506,6 @@ GLFWbool _glfwConnectWayland(int platformID, _GLFWplatform* platform) int _glfwInitWayland(void) { - const char* cursorTheme; - const char* cursorSizeStr; - char* cursorSizeEnd; - long cursorSizeLong; - int cursorSize; - // These must be set before any failure checks _glfw.wl.keyRepeatTimerfd = -1; _glfw.wl.cursorTimerfd = -1; @@ -652,34 +681,16 @@ int _glfwInitWayland(void) return GLFW_FALSE; } - if (_glfw.wl.pointer && _glfw.wl.shm) + if (!_glfw.wl.shm) { - cursorTheme = getenv("XCURSOR_THEME"); - cursorSizeStr = getenv("XCURSOR_SIZE"); - cursorSize = 32; - if (cursorSizeStr) - { - errno = 0; - cursorSizeLong = strtol(cursorSizeStr, &cursorSizeEnd, 10); - if (!*cursorSizeEnd && !errno && cursorSizeLong > 0 && cursorSizeLong <= INT_MAX) - cursorSize = (int)cursorSizeLong; - } - _glfw.wl.cursorTheme = - wl_cursor_theme_load(cursorTheme, cursorSize, _glfw.wl.shm); - if (!_glfw.wl.cursorTheme) - { - _glfwInputError(GLFW_PLATFORM_ERROR, - "Wayland: Failed to load default cursor theme"); - return GLFW_FALSE; - } - // If this happens to be NULL, we just fallback to the scale=1 version. - _glfw.wl.cursorThemeHiDPI = - wl_cursor_theme_load(cursorTheme, 2 * cursorSize, _glfw.wl.shm); - _glfw.wl.cursorSurface = - wl_compositor_create_surface(_glfw.wl.compositor); - _glfw.wl.cursorTimerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); + _glfwInputError(GLFW_PLATFORM_ERROR, + "Wayland: Failed to find wl_shm in your compositor"); + return GLFW_FALSE; } + if (!loadCursorTheme()) + return GLFW_FALSE; + if (_glfw.wl.seat && _glfw.wl.dataDeviceManager) { _glfw.wl.dataDevice =