SDL  2.0
hid.cpp File Reference
#include <jni.h>
#include <android/log.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "../hidapi/hidapi.h"
+ Include dependency graph for hid.cpp:

Go to the source code of this file.

Data Structures

struct  hid_device
 
class  hid_device_ref< T >
 
class  hid_mutex_guard
 
class  hid_buffer
 
class  hid_buffer_pool
 
struct  hid_buffer_pool::hid_buffer_entry
 
class  CHIDDevice
 

Macros

#define TAG   "hidapi"
 
#define LOGE(...)   __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
 
#define LOGV(...)
 
#define LOGD(...)
 
#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)
 

Typedefs

typedef uint32_t uint32
 
typedef uint64_t uint64
 

Functions

static jbyteArray NewByteArray (JNIEnv *env, const uint8_t *pData, size_t nDataLen)
 
static char * CreateStringFromJString (JNIEnv *env, const jstring &sString)
 
static wchar_t * CreateWStringFromJString (JNIEnv *env, const jstring &sString)
 
static wchar_t * CreateWStringFromWString (const wchar_t *pwSrc)
 
static hid_device_infoCopyHIDDeviceInfo (const hid_device_info *pInfo)
 
static void FreeHIDDeviceInfo (hid_device_info *pInfo)
 
static uint64_t get_timespec_ms (const struct timespec &ts)
 
static hid_device_ref< CHIDDeviceFindDevice (int nDeviceId)
 
static void ThreadDestroyed (void *value)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceRegisterCallback (JNIEnv *env, jobject thiz)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceReleaseCallback (JNIEnv *env, jobject thiz)
 
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)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenPending (JNIEnv *env, jobject thiz, int nDeviceID)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenResult (JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected (JNIEnv *env, jobject thiz, int nDeviceID)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceInputReport (JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value)
 
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceFeatureReport (JNIEnv *env, jobject thiz, int nDeviceID, jbyteArray value)
 
int hid_init (void)
 Initialize the HIDAPI library. More...
 
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate (unsigned short vendor_id, unsigned short product_id)
 Enumerate the HID Devices. More...
 
void HID_API_EXPORT HID_API_CALL hid_free_enumeration (struct hid_device_info *devs)
 Free an enumeration Linked List. More...
 
HID_API_EXPORT hid_device *HID_API_CALL hid_open (unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
 Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number. More...
 
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path (const char *path, int bExclusive)
 Open a HID device by its path name. More...
 
int HID_API_EXPORT HID_API_CALL hid_write (hid_device *device, const unsigned char *data, size_t length)
 Write an Output report to a HID device. More...
 
int HID_API_EXPORT HID_API_CALL hid_read_timeout (hid_device *device, unsigned char *data, size_t length, int milliseconds)
 Read an Input report from a HID device with timeout. More...
 
int HID_API_EXPORT HID_API_CALL hid_read (hid_device *device, unsigned char *data, size_t length)
 Read an Input report from a HID device. More...
 
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking (hid_device *device, int nonblock)
 Set the device handle to be non-blocking. More...
 
int HID_API_EXPORT HID_API_CALL hid_send_feature_report (hid_device *device, const unsigned char *data, size_t length)
 Send a Feature report to the device. More...
 
int HID_API_EXPORT HID_API_CALL hid_get_feature_report (hid_device *device, unsigned char *data, size_t length)
 Get a feature report from a HID device. More...
 
void HID_API_EXPORT HID_API_CALL hid_close (hid_device *device)
 Close a HID device. More...
 
int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device *device, wchar_t *string, size_t maxlen)
 Get The Manufacturer String from a HID device. More...
 
int HID_API_EXPORT_CALL hid_get_product_string (hid_device *device, wchar_t *string, size_t maxlen)
 Get The Product String from a HID device. More...
 
int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device *device, wchar_t *string, size_t maxlen)
 Get The Serial Number String from a HID device. More...
 
int HID_API_EXPORT_CALL hid_get_indexed_string (hid_device *device, int string_index, wchar_t *string, size_t maxlen)
 Get a string from a HID device, based on its string index. More...
 
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error (hid_device *device)
 Get a string describing the last error which occurred. More...
 
