From 53245d754e350fc96370e6d28b7678c439a857c2 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Tue, 28 Aug 2012 20:16:43 +0200 Subject: [PATCH] Added detection of joystick disconnect on X11. --- src/x11_joystick.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/x11_joystick.c b/src/x11_joystick.c index 8492850b..7f4fdbc1 100644 --- a/src/x11_joystick.c +++ b/src/x11_joystick.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -109,6 +110,7 @@ static void pollJoystickEvents(void) { #ifdef _GLFW_USE_LINUX_JOYSTICKS int i; + ssize_t result; struct js_event e; for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) @@ -117,8 +119,17 @@ static void pollJoystickEvents(void) continue; // 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 e.type &= ~JS_EVENT_INIT; @@ -221,6 +232,8 @@ void _glfwTerminateJoysticks(void) int _glfwPlatformGetJoystickParam(int joy, int param) { + pollJoystickEvents(); + if (!_glfwLibrary.X11.joystick[joy].present) return 0; @@ -251,11 +264,11 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numAxes) { int i; + pollJoystickEvents(); + if (!_glfwLibrary.X11.joystick[joy].present) return 0; - pollJoystickEvents(); - if (_glfwLibrary.X11.joystick[joy].numAxes < numAxes) numAxes = _glfwLibrary.X11.joystick[joy].numAxes; @@ -275,11 +288,11 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, { int i; + pollJoystickEvents(); + if (!_glfwLibrary.X11.joystick[joy].present) return 0; - pollJoystickEvents(); - if (_glfwLibrary.X11.joystick[joy].numButtons < numButtons) numButtons = _glfwLibrary.X11.joystick[joy].numButtons;