Debian Codegraph @

drivers/apple_embedded/display_server_apple_embedded.mm

raw
1 /**************************************************************************/
2 /* display_server_apple_embedded.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31 #import "display_server_apple_embedded.h"
32
33 #import "app_delegate_service.h"
34 #import "apple_embedded.h"
35 #import "godot_keyboard_input_view.h"
36 #import "godot_view_apple_embedded.h"
37 #import "godot_view_controller.h"
38 #import "key_mapping_apple_embedded.h"
39 #import "os_apple_embedded.h"
40 #import "tts_apple_embedded.h"
41
42 #include "core/config/project_settings.h"
43 #include "core/io/file_access_pack.h"
44
45 #import <GameController/GameController.h>
46
47 static const float kDisplayServerIOSAcceleration = 1.f;
48
49 DisplayServerAppleEmbedded *DisplayServerAppleEmbedded::get_singleton() {
50 return (DisplayServerAppleEmbedded *)DisplayServer::get_singleton();
51 }
52
53 DisplayServerAppleEmbedded::DisplayServerAppleEmbedded(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) {
54 KeyMappingAppleEmbedded::initialize();
55
56 rendering_driver = p_rendering_driver;
57
58 // Init TTS
59 bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
60 if (tts_enabled) {
61 initialize_tts();
62 }
63 native_menu = memnew(NativeMenu);
64
65 bool has_made_render_compositor_current = false;
66
67 #if defined(RD_ENABLED)
68 rendering_context = nullptr;
69 rendering_device = nullptr;
70
71 CALayer *layer = nullptr;
72
73 union {
74 #ifdef VULKAN_ENABLED
75 RenderingContextDriverVulkanAppleEmbedded::WindowPlatformData vulkan;
76 #endif
77 #ifdef METAL_ENABLED
78 GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wunguarded-availability")
79 // Eliminate "RenderingContextDriverMetal is only available on iOS 14.0 or newer".
80 RenderingContextDriverMetal::WindowPlatformData metal;
81 GODOT_CLANG_WARNING_POP
82 #endif
83 } wpd;
84
85 #if defined(VULKAN_ENABLED)
86 if (rendering_driver == "vulkan") {
87 layer = [GDTAppDelegateService.viewController.godotView initializeRenderingForDriver:@"vulkan"];
88 if (!layer) {
89 ERR_FAIL_MSG("Failed to create iOS Vulkan rendering layer.");
90 }
91 wpd.vulkan.layer_ptr = (CAMetalLayer *const *)&layer;
92 rendering_context = memnew(RenderingContextDriverVulkanAppleEmbedded);
93 }
94 #endif
95 #ifdef METAL_ENABLED
96 if (rendering_driver == "metal") {
97 if (@available(iOS 14.0, *)) {
98 layer = [GDTAppDelegateService.viewController.godotView initializeRenderingForDriver:@"metal"];
99 wpd.metal.layer = (CAMetalLayer *)layer;
100 rendering_context = memnew(RenderingContextDriverMetal);
101 } else {
102 OS::get_singleton()->alert("Metal is only supported on iOS 14.0 and later.");
103 r_error = ERR_UNAVAILABLE;
104 return;
105 }
106 }
107 #endif
108 if (rendering_context) {
109 if (rendering_context->initialize() != OK) {
110 memdelete(rendering_context);
111 rendering_context = nullptr;
112 #if defined(GLES3_ENABLED)
113 bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3");
114 if (fallback_to_opengl3 && rendering_driver != "opengl3") {
115 WARN_PRINT("Your device does not seem to support MoltenVK or Metal, switching to OpenGL 3.");
116 rendering_driver = "opengl3";
117 OS::get_singleton()->set_current_rendering_method("gl_compatibility");
118 OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
119 } else
120 #endif
121 {
122 ERR_PRINT(vformat("Failed to initialize %s context", rendering_driver));
123 r_error = ERR_UNAVAILABLE;
124 return;
125 }
126 }
127 }
128
129 if (rendering_context) {
130 if (rendering_context->window_create(MAIN_WINDOW_ID, &wpd) != OK) {
131 ERR_PRINT(vformat("Failed to create %s window.", rendering_driver));
132 memdelete(rendering_context);
133 rendering_context = nullptr;
134 r_error = ERR_UNAVAILABLE;
135 return;
136 }
137
138 Size2i size = Size2i(layer.bounds.size.width, layer.bounds.size.height) * screen_get_max_scale();
139 rendering_context->window_set_size(MAIN_WINDOW_ID, size.width, size.height);
140 rendering_context->window_set_vsync_mode(MAIN_WINDOW_ID, p_vsync_mode);
141
142 rendering_device = memnew(RenderingDevice);
143 if (rendering_device->initialize(rendering_context, MAIN_WINDOW_ID) != OK) {
144 rendering_device = nullptr;
145 memdelete(rendering_context);
146 rendering_context = nullptr;
147 r_error = ERR_UNAVAILABLE;
148 return;
149 }
150 rendering_device->screen_create(MAIN_WINDOW_ID);
151
152 RendererCompositorRD::make_current();
153 has_made_render_compositor_current = true;
154 }
155 #endif
156
157 #if defined(GLES3_ENABLED)
158 if (rendering_driver == "opengl3") {
159 CALayer *layer = [GDTAppDelegateService.viewController.godotView initializeRenderingForDriver:@"opengl3"];
160
161 if (!layer) {
162 ERR_FAIL_MSG("Failed to create iOS OpenGLES rendering layer.");
163 }
164
165 RasterizerGLES3::make_current(false);
166 has_made_render_compositor_current = true;
167 }
168 #endif
169
170 ERR_FAIL_COND_MSG(!has_made_render_compositor_current, vformat("Failed to make RendererCompositor current for rendering driver %s", rendering_driver));
171
172 bool keep_screen_on = bool(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
173 screen_set_keep_on(keep_screen_on);
174
175 Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
176
177 r_error = OK;
178 }
179
180 DisplayServerAppleEmbedded::~DisplayServerAppleEmbedded() {
181 if (native_menu) {
182 memdelete(native_menu);
183 native_menu = nullptr;
184 }
185
186 #if defined(RD_ENABLED)
187 if (rendering_device) {
188 rendering_device->screen_free(MAIN_WINDOW_ID);
189 memdelete(rendering_device);
190 rendering_device = nullptr;
191 }
192
193 if (rendering_context) {
194 rendering_context->window_destroy(MAIN_WINDOW_ID);
195 memdelete(rendering_context);
196 rendering_context = nullptr;
197 }
198 #endif
199 }
200
201 Vector<String> DisplayServerAppleEmbedded::get_rendering_drivers_func() {
202 Vector<String> drivers;
203
204 #if defined(VULKAN_ENABLED)
205 drivers.push_back("vulkan");
206 #endif
207 #if defined(METAL_ENABLED)
208 if (@available(ios 14.0, *)) {
209 drivers.push_back("metal");
210 }
211 #endif
212 #if defined(GLES3_ENABLED)
213 drivers.push_back("opengl3");
214 #endif
215
216 return drivers;
217 }
218
219 // MARK: Events
220
221 void DisplayServerAppleEmbedded::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
222 window_resize_callback = p_callable;
223 }
224
225 void DisplayServerAppleEmbedded::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
226 window_event_callback = p_callable;
227 }
228 void DisplayServerAppleEmbedded::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
229 input_event_callback = p_callable;
230 }
231
232 void DisplayServerAppleEmbedded::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
233 input_text_callback = p_callable;
234 }
235
236 void DisplayServerAppleEmbedded::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
237 // Probably not supported for iOS
238 }
239
240 void DisplayServerAppleEmbedded::process_events() {
241 Input::get_singleton()->flush_buffered_events();
242 }
243
244 void DisplayServerAppleEmbedded::_dispatch_input_events(const Ref<InputEvent> &p_event) {
245 DisplayServerAppleEmbedded::get_singleton()->send_input_event(p_event);
246 }
247
248 void DisplayServerAppleEmbedded::send_input_event(const Ref<InputEvent> &p_event) const {
249 _window_callback(input_event_callback, p_event);
250 }
251
252 void DisplayServerAppleEmbedded::send_input_text(const String &p_text) const {
253 _window_callback(input_text_callback, p_text);
254 }
255
256 void DisplayServerAppleEmbedded::send_window_event(DisplayServer::WindowEvent p_event) const {
257 _window_callback(window_event_callback, int(p_event));
258 }
259
260 void DisplayServerAppleEmbedded::_window_callback(const Callable &p_callable, const Variant &p_arg) const {
261 if (p_callable.is_valid()) {
262 p_callable.call(p_arg);
263 }
264 }
265
266 // MARK: - Input
267
268 // MARK: Touches
269
270 void DisplayServerAppleEmbedded::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click) {
271 Ref<InputEventScreenTouch> ev;
272 ev.instantiate();
273
274 ev->set_index(p_idx);
275 ev->set_pressed(p_pressed);
276 ev->set_position(Vector2(p_x, p_y));
277 ev->set_double_tap(p_double_click);
278 perform_event(ev);
279 }
280
281 void DisplayServerAppleEmbedded::touch_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y, float p_pressure, Vector2 p_tilt) {
282 Ref<InputEventScreenDrag> ev;
283 ev.instantiate();
284 ev->set_index(p_idx);
285 ev->set_pressure(p_pressure);
286 ev->set_tilt(p_tilt);
287 ev->set_position(Vector2(p_x, p_y));
288 ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
289 ev->set_relative_screen_position(ev->get_relative());
290 perform_event(ev);
291 }
292
293 void DisplayServerAppleEmbedded::perform_event(const Ref<InputEvent> &p_event) {
294 Input *input_singleton = Input::get_singleton();
295 if (input_singleton == nullptr) {
296 return;
297 }
298
299 input_singleton->parse_input_event(p_event);
300 }
301
302 void DisplayServerAppleEmbedded::touches_canceled(int p_idx) {
303 touch_press(p_idx, -1, -1, false, false);
304 }
305
306 // MARK: Keyboard
307
308 void DisplayServerAppleEmbedded::key(Key p_key, char32_t p_char, Key p_unshifted, Key p_physical, NSInteger p_modifier, bool p_pressed, KeyLocation p_location) {
309 Ref<InputEventKey> ev;
310 ev.instantiate();
311 ev->set_echo(false);
312 ev->set_pressed(p_pressed);
313 ev->set_keycode(fix_keycode(p_char, p_key));
314 if (@available(iOS 13.4, *)) {
315 if (p_key != Key::SHIFT) {
316 ev->set_shift_pressed(p_modifier & UIKeyModifierShift);
317 }
318 if (p_key != Key::CTRL) {
319 ev->set_ctrl_pressed(p_modifier & UIKeyModifierControl);
320 }
321 if (p_key != Key::ALT) {
322 ev->set_alt_pressed(p_modifier & UIKeyModifierAlternate);
323 }
324 if (p_key != Key::META) {
325 ev->set_meta_pressed(p_modifier & UIKeyModifierCommand);
326 }
327 }
328 ev->set_key_label(p_unshifted);
329 ev->set_physical_keycode(p_physical);
330 ev->set_unicode(fix_unicode(p_char));
331 ev->set_location(p_location);
332 perform_event(ev);
333 }
334
335 // MARK: Motion
336
337 void DisplayServerAppleEmbedded::update_gravity(const Vector3 &p_gravity) {
338 Input::get_singleton()->set_gravity(p_gravity);
339 }
340
341 void DisplayServerAppleEmbedded::update_accelerometer(const Vector3 &p_accelerometer) {
342 Input::get_singleton()->set_accelerometer(p_accelerometer / kDisplayServerIOSAcceleration);
343 }
344
345 void DisplayServerAppleEmbedded::update_magnetometer(const Vector3 &p_magnetometer) {
346 Input::get_singleton()->set_magnetometer(p_magnetometer);
347 }
348
349 void DisplayServerAppleEmbedded::update_gyroscope(const Vector3 &p_gyroscope) {
350 Input::get_singleton()->set_gyroscope(p_gyroscope);
351 }
352
353 // MARK: -
354
355 bool DisplayServerAppleEmbedded::has_feature(Feature p_feature) const {
356 switch (p_feature) {
357 #ifndef DISABLE_DEPRECATED
358 case FEATURE_GLOBAL_MENU: {
359 return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU));
360 } break;
361 #endif
362 // case FEATURE_CURSOR_SHAPE:
363 // case FEATURE_CUSTOM_CURSOR_SHAPE:
364 // case FEATURE_HIDPI:
365 // case FEATURE_ICON:
366 // case FEATURE_IME:
367 // case FEATURE_MOUSE:
368 // case FEATURE_MOUSE_WARP:
369 // case FEATURE_NATIVE_DIALOG:
370 // case FEATURE_NATIVE_DIALOG_INPUT:
371 // case FEATURE_NATIVE_DIALOG_FILE:
372 // case FEATURE_NATIVE_DIALOG_FILE_EXTRA:
373 // case FEATURE_NATIVE_DIALOG_FILE_MIME:
374 // case FEATURE_NATIVE_ICON:
375 // case FEATURE_WINDOW_TRANSPARENCY:
376 case FEATURE_CLIPBOARD:
377 case FEATURE_KEEP_SCREEN_ON:
378 case FEATURE_ORIENTATION:
379 case FEATURE_TOUCHSCREEN:
380 case FEATURE_VIRTUAL_KEYBOARD:
381 case FEATURE_TEXT_TO_SPEECH:
382 return true;
383 default:
384 return false;
385 }
386 }
387
388 void DisplayServerAppleEmbedded::initialize_tts() const {
389 const_cast<DisplayServerAppleEmbedded *>(this)->tts = [[GDTTTS alloc] init];
390 }
391
392 bool DisplayServerAppleEmbedded::tts_is_speaking() const {
393 if (unlikely(!tts)) {
394 initialize_tts();
395 }
396 ERR_FAIL_NULL_V(tts, false);
397 return [tts isSpeaking];
398 }
399
400 bool DisplayServerAppleEmbedded::tts_is_paused() const {
401 if (unlikely(!tts)) {
402 initialize_tts();
403 }
404 ERR_FAIL_NULL_V(tts, false);
405 return [tts isPaused];
406 }
407
408 TypedArray<Dictionary> DisplayServerAppleEmbedded::tts_get_voices() const {
409 if (unlikely(!tts)) {
410 initialize_tts();
411 }
412 ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>());
413 return [tts getVoices];
414 }
415
416 void DisplayServerAppleEmbedded::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) {
417 if (unlikely(!tts)) {
418 initialize_tts();
419 }
420 ERR_FAIL_NULL(tts);
421 [tts speak:p_text voice:p_voice volume:p_volume pitch:p_pitch rate:p_rate utterance_id:p_utterance_id interrupt:p_interrupt];
422 }
423
424 void DisplayServerAppleEmbedded::tts_pause() {
425 if (unlikely(!tts)) {
426 initialize_tts();
427 }
428 ERR_FAIL_NULL(tts);
429 [tts pauseSpeaking];
430 }
431
432 void DisplayServerAppleEmbedded::tts_resume() {
433 if (unlikely(!tts)) {
434 initialize_tts();
435 }
436 ERR_FAIL_NULL(tts);
437 [tts resumeSpeaking];
438 }
439
440 void DisplayServerAppleEmbedded::tts_stop() {
441 if (unlikely(!tts)) {
442 initialize_tts();
443 }
444 ERR_FAIL_NULL(tts);
445 [tts stopSpeaking];
446 }
447
448 bool DisplayServerAppleEmbedded::is_dark_mode_supported() const {
449 if (@available(iOS 13.0, *)) {
450 return true;
451 } else {
452 return false;
453 }
454 }
455
456 bool DisplayServerAppleEmbedded::is_dark_mode() const {
457 if (@available(iOS 13.0, *)) {
458 return [UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark;
459 } else {
460 return false;
461 }
462 }
463
464 void DisplayServerAppleEmbedded::set_system_theme_change_callback(const Callable &p_callable) {
465 system_theme_changed = p_callable;
466 }
467
468 void DisplayServerAppleEmbedded::emit_system_theme_changed() {
469 if (system_theme_changed.is_valid()) {
470 Variant ret;
471 Callable::CallError ce;
472 system_theme_changed.callp(nullptr, 0, ret, ce);
473 if (ce.error != Callable::CallError::CALL_OK) {
474 ERR_PRINT(vformat("Failed to execute system theme changed callback: %s.", Variant::get_callable_error_text(system_theme_changed, nullptr, 0, ce)));
475 }
476 }
477 }
478
479 Rect2i DisplayServerAppleEmbedded::get_display_safe_area() const {
480 UIEdgeInsets insets = UIEdgeInsetsZero;
481 UIView *view = GDTAppDelegateService.viewController.godotView;
482 if ([view respondsToSelector:@selector(safeAreaInsets)]) {
483 insets = [view safeAreaInsets];
484 }
485 float scale = screen_get_scale();
486 Size2i insets_position = Size2i(insets.left, insets.top) * scale;
487 Size2i insets_size = Size2i(insets.left + insets.right, insets.top + insets.bottom) * scale;
488 return Rect2i(screen_get_position() + insets_position, screen_get_size() - insets_size);
489 }
490
491 int DisplayServerAppleEmbedded::get_screen_count() const {
492 return 1;
493 }
494
495 int DisplayServerAppleEmbedded::get_primary_screen() const {
496 return 0;
497 }
498
499 Point2i DisplayServerAppleEmbedded::screen_get_position(int p_screen) const {
500 p_screen = _get_screen_index(p_screen);
501 int screen_count = get_screen_count();
502 ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i());
503
504 return Point2i(0, 0);
505 }
506
507 Size2i DisplayServerAppleEmbedded::screen_get_size(int p_screen) const {
508 p_screen = _get_screen_index(p_screen);
509 int screen_count = get_screen_count();
510 ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i());
511
512 CALayer *layer = GDTAppDelegateService.viewController.godotView.renderingLayer;
513
514 if (!layer) {
515 return Size2i();
516 }
517
518 return Size2i(layer.bounds.size.width, layer.bounds.size.height) * screen_get_scale(p_screen);
519 }
520
521 Rect2i DisplayServerAppleEmbedded::screen_get_usable_rect(int p_screen) const {
522 p_screen = _get_screen_index(p_screen);
523 int screen_count = get_screen_count();
524 ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i());
525
526 return Rect2i(screen_get_position(p_screen), screen_get_size(p_screen));
527 }
528
529 Vector<DisplayServer::WindowID> DisplayServerAppleEmbedded::get_window_list() const {
530 Vector<DisplayServer::WindowID> list;
531 list.push_back(MAIN_WINDOW_ID);
532 return list;
533 }
534
535 DisplayServer::WindowID DisplayServerAppleEmbedded::get_window_at_screen_position(const Point2i &p_position) const {
536 return MAIN_WINDOW_ID;
537 }
538
539 int64_t DisplayServerAppleEmbedded::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
540 ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, 0);
541 switch (p_handle_type) {
542 case DISPLAY_HANDLE: {
543 return 0; // Not supported.
544 }
545 case WINDOW_HANDLE: {
546 return (int64_t)GDTAppDelegateService.viewController;
547 }
548 case WINDOW_VIEW: {
549 return (int64_t)GDTAppDelegateService.viewController.godotView;
550 }
551 default: {
552 return 0;
553 }
554 }
555 }
556
557 void DisplayServerAppleEmbedded::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
558 window_attached_instance_id = p_instance;
559 }
560
561 ObjectID DisplayServerAppleEmbedded::window_get_attached_instance_id(WindowID p_window) const {
562 return window_attached_instance_id;
563 }
564
565 void DisplayServerAppleEmbedded::window_set_title(const String &p_title, WindowID p_window) {
566 // Probably not supported for iOS
567 }
568
569 int DisplayServerAppleEmbedded::window_get_current_screen(WindowID p_window) const {
570 ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, INVALID_SCREEN);
571 return 0;
572 }
573
574 void DisplayServerAppleEmbedded::window_set_current_screen(int p_screen, WindowID p_window) {
575 // Probably not supported for iOS
576 }
577
578 Point2i DisplayServerAppleEmbedded::window_get_position(WindowID p_window) const {
579 return Point2i();
580 }
581
582 Point2i DisplayServerAppleEmbedded::window_get_position_with_decorations(WindowID p_window) const {
583 return Point2i();
584 }
585
586 void DisplayServerAppleEmbedded::window_set_position(const Point2i &p_position, WindowID p_window) {
587 // Probably not supported for single window iOS app
588 }
589
590 void DisplayServerAppleEmbedded::window_set_transient(WindowID p_window, WindowID p_parent) {
591 // Probably not supported for iOS
592 }
593
594 void DisplayServerAppleEmbedded::window_set_max_size(const Size2i p_size, WindowID p_window) {
595 // Probably not supported for iOS
596 }
597
598 Size2i DisplayServerAppleEmbedded::window_get_max_size(WindowID p_window) const {
599 return Size2i();
600 }
601
602 void DisplayServerAppleEmbedded::window_set_min_size(const Size2i p_size, WindowID p_window) {
603 // Probably not supported for iOS
604 }
605
606 Size2i DisplayServerAppleEmbedded::window_get_min_size(WindowID p_window) const {
607 return Size2i();
608 }
609
610 void DisplayServerAppleEmbedded::window_set_size(const Size2i p_size, WindowID p_window) {
611 // Probably not supported for iOS
612 }
613
614 Size2i DisplayServerAppleEmbedded::window_get_size(WindowID p_window) const {
615 CGRect viewBounds = GDTAppDelegateService.viewController.view.bounds;
616 return Size2i(viewBounds.size.width, viewBounds.size.height) * screen_get_max_scale();
617 }
618
619 Size2i DisplayServerAppleEmbedded::window_get_size_with_decorations(WindowID p_window) const {
620 return window_get_size(p_window);
621 }
622
623 void DisplayServerAppleEmbedded::window_set_mode(WindowMode p_mode, WindowID p_window) {
624 // Probably not supported for iOS
625 }
626
627 DisplayServer::WindowMode DisplayServerAppleEmbedded::window_get_mode(WindowID p_window) const {
628 return WindowMode::WINDOW_MODE_FULLSCREEN;
629 }
630
631 bool DisplayServerAppleEmbedded::window_is_maximize_allowed(WindowID p_window) const {
632 return false;
633 }
634
635 void DisplayServerAppleEmbedded::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
636 // Probably not supported for iOS
637 }
638
639 bool DisplayServerAppleEmbedded::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
640 return false;
641 }
642
643 void DisplayServerAppleEmbedded::window_request_attention(WindowID p_window) {
644 // Probably not supported for iOS
645 }
646
647 void DisplayServerAppleEmbedded::window_move_to_foreground(WindowID p_window) {
648 // Probably not supported for iOS
649 }
650
651 bool DisplayServerAppleEmbedded::window_is_focused(WindowID p_window) const {
652 return true;
653 }
654
655 float DisplayServerAppleEmbedded::screen_get_max_scale() const {
656 return screen_get_scale(SCREEN_OF_MAIN_WINDOW);
657 }
658
659 void DisplayServerAppleEmbedded::screen_set_orientation(DisplayServer::ScreenOrientation p_orientation, int p_screen) {
660 p_screen = _get_screen_index(p_screen);
661 int screen_count = get_screen_count();
662 ERR_FAIL_INDEX(p_screen, screen_count);
663
664 screen_orientation = p_orientation;
665 if (@available(iOS 16.0, *)) {
666 [GDTAppDelegateService.viewController setNeedsUpdateOfSupportedInterfaceOrientations];
667 }
668 #if !defined(VISIONOS_ENABLED)
669 else {
670 [UIViewController attemptRotationToDeviceOrientation];
671 }
672 #endif
673 }
674
675 DisplayServer::ScreenOrientation DisplayServerAppleEmbedded::screen_get_orientation(int p_screen) const {
676 p_screen = _get_screen_index(p_screen);
677 int screen_count = get_screen_count();
678 ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_LANDSCAPE);
679
680 return screen_orientation;
681 }
682
683 bool DisplayServerAppleEmbedded::window_can_draw(WindowID p_window) const {
684 return true;
685 }
686
687 bool DisplayServerAppleEmbedded::can_any_window_draw() const {
688 return true;
689 }
690
691 bool DisplayServerAppleEmbedded::is_touchscreen_available() const {
692 return true;
693 }
694
695 _FORCE_INLINE_ int _convert_utf32_offset_to_utf16(const String &p_existing_text, int p_pos) {
696 int limit = p_pos;
697 for (int i = 0; i < MIN(p_existing_text.length(), p_pos); i++) {
698 if (p_existing_text[i] > 0xffff) {
699 limit++;
700 }
701 }
702 return limit;
703 }
704
705 void DisplayServerAppleEmbedded::virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, VirtualKeyboardType p_type, int p_max_length, int p_cursor_start, int p_cursor_end) {
706 NSString *existingString = [[NSString alloc] initWithUTF8String:p_existing_text.utf8().get_data()];
707
708 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
709 GDTAppDelegateService.viewController.keyboardView.textContentType = nil;
710 switch (p_type) {
711 case KEYBOARD_TYPE_DEFAULT: {
712 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
713 } break;
714 case KEYBOARD_TYPE_MULTILINE: {
715 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
716 } break;
717 case KEYBOARD_TYPE_NUMBER: {
718 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeNumberPad;
719 } break;
720 case KEYBOARD_TYPE_NUMBER_DECIMAL: {
721 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeDecimalPad;
722 } break;
723 case KEYBOARD_TYPE_PHONE: {
724 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypePhonePad;
725 GDTAppDelegateService.viewController.keyboardView.textContentType = UITextContentTypeTelephoneNumber;
726 } break;
727 case KEYBOARD_TYPE_EMAIL_ADDRESS: {
728 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeEmailAddress;
729 GDTAppDelegateService.viewController.keyboardView.textContentType = UITextContentTypeEmailAddress;
730 } break;
731 case KEYBOARD_TYPE_PASSWORD: {
732 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
733 GDTAppDelegateService.viewController.keyboardView.textContentType = UITextContentTypePassword;
734 } break;
735 case KEYBOARD_TYPE_URL: {
736 GDTAppDelegateService.viewController.keyboardView.keyboardType = UIKeyboardTypeWebSearch;
737 GDTAppDelegateService.viewController.keyboardView.textContentType = UITextContentTypeURL;
738 } break;
739 }
740
741 [GDTAppDelegateService.viewController.keyboardView
742 becomeFirstResponderWithString:existingString
743 cursorStart:_convert_utf32_offset_to_utf16(p_existing_text, p_cursor_start)
744 cursorEnd:_convert_utf32_offset_to_utf16(p_existing_text, p_cursor_end)];
745 }
746
747 bool DisplayServerAppleEmbedded::is_keyboard_active() const {
748 return [GDTAppDelegateService.viewController.keyboardView isFirstResponder];
749 }
750
751 void DisplayServerAppleEmbedded::virtual_keyboard_hide() {
752 [GDTAppDelegateService.viewController.keyboardView resignFirstResponder];
753 }
754
755 void DisplayServerAppleEmbedded::virtual_keyboard_set_height(int height) {
756 virtual_keyboard_height = height * screen_get_max_scale();
757 }
758
759 int DisplayServerAppleEmbedded::virtual_keyboard_get_height() const {
760 return virtual_keyboard_height;
761 }
762
763 bool DisplayServerAppleEmbedded::has_hardware_keyboard() const {
764 if (@available(iOS 14.0, *)) {
765 return [GCKeyboard coalescedKeyboard];
766 } else {
767 return false;
768 }
769 }
770
771 void DisplayServerAppleEmbedded::clipboard_set(const String &p_text) {
772 [UIPasteboard generalPasteboard].string = [NSString stringWithUTF8String:p_text.utf8().get_data()];
773 }
774
775 String DisplayServerAppleEmbedded::clipboard_get() const {
776 NSString *text = [UIPasteboard generalPasteboard].string;
777
778 return String::utf8([text UTF8String]);
779 }
780
781 void DisplayServerAppleEmbedded::screen_set_keep_on(bool p_enable) {
782 [UIApplication sharedApplication].idleTimerDisabled = p_enable;
783 }
784
785 bool DisplayServerAppleEmbedded::screen_is_kept_on() const {
786 return [UIApplication sharedApplication].idleTimerDisabled;
787 }
788
789 void DisplayServerAppleEmbedded::resize_window(CGSize viewSize) {
790 Size2i size = Size2i(viewSize.width, viewSize.height) * screen_get_max_scale();
791
792 #if defined(RD_ENABLED)
793 if (rendering_context) {
794 rendering_context->window_set_size(MAIN_WINDOW_ID, size.x, size.y);
795 }
796 #endif
797
798 Variant resize_rect = Rect2i(Point2i(), size);
799 _window_callback(window_resize_callback, resize_rect);
800 }
801
802 void DisplayServerAppleEmbedded::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
803 _THREAD_SAFE_METHOD_
804 #if defined(RD_ENABLED)
805 if (rendering_context) {
806 rendering_context->window_set_vsync_mode(p_window, p_vsync_mode);
807 }
808 #endif
809 }
810
811 DisplayServer::VSyncMode DisplayServerAppleEmbedded::window_get_vsync_mode(WindowID p_window) const {
812 _THREAD_SAFE_METHOD_
813 #if defined(RD_ENABLED)
814 if (rendering_context) {
815 return rendering_context->window_get_vsync_mode(p_window);
816 }
817 #endif
818 return DisplayServer::VSYNC_ENABLED;
819 }
820
821 void DisplayServerAppleEmbedded::set_native_icon(const String &p_filename) {
822 // Not supported on Apple embedded platforms.
823 }
824
825 void DisplayServerAppleEmbedded::set_icon(const Ref<Image> &p_icon) {
826 // Not supported on Apple embedded platforms.
827 }