From f9041771d50e7ae5797a8bcdcf2c0fbaa7756910 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 1 Apr 2015 12:14:56 -0400 Subject: [PATCH] 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! --- .../src/org/libsdl/app/SDLActivity.java | 19 +++++++++++++++---- src/video/android/SDL_androidtouch.c | 3 +++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java index 70d1acc664a4f..2a228d63e3079 100644 --- a/android-project/src/org/libsdl/app/SDLActivity.java +++ b/android-project/src/org/libsdl/app/SDLActivity.java @@ -41,6 +41,10 @@ public class SDLActivity extends Activity { /** If shared libraries (e.g. SDL or the native application) could not be loaded. */ public static boolean mBrokenLibraries; + // If we want to separate mouse and touch events. + // This is only toggled in native code when a hint is set! + public static boolean mSeparateMouseAndTouch; + // Main components protected static SDLActivity mSingleton; protected static SDLSurface mSurface; @@ -1130,11 +1134,18 @@ public boolean onTouch(View v, MotionEvent event) { int i = -1; float x,y,p; - if (event.getSource() == InputDevice.SOURCE_MOUSE && - SDLActivity.nativeGetHint("SDL_ANDROID_SEPARATE_MOUSE_AND_TOUCH").equals("1")) { + // !!! FIXME: dump this SDK check after 2.0.4 ships and require API14. + if (event.getSource() == InputDevice.SOURCE_MOUSE && SDLActivity.mSeparateMouseAndTouch) { + if (Build.VERSION.SDK_INT < 14) { mouseButton = 1; // For Android==12 all mouse buttons are the left button - - SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0)); + } else { + try { + mouseButton = event.getClass().getMethod("getButtonState").invoke(event); + } catch(Exception e) { + mouseButton = 1; // oh well. + } + } + SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0)); } else { switch(action) { case MotionEvent.ACTION_MOVE: diff --git a/src/video/android/SDL_androidtouch.c b/src/video/android/SDL_androidtouch.c index 27b78598f2cd7..4a5612acdd155 100644 --- a/src/video/android/SDL_androidtouch.c +++ b/src/video/android/SDL_androidtouch.c @@ -57,6 +57,9 @@ SeparateEventsHintWatcher(void *userdata, const char *name, const char *oldValue, const char *newValue) { separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0)); + JNIEnv *env = Android_JNI_GetEnv(); + jfieldID fid = (*env)->GetStaticFieldID(env, mActivityClass, "mSeparateMouseAndTouch", "Z"); + (*env)->SetStaticBooleanField(env, mActivityClass, fid, separate_mouse_and_touch ? JNI_TRUE : JNI_FALSE); } void Android_InitTouch(void)