Implemented sensor API

This commit is contained in:
Jonathan De Wachter 2014-01-06 05:37:23 +01:00
parent 59c3c2c0b0
commit 7daaaa649e
20 changed files with 1775 additions and 4 deletions

View file

@ -0,0 +1,94 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/SensorImpl.hpp>
#include <SFML/System/Lock.hpp>
#include <SFML/System/Time.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void SensorImpl::initialize()
{
// To implement
}
////////////////////////////////////////////////////////////
void SensorImpl::cleanup()
{
// To implement
}
////////////////////////////////////////////////////////////
SensorCaps& SensorImpl::initialize(unsigned int type)
{
// To implement
SensorCaps capabilities;
capabilities.available = false;
return capabilities;
}
////////////////////////////////////////////////////////////
void SensorImpl::terminate()
{
// To implement
}
////////////////////////////////////////////////////////////
SensorState& SensorImpl::update()
{
// To implement
static SensorState state;
return state;
}
////////////////////////////////////////////////////////////
bool SensorImpl::isEnable()
{
// To implement
return false;
}
////////////////////////////////////////////////////////////
void SensorImpl::setEnable(bool enable)
{
// To implement
}
////////////////////////////////////////////////////////////
void SensorImpl::setRefreshRate(const Time& rate)
{
// To implement
}
} // namespace priv
} // namespace sf

View file

@ -0,0 +1,108 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORIMPLANDROID_HPP
#define SFML_SENSORIMPLANDROID_HPP
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Android implementation of sensors
///
////////////////////////////////////////////////////////////
class SensorImpl
{
public :
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the sensor module
///
////////////////////////////////////////////////////////////
static void initialize();
////////////////////////////////////////////////////////////
/// \brief Perform the global cleanup of the sensor module
///
////////////////////////////////////////////////////////////
static void cleanup();
////////////////////////////////////////////////////////////
/// \brief Initialize the sensor
///
/// \param type Index assigned to the sensor
///
/// \return The sensor capabilities
///
////////////////////////////////////////////////////////////
SensorCaps& initialize(unsigned int type);
////////////////////////////////////////////////////////////
/// \brief Close the sensor
///
////////////////////////////////////////////////////////////
void terminate();
////////////////////////////////////////////////////////////
/// \brief Update the sensor and get its new state
///
/// \return Sensor state
///
////////////////////////////////////////////////////////////
SensorState& update();
////////////////////////////////////////////////////////////
/// \brief Check if the sensor is enabled
///
/// \return True if the sensor is enabled, false otherwise
///
////////////////////////////////////////////////////////////
bool isEnable();
////////////////////////////////////////////////////////////
/// \brief Enable or disable the sensor
///
/// \param enable True to enable, false to disable
///
////////////////////////////////////////////////////////////
void setEnable(bool enable);
////////////////////////////////////////////////////////////
/// \brief Set the refresh rate of the sensor
///
/// \param rate Delay between each refresh
///
////////////////////////////////////////////////////////////
void setRefreshRate(const Time& rate);
};
} // namespace priv
} // namespace sf
#endif // SFML_SENSORIMPLANDROID_HPP

View file

