os_ios.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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_apple = memnew(JoypadApple);
  112. }
  113. void OS_IOS::deinitialize_modules() {
  114. if (joypad_apple) {
  115. memdelete(joypad_apple);
  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. joypad_apple->process_joypads();
  142. return Main::iteration();
  143. }
  144. void OS_IOS::start() {
  145. if (Main::start() == EXIT_SUCCESS) {
  146. main_loop->initialize();
  147. }
  148. }
  149. void OS_IOS::finalize() {
  150. deinitialize_modules();
  151. // Already gets called
  152. //delete_main_loop();
  153. }
  154. // MARK: Dynamic Libraries
  155. _FORCE_INLINE_ String OS_IOS::get_framework_executable(const String &p_path) {
  156. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  157. // Read framework bundle to get executable name.
  158. NSURL *url = [NSURL fileURLWithPath:@(p_path.utf8().get_data())];
  159. NSBundle *bundle = [NSBundle bundleWithURL:url];
  160. if (bundle) {
  161. String exe_path = String::utf8([[bundle executablePath] UTF8String]);
  162. if (da->file_exists(exe_path)) {
  163. return exe_path;
  164. }
  165. }
  166. // Try default executable name (invalid framework).
  167. if (da->dir_exists(p_path) && da->file_exists(p_path.path_join(p_path.get_file().get_basename()))) {
  168. return p_path.path_join(p_path.get_file().get_basename());
  169. }
  170. // Not a framework, try loading as .dylib.
  171. return p_path;
  172. }
  173. Error OS_IOS::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
  174. if (p_path.length() == 0) {
  175. // Static xcframework.
  176. p_library_handle = RTLD_SELF;
  177. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  178. *p_data->r_resolved_path = p_path;
  179. }
  180. return OK;
  181. }
  182. String path = get_framework_executable(p_path);
  183. if (!FileAccess::exists(path)) {
  184. // Load .dylib or framework from within the executable path.
  185. path = get_framework_executable(get_executable_path().get_base_dir().path_join(p_path.get_file()));
  186. }
  187. if (!FileAccess::exists(path)) {
  188. // Load .dylib converted to framework from within the executable path.
  189. path = get_framework_executable(get_executable_path().get_base_dir().path_join(p_path.get_file().get_basename() + ".framework"));
  190. }
  191. if (!FileAccess::exists(path)) {
  192. // Load .dylib or framework from a standard iOS location.
  193. path = get_framework_executable(get_executable_path().get_base_dir().path_join("Frameworks").path_join(p_path.get_file()));
  194. }
  195. if (!FileAccess::exists(path)) {
  196. // Load .dylib converted to framework from a standard iOS location.
  197. path = get_framework_executable(get_executable_path().get_base_dir().path_join("Frameworks").path_join(p_path.get_file().get_basename() + ".framework"));
  198. }
  199. ERR_FAIL_COND_V(!FileAccess::exists(path), ERR_FILE_NOT_FOUND);
  200. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  201. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
  202. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  203. *p_data->r_resolved_path = path;
  204. }
  205. return OK;
  206. }
  207. Error OS_IOS::close_dynamic_library(void *p_library_handle) {
  208. if (p_library_handle == RTLD_SELF) {
  209. return OK;
  210. }
  211. return OS_Unix::close_dynamic_library(p_library_handle);
  212. }
  213. Error OS_IOS::get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional) {
  214. if (p_library_handle == RTLD_SELF) {
  215. void **ptr = OS_IOS::dynamic_symbol_lookup_table.getptr(p_name);
  216. if (ptr) {
  217. p_symbol_handle = *ptr;
  218. return OK;
  219. }
  220. }
  221. return OS_Unix::get_dynamic_library_symbol_handle(p_library_handle, p_name, p_symbol_handle, p_optional);
  222. }
  223. String OS_IOS::get_name() const {
  224. return "iOS";
  225. }
  226. String OS_IOS::get_distribution_name() const {
  227. return get_name();
  228. }
  229. String OS_IOS::get_version() const {
  230. NSOperatingSystemVersion ver = [NSProcessInfo processInfo].operatingSystemVersion;
  231. return vformat("%d.%d.%d", (int64_t)ver.majorVersion, (int64_t)ver.minorVersion, (int64_t)ver.patchVersion);
  232. }
  233. String OS_IOS::get_model_name() const {
  234. String model = ios->get_model();
  235. if (model != "") {
  236. return model;
  237. }
  238. return OS_Unix::get_model_name();
  239. }
  240. Error OS_IOS::shell_open(const String &p_uri) {
  241. NSString *urlPath = [[NSString alloc] initWithUTF8String:p_uri.utf8().get_data()];
  242. NSURL *url = [NSURL URLWithString:urlPath];
  243. if (![[UIApplication sharedApplication] canOpenURL:url]) {
  244. return ERR_CANT_OPEN;
  245. }
  246. print_verbose(vformat("Opening URL %s", p_uri));
  247. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  248. return OK;
  249. }
  250. String OS_IOS::get_user_data_dir(const String &p_user_dir) const {
  251. static String ret;
  252. if (ret.is_empty()) {
  253. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  254. if (paths && [paths count] >= 1) {
  255. ret.parse_utf8([[paths firstObject] UTF8String]);
  256. }
  257. }
  258. return ret;
  259. }
  260. String OS_IOS::get_cache_path() const {
  261. static String ret;
  262. if (ret.is_empty()) {
  263. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  264. if (paths && [paths count] >= 1) {
  265. ret.parse_utf8([[paths firstObject] UTF8String]);
  266. }
  267. }
  268. return ret;
  269. }
  270. String OS_IOS::get_temp_path() const {
  271. static String ret;
  272. if (ret.is_empty()) {
  273. NSURL *url = [NSURL fileURLWithPath:NSTemporaryDirectory()
  274. isDirectory:YES];
  275. if (url) {
  276. ret = String::utf8([url.path UTF8String]);
  277. ret = ret.trim_prefix("file://");
  278. }
  279. }
  280. return ret;
  281. }
  282. String OS_IOS::get_locale() const {
  283. NSString *preferredLanguage = [NSLocale preferredLanguages].firstObject;
  284. if (preferredLanguage) {
  285. return String::utf8([preferredLanguage UTF8String]).replace("-", "_");
  286. }
  287. NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
  288. return String::utf8([localeIdentifier UTF8String]).replace("-", "_");
  289. }
  290. String OS_IOS::get_unique_id() const {
  291. NSString *uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
  292. return String::utf8([uuid UTF8String]);
  293. }
  294. String OS_IOS::get_processor_name() const {
  295. char buffer[256];
  296. size_t buffer_len = 256;
  297. if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, nullptr, 0) == 0) {
  298. return String::utf8(buffer, buffer_len);
  299. }
  300. ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
  301. }
  302. Vector<String> OS_IOS::get_system_fonts() const {
  303. HashSet<String> font_names;
  304. CFArrayRef fonts = CTFontManagerCopyAvailableFontFamilyNames();
  305. if (fonts) {
  306. for (CFIndex i = 0; i < CFArrayGetCount(fonts); i++) {
  307. CFStringRef cf_name = (CFStringRef)CFArrayGetValueAtIndex(fonts, i);
  308. if (cf_name && (CFStringGetLength(cf_name) > 0) && (CFStringCompare(cf_name, CFSTR("LastResort"), kCFCompareCaseInsensitive) != kCFCompareEqualTo) && (CFStringGetCharacterAtIndex(cf_name, 0) != '.')) {
  309. NSString *ns_name = (__bridge NSString *)cf_name;
  310. font_names.insert(String::utf8([ns_name UTF8String]));
  311. }
  312. }
  313. CFRelease(fonts);
  314. }
  315. Vector<String> ret;
  316. for (const String &E : font_names) {
  317. ret.push_back(E);
  318. }
  319. return ret;
  320. }
  321. String OS_IOS::_get_default_fontname(const String &p_font_name) const {
  322. String font_name = p_font_name;
  323. if (font_name.to_lower() == "sans-serif") {
  324. font_name = "Helvetica";
  325. } else if (font_name.to_lower() == "serif") {
  326. font_name = "Times";
  327. } else if (font_name.to_lower() == "monospace") {
  328. font_name = "Courier";
  329. } else if (font_name.to_lower() == "fantasy") {
  330. font_name = "Papyrus";
  331. } else if (font_name.to_lower() == "cursive") {
  332. font_name = "Apple Chancery";
  333. };
  334. return font_name;
  335. }
  336. CGFloat OS_IOS::_weight_to_ct(int p_weight) const {
  337. if (p_weight < 150) {
  338. return -0.80;
  339. } else if (p_weight < 250) {
  340. return -0.60;
  341. } else if (p_weight < 350) {
  342. return -0.40;
  343. } else if (p_weight < 450) {
  344. return 0.0;
  345. } else if (p_weight < 550) {
  346. return 0.23;
  347. } else if (p_weight < 650) {
  348. return 0.30;
  349. } else if (p_weight < 750) {
  350. return 0.40;
  351. } else if (p_weight < 850) {
  352. return 0.56;
  353. } else if (p_weight < 925) {
  354. return 0.62;
  355. } else {
  356. return 1.00;
  357. }
  358. }
  359. CGFloat OS_IOS::_stretch_to_ct(int p_stretch) const {
  360. if (p_stretch < 56) {
  361. return -0.5;
  362. } else if (p_stretch < 69) {
  363. return -0.37;
  364. } else if (p_stretch < 81) {
  365. return -0.25;
  366. } else if (p_stretch < 93) {
  367. return -0.13;
  368. } else if (p_stretch < 106) {
  369. return 0.0;
  370. } else if (p_stretch < 137) {
  371. return 0.13;
  372. } else if (p_stretch < 144) {
  373. return 0.25;
  374. } else if (p_stretch < 162) {
  375. return 0.37;
  376. } else {
  377. return 0.5;
  378. }
  379. }
  380. 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 {
  381. Vector<String> ret;
  382. String font_name = _get_default_fontname(p_font_name);
  383. CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8);
  384. CTFontSymbolicTraits traits = 0;
  385. if (p_weight >= 700) {
  386. traits |= kCTFontBoldTrait;
  387. }
  388. if (p_italic) {
  389. traits |= kCTFontItalicTrait;
  390. }
  391. if (p_stretch < 100) {
  392. traits |= kCTFontCondensedTrait;
  393. } else if (p_stretch > 100) {
  394. traits |= kCTFontExpandedTrait;
  395. }
  396. CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits);
  397. CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  398. CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits);
  399. CGFloat weight = _weight_to_ct(p_weight);
  400. CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight);
  401. CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight);
  402. CGFloat stretch = _stretch_to_ct(p_stretch);
  403. CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch);
  404. CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch);
  405. CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  406. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
  407. CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict);
  408. CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes);
  409. if (font) {
  410. CTFontRef family = CTFontCreateWithFontDescriptor(font, 0, nullptr);
  411. CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, p_text.utf8().get_data(), kCFStringEncodingUTF8);
  412. CFRange range = CFRangeMake(0, CFStringGetLength(string));
  413. CTFontRef fallback_family = CTFontCreateForString(family, string, range);
  414. if (fallback_family) {
  415. CTFontDescriptorRef fallback_font = CTFontCopyFontDescriptor(fallback_family);
  416. if (fallback_font) {
  417. CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fallback_font, kCTFontURLAttribute);
  418. if (url) {
  419. NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]];
  420. ret.push_back(String::utf8([font_path UTF8String]));
  421. CFRelease(url);
  422. }
  423. CFRelease(fallback_font);
  424. }
  425. CFRelease(fallback_family);
  426. }
  427. CFRelease(string);
  428. CFRelease(font);
  429. }
  430. CFRelease(attributes);
  431. CFRelease(traits_dict);
  432. CFRelease(sym_traits);
  433. CFRelease(font_stretch);
  434. CFRelease(font_weight);
  435. CFRelease(name);
  436. return ret;
  437. }
  438. String OS_IOS::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  439. String ret;
  440. String font_name = _get_default_fontname(p_font_name);
  441. CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8);
  442. CTFontSymbolicTraits traits = 0;
  443. if (p_weight >= 700) {
  444. traits |= kCTFontBoldTrait;
  445. }
  446. if (p_italic) {
  447. traits |= kCTFontItalicTrait;
  448. }
  449. if (p_stretch < 100) {
  450. traits |= kCTFontCondensedTrait;
  451. } else if (p_stretch > 100) {
  452. traits |= kCTFontExpandedTrait;
  453. }
  454. CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits);
  455. CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  456. CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits);
  457. CGFloat weight = _weight_to_ct(p_weight);
  458. CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight);
  459. CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight);
  460. CGFloat stretch = _stretch_to_ct(p_stretch);
  461. CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch);
  462. CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch);
  463. CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  464. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
  465. CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict);
  466. CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes);
  467. if (font) {
  468. CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(font, kCTFontURLAttribute);
  469. if (url) {
  470. NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]];
  471. ret = String::utf8([font_path UTF8String]);
  472. CFRelease(url);
  473. }
  474. CFRelease(font);
  475. }
  476. CFRelease(attributes);
  477. CFRelease(traits_dict);
  478. CFRelease(sym_traits);
  479. CFRelease(font_stretch);
  480. CFRelease(font_weight);
  481. CFRelease(name);
  482. return ret;
  483. }
  484. void OS_IOS::vibrate_handheld(int p_duration_ms, float p_amplitude) {
  485. if (ios->supports_haptic_engine()) {
  486. if (p_amplitude > 0.0) {
  487. p_amplitude = CLAMP(p_amplitude, 0.0, 1.0);
  488. }
  489. ios->vibrate_haptic_engine((float)p_duration_ms / 1000.f, p_amplitude);
  490. } else {
  491. // iOS <13 does not support duration for vibration
  492. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  493. }
  494. }
  495. bool OS_IOS::_check_internal_feature_support(const String &p_feature) {
  496. if (p_feature == "system_fonts") {
  497. return true;
  498. }
  499. if (p_feature == "mobile") {
  500. return true;
  501. }
  502. return false;
  503. }
  504. void OS_IOS::on_focus_out() {
  505. if (is_focused) {
  506. is_focused = false;
  507. if (DisplayServerIOS::get_singleton()) {
  508. DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  509. }
  510. if (OS::get_singleton()->get_main_loop()) {
  511. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  512. }
  513. [AppDelegate.viewController.godotView stopRendering];
  514. audio_driver.stop();
  515. }
  516. }
  517. void OS_IOS::on_focus_in() {
  518. if (!is_focused) {
  519. is_focused = true;
  520. if (DisplayServerIOS::get_singleton()) {
  521. DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  522. }
  523. if (OS::get_singleton()->get_main_loop()) {
  524. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  525. }
  526. [AppDelegate.viewController.godotView startRendering];
  527. audio_driver.start();
  528. }
  529. }
  530. void OS_IOS::on_enter_background() {
  531. // Do not check for is_focused, because on_focus_out will always be fired first by applicationWillResignActive.
  532. if (OS::get_singleton()->get_main_loop()) {
  533. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  534. }
  535. on_focus_out();
  536. }
  537. void OS_IOS::on_exit_background() {
  538. if (!is_focused) {
  539. on_focus_in();
  540. if (OS::get_singleton()->get_main_loop()) {
  541. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  542. }
  543. }
  544. }
  545. #endif // IOS_ENABLED