diff options
| author | 2021-09-20 16:34:05 -0500 | |
|---|---|---|
| committer | 2021-11-24 20:30:22 -0600 | |
| commit | ea7b1fbc673e40d5e51f2d185ebe8542741164fa (patch) | |
| tree | f5bbd57f06b71d7fdd34c373d4b0032154b55d42 /src/input_common/input_engine.cpp | |
| parent | core/hid: Move motion_input, create input converter and hid_types (diff) | |
| download | yuzu-ea7b1fbc673e40d5e51f2d185ebe8542741164fa.tar.gz yuzu-ea7b1fbc673e40d5e51f2d185ebe8542741164fa.tar.xz yuzu-ea7b1fbc673e40d5e51f2d185ebe8542741164fa.zip | |
input_common: Create input_engine
Diffstat (limited to 'src/input_common/input_engine.cpp')
| -rw-r--r-- | src/input_common/input_engine.cpp | 361 |
1 files changed, 361 insertions, 0 deletions
diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp new file mode 100644 index 000000000..1534f24b0 --- /dev/null +++ b/src/input_common/input_engine.cpp | |||
| @@ -0,0 +1,361 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included | ||
| 4 | |||
| 5 | #include "common/logging/log.h" | ||
| 6 | #include "common/param_package.h" | ||
| 7 | #include "input_common/input_engine.h" | ||
| 8 | |||
| 9 | namespace InputCommon { | ||
| 10 | |||
| 11 | void InputEngine::PreSetController(const PadIdentifier& identifier) { | ||
| 12 | std::lock_guard lock{mutex}; | ||
| 13 | if (!controller_list.contains(identifier)) { | ||
| 14 | controller_list.insert_or_assign(identifier, ControllerData{}); | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) { | ||
| 19 | std::lock_guard lock{mutex}; | ||
| 20 | ControllerData& controller = controller_list.at(identifier); | ||
| 21 | if (!controller.buttons.contains(button)) { | ||
| 22 | controller.buttons.insert_or_assign(button, false); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) { | ||
| 27 | std::lock_guard lock{mutex}; | ||
| 28 | ControllerData& controller = controller_list.at(identifier); | ||
| 29 | if (!controller.hat_buttons.contains(button)) { | ||
| 30 | controller.hat_buttons.insert_or_assign(button, u8{0}); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) { | ||
| 35 | std::lock_guard lock{mutex}; | ||
| 36 | ControllerData& controller = controller_list.at(identifier); | ||
| 37 | if (!controller.axes.contains(axis)) { | ||
| 38 | controller.axes.insert_or_assign(axis, 0.0f); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) { | ||
| 43 | std::lock_guard lock{mutex}; | ||
| 44 | ControllerData& controller = controller_list.at(identifier); | ||
| 45 | if (!controller.motions.contains(motion)) { | ||
| 46 | controller.motions.insert_or_assign(motion, BasicMotion{}); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) { | ||
| 51 | { | ||
| 52 | std::lock_guard lock{mutex}; | ||
| 53 | ControllerData& controller = controller_list.at(identifier); | ||
| 54 | if (!configuring) { | ||
| 55 | controller.buttons.insert_or_assign(button, value); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | TriggerOnButtonChange(identifier, button, value); | ||
| 59 | } | ||
| 60 | |||
| 61 | void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) { | ||
| 62 | { | ||
| 63 | std::lock_guard lock{mutex}; | ||
| 64 | ControllerData& controller = controller_list.at(identifier); | ||
| 65 | if (!configuring) { | ||
| 66 | controller.hat_buttons.insert_or_assign(button, value); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | TriggerOnHatButtonChange(identifier, button, value); | ||
| 70 | } | ||
| 71 | |||
| 72 | void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) { | ||
| 73 | { | ||
| 74 | std::lock_guard lock{mutex}; | ||
| 75 | ControllerData& controller = controller_list.at(identifier); | ||
| 76 | if (!configuring) { | ||
| 77 | controller.axes.insert_or_assign(axis, value); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | TriggerOnAxisChange(identifier, axis, value); | ||
| 81 | } | ||
| 82 | |||
| 83 | void InputEngine::SetBattery(const PadIdentifier& identifier, BatteryLevel value) { | ||
| 84 | { | ||
| 85 | std::lock_guard lock{mutex}; | ||
| 86 | ControllerData& controller = controller_list.at(identifier); | ||
| 87 | if (!configuring) { | ||
| 88 | controller.battery = value; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | TriggerOnBatteryChange(identifier, value); | ||
| 92 | } | ||
| 93 | |||
| 94 | void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, BasicMotion value) { | ||
| 95 | { | ||
| 96 | std::lock_guard lock{mutex}; | ||
| 97 | ControllerData& controller = controller_list.at(identifier); | ||
| 98 | if (!configuring) { | ||
| 99 | controller.motions.insert_or_assign(motion, value); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | TriggerOnMotionChange(identifier, motion, value); | ||
| 103 | } | ||
| 104 | |||
| 105 | bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const { | ||
| 106 | std::lock_guard lock{mutex}; | ||
| 107 | if (!controller_list.contains(identifier)) { | ||
| 108 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), | ||
| 109 | identifier.pad, identifier.port); | ||
| 110 | return false; | ||
| 111 | } | ||
| 112 | ControllerData controller = controller_list.at(identifier); | ||
| 113 | if (!controller.buttons.contains(button)) { | ||
| 114 | LOG_ERROR(Input, "Invalid button {}", button); | ||
| 115 | return false; | ||
| 116 | } | ||
| 117 | return controller.buttons.at(button); | ||
| 118 | } | ||
| 119 | |||
| 120 | bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const { | ||
| 121 | std::lock_guard lock{mutex}; | ||
| 122 | if (!controller_list.contains(identifier)) { | ||
| 123 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), | ||
| 124 | identifier.pad, identifier.port); | ||
| 125 | return false; | ||
| 126 | } | ||
| 127 | ControllerData controller = controller_list.at(identifier); | ||
| 128 | if (!controller.hat_buttons.contains(button)) { | ||
| 129 | LOG_ERROR(Input, "Invalid hat button {}", button); | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | return (controller.hat_buttons.at(button) & direction) != 0; | ||
| 133 | } | ||
| 134 | |||
| 135 | f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const { | ||
| 136 | std::lock_guard lock{mutex}; | ||
| 137 | if (!controller_list.contains(identifier)) { | ||
| 138 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), | ||
| 139 | identifier.pad, identifier.port); | ||
| 140 | return 0.0f; | ||
| 141 | } | ||
| 142 | ControllerData controller = controller_list.at(identifier); | ||
| 143 | if (!controller.axes.contains(axis)) { | ||
| 144 | LOG_ERROR(Input, "Invalid axis {}", axis); | ||
| 145 | return 0.0f; | ||
| 146 | } | ||
| 147 | return controller.axes.at(axis); | ||
| 148 | } | ||
| 149 | |||
| 150 | BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const { | ||
| 151 | std::lock_guard lock{mutex}; | ||
| 152 | if (!controller_list.contains(identifier)) { | ||
| 153 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), | ||
| 154 | identifier.pad, identifier.port); | ||
| 155 | return BatteryLevel::Charging; | ||
| 156 | } | ||
| 157 | ControllerData controller = controller_list.at(identifier); | ||
| 158 | return controller.battery; | ||
| 159 | } | ||
| 160 | |||
| 161 | BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const { | ||
| 162 | std::lock_guard lock{mutex}; | ||
| 163 | if (!controller_list.contains(identifier)) { | ||
| 164 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), | ||
| 165 | identifier.pad, identifier.port); | ||
| 166 | return {}; | ||
| 167 | } | ||
| 168 | ControllerData controller = controller_list.at(identifier); | ||
| 169 | return controller.motions.at(motion); | ||
| 170 | } | ||
| 171 | |||
| 172 | void InputEngine::ResetButtonState() { | ||
| 173 | for (std::pair<PadIdentifier, ControllerData> controller : controller_list) { | ||
| 174 | for (std::pair<int, bool> button : controller.second.buttons) { | ||
| 175 | SetButton(controller.first, button.first, false); | ||
| 176 | } | ||
| 177 | for (std::pair<int, bool> button : controller.second.hat_buttons) { | ||
| 178 | SetHatButton(controller.first, button.first, false); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | void InputEngine::ResetAnalogState() { | ||
| 184 | for (std::pair<PadIdentifier, ControllerData> controller : controller_list) { | ||
| 185 | for (std::pair<int, float> axis : controller.second.axes) { | ||
| 186 | SetAxis(controller.first, axis.first, 0.0); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) { | ||
| 192 | std::lock_guard lock{mutex_callback}; | ||
| 193 | for (const std::pair<int, InputIdentifier> poller_pair : callback_list) { | ||
| 194 | const InputIdentifier& poller = poller_pair.second; | ||
| 195 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) { | ||
| 196 | continue; | ||
| 197 | } | ||
| 198 | if (poller.callback.on_change) { | ||
| 199 | poller.callback.on_change(); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | if (!configuring || !mapping_callback.on_data) { | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | if (value == GetButton(identifier, button)) { | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | mapping_callback.on_data(MappingData{ | ||
| 209 | .engine = GetEngineName(), | ||
| 210 | .pad = identifier, | ||
| 211 | .type = EngineInputType::Button, | ||
| 212 | .index = button, | ||
| 213 | .button_value = value, | ||
| 214 | }); | ||
| 215 | } | ||
| 216 | |||
| 217 | void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) { | ||
| 218 | std::lock_guard lock{mutex_callback}; | ||
| 219 | for (const std::pair<int, InputIdentifier> poller_pair : callback_list) { | ||
| 220 | const InputIdentifier& poller = poller_pair.second; | ||
| 221 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) { | ||
| 222 | continue; | ||
| 223 | } | ||
| 224 | if (poller.callback.on_change) { | ||
| 225 | poller.callback.on_change(); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | if (!configuring || !mapping_callback.on_data) { | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | for (std::size_t index = 1; index < 0xff; index <<= 1) { | ||
| 232 | bool button_value = (value & index) != 0; | ||
| 233 | if (button_value == GetHatButton(identifier, button, static_cast<u8>(index))) { | ||
| 234 | continue; | ||
| 235 | } | ||
| 236 | mapping_callback.on_data(MappingData{ | ||
| 237 | .engine = GetEngineName(), | ||
| 238 | .pad = identifier, | ||
| 239 | .type = EngineInputType::HatButton, | ||
| 240 | .index = button, | ||
| 241 | .hat_name = GetHatButtonName(static_cast<u8>(index)), | ||
| 242 | }); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) { | ||
| 247 | std::lock_guard lock{mutex_callback}; | ||
| 248 | for (const std::pair<int, InputIdentifier> poller_pair : callback_list) { | ||
| 249 | const InputIdentifier& poller = poller_pair.second; | ||
| 250 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) { | ||
| 251 | continue; | ||
| 252 | } | ||
| 253 | if (poller.callback.on_change) { | ||
| 254 | poller.callback.on_change(); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | if (!configuring || !mapping_callback.on_data) { | ||
| 258 | return; | ||
| 259 | } | ||
| 260 | if (std::abs(value - GetAxis(identifier, axis)) < 0.5f) { | ||
| 261 | return; | ||
| 262 | } | ||
| 263 | mapping_callback.on_data(MappingData{ | ||
| 264 | .engine = GetEngineName(), | ||
| 265 | .pad = identifier, | ||
| 266 | .type = EngineInputType::Analog, | ||
| 267 | .index = axis, | ||
| 268 | .axis_value = value, | ||
| 269 | }); | ||
| 270 | } | ||
| 271 | |||
| 272 | void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, | ||
| 273 | [[maybe_unused]] BatteryLevel value) { | ||
| 274 | std::lock_guard lock{mutex_callback}; | ||
| 275 | for (const std::pair<int, InputIdentifier> poller_pair : callback_list) { | ||
| 276 | const InputIdentifier& poller = poller_pair.second; | ||
| 277 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) { | ||
| 278 | continue; | ||
| 279 | } | ||
| 280 | if (poller.callback.on_change) { | ||
| 281 | poller.callback.on_change(); | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion, | ||
| 287 | BasicMotion value) { | ||
| 288 | std::lock_guard lock{mutex_callback}; | ||
| 289 | for (const std::pair<int, InputIdentifier> poller_pair : callback_list) { | ||
| 290 | const InputIdentifier& poller = poller_pair.second; | ||
| 291 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) { | ||
| 292 | continue; | ||
| 293 | } | ||
| 294 | if (poller.callback.on_change) { | ||
| 295 | poller.callback.on_change(); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | if (!configuring || !mapping_callback.on_data) { | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | if (std::abs(value.gyro_x) < 1.0f && std::abs(value.gyro_y) < 1.0f && | ||
| 302 | std::abs(value.gyro_z) < 1.0f) { | ||
| 303 | return; | ||
| 304 | } | ||
| 305 | mapping_callback.on_data(MappingData{ | ||
| 306 | .engine = GetEngineName(), | ||
| 307 | .pad = identifier, | ||
| 308 | .type = EngineInputType::Motion, | ||
| 309 | .index = motion, | ||
| 310 | .motion_value = value, | ||
| 311 | }); | ||
| 312 | } | ||
| 313 | |||
| 314 | bool InputEngine::IsInputIdentifierEqual(const InputIdentifier& input_identifier, | ||
| 315 | const PadIdentifier& identifier, EngineInputType type, | ||
| 316 | std::size_t index) const { | ||
| 317 | if (input_identifier.type != type) { | ||
| 318 | return false; | ||
| 319 | } | ||
| 320 | if (input_identifier.index != index) { | ||
| 321 | return false; | ||
| 322 | } | ||
| 323 | if (input_identifier.identifier != identifier) { | ||
| 324 | return false; | ||
| 325 | } | ||
| 326 | return true; | ||
| 327 | } | ||
| 328 | |||
| 329 | void InputEngine::BeginConfiguration() { | ||
| 330 | configuring = true; | ||
| 331 | } | ||
| 332 | |||
| 333 | void InputEngine::EndConfiguration() { | ||
| 334 | configuring = false; | ||
| 335 | } | ||
| 336 | |||
| 337 | const std::string& InputEngine::GetEngineName() const { | ||
| 338 | return input_engine; | ||
| 339 | } | ||
| 340 | |||
| 341 | int InputEngine::SetCallback(InputIdentifier input_identifier) { | ||
| 342 | std::lock_guard lock{mutex_callback}; | ||
| 343 | callback_list.insert_or_assign(last_callback_key, input_identifier); | ||
| 344 | return last_callback_key++; | ||
| 345 | } | ||
| 346 | |||
| 347 | void InputEngine::SetMappingCallback(MappingCallback callback) { | ||
| 348 | std::lock_guard lock{mutex_callback}; | ||
| 349 | mapping_callback = std::move(callback); | ||
| 350 | } | ||
| 351 | |||
| 352 | void InputEngine::DeleteCallback(int key) { | ||
| 353 | std::lock_guard lock{mutex_callback}; | ||
| 354 | if (!callback_list.contains(key)) { | ||
| 355 | LOG_ERROR(Input, "Tried to delete non-existent callback {}", key); | ||
| 356 | return; | ||
| 357 | } | ||
| 358 | callback_list.erase(key); | ||
| 359 | } | ||
| 360 | |||
| 361 | } // namespace InputCommon | ||