Add null joystick backend

This prepares the X11 backend to support other joystick APIs, for
example the FreeBSD libusb one.
This commit is contained in:
Camilla Löwy 2017-01-31 03:29:28 +01:00
parent c5694b3013
commit 67a55efa27
13 changed files with 103 additions and 38 deletions

View File

@ -10,6 +10,10 @@ if (NOT CMAKE_VERSION VERSION_LESS "3.0")
cmake_policy(SET CMP0042 OLD) cmake_policy(SET CMP0042 OLD)
endif() endif()
if (NOT CMAKE_VERSION VERSION_LESS "3.1")
cmake_policy(SET CMP0054 NEW)
endif()
set(GLFW_VERSION_MAJOR "3") set(GLFW_VERSION_MAJOR "3")
set(GLFW_VERSION_MINOR "3") set(GLFW_VERSION_MINOR "3")
set(GLFW_VERSION_PATCH "0") set(GLFW_VERSION_PATCH "0")

View File

@ -18,12 +18,19 @@ elseif (_GLFW_WIN32)
win32_monitor.c win32_time.c win32_tls.c win32_window.c win32_monitor.c win32_time.c win32_tls.c win32_window.c
wgl_context.c egl_context.c) wgl_context.c egl_context.c)
elseif (_GLFW_X11) elseif (_GLFW_X11)
set(glfw_HEADERS ${common_HEADERS} x11_platform.h xkb_unicode.h set(glfw_HEADERS ${common_HEADERS} x11_platform.h xkb_unicode.h posix_time.h
linux_joystick.h posix_time.h posix_tls.h glx_context.h posix_tls.h glx_context.h egl_context.h)
egl_context.h)
set(glfw_SOURCES ${common_SOURCES} x11_init.c x11_monitor.c x11_window.c set(glfw_SOURCES ${common_SOURCES} x11_init.c x11_monitor.c x11_window.c
xkb_unicode.c linux_joystick.c posix_time.c posix_tls.c xkb_unicode.c posix_time.c posix_tls.c glx_context.c
glx_context.c egl_context.c) egl_context.c)
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(glfw_HEADERS ${glfw_HEADERS} linux_joystick.h)
set(glfw_SOURCES ${glfw_SOURCES} linux_joystick.c)
else()
set(glfw_HEADERS ${glfw_HEADERS} null_joystick.h)
set(glfw_SOURCES ${glfw_SOURCES} null_joystick.c)
endif()
elseif (_GLFW_WAYLAND) elseif (_GLFW_WAYLAND)
set(glfw_HEADERS ${common_HEADERS} wl_platform.h linux_joystick.h set(glfw_HEADERS ${common_HEADERS} wl_platform.h linux_joystick.h
posix_time.h posix_tls.h xkb_unicode.h egl_context.h) posix_time.h posix_tls.h xkb_unicode.h egl_context.h)
@ -46,10 +53,10 @@ elseif (_GLFW_MIR)
linux_joystick.c posix_time.c posix_tls.c xkb_unicode.c linux_joystick.c posix_time.c posix_tls.c xkb_unicode.c
egl_context.c) egl_context.c)
elseif (_GLFW_OSMESA) elseif (_GLFW_OSMESA)
set(glfw_HEADERS ${common_HEADERS} osmesa_platform.h set(glfw_HEADERS ${common_HEADERS} osmesa_platform.h null_joystick.h
posix_time.h posix_tls.h osmesa_context.h) posix_time.h posix_tls.h osmesa_context.h)
set(glfw_SOURCES ${common_SOURCES} osmesa_init.c osmesa_monitor.c set(glfw_SOURCES ${common_SOURCES} osmesa_init.c osmesa_monitor.c
osmesa_window.c posix_time.c posix_tls.c osmesa_window.c null_joystick.c posix_time.c posix_tls.c
osmesa_context.c) osmesa_context.c)
endif() endif()

View File

