diff --git a/config.h b/config.h index d6aabf4d52..6539d2c196 100644 --- a/config.h +++ b/config.h @@ -118,42 +118,49 @@ static const bool audio_sync = true; // Keybinds, Joypad //////////////////// +// Axis threshold (between 0.0 and 1.0) +// How far an axis must be tilted to result in a button press +#define AXIS_THRESHOLD 0.8 + // To figure out which joypad buttons to use, check jstest or similar. +// Axes are configured using the axis number for the positive (up, right) +// direction and the number's two's-complement (~) for negative directions. +// To use the axis, set the button to -1. // Player 1 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 }, - { SNES_DEVICE_ID_JOYPAD_X, 'S', 3 }, - { SNES_DEVICE_ID_JOYPAD_Y, 'A', 2 }, - { SNES_DEVICE_ID_JOYPAD_L, 'Q', 4 }, - { SNES_DEVICE_ID_JOYPAD_R, 'W', 5 }, - { SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 11 }, - { SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 12 }, - { SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 13 }, - { SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 14 }, - { SNES_DEVICE_ID_JOYPAD_START, GLFW_KEY_ENTER, 6 }, - { SNES_DEVICE_ID_JOYPAD_SELECT, GLFW_KEY_RSHIFT, 7 }, - { SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9 }, + // SNES button | keyboard key | js btn | js axis | + { SNES_DEVICE_ID_JOYPAD_A, 'X', 1, 0 }, + { SNES_DEVICE_ID_JOYPAD_B, 'Z', 0, 0 }, + { SNES_DEVICE_ID_JOYPAD_X, 'S', 3, 0 }, + { SNES_DEVICE_ID_JOYPAD_Y, 'A', 2, 0 }, + { SNES_DEVICE_ID_JOYPAD_L, 'Q', 4, 0 }, + { SNES_DEVICE_ID_JOYPAD_R, 'W', 5, 0 }, + { SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 11, ~4 }, + { SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 12, 4 }, + { SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 13, 5 }, + { SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 14, ~5 }, + { SNES_DEVICE_ID_JOYPAD_START, GLFW_KEY_ENTER, 6, 0 }, + { SNES_DEVICE_ID_JOYPAD_SELECT, GLFW_KEY_RSHIFT, 7, 0 }, + { SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9, 0 }, { -1 } }; // Player 2 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, 'J', 11 }, - { SNES_DEVICE_ID_JOYPAD_RIGHT, 'L', 12 }, - { SNES_DEVICE_ID_JOYPAD_UP, 'I', 13 }, - { SNES_DEVICE_ID_JOYPAD_DOWN, 'K', 14 }, - { SNES_DEVICE_ID_JOYPAD_START, 'P', 6 }, - { SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 7 }, + // SNES button | keyboard key | js btn | js axis | + { SNES_DEVICE_ID_JOYPAD_A, 'B', 1, 0 }, + { SNES_DEVICE_ID_JOYPAD_B, 'V', 0, 0 }, + { SNES_DEVICE_ID_JOYPAD_X, 'G', 3, 0 }, + { SNES_DEVICE_ID_JOYPAD_Y, 'F', 2, 0 }, + { SNES_DEVICE_ID_JOYPAD_L, 'R', 4, 0 }, + { SNES_DEVICE_ID_JOYPAD_R, 'T', 5, 0 }, + { SNES_DEVICE_ID_JOYPAD_LEFT, 'J', 11, ~4 }, + { SNES_DEVICE_ID_JOYPAD_RIGHT, 'L', 12, 4 }, + { SNES_DEVICE_ID_JOYPAD_UP, 'I', 13, 5 }, + { SNES_DEVICE_ID_JOYPAD_DOWN, 'K', 14, ~5 }, + { SNES_DEVICE_ID_JOYPAD_START, 'P', 6, 0 }, + { SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 7, 0 }, { -1 } }; diff --git a/driver.h b/driver.h index 346b75ec64..366021bbde 100644 --- a/driver.h +++ b/driver.h @@ -32,6 +32,7 @@ struct snes_keybind int id; int key; int joykey; + int joyaxis; }; typedef struct video_info diff --git a/gl.c b/gl.c index fac7c6946d..64b173bef1 100644 --- a/gl.c +++ b/gl.c @@ -75,9 +75,11 @@ static void glfw_input_poll(void *data) } #define BUTTONS_MAX 128 +#define AXES_MAX 128 static int joypad_id[2]; static int joypad_buttons[2]; +static int joypad_axes[2]; static bool joypad_inited = false; static int joypad_count = 0; @@ -93,6 +95,9 @@ static int init_joypads(int max_pads) joypad_buttons[count] = glfwGetJoystickParam(i, GLFW_BUTTONS); if (joypad_buttons[count] > BUTTONS_MAX) joypad_buttons[count] = BUTTONS_MAX; + joypad_axes[count] = glfwGetJoystickParam(i, GLFW_AXES); + if (joypad_axes[count] > AXES_MAX) + joypad_axes[count] = AXES_MAX; count++; } } @@ -100,13 +105,17 @@ static int init_joypads(int max_pads) return count; } -static bool glfw_is_pressed(int port_num, const struct snes_keybind *key, unsigned char *buttons) +static bool glfw_is_pressed(int port_num, const struct snes_keybind *key, unsigned char *buttons, float *axes) { if (glfwGetKey(key->key)) return true; if (port_num >= joypad_count) return false; - return (key->joykey < joypad_buttons[port_num] && buttons[key->joykey] == GLFW_PRESS); + if (key->joykey >= 0) + return (key->joykey < joypad_buttons[port_num] && buttons[key->joykey] == GLFW_PRESS); + if (key->joyaxis >= 0) + return (key->joyaxis < joypad_axes[port_num] && axes[key->joyaxis] >= AXIS_THRESHOLD); + return (~key->joyaxis < joypad_axes[port_num] && axes[~key->joyaxis] <= -AXIS_THRESHOLD); } static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id) @@ -119,9 +128,12 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b int port_num = port ? 1 : 0; unsigned char buttons[BUTTONS_MAX]; + float axes[AXES_MAX]; - if ( joypad_count > port_num ) + if ( joypad_count > port_num ) { glfwGetJoystickButtons(joypad_id[port_num], buttons, joypad_buttons[port_num]); + glfwGetJoystickPos(joypad_id[port_num], axes, joypad_axes[port_num]); + } const struct snes_keybind *snes_keybinds; @@ -134,9 +146,9 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b bool pressed = false; for ( int i = 0; snes_keybinds[i].id != -1; i++ ) if ( snes_keybinds[i].id == SNES_FAST_FORWARD_KEY ) - set_fast_forward_button(glfw_is_pressed(port_num, &snes_keybinds[i], buttons)); + set_fast_forward_button(glfw_is_pressed(port_num, &snes_keybinds[i], buttons, axes)); else if ( !pressed && snes_keybinds[i].id == (int)id ) - pressed = glfw_is_pressed(port_num, &snes_keybinds[i], buttons); + pressed = glfw_is_pressed(port_num, &snes_keybinds[i], buttons, axes); return pressed; }