Android: more separate-mouse-and-touch work.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 01 Apr 2015 12:14:56 -0400
changeset 9449a65088766e35
parent 9448 3d3ed3a5ba8d
child 9450 bcb16ffce95b
Android: more separate-mouse-and-touch work.

This avoids a hint lookup for each mouse event we get by setting a static Java
variable from native code during our hint watcher callback.

Also attempts to do the right thing with mouse buttons if you happen to be
on an API14 (Ice Cream Sandwich, Android 4.0) or later device. We still
target API12 (Honeycomb MR1, Android 3.1) for SDL 2.0.4 though.

This isn't tested, so I'm pushing to see what the Android buildbot says. Stand
back, I'm a professional!
android-project/src/org/libsdl/app/SDLActivity.java
src/video/android/SDL_androidtouch.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Mar 30 11:31:53 2015 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Apr 01 12:14:56 2015 -0400
     1.3 @@ -41,6 +41,10 @@
     1.4      /** If shared libraries (e.g. SDL or the native application) could not be loaded. */
     1.5      public static boolean mBrokenLibraries;
     1.6  
     1.7 +    // If we want to separate mouse and touch events.
     1.8 +    //  This is only toggled in native code when a hint is set!
     1.9 +    public static boolean mSeparateMouseAndTouch;
    1.10 +
    1.11      // Main components
    1.12      protected static SDLActivity mSingleton;
    1.13      protected static SDLSurface mSurface;
    1.14 @@ -1130,11 +1134,18 @@
    1.15          int i = -1;
    1.16          float x,y,p;
    1.17  
    1.18 -        if (event.getSource() == InputDevice.SOURCE_MOUSE &&
    1.19 -            SDLActivity.nativeGetHint("SDL_ANDROID_SEPARATE_MOUSE_AND_TOUCH").equals("1")) {
    1.20 +        // !!! FIXME: dump this SDK check after 2.0.4 ships and require API14.
    1.21 +        if (event.getSource() == InputDevice.SOURCE_MOUSE && SDLActivity.mSeparateMouseAndTouch) {
    1.22 +            if (Build.VERSION.SDK_INT < 14) {
    1.23                  mouseButton = 1;    // For Android==12 all mouse buttons are the left button
    1.24 - 
    1.25 -                SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0));
    1.26 +            } else {
    1.27 +                try {
    1.28 +                    mouseButton = event.getClass().getMethod("getButtonState").invoke(event);
    1.29 +                } catch(Exception e) {
    1.30 +                    mouseButton = 1;    // oh well.
    1.31 +                }
    1.32 +            }
    1.33 +            SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0));
    1.34          } else {
    1.35              switch(action) {
    1.36                  case MotionEvent.ACTION_MOVE:
     2.1 --- a/src/video/android/SDL_androidtouch.c	Mon Mar 30 11:31:53 2015 -0700
     2.2 +++ b/src/video/android/SDL_androidtouch.c	Wed Apr 01 12:14:56 2015 -0400
     2.3 @@ -57,6 +57,9 @@
     2.4                            const char *oldValue, const char *newValue)
     2.5  {
     2.6      separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0));
     2.7 +    JNIEnv *env = Android_JNI_GetEnv();
     2.8 +    jfieldID fid = (*env)->GetStaticFieldID(env, mActivityClass, "mSeparateMouseAndTouch", "Z");
     2.9 +    (*env)->SetStaticBooleanField(env, mActivityClass, fid, separate_mouse_and_touch ? JNI_TRUE : JNI_FALSE);
    2.10  }
    2.11  
    2.12  void Android_InitTouch(void)