2020-06-21 17:36:28 +01:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
2020-06-21 20:31:57 +01:00
|
|
|
|
2020-06-22 03:58:53 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2020-06-21 17:36:28 +01:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "input_common/gcadapter/gc_adapter.h"
|
|
|
|
|
|
|
|
namespace GCAdapter {
|
|
|
|
|
2020-07-03 16:52:07 +01:00
|
|
|
/// Used to loop through and assign button in poller
|
|
|
|
constexpr std::array<PadButton, 12> PadButtonArray{
|
|
|
|
PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN,
|
|
|
|
PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R,
|
|
|
|
PadButton::PAD_TRIGGER_L, PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B,
|
|
|
|
PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START,
|
|
|
|
};
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
Adapter::Adapter() {
|
|
|
|
if (usb_adapter_handle != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG_INFO(Input, "GC Adapter Initialization started");
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
current_status = NO_ADAPTER_DETECTED;
|
|
|
|
libusb_init(&libusb_ctx);
|
2020-07-07 02:58:31 +01:00
|
|
|
get_origin.fill(true);
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
StartScanThread();
|
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) {
|
2020-06-21 17:36:28 +01:00
|
|
|
GCPadStatus pad = {};
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4);
|
2020-06-21 17:36:28 +01:00
|
|
|
adapter_controllers_status[port] = type;
|
|
|
|
|
2020-07-03 16:52:07 +01:00
|
|
|
static constexpr std::array<PadButton, 8> b1_buttons{
|
2020-06-24 16:39:30 +01:00
|
|
|
PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X,
|
|
|
|
PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT,
|
2020-07-03 16:52:07 +01:00
|
|
|
PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP,
|
|
|
|
};
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-07-03 16:52:07 +01:00
|
|
|
static constexpr std::array<PadButton, 4> b2_buttons{
|
|
|
|
PadButton::PAD_BUTTON_START,
|
|
|
|
PadButton::PAD_TRIGGER_Z,
|
|
|
|
PadButton::PAD_TRIGGER_R,
|
|
|
|
PadButton::PAD_TRIGGER_L,
|
|
|
|
};
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
if (adapter_controllers_status[port] != ControllerTypes::None) {
|
|
|
|
const u8 b1 = adapter_payload[1 + (9 * port) + 1];
|
|
|
|
const u8 b2 = adapter_payload[1 + (9 * port) + 2];
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-30 22:28:02 +01:00
|
|
|
for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
|
2020-07-03 16:52:07 +01:00
|
|
|
if ((b1 & (1U << i)) != 0) {
|
2020-06-24 16:39:30 +01:00
|
|
|
pad.button |= static_cast<u16>(b1_buttons[i]);
|
2020-06-23 22:37:15 +01:00
|
|
|
}
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-23 22:37:15 +01:00
|
|
|
|
2020-06-30 22:28:02 +01:00
|
|
|
for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
|
2020-07-03 16:52:07 +01:00
|
|
|
if ((b2 & (1U << j)) != 0) {
|
2020-06-24 16:39:30 +01:00
|
|
|
pad.button |= static_cast<u16>(b2_buttons[j]);
|
2020-06-23 22:37:15 +01:00
|
|
|
}
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
pad.stick_x = adapter_payload[1 + (9 * port) + 3];
|
|
|
|
pad.stick_y = adapter_payload[1 + (9 * port) + 4];
|
|
|
|
pad.substick_x = adapter_payload[1 + (9 * port) + 5];
|
|
|
|
pad.substick_y = adapter_payload[1 + (9 * port) + 6];
|
|
|
|
pad.trigger_left = adapter_payload[1 + (9 * port) + 7];
|
|
|
|
pad.trigger_right = adapter_payload[1 + (9 * port) + 8];
|
2020-07-07 02:58:31 +01:00
|
|
|
|
|
|
|
if (get_origin[port]) {
|
|
|
|
origin_status[port].stick_x = pad.stick_x;
|
|
|
|
origin_status[port].stick_y = pad.stick_y;
|
|
|
|
origin_status[port].substick_x = pad.substick_x;
|
|
|
|
origin_status[port].substick_y = pad.substick_y;
|
|
|
|
origin_status[port].trigger_left = pad.trigger_left;
|
|
|
|
origin_status[port].trigger_right = pad.trigger_right;
|
|
|
|
get_origin[port] = false;
|
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
return pad;
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:11:59 +01:00
|
|
|
void Adapter::PadToState(const GCPadStatus& pad, GCState& state) {
|
2020-06-27 04:46:49 +01:00
|
|
|
for (const auto& button : PadButtonArray) {
|
2020-06-30 22:28:02 +01:00
|
|
|
const u16 button_value = static_cast<u16>(button);
|
2020-06-24 16:39:30 +01:00
|
|
|
state.buttons.insert_or_assign(button_value, pad.button & button_value);
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickX), pad.stick_x);
|
|
|
|
state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickY), pad.stick_y);
|
|
|
|
state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickX), pad.substick_x);
|
|
|
|
state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickY), pad.substick_y);
|
|
|
|
state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerLeft), pad.trigger_left);
|
|
|
|
state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerRight), pad.trigger_right);
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::Read() {
|
2020-06-30 22:28:02 +01:00
|
|
|
LOG_DEBUG(Input, "GC Adapter Read() thread started");
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
int payload_size_in, payload_size_copy;
|
2020-06-22 23:11:59 +01:00
|
|
|
std::array<u8, 37> adapter_payload;
|
2020-06-23 22:37:15 +01:00
|
|
|
std::array<u8, 37> adapter_payload_copy;
|
|
|
|
std::array<GCPadStatus, 4> pads;
|
2020-06-22 23:11:59 +01:00
|
|
|
|
2020-06-21 17:36:28 +01:00
|
|
|
while (adapter_thread_running) {
|
2020-06-22 23:11:59 +01:00
|
|
|
libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(),
|
2020-06-26 00:31:51 +01:00
|
|
|
sizeof(adapter_payload), &payload_size_in, 16);
|
2020-06-23 22:37:15 +01:00
|
|
|
payload_size_copy = 0;
|
2020-07-01 17:52:50 +01:00
|
|
|
// this mutex might be redundant?
|
2020-06-21 17:36:28 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
|
|
|
std::copy(std::begin(adapter_payload), std::end(adapter_payload),
|
2020-06-23 22:37:15 +01:00
|
|
|
std::begin(adapter_payload_copy));
|
|
|
|
payload_size_copy = payload_size_in;
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-06-21 20:31:57 +01:00
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
if (payload_size_copy != sizeof(adapter_payload_copy) ||
|
|
|
|
adapter_payload_copy[0] != LIBUSB_DT_HID) {
|
2020-07-03 16:52:07 +01:00
|
|
|
LOG_ERROR(Input, "error reading payload (size: {}, type: {:02x})", payload_size_copy,
|
2020-06-23 22:37:15 +01:00
|
|
|
adapter_payload_copy[0]);
|
2020-06-26 00:31:51 +01:00
|
|
|
adapter_thread_running = false; // error reading from adapter, stop reading.
|
2020-06-30 22:28:02 +01:00
|
|
|
break;
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-06-30 22:28:02 +01:00
|
|
|
for (std::size_t port = 0; port < pads.size(); ++port) {
|
|
|
|
pads[port] = GetPadStatus(port, adapter_payload_copy);
|
2020-06-21 17:36:28 +01:00
|
|
|
if (DeviceConnected(port) && configuring) {
|
2020-07-07 02:58:31 +01:00
|
|
|
if (pads[port].button != 0) {
|
2020-06-23 22:37:15 +01:00
|
|
|
pad_queue[port].Push(pads[port]);
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
|
|
|
|
// Accounting for a threshold here because of some controller variance
|
2020-07-07 02:58:31 +01:00
|
|
|
if (pads[port].stick_x > origin_status[port].stick_x + pads[port].THRESHOLD ||
|
|
|
|
pads[port].stick_x < origin_status[port].stick_x - pads[port].THRESHOLD) {
|
2020-06-23 22:37:15 +01:00
|
|
|
pads[port].axis = GCAdapter::PadAxes::StickX;
|
|
|
|
pads[port].axis_value = pads[port].stick_x;
|
|
|
|
pad_queue[port].Push(pads[port]);
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-07-07 02:58:31 +01:00
|
|
|
if (pads[port].stick_y > origin_status[port].stick_y + pads[port].THRESHOLD ||
|
|
|
|
pads[port].stick_y < origin_status[port].stick_y - pads[port].THRESHOLD) {
|
2020-06-23 22:37:15 +01:00
|
|
|
pads[port].axis = GCAdapter::PadAxes::StickY;
|
|
|
|
pads[port].axis_value = pads[port].stick_y;
|
|
|
|
pad_queue[port].Push(pads[port]);
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-07-07 02:58:31 +01:00
|
|
|
if (pads[port].substick_x > origin_status[port].substick_x + pads[port].THRESHOLD ||
|
|
|
|
pads[port].substick_x < origin_status[port].substick_x - pads[port].THRESHOLD) {
|
2020-06-23 22:37:15 +01:00
|
|
|
pads[port].axis = GCAdapter::PadAxes::SubstickX;
|
|
|
|
pads[port].axis_value = pads[port].substick_x;
|
|
|
|
pad_queue[port].Push(pads[port]);
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-07-07 02:58:31 +01:00
|
|
|
if (pads[port].substick_y > origin_status[port].substick_y + pads[port].THRESHOLD ||
|
|
|
|
pads[port].substick_y < origin_status[port].substick_y - pads[port].THRESHOLD) {
|
2020-06-23 22:37:15 +01:00
|
|
|
pads[port].axis = GCAdapter::PadAxes::SubstickY;
|
|
|
|
pads[port].axis_value = pads[port].substick_y;
|
|
|
|
pad_queue[port].Push(pads[port]);
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-07-04 05:40:48 +01:00
|
|
|
if (pads[port].trigger_left > pads[port].TRIGGER_THRESHOLD) {
|
2020-07-02 20:54:44 +01:00
|
|
|
pads[port].axis = GCAdapter::PadAxes::TriggerLeft;
|
|
|
|
pads[port].axis_value = pads[port].trigger_left;
|
|
|
|
pad_queue[port].Push(pads[port]);
|
|
|
|
}
|
2020-07-04 05:40:48 +01:00
|
|
|
if (pads[port].trigger_right > pads[port].TRIGGER_THRESHOLD) {
|
2020-07-02 20:54:44 +01:00
|
|
|
pads[port].axis = GCAdapter::PadAxes::TriggerRight;
|
|
|
|
pads[port].axis_value = pads[port].trigger_right;
|
|
|
|
pad_queue[port].Push(pads[port]);
|
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
2020-06-23 22:37:15 +01:00
|
|
|
PadToState(pads[port], state[port]);
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
std::this_thread::yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::ScanThreadFunc() {
|
2020-06-21 17:36:28 +01:00
|
|
|
LOG_INFO(Input, "GC Adapter scanning thread started");
|
|
|
|
|
|
|
|
while (detect_thread_running) {
|
|
|
|
if (usb_adapter_handle == nullptr) {
|
|
|
|
std::lock_guard<std::mutex> lk(initialization_mutex);
|
|
|
|
Setup();
|
|
|
|
}
|
2020-06-22 03:58:53 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::StartScanThread() {
|
2020-06-21 20:31:57 +01:00
|
|
|
if (detect_thread_running) {
|
2020-06-21 17:36:28 +01:00
|
|
|
return;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
|
|
|
if (!libusb_ctx) {
|
2020-06-21 17:36:28 +01:00
|
|
|
return;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
|
|
|
|
detect_thread_running = true;
|
2020-06-21 23:43:01 +01:00
|
|
|
detect_thread = std::thread([=] { ScanThreadFunc(); });
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::StopScanThread() {
|
2020-06-23 17:47:58 +01:00
|
|
|
detect_thread_running = false;
|
2020-06-21 17:36:28 +01:00
|
|
|
detect_thread.join();
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::Setup() {
|
2020-06-21 17:36:28 +01:00
|
|
|
// Reset the error status in case the adapter gets unplugged
|
2020-06-21 20:31:57 +01:00
|
|
|
if (current_status < 0) {
|
2020-06-21 17:36:28 +01:00
|
|
|
current_status = NO_ADAPTER_DETECTED;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-22 02:15:58 +01:00
|
|
|
adapter_controllers_status.fill(ControllerTypes::None);
|
2020-06-21 20:31:57 +01:00
|
|
|
|
2020-06-30 22:28:02 +01:00
|
|
|
// pointer to list of connected usb devices
|
|
|
|
libusb_device** devices;
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-30 22:28:02 +01:00
|
|
|
// populate the list of devices, get the count
|
|
|
|
const std::size_t device_count = libusb_get_device_list(libusb_ctx, &devices);
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-30 22:28:02 +01:00
|
|
|
for (std::size_t index = 0; index < device_count; ++index) {
|
|
|
|
if (CheckDeviceAccess(devices[index])) {
|
2020-06-23 22:37:15 +01:00
|
|
|
// GC Adapter found and accessible, registering it
|
2020-06-30 22:28:02 +01:00
|
|
|
GetGCEndpoint(devices[index]);
|
2020-06-21 17:36:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
bool Adapter::CheckDeviceAccess(libusb_device* device) {
|
2020-06-21 17:36:28 +01:00
|
|
|
libusb_device_descriptor desc;
|
2020-06-23 22:37:15 +01:00
|
|
|
const int get_descriptor_error = libusb_get_device_descriptor(device, &desc);
|
|
|
|
if (get_descriptor_error) {
|
2020-06-21 17:36:28 +01:00
|
|
|
// could not acquire the descriptor, no point in trying to use it.
|
2020-07-03 16:52:07 +01:00
|
|
|
LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: {}",
|
2020-06-23 22:37:15 +01:00
|
|
|
get_descriptor_error);
|
2020-06-21 17:36:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
|
2020-06-27 04:46:49 +01:00
|
|
|
// This isn't the device we are looking for.
|
2020-06-21 17:36:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-23 22:37:15 +01:00
|
|
|
const int open_error = libusb_open(device, &usb_adapter_handle);
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
if (open_error == LIBUSB_ERROR_ACCESS) {
|
2020-07-03 16:52:07 +01:00
|
|
|
LOG_ERROR(Input, "Yuzu can not gain access to this device: ID {:04X}:{:04X}.",
|
|
|
|
desc.idVendor, desc.idProduct);
|
2020-06-21 17:36:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-23 22:37:15 +01:00
|
|
|
if (open_error) {
|
2020-07-03 16:52:07 +01:00
|
|
|
LOG_ERROR(Input, "libusb_open failed to open device with error = {}", open_error);
|
2020-06-21 17:36:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
int kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle, 0);
|
|
|
|
if (kernel_driver_error == 1) {
|
|
|
|
kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle, 0);
|
|
|
|
if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
|
2020-07-03 16:52:07 +01:00
|
|
|
LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}",
|
2020-06-23 22:37:15 +01:00
|
|
|
kernel_driver_error);
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
|
2020-06-21 17:36:28 +01:00
|
|
|
libusb_close(usb_adapter_handle);
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:37:15 +01:00
|
|
|
const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0);
|
|
|
|
if (interface_claim_error) {
|
2020-07-03 16:52:07 +01:00
|
|
|
LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error);
|
2020-06-21 17:36:28 +01:00
|
|
|
libusb_close(usb_adapter_handle);
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::GetGCEndpoint(libusb_device* device) {
|
2020-06-21 17:36:28 +01:00
|
|
|
libusb_config_descriptor* config = nullptr;
|
|
|
|
libusb_get_config_descriptor(device, 0, &config);
|
|
|
|
for (u8 ic = 0; ic < config->bNumInterfaces; ic++) {
|
|
|
|
const libusb_interface* interfaceContainer = &config->interface[ic];
|
|
|
|
for (int i = 0; i < interfaceContainer->num_altsetting; i++) {
|
|
|
|
const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
|
|
|
|
for (u8 e = 0; e < interface->bNumEndpoints; e++) {
|
|
|
|
const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
|
2020-06-21 20:31:57 +01:00
|
|
|
if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
|
2020-06-21 17:36:28 +01:00
|
|
|
input_endpoint = endpoint->bEndpointAddress;
|
2020-07-01 17:52:50 +01:00
|
|
|
} else {
|
|
|
|
output_endpoint = endpoint->bEndpointAddress;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 17:52:50 +01:00
|
|
|
// This transfer seems to be responsible for clearing the state of the adapter
|
|
|
|
// Used to clear the "busy" state of when the device is unexpectedly unplugged
|
|
|
|
unsigned char clear_payload = 0x13;
|
|
|
|
libusb_interrupt_transfer(usb_adapter_handle, output_endpoint, &clear_payload,
|
|
|
|
sizeof(clear_payload), nullptr, 16);
|
2020-06-21 17:36:28 +01:00
|
|
|
|
|
|
|
adapter_thread_running = true;
|
|
|
|
current_status = ADAPTER_DETECTED;
|
2020-06-21 23:43:01 +01:00
|
|
|
adapter_input_thread = std::thread([=] { Read(); }); // Read input
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
Adapter::~Adapter() {
|
2020-06-21 17:36:28 +01:00
|
|
|
StopScanThread();
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::Reset() {
|
2020-06-21 17:36:28 +01:00
|
|
|
std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock);
|
2020-06-21 20:31:57 +01:00
|
|
|
if (!lock.try_lock()) {
|
2020-06-21 17:36:28 +01:00
|
|
|
return;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
|
|
|
if (current_status != ADAPTER_DETECTED) {
|
2020-06-21 17:36:28 +01:00
|
|
|
return;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-21 20:31:57 +01:00
|
|
|
if (adapter_thread_running) {
|
2020-06-23 17:47:58 +01:00
|
|
|
adapter_thread_running = false;
|
2020-06-21 20:31:57 +01:00
|
|
|
}
|
2020-06-26 00:31:51 +01:00
|
|
|
adapter_input_thread.join();
|
2020-06-21 17:36:28 +01:00
|
|
|
|
2020-06-22 02:15:58 +01:00
|
|
|
adapter_controllers_status.fill(ControllerTypes::None);
|
2020-07-07 02:58:31 +01:00
|
|
|
get_origin.fill(true);
|
2020-06-21 17:36:28 +01:00
|
|
|
current_status = NO_ADAPTER_DETECTED;
|
|
|
|
|
|
|
|
if (usb_adapter_handle) {
|
2020-06-26 00:31:51 +01:00
|
|
|
libusb_release_interface(usb_adapter_handle, 1);
|
2020-06-21 17:36:28 +01:00
|
|
|
libusb_close(usb_adapter_handle);
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
}
|
2020-06-23 17:47:58 +01:00
|
|
|
|
|
|
|
if (libusb_ctx) {
|
|
|
|
libusb_exit(libusb_ctx);
|
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
bool Adapter::DeviceConnected(int port) {
|
|
|
|
return adapter_controllers_status[port] != ControllerTypes::None;
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::ResetDeviceType(int port) {
|
|
|
|
adapter_controllers_status[port] = ControllerTypes::None;
|
2020-06-21 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::BeginConfiguration() {
|
2020-07-07 02:58:31 +01:00
|
|
|
get_origin.fill(true);
|
2020-06-22 02:15:58 +01:00
|
|
|
for (auto& pq : pad_queue) {
|
|
|
|
pq.Clear();
|
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
configuring = true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
void Adapter::EndConfiguration() {
|
2020-06-22 02:15:58 +01:00
|
|
|
for (auto& pq : pad_queue) {
|
|
|
|
pq.Clear();
|
|
|
|
}
|
2020-06-21 17:36:28 +01:00
|
|
|
configuring = false;
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
|
|
|
|
return pad_queue;
|
|
|
|
}
|
|
|
|
|
2020-06-22 02:50:58 +01:00
|
|
|
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
|
|
|
|
return pad_queue;
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:43:01 +01:00
|
|
|
std::array<GCState, 4>& Adapter::GetPadState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2020-06-22 02:50:58 +01:00
|
|
|
const std::array<GCState, 4>& Adapter::GetPadState() const {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2020-07-07 02:58:31 +01:00
|
|
|
int Adapter::GetOriginValue(int port, int axis) {
|
|
|
|
const PadAxes padaxis = static_cast<PadAxes>(axis);
|
|
|
|
if (padaxis == PadAxes::StickX)
|
|
|
|
return origin_status[port].stick_x;
|
|
|
|
if (padaxis == PadAxes::StickY)
|
|
|
|
return origin_status[port].stick_y;
|
|
|
|
if (padaxis == PadAxes::SubstickX)
|
|
|
|
return origin_status[port].substick_x;
|
|
|
|
if (padaxis == PadAxes::SubstickY)
|
|
|
|
return origin_status[port].substick_x;
|
|
|
|
if (padaxis == PadAxes::TriggerLeft)
|
|
|
|
return origin_status[port].trigger_left;
|
|
|
|
if (padaxis == PadAxes::TriggerRight)
|
|
|
|
return origin_status[port].trigger_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int Adapter::GetOriginValue(int port, int axis) const {
|
|
|
|
const PadAxes padaxis = static_cast<PadAxes>(axis);
|
|
|
|
if (padaxis == PadAxes::StickX)
|
|
|
|
return origin_status[port].stick_x;
|
|
|
|
if (padaxis == PadAxes::StickY)
|
|
|
|
return origin_status[port].stick_y;
|
|
|
|
if (padaxis == PadAxes::SubstickX)
|
|
|
|
return origin_status[port].substick_x;
|
|
|
|
if (padaxis == PadAxes::SubstickY)
|
|
|
|
return origin_status[port].substick_x;
|
|
|
|
if (padaxis == PadAxes::TriggerLeft)
|
|
|
|
return origin_status[port].trigger_left;
|
|
|
|
if (padaxis == PadAxes::TriggerRight)
|
|
|
|
return origin_status[port].trigger_right;
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:11:59 +01:00
|
|
|
} // namespace GCAdapter
|