Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL: Use scancodes for keyboard bindings (#905) #2605

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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