x11: Don't allow the changing of certain scancodes

The X11 driver uses scancodes derived from keysyms to map the scancodes for extended keys to the physical keyboard, however, this can be incorrect when using certain XKB options (e.g. caps:swapescape), which changes the keysyms emitted by certain keys, but does not imply that their scancodes or positions should be altered. Mark selected scancodes as being non-remappable so that their scancodes aren't changed by toggling XKB mapping options.
This commit is contained in:
Frank Praznik 2023-03-30 11:23:53 -04:00
parent 655a07bdd8
commit 1fd15c9b4e
1 changed files with 27 additions and 2 deletions

View File

@ -44,6 +44,31 @@ static SDL_ScancodeTable scancode_set[] = {
SDL_SCANCODE_TABLE_XVNC,
};
static SDL_bool X11_ScancodeIsRemappable(SDL_Scancode scancode)
{
/*
* XKB remappings can assign different keysyms for these scancodes, but
* as these keys are in fixed positions, the scancodes themselves shouldn't
* be switched. Mark them as not being remappable.
*/
switch (scancode) {
case SDL_SCANCODE_ESCAPE:
case SDL_SCANCODE_CAPSLOCK:
case SDL_SCANCODE_NUMLOCKCLEAR:
case SDL_SCANCODE_LSHIFT:
case SDL_SCANCODE_RSHIFT:
case SDL_SCANCODE_LCTRL:
case SDL_SCANCODE_RCTRL:
case SDL_SCANCODE_LALT:
case SDL_SCANCODE_RALT:
case SDL_SCANCODE_LGUI:
case SDL_SCANCODE_RGUI:
return SDL_FALSE;
default:
return SDL_TRUE;
}
}
/* This function only correctly maps letters and numbers for keyboards in US QWERTY layout */
static SDL_Scancode X11_KeyCodeToSDLScancode(_THIS, KeyCode keycode)
{
@ -264,8 +289,8 @@ int X11_InitKeyboard(_THIS)
if (scancode == data->key_layout[i]) {
continue;
}
if (default_keymap[scancode] >= SDLK_SCANCODE_MASK) {
/* Not a character key, safe to remap */
if (default_keymap[scancode] >= SDLK_SCANCODE_MASK && X11_ScancodeIsRemappable(scancode)) {
/* Not a character key and the scancode is safe to remap */
#ifdef DEBUG_KEYBOARD
SDL_Log("Changing scancode, was %d (%s), now %d (%s)\n", data->key_layout[i], SDL_GetScancodeName(data->key_layout[i]), scancode, SDL_GetScancodeName(scancode));
#endif