Commit 3f89d1704d84f20253e0c86a8af2aebdca6f5409

Sam Lantinga 2022-09-01T11:30:02

Fixed building with libusb not dynamicaly loaded

diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 7e25075..80f31c7 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -1186,7 +1186,7 @@ macro(CheckHIDAPI)
           set(HAVE_LIBUSB TRUE)
           set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBUSB_CFLAGS}")
           if(HIDAPI_ONLY_LIBUSB)
-            list(APPEND EXTRA_LIBS ${LIBUSB_LIBS})
+            list(APPEND EXTRA_LIBS ${LIBUSB_LIBRARIES})
           elseif(OS2)
             set(SDL_LIBUSB_DYNAMIC "\"usb100.dll\"")
           else()
diff --git a/configure.ac b/configure.ac
index b539f61..d27fa9a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3581,6 +3581,7 @@ CheckHIDAPI()
 
         if test x$hidapi_support = xyes; then
             if test x$have_libusb_h = xyes; then
+                AC_DEFINE(HAVE_LIBUSB)
                 EXTRA_CFLAGS="$EXTRA_CFLAGS $LIBUSB_CFLAGS"
                 if test x$require_hidapi_libusb = xyes; then
                     EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBUSB_LIBS"
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 2d21b37..204a12e 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -224,6 +224,7 @@
 #cmakedefine HAVE_INOTIFY_INIT 1
 #cmakedefine HAVE_INOTIFY_INIT1 1
 #cmakedefine HAVE_INOTIFY 1
+#cmakedefine HAVE_LIBUSB 1
 #cmakedefine HAVE_O_CLOEXEC 1
 
 /* Apple platforms might be building universal binaries, where Intel builds
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index 7b6eb5d..56b5302 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -227,6 +227,7 @@
 #undef HAVE_IBUS_IBUS_H
 #undef HAVE_IMMINTRIN_H
 #undef HAVE_LIBUDEV_H
+#undef HAVE_LIBUSB
 #undef HAVE_LIBSAMPLERATE_H
 #undef HAVE_LIBDECOR_H
 #undef HAVE_LSXINTRIN_H
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index 82abcbd..b668396 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -682,7 +682,7 @@ static const SDL_UDEV_Symbols *udev_ctx = NULL;
 #endif /* HAVE_DRIVER_BACKEND */
 
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
 /* libusb HIDAPI Implementation */
 
 /* Include this now, for our dynamically-loaded libusb context */
@@ -817,6 +817,33 @@ SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
 #undef HIDAPI_H__
 #include "libusb/hid.c"
 
+#undef libusb_init
+#undef libusb_exit
+#undef libusb_get_device_list
+#undef libusb_free_device_list
+#undef libusb_get_device_descriptor
+#undef libusb_get_active_config_descriptor
+#undef libusb_get_config_descriptor
+#undef libusb_free_config_descriptor
+#undef libusb_get_bus_number
+#undef libusb_get_device_address
+#undef libusb_open
+#undef libusb_close
+#undef libusb_claim_interface
+#undef libusb_release_interface
+#undef libusb_kernel_driver_active
+#undef libusb_detach_kernel_driver
+#undef libusb_attach_kernel_driver
+#undef libusb_set_interface_alt_setting
+#undef libusb_alloc_transfer
+#undef libusb_submit_transfer
+#undef libusb_cancel_transfer
+#undef libusb_free_transfer
+#undef libusb_control_transfer
+#undef libusb_interrupt_transfer
+#undef libusb_handle_events
+#undef libusb_handle_events_completed
+
 #undef hid_device
 #undef hid_device_
 #undef hid_init
@@ -844,7 +871,7 @@ SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
 #undef make_path
 #undef read_thread
 
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
 #endif /* !SDL_HIDAPI_DISABLED */
 
