diff --git a/src/sdl/inputSDL.cpp b/src/sdl/inputSDL.cpp index 8f0c144c..29bc95da 100644 --- a/src/sdl/inputSDL.cpp +++ b/src/sdl/inputSDL.cpp @@ -89,6 +89,64 @@ uint16_t defaultMotion[4] = { int sensorX = 2047; int sensorY = 2047; +static uint16_t sdlGetHatCode(const SDL_Event &event) +{ + return ( + ((event.jhat.which + 1) << 12) | + (event.jhat.hat << 2) | + ( + event.jhat.value & SDL_HAT_UP ? 0 : + event.jhat.value & SDL_HAT_DOWN ? 1 : + event.jhat.value & SDL_HAT_RIGHT ? 2 : + event.jhat.value & SDL_HAT_LEFT ? 3 : 0 + ) + ); +} + +static uint16_t sdlGetButtonCode(const SDL_Event &event) +{ + return ( + ((event.jbutton.which + 1) << 12) | + (event.jbutton.button + 0x80) + ); +} + +static uint16_t sdlGetAxisCode(const SDL_Event &event) +{ + return ( + ((event.jaxis.which + 1) << 12) | + (event.jaxis.axis << 1) | + ( + event.jaxis.value > 16384 ? 1 : + event.jaxis.value < -16384 ? 0 : 0 + ) + ); +} + +uint16_t inputGetEventCode(const SDL_Event &event) +{ + switch(event.type) + { + case SDL_KEYDOWN: + case SDL_KEYUP: + return event.key.keysym.sym; + break; + case SDL_JOYHATMOTION: + return sdlGetHatCode(event); + break; + case SDL_JOYBUTTONDOWN: + case SDL_JOYBUTTONUP: + return sdlGetButtonCode(event); + break; + case SDL_JOYAXISMOTION: + return sdlGetAxisCode(event); + break; + default: + return 0; + break; + } +} + void inputSetKeymap(int joy, EKey key, uint16_t code) { joypad[joy][key] = code; diff --git a/src/sdl/inputSDL.h b/src/sdl/inputSDL.h index f754db6a..75515273 100644 --- a/src/sdl/inputSDL.h +++ b/src/sdl/inputSDL.h @@ -66,11 +66,18 @@ void inputSetMotionKeymap(EKey key, uint16_t code); bool inputToggleAutoFire(EKey key); /** - * Update the emulated pads state with an SDL event + * Update the emulated pads state with a SDL event * @param SDL_Event An event that has just occured */ void inputProcessSDLEvent(const SDL_Event &event); +/** + * Get the keymap code corresponding to a SDL event + * @param SDL_Event An event that has just occured + * @return Keymap code + */ +uint16_t inputGetEventCode(const SDL_Event &event); + /** * Read the state of an emulated joypad * @param which Emulated joypad index