From 1251348c96a2865cf7fab5b183683eb70111106a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Wed, 10 Dec 2025 13:19:35 +0200 Subject: [PATCH] [Wayland] Implement `keyboard_get_label_from_physical` --- .../wayland/display_server_wayland.cpp | 6 ++++ .../linuxbsd/wayland/display_server_wayland.h | 1 + platform/linuxbsd/wayland/wayland_thread.cpp | 30 +++++++++++++++++-- platform/linuxbsd/wayland/wayland_thread.h | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/platform/linuxbsd/wayland/display_server_wayland.cpp b/platform/linuxbsd/wayland/display_server_wayland.cpp index 95ceac8aaf8..ea98b513005 100644 --- a/platform/linuxbsd/wayland/display_server_wayland.cpp +++ b/platform/linuxbsd/wayland/display_server_wayland.cpp @@ -1665,6 +1665,12 @@ Key DisplayServerWayland::keyboard_get_keycode_from_physical(Key p_keycode) cons return key; } +Key DisplayServerWayland::keyboard_get_label_from_physical(Key p_keycode) const { + MutexLock mutex_lock(wayland_thread.mutex); + + return wayland_thread.keyboard_get_label_from_physical(p_keycode); +} + bool DisplayServerWayland::color_picker(const Callable &p_callback) { #ifdef DBUS_ENABLED if (!portal_desktop) { diff --git a/platform/linuxbsd/wayland/display_server_wayland.h b/platform/linuxbsd/wayland/display_server_wayland.h index 2a632c145a2..0b828e12565 100644 --- a/platform/linuxbsd/wayland/display_server_wayland.h +++ b/platform/linuxbsd/wayland/display_server_wayland.h @@ -339,6 +339,7 @@ public: virtual String keyboard_get_layout_language(int p_index) const override; virtual String keyboard_get_layout_name(int p_index) const override; virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const override; + virtual Key keyboard_get_label_from_physical(Key p_keycode) const override; virtual bool color_picker(const Callable &p_callback) override; diff --git a/platform/linuxbsd/wayland/wayland_thread.cpp b/platform/linuxbsd/wayland/wayland_thread.cpp index 70536d53ae6..f7f061a1124 100644 --- a/platform/linuxbsd/wayland/wayland_thread.cpp +++ b/platform/linuxbsd/wayland/wayland_thread.cpp @@ -4932,11 +4932,35 @@ Key WaylandThread::keyboard_get_key_from_physical(Key p_key) const { SeatState *ss = wl_seat_get_seat_state(wl_seat_current); if (ss && ss->xkb_state) { - xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(p_key); - return KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode)); + Key modifiers = p_key & KeyModifierMask::MODIFIER_MASK; + Key keycode_no_mod = p_key & KeyModifierMask::CODE_MASK; + + xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(keycode_no_mod); + Key key = KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode)); + return (Key)(key | modifiers); } - return Key::NONE; + return p_key; +} + +Key WaylandThread::keyboard_get_label_from_physical(Key p_key) const { + SeatState *ss = wl_seat_get_seat_state(wl_seat_current); + + if (ss && ss->xkb_state) { + Key modifiers = p_key & KeyModifierMask::MODIFIER_MASK; + Key keycode_no_mod = p_key & KeyModifierMask::CODE_MASK; + + xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(keycode_no_mod); + xkb_keycode_t xkb_keysym = xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode); + char32_t chr = xkb_keysym_to_utf32(xkb_keysym_to_upper(xkb_keysym)); + if (chr != 0) { + String keysym = String::chr(chr); + Key key = fix_key_label(keysym[0], KeyMappingXKB::get_keycode(xkb_keysym)); + return (Key)(key | modifiers); + } + } + + return p_key; } void WaylandThread::keyboard_echo_keys() { diff --git a/platform/linuxbsd/wayland/wayland_thread.h b/platform/linuxbsd/wayland/wayland_thread.h index e16681e3cb8..f35f27f69ad 100644 --- a/platform/linuxbsd/wayland/wayland_thread.h +++ b/platform/linuxbsd/wayland/wayland_thread.h @@ -1148,6 +1148,7 @@ public: String keyboard_get_layout_name(int p_index) const; Key keyboard_get_key_from_physical(Key p_key) const; + Key keyboard_get_label_from_physical(Key p_key) const; void keyboard_echo_keys();