Fixed bug #13493: Assertion failure at SDL_AddTouch with Android API 28

Java touch id should be -1 because it's reserved for internal SDL
synthetic events.
It should also not be 0, because this is SDL invalid value.
This commit is contained in:
Sylvain
2025-08-08 11:43:42 +02:00
committed by Sylvain Becker
parent 6e422e5ff2
commit 970c0bfe96
3 changed files with 23 additions and 6 deletions

View File

@@ -1080,7 +1080,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
{
const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
SDL_AddTouch((SDL_TouchID)touchId, SDL_TOUCH_DEVICE_DIRECT, utfname);
SDL_AddTouch(Android_ConvertJavaTouchID(touchId),
SDL_TOUCH_DEVICE_DIRECT, utfname);
(*env)->ReleaseStringUTFChars(env, name, utfname);
}

View File

@@ -47,6 +47,25 @@ void Android_QuitTouch(void)
{
}
SDL_TouchID Android_ConvertJavaTouchID(int touchID)
{
SDL_TouchID retval = touchID;
if (touchID < 0) {
// Touch ID -1 appears when using Android emulator, eg:
// adb shell input mouse tap 100 100
// adb shell input touchscreen tap 100 100
//
// Prevent to be -1, since it's used in SDL internal for synthetic events:
retval -= 1;
} else {
// Touch ID 0 is invalid
retval += 1;
}
return retval;
}
void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
{
SDL_TouchID touchDeviceId = 0;
@@ -56,11 +75,7 @@ void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_fin
return;
}
/* Touch device -1 appears when using Android emulator, eg:
* adb shell input mouse tap 100 100
* adb shell input touchscreen tap 100 100
*/
touchDeviceId = (SDL_TouchID)(touch_device_id_in + 2);
touchDeviceId = Android_ConvertJavaTouchID(touch_device_id_in);
// Finger ID should be greater than 0
fingerId = (SDL_FingerID)(pointer_finger_id_in + 1);

View File

@@ -25,3 +25,4 @@
extern void Android_InitTouch(void);
extern void Android_QuitTouch(void);
extern void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p);
extern SDL_TouchID Android_ConvertJavaTouchID(int touchID);