Skip to content

Commit

Permalink
Add Android support for relative mouse mode to SDL.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jun 5, 2018
1 parent 9d6ac3d commit 2dedbc7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
26 changes: 22 additions & 4 deletions src/core/android/SDL_android.c
Expand Up @@ -102,7 +102,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(

JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
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)(
JNIEnv* env, jclass jcls,
Expand Down Expand Up @@ -226,6 +226,8 @@ static jmethodID midGetDisplayDPI;
static jmethodID midCreateCustomCursor;
static jmethodID midSetCustomCursor;
static jmethodID midSetSystemCursor;
static jmethodID midSupportsRelativeMouse;
static jmethodID midSetRelativeMouseEnabled;

/* audio manager */
static jclass mAudioManagerClass;
Expand Down Expand Up @@ -339,12 +341,15 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(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 ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!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?");
}

Expand Down Expand Up @@ -682,9 +687,9 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
/* Mouse */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
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 */
Expand Down Expand Up @@ -2202,6 +2207,19 @@ SDL_bool Android_JNI_SetSystemCursor(int 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__ */

/* vi: set ts=4 sw=4 expandtab: */
4 changes: 4 additions & 0 deletions src/core/android/SDL_android.h
Expand Up @@ -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_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++ */
#ifdef __cplusplus
/* *INDENT-OFF* */
Expand Down
1 change: 1 addition & 0 deletions src/events/SDL_mouse.c
Expand Up @@ -688,6 +688,7 @@ static SDL_bool
ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
{
if (!mouse->SetRelativeMouseMode) {
SDL_assert(mouse->WarpMouse); /* Need this functionality for relative mode warp implementation */
return SDL_TRUE;
}

Expand Down
23 changes: 19 additions & 4 deletions src/video/android/SDL_androidmouse.c
Expand Up @@ -137,6 +137,20 @@ Android_ShowCursor(SDL_Cursor * cursor)
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
Android_InitMouse(void)
{
Expand All @@ -146,6 +160,7 @@ Android_InitMouse(void)
mouse->CreateSystemCursor = Android_CreateSystemCursor;
mouse->ShowCursor = Android_ShowCursor;
mouse->FreeCursor = Android_FreeCursor;
mouse->SetRelativeMouseMode = Android_SetRelativeMouseMode;

SDL_SetDefaultCursor(Android_CreateDefaultCursor());

Expand All @@ -172,7 +187,7 @@ TranslateButton(int state)
}

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;
Uint8 button;
Expand All @@ -186,21 +201,21 @@ Android_OnMouse(int state, int action, float x, float y)
changes = state & ~last_state;
button = TranslateButton(changes);
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);
break;

case ACTION_UP:
changes = last_state & ~state;
button = TranslateButton(changes);
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);
break;

case ACTION_MOVE:
case ACTION_HOVER_MOVE:
SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
SDL_SendMouseMotion(Android_Window, 0, relative, x, y);
break;

case ACTION_SCROLL:
Expand Down
2 changes: 1 addition & 1 deletion src/video/android/SDL_androidmouse.h
Expand Up @@ -25,7 +25,7 @@
#include "SDL_androidvideo.h"

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_ */

Expand Down

0 comments on commit 2dedbc7

Please sign in to comment.