SDL: Added Android hidapi 0.14.0 support

From bb12c6e03eb529709ff2890d27995da47c26260b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 24 May 2023 18:08:34 -0700
Subject: [PATCH] Added Android hidapi 0.14.0 support

---
 .../main/java/org/libsdl/app/HIDDevice.java   |    5 +-
 .../app/HIDDeviceBLESteamController.java      |   49 +-
 .../java/org/libsdl/app/HIDDeviceManager.java |   31 +-
 .../java/org/libsdl/app/HIDDeviceUSB.java     |   77 +-
 src/hidapi/android/hid.cpp                    | 1457 +++++++++++++++++
 src/hidapi/android/hid.h                      |   39 +
 6 files changed, 1565 insertions(+), 93 deletions(-)
 create mode 100644 src/hidapi/android/hid.cpp
 create mode 100644 src/hidapi/android/hid.h

diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDevice.java b/android-project/app/src/main/java/org/libsdl/app/HIDDevice.java
index 955df5d14c08..f96095324b85 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDevice.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDevice.java
@@ -13,9 +13,8 @@
     public String getProductName();
     public UsbDevice getDevice();
     public boolean open();
-    public int sendFeatureReport(byte[] report);
-    public int sendOutputReport(byte[] report);
-    public boolean getFeatureReport(byte[] report);
+    public int writeReport(byte[] report, boolean feature);
+    public boolean readReport(byte[] report, boolean feature);
     public void setFrozen(boolean frozen);
     public void close();
     public void shutdown();
diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java
index ee5521fd5e3f..a7b85d0cf857 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java
@@ -457,7 +457,7 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
         //Log.v(TAG, "onCharacteristicRead status=" + status + " uuid=" + characteristic.getUuid());
 
         if (characteristic.getUuid().equals(reportCharacteristic) && !mFrozen) {
-            mManager.HIDDeviceFeatureReport(getId(), characteristic.getValue());
+            mManager.HIDDeviceReportResponse(getId(), characteristic.getValue());
         }
 
         finishCurrentGattOperation();
@@ -575,50 +575,45 @@ public boolean open() {
     }
 
     @Override