@ -24,7 +24,12 @@ set(SRC
${INCROOT}/Mouse.hpp
${SRCROOT}/Mouse.cpp
${INCROOT}/Touch.hpp
${SRCROOT}/Touch.cpp
${SRCROOT}/Touch.cpp
${INCROOT}/Sensor.hpp
${SRCROOT}/Sensor.cpp
${SRCROOT}/SensorImpl.hpp
${SRCROOT}/SensorManager.cpp
${SRCROOT}/SensorManager.hpp
${SRCROOT}/VideoMode.cpp
${INCROOT}/VideoMode.hpp
${SRCROOT}/VideoModeImpl.hpp
@ -64,6 +69,8 @@ elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD)
${SRCROOT}/Unix/Display.hpp
${SRCROOT}/Unix/InputImpl.cpp
${SRCROOT}/Unix/InputImpl.hpp
${SRCROOT}/Unix/SensorImpl.cpp
${SRCROOT}/Unix/SensorImpl.hpp
${SRCROOT}/Unix/VideoModeImpl.cpp
${SRCROOT}/Unix/WindowImplX11.cpp
${SRCROOT}/Unix/WindowImplX11.hpp
@ -103,6 +110,8 @@ elseif(SFML_OS_MACOSX)
${SRCROOT}/OSX/HIDJoystickManager.cpp
${SRCROOT}/OSX/JoystickImpl.cpp
${SRCROOT}/OSX/JoystickImpl.hpp
${SRCROOT}/OSX/SensorImpl.cpp
${SRCROOT}/OSX/SensorImpl.hpp
${SRCROOT}/OSX/SFApplication.h
${SRCROOT}/OSX/SFApplication.m
${SRCROOT}/OSX/SFApplicationDelegate.h
@ -135,6 +144,8 @@ elseif(SFML_OS_IOS)
${SRCROOT}/iOS/InputImpl.hpp
${SRCROOT}/iOS/JoystickImpl.mm
${SRCROOT}/iOS/JoystickImpl.hpp
${SRCROOT}/iOS/SensorImpl.mm
${SRCROOT}/iOS/SensorImpl.hpp
${SRCROOT}/iOS/VideoModeImpl.mm
${SRCROOT}/iOS/WindowImplUIKit.hpp
${SRCROOT}/iOS/WindowImplUIKit.mm
@ -157,7 +168,9 @@ elseif(SFML_OS_ANDROID)
${SRCROOT}/Android/InputImpl.hpp
${SRCROOT}/Android/InputImpl.cpp
${SRCROOT}/Android/JoystickImpl.hpp
${SRCROOT}/Android/JoystickImpl.cpp)
${SRCROOT}/Android/JoystickImpl.cpp
${SRCROOT}/Android/SensorImpl.hpp
${SRCROOT}/Android/SensorImpl.cpp)
source_group("android" FILES ${PLATFORM_SRC})
endif()

View file

@ -0,0 +1,109 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent.gom@gmail.com),
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORIMPLOSX_HPP
#define SFML_SENSORIMPLOSX_HPP
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Mac OS X implementation of sensors
///
////////////////////////////////////////////////////////////
class SensorImpl
{
public :
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the sensor module
///
////////////////////////////////////////////////////////////
static void initialize();
////////////////////////////////////////////////////////////
/// \brief Perform the global cleanup of the sensor module
///
////////////////////////////////////////////////////////////
static void cleanup();
////////////////////////////////////////////////////////////
/// \brief Initialize the sensor
///
/// \param type Index assigned to the sensor
///
/// \return The sensor capabilities
///
////////////////////////////////////////////////////////////
SensorCaps& initialize(unsigned int type);
////////////////////////////////////////////////////////////
/// \brief Close the sensor
///
////////////////////////////////////////////////////////////
void terminate();
////////////////////////////////////////////////////////////
/// \brief Update the sensor and get its new state
///
/// \return Sensor state
///
////////////////////////////////////////////////////////////
SensorState& update();
////////////////////////////////////////////////////////////
/// \brief Check if the sensor is enabled
///
/// \return True if the sensor is enabled, false otherwise
///
////////////////////////////////////////////////////////////
bool isEnable();
////////////////////////////////////////////////////////////
/// \brief Enable or disable the sensor
///
/// \param enable True to enable, false to disable
///
////////////////////////////////////////////////////////////
void setEnable(bool enable);
////////////////////////////////////////////////////////////
/// \brief Set the refresh rate of the sensor
///
/// \param rate Delay between each refresh
///
////////////////////////////////////////////////////////////
void setRefreshRate(const Time& rate);
};
} // namespace priv
} // namespace sf
#endif // SFML_SENSORIMPLOSX_HPP

View file

@ -0,0 +1,93 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent.gom@gmail.com),
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/SensorImpl.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void SensorImpl::initialize()
{
// Not applicable
}
////////////////////////////////////////////////////////////
void SensorImpl::cleanup()
{
// Not applicable
}
////////////////////////////////////////////////////////////
SensorCaps& SensorImpl::initialize(unsigned int type)
{
// Not applicable
SensorCaps capabilities;
capabilities.available = false;
return capabilities;
}
////////////////////////////////////////////////////////////
void SensorImpl::terminate()
{
// Not applicable
}
////////////////////////////////////////////////////////////
SensorState& SensorImpl::update()
{
// Not applicable
static SensorState state;
return state;
}
////////////////////////////////////////////////////////////
bool SensorImpl::isEnable()
{
// Not applicable
return false;
}
////////////////////////////////////////////////////////////
void SensorImpl::setEnable(bool enable)
{
// Not applicable
}
////////////////////////////////////////////////////////////
void SensorImpl::setRefreshRate(const Time& rate)
{
// Not applicable
}
} // namespace priv
} // namespace sf

