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