2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#include <IOKit/hid/IOHIDLib.h>
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/OSX/OSXJoystick.h"
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace OSX
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2011-01-30 05:46:19 +00:00
|
|
|
Joystick::Joystick(IOHIDDeviceRef device, std::string name, int index)
|
2010-04-27 07:33:33 +00:00
|
|
|
: m_device(device)
|
2011-01-30 05:46:19 +00:00
|
|
|
, m_device_name(name)
|
|
|
|
, m_index(index)
|
2014-01-28 23:11:51 +00:00
|
|
|
, m_ff_device(nullptr)
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
|
|
|
// Buttons
|
|
|
|
NSDictionary *buttonDict =
|
|
|
|
[NSDictionary dictionaryWithObjectsAndKeys:
|
2011-01-14 05:06:08 +00:00
|
|
|
[NSNumber numberWithInteger: kIOHIDElementTypeInput_Button],
|
2010-06-26 13:03:25 +00:00
|
|
|
@kIOHIDElementTypeKey,
|
2011-01-14 05:06:08 +00:00
|
|
|
[NSNumber numberWithInteger: kHIDPage_Button],
|
2010-06-26 13:03:25 +00:00
|
|
|
@kIOHIDElementUsagePageKey,
|
2010-04-27 07:33:33 +00:00
|
|
|
nil];
|
2010-06-26 13:03:25 +00:00
|
|
|
|
|
|
|
CFArrayRef buttons = IOHIDDeviceCopyMatchingElements(m_device,
|
|
|
|
(CFDictionaryRef)buttonDict, kIOHIDOptionsTypeNone);
|
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
if (buttons)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < CFArrayGetCount(buttons); i++)
|
|
|
|
{
|
2010-06-26 13:03:25 +00:00
|
|
|
IOHIDElementRef e =
|
|
|
|
(IOHIDElementRef)CFArrayGetValueAtIndex(buttons, i);
|
2014-03-09 20:14:26 +00:00
|
|
|
//DeviceElementDebugPrint(e, nullptr);
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
AddInput(new Button(e, m_device));
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
CFRelease(buttons);
|
|
|
|
}
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
// Axes
|
|
|
|
NSDictionary *axisDict =
|
|
|
|
[NSDictionary dictionaryWithObjectsAndKeys:
|
2011-01-14 05:06:08 +00:00
|
|
|
[NSNumber numberWithInteger: kIOHIDElementTypeInput_Misc],
|
2010-06-26 13:03:25 +00:00
|
|
|
@kIOHIDElementTypeKey,
|
2010-04-27 07:33:33 +00:00
|
|
|
nil];
|
2010-06-26 13:03:25 +00:00
|
|
|
|
|
|
|
CFArrayRef axes = IOHIDDeviceCopyMatchingElements(m_device,
|
|
|
|
(CFDictionaryRef)axisDict, kIOHIDOptionsTypeNone);
|
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
if (axes)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < CFArrayGetCount(axes); i++)
|
|
|
|
{
|
2010-06-26 13:03:25 +00:00
|
|
|
IOHIDElementRef e =
|
|
|
|
(IOHIDElementRef)CFArrayGetValueAtIndex(axes, i);
|
2014-03-09 20:14:26 +00:00
|
|
|
//DeviceElementDebugPrint(e, nullptr);
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2011-01-16 17:00:17 +00:00
|
|
|
if (IOHIDElementGetUsage(e) == kHIDUsage_GD_Hatswitch) {
|
2011-03-14 01:20:11 +00:00
|
|
|
AddInput(new Hat(e, m_device, Hat::up));
|
|
|
|
AddInput(new Hat(e, m_device, Hat::right));
|
|
|
|
AddInput(new Hat(e, m_device, Hat::down));
|
|
|
|
AddInput(new Hat(e, m_device, Hat::left));
|
2011-01-16 17:00:17 +00:00
|
|
|
} else {
|
2013-01-17 21:41:18 +00:00
|
|
|
AddAnalogInputs(new Axis(e, m_device, Axis::negative),
|
|
|
|
new Axis(e, m_device, Axis::positive));
|
2011-01-16 17:00:17 +00:00
|
|
|
}
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
CFRelease(axes);
|
|
|
|
}
|
2014-01-28 23:11:51 +00:00
|
|
|
|
|
|
|
// Force Feedback
|
|
|
|
FFCAPABILITIES ff_caps;
|
|
|
|
if (SUCCEEDED(ForceFeedback::FFDeviceAdapter::Create(IOHIDDeviceGetService(m_device), &m_ff_device)) &&
|
|
|
|
SUCCEEDED(FFDeviceGetForceFeedbackCapabilities(m_ff_device->m_device, &ff_caps)))
|
|
|
|
{
|
|
|
|
InitForceFeedback(m_ff_device, ff_caps.numFfAxes);
|
|
|
|
}
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 23:11:51 +00:00
|
|
|
Joystick::~Joystick()
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
2014-01-28 23:11:51 +00:00
|
|
|
if (m_ff_device)
|
|
|
|
m_ff_device->Release();
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 23:11:51 +00:00
|
|
|
bool Joystick::UpdateInput()
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
std::string Joystick::GetName() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
|
|
|
return m_device_name;
|
|
|
|
}
|
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
std::string Joystick::GetSource() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
2011-01-30 05:46:19 +00:00
|
|
|
return "Input";
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
int Joystick::GetId() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
2011-01-30 05:46:19 +00:00
|
|
|
return m_index;
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Joystick::Button::GetState() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
|
|
|
IOHIDValueRef value;
|
2011-03-14 01:20:11 +00:00
|
|
|
if (IOHIDDeviceGetValue(m_device, m_element, &value) == kIOReturnSuccess)
|
2011-01-14 05:06:08 +00:00
|
|
|
return IOHIDValueGetIntegerValue(value);
|
|
|
|
else
|
|
|
|
return 0;
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
std::string Joystick::Button::GetName() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
s << IOHIDElementGetUsage(m_element);
|
|
|
|
return std::string("Button ") + s.str();
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
Joystick::Axis::Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction dir)
|
2010-04-27 07:33:33 +00:00
|
|
|
: m_element(element)
|
2011-03-14 01:20:11 +00:00
|
|
|
, m_device(device)
|
2010-04-27 07:33:33 +00:00
|
|
|
, m_direction(dir)
|
|
|
|
{
|
|
|
|
// Need to parse the element a bit first
|
|
|
|
std::string description("unk");
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2013-02-05 19:51:08 +00:00
|
|
|
int const usage = IOHIDElementGetUsage(m_element);
|
|
|
|
switch (usage)
|
|
|
|
{
|
2010-06-26 13:03:25 +00:00
|
|
|
case kHIDUsage_GD_X:
|
|
|
|
description = "X";
|
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Y:
|
|
|
|
description = "Y";
|
|
|
|
break;
|
2011-01-14 05:06:08 +00:00
|
|
|
case kHIDUsage_GD_Z:
|
|
|
|
description = "Z";
|
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Rx:
|
|
|
|
description = "Rx";
|
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Ry:
|
|
|
|
description = "Ry";
|
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Rz:
|
|
|
|
description = "Rz";
|
|
|
|
break;
|
2010-06-26 13:03:25 +00:00
|
|
|
case kHIDUsage_GD_Wheel:
|
|
|
|
description = "Wheel";
|
|
|
|
break;
|
|
|
|
case kHIDUsage_Csmr_ACPan:
|
|
|
|
description = "Pan";
|
|
|
|
break;
|
2013-02-05 19:51:08 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << usage;
|
|
|
|
description = s.str();
|
|
|
|
break;
|
|
|
|
}
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
m_name = std::string("Axis ") + description;
|
2010-04-27 15:22:23 +00:00
|
|
|
m_name.append((m_direction == positive) ? "+" : "-");
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2011-01-15 21:16:13 +00:00
|
|
|
m_neutral = (IOHIDElementGetLogicalMax(m_element) +
|
2011-01-14 05:06:08 +00:00
|
|
|
IOHIDElementGetLogicalMin(m_element)) / 2.;
|
2011-01-15 21:16:13 +00:00
|
|
|
m_scale = 1 / fabs(IOHIDElementGetLogicalMax(m_element) - m_neutral);
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Joystick::Axis::GetState() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
|
|
|
IOHIDValueRef value;
|
2011-01-14 05:06:08 +00:00
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
if (IOHIDDeviceGetValue(m_device, m_element, &value) == kIOReturnSuccess)
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
2011-02-27 00:15:26 +00:00
|
|
|
// IOHIDValueGetIntegerValue() crashes when trying
|
|
|
|
// to convert unusually large element values.
|
|
|
|
if (IOHIDValueGetLength(value) > 2)
|
|
|
|
return 0;
|
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
float position = IOHIDValueGetIntegerValue(value);
|
2010-06-26 13:03:25 +00:00
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
if (m_direction == positive && position > m_neutral)
|
2011-01-15 21:16:13 +00:00
|
|
|
return (position - m_neutral) * m_scale;
|
2011-01-14 05:06:08 +00:00
|
|
|
if (m_direction == negative && position < m_neutral)
|
2011-01-15 21:16:13 +00:00
|
|
|
return (m_neutral - position) * m_scale;
|
2011-01-16 17:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-04-27 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 05:06:08 +00:00
|
|
|
std::string Joystick::Axis::GetName() const
|
2010-04-27 07:33:33 +00:00
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
Joystick::Hat::Hat(IOHIDElementRef element, IOHIDDeviceRef device, direction dir)
|
2011-01-16 17:00:17 +00:00
|
|
|
: m_element(element)
|
2011-03-14 01:20:11 +00:00
|
|
|
, m_device(device)
|
2011-01-16 17:00:17 +00:00
|
|
|
, m_direction(dir)
|
|
|
|
{
|
|
|
|
switch (dir) {
|
|
|
|
case up:
|
|
|
|
m_name = "Up";
|
|
|
|
break;
|
|
|
|
case right:
|
|
|
|
m_name = "Right";
|
|
|
|
break;
|
|
|
|
case down:
|
|
|
|
m_name = "Down";
|
|
|
|
break;
|
|
|
|
case left:
|
|
|
|
m_name = "Left";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_name = "unk";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Joystick::Hat::GetState() const
|
2011-01-16 17:00:17 +00:00
|
|
|
{
|
|
|
|
IOHIDValueRef value;
|
|
|
|
int position;
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
if (IOHIDDeviceGetValue(m_device, m_element, &value) == kIOReturnSuccess)
|
2011-01-16 17:00:17 +00:00
|
|
|
{
|
|
|
|
position = IOHIDValueGetIntegerValue(value);
|
|
|
|
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
|
|
|
if (m_direction == up)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (m_direction == up || m_direction == right)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (m_direction == right)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (m_direction == right || m_direction == down)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (m_direction == down)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (m_direction == down || m_direction == left)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (m_direction == left)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (m_direction == left || m_direction == up)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::Hat::GetName() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2010-04-27 07:33:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|