From 09ab752aa399f54dddc558442d05aa39b5b48d80 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 24 Aug 2018 10:41:57 -0700 Subject: [PATCH] Implement SDL_HapticStopEffect on Android (thanks Rachel!) --- .../java/org/libsdl/app/SDLControllerManager.java | 15 +++++++++++++++ src/core/android/SDL_android.c | 10 +++++++++- src/core/android/SDL_android.h | 1 + src/haptic/android/SDL_syshaptic.c | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java index 3e1e40f75..342610fe7 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java @@ -81,6 +81,14 @@ public class SDLControllerManager mHapticHandler.run(device_id, length); } + /** + * This method is called by SDL using JNI. + */ + public static void hapticStop(int device_id) + { + mHapticHandler.stop(device_id); + } + // Check if a given device is considered a possible SDL joystick public static boolean isDeviceSDLJoystick(int deviceId) { InputDevice device = InputDevice.getDevice(deviceId); @@ -422,6 +430,13 @@ class SDLHapticHandler { } } + public void stop(int device_id) { + SDLHaptic haptic = getHaptic(device_id); + if (haptic != null) { + haptic.vib.cancel(); + } + } + public void pollHapticDevices() { final int deviceId_VIBRATOR_SERVICE = 999999; diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c index f3f10a947..91786042d 100644 --- a/src/core/android/SDL_android.c +++ b/src/core/android/SDL_android.c @@ -258,6 +258,7 @@ static jclass mControllerManagerClass; static jmethodID midPollInputDevices; static jmethodID midPollHapticDevices; static jmethodID midHapticRun; +static jmethodID midHapticStop; /* static fields */ static jfieldID fidSeparateMouseAndTouch; @@ -430,8 +431,10 @@ JNIEXPORT void JNICALL SDL_JAVA_CONTROLLER_INTERFACE(nativeSetupJNI)(JNIEnv* mEn "pollHapticDevices", "()V"); midHapticRun = (*mEnv)->GetStaticMethodID(mEnv, mControllerManagerClass, "hapticRun", "(II)V"); + midHapticStop = (*mEnv)->GetStaticMethodID(mEnv, mControllerManagerClass, + "hapticStop", "(I)V"); - if (!midPollInputDevices || !midPollHapticDevices || !midHapticRun) { + if (!midPollInputDevices || !midPollHapticDevices || !midHapticRun || !midHapticStop) { __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLControllerManager.java?"); } @@ -1892,6 +1895,11 @@ void Android_JNI_HapticRun(int device_id, int length) (*env)->CallStaticVoidMethod(env, mControllerManagerClass, midHapticRun, device_id, length); } +void Android_JNI_HapticStop(int device_id) +{ + JNIEnv *env = Android_JNI_GetEnv(); + (*env)->CallStaticVoidMethod(env, mControllerManagerClass, midHapticStop, device_id); +} /* See SDLActivity.java for constants. */ #define COMMAND_SET_KEEP_SCREEN_ON 5 diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h index 6b3dfcb9e..a3a7b192c 100644 --- a/src/core/android/SDL_android.h +++ b/src/core/android/SDL_android.h @@ -79,6 +79,7 @@ void Android_JNI_PollInputDevices(void); /* Haptic support */ void Android_JNI_PollHapticDevices(void); void Android_JNI_HapticRun(int device_id, int length); +void Android_JNI_HapticStop(int device_id); /* Video */ void Android_JNI_SuspendScreenSaver(SDL_bool suspend); diff --git a/src/haptic/android/SDL_syshaptic.c b/src/haptic/android/SDL_syshaptic.c index 1fb235249..5dd15526b 100644 --- a/src/haptic/android/SDL_syshaptic.c +++ b/src/haptic/android/SDL_syshaptic.c @@ -238,6 +238,7 @@ SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, int SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect) { + Android_JNI_HapticStop (((SDL_hapticlist_item *)haptic->hwdata)->device_id); return 0; }