diff --git a/docs/changelog.rst b/docs/changelog.rst index 9debf3347..a659b698a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -236,6 +236,10 @@ Detailed list of changes - IME: Move cursor position within pre-edit text (:iss:`10807`) +- Wayland: Fix kitty hanging when creating a new OS window if an output was + plugged and unplugged quickly and the compositor never sent its logical + geometry + 0.50.0 [2026-11-03] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/glfw/wl_init.c b/glfw/wl_init.c index 8eb4d9102..30f782886 100644 --- a/glfw/wl_init.c +++ b/glfw/wl_init.c @@ -861,7 +861,7 @@ registryHandleGlobal(void *data UNUSED, struct wl_registry *registry, uint32_t n _glfw.wl.xdg_toplevel_drag_manager_v1 = wl_registry_bind(registry, name, &xdg_toplevel_drag_manager_v1_interface, 1); } else if (is(zxdg_output_manager_v1)) { _glfw.wl.xdg_output_manager = wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, MIN(version, 3u)); - for (int i = 0; i < _glfw.monitorCount; i++) _glfwCreateXdgOutputWayland(_glfw.monitors[i]); + _glfwCreateXdgOutputsWayland(); } else if (is(zwlr_virtual_pointer_manager_v1)) { _glfw.wl.virtual_pointer_manager = wl_registry_bind(registry, name, &zwlr_virtual_pointer_manager_v1_interface, 1); } else if (is(zwp_virtual_keyboard_manager_v1)) { @@ -872,25 +872,7 @@ registryHandleGlobal(void *data UNUSED, struct wl_registry *registry, uint32_t n static void registryHandleGlobalRemove(void *data UNUSED, struct wl_registry *registry UNUSED, uint32_t name) { - _GLFWmonitor *monitor; - - for (int i = 0; i < _glfw.monitorCount; ++i) { - monitor = _glfw.monitors[i]; - if (monitor->wl.name == name) { - for (_GLFWwindow *window = _glfw.windowListHead; window; window = window->next) { - bool was_on_monitor = false; - for (int m = window->wl.monitorsCount - 1; m >= 0; m--) { - if (window->wl.monitors[m] == monitor) { - remove_i_from_array(window->wl.monitors, m, window->wl.monitorsCount); - was_on_monitor = true; - } - } - if (was_on_monitor) _glfwWaylandSurfaceOutputsChanged(window); - } - _glfwInputMonitor(monitor, GLFW_DISCONNECTED, 0); - return; - } - } + _glfwRemoveOutputWayland(name); } @@ -1100,6 +1082,7 @@ _glfwPlatformTerminate(void) { } free(_glfw.wl.activation_requests.array); } + _glfwFreePendingOutputsWayland(); glfw_xkb_release(&_glfw.wl.xkb); glfw_dbus_terminate(&_glfw.wl.dbus); diff --git a/glfw/wl_monitor.c b/glfw/wl_monitor.c index 90c131732..6e9c32b57 100644 --- a/glfw/wl_monitor.c +++ b/glfw/wl_monitor.c @@ -73,13 +73,23 @@ outputHandleMode(void *data, struct wl_output *output UNUSED, uint32_t flags, in if (flags & WL_OUTPUT_MODE_CURRENT) monitor->wl.currentMode = monitor->modeCount - 1; } +static bool +removePendingMonitor(_GLFWmonitor *monitor) { + for (int i = 0; i < _glfw.wl.pending_monitors.count; i++) { + if (_glfw.wl.pending_monitors.array[i] == monitor) { + remove_i_from_array(_glfw.wl.pending_monitors.array, i, _glfw.wl.pending_monitors.count); + return true; + } + } + return false; +} + static void outputHandleDone(void *data, struct wl_output *output UNUSED) { struct _GLFWmonitor *monitor = data; - for (int i = 0; i < _glfw.monitorCount; i++) { - if (_glfw.monitors[i] == monitor) return; - } - _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_LAST); + // Only monitors still pending need to be announced, done is re-sent + // whenever output properties change. + if (removePendingMonitor(monitor)) _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_LAST); } static void @@ -217,10 +227,69 @@ _glfwAddOutputWayland(uint32_t name, uint32_t version) { monitor->wl.output = output; monitor->wl.name = name; + _GLFWmonitor **pending = realloc(_glfw.wl.pending_monitors.array, sizeof(pending[0]) * ((size_t)_glfw.wl.pending_monitors.count + 1)); + if (!pending) { + _glfwInputError(GLFW_OUT_OF_MEMORY, "Wayland: Out of memory while adding output"); + _glfwFreeMonitor(monitor); + return; + } + _glfw.wl.pending_monitors.array = pending; + _glfw.wl.pending_monitors.array[_glfw.wl.pending_monitors.count++] = monitor; + wl_output_add_listener(output, &outputListener, monitor); _glfwCreateXdgOutputWayland(monitor); } +// Handle removal of the wl_output global with the specified registry name. +// Returns true if an output was found and removed. +bool +_glfwRemoveOutputWayland(uint32_t name) { + // The global may be removed before wl_output.done is received, for + // example when an output is unplugged right after being plugged in. Such + // monitors were never announced, so just free them, otherwise they would + // be announced on a later done event despite being dead. + for (int i = 0; i < _glfw.wl.pending_monitors.count; i++) { + _GLFWmonitor *monitor = _glfw.wl.pending_monitors.array[i]; + if (monitor->wl.name == name) { + remove_i_from_array(_glfw.wl.pending_monitors.array, i, _glfw.wl.pending_monitors.count); + _glfwFreeMonitor(monitor); + return true; + } + } + for (int i = 0; i < _glfw.monitorCount; ++i) { + _GLFWmonitor *monitor = _glfw.monitors[i]; + if (monitor->wl.name == name) { + for (_GLFWwindow *window = _glfw.windowListHead; window; window = window->next) { + bool was_on_monitor = false; + for (int m = window->wl.monitorsCount - 1; m >= 0; m--) { + if (window->wl.monitors[m] == monitor) { + remove_i_from_array(window->wl.monitors, m, window->wl.monitorsCount); + was_on_monitor = true; + } + } + if (was_on_monitor) _glfwWaylandSurfaceOutputsChanged(window); + } + _glfwInputMonitor(monitor, GLFW_DISCONNECTED, 0); + return true; + } + } + return false; +} + +void +_glfwFreePendingOutputsWayland(void) { + for (int i = 0; i < _glfw.wl.pending_monitors.count; i++) _glfwFreeMonitor(_glfw.wl.pending_monitors.array[i]); + free(_glfw.wl.pending_monitors.array); + _glfw.wl.pending_monitors.array = NULL; + _glfw.wl.pending_monitors.count = 0; +} + +void +_glfwCreateXdgOutputsWayland(void) { + for (int i = 0; i < _glfw.monitorCount; i++) _glfwCreateXdgOutputWayland(_glfw.monitors[i]); + for (int i = 0; i < _glfw.wl.pending_monitors.count; i++) _glfwCreateXdgOutputWayland(_glfw.wl.pending_monitors.array[i]); +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// @@ -293,6 +362,53 @@ glfwGetWaylandMonitor(GLFWmonitor *handle) { return monitor->wl.output; } +static _GLFWmonitor * +monitorForRegistryName(uint32_t name) { + for (int i = 0; i < _glfw.monitorCount; i++) { + if (_glfw.monitors[i]->wl.name == name) return _glfw.monitors[i]; + } + return NULL; +} + +static bool +needsXdgGeometry(const _GLFWmonitor *m) { + return m->wl.xdg_output && !m->wl.xdg_geometry_wait_abandoned && !(m->wl.xdg_size_received && m->wl.xdg_position_received); +} + +// The compositor sends xdg_output logical geometry in response to +// get_xdg_output, so it is guaranteed to have arrived after a roundtrip, +// unless the compositor never sends it at all, for instance for an +// xdg_output created on an output that has just been removed. Use a few +// extra roundtrips for robustness, but never wait indefinitely. +#define MAX_XDG_GEOMETRY_ROUNDTRIPS 4 + +// Wait for xdg_output geometry for all monitors, or just the monitor with the +// specified registry name if only_name is non-zero. Monitors can be freed by +// events dispatched during a roundtrip, so they are looked up afresh after +// every roundtrip rather than holding pointers across them. +static void +waitForXdgGeometry(uint32_t only_name) { + for (int roundtrips = 0;; roundtrips++) { + bool waiting = false; + for (int i = 0; i < _glfw.monitorCount && !waiting; i++) { + _GLFWmonitor *m = _glfw.monitors[i]; + if ((!only_name || m->wl.name == only_name) && needsXdgGeometry(m)) waiting = true; + } + if (!waiting) return; + if (roundtrips >= MAX_XDG_GEOMETRY_ROUNDTRIPS || wl_display_roundtrip(_glfw.wl.display) < 0) break; + } + for (int i = 0; i < _glfw.monitorCount; i++) { + _GLFWmonitor *m = _glfw.monitors[i]; + if ((!only_name || m->wl.name == only_name) && needsXdgGeometry(m)) { + m->wl.xdg_geometry_wait_abandoned = true; + _glfwInputError( + GLFW_PLATFORM_ERROR, + "Wayland: compositor did not send xdg_output logical geometry for output %s, ignoring it", + m->name ? m->name : "(unnamed)"); + } + } +} + GLFWAPI double glfwGetWaylandCurrentMonitorFractionalScale(void) { _GLFW_REQUIRE_INIT_OR_RETURN(1.0); @@ -318,12 +434,7 @@ glfwGetWaylandCurrentMonitorFractionalScale(void) { // it does, so wait for every monitor's logical geometry before trusting // the reported positions (otherwise an as-yet-unpositioned output would // spuriously match (0, 0)). - for (int i = 0; i < _glfw.monitorCount; i++) { - _GLFWmonitor *m = _glfw.monitors[i]; - while (m->wl.xdg_output && !(m->wl.xdg_size_received && m->wl.xdg_position_received)) { - if (wl_display_roundtrip(_glfw.wl.display) < 0) break; - } - } + waitForXdgGeometry(0); for (int i = 0; i < _glfw.monitorCount; i++) { _GLFWmonitor *m = _glfw.monitors[i]; if (m->wl.xdg_output && m->wl.xdg_size_received && m->wl.xdg_position_received && m->wl.x == 0 && m->wl.y == 0) { @@ -331,15 +442,22 @@ glfwGetWaylandCurrentMonitorFractionalScale(void) { break; } } - if (!monitor) monitor = _glfw.monitors[0]; + if (!monitor) { + // Monitors may have been removed during the roundtrips + if (_glfw.monitorCount == 0) return 1.0; + monitor = _glfw.monitors[0]; + } } if (!monitor->wl.xdg_output) return monitor->wl.scale > 0 ? (double)monitor->wl.scale : 1.0; - // Block until the compositor has delivered xdg_output logical size and position. - while (!(monitor->wl.xdg_size_received && monitor->wl.xdg_position_received)) { - if (wl_display_roundtrip(_glfw.wl.display) < 0) break; - } + // Wait for the compositor to deliver xdg_output logical size and position. + // The monitor may be removed while waiting, so look it up again afterwards. + const uint32_t name = monitor->wl.name; + const int fallback_scale = monitor->wl.scale > 0 ? monitor->wl.scale : 1; + waitForXdgGeometry(name); + monitor = monitorForRegistryName(name); + if (!monitor) return (double)fallback_scale; if (monitor->wl.fractional_scale <= 0.0) computeXdgFractionalScale(monitor); diff --git a/glfw/wl_platform.h b/glfw/wl_platform.h index 5a53746dc..a6597224b 100644 --- a/glfw/wl_platform.h +++ b/glfw/wl_platform.h @@ -397,6 +397,13 @@ typedef struct _GLFWlibraryWayland { struct zwp_pointer_gestures_v1 *pointer_gestures; struct zwp_pointer_gesture_hold_v1 *pointer_gesture_hold; struct zxdg_output_manager_v1 *xdg_output_manager; + // Outputs that have been bound but not yet announced to GLFW via + // wl_output.done. They are not in _glfw.monitors yet but must still be + // freed if their global is removed before the done event arrives. + struct { + _GLFWmonitor **array; + int count; + } pending_monitors; struct zwlr_virtual_pointer_manager_v1 *virtual_pointer_manager; struct zwlr_virtual_pointer_v1 *virtual_pointer; struct zwp_virtual_keyboard_manager_v1 *virtual_keyboard_manager; @@ -519,6 +526,10 @@ typedef struct _GLFWmonitorWayland { double fractional_scale; bool xdg_size_received; bool xdg_position_received; + // Set once we have given up waiting for xdg_output geometry for this + // monitor, so that we dont block repeatedly on a compositor that never + // sends it. + bool xdg_geometry_wait_abandoned; } _GLFWmonitorWayland; @@ -539,6 +550,9 @@ typedef struct _GLFWcursorWayland { void _glfwAddOutputWayland(uint32_t name, uint32_t version); void _glfwCreateXdgOutputWayland(_GLFWmonitor *monitor); +bool _glfwRemoveOutputWayland(uint32_t name); +void _glfwFreePendingOutputsWayland(void); +void _glfwCreateXdgOutputsWayland(void); void _glfwWaylandBeforeBufferSwap(_GLFWwindow *window); void _glfwWaylandAfterBufferSwap(_GLFWwindow *window); void _glfwWaylandSurfaceOutputsChanged(_GLFWwindow *window);