2020-06-21 18:36:28 +02:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
2020-06-21 21:31:57 +02:00
|
|
|
|
2020-06-22 04:58:53 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2020-09-15 00:34:39 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4200) // nonstandard extension used : zero-sized array in struct/union
|
|
|
|
#endif
|
2020-07-13 20:48:19 +02:00
|
|
|
#include <libusb.h>
|
2020-09-15 00:34:39 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
#include "common/logging/log.h"
|
2020-09-02 01:17:59 +02:00
|
|
|
#include "common/param_package.h"
|
2020-06-21 18:36:28 +02:00
|
|
|
#include "input_common/gcadapter/gc_adapter.h"
|
2020-09-02 01:17:59 +02:00
|
|
|
#include "input_common/settings.h"
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
namespace GCAdapter {
|
|
|
|
|
2020-10-14 08:51:14 +02:00
|
|
|
// Used to loop through and assign button in poller
|
2020-07-03 17:52:07 +02:00
|
|
|
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-10-14 08:51:14 +02:00
|
|
|
static void PadToState(const GCPadStatus& pad, GCState& out_state) {
|
|
|
|
for (const auto& button : PadButtonArray) {
|
|
|
|
const auto button_key = static_cast<u16>(button);
|
|
|
|
const auto button_value = (pad.button & button_key) != 0;
|
|
|
|
out_state.buttons.insert_or_assign(static_cast<s32>(button_key), button_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < pad.axis_values.size(); ++i) {
|
|
|
|
out_state.axes.insert_or_assign(static_cast<u32>(i), pad.axis_values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
Adapter::Adapter() {
|
|
|
|
if (usb_adapter_handle != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG_INFO(Input, "GC Adapter Initialization started");
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-08 19:19:33 +02:00
|
|
|
const int init_res = libusb_init(&libusb_ctx);
|
|
|
|
if (init_res == LIBUSB_SUCCESS) {
|
2020-07-16 19:00:04 +02:00
|
|
|
Setup();
|
2020-07-08 19:19:33 +02:00
|
|
|
} else {
|
|
|
|
LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res);
|
|
|
|
}
|
2020-06-22 00:43:01 +02:00
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-12 21:36:27 +02:00
|
|
|
GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) {
|
2020-06-21 18:36:28 +02:00
|
|
|
GCPadStatus pad = {};
|
2020-07-17 18:10:32 +02:00
|
|
|
const std::size_t offset = 1 + (9 * port);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-16 19:00:04 +02:00
|
|
|
adapter_controllers_status[port] = static_cast<ControllerTypes>(adapter_payload[offset] >> 4);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-03 17:52:07 +02:00
|
|
|
static constexpr std::array<PadButton, 8> b1_buttons{
|
2020-06-24 17:39:30 +02: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 17:52:07 +02:00
|
|
|
PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP,
|
|
|
|
};
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-03 17:52:07 +02: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 18:36:28 +02:00
|
|
|
|
2020-07-16 19:00:04 +02:00
|
|
|
static constexpr std::array<PadAxes, 6> axes{
|
|
|
|
PadAxes::StickX, PadAxes::StickY, PadAxes::SubstickX,
|
|
|
|
PadAxes::SubstickY, PadAxes::TriggerLeft, PadAxes::TriggerRight,
|
|
|
|
};
|
|
|
|
|
2020-07-07 04:09:07 +02:00
|
|
|
if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) {
|
|
|
|
// Controller may have been disconnected, recalibrate if reconnected.
|
|
|
|
get_origin[port] = true;
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:37:15 +02:00
|
|
|
if (adapter_controllers_status[port] != ControllerTypes::None) {
|
2020-07-16 19:00:04 +02:00
|
|
|
const u8 b1 = adapter_payload[offset + 1];
|
|
|
|
const u8 b2 = adapter_payload[offset + 2];
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-30 23:28:02 +02:00
|
|
|
for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
|
2020-07-03 17:52:07 +02:00
|
|
|
if ((b1 & (1U << i)) != 0) {
|
2020-10-14 08:51:14 +02:00
|
|
|
pad.button = static_cast<u16>(pad.button | static_cast<u16>(b1_buttons[i]));
|
2020-06-23 23:37:15 +02:00
|
|
|
}
|
2020-06-21 21:31:57 +02:00
|
|
|
}
|
2020-06-23 23:37:15 +02:00
|
|
|
|
2020-06-30 23:28:02 +02:00
|
|
|
for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
|
2020-07-03 17:52:07 +02:00
|
|
|
if ((b2 & (1U << j)) != 0) {
|
2020-10-14 08:51:14 +02:00
|
|
|
pad.button = static_cast<u16>(pad.button | static_cast<u16>(b2_buttons[j]));
|
2020-06-23 23:37:15 +02:00
|
|
|
}
|
2020-06-21 21:31:57 +02:00
|
|
|
}
|
2020-07-16 19:00:04 +02:00
|
|
|
for (PadAxes axis : axes) {
|
2020-10-14 08:51:14 +02:00
|
|
|
const auto index = static_cast<std::size_t>(axis);
|
2020-07-16 19:00:04 +02:00
|
|
|
pad.axis_values[index] = adapter_payload[offset + 3 + index];
|
|
|
|
}
|
2020-07-07 03:58:31 +02:00
|
|
|
|
|
|
|
if (get_origin[port]) {
|
2020-07-16 19:00:04 +02:00
|
|
|
origin_status[port].axis_values = pad.axis_values;
|
2020-07-07 03:58:31 +02:00
|
|
|
get_origin[port] = false;
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
return pad;
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Adapter::Read() {
|
2020-06-30 23:28:02 +02:00
|
|
|
LOG_DEBUG(Input, "GC Adapter Read() thread started");
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-16 19:00:04 +02:00
|
|
|
int payload_size;
|
2020-06-23 00:11:59 +02:00
|
|
|
std::array<u8, 37> adapter_payload;
|
2020-06-23 23:37:15 +02:00
|
|
|
std::array<GCPadStatus, 4> pads;
|
2020-06-23 00:11:59 +02:00
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
while (adapter_thread_running) {
|
2020-06-23 00:11:59 +02:00
|
|
|
libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(),
|
2020-07-16 19:00:04 +02:00
|
|
|
sizeof(adapter_payload), &payload_size, 16);
|
2020-06-21 21:31:57 +02:00
|
|
|
|
2020-07-16 19:00:04 +02:00
|
|
|
if (payload_size != sizeof(adapter_payload) || adapter_payload[0] != LIBUSB_DT_HID) {
|
2020-07-17 18:10:32 +02:00
|
|
|
LOG_ERROR(Input,
|
|
|
|
"Error reading payload (size: {}, type: {:02x}) Is the adapter connected?",
|
2020-07-16 19:00:04 +02:00
|
|
|
payload_size, adapter_payload[0]);
|
2020-06-26 01:31:51 +02:00
|
|
|
adapter_thread_running = false; // error reading from adapter, stop reading.
|
2020-06-30 23:28:02 +02:00
|
|
|
break;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-06-30 23:28:02 +02:00
|
|
|
for (std::size_t port = 0; port < pads.size(); ++port) {
|
2020-07-16 19:00:04 +02:00
|
|
|
pads[port] = GetPadStatus(port, adapter_payload);
|
2020-06-21 18:36:28 +02:00
|
|
|
if (DeviceConnected(port) && configuring) {
|
2020-07-07 03:58:31 +02:00
|
|
|
if (pads[port].button != 0) {
|
2020-06-23 23:37:15 +02:00
|
|
|
pad_queue[port].Push(pads[port]);
|
2020-06-21 21:31:57 +02:00
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-16 19:00:04 +02:00
|
|
|
// Accounting for a threshold here to ensure an intentional press
|
|
|
|
for (size_t i = 0; i < pads[port].axis_values.size(); ++i) {
|
|
|
|
const u8 value = pads[port].axis_values[i];
|
|
|
|
const u8 origin = origin_status[port].axis_values[i];
|
|
|
|
|
|
|
|
if (value > origin + pads[port].THRESHOLD ||
|
|
|
|
value < origin - pads[port].THRESHOLD) {
|
|
|
|
pads[port].axis = static_cast<PadAxes>(i);
|
|
|
|
pads[port].axis_value = pads[port].axis_values[i];
|
|
|
|
pad_queue[port].Push(pads[port]);
|
|
|
|
}
|
2020-07-02 21:54:44 +02:00
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-06-23 23:37:15 +02:00
|
|
|
PadToState(pads[port], state[port]);
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
std::this_thread::yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Adapter::Setup() {
|
2020-07-16 19:00:04 +02:00
|
|
|
// Initialize all controllers as unplugged
|
2020-06-22 03:15:58 +02:00
|
|
|
adapter_controllers_status.fill(ControllerTypes::None);
|
2020-07-16 19:00:04 +02:00
|
|
|
// Initialize all ports to store axis origin values
|
|
|
|
get_origin.fill(true);
|
2020-06-21 21:31:57 +02:00
|
|
|
|
2020-06-30 23:28:02 +02:00
|
|
|
// pointer to list of connected usb devices
|
2020-07-08 20:18:54 +02:00
|
|
|
libusb_device** devices{};
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-30 23:28:02 +02:00
|
|
|
// populate the list of devices, get the count
|
2020-07-08 20:18:54 +02:00
|
|
|
const ssize_t device_count = libusb_get_device_list(libusb_ctx, &devices);
|
2020-07-08 19:19:33 +02:00
|
|
|
if (device_count < 0) {
|
|
|
|
LOG_ERROR(Input, "libusb_get_device_list failed with error: {}", device_count);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-08 20:18:54 +02:00
|
|
|
if (devices != nullptr) {
|
2020-07-12 21:36:27 +02:00
|
|
|
for (std::size_t index = 0; index < static_cast<std::size_t>(device_count); ++index) {
|
2020-07-08 20:18:54 +02:00
|
|
|
if (CheckDeviceAccess(devices[index])) {
|
|
|
|
// GC Adapter found and accessible, registering it
|
|
|
|
GetGCEndpoint(devices[index]);
|
|
|
|
break;
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-07-08 20:18:54 +02:00
|
|
|
libusb_free_device_list(devices, 1);
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
bool Adapter::CheckDeviceAccess(libusb_device* device) {
|
2020-06-21 18:36:28 +02:00
|
|
|
libusb_device_descriptor desc;
|
2020-06-23 23:37:15 +02:00
|
|
|
const int get_descriptor_error = libusb_get_device_descriptor(device, &desc);
|
|
|
|
if (get_descriptor_error) {
|
2020-06-21 18:36:28 +02:00
|
|
|
// could not acquire the descriptor, no point in trying to use it.
|
2020-07-03 17:52:07 +02:00
|
|
|
LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: {}",
|
2020-06-23 23:37:15 +02:00
|
|
|
get_descriptor_error);
|
2020-06-21 18:36:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
|
2020-06-27 05:46:49 +02:00
|
|
|
// This isn't the device we are looking for.
|
2020-06-21 18:36:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-23 23:37:15 +02:00
|
|
|
const int open_error = libusb_open(device, &usb_adapter_handle);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-23 23:37:15 +02:00
|
|
|
if (open_error == LIBUSB_ERROR_ACCESS) {
|
2020-07-03 17:52:07 +02:00
|
|
|
LOG_ERROR(Input, "Yuzu can not gain access to this device: ID {:04X}:{:04X}.",
|
|
|
|
desc.idVendor, desc.idProduct);
|
2020-06-21 18:36:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-23 23:37:15 +02:00
|
|
|
if (open_error) {
|
2020-07-03 17:52:07 +02:00
|
|
|
LOG_ERROR(Input, "libusb_open failed to open device with error = {}", open_error);
|
2020-06-21 18:36:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:37:15 +02: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 17:52:07 +02:00
|
|
|
LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}",
|
2020-06-23 23:37:15 +02:00
|
|
|
kernel_driver_error);
|
2020-06-21 21:31:57 +02:00
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 23:37:15 +02:00
|
|
|
if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
|
2020-06-21 18:36:28 +02:00
|
|
|
libusb_close(usb_adapter_handle);
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:37:15 +02:00
|
|
|
const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0);
|
|
|
|
if (interface_claim_error) {
|
2020-07-03 17:52:07 +02:00
|
|
|
LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error);
|
2020-06-21 18:36:28 +02:00
|
|
|
libusb_close(usb_adapter_handle);
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Adapter::GetGCEndpoint(libusb_device* device) {
|
2020-06-21 18:36:28 +02:00
|
|
|
libusb_config_descriptor* config = nullptr;
|
2020-07-08 20:18:54 +02:00
|
|
|
const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config);
|
|
|
|
if (config_descriptor_return != LIBUSB_SUCCESS) {
|
2020-07-08 19:19:33 +02:00
|
|
|
LOG_ERROR(Input, "libusb_get_config_descriptor failed with error = {}",
|
2020-07-08 20:18:54 +02:00
|
|
|
config_descriptor_return);
|
2020-07-08 19:19:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
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-10-14 08:51:14 +02:00
|
|
|
if ((endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) != 0) {
|
2020-06-21 18:36:28 +02:00
|
|
|
input_endpoint = endpoint->bEndpointAddress;
|
2020-07-01 18:52:50 +02:00
|
|
|
} else {
|
|
|
|
output_endpoint = endpoint->bEndpointAddress;
|
2020-06-21 21:31:57 +02:00
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 18:52:50 +02: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 18:36:28 +02:00
|
|
|
|
|
|
|
adapter_thread_running = true;
|
2020-07-25 09:38:22 +02:00
|
|
|
adapter_input_thread = std::thread(&Adapter::Read, this);
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
Adapter::~Adapter() {
|
2020-06-21 18:36:28 +02:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Adapter::Reset() {
|
2020-06-21 21:31:57 +02:00
|
|
|
if (adapter_thread_running) {
|
2020-06-23 18:47:58 +02:00
|
|
|
adapter_thread_running = false;
|
2020-06-21 21:31:57 +02:00
|
|
|
}
|
2020-07-26 09:05:57 +02:00
|
|
|
if (adapter_input_thread.joinable()) {
|
|
|
|
adapter_input_thread.join();
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 03:15:58 +02:00
|
|
|
adapter_controllers_status.fill(ControllerTypes::None);
|
2020-07-07 03:58:31 +02:00
|
|
|
get_origin.fill(true);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
if (usb_adapter_handle) {
|
2020-06-26 01:31:51 +02:00
|
|
|
libusb_release_interface(usb_adapter_handle, 1);
|
2020-06-21 18:36:28 +02:00
|
|
|
libusb_close(usb_adapter_handle);
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
}
|
2020-06-23 18:47:58 +02:00
|
|
|
|
|
|
|
if (libusb_ctx) {
|
|
|
|
libusb_exit(libusb_ctx);
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 01:17:59 +02:00
|
|
|
std::vector<Common::ParamPackage> Adapter::GetInputDevices() const {
|
|
|
|
std::vector<Common::ParamPackage> devices;
|
|
|
|
for (std::size_t port = 0; port < state.size(); ++port) {
|
|
|
|
if (!DeviceConnected(port)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string name = fmt::format("Gamecube Controller {}", port);
|
|
|
|
devices.emplace_back(Common::ParamPackage{
|
|
|
|
{"class", "gcpad"},
|
|
|
|
{"display", std::move(name)},
|
|
|
|
{"port", std::to_string(port)},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputCommon::ButtonMapping Adapter::GetButtonMappingForDevice(
|
|
|
|
const Common::ParamPackage& params) const {
|
|
|
|
// This list is missing ZL/ZR since those are not considered buttons.
|
|
|
|
// We will add those afterwards
|
|
|
|
// This list also excludes any button that can't be really mapped
|
|
|
|
static constexpr std::array<std::pair<Settings::NativeButton::Values, PadButton>, 12>
|
|
|
|
switch_to_gcadapter_button = {
|
|
|
|
std::pair{Settings::NativeButton::A, PadButton::PAD_BUTTON_A},
|
|
|
|
{Settings::NativeButton::B, PadButton::PAD_BUTTON_B},
|
|
|
|
{Settings::NativeButton::X, PadButton::PAD_BUTTON_X},
|
|
|
|
{Settings::NativeButton::Y, PadButton::PAD_BUTTON_Y},
|
|
|
|
{Settings::NativeButton::Plus, PadButton::PAD_BUTTON_START},
|
|
|
|
{Settings::NativeButton::DLeft, PadButton::PAD_BUTTON_LEFT},
|
|
|
|
{Settings::NativeButton::DUp, PadButton::PAD_BUTTON_UP},
|
|
|
|
{Settings::NativeButton::DRight, PadButton::PAD_BUTTON_RIGHT},
|
|
|
|
{Settings::NativeButton::DDown, PadButton::PAD_BUTTON_DOWN},
|
|
|
|
{Settings::NativeButton::SL, PadButton::PAD_TRIGGER_L},
|
|
|
|
{Settings::NativeButton::SR, PadButton::PAD_TRIGGER_R},
|
|
|
|
{Settings::NativeButton::R, PadButton::PAD_TRIGGER_Z},
|
|
|
|
};
|
|
|
|
if (!params.Has("port")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
InputCommon::ButtonMapping mapping{};
|
|
|
|
for (const auto& [switch_button, gcadapter_button] : switch_to_gcadapter_button) {
|
|
|
|
Common::ParamPackage button_params({{"engine", "gcpad"}});
|
|
|
|
button_params.Set("port", params.Get("port", 0));
|
|
|
|
button_params.Set("button", static_cast<int>(gcadapter_button));
|
|
|
|
mapping.insert_or_assign(switch_button, std::move(button_params));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the missing bindings for ZL/ZR
|
|
|
|
static constexpr std::array<std::pair<Settings::NativeButton::Values, PadAxes>, 2>
|
|
|
|
switch_to_gcadapter_axis = {
|
|
|
|
std::pair{Settings::NativeButton::ZL, PadAxes::TriggerLeft},
|
|
|
|
{Settings::NativeButton::ZR, PadAxes::TriggerRight},
|
|
|
|
};
|
|
|
|
for (const auto& [switch_button, gcadapter_axis] : switch_to_gcadapter_axis) {
|
|
|
|
Common::ParamPackage button_params({{"engine", "gcpad"}});
|
|
|
|
button_params.Set("port", params.Get("port", 0));
|
|
|
|
button_params.Set("button", static_cast<int>(PadButton::PAD_STICK));
|
|
|
|
button_params.Set("axis", static_cast<int>(gcadapter_axis));
|
|
|
|
mapping.insert_or_assign(switch_button, std::move(button_params));
|
|
|
|
}
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputCommon::AnalogMapping Adapter::GetAnalogMappingForDevice(
|
|
|
|
const Common::ParamPackage& params) const {
|
|
|
|
if (!params.Has("port")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
InputCommon::AnalogMapping mapping = {};
|
|
|
|
Common::ParamPackage left_analog_params;
|
|
|
|
left_analog_params.Set("engine", "gcpad");
|
|
|
|
left_analog_params.Set("port", params.Get("port", 0));
|
|
|
|
left_analog_params.Set("axis_x", static_cast<int>(PadAxes::StickX));
|
|
|
|
left_analog_params.Set("axis_y", static_cast<int>(PadAxes::StickY));
|
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params));
|
|
|
|
Common::ParamPackage right_analog_params;
|
|
|
|
right_analog_params.Set("engine", "gcpad");
|
|
|
|
right_analog_params.Set("port", params.Get("port", 0));
|
|
|
|
right_analog_params.Set("axis_x", static_cast<int>(PadAxes::SubstickX));
|
|
|
|
right_analog_params.Set("axis_y", static_cast<int>(PadAxes::SubstickY));
|
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
2020-09-07 08:45:04 +02:00
|
|
|
bool Adapter::DeviceConnected(std::size_t port) const {
|
2020-06-22 00:43:01 +02:00
|
|
|
return adapter_controllers_status[port] != ControllerTypes::None;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 21:36:27 +02:00
|
|
|
void Adapter::ResetDeviceType(std::size_t port) {
|
2020-06-22 00:43:01 +02:00
|
|
|
adapter_controllers_status[port] = ControllerTypes::None;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Adapter::BeginConfiguration() {
|
2020-07-07 03:58:31 +02:00
|
|
|
get_origin.fill(true);
|
2020-06-22 03:15:58 +02:00
|
|
|
for (auto& pq : pad_queue) {
|
|
|
|
pq.Clear();
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
configuring = true;
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Adapter::EndConfiguration() {
|
2020-06-22 03:15:58 +02:00
|
|
|
for (auto& pq : pad_queue) {
|
|
|
|
pq.Clear();
|
|
|
|
}
|
2020-06-21 18:36:28 +02:00
|
|
|
configuring = false;
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
|
|
|
|
return pad_queue;
|
|
|
|
}
|
|
|
|
|
2020-06-22 03:50:58 +02:00
|
|
|
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
|
|
|
|
return pad_queue;
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
std::array<GCState, 4>& Adapter::GetPadState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2020-06-22 03:50:58 +02:00
|
|
|
const std::array<GCState, 4>& Adapter::GetPadState() const {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:51:14 +02:00
|
|
|
int Adapter::GetOriginValue(u32 port, u32 axis) const {
|
2020-07-16 19:00:04 +02:00
|
|
|
return origin_status[port].axis_values[axis];
|
2020-07-07 03:58:31 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 00:11:59 +02:00
|
|
|
} // namespace GCAdapter
|