From 07fabc066377945d8b8b1069899160986dc152af Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 7 Apr 2020 19:21:24 -0700 Subject: [PATCH] Fix CheckJNI abort with rumble values greater than 0x7FFF --- app/src/main/jni/moonlight-core/callbacks.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/jni/moonlight-core/callbacks.c b/app/src/main/jni/moonlight-core/callbacks.c index 49264b50..203e9a0d 100644 --- a/app/src/main/jni/moonlight-core/callbacks.c +++ b/app/src/main/jni/moonlight-core/callbacks.c @@ -304,7 +304,10 @@ void BridgeClConnectionTerminated(int errorCode) { void BridgeClRumble(unsigned short controllerNumber, unsigned short lowFreqMotor, unsigned short highFreqMotor) { JNIEnv* env = GetThreadEnv(); - (*env)->CallStaticVoidMethod(env, GlobalBridgeClass, BridgeClRumbleMethod, controllerNumber, lowFreqMotor, highFreqMotor); + // The seemingly redundant short casts are required in order to convert the unsigned short to a signed short. + // If we leave it as an unsigned short, CheckJNI will fail when the value exceeds 32767. The cast itself is + // fine because the Java code treats the value as unsigned even though it's stored in a signed type. + (*env)->CallStaticVoidMethod(env, GlobalBridgeClass, BridgeClRumbleMethod, controllerNumber, (short)lowFreqMotor, (short)highFreqMotor); if ((*env)->ExceptionCheck(env)) { // We will crash here (*JVM)->DetachCurrentThread(JVM);