os_iphone.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*************************************************************************/
  2. /* os_iphone.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. #ifdef IPHONE_ENABLED
  31. #include "os_iphone.h"
  32. #include "drivers/gles2/rasterizer_gles2.h"
  33. #include "drivers/gles3/rasterizer_gles3.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 "core/io/file_access_pack.h"
  38. #include "core/os/dir_access.h"
  39. #include "core/os/file_access.h"
  40. #include "core/project_settings.h"
  41. #include "drivers/unix/syslog_logger.h"
  42. #include "sem_iphone.h"
  43. #include "ios.h"
  44. #include <dlfcn.h>
  45. int OSIPhone::get_video_driver_count() const {
  46. return 2;
  47. };
  48. const char *OSIPhone::get_video_driver_name(int p_driver) const {
  49. switch (p_driver) {
  50. case VIDEO_DRIVER_GLES3:
  51. return "GLES3";
  52. case VIDEO_DRIVER_GLES2:
  53. return "GLES2";
  54. }
  55. ERR_EXPLAIN("Invalid video driver index " + itos(p_driver));
  56. ERR_FAIL_V(NULL);
  57. };
  58. OSIPhone *OSIPhone::get_singleton() {
  59. return (OSIPhone *)OS::get_singleton();
  60. };
  61. extern int gl_view_base_fb; // from gl_view.mm
  62. void OSIPhone::set_data_dir(String p_dir) {
  63. DirAccess *da = DirAccess::open(p_dir);
  64. data_dir = da->get_current_dir();
  65. printf("setting data dir to %ls from %ls\n", data_dir.c_str(), p_dir.c_str());
  66. memdelete(da);
  67. };
  68. void OSIPhone::set_unique_id(String p_ID) {
  69. unique_ID = p_ID;
  70. };
  71. String OSIPhone::get_unique_id() const {
  72. return unique_ID;
  73. };
  74. void OSIPhone::initialize_core() {
  75. OS_Unix::initialize_core();
  76. SemaphoreIphone::make_default();
  77. set_data_dir(data_dir);
  78. };
  79. int OSIPhone::get_current_video_driver() const {
  80. return video_driver_index;
  81. }
  82. extern bool gles3_available; // from gl_view.mm
  83. Error OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  84. bool use_gl3 = GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES3";
  85. bool gl_initialization_error = false;
  86. while (true) {
  87. if (use_gl3) {
  88. if (RasterizerGLES3::is_viable() == OK && gles3_available) {
  89. RasterizerGLES3::register_config();
  90. RasterizerGLES3::make_current();
  91. break;
  92. } else {
  93. if (GLOBAL_GET("rendering/quality/driver/driver_fallback") == "Best") {
  94. p_video_driver = VIDEO_DRIVER_GLES2;
  95. use_gl3 = false;
  96. continue;
  97. } else {
  98. gl_initialization_error = true;
  99. break;
  100. }
  101. }
  102. } else {
  103. if (RasterizerGLES2::is_viable() == OK) {
  104. RasterizerGLES2::register_config();
  105. RasterizerGLES2::make_current();
  106. break;
  107. } else {
  108. gl_initialization_error = true;
  109. break;
  110. }
  111. }
  112. }
  113. if (gl_initialization_error) {
  114. OS::get_singleton()->alert("Your device does not support any of the supported OpenGL versions.",
  115. "Unable to initialize Video driver");
  116. return ERR_UNAVAILABLE;
  117. }
  118. video_driver_index = p_video_driver;
  119. visual_server = memnew(VisualServerRaster);
  120. // FIXME: Reimplement threaded rendering
  121. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  122. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  123. }
  124. visual_server->init();
  125. //visual_server->cursor_set_visible(false, 0);
  126. // reset this to what it should be, it will have been set to 0 after visual_server->init() is called
  127. if (use_gl3)
  128. RasterizerStorageGLES3::system_fbo = gl_view_base_fb;
  129. else
  130. RasterizerStorageGLES2::system_fbo = gl_view_base_fb;
  131. AudioDriverManager::initialize(p_audio_driver);
  132. input = memnew(InputDefault);
  133. #ifdef GAME_CENTER_ENABLED
  134. game_center = memnew(GameCenter);
  135. Engine::get_singleton()->add_singleton(Engine::Singleton("GameCenter", game_center));
  136. game_center->connect();
  137. #endif
  138. #ifdef STOREKIT_ENABLED
  139. store_kit = memnew(InAppStore);
  140. Engine::get_singleton()->add_singleton(Engine::Singleton("InAppStore", store_kit));
  141. #endif
  142. #ifdef ICLOUD_ENABLED
  143. icloud = memnew(ICloud);
  144. Engine::get_singleton()->add_singleton(Engine::Singleton("ICloud", icloud));
  145. //icloud->connect();
  146. #endif
  147. Engine::get_singleton()->add_singleton(Engine::Singleton("iOS", memnew(iOS)));
  148. return OK;
  149. };
  150. MainLoop *OSIPhone::get_main_loop() const {
  151. return main_loop;
  152. };
  153. void OSIPhone::set_main_loop(MainLoop *p_main_loop) {
  154. main_loop = p_main_loop;
  155. if (main_loop) {
  156. input->set_main_loop(p_main_loop);
  157. main_loop->init();
  158. }
  159. };
  160. bool OSIPhone::iterate() {
  161. if (!main_loop)
  162. return true;
  163. if (main_loop) {
  164. for (int i = 0; i < event_count; i++) {
  165. input->parse_input_event(event_queue[i]);
  166. };
  167. };
  168. event_count = 0;
  169. return Main::iteration();
  170. };
  171. void OSIPhone::key(uint32_t p_key, bool p_pressed) {
  172. Ref<InputEventKey> ev;
  173. ev.instance();
  174. ev->set_echo(false);
  175. ev->set_pressed(p_pressed);
  176. ev->set_scancode(p_key);
  177. ev->set_unicode(p_key);
  178. queue_event(ev);
  179. };
  180. void OSIPhone::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick) {
  181. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  182. Ref<InputEventScreenTouch> ev;
  183. ev.instance();
  184. ev->set_index(p_idx);
  185. ev->set_pressed(p_pressed);
  186. ev->set_position(Vector2(p_x, p_y));
  187. queue_event(ev);
  188. };
  189. touch_list.pressed[p_idx] = p_pressed;
  190. };
  191. void OSIPhone::touch_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y) {
  192. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  193. Ref<InputEventScreenDrag> ev;
  194. ev.instance();
  195. ev->set_index(p_idx);
  196. ev->set_position(Vector2(p_x, p_y));
  197. ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
  198. queue_event(ev);
  199. };
  200. };
  201. void OSIPhone::queue_event(const Ref<InputEvent> &p_event) {
  202. ERR_FAIL_INDEX(event_count, MAX_EVENTS);
  203. event_queue[event_count++] = p_event;
  204. };
  205. void OSIPhone::touches_cancelled() {
  206. for (int i = 0; i < MAX_MOUSE_COUNT; i++) {
  207. if (touch_list.pressed[i]) {
  208. // send a mouse_up outside the screen
  209. touch_press(i, -1, -1, false, false);
  210. };
  211. };
  212. };
  213. static const float ACCEL_RANGE = 1;
  214. void OSIPhone::update_gravity(float p_x, float p_y, float p_z) {
  215. input->set_gravity(Vector3(p_x, p_y, p_z));
  216. };
  217. void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {
  218. // Found out the Z should not be negated! Pass as is!
  219. input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, p_z / (float)ACCEL_RANGE));
  220. /*
  221. if (p_x != last_accel.x) {
  222. //printf("updating accel x %f\n", p_x);
  223. InputEvent ev;
  224. ev.type = InputEvent::JOYPAD_MOTION;
  225. ev.device = 0;
  226. ev.joy_motion.axis = JOY_ANALOG_0;
  227. ev.joy_motion.axis_value = (p_x / (float)ACCEL_RANGE);
  228. last_accel.x = p_x;
  229. queue_event(ev);
  230. };
  231. if (p_y != last_accel.y) {
  232. //printf("updating accel y %f\n", p_y);
  233. InputEvent ev;
  234. ev.type = InputEvent::JOYPAD_MOTION;
  235. ev.device = 0;
  236. ev.joy_motion.axis = JOY_ANALOG_1;
  237. ev.joy_motion.axis_value = (p_y / (float)ACCEL_RANGE);
  238. last_accel.y = p_y;
  239. queue_event(ev);
  240. };
  241. if (p_z != last_accel.z) {
  242. //printf("updating accel z %f\n", p_z);
  243. InputEvent ev;
  244. ev.type = InputEvent::JOYPAD_MOTION;
  245. ev.device = 0;
  246. ev.joy_motion.axis = JOY_ANALOG_2;
  247. ev.joy_motion.axis_value = ( (1.0 - p_z) / (float)ACCEL_RANGE);
  248. last_accel.z = p_z;
  249. queue_event(ev);
  250. };
  251. */
  252. };
  253. void OSIPhone::update_magnetometer(float p_x, float p_y, float p_z) {
  254. input->set_magnetometer(Vector3(p_x, p_y, p_z));
  255. };
  256. void OSIPhone::update_gyroscope(float p_x, float p_y, float p_z) {
  257. input->set_gyroscope(Vector3(p_x, p_y, p_z));
  258. };
  259. int OSIPhone::get_unused_joy_id() {
  260. return input->get_unused_joy_id();
  261. };
  262. void OSIPhone::joy_connection_changed(int p_idx, bool p_connected, String p_name) {
  263. input->joy_connection_changed(p_idx, p_connected, p_name);
  264. };
  265. void OSIPhone::joy_button(int p_device, int p_button, bool p_pressed) {
  266. input->joy_button(p_device, p_button, p_pressed);
  267. };
  268. void OSIPhone::joy_axis(int p_device, int p_axis, const InputDefault::JoyAxis &p_value) {
  269. input->joy_axis(p_device, p_axis, p_value);
  270. };
  271. void OSIPhone::delete_main_loop() {
  272. if (main_loop) {
  273. main_loop->finish();
  274. memdelete(main_loop);
  275. };
  276. main_loop = NULL;
  277. };
  278. void OSIPhone::finalize() {
  279. if (main_loop) // should not happen?
  280. memdelete(main_loop);
  281. visual_server->finish();
  282. memdelete(visual_server);
  283. // memdelete(rasterizer);
  284. memdelete(input);
  285. };
  286. void OSIPhone::set_mouse_show(bool p_show){};
  287. void OSIPhone::set_mouse_grab(bool p_grab){};
  288. bool OSIPhone::is_mouse_grab_enabled() const {
  289. return true;
  290. };
  291. Point2 OSIPhone::get_mouse_position() const {
  292. return Point2();
  293. };
  294. int OSIPhone::get_mouse_button_state() const {
  295. return 0;
  296. };
  297. void OSIPhone::set_window_title(const String &p_title){};
  298. void OSIPhone::alert(const String &p_alert, const String &p_title) {
  299. const CharString utf8_alert = p_alert.utf8();
  300. const CharString utf8_title = p_title.utf8();
  301. iOS::alert(utf8_alert.get_data(), utf8_title.get_data());
  302. }
  303. Error OSIPhone::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  304. if (p_path.length() == 0) {
  305. p_library_handle = RTLD_SELF;
  306. return OK;
  307. }
  308. return OS_Unix::open_dynamic_library(p_path, p_library_handle, p_also_set_library_path);
  309. }
  310. Error OSIPhone::close_dynamic_library(void *p_library_handle) {
  311. if (p_library_handle == RTLD_SELF) {
  312. return OK;
  313. }
  314. return OS_Unix::close_dynamic_library(p_library_handle);
  315. }
  316. HashMap<String, void *> OSIPhone::dynamic_symbol_lookup_table;
  317. void register_dynamic_symbol(char *name, void *address) {
  318. OSIPhone::dynamic_symbol_lookup_table[String(name)] = address;
  319. }
  320. Error OSIPhone::get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional) {
  321. if (p_library_handle == RTLD_SELF) {
  322. void **ptr = OSIPhone::dynamic_symbol_lookup_table.getptr(p_name);
  323. if (ptr) {
  324. p_symbol_handle = *ptr;
  325. return OK;
  326. }
  327. }
  328. return OS_Unix::get_dynamic_library_symbol_handle(p_library_handle, p_name, p_symbol_handle, p_optional);
  329. }
  330. void OSIPhone::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  331. video_mode = p_video_mode;
  332. };
  333. OS::VideoMode OSIPhone::get_video_mode(int p_screen) const {
  334. return video_mode;
  335. };
  336. void OSIPhone::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  337. p_list->push_back(video_mode);
  338. };
  339. bool OSIPhone::can_draw() const {
  340. if (native_video_is_playing())
  341. return false;
  342. return true;
  343. };
  344. int OSIPhone::set_base_framebuffer(int p_fb) {
  345. // gl_view_base_fb has not been updated yet
  346. RasterizerStorageGLES3::system_fbo = p_fb;
  347. return 0;
  348. };
  349. bool OSIPhone::has_virtual_keyboard() const {
  350. return true;
  351. };
  352. extern void _show_keyboard(String p_existing);
  353. extern void _hide_keyboard();
  354. extern Error _shell_open(String p_uri);
  355. extern void _set_keep_screen_on(bool p_enabled);
  356. void OSIPhone::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  357. _show_keyboard(p_existing_text);
  358. };
  359. void OSIPhone::hide_virtual_keyboard() {
  360. _hide_keyboard();
  361. };
  362. void OSIPhone::set_virtual_keyboard_height(int p_height) {
  363. virtual_keyboard_height = p_height;
  364. }
  365. int OSIPhone::get_virtual_keyboard_height() const {
  366. return virtual_keyboard_height;
  367. }
  368. Error OSIPhone::shell_open(String p_uri) {
  369. return _shell_open(p_uri);
  370. };
  371. void OSIPhone::set_keep_screen_on(bool p_enabled) {
  372. OS::set_keep_screen_on(p_enabled);
  373. _set_keep_screen_on(p_enabled);
  374. };
  375. void OSIPhone::set_cursor_shape(CursorShape p_shape){
  376. };
  377. String OSIPhone::get_user_data_dir() const {
  378. return data_dir;
  379. };
  380. void OSIPhone::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot){};
  381. String OSIPhone::get_name() {
  382. return "iOS";
  383. };
  384. Size2 OSIPhone::get_window_size() const {
  385. return Vector2(video_mode.width, video_mode.height);
  386. }
  387. extern Rect2 _get_ios_window_safe_area(float p_window_width, float p_window_height);
  388. Rect2 OSIPhone::get_window_safe_area() const {
  389. return _get_ios_window_safe_area(video_mode.width, video_mode.height);
  390. }
  391. bool OSIPhone::has_touchscreen_ui_hint() const {
  392. return true;
  393. }
  394. void OSIPhone::set_locale(String p_locale) {
  395. locale_code = p_locale;
  396. }
  397. String OSIPhone::get_locale() const {
  398. return locale_code;
  399. }
  400. extern bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  401. extern bool _is_video_playing();
  402. extern void _pause_video();
  403. extern void _unpause_video();
  404. extern void _stop_video();
  405. extern void _focus_out_video();
  406. Error OSIPhone::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  407. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  408. bool exists = f && f->is_open();
  409. String tempFile = get_user_data_dir();
  410. if (!exists)
  411. return FAILED;
  412. if (p_path.begins_with("res://")) {
  413. if (PackedData::get_singleton()->has_path(p_path)) {
  414. print("Unable to play %S using the native player as it resides in a .pck file\n", p_path.c_str());
  415. return ERR_INVALID_PARAMETER;
  416. } else {
  417. p_path = p_path.replace("res:/", ProjectSettings::get_singleton()->get_resource_path());
  418. }
  419. } else if (p_path.begins_with("user://"))
  420. p_path = p_path.replace("user:/", get_user_data_dir());
  421. memdelete(f);
  422. print("Playing video: %S\n", p_path.c_str());
  423. if (_play_video(p_path, p_volume, p_audio_track, p_subtitle_track))
  424. return OK;
  425. return FAILED;
  426. }
  427. bool OSIPhone::native_video_is_playing() const {
  428. return _is_video_playing();
  429. }
  430. void OSIPhone::native_video_pause() {
  431. if (native_video_is_playing())
  432. _pause_video();
  433. }
  434. void OSIPhone::native_video_unpause() {
  435. _unpause_video();
  436. };
  437. void OSIPhone::native_video_focus_out() {
  438. _focus_out_video();
  439. };
  440. void OSIPhone::native_video_stop() {
  441. if (native_video_is_playing())
  442. _stop_video();
  443. }
  444. bool OSIPhone::_check_internal_feature_support(const String &p_feature) {
  445. return p_feature == "mobile" || p_feature == "etc" || p_feature == "pvrtc" || p_feature == "etc2";
  446. }
  447. // Initialization order between compilation units is not guaranteed,
  448. // so we use this as a hack to ensure certain code is called before
  449. // everything else, but after all units are initialized.
  450. typedef void (*init_callback)();
  451. static init_callback *ios_init_callbacks = NULL;
  452. static int ios_init_callbacks_count = 0;
  453. static int ios_init_callbacks_capacity = 0;
  454. void add_ios_init_callback(init_callback cb) {
  455. if (ios_init_callbacks_count == ios_init_callbacks_capacity) {
  456. void *new_ptr = realloc(ios_init_callbacks, sizeof(cb) * 32);
  457. if (new_ptr) {
  458. ios_init_callbacks = (init_callback *)(new_ptr);
  459. ios_init_callbacks_capacity += 32;
  460. }
  461. }
  462. if (ios_init_callbacks_capacity > ios_init_callbacks_count) {
  463. ios_init_callbacks[ios_init_callbacks_count] = cb;
  464. ++ios_init_callbacks_count;
  465. }
  466. }
  467. OSIPhone::OSIPhone(int width, int height, String p_data_dir) {
  468. for (int i = 0; i < ios_init_callbacks_count; ++i) {
  469. ios_init_callbacks[i]();
  470. }
  471. free(ios_init_callbacks);
  472. ios_init_callbacks = NULL;
  473. ios_init_callbacks_count = 0;
  474. ios_init_callbacks_capacity = 0;
  475. main_loop = NULL;
  476. visual_server = NULL;
  477. VideoMode vm;
  478. vm.fullscreen = true;
  479. vm.width = width;
  480. vm.height = height;
  481. vm.resizable = false;
  482. set_video_mode(vm);
  483. event_count = 0;
  484. virtual_keyboard_height = 0;
  485. // can't call set_data_dir from here, since it requires DirAccess
  486. // which is initialized in initialize_core
  487. data_dir = p_data_dir;
  488. Vector<Logger *> loggers;
  489. loggers.push_back(memnew(SyslogLogger));
  490. #ifdef DEBUG_ENABLED
  491. // it seems iOS app's stdout/stderr is only obtainable if you launch it from Xcode
  492. loggers.push_back(memnew(StdLogger));
  493. #endif
  494. _set_logger(memnew(CompositeLogger(loggers)));
  495. AudioDriverManager::add_driver(&audio_driver);
  496. };
  497. OSIPhone::~OSIPhone() {
  498. }
  499. #endif