Skip to content

Commit 2edb264

Browse files
committed
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.
1 parent 248bcf6 commit 2edb264

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/core/android/SDL_android.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
10801080
{
10811081
const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
10821082

1083-
SDL_AddTouch((SDL_TouchID)touchId, SDL_TOUCH_DEVICE_DIRECT, utfname);
1083+
SDL_AddTouch(Android_ConvertTouchdId_Java_to_SDL(touchId),
1084+
SDL_TOUCH_DEVICE_DIRECT, utfname);
10841085

10851086
(*env)->ReleaseStringUTFChars(env, name, utfname);
10861087
}

src/video/android/SDL_androidtouch.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ void Android_QuitTouch(void)
4747
{
4848
}
4949

50+
51+
SDL_TouchID Android_ConvertTouchdId_Java_to_SDL(int touch_device_id_in)
52+
{
53+
SDL_TouchID touchId = touch_device_id_in;
54+
55+
if (touch_device_id_in < 0) {
56+
// Touch ID -1 appears when using Android emulator, eg:
57+
// adb shell input mouse tap 100 100
58+
// adb shell input touchscreen tap 100 100
59+
//
60+
// Prevent to be -1, since it's used in SDL internal for synthetic events:
61+
touchId -= 1;
62+
} else {
63+
// Touch ID 0 is invalid
64+
touchId += 1;
65+
}
66+
return touchId;
67+
}
68+
5069
void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
5170
{
5271
SDL_TouchID touchDeviceId = 0;
@@ -56,11 +75,7 @@ void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_fin
5675
return;
5776
}
5877

59-
/* Touch device -1 appears when using Android emulator, eg:
60-
* adb shell input mouse tap 100 100
61-
* adb shell input touchscreen tap 100 100
62-
*/
63-
touchDeviceId = (SDL_TouchID)(touch_device_id_in + 2);
78+
touchDeviceId = Android_ConvertTouchdId_Java_to_SDL(touch_device_id_in);
6479

6580
// Finger ID should be greater than 0
6681
fingerId = (SDL_FingerID)(pointer_finger_id_in + 1);

src/video/android/SDL_androidtouch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
extern void Android_InitTouch(void);
2626
extern void Android_QuitTouch(void);
2727
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);
28+
extern SDL_TouchID Android_ConvertTouchdId_Java_to_SDL(int touch_device_id_in);

0 commit comments

Comments
 (0)