int hid_exit (void)
 Finalize the HIDAPI library. More...
 

Variables

static JavaVM * g_JVM
 
static pthread_key_t g_ThreadKey
 
static jclass g_HIDDeviceManagerCallbackClass
 
static jobject g_HIDDeviceManagerCallbackHandler
 
static jmethodID g_midHIDDeviceManagerOpen
 
static jmethodID g_midHIDDeviceManagerSendOutputReport
 
static jmethodID g_midHIDDeviceManagerSendFeatureReport
 
static jmethodID g_midHIDDeviceManagerGetFeatureReport
 
static jmethodID g_midHIDDeviceManagerClose
 
static pthread_mutex_t g_DevicesMutex = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t g_DevicesRefCountMutex = PTHREAD_MUTEX_INITIALIZER
 
static hid_device_ref< CHIDDeviceg_Devices
 

Macro Definition Documentation

◆ CONCAT1

#define CONCAT1 (   prefix,
  class,
  function 
)    CONCAT2(prefix, class, function)

Definition at line 30 of file hid.cpp.

◆ CONCAT2

#define CONCAT2 (   prefix,
  class,
  function 
)    Java_ ## prefix ## _ ## class ## _ ## function

Definition at line 31 of file hid.cpp.

◆ HID_DEVICE_MANAGER_JAVA_INTERFACE

#define HID_DEVICE_MANAGER_JAVA_INTERFACE (   function)    CONCAT1(SDL_JAVA_PREFIX, HIDDeviceManager, function)

Definition at line 32 of file hid.cpp.

Referenced by ThreadDestroyed().

◆ LOGD

#define LOGD (   ...)

Definition at line 26 of file hid.cpp.

Referenced by CHIDDevice::BOpen(), hid_close(), and hid_open_path().

◆ LOGE

#define LOGE (   ...)    __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)

Definition at line 19 of file hid.cpp.

Referenced by CHIDDevice::ExceptionCheck().

◆ LOGV

◆ SDL_JAVA_PREFIX

#define SDL_JAVA_PREFIX   org_libsdl_app

Definition at line 29 of file hid.cpp.

◆ TAG

#define TAG   "hidapi"

Definition at line 16 of file hid.cpp.

Referenced by HIDDeviceRegisterCallback().

Typedef Documentation

◆ uint32

typedef uint32_t uint32

Definition at line 36 of file hid.cpp.

◆ uint64

typedef uint64_t uint64

Definition at line 37 of file hid.cpp.

Function Documentation

◆ CopyHIDDeviceInfo()

static hid_device_info* CopyHIDDeviceInfo ( const hid_device_info pInfo)
static

Definition at line 315 of file hid.cpp.

References CreateWStringFromWString(), hid_device_info::manufacturer_string, hid_device_info::path, hid_device_info::product_string, and hid_device_info::serial_number.

Referenced by hid_enumerate().

316 {
317  hid_device_info *pCopy = new hid_device_info;
318  *pCopy = *pInfo;
319  pCopy->path = strdup( pInfo->path );
323  return pCopy;
324 }
wchar_t * manufacturer_string
Definition: hidapi.h:66
char * path
Definition: hidapi.h:55
wchar_t * serial_number
Definition: hidapi.h:61
wchar_t * product_string
Definition: hidapi.h:68
static wchar_t * CreateWStringFromWString(const wchar_t *pwSrc)
Definition: hid.cpp:306

◆ CreateStringFromJString()

static char* CreateStringFromJString ( JNIEnv *  env,
const jstring &  sString 
)
static

Definition at line 280 of file hid.cpp.

References malloc, memcpy, and NULL.

Referenced by HIDDeviceConnected().

281 {
282  size_t nLength = env->GetStringUTFLength( sString );
283  const char *pjChars = env->GetStringUTFChars( sString, NULL );
284  char *psString = (char*)malloc( nLength + 1 );
285  memcpy( psString, pjChars, nLength );
286  psString[ nLength ] = '\0';
287  env->ReleaseStringUTFChars( sString, pjChars );
288  return psString;
289 }
#define NULL
Definition: begin_code.h:164
#define memcpy
Definition: SDL_malloc.c:622
#define malloc
Definition: SDL_qsort.c:47