View file

@ -0,0 +1,83 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Sensor.hpp>
#include <SFML/Window/SensorManager.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
bool Sensor::isAvailable(Type sensor)
{
return priv::SensorManager::getInstance().getCapabilities(sensor).available;
}
////////////////////////////////////////////////////////////
float Sensor::getResolution(Type sensor)
{
return priv::SensorManager::getInstance().getCapabilities(sensor).resolution;
}
////////////////////////////////////////////////////////////
Vector2f Sensor::getMaximumRange(Type sensor)
{
return priv::SensorManager::getInstance().getCapabilities(sensor).maximumRange;
}
////////////////////////////////////////////////////////////
Time Sensor::getMinimumDelay(Type sensor)
{
return priv::SensorManager::getInstance().getCapabilities(sensor).minimumDelay;
}
////////////////////////////////////////////////////////////
bool Sensor::isEnable(Type sensor)
{
return priv::SensorManager::getInstance().isEnable(sensor);
}
////////////////////////////////////////////////////////////
void Sensor::setEnable(Type sensor, bool enable)
{
return priv::SensorManager::getInstance().setEnable(sensor, enable);
}
////////////////////////////////////////////////////////////
void Sensor::setRefreshRate(Type sensor, const Time& rate)
{
priv::SensorManager::getInstance().setRefreshRate(sensor, rate);
}
////////////////////////////////////////////////////////////
Sensor::Data Sensor::getData(Type sensor)
{
return priv::SensorManager::getInstance().getState(sensor).pendingData->back();
}
} // namespace sf

View file

@ -0,0 +1,100 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORIMPL_HPP
#define SFML_SENSORIMPL_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/Window/Sensor.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Vector2.hpp>
#include <queue>
#include <cstddef>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Structure holding a sensor's capabilities
///
////////////////////////////////////////////////////////////
struct SensorCaps
{
SensorCaps()
{
available = false;
resolution = 0;
maximumRange = Vector2f(0, 0);
minimumDelay = Time::Zero;
}
bool available; ///< Is the sensor available on the underlying platform
float resolution; ///< How sensible the sensor is in the sensor's unit
Vector2f maximumRange; ///< Maximum range of the sensor in the sensor's unit
Time minimumDelay; ///< Minimum delay allowed between two events
};
////////////////////////////////////////////////////////////
/// \brief Structure holding a sensor's state
///
////////////////////////////////////////////////////////////
struct SensorState
{
SensorState()
{
pendingData = NULL;
enabled = false;
refreshRate = Time::Zero;
}
std::queue<Sensor::Data>* pendingData; ///< Pending sensor data
bool enabled; ///< Is the sensor currently enabled?
Time refreshRate; ///< Delay between two events
};
} // namespace priv
} // namespace sf
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/Window/Win32/SensorImpl.hpp>
#elif defined(SFML_SYSTEM_LINUX)
#include <SFML/Window/Unix/SensorImpl.hpp>
#elif defined(SFML_SYSTEM_MACOS)
#include <SFML/Window/OSX/SensorImpl.hpp>
#elif defined(SFML_SYSTEM_IOS)
#include <SFML/Window/iOS/SensorImpl.hpp>
#elif defined(SFML_SYSTEM_ANDROID)
#include <SFML/Window/Android/SensorImpl.hpp>
#endif
#endif // SFML_SENSORIMPL_HPP

View file

