Fixed right click not detected on OS X with trackpads, close #716

Now that `+[NSEvent pressedMouseButtons]` can be used, part of HIDInputManager becomes obsolete and is therefore removed.
This commit is contained in:
Marco Antognini 2014-10-25 13:20:07 +02:00 committed by Lukas Dürrenberger
parent ab4d4d7477
commit 8928baa5ce
3 changed files with 8 additions and 179 deletions

View File

@ -31,7 +31,6 @@
////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <Carbon/Carbon.h>
#include <IOKit/hid/IOHIDDevice.h>
@ -48,8 +47,8 @@ typedef std::vector<IOHIDElementRef> IOHIDElements;
////////////////////////////////////////////////////////////
/// \brief sf::priv::InputImpl helper
///
/// This class manage as a singleton instance the keyboard and mouse states.
/// It's only purpose is to help sf::priv::InputImpl class.
/// This class manage as a singleton instance the keyboard state.
/// Its purpose is to help sf::priv::InputImpl class.
///
////////////////////////////////////////////////////////////
class HIDInputManager : NonCopyable
@ -76,16 +75,6 @@ public:
////////////////////////////////////////////////////////////
bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
bool isMouseButtonPressed(Mouse::Button button);
public:
////////////////////////////////////////////////////////////
@ -152,14 +141,6 @@ private:
////////////////////////////////////////////////////////////
void initializeKeyboard();
////////////////////////////////////////////////////////////
/// \brief Initialize the mouse part of this class
///
/// If something went wrong freeUp is called
///
////////////////////////////////////////////////////////////
void initializeMouse();
////////////////////////////////////////////////////////////
/// \brief Load the given keyboard into m_keys
///
@ -171,17 +152,6 @@ private:
////////////////////////////////////////////////////////////
void loadKeyboard(IOHIDDeviceRef keyboard);
////////////////////////////////////////////////////////////
/// \brief Load the given mouse into m_buttons
///
/// If the given mouse has no button this function simply
/// returns. freeUp is _not_ called because this is not fatal.
///
/// \param mouse Mouse to load
///
////////////////////////////////////////////////////////////
void loadMouse(IOHIDDeviceRef mouse);
////////////////////////////////////////////////////////////
/// \brief Load the given key into m_keys
///
@ -192,16 +162,6 @@ private:
////////////////////////////////////////////////////////////
void loadKey(IOHIDElementRef key);
////////////////////////////////////////////////////////////
/// \brief Load the given button into m_buttons
///
/// freeUp is _not_ called by this function.
///
/// \param button Button to load
///
////////////////////////////////////////////////////////////
void loadButton(IOHIDElementRef button);
////////////////////////////////////////////////////////////
/// \brief Release all resources
///
@ -224,11 +184,11 @@ private:
CFSetRef copyDevices(UInt32 page, UInt32 usage);
////////////////////////////////////////////////////////////
/// \brief Check if a key / mouse button is pressed
/// \brief Check if a key is pressed
///
/// \param elements HID elements mapping to this key / mouse button
/// \param elements HID elements mapping to this key
///
/// \return True if the key / button is pressed, false otherwise
/// \return True if the key is pressed, false otherwise
///
/// \see isKeyPressed, isMouseButtonPressed
///
@ -258,7 +218,6 @@ private:
IOHIDManagerRef m_manager; ///< HID Manager
IOHIDElements m_keys[Keyboard::KeyCount]; ///< All the keys on any connected keyboard
IOHIDElements m_buttons[Mouse::ButtonCount];///< All the buttons on any connected mouse
////////////////////////////////////////////////////////////
/// m_keys' index corresponds to sf::Keyboard::Key enum.
@ -267,8 +226,6 @@ private:
/// with the same sf::Keyboard::Key then m_keys[XYZ] contains all these
/// HID keys.
///
/// m_buttons works the same way.
///
////////////////////////////////////////////////////////////
};

View File

