Add Android support for relative mouse mode to SDL.

Sam Lantinga 2018-06-05 12:46:11 -07:00
parent 9d6ac3deff
commit 2dedbc7262
5 changed files with 47 additions and 9 deletions

View File

@ -102,7 +102,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
JNIEnv* env, jclass jcls, JNIEnv* env, jclass jcls,
jint button, jint action, jfloat x, jfloat y); jint button, jint action, jfloat x, jfloat y, jboolean relative);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeAccel)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeAccel)(
JNIEnv* env, jclass jcls, JNIEnv* env, jclass jcls,
@ -226,6 +226,8 @@ static jmethodID midGetDisplayDPI;
static jmethodID midCreateCustomCursor; static jmethodID midCreateCustomCursor;
static jmethodID midSetCustomCursor; static jmethodID midSetCustomCursor;
static jmethodID midSetSystemCursor; static jmethodID midSetSystemCursor;
static jmethodID midSupportsRelativeMouse;
static jmethodID midSetRelativeMouseEnabled;
/* audio manager */ /* audio manager */
static jclass mAudioManagerClass; static jclass mAudioManagerClass;
@ -339,12 +341,15 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(I)Z"); midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(I)Z");
midSetSystemCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setSystemCursor", "(I)Z"); midSetSystemCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setSystemCursor", "(I)Z");
midSupportsRelativeMouse = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "supportsRelativeMouse", "()Z");
midSetRelativeMouseEnabled = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");
if (!midGetNativeSurface || if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds || !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText || !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI || !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
!midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor) { !midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor || !midSupportsRelativeMouse || !midSetRelativeMouseEnabled) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?"); __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
} }
@ -682,9 +687,9 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
/* Mouse */ /* Mouse */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
JNIEnv* env, jclass jcls, JNIEnv* env, jclass jcls,
jint button, jint action, jfloat x, jfloat y) jint button, jint action, jfloat x, jfloat y, jboolean relative)
{ {
Android_OnMouse(button, action, x, y); Android_OnMouse(button, action, x, y, relative);
} }
/* Accelerometer */ /* Accelerometer */
@ -2202,6 +2207,19 @@ SDL_bool Android_JNI_SetSystemCursor(int cursorID)
return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetSystemCursor, cursorID); return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetSystemCursor, cursorID);
} }
SDL_bool Android_JNI_SupportsRelativeMouse()
{
JNIEnv *mEnv = Android_JNI_GetEnv();
return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSupportsRelativeMouse);
}
SDL_bool Android_JNI_SetRelativeMouseEnabled(SDL_bool enabled)
{
JNIEnv *mEnv = Android_JNI_GetEnv();
return (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, midSetRelativeMouseEnabled, (enabled == 1));
}
#endif /* __ANDROID__ */ #endif /* __ANDROID__ */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -107,6 +107,10 @@ int Android_JNI_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y);
SDL_bool Android_JNI_SetCustomCursor(int cursorID); SDL_bool Android_JNI_SetCustomCursor(int cursorID);
SDL_bool Android_JNI_SetSystemCursor(int cursorID); SDL_bool Android_JNI_SetSystemCursor(int cursorID);
/* Relative mouse support */
SDL_bool Android_JNI_SupportsRelativeMouse();
SDL_bool Android_JNI_SetRelativeMouseEnabled(SDL_bool enabled);
/* Ends C function definitions when using C++ */ /* Ends C function definitions when using C++ */
#ifdef __cplusplus #ifdef __cplusplus
/* *INDENT-OFF* */ /* *INDENT-OFF* */

View File

@ -688,6 +688,7 @@ static SDL_bool
ShouldUseRelativeModeWarp(SDL_Mouse *mouse) ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
{ {
if (!mouse->SetRelativeMouseMode) { if (!mouse->SetRelativeMouseMode) {
SDL_assert(mouse->WarpMouse); /* Need this functionality for relative mode warp implementation */
return SDL_TRUE; return SDL_TRUE;
} }

View File

@ -137,6 +137,20 @@ Android_ShowCursor(SDL_Cursor * cursor)
return 0; return 0;
} }
static int
Android_SetRelativeMouseMode(SDL_bool enabled)
{
if (!Android_JNI_SupportsRelativeMouse()) {
return SDL_Unsupported();
}
if (!Android_JNI_SetRelativeMouseEnabled(enabled)) {
return SDL_Unsupported();
}
return 0;
}
void void
Android_InitMouse(void) Android_InitMouse(void)
{ {
@ -146,6 +160,7 @@ Android_InitMouse(void)
mouse->CreateSystemCursor = Android_CreateSystemCursor; mouse->CreateSystemCursor = Android_CreateSystemCursor;
mouse->ShowCursor = Android_ShowCursor; mouse->ShowCursor = Android_ShowCursor;
mouse->FreeCursor = Android_FreeCursor; mouse->FreeCursor = Android_FreeCursor;
mouse->SetRelativeMouseMode = Android_SetRelativeMouseMode;
SDL_SetDefaultCursor(Android_CreateDefaultCursor()); SDL_SetDefaultCursor(Android_CreateDefaultCursor());
@ -172,7 +187,7 @@ TranslateButton(int state)
} }
void void
Android_OnMouse(int state, int action, float x, float y) Android_OnMouse(int state, int action, float x, float y, SDL_bool relative)
{ {
int changes; int changes;
Uint8 button; Uint8 button;
@ -186,7 +201,7 @@ Android_OnMouse(int state, int action, float x, float y)
changes = state & ~last_state; changes = state & ~last_state;
button = TranslateButton(changes); button = TranslateButton(changes);
last_state = state; last_state = state;
SDL_SendMouseMotion(Android_Window, 0, 0, x, y); SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button); SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button);
break; break;
@ -194,13 +209,13 @@ Android_OnMouse(int state, int action, float x, float y)
changes = last_state & ~state; changes = last_state & ~state;
button = TranslateButton(changes); button = TranslateButton(changes);
last_state = state; last_state = state;
SDL_SendMouseMotion(Android_Window, 0, 0, x, y); SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button); SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button);
break; break;
case ACTION_MOVE: case ACTION_MOVE:
case ACTION_HOVER_MOVE: case ACTION_HOVER_MOVE:
SDL_SendMouseMotion(Android_Window, 0, 0, x, y); SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
break; break;
case ACTION_SCROLL: case ACTION_SCROLL:

View File

@ -25,7 +25,7 @@
#include "SDL_androidvideo.h" #include "SDL_androidvideo.h"
extern void Android_InitMouse(void); extern void Android_InitMouse(void);
extern void Android_OnMouse( int button, int action, float x, float y); extern void Android_OnMouse(int button, int action, float x, float y, SDL_bool relative);
#endif /* SDL_androidmouse_h_ */ #endif /* SDL_androidmouse_h_ */