Skip to content

Commit

Permalink
DInput: Support for up to 128 buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Florin9doi authored and refractionpcsx2 committed Jan 25, 2023
1 parent 15a38f5 commit 64fbaff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
20 changes: 9 additions & 11 deletions pcsx2/Frontend/DInputSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ bool DInputSource::Initialize(SettingsInterface& si, std::unique_lock<std::mutex
}

PFNDIRECTINPUT8CREATE create = reinterpret_cast<PFNDIRECTINPUT8CREATE>(GetProcAddress(m_dinput_module.get(), "DirectInput8Create"));
PFNGETDFDIJOYSTICK get_joystick_data_format = reinterpret_cast<PFNGETDFDIJOYSTICK>(GetProcAddress(m_dinput_module.get(), "GetdfDIJoystick"));
if (!create || !get_joystick_data_format)
if (!create)
{
Console.Error("Failed to get DInput function pointers.");
return false;
}

HRESULT hr =
create(GetModuleHandleA(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8W, reinterpret_cast<LPVOID*>(m_dinput.put()), nullptr);
m_joystick_data_format = get_joystick_data_format();
if (FAILED(hr) || !m_joystick_data_format)
if (FAILED(hr))
{
Console.Error("DirectInput8Create() failed: %08X", hr);
return false;
Expand Down Expand Up @@ -182,7 +180,7 @@ bool DInputSource::AddDevice(ControllerData& cd, const std::string& name)
Console.Warning("Failed to set exclusive mode for '%s'", name.c_str());
}

hr = cd.device->SetDataFormat(m_joystick_data_format);
hr = cd.device->SetDataFormat(&c_dfDIJoystick2);
if (FAILED(hr))
{
Console.Error("Failed to set data format for '%s'", name.c_str());
Expand All @@ -207,9 +205,9 @@ bool DInputSource::AddDevice(ControllerData& cd, const std::string& name)

cd.num_buttons = caps.dwButtons;

static constexpr const u32 axis_offsets[] = {offsetof(DIJOYSTATE, lX), offsetof(DIJOYSTATE, lY), offsetof(DIJOYSTATE, lZ),
offsetof(DIJOYSTATE, lRz), offsetof(DIJOYSTATE, lRx), offsetof(DIJOYSTATE, lRy), offsetof(DIJOYSTATE, rglSlider[0]),
offsetof(DIJOYSTATE, rglSlider[1])};
static constexpr const u32 axis_offsets[] = {offsetof(DIJOYSTATE2, lX), offsetof(DIJOYSTATE2, lY), offsetof(DIJOYSTATE2, lZ),
offsetof(DIJOYSTATE2, lRz), offsetof(DIJOYSTATE2, lRx), offsetof(DIJOYSTATE2, lRy), offsetof(DIJOYSTATE2, rglSlider[0]),
offsetof(DIJOYSTATE2, rglSlider[1])};
for (const u32 offset : axis_offsets)
{
// ask for 16 bits of axis range
Expand Down Expand Up @@ -253,7 +251,7 @@ void DInputSource::PollEvents()
if (cd.needs_poll)
cd.device->Poll();

DIJOYSTATE js;
DIJOYSTATE2 js;
HRESULT hr = cd.device->GetDeviceState(sizeof(js), &js);
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
{
Expand Down Expand Up @@ -419,10 +417,10 @@ std::string DInputSource::ConvertKeyToString(InputBindingKey key)
return ret;
}

void DInputSource::CheckForStateChanges(size_t index, const DIJOYSTATE& new_state)
void DInputSource::CheckForStateChanges(size_t index, const DIJOYSTATE2& new_state)
{
ControllerData& cd = m_controllers[index];
DIJOYSTATE& last_state = cd.last_state;
DIJOYSTATE2& last_state = cd.last_state;

for (size_t i = 0; i < cd.axis_offsets.size(); i++)
{
Expand Down
7 changes: 3 additions & 4 deletions pcsx2/Frontend/DInputSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DInputSource final : public InputSource

enum : u32
{
MAX_NUM_BUTTONS = 32,
MAX_NUM_BUTTONS = 128,
};

DInputSource();
Expand All @@ -65,7 +65,7 @@ class DInputSource final : public InputSource
struct ControllerData
{
wil::com_ptr_nothrow<IDirectInputDevice8W> device;
DIJOYSTATE last_state = {};
DIJOYSTATE2 last_state = {};
GUID guid = {};
std::vector<u32> axis_offsets;
u32 num_buttons = 0;
Expand All @@ -83,12 +83,11 @@ class DInputSource final : public InputSource

bool AddDevice(ControllerData& cd, const std::string& name);

void CheckForStateChanges(size_t index, const DIJOYSTATE& new_state);
void CheckForStateChanges(size_t index, const DIJOYSTATE2& new_state);

// Those must go first in the class so they are destroyed last
wil::unique_hmodule m_dinput_module;
wil::com_ptr_nothrow<IDirectInput8W> m_dinput;
LPCDIDATAFORMAT m_joystick_data_format{};
HWND m_toplevel_window = nullptr;

ControllerDataArray m_controllers;
Expand Down

0 comments on commit 64fbaff

Please sign in to comment.