Implement SDL_HapticStopEffect on Android (thanks Rachel!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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 3e1e40f..342610f 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 f3f10a9..9178604 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 6b3dfcb..a3a7b19 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 1fb2352..5dd1552 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;
}