virtualx-engine/platform/android/java_godot_lib_jni.cpp

521 lines
17 KiB
C++
Raw Normal View History

/**************************************************************************/
/* java_godot_lib_jni.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "java_godot_lib_jni.h"
#include "java_godot_io_wrapper.h"
#include "java_godot_wrapper.h"
#include "android/asset_manager_jni.h"
#include "android_input_handler.h"
#include "api/java_class_wrapper.h"
#include "api/jni_singleton.h"
#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "core/input/input.h"
#include "dir_access_jandroid.h"
2020-03-27 17:30:18 +01:00
#include "display_server_android.h"
#include "file_access_android.h"
#include "file_access_filesystem_jandroid.h"
#include "jni_utils.h"
#include "main/main.h"
#include "net_socket_android.h"
#include "os_android.h"
#include "string_android.h"
#include "thread_jandroid.h"
#include "tts_android.h"
#include <android/input.h>
#include <unistd.h>
2020-03-05 19:00:28 +01:00
#include <android/native_window_jni.h>
2020-04-02 01:20:12 +02:00
static JavaClassWrapper *java_class_wrapper = nullptr;
static OS_Android *os_android = nullptr;
static AndroidInputHandler *input_handler = nullptr;
2020-04-02 01:20:12 +02:00
static GodotJavaWrapper *godot_java = nullptr;
static GodotIOJavaWrapper *godot_io_java = nullptr;
2014-02-10 02:10:30 +01:00
static SafeNumeric<int> step; // Shared between UI and render threads
2014-02-10 02:10:30 +01:00
static Size2 new_size;
static Vector3 accelerometer;
static Vector3 gravity;
static Vector3 magnetometer;
static Vector3 gyroscope;
2014-02-10 02:10:30 +01:00
static void _terminate(JNIEnv *env, bool p_restart = false) {
step.set(-1); // Ensure no further steps are attempted and no further events are sent
// lets cleanup
if (java_class_wrapper) {
memdelete(java_class_wrapper);
}
if (input_handler) {
delete input_handler;
}
// Whether restarting is handled by 'Main::cleanup()'
bool restart_on_cleanup = false;
if (os_android) {
restart_on_cleanup = os_android->is_restart_on_exit_set();
os_android->main_loop_end();
Main::cleanup();
delete os_android;
}
if (godot_io_java) {
delete godot_io_java;
}
if (godot_java) {
if (!restart_on_cleanup) {
if (p_restart) {
godot_java->restart(env);
} else {
godot_java->force_quit(env);
}
}
delete godot_java;
}
}
extern "C" {
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
if (godot_io_java) {
godot_io_java->set_vk_height(p_height);
}
}
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject p_activity, jobject p_godot_instance, jobject p_asset_manager, jobject p_godot_io, jobject p_net_utils, jobject p_directory_access_handler, jobject p_file_access_handler, jboolean p_use_apk_expansion, jobject p_godot_tts) {
2014-02-10 02:10:30 +01:00
JavaVM *jvm;
env->GetJavaVM(&jvm);
// create our wrapper classes
godot_java = new GodotJavaWrapper(env, p_activity, p_godot_instance);
godot_io_java = new GodotIOJavaWrapper(env, p_godot_io);
2014-02-10 02:10:30 +01:00
init_thread_jandroid(jvm, env);
2015-09-04 04:24:55 +02:00
jobject amgr = env->NewGlobalRef(p_asset_manager);
2015-09-04 04:24:55 +02:00
FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
2014-02-10 02:10:30 +01:00
DirAccessJAndroid::setup(p_directory_access_handler);
FileAccessFilesystemJAndroid::setup(p_file_access_handler);
NetSocketAndroid::setup(p_net_utils);
TTS_Android::setup(p_godot_tts);
os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
2014-02-10 02:10:30 +01:00
return godot_java->on_video_init(env);
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
_terminate(env, false);
2014-02-10 02:10:30 +01:00
}
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline) {
setup_android_thread();
2014-02-10 02:10:30 +01:00
2020-04-02 01:20:12 +02:00
const char **cmdline = nullptr;
jstring *j_cmdline = nullptr;
int cmdlen = 0;
if (p_cmdline) {
cmdlen = env->GetArrayLength(p_cmdline);
if (cmdlen) {
cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
ERR_FAIL_NULL_V_MSG(cmdline, false, "Out of memory.");
2020-04-02 01:20:12 +02:00
cmdline[cmdlen] = nullptr;
j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
ERR_FAIL_NULL_V_MSG(j_cmdline, false, "Out of memory.");
for (int i = 0; i < cmdlen; i++) {
jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
2022-05-30 23:13:49 +02:00
const char *rawString = env->GetStringUTFChars(string, nullptr);
cmdline[i] = rawString;
j_cmdline[i] = string;
}
}
}
Error err = Main::setup(OS_Android::ANDROID_EXEC_PATH, cmdlen, (char **)cmdline, false);
if (cmdline) {
if (j_cmdline) {
for (int i = 0; i < cmdlen; ++i) {
env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
}
memfree(j_cmdline);
}
memfree(cmdline);
}
// Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated.
if (err != OK) {
return false;
}
java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
GDREGISTER_CLASS(JNISingleton);
return true;
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jobject p_surface, jint p_width, jint p_height) {
if (os_android) {
os_android->set_display_size(Size2i(p_width, p_height));
// No need to reset the surface during startup
if (step.get() > 0) {
if (p_surface) {
ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
os_android->set_native_window(native_window);
DisplayServerAndroid::get_singleton()->reset_window();
2021-05-03 18:23:05 +02:00
DisplayServerAndroid::get_singleton()->notify_surface_changed(p_width, p_height);
}
}
}
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jobject p_surface) {
if (os_android) {
if (step.get() == 0) {
// During startup
2020-03-05 19:00:28 +01:00
if (p_surface) {
ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
os_android->set_native_window(native_window);
}
} else {
2020-03-05 19:00:28 +01:00
// Rendering context recreated because it was lost; restart app to let it reload everything
_terminate(env, true);
}
}
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
if (step.get() == 0) {
return;
}
if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {
dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST);
}
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ttsCallback(JNIEnv *env, jclass clazz, jint event, jint id, jint pos) {
TTS_Android::_java_utterance_callback(event, id, pos);
}
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
if (step.get() == -1) {
return true;
}
if (step.get() == 0) {
// Since Godot is initialized on the UI thread, main_thread_id was set to that thread's id,
// but for Godot purposes, the main thread is the one running the game loop
Main::setup2(Thread::get_caller_id());
input_handler = new AndroidInputHandler();
step.increment();
return true;
}
if (step.get() == 1) {
2014-02-10 02:10:30 +01:00
if (!Main::start()) {
return true; // should exit instead and print the error
2014-02-10 02:10:30 +01:00
}
godot_java->on_godot_setup_completed(env);
2014-02-10 02:10:30 +01:00
os_android->main_loop_begin();
godot_java->on_godot_main_loop_started(env);
step.increment();
2014-02-10 02:10:30 +01:00
}
2020-03-27 17:30:18 +01:00
DisplayServerAndroid::get_singleton()->process_accelerometer(accelerometer);
DisplayServerAndroid::get_singleton()->process_gravity(gravity);
DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer);
DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope);
bool should_swap_buffers = false;
if (os_android->main_loop_iterate(&should_swap_buffers)) {
_terminate(env, false);
2014-02-10 02:10:30 +01:00
}
return should_swap_buffers;
2014-02-10 02:10:30 +01:00
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchMouseEvent(JNIEnv *env, jclass clazz, jint p_event_type, jint p_button_mask, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y, jboolean p_double_click, jboolean p_source_mouse_relative) {
if (step.get() <= 0) {
return;
}
input_handler->process_mouse_event(p_event_type, p_button_mask, Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y), p_double_click, p_source_mouse_relative);
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchTouchEvent(JNIEnv *env, jclass clazz, jint ev, jint pointer, jint pointer_count, jfloatArray position, jboolean p_double_tap) {
if (step.get() <= 0) {
return;
}
Vector<AndroidInputHandler::TouchPos> points;
for (int i = 0; i < pointer_count; i++) {
jfloat p[3];
env->GetFloatArrayRegion(position, i * 3, 3, p);
AndroidInputHandler::TouchPos tp;
tp.pos = Point2(p[1], p[2]);
tp.id = (int)p[0];
2014-02-10 02:10:30 +01:00
points.push_back(tp);
}
input_handler->process_touch_event(ev, pointer, points, p_double_tap);
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnify(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_factor) {
if (step.get() <= 0) {
2018-08-29 17:06:48 +02:00
return;
}
input_handler->process_magnify(Point2(p_x, p_y), p_factor);
2014-02-10 02:10:30 +01:00
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_pan(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y) {
if (step.get() <= 0) {
return;
}
input_handler->process_pan(Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y));
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
if (step.get() <= 0) {
return;
}
AndroidInputHandler::JoypadEvent jevent;
2016-01-24 05:11:59 +01:00
jevent.device = p_device;
jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON;
2016-01-24 05:11:59 +01:00
jevent.index = p_button;
jevent.pressed = p_pressed;
input_handler->process_joy_event(jevent);
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
2021-08-13 23:31:57 +02:00
if (step.get() <= 0) {
return;
2021-08-13 23:31:57 +02:00
}
AndroidInputHandler::JoypadEvent jevent;
2016-01-24 05:11:59 +01:00
jevent.device = p_device;
jevent.type = AndroidInputHandler::JOY_EVENT_AXIS;
2016-01-24 05:11:59 +01:00
jevent.index = p_axis;
jevent.value = p_value;
input_handler->process_joy_event(jevent);
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
2021-08-13 23:31:57 +02:00
if (step.get() <= 0) {
return;
2021-08-13 23:31:57 +02:00
}
AndroidInputHandler::JoypadEvent jevent;
2016-01-24 05:11:59 +01:00
jevent.device = p_device;
jevent.type = AndroidInputHandler::JOY_EVENT_HAT;
BitField<HatMask> hat;
2016-01-24 05:11:59 +01:00
if (p_hat_x != 0) {
2021-08-13 23:31:57 +02:00
if (p_hat_x < 0) {
hat.set_flag(HatMask::LEFT);
2021-08-13 23:31:57 +02:00
} else {
hat.set_flag(HatMask::RIGHT);
2021-08-13 23:31:57 +02:00
}
2016-01-24 05:11:59 +01:00
}
if (p_hat_y != 0) {
2021-08-13 23:31:57 +02:00
if (p_hat_y < 0) {
hat.set_flag(HatMask::UP);
2021-08-13 23:31:57 +02:00
} else {
hat.set_flag(HatMask::DOWN);
2021-08-13 23:31:57 +02:00
}
2016-01-24 05:11:59 +01:00
}
jevent.hat = hat;
input_handler->process_joy_event(jevent);
2016-01-24 05:11:59 +01:00
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
2016-01-24 05:11:59 +01:00
if (os_android) {
String name = jstring_to_string(p_name, env);
Input::get_singleton()->joy_connection_changed(p_device, p_connected, name);
2016-01-24 05:11:59 +01:00
}
}
// Called on the UI thread
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_physical_keycode, jint p_unicode, jint p_key_label, jboolean p_pressed) {
if (step.get() <= 0) {
return;
}
input_handler->process_key_event(p_physical_keycode, p_unicode, p_key_label, p_pressed);
}
2014-02-10 02:10:30 +01:00
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
accelerometer = Vector3(x, y, z);
2014-02-10 02:10:30 +01:00
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
gravity = Vector3(x, y, z);
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
magnetometer = Vector3(x, y, z);
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
gyroscope = Vector3(x, y, z);
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
if (step.get() <= 0) {
return;
}
os_android->main_loop_focusin();
2014-02-10 02:10:30 +01:00
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
if (step.get() <= 0) {
return;
}
os_android->main_loop_focusout();
2014-02-10 02:10:30 +01:00
}
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
String js = jstring_to_string(path, env);
2014-02-10 02:10:30 +01:00
2022-10-18 16:43:37 +02:00
return env->NewStringUTF(GLOBAL_GET(js).operator String().utf8().get_data());
2014-02-10 02:10:30 +01:00
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
Object *obj = ObjectDB::get_instance(ObjectID(ID));
2022-05-30 23:13:49 +02:00
ERR_FAIL_NULL(obj);
2014-02-10 02:10:30 +01:00
int res = env->PushLocalFrame(16);
ERR_FAIL_COND(res != 0);
String str_method = jstring_to_string(method, env);
2014-02-10 02:10:30 +01:00
int count = env->GetArrayLength(params);
Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
Variant **vptr = (Variant **)alloca(sizeof(Variant *) * count);
for (int i = 0; i < count; i++) {
2022-05-30 23:13:49 +02:00
jobject jobj = env->GetObjectArrayElement(params, i);
Variant v;
2022-05-30 23:13:49 +02:00
if (jobj) {
v = _jobject_to_variant(env, jobj);
}
2014-02-10 02:10:30 +01:00
memnew_placement(&vlist[i], Variant);
vlist[i] = v;
vptr[i] = &vlist[i];
2022-05-30 23:13:49 +02:00
env->DeleteLocalRef(jobj);
}
2014-02-10 02:10:30 +01:00
Callable::CallError err;
obj->callp(str_method, (const Variant **)vptr, count, err);
2020-04-02 01:20:12 +02:00
env->PopLocalFrame(nullptr);
}
2014-02-10 02:10:30 +01:00
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
Object *obj = ObjectDB::get_instance(ObjectID(ID));
2022-05-30 23:13:49 +02:00
ERR_FAIL_NULL(obj);
int res = env->PushLocalFrame(16);
ERR_FAIL_COND(res != 0);
String str_method = jstring_to_string(method, env);
int count = env->GetArrayLength(params);
Variant *args = (Variant *)alloca(sizeof(Variant) * count);
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * count);
for (int i = 0; i < count; i++) {
2022-05-30 23:13:49 +02:00
jobject jobj = env->GetObjectArrayElement(params, i);
if (jobj) {
args[i] = _jobject_to_variant(env, jobj);
}
2022-05-30 23:13:49 +02:00
env->DeleteLocalRef(jobj);
argptrs[i] = &args[i];
}
MessageQueue::get_singleton()->push_callp(obj, str_method, (const Variant **)argptrs, count);
2022-05-30 23:13:49 +02:00
2020-04-02 01:20:12 +02:00
env->PopLocalFrame(nullptr);
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
String permission = jstring_to_string(p_permission, env);
if (permission == "android.permission.RECORD_AUDIO" && p_result) {
AudioDriver::get_singleton()->capture_start();
}
if (os_android->get_main_loop()) {
os_android->get_main_loop()->emit_signal(SNAME("on_request_permissions_result"), permission, p_result == JNI_TRUE);
}
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
if (step.get() <= 0) {
return;
}
// We force redraw to ensure we render at least once when resuming the app.
Main::force_redraw();
if (os_android->get_main_loop()) {
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
}
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
if (step.get() <= 0) {
return;
}
if (os_android->get_main_loop()) {
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
}
}
}