From b213063d75852d292934cb588d04fe3c893af4f7 Mon Sep 17 00:00:00 2001 From: emoose Date: Wed, 8 Jul 2020 23:12:16 +0100 Subject: [PATCH] Allow button remapping via INI settings --- Xb2XInput/Xb2XInput.cpp | 5 +++ Xb2XInput/XboxController.cpp | 78 ++++++++++++++++++++++++++++++++++++ Xb2XInput/XboxController.hpp | 3 ++ dist/Xb2XInput.ini | 24 +++++++++++ 4 files changed, 110 insertions(+) diff --git a/Xb2XInput/Xb2XInput.cpp b/Xb2XInput/Xb2XInput.cpp index 622f69d..8094db5 100644 --- a/Xb2XInput/Xb2XInput.cpp +++ b/Xb2XInput/Xb2XInput.cpp @@ -387,6 +387,11 @@ std::unordered_map xinput_buttons = { "LEFT", XUSB_GAMEPAD_DPAD_LEFT }, { "RIGHT", XUSB_GAMEPAD_DPAD_RIGHT }, + { "DPADUP", XUSB_GAMEPAD_DPAD_UP }, + { "DPADDOWN", XUSB_GAMEPAD_DPAD_DOWN }, + { "DPADLEFT", XUSB_GAMEPAD_DPAD_LEFT }, + { "DPADRIGHT", XUSB_GAMEPAD_DPAD_RIGHT }, + { "A", XUSB_GAMEPAD_A }, { "B", XUSB_GAMEPAD_B }, { "X", XUSB_GAMEPAD_X }, diff --git a/Xb2XInput/XboxController.cpp b/Xb2XInput/XboxController.cpp index 70c2a06..5a82db8 100644 --- a/Xb2XInput/XboxController.cpp +++ b/Xb2XInput/XboxController.cpp @@ -60,6 +60,19 @@ extern int combo_guideButton; extern int combo_deadzoneIncrease; extern int combo_deadzoneDecrease; +// Hacky defines to help with remap stuff +#define XUSB_GAMEPAD_Start XUSB_GAMEPAD_START +#define XUSB_GAMEPAD_Back XUSB_GAMEPAD_BACK +#define XUSB_GAMEPAD_LS XUSB_GAMEPAD_LEFT_THUMB +#define XUSB_GAMEPAD_RS XUSB_GAMEPAD_RIGHT_THUMB +#define XUSB_GAMEPAD_White XUSB_GAMEPAD_LEFT_SHOULDER +#define XUSB_GAMEPAD_Black XUSB_GAMEPAD_RIGHT_SHOULDER + +#define XUSB_GAMEPAD_DpadUp XUSB_GAMEPAD_DPAD_UP +#define XUSB_GAMEPAD_DpadDown XUSB_GAMEPAD_DPAD_DOWN +#define XUSB_GAMEPAD_DpadLeft XUSB_GAMEPAD_DPAD_LEFT +#define XUSB_GAMEPAD_DpadRight XUSB_GAMEPAD_DPAD_RIGHT + void dbgprintf(const char* format, ...) { static char buffer[256]; @@ -296,6 +309,39 @@ XboxController::XboxController(libusb_device_handle* handle, uint8_t* usb_ports, deadzone_.bLeftTrigger = min(max(GetSettingInt("DeadzoneLeftTrigger", 0), 0), 0xFF); deadzone_.bRightTrigger = min(max(GetSettingInt("DeadzoneRightTrigger", 0), 0), 0xFF); + button_remap_.clear(); + if (GetSettingBool("RemapEnable", false)) + { + std::string remap; + + int ParseButtonCombination(const char* combo); +#define LoadMap(btn) \ + remap = GetSettingString("Remap" #btn, ""); \ + if (remap.length()) \ + { \ + int combo = ParseButtonCombination(remap.c_str()); \ + if (combo) \ + button_remap_[XUSB_GAMEPAD_##btn] = combo; \ + } + + LoadMap(A); + LoadMap(B); + LoadMap(X); + LoadMap(Y); + LoadMap(Start); + LoadMap(Back); + LoadMap(LS); + LoadMap(RS); + LoadMap(Black); + LoadMap(White); + LoadMap(DpadUp); + LoadMap(DpadDown); + LoadMap(DpadLeft); + LoadMap(DpadRight); + +#undef LoadMap + } + usb_product_ = usb_desc_.idProduct; usb_vendor_ = usb_desc_.idVendor; } @@ -464,6 +510,38 @@ bool XboxController::update() gamepad_.wButtons |= input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_WHITE] ? XUSB_GAMEPAD_LEFT_SHOULDER : 0; gamepad_.wButtons |= input_prev_.Gamepad.bAnalogButtons[OGXINPUT_GAMEPAD_BLACK] ? XUSB_GAMEPAD_RIGHT_SHOULDER : 0; + if (button_remap_.size()) + { + auto buttons = gamepad_.wButtons; + gamepad_.wButtons = 0; + + // TODO: could probably change this into a loop over XUSB_BUTTON enum instead of a macro? +#define LoadMap(btn) \ + if (buttons & XUSB_GAMEPAD_##btn) \ + { \ + auto remap = (int)XUSB_GAMEPAD_##btn; \ + if (button_remap_.count(XUSB_GAMEPAD_##btn)) \ + remap = button_remap_[XUSB_GAMEPAD_##btn]; \ + gamepad_.wButtons |= remap; \ + } + + LoadMap(A); + LoadMap(B); + LoadMap(X); + LoadMap(Y); + LoadMap(Start); + LoadMap(Back); + LoadMap(LS); + LoadMap(RS); + LoadMap(Black); + LoadMap(White); + LoadMap(DpadUp); + LoadMap(DpadDown); + LoadMap(DpadLeft); + LoadMap(DpadRight); +#undef LoadMap + } + // Secret Deadzone Adjustment Combinations: extern bool deadzoneCombinationEnabled; if(deadzoneCombinationEnabled){ diff --git a/Xb2XInput/XboxController.hpp b/Xb2XInput/XboxController.hpp index 21ce0f1..dd1fcb7 100644 --- a/Xb2XInput/XboxController.hpp +++ b/Xb2XInput/XboxController.hpp @@ -5,6 +5,7 @@ #include #include +#include // original xbox XINPUT definitions from https://github.com/paralin/hl2sdk/blob/master/common/xbox/xboxstubs.h @@ -113,6 +114,8 @@ class XboxController bool guide_enabled_ = false; bool vibration_enabled_ = false; + std::unordered_map button_remap_; + bool update(); int GetSettingInt(const std::string& setting, int default_val); diff --git a/dist/Xb2XInput.ini b/dist/Xb2XInput.ini index 22b8e9b..b727a05 100644 --- a/dist/Xb2XInput.ini +++ b/dist/Xb2XInput.ini @@ -40,6 +40,30 @@ DeadzoneRightStick=0 DeadzoneLeftTrigger=0 DeadzoneRightTrigger=0 +# RemapEnable (default false) +# Whether or not button remapping is enabled +RemapEnable = false + +# Remap* +# What combination to remap each button as +# Can be a single button or a combination (see Combinations section above) +# Only buttons can be remapped atm, won't work with sticks or triggers +RemapA = B +RemapB = A +RemapX = Y +RemapY = X +RemapStart = Back +RemapBack = Start +RemapLS = RS +RemapRS = LS +RemapWhite = RB +RemapBlack = LB +RemapDpadUp = DpadDown +RemapDpadDown = DpadUp +# 'Dpad' part of combination can be skipped if desired: +RemapDpadLeft = Right +RemapDpadRight = Left + [0738:4526] # This section configures controllers that use the VID/PID of 0738:4526 (as not all controllers may have unique serial numbers) EnableGuide=true