Bug 489021 - RemoteDesktop.NotifyKeyboardKeysym doesn't use correct keyboard layout to calculate keycode
Summary: RemoteDesktop.NotifyKeyboardKeysym doesn't use correct keyboard layout to cal...
Status: REPORTED
Alias: None
Product: xdg-desktop-portal-kde
Classification: Plasma
Component: general (show other bugs)
Version: 6.1.0
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Plasma Bugs List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-06-23 05:41 UTC by Aroun
Modified: 2024-07-22 14:27 UTC (History)
8 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Aroun 2024-06-23 05:41:49 UTC
***
If you're not sure this is actually a bug, instead post about it at https://discuss.kde.org

If you're reporting a crash, attach a backtrace with debug symbols; see https://community.kde.org/Guidelines_and_HOWTOs/Debugging/How_to_create_useful_crash_reports
***

SUMMARY
Inside of Plasma, my keyboard layouts are configured as 1. French (BEPO, AFNOR), 2. French (alt.), and 3. English (US, intl., with dead keys).
The keyboard layout configured in `dpkg-reconfigure keyboard-configuration` is Generic 105-key PC, French, French (alt.), used in sddm and in ttys.
I first noticed this in the WIP KeepassXC implementation of auto-type in Wayland at https://github.com/keepassxreboot/keepassxc/pull/10905, but can also reproduce the issue with KDE Connect, both are using xdg-desktop-portal.

STEPS TO REPRODUCE
1. Have multiple keyboard layout configured in plasma
2. Connect a device through KDE Connect
3. On the connected device, use the Remote input feature and press the keyboard icon

OBSERVED RESULT
If the keyboard layout in Plasma is set to 'fr bepo_afnor', and I press 'a' on the device, 'b' is typed in Plasma.
If the keyboard layout in Plasma is set to 'fr oss', and I press 'a' on the device, 'a' is typed in Plasma.
If the keyboard layout in Plasma is set to 'us intl', and I press 'a' on the device, 'q' is typed in Plasma.

EXPECTED RESULT
Whatever be the keyboard layout set in Plasma, when typing 'a' on the device, 'a' should be typed.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Tuxedo OS 3
KDE Plasma Version: 6.0.5
KDE Frameworks Version: 6.2.0
Qt Version: 6.7.0
Comment 1 michaelk83 2024-06-26 09:07:11 UTC
@Nate Graham, although the steps to reproduce are with KDE Connect, the bug is experienced in relation to xdg-desktop-portal-kde, unrelated to KDE Connect or to Android. See for example https://github.com/keepassxreboot/keepassxc/pull/10905#issuecomment-2171362215 :

> The only issue I’m having is that when I switch keyboard layout, the autotype changes.
> On my default fr oss layout it types correctly. Changing to fr bepo_afnor or us intl types wrong characters.
> It is actually pressing the same keys on the keyboard, but now they have different characters.

The previous product selection was correct. The bug is in the desktop portal implementation, and should be handled there.
Comment 2 Nate Graham 2024-06-26 15:05:06 UTC
Can you show where the bug is in our portal implementation? And maybe also update the title to describe the problem more fully?
Comment 3 Zachary Freed 2024-06-27 20:52:38 UTC
I'm the person implementing the aforementioned auto-type feature in KeepassXC.

I've tracked down the source of the issue to the Xkb class in src/waylandintegration.cpp, it seems like the implementer got mixed up between the client and server. The keymap is correctly obtained through the `keyboard_keymap` event, and a state is created from that keymap. At the start of `keycodeFromKeysym`, the layout is attempted to be obtained from the state through calling `xkb_state_serialize_layout`.

However, as the desktop portal is acting as a XKB client here, it is supposed to be tracking the state itself, and the `xkb_state_serialize_layout` is for server applications to send the layout to clients. Because the desktop portal is using a default state, without tracking anything, this will always return the first defined layout in the keymap, meaning regardless of what layouts are configured, the first layout in the defined order will determine what keys are pressed to produce a symbol (with obviously terrible results if you use a non qwerty layout). 

The correct implementation doesn't require an `xkb_state` to be created at all. Rather,  the `keyboard_modifiers` event should be implemented, which I believe is called by the display server whenever the group (aka layout) is changed, and that value can then be passed to the rest of the `keycodeFromKeysym` function
Comment 4 airmailsteam 2024-07-22 14:27:37 UTC
Typing from KDE connect on android over to Plasma wayland, messes with the capitalisation of the letters. If caps lock on the PC is on, all the letters sent from KDE connect appear as upper case and vice versa if caps lock is off. 

The expected behaviour would be to have the correct letter typed with the case being sent by KDE connect. This breaks typing passwords from my password manager on the phone since they are case sensitive

---
Not sure if it belongs here or if should be a separate bug (if it is, happy to make one)