◆ CreateWStringFromJString()

static wchar_t* CreateWStringFromJString ( JNIEnv *  env,
const jstring &  sString 
)
static

Definition at line 291 of file hid.cpp.

References malloc, and NULL.

Referenced by HIDDeviceConnected().

292 {
293  size_t nLength = env->GetStringLength( sString );
294  const jchar *pjChars = env->GetStringChars( sString, NULL );
295  wchar_t *pwString = (wchar_t*)malloc( ( nLength + 1 ) * sizeof( wchar_t ) );
296  wchar_t *pwChars = pwString;
297  for ( size_t iIndex = 0; iIndex < nLength; ++iIndex )
298  {
299  pwChars[ iIndex ] = pjChars[ iIndex ];
300  }
301  pwString[ nLength ] = '\0';
302  env->ReleaseStringChars( sString, pjChars );
303  return pwString;
304 }
#define NULL
Definition: begin_code.h:164
#define malloc
Definition: SDL_qsort.c:47

◆ CreateWStringFromWString()

static wchar_t* CreateWStringFromWString ( const wchar_t *  pwSrc)
static

Definition at line 306 of file hid.cpp.

References malloc, and memcpy.

Referenced by CopyHIDDeviceInfo().

307 {
308  size_t nLength = wcslen( pwSrc );
309  wchar_t *pwString = (wchar_t*)malloc( ( nLength + 1 ) * sizeof( wchar_t ) );
310  memcpy( pwString, pwSrc, nLength * sizeof( wchar_t ) );
311  pwString[ nLength ] = '\0';
312  return pwString;
313 }
#define memcpy
Definition: SDL_malloc.c:622
#define malloc
Definition: SDL_qsort.c:47

◆ FindDevice()

static hid_device_ref<CHIDDevice> FindDevice ( int  nDeviceId)
static

Definition at line 709 of file hid.cpp.

References g_DevicesMutex, CHIDDevice::GetId(), and CHIDDevice::next.

Referenced by hid_close(), hid_get_feature_report(), hid_get_manufacturer_string(), hid_get_product_string(), hid_get_serial_number_string(), hid_read_timeout(), hid_send_feature_report(), hid_write(), HIDDeviceFeatureReport(), HIDDeviceInputReport(), HIDDeviceOpenPending(), and HIDDeviceOpenResult().

710 {
712 
714  for ( pDevice = g_Devices; pDevice; pDevice = pDevice->next )
715  {
716  if ( pDevice->GetId() == nDeviceId )
717  {
718  break;
719  }
720  }
721  return pDevice;
722 }
hid_device_ref< CHIDDevice > next
Definition: hid.cpp:701
static pthread_mutex_t g_DevicesMutex
Definition: hid.cpp:705
static hid_device_ref< CHIDDevice > g_Devices
Definition: hid.cpp:707
int GetId()
Definition: hid.cpp:390

◆ FreeHIDDeviceInfo()

static void FreeHIDDeviceInfo ( hid_device_info pInfo)
static

Definition at line 326 of file hid.cpp.

References free, hid_device_info::manufacturer_string, hid_device_info::path, hid_device_info::product_string, and hid_device_info::serial_number.

Referenced by hid_free_enumeration(), and CHIDDevice::~CHIDDevice().

327 {
328  free( pInfo->path );
329  free( pInfo->serial_number );
330  free( pInfo->manufacturer_string );
331  free( pInfo->product_string );
332  delete pInfo;
333 }
SDL_EventEntry * free
Definition: SDL_events.c:84
wchar_t * manufacturer_string
Definition: hidapi.h:66
char * path
Definition: hidapi.h:55
wchar_t * serial_number
Definition: hidapi.h:61
wchar_t * product_string
Definition: hidapi.h:68

◆ get_timespec_ms()

static uint64_t get_timespec_ms ( const struct timespec &  ts)
static

Definition at line 343 of file hid.cpp.

Referenced by CHIDDevice::BOpen(), and CHIDDevice::GetFeatureReport().

344 {
345  return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
346 }
unsigned long long uint64_t

◆ HIDDeviceConnected()

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 
)

Definition at line 831 of file hid.cpp.

