From f9a417afd05c066d6021350dc6508cdb1c950e1c Mon Sep 17 00:00:00 2001 From: wr5537 Date: Tue, 24 Nov 2026 10:27:36 +0800 Subject: [PATCH] fix: restore keyboard event mask for X11 popup windows The dxcb platform plugin creates override-redirect popup windows with an event mask that only selects property/visibility/substructure notification. Keyboard events are delivered per-client based on the event mask each client selected, so without KeyPressMask/KeyReleaseMask/FocusChangeMask no key event ever reaches Qt - while mouse events (delivered through XInput2) keep working. This silently breaks text input in popup windows of the process, e.g. the launcher search box. Install a native event filter on QCoreApplication startup which restores the keyboard/focus masks through Qt's own xcb connection whenever one of our top-level windows gets mapped, configured or focused. Event masks are per-client, so only Qt's own event delivery is affected. --- CMakeLists.txt | 1 + x11keymaskfixer.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++ x11keymaskfixer.h | 34 ++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 x11keymaskfixer.cpp create mode 100644 x11keymaskfixer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b67d978..e2fce0ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ set(SOURCE_FILES launchercontroller.cpp launchercontroller.h debughelper.cpp debughelper.h inputeventitem.h inputeventitem.cpp + x11keymaskfixer.h x11keymaskfixer.cpp ) set(QML_FILES diff --git a/x11keymaskfixer.cpp b/x11keymaskfixer.cpp new file mode 100644 index 00000000..0b0df5e0 --- /dev/null +++ b/x11keymaskfixer.cpp @@ -0,0 +1,99 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#include "x11keymaskfixer.h" + +#include +#include +#include +#include +#include + +void X11KeyMaskFixer::install() +{ + static X11KeyMaskFixer *self = nullptr; + if (!self) { + self = new X11KeyMaskFixer; + QCoreApplication::instance()->installNativeEventFilter(self); + } +} + +void X11KeyMaskFixer::fixWindow(QWindow *window) +{ + const QString platform = QGuiApplication::platformName(); + if (platform != QLatin1String("dxcb") && platform != QLatin1String("xcb")) + return; + + auto *nativeInterface = QGuiApplication::platformNativeInterface(); + if (!nativeInterface) + return; + auto *connection = static_cast( + nativeInterface->nativeResourceForIntegration("connection")); + if (!connection || !window) + return; + + const xcb_window_t winId = window->winId(); + xcb_get_window_attributes_cookie_t cookie = xcb_get_window_attributes_unchecked(connection, winId); + xcb_get_window_attributes_reply_t *reply = xcb_get_window_attributes_reply(connection, cookie, nullptr); + if (!reply) + return; + const unsigned mask = reply->your_event_mask; + free(reply); + + const unsigned keyboardMask = XCB_EVENT_MASK_KEY_PRESS + | XCB_EVENT_MASK_KEY_RELEASE + | XCB_EVENT_MASK_FOCUS_CHANGE; + if ((mask & keyboardMask) == keyboardMask) + return; + + // OR in the missing masks, keeping whatever this client already selected. + const uint32_t newMask = mask | keyboardMask + | XCB_EVENT_MASK_BUTTON_PRESS + | XCB_EVENT_MASK_BUTTON_RELEASE + | XCB_EVENT_MASK_BUTTON_MOTION + | XCB_EVENT_MASK_ENTER_WINDOW + | XCB_EVENT_MASK_LEAVE_WINDOW + | XCB_EVENT_MASK_POINTER_MOTION + | XCB_EVENT_MASK_PROPERTY_CHANGE; + xcb_change_window_attributes(connection, winId, XCB_CW_EVENT_MASK, &newMask); + xcb_flush(connection); +} + +bool X11KeyMaskFixer::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *) +{ + if (eventType != QByteArrayLiteral("xcb_generic_event_t") || !message) + return false; + + auto *event = static_cast(message); + xcb_window_t windowId = 0; + switch (event->response_type & 0x7f) { + case XCB_MAP_NOTIFY: + windowId = reinterpret_cast(event)->window; + break; + case XCB_CONFIGURE_NOTIFY: + windowId = reinterpret_cast(event)->window; + break; + case XCB_FOCUS_IN: + case XCB_FOCUS_OUT: + windowId = reinterpret_cast(event)->event; + break; + default: + return false; + } + + const auto windows = QGuiApplication::topLevelWindows(); + for (QWindow *window : windows) { + if (window->isVisible() && window->winId() == windowId) { + fixWindow(window); + break; + } + } + return false; +} + +static void installX11KeyMaskFixer() +{ + X11KeyMaskFixer::install(); +} + +Q_COREAPP_STARTUP_FUNCTION(installX11KeyMaskFixer) diff --git a/x11keymaskfixer.h b/x11keymaskfixer.h new file mode 100644 index 00000000..2dad4383 --- /dev/null +++ b/x11keymaskfixer.h @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + +#include +#include + +class QWindow; + +// On X11 the dxcb platform plugin creates override-redirect popup windows +// with an event mask that lacks KeyPressMask/KeyReleaseMask/FocusChangeMask. +// X11 delivers keyboard events per-client based on the event mask each client +// selected, so without those masks no key event ever reaches Qt, while mouse +// events (delivered through XInput2) keep working. This silently breaks text +// input in any popup window of the process, e.g. the launcher search box. +// +// This helper installs a native event filter on QCoreApplication startup and +// restores the keyboard/focus event masks through Qt's own xcb connection +// whenever one of our top-level windows gets mapped or focused. The masks are +// per-client, so only Qt's own event delivery is affected. +class X11KeyMaskFixer : public QAbstractNativeEventFilter +{ +public: + static void install(); + + // Read the window's current event mask and OR in the keyboard, focus and + // pointer masks if they are missing. Idempotent, safe to call repeatedly. + static void fixWindow(QWindow *window); + +private: + bool nativeEventFilter(const QByteArray &eventType, void *message, + qintptr *result) override; +};