mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-05-31 08:57:40 +00:00
Use SDL_GetAndroidJNIEnv() in hid.cpp
This commit is contained in:
parent
2d3fa03d53
commit
60c3eaf5ee
1 changed files with 6 additions and 47 deletions
|
@ -98,9 +98,6 @@ struct hid_device_
|
||||||
int m_nDeviceRefCount;
|
int m_nDeviceRefCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
static JavaVM *g_JVM;
|
|
||||||
static pthread_key_t g_ThreadKey;
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class hid_device_ref
|
class hid_device_ref
|
||||||
{
|
{
|
||||||
|
@ -495,10 +492,7 @@ public:
|
||||||
|
|
||||||
bool BOpen()
|
bool BOpen()
|
||||||
{
|
{
|
||||||
// Make sure thread is attached to JVM/env
|
JNIEnv *env = SDL_GetAndroidJNIEnv();
|
||||||
JNIEnv *env;
|
|
||||||
g_JVM->AttachCurrentThread( &env, NULL );
|
|
||||||
pthread_setspecific( g_ThreadKey, (void*)env );
|
|
||||||
|
|
||||||
if ( !g_HIDDeviceManagerCallbackHandler )
|
if ( !g_HIDDeviceManagerCallbackHandler )
|
||||||
{
|
{
|
||||||
|
@ -610,10 +604,7 @@ public:
|
||||||
|
|
||||||
int WriteReport( const unsigned char *pData, size_t nDataLen, bool bFeature )
|
int WriteReport( const unsigned char *pData, size_t nDataLen, bool bFeature )
|
||||||
{
|
{
|
||||||
// Make sure thread is attached to JVM/env
|
JNIEnv *env = SDL_GetAndroidJNIEnv();
|
||||||
JNIEnv *env;
|
|
||||||
g_JVM->AttachCurrentThread( &env, NULL );
|
|
||||||
pthread_setspecific( g_ThreadKey, (void*)env );
|
|
||||||
|
|
||||||
int nRet = -1;
|
int nRet = -1;
|
||||||
if ( g_HIDDeviceManagerCallbackHandler )
|
if ( g_HIDDeviceManagerCallbackHandler )
|
||||||
|
@ -645,10 +636,7 @@ public:
|
||||||
|
|
||||||
int ReadReport( unsigned char *pData, size_t nDataLen, bool bFeature )
|
int ReadReport( unsigned char *pData, size_t nDataLen, bool bFeature )
|
||||||
{
|
{
|
||||||
// Make sure thread is attached to JVM/env
|
JNIEnv *env = SDL_GetAndroidJNIEnv();
|
||||||
JNIEnv *env;
|
|
||||||
g_JVM->AttachCurrentThread( &env, NULL );
|
|
||||||
pthread_setspecific( g_ThreadKey, (void*)env );
|
|
||||||
|
|
||||||
if ( !g_HIDDeviceManagerCallbackHandler )
|
if ( !g_HIDDeviceManagerCallbackHandler )
|
||||||
{
|
{
|
||||||
|
@ -721,10 +709,7 @@ public:
|
||||||
|
|
||||||
void Close( bool bDeleteDevice )
|
void Close( bool bDeleteDevice )
|
||||||
{
|
{
|
||||||
// Make sure thread is attached to JVM/env
|
JNIEnv *env = SDL_GetAndroidJNIEnv();
|
||||||
JNIEnv *env;
|
|
||||||
g_JVM->AttachCurrentThread( &env, NULL );
|
|
||||||
pthread_setspecific( g_ThreadKey, (void*)env );
|
|
||||||
|
|
||||||
if ( g_HIDDeviceManagerCallbackHandler )
|
if ( g_HIDDeviceManagerCallbackHandler )
|
||||||
{
|
{
|
||||||
|
@ -793,16 +778,6 @@ static hid_device_ref<CHIDDevice> FindDevice( int nDeviceId )
|
||||||
return pDevice;
|
return pDevice;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ThreadDestroyed(void* value)
|
|
||||||
{
|
|
||||||
/* The thread is being destroyed, detach it from the Java VM and set the g_ThreadKey value to NULL as required */
|
|
||||||
JNIEnv *env = (JNIEnv*) value;
|
|
||||||
if (env != NULL) {
|
|
||||||
g_JVM->DetachCurrentThread();
|
|
||||||
pthread_setspecific(g_ThreadKey, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz);
|
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz);
|
||||||
|
@ -834,16 +809,6 @@ JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallba
|
||||||
{
|
{
|
||||||
LOGV( "HIDDeviceRegisterCallback()");
|
LOGV( "HIDDeviceRegisterCallback()");
|
||||||
|
|
||||||
env->GetJavaVM( &g_JVM );
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
|
|
||||||
* Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
|
|
||||||
*/
|
|
||||||
if (pthread_key_create(&g_ThreadKey, ThreadDestroyed) != 0) {
|
|
||||||
__android_log_print(ANDROID_LOG_ERROR, TAG, "Error initializing pthread key");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( g_HIDDeviceManagerCallbackHandler != NULL )
|
if ( g_HIDDeviceManagerCallbackHandler != NULL )
|
||||||
{
|
{
|
||||||
env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
|
env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
|
||||||
|
@ -1035,10 +1000,7 @@ static void SDLCALL RequestBluetoothPermissionCallback( void *userdata, const ch
|
||||||
|
|
||||||
if ( granted && g_HIDDeviceManagerCallbackHandler )
|
if ( granted && g_HIDDeviceManagerCallbackHandler )
|
||||||
{
|
{
|
||||||
// Make sure thread is attached to JVM/env
|
JNIEnv *env = SDL_GetAndroidJNIEnv();
|
||||||
JNIEnv *env;
|
|
||||||
g_JVM->AttachCurrentThread( &env, NULL );
|
|
||||||
pthread_setspecific( g_ThreadKey, (void*)env );
|
|
||||||
|
|
||||||
env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, false, true );
|
env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, false, true );
|
||||||
}
|
}
|
||||||
|
@ -1051,10 +1013,7 @@ int hid_init(void)
|
||||||
// HIDAPI doesn't work well with Android < 4.3
|
// HIDAPI doesn't work well with Android < 4.3
|
||||||
if ( SDL_GetAndroidSDKVersion() >= 18 )
|
if ( SDL_GetAndroidSDKVersion() >= 18 )
|
||||||
{
|
{
|
||||||
// Make sure thread is attached to JVM/env
|
JNIEnv *env = SDL_GetAndroidJNIEnv();
|
||||||
JNIEnv *env;
|
|
||||||
g_JVM->AttachCurrentThread( &env, NULL );
|
|
||||||
pthread_setspecific( g_ThreadKey, (void*)env );
|
|
||||||
|
|
||||||
env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, true, false );
|
env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerInitialize, true, false );
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue