diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 02a96acc..81c7e2dc 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -110,7 +110,7 @@ static void closeJoystick(_GLFWjoystick* js) CFRelease(js->ns.hats); _glfwFreeJoystick(js); - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED); + _glfwInputJoystick(js, GLFW_DISCONNECTED); } // Callback for user-initiated joystick addition @@ -261,7 +261,7 @@ static void matchCallback(void* context, js->ns.buttons = buttons; js->ns.hats = hats; - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED); + _glfwInputJoystick(js, GLFW_CONNECTED); } // Callback for user-initiated joystick removal @@ -384,10 +384,8 @@ void _glfwTerminateJoysticksNS(void) ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// -int _glfwPlatformPollJoystick(int jid, int mode) +int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode) { - _GLFWjoystick* js = _glfw.joysticks + jid; - if (mode & _GLFW_POLL_AXES) { CFIndex i; @@ -406,11 +404,11 @@ int _glfwPlatformPollJoystick(int jid, int mode) const long delta = axis->maximum - axis->minimum; if (delta == 0) - _glfwInputJoystickAxis(jid, i, 0.f); + _glfwInputJoystickAxis(js, i, 0.f); else { const float value = (2.f * (raw - axis->minimum) / delta) - 1.f; - _glfwInputJoystickAxis(jid, i, value); + _glfwInputJoystickAxis(js, i, value); } } } @@ -424,7 +422,7 @@ int _glfwPlatformPollJoystick(int jid, int mode) _GLFWjoyelementNS* button = (_GLFWjoyelementNS*) CFArrayGetValueAtIndex(js->ns.buttons, i); const char value = getElementValue(js, button) - button->minimum; - _glfwInputJoystickButton(jid, i, value); + _glfwInputJoystickButton(js, i, value); } for (i = 0; i < CFArrayGetCount(js->ns.hats); i++) @@ -448,7 +446,7 @@ int _glfwPlatformPollJoystick(int jid, int mode) if (state < 0 || state > 8) state = 8; - _glfwInputJoystickHat(jid, i, states[state]); + _glfwInputJoystickHat(js, i, states[state]); } } diff --git a/src/input.c b/src/input.c index ea4aa136..523bfa98 100644 --- a/src/input.c +++ b/src/input.c @@ -256,25 +256,24 @@ void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths) window->callbacks.drop((GLFWwindow*) window, count, paths); } -void _glfwInputJoystick(int jid, int event) +void _glfwInputJoystick(_GLFWjoystick* js, int event) { if (_glfw.callbacks.joystick) - _glfw.callbacks.joystick(jid, event); + _glfw.callbacks.joystick(js - _glfw.joysticks, event); } -void _glfwInputJoystickAxis(int jid, int axis, float value) +void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value) { - _glfw.joysticks[jid].axes[axis] = value; + js->axes[axis] = value; } -void _glfwInputJoystickButton(int jid, int button, char value) +void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value) { - _glfw.joysticks[jid].buttons[button] = value; + js->buttons[button] = value; } -void _glfwInputJoystickHat(int jid, int hat, char value) +void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value) { - _GLFWjoystick* js = _glfw.joysticks + jid; const int base = js->buttonCount + hat * 4; js->buttons[base + 0] = (value & 0x01) ? GLFW_PRESS : GLFW_RELEASE; @@ -753,6 +752,8 @@ GLFWAPI GLFWdropfun glfwSetDropCallback(GLFWwindow* handle, GLFWdropfun cbfun) GLFWAPI int glfwJoystickPresent(int jid) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); @@ -764,14 +765,17 @@ GLFWAPI int glfwJoystickPresent(int jid) return GLFW_FALSE; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return GLFW_FALSE; - return _glfwPlatformPollJoystick(jid, _GLFW_POLL_PRESENCE); + return _glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE); } GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); assert(count != NULL); @@ -786,18 +790,21 @@ GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count) return NULL; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return NULL; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_AXES)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_AXES)) return NULL; - *count = _glfw.joysticks[jid].axisCount; - return _glfw.joysticks[jid].axes; + *count = js->axisCount; + return js->axes; } GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); assert(count != NULL); @@ -812,25 +819,25 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count) return NULL; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return NULL; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_BUTTONS)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS)) return NULL; if (_glfw.hints.init.hatButtons) - { - *count = _glfw.joysticks[jid].buttonCount + - _glfw.joysticks[jid].hatCount * 4; - } + *count = js->buttonCount + js->hatCount * 4; else - *count = _glfw.joysticks[jid].buttonCount; + *count = js->buttonCount; - return _glfw.joysticks[jid].buttons; + return js->buttons; } GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); assert(count != NULL); @@ -845,18 +852,21 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count) return NULL; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return NULL; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_BUTTONS)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS)) return NULL; - *count = _glfw.joysticks[jid].hatCount; - return _glfw.joysticks[jid].hats; + *count = js->hatCount; + return js->hats; } GLFWAPI const char* glfwGetJoystickName(int jid) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); @@ -868,13 +878,14 @@ GLFWAPI const char* glfwGetJoystickName(int jid) return NULL; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return NULL; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_PRESENCE)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE)) return NULL; - return _glfw.joysticks[jid].name; + return js->name; } GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun) @@ -944,6 +955,8 @@ GLFWAPI int glfwUpdateGamepadMappings(const char* string) GLFWAPI int glfwJoystickIsGamepad(int jid) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); @@ -955,17 +968,20 @@ GLFWAPI int glfwJoystickIsGamepad(int jid) return GLFW_FALSE; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return GLFW_FALSE; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_PRESENCE)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE)) return GLFW_FALSE; - return _glfw.joysticks[jid].mapping != NULL; + return js->mapping != NULL; } GLFWAPI const char* glfwGetGamepadName(int jid) { + _GLFWjoystick* js; + assert(jid >= GLFW_JOYSTICK_1); assert(jid <= GLFW_JOYSTICK_LAST); @@ -977,16 +993,17 @@ GLFWAPI const char* glfwGetGamepadName(int jid) return NULL; } - if (!_glfw.joysticks[jid].present) + js = _glfw.joysticks + jid; + if (!js->present) return NULL; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_PRESENCE)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE)) return NULL; - if (!_glfw.joysticks[jid].mapping) + if (!js->mapping) return NULL; - return _glfw.joysticks[jid].mapping->name; + return js->mapping->name; } GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state) @@ -1009,11 +1026,10 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state) } js = _glfw.joysticks + jid; - if (!js->present) return GLFW_FALSE; - if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_ALL)) + if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_ALL)) return GLFW_FALSE; if (!js->mapping) diff --git a/src/internal.h b/src/internal.h index 1ec302ce..d00c7fc0 100644 --- a/src/internal.h +++ b/src/internal.h @@ -254,9 +254,6 @@ typedef void (APIENTRY * PFN_vkVoidFunction)(void); y = t; \ } -// Maps a joystick pointer to an ID -#define _GLFW_JOYSTICK_ID(js) ((int) ((js) - _glfw.joysticks)) - //======================================================================== // Platform-independent structures @@ -645,7 +642,7 @@ void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp) void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string); const char* _glfwPlatformGetClipboardString(_GLFWwindow* window); -int _glfwPlatformPollJoystick(int jid, int mode); +int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode); void _glfwPlatformUpdateGamepadGUID(char* guid); uint64_t _glfwPlatformGetTimerValue(void); @@ -866,32 +863,32 @@ void _glfwInputError(int code, const char* format, ...); void _glfwInputDrop(_GLFWwindow* window, int count, const char** names); /*! @brief Notifies shared code of a joystick connection or disconnection. - * @param[in] jid The joystick that was connected or disconnected. + * @param[in] js The joystick that was connected or disconnected. * @param[in] event One of `GLFW_CONNECTED` or `GLFW_DISCONNECTED`. * @ingroup event */ -void _glfwInputJoystick(int jid, int event); +void _glfwInputJoystick(_GLFWjoystick* js, int event); /*! @brief Notifies shared code of the new value of a joystick axis. - * @param[in] jid The joystick whose axis to update. + * @param[in] js The joystick whose axis to update. * @param[in] axis The index of the axis to update. * @param[in] value The new value of the axis. */ -void _glfwInputJoystickAxis(int jid, int axis, float value); +void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value); /*! @brief Notifies shared code of the new value of a joystick button. - * @param[in] jid The joystick whose button to update. + * @param[in] js The joystick whose button to update. * @param[in] button The index of the button to update. * @param[in] value The new value of the button. */ -void _glfwInputJoystickButton(int jid, int button, char value); +void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value); /*! @brief Notifies shared code of the new value of a joystick hat. - * @param[in] jid The joystick whose hat to update. + * @param[in] js The joystick whose hat to update. * @param[in] button The index of the hat to update. * @param[in] value The new value of the hat. */ -void _glfwInputJoystickHat(int jid, int hat, char value); +void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value); //======================================================================== diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 172de93b..a16f208c 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -42,7 +42,7 @@ // static void handleKeyEvent(_GLFWjoystick* js, int code, int value) { - _glfwInputJoystickButton(_GLFW_JOYSTICK_ID(js), + _glfwInputJoystickButton(js, js->linjs.keyMap[code - BTN_MISC], value ? GLFW_PRESS : GLFW_RELEASE); } @@ -51,7 +51,6 @@ static void handleKeyEvent(_GLFWjoystick* js, int code, int value) // static void handleAbsEvent(_GLFWjoystick* js, int code, int value) { - const int jid = _GLFW_JOYSTICK_ID(js); const int index = js->linjs.absMap[code]; if (code >= ABS_HAT0X && code <= ABS_HAT3Y) @@ -76,7 +75,7 @@ static void handleAbsEvent(_GLFWjoystick* js, int code, int value) else if (value > 0) state[axis] = 2; - _glfwInputJoystickHat(jid, index, stateMap[state[0]][state[1]]); + _glfwInputJoystickHat(js, index, stateMap[state[0]][state[1]]); } else { @@ -92,7 +91,7 @@ static void handleAbsEvent(_GLFWjoystick* js, int code, int value) normalized = normalized * 2.0f - 1.0f; } - _glfwInputJoystickAxis(jid, index, normalized); + _glfwInputJoystickAxis(js, index, normalized); } } @@ -229,7 +228,7 @@ static GLFWbool openJoystickDevice(const char* path) pollAbsState(js); - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED); + _glfwInputJoystick(js, GLFW_CONNECTED); return GLFW_TRUE; } @@ -241,7 +240,7 @@ static void closeJoystick(_GLFWjoystick* js) { close(js->linjs.fd); _glfwFreeJoystick(js); - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED); + _glfwInputJoystick(js, GLFW_DISCONNECTED); } // Lexically compare joysticks by name; used by qsort @@ -398,10 +397,8 @@ void _glfwDetectJoystickConnectionLinux(void) ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// -int _glfwPlatformPollJoystick(int jid, int mode) +int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode) { - _GLFWjoystick* js = _glfw.joysticks + jid; - // Read all queued events (non-blocking) for (;;) { diff --git a/src/win32_joystick.c b/src/win32_joystick.c index fe00747a..759d1d44 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -254,7 +254,7 @@ static void closeJoystick(_GLFWjoystick* js) } _glfwFreeJoystick(js); - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED); + _glfwInputJoystick(js, GLFW_DISCONNECTED); } // DirectInput device object enumeration callback @@ -469,7 +469,7 @@ static BOOL CALLBACK deviceCallback(const DIDEVICEINSTANCE* di, void* user) js->win32.objects = data.objects; js->win32.objectCount = data.objectCount; - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED); + _glfwInputJoystick(js, GLFW_CONNECTED); return DIENUM_CONTINUE; } @@ -552,7 +552,7 @@ void _glfwDetectJoystickConnectionWin32(void) js->win32.index = index; - _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED); + _glfwInputJoystick(js, GLFW_CONNECTED); } } @@ -589,10 +589,8 @@ void _glfwDetectJoystickDisconnectionWin32(void) ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// -int _glfwPlatformPollJoystick(int jid, int mode) +int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode) { - _GLFWjoystick* js = _glfw.joysticks + jid; - if (js->win32.device) { int i, ai = 0, bi = 0, pi = 0; @@ -631,7 +629,7 @@ int _glfwPlatformPollJoystick(int jid, int mode) case _GLFW_TYPE_SLIDER: { const float value = (*((LONG*) data) + 0.5f) / 32767.5f; - _glfwInputJoystickAxis(jid, ai, value); + _glfwInputJoystickAxis(js, ai, value); ai++; break; } @@ -639,7 +637,7 @@ int _glfwPlatformPollJoystick(int jid, int mode) case _GLFW_TYPE_BUTTON: { const char value = (*((BYTE*) data) & 0x80) != 0; - _glfwInputJoystickButton(jid, bi, value); + _glfwInputJoystickButton(js, bi, value); bi++; break; } @@ -664,7 +662,7 @@ int _glfwPlatformPollJoystick(int jid, int mode) if (state < 0 || state > 8) state = 8; - _glfwInputJoystickHat(jid, pi, states[state]); + _glfwInputJoystickHat(js, pi, states[state]); pi++; break; } @@ -728,12 +726,12 @@ int _glfwPlatformPollJoystick(int jid, int mode) axes[5] = xis.Gamepad.bRightTrigger / 127.5f - 1.f; for (i = 0; i < 6; i++) - _glfwInputJoystickAxis(jid, i, axes[i]); + _glfwInputJoystickAxis(js, i, axes[i]); for (i = 0; i < 10; i++) { const char value = (xis.Gamepad.wButtons & buttons[i]) ? 1 : 0; - _glfwInputJoystickButton(jid, i, value); + _glfwInputJoystickButton(js, i, value); } if (xis.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) @@ -745,7 +743,7 @@ int _glfwPlatformPollJoystick(int jid, int mode) if (xis.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) dpad |= GLFW_HAT_LEFT; - _glfwInputJoystickHat(jid, 0, dpad); + _glfwInputJoystickHat(js, 0, dpad); } return GLFW_TRUE;