os_android.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*************************************************************************/
  2. /* os_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/project_settings.h"
  32. #include "drivers/gles2/rasterizer_gles2.h"
  33. #include "drivers/gles3/rasterizer_gles3.h"
  34. #include "drivers/unix/dir_access_unix.h"
  35. #include "drivers/unix/file_access_unix.h"
  36. #include "main/main.h"
  37. #include "servers/visual/visual_server_raster.h"
  38. #include "servers/visual/visual_server_wrap_mt.h"
  39. #include "dir_access_jandroid.h"
  40. #include "file_access_android.h"
  41. #include "net_socket_android.h"
  42. #include <android/input.h>
  43. #include <core/os/keyboard.h>
  44. #include <dlfcn.h>
  45. #include "java_godot_io_wrapper.h"
  46. #include "java_godot_wrapper.h"
  47. String _remove_symlink(const String &dir) {
  48. // Workaround for Android 6.0+ using a symlink.
  49. // Save the current directory.
  50. char current_dir_name[2048];
  51. getcwd(current_dir_name, 2048);
  52. // Change directory to the external data directory.
  53. chdir(dir.utf8().get_data());
  54. // Get the actual directory without the potential symlink.
  55. char dir_name_wihout_symlink[2048];
  56. getcwd(dir_name_wihout_symlink, 2048);
  57. // Convert back to a String.
  58. String dir_without_symlink(dir_name_wihout_symlink);
  59. // Restore original current directory.
  60. chdir(current_dir_name);
  61. return dir_without_symlink;
  62. }
  63. class AndroidLogger : public Logger {
  64. public:
  65. virtual void logv(const char *p_format, va_list p_list, bool p_err) {
  66. __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  67. }
  68. virtual ~AndroidLogger() {}
  69. };
  70. int OS_Android::get_video_driver_count() const {
  71. return 2;
  72. }
  73. const char *OS_Android::get_video_driver_name(int p_driver) const {
  74. switch (p_driver) {
  75. case VIDEO_DRIVER_GLES3:
  76. return "GLES3";
  77. case VIDEO_DRIVER_GLES2:
  78. return "GLES2";
  79. }
  80. ERR_FAIL_V_MSG(NULL, "Invalid video driver index: " + itos(p_driver) + ".");
  81. }
  82. int OS_Android::get_audio_driver_count() const {
  83. return 1;
  84. }
  85. const char *OS_Android::get_audio_driver_name(int p_driver) const {
  86. return "Android";
  87. }
  88. void OS_Android::initialize_core() {
  89. OS_Unix::initialize_core();
  90. if (use_apk_expansion)
  91. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  92. else {
  93. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  94. }
  95. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  96. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  97. if (use_apk_expansion)
  98. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  99. else
  100. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  101. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  102. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  103. NetSocketAndroid::make_default();
  104. }
  105. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  106. ERR_FAIL_COND(!p_gl_extensions);
  107. gl_extensions = p_gl_extensions;
  108. }
  109. int OS_Android::get_current_video_driver() const {
  110. return video_driver_index;
  111. }
  112. Error OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  113. bool use_gl3 = godot_java->get_gles_version_code() >= 0x00030000;
  114. use_gl3 = use_gl3 && (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES3");
  115. bool gl_initialization_error = false;
  116. while (true) {
  117. if (use_gl3) {
  118. if (RasterizerGLES3::is_viable() == OK) {
  119. godot_java->gfx_init(false);
  120. RasterizerGLES3::register_config();
  121. RasterizerGLES3::make_current();
  122. break;
  123. } else {
  124. if (GLOBAL_GET("rendering/quality/driver/fallback_to_gles2")) {
  125. p_video_driver = VIDEO_DRIVER_GLES2;
  126. use_gl3 = false;
  127. continue;
  128. } else {
  129. gl_initialization_error = true;
  130. break;
  131. }
  132. }
  133. } else {
  134. if (RasterizerGLES2::is_viable() == OK) {
  135. godot_java->gfx_init(true);
  136. RasterizerGLES2::register_config();
  137. RasterizerGLES2::make_current();
  138. break;
  139. } else {
  140. gl_initialization_error = true;
  141. break;
  142. }
  143. }
  144. }
  145. transparency_enabled = p_desired.layered;
  146. if (gl_initialization_error) {
  147. OS::get_singleton()->alert("Your device does not support any of the supported OpenGL versions.\n"
  148. "Please try updating your Android version.",
  149. "Unable to initialize Video driver");
  150. return ERR_UNAVAILABLE;
  151. }
  152. video_driver_index = p_video_driver;
  153. visual_server = memnew(VisualServerRaster);
  154. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  155. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  156. }
  157. visual_server->init();
  158. AudioDriverManager::initialize(p_audio_driver);
  159. input = memnew(InputDefault);
  160. input->set_use_input_buffering(true); // Needed because events will come directly from the UI thread
  161. input->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  162. //power_manager = memnew(PowerAndroid);
  163. return OK;
  164. }
  165. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  166. main_loop = p_main_loop;
  167. input->set_main_loop(p_main_loop);
  168. }
  169. void OS_Android::delete_main_loop() {
  170. memdelete(main_loop);
  171. }
  172. void OS_Android::finalize() {
  173. memdelete(input);
  174. }
  175. GodotJavaWrapper *OS_Android::get_godot_java() {
  176. return godot_java;
  177. }
  178. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  179. return godot_io_java;
  180. }
  181. void OS_Android::alert(const String &p_alert, const String &p_title) {
  182. //print("ALERT: %s\n", p_alert.utf8().get_data());
  183. godot_java->alert(p_alert, p_title);
  184. }
  185. bool OS_Android::request_permission(const String &p_name) {
  186. return godot_java->request_permission(p_name);
  187. }
  188. bool OS_Android::request_permissions() {
  189. return godot_java->request_permissions();
  190. }
  191. Vector<String> OS_Android::get_granted_permissions() const {
  192. return godot_java->get_granted_permissions();
  193. }
  194. Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  195. p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
  196. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + ".");
  197. return OK;
  198. }
  199. void OS_Android::set_mouse_show(bool p_show) {
  200. //android has no mouse...
  201. }
  202. void OS_Android::set_mouse_grab(bool p_grab) {
  203. //it really has no mouse...!
  204. }
  205. bool OS_Android::is_mouse_grab_enabled() const {
  206. //*sigh* technology has evolved so much since i was a kid..
  207. return false;
  208. }
  209. Point2 OS_Android::get_mouse_position() const {
  210. return input->get_mouse_position();
  211. }
  212. int OS_Android::get_mouse_button_state() const {
  213. return input->get_mouse_button_mask();
  214. }
  215. void OS_Android::set_window_title(const String &p_title) {
  216. //This queries/updates the currently connected devices/joypads
  217. //Set_window_title is called when initializing the main loop (main.cpp)
  218. //therefore this place is found to be suitable (I found no better).
  219. godot_java->init_input_devices();
  220. }
  221. void OS_Android::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  222. }
  223. OS::VideoMode OS_Android::get_video_mode(int p_screen) const {
  224. return default_videomode;
  225. }
  226. void OS_Android::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  227. p_list->push_back(default_videomode);
  228. }
  229. void OS_Android::set_keep_screen_on(bool p_enabled) {
  230. OS::set_keep_screen_on(p_enabled);
  231. godot_java->set_keep_screen_on(p_enabled);
  232. }
  233. Size2 OS_Android::get_window_size() const {
  234. return Vector2(default_videomode.width, default_videomode.height);
  235. }
  236. Rect2 OS_Android::get_window_safe_area() const {
  237. int xywh[4];
  238. godot_io_java->get_window_safe_area(xywh);
  239. return Rect2(xywh[0], xywh[1], xywh[2], xywh[3]);
  240. }
  241. String OS_Android::get_name() const {
  242. return "Android";
  243. }
  244. MainLoop *OS_Android::get_main_loop() const {
  245. return main_loop;
  246. }
  247. bool OS_Android::can_draw() const {
  248. return true; //always?
  249. }
  250. void OS_Android::main_loop_begin() {
  251. if (main_loop)
  252. main_loop->init();
  253. }
  254. bool OS_Android::main_loop_iterate() {
  255. if (!main_loop)
  256. return false;
  257. return Main::iteration();
  258. }
  259. void OS_Android::main_loop_end() {
  260. if (main_loop)
  261. main_loop->finish();
  262. }
  263. void OS_Android::main_loop_focusout() {
  264. if (main_loop)
  265. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  266. audio_driver_android.set_pause(true);
  267. }
  268. void OS_Android::main_loop_focusin() {
  269. if (main_loop)
  270. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  271. audio_driver_android.set_pause(false);
  272. }
  273. void OS_Android::process_accelerometer(const Vector3 &p_accelerometer) {
  274. input->set_accelerometer(p_accelerometer);
  275. }
  276. void OS_Android::process_gravity(const Vector3 &p_gravity) {
  277. input->set_gravity(p_gravity);
  278. }
  279. void OS_Android::process_magnetometer(const Vector3 &p_magnetometer) {
  280. input->set_magnetometer(p_magnetometer);
  281. }
  282. void OS_Android::process_gyroscope(const Vector3 &p_gyroscope) {
  283. input->set_gyroscope(p_gyroscope);
  284. }
  285. bool OS_Android::has_touchscreen_ui_hint() const {
  286. return true;
  287. }
  288. bool OS_Android::has_virtual_keyboard() const {
  289. return true;
  290. }
  291. int OS_Android::get_virtual_keyboard_height() const {
  292. return godot_io_java->get_vk_height();
  293. // ERR_PRINT("Cannot obtain virtual keyboard height.");
  294. // return 0;
  295. }
  296. void OS_Android::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  297. if (godot_io_java->has_vk()) {
  298. godot_io_java->show_vk(p_existing_text, p_multiline, p_max_input_length, p_cursor_start, p_cursor_end);
  299. } else {
  300. ERR_PRINT("Virtual keyboard not available");
  301. };
  302. }
  303. void OS_Android::hide_virtual_keyboard() {
  304. if (godot_io_java->has_vk()) {
  305. godot_io_java->hide_vk();
  306. } else {
  307. ERR_PRINT("Virtual keyboard not available");
  308. };
  309. }
  310. void OS_Android::init_video_mode(int p_video_width, int p_video_height) {
  311. default_videomode.width = p_video_width;
  312. default_videomode.height = p_video_height;
  313. default_videomode.fullscreen = true;
  314. default_videomode.resizable = false;
  315. }
  316. void OS_Android::set_display_size(Size2 p_size) {
  317. default_videomode.width = p_size.x;
  318. default_videomode.height = p_size.y;
  319. }
  320. Error OS_Android::shell_open(String p_uri) {
  321. return godot_io_java->open_uri(p_uri);
  322. }
  323. String OS_Android::get_resource_dir() const {
  324. return "/"; //android has its own filesystem for resources inside the APK
  325. }
  326. String OS_Android::get_locale() const {
  327. String locale = godot_io_java->get_locale();
  328. if (locale != "") {
  329. return locale;
  330. }
  331. return OS_Unix::get_locale();
  332. }
  333. void OS_Android::set_clipboard(const String &p_text) {
  334. // DO we really need the fallback to OS_Unix here?!
  335. if (godot_java->has_set_clipboard()) {
  336. godot_java->set_clipboard(p_text);
  337. } else {
  338. OS_Unix::set_clipboard(p_text);
  339. }
  340. }
  341. String OS_Android::get_clipboard() const {
  342. // DO we really need the fallback to OS_Unix here?!
  343. if (godot_java->has_get_clipboard()) {
  344. return godot_java->get_clipboard();
  345. }
  346. return OS_Unix::get_clipboard();
  347. }
  348. bool OS_Android::has_clipboard() const {
  349. // DO we really need the fallback to OS_Unix here?!
  350. if (godot_java->has_has_clipboard()) {
  351. return godot_java->has_clipboard();
  352. }
  353. return OS_Unix::has_clipboard();
  354. }
  355. String OS_Android::get_model_name() const {
  356. String model = godot_io_java->get_model();
  357. if (model != "")
  358. return model;
  359. return OS_Unix::get_model_name();
  360. }
  361. int OS_Android::get_screen_dpi(int p_screen) const {
  362. return godot_io_java->get_screen_dpi();
  363. }
  364. float OS_Android::get_screen_refresh_rate(int p_screen) const {
  365. return godot_io_java->get_screen_refresh_rate(OS::get_singleton()->SCREEN_REFRESH_RATE_FALLBACK);
  366. }
  367. String OS_Android::get_data_path() const {
  368. return get_user_data_dir();
  369. }
  370. String OS_Android::get_user_data_dir() const {
  371. if (data_dir_cache != String())
  372. return data_dir_cache;
  373. String data_dir = godot_io_java->get_user_data_dir();
  374. if (data_dir != "") {
  375. data_dir_cache = _remove_symlink(data_dir);
  376. return data_dir_cache;
  377. }
  378. return ".";
  379. }
  380. String OS_Android::get_cache_path() const {
  381. if (cache_dir_cache != String())
  382. return cache_dir_cache;
  383. String cache_dir = godot_io_java->get_cache_dir();
  384. if (cache_dir != "") {
  385. cache_dir_cache = _remove_symlink(cache_dir);
  386. return cache_dir_cache;
  387. }
  388. return ".";
  389. }
  390. void OS_Android::set_screen_orientation(ScreenOrientation p_orientation) {
  391. godot_io_java->set_screen_orientation(p_orientation);
  392. }
  393. OS::ScreenOrientation OS_Android::get_screen_orientation() const {
  394. const int orientation = godot_io_java->get_screen_orientation();
  395. ERR_FAIL_INDEX_V_MSG(orientation, 7, OS::ScreenOrientation(0), "Unrecognized screen orientation.");
  396. return OS::ScreenOrientation(orientation);
  397. }
  398. String OS_Android::get_unique_id() const {
  399. String unique_id = godot_io_java->get_unique_id();
  400. if (unique_id != "")
  401. return unique_id;
  402. return OS::get_unique_id();
  403. }
  404. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  405. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  406. }
  407. void OS_Android::set_offscreen_gl_available(bool p_available) {
  408. secondary_gl_available = p_available;
  409. }
  410. bool OS_Android::is_offscreen_gl_available() const {
  411. return secondary_gl_available;
  412. }
  413. void OS_Android::set_offscreen_gl_current(bool p_current) {
  414. if (secondary_gl_available) {
  415. godot_java->set_offscreen_gl_current(nullptr, p_current);
  416. }
  417. }
  418. bool OS_Android::is_joy_known(int p_device) {
  419. return input->is_joy_mapped(p_device);
  420. }
  421. String OS_Android::get_joy_guid(int p_device) const {
  422. return input->get_joy_guid_remapped(p_device);
  423. }
  424. void OS_Android::vibrate_handheld(int p_duration_ms) {
  425. godot_java->vibrate(p_duration_ms);
  426. }
  427. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  428. if (p_feature == "mobile") {
  429. //TODO support etc2 only if GLES3 driver is selected
  430. return true;
  431. }
  432. #if defined(__aarch64__)
  433. if (p_feature == "arm64-v8a") {
  434. return true;
  435. }
  436. #elif defined(__ARM_ARCH_7A__)
  437. if (p_feature == "armeabi-v7a" || p_feature == "armeabi") {
  438. return true;
  439. }
  440. #elif defined(__arm__)
  441. if (p_feature == "armeabi") {
  442. return true;
  443. }
  444. #endif
  445. return false;
  446. }
  447. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  448. use_apk_expansion = p_use_apk_expansion;
  449. default_videomode.width = 800;
  450. default_videomode.height = 600;
  451. default_videomode.fullscreen = true;
  452. default_videomode.resizable = false;
  453. main_loop = NULL;
  454. gl_extensions = NULL;
  455. //rasterizer = NULL;
  456. use_gl2 = false;
  457. godot_java = p_godot_java;
  458. godot_io_java = p_godot_io_java;
  459. Vector<Logger *> loggers;
  460. loggers.push_back(memnew(AndroidLogger));
  461. _set_logger(memnew(CompositeLogger(loggers)));
  462. AudioDriverManager::add_driver(&audio_driver_android);
  463. }
  464. OS_Android::~OS_Android() {
  465. }