From 572c9e176bc5d2b6051aed74cdd9a91e1087e7c6 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Wed, 7 May 2025 17:41:59 +0600 Subject: [PATCH] Implement keyboard grabbing for Wayland --- src/qt/CMakeLists.txt | 1 + src/qt/qt_main.cpp | 10 ++ src/qt/qt_mainwindow.cpp | 25 ++- src/qt/wl_mouse.cpp | 37 ++++- ...keyboard-shortcuts-inhibit-unstable-v1.xml | 143 ++++++++++++++++++ 5 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 wl_protocols/keyboard-shortcuts-inhibit-unstable-v1.xml diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index 4231034d5..df13d42e2 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -453,6 +453,7 @@ if (UNIX AND NOT APPLE AND NOT HAIKU) set(WL_SOURCE_VAR) ecm_add_wayland_client_protocol(WL_SOURCE_VAR PROTOCOL ${CMAKE_SOURCE_DIR}/wl_protocols/relative-pointer-unstable-v1.xml BASENAME relative-pointer-unstable-v1) ecm_add_wayland_client_protocol(WL_SOURCE_VAR PROTOCOL ${CMAKE_SOURCE_DIR}/wl_protocols/pointer-constraints-unstable-v1.xml BASENAME pointer-constraints-unstable-v1) + ecm_add_wayland_client_protocol(WL_SOURCE_VAR PROTOCOL ${CMAKE_SOURCE_DIR}/wl_protocols/keyboard-shortcuts-inhibit-unstable-v1.xml BASENAME keyboard-shortcuts-inhibit-unstable-v1) target_include_directories(ui PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${Qt${QT_MAJOR}Gui_PRIVATE_INCLUDE_DIRS}) target_sources(ui PRIVATE ${WL_SOURCE_VAR} wl_mouse.cpp) if (XKBCOMMON_FOUND) diff --git a/src/qt/qt_main.cpp b/src/qt/qt_main.cpp index 1f1dd6b49..5cea8c698 100644 --- a/src/qt/qt_main.cpp +++ b/src/qt/qt_main.cpp @@ -672,6 +672,16 @@ main(int argc, char *argv[]) } else { main_window->show(); } +#ifdef __unix__ + if (QApplication::platformName().contains("wayland")) { + /* Force a sync. */ + (void)main_window->winId(); + QApplication::sync(); + extern void wl_keyboard_grab(QWindow *window); + wl_keyboard_grab(main_window->windowHandle()); + } +#endif + app.installEventFilter(main_window); diff --git a/src/qt/qt_mainwindow.cpp b/src/qt/qt_mainwindow.cpp index e966e043a..3e0f9f001 100644 --- a/src/qt/qt_mainwindow.cpp +++ b/src/qt/qt_mainwindow.cpp @@ -322,7 +322,8 @@ MainWindow::MainWindow(QWidget *parent) if (ui->stackedWidget->mouse_capture_func) ui->stackedWidget->mouse_capture_func(this->windowHandle()); } else { - this->releaseKeyboard(); + if (!(windowState() & Qt::WindowActive)) + this->releaseKeyboard(); if (ui->stackedWidget->mouse_uncapture_func) { ui->stackedWidget->mouse_uncapture_func(); } @@ -1492,8 +1493,26 @@ MainWindow::eventFilter(QObject *receiver, QEvent *event) curdopause = dopause; plat_pause(isShowMessage ? 2 : 1); emit setMouseCapture(false); + releaseKeyboard(); } else if (event->type() == QEvent::WindowUnblocked) { plat_pause(curdopause); +#ifdef __unix__ + if (!QApplication::platformName().contains("wayland") && (this->windowState() & Qt::WindowActive)) { + this->grabKeyboard(); + } +#endif + } else if (event->type() == QEvent::WindowActivate) { +#ifdef __unix__ + if (!QApplication::platformName().contains("wayland")) { + this->grabKeyboard(); + } +#endif + } else if (event->type() == QEvent::WindowDeactivate) { +#ifdef __unix__ + if (!QApplication::platformName().contains("wayland")) { + this->releaseKeyboard(); + } +#endif } } @@ -1611,13 +1630,13 @@ MainWindow::getRenderWidgetSize() void MainWindow::focusInEvent(QFocusEvent *event) { - this->grabKeyboard(); + //this->grabKeyboard(); } void MainWindow::focusOutEvent(QFocusEvent *event) { - this->releaseKeyboard(); + //this->releaseKeyboard(); } void diff --git a/src/qt/wl_mouse.cpp b/src/qt/wl_mouse.cpp index 5d6d95a0a..9201c4ec8 100644 --- a/src/qt/wl_mouse.cpp +++ b/src/qt/wl_mouse.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -30,10 +31,12 @@ extern "C" { #include <86box/plat.h> } -static zwp_relative_pointer_manager_v1 *rel_manager = nullptr; -static zwp_relative_pointer_v1 *rel_pointer = nullptr; -static zwp_pointer_constraints_v1 *conf_pointer_interface = nullptr; -static zwp_locked_pointer_v1 *conf_pointer = nullptr; +static zwp_relative_pointer_manager_v1 *rel_manager = nullptr; +static zwp_relative_pointer_v1 *rel_pointer = nullptr; +static zwp_pointer_constraints_v1 *conf_pointer_interface = nullptr; +static zwp_locked_pointer_v1 *conf_pointer = nullptr; +static zwp_keyboard_shortcuts_inhibit_manager_v1 *kbd_manager = nullptr; +static zwp_keyboard_shortcuts_inhibitor_v1 *kbd_inhibitor = nullptr; static bool wl_init_ok = false; @@ -47,6 +50,12 @@ static struct zwp_relative_pointer_v1_listener rel_listener = { rel_mouse_event }; +static struct zwp_keyboard_shortcuts_inhibitor_v1_listener kbd_listener +{ + [](void *data, struct zwp_keyboard_shortcuts_inhibitor_v1 *zwp_keyboard_shortcuts_inhibitor_v1) -> void {}, + [](void *data, struct zwp_keyboard_shortcuts_inhibitor_v1 *zwp_keyboard_shortcuts_inhibitor_v1) -> void {} +}; + static void display_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version) @@ -57,16 +66,25 @@ display_handle_global(void *data, struct wl_registry *registry, uint32_t id, if (!strcmp(interface, "zwp_pointer_constraints_v1")) { conf_pointer_interface = (zwp_pointer_constraints_v1 *) wl_registry_bind(registry, id, &zwp_pointer_constraints_v1_interface, version); } + if (!strcmp(interface, "zwp_keyboard_shortcuts_inhibit_manager_v1")) { + kbd_manager = (zwp_keyboard_shortcuts_inhibit_manager_v1 *) wl_registry_bind(registry, id, &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, version); + } } static void display_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name) { plat_mouse_capture(0); + if (kbd_inhibitor) { + zwp_keyboard_shortcuts_inhibitor_v1_destroy(kbd_inhibitor); + kbd_inhibitor = nullptr; + } + zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(kbd_manager); zwp_relative_pointer_manager_v1_destroy(rel_manager); zwp_pointer_constraints_v1_destroy(conf_pointer_interface); rel_manager = nullptr; conf_pointer_interface = nullptr; + kbd_manager = nullptr; } static const struct wl_registry_listener registry_listener = { @@ -90,9 +108,20 @@ wl_init() } } +void +wl_keyboard_grab(QWindow *window) +{ + if (!kbd_inhibitor && kbd_manager) { + kbd_inhibitor = zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(kbd_manager, (wl_surface *) QGuiApplication::platformNativeInterface()->nativeResourceForWindow("surface", window), (wl_seat *) QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("wl_seat")); + } +} + void wl_mouse_capture(QWindow *window) { + if (!kbd_inhibitor) { + kbd_inhibitor = zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(kbd_manager, (wl_surface *) QGuiApplication::platformNativeInterface()->nativeResourceForWindow("surface", window), (wl_seat *) QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("wl_seat")); + } if (rel_manager) { rel_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(rel_manager, (wl_pointer *) QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("wl_pointer")); zwp_relative_pointer_v1_add_listener(rel_pointer, &rel_listener, nullptr); diff --git a/wl_protocols/keyboard-shortcuts-inhibit-unstable-v1.xml b/wl_protocols/keyboard-shortcuts-inhibit-unstable-v1.xml new file mode 100644 index 000000000..27748764d --- /dev/null +++ b/wl_protocols/keyboard-shortcuts-inhibit-unstable-v1.xml @@ -0,0 +1,143 @@ + + + + + Copyright © 2017 Red Hat Inc. + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + This protocol specifies a way for a client to request the compositor + to ignore its own keyboard shortcuts for a given seat, so that all + key events from that seat get forwarded to a surface. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible + changes may be added together with the corresponding interface + version bump. + Backward incompatible changes are done by bumping the version + number in the protocol and interface names and resetting the + interface version. Once the protocol is to be declared stable, + the 'z' prefix and the version number in the protocol and + interface names are removed and the interface version number is + reset. + + + + + A global interface used for inhibiting the compositor keyboard shortcuts. + + + + + Destroy the keyboard shortcuts inhibitor manager. + + + + + + Create a new keyboard shortcuts inhibitor object associated with + the given surface for the given seat. + + If shortcuts are already inhibited for the specified seat and surface, + a protocol error "already_inhibited" is raised by the compositor. + + + + + + + + + + + + + + A keyboard shortcuts inhibitor instructs the compositor to ignore + its own keyboard shortcuts when the associated surface has keyboard + focus. As a result, when the surface has keyboard focus on the given + seat, it will receive all key events originating from the specified + seat, even those which would normally be caught by the compositor for + its own shortcuts. + + The Wayland compositor is however under no obligation to disable + all of its shortcuts, and may keep some special key combo for its own + use, including but not limited to one allowing the user to forcibly + restore normal keyboard events routing in the case of an unwilling + client. The compositor may also use the same key combo to reactivate + an existing shortcut inhibitor that was previously deactivated on + user request. + + When the compositor restores its own keyboard shortcuts, an + "inactive" event is emitted to notify the client that the keyboard + shortcuts inhibitor is not effectively active for the surface and + seat any more, and the client should not expect to receive all + keyboard events. + + When the keyboard shortcuts inhibitor is inactive, the client has + no way to forcibly reactivate the keyboard shortcuts inhibitor. + + The user can chose to re-enable a previously deactivated keyboard + shortcuts inhibitor using any mechanism the compositor may offer, + in which case the compositor will send an "active" event to notify + the client. + + If the surface is destroyed, unmapped, or loses the seat's keyboard + focus, the keyboard shortcuts inhibitor becomes irrelevant and the + compositor will restore its own keyboard shortcuts but no "inactive" + event is emitted in this case. + + + + + Remove the keyboard shortcuts inhibitor from the associated wl_surface. + + + + + + This event indicates that the shortcut inhibitor is active. + + The compositor sends this event every time compositor shortcuts + are inhibited on behalf of the surface. When active, the client + may receive input events normally reserved by the compositor + (see zwp_keyboard_shortcuts_inhibitor_v1). + + This occurs typically when the initial request "inhibit_shortcuts" + first becomes active or when the user instructs the compositor to + re-enable and existing shortcuts inhibitor using any mechanism + offered by the compositor. + + + + + + This event indicates that the shortcuts inhibitor is inactive, + normal shortcuts processing is restored by the compositor. + + + +