hidapi: Add SDL_hidapi.c, allows support for multiple hidapi backends.
authorEthan Lee <flibitijibibo@flibitijibibo.com>
Wed, 31 Jul 2019 12:20:55 -0400
changeset 129740fef4b21fa1d
parent 12973 c52f7214f707
child 12976 07104eac00fb
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.
CMakeLists.txt
cmake/sdlchecks.cmake
configure
configure.ac
include/SDL_config.h.cmake
include/SDL_config.h.in
src/hidapi/SDL_hidapi.c
     1.1 --- a/CMakeLists.txt	Wed Jul 31 10:20:37 2019 -0700
     1.2 +++ b/CMakeLists.txt	Wed Jul 31 12:20:55 2019 -0400
     1.3 @@ -156,11 +156,10 @@
     1.4  endif()
     1.5  
     1.6  # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
     1.7 -#  so we'll just use libusb when it's available. Except that libusb
     1.8 -#  requires root permissions to open devices, so that's not generally
     1.9 -#  useful, and we'll disable this by default on Unix. Windows and macOS
    1.10 -#  can use it without root access, though, so enable by default there.
    1.11 -if(WINDOWS OR APPLE OR ANDROID)
    1.12 +#  so we'll just use libusb when it's available. libusb does not support iOS,
    1.13 +#  so we default to yes on iOS.
    1.14 +#  TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
    1.15 +if(WINDOWS OR IOS OR ANDROID)
    1.16    set(HIDAPI_SKIP_LIBUSB TRUE)
    1.17  else()
    1.18    set(HIDAPI_SKIP_LIBUSB FALSE)
    1.19 @@ -169,6 +168,14 @@
    1.20    set(OPT_DEF_HIDAPI ON)
    1.21  endif()
    1.22  
    1.23 +# On the other hand, *BSD specifically uses libusb only, so we make a special
    1.24 +#  case just for them.
    1.25 +if(FREEBSD OR NETBSD OR OPENBSD OR BSDI)
    1.26 +  set(HIDAPI_ONLY_LIBUSB TRUE)
    1.27 +else()
    1.28 +  set(HIDAPI_ONLY_LIBUSB FALSE)
    1.29 +endif()
    1.30 +
    1.31  # Compiler info
    1.32  if(CMAKE_COMPILER_IS_GNUCC)
    1.33    set(USE_GCC TRUE)
    1.34 @@ -1376,6 +1383,7 @@
    1.35  
    1.36    if(SDL_JOYSTICK)
    1.37      CheckHIDAPI()
    1.38 +    # TODO: Remove this hid.c block when SDL_hidapi.c is supported on Windows!
    1.39      if(HAVE_HIDAPI)
    1.40        set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/windows/hid.c)
    1.41      endif()
    1.42 @@ -1466,8 +1474,6 @@
    1.43      if(HAVE_HIDAPI)
    1.44        if(IOS)
    1.45          set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/ios/hid.m)
    1.46 -      else()
    1.47 -        set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/mac/hid.c)
    1.48        endif()
    1.49      endif()
    1.50      set(SDL_JOYSTICK_IOKIT 1)
     2.1 --- a/cmake/sdlchecks.cmake	Wed Jul 31 10:20:37 2019 -0700
     2.2 +++ b/cmake/sdlchecks.cmake	Wed Jul 31 12:20:55 2019 -0400
     2.3 @@ -1088,8 +1088,15 @@
     2.4        set(SOURCE_FILES ${SOURCE_FILES} ${HIDAPI_SOURCES})
     2.5        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBUSB_CFLAGS} -I${SDL2_SOURCE_DIR}/src/hidapi/hidapi")
     2.6        if(NOT HIDAPI_SKIP_LIBUSB)
     2.7 -        set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/libusb/hid.c)
     2.8 -        list(APPEND EXTRA_LIBS ${LIBUSB_LIBS})
     2.9 +        if(HIDAPI_ONLY_LIBUSB)
    2.10 +          set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/libusb/hid.c)
    2.11 +          list(APPEND EXTRA_LIBS ${LIBUSB_LIBS})
    2.12 +        else()
    2.13 +          set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/SDL_hidapi.c)
    2.14 +          # libusb is loaded dynamically, so don't add it to EXTRA_LIBS
    2.15 +          FindLibraryAndSONAME("usb-1.0")
    2.16 +          set(SDL_LIBUSB_DYNAMIC "\"${USB_LIB_SONAME}\"")
    2.17 +        endif()
    2.18        endif()
    2.19      endif()
    2.20    endif()
     3.1 --- a/configure	Wed Jul 31 10:20:37 2019 -0700
     3.2 +++ b/configure	Wed Jul 31 12:20:55 2019 -0400
     3.3 @@ -24106,16 +24106,24 @@
     3.4      # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
     3.5      # so we'll just use libusb when it's available.
     3.6      #
     3.7 -    # Except that libusb requires root permissions to open devices, so that's not generally useful, and we'll disable this by default.
     3.8 -    #
     3.9 -    #  On macOS and Windows, where you don't need libusb or root, we default to yes.
    3.10 +    # libusb does not support iOS, so we default to yes on iOS.
    3.11 +    # TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
    3.12      skiplibusb=no
    3.13      case "$host" in
    3.14 -        *-*-cygwin* | *-*-mingw32* | *-*-darwin* )
    3.15 +        *-*-cygwin* | *-*-mingw32* | arm*-apple-darwin* | *-ios-* )
    3.16              skiplibusb=yes
    3.17              ;;
    3.18      esac
    3.19  
    3.20 +    # On the other hand, *BSD specifically uses libusb only, so we make a
    3.21 +    # special case just for them.
    3.22 +    onlylibusb=no
    3.23 +    case "$host" in
    3.24 +        *-*-*bsd* )
    3.25 +            onlylibusb=yes
    3.26 +            ;;
    3.27 +    esac
    3.28 +
    3.29      # Check whether --enable-hidapi was given.
    3.30  if test "${enable_hidapi+set}" = set; then :
    3.31    enableval=$enable_hidapi;
    3.32 @@ -24224,9 +24232,35 @@
    3.33              SOURCES="$SOURCES $srcdir/src/joystick/hidapi/*.c"
    3.34  
    3.35              if test x$skiplibusb = xno; then
    3.36 -                SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
    3.37                  EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS"
    3.38 -                EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
    3.39 +                if test x$onlylibusb = xyes; then
    3.40 +                    SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
    3.41 +                    EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
    3.42 +                else
    3.43 +                    if test x$have_loadso != xyes; then
    3.44 +                        { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: You must have SDL_LoadObject() support for dynamic libusb loading" >&5
    3.45 +$as_echo "$as_me: WARNING: You must have SDL_LoadObject() support for dynamic libusb loading" >&2;}
    3.46 +                    fi
    3.47 +                    SOURCES="$SOURCES $srcdir/src/hidapi/SDL_hidapi.c"
    3.48 +                    # libusb is loaded dynamically, so don't add it to LDFLAGS
    3.49 +                    libusb_lib=""
    3.50 +                    case "$host" in
    3.51 +                        *-*-darwin* )
    3.52 +                            libusb_lib="libusb-1.0.0.dylib"
    3.53 +                            ;;
    3.54 +                        *-*-cygwin* | *-*-mingw32* )
    3.55 +                            libusb_lib="libusb-1.0.dll"
    3.56 +                            ;;
    3.57 +                    esac
    3.58 +                    if test x$libusb_lib = x; then
    3.59 +                        libusb_lib=`find_lib "libusb-1.0.so.*" "" | sed 's/.*\/\(.*\)/\1/; q'`
    3.60 +                    fi
    3.61 +
    3.62 +cat >>confdefs.h <<_ACEOF
    3.63 +#define SDL_LIBUSB_DYNAMIC "$libusb_lib"
    3.64 +_ACEOF
    3.65 +
    3.66 +                fi
    3.67              fi
    3.68          fi
    3.69  
    3.70 @@ -24732,6 +24766,7 @@
    3.71              fi
    3.72              SOURCES="$SOURCES $srcdir/src/joystick/windows/*.c"
    3.73              have_joystick=yes
    3.74 +            # TODO: Remove this block once SDL_hidapi.c supports Windows!
    3.75              if test x$hidapi_support = xyes; then
    3.76                  SOURCES="$SOURCES $srcdir/src/hidapi/windows/hid.c"
    3.77              fi
    3.78 @@ -25077,9 +25112,6 @@
    3.79  
    3.80              SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c"
    3.81              have_joystick=yes
    3.82 -            if test x$hidapi_support = xyes; then
    3.83 -                SOURCES="$SOURCES $srcdir/src/hidapi/mac/hid.c"
    3.84 -            fi
    3.85          fi
    3.86          # Set up files for the haptic library
    3.87          if test x$enable_haptic = xyes; then
     4.1 --- a/configure.ac	Wed Jul 31 10:20:37 2019 -0700
     4.2 +++ b/configure.ac	Wed Jul 31 12:20:55 2019 -0400
     4.3 @@ -3198,16 +3198,24 @@
     4.4      # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
     4.5      # so we'll just use libusb when it's available.
     4.6      #
     4.7 -    # Except that libusb requires root permissions to open devices, so that's not generally useful, and we'll disable this by default.
     4.8 -    #
     4.9 -    #  On macOS and Windows, where you don't need libusb or root, we default to yes.
    4.10 +    # libusb does not support iOS, so we default to yes on iOS.
    4.11 +    # TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
    4.12      skiplibusb=no
    4.13      case "$host" in
    4.14 -        *-*-cygwin* | *-*-mingw32* | *-*-darwin* )
    4.15 +        *-*-cygwin* | *-*-mingw32* | arm*-apple-darwin* | *-ios-* )
    4.16              skiplibusb=yes
    4.17              ;;
    4.18      esac
    4.19  
    4.20 +    # On the other hand, *BSD specifically uses libusb only, so we make a
    4.21 +    # special case just for them.
    4.22 +    onlylibusb=no
    4.23 +    case "$host" in
    4.24 +        *-*-*bsd* )
    4.25 +            onlylibusb=yes
    4.26 +            ;;
    4.27 +    esac
    4.28 +
    4.29      AC_ARG_ENABLE(hidapi,
    4.30  AS_HELP_STRING([--enable-hidapi], [use HIDAPI for low level joystick drivers [[default=maybe]]]),
    4.31                    , enable_hidapi=maybe)
    4.32 @@ -3237,9 +3245,30 @@
    4.33              SOURCES="$SOURCES $srcdir/src/joystick/hidapi/*.c"
    4.34  
    4.35              if test x$skiplibusb = xno; then
    4.36 -                SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
    4.37                  EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS"
    4.38 -                EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
    4.39 +                if test x$onlylibusb = xyes; then
    4.40 +                    SOURCES="$SOURCES $srcdir/src/hidapi/libusb/hid.c"
    4.41 +                    EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
    4.42 +                else
    4.43 +                    if test x$have_loadso != xyes; then
    4.44 +                        AC_MSG_WARN([You must have SDL_LoadObject() support for dynamic libusb loading])
    4.45 +                    fi
    4.46 +                    SOURCES="$SOURCES $srcdir/src/hidapi/SDL_hidapi.c"
    4.47 +                    # libusb is loaded dynamically, so don't add it to LDFLAGS
    4.48 +                    libusb_lib=""
    4.49 +                    case "$host" in
    4.50 +                        *-*-darwin* )
    4.51 +                            libusb_lib="libusb-1.0.0.dylib"
    4.52 +                            ;;
    4.53 +                        *-*-cygwin* | *-*-mingw32* )
    4.54 +                            libusb_lib="libusb-1.0.dll"
    4.55 +                            ;;
    4.56 +                    esac
    4.57 +                    if test x$libusb_lib = x; then
    4.58 +                        libusb_lib=[`find_lib "libusb-1.0.so.*" "" | sed 's/.*\/\(.*\)/\1/; q'`]
    4.59 +                    fi
    4.60 +                    AC_DEFINE_UNQUOTED(SDL_LIBUSB_DYNAMIC, "$libusb_lib", [ ])
    4.61 +                fi
    4.62              fi
    4.63          fi
    4.64  
    4.65 @@ -3599,6 +3628,7 @@
    4.66              fi
    4.67              SOURCES="$SOURCES $srcdir/src/joystick/windows/*.c"
    4.68              have_joystick=yes
    4.69 +            # TODO: Remove this block once SDL_hidapi.c supports Windows!
    4.70              if test x$hidapi_support = xyes; then
    4.71                  SOURCES="$SOURCES $srcdir/src/hidapi/windows/hid.c"
    4.72              fi
    4.73 @@ -3842,9 +3872,6 @@
    4.74              AC_DEFINE(SDL_JOYSTICK_IOKIT, 1, [ ])
    4.75              SOURCES="$SOURCES $srcdir/src/joystick/darwin/*.c"
    4.76              have_joystick=yes
    4.77 -            if test x$hidapi_support = xyes; then
    4.78 -                SOURCES="$SOURCES $srcdir/src/hidapi/mac/hid.c"
    4.79 -            fi
    4.80          fi
    4.81          # Set up files for the haptic library
    4.82          if test x$enable_haptic = xyes; then
     5.1 --- a/include/SDL_config.h.cmake	Wed Jul 31 10:20:37 2019 -0700
     5.2 +++ b/include/SDL_config.h.cmake	Wed Jul 31 12:20:55 2019 -0400
     5.3 @@ -294,6 +294,7 @@
     5.4  #cmakedefine SDL_HAPTIC_DINPUT @SDL_HAPTIC_DINPUT@
     5.5  #cmakedefine SDL_HAPTIC_XINPUT @SDL_HAPTIC_XINPUT@
     5.6  #cmakedefine SDL_HAPTIC_ANDROID @SDL_HAPTIC_ANDROID@
     5.7 +#cmakedefine SDL_LIBUSB_DYNAMIC @SDL_LIBUSB_DYNAMIC@
     5.8  
     5.9  /* Enable various sensor drivers */
    5.10  #cmakedefine SDL_SENSOR_ANDROID @SDL_SENSOR_ANDROID@
     6.1 --- a/include/SDL_config.h.in	Wed Jul 31 10:20:37 2019 -0700
     6.2 +++ b/include/SDL_config.h.in	Wed Jul 31 12:20:55 2019 -0400
     6.3 @@ -412,6 +412,9 @@
     6.4  /* Enable dynamic udev support */
     6.5  #undef SDL_UDEV_DYNAMIC
     6.6  
     6.7 +/* Enable dynamic libusb support */
     6.8 +#undef SDL_LIBUSB_DYNAMIC
     6.9 +
    6.10  /* Enable dynamic libsamplerate support */
    6.11  #undef SDL_LIBSAMPLERATE_DYNAMIC
    6.12  
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/hidapi/SDL_hidapi.c	Wed Jul 31 12:20:55 2019 -0400
     7.3 @@ -0,0 +1,744 @@
     7.4 +/*
     7.5 +  Simple DirectMedia Layer
     7.6 +  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     7.7 +
     7.8 +  This software is provided 'as-is', without any express or implied
     7.9 +  warranty.  In no event will the authors be held liable for any damages
    7.10 +  arising from the use of this software.
    7.11 +
    7.12 +  Permission is granted to anyone to use this software for any purpose,
    7.13 +  including commercial applications, and to alter it and redistribute it
    7.14 +  freely, subject to the following restrictions:
    7.15 +
    7.16 +  1. The origin of this software must not be misrepresented; you must not
    7.17 +     claim that you wrote the original software. If you use this software
    7.18 +     in a product, an acknowledgment in the product documentation would be
    7.19 +     appreciated but is not required.
    7.20 +  2. Altered source versions must be plainly marked as such, and must not be
    7.21 +     misrepresented as being the original software.
    7.22 +  3. This notice may not be removed or altered from any source distribution.
    7.23 +*/
    7.24 +
    7.25 +/* Original hybrid wrapper for Linux by Valve Software. Their original notes:
    7.26 + *
    7.27 + * The libusb version doesn't support Bluetooth, but not all Linux
    7.28 + * distributions allow access to /dev/hidraw*
    7.29 + *
    7.30 + * This merges the two, at a small performance cost, until distributions
    7.31 + * have granted access to /dev/hidraw*
    7.32 + */
    7.33 +
    7.34 +#include "../SDL_internal.h"
    7.35 +#include "SDL_loadso.h"
    7.36 +
    7.37 +#ifdef SDL_JOYSTICK_HIDAPI
    7.38 +
    7.39 +/* Platform HIDAPI Implementation */
    7.40 +
    7.41 +#define hid_device_                     PLATFORM_hid_device_
    7.42 +#define hid_device                      PLATFORM_hid_device
    7.43 +#define hid_device_info                 PLATFORM_hid_device_info
    7.44 +#define hid_init                        PLATFORM_hid_init
    7.45 +#define hid_exit                        PLATFORM_hid_exit
    7.46 +#define hid_enumerate                   PLATFORM_hid_enumerate
    7.47 +#define hid_free_enumeration            PLATFORM_hid_free_enumeration
    7.48 +#define hid_open                        PLATFORM_hid_open
    7.49 +#define hid_open_path                   PLATFORM_hid_open_path
    7.50 +#define hid_write                       PLATFORM_hid_write
    7.51 +#define hid_read_timeout                PLATFORM_hid_read_timeout
    7.52 +#define hid_read                        PLATFORM_hid_read
    7.53 +#define hid_set_nonblocking             PLATFORM_hid_set_nonblocking
    7.54 +#define hid_send_feature_report         PLATFORM_hid_send_feature_report
    7.55 +#define hid_get_feature_report          PLATFORM_hid_get_feature_report
    7.56 +#define hid_close                       PLATFORM_hid_close
    7.57 +#define hid_get_manufacturer_string     PLATFORM_hid_get_manufacturer_string
    7.58 +#define hid_get_product_string          PLATFORM_hid_get_product_string
    7.59 +#define hid_get_serial_number_string    PLATFORM_hid_get_serial_number_string
    7.60 +#define hid_get_indexed_string          PLATFORM_hid_get_indexed_string
    7.61 +#define hid_error                       PLATFORM_hid_error
    7.62 +#define new_hid_device                  PLATFORM_new_hid_device
    7.63 +#define free_hid_device                 PLATFORM_free_hid_device
    7.64 +#define input_report                    PLATFORM_input_report
    7.65 +#define return_data                     PLATFORM_return_data
    7.66 +#define make_path                       PLATFORM_make_path
    7.67 +#define read_thread                     PLATFORM_read_thread
    7.68 +
    7.69 +#if __LINUX__
    7.70 +
    7.71 +#include "../../core/linux/SDL_udev.h"
    7.72 +#if SDL_USE_LIBUDEV
    7.73 +static const SDL_UDEV_Symbols *udev_ctx = NULL;
    7.74 +
    7.75 +#define udev_device_get_sysattr_value                    udev_ctx->udev_device_get_sysattr_value
    7.76 +#define udev_new                                         udev_ctx->udev_new
    7.77 +#define udev_unref                                       udev_ctx->udev_unref
    7.78 +#define udev_device_new_from_devnum                      udev_ctx->udev_device_new_from_devnum
    7.79 +#define udev_device_get_parent_with_subsystem_devtype    udev_ctx->udev_device_get_parent_with_subsystem_devtype
    7.80 +#define udev_device_unref                                udev_ctx->udev_device_unref
    7.81 +#define udev_enumerate_new                               udev_ctx->udev_enumerate_new
    7.82 +#define udev_enumerate_add_match_subsystem               udev_ctx->udev_enumerate_add_match_subsystem
    7.83 +#define udev_enumerate_scan_devices                      udev_ctx->udev_enumerate_scan_devices
    7.84 +#define udev_enumerate_get_list_entry                    udev_ctx->udev_enumerate_get_list_entry
    7.85 +#define udev_list_entry_get_name                         udev_ctx->udev_list_entry_get_name
    7.86 +#define udev_device_new_from_syspath                     udev_ctx->udev_device_new_from_syspath
    7.87 +#define udev_device_get_devnode                          udev_ctx->udev_device_get_devnode
    7.88 +#define udev_list_entry_get_next                         udev_ctx->udev_list_entry_get_next
    7.89 +#define udev_enumerate_unref                             udev_ctx->udev_enumerate_unref
    7.90 +
    7.91 +#include "linux/hid.c"
    7.92 +#define HAVE_PLATFORM_BACKEND 1
    7.93 +#endif /* SDL_USE_LIBUDEV */
    7.94 +
    7.95 +#elif __MACOSX__
    7.96 +#include "mac/hid.c"
    7.97 +#define HAVE_PLATFORM_BACKEND 1
    7.98 +#define udev_ctx 1
    7.99 +#elif __WINDOWS__
   7.100 +#include "windows/hid.c"
   7.101 +#define HAVE_PLATFORM_BACKEND 1
   7.102 +#define udev_ctx 1
   7.103 +#else
   7.104 +#error Need a hid.c for this platform!
   7.105 +#endif
   7.106 +
   7.107 +#undef hid_device_
   7.108 +#undef hid_device
   7.109 +#undef hid_device_info
   7.110 +#undef hid_init
   7.111 +#undef hid_exit
   7.112 +#undef hid_enumerate
   7.113 +#undef hid_free_enumeration
   7.114 +#undef hid_open
   7.115 +#undef hid_open_path
   7.116 +#undef hid_write
   7.117 +#undef hid_read_timeout
   7.118 +#undef hid_read
   7.119 +#undef hid_set_nonblocking
   7.120 +#undef hid_send_feature_report
   7.121 +#undef hid_get_feature_report
   7.122 +#undef hid_close
   7.123 +#undef hid_get_manufacturer_string
   7.124 +#undef hid_get_product_string
   7.125 +#undef hid_get_serial_number_string
   7.126 +#undef hid_get_indexed_string
   7.127 +#undef hid_error
   7.128 +#undef new_hid_device
   7.129 +#undef free_hid_device
   7.130 +#undef input_report
   7.131 +#undef return_data
   7.132 +#undef make_path
   7.133 +#undef read_thread
   7.134 +
   7.135 +#ifndef SDL_LIBUSB_DYNAMIC
   7.136 +#if __WINDOWS__
   7.137 +#define SDL_LIBUSB_DYNAMIC "libusb-1.0.dll"
   7.138 +#endif /* __WINDOWS__ */
   7.139 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.140 +
   7.141 +#ifdef SDL_LIBUSB_DYNAMIC
   7.142 +/* libusb HIDAPI Implementation */
   7.143 +
   7.144 +/* Include this now, for our dynamically-loaded libusb context */
   7.145 +#include <libusb.h>
   7.146 +
   7.147 +static struct
   7.148 +{
   7.149 +    void* libhandle;
   7.150 +
   7.151 +    int (*init)(libusb_context **ctx);
   7.152 +    void (*exit)(libusb_context *ctx);
   7.153 +    ssize_t (*get_device_list)(libusb_context *ctx, libusb_device ***list);
   7.154 +    void (*free_device_list)(libusb_device **list, int unref_devices);
   7.155 +    int (*get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
   7.156 +    int (*get_active_config_descriptor)(libusb_device *dev,    struct libusb_config_descriptor **config);
   7.157 +    int (*get_config_descriptor)(
   7.158 +        libusb_device *dev,
   7.159 +        uint8_t config_index,
   7.160 +        struct libusb_config_descriptor **config
   7.161 +    );
   7.162 +    void (*free_config_descriptor)(struct libusb_config_descriptor *config);
   7.163 +    uint8_t (*get_bus_number)(libusb_device *dev);
   7.164 +    uint8_t (*get_device_address)(libusb_device *dev);
   7.165 +    int (*open)(libusb_device *dev, libusb_device_handle **dev_handle);
   7.166 +    void (*close)(libusb_device_handle *dev_handle);
   7.167 +    int (*claim_interface)(libusb_device_handle *dev_handle, int interface_number);
   7.168 +    int (*release_interface)(libusb_device_handle *dev_handle, int interface_number);
   7.169 +    int (*kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
   7.170 +    int (*detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
   7.171 +    struct libusb_transfer * (*alloc_transfer)(int iso_packets);
   7.172 +    int (*submit_transfer)(struct libusb_transfer *transfer);
   7.173 +    int (*cancel_transfer)(struct libusb_transfer *transfer);
   7.174 +    void (*free_transfer)(struct libusb_transfer *transfer);
   7.175 +    int (*control_transfer)(
   7.176 +        libusb_device_handle *dev_handle,
   7.177 +        uint8_t request_type,
   7.178 +        uint8_t bRequest,
   7.179 +        uint16_t wValue,
   7.180 +        uint16_t wIndex,
   7.181 +        unsigned char *data,
   7.182 +        uint16_t wLength,
   7.183 +        unsigned int timeout
   7.184 +    );
   7.185 +    int (*interrupt_transfer)(
   7.186 +        libusb_device_handle *dev_handle,
   7.187 +        unsigned char endpoint,
   7.188 +        unsigned char *data,
   7.189 +        int length,
   7.190 +        int *actual_length,
   7.191 +        unsigned int timeout
   7.192 +    );
   7.193 +    int (*handle_events)(libusb_context *ctx);
   7.194 +    int (*handle_events_completed)(libusb_context *ctx, int *completed);
   7.195 +} libusb_ctx;
   7.196 +
   7.197 +#define libusb_init                            libusb_ctx.init
   7.198 +#define libusb_exit                            libusb_ctx.exit
   7.199 +#define libusb_get_device_list                 libusb_ctx.get_device_list
   7.200 +#define libusb_free_device_list                libusb_ctx.free_device_list
   7.201 +#define libusb_get_device_descriptor           libusb_ctx.get_device_descriptor
   7.202 +#define libusb_get_active_config_descriptor    libusb_ctx.get_active_config_descriptor
   7.203 +#define libusb_get_config_descriptor           libusb_ctx.get_config_descriptor
   7.204 +#define libusb_free_config_descriptor          libusb_ctx.free_config_descriptor
   7.205 +#define libusb_get_bus_number                  libusb_ctx.get_bus_number
   7.206 +#define libusb_get_device_address              libusb_ctx.get_device_address
   7.207 +#define libusb_open                            libusb_ctx.open
   7.208 +#define libusb_close                           libusb_ctx.close
   7.209 +#define libusb_claim_interface                 libusb_ctx.claim_interface
   7.210 +#define libusb_release_interface               libusb_ctx.release_interface
   7.211 +#define libusb_kernel_driver_active            libusb_ctx.kernel_driver_active
   7.212 +#define libusb_detach_kernel_driver            libusb_ctx.detach_kernel_driver
   7.213 +#define libusb_alloc_transfer                  libusb_ctx.alloc_transfer
   7.214 +#define libusb_submit_transfer                 libusb_ctx.submit_transfer
   7.215 +#define libusb_cancel_transfer                 libusb_ctx.cancel_transfer
   7.216 +#define libusb_free_transfer                   libusb_ctx.free_transfer
   7.217 +#define libusb_control_transfer                libusb_ctx.control_transfer
   7.218 +#define libusb_interrupt_transfer              libusb_ctx.interrupt_transfer
   7.219 +#define libusb_handle_events                   libusb_ctx.handle_events
   7.220 +#define libusb_handle_events_completed         libusb_ctx.handle_events_completed
   7.221 +
   7.222 +#define hid_device_                     LIBUSB_hid_device_
   7.223 +#define hid_device                      LIBUSB_hid_device
   7.224 +#define hid_device_info                 LIBUSB_hid_device_info
   7.225 +#define hid_init                        LIBUSB_hid_init
   7.226 +#define hid_exit                        LIBUSB_hid_exit
   7.227 +#define hid_enumerate                   LIBUSB_hid_enumerate
   7.228 +#define hid_free_enumeration            LIBUSB_hid_free_enumeration
   7.229 +#define hid_open                        LIBUSB_hid_open
   7.230 +#define hid_open_path                   LIBUSB_hid_open_path
   7.231 +#define hid_write                       LIBUSB_hid_write
   7.232 +#define hid_read_timeout                LIBUSB_hid_read_timeout
   7.233 +#define hid_read                        LIBUSB_hid_read
   7.234 +#define hid_set_nonblocking             LIBUSB_hid_set_nonblocking
   7.235 +#define hid_send_feature_report         LIBUSB_hid_send_feature_report
   7.236 +#define hid_get_feature_report          LIBUSB_hid_get_feature_report
   7.237 +#define hid_close                       LIBUSB_hid_close
   7.238 +#define hid_get_manufacturer_string     LIBUSB_hid_get_manufacturer_string
   7.239 +#define hid_get_product_string          LIBUSB_hid_get_product_string
   7.240 +#define hid_get_serial_number_string    LIBUSB_hid_get_serial_number_string
   7.241 +#define hid_get_indexed_string          LIBUSB_hid_get_indexed_string
   7.242 +#define hid_error                       LIBUSB_hid_error
   7.243 +#define new_hid_device                  LIBUSB_new_hid_device
   7.244 +#define free_hid_device                 LIBUSB_free_hid_device
   7.245 +#define input_report                    LIBUSB_input_report
   7.246 +#define return_data                     LIBUSB_return_data
   7.247 +#define make_path                       LIBUSB_make_path
   7.248 +#define read_thread                     LIBUSB_read_thread
   7.249 +
   7.250 +#ifndef __FreeBSD__
   7.251 +/* this is awkwardly inlined, so we need to re-implement it here
   7.252 + * so we can override the libusb_control_transfer call */
   7.253 +static int
   7.254 +SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
   7.255 +                                 uint8_t descriptor_index, uint16_t lang_id,
   7.256 +                                 unsigned char *data, int length)
   7.257 +{
   7.258 +    return libusb_control_transfer(dev,
   7.259 +                                   LIBUSB_ENDPOINT_IN | 0x0, /* Endpoint 0 IN */
   7.260 +                                   LIBUSB_REQUEST_GET_DESCRIPTOR,
   7.261 +                                   (LIBUSB_DT_STRING << 8) | descriptor_index,
   7.262 +                                   lang_id,
   7.263 +                                   data,
   7.264 +                                   (uint16_t) length,
   7.265 +                                   1000);
   7.266 +}
   7.267 +#define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
   7.268 +#endif /* __FreeBSD__ */
   7.269 +
   7.270 +#undef HIDAPI_H__
   7.271 +#include "libusb/hid.c"
   7.272 +
   7.273 +#undef hid_device_
   7.274 +#undef hid_device
   7.275 +#undef hid_device_info
   7.276 +#undef hid_init
   7.277 +#undef hid_exit
   7.278 +#undef hid_enumerate
   7.279 +#undef hid_free_enumeration
   7.280 +#undef hid_open
   7.281 +#undef hid_open_path
   7.282 +#undef hid_write
   7.283 +#undef hid_read_timeout
   7.284 +#undef hid_read
   7.285 +#undef hid_set_nonblocking
   7.286 +#undef hid_send_feature_report
   7.287 +#undef hid_get_feature_report
   7.288 +#undef hid_close
   7.289 +#undef hid_get_manufacturer_string
   7.290 +#undef hid_get_product_string
   7.291 +#undef hid_get_serial_number_string
   7.292 +#undef hid_get_indexed_string
   7.293 +#undef hid_error
   7.294 +#undef new_hid_device
   7.295 +#undef free_hid_device
   7.296 +#undef input_report
   7.297 +#undef return_data
   7.298 +#undef make_path
   7.299 +#undef read_thread
   7.300 +
   7.301 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.302 +
   7.303 +/* Shared HIDAPI Implementation */
   7.304 +
   7.305 +#undef HIDAPI_H__
   7.306 +#include "hidapi.h"
   7.307 +
   7.308 +struct hidapi_backend {
   7.309 +#define F(x) typeof(x) *x
   7.310 +    F(hid_write);
   7.311 +    F(hid_read_timeout);
   7.312 +    F(hid_read);
   7.313 +    F(hid_set_nonblocking);
   7.314 +    F(hid_send_feature_report);
   7.315 +    F(hid_get_feature_report);
   7.316 +    F(hid_close);
   7.317 +    F(hid_get_manufacturer_string);
   7.318 +    F(hid_get_product_string);
   7.319 +    F(hid_get_serial_number_string);
   7.320 +    F(hid_get_indexed_string);
   7.321 +    F(hid_error);
   7.322 +#undef F
   7.323 +};
   7.324 +
   7.325 +#if HAVE_PLATFORM_BACKEND
   7.326 +static const struct hidapi_backend PLATFORM_Backend = {
   7.327 +    (void*)PLATFORM_hid_write,
   7.328 +    (void*)PLATFORM_hid_read_timeout,
   7.329 +    (void*)PLATFORM_hid_read,
   7.330 +    (void*)PLATFORM_hid_set_nonblocking,
   7.331 +    (void*)PLATFORM_hid_send_feature_report,
   7.332 +    (void*)PLATFORM_hid_get_feature_report,
   7.333 +    (void*)PLATFORM_hid_close,
   7.334 +    (void*)PLATFORM_hid_get_manufacturer_string,
   7.335 +    (void*)PLATFORM_hid_get_product_string,
   7.336 +    (void*)PLATFORM_hid_get_serial_number_string,
   7.337 +    (void*)PLATFORM_hid_get_indexed_string,
   7.338 +    (void*)PLATFORM_hid_error
   7.339 +};
   7.340 +#endif /* HAVE_PLATFORM_BACKEND */
   7.341 +
   7.342 +#ifdef SDL_LIBUSB_DYNAMIC
   7.343 +static const struct hidapi_backend LIBUSB_Backend = {
   7.344 +    (void*)LIBUSB_hid_write,
   7.345 +    (void*)LIBUSB_hid_read_timeout,
   7.346 +    (void*)LIBUSB_hid_read,
   7.347 +    (void*)LIBUSB_hid_set_nonblocking,
   7.348 +    (void*)LIBUSB_hid_send_feature_report,
   7.349 +    (void*)LIBUSB_hid_get_feature_report,
   7.350 +    (void*)LIBUSB_hid_close,
   7.351 +    (void*)LIBUSB_hid_get_manufacturer_string,
   7.352 +    (void*)LIBUSB_hid_get_product_string,
   7.353 +    (void*)LIBUSB_hid_get_serial_number_string,
   7.354 +    (void*)LIBUSB_hid_get_indexed_string,
   7.355 +    (void*)LIBUSB_hid_error
   7.356 +};
   7.357 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.358 +
   7.359 +typedef struct _HIDDeviceWrapper HIDDeviceWrapper;
   7.360 +struct _HIDDeviceWrapper
   7.361 +{
   7.362 +    hid_device *device; /* must be first field */
   7.363 +    const struct hidapi_backend *backend;
   7.364 +};
   7.365 +
   7.366 +static HIDDeviceWrapper *
   7.367 +CreateHIDDeviceWrapper(hid_device *device, const struct hidapi_backend *backend)
   7.368 +{
   7.369 +    HIDDeviceWrapper *ret = SDL_malloc(sizeof(*ret));
   7.370 +    ret->device = device;
   7.371 +    ret->backend = backend;
   7.372 +    return ret;
   7.373 +}
   7.374 +
   7.375 +static hid_device *
   7.376 +WrapHIDDevice(HIDDeviceWrapper *wrapper)
   7.377 +{
   7.378 +    return (hid_device *)wrapper;
   7.379 +}
   7.380 +
   7.381 +static HIDDeviceWrapper *
   7.382 +UnwrapHIDDevice(hid_device *device)
   7.383 +{
   7.384 +    return (HIDDeviceWrapper *)device;
   7.385 +}
   7.386 +
   7.387 +static void
   7.388 +DeleteHIDDeviceWrapper(HIDDeviceWrapper *device)
   7.389 +{
   7.390 +    SDL_free(device);
   7.391 +}
   7.392 +
   7.393 +#define COPY_IF_EXISTS(var) \
   7.394 +    if (pSrc->var != NULL) { \
   7.395 +        pDst->var = SDL_strdup(pSrc->var); \
   7.396 +    } else { \
   7.397 +        pDst->var = NULL; \
   7.398 +    }
   7.399 +#define WCOPY_IF_EXISTS(var) \
   7.400 +    if (pSrc->var != NULL) { \
   7.401 +        pDst->var = SDL_wcsdup(pSrc->var); \
   7.402 +    } else { \
   7.403 +        pDst->var = NULL; \
   7.404 +    }
   7.405 +
   7.406 +#ifdef SDL_LIBUSB_DYNAMIC
   7.407 +static void
   7.408 +LIBUSB_CopyHIDDeviceInfo(struct LIBUSB_hid_device_info *pSrc,
   7.409 +                         struct hid_device_info *pDst)
   7.410 +{
   7.411 +    COPY_IF_EXISTS(path)
   7.412 +    pDst->vendor_id = pSrc->vendor_id;
   7.413 +    pDst->product_id = pSrc->product_id;
   7.414 +    WCOPY_IF_EXISTS(serial_number)
   7.415 +    pDst->release_number = pSrc->release_number;
   7.416 +    WCOPY_IF_EXISTS(manufacturer_string)
   7.417 +    WCOPY_IF_EXISTS(product_string)
   7.418 +    pDst->usage_page = pSrc->usage_page;
   7.419 +    pDst->usage = pSrc->usage;
   7.420 +    pDst->interface_number = pSrc->interface_number;
   7.421 +    pDst->next = NULL;
   7.422 +}
   7.423 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.424 +
   7.425 +#if HAVE_PLATFORM_BACKEND
   7.426 +static void
   7.427 +PLATFORM_CopyHIDDeviceInfo(struct PLATFORM_hid_device_info *pSrc,
   7.428 +                           struct hid_device_info *pDst)
   7.429 +{
   7.430 +    COPY_IF_EXISTS(path)
   7.431 +    pDst->vendor_id = pSrc->vendor_id;
   7.432 +    pDst->product_id = pSrc->product_id;
   7.433 +    WCOPY_IF_EXISTS(serial_number)
   7.434 +    pDst->release_number = pSrc->release_number;
   7.435 +    WCOPY_IF_EXISTS(manufacturer_string)
   7.436 +    WCOPY_IF_EXISTS(product_string)
   7.437 +    pDst->usage_page = pSrc->usage_page;
   7.438 +    pDst->usage = pSrc->usage;
   7.439 +    pDst->interface_number = pSrc->interface_number;
   7.440 +    pDst->next = NULL;
   7.441 +}
   7.442 +#endif /* HAVE_PLATFORM_BACKEND */
   7.443 +
   7.444 +#undef COPY_IF_EXISTS
   7.445 +#undef WCOPY_IF_EXISTS
   7.446 +
   7.447 +static SDL_bool SDL_hidapi_wasinit = SDL_FALSE;
   7.448 +
   7.449 +int HID_API_EXPORT HID_API_CALL hid_init(void)
   7.450 +{
   7.451 +    int err;
   7.452 +
   7.453 +    if (SDL_hidapi_wasinit == SDL_TRUE) {
   7.454 +        return 0;
   7.455 +    }
   7.456 +
   7.457 +#ifdef SDL_LIBUSB_DYNAMIC
   7.458 +    libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
   7.459 +    if (libusb_ctx.libhandle != NULL) {
   7.460 +        #define LOAD_LIBUSB_SYMBOL(func) \
   7.461 +            libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func);
   7.462 +        LOAD_LIBUSB_SYMBOL(init)
   7.463 +        LOAD_LIBUSB_SYMBOL(exit)
   7.464 +        LOAD_LIBUSB_SYMBOL(get_device_list)
   7.465 +        LOAD_LIBUSB_SYMBOL(free_device_list)
   7.466 +        LOAD_LIBUSB_SYMBOL(get_device_descriptor)
   7.467 +        LOAD_LIBUSB_SYMBOL(get_active_config_descriptor)
   7.468 +        LOAD_LIBUSB_SYMBOL(get_config_descriptor)
   7.469 +        LOAD_LIBUSB_SYMBOL(free_config_descriptor)
   7.470 +        LOAD_LIBUSB_SYMBOL(get_bus_number)
   7.471 +        LOAD_LIBUSB_SYMBOL(get_device_address)
   7.472 +        LOAD_LIBUSB_SYMBOL(open)
   7.473 +        LOAD_LIBUSB_SYMBOL(close)
   7.474 +        LOAD_LIBUSB_SYMBOL(claim_interface)
   7.475 +        LOAD_LIBUSB_SYMBOL(release_interface)
   7.476 +        LOAD_LIBUSB_SYMBOL(kernel_driver_active)
   7.477 +        LOAD_LIBUSB_SYMBOL(detach_kernel_driver)
   7.478 +        LOAD_LIBUSB_SYMBOL(alloc_transfer)
   7.479 +        LOAD_LIBUSB_SYMBOL(submit_transfer)
   7.480 +        LOAD_LIBUSB_SYMBOL(cancel_transfer)
   7.481 +        LOAD_LIBUSB_SYMBOL(free_transfer)
   7.482 +        LOAD_LIBUSB_SYMBOL(control_transfer)
   7.483 +        LOAD_LIBUSB_SYMBOL(interrupt_transfer)
   7.484 +        LOAD_LIBUSB_SYMBOL(handle_events)
   7.485 +        LOAD_LIBUSB_SYMBOL(handle_events_completed)
   7.486 +        #undef LOAD_LIBUSB_SYMBOL
   7.487 +
   7.488 +        if ((err = LIBUSB_hid_init()) < 0) {
   7.489 +            SDL_UnloadObject(libusb_ctx.libhandle);
   7.490 +            return err;
   7.491 +        }
   7.492 +    }
   7.493 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.494 +
   7.495 +#if HAVE_PLATFORM_BACKEND
   7.496 +#if __LINUX__
   7.497 +    udev_ctx = SDL_UDEV_GetUdevSyms();
   7.498 +#endif /* __LINUX __ */
   7.499 +    if (udev_ctx && (err = PLATFORM_hid_init()) < 0) {
   7.500 +#ifdef SDL_LIBUSB_DYNAMIC
   7.501 +        if (libusb_ctx.libhandle) {
   7.502 +            SDL_UnloadObject(libusb_ctx.libhandle);
   7.503 +        }
   7.504 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.505 +        return err;
   7.506 +    }
   7.507 +#endif /* HAVE_PLATFORM_BACKEND */
   7.508 +
   7.509 +    return 0;
   7.510 +}
   7.511 +
   7.512 +int HID_API_EXPORT HID_API_CALL hid_exit(void)
   7.513 +{
   7.514 +    int err = 0;
   7.515 +
   7.516 +    if (SDL_hidapi_wasinit == SDL_FALSE) {
   7.517 +        return 0;
   7.518 +    }
   7.519 +
   7.520 +#if HAVE_PLATFORM_BACKEND
   7.521 +    if (udev_ctx) {
   7.522 +        err = PLATFORM_hid_exit();
   7.523 +    }
   7.524 +#endif /* HAVE_PLATFORM_BACKEND */
   7.525 +#ifdef SDL_LIBUSB_DYNAMIC
   7.526 +    if (libusb_ctx.libhandle) {
   7.527 +        err |= LIBUSB_hid_exit(); /* Ehhhhh */
   7.528 +        SDL_UnloadObject(libusb_ctx.libhandle);
   7.529 +    }
   7.530 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.531 +    return err;
   7.532 +}
   7.533 +
   7.534 +struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
   7.535 +{
   7.536 +#if HAVE_PLATFORM_BACKEND
   7.537 +    struct PLATFORM_hid_device_info *raw_devs = NULL;
   7.538 +    struct PLATFORM_hid_device_info *raw_dev;
   7.539 +#endif /* HAVE_PLATFORM_BACKEND */
   7.540 +    struct hid_device_info *devs = NULL, *last = NULL, *new_dev;
   7.541 +    SDL_bool bFound;
   7.542 +
   7.543 +    if (SDL_hidapi_wasinit == SDL_FALSE) {
   7.544 +        hid_init();
   7.545 +    }
   7.546 +
   7.547 +#if HAVE_PLATFORM_BACKEND
   7.548 +    if (udev_ctx) {
   7.549 +        raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
   7.550 +    }
   7.551 +#endif /* HAVE_PLATFORM_BACKEND */
   7.552 +
   7.553 +#ifdef SDL_LIBUSB_DYNAMIC
   7.554 +    if (libusb_ctx.libhandle) {
   7.555 +        struct LIBUSB_hid_device_info *usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
   7.556 +        struct LIBUSB_hid_device_info *usb_dev;
   7.557 +        for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
   7.558 +            bFound = SDL_FALSE;
   7.559 +#if HAVE_PLATFORM_BACKEND
   7.560 +            for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
   7.561 +                if (usb_dev->vendor_id == raw_dev->vendor_id &&
   7.562 +                    usb_dev->product_id == raw_dev->product_id) {
   7.563 +
   7.564 +                    bFound = SDL_TRUE;
   7.565 +                    break;
   7.566 +                }
   7.567 +            }
   7.568 +#endif
   7.569 +
   7.570 +            if (!bFound) {
   7.571 +                new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
   7.572 +                LIBUSB_CopyHIDDeviceInfo(usb_dev, new_dev);
   7.573 +
   7.574 +                if (last != NULL) {
   7.575 +                    last->next = new_dev;
   7.576 +                } else {
   7.577 +                    devs = new_dev;
   7.578 +                }
   7.579 +                last = new_dev;
   7.580 +            }
   7.581 +        }
   7.582 +        LIBUSB_hid_free_enumeration(usb_devs);
   7.583 +    }
   7.584 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.585 +
   7.586 +#if HAVE_PLATFORM_BACKEND
   7.587 +    if (udev_ctx) {
   7.588 +        for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
   7.589 +            new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
   7.590 +            PLATFORM_CopyHIDDeviceInfo(raw_dev, new_dev);
   7.591 +            new_dev->next = NULL;
   7.592 +
   7.593 +            if (last != NULL) {
   7.594 +                last->next = new_dev;
   7.595 +            } else {
   7.596 +                devs = new_dev;
   7.597 +            }
   7.598 +            last = new_dev;
   7.599 +        }
   7.600 +        PLATFORM_hid_free_enumeration(raw_devs);
   7.601 +    }
   7.602 +#endif /* HAVE_PLATFORM_BACKEND */
   7.603 +
   7.604 +    return devs;
   7.605 +}
   7.606 +
   7.607 +void  HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
   7.608 +{
   7.609 +    while (devs) {
   7.610 +        struct hid_device_info *next = devs->next;
   7.611 +        SDL_free(devs->path);
   7.612 +        SDL_free(devs->serial_number);
   7.613 +        SDL_free(devs->manufacturer_string);
   7.614 +        SDL_free(devs->product_string);
   7.615 +        SDL_free(devs);
   7.616 +        devs = next;
   7.617 +    }
   7.618 +}
   7.619 +
   7.620 +HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
   7.621 +{
   7.622 +    hid_device *pDevice = NULL;
   7.623 +
   7.624 +    if (SDL_hidapi_wasinit == SDL_FALSE) {
   7.625 +        hid_init();
   7.626 +    }
   7.627 +
   7.628 +#if HAVE_PLATFORM_BACKEND
   7.629 +    if (udev_ctx &&
   7.630 +        (pDevice = (hid_device*) PLATFORM_hid_open(vendor_id, product_id, serial_number)) != NULL) {
   7.631 +
   7.632 +        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
   7.633 +        return WrapHIDDevice(wrapper);
   7.634 +    }
   7.635 +#endif /* HAVE_PLATFORM_BACKEND */
   7.636 +#ifdef SDL_LIBUSB_DYNAMIC
   7.637 +    if (libusb_ctx.libhandle &&
   7.638 +        (pDevice = (hid_device*) LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
   7.639 +
   7.640 +        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
   7.641 +        return WrapHIDDevice(wrapper);
   7.642 +    }
   7.643 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.644 +    return NULL;
   7.645 +}
   7.646 +
   7.647 +HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path, int bExclusive /* = false */)
   7.648 +{
   7.649 +    hid_device *pDevice = NULL;
   7.650 +
   7.651 +    if (SDL_hidapi_wasinit == SDL_FALSE) {
   7.652 +        hid_init();
   7.653 +    }
   7.654 +
   7.655 +#if HAVE_PLATFORM_BACKEND
   7.656 +    if (udev_ctx &&
   7.657 +        (pDevice = (hid_device*) PLATFORM_hid_open_path(path, bExclusive)) != NULL) {
   7.658 +
   7.659 +        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
   7.660 +        return WrapHIDDevice(wrapper);
   7.661 +    }
   7.662 +#endif /* HAVE_PLATFORM_BACKEND */
   7.663 +#ifdef SDL_LIBUSB_DYNAMIC
   7.664 +    if (libusb_ctx.libhandle &&
   7.665 +        (pDevice = (hid_device*) LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
   7.666 +
   7.667 +        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
   7.668 +        return WrapHIDDevice(wrapper);
   7.669 +    }
   7.670 +#endif /* SDL_LIBUSB_DYNAMIC */
   7.671 +    return NULL;
   7.672 +}
   7.673 +
   7.674 +int  HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
   7.675 +{
   7.676 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.677 +    return wrapper->backend->hid_write(wrapper->device, data, length);
   7.678 +}
   7.679 +
   7.680 +int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
   7.681 +{
   7.682 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.683 +    return wrapper->backend->hid_read_timeout(wrapper->device, data, length, milliseconds);
   7.684 +}
   7.685 +
   7.686 +int  HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
   7.687 +{
   7.688 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.689 +    return wrapper->backend->hid_read(wrapper->device, data, length);
   7.690 +}
   7.691 +
   7.692 +int  HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
   7.693 +{
   7.694 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.695 +    return wrapper->backend->hid_set_nonblocking(wrapper->device, nonblock);
   7.696 +}
   7.697 +
   7.698 +int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
   7.699 +{
   7.700 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.701 +    return wrapper->backend->hid_send_feature_report(wrapper->device, data, length);
   7.702 +}
   7.703 +
   7.704 +int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
   7.705 +{
   7.706 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.707 +    return wrapper->backend->hid_get_feature_report(wrapper->device, data, length);
   7.708 +}
   7.709 +
   7.710 +void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
   7.711 +{
   7.712 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.713 +    wrapper->backend->hid_close(wrapper->device);
   7.714 +    DeleteHIDDeviceWrapper(wrapper);
   7.715 +}
   7.716 +
   7.717 +int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
   7.718 +{
   7.719 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.720 +    return wrapper->backend->hid_get_manufacturer_string(wrapper->device, string, maxlen);
   7.721 +}
   7.722 +
   7.723 +int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
   7.724 +{
   7.725 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.726 +    return wrapper->backend->hid_get_product_string(wrapper->device, string, maxlen);
   7.727 +}
   7.728 +
   7.729 +int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
   7.730 +{
   7.731 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.732 +    return wrapper->backend->hid_get_serial_number_string(wrapper->device, string, maxlen);
   7.733 +}
   7.734 +
   7.735 +int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
   7.736 +{
   7.737 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.738 +    return wrapper->backend->hid_get_indexed_string(wrapper->device, string_index, string, maxlen);
   7.739 +}
   7.740 +
   7.741 +HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device)
   7.742 +{
   7.743 +    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
   7.744 +    return wrapper->backend->hid_error(wrapper->device);
   7.745 +}
   7.746 +
   7.747 +#endif /* SDL_JOYSTICK_HIDAPI */