Added Linux joystick hot-plugging via inotify.

This is a temporary solution until the proper libudev one.

Progresses #17.
This commit is contained in:
Camilla Berglund 2013-10-16 17:24:37 +02:00
parent b19fb4c24d
commit 56202ee7c6
5 changed files with 116 additions and 44 deletions

View File

@ -148,6 +148,8 @@ GLFW bundles a number of dependencies in the `deps/` directory.
- [X11] Bugfix: Full screen override redirect windows were not always - [X11] Bugfix: Full screen override redirect windows were not always
positioned over the specified monitor positioned over the specified monitor
- [X11] Bugfix: Character input did not work for the default `"C"` locale - [X11] Bugfix: Character input did not work for the default `"C"` locale
- [X11] Bugfix: Joysticks connected after `glfwInit` were not detected
(temporary inotify solution until proper libudev solution)
## Contact ## Contact

View File

@ -27,14 +27,14 @@
#include "internal.h" #include "internal.h"
#ifdef __linux__ #if defined(__linux__)
#include <linux/joystick.h> #include <linux/joystick.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/inotify.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <regex.h>
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -45,16 +45,34 @@
// Attempt to open the specified joystick device // Attempt to open the specified joystick device
// //
static int openJoystickDevice(int joy, const char* path) static void openJoystickDevice(const char* path)
{ {
#ifdef __linux__ #if defined(__linux__)
char axisCount, buttonCount; char axisCount, buttonCount;
char name[256]; char name[256];
int fd, version; int joy, fd, version;
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{
if (!_glfw.linux_js[joy].present)
continue;
if (strcmp(_glfw.linux_js[joy].path, path) == 0)
return;
}
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{
if (!_glfw.linux_js[joy].present)
break;
}
if (joy > GLFW_JOYSTICK_LAST)
return;
fd = open(path, O_RDONLY | O_NONBLOCK); fd = open(path, O_RDONLY | O_NONBLOCK);
if (fd == -1) if (fd == -1)
return GL_FALSE; return;
_glfw.linux_js[joy].fd = fd; _glfw.linux_js[joy].fd = fd;
@ -64,13 +82,14 @@ static int openJoystickDevice(int joy, const char* path)
{ {
// It's an old 0.x interface (we don't support it) // It's an old 0.x interface (we don't support it)
close(fd); close(fd);
return GL_FALSE; return;
} }
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[joy].name = strdup(name); _glfw.linux_js[joy].name = strdup(name);
_glfw.linux_js[joy].path = strdup(path);
ioctl(fd, JSIOCGAXES, &axisCount); ioctl(fd, JSIOCGAXES, &axisCount);
_glfw.linux_js[joy].axisCount = (int) axisCount; _glfw.linux_js[joy].axisCount = (int) axisCount;
@ -83,17 +102,34 @@ static int openJoystickDevice(int joy, const char* path)
_glfw.linux_js[joy].present = GL_TRUE; _glfw.linux_js[joy].present = GL_TRUE;
#endif // __linux__ #endif // __linux__
return GL_TRUE;
} }
// Polls for and processes events for all present joysticks // Polls for and processes events for all present joysticks
// //
static void pollJoystickEvents(void) static void pollJoystickEvents(void)
{ {
#ifdef __linux__ #if defined(__linux__)
int i; int i;
struct js_event e; struct js_event e;
ssize_t offset = 0;
char buffer[16384];
const ssize_t size = read(_glfw.x11.inotify.fd, buffer, sizeof(buffer));
while (size > offset)
{
regmatch_t match;
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
if (regexec(&_glfw.x11.inotify.regex, e->name, 1, &match, 0) == 0)
{
char path[20];
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
openJoystickDevice(path);
}
offset += sizeof(struct inotify_event) + e->len;
}
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
{ {
@ -113,6 +149,7 @@ static void pollJoystickEvents(void)
free(_glfw.linux_js[i].axes); free(_glfw.linux_js[i].axes);
free(_glfw.linux_js[i].buttons); free(_glfw.linux_js[i].buttons);
free(_glfw.linux_js[i].name); free(_glfw.linux_js[i].name);
free(_glfw.linux_js[i].path);
memset(&_glfw.linux_js[i], 0, sizeof(_glfw.linux_js[i])); memset(&_glfw.linux_js[i], 0, sizeof(_glfw.linux_js[i]));
} }
@ -150,58 +187,69 @@ static void pollJoystickEvents(void)
// Initialize joystick interface // Initialize joystick interface
// //
void _glfwInitJoysticks(void) int _glfwInitJoysticks(void)
{ {
#ifdef __linux__ #if defined(__linux__)
int joy = 0; const char* dirname = "/dev/input";
size_t i;
regex_t regex;
DIR* dir; DIR* dir;
const char* dirs[] = struct dirent* entry;
{
"/dev/input",
"/dev"
};
if (regcomp(&regex, "^js[0-9]\\+$", 0) != 0) _glfw.x11.inotify.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (_glfw.x11.inotify.fd == -1)
{
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to initialize inotify");
return GL_FALSE;
}
// HACK: Register for IN_ATTRIB as well to get notified when udev is done
// This works well in practice but the true way is libudev
_glfw.x11.inotify.wd = inotify_add_watch(_glfw.x11.inotify.fd,
dirname,
IN_CREATE | IN_ATTRIB);
if (_glfw.x11.inotify.wd == -1)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"X11: Failed to add watch to %s", dirname);
return GL_FALSE;
}
if (regcomp(&_glfw.x11.inotify.regex, "^js[0-9]\\+$", 0) != 0)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to compile regex"); _glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to compile regex");
return; return GL_FALSE;
} }
for (i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++) dir = opendir(dirname);
if (!dir)
{ {
struct dirent* entry; _glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to open %s", dirname);
return GL_FALSE;
}
dir = opendir(dirs[i]); while ((entry = readdir(dir)))
if (!dir) {
char path[20];
regmatch_t match;
if (regexec(&_glfw.x11.inotify.regex, entry->d_name, 1, &match, 0) != 0)
continue; continue;
while ((entry = readdir(dir))) snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
{ openJoystickDevice(path);
char path[20];
regmatch_t match;
if (regexec(&regex, entry->d_name, 1, &match, 0) != 0)
continue;
snprintf(path, sizeof(path), "%s/%s", dirs[i], entry->d_name);
if (openJoystickDevice(joy, path))
joy++;
}
closedir(dir);
} }
regfree(&regex); closedir(dir);
#endif // __linux__ #endif // __linux__
return GL_TRUE;
} }
// Close all opened joystick handles // Close all opened joystick handles
// //
void _glfwTerminateJoysticks(void) void _glfwTerminateJoysticks(void)
{ {
#ifdef __linux__ #if defined(__linux__)
int i; int i;
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
@ -212,8 +260,17 @@ void _glfwTerminateJoysticks(void)
free(_glfw.linux_js[i].axes); free(_glfw.linux_js[i].axes);
free(_glfw.linux_js[i].buttons); free(_glfw.linux_js[i].buttons);
free(_glfw.linux_js[i].name); free(_glfw.linux_js[i].name);
free(_glfw.linux_js[i].path);
} }
} }
regfree(&_glfw.x11.inotify.regex);
if (_glfw.x11.inotify.wd > 0)
close(_glfw.x11.inotify.wd);
if (_glfw.x11.inotify.fd > 0)
close(_glfw.x11.inotify.fd);
#endif // __linux__ #endif // __linux__
} }