@@ -899,7 +926,7 @@ static const struct hidapi_backend DRIVER_Backend = {
 };
 #endif /* HAVE_DRIVER_BACKEND */
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
 static const struct hidapi_backend LIBUSB_Backend = {
     (void*)LIBUSB_hid_write,
     (void*)LIBUSB_hid_read_timeout,
@@ -914,7 +941,7 @@ static const struct hidapi_backend LIBUSB_Backend = {
     (void*)LIBUSB_hid_get_indexed_string,
     (void*)LIBUSB_hid_error
 };
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
 struct SDL_hid_device_
 {
@@ -924,7 +951,7 @@ struct SDL_hid_device_
 };
 static char device_magic;
 
-#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
+#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
 
 static SDL_hid_device *
 CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
@@ -936,7 +963,7 @@ CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
     return wrapper;
 }
 
-#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
 
 static void
 DeleteHIDDeviceWrapper(SDL_hid_device *device)
@@ -952,7 +979,7 @@ DeleteHIDDeviceWrapper(SDL_hid_device *device)
     }
 
 #if !SDL_HIDAPI_DISABLED
-#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
+#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
 
 #define COPY_IF_EXISTS(var) \
     if (pSrc->var != NULL) { \
@@ -989,7 +1016,7 @@ CopyHIDDeviceInfo(struct SDL_hid_device_info *pSrc, struct SDL_hid_device_info *
 #undef COPY_IF_EXISTS
 #undef WCOPY_IF_EXISTS
 
-#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
 #endif /* !SDL_HIDAPI_DISABLED */
 
 static int SDL_hidapi_refcount = 0;
@@ -1030,23 +1057,32 @@ int SDL_hid_init(void)
     }
 #endif
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     if (SDL_getenv("SDL_HIDAPI_DISABLE_LIBUSB") != NULL) {
         SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
                      "libusb disabled by SDL_HIDAPI_DISABLE_LIBUSB");
         libusb_ctx.libhandle = NULL;
     } else {
         ++attempts;
+#ifdef SDL_LIBUSB_DYNAMIC
         libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
+#else
+        libusb_ctx.libhandle = (void *)1;
+#endif
         if (libusb_ctx.libhandle != NULL) {
             SDL_bool loaded = SDL_TRUE;
             #ifdef __OS2__
+#ifdef SDL_LIBUSB_DYNAMIC
             #define LOAD_LIBUSB_SYMBOL(func) \
                 if (!(libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle,"_libusb_" #func))) {loaded = SDL_FALSE;}
             #else
             #define LOAD_LIBUSB_SYMBOL(func) \
                 if (!(libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) {loaded = SDL_FALSE;}
             #endif
+#else
+            #define LOAD_LIBUSB_SYMBOL(func) \
+                libusb_ctx.func = libusb_##func;
+#endif
             LOAD_LIBUSB_SYMBOL(init)
             LOAD_LIBUSB_SYMBOL(exit)
             LOAD_LIBUSB_SYMBOL(get_device_list)
@@ -1076,18 +1112,22 @@ int SDL_hid_init(void)
             #undef LOAD_LIBUSB_SYMBOL
 
             if (!loaded) {
+#ifdef SDL_LIBUSB_DYNAMIC
                 SDL_UnloadObject(libusb_ctx.libhandle);
+#endif
                 libusb_ctx.libhandle = NULL;
                 /* SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function"); */
             } else if (LIBUSB_hid_init() < 0) {
+#ifdef SDL_LIBUSB_DYNAMIC
                 SDL_UnloadObject(libusb_ctx.libhandle);
+#endif
                 libusb_ctx.libhandle = NULL;
             } else {
                 ++success;
             }
         }
     }
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
 #if HAVE_PLATFORM_BACKEND
     ++attempts;
@@ -1133,13 +1173,15 @@ int SDL_hid_exit(void)
 #endif /* __LINUX __ */
 #endif /* HAVE_PLATFORM_BACKEND */
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     if (libusb_ctx.libhandle) {
         result |= LIBUSB_hid_exit();
+#ifdef SDL_LIBUSB_DYNAMIC
         SDL_UnloadObject(libusb_ctx.libhandle);
+#endif
         libusb_ctx.libhandle = NULL;
     }
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
     return result;
 }
@@ -1168,8 +1210,8 @@ Uint32 SDL_hid_device_change_count(void)
 
 struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
 {
-#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
-#ifdef SDL_LIBUSB_DYNAMIC
+#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
+#ifdef HAVE_LIBUSB
     struct SDL_hid_device_info *usb_devs = NULL;
     struct SDL_hid_device_info *usb_dev;
 #endif
@@ -1187,7 +1229,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
         return NULL;
     }
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     if (libusb_ctx.libhandle) {
         usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
 #ifdef DEBUG_HIDAPI
@@ -1216,7 +1258,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
             last = new_dev;
         }
     }
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
 #ifdef HAVE_DRIVER_BACKEND
     driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
@@ -1246,7 +1288,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
                     raw_dev->manufacturer_string, raw_dev->product_string,
                     raw_dev->vendor_id, raw_dev->product_id);
 #endif
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
             for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
                 if (raw_dev->vendor_id == usb_dev->vendor_id &&
                     raw_dev->product_id == usb_dev->product_id &&
@@ -1269,7 +1311,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
             if (!bFound) {
                 new_dev = (struct SDL_hid_device_info*) SDL_malloc(sizeof(struct SDL_hid_device_info));
                 if (!new_dev) {
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
                     if (libusb_ctx.libhandle) {
                         LIBUSB_hid_free_enumeration(usb_devs);
                     }
@@ -1294,7 +1336,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
     }
 #endif /* HAVE_PLATFORM_BACKEND */
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     if (libusb_ctx.libhandle) {
         LIBUSB_hid_free_enumeration(usb_devs);
     }
@@ -1303,7 +1345,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned
 
 #else
     return NULL;
-#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
 }
 
 void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
@@ -1321,7 +1363,7 @@ void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
 
 SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
 {
-#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
+#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
     void *pDevice = NULL;
 
     if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
@@ -1341,21 +1383,21 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
     }
 #endif /* HAVE_DRIVER_BACKEND */
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     if (libusb_ctx.libhandle &&
         (pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
         return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
     }
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
-#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
 
     return NULL;
 }
 
 SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */)
 {
-#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
+#if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
     void *pDevice = NULL;
 
     if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
@@ -1375,14 +1417,14 @@ SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */
     }
 #endif /* HAVE_DRIVER_BACKEND */
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     if (libusb_ctx.libhandle &&
         (pDevice = LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
         return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
     }
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 
-#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
 
     return NULL;
 }
