os_android.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*************************************************************************/
  2. /* os_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "os_android.h"
  31. #include "core/config/project_settings.h"
  32. #include "drivers/unix/dir_access_unix.h"
  33. #include "drivers/unix/file_access_unix.h"
  34. #include "main/main.h"
  35. #include "platform/android/display_server_android.h"
  36. #include "dir_access_jandroid.h"
  37. #include "file_access_android.h"
  38. #include "net_socket_android.h"
  39. #include <dlfcn.h>
  40. #include "java_godot_io_wrapper.h"
  41. #include "java_godot_wrapper.h"
  42. String _remove_symlink(const String &dir) {
  43. // Workaround for Android 6.0+ using a symlink.
  44. // Save the current directory.
  45. char current_dir_name[2048];
  46. getcwd(current_dir_name, 2048);
  47. // Change directory to the external data directory.
  48. chdir(dir.utf8().get_data());
  49. // Get the actual directory without the potential symlink.
  50. char dir_name_wihout_symlink[2048];
  51. getcwd(dir_name_wihout_symlink, 2048);
  52. // Convert back to a String.
  53. String dir_without_symlink(dir_name_wihout_symlink);
  54. // Restore original current directory.
  55. chdir(current_dir_name);
  56. return dir_without_symlink;
  57. }
  58. class AndroidLogger : public Logger {
  59. public:
  60. virtual void logv(const char *p_format, va_list p_list, bool p_err) {
  61. __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  62. }
  63. virtual ~AndroidLogger() {}
  64. };
  65. void OS_Android::alert(const String &p_alert, const String &p_title) {
  66. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  67. ERR_FAIL_COND(!godot_java);
  68. godot_java->alert(p_alert, p_title);
  69. }
  70. void OS_Android::initialize_core() {
  71. OS_Unix::initialize_core();
  72. if (use_apk_expansion)
  73. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  74. else {
  75. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  76. }
  77. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  78. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  79. if (use_apk_expansion)
  80. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  81. else
  82. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  83. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  84. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  85. NetSocketAndroid::make_default();
  86. }
  87. void OS_Android::initialize() {
  88. initialize_core();
  89. }
  90. void OS_Android::initialize_joypads() {
  91. Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  92. // This queries/updates the currently connected devices/joypads.
  93. godot_java->init_input_devices();
  94. }
  95. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  96. main_loop = p_main_loop;
  97. }
  98. void OS_Android::delete_main_loop() {
  99. if (main_loop) {
  100. memdelete(main_loop);
  101. main_loop = nullptr;
  102. }
  103. }
  104. void OS_Android::finalize() {
  105. }
  106. OS_Android *OS_Android::get_singleton() {
  107. return (OS_Android *)OS::get_singleton();
  108. }
  109. GodotJavaWrapper *OS_Android::get_godot_java() {
  110. return godot_java;
  111. }
  112. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  113. return godot_io_java;
  114. }
  115. bool OS_Android::request_permission(const String &p_name) {
  116. return godot_java->request_permission(p_name);
  117. }
  118. bool OS_Android::request_permissions() {
  119. return godot_java->request_permissions();
  120. }
  121. Vector<String> OS_Android::get_granted_permissions() const {
  122. return godot_java->get_granted_permissions();
  123. }
  124. Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  125. p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
  126. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
  127. return OK;
  128. }
  129. String OS_Android::get_name() const {
  130. return "Android";
  131. }
  132. MainLoop *OS_Android::get_main_loop() const {
  133. return main_loop;
  134. }
  135. void OS_Android::main_loop_begin() {
  136. if (main_loop)
  137. main_loop->initialize();
  138. }
  139. bool OS_Android::main_loop_iterate() {
  140. if (!main_loop)
  141. return false;
  142. DisplayServerAndroid::get_singleton()->process_events();
  143. return Main::iteration();
  144. }
  145. void OS_Android::main_loop_end() {
  146. if (main_loop)
  147. main_loop->finalize();
  148. }
  149. void OS_Android::main_loop_focusout() {
  150. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  151. audio_driver_android.set_pause(true);
  152. }
  153. void OS_Android::main_loop_focusin() {
  154. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  155. audio_driver_android.set_pause(false);
  156. }
  157. Error OS_Android::shell_open(String p_uri) {
  158. return godot_io_java->open_uri(p_uri);
  159. }
  160. String OS_Android::get_resource_dir() const {
  161. return "/"; //android has its own filesystem for resources inside the APK
  162. }
  163. String OS_Android::get_locale() const {
  164. String locale = godot_io_java->get_locale();
  165. if (locale != "") {
  166. return locale;
  167. }
  168. return OS_Unix::get_locale();
  169. }
  170. String OS_Android::get_model_name() const {
  171. String model = godot_io_java->get_model();
  172. if (model != "")
  173. return model;
  174. return OS_Unix::get_model_name();
  175. }
  176. String OS_Android::get_data_path() const {
  177. return get_user_data_dir();
  178. }
  179. String OS_Android::get_user_data_dir() const {
  180. if (data_dir_cache != String())
  181. return data_dir_cache;
  182. String data_dir = godot_io_java->get_user_data_dir();
  183. if (data_dir != "") {
  184. data_dir_cache = _remove_symlink(data_dir);
  185. return data_dir_cache;
  186. }
  187. return ".";
  188. }
  189. String OS_Android::get_cache_path() const {
  190. if (cache_dir_cache != String())
  191. return cache_dir_cache;
  192. String cache_dir = godot_io_java->get_cache_dir();
  193. if (cache_dir != "") {
  194. cache_dir_cache = _remove_symlink(cache_dir);
  195. return cache_dir_cache;
  196. }
  197. return ".";
  198. }
  199. String OS_Android::get_unique_id() const {
  200. String unique_id = godot_io_java->get_unique_id();
  201. if (unique_id != "")
  202. return unique_id;
  203. return OS::get_unique_id();
  204. }
  205. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  206. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  207. }
  208. void OS_Android::set_display_size(const Size2i &p_size) {
  209. display_size = p_size;
  210. }
  211. Size2i OS_Android::get_display_size() const {
  212. return display_size;
  213. }
  214. void OS_Android::set_context_is_16_bits(bool p_is_16) {
  215. #if defined(GLES3_ENABLED)
  216. //if (rasterizer)
  217. // rasterizer->set_force_16_bits_fbo(p_is_16);
  218. #endif
  219. }
  220. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  221. #if defined(GLES3_ENABLED)
  222. ERR_FAIL_COND(!p_gl_extensions);
  223. gl_extensions = p_gl_extensions;
  224. #endif
  225. }
  226. void OS_Android::set_native_window(ANativeWindow *p_native_window) {
  227. #if defined(VULKAN_ENABLED)
  228. native_window = p_native_window;
  229. #endif
  230. }
  231. ANativeWindow *OS_Android::get_native_window() const {
  232. #if defined(VULKAN_ENABLED)
  233. return native_window;
  234. #else
  235. return nullptr;
  236. #endif
  237. }
  238. void OS_Android::vibrate_handheld(int p_duration_ms) {
  239. godot_java->vibrate(p_duration_ms);
  240. }
  241. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  242. if (p_feature == "mobile") {
  243. return true;
  244. }
  245. #if defined(__aarch64__)
  246. if (p_feature == "arm64-v8a") {
  247. return true;
  248. }
  249. #elif defined(__ARM_ARCH_7A__)
  250. if (p_feature == "armeabi-v7a" || p_feature == "armeabi") {
  251. return true;
  252. }
  253. #elif defined(__arm__)
  254. if (p_feature == "armeabi") {
  255. return true;
  256. }
  257. #endif
  258. return false;
  259. }
  260. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  261. display_size.width = 800;
  262. display_size.height = 600;
  263. use_apk_expansion = p_use_apk_expansion;
  264. main_loop = nullptr;
  265. #if defined(GLES3_ENABLED)
  266. gl_extensions = nullptr;
  267. use_gl2 = false;
  268. #endif
  269. #if defined(VULKAN_ENABLED)
  270. native_window = nullptr;
  271. #endif
  272. godot_java = p_godot_java;
  273. godot_io_java = p_godot_io_java;
  274. Vector<Logger *> loggers;
  275. loggers.push_back(memnew(AndroidLogger));
  276. _set_logger(memnew(CompositeLogger(loggers)));
  277. AudioDriverManager::add_driver(&audio_driver_android);
  278. DisplayServerAndroid::register_android_driver();
  279. }
  280. OS_Android::~OS_Android() {
  281. }