display_server_apple_embedded.mm 26 KB

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