2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_android.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2021-01-02 03:13:46 +08:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "os_android.h"
|
2015-01-11 04:35:26 +08:00
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "drivers/unix/dir_access_unix.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "drivers/unix/file_access_unix.h"
|
|
|
|
#include "main/main.h"
|
2020-03-28 00:30:18 +08:00
|
|
|
#include "platform/android/display_server_android.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
#include "dir_access_jandroid.h"
|
2020-12-07 02:39:45 +08:00
|
|
|
#include "file_access_android.h"
|
2019-11-25 22:01:44 +08:00
|
|
|
#include "net_socket_android.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-01-13 21:40:20 +08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2019-03-13 20:51:55 +08:00
|
|
|
#include "java_godot_io_wrapper.h"
|
|
|
|
#include "java_godot_wrapper.h"
|
|
|
|
|
2017-09-22 13:56:02 +08:00
|
|
|
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() {}
|
|
|
|
};
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void OS_Android::initialize_core() {
|
|
|
|
OS_Unix::initialize_core();
|
|
|
|
|
2014-06-28 10:21:45 +08:00
|
|
|
if (use_apk_expansion)
|
|
|
|
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
|
2015-09-04 10:24:55 +08:00
|
|
|
else {
|
|
|
|
FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
|
|
|
|
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
|
2014-06-28 10:21:45 +08:00
|
|
|
if (use_apk_expansion)
|
|
|
|
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
|
|
|
|
else
|
|
|
|
DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
|
2014-02-10 09:10:30 +08:00
|
|
|
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
|
|
|
|
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
|
2019-11-25 22:01:44 +08:00
|
|
|
|
|
|
|
NetSocketAndroid::make_default();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
void OS_Android::initialize() {
|
|
|
|
initialize_core();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2018-07-20 14:37:10 +08:00
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
void OS_Android::initialize_joypads() {
|
2020-04-28 21:19:37 +08:00
|
|
|
Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
// This queries/updates the currently connected devices/joypads.
|
|
|
|
godot_java->init_input_devices();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void OS_Android::set_main_loop(MainLoop *p_main_loop) {
|
|
|
|
main_loop = p_main_loop;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Android::delete_main_loop() {
|
2020-03-28 00:30:18 +08:00
|
|
|
if (main_loop) {
|
|
|
|
memdelete(main_loop);
|
|
|
|
main_loop = nullptr;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Android::finalize() {
|
2020-03-28 00:30:18 +08:00
|
|
|
}
|
2017-08-20 22:17:24 +08:00
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
OS_Android *OS_Android::get_singleton() {
|
|
|
|
return (OS_Android *)OS::get_singleton();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:55 +08:00
|
|
|
GodotJavaWrapper *OS_Android::get_godot_java() {
|
|
|
|
return godot_java;
|
|
|
|
}
|
|
|
|
|
|
|
|
GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
|
|
|
|
return godot_io_java;
|
|
|
|
}
|
|
|
|
|
2019-03-05 10:06:37 +08:00
|
|
|
bool OS_Android::request_permission(const String &p_name) {
|
2019-03-13 20:51:55 +08:00
|
|
|
return godot_java->request_permission(p_name);
|
2019-03-05 10:06:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 02:17:44 +08:00
|
|
|
bool OS_Android::request_permissions() {
|
|
|
|
return godot_java->request_permissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> OS_Android::get_granted_permissions() const {
|
|
|
|
return godot_java->get_granted_permissions();
|
|
|
|
}
|
|
|
|
|
2018-01-13 21:40:20 +08:00
|
|
|
Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
|
|
|
|
p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
|
2019-08-09 12:49:33 +08:00
|
|
|
ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
|
2018-01-13 21:40:20 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-05-21 01:36:24 +08:00
|
|
|
String OS_Android::get_name() const {
|
2014-02-10 09:10:30 +08:00
|
|
|
return "Android";
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop *OS_Android::get_main_loop() const {
|
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Android::main_loop_begin() {
|
|
|
|
if (main_loop)
|
2020-12-22 17:50:29 +08:00
|
|
|
main_loop->initialize();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
bool OS_Android::main_loop_iterate() {
|
|
|
|
if (!main_loop)
|
|
|
|
return false;
|
2020-10-08 07:37:58 +08:00
|
|
|
DisplayServerAndroid::get_singleton()->process_events();
|
2014-02-10 09:10:30 +08:00
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Android::main_loop_end() {
|
|
|
|
if (main_loop)
|
2020-12-22 17:50:29 +08:00
|
|
|
main_loop->finalize();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Android::main_loop_focusout() {
|
2020-03-28 00:30:18 +08:00
|
|
|
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
|
2014-02-10 09:10:30 +08:00
|
|
|
audio_driver_android.set_pause(true);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void OS_Android::main_loop_focusin() {
|
2020-03-28 00:30:18 +08:00
|
|
|
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
|
2014-02-10 09:10:30 +08:00
|
|
|
audio_driver_android.set_pause(false);
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:34:32 +08:00
|
|
|
void OS_Android::main_loop_request_go_back() {
|
2020-03-28 00:30:18 +08:00
|
|
|
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error OS_Android::shell_open(String p_uri) {
|
2019-03-13 20:51:55 +08:00
|
|
|
return godot_io_java->open_uri(p_uri);
|
2016-07-21 18:07:01 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
String OS_Android::get_resource_dir() const {
|
2018-10-25 08:19:21 +08:00
|
|
|
return "/"; //android has its own filesystem for resources inside the APK
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String OS_Android::get_locale() const {
|
2019-03-13 20:51:55 +08:00
|
|
|
String locale = godot_io_java->get_locale();
|
|
|
|
if (locale != "") {
|
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
return OS_Unix::get_locale();
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_Android::get_model_name() const {
|
2019-03-13 20:51:55 +08:00
|
|
|
String model = godot_io_java->get_model();
|
|
|
|
if (model != "")
|
|
|
|
return model;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
return OS_Unix::get_model_name();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 22:25:22 +08:00
|
|
|
String OS_Android::get_user_data_dir() const {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (data_dir_cache != String())
|
2016-07-02 22:48:02 +08:00
|
|
|
return data_dir_cache;
|
|
|
|
|
2019-03-13 20:51:55 +08:00
|
|
|
String data_dir = godot_io_java->get_user_data_dir();
|
|
|
|
if (data_dir != "") {
|
2016-07-02 22:48:02 +08:00
|
|
|
//store current dir
|
|
|
|
char real_current_dir_name[2048];
|
2017-03-05 23:44:50 +08:00
|
|
|
getcwd(real_current_dir_name, 2048);
|
2016-07-02 22:48:02 +08:00
|
|
|
|
|
|
|
//go to data dir
|
|
|
|
chdir(data_dir.utf8().get_data());
|
|
|
|
|
|
|
|
//get actual data dir, so we resolve potential symlink (Android 6.0+ seems to use symlink)
|
|
|
|
char data_current_dir_name[2048];
|
2017-03-05 23:44:50 +08:00
|
|
|
getcwd(data_current_dir_name, 2048);
|
2016-07-02 22:48:02 +08:00
|
|
|
|
|
|
|
//cache by parsing utf8
|
|
|
|
data_dir_cache.parse_utf8(data_current_dir_name);
|
|
|
|
|
|
|
|
//restore original dir so we don't mess things up
|
|
|
|
chdir(real_current_dir_name);
|
|
|
|
|
2016-07-03 03:04:00 +08:00
|
|
|
return data_dir_cache;
|
2016-07-02 22:48:02 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
return ".";
|
2016-07-21 18:07:01 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-08-07 18:17:31 +08:00
|
|
|
String OS_Android::get_unique_id() const {
|
2019-03-13 20:51:55 +08:00
|
|
|
String unique_id = godot_io_java->get_unique_id();
|
|
|
|
if (unique_id != "")
|
|
|
|
return unique_id;
|
|
|
|
|
2017-08-07 18:17:31 +08:00
|
|
|
return OS::get_unique_id();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2014-12-03 01:02:41 +08:00
|
|
|
String OS_Android::get_system_dir(SystemDir p_dir) const {
|
2019-03-13 20:51:55 +08:00
|
|
|
return godot_io_java->get_system_dir(p_dir);
|
2014-12-03 01:02:41 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
void OS_Android::set_display_size(const Size2i &p_size) {
|
|
|
|
display_size = p_size;
|
|
|
|
}
|
2019-03-13 20:51:55 +08:00
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
Size2i OS_Android::get_display_size() const {
|
|
|
|
return display_size;
|
2014-03-14 09:57:24 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2015-12-02 22:15:48 +08:00
|
|
|
void OS_Android::set_context_is_16_bits(bool p_is_16) {
|
2020-03-28 00:30:18 +08:00
|
|
|
#if defined(OPENGL_ENABLED)
|
2017-03-26 21:59:13 +08:00
|
|
|
//use_16bits_fbo = p_is_16;
|
|
|
|
//if (rasterizer)
|
|
|
|
// rasterizer->set_force_16_bits_fbo(p_is_16);
|
2020-03-28 00:30:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
|
|
|
|
#if defined(OPENGL_ENABLED)
|
|
|
|
ERR_FAIL_COND(!p_gl_extensions);
|
|
|
|
gl_extensions = p_gl_extensions;
|
|
|
|
#endif
|
2015-12-02 22:15:48 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 02:00:28 +08:00
|
|
|
void OS_Android::set_native_window(ANativeWindow *p_native_window) {
|
|
|
|
#if defined(VULKAN_ENABLED)
|
|
|
|
native_window = p_native_window;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-28 00:30:18 +08:00
|
|
|
ANativeWindow *OS_Android::get_native_window() const {
|
|
|
|
#if defined(VULKAN_ENABLED)
|
|
|
|
return native_window;
|
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2016-01-24 12:11:59 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 23:27:29 +08:00
|
|
|
void OS_Android::vibrate_handheld(int p_duration_ms) {
|
|
|
|
godot_java->vibrate(p_duration_ms);
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:00:46 +08:00
|
|
|
bool OS_Android::_check_internal_feature_support(const String &p_feature) {
|
2019-02-26 22:58:47 +08:00
|
|
|
if (p_feature == "mobile") {
|
2017-10-02 23:01:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#if defined(__aarch64__)
|
|
|
|
if (p_feature == "arm64-v8a") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#elif defined(__ARM_ARCH_7A__)
|
|
|
|
if (p_feature == "armeabi-v7a" || p_feature == "armeabi") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#elif defined(__arm__)
|
|
|
|
if (p_feature == "armeabi") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
2017-07-20 04:00:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:55 +08:00
|
|
|
OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
|
2020-03-28 00:30:18 +08:00
|
|
|
display_size.width = 800;
|
|
|
|
display_size.height = 600;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
use_apk_expansion = p_use_apk_expansion;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
main_loop = nullptr;
|
2020-03-28 00:30:18 +08:00
|
|
|
|
|
|
|
#if defined(OPENGL_ENABLED)
|
2020-04-02 07:20:12 +08:00
|
|
|
gl_extensions = nullptr;
|
2017-03-05 23:44:50 +08:00
|
|
|
use_gl2 = false;
|
2020-11-24 17:12:55 +08:00
|
|
|
use_16bits_fbo = false;
|
2020-03-28 00:30:18 +08:00
|
|
|
#endif
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-06 02:00:28 +08:00
|
|
|
#if defined(VULKAN_ENABLED)
|
2020-03-28 00:30:18 +08:00
|
|
|
native_window = nullptr;
|
2020-03-06 02:00:28 +08:00
|
|
|
#endif
|
|
|
|
|
2019-03-13 20:51:55 +08:00
|
|
|
godot_java = p_godot_java;
|
|
|
|
godot_io_java = p_godot_io_java;
|
2017-09-22 13:56:02 +08:00
|
|
|
|
2017-11-21 17:35:01 +08:00
|
|
|
Vector<Logger *> loggers;
|
|
|
|
loggers.push_back(memnew(AndroidLogger));
|
|
|
|
_set_logger(memnew(CompositeLogger(loggers)));
|
2018-03-05 01:18:05 +08:00
|
|
|
|
|
|
|
AudioDriverManager::add_driver(&audio_driver_android);
|
2020-03-28 00:30:18 +08:00
|
|
|
|
|
|
|
DisplayServerAndroid::register_android_driver();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OS_Android::~OS_Android() {
|
|
|
|
}
|