diff options
Diffstat (limited to 'src/input_common/gcadapter/gc_adapter.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 185 |
1 files changed, 40 insertions, 145 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 898a278a9..02d06876f 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -24,12 +24,9 @@ Adapter::Adapter() { | |||
| 24 | } | 24 | } |
| 25 | LOG_INFO(Input, "GC Adapter Initialization started"); | 25 | LOG_INFO(Input, "GC Adapter Initialization started"); |
| 26 | 26 | ||
| 27 | current_status = NO_ADAPTER_DETECTED; | ||
| 28 | get_origin.fill(true); | ||
| 29 | |||
| 30 | const int init_res = libusb_init(&libusb_ctx); | 27 | const int init_res = libusb_init(&libusb_ctx); |
| 31 | if (init_res == LIBUSB_SUCCESS) { | 28 | if (init_res == LIBUSB_SUCCESS) { |
| 32 | StartScanThread(); | 29 | Setup(); |
| 33 | } else { | 30 | } else { |
| 34 | LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res); | 31 | LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res); |
| 35 | } | 32 | } |
| @@ -37,9 +34,9 @@ Adapter::Adapter() { | |||
| 37 | 34 | ||
| 38 | GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { | 35 | GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { |
| 39 | GCPadStatus pad = {}; | 36 | GCPadStatus pad = {}; |
| 37 | const std::size_t offset = 1 + (9 * port); | ||
| 40 | 38 | ||
| 41 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); | 39 | adapter_controllers_status[port] = static_cast<ControllerTypes>(adapter_payload[offset] >> 4); |
| 42 | adapter_controllers_status[port] = type; | ||
| 43 | 40 | ||
| 44 | static constexpr std::array<PadButton, 8> b1_buttons{ | 41 | static constexpr std::array<PadButton, 8> b1_buttons{ |
| 45 | PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X, | 42 | PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X, |
| @@ -54,14 +51,19 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad | |||
| 54 | PadButton::PAD_TRIGGER_L, | 51 | PadButton::PAD_TRIGGER_L, |
| 55 | }; | 52 | }; |
| 56 | 53 | ||
| 54 | static constexpr std::array<PadAxes, 6> axes{ | ||
| 55 | PadAxes::StickX, PadAxes::StickY, PadAxes::SubstickX, | ||
| 56 | PadAxes::SubstickY, PadAxes::TriggerLeft, PadAxes::TriggerRight, | ||
| 57 | }; | ||
| 58 | |||
| 57 | if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) { | 59 | if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) { |
| 58 | // Controller may have been disconnected, recalibrate if reconnected. | 60 | // Controller may have been disconnected, recalibrate if reconnected. |
| 59 | get_origin[port] = true; | 61 | get_origin[port] = true; |
| 60 | } | 62 | } |
| 61 | 63 | ||
| 62 | if (adapter_controllers_status[port] != ControllerTypes::None) { | 64 | if (adapter_controllers_status[port] != ControllerTypes::None) { |
| 63 | const u8 b1 = adapter_payload[1 + (9 * port) + 1]; | 65 | const u8 b1 = adapter_payload[offset + 1]; |
| 64 | const u8 b2 = adapter_payload[1 + (9 * port) + 2]; | 66 | const u8 b2 = adapter_payload[offset + 2]; |
| 65 | 67 | ||
| 66 | for (std::size_t i = 0; i < b1_buttons.size(); ++i) { | 68 | for (std::size_t i = 0; i < b1_buttons.size(); ++i) { |
| 67 | if ((b1 & (1U << i)) != 0) { | 69 | if ((b1 & (1U << i)) != 0) { |
| @@ -74,21 +76,13 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad | |||
| 74 | pad.button |= static_cast<u16>(b2_buttons[j]); | 76 | pad.button |= static_cast<u16>(b2_buttons[j]); |
| 75 | } | 77 | } |
| 76 | } | 78 | } |
| 77 | 79 | for (PadAxes axis : axes) { | |
| 78 | pad.stick_x = adapter_payload[1 + (9 * port) + 3]; | 80 | const std::size_t index = static_cast<std::size_t>(axis); |
| 79 | pad.stick_y = adapter_payload[1 + (9 * port) + 4]; | 81 | pad.axis_values[index] = adapter_payload[offset + 3 + index]; |
| 80 | pad.substick_x = adapter_payload[1 + (9 * port) + 5]; | 82 | } |
| 81 | pad.substick_y = adapter_payload[1 + (9 * port) + 6]; | ||
| 82 | pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; | ||
| 83 | pad.trigger_right = adapter_payload[1 + (9 * port) + 8]; | ||
| 84 | 83 | ||
| 85 | if (get_origin[port]) { | 84 | if (get_origin[port]) { |
| 86 | origin_status[port].stick_x = pad.stick_x; | 85 | origin_status[port].axis_values = pad.axis_values; |
| 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; | 86 | get_origin[port] = false; |
| 93 | } | 87 | } |
| 94 | } | 88 | } |
| @@ -101,82 +95,47 @@ void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { | |||
| 101 | state.buttons.insert_or_assign(button_value, pad.button & button_value); | 95 | state.buttons.insert_or_assign(button_value, pad.button & button_value); |
| 102 | } | 96 | } |
| 103 | 97 | ||
| 104 | state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickX), pad.stick_x); | 98 | for (size_t i = 0; i < pad.axis_values.size(); ++i) { |
| 105 | state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickY), pad.stick_y); | 99 | state.axes.insert_or_assign(static_cast<u8>(i), pad.axis_values[i]); |
| 106 | state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickX), pad.substick_x); | 100 | } |
| 107 | state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickY), pad.substick_y); | ||
| 108 | state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerLeft), pad.trigger_left); | ||
| 109 | state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerRight), pad.trigger_right); | ||
| 110 | } | 101 | } |
| 111 | 102 | ||
| 112 | void Adapter::Read() { | 103 | void Adapter::Read() { |
| 113 | LOG_DEBUG(Input, "GC Adapter Read() thread started"); | 104 | LOG_DEBUG(Input, "GC Adapter Read() thread started"); |
| 114 | 105 | ||
| 115 | int payload_size_in, payload_size_copy; | 106 | int payload_size; |
| 116 | std::array<u8, 37> adapter_payload; | 107 | std::array<u8, 37> adapter_payload; |
| 117 | std::array<u8, 37> adapter_payload_copy; | ||
| 118 | std::array<GCPadStatus, 4> pads; | 108 | std::array<GCPadStatus, 4> pads; |
| 119 | 109 | ||
| 120 | while (adapter_thread_running) { | 110 | while (adapter_thread_running) { |
| 121 | libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), | 111 | libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), |
| 122 | sizeof(adapter_payload), &payload_size_in, 16); | 112 | sizeof(adapter_payload), &payload_size, 16); |
| 123 | payload_size_copy = 0; | ||
| 124 | // this mutex might be redundant? | ||
| 125 | { | ||
| 126 | std::lock_guard<std::mutex> lk(s_mutex); | ||
| 127 | std::copy(std::begin(adapter_payload), std::end(adapter_payload), | ||
| 128 | std::begin(adapter_payload_copy)); | ||
| 129 | payload_size_copy = payload_size_in; | ||
| 130 | } | ||
| 131 | 113 | ||
| 132 | if (payload_size_copy != sizeof(adapter_payload_copy) || | 114 | if (payload_size != sizeof(adapter_payload) || adapter_payload[0] != LIBUSB_DT_HID) { |
| 133 | adapter_payload_copy[0] != LIBUSB_DT_HID) { | 115 | LOG_ERROR(Input, |
| 134 | LOG_ERROR(Input, "error reading payload (size: {}, type: {:02x})", payload_size_copy, | 116 | "Error reading payload (size: {}, type: {:02x}) Is the adapter connected?", |
| 135 | adapter_payload_copy[0]); | 117 | payload_size, adapter_payload[0]); |
| 136 | adapter_thread_running = false; // error reading from adapter, stop reading. | 118 | adapter_thread_running = false; // error reading from adapter, stop reading. |
| 137 | break; | 119 | break; |
| 138 | } | 120 | } |
| 139 | for (std::size_t port = 0; port < pads.size(); ++port) { | 121 | for (std::size_t port = 0; port < pads.size(); ++port) { |
| 140 | pads[port] = GetPadStatus(port, adapter_payload_copy); | 122 | pads[port] = GetPadStatus(port, adapter_payload); |
| 141 | if (DeviceConnected(port) && configuring) { | 123 | if (DeviceConnected(port) && configuring) { |
| 142 | if (pads[port].button != 0) { | 124 | if (pads[port].button != 0) { |
| 143 | pad_queue[port].Push(pads[port]); | 125 | pad_queue[port].Push(pads[port]); |
| 144 | } | 126 | } |
| 145 | 127 | ||
| 146 | // Accounting for a threshold here because of some controller variance | 128 | // Accounting for a threshold here to ensure an intentional press |
| 147 | if (pads[port].stick_x > origin_status[port].stick_x + pads[port].THRESHOLD || | 129 | for (size_t i = 0; i < pads[port].axis_values.size(); ++i) { |
| 148 | pads[port].stick_x < origin_status[port].stick_x - pads[port].THRESHOLD) { | 130 | const u8 value = pads[port].axis_values[i]; |
| 149 | pads[port].axis = GCAdapter::PadAxes::StickX; | 131 | const u8 origin = origin_status[port].axis_values[i]; |
| 150 | pads[port].axis_value = pads[port].stick_x; | 132 | |
| 151 | pad_queue[port].Push(pads[port]); | 133 | if (value > origin + pads[port].THRESHOLD || |
| 152 | } | 134 | value < origin - pads[port].THRESHOLD) { |
| 153 | if (pads[port].stick_y > origin_status[port].stick_y + pads[port].THRESHOLD || | 135 | pads[port].axis = static_cast<PadAxes>(i); |
| 154 | pads[port].stick_y < origin_status[port].stick_y - pads[port].THRESHOLD) { | 136 | pads[port].axis_value = pads[port].axis_values[i]; |
| 155 | pads[port].axis = GCAdapter::PadAxes::StickY; | 137 | pad_queue[port].Push(pads[port]); |
| 156 | pads[port].axis_value = pads[port].stick_y; | 138 | } |
| 157 | pad_queue[port].Push(pads[port]); | ||
| 158 | } | ||
| 159 | if (pads[port].substick_x > origin_status[port].substick_x + pads[port].THRESHOLD || | ||
| 160 | pads[port].substick_x < origin_status[port].substick_x - pads[port].THRESHOLD) { | ||
| 161 | pads[port].axis = GCAdapter::PadAxes::SubstickX; | ||
| 162 | pads[port].axis_value = pads[port].substick_x; | ||
| 163 | pad_queue[port].Push(pads[port]); | ||
| 164 | } | ||
| 165 | if (pads[port].substick_y > origin_status[port].substick_y + pads[port].THRESHOLD || | ||
| 166 | pads[port].substick_y < origin_status[port].substick_y - pads[port].THRESHOLD) { | ||
| 167 | pads[port].axis = GCAdapter::PadAxes::SubstickY; | ||
| 168 | pads[port].axis_value = pads[port].substick_y; | ||
| 169 | pad_queue[port].Push(pads[port]); | ||
| 170 | } | ||
| 171 | if (pads[port].trigger_left > pads[port].TRIGGER_THRESHOLD) { | ||
| 172 | pads[port].axis = GCAdapter::PadAxes::TriggerLeft; | ||
| 173 | pads[port].axis_value = pads[port].trigger_left; | ||
| 174 | pad_queue[port].Push(pads[port]); | ||
| 175 | } | ||
| 176 | if (pads[port].trigger_right > pads[port].TRIGGER_THRESHOLD) { | ||
| 177 | pads[port].axis = GCAdapter::PadAxes::TriggerRight; | ||
| 178 | pads[port].axis_value = pads[port].trigger_right; | ||
| 179 | pad_queue[port].Push(pads[port]); | ||
| 180 | } | 139 | } |
| 181 | } | 140 | } |
| 182 | PadToState(pads[port], state[port]); | 141 | PadToState(pads[port], state[port]); |
| @@ -185,42 +144,11 @@ void Adapter::Read() { | |||
| 185 | } | 144 | } |
| 186 | } | 145 | } |
| 187 | 146 | ||
| 188 | void Adapter::ScanThreadFunc() { | ||
| 189 | LOG_INFO(Input, "GC Adapter scanning thread started"); | ||
| 190 | |||
| 191 | while (detect_thread_running) { | ||
| 192 | if (usb_adapter_handle == nullptr) { | ||
| 193 | std::lock_guard<std::mutex> lk(initialization_mutex); | ||
| 194 | Setup(); | ||
| 195 | } | ||
| 196 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | void Adapter::StartScanThread() { | ||
| 201 | if (detect_thread_running) { | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | if (!libusb_ctx) { | ||
| 205 | return; | ||
| 206 | } | ||
| 207 | |||
| 208 | detect_thread_running = true; | ||
| 209 | detect_thread = std::thread(&Adapter::ScanThreadFunc, this); | ||
| 210 | } | ||
| 211 | |||
| 212 | void Adapter::StopScanThread() { | ||
| 213 | detect_thread_running = false; | ||
| 214 | detect_thread.join(); | ||
| 215 | } | ||
| 216 | |||
| 217 | void Adapter::Setup() { | 147 | void Adapter::Setup() { |
| 218 | // Reset the error status in case the adapter gets unplugged | 148 | // Initialize all controllers as unplugged |
| 219 | if (current_status < 0) { | ||
| 220 | current_status = NO_ADAPTER_DETECTED; | ||
| 221 | } | ||
| 222 | |||
| 223 | adapter_controllers_status.fill(ControllerTypes::None); | 149 | adapter_controllers_status.fill(ControllerTypes::None); |
| 150 | // Initialize all ports to store axis origin values | ||
| 151 | get_origin.fill(true); | ||
| 224 | 152 | ||
| 225 | // pointer to list of connected usb devices | 153 | // pointer to list of connected usb devices |
| 226 | libusb_device** devices{}; | 154 | libusb_device** devices{}; |
| @@ -229,8 +157,6 @@ void Adapter::Setup() { | |||
| 229 | const ssize_t device_count = libusb_get_device_list(libusb_ctx, &devices); | 157 | const ssize_t device_count = libusb_get_device_list(libusb_ctx, &devices); |
| 230 | if (device_count < 0) { | 158 | if (device_count < 0) { |
| 231 | LOG_ERROR(Input, "libusb_get_device_list failed with error: {}", device_count); | 159 | LOG_ERROR(Input, "libusb_get_device_list failed with error: {}", device_count); |
| 232 | detect_thread_running = false; // Stop the loop constantly checking for gc adapter | ||
| 233 | // TODO: For hotplug+gc adapter checkbox implementation, revert this. | ||
| 234 | return; | 160 | return; |
| 235 | } | 161 | } |
| 236 | 162 | ||
| @@ -244,9 +170,6 @@ void Adapter::Setup() { | |||
| 244 | } | 170 | } |
| 245 | libusb_free_device_list(devices, 1); | 171 | libusb_free_device_list(devices, 1); |
| 246 | } | 172 | } |
| 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; | ||
| 250 | } | 173 | } |
| 251 | 174 | ||
| 252 | bool Adapter::CheckDeviceAccess(libusb_device* device) { | 175 | bool Adapter::CheckDeviceAccess(libusb_device* device) { |
| @@ -331,24 +254,14 @@ void Adapter::GetGCEndpoint(libusb_device* device) { | |||
| 331 | sizeof(clear_payload), nullptr, 16); | 254 | sizeof(clear_payload), nullptr, 16); |
| 332 | 255 | ||
| 333 | adapter_thread_running = true; | 256 | adapter_thread_running = true; |
| 334 | current_status = ADAPTER_DETECTED; | ||
| 335 | adapter_input_thread = std::thread([=] { Read(); }); // Read input | 257 | adapter_input_thread = std::thread([=] { Read(); }); // Read input |
| 336 | } | 258 | } |
| 337 | 259 | ||
| 338 | Adapter::~Adapter() { | 260 | Adapter::~Adapter() { |
| 339 | StopScanThread(); | ||
| 340 | Reset(); | 261 | Reset(); |
| 341 | } | 262 | } |
| 342 | 263 | ||
| 343 | void Adapter::Reset() { | 264 | void Adapter::Reset() { |
| 344 | std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock); | ||
| 345 | if (!lock.try_lock()) { | ||
| 346 | return; | ||
| 347 | } | ||
| 348 | if (current_status != ADAPTER_DETECTED) { | ||
| 349 | return; | ||
| 350 | } | ||
| 351 | |||
| 352 | if (adapter_thread_running) { | 265 | if (adapter_thread_running) { |
| 353 | adapter_thread_running = false; | 266 | adapter_thread_running = false; |
| 354 | } | 267 | } |
| @@ -356,7 +269,6 @@ void Adapter::Reset() { | |||
| 356 | 269 | ||
| 357 | adapter_controllers_status.fill(ControllerTypes::None); | 270 | adapter_controllers_status.fill(ControllerTypes::None); |
| 358 | get_origin.fill(true); | 271 | get_origin.fill(true); |
| 359 | current_status = NO_ADAPTER_DETECTED; | ||
| 360 | 272 | ||
| 361 | if (usb_adapter_handle) { | 273 | if (usb_adapter_handle) { |
| 362 | libusb_release_interface(usb_adapter_handle, 1); | 274 | libusb_release_interface(usb_adapter_handle, 1); |
| @@ -409,24 +321,7 @@ const std::array<GCState, 4>& Adapter::GetPadState() const { | |||
| 409 | } | 321 | } |
| 410 | 322 | ||
| 411 | int Adapter::GetOriginValue(int port, int axis) const { | 323 | int Adapter::GetOriginValue(int port, int axis) const { |
| 412 | const auto& status = origin_status[port]; | 324 | return origin_status[port].axis_values[axis]; |
| 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 | } | 325 | } |
| 431 | 326 | ||
| 432 | } // namespace GCAdapter | 327 | } // namespace GCAdapter |