virtualx-engine/platform/android/os_android.cpp

519 lines
16 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* os_android.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2014-02-10 02:10:30 +01:00
#include "os_android.h"
#include "core/config/project_settings.h"
2014-02-10 02:10:30 +01:00
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
#include "main/main.h"
2020-03-27 17:30:18 +01:00
#include "platform/android/display_server_android.h"
#include "scene/main/scene_tree.h"
#include "servers/rendering_server.h"
2014-02-10 02:10:30 +01:00
#include "dir_access_jandroid.h"
2020-12-06 19:39:45 +01:00
#include "file_access_android.h"
#include "file_access_filesystem_jandroid.h"
#include "net_socket_android.h"
2014-02-10 02:10:30 +01:00
2018-01-13 14:40:20 +01:00
#include <dlfcn.h>
#include <sys/system_properties.h>
2018-01-13 14:40:20 +01:00
#include "java_godot_io_wrapper.h"
#include "java_godot_wrapper.h"
const char *OS_Android::ANDROID_EXEC_PATH = "apk";
String _remove_symlink(const String &dir) {
// Workaround for Android 6.0+ using a symlink.
// Save the current directory.
char current_dir_name[2048];
getcwd(current_dir_name, 2048);
// Change directory to the external data directory.
chdir(dir.utf8().get_data());
// Get the actual directory without the potential symlink.
char dir_name_wihout_symlink[2048];
getcwd(dir_name_wihout_symlink, 2048);
// Convert back to a String.
String dir_without_symlink(dir_name_wihout_symlink);
// Restore original current directory.
chdir(current_dir_name);
return dir_without_symlink;
}
class AndroidLogger : public Logger {
public:
virtual void logv(const char *p_format, va_list p_list, bool p_err) {
__android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
}
virtual ~AndroidLogger() {}
};
void OS_Android::alert(const String &p_alert, const String &p_title) {
2022-05-30 23:13:49 +02:00
ERR_FAIL_NULL(godot_java);
godot_java->alert(p_alert, p_title);
}
2014-02-10 02:10:30 +01:00
void OS_Android::initialize_core() {
OS_Unix::initialize_core();
#ifdef TOOLS_ENABLED
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
#else
if (use_apk_expansion) {
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
} else {
2015-09-04 04:24:55 +02:00
FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
}
#endif
2014-02-10 02:10:30 +01:00
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_FILESYSTEM);
#ifdef TOOLS_ENABLED
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
#else
if (use_apk_expansion) {
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
} else {
DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
}
#endif
2014-02-10 02:10:30 +01:00
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_FILESYSTEM);
NetSocketAndroid::make_default();
2014-02-10 02:10:30 +01:00
}
2020-03-27 17:30:18 +01:00
void OS_Android::initialize() {
initialize_core();
2014-02-10 02:10:30 +01:00
}
2020-03-27 17:30:18 +01:00
void OS_Android::initialize_joypads() {
Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
2014-02-10 02:10:30 +01:00
2020-03-27 17:30:18 +01:00
// This queries/updates the currently connected devices/joypads.
godot_java->init_input_devices();
2014-02-10 02:10:30 +01:00
}
void OS_Android::set_main_loop(MainLoop *p_main_loop) {
main_loop = p_main_loop;
2014-02-10 02:10:30 +01:00
}
void OS_Android::delete_main_loop() {
2020-03-27 17:30:18 +01:00
if (main_loop) {
memdelete(main_loop);
main_loop = nullptr;
}
2014-02-10 02:10:30 +01:00
}
void OS_Android::finalize() {
2020-03-27 17:30:18 +01:00
}
2020-03-27 17:30:18 +01:00
OS_Android *OS_Android::get_singleton() {
2022-04-07 12:23:40 +02:00
return static_cast<OS_Android *>(OS::get_singleton());
2014-02-10 02:10:30 +01:00
}
GodotJavaWrapper *OS_Android::get_godot_java() {
return godot_java;
}
GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
return godot_io_java;
}
bool OS_Android::request_permission(const String &p_name) {
return godot_java->request_permission(p_name);
}
bool OS_Android::request_permissions() {
return godot_java->request_permissions();
}
Vector<String> OS_Android::get_granted_permissions() const {
return godot_java->get_granted_permissions();
}
Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path) {
String path = p_path;
if (!FileAccess::exists(path)) {
path = p_path.get_file();
}
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
2022-05-30 23:13:49 +02:00
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
if (r_resolved_path != nullptr) {
*r_resolved_path = path;
}
2018-01-13 14:40:20 +01:00
return OK;
}
String OS_Android::get_name() const {
2014-02-10 02:10:30 +01:00
return "Android";
}
String OS_Android::get_system_property(const char *key) const {
static String value;
char value_str[PROP_VALUE_MAX];
if (__system_property_get(key, value_str)) {
value = String(value_str);
}
return value;
}
String OS_Android::get_distribution_name() const {
if (!get_system_property("ro.havoc.version").is_empty()) {
return "Havoc OS";
} else if (!get_system_property("org.pex.version").is_empty()) { // Putting before "Pixel Experience", because it's derivating from it.
return "Pixel Extended";
} else if (!get_system_property("org.pixelexperience.version").is_empty()) {
return "Pixel Experience";
} else if (!get_system_property("ro.potato.version").is_empty()) {
return "POSP";
} else if (!get_system_property("ro.xtended.version").is_empty()) {
return "Project-Xtended";
} else if (!get_system_property("org.evolution.version").is_empty()) {
return "Evolution X";
} else if (!get_system_property("ro.corvus.version").is_empty()) {
return "Corvus-Q";
} else if (!get_system_property("ro.pa.version").is_empty()) {
return "Paranoid Android";
} else if (!get_system_property("ro.crdroid.version").is_empty()) {
return "crDroid Android";
} else if (!get_system_property("ro.syberia.version").is_empty()) {
return "Syberia Project";
} else if (!get_system_property("ro.arrow.version").is_empty()) {
return "ArrowOS";
} else if (!get_system_property("ro.lineage.version").is_empty()) { // Putting LineageOS last, just in case any derivative writes to "ro.lineage.version".
return "LineageOS";
}
if (!get_system_property("ro.modversion").is_empty()) { // Handles other Android custom ROMs.
return vformat("%s %s", get_name(), "Custom ROM");
}
// Handles stock Android.
return get_name();
}
String OS_Android::get_version() const {
const Vector<const char *> roms = { "ro.havoc.version", "org.pex.version", "org.pixelexperience.version",
"ro.potato.version", "ro.xtended.version", "org.evolution.version", "ro.corvus.version", "ro.pa.version",
"ro.crdroid.version", "ro.syberia.version", "ro.arrow.version", "ro.lineage.version" };
for (int i = 0; i < roms.size(); i++) {
static String rom_version = get_system_property(roms[i]);
if (!rom_version.is_empty()) {
return rom_version;
}
}
static String mod_version = get_system_property("ro.modversion"); // Handles other Android custom ROMs.
if (!mod_version.is_empty()) {
return mod_version;
}
// Handles stock Android.
static String sdk_version = get_system_property("ro.build.version.sdk_int");
static String build = get_system_property("ro.build.version.incremental");
if (!sdk_version.is_empty()) {
if (!build.is_empty()) {
return vformat("%s.%s", sdk_version, build);
}
return sdk_version;
}
return "";
}
2014-02-10 02:10:30 +01:00
MainLoop *OS_Android::get_main_loop() const {
return main_loop;
}
void OS_Android::main_loop_begin() {
if (main_loop) {
main_loop->initialize();
}
2014-02-10 02:10:30 +01:00
}
bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
if (!main_loop) {
2014-02-10 02:10:30 +01:00
return false;
}
DisplayServerAndroid::get_singleton()->process_events();
uint64_t current_frames_drawn = Engine::get_singleton()->get_frames_drawn();
bool exit = Main::iteration();
if (r_should_swap_buffers) {
*r_should_swap_buffers = !is_in_low_processor_usage_mode() || RenderingServer::get_singleton()->has_changed() || current_frames_drawn != Engine::get_singleton()->get_frames_drawn();
}
return exit;
2014-02-10 02:10:30 +01:00
}
void OS_Android::main_loop_end() {
if (main_loop) {
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
if (scene_tree) {
scene_tree->quit();
}
main_loop->finalize();
}
2014-02-10 02:10:30 +01:00
}
void OS_Android::main_loop_focusout() {
2020-03-27 17:30:18 +01:00
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
2014-02-10 02:10:30 +01:00
audio_driver_android.set_pause(true);
}
void OS_Android::main_loop_focusin() {
2020-03-27 17:30:18 +01:00
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
2014-02-10 02:10:30 +01:00
audio_driver_android.set_pause(false);
}
Error OS_Android::shell_open(String p_uri) {
return godot_io_java->open_uri(p_uri);
}
2014-02-10 02:10:30 +01:00
String OS_Android::get_resource_dir() const {
#ifdef TOOLS_ENABLED
return OS_Unix::get_resource_dir();
#else
return "/"; //android has its own filesystem for resources inside the APK
#endif
2014-02-10 02:10:30 +01:00
}
String OS_Android::get_locale() const {
String locale = godot_io_java->get_locale();
if (!locale.is_empty()) {
return locale;
}
2014-02-10 02:10:30 +01:00
return OS_Unix::get_locale();
}
String OS_Android::get_model_name() const {
String model = godot_io_java->get_model();
if (!model.is_empty()) {
return model;
}
return OS_Unix::get_model_name();
2014-02-10 02:10:30 +01:00
}
String OS_Android::get_data_path() const {
return get_user_data_dir();
}
String OS_Android::get_executable_path() const {
// Since unix process creation is restricted on Android, we bypass
// OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
// Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
// manner.
return OS::get_executable_path();
}
String OS_Android::get_user_data_dir() const {
if (!data_dir_cache.is_empty()) {
return data_dir_cache;
}
String data_dir = godot_io_java->get_user_data_dir();
if (!data_dir.is_empty()) {
data_dir_cache = _remove_symlink(data_dir);
2016-07-02 21:04:00 +02:00
return data_dir_cache;
}
return ".";
}
String OS_Android::get_cache_path() const {
if (!cache_dir_cache.is_empty()) {
return cache_dir_cache;
}
String cache_dir = godot_io_java->get_cache_dir();
if (!cache_dir.is_empty()) {
cache_dir_cache = _remove_symlink(cache_dir);
return cache_dir_cache;
}
2014-02-10 02:10:30 +01:00
return ".";
}
2014-02-10 02:10:30 +01:00
String OS_Android::get_unique_id() const {
String unique_id = godot_io_java->get_unique_id();
if (!unique_id.is_empty()) {
return unique_id;
}
return OS::get_unique_id();
2014-02-10 02:10:30 +01:00
}
String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
return godot_io_java->get_system_dir(p_dir, p_shared_storage);
}
Error OS_Android::move_to_trash(const String &p_path) {
Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
if (da_ref.is_null()) {
return FAILED;
}
// Check if it's a directory
if (da_ref->dir_exists(p_path)) {
Error err = da_ref->change_dir(p_path);
if (err) {
return err;
}
// This is directory, let's erase its contents
err = da_ref->erase_contents_recursive();
if (err) {
return err;
}
// Remove the top directory
return da_ref->remove(p_path);
} else if (da_ref->file_exists(p_path)) {
// This is a file, let's remove it.
return da_ref->remove(p_path);
} else {
return FAILED;
}
}
2020-03-27 17:30:18 +01:00
void OS_Android::set_display_size(const Size2i &p_size) {
display_size = p_size;
}
2020-03-27 17:30:18 +01:00
Size2i OS_Android::get_display_size() const {
return display_size;
}
2014-02-10 02:10:30 +01:00
2020-03-27 17:30:18 +01:00
void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
#if defined(GLES3_ENABLED)
2022-05-30 23:13:49 +02:00
ERR_FAIL_NULL(p_gl_extensions);
2020-03-27 17:30:18 +01:00
gl_extensions = p_gl_extensions;
#endif
}
2020-03-05 19:00:28 +01:00
void OS_Android::set_native_window(ANativeWindow *p_native_window) {
#if defined(VULKAN_ENABLED)
native_window = p_native_window;
#endif
}
2020-03-27 17:30:18 +01:00
ANativeWindow *OS_Android::get_native_window() const {
#if defined(VULKAN_ENABLED)
return native_window;
#else
return nullptr;
#endif
2016-01-24 05:11:59 +01:00
}
2019-08-17 17:27:29 +02:00
void OS_Android::vibrate_handheld(int p_duration_ms) {
godot_java->vibrate(p_duration_ms);
}
String OS_Android::get_config_path() const {
return get_user_data_dir().path_join("config");
}
bool OS_Android::_check_internal_feature_support(const String &p_feature) {
if (p_feature == "mobile") {
return true;
}
#if defined(__aarch64__)
if (p_feature == "arm64-v8a" || p_feature == "arm64") {
return true;
}
#elif defined(__ARM_ARCH_7A__)
if (p_feature == "armeabi-v7a" || p_feature == "armeabi" || p_feature == "arm32") {
return true;
}
#elif defined(__arm__)
if (p_feature == "armeabi" || p_feature == "arm") {
return true;
}
#endif
return false;
}
OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
2020-03-27 17:30:18 +01:00
display_size.width = 800;
display_size.height = 600;
2014-02-10 02:10:30 +01:00
use_apk_expansion = p_use_apk_expansion;
2014-02-10 02:10:30 +01:00
2020-04-02 01:20:12 +02:00
main_loop = nullptr;
2020-03-27 17:30:18 +01:00
#if defined(GLES3_ENABLED)
2020-04-02 01:20:12 +02:00
gl_extensions = nullptr;
use_gl2 = false;
2020-03-27 17:30:18 +01:00
#endif
2014-02-10 02:10:30 +01:00
2020-03-05 19:00:28 +01:00
#if defined(VULKAN_ENABLED)
2020-03-27 17:30:18 +01:00
native_window = nullptr;
2020-03-05 19:00:28 +01:00
#endif
godot_java = p_godot_java;
godot_io_java = p_godot_io_java;
Vector<Logger *> loggers;
loggers.push_back(memnew(AndroidLogger));
_set_logger(memnew(CompositeLogger(loggers)));
AudioDriverManager::add_driver(&audio_driver_android);
2020-03-27 17:30:18 +01:00
DisplayServerAndroid::register_android_driver();
2014-02-10 02:10:30 +01:00
}
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) {
if (p_path == ANDROID_EXEC_PATH) {
return create_instance(p_arguments);
} else {
return OS_Unix::execute(p_path, p_arguments, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
}
}
Error OS_Android::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
if (p_path == ANDROID_EXEC_PATH) {
return create_instance(p_arguments, r_child_id);
} else {
return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console);
}
}
Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
godot_java->create_new_godot_instance(p_arguments);
return OK;
}
2014-02-10 02:10:30 +01:00
OS_Android::~OS_Android() {
}