diff options
| author | 2021-11-14 14:09:29 -0600 | |
|---|---|---|
| committer | 2021-11-24 20:30:28 -0600 | |
| commit | 654d76e79e84a3384fa503fac9003a5d0a32f28b (patch) | |
| tree | 7a0d436a55aa73401d7b77bae4870c10ceca16cd /src/core/hid/emulated_devices.cpp | |
| parent | input_common: Allow keyboard to be backwards compatible (diff) | |
| download | yuzu-654d76e79e84a3384fa503fac9003a5d0a32f28b.tar.gz yuzu-654d76e79e84a3384fa503fac9003a5d0a32f28b.tar.xz yuzu-654d76e79e84a3384fa503fac9003a5d0a32f28b.zip | |
core/hid: Fully implement native mouse
Diffstat (limited to 'src/core/hid/emulated_devices.cpp')
| -rw-r--r-- | src/core/hid/emulated_devices.cpp | 124 |
1 files changed, 93 insertions, 31 deletions
diff --git a/src/core/hid/emulated_devices.cpp b/src/core/hid/emulated_devices.cpp index 45e0bd80d..70a494097 100644 --- a/src/core/hid/emulated_devices.cpp +++ b/src/core/hid/emulated_devices.cpp | |||
| @@ -15,21 +15,34 @@ EmulatedDevices::EmulatedDevices() = default; | |||
| 15 | EmulatedDevices::~EmulatedDevices() = default; | 15 | EmulatedDevices::~EmulatedDevices() = default; |
| 16 | 16 | ||
| 17 | void EmulatedDevices::ReloadFromSettings() { | 17 | void EmulatedDevices::ReloadFromSettings() { |
| 18 | const auto& mouse = Settings::values.mouse_buttons; | ||
| 19 | |||
| 20 | for (std::size_t index = 0; index < mouse.size(); ++index) { | ||
| 21 | mouse_button_params[index] = Common::ParamPackage(mouse[index]); | ||
| 22 | } | ||
| 23 | ReloadInput(); | 18 | ReloadInput(); |
| 24 | } | 19 | } |
| 25 | 20 | ||
| 26 | void EmulatedDevices::ReloadInput() { | 21 | void EmulatedDevices::ReloadInput() { |
| 27 | std::transform(mouse_button_params.begin() + Settings::NativeMouseButton::MOUSE_HID_BEGIN, | 22 | // If you load any device here add the equivalent to the UnloadInput() function |
| 28 | mouse_button_params.begin() + Settings::NativeMouseButton::MOUSE_HID_END, | ||
| 29 | mouse_button_devices.begin(), | ||
| 30 | Common::Input::CreateDevice<Common::Input::InputDevice>); | ||
| 31 | |||
| 32 | std::size_t key_index = 0; | 23 | std::size_t key_index = 0; |
| 24 | for (auto& mouse_device : mouse_button_devices) { | ||
| 25 | Common::ParamPackage mouse_params; | ||
| 26 | mouse_params.Set("engine", "mouse"); | ||
| 27 | mouse_params.Set("button", static_cast<int>(key_index)); | ||
| 28 | mouse_device = Common::Input::CreateDevice<Common::Input::InputDevice>(mouse_params); | ||
| 29 | key_index++; | ||
| 30 | } | ||
| 31 | |||
| 32 | mouse_stick_device = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>( | ||
| 33 | "engine:mouse,axis_x:0,axis_y:1"); | ||
| 34 | |||
| 35 | // First two axis are reserved for mouse position | ||
| 36 | key_index = 2; | ||
| 37 | for (auto& mouse_device : mouse_analog_devices) { | ||
| 38 | Common::ParamPackage mouse_params; | ||
| 39 | mouse_params.Set("engine", "mouse"); | ||
| 40 | mouse_params.Set("axis", static_cast<int>(key_index)); | ||
| 41 | mouse_device = Common::Input::CreateDevice<Common::Input::InputDevice>(mouse_params); | ||
| 42 | key_index++; | ||
| 43 | } | ||
| 44 | |||
| 45 | key_index = 0; | ||
| 33 | for (auto& keyboard_device : keyboard_devices) { | 46 | for (auto& keyboard_device : keyboard_devices) { |
| 34 | // Keyboard keys are only mapped on port 1, pad 0 | 47 | // Keyboard keys are only mapped on port 1, pad 0 |
| 35 | Common::ParamPackage keyboard_params; | 48 | Common::ParamPackage keyboard_params; |
| @@ -64,6 +77,23 @@ void EmulatedDevices::ReloadInput() { | |||
| 64 | mouse_button_devices[index]->SetCallback(button_callback); | 77 | mouse_button_devices[index]->SetCallback(button_callback); |
| 65 | } | 78 | } |
| 66 | 79 | ||
| 80 | for (std::size_t index = 0; index < mouse_analog_devices.size(); ++index) { | ||
| 81 | if (!mouse_analog_devices[index]) { | ||
| 82 | continue; | ||
| 83 | } | ||
| 84 | Common::Input::InputCallback button_callback{ | ||
| 85 | [this, index](Common::Input::CallbackStatus callback) { | ||
| 86 | SetMouseAnalog(callback, index); | ||
| 87 | }}; | ||
| 88 | mouse_analog_devices[index]->SetCallback(button_callback); | ||
| 89 | } | ||
| 90 | |||
| 91 | if (mouse_stick_device) { | ||
| 92 | Common::Input::InputCallback button_callback{ | ||
| 93 | [this](Common::Input::CallbackStatus callback) { SetMouseStick(callback); }}; | ||
| 94 | mouse_stick_device->SetCallback(button_callback); | ||
| 95 | } | ||
| 96 | |||
| 67 | for (std::size_t index = 0; index < keyboard_devices.size(); ++index) { | 97 | for (std::size_t index = 0; index < keyboard_devices.size(); ++index) { |
| 68 | if (!keyboard_devices[index]) { | 98 | if (!keyboard_devices[index]) { |
| 69 | continue; | 99 | continue; |
| @@ -91,6 +121,10 @@ void EmulatedDevices::UnloadInput() { | |||
| 91 | for (auto& button : mouse_button_devices) { | 121 | for (auto& button : mouse_button_devices) { |
| 92 | button.reset(); | 122 | button.reset(); |
| 93 | } | 123 | } |
| 124 | for (auto& analog : mouse_analog_devices) { | ||
| 125 | analog.reset(); | ||
| 126 | } | ||
| 127 | mouse_stick_device.reset(); | ||
| 94 | for (auto& button : keyboard_devices) { | 128 | for (auto& button : keyboard_devices) { |
| 95 | button.reset(); | 129 | button.reset(); |
| 96 | } | 130 | } |
| @@ -116,12 +150,6 @@ void EmulatedDevices::SaveCurrentConfig() { | |||
| 116 | if (!is_configuring) { | 150 | if (!is_configuring) { |
| 117 | return; | 151 | return; |
| 118 | } | 152 | } |
| 119 | |||
| 120 | auto& mouse = Settings::values.mouse_buttons; | ||
| 121 | |||
| 122 | for (std::size_t index = 0; index < mouse.size(); ++index) { | ||
| 123 | mouse[index] = mouse_button_params[index].Serialize(); | ||
| 124 | } | ||
| 125 | } | 153 | } |
| 126 | 154 | ||
| 127 | void EmulatedDevices::RestoreConfig() { | 155 | void EmulatedDevices::RestoreConfig() { |
| @@ -131,21 +159,6 @@ void EmulatedDevices::RestoreConfig() { | |||
| 131 | ReloadFromSettings(); | 159 | ReloadFromSettings(); |
| 132 | } | 160 | } |
| 133 | 161 | ||
| 134 | Common::ParamPackage EmulatedDevices::GetMouseButtonParam(std::size_t index) const { | ||
| 135 | if (index >= mouse_button_params.size()) { | ||
| 136 | return {}; | ||
| 137 | } | ||
| 138 | return mouse_button_params[index]; | ||
| 139 | } | ||
| 140 | |||
| 141 | void EmulatedDevices::SetMouseButtonParam(std::size_t index, Common::ParamPackage param) { | ||
| 142 | if (index >= mouse_button_params.size()) { | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | mouse_button_params[index] = param; | ||
| 146 | ReloadInput(); | ||
| 147 | } | ||
| 148 | |||
| 149 | void EmulatedDevices::SetKeyboardButton(Common::Input::CallbackStatus callback, std::size_t index) { | 162 | void EmulatedDevices::SetKeyboardButton(Common::Input::CallbackStatus callback, std::size_t index) { |
| 150 | if (index >= device_status.keyboard_values.size()) { | 163 | if (index >= device_status.keyboard_values.size()) { |
| 151 | return; | 164 | return; |
| @@ -334,6 +347,51 @@ void EmulatedDevices::SetMouseButton(Common::Input::CallbackStatus callback, std | |||
| 334 | TriggerOnChange(DeviceTriggerType::Mouse); | 347 | TriggerOnChange(DeviceTriggerType::Mouse); |
| 335 | } | 348 | } |
| 336 | 349 | ||
| 350 | void EmulatedDevices::SetMouseAnalog(Common::Input::CallbackStatus callback, std::size_t index) { | ||
| 351 | if (index >= device_status.mouse_analog_values.size()) { | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | std::lock_guard lock{mutex}; | ||
| 355 | const auto analog_value = TransformToAnalog(callback); | ||
| 356 | |||
| 357 | device_status.mouse_analog_values[index] = analog_value; | ||
| 358 | |||
| 359 | if (is_configuring) { | ||
| 360 | device_status.mouse_position_state = {}; | ||
| 361 | TriggerOnChange(DeviceTriggerType::Mouse); | ||
| 362 | return; | ||
| 363 | } | ||
| 364 | |||
| 365 | switch (index) { | ||
| 366 | case Settings::NativeMouseWheel::X: | ||
| 367 | device_status.mouse_wheel_state.x = static_cast<s32>(analog_value.value); | ||
| 368 | break; | ||
| 369 | case Settings::NativeMouseWheel::Y: | ||
| 370 | device_status.mouse_wheel_state.y = static_cast<s32>(analog_value.value); | ||
| 371 | break; | ||
| 372 | } | ||
| 373 | |||
| 374 | TriggerOnChange(DeviceTriggerType::Mouse); | ||
| 375 | } | ||
| 376 | |||
| 377 | void EmulatedDevices::SetMouseStick(Common::Input::CallbackStatus callback) { | ||
| 378 | std::lock_guard lock{mutex}; | ||
| 379 | const auto stick_value = TransformToStick(callback); | ||
| 380 | |||
| 381 | device_status.mouse_stick_value = stick_value; | ||
| 382 | |||
| 383 | if (is_configuring) { | ||
| 384 | device_status.mouse_position_state = {}; | ||
| 385 | TriggerOnChange(DeviceTriggerType::Mouse); | ||
| 386 | return; | ||
| 387 | } | ||
| 388 | |||
| 389 | device_status.mouse_position_state.x = stick_value.x.value; | ||
| 390 | device_status.mouse_position_state.y = stick_value.y.value; | ||
| 391 | |||
| 392 | TriggerOnChange(DeviceTriggerType::Mouse); | ||
| 393 | } | ||
| 394 | |||
| 337 | KeyboardValues EmulatedDevices::GetKeyboardValues() const { | 395 | KeyboardValues EmulatedDevices::GetKeyboardValues() const { |
| 338 | return device_status.keyboard_values; | 396 | return device_status.keyboard_values; |
| 339 | } | 397 | } |
| @@ -362,6 +420,10 @@ MousePosition EmulatedDevices::GetMousePosition() const { | |||
| 362 | return device_status.mouse_position_state; | 420 | return device_status.mouse_position_state; |
| 363 | } | 421 | } |
| 364 | 422 | ||
| 423 | AnalogStickState EmulatedDevices::GetMouseDeltaWheel() const { | ||
| 424 | return device_status.mouse_wheel_state; | ||
| 425 | } | ||
| 426 | |||
| 365 | void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) { | 427 | void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) { |
| 366 | for (const auto& poller_pair : callback_list) { | 428 | for (const auto& poller_pair : callback_list) { |
| 367 | const InterfaceUpdateCallback& poller = poller_pair.second; | 429 | const InterfaceUpdateCallback& poller = poller_pair.second; |