[iOS] Fixed freeze in joystick initialization when one or more sensors were not available on a device

This commit is contained in:
Laurent Gomila 2013-11-06 22:45:02 +01:00 committed by Jonathan De Wachter
parent d908ffa498
commit cd89e462fa

View File

@ -48,36 +48,14 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void JoystickImpl::initialize() void JoystickImpl::initialize()
{ {
static const NSTimeInterval updateInterval = 1. / 60.; // Nothing to do
[SFAppDelegate getInstance].motionManager.accelerometerUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startAccelerometerUpdates];
[SFAppDelegate getInstance].motionManager.gyroUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startGyroUpdates];
[SFAppDelegate getInstance].motionManager.magnetometerUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startMagnetometerUpdates];
[SFAppDelegate getInstance].motionManager.deviceMotionUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startDeviceMotionUpdates];
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void JoystickImpl::cleanup() void JoystickImpl::cleanup()
{ {
if ([SFAppDelegate getInstance].motionManager.accelerometerActive) // Nothing to do
[[SFAppDelegate getInstance].motionManager stopAccelerometerUpdates];
if ([SFAppDelegate getInstance].motionManager.gyroActive)
[[SFAppDelegate getInstance].motionManager stopGyroUpdates];
if ([SFAppDelegate getInstance].motionManager.magnetometerActive)
[[SFAppDelegate getInstance].motionManager stopMagnetometerUpdates];
if ([SFAppDelegate getInstance].motionManager.deviceMotionActive)
[[SFAppDelegate getInstance].motionManager stopDeviceMotionUpdates];
} }
@ -108,6 +86,38 @@ bool JoystickImpl::isConnected(unsigned int index)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool JoystickImpl::open(unsigned int index) bool JoystickImpl::open(unsigned int index)
{ {
// Enable the corresponding sensor
static const NSTimeInterval updateInterval = 1. / 60.;
switch (index)
{
case Accelerometer:
[SFAppDelegate getInstance].motionManager.accelerometerUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startAccelerometerUpdates];
break;
case Gyroscope:
[SFAppDelegate getInstance].motionManager.gyroUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startGyroUpdates];
break;
case Magnetometer:
[SFAppDelegate getInstance].motionManager.magnetometerUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startMagnetometerUpdates];
break;
case UserAcceleration:
case AbsoluteOrientation:
if (![SFAppDelegate getInstance].motionManager.deviceMotionActive)
{
[SFAppDelegate getInstance].motionManager.deviceMotionUpdateInterval = updateInterval;
[[SFAppDelegate getInstance].motionManager startDeviceMotionUpdates];
}
break;
default:
break;
}
// Save the index // Save the index
m_index = index; m_index = index;
@ -118,7 +128,33 @@ bool JoystickImpl::open(unsigned int index)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void JoystickImpl::close() void JoystickImpl::close()
{ {
// Nothing to do // Disable the corresponding sensor
switch (m_index)
{
case Accelerometer:
if ([SFAppDelegate getInstance].motionManager.accelerometerActive)
[[SFAppDelegate getInstance].motionManager stopAccelerometerUpdates];
break;
case Gyroscope:
if ([SFAppDelegate getInstance].motionManager.gyroActive)
[[SFAppDelegate getInstance].motionManager stopGyroUpdates];
break;
case Magnetometer:
if ([SFAppDelegate getInstance].motionManager.magnetometerActive)
[[SFAppDelegate getInstance].motionManager stopMagnetometerUpdates];
break;
case UserAcceleration:
case AbsoluteOrientation:
if ([SFAppDelegate getInstance].motionManager.deviceMotionActive)
[[SFAppDelegate getInstance].motionManager stopDeviceMotionUpdates];
break;
default:
break;
}
} }