-    public int sendFeatureReport(byte[] report) {
+    public int writeReport(byte[] report, boolean feature) {
         if (!isRegistered()) {
-            Log.e(TAG, "Attempted sendFeatureReport before Steam Controller is registered!");
+            Log.e(TAG, "Attempted writeReport before Steam Controller is registered!");
             if (mIsConnected) {
                 probeService(this);
             }
             return -1;
         }
 
-        // We need to skip the first byte, as that doesn't go over the air
-        byte[] actual_report = Arrays.copyOfRange(report, 1, report.length - 1);
-        //Log.v(TAG, "sendFeatureReport " + HexDump.dumpHexString(actual_report));
-        writeCharacteristic(reportCharacteristic, actual_report);
-        return report.length;
-    }
-
-    @Override
-    public int sendOutputReport(byte[] report) {
-        if (!isRegistered()) {
-            Log.e(TAG, "Attempted sendOutputReport before Steam Controller is registered!");
-            if (mIsConnected) {
-                probeService(this);
-            }
-            return -1;
+        if (feature) {
+            // We need to skip the first byte, as that doesn't go over the air
+            byte[] actual_report = Arrays.copyOfRange(report, 1, report.length - 1);
+            //Log.v(TAG, "writeFeatureReport " + HexDump.dumpHexString(actual_report));
+            writeCharacteristic(reportCharacteristic, actual_report);
+            return report.length;
+        } else {
+            //Log.v(TAG, "writeOutputReport " + HexDump.dumpHexString(report));
+            writeCharacteristic(reportCharacteristic, report);
+            return report.length;
         }
-
-        //Log.v(TAG, "sendFeatureReport " + HexDump.dumpHexString(report));
-        writeCharacteristic(reportCharacteristic, report);
-        return report.length;
     }
 
     @Override
-    public boolean getFeatureReport(byte[] report) {
+    public boolean readReport(byte[] report, boolean feature) {
         if (!isRegistered()) {
-            Log.e(TAG, "Attempted getFeatureReport before Steam Controller is registered!");
+            Log.e(TAG, "Attempted readReport before Steam Controller is registered!");
             if (mIsConnected) {
                 probeService(this);
             }
             return false;
         }
 
-        //Log.v(TAG, "getFeatureReport");
-        readCharacteristic(reportCharacteristic);
-        return true;
+        if (feature) {
+            readCharacteristic(reportCharacteristic);
+            return true;
+        } else {
+            // Not implemented
+            return false;
+        }
     }
 
     @Override
diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
index 4054bd73e3da..f79bf1862ce1 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
@@ -599,9 +599,9 @@ public boolean openDevice(int deviceID) {
         return false;
     }
 
-    public int sendOutputReport(int deviceID, byte[] report) {
+    public int writeReport(int deviceID, byte[] report, boolean feature) {
         try {
-            //Log.v(TAG, "sendOutputReport deviceID=" + deviceID + " length=" + report.length);
+            //Log.v(TAG, "writeReport deviceID=" + deviceID + " length=" + report.length);
             HIDDevice device;
             device = getDevice(deviceID);
             if (device == null) {
@@ -609,33 +609,16 @@ public int sendOutputReport(int deviceID, byte[] report) {
                 return -1;
             }
 
-            return device.sendOutputReport(report);
+            return device.writeReport(report, feature);
         } catch (Exception e) {
             Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
         }
         return -1;
     }
 
-    public int sendFeatureReport(int deviceID, byte[] report) {
+    public boolean readReport(int deviceID, byte[] report, boolean feature) {
         try {
-            //Log.v(TAG, "sendFeatureReport deviceID=" + deviceID + " length=" + report.length);
-            HIDDevice device;
-            device = getDevice(deviceID);
-            if (device == null) {
-                HIDDeviceDisconnected(deviceID);
-                return -1;
-            }
-
-            return device.sendFeatureReport(report);
-        } catch (Exception e) {
-            Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
-        }
-        return -1;
-    }
-
-    public boolean getFeatureReport(int deviceID, byte[] report) {
-        try {
-            //Log.v(TAG, "getFeatureReport deviceID=" + deviceID);
+            //Log.v(TAG, "readReport deviceID=" + deviceID);
             HIDDevice device;
             device = getDevice(deviceID);
             if (device == null) {
@@ -643,7 +626,7 @@ public boolean getFeatureReport(int deviceID, byte[] report) {
                 return false;
             }
 
-            return device.getFeatureReport(report);
+            return device.readReport(report, feature);
         } catch (Exception e) {
             Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
         }
@@ -680,5 +663,5 @@ public void closeDevice(int deviceID) {
     native void HIDDeviceDisconnected(int deviceID);
 
     native void HIDDeviceInputReport(int deviceID, byte[] report);
-    native void HIDDeviceFeatureReport(int deviceID, byte[] report);
+    native void HIDDeviceReportResponse(int deviceID, byte[] report);
 }
diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java
index bfe0cf954d95..5b5d201f7877 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java
@@ -153,49 +153,48 @@ public boolean open() {
     }
 
     @Override
-    public int sendFeatureReport(byte[] report) {
-        int res = -1;
-        int offset = 0;
-        int length = report.length;
-        boolean skipped_report_id = false;
-        byte report_number = report[0];
-
-        if (report_number == 0x0) {
-            ++offset;
-            --length;
-            skipped_report_id = true;
-        }
-
-        res = mConnection.controlTransfer(
-            UsbConstants.USB_TYPE_CLASS | 0x01 /*RECIPIENT_INTERFACE*/ | UsbConstants.USB_DIR_OUT,
-            0x09/*HID set_report*/,
-            (3/*HID feature*/ << 8) | report_number,
-            mInterface,
-            report, offset, length,
-            1000/*timeout millis*/);
-
-        if (res < 0) {
-            Log.w(TAG, "sendFeatureReport() returned " + res + " on device " + getDeviceName());
-            return -1;
-        }
+    public int writeReport(byte[] report, boolean feature) {
+        if (feature) {
+            int res = -1;
+            int offset = 0;
+            int length = report.length;
+            boolean skipped_report_id = false;
+            byte report_number = report[0];
+
+            if (report_number == 0x0) {
+                ++offset;
+                --length;
+                skipped_report_id = true;
+            }
 
-        if (skipped_report_id) {
-            ++length;
-        }
-        return length;
-    }
+            res = mConnection.controlTransfer(
+                UsbConstants.USB_TYPE_CLASS | 0x01 /*RECIPIENT_INTERFACE*/ | UsbConstants.USB_DIR_OUT,
+                0x09/*HID set_report*/,
+                (3/*HID feature*/ << 8) | report_number,
+                mInterface,
+                report, offset, length,
+                1000/*timeout millis*/);
+
+            if (res < 0) {
+                Log.w(TAG, "writeFeatureReport() returned " + res + " on device " + getDeviceName());
+                return -1;
+            }
 
-    @Override
-    public int sendOutputReport(byte[] report) {
-        int r = mConnection.bulkTransfer(mOutputEndpoint, report, report.length, 1000);
-        if (r != report.length) {
-            Log.w(TAG, "sendOutputReport() returned " + r + " on device " + getDeviceName());
+            if (skipped_report_id) {
+                ++length;
+            }
+            return length;
+        } else {
+            int res = mConnection.bulkTransfer(mOutputEndpoint, report, report.length, 1000);
+            if (res != report.length) {
+                Log.w(TAG, "writeOutputReport() returned " + res + " on device " + getDeviceName());
+            }
+            return res;
         }
-        return r;
     }
 
     @Override
-    public boolean getFeatureReport(byte[] report) {
+    public boolean readReport(byte[] report, boolean feature) {
         int res = -1;
         int offset = 0;
         int length = report.length;
@@ -213,7 +212,7 @@ public boolean getFeatureReport(byte[] report) {
         res = mConnection.controlTransfer(
             UsbConstants.USB_TYPE_CLASS | 0x01 /*RECIPIENT_INTERFACE*/ | UsbConstants.USB_DIR_IN,
             0x01/*HID get_report*/,
-            (3/*HID feature*/ << 8) | report_number,
+            ((feature ? 3/*HID feature*/ : 1/*HID Input*/) << 8) | report_number,
             mInterface,
             report, offset, length,
             1000/*timeout millis*/);
@@ -234,7 +233,7 @@ public boolean getFeatureReport(byte[] report) {
         } else {
             data = Arrays.copyOfRange(report, 0, res);
         }
-        mManager.HIDDeviceFeatureReport(mDeviceId, data);
+        mManager.HIDDeviceReportResponse(mDeviceId, data);
 
         return true;
     }
diff --git a/src/hidapi/android/hid.cpp b/src/hidapi/android/hid.cpp
new file mode 100644
index 000000000000..ae4b40c60eb7
--- /dev/null
+++ b/src/hidapi/android/hid.cpp
@@ -0,0 +1,1457 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 2022 Valve Corporation
+
+  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 "SDL_internal.h"
+
+// Purpose: A wrapper implementing "HID" API for Android
+//
+//          This layer glues the hidapi API to Android's USB and BLE stack.
+
+#include "hid.h"
+
+// Common to stub version and non-stub version of functions
+#include <jni.h>
+#include <android/log.h>
+
+#define TAG "hidapi"
+
+// Have error log always available
+#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
+
+#ifdef DEBUG
+#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
+#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
+#else
+#define LOGV(...)
+#define LOGD(...)
+#endif
+
+#define SDL_JAVA_PREFIX                                 org_libsdl_app
+#define CONCAT1(prefix, class, function)                CONCAT2(prefix, class, function)
+#define CONCAT2(prefix, class, function)                Java_ ## prefix ## _ ## class ## _ ## function
+#define HID_DEVICE_MANAGER_JAVA_INTERFACE(function)     CONCAT1(SDL_JAVA_PREFIX, HIDDeviceManager, function)
+
+
+#ifndef SDL_HIDAPI_DISABLED
+
+#include "../../core/android/SDL_android.h"
+
+#define hid_close                    PLATFORM_hid_close
+#define hid_device                   PLATFORM_hid_device
+#define hid_device_                  PLATFORM_hid_device_
+#define hid_enumerate                PLATFORM_hid_enumerate
+#define hid_error                    PLATFORM_hid_error
+#define hid_exit                     PLATFORM_hid_exit
+#define hid_free_enumeration         PLATFORM_hid_free_enumeration
+#define hid_get_device_info          PLATFORM_hid_get_device_info
+#define hid_get_feature_report       PLATFORM_hid_get_feature_report
+#define hid_get_indexed_string       PLATFORM_hid_get_indexed_string
+#define hid_get_input_report         PLATFORM_hid_get_input_report
+#define hid_get_manufacturer_string  PLATFORM_hid_get_manufacturer_string
+#define hid_get_product_string       PLATFORM_hid_get_product_string
+#define hid_get_report_descriptor    PLATFORM_hid_get_report_descriptor
+#define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
+#define hid_init                     PLATFORM_hid_init
+#define hid_open_path                PLATFORM_hid_open_path
+#define hid_open                     PLATFORM_hid_open
+#define hid_read                     PLATFORM_hid_read
+#define hid_read_timeout             PLATFORM_hid_read_timeout
+#define hid_send_feature_report      PLATFORM_hid_send_feature_report
+#define hid_set_nonblocking          PLATFORM_hid_set_nonblocking
+#define hid_version                  PLATFORM_hid_version
+#define hid_version_str              PLATFORM_hid_version_str
+#define hid_write                    PLATFORM_hid_write
+
+#include <pthread.h>
+#include <errno.h>	// For ETIMEDOUT and ECONNRESET
+#include <stdlib.h> // For malloc() and free()
+
+#include "../hidapi/hidapi.h"
+
+typedef uint32_t uint32;
+typedef uint64_t uint64;
+
+
+struct hid_device_
+{
+	int m_nId;
+	int m_nDeviceRefCount;
+};
+
+static JavaVM *g_JVM;
+static pthread_key_t g_ThreadKey;
+
+template<class T>
+class hid_device_ref
+{
+public:
+	hid_device_ref( T *pObject = nullptr ) : m_pObject( nullptr )
+	{
+		SetObject( pObject );
+	}
+
+	hid_device_ref( const hid_device_ref &rhs ) : m_pObject( nullptr )
+	{
+		SetObject( rhs.GetObject() );
+	}
+
+	~hid_device_ref()
+	{
+		SetObject( nullptr );
+	}
+
+	void SetObject( T *pObject )
+	{
+		if ( m_pObject && m_pObject->DecrementRefCount() == 0 )
+		{
+			delete m_pObject;
+		}
+
+		m_pObject = pObject;
+
+		if ( m_pObject )
+		{
+			m_pObject->IncrementRefCount();
+		}
+	}
+
+	hid_device_ref &operator =( T *pObject )
+	{
+		SetObject( pObject );
+		return *this;
+	}
+
+	hid_device_ref &operator =( const hid_device_ref &rhs )
+	{
+		SetObject( rhs.GetObject() );
+		return *this;
+	}
+
+	T *GetObject() const
+	{
+		return m_pObject;
+	}
+
+	T* operator->() const
+	{
+		return m_pObject;
+	}
+
+	operator bool() const
+	{
+		return ( m_pObject != nullptr );
+	}
+
+private:
+	T *m_pObject;
+};
+
+class hid_mutex_guard
+{
+public:
+	hid_mutex_guard( pthread_mutex_t *pMutex ) : m_pMutex( pMutex )
+	{
+		pthread_mutex_lock( m_pMutex );
+	}
+	~hid_mutex_guard()
+	{
+		pthread_mutex_unlock( m_pMutex );
+	}
+
+private:
+	pthread_mutex_t *m_pMutex;
+};
+
+class hid_buffer
+{
+public:
+	hid_buffer() : m_pData( nullptr ), m_nSize( 0 ), m_nAllocated( 0 )
+	{
+	}
+
+	hid_buffer( const uint8_t *pData, size_t nSize ) : m_pData( nullptr ), m_nSize( 0 ), m_nAllocated( 0 )
+	{
+		assign( pData, nSize );
+	}
+
+	~hid_buffer()
+	{
+		delete[] m_pData;
+	}
+
+	void assign( const uint8_t *pData, size_t nSize )
+	{
+		if ( nSize > m_nAllocated )
+		{
+			delete[] m_pData;
+			m_pData = new uint8_t[ nSize ];
+			m_nAllocated = nSize;
+		}
+
+		m_nSize = nSize;
+		SDL_memcpy( m_pData, pData, nSize );
+	}
+
+	void clear()
+	{
+		m_nSize = 0;
+	}
+
+	size_t size() const
+	{
+		return m_nSize;
+	}
+
+	const uint8_t *data() const
+	{
+		return m_pData;
+	}
+
+private:
+	uint8_t *m_pData;
+	size_t m_nSize;
+	size_t m_nAllocated;
+};
+
+class hid_buffer_pool
+{
+public:
+	hid_buffer_pool() : m_nSize( 0 ), m_pHead( nullptr ), m_pTail( nullptr ), m_pFree( nullptr )
+	{
+	}
+
+	~hid_buffer_pool()
+	{
+		clear();
+
+		while ( m_pFree )
+		{
+			hid_buffer_entry *pEntry = m_pFree;
+			m_pFree = m_pFree->m_pNext;
+			delete pEntry;
+		}
+	}
+
+	size_t size() const { return m_nSize; }
+
+	const hid_buffer &front() const { return m_pHead->m_buffer; }
+
+	void pop_front()
+	{
+		hid_buffer_entry *pEntry = m_pHead;
+		if ( pEntry )
+		{
+			m_pHead = pEntry->m_pNext;
+			if ( !m_pHead )
+			{
+				m_pTail = nullptr;
+			}
+			pEntry->m_pNext = m_pFree;
+			m_pFree = pEntry;
+			--m_nSize;
+		}
+	}
+
+	void emplace_back( const uint8_t *pData, size_t nSize )
+	{
+		hid_buffer_entry *pEntry;
+
+		if ( m_pFree )
+		{
+			pEntry = m_pFree;
+			m_pFree = m_pFree->m_pNext;
+		}
+		else
+		{
+			pEntry = new hid_buffer_entry;
+		}
+		pEntry->m_pNext = nullptr;
+
+		if ( m_pTail )
+		{
+			m_pTail->m_pNext = pEntry;
+		}
+		else
+		{
+			m_pHead = pEntry;
+		}
+		m_pTail = pEntry;
+
+		pEntry->m_buffer.assign( pData, nSize );
+		++m_nSize;
+	}
+
+	void clear()
+	{
+		while ( size() > 0 )
+		{
+			pop_front();
+		}
+	}
+
+private:
+	struct hid_buffer_entry
+	{
+		hid_buffer m_buffer;
+		hid_buffer_entry *m_pNext;
+	};
+
+	size_t m_nSize;
+	hid_buffer_entry *m_pHead;
+	hid_buffer_entry *m_pTail;
+	hid_buffer_entry *m_pFree;
+};
+
+static jbyteArray NewByteArray( JNIEnv* env, const uint8_t *pData, size_t nDataLen )
+{
+	jbyteArray array = env->NewByteArray( (jsize)nDataLen );
+	jbyte *pBuf = env->GetByteArrayElements( array, NULL );
+	SDL_memcpy( pBuf, pData, nDataLen );
+	env->ReleaseByteArrayElements( array, pBuf, 0 );
+
+	return array;
+}
+
+static char *CreateStringFromJString( JNIEnv *env, const jstring &sString )
+{
+	size_t nLength = env->GetStringUTFLength( sString );
+	const char *pjChars = env->GetStringUTFChars( sString, NULL );
+	char *psString = (char*)malloc( nLength + 1 );
+	SDL_memcpy( psString, pjChars, nLength );
+	psString[ nLength ] = '\0';
+	env->ReleaseStringUTFChars( sString, pjChars );
+	return psString;
+}
+
+static wchar_t *CreateWStringFromJString( JNIEnv *env, const jstring &sString )
+{
+	size_t nLength = env->GetStringLength( sString );
+	const jchar *pjChars = env->GetStringChars( sString, NULL );
+	wchar_t *pwString = (wchar_t*)malloc( ( nLength + 1 ) * sizeof( wchar_t ) );
+	wchar_t *pwChars = pwString;
+	for ( size_t iIndex = 0; iIndex < nLength; ++iIndex )
+	{
+		pwChars[ iIndex ] = pjChars[ iIndex ];
+	}
+	pwString[ nLength ] = '\0';
+	env->ReleaseStringChars( sString, pjChars );
+	return pwString;
+}
+
+static wchar_t *CreateWStringFromWString( const wchar_t *pwSrc )
+{
+	size_t nLength = SDL_wcslen( pwSrc );
+	wchar_t *pwString = (wchar_t*)malloc( ( nLength + 1 ) * sizeof( wchar_t ) );
+	SDL_memcpy( pwString, pwSrc, nLength * sizeof( wchar_t ) );
+	pwString[ nLength ] = '\0';
+	return pwString;
+}
+
+static hid_device_info *CopyHIDDeviceInfo( const hid_device_info *pInfo )
+{
+	hid_device_info *pCopy = new hid_device_info;
+	*pCopy = *pInfo;
+	pCopy->path = SDL_strdup( pInfo->path );
+	pCopy->product_string = CreateWStringFromWString( pInfo->product_string );
+	pCopy->manufacturer_string = CreateWStringFromWString( pInfo->manufacturer_string );
+	pCopy->serial_number = CreateWStringFromWString( pInfo->serial_number );
+	return pCopy;
+}
+
+static void FreeHIDDeviceInfo( hid_device_info *pInfo )
+{
+	free( pInfo->path );
+	free( pInfo->serial_number );
+	free( pInfo->manufacturer_string );
+	free( pInfo->product_string );
+	delete pInfo;
+}
+
+static jclass  g_HIDDeviceManagerCallbackClass;
+static jobject g_HIDDeviceManagerCallbackHandler;
+static jmethodID g_midHIDDeviceManagerInitialize;
+static jmethodID g_midHIDDeviceManagerOpen;
+static jmethodID g_midHIDDeviceManagerWriteReport;
+static jmethodID g_midHIDDeviceManagerReadReport;
+static jmethodID g_midHIDDeviceManagerClose;
+static bool g_initialized = false;
+
+static uint64_t get_timespec_ms( const struct timespec &ts )
+{
+	return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+}
+
+static void ExceptionCheck( JNIEnv *env, const char *pszClassName, const char *pszMethodName )
+{
+	if ( env->ExceptionCheck() )
+	{
+		// Get our exception
+		jthrowable jExcept = env->ExceptionOccurred();
+
+		// Clear the exception so we can call JNI again
+		env->ExceptionClear();
+
+		// Get our exception message
+		jclass jExceptClass = env->GetObjectClass( jExcept );
+		jmethodID jMessageMethod = env->GetMethodID( jExceptClass, "getMessage", "()Ljava/lang/String;" );
+		jstring jMessage = (jstring)( env->CallObjectMethod( jExcept, jMessageMethod ) );
+		const char *pszMessage = env->GetStringUTFChars( jMessage, NULL );
+
+		// ...and log it.
+		LOGE( "%s%s%s threw an exception: %s",
+			pszClassName ? pszClassName : "",
+			pszClassName ? "::" : "",
+			pszMethodName, pszMessage );
+
+		// Cleanup
+		env->ReleaseStringUTFChars( jMessage, pszMessage );
+		env->DeleteLocalRef( jMessage );
+		env->DeleteLocalRef( jExceptClass );
+		env->DeleteLocalRef( jExcept );
+	}
+}
+
+class CHIDDevice
+{
+public:
+	CHIDDevice( int nDeviceID, hid_device_info *pInfo )
+	{
+		m_nId = nDeviceID;
+		m_pInfo = pInfo;
+
+		// The Bluetooth Steam Controller needs special handling
+		const int VALVE_USB_VID	= 0x28DE;
+		const int D0G_BLE2_PID = 0x1106;
+		if ( pInfo->vendor_id == VALVE_USB_VID && pInfo->product_id == D0G_BLE2_PID )
+		{
+			m_bIsBLESteamController = true;
+		}
+	}
+
+	~CHIDDevice()
+	{
+		FreeHIDDeviceInfo( m_pInfo );
+
+		// Note that we don't delete m_pDevice, as the app may still have a reference to it
+	}
+
+	int IncrementRefCount()
+	{
+		int nValue;
+		pthread_mutex_lock( &m_refCountLock );
+		nValue = ++m_nRefCount;
+		pthread_mutex_unlock( &m_refCountLock );
+		return nValue;
+	}
+
+	int DecrementRefCount()
+	{
+		int nValue;
+		pthread_mutex_lock( &m_refCountLock );
+		nValue = --m_nRefCount;
+		pthread_mutex_unlock( &m_refCountLock );
+		return nValue;
+	}
+
+	int GetId()
+	{
+		return m_nId;
+	}
+
+	hid_device_info *GetDeviceInfo()
+	{
+		return m_pInfo;
+	}
+
+	hid_device *GetDevice()
+	{
+		return m_pDevice;
+	}
+
+	void ExceptionCheck( JNIEnv *env, const char *pszMethodName )
+	{
+		::ExceptionCheck( env, "CHIDDevice", pszMethodName );
+	}
+
+	bool BOpen()
+	{
+		// Make sure thread is attached to JVM/env
+		JNIEnv *env;
+		g_JVM->AttachCurrentThread( &env, NULL );
+		pthread_setspecific( g_ThreadKey, (void*)env );
+
+		if ( !g_HIDDeviceManagerCallbackHandler )
+		{
+			LOGV( "Device open without callback handler" );
+			return false;
+		}
+
+		m_bIsWaitingForOpen = false;
+		m_bOpenResult = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerOpen, m_nId );
+		ExceptionCheck( env, "BOpen" );
+
+		if ( m_bIsWaitingForOpen )
+		{
+			hid_mutex_guard cvl( &m_cvLock );
+
+			const int OPEN_TIMEOUT_SECONDS = 60;
+			struct timespec ts, endtime;
+			clock_gettime( CLOCK_REALTIME, &ts );
+			endtime = ts;
+			endtime.tv_sec += OPEN_TIMEOUT_SECONDS;
+			do
+			{
+				if ( pthread_cond_timedwait( &m_cv, &m_cvLock, &endtime ) != 0 )
+				{
+					break;
+				}
+			}
+			while ( m_bIsWaitingForOpen && get_timespec_ms( ts ) < get_timespec_ms( endtime ) );
+		}
+
+		if ( !m_bOpenResult )
+		{
+			if ( m_bIsWaitingForOpen )
+			{
+				LOGV( "Device open failed - timed out waiting for device permission" );
+			}
+			else
+			{
+				LOGV( "Device open failed" );
+			}
+			return false;
+		}
+
+		m_pDevice = new hid_device;
+		m_pDevice->m_nId = m_nId;
+		m_pDevice->m_nDeviceRefCount = 1;
+		LOGD("Creating device %d (%p), refCount = 1\n", m_pDevice->m_nId, m_pDevice);
+		return true;
+	}
+
+	void SetOpenPending()
+	{
+		m_bIsWaitingForOpen = true;
+	}
+
+	void SetOpenResult( bool bResult )
+	{
+		if ( m_bIsWaitingForOpen )
+		{
+			m_bOpenResult = bResult;
+			m_bIsWaitingForOpen = false;
+			pthread_cond_signal( &m_cv );
+		}
+	}
+
+	void ProcessInput( const uint8_t *pBuf, size_t nBufSize )
+	{
+		hid_mutex_guard l( &m_dataLock );
+
+		size_t MAX_REPORT_QUEUE_SIZE = 16;
+		if ( m_vecData.size() >= MAX_REPORT_QUEUE_SIZE )
+		{
+			m_vecData.pop_front();
+		}
+		m_vecData.emplace_back( pBuf, nBufSize );
+	}
+
+	int GetInput( unsigned char *data, size_t length )
+	{
+		hid_mutex_guard l( &m_dataLock );
+
+		if ( m_vecData.size() == 0 )
+		{
+//			LOGV( "hid_read_timeout no data available" );
+			return 0;
+		}
+
+		const hid_buffer &buffer = m_vecData.front();
+		size_t nDataLen = buffer.size() > length ? length : buffer.size();
+		if ( m_bIsBLESteamController )
+		{
+			data[0] = 0x03;
+			SDL_memcpy( data + 1, buffer.data(), nDataLen );
+			++nDataLen;
+		}
+		else
+		{
+			SDL_memcpy( data, buffer.data(), nDataLen );
+		}
+		m_vecData.pop_front();
+
+//		LOGV("Read %u bytes", nDataLen);
+//		LOGV("%02x %02x %02x %02x %02x %02x %02x %02x ....",
+//			 data[0], data[1], data[2], data[3],
+//			 data[4], data[5], data[6], data[7]);
+
+		return (int)nDataLen;
+	}
+
+	int WriteReport( const unsigned char *pData, size_t nDataLen, bool bFeature )
+	{
+		// Make sure thread is attached to JVM/env
+		JNIEnv *env;
+		g_JVM->AttachCurrentThread( &env, NULL );
+		pthread_setspecific( g_ThreadKey, (void*)env );
+
+		int nRet = -1;
+		if ( g_HIDDeviceManagerCallbackHandler )
+		{
+			jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
+			nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerWriteReport, m_nId, pBuf, bFeature );
+			ExceptionCheck( env, "WriteReport" );
+			env->DeleteLocalRef( pBuf );
+		}
+		else
+		{
+			LOGV( "WriteReport without callback handler" );
+		}
+		return nRet;
+	}
+
+	void ProcessReportResponse( const uint8_t *pBuf, size_t nBufSize )
+	{
+		hid_mutex_guard cvl( &m_cvLock );
+		if ( m_bIsWaitingForReportResponse )
+		{
+			m_reportResponse.assign( pBuf, nBufSize );
+
+			m_bIsWaitingForReportResponse = false;
+			m_nReportResponseError = 0;
+			pthread_cond_signal( &m_cv );
+		}
+	}
+
+	int ReadReport( unsigned char *pData, size_t nDataLen, bool bFeature )
+	{
+		// Make sure thread is attached to JVM/env
+		JNIEnv *env;
+		g_JVM->AttachCurrentThread( &env, NULL );
+		pthread_setspecific( g_ThreadKey, (void*)env );
+
+		if ( !g_HIDDeviceManagerCallbackHandler )
+		{
+			LOGV( "ReadReport without callback handler" );
+			return -1;
+		}
+
+		{
+			hid_mutex_guard cvl( &m_cvLock );
+			if ( m_bIsWaitingForReportResponse )
+			{
+				LOGV( "Get feature report already ongoing... bail" );
+				return -1; // Read already ongoing, we currently do not serialize, TODO
+			}
+			m_bIsWaitingForReportResponse = true;
+		}
+
+		jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
+		int nRet = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerReadReport, m_nId, pBuf, bFeature ) ? 0 : -1;
+		ExceptionCheck( env, "ReadReport" );
+		env->DeleteLocalRef( pBuf );
+		if ( nRet < 0 )
+		{
+			LOGV( "ReadReport failed" );
+			m_bIsWaitingForReportResponse = false;
+			return -1;
+		}
+
+		{
+			hid_mutex_guard cvl( &m_cvLock );
+			if ( m_bIsWaitingForReportResponse )
+			{
+				LOGV("=== Going to sleep" );
+				// Wait in CV until we are no longer waiting for a feature report.
+				const int FEATURE_REPORT_TIMEOUT_SECONDS = 2;
+				struct timespec ts, endtime;
+				clock_gettime( CLOCK_REALTIME, &ts );
+				endtime = ts;
+				endtime.tv_sec += FEATURE_REPORT_TIMEOUT_SECONDS;
+				do
+				{
+					if ( pthread_cond_timedwait( &m_cv, &m_cvLock, &endtime ) != 0 )
+					{
+						break;
+					}
+				}
+				while ( m_bIsWaitingForReportResponse && get_timespec_ms( ts ) < get_timespec_ms( endtime ) );
+
+				// We are back
+				if ( m_bIsWaitingForReportResponse )
+				{
+					m_nReportResponseError = -ETIMEDOUT;
+					m_bIsWaitingForReportResponse = false;
+				}
+				LOGV( "=== Got feature report err=%d", m_nReportResponseError );
+				if ( m_nReportResponseError != 0 )
+				{
+					return m_nReportResponseError;
+				}
+			}
+
+			size_t uBytesToCopy = m_reportResponse.size() > nDataLen ? nDataLen : m_reportResponse.size();
+			SDL_memcpy( pData, m_reportResponse.data(), uBytesToCopy );
+			m_reportResponse.clear();
+			LOGV( "=== Got %u bytes", uBytesToCopy );
+
+			return (int)uBytesToCopy;
+		}
+	}
+
+	void Close( bool bDeleteDevice )
+	{
+		// Make sure thread is attached to JVM/env
+		JNIEnv *env;
+		g_JVM->AttachCurrentThread( &env, NULL );
+		pthread_setspecific( g_ThreadKey, (void*)env );
+
+		if ( g_HIDDeviceManagerCallbackHandler )
+		{
+			env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
+			ExceptionCheck( env, "Close" );
+		}
+
+		hid_mutex_guard dataLock( &m_dataLock );
+		m_vecData.clear();
+
+		// Clean and release pending feature report reads
+		hid_mutex_guard cvLock( &m_cvLock );
+		m_reportResponse.clear();
+		m_bIsWaitingForReportResponse = false;
+		m_nReportResponseError = -ECONNRESET;
+		pthread_cond_broadcast( &m_cv );
+
+		if ( bDeleteDevice )
+		{
+			delete m_pDevice;
+			m_pDevice = nullptr;
+		}
+	}
+
+private:
+	pthread_mutex_t m_refCountLock = PTHREAD_MUTEX_INITIALIZER;
+	int m_nRefCount = 0;
+	int m_nId = 0;
+	hid_device_info *m_pInfo = nullptr;
+	hid_device *m_pDevice = nullptr;
+	bool m_bIsBLESteamController = false;
+
+	pthread_mutex_t m_dataLock = PTHREAD_MUTEX_INITIALIZER; // This lock has to be held to access m_vecData
+	hid_bu

(Patch may be truncated, please check the link at the top of this post.)