os_android.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*************************************************************************/
  2. /* os_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "os_android.h"
  31. #include "core/project_settings.h"
  32. #include "drivers/gles2/rasterizer_gles2.h"
  33. #include "drivers/gles3/rasterizer_gles3.h"
  34. #include "drivers/unix/dir_access_unix.h"
  35. #include "drivers/unix/file_access_unix.h"
  36. #include "main/main.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "servers/visual/visual_server_raster.h"
  39. #include "servers/visual/visual_server_wrap_mt.h"
  40. #include "dir_access_jandroid.h"
  41. #include "file_access_android.h"
  42. #include "net_socket_android.h"
  43. #include <android/input.h>
  44. #include <core/os/keyboard.h>
  45. #include <dlfcn.h>
  46. #include "java_godot_io_wrapper.h"
  47. #include "java_godot_wrapper.h"
  48. const char *OS_Android::ANDROID_EXEC_PATH = "apk";
  49. String _remove_symlink(const String &dir) {
  50. // Workaround for Android 6.0+ using a symlink.
  51. // Save the current directory.
  52. char current_dir_name[2048];
  53. getcwd(current_dir_name, 2048);
  54. // Change directory to the external data directory.
  55. chdir(dir.utf8().get_data());
  56. // Get the actual directory without the potential symlink.
  57. char dir_name_wihout_symlink[2048];
  58. getcwd(dir_name_wihout_symlink, 2048);
  59. // Convert back to a String.
  60. String dir_without_symlink(dir_name_wihout_symlink);
  61. // Restore original current directory.
  62. chdir(current_dir_name);
  63. return dir_without_symlink;
  64. }
  65. class AndroidLogger : public Logger {
  66. public:
  67. virtual void logv(const char *p_format, va_list p_list, bool p_err) {
  68. __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  69. }
  70. virtual ~AndroidLogger() {}
  71. };
  72. int OS_Android::get_video_driver_count() const {
  73. return 2;
  74. }
  75. const char *OS_Android::get_video_driver_name(int p_driver) const {
  76. switch (p_driver) {
  77. case VIDEO_DRIVER_GLES3:
  78. return "GLES3";
  79. case VIDEO_DRIVER_GLES2:
  80. return "GLES2";
  81. }
  82. ERR_FAIL_V_MSG(NULL, "Invalid video driver index: " + itos(p_driver) + ".");
  83. }
  84. int OS_Android::get_audio_driver_count() const {
  85. return 1;
  86. }
  87. const char *OS_Android::get_audio_driver_name(int p_driver) const {
  88. return "Android";
  89. }
  90. void OS_Android::initialize_core() {
  91. OS_Unix::initialize_core();
  92. #ifdef TOOLS_ENABLED
  93. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  94. #else
  95. if (use_apk_expansion)
  96. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  97. else {
  98. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  99. }
  100. #endif
  101. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  102. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  103. #ifdef TOOLS_ENABLED
  104. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  105. #else
  106. if (use_apk_expansion)
  107. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  108. else
  109. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  110. #endif
  111. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  112. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  113. NetSocketAndroid::make_default();
  114. }
  115. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  116. ERR_FAIL_COND(!p_gl_extensions);
  117. gl_extensions = p_gl_extensions;
  118. }
  119. int OS_Android::get_current_video_driver() const {
  120. return video_driver_index;
  121. }
  122. Error OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  123. bool use_gl3 = godot_java->get_gles_version_code() >= 0x00030000;
  124. use_gl3 = use_gl3 && (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES3");
  125. bool gl_initialization_error = false;
  126. while (true) {
  127. if (use_gl3) {
  128. if (RasterizerGLES3::is_viable() == OK) {
  129. godot_java->gfx_init(false);
  130. RasterizerGLES3::register_config();
  131. RasterizerGLES3::make_current();
  132. break;
  133. } else {
  134. if (GLOBAL_GET("rendering/quality/driver/fallback_to_gles2")) {
  135. p_video_driver = VIDEO_DRIVER_GLES2;
  136. use_gl3 = false;
  137. continue;
  138. } else {
  139. gl_initialization_error = true;
  140. break;
  141. }
  142. }
  143. } else {
  144. if (RasterizerGLES2::is_viable() == OK) {
  145. godot_java->gfx_init(true);
  146. RasterizerGLES2::register_config();
  147. RasterizerGLES2::make_current();
  148. break;
  149. } else {
  150. gl_initialization_error = true;
  151. break;
  152. }
  153. }
  154. }
  155. transparency_enabled = p_desired.layered;
  156. if (gl_initialization_error) {
  157. OS::get_singleton()->alert("Your device does not support any of the supported OpenGL versions.\n"
  158. "Please try updating your Android version.",
  159. "Unable to initialize Video driver");
  160. return ERR_UNAVAILABLE;
  161. }
  162. video_driver_index = p_video_driver;
  163. visual_server = memnew(VisualServerRaster);
  164. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  165. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  166. }
  167. visual_server->init();
  168. AudioDriverManager::initialize(p_audio_driver);
  169. input = memnew(InputDefault);
  170. input->set_use_input_buffering(true); // Needed because events will come directly from the UI thread
  171. input->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  172. //power_manager = memnew(PowerAndroid);
  173. return OK;
  174. }
  175. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  176. main_loop = p_main_loop;
  177. input->set_main_loop(p_main_loop);
  178. }
  179. void OS_Android::delete_main_loop() {
  180. memdelete(main_loop);
  181. }
  182. void OS_Android::finalize() {
  183. memdelete(input);
  184. }
  185. GodotJavaWrapper *OS_Android::get_godot_java() {
  186. return godot_java;
  187. }
  188. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  189. return godot_io_java;
  190. }
  191. void OS_Android::alert(const String &p_alert, const String &p_title) {
  192. //print("ALERT: %s\n", p_alert.utf8().get_data());
  193. godot_java->alert(p_alert, p_title);
  194. }
  195. bool OS_Android::request_permission(const String &p_name) {
  196. return godot_java->request_permission(p_name);
  197. }
  198. bool OS_Android::request_permissions() {
  199. return godot_java->request_permissions();
  200. }
  201. Vector<String> OS_Android::get_granted_permissions() const {
  202. return godot_java->get_granted_permissions();
  203. }
  204. Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  205. p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
  206. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
  207. return OK;
  208. }
  209. void OS_Android::set_mouse_show(bool p_show) {
  210. //android has no mouse...
  211. }
  212. void OS_Android::set_mouse_grab(bool p_grab) {
  213. //it really has no mouse...!
  214. }
  215. bool OS_Android::is_mouse_grab_enabled() const {
  216. //*sigh* technology has evolved so much since i was a kid..
  217. return false;
  218. }
  219. Point2 OS_Android::get_mouse_position() const {
  220. return input->get_mouse_position();
  221. }
  222. int OS_Android::get_mouse_button_state() const {
  223. return input->get_mouse_button_mask();
  224. }
  225. void OS_Android::set_window_title(const String &p_title) {
  226. //This queries/updates the currently connected devices/joypads
  227. //Set_window_title is called when initializing the main loop (main.cpp)
  228. //therefore this place is found to be suitable (I found no better).
  229. godot_java->init_input_devices();
  230. }
  231. void OS_Android::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  232. }
  233. OS::VideoMode OS_Android::get_video_mode(int p_screen) const {
  234. return default_videomode;
  235. }
  236. void OS_Android::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  237. p_list->push_back(default_videomode);
  238. }
  239. void OS_Android::set_keep_screen_on(bool p_enabled) {
  240. OS::set_keep_screen_on(p_enabled);
  241. godot_java->set_keep_screen_on(p_enabled);
  242. }
  243. Size2 OS_Android::get_window_size() const {
  244. return Vector2(default_videomode.width, default_videomode.height);
  245. }
  246. Rect2 OS_Android::get_window_safe_area() const {
  247. int xywh[4];
  248. godot_io_java->get_window_safe_area(xywh);
  249. return Rect2(xywh[0], xywh[1], xywh[2], xywh[3]);
  250. }
  251. String OS_Android::get_name() const {
  252. return "Android";
  253. }
  254. MainLoop *OS_Android::get_main_loop() const {
  255. return main_loop;
  256. }
  257. bool OS_Android::can_draw() const {
  258. return true; //always?
  259. }
  260. void OS_Android::main_loop_begin() {
  261. if (main_loop)
  262. main_loop->init();
  263. }
  264. bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
  265. if (!main_loop)
  266. return false;
  267. bool exit = Main::iteration();
  268. if (r_should_swap_buffers) {
  269. *r_should_swap_buffers = !is_in_low_processor_usage_mode() || _update_pending;
  270. }
  271. return exit;
  272. }
  273. void OS_Android::main_loop_end() {
  274. if (main_loop) {
  275. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  276. if (scene_tree) {
  277. scene_tree->quit();
  278. }
  279. main_loop->finish();
  280. }
  281. }
  282. void OS_Android::main_loop_focusout() {
  283. if (main_loop)
  284. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  285. audio_driver_android.set_pause(true);
  286. }
  287. void OS_Android::main_loop_focusin() {
  288. if (main_loop)
  289. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  290. audio_driver_android.set_pause(false);
  291. }
  292. void OS_Android::process_accelerometer(const Vector3 &p_accelerometer) {
  293. input->set_accelerometer(p_accelerometer);
  294. }
  295. void OS_Android::process_gravity(const Vector3 &p_gravity) {
  296. input->set_gravity(p_gravity);
  297. }
  298. void OS_Android::process_magnetometer(const Vector3 &p_magnetometer) {
  299. input->set_magnetometer(p_magnetometer);
  300. }
  301. void OS_Android::process_gyroscope(const Vector3 &p_gyroscope) {
  302. input->set_gyroscope(p_gyroscope);
  303. }
  304. bool OS_Android::has_touchscreen_ui_hint() const {
  305. return true;
  306. }
  307. bool OS_Android::has_virtual_keyboard() const {
  308. return true;
  309. }
  310. int OS_Android::get_virtual_keyboard_height() const {
  311. return godot_io_java->get_vk_height();
  312. // ERR_PRINT("Cannot obtain virtual keyboard height.");
  313. // return 0;
  314. }
  315. void OS_Android::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  316. if (godot_io_java->has_vk()) {
  317. godot_io_java->show_vk(p_existing_text, p_multiline, p_max_input_length, p_cursor_start, p_cursor_end);
  318. } else {
  319. ERR_PRINT("Virtual keyboard not available");
  320. };
  321. }
  322. void OS_Android::hide_virtual_keyboard() {
  323. if (godot_io_java->has_vk()) {
  324. godot_io_java->hide_vk();
  325. } else {
  326. ERR_PRINT("Virtual keyboard not available");
  327. };
  328. }
  329. void OS_Android::init_video_mode(int p_video_width, int p_video_height) {
  330. default_videomode.width = p_video_width;
  331. default_videomode.height = p_video_height;
  332. default_videomode.fullscreen = true;
  333. default_videomode.resizable = false;
  334. }
  335. void OS_Android::set_display_size(Size2 p_size) {
  336. default_videomode.width = p_size.x;
  337. default_videomode.height = p_size.y;
  338. }
  339. Error OS_Android::shell_open(String p_uri) {
  340. return godot_io_java->open_uri(p_uri);
  341. }
  342. String OS_Android::get_resource_dir() const {
  343. #ifdef TOOLS_ENABLED
  344. return OS_Unix::get_resource_dir();
  345. #else
  346. return "/"; //android has its own filesystem for resources inside the APK
  347. #endif
  348. }
  349. String OS_Android::get_locale() const {
  350. String locale = godot_io_java->get_locale();
  351. if (locale != "") {
  352. return locale;
  353. }
  354. return OS_Unix::get_locale();
  355. }
  356. void OS_Android::set_clipboard(const String &p_text) {
  357. // DO we really need the fallback to OS_Unix here?!
  358. if (godot_java->has_set_clipboard()) {
  359. godot_java->set_clipboard(p_text);
  360. } else {
  361. OS_Unix::set_clipboard(p_text);
  362. }
  363. }
  364. String OS_Android::get_clipboard() const {
  365. // DO we really need the fallback to OS_Unix here?!
  366. if (godot_java->has_get_clipboard()) {
  367. return godot_java->get_clipboard();
  368. }
  369. return OS_Unix::get_clipboard();
  370. }
  371. bool OS_Android::has_clipboard() const {
  372. // DO we really need the fallback to OS_Unix here?!
  373. if (godot_java->has_has_clipboard()) {
  374. return godot_java->has_clipboard();
  375. }
  376. return OS_Unix::has_clipboard();
  377. }
  378. String OS_Android::get_model_name() const {
  379. String model = godot_io_java->get_model();
  380. if (model != "")
  381. return model;
  382. return OS_Unix::get_model_name();
  383. }
  384. int OS_Android::get_screen_dpi(int p_screen) const {
  385. return godot_io_java->get_screen_dpi();
  386. }
  387. float OS_Android::get_screen_refresh_rate(int p_screen) const {
  388. return godot_io_java->get_screen_refresh_rate(OS::get_singleton()->SCREEN_REFRESH_RATE_FALLBACK);
  389. }
  390. String OS_Android::get_data_path() const {
  391. return get_user_data_dir();
  392. }
  393. String OS_Android::get_executable_path() const {
  394. // Since unix process creation is restricted on Android, we bypass
  395. // OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
  396. // Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
  397. // manner.
  398. return OS::get_executable_path();
  399. }
  400. String OS_Android::get_user_data_dir() const {
  401. if (data_dir_cache != String())
  402. return data_dir_cache;
  403. String data_dir = godot_io_java->get_user_data_dir();
  404. if (data_dir != "") {
  405. data_dir_cache = _remove_symlink(data_dir);
  406. return data_dir_cache;
  407. }
  408. return ".";
  409. }
  410. String OS_Android::get_cache_path() const {
  411. if (cache_dir_cache != String())
  412. return cache_dir_cache;
  413. String cache_dir = godot_io_java->get_cache_dir();
  414. if (cache_dir != "") {
  415. cache_dir_cache = _remove_symlink(cache_dir);
  416. return cache_dir_cache;
  417. }
  418. return ".";
  419. }
  420. void OS_Android::set_screen_orientation(ScreenOrientation p_orientation) {
  421. godot_io_java->set_screen_orientation(p_orientation);
  422. }
  423. OS::ScreenOrientation OS_Android::get_screen_orientation() const {
  424. const int orientation = godot_io_java->get_screen_orientation();
  425. ERR_FAIL_INDEX_V_MSG(orientation, 7, OS::ScreenOrientation(0), "Unrecognized screen orientation.");
  426. return OS::ScreenOrientation(orientation);
  427. }
  428. String OS_Android::get_unique_id() const {
  429. String unique_id = godot_io_java->get_unique_id();
  430. if (unique_id != "")
  431. return unique_id;
  432. return OS::get_unique_id();
  433. }
  434. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  435. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  436. }
  437. void OS_Android::set_offscreen_gl_available(bool p_available) {
  438. secondary_gl_available = p_available;
  439. }
  440. bool OS_Android::is_offscreen_gl_available() const {
  441. return secondary_gl_available;
  442. }
  443. void OS_Android::set_offscreen_gl_current(bool p_current) {
  444. if (secondary_gl_available) {
  445. godot_java->set_offscreen_gl_current(nullptr, p_current);
  446. }
  447. }
  448. bool OS_Android::is_joy_known(int p_device) {
  449. return input->is_joy_mapped(p_device);
  450. }
  451. String OS_Android::get_joy_guid(int p_device) const {
  452. return input->get_joy_guid_remapped(p_device);
  453. }
  454. void OS_Android::vibrate_handheld(int p_duration_ms) {
  455. godot_java->vibrate(p_duration_ms);
  456. }
  457. String OS_Android::get_config_path() const {
  458. return get_user_data_dir().plus_file("config");
  459. }
  460. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  461. if (p_feature == "mobile") {
  462. //TODO support etc2 only if GLES3 driver is selected
  463. return true;
  464. }
  465. #if defined(__aarch64__)
  466. if (p_feature == "arm64-v8a") {
  467. return true;
  468. }
  469. #elif defined(__ARM_ARCH_7A__)
  470. if (p_feature == "armeabi-v7a" || p_feature == "armeabi") {
  471. return true;
  472. }
  473. #elif defined(__arm__)
  474. if (p_feature == "armeabi") {
  475. return true;
  476. }
  477. #endif
  478. return false;
  479. }
  480. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  481. use_apk_expansion = p_use_apk_expansion;
  482. default_videomode.width = 800;
  483. default_videomode.height = 600;
  484. default_videomode.fullscreen = true;
  485. default_videomode.resizable = false;
  486. main_loop = NULL;
  487. gl_extensions = NULL;
  488. //rasterizer = NULL;
  489. use_gl2 = false;
  490. godot_java = p_godot_java;
  491. godot_io_java = p_godot_io_java;
  492. Vector<Logger *> loggers;
  493. loggers.push_back(memnew(AndroidLogger));
  494. _set_logger(memnew(CompositeLogger(loggers)));
  495. AudioDriverManager::add_driver(&audio_driver_android);
  496. }
  497. Error OS_Android::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
  498. if (p_path == ANDROID_EXEC_PATH) {
  499. return create_instance(p_arguments, r_child_id);
  500. } else {
  501. return OS_Unix::execute(p_path, p_arguments, p_blocking, r_child_id, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
  502. }
  503. }
  504. Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
  505. godot_java->create_new_godot_instance(p_arguments);
  506. return OK;
  507. }
  508. OS_Android::~OS_Android() {
  509. }