Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions runtime/include/runtime_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ struct RuntimeUserConfig {
// comma-separated SDL-style physical button names ("south", or
// "dpad_up,left_shoulder") as values; pressing either bound button counts.
std::array<std::optional<std::string>, 12> controllerButtons;
// Per-port overrides of controllerButtons ("a_1" .. "right_4", the suffix is port + 1).
// A port falls back to the shared controllerButtons entry while its override is unset, so
// configs written before per-port bindings keep applying to every port unchanged.
std::array<std::array<std::optional<std::string>, 12>, 4> controllerPortButtons;
std::optional<bool> rumbleEnabled;
// Shake a motion controller to press the D-pad, like shaking a Wii Remote.
std::optional<bool> motionTrick;
Expand Down Expand Up @@ -413,6 +417,21 @@ inline RuntimeUserConfig ParseConfigDocument(const toml::value& document) {
for (const auto& [key, value] : section->as_table()) {
if (key.rfind("expr_", 0) == 0 && value.is_string()) {
config.controllerExpressions[key] = value.as_string();
continue;
}
// "<button>_<port + 1>" overrides of the shared [controller] bindings. No button
// name contains an underscore, so the last underscore always splits off the port.
const auto split = key.find_last_of('_');
if (split == std::string::npos || split == 0 || split + 2 != key.size() ||
key[split + 1] < '1' || key[split + 1] > '4' || !value.is_string()) {
continue;
}
const std::string button(key.substr(0, split));
for (size_t index = 0; index < buttonKeys.size(); ++index) {
if (button == buttonKeys[index]) {
config.controllerPortButtons[key[split + 1] - '1'][index] = value.as_string();
break;
}
}
}
}
Expand Down Expand Up @@ -700,6 +719,39 @@ inline bool SetControllerButton(size_t index, std::string value) {
return WriteSetting("controller", kControllerButtonKeys[index], FormatString(value));
}

// Per-port bindings are stored as "[controller] <key>_<port + 1>" (a_1 .. right_4). The shared
// [controller] bindings remain the fallback for ports that were never rebound individually.
inline constexpr size_t kControllerPortCount = 4;

inline std::string PortControllerButtonKey(uint32_t port, size_t index) {
if (port >= kControllerPortCount || index >= kControllerButtonKeys.size()) {
return {};
}
return std::string(kControllerButtonKeys[index]) + "_" + std::to_string(port + 1);
}

// The port's own override when set, else the shared binding that every port used before
// per-port bindings existed.
inline const std::optional<std::string>& PortControllerButton(uint32_t port, size_t index) {
static const std::optional<std::string> empty;
if (port < kControllerPortCount && index < kControllerButtonKeys.size()) {
const auto& portValue = Get().controllerPortButtons[port][index];
if (portValue) {
return portValue;
}
}
return ControllerButton(index);
}

inline bool SetPortControllerButton(uint32_t port, size_t index, std::string value) {
const std::string key = PortControllerButtonKey(port, index);
if (key.empty()) {
return false;
}
Mutable().controllerPortButtons[port][index] = value;
return WriteSetting("controller", key, FormatString(value));
}

inline std::string ControllerExpression(const std::string& key) {
const auto it = Get().controllerExpressions.find(key);
return it == Get().controllerExpressions.end() ? std::string() : it->second;
Expand Down
27 changes: 16 additions & 11 deletions runtime/src/settings_overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ std::array<int32_t, PAD_MAX_CONTROLLERS> g_configuredControllerIndices = [] {
using ControllerNames::kNativeButtons;
using ControllerNames::NativeButtonItem;
constexpr const auto& kControllerButtons = ControllerNames::kGameCubeButtons;
// Per-port bindings persist as [controller] "<key>_<port + 1>"; keep the config side's port
// count honest against the pad layer's channel count.
static_assert(RuntimeConfigFile::kControllerPortCount == PAD_MAX_CONTROLLERS);

// Classic Controller Pro layout, indexed like kControllerButtons: the SNES-style
// diamond (A right, B bottom, X top, Y left) with digital bumpers driving the GC
Expand Down Expand Up @@ -261,11 +264,11 @@ void ApplyConfiguredMappings() {
if (controllerIndex < 0) {
continue;
}
// The [controller] bindings are positional and shared by every port, so
// they describe whatever pad the user set them up with (usually an Xbox
// layout: a = south). A Wii U Pro Controller has a fixed, known layout
// (A on the east position) that aurora already maps by name; applying
// the shared bindings on top swaps A/B and X/Y.
// The [controller] bindings are positional and describe whatever pad the
// user set them up with (usually an Xbox layout: a = south), so they are
// per-port overrides over a shared fallback. A Wii U Pro Controller has a
// fixed, known layout (A on the east position) that aurora already maps
// by name; applying configured bindings on top swaps A/B and X/Y.
if (const char* name = PADGetNameForControllerIndex(static_cast<uint32_t>(controllerIndex));
name != nullptr && std::string_view(name).find("Wii U Pro") != std::string_view::npos) {
continue;
Expand All @@ -276,7 +279,7 @@ void ApplyConfiguredMappings() {
continue;
}
for (size_t i = 0; i < kControllerButtons.size(); ++i) {
const auto& configured = RuntimeConfigFile::ControllerButton(i);
const auto& configured = RuntimeConfigFile::PortControllerButton(port, i);
if (!configured) {
continue;
}
Expand Down Expand Up @@ -500,7 +503,8 @@ void CompleteRebind(uint32_t value) {
std::string config = NativeBindingConfig(primaryValue);
if (alternateValue != PAD_NATIVE_BUTTON_INVALID) config += ',' + NativeBindingConfig(alternateValue);
for (size_t i = 0; i < kControllerButtons.size(); ++i)
if (kControllerButtons[i].padButton == capture.target) RuntimeConfigFile::SetControllerButton(i, config);
if (kControllerButtons[i].padButton == capture.target)
RuntimeConfigFile::SetPortControllerButton(capture.port, i, config);
} else if (capture.kind == RebindKind::MuteHotkey) {
g_muteHotkey = static_cast<int32_t>(value);
RuntimeConfigFile::SetMuteHotkey(g_muteHotkey);
Expand Down Expand Up @@ -819,13 +823,13 @@ void DrawGamepadMapping(uint32_t port) {
uint32_t altMappingCount = 0;
PADButtonMapping* altMappings = PADGetAltButtonMappings(port, &altMappingCount);

const auto writeBinding = [](size_t index, uint32_t primaryNative, uint32_t altNative) {
const auto writeBinding = [port](size_t index, uint32_t primaryNative, uint32_t altNative) {
std::string value = NativeBindingConfig(primaryNative);
if (altNative != PAD_NATIVE_BUTTON_INVALID) {
value += ',';
value += NativeBindingConfig(altNative);
}
RuntimeConfigFile::SetControllerButton(index, value);
RuntimeConfigFile::SetPortControllerButton(port, index, value);
};

// Which rows show the second-binding button without one being bound yet;
Expand All @@ -848,7 +852,7 @@ void DrawGamepadMapping(uint32_t port) {
return mapping.padButton == kControllerButtons[i].padButton;
});
if (it != restored + restoredCount) {
RuntimeConfigFile::SetControllerButton(i, NativeButtonForValue(it->nativeButton).configName);
RuntimeConfigFile::SetPortControllerButton(port, i, NativeButtonForValue(it->nativeButton).configName);
}
}
}
Expand All @@ -862,7 +866,7 @@ void DrawGamepadMapping(uint32_t port) {
PADSetButtonMapping(port, PADButtonMapping{native->nativeButton, kControllerButtons[i].padButton});
PADSetAltButtonMapping(port,
PADButtonMapping{PAD_NATIVE_BUTTON_INVALID, kControllerButtons[i].padButton});
RuntimeConfigFile::SetControllerButton(i, preset[i]);
RuntimeConfigFile::SetPortControllerButton(port, i, preset[i]);
}
}
altRowExpanded.fill(false);
Expand All @@ -879,6 +883,7 @@ void DrawGamepadMapping(uint32_t port) {
}

ImGui::TextDisabled("Click a binding, then press the input to use. Triggers: LT/L2, RT/R2.");
ImGui::TextDisabled("Bindings are saved for this port; ports never rebound use the shared mapping.");
const float bindingWidth = ImGui::CalcTextSize("Right shoulder (RB / R1)").x +
ImGui::GetFrameHeight() + ImGui::GetStyle().FramePadding.x * 2.0f;
const float bindingX = ImGui::GetCursorPosX() + ImGui::CalcTextSize("D-pad Right").x +
Expand Down
28 changes: 28 additions & 0 deletions runtime/tests/runtime_config_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,33 @@ int main() {
document->at("video").at("advanced").at("foo").as_integer() == 1,
"key was not added to an implicit table")) return 1;
}
{
std::istringstream input("[controller]\na_1 = \"south\"\nright_4 = \"east\"\nl_2 = \"west\"\n"
"a_0 = \"north\"\na_5 = \"north\"\nnotabutton_1 = \"south\"\n"
"a_x = \"south\"\na_3 = 1\n");
const RuntimeUserConfig config = RuntimeConfigFile::ParseConfig(input, "ports.toml");
if (!Require(config.controllerPortButtons[0][0] == std::optional<std::string>{"south"} &&
config.controllerPortButtons[3][11] == std::optional<std::string>{"east"} &&
config.controllerPortButtons[1][6] == std::optional<std::string>{"west"},
"valid per-port button overrides were not loaded")) return 1;
size_t setCount = 0;
for (const auto& portButtons : config.controllerPortButtons)
for (const auto& button : portButtons)
if (button) ++setCount;
if (!Require(setCount == 3, "out-of-range, non-string, or unknown per-port keys were accepted")) return 1;
}
{
std::istringstream input("[controller]\na = \"west\"\na_1 = \"south\"\nb_2 = \"east\"\n");
const RuntimeUserConfig config = RuntimeConfigFile::ParseConfig(input, "ports-fallback.toml");
if (!Require(config.controllerButtons[0] == std::optional<std::string>{"west"} &&
config.controllerPortButtons[0][0] == std::optional<std::string>{"south"} &&
config.controllerPortButtons[1][1] == std::optional<std::string>{"east"},
"shared and per-port button bindings were not kept apart")) return 1;
}
if (!Require(RuntimeConfigFile::PortControllerButtonKey(0, 0) == "a_1" &&
RuntimeConfigFile::PortControllerButtonKey(3, 11) == "right_4" &&
RuntimeConfigFile::PortControllerButtonKey(4, 0).empty() &&
RuntimeConfigFile::PortControllerButtonKey(0, 12).empty(),
"per-port button key layout is wrong")) return 1;
return 0;
}