SDL: added missing hidapi stuff for os/2 with libusb

From f14c7bb443dd6c85dbf43c50b5a65cc32157b25e Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sun, 9 Jan 2022 20:33:32 +0300
Subject: [PATCH] added missing hidapi stuff for os/2 with libusb

---
 Makefile.os2             | 9 +++++++++
 configure                | 7 +++++++
 configure.ac             | 7 +++++++
 include/SDL_config_os2.h | 6 ++++++
 src/hidapi/SDL_hidapi.c  | 5 +++++
 5 files changed, 34 insertions(+)

diff --git a/Makefile.os2 b/Makefile.os2
index 856bec8ccc7..6af282d5283 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -4,6 +4,11 @@
 # If you have GNU libiconv installed (iconv2.dll), you
 # can compile against it by specifying LIBICONV=1, e.g.:
 # wmake -f Makefile.os2 LIBICONV=1
+#
+# If you have libusb-1.0 installed (usb100.dll, libusb.h), you
+# can compile hidapi joystick support against it (experimental)
+# by specifying HIDAPI=1, e.g.:
+# wmake -f Makefile.os2 HIDAPI=1
 
 LIBNAME = SDL2
 VERSION = 2.0.20
@@ -42,6 +47,10 @@ LIBS+= $(ICONVLIB)
 !else
 LIBS+= libuls.lib libconv.lib
 !endif
+# hidapi (libusb):
+!ifeq HIDAPI 1
+CFLAGS_DLL+= -DHAVE_LIBUSB_H=1
+!endif
 # building SDL itself (for DECLSPEC):
 CFLAGS_DLL+= -DBUILD_SDL
 
diff --git a/configure b/configure
index 0da00de854e..a02b8f93b97 100755
--- a/configure
+++ b/configure
@@ -25403,6 +25403,9 @@ fi
                 enable_hidapi_libusb=yes
                 require_hidapi_libusb=yes
                 ;;
+           *-*-os2* )
+                enable_hidapi_libusb=yes
+                ;;
         esac
 
         hidapi_support=yes
@@ -25513,6 +25516,9 @@ $as_echo "$as_me: WARNING: You must have SDL_LoadObject() support for dynamic li
                         *-*-cygwin* | *-*-mingw* )
                             libusb_lib="libusb-1.0.dll"
                             ;;
+                        *-*-os2* )
+                            libusb_lib="usb100.dll"
+                            ;;
                     esac
                     if test x$libusb_lib = x; then
                         libusb_lib=`find_lib "libusb-1.0.so.*" "" | sed 's/.*\/\(.*\)/\1/; q'`
@@ -26720,6 +26726,7 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
         CheckDummyVideo
         CheckDiskAudio
         CheckDummyAudio
+        CheckHIDAPI
 
         # Set up the core platform files
         SOURCES="$SOURCES $srcdir/src/core/os2/*.c"
diff --git a/configure.ac b/configure.ac
index 689f54639ec..3c1c6c85838 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3489,6 +3489,9 @@ CheckHIDAPI()
                 enable_hidapi_libusb=yes
                 require_hidapi_libusb=yes
                 ;;
+           *-*-os2* )
+                enable_hidapi_libusb=yes
+                ;;
         esac
 
         hidapi_support=yes
@@ -3521,6 +3524,9 @@ CheckHIDAPI()
                         *-*-cygwin* | *-*-mingw* )
                             libusb_lib="libusb-1.0.dll"
                             ;;
+                        *-*-os2* )
+                            libusb_lib="usb100.dll"
+                            ;;
                     esac
                     if test x$libusb_lib = x; then
                         libusb_lib=[`find_lib "libusb-1.0.so.*" "" | sed 's/.*\/\(.*\)/\1/; q'`]
@@ -4429,6 +4435,7 @@ case "$host" in
         CheckDummyVideo
         CheckDiskAudio
         CheckDummyAudio
+        CheckHIDAPI
 
         # Set up the core platform files
         SOURCES="$SOURCES $srcdir/src/core/os2/*.c"
diff --git a/include/SDL_config_os2.h b/include/SDL_config_os2.h
index 7c2342d3db6..1728bd772bc 100644
--- a/include/SDL_config_os2.h
+++ b/include/SDL_config_os2.h
@@ -38,8 +38,14 @@
 #define SDL_VIDEO_DRIVER_DUMMY 1
 #define SDL_VIDEO_DRIVER_OS2 1
 #define SDL_JOYSTICK_OS2 1
+#ifndef HAVE_LIBUSB_H  /* see Makefile */
 #define SDL_HIDAPI_DISABLED 1
 /*#undef SDL_JOYSTICK_HIDAPI */
+#else
+#define SDL_JOYSTICK_HIDAPI 1
+/* dynamically loaded libusb-1.0 dll: */
+#define SDL_LIBUSB_DYNAMIC "usb100.dll"
+#endif
 /*#undef SDL_JOYSTICK_VIRTUAL */
 
 /* Enable OpenGL support */
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index 2ddce67413c..aacff63bf03 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -1036,8 +1036,13 @@ int SDL_hid_init(void)
     libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
     if (libusb_ctx.libhandle != NULL) {
         SDL_bool loaded = SDL_TRUE;
+        #ifdef __OS2__
+        #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
         LOAD_LIBUSB_SYMBOL(init)
         LOAD_LIBUSB_SYMBOL(exit)
         LOAD_LIBUSB_SYMBOL(get_device_list)