Update cocoa_joystick.m

This commit is contained in:
IntellectualKitty 2016-11-25 20:56:01 -07:00 committed by GitHub
parent 39f1aea42c
commit a185cbe9b4

View File

@ -187,6 +187,7 @@ static void removeJoystick(_GLFWjoystickNS* js)
free(js->axes); free(js->axes);
free(js->buttons); free(js->buttons);
free(js->hats);
memset(js, 0, sizeof(_GLFWjoystickNS)); memset(js, 0, sizeof(_GLFWjoystickNS));
@ -224,7 +225,6 @@ static GLFWbool pollJoystickAxisEvents(_GLFWjoystickNS* js)
static GLFWbool pollJoystickButtonEvents(_GLFWjoystickNS* js) static GLFWbool pollJoystickButtonEvents(_GLFWjoystickNS* js)
{ {
CFIndex i; CFIndex i;
int buttonIndex = 0;
if (!js->present) if (!js->present)
return GLFW_FALSE; return GLFW_FALSE;
@ -235,29 +235,41 @@ static GLFWbool pollJoystickButtonEvents(_GLFWjoystickNS* js)
CFArrayGetValueAtIndex(js->buttonElements, i); CFArrayGetValueAtIndex(js->buttonElements, i);
if (getElementValue(js, button)) if (getElementValue(js, button))
js->buttons[buttonIndex++] = GLFW_PRESS; js->buttons[i] = GLFW_PRESS;
else else
js->buttons[buttonIndex++] = GLFW_RELEASE; js->buttons[i] = GLFW_RELEASE;
} }
return GLFW_TRUE;
}
// Polls for joystick hat events and updates GLFW state
//
static GLFWbool pollJoystickHatEvents(_GLFWjoystickNS* js)
{
CFIndex i;
if (!js->present)
return GLFW_FALSE;
for (i = 0; i < CFArrayGetCount(js->hatElements); i++) for (i = 0; i < CFArrayGetCount(js->hatElements); i++)
{ {
_GLFWjoyelementNS* hat = (_GLFWjoyelementNS*) _GLFWjoyelementNS* hat = (_GLFWjoyelementNS*)
CFArrayGetValueAtIndex(js->hatElements, i); CFArrayGetValueAtIndex(js->hatElements, i);
// Bit fields of button presses for each direction, including nil // Bit fields of button presses for each direction, including nil
const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 }; const unsigned char directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
long j, value = getElementValue(js, hat); long j, value = getElementValue(js, hat);
if (value < 0 || value > 8) if (value < 0 || value > 8)
value = 8; value = 8;
js->hats[i] = 0;
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
{ {
if (directions[value] & (1 << j)) unsigned char bit_value = 1 << j;
js->buttons[buttonIndex++] = GLFW_PRESS; if (directions[value] & bit_value)
else js->hats[i] |= bit_value;
js->buttons[buttonIndex++] = GLFW_RELEASE;
} }
} }
@ -321,8 +333,8 @@ static void matchCallback(void* context,
CFRelease(arrayRef); CFRelease(arrayRef);
js->axes = calloc(CFArrayGetCount(js->axisElements), sizeof(float)); js->axes = calloc(CFArrayGetCount(js->axisElements), sizeof(float));
js->buttons = calloc(CFArrayGetCount(js->buttonElements) + js->buttons = calloc(CFArrayGetCount(js->buttonElements), 1);
CFArrayGetCount(js->hatElements) * 4, 1); js->hats = calloc(CFArrayGetCount(js->hatElements), 1);
_glfwInputJoystickChange(jid, GLFW_CONNECTED); _glfwInputJoystickChange(jid, GLFW_CONNECTED);
} }
@ -495,11 +507,20 @@ const unsigned char* _glfwPlatformGetJoystickButtons(int jid, int* count)
if (!pollJoystickButtonEvents(js)) if (!pollJoystickButtonEvents(js))
return NULL; return NULL;
*count = (int) CFArrayGetCount(js->buttonElements) + *count = (int) CFArrayGetCount(js->buttonElements);
(int) CFArrayGetCount(js->hatElements) * 4;
return js->buttons; return js->buttons;
} }
const unsigned char* _glfwPlatformGetJoystickHats(int jid, int* count)
{
_GLFWjoystickNS* js = _glfw.ns_js + jid;
if (!pollJoystickHatEvents(js))
return NULL;
*count = (int) CFArrayGetCount(js->hatElements);
return js->hats;
}
const char* _glfwPlatformGetJoystickName(int jid) const char* _glfwPlatformGetJoystickName(int jid)
{ {
_GLFWjoystickNS* js = _glfw.ns_js + jid; _GLFWjoystickNS* js = _glfw.ns_js + jid;