os_ios.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /**************************************************************************/
  2. /* os_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 "os_ios.h"
  31. #ifdef IOS_ENABLED
  32. #import "app_delegate.h"
  33. #import "display_server_ios.h"
  34. #import "godot_view.h"
  35. #import "ios_terminal_logger.h"
  36. #import "view_controller.h"
  37. #include "core/config/project_settings.h"
  38. #include "core/io/dir_access.h"
  39. #include "core/io/file_access.h"
  40. #include "core/io/file_access_pack.h"
  41. #include "drivers/unix/syslog_logger.h"
  42. #include "main/main.h"
  43. #import <AudioToolbox/AudioServices.h>
  44. #import <CoreText/CoreText.h>
  45. #import <UIKit/UIKit.h>
  46. #import <dlfcn.h>
  47. #include <sys/sysctl.h>
  48. #if defined(RD_ENABLED)
  49. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  50. #import <QuartzCore/CAMetalLayer.h>
  51. #if defined(VULKAN_ENABLED)
  52. #include "drivers/vulkan/godot_vulkan.h"
  53. #endif // VULKAN_ENABLED
  54. #endif
  55. // Initialization order between compilation units is not guaranteed,
  56. // so we use this as a hack to ensure certain code is called before
  57. // everything else, but after all units are initialized.
  58. typedef void (*init_callback)();
  59. static init_callback *ios_init_callbacks = nullptr;
  60. static int ios_init_callbacks_count = 0;
  61. static int ios_init_callbacks_capacity = 0;
  62. HashMap<String, void *> OS_IOS::dynamic_symbol_lookup_table;
  63. void add_ios_init_callback(init_callback cb) {
  64. if (ios_init_callbacks_count == ios_init_callbacks_capacity) {
  65. void *new_ptr = realloc(ios_init_callbacks, sizeof(cb) * (ios_init_callbacks_capacity + 32));
  66. if (new_ptr) {
  67. ios_init_callbacks = (init_callback *)(new_ptr);
  68. ios_init_callbacks_capacity += 32;
  69. } else {
  70. ERR_FAIL_MSG("Unable to allocate memory for extension callbacks.");
  71. }
  72. }
  73. ios_init_callbacks[ios_init_callbacks_count++] = cb;
  74. }
  75. void register_dynamic_symbol(char *name, void *address) {
  76. OS_IOS::dynamic_symbol_lookup_table[String(name)] = address;
  77. }
  78. OS_IOS *OS_IOS::get_singleton() {
  79. return (OS_IOS *)OS::get_singleton();
  80. }
  81. OS_IOS::OS_IOS() {
  82. for (int i = 0; i < ios_init_callbacks_count; ++i) {
  83. ios_init_callbacks[i]();
  84. }
  85. free(ios_init_callbacks);
  86. ios_init_callbacks = nullptr;
  87. ios_init_callbacks_count = 0;
  88. ios_init_callbacks_capacity = 0;
  89. main_loop = nullptr;
  90. Vector<Logger *> loggers;
  91. loggers.push_back(memnew(IOSTerminalLogger));
  92. _set_logger(memnew(CompositeLogger(loggers)));
  93. AudioDriverManager::add_driver(&audio_driver);
  94. DisplayServerIOS::register_ios_driver();
  95. }
  96. OS_IOS::~OS_IOS() {}
  97. void OS_IOS::alert(const String &p_alert, const String &p_title) {
  98. const CharString utf8_alert = p_alert.utf8();
  99. const CharString utf8_title = p_title.utf8();
  100. iOS::alert(utf8_alert.get_data(), utf8_title.get_data());
  101. }
  102. void OS_IOS::initialize_core() {
  103. OS_Unix::initialize_core();
  104. }
  105. void OS_IOS::initialize() {
  106. initialize_core();
  107. }
  108. void OS_IOS::initialize_modules() {
  109. ios = memnew(iOS);
  110. Engine::get_singleton()->add_singleton(Engine::Singleton("iOS", ios));
  111. joypad_ios = memnew(JoypadIOS);
  112. }
  113. void OS_IOS::deinitialize_modules() {
  114. if (joypad_ios) {
  115. memdelete(joypad_ios);
  116. }
  117. if (ios) {
  118. memdelete(ios);
  119. }
  120. }
  121. void OS_IOS::set_main_loop(MainLoop *p_main_loop) {
  122. main_loop = p_main_loop;
  123. }
  124. MainLoop *OS_IOS::get_main_loop() const {
  125. return main_loop;
  126. }
  127. void OS_IOS::delete_main_loop() {
  128. if (main_loop) {
  129. main_loop->finalize();
  130. memdelete(main_loop);
  131. }
  132. main_loop = nullptr;
  133. }
  134. bool OS_IOS::iterate() {
  135. if (!main_loop) {
  136. return true;
  137. }
  138. if (DisplayServer::get_singleton()) {
  139. DisplayServer::get_singleton()->process_events();
  140. }
  141. return Main::iteration();
  142. }
  143. void OS_IOS::start() {
  144. if (Main::start() == EXIT_SUCCESS) {
  145. main_loop->initialize();
  146. }
  147. if (joypad_ios) {
  148. joypad_ios->start_processing();
  149. }
  150. }
  151. void OS_IOS::finalize() {
  152. deinitialize_modules();
  153. // Already gets called
  154. //delete_main_loop();
  155. }
  156. // MARK: Dynamic Libraries
  157. _FORCE_INLINE_ String OS_IOS::get_framework_executable(const String &p_path) {
  158. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  159. // Read framework bundle to get executable name.
  160. NSURL *url = [NSURL fileURLWithPath:@(p_path.utf8().get_data())];
  161. NSBundle *bundle = [NSBundle bundleWithURL:url];
  162. if (bundle) {
  163. String exe_path = String::utf8([[bundle executablePath] UTF8String]);
  164. if (da->file_exists(exe_path)) {
  165. return exe_path;
  166. }
  167. }
  168. // Try default executable name (invalid framework).
  169. if (da->dir_exists(p_path) && da->file_exists(p_path.path_join(p_path.get_file().get_basename()))) {
  170. return p_path.path_join(p_path.get_file().get_basename());
  171. }
  172. // Not a framework, try loading as .dylib.
  173. return p_path;
  174. }
  175. Error OS_IOS::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
  176. if (p_path.length() == 0) {
  177. // Static xcframework.
  178. p_library_handle = RTLD_SELF;
  179. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  180. *p_data->r_resolved_path = p_path;
  181. }
  182. return OK;
  183. }
  184. String path = get_framework_executable(p_path);
  185. if (!FileAccess::exists(path)) {
  186. // Load .dylib or framework from within the executable path.
  187. path = get_framework_executable(get_executable_path().get_base_dir().path_join(p_path.get_file()));
  188. }
  189. if (!FileAccess::exists(path)) {
  190. // Load .dylib converted to framework from within the executable path.
  191. path = get_framework_executable(get_executable_path().get_base_dir().path_join(p_path.get_file().get_basename() + ".framework"));
  192. }
  193. if (!FileAccess::exists(path)) {
  194. // Load .dylib or framework from a standard iOS location.
  195. path = get_framework_executable(get_executable_path().get_base_dir().path_join("Frameworks").path_join(p_path.get_file()));
  196. }
  197. if (!FileAccess::exists(path)) {
  198. // Load .dylib converted to framework from a standard iOS location.
  199. path = get_framework_executable(get_executable_path().get_base_dir().path_join("Frameworks").path_join(p_path.get_file().get_basename() + ".framework"));
  200. }
  201. ERR_FAIL_COND_V(!FileAccess::exists(path), ERR_FILE_NOT_FOUND);
  202. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  203. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
  204. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  205. *p_data->r_resolved_path = path;
  206. }
  207. return OK;
  208. }
  209. Error OS_IOS::close_dynamic_library(void *p_library_handle) {
  210. if (p_library_handle == RTLD_SELF) {
  211. return OK;
  212. }
  213. return OS_Unix::close_dynamic_library(p_library_handle);
  214. }
  215. Error OS_IOS::get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional) {
  216. if (p_library_handle == RTLD_SELF) {
  217. void **ptr = OS_IOS::dynamic_symbol_lookup_table.getptr(p_name);
  218. if (ptr) {
  219. p_symbol_handle = *ptr;
  220. return OK;
  221. }
  222. }
  223. return OS_Unix::get_dynamic_library_symbol_handle(p_library_handle, p_name, p_symbol_handle, p_optional);
  224. }
  225. String OS_IOS::get_name() const {
  226. return "iOS";
  227. }
  228. String OS_IOS::get_distribution_name() const {
  229. return get_name();
  230. }
  231. String OS_IOS::get_version() const {
  232. NSOperatingSystemVersion ver = [NSProcessInfo processInfo].operatingSystemVersion;
  233. return vformat("%d.%d.%d", (int64_t)ver.majorVersion, (int64_t)ver.minorVersion, (int64_t)ver.patchVersion);
  234. }
  235. String OS_IOS::get_model_name() const {
  236. String model = ios->get_model();
  237. if (model != "") {
  238. return model;
  239. }
  240. return OS_Unix::get_model_name();
  241. }
  242. Error OS_IOS::shell_open(const String &p_uri) {
  243. NSString *urlPath = [[NSString alloc] initWithUTF8String:p_uri.utf8().get_data()];
  244. NSURL *url = [NSURL URLWithString:urlPath];
  245. if (![[UIApplication sharedApplication] canOpenURL:url]) {
  246. return ERR_CANT_OPEN;
  247. }
  248. print_verbose(vformat("Opening URL %s", p_uri));
  249. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  250. return OK;
  251. }
  252. String OS_IOS::get_user_data_dir() const {
  253. static String ret;
  254. if (ret.is_empty()) {
  255. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  256. if (paths && [paths count] >= 1) {
  257. ret.parse_utf8([[paths firstObject] UTF8String]);
  258. }
  259. }
  260. return ret;
  261. }
  262. String OS_IOS::get_cache_path() const {
  263. static String ret;
  264. if (ret.is_empty()) {
  265. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  266. if (paths && [paths count] >= 1) {
  267. ret.parse_utf8([[paths firstObject] UTF8String]);
  268. }
  269. }
  270. return ret;
  271. }
  272. String OS_IOS::get_locale() const {
  273. NSString *preferedLanguage = [NSLocale preferredLanguages].firstObject;
  274. if (preferedLanguage) {
  275. return String::utf8([preferedLanguage UTF8String]).replace("-", "_");
  276. }
  277. NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
  278. return String::utf8([localeIdentifier UTF8String]).replace("-", "_");
  279. }
  280. String OS_IOS::get_unique_id() const {
  281. NSString *uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
  282. return String::utf8([uuid UTF8String]);
  283. }
  284. String OS_IOS::get_processor_name() const {
  285. char buffer[256];
  286. size_t buffer_len = 256;
  287. if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, nullptr, 0) == 0) {
  288. return String::utf8(buffer, buffer_len);
  289. }
  290. ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
  291. }
  292. Vector<String> OS_IOS::get_system_fonts() const {
  293. HashSet<String> font_names;
  294. CFArrayRef fonts = CTFontManagerCopyAvailableFontFamilyNames();
  295. if (fonts) {
  296. for (CFIndex i = 0; i < CFArrayGetCount(fonts); i++) {
  297. CFStringRef cf_name = (CFStringRef)CFArrayGetValueAtIndex(fonts, i);
  298. if (cf_name && (CFStringGetLength(cf_name) > 0) && (CFStringCompare(cf_name, CFSTR("LastResort"), kCFCompareCaseInsensitive) != kCFCompareEqualTo) && (CFStringGetCharacterAtIndex(cf_name, 0) != '.')) {
  299. NSString *ns_name = (__bridge NSString *)cf_name;
  300. font_names.insert(String::utf8([ns_name UTF8String]));
  301. }
  302. }
  303. CFRelease(fonts);
  304. }
  305. Vector<String> ret;
  306. for (const String &E : font_names) {
  307. ret.push_back(E);
  308. }
  309. return ret;
  310. }
  311. String OS_IOS::_get_default_fontname(const String &p_font_name) const {
  312. String font_name = p_font_name;
  313. if (font_name.to_lower() == "sans-serif") {
  314. font_name = "Helvetica";
  315. } else if (font_name.to_lower() == "serif") {
  316. font_name = "Times";
  317. } else if (font_name.to_lower() == "monospace") {
  318. font_name = "Courier";
  319. } else if (font_name.to_lower() == "fantasy") {
  320. font_name = "Papyrus";
  321. } else if (font_name.to_lower() == "cursive") {
  322. font_name = "Apple Chancery";
  323. };
  324. return font_name;
  325. }
  326. CGFloat OS_IOS::_weight_to_ct(int p_weight) const {
  327. if (p_weight < 150) {
  328. return -0.80;
  329. } else if (p_weight < 250) {
  330. return -0.60;
  331. } else if (p_weight < 350) {
  332. return -0.40;
  333. } else if (p_weight < 450) {
  334. return 0.0;
  335. } else if (p_weight < 550) {
  336. return 0.23;
  337. } else if (p_weight < 650) {
  338. return 0.30;
  339. } else if (p_weight < 750) {
  340. return 0.40;
  341. } else if (p_weight < 850) {
  342. return 0.56;
  343. } else if (p_weight < 925) {
  344. return 0.62;
  345. } else {
  346. return 1.00;
  347. }
  348. }
  349. CGFloat OS_IOS::_stretch_to_ct(int p_stretch) const {
  350. if (p_stretch < 56) {
  351. return -0.5;
  352. } else if (p_stretch < 69) {
  353. return -0.37;
  354. } else if (p_stretch < 81) {
  355. return -0.25;
  356. } else if (p_stretch < 93) {
  357. return -0.13;
  358. } else if (p_stretch < 106) {
  359. return 0.0;
  360. } else if (p_stretch < 137) {
  361. return 0.13;
  362. } else if (p_stretch < 144) {
  363. return 0.25;
  364. } else if (p_stretch < 162) {
  365. return 0.37;
  366. } else {
  367. return 0.5;
  368. }
  369. }
  370. Vector<String> OS_IOS::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const {
  371. Vector<String> ret;
  372. String font_name = _get_default_fontname(p_font_name);
  373. CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8);
  374. CTFontSymbolicTraits traits = 0;
  375. if (p_weight >= 700) {
  376. traits |= kCTFontBoldTrait;
  377. }
  378. if (p_italic) {
  379. traits |= kCTFontItalicTrait;
  380. }
  381. if (p_stretch < 100) {
  382. traits |= kCTFontCondensedTrait;
  383. } else if (p_stretch > 100) {
  384. traits |= kCTFontExpandedTrait;
  385. }
  386. CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits);
  387. CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  388. CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits);
  389. CGFloat weight = _weight_to_ct(p_weight);
  390. CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight);
  391. CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight);
  392. CGFloat stretch = _stretch_to_ct(p_stretch);
  393. CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch);
  394. CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch);
  395. CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  396. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
  397. CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict);
  398. CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes);
  399. if (font) {
  400. CTFontRef family = CTFontCreateWithFontDescriptor(font, 0, nullptr);
  401. CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, p_text.utf8().get_data(), kCFStringEncodingUTF8);
  402. CFRange range = CFRangeMake(0, CFStringGetLength(string));
  403. CTFontRef fallback_family = CTFontCreateForString(family, string, range);
  404. if (fallback_family) {
  405. CTFontDescriptorRef fallback_font = CTFontCopyFontDescriptor(fallback_family);
  406. if (fallback_font) {
  407. CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fallback_font, kCTFontURLAttribute);
  408. if (url) {
  409. NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]];
  410. ret.push_back(String::utf8([font_path UTF8String]));
  411. CFRelease(url);
  412. }
  413. CFRelease(fallback_font);
  414. }
  415. CFRelease(fallback_family);
  416. }
  417. CFRelease(string);
  418. CFRelease(font);
  419. }
  420. CFRelease(attributes);
  421. CFRelease(traits_dict);
  422. CFRelease(sym_traits);
  423. CFRelease(font_stretch);
  424. CFRelease(font_weight);
  425. CFRelease(name);
  426. return ret;
  427. }
  428. String OS_IOS::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  429. String ret;
  430. String font_name = _get_default_fontname(p_font_name);
  431. CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8);
  432. CTFontSymbolicTraits traits = 0;
  433. if (p_weight >= 700) {
  434. traits |= kCTFontBoldTrait;
  435. }
  436. if (p_italic) {
  437. traits |= kCTFontItalicTrait;
  438. }
  439. if (p_stretch < 100) {
  440. traits |= kCTFontCondensedTrait;
  441. } else if (p_stretch > 100) {
  442. traits |= kCTFontExpandedTrait;
  443. }
  444. CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits);
  445. CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  446. CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits);
  447. CGFloat weight = _weight_to_ct(p_weight);
  448. CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight);
  449. CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight);
  450. CGFloat stretch = _stretch_to_ct(p_stretch);
  451. CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch);
  452. CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch);
  453. CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  454. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
  455. CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict);
  456. CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes);
  457. if (font) {
  458. CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(font, kCTFontURLAttribute);
  459. if (url) {
  460. NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]];
  461. ret = String::utf8([font_path UTF8String]);
  462. CFRelease(url);
  463. }
  464. CFRelease(font);
  465. }
  466. CFRelease(attributes);
  467. CFRelease(traits_dict);
  468. CFRelease(sym_traits);
  469. CFRelease(font_stretch);
  470. CFRelease(font_weight);
  471. CFRelease(name);
  472. return ret;
  473. }
  474. void OS_IOS::vibrate_handheld(int p_duration_ms, float p_amplitude) {
  475. if (ios->supports_haptic_engine()) {
  476. if (p_amplitude > 0.0) {
  477. p_amplitude = CLAMP(p_amplitude, 0.0, 1.0);
  478. }
  479. ios->vibrate_haptic_engine((float)p_duration_ms / 1000.f, p_amplitude);
  480. } else {
  481. // iOS <13 does not support duration for vibration
  482. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  483. }
  484. }
  485. bool OS_IOS::_check_internal_feature_support(const String &p_feature) {
  486. if (p_feature == "system_fonts") {
  487. return true;
  488. }
  489. if (p_feature == "mobile") {
  490. return true;
  491. }
  492. return false;
  493. }
  494. void OS_IOS::on_focus_out() {
  495. if (is_focused) {
  496. is_focused = false;
  497. if (DisplayServerIOS::get_singleton()) {
  498. DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  499. }
  500. if (OS::get_singleton()->get_main_loop()) {
  501. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  502. }
  503. [AppDelegate.viewController.godotView stopRendering];
  504. audio_driver.stop();
  505. }
  506. }
  507. void OS_IOS::on_focus_in() {
  508. if (!is_focused) {
  509. is_focused = true;
  510. if (DisplayServerIOS::get_singleton()) {
  511. DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  512. }
  513. if (OS::get_singleton()->get_main_loop()) {
  514. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  515. }
  516. [AppDelegate.viewController.godotView startRendering];
  517. audio_driver.start();
  518. }
  519. }
  520. void OS_IOS::on_enter_background() {
  521. // Do not check for is_focused, because on_focus_out will always be fired first by applicationWillResignActive.
  522. if (OS::get_singleton()->get_main_loop()) {
  523. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  524. }
  525. on_focus_out();
  526. }
  527. void OS_IOS::on_exit_background() {
  528. if (!is_focused) {
  529. on_focus_in();
  530. if (OS::get_singleton()->get_main_loop()) {
  531. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  532. }
  533. }
  534. }
  535. #endif // IOS_ENABLED