java_godot_lib_jni.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 "api/jni_singleton.h"
  34. #include "dir_access_jandroid.h"
  35. #include "display_server_android.h"
  36. #include "file_access_android.h"
  37. #include "file_access_filesystem_jandroid.h"
  38. #include "java_godot_io_wrapper.h"
  39. #include "java_godot_wrapper.h"
  40. #include "jni_utils.h"
  41. #include "net_socket_android.h"
  42. #include "os_android.h"
  43. #include "plugin/godot_plugin_jni.h"
  44. #include "string_android.h"
  45. #include "thread_jandroid.h"
  46. #include "tts_android.h"
  47. #include "core/config/engine.h"
  48. #include "core/config/project_settings.h"
  49. #include "core/input/input.h"
  50. #include "main/main.h"
  51. #include "servers/xr_server.h"
  52. #ifdef TOOLS_ENABLED
  53. #include "editor/editor_settings.h"
  54. #endif
  55. #include <android/asset_manager_jni.h>
  56. #include <android/input.h>
  57. #include <android/native_window_jni.h>
  58. #include <unistd.h>
  59. static JavaClassWrapper *java_class_wrapper = nullptr;
  60. static OS_Android *os_android = nullptr;
  61. static AndroidInputHandler *input_handler = nullptr;
  62. static GodotJavaWrapper *godot_java = nullptr;
  63. static GodotIOJavaWrapper *godot_io_java = nullptr;
  64. enum StartupStep {
  65. STEP_TERMINATED = -1,
  66. STEP_SETUP,
  67. STEP_SHOW_LOGO,
  68. STEP_STARTED
  69. };
  70. static SafeNumeric<int> step; // Shared between UI and render threads
  71. static Size2 new_size;
  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. if (godot_java) {
  107. godot_java->on_godot_terminating(env);
  108. if (!restart_on_cleanup) {
  109. if (p_restart) {
  110. godot_java->restart(env);
  111. } else {
  112. godot_java->force_quit(env);
  113. }
  114. }
  115. delete godot_java;
  116. }
  117. }
  118. extern "C" {
  119. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
  120. if (godot_io_java) {
  121. godot_io_java->set_vk_height(p_height);
  122. }
  123. }
  124. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject p_activity, jobject p_godot_instance, jobject p_asset_manager, jobject p_godot_io, jobject p_net_utils, jobject p_directory_access_handler, jobject p_file_access_handler, jboolean p_use_apk_expansion) {
  125. JavaVM *jvm;
  126. env->GetJavaVM(&jvm);
  127. // create our wrapper classes
  128. godot_java = new GodotJavaWrapper(env, p_activity, p_godot_instance);
  129. godot_io_java = new GodotIOJavaWrapper(env, p_godot_io);
  130. init_thread_jandroid(jvm, env);
  131. FileAccessAndroid::setup(p_asset_manager);
  132. DirAccessJAndroid::setup(p_directory_access_handler);
  133. FileAccessFilesystemJAndroid::setup(p_file_access_handler);
  134. NetSocketAndroid::setup(p_net_utils);
  135. os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
  136. return true;
  137. }
  138. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
  139. _terminate(env, false);
  140. }
  141. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline, jobject p_godot_tts) {
  142. setup_android_thread();
  143. const char **cmdline = nullptr;
  144. jstring *j_cmdline = nullptr;
  145. int cmdlen = 0;
  146. if (p_cmdline) {
  147. cmdlen = env->GetArrayLength(p_cmdline);
  148. if (cmdlen) {
  149. cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
  150. ERR_FAIL_NULL_V_MSG(cmdline, false, "Out of memory.");
  151. cmdline[cmdlen] = nullptr;
  152. j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
  153. ERR_FAIL_NULL_V_MSG(j_cmdline, false, "Out of memory.");
  154. for (int i = 0; i < cmdlen; i++) {
  155. jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
  156. const char *rawString = env->GetStringUTFChars(string, nullptr);
  157. cmdline[i] = rawString;
  158. j_cmdline[i] = string;
  159. }
  160. }
  161. }
  162. Error err = Main::setup(OS_Android::ANDROID_EXEC_PATH, cmdlen, (char **)cmdline, false);
  163. if (cmdline) {
  164. if (j_cmdline) {
  165. for (int i = 0; i < cmdlen; ++i) {
  166. env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
  167. }
  168. memfree(j_cmdline);
  169. }
  170. memfree(cmdline);
  171. }
  172. // Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated.
  173. if (err != OK) {
  174. return false;
  175. }
  176. TTS_Android::setup(p_godot_tts);
  177. java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
  178. GDREGISTER_CLASS(JNISingleton);
  179. return true;
  180. }
  181. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jobject p_surface, jint p_width, jint p_height) {
  182. if (os_android) {
  183. os_android->set_display_size(Size2i(p_width, p_height));
  184. // No need to reset the surface during startup
  185. if (step.get() > STEP_SETUP) {
  186. if (p_surface) {
  187. ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
  188. os_android->set_native_window(native_window);
  189. }
  190. DisplayServerAndroid::get_singleton()->reset_window();
  191. DisplayServerAndroid::get_singleton()->notify_surface_changed(p_width, p_height);
  192. }
  193. }
  194. }
  195. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jobject p_surface) {
  196. if (os_android) {
  197. if (step.get() == STEP_SETUP) {
  198. // During startup
  199. if (p_surface) {
  200. ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
  201. os_android->set_native_window(native_window);
  202. }
  203. } else {
  204. // Rendering context recreated because it was lost; restart app to let it reload everything
  205. _terminate(env, true);
  206. }
  207. }
  208. }
  209. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
  210. if (step.get() <= STEP_SETUP) {
  211. return;
  212. }
  213. if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {
  214. dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST);
  215. }
  216. }
  217. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ttsCallback(JNIEnv *env, jclass clazz, jint event, jint id, jint pos) {
  218. TTS_Android::_java_utterance_callback(event, id, pos);
  219. }
  220. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
  221. if (step.get() == STEP_TERMINATED) {
  222. return true;
  223. }
  224. if (step.get() == STEP_SETUP) {
  225. // Since Godot is initialized on the UI thread, main_thread_id was set to that thread's id,
  226. // but for Godot purposes, the main thread is the one running the game loop
  227. Main::setup2(false); // The logo is shown in the next frame otherwise we run into rendering issues
  228. input_handler = new AndroidInputHandler();
  229. step.increment();
  230. return true;
  231. }
  232. if (step.get() == STEP_SHOW_LOGO) {
  233. bool xr_enabled;
  234. if (XRServer::get_xr_mode() == XRServer::XRMODE_DEFAULT) {
  235. xr_enabled = GLOBAL_GET("xr/shaders/enabled");
  236. } else {
  237. xr_enabled = XRServer::get_xr_mode() == XRServer::XRMODE_ON;
  238. }
  239. // Unlike PCVR, there's no additional 2D screen onto which to render the boot logo,
  240. // so we skip this step if xr is enabled.
  241. if (!xr_enabled) {
  242. Main::setup_boot_logo();
  243. }
  244. step.increment();
  245. return true;
  246. }
  247. if (step.get() == STEP_STARTED) {
  248. if (Main::start() != EXIT_SUCCESS) {
  249. return true; // should exit instead and print the error
  250. }
  251. godot_java->on_godot_setup_completed(env);
  252. os_android->main_loop_begin();
  253. godot_java->on_godot_main_loop_started(env);
  254. step.increment();
  255. }
  256. DisplayServerAndroid::get_singleton()->process_accelerometer(accelerometer);
  257. DisplayServerAndroid::get_singleton()->process_gravity(gravity);
  258. DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer);
  259. DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope);
  260. bool should_swap_buffers = false;
  261. if (os_android->main_loop_iterate(&should_swap_buffers)) {
  262. _terminate(env, false);
  263. }
  264. return should_swap_buffers;
  265. }
  266. // Called on the UI thread
  267. 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) {
  268. if (step.get() <= STEP_SETUP) {
  269. return;
  270. }
  271. 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));
  272. }
  273. // Called on the UI thread
  274. 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) {
  275. if (step.get() <= STEP_SETUP) {
  276. return;
  277. }
  278. Vector<AndroidInputHandler::TouchPos> points;
  279. for (int i = 0; i < pointer_count; i++) {
  280. jfloat p[6];
  281. env->GetFloatArrayRegion(position, i * 6, 6, p);
  282. AndroidInputHandler::TouchPos tp;
  283. tp.id = (int)p[0];
  284. tp.pos = Point2(p[1], p[2]);
  285. tp.pressure = p[3];
  286. tp.tilt = Vector2(p[4], p[5]);
  287. points.push_back(tp);
  288. }
  289. input_handler->process_touch_event(ev, pointer, points, p_double_tap);
  290. }
  291. // Called on the UI thread
  292. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnify(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_factor) {
  293. if (step.get() <= STEP_SETUP) {
  294. return;
  295. }
  296. input_handler->process_magnify(Point2(p_x, p_y), p_factor);
  297. }
  298. // Called on the UI thread
  299. 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) {
  300. if (step.get() <= STEP_SETUP) {
  301. return;
  302. }
  303. input_handler->process_pan(Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y));
  304. }
  305. // Called on the UI thread
  306. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
  307. if (step.get() <= STEP_SETUP) {
  308. return;
  309. }
  310. AndroidInputHandler::JoypadEvent jevent;
  311. jevent.device = p_device;
  312. jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON;
  313. jevent.index = p_button;
  314. jevent.pressed = p_pressed;
  315. input_handler->process_joy_event(jevent);
  316. }
  317. // Called on the UI thread
  318. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
  319. if (step.get() <= STEP_SETUP) {
  320. return;
  321. }
  322. AndroidInputHandler::JoypadEvent jevent;
  323. jevent.device = p_device;
  324. jevent.type = AndroidInputHandler::JOY_EVENT_AXIS;
  325. jevent.index = p_axis;
  326. jevent.value = p_value;
  327. input_handler->process_joy_event(jevent);
  328. }
  329. // Called on the UI thread
  330. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
  331. if (step.get() <= STEP_SETUP) {
  332. return;
  333. }
  334. AndroidInputHandler::JoypadEvent jevent;
  335. jevent.device = p_device;
  336. jevent.type = AndroidInputHandler::JOY_EVENT_HAT;
  337. BitField<HatMask> hat;
  338. if (p_hat_x != 0) {
  339. if (p_hat_x < 0) {
  340. hat.set_flag(HatMask::LEFT);
  341. } else {
  342. hat.set_flag(HatMask::RIGHT);
  343. }
  344. }
  345. if (p_hat_y != 0) {
  346. if (p_hat_y < 0) {
  347. hat.set_flag(HatMask::UP);
  348. } else {
  349. hat.set_flag(HatMask::DOWN);
  350. }
  351. }
  352. jevent.hat = hat;
  353. input_handler->process_joy_event(jevent);
  354. }
  355. // Called on the UI thread
  356. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
  357. if (os_android) {
  358. String name = jstring_to_string(p_name, env);
  359. Input::get_singleton()->joy_connection_changed(p_device, p_connected, name);
  360. }
  361. }
  362. // Called on the UI thread
  363. 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) {
  364. if (step.get() <= STEP_SETUP) {
  365. return;
  366. }
  367. input_handler->process_key_event(p_physical_keycode, p_unicode, p_key_label, p_pressed, p_echo);
  368. }
  369. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  370. accelerometer = Vector3(x, y, z);
  371. }
  372. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  373. gravity = Vector3(x, y, z);
  374. }
  375. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  376. magnetometer = Vector3(x, y, z);
  377. }
  378. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  379. gyroscope = Vector3(x, y, z);
  380. }
  381. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
  382. if (step.get() <= STEP_SETUP) {
  383. return;
  384. }
  385. os_android->main_loop_focusin();
  386. }
  387. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
  388. if (step.get() <= STEP_SETUP) {
  389. return;
  390. }
  391. os_android->main_loop_focusout();
  392. }
  393. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
  394. String js = jstring_to_string(path, env);
  395. return env->NewStringUTF(GLOBAL_GET(js).operator String().utf8().get_data());
  396. }
  397. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getEditorSetting(JNIEnv *env, jclass clazz, jstring p_setting_key) {
  398. String editor_setting = "";
  399. #ifdef TOOLS_ENABLED
  400. String godot_setting_key = jstring_to_string(p_setting_key, env);
  401. editor_setting = EDITOR_GET(godot_setting_key).operator String();
  402. #else
  403. WARN_PRINT("Access to the Editor Settings in only available on Editor builds");
  404. #endif
  405. return env->NewStringUTF(editor_setting.utf8().get_data());
  406. }
  407. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  408. Object *obj = ObjectDB::get_instance(ObjectID(ID));
  409. ERR_FAIL_NULL(obj);
  410. String str_method = jstring_to_string(method, env);
  411. int count = env->GetArrayLength(params);
  412. Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
  413. const Variant **vptr = (const Variant **)alloca(sizeof(Variant *) * count);
  414. for (int i = 0; i < count; i++) {
  415. jobject jobj = env->GetObjectArrayElement(params, i);
  416. ERR_FAIL_NULL(jobj);
  417. memnew_placement(&vlist[i], Variant(_jobject_to_variant(env, jobj)));
  418. vptr[i] = &vlist[i];
  419. env->DeleteLocalRef(jobj);
  420. }
  421. Callable::CallError err;
  422. obj->callp(str_method, vptr, count, err);
  423. }
  424. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  425. Object *obj = ObjectDB::get_instance(ObjectID(ID));
  426. ERR_FAIL_NULL(obj);
  427. String str_method = jstring_to_string(method, env);
  428. int count = env->GetArrayLength(params);
  429. Variant *args = (Variant *)alloca(sizeof(Variant) * count);
  430. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * count);
  431. for (int i = 0; i < count; i++) {
  432. jobject jobj = env->GetObjectArrayElement(params, i);
  433. ERR_FAIL_NULL(jobj);
  434. memnew_placement(&args[i], Variant(_jobject_to_variant(env, jobj)));
  435. argptrs[i] = &args[i];
  436. env->DeleteLocalRef(jobj);
  437. }
  438. Callable(obj, str_method).call_deferredp(argptrs, count);
  439. }
  440. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onNightModeChanged(JNIEnv *env, jclass clazz) {
  441. DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton();
  442. if (ds) {
  443. ds->emit_system_theme_changed();
  444. }
  445. }
  446. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
  447. String permission = jstring_to_string(p_permission, env);
  448. if (permission == "android.permission.RECORD_AUDIO" && p_result) {
  449. AudioDriver::get_singleton()->input_start();
  450. }
  451. if (os_android->get_main_loop()) {
  452. os_android->get_main_loop()->emit_signal(SNAME("on_request_permissions_result"), permission, p_result == JNI_TRUE);
  453. }
  454. }
  455. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
  456. if (step.get() <= STEP_SETUP) {
  457. return;
  458. }
  459. // We force redraw to ensure we render at least once when resuming the app.
  460. Main::force_redraw();
  461. if (os_android->get_main_loop()) {
  462. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  463. }
  464. }
  465. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
  466. if (step.get() <= STEP_SETUP) {
  467. return;
  468. }
  469. if (os_android->get_main_loop()) {
  470. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  471. }
  472. }
  473. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz) {
  474. Input *input = Input::get_singleton();
  475. if (input) {
  476. return !input->is_agile_input_event_flushing();
  477. }
  478. return false;
  479. }
  480. }