Only poll requested joystick on Linux

This also performs minor cleanup.
This commit is contained in:
Camilla Berglund 2015-12-10 14:07:42 +01:00
parent ee27930628
commit 8f0f1cf6c1
2 changed files with 72 additions and 77 deletions

View File

@ -51,6 +51,7 @@ static void openJoystickDevice(const char* path)
char axisCount, buttonCount; char axisCount, buttonCount;
char name[256]; char name[256];
int joy, fd, version; int joy, fd, version;
_GLFWjoystickLinux* js;
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
@ -74,8 +75,6 @@ static void openJoystickDevice(const char* path)
if (fd == -1) if (fd == -1)
return; return;
_glfw.linux_js.js[joy].fd = fd;
// Verify that the joystick driver version is at least 1.0 // Verify that the joystick driver version is at least 1.0
ioctl(fd, JSIOCGVERSION, &version); ioctl(fd, JSIOCGVERSION, &version);
if (version < 0x010000) if (version < 0x010000)
@ -88,29 +87,28 @@ static void openJoystickDevice(const char* path)
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0) if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
strncpy(name, "Unknown", sizeof(name)); strncpy(name, "Unknown", sizeof(name));
_glfw.linux_js.js[joy].name = strdup(name); js = _glfw.linux_js.js + joy;
_glfw.linux_js.js[joy].path = strdup(path); js->name = strdup(name);
js->path = strdup(path);
js->fd = fd;
ioctl(fd, JSIOCGAXES, &axisCount); ioctl(fd, JSIOCGAXES, &axisCount);
_glfw.linux_js.js[joy].axisCount = (int) axisCount; js->axisCount = (int) axisCount;
js->axes = calloc(axisCount, sizeof(float));
ioctl(fd, JSIOCGBUTTONS, &buttonCount); ioctl(fd, JSIOCGBUTTONS, &buttonCount);
_glfw.linux_js.js[joy].buttonCount = (int) buttonCount; js->buttonCount = (int) buttonCount;
js->buttons = calloc(buttonCount, 1);
_glfw.linux_js.js[joy].axes = calloc(axisCount, sizeof(float)); js->present = GLFW_TRUE;
_glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1);
_glfw.linux_js.js[joy].present = GLFW_TRUE;
#endif // __linux__ #endif // __linux__
} }
// Polls for and processes events for all present joysticks // Polls for and processes events the specified joystick
// //
static void pollJoystickEvents(void) static GLFWbool pollJoystickEvents(_GLFWjoystickLinux* js)
{ {
#if defined(__linux__) #if defined(__linux__)
int i;
struct js_event e;
ssize_t offset = 0; ssize_t offset = 0;
char buffer[16384]; char buffer[16384];
@ -131,53 +129,41 @@ static void pollJoystickEvents(void)
offset += sizeof(struct inotify_event) + e->len; offset += sizeof(struct inotify_event) + e->len;
} }
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) if (!js->present)
{ return GLFW_FALSE;
if (!_glfw.linux_js.js[i].present)
continue;
// Read all queued events (non-blocking) // Read all queued events (non-blocking)
for (;;) for (;;)
{ {
struct js_event e;
errno = 0; errno = 0;
if (read(_glfw.linux_js.js[i].fd, &e, sizeof(e)) < 0) if (read(js->fd, &e, sizeof(e)) < 0)
{ {
// Reset the joystick slot if the device was disconnected
if (errno == ENODEV) if (errno == ENODEV)
{ {
// The joystick was disconnected free(js->axes);
free(js->buttons);
free(js->name);
free(js->path);
free(_glfw.linux_js.js[i].axes); memset(js, 0, sizeof(_GLFWjoystickLinux));
free(_glfw.linux_js.js[i].buttons);
free(_glfw.linux_js.js[i].name);
free(_glfw.linux_js.js[i].path);
memset(&_glfw.linux_js.js[i], 0, sizeof(_glfw.linux_js.js[i]));
} }
break; break;
} }
// We don't care if it's an init event or not // Clear the initial-state bit
e.type &= ~JS_EVENT_INIT; e.type &= ~JS_EVENT_INIT;
switch (e.type) if (e.type == JS_EVENT_AXIS)
{ js->axes[e.number] = (float) e.value / 32767.0f;
case JS_EVENT_AXIS: else if (e.type == JS_EVENT_BUTTON)
_glfw.linux_js.js[i].axes[e.number] = js->buttons[e.number] = e.value ? GLFW_PRESS : GLFW_RELEASE;
(float) e.value / 32767.0f;
break;
case JS_EVENT_BUTTON:
_glfw.linux_js.js[i].buttons[e.number] =
e.value ? GLFW_PRESS : GLFW_RELEASE;
break;
default:
break;
}
}
} }
#endif // __linux__ #endif // __linux__
return js->present;
} }
@ -294,31 +280,36 @@ void _glfwTerminateJoysticks(void)
int _glfwPlatformJoystickPresent(int joy) int _glfwPlatformJoystickPresent(int joy)
{ {
pollJoystickEvents(); _GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
return pollJoystickEvents(js);
return _glfw.linux_js.js[joy].present;
} }
const float* _glfwPlatformGetJoystickAxes(int joy, int* count) const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{ {
pollJoystickEvents(); _GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
if (!pollJoystickEvents(js))
return NULL;
*count = _glfw.linux_js.js[joy].axisCount; *count = js->axisCount;
return _glfw.linux_js.js[joy].axes; return js->axes;
} }
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count) const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{ {
pollJoystickEvents(); _GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
if (!pollJoystickEvents(js))
return NULL;
*count = _glfw.linux_js.js[joy].buttonCount; *count = js->buttonCount;
return _glfw.linux_js.js[joy].buttons; return js->buttons;
} }
const char* _glfwPlatformGetJoystickName(int joy) const char* _glfwPlatformGetJoystickName(int joy)
{ {
pollJoystickEvents(); _GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
if (!pollJoystickEvents(js))
return NULL;
return _glfw.linux_js.js[joy].name; return js->name;
} }

View File

@ -29,16 +29,13 @@
#include <regex.h> #include <regex.h>
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \ #define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE _GLFWjoylistLinux linux_js
_GLFWjoystickLinux linux_js
// Linux-specific joystick API data // Linux-specific joystick data
// //
typedef struct _GLFWjoystickLinux typedef struct _GLFWjoystickLinux
{ {
struct
{
int present; int present;
int fd; int fd;
float* axes; float* axes;
@ -47,14 +44,21 @@ typedef struct _GLFWjoystickLinux
int buttonCount; int buttonCount;
char* name; char* name;
char* path; char* path;
} js[GLFW_JOYSTICK_LAST + 1]; } _GLFWjoystickLinux;
// Linux-specific joystick API data
//
typedef struct _GLFWjoylistLinux
{
_GLFWjoystickLinux js[GLFW_JOYSTICK_LAST + 1];
#if defined(__linux__) #if defined(__linux__)
int inotify; int inotify;
int watch; int watch;
regex_t regex; regex_t regex;
#endif /*__linux__*/ #endif /*__linux__*/
} _GLFWjoystickLinux; } _GLFWjoylistLinux;
int _glfwInitJoysticks(void); int _glfwInitJoysticks(void);