os_android.cpp 9.4 KB

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