Add Android support for relative mouse mode to SDL.
authorSam Lantinga <slouken@libsdl.org>
Tue, 05 Jun 2018 12:46:11 -0700
changeset 12007ecfb6a793c30
parent 12006 1b7ed3438a7f
child 12008 91f9b8f22b17
Add Android support for relative mouse mode to SDL.
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/events/SDL_mouse.c
src/video/android/SDL_androidmouse.c
src/video/android/SDL_androidmouse.h
     1.1 --- a/src/core/android/SDL_android.c	Tue Jun 05 12:46:09 2018 -0700
     1.2 +++ b/src/core/android/SDL_android.c	Tue Jun 05 12:46:11 2018 -0700
     1.3 @@ -102,7 +102,7 @@
     1.4  
     1.5  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
     1.6          JNIEnv* env, jclass jcls,
     1.7 -        jint button, jint action, jfloat x, jfloat y);
     1.8 +        jint button, jint action, jfloat x, jfloat y, jboolean relative);
     1.9  
    1.10  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeAccel)(
    1.11          JNIEnv* env, jclass jcls,
    1.12 @@ -226,6 +226,8 @@
    1.13  static jmethodID midCreateCustomCursor;
    1.14  static jmethodID midSetCustomCursor;
    1.15  static jmethodID midSetSystemCursor;
    1.16 +static jmethodID midSupportsRelativeMouse;
    1.17 +static jmethodID midSetRelativeMouseEnabled;
    1.18  
    1.19  /* audio manager */
    1.20  static jclass mAudioManagerClass;
    1.21 @@ -339,12 +341,15 @@
    1.22      midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(I)Z");
    1.23      midSetSystemCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setSystemCursor", "(I)Z");
    1.24  
    1.25 +    midSupportsRelativeMouse = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "supportsRelativeMouse", "()Z");
    1.26 +    midSetRelativeMouseEnabled = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");
    1.27 +
    1.28      if (!midGetNativeSurface ||
    1.29         !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
    1.30         !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
    1.31         !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
    1.32         !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
    1.33 -       !midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor) {
    1.34 +       !midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor || !midSupportsRelativeMouse || !midSetRelativeMouseEnabled) {
    1.35          __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
    1.36      }
    1.37  
    1.38 @@ -682,9 +687,9 @@
    1.39  /* Mouse */
    1.40  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
    1.41                                      JNIEnv* env, jclass jcls,
    1.42 -                                    jint button, jint action, jfloat x, jfloat y)
    1.43 +                                    jint button, jint action, jfloat x, jfloat y, jboolean relative)
    1.44  {
    1.45 -    Android_OnMouse(button, action, x, y);
    1.46 +    Android_OnMouse(button, action, x, y, relative);
    1.47  }
    1.48  
    1.49  /* Accelerometer */
    1.50 @@ -2202,6 +2207,19 @@
    1.51      return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetSystemCursor, cursorID);
    1.52  }
    1.53  
    1.54 +SDL_bool Android_JNI_SupportsRelativeMouse()
    1.55 +{
    1.56 +    JNIEnv *mEnv = Android_JNI_GetEnv();
    1.57 +    return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSupportsRelativeMouse);
    1.58 +}
    1.59 +
    1.60 +SDL_bool Android_JNI_SetRelativeMouseEnabled(SDL_bool enabled)
    1.61 +{
    1.62 +    JNIEnv *mEnv = Android_JNI_GetEnv();
    1.63 +    return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetRelativeMouseEnabled, (enabled == 1));
    1.64 +}
    1.65 +
    1.66 +
    1.67  #endif /* __ANDROID__ */
    1.68  
    1.69  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/core/android/SDL_android.h	Tue Jun 05 12:46:09 2018 -0700
     2.2 +++ b/src/core/android/SDL_android.h	Tue Jun 05 12:46:11 2018 -0700
     2.3 @@ -107,6 +107,10 @@
     2.4  SDL_bool Android_JNI_SetCustomCursor(int cursorID);
     2.5  SDL_bool Android_JNI_SetSystemCursor(int cursorID);
     2.6  
     2.7 +/* Relative mouse support */
     2.8 +SDL_bool Android_JNI_SupportsRelativeMouse();
     2.9 +SDL_bool Android_JNI_SetRelativeMouseEnabled(SDL_bool enabled);
    2.10 +
    2.11  /* Ends C function definitions when using C++ */
    2.12  #ifdef __cplusplus
    2.13  /* *INDENT-OFF* */
     3.1 --- a/src/events/SDL_mouse.c	Tue Jun 05 12:46:09 2018 -0700
     3.2 +++ b/src/events/SDL_mouse.c	Tue Jun 05 12:46:11 2018 -0700
     3.3 @@ -688,6 +688,7 @@
     3.4  ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
     3.5  {
     3.6      if (!mouse->SetRelativeMouseMode) {
     3.7 +        SDL_assert(mouse->WarpMouse);   /* Need this functionality for relative mode warp implementation */
     3.8          return SDL_TRUE;
     3.9      }
    3.10  
     4.1 --- a/src/video/android/SDL_androidmouse.c	Tue Jun 05 12:46:09 2018 -0700
     4.2 +++ b/src/video/android/SDL_androidmouse.c	Tue Jun 05 12:46:11 2018 -0700
     4.3 @@ -137,6 +137,20 @@
     4.4      return 0;
     4.5  }
     4.6  
     4.7 +static int
     4.8 +Android_SetRelativeMouseMode(SDL_bool enabled)
     4.9 +{
    4.10 +    if (!Android_JNI_SupportsRelativeMouse()) {
    4.11 +        return SDL_Unsupported();
    4.12 +    }
    4.13 +
    4.14 +    if (!Android_JNI_SetRelativeMouseEnabled(enabled)) {
    4.15 +        return SDL_Unsupported();
    4.16 +    }
    4.17 +
    4.18 +    return 0;
    4.19 +}
    4.20 +
    4.21  void
    4.22  Android_InitMouse(void)
    4.23  {
    4.24 @@ -146,6 +160,7 @@
    4.25      mouse->CreateSystemCursor = Android_CreateSystemCursor;
    4.26      mouse->ShowCursor = Android_ShowCursor;
    4.27      mouse->FreeCursor = Android_FreeCursor;
    4.28 +    mouse->SetRelativeMouseMode = Android_SetRelativeMouseMode;
    4.29  
    4.30      SDL_SetDefaultCursor(Android_CreateDefaultCursor());
    4.31  
    4.32 @@ -172,7 +187,7 @@
    4.33  }
    4.34  
    4.35  void
    4.36 -Android_OnMouse(int state, int action, float x, float y)
    4.37 +Android_OnMouse(int state, int action, float x, float y, SDL_bool relative)
    4.38  {
    4.39      int changes;
    4.40      Uint8 button;
    4.41 @@ -186,7 +201,7 @@
    4.42              changes = state & ~last_state;
    4.43              button = TranslateButton(changes);
    4.44              last_state = state;
    4.45 -            SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
    4.46 +            SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
    4.47              SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button);
    4.48              break;
    4.49  
    4.50 @@ -194,13 +209,13 @@
    4.51              changes = last_state & ~state;
    4.52              button = TranslateButton(changes);
    4.53              last_state = state;
    4.54 -            SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
    4.55 +            SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
    4.56              SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button);
    4.57              break;
    4.58  
    4.59          case ACTION_MOVE:
    4.60          case ACTION_HOVER_MOVE:
    4.61 -            SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
    4.62 +            SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
    4.63              break;
    4.64  
    4.65          case ACTION_SCROLL:
     5.1 --- a/src/video/android/SDL_androidmouse.h	Tue Jun 05 12:46:09 2018 -0700
     5.2 +++ b/src/video/android/SDL_androidmouse.h	Tue Jun 05 12:46:11 2018 -0700
     5.3 @@ -25,7 +25,7 @@
     5.4  #include "SDL_androidvideo.h"
     5.5  
     5.6  extern void Android_InitMouse(void);
     5.7 -extern void Android_OnMouse( int button, int action, float x, float y);
     5.8 +extern void Android_OnMouse(int button, int action, float x, float y, SDL_bool relative);
     5.9  
    5.10  #endif /* SDL_androidmouse_h_ */
    5.11