2020-06-21 16:36:28 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
2020-06-21 19:31:57 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
#include <fmt/format.h>
|
2020-07-13 18:48:19 +00:00
|
|
|
#include <libusb.h>
|
2020-09-14 22:34:39 +00:00
|
|
|
|
2020-06-21 16:36:28 +00:00
|
|
|
#include "common/logging/log.h"
|
2020-09-01 23:17:59 +00:00
|
|
|
#include "common/param_package.h"
|
2021-04-14 23:07:40 +00:00
|
|
|
#include "common/settings_input.h"
|
2021-09-20 22:22:07 +00:00
|
|
|
#include "common/thread.h"
|
|
|
|
#include "input_common/drivers/gc_adapter.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
class LibUSBContext {
|
|
|
|
public:
|
|
|
|
explicit LibUSBContext() {
|
|
|
|
init_result = libusb_init(&ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
~LibUSBContext() {
|
|
|
|
libusb_exit(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
LibUSBContext& operator=(const LibUSBContext&) = delete;
|
|
|
|
LibUSBContext(const LibUSBContext&) = delete;
|
|
|
|
|
|
|
|
LibUSBContext& operator=(LibUSBContext&&) noexcept = delete;
|
|
|
|
LibUSBContext(LibUSBContext&&) noexcept = delete;
|
|
|
|
|
|
|
|
[[nodiscard]] int InitResult() const noexcept {
|
|
|
|
return init_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] libusb_context* get() noexcept {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
libusb_context* ctx;
|
|
|
|
int init_result{};
|
|
|
|
};
|
|
|
|
|
|
|
|
class LibUSBDeviceHandle {
|
|
|
|
public:
|
|
|
|
explicit LibUSBDeviceHandle(libusb_context* ctx, uint16_t vid, uint16_t pid) noexcept {
|
|
|
|
handle = libusb_open_device_with_vid_pid(ctx, vid, pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
~LibUSBDeviceHandle() noexcept {
|
|
|
|
if (handle) {
|
|
|
|
libusb_release_interface(handle, 1);
|
|
|
|
libusb_close(handle);
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
LibUSBDeviceHandle& operator=(const LibUSBDeviceHandle&) = delete;
|
|
|
|
LibUSBDeviceHandle(const LibUSBDeviceHandle&) = delete;
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
LibUSBDeviceHandle& operator=(LibUSBDeviceHandle&&) noexcept = delete;
|
|
|
|
LibUSBDeviceHandle(LibUSBDeviceHandle&&) noexcept = delete;
|
|
|
|
|
|
|
|
[[nodiscard]] libusb_device_handle* get() noexcept {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
libusb_device_handle* handle{};
|
|
|
|
};
|
|
|
|
|
2021-12-13 14:05:23 +00:00
|
|
|
GCAdapter::GCAdapter(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
|
2021-09-20 22:22:07 +00:00
|
|
|
if (usb_adapter_handle) {
|
2020-06-21 22:43:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-30 16:20:47 +00:00
|
|
|
LOG_DEBUG(Input, "Initialization started");
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
libusb_ctx = std::make_unique<LibUSBContext>();
|
|
|
|
const int init_res = libusb_ctx->InitResult();
|
2020-07-08 17:19:33 +00:00
|
|
|
if (init_res == LIBUSB_SUCCESS) {
|
2021-09-20 22:22:07 +00:00
|
|
|
adapter_scan_thread =
|
|
|
|
std::jthread([this](std::stop_token stop_token) { AdapterScanThread(stop_token); });
|
2020-07-08 17:19:33 +00:00
|
|
|
} else {
|
|
|
|
LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res);
|
|
|
|
}
|
2020-06-21 22:43:01 +00:00
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
GCAdapter::~GCAdapter() {
|
2020-10-12 23:11:22 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::AdapterInputThread(std::stop_token stop_token) {
|
2021-10-30 16:20:47 +00:00
|
|
|
LOG_DEBUG(Input, "Input thread started");
|
2021-09-20 22:22:07 +00:00
|
|
|
Common::SetCurrentThreadName("yuzu:input:GCAdapter");
|
2020-10-12 23:11:22 +00:00
|
|
|
s32 payload_size{};
|
|
|
|
AdapterPayload adapter_payload{};
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
adapter_scan_thread = {};
|
2020-10-12 23:11:22 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
while (!stop_token.stop_requested()) {
|
|
|
|
libusb_interrupt_transfer(usb_adapter_handle->get(), input_endpoint, adapter_payload.data(),
|
2020-10-12 23:11:22 +00:00
|
|
|
static_cast<s32>(adapter_payload.size()), &payload_size, 16);
|
|
|
|
if (IsPayloadCorrect(adapter_payload, payload_size)) {
|
|
|
|
UpdateControllers(adapter_payload);
|
|
|
|
UpdateVibrations();
|
|
|
|
}
|
|
|
|
std::this_thread::yield();
|
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
if (restart_scan_thread) {
|
2021-09-20 22:22:07 +00:00
|
|
|
adapter_scan_thread =
|
|
|
|
std::jthread([this](std::stop_token token) { AdapterScanThread(token); });
|
2020-10-12 23:11:22 +00:00
|
|
|
restart_scan_thread = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
bool GCAdapter::IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size) {
|
2020-10-12 23:11:22 +00:00
|
|
|
if (payload_size != static_cast<s32>(adapter_payload.size()) ||
|
|
|
|
adapter_payload[0] != LIBUSB_DT_HID) {
|
|
|
|
LOG_DEBUG(Input, "Error reading payload (size: {}, type: {:02x})", payload_size,
|
|
|
|
adapter_payload[0]);
|
|
|
|
if (input_error_counter++ > 20) {
|
2021-10-30 16:20:47 +00:00
|
|
|
LOG_ERROR(Input, "Timeout, Is the adapter connected?");
|
2021-09-20 22:22:07 +00:00
|
|
|
adapter_input_thread.request_stop();
|
2020-10-12 23:11:22 +00:00
|
|
|
restart_scan_thread = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_error_counter = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::UpdateControllers(const AdapterPayload& adapter_payload) {
|
2020-10-12 23:11:22 +00:00
|
|
|
for (std::size_t port = 0; port < pads.size(); ++port) {
|
|
|
|
const std::size_t offset = 1 + (9 * port);
|
|
|
|
const auto type = static_cast<ControllerTypes>(adapter_payload[offset] >> 4);
|
|
|
|
UpdatePadType(port, type);
|
|
|
|
if (DeviceConnected(port)) {
|
|
|
|
const u8 b1 = adapter_payload[offset + 1];
|
|
|
|
const u8 b2 = adapter_payload[offset + 2];
|
|
|
|
UpdateStateButtons(port, b1, b2);
|
|
|
|
UpdateStateAxes(port, adapter_payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::UpdatePadType(std::size_t port, ControllerTypes pad_type) {
|
2020-10-12 23:11:22 +00:00
|
|
|
if (pads[port].type == pad_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Device changed reset device and set new type
|
2021-10-19 05:12:24 +00:00
|
|
|
pads[port].axis_origin = {};
|
|
|
|
pads[port].reset_origin_counter = {};
|
|
|
|
pads[port].enable_vibration = {};
|
|
|
|
pads[port].rumble_amplitude = {};
|
2020-10-12 23:11:22 +00:00
|
|
|
pads[port].type = pad_type;
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::UpdateStateButtons(std::size_t port, [[maybe_unused]] u8 b1,
|
|
|
|
[[maybe_unused]] u8 b2) {
|
2020-10-12 23:11:22 +00:00
|
|
|
if (port >= pads.size()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-07-03 15:52:07 +00:00
|
|
|
static constexpr std::array<PadButton, 8> b1_buttons{
|
2020-10-12 23:11:22 +00:00
|
|
|
PadButton::ButtonA, PadButton::ButtonB, PadButton::ButtonX, PadButton::ButtonY,
|
|
|
|
PadButton::ButtonLeft, PadButton::ButtonRight, PadButton::ButtonDown, PadButton::ButtonUp,
|
2020-07-03 15:52:07 +00:00
|
|
|
};
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-07-03 15:52:07 +00:00
|
|
|
static constexpr std::array<PadButton, 4> b2_buttons{
|
2020-10-12 23:11:22 +00:00
|
|
|
PadButton::ButtonStart,
|
|
|
|
PadButton::TriggerZ,
|
|
|
|
PadButton::TriggerR,
|
|
|
|
PadButton::TriggerL,
|
2020-07-03 15:52:07 +00:00
|
|
|
};
|
2021-09-20 22:22:07 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
|
2021-09-20 22:22:07 +00:00
|
|
|
const bool button_status = (b1 & (1U << i)) != 0;
|
|
|
|
const int button = static_cast<int>(b1_buttons[i]);
|
|
|
|
SetButton(pads[port].identifier, button, button_status);
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
|
2021-09-20 22:22:07 +00:00
|
|
|
const bool button_status = (b2 & (1U << j)) != 0;
|
|
|
|
const int button = static_cast<int>(b2_buttons[j]);
|
|
|
|
SetButton(pads[port].identifier, button, button_status);
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload) {
|
2020-10-12 23:11:22 +00:00
|
|
|
if (port >= pads.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t offset = 1 + (9 * port);
|
2020-07-16 17:00:04 +00:00
|
|
|
static constexpr std::array<PadAxes, 6> axes{
|
|
|
|
PadAxes::StickX, PadAxes::StickY, PadAxes::SubstickX,
|
|
|
|
PadAxes::SubstickY, PadAxes::TriggerLeft, PadAxes::TriggerRight,
|
|
|
|
};
|
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
for (const PadAxes axis : axes) {
|
|
|
|
const auto index = static_cast<std::size_t>(axis);
|
|
|
|
const u8 axis_value = adapter_payload[offset + 3 + index];
|
2021-04-25 16:45:42 +00:00
|
|
|
if (pads[port].reset_origin_counter <= 18) {
|
|
|
|
if (pads[port].axis_origin[index] != axis_value) {
|
|
|
|
pads[port].reset_origin_counter = 0;
|
|
|
|
}
|
2020-10-12 23:11:22 +00:00
|
|
|
pads[port].axis_origin[index] = axis_value;
|
2021-04-25 16:45:42 +00:00
|
|
|
pads[port].reset_origin_counter++;
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
2021-10-21 04:18:04 +00:00
|
|
|
const f32 axis_status = (axis_value - pads[port].axis_origin[index]) / 100.0f;
|
2021-09-20 22:22:07 +00:00
|
|
|
SetAxis(pads[port].identifier, static_cast<int>(index), axis_status);
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::AdapterScanThread(std::stop_token stop_token) {
|
|
|
|
Common::SetCurrentThreadName("yuzu:input:ScanGCAdapter");
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
pads = {};
|
|
|
|
while (!stop_token.stop_requested() && !Setup()) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
bool GCAdapter::Setup() {
|
|
|
|
constexpr u16 nintendo_vid = 0x057e;
|
|
|
|
constexpr u16 gc_adapter_pid = 0x0337;
|
|
|
|
usb_adapter_handle =
|
|
|
|
std::make_unique<LibUSBDeviceHandle>(libusb_ctx->get(), nintendo_vid, gc_adapter_pid);
|
|
|
|
if (!usb_adapter_handle->get()) {
|
|
|
|
return false;
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
|
|
|
if (!CheckDeviceAccess()) {
|
2021-09-20 22:22:07 +00:00
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
libusb_device* const device = libusb_get_device(usb_adapter_handle->get());
|
2020-10-12 23:11:22 +00:00
|
|
|
|
|
|
|
LOG_INFO(Input, "GC adapter is now connected");
|
|
|
|
// GC Adapter found and accessible, registering it
|
|
|
|
if (GetGCEndpoint(device)) {
|
|
|
|
rumble_enabled = true;
|
|
|
|
input_error_counter = 0;
|
|
|
|
output_error_counter = 0;
|
2021-09-20 22:22:07 +00:00
|
|
|
|
|
|
|
std::size_t port = 0;
|
|
|
|
for (GCController& pad : pads) {
|
|
|
|
pad.identifier = {
|
2021-10-31 03:23:10 +00:00
|
|
|
.guid = Common::UUID{Common::INVALID_UUID},
|
2021-09-20 22:22:07 +00:00
|
|
|
.port = port++,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
|
|
|
PreSetController(pad.identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter_input_thread =
|
|
|
|
std::jthread([this](std::stop_token stop_token) { AdapterInputThread(stop_token); });
|
|
|
|
return true;
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
2021-09-20 22:22:07 +00:00
|
|
|
return false;
|
2020-10-12 23:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
bool GCAdapter::CheckDeviceAccess() {
|
|
|
|
s32 kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle->get(), 0);
|
2020-06-23 21:37:15 +00:00
|
|
|
if (kernel_driver_error == 1) {
|
2021-09-20 22:22:07 +00:00
|
|
|
kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle->get(), 0);
|
2020-06-23 21:37:15 +00:00
|
|
|
if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
|
2020-07-03 15:52:07 +00:00
|
|
|
LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}",
|
2020-06-23 21:37:15 +00:00
|
|
|
kernel_driver_error);
|
2020-06-21 19:31:57 +00:00
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:37:15 +00:00
|
|
|
if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
|
2020-06-21 16:36:28 +00:00
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
const int interface_claim_error = libusb_claim_interface(usb_adapter_handle->get(), 0);
|
2020-06-23 21:37:15 +00:00
|
|
|
if (interface_claim_error) {
|
2020-07-03 15:52:07 +00:00
|
|
|
LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error);
|
2020-06-21 16:36:28 +00:00
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-30 16:20:47 +00:00
|
|
|
// This fixes payload problems from offbrand GCAdapters
|
|
|
|
const s32 control_transfer_error =
|
|
|
|
libusb_control_transfer(usb_adapter_handle->get(), 0x21, 11, 0x0001, 0, nullptr, 0, 1000);
|
|
|
|
if (control_transfer_error < 0) {
|
|
|
|
LOG_ERROR(Input, "libusb_control_transfer failed with error= {}", control_transfer_error);
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:36:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
bool GCAdapter::GetGCEndpoint(libusb_device* device) {
|
2020-06-21 16:36:28 +00:00
|
|
|
libusb_config_descriptor* config = nullptr;
|
2020-07-08 18:18:54 +00:00
|
|
|
const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config);
|
|
|
|
if (config_descriptor_return != LIBUSB_SUCCESS) {
|
2020-07-08 17:19:33 +00:00
|
|
|
LOG_ERROR(Input, "libusb_get_config_descriptor failed with error = {}",
|
2020-07-08 18:18:54 +00:00
|
|
|
config_descriptor_return);
|
2020-10-12 23:11:22 +00:00
|
|
|
return false;
|
2020-07-08 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:36:28 +00: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 06:51:14 +00:00
|
|
|
if ((endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) != 0) {
|
2020-06-21 16:36:28 +00:00
|
|
|
input_endpoint = endpoint->bEndpointAddress;
|
2020-07-01 16:52:50 +00:00
|
|
|
} else {
|
|
|
|
output_endpoint = endpoint->bEndpointAddress;
|
2020-06-21 19:31:57 +00:00
|
|
|
}
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 16:52:50 +00: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;
|
2021-09-20 22:22:07 +00:00
|
|
|
libusb_interrupt_transfer(usb_adapter_handle->get(), output_endpoint, &clear_payload,
|
2020-07-01 16:52:50 +00:00
|
|
|
sizeof(clear_payload), nullptr, 16);
|
2020-10-12 23:11:22 +00:00
|
|
|
return true;
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::VibrationError GCAdapter::SetRumble(const PadIdentifier& identifier,
|
|
|
|
const Common::Input::VibrationStatus vibration) {
|
2021-09-20 22:22:07 +00:00
|
|
|
const auto mean_amplitude = (vibration.low_amplitude + vibration.high_amplitude) * 0.5f;
|
|
|
|
const auto processed_amplitude =
|
|
|
|
static_cast<u8>((mean_amplitude + std::pow(mean_amplitude, 0.3f)) * 0.5f * 0x8);
|
2021-10-23 21:32:16 +00:00
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
pads[identifier.port].rumble_amplitude = processed_amplitude;
|
2021-10-11 05:43:11 +00:00
|
|
|
|
|
|
|
if (!rumble_enabled) {
|
2021-10-31 03:23:10 +00:00
|
|
|
return Common::Input::VibrationError::Disabled;
|
2021-10-11 05:43:11 +00:00
|
|
|
}
|
2021-10-31 03:23:10 +00:00
|
|
|
return Common::Input::VibrationError::None;
|
2021-10-23 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::UpdateVibrations() {
|
|
|
|
// Use 8 states to keep the switching between on/off fast enough for
|
|
|
|
// a human to feel different vibration strenght
|
|
|
|
// More states == more rumble strengths == slower update time
|
|
|
|
constexpr u8 vibration_states = 8;
|
|
|
|
|
|
|
|
vibration_counter = (vibration_counter + 1) % vibration_states;
|
|
|
|
|
|
|
|
for (GCController& pad : pads) {
|
|
|
|
const bool vibrate = pad.rumble_amplitude > vibration_counter;
|
|
|
|
vibration_changed |= vibrate != pad.enable_vibration;
|
|
|
|
pad.enable_vibration = vibrate;
|
2021-10-23 21:32:16 +00:00
|
|
|
}
|
2021-09-20 22:22:07 +00:00
|
|
|
SendVibrations();
|
2021-10-23 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::SendVibrations() {
|
|
|
|
if (!rumble_enabled || !vibration_changed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s32 size{};
|
|
|
|
constexpr u8 rumble_command = 0x11;
|
|
|
|
const u8 p1 = pads[0].enable_vibration;
|
|
|
|
const u8 p2 = pads[1].enable_vibration;
|
|
|
|
const u8 p3 = pads[2].enable_vibration;
|
|
|
|
const u8 p4 = pads[3].enable_vibration;
|
|
|
|
std::array<u8, 5> payload = {rumble_command, p1, p2, p3, p4};
|
|
|
|
const int err =
|
|
|
|
libusb_interrupt_transfer(usb_adapter_handle->get(), output_endpoint, payload.data(),
|
|
|
|
static_cast<s32>(payload.size()), &size, 16);
|
|
|
|
if (err) {
|
2021-10-30 16:20:47 +00:00
|
|
|
LOG_DEBUG(Input, "Libusb write failed: {}", libusb_error_name(err));
|
2021-09-20 22:22:07 +00:00
|
|
|
if (output_error_counter++ > 5) {
|
2021-10-30 16:20:47 +00:00
|
|
|
LOG_ERROR(Input, "Output timeout, Rumble disabled");
|
2021-09-20 22:22:07 +00:00
|
|
|
rumble_enabled = false;
|
|
|
|
}
|
|
|
|
return;
|
2021-10-23 21:32:16 +00:00
|
|
|
}
|
2021-09-20 22:22:07 +00:00
|
|
|
output_error_counter = 0;
|
|
|
|
vibration_changed = false;
|
2021-10-23 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
bool GCAdapter::DeviceConnected(std::size_t port) const {
|
|
|
|
return pads[port].type != ControllerTypes::None;
|
2021-10-23 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
void GCAdapter::Reset() {
|
|
|
|
adapter_scan_thread = {};
|
|
|
|
adapter_input_thread = {};
|
|
|
|
usb_adapter_handle = nullptr;
|
|
|
|
pads = {};
|
|
|
|
libusb_ctx = nullptr;
|
2020-06-21 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
std::vector<Common::ParamPackage> GCAdapter::GetInputDevices() const {
|
2020-09-01 23:17:59 +00:00
|
|
|
std::vector<Common::ParamPackage> devices;
|
2020-10-12 23:11:22 +00:00
|
|
|
for (std::size_t port = 0; port < pads.size(); ++port) {
|
2020-09-01 23:17:59 +00:00
|
|
|
if (!DeviceConnected(port)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-19 05:12:24 +00:00
|
|
|
Common::ParamPackage identifier{};
|
|
|
|
identifier.Set("engine", GetEngineName());
|
|
|
|
identifier.Set("display", fmt::format("Gamecube Controller {}", port + 1));
|
|
|
|
identifier.Set("port", static_cast<int>(port));
|
|
|
|
devices.emplace_back(identifier);
|
2020-09-01 23:17:59 +00:00
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
ButtonMapping GCAdapter::GetButtonMappingForDevice(const Common::ParamPackage& params) {
|
2020-09-01 23:17:59 +00:00
|
|
|
// 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 = {
|
2020-10-12 23:11:22 +00:00
|
|
|
std::pair{Settings::NativeButton::A, PadButton::ButtonA},
|
|
|
|
{Settings::NativeButton::B, PadButton::ButtonB},
|
|
|
|
{Settings::NativeButton::X, PadButton::ButtonX},
|
|
|
|
{Settings::NativeButton::Y, PadButton::ButtonY},
|
|
|
|
{Settings::NativeButton::Plus, PadButton::ButtonStart},
|
|
|
|
{Settings::NativeButton::DLeft, PadButton::ButtonLeft},
|
|
|
|
{Settings::NativeButton::DUp, PadButton::ButtonUp},
|
|
|
|
{Settings::NativeButton::DRight, PadButton::ButtonRight},
|
|
|
|
{Settings::NativeButton::DDown, PadButton::ButtonDown},
|
|
|
|
{Settings::NativeButton::SL, PadButton::TriggerL},
|
|
|
|
{Settings::NativeButton::SR, PadButton::TriggerR},
|
|
|
|
{Settings::NativeButton::R, PadButton::TriggerZ},
|
2020-09-01 23:17:59 +00:00
|
|
|
};
|
|
|
|
if (!params.Has("port")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
ButtonMapping mapping{};
|
2020-09-01 23:17:59 +00:00
|
|
|
for (const auto& [switch_button, gcadapter_button] : switch_to_gcadapter_button) {
|
2021-10-19 05:12:24 +00:00
|
|
|
Common::ParamPackage button_params{};
|
|
|
|
button_params.Set("engine", GetEngineName());
|
2020-09-01 23:17:59 +00:00
|
|
|
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
|
2021-09-20 22:22:07 +00:00
|
|
|
static constexpr std::array<std::tuple<Settings::NativeButton::Values, PadButton, PadAxes>, 2>
|
2020-09-01 23:17:59 +00:00
|
|
|
switch_to_gcadapter_axis = {
|
2021-09-20 22:22:07 +00:00
|
|
|
std::tuple{Settings::NativeButton::ZL, PadButton::TriggerL, PadAxes::TriggerLeft},
|
|
|
|
{Settings::NativeButton::ZR, PadButton::TriggerR, PadAxes::TriggerRight},
|
2020-09-01 23:17:59 +00:00
|
|
|
};
|
2021-09-20 22:22:07 +00:00
|
|
|
for (const auto& [switch_button, gcadapter_buton, gcadapter_axis] : switch_to_gcadapter_axis) {
|
2021-10-19 05:12:24 +00:00
|
|
|
Common::ParamPackage button_params{};
|
|
|
|
button_params.Set("engine", GetEngineName());
|
2020-09-01 23:17:59 +00:00
|
|
|
button_params.Set("port", params.Get("port", 0));
|
2021-09-20 22:22:07 +00:00
|
|
|
button_params.Set("button", static_cast<s32>(gcadapter_buton));
|
2020-10-12 23:11:22 +00:00
|
|
|
button_params.Set("axis", static_cast<s32>(gcadapter_axis));
|
|
|
|
button_params.Set("threshold", 0.5f);
|
2021-09-20 22:22:07 +00:00
|
|
|
button_params.Set("range", 1.9f);
|
2020-10-12 23:11:22 +00:00
|
|
|
button_params.Set("direction", "+");
|
2020-09-01 23:17:59 +00:00
|
|
|
mapping.insert_or_assign(switch_button, std::move(button_params));
|
|
|
|
}
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
AnalogMapping GCAdapter::GetAnalogMappingForDevice(const Common::ParamPackage& params) {
|
2020-09-01 23:17:59 +00:00
|
|
|
if (!params.Has("port")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
AnalogMapping mapping = {};
|
2020-09-01 23:17:59 +00:00
|
|
|
Common::ParamPackage left_analog_params;
|
2021-10-19 05:12:24 +00:00
|
|
|
left_analog_params.Set("engine", GetEngineName());
|
2020-09-01 23:17:59 +00:00
|
|
|
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;
|
2021-10-19 05:12:24 +00:00
|
|
|
right_analog_params.Set("engine", GetEngineName());
|
2020-09-01 23:17:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-21 20:12:01 +00:00
|
|
|
Common::Input::ButtonNames GCAdapter::GetUIButtonName(const Common::ParamPackage& params) const {
|
2021-10-19 05:12:24 +00:00
|
|
|
PadButton button = static_cast<PadButton>(params.Get("button", 0));
|
|
|
|
switch (button) {
|
|
|
|
case PadButton::ButtonLeft:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonLeft;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonRight:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonRight;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonDown:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonDown;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonUp:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonUp;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::TriggerZ:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::TriggerZ;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::TriggerR:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::TriggerR;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::TriggerL:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::TriggerL;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonA:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonA;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonB:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonB;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonX:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonX;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonY:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonY;
|
2021-10-19 05:12:24 +00:00
|
|
|
case PadButton::ButtonStart:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::ButtonStart;
|
2021-10-19 05:12:24 +00:00
|
|
|
default:
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::Undefined;
|
2021-10-19 05:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 20:12:01 +00:00
|
|
|
Common::Input::ButtonNames GCAdapter::GetUIName(const Common::ParamPackage& params) const {
|
2021-09-20 22:22:07 +00:00
|
|
|
if (params.Has("button")) {
|
2021-11-21 20:12:01 +00:00
|
|
|
return GetUIButtonName(params);
|
2021-10-19 05:12:24 +00:00
|
|
|
}
|
|
|
|
if (params.Has("axis")) {
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::Value;
|
2021-09-20 22:22:07 +00:00
|
|
|
}
|
2020-06-22 01:50:58 +00:00
|
|
|
|
2021-11-21 20:12:01 +00:00
|
|
|
return Common::Input::ButtonNames::Invalid;
|
2020-07-07 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 22:22:07 +00:00
|
|
|
} // namespace InputCommon
|