From 6f1070cccd4fb9731d3fda8b1c236245a9e3a694 Mon Sep 17 00:00:00 2001 From: Nadia Pedersen Date: Sun, 3 Jun 2018 00:21:43 +0200 Subject: [PATCH] SDL: Add controller hat support in-game and in the GUI. --- SDL/gui.c | 17 +++++++++++++++++ SDL/main.c | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/SDL/gui.c b/SDL/gui.c index 86517647..3e0151b6 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -729,6 +729,23 @@ void run_gui(bool is_running) else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) event.key.keysym.scancode = SDL_SCANCODE_LEFT; else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) event.key.keysym.scancode = SDL_SCANCODE_RIGHT; break; + + case SDL_JOYHATMOTION: { + uint8_t value = event.jhat.value; + if (value != 0) { + uint32_t scancode = + value == SDL_HAT_UP ? SDL_SCANCODE_UP + : value == SDL_HAT_DOWN ? SDL_SCANCODE_DOWN + : value == SDL_HAT_LEFT ? SDL_SCANCODE_LEFT + : value == SDL_HAT_RIGHT ? SDL_SCANCODE_RIGHT + : 0; + + if (scancode != 0) { + event.type = SDL_KEYDOWN; + event.key.keysym.scancode = scancode; + } + } + } case SDL_JOYAXISMOTION: { static bool axis_active[2] = {false, false}; diff --git a/SDL/main.c b/SDL/main.c index e25c7b28..f3ee39e7 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -165,6 +165,21 @@ static void handle_events(GB_gameboy_t *gb) GB_set_key_state(gb, GB_KEY_LEFT, event.jaxis.value < -0x4000); } break; + + case SDL_JOYHATMOTION: + { + uint8_t value = event.jhat.value; + int8_t updown = + value == SDL_HAT_LEFTUP || value == SDL_HAT_UP || value == SDL_HAT_RIGHTUP ? -1 : (value == SDL_HAT_LEFTDOWN || value == SDL_HAT_DOWN || value == SDL_HAT_RIGHTDOWN ? 1 : 0); + int8_t leftright = + value == SDL_HAT_LEFTUP || value == SDL_HAT_LEFT || value == SDL_HAT_LEFTDOWN ? -1 : (value == SDL_HAT_RIGHTUP || value == SDL_HAT_RIGHT || value == SDL_HAT_RIGHTDOWN ? 1 : 0); + + GB_set_key_state(gb, GB_KEY_LEFT, leftright == -1); + GB_set_key_state(gb, GB_KEY_RIGHT, leftright == 1); + GB_set_key_state(gb, GB_KEY_UP, updown == -1); + GB_set_key_state(gb, GB_KEY_DOWN, updown == 1); + break; + }; case SDL_KEYDOWN: switch (event.key.keysym.scancode) {