From 90d9503789d9c312c5f32e958752f747138ebfdd Mon Sep 17 00:00:00 2001 From: Dietrich Epp Date: Sat, 30 Jul 2022 16:40:40 -0400 Subject: [PATCH] SDL: Use scancodes for keyboard bindings (#905) Core key bindings for SDL >= 2.0 are now independent of the keyboard layout. QWERTY users get ZXAS, German users get YXAS, French layouts get WXQS, etc. Existing customizations will be ignored, users with customized bindings for SDL will get the new defaults and have to create a new configuration. Existing functionality is preserved for SDL 1.x. --- src/platform/sdl/sdl-events.c | 37 +++++++++++++++++++++++++++++++++++ src/platform/sdl/sdl-events.h | 5 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/platform/sdl/sdl-events.c b/src/platform/sdl/sdl-events.c index a675ff2c093..8d2f2f63492 100644 --- a/src/platform/sdl/sdl-events.c +++ b/src/platform/sdl/sdl-events.c @@ -21,6 +21,13 @@ #define GUI_MOD KMOD_CTRL #endif +#if SDL_VERSION_ATLEAST(2, 0, 0) +#define USE_SCANCODES 1 +#define SDL_BINDING_KEY SDL_BINDING_SCANCODE +#else +#define SDL_BINDING_KEY SDL_BINDING_KEYCODE +#endif + #define GYRO_STEPS 100 #define RUMBLE_PWM 16 #define RUMBLE_STEPS 2 @@ -143,6 +150,31 @@ void mSDLEventsLoadConfig(struct mSDLEvents* context, const struct Configuration } void mSDLInitBindingsGBA(struct mInputMap* inputMap) { +#if USE_SCANCODES +#ifdef BUILD_PANDORA + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_PAGEDOWN, GBA_KEY_A); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_END, GBA_KEY_B); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RSHIFT, GBA_KEY_L); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RCTRL, GBA_KEY_R); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LALT, GBA_KEY_START); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LCTRL, GBA_KEY_SELECT); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT); +#else + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT); + mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT); +#endif +#else #ifdef BUILD_PANDORA mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A); mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B); @@ -165,6 +197,7 @@ void mSDLInitBindingsGBA(struct mInputMap* inputMap) { mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN); mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT); mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT); +#endif #endif struct mInputAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 }; @@ -463,9 +496,13 @@ static void _pauseAfterFrame(struct mCoreThread* context) { static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) { int key = -1; +#if USE_SCANCODES + key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode); +#else if (!(event->keysym.mod & ~(KMOD_NUM | KMOD_CAPS))) { key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym); } +#endif if (key != -1) { mCoreThreadInterrupt(context); if (event->type == SDL_KEYDOWN) { diff --git a/src/platform/sdl/sdl-events.h b/src/platform/sdl/sdl-events.h index 8bdbd7c0b04..005d09b7ec0 100644 --- a/src/platform/sdl/sdl-events.h +++ b/src/platform/sdl/sdl-events.h @@ -27,7 +27,10 @@ CXX_GUARD_START mLOG_DECLARE_CATEGORY(SDL_EVENTS); -#define SDL_BINDING_KEY 0x53444C4BU +// SDL_BINDING_KEYCODE: Bindings for SDL_Keysym.sim (SDL < 2.0). +#define SDL_BINDING_KEYCODE 0x53444C4BU +// SDL_BINDING_SCANCODE: Bindings for SDL_Keysym.scancode (SDL >= 2.0). +#define SDL_BINDING_SCANCODE 0x53444C53U #define SDL_BINDING_BUTTON 0x53444C42U #define MAX_PLAYERS 4