os_iphone.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*************************************************************************/
  2. /* os_iphone.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef IPHONE_ENABLED
  31. #include "os_iphone.h"
  32. #if defined(OPENGL_ENABLED)
  33. #include "drivers/gles2/rasterizer_gles2.h"
  34. #endif
  35. #if defined(VULKAN_ENABLED)
  36. #include "servers/visual/rasterizer_rd/rasterizer_rd.h"
  37. // #import <QuartzCore/CAMetalLayer.h>
  38. #include <vulkan/vulkan_metal.h>
  39. #endif
  40. #include "servers/visual/visual_server_raster.h"
  41. #include "servers/visual/visual_server_wrap_mt.h"
  42. #include "main/main.h"
  43. #include "core/io/file_access_pack.h"
  44. #include "core/os/dir_access.h"
  45. #include "core/os/file_access.h"
  46. #include "core/project_settings.h"
  47. #include "drivers/unix/syslog_logger.h"
  48. #include "semaphore_iphone.h"
  49. #include <dlfcn.h>
  50. int OSIPhone::get_video_driver_count() const {
  51. return 2;
  52. };
  53. const char *OSIPhone::get_video_driver_name(int p_driver) const {
  54. switch (p_driver) {
  55. case VIDEO_DRIVER_GLES2:
  56. return "GLES2";
  57. }
  58. ERR_FAIL_V_MSG(NULL, "Invalid video driver index: " + itos(p_driver) + ".");
  59. };
  60. OSIPhone *OSIPhone::get_singleton() {
  61. return (OSIPhone *)OS::get_singleton();
  62. };
  63. extern int gl_view_base_fb; // from gl_view.mm
  64. void OSIPhone::set_data_dir(String p_dir) {
  65. DirAccess *da = DirAccess::open(p_dir);
  66. data_dir = da->get_current_dir();
  67. printf("setting data dir to %ls from %ls\n", data_dir.c_str(), p_dir.c_str());
  68. memdelete(da);
  69. };
  70. void OSIPhone::set_unique_id(String p_id) {
  71. unique_id = p_id;
  72. };
  73. String OSIPhone::get_unique_id() const {
  74. return unique_id;
  75. };
  76. void OSIPhone::initialize_core() {
  77. OS_Unix::initialize_core();
  78. SemaphoreIphone::make_default();
  79. set_data_dir(data_dir);
  80. };
  81. int OSIPhone::get_current_video_driver() const {
  82. return video_driver_index;
  83. }
  84. Error OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  85. video_driver_index = p_video_driver;
  86. #if defined(OPENGL_ENABLED)
  87. bool gl_initialization_error = false;
  88. // FIXME: Add Vulkan support via MoltenVK. Add fallback code back?
  89. if (RasterizerGLES2::is_viable() == OK) {
  90. RasterizerGLES2::register_config();
  91. RasterizerGLES2::make_current();
  92. } else {
  93. gl_initialization_error = true;
  94. }
  95. if (gl_initialization_error) {
  96. OS::get_singleton()->alert("Your device does not support any of the supported OpenGL versions.",
  97. "Unable to initialize video driver");
  98. return ERR_UNAVAILABLE;
  99. }
  100. #endif
  101. #if defined(VULKAN_ENABLED)
  102. RasterizerRD::make_current();
  103. #endif
  104. visual_server = memnew(VisualServerRaster);
  105. // FIXME: Reimplement threaded rendering
  106. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  107. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  108. }
  109. visual_server->init();
  110. //visual_server->cursor_set_visible(false, 0);
  111. #if defined(OPENGL_ENABLED)
  112. // reset this to what it should be, it will have been set to 0 after visual_server->init() is called
  113. RasterizerStorageGLES2::system_fbo = gl_view_base_fb;
  114. #endif
  115. AudioDriverManager::initialize(p_audio_driver);
  116. input = memnew(InputDefault);
  117. #ifdef GAME_CENTER_ENABLED
  118. game_center = memnew(GameCenter);
  119. Engine::get_singleton()->add_singleton(Engine::Singleton("GameCenter", game_center));
  120. game_center->connect();
  121. #endif
  122. #ifdef STOREKIT_ENABLED
  123. store_kit = memnew(InAppStore);
  124. Engine::get_singleton()->add_singleton(Engine::Singleton("InAppStore", store_kit));
  125. #endif
  126. #ifdef ICLOUD_ENABLED
  127. icloud = memnew(ICloud);
  128. Engine::get_singleton()->add_singleton(Engine::Singleton("ICloud", icloud));
  129. //icloud->connect();
  130. #endif
  131. ios = memnew(iOS);
  132. Engine::get_singleton()->add_singleton(Engine::Singleton("iOS", ios));
  133. return OK;
  134. };
  135. MainLoop *OSIPhone::get_main_loop() const {
  136. return main_loop;
  137. };
  138. void OSIPhone::set_main_loop(MainLoop *p_main_loop) {
  139. main_loop = p_main_loop;
  140. if (main_loop) {
  141. input->set_main_loop(p_main_loop);
  142. main_loop->init();
  143. }
  144. };
  145. bool OSIPhone::iterate() {
  146. if (!main_loop)
  147. return true;
  148. if (main_loop) {
  149. for (int i = 0; i < event_count; i++) {
  150. input->parse_input_event(event_queue[i]);
  151. };
  152. };
  153. event_count = 0;
  154. return Main::iteration();
  155. };
  156. void OSIPhone::key(uint32_t p_key, bool p_pressed) {
  157. Ref<InputEventKey> ev;
  158. ev.instance();
  159. ev->set_echo(false);
  160. ev->set_pressed(p_pressed);
  161. ev->set_keycode(p_key);
  162. ev->set_physical_keycode(p_key);
  163. ev->set_unicode(p_key);
  164. queue_event(ev);
  165. };
  166. void OSIPhone::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick) {
  167. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  168. Ref<InputEventScreenTouch> ev;
  169. ev.instance();
  170. ev->set_index(p_idx);
  171. ev->set_pressed(p_pressed);
  172. ev->set_position(Vector2(p_x, p_y));
  173. queue_event(ev);
  174. };
  175. touch_list.pressed[p_idx] = p_pressed;
  176. };
  177. void OSIPhone::touch_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y) {
  178. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  179. Ref<InputEventScreenDrag> ev;
  180. ev.instance();
  181. ev->set_index(p_idx);
  182. ev->set_position(Vector2(p_x, p_y));
  183. ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
  184. queue_event(ev);
  185. };
  186. };
  187. void OSIPhone::queue_event(const Ref<InputEvent> &p_event) {
  188. ERR_FAIL_INDEX(event_count, MAX_EVENTS);
  189. event_queue[event_count++] = p_event;
  190. };
  191. void OSIPhone::touches_cancelled() {
  192. for (int i = 0; i < MAX_MOUSE_COUNT; i++) {
  193. if (touch_list.pressed[i]) {
  194. // send a mouse_up outside the screen
  195. touch_press(i, -1, -1, false, false);
  196. };
  197. };
  198. };
  199. static const float ACCEL_RANGE = 1;
  200. void OSIPhone::update_gravity(float p_x, float p_y, float p_z) {
  201. input->set_gravity(Vector3(p_x, p_y, p_z));
  202. };
  203. void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {
  204. // Found out the Z should not be negated! Pass as is!
  205. input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, p_z / (float)ACCEL_RANGE));
  206. /*
  207. if (p_x != last_accel.x) {
  208. //printf("updating accel x %f\n", p_x);
  209. InputEvent ev;
  210. ev.type = InputEvent::JOYPAD_MOTION;
  211. ev.device = 0;
  212. ev.joy_motion.axis = JOY_ANALOG_0;
  213. ev.joy_motion.axis_value = (p_x / (float)ACCEL_RANGE);
  214. last_accel.x = p_x;
  215. queue_event(ev);
  216. };
  217. if (p_y != last_accel.y) {
  218. //printf("updating accel y %f\n", p_y);
  219. InputEvent ev;
  220. ev.type = InputEvent::JOYPAD_MOTION;
  221. ev.device = 0;
  222. ev.joy_motion.axis = JOY_ANALOG_1;
  223. ev.joy_motion.axis_value = (p_y / (float)ACCEL_RANGE);
  224. last_accel.y = p_y;
  225. queue_event(ev);
  226. };
  227. if (p_z != last_accel.z) {
  228. //printf("updating accel z %f\n", p_z);
  229. InputEvent ev;
  230. ev.type = InputEvent::JOYPAD_MOTION;
  231. ev.device = 0;
  232. ev.joy_motion.axis = JOY_ANALOG_2;
  233. ev.joy_motion.axis_value = ( (1.0 - p_z) / (float)ACCEL_RANGE);
  234. last_accel.z = p_z;
  235. queue_event(ev);
  236. };
  237. */
  238. };
  239. void OSIPhone::update_magnetometer(float p_x, float p_y, float p_z) {
  240. input->set_magnetometer(Vector3(p_x, p_y, p_z));
  241. };
  242. void OSIPhone::update_gyroscope(float p_x, float p_y, float p_z) {
  243. input->set_gyroscope(Vector3(p_x, p_y, p_z));
  244. };
  245. int OSIPhone::get_unused_joy_id() {
  246. return input->get_unused_joy_id();
  247. };
  248. void OSIPhone::joy_connection_changed(int p_idx, bool p_connected, String p_name) {
  249. input->joy_connection_changed(p_idx, p_connected, p_name);
  250. };
  251. void OSIPhone::joy_button(int p_device, int p_button, bool p_pressed) {
  252. input->joy_button(p_device, p_button, p_pressed);
  253. };
  254. void OSIPhone::joy_axis(int p_device, int p_axis, const InputDefault::JoyAxis &p_value) {
  255. input->joy_axis(p_device, p_axis, p_value);
  256. };
  257. void OSIPhone::delete_main_loop() {
  258. if (main_loop) {
  259. main_loop->finish();
  260. memdelete(main_loop);
  261. };
  262. main_loop = NULL;
  263. };
  264. void OSIPhone::finalize() {
  265. delete_main_loop();
  266. memdelete(input);
  267. memdelete(ios);
  268. #ifdef GAME_CENTER_ENABLED
  269. memdelete(game_center);
  270. #endif
  271. #ifdef STOREKIT_ENABLED
  272. memdelete(store_kit);
  273. #endif
  274. #ifdef ICLOUD_ENABLED
  275. memdelete(icloud);
  276. #endif
  277. visual_server->finish();
  278. memdelete(visual_server);
  279. // memdelete(rasterizer);
  280. // Free unhandled events before close
  281. for (int i = 0; i < MAX_EVENTS; i++) {
  282. event_queue[i].unref();
  283. };
  284. event_count = 0;
  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. #if defined(OPENGL_ENABLED)
  346. // gl_view_base_fb has not been updated yet
  347. RasterizerStorageGLES2::system_fbo = p_fb;
  348. #endif
  349. return 0;
  350. };
  351. bool OSIPhone::has_virtual_keyboard() const {
  352. return true;
  353. };
  354. extern void _show_keyboard(String p_existing);
  355. extern void _hide_keyboard();
  356. extern Error _shell_open(String p_uri);
  357. extern void _set_keep_screen_on(bool p_enabled);
  358. extern void _vibrate();
  359. void OSIPhone::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, int p_max_input_length) {
  360. _show_keyboard(p_existing_text);
  361. };
  362. void OSIPhone::hide_virtual_keyboard() {
  363. _hide_keyboard();
  364. };
  365. void OSIPhone::set_virtual_keyboard_height(int p_height) {
  366. virtual_keyboard_height = p_height;
  367. }
  368. int OSIPhone::get_virtual_keyboard_height() const {
  369. return virtual_keyboard_height;
  370. }
  371. Error OSIPhone::shell_open(String p_uri) {
  372. return _shell_open(p_uri);
  373. };
  374. void OSIPhone::set_keep_screen_on(bool p_enabled) {
  375. OS::set_keep_screen_on(p_enabled);
  376. _set_keep_screen_on(p_enabled);
  377. };
  378. String OSIPhone::get_user_data_dir() const {
  379. return data_dir;
  380. };
  381. String OSIPhone::get_name() const {
  382. return "iOS";
  383. };
  384. String OSIPhone::get_model_name() const {
  385. String model = ios->get_model();
  386. if (model != "")
  387. return model;
  388. return OS_Unix::get_model_name();
  389. }
  390. Size2 OSIPhone::get_window_size() const {
  391. return Vector2(video_mode.width, video_mode.height);
  392. }
  393. extern Rect2 _get_ios_window_safe_area(float p_window_width, float p_window_height);
  394. Rect2 OSIPhone::get_window_safe_area() const {
  395. return _get_ios_window_safe_area(video_mode.width, video_mode.height);
  396. }
  397. bool OSIPhone::has_touchscreen_ui_hint() const {
  398. return true;
  399. }
  400. void OSIPhone::set_locale(String p_locale) {
  401. locale_code = p_locale;
  402. }
  403. String OSIPhone::get_locale() const {
  404. return locale_code;
  405. }
  406. extern bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  407. extern bool _is_video_playing();
  408. extern void _pause_video();
  409. extern void _unpause_video();
  410. extern void _stop_video();
  411. extern void _focus_out_video();
  412. Error OSIPhone::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  413. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  414. bool exists = f && f->is_open();
  415. String tempFile = get_user_data_dir();
  416. if (!exists)
  417. return FAILED;
  418. if (p_path.begins_with("res://")) {
  419. if (PackedData::get_singleton()->has_path(p_path)) {
  420. print("Unable to play %S using the native player as it resides in a .pck file\n", p_path.c_str());
  421. return ERR_INVALID_PARAMETER;
  422. } else {
  423. p_path = p_path.replace("res:/", ProjectSettings::get_singleton()->get_resource_path());
  424. }
  425. } else if (p_path.begins_with("user://"))
  426. p_path = p_path.replace("user:/", get_user_data_dir());
  427. memdelete(f);
  428. print("Playing video: %S\n", p_path.c_str());
  429. if (_play_video(p_path, p_volume, p_audio_track, p_subtitle_track))
  430. return OK;
  431. return FAILED;
  432. }
  433. bool OSIPhone::native_video_is_playing() const {
  434. return _is_video_playing();
  435. }
  436. void OSIPhone::native_video_pause() {
  437. if (native_video_is_playing())
  438. _pause_video();
  439. }
  440. void OSIPhone::native_video_unpause() {
  441. _unpause_video();
  442. };
  443. void OSIPhone::native_video_focus_out() {
  444. _focus_out_video();
  445. };
  446. void OSIPhone::native_video_stop() {
  447. if (native_video_is_playing())
  448. _stop_video();
  449. }
  450. void OSIPhone::vibrate_handheld(int p_duration_ms) {
  451. // iOS does not support duration for vibration
  452. _vibrate();
  453. }
  454. bool OSIPhone::_check_internal_feature_support(const String &p_feature) {
  455. return p_feature == "mobile";
  456. }
  457. // Initialization order between compilation units is not guaranteed,
  458. // so we use this as a hack to ensure certain code is called before
  459. // everything else, but after all units are initialized.
  460. typedef void (*init_callback)();
  461. static init_callback *ios_init_callbacks = NULL;
  462. static int ios_init_callbacks_count = 0;
  463. static int ios_init_callbacks_capacity = 0;
  464. void add_ios_init_callback(init_callback cb) {
  465. if (ios_init_callbacks_count == ios_init_callbacks_capacity) {
  466. void *new_ptr = realloc(ios_init_callbacks, sizeof(cb) * 32);
  467. if (new_ptr) {
  468. ios_init_callbacks = (init_callback *)(new_ptr);
  469. ios_init_callbacks_capacity += 32;
  470. }
  471. }
  472. if (ios_init_callbacks_capacity > ios_init_callbacks_count) {
  473. ios_init_callbacks[ios_init_callbacks_count] = cb;
  474. ++ios_init_callbacks_count;
  475. }
  476. }
  477. OSIPhone::OSIPhone(int width, int height, String p_data_dir) {
  478. for (int i = 0; i < ios_init_callbacks_count; ++i) {
  479. ios_init_callbacks[i]();
  480. }
  481. free(ios_init_callbacks);
  482. ios_init_callbacks = NULL;
  483. ios_init_callbacks_count = 0;
  484. ios_init_callbacks_capacity = 0;
  485. main_loop = NULL;
  486. visual_server = NULL;
  487. VideoMode vm;
  488. vm.fullscreen = true;
  489. vm.width = width;
  490. vm.height = height;
  491. vm.resizable = false;
  492. set_video_mode(vm);
  493. event_count = 0;
  494. virtual_keyboard_height = 0;
  495. // can't call set_data_dir from here, since it requires DirAccess
  496. // which is initialized in initialize_core
  497. data_dir = p_data_dir;
  498. Vector<Logger *> loggers;
  499. loggers.push_back(memnew(SyslogLogger));
  500. #ifdef DEBUG_ENABLED
  501. // it seems iOS app's stdout/stderr is only obtainable if you launch it from Xcode
  502. loggers.push_back(memnew(StdLogger));
  503. #endif
  504. _set_logger(memnew(CompositeLogger(loggers)));
  505. AudioDriverManager::add_driver(&audio_driver);
  506. };
  507. OSIPhone::~OSIPhone() {
  508. }
  509. #endif