diff --git a/config.h b/config.h index d9ae22133a..6d908a18ca 100644 --- a/config.h +++ b/config.h @@ -114,7 +114,7 @@ static const bool audio_sync = true; // To figure out which joypad buttons to use, check jstest or similar. -static const struct snes_keybind snes_keybinds[] = { +static const struct snes_keybind snes_keybinds_1[] = { // SNES button | keyboard key | joypad button | { SNES_DEVICE_ID_JOYPAD_A, 'X', 1 }, { SNES_DEVICE_ID_JOYPAD_B, 'Z', 0 }, @@ -132,6 +132,24 @@ static const struct snes_keybind snes_keybinds[] = { { -1 } }; +static const struct snes_keybind snes_keybinds_2[] = { + // SNES button | keyboard key | joypad button | + { SNES_DEVICE_ID_JOYPAD_A, 'B', 1 }, + { SNES_DEVICE_ID_JOYPAD_B, 'V', 0 }, + { SNES_DEVICE_ID_JOYPAD_X, 'G', 3 }, + { SNES_DEVICE_ID_JOYPAD_Y, 'F', 2 }, + { SNES_DEVICE_ID_JOYPAD_L, 'R', 4 }, + { SNES_DEVICE_ID_JOYPAD_R, 'T', 5 }, + { SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 12 }, + { SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 13 }, + { SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 10 }, + { SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 11 }, + { SNES_DEVICE_ID_JOYPAD_START, 'P', 6 }, + { SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 14 }, + { -1 } + //{ SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9 }, +}; + ///// Save state #define SAVE_STATE_KEY GLFW_KEY_F2 ///// Load state diff --git a/driver.h b/driver.h index 887c90505a..27a17cd50d 100644 --- a/driver.h +++ b/driver.h @@ -59,7 +59,7 @@ typedef struct input_driver { void* (*init)(void); void (*poll)(void* data); - int16_t (*input_state)(void* data, const struct snes_keybind *snes_keybinds, bool port, unsigned device, unsigned index, unsigned id); + int16_t (*input_state)(void* data, const struct snes_keybind **snes_keybinds, bool port, unsigned device, unsigned index, unsigned id); void (*free)(void* data); } input_driver_t; diff --git a/gl.c b/gl.c index 8274e6276d..245fd36807 100644 --- a/gl.c +++ b/gl.c @@ -42,58 +42,74 @@ static void glfw_input_poll(void *data) glfwPollEvents(); } -static int16_t glfw_input_state(void *data, const struct snes_keybind *snes_keybinds, bool port, unsigned device, unsigned index, unsigned id) +static int joypad_id[2]; +static int joypad_buttons[2]; +static bool joypad_inited = false; +static int joypad_count = 0; + +static int init_joypads(int max_pads) { - - (void)data; - - if ( port != 0 || device != SNES_DEVICE_JOYPAD ) - return 0; - - int i; - int joypad_id = -1; - int joypad_buttons = -1; - - // Finds the first joypad that's alive - for ( i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++ ) + // Finds the first (two) joypads that are alive + int count = 0; + for ( int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST && count < max_pads; i++ ) { if ( glfwGetJoystickParam(i, GLFW_PRESENT) == GL_TRUE ) { - joypad_id = i; - joypad_buttons = glfwGetJoystickParam(i, GLFW_BUTTONS); - break; + joypad_id[count] = i; + joypad_buttons[count] = glfwGetJoystickParam(i, GLFW_BUTTONS); + count++; } } + joypad_inited = true; + return count; +} - unsigned char buttons[128]; - if ( joypad_id != -1 ) - { - glfwGetJoystickButtons(joypad_id, buttons, joypad_buttons); - } +static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id) +{ + + if ( device != SNES_DEVICE_JOYPAD ) + return 0; + + if ( !joypad_inited ) + joypad_count = init_joypads(2); + + int port_num = port ? 1 : 0; + unsigned char buttons[joypad_buttons[port_num]]; + + if ( joypad_count > id ) + glfwGetJoystickButtons(joypad_id[port_num], buttons, joypad_buttons[port_num]); + + + const struct snes_keybind *snes_keybinds; + if (port == SNES_PORT_1) + snes_keybinds = binds[0]; + else + snes_keybinds = binds[1]; // Finds fast forwarding state. - for ( i = 0; snes_keybinds[i].id != -1; i++ ) + for ( int i = 0; snes_keybinds[i].id != -1; i++ ) { if ( snes_keybinds[i].id == SNES_FAST_FORWARD_KEY ) { bool pressed = false; if ( glfwGetKey(snes_keybinds[i].key) ) pressed = true; - else if ( snes_keybinds[i].joykey < joypad_buttons && buttons[snes_keybinds[i].joykey] == GLFW_PRESS ) + else if ( (joypad_count > id) && (snes_keybinds[i].joykey < joypad_buttons[port_num]) && (buttons[snes_keybinds[i].joykey] == GLFW_PRESS) ) pressed = true; set_fast_forward_button(pressed); break; } } - for ( i = 0; snes_keybinds[i].id != -1; i++ ) + // Checks if button is pressed + for ( int i = 0; snes_keybinds[i].id != -1; i++ ) { if ( snes_keybinds[i].id == (int)id ) { if ( glfwGetKey(snes_keybinds[i].key) ) return 1; - if ( snes_keybinds[i].joykey < joypad_buttons && buttons[snes_keybinds[i].joykey] == GLFW_PRESS ) + if ( (joypad_count > port_num) && (snes_keybinds[i].joykey < joypad_buttons[port_num]) && (buttons[snes_keybinds[i].joykey] == GLFW_PRESS) ) return 1; } } diff --git a/ssnes.c b/ssnes.c index 48d2bd2f4e..70e3739e57 100644 --- a/ssnes.c +++ b/ssnes.c @@ -320,7 +320,8 @@ static void input_poll(void) static int16_t input_state(bool port, unsigned device, unsigned index, unsigned id) { - return driver.input->input_state(driver.input_data, snes_keybinds, port, device, index, id); + const struct snes_keybind *binds[] = { snes_keybinds_1, snes_keybinds_2 }; + return driver.input->input_state(driver.input_data, binds, port, device, index, id); } static void fill_pathname(char *out_path, char *in_path, const char *replace)