iPhone_Sensors.mm 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. #define SIMULATE_ATTITUDE_FROM_GRAVITY 1
  2. #import "iPhone_Sensors.h"
  3. #if UNITY_USES_LOCATION
  4. #import <CoreLocation/CoreLocation.h>
  5. #endif
  6. #if !PLATFORM_TVOS
  7. #import <CoreMotion/CoreMotion.h>
  8. #endif
  9. #import <GameController/GameController.h>
  10. #include "OrientationSupport.h"
  11. #include "Unity/UnityInterface.h"
  12. #include "Vector3.h"
  13. #include "Quaternion4.h"
  14. typedef void (^ControllerPausedHandler)(GCController *controller);
  15. static NSArray* QueryControllerCollection();
  16. #if PLATFORM_TVOS
  17. static bool gTVRemoteTouchesEnabled = true;
  18. static bool gTVRemoteAllowRotationInitialValue = false;
  19. static bool gTVRemoteReportsAbsoluteDpadValuesInitialValue = false;
  20. #endif
  21. static bool gCompensateSensors = true;
  22. bool gEnableGyroscope = false;
  23. static bool gJoysticksInited = false;
  24. #define MAX_JOYSTICKS 4
  25. static bool gPausedJoysticks[MAX_JOYSTICKS] = {false, false, false, false};
  26. static id gGameControllerClass = nil;
  27. // This defines the number of maximum acceleration events Unity will queue internally for scripts to access.
  28. int gMaxQueuedAccelerationEvents = 2 * 60; // 120 events or 2 seconds at 60Hz reporting.
  29. static ControllerPausedHandler gControllerHandler = ^(GCController *controller)
  30. {
  31. NSArray* list = QueryControllerCollection();
  32. if (list != nil)
  33. {
  34. NSUInteger idx = [list indexOfObject: controller];
  35. if (idx < MAX_JOYSTICKS)
  36. {
  37. gPausedJoysticks[idx] = !gPausedJoysticks[idx];
  38. }
  39. }
  40. };
  41. bool IsCompensatingSensors() { return gCompensateSensors; }
  42. void SetCompensatingSensors(bool val) { gCompensateSensors = val; }
  43. inline float UnityReorientHeading(float heading)
  44. {
  45. if (IsCompensatingSensors())
  46. {
  47. float rotateBy = 0.f;
  48. switch (UnityCurrentOrientation())
  49. {
  50. case portraitUpsideDown:
  51. rotateBy = -180.f;
  52. break;
  53. case landscapeLeft:
  54. rotateBy = -270.f;
  55. break;
  56. case landscapeRight:
  57. rotateBy = -90.f;
  58. break;
  59. default:
  60. break;
  61. }
  62. return fmodf((360.f + heading + rotateBy), 360.f);
  63. }
  64. else
  65. {
  66. return heading;
  67. }
  68. }
  69. inline Vector3f UnityReorientVector3(float x, float y, float z)
  70. {
  71. if (IsCompensatingSensors())
  72. {
  73. Vector3f res;
  74. switch (UnityCurrentOrientation())
  75. {
  76. case portraitUpsideDown:
  77. { res = (Vector3f) {-x, -y, z}; }
  78. break;
  79. case landscapeLeft:
  80. { res = (Vector3f) {-y, x, z}; }
  81. break;
  82. case landscapeRight:
  83. { res = (Vector3f) {y, -x, z}; }
  84. break;
  85. default:
  86. { res = (Vector3f) {x, y, z}; }
  87. }
  88. return res;
  89. }
  90. else
  91. {
  92. return (Vector3f) {x, y, z};
  93. }
  94. }
  95. inline Quaternion4f UnityReorientQuaternion(float x, float y, float z, float w)
  96. {
  97. if (IsCompensatingSensors())
  98. {
  99. Quaternion4f res, inp = {x, y, z, w};
  100. switch (UnityCurrentOrientation())
  101. {
  102. case landscapeLeft:
  103. QuatMultiply(res, inp, gQuatRot[1]);
  104. break;
  105. case portraitUpsideDown:
  106. QuatMultiply(res, inp, gQuatRot[2]);
  107. break;
  108. case landscapeRight:
  109. QuatMultiply(res, inp, gQuatRot[3]);
  110. break;
  111. default:
  112. res = inp;
  113. }
  114. return res;
  115. }
  116. else
  117. {
  118. return (Quaternion4f) {x, y, z, w};
  119. }
  120. }
  121. #if PLATFORM_TVOS
  122. static bool sGCMotionForwardingEnabled = false;
  123. static bool sGCMotionForwardedForCurrentFrame = false;
  124. #else
  125. static CMMotionManager* sMotionManager = nil;
  126. static NSOperationQueue* sMotionQueue = nil;
  127. #endif
  128. // Current update interval or 0.0f if not initialized. This is returned
  129. // to the user as current update interval and this value is set to 0.0f when
  130. // gyroscope is disabled.
  131. static float sUpdateInterval = 0.0f;
  132. // Update interval set by the user. Core motion will be set-up to use
  133. // this update interval after disabling and re-enabling gyroscope
  134. // so users can set update interval, disable gyroscope, enable gyroscope and
  135. // after that gyroscope will be updated at this previously set interval.
  136. #if !PLATFORM_TVOS
  137. static float sUserUpdateInterval = 1.0f / 30.0f;
  138. #endif
  139. void SensorsCleanup()
  140. {
  141. #if !PLATFORM_TVOS
  142. if (sMotionManager != nil)
  143. {
  144. [sMotionManager stopGyroUpdates];
  145. [sMotionManager stopDeviceMotionUpdates];
  146. [sMotionManager stopAccelerometerUpdates];
  147. sMotionManager = nil;
  148. }
  149. sMotionQueue = nil;
  150. #endif
  151. }
  152. extern "C" void UnityCoreMotionStart()
  153. {
  154. #if PLATFORM_TVOS
  155. sGCMotionForwardingEnabled = true;
  156. #else
  157. if (sMotionQueue == nil)
  158. sMotionQueue = [[NSOperationQueue alloc] init];
  159. bool initMotionManager = (sMotionManager == nil);
  160. if (initMotionManager)
  161. sMotionManager = [[CMMotionManager alloc] init];
  162. if (gEnableGyroscope && sMotionManager.gyroAvailable)
  163. {
  164. [sMotionManager startGyroUpdates];
  165. [sMotionManager setGyroUpdateInterval: sUpdateInterval];
  166. }
  167. if (gEnableGyroscope && sMotionManager.deviceMotionAvailable)
  168. {
  169. [sMotionManager startDeviceMotionUpdates];
  170. [sMotionManager setDeviceMotionUpdateInterval: sUpdateInterval];
  171. }
  172. if (initMotionManager && sMotionManager.accelerometerAvailable)
  173. {
  174. int frequency = UnityGetAccelerometerFrequency();
  175. if (frequency > 0)
  176. {
  177. [sMotionManager startAccelerometerUpdatesToQueue: sMotionQueue withHandler:^(CMAccelerometerData* data, NSError* error) {
  178. Vector3f res = UnityReorientVector3(data.acceleration.x, data.acceleration.y, data.acceleration.z);
  179. UnityDidAccelerate(res.x, res.y, res.z, data.timestamp);
  180. }];
  181. [sMotionManager setAccelerometerUpdateInterval: 1.0f / frequency];
  182. }
  183. }
  184. #endif
  185. }
  186. extern "C" void UnityCoreMotionStop()
  187. {
  188. #if PLATFORM_TVOS
  189. sGCMotionForwardingEnabled = false;
  190. #else
  191. if (sMotionManager != nil)
  192. {
  193. [sMotionManager stopGyroUpdates];
  194. [sMotionManager stopDeviceMotionUpdates];
  195. }
  196. #endif
  197. }
  198. extern "C" void UnitySetGyroUpdateInterval(int idx, float interval)
  199. {
  200. #if !PLATFORM_TVOS
  201. static const float _MinUpdateInterval = 1.0f / 60.0f;
  202. static const float _MaxUpdateInterval = 1.0f;
  203. if (interval < _MinUpdateInterval)
  204. interval = _MinUpdateInterval;
  205. else if (interval > _MaxUpdateInterval)
  206. interval = _MaxUpdateInterval;
  207. sUserUpdateInterval = interval;
  208. if (sMotionManager)
  209. {
  210. sUpdateInterval = interval;
  211. [sMotionManager setGyroUpdateInterval: interval];
  212. [sMotionManager setDeviceMotionUpdateInterval: interval];
  213. }
  214. #endif
  215. }
  216. extern "C" float UnityGetGyroUpdateInterval(int idx)
  217. {
  218. return sUpdateInterval;
  219. }
  220. extern "C" void UnityUpdateGyroData()
  221. {
  222. #if !PLATFORM_TVOS
  223. CMRotationRate rotationRate = { 0.0, 0.0, 0.0 };
  224. CMRotationRate rotationRateUnbiased = { 0.0, 0.0, 0.0 };
  225. CMAcceleration userAcceleration = { 0.0, 0.0, 0.0 };
  226. CMAcceleration gravity = { 0.0, 0.0, 0.0 };
  227. CMQuaternion attitude = { 0.0, 0.0, 0.0, 1.0 };
  228. if (sMotionManager != nil)
  229. {
  230. CMGyroData *gyroData = sMotionManager.gyroData;
  231. CMDeviceMotion *motionData = sMotionManager.deviceMotion;
  232. if (gyroData != nil)
  233. {
  234. rotationRate = gyroData.rotationRate;
  235. }
  236. if (motionData != nil)
  237. {
  238. CMAttitude *att = motionData.attitude;
  239. attitude = att.quaternion;
  240. rotationRateUnbiased = motionData.rotationRate;
  241. userAcceleration = motionData.userAcceleration;
  242. gravity = motionData.gravity;
  243. }
  244. }
  245. Vector3f reorientedRotRate = UnityReorientVector3(rotationRate.x, rotationRate.y, rotationRate.z);
  246. UnitySensorsSetGyroRotationRate(0, reorientedRotRate.x, reorientedRotRate.y, reorientedRotRate.z);
  247. Vector3f reorientedRotRateUnbiased = UnityReorientVector3(rotationRateUnbiased.x, rotationRateUnbiased.y, rotationRateUnbiased.z);
  248. UnitySensorsSetGyroRotationRateUnbiased(0, reorientedRotRateUnbiased.x, reorientedRotRateUnbiased.y, reorientedRotRateUnbiased.z);
  249. Vector3f reorientedUserAcc = UnityReorientVector3(userAcceleration.x, userAcceleration.y, userAcceleration.z);
  250. UnitySensorsSetUserAcceleration(0, reorientedUserAcc.x, reorientedUserAcc.y, reorientedUserAcc.z);
  251. Vector3f reorientedG = UnityReorientVector3(gravity.x, gravity.y, gravity.z);
  252. UnitySensorsSetGravity(0, reorientedG.x, reorientedG.y, reorientedG.z);
  253. Quaternion4f reorientedAtt = UnityReorientQuaternion(attitude.x, attitude.y, attitude.z, attitude.w);
  254. UnitySensorsSetAttitude(0, reorientedAtt.x, reorientedAtt.y, reorientedAtt.z, reorientedAtt.w);
  255. #endif
  256. }
  257. extern "C" int UnityIsGyroEnabled(int idx)
  258. {
  259. #if PLATFORM_TVOS
  260. return sGCMotionForwardingEnabled;
  261. #else
  262. if (sMotionManager == nil)
  263. return 0;
  264. return sMotionManager.gyroAvailable && sMotionManager.gyroActive;
  265. #endif
  266. }
  267. extern "C" int UnityIsGyroAvailable()
  268. {
  269. #if PLATFORM_TVOS
  270. return true;
  271. #else
  272. if (sMotionManager != nil)
  273. return sMotionManager.gyroAvailable;
  274. #endif
  275. return 0;
  276. }
  277. // -- Joystick stuff --
  278. #pragma clang diagnostic push
  279. #pragma clang diagnostic ignored "-Wobjc-method-access"
  280. enum JoystickButtonNumbers
  281. {
  282. BTN_PAUSE = 0,
  283. BTN_DPAD_UP = 4,
  284. BTN_DPAD_RIGHT = 5,
  285. BTN_DPAD_DOWN = 6,
  286. BTN_DPAD_LEFT = 7,
  287. BTN_Y = 12,
  288. BTN_B = 13,
  289. BTN_A = 14,
  290. BTN_X = 15,
  291. BTN_L1 = 8,
  292. BTN_L2 = 10,
  293. BTN_R1 = 9,
  294. BTN_R2 = 11,
  295. BTN_MENU = 16,
  296. BTN_COUNT
  297. };
  298. typedef struct
  299. {
  300. int buttonCode;
  301. bool state;
  302. } JoystickButtonState;
  303. JoystickButtonState gAggregatedJoystickState[BTN_COUNT];
  304. static float GetAxisValue(GCControllerAxisInput* axis)
  305. {
  306. return axis.value;
  307. }
  308. static float GetButtonValue(GCControllerButtonInput* button)
  309. {
  310. return button.value;
  311. }
  312. static BOOL GetButtonPressed(GCControllerButtonInput* button)
  313. {
  314. return button.pressed;
  315. }
  316. extern "C" void UnityInitJoysticks()
  317. {
  318. if (!gJoysticksInited)
  319. {
  320. NSBundle* bundle = [NSBundle bundleWithPath: @"/System/Library/Frameworks/GameController.framework"];
  321. if (bundle)
  322. {
  323. [bundle load];
  324. gGameControllerClass = [bundle classNamed: @"GCController"];
  325. //Apply settings that could have been set by user scripts before controller initialization
  326. #if PLATFORM_TVOS
  327. UnitySetAppleTVRemoteAllowRotation(gTVRemoteAllowRotationInitialValue);
  328. UnitySetAppleTVRemoteReportAbsoluteDpadValues(gTVRemoteReportsAbsoluteDpadValuesInitialValue);
  329. #endif
  330. }
  331. for (int i = 0; i < BTN_COUNT; i++)
  332. {
  333. char buf[128];
  334. sprintf(buf, "joystick button %d", i);
  335. gAggregatedJoystickState[i].buttonCode = UnityStringToKey(buf);
  336. gAggregatedJoystickState[i].state = false;
  337. }
  338. gJoysticksInited = true;
  339. }
  340. }
  341. static NSArray* QueryControllerCollection()
  342. {
  343. return gGameControllerClass != nil ? (NSArray*)[gGameControllerClass performSelector: @selector(controllers)] : nil;
  344. }
  345. static void ResetAggregatedJoystickState()
  346. {
  347. for (int i = 0; i < BTN_COUNT; i++)
  348. {
  349. gAggregatedJoystickState[i].state = false;
  350. }
  351. }
  352. static void SetAggregatedJoystickState()
  353. {
  354. for (int i = 0; i < BTN_COUNT; i++)
  355. {
  356. UnitySetKeyState(gAggregatedJoystickState[i].buttonCode, gAggregatedJoystickState[i].state);
  357. }
  358. }
  359. // Mirror button input into virtual joystick 0
  360. static void ReportAggregatedJoystickButton(int buttonNum, int state)
  361. {
  362. assert(buttonNum < BTN_COUNT);
  363. gAggregatedJoystickState[buttonNum].state |= (bool)state;
  364. }
  365. static void SetJoystickButtonState(int joyNum, int buttonNum, int state)
  366. {
  367. char buf[128];
  368. sprintf(buf, "joystick %d button %d", joyNum, buttonNum);
  369. UnitySetKeyState(UnityStringToKey(buf), state);
  370. ReportAggregatedJoystickButton(buttonNum, state);
  371. }
  372. static void ReportJoystickButton(int idx, JoystickButtonNumbers num, GCControllerButtonInput* button)
  373. {
  374. SetJoystickButtonState(idx + 1, num, GetButtonPressed(button));
  375. UnitySetJoystickPosition(idx + 1, num, GetButtonValue(button));
  376. }
  377. template<class ClassXYZ>
  378. static void ReportJoystickXYZAxes(int idx, int xaxis, int yaxis, int zaxis, const ClassXYZ& xyz)
  379. {
  380. UnitySetJoystickPosition(idx + 1, xaxis, xyz.x);
  381. UnitySetJoystickPosition(idx + 1, yaxis, xyz.y);
  382. UnitySetJoystickPosition(idx + 1, zaxis, xyz.z);
  383. }
  384. template<class ClassXYZW>
  385. static void ReportJoystickXYZWAxes(int idx, int xaxis, int yaxis, int zaxis, int waxis,
  386. const ClassXYZW& xyzw)
  387. {
  388. UnitySetJoystickPosition(idx + 1, xaxis, xyzw.x);
  389. UnitySetJoystickPosition(idx + 1, yaxis, xyzw.y);
  390. UnitySetJoystickPosition(idx + 1, zaxis, xyzw.z);
  391. UnitySetJoystickPosition(idx + 1, waxis, xyzw.w);
  392. }
  393. #if PLATFORM_TVOS
  394. static void ReportJoystickMicro(int idx, GCMicroGamepad* gamepad)
  395. {
  396. GCControllerDirectionPad* dpad = [gamepad dpad];
  397. UnitySetJoystickPosition(idx + 1, 0, GetAxisValue([dpad xAxis]));
  398. UnitySetJoystickPosition(idx + 1, 1, -GetAxisValue([dpad yAxis]));
  399. ReportJoystickButton(idx, BTN_DPAD_UP, [dpad up]);
  400. ReportJoystickButton(idx, BTN_DPAD_RIGHT, [dpad right]);
  401. ReportJoystickButton(idx, BTN_DPAD_DOWN, [dpad down]);
  402. ReportJoystickButton(idx, BTN_DPAD_LEFT, [dpad left]);
  403. ReportJoystickButton(idx, BTN_A, [gamepad buttonA]);
  404. ReportJoystickButton(idx, BTN_X, [gamepad buttonX]);
  405. }
  406. #endif
  407. static void ReportJoystickBasic(int idx, GCGamepad* gamepad)
  408. {
  409. GCControllerDirectionPad* dpad = [gamepad dpad];
  410. UnitySetJoystickPosition(idx + 1, 0, GetAxisValue([dpad xAxis]));
  411. UnitySetJoystickPosition(idx + 1, 1, -GetAxisValue([dpad yAxis]));
  412. ReportJoystickButton(idx, BTN_DPAD_UP, [dpad up]);
  413. ReportJoystickButton(idx, BTN_DPAD_RIGHT, [dpad right]);
  414. ReportJoystickButton(idx, BTN_DPAD_DOWN, [dpad down]);
  415. ReportJoystickButton(idx, BTN_DPAD_LEFT, [dpad left]);
  416. ReportJoystickButton(idx, BTN_A, [gamepad buttonA]);
  417. ReportJoystickButton(idx, BTN_B, [gamepad buttonB]);
  418. ReportJoystickButton(idx, BTN_Y, [gamepad buttonY]);
  419. ReportJoystickButton(idx, BTN_X, [gamepad buttonX]);
  420. ReportJoystickButton(idx, BTN_L1, [gamepad leftShoulder]);
  421. ReportJoystickButton(idx, BTN_R1, [gamepad rightShoulder]);
  422. }
  423. static void ReportJoystickExtended(int idx, GCExtendedGamepad* gamepad)
  424. {
  425. GCControllerDirectionPad* dpad = [gamepad dpad];
  426. GCControllerDirectionPad* leftStick = [gamepad leftThumbstick];
  427. GCControllerDirectionPad* rightStick = [gamepad rightThumbstick];
  428. UnitySetJoystickPosition(idx + 1, 0, GetAxisValue([leftStick xAxis]));
  429. UnitySetJoystickPosition(idx + 1, 1, -GetAxisValue([leftStick yAxis]));
  430. UnitySetJoystickPosition(idx + 1, 2, GetAxisValue([rightStick xAxis]));
  431. UnitySetJoystickPosition(idx + 1, 3, -GetAxisValue([rightStick yAxis]));
  432. ReportJoystickButton(idx, BTN_DPAD_UP, [dpad up]);
  433. ReportJoystickButton(idx, BTN_DPAD_RIGHT, [dpad right]);
  434. ReportJoystickButton(idx, BTN_DPAD_DOWN, [dpad down]);
  435. ReportJoystickButton(idx, BTN_DPAD_LEFT, [dpad left]);
  436. ReportJoystickButton(idx, BTN_A, [gamepad buttonA]);
  437. ReportJoystickButton(idx, BTN_B, [gamepad buttonB]);
  438. ReportJoystickButton(idx, BTN_Y, [gamepad buttonY]);
  439. ReportJoystickButton(idx, BTN_X, [gamepad buttonX]);
  440. ReportJoystickButton(idx, BTN_L1, [gamepad leftShoulder]);
  441. ReportJoystickButton(idx, BTN_R1, [gamepad rightShoulder]);
  442. ReportJoystickButton(idx, BTN_L2, [gamepad leftTrigger]);
  443. ReportJoystickButton(idx, BTN_R2, [gamepad rightTrigger]);
  444. }
  445. static void SimulateAttitudeViaGravityVector(const Vector3f& gravity, Quaternion4f& currentAttitude, Vector3f& rotationRate)
  446. {
  447. static Quaternion4f lastAttitude = QuatIdentity();
  448. static double lastTime = 0.0;
  449. double currentTime = [NSDate timeIntervalSinceReferenceDate];
  450. double deltaTime = lastTime - currentTime;
  451. currentAttitude = QuatRotationFromTo(gravity, VecMake(0.0f, 0.0f, -1.0f));
  452. rotationRate = VecScale(1.0f / deltaTime, QuatToEuler(QuatDifference(currentAttitude, lastAttitude)));
  453. lastAttitude = currentAttitude;
  454. lastTime = currentTime;
  455. }
  456. // Note that joystick axis numbers in documentation are shifted
  457. // by one. 1st axis is referred to by index 0, 16th by 15, etc.
  458. static void ReportJoystickMotion(int idx, GCMotion* motion)
  459. {
  460. Vector3f rotationRate = VecMake(0.0f, 0.0f, 0.0f);
  461. Quaternion4f attitude = QuatMake(0.0f, 0.0f, 0.0f, 1.0f);
  462. bool gotRotationData = false;
  463. if ([motion respondsToSelector: @selector(hasAttitudeAndRotationRate)])
  464. {
  465. #if UNITY_HAS_IOSSDK_11_0 || UNITY_HAS_TVOSSDK_11_0
  466. if (motion.hasAttitudeAndRotationRate)
  467. {
  468. rotationRate = {(float)motion.rotationRate.x, (float)motion.rotationRate.y, (float)motion.rotationRate.z};
  469. attitude = {(float)motion.attitude.x, (float)motion.attitude.y, (float)motion.attitude.z, (float)motion.attitude.w};
  470. gotRotationData = true;
  471. }
  472. #endif
  473. }
  474. else
  475. {
  476. #if PLATFORM_IOS
  477. // on iOS we assume that rotationRate and attitude is correct, unless
  478. // hasAttitudeAndRotationRate tells us otherwise.
  479. // on tvOS, rotationRate and attitude are unavailable if hasAttitudeAndRotationRate is unavailable.
  480. rotationRate = {(float)motion.rotationRate.x, (float)motion.rotationRate.y, (float)motion.rotationRate.z};
  481. attitude = {(float)motion.attitude.x, (float)motion.attitude.y, (float)motion.attitude.z, (float)motion.attitude.w};
  482. gotRotationData = true;
  483. #endif
  484. }
  485. #if SIMULATE_ATTITUDE_FROM_GRAVITY
  486. if (!gotRotationData)
  487. SimulateAttitudeViaGravityVector(VecMake((float)motion.gravity.x, (float)motion.gravity.y, (float)motion.gravity.z), attitude, rotationRate);
  488. #endif
  489. // From docs:
  490. // gravity (x,y,z) : 16, 17, 18
  491. // user acceleration: 19, 20, 21
  492. // rotation rate: 22, 23, 24
  493. // attitude quaternion (x,y,z,w): 25, 26, 27, 28
  494. ReportJoystickXYZAxes(idx, 15, 16, 17, motion.gravity);
  495. ReportJoystickXYZAxes(idx, 18, 19, 20, motion.userAcceleration);
  496. ReportJoystickXYZAxes(idx, 21, 22, 23, rotationRate);
  497. ReportJoystickXYZWAxes(idx, 24, 25, 26, 27, attitude);
  498. #if PLATFORM_TVOS
  499. if (sGCMotionForwardingEnabled && !sGCMotionForwardedForCurrentFrame)
  500. {
  501. UnitySensorsSetGravity(0, motion.gravity.x, motion.gravity.y, motion.gravity.z);
  502. UnitySensorsSetUserAcceleration(0, motion.userAcceleration.x, motion.userAcceleration.y, motion.userAcceleration.z);
  503. UnitySensorsSetGyroRotationRate(0, rotationRate.y, rotationRate.x, rotationRate.z);
  504. UnitySensorsSetAttitude(0, attitude.x, attitude.y, attitude.z, attitude.w);
  505. UnityDidAccelerate(motion.userAcceleration.x + motion.gravity.x, motion.userAcceleration.y + motion.gravity.y, motion.userAcceleration.z + motion.gravity.z, [[NSDate date] timeIntervalSince1970]);
  506. sGCMotionForwardedForCurrentFrame = true;
  507. }
  508. #endif
  509. }
  510. static void ReportJoystick(GCController* controller, int idx)
  511. {
  512. if (controller.controllerPausedHandler == nil)
  513. controller.controllerPausedHandler = gControllerHandler;
  514. if ([controller extendedGamepad] != nil)
  515. ReportJoystickExtended(idx, [controller extendedGamepad]);
  516. else if ([controller gamepad] != nil)
  517. ReportJoystickBasic(idx, [controller gamepad]);
  518. #if PLATFORM_TVOS
  519. else if ([controller microGamepad] != nil)
  520. ReportJoystickMicro(idx, [controller microGamepad]);
  521. #endif
  522. else
  523. {
  524. // TODO: do something with not supported gamepad profiles
  525. }
  526. if (controller.motion != nil)
  527. ReportJoystickMotion(idx, controller.motion);
  528. // Map pause button
  529. SetJoystickButtonState(idx + 1, BTN_PAUSE, gPausedJoysticks[idx]);
  530. // Reset pause button
  531. gPausedJoysticks[idx] = false;
  532. }
  533. // On tvOS simulator we implement a fake remote as tvOS simulator
  534. // does not support controllers (yet)
  535. #if UNITY_TVOS_SIMULATOR_FAKE_REMOTE
  536. struct FakeRemoteState
  537. {
  538. bool pressedX, pressedA;
  539. bool pressedUp, pressedDown, pressedLeft, pressedRight;
  540. float xAxis, yAxis;
  541. FakeRemoteState() :
  542. pressedX(false),
  543. pressedA(false),
  544. pressedUp(false),
  545. pressedDown(false),
  546. pressedLeft(false),
  547. pressedRight(false),
  548. xAxis(0),
  549. yAxis(0)
  550. {}
  551. };
  552. static FakeRemoteState gFakeRemoteState;
  553. static void ReportFakeRemoteButton(int idx, JoystickButtonNumbers num, bool pressed)
  554. {
  555. SetJoystickButtonState(idx + 1, num, pressed);
  556. UnitySetJoystickPosition(idx + 1, num, pressed);
  557. }
  558. void ReportFakeRemote(int idx)
  559. {
  560. UnitySetJoystickPosition(idx + 1, 0, gFakeRemoteState.xAxis);
  561. UnitySetJoystickPosition(idx + 1, 1, -gFakeRemoteState.yAxis);
  562. ReportFakeRemoteButton(idx, BTN_DPAD_UP, gFakeRemoteState.pressedUp);
  563. ReportFakeRemoteButton(idx, BTN_DPAD_RIGHT, gFakeRemoteState.pressedRight);
  564. ReportFakeRemoteButton(idx, BTN_DPAD_DOWN, gFakeRemoteState.pressedDown);
  565. ReportFakeRemoteButton(idx, BTN_DPAD_LEFT, gFakeRemoteState.pressedLeft);
  566. ReportFakeRemoteButton(idx, BTN_A, gFakeRemoteState.pressedA);
  567. ReportFakeRemoteButton(idx, BTN_X, gFakeRemoteState.pressedX);
  568. }
  569. #endif
  570. extern "C" void UnityUpdateJoystickData()
  571. {
  572. UnityInitJoysticks();
  573. NSArray* list = QueryControllerCollection();
  574. #if PLATFORM_TVOS
  575. sGCMotionForwardedForCurrentFrame = false;
  576. #endif
  577. // Clear aggregated joystick state
  578. ResetAggregatedJoystickState();
  579. if (list != nil)
  580. {
  581. for (int i = 0; i < [list count]; i++)
  582. {
  583. id controller = [list objectAtIndex: i];
  584. ReportJoystick(controller, i);
  585. }
  586. }
  587. #if UNITY_TVOS_SIMULATOR_FAKE_REMOTE
  588. int remoteIndex = list != nil ? (int)[list count] : 0;
  589. ReportFakeRemote(remoteIndex);
  590. #endif
  591. // Report all aggregated joystick button in bulk
  592. SetAggregatedJoystickState();
  593. }
  594. extern "C" int UnityGetJoystickCount()
  595. {
  596. NSArray* list = QueryControllerCollection();
  597. int count = list != nil ? (int)[list count] : 0;
  598. #if UNITY_TVOS_SIMULATOR_FAKE_REMOTE
  599. count++;
  600. #endif
  601. return count;
  602. }
  603. static void PrintJoystickIdentifier(int idx, char* buffer, int maxLen, const char* typeString,
  604. const char* attachment, const char* vendorName)
  605. {
  606. snprintf(buffer, maxLen, "[%s,%s] joystick %d by %s",
  607. typeString, attachment, idx + 1, vendorName);
  608. }
  609. extern "C" void UnityGetJoystickName(int idx, char* buffer, int maxLen)
  610. {
  611. GCController* controller = [QueryControllerCollection() objectAtIndex: idx];
  612. if (controller != nil)
  613. {
  614. // iOS 8 has bug, which is encountered when controller is being attached
  615. // while app is still running. It creates two instances of controller object:
  616. // one original and one "Forwarded", accesing later properties are causing crashes
  617. const char* attached = "unknown";
  618. // Controller is good one
  619. if ([[controller vendorName] rangeOfString: @"Forwarded"].location == NSNotFound)
  620. attached = (controller.attachedToDevice ? "wired" : "wireless");
  621. const char* typeString = [controller extendedGamepad] != nil ? "extended" : "basic";
  622. PrintJoystickIdentifier(idx, buffer, maxLen, typeString, attached,
  623. [[controller vendorName] UTF8String]);
  624. }
  625. else
  626. {
  627. #if UNITY_TVOS_SIMULATOR_FAKE_REMOTE
  628. if (idx == [QueryControllerCollection() count])
  629. {
  630. PrintJoystickIdentifier(idx, buffer, maxLen, "basic", "wireless", "Unity");
  631. return;
  632. }
  633. #endif
  634. strncpy(buffer, "unknown", maxLen);
  635. }
  636. }
  637. extern "C" void UnityGetJoystickAxisName(int idx, int axis, char* buffer, int maxLen)
  638. {
  639. }
  640. extern "C" void UnityGetNiceKeyname(int key, char* buffer, int maxLen)
  641. {
  642. }
  643. #pragma clang diagnostic pop
  644. #if UNITY_USES_LOCATION
  645. @interface LocationServiceDelegate : NSObject<CLLocationManagerDelegate>
  646. @end
  647. #endif
  648. void
  649. UnitySetLastLocation(double timestamp,
  650. float latitude,
  651. float longitude,
  652. float altitude,
  653. float horizontalAccuracy,
  654. float verticalAccuracy);
  655. void
  656. UnitySetLastHeading(float magneticHeading,
  657. float trueHeading,
  658. float rawX, float rawY, float rawZ,
  659. double timestamp);
  660. #if UNITY_USES_LOCATION
  661. struct LocationServiceInfo
  662. {
  663. private:
  664. LocationServiceDelegate* delegate;
  665. CLLocationManager* locationManager;
  666. public:
  667. LocationServiceStatus locationStatus;
  668. LocationServiceStatus headingStatus;
  669. float desiredAccuracy;
  670. float distanceFilter;
  671. LocationServiceInfo();
  672. CLLocationManager* GetLocationManager();
  673. };
  674. LocationServiceInfo::LocationServiceInfo()
  675. {
  676. locationStatus = kLocationServiceStopped;
  677. desiredAccuracy = kCLLocationAccuracyKilometer;
  678. distanceFilter = 500;
  679. headingStatus = kLocationServiceStopped;
  680. }
  681. static LocationServiceInfo gLocationServiceStatus;
  682. CLLocationManager* LocationServiceInfo::GetLocationManager()
  683. {
  684. if (locationManager == nil)
  685. {
  686. locationManager = [[CLLocationManager alloc] init];
  687. delegate = [LocationServiceDelegate alloc];
  688. locationManager.delegate = delegate;
  689. }
  690. return locationManager;
  691. }
  692. #endif
  693. bool LocationService::IsServiceEnabledByUser()
  694. {
  695. #if UNITY_USES_LOCATION
  696. return [CLLocationManager locationServicesEnabled];
  697. #else
  698. return false;
  699. #endif
  700. }
  701. void LocationService::SetDesiredAccuracy(float val)
  702. {
  703. #if UNITY_USES_LOCATION
  704. gLocationServiceStatus.desiredAccuracy = val;
  705. #endif
  706. }
  707. float LocationService::GetDesiredAccuracy()
  708. {
  709. #if UNITY_USES_LOCATION
  710. return gLocationServiceStatus.desiredAccuracy;
  711. #else
  712. return NAN;
  713. #endif
  714. }
  715. void LocationService::SetDistanceFilter(float val)
  716. {
  717. #if UNITY_USES_LOCATION
  718. gLocationServiceStatus.distanceFilter = val;
  719. #endif
  720. }
  721. float LocationService::GetDistanceFilter()
  722. {
  723. #if UNITY_USES_LOCATION
  724. return gLocationServiceStatus.distanceFilter;
  725. #else
  726. return NAN;
  727. #endif
  728. }
  729. void LocationService::StartUpdatingLocation()
  730. {
  731. #if UNITY_USES_LOCATION
  732. if (gLocationServiceStatus.locationStatus != kLocationServiceRunning)
  733. {
  734. CLLocationManager* locationManager = gLocationServiceStatus.GetLocationManager();
  735. [locationManager requestWhenInUseAuthorization];
  736. locationManager.desiredAccuracy = gLocationServiceStatus.desiredAccuracy;
  737. // Set a movement threshold for new events
  738. locationManager.distanceFilter = gLocationServiceStatus.distanceFilter;
  739. #if PLATFORM_IOS
  740. [locationManager startUpdatingLocation];
  741. #else
  742. [locationManager requestLocation];
  743. #endif
  744. gLocationServiceStatus.locationStatus = kLocationServiceInitializing;
  745. }
  746. #endif
  747. }
  748. void LocationService::StopUpdatingLocation()
  749. {
  750. #if UNITY_USES_LOCATION
  751. if (gLocationServiceStatus.locationStatus != kLocationServiceStopped)
  752. {
  753. [gLocationServiceStatus.GetLocationManager() stopUpdatingLocation];
  754. gLocationServiceStatus.locationStatus = kLocationServiceStopped;
  755. }
  756. #endif
  757. }
  758. void LocationService::SetHeadingUpdatesEnabled(bool enabled)
  759. {
  760. #if PLATFORM_IOS && UNITY_USES_LOCATION
  761. if (enabled)
  762. {
  763. if (gLocationServiceStatus.headingStatus != kLocationServiceRunning &&
  764. IsHeadingAvailable())
  765. {
  766. CLLocationManager* locationManager = gLocationServiceStatus.GetLocationManager();
  767. [locationManager startUpdatingHeading];
  768. gLocationServiceStatus.headingStatus = kLocationServiceInitializing;
  769. }
  770. }
  771. else
  772. {
  773. if (gLocationServiceStatus.headingStatus != kLocationServiceStopped)
  774. {
  775. [gLocationServiceStatus.GetLocationManager() stopUpdatingHeading];
  776. gLocationServiceStatus.headingStatus = kLocationServiceStopped;
  777. }
  778. }
  779. #endif
  780. }
  781. bool LocationService::IsHeadingUpdatesEnabled()
  782. {
  783. #if UNITY_USES_LOCATION
  784. return (gLocationServiceStatus.headingStatus == kLocationServiceRunning);
  785. #else
  786. return false;
  787. #endif
  788. }
  789. int UnityGetLocationStatus()
  790. {
  791. #if UNITY_USES_LOCATION
  792. return (LocationServiceStatus)gLocationServiceStatus.locationStatus;
  793. #else
  794. return kLocationServiceFailed;
  795. #endif
  796. }
  797. int UnityGetHeadingStatus()
  798. {
  799. #if UNITY_USES_LOCATION
  800. return (LocationServiceStatus)gLocationServiceStatus.headingStatus;
  801. #else
  802. return kLocationServiceFailed;
  803. #endif
  804. }
  805. bool LocationService::IsHeadingAvailable()
  806. {
  807. #if PLATFORM_IOS && UNITY_USES_LOCATION
  808. return [CLLocationManager headingAvailable];
  809. #else
  810. return false;
  811. #endif
  812. }
  813. #if UNITY_USES_LOCATION
  814. @implementation LocationServiceDelegate
  815. - (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations
  816. {
  817. CLLocation* lastLocation = locations.lastObject;
  818. gLocationServiceStatus.locationStatus = kLocationServiceRunning;
  819. UnitySetLastLocation([lastLocation.timestamp timeIntervalSince1970],
  820. lastLocation.coordinate.latitude, lastLocation.coordinate.longitude, lastLocation.altitude,
  821. lastLocation.horizontalAccuracy, lastLocation.verticalAccuracy
  822. );
  823. }
  824. #if PLATFORM_IOS
  825. - (void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading
  826. {
  827. gLocationServiceStatus.headingStatus = kLocationServiceRunning;
  828. Vector3f reorientedRawHeading = UnityReorientVector3(newHeading.x, newHeading.y, newHeading.z);
  829. UnitySetLastHeading(UnityReorientHeading(newHeading.magneticHeading),
  830. UnityReorientHeading(newHeading.trueHeading),
  831. reorientedRawHeading.x, reorientedRawHeading.y, reorientedRawHeading.z,
  832. [newHeading.timestamp timeIntervalSince1970]);
  833. }
  834. #endif
  835. - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager*)manager
  836. {
  837. return NO;
  838. }
  839. - (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error;
  840. {
  841. gLocationServiceStatus.locationStatus = kLocationServiceFailed;
  842. gLocationServiceStatus.headingStatus = kLocationServiceFailed;
  843. }
  844. @end
  845. #endif
  846. #if PLATFORM_TVOS
  847. GCMicroGamepad* QueryMicroController()
  848. {
  849. NSArray* list = QueryControllerCollection();
  850. for (GCController* controller in list)
  851. {
  852. if (controller.microGamepad != nil)
  853. return controller.microGamepad;
  854. }
  855. return nil;
  856. }
  857. extern "C" int UnityGetAppleTVRemoteTouchesEnabled()
  858. {
  859. return gTVRemoteTouchesEnabled;
  860. }
  861. extern "C" void UnitySetAppleTVRemoteTouchesEnabled(int val)
  862. {
  863. gTVRemoteTouchesEnabled = val;
  864. }
  865. extern "C" int UnityGetAppleTVRemoteAllowExitToMenu()
  866. {
  867. return ((GCEventViewController*)UnityGetGLViewController()).controllerUserInteractionEnabled;
  868. }
  869. extern "C" void UnitySetAppleTVRemoteAllowExitToMenu(int val)
  870. {
  871. ((GCEventViewController*)UnityGetGLViewController()).controllerUserInteractionEnabled = val;
  872. }
  873. extern "C" int UnityGetAppleTVRemoteAllowRotation()
  874. {
  875. GCMicroGamepad* controller = QueryMicroController();
  876. if (controller != nil)
  877. return controller.allowsRotation;
  878. else
  879. return false;
  880. }
  881. extern "C" void UnitySetAppleTVRemoteAllowRotation(int val)
  882. {
  883. GCMicroGamepad* controller = QueryMicroController();
  884. if (controller != nil)
  885. controller.allowsRotation = val;
  886. else
  887. gTVRemoteAllowRotationInitialValue = val;
  888. }
  889. extern "C" int UnityGetAppleTVRemoteReportAbsoluteDpadValues()
  890. {
  891. GCMicroGamepad* controller = QueryMicroController();
  892. if (controller != nil)
  893. return controller.reportsAbsoluteDpadValues;
  894. else
  895. return false;
  896. }
  897. extern "C" void UnitySetAppleTVRemoteReportAbsoluteDpadValues(int val)
  898. {
  899. NSArray* list = QueryControllerCollection();
  900. for (GCController* controller in list)
  901. {
  902. if (controller.microGamepad != nil)
  903. controller.microGamepad.reportsAbsoluteDpadValues = val;
  904. else
  905. gTVRemoteReportsAbsoluteDpadValuesInitialValue = val;
  906. }
  907. }
  908. #endif
  909. #if UNITY_TVOS_SIMULATOR_FAKE_REMOTE
  910. static void FakeRemoteStateSetButton(UIPressType type, bool state)
  911. {
  912. switch (type)
  913. {
  914. case UIPressTypeUpArrow: gFakeRemoteState.pressedUp = state; return;
  915. case UIPressTypeDownArrow: gFakeRemoteState.pressedDown = state; return;
  916. case UIPressTypeLeftArrow: gFakeRemoteState.pressedLeft = state; return;
  917. case UIPressTypeRightArrow: gFakeRemoteState.pressedRight = state; return;
  918. case UIPressTypeSelect: gFakeRemoteState.pressedA = state; return;
  919. case UIPressTypePlayPause: gFakeRemoteState.pressedX = state; return;
  920. }
  921. }
  922. void ReportSimulatedRemoteButtonPress(UIPressType type)
  923. {
  924. FakeRemoteStateSetButton(type, true);
  925. }
  926. void ReportSimulatedRemoteButtonRelease(UIPressType type)
  927. {
  928. FakeRemoteStateSetButton(type, false);
  929. }
  930. static float FakeRemoteMapTouchToAxis(float pos, float bounds)
  931. {
  932. float halfRange = bounds / 2;
  933. return (pos - halfRange) / halfRange;
  934. }
  935. void ReportSimulatedRemoteTouchesBegan(UIView* view, NSSet* touches)
  936. {
  937. ReportSimulatedRemoteTouchesMoved(view, touches);
  938. }
  939. void ReportSimulatedRemoteTouchesMoved(UIView* view, NSSet* touches)
  940. {
  941. for (UITouch* touch in touches)
  942. {
  943. gFakeRemoteState.xAxis = FakeRemoteMapTouchToAxis([touch locationInView: view].x, view.bounds.size.width);
  944. gFakeRemoteState.yAxis = FakeRemoteMapTouchToAxis([touch locationInView: view].y, view.bounds.size.height);
  945. // We assume that at most single touch is received.
  946. break;
  947. }
  948. }
  949. void ReportSimulatedRemoteTouchesEnded(UIView* view, NSSet* touches)
  950. {
  951. gFakeRemoteState.xAxis = 0;
  952. gFakeRemoteState.yAxis = 0;
  953. }
  954. #endif