1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-27 04:28:52 -05:00

Improved robustness of Linux joystick init.

This commit is contained in:
Camilla Berglund 2015-01-06 19:22:19 +01:00
parent 0e3cb945e8
commit 6c7509c942

View File

@ -192,12 +192,13 @@ int _glfwInitJoysticks(void)
#if defined(__linux__) #if defined(__linux__)
const char* dirname = "/dev/input"; const char* dirname = "/dev/input";
DIR* dir; DIR* dir;
struct dirent* entry;
_glfw.linux_js.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); _glfw.linux_js.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (_glfw.linux_js.inotify == -1) if (_glfw.linux_js.inotify == -1)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to initialize inotify"); _glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to initialize inotify: %s",
strerror(errno));
return GL_FALSE; return GL_FALSE;
} }
@ -210,8 +211,10 @@ int _glfwInitJoysticks(void)
if (_glfw.linux_js.watch == -1) if (_glfw.linux_js.watch == -1)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to add watch to %s", dirname); "Linux: Failed to watch for joystick connections in %s: %s",
return GL_FALSE; dirname,
strerror(errno));
// Continue without device connection notifications
} }
if (regcomp(&_glfw.linux_js.regex, "^js[0-9]\\+$", 0) != 0) if (regcomp(&_glfw.linux_js.regex, "^js[0-9]\\+$", 0) != 0)
@ -221,25 +224,33 @@ int _glfwInitJoysticks(void)
} }
dir = opendir(dirname); dir = opendir(dirname);
if (!dir) if (dir)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to open %s", dirname); struct dirent* entry;
return GL_FALSE;
while ((entry = readdir(dir)))
{
char path[20];
regmatch_t match;
if (regexec(&_glfw.linux_js.regex, entry->d_name, 1, &match, 0) != 0)
continue;
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
openJoystickDevice(path);
}
closedir(dir);
}
else
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to open joystick device directory %s: %s",
dirname,
strerror(errno));
// Continue with no joysticks detected
} }
while ((entry = readdir(dir)))
{
char path[20];
regmatch_t match;
if (regexec(&_glfw.linux_js.regex, entry->d_name, 1, &match, 0) != 0)
continue;
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
openJoystickDevice(path);
}
closedir(dir);
#endif // __linux__ #endif // __linux__
return GL_TRUE; return GL_TRUE;