|
@@ -1,5 +1,5 @@
|
|
|
/*************************************************************************/
|
|
|
-/* java_glue.cpp */
|
|
|
+/* java_godot_lib_jni.cpp */
|
|
|
/*************************************************************************/
|
|
|
/* This file is part of: */
|
|
|
/* GODOT ENGINE */
|
|
@@ -28,7 +28,10 @@
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
/*************************************************************************/
|
|
|
|
|
|
-#include "java_glue.h"
|
|
|
+#include "java_godot_lib_jni.h"
|
|
|
+#include "java_godot_io_wrapper.h"
|
|
|
+#include "java_godot_wrapper.h"
|
|
|
+
|
|
|
#include "android/asset_manager_jni.h"
|
|
|
#include "audio_driver_jandroid.h"
|
|
|
#include "core/engine.h"
|
|
@@ -47,6 +50,8 @@
|
|
|
|
|
|
static JavaClassWrapper *java_class_wrapper = NULL;
|
|
|
static OS_Android *os_android = NULL;
|
|
|
+static GodotJavaWrapper *godot_java = NULL;
|
|
|
+static GodotIOJavaWrapper *godot_io_java = NULL;
|
|
|
|
|
|
struct jvalret {
|
|
|
|
|
@@ -588,181 +593,23 @@ TST tst;
|
|
|
|
|
|
static bool initialized = false;
|
|
|
static int step = 0;
|
|
|
+
|
|
|
static Size2 new_size;
|
|
|
static Vector3 accelerometer;
|
|
|
static Vector3 gravity;
|
|
|
static Vector3 magnetometer;
|
|
|
static Vector3 gyroscope;
|
|
|
static HashMap<String, JNISingleton *> jni_singletons;
|
|
|
-static jobject godot_io;
|
|
|
-
|
|
|
-typedef void (*GFXInitFunc)(void *ud, bool gl2);
|
|
|
-
|
|
|
-static jmethodID _on_video_init = 0;
|
|
|
-static jmethodID _restart = 0;
|
|
|
-static jobject _godot_instance;
|
|
|
-
|
|
|
-static jmethodID _openURI = 0;
|
|
|
-static jmethodID _getDataDir = 0;
|
|
|
-static jmethodID _getLocale = 0;
|
|
|
-static jmethodID _getClipboard = 0;
|
|
|
-static jmethodID _setClipboard = 0;
|
|
|
-static jmethodID _getModel = 0;
|
|
|
-static jmethodID _getScreenDPI = 0;
|
|
|
-static jmethodID _showKeyboard = 0;
|
|
|
-static jmethodID _hideKeyboard = 0;
|
|
|
-static jmethodID _setScreenOrientation = 0;
|
|
|
-static jmethodID _getUniqueID = 0;
|
|
|
-static jmethodID _getSystemDir = 0;
|
|
|
-static jmethodID _getGLESVersionCode = 0;
|
|
|
-static jmethodID _playVideo = 0;
|
|
|
-static jmethodID _isVideoPlaying = 0;
|
|
|
-static jmethodID _pauseVideo = 0;
|
|
|
-static jmethodID _stopVideo = 0;
|
|
|
-static jmethodID _setKeepScreenOn = 0;
|
|
|
-static jmethodID _alertDialog = 0;
|
|
|
-static jmethodID _requestPermission = 0;
|
|
|
-
|
|
|
-static void _gfx_init_func(void *ud, bool gl2) {
|
|
|
-}
|
|
|
-
|
|
|
-static int _open_uri(const String &p_uri) {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
|
|
|
- return env->CallIntMethod(godot_io, _openURI, jStr);
|
|
|
-}
|
|
|
-
|
|
|
-static String _get_user_data_dir() {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring s = (jstring)env->CallObjectMethod(godot_io, _getDataDir);
|
|
|
- return jstring_to_string(s, env);
|
|
|
-}
|
|
|
-
|
|
|
-static String _get_locale() {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring s = (jstring)env->CallObjectMethod(godot_io, _getLocale);
|
|
|
- return jstring_to_string(s, env);
|
|
|
-}
|
|
|
-
|
|
|
-static String _get_clipboard() {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring s = (jstring)env->CallObjectMethod(_godot_instance, _getClipboard);
|
|
|
- return jstring_to_string(s, env);
|
|
|
-}
|
|
|
-
|
|
|
-static void _set_clipboard(const String &p_text) {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
|
|
|
- env->CallVoidMethod(_godot_instance, _setClipboard, jStr);
|
|
|
-}
|
|
|
-
|
|
|
-static String _get_model() {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring s = (jstring)env->CallObjectMethod(godot_io, _getModel);
|
|
|
- return jstring_to_string(s, env);
|
|
|
-}
|
|
|
-
|
|
|
-static int _get_screen_dpi() {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- return env->CallIntMethod(godot_io, _getScreenDPI);
|
|
|
-}
|
|
|
-
|
|
|
-static String _get_unique_id() {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring s = (jstring)env->CallObjectMethod(godot_io, _getUniqueID);
|
|
|
- return jstring_to_string(s, env);
|
|
|
-}
|
|
|
-
|
|
|
-static void _show_vk(const String &p_existing) {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
|
|
|
- env->CallVoidMethod(godot_io, _showKeyboard, jStr);
|
|
|
-}
|
|
|
-
|
|
|
-static void _set_screen_orient(int p_orient) {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- env->CallVoidMethod(godot_io, _setScreenOrientation, p_orient);
|
|
|
-}
|
|
|
-
|
|
|
-static String _get_system_dir(int p_dir) {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring s = (jstring)env->CallObjectMethod(godot_io, _getSystemDir, p_dir);
|
|
|
- return jstring_to_string(s, env);
|
|
|
-}
|
|
|
-
|
|
|
-static int _get_gles_version_code() {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- return env->CallIntMethod(_godot_instance, _getGLESVersionCode);
|
|
|
-}
|
|
|
-
|
|
|
-static void _hide_vk() {
|
|
|
-
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- env->CallVoidMethod(godot_io, _hideKeyboard);
|
|
|
-}
|
|
|
|
|
|
// virtual Error native_video_play(String p_path);
|
|
|
// virtual bool native_video_is_playing();
|
|
|
// virtual void native_video_pause();
|
|
|
// virtual void native_video_stop();
|
|
|
|
|
|
-static void _play_video(const String &p_path) {
|
|
|
-}
|
|
|
-
|
|
|
-static bool _is_video_playing() {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- return env->CallBooleanMethod(godot_io, _isVideoPlaying);
|
|
|
- //return false;
|
|
|
-}
|
|
|
-
|
|
|
-static void _pause_video() {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- env->CallVoidMethod(godot_io, _pauseVideo);
|
|
|
-}
|
|
|
-
|
|
|
-static void _stop_video() {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- env->CallVoidMethod(godot_io, _stopVideo);
|
|
|
-}
|
|
|
-
|
|
|
-static void _set_keep_screen_on(bool p_enabled) {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- env->CallVoidMethod(_godot_instance, _setKeepScreenOn, p_enabled);
|
|
|
-}
|
|
|
-
|
|
|
-static void _alert(const String &p_message, const String &p_title) {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
|
|
|
- jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
|
|
|
- env->CallVoidMethod(_godot_instance, _alertDialog, jStrMessage, jStrTitle);
|
|
|
-}
|
|
|
-
|
|
|
-static bool _request_permission(const String &p_name) {
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
- jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
|
|
|
- return env->CallBooleanMethod(_godot_instance, _requestPermission, jStrName);
|
|
|
-}
|
|
|
-
|
|
|
-// volatile because it can be changed from non-main thread and we need to
|
|
|
-// ensure the change is immediately visible to other threads.
|
|
|
-static volatile int virtual_keyboard_height;
|
|
|
-
|
|
|
-static int _get_vk_height() {
|
|
|
- return virtual_keyboard_height;
|
|
|
-}
|
|
|
-
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jobject obj, jint p_height) {
|
|
|
- virtual_keyboard_height = p_height;
|
|
|
+ if (godot_io_java) {
|
|
|
+ godot_io_java->set_vk_height(p_height);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jobject obj, jobject activity, jobject p_asset_manager, jboolean p_use_apk_expansion) {
|
|
@@ -772,70 +619,42 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
|
|
|
JavaVM *jvm;
|
|
|
env->GetJavaVM(&jvm);
|
|
|
|
|
|
- _godot_instance = env->NewGlobalRef(activity);
|
|
|
- //_godot_instance=activity;
|
|
|
-
|
|
|
- {
|
|
|
- //setup IO Object
|
|
|
-
|
|
|
- jclass cls = env->FindClass("org/godotengine/godot/Godot");
|
|
|
- if (cls) {
|
|
|
-
|
|
|
- cls = (jclass)env->NewGlobalRef(cls);
|
|
|
- }
|
|
|
-
|
|
|
- jfieldID fid = env->GetStaticFieldID(cls, "io", "Lorg/godotengine/godot/GodotIO;");
|
|
|
- jobject ob = env->GetStaticObjectField(cls, fid);
|
|
|
- jobject gob = env->NewGlobalRef(ob);
|
|
|
-
|
|
|
- godot_io = gob;
|
|
|
-
|
|
|
- _on_video_init = env->GetMethodID(cls, "onVideoInit", "()V");
|
|
|
- _restart = env->GetMethodID(cls, "restart", "()V");
|
|
|
- _setKeepScreenOn = env->GetMethodID(cls, "setKeepScreenOn", "(Z)V");
|
|
|
- _alertDialog = env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
|
|
|
- _getGLESVersionCode = env->GetMethodID(cls, "getGLESVersionCode", "()I");
|
|
|
- _getClipboard = env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;");
|
|
|
- _setClipboard = env->GetMethodID(cls, "setClipboard", "(Ljava/lang/String;)V");
|
|
|
- _requestPermission = env->GetMethodID(cls, "requestPermission", "(Ljava/lang/String;)Z");
|
|
|
-
|
|
|
- if (cls) {
|
|
|
- jclass c = env->GetObjectClass(gob);
|
|
|
- _openURI = env->GetMethodID(c, "openURI", "(Ljava/lang/String;)I");
|
|
|
- _getDataDir = env->GetMethodID(c, "getDataDir", "()Ljava/lang/String;");
|
|
|
- _getLocale = env->GetMethodID(c, "getLocale", "()Ljava/lang/String;");
|
|
|
- _getModel = env->GetMethodID(c, "getModel", "()Ljava/lang/String;");
|
|
|
- _getScreenDPI = env->GetMethodID(c, "getScreenDPI", "()I");
|
|
|
- _getUniqueID = env->GetMethodID(c, "getUniqueID", "()Ljava/lang/String;");
|
|
|
- _showKeyboard = env->GetMethodID(c, "showKeyboard", "(Ljava/lang/String;)V");
|
|
|
- _hideKeyboard = env->GetMethodID(c, "hideKeyboard", "()V");
|
|
|
- _setScreenOrientation = env->GetMethodID(c, "setScreenOrientation", "(I)V");
|
|
|
- _getSystemDir = env->GetMethodID(c, "getSystemDir", "(I)Ljava/lang/String;");
|
|
|
- _playVideo = env->GetMethodID(c, "playVideo", "(Ljava/lang/String;)V");
|
|
|
- _isVideoPlaying = env->GetMethodID(c, "isVideoPlaying", "()Z");
|
|
|
- _pauseVideo = env->GetMethodID(c, "pauseVideo", "()V");
|
|
|
- _stopVideo = env->GetMethodID(c, "stopVideo", "()V");
|
|
|
- }
|
|
|
+ // create our wrapper classes
|
|
|
+ godot_java = new GodotJavaWrapper(env, activity); // our activity is our godot instance is our activity..
|
|
|
+ godot_io_java = new GodotIOJavaWrapper(env, godot_java->get_member_object("io", "Lorg/godotengine/godot/GodotIO;", env));
|
|
|
|
|
|
- ThreadAndroid::make_default(jvm);
|
|
|
+ ThreadAndroid::make_default(jvm);
|
|
|
#ifdef USE_JAVA_FILE_ACCESS
|
|
|
- FileAccessJAndroid::setup(gob);
|
|
|
+ FileAccessJAndroid::setup(godot_io_java->get_instance());
|
|
|
#else
|
|
|
|
|
|
- jobject amgr = env->NewGlobalRef(p_asset_manager);
|
|
|
+ jobject amgr = env->NewGlobalRef(p_asset_manager);
|
|
|
|
|
|
- FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
|
|
|
+ FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
|
|
|
#endif
|
|
|
- DirAccessJAndroid::setup(gob);
|
|
|
- AudioDriverAndroid::setup(gob);
|
|
|
- }
|
|
|
|
|
|
- os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_user_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _get_vk_height, _set_screen_orient, _get_unique_id, _get_system_dir, _get_gles_version_code, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, _set_clipboard, _get_clipboard, _request_permission, p_use_apk_expansion);
|
|
|
+ DirAccessJAndroid::setup(godot_io_java->get_instance());
|
|
|
+ AudioDriverAndroid::setup(godot_io_java->get_instance());
|
|
|
+
|
|
|
+ os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
|
|
|
|
|
|
char wd[500];
|
|
|
getcwd(wd, 500);
|
|
|
|
|
|
- env->CallVoidMethod(_godot_instance, _on_video_init);
|
|
|
+ godot_java->on_video_init(env);
|
|
|
+}
|
|
|
+
|
|
|
+JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env) {
|
|
|
+ // lets cleanup
|
|
|
+ if (godot_io_java) {
|
|
|
+ delete godot_io_java;
|
|
|
+ }
|
|
|
+ if (godot_java) {
|
|
|
+ delete godot_java;
|
|
|
+ }
|
|
|
+ if (os_android) {
|
|
|
+ delete os_android;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static void _initialize_java_modules() {
|
|
@@ -852,17 +671,12 @@ static void _initialize_java_modules() {
|
|
|
Vector<String> mods = modules.split(",", false);
|
|
|
|
|
|
if (mods.size()) {
|
|
|
+ jobject cls = godot_java->get_class_loader();
|
|
|
|
|
|
- JNIEnv *env = ThreadAndroid::get_env();
|
|
|
-
|
|
|
- jclass activityClass = env->FindClass("org/godotengine/godot/Godot");
|
|
|
-
|
|
|
- jmethodID getClassLoader = env->GetMethodID(activityClass, "getClassLoader", "()Ljava/lang/ClassLoader;");
|
|
|
-
|
|
|
- jobject cls = env->CallObjectMethod(_godot_instance, getClassLoader);
|
|
|
+ // TODO create wrapper for class loader
|
|
|
|
|
|
+ JNIEnv *env = ThreadAndroid::get_env();
|
|
|
jclass classLoader = env->FindClass("java/lang/ClassLoader");
|
|
|
-
|
|
|
jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
|
|
|
|
|
|
for (int i = 0; i < mods.size(); i++) {
|
|
@@ -886,7 +700,7 @@ static void _initialize_java_modules() {
|
|
|
ERR_EXPLAIN("Couldn't find proper initialize function 'public static Godot.SingletonBase Class::initialize(Activity p_activity)' initializer for singleton class: " + m);
|
|
|
ERR_CONTINUE(!initialize);
|
|
|
}
|
|
|
- jobject obj = env->CallStaticObjectMethod(singletonClass, initialize, _godot_instance);
|
|
|
+ jobject obj = env->CallStaticObjectMethod(singletonClass, initialize, godot_java->get_activity());
|
|
|
env->NewGlobalRef(obj);
|
|
|
}
|
|
|
}
|
|
@@ -931,7 +745,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jo
|
|
|
return; //should exit instead and print the error
|
|
|
}
|
|
|
|
|
|
- java_class_wrapper = memnew(JavaClassWrapper(_godot_instance));
|
|
|
+ java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
|
|
|
Engine::get_singleton()->add_singleton(Engine::Singleton("JavaClassWrapper", java_class_wrapper));
|
|
|
_initialize_java_modules();
|
|
|
}
|
|
@@ -951,7 +765,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *en
|
|
|
} else {
|
|
|
// GL context recreated because it was lost; restart app to let it reload everything
|
|
|
os_android->main_loop_end();
|
|
|
- env->CallVoidMethod(_godot_instance, _restart);
|
|
|
+ godot_java->restart(env);
|
|
|
step = -1; // Ensure no further steps are attempted
|
|
|
}
|
|
|
}
|
|
@@ -987,18 +801,13 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, job
|
|
|
}
|
|
|
|
|
|
os_android->process_accelerometer(accelerometer);
|
|
|
-
|
|
|
os_android->process_gravity(gravity);
|
|
|
-
|
|
|
os_android->process_magnetometer(magnetometer);
|
|
|
-
|
|
|
os_android->process_gyroscope(gyroscope);
|
|
|
|
|
|
if (os_android->main_loop_iterate()) {
|
|
|
|
|
|
- jclass cls = env->FindClass("org/godotengine/godot/Godot");
|
|
|
- jmethodID _finish = env->GetMethodID(cls, "forceQuit", "()V");
|
|
|
- env->CallVoidMethod(_godot_instance, _finish);
|
|
|
+ godot_java->force_quit(env);
|
|
|
}
|
|
|
}
|
|
|
|