References CHIDDevice::CHIDDevice(), CreateStringFromJString(), CreateWStringFromJString(), g_DevicesMutex, hid_device_info::interface_number, LOGV, hid_device_info::manufacturer_string, memset, CHIDDevice::next, hid_device_info::path, hid_device_info::product_id, hid_device_info::product_string, hid_device_info::release_number, hid_device_info::serial_number, and hid_device_info::vendor_id.

Referenced by ThreadDestroyed().

832 {
833  LOGV( "HIDDeviceConnected() id=%d VID/PID = %.4x/%.4x, interface %d\n", nDeviceID, nVendorId, nProductId, nInterface );
834 
835  hid_device_info *pInfo = new hid_device_info;
836  memset( pInfo, 0, sizeof( *pInfo ) );
837  pInfo->path = CreateStringFromJString( env, sIdentifier );
838  pInfo->vendor_id = nVendorId;
839  pInfo->product_id = nProductId;
840  pInfo->serial_number = CreateWStringFromJString( env, sSerialNumber );
841  pInfo->release_number = nReleaseNumber;
842  pInfo->manufacturer_string = CreateWStringFromJString( env, sManufacturer );
843  pInfo->product_string = CreateWStringFromJString( env, sProduct );
844  pInfo->interface_number = nInterface;
845 
846  hid_device_ref<CHIDDevice> pDevice( new CHIDDevice( nDeviceID, pInfo ) );
847 
849  hid_device_ref<CHIDDevice> pLast, pCurr;
850  for ( pCurr = g_Devices; pCurr; pLast = pCurr, pCurr = pCurr->next )
851  {
852  continue;
853  }
854  if ( pLast )
855  {
856  pLast->next = pDevice;
857  }
858  else
859  {
860  g_Devices = pDevice;
861  }
862 }
int interface_number
Definition: hidapi.h:79
#define memset
Definition: SDL_malloc.c:619
hid_device_ref< CHIDDevice > next
Definition: hid.cpp:701
wchar_t * manufacturer_string
Definition: hidapi.h:66
char * path
Definition: hidapi.h:55
static pthread_mutex_t g_DevicesMutex
Definition: hid.cpp:705
static hid_device_ref< CHIDDevice > g_Devices
Definition: hid.cpp:707
unsigned short product_id
Definition: hidapi.h:59
wchar_t * serial_number
Definition: hidapi.h:61
static char * CreateStringFromJString(JNIEnv *env, const jstring &sString)
Definition: hid.cpp:280
unsigned short vendor_id
Definition: hidapi.h:57
wchar_t * product_string
Definition: hidapi.h:68
#define LOGV(...)
Definition: hid.cpp:25
unsigned short release_number
Definition: hidapi.h:64
static wchar_t * CreateWStringFromJString(JNIEnv *env, const jstring &sString)
Definition: hid.cpp:291

◆ HIDDeviceDisconnected()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected ( JNIEnv *  env,
jobject  thiz,
int  nDeviceID 
)

Definition at line 887 of file hid.cpp.

References CHIDDevice::Close(), g_DevicesMutex, LOGV, and CHIDDevice::next.

Referenced by ThreadDestroyed().

888 {
889  LOGV( "HIDDeviceDisconnected() id=%d\n", nDeviceID );
891  {
893  hid_device_ref<CHIDDevice> pLast, pCurr;
894  for ( pCurr = g_Devices; pCurr; pLast = pCurr, pCurr = pCurr->next )
895  {
896  if ( pCurr->GetId() == nDeviceID )
897  {
898  pDevice = pCurr;
899 
900  if ( pLast )
901  {
902  pLast->next = pCurr->next;
903  }
904  else
905  {
906  g_Devices = pCurr->next;
907  }
908  }
909  }
910  }
911  if ( pDevice )
912  {
913  pDevice->Close( false );
914  }
915 }
hid_device_ref< CHIDDevice > next
Definition: hid.cpp:701
static pthread_mutex_t g_DevicesMutex
Definition: hid.cpp:705
static hid_device_ref< CHIDDevice > g_Devices
Definition: hid.cpp:707
void Close(bool bDeleteDevice)
Definition: hid.cpp:653
#define LOGV(...)
Definition: hid.cpp:25

