From a630f36d0061adbdb5eb46621cc9a257a8d394a6 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sun, 12 Apr 2020 11:26:14 -0400 Subject: [PATCH 01/15] add windows gamepad rumble implementation --- include/GLFW/glfw3.h | 31 +++++++++++++++++++++++++++++++ src/input.c | 28 ++++++++++++++++++++++++++++ src/internal.h | 1 + src/win32_init.c | 2 ++ src/win32_joystick.c | 13 +++++++++++++ src/win32_platform.h | 3 +++ 6 files changed, 78 insertions(+) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index e5b9b6c6..cd6b53b5 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5348,6 +5348,37 @@ GLFWAPI const char* glfwGetGamepadName(int jid); */ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); +/*! @brief Sets the specified gamepad's vibration effect intensity. + * + * This function sends vibration data to the specified Xbox-like gamepad. + * + * Vibration intensity is a value between 0.0 and 1.0 inclusive, where 0.0 is no + * vibration, and 1.0 is maximum vibration. It is set separately for the + * gamepad's low frequency and high frequency rumble motors. + * + * If the specified gamepad is not present or does not support the rumble + * effect, this function will return `GLFW_FALSE` but will not generate an + * error. + * + * @param[in] jid The [joystick](@ref joysticks) to vibrate. + * @param[in] slowMotorSpeed The low frequency rumble intensity. + * @param[in] fastMotorSpeed The high frequency rumble intensity. + * @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is + * connected, or the joystick does not support the rumble effect. + * + * @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 glfwUpdateGamepadMappings + * @sa @ref glfwJoystickIsGamepad + * + * @ingroup input + */ +GLFWAPI int glfwSetGamepadRumble(int jid, float slowMotorSpeed, float fastMotorSpeed); + /*! @brief Sets the clipboard to the specified string. * * This function sets the system clipboard to the specified, UTF-8 encoded diff --git a/src/input.c b/src/input.c index f6163093..35fbb072 100644 --- a/src/input.c +++ b/src/input.c @@ -1312,6 +1312,34 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state) return GLFW_TRUE; } +GLFWAPI int glfwSetGamepadRumble(int jid, float slowMotorSpeed, float fastMotorSpeed) +{ + _GLFWjoystick* js; + + assert(jid >= GLFW_JOYSTICK_1); + assert(jid <= GLFW_JOYSTICK_LAST); + + _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE); + + if (jid < 0 || jid > GLFW_JOYSTICK_LAST) + { + _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid); + return GLFW_FALSE; + } + + js = _glfw.joysticks + jid; + if (!js->present) + return GLFW_FALSE; + + slowMotorSpeed = slowMotorSpeed < 0.0f ? 0.0f : slowMotorSpeed; + slowMotorSpeed = slowMotorSpeed > 1.0f ? 1.0f : slowMotorSpeed; + + fastMotorSpeed = fastMotorSpeed < 0.0f ? 0.0f : fastMotorSpeed; + fastMotorSpeed = fastMotorSpeed > 1.0f ? 1.0f : fastMotorSpeed; + + return _glfwPlatformSetGamepadRumble(js, slowMotorSpeed, fastMotorSpeed); +} + GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string) { assert(string != NULL); diff --git a/src/internal.h b/src/internal.h index 6d7587c8..99f7f3f7 100644 --- a/src/internal.h +++ b/src/internal.h @@ -628,6 +628,7 @@ const char* _glfwPlatformGetClipboardString(void); int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode); void _glfwPlatformUpdateGamepadGUID(char* guid); +int _glfwPlatformSetGamepadRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed); uint64_t _glfwPlatformGetTimerValue(void); uint64_t _glfwPlatformGetTimerFrequency(void); diff --git a/src/win32_init.c b/src/win32_init.c index 260e888e..4ea6ef3e 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -128,6 +128,8 @@ static GLFWbool loadLibraries(void) GetProcAddress(_glfw.win32.xinput.instance, "XInputGetCapabilities"); _glfw.win32.xinput.GetState = (PFN_XInputGetState) GetProcAddress(_glfw.win32.xinput.instance, "XInputGetState"); + _glfw.win32.xinput.SetState = (PFN_XInputSetState) + GetProcAddress(_glfw.win32.xinput.instance, "XInputSetState"); break; } diff --git a/src/win32_joystick.c b/src/win32_joystick.c index c19f77c5..e707d7b0 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -753,3 +753,16 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } } +int _glfwPlatformSetGamepadRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +{ + if (js->win32.device) + return GLFW_FALSE; + + XINPUT_VIBRATION effect; + ZeroMemory(&effect, sizeof(XINPUT_VIBRATION)); + + effect.wLeftMotorSpeed = (WORD)(65535.0f * slowMotorSpeed); + effect.wRightMotorSpeed = (WORD)(65535.0f * fastMotorSpeed); + + return (int) (XInputSetState(js->win32.index, &effect) == ERROR_SUCCESS); +} \ No newline at end of file diff --git a/src/win32_platform.h b/src/win32_platform.h index 2b00b001..ac84313a 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -222,8 +222,10 @@ typedef DWORD (WINAPI * PFN_timeGetTime)(void); // xinput.dll function pointer typedefs typedef DWORD (WINAPI * PFN_XInputGetCapabilities)(DWORD,DWORD,XINPUT_CAPABILITIES*); typedef DWORD (WINAPI * PFN_XInputGetState)(DWORD,XINPUT_STATE*); +typedef DWORD (WINAPI * PFN_XInputSetState)(DWORD,XINPUT_VIBRATION*); #define XInputGetCapabilities _glfw.win32.xinput.GetCapabilities #define XInputGetState _glfw.win32.xinput.GetState +#define XInputSetState _glfw.win32.xinput.SetState // dinput8.dll function pointer typedefs typedef HRESULT (WINAPI * PFN_DirectInput8Create)(HINSTANCE,DWORD,REFIID,LPVOID*,LPUNKNOWN); @@ -357,6 +359,7 @@ typedef struct _GLFWlibraryWin32 HINSTANCE instance; PFN_XInputGetCapabilities GetCapabilities; PFN_XInputGetState GetState; + PFN_XInputSetState SetState; } xinput; struct { From 6d8d416c95ab26a4f9381aebfc541a3736cb6781 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sun, 12 Apr 2020 11:26:40 -0400 Subject: [PATCH 02/15] add rumble controls to test gui --- tests/joysticks.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/joysticks.c b/tests/joysticks.c index d4aef36f..562535b3 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -57,6 +57,9 @@ static GLFWwindow* window; static int joysticks[GLFW_JOYSTICK_LAST + 1]; static int joystick_count = 0; +static float slowRumble[GLFW_JOYSTICK_LAST + 1]; +static float fastRumble[GLFW_JOYSTICK_LAST + 1]; + static void error_callback(int error, const char* description) { fprintf(stderr, "Error: %s\n", description); @@ -174,7 +177,9 @@ int main(void) struct nk_context* nk; struct nk_font_atlas* atlas; - memset(joysticks, 0, sizeof(joysticks)); + memset(joysticks, 0, sizeof(joysticks)); + memset(slowRumble, 0, sizeof(slowRumble)); + memset(fastRumble, 0, sizeof(fastRumble)); glfwSetErrorCallback(error_callback); @@ -233,6 +238,11 @@ int main(void) { if (nk_button_label(nk, joystick_label(joysticks[i]))) nk_window_set_focus(nk, joystick_label(joysticks[i])); + + slowRumble[i] = nk_slide_float(nk, 0.0f, slowRumble[i], 1.0f, 0.05f); + fastRumble[i] = nk_slide_float(nk, 0.0f, fastRumble[i], 1.0f, 0.05f); + + glfwSetGamepadRumble(joysticks[i], slowRumble[i], fastRumble[i]); } } else From b0840ad5bbe2c1de63534b74667eb18cc4c9911f Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Thu, 16 Apr 2020 10:17:16 -0400 Subject: [PATCH 03/15] add linux rumble support --- src/linux_joystick.c | 78 +++++++++++++++++++++++++++++++++++++++++++- src/linux_joystick.h | 1 + 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 1f9b35fe..9f278e83 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -122,6 +122,49 @@ static void pollAbsState(_GLFWjoystick* js) #define isBitSet(bit, arr) (arr[(bit) / 8] & (1 << ((bit) % 8))) +static void checkForceFeedback(_GLFWjoystickLinux *linjs) +{ + linjs->rumble = NULL; + struct ff_effect* effect = NULL; + + char ffBits[(FF_CNT + 7) / 8] = {0}; + if (ioctl(linjs->fd, EVIOCGBIT(EV_FF, sizeof(ffBits)), ffBits) < 0) + { + return; + } + + if (isBitSet(FF_RUMBLE, ffBits)) + { + effect = malloc(sizeof(struct ff_effect)); + *effect = (struct ff_effect) + { + .type = FF_RUMBLE, + .id = -1, + .direction = 0, + .trigger = { + .button = 0, + .interval = 0 + }, + .replay = { + .length = 2000, // xinput rumble lasts ~2 seconds + .delay = 0 + }, + .u.rumble = { + .strong_magnitude = 0, + .weak_magnitude = 0 + } + }; + + if (ioctl(linjs->fd, EVIOCSFF, effect) < 0) + { + free(effect); + return; + } else { + linjs->rumble = effect; + } + } +} + // Attempt to open the specified joystick device // static GLFWbool openJoystickDevice(const char* path) @@ -135,7 +178,7 @@ static GLFWbool openJoystickDevice(const char* path) } _GLFWjoystickLinux linjs = {0}; - linjs.fd = open(path, O_RDONLY | O_NONBLOCK); + linjs.fd = open(path, O_RDWR | O_NONBLOCK); if (linjs.fd == -1) return GLFW_FALSE; @@ -222,6 +265,8 @@ static GLFWbool openJoystickDevice(const char* path) } } + checkForceFeedback(&linjs); + _GLFWjoystick* js = _glfwAllocJoystick(name, guid, axisCount, buttonCount, hatCount); if (!js) @@ -246,6 +291,8 @@ static GLFWbool openJoystickDevice(const char* path) static void closeJoystick(_GLFWjoystick* js) { close(js->linjs.fd); + if (js->linjs.rumble) + free(js->linjs.rumble); _glfwFreeJoystick(js); _glfwInputJoystick(js, GLFW_DISCONNECTED); } @@ -431,3 +478,32 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) { } + +int _glfwPlatformSetGamepadRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +{ + _GLFWjoystickLinux *linjs = &js->linjs; + + if (!js->linjs.rumble) + return GLFW_FALSE; + + js->linjs.rumble->u.rumble = (struct ff_rumble_effect) + { + .strong_magnitude = 65535 * slowMotorSpeed, + .weak_magnitude = 65535 * fastMotorSpeed + }; + + struct input_event play = + { + .type = EV_FF, + .code = linjs->rumble->id, + .value = 1 + }; + + if (ioctl(linjs->fd, EVIOCSFF, linjs->rumble) < 0 || + write(linjs->fd, (const void*) &play, sizeof(play)) < 0) + { + return GLFW_FALSE; + } + + return GLFW_TRUE; +} \ No newline at end of file diff --git a/src/linux_joystick.h b/src/linux_joystick.h index 7373f130..c40f9843 100644 --- a/src/linux_joystick.h +++ b/src/linux_joystick.h @@ -43,6 +43,7 @@ typedef struct _GLFWjoystickLinux int absMap[ABS_CNT]; struct input_absinfo absInfo[ABS_CNT]; int hats[4][2]; + struct ff_effect *rumble; } _GLFWjoystickLinux; // Linux-specific joystick API data From 6db3f00ffb099e65e80ecd0478965bb36f5197fb Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Thu, 16 Apr 2020 10:29:52 -0400 Subject: [PATCH 04/15] minor refactoring, documentation tweaks --- include/GLFW/glfw3.h | 10 ++++++---- src/input.c | 4 ++-- src/internal.h | 2 +- src/linux_joystick.c | 2 +- src/win32_joystick.c | 2 +- tests/joysticks.c | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index cd6b53b5..cd120b46 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5348,16 +5348,18 @@ GLFWAPI const char* glfwGetGamepadName(int jid); */ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); -/*! @brief Sets the specified gamepad's vibration effect intensity. +/*! @brief Sets the intensity of an Xbox-like gamepad's rumble effect. * - * This function sends vibration data to the specified Xbox-like gamepad. + * This function sends vibration data to gamepads that implement haptic + * feedback effects using two vibration motors: a low-frequency motor, and + * a high-frequency motor. * * Vibration intensity is a value between 0.0 and 1.0 inclusive, where 0.0 is no * vibration, and 1.0 is maximum vibration. It is set separately for the * gamepad's low frequency and high frequency rumble motors. * * If the specified gamepad is not present or does not support the rumble - * effect, this function will return `GLFW_FALSE` but will not generate an + * effect, this function will return `GLFW_FALSE` but will not generate an * error. * * @param[in] jid The [joystick](@ref joysticks) to vibrate. @@ -5377,7 +5379,7 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); * * @ingroup input */ -GLFWAPI int glfwSetGamepadRumble(int jid, float slowMotorSpeed, float fastMotorSpeed); +GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorSpeed, float fastMotorSpeed); /*! @brief Sets the clipboard to the specified string. * diff --git a/src/input.c b/src/input.c index 35fbb072..e18e0740 100644 --- a/src/input.c +++ b/src/input.c @@ -1312,7 +1312,7 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state) return GLFW_TRUE; } -GLFWAPI int glfwSetGamepadRumble(int jid, float slowMotorSpeed, float fastMotorSpeed) +GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorSpeed, float fastMotorSpeed) { _GLFWjoystick* js; @@ -1337,7 +1337,7 @@ GLFWAPI int glfwSetGamepadRumble(int jid, float slowMotorSpeed, float fastMotorS fastMotorSpeed = fastMotorSpeed < 0.0f ? 0.0f : fastMotorSpeed; fastMotorSpeed = fastMotorSpeed > 1.0f ? 1.0f : fastMotorSpeed; - return _glfwPlatformSetGamepadRumble(js, slowMotorSpeed, fastMotorSpeed); + return _glfwPlatformSetJoystickRumble(js, slowMotorSpeed, fastMotorSpeed); } GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string) diff --git a/src/internal.h b/src/internal.h index 99f7f3f7..e451d9c3 100644 --- a/src/internal.h +++ b/src/internal.h @@ -628,7 +628,7 @@ const char* _glfwPlatformGetClipboardString(void); int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode); void _glfwPlatformUpdateGamepadGUID(char* guid); -int _glfwPlatformSetGamepadRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed); +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed); uint64_t _glfwPlatformGetTimerValue(void); uint64_t _glfwPlatformGetTimerFrequency(void); diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 9f278e83..a8182612 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -479,7 +479,7 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } -int _glfwPlatformSetGamepadRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) { _GLFWjoystickLinux *linjs = &js->linjs; diff --git a/src/win32_joystick.c b/src/win32_joystick.c index e707d7b0..4c0b0b5c 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -753,7 +753,7 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } } -int _glfwPlatformSetGamepadRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) { if (js->win32.device) return GLFW_FALSE; diff --git a/tests/joysticks.c b/tests/joysticks.c index 562535b3..038c9a13 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -242,7 +242,7 @@ int main(void) slowRumble[i] = nk_slide_float(nk, 0.0f, slowRumble[i], 1.0f, 0.05f); fastRumble[i] = nk_slide_float(nk, 0.0f, fastRumble[i], 1.0f, 0.05f); - glfwSetGamepadRumble(joysticks[i], slowRumble[i], fastRumble[i]); + glfwSetJoystickRumble(joysticks[i], slowRumble[i], fastRumble[i]); } } else From d436f7ae77e78a2265c451941ebb7fccf3ad3541 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Thu, 16 Apr 2020 20:16:46 -0400 Subject: [PATCH 05/15] add stubs to other platforms --- src/cocoa_joystick.m | 5 +++++ src/null_joystick.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 88636a87..c351b4eb 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -485,3 +485,8 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } } +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +{ + return GLFW_FALSE; +} + diff --git a/src/null_joystick.c b/src/null_joystick.c index 36c18aa2..27c3a323 100644 --- a/src/null_joystick.c +++ b/src/null_joystick.c @@ -42,3 +42,8 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) { } +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +{ + return GLFW_FALSE; +} + From 1da1879025792bd493b8a8fad24af6d9ecf6ee88 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Fri, 17 Apr 2020 19:08:05 -0400 Subject: [PATCH 06/15] rename helper --- src/linux_joystick.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linux_joystick.c b/src/linux_joystick.c index a8182612..27a929b5 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -122,7 +122,7 @@ static void pollAbsState(_GLFWjoystick* js) #define isBitSet(bit, arr) (arr[(bit) / 8] & (1 << ((bit) % 8))) -static void checkForceFeedback(_GLFWjoystickLinux *linjs) +static void initJoystickForceFeedback(_GLFWjoystickLinux *linjs) { linjs->rumble = NULL; struct ff_effect* effect = NULL; @@ -265,7 +265,7 @@ static GLFWbool openJoystickDevice(const char* path) } } - checkForceFeedback(&linjs); + initJoystickForceFeedback(&linjs); _GLFWjoystick* js = _glfwAllocJoystick(name, guid, axisCount, buttonCount, hatCount); From 96d7b9acf4f4d718d31026e9162075df6052dc72 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sat, 18 Apr 2020 21:43:50 -0400 Subject: [PATCH 07/15] move rumble controls to device window; add labels --- tests/joysticks.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/joysticks.c b/tests/joysticks.c index 038c9a13..4c2fd28f 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -238,11 +238,6 @@ int main(void) { if (nk_button_label(nk, joystick_label(joysticks[i]))) nk_window_set_focus(nk, joystick_label(joysticks[i])); - - slowRumble[i] = nk_slide_float(nk, 0.0f, slowRumble[i], 1.0f, 0.05f); - fastRumble[i] = nk_slide_float(nk, 0.0f, fastRumble[i], 1.0f, 0.05f); - - glfwSetJoystickRumble(joysticks[i], slowRumble[i], fastRumble[i]); } } else @@ -335,6 +330,16 @@ int main(void) nk_layout_row_dynamic(nk, 30, 8); hat_widget(nk, hat); + + nk_layout_row_dynamic(nk, 30, 2); + nk_label(nk, "Slow rumble motor intensity", NK_TEXT_LEFT); + nk_label(nk, "Fast rumble motor intensity", NK_TEXT_LEFT); + + nk_layout_row_dynamic(nk, 30, 2); + slowRumble[i] = nk_slide_float(nk, 0.0f, slowRumble[i], 1.0f, 0.05f); + fastRumble[i] = nk_slide_float(nk, 0.0f, fastRumble[i], 1.0f, 0.05f); + + glfwSetJoystickRumble(joysticks[i], slowRumble[i], fastRumble[i]); } else nk_label(nk, "Joystick has no gamepad mapping", NK_TEXT_LEFT); From 7734040b2249921b1cd3883832d9f2f8bcfbc247 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 18:05:43 -0400 Subject: [PATCH 08/15] minor cleanup --- src/linux_joystick.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 27a929b5..4640f53b 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -158,7 +158,6 @@ static void initJoystickForceFeedback(_GLFWjoystickLinux *linjs) if (ioctl(linjs->fd, EVIOCSFF, effect) < 0) { free(effect); - return; } else { linjs->rumble = effect; } From 7e7d8110fccf29fd17934e8dd799bbaeb2dcf2fd Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 18:30:18 -0400 Subject: [PATCH 09/15] give parameters more sensible names --- deps/mingw/xinput.h | 4 ++-- include/GLFW/glfw3.h | 9 ++++++--- src/cocoa_joystick.m | 2 +- src/input.c | 12 ++++++------ src/internal.h | 2 +- src/linux_joystick.c | 6 +++--- src/null_joystick.c | 2 +- src/win32_joystick.c | 6 +++--- 8 files changed, 23 insertions(+), 20 deletions(-) diff --git a/deps/mingw/xinput.h b/deps/mingw/xinput.h index d3ca726c..4480cc44 100644 --- a/deps/mingw/xinput.h +++ b/deps/mingw/xinput.h @@ -182,8 +182,8 @@ typedef struct _XINPUT_STATE { */ typedef struct _XINPUT_VIBRATION { - WORD wLeftMotorSpeed; - WORD wRightMotorSpeed; + WORD wLeftMotorIntensity; + WORD wRightMotorIntensity; } XINPUT_VIBRATION, *PXINPUT_VIBRATION; /* diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index cd120b46..092336c0 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5363,8 +5363,8 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); * error. * * @param[in] jid The [joystick](@ref joysticks) to vibrate. - * @param[in] slowMotorSpeed The low frequency rumble intensity. - * @param[in] fastMotorSpeed The high frequency rumble intensity. + * @param[in] slowMotorIntensity The low frequency rumble intensity. + * @param[in] fastMotorIntensity The high frequency rumble intensity. * @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is * connected, or the joystick does not support the rumble effect. * @@ -5373,13 +5373,16 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); * * @thread_safety This function must only be called from the main thread. * + * @note @win32 This function is only implemented for XInput devices. + * @note @macos This function is not implemented. + * * @sa @ref gamepad * @sa @ref glfwUpdateGamepadMappings * @sa @ref glfwJoystickIsGamepad * * @ingroup input */ -GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorSpeed, float fastMotorSpeed); +GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorIntensity, float fastMotorIntensity); /*! @brief Sets the clipboard to the specified string. * diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index c351b4eb..687733dc 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -485,7 +485,7 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } } -int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity) { return GLFW_FALSE; } diff --git a/src/input.c b/src/input.c index e18e0740..6a125d6a 100644 --- a/src/input.c +++ b/src/input.c @@ -1312,7 +1312,7 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state) return GLFW_TRUE; } -GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorSpeed, float fastMotorSpeed) +GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorIntensity, float fastMotorIntensity) { _GLFWjoystick* js; @@ -1331,13 +1331,13 @@ GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorSpeed, float fastMotor if (!js->present) return GLFW_FALSE; - slowMotorSpeed = slowMotorSpeed < 0.0f ? 0.0f : slowMotorSpeed; - slowMotorSpeed = slowMotorSpeed > 1.0f ? 1.0f : slowMotorSpeed; + slowMotorIntensity = slowMotorIntensity < 0.0f ? 0.0f : slowMotorIntensity; + slowMotorIntensity = slowMotorIntensity > 1.0f ? 1.0f : slowMotorIntensity; - fastMotorSpeed = fastMotorSpeed < 0.0f ? 0.0f : fastMotorSpeed; - fastMotorSpeed = fastMotorSpeed > 1.0f ? 1.0f : fastMotorSpeed; + fastMotorIntensity = fastMotorIntensity < 0.0f ? 0.0f : fastMotorIntensity; + fastMotorIntensity = fastMotorIntensity > 1.0f ? 1.0f : fastMotorIntensity; - return _glfwPlatformSetJoystickRumble(js, slowMotorSpeed, fastMotorSpeed); + return _glfwPlatformSetJoystickRumble(js, slowMotorIntensity, fastMotorIntensity); } GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string) diff --git a/src/internal.h b/src/internal.h index e451d9c3..4ffbf6c1 100644 --- a/src/internal.h +++ b/src/internal.h @@ -628,7 +628,7 @@ const char* _glfwPlatformGetClipboardString(void); int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode); void _glfwPlatformUpdateGamepadGUID(char* guid); -int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed); +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity); uint64_t _glfwPlatformGetTimerValue(void); uint64_t _glfwPlatformGetTimerFrequency(void); diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 4640f53b..9fe1f406 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -478,7 +478,7 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } -int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity) { _GLFWjoystickLinux *linjs = &js->linjs; @@ -487,8 +487,8 @@ int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, floa js->linjs.rumble->u.rumble = (struct ff_rumble_effect) { - .strong_magnitude = 65535 * slowMotorSpeed, - .weak_magnitude = 65535 * fastMotorSpeed + .strong_magnitude = 65535 * slowMotorIntensity, + .weak_magnitude = 65535 * fastMotorIntensity }; struct input_event play = diff --git a/src/null_joystick.c b/src/null_joystick.c index 27c3a323..150e36ab 100644 --- a/src/null_joystick.c +++ b/src/null_joystick.c @@ -42,7 +42,7 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) { } -int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity) { return GLFW_FALSE; } diff --git a/src/win32_joystick.c b/src/win32_joystick.c index 4c0b0b5c..dd8b2a68 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -753,7 +753,7 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) } } -int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, float fastMotorSpeed) +int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity) { if (js->win32.device) return GLFW_FALSE; @@ -761,8 +761,8 @@ int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorSpeed, floa XINPUT_VIBRATION effect; ZeroMemory(&effect, sizeof(XINPUT_VIBRATION)); - effect.wLeftMotorSpeed = (WORD)(65535.0f * slowMotorSpeed); - effect.wRightMotorSpeed = (WORD)(65535.0f * fastMotorSpeed); + effect.wLeftMotorIntensity = (WORD)(65535.0f * slowMotorIntensity); + effect.wRightMotorIntensity = (WORD)(65535.0f * fastMotorIntensity); return (int) (XInputSetState(js->win32.index, &effect) == ERROR_SUCCESS); } \ No newline at end of file From e60286d58a238bb85bedc5875460ddd2bffeeaf2 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 18:32:09 -0400 Subject: [PATCH 10/15] minor documentation tweak --- include/GLFW/glfw3.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 092336c0..b71e658c 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5363,8 +5363,8 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); * error. * * @param[in] jid The [joystick](@ref joysticks) to vibrate. - * @param[in] slowMotorIntensity The low frequency rumble intensity. - * @param[in] fastMotorIntensity The high frequency rumble intensity. + * @param[in] slowMotorIntensity The low frequency vibration intensity. + * @param[in] fastMotorIntensity The high frequency vibration intensity. * @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is * connected, or the joystick does not support the rumble effect. * From 8fe5ce9335f79bda2ae06f84479736dc9efd944c Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 18:40:37 -0400 Subject: [PATCH 11/15] remove references to gamepads in documentation --- include/GLFW/glfw3.h | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index b71e658c..ad0f4420 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5348,25 +5348,24 @@ GLFWAPI const char* glfwGetGamepadName(int jid); */ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); -/*! @brief Sets the intensity of an Xbox-like gamepad's rumble effect. +/*! @brief Sets the intensity of a joystick's rumble effect. * - * This function sends vibration data to gamepads that implement haptic - * feedback effects using two vibration motors: a low-frequency motor, and - * a high-frequency motor. + * This function sends vibration data to joysticks that implement haptic feedback + * effects using two vibration motors: a low-frequency motor, and a + * high-frequency motor. * * Vibration intensity is a value between 0.0 and 1.0 inclusive, where 0.0 is no - * vibration, and 1.0 is maximum vibration. It is set separately for the - * gamepad's low frequency and high frequency rumble motors. + * vibration, and 1.0 is maximum vibration. It is set separately for the + * joystick's low frequency and high frequency rumble motors. * - * If the specified gamepad is not present or does not support the rumble - * effect, this function will return `GLFW_FALSE` but will not generate an - * error. + * If the specified joystick is not present or does not support the rumble effect, + * this function will return `GLFW_FALSE` but will not generate an error. * * @param[in] jid The [joystick](@ref joysticks) to vibrate. * @param[in] slowMotorIntensity The low frequency vibration intensity. * @param[in] fastMotorIntensity The high frequency vibration intensity. - * @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is - * connected, or the joystick does not support the rumble effect. + * @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is connected, + * or the joystick does not support the rumble effect. * * @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref * GLFW_INVALID_ENUM. @@ -5376,10 +5375,6 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); * @note @win32 This function is only implemented for XInput devices. * @note @macos This function is not implemented. * - * @sa @ref gamepad - * @sa @ref glfwUpdateGamepadMappings - * @sa @ref glfwJoystickIsGamepad - * * @ingroup input */ GLFWAPI int glfwSetJoystickRumble(int jid, float slowMotorIntensity, float fastMotorIntensity); From 4d5062d654e63fa527b9ebdbdd038da0552cbb22 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 19:33:01 -0400 Subject: [PATCH 12/15] fix typo --- deps/mingw/xinput.h | 4 ++-- src/win32_joystick.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deps/mingw/xinput.h b/deps/mingw/xinput.h index 4480cc44..d3ca726c 100644 --- a/deps/mingw/xinput.h +++ b/deps/mingw/xinput.h @@ -182,8 +182,8 @@ typedef struct _XINPUT_STATE { */ typedef struct _XINPUT_VIBRATION { - WORD wLeftMotorIntensity; - WORD wRightMotorIntensity; + WORD wLeftMotorSpeed; + WORD wRightMotorSpeed; } XINPUT_VIBRATION, *PXINPUT_VIBRATION; /* diff --git a/src/win32_joystick.c b/src/win32_joystick.c index dd8b2a68..62b78f56 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -761,8 +761,8 @@ int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, XINPUT_VIBRATION effect; ZeroMemory(&effect, sizeof(XINPUT_VIBRATION)); - effect.wLeftMotorIntensity = (WORD)(65535.0f * slowMotorIntensity); - effect.wRightMotorIntensity = (WORD)(65535.0f * fastMotorIntensity); + effect.wLeftMotorSpeed = (WORD)(65535.0f * slowMotorIntensity); + effect.wRightMotorSpeed = (WORD)(65535.0f * fastMotorIntensity); return (int) (XInputSetState(js->win32.index, &effect) == ERROR_SUCCESS); } \ No newline at end of file From ca2a6c11a194598d3bc9a3610960f9f3531aba0e Mon Sep 17 00:00:00 2001 From: Nikita Date: Sat, 18 Apr 2020 23:43:00 -0400 Subject: [PATCH 13/15] fix mixed declarations error in win32_joystick.c --- src/win32_joystick.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/win32_joystick.c b/src/win32_joystick.c index 62b78f56..ae9490dd 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -755,10 +755,11 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity) { + XINPUT_VIBRATION effect; + if (js->win32.device) return GLFW_FALSE; - XINPUT_VIBRATION effect; ZeroMemory(&effect, sizeof(XINPUT_VIBRATION)); effect.wLeftMotorSpeed = (WORD)(65535.0f * slowMotorIntensity); From da7d21c75a696e54ff9302752fd843c36dc16e55 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 20:52:30 -0400 Subject: [PATCH 14/15] add missing newlines --- src/linux_joystick.c | 3 +-- src/win32_joystick.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 9fe1f406..469683e2 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -477,7 +477,6 @@ void _glfwPlatformUpdateGamepadGUID(char* guid) { } - int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, float fastMotorIntensity) { _GLFWjoystickLinux *linjs = &js->linjs; @@ -505,4 +504,4 @@ int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, } return GLFW_TRUE; -} \ No newline at end of file +} diff --git a/src/win32_joystick.c b/src/win32_joystick.c index ae9490dd..b9c28f02 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -766,4 +766,4 @@ int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, effect.wRightMotorSpeed = (WORD)(65535.0f * fastMotorIntensity); return (int) (XInputSetState(js->win32.index, &effect) == ERROR_SUCCESS); -} \ No newline at end of file +} From 9bd85109d5bc0068d063845bdd87a892acfcc580 Mon Sep 17 00:00:00 2001 From: Nikita Leonidov Date: Sat, 18 Apr 2020 21:02:10 -0400 Subject: [PATCH 15/15] remove trailing whitespace --- include/GLFW/glfw3.h | 10 +++++----- src/win32_joystick.c | 2 +- tests/joysticks.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index ad0f4420..6bd06841 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5349,15 +5349,15 @@ GLFWAPI const char* glfwGetGamepadName(int jid); GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); /*! @brief Sets the intensity of a joystick's rumble effect. - * + * * This function sends vibration data to joysticks that implement haptic feedback * effects using two vibration motors: a low-frequency motor, and a * high-frequency motor. - * - * Vibration intensity is a value between 0.0 and 1.0 inclusive, where 0.0 is no + * + * Vibration intensity is a value between 0.0 and 1.0 inclusive, where 0.0 is no * vibration, and 1.0 is maximum vibration. It is set separately for the * joystick's low frequency and high frequency rumble motors. - * + * * If the specified joystick is not present or does not support the rumble effect, * this function will return `GLFW_FALSE` but will not generate an error. * @@ -5366,7 +5366,7 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); * @param[in] fastMotorIntensity The high frequency vibration intensity. * @return `GLFW_TRUE` if successful, or `GLFW_FALSE` if no joystick is connected, * or the joystick does not support the rumble effect. - * + * * @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref * GLFW_INVALID_ENUM. * diff --git a/src/win32_joystick.c b/src/win32_joystick.c index b9c28f02..affd7eae 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -759,7 +759,7 @@ int _glfwPlatformSetJoystickRumble(_GLFWjoystick* js, float slowMotorIntensity, if (js->win32.device) return GLFW_FALSE; - + ZeroMemory(&effect, sizeof(XINPUT_VIBRATION)); effect.wLeftMotorSpeed = (WORD)(65535.0f * slowMotorIntensity); diff --git a/tests/joysticks.c b/tests/joysticks.c index 4c2fd28f..17bd0427 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -334,7 +334,7 @@ int main(void) nk_layout_row_dynamic(nk, 30, 2); nk_label(nk, "Slow rumble motor intensity", NK_TEXT_LEFT); nk_label(nk, "Fast rumble motor intensity", NK_TEXT_LEFT); - + nk_layout_row_dynamic(nk, 30, 2); slowRumble[i] = nk_slide_float(nk, 0.0f, slowRumble[i], 1.0f, 0.05f); fastRumble[i] = nk_slide_float(nk, 0.0f, fastRumble[i], 1.0f, 0.05f);