CCDevice-ios.mm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCApplication.h"
  23. #include "platform/CCDevice.h"
  24. #include "platform/ios/CCEAGLView-ios.h"
  25. // Vibrate
  26. #import <AudioToolbox/AudioToolbox.h>
  27. #include "base/ccTypes.h"
  28. #include "platform/apple/CCDevice-apple.h"
  29. #include "CCReachability.h"
  30. #import <UIKit/UIKit.h>
  31. // Accelerometer
  32. #import <CoreMotion/CoreMotion.h>
  33. #include <CoreFoundation/CoreFoundation.h>
  34. #include <CoreText/CoreText.h>
  35. #include <sys/utsname.h>
  36. static const float g = 9.80665;
  37. static const float radToDeg = (180/M_PI);
  38. @interface CCMotionDispatcher : NSObject<UIAccelerometerDelegate>
  39. {
  40. CMMotionManager* _motionManager;
  41. cocos2d::Device::MotionValue _motionValue;
  42. float _interval; // unit: seconds
  43. bool _enabled;
  44. }
  45. + (id) sharedMotionDispatcher;
  46. - (id) init;
  47. - (void) setMotionEnabled: (bool) isEnabled;
  48. - (void) setMotionInterval:(float) interval;
  49. @end
  50. @implementation CCMotionDispatcher
  51. static CCMotionDispatcher* __motionDispatcher = nullptr;
  52. + (id) sharedMotionDispatcher
  53. {
  54. if (__motionDispatcher == nil) {
  55. __motionDispatcher = [[CCMotionDispatcher alloc] init];
  56. }
  57. return __motionDispatcher;
  58. }
  59. - (id) init
  60. {
  61. if( (self = [super init]) ) {
  62. _enabled = false;
  63. _interval = 1.0f / 60.0f;
  64. _motionManager = [[CMMotionManager alloc] init];
  65. }
  66. return self;
  67. }
  68. - (void) dealloc
  69. {
  70. __motionDispatcher = nullptr;
  71. [_motionManager release];
  72. [super dealloc];
  73. }
  74. - (void) setMotionEnabled: (bool) enabled
  75. {
  76. if (_enabled == enabled)
  77. return;
  78. bool isDeviceMotionAvailable = _motionManager.isDeviceMotionAvailable;
  79. if (enabled)
  80. {
  81. // Has Gyro? (iPhone4 and newer)
  82. if (isDeviceMotionAvailable) {
  83. [_motionManager startDeviceMotionUpdates];
  84. _motionManager.deviceMotionUpdateInterval = _interval;
  85. }
  86. // Only basic accelerometer data
  87. else {
  88. [_motionManager startAccelerometerUpdates];
  89. _motionManager.accelerometerUpdateInterval = _interval;
  90. }
  91. }
  92. else
  93. {
  94. // Has Gyro? (iPhone4 and newer)
  95. if (isDeviceMotionAvailable) {
  96. [_motionManager stopDeviceMotionUpdates];
  97. }
  98. // Only basic accelerometer data
  99. else {
  100. [_motionManager stopAccelerometerUpdates];
  101. }
  102. }
  103. _enabled = enabled;
  104. }
  105. -(void) setMotionInterval:(float)interval
  106. {
  107. _interval = interval;
  108. if (_enabled)
  109. {
  110. if (_motionManager.isDeviceMotionAvailable) {
  111. _motionManager.deviceMotionUpdateInterval = _interval;
  112. }
  113. else {
  114. _motionManager.accelerometerUpdateInterval = _interval;
  115. }
  116. }
  117. }
  118. -(const cocos2d::Device::MotionValue&) getMotionValue {
  119. if (_motionManager.isDeviceMotionAvailable) {
  120. CMDeviceMotion* motion = _motionManager.deviceMotion;
  121. _motionValue.accelerationX = motion.userAcceleration.x * g;
  122. _motionValue.accelerationY = motion.userAcceleration.y * g;
  123. _motionValue.accelerationZ = motion.userAcceleration.z * g;
  124. _motionValue.accelerationIncludingGravityX = (motion.userAcceleration.x + motion.gravity.x) * g;
  125. _motionValue.accelerationIncludingGravityY = (motion.userAcceleration.y + motion.gravity.y) * g;
  126. _motionValue.accelerationIncludingGravityZ = (motion.userAcceleration.z + motion.gravity.z) * g;
  127. _motionValue.rotationRateAlpha = motion.rotationRate.x * radToDeg;
  128. _motionValue.rotationRateBeta = motion.rotationRate.y * radToDeg;
  129. _motionValue.rotationRateGamma = motion.rotationRate.z * radToDeg;
  130. }
  131. else {
  132. CMAccelerometerData* acc = _motionManager.accelerometerData;
  133. _motionValue.accelerationIncludingGravityX = acc.acceleration.x * g;
  134. _motionValue.accelerationIncludingGravityY = acc.acceleration.y * g;
  135. _motionValue.accelerationIncludingGravityZ = acc.acceleration.z * g;
  136. }
  137. return _motionValue;
  138. }
  139. @end
  140. //
  141. NS_CC_BEGIN
  142. int Device::getDPI()
  143. {
  144. static int dpi = -1;
  145. if (dpi == -1)
  146. {
  147. float scale = 1.0f;
  148. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  149. scale = [[UIScreen mainScreen] scale];
  150. }
  151. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  152. dpi = 132 * scale;
  153. } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  154. dpi = 163 * scale;
  155. } else {
  156. dpi = 160 * scale;
  157. }
  158. }
  159. return dpi;
  160. }
  161. void Device::setAccelerometerEnabled(bool isEnabled)
  162. {
  163. #if !defined(CC_TARGET_OS_TVOS)
  164. [[CCMotionDispatcher sharedMotionDispatcher] setMotionEnabled:isEnabled];
  165. #endif
  166. }
  167. void Device::setAccelerometerInterval(float interval)
  168. {
  169. #if !defined(CC_TARGET_OS_TVOS)
  170. [[CCMotionDispatcher sharedMotionDispatcher] setMotionInterval:interval];
  171. #endif
  172. }
  173. const Device::MotionValue& Device::getDeviceMotionValue()
  174. {
  175. #if !defined(CC_TARGET_OS_TVOS)
  176. return [[CCMotionDispatcher sharedMotionDispatcher] getMotionValue];
  177. #else
  178. static Device::MotionValue ret;
  179. return ret;
  180. #endif
  181. }
  182. Device::Rotation Device::getDeviceRotation()
  183. {
  184. Rotation ret = Device::Rotation::_0;
  185. switch ([[UIApplication sharedApplication] statusBarOrientation])
  186. {
  187. case UIInterfaceOrientationLandscapeRight:
  188. ret = Device::Rotation::_90;
  189. break;
  190. case UIInterfaceOrientationLandscapeLeft:
  191. ret = Device::Rotation::_270;
  192. break;
  193. case UIInterfaceOrientationPortraitUpsideDown:
  194. ret = Device::Rotation::_180;
  195. break;
  196. case UIInterfaceOrientationPortrait:
  197. ret = Device::Rotation::_0;
  198. break;
  199. default:
  200. assert(false);
  201. break;
  202. }
  203. return ret;
  204. }
  205. std::string Device::getDeviceModel()
  206. {
  207. struct utsname systemInfo;
  208. uname(&systemInfo);
  209. return systemInfo.machine;
  210. }
  211. void Device::setKeepScreenOn(bool value)
  212. {
  213. [[UIApplication sharedApplication] setIdleTimerDisabled:(BOOL)value];
  214. }
  215. /*!
  216. @brief Only works on iOS devices that support vibration (such as iPhone). Should only be used for important alerts. Use risks rejection in iTunes Store.
  217. @param duration ignored for iOS
  218. */
  219. void Device::vibrate(float duration)
  220. {
  221. // See https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html#//apple_ref/c/econst/kSystemSoundID_Vibrate
  222. CC_UNUSED_PARAM(duration);
  223. // automatically vibrates for approximately 0.4 seconds
  224. AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
  225. }
  226. float Device::getBatteryLevel()
  227. {
  228. return [UIDevice currentDevice].batteryLevel;
  229. }
  230. Device::NetworkType Device::getNetworkType()
  231. {
  232. static Reachability* __reachability = nullptr;
  233. if (__reachability == nullptr)
  234. {
  235. __reachability = Reachability::createForInternetConnection();
  236. __reachability->retain();
  237. }
  238. NetworkType ret = NetworkType::NONE;
  239. Reachability::NetworkStatus status = __reachability->getCurrentReachabilityStatus();
  240. switch (status) {
  241. case Reachability::NetworkStatus::REACHABLE_VIA_WIFI:
  242. ret = NetworkType::LAN;
  243. break;
  244. case Reachability::NetworkStatus::REACHABLE_VIA_WWAN:
  245. ret = NetworkType::WWAN;
  246. break;
  247. default:
  248. ret = NetworkType::NONE;
  249. break;
  250. }
  251. return ret;
  252. }
  253. cocos2d::Vec4 Device::getSafeAreaEdge()
  254. {
  255. UIView* screenView = (UIView*)Application::getInstance()->getView();
  256. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  257. float version = [[UIDevice currentDevice].systemVersion floatValue];
  258. if (version >= 11.0f)
  259. {
  260. #pragma clang diagnostic push
  261. #pragma clang diagnostic ignored "-Wpartial-availability"
  262. UIEdgeInsets safeAreaEdge = screenView.safeAreaInsets;
  263. #pragma clang diagnostic pop
  264. return cocos2d::Vec4(safeAreaEdge.top, safeAreaEdge.left, safeAreaEdge.bottom, safeAreaEdge.right);
  265. }
  266. #endif
  267. // If running on iOS devices lower than 11.0, return ZERO Vec4.
  268. return cocos2d::Vec4();
  269. }
  270. NS_CC_END