summaryrefslogtreecommitdiff
path: root/platform/iphone/os_iphone.cpp
blob: b60da43374f8ede7aa7d0521a3791b20e0843bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*************************************************************************/
/*  os_iphone.cpp                                                        */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#ifdef IPHONE_ENABLED

#include "os_iphone.h"

#include "drivers/gles2/rasterizer_gles2.h"


#include "servers/visual/visual_server_raster.h"
#include "servers/visual/visual_server_wrap_mt.h"

#include "main/main.h"
#include "audio_driver_iphone.h"

#include "core/os/dir_access.h"
#include "core/os/file_access.h"
#include "core/globals.h"

#include "sem_iphone.h"

int OSIPhone::get_video_driver_count() const {

	return 1;
};

const char * OSIPhone::get_video_driver_name(int p_driver) const {

	return "GLES2";
};

OSIPhone* OSIPhone::get_singleton() {

	return (OSIPhone*)OS::get_singleton();
};


OS::VideoMode OSIPhone::get_default_video_mode() const {

	return video_mode;
};

uint8_t OSIPhone::get_orientations() const {

	return supported_orientations;
};

extern int gl_view_base_fb; // from gl_view.mm

void OSIPhone::set_data_dir(String p_dir) {

	DirAccess* da = DirAccess::open(p_dir);

	data_dir = da->get_current_dir();
	printf("setting data dir to %ls from %ls\n", data_dir.c_str(), p_dir.c_str());
	memdelete(da);
};

void OSIPhone::set_unique_ID(String p_ID) {

	unique_ID = p_ID;
};

String OSIPhone::get_unique_ID() const {

	return unique_ID;
};

void OSIPhone::initialize_core() {

	OS_Unix::initialize_core();
	SemaphoreIphone::make_default();
};

void OSIPhone::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {

	supported_orientations = 0;
	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal", true)?1:0) << LandscapeLeft);
	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal_flipped", false)?1:0) << LandscapeRight);
	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical", false)?1:0) << PortraitDown);
	supported_orientations |= ((GLOBAL_DEF("video_mode/allow_vertical_flipped", false)?1:0) << PortraitUp);

	rasterizer_gles22 = memnew( RasterizerGLES2(false, false, false) );
	rasterizer = rasterizer_gles22;
	rasterizer_gles22->set_base_framebuffer(gl_view_base_fb);

	visual_server = memnew( VisualServerRaster(rasterizer) );
	if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {

		visual_server = memnew(VisualServerWrapMT(visual_server, false));
	};
	visual_server->init();

	visual_server->init();
	visual_server->cursor_set_visible(false, 0);

	audio_driver = memnew(AudioDriverIphone);
	audio_driver->set_singleton();
	audio_driver->init();

	sample_manager = memnew( SampleManagerMallocSW );
	audio_server = memnew( AudioServerSW(sample_manager) );
	audio_server->init();
	spatial_sound_server = memnew( SpatialSoundServerSW );
	spatial_sound_server->init();

	spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
	spatial_sound_2d_server->init();

	//
	physics_server = memnew( PhysicsServerSW );
	physics_server->init();
	//physics_2d_server = memnew( Physics2DServerSW );
	physics_2d_server = Physics2DServerWrapMT::init_server<Physics2DServerSW>();
	physics_2d_server->init();

	input = memnew( InputDefault );

	/*
#ifdef IOS_SCORELOOP_ENABLED
	scoreloop = memnew(ScoreloopIOS);
	Globals::get_singleton()->add_singleton(Globals::Singleton("Scoreloop", scoreloop));
	scoreloop->connect();
#endif
	*/

#ifdef GAME_CENTER_ENABLED
	game_center = memnew(GameCenter);
	Globals::get_singleton()->add_singleton(Globals::Singleton("GameCenter", game_center));
	game_center->connect();
#endif

#ifdef STOREKIT_ENABLED
	store_kit = memnew(InAppStore);
	Globals::get_singleton()->add_singleton(Globals::Singleton("InAppStore", store_kit));
#endif		

#ifdef ICLOUD_ENABLED
	icloud = memnew(ICloud);
	Globals::get_singleton()->add_singleton(Globals::Singleton("ICloud", icloud));
	//icloud->connect();
#endif		
};

MainLoop *OSIPhone::get_main_loop() const {

	return main_loop;
};


void OSIPhone::set_main_loop( MainLoop * p_main_loop ) {

	main_loop = p_main_loop;

	if (main_loop) {
		input->set_main_loop(p_main_loop);
		main_loop->init();
	}
};


bool OSIPhone::iterate() {

	if (!main_loop)
		return true;

	if (main_loop) {
		for (int i=0; i<event_count; i++) {

			input->parse_input_event(event_queue[i]);
		};
	};
	event_count = 0;

	return Main::iteration();
};