◆ HIDDeviceFeatureReport()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceFeatureReport ( JNIEnv *  env,
jobject  thiz,
int  nDeviceID,
jbyteArray  value 
)

Definition at line 934 of file hid.cpp.

References FindDevice(), LOGV, NULL, and CHIDDevice::ProcessFeatureReport().

Referenced by ThreadDestroyed().

935 {
936  jbyte *pBuf = env->GetByteArrayElements(value, NULL);
937  jsize nBufSize = env->GetArrayLength(value);
938 
939  LOGV( "HIDDeviceFeatureReport() id=%d len=%u\n", nDeviceID, nBufSize );
940  hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
941  if ( pDevice )
942  {
943  pDevice->ProcessFeatureReport( reinterpret_cast< const uint8_t* >( pBuf ), nBufSize );
944  }
945 
946  env->ReleaseByteArrayElements(value, pBuf, 0);
947 }
void ProcessFeatureReport(const uint8_t *pBuf, size_t nBufSize)
Definition: hid.cpp:570
GLsizei const GLfloat * value
#define NULL
Definition: begin_code.h:164
#define LOGV(...)
Definition: hid.cpp:25
static hid_device_ref< CHIDDevice > FindDevice(int nDeviceId)
Definition: hid.cpp:709

◆ HIDDeviceInputReport()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceInputReport ( JNIEnv *  env,
jobject  thiz,
int  nDeviceID,
jbyteArray  value 
)

Definition at line 918 of file hid.cpp.

References FindDevice(), NULL, and CHIDDevice::ProcessInput().

Referenced by ThreadDestroyed().

919 {
920  jbyte *pBuf = env->GetByteArrayElements(value, NULL);
921  jsize nBufSize = env->GetArrayLength(value);
922 
923 // LOGV( "HIDDeviceInput() id=%d len=%u\n", nDeviceID, nBufSize );
924  hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
925  if ( pDevice )
926  {
927  pDevice->ProcessInput( reinterpret_cast< const uint8_t* >( pBuf ), nBufSize );
928  }
929 
930  env->ReleaseByteArrayElements(value, pBuf, 0);
931 }
GLsizei const GLfloat * value
#define NULL
Definition: begin_code.h:164
void ProcessInput(const uint8_t *pBuf, size_t nBufSize)
Definition: hid.cpp:497
static hid_device_ref< CHIDDevice > FindDevice(int nDeviceId)
Definition: hid.cpp:709

◆ HIDDeviceOpenPending()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenPending ( JNIEnv *  env,
jobject  thiz,
int  nDeviceID 
)

Definition at line 865 of file hid.cpp.

References FindDevice(), LOGV, and CHIDDevice::SetOpenPending().

Referenced by ThreadDestroyed().

866 {
867  LOGV( "HIDDeviceOpenPending() id=%d\n", nDeviceID );
868  hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
869  if ( pDevice )
870  {
871  pDevice->SetOpenPending();
872  }
873 }
void SetOpenPending()
Definition: hid.cpp:482
#define LOGV(...)
Definition: hid.cpp:25
static hid_device_ref< CHIDDevice > FindDevice(int nDeviceId)
Definition: hid.cpp:709

◆ HIDDeviceOpenResult()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenResult ( JNIEnv *  env,
jobject  thiz,
int  nDeviceID,
bool  bOpened 
)

Definition at line 876 of file hid.cpp.

References FindDevice(), LOGV, and CHIDDevice::SetOpenResult().

Referenced by ThreadDestroyed().

877 {
878  LOGV( "HIDDeviceOpenResult() id=%d, result=%s\n", nDeviceID, bOpened ? "true" : "false" );
879  hid_device_ref<CHIDDevice> pDevice = FindDevice( nDeviceID );
880  if ( pDevice )
881  {
882  pDevice->SetOpenResult( bOpened );
883  }
884 }
void SetOpenResult(bool bResult)
Definition: hid.cpp:487
#define LOGV(...)
Definition: hid.cpp:25
static hid_device_ref< CHIDDevice > FindDevice(int nDeviceId)
Definition: hid.cpp:709

◆ HIDDeviceRegisterCallback()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceRegisterCallback ( JNIEnv *  env,
jobject  thiz 
)

