2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2019-03-13 13:51:55 +01:00
|
|
|
#include "java_godot_lib_jni.h"
|
|
|
|
#include "java_godot_io_wrapper.h"
|
|
|
|
#include "java_godot_wrapper.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "android/asset_manager_jni.h"
|
2021-08-04 21:22:11 +02:00
|
|
|
#include "android_input_handler.h"
|
2020-01-19 20:02:40 +01:00
|
|
|
#include "api/java_class_wrapper.h"
|
2020-03-25 22:01:12 +01:00
|
|
|
#include "api/jni_singleton.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/engine.h"
|
|
|
|
#include "core/project_settings.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "dir_access_jandroid.h"
|
|
|
|
#include "file_access_android.h"
|
2021-07-11 03:39:31 +02:00
|
|
|
#include "file_access_filesystem_jandroid.h"
|
2019-10-18 18:59:04 +02:00
|
|
|
#include "jni_utils.h"
|
2016-01-24 05:11:59 +01:00
|
|
|
#include "main/input_default.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "main/main.h"
|
2019-11-25 15:01:44 +01:00
|
|
|
#include "net_socket_android.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "os_android.h"
|
2019-02-26 21:54:45 +01:00
|
|
|
#include "string_android.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "thread_jandroid.h"
|
2022-05-23 14:02:12 +02:00
|
|
|
#include "tts_android.h"
|
2020-10-09 15:35:45 +02:00
|
|
|
|
|
|
|
#include <android/input.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <unistd.h>
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
static JavaClassWrapper *java_class_wrapper = nullptr;
|
|
|
|
static OS_Android *os_android = nullptr;
|
|
|
|
static AndroidInputHandler *input_handler = nullptr;
|
|
|
|
static GodotJavaWrapper *godot_java = nullptr;
|
|
|
|
static GodotIOJavaWrapper *godot_io_java = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-08-13 02:44:57 +02:00
|
|
|
static SafeNumeric<int> step; // Shared between UI and render threads
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02: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
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
static void _initialize_java_modules() {
|
|
|
|
if (!ProjectSettings::get_singleton()->has_setting("android/modules")) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
String modules = ProjectSettings::get_singleton()->get("android/modules");
|
|
|
|
modules = modules.strip_edges();
|
|
|
|
if (modules == String()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Vector<String> mods = modules.split(",", false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
if (mods.size()) {
|
|
|
|
jobject cls = godot_java->get_class_loader();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
// TODO create wrapper for class loader
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-01-27 20:10:10 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2019-10-18 18:59:04 +02:00
|
|
|
jclass classLoader = env->FindClass("java/lang/ClassLoader");
|
|
|
|
jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
for (int i = 0; i < mods.size(); i++) {
|
|
|
|
String m = mods[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-15 11:43:42 +02:00
|
|
|
// Deprecated in Godot 3.2.2, it's now a plugin to enable in export preset.
|
|
|
|
if (m == "org/godotengine/godot/GodotPaymentV3") {
|
|
|
|
WARN_PRINT("GodotPaymentV3 is deprecated and is replaced by the 'GodotPayment' plugin, which should be enabled in the Android export preset.");
|
|
|
|
print_line("Skipping Android module: " + m);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
print_line("Loading Android module: " + m);
|
|
|
|
jstring strClassName = env->NewStringUTF(m.utf8().get_data());
|
|
|
|
jclass singletonClass = (jclass)env->CallObjectMethod(cls, findClass, strClassName);
|
|
|
|
ERR_CONTINUE_MSG(!singletonClass, "Couldn't find singleton for class: " + m + ".");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
jmethodID initialize = env->GetStaticMethodID(singletonClass, "initialize", "(Landroid/app/Activity;)Lorg/godotengine/godot/Godot$SingletonBase;");
|
|
|
|
ERR_CONTINUE_MSG(!initialize, "Couldn't find proper initialize function 'public static Godot.SingletonBase Class::initialize(Activity p_activity)' initializer for singleton class: " + m + ".");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
jobject obj = env->CallStaticObjectMethod(singletonClass, initialize, godot_java->get_activity());
|
|
|
|
env->NewGlobalRef(obj);
|
2015-03-03 18:39:13 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-11-15 17:46:45 +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) {
|
|
|
|
godot_java->destroy_offscreen_gl(env);
|
|
|
|
if (!restart_on_cleanup) {
|
|
|
|
if (p_restart) {
|
|
|
|
godot_java->restart(env);
|
|
|
|
} else {
|
|
|
|
godot_java->force_quit(env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete godot_java;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
extern "C" {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
|
2019-03-13 13:51:55 +01:00
|
|
|
if (godot_io_java) {
|
|
|
|
godot_io_java->set_vk_height(p_height);
|
|
|
|
}
|
2017-10-04 10:39:31 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 18:45:22 +02:00
|
|
|
JNIEXPORT void 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);
|
|
|
|
|
2019-03-13 13:51:55 +01:00
|
|
|
// create our wrapper classes
|
2021-07-11 03:39:31 +02:00
|
|
|
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
|
|
|
|
2021-01-27 20:10:10 +01:00
|
|
|
init_thread_jandroid(jvm, env);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
jobject amgr = env->NewGlobalRef(p_asset_manager);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
DirAccessJAndroid::setup(p_directory_access_handler);
|
|
|
|
FileAccessFilesystemJAndroid::setup(p_file_access_handler);
|
|
|
|
NetSocketAndroid::setup(p_net_utils);
|
2022-08-14 18:45:22 +02:00
|
|
|
TTS_Android::setup(p_godot_tts);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
godot_java->on_video_init(env);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-06-15 09:46:13 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
|
2022-11-15 17:46:45 +01:00
|
|
|
_terminate(env, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 23:25:22 +02:00
|
|
|
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline) {
|
2021-01-27 20:10:10 +01:00
|
|
|
setup_android_thread();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
const char **cmdline = nullptr;
|
|
|
|
jstring *j_cmdline = nullptr;
|
2017-08-18 16:17:35 +02:00
|
|
|
int cmdlen = 0;
|
|
|
|
if (p_cmdline) {
|
|
|
|
cmdlen = env->GetArrayLength(p_cmdline);
|
|
|
|
if (cmdlen) {
|
2021-03-10 19:31:44 +01:00
|
|
|
cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
|
2022-09-07 23:25:22 +02:00
|
|
|
ERR_FAIL_NULL_V_MSG(cmdline, false, "Out of memory.");
|
2022-05-30 23:13:49 +02:00
|
|
|
cmdline[cmdlen] = nullptr;
|
2021-03-10 19:31:44 +01:00
|
|
|
j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
|
2022-09-07 23:25:22 +02:00
|
|
|
ERR_FAIL_NULL_V_MSG(j_cmdline, false, "Out of memory.");
|
2017-08-18 16:17:35 +02:00
|
|
|
|
|
|
|
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);
|
2017-08-18 16:17:35 +02:00
|
|
|
|
|
|
|
cmdline[i] = rawString;
|
2019-02-26 21:54:45 +01:00
|
|
|
j_cmdline[i] = string;
|
2017-08-18 16:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:45:16 +02:00
|
|
|
Error err = Main::setup(OS_Android::ANDROID_EXEC_PATH, cmdlen, (char **)cmdline, false);
|
2017-08-18 16:17:35 +02:00
|
|
|
if (cmdline) {
|
2019-02-26 21:54:45 +01:00
|
|
|
if (j_cmdline) {
|
|
|
|
for (int i = 0; i < cmdlen; ++i) {
|
|
|
|
env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
|
|
|
|
}
|
2021-03-10 19:31:44 +01:00
|
|
|
memfree(j_cmdline);
|
2019-02-26 21:54:45 +01:00
|
|
|
}
|
2021-03-10 19:31:44 +01:00
|
|
|
memfree(cmdline);
|
2017-08-18 16:17:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 01:05:20 +02:00
|
|
|
// Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated.
|
2017-08-18 16:17:35 +02:00
|
|
|
if (err != OK) {
|
2022-09-07 23:25:22 +02:00
|
|
|
return false;
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
|
2020-03-25 22:01:12 +01:00
|
|
|
ClassDB::register_class<JNISingleton>();
|
2019-10-18 18:59:04 +02:00
|
|
|
_initialize_java_modules();
|
2022-09-07 23:25:22 +02:00
|
|
|
return true;
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
2014-03-12 13:41:09 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jint width, jint height) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (os_android) {
|
2019-10-18 18:59:04 +02:00
|
|
|
os_android->set_display_size(Size2(width, height));
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
2014-03-12 13:41:09 +01:00
|
|
|
|
2021-09-24 14:07:44 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz) {
|
2019-10-18 18:59:04 +02:00
|
|
|
if (os_android) {
|
2021-08-13 02:44:57 +02:00
|
|
|
if (step.get() == 0) {
|
2019-10-18 18:59:04 +02:00
|
|
|
// During startup
|
2021-09-24 14:07:44 +02:00
|
|
|
os_android->set_offscreen_gl_available(godot_java->create_offscreen_gl(env));
|
2019-10-18 18:59:04 +02:00
|
|
|
} else {
|
|
|
|
// GL context recreated because it was lost; restart app to let it reload everything
|
2022-11-15 17:46:45 +01:00
|
|
|
_terminate(env, true);
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 13:41:09 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() == 0) {
|
2019-10-18 18:59:04 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-04 21:22:11 +02:00
|
|
|
if (os_android->get_main_loop()) {
|
|
|
|
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST);
|
|
|
|
}
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 14:02:12 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:52 +02:00
|
|
|
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() == -1) {
|
2022-03-28 02:55:52 +02:00
|
|
|
return true;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-13 02:44:57 +02:00
|
|
|
if (step.get() == 0) {
|
2019-10-18 18:59:04 +02:00
|
|
|
// 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());
|
2021-08-04 21:22:11 +02:00
|
|
|
input_handler = new AndroidInputHandler();
|
2021-08-13 02:44:57 +02:00
|
|
|
step.increment();
|
2022-03-28 02:55:52 +02:00
|
|
|
return true;
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 02:44:57 +02:00
|
|
|
if (step.get() == 1) {
|
2019-10-18 18:59:04 +02:00
|
|
|
if (!Main::start()) {
|
2022-03-28 02:55:52 +02:00
|
|
|
return true; // should exit instead and print the error
|
2014-03-12 13:41:09 +01:00
|
|
|
}
|
2019-10-18 18:59:04 +02:00
|
|
|
|
2021-03-16 08:36:31 +01:00
|
|
|
godot_java->on_godot_setup_completed(env);
|
2019-10-18 18:59:04 +02:00
|
|
|
os_android->main_loop_begin();
|
2020-03-19 23:41:50 +01:00
|
|
|
godot_java->on_godot_main_loop_started(env);
|
2021-08-13 02:44:57 +02:00
|
|
|
step.increment();
|
2014-03-12 13:41:09 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
os_android->process_accelerometer(accelerometer);
|
|
|
|
os_android->process_gravity(gravity);
|
|
|
|
os_android->process_magnetometer(magnetometer);
|
|
|
|
os_android->process_gyroscope(gyroscope);
|
|
|
|
|
2022-03-28 02:55:52 +02:00
|
|
|
bool should_swap_buffers = false;
|
|
|
|
if (os_android->main_loop_iterate(&should_swap_buffers)) {
|
2022-11-15 17:46:45 +01:00
|
|
|
_terminate(env, false);
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
2022-03-28 02:55:52 +02:00
|
|
|
|
|
|
|
return should_swap_buffers;
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
|
2022-08-15 08:07:43 +02:00
|
|
|
// Called on the UI thread
|
2022-10-20 16:16:29 +02:00
|
|
|
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) {
|
2022-08-15 08:07:43 +02:00
|
|
|
if (step.get() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:16:29 +02:00
|
|
|
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);
|
2022-08-15 08:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called on the UI thread
|
2022-10-19 05:03:31 +02:00
|
|
|
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) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2019-10-18 18:59:04 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-04 21:22:11 +02:00
|
|
|
Vector<AndroidInputHandler::TouchPos> points;
|
2020-10-09 15:35:45 +02:00
|
|
|
for (int i = 0; i < pointer_count; i++) {
|
|
|
|
jfloat p[3];
|
2022-08-15 08:07:43 +02:00
|
|
|
env->GetFloatArrayRegion(position, i * 3, 3, p);
|
2021-08-04 21:22:11 +02:00
|
|
|
AndroidInputHandler::TouchPos tp;
|
2019-10-18 18:59:04 +02:00
|
|
|
tp.pos = Point2(p[1], p[2]);
|
2020-10-09 15:35:45 +02:00
|
|
|
tp.id = (int)p[0];
|
2019-10-18 18:59:04 +02:00
|
|
|
points.push_back(tp);
|
|
|
|
}
|
|
|
|
|
2022-10-19 05:03:31 +02:00
|
|
|
input_handler->process_touch_event(ev, pointer, points, p_double_tap);
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2022-08-15 08:07:43 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnify(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_factor) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2019-10-18 18:59:04 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2022-08-15 08:07:43 +02:00
|
|
|
input_handler->process_magnify(Point2(p_x, p_y), p_factor);
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2022-08-15 08:07:43 +02:00
|
|
|
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) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2019-10-18 18:59:04 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2022-08-15 08:07:43 +02:00
|
|
|
input_handler->process_pan(Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y));
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2018-03-27 17:03:44 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-04 21:22:11 +02:00
|
|
|
AndroidInputHandler::JoypadEvent jevent;
|
2016-01-24 05:11:59 +01:00
|
|
|
jevent.device = p_device;
|
2021-08-04 21:22:11 +02:00
|
|
|
jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON;
|
2016-01-24 05:11:59 +01:00
|
|
|
jevent.index = p_button;
|
2017-06-07 05:29:19 +02:00
|
|
|
jevent.pressed = p_pressed;
|
2014-07-02 05:09:36 +02:00
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
input_handler->process_joy_event(jevent);
|
2016-07-21 12:07:01 +02:00
|
|
|
}
|
2014-07-02 05:09:36 +02:00
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2018-03-27 17:03:44 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-04 21:22:11 +02:00
|
|
|
AndroidInputHandler::JoypadEvent jevent;
|
2016-01-24 05:11:59 +01:00
|
|
|
jevent.device = p_device;
|
2021-08-04 21:22:11 +02:00
|
|
|
jevent.type = AndroidInputHandler::JOY_EVENT_AXIS;
|
2016-01-24 05:11:59 +01:00
|
|
|
jevent.index = p_axis;
|
|
|
|
jevent.value = p_value;
|
2014-07-02 05:09:36 +02:00
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
input_handler->process_joy_event(jevent);
|
2016-07-21 12:07:01 +02:00
|
|
|
}
|
2014-07-02 05:09:36 +02:00
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2018-03-27 17:03:44 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-04 21:22:11 +02:00
|
|
|
AndroidInputHandler::JoypadEvent jevent;
|
2016-01-24 05:11:59 +01:00
|
|
|
jevent.device = p_device;
|
2021-08-04 21:22:11 +02:00
|
|
|
jevent.type = AndroidInputHandler::JOY_EVENT_HAT;
|
2016-01-24 05:11:59 +01:00
|
|
|
int hat = 0;
|
|
|
|
if (p_hat_x != 0) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (p_hat_x < 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
hat |= InputDefault::HAT_MASK_LEFT;
|
2022-05-30 23:13:49 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
hat |= InputDefault::HAT_MASK_RIGHT;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2016-01-24 05:11:59 +01:00
|
|
|
}
|
|
|
|
if (p_hat_y != 0) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (p_hat_y < 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
hat |= InputDefault::HAT_MASK_UP;
|
2022-05-30 23:13:49 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
hat |= InputDefault::HAT_MASK_DOWN;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2016-01-24 05:11:59 +01:00
|
|
|
}
|
|
|
|
jevent.hat = hat;
|
2017-10-02 11:40:16 +02:00
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
input_handler->process_joy_event(jevent);
|
2016-01-24 05:11:59 +01:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2021-08-04 21:22:11 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2021-08-04 21:22:11 +02:00
|
|
|
String name = jstring_to_string(p_name, env);
|
|
|
|
input_handler->joy_connection_changed(p_device, p_connected, name);
|
2016-01-24 05:11:59 +01:00
|
|
|
}
|
2014-07-02 05:09:36 +02:00
|
|
|
|
2021-08-04 21:22:11 +02:00
|
|
|
// Called on the UI thread
|
2022-07-10 13:32:01 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_scancode, jint p_physical_scancode, jint p_unicode, jboolean p_pressed) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2018-03-27 17:03:44 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2022-07-10 13:32:01 +02:00
|
|
|
input_handler->process_key_event(p_scancode, p_physical_scancode, p_unicode, p_pressed);
|
2016-07-21 12:07:01 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
|
2017-03-05 16:44:50 +01:00
|
|
|
accelerometer = Vector3(x, y, z);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
|
2017-11-10 23:42:23 +01:00
|
|
|
gravity = Vector3(x, y, z);
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
|
2017-03-05 16:44:50 +01:00
|
|
|
magnetometer = Vector3(x, y, z);
|
2016-05-27 19:29:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
|
2017-03-05 16:44:50 +01:00
|
|
|
gyroscope = Vector3(x, y, z);
|
2016-07-15 09:31:34 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2018-03-27 17:03:44 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2018-03-27 17:03:44 +02:00
|
|
|
os_android->main_loop_focusin();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2018-03-27 17:03:44 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2018-03-27 17:03:44 +02:00
|
|
|
os_android->main_loop_focusout();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
|
2019-02-26 21:54:45 +01:00
|
|
|
String js = jstring_to_string(path, env);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-07-19 22:00:46 +02:00
|
|
|
return env->NewStringUTF(ProjectSettings::get_singleton()->get(js).operator String().utf8().get_data());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 13:26:18 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(ID);
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(obj);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-05-18 17:45:53 +02:00
|
|
|
int res = env->PushLocalFrame(16);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(res != 0);
|
2015-05-18 17:45:53 +02:00
|
|
|
|
2019-02-26 21:54:45 +01:00
|
|
|
String str_method = jstring_to_string(method, env);
|
2015-05-18 17:45:53 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int count = env->GetArrayLength(params);
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-08-02 03:10:38 +02:00
|
|
|
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
|
|
|
|
|
|
|
Variant::CallError err;
|
2017-03-05 16:44:50 +01:00
|
|
|
obj->call(str_method, (const Variant **)vptr, count, err);
|
2015-05-18 17:45:53 +02:00
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
env->PopLocalFrame(nullptr);
|
2016-07-21 12:07:01 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-24 13:26:18 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(ID);
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(obj);
|
2014-02-13 22:03:28 +01:00
|
|
|
|
2015-05-18 17:45:53 +02:00
|
|
|
int res = env->PushLocalFrame(16);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(res != 0);
|
2015-05-18 17:45:53 +02:00
|
|
|
|
2019-02-26 21:54:45 +01:00
|
|
|
String str_method = jstring_to_string(method, env);
|
2015-05-18 17:45:53 +02:00
|
|
|
|
2014-02-13 22:03:28 +01:00
|
|
|
int count = env->GetArrayLength(params);
|
|
|
|
Variant args[VARIANT_ARG_MAX];
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < MIN(count, VARIANT_ARG_MAX); i++) {
|
2022-05-30 23:13:49 +02:00
|
|
|
jobject jobj = env->GetObjectArrayElement(params, i);
|
|
|
|
if (jobj) {
|
|
|
|
args[i] = _jobject_to_variant(env, jobj);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(jobj);
|
|
|
|
}
|
2014-02-13 22:03:28 +01:00
|
|
|
|
2021-10-24 04:40:03 +02:00
|
|
|
static_assert(VARIANT_ARG_MAX == 8, "This code needs to be updated if VARIANT_ARG_MAX != 8");
|
|
|
|
obj->call_deferred(str_method, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
|
2022-05-30 23:13:49 +02:00
|
|
|
|
|
|
|
env->PopLocalFrame(nullptr);
|
2016-07-21 12:07:01 +02:00
|
|
|
}
|
2019-03-05 03:06:37 +01:00
|
|
|
|
2019-10-18 18:59:04 +02:00
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
|
2019-03-05 03:06:37 +01:00
|
|
|
String permission = jstring_to_string(p_permission, env);
|
|
|
|
if (permission == "android.permission.RECORD_AUDIO" && p_result) {
|
|
|
|
AudioDriver::get_singleton()->capture_start();
|
|
|
|
}
|
2019-10-06 20:17:44 +02:00
|
|
|
|
|
|
|
if (os_android->get_main_loop()) {
|
|
|
|
os_android->get_main_loop()->emit_signal("on_request_permissions_result", permission, p_result == JNI_TRUE);
|
|
|
|
}
|
2019-03-05 03:06:37 +01:00
|
|
|
}
|
2019-09-09 23:42:17 +02:00
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2019-09-09 23:42:17 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2022-04-25 09:03:00 +02:00
|
|
|
// We force redraw to ensure we render at least once when resuming the app.
|
|
|
|
Main::force_redraw();
|
2019-09-09 23:42:17 +02:00
|
|
|
if (os_android->get_main_loop()) {
|
|
|
|
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APP_RESUMED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (step.get() <= 0) {
|
2019-09-09 23:42:17 +02:00
|
|
|
return;
|
2022-05-30 23:13:49 +02:00
|
|
|
}
|
2019-09-09 23:42:17 +02:00
|
|
|
if (os_android->get_main_loop()) {
|
|
|
|
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APP_PAUSED);
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 18:59:04 +02:00
|
|
|
}
|