From 61f9dbd95d0cdfac1e361137ee7cce2a8f63b82e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 28 Dec 2017 20:22:54 +0200 Subject: [PATCH] =?UTF-8?q?Use=20SDL=E2=80=99s=20key=20mapping=20when=20av?= =?UTF-8?q?ailable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/apu.c | 3 +- SDL/gui.c | 124 ++++++++++++++++++++++++++++++++++++++++++++--------- SDL/gui.h | 1 + SDL/main.c | 18 +++++++- 4 files changed, 123 insertions(+), 23 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 5ba1911..c74bb6d 100755 --- a/Core/apu.c +++ b/Core/apu.c @@ -93,7 +93,8 @@ static void render(GB_gameboy_t *gb) gb->apu_output.highpass_diff = (GB_double_sample_t) {left_volume * (1 - gb->apu_output.highpass_rate) + gb->apu_output.highpass_diff.left * gb->apu_output.highpass_rate, right_volume * (1 - gb->apu_output.highpass_rate) + gb->apu_output.highpass_diff.right * gb->apu_output.highpass_rate}; - + + case GB_HIGHPASS_MAX:; } } diff --git a/SDL/gui.c b/SDL/gui.c index d44cc00..ba2cb73 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -472,6 +472,7 @@ static void enter_controls_menu(unsigned index) static unsigned joypad_index = 0; SDL_Joystick *joystick = NULL; +SDL_GameController *controller = NULL; const char *current_joypad_name(unsigned index) { static char name[23] = {0,}; @@ -500,14 +501,92 @@ static void cycle_joypads(unsigned index) if (joypad_index >= SDL_NumJoysticks()) { joypad_index = 0; } - if (joystick) { - SDL_JoystickClose(joystick); + if (controller) { + SDL_GameControllerClose(controller); + controller = NULL; } - joystick = SDL_JoystickOpen(joypad_index); + else if (joystick) { + SDL_JoystickClose(joystick); + joystick = NULL; + } + if ((controller = SDL_GameControllerOpen(joypad_index))){ + joystick = SDL_GameControllerGetJoystick(controller); + } + else { + joystick = SDL_JoystickOpen(joypad_index); + } +} + +static void cycle_joypads_backwards(unsigned index) +{ + joypad_index++; + if (joypad_index >= SDL_NumJoysticks()) { + joypad_index = SDL_NumJoysticks() - 1; + } + if (controller) { + SDL_GameControllerClose(controller); + controller = NULL; + } + else if (joystick) { + SDL_JoystickClose(joystick); + joystick = NULL; + } + if ((controller = SDL_GameControllerOpen(joypad_index))){ + joystick = SDL_GameControllerGetJoystick(controller); + } + else { + joystick = SDL_JoystickOpen(joypad_index); + } +} + +unsigned fix_joypad_axis(unsigned axis) +{ + if (controller) { + /* Convert to the mapping used by generic Xbox-style controllers */ + for (SDL_GameControllerAxis i = 0; i < SDL_CONTROLLER_AXIS_MAX; i++) { + if (SDL_GameControllerGetBindForAxis(controller, i).value.axis == axis) { + if (i == SDL_CONTROLLER_AXIS_LEFTX || i == SDL_CONTROLLER_AXIS_RIGHTX) return 0; + if (i == SDL_CONTROLLER_AXIS_LEFTY || i == SDL_CONTROLLER_AXIS_RIGHTY) return 1; + return i; + } + } + return -1; + } + + + if (configuration.div_joystick) { + axis >>= 1; + } + + return axis & 1; } unsigned fix_joypad_button(unsigned button) { + if (controller) { + /* Convert to the mapping used by generic Xbox-style controllers */ + for (SDL_GameControllerButton i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++) { + if (SDL_GameControllerGetBindForButton(controller, i).value.button == button) { + if (i == SDL_CONTROLLER_BUTTON_START) { + return 9; + } + if (i == 9) { + return SDL_CONTROLLER_BUTTON_START; + } + + if (i == SDL_CONTROLLER_BUTTON_BACK) { + return 8; + } + if (i == 8) { + return SDL_CONTROLLER_BUTTON_BACK; + } + return i; + } + } + return -1; + } + + if (configuration.div_joystick) { button >>= 1; } @@ -525,18 +604,6 @@ unsigned fix_joypad_button(unsigned button) return button; } -static void cycle_joypads_backwards(unsigned index) -{ - joypad_index++; - if (joypad_index >= SDL_NumJoysticks()) { - joypad_index = SDL_NumJoysticks() - 1; - } - if (joystick) { - SDL_JoystickClose(joystick); - } - joystick = SDL_JoystickOpen(joypad_index); -} - static void detect_joypad_layout(unsigned index) { gui_state = WAITING_FOR_JBUTTON; @@ -560,11 +627,23 @@ extern void set_filename(const char *new_filename, bool new_should_free); void run_gui(bool is_running) { if (joystick && !SDL_NumJoysticks()) { - SDL_JoystickClose(joystick); - joystick = NULL; + if (controller) { + SDL_GameControllerClose(controller); + controller = NULL; + joystick = NULL; + } + else { + SDL_JoystickClose(joystick); + joystick = NULL; + } } else if (!joystick && SDL_NumJoysticks()) { - joystick = SDL_JoystickOpen(0); + if ((controller = SDL_GameControllerOpen(0))){ + joystick = SDL_GameControllerGetJoystick(controller); + } + else { + joystick = SDL_JoystickOpen(0); + } } /* Draw the background screen */ static SDL_Surface *converted_background = NULL; @@ -599,11 +678,16 @@ void run_gui(bool is_running) else if (event.jbutton.button == 8 || event.jbutton.button == 9) { event.key.keysym.scancode = SDL_SCANCODE_ESCAPE; } + else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) event.key.keysym.scancode = SDL_SCANCODE_UP; + else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) event.key.keysym.scancode = SDL_SCANCODE_DOWN; + 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_JOYAXISMOTION: { static bool axis_active[2] = {false, false}; - if ((event.jaxis.axis >> configuration.div_joystick) & 1) { + event.jaxis.axis = fix_joypad_axis(event.jaxis.axis); + if (event.jaxis.axis == 1) { if (event.jaxis.value > 0x4000) { if (!axis_active[1]) { event.type = SDL_KEYDOWN; @@ -622,7 +706,7 @@ void run_gui(bool is_running) axis_active[1] = false; } } - else { + else if (event.jaxis.axis == 0) { if (event.jaxis.value > 0x4000) { if (!axis_active[0]) { event.type = SDL_KEYDOWN; diff --git a/SDL/gui.h b/SDL/gui.h index 9a4b56e..b68d0d1 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -51,6 +51,7 @@ extern configuration_t configuration; void update_viewport(void); void run_gui(bool is_running); unsigned fix_joypad_button(unsigned button); +unsigned fix_joypad_axis(unsigned axis); void render_texture(void *pixels, void *previous); #endif diff --git a/SDL/main.c b/SDL/main.c index 59469fd..79271a9 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -115,9 +115,22 @@ static void handle_events(GB_gameboy_t *gb) else if (event.jbutton.button == 9) { GB_set_key_state(gb, GB_KEY_START, event.type == SDL_JOYBUTTONDOWN); } + else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) { + GB_set_key_state(gb, GB_KEY_UP, event.type == SDL_JOYBUTTONDOWN); + } + else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) { + GB_set_key_state(gb, GB_KEY_DOWN, event.type == SDL_JOYBUTTONDOWN); + } + else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) { + GB_set_key_state(gb, GB_KEY_LEFT, event.type == SDL_JOYBUTTONDOWN); + } + else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) { + GB_set_key_state(gb, GB_KEY_RIGHT, event.type == SDL_JOYBUTTONDOWN); + } else if (event.jbutton.button & 1) { GB_set_turbo_mode(gb, event.type == SDL_JOYBUTTONDOWN, false); } + else { bool audio_playing = SDL_GetAudioStatus() == SDL_AUDIO_PLAYING; if (audio_playing) { @@ -133,11 +146,12 @@ static void handle_events(GB_gameboy_t *gb) break; case SDL_JOYAXISMOTION: - if ((event.jaxis.axis >> configuration.div_joystick) & 1) { + event.jaxis.axis = fix_joypad_axis(event.jaxis.axis); + if (event.jaxis.axis == 1) { GB_set_key_state(gb, GB_KEY_DOWN, event.jaxis.value > 0x4000); GB_set_key_state(gb, GB_KEY_UP, event.jaxis.value < -0x4000); } - else { + else if (event.jaxis.axis == 0) { GB_set_key_state(gb, GB_KEY_RIGHT, event.jaxis.value > 0x4000); GB_set_key_state(gb, GB_KEY_LEFT, event.jaxis.value < -0x4000); }