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 
32 #include "os_iphone.h"
33 
34 #include "drivers/gles2/rasterizer_gles2.h"
35 
36 #include "servers/visual/visual_server_raster.h"
37 #include "servers/visual/visual_server_wrap_mt.h"
38 
39 #include "audio_driver_iphone.h"
40 #include "main/main.h"
41 
42 #include "core/globals.h"
43 #include "core/io/file_access_pack.h"
44 #include "core/os/dir_access.h"
45 #include "core/os/file_access.h"
46 
47 #include "sem_iphone.h"
48 
49 #include "ios.h"
50 
get_video_driver_count() const51 int OSIPhone::get_video_driver_count() const {
52 
53 	return 1;
54 };
55 
get_video_driver_name(int p_driver) const56 const char *OSIPhone::get_video_driver_name(int p_driver) const {
57 
58 	return "GLES2";
59 };
60 
get_singleton()61 OSIPhone *OSIPhone::get_singleton() {
62 
63 	return (OSIPhone *)OS::get_singleton();
64 };
65 
get_default_video_mode() const66 OS::VideoMode OSIPhone::get_default_video_mode() const {
67 
68 	return video_mode;
69 };
70 
get_orientations() const71 uint8_t OSIPhone::get_orientations() const {
72 
73 	return supported_orientations;
74 };
75 
76 extern int gl_view_base_fb; // from gl_view.mm
77 
set_data_dir(String p_dir)78 void OSIPhone::set_data_dir(String p_dir) {
79 
80 	DirAccess *da = DirAccess::open(p_dir);
81 
82 	data_dir = da->get_current_dir();
83 	printf("setting data dir to %ls from %ls\n", data_dir.c_str(), p_dir.c_str());
84 	memdelete(da);
85 };
86 
set_unique_ID(String p_ID)87 void OSIPhone::set_unique_ID(String p_ID) {
88 
89 	unique_ID = p_ID;
90 };
91 
get_unique_ID() const92 String OSIPhone::get_unique_ID() const {
93 
94 	return unique_ID;
95 };
96 
initialize_core()97 void OSIPhone::initialize_core() {
98 
99 	OS_Unix::initialize_core();
100 	SemaphoreIphone::make_default();
101 };
102 
initialize(const VideoMode & p_desired,int p_video_driver,int p_audio_driver)103 void OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
104 
105 	supported_orientations = 0;
106 	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal", true) ? 1 : 0) << LandscapeLeft);
107 	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal_flipped", false) ? 1 : 0) << LandscapeRight);
108 	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical", false) ? 1 : 0) << PortraitDown);
109 	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical_flipped", false) ? 1 : 0) << PortraitUp);
110 
111 	rasterizer_gles22 = memnew(RasterizerGLES2(false, false, false));
112 	rasterizer = rasterizer_gles22;
113 	rasterizer_gles22->set_base_framebuffer(gl_view_base_fb);
114 
115 	visual_server = memnew(VisualServerRaster(rasterizer));
116 	if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
117 
118 		visual_server = memnew(VisualServerWrapMT(visual_server, false));
119 	};
120 	visual_server->init();
121 
122 	visual_server->init();
123 
124 	audio_driver = memnew(AudioDriverIphone);
125 	audio_driver->set_singleton();
126 	audio_driver->init();
127 
128 	sample_manager = memnew(SampleManagerMallocSW);
129 	audio_server = memnew(AudioServerSW(sample_manager));
130 	audio_server->init();
131 	spatial_sound_server = memnew(SpatialSoundServerSW);
132 	spatial_sound_server->init();
133 
134 	spatial_sound_2d_server = memnew(SpatialSound2DServerSW);
135 	spatial_sound_2d_server->init();
136 
137 	//
138 	physics_server = memnew(PhysicsServerSW);
139 	physics_server->init();
140 	//physics_2d_server = memnew( Physics2DServerSW );
141 	physics_2d_server = Physics2DServerWrapMT::init_server<Physics2DServerSW>();
142 	physics_2d_server->init();
143 
144 	input = memnew(InputDefault);
145 
146 	/*
147 #ifdef IOS_SCORELOOP_ENABLED
148 	scoreloop = memnew(ScoreloopIOS);
149 	Globals::get_singleton()->add_singleton(Globals::Singleton("Scoreloop", scoreloop));
150 	scoreloop->connect();
151 #endif
152 	*/
153 
154 #ifdef GAME_CENTER_ENABLED
155 	game_center = memnew(GameCenter);
156 	Globals::get_singleton()->add_singleton(Globals::Singleton("GameCenter", game_center));
157 	game_center->connect();
158 #endif
159 
160 #ifdef STOREKIT_ENABLED
161 	store_kit = memnew(InAppStore);
162 	Globals::get_singleton()->add_singleton(Globals::Singleton("InAppStore", store_kit));
163 #endif
164 
165 #ifdef ICLOUD_ENABLED
166 	icloud = memnew(ICloud);
167 	Globals::get_singleton()->add_singleton(Globals::Singleton("ICloud", icloud));
168 //icloud->connect();
169 #endif
170 	Globals::get_singleton()->add_singleton(Globals::Singleton("iOS", memnew(iOS)));
171 };
172 
get_main_loop() const173 MainLoop *OSIPhone::get_main_loop() const {
174 
175 	return main_loop;
176 };
177 
set_main_loop(MainLoop * p_main_loop)178 void OSIPhone::set_main_loop(MainLoop *p_main_loop) {
179 
180 	main_loop = p_main_loop;
181 
182 	if (main_loop) {
183 		input->set_main_loop(p_main_loop);
184 		main_loop->init();
185 	}
186 };
187 
iterate()188 bool OSIPhone::iterate() {
189 
190 	if (!main_loop)
191 		return true;
192 
193 	if (main_loop) {
194 		for (int i = 0; i < event_count; i++) {
195 
196 			input->parse_input_event(event_queue[i]);
197 		};
198 	};
199 	event_count = 0;
200 
201 	return Main::iteration();
202 };
203 
key(uint32_t p_key,bool p_pressed)204 void OSIPhone::key(uint32_t p_key, bool p_pressed) {
205 
206 	InputEvent ev;
207 	ev.type = InputEvent::KEY;
208 	ev.ID = ++last_event_id;
209 	ev.key.echo = false;
210 	ev.key.pressed = p_pressed;
211 	ev.key.scancode = p_key;
212 	ev.key.unicode = p_key;
213 	queue_event(ev);
214 };
215 
mouse_button(int p_idx,int p_x,int p_y,bool p_pressed,bool p_doubleclick,bool p_use_as_mouse)216 void OSIPhone::mouse_button(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick, bool p_use_as_mouse) {
217 
218 	if (!GLOBAL_DEF("debug/disable_touch", false)) {
219 		InputEvent ev;
220 		ev.type = InputEvent::SCREEN_TOUCH;
221 		ev.ID = ++last_event_id;
222 		ev.screen_touch.index = p_idx;
223 		ev.screen_touch.pressed = p_pressed;
224 		ev.screen_touch.x = p_x;
225 		ev.screen_touch.y = p_y;
226 		queue_event(ev);
227 	};
228 
229 	mouse_list.pressed[p_idx] = p_pressed;
230 
231 	if (p_use_as_mouse) {
232 
233 		InputEvent ev;
234 		ev.type = InputEvent::MOUSE_BUTTON;
235 		ev.device = 0;
236 		ev.mouse_button.pointer_index = p_idx;
237 		ev.ID = ++last_event_id;
238 
239 		// swaped it for tilted screen
240 		//ev.mouse_button.x = ev.mouse_button.global_x = video_mode.height - p_y;
241 		//ev.mouse_button.y = ev.mouse_button.global_y = p_x;
242 		ev.mouse_button.x = ev.mouse_button.global_x = p_x;
243 		ev.mouse_button.y = ev.mouse_button.global_y = p_y;
244 
245 		//mouse_list.pressed[p_idx] = p_pressed;
246 
247 		input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
248 		ev.mouse_button.button_index = BUTTON_LEFT;
249 		ev.mouse_button.doubleclick = p_doubleclick;
250 		ev.mouse_button.pressed = p_pressed;
251 
252 		queue_event(ev);
253 	};
254 };
255 
mouse_move(int p_idx,int p_prev_x,int p_prev_y,int p_x,int p_y,bool p_use_as_mouse)256 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) {
257 
258 	if (!GLOBAL_DEF("debug/disable_touch", false)) {
259 
260 		InputEvent ev;
261 		ev.type = InputEvent::SCREEN_DRAG;
262 		ev.ID = ++last_event_id;
263 		ev.screen_drag.index = p_idx;
264 		ev.screen_drag.x = p_x;
265 		ev.screen_drag.y = p_y;
266 		ev.screen_drag.relative_x = p_x - p_prev_x;
267 		ev.screen_drag.relative_y = p_y - p_prev_y;
268 		queue_event(ev);
269 	};
270 
271 	if (p_use_as_mouse) {
272 		InputEvent ev;
273 		ev.type = InputEvent::MOUSE_MOTION;
274 		ev.device = 0;
275 		ev.mouse_motion.pointer_index = p_idx;
276 		ev.ID = ++last_event_id;
277 
278 		if (true) { // vertical
279 
280 			ev.mouse_motion.x = ev.mouse_button.global_x = p_x;
281 			ev.mouse_motion.y = ev.mouse_button.global_y = p_y;
282 			ev.mouse_motion.relative_x = ev.mouse_motion.x - p_prev_x;
283 			ev.mouse_motion.relative_y = ev.mouse_motion.y - p_prev_y;
284 
285 		} else { // horizontal?
286 			ev.mouse_motion.x = ev.mouse_button.global_x = video_mode.height - p_y;
287 			ev.mouse_motion.y = ev.mouse_button.global_y = p_x;
288 			ev.mouse_motion.relative_x = ev.mouse_motion.x - (video_mode.height - p_prev_x);
289 			ev.mouse_motion.relative_y = ev.mouse_motion.y - p_prev_x;
290 		};
291 
292 		input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
293 		ev.mouse_motion.speed_x = input->get_mouse_speed().x;
294 		ev.mouse_motion.speed_y = input->get_mouse_speed().y;
295 		ev.mouse_motion.button_mask = 1; // pressed
296 
297 		queue_event(ev);
298 	};
299 };
300 
queue_event(const InputEvent & p_event)301 void OSIPhone::queue_event(const InputEvent &p_event) {
302 
303 	ERR_FAIL_INDEX(event_count, MAX_EVENTS);
304 
305 	event_queue[event_count++] = p_event;
306 };
307 
touches_cancelled()308 void OSIPhone::touches_cancelled() {
309 
310 	for (int i = 0; i < MAX_MOUSE_COUNT; i++) {
311 
312 		if (mouse_list.pressed[i]) {
313 
314 			// send a mouse_up outside the screen
315 			mouse_button(i, -1, -1, false, false, false);
316 		};
317 	};
318 };
319 
320 static const float ACCEL_RANGE = 1;
321 
update_gravity(float p_x,float p_y,float p_z)322 void OSIPhone::update_gravity(float p_x, float p_y, float p_z) {
323 	input->set_gravity(Vector3(p_x, p_y, p_z));
324 };
325 
update_accelerometer(float p_x,float p_y,float p_z)326 void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {
327 
328 	// Found out the Z should not be negated! Pass as is!
329 	input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, p_z / (float)ACCEL_RANGE));
330 };
331 
update_magnetometer(float p_x,float p_y,float p_z)332 void OSIPhone::update_magnetometer(float p_x, float p_y, float p_z) {
333 	input->set_magnetometer(Vector3(p_x, p_y, p_z));
334 };
335 
update_gyroscope(float p_x,float p_y,float p_z)336 void OSIPhone::update_gyroscope(float p_x, float p_y, float p_z) {
337 	input->set_gyroscope(Vector3(p_x, p_y, p_z));
338 };
339 
get_unused_joy_id()340 int OSIPhone::get_unused_joy_id() {
341 	return input->get_unused_joy_id();
342 };
343 
joy_connection_changed(int p_idx,bool p_connected,String p_name)344 void OSIPhone::joy_connection_changed(int p_idx, bool p_connected, String p_name) {
345 	input->joy_connection_changed(p_idx, p_connected, p_name);
346 };
347 
joy_button(int p_device,int p_button,bool p_pressed)348 void OSIPhone::joy_button(int p_device, int p_button, bool p_pressed) {
349 	// p_last_id is being retired..
350 	uint32_t last_id = input->joy_button(0, p_device, p_button, p_pressed);
351 };
352 
joy_axis(int p_device,int p_axis,const InputDefault::JoyAxis & p_value)353 void OSIPhone::joy_axis(int p_device, int p_axis, const InputDefault::JoyAxis &p_value) {
354 	// p_last_id is being retired..
355 	uint32_t last_id = input->joy_axis(0, p_device, p_axis, p_value);
356 };
357 
delete_main_loop()358 void OSIPhone::delete_main_loop() {
359 
360 	if (main_loop) {
361 		main_loop->finish();
362 		memdelete(main_loop);
363 	};
364 
365 	main_loop = NULL;
366 };
367 
finalize()368 void OSIPhone::finalize() {
369 
370 	if (main_loop) // should not happen?
371 		memdelete(main_loop);
372 
373 	spatial_sound_server->finish();
374 	memdelete(spatial_sound_server);
375 	spatial_sound_2d_server->finish();
376 	memdelete(spatial_sound_2d_server);
377 
378 	audio_server->finish();
379 	memdelete(audio_server);
380 
381 	visual_server->finish();
382 	memdelete(visual_server);
383 	memdelete(rasterizer);
384 
385 	physics_server->finish();
386 	memdelete(physics_server);
387 
388 	physics_2d_server->finish();
389 	memdelete(physics_2d_server);
390 
391 	memdelete(input);
392 };
393 
set_mouse_show(bool p_show)394 void OSIPhone::set_mouse_show(bool p_show){};
set_mouse_grab(bool p_grab)395 void OSIPhone::set_mouse_grab(bool p_grab){};
396 
is_mouse_grab_enabled() const397 bool OSIPhone::is_mouse_grab_enabled() const {
398 
399 	return true;
400 };
401 
get_mouse_pos() const402 Point2 OSIPhone::get_mouse_pos() const {
403 
404 	return Point2();
405 };
406 
get_mouse_button_state() const407 int OSIPhone::get_mouse_button_state() const {
408 
409 	return mouse_list.pressed[0];
410 };
411 
set_window_title(const String & p_title)412 void OSIPhone::set_window_title(const String &p_title){};
413 
alert(const String & p_alert,const String & p_title)414 void OSIPhone::alert(const String &p_alert, const String &p_title) {
415 
416 	const CharString utf8_alert = p_alert.utf8();
417 	const CharString utf8_title = p_title.utf8();
418 	iOS::alert(utf8_alert.get_data(), utf8_title.get_data());
419 }
420 
set_video_mode(const VideoMode & p_video_mode,int p_screen)421 void OSIPhone::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
422 
423 	video_mode = p_video_mode;
424 };
425 
get_video_mode(int p_screen) const426 OS::VideoMode OSIPhone::get_video_mode(int p_screen) const {
427 
428 	return video_mode;
429 };
430 
get_fullscreen_mode_list(List<VideoMode> * p_list,int p_screen) const431 void OSIPhone::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
432 
433 	p_list->push_back(video_mode);
434 };
435 
can_draw() const436 bool OSIPhone::can_draw() const {
437 
438 	if (native_video_is_playing())
439 		return false;
440 	return true;
441 };
442 
set_base_framebuffer(int p_fb)443 int OSIPhone::set_base_framebuffer(int p_fb) {
444 
445 	if (rasterizer_gles22) {
446 		rasterizer_gles22->set_base_framebuffer(p_fb);
447 	};
448 	return 0;
449 };
450 
has_virtual_keyboard() const451 bool OSIPhone::has_virtual_keyboard() const {
452 	return true;
453 };
454 
455 extern void _show_keyboard(String p_existing);
456 extern void _hide_keyboard();
457 extern Error _shell_open(String p_uri);
458 extern void _set_keep_screen_on(bool p_enabled);
459 
show_virtual_keyboard(const String & p_existing_text,const Rect2 & p_screen_rect)460 void OSIPhone::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
461 	_show_keyboard(p_existing_text);
462 };
463 
hide_virtual_keyboard()464 void OSIPhone::hide_virtual_keyboard() {
465 	_hide_keyboard();
466 };
467 
shell_open(String p_uri)468 Error OSIPhone::shell_open(String p_uri) {
469 	return _shell_open(p_uri);
470 };
471 
set_keep_screen_on(bool p_enabled)472 void OSIPhone::set_keep_screen_on(bool p_enabled) {
473 	OS::set_keep_screen_on(p_enabled);
474 	_set_keep_screen_on(p_enabled);
475 };
476 
set_cursor_shape(CursorShape p_shape)477 void OSIPhone::set_cursor_shape(CursorShape p_shape){
478 
479 };
480 
set_custom_mouse_cursor(const RES & p_cursor,CursorShape p_shape,const Vector2 & p_hotspot)481 void OSIPhone::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot){
482 
483 };
484 
get_data_dir() const485 String OSIPhone::get_data_dir() const {
486 
487 	return data_dir;
488 };
489 
get_name()490 String OSIPhone::get_name() {
491 
492 	return "iOS";
493 };
494 
get_window_size() const495 Size2 OSIPhone::get_window_size() const {
496 
497 	return Vector2(video_mode.width, video_mode.height);
498 }
499 
has_touchscreen_ui_hint() const500 bool OSIPhone::has_touchscreen_ui_hint() const {
501 
502 	return true;
503 }
504 
set_locale(String p_locale)505 void OSIPhone::set_locale(String p_locale) {
506 	locale_code = p_locale;
507 }
508 
get_locale() const509 String OSIPhone::get_locale() const {
510 	return locale_code;
511 }
512 
513 extern bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
514 extern bool _is_video_playing();
515 extern void _pause_video();
516 extern void _unpause_video();
517 extern void _stop_video();
518 extern void _focus_out_video();
519 
native_video_play(String p_path,float p_volume,String p_audio_track,String p_subtitle_track)520 Error OSIPhone::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
521 	FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
522 	bool exists = f && f->is_open();
523 
524 	String tempFile = get_data_dir();
525 	if (!exists)
526 		return FAILED;
527 
528 	if (p_path.begins_with("res://")) {
529 		if (PackedData::get_singleton()->has_path(p_path)) {
530 			print("Unable to play %S using the native player as it resides in a .pck file\n", p_path.c_str());
531 			return ERR_INVALID_PARAMETER;
532 		} else {
533 			p_path = p_path.replace("res:/", Globals::get_singleton()->get_resource_path());
534 		}
535 	} else if (p_path.begins_with("user://"))
536 		p_path = p_path.replace("user:/", get_data_dir());
537 
538 	memdelete(f);
539 
540 	print("Playing video: %S\n", p_path.c_str());
541 	if (_play_video(p_path, p_volume, p_audio_track, p_subtitle_track))
542 		return OK;
543 	return FAILED;
544 }
545 
native_video_is_playing() const546 bool OSIPhone::native_video_is_playing() const {
547 	return _is_video_playing();
548 }
549 
native_video_pause()550 void OSIPhone::native_video_pause() {
551 	if (native_video_is_playing())
552 		_pause_video();
553 }
554 
native_video_unpause()555 void OSIPhone::native_video_unpause() {
556 	_unpause_video();
557 };
558 
native_video_focus_out()559 void OSIPhone::native_video_focus_out() {
560 	_focus_out_video();
561 };
562 
native_video_stop()563 void OSIPhone::native_video_stop() {
564 	if (native_video_is_playing())
565 		_stop_video();
566 }
567 
OSIPhone(int width,int height)568 OSIPhone::OSIPhone(int width, int height) {
569 
570 	rasterizer_gles22 = NULL;
571 	main_loop = NULL;
572 	visual_server = NULL;
573 	rasterizer = NULL;
574 
575 	VideoMode vm;
576 	vm.fullscreen = true;
577 	vm.width = width;
578 	vm.height = height;
579 	vm.resizable = false;
580 	set_video_mode(vm);
581 	event_count = 0;
582 	last_event_id = 0;
583 };
584 
~OSIPhone()585 OSIPhone::~OSIPhone() {
586 }
587 
588 #endif
589