os_ios.mm 19 KB

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