void OSIPhone::key(uint32_t p_key, bool p_pressed) {

	InputEvent ev;
	ev.type = InputEvent::KEY;
	ev.ID = ++last_event_id;
	ev.key.echo = false;
	ev.key.pressed = p_pressed;
	ev.key.scancode = p_key;
	ev.key.unicode = p_key;
	queue_event(ev);
};

void OSIPhone::mouse_button(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick, bool p_use_as_mouse) {

	if (!GLOBAL_DEF("debug/disable_touch", false)) {
		InputEvent ev;
		ev.type = InputEvent::SCREEN_TOUCH;
		ev.ID = ++last_event_id;
		ev.screen_touch.index=p_idx;
		ev.screen_touch.pressed=p_pressed;
		ev.screen_touch.x=p_x;
		ev.screen_touch.y=p_y;
		queue_event(ev);
	};

	mouse_list.pressed[p_idx] = p_pressed;

	if (p_use_as_mouse) {

		InputEvent ev;
		ev.type = InputEvent::MOUSE_BUTTON;
		ev.device = 0;
		ev.mouse_button.pointer_index = p_idx;
		ev.ID = ++last_event_id;

		// swaped it for tilted screen
		//ev.mouse_button.x = ev.mouse_button.global_x = video_mode.height - p_y;
		//ev.mouse_button.y = ev.mouse_button.global_y = p_x;
		ev.mouse_button.x = ev.mouse_button.global_x = p_x;
		ev.mouse_button.y = ev.mouse_button.global_y = p_y;

		//mouse_list.pressed[p_idx] = p_pressed;

		input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
		ev.mouse_button.button_index = BUTTON_LEFT;
		ev.mouse_button.doubleclick = p_doubleclick;
		ev.mouse_button.pressed = p_pressed;

		queue_event(ev);
	};
};

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) {

	if (!GLOBAL_DEF("debug/disable_touch", false)) {

		InputEvent ev;
		ev.type=InputEvent::SCREEN_DRAG;
		ev.ID = ++last_event_id;
		ev.screen_drag.index=p_idx;
		ev.screen_drag.x=p_x;
		ev.screen_drag.y=p_y;
		ev.screen_drag.relative_x = p_x - p_prev_x;
		ev.screen_drag.relative_y = p_y - p_prev_y;
		queue_event(ev);
	};

	if (p_use_as_mouse) {
		InputEvent ev;
		ev.type = InputEvent::MOUSE_MOTION;
		ev.device = 0;
		ev.mouse_motion.pointer_index = p_idx;
		ev.ID = ++last_event_id;

		if (true) { // vertical

			ev.mouse_motion.x = ev.mouse_button.global_x = p_x;
			ev.mouse_motion.y = ev.mouse_button.global_y = p_y;
			ev.mouse_motion.relative_x = ev.mouse_motion.x - p_prev_x;
			ev.mouse_motion.relative_y = ev.mouse_motion.y - p_prev_y;

		} else { // horizontal?
			ev.mouse_motion.x = ev.mouse_button.global_x = video_mode.height - p_y;
			ev.mouse_motion.y = ev.mouse_button.global_y = p_x;
			ev.mouse_motion.relative_x = ev.mouse_motion.x - (video_mode.height - p_prev_x);
			ev.mouse_motion.relative_y = ev.mouse_motion.y - p_prev_x;
		};

		input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
		ev.mouse_motion.speed_x=input->get_mouse_speed().x;
		ev.mouse_motion.speed_y=input->get_mouse_speed().y;
		ev.mouse_motion.button_mask = 1; // pressed

		queue_event(ev);
	};
};

void OSIPhone::queue_event(const InputEvent& p_event) {

	ERR_FAIL_INDEX( event_count, MAX_EVENTS );

	event_queue[event_count++] = p_event; 
};

void OSIPhone::touches_cancelled() {

	for (int i=0; i<MAX_MOUSE_COUNT; i++) {

		if (mouse_list.pressed[i]) {

			// send a mouse_up outside the screen
			mouse_button(i, -1, -1, false, false, false);
		};
	};
};

static const float ACCEL_RANGE = 1;

void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {

	input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, -p_z / (float)ACCEL_RANGE));

	/*
	if (p_x != last_accel.x) {
		//printf("updating accel x %f\n", p_x);
		InputEvent ev;
		ev.type = InputEvent::JOYSTICK_MOTION;
		ev.device = 0;
		ev.joy_motion.axis = JOY_ANALOG_0_X;
		ev.joy_motion.axis_value = (p_x / (float)ACCEL_RANGE);
		ev.ID = ++last_event_id;
		last_accel.x = p_x;
		queue_event(ev);
	};
	if (p_y != last_accel.y) {
		//printf("updating accel y %f\n", p_y);
		InputEvent ev;
		ev.type = InputEvent::JOYSTICK_MOTION;
		ev.device = 0;
		ev.joy_motion.axis = JOY_ANALOG_0_Y;
		ev.joy_motion.axis_value = (p_y / (float)ACCEL_RANGE);
		ev.ID = ++last_event_id;
		last_accel.y = p_y;
		queue_event(ev);
	};
	if (p_z != last_accel.z) {
		//printf("updating accel z %f\n", p_z);
		InputEvent ev;
		ev.type = InputEvent::JOYSTICK_MOTION;
		ev.device = 0;
		ev.joy_motion.axis = JOY_ANALOG_1_X;
		ev.joy_motion.axis_value = ( (1.0 - p_z) / (float)ACCEL_RANGE);
		ev.ID = ++last_event_id;
		last_accel.z = p_z;
		queue_event(ev);
	};
	*/
};



