os_android.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*************************************************************************/
  2. /* os_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "os_android.h"
  30. #include "drivers/gles2/rasterizer_gles2.h"
  31. #include "core/io/file_access_buffered_fa.h"
  32. #include "drivers/unix/file_access_unix.h"
  33. #include "drivers/unix/dir_access_unix.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. #include "servers/visual/visual_server_wrap_mt.h"
  36. #include "main/main.h"
  37. #include "file_access_android.h"
  38. #include "core/globals.h"
  39. #ifdef ANDROID_NATIVE_ACTIVITY
  40. #include "file_access_android.h"
  41. #include "dir_access_android.h"
  42. #else
  43. #include "file_access_jandroid.h"
  44. #include "dir_access_jandroid.h"
  45. #endif
  46. int OS_Android::get_video_driver_count() const {
  47. return 1;
  48. }
  49. const char * OS_Android::get_video_driver_name(int p_driver) const {
  50. return "GLES2";
  51. }
  52. OS::VideoMode OS_Android::get_default_video_mode() const {
  53. return OS::VideoMode();
  54. }
  55. int OS_Android::get_audio_driver_count() const {
  56. return 1;
  57. }
  58. const char * OS_Android::get_audio_driver_name(int p_driver) const {
  59. return "Android";
  60. }
  61. void OS_Android::initialize_core() {
  62. OS_Unix::initialize_core();
  63. #ifdef ANDROID_NATIVE_ACTIVITY
  64. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  65. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  66. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  67. //FileAccessBufferedFA<FileAccessUnix>::make_default();
  68. DirAccess::make_default<DirAccessAndroid>(DirAccess::ACCESS_RESOURCES);
  69. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  70. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  71. #else
  72. if (use_apk_expansion)
  73. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  74. else {
  75. #ifdef USE_JAVA_FILE_ACCESS
  76. FileAccess::make_default<FileAccessBufferedFA<FileAccessJAndroid> >(FileAccess::ACCESS_RESOURCES);
  77. #else
  78. //FileAccess::make_default<FileAccessBufferedFA<FileAccessAndroid> >(FileAccess::ACCESS_RESOURCES);
  79. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  80. #endif
  81. }
  82. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  83. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  84. //FileAccessBufferedFA<FileAccessUnix>::make_default();
  85. if (use_apk_expansion)
  86. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  87. else
  88. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  89. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  90. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  91. #endif
  92. }
  93. void OS_Android::set_opengl_extensions(const char* p_gl_extensions) {
  94. ERR_FAIL_COND(!p_gl_extensions);
  95. gl_extensions=p_gl_extensions;
  96. }
  97. void OS_Android::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  98. use_gl2=p_video_driver!=1;
  99. if (gfx_init_func)
  100. gfx_init_func(gfx_init_ud,use_gl2);
  101. AudioDriverManagerSW::add_driver(&audio_driver_android);
  102. RasterizerGLES2 *rasterizer_gles22=memnew( RasterizerGLES2(false,use_reload_hooks,false,use_reload_hooks ) );
  103. if (gl_extensions)
  104. rasterizer_gles22->set_extensions(gl_extensions);
  105. rasterizer = rasterizer_gles22;
  106. rasterizer->set_force_16_bits_fbo(use_16bits_fbo);
  107. visual_server = memnew( VisualServerRaster(rasterizer) );
  108. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  109. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  110. };
  111. visual_server->init();
  112. visual_server->cursor_set_visible(false, 0);
  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. #if 0
  138. print_line("preGS");
  139. FileAccess *f=memnew( FileAccessAndroid );
  140. print("made f %p\n",f);
  141. Error err = f->open("AndroidManifest.xml",FileAccess::READ);
  142. if (err) {
  143. print("************NO FILE!!\n");
  144. } else {
  145. print("************YES FILE!!\n");
  146. }
  147. f->close();
  148. print_line("end");
  149. AAssetDir* aad = AAssetManager_openDir(FileAccessAndroid::asset_manager,".");
  150. if (aad) {
  151. print_line("DIR OPEN OK");
  152. const char *fn= AAssetDir_getNextFileName(aad);
  153. while(fn) {
  154. print_line("FNAME: "+String(fn));
  155. fn= AAssetDir_getNextFileName(aad);
  156. }
  157. AAssetDir_close(aad);
  158. } else {
  159. print_line("DIR NO OPEN");
  160. }
  161. #endif
  162. }
  163. void OS_Android::delete_main_loop() {
  164. memdelete( main_loop );
  165. }
  166. void OS_Android::finalize() {
  167. memdelete(input);
  168. }
  169. void OS_Android::vprint(const char* p_format, va_list p_list, bool p_stderr) {
  170. __android_log_vprint(p_stderr?ANDROID_LOG_ERROR:ANDROID_LOG_INFO,"godot",p_format,p_list);
  171. }
  172. void OS_Android::print(const char *p_format, ... ) {
  173. va_list argp;
  174. va_start(argp, p_format);
  175. __android_log_vprint(ANDROID_LOG_INFO,"godot",p_format,argp);
  176. va_end(argp);
  177. }
  178. void OS_Android::alert(const String& p_alert,const String& p_title) {
  179. print("ALERT: %s\n",p_alert.utf8().get_data());
  180. if (alert_func)
  181. alert_func(p_alert, p_title);
  182. }
  183. void OS_Android::set_mouse_show(bool p_show) {
  184. //android has no mouse...
  185. }
  186. void OS_Android::set_mouse_grab(bool p_grab) {
  187. //it really has no mouse...!
  188. }
  189. bool OS_Android::is_mouse_grab_enabled() const {
  190. //*sigh* technology has evolved so much since i was a kid..
  191. return false;
  192. }
  193. Point2 OS_Android::get_mouse_pos() const {
  194. return Point2();
  195. }
  196. int OS_Android::get_mouse_button_state() const {
  197. return 0;
  198. }
  199. void OS_Android::set_window_title(const String& p_title) {
  200. }
  201. //interesting byt not yet
  202. //void set_clipboard(const String& p_text);
  203. //String get_clipboard() const;
  204. void OS_Android::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
  205. }
  206. OS::VideoMode OS_Android::get_video_mode(int p_screen) const {
  207. return default_videomode;
  208. }
  209. void OS_Android::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  210. p_list->push_back(default_videomode);
  211. }
  212. void OS_Android::set_keep_screen_on(bool p_enabled) {
  213. OS::set_keep_screen_on(p_enabled);
  214. if (set_keep_screen_on_func) {
  215. set_keep_screen_on_func(p_enabled);
  216. }
  217. }
  218. Size2 OS_Android::get_window_size() const {
  219. return Vector2(default_videomode.width,default_videomode.height);
  220. }
  221. String OS_Android::get_name() {
  222. return "Android";
  223. }
  224. MainLoop *OS_Android::get_main_loop() const {
  225. return main_loop;
  226. }
  227. bool OS_Android::can_draw() const {
  228. return true; //always?
  229. }
  230. void OS_Android::set_cursor_shape(CursorShape p_shape) {
  231. //android really really really has no mouse.. how amazing..
  232. }
  233. void OS_Android::main_loop_begin() {
  234. if (main_loop)
  235. main_loop->init();
  236. }
  237. bool OS_Android::main_loop_iterate() {
  238. if (!main_loop)
  239. return false;
  240. return Main::iteration();
  241. }
  242. void OS_Android::main_loop_end() {
  243. if (main_loop)
  244. main_loop->finish();
  245. }
  246. void OS_Android::main_loop_focusout() {
  247. if (main_loop)
  248. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  249. audio_driver_android.set_pause(true);
  250. }
  251. void OS_Android::main_loop_focusin(){
  252. if (main_loop)
  253. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  254. audio_driver_android.set_pause(false);
  255. }
  256. void OS_Android::process_joy_event(OS_Android::JoystickEvent p_event) {
  257. switch (p_event.type) {
  258. case JOY_EVENT_BUTTON:
  259. last_id = input->joy_button(last_id, p_event.device, p_event.index, p_event.pressed);
  260. break;
  261. case JOY_EVENT_AXIS:
  262. InputDefault::JoyAxis value;
  263. value.min = -1;
  264. value.value = p_event.value;
  265. last_id = input->joy_axis(last_id, p_event.device, p_event.index, value);
  266. break;
  267. case JOY_EVENT_HAT:
  268. last_id = input->joy_hat(last_id, p_event.device, p_event.hat);
  269. break;
  270. default:
  271. return;
  272. }
  273. }
  274. void OS_Android::process_event(InputEvent p_event) {
  275. p_event.ID = last_id++;
  276. input->parse_input_event(p_event);
  277. }
  278. void OS_Android::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
  279. // print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  280. switch(p_what) {
  281. case 0: { //gesture begin
  282. if (touch.size()) {
  283. //end all if exist
  284. InputEvent ev;
  285. ev.type=InputEvent::MOUSE_BUTTON;
  286. ev.ID=last_id++;
  287. ev.mouse_button.button_index=BUTTON_LEFT;
  288. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  289. ev.mouse_button.pressed=false;
  290. ev.mouse_button.x=touch[0].pos.x;
  291. ev.mouse_button.y=touch[0].pos.y;
  292. ev.mouse_button.global_x=touch[0].pos.x;
  293. ev.mouse_button.global_y=touch[0].pos.y;
  294. input->parse_input_event(ev);
  295. for(int i=0;i<touch.size();i++) {
  296. InputEvent ev;
  297. ev.type=InputEvent::SCREEN_TOUCH;
  298. ev.ID=last_id++;
  299. ev.screen_touch.index=touch[i].id;
  300. ev.screen_touch.pressed=false;
  301. ev.screen_touch.x=touch[i].pos.x;
  302. ev.screen_touch.y=touch[i].pos.y;
  303. input->parse_input_event(ev);
  304. }
  305. }
  306. touch.resize(p_points.size());
  307. for(int i=0;i<p_points.size();i++) {
  308. touch[i].id=p_points[i].id;
  309. touch[i].pos=p_points[i].pos;
  310. }
  311. {
  312. //send mouse
  313. InputEvent ev;
  314. ev.type=InputEvent::MOUSE_BUTTON;
  315. ev.ID=last_id++;
  316. ev.mouse_button.button_index=BUTTON_LEFT;
  317. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  318. ev.mouse_button.pressed=true;
  319. ev.mouse_button.x=touch[0].pos.x;
  320. ev.mouse_button.y=touch[0].pos.y;
  321. ev.mouse_button.global_x=touch[0].pos.x;
  322. ev.mouse_button.global_y=touch[0].pos.y;
  323. last_mouse=touch[0].pos;
  324. input->parse_input_event(ev);
  325. }
  326. //send touch
  327. for(int i=0;i<touch.size();i++) {
  328. InputEvent ev;
  329. ev.type=InputEvent::SCREEN_TOUCH;
  330. ev.ID=last_id++;
  331. ev.screen_touch.index=touch[i].id;
  332. ev.screen_touch.pressed=true;
  333. ev.screen_touch.x=touch[i].pos.x;
  334. ev.screen_touch.y=touch[i].pos.y;
  335. input->parse_input_event(ev);
  336. }
  337. } break;
  338. case 1: { //motion
  339. if (p_points.size()) {
  340. //send mouse, should look for point 0?
  341. InputEvent ev;
  342. ev.type=InputEvent::MOUSE_MOTION;
  343. ev.ID=last_id++;
  344. ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
  345. ev.mouse_motion.x=p_points[0].pos.x;
  346. ev.mouse_motion.y=p_points[0].pos.y;
  347. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  348. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  349. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  350. ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
  351. ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
  352. last_mouse=p_points[0].pos;
  353. input->parse_input_event(ev);
  354. }
  355. ERR_FAIL_COND(touch.size()!=p_points.size());
  356. for(int i=0;i<touch.size();i++) {
  357. int idx=-1;
  358. for(int j=0;j<p_points.size();j++) {
  359. if (touch[i].id==p_points[j].id) {
  360. idx=j;
  361. break;
  362. }
  363. }
  364. ERR_CONTINUE(idx==-1);
  365. if (touch[i].pos==p_points[idx].pos)
  366. continue; //no move unncesearily
  367. InputEvent ev;
  368. ev.type=InputEvent::SCREEN_DRAG;
  369. ev.ID=last_id++;
  370. ev.screen_drag.index=touch[i].id;
  371. ev.screen_drag.x=p_points[idx].pos.x;
  372. ev.screen_drag.y=p_points[idx].pos.y;
  373. ev.screen_drag.relative_x=p_points[idx].pos.x - touch[i].pos.x;
  374. ev.screen_drag.relative_y=p_points[idx].pos.y - touch[i].pos.y;
  375. input->parse_input_event(ev);
  376. touch[i].pos=p_points[idx].pos;
  377. }
  378. } break;
  379. case 2: { //release
  380. if (touch.size()) {
  381. //end all if exist
  382. InputEvent ev;
  383. ev.type=InputEvent::MOUSE_BUTTON;
  384. ev.ID=last_id++;
  385. ev.mouse_button.button_index=BUTTON_LEFT;
  386. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  387. ev.mouse_button.pressed=false;
  388. ev.mouse_button.x=touch[0].pos.x;
  389. ev.mouse_button.y=touch[0].pos.y;
  390. ev.mouse_button.global_x=touch[0].pos.x;
  391. ev.mouse_button.global_y=touch[0].pos.y;
  392. input->parse_input_event(ev);
  393. for(int i=0;i<touch.size();i++) {
  394. InputEvent ev;
  395. ev.type=InputEvent::SCREEN_TOUCH;
  396. ev.ID=last_id++;
  397. ev.screen_touch.index=touch[i].id;
  398. ev.screen_touch.pressed=false;
  399. ev.screen_touch.x=touch[i].pos.x;
  400. ev.screen_touch.y=touch[i].pos.y;
  401. input->parse_input_event(ev);
  402. }
  403. touch.clear();
  404. }
  405. } break;
  406. case 3: { // add tuchi
  407. ERR_FAIL_INDEX(p_pointer,p_points.size());
  408. TouchPos tp=p_points[p_pointer];
  409. touch.push_back(tp);
  410. InputEvent ev;
  411. ev.type=InputEvent::SCREEN_TOUCH;
  412. ev.ID=last_id++;
  413. ev.screen_touch.index=tp.id;
  414. ev.screen_touch.pressed=true;
  415. ev.screen_touch.x=tp.pos.x;
  416. ev.screen_touch.y=tp.pos.y;
  417. input->parse_input_event(ev);
  418. } break;
  419. case 4: {
  420. for(int i=0;i<touch.size();i++) {
  421. if (touch[i].id==p_pointer) {
  422. InputEvent ev;
  423. ev.type=InputEvent::SCREEN_TOUCH;
  424. ev.ID=last_id++;
  425. ev.screen_touch.index=touch[i].id;
  426. ev.screen_touch.pressed=false;
  427. ev.screen_touch.x=touch[i].pos.x;
  428. ev.screen_touch.y=touch[i].pos.y;
  429. input->parse_input_event(ev);
  430. touch.remove(i);
  431. i--;
  432. }
  433. }
  434. } break;
  435. }
  436. }
  437. void OS_Android::process_accelerometer(const Vector3& p_accelerometer) {
  438. input->set_accelerometer(p_accelerometer);
  439. }
  440. void OS_Android::process_magnetometer(const Vector3& p_magnetometer) {
  441. input->set_magnetometer(p_magnetometer);
  442. }
  443. void OS_Android::process_gyroscope(const Vector3& p_gyroscope) {
  444. input->set_gyroscope(p_gyroscope);
  445. }
  446. bool OS_Android::has_touchscreen_ui_hint() const {
  447. return true;
  448. }
  449. bool OS_Android::has_virtual_keyboard() const {
  450. return true;
  451. }
  452. void OS_Android::show_virtual_keyboard(const String& p_existing_text,const Rect2& p_screen_rect) {
  453. if (show_virtual_keyboard_func) {
  454. show_virtual_keyboard_func(p_existing_text);
  455. } else {
  456. ERR_PRINT("Virtual keyboard not available");
  457. };
  458. }
  459. void OS_Android::hide_virtual_keyboard() {
  460. if (hide_virtual_keyboard_func) {
  461. hide_virtual_keyboard_func();
  462. } else {
  463. ERR_PRINT("Virtual keyboard not available");
  464. };
  465. }
  466. void OS_Android::init_video_mode(int p_video_width,int p_video_height) {
  467. default_videomode.width=p_video_width;
  468. default_videomode.height=p_video_height;
  469. default_videomode.fullscreen=true;
  470. default_videomode.resizable=false;
  471. }
  472. void OS_Android::main_loop_request_quit() {
  473. if (main_loop)
  474. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  475. }
  476. void OS_Android::set_display_size(Size2 p_size) {
  477. default_videomode.width=p_size.x;
  478. default_videomode.height=p_size.y;
  479. }
  480. void OS_Android::reload_gfx() {
  481. if (gfx_init_func)
  482. gfx_init_func(gfx_init_ud,use_gl2);
  483. if (rasterizer)
  484. rasterizer->reload_vram();
  485. }
  486. Error OS_Android::shell_open(String p_uri) {
  487. if (open_uri_func)
  488. return open_uri_func(p_uri)?ERR_CANT_OPEN:OK;
  489. return ERR_UNAVAILABLE;
  490. }
  491. String OS_Android::get_resource_dir() const {
  492. return "/"; //android has it's own filesystem for resources inside the APK
  493. }
  494. String OS_Android::get_locale() const {
  495. if (get_locale_func)
  496. return get_locale_func();
  497. return OS_Unix::get_locale();
  498. }
  499. String OS_Android::get_model_name() const {
  500. if (get_model_func)
  501. return get_model_func();
  502. return OS_Unix::get_model_name();
  503. }
  504. int OS_Android::get_screen_dpi(int p_screen) const {
  505. if (get_screen_dpi_func) {
  506. return get_screen_dpi_func();
  507. }
  508. return 160;
  509. }
  510. void OS_Android::set_need_reload_hooks(bool p_needs_them) {
  511. use_reload_hooks=p_needs_them;
  512. }
  513. String OS_Android::get_data_dir() const {
  514. if (data_dir_cache!=String())
  515. return data_dir_cache;
  516. if (get_data_dir_func) {
  517. String data_dir=get_data_dir_func();
  518. //store current dir
  519. char real_current_dir_name[2048];
  520. getcwd(real_current_dir_name,2048);
  521. //go to data dir
  522. chdir(data_dir.utf8().get_data());
  523. //get actual data dir, so we resolve potential symlink (Android 6.0+ seems to use symlink)
  524. char data_current_dir_name[2048];
  525. getcwd(data_current_dir_name,2048);
  526. //cache by parsing utf8
  527. data_dir_cache.parse_utf8(data_current_dir_name);
  528. //restore original dir so we don't mess things up
  529. chdir(real_current_dir_name);
  530. return data_dir_cache;
  531. }
  532. return ".";
  533. //return GlobalConfig::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  534. }
  535. void OS_Android::set_screen_orientation(ScreenOrientation p_orientation) {
  536. if (set_screen_orientation_func)
  537. set_screen_orientation_func(p_orientation);
  538. }
  539. String OS_Android::get_unique_ID() const {
  540. if (get_unique_id_func)
  541. return get_unique_id_func();
  542. return OS::get_unique_ID();
  543. }
  544. Error OS_Android::native_video_play(String p_path, float p_volume) {
  545. if (video_play_func)
  546. video_play_func(p_path);
  547. return OK;
  548. }
  549. bool OS_Android::native_video_is_playing() {
  550. if (video_is_playing_func)
  551. return video_is_playing_func();
  552. return false;
  553. }
  554. void OS_Android::native_video_pause() {
  555. if (video_pause_func)
  556. video_pause_func();
  557. }
  558. String OS_Android::get_system_dir(SystemDir p_dir) const {
  559. if (get_system_dir_func)
  560. return get_system_dir_func(p_dir);
  561. return String(".");
  562. }
  563. void OS_Android::native_video_stop() {
  564. if (video_stop_func)
  565. video_stop_func();
  566. }
  567. void OS_Android::set_context_is_16_bits(bool p_is_16) {
  568. use_16bits_fbo=p_is_16;
  569. if (rasterizer)
  570. rasterizer->set_force_16_bits_fbo(p_is_16);
  571. }
  572. void OS_Android::joy_connection_changed(int p_device, bool p_connected, String p_name) {
  573. return input->joy_connection_changed(p_device, p_connected, p_name, "");
  574. }
  575. bool OS_Android::is_joy_known(int p_device) {
  576. return input->is_joy_mapped(p_device);
  577. }
  578. String OS_Android::get_joy_guid(int p_device) const {
  579. return input->get_joy_guid_remapped(p_device);
  580. }
  581. 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) {
  582. use_apk_expansion=p_use_apk_expansion;
  583. default_videomode.width=800;
  584. default_videomode.height=600;
  585. default_videomode.fullscreen=true;
  586. default_videomode.resizable=false;
  587. gfx_init_func=p_gfx_init_func;
  588. gfx_init_ud=p_gfx_init_ud;
  589. main_loop=NULL;
  590. last_id=1;
  591. gl_extensions=NULL;
  592. rasterizer=NULL;
  593. use_gl2=false;
  594. open_uri_func=p_open_uri_func;
  595. get_data_dir_func=p_get_data_dir_func;
  596. get_locale_func=p_get_locale_func;
  597. get_model_func=p_get_model_func;
  598. get_screen_dpi_func = p_get_screen_dpi_func;
  599. get_unique_id_func=p_get_unique_id;
  600. get_system_dir_func=p_get_sdir_func;
  601. video_play_func = p_video_play_func;
  602. video_is_playing_func = p_video_is_playing_func;
  603. video_pause_func = p_video_pause_func;
  604. video_stop_func = p_video_stop_func;
  605. show_virtual_keyboard_func = p_show_vk;
  606. hide_virtual_keyboard_func = p_hide_vk;
  607. set_screen_orientation_func=p_screen_orient;
  608. set_keep_screen_on_func = p_set_keep_screen_on_func;
  609. alert_func = p_alert_func;
  610. use_reload_hooks=false;
  611. }
  612. OS_Android::~OS_Android() {
  613. }