diff options
Diffstat (limited to 'src/core/hid')
| -rw-r--r-- | src/core/hid/emulated_controller.cpp | 90 | ||||
| -rw-r--r-- | src/core/hid/emulated_controller.h | 7 | ||||
| -rw-r--r-- | src/core/hid/emulated_devices.cpp | 83 | ||||
| -rw-r--r-- | src/core/hid/emulated_devices.h | 29 | ||||
| -rw-r--r-- | src/core/hid/hid_types.h | 26 | ||||
| -rw-r--r-- | src/core/hid/motion_input.cpp | 14 | ||||
| -rw-r--r-- | src/core/hid/motion_input.h | 6 |
7 files changed, 178 insertions, 77 deletions
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index 6d5a3dead..9f0ceca49 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -82,7 +82,12 @@ Settings::ControllerType EmulatedController::MapNPadToSettingsType(NpadStyleInde } void EmulatedController::ReloadFromSettings() { - const auto player_index = NpadIdTypeToIndex(npad_id_type); + if (npad_id_type == NpadIdType::Other) { + ReloadDebugPadFromSettings(); + return; + } + + const auto player_index = NpadIdTypeToConfigIndex(npad_id_type); const auto& player = Settings::values.players.GetValue()[player_index]; for (std::size_t index = 0; index < player.buttons.size(); ++index) { @@ -111,13 +116,21 @@ void EmulatedController::ReloadFromSettings() { ring_params[0] = Common::ParamPackage(Settings::values.ringcon_analogs); - // Other or debug controller should always be a pro controller - if (npad_id_type != NpadIdType::Other) { - SetNpadStyleIndex(MapSettingsTypeToNPad(player.controller_type)); - original_npad_type = npad_type; - } else { - SetNpadStyleIndex(NpadStyleIndex::ProController); - original_npad_type = npad_type; + SetNpadStyleIndex(MapSettingsTypeToNPad(player.controller_type)); + original_npad_type = npad_type; + + // Player 1 shares config with handheld. Disable controller when handheld is selected + if (npad_id_type == NpadIdType::Player1 && npad_type == NpadStyleIndex::Handheld) { + Disconnect(); + ReloadInput(); + return; + } + + // Handheld shares config with player 1. Disable controller when handheld isn't selected + if (npad_id_type == NpadIdType::Handheld && npad_type != NpadStyleIndex::Handheld) { + Disconnect(); + ReloadInput(); + return; } Disconnect(); @@ -128,6 +141,33 @@ void EmulatedController::ReloadFromSettings() { ReloadInput(); } +void EmulatedController::ReloadDebugPadFromSettings() { + for (std::size_t index = 0; index < Settings::values.debug_pad_buttons.size(); ++index) { + button_params[index] = Common::ParamPackage(Settings::values.debug_pad_buttons[index]); + } + for (std::size_t index = 0; index < Settings::values.debug_pad_analogs.size(); ++index) { + stick_params[index] = Common::ParamPackage(Settings::values.debug_pad_analogs[index]); + } + for (std::size_t index = 0; index < motion_params.size(); ++index) { + motion_params[index] = {}; + } + + controller.color_values = {}; + controller.colors_state.fullkey = {}; + controller.colors_state.left = {}; + controller.colors_state.right = {}; + ring_params[0] = {}; + SetNpadStyleIndex(NpadStyleIndex::ProController); + original_npad_type = npad_type; + + Disconnect(); + if (Settings::values.debug_pad_enabled) { + Connect(); + } + + ReloadInput(); +} + void EmulatedController::LoadDevices() { // TODO(german77): Use more buttons to detect the correct device const auto left_joycon = button_params[Settings::NativeButton::DRight]; @@ -363,7 +403,17 @@ void EmulatedController::ReloadInput() { SetMotion(callback, index); }, }); - motion_devices[index]->ForceUpdate(); + + // Restore motion state + auto& emulated_motion = controller.motion_values[index].emulated; + auto& motion = controller.motion_state[index]; + emulated_motion.ResetRotations(); + emulated_motion.ResetQuaternion(); + motion.accel = emulated_motion.GetAcceleration(); + motion.gyro = emulated_motion.GetGyroscope(); + motion.rotation = emulated_motion.GetRotations(); + motion.orientation = emulated_motion.GetOrientation(); + motion.is_at_rest = !emulated_motion.IsMoving(motion_sensitivity); } for (std::size_t index = 0; index < camera_devices.size(); ++index) { @@ -550,9 +600,23 @@ bool EmulatedController::IsConfiguring() const { } void EmulatedController::SaveCurrentConfig() { - const auto player_index = NpadIdTypeToIndex(npad_id_type); + // Other can't alter the config from here + if (npad_id_type == NpadIdType::Other) { + return; + } + + const auto player_index = NpadIdTypeToConfigIndex(npad_id_type); auto& player = Settings::values.players.GetValue()[player_index]; - player.connected = is_connected; + + // Only save the connected status when handheld is connected + if (npad_id_type == NpadIdType::Handheld && npad_type == NpadStyleIndex::Handheld) { + player.connected = is_connected; + } + + if (npad_id_type != NpadIdType::Handheld && npad_type != NpadStyleIndex::Handheld) { + player.connected = is_connected; + } + player.controller_type = MapNPadToSettingsType(npad_type); for (std::size_t index = 0; index < player.buttons.size(); ++index) { player.buttons[index] = button_params[index].Serialize(); @@ -1142,7 +1206,7 @@ bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue v if (!output_devices[device_index]) { return false; } - const auto player_index = NpadIdTypeToIndex(npad_id_type); + const auto player_index = NpadIdTypeToConfigIndex(npad_id_type); const auto& player = Settings::values.players.GetValue()[player_index]; const f32 strength = static_cast<f32>(player.vibration_strength) / 100.0f; @@ -1168,7 +1232,7 @@ bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue v } bool EmulatedController::IsVibrationEnabled(std::size_t device_index) { - const auto player_index = NpadIdTypeToIndex(npad_id_type); + const auto player_index = NpadIdTypeToConfigIndex(npad_id_type); const auto& player = Settings::values.players.GetValue()[player_index]; if (!player.vibration_enabled) { diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index a9da465a2..99572b3bd 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h @@ -250,9 +250,14 @@ public: /// Reload all input devices void ReloadInput(); - /// Overrides current mapped devices with the stored configuration and reloads all input devices + /// Overrides current mapped devices with the stored configuration and reloads all input + /// callbacks void ReloadFromSettings(); + /// Overrides current mapped debug pad with the stored configuration and reloads all input + /// callbacks + void ReloadDebugPadFromSettings(); + /// Saves the current mapped configuration void SaveCurrentConfig(); diff --git a/src/core/hid/emulated_devices.cpp b/src/core/hid/emulated_devices.cpp index 578a6ff61..8e165dded 100644 --- a/src/core/hid/emulated_devices.cpp +++ b/src/core/hid/emulated_devices.cpp @@ -19,52 +19,53 @@ void EmulatedDevices::ReloadFromSettings() { void EmulatedDevices::ReloadInput() { // If you load any device here add the equivalent to the UnloadInput() function + + // Native Mouse is mapped on port 1, pad 0 + const Common::ParamPackage mouse_params{"engine:mouse,port:1,pad:0"}; + + // Keyboard keys is mapped on port 1, pad 0 for normal keys, pad 1 for moddifier keys + const Common::ParamPackage keyboard_params{"engine:keyboard,port:1"}; + std::size_t key_index = 0; for (auto& mouse_device : mouse_button_devices) { - Common::ParamPackage mouse_params; - mouse_params.Set("engine", "mouse"); - mouse_params.Set("button", static_cast<int>(key_index)); - mouse_device = Common::Input::CreateInputDevice(mouse_params); + Common::ParamPackage mouse_button_params = mouse_params; + mouse_button_params.Set("button", static_cast<int>(key_index)); + mouse_device = Common::Input::CreateInputDevice(mouse_button_params); key_index++; } - mouse_stick_device = - Common::Input::CreateInputDeviceFromString("engine:mouse,axis_x:0,axis_y:1"); + Common::ParamPackage mouse_position_params = mouse_params; + mouse_position_params.Set("axis_x", 0); + mouse_position_params.Set("axis_y", 1); + mouse_position_params.Set("deadzone", 0.0f); + mouse_position_params.Set("range", 1.0f); + mouse_position_params.Set("threshold", 0.0f); + mouse_stick_device = Common::Input::CreateInputDevice(mouse_position_params); // First two axis are reserved for mouse position key_index = 2; - for (auto& mouse_device : mouse_analog_devices) { - // Mouse axis are only mapped on port 1, pad 0 - Common::ParamPackage mouse_params; - mouse_params.Set("engine", "mouse"); - mouse_params.Set("axis", static_cast<int>(key_index)); - mouse_params.Set("port", 1); - mouse_params.Set("pad", 0); - mouse_device = Common::Input::CreateInputDevice(mouse_params); + for (auto& mouse_device : mouse_wheel_devices) { + Common::ParamPackage mouse_wheel_params = mouse_params; + mouse_wheel_params.Set("axis", static_cast<int>(key_index)); + mouse_device = Common::Input::CreateInputDevice(mouse_wheel_params); key_index++; } key_index = 0; for (auto& keyboard_device : keyboard_devices) { - // Keyboard keys are only mapped on port 1, pad 0 - Common::ParamPackage keyboard_params; - keyboard_params.Set("engine", "keyboard"); - keyboard_params.Set("button", static_cast<int>(key_index)); - keyboard_params.Set("port", 1); - keyboard_params.Set("pad", 0); - keyboard_device = Common::Input::CreateInputDevice(keyboard_params); + Common::ParamPackage keyboard_key_params = keyboard_params; + keyboard_key_params.Set("button", static_cast<int>(key_index)); + keyboard_key_params.Set("pad", 0); + keyboard_device = Common::Input::CreateInputDevice(keyboard_key_params); key_index++; } key_index = 0; for (auto& keyboard_device : keyboard_modifier_devices) { - // Keyboard moddifiers are only mapped on port 1, pad 1 - Common::ParamPackage keyboard_params; - keyboard_params.Set("engine", "keyboard"); - keyboard_params.Set("button", static_cast<int>(key_index)); - keyboard_params.Set("port", 1); - keyboard_params.Set("pad", 1); - keyboard_device = Common::Input::CreateInputDevice(keyboard_params); + Common::ParamPackage keyboard_moddifier_params = keyboard_params; + keyboard_moddifier_params.Set("button", static_cast<int>(key_index)); + keyboard_moddifier_params.Set("pad", 1); + keyboard_device = Common::Input::CreateInputDevice(keyboard_moddifier_params); key_index++; } @@ -80,14 +81,14 @@ void EmulatedDevices::ReloadInput() { }); } - for (std::size_t index = 0; index < mouse_analog_devices.size(); ++index) { - if (!mouse_analog_devices[index]) { + for (std::size_t index = 0; index < mouse_wheel_devices.size(); ++index) { + if (!mouse_wheel_devices[index]) { continue; } - mouse_analog_devices[index]->SetCallback({ + mouse_wheel_devices[index]->SetCallback({ .on_change = [this, index](const Common::Input::CallbackStatus& callback) { - SetMouseAnalog(callback, index); + SetMouseWheel(callback, index); }, }); } @@ -95,7 +96,9 @@ void EmulatedDevices::ReloadInput() { if (mouse_stick_device) { mouse_stick_device->SetCallback({ .on_change = - [this](const Common::Input::CallbackStatus& callback) { SetMouseStick(callback); }, + [this](const Common::Input::CallbackStatus& callback) { + SetMousePosition(callback); + }, }); } @@ -128,7 +131,7 @@ void EmulatedDevices::UnloadInput() { for (auto& button : mouse_button_devices) { button.reset(); } - for (auto& analog : mouse_analog_devices) { + for (auto& analog : mouse_wheel_devices) { analog.reset(); } mouse_stick_device.reset(); @@ -362,18 +365,18 @@ void EmulatedDevices::SetMouseButton(const Common::Input::CallbackStatus& callba TriggerOnChange(DeviceTriggerType::Mouse); } -void EmulatedDevices::SetMouseAnalog(const Common::Input::CallbackStatus& callback, - std::size_t index) { - if (index >= device_status.mouse_analog_values.size()) { +void EmulatedDevices::SetMouseWheel(const Common::Input::CallbackStatus& callback, + std::size_t index) { + if (index >= device_status.mouse_wheel_values.size()) { return; } std::unique_lock lock{mutex}; const auto analog_value = TransformToAnalog(callback); - device_status.mouse_analog_values[index] = analog_value; + device_status.mouse_wheel_values[index] = analog_value; if (is_configuring) { - device_status.mouse_position_state = {}; + device_status.mouse_wheel_state = {}; lock.unlock(); TriggerOnChange(DeviceTriggerType::Mouse); return; @@ -392,7 +395,7 @@ void EmulatedDevices::SetMouseAnalog(const Common::Input::CallbackStatus& callba TriggerOnChange(DeviceTriggerType::Mouse); } -void EmulatedDevices::SetMouseStick(const Common::Input::CallbackStatus& callback) { +void EmulatedDevices::SetMousePosition(const Common::Input::CallbackStatus& callback) { std::unique_lock lock{mutex}; const auto touch_value = TransformToTouch(callback); diff --git a/src/core/hid/emulated_devices.h b/src/core/hid/emulated_devices.h index 76f9150df..caf2ca659 100644 --- a/src/core/hid/emulated_devices.h +++ b/src/core/hid/emulated_devices.h @@ -23,8 +23,8 @@ using KeyboardModifierDevices = std::array<std::unique_ptr<Common::Input::InputD Settings::NativeKeyboard::NumKeyboardMods>; using MouseButtonDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMouseButton::NumMouseButtons>; -using MouseAnalogDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, - Settings::NativeMouseWheel::NumMouseWheels>; +using MouseWheelDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, + Settings::NativeMouseWheel::NumMouseWheels>; using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>; using MouseButtonParams = @@ -36,7 +36,7 @@ using KeyboardModifierValues = std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>; using MouseButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>; -using MouseAnalogValues = +using MouseWheelValues = std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>; using MouseStickValue = Common::Input::TouchStatus; @@ -50,7 +50,7 @@ struct DeviceStatus { KeyboardValues keyboard_values{}; KeyboardModifierValues keyboard_moddifier_values{}; MouseButtonValues mouse_button_values{}; - MouseAnalogValues mouse_analog_values{}; + MouseWheelValues mouse_wheel_values{}; MouseStickValue mouse_stick_value{}; // Data for HID serices @@ -111,15 +111,6 @@ public: /// Reverts any mapped changes made that weren't saved void RestoreConfig(); - // Returns the current mapped ring device - Common::ParamPackage GetRingParam() const; - - /** - * Updates the current mapped ring device - * @param param ParamPackage with ring sensor data to be mapped - */ - void SetRingParam(Common::ParamPackage param); - /// Returns the latest status of button input from the keyboard with parameters KeyboardValues GetKeyboardValues() const; @@ -187,19 +178,13 @@ private: * @param callback A CallbackStatus containing the wheel status * @param index wheel ID to be updated */ - void SetMouseAnalog(const Common::Input::CallbackStatus& callback, std::size_t index); + void SetMouseWheel(const Common::Input::CallbackStatus& callback, std::size_t index); /** * Updates the mouse position status of the mouse device * @param callback A CallbackStatus containing the position status */ - void SetMouseStick(const Common::Input::CallbackStatus& callback); - - /** - * Updates the ring analog sensor status of the ring controller - * @param callback A CallbackStatus containing the force status - */ - void SetRingAnalog(const Common::Input::CallbackStatus& callback); + void SetMousePosition(const Common::Input::CallbackStatus& callback); /** * Triggers a callback that something has changed on the device status @@ -212,7 +197,7 @@ private: KeyboardDevices keyboard_devices; KeyboardModifierDevices keyboard_modifier_devices; MouseButtonDevices mouse_button_devices; - MouseAnalogDevices mouse_analog_devices; + MouseWheelDevices mouse_wheel_devices; MouseStickDevice mouse_stick_device; mutable std::mutex mutex; diff --git a/src/core/hid/hid_types.h b/src/core/hid/hid_types.h index 6b35f448c..983f0addd 100644 --- a/src/core/hid/hid_types.h +++ b/src/core/hid/hid_types.h @@ -690,6 +690,32 @@ constexpr size_t NpadIdTypeToIndex(NpadIdType npad_id_type) { } } +/// Converts a NpadIdType to a config array index. +constexpr size_t NpadIdTypeToConfigIndex(NpadIdType npad_id_type) { + switch (npad_id_type) { + case NpadIdType::Player1: + return 0; + case NpadIdType::Player2: + return 1; + case NpadIdType::Player3: + return 2; + case NpadIdType::Player4: + return 3; + case NpadIdType::Player5: + return 4; + case NpadIdType::Player6: + return 5; + case NpadIdType::Player7: + return 6; + case NpadIdType::Player8: + return 7; + case NpadIdType::Other: + case NpadIdType::Handheld: + default: + return 0; + } +} + /// Converts an array index to a NpadIdType constexpr NpadIdType IndexToNpadIdType(size_t index) { switch (index) { diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp index eef6edf4b..0dd66c1cc 100644 --- a/src/core/hid/motion_input.cpp +++ b/src/core/hid/motion_input.cpp @@ -10,6 +10,8 @@ MotionInput::MotionInput() { // Initialize PID constants with default values SetPID(0.3f, 0.005f, 0.0f); SetGyroThreshold(ThresholdStandard); + ResetQuaternion(); + ResetRotations(); } void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { @@ -20,11 +22,19 @@ void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { accel = acceleration; + + accel.x = std::clamp(accel.x, -AccelMaxValue, AccelMaxValue); + accel.y = std::clamp(accel.y, -AccelMaxValue, AccelMaxValue); + accel.z = std::clamp(accel.z, -AccelMaxValue, AccelMaxValue); } void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { gyro = gyroscope - gyro_bias; + gyro.x = std::clamp(gyro.x, -GyroMaxValue, GyroMaxValue); + gyro.y = std::clamp(gyro.y, -GyroMaxValue, GyroMaxValue); + gyro.z = std::clamp(gyro.z, -GyroMaxValue, GyroMaxValue); + // Auto adjust drift to minimize drift if (!IsMoving(IsAtRestRelaxed)) { gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f); @@ -61,6 +71,10 @@ void MotionInput::ResetRotations() { rotations = {}; } +void MotionInput::ResetQuaternion() { + quat = {{0.0f, 0.0f, -1.0f}, 0.0f}; +} + bool MotionInput::IsMoving(f32 sensitivity) const { return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f; } diff --git a/src/core/hid/motion_input.h b/src/core/hid/motion_input.h index 9180bb9aa..e2c1bbf95 100644 --- a/src/core/hid/motion_input.h +++ b/src/core/hid/motion_input.h @@ -20,6 +20,9 @@ public: static constexpr float IsAtRestStandard = 0.01f; static constexpr float IsAtRestThight = 0.005f; + static constexpr float GyroMaxValue = 5.0f; + static constexpr float AccelMaxValue = 7.0f; + explicit MotionInput(); MotionInput(const MotionInput&) = default; @@ -40,6 +43,7 @@ public: void EnableReset(bool reset); void ResetRotations(); + void ResetQuaternion(); void UpdateRotation(u64 elapsed_time); void UpdateOrientation(u64 elapsed_time); @@ -69,7 +73,7 @@ private: Common::Vec3f derivative_error; // Quaternion containing the device orientation - Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f}; + Common::Quaternion<f32> quat; // Number of full rotations in each axis Common::Vec3f rotations; |
