core/hid: Only signal when needed

This commit is contained in:
german77 2021-10-19 00:12:24 -05:00 committed by Narr the Reg
parent 4d308fd0b4
commit 601ac43495
11 changed files with 242 additions and 155 deletions

View File

@ -30,6 +30,7 @@ enum class InputType {
}; };
enum class BatteryLevel { enum class BatteryLevel {
None,
Empty, Empty,
Critical, Critical,
Low, Low,

View File

@ -94,7 +94,6 @@ void EmulatedController::ReloadFromSettings() {
void EmulatedController::ReloadInput() { void EmulatedController::ReloadInput() {
//LOG_ERROR(Service_HID, "reload config {}", NpadIdTypeToIndex(npad_id_type)); //LOG_ERROR(Service_HID, "reload config {}", NpadIdTypeToIndex(npad_id_type));
// If you load any device here add the equivalent to the UnloadInput() function // If you load any device here add the equivalent to the UnloadInput() function
const auto player_index = NpadIdTypeToIndex(npad_id_type);
const auto left_side = button_params[Settings::NativeButton::ZL]; const auto left_side = button_params[Settings::NativeButton::ZL];
const auto right_side = button_params[Settings::NativeButton::ZR]; const auto right_side = button_params[Settings::NativeButton::ZR];
@ -337,120 +336,130 @@ void EmulatedController::SetButton(Input::CallbackStatus callback, std::size_t i
if (index >= controller.button_values.size()) { if (index >= controller.button_values.size()) {
return; return;
} }
std::lock_guard lock{mutex}; {
bool value_changed = false; std::lock_guard lock{mutex};
const auto new_status = TransformToButton(callback); bool value_changed = false;
auto& current_status = controller.button_values[index]; const auto new_status = TransformToButton(callback);
current_status.toggle = new_status.toggle; auto& current_status = controller.button_values[index];
current_status.toggle = new_status.toggle;
// Update button status with current // Update button status with current
if (!current_status.toggle) { if (!current_status.toggle) {
current_status.locked = false;
if (current_status.value != new_status.value) {
current_status.value = new_status.value;
value_changed = true;
}
} else {
// Toggle button and lock status
if (new_status.value && !current_status.locked) {
current_status.locked = true;
current_status.value = !current_status.value;
value_changed = true;
}
// Unlock button ready for next press
if (!new_status.value && current_status.locked) {
current_status.locked = false; current_status.locked = false;
if (current_status.value != new_status.value) {
current_status.value = new_status.value;
value_changed = true;
}
} else {
// Toggle button and lock status
if (new_status.value && !current_status.locked) {
current_status.locked = true;
current_status.value = !current_status.value;
value_changed = true;
}
// Unlock button ready for next press
if (!new_status.value && current_status.locked) {
current_status.locked = false;
}
}
if (!value_changed) {
return;
}
if (is_configuring) {
controller.npad_button_state.raw = NpadButton::None;
controller.debug_pad_button_state.raw = 0;
TriggerOnChange(ControllerTriggerType::Button, false);
return;
}
switch (index) {
case Settings::NativeButton::A:
controller.npad_button_state.a.Assign(current_status.value);
controller.debug_pad_button_state.a.Assign(current_status.value);
break;
case Settings::NativeButton::B:
controller.npad_button_state.b.Assign(current_status.value);
controller.debug_pad_button_state.b.Assign(current_status.value);
break;
case Settings::NativeButton::X:
controller.npad_button_state.x.Assign(current_status.value);
controller.debug_pad_button_state.x.Assign(current_status.value);
break;
case Settings::NativeButton::Y:
controller.npad_button_state.y.Assign(current_status.value);
controller.debug_pad_button_state.y.Assign(current_status.value);
break;
case Settings::NativeButton::LStick:
controller.npad_button_state.stick_l.Assign(current_status.value);
break;
case Settings::NativeButton::RStick:
controller.npad_button_state.stick_r.Assign(current_status.value);
break;
case Settings::NativeButton::L:
controller.npad_button_state.l.Assign(current_status.value);
controller.debug_pad_button_state.l.Assign(current_status.value);
break;
case Settings::NativeButton::R:
controller.npad_button_state.r.Assign(current_status.value);
controller.debug_pad_button_state.r.Assign(current_status.value);
break;
case Settings::NativeButton::ZL:
controller.npad_button_state.zl.Assign(current_status.value);
controller.debug_pad_button_state.zl.Assign(current_status.value);
break;
case Settings::NativeButton::ZR:
controller.npad_button_state.zr.Assign(current_status.value);
controller.debug_pad_button_state.zr.Assign(current_status.value);
break;
case Settings::NativeButton::Plus:
controller.npad_button_state.plus.Assign(current_status.value);
controller.debug_pad_button_state.plus.Assign(current_status.value);
break;
case Settings::NativeButton::Minus:
controller.npad_button_state.minus.Assign(current_status.value);
controller.debug_pad_button_state.minus.Assign(current_status.value);
break;
case Settings::NativeButton::DLeft:
controller.npad_button_state.left.Assign(current_status.value);
controller.debug_pad_button_state.d_left.Assign(current_status.value);
break;
case Settings::NativeButton::DUp:
controller.npad_button_state.up.Assign(current_status.value);
controller.debug_pad_button_state.d_up.Assign(current_status.value);
break;
case Settings::NativeButton::DRight:
controller.npad_button_state.right.Assign(current_status.value);
controller.debug_pad_button_state.d_right.Assign(current_status.value);
break;
case Settings::NativeButton::DDown:
controller.npad_button_state.down.Assign(current_status.value);
controller.debug_pad_button_state.d_down.Assign(current_status.value);
break;
case Settings::NativeButton::SL:
controller.npad_button_state.left_sl.Assign(current_status.value);
controller.npad_button_state.right_sl.Assign(current_status.value);
break;
case Settings::NativeButton::SR:
controller.npad_button_state.left_sr.Assign(current_status.value);
controller.npad_button_state.right_sr.Assign(current_status.value);
break;
case Settings::NativeButton::Home:
case Settings::NativeButton::Screenshot:
break;
} }
} }
if (!is_connected) {
if (!value_changed) { if (npad_id_type == NpadIdType::Player1 && npad_type != NpadType::Handheld) {
return; Connect();
}
if (npad_id_type == NpadIdType::Handheld && npad_type == NpadType::Handheld) {
Connect();
}
} }
TriggerOnChange(ControllerTriggerType::Button, true);
if (is_configuring) {
controller.npad_button_state.raw = NpadButton::None;
controller.debug_pad_button_state.raw = 0;
TriggerOnChange(ControllerTriggerType::Button);
return;
}
switch (index) {
case Settings::NativeButton::A:
controller.npad_button_state.a.Assign(current_status.value);
controller.debug_pad_button_state.a.Assign(current_status.value);
break;
case Settings::NativeButton::B:
controller.npad_button_state.b.Assign(current_status.value);
controller.debug_pad_button_state.b.Assign(current_status.value);
break;
case Settings::NativeButton::X:
controller.npad_button_state.x.Assign(current_status.value);
controller.debug_pad_button_state.x.Assign(current_status.value);
break;
case Settings::NativeButton::Y:
controller.npad_button_state.y.Assign(current_status.value);
controller.debug_pad_button_state.y.Assign(current_status.value);
break;
case Settings::NativeButton::LStick:
controller.npad_button_state.stick_l.Assign(current_status.value);
break;
case Settings::NativeButton::RStick:
controller.npad_button_state.stick_r.Assign(current_status.value);
break;
case Settings::NativeButton::L:
controller.npad_button_state.l.Assign(current_status.value);
controller.debug_pad_button_state.l.Assign(current_status.value);
break;
case Settings::NativeButton::R:
controller.npad_button_state.r.Assign(current_status.value);
controller.debug_pad_button_state.r.Assign(current_status.value);
break;
case Settings::NativeButton::ZL:
controller.npad_button_state.zl.Assign(current_status.value);
controller.debug_pad_button_state.zl.Assign(current_status.value);
break;
case Settings::NativeButton::ZR:
controller.npad_button_state.zr.Assign(current_status.value);
controller.debug_pad_button_state.zr.Assign(current_status.value);
break;
case Settings::NativeButton::Plus:
controller.npad_button_state.plus.Assign(current_status.value);
controller.debug_pad_button_state.plus.Assign(current_status.value);
break;
case Settings::NativeButton::Minus:
controller.npad_button_state.minus.Assign(current_status.value);
controller.debug_pad_button_state.minus.Assign(current_status.value);
break;
case Settings::NativeButton::DLeft:
controller.npad_button_state.left.Assign(current_status.value);
controller.debug_pad_button_state.d_left.Assign(current_status.value);
break;
case Settings::NativeButton::DUp:
controller.npad_button_state.up.Assign(current_status.value);
controller.debug_pad_button_state.d_up.Assign(current_status.value);
break;
case Settings::NativeButton::DRight:
controller.npad_button_state.right.Assign(current_status.value);
controller.debug_pad_button_state.d_right.Assign(current_status.value);
break;
case Settings::NativeButton::DDown:
controller.npad_button_state.down.Assign(current_status.value);
controller.debug_pad_button_state.d_down.Assign(current_status.value);
break;
case Settings::NativeButton::SL:
controller.npad_button_state.left_sl.Assign(current_status.value);
controller.npad_button_state.right_sl.Assign(current_status.value);
break;
case Settings::NativeButton::SR:
controller.npad_button_state.left_sr.Assign(current_status.value);
controller.npad_button_state.right_sr.Assign(current_status.value);
break;
case Settings::NativeButton::Home:
case Settings::NativeButton::Screenshot:
break;
}
TriggerOnChange(ControllerTriggerType::Button);
} }
void EmulatedController::SetStick(Input::CallbackStatus callback, std::size_t index) { void EmulatedController::SetStick(Input::CallbackStatus callback, std::size_t index) {
@ -463,7 +472,7 @@ void EmulatedController::SetStick(Input::CallbackStatus callback, std::size_t in
if (is_configuring) { if (is_configuring) {
controller.analog_stick_state.left = {}; controller.analog_stick_state.left = {};
controller.analog_stick_state.right = {}; controller.analog_stick_state.right = {};
TriggerOnChange(ControllerTriggerType::Stick); TriggerOnChange(ControllerTriggerType::Stick, false);
return; return;
} }
@ -489,7 +498,7 @@ void EmulatedController::SetStick(Input::CallbackStatus callback, std::size_t in
break; break;
} }
TriggerOnChange(ControllerTriggerType::Stick); TriggerOnChange(ControllerTriggerType::Stick, true);
} }
void EmulatedController::SetTrigger(Input::CallbackStatus callback, std::size_t index) { void EmulatedController::SetTrigger(Input::CallbackStatus callback, std::size_t index) {
@ -502,7 +511,7 @@ void EmulatedController::SetTrigger(Input::CallbackStatus callback, std::size_t
if (is_configuring) { if (is_configuring) {
controller.gc_trigger_state.left = 0; controller.gc_trigger_state.left = 0;
controller.gc_trigger_state.right = 0; controller.gc_trigger_state.right = 0;
TriggerOnChange(ControllerTriggerType::Trigger); TriggerOnChange(ControllerTriggerType::Trigger, false);
return; return;
} }
@ -520,7 +529,7 @@ void EmulatedController::SetTrigger(Input::CallbackStatus callback, std::size_t
break; break;
} }
TriggerOnChange(ControllerTriggerType::Trigger); TriggerOnChange(ControllerTriggerType::Trigger, true);
} }
void EmulatedController::SetMotion(Input::CallbackStatus callback, std::size_t index) { void EmulatedController::SetMotion(Input::CallbackStatus callback, std::size_t index) {
@ -546,7 +555,7 @@ void EmulatedController::SetMotion(Input::CallbackStatus callback, std::size_t i
emulated.UpdateOrientation(raw_status.delta_timestamp); emulated.UpdateOrientation(raw_status.delta_timestamp);
if (is_configuring) { if (is_configuring) {
TriggerOnChange(ControllerTriggerType::Motion); TriggerOnChange(ControllerTriggerType::Motion, false);
return; return;
} }
@ -557,7 +566,7 @@ void EmulatedController::SetMotion(Input::CallbackStatus callback, std::size_t i
motion.orientation = emulated.GetOrientation(); motion.orientation = emulated.GetOrientation();
motion.is_at_rest = emulated.IsMoving(motion_sensitivity); motion.is_at_rest = emulated.IsMoving(motion_sensitivity);
TriggerOnChange(ControllerTriggerType::Motion); TriggerOnChange(ControllerTriggerType::Motion, true);
} }
void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t index) { void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t index) {
@ -568,7 +577,7 @@ void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t
controller.battery_values[index] = TransformToBattery(callback); controller.battery_values[index] = TransformToBattery(callback);
if (is_configuring) { if (is_configuring) {
TriggerOnChange(ControllerTriggerType::Battery); TriggerOnChange(ControllerTriggerType::Battery, false);
return; return;
} }
@ -593,6 +602,7 @@ void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t
case Input::BatteryLevel::Empty: case Input::BatteryLevel::Empty:
battery_level = 0; battery_level = 0;
break; break;
case Input::BatteryLevel::None:
case Input::BatteryLevel::Full: case Input::BatteryLevel::Full:
default: default:
is_powered = true; is_powered = true;
@ -623,7 +633,7 @@ void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t
}; };
break; break;
} }
TriggerOnChange(ControllerTriggerType::Battery); TriggerOnChange(ControllerTriggerType::Battery, true);
} }
bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) { bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) {
@ -677,7 +687,7 @@ void EmulatedController::Connect() {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
if (is_configuring) { if (is_configuring) {
temporary_is_connected = true; temporary_is_connected = true;
TriggerOnChange(ControllerTriggerType::Connected); TriggerOnChange(ControllerTriggerType::Connected,false);
return; return;
} }
@ -687,7 +697,7 @@ void EmulatedController::Connect() {
is_connected = true; is_connected = true;
} }
LOG_ERROR(Service_HID, "Connected controller {}", NpadIdTypeToIndex(npad_id_type)); LOG_ERROR(Service_HID, "Connected controller {}", NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Connected); TriggerOnChange(ControllerTriggerType::Connected,true);
} }
void EmulatedController::Disconnect() { void EmulatedController::Disconnect() {
@ -697,7 +707,7 @@ void EmulatedController::Disconnect() {
temporary_is_connected = false; temporary_is_connected = false;
LOG_ERROR(Service_HID, "Disconnected temporal controller {}", LOG_ERROR(Service_HID, "Disconnected temporal controller {}",
NpadIdTypeToIndex(npad_id_type)); NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Disconnected); TriggerOnChange(ControllerTriggerType::Disconnected,false);
return; return;
} }
@ -707,7 +717,7 @@ void EmulatedController::Disconnect() {
is_connected = false; is_connected = false;
} }
LOG_ERROR(Service_HID, "Disconnected controller {}", NpadIdTypeToIndex(npad_id_type)); LOG_ERROR(Service_HID, "Disconnected controller {}", NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Disconnected); TriggerOnChange(ControllerTriggerType::Disconnected,true);
} }
bool EmulatedController::IsConnected(bool temporary) const { bool EmulatedController::IsConnected(bool temporary) const {
@ -741,7 +751,7 @@ void EmulatedController::SetNpadType(NpadType npad_type_) {
return; return;
} }
temporary_npad_type = npad_type_; temporary_npad_type = npad_type_;
TriggerOnChange(ControllerTriggerType::Type); TriggerOnChange(ControllerTriggerType::Type,false);
return; return;
} }
@ -754,7 +764,7 @@ void EmulatedController::SetNpadType(NpadType npad_type_) {
} }
npad_type = npad_type_; npad_type = npad_type_;
} }
TriggerOnChange(ControllerTriggerType::Type); TriggerOnChange(ControllerTriggerType::Type,true);
} }
LedPattern EmulatedController::GetLedPattern() const { LedPattern EmulatedController::GetLedPattern() const {
@ -844,9 +854,12 @@ BatteryLevelState EmulatedController::GetBattery() const {
return controller.battery_state; return controller.battery_state;
} }
void EmulatedController::TriggerOnChange(ControllerTriggerType type) { void EmulatedController::TriggerOnChange(ControllerTriggerType type, bool is_service_update) {
for (const std::pair<int, ControllerUpdateCallback> poller_pair : callback_list) { for (const std::pair<int, ControllerUpdateCallback> poller_pair : callback_list) {
const ControllerUpdateCallback& poller = poller_pair.second; const ControllerUpdateCallback& poller = poller_pair.second;
if (!is_service_update && poller.is_service) {
continue;
}
if (poller.on_change) { if (poller.on_change) {
poller.on_change(type); poller.on_change(type);
} }

View File

@ -113,6 +113,7 @@ enum class ControllerTriggerType {
struct ControllerUpdateCallback { struct ControllerUpdateCallback {
std::function<void(ControllerTriggerType)> on_change; std::function<void(ControllerTriggerType)> on_change;
bool is_service;
}; };
class EmulatedController { class EmulatedController {
@ -325,9 +326,10 @@ private:
/** /**
* Triggers a callback that something has changed on the controller status * Triggers a callback that something has changed on the controller status
* @param Input type of the event to trigger * @param type: Input type of the event to trigger
* @param is_service_update: indicates if this event should be sended to only services
*/ */
void TriggerOnChange(ControllerTriggerType type); void TriggerOnChange(ControllerTriggerType type, bool is_service_update);
NpadIdType npad_id_type; NpadIdType npad_id_type;
NpadType npad_type{NpadType::None}; NpadType npad_type{NpadType::None};

View File

@ -10,7 +10,7 @@
namespace Core::HID { namespace Core::HID {
Input::BatteryStatus TransformToBattery(const Input::CallbackStatus& callback) { Input::BatteryStatus TransformToBattery(const Input::CallbackStatus& callback) {
Input::BatteryStatus battery{}; Input::BatteryStatus battery{Input::BatteryStatus::None};
switch (callback.type) { switch (callback.type) {
case Input::InputType::Analog: case Input::InputType::Analog:
case Input::InputType::Trigger: { case Input::InputType::Trigger: {

View File

@ -104,7 +104,10 @@ Controller_NPad::Controller_NPad(Core::System& system_,
controller.vibration[0].latest_vibration_value = DEFAULT_VIBRATION_VALUE; controller.vibration[0].latest_vibration_value = DEFAULT_VIBRATION_VALUE;
controller.vibration[1].latest_vibration_value = DEFAULT_VIBRATION_VALUE; controller.vibration[1].latest_vibration_value = DEFAULT_VIBRATION_VALUE;
Core::HID::ControllerUpdateCallback engine_callback{ Core::HID::ControllerUpdateCallback engine_callback{
[this, i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); }}; .on_change = [this,
i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); },
.is_service = true,
};
controller.callback_key = controller.device->SetCallback(engine_callback); controller.callback_key = controller.device->SetCallback(engine_callback);
} }
} }
@ -283,7 +286,6 @@ void Controller_NPad::OnInit() {
// Prefill controller buffers // Prefill controller buffers
for (auto& controller : controller_data) { for (auto& controller : controller_data) {
NPadGenericState dummy_pad_state{};
auto& npad = controller.shared_memory_entry; auto& npad = controller.shared_memory_entry;
for (std::size_t i = 0; i < 19; ++i) { for (std::size_t i = 0; i < 19; ++i) {
WriteEmptyEntry(npad); WriteEmptyEntry(npad);

View File

@ -150,7 +150,10 @@ void GCAdapter::UpdatePadType(std::size_t port, ControllerTypes pad_type) {
return; return;
} }
// Device changed reset device and set new type // Device changed reset device and set new type
pads[port] = {}; pads[port].axis_origin = {};
pads[port].reset_origin_counter = {};
pads[port].enable_vibration = {};
pads[port].rumble_amplitude = {};
pads[port].type = pad_type; pads[port].type = pad_type;
} }
@ -396,12 +399,11 @@ std::vector<Common::ParamPackage> GCAdapter::GetInputDevices() const {
if (!DeviceConnected(port)) { if (!DeviceConnected(port)) {
continue; continue;
} }
const std::string name = fmt::format("Gamecube Controller {}", port + 1); Common::ParamPackage identifier{};
devices.emplace_back(Common::ParamPackage{ identifier.Set("engine", GetEngineName());
{"engine", "gcpad"}, identifier.Set("display", fmt::format("Gamecube Controller {}", port + 1));
{"display", std::move(name)}, identifier.Set("port", static_cast<int>(port));
{"port", std::to_string(port)}, devices.emplace_back(identifier);
});
} }
return devices; return devices;
} }
@ -431,7 +433,8 @@ ButtonMapping GCAdapter::GetButtonMappingForDevice(const Common::ParamPackage& p
ButtonMapping mapping{}; ButtonMapping mapping{};
for (const auto& [switch_button, gcadapter_button] : switch_to_gcadapter_button) { for (const auto& [switch_button, gcadapter_button] : switch_to_gcadapter_button) {
Common::ParamPackage button_params({{"engine", "gcpad"}}); Common::ParamPackage button_params{};
button_params.Set("engine", GetEngineName());
button_params.Set("port", params.Get("port", 0)); button_params.Set("port", params.Get("port", 0));
button_params.Set("button", static_cast<int>(gcadapter_button)); button_params.Set("button", static_cast<int>(gcadapter_button));
mapping.insert_or_assign(switch_button, std::move(button_params)); mapping.insert_or_assign(switch_button, std::move(button_params));
@ -444,7 +447,8 @@ ButtonMapping GCAdapter::GetButtonMappingForDevice(const Common::ParamPackage& p
{Settings::NativeButton::ZR, PadButton::TriggerR, PadAxes::TriggerRight}, {Settings::NativeButton::ZR, PadButton::TriggerR, PadAxes::TriggerRight},
}; };
for (const auto& [switch_button, gcadapter_buton, gcadapter_axis] : switch_to_gcadapter_axis) { for (const auto& [switch_button, gcadapter_buton, gcadapter_axis] : switch_to_gcadapter_axis) {
Common::ParamPackage button_params({{"engine", "gcpad"}}); Common::ParamPackage button_params{};
button_params.Set("engine", GetEngineName());
button_params.Set("port", params.Get("port", 0)); button_params.Set("port", params.Get("port", 0));
button_params.Set("button", static_cast<s32>(gcadapter_buton)); button_params.Set("button", static_cast<s32>(gcadapter_buton));
button_params.Set("axis", static_cast<s32>(gcadapter_axis)); button_params.Set("axis", static_cast<s32>(gcadapter_axis));
@ -463,13 +467,13 @@ AnalogMapping GCAdapter::GetAnalogMappingForDevice(const Common::ParamPackage& p
AnalogMapping mapping = {}; AnalogMapping mapping = {};
Common::ParamPackage left_analog_params; Common::ParamPackage left_analog_params;
left_analog_params.Set("engine", "gcpad"); left_analog_params.Set("engine", GetEngineName());
left_analog_params.Set("port", params.Get("port", 0)); left_analog_params.Set("port", params.Get("port", 0));
left_analog_params.Set("axis_x", static_cast<int>(PadAxes::StickX)); left_analog_params.Set("axis_x", static_cast<int>(PadAxes::StickX));
left_analog_params.Set("axis_y", static_cast<int>(PadAxes::StickY)); left_analog_params.Set("axis_y", static_cast<int>(PadAxes::StickY));
mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params)); mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params));
Common::ParamPackage right_analog_params; Common::ParamPackage right_analog_params;
right_analog_params.Set("engine", "gcpad"); right_analog_params.Set("engine", GetEngineName());
right_analog_params.Set("port", params.Get("port", 0)); right_analog_params.Set("port", params.Get("port", 0));
right_analog_params.Set("axis_x", static_cast<int>(PadAxes::SubstickX)); right_analog_params.Set("axis_x", static_cast<int>(PadAxes::SubstickX));
right_analog_params.Set("axis_y", static_cast<int>(PadAxes::SubstickY)); right_analog_params.Set("axis_y", static_cast<int>(PadAxes::SubstickY));
@ -477,9 +481,56 @@ AnalogMapping GCAdapter::GetAnalogMappingForDevice(const Common::ParamPackage& p
return mapping; return mapping;
} }
std::string GCAdapter::GetUIButtonName(const Common::ParamPackage& params) const {
PadButton button = static_cast<PadButton>(params.Get("button", 0));
switch (button) {
case PadButton::ButtonLeft:
return "left";
break;
case PadButton::ButtonRight:
return "right";
break;
case PadButton::ButtonDown:
return "down";
break;
case PadButton::ButtonUp:
return "up";
break;
case PadButton::TriggerZ:
return "Z";
break;
case PadButton::TriggerR:
return "R";
break;
case PadButton::TriggerL:
return "L";
break;
case PadButton::ButtonA:
return "A";
break;
case PadButton::ButtonB:
return "B";
break;
case PadButton::ButtonX:
return "X";
break;
case PadButton::ButtonY:
return "Y";
break;
case PadButton::ButtonStart:
return "start";
break;
default:
return "Unkown GC";
}
}
std::string GCAdapter::GetUIName(const Common::ParamPackage& params) const { std::string GCAdapter::GetUIName(const Common::ParamPackage& params) const {
if (params.Has("button")) { if (params.Has("button")) {
return fmt::format("Button {}", params.Get("button", 0)); return fmt::format("Button {}", GetUIButtonName(params));
}
if (params.Has("axis")) {
return fmt::format("Axis {}", params.Get("axis",0));
} }
return "Bad GC Adapter"; return "Bad GC Adapter";

View File

@ -105,8 +105,12 @@ private:
void Reset(); void Reset();
void UpdateVibrations(); void UpdateVibrations();
// Updates vibration state of all controllers
/// Updates vibration state of all controllers
void SendVibrations(); void SendVibrations();
std::string GetUIButtonName(const Common::ParamPackage& params) const;
std::unique_ptr<LibUSBDeviceHandle> usb_adapter_handle; std::unique_ptr<LibUSBDeviceHandle> usb_adapter_handle;
std::array<GCController, 4> pads; std::array<GCController, 4> pads;

View File

@ -26,7 +26,7 @@ void Keyboard::ReleaseAllKeys() {
std::vector<Common::ParamPackage> Keyboard::GetInputDevices() const { std::vector<Common::ParamPackage> Keyboard::GetInputDevices() const {
std::vector<Common::ParamPackage> devices; std::vector<Common::ParamPackage> devices;
devices.emplace_back(Common::ParamPackage{ devices.emplace_back(Common::ParamPackage{
{"engine", "keyboard"}, {"engine", GetEngineName()},
{"display", "Keyboard Only"}, {"display", "Keyboard Only"},
}); });
return devices; return devices;

View File

@ -305,6 +305,7 @@ void SDLDriver::InitJoystick(int joystick_index) {
if (joystick_map.find(guid) == joystick_map.end()) { if (joystick_map.find(guid) == joystick_map.end()) {
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller);
PreSetController(joystick->GetPadIdentifier()); PreSetController(joystick->GetPadIdentifier());
SetBattery(joystick->GetPadIdentifier(), joystick->GetBatteryLevel());
joystick_map[guid].emplace_back(std::move(joystick)); joystick_map[guid].emplace_back(std::move(joystick));
return; return;
} }
@ -322,6 +323,7 @@ void SDLDriver::InitJoystick(int joystick_index) {
const int port = static_cast<int>(joystick_guid_list.size()); const int port = static_cast<int>(joystick_guid_list.size());
auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller); auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller);
PreSetController(joystick->GetPadIdentifier()); PreSetController(joystick->GetPadIdentifier());
SetBattery(joystick->GetPadIdentifier(), joystick->GetBatteryLevel());
joystick_guid_list.emplace_back(std::move(joystick)); joystick_guid_list.emplace_back(std::move(joystick));
} }
@ -472,7 +474,7 @@ std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const {
const std::string name = const std::string name =
fmt::format("{} {}", joystick->GetControllerName(), joystick->GetPort()); fmt::format("{} {}", joystick->GetControllerName(), joystick->GetPort());
devices.emplace_back(Common::ParamPackage{ devices.emplace_back(Common::ParamPackage{
{"engine", "sdl"}, {"engine", GetEngineName()},
{"display", std::move(name)}, {"display", std::move(name)},
{"guid", joystick->GetGUID()}, {"guid", joystick->GetGUID()},
{"port", std::to_string(joystick->GetPort())}, {"port", std::to_string(joystick->GetPort())},
@ -495,7 +497,7 @@ std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const {
const std::string name = const std::string name =
fmt::format("{} {}", "Nintendo Dual Joy-Con", joystick->GetPort()); fmt::format("{} {}", "Nintendo Dual Joy-Con", joystick->GetPort());
devices.emplace_back(Common::ParamPackage{ devices.emplace_back(Common::ParamPackage{
{"engine", "sdl"}, {"engine", GetEngineName()},
{"display", std::move(name)}, {"display", std::move(name)},
{"guid", joystick->GetGUID()}, {"guid", joystick->GetGUID()},
{"guid2", joystick2->GetGUID()}, {"guid2", joystick2->GetGUID()},
@ -527,7 +529,8 @@ Input::VibrationError SDLDriver::SetRumble(const PadIdentifier& identifier,
} }
Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid, Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid,
s32 axis, float value) const { s32 axis, float value) const {
Common::ParamPackage params({{"engine", "sdl"}}); Common::ParamPackage params{};
params.Set("engine", GetEngineName());
params.Set("port", port); params.Set("port", port);
params.Set("guid", std::move(guid)); params.Set("guid", std::move(guid));
params.Set("axis", axis); params.Set("axis", axis);
@ -538,7 +541,8 @@ Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::
Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::string guid, Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::string guid,
s32 button) const { s32 button) const {
Common::ParamPackage params({{"engine", "sdl"}}); Common::ParamPackage params{};
params.Set("engine", GetEngineName());
params.Set("port", port); params.Set("port", port);
params.Set("guid", std::move(guid)); params.Set("guid", std::move(guid));
params.Set("button", button); params.Set("button", button);
@ -547,8 +551,8 @@ Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::
Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::string guid, s32 hat, Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::string guid, s32 hat,
u8 value) const { u8 value) const {
Common::ParamPackage params({{"engine", "sdl"}}); Common::ParamPackage params{};
params.Set("engine", GetEngineName());
params.Set("port", port); params.Set("port", port);
params.Set("guid", std::move(guid)); params.Set("guid", std::move(guid));
params.Set("hat", hat); params.Set("hat", hat);
@ -557,7 +561,9 @@ Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::str
} }
Common::ParamPackage SDLDriver::BuildMotionParam(int port, std::string guid) const { Common::ParamPackage SDLDriver::BuildMotionParam(int port, std::string guid) const {
Common::ParamPackage params({{"engine", "sdl"}, {"motion", "0"}}); Common::ParamPackage params{};
params.Set("engine", GetEngineName());
params.Set("motion", 0);
params.Set("port", port); params.Set("port", port);
params.Set("guid", std::move(guid)); params.Set("guid", std::move(guid));
return params; return params;
@ -583,7 +589,7 @@ Common::ParamPackage SDLDriver::BuildParamPackageForAnalog(PadIdentifier identif
int axis_y, float offset_x, int axis_y, float offset_x,
float offset_y) const { float offset_y) const {
Common::ParamPackage params; Common::ParamPackage params;
params.Set("engine", "sdl"); params.Set("engine", GetEngineName());
params.Set("port", static_cast<int>(identifier.port)); params.Set("port", static_cast<int>(identifier.port));
params.Set("guid", identifier.guid.Format()); params.Set("guid", identifier.guid.Format());
params.Set("axis_x", axis_x); params.Set("axis_x", axis_x);

View File

@ -127,7 +127,7 @@ void Tas::WriteTasFile(std::u8string file_name) {
std::string output_text; std::string output_text;
for (size_t frame = 0; frame < record_commands.size(); frame++) { for (size_t frame = 0; frame < record_commands.size(); frame++) {
const TASCommand& line = record_commands[frame]; const TASCommand& line = record_commands[frame];
output_text += fmt::format("{} {} {} {} {}\n", frame, WriteCommandButtons(line.buttons), output_text += fmt::format("{} {} {} {}\n", frame, WriteCommandButtons(line.buttons),
WriteCommandAxis(line.l_axis), WriteCommandAxis(line.r_axis)); WriteCommandAxis(line.l_axis), WriteCommandAxis(line.r_axis));
} }
const auto bytes_written = Common::FS::WriteStringToFile( const auto bytes_written = Common::FS::WriteStringToFile(

View File

@ -27,7 +27,9 @@ void PlayerControlPreview::SetController(Core::HID::EmulatedController* controll
is_controller_set = true; is_controller_set = true;
controller = controller_; controller = controller_;
Core::HID::ControllerUpdateCallback engine_callback{ Core::HID::ControllerUpdateCallback engine_callback{
[this](Core::HID::ControllerTriggerType type) { ControllerUpdate(type); }}; .on_change = [this](Core::HID::ControllerTriggerType type) { ControllerUpdate(type); },
.is_service = false,
};
callback_key = controller->SetCallback(engine_callback); callback_key = controller->SetCallback(engine_callback);
ControllerUpdate(Core::HID::ControllerTriggerType::All); ControllerUpdate(Core::HID::ControllerTriggerType::All);
} }
@ -810,7 +812,7 @@ void PlayerControlPreview::DrawProController(QPainter& p, const QPointF center)
DrawSymbol(p, center + QPoint(29, -56), Symbol::House, 3.9f); DrawSymbol(p, center + QPoint(29, -56), Symbol::House, 3.9f);
// Draw battery // Draw battery
DrawBattery(p, center + QPoint(-30, -165), battery_values[0]); DrawBattery(p, center + QPoint(-30, -160), battery_values[0]);
} }
void PlayerControlPreview::DrawGCController(QPainter& p, const QPointF center) { void PlayerControlPreview::DrawGCController(QPainter& p, const QPointF center) {
@ -2655,6 +2657,9 @@ void PlayerControlPreview::DrawTriggerButton(QPainter& p, const QPointF center,
} }
void PlayerControlPreview::DrawBattery(QPainter& p, QPointF center, Input::BatteryLevel battery) { void PlayerControlPreview::DrawBattery(QPainter& p, QPointF center, Input::BatteryLevel battery) {
if (battery == Input::BatteryLevel::None) {
return;
}
p.setPen(colors.outline); p.setPen(colors.outline);
p.setBrush(colors.transparent); p.setBrush(colors.transparent);
p.drawRect(center.x(), center.y(), 56, 20); p.drawRect(center.x(), center.y(), 56, 20);
@ -2669,6 +2674,7 @@ void PlayerControlPreview::DrawBattery(QPainter& p, QPointF center, Input::Batte
p.drawRect(center.x() + 42, center.y(), 14, 20); p.drawRect(center.x() + 42, center.y(), 14, 20);
p.drawRect(center.x() + 28, center.y(), 14, 20); p.drawRect(center.x() + 28, center.y(), 14, 20);
p.drawRect(center.x() + 14, center.y(), 14, 20); p.drawRect(center.x() + 14, center.y(), 14, 20);
p.drawRect(center.x(), center.y(), 14, 20);
break; break;
case Input::BatteryLevel::Medium: case Input::BatteryLevel::Medium:
p.drawRect(center.x() + 28, center.y(), 14, 20); p.drawRect(center.x() + 28, center.y(), 14, 20);
@ -2685,6 +2691,8 @@ void PlayerControlPreview::DrawBattery(QPainter& p, QPointF center, Input::Batte
case Input::BatteryLevel::Empty: case Input::BatteryLevel::Empty:
p.drawRect(center.x(), center.y(), 5, 20); p.drawRect(center.x(), center.y(), 5, 20);
break; break;
default:
break;
} }
} }