os_android.cpp 9.3 KB

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