display_server_ios.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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();
  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. const Variant *argp = &p_arg;
  159. Variant ret;
  160. Callable::CallError ce;
  161. p_callable.callp((const Variant **)&argp, 1, ret, ce);
  162. }
  163. }
  164. // MARK: - Input
  165. // MARK: Touches
  166. void DisplayServerIOS::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click) {
  167. Ref<InputEventScreenTouch> ev;
  168. ev.instantiate();
  169. ev->set_index(p_idx);
  170. ev->set_pressed(p_pressed);
  171. ev->set_position(Vector2(p_x, p_y));
  172. ev->set_double_tap(p_double_click);
  173. perform_event(ev);
  174. }
  175. 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) {
  176. Ref<InputEventScreenDrag> ev;
  177. ev.instantiate();
  178. ev->set_index(p_idx);
  179. ev->set_pressure(p_pressure);
  180. ev->set_tilt(p_tilt);
  181. ev->set_position(Vector2(p_x, p_y));
  182. ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y));
  183. perform_event(ev);
  184. }
  185. void DisplayServerIOS::perform_event(const Ref<InputEvent> &p_event) {
  186. Input::get_singleton()->parse_input_event(p_event);
  187. }
  188. void DisplayServerIOS::touches_canceled(int p_idx) {
  189. touch_press(p_idx, -1, -1, false, false);
  190. }
  191. // MARK: Keyboard
  192. void DisplayServerIOS::key(Key p_key, char32_t p_char, Key p_unshifted, Key p_physical, NSInteger p_modifier, bool p_pressed) {
  193. Ref<InputEventKey> ev;
  194. ev.instantiate();
  195. ev->set_echo(false);
  196. ev->set_pressed(p_pressed);
  197. ev->set_keycode(fix_keycode(p_char, p_key));
  198. if (@available(iOS 13.4, *)) {
  199. if (p_key != Key::SHIFT) {
  200. ev->set_shift_pressed(p_modifier & UIKeyModifierShift);
  201. }
  202. if (p_key != Key::CTRL) {
  203. ev->set_ctrl_pressed(p_modifier & UIKeyModifierControl);
  204. }
  205. if (p_key != Key::ALT) {
  206. ev->set_alt_pressed(p_modifier & UIKeyModifierAlternate);
  207. }
  208. if (p_key != Key::META) {
  209. ev->set_meta_pressed(p_modifier & UIKeyModifierCommand);
  210. }
  211. }
  212. ev->set_key_label(p_unshifted);
  213. ev->set_physical_keycode(p_physical);
  214. ev->set_unicode(fix_unicode(p_char));
  215. perform_event(ev);
  216. }
  217. // MARK: Motion
  218. void DisplayServerIOS::update_gravity(float p_x, float p_y, float p_z) {
  219. Input::get_singleton()->set_gravity(Vector3(p_x, p_y, p_z));
  220. }
  221. void DisplayServerIOS::update_accelerometer(float p_x, float p_y, float p_z) {
  222. // Found out the Z should not be negated! Pass as is!
  223. Vector3 v_accelerometer = Vector3(
  224. p_x / kDisplayServerIOSAcceleration,
  225. p_y / kDisplayServerIOSAcceleration,
  226. p_z / kDisplayServerIOSAcceleration);
  227. Input::get_singleton()->set_accelerometer(v_accelerometer);
  228. }
  229. void DisplayServerIOS::update_magnetometer(float p_x, float p_y, float p_z) {
  230. Input::get_singleton()->set_magnetometer(Vector3(p_x, p_y, p_z));
  231. }
  232. void DisplayServerIOS::update_gyroscope(float p_x, float p_y, float p_z) {
  233. Input::get_singleton()->set_gyroscope(Vector3(p_x, p_y, p_z));
  234. }
  235. // MARK: -
  236. bool DisplayServerIOS::has_feature(Feature p_feature) const {
  237. switch (p_feature) {
  238. // case FEATURE_CURSOR_SHAPE:
  239. // case FEATURE_CUSTOM_CURSOR_SHAPE:
  240. // case FEATURE_GLOBAL_MENU:
  241. // case FEATURE_HIDPI:
  242. // case FEATURE_ICON:
  243. // case FEATURE_IME:
  244. // case FEATURE_MOUSE:
  245. // case FEATURE_MOUSE_WARP:
  246. // case FEATURE_NATIVE_DIALOG:
  247. // case FEATURE_NATIVE_ICON:
  248. // case FEATURE_WINDOW_TRANSPARENCY:
  249. case FEATURE_CLIPBOARD:
  250. case FEATURE_KEEP_SCREEN_ON:
  251. case FEATURE_ORIENTATION:
  252. case FEATURE_TOUCHSCREEN:
  253. case FEATURE_VIRTUAL_KEYBOARD:
  254. case FEATURE_TEXT_TO_SPEECH:
  255. return true;
  256. default:
  257. return false;
  258. }
  259. }
  260. String DisplayServerIOS::get_name() const {
  261. return "iOS";
  262. }
  263. bool DisplayServerIOS::tts_is_speaking() const {
  264. ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  265. return [tts isSpeaking];
  266. }
  267. bool DisplayServerIOS::tts_is_paused() const {
  268. ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  269. return [tts isPaused];
  270. }
  271. TypedArray<Dictionary> DisplayServerIOS::tts_get_voices() const {
  272. ERR_FAIL_COND_V_MSG(!tts, TypedArray<Dictionary>(), "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  273. return [tts getVoices];
  274. }
  275. 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) {
  276. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  277. [tts speak:p_text voice:p_voice volume:p_volume pitch:p_pitch rate:p_rate utterance_id:p_utterance_id interrupt:p_interrupt];
  278. }
  279. void DisplayServerIOS::tts_pause() {
  280. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  281. [tts pauseSpeaking];
  282. }
  283. void DisplayServerIOS::tts_resume() {
  284. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  285. [tts resumeSpeaking];
  286. }
  287. void DisplayServerIOS::tts_stop() {
  288. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  289. [tts stopSpeaking];
  290. }
  291. Rect2i DisplayServerIOS::get_display_safe_area() const {
  292. UIEdgeInsets insets = UIEdgeInsetsZero;
  293. UIView *view = AppDelegate.viewController.godotView;
  294. if ([view respondsToSelector:@selector(safeAreaInsets)]) {
  295. insets = [view safeAreaInsets];
  296. }
  297. float scale = screen_get_scale();
  298. Size2i insets_position = Size2i(insets.left, insets.top) * scale;
  299. Size2i insets_size = Size2i(insets.left + insets.right, insets.top + insets.bottom) * scale;
  300. return Rect2i(screen_get_position() + insets_position, screen_get_size() - insets_size);
  301. }
  302. int DisplayServerIOS::get_screen_count() const {
  303. return 1;
  304. }
  305. int DisplayServerIOS::get_primary_screen() const {
  306. return 0;
  307. }
  308. Point2i DisplayServerIOS::screen_get_position(int p_screen) const {
  309. return Size2i();
  310. }
  311. Size2i DisplayServerIOS::screen_get_size(int p_screen) const {
  312. CALayer *layer = AppDelegate.viewController.godotView.renderingLayer;
  313. if (!layer) {
  314. return Size2i();
  315. }
  316. return Size2i(layer.bounds.size.width, layer.bounds.size.height) * screen_get_scale(p_screen);
  317. }
  318. Rect2i DisplayServerIOS::screen_get_usable_rect(int p_screen) const {
  319. return Rect2i(screen_get_position(p_screen), screen_get_size(p_screen));
  320. }
  321. int DisplayServerIOS::screen_get_dpi(int p_screen) const {
  322. struct utsname systemInfo;
  323. uname(&systemInfo);
  324. NSString *string = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
  325. NSDictionary *iOSModelToDPI = [GodotDeviceMetrics dpiList];
  326. for (NSArray *keyArray in iOSModelToDPI) {
  327. if ([keyArray containsObject:string]) {
  328. NSNumber *value = iOSModelToDPI[keyArray];
  329. return [value intValue];
  330. }
  331. }
  332. // If device wasn't found in dictionary
  333. // make a best guess from device metrics.
  334. CGFloat scale = [UIScreen mainScreen].scale;
  335. UIUserInterfaceIdiom idiom = [UIDevice currentDevice].userInterfaceIdiom;
  336. switch (idiom) {
  337. case UIUserInterfaceIdiomPad:
  338. return scale == 2 ? 264 : 132;
  339. case UIUserInterfaceIdiomPhone: {
  340. if (scale == 3) {
  341. CGFloat nativeScale = [UIScreen mainScreen].nativeScale;
  342. return nativeScale == 3 ? 458 : 401;
  343. }
  344. return 326;
  345. }
  346. default:
  347. return 72;
  348. }
  349. }
  350. float DisplayServerIOS::screen_get_refresh_rate(int p_screen) const {
  351. return [UIScreen mainScreen].maximumFramesPerSecond;
  352. }
  353. float DisplayServerIOS::screen_get_scale(int p_screen) const {
  354. return [UIScreen mainScreen].scale;
  355. }
  356. Vector<DisplayServer::WindowID> DisplayServerIOS::get_window_list() const {
  357. Vector<DisplayServer::WindowID> list;
  358. list.push_back(MAIN_WINDOW_ID);
  359. return list;
  360. }
  361. DisplayServer::WindowID DisplayServerIOS::get_window_at_screen_position(const Point2i &p_position) const {
  362. return MAIN_WINDOW_ID;
  363. }
  364. int64_t DisplayServerIOS::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  365. ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, 0);
  366. switch (p_handle_type) {
  367. case DISPLAY_HANDLE: {
  368. return 0; // Not supported.
  369. }
  370. case WINDOW_HANDLE: {
  371. return (int64_t)AppDelegate.viewController;
  372. }
  373. case WINDOW_VIEW: {
  374. return (int64_t)AppDelegate.viewController.godotView;
  375. }
  376. default: {
  377. return 0;
  378. }
  379. }
  380. }
  381. void DisplayServerIOS::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  382. window_attached_instance_id = p_instance;
  383. }
  384. ObjectID DisplayServerIOS::window_get_attached_instance_id(WindowID p_window) const {
  385. return window_attached_instance_id;
  386. }
  387. void DisplayServerIOS::window_set_title(const String &p_title, WindowID p_window) {
  388. // Probably not supported for iOS
  389. }
  390. int DisplayServerIOS::window_get_current_screen(WindowID p_window) const {
  391. return SCREEN_OF_MAIN_WINDOW;
  392. }
  393. void DisplayServerIOS::window_set_current_screen(int p_screen, WindowID p_window) {
  394. // Probably not supported for iOS
  395. }
  396. Point2i DisplayServerIOS::window_get_position(WindowID p_window) const {
  397. return Point2i();
  398. }
  399. Point2i DisplayServerIOS::window_get_position_with_decorations(WindowID p_window) const {
  400. return Point2i();
  401. }
  402. void DisplayServerIOS::window_set_position(const Point2i &p_position, WindowID p_window) {
  403. // Probably not supported for single window iOS app
  404. }
  405. void DisplayServerIOS::window_set_transient(WindowID p_window, WindowID p_parent) {
  406. // Probably not supported for iOS
  407. }
  408. void DisplayServerIOS::window_set_max_size(const Size2i p_size, WindowID p_window) {
  409. // Probably not supported for iOS
  410. }
  411. Size2i DisplayServerIOS::window_get_max_size(WindowID p_window) const {
  412. return Size2i();
  413. }
  414. void DisplayServerIOS::window_set_min_size(const Size2i p_size, WindowID p_window) {
  415. // Probably not supported for iOS
  416. }
  417. Size2i DisplayServerIOS::window_get_min_size(WindowID p_window) const {
  418. return Size2i();
  419. }
  420. void DisplayServerIOS::window_set_size(const Size2i p_size, WindowID p_window) {
  421. // Probably not supported for iOS
  422. }
  423. Size2i DisplayServerIOS::window_get_size(WindowID p_window) const {
  424. CGRect screenBounds = [UIScreen mainScreen].bounds;
  425. return Size2i(screenBounds.size.width, screenBounds.size.height) * screen_get_max_scale();
  426. }
  427. Size2i DisplayServerIOS::window_get_size_with_decorations(WindowID p_window) const {
  428. return window_get_size(p_window);
  429. }
  430. void DisplayServerIOS::window_set_mode(WindowMode p_mode, WindowID p_window) {
  431. // Probably not supported for iOS
  432. }
  433. DisplayServer::WindowMode DisplayServerIOS::window_get_mode(WindowID p_window) const {
  434. return WindowMode::WINDOW_MODE_FULLSCREEN;
  435. }
  436. bool DisplayServerIOS::window_is_maximize_allowed(WindowID p_window) const {
  437. return false;
  438. }
  439. void DisplayServerIOS::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  440. // Probably not supported for iOS
  441. }
  442. bool DisplayServerIOS::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  443. return false;
  444. }
  445. void DisplayServerIOS::window_request_attention(WindowID p_window) {
  446. // Probably not supported for iOS
  447. }
  448. void DisplayServerIOS::window_move_to_foreground(WindowID p_window) {
  449. // Probably not supported for iOS
  450. }
  451. bool DisplayServerIOS::window_is_focused(WindowID p_window) const {
  452. return true;
  453. }
  454. float DisplayServerIOS::screen_get_max_scale() const {
  455. return screen_get_scale(SCREEN_OF_MAIN_WINDOW);
  456. }
  457. void DisplayServerIOS::screen_set_orientation(DisplayServer::ScreenOrientation p_orientation, int p_screen) {
  458. screen_orientation = p_orientation;
  459. if (@available(iOS 16.0, *)) {
  460. [AppDelegate.viewController setNeedsUpdateOfSupportedInterfaceOrientations];
  461. } else {
  462. [UIViewController attemptRotationToDeviceOrientation];
  463. }
  464. }
  465. DisplayServer::ScreenOrientation DisplayServerIOS::screen_get_orientation(int p_screen) const {
  466. return screen_orientation;
  467. }
  468. bool DisplayServerIOS::window_can_draw(WindowID p_window) const {
  469. return true;
  470. }
  471. bool DisplayServerIOS::can_any_window_draw() const {
  472. return true;
  473. }
  474. bool DisplayServerIOS::is_touchscreen_available() const {
  475. return true;
  476. }
  477. _FORCE_INLINE_ int _convert_utf32_offset_to_utf16(const String &p_existing_text, int p_pos) {
  478. int limit = p_pos;
  479. for (int i = 0; i < MIN(p_existing_text.length(), p_pos); i++) {
  480. if (p_existing_text[i] > 0xffff) {
  481. limit++;
  482. }
  483. }
  484. return limit;
  485. }
  486. 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) {
  487. NSString *existingString = [[NSString alloc] initWithUTF8String:p_existing_text.utf8().get_data()];
  488. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  489. AppDelegate.viewController.keyboardView.textContentType = nil;
  490. switch (p_type) {
  491. case KEYBOARD_TYPE_DEFAULT: {
  492. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  493. } break;
  494. case KEYBOARD_TYPE_MULTILINE: {
  495. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  496. } break;
  497. case KEYBOARD_TYPE_NUMBER: {
  498. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeNumberPad;
  499. } break;
  500. case KEYBOARD_TYPE_NUMBER_DECIMAL: {
  501. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDecimalPad;
  502. } break;
  503. case KEYBOARD_TYPE_PHONE: {
  504. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypePhonePad;
  505. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypeTelephoneNumber;
  506. } break;
  507. case KEYBOARD_TYPE_EMAIL_ADDRESS: {
  508. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeEmailAddress;
  509. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypeEmailAddress;
  510. } break;
  511. case KEYBOARD_TYPE_PASSWORD: {
  512. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeDefault;
  513. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypePassword;
  514. } break;
  515. case KEYBOARD_TYPE_URL: {
  516. AppDelegate.viewController.keyboardView.keyboardType = UIKeyboardTypeWebSearch;
  517. AppDelegate.viewController.keyboardView.textContentType = UITextContentTypeURL;
  518. } break;
  519. }
  520. [AppDelegate.viewController.keyboardView
  521. becomeFirstResponderWithString:existingString
  522. cursorStart:_convert_utf32_offset_to_utf16(p_existing_text, p_cursor_start)
  523. cursorEnd:_convert_utf32_offset_to_utf16(p_existing_text, p_cursor_end)];
  524. }
  525. bool DisplayServerIOS::is_keyboard_active() const {
  526. return [AppDelegate.viewController.keyboardView isFirstResponder];
  527. }
  528. void DisplayServerIOS::virtual_keyboard_hide() {
  529. [AppDelegate.viewController.keyboardView resignFirstResponder];
  530. }
  531. void DisplayServerIOS::virtual_keyboard_set_height(int height) {
  532. virtual_keyboard_height = height * screen_get_max_scale();
  533. }
  534. int DisplayServerIOS::virtual_keyboard_get_height() const {
  535. return virtual_keyboard_height;
  536. }
  537. void DisplayServerIOS::clipboard_set(const String &p_text) {
  538. [UIPasteboard generalPasteboard].string = [NSString stringWithUTF8String:p_text.utf8()];
  539. }
  540. String DisplayServerIOS::clipboard_get() const {
  541. NSString *text = [UIPasteboard generalPasteboard].string;
  542. return String::utf8([text UTF8String]);
  543. }
  544. void DisplayServerIOS::screen_set_keep_on(bool p_enable) {
  545. [UIApplication sharedApplication].idleTimerDisabled = p_enable;
  546. }
  547. bool DisplayServerIOS::screen_is_kept_on() const {
  548. return [UIApplication sharedApplication].idleTimerDisabled;
  549. }
  550. void DisplayServerIOS::resize_window(CGSize viewSize) {
  551. Size2i size = Size2i(viewSize.width, viewSize.height) * screen_get_max_scale();
  552. #if defined(VULKAN_ENABLED)
  553. if (context_vulkan) {
  554. context_vulkan->window_resize(MAIN_WINDOW_ID, size.x, size.y);
  555. }
  556. #endif
  557. Variant resize_rect = Rect2i(Point2i(), size);
  558. _window_callback(window_resize_callback, resize_rect);
  559. }
  560. void DisplayServerIOS::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
  561. _THREAD_SAFE_METHOD_
  562. #if defined(VULKAN_ENABLED)
  563. if (context_vulkan) {
  564. context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
  565. }
  566. #endif
  567. }
  568. DisplayServer::VSyncMode DisplayServerIOS::window_get_vsync_mode(WindowID p_window) const {
  569. _THREAD_SAFE_METHOD_
  570. #if defined(VULKAN_ENABLED)
  571. if (context_vulkan) {
  572. return context_vulkan->get_vsync_mode(p_window);
  573. }
  574. #endif
  575. return DisplayServer::VSYNC_ENABLED;
  576. }