diff options
| author | 2020-07-16 11:40:09 -0400 | |
|---|---|---|
| committer | 2020-07-16 11:40:09 -0400 | |
| commit | a89dfc9183347be638f49f8d9ca4d0f38ca9de76 (patch) | |
| tree | 3c01f62ddd0b416ced79f288736e0d1fa9b6a586 /src/input_common/gcadapter/gc_adapter.cpp | |
| parent | Merge pull request #4337 from lat9nq/fix-per-game-async (diff) | |
| parent | Rebase to master (diff) | |
| download | yuzu-a89dfc9183347be638f49f8d9ca4d0f38ca9de76.tar.gz yuzu-a89dfc9183347be638f49f8d9ca4d0f38ca9de76.tar.xz yuzu-a89dfc9183347be638f49f8d9ca4d0f38ca9de76.zip | |
Merge pull request #4261 from ameerj/gc-calibration
input_common: GC Controller save and compare against analog origin state
Diffstat (limited to '')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 38210ffcb..898a278a9 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -25,6 +25,7 @@ Adapter::Adapter() { | |||
| 25 | LOG_INFO(Input, "GC Adapter Initialization started"); | 25 | LOG_INFO(Input, "GC Adapter Initialization started"); |
| 26 | 26 | ||
| 27 | current_status = NO_ADAPTER_DETECTED; | 27 | current_status = NO_ADAPTER_DETECTED; |
| 28 | get_origin.fill(true); | ||
| 28 | 29 | ||
| 29 | const int init_res = libusb_init(&libusb_ctx); | 30 | const int init_res = libusb_init(&libusb_ctx); |
| 30 | if (init_res == LIBUSB_SUCCESS) { | 31 | if (init_res == LIBUSB_SUCCESS) { |
| @@ -36,13 +37,8 @@ Adapter::Adapter() { | |||
| 36 | 37 | ||
| 37 | GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { | 38 | GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { |
| 38 | GCPadStatus pad = {}; | 39 | GCPadStatus pad = {}; |
| 39 | bool get_origin = false; | ||
| 40 | 40 | ||
| 41 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); | 41 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); |
| 42 | if (type != ControllerTypes::None) { | ||
| 43 | get_origin = true; | ||
| 44 | } | ||
| 45 | |||
| 46 | adapter_controllers_status[port] = type; | 42 | adapter_controllers_status[port] = type; |
| 47 | 43 | ||
| 48 | static constexpr std::array<PadButton, 8> b1_buttons{ | 44 | static constexpr std::array<PadButton, 8> b1_buttons{ |
| @@ -58,6 +54,11 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad | |||
| 58 | PadButton::PAD_TRIGGER_L, | 54 | PadButton::PAD_TRIGGER_L, |
| 59 | }; | 55 | }; |
| 60 | 56 | ||
| 57 | if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) { | ||
| 58 | // Controller may have been disconnected, recalibrate if reconnected. | ||
| 59 | get_origin[port] = true; | ||
| 60 | } | ||
| 61 | |||
| 61 | if (adapter_controllers_status[port] != ControllerTypes::None) { | 62 | if (adapter_controllers_status[port] != ControllerTypes::None) { |
| 62 | const u8 b1 = adapter_payload[1 + (9 * port) + 1]; | 63 | const u8 b1 = adapter_payload[1 + (9 * port) + 1]; |
| 63 | const u8 b2 = adapter_payload[1 + (9 * port) + 2]; | 64 | const u8 b2 = adapter_payload[1 + (9 * port) + 2]; |
| @@ -74,16 +75,22 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad | |||
| 74 | } | 75 | } |
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | if (get_origin) { | ||
| 78 | pad.button |= PAD_GET_ORIGIN; | ||
| 79 | } | ||
| 80 | |||
| 81 | pad.stick_x = adapter_payload[1 + (9 * port) + 3]; | 78 | pad.stick_x = adapter_payload[1 + (9 * port) + 3]; |
| 82 | pad.stick_y = adapter_payload[1 + (9 * port) + 4]; | 79 | pad.stick_y = adapter_payload[1 + (9 * port) + 4]; |
| 83 | pad.substick_x = adapter_payload[1 + (9 * port) + 5]; | 80 | pad.substick_x = adapter_payload[1 + (9 * port) + 5]; |
| 84 | pad.substick_y = adapter_payload[1 + (9 * port) + 6]; | 81 | pad.substick_y = adapter_payload[1 + (9 * port) + 6]; |
| 85 | pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; | 82 | pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; |
| 86 | pad.trigger_right = adapter_payload[1 + (9 * port) + 8]; | 83 | pad.trigger_right = adapter_payload[1 + (9 * port) + 8]; |
| 84 | |||
| 85 | if (get_origin[port]) { | ||
| 86 | origin_status[port].stick_x = pad.stick_x; | ||
| 87 | origin_status[port].stick_y = pad.stick_y; | ||
| 88 | origin_status[port].substick_x = pad.substick_x; | ||
| 89 | origin_status[port].substick_y = pad.substick_y; | ||
| 90 | origin_status[port].trigger_left = pad.trigger_left; | ||
| 91 | origin_status[port].trigger_right = pad.trigger_right; | ||
| 92 | get_origin[port] = false; | ||
| 93 | } | ||
| 87 | } | 94 | } |
| 88 | return pad; | 95 | return pad; |
| 89 | } | 96 | } |
| @@ -132,31 +139,31 @@ void Adapter::Read() { | |||
| 132 | for (std::size_t port = 0; port < pads.size(); ++port) { | 139 | for (std::size_t port = 0; port < pads.size(); ++port) { |
| 133 | pads[port] = GetPadStatus(port, adapter_payload_copy); | 140 | pads[port] = GetPadStatus(port, adapter_payload_copy); |
| 134 | if (DeviceConnected(port) && configuring) { | 141 | if (DeviceConnected(port) && configuring) { |
| 135 | if (pads[port].button != PAD_GET_ORIGIN) { | 142 | if (pads[port].button != 0) { |
| 136 | pad_queue[port].Push(pads[port]); | 143 | pad_queue[port].Push(pads[port]); |
| 137 | } | 144 | } |
| 138 | 145 | ||
| 139 | // Accounting for a threshold here because of some controller variance | 146 | // Accounting for a threshold here because of some controller variance |
| 140 | if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD || | 147 | if (pads[port].stick_x > origin_status[port].stick_x + pads[port].THRESHOLD || |
| 141 | pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) { | 148 | pads[port].stick_x < origin_status[port].stick_x - pads[port].THRESHOLD) { |
| 142 | pads[port].axis = GCAdapter::PadAxes::StickX; | 149 | pads[port].axis = GCAdapter::PadAxes::StickX; |
| 143 | pads[port].axis_value = pads[port].stick_x; | 150 | pads[port].axis_value = pads[port].stick_x; |
| 144 | pad_queue[port].Push(pads[port]); | 151 | pad_queue[port].Push(pads[port]); |
| 145 | } | 152 | } |
| 146 | if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD || | 153 | if (pads[port].stick_y > origin_status[port].stick_y + pads[port].THRESHOLD || |
| 147 | pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) { | 154 | pads[port].stick_y < origin_status[port].stick_y - pads[port].THRESHOLD) { |
| 148 | pads[port].axis = GCAdapter::PadAxes::StickY; | 155 | pads[port].axis = GCAdapter::PadAxes::StickY; |
| 149 | pads[port].axis_value = pads[port].stick_y; | 156 | pads[port].axis_value = pads[port].stick_y; |
| 150 | pad_queue[port].Push(pads[port]); | 157 | pad_queue[port].Push(pads[port]); |
| 151 | } | 158 | } |
| 152 | if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD || | 159 | if (pads[port].substick_x > origin_status[port].substick_x + pads[port].THRESHOLD || |
| 153 | pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) { | 160 | pads[port].substick_x < origin_status[port].substick_x - pads[port].THRESHOLD) { |
| 154 | pads[port].axis = GCAdapter::PadAxes::SubstickX; | 161 | pads[port].axis = GCAdapter::PadAxes::SubstickX; |
| 155 | pads[port].axis_value = pads[port].substick_x; | 162 | pads[port].axis_value = pads[port].substick_x; |
| 156 | pad_queue[port].Push(pads[port]); | 163 | pad_queue[port].Push(pads[port]); |
| 157 | } | 164 | } |
| 158 | if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD || | 165 | if (pads[port].substick_y > origin_status[port].substick_y + pads[port].THRESHOLD || |
| 159 | pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) { | 166 | pads[port].substick_y < origin_status[port].substick_y - pads[port].THRESHOLD) { |
| 160 | pads[port].axis = GCAdapter::PadAxes::SubstickY; | 167 | pads[port].axis = GCAdapter::PadAxes::SubstickY; |
| 161 | pads[port].axis_value = pads[port].substick_y; | 168 | pads[port].axis_value = pads[port].substick_y; |
| 162 | pad_queue[port].Push(pads[port]); | 169 | pad_queue[port].Push(pads[port]); |
| @@ -237,6 +244,9 @@ void Adapter::Setup() { | |||
| 237 | } | 244 | } |
| 238 | libusb_free_device_list(devices, 1); | 245 | libusb_free_device_list(devices, 1); |
| 239 | } | 246 | } |
| 247 | // Break out of the ScanThreadFunc() loop that is constantly looking for the device | ||
| 248 | // Assumes user has GC adapter plugged in before launch to use the adapter | ||
| 249 | detect_thread_running = false; | ||
| 240 | } | 250 | } |
| 241 | 251 | ||
| 242 | bool Adapter::CheckDeviceAccess(libusb_device* device) { | 252 | bool Adapter::CheckDeviceAccess(libusb_device* device) { |
| @@ -345,6 +355,7 @@ void Adapter::Reset() { | |||
| 345 | adapter_input_thread.join(); | 355 | adapter_input_thread.join(); |
| 346 | 356 | ||
| 347 | adapter_controllers_status.fill(ControllerTypes::None); | 357 | adapter_controllers_status.fill(ControllerTypes::None); |
| 358 | get_origin.fill(true); | ||
| 348 | current_status = NO_ADAPTER_DETECTED; | 359 | current_status = NO_ADAPTER_DETECTED; |
| 349 | 360 | ||
| 350 | if (usb_adapter_handle) { | 361 | if (usb_adapter_handle) { |
| @@ -367,6 +378,7 @@ void Adapter::ResetDeviceType(std::size_t port) { | |||
| 367 | } | 378 | } |
| 368 | 379 | ||
| 369 | void Adapter::BeginConfiguration() { | 380 | void Adapter::BeginConfiguration() { |
| 381 | get_origin.fill(true); | ||
| 370 | for (auto& pq : pad_queue) { | 382 | for (auto& pq : pad_queue) { |
| 371 | pq.Clear(); | 383 | pq.Clear(); |
| 372 | } | 384 | } |
| @@ -396,4 +408,25 @@ const std::array<GCState, 4>& Adapter::GetPadState() const { | |||
| 396 | return state; | 408 | return state; |
| 397 | } | 409 | } |
| 398 | 410 | ||
| 411 | int Adapter::GetOriginValue(int port, int axis) const { | ||
| 412 | const auto& status = origin_status[port]; | ||
| 413 | |||
| 414 | switch (static_cast<PadAxes>(axis)) { | ||
| 415 | case PadAxes::StickX: | ||
| 416 | return status.stick_x; | ||
| 417 | case PadAxes::StickY: | ||
| 418 | return status.stick_y; | ||
| 419 | case PadAxes::SubstickX: | ||
| 420 | return status.substick_x; | ||
| 421 | case PadAxes::SubstickY: | ||
| 422 | return status.substick_y; | ||
| 423 | case PadAxes::TriggerLeft: | ||
| 424 | return status.trigger_left; | ||
| 425 | case PadAxes::TriggerRight: | ||
| 426 | return status.trigger_right; | ||
| 427 | default: | ||
| 428 | return 0; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 399 | } // namespace GCAdapter | 432 | } // namespace GCAdapter |