Android: add name for Touch devices and simplification, from bug 3958
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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 d88cde9..8191d94 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 class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
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 class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
/**
* 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 0afaa76..ee823db 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 f8830cc..e615c5a 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 <jni.h>
diff --git a/src/video/android/SDL_androidtouch.c b/src/video/android/SDL_androidtouch.c
index 0fd487a..fde3375 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)