java_godot_lib_jni.cpp 23 KB

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