os_iphone.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*************************************************************************/
  2. /* os_iphone.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. /* Copyright (c) 2014-2017 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/gles3/rasterizer_gles3.h"
  33. #include "servers/visual/visual_server_raster.h"
  34. //#include "servers/visual/visual_server_wrap_mt.h"
  35. #include "audio_driver_iphone.h"
  36. #include "main/main.h"
  37. #include "core/global_config.h"
  38. #include "core/io/file_access_pack.h"
  39. #include "core/os/dir_access.h"
  40. #include "core/os/file_access.h"
  41. #include "sem_iphone.h"
  42. #include "ios.h"
  43. int OSIPhone::get_video_driver_count() const {
  44. return 1;
  45. };
  46. const char *OSIPhone::get_video_driver_name(int p_driver) const {
  47. return "GLES2";
  48. };
  49. OSIPhone *OSIPhone::get_singleton() {
  50. return (OSIPhone *)OS::get_singleton();
  51. };
  52. OS::VideoMode OSIPhone::get_default_video_mode() const {
  53. return video_mode;
  54. };
  55. uint8_t OSIPhone::get_orientations() const {
  56. return supported_orientations;
  57. };
  58. extern int gl_view_base_fb; // from gl_view.mm
  59. void OSIPhone::set_data_dir(String p_dir) {
  60. DirAccess *da = DirAccess::open(p_dir);
  61. data_dir = da->get_current_dir();
  62. printf("setting data dir to %ls from %ls\n", data_dir.c_str(), p_dir.c_str());
  63. memdelete(da);
  64. };
  65. void OSIPhone::set_unique_ID(String p_ID) {
  66. unique_ID = p_ID;
  67. };
  68. String OSIPhone::get_unique_ID() const {
  69. return unique_ID;
  70. };
  71. void OSIPhone::initialize_core() {
  72. OS_Unix::initialize_core();
  73. SemaphoreIphone::make_default();
  74. };
  75. void OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  76. supported_orientations = 0;
  77. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal", true) ? 1 : 0) << LandscapeLeft);
  78. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal_flipped", false) ? 1 : 0) << LandscapeRight);
  79. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical", false) ? 1 : 0) << PortraitDown);
  80. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical_flipped", false) ? 1 : 0) << PortraitUp);
  81. RasterizerGLES3::register_config();
  82. RasterizerGLES3::make_current();
  83. RasterizerStorageGLES3::system_fbo = gl_view_base_fb;
  84. visual_server = memnew(VisualServerRaster());
  85. /*
  86. FIXME: Reimplement threaded rendering? Or remove?
  87. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  88. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  89. };
  90. */
  91. visual_server->init();
  92. visual_server->cursor_set_visible(false, 0);
  93. audio_driver = memnew(AudioDriverIphone);
  94. audio_driver->set_singleton();
  95. audio_driver->init();
  96. // init physics servers
  97. physics_server = memnew(PhysicsServerSW);
  98. physics_server->init();
  99. //physics_2d_server = memnew( Physics2DServerSW );
  100. physics_2d_server = Physics2DServerWrapMT::init_server<Physics2DServerSW>();
  101. physics_2d_server->init();
  102. input = memnew(InputDefault);
  103. /*
  104. #ifdef IOS_SCORELOOP_ENABLED
  105. scoreloop = memnew(ScoreloopIOS);
  106. GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("Scoreloop", scoreloop));
  107. scoreloop->connect();
  108. #endif
  109. */
  110. #ifdef GAME_CENTER_ENABLED
  111. game_center = memnew(GameCenter);
  112. GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("GameCenter", game_center));
  113. game_center->connect();
  114. #endif
  115. #ifdef STOREKIT_ENABLED
  116. store_kit = memnew(InAppStore);
  117. GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("InAppStore", store_kit));
  118. #endif
  119. #ifdef ICLOUD_ENABLED
  120. icloud = memnew(ICloud);
  121. GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("ICloud", icloud));
  122. //icloud->connect();
  123. #endif
  124. GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("iOS", memnew(iOS)));
  125. };
  126. MainLoop *OSIPhone::get_main_loop() const {
  127. return main_loop;
  128. };
  129. void OSIPhone::set_main_loop(MainLoop *p_main_loop) {
  130. main_loop = p_main_loop;
  131. if (main_loop) {
  132. input->set_main_loop(p_main_loop);
  133. main_loop->init();
  134. }
  135. };
  136. bool OSIPhone::iterate() {
  137. if (!main_loop)
  138. return true;
  139. if (main_loop) {
  140. for (int i = 0; i < event_count; i++) {
  141. input->parse_input_event(event_queue[i]);
  142. };
  143. };
  144. event_count = 0;
  145. return Main::iteration();
  146. };
  147. void OSIPhone::key(uint32_t p_key, bool p_pressed) {
  148. Ref<InputEventKey> ev;
  149. ev.instance();
  150. ev->set_echo(false);
  151. ev->set_pressed(p_pressed);
  152. ev->set_scancode(p_key);
  153. ev->set_unicode(p_key);
  154. queue_event(ev);
  155. };
  156. void OSIPhone::mouse_button(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick, bool p_use_as_mouse) {
  157. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  158. Ref<InputEventScreenTouch> ev;
  159. ev.instance();
  160. ev->set_index(p_idx);
  161. ev->set_pressed(p_pressed);
  162. ev->set_pos(Vector2(p_x, p_y));
  163. queue_event(ev);
  164. };
  165. mouse_list.pressed[p_idx] = p_pressed;
  166. if (p_use_as_mouse) {
  167. Ref<InputEventMouseButton> ev;
  168. ev.instance();
  169. // swaped it for tilted screen
  170. //ev->get_pos().x = ev.mouse_button.global_x = video_mode.height - p_y;
  171. //ev->get_pos().y = ev.mouse_button.global_y = p_x;
  172. ev->set_pos(Vector2(video_mode.height - p_y, p_x));
  173. ev->set_global_pos(Vector2(video_mode.height - p_y, p_x));
  174. //mouse_list.pressed[p_idx] = p_pressed;
  175. input->set_mouse_position(ev->get_pos());
  176. ev->set_button_index(BUTTON_LEFT);
  177. ev->set_doubleclick(p_doubleclick);
  178. ev->set_pressed(p_pressed);
  179. queue_event(ev);
  180. };
  181. };
  182. void OSIPhone::mouse_move(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y, bool p_use_as_mouse) {
  183. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  184. Ref<InputEventScreenDrag> ev;
  185. ev.instance();
  186. ev->set_index(p_idx);
  187. ev->set_pos(Vector2(p_x, p_y));
  188. ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
  189. queue_event(ev);
  190. };
  191. if (p_use_as_mouse) {
  192. Ref<InputEventMouseMotion> ev;
  193. ev.instance();
  194. ev->set_pos(Vector2(p_x, p_y));
  195. ev->set_global_pos(Vector2(p_x, p_y));
  196. ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
  197. input->set_mouse_position(ev->get_pos());
  198. ev->set_speed(input->get_last_mouse_speed());
  199. ev->set_button_mask(BUTTON_LEFT); // pressed
  200. queue_event(ev);
  201. };
  202. };
  203. void OSIPhone::queue_event(const Ref<InputEvent> &p_event) {
  204. ERR_FAIL_INDEX(event_count, MAX_EVENTS);
  205. event_queue[event_count++] = p_event;
  206. };
  207. void OSIPhone::touches_cancelled() {
  208. for (int i = 0; i < MAX_MOUSE_COUNT; i++) {
  209. if (mouse_list.pressed[i]) {
  210. // send a mouse_up outside the screen
  211. mouse_button(i, -1, -1, false, false, false);
  212. };
  213. };
  214. };
  215. static const float ACCEL_RANGE = 1;
  216. void OSIPhone::update_gravity(float p_x, float p_y, float p_z) {
  217. input->set_gravity(Vector3(p_x, p_y, p_z));
  218. };
  219. void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {
  220. // Found out the Z should not be negated! Pass as is!
  221. input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, p_z / (float)ACCEL_RANGE));
  222. /*
  223. if (p_x != last_accel.x) {
  224. //printf("updating accel x %f\n", p_x);
  225. InputEvent ev;
  226. ev.type = InputEvent::JOYPAD_MOTION;
  227. ev.device = 0;
  228. ev.joy_motion.axis = JOY_ANALOG_0;
  229. ev.joy_motion.axis_value = (p_x / (float)ACCEL_RANGE);
  230. last_accel.x = p_x;
  231. queue_event(ev);
  232. };
  233. if (p_y != last_accel.y) {
  234. //printf("updating accel y %f\n", p_y);
  235. InputEvent ev;
  236. ev.type = InputEvent::JOYPAD_MOTION;
  237. ev.device = 0;
  238. ev.joy_motion.axis = JOY_ANALOG_1;
  239. ev.joy_motion.axis_value = (p_y / (float)ACCEL_RANGE);
  240. last_accel.y = p_y;
  241. queue_event(ev);
  242. };
  243. if (p_z != last_accel.z) {
  244. //printf("updating accel z %f\n", p_z);
  245. InputEvent ev;
  246. ev.type = InputEvent::JOYPAD_MOTION;
  247. ev.device = 0;
  248. ev.joy_motion.axis = JOY_ANALOG_2;
  249. ev.joy_motion.axis_value = ( (1.0 - p_z) / (float)ACCEL_RANGE);
  250. last_accel.z = p_z;
  251. queue_event(ev);
  252. };
  253. */
  254. };
  255. void OSIPhone::update_magnetometer(float p_x, float p_y, float p_z) {
  256. input->set_magnetometer(Vector3(p_x, p_y, p_z));
  257. };
  258. void OSIPhone::update_gyroscope(float p_x, float p_y, float p_z) {
  259. input->set_gyroscope(Vector3(p_x, p_y, p_z));
  260. };
  261. int OSIPhone::get_unused_joy_id() {
  262. return input->get_unused_joy_id();
  263. };
  264. void OSIPhone::joy_connection_changed(int p_idx, bool p_connected, String p_name) {
  265. input->joy_connection_changed(p_idx, p_connected, p_name);
  266. };
  267. void OSIPhone::joy_button(int p_device, int p_button, bool p_pressed) {
  268. input->joy_button(p_device, p_button, p_pressed);
  269. };
  270. void OSIPhone::joy_axis(int p_device, int p_axis, const InputDefault::JoyAxis &p_value) {
  271. input->joy_axis(p_device, p_axis, p_value);
  272. };
  273. void OSIPhone::delete_main_loop() {
  274. if (main_loop) {
  275. main_loop->finish();
  276. memdelete(main_loop);
  277. };
  278. main_loop = NULL;
  279. };
  280. void OSIPhone::finalize() {
  281. if (main_loop) // should not happen?
  282. memdelete(main_loop);
  283. visual_server->finish();
  284. memdelete(visual_server);
  285. // memdelete(rasterizer);
  286. physics_server->finish();
  287. memdelete(physics_server);
  288. physics_2d_server->finish();
  289. memdelete(physics_2d_server);
  290. memdelete(input);
  291. };
  292. void OSIPhone::set_mouse_show(bool p_show){};
  293. void OSIPhone::set_mouse_grab(bool p_grab){};
  294. bool OSIPhone::is_mouse_grab_enabled() const {
  295. return true;
  296. };
  297. Point2 OSIPhone::get_mouse_position() const {
  298. return Point2();
  299. };
  300. int OSIPhone::get_mouse_button_state() const {
  301. return mouse_list.pressed[0];
  302. };
  303. void OSIPhone::set_window_title(const String &p_title){};
  304. void OSIPhone::alert(const String &p_alert, const String &p_title) {
  305. const CharString utf8_alert = p_alert.utf8();
  306. const CharString utf8_title = p_title.utf8();
  307. iOS::alert(utf8_alert.get_data(), utf8_title.get_data());
  308. }
  309. void OSIPhone::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  310. video_mode = p_video_mode;
  311. };
  312. OS::VideoMode OSIPhone::get_video_mode(int p_screen) const {
  313. return video_mode;
  314. };
  315. void OSIPhone::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  316. p_list->push_back(video_mode);
  317. };
  318. bool OSIPhone::can_draw() const {
  319. if (native_video_is_playing())
  320. return false;
  321. return true;
  322. };
  323. int OSIPhone::set_base_framebuffer(int p_fb) {
  324. RasterizerStorageGLES3::system_fbo = gl_view_base_fb;
  325. return 0;
  326. };
  327. bool OSIPhone::has_virtual_keyboard() const {
  328. return true;
  329. };
  330. extern void _show_keyboard(String p_existing);
  331. extern void _hide_keyboard();
  332. extern Error _shell_open(String p_uri);
  333. extern void _set_keep_screen_on(bool p_enabled);
  334. void OSIPhone::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  335. _show_keyboard(p_existing_text);
  336. };
  337. void OSIPhone::hide_virtual_keyboard() {
  338. _hide_keyboard();
  339. };
  340. Error OSIPhone::shell_open(String p_uri) {
  341. return _shell_open(p_uri);
  342. };
  343. void OSIPhone::set_keep_screen_on(bool p_enabled) {
  344. OS::set_keep_screen_on(p_enabled);
  345. _set_keep_screen_on(p_enabled);
  346. };
  347. void OSIPhone::set_cursor_shape(CursorShape p_shape){
  348. };
  349. String OSIPhone::get_data_dir() const {
  350. return data_dir;
  351. };
  352. String OSIPhone::get_name() {
  353. return "iOS";
  354. };
  355. Size2 OSIPhone::get_window_size() const {
  356. return Vector2(video_mode.width, video_mode.height);
  357. }
  358. bool OSIPhone::has_touchscreen_ui_hint() const {
  359. return true;
  360. }
  361. void OSIPhone::set_locale(String p_locale) {
  362. locale_code = p_locale;
  363. }
  364. String OSIPhone::get_locale() const {
  365. return locale_code;
  366. }
  367. extern bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  368. extern bool _is_video_playing();
  369. extern void _pause_video();
  370. extern void _unpause_video();
  371. extern void _stop_video();
  372. extern void _focus_out_video();
  373. Error OSIPhone::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  374. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  375. bool exists = f && f->is_open();
  376. String tempFile = get_data_dir();
  377. if (!exists)
  378. return FAILED;
  379. if (p_path.begins_with("res://")) {
  380. if (PackedData::get_singleton()->has_path(p_path)) {
  381. print("Unable to play %S using the native player as it resides in a .pck file\n", p_path.c_str());
  382. return ERR_INVALID_PARAMETER;
  383. } else {
  384. p_path = p_path.replace("res:/", GlobalConfig::get_singleton()->get_resource_path());
  385. }
  386. } else if (p_path.begins_with("user://"))
  387. p_path = p_path.replace("user:/", get_data_dir());
  388. memdelete(f);
  389. print("Playing video: %S\n", p_path.c_str());
  390. if (_play_video(p_path, p_volume, p_audio_track, p_subtitle_track))
  391. return OK;
  392. return FAILED;
  393. }
  394. bool OSIPhone::native_video_is_playing() const {
  395. return _is_video_playing();
  396. }
  397. void OSIPhone::native_video_pause() {
  398. if (native_video_is_playing())
  399. _pause_video();
  400. }
  401. void OSIPhone::native_video_unpause() {
  402. _unpause_video();
  403. };
  404. void OSIPhone::native_video_focus_out() {
  405. _focus_out_video();
  406. };
  407. void OSIPhone::native_video_stop() {
  408. if (native_video_is_playing())
  409. _stop_video();
  410. }
  411. OSIPhone::OSIPhone(int width, int height) {
  412. main_loop = NULL;
  413. visual_server = NULL;
  414. VideoMode vm;
  415. vm.fullscreen = true;
  416. vm.width = width;
  417. vm.height = height;
  418. vm.resizable = false;
  419. set_video_mode(vm);
  420. event_count = 0;
  421. };
  422. OSIPhone::~OSIPhone() {
  423. }
  424. #endif