Skip to content

Commit

Permalink
Allow button remapping via INI settings
Browse files Browse the repository at this point in the history
  • Loading branch information
emoose committed Jul 8, 2020
1 parent 88b3abd commit b213063
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Xb2XInput/Xb2XInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ std::unordered_map<std::string, int> 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 },
Expand Down
78 changes: 78 additions & 0 deletions Xb2XInput/XboxController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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){
Expand Down
3 changes: 3 additions & 0 deletions Xb2XInput/XboxController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <vector>
#include <mutex>
#include <unordered_map>

// original xbox XINPUT definitions from https://github.com/paralin/hl2sdk/blob/master/common/xbox/xboxstubs.h

Expand Down Expand Up @@ -113,6 +114,8 @@ class XboxController
bool guide_enabled_ = false;
bool vibration_enabled_ = false;

std::unordered_map<int, int> button_remap_;

bool update();

int GetSettingInt(const std::string& setting, int default_val);
Expand Down
24 changes: 24 additions & 0 deletions dist/Xb2XInput.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b213063

Please sign in to comment.