@ -0,0 +1,150 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/SensorManager.hpp>
#include <SFML/System/Err.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
SensorManager& SensorManager::getInstance()
{
static SensorManager instance;
return instance;
}
////////////////////////////////////////////////////////////
const SensorCaps& SensorManager::getCapabilities(unsigned int sensor) const
{
return m_sensors[sensor].capabilities;
}
////////////////////////////////////////////////////////////
const SensorState& SensorManager::getState(unsigned int sensor) const
{
return m_sensors[sensor].state;
}
////////////////////////////////////////////////////////////
void SensorManager::update()
{
for (int i = 0; i < Sensor::Count; ++i)
{
Item& item = m_sensors[i];
// Skip unavailable sensors
if (!item.capabilities.available)
continue;
// Get the current state of the sensor
item.state = item.sensor.update();
}
}
////////////////////////////////////////////////////////////
bool SensorManager::isEnable(unsigned int sensor)
{
if (!m_sensors[sensor].capabilities.available)
{
err() << "This sensor isn't available on your system (call Sensor::isAvailable to check it)" << std::endl;
return false;
}
return m_sensors[sensor].sensor.isEnable();
}
////////////////////////////////////////////////////////////
void SensorManager::setEnable(unsigned int sensor, bool enable)
{
if (!m_sensors[sensor].capabilities.available)
{
err() << "This sensor isn't available on your system (call Sensor::isAvailable to check it)" << std::endl;
return;
}
m_sensors[sensor].sensor.setEnable(enable);
}
////////////////////////////////////////////////////////////
void SensorManager::setRefreshRate(unsigned int sensor, const Time& rate)
{
if (!m_sensors[sensor].capabilities.available)
{
err() << "This sensor isn't available on your system (call Sensor::isAvailable to check it)" << std::endl;
return;
}
m_sensors[sensor].sensor.setRefreshRate(rate);
}
////////////////////////////////////////////////////////////
SensorManager::SensorManager()
{
// Global sensor initialization
SensorImpl::initialize();
// Per sensor initialization
for (int i = 0; i < Sensor::Count; ++i)
{
// Initialize the sensor and get its capabilities
m_sensors[i].capabilities = m_sensors[i].sensor.initialize(i);
// Available sensors are disabled by default
if (m_sensors[i].capabilities.available)
m_sensors[i].sensor.setEnable(false);
}
}
////////////////////////////////////////////////////////////
SensorManager::~SensorManager()
{
// Per sensor cleanup
for (int i = 0; i < Sensor::Count; ++i)
{
if (m_sensors[i].capabilities.available)
{
// Disable the sensor
m_sensors[i].sensor.setEnable(false);
// Terminate the sensor
m_sensors[i].sensor.terminate();
}
}
// Global sensor cleanup
SensorImpl::cleanup();
}
} // namespace priv
} // namespace sf

View file

@ -0,0 +1,146 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORMANAGER_HPP
#define SFML_SENSORMANAGER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Sensor.hpp>
#include <SFML/Window/SensorImpl.hpp>
#include <SFML/System/NonCopyable.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Global sensor manager
///
////////////////////////////////////////////////////////////
class SensorManager : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// \brief Get the global unique instance of the manager
///
/// \return Unique instance of the sensor manager
///
////////////////////////////////////////////////////////////
static SensorManager& getInstance();
////////////////////////////////////////////////////////////
/// \brief Get the capabilities of a sensor
///
/// \param sensor Index of the sensor
///
/// \return Capabilities of the sensor
///
////////////////////////////////////////////////////////////
const SensorCaps& getCapabilities(unsigned int sensor) const;
////////////////////////////////////////////////////////////
/// \brief Get the current state of a sensor
///
/// \param sensor Index of the sensor
///
/// \return Current state of the sensor
///
////////////////////////////////////////////////////////////
const SensorState& getState(unsigned int sensor) const;
////////////////////////////////////////////////////////////
/// \brief Update the state of all the sensors
///
////////////////////////////////////////////////////////////
void update();
////////////////////////////////////////////////////////////
/// \brief Get the current status of a sensor
///
/// \param sensor Index of the sensor
///
/// \return True if the sensor is enabled, false otherwise
///
////////////////////////////////////////////////////////////
bool isEnable(unsigned int sensor);
////////////////////////////////////////////////////////////
/// \brief Enable or disable a sensor
///
/// \param sensor Index of the sensor
/// \param enable Whether it should be enabled or not
///
////////////////////////////////////////////////////////////
void setEnable(unsigned int sensor, bool enable);
////////////////////////////////////////////////////////////
/// \brief Set the refresh rate of a sensor
///
/// \param sensor Index of the sensor
/// \param rate Delay between each event
///
////////////////////////////////////////////////////////////
void setRefreshRate(unsigned int sensor, const Time& rate);
private:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
SensorManager();
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~SensorManager();
////////////////////////////////////////////////////////////
/// \brief Sensor information and state
///
////////////////////////////////////////////////////////////
struct Item
{
SensorImpl sensor; ///< Sensor implementation
SensorState state; ///< The current sensor state
SensorCaps capabilities; ///< The sensor capabilities
};
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Item m_sensors[Sensor::Count]; ///< Sensors information and state
};
} // namespace priv
} // namespace sf
#endif // SFML_SENSORMANAGER_HPP

