display_server_ios.mm 26 KB

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