mirror of
https://github.com/glfw/glfw.git
synced 2026-09-20 16:30:45 +00:00
Implement glfwGetJoystickHats
This moves the buttons-as-hats logic to shared code and adds the GLFW_JOYSTICK_HAT_BUTTONS input mode as a way to disable this legacy behavior. Fixes #889.
This commit is contained in:
+40
-4
@@ -140,6 +140,19 @@ void _glfwInputJoystickButton(int jid, int button, char value)
|
||||
_glfw.joysticks[jid].buttons[button] = value;
|
||||
}
|
||||
|
||||
void _glfwInputJoystickHat(int jid, 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;
|
||||
js->buttons[base + 1] = (value & 0x02) ? GLFW_PRESS : GLFW_RELEASE;
|
||||
js->buttons[base + 2] = (value & 0x04) ? GLFW_PRESS : GLFW_RELEASE;
|
||||
js->buttons[base + 3] = (value & 0x08) ? GLFW_PRESS : GLFW_RELEASE;
|
||||
|
||||
js->hats[hat] = value;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW internal API //////
|
||||
@@ -152,7 +165,10 @@ GLFWbool _glfwIsPrintable(int key)
|
||||
key == GLFW_KEY_KP_EQUAL;
|
||||
}
|
||||
|
||||
_GLFWjoystick* _glfwAllocJoystick(const char* name, int axisCount, int buttonCount)
|
||||
_GLFWjoystick* _glfwAllocJoystick(const char* name,
|
||||
int axisCount,
|
||||
int buttonCount,
|
||||
int hatCount)
|
||||
{
|
||||
int jid;
|
||||
_GLFWjoystick* js;
|
||||
@@ -170,9 +186,11 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name, int axisCount, int buttonCou
|
||||
js->present = GLFW_TRUE;
|
||||
js->name = strdup(name);
|
||||
js->axes = calloc(axisCount, sizeof(float));
|
||||
js->buttons = calloc(buttonCount, 1);
|
||||
js->buttons = calloc(buttonCount + hatCount * 4, 1);
|
||||
js->hats = calloc(hatCount, 1);
|
||||
js->axisCount = axisCount;
|
||||
js->buttonCount = buttonCount;
|
||||
js->hatCount = hatCount;
|
||||
|
||||
return js;
|
||||
}
|
||||
@@ -182,6 +200,7 @@ void _glfwFreeJoystick(_GLFWjoystick* js)
|
||||
free(js->name);
|
||||
free(js->axes);
|
||||
free(js->buttons);
|
||||
free(js->hats);
|
||||
memset(js, 0, sizeof(_GLFWjoystick));
|
||||
}
|
||||
|
||||
@@ -663,13 +682,23 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
|
||||
if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_BUTTONS))
|
||||
return NULL;
|
||||
|
||||
*count = _glfw.joysticks[jid].buttonCount;
|
||||
if (_glfw.hints.init.hatButtons)
|
||||
{
|
||||
*count = _glfw.joysticks[jid].buttonCount +
|
||||
_glfw.joysticks[jid].hatCount * 4;
|
||||
}
|
||||
else
|
||||
*count = _glfw.joysticks[jid].buttonCount;
|
||||
|
||||
return _glfw.joysticks[jid].buttons;
|
||||
}
|
||||
|
||||
GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
|
||||
{
|
||||
assert(jid >= GLFW_JOYSTICK_1);
|
||||
assert(jid <= GLFW_JOYSTICK_LAST);
|
||||
assert(count != NULL);
|
||||
|
||||
*count = 0;
|
||||
|
||||
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
|
||||
@@ -680,7 +709,14 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
if (!_glfw.joysticks[jid].present)
|
||||
return NULL;
|
||||
|
||||
if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_BUTTONS))
|
||||
return NULL;
|
||||
|
||||
*count = _glfw.joysticks[jid].hatCount;
|
||||
return _glfw.joysticks[jid].hats;
|
||||
}
|
||||
|
||||
GLFWAPI const char* glfwGetJoystickName(int jid)
|
||||
|
||||
Reference in New Issue
Block a user