DeviceSettings.mm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #include <sys/types.h>
  2. #include <sys/sysctl.h>
  3. #include "UnityAppController.h"
  4. #include "UnityView.h"
  5. #include "DisplayManager.h"
  6. // ad/vendor ids
  7. #if UNITY_USES_IAD
  8. #include <AdSupport/ASIdentifierManager.h>
  9. static id QueryASIdentifierManager()
  10. {
  11. NSBundle* bundle = [NSBundle bundleWithPath: @"/System/Library/Frameworks/AdSupport.framework"];
  12. if (bundle)
  13. {
  14. [bundle load];
  15. return [NSClassFromString(@"ASIdentifierManager") performSelector: @selector(sharedManager)];
  16. }
  17. return nil;
  18. }
  19. #endif
  20. extern "C" const char* UnityAdIdentifier()
  21. {
  22. static const char* _ADID = NULL;
  23. #if UNITY_USES_IAD
  24. static const NSString* _ADIDNSString = nil;
  25. // ad id can be reset during app lifetime
  26. id manager = QueryASIdentifierManager();
  27. if (manager)
  28. {
  29. NSString* adid = [[manager performSelector: @selector(advertisingIdentifier)] UUIDString];
  30. // Do stuff to avoid UTF8String leaks. We still leak if ADID changes, but that shouldn't happen too often.
  31. if (![_ADIDNSString isEqualToString: adid])
  32. {
  33. _ADIDNSString = adid;
  34. free((void*)_ADID);
  35. _ADID = AllocCString(adid);
  36. }
  37. }
  38. #endif
  39. return _ADID;
  40. }
  41. extern "C" int UnityGetLowPowerModeEnabled()
  42. {
  43. return [[NSProcessInfo processInfo] isLowPowerModeEnabled] ? 1 : 0;
  44. }
  45. extern "C" int UnityGetWantsSoftwareDimming()
  46. {
  47. #if !PLATFORM_TVOS && !PLATFORM_VISIONOS
  48. UIScreen* mainScreen = [UIScreen mainScreen];
  49. return mainScreen.wantsSoftwareDimming ? 1 : 0;
  50. #else
  51. return 0;
  52. #endif
  53. }
  54. extern "C" void UnitySetWantsSoftwareDimming(int enabled)
  55. {
  56. #if !PLATFORM_TVOS && !PLATFORM_VISIONOS
  57. UIScreen* mainScreen = [UIScreen mainScreen];
  58. mainScreen.wantsSoftwareDimming = enabled;
  59. #endif
  60. }
  61. extern "C" int UnityGetIosAppOnMac()
  62. {
  63. if (@available(iOS 14, tvOS 14, *))
  64. return [[NSProcessInfo processInfo] isiOSAppOnMac] ? 1 : 0;
  65. return 0;
  66. }
  67. extern "C" int UnityAdTrackingEnabled()
  68. {
  69. bool _AdTrackingEnabled = false;
  70. #if UNITY_USES_IAD
  71. // ad tracking can be changed during app lifetime
  72. id manager = QueryASIdentifierManager();
  73. if (manager)
  74. _AdTrackingEnabled = [manager performSelector: @selector(isAdvertisingTrackingEnabled)];
  75. #endif
  76. return _AdTrackingEnabled ? 1 : 0;
  77. }
  78. extern "C" const char* UnityVendorIdentifier()
  79. {
  80. static const char* _VendorID = NULL;
  81. if (_VendorID == NULL)
  82. _VendorID = AllocCString([[UIDevice currentDevice].identifierForVendor UUIDString]);
  83. return _VendorID;
  84. }
  85. // UIDevice properties
  86. #define QUERY_UIDEVICE_PROPERTY(FUNC, PROP) \
  87. extern "C" const char* FUNC() \
  88. { \
  89. static const char* value = NULL; \
  90. if (value == NULL && [UIDevice instancesRespondToSelector:@selector(PROP)]) \
  91. value = AllocCString([UIDevice currentDevice].PROP); \
  92. return value; \
  93. }
  94. QUERY_UIDEVICE_PROPERTY(UnityDeviceName, name)
  95. QUERY_UIDEVICE_PROPERTY(UnitySystemName, systemName)
  96. QUERY_UIDEVICE_PROPERTY(UnitySystemVersion, systemVersion)
  97. #undef QUERY_UIDEVICE_PROPERTY
  98. // hw info
  99. extern "C" const char* UnityDeviceModel()
  100. {
  101. static const char* _DeviceModel = NULL;
  102. if (_DeviceModel == NULL)
  103. {
  104. size_t size;
  105. ::sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  106. char* model = (char*)::malloc(size + 1);
  107. ::sysctlbyname("hw.machine", model, &size, NULL, 0);
  108. model[size] = 0;
  109. #if TARGET_OS_SIMULATOR
  110. if (!strncmp(model, "i386", 4) || !strncmp(model, "x86_64", 6))
  111. {
  112. NSString* simModel = [[NSProcessInfo processInfo] environment][@"SIMULATOR_MODEL_IDENTIFIER"];
  113. if ([simModel length] > 0)
  114. {
  115. _DeviceModel = AllocCString(simModel);
  116. ::free(model);
  117. return _DeviceModel;
  118. }
  119. }
  120. #endif
  121. _DeviceModel = AllocCString([NSString stringWithUTF8String: model]);
  122. ::free(model);
  123. }
  124. return _DeviceModel;
  125. }
  126. extern "C" int UnityDeviceCPUCount()
  127. {
  128. static int _DeviceCPUCount = -1;
  129. if (_DeviceCPUCount <= 0)
  130. {
  131. // maybe would be better to use HW_AVAILCPU
  132. int ctlName[] = {CTL_HW, HW_NCPU};
  133. size_t dataLen = sizeof(_DeviceCPUCount);
  134. ::sysctl(ctlName, 2, &_DeviceCPUCount, &dataLen, NULL, 0);
  135. }
  136. return _DeviceCPUCount;
  137. }
  138. extern "C" int UnityGetPhysicalMemory()
  139. {
  140. return (int)(NSProcessInfo.processInfo.physicalMemory / (1024ULL * 1024ULL));
  141. }
  142. // misc
  143. extern "C" const char* UnitySystemLanguage()
  144. {
  145. static const char* _SystemLanguage = NULL;
  146. if (_SystemLanguage == NULL)
  147. {
  148. NSArray* lang = [[NSUserDefaults standardUserDefaults] objectForKey: @"AppleLanguages"];
  149. if (lang.count > 0)
  150. _SystemLanguage = AllocCString(lang[0]);
  151. }
  152. return _SystemLanguage;
  153. }
  154. enum DeviceType : uint8_t
  155. {
  156. deviceTypeUnknown = 0,
  157. iPhone = 1,
  158. iPad = 2,
  159. iPod = 3,
  160. AppleTV = 4
  161. };
  162. struct DeviceTableEntry
  163. {
  164. DeviceType deviceType;
  165. uint8_t majorGen;
  166. uint8_t minorGenMin;
  167. uint8_t minorGenMax;
  168. DeviceGeneration device;
  169. };
  170. DeviceTableEntry DeviceTable[] =
  171. {
  172. { iPhone, 2, 1, 1, deviceiPhone3GS },
  173. { iPhone, 3, 1, 3, deviceiPhone4 },
  174. { iPhone, 4, 1, 1, deviceiPhone4S },
  175. { iPhone, 5, 3, 4, deviceiPhone5C },
  176. { iPhone, 5, 1, 2, deviceiPhone5 },
  177. { iPhone, 6, 1, 2, deviceiPhone5S },
  178. { iPhone, 7, 2, 2, deviceiPhone6 },
  179. { iPhone, 7, 1, 1, deviceiPhone6Plus },
  180. { iPhone, 8, 1, 1, deviceiPhone6S },
  181. { iPhone, 8, 2, 2, deviceiPhone6SPlus },
  182. { iPhone, 8, 4, 4, deviceiPhoneSE1Gen },
  183. { iPhone, 9, 1, 1, deviceiPhone7 },
  184. { iPhone, 9, 3, 3, deviceiPhone7 },
  185. { iPhone, 9, 2, 2, deviceiPhone7Plus },
  186. { iPhone, 9, 4, 4, deviceiPhone7Plus },
  187. { iPhone, 10, 1, 1, deviceiPhone8 },
  188. { iPhone, 10, 4, 4, deviceiPhone8 },
  189. { iPhone, 10, 2, 2, deviceiPhone8Plus },
  190. { iPhone, 10, 5, 5, deviceiPhone8Plus },
  191. { iPhone, 10, 3, 3, deviceiPhoneX },
  192. { iPhone, 10, 6, 6, deviceiPhoneX },
  193. { iPhone, 11, 8, 8, deviceiPhoneXR },
  194. { iPhone, 11, 2, 2, deviceiPhoneXS },
  195. { iPhone, 11, 4, 4, deviceiPhoneXSMax },
  196. { iPhone, 11, 6, 6, deviceiPhoneXSMax },
  197. { iPhone, 12, 1, 1, deviceiPhone11 },
  198. { iPhone, 12, 3, 3, deviceiPhone11Pro },
  199. { iPhone, 12, 5, 5, deviceiPhone11ProMax },
  200. { iPhone, 12, 8, 8, deviceiPhoneSE2Gen },
  201. { iPhone, 13, 1, 1, deviceiPhone12Mini },
  202. { iPhone, 13, 2, 2, deviceiPhone12 },
  203. { iPhone, 13, 3, 3, deviceiPhone12Pro },
  204. { iPhone, 13, 4, 4, deviceiPhone12ProMax },
  205. { iPhone, 14, 4, 4, deviceiPhone13Mini },
  206. { iPhone, 14, 5, 5, deviceiPhone13 },
  207. { iPhone, 14, 2, 2, deviceiPhone13Pro },
  208. { iPhone, 14, 3, 3, deviceiPhone13ProMax },
  209. { iPhone, 14, 6, 6, deviceiPhoneSE3Gen },
  210. { iPhone, 14, 7, 7, deviceiPhone14 },
  211. { iPhone, 14, 8, 8, deviceiPhone14Plus },
  212. { iPhone, 15, 2, 2, deviceiPhone14Pro },
  213. { iPhone, 15, 3, 3, deviceiPhone14ProMax },
  214. { iPod, 4, 1, 1, deviceiPodTouch4Gen },
  215. { iPod, 5, 1, 1, deviceiPodTouch5Gen },
  216. { iPod, 7, 1, 1, deviceiPodTouch6Gen },
  217. { iPod, 9, 1, 1, deviceiPodTouch7Gen },
  218. { iPad, 2, 5, 7, deviceiPadMini1Gen },
  219. { iPad, 4, 4, 6, deviceiPadMini2Gen },
  220. { iPad, 4, 7, 9, deviceiPadMini3Gen },
  221. { iPad, 5, 1, 2, deviceiPadMini4Gen },
  222. { iPad, 11, 1, 2, deviceiPadMini5Gen },
  223. { iPad, 14, 1, 2, deviceiPadMini6Gen },
  224. { iPad, 2, 1, 4, deviceiPad2Gen },
  225. { iPad, 3, 1, 3, deviceiPad3Gen },
  226. { iPad, 3, 4, 6, deviceiPad4Gen },
  227. { iPad, 6, 11, 12, deviceiPad5Gen },
  228. { iPad, 7, 5, 6, deviceiPad6Gen },
  229. { iPad, 7, 11, 12, deviceiPad7Gen },
  230. { iPad, 12, 1, 2, deviceiPad9Gen },
  231. { iPad, 4, 1, 3, deviceiPadAir1 },
  232. { iPad, 5, 3, 4, deviceiPadAir2 },
  233. { iPad, 11, 3, 4, deviceiPadAir3Gen },
  234. { iPad, 6, 7, 8, deviceiPadPro1Gen },
  235. { iPad, 7, 1, 2, deviceiPadPro2Gen },
  236. { iPad, 6, 3, 4, deviceiPadPro10Inch1Gen },
  237. { iPad, 7, 3, 4, deviceiPadPro10Inch2Gen },
  238. { iPad, 8, 1, 4, deviceiPadPro11Inch },
  239. { iPad, 8, 9, 10, deviceiPadPro11Inch2Gen },
  240. { iPad, 13, 6, 7, deviceiPadPro11Inch3Gen },
  241. { iPad, 8, 5, 8, deviceiPadPro3Gen },
  242. { iPad, 8, 11, 12, deviceiPadPro4Gen },
  243. { iPad, 13, 10, 11, deviceiPadPro5Gen },
  244. { iPad, 11, 6, 7, deviceiPad8Gen },
  245. { iPad, 13, 1, 2, deviceiPadAir4Gen },
  246. { iPad, 13, 16, 17, deviceiPadAir5Gen },
  247. { iPad, 14, 5, 6, deviceiPadPro6Gen },
  248. { iPad, 14, 3, 4, deviceiPadPro11Inch4Gen },
  249. { iPad, 13, 18, 19, deviceiPad10Gen },
  250. { AppleTV, 5, 3, 3, deviceAppleTVHD },
  251. { AppleTV, 6, 2, 2, deviceAppleTV4K },
  252. { AppleTV, 11, 1, 1, deviceAppleTV4K2Gen },
  253. { AppleTV, 14, 1, 1, deviceAppleTV4K3Gen },
  254. };
  255. extern "C" int ParseDeviceGeneration(const char* model)
  256. {
  257. DeviceType deviceType = deviceTypeUnknown;
  258. if (!strncmp(model, "iPhone", 6))
  259. {
  260. deviceType = iPhone;
  261. model += 6;
  262. }
  263. else if (!strncmp(model, "iPad", 4))
  264. {
  265. deviceType = iPad;
  266. model += 4;
  267. }
  268. else if (!strncmp(model, "iPod", 4))
  269. {
  270. deviceType = iPod;
  271. model += 4;
  272. }
  273. else if (!strncmp(model, "AppleTV", 7))
  274. {
  275. deviceType = AppleTV;
  276. model += 7;
  277. }
  278. char* endPtr;
  279. int majorGen = (int)strtol(model, &endPtr, 10);
  280. int minorGen = (int)strtol(endPtr + 1, &endPtr, 10);
  281. if (strlen(endPtr) == 0)
  282. {
  283. for (int i = 0; i < sizeof(DeviceTable) / sizeof(DeviceTable[0]); ++i)
  284. {
  285. if (deviceType != DeviceTable[i].deviceType)
  286. continue;
  287. if (majorGen != DeviceTable[i].majorGen)
  288. continue;
  289. if (minorGen < DeviceTable[i].minorGenMin || minorGen > DeviceTable[i].minorGenMax)
  290. continue;
  291. return DeviceTable[i].device;
  292. }
  293. }
  294. if (deviceType == iPhone)
  295. return deviceiPhoneUnknown;
  296. else if (deviceType == iPad)
  297. return deviceiPadUnknown;
  298. else if (deviceType == iPod)
  299. return deviceiPodTouchUnknown;
  300. return deviceUnknown;
  301. }
  302. extern "C" int UnityDeviceGeneration()
  303. {
  304. static int _DeviceGeneration = deviceUnknown;
  305. if (_DeviceGeneration == deviceUnknown)
  306. {
  307. const char* model = UnityDeviceModel();
  308. _DeviceGeneration = ParseDeviceGeneration(model);
  309. }
  310. return _DeviceGeneration;
  311. }
  312. // Currently a manual process to add devices that have a cutout (notch). If you add one here you need to also update UnityView GetCutoutToScreenRatio()
  313. extern "C" int UnityDeviceHasCutout()
  314. {
  315. switch (UnityDeviceGeneration())
  316. {
  317. case deviceiPhoneX: case deviceiPhoneXS: case deviceiPhoneXSMax: case deviceiPhoneXR:
  318. case deviceiPhone11: case deviceiPhone11Pro: case deviceiPhone11ProMax:
  319. case deviceiPhone12: case deviceiPhone12Mini: case deviceiPhone12Pro: case deviceiPhone12ProMax:
  320. case deviceiPhone13: case deviceiPhone13Mini: case deviceiPhone13Pro: case deviceiPhone13ProMax:
  321. case deviceiPhone14: case deviceiPhone14Plus: case deviceiPhone14Pro: case deviceiPhone14ProMax:
  322. return 1;
  323. default:
  324. return 0;
  325. }
  326. }
  327. // Devices with a cutout do not support Portrait UpsideDown orientation.
  328. extern "C" int UnityDeviceSupportsUpsideDown()
  329. {
  330. return UnityDeviceHasCutout() ? 0 : 1;
  331. }
  332. extern "C" int UnityDeviceSupportedOrientations()
  333. {
  334. int orientations = (1 << portrait) | (1 << landscapeLeft) | (1 << landscapeRight);
  335. if (UnityDeviceSupportsUpsideDown())
  336. orientations |= (1 << portraitUpsideDown);
  337. return orientations;
  338. }
  339. extern "C" int UnityDeviceIsForceTouchSupported()
  340. {
  341. return UnityGetUnityView().traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
  342. }
  343. extern "C" int UnityDeviceIsStylusTouchSupported()
  344. {
  345. const int deviceGen = UnityDeviceGeneration();
  346. return (deviceGen == deviceiPadPro1Gen ||
  347. deviceGen == deviceiPadPro10Inch1Gen ||
  348. deviceGen == deviceiPadPro2Gen ||
  349. deviceGen == deviceiPadPro10Inch2Gen ||
  350. deviceGen == deviceiPadPro11Inch ||
  351. deviceGen == deviceiPadPro3Gen ||
  352. deviceGen == deviceiPad6Gen) ? 1 : 0;
  353. }
  354. extern "C" int UnityDeviceCanShowWideColor()
  355. {
  356. return UnityGetUnityView().traitCollection.displayGamut == UIDisplayGamutP3;
  357. }
  358. extern "C" float UnityDeviceDPI()
  359. {
  360. static float _DeviceDPI = -1.0f;
  361. if (_DeviceDPI < 0.0f)
  362. {
  363. switch (UnityDeviceGeneration())
  364. {
  365. // iPhone
  366. case deviceiPhone3GS:
  367. _DeviceDPI = 163.0f; break;
  368. case deviceiPhone4:
  369. case deviceiPhone4S:
  370. case deviceiPhone5:
  371. case deviceiPhone5C:
  372. case deviceiPhone5S:
  373. case deviceiPhone6:
  374. case deviceiPhone6S:
  375. case deviceiPhoneSE1Gen:
  376. case deviceiPhone7:
  377. case deviceiPhone8:
  378. case deviceiPhoneXR:
  379. case deviceiPhone11:
  380. case deviceiPhoneSE2Gen:
  381. case deviceiPhoneSE3Gen:
  382. _DeviceDPI = 326.0f; break;
  383. case deviceiPhone6Plus:
  384. case deviceiPhone6SPlus:
  385. case deviceiPhone7Plus:
  386. case deviceiPhone8Plus:
  387. _DeviceDPI = 401.0f; break;
  388. case deviceiPhoneX:
  389. case deviceiPhoneXS:
  390. case deviceiPhoneXSMax:
  391. case deviceiPhone11Pro:
  392. case deviceiPhone11ProMax:
  393. case deviceiPhone12ProMax:
  394. case deviceiPhone13ProMax:
  395. case deviceiPhone14Plus:
  396. _DeviceDPI = 458.0f; break;
  397. case deviceiPhone12:
  398. case deviceiPhone12Pro:
  399. case deviceiPhone13:
  400. case deviceiPhone13Pro:
  401. case deviceiPhone14:
  402. case deviceiPhone14Pro:
  403. case deviceiPhone14ProMax:
  404. _DeviceDPI = 460.0f; break;
  405. case deviceiPhone12Mini:
  406. case deviceiPhone13Mini:
  407. _DeviceDPI = 476.0f; break;
  408. // iPad
  409. case deviceiPad2Gen:
  410. _DeviceDPI = 132.0f; break;
  411. case deviceiPad3Gen:
  412. case deviceiPad4Gen: // iPad retina
  413. case deviceiPadAir1:
  414. case deviceiPadAir2:
  415. case deviceiPadAir3Gen:
  416. case deviceiPadPro1Gen:
  417. case deviceiPadPro10Inch1Gen:
  418. case deviceiPadPro2Gen:
  419. case deviceiPadPro10Inch2Gen:
  420. case deviceiPad5Gen:
  421. case deviceiPad6Gen:
  422. case deviceiPadPro11Inch:
  423. case deviceiPadPro3Gen:
  424. case deviceiPadPro11Inch2Gen:
  425. case deviceiPadPro11Inch3Gen:
  426. case deviceiPadPro4Gen:
  427. case deviceiPadPro5Gen:
  428. case deviceiPad8Gen:
  429. case deviceiPadAir4Gen:
  430. case deviceiPad9Gen:
  431. case deviceiPadAir5Gen:
  432. _DeviceDPI = 264.0f; break;
  433. case deviceiPad7Gen:
  434. _DeviceDPI = 326.0f; break;
  435. // iPad mini
  436. case deviceiPadMini1Gen:
  437. _DeviceDPI = 163.0f; break;
  438. case deviceiPadMini2Gen:
  439. case deviceiPadMini3Gen:
  440. case deviceiPadMini4Gen:
  441. case deviceiPadMini5Gen:
  442. case deviceiPadMini6Gen:
  443. _DeviceDPI = 326.0f; break;
  444. // iPod
  445. case deviceiPodTouch4Gen:
  446. case deviceiPodTouch5Gen:
  447. case deviceiPodTouch6Gen:
  448. case deviceiPodTouch7Gen:
  449. _DeviceDPI = 326.0f; break;
  450. // unknown (new) devices
  451. case deviceiPhoneUnknown:
  452. _DeviceDPI = 326.0f; break;
  453. case deviceiPadUnknown:
  454. case deviceiPadPro6Gen:
  455. case deviceiPadPro11Inch4Gen:
  456. case deviceiPad10Gen:
  457. _DeviceDPI = 264.0f; break;
  458. case deviceiPodTouchUnknown:
  459. _DeviceDPI = 326.0f; break;
  460. }
  461. // If we didn't find DPI, set it to "unknown" value.
  462. if (_DeviceDPI < 0.0f)
  463. _DeviceDPI = 0.0f;
  464. }
  465. return _DeviceDPI;
  466. }
  467. // device id with fallback for pre-ios7
  468. extern "C" const char* UnityDeviceUniqueIdentifier()
  469. {
  470. static const char* _DeviceID = NULL;
  471. if (_DeviceID == NULL)
  472. _DeviceID = UnityVendorIdentifier();
  473. return _DeviceID;
  474. }