View file

@ -0,0 +1,92 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/SensorImpl.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void SensorImpl::initialize()
{
// Not applicable
}
////////////////////////////////////////////////////////////
void SensorImpl::cleanup()
{
// Not applicable
}
////////////////////////////////////////////////////////////
SensorCaps& SensorImpl::initialize(unsigned int type)
{
// Not applicable
SensorCaps capabilities;
capabilities.available = false;
return capabilities;
}
////////////////////////////////////////////////////////////
void SensorImpl::terminate()
{
// Not applicable
}
////////////////////////////////////////////////////////////
SensorState& SensorImpl::update()
{
// Not applicable
static SensorState state;
return state;
}
////////////////////////////////////////////////////////////
bool SensorImpl::isEnable()
{
// Not applicable
return false;
}
////////////////////////////////////////////////////////////
void SensorImpl::setEnable(bool enable)
{
// Not applicable
}
////////////////////////////////////////////////////////////
void SensorImpl::setRefreshRate(const Time& rate)
{
// Not applicable
}
} // namespace priv
} // namespace sf

View file

@ -0,0 +1,108 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORIMPLLINUX_HPP
#define SFML_SENSORIMPLLINUX_HPP
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Linux implementation of sensors
///
////////////////////////////////////////////////////////////
class SensorImpl
{
public :
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the sensor module
///
////////////////////////////////////////////////////////////
static void initialize();
////////////////////////////////////////////////////////////
/// \brief Perform the global cleanup of the sensor module
///
////////////////////////////////////////////////////////////
static void cleanup();
////////////////////////////////////////////////////////////
/// \brief Initialize the sensor
///
/// \param type Index assigned to the sensor
///
/// \return The sensor capabilities
///
////////////////////////////////////////////////////////////
SensorCaps& initialize(unsigned int type);
////////////////////////////////////////////////////////////
/// \brief Close the sensor
///
////////////////////////////////////////////////////////////
void terminate();
////////////////////////////////////////////////////////////
/// \brief Update the sensor and get its new state
///
/// \return Sensor state
///
////////////////////////////////////////////////////////////
SensorState& update();
////////////////////////////////////////////////////////////
/// \brief Check if the sensor is enabled
///
/// \return True if the sensor is enabled, false otherwise
///
////////////////////////////////////////////////////////////
bool isEnable();
////////////////////////////////////////////////////////////
/// \brief Enable or disable the sensor
///
/// \param enable True to enable, false to disable
///
////////////////////////////////////////////////////////////
void setEnable(bool enable);
////////////////////////////////////////////////////////////
/// \brief Set the refresh rate of the sensor
///
/// \param rate Delay between each refresh
///
////////////////////////////////////////////////////////////
void setRefreshRate(const Time& rate);
};
} // namespace priv
} // namespace sf
#endif // SFML_SENSORIMPLLINUX_HPP

View file

@ -0,0 +1,92 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/SensorImpl.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void SensorImpl::initialize()
{
// Not applicable
}
////////////////////////////////////////////////////////////
void SensorImpl::cleanup()
{
// Not applicable
}
////////////////////////////////////////////////////////////
SensorCaps& SensorImpl::initialize(unsigned int type)
{
// Not applicable
SensorCaps capabilities;
capabilities.available = false;
return capabilities;
}
////////////////////////////////////////////////////////////
void SensorImpl::terminate()
{
// Not applicable
}
////////////////////////////////////////////////////////////
SensorState& SensorImpl::update()
{
// Not applicable
static SensorState state;
return state;
}
////////////////////////////////////////////////////////////
bool SensorImpl::isEnable()
{
// Not applicable
return false;
}
////////////////////////////////////////////////////////////
void SensorImpl::setEnable(bool enable)
{
// Not applicable
}
////////////////////////////////////////////////////////////
void SensorImpl::setRefreshRate(const Time& rate)
{
// Not applicable
}
} // namespace priv
} // namespace sf

