From ad396b9a028f3576585f38cd3c0e4934cc7d8d9d Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Fri, 5 Nov 2021 22:46:55 +0800
Subject: [PATCH] Add SDL12COMPAT_USE_KEYBOARD_LAYOUT hint
SDL 1.2 seems to only account for (non-en_US) keyboard layouts on some
platforms and configurations. In particular, the KeySym may contain the
translated key, or may contain a "scancode" which is layout-independent.
In particular, on X11 the configured layout is used, but only (it seems)
if it is the only configured layout. (On systems where multiple layouts
are configured, the US layout is used regardless of the configured one.)
Similarly, the windib backend deliberately forces the US layout in order
to match the DirectX backend, which only provides scancodes.
Since this behaviour is very inconsistant between different
configurations, this patch provides a way of configuring which is used
at runtime:
If the SDL12COMPAT_USE_KEYBOARD_LAYOUT environment variable is set to a
nonzero value, sdl12compat will translate keyboard input according to
the current layout. This matches SDL 1.2 on X11 with only one configured
layout. Otherwise (the default), sdl12compat will use SDL2 scancodes for
keyboard input, essentially forcing all input to the US layout. This
matches SDL 1.2 on Windows, and on X11 with multiple configured layouts.
Regardless of the value of this environment variable, the 'unicode'
value will be affected by keyboard layout.
For discussion and more details see:
- sdl12compat issue #135: Non-US Keyboard layouts not supported
https://github.com/libsdl-org/sdl12-compat/issues/135
- sdl12compat PR #97: Use Scancodes instead of Keycodes
https://github.com/libsdl-org/sdl12-compat/pull/97
- SDL 1.2 issue #95: windib returns locale dependent SDLK_ codes
https://github.com/libsdl-org/SDL-1.2/issues/95
---
src/SDL12_compat.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 8e553fc..d523595 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -923,6 +923,7 @@ static char *WindowTitle = NULL;
static char *WindowIconTitle = NULL;
static SDL_Surface *VideoIcon20 = NULL;
static int EnabledUnicode = 0;
+static SDL_bool TranslateKeyboardLayout = SDL_FALSE;
static int VideoDisplayIndex = 0;
static SDL_bool SupportSysWM = SDL_FALSE;
static SDL_bool CDRomInit = SDL_FALSE;
@@ -1729,9 +1730,12 @@ static int
Init12Video(void)
{
const char *driver = SDL20_GetCurrentVideoDriver();
+ const char *layout_env = SDL20_getenv("SDL12COMPAT_USE_KEYBOARD_LAYOUT");
SDL_DisplayMode mode;
int i;
+ TranslateKeyboardLayout = (!layout_env || SDL20_atoi(layout_env)) ? SDL_TRUE : SDL_FALSE;
+
IsDummyVideo = ((driver != NULL) && (SDL20_strcmp(driver, "dummy") == 0)) ? SDL_TRUE : SDL_FALSE;
for (i = 0; i < SDL12_MAXEVENTS-1; i++)
@@ -2561,8 +2565,6 @@ SDL_GetKeyName(SDL12Key key)
return (char *) "unknown key";
}
-#if 0 /* https://github.com/libsdl-org/sdl12-compat/pull/97 */
-# define KeysymFromSDL2(_ev20) Keysym20to12((_ev20)->key.keysym.sym)
static SDL12Key
Keysym20to12(const SDL_Keycode keysym20)
{
@@ -2649,8 +2651,6 @@ Keysym20to12(const SDL_Keycode keysym20)
FIXME("map some of the SDLK12_WORLD keys");
return SDLK12_UNKNOWN;
}
-#else /* https://github.com/libsdl-org/sdl12-compat/pull/97 */
-# define KeysymFromSDL2(_ev20) Scancode20toKeysym12((_ev20)->key.keysym.scancode)
static SDL12Key
Scancode20toKeysym12(const SDL_Scancode scancode20)
{
@@ -2795,7 +2795,6 @@ Scancode20toKeysym12(const SDL_Scancode scancode20)
FIXME("map some of the SDLK12_WORLD keys");
return SDLK12_UNKNOWN;
}
-#endif
static Uint8
Scancode20to12(SDL_Scancode sc)
@@ -3086,7 +3085,12 @@ EventFilter20to12(void *data, SDL_Event *event20)
if (event20->key.repeat) {
return 1; /* ignore 2.0-style key repeat events */
}
- event12.key.keysym.sym = KeysymFromSDL2(event20);
+
+ if (TranslateKeyboardLayout) {
+ event12.key.keysym.sym = Keysym20to12(event20->key.keysym.sym);
+ } else {
+ event12.key.keysym.sym = Scancode20toKeysym12(event20->key.keysym.scancode);
+ }
KeyState[event12.key.keysym.sym] = event20->key.state;
@@ -3108,7 +3112,11 @@ EventFilter20to12(void *data, SDL_Event *event20)
return 1; /* ignore 2.0-style key repeat events */
}
- PendingKeydownEvent.key.keysym.sym = KeysymFromSDL2(event20);
+ if (TranslateKeyboardLayout) {
+ PendingKeydownEvent.key.keysym.sym = Keysym20to12(event20->key.keysym.sym);
+ } else {
+ PendingKeydownEvent.key.keysym.sym = Scancode20toKeysym12(event20->key.keysym.scancode);
+ }
KeyState[PendingKeydownEvent.key.keysym.sym] = event20->key.state;