Definition at line 761 of file hid.cpp.

References g_HIDDeviceManagerCallbackClass, g_HIDDeviceManagerCallbackHandler, g_JVM, g_midHIDDeviceManagerClose, g_midHIDDeviceManagerGetFeatureReport, g_midHIDDeviceManagerOpen, g_midHIDDeviceManagerSendFeatureReport, g_midHIDDeviceManagerSendOutputReport, g_ThreadKey, LOGV, NULL, TAG, and ThreadDestroyed().

Referenced by ThreadDestroyed().

762 {
763  LOGV( "HIDDeviceRegisterCallback()");
764 
765  env->GetJavaVM( &g_JVM );
766 
767  /*
768  * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
769  * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
770  */
771  if (pthread_key_create(&g_ThreadKey, ThreadDestroyed) != 0) {
772  __android_log_print(ANDROID_LOG_ERROR, TAG, "Error initializing pthread key");
773  }
774 
776  {
777  env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
779  env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
781  }
782 
783  g_HIDDeviceManagerCallbackHandler = env->NewGlobalRef( thiz );
784  jclass objClass = env->GetObjectClass( thiz );
785  if ( objClass )
786  {
787  g_HIDDeviceManagerCallbackClass = reinterpret_cast< jclass >( env->NewGlobalRef( objClass ) );
788  g_midHIDDeviceManagerOpen = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "openDevice", "(I)Z" );
790  {
791  __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing openDevice" );
792  }
793  g_midHIDDeviceManagerSendOutputReport = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "sendOutputReport", "(I[B)I" );
795  {
796  __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing sendOutputReport" );
797  }
798  g_midHIDDeviceManagerSendFeatureReport = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "sendFeatureReport", "(I[B)I" );
800  {
801  __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing sendFeatureReport" );
802  }
803  g_midHIDDeviceManagerGetFeatureReport = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "getFeatureReport", "(I[B)Z" );
805  {
806  __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing getFeatureReport" );
807  }
808  g_midHIDDeviceManagerClose = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "closeDevice", "(I)V" );
810  {
811  __android_log_print(ANDROID_LOG_ERROR, TAG, "HIDDeviceRegisterCallback: callback class missing closeDevice" );
812  }
813  env->DeleteLocalRef( objClass );
814  }
815 }
static jmethodID g_midHIDDeviceManagerOpen
Definition: hid.cpp:337
static JavaVM * g_JVM
Definition: hid.cpp:46
static jmethodID g_midHIDDeviceManagerGetFeatureReport
Definition: hid.cpp:340
static jobject g_HIDDeviceManagerCallbackHandler
Definition: hid.cpp:336
static jmethodID g_midHIDDeviceManagerSendOutputReport
Definition: hid.cpp:338
static jmethodID g_midHIDDeviceManagerClose
Definition: hid.cpp:341
static jmethodID g_midHIDDeviceManagerSendFeatureReport
Definition: hid.cpp:339
static void ThreadDestroyed(void *value)
Definition: hid.cpp:724
#define NULL
Definition: begin_code.h:164
static pthread_key_t g_ThreadKey
Definition: hid.cpp:47
#define LOGV(...)
Definition: hid.cpp:25
#define TAG
Definition: hid.cpp:16
static jclass g_HIDDeviceManagerCallbackClass
Definition: hid.cpp:335

◆ HIDDeviceReleaseCallback()

JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceReleaseCallback ( JNIEnv *  env,
jobject  thiz 
)

Definition at line 818 of file hid.cpp.

References g_HIDDeviceManagerCallbackClass, g_HIDDeviceManagerCallbackHandler, LOGV, and NULL.

Referenced by ThreadDestroyed().

819 {
820  LOGV("HIDDeviceReleaseCallback");
821  if ( env->IsSameObject( thiz, g_HIDDeviceManagerCallbackHandler ) )
822  {
823  env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
825  env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
827  }
828 }
static jobject g_HIDDeviceManagerCallbackHandler
Definition: hid.cpp:336
#define NULL
Definition: begin_code.h:164
#define LOGV(...)
Definition: hid.cpp:25
static jclass g_HIDDeviceManagerCallbackClass
Definition: hid.cpp:335

