From 496c7000425ea2133a109388ed7f45b1da3acc92 Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Sun, 22 Aug 2021 08:18:50 -0400 Subject: [PATCH] Enable joystick support on FreeBSD using the Linux driver. This is arguably incomplete; it supports joysticks via webcamd (which uses Linux drivers running in userspace), but not via uhidd (which is FreeBSD's own thing). Nevertheless a strict improvement. Tested using a gamepad I have that is supported by the "xpad" Linux driver. Note: adds build dependency on devel/evdev-proto and devel/libinotify. To actually use it, need to install multimedia/webcamd at runtime (if not installed or not running, no joysticks will be detected). Fixes #1892. --- README.md | 4 ++++ src/CMakeLists.txt | 6 ++++++ src/linux_joystick.c | 2 +- src/linux_joystick.h | 4 ++++ src/wl_platform.h | 2 +- src/x11_init.c | 2 +- src/x11_platform.h | 2 +- src/x11_window.c | 6 +++--- 8 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0f5c2b9d..7152c3d4 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,10 @@ in the documentation for more information. GLFW itself needs only CMake 3.1 or later and the headers and libraries for your OS and window system. +As an exception, on FreeBSD, the `devel/evdev-proto` and `devel/libinotify` +ports need to be installed; furthermore, to actually use gamepads/joysticks, +`multimedia/webcamd` needs to be installed and configured. + The examples and test programs depend on a number of tiny libraries. These are located in the `deps/` directory. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea16f195..68a6e646 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,6 +49,12 @@ endif() if (_GLFW_X11 OR _GLFW_WAYLAND) if (CMAKE_SYSTEM_NAME STREQUAL "Linux") target_sources(glfw PRIVATE linux_joystick.h linux_joystick.c) + elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") + target_sources(glfw PRIVATE linux_joystick.h linux_joystick.c) + pkg_check_modules(LIBINOTIFY REQUIRED libinotify) + target_link_libraries(glfw PRIVATE ${LIBINOTIFY_LINK_LIBRARIES}) + target_include_directories(glfw PRIVATE ${LIBINOTIFY_INCLUDE_DIRS}) + target_compile_options(glfw PRIVATE ${LIBINOTIFY_CFLAGS_OTHER}) else() target_sources(glfw PRIVATE null_joystick.h null_joystick.c) endif() diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 122bc66a..5e64a6d6 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -324,7 +324,7 @@ GLFWbool _glfwPlatformInitJoysticks(void) // Continue without device connection notifications if inotify fails - if (regcomp(&_glfw.linjs.regex, "^event[0-9]\\+$", 0) != 0) + if (regcomp(&_glfw.linjs.regex, "^event[0-9][0-9]*$", 0) != 0) { _glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex"); return GLFW_FALSE; diff --git a/src/linux_joystick.h b/src/linux_joystick.h index 1c3ca751..c4e2fa0e 100644 --- a/src/linux_joystick.h +++ b/src/linux_joystick.h @@ -25,7 +25,11 @@ //======================================================================== #include +#ifdef __linux__ #include +#else +#include +#endif #include #define _GLFW_PLATFORM_JOYSTICK_STATE _GLFWjoystickLinux linjs diff --git a/src/wl_platform.h b/src/wl_platform.h index a24943c9..564cbcce 100644 --- a/src/wl_platform.h +++ b/src/wl_platform.h @@ -47,7 +47,7 @@ typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR #include "posix_thread.h" #include "posix_time.h" -#ifdef __linux__ +#if defined(__linux__) || defined(__FreeBSD__) #include "linux_joystick.h" #else #include "null_joystick.h" diff --git a/src/x11_init.c b/src/x11_init.c index 6287514b..128ce884 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -1487,7 +1487,7 @@ const char* _glfwPlatformGetVersionString(void) #if defined(_POSIX_MONOTONIC_CLOCK) " monotonic" #endif -#if defined(__linux__) +#if defined(__linux__) || defined(__FreeBSD__) " evdev" #endif #if defined(_GLFW_BUILD_DLL) diff --git a/src/x11_platform.h b/src/x11_platform.h index c5137bc3..4a8043ed 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -378,7 +378,7 @@ typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)(Vk #include "posix_time.h" #include "xkb_unicode.h" #include "glx_context.h" -#if defined(__linux__) +#if defined(__linux__) || defined(__FreeBSD__) #include "linux_joystick.h" #else #include "null_joystick.h" diff --git a/src/x11_window.c b/src/x11_window.c index a360c15c..7d6c5012 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -67,7 +67,7 @@ static GLFWbool waitForEvent(double* timeout) const int fd = ConnectionNumber(_glfw.x11.display); int count = fd + 1; -#if defined(__linux__) +#if defined(__linux__) || defined(__FreeBSD__) if (_glfw.linjs.inotify > fd) count = _glfw.linjs.inotify + 1; #endif @@ -75,7 +75,7 @@ static GLFWbool waitForEvent(double* timeout) { FD_ZERO(&fds); FD_SET(fd, &fds); -#if defined(__linux__) +#if defined(__linux__) || defined(__FreeBSD__) if (_glfw.linjs.inotify > 0) FD_SET(_glfw.linjs.inotify, &fds); #endif @@ -2779,7 +2779,7 @@ void _glfwPlatformPollEvents(void) { _GLFWwindow* window; -#if defined(__linux__) +#if defined(__linux__) || defined(__FreeBSD__) if (_glfw.joysticksInitialized) _glfwDetectJoystickConnectionLinux(); #endif