From 8b2b1d7eda44ab8d12fe2cb09058338d93f12a6b Mon Sep 17 00:00:00 2001 From: Philippe Groarke Date: Sun, 22 Sep 2019 13:36:46 -0400 Subject: [PATCH] joystickcallback : Add new callback, add overload. --- .gitignore | 2 +- include/GLFW/glfw3.h | 64 ++++++++++++++++++++++++++++++++++++++++++++ src/input.c | 22 +++++++++++++++ src/internal.h | 1 + 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 96d5ba3f6..d82ff6fa3 100644 --- a/.gitignore +++ b/.gitignore @@ -82,4 +82,4 @@ tests/timeout tests/title tests/triangle-vulkan tests/windows - +/[Bb]uild*/ diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index dad45a496..560143abb 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1613,6 +1613,32 @@ typedef void (* GLFWmonitorfun)(GLFWmonitor*,int); */ typedef void (* GLFWjoystickfun)(int,int); +/*! @brief The function pointer type for window joystick configuration callbacks. + * + * This is the function pointer type for window joystick configuration callbacks. + * Even though joystick configuration callbacks are global events, you may + * use this version of the callback to recieve valid window pointers in + * your callback. Must be used with appropriate glfwSetJoystickCallback overload. + * + * A joystick configuration callback function has the following signature: + * @code + * void function_name(GLFWwindow* window, int jid, int event) + * @endcode + * + * @param[in] window A valid window associated to the event. + * @param[in] jid The joystick that was connected or disconnected. + * @param[in] event One of `GLFW_CONNECTED` or `GLFW_DISCONNECTED`. Future + * releases may add more events. + * + * @sa @ref joystick_event + * @sa @ref glfwSetJoystickCallback + * + * @since Added in version 3.4. + * + * @ingroup input + */ +typedef void (* GLFWjoystickwindowfun)(GLFWwindow*,int,int); + /*! @brief Video mode type. * * This describes a single video mode. @@ -5139,6 +5165,44 @@ GLFWAPI int glfwJoystickIsGamepad(int jid); */ GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun callback); +/*! @brief Sets the window joystick configuration callback. + * + * This function sets the window joystick configuration callback, or removes the + * currently set callback. This is called when a joystick is connected to or + * disconnected from the system. Windows which have registered to recieve + * this callback will be notified, and provided with their window pointer. + * + * For joystick connection and disconnection events to be delivered on all + * 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 if the joystick is not present. + * + * @param[in] window The window whose callback to set. + * @param[in] callback The new callback, or `NULL` to remove the currently set + * callback. + * @return The previously set callback, or `NULL` if no callback was set or the + * library had not been [initialized](@ref intro_init). + * + * @callback_signature + * @code + * void function_name(GLFWwindow* window, int jid, int event) + * @endcode + * For more information about the callback parameters, see the + * [function pointer type](@ref GLFWjoystickwindowfun). + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED. + * + * @thread_safety This function must only be called from the main thread. + * + * @sa @ref joystick_event + * + * @since Added in version 3.4. + * + * @ingroup input + */ +GLFWAPI GLFWjoystickwindowfun glfwSetJoystickCallback(GLFWwindow* window, GLFWjoystickwindowfun callback); + /*! @brief Adds the specified SDL_GameControllerDB gamepad mappings. * * This function parses the specified ASCII encoded string and updates the diff --git a/src/input.c b/src/input.c index 282917504..d0b19ed2a 100644 --- a/src/input.c +++ b/src/input.c @@ -373,6 +373,18 @@ void _glfwInputJoystick(_GLFWjoystick* js, int event) if (_glfw.callbacks.joystick) _glfw.callbacks.joystick(jid, event); + + // Send event to windows that want the notification. + { + _GLFWwindow* window; + + for (window = _glfw.windowListHead; window; window = window->next) { + GLFWjoystickwindowfun fun = window->callbacks.joystickConnect; + if (fun) { + fun((GLFWwindow*) window, jid, event); + } + } + } } // Notifies shared code of the new value of a joystick axis @@ -1112,6 +1124,16 @@ GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun) return cbfun; } +GLFWAPI GLFWjoystickwindowfun glfwSetJoystickCallback(GLFWwindow* handle, GLFWjoystickwindowfun cbfun) +{ + _GLFWwindow* window = (_GLFWwindow*) handle; + assert(window != NULL); + + _GLFW_REQUIRE_INIT_OR_RETURN(NULL); + _GLFW_SWAP_POINTERS(window->callbacks.joystickConnect, cbfun); + return cbfun; +} + GLFWAPI int glfwUpdateGamepadMappings(const char* string) { int jid; diff --git a/src/internal.h b/src/internal.h index acdae22d4..03bb959ae 100644 --- a/src/internal.h +++ b/src/internal.h @@ -412,6 +412,7 @@ struct _GLFWwindow GLFWcharfun character; GLFWcharmodsfun charmods; GLFWdropfun drop; + GLFWjoystickwindowfun joystickConnect; } callbacks; // This is defined in the window API's platform.h