Add support for SDL_GameControllerDB

This adds support for importing and applying mappings from the
SDL_GameControllerDB database.

Related to #900.
This commit is contained in:
Camilla Löwy
2017-06-18 15:13:25 +02:00
parent 07bf2b166b
commit 953106e74d
19 changed files with 1186 additions and 80 deletions

View File

@@ -559,6 +559,52 @@ extern "C" {
#define GLFW_JOYSTICK_LAST GLFW_JOYSTICK_16
/*! @} */
/*! @defgroup gamepad_buttons Gamepad buttons
* @brief Gamepad buttons.
*
* See @ref gamepad for how these are used.
*
* @ingroup input
* @{ */
#define GLFW_GAMEPAD_BUTTON_A 0
#define GLFW_GAMEPAD_BUTTON_B 1
#define GLFW_GAMEPAD_BUTTON_X 2
#define GLFW_GAMEPAD_BUTTON_Y 3
#define GLFW_GAMEPAD_BUTTON_LEFT_BUMPER 4
#define GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER 5
#define GLFW_GAMEPAD_BUTTON_BACK 6
#define GLFW_GAMEPAD_BUTTON_START 7
#define GLFW_GAMEPAD_BUTTON_GUIDE 8
#define GLFW_GAMEPAD_BUTTON_LEFT_THUMB 9
#define GLFW_GAMEPAD_BUTTON_RIGHT_THUMB 10
#define GLFW_GAMEPAD_BUTTON_DPAD_UP 11
#define GLFW_GAMEPAD_BUTTON_DPAD_RIGHT 12
#define GLFW_GAMEPAD_BUTTON_DPAD_DOWN 13
#define GLFW_GAMEPAD_BUTTON_DPAD_LEFT 14
#define GLFW_GAMEPAD_BUTTON_LAST GLFW_GAMEPAD_BUTTON_DPAD_LEFT
#define GLFW_GAMEPAD_BUTTON_CROSS GLFW_GAMEPAD_BUTTON_A
#define GLFW_GAMEPAD_BUTTON_CIRCLE GLFW_GAMEPAD_BUTTON_B
#define GLFW_GAMEPAD_BUTTON_SQUARE GLFW_GAMEPAD_BUTTON_X
#define GLFW_GAMEPAD_BUTTON_TRIANGLE GLFW_GAMEPAD_BUTTON_Y
/*! @} */
/*! @defgroup gamepad_axes Gamepad axes
* @brief Gamepad axes.
*
* See @ref gamepad for how these are used.
*
* @ingroup input
* @{ */
#define GLFW_GAMEPAD_AXIS_LEFT_X 0
#define GLFW_GAMEPAD_AXIS_LEFT_Y 1
#define GLFW_GAMEPAD_AXIS_RIGHT_X 2
#define GLFW_GAMEPAD_AXIS_RIGHT_Y 3
#define GLFW_GAMEPAD_AXIS_LEFT_TRIGGER 4
#define GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER 5
#define GLFW_GAMEPAD_AXIS_LAST GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER
/*! @} */
/*! @defgroup errors Error codes
* @brief Error codes.
*
@@ -1475,6 +1521,27 @@ typedef struct GLFWimage
unsigned char* pixels;
} GLFWimage;
/*! @brief Gamepad input state
*
* This describes the input state of a gamepad.
*
* @sa @ref gamepad
* @sa @ref glfwGetGamepadState
*
* @since Added in version 3.3.
*/
typedef struct GLFWgamepadstate
{
/*! The states of each [gamepad button](@ref gamepad_buttons), `GLFW_PRESS`
* or `GLFW_RELEASE`.
*/
char buttons[15];
/*! The states of each [gamepad axis](@ref gamepad_axes), in the range -1.0
* to 1.0 inclusive.
*/
float axes[6];
} GLFWgamepadstate;
/*************************************************************************
* GLFW API functions
@@ -4118,9 +4185,9 @@ GLFWAPI int glfwJoystickPresent(int jid);
* This function returns the values of all axes of the specified joystick.
* Each element in the array is a value between -1.0 and 1.0.
*
* Querying a joystick ID with no device present is not an error, but will
* cause this function to return `NULL`. Call @ref glfwJoystickPresent to
* check device presence.
* If the specified joystick is not present this function will return `NULL`
* but will not generate an error. Call @ref glfwJoystickPresent to check
* device presence.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @param[out] count Where to store the number of axis values in the returned
@@ -4158,9 +4225,9 @@ GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count);
* _left_. To disable these extra buttons, set the @ref
* GLFW_JOYSTICK_HAT_BUTTONS init hint before initialization.
*
* Querying a joystick ID with no device present is not an error, but will
* cause this function to return `NULL`. Call @ref glfwJoystickPresent to
* check device presence.
* If the specified joystick is not present this function will return `NULL`
* but will not generate an error. Call @ref glfwJoystickPresent to check
* device presence.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @param[out] count Where to store the number of button states in the returned
@@ -4215,9 +4282,9 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count);
* }
* @endcode
*
* Querying a joystick ID with no device present is not an error, but will
* cause this function to return `NULL`. Call @ref glfwJoystickPresent to
* check device presence.
* If the specified joystick is not present this function will return `NULL`
* but will not generate an error. Call @ref glfwJoystickPresent to check
* device presence.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @param[out] count Where to store the number of hat states in the returned
@@ -4250,9 +4317,9 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count);
* The returned string is allocated and freed by GLFW. You should not free it
* yourself.
*
* Querying a joystick ID with no device present is not an error, but will
* cause this function to return `NULL`. Call @ref glfwJoystickPresent to
* check device presence.
* If the specified joystick is not present this function will return `NULL`
* but will not generate an error. Call @ref glfwJoystickPresent to check
* device presence.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @return The UTF-8 encoded name of the joystick, or `NULL` if the joystick
@@ -4275,6 +4342,33 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count);
*/
GLFWAPI const char* glfwGetJoystickName(int jid);
/*! @brief Returns whether the specified joystick has a gamepad mapping.
*
* This function returns whether the specified joystick is both present and has
* a gamepad mapping.
*
* If the specified joystick is present but does not have a gamepad mapping
* this function will return `GLFW_FALSE` but will not generate an error. Call
* @ref glfwJoystickPresent to only check device presence.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @return `GLFW_TRUE` if a joystick is both present and has a gamepad mapping,
* or `GLFW_FALSE` otherwise.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_INVALID_ENUM.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref gamepad
* @sa @ref glfwGetGamepadState
*
* @since Added in version 3.3.
*
* @ingroup input
*/
GLFWAPI int glfwJoystickIsGamepad(int jid);
/*! @brief Sets the joystick configuration callback.
*
* This function sets the joystick configuration callback, or removes the
@@ -4285,7 +4379,7 @@ GLFWAPI const char* glfwGetJoystickName(int jid);
* platforms, you need to call one of the [event processing](@ref events)
* functions. Joystick disconnection may also be detected and the callback
* called by joystick functions. The function will then return whatever it
* returns for a disconnected joystick.
* returns if the joystick is not present.
*
* @param[in] cbfun The new callback, or `NULL` to remove the currently set
* callback.
@@ -4304,6 +4398,105 @@ GLFWAPI const char* glfwGetJoystickName(int jid);
*/
GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun);
/*! @brief Adds the specified SDL_GameControllerDB gamepad mappings.
*
* This function parses the specified ASCII encoded string and updates the
* internal list with any gamepad mappings it finds. This string may
* contain either a single gamepad mapping or many mappings separated by
* newlines. The parser supports the full format of the `gamecontrollerdb.txt`
* source file including empty lines and comments.
*
* See @ref gamepad_mapping for a description of the format.
*
* If there is already a gamepad mapping for a given GUID in the internal list,
* it will be replaced by the one passed to this function. If the library is
* terminated and re-initialized the internal list will revert to the built-in
* default.
*
* @param[in] string The string containing the gamepad mappings.
* @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if an
* [error](@ref error_handling) occurred.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_INVALID_VALUE.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref gamepad
* @sa @ref glfwJoystickIsGamepad
* @sa @ref glfwGetGamepadName
*
* @since Added in version 3.3.
*
* @ingroup input
*/
GLFWAPI int glfwUpdateGamepadMappings(const char* string);
/*! @brief Returns the human-readable gamepad name for the specified joystick.
*
* This function returns the human-readable name of the gamepad from the
* gamepad mapping assigned to the specified joystick.
*
* If the specified joystick is not present or does not have a gamepad mapping
* this function will return `NULL` but will not generate an error. Call @ref
* glfwJoystickIsGamepad to check whether it is present and has a gamepad mapping.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @return The UTF-8 encoded name of the gamepad, or `NULL` if the
* joystick is not present, does not have a mapping or an
* [error](@ref error_handling) occurred.
*
* @pointer_lifetime The returned string is allocated and freed by GLFW. You
* should not free it yourself. It is valid until the specified joystick is
* disconnected, the gamepad mappings are updated or the library is terminated.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref gamepad
* @sa @ref glfwJoystickIsGamepad
*
* @since Added in version 3.3.
*
* @ingroup input
*/
GLFWAPI const char* glfwGetGamepadName(int jid);
/*! @brief Retrieves the state of the specified joystick remapped as a gamepad.
*
* This function retrives the state of the specified joystick remapped to
* an Xbox-like gamepad.
*
* If the specified joystick is not present this function will return
* `GLFW_FALSE` but will not generate an error. Call @ref
* glfwJoystickIsGamepad to check whether it is present and has a gamepad
* mapping.
*
* The Guide button may not be available for input as it is often hooked by the
* system or the Steam client.
*
* Not all devices have all the buttons or axes provided by @ref
* GLFWgamepadstate. Unavailable buttons and axes will always report
* `GLFW_RELEASE` and 1.0 respectively.
*
* @param[in] jid The [joystick](@ref joysticks) to query.
* @param[out] state The gamepad input state of the joystick.
* @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is
* connected, it has no gamepad mapping or an [error](@ref error_handling)
* occurred.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_INVALID_ENUM.
*
* @sa @ref gamepad
* @sa @ref glfwUpdateGamepadMappings
* @sa @ref glfwJoystickIsGamepad
*
* @since Added in version 3.3.
*
* @ingroup input
*/
GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state);
/*! @brief Sets the clipboard to the specified string.
*
* This function sets the system clipboard to the specified, UTF-8 encoded