Refactoring of HIDInputManager

This commit is contained in:
Marco Antognini 2013-09-20 22:04:15 +02:00
parent d77f241938
commit 369b7faa1c
2 changed files with 61 additions and 79 deletions

View File

@ -42,6 +42,9 @@ namespace sf
{
namespace priv
{
typedef std::vector<IOHIDElementRef> IOHIDElements;
////////////////////////////////////////////////////////////
/// \brief This class manage as a singleton instance the
/// keyboard and mouse states. It's only purpose is
@ -219,6 +222,18 @@ private :
////////////////////////////////////////////////////////////
CFSetRef copyDevices(UInt32 page, UInt32 usage);
////////////////////////////////////////////////////////////
/// \brief Check if a key / mouse button is pressed
///
/// \param elements HID elements mapping to this key / mouse button
///
/// \return True if the key / button is pressed, false otherwise
///
/// \see isKeyPressed, isMouseButtonPressed
///
////////////////////////////////////////////////////////////
bool isPressed(IOHIDElements& elements);
////////////////////////////////////////////////////////////
/// \brief Convert a HID key usage to its corresponding virtual code
///
@ -241,7 +256,6 @@ private :
UCKeyboardLayout* m_layout; ///< Current Keyboard Layout
IOHIDManagerRef m_manager; ///< HID Manager
typedef std::vector<IOHIDElementRef> IOHIDElements;
IOHIDElements m_keys[Keyboard::KeyCount]; ///< All the keys on any connected keyboard
IOHIDElements m_buttons[Mouse::ButtonCount];///< All the buttons on any connected mouse

View File

@ -45,87 +45,13 @@ HIDInputManager& HIDInputManager::getInstance()
////////////////////////////////////////////////////////////
bool HIDInputManager::isKeyPressed(Keyboard::Key key)
{
if (!m_isValid) {
sf::err() << "HIDInputManager is invalid." << std::endl;
return false;
}
// state = true if at least one corresponding HID key is pressed
bool state = false;
for (IOHIDElements::iterator it = m_keys[key].begin(); it != m_keys[key].end();) {
IOHIDValueRef value = 0;
IOHIDDeviceRef device = IOHIDElementGetDevice(*it);
IOHIDDeviceGetValue(device, *it, &value);
if (!value) {
// This means some kind of error / deconnection so we remove this
// element from our keys
CFRelease(*it);
it = m_keys[key].erase(it);
} else if (IOHIDValueGetIntegerValue(value) == 1) {
// This means the key is pressed
state = true;
break; // Stop here
} else {
// This means the key is released
++it;
}
}
return state;
return isPressed(m_keys[key]);
}
////////////////////////////////////////////////////////////
bool HIDInputManager::isMouseButtonPressed(Mouse::Button button)
{
if (!m_isValid) {
sf::err() << "HIDInputManager is invalid." << std::endl;
return false;
}
// state = true if at least one corresponding HID button is pressed
bool state = false;
for (IOHIDElements::iterator it = m_buttons[button].begin(); it != m_buttons[button].end();) {
IOHIDValueRef value = 0;
IOHIDDeviceRef device = IOHIDElementGetDevice(*it);
IOHIDDeviceGetValue(device, *it, &value);
if (!value) {
// This means some kind of error / deconnection so we remove this
// element from our buttons
CFRelease(*it);
it = m_buttons[button].erase(it);
} else if (IOHIDValueGetIntegerValue(value) == 1) {
// This means the button is pressed
state = true;
break; // Stop here
} else {
// This means the button is released
++it
}
}
return state;
return isPressed(m_buttons[button]);
}
@ -237,7 +163,7 @@ HIDInputManager::~HIDInputManager()
void HIDInputManager::initializeKeyboard()
{
////////////////////////////////////////////////////////////
// The purpose of this function is to initalize m_keys so we can get
// The purpose of this function is to initialize m_keys so we can get
// the associate IOHIDElementRef with a sf::Keyboard::Key in ~constant~ time.
// Get only keyboards
@ -272,7 +198,7 @@ void HIDInputManager::initializeKeyboard()
void HIDInputManager::initializeMouse()
{
////////////////////////////////////////////////////////////
// The purpose of this function is to initalize m_buttons so we can get
// The purpose of this function is to initialize m_buttons so we can get
// the associate IOHIDElementRef with a sf::Mouse::Button in ~constant~ time.
// Get only mouses
@ -556,6 +482,48 @@ CFSetRef HIDInputManager::copyDevices(UInt32 page, UInt32 usage)
return devices;
}
bool HIDInputManager::isPressed(IOHIDElements& elements)
{
if (!m_isValid) {
sf::err() << "HIDInputManager is invalid." << std::endl;
return false;
}
// state = true if at least one corresponding HID button is pressed
bool state = false;
for (IOHIDElements::iterator it = elements.begin(); it != elements.end();) {
IOHIDValueRef value = 0;
IOHIDDeviceRef device = IOHIDElementGetDevice(*it);
IOHIDDeviceGetValue(device, *it, &value);
if (!value) {
// This means some kind of error / disconnection so we remove this
// element from our buttons
CFRelease(*it);
it = elements.erase(it);
} else if (IOHIDValueGetIntegerValue(value) == 1) {
// This means the button is pressed
state = true;
break; // Stop here
} else {
// This means the button is released
++it;
}
}
return state;
}
////////////////////////////////////////////////////////////
UInt8 HIDInputManager::usageToVirtualCode(UInt32 usage)