1
0
Fork 0
forked from suyu/suyu

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 {
None,
Empty,
Critical,
Low,

View file

@ -94,7 +94,6 @@ void EmulatedController::ReloadFromSettings() {
void EmulatedController::ReloadInput() {
//LOG_ERROR(Service_HID, "reload config {}", NpadIdTypeToIndex(npad_id_type));
// 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 right_side = button_params[Settings::NativeButton::ZR];
@ -337,6 +336,7 @@ void EmulatedController::SetButton(Input::CallbackStatus callback, std::size_t i
if (index >= controller.button_values.size()) {
return;
}
{
std::lock_guard lock{mutex};
bool value_changed = false;
const auto new_status = TransformToButton(callback);
@ -371,7 +371,7 @@ void EmulatedController::SetButton(Input::CallbackStatus callback, std::size_t i
if (is_configuring) {
controller.npad_button_state.raw = NpadButton::None;
controller.debug_pad_button_state.raw = 0;
TriggerOnChange(ControllerTriggerType::Button);
TriggerOnChange(ControllerTriggerType::Button, false);
return;
}
@ -450,7 +450,16 @@ void EmulatedController::SetButton(Input::CallbackStatus callback, std::size_t i
case Settings::NativeButton::Screenshot:
break;
}
TriggerOnChange(ControllerTriggerType::Button);
}
if (!is_connected) {
if (npad_id_type == NpadIdType::Player1 && npad_type != NpadType::Handheld) {
Connect();
}
if (npad_id_type == NpadIdType::Handheld && npad_type == NpadType::Handheld) {
Connect();
}
}
TriggerOnChange(ControllerTriggerType::Button, true);
}
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) {
controller.analog_stick_state.left = {};
controller.analog_stick_state.right = {};
TriggerOnChange(ControllerTriggerType::Stick);
TriggerOnChange(ControllerTriggerType::Stick, false);
return;
}
@ -489,7 +498,7 @@ void EmulatedController::SetStick(Input::CallbackStatus callback, std::size_t in
break;
}
TriggerOnChange(ControllerTriggerType::Stick);
TriggerOnChange(ControllerTriggerType::Stick, true);
}
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) {
controller.gc_trigger_state.left = 0;
controller.gc_trigger_state.right = 0;
TriggerOnChange(ControllerTriggerType::Trigger);
TriggerOnChange(ControllerTriggerType::Trigger, false);
return;
}
@ -520,7 +529,7 @@ void EmulatedController::SetTrigger(Input::CallbackStatus callback, std::size_t
break;
}
TriggerOnChange(ControllerTriggerType::Trigger);
TriggerOnChange(ControllerTriggerType::Trigger, true);
}
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);
if (is_configuring) {
TriggerOnChange(ControllerTriggerType::Motion);
TriggerOnChange(ControllerTriggerType::Motion, false);
return;
}
@ -557,7 +566,7 @@ void EmulatedController::SetMotion(Input::CallbackStatus callback, std::size_t i
motion.orientation = emulated.GetOrientation();
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) {
@ -568,7 +577,7 @@ void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t
controller.battery_values[index] = TransformToBattery(callback);
if (is_configuring) {
TriggerOnChange(ControllerTriggerType::Battery);
TriggerOnChange(ControllerTriggerType::Battery, false);
return;
}
@ -593,6 +602,7 @@ void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t
case Input::BatteryLevel::Empty:
battery_level = 0;
break;
case Input::BatteryLevel::None:
case Input::BatteryLevel::Full:
default:
is_powered = true;
@ -623,7 +633,7 @@ void EmulatedController::SetBattery(Input::CallbackStatus callback, std::size_t
};
break;
}
TriggerOnChange(ControllerTriggerType::Battery);
TriggerOnChange(ControllerTriggerType::Battery, true);
}
bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) {
@ -677,7 +687,7 @@ void EmulatedController::Connect() {
std::lock_guard lock{mutex};
if (is_configuring) {
temporary_is_connected = true;
TriggerOnChange(ControllerTriggerType::Connected);
TriggerOnChange(ControllerTriggerType::Connected,false);
return;
}
@ -687,7 +697,7 @@ void EmulatedController::Connect() {
is_connected = true;
}
LOG_ERROR(Service_HID, "Connected controller {}", NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Connected);
TriggerOnChange(ControllerTriggerType::Connected,true);
}
void EmulatedController::Disconnect() {
@ -697,7 +707,7 @@ void EmulatedController::Disconnect() {
temporary_is_connected = false;
LOG_ERROR(Service_HID, "Disconnected temporal controller {}",
NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Disconnected);
TriggerOnChange(ControllerTriggerType::Disconnected,false);
return;
}
@ -707,7 +717,7 @@ void EmulatedController::Disconnect() {
is_connected = false;
}
LOG_ERROR(Service_HID, "Disconnected controller {}", NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Disconnected);
TriggerOnChange(ControllerTriggerType::Disconnected,true);
}
bool EmulatedController::IsConnected(bool temporary) const {
@ -741,7 +751,7 @@ void EmulatedController::SetNpadType(NpadType npad_type_) {
return;
}
temporary_npad_type = npad_type_;
TriggerOnChange(ControllerTriggerType::Type);
TriggerOnChange(ControllerTriggerType::Type,false);
return;
}
@ -754,7 +764,7 @@ void EmulatedController::SetNpadType(NpadType npad_type_) {
}
npad_type = npad_type_;
}
TriggerOnChange(ControllerTriggerType::Type);
TriggerOnChange(ControllerTriggerType::Type,true);
}
LedPattern EmulatedController::GetLedPattern() const {
@ -844,9 +854,12 @@ BatteryLevelState EmulatedController::GetBattery() const {
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) {
const ControllerUpdateCallback& poller = poller_pair.second;
if (!is_service_update && poller.is_service) {
continue;
}
if (poller.on_change) {
poller.on_change(type);
}

View file

@ -113,6 +113,7 @@ enum class ControllerTriggerType {
struct ControllerUpdateCallback {
std::function<void(ControllerTriggerType)> on_change;
bool is_service;
};
class EmulatedController {
@ -325,9 +326,10 @@ private:
/**
* 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;
NpadType npad_type{NpadType::None};

View file

@ -10,7 +10,7 @@
namespace Core::HID {
Input::BatteryStatus TransformToBattery(const Input::CallbackStatus& callback) {
Input::BatteryStatus battery{};
Input::BatteryStatus battery{Input::BatteryStatus::None};
switch (callback.type) {
case Input::InputType::Analog:
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[1].latest_vibration_value = DEFAULT_VIBRATION_VALUE;
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);
}
}
@ -283,7 +286,6 @@ void Controller_NPad::OnInit() {
// Prefill controller buffers
for (auto& controller : controller_data) {
NPadGenericState dummy_pad_state{};
auto& npad = controller.shared_memory_entry;
for (std::size_t i = 0; i < 19; ++i) {
WriteEmptyEntry(npad);

View file

@ -150,7 +150,10 @@ void GCAdapter::UpdatePadType(std::size_t port, ControllerTypes pad_type) {
return;
}
// 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;
}
@ -396,12 +399,11 @@ std::vector<Common::ParamPackage> GCAdapter::GetInputDevices() const {
if (!DeviceConnected(port)) {
continue;
}
const std::string name = fmt::format("Gamecube Controller {}", port + 1);
devices.emplace_back(Common::ParamPackage{
{"engine", "gcpad"},
{"display", std::move(name)},
{"port", std::to_string(port)},
});
Common::ParamPackage identifier{};
identifier.Set("engine", GetEngineName());
identifier.Set("display", fmt::format("Gamecube Controller {}", port + 1));
identifier.Set("port", static_cast<int>(port));
devices.emplace_back(identifier);
}
return devices;
}
@ -431,7 +433,8 @@ ButtonMapping GCAdapter::GetButtonMappingForDevice(const Common::ParamPackage& p
ButtonMapping mapping{};
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("button", static_cast<int>(gcadapter_button));
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},
};
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("button", static_cast<s32>(gcadapter_buton));
button_params.Set("axis", static_cast<s32>(gcadapter_axis));
@ -463,13 +467,13 @@ AnalogMapping GCAdapter::GetAnalogMappingForDevice(const Common::ParamPackage& p
AnalogMapping mapping = {};
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("axis_x", static_cast<int>(PadAxes::StickX));
left_analog_params.Set("axis_y", static_cast<int>(PadAxes::StickY));
mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_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("axis_x", static_cast<int>(PadAxes::SubstickX));
right_analog_params.Set("axis_y", static_cast<int>(PadAxes::SubstickY));
@ -477,9 +481,56 @@ AnalogMapping GCAdapter::GetAnalogMappingForDevice(const Common::ParamPackage& p
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 {
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";

View file

@ -105,8 +105,12 @@ private:
void Reset();
void UpdateVibrations();
// Updates vibration state of all controllers
/// Updates vibration state of all controllers
void SendVibrations();
std::string GetUIButtonName(const Common::ParamPackage& params) const;
std::unique_ptr<LibUSBDeviceHandle> usb_adapter_handle;
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> devices;
devices.emplace_back(Common::ParamPackage{
{"engine", "keyboard"},
{"engine", GetEngineName()},
{"display", "Keyboard Only"},
});
return devices;

View file

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

View file

@ -127,7 +127,7 @@ void Tas::WriteTasFile(std::u8string file_name) {
std::string output_text;
for (size_t frame = 0; frame < record_commands.size(); 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));
}
const auto bytes_written = Common::FS::WriteStringToFile(

View file

@ -27,7 +27,9 @@ void PlayerControlPreview::SetController(Core::HID::EmulatedController* controll
is_controller_set = true;
controller = controller_;
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);
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);
// 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) {
@ -2655,6 +2657,9 @@ void PlayerControlPreview::DrawTriggerButton(QPainter& p, const QPointF center,
}
void PlayerControlPreview::DrawBattery(QPainter& p, QPointF center, Input::BatteryLevel battery) {
if (battery == Input::BatteryLevel::None) {
return;
}
p.setPen(colors.outline);
p.setBrush(colors.transparent);
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() + 28, center.y(), 14, 20);
p.drawRect(center.x() + 14, center.y(), 14, 20);
p.drawRect(center.x(), center.y(), 14, 20);
break;
case Input::BatteryLevel::Medium:
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:
p.drawRect(center.x(), center.y(), 5, 20);
break;
default:
break;
}
}