View file

@ -0,0 +1,108 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORIMPLWIN32_HPP
#define SFML_SENSORIMPLWIN32_HPP
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Windows implementation of sensors
///
////////////////////////////////////////////////////////////
class SensorImpl
{
public :
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the sensor module
///
////////////////////////////////////////////////////////////
static void initialize();
////////////////////////////////////////////////////////////
/// \brief Perform the global cleanup of the sensor module
///
////////////////////////////////////////////////////////////
static void cleanup();
////////////////////////////////////////////////////////////
/// \brief Initialize the sensor
///
/// \param type Index assigned to the sensor
///
/// \return The sensor capabilities
///
////////////////////////////////////////////////////////////
SensorCaps& initialize(unsigned int type);
////////////////////////////////////////////////////////////
/// \brief Close the sensor
///
////////////////////////////////////////////////////////////
void terminate();
////////////////////////////////////////////////////////////
/// \brief Update the sensor and get its new state
///
/// \return Sensor state
///
////////////////////////////////////////////////////////////
SensorState& update();
////////////////////////////////////////////////////////////
/// \brief Check if the sensor is enabled
///
/// \return True if the sensor is enabled, false otherwise
///
////////////////////////////////////////////////////////////
bool isEnable();
////////////////////////////////////////////////////////////
/// \brief Enable or disable the sensor
///
/// \param enable True to enable, false to disable
///
////////////////////////////////////////////////////////////
void setEnable(bool enable);
////////////////////////////////////////////////////////////
/// \brief Set the refresh rate of the sensor
///
/// \param rate Delay between each refresh
///
////////////////////////////////////////////////////////////
void setRefreshRate(const Time& rate);
};
} // namespace priv
} // namespace sf
#endif // SFML_SENSORIMPLWIN32_HPP

View file

@ -28,6 +28,7 @@
#include <SFML/Window/WindowImpl.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/JoystickManager.hpp>
#include <SFML/Window/SensorManager.hpp>
#include <SFML/System/Sleep.hpp>
#include <algorithm>
#include <cmath>
@ -86,6 +87,11 @@ m_joyThreshold(0.1f)
JoystickManager::getInstance().update();
for (unsigned int i = 0; i < Joystick::Count; ++i)
m_joyStates[i] = JoystickManager::getInstance().getState(i);
// Get the initial sensor states
SensorManager::getInstance().update();
for (unsigned int i = 0; i < Sensor::Count; ++i)
m_senStates[i] = SensorManager::getInstance().getState(i);
}
@ -108,10 +114,11 @@ bool WindowImpl::popEvent(Event& event, bool block)
{
// If the event queue is empty, let's first check if new events are available from the OS
if (m_events.empty())
{
{
// Get events from the system
processJoystickEvents();
processEvents();
processSensorEvents();
processEvents();
// In blocking mode, we must process events until one is triggered
if (block)
@ -123,6 +130,7 @@ bool WindowImpl::popEvent(Event& event, bool block)
{
sleep(milliseconds(10));
processJoystickEvents();
processSensorEvents();
processEvents();
}
}
@ -213,6 +221,52 @@ void WindowImpl::processJoystickEvents()
}
////////////////////////////////////////////////////////////
void WindowImpl::processSensorEvents()
{
// First update sensor states
SensorManager::getInstance().update();
for (unsigned int i = 0; i < Sensor::Count; ++i)
{
// Copy the previous state of the sensor and get the new one
SensorState previousState = m_senStates[i];
m_senStates[i] = SensorManager::getInstance().getState(i);
// Check whether it's still enabled
bool enabled = m_senStates[i].enabled;
if (previousState.enabled ^ enabled)
{
Event event;
event.type = enabled ? Event::SensorEnabled : Event::SensorDisabled;
event.sensor.type = static_cast<Sensor::Type>(i);
pushEvent(event);
// This sensor has been disabled, we don't want these pending data
if (!enabled)
{
while (!m_senStates[i].pendingData->empty())
m_senStates[i].pendingData->pop();
}
}
if (enabled)
{
// Send pending sensor data events
while (!m_senStates[i].pendingData->empty())
{
Event event;
event.type = Event::SensorData;
event.sensor.type = static_cast<Sensor::Type>(i);
event.sensor.data = m_senStates[i].pendingData->front();
pushEvent(event);
m_senStates[i].pendingData->pop();
}
}
}
}
} // namespace priv
} // namespace sf

