Skip to content

Commit

Permalink
SDL: Use scancodes for keyboard bindings (mgba-emu#905)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
depp committed Jul 30, 2022
1 parent 4247fd0 commit 90d9503
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/platform/sdl/sdl-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 };
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/platform/sdl/sdl-events.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 90d9503

Please sign in to comment.