java_godot_lib_jni.cpp 22 KB

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