os_iphone.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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-2015 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. #ifdef IPHONE_ENABLED
  30. #include "os_iphone.h"
  31. #include "drivers/gles2/rasterizer_gles2.h"
  32. #include "servers/visual/visual_server_raster.h"
  33. #include "servers/visual/visual_server_wrap_mt.h"
  34. #include "main/main.h"
  35. #include "audio_driver_iphone.h"
  36. #include "core/os/dir_access.h"
  37. #include "core/os/file_access.h"
  38. #include "core/globals.h"
  39. #include "sem_iphone.h"
  40. int OSIPhone::get_video_driver_count() const {
  41. return 1;
  42. };
  43. const char * OSIPhone::get_video_driver_name(int p_driver) const {
  44. return "GLES2";
  45. };
  46. OSIPhone* OSIPhone::get_singleton() {
  47. return (OSIPhone*)OS::get_singleton();
  48. };
  49. OS::VideoMode OSIPhone::get_default_video_mode() const {
  50. return video_mode;
  51. };
  52. uint8_t OSIPhone::get_orientations() const {
  53. return supported_orientations;
  54. };
  55. extern int gl_view_base_fb; // from gl_view.mm
  56. void OSIPhone::set_data_dir(String p_dir) {
  57. DirAccess* da = DirAccess::open(p_dir);
  58. data_dir = da->get_current_dir();
  59. printf("setting data dir to %ls from %ls\n", data_dir.c_str(), p_dir.c_str());
  60. memdelete(da);
  61. };
  62. void OSIPhone::set_unique_ID(String p_ID) {
  63. unique_ID = p_ID;
  64. };
  65. String OSIPhone::get_unique_ID() const {
  66. return unique_ID;
  67. };
  68. void OSIPhone::initialize_core() {
  69. OS_Unix::initialize_core();
  70. SemaphoreIphone::make_default();
  71. };
  72. void OSIPhone::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  73. supported_orientations = 0;
  74. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal", true)?1:0) << LandscapeLeft);
  75. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal_flipped", false)?1:0) << LandscapeRight);
  76. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical", false)?1:0) << PortraitDown);
  77. supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical_flipped", false)?1:0) << PortraitUp);
  78. rasterizer_gles22 = memnew( RasterizerGLES2(false, false, false) );
  79. rasterizer = rasterizer_gles22;
  80. rasterizer_gles22->set_base_framebuffer(gl_view_base_fb);
  81. visual_server = memnew( VisualServerRaster(rasterizer) );
  82. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  83. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  84. };
  85. visual_server->init();
  86. visual_server->init();
  87. visual_server->cursor_set_visible(false, 0);
  88. audio_driver = memnew(AudioDriverIphone);
  89. audio_driver->set_singleton();
  90. audio_driver->init();
  91. sample_manager = memnew( SampleManagerMallocSW );
  92. audio_server = memnew( AudioServerSW(sample_manager) );
  93. audio_server->init();
  94. spatial_sound_server = memnew( SpatialSoundServerSW );
  95. spatial_sound_server->init();
  96. spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
  97. spatial_sound_2d_server->init();
  98. //
  99. physics_server = memnew( PhysicsServerSW );
  100. physics_server->init();
  101. physics_2d_server = memnew( Physics2DServerSW );
  102. physics_2d_server->init();
  103. input = memnew( InputDefault );
  104. /*
  105. #ifdef IOS_SCORELOOP_ENABLED
  106. scoreloop = memnew(ScoreloopIOS);
  107. Globals::get_singleton()->add_singleton(Globals::Singleton("Scoreloop", scoreloop));
  108. scoreloop->connect();
  109. #endif
  110. */
  111. #ifdef GAME_CENTER_ENABLED
  112. game_center = memnew(GameCenter);
  113. Globals::get_singleton()->add_singleton(Globals::Singleton("GameCenter", game_center));
  114. game_center->connect();
  115. #endif
  116. #ifdef STOREKIT_ENABLED
  117. store_kit = memnew(InAppStore);
  118. Globals::get_singleton()->add_singleton(Globals::Singleton("InAppStore", store_kit));
  119. #endif
  120. };
  121. MainLoop *OSIPhone::get_main_loop() const {
  122. return main_loop;
  123. };
  124. void OSIPhone::set_main_loop( MainLoop * p_main_loop ) {
  125. main_loop = p_main_loop;
  126. if (main_loop) {
  127. input->set_main_loop(p_main_loop);
  128. main_loop->init();
  129. }
  130. };
  131. bool OSIPhone::iterate() {
  132. if (!main_loop)
  133. return true;
  134. if (main_loop) {
  135. for (int i=0; i<event_count; i++) {
  136. input->parse_input_event(event_queue[i]);
  137. };
  138. };
  139. event_count = 0;
  140. return Main::iteration();
  141. };
  142. void OSIPhone::key(uint32_t p_key, bool p_pressed) {
  143. InputEvent ev;
  144. ev.type = InputEvent::KEY;
  145. ev.ID = ++last_event_id;
  146. ev.key.echo = false;
  147. ev.key.pressed = p_pressed;
  148. ev.key.scancode = p_key;
  149. ev.key.unicode = p_key;
  150. queue_event(ev);
  151. };
  152. void OSIPhone::mouse_button(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick, bool p_use_as_mouse) {
  153. if (!GLOBAL_DEF("debug/disable_touch", false)) {
  154. InputEvent ev;
  155. ev.type = InputEvent::SCREEN_TOUCH;
  156. ev.ID = ++last_event_id;
  157. ev.screen_touch.index=p_idx;
  158. ev.screen_touch.pressed=p_pressed;
  159. ev.screen_touch.x=p_x;
  160. ev.screen_touch.y=p_y;
  161. queue_event(ev);
  162. };
  163. if (p_use_as_mouse) {
  164. InputEvent ev;
  165. ev.type = InputEvent::MOUSE_BUTTON;
  166. ev.device = 0;
  167. ev.mouse_button.pointer_index = p_idx;
  168. ev.ID = ++last_event_id;
  169. // swaped it for tilted screen
  170. //ev.mouse_button.x = ev.mouse_button.global_x = video_mode.height - p_y;
  171. //ev.mouse_button.y = ev.mouse_button.global_y = p_x;
  172. ev.mouse_button.x = ev.mouse_button.global_x = p_x;
  173. ev.mouse_button.y = ev.mouse_button.global_y = p_y;
  174. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  175. ev.mouse_button.button_index = BUTTON_LEFT;
  176. ev.mouse_button.doubleclick = p_doubleclick;
  177. ev.mouse_button.pressed = p_pressed;
  178. mouse_list.pressed[p_idx] = 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. InputEvent ev;
  185. ev.type=InputEvent::SCREEN_DRAG;
  186. ev.ID = ++last_event_id;
  187. ev.screen_drag.index=p_idx;
  188. ev.screen_drag.x=p_x;
  189. ev.screen_drag.y=p_y;
  190. ev.screen_drag.relative_x = p_x - p_prev_x;
  191. ev.screen_drag.relative_y = p_y - p_prev_y;
  192. queue_event(ev);
  193. };
  194. if (p_use_as_mouse) {
  195. InputEvent ev;
  196. ev.type = InputEvent::MOUSE_MOTION;
  197. ev.device = 0;
  198. ev.mouse_motion.pointer_index = p_idx;
  199. ev.ID = ++last_event_id;
  200. if (true) { // vertical
  201. ev.mouse_motion.x = ev.mouse_button.global_x = p_x;
  202. ev.mouse_motion.y = ev.mouse_button.global_y = p_y;
  203. ev.mouse_motion.relative_x = ev.mouse_motion.x - p_prev_x;
  204. ev.mouse_motion.relative_y = ev.mouse_motion.y - p_prev_y;
  205. } else { // horizontal?
  206. ev.mouse_motion.x = ev.mouse_button.global_x = video_mode.height - p_y;
  207. ev.mouse_motion.y = ev.mouse_button.global_y = p_x;
  208. ev.mouse_motion.relative_x = ev.mouse_motion.x - (video_mode.height - p_prev_x);
  209. ev.mouse_motion.relative_y = ev.mouse_motion.y - p_prev_x;
  210. };
  211. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  212. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  213. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  214. ev.mouse_motion.button_mask = 1; // pressed
  215. queue_event(ev);
  216. };
  217. };
  218. void OSIPhone::queue_event(const InputEvent& p_event) {
  219. ERR_FAIL_INDEX( event_count, MAX_EVENTS );
  220. event_queue[event_count++] = p_event;
  221. };
  222. void OSIPhone::touches_cancelled() {
  223. for (int i=0; i<MAX_MOUSE_COUNT; i++) {
  224. if (mouse_list.pressed[i]) {
  225. // send a mouse_up outside the screen
  226. mouse_button(i, -1, -1, false, false, false);
  227. };
  228. };
  229. };
  230. static const float ACCEL_RANGE = 1;
  231. void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {
  232. input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, -p_z / (float)ACCEL_RANGE));
  233. /*
  234. if (p_x != last_accel.x) {
  235. //printf("updating accel x %f\n", p_x);
  236. InputEvent ev;
  237. ev.type = InputEvent::JOYSTICK_MOTION;
  238. ev.device = 0;
  239. ev.joy_motion.axis = JOY_ANALOG_0_X;
  240. ev.joy_motion.axis_value = (p_x / (float)ACCEL_RANGE);
  241. ev.ID = ++last_event_id;
  242. last_accel.x = p_x;
  243. queue_event(ev);
  244. };
  245. if (p_y != last_accel.y) {
  246. //printf("updating accel y %f\n", p_y);
  247. InputEvent ev;
  248. ev.type = InputEvent::JOYSTICK_MOTION;
  249. ev.device = 0;
  250. ev.joy_motion.axis = JOY_ANALOG_0_Y;
  251. ev.joy_motion.axis_value = (p_y / (float)ACCEL_RANGE);
  252. ev.ID = ++last_event_id;
  253. last_accel.y = p_y;
  254. queue_event(ev);
  255. };
  256. if (p_z != last_accel.z) {
  257. //printf("updating accel z %f\n", p_z);
  258. InputEvent ev;
  259. ev.type = InputEvent::JOYSTICK_MOTION;
  260. ev.device = 0;
  261. ev.joy_motion.axis = JOY_ANALOG_1_X;
  262. ev.joy_motion.axis_value = ( (1.0 - p_z) / (float)ACCEL_RANGE);
  263. ev.ID = ++last_event_id;
  264. last_accel.z = p_z;
  265. queue_event(ev);
  266. };
  267. */
  268. };
  269. void OSIPhone::delete_main_loop() {
  270. if (main_loop) {
  271. main_loop->finish();
  272. memdelete(main_loop);
  273. };
  274. main_loop = NULL;
  275. };
  276. void OSIPhone::finalize() {
  277. if(main_loop) // should not happen?
  278. memdelete(main_loop);
  279. visual_server->finish();
  280. memdelete(visual_server);
  281. memdelete(rasterizer);
  282. physics_server->finish();
  283. memdelete(physics_server);
  284. physics_2d_server->finish();
  285. memdelete(physics_2d_server);
  286. spatial_sound_server->finish();
  287. memdelete(spatial_sound_server);
  288. memdelete(input);
  289. spatial_sound_2d_server->finish();
  290. memdelete(spatial_sound_2d_server);
  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_pos() 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::set_video_mode(const VideoMode& p_video_mode, int p_screen) {
  305. video_mode = p_video_mode;
  306. };
  307. OS::VideoMode OSIPhone::get_video_mode(int p_screen) const {
  308. return video_mode;
  309. };
  310. void OSIPhone::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  311. p_list->push_back(video_mode);
  312. };
  313. bool OSIPhone::can_draw() const {
  314. if (native_video_is_playing())
  315. return false;
  316. return true;
  317. };
  318. int OSIPhone::set_base_framebuffer(int p_fb) {
  319. if (rasterizer_gles22) {
  320. rasterizer_gles22->set_base_framebuffer(p_fb);
  321. };
  322. return 0;
  323. };
  324. bool OSIPhone::has_virtual_keyboard() const {
  325. return true;
  326. };
  327. extern void _show_keyboard(String p_existing);
  328. extern void _hide_keyboard();
  329. extern Error _shell_open(String p_uri);
  330. void OSIPhone::show_virtual_keyboard(const String& p_existing_text,const Rect2& p_screen_rect) {
  331. _show_keyboard(p_existing_text);
  332. };
  333. void OSIPhone::hide_virtual_keyboard() {
  334. _hide_keyboard();
  335. };
  336. Error OSIPhone::shell_open(String p_uri) {
  337. return _shell_open(p_uri);
  338. };
  339. void OSIPhone::set_cursor_shape(CursorShape p_shape) {
  340. };
  341. String OSIPhone::get_data_dir() const {
  342. return data_dir;
  343. };
  344. String OSIPhone::get_name() {
  345. return "iOS";
  346. };
  347. Size2 OSIPhone::get_window_size() const {
  348. return Vector2(video_mode.width, video_mode.height);
  349. }
  350. bool OSIPhone::has_touchscreen_ui_hint() const {
  351. return true;
  352. }
  353. void OSIPhone::set_locale(String p_locale)
  354. {
  355. locale_code = p_locale;
  356. }
  357. String OSIPhone::get_locale() const {
  358. return locale_code;
  359. }
  360. extern bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  361. extern bool _is_video_playing();
  362. extern void _pause_video();
  363. extern void _unpause_video();
  364. extern void _stop_video();
  365. extern void _focus_out_video();
  366. Error OSIPhone::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  367. FileAccess* f = FileAccess::open(p_path, FileAccess::READ);
  368. bool exists = f && f->is_open();
  369. printf("file exists for %ls, %i, %p\n", p_path.c_str(), (int)exists, f);
  370. if (f)
  371. memdelete(f);
  372. if (!exists)
  373. return FAILED;
  374. if ( _play_video(p_path, p_volume, p_audio_track, p_subtitle_track) )
  375. return OK;
  376. return FAILED;
  377. }
  378. bool OSIPhone::native_video_is_playing() const {
  379. return _is_video_playing();
  380. }
  381. void OSIPhone::native_video_pause() {
  382. if (native_video_is_playing())
  383. _pause_video();
  384. }
  385. void OSIPhone::native_video_unpause() {
  386. _unpause_video();
  387. };
  388. void OSIPhone::native_video_focus_out() {
  389. _focus_out_video();
  390. };
  391. void OSIPhone::native_video_stop() {
  392. if (native_video_is_playing())
  393. _stop_video();
  394. }
  395. OSIPhone::OSIPhone(int width, int height) {
  396. rasterizer_gles22 = NULL;
  397. main_loop = NULL;
  398. visual_server = NULL;
  399. rasterizer = NULL;
  400. VideoMode vm;
  401. vm.fullscreen = true;
  402. vm.width = width;
  403. vm.height = height;
  404. vm.resizable = false;
  405. set_video_mode(vm);
  406. event_count = 0;
  407. last_event_id = 0;
  408. };
  409. OSIPhone::~OSIPhone() {
  410. }
  411. #endif