mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-15 22:35:59 +00:00
Added detection of touch devices before first touch events happen on Android.
On Android available touch devices are now added with video initialization (like the keyboard). This fixes SDL_GetNumTouchDevices() returning 0 before any touch events happened although there is a touch screen available. The adding of touch devices after a touch event was received is still active to allow connecting devices later (if this is possible) and to provide a fallback if the new init did not work somehow. For the implementation JNI was used and API level 9 is required. There seems to be nothing in the Android NDK's input header (input.h) to implement everything on C side without communication with Java side.
This commit is contained in:
@@ -1186,6 +1186,32 @@ int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seco
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* returns number of found touch devices as return value and ids in parameter ids */
|
||||
int Android_JNI_GetTouchDeviceIds(int **ids) {
|
||||
JNIEnv *env = Android_JNI_GetEnv();
|
||||
jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */
|
||||
jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "inputGetInputDeviceIds", "(I)[I");
|
||||
jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, mid, sources);
|
||||
int number = 0;
|
||||
*ids = NULL;
|
||||
if (array) {
|
||||
number = (int) (*env)->GetArrayLength(env, array);
|
||||
if (0 < number) {
|
||||
jint* elements = (*env)->GetIntArrayElements(env, array, NULL);
|
||||
if (elements) {
|
||||
int i;
|
||||
*ids = SDL_malloc(number * sizeof (*ids[0]));
|
||||
for (i = 0; i < number; ++i) { /* not assuming sizeof (jint) == sizeof (int) */
|
||||
*ids[i] = elements[i];
|
||||
}
|
||||
(*env)->ReleaseIntArrayElements(env, array, elements, JNI_ABORT);
|
||||
}
|
||||
}
|
||||
(*env)->DeleteLocalRef(env, array);
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
/* sends message to be handled on the UI event dispatch thread */
|
||||
int Android_JNI_SendMessage(int command, int param)
|
||||
{
|
||||
|
Reference in New Issue
Block a user