Added detection of joystick disconnect on X11.

This commit is contained in:
Camilla Berglund 2012-08-28 20:16:43 +02:00
parent e10d935efe
commit 53245d754e

View File

@ -36,6 +36,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -109,6 +110,7 @@ static void pollJoystickEvents(void)
{ {
#ifdef _GLFW_USE_LINUX_JOYSTICKS #ifdef _GLFW_USE_LINUX_JOYSTICKS
int i; int i;
ssize_t result;
struct js_event e; struct js_event e;
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
@ -117,8 +119,17 @@ static void pollJoystickEvents(void)
continue; continue;
// Read all queued events (non-blocking) // Read all queued events (non-blocking)
while (read(_glfwLibrary.X11.joystick[i].fd, &e, sizeof(e)) > 0) for (;;)
{ {
errno = 0;
result = read(_glfwLibrary.X11.joystick[i].fd, &e, sizeof(e));
if (errno == ENODEV)
_glfwLibrary.X11.joystick[i].present = GL_FALSE;
if (result <= 0)
break;
// We don't care if it's an init event or not // We don't care if it's an init event or not
e.type &= ~JS_EVENT_INIT; e.type &= ~JS_EVENT_INIT;
@ -221,6 +232,8 @@ void _glfwTerminateJoysticks(void)
int _glfwPlatformGetJoystickParam(int joy, int param) int _glfwPlatformGetJoystickParam(int joy, int param)
{ {
pollJoystickEvents();
if (!_glfwLibrary.X11.joystick[joy].present) if (!_glfwLibrary.X11.joystick[joy].present)
return 0; return 0;
@ -251,11 +264,11 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numAxes)
{ {
int i; int i;
pollJoystickEvents();
if (!_glfwLibrary.X11.joystick[joy].present) if (!_glfwLibrary.X11.joystick[joy].present)
return 0; return 0;
pollJoystickEvents();
if (_glfwLibrary.X11.joystick[joy].numAxes < numAxes) if (_glfwLibrary.X11.joystick[joy].numAxes < numAxes)
numAxes = _glfwLibrary.X11.joystick[joy].numAxes; numAxes = _glfwLibrary.X11.joystick[joy].numAxes;
@ -275,11 +288,11 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
{ {
int i; int i;
pollJoystickEvents();
if (!_glfwLibrary.X11.joystick[joy].present) if (!_glfwLibrary.X11.joystick[joy].present)
return 0; return 0;
pollJoystickEvents();
if (_glfwLibrary.X11.joystick[joy].numButtons < numButtons) if (_glfwLibrary.X11.joystick[joy].numButtons < numButtons)
numButtons = _glfwLibrary.X11.joystick[joy].numButtons; numButtons = _glfwLibrary.X11.joystick[joy].numButtons;