◆ NewByteArray()

static jbyteArray NewByteArray ( JNIEnv *  env,
const uint8_t pData,
size_t  nDataLen 
)
static

Definition at line 270 of file hid.cpp.

References memcpy, and NULL.

Referenced by CHIDDevice::GetFeatureReport(), CHIDDevice::SendFeatureReport(), and CHIDDevice::SendOutputReport().

271 {
272  jbyteArray array = env->NewByteArray( nDataLen );
273  jbyte *pBuf = env->GetByteArrayElements( array, NULL );
274  memcpy( pBuf, pData, nDataLen );
275  env->ReleaseByteArrayElements( array, pBuf, 0 );
276 
277  return array;
278 }
#define NULL
Definition: begin_code.h:164
#define memcpy
Definition: SDL_malloc.c:622
GLenum array

◆ ThreadDestroyed()

static void ThreadDestroyed ( void value)
static

Definition at line 724 of file hid.cpp.

References g_JVM, g_ThreadKey, HID_DEVICE_MANAGER_JAVA_INTERFACE, HIDDeviceConnected(), HIDDeviceDisconnected(), HIDDeviceFeatureReport(), HIDDeviceInputReport(), HIDDeviceOpenPending(), HIDDeviceOpenResult(), HIDDeviceRegisterCallback(), HIDDeviceReleaseCallback(), and NULL.

Referenced by HIDDeviceRegisterCallback().

725 {
726  /* The thread is being destroyed, detach it from the Java VM and set the g_ThreadKey value to NULL as required */
727  JNIEnv *env = (JNIEnv*) value;
728  if (env != NULL) {
729  g_JVM->DetachCurrentThread();
730  pthread_setspecific(g_ThreadKey, NULL);
731  }
732 }
static JavaVM * g_JVM
Definition: hid.cpp:46
GLsizei const GLfloat * value
#define NULL
Definition: begin_code.h:164
static pthread_key_t g_ThreadKey
Definition: hid.cpp:47

Variable Documentation

◆ g_Devices

hid_device_ref<CHIDDevice> g_Devices
static

Definition at line 707 of file hid.cpp.

◆ g_DevicesMutex

pthread_mutex_t g_DevicesMutex = PTHREAD_MUTEX_INITIALIZER
static

◆ g_DevicesRefCountMutex

pthread_mutex_t g_DevicesRefCountMutex = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 706 of file hid.cpp.

Referenced by hid_close(), and hid_open_path().

◆ g_HIDDeviceManagerCallbackClass

jclass g_HIDDeviceManagerCallbackClass
static

Definition at line 335 of file hid.cpp.

Referenced by HIDDeviceRegisterCallback(), and HIDDeviceReleaseCallback().

◆ g_HIDDeviceManagerCallbackHandler

◆ g_JVM

◆ g_midHIDDeviceManagerClose

jmethodID g_midHIDDeviceManagerClose
static

Definition at line 341 of file hid.cpp.

Referenced by CHIDDevice::Close(), and HIDDeviceRegisterCallback().

◆ g_midHIDDeviceManagerGetFeatureReport

jmethodID g_midHIDDeviceManagerGetFeatureReport
static

Definition at line 340 of file hid.cpp.

Referenced by CHIDDevice::GetFeatureReport(), and HIDDeviceRegisterCallback().

◆ g_midHIDDeviceManagerOpen

jmethodID g_midHIDDeviceManagerOpen
static

Definition at line 337 of file hid.cpp.

Referenced by CHIDDevice::BOpen(), and HIDDeviceRegisterCallback().

◆ g_midHIDDeviceManagerSendFeatureReport

jmethodID g_midHIDDeviceManagerSendFeatureReport
static

Definition at line 339 of file hid.cpp.

Referenced by HIDDeviceRegisterCallback(), and CHIDDevice::SendFeatureReport().

◆ g_midHIDDeviceManagerSendOutputReport

jmethodID g_midHIDDeviceManagerSendOutputReport
static

Definition at line 338 of file hid.cpp.

Referenced by HIDDeviceRegisterCallback(), and CHIDDevice::SendOutputReport().

◆ g_ThreadKey