Add function to retrieve all available joystick buttons

This commit is contained in:
Roland Rossgotterer
2019-02-14 16:08:40 +01:00
committed by Mat Sutcliffe
parent 2503a55dae
commit e5c435f281
9 changed files with 97 additions and 30 deletions

View File

@@ -13,6 +13,7 @@
//! \file
#include "blackinput/joystick.h"
#include "blackmisc/input/joystickbutton.h"
#include <QHash>
#include <IOKit/hid/IOHIDManager.h>
@@ -35,12 +36,15 @@ namespace BlackInput
//! Initialize device
bool init(const IOHIDDeviceRef device);
//! Get all available device buttons
BlackMisc::Input::CJoystickButtonList getDeviceButtons() const;
//! Return the native IOHIDDeviceRef
IOHIDDeviceRef getNativeDevice() const { return m_deviceRef; }
signals:
//! Joystick button changed
void buttonChanged(const QString &name, int index, bool isPressed);
void buttonChanged(const BlackMisc::Input::CJoystickButton &joystickButton, bool isPressed);
private:
friend bool operator == (const CJoystickDevice &lhs, const CJoystickDevice &rhs);
@@ -55,7 +59,7 @@ namespace BlackInput
QString m_deviceName = "unknown"; //!< Device name
// IOHIDDeviceRef is owned by IOHIDManager. Do not release it.
IOHIDDeviceRef m_deviceRef = nullptr;
QHash<IOHIDElementRef, int> m_joystickDeviceInputs;
QHash<IOHIDElementRef, BlackMisc::Input::CJoystickButton> m_joystickDeviceInputs;
};
//! MacOS implemenation of IJoystick
@@ -73,6 +77,9 @@ namespace BlackInput
//! Destructor
virtual ~CJoystickMacOS() override;
//! \copydoc BlackInput::IJoystick::getAllAvailableJoystickButtons()
virtual BlackMisc::Input::CJoystickButtonList getAllAvailableJoystickButtons() const override;
protected:
virtual bool init() override;
@@ -88,7 +95,7 @@ namespace BlackInput
//! Remove joystick device
void removeJoystickDevice(const IOHIDDeviceRef device);
void joystickButtonChanged(const QString &name, int index, bool isPressed);
void joystickButtonChanged(const BlackMisc::Input::CJoystickButton &joystickButton, bool isPressed);
static void matchCallback(void* context, IOReturn result, void* sender, IOHIDDeviceRef device);
static void removeCallback(void* context, IOReturn result, void* sender, IOHIDDeviceRef device);

View File

@@ -17,6 +17,7 @@
#include <array>
using namespace BlackMisc;
using namespace BlackMisc::Input;
namespace BlackInput
{
@@ -52,8 +53,8 @@ namespace BlackInput
{
CLogMessage(static_cast<CJoystickMacOS *>(nullptr)).debug() << "Found joystick button " << m_joystickDeviceInputs.size();
int size = m_joystickDeviceInputs.size();
m_joystickDeviceInputs.insert(elementRef, size);
int number = m_joystickDeviceInputs.size();
m_joystickDeviceInputs.insert(elementRef, { m_deviceName, number });
IOHIDDeviceRegisterInputValueCallback(device, valueCallback, this);
}
}
@@ -66,15 +67,20 @@ namespace BlackInput
return true;
}
CJoystickButtonList CJoystickDevice::getDeviceButtons() const
{
return CSequence<CJoystickButton>(m_joystickDeviceInputs.values());
}
void CJoystickDevice::processButtonEvent(IOHIDValueRef value)
{
IOHIDElementRef element = IOHIDValueGetElement(value);
int buttonNumber = m_joystickDeviceInputs.value(element, -1);
if (buttonNumber != -1)
CJoystickButton joystickButton = m_joystickDeviceInputs.value(element);
if (joystickButton.isValid())
{
bool isPressed = IOHIDValueGetIntegerValue(value) == 1;
if (isPressed) { emit buttonChanged(m_deviceName, buttonNumber, true); }
else { emit buttonChanged(m_deviceName, buttonNumber, false); }
if (isPressed) { emit buttonChanged(joystickButton, true); }
else { emit buttonChanged(joystickButton, false); }
}
}
@@ -104,6 +110,16 @@ namespace BlackInput
}
}
CJoystickButtonList CJoystickMacOS::getAllAvailableJoystickButtons() const
{
CJoystickButtonList availableButtons;
for (const CJoystickDevice *device : as_const(m_joystickDevices))
{
availableButtons.push_back(device->getDeviceButtons());
}
return availableButtons;
}
bool CJoystickMacOS::init()
{
m_hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
@@ -182,11 +198,11 @@ namespace BlackInput
}
}
void CJoystickMacOS::joystickButtonChanged(const QString &name, int index, bool isPressed)
void CJoystickMacOS::joystickButtonChanged(const CJoystickButton &joystickButton, bool isPressed)
{
BlackMisc::Input::CHotkeyCombination oldCombination(m_buttonCombination);
if (isPressed) { m_buttonCombination.addJoystickButton({name, index}); }
else { m_buttonCombination.removeJoystickButton({name, index}); }
CHotkeyCombination oldCombination(m_buttonCombination);
if (isPressed) { m_buttonCombination.addJoystickButton(joystickButton); }
else { m_buttonCombination.removeJoystickButton(joystickButton); }
if (oldCombination != m_buttonCombination)
{