View file

@ -34,6 +34,8 @@
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Joystick.hpp>
#include <SFML/Window/JoystickImpl.hpp>
#include <SFML/Window/Sensor.hpp>
#include <SFML/Window/SensorImpl.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/WindowHandle.hpp>
#include <SFML/Window/ContextSettings.hpp>
@ -225,12 +227,19 @@ private :
///
////////////////////////////////////////////////////////////
void processJoystickEvents();
////////////////////////////////////////////////////////////
/// \brief Read the sensors state and generate the appropriate events
///
////////////////////////////////////////////////////////////
void processSensorEvents();
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::queue<Event> m_events; ///< Queue of available events
JoystickState m_joyStates[Joystick::Count]; ///< Previous state of the joysticks
SensorState m_senStates[Sensor::Count]; ///< Previous state of the sensors
float m_joyThreshold; ///< Joystick threshold (minimum motion for MOVE event to be generated)
};

View file

@ -0,0 +1,108 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SENSORIMPLIOS_HPP
#define SFML_SENSORIMPLIOS_HPP
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief iOS implementation of sensors
///
////////////////////////////////////////////////////////////
class SensorImpl
{
public :
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the sensor module
///
////////////////////////////////////////////////////////////
static void initialize();
////////////////////////////////////////////////////////////
/// \brief Perform the global cleanup of the sensor module
///
////////////////////////////////////////////////////////////
static void cleanup();
////////////////////////////////////////////////////////////
/// \brief Initialize the sensor
///
/// \param type Index assigned to the sensor
///
/// \return The sensor capabilities
///
////////////////////////////////////////////////////////////
SensorCaps& initialize(unsigned int type);
////////////////////////////////////////////////////////////
/// \brief Close the sensor
///
////////////////////////////////////////////////////////////
void terminate();
////////////////////////////////////////////////////////////
/// \brief Update the sensor and get its new state
///
/// \return Sensor state
///
////////////////////////////////////////////////////////////
SensorState& update();
////////////////////////////////////////////////////////////
/// \brief Check if the sensor is enabled
///
/// \return True if the sensor is enabled, false otherwise
///
////////////////////////////////////////////////////////////
bool isEnable();
////////////////////////////////////////////////////////////
/// \brief Enable or disable the sensor
///
/// \param enable True to enable, false to disable
///
////////////////////////////////////////////////////////////
void setEnable(bool enable);
////////////////////////////////////////////////////////////
/// \brief Set the refresh rate of the sensor
///
/// \param rate Delay between each refresh
///
////////////////////////////////////////////////////////////
void setRefreshRate(const Time& rate);
};
} // namespace priv
} // namespace sf
#endif // SFML_SENSORIMPLIOS_HPP

View file

@ -0,0 +1,92 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/SensorImpl.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void SensorImpl::initialize()
{
// To implement
}
////////////////////////////////////////////////////////////
void SensorImpl::cleanup()
{
// To implement
}
////////////////////////////////////////////////////////////
SensorCaps& SensorImpl::initialize(unsigned int type)
{
// To implement
SensorCaps capabilities;
capabilities.available = false;
return capabilities;
}
////////////////////////////////////////////////////////////
void SensorImpl::terminate()
{
// To implement
}
////////////////////////////////////////////////////////////
SensorState& SensorImpl::update()
{
// To implement
static SensorState state;
return state;
}
////////////////////////////////////////////////////////////
bool SensorImpl::isEnable()
{
// To implement
return false;
}
////////////////////////////////////////////////////////////
void SensorImpl::setEnable(bool enable)
{
// To implement
}
////////////////////////////////////////////////////////////
void SensorImpl::setRefreshRate(const Time& rate)
{
// To implement
}
} // namespace priv
} // namespace sf