diff options
Diffstat (limited to 'src/input_common')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 178 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.h | 54 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 24 |
3 files changed, 75 insertions, 181 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 |
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index e2cdd6255..bed81915c 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h | |||
| @@ -17,12 +17,6 @@ struct libusb_device_handle; | |||
| 17 | 17 | ||
| 18 | namespace GCAdapter { | 18 | namespace GCAdapter { |
| 19 | 19 | ||
| 20 | enum { | ||
| 21 | PAD_USE_ORIGIN = 0x0080, | ||
| 22 | PAD_GET_ORIGIN = 0x2000, | ||
| 23 | PAD_ERR_STATUS = 0x8000, | ||
| 24 | }; | ||
| 25 | |||
| 26 | enum class PadButton { | 20 | enum class PadButton { |
| 27 | PAD_BUTTON_LEFT = 0x0001, | 21 | PAD_BUTTON_LEFT = 0x0001, |
| 28 | PAD_BUTTON_RIGHT = 0x0002, | 22 | PAD_BUTTON_RIGHT = 0x0002, |
| @@ -53,24 +47,10 @@ enum class PadAxes : u8 { | |||
| 53 | }; | 47 | }; |
| 54 | 48 | ||
| 55 | struct GCPadStatus { | 49 | struct GCPadStatus { |
| 56 | u16 button{}; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits | 50 | u16 button{}; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits |
| 57 | u8 stick_x{}; // 0 <= stick_x <= 255 | 51 | |
| 58 | u8 stick_y{}; // 0 <= stick_y <= 255 | 52 | std::array<u8, 6> axis_values{}; // Triggers and sticks, following indices defined in PadAxes |
| 59 | u8 substick_x{}; // 0 <= substick_x <= 255 | 53 | static constexpr u8 THRESHOLD = 50; // Threshold for axis press for polling |
| 60 | u8 substick_y{}; // 0 <= substick_y <= 255 | ||
| 61 | u8 trigger_left{}; // 0 <= trigger_left <= 255 | ||
| 62 | u8 trigger_right{}; // 0 <= trigger_right <= 255 | ||
| 63 | |||
| 64 | static constexpr u8 MAIN_STICK_CENTER_X = 0x80; | ||
| 65 | static constexpr u8 MAIN_STICK_CENTER_Y = 0x80; | ||
| 66 | static constexpr u8 MAIN_STICK_RADIUS = 0x7f; | ||
| 67 | static constexpr u8 C_STICK_CENTER_X = 0x80; | ||
| 68 | static constexpr u8 C_STICK_CENTER_Y = 0x80; | ||
| 69 | static constexpr u8 C_STICK_RADIUS = 0x7f; | ||
| 70 | static constexpr u8 THRESHOLD = 10; | ||
| 71 | |||
| 72 | // 256/4, at least a quarter press to count as a press. For polling mostly | ||
| 73 | static constexpr u8 TRIGGER_THRESHOLD = 64; | ||
| 74 | 54 | ||
| 75 | u8 port{}; | 55 | u8 port{}; |
| 76 | PadAxes axis{PadAxes::Undefined}; | 56 | PadAxes axis{PadAxes::Undefined}; |
| @@ -84,11 +64,6 @@ struct GCState { | |||
| 84 | 64 | ||
| 85 | enum class ControllerTypes { None, Wired, Wireless }; | 65 | enum class ControllerTypes { None, Wired, Wireless }; |
| 86 | 66 | ||
| 87 | enum { | ||
| 88 | NO_ADAPTER_DETECTED = 0, | ||
| 89 | ADAPTER_DETECTED = 1, | ||
| 90 | }; | ||
| 91 | |||
| 92 | class Adapter { | 67 | class Adapter { |
| 93 | public: | 68 | public: |
| 94 | /// Initialize the GC Adapter capture and read sequence | 69 | /// Initialize the GC Adapter capture and read sequence |
| @@ -109,18 +84,14 @@ public: | |||
| 109 | std::array<GCState, 4>& GetPadState(); | 84 | std::array<GCState, 4>& GetPadState(); |
| 110 | const std::array<GCState, 4>& GetPadState() const; | 85 | const std::array<GCState, 4>& GetPadState() const; |
| 111 | 86 | ||
| 87 | int GetOriginValue(int port, int axis) const; | ||
| 88 | |||
| 112 | private: | 89 | private: |
| 113 | GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); | 90 | GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); |
| 114 | 91 | ||
| 115 | void PadToState(const GCPadStatus& pad, GCState& state); | 92 | void PadToState(const GCPadStatus& pad, GCState& state); |
| 116 | 93 | ||
| 117 | void Read(); | 94 | void Read(); |
| 118 | void ScanThreadFunc(); | ||
| 119 | /// Begin scanning for the GC Adapter. | ||
| 120 | void StartScanThread(); | ||
| 121 | |||
| 122 | /// Stop scanning for the adapter | ||
| 123 | void StopScanThread(); | ||
| 124 | 95 | ||
| 125 | /// Resets status of device connected to port | 96 | /// Resets status of device connected to port |
| 126 | void ResetDeviceType(std::size_t port); | 97 | void ResetDeviceType(std::size_t port); |
| @@ -137,19 +108,11 @@ private: | |||
| 137 | /// For use in initialization, querying devices to find the adapter | 108 | /// For use in initialization, querying devices to find the adapter |
| 138 | void Setup(); | 109 | void Setup(); |
| 139 | 110 | ||
| 140 | int current_status = NO_ADAPTER_DETECTED; | ||
| 141 | libusb_device_handle* usb_adapter_handle = nullptr; | 111 | libusb_device_handle* usb_adapter_handle = nullptr; |
| 142 | std::array<ControllerTypes, 4> adapter_controllers_status{}; | ||
| 143 | |||
| 144 | std::mutex s_mutex; | ||
| 145 | 112 | ||
| 146 | std::thread adapter_input_thread; | 113 | std::thread adapter_input_thread; |
| 147 | bool adapter_thread_running; | 114 | bool adapter_thread_running; |
| 148 | 115 | ||
| 149 | std::mutex initialization_mutex; | ||
| 150 | std::thread detect_thread; | ||
| 151 | bool detect_thread_running = false; | ||
| 152 | |||
| 153 | libusb_context* libusb_ctx; | 116 | libusb_context* libusb_ctx; |
| 154 | 117 | ||
| 155 | u8 input_endpoint = 0; | 118 | u8 input_endpoint = 0; |
| @@ -157,8 +120,11 @@ private: | |||
| 157 | 120 | ||
| 158 | bool configuring = false; | 121 | bool configuring = false; |
| 159 | 122 | ||
| 160 | std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue; | ||
| 161 | std::array<GCState, 4> state; | 123 | std::array<GCState, 4> state; |
| 124 | std::array<bool, 4> get_origin; | ||
| 125 | std::array<GCPadStatus, 4> origin_status; | ||
| 126 | std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue; | ||
| 127 | std::array<ControllerTypes, 4> adapter_controllers_status{}; | ||
| 162 | }; | 128 | }; |
| 163 | 129 | ||
| 164 | } // namespace GCAdapter | 130 | } // namespace GCAdapter |
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index b20419ec3..96e22d3ad 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -38,18 +38,12 @@ public: | |||
| 38 | explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, | 38 | explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, |
| 39 | GCAdapter::Adapter* adapter) | 39 | GCAdapter::Adapter* adapter) |
| 40 | : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), | 40 | : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), |
| 41 | gcadapter(adapter) { | 41 | gcadapter(adapter), origin_value(adapter->GetOriginValue(port_, axis_)) {} |
| 42 | // L/R triggers range is only in positive direction beginning near 0 | ||
| 43 | // 0.0 threshold equates to near half trigger press, but threshold accounts for variability. | ||
| 44 | if (axis > 3) { | ||
| 45 | threshold *= -0.5; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | 42 | ||
| 49 | bool GetStatus() const override { | 43 | bool GetStatus() const override { |
| 50 | if (gcadapter->DeviceConnected(port)) { | 44 | if (gcadapter->DeviceConnected(port)) { |
| 51 | const float axis_value = | 45 | const float current_axis_value = gcadapter->GetPadState()[port].axes.at(axis); |
| 52 | (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; | 46 | const float axis_value = (current_axis_value - origin_value) / 128.0f; |
| 53 | if (trigger_if_greater) { | 47 | if (trigger_if_greater) { |
| 54 | // TODO: Might be worthwile to set a slider for the trigger threshold. It is | 48 | // TODO: Might be worthwile to set a slider for the trigger threshold. It is |
| 55 | // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick | 49 | // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick |
| @@ -66,6 +60,7 @@ private: | |||
| 66 | float threshold; | 60 | float threshold; |
| 67 | bool trigger_if_greater; | 61 | bool trigger_if_greater; |
| 68 | GCAdapter::Adapter* gcadapter; | 62 | GCAdapter::Adapter* gcadapter; |
| 63 | const float origin_value; | ||
| 69 | }; | 64 | }; |
| 70 | 65 | ||
| 71 | GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | 66 | GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) |
| @@ -155,15 +150,18 @@ void GCButtonFactory::EndConfiguration() { | |||
| 155 | class GCAnalog final : public Input::AnalogDevice { | 150 | class GCAnalog final : public Input::AnalogDevice { |
| 156 | public: | 151 | public: |
| 157 | GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) | 152 | GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) |
| 158 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} | 153 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), |
| 154 | origin_value_x(adapter->GetOriginValue(port_, axis_x_)), | ||
| 155 | origin_value_y(adapter->GetOriginValue(port_, axis_y_)) {} | ||
| 159 | 156 | ||
| 160 | float GetAxis(int axis) const { | 157 | float GetAxis(int axis) const { |
| 161 | if (gcadapter->DeviceConnected(port)) { | 158 | if (gcadapter->DeviceConnected(port)) { |
| 162 | std::lock_guard lock{mutex}; | 159 | std::lock_guard lock{mutex}; |
| 160 | const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; | ||
| 163 | // division is not by a perfect 128 to account for some variance in center location | 161 | // division is not by a perfect 128 to account for some variance in center location |
| 164 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range | 162 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range |
| 165 | // [20-230] | 163 | // [20-230] |
| 166 | return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; | 164 | return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / 95.0f; |
| 167 | } | 165 | } |
| 168 | return 0.0f; | 166 | return 0.0f; |
| 169 | } | 167 | } |
| @@ -215,8 +213,10 @@ private: | |||
| 215 | const int axis_x; | 213 | const int axis_x; |
| 216 | const int axis_y; | 214 | const int axis_y; |
| 217 | const float deadzone; | 215 | const float deadzone; |
| 218 | mutable std::mutex mutex; | ||
| 219 | GCAdapter::Adapter* gcadapter; | 216 | GCAdapter::Adapter* gcadapter; |
| 217 | const float origin_value_x; | ||
| 218 | const float origin_value_y; | ||
| 219 | mutable std::mutex mutex; | ||
| 220 | }; | 220 | }; |
| 221 | 221 | ||
| 222 | /// An analog device factory that creates analog devices from GC Adapter | 222 | /// An analog device factory that creates analog devices from GC Adapter |