2010-04-02 02:48:24 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/XInput/XInput.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2014-04-17 11:03:44 +00:00
|
|
|
#ifndef XINPUT_GAMEPAD_GUIDE
|
|
|
|
#define XINPUT_GAMEPAD_GUIDE 0x0400
|
|
|
|
#endif
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace XInput
|
|
|
|
{
|
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
static const struct
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
const char* const name;
|
|
|
|
const WORD bitmask;
|
2013-10-29 05:23:17 +00:00
|
|
|
} named_buttons[] =
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
{ "Button A", XINPUT_GAMEPAD_A },
|
|
|
|
{ "Button B", XINPUT_GAMEPAD_B },
|
|
|
|
{ "Button X", XINPUT_GAMEPAD_X },
|
|
|
|
{ "Button Y", XINPUT_GAMEPAD_Y },
|
|
|
|
{ "Pad N", XINPUT_GAMEPAD_DPAD_UP },
|
|
|
|
{ "Pad S", XINPUT_GAMEPAD_DPAD_DOWN },
|
|
|
|
{ "Pad W", XINPUT_GAMEPAD_DPAD_LEFT },
|
|
|
|
{ "Pad E", XINPUT_GAMEPAD_DPAD_RIGHT },
|
|
|
|
{ "Start", XINPUT_GAMEPAD_START },
|
|
|
|
{ "Back", XINPUT_GAMEPAD_BACK },
|
|
|
|
{ "Shoulder L", XINPUT_GAMEPAD_LEFT_SHOULDER },
|
|
|
|
{ "Shoulder R", XINPUT_GAMEPAD_RIGHT_SHOULDER },
|
2014-04-17 11:03:44 +00:00
|
|
|
{ "Guide", XINPUT_GAMEPAD_GUIDE },
|
2010-04-02 02:48:24 +00:00
|
|
|
{ "Thumb L", XINPUT_GAMEPAD_LEFT_THUMB },
|
|
|
|
{ "Thumb R", XINPUT_GAMEPAD_RIGHT_THUMB }
|
|
|
|
};
|
|
|
|
|
2013-04-15 02:53:10 +00:00
|
|
|
static const char* const named_triggers[] =
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
"Trigger L",
|
|
|
|
"Trigger R"
|
|
|
|
};
|
|
|
|
|
2013-04-15 02:53:10 +00:00
|
|
|
static const char* const named_axes[] =
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
"Left X",
|
|
|
|
"Left Y",
|
|
|
|
"Right X",
|
|
|
|
"Right Y"
|
|
|
|
};
|
|
|
|
|
2013-04-15 02:53:10 +00:00
|
|
|
static const char* const named_motors[] =
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
"Motor L",
|
|
|
|
"Motor R"
|
|
|
|
};
|
|
|
|
|
2013-10-19 09:27:57 +00:00
|
|
|
static HMODULE hXInput = nullptr;
|
|
|
|
|
|
|
|
typedef decltype(&XInputGetCapabilities) XInputGetCapabilities_t;
|
|
|
|
typedef decltype(&XInputSetState) XInputSetState_t;
|
|
|
|
typedef decltype(&XInputGetState) XInputGetState_t;
|
|
|
|
|
|
|
|
static XInputGetCapabilities_t PXInputGetCapabilities = nullptr;
|
|
|
|
static XInputSetState_t PXInputSetState = nullptr;
|
|
|
|
static XInputGetState_t PXInputGetState = nullptr;
|
|
|
|
|
2014-04-17 11:03:44 +00:00
|
|
|
static bool haveGuideButton = false;
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
void Init(std::vector<Core::Device*>& devices)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2013-10-19 09:27:57 +00:00
|
|
|
if (!hXInput)
|
|
|
|
{
|
|
|
|
// Try for the most recent version we were compiled against (will only work if running on Win8+)
|
|
|
|
hXInput = ::LoadLibrary(XINPUT_DLL);
|
|
|
|
if (!hXInput)
|
|
|
|
{
|
|
|
|
// Drop back to DXSDK June 2010 version. Requires DX June 2010 redist.
|
|
|
|
hXInput = ::LoadLibrary(TEXT("xinput1_3.dll"));
|
|
|
|
if (!hXInput)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PXInputGetCapabilities = (XInputGetCapabilities_t)::GetProcAddress(hXInput, "XInputGetCapabilities");
|
|
|
|
PXInputSetState = (XInputSetState_t)::GetProcAddress(hXInput, "XInputSetState");
|
2014-04-17 11:03:44 +00:00
|
|
|
|
|
|
|
// Ordinal 100 is the same as XInputGetState, except it doesn't dummy out the guide
|
|
|
|
// button info. Try loading it and fall back if needed.
|
|
|
|
PXInputGetState = (XInputGetState_t)::GetProcAddress(hXInput, (LPCSTR)100);
|
|
|
|
if (PXInputGetState)
|
|
|
|
haveGuideButton = true;
|
|
|
|
else
|
|
|
|
PXInputGetState = (XInputGetState_t)::GetProcAddress(hXInput, "XInputGetState");
|
|
|
|
|
2013-10-19 09:27:57 +00:00
|
|
|
if (!PXInputGetCapabilities ||
|
|
|
|
!PXInputSetState ||
|
|
|
|
!PXInputGetState)
|
|
|
|
{
|
|
|
|
::FreeLibrary(hXInput);
|
|
|
|
hXInput = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-03-29 10:05:44 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
XINPUT_CAPABILITIES caps;
|
2011-03-14 01:20:11 +00:00
|
|
|
for (int i = 0; i != 4; ++i)
|
2013-10-19 09:27:57 +00:00
|
|
|
if (ERROR_SUCCESS == PXInputGetCapabilities(i, 0, &caps))
|
2011-03-14 01:20:11 +00:00
|
|
|
devices.push_back(new Device(caps, i));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-19 09:27:57 +00:00
|
|
|
void DeInit()
|
|
|
|
{
|
|
|
|
if (hXInput)
|
|
|
|
{
|
|
|
|
::FreeLibrary(hXInput);
|
|
|
|
hXInput = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
Device::Device(const XINPUT_CAPABILITIES& caps, u8 index)
|
|
|
|
: m_index(index), m_subtype(caps.SubType)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
ZeroMemory(&m_state_out, sizeof(m_state_out));
|
|
|
|
ZeroMemory(&m_current_state_out, sizeof(m_current_state_out));
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
// XInputGetCaps seems to always claim all capabilities are supported
|
2013-04-15 02:53:10 +00:00
|
|
|
// but I will leave all this stuff in, incase m$ fixes xinput up a bit
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
// get supported buttons
|
2011-03-14 01:20:11 +00:00
|
|
|
for (int i = 0; i != sizeof(named_buttons)/sizeof(*named_buttons); ++i)
|
2013-04-15 02:53:10 +00:00
|
|
|
{
|
2014-04-17 11:03:44 +00:00
|
|
|
// Guide button is never reported in caps
|
|
|
|
if ((named_buttons[i].bitmask & caps.Gamepad.wButtons) ||
|
|
|
|
((named_buttons[i].bitmask & XINPUT_GAMEPAD_GUIDE) && haveGuideButton))
|
2011-03-14 01:20:11 +00:00
|
|
|
AddInput(new Button(i, m_state_in.Gamepad.wButtons));
|
2013-04-15 02:53:10 +00:00
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
// get supported triggers
|
2011-03-14 01:20:11 +00:00
|
|
|
for (int i = 0; i != sizeof(named_triggers)/sizeof(*named_triggers); ++i)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2014-02-17 04:51:41 +00:00
|
|
|
//BYTE val = (&caps.Gamepad.bLeftTrigger)[i]; // should be max value / MSDN lies
|
2011-03-14 01:20:11 +00:00
|
|
|
if ((&caps.Gamepad.bLeftTrigger)[i])
|
|
|
|
AddInput(new Trigger(i, (&m_state_in.Gamepad.bLeftTrigger)[i], 255 ));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get supported axes
|
2011-03-14 01:20:11 +00:00
|
|
|
for (int i = 0; i != sizeof(named_axes)/sizeof(*named_axes); ++i)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2013-04-19 13:21:45 +00:00
|
|
|
//SHORT val = (&caps.Gamepad.sThumbLX)[i]; // xinput doesn't give the range / MSDN is a liar
|
2011-03-14 01:20:11 +00:00
|
|
|
if ((&caps.Gamepad.sThumbLX)[i])
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
const SHORT& ax = (&m_state_in.Gamepad.sThumbLX)[i];
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
// each axis gets a negative and a positive input instance associated with it
|
2011-03-14 01:20:11 +00:00
|
|
|
AddInput(new Axis(i, ax, -32768));
|
|
|
|
AddInput(new Axis(i, ax, 32767));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get supported motors
|
2011-03-14 01:20:11 +00:00
|
|
|
for (int i = 0; i != sizeof(named_motors)/sizeof(*named_motors); ++i)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2014-02-17 04:51:41 +00:00
|
|
|
//WORD val = (&caps.Vibration.wLeftMotorSpeed)[i]; // should be max value / nope, more lies
|
2011-03-14 01:20:11 +00:00
|
|
|
if ((&caps.Vibration.wLeftMotorSpeed)[i])
|
|
|
|
AddOutput(new Motor(i, (&m_state_out.wLeftMotorSpeed)[i], 65535));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClearInputState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::ClearInputState()
|
|
|
|
{
|
2013-10-29 05:23:17 +00:00
|
|
|
ZeroMemory(&m_state_in, sizeof(m_state_in));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Device::GetName() const
|
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
switch (m_subtype)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2013-10-19 09:27:57 +00:00
|
|
|
case XINPUT_DEVSUBTYPE_GAMEPAD: return "Gamepad"; break;
|
|
|
|
case XINPUT_DEVSUBTYPE_WHEEL: return "Wheel"; break;
|
|
|
|
case XINPUT_DEVSUBTYPE_ARCADE_STICK: return "Arcade Stick"; break;
|
|
|
|
case XINPUT_DEVSUBTYPE_FLIGHT_STICK: return "Flight Stick"; break;
|
|
|
|
case XINPUT_DEVSUBTYPE_DANCE_PAD: return "Dance Pad"; break;
|
|
|
|
case XINPUT_DEVSUBTYPE_GUITAR: return "Guitar"; break;
|
|
|
|
case XINPUT_DEVSUBTYPE_DRUM_KIT: return "Drum Kit"; break;
|
|
|
|
default: return "Device"; break;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Device::GetId() const
|
|
|
|
{
|
|
|
|
return m_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Device::GetSource() const
|
|
|
|
{
|
|
|
|
return "XInput";
|
|
|
|
}
|
|
|
|
|
2013-04-15 02:53:10 +00:00
|
|
|
// Update I/O
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
bool Device::UpdateInput()
|
|
|
|
{
|
2013-10-19 09:27:57 +00:00
|
|
|
return (ERROR_SUCCESS == PXInputGetState(m_index, &m_state_in));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Device::UpdateOutput()
|
|
|
|
{
|
2010-04-13 05:15:38 +00:00
|
|
|
// this if statement is to make rumble work better when multiple ControllerInterfaces are using the device
|
2010-04-22 07:11:49 +00:00
|
|
|
// only calls XInputSetState if the state changed
|
2011-03-14 01:20:11 +00:00
|
|
|
if (memcmp(&m_state_out, &m_current_state_out, sizeof(m_state_out)))
|
2010-04-13 05:15:38 +00:00
|
|
|
{
|
2010-04-22 07:11:49 +00:00
|
|
|
m_current_state_out = m_state_out;
|
2013-10-19 09:27:57 +00:00
|
|
|
return (ERROR_SUCCESS == PXInputSetState(m_index, &m_state_out));
|
2010-04-13 05:15:38 +00:00
|
|
|
}
|
|
|
|
else
|
2013-04-15 02:53:10 +00:00
|
|
|
{
|
2010-04-13 05:15:38 +00:00
|
|
|
return true;
|
2013-04-15 02:53:10 +00:00
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET name/source/id
|
|
|
|
|
|
|
|
std::string Device::Button::GetName() const
|
|
|
|
{
|
|
|
|
return named_buttons[m_index].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Device::Axis::GetName() const
|
|
|
|
{
|
2010-07-16 03:43:11 +00:00
|
|
|
return std::string(named_axes[m_index]) + (m_range<0 ? '-' : '+');
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Device::Trigger::GetName() const
|
|
|
|
{
|
|
|
|
return named_triggers[m_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Device::Motor::GetName() const
|
|
|
|
{
|
|
|
|
return named_motors[m_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET / SET STATES
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Device::Button::GetState() const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return (m_buttons & named_buttons[m_index].bitmask) > 0;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Device::Trigger::GetState() const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return ControlState(m_trigger) / m_range;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Device::Axis::GetState() const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return std::max( 0.0f, ControlState(m_axis) / m_range );
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
void Device::Motor::SetState(ControlState state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
m_motor = (WORD)(state * m_range);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|