@ -27,7 +27,6 @@
#include "internal.h" #include "internal.h"
#if defined(__linux__)
#include <linux/joystick.h> #include <linux/joystick.h>
#include <sys/types.h> #include <sys/types.h>
@ -40,12 +39,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#endif // __linux__
// Attempt to open the specified joystick device // Attempt to open the specified joystick device
// //
#if defined(__linux__)
static GLFWbool openJoystickDevice(const char* path) static GLFWbool openJoystickDevice(const char* path)
{ {
char axisCount, buttonCount; char axisCount, buttonCount;
@ -93,11 +90,9 @@ static GLFWbool openJoystickDevice(const char* path)
_glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED); _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED);
return GLFW_TRUE; return GLFW_TRUE;
} }
#endif // __linux__
// Frees all resources associated with the specified joystick // Frees all resources associated with the specified joystick
// //
#if defined(__linux__)
static void closeJoystick(_GLFWjoystick* js) static void closeJoystick(_GLFWjoystick* js)
{ {
close(js->linjs.fd); close(js->linjs.fd);
@ -105,18 +100,15 @@ static void closeJoystick(_GLFWjoystick* js)
_glfwFreeJoystick(js); _glfwFreeJoystick(js);
_glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED); _glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED);
} }
#endif // __linux__
// Lexically compare joysticks by name; used by qsort // Lexically compare joysticks by name; used by qsort
// //
#if defined(__linux__)
static int compareJoysticks(const void* fp, const void* sp) static int compareJoysticks(const void* fp, const void* sp)
{ {
const _GLFWjoystick* fj = fp; const _GLFWjoystick* fj = fp;
const _GLFWjoystick* sj = sp; const _GLFWjoystick* sj = sp;
return strcmp(fj->linjs.path, sj->linjs.path); return strcmp(fj->linjs.path, sj->linjs.path);
} }
#endif // __linux__
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -127,7 +119,6 @@ static int compareJoysticks(const void* fp, const void* sp)
// //
GLFWbool _glfwInitJoysticksLinux(void) GLFWbool _glfwInitJoysticksLinux(void)
{ {
#if defined(__linux__)
DIR* dir; DIR* dir;
int count = 0; int count = 0;
const char* dirname = "/dev/input"; const char* dirname = "/dev/input";
@ -192,8 +183,6 @@ GLFWbool _glfwInitJoysticksLinux(void)
} }
qsort(_glfw.joysticks, count, sizeof(_GLFWjoystick), compareJoysticks); qsort(_glfw.joysticks, count, sizeof(_GLFWjoystick), compareJoysticks);
#endif // __linux__
return GLFW_TRUE; return GLFW_TRUE;
} }
@ -201,7 +190,6 @@ GLFWbool _glfwInitJoysticksLinux(void)
// //
void _glfwTerminateJoysticksLinux(void) void _glfwTerminateJoysticksLinux(void)
{ {
#if defined(__linux__)
int jid; int jid;
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++) for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
@ -220,12 +208,10 @@ void _glfwTerminateJoysticksLinux(void)
close(_glfw.linjs.inotify); close(_glfw.linjs.inotify);
} }
#endif // __linux__
} }
void _glfwDetectJoystickConnectionLinux(void) void _glfwDetectJoystickConnectionLinux(void)
{ {
#if defined(__linux__)
ssize_t offset = 0; ssize_t offset = 0;
char buffer[16384]; char buffer[16384];
@ -260,7 +246,6 @@ void _glfwDetectJoystickConnectionLinux(void)
offset += sizeof(struct inotify_event) + e->len; offset += sizeof(struct inotify_event) + e->len;
} }
#endif
} }
@ -270,7 +255,6 @@ void _glfwDetectJoystickConnectionLinux(void)
int _glfwPlatformPollJoystick(int jid, int mode) int _glfwPlatformPollJoystick(int jid, int mode)
{ {
#if defined(__linux__)
_GLFWjoystick* js = _glfw.joysticks + jid; _GLFWjoystick* js = _glfw.joysticks + jid;
// Read all queued events (non-blocking) // Read all queued events (non-blocking)
@ -296,7 +280,7 @@ int _glfwPlatformPollJoystick(int jid, int mode)
else if (e.type == JS_EVENT_BUTTON) else if (e.type == JS_EVENT_BUTTON)
_glfwInputJoystickButton(jid, e.number, e.value ? 1 : 0); _glfwInputJoystickButton(jid, e.number, e.value ? 1 : 0);
} }
#endif // __linux__
return js->present; return js->present;
} }

View File

@ -45,11 +45,9 @@ typedef struct _GLFWjoystickLinux
// //
typedef struct _GLFWlibraryLinux typedef struct _GLFWlibraryLinux
{ {
#if defined(__linux__)
int inotify; int inotify;
int watch; int watch;
regex_t regex; regex_t regex;
#endif /*__linux__*/
} _GLFWlibraryLinux; } _GLFWlibraryLinux;

View File

