From 9ad08adcccb6617ac35e1d7fde20e1da9f9d3843 Mon Sep 17 00:00:00 2001 From: Richard Young Date: Mon, 13 Jul 2026 18:23:22 -0400 Subject: [PATCH] Fix GTK keyboard event crash before initialization GTK can deliver keyboard events during startup or focus changes before the keyboard state is initialized. The event handler previously dereferenced the missing state, which could cause a null pointer crash and trigger Gdk-CRITICAL from gdk_keymap_get_direction: assertion 'GDK_IS_KEYMAP (keymap)' failed. Validate the keyboard state before processing events and safely ignore events received before initialization completes. Normal behavior is unchanged once the backend is ready. --- src/platform/linux/window_manager_linux.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/platform/linux/window_manager_linux.cpp b/src/platform/linux/window_manager_linux.cpp index 93cbb37..4ef0482 100644 --- a/src/platform/linux/window_manager_linux.cpp +++ b/src/platform/linux/window_manager_linux.cpp @@ -325,24 +325,11 @@ std::shared_ptr WindowManager::GetCurrent() { return nullptr; } - // Try to get the focused window - GdkSeat* seat = gdk_display_get_default_seat(display); - if (seat) { - GdkDevice* keyboard = gdk_seat_get_keyboard(seat); - if (keyboard) { - GdkWindow* focused_window = gdk_device_get_window_at_position(keyboard, nullptr, nullptr); - if (focused_window) { - WindowId window_id = GetOrCreateWindowId(focused_window); - return Get(window_id); - } - } - } - - // Fallback: get the first visible window + // GTK tracks the active toplevel, which is the window receiving keystrokes. GList* toplevels = gtk_window_list_toplevels(); for (GList* l = toplevels; l != nullptr; l = l->next) { GtkWindow* gtk_window = GTK_WINDOW(l->data); - if (gtk_widget_get_visible(GTK_WIDGET(gtk_window))) { + if (gtk_window_is_active(gtk_window)) { GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(gtk_window)); if (gdk_window) { WindowId window_id = GetOrCreateWindowId(gdk_window);