@@ -1536,7 +1578,7 @@ void SDL_hid_ble_scan(SDL_bool active)
 /* This is needed to enable input for Nyko and EVORETRO GameCube adaptors */
 void SDL_EnableGameCubeAdaptors(void)
 {
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
     libusb_context *context = NULL;
     libusb_device **devs = NULL;
     libusb_device_handle *handle = NULL;
@@ -1585,7 +1627,7 @@ void SDL_EnableGameCubeAdaptors(void)
 
         libusb_exit(context);
     }
-#endif /* SDL_LIBUSB_DYNAMIC */
+#endif /* HAVE_LIBUSB */
 }
 #endif /* HAVE_ENABLE_GAMECUBE_ADAPTORS */
 
diff --git a/src/hidapi/SDL_hidapi_c.h b/src/hidapi/SDL_hidapi_c.h
index 1b40942..c7343b1 100644
--- a/src/hidapi/SDL_hidapi_c.h
+++ b/src/hidapi/SDL_hidapi_c.h
@@ -22,7 +22,7 @@
 
 #ifdef SDL_JOYSTICK_HIDAPI
 
-#ifdef SDL_LIBUSB_DYNAMIC
+#ifdef HAVE_LIBUSB
 #define HAVE_ENABLE_GAMECUBE_ADAPTORS
 #endif