os_android.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /**************************************************************************/
  2. /* os_android.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 "os_android.h"
  31. #include "dir_access_jandroid.h"
  32. #include "display_server_android.h"
  33. #include "file_access_android.h"
  34. #include "file_access_filesystem_jandroid.h"
  35. #include "java_godot_io_wrapper.h"
  36. #include "java_godot_wrapper.h"
  37. #include "net_socket_android.h"
  38. #include "core/config/project_settings.h"
  39. #include "core/extension/gdextension_manager.h"
  40. #include "core/io/xml_parser.h"
  41. #include "drivers/unix/dir_access_unix.h"
  42. #include "drivers/unix/file_access_unix.h"
  43. #ifdef TOOLS_ENABLED
  44. #include "editor/editor_node.h"
  45. #include "editor/plugins/game_view_plugin.h"
  46. #endif
  47. #include "main/main.h"
  48. #include "scene/main/scene_tree.h"
  49. #include "servers/rendering_server.h"
  50. #include <dlfcn.h>
  51. #include <sys/system_properties.h>
  52. const char *OS_Android::ANDROID_EXEC_PATH = "apk";
  53. String _remove_symlink(const String &dir) {
  54. // Workaround for Android 6.0+ using a symlink.
  55. // Save the current directory.
  56. char current_dir_name[2048];
  57. getcwd(current_dir_name, 2048);
  58. // Change directory to the external data directory.
  59. chdir(dir.utf8().get_data());
  60. // Get the actual directory without the potential symlink.
  61. char dir_name_without_symlink[2048];
  62. getcwd(dir_name_without_symlink, 2048);
  63. // Convert back to a String.
  64. String dir_without_symlink(dir_name_without_symlink);
  65. // Restore original current directory.
  66. chdir(current_dir_name);
  67. return dir_without_symlink;
  68. }
  69. class AndroidLogger : public Logger {
  70. public:
  71. virtual void logv(const char *p_format, va_list p_list, bool p_err) {
  72. __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  73. }
  74. virtual ~AndroidLogger() {}
  75. };
  76. void OS_Android::alert(const String &p_alert, const String &p_title) {
  77. ERR_FAIL_NULL(godot_java);
  78. godot_java->alert(p_alert, p_title);
  79. }
  80. void OS_Android::initialize_core() {
  81. OS_Unix::initialize_core();
  82. #ifdef TOOLS_ENABLED
  83. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  84. #else
  85. if (use_apk_expansion) {
  86. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  87. } else {
  88. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  89. }
  90. #endif
  91. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  92. FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_FILESYSTEM);
  93. #ifdef TOOLS_ENABLED
  94. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  95. #else
  96. if (use_apk_expansion) {
  97. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  98. } else {
  99. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  100. }
  101. #endif
  102. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  103. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_FILESYSTEM);
  104. NetSocketAndroid::make_default();
  105. }
  106. void OS_Android::initialize() {
  107. initialize_core();
  108. }
  109. void OS_Android::initialize_joypads() {
  110. Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  111. // This queries/updates the currently connected devices/joypads.
  112. godot_java->init_input_devices();
  113. }
  114. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  115. main_loop = p_main_loop;
  116. }
  117. void OS_Android::delete_main_loop() {
  118. if (main_loop) {
  119. memdelete(main_loop);
  120. main_loop = nullptr;
  121. }
  122. }
  123. void OS_Android::finalize() {
  124. }
  125. OS_Android *OS_Android::get_singleton() {
  126. return static_cast<OS_Android *>(OS::get_singleton());
  127. }
  128. GodotJavaWrapper *OS_Android::get_godot_java() {
  129. return godot_java;
  130. }
  131. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  132. return godot_io_java;
  133. }
  134. bool OS_Android::request_permission(const String &p_name) {
  135. return godot_java->request_permission(p_name);
  136. }
  137. bool OS_Android::request_permissions() {
  138. return godot_java->request_permissions();
  139. }
  140. Vector<String> OS_Android::get_granted_permissions() const {
  141. return godot_java->get_granted_permissions();
  142. }
  143. bool OS_Android::copy_dynamic_library(const String &p_library_path, const String &p_target_dir, String *r_copy_path) {
  144. if (!FileAccess::exists(p_library_path)) {
  145. return false;
  146. }
  147. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_library_path);
  148. if (da_ref.is_null()) {
  149. return false;
  150. }
  151. String copy_path = p_target_dir.path_join(p_library_path.get_file());
  152. bool copy_exists = FileAccess::exists(copy_path);
  153. if (copy_exists) {
  154. print_verbose("Deleting existing library copy " + copy_path);
  155. if (da_ref->remove(copy_path) != OK) {
  156. print_verbose("Unable to delete " + copy_path);
  157. }
  158. }
  159. print_verbose("Copying " + p_library_path + " to " + p_target_dir);
  160. Error create_dir_result = da_ref->make_dir_recursive(p_target_dir);
  161. if (create_dir_result == OK || create_dir_result == ERR_ALREADY_EXISTS) {
  162. copy_exists = da_ref->copy(p_library_path, copy_path) == OK;
  163. }
  164. if (copy_exists && r_copy_path != nullptr) {
  165. *r_copy_path = copy_path;
  166. }
  167. return copy_exists;
  168. }
  169. Error OS_Android::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
  170. String path = p_path;
  171. bool so_file_exists = true;
  172. if (!FileAccess::exists(path)) {
  173. path = p_path.get_file();
  174. so_file_exists = false;
  175. }
  176. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  177. if (!p_library_handle && so_file_exists) {
  178. // The library (and its dependencies) may be on the sdcard and thus inaccessible.
  179. // Try to copy to the internal directory for access.
  180. const String dynamic_library_path = get_dynamic_libraries_path();
  181. if (p_data != nullptr && p_data->library_dependencies != nullptr && !p_data->library_dependencies->is_empty()) {
  182. // Copy the library dependencies
  183. print_verbose("Copying library dependencies..");
  184. for (const String &library_dependency_path : *p_data->library_dependencies) {
  185. String internal_library_dependency_path;
  186. if (!copy_dynamic_library(library_dependency_path, dynamic_library_path.path_join(library_dependency_path.get_base_dir()), &internal_library_dependency_path)) {
  187. ERR_PRINT(vformat("Unable to copy library dependency %s", library_dependency_path));
  188. } else {
  189. void *lib_dependency_handle = dlopen(internal_library_dependency_path.utf8().get_data(), RTLD_NOW);
  190. if (!lib_dependency_handle) {
  191. ERR_PRINT(vformat("Can't open dynamic library dependency: %s. Error: %s.", internal_library_dependency_path, dlerror()));
  192. }
  193. }
  194. }
  195. }
  196. String internal_path;
  197. print_verbose("Copying library " + p_path);
  198. const bool internal_so_file_exists = copy_dynamic_library(p_path, dynamic_library_path.path_join(p_path.get_base_dir()), &internal_path);
  199. if (internal_so_file_exists) {
  200. print_verbose("Opening library " + internal_path);
  201. p_library_handle = dlopen(internal_path.utf8().get_data(), RTLD_NOW);
  202. if (p_library_handle) {
  203. path = internal_path;
  204. }
  205. }
  206. }
  207. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
  208. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  209. *p_data->r_resolved_path = path;
  210. }
  211. return OK;
  212. }
  213. String OS_Android::get_name() const {
  214. return "Android";
  215. }
  216. String OS_Android::get_system_property(const char *key) const {
  217. String value;
  218. char value_str[PROP_VALUE_MAX];
  219. if (__system_property_get(key, value_str)) {
  220. value = String(value_str);
  221. }
  222. return value;
  223. }
  224. String OS_Android::get_distribution_name() const {
  225. if (!get_system_property("ro.havoc.version").is_empty()) {
  226. return "Havoc OS";
  227. } else if (!get_system_property("org.pex.version").is_empty()) { // Putting before "Pixel Experience", because it's derivating from it.
  228. return "Pixel Extended";
  229. } else if (!get_system_property("org.pixelexperience.version").is_empty()) {
  230. return "Pixel Experience";
  231. } else if (!get_system_property("ro.potato.version").is_empty()) {
  232. return "POSP";
  233. } else if (!get_system_property("ro.xtended.version").is_empty()) {
  234. return "Project-Xtended";
  235. } else if (!get_system_property("org.evolution.version").is_empty()) {
  236. return "Evolution X";
  237. } else if (!get_system_property("ro.corvus.version").is_empty()) {
  238. return "Corvus-Q";
  239. } else if (!get_system_property("ro.pa.version").is_empty()) {
  240. return "Paranoid Android";
  241. } else if (!get_system_property("ro.crdroid.version").is_empty()) {
  242. return "crDroid Android";
  243. } else if (!get_system_property("ro.syberia.version").is_empty()) {
  244. return "Syberia Project";
  245. } else if (!get_system_property("ro.arrow.version").is_empty()) {
  246. return "ArrowOS";
  247. } else if (!get_system_property("ro.lineage.version").is_empty()) { // Putting LineageOS last, just in case any derivative writes to "ro.lineage.version".
  248. return "LineageOS";
  249. }
  250. if (!get_system_property("ro.modversion").is_empty()) { // Handles other Android custom ROMs.
  251. return vformat("%s %s", get_name(), "Custom ROM");
  252. }
  253. // Handles stock Android.
  254. return get_name();
  255. }
  256. String OS_Android::get_version() const {
  257. const Vector<const char *> roms = { "ro.havoc.version", "org.pex.version", "org.pixelexperience.version",
  258. "ro.potato.version", "ro.xtended.version", "org.evolution.version", "ro.corvus.version", "ro.pa.version",
  259. "ro.crdroid.version", "ro.syberia.version", "ro.arrow.version", "ro.lineage.version" };
  260. for (int i = 0; i < roms.size(); i++) {
  261. String rom_version = get_system_property(roms[i]);
  262. if (!rom_version.is_empty()) {
  263. return rom_version;
  264. }
  265. }
  266. String mod_version = get_system_property("ro.modversion"); // Handles other Android custom ROMs.
  267. if (!mod_version.is_empty()) {
  268. return mod_version;
  269. }
  270. // Handles stock Android.
  271. String sdk_version = get_system_property("ro.build.version.sdk");
  272. String build = get_system_property("ro.build.version.incremental");
  273. if (!sdk_version.is_empty()) {
  274. if (!build.is_empty()) {
  275. return vformat("%s.%s", sdk_version, build);
  276. }
  277. return sdk_version;
  278. }
  279. return "";
  280. }
  281. String OS_Android::get_version_alias() const {
  282. String release = get_system_property("ro.build.version.release_or_codename");
  283. String sdk_version = get_system_property("ro.build.version.sdk");
  284. String build = get_system_property("ro.build.version.incremental");
  285. return vformat("%s (SDK %s build %s)", release, sdk_version, build);
  286. }
  287. MainLoop *OS_Android::get_main_loop() const {
  288. return main_loop;
  289. }
  290. void OS_Android::main_loop_begin() {
  291. if (main_loop) {
  292. main_loop->initialize();
  293. }
  294. #ifdef TOOLS_ENABLED
  295. if (Engine::get_singleton()->is_editor_hint()) {
  296. GameViewPlugin *game_view_plugin = Object::cast_to<GameViewPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_plugin_by_name("Game"));
  297. if (game_view_plugin != nullptr) {
  298. game_view_plugin->connect("main_screen_changed", callable_mp_static(&OS_Android::_on_main_screen_changed));
  299. }
  300. }
  301. #endif
  302. }
  303. bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
  304. if (!main_loop) {
  305. return false;
  306. }
  307. DisplayServerAndroid::get_singleton()->reset_swap_buffers_flag();
  308. DisplayServerAndroid::get_singleton()->process_events();
  309. uint64_t current_frames_drawn = Engine::get_singleton()->get_frames_drawn();
  310. bool exit = Main::iteration();
  311. if (r_should_swap_buffers) {
  312. *r_should_swap_buffers = !is_in_low_processor_usage_mode() ||
  313. DisplayServerAndroid::get_singleton()->should_swap_buffers() ||
  314. RenderingServer::get_singleton()->has_changed() ||
  315. current_frames_drawn != Engine::get_singleton()->get_frames_drawn();
  316. }
  317. return exit;
  318. }
  319. void OS_Android::main_loop_end() {
  320. #ifdef TOOLS_ENABLED
  321. if (Engine::get_singleton()->is_editor_hint()) {
  322. GameViewPlugin *game_view_plugin = Object::cast_to<GameViewPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_plugin_by_name("Game"));
  323. if (game_view_plugin != nullptr) {
  324. game_view_plugin->disconnect("main_screen_changed", callable_mp_static(&OS_Android::_on_main_screen_changed));
  325. }
  326. }
  327. #endif
  328. if (main_loop) {
  329. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  330. if (scene_tree) {
  331. scene_tree->quit();
  332. }
  333. main_loop->finalize();
  334. }
  335. }
  336. #ifdef TOOLS_ENABLED
  337. void OS_Android::_on_main_screen_changed(const String &p_screen_name) {
  338. if (OS_Android::get_singleton() != nullptr && OS_Android::get_singleton()->get_godot_java() != nullptr) {
  339. OS_Android::get_singleton()->get_godot_java()->on_editor_workspace_selected(p_screen_name);
  340. }
  341. }
  342. #endif
  343. void OS_Android::main_loop_focusout() {
  344. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  345. if (OS::get_singleton()->get_main_loop()) {
  346. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  347. }
  348. audio_driver_android.set_pause(true);
  349. }
  350. void OS_Android::main_loop_focusin() {
  351. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  352. if (OS::get_singleton()->get_main_loop()) {
  353. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  354. }
  355. audio_driver_android.set_pause(false);
  356. }
  357. Error OS_Android::shell_open(const String &p_uri) {
  358. return godot_io_java->open_uri(p_uri);
  359. }
  360. String OS_Android::get_resource_dir() const {
  361. #ifdef TOOLS_ENABLED
  362. return OS_Unix::get_resource_dir();
  363. #else
  364. if (remote_fs_dir.is_empty()) {
  365. return "/"; // Android has its own filesystem for resources inside the APK
  366. } else {
  367. return remote_fs_dir;
  368. }
  369. #endif
  370. }
  371. String OS_Android::get_locale() const {
  372. String locale = godot_io_java->get_locale();
  373. if (!locale.is_empty()) {
  374. return locale;
  375. }
  376. return OS_Unix::get_locale();
  377. }
  378. String OS_Android::get_model_name() const {
  379. String model = godot_io_java->get_model();
  380. if (!model.is_empty()) {
  381. return model;
  382. }
  383. return OS_Unix::get_model_name();
  384. }
  385. String OS_Android::get_data_path() const {
  386. return OS::get_user_data_dir();
  387. }
  388. void OS_Android::_load_system_font_config() const {
  389. font_aliases.clear();
  390. fonts.clear();
  391. font_names.clear();
  392. Ref<XMLParser> parser;
  393. parser.instantiate();
  394. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  395. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  396. Error err = parser->open(String(getenv("ANDROID_ROOT")).path_join("/etc/fonts.xml"));
  397. if (err == OK) {
  398. bool in_font_node = false;
  399. String fb, fn;
  400. FontInfo fi;
  401. while (parser->read() == OK) {
  402. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  403. in_font_node = false;
  404. if (parser->get_node_name() == "familyset") {
  405. int ver = parser->has_attribute("version") ? parser->get_named_attribute_value("version").to_int() : 0;
  406. if (ver < 21) {
  407. ERR_PRINT(vformat("Unsupported font config version %s", ver));
  408. break;
  409. }
  410. } else if (parser->get_node_name() == "alias") {
  411. String name = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
  412. String to = parser->has_attribute("to") ? parser->get_named_attribute_value("to").strip_edges() : String();
  413. if (!name.is_empty() && !to.is_empty()) {
  414. font_aliases[name] = to;
  415. }
  416. } else if (parser->get_node_name() == "family") {
  417. fn = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
  418. String lang_code = parser->has_attribute("lang") ? parser->get_named_attribute_value("lang").strip_edges() : String();
  419. Vector<String> lang_codes = lang_code.split(",");
  420. for (int i = 0; i < lang_codes.size(); i++) {
  421. Vector<String> lang_code_elements = lang_codes[i].split("-");
  422. if (lang_code_elements.size() >= 1 && lang_code_elements[0] != "und") {
  423. // Add missing script codes.
  424. if (lang_code_elements[0] == "ko") {
  425. fi.script.insert("Hani");
  426. fi.script.insert("Hang");
  427. }
  428. if (lang_code_elements[0] == "ja") {
  429. fi.script.insert("Hani");
  430. fi.script.insert("Kana");
  431. fi.script.insert("Hira");
  432. }
  433. if (!lang_code_elements[0].is_empty()) {
  434. fi.lang.insert(lang_code_elements[0]);
  435. }
  436. }
  437. if (lang_code_elements.size() >= 2) {
  438. // Add common codes for variants and remove variants not supported by HarfBuzz/ICU.
  439. if (lang_code_elements[1] == "Aran") {
  440. fi.script.insert("Arab");
  441. }
  442. if (lang_code_elements[1] == "Cyrs") {
  443. fi.script.insert("Cyrl");
  444. }
  445. if (lang_code_elements[1] == "Hanb") {
  446. fi.script.insert("Hani");
  447. fi.script.insert("Bopo");
  448. }
  449. if (lang_code_elements[1] == "Hans" || lang_code_elements[1] == "Hant") {
  450. fi.script.insert("Hani");
  451. }
  452. if (lang_code_elements[1] == "Syrj" || lang_code_elements[1] == "Syre" || lang_code_elements[1] == "Syrn") {
  453. fi.script.insert("Syrc");
  454. }
  455. if (!lang_code_elements[1].is_empty() && lang_code_elements[1] != "Zsym" && lang_code_elements[1] != "Zsye" && lang_code_elements[1] != "Zmth") {
  456. fi.script.insert(lang_code_elements[1]);
  457. }
  458. }
  459. }
  460. } else if (parser->get_node_name() == "font") {
  461. in_font_node = true;
  462. fb = parser->has_attribute("fallbackFor") ? parser->get_named_attribute_value("fallbackFor").strip_edges() : String();
  463. fi.weight = parser->has_attribute("weight") ? parser->get_named_attribute_value("weight").to_int() : 400;
  464. fi.italic = parser->has_attribute("style") && parser->get_named_attribute_value("style").strip_edges() == "italic";
  465. }
  466. }
  467. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  468. if (in_font_node) {
  469. fi.filename = parser->get_node_data().strip_edges();
  470. fi.font_name = fn;
  471. if (da->file_exists(root.path_join(fi.filename))) {
  472. if (!fb.is_empty() && fn.is_empty()) {
  473. fi.font_name = fb;
  474. fi.priority = 2;
  475. }
  476. if (fi.font_name.is_empty()) {
  477. fi.font_name = "sans-serif";
  478. fi.priority = 5;
  479. }
  480. if (fi.font_name.ends_with("-condensed")) {
  481. fi.stretch = 75;
  482. fi.font_name = fi.font_name.trim_suffix("-condensed");
  483. }
  484. fonts.push_back(fi);
  485. font_names.insert(fi.font_name);
  486. }
  487. }
  488. }
  489. if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) {
  490. in_font_node = false;
  491. if (parser->get_node_name() == "font") {
  492. fb = String();
  493. fi.font_name = String();
  494. fi.priority = 0;
  495. fi.weight = 400;
  496. fi.stretch = 100;
  497. fi.italic = false;
  498. } else if (parser->get_node_name() == "family") {
  499. fi = FontInfo();
  500. fn = String();
  501. }
  502. }
  503. }
  504. parser->close();
  505. } else {
  506. ERR_PRINT("Unable to load font config");
  507. }
  508. font_config_loaded = true;
  509. }
  510. Vector<String> OS_Android::get_system_fonts() const {
  511. if (!font_config_loaded) {
  512. _load_system_font_config();
  513. }
  514. Vector<String> ret;
  515. for (const String &E : font_names) {
  516. ret.push_back(E);
  517. }
  518. return ret;
  519. }
  520. Vector<String> OS_Android::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const {
  521. if (!font_config_loaded) {
  522. _load_system_font_config();
  523. }
  524. String font_name = p_font_name.to_lower();
  525. if (font_aliases.has(font_name)) {
  526. font_name = font_aliases[font_name];
  527. }
  528. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  529. String lang_prefix = p_locale.split("_")[0];
  530. Vector<String> ret;
  531. int best_score = 0;
  532. for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
  533. int score = 0;
  534. if (!E->get().script.is_empty() && !p_script.is_empty() && !E->get().script.has(p_script)) {
  535. continue;
  536. }
  537. float sim = E->get().font_name.similarity(font_name);
  538. if (sim > 0.0) {
  539. score += (60 * sim + 5 - E->get().priority);
  540. }
  541. if (E->get().lang.has(p_locale)) {
  542. score += 120;
  543. } else if (E->get().lang.has(lang_prefix)) {
  544. score += 115;
  545. }
  546. if (E->get().script.has(p_script)) {
  547. score += 240;
  548. }
  549. score += (20 - Math::abs(E->get().weight - p_weight) / 50);
  550. score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
  551. if (E->get().italic == p_italic) {
  552. score += 30;
  553. }
  554. if (score > best_score) {
  555. best_score = score;
  556. if (!ret.has(root.path_join(E->get().filename))) {
  557. ret.insert(0, root.path_join(E->get().filename));
  558. }
  559. } else if (score == best_score || E->get().script.is_empty()) {
  560. if (!ret.has(root.path_join(E->get().filename))) {
  561. ret.push_back(root.path_join(E->get().filename));
  562. }
  563. }
  564. if (score >= 490) {
  565. break; // Perfect match.
  566. }
  567. }
  568. return ret;
  569. }
  570. String OS_Android::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  571. if (!font_config_loaded) {
  572. _load_system_font_config();
  573. }
  574. String font_name = p_font_name.to_lower();
  575. if (font_aliases.has(font_name)) {
  576. font_name = font_aliases[font_name];
  577. }
  578. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  579. int best_score = 0;
  580. const List<FontInfo>::Element *best_match = nullptr;
  581. for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
  582. int score = 0;
  583. if (E->get().font_name == font_name) {
  584. score += (65 - E->get().priority);
  585. }
  586. score += (20 - Math::abs(E->get().weight - p_weight) / 50);
  587. score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
  588. if (E->get().italic == p_italic) {
  589. score += 30;
  590. }
  591. if (score >= 60 && score > best_score) {
  592. best_score = score;
  593. best_match = E;
  594. }
  595. if (score >= 140) {
  596. break; // Perfect match.
  597. }
  598. }
  599. if (best_match) {
  600. return root.path_join(best_match->get().filename);
  601. }
  602. return String();
  603. }
  604. String OS_Android::get_executable_path() const {
  605. // Since unix process creation is restricted on Android, we bypass
  606. // OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
  607. // Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
  608. // manner.
  609. return OS::get_executable_path();
  610. }
  611. String OS_Android::get_user_data_dir(const String &p_user_dir) const {
  612. if (!data_dir_cache.is_empty()) {
  613. return data_dir_cache;
  614. }
  615. String data_dir = godot_io_java->get_user_data_dir(p_user_dir);
  616. if (!data_dir.is_empty()) {
  617. data_dir_cache = _remove_symlink(data_dir);
  618. return data_dir_cache;
  619. }
  620. return ".";
  621. }
  622. String OS_Android::get_dynamic_libraries_path() const {
  623. return get_cache_path().path_join("dynamic_libraries");
  624. }
  625. String OS_Android::get_cache_path() const {
  626. if (!cache_dir_cache.is_empty()) {
  627. return cache_dir_cache;
  628. }
  629. String cache_dir = godot_io_java->get_cache_dir();
  630. if (!cache_dir.is_empty()) {
  631. cache_dir_cache = _remove_symlink(cache_dir);
  632. return cache_dir_cache;
  633. }
  634. return ".";
  635. }
  636. String OS_Android::get_temp_path() const {
  637. if (!temp_dir_cache.is_empty()) {
  638. return temp_dir_cache;
  639. }
  640. String temp_dir = godot_io_java->get_temp_dir();
  641. if (!temp_dir.is_empty()) {
  642. temp_dir_cache = _remove_symlink(temp_dir);
  643. return temp_dir_cache;
  644. }
  645. return ".";
  646. }
  647. String OS_Android::get_unique_id() const {
  648. String unique_id = godot_io_java->get_unique_id();
  649. if (!unique_id.is_empty()) {
  650. return unique_id;
  651. }
  652. return OS::get_unique_id();
  653. }
  654. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  655. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  656. }
  657. Error OS_Android::move_to_trash(const String &p_path) {
  658. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
  659. if (da_ref.is_null()) {
  660. return FAILED;
  661. }
  662. // Check if it's a directory
  663. if (da_ref->dir_exists(p_path)) {
  664. Error err = da_ref->change_dir(p_path);
  665. if (err) {
  666. return err;
  667. }
  668. // This is directory, let's erase its contents
  669. err = da_ref->erase_contents_recursive();
  670. if (err) {
  671. return err;
  672. }
  673. // Remove the top directory
  674. return da_ref->remove(p_path);
  675. } else if (da_ref->file_exists(p_path)) {
  676. // This is a file, let's remove it.
  677. return da_ref->remove(p_path);
  678. } else {
  679. return FAILED;
  680. }
  681. }
  682. void OS_Android::set_display_size(const Size2i &p_size) {
  683. display_size = p_size;
  684. }
  685. Size2i OS_Android::get_display_size() const {
  686. return display_size;
  687. }
  688. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  689. #if defined(GLES3_ENABLED)
  690. ERR_FAIL_NULL(p_gl_extensions);
  691. gl_extensions = p_gl_extensions;
  692. #endif
  693. }
  694. void OS_Android::set_native_window(ANativeWindow *p_native_window) {
  695. #if defined(VULKAN_ENABLED)
  696. native_window = p_native_window;
  697. #endif
  698. }
  699. ANativeWindow *OS_Android::get_native_window() const {
  700. #if defined(VULKAN_ENABLED)
  701. return native_window;
  702. #else
  703. return nullptr;
  704. #endif
  705. }
  706. void OS_Android::vibrate_handheld(int p_duration_ms, float p_amplitude) {
  707. godot_java->vibrate(p_duration_ms, p_amplitude);
  708. }
  709. String OS_Android::get_config_path() const {
  710. return OS::get_user_data_dir().path_join("config");
  711. }
  712. void OS_Android::benchmark_begin_measure(const String &p_context, const String &p_what) {
  713. #ifdef TOOLS_ENABLED
  714. godot_java->begin_benchmark_measure(p_context, p_what);
  715. #endif
  716. }
  717. void OS_Android::benchmark_end_measure(const String &p_context, const String &p_what) {
  718. #ifdef TOOLS_ENABLED
  719. godot_java->end_benchmark_measure(p_context, p_what);
  720. #endif
  721. }
  722. void OS_Android::benchmark_dump() {
  723. #ifdef TOOLS_ENABLED
  724. if (!is_use_benchmark_set()) {
  725. return;
  726. }
  727. godot_java->dump_benchmark(get_benchmark_file());
  728. #endif
  729. }
  730. #ifdef TOOLS_ENABLED
  731. Error OS_Android::sign_apk(const String &p_input_path, const String &p_output_path, const String &p_keystore_path, const String &p_keystore_user, const String &p_keystore_password) {
  732. return godot_java->sign_apk(p_input_path, p_output_path, p_keystore_path, p_keystore_user, p_keystore_password);
  733. }
  734. Error OS_Android::verify_apk(const String &p_apk_path) {
  735. return godot_java->verify_apk(p_apk_path);
  736. }
  737. #endif
  738. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  739. if (p_feature == "macos" || p_feature == "web_ios" || p_feature == "web_macos" || p_feature == "windows") {
  740. return false;
  741. }
  742. if (p_feature == "system_fonts") {
  743. return true;
  744. }
  745. if (p_feature == "mobile") {
  746. return true;
  747. }
  748. #if defined(__aarch64__)
  749. if (p_feature == "arm64-v8a" || p_feature == "arm64") {
  750. return true;
  751. }
  752. #elif defined(__ARM_ARCH_7A__)
  753. if (p_feature == "armeabi-v7a" || p_feature == "armeabi" || p_feature == "arm32") {
  754. return true;
  755. }
  756. #elif defined(__arm__)
  757. if (p_feature == "armeabi" || p_feature == "arm") {
  758. return true;
  759. }
  760. #endif
  761. if (godot_java->has_feature(p_feature)) {
  762. return true;
  763. }
  764. return false;
  765. }
  766. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  767. display_size.width = DEFAULT_WINDOW_WIDTH;
  768. display_size.height = DEFAULT_WINDOW_HEIGHT;
  769. use_apk_expansion = p_use_apk_expansion;
  770. main_loop = nullptr;
  771. #if defined(GLES3_ENABLED)
  772. gl_extensions = nullptr;
  773. #endif
  774. #if defined(VULKAN_ENABLED)
  775. native_window = nullptr;
  776. #endif
  777. godot_java = p_godot_java;
  778. godot_io_java = p_godot_io_java;
  779. Vector<Logger *> loggers;
  780. loggers.push_back(memnew(AndroidLogger));
  781. _set_logger(memnew(CompositeLogger(loggers)));
  782. AudioDriverManager::add_driver(&audio_driver_android);
  783. DisplayServerAndroid::register_android_driver();
  784. }
  785. 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) {
  786. if (p_path == ANDROID_EXEC_PATH) {
  787. return create_instance(p_arguments);
  788. } else {
  789. return OS_Unix::execute(p_path, p_arguments, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
  790. }
  791. }
  792. Error OS_Android::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
  793. if (p_path == ANDROID_EXEC_PATH) {
  794. return create_instance(p_arguments, r_child_id);
  795. } else {
  796. return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console);
  797. }
  798. }
  799. Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
  800. int instance_id = godot_java->create_new_godot_instance(p_arguments);
  801. if (instance_id == -1) {
  802. return FAILED;
  803. }
  804. if (r_child_id) {
  805. *r_child_id = instance_id;
  806. }
  807. return OK;
  808. }
  809. Error OS_Android::kill(const ProcessID &p_pid) {
  810. if (godot_java->force_quit(nullptr, p_pid)) {
  811. return OK;
  812. }
  813. return OS_Unix::kill(p_pid);
  814. }
  815. String OS_Android::get_system_ca_certificates() {
  816. return godot_java->get_ca_certificates();
  817. }
  818. Error OS_Android::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
  819. r_project_path = OS::get_user_data_dir();
  820. Error err = OS_Unix::setup_remote_filesystem(p_server_host, p_port, p_password, r_project_path);
  821. if (err == OK) {
  822. remote_fs_dir = r_project_path;
  823. FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_RESOURCES);
  824. }
  825. return err;
  826. }
  827. void OS_Android::load_platform_gdextensions() const {
  828. Vector<String> extension_list_config_file = godot_java->get_gdextension_list_config_file();
  829. for (String config_file_path : extension_list_config_file) {
  830. GDExtensionManager::LoadStatus err = GDExtensionManager::get_singleton()->load_extension(config_file_path);
  831. ERR_CONTINUE_MSG(err == GDExtensionManager::LOAD_STATUS_FAILED, "Error loading platform extension: " + config_file_path);
  832. }
  833. }
  834. OS_Android::~OS_Android() {
  835. }