New joystick API.

This commit is contained in:
Camilla Berglund
2013-04-24 19:25:42 +02:00
parent c4d856bcb2
commit 7f2eb7b15b
10 changed files with 280 additions and 461 deletions
+29 -58
View File
@@ -50,7 +50,7 @@
static int openJoystickDevice(int joy, const char* path)
{
#ifdef __linux__
char numAxes, numButtons;
char axisCount, buttonCount;
char name[256];
int fd, version;
@@ -74,14 +74,14 @@ static int openJoystickDevice(int joy, const char* path)
_glfw.x11.joystick[joy].name = strdup(name);
ioctl(fd, JSIOCGAXES, &numAxes);
_glfw.x11.joystick[joy].numAxes = (int) numAxes;
ioctl(fd, JSIOCGAXES, &axisCount);
_glfw.x11.joystick[joy].axisCount = (int) axisCount;
ioctl(fd, JSIOCGBUTTONS, &numButtons);
_glfw.x11.joystick[joy].numButtons = (int) numButtons;
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
_glfw.x11.joystick[joy].buttonCount = (int) buttonCount;
_glfw.x11.joystick[joy].axis = (float*) calloc(numAxes, sizeof(float));
_glfw.x11.joystick[joy].button = (unsigned char*) calloc(numButtons, 1);
_glfw.x11.joystick[joy].axes = (float*) calloc(axisCount, sizeof(float));
_glfw.x11.joystick[joy].buttons = (unsigned char*) calloc(buttonCount, 1);
_glfw.x11.joystick[joy].present = GL_TRUE;
#endif // __linux__
@@ -110,7 +110,12 @@ static void pollJoystickEvents(void)
result = read(_glfw.x11.joystick[i].fd, &e, sizeof(e));
if (errno == ENODEV)
{
free(_glfw.x11.joystick[i].axes);
free(_glfw.x11.joystick[i].buttons);
free(_glfw.x11.joystick[i].name);
_glfw.x11.joystick[i].present = GL_FALSE;
}
if (result == -1)
break;
@@ -121,21 +126,21 @@ static void pollJoystickEvents(void)
switch (e.type)
{
case JS_EVENT_AXIS:
_glfw.x11.joystick[i].axis[e.number] =
_glfw.x11.joystick[i].axes[e.number] =
(float) e.value / 32767.0f;
// We need to change the sign for the Y axes, so that
// positive = up/forward, according to the GLFW spec.
if (e.number & 1)
{
_glfw.x11.joystick[i].axis[e.number] =
-_glfw.x11.joystick[i].axis[e.number];
_glfw.x11.joystick[i].axes[e.number] =
-_glfw.x11.joystick[i].axes[e.number];
}
break;
case JS_EVENT_BUTTON:
_glfw.x11.joystick[i].button[e.number] =
_glfw.x11.joystick[i].buttons[e.number] =
e.value ? GLFW_PRESS : GLFW_RELEASE;
break;
@@ -214,8 +219,8 @@ void _glfwTerminateJoysticks(void)
if (_glfw.x11.joystick[i].present)
{
close(_glfw.x11.joystick[i].fd);
free(_glfw.x11.joystick[i].axis);
free(_glfw.x11.joystick[i].button);
free(_glfw.x11.joystick[i].axes);
free(_glfw.x11.joystick[i].buttons);
free(_glfw.x11.joystick[i].name);
_glfw.x11.joystick[i].present = GL_FALSE;
@@ -229,72 +234,38 @@ void _glfwTerminateJoysticks(void)
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
int _glfwPlatformGetJoystickParam(int joy, int param)
int _glfwPlatformJoystickPresent(int joy)
{
pollJoystickEvents();
if (!_glfw.x11.joystick[joy].present)
return 0;
switch (param)
{
case GLFW_PRESENT:
return GL_TRUE;
case GLFW_AXES:
return _glfw.x11.joystick[joy].numAxes;
case GLFW_BUTTONS:
return _glfw.x11.joystick[joy].numButtons;
default:
_glfwInputError(GLFW_INVALID_ENUM, NULL);
}
return 0;
return _glfw.x11.joystick[joy].present;
}
int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numAxes)
float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{
int i;
pollJoystickEvents();
if (!_glfw.x11.joystick[joy].present)
return 0;
return NULL;
if (_glfw.x11.joystick[joy].numAxes < numAxes)
numAxes = _glfw.x11.joystick[joy].numAxes;
for (i = 0; i < numAxes; i++)
axes[i] = _glfw.x11.joystick[joy].axis[i];
return numAxes;
*count = _glfw.x11.joystick[joy].axisCount;
return _glfw.x11.joystick[joy].axes;
}
int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
int numButtons)
unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{
int i;
pollJoystickEvents();
if (!_glfw.x11.joystick[joy].present)
return 0;
return NULL;
if (_glfw.x11.joystick[joy].numButtons < numButtons)
numButtons = _glfw.x11.joystick[joy].numButtons;
for (i = 0; i < numButtons; i++)
buttons[i] = _glfw.x11.joystick[joy].button[i];
return numButtons;
*count = _glfw.x11.joystick[joy].buttonCount;
return _glfw.x11.joystick[joy].buttons;
}
const char* _glfwPlatformGetJoystickName(int joy)
{
if (!_glfw.x11.joystick[joy].present)
return NULL;
pollJoystickEvents();
return _glfw.x11.joystick[joy].name;
}