From 06b30d1cc3d4ff823f6baa3bf5612198bbef55b7 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Thu, 23 Jul 2020 13:52:28 +0200 Subject: [PATCH] GLFW: Cleanup From upstream: https://github.com/glfw/glfw/commit/f771d412925f64c351167e7a1e6a7d35770d3ecf. --- glfw/cocoa_joystick.m | 10 +---- glfw/linux_joystick.c | 91 ++++++++++++++++++++----------------------- 2 files changed, 44 insertions(+), 57 deletions(-) diff --git a/glfw/cocoa_joystick.m b/glfw/cocoa_joystick.m index 5049b6ebf..25588af45 100644 --- a/glfw/cocoa_joystick.m +++ b/glfw/cocoa_joystick.m @@ -291,11 +291,9 @@ static void removeCallback(void* context UNUSED, ////////////////////////////////////////////////////////////////////////// -////// GLFW internal API ////// +////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// -// Initialize joystick interface -// bool _glfwPlatformInitJoysticks(void) { CFMutableArrayRef matching; @@ -373,8 +371,6 @@ bool _glfwPlatformInitJoysticks(void) return true; } -// Close all opened joystick handles -// void _glfwPlatformTerminateJoysticks(void) { int jid; @@ -390,10 +386,6 @@ void _glfwPlatformTerminateJoysticks(void) } -////////////////////////////////////////////////////////////////////////// -////// GLFW platform API ////// -////////////////////////////////////////////////////////////////////////// - int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode) { if (mode & _GLFW_POLL_AXES) diff --git a/glfw/linux_joystick.c b/glfw/linux_joystick.c index 0cde579ae..09952c6d3 100644 --- a/glfw/linux_joystick.c +++ b/glfw/linux_joystick.c @@ -264,8 +264,49 @@ static int compareJoysticks(const void* fp, const void* sp) ////// GLFW internal API ////// ////////////////////////////////////////////////////////////////////////// -// Initialize joystick interface -// +void _glfwDetectJoystickConnectionLinux(void) +{ + if (_glfw.linjs.inotify <= 0) + return; + + ssize_t offset = 0; + char buffer[16384]; + const ssize_t size = read(_glfw.linjs.inotify, buffer, sizeof(buffer)); + + while (size > offset) + { + regmatch_t match; + const struct inotify_event* e = (struct inotify_event*) (buffer + offset); + + offset += sizeof(struct inotify_event) + e->len; + + if (regexec(&_glfw.linjs.regex, e->name, 1, &match, 0) != 0) + continue; + + char path[PATH_MAX]; + snprintf(path, sizeof(path), "/dev/input/%s", e->name); + + if (e->mask & (IN_CREATE | IN_ATTRIB)) + openJoystickDevice(path); + else if (e->mask & IN_DELETE) + { + for (int jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++) + { + if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0) + { + closeJoystick(_glfw.joysticks + jid); + break; + } + } + } + } +} + + +////////////////////////////////////////////////////////////////////////// +////// GLFW platform API ////// +////////////////////////////////////////////////////////////////////////// + bool _glfwPlatformInitJoysticks(void) { const char* dirname = "/dev/input"; @@ -320,8 +361,6 @@ bool _glfwPlatformInitJoysticks(void) return true; } -// Close all opened joystick handles -// void _glfwPlatformTerminateJoysticks(void) { int jid; @@ -343,50 +382,6 @@ void _glfwPlatformTerminateJoysticks(void) } } -void _glfwDetectJoystickConnectionLinux(void) -{ - - if (_glfw.linjs.inotify <= 0) - return; - - ssize_t offset = 0; - char buffer[16384]; - const ssize_t size = read(_glfw.linjs.inotify, buffer, sizeof(buffer)); - - while (size > offset) - { - regmatch_t match; - const struct inotify_event* e = (struct inotify_event*) (buffer + offset); - - offset += sizeof(struct inotify_event) + e->len; - - if (regexec(&_glfw.linjs.regex, e->name, 1, &match, 0) != 0) - continue; - - char path[PATH_MAX]; - snprintf(path, sizeof(path), "/dev/input/%s", e->name); - - if (e->mask & (IN_CREATE | IN_ATTRIB)) - openJoystickDevice(path); - else if (e->mask & IN_DELETE) - { - for (int jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++) - { - if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0) - { - closeJoystick(_glfw.joysticks + jid); - break; - } - } - } - } -} - - -////////////////////////////////////////////////////////////////////////// -////// GLFW platform API ////// -////////////////////////////////////////////////////////////////////////// - int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode UNUSED) { // Read all queued events (non-blocking)