os_android.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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/config/project_settings.h"
  32. #include "drivers/unix/dir_access_unix.h"
  33. #include "drivers/unix/file_access_unix.h"
  34. #include "main/main.h"
  35. #include "platform/android/display_server_android.h"
  36. #include "scene/main/scene_tree.h"
  37. #include "servers/rendering_server.h"
  38. #include "dir_access_jandroid.h"
  39. #include "file_access_android.h"
  40. #include "file_access_filesystem_jandroid.h"
  41. #include "net_socket_android.h"
  42. #include <dlfcn.h>
  43. #include <sys/system_properties.h>
  44. #include "java_godot_io_wrapper.h"
  45. #include "java_godot_wrapper.h"
  46. const char *OS_Android::ANDROID_EXEC_PATH = "apk";
  47. String _remove_symlink(const String &dir) {
  48. // Workaround for Android 6.0+ using a symlink.
  49. // Save the current directory.
  50. char current_dir_name[2048];
  51. getcwd(current_dir_name, 2048);
  52. // Change directory to the external data directory.
  53. chdir(dir.utf8().get_data());
  54. // Get the actual directory without the potential symlink.
  55. char dir_name_wihout_symlink[2048];
  56. getcwd(dir_name_wihout_symlink, 2048);
  57. // Convert back to a String.
  58. String dir_without_symlink(dir_name_wihout_symlink);
  59. // Restore original current directory.
  60. chdir(current_dir_name);
  61. return dir_without_symlink;
  62. }
  63. class AndroidLogger : public Logger {
  64. public:
  65. virtual void logv(const char *p_format, va_list p_list, bool p_err) {
  66. __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  67. }
  68. virtual ~AndroidLogger() {}
  69. };
  70. void OS_Android::alert(const String &p_alert, const String &p_title) {
  71. ERR_FAIL_NULL(godot_java);
  72. godot_java->alert(p_alert, p_title);
  73. }
  74. void OS_Android::initialize_core() {
  75. OS_Unix::initialize_core();
  76. #ifdef TOOLS_ENABLED
  77. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  78. #else
  79. if (use_apk_expansion) {
  80. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  81. } else {
  82. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  83. }
  84. #endif
  85. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  86. FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_FILESYSTEM);
  87. #ifdef TOOLS_ENABLED
  88. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  89. #else
  90. if (use_apk_expansion) {
  91. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  92. } else {
  93. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  94. }
  95. #endif
  96. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  97. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_FILESYSTEM);
  98. NetSocketAndroid::make_default();
  99. }
  100. void OS_Android::initialize() {
  101. initialize_core();
  102. }
  103. void OS_Android::initialize_joypads() {
  104. Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  105. // This queries/updates the currently connected devices/joypads.
  106. godot_java->init_input_devices();
  107. }
  108. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  109. main_loop = p_main_loop;
  110. }
  111. void OS_Android::delete_main_loop() {
  112. if (main_loop) {
  113. memdelete(main_loop);
  114. main_loop = nullptr;
  115. }
  116. }
  117. void OS_Android::finalize() {
  118. }
  119. OS_Android *OS_Android::get_singleton() {
  120. return static_cast<OS_Android *>(OS::get_singleton());
  121. }
  122. GodotJavaWrapper *OS_Android::get_godot_java() {
  123. return godot_java;
  124. }
  125. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  126. return godot_io_java;
  127. }
  128. bool OS_Android::request_permission(const String &p_name) {
  129. return godot_java->request_permission(p_name);
  130. }
  131. bool OS_Android::request_permissions() {
  132. return godot_java->request_permissions();
  133. }
  134. Vector<String> OS_Android::get_granted_permissions() const {
  135. return godot_java->get_granted_permissions();
  136. }
  137. Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path) {
  138. p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
  139. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
  140. if (r_resolved_path != nullptr) {
  141. *r_resolved_path = p_path;
  142. }
  143. return OK;
  144. }
  145. String OS_Android::get_name() const {
  146. return "Android";
  147. }
  148. String OS_Android::get_system_property(const char *key) const {
  149. static String value;
  150. char value_str[PROP_VALUE_MAX];
  151. if (__system_property_get(key, value_str)) {
  152. value = String(value_str);
  153. }
  154. return value;
  155. }
  156. String OS_Android::get_distribution_name() const {
  157. if (!get_system_property("ro.havoc.version").is_empty()) {
  158. return "Havoc OS";
  159. } else if (!get_system_property("org.pex.version").is_empty()) { // Putting before "Pixel Experience", because it's derivating from it.
  160. return "Pixel Extended";
  161. } else if (!get_system_property("org.pixelexperience.version").is_empty()) {
  162. return "Pixel Experience";
  163. } else if (!get_system_property("ro.potato.version").is_empty()) {
  164. return "POSP";
  165. } else if (!get_system_property("ro.xtended.version").is_empty()) {
  166. return "Project-Xtended";
  167. } else if (!get_system_property("org.evolution.version").is_empty()) {
  168. return "Evolution X";
  169. } else if (!get_system_property("ro.corvus.version").is_empty()) {
  170. return "Corvus-Q";
  171. } else if (!get_system_property("ro.pa.version").is_empty()) {
  172. return "Paranoid Android";
  173. } else if (!get_system_property("ro.crdroid.version").is_empty()) {
  174. return "crDroid Android";
  175. } else if (!get_system_property("ro.syberia.version").is_empty()) {
  176. return "Syberia Project";
  177. } else if (!get_system_property("ro.arrow.version").is_empty()) {
  178. return "ArrowOS";
  179. } else if (!get_system_property("ro.lineage.version").is_empty()) { // Putting LineageOS last, just in case any derivative writes to "ro.lineage.version".
  180. return "LineageOS";
  181. }
  182. if (!get_system_property("ro.modversion").is_empty()) { // Handles other Android custom ROMs.
  183. return vformat("%s %s", get_name(), "Custom ROM");
  184. }
  185. // Handles stock Android.
  186. return get_name();
  187. }
  188. String OS_Android::get_version() const {
  189. const Vector<const char *> roms = { "ro.havoc.version", "org.pex.version", "org.pixelexperience.version",
  190. "ro.potato.version", "ro.xtended.version", "org.evolution.version", "ro.corvus.version", "ro.pa.version",
  191. "ro.crdroid.version", "ro.syberia.version", "ro.arrow.version", "ro.lineage.version" };
  192. for (int i = 0; i < roms.size(); i++) {
  193. static String rom_version = get_system_property(roms[i]);
  194. if (!rom_version.is_empty()) {
  195. return rom_version;
  196. }
  197. }
  198. static String mod_version = get_system_property("ro.modversion"); // Handles other Android custom ROMs.
  199. if (!mod_version.is_empty()) {
  200. return mod_version;
  201. }
  202. // Handles stock Android.
  203. static String sdk_version = get_system_property("ro.build.version.sdk_int");
  204. static String build = get_system_property("ro.build.version.incremental");
  205. if (!sdk_version.is_empty()) {
  206. if (!build.is_empty()) {
  207. return vformat("%s.%s", sdk_version, build);
  208. }
  209. return sdk_version;
  210. }
  211. return "";
  212. }
  213. MainLoop *OS_Android::get_main_loop() const {
  214. return main_loop;
  215. }
  216. void OS_Android::main_loop_begin() {
  217. if (main_loop) {
  218. main_loop->initialize();
  219. }
  220. }
  221. bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
  222. if (!main_loop) {
  223. return false;
  224. }
  225. DisplayServerAndroid::get_singleton()->process_events();
  226. uint64_t current_frames_drawn = Engine::get_singleton()->get_frames_drawn();
  227. bool exit = Main::iteration();
  228. if (r_should_swap_buffers) {
  229. *r_should_swap_buffers = !is_in_low_processor_usage_mode() || RenderingServer::get_singleton()->has_changed() || current_frames_drawn != Engine::get_singleton()->get_frames_drawn();
  230. }
  231. return exit;
  232. }
  233. void OS_Android::main_loop_end() {
  234. if (main_loop) {
  235. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  236. if (scene_tree) {
  237. scene_tree->quit();
  238. }
  239. main_loop->finalize();
  240. }
  241. }
  242. void OS_Android::main_loop_focusout() {
  243. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  244. audio_driver_android.set_pause(true);
  245. }
  246. void OS_Android::main_loop_focusin() {
  247. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  248. audio_driver_android.set_pause(false);
  249. }
  250. Error OS_Android::shell_open(String p_uri) {
  251. return godot_io_java->open_uri(p_uri);
  252. }
  253. String OS_Android::get_resource_dir() const {
  254. #ifdef TOOLS_ENABLED
  255. return OS_Unix::get_resource_dir();
  256. #else
  257. return "/"; //android has its own filesystem for resources inside the APK
  258. #endif
  259. }
  260. String OS_Android::get_locale() const {
  261. String locale = godot_io_java->get_locale();
  262. if (!locale.is_empty()) {
  263. return locale;
  264. }
  265. return OS_Unix::get_locale();
  266. }
  267. String OS_Android::get_model_name() const {
  268. String model = godot_io_java->get_model();
  269. if (!model.is_empty()) {
  270. return model;
  271. }
  272. return OS_Unix::get_model_name();
  273. }
  274. String OS_Android::get_data_path() const {
  275. return get_user_data_dir();
  276. }
  277. String OS_Android::get_executable_path() const {
  278. // Since unix process creation is restricted on Android, we bypass
  279. // OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
  280. // Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
  281. // manner.
  282. return OS::get_executable_path();
  283. }
  284. String OS_Android::get_user_data_dir() const {
  285. if (!data_dir_cache.is_empty()) {
  286. return data_dir_cache;
  287. }
  288. String data_dir = godot_io_java->get_user_data_dir();
  289. if (!data_dir.is_empty()) {
  290. data_dir_cache = _remove_symlink(data_dir);
  291. return data_dir_cache;
  292. }
  293. return ".";
  294. }
  295. String OS_Android::get_cache_path() const {
  296. if (!cache_dir_cache.is_empty()) {
  297. return cache_dir_cache;
  298. }
  299. String cache_dir = godot_io_java->get_cache_dir();
  300. if (!cache_dir.is_empty()) {
  301. cache_dir_cache = _remove_symlink(cache_dir);
  302. return cache_dir_cache;
  303. }
  304. return ".";
  305. }
  306. String OS_Android::get_unique_id() const {
  307. String unique_id = godot_io_java->get_unique_id();
  308. if (!unique_id.is_empty()) {
  309. return unique_id;
  310. }
  311. return OS::get_unique_id();
  312. }
  313. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  314. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  315. }
  316. Error OS_Android::move_to_trash(const String &p_path) {
  317. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
  318. if (da_ref.is_null()) {
  319. return FAILED;
  320. }
  321. // Check if it's a directory
  322. if (da_ref->dir_exists(p_path)) {
  323. Error err = da_ref->change_dir(p_path);
  324. if (err) {
  325. return err;
  326. }
  327. // This is directory, let's erase its contents
  328. err = da_ref->erase_contents_recursive();
  329. if (err) {
  330. return err;
  331. }
  332. // Remove the top directory
  333. return da_ref->remove(p_path);
  334. } else if (da_ref->file_exists(p_path)) {
  335. // This is a file, let's remove it.
  336. return da_ref->remove(p_path);
  337. } else {
  338. return FAILED;
  339. }
  340. }
  341. void OS_Android::set_display_size(const Size2i &p_size) {
  342. display_size = p_size;
  343. }
  344. Size2i OS_Android::get_display_size() const {
  345. return display_size;
  346. }
  347. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  348. #if defined(GLES3_ENABLED)
  349. ERR_FAIL_NULL(p_gl_extensions);
  350. gl_extensions = p_gl_extensions;
  351. #endif
  352. }
  353. void OS_Android::set_native_window(ANativeWindow *p_native_window) {
  354. #if defined(VULKAN_ENABLED)
  355. native_window = p_native_window;
  356. #endif
  357. }
  358. ANativeWindow *OS_Android::get_native_window() const {
  359. #if defined(VULKAN_ENABLED)
  360. return native_window;
  361. #else
  362. return nullptr;
  363. #endif
  364. }
  365. void OS_Android::vibrate_handheld(int p_duration_ms) {
  366. godot_java->vibrate(p_duration_ms);
  367. }
  368. String OS_Android::get_config_path() const {
  369. return get_user_data_dir().path_join("config");
  370. }
  371. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  372. if (p_feature == "mobile") {
  373. return true;
  374. }
  375. #if defined(__aarch64__)
  376. if (p_feature == "arm64-v8a" || p_feature == "arm64") {
  377. return true;
  378. }
  379. #elif defined(__ARM_ARCH_7A__)
  380. if (p_feature == "armeabi-v7a" || p_feature == "armeabi" || p_feature == "arm32") {
  381. return true;
  382. }
  383. #elif defined(__arm__)
  384. if (p_feature == "armeabi" || p_feature == "arm") {
  385. return true;
  386. }
  387. #endif
  388. return false;
  389. }
  390. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  391. display_size.width = 800;
  392. display_size.height = 600;
  393. use_apk_expansion = p_use_apk_expansion;
  394. main_loop = nullptr;
  395. #if defined(GLES3_ENABLED)
  396. gl_extensions = nullptr;
  397. use_gl2 = false;
  398. #endif
  399. #if defined(VULKAN_ENABLED)
  400. native_window = nullptr;
  401. #endif
  402. godot_java = p_godot_java;
  403. godot_io_java = p_godot_io_java;
  404. Vector<Logger *> loggers;
  405. loggers.push_back(memnew(AndroidLogger));
  406. _set_logger(memnew(CompositeLogger(loggers)));
  407. AudioDriverManager::add_driver(&audio_driver_android);
  408. DisplayServerAndroid::register_android_driver();
  409. }
  410. Error OS_Android::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
  411. if (p_path == ANDROID_EXEC_PATH) {
  412. return create_instance(p_arguments);
  413. } else {
  414. return OS_Unix::execute(p_path, p_arguments, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
  415. }
  416. }
  417. Error OS_Android::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
  418. if (p_path == ANDROID_EXEC_PATH) {
  419. return create_instance(p_arguments, r_child_id);
  420. } else {
  421. return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console);
  422. }
  423. }
  424. Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
  425. godot_java->create_new_godot_instance(p_arguments);
  426. return OK;
  427. }
  428. OS_Android::~OS_Android() {
  429. }