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