hidapi: Add SDL_hidapi.c, allows support for multiple hidapi backends.

This is currently supported on Linux and macOS. iOS and Android are not
supported at all, Windows support could be added with some changes to the libusb
backend. The Visual Studio and Xcode projects do not use this feature.

Based on Valve Software's hid.cpp, written in collaboration with Andrew Eikum.
This commit is contained in:
Ethan Lee
2019-07-31 12:20:55 -04:00
parent c10a87429d
commit f7d82e5616
7 changed files with 847 additions and 27 deletions
+13 -7
View File
@@ -156,11 +156,10 @@ if(UNIX OR MINGW OR MSYS)
endif() endif()
# The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers, # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
# so we'll just use libusb when it's available. Except that libusb # so we'll just use libusb when it's available. libusb does not support iOS,
# requires root permissions to open devices, so that's not generally # so we default to yes on iOS.
# useful, and we'll disable this by default on Unix. Windows and macOS # TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
# can use it without root access, though, so enable by default there. if(WINDOWS OR IOS OR ANDROID)
if(WINDOWS OR APPLE OR ANDROID)
set(HIDAPI_SKIP_LIBUSB TRUE) set(HIDAPI_SKIP_LIBUSB TRUE)
else() else()
set(HIDAPI_SKIP_LIBUSB FALSE) set(HIDAPI_SKIP_LIBUSB FALSE)
@@ -169,6 +168,14 @@ if (HIDAPI_SKIP_LIBUSB)
set(OPT_DEF_HIDAPI ON) set(OPT_DEF_HIDAPI ON)
endif() endif()
# On the other hand, *BSD specifically uses libusb only, so we make a special
# case just for them.
if(FREEBSD OR NETBSD OR OPENBSD OR BSDI)
set(HIDAPI_ONLY_LIBUSB TRUE)
else()
set(HIDAPI_ONLY_LIBUSB FALSE)
endif()
# Compiler info # Compiler info
if(CMAKE_COMPILER_IS_GNUCC) if(CMAKE_COMPILER_IS_GNUCC)
set(USE_GCC TRUE) set(USE_GCC TRUE)
@@ -1376,6 +1383,7 @@ elseif(WINDOWS)
if(SDL_JOYSTICK) if(SDL_JOYSTICK)
CheckHIDAPI() CheckHIDAPI()
# TODO: Remove this hid.c block when SDL_hidapi.c is supported on Windows!
if(HAVE_HIDAPI) if(HAVE_HIDAPI)
set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/windows/hid.c) set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/windows/hid.c)
endif() endif()
@@ -1466,8 +1474,6 @@ elseif(APPLE)
if(HAVE_HIDAPI) if(HAVE_HIDAPI)
if(IOS) if(IOS)
set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/ios/hid.m) set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/ios/hid.m)
else()
set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/mac/hid.c)
endif() endif()
endif() endif()
set(SDL_JOYSTICK_IOKIT 1) set(SDL_JOYSTICK_IOKIT 1)
+7
View File
@@ -1088,8 +1088,15 @@ macro(CheckHIDAPI)
set(SOURCE_FILES ${SOURCE_FILES} ${HIDAPI_SOURCES}) set(SOURCE_FILES ${SOURCE_FILES} ${HIDAPI_SOURCES})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBUSB_CFLAGS} -I${SDL2_SOURCE_DIR}/src/hidapi/hidapi") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBUSB_CFLAGS} -I${SDL2_SOURCE_DIR}/src/hidapi/hidapi")
if(NOT HIDAPI_SKIP_LIBUSB) if(NOT HIDAPI_SKIP_LIBUSB)
if(HIDAPI_ONLY_LIBUSB)
set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/libusb/hid.c) set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/libusb/hid.c)
list(APPEND EXTRA_LIBS ${LIBUSB_LIBS}) list(APPEND EXTRA_LIBS ${LIBUSB_LIBS})
else()
set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/SDL_hidapi.c)
# libusb is loaded dynamically, so don't add it to EXTRA_LIBS
FindLibraryAndSONAME("usb-1.0")
set(SDL_LIBUSB_DYNAMIC "\"${USB_LIB_SONAME}\"")
endif()
endif() endif()
endif() endif()
endif() endif()
Vendored
+40 -8
View File
@@ -24106,16 +24106,24 @@ CheckHIDAPI()
# The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers, # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
# so we'll just use libusb when it's available. # so we'll just use libusb when it's available.
# #
# Except that libusb requires root permissions to open devices, so that's not generally useful, and we'll disable this by default. # libusb does not support iOS, so we default to yes on iOS.
# # TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
# On macOS and Windows, where you don't need libusb or root, we default to yes.
skiplibusb=no skiplibusb=no
case "$host" in case "$host" in
*-*-cygwin* | *-*-mingw32* | *-*-darwin* ) *-*-cygwin* | *-*-mingw32* | arm*-apple-darwin* | *-ios-* )
skiplibusb=yes skiplibusb=yes
;; ;;
esac esac
# On the other hand, *BSD specifically uses libusb only, so we make a
# special case just for them.
onlylibusb=no
case "$host" in
*-*-*bsd* )
onlylibusb=yes
;;
esac
# Check whether --enable-hidapi was given. # Check whether --enable-hidapi was given.
if test "${enable_hidapi+set}" = set; then : if test "${enable_hidapi+set}" = set; then :
enableval=$enable_hidapi; enableval=$enable_hidapi;
@@ -24224,9 +24232,35 @@ $as_echo "#define SDL_JOYSTICK_HIDAPI 1" >>confdefs.h
SOURCES="$SOURCES $srcdir/src/joystick/hidapi/*.c" SOURCES="$SOURCES $srcdir/src/joystick/hidapi/*.c"
if test x$skiplibusb = xno; then if test x$skiplibusb = xno; then
SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS" EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS"
if test x$onlylibusb = xyes; then
SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS" EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
else
if test x$have_loadso != xyes; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: You must have SDL_LoadObject() support for dynamic libusb loading" >&5
$as_echo "$as_me: WARNING: You must have SDL_LoadObject() support for dynamic libusb loading" >&2;}
fi
SOURCES="$SOURCES $srcdir/src/hidapi/SDL_hidapi.c"
# libusb is loaded dynamically, so don't add it to LDFLAGS
libusb_lib=""
case "$host" in
*-*-darwin* )
libusb_lib="libusb-1.0.0.dylib"
;;
*-*-cygwin* | *-*-mingw32* )
libusb_lib="libusb-1.0.dll"
;;
esac
if test x$libusb_lib = x; then
libusb_lib=`find_lib "libusb-1.0.so.*" "" | sed 's/.*\/\(.*\)/\1/; q'`
fi
cat >>confdefs.h <<_ACEOF
#define SDL_LIBUSB_DYNAMIC "$libusb_lib"
_ACEOF
fi
fi fi
fi fi
@@ -24732,6 +24766,7 @@ $as_echo "#define SDL_JOYSTICK_WINMM 1" >>confdefs.h
fi fi
SOURCES="$SOURCES $srcdir/src/joystick/windows/*.c" SOURCES="$SOURCES $srcdir/src/joystick/windows/*.c"
have_joystick=yes have_joystick=yes
# TODO: Remove this block once SDL_hidapi.c supports Windows!
if test x$hidapi_support = xyes; then if test x$hidapi_support = xyes; then
SOURCES="$SOURCES $srcdir/src/hidapi/windows/hid.c" SOURCES="$SOURCES $srcdir/src/hidapi/windows/hid.c"
fi fi
@@ -25077,9 +25112,6 @@ $as_echo "#define SDL_JOYSTICK_IOKIT 1" >>confdefs.h
SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c" SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c"
have_joystick=yes have_joystick=yes
if test x$hidapi_support = xyes; then
SOURCES="$SOURCES $srcdir/src/hidapi/mac/hid.c"
fi
fi fi
# Set up files for the haptic library # Set up files for the haptic library
if test x$enable_haptic = xyes; then if test x$enable_haptic = xyes; then
+35 -8
View File
@@ -3198,16 +3198,24 @@ CheckHIDAPI()
# The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers, # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
# so we'll just use libusb when it's available. # so we'll just use libusb when it's available.
# #
# Except that libusb requires root permissions to open devices, so that's not generally useful, and we'll disable this by default. # libusb does not support iOS, so we default to yes on iOS.
# # TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
# On macOS and Windows, where you don't need libusb or root, we default to yes.
skiplibusb=no skiplibusb=no
case "$host" in case "$host" in
*-*-cygwin* | *-*-mingw32* | *-*-darwin* ) *-*-cygwin* | *-*-mingw32* | arm*-apple-darwin* | *-ios-* )
skiplibusb=yes skiplibusb=yes
;; ;;
esac esac
# On the other hand, *BSD specifically uses libusb only, so we make a
# special case just for them.
onlylibusb=no
case "$host" in
*-*-*bsd* )
onlylibusb=yes
;;
esac
AC_ARG_ENABLE(hidapi, AC_ARG_ENABLE(hidapi,
AS_HELP_STRING([--enable-hidapi], [use HIDAPI for low level joystick drivers [[default=maybe]]]), AS_HELP_STRING([--enable-hidapi], [use HIDAPI for low level joystick drivers [[default=maybe]]]),
, enable_hidapi=maybe) , enable_hidapi=maybe)
@@ -3237,9 +3245,30 @@ AS_HELP_STRING([--enable-hidapi], [use HIDAPI for low level joystick drivers [[d
SOURCES="$SOURCES $srcdir/src/joystick/hidapi/*.c" SOURCES="$SOURCES $srcdir/src/joystick/hidapi/*.c"
if test x$skiplibusb = xno; then if test x$skiplibusb = xno; then
SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS" EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS"
if test x$onlylibusb = xyes; then
SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS" EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
else
if test x$have_loadso != xyes; then
AC_MSG_WARN([You must have SDL_LoadObject() support for dynamic libusb loading])
fi
SOURCES="$SOURCES $srcdir/src/hidapi/SDL_hidapi.c"
# libusb is loaded dynamically, so don't add it to LDFLAGS
libusb_lib=""
case "$host" in
*-*-darwin* )
libusb_lib="libusb-1.0.0.dylib"
;;
*-*-cygwin* | *-*-mingw32* )
libusb_lib="libusb-1.0.dll"
;;
esac
if test x$libusb_lib = x; then
libusb_lib=[`find_lib "libusb-1.0.so.*" "" | sed 's/.*\/\(.*\)/\1/; q'`]
fi
AC_DEFINE_UNQUOTED(SDL_LIBUSB_DYNAMIC, "$libusb_lib", [ ])
fi
fi fi
fi fi
@@ -3599,6 +3628,7 @@ AS_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
fi fi
SOURCES="$SOURCES $srcdir/src/joystick/windows/*.c" SOURCES="$SOURCES $srcdir/src/joystick/windows/*.c"
have_joystick=yes have_joystick=yes
# TODO: Remove this block once SDL_hidapi.c supports Windows!
if test x$hidapi_support = xyes; then if test x$hidapi_support = xyes; then
SOURCES="$SOURCES $srcdir/src/hidapi/windows/hid.c" SOURCES="$SOURCES $srcdir/src/hidapi/windows/hid.c"
fi fi
@@ -3842,9 +3872,6 @@ AS_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
AC_DEFINE(SDL_JOYSTICK_IOKIT, 1, [ ]) AC_DEFINE(SDL_JOYSTICK_IOKIT, 1, [ ])
SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c" SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c"
have_joystick=yes have_joystick=yes
if test x$hidapi_support = xyes; then
SOURCES="$SOURCES $srcdir/src/hidapi/mac/hid.c"
fi
fi fi
# Set up files for the haptic library # Set up files for the haptic library
if test x$enable_haptic = xyes; then if test x$enable_haptic = xyes; then
+1
View File
@@ -294,6 +294,7 @@
#cmakedefine SDL_HAPTIC_DINPUT @SDL_HAPTIC_DINPUT@ #cmakedefine SDL_HAPTIC_DINPUT @SDL_HAPTIC_DINPUT@
#cmakedefine SDL_HAPTIC_XINPUT @SDL_HAPTIC_XINPUT@ #cmakedefine SDL_HAPTIC_XINPUT @SDL_HAPTIC_XINPUT@
#cmakedefine SDL_HAPTIC_ANDROID @SDL_HAPTIC_ANDROID@ #cmakedefine SDL_HAPTIC_ANDROID @SDL_HAPTIC_ANDROID@
#cmakedefine SDL_LIBUSB_DYNAMIC @SDL_LIBUSB_DYNAMIC@
/* Enable various sensor drivers */ /* Enable various sensor drivers */
#cmakedefine SDL_SENSOR_ANDROID @SDL_SENSOR_ANDROID@ #cmakedefine SDL_SENSOR_ANDROID @SDL_SENSOR_ANDROID@
+3
View File
@@ -412,6 +412,9 @@
/* Enable dynamic udev support */ /* Enable dynamic udev support */
#undef SDL_UDEV_DYNAMIC #undef SDL_UDEV_DYNAMIC
/* Enable dynamic libusb support */
#undef SDL_LIBUSB_DYNAMIC
/* Enable dynamic libsamplerate support */ /* Enable dynamic libsamplerate support */
#undef SDL_LIBSAMPLERATE_DYNAMIC #undef SDL_LIBSAMPLERATE_DYNAMIC
File diff suppressed because it is too large Load Diff