| 1 | /**************************************************************************/ |
| 2 | /* java_godot_lib_jni.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "java_godot_lib_jni.h" |
| 32 | |
| 33 | #include "android_input_handler.h" |
| 34 | #include "api/java_class_wrapper.h" |
| 35 | #include "dir_access_jandroid.h" |
| 36 | #include "display_server_android.h" |
| 37 | #include "file_access_android.h" |
| 38 | #include "file_access_filesystem_jandroid.h" |
| 39 | #include "java_godot_io_wrapper.h" |
| 40 | #include "java_godot_wrapper.h" |
| 41 | #include "jni_utils.h" |
| 42 | #include "net_socket_android.h" |
| 43 | #include "os_android.h" |
| 44 | #include "plugin/godot_plugin_jni.h" |
| 45 | #include "thread_jandroid.h" |
| 46 | #include "tts_android.h" |
| 47 | |
| 48 | #include "core/config/engine.h" |
| 49 | #include "core/config/project_settings.h" |
| 50 | #include "core/input/input.h" |
| 51 | #include "core/os/main_loop.h" |
| 52 | #include "core/profiling/profiling.h" |
| 53 | #include "main/main.h" |
| 54 | #include "servers/rendering/rendering_server.h" |
| 55 | |
| 56 | #include "servers/camera/camera_server.h" |
| 57 | |
| 58 | #ifndef XR_DISABLED |
| 59 | #include "servers/xr/xr_server.h" |
| 60 | #endif // XR_DISABLED |
| 61 | |
| 62 | #ifdef TOOLS_ENABLED |
| 63 | #include "editor/settings/editor_settings.h" |
| 64 | #endif |
| 65 | |
| 66 | #include <android/asset_manager_jni.h> |
| 67 | #include <android/input.h> |
| 68 | #include <android/native_window_jni.h> |
| 69 | #include <unistd.h> |
| 70 | |
| 71 | static JavaClassWrapper *java_class_wrapper = nullptr; |
| 72 | static OS_Android *os_android = nullptr; |
| 73 | static AndroidInputHandler *input_handler = nullptr; |
| 74 | static GodotJavaWrapper *godot_java = nullptr; |
| 75 | static GodotIOJavaWrapper *godot_io_java = nullptr; |
| 76 | |
| 77 | enum StartupStep { |
| 78 | STEP_TERMINATED = -1, |
| 79 | STEP_SETUP, |
| 80 | STEP_SHOW_LOGO, |
| 81 | STEP_STARTED |
| 82 | }; |
| 83 | |
| 84 | static SafeNumeric<int> step; // Shared between UI and render threads |
| 85 | |
| 86 | static Vector3 accelerometer; |
| 87 | static Vector3 gravity; |
| 88 | static Vector3 magnetometer; |
| 89 | static Vector3 gyroscope; |
| 90 | |
| 91 | static void _terminate(JNIEnv *env, bool p_restart = false) { |
| 92 | if (step.get() == STEP_TERMINATED) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | step.set(STEP_TERMINATED); // Ensure no further steps are attempted and no further events are sent |
| 97 | |
| 98 | // lets cleanup |
| 99 | // Unregister android plugins |
| 100 | unregister_plugins_singletons(); |
| 101 | |
| 102 | if (java_class_wrapper) { |
| 103 | memdelete(java_class_wrapper); |
| 104 | } |
| 105 | if (input_handler) { |
| 106 | delete input_handler; |
| 107 | } |
| 108 | // Whether restarting is handled by 'Main::cleanup()' |
| 109 | bool restart_on_cleanup = false; |
| 110 | if (os_android) { |
| 111 | restart_on_cleanup = os_android->is_restart_on_exit_set(); |
| 112 | os_android->main_loop_end(); |
| 113 | Main::cleanup(); |
| 114 | delete os_android; |
| 115 | } |
| 116 | if (godot_io_java) { |
| 117 | delete godot_io_java; |
| 118 | } |
| 119 | |
| 120 | TTS_Android::terminate(); |
| 121 | FileAccessAndroid::terminate(); |
| 122 | DirAccessJAndroid::terminate(); |
| 123 | FileAccessFilesystemJAndroid::terminate(); |
| 124 | NetSocketAndroid::terminate(); |
| 125 | |
| 126 | cleanup_android_class_loader(); |
| 127 | godot_cleanup_profiler(); |
| 128 | |
| 129 | if (godot_java) { |
| 130 | godot_java->on_godot_terminating(env); |
| 131 | if (!restart_on_cleanup) { |
| 132 | if (p_restart) { |
| 133 | godot_java->restart(env); |
| 134 | } else { |
| 135 | godot_java->force_quit(env); |
| 136 | } |
| 137 | } |
| 138 | delete godot_java; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | extern "C" { |
| 143 | |
| 144 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) { |
| 145 | if (godot_io_java) { |
| 146 | godot_io_java->set_vk_height(p_height); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, 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) { |
| 151 | godot_init_profiler(); |
| 152 | |
| 153 | JavaVM *jvm; |
| 154 | env->GetJavaVM(&jvm); |
| 155 | |
| 156 | init_thread_jandroid(jvm, env); |
| 157 | setup_android_class_loader(); |
| 158 | |
| 159 | // create our wrapper classes |
| 160 | godot_java = new GodotJavaWrapper(env, p_godot_instance); |
| 161 | godot_io_java = new GodotIOJavaWrapper(env, p_godot_io); |
| 162 | |
| 163 | FileAccessAndroid::setup(p_asset_manager); |
| 164 | DirAccessJAndroid::setup(p_directory_access_handler); |
| 165 | FileAccessFilesystemJAndroid::setup(p_file_access_handler); |
| 166 | NetSocketAndroid::setup(p_net_utils); |
| 167 | |
| 168 | os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion); |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) { |
| 174 | _terminate(env, false); |
| 175 | } |
| 176 | |
| 177 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline, jobject p_godot_tts) { |
| 178 | setup_android_thread(); |
| 179 | |
| 180 | const char **cmdline = nullptr; |
| 181 | jstring *j_cmdline = nullptr; |
| 182 | int cmdlen = 0; |
| 183 | if (p_cmdline) { |
| 184 | cmdlen = env->GetArrayLength(p_cmdline); |
| 185 | if (cmdlen) { |
| 186 | cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *)); |
| 187 | ERR_FAIL_NULL_V_MSG(cmdline, false, "Out of memory."); |
| 188 | cmdline[cmdlen] = nullptr; |
| 189 | j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring)); |
| 190 | ERR_FAIL_NULL_V_MSG(j_cmdline, false, "Out of memory."); |
| 191 | |
| 192 | for (int i = 0; i < cmdlen; i++) { |
| 193 | jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i); |
| 194 | const char *rawString = env->GetStringUTFChars(string, nullptr); |
| 195 | |
| 196 | cmdline[i] = rawString; |
| 197 | j_cmdline[i] = string; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | Error err = Main::setup(OS_Android::ANDROID_EXEC_PATH, cmdlen, (char **)cmdline, false); |
| 203 | if (cmdline) { |
| 204 | if (j_cmdline) { |
| 205 | for (int i = 0; i < cmdlen; ++i) { |
| 206 | env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]); |
| 207 | } |
| 208 | memfree(j_cmdline); |
| 209 | } |
| 210 | memfree(cmdline); |
| 211 | } |
| 212 | |
| 213 | // Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated. |
| 214 | if (err != OK) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | TTS_Android::setup(p_godot_tts); |
| 219 | |
| 220 | java_class_wrapper = memnew(JavaClassWrapper); |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jobject p_surface, jint p_width, jint p_height) { |
| 225 | if (os_android) { |
| 226 | os_android->set_display_size(Size2i(p_width, p_height)); |
| 227 | |
| 228 | // No need to reset the surface during startup |
| 229 | if (step.get() > STEP_SETUP) { |
| 230 | if (p_surface) { |
| 231 | ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface); |
| 232 | os_android->set_native_window(native_window); |
| 233 | } |
| 234 | DisplayServerAndroid::get_singleton()->reset_window(); |
| 235 | DisplayServerAndroid::get_singleton()->notify_surface_changed(p_width, p_height); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jobject p_surface) { |
| 241 | if (os_android) { |
| 242 | if (step.get() == STEP_SETUP) { |
| 243 | // During startup |
| 244 | if (p_surface) { |
| 245 | ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface); |
| 246 | os_android->set_native_window(native_window); |
| 247 | } |
| 248 | } else { |
| 249 | // Rendering context recreated because it was lost; restart app to let it reload everything |
| 250 | _terminate(env, true); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) { |
| 256 | if (step.get() <= STEP_SETUP) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) { |
| 261 | dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ttsCallback(JNIEnv *env, jclass clazz, jint event, jlong id, jint pos) { |
| 266 | TTS_Android::_java_utterance_callback(event, id, pos); |
| 267 | } |
| 268 | |
| 269 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) { |
| 270 | if (step.get() == STEP_TERMINATED) { |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | if (step.get() == STEP_SETUP) { |
| 275 | // Since Godot is initialized on the UI thread, main_thread_id was set to that thread's id, |
| 276 | // but for Godot purposes, the main thread is the one running the game loop |
| 277 | Main::setup2(false); // The logo is shown in the next frame otherwise we run into rendering issues |
| 278 | input_handler = new AndroidInputHandler(); |
| 279 | step.increment(); |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | if (step.get() == STEP_SHOW_LOGO) { |
| 284 | bool xr_enabled = false; |
| 285 | #ifndef XR_DISABLED |
| 286 | // Unlike PCVR, there's no additional 2D screen onto which to render the boot logo, |
| 287 | // so we skip this step if xr is enabled. |
| 288 | if (XRServer::get_xr_mode() == XRServer::XRMODE_DEFAULT) { |
| 289 | xr_enabled = GLOBAL_GET_CACHED(bool, "xr/shaders/enabled"); |
| 290 | } else { |
| 291 | xr_enabled = XRServer::get_xr_mode() == XRServer::XRMODE_ON; |
| 292 | } |
| 293 | #endif // XR_DISABLED |
| 294 | if (!xr_enabled) { |
| 295 | Main::setup_boot_logo(); |
| 296 | } |
| 297 | |
| 298 | step.increment(); |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | if (step.get() == STEP_STARTED) { |
| 303 | if (Main::start() != EXIT_SUCCESS) { |
| 304 | return true; // should exit instead and print the error |
| 305 | } |
| 306 | |
| 307 | godot_java->on_godot_setup_completed(env); |
| 308 | os_android->main_loop_begin(); |
| 309 | godot_java->on_godot_main_loop_started(env); |
| 310 | step.increment(); |
| 311 | } |
| 312 | |
| 313 | DisplayServerAndroid::get_singleton()->process_accelerometer(accelerometer); |
| 314 | DisplayServerAndroid::get_singleton()->process_gravity(gravity); |
| 315 | DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer); |
| 316 | DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope); |
| 317 | |
| 318 | bool should_swap_buffers = false; |
| 319 | if (os_android->main_loop_iterate(&should_swap_buffers)) { |
| 320 | _terminate(env, false); |
| 321 | } |
| 322 | |
| 323 | return should_swap_buffers; |
| 324 | } |
| 325 | |
| 326 | // Called on the UI thread |
| 327 | 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, jfloat p_pressure, jfloat p_tilt_x, jfloat p_tilt_y) { |
| 328 | if (step.get() <= STEP_SETUP) { |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | 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, p_pressure, Vector2(p_tilt_x, p_tilt_y)); |
| 333 | } |
| 334 | |
| 335 | // Called on the UI thread |
| 336 | 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) { |
| 337 | if (step.get() <= STEP_SETUP) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | Vector<AndroidInputHandler::TouchPos> points; |
| 342 | for (int i = 0; i < pointer_count; i++) { |
| 343 | jfloat p[6]; |
| 344 | env->GetFloatArrayRegion(position, i * 6, 6, p); |
| 345 | AndroidInputHandler::TouchPos tp; |
| 346 | tp.id = (int)p[0]; |
| 347 | tp.pos = Point2(p[1], p[2]); |
| 348 | tp.pressure = p[3]; |
| 349 | tp.tilt = Vector2(p[4], p[5]); |
| 350 | points.push_back(tp); |
| 351 | } |
| 352 | |
| 353 | input_handler->process_touch_event(ev, pointer, points, p_double_tap); |
| 354 | } |
| 355 | |
| 356 | // Called on the UI thread |
| 357 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnify(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_factor) { |
| 358 | if (step.get() <= STEP_SETUP) { |
| 359 | return; |
| 360 | } |
| 361 | input_handler->process_magnify(Point2(p_x, p_y), p_factor); |
| 362 | } |
| 363 | |
| 364 | // Called on the UI thread |
| 365 | 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) { |
| 366 | if (step.get() <= STEP_SETUP) { |
| 367 | return; |
| 368 | } |
| 369 | input_handler->process_pan(Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y)); |
| 370 | } |
| 371 | |
| 372 | // Called on the UI thread |
| 373 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) { |
| 374 | if (step.get() <= STEP_SETUP) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | AndroidInputHandler::JoypadEvent jevent; |
| 379 | jevent.device = p_device; |
| 380 | jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON; |
| 381 | jevent.index = p_button; |
| 382 | jevent.pressed = p_pressed; |
| 383 | |
| 384 | input_handler->process_joy_event(jevent); |
| 385 | } |
| 386 | |
| 387 | // Called on the UI thread |
| 388 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) { |
| 389 | if (step.get() <= STEP_SETUP) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | AndroidInputHandler::JoypadEvent jevent; |
| 394 | jevent.device = p_device; |
| 395 | jevent.type = AndroidInputHandler::JOY_EVENT_AXIS; |
| 396 | jevent.index = p_axis; |
| 397 | jevent.value = p_value; |
| 398 | |
| 399 | input_handler->process_joy_event(jevent); |
| 400 | } |
| 401 | |
| 402 | // Called on the UI thread |
| 403 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) { |
| 404 | if (step.get() <= STEP_SETUP) { |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | AndroidInputHandler::JoypadEvent jevent; |
| 409 | jevent.device = p_device; |
| 410 | jevent.type = AndroidInputHandler::JOY_EVENT_HAT; |
| 411 | BitField<HatMask> hat = HatMask::CENTER; |
| 412 | if (p_hat_x != 0) { |
| 413 | if (p_hat_x < 0) { |
| 414 | hat.set_flag(HatMask::LEFT); |
| 415 | } else { |
| 416 | hat.set_flag(HatMask::RIGHT); |
| 417 | } |
| 418 | } |
| 419 | if (p_hat_y != 0) { |
| 420 | if (p_hat_y < 0) { |
| 421 | hat.set_flag(HatMask::UP); |
| 422 | } else { |
| 423 | hat.set_flag(HatMask::DOWN); |
| 424 | } |
| 425 | } |
| 426 | jevent.hat = hat; |
| 427 | |
| 428 | input_handler->process_joy_event(jevent); |
| 429 | } |
| 430 | |
| 431 | // Called on the UI thread |
| 432 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) { |
| 433 | if (os_android) { |
| 434 | String name = jstring_to_string(p_name, env); |
| 435 | Input *input = Input::get_singleton(); |
| 436 | if (input) { |
| 437 | input->joy_connection_changed(p_device, p_connected, name); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // Called on the UI thread |
| 443 | 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, jboolean p_echo) { |
| 444 | if (step.get() <= STEP_SETUP) { |
| 445 | return; |
| 446 | } |
| 447 | input_handler->process_key_event(p_physical_keycode, p_unicode, p_key_label, p_pressed, p_echo); |
| 448 | } |
| 449 | |
| 450 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) { |
| 451 | accelerometer = Vector3(x, y, z); |
| 452 | } |
| 453 | |
| 454 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) { |
| 455 | gravity = Vector3(x, y, z); |
| 456 | } |
| 457 | |
| 458 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) { |
| 459 | magnetometer = Vector3(x, y, z); |
| 460 | } |
| 461 | |
| 462 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) { |
| 463 | gyroscope = Vector3(x, y, z); |
| 464 | } |
| 465 | |
| 466 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) { |
| 467 | if (step.get() <= STEP_SETUP) { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | os_android->main_loop_focusin(); |
| 472 | } |
| 473 | |
| 474 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) { |
| 475 | if (step.get() <= STEP_SETUP) { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | os_android->main_loop_focusout(); |
| 480 | } |
| 481 | |
| 482 | JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) { |
| 483 | String js = jstring_to_string(path, env); |
| 484 | |
| 485 | Variant setting_with_override = GLOBAL_GET(js); |
| 486 | String setting_value = (setting_with_override.get_type() == Variant::NIL) ? "" : setting_with_override; |
| 487 | return env->NewStringUTF(setting_value.utf8().get_data()); |
| 488 | } |
| 489 | |
| 490 | JNIEXPORT jobjectArray JNICALL Java_org_godotengine_godot_GodotLib_getRendererInfo(JNIEnv *env, jclass clazz) { |
| 491 | String rendering_driver = RenderingServer::get_singleton()->get_current_rendering_driver_name(); |
| 492 | String rendering_method = RenderingServer::get_singleton()->get_current_rendering_method(); |
| 493 | |
| 494 | jobjectArray result = env->NewObjectArray(2, jni_find_class(env, "java/lang/String"), nullptr); |
| 495 | env->SetObjectArrayElement(result, 0, env->NewStringUTF(rendering_driver.utf8().get_data())); |
| 496 | env->SetObjectArrayElement(result, 1, env->NewStringUTF(rendering_method.utf8().get_data())); |
| 497 | |
| 498 | return result; |
| 499 | } |
| 500 | |
| 501 | JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getEditorSetting(JNIEnv *env, jclass clazz, jstring p_setting_key) { |
| 502 | String editor_setting_value = ""; |
| 503 | #ifdef TOOLS_ENABLED |
| 504 | String godot_setting_key = jstring_to_string(p_setting_key, env); |
| 505 | Variant editor_setting = EDITOR_GET(godot_setting_key); |
| 506 | editor_setting_value = (editor_setting.get_type() == Variant::NIL) ? "" : editor_setting; |
| 507 | #else |
| 508 | WARN_PRINT("Access to the Editor Settings in only available on Editor builds"); |
| 509 | #endif |
| 510 | |
| 511 | return env->NewStringUTF(editor_setting_value.utf8().get_data()); |
| 512 | } |
| 513 | |
| 514 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setEditorSetting(JNIEnv *env, jclass clazz, jstring p_key, jobject p_data) { |
| 515 | #ifdef TOOLS_ENABLED |
| 516 | if (EditorSettings::get_singleton() != nullptr) { |
| 517 | String key = jstring_to_string(p_key, env); |
| 518 | Variant data = _jobject_to_variant(env, p_data); |
| 519 | EditorSettings::get_singleton()->set(key, data); |
| 520 | } |
| 521 | #else |
| 522 | WARN_PRINT("Access to the Editor Settings in only available on Editor builds"); |
| 523 | #endif |
| 524 | } |
| 525 | |
| 526 | JNIEXPORT jobject JNICALL Java_org_godotengine_godot_GodotLib_getEditorProjectMetadata(JNIEnv *env, jclass clazz, jstring p_section, jstring p_key, jobject p_default_value) { |
| 527 | jvalue result; |
| 528 | |
| 529 | #ifdef TOOLS_ENABLED |
| 530 | if (EditorSettings::get_singleton() != nullptr) { |
| 531 | String section = jstring_to_string(p_section, env); |
| 532 | String key = jstring_to_string(p_key, env); |
| 533 | Variant default_value = _jobject_to_variant(env, p_default_value); |
| 534 | Variant data = EditorSettings::get_singleton()->get_project_metadata(section, key, default_value); |
| 535 | result.l = _variant_to_jobject(env, data.get_type(), &data); |
| 536 | } |
| 537 | #else |
| 538 | WARN_PRINT("Access to the Editor Settings Project Metadata is only available on Editor builds"); |
| 539 | #endif |
| 540 | |
| 541 | return result.l; |
| 542 | } |
| 543 | |
| 544 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setEditorProjectMetadata(JNIEnv *env, jclass clazz, jstring p_section, jstring p_key, jobject p_data) { |
| 545 | #ifdef TOOLS_ENABLED |
| 546 | if (EditorSettings::get_singleton() != nullptr) { |
| 547 | String section = jstring_to_string(p_section, env); |
| 548 | String key = jstring_to_string(p_key, env); |
| 549 | Variant data = _jobject_to_variant(env, p_data); |
| 550 | EditorSettings::get_singleton()->set_project_metadata(section, key, data); |
| 551 | } |
| 552 | #else |
| 553 | WARN_PRINT("Access to the Editor Settings Project Metadata is only available on Editor builds"); |
| 554 | #endif |
| 555 | } |
| 556 | |
| 557 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onNightModeChanged(JNIEnv *env, jclass clazz) { |
| 558 | DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton(); |
| 559 | if (ds) { |
| 560 | ds->emit_system_theme_changed(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hardwareKeyboardConnected(JNIEnv *env, jclass clazz, jboolean p_connected) { |
| 565 | DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton(); |
| 566 | if (ds) { |
| 567 | ds->emit_hardware_keyboard_connection_changed(p_connected); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_filePickerCallback(JNIEnv *env, jclass clazz, jboolean p_ok, jobjectArray p_selected_paths) { |
| 572 | DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton(); |
| 573 | if (ds) { |
| 574 | Vector<String> selected_paths; |
| 575 | |
| 576 | jint length = env->GetArrayLength(p_selected_paths); |
| 577 | for (jint i = 0; i < length; ++i) { |
| 578 | jstring java_string = (jstring)env->GetObjectArrayElement(p_selected_paths, i); |
| 579 | String path = jstring_to_string(java_string, env); |
| 580 | selected_paths.push_back(path); |
| 581 | env->DeleteLocalRef(java_string); |
| 582 | } |
| 583 | ds->emit_file_picker_callback(p_ok, selected_paths); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) { |
| 588 | String permission = jstring_to_string(p_permission, env); |
| 589 | if (permission == "android.permission.RECORD_AUDIO" && p_result) { |
| 590 | AudioDriver::get_singleton()->input_start(); |
| 591 | } |
| 592 | |
| 593 | if (os_android->get_main_loop()) { |
| 594 | os_android->get_main_loop()->emit_signal(SNAME("on_request_permissions_result"), permission, p_result == JNI_TRUE); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) { |
| 599 | if (step.get() <= STEP_SETUP) { |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | // We force redraw to ensure we render at least once when resuming the app. |
| 604 | Main::force_redraw(); |
| 605 | CameraServer *camera_server = CameraServer::get_singleton(); |
| 606 | if (camera_server) { |
| 607 | camera_server->handle_application_resume(); |
| 608 | } |
| 609 | if (os_android->get_main_loop()) { |
| 610 | os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) { |
| 615 | if (step.get() <= STEP_SETUP) { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | CameraServer *camera_server = CameraServer::get_singleton(); |
| 620 | if (camera_server) { |
| 621 | camera_server->handle_application_pause(); |
| 622 | } |
| 623 | |
| 624 | if (os_android->get_main_loop()) { |
| 625 | os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED); |
| 626 | } |
| 627 | |
| 628 | if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) { |
| 629 | dsa->notify_application_paused(); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz, jint p_orientation) { |
| 634 | if (step.get() <= STEP_SETUP) { |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | CameraServer *camera_server = CameraServer::get_singleton(); |
| 639 | if (camera_server) { |
| 640 | camera_server->handle_display_rotation_change(p_orientation); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz) { |
| 645 | Input *input = Input::get_singleton(); |
| 646 | if (input) { |
| 647 | return !input->is_agile_input_event_flushing(); |
| 648 | } |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getProjectResourceDir(JNIEnv *env, jclass clazz) { |
| 653 | const String resource_dir = OS::get_singleton()->get_resource_dir(); |
| 654 | return env->NewStringUTF(resource_dir.utf8().get_data()); |
| 655 | } |
| 656 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_isEditorHint(JNIEnv *env, jclass clazz) { |
| 657 | Engine *engine = Engine::get_singleton(); |
| 658 | if (engine) { |
| 659 | return engine->is_editor_hint(); |
| 660 | } |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_isProjectManagerHint(JNIEnv *env, jclass clazz) { |
| 665 | Engine *engine = Engine::get_singleton(); |
| 666 | if (engine) { |
| 667 | return engine->is_project_manager_hint(); |
| 668 | } |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_hasFeature(JNIEnv *env, jclass clazz, jstring p_feature) { |
| 673 | OS *os = OS::get_singleton(); |
| 674 | if (os) { |
| 675 | String feature = jstring_to_string(p_feature, env); |
| 676 | return os->has_feature(feature); |
| 677 | } |
| 678 | return false; |
| 679 | } |
| 680 | } |