Move internals to shared joystick objects

Preparation for gamecontrollerdb support and the gamepad API.
This commit is contained in:
Camilla Löwy
2017-01-05 19:44:15 +01:00
parent 43c1910453
commit bfd564f257
10 changed files with 619 additions and 753 deletions
+86 -7
View File
@@ -30,6 +30,7 @@
#include <assert.h>
#include <float.h>
#include <stdlib.h>
#include <string.h>
// Internal key state used for sticky keys
#define _GLFW_STICK 3
@@ -123,12 +124,22 @@ void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths)
window->callbacks.drop((GLFWwindow*) window, count, paths);
}
void _glfwInputJoystickChange(int jid, int event)
void _glfwInputJoystick(int jid, int event)
{
if (_glfw.callbacks.joystick)
_glfw.callbacks.joystick(jid, event);
}
void _glfwInputJoystickAxis(int jid, int axis, float value)
{
_glfw.joysticks[jid].axes[axis] = value;
}
void _glfwInputJoystickButton(int jid, int button, char value)
{
_glfw.joysticks[jid].buttons[button] = value;
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
@@ -141,6 +152,39 @@ GLFWbool _glfwIsPrintable(int key)
key == GLFW_KEY_KP_EQUAL;
}
_GLFWjoystick* _glfwAllocJoystick(const char* name, int axisCount, int buttonCount)
{
int jid;
_GLFWjoystick* js;
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
{
if (!_glfw.joysticks[jid].present)
break;
}
if (jid > GLFW_JOYSTICK_LAST)
return NULL;
js = _glfw.joysticks + jid;
js->present = GLFW_TRUE;
js->name = strdup(name);
js->axes = calloc(axisCount, sizeof(float));
js->buttons = calloc(buttonCount, 1);
js->axisCount = axisCount;
js->buttonCount = buttonCount;
return js;
}
void _glfwFreeJoystick(_GLFWjoystick* js)
{
free(js->name);
free(js->axes);
free(js->buttons);
memset(js, 0, sizeof(_GLFWjoystick));
}
//////////////////////////////////////////////////////////////////////////
////// GLFW public API //////
@@ -554,20 +598,29 @@ GLFWAPI GLFWdropfun glfwSetDropCallback(GLFWwindow* handle, GLFWdropfun cbfun)
GLFWAPI int glfwJoystickPresent(int jid)
{
_GLFW_REQUIRE_INIT_OR_RETURN(0);
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 %i", jid);
return 0;
return GLFW_FALSE;
}
return _glfwPlatformJoystickPresent(jid);
if (!_glfw.joysticks[jid].present)
return GLFW_FALSE;
return _glfwPlatformPollJoystick(jid, _GLFW_POLL_PRESENCE);
}
GLFWAPI const float* glfwGetJoystickAxes(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);
@@ -578,12 +631,22 @@ GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count)
return NULL;
}
return _glfwPlatformGetJoystickAxes(jid, count);
if (!_glfw.joysticks[jid].present)
return NULL;
if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_AXES))
return NULL;
*count = _glfw.joysticks[jid].axisCount;
return _glfw.joysticks[jid].axes;
}
GLFWAPI const unsigned char* glfwGetJoystickButtons(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);
@@ -594,11 +657,21 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
return NULL;
}
return _glfwPlatformGetJoystickButtons(jid, count);
if (!_glfw.joysticks[jid].present)
return NULL;
if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_BUTTONS))
return NULL;
*count = _glfw.joysticks[jid].buttonCount;
return _glfw.joysticks[jid].buttons;
}
GLFWAPI const char* glfwGetJoystickName(int jid)
{
assert(jid >= GLFW_JOYSTICK_1);
assert(jid <= GLFW_JOYSTICK_LAST);
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
@@ -607,7 +680,13 @@ GLFWAPI const char* glfwGetJoystickName(int jid)
return NULL;
}
return _glfwPlatformGetJoystickName(jid);
if (!_glfw.joysticks[jid].present)
return NULL;
if (!_glfwPlatformPollJoystick(jid, _GLFW_POLL_PRESENCE))
return NULL;
return _glfw.joysticks[jid].name;
}
GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)