From f1fb7383b7e3f542fdb9cf77c3fa2570562e9c3b Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 25 Mar 2021 13:50:13 +1000 Subject: [PATCH] DInputControllerInterface: Support diagonals in hat --- .../dinput_controller_interface.cpp | 41 +++++++++++-------- .../dinput_controller_interface.h | 2 +- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/frontend-common/dinput_controller_interface.cpp b/src/frontend-common/dinput_controller_interface.cpp index 292f04c13..5aed98cce 100644 --- a/src/frontend-common/dinput_controller_interface.cpp +++ b/src/frontend-common/dinput_controller_interface.cpp @@ -222,19 +222,24 @@ void DInputControllerInterface::PollEvents() } } -u32 DInputControllerInterface::GetHatDirection(DWORD hat) +std::array DInputControllerInterface::GetHatButtons(DWORD hat) { + std::array buttons = {}; + const WORD hv = LOWORD(hat); - if (hv == 0xFFFF) - return NUM_HAT_DIRECTIONS; - else if (hv < 9000) - return HAT_DIRECTION_UP; - else if (hv < 18000) - return HAT_DIRECTION_RIGHT; - else if (hv < 27000) - return HAT_DIRECTION_DOWN; - else - return HAT_DIRECTION_LEFT; + if (hv != 0xFFFF) + { + if ((hv >= 0 && hv < 9000) || hv >= 31500) + buttons[HAT_DIRECTION_UP] = true; + if (hv >= 4500 && hv < 18000) + buttons[HAT_DIRECTION_RIGHT] = true; + if (hv >= 13500 && hv < 27000) + buttons[HAT_DIRECTION_DOWN] = true; + if (hv >= 22500) + buttons[HAT_DIRECTION_LEFT] = true; + } + + return buttons; } void DInputControllerInterface::CheckForStateChanges(u32 index, const DIJOYSTATE& new_state) @@ -268,14 +273,16 @@ void DInputControllerInterface::CheckForStateChanges(u32 index, const DIJOYSTATE { if (last_state.rgdwPOV[0] != new_state.rgdwPOV[0]) { + Log_InfoPrintf("Hat %u", LOWORD(new_state.rgdwPOV[0])); // map hats to the last buttons - const u32 old_direction = GetHatDirection(last_state.rgdwPOV[0]); - if (old_direction != NUM_HAT_DIRECTIONS) - HandleButtonEvent(index, cd.num_buttons + old_direction, false); + const std::array old_buttons(GetHatButtons(last_state.rgdwPOV[0])); + const std::array new_buttons(GetHatButtons(new_state.rgdwPOV[0])); + for (u32 i = 0; i < NUM_HAT_DIRECTIONS; i++) + { + if (old_buttons[i] != new_buttons[i]) + HandleButtonEvent(index, cd.num_buttons + i, new_buttons[i]); + } - const u32 new_direction = GetHatDirection(new_state.rgdwPOV[0]); - if (new_direction != NUM_HAT_DIRECTIONS) - HandleButtonEvent(index, cd.num_buttons + new_direction, true); last_state.rgdwPOV[0] = new_state.rgdwPOV[0]; } } diff --git a/src/frontend-common/dinput_controller_interface.h b/src/frontend-common/dinput_controller_interface.h index fa8183b39..86c0e22b3 100644 --- a/src/frontend-common/dinput_controller_interface.h +++ b/src/frontend-common/dinput_controller_interface.h @@ -79,7 +79,7 @@ private: void AddDevices(); bool AddDevice(ControllerData& cd, const char* name); - static u32 GetHatDirection(DWORD hat); + static std::array GetHatButtons(DWORD hat); void CheckForStateChanges(u32 index, const DIJOYSTATE& new_state);