From 57afaefd2ccc5c2c6577f37994c5033af903435b Mon Sep 17 00:00:00 2001 From: David Madison Date: Sat, 27 Mar 2021 06:07:42 -0400 Subject: [PATCH] Check direct enum comparisons by index To not break support for users who used the "wrong" control enumeration for that controller, but had their program work because the "wrong" enum used the same ID. --- PS4Parser.cpp | 5 +++-- PS5Parser.cpp | 5 +++-- XBOXONE.cpp | 14 +++++++------- XBOXONESParser.cpp | 24 ++++++++++++------------ XBOXRECV.cpp | 14 +++++++------- XBOXUSB.cpp | 14 +++++++------- 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/PS4Parser.cpp b/PS4Parser.cpp index a8353ce5..aee04d2c 100644 --- a/PS4Parser.cpp +++ b/PS4Parser.cpp @@ -70,9 +70,10 @@ bool PS4Parser::getButtonClick(ButtonEnum b) { } uint8_t PS4Parser::getAnalogButton(ButtonEnum b) { - if (b == L2) // These are the only analog buttons on the controller + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + if (index == legacyButtonValues(L2)) // These are the only analog buttons on the controller return ps4Data.trigger[0]; - else if (b == R2) + else if (index == legacyButtonValues(R2)) return ps4Data.trigger[1]; return 0; } diff --git a/PS5Parser.cpp b/PS5Parser.cpp index b0c6bc0b..8d53997c 100644 --- a/PS5Parser.cpp +++ b/PS5Parser.cpp @@ -74,9 +74,10 @@ bool PS5Parser::getButtonClick(ButtonEnum b) { } uint8_t PS5Parser::getAnalogButton(ButtonEnum b) { - if (b == L2) // These are the only analog buttons on the controller + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + if (index == legacyButtonValues(L2)) // These are the only analog buttons on the controller return ps5Data.trigger[0]; - else if (b == R2) + else if (index == legacyButtonValues(R2)) return ps5Data.trigger[1]; return 0; } diff --git a/XBOXONE.cpp b/XBOXONE.cpp index 336618be..7345c734 100644 --- a/XBOXONE.cpp +++ b/XBOXONE.cpp @@ -378,29 +378,29 @@ void XBOXONE::readReport() { } uint16_t XBOXONE::getButtonPress(ButtonEnum b) { - if(b == L2) // These are analog buttons - return triggerValue[0]; - else if(b == R2) - return triggerValue[1]; const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) // These are analog buttons + return triggerValue[0]; + else if(index == legacyButtonValues(R2)) + return triggerValue[1]; return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[index]))); } bool XBOXONE::getButtonClick(ButtonEnum b) { - if(b == L2) { + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) { if(L2Clicked) { L2Clicked = false; return true; } return false; - } else if(b == R2) { + } else if(index == legacyButtonValues(R2)) { if(R2Clicked) { R2Clicked = false; return true; } return false; } - const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]); bool click = (ButtonClickState & button); ButtonClickState &= ~button; // Clear "click" event diff --git a/XBOXONESParser.cpp b/XBOXONESParser.cpp index 4c9c23e4..b92bbeb7 100644 --- a/XBOXONESParser.cpp +++ b/XBOXONESParser.cpp @@ -76,37 +76,37 @@ bool XBOXONESParser::checkDpad(ButtonEnum b) { } uint16_t XBOXONESParser::getButtonPress(ButtonEnum b) { - if (b == L2) - return xboxOneSData.trigger[0]; - else if (b == R2) - return xboxOneSData.trigger[1]; - else if (b <= LEFT) // Dpad - return checkDpad(b); - else if (b == XBOX) - return xboxButtonState; const int8_t index = getButtonIndex(b); if (index < 0) return 0; + if (index == legacyButtonValues(L2)) + return xboxOneSData.trigger[0]; + else if (index == legacyButtonValues(R2)) + return xboxOneSData.trigger[1]; + else if (index <= LEFT) // Dpad + return checkDpad(b); + else if (index == legacyButtonValues(XBOX)) + return xboxButtonState; return xboxOneSData.btn.val & (1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[index])); } bool XBOXONESParser::getButtonClick(ButtonEnum b) { - if(b == L2) { + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) { if(L2Clicked) { L2Clicked = false; return true; } return false; - } else if(b == R2) { + } else if(index == legacyButtonValues(R2)) { if(R2Clicked) { R2Clicked = false; return true; } return false; - } else if (b == XBOX) { + } else if (index == legacyButtonValues(XBOX)) { bool click = xboxbuttonClickState; xboxbuttonClickState = 0; // Clear "click" event return click; } - const int8_t index = getButtonIndex(b); if (index < 0) return 0; uint32_t mask = 1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[index]); bool click = buttonClickState.val & mask; buttonClickState.val &= ~mask; // Clear "click" event diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index d00c1fa2..1a7ac318 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -408,29 +408,29 @@ void XBOXRECV::printReport(uint8_t controller __attribute__((unused)), uint8_t n } uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { - if(b == L2) // These are analog buttons - return (uint8_t)(ButtonState[controller] >> 8); - else if(b == R2) - return (uint8_t)ButtonState[controller]; const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) // These are analog buttons + return (uint8_t)(ButtonState[controller] >> 8); + else if(index == legacyButtonValues(R2)) + return (uint8_t)ButtonState[controller]; return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16)); } bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) { - if(b == L2) { + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) { if(L2Clicked[controller]) { L2Clicked[controller] = false; return true; } return false; - } else if(b == R2) { + } else if(index == legacyButtonValues(R2)) { if(R2Clicked[controller]) { R2Clicked[controller] = false; return true; } return false; } - const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]); bool click = (ButtonClickState[controller] & button); ButtonClickState[controller] &= ~button; // clear "click" event diff --git a/XBOXUSB.cpp b/XBOXUSB.cpp index 3749a5ad..6b44299e 100644 --- a/XBOXUSB.cpp +++ b/XBOXUSB.cpp @@ -281,29 +281,29 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep } uint8_t XBOXUSB::getButtonPress(ButtonEnum b) { - if(b == L2) // These are analog buttons - return (uint8_t)(ButtonState >> 8); - else if(b == R2) - return (uint8_t)ButtonState; const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) // These are analog buttons + return (uint8_t)(ButtonState >> 8); + else if(index == legacyButtonValues(R2)) + return (uint8_t)ButtonState; return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16)); } bool XBOXUSB::getButtonClick(ButtonEnum b) { - if(b == L2) { + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + if(index == legacyButtonValues(L2)) { if(L2Clicked) { L2Clicked = false; return true; } return false; - } else if(b == R2) { + } else if(index == legacyButtonValues(R2)) { if(R2Clicked) { R2Clicked = false; return true; } return false; } - const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]); bool click = (ButtonClickState & button); ButtonClickState &= ~button; // clear "click" event