hid.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 2022 Valve Corporation
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // Purpose: A wrapper implementing "HID" API for Android
  20. //
  21. // This layer glues the hidapi API to Android's USB and BLE stack.
  22. #include "hid.h"
  23. // Common to stub version and non-stub version of functions
  24. #include <jni.h>
  25. #include <android/log.h>
  26. #define TAG "hidapi"
  27. // Have error log always available
  28. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
  29. #ifdef DEBUG
  30. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
  31. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
  32. #else
  33. #define LOGV(...)
  34. #define LOGD(...)
  35. #endif
  36. #define SDL_JAVA_PREFIX org_libsdl_app
  37. #define CONCAT1(prefix, class, function) CONCAT2(prefix, class, function)
  38. #define CONCAT2(prefix, class, function) Java_ ## prefix ## _ ## class ## _ ## function
  39. #define HID_DEVICE_MANAGER_JAVA_INTERFACE(function) CONCAT1(SDL_JAVA_PREFIX, HIDDeviceManager, function)
  40. #ifndef SDL_HIDAPI_DISABLED
  41. extern "C" {
  42. #include "../SDL_hidapi_c.h"
  43. }
  44. #include "../../core/android/SDL_android.h"
  45. #define hid_close PLATFORM_hid_close
  46. #define hid_device PLATFORM_hid_device
  47. #define hid_device_ PLATFORM_hid_device_
  48. #define hid_enumerate PLATFORM_hid_enumerate
  49. #define hid_error PLATFORM_hid_error
  50. #define hid_exit PLATFORM_hid_exit
  51. #define hid_free_enumeration PLATFORM_hid_free_enumeration
  52. #define hid_get_device_info PLATFORM_hid_get_device_info
  53. #define hid_get_feature_report PLATFORM_hid_get_feature_report
  54. #define hid_get_indexed_string PLATFORM_hid_get_indexed_string
  55. #define hid_get_input_report PLATFORM_hid_get_input_report
  56. #define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string
  57. #define hid_get_product_string PLATFORM_hid_get_product_string
  58. #define hid_get_report_descriptor PLATFORM_hid_get_report_descriptor
  59. #define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
  60. #define hid_init PLATFORM_hid_init
  61. #define hid_open_path PLATFORM_hid_open_path
  62. #define hid_open PLATFORM_hid_open
  63. #define hid_read PLATFORM_hid_read
  64. #define hid_read_timeout PLATFORM_hid_read_timeout
  65. #define hid_send_feature_report PLATFORM_hid_send_feature_report
  66. #define hid_set_nonblocking PLATFORM_hid_set_nonblocking
  67. #define hid_version PLATFORM_hid_version
  68. #define hid_version_str PLATFORM_hid_version_str
  69. #define hid_write PLATFORM_hid_write
  70. #include <pthread.h>
  71. #include <errno.h> // For ETIMEDOUT and ECONNRESET
  72. #include <stdlib.h> // For malloc() and free()
  73. #include "../hidapi/hidapi.h"
  74. typedef uint32_t uint32;
  75. typedef uint64_t uint64;
  76. #define D0G_BLE2_PID 0x1106
  77. #define TRITON_BLE_PID 0x1303
  78. struct hid_device_
  79. {
  80. int m_nId;
  81. int m_nDeviceRefCount;
  82. };
  83. template<class T>
  84. class hid_device_ref
  85. {
  86. public:
  87. hid_device_ref( T *pObject = nullptr ) : m_pObject( nullptr )
  88. {
  89. SetObject( pObject );
  90. }
  91. hid_device_ref( const hid_device_ref &rhs ) : m_pObject( nullptr )
  92. {
  93. SetObject( rhs.GetObject() );
  94. }
  95. ~hid_device_ref()
  96. {
  97. SetObject( nullptr );
  98. }
  99. void SetObject( T *pObject )
  100. {
  101. if ( m_pObject && m_pObject->DecrementRefCount() == 0 )
  102. {
  103. delete m_pObject;
  104. }
  105. m_pObject = pObject;
  106. if ( m_pObject )
  107. {
  108. m_pObject->IncrementRefCount();
  109. }
  110. }
  111. hid_device_ref &operator =( T *pObject )
  112. {
  113. SetObject( pObject );
  114. return *this;
  115. }
  116. hid_device_ref &operator =( const hid_device_ref &rhs )
  117. {
  118. SetObject( rhs.GetObject() );
  119. return *this;
  120. }
  121. T *GetObject() const
  122. {
  123. return m_pObject;
  124. }
  125. T* operator->() const
  126. {
  127. return m_pObject;
  128. }
  129. operator bool() const
  130. {
  131. return ( m_pObject != nullptr );
  132. }
  133. private:
  134. T *m_pObject;
  135. };
  136. class hid_mutex_guard
  137. {
  138. public:
  139. hid_mutex_guard( pthread_mutex_t *pMutex ) : m_pMutex( pMutex )
  140. {
  141. pthread_mutex_lock( m_pMutex );
  142. }
  143. ~hid_mutex_guard()
  144. {
  145. pthread_mutex_unlock( m_pMutex );
  146. }
  147. private:
  148. pthread_mutex_t *m_pMutex;
  149. };
  150. class hid_buffer
  151. {
  152. public:
  153. hid_buffer() : m_pData( nullptr ), m_nSize( 0 ), m_nAllocated( 0 )
  154. {
  155. }
  156. hid_buffer( const uint8_t *pData, size_t nSize ) : m_pData( nullptr ), m_nSize( 0 ), m_nAllocated( 0 )
  157. {
  158. assign( pData, nSize );
  159. }
  160. ~hid_buffer()
  161. {
  162. delete[] m_pData;
  163. }
  164. void assign( const uint8_t *pData, size_t nSize )
  165. {
  166. if ( nSize > m_nAllocated )
  167. {
  168. delete[] m_pData;
  169. m_pData = new uint8_t[ nSize ];
  170. m_nAllocated = nSize;
  171. }
  172. m_nSize = nSize;
  173. SDL_memcpy( m_pData, pData, nSize );
  174. }
  175. void clear()
  176. {
  177. m_nSize = 0;
  178. }
  179. size_t size() const
  180. {
  181. return m_nSize;
  182. }
  183. const uint8_t *data() const
  184. {
  185. return m_pData;
  186. }
  187. private:
  188. uint8_t *m_pData;
  189. size_t m_nSize;
  190. size_t m_nAllocated;
  191. };
  192. class hid_buffer_pool
  193. {
  194. public:
  195. hid_buffer_pool() : m_nSize( 0 ), m_pHead( nullptr ), m_pTail( nullptr ), m_pFree( nullptr )
  196. {
  197. }
  198. ~hid_buffer_pool()
  199. {
  200. clear();
  201. while ( m_pFree )
  202. {
  203. hid_buffer_entry *pEntry = m_pFree;
  204. m_pFree = m_pFree->m_pNext;
  205. delete pEntry;
  206. }
  207. }
  208. size_t size() const { return m_nSize; }
  209. const hid_buffer &front() const { return m_pHead->m_buffer; }
  210. void pop_front()
  211. {
  212. hid_buffer_entry *pEntry = m_pHead;
  213. if ( pEntry )
  214. {
  215. m_pHead = pEntry->m_pNext;
  216. if ( !m_pHead )
  217. {
  218. m_pTail = nullptr;
  219. }
  220. pEntry->m_pNext = m_pFree;
  221. m_pFree = pEntry;
  222. --m_nSize;
  223. }
  224. }
  225. void emplace_back( const uint8_t *pData, size_t nSize )
  226. {
  227. hid_buffer_entry *pEntry;
  228. if ( m_pFree )
  229. {
  230. pEntry = m_pFree;
  231. m_pFree = m_pFree->m_pNext;
  232. }
  233. else
  234. {
  235. pEntry = new hid_buffer_entry;
  236. }
  237. pEntry->m_pNext = nullptr;
  238. if ( m_pTail )
  239. {
  240. m_pTail->m_pNext = pEntry;
  241. }
  242. else
  243. {
  244. m_pHead = pEntry;
  245. }
  246. m_pTail = pEntry;
  247. pEntry->m_buffer.assign( pData, nSize );
  248. ++m_nSize;
  249. }
  250. void clear()
  251. {
  252. while ( size() > 0 )
  253. {
  254. pop_front();
  255. }
  256. }
  257. private:
  258. struct hid_buffer_entry
  259. {
  260. hid_buffer m_buffer;
  261. hid_buffer_entry *m_pNext;
  262. };
  263. size_t m_nSize;
  264. hid_buffer_entry *m_pHead;
  265. hid_buffer_entry *m_pTail;
  266. hid_buffer_entry *m_pFree;
  267. };
  268. static jbyteArray NewByteArray( JNIEnv *env, const uint8_t *pData, size_t nDataLen )
  269. {
  270. jbyteArray array = env->NewByteArray( (jsize)nDataLen );
  271. jbyte *pBuf = env->GetByteArrayElements( array, NULL );
  272. SDL_memcpy( pBuf, pData, nDataLen );
  273. env->ReleaseByteArrayElements( array, pBuf, 0 );
  274. return array;
  275. }
  276. static char *CreateStringFromJString( JNIEnv *env, const jstring &sString )
  277. {
  278. size_t nLength = env->GetStringUTFLength( sString );
  279. const char *pjChars = env->GetStringUTFChars( sString, NULL );
  280. char *psString = (char *)malloc( nLength + 1 );
  281. SDL_memcpy( psString, pjChars, nLength );
  282. psString[ nLength ] = '\0';
  283. env->ReleaseStringUTFChars( sString, pjChars );
  284. return psString;
  285. }
  286. static wchar_t *CreateWStringFromJString( JNIEnv *env, const jstring &sString )
  287. {
  288. size_t nLength = env->GetStringLength( sString );
  289. const jchar *pjChars = env->GetStringChars( sString, NULL );
  290. wchar_t *pwString = (wchar_t *)malloc( ( nLength + 1 ) * sizeof( wchar_t ) );
  291. wchar_t *pwChars = pwString;
  292. for ( size_t iIndex = 0; iIndex < nLength; ++iIndex )
  293. {
  294. pwChars[ iIndex ] = pjChars[ iIndex ];
  295. }
  296. pwString[ nLength ] = '\0';
  297. env->ReleaseStringChars( sString, pjChars );
  298. return pwString;
  299. }
  300. static wchar_t *CreateWStringFromWString( const wchar_t *pwSrc )
  301. {
  302. size_t nLength = SDL_wcslen( pwSrc );
  303. wchar_t *pwString = (wchar_t *)malloc( ( nLength + 1 ) * sizeof( wchar_t ) );
  304. SDL_memcpy( pwString, pwSrc, nLength * sizeof( wchar_t ) );
  305. pwString[ nLength ] = '\0';
  306. return pwString;
  307. }
  308. static hid_device_info *CopyHIDDeviceInfo( const hid_device_info *pInfo )
  309. {
  310. hid_device_info *pCopy = new hid_device_info;
  311. *pCopy = *pInfo;
  312. pCopy->path = SDL_strdup( pInfo->path );
  313. pCopy->product_string = CreateWStringFromWString( pInfo->product_string );
  314. pCopy->manufacturer_string = CreateWStringFromWString( pInfo->manufacturer_string );
  315. pCopy->serial_number = CreateWStringFromWString( pInfo->serial_number );
  316. return pCopy;
  317. }
  318. static void FreeHIDDeviceInfo( hid_device_info *pInfo )
  319. {
  320. free( pInfo->path );
  321. free( pInfo->serial_number );
  322. free( pInfo->manufacturer_string );
  323. free( pInfo->product_string );
  324. delete pInfo;
  325. }
  326. static jclass g_HIDDeviceManagerCallbackClass;
  327. static jobject g_HIDDeviceManagerCallbackHandler;
  328. static jmethodID g_midHIDDeviceManagerInitialize;
  329. static jmethodID g_midHIDDeviceManagerOpen;
  330. static jmethodID g_midHIDDeviceManagerWriteReport;
  331. static jmethodID g_midHIDDeviceManagerReadReport;
  332. static jmethodID g_midHIDDeviceManagerClose;
  333. static bool g_initialized = false;
  334. static uint64_t get_timespec_ms( const struct timespec &ts )
  335. {
  336. return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  337. }
  338. static void ExceptionCheck( JNIEnv *env, const char *pszClassName, const char *pszMethodName )
  339. {
  340. if ( env->ExceptionCheck() )
  341. {
  342. // Get our exception
  343. jthrowable jExcept = env->ExceptionOccurred();
  344. // Clear the exception so we can call JNI again
  345. env->ExceptionClear();
  346. // Get our exception message
  347. jclass jExceptClass = env->GetObjectClass( jExcept );
  348. jmethodID jMessageMethod = env->GetMethodID( jExceptClass, "getMessage", "()Ljava/lang/String;" );
  349. jstring jMessage = (jstring)( env->CallObjectMethod( jExcept, jMessageMethod ) );
  350. const char *pszMessage = env->GetStringUTFChars( jMessage, NULL );
  351. // ...and log it.
  352. LOGE( "%s%s%s threw an exception: %s",
  353. pszClassName ? pszClassName : "",
  354. pszClassName ? "::" : "",
  355. pszMethodName, pszMessage );
  356. // Cleanup
  357. env->ReleaseStringUTFChars( jMessage, pszMessage );
  358. env->DeleteLocalRef( jMessage );
  359. env->DeleteLocalRef( jExceptClass );
  360. env->DeleteLocalRef( jExcept );
  361. }
  362. }
  363. class CHIDDevice
  364. {
  365. public:
  366. CHIDDevice( int nDeviceID, hid_device_info *pInfo )
  367. {
  368. m_nId = nDeviceID;
  369. m_pInfo = pInfo;
  370. // The Bluetooth Steam Controller needs special handling
  371. const int VALVE_USB_VID = 0x28DE;
  372. if ( pInfo->vendor_id == VALVE_USB_VID && ( pInfo->product_id == D0G_BLE2_PID || pInfo->product_id == TRITON_BLE_PID ) )
  373. {
  374. m_bIsBLESteamController = true;
  375. }
  376. }
  377. ~CHIDDevice()
  378. {
  379. FreeHIDDeviceInfo( m_pInfo );
  380. // Note that we don't delete m_pDevice, as the app may still have a reference to it
  381. }
  382. int IncrementRefCount()
  383. {
  384. int nValue;
  385. pthread_mutex_lock( &m_refCountLock );
  386. nValue = ++m_nRefCount;
  387. pthread_mutex_unlock( &m_refCountLock );
  388. return nValue;
  389. }
  390. int DecrementRefCount()
  391. {
  392. int nValue;
  393. pthread_mutex_lock( &m_refCountLock );
  394. nValue = --m_nRefCount;
  395. pthread_mutex_unlock( &m_refCountLock );
  396. return nValue;
  397. }
  398. int GetId()
  399. {
  400. return m_nId;
  401. }
  402. hid_device_info *GetDeviceInfo()
  403. {
  404. return m_pInfo;
  405. }
  406. hid_device *GetDevice()
  407. {
  408. return m_pDevice;
  409. }
  410. void ExceptionCheck( JNIEnv *env, const char *pszMethodName )
  411. {
  412. ::ExceptionCheck( env, "CHIDDevice", pszMethodName );
  413. }
  414. bool BOpen()
  415. {
  416. JNIEnv *env = (JNIEnv *)SDL_GetAndroidJNIEnv();
  417. if ( !g_HIDDeviceManagerCallbackHandler )
  418. {
  419. LOGV( "Device open without callback handler" );
  420. return false;
  421. }
  422. if ( m_bIsWaitingForOpen )
  423. {
  424. SDL_SetError( "Waiting for permission" );
  425. return false;
  426. }
  427. if ( !m_bOpenResult )
  428. {
  429. m_bOpenResult = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerOpen, m_nId );
  430. ExceptionCheck( env, "BOpen" );
  431. if ( m_bIsWaitingForOpen )
  432. {
  433. LOGV( "Device open waiting for permission" );
  434. SDL_SetError( "Waiting for permission" );
  435. m_bWasOpenPending = true;
  436. return false;
  437. }
  438. if ( !m_bOpenResult )
  439. {
  440. LOGV( "Device open failed" );
  441. SDL_SetError( "Device open failed" );
  442. return false;
  443. }
  444. }
  445. m_pDevice = new hid_device;
  446. m_pDevice->m_nId = m_nId;
  447. m_pDevice->m_nDeviceRefCount = 1;
  448. LOGD("Creating device %d (%p), refCount = 1\n", m_pDevice->m_nId, m_pDevice);
  449. return true;
  450. }
  451. void SetOpenPending()
  452. {
  453. m_bIsWaitingForOpen = true;
  454. }
  455. bool BOpenPending() const
  456. {
  457. return m_bIsWaitingForOpen;
  458. }
  459. void SetWasOpenPending( bool bState )
  460. {
  461. m_bWasOpenPending = bState;
  462. }
  463. bool BWasOpenPending() const
  464. {
  465. return m_bWasOpenPending;
  466. }
  467. void SetOpenResult( bool bResult )
  468. {
  469. if ( m_bIsWaitingForOpen )
  470. {
  471. m_bOpenResult = bResult;
  472. m_bIsWaitingForOpen = false;
  473. if ( m_bOpenResult )
  474. {
  475. LOGV( "Device open succeeded" );
  476. }
  477. else
  478. {
  479. LOGV( "Device open failed" );
  480. }
  481. }
  482. }
  483. bool BOpenResult() const
  484. {
  485. return m_bOpenResult;
  486. }
  487. void ProcessInput( const uint8_t *pBuf, size_t nBufSize )
  488. {
  489. hid_mutex_guard l( &m_dataLock );
  490. size_t MAX_REPORT_QUEUE_SIZE = 16;
  491. if ( m_vecData.size() >= MAX_REPORT_QUEUE_SIZE )
  492. {
  493. m_vecData.pop_front();
  494. }
  495. m_vecData.emplace_back( pBuf, nBufSize );
  496. }
  497. int GetInput( unsigned char *data, size_t length )
  498. {
  499. hid_mutex_guard l( &m_dataLock );
  500. if ( m_vecData.size() == 0 )
  501. {
  502. // LOGV( "hid_read_timeout no data available" );
  503. return 0;
  504. }
  505. const hid_buffer &buffer = m_vecData.front();
  506. size_t nDataLen = buffer.size() > length ? length : buffer.size();
  507. if ( m_bIsBLESteamController )
  508. {
  509. if ( m_pInfo->product_id == TRITON_BLE_PID )
  510. {
  511. data[0] = 0x45;
  512. }
  513. else
  514. {
  515. data[0] = 0x03;
  516. }
  517. SDL_memcpy( data + 1, buffer.data(), nDataLen );
  518. ++nDataLen;
  519. }
  520. else
  521. {
  522. SDL_memcpy( data, buffer.data(), nDataLen );
  523. }
  524. m_vecData.pop_front();
  525. // LOGV("Read %u bytes", nDataLen);
  526. // LOGV("%02x %02x %02x %02x %02x %02x %02x %02x ....",
  527. // data[0], data[1], data[2], data[3],
  528. // data[4], data[5], data[6], data[7]);
  529. return (int)nDataLen;
  530. }
  531. int WriteReport( const unsigned char *pData, size_t nDataLen, bool bFeature )
  532. {
  533. JNIEnv *env = (JNIEnv *)SDL_GetAndroidJNIEnv();
  534. if ( !g_HIDDeviceManagerCallbackHandler )
  535. {
  536. LOGV( "WriteReport without callback handler" );
  537. return -1;
  538. }
  539. jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
  540. int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerWriteReport, m_nId, pBuf, bFeature );
  541. ExceptionCheck( env, "WriteReport" );
  542. env->DeleteLocalRef( pBuf );
  543. return nRet;
  544. }
  545. void ProcessReportResponse( const uint8_t *pBuf, size_t nBufSize )
  546. {
  547. hid_mutex_guard cvl( &m_cvLock );
  548. if ( m_bIsWaitingForReportResponse )
  549. {
  550. m_reportResponse.assign( pBuf, nBufSize );
  551. m_bIsWaitingForReportResponse = false;
  552. m_nReportResponseError = 0;
  553. pthread_cond_signal( &m_cv );
  554. }
  555. }
  556. int ReadReport( unsigned char *pData, size_t nDataLen, bool bFeature )
  557. {
  558. JNIEnv *env = (JNIEnv *)SDL_GetAndroidJNIEnv();
  559. if ( !g_HIDDeviceManagerCallbackHandler )
  560. {
  561. LOGV( "ReadReport without callback handler" );
  562. return -1;
  563. }
  564. {
  565. hid_mutex_guard cvl( &m_cvLock );
  566. if ( m_bIsWaitingForReportResponse )
  567. {
  568. LOGV( "Get feature report already ongoing... bail" );
  569. return -1; // Read already ongoing, we currently do not serialize, TODO
  570. }
  571. m_bIsWaitingForReportResponse = true;
  572. }
  573. jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
  574. int nRet = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerReadReport, m_nId, pBuf, bFeature ) ? 0 : -1;
  575. ExceptionCheck( env, "ReadReport" );
  576. env->DeleteLocalRef( pBuf );
  577. if ( nRet < 0 )
  578. {
  579. LOGV( "ReadReport failed" );
  580. m_bIsWaitingForReportResponse = false;
  581. return -1;
  582. }
  583. {
  584. hid_mutex_guard cvl( &m_cvLock );
  585. if ( m_bIsWaitingForReportResponse )
  586. {
  587. LOGV("=== Going to sleep" );
  588. // Wait in CV until we are no longer waiting for a feature report.
  589. const int FEATURE_REPORT_TIMEOUT_SECONDS = 2;
  590. struct timespec ts, endtime;
  591. clock_gettime( CLOCK_REALTIME, &ts );
  592. endtime = ts;
  593. endtime.tv_sec += FEATURE_REPORT_TIMEOUT_SECONDS;
  594. do
  595. {
  596. if ( pthread_cond_timedwait( &m_cv, &m_cvLock, &endtime ) != 0 )
  597. {
  598. break;
  599. }
  600. }
  601. while ( m_bIsWaitingForReportResponse && get_timespec_ms( ts ) < get_timespec_ms( endtime ) );
  602. // We are back
  603. if ( m_bIsWaitingForReportResponse )
  604. {
  605. m_nReportResponseError = -ETIMEDOUT;
  606. m_bIsWaitingForReportResponse = false;
  607. }
  608. LOGV( "=== Got feature report err=%d", m_nReportResponseError );
  609. if ( m_nReportResponseError != 0 )
  610. {
  611. return m_nReportResponseError;
  612. }
  613. }
  614. size_t uBytesToCopy = m_reportResponse.size() > nDataLen ? nDataLen : m_reportResponse.size();
  615. SDL_memcpy( pData, m_reportResponse.data(), uBytesToCopy );
  616. m_reportResponse.clear();
  617. LOGV( "=== Got %zu bytes", uBytesToCopy );
  618. return (int)uBytesToCopy;
  619. }
  620. }
  621. void Close( bool bDeleteDevice )
  622. {
  623. JNIEnv *env = (JNIEnv *)SDL_GetAndroidJNIEnv();
  624. if ( g_HIDDeviceManagerCallbackHandler )
  625. {
  626. if ( !m_bIsWaitingForOpen && m_bOpenResult )
  627. {
  628. env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
  629. ExceptionCheck( env, "Close" );
  630. }
  631. }
  632. hid_mutex_guard dataLock( &m_dataLock );
  633. m_vecData.clear();
  634. // Clean and release pending feature report reads
  635. hid_mutex_guard cvLock( &m_cvLock );
  636. m_reportResponse.clear();
  637. m_bIsWaitingForReportResponse = false;
  638. m_nReportResponseError = -ECONNRESET;
  639. pthread_cond_broadcast( &m_cv );
  640. m_bOpenResult = false;
  641. if ( bDeleteDevice )
  642. {
  643. delete m_pDevice;
  644. m_pDevice = nullptr;
  645. }
  646. }
  647. private:
  648. pthread_mutex_t m_refCountLock = PTHREAD_MUTEX_INITIALIZER;
  649. int m_nRefCount = 0;
  650. int m_nId = 0;
  651. hid_device_info *m_pInfo = nullptr;
  652. hid_device *m_pDevice = nullptr;
  653. bool m_bIsBLESteamController = false;
  654. pthread_mutex_t m_dataLock = PTHREAD_MUTEX_INITIALIZER; // This lock has to be held to access m_vecData
  655. hid_buffer_pool m_vecData;
  656. // For handling get_feature_report
  657. pthread_mutex_t m_cvLock = PTHREAD_MUTEX_INITIALIZER; // This lock has to be held to access any variables below
  658. pthread_cond_t m_cv = PTHREAD_COND_INITIALIZER;
  659. bool m_bIsWaitingForOpen = false;
  660. bool m_bWasOpenPending = false;
  661. bool m_bOpenResult = false;
  662. bool m_bIsWaitingForReportResponse = false;
  663. int m_nReportResponseError = 0;
  664. hid_buffer m_reportResponse;
  665. public:
  666. hid_device_ref<CHIDDevice> next;
  667. };
  668. class CHIDDevice;
  669. static pthread_mutex_t g_DevicesMutex = PTHREAD_MUTEX_INITIALIZER;
  670. static pthread_mutex_t g_DevicesRefCountMutex = PTHREAD_MUTEX_INITIALIZER;
  671. static hid_device_ref<CHIDDevice> g_Devices;
  672. static hid_device_ref<CHIDDevice> FindDevice( int nDeviceId )
  673. {
  674. hid_device_ref<CHIDDevice> pDevice;
  675. hid_mutex_guard l( &g_DevicesMutex );
  676. for ( pDevice = g_Devices; pDevice; pDevice = pDevice->next )
  677. {
  678. if ( pDevice->GetId() == nDeviceId )
  679. {
  680. break;
  681. }
  682. }
  683. return pDevice;
  684. }
  685. extern "C"
  686. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz);
  687. extern "C"
  688. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback)(JNIEnv *env, jobject thiz);
  689. extern "C"
  690. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceConnected)(JNIEnv *env, jobject thiz, int nDeviceID, jstring sIdentifier, int nVendorId, int nProductId, jstring sSerialNumber, int nReleaseNumber, jstring sManufacturer, jstring sProduct, int nInterface, int nInterfaceClass, int nInterfaceSubclass, int nInterfaceProtocol, bool bBluetooth );
  691. extern "C"
  692. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenPending)(JNIEnv *env, jobject thiz, int nDeviceID);
  693. extern "C"
  694. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenResult)(JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened);
  695. extern "C"
  696. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceDisconnected)(JNIEnv *env, jobject thiz, int nDeviceID);
  697. extern "C"
  698. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceInputReport)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value);
  699. extern "C"
  700. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReportResponse)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value);
  701. extern "C"
  702. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz )
  703. {
  704. LOGV( "HIDDeviceRegisterCallback()");
  705. if ( g_HIDDeviceManagerCallbackHandler != NULL )
  706. {
  707. env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
  708. g_HIDDeviceManagerCallbackClass = NULL;
  709. env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
  710. g_HIDDeviceManagerCallbackHandler = NULL;
  711. }
  712. g_HIDDeviceManagerCallbackHandler = env->NewGlobalRef( thiz );
  713. jclass objClass = env->GetObjectClass( thiz );
  714. if ( objClass )
  715. {
  716. g_HIDDeviceManagerCallbackClass = reinterpret_cast< jclass >( env->NewGlobalRef( objClass ) );
  717. g_midHIDDeviceManagerInitialize = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "initialize", "(ZZ)Z" );
  718. if ( !g_midHIDDeviceManagerInitialize )
  719. {
  720. __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing initialize" );
  721. }
  722. g_midHIDDeviceManagerOpen = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "openDevice", "(I)Z" );
  723. if ( !g_midHIDDeviceManagerOpen )
  724. {
  725. __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing openDevice" );
  726. }
  727. g_midHIDDeviceManagerWriteReport = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "writeReport", "(I[BZ)I" );
  728. if ( !g_midHIDDeviceManagerWriteReport )
  729. {
  730. __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing writeReport" );
  731. }
  732. g_midHIDDeviceManagerReadReport = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "readReport", "(I[BZ)Z" );
  733. if ( !g_midHIDDeviceManagerReadReport )
  734. {
  735. __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing getFeatureReport" );
  736. }
  737. g_midHIDDeviceManagerClose = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "closeDevice", "(I)V" );
  738. if ( !g_midHIDDeviceManagerClose )
  739. {
  740. __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing closeDevice" );
  741. }
  742. env->DeleteLocalRef( objClass );
  743. }
  744. }
  745. extern "C"
  746. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback)(JNIEnv *env, jobject thiz)
  747. {
  748. LOGV("HIDDeviceReleaseCallback");
  749. if ( env->IsSameObject( thiz, g_HIDDeviceManagerCallbackHandler ) )
  750. {
  751. env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
  752. g_HIDDeviceManagerCallbackClass = NULL;
  753. env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
  754. g_HIDDeviceManagerCallbackHandler = NULL;
  755. g_initialized = false;
  756. }
  757. }
  758. extern "C"
  759. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceConnected)(JNIEnv *env, jobject thiz, int nDeviceID, jstring sIdentifier, int nVendorId, int nProductId, jstring sSerialNumber, int nReleaseNumber, jstring sManufacturer, jstring sProduct, int nInterface, int nInterfaceClass, int nInterfaceSubclass, int nInterfaceProtocol, bool bBluetooth )
  760. {
  761. LOGV( "HIDDeviceConnected() id=%d VID/PID = %.4x/%.4x, interface %d\n", nDeviceID, nVendorId, nProductId, nInterface );
  762. hid_device_info *pInfo = new hid_device_info;
  763. SDL_memset( pInfo, 0, sizeof( *pInfo ) );
  764. pInfo->path = CreateStringFromJString( env, sIdentifier );
  765. pInfo->vendor_id = nVendorId;
  766. pInfo->product_id = nProductId;
  767. pInfo->serial_number = CreateWStringFromJString( env, sSerialNumber );
  768. pInfo->release_number = nReleaseNumber;
  769. pInfo->manufacturer_string = CreateWStringFromJString( env, sManufacturer );
  770. pInfo->product_string = CreateWStringFromJString( env, sProduct );
  771. pInfo->interface_number = nInterface;
  772. pInfo->interface_class = nInterfaceClass;
  773. pInfo->interface_subclass = nInterfaceSubclass;
  774. pInfo->interface_protocol = nInterfaceProtocol;
  775. if ( bBluetooth )
  776. {
  777. pInfo->bus_type = HID_API_BUS_BLUETOOTH;
  778. }
  779. else
  780. {
  781. pInfo->bus_type = HID_API_BUS_USB;
  782. }
  783. hid_device_ref<CHIDDevice> pDevice( new CHIDDevice( nDeviceID, pInfo ) );
  784. hid_mutex_guard l( &g_DevicesMutex );
  785. hid_device_ref<CHIDDevice> pLast, pCurr;
  786. for ( pCurr = g_Devices; pCurr; pLast = pCurr, pCurr = pCurr->next )
  787. {
  788. continue;
  789. }
  790. if ( pLast )
  791. {
  792. pLast->next = pDevice;
  793. }
  794. else
  795. {
  796. g_Devices = pDevice;
  797. }
  798. }
  799. extern "C"
  800. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenPending)(JNIEnv *env, jobject thiz, int nDeviceID)
  801. {
  802. LOGV( "HIDDeviceOpenPending() id=%d\n", nDeviceID );
  803. hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
  804. if ( pDevice )
  805. {
  806. pDevice->SetOpenPending();
  807. }
  808. }
  809. extern "C"
  810. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenResult)(JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened)
  811. {
  812. LOGV( "HIDDeviceOpenResult() id=%d, result=%s\n", nDeviceID, bOpened ? "true" : "false" );
  813. hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
  814. if ( pDevice )
  815. {
  816. pDevice->SetOpenResult( bOpened );
  817. }
  818. }
  819. extern "C"
  820. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceDisconnected)(JNIEnv *env, jobject thiz, int nDeviceID)
  821. {
  822. LOGV( "HIDDeviceDisconnected() id=%d\n", nDeviceID );
  823. hid_device_ref<CHIDDevice> pDevice;
  824. {
  825. hid_mutex_guard l( &g_DevicesMutex );
  826. hid_device_ref<CHIDDevice> pLast, pCurr;
  827. for ( pCurr = g_Devices; pCurr; pLast = pCurr, pCurr = pCurr->next )
  828. {
  829. if ( pCurr->GetId() == nDeviceID )
  830. {
  831. pDevice = pCurr;
  832. if ( pLast )
  833. {
  834. pLast->next = pCurr->next;
  835. }
  836. else
  837. {
  838. g_Devices = pCurr->next;
  839. }
  840. }
  841. }
  842. }
  843. if ( pDevice )
  844. {
  845. pDevice->Close( false );
  846. }
  847. }
  848. extern "C"
  849. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceInputReport)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value)
  850. {
  851. jbyte *pBuf = env->GetByteArrayElements(value, NULL);
  852. jsize nBufSize = env->GetArrayLength(value);
  853. // LOGV( "HIDDeviceInput() id=%d len=%u\n", nDeviceID, nBufSize );
  854. hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
  855. if ( pDevice )
  856. {
  857. pDevice->ProcessInput( reinterpret_cast< const uint8_t * >( pBuf ), nBufSize );
  858. }
  859. env->ReleaseByteArrayElements(value, pBuf, 0);
  860. }
  861. extern "C"
  862. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReportResponse)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value)
  863. {
  864. jbyte *pBuf = env->GetByteArrayElements(value, NULL);
  865. jsize nBufSize = env->GetArrayLength(value);
  866. LOGV( "HIDDeviceReportResponse() id=%d len=%u\n", nDeviceID, nBufSize );
  867. hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
  868. if ( pDevice )
  869. {
  870. pDevice->ProcessReportResponse( reinterpret_cast< const uint8_t * >( pBuf ), nBufSize );
  871. }
  872. env->ReleaseByteArrayElements(value, pBuf, 0);
  873. }
  874. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  875. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  876. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  877. extern "C"
  878. {
  879. static void SDLCALL RequestBluetoothPermissionCallback( void *userdata, const char *permission, bool granted )
  880. {
  881. SDL_Log( "Bluetooth permission %s", granted ? "granted" : "denied" );
  882. if ( granted && g_HIDDeviceManagerCallbackHandler )
  883. {
  884. JNIEnv *env = (JNIEnv *)SDL_GetAndroidJNIEnv();
  885. env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, false, true );
  886. }
  887. }
  888. int hid_init(void)
  889. {
  890. if ( !g_initialized && g_HIDDeviceManagerCallbackHandler )
  891. {
  892. // HIDAPI doesn't work well with Android < 4.3
  893. if ( SDL_GetAndroidSDKVersion() >= 18 )
  894. {
  895. JNIEnv *env = (JNIEnv *)SDL_GetAndroidJNIEnv();
  896. env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, true, false );
  897. // Bluetooth is currently only used for Steam Controllers, so check that hint
  898. // before initializing Bluetooth, which will prompt the user for permission.
  899. if ( SDL_GetHintBoolean( SDL_HINT_JOYSTICK_HIDAPI_STEAM, false ) )
  900. {
  901. if ( SDL_GetAndroidSDKVersion() < 31 )
  902. {
  903. env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, false, true );
  904. }
  905. else
  906. {
  907. SDL_Log( "Requesting Bluetooth permission" );
  908. SDL_RequestAndroidPermission( "android.permission.BLUETOOTH_CONNECT", RequestBluetoothPermissionCallback, NULL );
  909. }
  910. }
  911. ExceptionCheck( env, NULL, "hid_init" );
  912. }
  913. g_initialized = true; // Regardless of result, so it's only called once
  914. }
  915. return 0;
  916. }
  917. struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  918. {
  919. struct hid_device_info *root = NULL;
  920. hid_mutex_guard l( &g_DevicesMutex );
  921. for ( hid_device_ref<CHIDDevice> pDevice = g_Devices; pDevice; pDevice = pDevice->next )
  922. {
  923. // Don't enumerate devices that are currently being opened, we'll re-enumerate them when we're done
  924. // Make sure we skip them at least once, so they get removed and then re-added to the caller's device list
  925. if ( pDevice->BWasOpenPending() )
  926. {
  927. // Don't enumerate devices that failed to open, otherwise the application might try to keep prompting for access
  928. if ( !pDevice->BOpenPending() && pDevice->BOpenResult() )
  929. {
  930. pDevice->SetWasOpenPending( false );
  931. }
  932. continue;
  933. }
  934. const hid_device_info *info = pDevice->GetDeviceInfo();
  935. /* See if there are any devices we should skip in enumeration */
  936. if (SDL_HIDAPI_ShouldIgnoreDevice(HID_API_BUS_UNKNOWN, info->vendor_id, info->product_id, 0, 0, false)) {
  937. continue;
  938. }
  939. if ( ( vendor_id == 0x0 || info->vendor_id == vendor_id ) &&
  940. ( product_id == 0x0 || info->product_id == product_id ) )
  941. {
  942. hid_device_info *dev = CopyHIDDeviceInfo( info );
  943. dev->next = root;
  944. root = dev;
  945. }
  946. }
  947. return root;
  948. }
  949. void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
  950. {
  951. while ( devs )
  952. {
  953. struct hid_device_info *next = devs->next;
  954. FreeHIDDeviceInfo( devs );
  955. devs = next;
  956. }
  957. }
  958. HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  959. {
  960. // TODO: Implement
  961. return NULL;
  962. }
  963. HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
  964. {
  965. LOGV( "hid_open_path( %s )", path );
  966. hid_device_ref< CHIDDevice > pDevice;
  967. {
  968. hid_mutex_guard r( &g_DevicesRefCountMutex );
  969. hid_mutex_guard l( &g_DevicesMutex );
  970. for ( hid_device_ref<CHIDDevice> pCurr = g_Devices; pCurr; pCurr = pCurr->next )
  971. {
  972. if ( SDL_strcmp( pCurr->GetDeviceInfo()->path, path ) == 0 )
  973. {
  974. hid_device *pValue = pCurr->GetDevice();
  975. if ( pValue )
  976. {
  977. ++pValue->m_nDeviceRefCount;
  978. LOGD("Incrementing device %d (%p), refCount = %d\n", pValue->m_nId, pValue, pValue->m_nDeviceRefCount);
  979. return pValue;
  980. }
  981. // Hold a shared pointer to the controller for the duration
  982. pDevice = pCurr;
  983. break;
  984. }
  985. }
  986. }
  987. if ( !pDevice )
  988. {
  989. SDL_SetError( "Couldn't find device with path %s", path );
  990. return NULL;
  991. }
  992. if ( pDevice->BOpen() )
  993. {
  994. return pDevice->GetDevice();
  995. }
  996. return NULL;
  997. }
  998. int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
  999. {
  1000. if ( device )
  1001. {
  1002. // LOGV( "hid_write id=%d length=%zu", device->m_nId, length );
  1003. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1004. if ( pDevice )
  1005. {
  1006. return pDevice->WriteReport( data, length, false );
  1007. }
  1008. }
  1009. return -1; // Controller was disconnected
  1010. }
  1011. static uint32_t getms()
  1012. {
  1013. struct timeval now;
  1014. gettimeofday(&now, NULL);
  1015. return (uint32_t)(now.tv_sec * 1000 + now.tv_usec / 1000);
  1016. }
  1017. static void delayms(uint32_t ms)
  1018. {
  1019. int was_error;
  1020. struct timespec elapsed, tv;
  1021. /* Set the timeout interval */
  1022. elapsed.tv_sec = ms / 1000;
  1023. elapsed.tv_nsec = (ms % 1000) * 1000000;
  1024. do {
  1025. errno = 0;
  1026. tv.tv_sec = elapsed.tv_sec;
  1027. tv.tv_nsec = elapsed.tv_nsec;
  1028. was_error = nanosleep(&tv, &elapsed);
  1029. } while (was_error && (errno == EINTR));
  1030. }
  1031. int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
  1032. {
  1033. if ( device )
  1034. {
  1035. // LOGV( "hid_read_timeout id=%d length=%u timeout=%d", device->m_nId, length, milliseconds );
  1036. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1037. if ( pDevice )
  1038. {
  1039. int nResult = pDevice->GetInput( data, length );
  1040. if ( nResult == 0 && milliseconds > 0 )
  1041. {
  1042. uint32_t start = getms();
  1043. do
  1044. {
  1045. delayms( 1 );
  1046. nResult = pDevice->GetInput( data, length );
  1047. } while ( nResult == 0 && ( getms() - start ) < milliseconds );
  1048. }
  1049. return nResult;
  1050. }
  1051. LOGV( "controller was disconnected" );
  1052. }
  1053. return -1; // Controller was disconnected
  1054. }
  1055. // TODO: Implement blocking
  1056. int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
  1057. {
  1058. // LOGV( "hid_read id=%d length=%zu", device->m_nId, length );
  1059. return hid_read_timeout( device, data, length, 0 );
  1060. }
  1061. // TODO: Implement?
  1062. int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
  1063. {
  1064. return -1;
  1065. }
  1066. int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
  1067. {
  1068. if ( device )
  1069. {
  1070. LOGV( "hid_send_feature_report id=%d length=%zu", device->m_nId, length );
  1071. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1072. if ( pDevice )
  1073. {
  1074. return pDevice->WriteReport( data, length, true );
  1075. }
  1076. }
  1077. return -1; // Controller was disconnected
  1078. }
  1079. // Synchronous operation. Will block until completed.
  1080. int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
  1081. {
  1082. if ( device )
  1083. {
  1084. LOGV( "hid_get_feature_report id=%d length=%zu", device->m_nId, length );
  1085. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1086. if ( pDevice )
  1087. {
  1088. return pDevice->ReadReport( data, length, true );
  1089. }
  1090. }
  1091. return -1; // Controller was disconnected
  1092. }
  1093. // Synchronous operation. Will block until completed.
  1094. int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *device, unsigned char *data, size_t length)
  1095. {
  1096. if ( device )
  1097. {
  1098. LOGV( "hid_get_input_report id=%d length=%zu", device->m_nId, length );
  1099. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1100. if ( pDevice )
  1101. {
  1102. return pDevice->ReadReport( data, length, false );
  1103. }
  1104. }
  1105. return -1; // Controller was disconnected
  1106. }
  1107. void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
  1108. {
  1109. if ( device )
  1110. {
  1111. LOGV( "hid_close id=%d", device->m_nId );
  1112. hid_mutex_guard r( &g_DevicesRefCountMutex );
  1113. LOGD("Decrementing device %d (%p), refCount = %d\n", device->m_nId, device, device->m_nDeviceRefCount - 1);
  1114. if ( --device->m_nDeviceRefCount == 0 )
  1115. {
  1116. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1117. if ( pDevice )
  1118. {
  1119. pDevice->Close( true );
  1120. }
  1121. else
  1122. {
  1123. delete device;
  1124. }
  1125. LOGD("Deleted device %p\n", device);
  1126. }
  1127. }
  1128. }
  1129. int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
  1130. {
  1131. if ( device )
  1132. {
  1133. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1134. if ( pDevice )
  1135. {
  1136. wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen );
  1137. return 0;
  1138. }
  1139. }
  1140. return -1;
  1141. }
  1142. int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
  1143. {
  1144. if ( device )
  1145. {
  1146. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1147. if ( pDevice )
  1148. {
  1149. wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen );
  1150. return 0;
  1151. }
  1152. }
  1153. return -1;
  1154. }
  1155. int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
  1156. {
  1157. if ( device )
  1158. {
  1159. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1160. if ( pDevice )
  1161. {
  1162. wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen );
  1163. return 0;
  1164. }
  1165. }
  1166. return -1;
  1167. }
  1168. int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  1169. {
  1170. return -1;
  1171. }
  1172. struct hid_device_info *hid_get_device_info(hid_device *device)
  1173. {
  1174. if ( device )
  1175. {
  1176. hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
  1177. if ( pDevice )
  1178. {
  1179. return pDevice->GetDeviceInfo();
  1180. }
  1181. }
  1182. return NULL;
  1183. }
  1184. int hid_get_report_descriptor(hid_device *device, unsigned char *buf, size_t buf_size)
  1185. {
  1186. // Not implemented
  1187. return -1;
  1188. }
  1189. HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *device)
  1190. {
  1191. return NULL;
  1192. }
  1193. int hid_exit(void)
  1194. {
  1195. return 0;
  1196. }
  1197. }
  1198. #else
  1199. extern "C"
  1200. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz);
  1201. extern "C"
  1202. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback)(JNIEnv *env, jobject thiz);
  1203. extern "C"
  1204. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceConnected)(JNIEnv *env, jobject thiz, int nDeviceID, jstring sIdentifier, int nVendorId, int nProductId, jstring sSerialNumber, int nReleaseNumber, jstring sManufacturer, jstring sProduct, int nInterface, int nInterfaceClass, int nInterfaceSubclass, int nInterfaceProtocol, bool bBluetooth );
  1205. extern "C"
  1206. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenPending)(JNIEnv *env, jobject thiz, int nDeviceID);
  1207. extern "C"
  1208. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenResult)(JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened);
  1209. extern "C"
  1210. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceDisconnected)(JNIEnv *env, jobject thiz, int nDeviceID);
  1211. extern "C"
  1212. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceInputReport)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value);
  1213. extern "C"
  1214. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReportResponse)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value);
  1215. extern "C"
  1216. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz )
  1217. {
  1218. LOGV("Stub HIDDeviceRegisterCallback()");
  1219. }
  1220. extern "C"
  1221. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback)(JNIEnv *env, jobject thiz)
  1222. {
  1223. LOGV("Stub HIDDeviceReleaseCallback()");
  1224. }
  1225. extern "C"
  1226. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceConnected)(JNIEnv *env, jobject thiz, int nDeviceID, jstring sIdentifier, int nVendorId, int nProductId, jstring sSerialNumber, int nReleaseNumber, jstring sManufacturer, jstring sProduct, int nInterface, int nInterfaceClass, int nInterfaceSubclass, int nInterfaceProtocol, bool bBluetooth )
  1227. {
  1228. LOGV("Stub HIDDeviceConnected() id=%d VID/PID = %.4x/%.4x, interface %d\n", nDeviceID, nVendorId, nProductId, nInterface);
  1229. }
  1230. extern "C"
  1231. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenPending)(JNIEnv *env, jobject thiz, int nDeviceID)
  1232. {
  1233. LOGV("Stub HIDDeviceOpenPending() id=%d\n", nDeviceID);
  1234. }
  1235. extern "C"
  1236. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenResult)(JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened)
  1237. {
  1238. LOGV("Stub HIDDeviceOpenResult() id=%d, result=%s\n", nDeviceID, bOpened ? "true" : "false");
  1239. }
  1240. extern "C"
  1241. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceDisconnected)(JNIEnv *env, jobject thiz, int nDeviceID)
  1242. {
  1243. LOGV("Stub HIDDeviceDisconnected() id=%d\n", nDeviceID);
  1244. }
  1245. extern "C"
  1246. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceInputReport)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value)
  1247. {
  1248. LOGV("Stub HIDDeviceInput() id=%d len=%u\n", nDeviceID, nBufSize);
  1249. }
  1250. extern "C"
  1251. JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReportResponse)(JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value)
  1252. {
  1253. LOGV("Stub HIDDeviceReportResponse() id=%d len=%u\n", nDeviceID, nBufSize);
  1254. }
  1255. #endif /* SDL_HIDAPI_DISABLED */
  1256. extern "C"
  1257. JNINativeMethod HIDDeviceManager_tab[8] = {
  1258. { "HIDDeviceRegisterCallback", "()V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback) },
  1259. { "HIDDeviceReleaseCallback", "()V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback) },
  1260. { "HIDDeviceConnected", "(ILjava/lang/String;IILjava/lang/String;ILjava/lang/String;Ljava/lang/String;IIIIZ)V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceConnected) },
  1261. { "HIDDeviceOpenPending", "(I)V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenPending) },
  1262. { "HIDDeviceOpenResult", "(IZ)V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceOpenResult) },
  1263. { "HIDDeviceDisconnected", "(I)V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceDisconnected) },
  1264. { "HIDDeviceInputReport", "(I[B)V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceInputReport) },
  1265. { "HIDDeviceReportResponse", "(I[B)V", (void*)HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReportResponse) }
  1266. };