os_android.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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::initialize_core() {
  66. OS_Unix::initialize_core();
  67. if (use_apk_expansion)
  68. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  69. else {
  70. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  71. }
  72. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  73. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  74. if (use_apk_expansion)
  75. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  76. else
  77. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  78. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  79. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  80. NetSocketAndroid::make_default();
  81. }
  82. void OS_Android::initialize() {
  83. initialize_core();
  84. }
  85. void OS_Android::initialize_joypads() {
  86. Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  87. // This queries/updates the currently connected devices/joypads.
  88. godot_java->init_input_devices();
  89. }
  90. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  91. main_loop = p_main_loop;
  92. }
  93. void OS_Android::delete_main_loop() {
  94. if (main_loop) {
  95. memdelete(main_loop);
  96. main_loop = nullptr;
  97. }
  98. }
  99. void OS_Android::finalize() {
  100. }
  101. OS_Android *OS_Android::get_singleton() {
  102. return (OS_Android *)OS::get_singleton();
  103. }
  104. GodotJavaWrapper *OS_Android::get_godot_java() {
  105. return godot_java;
  106. }
  107. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  108. return godot_io_java;
  109. }
  110. bool OS_Android::request_permission(const String &p_name) {
  111. return godot_java->request_permission(p_name);
  112. }
  113. bool OS_Android::request_permissions() {
  114. return godot_java->request_permissions();
  115. }
  116. Vector<String> OS_Android::get_granted_permissions() const {
  117. return godot_java->get_granted_permissions();
  118. }
  119. Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  120. p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
  121. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
  122. return OK;
  123. }
  124. String OS_Android::get_name() const {
  125. return "Android";
  126. }
  127. MainLoop *OS_Android::get_main_loop() const {
  128. return main_loop;
  129. }
  130. void OS_Android::main_loop_begin() {
  131. if (main_loop)
  132. main_loop->initialize();
  133. }
  134. bool OS_Android::main_loop_iterate() {
  135. if (!main_loop)
  136. return false;
  137. DisplayServerAndroid::get_singleton()->process_events();
  138. return Main::iteration();
  139. }
  140. void OS_Android::main_loop_end() {
  141. if (main_loop)
  142. main_loop->finalize();
  143. }
  144. void OS_Android::main_loop_focusout() {
  145. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  146. audio_driver_android.set_pause(true);
  147. }
  148. void OS_Android::main_loop_focusin() {
  149. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  150. audio_driver_android.set_pause(false);
  151. }
  152. void OS_Android::main_loop_request_go_back() {
  153. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST);
  154. }
  155. Error OS_Android::shell_open(String p_uri) {
  156. return godot_io_java->open_uri(p_uri);
  157. }
  158. String OS_Android::get_resource_dir() const {
  159. return "/"; //android has its own filesystem for resources inside the APK
  160. }
  161. String OS_Android::get_locale() const {
  162. String locale = godot_io_java->get_locale();
  163. if (locale != "") {
  164. return locale;
  165. }
  166. return OS_Unix::get_locale();
  167. }
  168. String OS_Android::get_model_name() const {
  169. String model = godot_io_java->get_model();
  170. if (model != "")
  171. return model;
  172. return OS_Unix::get_model_name();
  173. }
  174. String OS_Android::get_user_data_dir() const {
  175. if (data_dir_cache != String())
  176. return data_dir_cache;
  177. String data_dir = godot_io_java->get_user_data_dir();
  178. if (data_dir != "") {
  179. data_dir_cache = _remove_symlink(data_dir);
  180. return data_dir_cache;
  181. }
  182. return ".";
  183. }
  184. String OS_Android::get_external_data_dir() const {
  185. String data_dir = godot_io_java->get_external_data_dir();
  186. if (data_dir != "") {
  187. data_dir = _remove_symlink(data_dir);
  188. return data_dir;
  189. }
  190. return ".";
  191. }
  192. String OS_Android::get_unique_id() const {
  193. String unique_id = godot_io_java->get_unique_id();
  194. if (unique_id != "")
  195. return unique_id;
  196. return OS::get_unique_id();
  197. }
  198. String OS_Android::get_system_dir(SystemDir p_dir) const {
  199. return godot_io_java->get_system_dir(p_dir);
  200. }
  201. void OS_Android::set_display_size(const Size2i &p_size) {
  202. display_size = p_size;
  203. }
  204. Size2i OS_Android::get_display_size() const {
  205. return display_size;
  206. }
  207. void OS_Android::set_context_is_16_bits(bool p_is_16) {
  208. #if defined(OPENGL_ENABLED)
  209. //use_16bits_fbo = p_is_16;
  210. //if (rasterizer)
  211. // rasterizer->set_force_16_bits_fbo(p_is_16);
  212. #endif
  213. }
  214. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  215. #if defined(OPENGL_ENABLED)
  216. ERR_FAIL_COND(!p_gl_extensions);
  217. gl_extensions = p_gl_extensions;
  218. #endif
  219. }
  220. void OS_Android::set_native_window(ANativeWindow *p_native_window) {
  221. #if defined(VULKAN_ENABLED)
  222. native_window = p_native_window;
  223. #endif
  224. }
  225. ANativeWindow *OS_Android::get_native_window() const {
  226. #if defined(VULKAN_ENABLED)
  227. return native_window;
  228. #else
  229. return nullptr;
  230. #endif
  231. }
  232. void OS_Android::vibrate_handheld(int p_duration_ms) {
  233. godot_java->vibrate(p_duration_ms);
  234. }
  235. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  236. if (p_feature == "mobile") {
  237. return true;
  238. }
  239. #if defined(__aarch64__)
  240. if (p_feature == "arm64-v8a") {
  241. return true;
  242. }
  243. #elif defined(__ARM_ARCH_7A__)
  244. if (p_feature == "armeabi-v7a" || p_feature == "armeabi") {
  245. return true;
  246. }
  247. #elif defined(__arm__)
  248. if (p_feature == "armeabi") {
  249. return true;
  250. }
  251. #endif
  252. return false;
  253. }
  254. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  255. display_size.width = 800;
  256. display_size.height = 600;
  257. use_apk_expansion = p_use_apk_expansion;
  258. main_loop = nullptr;
  259. #if defined(OPENGL_ENABLED)
  260. gl_extensions = nullptr;
  261. use_gl2 = false;
  262. use_16bits_fbo = false;
  263. #endif
  264. #if defined(VULKAN_ENABLED)
  265. native_window = nullptr;
  266. #endif
  267. godot_java = p_godot_java;
  268. godot_io_java = p_godot_io_java;
  269. Vector<Logger *> loggers;
  270. loggers.push_back(memnew(AndroidLogger));
  271. _set_logger(memnew(CompositeLogger(loggers)));
  272. AudioDriverManager::add_driver(&audio_driver_android);
  273. DisplayServerAndroid::register_android_driver();
  274. }
  275. OS_Android::~OS_Android() {
  276. }