diff options
| author | 2024-02-05 17:07:50 -0600 | |
|---|---|---|
| committer | 2024-02-05 17:19:31 -0600 | |
| commit | 8f192b494ae1628a9c91007691123eb9ac9c759e (patch) | |
| tree | 848150eec872a41a05842ef35d0c271032b31138 /src/hid_core/resources | |
| parent | service: hid: Ensure aruid data is initialized (diff) | |
| download | yuzu-8f192b494ae1628a9c91007691123eb9ac9c759e.tar.gz yuzu-8f192b494ae1628a9c91007691123eb9ac9c759e.tar.xz yuzu-8f192b494ae1628a9c91007691123eb9ac9c759e.zip | |
service: hid: Add multiprocess support to six axis input
Diffstat (limited to 'src/hid_core/resources')
| -rw-r--r-- | src/hid_core/resources/six_axis/six_axis.cpp | 254 |
1 files changed, 130 insertions, 124 deletions
diff --git a/src/hid_core/resources/six_axis/six_axis.cpp b/src/hid_core/resources/six_axis/six_axis.cpp index abb6fd152..b407a5c76 100644 --- a/src/hid_core/resources/six_axis/six_axis.cpp +++ b/src/hid_core/resources/six_axis/six_axis.cpp | |||
| @@ -28,142 +28,148 @@ void SixAxis::OnRelease() {} | |||
| 28 | 28 | ||
| 29 | void SixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { | 29 | void SixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { |
| 30 | std::scoped_lock shared_lock{*shared_mutex}; | 30 | std::scoped_lock shared_lock{*shared_mutex}; |
| 31 | const u64 aruid = applet_resource->GetActiveAruid(); | ||
| 32 | auto* data = applet_resource->GetAruidData(aruid); | ||
| 33 | 31 | ||
| 34 | if (data == nullptr || !data->flag.is_assigned) { | 32 | for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) { |
| 35 | return; | 33 | const auto* data = applet_resource->GetAruidDataByIndex(aruid_index); |
| 36 | } | ||
| 37 | |||
| 38 | if (!IsControllerActivated()) { | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | 34 | ||
| 42 | for (std::size_t i = 0; i < controller_data.size(); ++i) { | 35 | if (data == nullptr || !data->flag.is_assigned) { |
| 43 | NpadSharedMemoryEntry& shared_memory = data->shared_memory_format->npad.npad_entry[i]; | ||
| 44 | auto& controller = controller_data[i]; | ||
| 45 | const auto& controller_type = controller.device->GetNpadStyleIndex(); | ||
| 46 | |||
| 47 | if (controller_type == Core::HID::NpadStyleIndex::None || | ||
| 48 | !controller.device->IsConnected()) { | ||
| 49 | continue; | 36 | continue; |
| 50 | } | 37 | } |
| 51 | 38 | ||
| 52 | const auto& motion_state = controller.device->GetMotions(); | 39 | if (!IsControllerActivated()) { |
| 53 | auto& sixaxis_fullkey_state = controller.sixaxis_fullkey_state; | 40 | return; |
| 54 | auto& sixaxis_handheld_state = controller.sixaxis_handheld_state; | ||
| 55 | auto& sixaxis_dual_left_state = controller.sixaxis_dual_left_state; | ||
| 56 | auto& sixaxis_dual_right_state = controller.sixaxis_dual_right_state; | ||
| 57 | auto& sixaxis_left_lifo_state = controller.sixaxis_left_lifo_state; | ||
| 58 | auto& sixaxis_right_lifo_state = controller.sixaxis_right_lifo_state; | ||
| 59 | |||
| 60 | auto& sixaxis_fullkey_lifo = shared_memory.internal_state.sixaxis_fullkey_lifo; | ||
| 61 | auto& sixaxis_handheld_lifo = shared_memory.internal_state.sixaxis_handheld_lifo; | ||
| 62 | auto& sixaxis_dual_left_lifo = shared_memory.internal_state.sixaxis_dual_left_lifo; | ||
| 63 | auto& sixaxis_dual_right_lifo = shared_memory.internal_state.sixaxis_dual_right_lifo; | ||
| 64 | auto& sixaxis_left_lifo = shared_memory.internal_state.sixaxis_left_lifo; | ||
| 65 | auto& sixaxis_right_lifo = shared_memory.internal_state.sixaxis_right_lifo; | ||
| 66 | |||
| 67 | // Clear previous state | ||
| 68 | sixaxis_fullkey_state = {}; | ||
| 69 | sixaxis_handheld_state = {}; | ||
| 70 | sixaxis_dual_left_state = {}; | ||
| 71 | sixaxis_dual_right_state = {}; | ||
| 72 | sixaxis_left_lifo_state = {}; | ||
| 73 | sixaxis_right_lifo_state = {}; | ||
| 74 | |||
| 75 | if (controller.sixaxis_sensor_enabled && Settings::values.motion_enabled.GetValue()) { | ||
| 76 | controller.sixaxis_at_rest = true; | ||
| 77 | for (std::size_t e = 0; e < motion_state.size(); ++e) { | ||
| 78 | controller.sixaxis_at_rest = | ||
| 79 | controller.sixaxis_at_rest && motion_state[e].is_at_rest; | ||
| 80 | } | ||
| 81 | } | 41 | } |
| 82 | 42 | ||
| 83 | const auto set_motion_state = [&](Core::HID::SixAxisSensorState& state, | 43 | for (std::size_t i = 0; i < controller_data.size(); ++i) { |
| 84 | const Core::HID::ControllerMotion& hid_state) { | 44 | NpadSharedMemoryEntry& shared_memory = data->shared_memory_format->npad.npad_entry[i]; |
| 85 | using namespace std::literals::chrono_literals; | 45 | auto& controller = controller_data[i]; |
| 86 | static constexpr Core::HID::SixAxisSensorState default_motion_state = { | 46 | const auto& controller_type = controller.device->GetNpadStyleIndex(); |
| 87 | .delta_time = std::chrono::nanoseconds(5ms).count(), | 47 | |
| 88 | .accel = {0, 0, -1.0f}, | 48 | if (!data->flag.enable_six_axis_sensor) { |
| 89 | .orientation = | 49 | continue; |
| 90 | { | 50 | } |
| 91 | Common::Vec3f{1.0f, 0, 0}, | 51 | |
| 92 | Common::Vec3f{0, 1.0f, 0}, | 52 | if (controller_type == Core::HID::NpadStyleIndex::None || |
| 93 | Common::Vec3f{0, 0, 1.0f}, | 53 | !controller.device->IsConnected()) { |
| 94 | }, | 54 | continue; |
| 95 | .attribute = {1}, | 55 | } |
| 56 | |||
| 57 | const auto& motion_state = controller.device->GetMotions(); | ||
| 58 | auto& sixaxis_fullkey_state = controller.sixaxis_fullkey_state; | ||
| 59 | auto& sixaxis_handheld_state = controller.sixaxis_handheld_state; | ||
| 60 | auto& sixaxis_dual_left_state = controller.sixaxis_dual_left_state; | ||
| 61 | auto& sixaxis_dual_right_state = controller.sixaxis_dual_right_state; | ||
| 62 | auto& sixaxis_left_lifo_state = controller.sixaxis_left_lifo_state; | ||
| 63 | auto& sixaxis_right_lifo_state = controller.sixaxis_right_lifo_state; | ||
| 64 | |||
| 65 | auto& sixaxis_fullkey_lifo = shared_memory.internal_state.sixaxis_fullkey_lifo; | ||
| 66 | auto& sixaxis_handheld_lifo = shared_memory.internal_state.sixaxis_handheld_lifo; | ||
| 67 | auto& sixaxis_dual_left_lifo = shared_memory.internal_state.sixaxis_dual_left_lifo; | ||
| 68 | auto& sixaxis_dual_right_lifo = shared_memory.internal_state.sixaxis_dual_right_lifo; | ||
| 69 | auto& sixaxis_left_lifo = shared_memory.internal_state.sixaxis_left_lifo; | ||
| 70 | auto& sixaxis_right_lifo = shared_memory.internal_state.sixaxis_right_lifo; | ||
| 71 | |||
| 72 | // Clear previous state | ||
| 73 | sixaxis_fullkey_state = {}; | ||
| 74 | sixaxis_handheld_state = {}; | ||
| 75 | sixaxis_dual_left_state = {}; | ||
| 76 | sixaxis_dual_right_state = {}; | ||
| 77 | sixaxis_left_lifo_state = {}; | ||
| 78 | sixaxis_right_lifo_state = {}; | ||
| 79 | |||
| 80 | if (controller.sixaxis_sensor_enabled && Settings::values.motion_enabled.GetValue()) { | ||
| 81 | controller.sixaxis_at_rest = true; | ||
| 82 | for (std::size_t e = 0; e < motion_state.size(); ++e) { | ||
| 83 | controller.sixaxis_at_rest = | ||
| 84 | controller.sixaxis_at_rest && motion_state[e].is_at_rest; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | const auto set_motion_state = [&](Core::HID::SixAxisSensorState& state, | ||
| 89 | const Core::HID::ControllerMotion& hid_state) { | ||
| 90 | using namespace std::literals::chrono_literals; | ||
| 91 | static constexpr Core::HID::SixAxisSensorState default_motion_state = { | ||
| 92 | .delta_time = std::chrono::nanoseconds(5ms).count(), | ||
| 93 | .accel = {0, 0, -1.0f}, | ||
| 94 | .orientation = | ||
| 95 | { | ||
| 96 | Common::Vec3f{1.0f, 0, 0}, | ||
| 97 | Common::Vec3f{0, 1.0f, 0}, | ||
| 98 | Common::Vec3f{0, 0, 1.0f}, | ||
| 99 | }, | ||
| 100 | .attribute = {1}, | ||
| 101 | }; | ||
| 102 | if (!controller.sixaxis_sensor_enabled) { | ||
| 103 | state = default_motion_state; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | if (!Settings::values.motion_enabled.GetValue()) { | ||
| 107 | state = default_motion_state; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | state.attribute.is_connected.Assign(1); | ||
| 111 | state.delta_time = std::chrono::nanoseconds(5ms).count(); | ||
| 112 | state.accel = hid_state.accel; | ||
| 113 | state.gyro = hid_state.gyro; | ||
| 114 | state.rotation = hid_state.rotation; | ||
| 115 | state.orientation = hid_state.orientation; | ||
| 96 | }; | 116 | }; |
| 97 | if (!controller.sixaxis_sensor_enabled) { | 117 | |
| 98 | state = default_motion_state; | 118 | switch (controller_type) { |
| 99 | return; | 119 | case Core::HID::NpadStyleIndex::None: |
| 120 | ASSERT(false); | ||
| 121 | break; | ||
| 122 | case Core::HID::NpadStyleIndex::Fullkey: | ||
| 123 | set_motion_state(sixaxis_fullkey_state, motion_state[0]); | ||
| 124 | break; | ||
| 125 | case Core::HID::NpadStyleIndex::Handheld: | ||
| 126 | set_motion_state(sixaxis_handheld_state, motion_state[0]); | ||
| 127 | break; | ||
| 128 | case Core::HID::NpadStyleIndex::JoyconDual: | ||
| 129 | set_motion_state(sixaxis_dual_left_state, motion_state[0]); | ||
| 130 | set_motion_state(sixaxis_dual_right_state, motion_state[1]); | ||
| 131 | break; | ||
| 132 | case Core::HID::NpadStyleIndex::JoyconLeft: | ||
| 133 | set_motion_state(sixaxis_left_lifo_state, motion_state[0]); | ||
| 134 | break; | ||
| 135 | case Core::HID::NpadStyleIndex::JoyconRight: | ||
| 136 | set_motion_state(sixaxis_right_lifo_state, motion_state[1]); | ||
| 137 | break; | ||
| 138 | case Core::HID::NpadStyleIndex::Pokeball: | ||
| 139 | using namespace std::literals::chrono_literals; | ||
| 140 | set_motion_state(sixaxis_fullkey_state, motion_state[0]); | ||
| 141 | sixaxis_fullkey_state.delta_time = std::chrono::nanoseconds(15ms).count(); | ||
| 142 | break; | ||
| 143 | default: | ||
| 144 | break; | ||
| 100 | } | 145 | } |
| 101 | if (!Settings::values.motion_enabled.GetValue()) { | 146 | |
| 102 | state = default_motion_state; | 147 | sixaxis_fullkey_state.sampling_number = |
| 103 | return; | 148 | sixaxis_fullkey_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; |
| 149 | sixaxis_handheld_state.sampling_number = | ||
| 150 | sixaxis_handheld_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 151 | sixaxis_dual_left_state.sampling_number = | ||
| 152 | sixaxis_dual_left_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 153 | sixaxis_dual_right_state.sampling_number = | ||
| 154 | sixaxis_dual_right_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 155 | sixaxis_left_lifo_state.sampling_number = | ||
| 156 | sixaxis_left_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 157 | sixaxis_right_lifo_state.sampling_number = | ||
| 158 | sixaxis_right_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 159 | |||
| 160 | if (IndexToNpadIdType(i) == Core::HID::NpadIdType::Handheld) { | ||
| 161 | // This buffer only is updated on handheld on HW | ||
| 162 | sixaxis_handheld_lifo.lifo.WriteNextEntry(sixaxis_handheld_state); | ||
| 163 | } else { | ||
| 164 | // Handheld doesn't update this buffer on HW | ||
| 165 | sixaxis_fullkey_lifo.lifo.WriteNextEntry(sixaxis_fullkey_state); | ||
| 104 | } | 166 | } |
| 105 | state.attribute.is_connected.Assign(1); | ||
| 106 | state.delta_time = std::chrono::nanoseconds(5ms).count(); | ||
| 107 | state.accel = hid_state.accel; | ||
| 108 | state.gyro = hid_state.gyro; | ||
| 109 | state.rotation = hid_state.rotation; | ||
| 110 | state.orientation = hid_state.orientation; | ||
| 111 | }; | ||
| 112 | |||
| 113 | switch (controller_type) { | ||
| 114 | case Core::HID::NpadStyleIndex::None: | ||
| 115 | ASSERT(false); | ||
| 116 | break; | ||
| 117 | case Core::HID::NpadStyleIndex::Fullkey: | ||
| 118 | set_motion_state(sixaxis_fullkey_state, motion_state[0]); | ||
| 119 | break; | ||
| 120 | case Core::HID::NpadStyleIndex::Handheld: | ||
| 121 | set_motion_state(sixaxis_handheld_state, motion_state[0]); | ||
| 122 | break; | ||
| 123 | case Core::HID::NpadStyleIndex::JoyconDual: | ||
| 124 | set_motion_state(sixaxis_dual_left_state, motion_state[0]); | ||
| 125 | set_motion_state(sixaxis_dual_right_state, motion_state[1]); | ||
| 126 | break; | ||
| 127 | case Core::HID::NpadStyleIndex::JoyconLeft: | ||
| 128 | set_motion_state(sixaxis_left_lifo_state, motion_state[0]); | ||
| 129 | break; | ||
| 130 | case Core::HID::NpadStyleIndex::JoyconRight: | ||
| 131 | set_motion_state(sixaxis_right_lifo_state, motion_state[1]); | ||
| 132 | break; | ||
| 133 | case Core::HID::NpadStyleIndex::Pokeball: | ||
| 134 | using namespace std::literals::chrono_literals; | ||
| 135 | set_motion_state(sixaxis_fullkey_state, motion_state[0]); | ||
| 136 | sixaxis_fullkey_state.delta_time = std::chrono::nanoseconds(15ms).count(); | ||
| 137 | break; | ||
| 138 | default: | ||
| 139 | break; | ||
| 140 | } | ||
| 141 | 167 | ||
| 142 | sixaxis_fullkey_state.sampling_number = | 168 | sixaxis_dual_left_lifo.lifo.WriteNextEntry(sixaxis_dual_left_state); |
| 143 | sixaxis_fullkey_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | 169 | sixaxis_dual_right_lifo.lifo.WriteNextEntry(sixaxis_dual_right_state); |
| 144 | sixaxis_handheld_state.sampling_number = | 170 | sixaxis_left_lifo.lifo.WriteNextEntry(sixaxis_left_lifo_state); |
| 145 | sixaxis_handheld_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | 171 | sixaxis_right_lifo.lifo.WriteNextEntry(sixaxis_right_lifo_state); |
| 146 | sixaxis_dual_left_state.sampling_number = | ||
| 147 | sixaxis_dual_left_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 148 | sixaxis_dual_right_state.sampling_number = | ||
| 149 | sixaxis_dual_right_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 150 | sixaxis_left_lifo_state.sampling_number = | ||
| 151 | sixaxis_left_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 152 | sixaxis_right_lifo_state.sampling_number = | ||
| 153 | sixaxis_right_lifo.lifo.ReadCurrentEntry().state.sampling_number + 1; | ||
| 154 | |||
| 155 | if (IndexToNpadIdType(i) == Core::HID::NpadIdType::Handheld) { | ||
| 156 | // This buffer only is updated on handheld on HW | ||
| 157 | sixaxis_handheld_lifo.lifo.WriteNextEntry(sixaxis_handheld_state); | ||
| 158 | } else { | ||
| 159 | // Handheld doesn't update this buffer on HW | ||
| 160 | sixaxis_fullkey_lifo.lifo.WriteNextEntry(sixaxis_fullkey_state); | ||
| 161 | } | 172 | } |
| 162 | |||
| 163 | sixaxis_dual_left_lifo.lifo.WriteNextEntry(sixaxis_dual_left_state); | ||
| 164 | sixaxis_dual_right_lifo.lifo.WriteNextEntry(sixaxis_dual_right_state); | ||
| 165 | sixaxis_left_lifo.lifo.WriteNextEntry(sixaxis_left_lifo_state); | ||
| 166 | sixaxis_right_lifo.lifo.WriteNextEntry(sixaxis_right_lifo_state); | ||
| 167 | } | 173 | } |
| 168 | } | 174 | } |
| 169 | 175 | ||