Skip to content

Commit

Permalink
Add unity callback
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Sep 22, 2024
1 parent 724e13c commit 3d039fe
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public class CallbackBridge {
//Quick and dirty: debul all key inputs to System.out
*/
}

Runtime.getRuntime().addShutdownHook(new Thread(CallbackBridge::restartUnitySession));
}

public static native void restartUnitySession();
public static void sendData(int type, String data) {
nativeSendData(false, type, data);
}
Expand Down
Binary file modified src/main/assets/lwjgl/lwjgl-glfw-classes.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions src/main/java/org/lwjgl/glfw/CallbackBridge.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lwjgl.glfw;

import android.app.Activity;
import android.content.*;
import android.view.Choreographer;

Expand Down Expand Up @@ -189,6 +190,11 @@ public static void removeGrabListener(GrabListener listener) {
}
}

public static void restartUnitySession(Activity activity) {
UnityPlayerActivity unity = (UnityPlayerActivity) activity;
unity.reinitUnity();
}

@CriticalNative
public static native void nativeSetUseInputStackQueue(boolean useInputStackQueue);

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/pojlib/UnityPlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.ActivityManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.util.DisplayMetrics;
import android.view.InputDevice;
import android.view.KeyEvent;
Expand Down Expand Up @@ -134,6 +137,16 @@ public static String installLWJGL(Activity activity) throws IOException {
return lwjgl.getAbsolutePath();
}

public void reinitUnity() {
this.runOnUiThread(() -> {
Intent start = this.getPackageManager().getLaunchIntentForPackage(getApplicationInfo().packageName);
start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this.startActivity(start);
this.finish();
Process.killProcess(Process.myPid());
});
}

public static DisplayMetrics getDisplayMetrics(Activity activity) {
DisplayMetrics displayMetrics = new DisplayMetrics();

Expand Down
Binary file modified src/main/jni/OpenOVR/libDrvOpenXR.a
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/jni/environ/environ.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct pojav_environ_s {
jmethodID method_onGrabStateChanged;
jmethodID method_glftSetWindowAttrib;
jmethodID method_internalWindowSizeChanged;
jmethodID method_restartUnity;
jclass bridgeClazz;
jclass vmGlfwClass;
jboolean isGrabbing;
Expand Down
22 changes: 17 additions & 5 deletions src/main/jni/input_bridge_v3.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jint JNI_OnLoad(JavaVM* vm, __attribute__((unused)) void* reserved) {
pojav_environ->bridgeClazz = (*pojav_environ->dalvikJNIEnvPtr_ANDROID)->NewGlobalRef(pojav_environ->dalvikJNIEnvPtr_ANDROID,(*pojav_environ->dalvikJNIEnvPtr_ANDROID) ->FindClass(pojav_environ->dalvikJNIEnvPtr_ANDROID,"org/lwjgl/glfw/CallbackBridge"));
pojav_environ->method_accessAndroidClipboard = (*pojav_environ->dalvikJNIEnvPtr_ANDROID)->GetStaticMethodID(pojav_environ->dalvikJNIEnvPtr_ANDROID, pojav_environ->bridgeClazz, "accessAndroidClipboard", "(ILjava/lang/String;)Ljava/lang/String;");
pojav_environ->method_onGrabStateChanged = (*pojav_environ->dalvikJNIEnvPtr_ANDROID)->GetStaticMethodID(pojav_environ->dalvikJNIEnvPtr_ANDROID, pojav_environ->bridgeClazz, "onGrabStateChanged", "(Z)V");
pojav_environ->method_restartUnity = (*pojav_environ->dalvikJNIEnvPtr_ANDROID)->GetStaticMethodID(pojav_environ->dalvikJNIEnvPtr_ANDROID, pojav_environ->bridgeClazz, "restartUnitySession", "(Landroid/app/Activity;)V");
pojav_environ->isUseStackQueueCall = JNI_FALSE;
} else if (pojav_environ->dalvikJavaVMPtr != vm) {
__android_log_print(ANDROID_LOG_INFO, "Native", "Saving JVM environ...");
Expand Down Expand Up @@ -389,6 +390,15 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetInputRead
return JavaCritical_org_lwjgl_glfw_CallbackBridge_nativeSetInputReady(inputReady);
}


JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_CallbackBridge_restartUnitySession(JNIEnv *env, jclass clazz) {
JNIEnv *dalvikEnv;
(*pojav_environ->dalvikJavaVMPtr)->AttachCurrentThread(pojav_environ->dalvikJavaVMPtr, &dalvikEnv, NULL);
(*dalvikEnv)->CallStaticVoidMethod(dalvikEnv, pojav_environ->bridgeClazz, pojav_environ->method_restartUnity, pojav_environ->activity);
(*pojav_environ->dalvikJavaVMPtr)->DetachCurrentThread(pojav_environ->dalvikJavaVMPtr);
}

JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(__attribute__((unused)) JNIEnv* env, __attribute__((unused)) jclass clazz, jboolean grabbing) {
JNIEnv *dalvikEnv;
(*pojav_environ->dalvikJavaVMPtr)->AttachCurrentThread(pojav_environ->dalvikJavaVMPtr, &dalvikEnv, NULL);
Expand Down Expand Up @@ -616,13 +626,15 @@ static bool tryCriticalNative(JNIEnv *env) {
static void registerFunctions(JNIEnv *env) {
bool use_critical_cc = tryCriticalNative(env);
jclass bridge_class = (*env)->FindClass(env, "org/lwjgl/glfw/CallbackBridge");
if(use_critical_cc) {
__android_log_print(ANDROID_LOG_INFO, "pojavexec", "CriticalNative is available. Enjoy the 4.6x times faster input!");
}else{
__android_log_print(ANDROID_LOG_INFO, "pojavexec", "CriticalNative is not available. Upgrade, maybe?");
if (use_critical_cc) {
__android_log_print(ANDROID_LOG_INFO, "pojavexec",
"CriticalNative is available. Enjoy the 4.6x times faster input!");
} else {
__android_log_print(ANDROID_LOG_INFO, "pojavexec",
"CriticalNative is not available. Upgrade, maybe?");
}
(*env)->RegisterNatives(env,
bridge_class,
use_critical_cc ? critical_fcns : noncritical_fcns,
sizeof(critical_fcns)/sizeof(critical_fcns[0]));
sizeof(critical_fcns) / sizeof(critical_fcns[0]));
}

0 comments on commit 3d039fe

Please sign in to comment.