core/hid: Add TAS input

This commit is contained in:
german77 2021-10-27 00:20:28 -05:00 committed by Narr the Reg
parent 1d71d4b874
commit c085e54316
6 changed files with 82 additions and 13 deletions

View file

@ -105,6 +105,8 @@ void EmulatedController::LoadDevices() {
output_params[LeftIndex].Set("output", true);
output_params[RightIndex].Set("output", true);
LoadTASParams();
std::transform(button_params.begin() + Settings::NativeButton::BUTTON_HID_BEGIN,
button_params.begin() + Settings::NativeButton::BUTTON_NS_END,
button_devices.begin(), Input::CreateDevice<Input::InputDevice>);
@ -120,6 +122,51 @@ void EmulatedController::LoadDevices() {
Input::CreateDevice<Input::InputDevice>);
std::transform(output_params.begin(), output_params.end(), output_devices.begin(),
Input::CreateDevice<Input::OutputDevice>);
// Initialize TAS devices
std::transform(tas_button_params.begin(), tas_button_params.end(), tas_button_devices.begin(),
Input::CreateDevice<Input::InputDevice>);
std::transform(tas_stick_params.begin(), tas_stick_params.begin(), tas_stick_devices.begin(),
Input::CreateDevice<Input::InputDevice>);
}
void EmulatedController::LoadTASParams() {
const auto player_index = NpadIdTypeToIndex(npad_id_type);
Common::ParamPackage common_params{};
common_params.Set("engine", "tas");
common_params.Set("pad", static_cast<int>(player_index));
for (auto& param : tas_button_params) {
param = common_params;
}
for (auto& param : tas_stick_params) {
param = common_params;
}
tas_button_params[Settings::NativeButton::A].Set("button", 1 << 0);
tas_button_params[Settings::NativeButton::B].Set("button", 1 << 1);
tas_button_params[Settings::NativeButton::X].Set("button", 1 << 2);
tas_button_params[Settings::NativeButton::Y].Set("button", 1 << 3);
tas_button_params[Settings::NativeButton::LStick].Set("button", 1 << 4);
tas_button_params[Settings::NativeButton::RStick].Set("button", 1 << 5);
tas_button_params[Settings::NativeButton::L].Set("button", 1 << 6);
tas_button_params[Settings::NativeButton::R].Set("button", 1 << 7);
tas_button_params[Settings::NativeButton::ZL].Set("button", 1 << 8);
tas_button_params[Settings::NativeButton::ZR].Set("button", 1 << 9);
tas_button_params[Settings::NativeButton::Plus].Set("button", 1 << 10);
tas_button_params[Settings::NativeButton::Minus].Set("button", 1 << 11);
tas_button_params[Settings::NativeButton::DLeft].Set("button", 1 << 12);
tas_button_params[Settings::NativeButton::DUp].Set("button", 1 << 13);
tas_button_params[Settings::NativeButton::DRight].Set("button", 1 << 14);
tas_button_params[Settings::NativeButton::DDown].Set("button", 1 << 15);
tas_button_params[Settings::NativeButton::SL].Set("button", 1 << 16);
tas_button_params[Settings::NativeButton::SR].Set("button", 1 << 17);
tas_button_params[Settings::NativeButton::Home].Set("button", 1 << 18);
tas_button_params[Settings::NativeButton::Screenshot].Set("button", 1 << 19);
tas_stick_params[Settings::NativeAnalog::LStick].Set("axis_x", 0);
tas_stick_params[Settings::NativeAnalog::LStick].Set("axis_y", 1);
tas_stick_params[Settings::NativeAnalog::RStick].Set("axis_x", 2);
tas_stick_params[Settings::NativeAnalog::RStick].Set("axis_y", 3);
}
void EmulatedController::ReloadInput() {
@ -174,6 +221,25 @@ void EmulatedController::ReloadInput() {
motion_devices[index]->SetCallback(motion_callback);
motion_devices[index]->ForceUpdate();
}
// Register TAS devices. No need to force update
for (std::size_t index = 0; index < tas_button_devices.size(); ++index) {
if (!tas_button_devices[index]) {
continue;
}
Input::InputCallback button_callback{
[this, index](Input::CallbackStatus callback) { SetButton(callback, index); }};
tas_button_devices[index]->SetCallback(button_callback);
}
for (std::size_t index = 0; index < tas_stick_devices.size(); ++index) {
if (!tas_stick_devices[index]) {
continue;
}
Input::InputCallback stick_callback{
[this, index](Input::CallbackStatus callback) { SetStick(callback, index); }};
tas_stick_devices[index]->SetCallback(stick_callback);
}
}
void EmulatedController::UnloadInput() {
@ -195,6 +261,12 @@ void EmulatedController::UnloadInput() {
for (auto& output : output_devices) {
output.reset();
}
for (auto& button : tas_button_devices) {
button.reset();
}
for (auto& stick : tas_stick_devices) {
stick.reset();
}
}
void EmulatedController::EnableConfiguration() {

View file

@ -299,6 +299,9 @@ private:
/// creates input devices from params
void LoadDevices();
/// Set the params for TAS devices
void LoadTASParams();
/**
* Updates the button status of the controller
* @param callback: A CallbackStatus containing the button status
@ -363,6 +366,12 @@ private:
BatteryDevices battery_devices;
OutputDevices output_devices;
// TAS related variables
ButtonParams tas_button_params;
StickParams tas_stick_params;
ButtonDevices tas_button_devices;
StickDevices tas_stick_devices;
mutable std::mutex mutex;
std::unordered_map<int, ControllerUpdateCallback> callback_list;
int last_callback_key = 0;