os_ios.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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_temp_path() const {
  273. static String ret;
  274. if (ret.is_empty()) {
  275. NSURL *url = [NSURL fileURLWithPath:NSTemporaryDirectory()
  276. isDirectory:YES];
  277. if (url) {
  278. ret = String::utf8([url.path UTF8String]);
  279. ret = ret.trim_prefix("file://");
  280. }
  281. }
  282. return ret;
  283. }
  284. String OS_IOS::get_locale() const {
  285. NSString *preferedLanguage = [NSLocale preferredLanguages].firstObject;
  286. if (preferedLanguage) {
  287. return String::utf8([preferedLanguage UTF8String]).replace("-", "_");
  288. }
  289. NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
  290. return String::utf8([localeIdentifier UTF8String]).replace("-", "_");
  291. }
  292. String OS_IOS::get_unique_id() const {
  293. NSString *uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
  294. return String::utf8([uuid UTF8String]);
  295. }
  296. String OS_IOS::get_processor_name() const {
  297. char buffer[256];
  298. size_t buffer_len = 256;
  299. if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, nullptr, 0) == 0) {
  300. return String::utf8(buffer, buffer_len);
  301. }
  302. ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
  303. }
  304. Vector<String> OS_IOS::get_system_fonts() const {
  305. HashSet<String> font_names;
  306. CFArrayRef fonts = CTFontManagerCopyAvailableFontFamilyNames();
  307. if (fonts) {
  308. for (CFIndex i = 0; i < CFArrayGetCount(fonts); i++) {
  309. CFStringRef cf_name = (CFStringRef)CFArrayGetValueAtIndex(fonts, i);
  310. if (cf_name && (CFStringGetLength(cf_name) > 0) && (CFStringCompare(cf_name, CFSTR("LastResort"), kCFCompareCaseInsensitive) != kCFCompareEqualTo) && (CFStringGetCharacterAtIndex(cf_name, 0) != '.')) {
  311. NSString *ns_name = (__bridge NSString *)cf_name;
  312. font_names.insert(String::utf8([ns_name UTF8String]));
  313. }
  314. }
  315. CFRelease(fonts);
  316. }
  317. Vector<String> ret;
  318. for (const String &E : font_names) {
  319. ret.push_back(E);
  320. }
  321. return ret;
  322. }
  323. String OS_IOS::_get_default_fontname(const String &p_font_name) const {
  324. String font_name = p_font_name;
  325. if (font_name.to_lower() == "sans-serif") {
  326. font_name = "Helvetica";
  327. } else if (font_name.to_lower() == "serif") {
  328. font_name = "Times";
  329. } else if (font_name.to_lower() == "monospace") {
  330. font_name = "Courier";
  331. } else if (font_name.to_lower() == "fantasy") {
  332. font_name = "Papyrus";
  333. } else if (font_name.to_lower() == "cursive") {
  334. font_name = "Apple Chancery";
  335. };
  336. return font_name;
  337. }
  338. CGFloat OS_IOS::_weight_to_ct(int p_weight) const {
  339. if (p_weight < 150) {
  340. return -0.80;
  341. } else if (p_weight < 250) {
  342. return -0.60;
  343. } else if (p_weight < 350) {
  344. return -0.40;
  345. } else if (p_weight < 450) {
  346. return 0.0;
  347. } else if (p_weight < 550) {
  348. return 0.23;
  349. } else if (p_weight < 650) {
  350. return 0.30;
  351. } else if (p_weight < 750) {
  352. return 0.40;
  353. } else if (p_weight < 850) {
  354. return 0.56;
  355. } else if (p_weight < 925) {
  356. return 0.62;
  357. } else {
  358. return 1.00;
  359. }
  360. }
  361. CGFloat OS_IOS::_stretch_to_ct(int p_stretch) const {
  362. if (p_stretch < 56) {
  363. return -0.5;
  364. } else if (p_stretch < 69) {
  365. return -0.37;
  366. } else if (p_stretch < 81) {
  367. return -0.25;
  368. } else if (p_stretch < 93) {
  369. return -0.13;
  370. } else if (p_stretch < 106) {
  371. return 0.0;
  372. } else if (p_stretch < 137) {
  373. return 0.13;
  374. } else if (p_stretch < 144) {
  375. return 0.25;
  376. } else if (p_stretch < 162) {
  377. return 0.37;
  378. } else {
  379. return 0.5;
  380. }
  381. }
  382. 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 {
  383. Vector<String> ret;
  384. String font_name = _get_default_fontname(p_font_name);
  385. CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8);
  386. CTFontSymbolicTraits traits = 0;
  387. if (p_weight >= 700) {
  388. traits |= kCTFontBoldTrait;
  389. }
  390. if (p_italic) {
  391. traits |= kCTFontItalicTrait;
  392. }
  393. if (p_stretch < 100) {
  394. traits |= kCTFontCondensedTrait;
  395. } else if (p_stretch > 100) {
  396. traits |= kCTFontExpandedTrait;
  397. }
  398. CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits);
  399. CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  400. CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits);
  401. CGFloat weight = _weight_to_ct(p_weight);
  402. CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight);
  403. CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight);
  404. CGFloat stretch = _stretch_to_ct(p_stretch);
  405. CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch);
  406. CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch);
  407. CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  408. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
  409. CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict);
  410. CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes);
  411. if (font) {
  412. CTFontRef family = CTFontCreateWithFontDescriptor(font, 0, nullptr);
  413. CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, p_text.utf8().get_data(), kCFStringEncodingUTF8);
  414. CFRange range = CFRangeMake(0, CFStringGetLength(string));
  415. CTFontRef fallback_family = CTFontCreateForString(family, string, range);
  416. if (fallback_family) {
  417. CTFontDescriptorRef fallback_font = CTFontCopyFontDescriptor(fallback_family);
  418. if (fallback_font) {
  419. CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fallback_font, kCTFontURLAttribute);
  420. if (url) {
  421. NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]];
  422. ret.push_back(String::utf8([font_path UTF8String]));
  423. CFRelease(url);
  424. }
  425. CFRelease(fallback_font);
  426. }
  427. CFRelease(fallback_family);
  428. }
  429. CFRelease(string);
  430. CFRelease(font);
  431. }
  432. CFRelease(attributes);
  433. CFRelease(traits_dict);
  434. CFRelease(sym_traits);
  435. CFRelease(font_stretch);
  436. CFRelease(font_weight);
  437. CFRelease(name);
  438. return ret;
  439. }
  440. String OS_IOS::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  441. String ret;
  442. String font_name = _get_default_fontname(p_font_name);
  443. CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8);
  444. CTFontSymbolicTraits traits = 0;
  445. if (p_weight >= 700) {
  446. traits |= kCTFontBoldTrait;
  447. }
  448. if (p_italic) {
  449. traits |= kCTFontItalicTrait;
  450. }
  451. if (p_stretch < 100) {
  452. traits |= kCTFontCondensedTrait;
  453. } else if (p_stretch > 100) {
  454. traits |= kCTFontExpandedTrait;
  455. }
  456. CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits);
  457. CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  458. CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits);
  459. CGFloat weight = _weight_to_ct(p_weight);
  460. CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight);
  461. CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight);
  462. CGFloat stretch = _stretch_to_ct(p_stretch);
  463. CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch);
  464. CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch);
  465. CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr);
  466. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
  467. CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict);
  468. CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes);
  469. if (font) {
  470. CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(font, kCTFontURLAttribute);
  471. if (url) {
  472. NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]];
  473. ret = String::utf8([font_path UTF8String]);
  474. CFRelease(url);
  475. }
  476. CFRelease(font);
  477. }
  478. CFRelease(attributes);
  479. CFRelease(traits_dict);
  480. CFRelease(sym_traits);
  481. CFRelease(font_stretch);
  482. CFRelease(font_weight);
  483. CFRelease(name);
  484. return ret;
  485. }
  486. void OS_IOS::vibrate_handheld(int p_duration_ms, float p_amplitude) {
  487. if (ios->supports_haptic_engine()) {
  488. if (p_amplitude > 0.0) {
  489. p_amplitude = CLAMP(p_amplitude, 0.0, 1.0);
  490. }
  491. ios->vibrate_haptic_engine((float)p_duration_ms / 1000.f, p_amplitude);
  492. } else {
  493. // iOS <13 does not support duration for vibration
  494. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  495. }
  496. }
  497. bool OS_IOS::_check_internal_feature_support(const String &p_feature) {
  498. if (p_feature == "system_fonts") {
  499. return true;
  500. }
  501. if (p_feature == "mobile") {
  502. return true;
  503. }
  504. return false;
  505. }
  506. void OS_IOS::on_focus_out() {
  507. if (is_focused) {
  508. is_focused = false;
  509. if (DisplayServerIOS::get_singleton()) {
  510. DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  511. }
  512. if (OS::get_singleton()->get_main_loop()) {
  513. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  514. }
  515. [AppDelegate.viewController.godotView stopRendering];
  516. audio_driver.stop();
  517. }
  518. }
  519. void OS_IOS::on_focus_in() {
  520. if (!is_focused) {
  521. is_focused = true;
  522. if (DisplayServerIOS::get_singleton()) {
  523. DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  524. }
  525. if (OS::get_singleton()->get_main_loop()) {
  526. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  527. }
  528. [AppDelegate.viewController.godotView startRendering];
  529. audio_driver.start();
  530. }
  531. }
  532. void OS_IOS::on_enter_background() {
  533. // Do not check for is_focused, because on_focus_out will always be fired first by applicationWillResignActive.
  534. if (OS::get_singleton()->get_main_loop()) {
  535. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  536. }
  537. on_focus_out();
  538. }
  539. void OS_IOS::on_exit_background() {
  540. if (!is_focused) {
  541. on_focus_in();
  542. if (OS::get_singleton()->get_main_loop()) {
  543. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  544. }
  545. }
  546. }
  547. #endif // IOS_ENABLED