os_ios.mm 22 KB

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