Android: add name for Touch devices and simplification, from bug 3958
authorSylvain Becker <sylvain.becker@gmail.com>
Thu, 10 Jan 2019 21:40:57 +0100
changeset 1252224bbe1496ef7
parent 12521 2a567875fe8e
child 12523 a50245e1a2c9
Android: add name for Touch devices and simplification, from bug 3958
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/video/android/SDL_androidtouch.c
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jan 10 18:05:56 2019 +0100
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jan 10 21:40:57 2019 +0100
     1.3 @@ -741,6 +741,7 @@
     1.4      public static native String nativeGetHint(String name);
     1.5      public static native void nativeSetenv(String name, String value);
     1.6      public static native void onNativeOrientationChanged(int orientation);
     1.7 +    public static native void nativeAddTouch(int touchId, String name);
     1.8  
     1.9      /**
    1.10       * This method is called by SDL using JNI.
    1.11 @@ -1062,16 +1063,14 @@
    1.12  
    1.13      /**
    1.14       * This method is called by SDL using JNI.
    1.15 -     * @return an array which may be empty but is never null.
    1.16       */
    1.17 -    public static int[] inputGetInputDeviceIds(int sources) {
    1.18 +    public static void initTouch() {
    1.19          int[] ids = InputDevice.getDeviceIds();
    1.20 -        int[] filtered = new int[ids.length];
    1.21 -        int used = 0;
    1.22 +
    1.23          for (int i = 0; i < ids.length; ++i) {
    1.24              InputDevice device = InputDevice.getDevice(ids[i]);
    1.25 -            if ((device != null) && ((device.getSources() & sources) != 0)) {
    1.26 -                filtered[used++] = device.getId();
    1.27 +            if (device != null && (device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) != 0) {
    1.28 +                nativeAddTouch(device.getId(), device.getName());
    1.29              }
    1.30          }
    1.31          return Arrays.copyOf(filtered, used);
     2.1 --- a/src/core/android/SDL_android.c	Thu Jan 10 18:05:56 2019 +0100
     2.2 +++ b/src/core/android/SDL_android.c	Thu Jan 10 21:40:57 2019 +0100
     2.3 @@ -149,6 +149,10 @@
     2.4          JNIEnv *env, jclass cls,
     2.5          jint orientation);
     2.6  
     2.7 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
     2.8 +        JNIEnv* env, jclass cls,
     2.9 +        jint touchId, jstring name);
    2.10 +
    2.11  /* Java class SDLInputConnection */
    2.12  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
    2.13          JNIEnv *env, jclass cls,
    2.14 @@ -238,7 +242,7 @@
    2.15  static jmethodID midIsChromebook;
    2.16  static jmethodID midIsDeXMode;
    2.17  static jmethodID midManualBackButton;
    2.18 -static jmethodID midInputGetInputDeviceIds;
    2.19 +static jmethodID midInitTouch;
    2.20  static jmethodID midSendMessage;
    2.21  static jmethodID midShowTextInput;
    2.22  static jmethodID midIsScreenKeyboardShown;
    2.23 @@ -366,8 +370,8 @@
    2.24                                  "isDeXMode", "()Z");
    2.25      midManualBackButton = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.26                                  "manualBackButton", "()V");
    2.27 -    midInputGetInputDeviceIds = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.28 -                                "inputGetInputDeviceIds", "(I)[I");
    2.29 +    midInitTouch = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.30 +                                "initTouch", "()V");
    2.31      midSendMessage = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.32                                  "sendMessage", "(II)Z");
    2.33      midShowTextInput =  (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.34 @@ -396,7 +400,7 @@
    2.35  
    2.36  
    2.37      if (!midGetNativeSurface || !midSetSurfaceViewFormat ||
    2.38 -       !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInputGetInputDeviceIds ||
    2.39 +       !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInitTouch ||
    2.40         !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
    2.41         !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
    2.42         !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
    2.43 @@ -596,6 +600,17 @@
    2.44      }
    2.45  }
    2.46  
    2.47 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeAddTouch)(
    2.48 +        JNIEnv* env, jclass cls,
    2.49 +        jint touchId, jstring name)
    2.50 +{
    2.51 +    const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
    2.52 +
    2.53 +    SDL_AddTouch((SDL_TouchID) touchId, SDL_TOUCH_DEVICE_DIRECT, utfname);
    2.54 +
    2.55 +    (*env)->ReleaseStringUTFChars(env, name, utfname);
    2.56 +}
    2.57 +
    2.58  /* Paddown */
    2.59  JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)(
    2.60                                      JNIEnv *env, jclass jcls,
    2.61 @@ -2074,29 +2089,10 @@
    2.62      return 0;
    2.63  }
    2.64  
    2.65 -/* returns number of found touch devices as return value and ids in parameter ids */
    2.66 -int Android_JNI_GetTouchDeviceIds(int **ids) {
    2.67 -    JNIEnv *env = Android_JNI_GetEnv();
    2.68 -    jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */
    2.69 -    jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, midInputGetInputDeviceIds, sources);
    2.70 -    int number = 0;
    2.71 -    *ids = NULL;
    2.72 -    if (array) {
    2.73 -        number = (int) (*env)->GetArrayLength(env, array);
    2.74 -        if (0 < number) {
    2.75 -            jint *elements = (*env)->GetIntArrayElements(env, array, NULL);
    2.76 -            if (elements) {
    2.77 -                int i;
    2.78 -                *ids = SDL_malloc(number * sizeof (**ids));
    2.79 -                for (i = 0; i < number; ++i) { /* not assuming sizeof (jint) == sizeof (int) */
    2.80 -                    (*ids)[i] = elements[i];
    2.81 -                }
    2.82 -                (*env)->ReleaseIntArrayElements(env, array, elements, JNI_ABORT);
    2.83 -            }
    2.84 -        }
    2.85 -        (*env)->DeleteLocalRef(env, array);
    2.86 -    }
    2.87 -    return number;
    2.88 +/* Add all touch devices */
    2.89 +int Android_JNI_InitTouch() {
    2.90 +     JNIEnv *env = Android_JNI_GetEnv();
    2.91 +    (*env)->CallStaticVoidMethod(env, mActivityClass, midInitTouch);
    2.92  }
    2.93  
    2.94  /* sets the mSeparateMouseAndTouch field */
     3.1 --- a/src/core/android/SDL_android.h	Thu Jan 10 18:05:56 2019 +0100
     3.2 +++ b/src/core/android/SDL_android.h	Thu Jan 10 21:40:57 2019 +0100
     3.3 @@ -95,7 +95,6 @@
     3.4  /* Touch support */
     3.5  int Android_JNI_InitTouch(void);
     3.6  void Android_JNI_SetSeparateMouseAndTouch(SDL_bool new_value);
     3.7 -int Android_JNI_GetTouchDeviceIds(int **ids);
     3.8  
     3.9  /* Threads */
    3.10  #include <jni.h>
     4.1 --- a/src/video/android/SDL_androidtouch.c	Thu Jan 10 18:05:56 2019 +0100
     4.2 +++ b/src/video/android/SDL_androidtouch.c	Thu Jan 10 21:40:57 2019 +0100
     4.3 @@ -63,19 +63,11 @@
     4.4  
     4.5  void Android_InitTouch(void)
     4.6  {
     4.7 -    int i;
     4.8 -    int *ids;
     4.9 -    const int number = Android_JNI_GetTouchDeviceIds(&ids);
    4.10 -
    4.11      SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
    4.12                          SeparateEventsHintWatcher, NULL);
    4.13  
    4.14 -    if (0 < number) {
    4.15 -        for (i = 0; i < number; ++i) {
    4.16 -            SDL_AddTouch((SDL_TouchID) ids[i], SDL_TOUCH_DEVICE_DIRECT, ""); /* no error handling */
    4.17 -        }
    4.18 -        SDL_free(ids);
    4.19 -    }
    4.20 +    /* Add all touch devices */
    4.21 +    Android_JNI_InitTouch();
    4.22  }
    4.23  
    4.24  void Android_QuitTouch(void)