display_server_ios.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /**************************************************************************/
  2. /* display_server_ios.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_ios.h"
  31. #import "app_delegate.h"
  32. #import "device_metrics.h"
  33. #import "godot_view.h"
  34. #import "ios.h"
  35. #import "key_mapping_ios.h"
  36. #import "keyboard_input_view.h"
  37. #import "os_ios.h"
  38. #import "tts_ios.h"
  39. #import "view_controller.h"
  40. #include "core/config/project_settings.h"
  41. #include "core/io/file_access_pack.h"
  42. #import <sys/utsname.h>
  43. static const float kDisplayServerIOSAcceleration = 1.f;
  44. DisplayServerIOS *DisplayServerIOS::get_singleton() {
  45. return (DisplayServerIOS *)DisplayServer::get_singleton();
  46. }
  47. DisplayServerIOS::DisplayServerIOS(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, Error &r_error) {
  48. KeyMappingIOS::initialize();
  49. rendering_driver = p_rendering_driver;
  50. // Init TTS
  51. bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
  52. if (tts_enabled) {
  53. tts = [[TTS_IOS alloc] init];
  54. }
  55. #if defined(VULKAN_ENABLED)
  56. context_vulkan = nullptr;
  57. rendering_device_vulkan = nullptr;
  58. if (rendering_driver == "vulkan") {
  59. context_vulkan = memnew(VulkanContextIOS);
  60. if (context_vulkan->initialize() != OK) {
  61. memdelete(context_vulkan);
  62. context_vulkan = nullptr;
  63. ERR_FAIL_MSG("Failed to initialize Vulkan context");
  64. }
  65. CALayer *layer = [AppDelegate.viewController.godotView initializeRenderingForDriver:@"vulkan"];
  66. if (!layer) {
  67. ERR_FAIL_MSG("Failed to create iOS Vulkan rendering layer.");
  68. }
  69. Size2i size = Size2i(layer.bounds.size.width, layer.bounds.size.height) * screen_get_max_scale();
  70. if (context_vulkan->window_create(MAIN_WINDOW_ID, p_vsync_mode, layer, size.width, size.height) != OK) {
  71. memdelete(context_vulkan);
  72. context_vulkan = nullptr;
  73. r_error = ERR_UNAVAILABLE;
  74. ERR_FAIL_MSG("Failed to create Vulkan window.");
  75. }
  76. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  77. rendering_device_vulkan->initialize(context_vulkan);
  78. RendererCompositorRD::make_current();
  79. }
  80. #endif
  81. #if defined(GLES3_ENABLED)
  82. if (rendering_driver == "opengl3") {
  83. CALayer *layer = [AppDelegate.viewController.godotView initializeRenderingForDriver:@"opengl3"];
  84. if (!layer) {
  85. ERR_FAIL_MSG("Failed to create iOS OpenGLES rendering layer.");
  86. }
  87. RasterizerGLES3::make_current(false);
  88. }
  89. #endif
  90. bool keep_screen_on = bool(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
  91. screen_set_keep_on(keep_screen_on);
  92. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  93. r_error = OK;
  94. }
  95. DisplayServerIOS::~DisplayServerIOS() {
  96. #if defined(VULKAN_ENABLED)
  97. if (rendering_device_vulkan) {
  98. rendering_device_vulkan->finalize();
  99. memdelete(rendering_device_vulkan);
  100. rendering_device_vulkan = nullptr;
  101. }
  102. if (context_vulkan) {
  103. context_vulkan->window_destroy(MAIN_WINDOW_ID);
  104. memdelete(context_vulkan);
  105. context_vulkan = nullptr;
  106. }
  107. #endif
  108. }
  109. DisplayServer *DisplayServerIOS::create_func(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, Error &r_error) {
  110. return memnew(DisplayServerIOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, r_error));
  111. }
  112. Vector<String> DisplayServerIOS::get_rendering_drivers_func() {
  113. Vector<String> drivers;
  114. #if defined(VULKAN_ENABLED)
  115. drivers.push_back("vulkan");
  116. #endif
  117. #if defined(GLES3_ENABLED)
  118. drivers.push_back("opengl3");
  119. #endif
  120. return drivers;
  121. }
  122. void DisplayServerIOS::register_ios_driver() {
  123. register_create_function("iOS", create_func, get_rendering_drivers_func);
  124. }
  125. // MARK: Events
  126. void DisplayServerIOS::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  127. window_resize_callback = p_callable;
  128. }
  129. void DisplayServerIOS::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  130. window_event_callback = p_callable;
  131. }
  132. void DisplayServerIOS::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  133. input_event_callback = p_callable;
  134. }
  135. void DisplayServerIOS::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  136. input_text_callback = p_callable;
  137. }
  138. void DisplayServerIOS::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  139. // Probably not supported for iOS
  140. }
  141. void DisplayServerIOS::process_events() {
  142. Input::get_singleton()->flush_buffered_events();
  143. }
  144. void DisplayServerIOS::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  145. DisplayServerIOS::get_singleton()->send_input_event(p_event);
  146. }
  147. void DisplayServerIOS::send_input_event(const Ref<InputEvent> &p_event) const {
  148. _window_callback(input_event_callback, p_event);
  149. }
  150. void DisplayServerIOS::send_input_text(const String &p_text) const {
  151. _window_callback(input_text_callback, p_text);
  152. }
  153. void DisplayServerIOS::send_window_event(DisplayServer::WindowEvent p_event) const {
  154. _window_callback(window_event_callback, int(p_event));
  155. }
  156. void DisplayServerIOS::_window_callback(const Callable &p_callable, const Variant &p_arg) const {
  157. if (!p_callable.is_null()) {
  158. p_callable.call(p_arg);
  159. }
  160. }
  161. // MARK: - Input
  162. // MARK: Touches
  163. void DisplayServerIOS::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click) {
  164. Ref<InputEventScreenTouch> ev;
  165. ev.instantiate();
  166. ev->set_index(p_idx);
  167. ev->set_pressed(p_pressed);
  168. ev->set_position(Vector2(p_x, p_y));
  169. ev->set_double_tap(p_double_click);
  170. perform_event(ev);
  171. }
  172. void DisplayServerIOS::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) {
  173. Ref<InputEventScreenDrag> ev;
  174. ev.instantiate();
  175. ev->set_index(p_idx);
  176. ev->set_pressure(p_pressure);
  177. ev->set_tilt(p_tilt);
  178. ev->set_position(Vector2(p_x, p_y));
  179. ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
  180. perform_event(ev);
  181. }
  182. void DisplayServerIOS::perform_event(const Ref<InputEvent> &p_event) {
  183. Input::get_singleton()->parse_input_event(p_event);
  184. }
  185. void DisplayServerIOS::touches_canceled(int p_idx) {
  186. touch_press(p_idx, -1, -1, false, false);
  187. }
  188. // MARK: Keyboard
  189. void DisplayServerIOS::key(Key p_key, char32_t p_char, Key p_unshifted, Key p_physical, NSInteger p_modifier, bool p_pressed) {
  190. Ref<InputEventKey> ev;
  191. ev.instantiate();
  192. ev->set_echo(false);
  193. ev->set_pressed(p_pressed);
  194. ev->set_keycode(fix_keycode(p_char, p_key));
  195. if (@available(iOS 13.4, *)) {
  196. if (p_key != Key::SHIFT) {
  197. ev->set_shift_pressed(p_modifier & UIKeyModifierShift);
  198. }
  199. if (p_key != Key::CTRL) {
  200. ev->set_ctrl_pressed(p_modifier & UIKeyModifierControl);
  201. }
  202. if (p_key != Key::ALT) {
  203. ev->set_alt_pressed(p_modifier & UIKeyModifierAlternate);
  204. }
  205. if (p_key != Key::META) {
  206. ev->set_meta_pressed(p_modifier & UIKeyModifierCommand);
  207. }
  208. }
  209. ev->set_key_label(p_unshifted);
  210. ev->set_physical_keycode(p_physical);
  211. ev->set_unicode(fix_unicode(p_char));
  212. perform_event(ev);
  213. }
  214. // MARK: Motion
  215. void DisplayServerIOS::update_gravity(float p_x, float p_y, float p_z) {
  216. Input::get_singleton()->set_gravity(Vector3(p_x, p_y, p_z));
  217. }
  218. void DisplayServerIOS::update_accelerometer(float p_x, float p_y, float p_z) {
  219. // Found out the Z should not be negated! Pass as is!
  220. Vector3 v_accelerometer = Vector3(
  221. p_x / kDisplayServerIOSAcceleration,
  222. p_y / kDisplayServerIOSAcceleration,
  223. p_z / kDisplayServerIOSAcceleration);
  224. Input::get_singleton()->set_accelerometer(v_accelerometer);
  225. }
  226. void DisplayServerIOS::update_magnetometer(float p_x, float p_y, float p_z) {
  227. Input::get_singleton()->set_magnetometer(Vector3(p_x, p_y, p_z));
  228. }
  229. void DisplayServerIOS::update_gyroscope(float p_x, float p_y, float p_z) {
  230. Input::get_singleton()->set_gyroscope(Vector3(p_x, p_y, p_z));
  231. }
  232. // MARK: -
  233. bool DisplayServerIOS::has_feature(Feature p_feature) const {
  234. switch (p_feature) {
  235. // case FEATURE_CURSOR_SHAPE:
  236. // case FEATURE_CUSTOM_CURSOR_SHAPE:
  237. // case FEATURE_GLOBAL_MENU:
  238. // case FEATURE_HIDPI:
  239. // case FEATURE_ICON:
  240. // case FEATURE_IME:
  241. // case FEATURE_MOUSE:
  242. // case FEATURE_MOUSE_WARP:
  243. // case FEATURE_NATIVE_DIALOG:
  244. // case FEATURE_NATIVE_ICON:
  245. // case FEATURE_WINDOW_TRANSPARENCY:
  246. case FEATURE_CLIPBOARD:
  247. case FEATURE_KEEP_SCREEN_ON:
  248. case FEATURE_ORIENTATION:
  249. case FEATURE_TOUCHSCREEN:
  250. case FEATURE_VIRTUAL_KEYBOARD:
  251. case FEATURE_TEXT_TO_SPEECH:
  252. return true;
  253. default:
  254. return false;
  255. }
  256. }
  257. String DisplayServerIOS::get_name() const {
  258. return "iOS";
  259. }
  260. bool DisplayServerIOS::tts_is_speaking() const {
  261. ERR_FAIL_NULL_V_MSG(tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  262. return [tts isSpeaking];
  263. }
  264. bool DisplayServerIOS::tts_is_paused() const {
  265. ERR_FAIL_NULL_V_MSG(tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  266. return [tts isPaused];
  267. }
  268. TypedArray<Dictionary> DisplayServerIOS::tts_get_voices() const {
  269. ERR_FAIL_NULL_V_MSG(tts, TypedArray<Dictionary>(), "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  270. return [tts getVoices];
  271. }
  272. void DisplayServerIOS::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) {
  273. ERR_FAIL_NULL_MSG(tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  274. [tts speak:p_text voice:p_voice volume:p_volume pitch:p_pitch rate:p_rate utterance_id:p_utterance_id interrupt:p_interrupt];
  275. }
  276. void DisplayServerIOS::tts_pause() {
  277. ERR_FAIL_NULL_MSG(tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  278. [tts pauseSpeaking];
  279. }
  280. void DisplayServerIOS::tts_resume() {
  281. ERR_FAIL_NULL_MSG(tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  282. [tts resumeSpeaking];
  283. }
  284. void DisplayServerIOS::tts_stop() {
  285. ERR_FAIL_NULL_MSG(tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  286. [tts stopSpeaking];
  287. }
  288. bool DisplayServerIOS::is_dark_mode_supported() const {
  289. if (@available(iOS 13.0, *)) {
  290. return true;
  291. } else {
  292. return false;
  293. }
  294. }
  295. bool DisplayServerIOS::is_dark_mode() const {
  296. if (@available(iOS 13.0, *)) {
  297. return [UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark;
  298. } else {
  299. return false;
  300. }
  301. }
  302. Rect2i DisplayServerIOS::get_display_safe_area() const {
  303. UIEdgeInsets insets = UIEdgeInsetsZero;
  304. UIView *view = AppDelegate.viewController.godotView;
  305. if ([view respondsToSelector:@selector(safeAreaInsets)]) {
  306. insets = [view safeAreaInsets];
  307. }
  308. float scale = screen_get_scale();
  309. Size2i insets_position = Size2i(insets.left, insets.top) * scale;
  310. Size2i insets_size = Size2i(insets.left + insets.right, insets.top + insets.bottom) * scale;
  311. return Rect2i(screen_get_position() + insets_position, screen_get_size() - insets_size);
  312. }
  313. int DisplayServerIOS::get_screen_count() const {
  314. return 1;
  315. }
  316. int DisplayServerIOS::get_primary_screen() const {
  317. return 0;
  318. }
  319. Point2i DisplayServerIOS::screen_get_position(int p_screen) const {
  320. return Size2i();
  321. }
  322. Size2i DisplayServerIOS::screen_get_size(int p_screen) const {
  323. CALayer *layer = AppDelegate.viewController.godotView.renderingLayer;
  324. if (!layer) {
  325. return Size2i();
  326. }
  327. return Size2i(layer.bounds.size.width, layer.bounds.size.height) * screen_get_scale(p_screen);
  328. }
  329. Rect2i DisplayServerIOS::screen_get_usable_rect(int p_screen) const {
  330. return Rect2i(screen_get_position(p_screen), screen_get_size(p_screen));
  331. }
  332. int DisplayServerIOS::screen_get_dpi(int p_screen) const {
  333. struct utsname systemInfo;
  334. uname(&systemInfo);
  335. NSString *string = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
  336. NSDictionary *iOSModelToDPI = [GodotDeviceMetrics dpiList];
  337. for (NSArray *keyArray in iOSModelToDPI) {
  338. if ([keyArray containsObject:string]) {
  339. NSNumber *value = iOSModelToDPI[keyArray];
  340. return [value intValue];
  341. }
  342. }
  343. // If device wasn't found in dictionary
  344. // make a best guess from device metrics.
  345. CGFloat scale = [UIScreen mainScreen].scale;
  346. UIUserInterfaceIdiom idiom = [UIDevice currentDevice].userInterfaceIdiom;
  347. switch (idiom) {
  348. case UIUserInterfaceIdiomPad:
  349. return scale == 2 ? 264 : 132;
  350. case UIUserInterfaceIdiomPhone: {
  351. if (scale == 3) {
  352. CGFloat nativeScale = [UIScreen mainScreen].nativeScale;
  353. return nativeScale == 3 ? 458 : 401;
  354. }
  355. return 326;
  356. }
  357. default:
  358. return 72;
  359. }
  360. }
  361. float DisplayServerIOS::screen_get_refresh_rate(int p_screen) const {
  362. return [UIScreen mainScreen].maximumFramesPerSecond;
  363. }
  364. float DisplayServerIOS::screen_get_scale(int p_screen) const {
  365. return [UIScreen mainScreen].scale;
  366. }
  367. Vector<DisplayServer::WindowID> DisplayServerIOS::get_window_list() const {
  368. Vector<DisplayServer::WindowID> list;
  369. list.push_back(MAIN_WINDOW_ID);
  370. return list;
  371. }
  372. DisplayServer::WindowID DisplayServerIOS::get_window_at_screen_position(const Point2i &p_position) const {
  373. return MAIN_WINDOW_ID;
  374. }
  375. int64_t DisplayServerIOS::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  376. ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, 0);
  377. switch (p_handle_type) {
  378. case DISPLAY_HANDLE: {
  379. return 0; // Not supported.
  380. }
  381. case WINDOW_HANDLE: {
  382. return (int64_t)AppDelegate.viewController;
  383. }
  384. case WINDOW_VIEW: {
  385. return (int64_t)AppDelegate.viewController.godotView;
  386. }
  387. default: {
  388. return 0;
  389. }
  390. }
  391. }
  392. void DisplayServerIOS::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  393. window_attached_instance_id = p_instance;
  394. }
  395. ObjectID DisplayServerIOS::window_get_attached_instance_id(WindowID p_window) const {
  396. return window_attached_instance_id;
  397. }
  398. void DisplayServerIOS::window_set_title(const String &p_title, WindowID p_window) {
  399. // Probably not supported for iOS
  400. }
  401. int DisplayServerIOS::window_get_current_screen(WindowID p_window) const {
  402. return SCREEN_OF_MAIN_WINDOW;
  403. }
  404. void DisplayServerIOS::window_set_current_screen(int p_screen, WindowID p_window) {
  405. // Probably not supported for iOS
  406. }
  407. Point2i DisplayServerIOS::window_get_position(WindowID p_window) const {
  408. return Point2i();
  409. }
  410. Point2i DisplayServerIOS::window_get_position_with_decorations(WindowID p_window) const {
  411. return Point2i();
  412. }
  413. void DisplayServerIOS::window_set_position(const Point2i &p_position, WindowID p_window) {
  414. // Probably not supported for single window iOS app
  415. }
  416. void DisplayServerIOS::window_set_transient(WindowID p_window, WindowID p_parent) {
  417. // Probably not supported for iOS
  418. }
  419. void DisplayServerIOS::window_set_max_size(const Size2i p_size, WindowID p_window) {
  420. // Probably not supported for iOS
  421. }
  422. Size2i DisplayServerIOS::window_get_max_size(WindowID p_window) const {
  423. return Size2i();
  424. }
  425. void DisplayServerIOS::window_set_min_size(const Size2i p_size, WindowID p_window) {
  426. // Probably not supported for iOS
  427. }
  428. Size2i DisplayServerIOS::window_get_min_size(WindowID p_window) const {
  429. return Size2i();
  430. }
  431. void DisplayServerIOS::window_set_size(const Size2i p_size, WindowID p_window) {
  432. // Probably not supported for iOS
  433. }
  434. Size2i DisplayServerIOS::window_get_size(WindowID p_window) const {
  435. CGRect screenBounds = [UIScreen mainScreen].bounds;
  436. return Size2i(screenBounds.size.width, screenBounds.size.height) * screen_get_max_scale();
  437. }
  438. Size2i DisplayServerIOS::window_get_size_with_decorations(WindowID p_window) const {
  439. return window_get_size(p_window);
  440. }
  441. void DisplayServerIOS::window_set_mode(WindowMode p_mode, WindowID p_window) {
  442. // Probably not supported for iOS
  443. }
  444. DisplayServer::WindowMode DisplayServerIOS::window_get_mode(WindowID p_window) const {
  445. return WindowMode::WINDOW_MODE_FULLSCREEN;
  446. }
  447. bool DisplayServerIOS::window_is_maximize_allowed(WindowID p_window) const {
  448. return false;
  449. }
  450. void DisplayServerIOS::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  451. // Probably not supported for iOS
  452. }
  453. bool DisplayServerIOS::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  454. return false;
  455. }
  456. void DisplayServerIOS::window_request_attention(WindowID p_window) {
  457. // Probably not supported for iOS
  458. }
  459. void DisplayServerIOS::window_move_to_foreground(WindowID p_window) {
  460. // Probably not supported for iOS
  461. }
  462. bool DisplayServerIOS::window_is_focused(WindowID p_window) const {
  463. return true;
  464. }
  465. float DisplayServerIOS::screen_get_max_scale() const {
  466. return screen_get_scale(SCREEN_OF_MAIN_WINDOW);
  467. }
  468. void DisplayServerIOS::screen_set_orientation(DisplayServer::ScreenOrientation p_orientation, int p_screen) {
  469. screen_orientation = p_orientation;
  470. if (@available(iOS 16.0, *)) {
  471. [AppDelegate.viewController setNeedsUpdateOfSupportedInterfaceOrientations];
  472. } else {
  473. [UIViewController attemptRotationToDeviceOrientation];
  474. }
  475. }
  476. DisplayServer::ScreenOrientation DisplayServerIOS::screen_get_orientation(int p_screen) const {
  477. return screen_orientation;
  478. }
  479. bool DisplayServerIOS::window_can_draw(WindowID p_window) const {
  480. return true;
  481. }
  482. bool DisplayServerIOS::can_any_window_draw() const {
  483. return true;
  484. }
  485. bool DisplayServerIOS::is_touchscreen_available() const {
  486. return true;
  487. }
  488. _FORCE_INLINE_ int _convert_utf32_offset_to_utf16(const String &p_existing_text, int p_pos) {
  489. int limit = p_pos;
  490. for (int i = 0; i < MIN(p_existing_text.length(), p_pos); i++) {
  491. if (p_existing_text[i] > 0xffff) {
  492. limit++;
  493. }
  494. }
  495. return limit;
  496. }
  497. void DisplayServerIOS::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) {
  498. NSString *existingString = [[NSString alloc] initWithUTF8String:p_existing_text.utf8().get_data()];
  499. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  500. AppDelegate.viewController.keyboardView.textContentType = nil;
  501. switch (p_type) {
  502. case KEYBOARD_TYPE_DEFAULT: {
  503. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  504. } break;
  505. case KEYBOARD_TYPE_MULTILINE: {
  506. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  507. } break;
  508. case KEYBOARD_TYPE_NUMBER: {
  509. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeNumberPad;
  510. } break;
  511. case KEYBOARD_TYPE_NUMBER_DECIMAL: {
  512. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDecimalPad;
  513. } break;
  514. case KEYBOARD_TYPE_PHONE: {
  515. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypePhonePad;
  516. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypeTelephoneNumber;
  517. } break;
  518. case KEYBOARD_TYPE_EMAIL_ADDRESS: {
  519. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeEmailAddress;
  520. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypeEmailAddress;
  521. } break;
  522. case KEYBOARD_TYPE_PASSWORD: {
  523. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  524. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypePassword;
  525. } break;
  526. case KEYBOARD_TYPE_URL: {
  527. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeWebSearch;
  528. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypeURL;
  529. } break;
  530. }
  531. [AppDelegate.viewController.keyboardView
  532. becomeFirstResponderWithString:existingString
  533. cursorStart:_convert_utf32_offset_to_utf16(p_existing_text, p_cursor_start)
  534. cursorEnd:_convert_utf32_offset_to_utf16(p_existing_text, p_cursor_end)];
  535. }
  536. bool DisplayServerIOS::is_keyboard_active() const {
  537. return [AppDelegate.viewController.keyboardView isFirstResponder];
  538. }
  539. void DisplayServerIOS::virtual_keyboard_hide() {
  540. [AppDelegate.viewController.keyboardView resignFirstResponder];
  541. }
  542. void DisplayServerIOS::virtual_keyboard_set_height(int height) {
  543. virtual_keyboard_height = height * screen_get_max_scale();
  544. }
  545. int DisplayServerIOS::virtual_keyboard_get_height() const {
  546. return virtual_keyboard_height;
  547. }
  548. void DisplayServerIOS::clipboard_set(const String &p_text) {
  549. [UIPasteboard generalPasteboard].string = [NSString stringWithUTF8String:p_text.utf8()];
  550. }
  551. String DisplayServerIOS::clipboard_get() const {
  552. NSString *text = [UIPasteboard generalPasteboard].string;
  553. return String::utf8([text UTF8String]);
  554. }
  555. void DisplayServerIOS::screen_set_keep_on(bool p_enable) {
  556. [UIApplication sharedApplication].idleTimerDisabled = p_enable;
  557. }
  558. bool DisplayServerIOS::screen_is_kept_on() const {
  559. return [UIApplication sharedApplication].idleTimerDisabled;
  560. }
  561. void DisplayServerIOS::resize_window(CGSize viewSize) {
  562. Size2i size = Size2i(viewSize.width, viewSize.height) * screen_get_max_scale();
  563. #if defined(VULKAN_ENABLED)
  564. if (context_vulkan) {
  565. context_vulkan->window_resize(MAIN_WINDOW_ID, size.x, size.y);
  566. }
  567. #endif
  568. Variant resize_rect = Rect2i(Point2i(), size);
  569. _window_callback(window_resize_callback, resize_rect);
  570. }
  571. void DisplayServerIOS::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
  572. _THREAD_SAFE_METHOD_
  573. #if defined(VULKAN_ENABLED)
  574. if (context_vulkan) {
  575. context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
  576. }
  577. #endif
  578. }
  579. DisplayServer::VSyncMode DisplayServerIOS::window_get_vsync_mode(WindowID p_window) const {
  580. _THREAD_SAFE_METHOD_
  581. #if defined(VULKAN_ENABLED)
  582. if (context_vulkan) {
  583. return context_vulkan->get_vsync_mode(p_window);
  584. }
  585. #endif
  586. return DisplayServer::VSYNC_ENABLED;
  587. }