void OSIPhone::delete_main_loop() {

	if (main_loop) {
		main_loop->finish();
		memdelete(main_loop);
	};

	main_loop = NULL;
};

void OSIPhone::finalize() {

	if(main_loop) // should not happen?
		memdelete(main_loop);

	visual_server->finish();
	memdelete(visual_server);
	memdelete(rasterizer);

	physics_server->finish();
	memdelete(physics_server);

	physics_2d_server->finish();
	memdelete(physics_2d_server);

	spatial_sound_server->finish();
	memdelete(spatial_sound_server);

	memdelete(input);

	spatial_sound_2d_server->finish();
	memdelete(spatial_sound_2d_server);

};

void OSIPhone::set_mouse_show(bool p_show) {  };
void OSIPhone::set_mouse_grab(bool p_grab) {  };

bool OSIPhone::is_mouse_grab_enabled() const {

	return true;
};

Point2 OSIPhone::get_mouse_pos() const {

	return Point2();
};

int OSIPhone::get_mouse_button_state() const {

	return mouse_list.pressed[0];
};

void OSIPhone::set_window_title(const String& p_title) {  };

void OSIPhone::set_video_mode(const VideoMode& p_video_mode, int p_screen) {

	video_mode = p_video_mode;
};

OS::VideoMode OSIPhone::get_video_mode(int p_screen) const {

	return video_mode;
};

void OSIPhone::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {

	p_list->push_back(video_mode);
};

bool OSIPhone::can_draw() const {

	if (native_video_is_playing())
		return false;
	return true;
};

int OSIPhone::set_base_framebuffer(int p_fb) {

	if (rasterizer_gles22) {
		rasterizer_gles22->set_base_framebuffer(p_fb);
	};
	return 0;
};

bool OSIPhone::has_virtual_keyboard() const {
	return true;
};

extern void _show_keyboard(String p_existing);
extern void _hide_keyboard();
extern Error _shell_open(String p_uri);

void OSIPhone::show_virtual_keyboard(const String& p_existing_text,const Rect2& p_screen_rect) {
	_show_keyboard(p_existing_text);
};

void OSIPhone::hide_virtual_keyboard() {
	_hide_keyboard();
};

Error OSIPhone::shell_open(String p_uri) {
	return _shell_open(p_uri);
};


void OSIPhone::set_cursor_shape(CursorShape p_shape) {


};

String OSIPhone::get_data_dir() const {

	return data_dir;
};

String OSIPhone::get_name() {

	return "iOS";
};

Size2 OSIPhone::get_window_size() const {
	
	return Vector2(video_mode.width, video_mode.height);
}

bool OSIPhone::has_touchscreen_ui_hint() const {

	return true;
}

void OSIPhone::set_locale(String p_locale)
{
	locale_code = p_locale;
}

String OSIPhone::get_locale() const {
	return locale_code;
}

extern bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
extern bool _is_video_playing();
extern void _pause_video();
extern void _unpause_video();
extern void _stop_video();
extern void _focus_out_video();

Error OSIPhone::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
	FileAccess* f = FileAccess::open(p_path, FileAccess::READ);
	bool exists = f && f->is_open();
	printf("file exists for %ls, %i, %p\n", p_path.c_str(), (int)exists, f);
	if (f)
		memdelete(f);
	if (!exists)
		return FAILED;
    if ( _play_video(p_path, p_volume, p_audio_track, p_subtitle_track) )
		return OK;
	return FAILED;
}

bool OSIPhone::native_video_is_playing() const {
    return _is_video_playing();
}

void OSIPhone::native_video_pause() {
	if (native_video_is_playing())
    	_pause_video();
}

void OSIPhone::native_video_unpause() {
	_unpause_video();
};

void OSIPhone::native_video_focus_out() {
	_focus_out_video();
};

void OSIPhone::native_video_stop() {
	if (native_video_is_playing())
    	_stop_video();
}

OSIPhone::OSIPhone(int width, int height) {

	rasterizer_gles22 = NULL;
	main_loop = NULL;
	visual_server = NULL;
	rasterizer = NULL;

	VideoMode vm;
	vm.fullscreen = true;
	vm.width = width;
	vm.height = height;
	vm.resizable = false;
	set_video_mode(vm);
	event_count = 0;
	last_event_id = 0;
};

OSIPhone::~OSIPhone() {

}

#endif