From e65723069fdda8e5b73773b06c666c9f5d4c7867 Mon Sep 17 00:00:00 2001 From: Enthuin Date: Thu, 20 Oct 2022 20:37:48 -0500 Subject: [PATCH] change gamepad state callback to use the state. post empty event after joystick polling to make waitEvents return --- include/GLFW/glfw3.h | 198 +++++++++++++++++++++---------------------- src/input.c | 25 +++--- tests/events.c | 6 +- 3 files changed, 111 insertions(+), 118 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 8e30fd2f..ecce3438 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1964,107 +1964,6 @@ typedef void (* GLFWmonitorfun)(GLFWmonitor* monitor, int event); */ typedef void (* GLFWjoystickfun)(int jid, int event); -/*! @brief The function pointer type for joystick button callbacks. - * - * This is the function pointer type for joystick button callbacks. A joystick - * button callback function has the following signature: - * @code - * void function_name(int jid, int button, int action) - * @endcode - * - * @param[in] jid The joystick that had a button pressed or released. - * @param[in] button The [joystick button](@ref buttons) that was pressed or released. - * @param[in] action `GLFW_PRESS` or `GLFW_RELEASE`. Future - * releases may add more actions. - * - * @sa @ref input_joystick_button - * @sa @ref glfwSetJoystickButonCallback - * - * @since Added in version 3.4. - * @ingroup input - */ -typedef void (* GLFWjoystickbuttonfun)(int,int,int); - -/*! @brief The function pointer type for joystick axis movement callbacks. - * - * This is the function pointer type for joystick axis movement callbacks. A joystick - * axis movement callback function has the following signature: - * @code - * void function_name(int jid, int axis, float position) - * @endcode - * - * @param[in] jid The joystick that had an axis moved. - * @param[in] axis The [joystick axis](@ref gamepad axes) that was moved. - * @param[in] position A value between -1.0 and 1.0 that indicates the position of the axis. - * - * @sa @ref input_gamepad_axis - * @sa @ref glfwSetJoystickAxisCallback - * - * @since Added in version 3.4. - * @ingroup input - */ -typedef void (* GLFWjoystickaxisfun)(int,int,float); - -/*! @brief The function pointer type for joystick hat movement callbacks. - * - * This is the function pointer type for joystick hat movement callbacks. A joystick - * hat movement callback function has the following signature: - * @code - * void function_name(int jid, int hat, int position) - * @endcode - * - * @param[in] jid The joystick that had an axis moved. - * @param[in] hat The [joystick hat](@ref joystick hats) that was moved. - * @param[in] position A value that indicates the position of the hat. - * The position parameter is one of the following values: - * - * Name | Value - * ---- | ----- - * `GLFW_HAT_CENTERED` | 0 - * `GLFW_HAT_UP` | 1 - * `GLFW_HAT_RIGHT` | 2 - * `GLFW_HAT_DOWN` | 4 - * `GLFW_HAT_LEFT` | 8 - * `GLFW_HAT_RIGHT_UP` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP` - * `GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN` - * `GLFW_HAT_LEFT_UP` | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP` - * `GLFW_HAT_LEFT_DOWN` | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN` - * - * The diagonal directions are bitwise combinations of the primary (up, right, - * down and left) directions and you can test for these individually by ANDing - * it with the corresponding direction. - * - * @sa @ref input_joystick_hat - * @sa @ref glfwSetJoystickHatCallback - * - * @since Added in version 3.4. - * @ingroup input - */ -typedef void (* GLFWjoystickhatfun)(int,int,int); - -/*! @brief The function pointer type for game pad state changes. - * - * This is the function pointer type for game pad state change callbacks. - * A game pad state change callback function has the following signature: - * @code - * void function_name(int jid, GLFWgamepadstate* state) - * @endcode - * - * @param[in] jid The ID of the game pad that changed state. - * @param[in] buttons The states of each - * [gamepad button](@ref gamepad_buttons), - * `GLFW_PRESS` or `GLFW_RELEASE`. - * @param[in] axes The states of each [gamepad axis](@ref gamepad_axes), - * in the range -1.0 to 1.0 inclusive. - * - * @sa @ref input_gamepad - * @sa @ref glfwSetGamepadStateCallback - * - * @since Added in version 3.4. - * @ingroup input - */ -typedef void (* GLFWgamepadstatefun)(int, unsigned char buttons[15], - float axes[6]); /*! @brief Video mode type. * * This describes a single video mode. @@ -2195,6 +2094,103 @@ typedef struct GLFWallocator void* user; } GLFWallocator; +/*! @brief The function pointer type for joystick button callbacks. + * + * This is the function pointer type for joystick button callbacks. A joystick + * button callback function has the following signature: + * @code + * void function_name(int jid, int button, int action) + * @endcode + * + * @param[in] jid The joystick that had a button pressed or released. + * @param[in] button The [joystick button](@ref buttons) that was pressed or released. + * @param[in] action `GLFW_PRESS` or `GLFW_RELEASE`. Future + * releases may add more actions. + * + * @sa @ref input_joystick_button + * @sa @ref glfwSetJoystickButonCallback + * + * @since Added in version 3.4. + * @ingroup input + */ +typedef void (* GLFWjoystickbuttonfun)(int,int,int); + +/*! @brief The function pointer type for joystick axis movement callbacks. + * + * This is the function pointer type for joystick axis movement callbacks. A joystick + * axis movement callback function has the following signature: + * @code + * void function_name(int jid, int axis, float position) + * @endcode + * + * @param[in] jid The joystick that had an axis moved. + * @param[in] axis The [joystick axis](@ref gamepad axes) that was moved. + * @param[in] position A value between -1.0 and 1.0 that indicates the position of the axis. + * + * @sa @ref input_gamepad_axis + * @sa @ref glfwSetJoystickAxisCallback + * + * @since Added in version 3.4. + * @ingroup input + */ +typedef void (* GLFWjoystickaxisfun)(int,int,float); + +/*! @brief The function pointer type for joystick hat movement callbacks. + * + * This is the function pointer type for joystick hat movement callbacks. A joystick + * hat movement callback function has the following signature: + * @code + * void function_name(int jid, int hat, int position) + * @endcode + * + * @param[in] jid The joystick that had an axis moved. + * @param[in] hat The [joystick hat](@ref joystick hats) that was moved. + * @param[in] position A value that indicates the position of the hat. + * The position parameter is one of the following values: + * + * Name | Value + * ---- | ----- + * `GLFW_HAT_CENTERED` | 0 + * `GLFW_HAT_UP` | 1 + * `GLFW_HAT_RIGHT` | 2 + * `GLFW_HAT_DOWN` | 4 + * `GLFW_HAT_LEFT` | 8 + * `GLFW_HAT_RIGHT_UP` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP` + * `GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN` + * `GLFW_HAT_LEFT_UP` | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP` + * `GLFW_HAT_LEFT_DOWN` | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN` + * + * The diagonal directions are bitwise combinations of the primary (up, right, + * down and left) directions and you can test for these individually by ANDing + * it with the corresponding direction. + * + * @sa @ref input_joystick_hat + * @sa @ref glfwSetJoystickHatCallback + * + * @since Added in version 3.4. + * @ingroup input + */ +typedef void (* GLFWjoystickhatfun)(int,int,int); + +/*! @brief The function pointer type for game pad state changes. + * + * This is the function pointer type for game pad state change callbacks. + * A game pad state change callback function has the following signature: + * @code + * void function_name(int jid, GLFWgamepadstate* state) + * @endcode + * + * @param[in] jid The ID of the game pad that changed state. + * @param[in] state the state of the game pad + * + * @sa @ref input_gamepad + * @sa @ref glfwSetGamepadStateCallback + * + * @since Added in version 3.4. + * @ingroup input + */ +typedef void (* GLFWgamepadstatefun)(int jid, GLFWgamepadstate state); + /************************************************************************* * GLFW API functions diff --git a/src/input.c b/src/input.c index 218b4578..3aff147a 100644 --- a/src/input.c +++ b/src/input.c @@ -427,7 +427,7 @@ void _glfwInputGamepad(_GLFWjoystick *js) GLFWgamepadstate state; if (glfwGetGamepadState(jid, &state)) { - _glfw.callbacks.gamepad_state(jid, state.buttons, state.axes); + _glfw.callbacks.gamepad_state(jid, state); } } } @@ -464,9 +464,7 @@ void _glfwInputJoystickAxis(_GLFWjoystick *js, int axis, float value) if (_glfw.callbacks.joystick_axis) _glfw.callbacks.joystick_axis(jid, axis, value); _glfwInputGamepad(js); - } - else - { + } else { js->axes[axis] = value; } } @@ -479,17 +477,15 @@ void _glfwInputJoystickButton(_GLFWjoystick *js, int button, char value) assert(button >= 0); assert(button < js->buttonCount); assert(value == GLFW_PRESS || value == GLFW_RELEASE); - if (js->buttons[button] != value) - { + + if (js->buttons[button] != value) { const int jid = (int)(js - _glfw.joysticks); js->buttons[button] = value; if (_glfw.callbacks.joystick_button) _glfw.callbacks.joystick_button(jid, button, value); _glfwInputGamepad(js); - } - else - { + } else { js->buttons[button] = value; } } @@ -610,7 +606,8 @@ void _glfwPollAllJoysticks() { if (_glfw.joysticks[jid].connected == GLFW_TRUE) { - _glfwPlatformPollJoystick(_glfw.joysticks + jid, _GLFW_POLL_ALL); + _glfw.platform.pollJoystick(_glfw.joysticks + jid, _GLFW_POLL_ALL); + glfwPostEmptyEvent(); } } } @@ -1310,28 +1307,28 @@ GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun) GLFWAPI GLFWgamepadstatefun glfwSetGamepadStateCallback(GLFWgamepadstatefun cbfun) { _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - _GLFW_SWAP_POINTERS(_glfw.callbacks.gamepad_state, cbfun); + _GLFW_SWAP(GLFWgamepadstatefun, _glfw.callbacks.gamepad_state, cbfun); return cbfun; } GLFWAPI GLFWjoystickbuttonfun glfwSetJoystickButtonCallback(GLFWjoystickbuttonfun cbfun) { _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - _GLFW_SWAP_POINTERS(_glfw.callbacks.joystick_button, cbfun); + _GLFW_SWAP(GLFWjoystickbuttonfun, _glfw.callbacks.joystick_button, cbfun); return cbfun; } GLFWAPI GLFWjoystickaxisfun glfwSetJoystickAxisCallback(GLFWjoystickaxisfun cbfun) { _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - _GLFW_SWAP_POINTERS(_glfw.callbacks.joystick_axis, cbfun); + _GLFW_SWAP(GLFWjoystickaxisfun, _glfw.callbacks.joystick_axis, cbfun); return cbfun; } GLFWAPI GLFWjoystickhatfun glfwSetJoystickHatCallback(GLFWjoystickhatfun cbfun) { _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - _GLFW_SWAP_POINTERS(_glfw.callbacks.joystick_hat, cbfun); + _GLFW_SWAP(GLFWjoystickhatfun, _glfw.callbacks.joystick_hat, cbfun); return cbfun; } diff --git a/tests/events.c b/tests/events.c index d849d37d..cfdea0dd 100644 --- a/tests/events.c +++ b/tests/events.c @@ -558,7 +558,7 @@ static void joystick_hat_callback(int jid, int hat, int value) { value); } -static void gamepad_state_callback(int jid, unsigned char buttons[15], float axes[6]) { +static void gamepad_state_callback(int jid, GLFWgamepadstate state) { int i = 0; printf("%08x at %0.3f: Gamepad %i (%s) state:", counter++, glfwGetTime(), @@ -567,11 +567,11 @@ static void gamepad_state_callback(int jid, unsigned char buttons[15], float axe printf("Buttons: "); for (i= 0 ; i < 15; i++) { - printf(" %d:%d", i, buttons[i]); + printf(" %d:%d", i, state.buttons[i]); } printf("Axes: "); for (i= 0 ; i < 6; i++) { - printf(" %d:%0.4f", i, axes[i]); + printf(" %d:%0.4f", i, state.axes[i]); } printf("\n");