View File

@ -42,10 +42,11 @@ typedef struct _GLFWjoystickLinux
unsigned char* buttons; unsigned char* buttons;
int buttonCount; int buttonCount;
char* name; char* name;
char* path;
} _GLFWjoystickLinux; } _GLFWjoystickLinux;
void _glfwInitJoysticks(void); int _glfwInitJoysticks(void);
void _glfwTerminateJoysticks(void); void _glfwTerminateJoysticks(void);
#endif // _linux_joystick_h_ #endif // _linux_joystick_h_

View File

@ -749,8 +749,10 @@ int _glfwPlatformInit(void)
if (!_glfwInitContextAPI()) if (!_glfwInitContextAPI())
return GL_FALSE; return GL_FALSE;
if (!_glfwInitJoysticks())
return GL_FALSE;
_glfwInitTimer(); _glfwInitTimer();
_glfwInitJoysticks();
return GL_TRUE; return GL_TRUE;
} }

View File

@ -31,6 +31,8 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
#include <regex.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
@ -214,6 +216,14 @@ typedef struct _GLFWlibraryX11
int versionMinor; int versionMinor;
} xinerama; } xinerama;
#if defined(__linux__)
struct {
int fd;
int wd;
regex_t regex;
} inotify;
#endif
} _GLFWlibraryX11; } _GLFWlibraryX11;