os_android.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*************************************************************************/
  2. /* os_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "drivers/gles2/rasterizer_gles2.h"
  32. #include "core/io/file_access_buffered_fa.h"
  33. #include "drivers/unix/dir_access_unix.h"
  34. #include "drivers/unix/file_access_unix.h"
  35. #include "main/main.h"
  36. #include "servers/visual/visual_server_raster.h"
  37. #include "servers/visual/visual_server_wrap_mt.h"
  38. #include "file_access_android.h"
  39. #include "core/globals.h"
  40. #ifdef ANDROID_NATIVE_ACTIVITY
  41. #include "dir_access_android.h"
  42. #include "file_access_android.h"
  43. #else
  44. #include "dir_access_jandroid.h"
  45. #include "file_access_jandroid.h"
  46. #endif
  47. int OS_Android::get_video_driver_count() const {
  48. return 1;
  49. }
  50. const char *OS_Android::get_video_driver_name(int p_driver) const {
  51. return "GLES2";
  52. }
  53. OS::VideoMode OS_Android::get_default_video_mode() const {
  54. return OS::VideoMode();
  55. }
  56. int OS_Android::get_audio_driver_count() const {
  57. return 1;
  58. }
  59. const char *OS_Android::get_audio_driver_name(int p_driver) const {
  60. return "Android";
  61. }
  62. void OS_Android::initialize_core() {
  63. OS_Unix::initialize_core();
  64. #ifdef ANDROID_NATIVE_ACTIVITY
  65. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  66. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  67. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  68. //FileAccessBufferedFA<FileAccessUnix>::make_default();
  69. DirAccess::make_default<DirAccessAndroid>(DirAccess::ACCESS_RESOURCES);
  70. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  71. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  72. #else
  73. if (use_apk_expansion)
  74. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  75. else {
  76. #ifdef USE_JAVA_FILE_ACCESS
  77. FileAccess::make_default<FileAccessBufferedFA<FileAccessJAndroid> >(FileAccess::ACCESS_RESOURCES);
  78. #else
  79. //FileAccess::make_default<FileAccessBufferedFA<FileAccessAndroid> >(FileAccess::ACCESS_RESOURCES);
  80. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  81. #endif
  82. }
  83. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  84. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  85. //FileAccessBufferedFA<FileAccessUnix>::make_default();
  86. if (use_apk_expansion)
  87. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  88. else
  89. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  90. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  91. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  92. #endif
  93. }
  94. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  95. ERR_FAIL_COND(!p_gl_extensions);
  96. gl_extensions = p_gl_extensions;
  97. }
  98. void OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  99. use_gl2 = p_video_driver != 1;
  100. if (gfx_init_func)
  101. gfx_init_func(gfx_init_ud, use_gl2);
  102. AudioDriverManagerSW::add_driver(&audio_driver_android);
  103. RasterizerGLES2 *rasterizer_gles22 = memnew(RasterizerGLES2(false, use_reload_hooks, false, use_reload_hooks));
  104. if (gl_extensions)
  105. rasterizer_gles22->set_extensions(gl_extensions);
  106. rasterizer = rasterizer_gles22;
  107. rasterizer->set_force_16_bits_fbo(use_16bits_fbo);
  108. visual_server = memnew(VisualServerRaster(rasterizer));
  109. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  110. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  111. };
  112. visual_server->init();
  113. AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
  114. if (AudioDriverManagerSW::get_driver(p_audio_driver)->init() != OK) {
  115. ERR_PRINT("Initializing audio failed.");
  116. }
  117. sample_manager = memnew(SampleManagerMallocSW);
  118. audio_server = memnew(AudioServerSW(sample_manager));
  119. audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR, true);
  120. audio_server->init();
  121. spatial_sound_server = memnew(SpatialSoundServerSW);
  122. spatial_sound_server->init();
  123. spatial_sound_2d_server = memnew(SpatialSound2DServerSW);
  124. spatial_sound_2d_server->init();
  125. //
  126. physics_server = memnew(PhysicsServerSW);
  127. physics_server->init();
  128. //physics_2d_server = memnew( Physics2DServerSW );
  129. physics_2d_server = Physics2DServerWrapMT::init_server<Physics2DServerSW>();
  130. physics_2d_server->init();
  131. input = memnew(InputDefault);
  132. input->set_fallback_mapping("Default Android Gamepad");
  133. }
  134. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  135. main_loop = p_main_loop;
  136. input->set_main_loop(p_main_loop);
  137. }
  138. void OS_Android::delete_main_loop() {
  139. memdelete(main_loop);
  140. }
  141. void OS_Android::finalize() {
  142. memdelete(input);
  143. }
  144. void OS_Android::vprint(const char *p_format, va_list p_list, bool p_stderr) {
  145. __android_log_vprint(p_stderr ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  146. }
  147. void OS_Android::print(const char *p_format, ...) {
  148. va_list argp;
  149. va_start(argp, p_format);
  150. __android_log_vprint(ANDROID_LOG_INFO, "godot", p_format, argp);
  151. va_end(argp);
  152. }
  153. void OS_Android::alert(const String &p_alert, const String &p_title) {
  154. print("ALERT: %s\n", p_alert.utf8().get_data());
  155. if (alert_func)
  156. alert_func(p_alert, p_title);
  157. }
  158. void OS_Android::set_mouse_show(bool p_show) {
  159. //android has no mouse...
  160. }
  161. void OS_Android::set_mouse_grab(bool p_grab) {
  162. //it really has no mouse...!
  163. }
  164. bool OS_Android::is_mouse_grab_enabled() const {
  165. //*sigh* technology has evolved so much since i was a kid..
  166. return false;
  167. }
  168. Point2 OS_Android::get_mouse_pos() const {
  169. return Point2();
  170. }
  171. int OS_Android::get_mouse_button_state() const {
  172. return 0;
  173. }
  174. void OS_Android::set_window_title(const String &p_title) {
  175. }
  176. void OS_Android::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  177. }
  178. OS::VideoMode OS_Android::get_video_mode(int p_screen) const {
  179. return default_videomode;
  180. }
  181. void OS_Android::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  182. p_list->push_back(default_videomode);
  183. }
  184. void OS_Android::set_keep_screen_on(bool p_enabled) {
  185. OS::set_keep_screen_on(p_enabled);
  186. if (set_keep_screen_on_func) {
  187. set_keep_screen_on_func(p_enabled);
  188. }
  189. }
  190. Size2 OS_Android::get_window_size() const {
  191. return Vector2(default_videomode.width, default_videomode.height);
  192. }
  193. String OS_Android::get_name() {
  194. return "Android";
  195. }
  196. MainLoop *OS_Android::get_main_loop() const {
  197. return main_loop;
  198. }
  199. bool OS_Android::can_draw() const {
  200. return true; //always?
  201. }
  202. void OS_Android::set_cursor_shape(CursorShape p_shape) {
  203. //android really really really has no mouse.. how amazing..
  204. }
  205. void OS_Android::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  206. // Since android has no mouse, we do not need to change its texture (how amazing !)
  207. }
  208. void OS_Android::main_loop_begin() {
  209. if (main_loop)
  210. main_loop->init();
  211. }
  212. bool OS_Android::main_loop_iterate() {
  213. if (!main_loop)
  214. return false;
  215. return Main::iteration();
  216. }
  217. void OS_Android::main_loop_end() {
  218. if (main_loop)
  219. main_loop->finish();
  220. }
  221. void OS_Android::main_loop_focusout() {
  222. if (main_loop)
  223. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  224. audio_driver_android.set_pause(true);
  225. }
  226. void OS_Android::main_loop_focusin() {
  227. if (main_loop)
  228. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  229. audio_driver_android.set_pause(false);
  230. }
  231. void OS_Android::process_joy_event(OS_Android::JoystickEvent p_event) {
  232. switch (p_event.type) {
  233. case JOY_EVENT_BUTTON:
  234. last_id = input->joy_button(last_id, p_event.device, p_event.index, p_event.pressed);
  235. break;
  236. case JOY_EVENT_AXIS:
  237. InputDefault::JoyAxis value;
  238. value.min = -1;
  239. value.value = p_event.value;
  240. last_id = input->joy_axis(last_id, p_event.device, p_event.index, value);
  241. break;
  242. case JOY_EVENT_HAT:
  243. last_id = input->joy_hat(last_id, p_event.device, p_event.hat);
  244. break;
  245. default:
  246. return;
  247. }
  248. }
  249. void OS_Android::process_event(InputEvent p_event) {
  250. p_event.ID = last_id++;
  251. input->parse_input_event(p_event);
  252. }
  253. void OS_Android::process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points) {
  254. // print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  255. switch (p_what) {
  256. case 0: { //gesture begin
  257. if (touch.size()) {
  258. //end all if exist
  259. InputEvent ev;
  260. ev.type = InputEvent::MOUSE_BUTTON;
  261. ev.ID = last_id++;
  262. ev.mouse_button.button_index = BUTTON_LEFT;
  263. ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
  264. ev.mouse_button.pressed = false;
  265. ev.mouse_button.x = touch[0].pos.x;
  266. ev.mouse_button.y = touch[0].pos.y;
  267. ev.mouse_button.global_x = touch[0].pos.x;
  268. ev.mouse_button.global_y = touch[0].pos.y;
  269. input->parse_input_event(ev);
  270. for (int i = 0; i < touch.size(); i++) {
  271. InputEvent ev;
  272. ev.type = InputEvent::SCREEN_TOUCH;
  273. ev.ID = last_id++;
  274. ev.screen_touch.index = touch[i].id;
  275. ev.screen_touch.pressed = false;
  276. ev.screen_touch.x = touch[i].pos.x;
  277. ev.screen_touch.y = touch[i].pos.y;
  278. input->parse_input_event(ev);
  279. }
  280. }
  281. touch.resize(p_points.size());
  282. for (int i = 0; i < p_points.size(); i++) {
  283. touch[i].id = p_points[i].id;
  284. touch[i].pos = p_points[i].pos;
  285. }
  286. {
  287. //send mouse
  288. InputEvent ev;
  289. ev.type = InputEvent::MOUSE_BUTTON;
  290. ev.ID = last_id++;
  291. ev.mouse_button.button_index = BUTTON_LEFT;
  292. ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
  293. ev.mouse_button.pressed = true;
  294. ev.mouse_button.x = touch[0].pos.x;
  295. ev.mouse_button.y = touch[0].pos.y;
  296. ev.mouse_button.global_x = touch[0].pos.x;
  297. ev.mouse_button.global_y = touch[0].pos.y;
  298. input->set_mouse_pos(Point2(touch[0].pos.x, touch[0].pos.y));
  299. last_mouse = touch[0].pos;
  300. input->parse_input_event(ev);
  301. }
  302. //send touch
  303. for (int i = 0; i < touch.size(); i++) {
  304. InputEvent ev;
  305. ev.type = InputEvent::SCREEN_TOUCH;
  306. ev.ID = last_id++;
  307. ev.screen_touch.index = touch[i].id;
  308. ev.screen_touch.pressed = true;
  309. ev.screen_touch.x = touch[i].pos.x;
  310. ev.screen_touch.y = touch[i].pos.y;
  311. input->parse_input_event(ev);
  312. }
  313. } break;
  314. case 1: { //motion
  315. if (p_points.size()) {
  316. //send mouse, should look for point 0?
  317. InputEvent ev;
  318. ev.type = InputEvent::MOUSE_MOTION;
  319. ev.ID = last_id++;
  320. ev.mouse_motion.button_mask = BUTTON_MASK_LEFT;
  321. ev.mouse_motion.x = p_points[0].pos.x;
  322. ev.mouse_motion.y = p_points[0].pos.y;
  323. input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  324. ev.mouse_motion.speed_x = input->get_mouse_speed().x;
  325. ev.mouse_motion.speed_y = input->get_mouse_speed().y;
  326. ev.mouse_motion.relative_x = p_points[0].pos.x - last_mouse.x;
  327. ev.mouse_motion.relative_y = p_points[0].pos.y - last_mouse.y;
  328. last_mouse = p_points[0].pos;
  329. input->parse_input_event(ev);
  330. }
  331. ERR_FAIL_COND(touch.size() != p_points.size());
  332. for (int i = 0; i < touch.size(); i++) {
  333. int idx = -1;
  334. for (int j = 0; j < p_points.size(); j++) {
  335. if (touch[i].id == p_points[j].id) {
  336. idx = j;
  337. break;
  338. }
  339. }
  340. ERR_CONTINUE(idx == -1);
  341. if (touch[i].pos == p_points[idx].pos)
  342. continue; //no move unncesearily
  343. InputEvent ev;
  344. ev.type = InputEvent::SCREEN_DRAG;
  345. ev.ID = last_id++;
  346. ev.screen_drag.index = touch[i].id;
  347. ev.screen_drag.x = p_points[idx].pos.x;
  348. ev.screen_drag.y = p_points[idx].pos.y;
  349. ev.screen_drag.relative_x = p_points[idx].pos.x - touch[i].pos.x;
  350. ev.screen_drag.relative_y = p_points[idx].pos.y - touch[i].pos.y;
  351. input->parse_input_event(ev);
  352. touch[i].pos = p_points[idx].pos;
  353. }
  354. } break;
  355. case 2: { //release
  356. if (touch.size()) {
  357. //end all if exist
  358. InputEvent ev;
  359. ev.type = InputEvent::MOUSE_BUTTON;
  360. ev.ID = last_id++;
  361. ev.mouse_button.button_index = BUTTON_LEFT;
  362. ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
  363. ev.mouse_button.pressed = false;
  364. ev.mouse_button.x = touch[0].pos.x;
  365. ev.mouse_button.y = touch[0].pos.y;
  366. ev.mouse_button.global_x = touch[0].pos.x;
  367. ev.mouse_button.global_y = touch[0].pos.y;
  368. input->set_mouse_pos(Point2(touch[0].pos.x, touch[0].pos.y));
  369. input->parse_input_event(ev);
  370. for (int i = 0; i < touch.size(); i++) {
  371. InputEvent ev;
  372. ev.type = InputEvent::SCREEN_TOUCH;
  373. ev.ID = last_id++;
  374. ev.screen_touch.index = touch[i].id;
  375. ev.screen_touch.pressed = false;
  376. ev.screen_touch.x = touch[i].pos.x;
  377. ev.screen_touch.y = touch[i].pos.y;
  378. input->parse_input_event(ev);
  379. }
  380. touch.clear();
  381. }
  382. } break;
  383. case 3: { // add tuchi
  384. ERR_FAIL_INDEX(p_pointer, p_points.size());
  385. TouchPos tp = p_points[p_pointer];
  386. touch.push_back(tp);
  387. InputEvent ev;
  388. ev.type = InputEvent::SCREEN_TOUCH;
  389. ev.ID = last_id++;
  390. ev.screen_touch.index = tp.id;
  391. ev.screen_touch.pressed = true;
  392. ev.screen_touch.x = tp.pos.x;
  393. ev.screen_touch.y = tp.pos.y;
  394. input->parse_input_event(ev);
  395. } break;
  396. case 4: {
  397. for (int i = 0; i < touch.size(); i++) {
  398. if (touch[i].id == p_pointer) {
  399. InputEvent ev;
  400. ev.type = InputEvent::SCREEN_TOUCH;
  401. ev.ID = last_id++;
  402. ev.screen_touch.index = touch[i].id;
  403. ev.screen_touch.pressed = false;
  404. ev.screen_touch.x = touch[i].pos.x;
  405. ev.screen_touch.y = touch[i].pos.y;
  406. input->parse_input_event(ev);
  407. touch.remove(i);
  408. i--;
  409. }
  410. }
  411. } break;
  412. }
  413. }
  414. void OS_Android::process_accelerometer(const Vector3 &p_accelerometer) {
  415. input->set_accelerometer(p_accelerometer);
  416. }
  417. void OS_Android::process_gravitymeter(const Vector3 &p_gravitymeter) {
  418. input->set_gravity(p_gravitymeter);
  419. }
  420. void OS_Android::process_magnetometer(const Vector3 &p_magnetometer) {
  421. input->set_magnetometer(p_magnetometer);
  422. }
  423. void OS_Android::process_gyroscope(const Vector3 &p_gyroscope) {
  424. input->set_gyroscope(p_gyroscope);
  425. }
  426. bool OS_Android::has_touchscreen_ui_hint() const {
  427. return true;
  428. }
  429. bool OS_Android::has_virtual_keyboard() const {
  430. return true;
  431. }
  432. void OS_Android::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  433. if (show_virtual_keyboard_func) {
  434. show_virtual_keyboard_func(p_existing_text);
  435. } else {
  436. ERR_PRINT("Virtual keyboard not available");
  437. };
  438. }
  439. void OS_Android::hide_virtual_keyboard() {
  440. if (hide_virtual_keyboard_func) {
  441. hide_virtual_keyboard_func();
  442. } else {
  443. ERR_PRINT("Virtual keyboard not available");
  444. };
  445. }
  446. void OS_Android::init_video_mode(int p_video_width, int p_video_height) {
  447. default_videomode.width = p_video_width;
  448. default_videomode.height = p_video_height;
  449. default_videomode.fullscreen = true;
  450. default_videomode.resizable = false;
  451. }
  452. void OS_Android::main_loop_request_quit() {
  453. if (main_loop)
  454. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  455. }
  456. void OS_Android::set_display_size(Size2 p_size) {
  457. default_videomode.width = p_size.x;
  458. default_videomode.height = p_size.y;
  459. }
  460. void OS_Android::reload_gfx() {
  461. if (gfx_init_func)
  462. gfx_init_func(gfx_init_ud, use_gl2);
  463. if (rasterizer)
  464. rasterizer->reload_vram();
  465. }
  466. Error OS_Android::shell_open(String p_uri) {
  467. if (open_uri_func)
  468. return open_uri_func(p_uri) ? ERR_CANT_OPEN : OK;
  469. return ERR_UNAVAILABLE;
  470. }
  471. String OS_Android::get_resource_dir() const {
  472. return "/"; //android has it's own filesystem for resources inside the APK
  473. }
  474. String OS_Android::get_locale() const {
  475. if (get_locale_func)
  476. return get_locale_func();
  477. return OS_Unix::get_locale();
  478. }
  479. void OS_Android::set_clipboard(const String &p_text) {
  480. if (set_clipboard_func) {
  481. set_clipboard_func(p_text);
  482. } else {
  483. OS_Unix::set_clipboard(p_text);
  484. }
  485. }
  486. String OS_Android::get_clipboard() const {
  487. if (get_clipboard_func) {
  488. return get_clipboard_func();
  489. }
  490. return OS_Unix::get_clipboard();
  491. }
  492. String OS_Android::get_model_name() const {
  493. if (get_model_func)
  494. return get_model_func();
  495. return OS_Unix::get_model_name();
  496. }
  497. int OS_Android::get_screen_dpi(int p_screen) const {
  498. if (get_screen_dpi_func) {
  499. return get_screen_dpi_func();
  500. }
  501. return 160;
  502. }
  503. void OS_Android::set_need_reload_hooks(bool p_needs_them) {
  504. use_reload_hooks = p_needs_them;
  505. }
  506. String OS_Android::get_data_dir() const {
  507. if (data_dir_cache != String())
  508. return data_dir_cache;
  509. if (get_data_dir_func) {
  510. String data_dir = get_data_dir_func();
  511. //store current dir
  512. char real_current_dir_name[2048];
  513. getcwd(real_current_dir_name, 2048);
  514. //go to data dir
  515. chdir(data_dir.utf8().get_data());
  516. //get actual data dir, so we resolve potential symlink (Android 6.0+ seems to use symlink)
  517. char data_current_dir_name[2048];
  518. getcwd(data_current_dir_name, 2048);
  519. //cache by parsing utf8
  520. data_dir_cache.parse_utf8(data_current_dir_name);
  521. //restore original dir so we don't mess things up
  522. chdir(real_current_dir_name);
  523. return data_dir_cache;
  524. }
  525. return ".";
  526. //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  527. }
  528. void OS_Android::set_screen_orientation(ScreenOrientation p_orientation) {
  529. if (set_screen_orientation_func)
  530. set_screen_orientation_func(p_orientation);
  531. }
  532. String OS_Android::get_unique_ID() const {
  533. if (get_unique_id_func)
  534. return get_unique_id_func();
  535. return OS::get_unique_ID();
  536. }
  537. Error OS_Android::native_video_play(String p_path, float p_volume) {
  538. if (video_play_func)
  539. video_play_func(p_path);
  540. return OK;
  541. }
  542. bool OS_Android::native_video_is_playing() {
  543. if (video_is_playing_func)
  544. return video_is_playing_func();
  545. return false;
  546. }
  547. void OS_Android::native_video_pause() {
  548. if (video_pause_func)
  549. video_pause_func();
  550. }
  551. String OS_Android::get_system_dir(SystemDir p_dir) const {
  552. if (get_system_dir_func)
  553. return get_system_dir_func(p_dir);
  554. return String(".");
  555. }
  556. void OS_Android::native_video_stop() {
  557. if (video_stop_func)
  558. video_stop_func();
  559. }
  560. void OS_Android::set_context_is_16_bits(bool p_is_16) {
  561. use_16bits_fbo = p_is_16;
  562. if (rasterizer)
  563. rasterizer->set_force_16_bits_fbo(p_is_16);
  564. }
  565. void OS_Android::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  566. return input->joy_connection_changed(p_device, p_connected, p_name, "");
  567. }
  568. bool OS_Android::is_joy_known(int p_device) {
  569. return input->is_joy_mapped(p_device);
  570. }
  571. void OS_Android::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
  572. const char *err_details;
  573. if (p_rationale && p_rationale[0])
  574. err_details = p_rationale;
  575. else
  576. err_details = p_code;
  577. switch (p_type) {
  578. case ERR_ERROR:
  579. print("ERROR: %s: %s\n", p_function, err_details);
  580. print(" At: %s:%i\n", p_file, p_line);
  581. if (emit_error_signal) {
  582. emit_error_signal("Error", p_function, err_details, p_file, p_line);
  583. }
  584. break;
  585. case ERR_WARNING:
  586. print("WARNING: %s: %s\n", p_function, err_details);
  587. print(" At: %s:%i\n", p_file, p_line);
  588. if (emit_error_signal) {
  589. emit_error_signal("Warning", p_function, err_details, p_file, p_line);
  590. }
  591. break;
  592. case ERR_SCRIPT:
  593. print("SCRIPT ERROR: %s: %s\n", p_function, err_details);
  594. print(" At: %s:%i\n", p_file, p_line);
  595. if (emit_error_signal) {
  596. emit_error_signal("Script error", p_function, err_details, p_file, p_line);
  597. }
  598. break;
  599. }
  600. }
  601. String OS_Android::get_joy_guid(int p_device) const {
  602. return input->get_joy_guid_remapped(p_device);
  603. }
  604. OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, bool p_use_apk_expansion, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func, EmitErrorSignal p_emit_error_signal) {
  605. use_apk_expansion = p_use_apk_expansion;
  606. default_videomode.width = 800;
  607. default_videomode.height = 600;
  608. default_videomode.fullscreen = true;
  609. default_videomode.resizable = false;
  610. gfx_init_func = p_gfx_init_func;
  611. gfx_init_ud = p_gfx_init_ud;
  612. main_loop = NULL;
  613. last_id = 1;
  614. gl_extensions = NULL;
  615. rasterizer = NULL;
  616. use_gl2 = false;
  617. open_uri_func = p_open_uri_func;
  618. get_data_dir_func = p_get_data_dir_func;
  619. get_locale_func = p_get_locale_func;
  620. get_model_func = p_get_model_func;
  621. get_screen_dpi_func = p_get_screen_dpi_func;
  622. get_unique_id_func = p_get_unique_id;
  623. get_system_dir_func = p_get_sdir_func;
  624. video_play_func = p_video_play_func;
  625. video_is_playing_func = p_video_is_playing_func;
  626. video_pause_func = p_video_pause_func;
  627. video_stop_func = p_video_stop_func;
  628. show_virtual_keyboard_func = p_show_vk;
  629. hide_virtual_keyboard_func = p_hide_vk;
  630. set_screen_orientation_func = p_screen_orient;
  631. set_keep_screen_on_func = p_set_keep_screen_on_func;
  632. alert_func = p_alert_func;
  633. use_reload_hooks = false;
  634. set_clipboard_func = p_set_clipboard_func;
  635. get_clipboard_func = p_get_clipboard_func;
  636. emit_error_signal = p_emit_error_signal;
  637. }
  638. OS_Android::~OS_Android() {
  639. }