@ -48,12 +48,6 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key)
return isPressed(m_keys[key]);
}
////////////////////////////////////////////////////////////
bool HIDInputManager::isMouseButtonPressed(Mouse::Button button)
{
return isPressed(m_buttons[button]);
}
////////////////////////////////////////////////////////////
long HIDInputManager::getLocationID(IOHIDDeviceRef device)
@ -138,17 +132,6 @@ m_manager(0)
// Initialize the keyboard
initializeKeyboard();
if (!m_isValid) {
return; // Something went wrong
}
// Initialize the mouse
initializeMouse();
if (!m_isValid) {
return; // Something went wrong
}
}
@ -193,41 +176,6 @@ void HIDInputManager::initializeKeyboard()
}
////////////////////////////////////////////////////////////
void HIDInputManager::initializeMouse()
{
////////////////////////////////////////////////////////////
// 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
CFSetRef mouses = copyDevices(kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse);
if (mouses == NULL)
{
freeUp();
return;
}
CFIndex mouseCount = CFSetGetCount(mouses); // >= 1 (asserted by copyDevices)
// Get an iterable array
CFTypeRef devicesArray[mouseCount];
CFSetGetValues(mouses, devicesArray);
for (CFIndex i = 0; i < mouseCount; ++i)
{
IOHIDDeviceRef mouse = (IOHIDDeviceRef)devicesArray[i];
loadMouse(mouse);
}
// Release unused stuff
CFRelease(mouses);
////////////////////////////////////////////////////////////
// At this point m_buttons is filled with as many IOHIDElementRef as possible
}
////////////////////////////////////////////////////////////
void HIDInputManager::loadKeyboard(IOHIDDeviceRef keyboard)
{
@ -267,45 +215,6 @@ void HIDInputManager::loadKeyboard(IOHIDDeviceRef keyboard)
}
////////////////////////////////////////////////////////////
void HIDInputManager::loadMouse(IOHIDDeviceRef mouse)
{
CFArrayRef buttons = IOHIDDeviceCopyMatchingElements(mouse,
NULL,
kIOHIDOptionsTypeNone);
if (buttons == NULL)
{
sf::err() << "We got a mouse without any buttons (1)" << std::endl;
return;
}
// How many elements are there?
CFIndex buttonCount = CFArrayGetCount(buttons);
if (buttonCount == 0)
{
sf::err() << "We got a mouse without any buttons (2)" << std::endl;
CFRelease(buttons);
return;
}
// Go through all connected elements.
for (CFIndex i = 0; i < buttonCount; ++i)
{
IOHIDElementRef aButton = (IOHIDElementRef) CFArrayGetValueAtIndex(buttons, i);
// Skip non-matching keys elements
if (IOHIDElementGetUsagePage(aButton) != kHIDPage_Button)
continue;
loadButton(aButton);
}
// Release unused stuff
CFRelease(buttons);
}
////////////////////////////////////////////////////////////
void HIDInputManager::loadKey(IOHIDElementRef key)
{
@ -397,37 +306,6 @@ void HIDInputManager::loadKey(IOHIDElementRef key)
}
////////////////////////////////////////////////////////////
void HIDInputManager::loadButton(IOHIDElementRef button)
{
// Identify the button
UInt32 usage = IOHIDElementGetUsage(button);
Mouse::Button dest = Mouse::ButtonCount;
// Extends kHIDUsage_Button_* enum with:
#define kHIDUsage_Button_5 0x05
switch (usage)
{
case kHIDUsage_Button_1: dest = Mouse::Left; break;
case kHIDUsage_Button_2: dest = Mouse::Right; break;
case kHIDUsage_Button_3: dest = Mouse::Middle; break;
case kHIDUsage_Button_4: dest = Mouse::XButton1; break;
case kHIDUsage_Button_5: dest = Mouse::XButton2; break;
default: dest = Mouse::ButtonCount; break;
}
if (dest != Mouse::ButtonCount)
{
// We know what kind of button it is!
m_buttons[dest].push_back(button);
// And don't forget to keep the reference alive for our usage
CFRetain(m_buttons[dest].back());
}
}
////////////////////////////////////////////////////////////
void HIDInputManager::freeUp()
{
@ -446,14 +324,6 @@ void HIDInputManager::freeUp()
m_keys[i].clear();
}
for (unsigned int i = 0; i < Mouse::ButtonCount; ++i)
{
for (IOHIDElements::iterator it = m_buttons[i].begin(); it != m_buttons[i].end(); ++it)
CFRelease(*it);
m_buttons[i].clear();
}
}

View File

@ -122,7 +122,9 @@ void InputImpl::setVirtualKeyboardVisible(bool /*visible*/)
////////////////////////////////////////////////////////////
bool InputImpl::isMouseButtonPressed(Mouse::Button button)
{
return HIDInputManager::getInstance().isMouseButtonPressed(button);
NSUInteger state = [NSEvent pressedMouseButtons];
NSUInteger flag = 1 << button;
return (state & flag) != 0;
}