@ -239,9 +239,7 @@ const char* _glfwPlatformGetVersionString(void)
#else #else
" gettimeofday" " gettimeofday"
#endif #endif
#if defined(__linux__)
" /dev/js" " /dev/js"
#endif
#if defined(_GLFW_BUILD_DLL) #if defined(_GLFW_BUILD_DLL)
" shared" " shared"
#endif #endif

38
src/null_joystick.c Normal file
View File

@ -0,0 +1,38 @@
//========================================================================
// GLFW 3.3 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
int _glfwPlatformPollJoystick(int jid, int mode)
{
return GLFW_FALSE;
}

34
src/null_joystick.h Normal file
View File

@ -0,0 +1,34 @@
//========================================================================
// GLFW 3.3 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#ifndef _glfw3_null_joystick_h_
#define _glfw3_null_joystick_h_
#define _GLFW_PLATFORM_JOYSTICK_STATE int nulljs
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE int nulljs
#endif // _glfw3_null_joystick_h_

View File

@ -35,13 +35,13 @@
#define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_MONITOR_STATE
#define _GLFW_PLATFORM_CURSOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE
#define _GLFW_EGL_CONTEXT_STATE #define _GLFW_EGL_CONTEXT_STATE
#define _GLFW_EGL_LIBRARY_CONTEXT_STATE #define _GLFW_EGL_LIBRARY_CONTEXT_STATE
#include "osmesa_context.h" #include "osmesa_context.h"
#include "posix_time.h" #include "posix_time.h"
#include "posix_tls.h" #include "posix_tls.h"
#include "null_joystick.h"
#if defined(_GLFW_WIN32) #if defined(_GLFW_WIN32)
#define _glfw_dlopen(name) LoadLibraryA(name) #define _glfw_dlopen(name) LoadLibraryA(name)

View File

@ -269,11 +269,6 @@ int _glfwPlatformGetKeyScancode(int key)
return -1; return -1;
} }
int _glfwPlatformPollJoystick(int jid, int mode)
{
return GLFW_FALSE;
}
void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
{ {
} }

View File

@ -739,9 +739,7 @@ const char* _glfwPlatformGetVersionString(void)
#else #else
" gettimeofday" " gettimeofday"
#endif #endif
#if defined(__linux__)
" /dev/js" " /dev/js"
#endif
#if defined(_GLFW_BUILD_DLL) #if defined(_GLFW_BUILD_DLL)
" shared" " shared"
#endif #endif

View File

@ -798,8 +798,10 @@ int _glfwPlatformInit(void)
if (!_glfwInitThreadLocalStoragePOSIX()) if (!_glfwInitThreadLocalStoragePOSIX())
return GLFW_FALSE; return GLFW_FALSE;
#if defined(__linux__)
if (!_glfwInitJoysticksLinux()) if (!_glfwInitJoysticksLinux())
return GLFW_FALSE; return GLFW_FALSE;
#endif
_glfwInitTimerPOSIX(); _glfwInitTimerPOSIX();
@ -853,7 +855,9 @@ void _glfwPlatformTerminate(void)
// cleanup callbacks that get called by it // cleanup callbacks that get called by it
_glfwTerminateGLX(); _glfwTerminateGLX();
#if defined(__linux__)
_glfwTerminateJoysticksLinux(); _glfwTerminateJoysticksLinux();
#endif
_glfwTerminateThreadLocalStoragePOSIX(); _glfwTerminateThreadLocalStoragePOSIX();
} }

View File

@ -89,10 +89,14 @@ typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)(Vk
#include "posix_tls.h" #include "posix_tls.h"
#include "posix_time.h" #include "posix_time.h"
#include "linux_joystick.h"
#include "xkb_unicode.h" #include "xkb_unicode.h"
#include "glx_context.h" #include "glx_context.h"
#include "egl_context.h" #include "egl_context.h"
#if defined(__linux__)
#include "linux_joystick.h"
#else
#include "null_joystick.h"
#endif
#define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL) #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
#define _glfw_dlclose(handle) dlclose(handle) #define _glfw_dlclose(handle) dlclose(handle)

View File

@ -2153,8 +2153,9 @@ void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
void _glfwPlatformPollEvents(void) void _glfwPlatformPollEvents(void)
{ {
#if defined(__linux__)
_glfwDetectJoystickConnectionLinux(); _glfwDetectJoystickConnectionLinux();
#endif
int count = XPending(_glfw.x11.display); int count = XPending(_glfw.x11.display);
while (count--) while (count--)
{ {