diff options
Diffstat (limited to 'src/input_common/gcadapter/gc_adapter.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 80 |
1 files changed, 57 insertions, 23 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 6d9f4d9eb..898a278a9 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #include <chrono> | 5 | #include <chrono> |
| 6 | #include <thread> | 6 | #include <thread> |
| 7 | #include <libusb.h> | ||
| 7 | #include "common/logging/log.h" | 8 | #include "common/logging/log.h" |
| 8 | #include "input_common/gcadapter/gc_adapter.h" | 9 | #include "input_common/gcadapter/gc_adapter.h" |
| 9 | 10 | ||
| @@ -24,6 +25,7 @@ Adapter::Adapter() { | |||
| 24 | LOG_INFO(Input, "GC Adapter Initialization started"); | 25 | LOG_INFO(Input, "GC Adapter Initialization started"); |
| 25 | 26 | ||
| 26 | current_status = NO_ADAPTER_DETECTED; | 27 | current_status = NO_ADAPTER_DETECTED; |
| 28 | get_origin.fill(true); | ||
| 27 | 29 | ||
| 28 | const int init_res = libusb_init(&libusb_ctx); | 30 | const int init_res = libusb_init(&libusb_ctx); |
| 29 | if (init_res == LIBUSB_SUCCESS) { | 31 | if (init_res == LIBUSB_SUCCESS) { |
| @@ -33,15 +35,10 @@ Adapter::Adapter() { | |||
| 33 | } | 35 | } |
| 34 | } | 36 | } |
| 35 | 37 | ||
| 36 | GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { | 38 | GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { |
| 37 | GCPadStatus pad = {}; | 39 | GCPadStatus pad = {}; |
| 38 | bool get_origin = false; | ||
| 39 | 40 | ||
| 40 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); | 41 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); |
| 41 | if (type != ControllerTypes::None) { | ||
| 42 | get_origin = true; | ||
| 43 | } | ||
| 44 | |||
| 45 | adapter_controllers_status[port] = type; | 42 | adapter_controllers_status[port] = type; |
| 46 | 43 | ||
| 47 | static constexpr std::array<PadButton, 8> b1_buttons{ | 44 | static constexpr std::array<PadButton, 8> b1_buttons{ |
| @@ -57,6 +54,11 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_pa | |||
| 57 | PadButton::PAD_TRIGGER_L, | 54 | PadButton::PAD_TRIGGER_L, |
| 58 | }; | 55 | }; |
| 59 | 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 | |||
| 60 | if (adapter_controllers_status[port] != ControllerTypes::None) { | 62 | if (adapter_controllers_status[port] != ControllerTypes::None) { |
| 61 | const u8 b1 = adapter_payload[1 + (9 * port) + 1]; | 63 | const u8 b1 = adapter_payload[1 + (9 * port) + 1]; |
| 62 | const u8 b2 = adapter_payload[1 + (9 * port) + 2]; | 64 | const u8 b2 = adapter_payload[1 + (9 * port) + 2]; |
| @@ -73,16 +75,22 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_pa | |||
| 73 | } | 75 | } |
| 74 | } | 76 | } |
| 75 | 77 | ||
| 76 | if (get_origin) { | ||
| 77 | pad.button |= PAD_GET_ORIGIN; | ||
| 78 | } | ||
| 79 | |||
| 80 | pad.stick_x = adapter_payload[1 + (9 * port) + 3]; | 78 | pad.stick_x = adapter_payload[1 + (9 * port) + 3]; |
| 81 | pad.stick_y = adapter_payload[1 + (9 * port) + 4]; | 79 | pad.stick_y = adapter_payload[1 + (9 * port) + 4]; |
| 82 | pad.substick_x = adapter_payload[1 + (9 * port) + 5]; | 80 | pad.substick_x = adapter_payload[1 + (9 * port) + 5]; |
| 83 | pad.substick_y = adapter_payload[1 + (9 * port) + 6]; | 81 | pad.substick_y = adapter_payload[1 + (9 * port) + 6]; |
| 84 | pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; | 82 | pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; |
| 85 | 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 | } | ||
| 86 | } | 94 | } |
| 87 | return pad; | 95 | return pad; |
| 88 | } | 96 | } |
| @@ -131,31 +139,31 @@ void Adapter::Read() { | |||
| 131 | for (std::size_t port = 0; port < pads.size(); ++port) { | 139 | for (std::size_t port = 0; port < pads.size(); ++port) { |
| 132 | pads[port] = GetPadStatus(port, adapter_payload_copy); | 140 | pads[port] = GetPadStatus(port, adapter_payload_copy); |
| 133 | if (DeviceConnected(port) && configuring) { | 141 | if (DeviceConnected(port) && configuring) { |
| 134 | if (pads[port].button != PAD_GET_ORIGIN) { | 142 | if (pads[port].button != 0) { |
| 135 | pad_queue[port].Push(pads[port]); | 143 | pad_queue[port].Push(pads[port]); |
| 136 | } | 144 | } |
| 137 | 145 | ||
| 138 | // Accounting for a threshold here because of some controller variance | 146 | // Accounting for a threshold here because of some controller variance |
| 139 | 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 || |
| 140 | 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) { |
| 141 | pads[port].axis = GCAdapter::PadAxes::StickX; | 149 | pads[port].axis = GCAdapter::PadAxes::StickX; |
| 142 | pads[port].axis_value = pads[port].stick_x; | 150 | pads[port].axis_value = pads[port].stick_x; |
| 143 | pad_queue[port].Push(pads[port]); | 151 | pad_queue[port].Push(pads[port]); |
| 144 | } | 152 | } |
| 145 | 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 || |
| 146 | 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) { |
| 147 | pads[port].axis = GCAdapter::PadAxes::StickY; | 155 | pads[port].axis = GCAdapter::PadAxes::StickY; |
| 148 | pads[port].axis_value = pads[port].stick_y; | 156 | pads[port].axis_value = pads[port].stick_y; |
| 149 | pad_queue[port].Push(pads[port]); | 157 | pad_queue[port].Push(pads[port]); |
| 150 | } | 158 | } |
| 151 | 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 || |
| 152 | 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) { |
| 153 | pads[port].axis = GCAdapter::PadAxes::SubstickX; | 161 | pads[port].axis = GCAdapter::PadAxes::SubstickX; |
| 154 | pads[port].axis_value = pads[port].substick_x; | 162 | pads[port].axis_value = pads[port].substick_x; |
| 155 | pad_queue[port].Push(pads[port]); | 163 | pad_queue[port].Push(pads[port]); |
| 156 | } | 164 | } |
| 157 | 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 || |
| 158 | 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) { |
| 159 | pads[port].axis = GCAdapter::PadAxes::SubstickY; | 167 | pads[port].axis = GCAdapter::PadAxes::SubstickY; |
| 160 | pads[port].axis_value = pads[port].substick_y; | 168 | pads[port].axis_value = pads[port].substick_y; |
| 161 | pad_queue[port].Push(pads[port]); | 169 | pad_queue[port].Push(pads[port]); |
| @@ -198,7 +206,7 @@ void Adapter::StartScanThread() { | |||
| 198 | } | 206 | } |
| 199 | 207 | ||
| 200 | detect_thread_running = true; | 208 | detect_thread_running = true; |
| 201 | detect_thread = std::thread([=] { ScanThreadFunc(); }); | 209 | detect_thread = std::thread(&Adapter::ScanThreadFunc, this); |
| 202 | } | 210 | } |
| 203 | 211 | ||
| 204 | void Adapter::StopScanThread() { | 212 | void Adapter::StopScanThread() { |
| @@ -227,7 +235,7 @@ void Adapter::Setup() { | |||
| 227 | } | 235 | } |
| 228 | 236 | ||
| 229 | if (devices != nullptr) { | 237 | if (devices != nullptr) { |
| 230 | for (std::size_t index = 0; index < device_count; ++index) { | 238 | for (std::size_t index = 0; index < static_cast<std::size_t>(device_count); ++index) { |
| 231 | if (CheckDeviceAccess(devices[index])) { | 239 | if (CheckDeviceAccess(devices[index])) { |
| 232 | // GC Adapter found and accessible, registering it | 240 | // GC Adapter found and accessible, registering it |
| 233 | GetGCEndpoint(devices[index]); | 241 | GetGCEndpoint(devices[index]); |
| @@ -236,6 +244,9 @@ void Adapter::Setup() { | |||
| 236 | } | 244 | } |
| 237 | libusb_free_device_list(devices, 1); | 245 | libusb_free_device_list(devices, 1); |
| 238 | } | 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; | ||
| 239 | } | 250 | } |
| 240 | 251 | ||
| 241 | bool Adapter::CheckDeviceAccess(libusb_device* device) { | 252 | bool Adapter::CheckDeviceAccess(libusb_device* device) { |
| @@ -344,6 +355,7 @@ void Adapter::Reset() { | |||
| 344 | adapter_input_thread.join(); | 355 | adapter_input_thread.join(); |
| 345 | 356 | ||
| 346 | adapter_controllers_status.fill(ControllerTypes::None); | 357 | adapter_controllers_status.fill(ControllerTypes::None); |
| 358 | get_origin.fill(true); | ||
| 347 | current_status = NO_ADAPTER_DETECTED; | 359 | current_status = NO_ADAPTER_DETECTED; |
| 348 | 360 | ||
| 349 | if (usb_adapter_handle) { | 361 | if (usb_adapter_handle) { |
| @@ -357,15 +369,16 @@ void Adapter::Reset() { | |||
| 357 | } | 369 | } |
| 358 | } | 370 | } |
| 359 | 371 | ||
| 360 | bool Adapter::DeviceConnected(int port) { | 372 | bool Adapter::DeviceConnected(std::size_t port) { |
| 361 | return adapter_controllers_status[port] != ControllerTypes::None; | 373 | return adapter_controllers_status[port] != ControllerTypes::None; |
| 362 | } | 374 | } |
| 363 | 375 | ||
| 364 | void Adapter::ResetDeviceType(int port) { | 376 | void Adapter::ResetDeviceType(std::size_t port) { |
| 365 | adapter_controllers_status[port] = ControllerTypes::None; | 377 | adapter_controllers_status[port] = ControllerTypes::None; |
| 366 | } | 378 | } |
| 367 | 379 | ||
| 368 | void Adapter::BeginConfiguration() { | 380 | void Adapter::BeginConfiguration() { |
| 381 | get_origin.fill(true); | ||
| 369 | for (auto& pq : pad_queue) { | 382 | for (auto& pq : pad_queue) { |
| 370 | pq.Clear(); | 383 | pq.Clear(); |
| 371 | } | 384 | } |
| @@ -395,4 +408,25 @@ const std::array<GCState, 4>& Adapter::GetPadState() const { | |||
| 395 | return state; | 408 | return state; |
| 396 | } | 409 | } |
| 397 | 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 | |||
| 398 | } // namespace GCAdapter | 432 | } // namespace GCAdapter |