From ae4ece840da41f0e3486f2aebbdbd3fc2f848b05 Mon Sep 17 00:00:00 2001 From: IntellectualKitty Date: Wed, 23 Mar 2016 16:11:34 -0600 Subject: [PATCH] Remove redundant OS X joystick polling Closes #729. --- src/cocoa_joystick.m | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 5c584f6d..9c7ccc42 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -192,27 +192,15 @@ static void removeJoystick(_GLFWjoydeviceNS* joystick) memset(joystick, 0, sizeof(_GLFWjoydeviceNS)); } -// Polls for joystick events and updates GLFW state +// Polls for joystick axis events and updates GLFW state // -static GLFWbool pollJoystickEvents(_GLFWjoydeviceNS* joystick) +static GLFWbool pollJoystickAxisEvents(_GLFWjoydeviceNS* joystick) { CFIndex i; - int buttonIndex = 0; if (!joystick->present) return GLFW_FALSE; - for (i = 0; i < CFArrayGetCount(joystick->buttonElements); i++) - { - _GLFWjoyelementNS* button = (_GLFWjoyelementNS*) - CFArrayGetValueAtIndex(joystick->buttonElements, i); - - if (getElementValue(joystick, button)) - joystick->buttons[buttonIndex++] = GLFW_PRESS; - else - joystick->buttons[buttonIndex++] = GLFW_RELEASE; - } - for (i = 0; i < CFArrayGetCount(joystick->axisElements); i++) { _GLFWjoyelementNS* axis = (_GLFWjoyelementNS*) @@ -227,6 +215,30 @@ static GLFWbool pollJoystickEvents(_GLFWjoydeviceNS* joystick) joystick->axes[i] = (2.f * (value - axis->minReport) / readScale) - 1.f; } + return GLFW_TRUE; +} + +// Polls for joystick button events and updates GLFW state +// +static GLFWbool pollJoystickButtonEvents(_GLFWjoydeviceNS* joystick) +{ + CFIndex i; + int buttonIndex = 0; + + if (!joystick->present) + return GLFW_FALSE; + + for (i = 0; i < CFArrayGetCount(joystick->buttonElements); i++) + { + _GLFWjoyelementNS* button = (_GLFWjoyelementNS*) + CFArrayGetValueAtIndex(joystick->buttonElements, i); + + if (getElementValue(joystick, button)) + joystick->buttons[buttonIndex++] = GLFW_PRESS; + else + joystick->buttons[buttonIndex++] = GLFW_RELEASE; + } + for (i = 0; i < CFArrayGetCount(joystick->hatElements); i++) { _GLFWjoyelementNS* hat = (_GLFWjoyelementNS*) @@ -469,13 +481,13 @@ void _glfwTerminateJoysticksNS(void) int _glfwPlatformJoystickPresent(int joy) { _GLFWjoydeviceNS* joystick = _glfw.ns_js.devices + joy; - return pollJoystickEvents(joystick); + return joystick->present ? GLFW_TRUE : GLFW_FALSE; } const float* _glfwPlatformGetJoystickAxes(int joy, int* count) { _GLFWjoydeviceNS* joystick = _glfw.ns_js.devices + joy; - if (!pollJoystickEvents(joystick)) + if (!pollJoystickAxisEvents(joystick)) return NULL; *count = (int) CFArrayGetCount(joystick->axisElements); @@ -485,7 +497,7 @@ const float* _glfwPlatformGetJoystickAxes(int joy, int* count) const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count) { _GLFWjoydeviceNS* joystick = _glfw.ns_js.devices + joy; - if (!pollJoystickEvents(joystick)) + if (!pollJoystickButtonEvents(joystick)) return NULL; *count = (int) CFArrayGetCount(joystick->buttonElements) + @@ -496,9 +508,9 @@ const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count) const char* _glfwPlatformGetJoystickName(int joy) { _GLFWjoydeviceNS* joystick = _glfw.ns_js.devices + joy; - if (!pollJoystickEvents(joystick)) + if (joystick->present) + return joystick->name; + else return NULL; - - return joystick->name; }