From 7a1d1baefc48a5a312a58ebfe4095f8703bca42d Mon Sep 17 00:00:00 2001 From: Sylvain Becker Date: Thu, 10 Jan 2019 21:40:57 +0100 Subject: [PATCH] Android: add name for Touch devices and simplification, from bug 3958 --- .../main/java/org/libsdl/app/SDLActivity.java | 11 ++-- src/core/android/SDL_android.c | 50 +++++++++---------- src/core/android/SDL_android.h | 1 - src/video/android/SDL_androidtouch.c | 12 +---- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java index d88cde9e7594e..8191d94bee0a9 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java @@ -741,6 +741,7 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId, public static native String nativeGetHint(String name); public static native void nativeSetenv(String name, String value); public static native void onNativeOrientationChanged(int orientation); + public static native void nativeAddTouch(int touchId, String name); /** * This method is called by SDL using JNI. @@ -1062,16 +1063,14 @@ public static void setSurfaceViewFormat(int format) { /** * This method is called by SDL using JNI. - * @return an array which may be empty but is never null. */ - public static int[] inputGetInputDeviceIds(int sources) { + public static void initTouch() { int[] ids = InputDevice.getDeviceIds(); - int[] filtered = new int[ids.length]; - int used = 0; + for (int i = 0; i < ids.length; ++i) { InputDevice device = InputDevice.getDevice(ids[i]); - if ((device != null) && ((device.getSources() & sources) != 0)) { - filtered[used++] = device.getId(); + if (device != null && (device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) != 0) { + nativeAddTouch(device.getId(), device.getName()); } } return Arrays.copyOf(filtered, used); diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c index 0afaa76a7bd25..ee823dba5514b 100644 --- a/src/core/android/SDL_android.c +++ b/src/core/android/SDL_android.c @@ -149,6 +149,10 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)( JNIEnv *env, jclass cls, jint orientation); +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)( + JNIEnv* env, jclass cls, + jint touchId, jstring name); + /* Java class SDLInputConnection */ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)( JNIEnv *env, jclass cls, @@ -238,7 +242,7 @@ static jmethodID midIsAndroidTV; static jmethodID midIsChromebook; static jmethodID midIsDeXMode; static jmethodID midManualBackButton; -static jmethodID midInputGetInputDeviceIds; +static jmethodID midInitTouch; static jmethodID midSendMessage; static jmethodID midShowTextInput; static jmethodID midIsScreenKeyboardShown; @@ -366,8 +370,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c "isDeXMode", "()Z"); midManualBackButton = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "manualBackButton", "()V"); - midInputGetInputDeviceIds = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, - "inputGetInputDeviceIds", "(I)[I"); + midInitTouch = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, + "initTouch", "()V"); midSendMessage = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "sendMessage", "(II)Z"); midShowTextInput = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, @@ -396,7 +400,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c if (!midGetNativeSurface || !midSetSurfaceViewFormat || - !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInputGetInputDeviceIds || + !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInitTouch || !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || !midClipboardSetText || !midClipboardGetText || !midClipboardHasText || !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI || @@ -596,6 +600,17 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)( } } +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)( + JNIEnv* env, jclass cls, + jint touchId, jstring name) +{ + const char *utfname = (*env)->GetStringUTFChars(env, name, NULL); + + SDL_AddTouch((SDL_TouchID) touchId, SDL_TOUCH_DEVICE_DIRECT, utfname); + + (*env)->ReleaseStringUTFChars(env, name, utfname); +} + /* Paddown */ JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)( JNIEnv *env, jclass jcls, @@ -2074,29 +2089,10 @@ 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 */ - jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, midInputGetInputDeviceIds, 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)); - 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; +/* Add all touch devices */ +int Android_JNI_InitTouch() { + JNIEnv *env = Android_JNI_GetEnv(); + (*env)->CallStaticVoidMethod(env, mActivityClass, midInitTouch); } /* sets the mSeparateMouseAndTouch field */ diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h index f8830cce8a14b..e615c5a04a0ac 100644 --- a/src/core/android/SDL_android.h +++ b/src/core/android/SDL_android.h @@ -95,7 +95,6 @@ void Android_JNI_SuspendScreenSaver(SDL_bool suspend); /* Touch support */ int Android_JNI_InitTouch(void); void Android_JNI_SetSeparateMouseAndTouch(SDL_bool new_value); -int Android_JNI_GetTouchDeviceIds(int **ids); /* Threads */ #include diff --git a/src/video/android/SDL_androidtouch.c b/src/video/android/SDL_androidtouch.c index 0fd487ab41457..fde33757bc490 100644 --- a/src/video/android/SDL_androidtouch.c +++ b/src/video/android/SDL_androidtouch.c @@ -63,19 +63,11 @@ SeparateEventsHintWatcher(void *userdata, const char *name, void Android_InitTouch(void) { - int i; - int *ids; - const int number = Android_JNI_GetTouchDeviceIds(&ids); - SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH, SeparateEventsHintWatcher, NULL); - if (0 < number) { - for (i = 0; i < number; ++i) { - SDL_AddTouch((SDL_TouchID) ids[i], SDL_TOUCH_DEVICE_DIRECT, ""); /* no error handling */ - } - SDL_free(ids); - } + /* Add all touch devices */ + Android_JNI_InitTouch(); } void Android_QuitTouch(void)