From 0248614add99c1df1bc7c9ff97091f678ff75aca Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 12:36:28 -0400 Subject: GC Adapter Implementation --- src/input_common/gcadapter/gc_adapter.cpp | 350 ++++++++++++++++++++++++++++++ src/input_common/gcadapter/gc_adapter.h | 116 ++++++++++ src/input_common/gcadapter/gc_poller.cpp | 310 ++++++++++++++++++++++++++ src/input_common/gcadapter/gc_poller.h | 59 +++++ 4 files changed, 835 insertions(+) create mode 100644 src/input_common/gcadapter/gc_adapter.cpp create mode 100644 src/input_common/gcadapter/gc_adapter.h create mode 100644 src/input_common/gcadapter/gc_poller.cpp create mode 100644 src/input_common/gcadapter/gc_poller.h (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp new file mode 100644 index 000000000..d42261d61 --- /dev/null +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -0,0 +1,350 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. +//* +#include "common/logging/log.h" +#include "common/threadsafe_queue.h" +#include "input_common/gcadapter/gc_adapter.h" + +Common::SPSCQueue pad_queue[4]; +struct GCState state[4]; + +namespace GCAdapter { + +static libusb_device_handle* usb_adapter_handle = nullptr; +static u8 adapter_controllers_status[4] = { + ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, + ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; + +static std::mutex s_mutex; + +static std::thread adapter_input_thread; +static bool adapter_thread_running; + +static std::mutex initialization_mutex; +static std::thread detect_thread; +static bool detect_thread_running = false; + +static libusb_context* libusb_ctx; + +static u8 input_endpoint = 0; + +static bool configuring = false; + +GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { + GCPadStatus pad = {}; + bool get_origin = false; + + u8 type = adapter_payload[1 + (9 * port)] >> 4; + if (type) + get_origin = true; + + adapter_controllers_status[port] = type; + + if (adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE) { + u8 b1 = adapter_payload[1 + (9 * port) + 1]; + u8 b2 = adapter_payload[1 + (9 * port) + 2]; + + if (b1 & (1 << 0)) + pad.button |= PAD_BUTTON_A; + if (b1 & (1 << 1)) + pad.button |= PAD_BUTTON_B; + if (b1 & (1 << 2)) + pad.button |= PAD_BUTTON_X; + if (b1 & (1 << 3)) + pad.button |= PAD_BUTTON_Y; + + if (b1 & (1 << 4)) + pad.button |= PAD_BUTTON_LEFT; + if (b1 & (1 << 5)) + pad.button |= PAD_BUTTON_RIGHT; + if (b1 & (1 << 6)) + pad.button |= PAD_BUTTON_DOWN; + if (b1 & (1 << 7)) + pad.button |= PAD_BUTTON_UP; + + if (b2 & (1 << 0)) + pad.button |= PAD_BUTTON_START; + if (b2 & (1 << 1)) + pad.button |= PAD_TRIGGER_Z; + if (b2 & (1 << 2)) + pad.button |= PAD_TRIGGER_R; + if (b2 & (1 << 3)) + pad.button |= PAD_TRIGGER_L; + + if (get_origin) + pad.button |= PAD_GET_ORIGIN; + + pad.stickX = adapter_payload[1 + (9 * port) + 3]; + pad.stickY = adapter_payload[1 + (9 * port) + 4]; + pad.substickX = adapter_payload[1 + (9 * port) + 5]; + pad.substickY = adapter_payload[1 + (9 * port) + 6]; + pad.triggerLeft = adapter_payload[1 + (9 * port) + 7]; + pad.triggerRight = adapter_payload[1 + (9 * port) + 8]; + } + return pad; +} + +void PadToState(GCPadStatus pad, GCState& state) { + //std::lock_guard lock{s_mutex}; + state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); + state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B); + state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); + state.buttons.insert_or_assign(PAD_BUTTON_Y, pad.button & PAD_BUTTON_Y); + state.buttons.insert_or_assign(PAD_BUTTON_LEFT, pad.button & PAD_BUTTON_LEFT); + state.buttons.insert_or_assign(PAD_BUTTON_RIGHT, pad.button & PAD_BUTTON_RIGHT); + state.buttons.insert_or_assign(PAD_BUTTON_DOWN, pad.button & PAD_BUTTON_DOWN); + state.buttons.insert_or_assign(PAD_BUTTON_UP, pad.button & PAD_BUTTON_UP); + state.buttons.insert_or_assign(PAD_BUTTON_START, pad.button & PAD_BUTTON_START); + state.buttons.insert_or_assign(PAD_TRIGGER_Z, pad.button & PAD_TRIGGER_Z); + state.buttons.insert_or_assign(PAD_TRIGGER_L, pad.button & PAD_TRIGGER_L); + state.buttons.insert_or_assign(PAD_TRIGGER_R, pad.button & PAD_TRIGGER_R); + state.axes.insert_or_assign(STICK_X, pad.stickX); + state.axes.insert_or_assign(STICK_Y, pad.stickY); + state.axes.insert_or_assign(SUBSTICK_X, pad.substickX); + state.axes.insert_or_assign(SUBSTICK_Y, pad.substickY); + state.axes.insert_or_assign(TRIGGER_LEFT, pad.triggerLeft); + state.axes.insert_or_assign(TRIGGER_RIGHT, pad.triggerRight); +} + +static void Read() { + LOG_INFO(Input, "GC Adapter Read() thread started"); + + int payload_size_in; + u8 adapter_payload[37]; + while (adapter_thread_running) { + libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload, + sizeof(adapter_payload), &payload_size_in, 32); + + int payload_size = 0; + u8 controller_payload_copy[37]; + + { + std::lock_guard lk(s_mutex); + std::copy(std::begin(adapter_payload), std::end(adapter_payload), + std::begin(controller_payload_copy)); + payload_size = payload_size_in; + } + + GCPadStatus pad[4]; + if (payload_size != sizeof(controller_payload_copy) || + controller_payload_copy[0] != LIBUSB_DT_HID) { + LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, + controller_payload_copy[0]); + } else { + for (int i = 0; i < 4; i++) + pad[i] = CheckStatus(i, controller_payload_copy); + } + for (int port = 0; port < 4; port++) { + if (DeviceConnected(port) && configuring) { + if (pad[port].button != PAD_GET_ORIGIN) + pad_queue[port].Push(pad[port]); + + // Accounting for a threshold here because of some controller variance + if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || + pad[port].stickX < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { + pad[port].axis_which = STICK_X; + pad[port].axis_value = pad[port].stickX; + pad_queue[port].Push(pad[port]); + } + if (pad[port].stickY > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || + pad[port].stickY < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { + pad[port].axis_which = STICK_Y; + pad[port].axis_value = pad[port].stickY; + pad_queue[port].Push(pad[port]); + } + if (pad[port].substickX > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || + pad[port].substickX < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { + pad[port].axis_which = SUBSTICK_X; + pad[port].axis_value = pad[port].substickX; + pad_queue[port].Push(pad[port]); + } + if (pad[port].substickY > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || + pad[port].substickY < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { + pad[port].axis_which = SUBSTICK_Y; + pad[port].axis_value = pad[port].substickY; + pad_queue[port].Push(pad[port]); + } + } + PadToState(pad[port], state[port]); + } + std::this_thread::yield(); + } +} + +static void ScanThreadFunc() { + LOG_INFO(Input, "GC Adapter scanning thread started"); + + while (detect_thread_running) { + if (usb_adapter_handle == nullptr) { + std::lock_guard lk(initialization_mutex); + Setup(); + } + Sleep(500); + } +} + +void Init() { + + if (usb_adapter_handle != nullptr) + return; + LOG_INFO(Input, "GC Adapter Initialization started"); + + current_status = NO_ADAPTER_DETECTED; + libusb_init(&libusb_ctx); + + StartScanThread(); +} + +void StartScanThread() { + if (detect_thread_running) + return; + if (!libusb_ctx) + return; + + detect_thread_running = true; + detect_thread = std::thread(ScanThreadFunc); +} + +void StopScanThread() { + detect_thread.join(); +} + +static void Setup() { + // Reset the error status in case the adapter gets unplugged + if (current_status < 0) + current_status = NO_ADAPTER_DETECTED; + + for (int i = 0; i < 4; i++) + adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; + + libusb_device** devs; // pointer to list of connected usb devices + + int cnt = libusb_get_device_list(libusb_ctx, &devs); //get the list of devices + + for (int i = 0; i < cnt; i++) { + if (CheckDeviceAccess(devs[i])) { + // GC Adapter found, registering it + GetGCEndpoint(devs[i]); + break; + } + } +} + +static bool CheckDeviceAccess(libusb_device* device) { + libusb_device_descriptor desc; + int ret = libusb_get_device_descriptor(device, &desc); + if (ret) { + // could not acquire the descriptor, no point in trying to use it. + LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", ret); + return false; + } + + if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) { + // This isn’t the device we are looking for. + return false; + } + ret = libusb_open(device, &usb_adapter_handle); + + if (ret == LIBUSB_ERROR_ACCESS) { + LOG_ERROR(Input, + "Yuzu can not gain access to this device: ID %04X:%04X.", + desc.idVendor, desc.idProduct); + return false; + } + if (ret) { + LOG_ERROR(Input, "libusb_open failed to open device with error = %d", ret); + return false; + } + + ret = libusb_kernel_driver_active(usb_adapter_handle, 0); + if (ret == 1) { + ret = libusb_detach_kernel_driver(usb_adapter_handle, 0); + if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) + LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret); + } + + if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { + libusb_close(usb_adapter_handle); + usb_adapter_handle = nullptr; + return false; + } + + ret = libusb_claim_interface(usb_adapter_handle, 0); + if (ret) { + LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", ret); + libusb_close(usb_adapter_handle); + usb_adapter_handle = nullptr; + return false; + } + + return true; +} + +static void GetGCEndpoint(libusb_device* device) { + 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]; + if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) + input_endpoint = endpoint->bEndpointAddress; + } + } + } + + adapter_thread_running = true; + current_status = ADAPTER_DETECTED; + + adapter_input_thread = std::thread(Read); // Read input +} + +void Shutdown() { + StopScanThread(); + Reset(); + + current_status = NO_ADAPTER_DETECTED; +} + +static void Reset() { + std::unique_lock lock(initialization_mutex, std::defer_lock); + if (!lock.try_lock()) + return; + if (current_status != ADAPTER_DETECTED) + return; + + if (adapter_thread_running) + adapter_input_thread.join(); + + for (int i = 0; i < 4; i++) + adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; + + current_status = NO_ADAPTER_DETECTED; + + if (usb_adapter_handle) { + libusb_release_interface(usb_adapter_handle, 0); + libusb_close(usb_adapter_handle); + usb_adapter_handle = nullptr; + } +} + +bool DeviceConnected(int port) { + return adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE; +} + +void ResetDeviceType(int port) { + adapter_controllers_status[port] = ControllerTypes::CONTROLLER_NONE; +} + +void BeginConfiguration() { + configuring = true; +} + +void EndConfiguration() { + configuring = false; +} + +} // end of namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h new file mode 100644 index 000000000..9b02d1382 --- /dev/null +++ b/src/input_common/gcadapter/gc_adapter.h @@ -0,0 +1,116 @@ +#pragma once +#include +#include +#include +#include +#include "common/common_types.h" + + +enum { + PAD_USE_ORIGIN = 0x0080, + PAD_GET_ORIGIN = 0x2000, + PAD_ERR_STATUS = 0x8000, +}; + +enum PadButton { + PAD_BUTTON_LEFT = 0x0001, + PAD_BUTTON_RIGHT = 0x0002, + PAD_BUTTON_DOWN = 0x0004, + PAD_BUTTON_UP = 0x0008, + PAD_TRIGGER_Z = 0x0010, + PAD_TRIGGER_R = 0x0020, + PAD_TRIGGER_L = 0x0040, + PAD_BUTTON_A = 0x0100, + PAD_BUTTON_B = 0x0200, + PAD_BUTTON_X = 0x0400, + PAD_BUTTON_Y = 0x0800, + PAD_BUTTON_START = 0x1000, + // Below is for compatibility with "AxisButton" type + PAD_STICK = 0x2000, + +}; + +enum PadAxes { STICK_X, STICK_Y, SUBSTICK_X, SUBSTICK_Y, TRIGGER_LEFT, TRIGGER_RIGHT }; + +struct GCPadStatus { + u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits + u8 stickX; // 0 <= stickX <= 255 + u8 stickY; // 0 <= stickY <= 255 + u8 substickX; // 0 <= substickX <= 255 + u8 substickY; // 0 <= substickY <= 255 + u8 triggerLeft; // 0 <= triggerLeft <= 255 + u8 triggerRight; // 0 <= triggerRight <= 255 + bool isConnected{true}; + + static const u8 MAIN_STICK_CENTER_X = 0x80; + static const u8 MAIN_STICK_CENTER_Y = 0x80; + static const u8 MAIN_STICK_RADIUS = 0x7f; + static const u8 C_STICK_CENTER_X = 0x80; + static const u8 C_STICK_CENTER_Y = 0x80; + static const u8 C_STICK_RADIUS = 0x7f; + + static const u8 TRIGGER_CENTER = 20; + static const u8 THRESHOLD = 10; + u8 port; + u8 axis_which = 255; + u8 axis_value = 255; +}; + +struct GCState { + std::unordered_map buttons; + std::unordered_map axes; +}; + + +namespace GCAdapter { +enum ControllerTypes { + CONTROLLER_NONE = 0, + CONTROLLER_WIRED = 1, + CONTROLLER_WIRELESS = 2 +}; + +enum { + NO_ADAPTER_DETECTED = 0, + ADAPTER_DETECTED = 1, +}; + +// Current adapter status: detected/not detected/in error (holds the error code) +static int current_status = NO_ADAPTER_DETECTED; + +GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); +/// Initialize the GC Adapter capture and read sequence +void Init(); + +/// Close the adapter read thread and release the adapter +void Shutdown(); + +/// Begin scanning for the GC Adapter. +void StartScanThread(); + +/// Stop scanning for the adapter +void StopScanThread(); + +/// Returns true if there is a device connected to port +bool DeviceConnected(int port); + +/// Resets status of device connected to port +void ResetDeviceType(int port); + +/// Returns true if we successfully gain access to GC Adapter +bool CheckDeviceAccess(libusb_device* device); + +/// Captures GC Adapter endpoint address, +void GetGCEndpoint(libusb_device* device); + +/// For shutting down, clear all data, join all threads, release usb +void Reset(); + +/// For use in initialization, querying devices to find the adapter +void Setup(); + +/// Used for polling +void BeginConfiguration(); + +void EndConfiguration(); + +} // end of namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp new file mode 100644 index 000000000..772bd8890 --- /dev/null +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -0,0 +1,310 @@ +#include +#include +#include +#include +#include "input_common/gcadapter/gc_poller.h" +#include "input_common/gcadapter/gc_adapter.h" +#include "common/threadsafe_queue.h" + +// Using extern as to avoid multply defined symbols. +extern Common::SPSCQueue pad_queue[4]; +extern struct GCState state[4]; + +namespace InputCommon { + +class GCButton final : public Input::ButtonDevice { +public: + explicit GCButton(int port_, int button_, int axis_) + : port(port_), button(button_) { + } + + ~GCButton() override; + + bool GetStatus() const override { + return state[port].buttons.at(button); + } + +private: + const int port; + const int button; +}; + +class GCAxisButton final : public Input::ButtonDevice { +public: + explicit GCAxisButton(int port_, int axis_, float threshold_, + bool trigger_if_greater_) + : port(port_), axis(axis_), threshold(threshold_), + trigger_if_greater(trigger_if_greater_) { + } + + + bool GetStatus() const override { + const float axis_value = (state[port].axes.at(axis) - 128.0f) / 128.0f; + if (trigger_if_greater) { + return axis_value > 0.10f; //TODO(ameerj) : Fix threshold. + } + return axis_value < -0.10f; + } + +private: + const int port; + const int axis; + float threshold; + bool trigger_if_greater; +}; + +GCButtonFactory::GCButtonFactory() { + GCAdapter::Init(); +} + +GCButton::~GCButton() { + GCAdapter::Shutdown(); +} + +std::unique_ptr GCButtonFactory::Create(const Common::ParamPackage& params) { + int button_id = params.Get("button", 0); + int port = params.Get("port", 0); + // For Axis buttons, used by the binary sticks. + if (params.Has("axis")) { + const int axis = params.Get("axis", 0); + const float threshold = params.Get("threshold", 0.5f); + const std::string direction_name = params.Get("direction", ""); + bool trigger_if_greater; + if (direction_name == "+") { + trigger_if_greater = true; + } else if (direction_name == "-") { + trigger_if_greater = false; + } else { + trigger_if_greater = true; + LOG_ERROR(Input, "Unknown direction {}", direction_name); + } + return std::make_unique(port, axis, threshold, trigger_if_greater); + } + + std::unique_ptr button = + std::make_unique(port, button_id, params.Get("axis", 0)); + return std::move(button); +} + +Common::ParamPackage GCButtonFactory::GetNextInput() { + Common::ParamPackage params; + GCPadStatus pad; + for (int i = 0; i < 4; i++) { + while (pad_queue[i].Pop(pad)) { + // This while loop will break on the earliest detected button + params.Set("engine", "gcpad"); + params.Set("port", i); + // I was debating whether to keep these verbose for ease of reading + // or to use a while loop shifting the bits to test and set the value. + if (pad.button & PAD_BUTTON_A) { + params.Set("button", PAD_BUTTON_A); + break; + } + if (pad.button & PAD_BUTTON_B) { + params.Set("button", PAD_BUTTON_B); + break; + } + if (pad.button & PAD_BUTTON_X) { + params.Set("button", PAD_BUTTON_X); + break; + } + if (pad.button & PAD_BUTTON_Y) { + params.Set("button", PAD_BUTTON_Y); + break; + } + if (pad.button & PAD_BUTTON_DOWN) { + params.Set("button", PAD_BUTTON_DOWN); + break; + } + if (pad.button & PAD_BUTTON_LEFT) { + params.Set("button", PAD_BUTTON_LEFT); + break; + } + if (pad.button & PAD_BUTTON_RIGHT) { + params.Set("button", PAD_BUTTON_RIGHT); + break; + } + if (pad.button & PAD_BUTTON_UP) { + params.Set("button", PAD_BUTTON_UP); + break; + } + if (pad.button & PAD_TRIGGER_L) { + params.Set("button", PAD_TRIGGER_L); + break; + } + if (pad.button & PAD_TRIGGER_R) { + params.Set("button", PAD_TRIGGER_R); + break; + } + if (pad.button & PAD_TRIGGER_Z) { + params.Set("button", PAD_TRIGGER_Z); + break; + } + if (pad.button & PAD_BUTTON_START) { + params.Set("button", PAD_BUTTON_START); + break; + } + // For Axis button implementation + if (pad.axis_which != 255) { + params.Set("axis", pad.axis_which); + params.Set("button", PAD_STICK); + if (pad.axis_value > 128) { + params.Set("direction", "+"); + params.Set("threshold", "0.5"); + } else { + params.Set("direction", "-"); + params.Set("threshold", "-0.5"); + } + break; + } + } + } + return params; +} + +void GCButtonFactory::BeginConfiguration() { + polling = true; + for (int i = 0; i < 4; i++) + pad_queue[i].Clear(); + GCAdapter::BeginConfiguration(); +} + +void GCButtonFactory::EndConfiguration() { + polling = false; + + for (int i = 0; i < 4; i++) + pad_queue[i].Clear(); + GCAdapter::EndConfiguration(); +} + +class GCAnalog final : public Input::AnalogDevice { +public: + GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_) + : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) { + } + + float GetAxis(int axis) const { + std::lock_guard lock{mutex}; + // division is not by a perfect 128 to account for some variance in center location + // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range [20-230] + return (state[port].axes.at(axis) - 128.0f) / 95.0f; + } + + std::tuple GetAnalog(int axis_x, int axis_y) const { + float x = GetAxis(axis_x); + float y = GetAxis(axis_y); + + // Make sure the coordinates are in the unit circle, + // otherwise normalize it. + float r = x * x + y * y; + if (r > 1.0f) { + r = std::sqrt(r); + x /= r; + y /= r; + } + + return std::make_tuple(x, y); + } + + std::tuple GetStatus() const override { + const auto [x, y] = GetAnalog(axis_x, axis_y); + const float r = std::sqrt((x * x) + (y * y)); + if (r > deadzone) { + return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), + y / r * (r - deadzone) / (1 - deadzone)); + } + return std::make_tuple(0.0f, 0.0f); + } + + bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { + const auto [x, y] = GetStatus(); + const float directional_deadzone = 0.4f; + switch (direction) { + case Input::AnalogDirection::RIGHT: + return x > directional_deadzone; + case Input::AnalogDirection::LEFT: + return x < -directional_deadzone; + case Input::AnalogDirection::UP: + return y > directional_deadzone; + case Input::AnalogDirection::DOWN: + return y < -directional_deadzone; + } + return false; + } + +private: + const int port; + const int axis_x; + const int axis_y; + const float deadzone; + mutable std::mutex mutex; +}; + + +/// An analog device factory that creates analog devices from GC Adapter +GCAnalogFactory::GCAnalogFactory() {}; + + +/** +* Creates analog device from joystick axes +* @param params contains parameters for creating the device: +* - "port": the nth gcpad on the adapter +* - "axis_x": the index of the axis to be bind as x-axis +* - "axis_y": the index of the axis to be bind as y-axis +*/ +std::unique_ptr GCAnalogFactory::Create(const Common::ParamPackage& params) { + const std::string guid = params.Get("guid", "0"); + const int port = params.Get("port", 0); + const int axis_x = params.Get("axis_x", 0); + const int axis_y = params.Get("axis_y", 1); + const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); + + return std::make_unique(port, axis_x, axis_y, deadzone); +} + +void GCAnalogFactory::BeginConfiguration() { + polling = true; + for (int i = 0; i < 4; i++) + pad_queue[i].Clear(); + GCAdapter::BeginConfiguration(); +} + +void GCAnalogFactory::EndConfiguration() { + polling = false; + for (int i = 0; i < 4; i++) + pad_queue[i].Clear(); + GCAdapter::EndConfiguration(); +} + +Common::ParamPackage GCAnalogFactory::GetNextInput() { + GCPadStatus pad; + for (int i = 0; i < 4; i++) { + while (pad_queue[i].Pop(pad)) { + if (pad.axis_which == 255 || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { + continue; + } + // An analog device needs two axes, so we need to store the axis for later and wait for + // a second SDL event. The axes also must be from the same joystick. + const int axis = pad.axis_which; + if (analog_x_axis == -1) { + analog_x_axis = axis; + controller_number = i; + } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == i) { + analog_y_axis = axis; + } + } + } + Common::ParamPackage params; + if (analog_x_axis != -1 && analog_y_axis != -1) { + params.Set("engine", "gcpad"); + params.Set("port", controller_number); + params.Set("axis_x", analog_x_axis); + params.Set("axis_y", analog_y_axis); + analog_x_axis = -1; + analog_y_axis = -1; + controller_number = -1; + return params; + } + return params; +} +} // namespace InputCommon diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h new file mode 100644 index 000000000..d115b1d2a --- /dev/null +++ b/src/input_common/gcadapter/gc_poller.h @@ -0,0 +1,59 @@ +#pragma once + +#include +#include "core/frontend/input.h" + +namespace InputCommon { + + +/** + * A button device factory representing a gcpad. It receives gcpad events and forward them + * to all button devices it created. + */ +class GCButtonFactory final : public Input::Factory { +public: + GCButtonFactory(); + + /** + * Creates a button device from a button press + * @param params contains parameters for creating the device: + * - "code": the code of the key to bind with the button + */ + std::unique_ptr Create(const Common::ParamPackage& params) override; + + Common::ParamPackage GetNextInput(); + + /// For device input configuration/polling + void BeginConfiguration(); + void EndConfiguration(); + + bool IsPolling() { + return polling; + } + +private: + bool polling = false; +}; + +/// An analog device factory that creates analog devices from GC Adapter +class GCAnalogFactory final : public Input::Factory { +public: + GCAnalogFactory(); + std::unique_ptr Create(const Common::ParamPackage& params) override; + Common::ParamPackage GetNextInput(); + + /// For device input configuration/polling + void BeginConfiguration(); + void EndConfiguration(); + + bool IsPolling() { + return polling; + } + +private: + int analog_x_axis = -1; + int analog_y_axis = -1; + int controller_number = -1; + bool polling = false; +}; +} // namespace InputCommon -- cgit v1.2.3 From c94583d867fd909d8731ba50e085352aae0e6885 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 15:31:57 -0400 Subject: Clang Formatting --- src/input_common/gcadapter/gc_adapter.cpp | 90 +++++++++++++++++++------------ src/input_common/gcadapter/gc_adapter.h | 16 +++--- src/input_common/gcadapter/gc_poller.cpp | 56 +++++++++---------- src/input_common/gcadapter/gc_poller.h | 5 +- 4 files changed, 96 insertions(+), 71 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index d42261d61..dc04116ce 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -1,7 +1,7 @@ // Copyright 2014 Dolphin Emulator Project // Licensed under GPLv2+ // Refer to the license.txt file included. -//* + #include "common/logging/log.h" #include "common/threadsafe_queue.h" #include "input_common/gcadapter/gc_adapter.h" @@ -45,35 +45,48 @@ GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { u8 b1 = adapter_payload[1 + (9 * port) + 1]; u8 b2 = adapter_payload[1 + (9 * port) + 2]; - if (b1 & (1 << 0)) + if (b1 & (1 << 0)) { pad.button |= PAD_BUTTON_A; - if (b1 & (1 << 1)) + } + if (b1 & (1 << 1)) { pad.button |= PAD_BUTTON_B; - if (b1 & (1 << 2)) + } + if (b1 & (1 << 2)) { pad.button |= PAD_BUTTON_X; - if (b1 & (1 << 3)) + } + if (b1 & (1 << 3)) { pad.button |= PAD_BUTTON_Y; + } - if (b1 & (1 << 4)) + if (b1 & (1 << 4)) { pad.button |= PAD_BUTTON_LEFT; - if (b1 & (1 << 5)) + } + if (b1 & (1 << 5)) { pad.button |= PAD_BUTTON_RIGHT; - if (b1 & (1 << 6)) + } + if (b1 & (1 << 6)) { pad.button |= PAD_BUTTON_DOWN; - if (b1 & (1 << 7)) + } + if (b1 & (1 << 7)) { pad.button |= PAD_BUTTON_UP; + } - if (b2 & (1 << 0)) + if (b2 & (1 << 0)) { pad.button |= PAD_BUTTON_START; - if (b2 & (1 << 1)) + } + if (b2 & (1 << 1)) { pad.button |= PAD_TRIGGER_Z; - if (b2 & (1 << 2)) + } + if (b2 & (1 << 2)) { pad.button |= PAD_TRIGGER_R; - if (b2 & (1 << 3)) + } + if (b2 & (1 << 3)) { pad.button |= PAD_TRIGGER_L; + } - if (get_origin) + if (get_origin) { pad.button |= PAD_GET_ORIGIN; + } pad.stickX = adapter_payload[1 + (9 * port) + 3]; pad.stickY = adapter_payload[1 + (9 * port) + 4]; @@ -86,7 +99,7 @@ GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { } void PadToState(GCPadStatus pad, GCState& state) { - //std::lock_guard lock{s_mutex}; + // std::lock_guard lock{s_mutex}; state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B); state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); @@ -125,7 +138,7 @@ static void Read() { std::begin(controller_payload_copy)); payload_size = payload_size_in; } - + GCPadStatus pad[4]; if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) { @@ -137,8 +150,9 @@ static void Read() { } for (int port = 0; port < 4; port++) { if (DeviceConnected(port) && configuring) { - if (pad[port].button != PAD_GET_ORIGIN) + if (pad[port].button != PAD_GET_ORIGIN) { pad_queue[port].Push(pad[port]); + } // Accounting for a threshold here because of some controller variance if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || @@ -186,8 +200,9 @@ static void ScanThreadFunc() { void Init() { - if (usb_adapter_handle != nullptr) + if (usb_adapter_handle != nullptr) { return; + } LOG_INFO(Input, "GC Adapter Initialization started"); current_status = NO_ADAPTER_DETECTED; @@ -197,10 +212,12 @@ void Init() { } void StartScanThread() { - if (detect_thread_running) + if (detect_thread_running) { return; - if (!libusb_ctx) + } + if (!libusb_ctx) { return; + } detect_thread_running = true; detect_thread = std::thread(ScanThreadFunc); @@ -212,15 +229,17 @@ void StopScanThread() { static void Setup() { // Reset the error status in case the adapter gets unplugged - if (current_status < 0) + if (current_status < 0) { current_status = NO_ADAPTER_DETECTED; + } - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; - + } + libusb_device** devs; // pointer to list of connected usb devices - int cnt = libusb_get_device_list(libusb_ctx, &devs); //get the list of devices + int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices for (int i = 0; i < cnt; i++) { if (CheckDeviceAccess(devs[i])) { @@ -247,9 +266,8 @@ static bool CheckDeviceAccess(libusb_device* device) { ret = libusb_open(device, &usb_adapter_handle); if (ret == LIBUSB_ERROR_ACCESS) { - LOG_ERROR(Input, - "Yuzu can not gain access to this device: ID %04X:%04X.", - desc.idVendor, desc.idProduct); + LOG_ERROR(Input, "Yuzu can not gain access to this device: ID %04X:%04X.", desc.idVendor, + desc.idProduct); return false; } if (ret) { @@ -260,8 +278,9 @@ static bool CheckDeviceAccess(libusb_device* device) { ret = libusb_kernel_driver_active(usb_adapter_handle, 0); if (ret == 1) { ret = libusb_detach_kernel_driver(usb_adapter_handle, 0); - if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) + if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret); + } } if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { @@ -290,8 +309,9 @@ static void GetGCEndpoint(libusb_device* device) { const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; for (u8 e = 0; e < interface->bNumEndpoints; e++) { const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; - if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) + if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { input_endpoint = endpoint->bEndpointAddress; + } } } } @@ -311,16 +331,20 @@ void Shutdown() { static void Reset() { std::unique_lock lock(initialization_mutex, std::defer_lock); - if (!lock.try_lock()) + if (!lock.try_lock()) { return; - if (current_status != ADAPTER_DETECTED) + } + if (current_status != ADAPTER_DETECTED) { return; + } - if (adapter_thread_running) + if (adapter_thread_running) { adapter_input_thread.join(); + } - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; + } current_status = NO_ADAPTER_DETECTED; diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 9b02d1382..05ee73c65 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -1,11 +1,14 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + #pragma once #include -#include -#include #include +#include +#include #include "common/common_types.h" - enum { PAD_USE_ORIGIN = 0x0080, PAD_GET_ORIGIN = 0x2000, @@ -61,13 +64,8 @@ struct GCState { std::unordered_map axes; }; - namespace GCAdapter { -enum ControllerTypes { - CONTROLLER_NONE = 0, - CONTROLLER_WIRED = 1, - CONTROLLER_WIRELESS = 2 -}; +enum ControllerTypes { CONTROLLER_NONE = 0, CONTROLLER_WIRED = 1, CONTROLLER_WIRELESS = 2 }; enum { NO_ADAPTER_DETECTED = 0, diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 772bd8890..51b3362d6 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -1,10 +1,14 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + #include #include #include #include -#include "input_common/gcadapter/gc_poller.h" -#include "input_common/gcadapter/gc_adapter.h" #include "common/threadsafe_queue.h" +#include "input_common/gcadapter/gc_adapter.h" +#include "input_common/gcadapter/gc_poller.h" // Using extern as to avoid multply defined symbols. extern Common::SPSCQueue pad_queue[4]; @@ -14,9 +18,7 @@ namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, int axis_) - : port(port_), button(button_) { - } + explicit GCButton(int port_, int button_, int axis_) : port(port_), button(button_) {} ~GCButton() override; @@ -31,17 +33,14 @@ private: class GCAxisButton final : public Input::ButtonDevice { public: - explicit GCAxisButton(int port_, int axis_, float threshold_, - bool trigger_if_greater_) - : port(port_), axis(axis_), threshold(threshold_), - trigger_if_greater(trigger_if_greater_) { + explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_) + : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_) { } - bool GetStatus() const override { const float axis_value = (state[port].axes.at(axis) - 128.0f) / 128.0f; if (trigger_if_greater) { - return axis_value > 0.10f; //TODO(ameerj) : Fix threshold. + return axis_value > 0.10f; // TODO(ameerj) : Fix threshold. } return axis_value < -0.10f; } @@ -164,29 +163,30 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { void GCButtonFactory::BeginConfiguration() { polling = true; - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { pad_queue[i].Clear(); + } GCAdapter::BeginConfiguration(); } void GCButtonFactory::EndConfiguration() { polling = false; - - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { pad_queue[i].Clear(); + } GCAdapter::EndConfiguration(); } class GCAnalog final : public Input::AnalogDevice { public: GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_) - : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) { - } + : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {} float GetAxis(int axis) const { std::lock_guard lock{mutex}; // division is not by a perfect 128 to account for some variance in center location - // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range [20-230] + // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range + // [20-230] return (state[port].axes.at(axis) - 128.0f) / 95.0f; } @@ -240,18 +240,16 @@ private: mutable std::mutex mutex; }; - /// An analog device factory that creates analog devices from GC Adapter -GCAnalogFactory::GCAnalogFactory() {}; - +GCAnalogFactory::GCAnalogFactory(){}; /** -* Creates analog device from joystick axes -* @param params contains parameters for creating the device: -* - "port": the nth gcpad on the adapter -* - "axis_x": the index of the axis to be bind as x-axis -* - "axis_y": the index of the axis to be bind as y-axis -*/ + * Creates analog device from joystick axes + * @param params contains parameters for creating the device: + * - "port": the nth gcpad on the adapter + * - "axis_x": the index of the axis to be bind as x-axis + * - "axis_y": the index of the axis to be bind as y-axis + */ std::unique_ptr GCAnalogFactory::Create(const Common::ParamPackage& params) { const std::string guid = params.Get("guid", "0"); const int port = params.Get("port", 0); @@ -264,15 +262,17 @@ std::unique_ptr GCAnalogFactory::Create(const Common::Param void GCAnalogFactory::BeginConfiguration() { polling = true; - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { pad_queue[i].Clear(); + } GCAdapter::BeginConfiguration(); } void GCAnalogFactory::EndConfiguration() { polling = false; - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { pad_queue[i].Clear(); + } GCAdapter::EndConfiguration(); } diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index d115b1d2a..43aa9e93a 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h @@ -1,3 +1,7 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + #pragma once #include @@ -5,7 +9,6 @@ namespace InputCommon { - /** * A button device factory representing a gcpad. It receives gcpad events and forward them * to all button devices it created. -- cgit v1.2.3 From 121af3646dad0f80453d2ffffa688dd4435d3acc Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 18:43:01 -0400 Subject: Singleton GC Adapter class, remove globals, fix naming convention Fix clang formatting Manual fix for configure_input_player formatting Add missing lib usb cmake command --- src/input_common/gcadapter/gc_adapter.cpp | 176 +++++++++++++++--------------- src/input_common/gcadapter/gc_adapter.h | 145 ++++++++++++++++-------- src/input_common/gcadapter/gc_poller.cpp | 125 +++++++++++---------- src/input_common/gcadapter/gc_poller.h | 3 + 4 files changed, 252 insertions(+), 197 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index dc04116ce..0696a96c7 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -3,45 +3,41 @@ // Refer to the license.txt file included. #include "common/logging/log.h" -#include "common/threadsafe_queue.h" #include "input_common/gcadapter/gc_adapter.h" -Common::SPSCQueue pad_queue[4]; -struct GCState state[4]; - namespace GCAdapter { +Adapter* Adapter::adapter_instance{nullptr}; -static libusb_device_handle* usb_adapter_handle = nullptr; -static u8 adapter_controllers_status[4] = { - ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, - ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; - -static std::mutex s_mutex; - -static std::thread adapter_input_thread; -static bool adapter_thread_running; - -static std::mutex initialization_mutex; -static std::thread detect_thread; -static bool detect_thread_running = false; +Adapter::Adapter() { + if (usb_adapter_handle != nullptr) { + return; + } + LOG_INFO(Input, "GC Adapter Initialization started"); -static libusb_context* libusb_ctx; + current_status = NO_ADAPTER_DETECTED; + libusb_init(&libusb_ctx); -static u8 input_endpoint = 0; + StartScanThread(); +} -static bool configuring = false; +Adapter* Adapter::GetInstance() { + if (!adapter_instance) { + adapter_instance = new Adapter; + } + return adapter_instance; +} -GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { +GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { GCPadStatus pad = {}; bool get_origin = false; - u8 type = adapter_payload[1 + (9 * port)] >> 4; - if (type) + ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); + if (type != ControllerTypes::None) get_origin = true; adapter_controllers_status[port] = type; - if (adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE) { + if (adapter_controllers_status[port] != ControllerTypes::None) { u8 b1 = adapter_payload[1 + (9 * port) + 1]; u8 b2 = adapter_payload[1 + (9 * port) + 2]; @@ -88,18 +84,17 @@ GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { pad.button |= PAD_GET_ORIGIN; } - pad.stickX = adapter_payload[1 + (9 * port) + 3]; - pad.stickY = adapter_payload[1 + (9 * port) + 4]; - pad.substickX = adapter_payload[1 + (9 * port) + 5]; - pad.substickY = adapter_payload[1 + (9 * port) + 6]; - pad.triggerLeft = adapter_payload[1 + (9 * port) + 7]; - pad.triggerRight = adapter_payload[1 + (9 * port) + 8]; + 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]; } return pad; } -void PadToState(GCPadStatus pad, GCState& state) { - // std::lock_guard lock{s_mutex}; +void Adapter::PadToState(GCPadStatus pad, GCState& state) { state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B); state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); @@ -112,15 +107,15 @@ void PadToState(GCPadStatus pad, GCState& state) { state.buttons.insert_or_assign(PAD_TRIGGER_Z, pad.button & PAD_TRIGGER_Z); state.buttons.insert_or_assign(PAD_TRIGGER_L, pad.button & PAD_TRIGGER_L); state.buttons.insert_or_assign(PAD_TRIGGER_R, pad.button & PAD_TRIGGER_R); - state.axes.insert_or_assign(STICK_X, pad.stickX); - state.axes.insert_or_assign(STICK_Y, pad.stickY); - state.axes.insert_or_assign(SUBSTICK_X, pad.substickX); - state.axes.insert_or_assign(SUBSTICK_Y, pad.substickY); - state.axes.insert_or_assign(TRIGGER_LEFT, pad.triggerLeft); - state.axes.insert_or_assign(TRIGGER_RIGHT, pad.triggerRight); + state.axes.insert_or_assign(static_cast(PadAxes::StickX), pad.stick_x); + state.axes.insert_or_assign(static_cast(PadAxes::StickY), pad.stick_y); + state.axes.insert_or_assign(static_cast(PadAxes::SubstickX), pad.substick_x); + state.axes.insert_or_assign(static_cast(PadAxes::SubstickY), pad.substick_y); + state.axes.insert_or_assign(static_cast(PadAxes::TriggerLeft), pad.trigger_left); + state.axes.insert_or_assign(static_cast(PadAxes::TriggerRight), pad.trigger_right); } -static void Read() { +void Adapter::Read() { LOG_INFO(Input, "GC Adapter Read() thread started"); int payload_size_in; @@ -145,8 +140,9 @@ static void Read() { LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, controller_payload_copy[0]); } else { - for (int i = 0; i < 4; i++) - pad[i] = CheckStatus(i, controller_payload_copy); + for (int port = 0; port < 4; port++) { + pad[port] = CheckStatus(port, controller_payload_copy); + } } for (int port = 0; port < 4; port++) { if (DeviceConnected(port) && configuring) { @@ -155,28 +151,36 @@ static void Read() { } // Accounting for a threshold here because of some controller variance - if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || - pad[port].stickX < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { - pad[port].axis_which = STICK_X; - pad[port].axis_value = pad[port].stickX; + if (pad[port].stick_x > + pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD || + pad[port].stick_x < + pad_constants.MAIN_STICK_CENTER_X - pad_constants.THRESHOLD) { + pad[port].axis = GCAdapter::PadAxes::StickX; + pad[port].axis_value = pad[port].stick_x; pad_queue[port].Push(pad[port]); } - if (pad[port].stickY > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || - pad[port].stickY < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { - pad[port].axis_which = STICK_Y; - pad[port].axis_value = pad[port].stickY; + if (pad[port].stick_y > + pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD || + pad[port].stick_y < + pad_constants.MAIN_STICK_CENTER_Y - pad_constants.THRESHOLD) { + pad[port].axis = GCAdapter::PadAxes::StickY; + pad[port].axis_value = pad[port].stick_y; pad_queue[port].Push(pad[port]); } - if (pad[port].substickX > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || - pad[port].substickX < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { - pad[port].axis_which = SUBSTICK_X; - pad[port].axis_value = pad[port].substickX; + if (pad[port].substick_x > + pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD || + pad[port].substick_x < + pad_constants.C_STICK_CENTER_X - pad_constants.THRESHOLD) { + pad[port].axis = GCAdapter::PadAxes::SubstickX; + pad[port].axis_value = pad[port].substick_x; pad_queue[port].Push(pad[port]); } - if (pad[port].substickY > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || - pad[port].substickY < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { - pad[port].axis_which = SUBSTICK_Y; - pad[port].axis_value = pad[port].substickY; + if (pad[port].substick_y > + pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD || + pad[port].substick_y < + pad_constants.C_STICK_CENTER_Y - pad_constants.THRESHOLD) { + pad[port].axis = GCAdapter::PadAxes::SubstickY; + pad[port].axis_value = pad[port].substick_y; pad_queue[port].Push(pad[port]); } } @@ -186,7 +190,7 @@ static void Read() { } } -static void ScanThreadFunc() { +void Adapter::ScanThreadFunc() { LOG_INFO(Input, "GC Adapter scanning thread started"); while (detect_thread_running) { @@ -198,20 +202,7 @@ static void ScanThreadFunc() { } } -void Init() { - - if (usb_adapter_handle != nullptr) { - return; - } - LOG_INFO(Input, "GC Adapter Initialization started"); - - current_status = NO_ADAPTER_DETECTED; - libusb_init(&libusb_ctx); - - StartScanThread(); -} - -void StartScanThread() { +void Adapter::StartScanThread() { if (detect_thread_running) { return; } @@ -220,21 +211,21 @@ void StartScanThread() { } detect_thread_running = true; - detect_thread = std::thread(ScanThreadFunc); + detect_thread = std::thread([=] { ScanThreadFunc(); }); } -void StopScanThread() { +void Adapter::StopScanThread() { detect_thread.join(); } -static void Setup() { +void Adapter::Setup() { // Reset the error status in case the adapter gets unplugged if (current_status < 0) { current_status = NO_ADAPTER_DETECTED; } for (int i = 0; i < 4; i++) { - adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; + adapter_controllers_status[i] = ControllerTypes::None; } libusb_device** devs; // pointer to list of connected usb devices @@ -250,7 +241,7 @@ static void Setup() { } } -static bool CheckDeviceAccess(libusb_device* device) { +bool Adapter::CheckDeviceAccess(libusb_device* device) { libusb_device_descriptor desc; int ret = libusb_get_device_descriptor(device, &desc); if (ret) { @@ -300,7 +291,7 @@ static bool CheckDeviceAccess(libusb_device* device) { return true; } -static void GetGCEndpoint(libusb_device* device) { +void Adapter::GetGCEndpoint(libusb_device* device) { libusb_config_descriptor* config = nullptr; libusb_get_config_descriptor(device, 0, &config); for (u8 ic = 0; ic < config->bNumInterfaces; ic++) { @@ -318,18 +309,17 @@ static void GetGCEndpoint(libusb_device* device) { adapter_thread_running = true; current_status = ADAPTER_DETECTED; - - adapter_input_thread = std::thread(Read); // Read input + adapter_input_thread = std::thread([=] { Read(); }); // Read input } -void Shutdown() { +Adapter::~Adapter() { StopScanThread(); Reset(); current_status = NO_ADAPTER_DETECTED; } -static void Reset() { +void Adapter::Reset() { std::unique_lock lock(initialization_mutex, std::defer_lock); if (!lock.try_lock()) { return; @@ -343,7 +333,7 @@ static void Reset() { } for (int i = 0; i < 4; i++) { - adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; + adapter_controllers_status[i] = ControllerTypes::None; } current_status = NO_ADAPTER_DETECTED; @@ -355,20 +345,28 @@ static void Reset() { } } -bool DeviceConnected(int port) { - return adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE; +bool Adapter::DeviceConnected(int port) { + return adapter_controllers_status[port] != ControllerTypes::None; } -void ResetDeviceType(int port) { - adapter_controllers_status[port] = ControllerTypes::CONTROLLER_NONE; +void Adapter::ResetDeviceType(int port) { + adapter_controllers_status[port] = ControllerTypes::None; } -void BeginConfiguration() { +void Adapter::BeginConfiguration() { configuring = true; } -void EndConfiguration() { +void Adapter::EndConfiguration() { configuring = false; } +std::array, 4>& Adapter::GetPadQueue() { + return pad_queue; +} + +std::array& Adapter::GetPadState() { + return state; +} + } // end of namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 05ee73c65..3d5c41f9a 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -8,6 +8,9 @@ #include #include #include "common/common_types.h" +#include "common/threadsafe_queue.h" + +namespace GCAdapter { enum { PAD_USE_ORIGIN = 0x0080, @@ -33,29 +36,38 @@ enum PadButton { }; -enum PadAxes { STICK_X, STICK_Y, SUBSTICK_X, SUBSTICK_Y, TRIGGER_LEFT, TRIGGER_RIGHT }; +enum class PadAxes : u8 { + StickX, + StickY, + SubstickX, + SubstickY, + TriggerLeft, + TriggerRight, + Undefined, +}; +const struct GCPadConstants { + const u8 MAIN_STICK_CENTER_X = 0x80; + const u8 MAIN_STICK_CENTER_Y = 0x80; + const u8 MAIN_STICK_RADIUS = 0x7f; + const u8 C_STICK_CENTER_X = 0x80; + const u8 C_STICK_CENTER_Y = 0x80; + const u8 C_STICK_RADIUS = 0x7f; + + const u8 TRIGGER_CENTER = 20; + const u8 THRESHOLD = 10; +} pad_constants; struct GCPadStatus { - u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits - u8 stickX; // 0 <= stickX <= 255 - u8 stickY; // 0 <= stickY <= 255 - u8 substickX; // 0 <= substickX <= 255 - u8 substickY; // 0 <= substickY <= 255 - u8 triggerLeft; // 0 <= triggerLeft <= 255 - u8 triggerRight; // 0 <= triggerRight <= 255 - bool isConnected{true}; - - static const u8 MAIN_STICK_CENTER_X = 0x80; - static const u8 MAIN_STICK_CENTER_Y = 0x80; - static const u8 MAIN_STICK_RADIUS = 0x7f; - static const u8 C_STICK_CENTER_X = 0x80; - static const u8 C_STICK_CENTER_Y = 0x80; - static const u8 C_STICK_RADIUS = 0x7f; - - static const u8 TRIGGER_CENTER = 20; - static const u8 THRESHOLD = 10; + u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits + u8 stick_x; // 0 <= stick_x <= 255 + u8 stick_y; // 0 <= stick_y <= 255 + u8 substick_x; // 0 <= substick_x <= 255 + u8 substick_y; // 0 <= substick_y <= 255 + u8 trigger_left; // 0 <= trigger_left <= 255 + u8 trigger_right; // 0 <= trigger_right <= 255 + u8 port; - u8 axis_which = 255; + PadAxes axis = PadAxes::Undefined; u8 axis_value = 255; }; @@ -64,51 +76,88 @@ struct GCState { std::unordered_map axes; }; -namespace GCAdapter { -enum ControllerTypes { CONTROLLER_NONE = 0, CONTROLLER_WIRED = 1, CONTROLLER_WIRELESS = 2 }; +enum class ControllerTypes { None, Wired, Wireless }; enum { NO_ADAPTER_DETECTED = 0, ADAPTER_DETECTED = 1, }; -// Current adapter status: detected/not detected/in error (holds the error code) -static int current_status = NO_ADAPTER_DETECTED; +/// Singleton Adapter class +class Adapter { +public: + /// For retreiving the singleton instance + static Adapter* GetInstance(); + + /// Used for polling + void BeginConfiguration(); + void EndConfiguration(); + + std::array, 4>& GetPadQueue(); + std::array& GetPadState(); + +private: + /// Singleton instance. + static Adapter* adapter_instance; + + /// Initialize the GC Adapter capture and read sequence + Adapter(); -GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); -/// Initialize the GC Adapter capture and read sequence -void Init(); + /// Close the adapter read thread and release the adapter + ~Adapter(); -/// Close the adapter read thread and release the adapter -void Shutdown(); + GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); -/// Begin scanning for the GC Adapter. -void StartScanThread(); + void PadToState(GCPadStatus pad, GCState& state); -/// Stop scanning for the adapter -void StopScanThread(); + void Read(); + void ScanThreadFunc(); + /// Begin scanning for the GC Adapter. + void StartScanThread(); -/// Returns true if there is a device connected to port -bool DeviceConnected(int port); + /// Stop scanning for the adapter + void StopScanThread(); -/// Resets status of device connected to port -void ResetDeviceType(int port); + /// Returns true if there is a device connected to port + bool DeviceConnected(int port); -/// Returns true if we successfully gain access to GC Adapter -bool CheckDeviceAccess(libusb_device* device); + /// Resets status of device connected to port + void ResetDeviceType(int port); -/// Captures GC Adapter endpoint address, -void GetGCEndpoint(libusb_device* device); + /// Returns true if we successfully gain access to GC Adapter + bool CheckDeviceAccess(libusb_device* device); -/// For shutting down, clear all data, join all threads, release usb -void Reset(); + /// Captures GC Adapter endpoint address, + void GetGCEndpoint(libusb_device* device); -/// For use in initialization, querying devices to find the adapter -void Setup(); + /// For shutting down, clear all data, join all threads, release usb + void Reset(); -/// Used for polling -void BeginConfiguration(); + /// For use in initialization, querying devices to find the adapter + void Setup(); -void EndConfiguration(); + int current_status = NO_ADAPTER_DETECTED; + libusb_device_handle* usb_adapter_handle = nullptr; + ControllerTypes adapter_controllers_status[4] = {ControllerTypes::None, ControllerTypes::None, + ControllerTypes::None, ControllerTypes::None}; + + std::mutex s_mutex; + + std::thread adapter_input_thread; + bool adapter_thread_running; + + std::mutex initialization_mutex; + std::thread detect_thread; + bool detect_thread_running = false; + + libusb_context* libusb_ctx; + + u8 input_endpoint = 0; + + bool configuring = false; + + std::array, 4> pad_queue; + std::array state; +}; } // end of namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 51b3362d6..0e591baca 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -10,35 +10,34 @@ #include "input_common/gcadapter/gc_adapter.h" #include "input_common/gcadapter/gc_poller.h" -// Using extern as to avoid multply defined symbols. -extern Common::SPSCQueue pad_queue[4]; -extern struct GCState state[4]; - namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, int axis_) : port(port_), button(button_) {} + explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) + : port(port_), button(button_), gcadapter(adapter) {} ~GCButton() override; bool GetStatus() const override { - return state[port].buttons.at(button); + return gcadapter->GetPadState()[port].buttons.at(button); } private: const int port; const int button; + GCAdapter::Adapter* gcadapter; }; class GCAxisButton final : public Input::ButtonDevice { public: - explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_) - : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_) { - } + explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, + GCAdapter::Adapter* adapter) + : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), + gcadapter(adapter) {} bool GetStatus() const override { - const float axis_value = (state[port].axes.at(axis) - 128.0f) / 128.0f; + const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; if (trigger_if_greater) { return axis_value > 0.10f; // TODO(ameerj) : Fix threshold. } @@ -50,14 +49,15 @@ private: const int axis; float threshold; bool trigger_if_greater; + GCAdapter::Adapter* gcadapter; }; GCButtonFactory::GCButtonFactory() { - GCAdapter::Init(); + adapter = GCAdapter::Adapter::GetInstance(); } GCButton::~GCButton() { - GCAdapter::Shutdown(); + // GCAdapter::Shutdown(); } std::unique_ptr GCButtonFactory::Create(const Common::ParamPackage& params) { @@ -77,76 +77,76 @@ std::unique_ptr GCButtonFactory::Create(const Common::Param trigger_if_greater = true; LOG_ERROR(Input, "Unknown direction {}", direction_name); } - return std::make_unique(port, axis, threshold, trigger_if_greater); + return std::make_unique(port, axis, threshold, trigger_if_greater, adapter); } std::unique_ptr button = - std::make_unique(port, button_id, params.Get("axis", 0)); + std::make_unique(port, button_id, params.Get("axis", 0), adapter); return std::move(button); } Common::ParamPackage GCButtonFactory::GetNextInput() { Common::ParamPackage params; - GCPadStatus pad; + GCAdapter::GCPadStatus pad; for (int i = 0; i < 4; i++) { - while (pad_queue[i].Pop(pad)) { + while (adapter->GetPadQueue()[i].Pop(pad)) { // This while loop will break on the earliest detected button params.Set("engine", "gcpad"); params.Set("port", i); // I was debating whether to keep these verbose for ease of reading // or to use a while loop shifting the bits to test and set the value. - if (pad.button & PAD_BUTTON_A) { - params.Set("button", PAD_BUTTON_A); + if (pad.button & GCAdapter::PAD_BUTTON_A) { + params.Set("button", GCAdapter::PAD_BUTTON_A); break; } - if (pad.button & PAD_BUTTON_B) { - params.Set("button", PAD_BUTTON_B); + if (pad.button & GCAdapter::PAD_BUTTON_B) { + params.Set("button", GCAdapter::PAD_BUTTON_B); break; } - if (pad.button & PAD_BUTTON_X) { - params.Set("button", PAD_BUTTON_X); + if (pad.button & GCAdapter::PAD_BUTTON_X) { + params.Set("button", GCAdapter::PAD_BUTTON_X); break; } - if (pad.button & PAD_BUTTON_Y) { - params.Set("button", PAD_BUTTON_Y); + if (pad.button & GCAdapter::PAD_BUTTON_Y) { + params.Set("button", GCAdapter::PAD_BUTTON_Y); break; } - if (pad.button & PAD_BUTTON_DOWN) { - params.Set("button", PAD_BUTTON_DOWN); + if (pad.button & GCAdapter::PAD_BUTTON_DOWN) { + params.Set("button", GCAdapter::PAD_BUTTON_DOWN); break; } - if (pad.button & PAD_BUTTON_LEFT) { - params.Set("button", PAD_BUTTON_LEFT); + if (pad.button & GCAdapter::PAD_BUTTON_LEFT) { + params.Set("button", GCAdapter::PAD_BUTTON_LEFT); break; } - if (pad.button & PAD_BUTTON_RIGHT) { - params.Set("button", PAD_BUTTON_RIGHT); + if (pad.button & GCAdapter::PAD_BUTTON_RIGHT) { + params.Set("button", GCAdapter::PAD_BUTTON_RIGHT); break; } - if (pad.button & PAD_BUTTON_UP) { - params.Set("button", PAD_BUTTON_UP); + if (pad.button & GCAdapter::PAD_BUTTON_UP) { + params.Set("button", GCAdapter::PAD_BUTTON_UP); break; } - if (pad.button & PAD_TRIGGER_L) { - params.Set("button", PAD_TRIGGER_L); + if (pad.button & GCAdapter::PAD_TRIGGER_L) { + params.Set("button", GCAdapter::PAD_TRIGGER_L); break; } - if (pad.button & PAD_TRIGGER_R) { - params.Set("button", PAD_TRIGGER_R); + if (pad.button & GCAdapter::PAD_TRIGGER_R) { + params.Set("button", GCAdapter::PAD_TRIGGER_R); break; } - if (pad.button & PAD_TRIGGER_Z) { - params.Set("button", PAD_TRIGGER_Z); + if (pad.button & GCAdapter::PAD_TRIGGER_Z) { + params.Set("button", GCAdapter::PAD_TRIGGER_Z); break; } - if (pad.button & PAD_BUTTON_START) { - params.Set("button", PAD_BUTTON_START); + if (pad.button & GCAdapter::PAD_BUTTON_START) { + params.Set("button", GCAdapter::PAD_BUTTON_START); break; } // For Axis button implementation - if (pad.axis_which != 255) { - params.Set("axis", pad.axis_which); - params.Set("button", PAD_STICK); + if (pad.axis != GCAdapter::PadAxes::Undefined) { + params.Set("axis", static_cast(pad.axis)); + params.Set("button", GCAdapter::PAD_STICK); if (pad.axis_value > 128) { params.Set("direction", "+"); params.Set("threshold", "0.5"); @@ -164,30 +164,30 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { void GCButtonFactory::BeginConfiguration() { polling = true; for (int i = 0; i < 4; i++) { - pad_queue[i].Clear(); + adapter->GetPadQueue()[i].Clear(); } - GCAdapter::BeginConfiguration(); + adapter->BeginConfiguration(); } void GCButtonFactory::EndConfiguration() { polling = false; for (int i = 0; i < 4; i++) { - pad_queue[i].Clear(); + adapter->GetPadQueue()[i].Clear(); } - GCAdapter::EndConfiguration(); + adapter->EndConfiguration(); } class GCAnalog final : public Input::AnalogDevice { public: - GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_) - : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {} + GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) + : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} float GetAxis(int axis) const { std::lock_guard lock{mutex}; // division is not by a perfect 128 to account for some variance in center location // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range // [20-230] - return (state[port].axes.at(axis) - 128.0f) / 95.0f; + return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; } std::tuple GetAnalog(int axis_x, int axis_y) const { @@ -238,10 +238,13 @@ private: const int axis_y; const float deadzone; mutable std::mutex mutex; + GCAdapter::Adapter* gcadapter; }; /// An analog device factory that creates analog devices from GC Adapter -GCAnalogFactory::GCAnalogFactory(){}; +GCAnalogFactory::GCAnalogFactory() { + adapter = GCAdapter::Adapter::GetInstance(); +}; /** * Creates analog device from joystick axes @@ -257,35 +260,36 @@ std::unique_ptr GCAnalogFactory::Create(const Common::Param const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); - return std::make_unique(port, axis_x, axis_y, deadzone); + return std::make_unique(port, axis_x, axis_y, deadzone, adapter); } void GCAnalogFactory::BeginConfiguration() { polling = true; for (int i = 0; i < 4; i++) { - pad_queue[i].Clear(); + adapter->GetPadQueue()[i].Clear(); } - GCAdapter::BeginConfiguration(); + adapter->BeginConfiguration(); } void GCAnalogFactory::EndConfiguration() { polling = false; for (int i = 0; i < 4; i++) { - pad_queue[i].Clear(); + adapter->GetPadQueue()[i].Clear(); } - GCAdapter::EndConfiguration(); + adapter->EndConfiguration(); } Common::ParamPackage GCAnalogFactory::GetNextInput() { - GCPadStatus pad; + GCAdapter::GCPadStatus pad; for (int i = 0; i < 4; i++) { - while (pad_queue[i].Pop(pad)) { - if (pad.axis_which == 255 || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { + while (adapter->GetPadQueue()[i].Pop(pad)) { + if (pad.axis == GCAdapter::PadAxes::Undefined || + std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { continue; } // An analog device needs two axes, so we need to store the axis for later and wait for // a second SDL event. The axes also must be from the same joystick. - const int axis = pad.axis_which; + const u8 axis = static_cast(pad.axis); if (analog_x_axis == -1) { analog_x_axis = axis; controller_number = i; @@ -307,4 +311,5 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { } return params; } + } // namespace InputCommon diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index 43aa9e93a..29b8c0b7c 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h @@ -35,6 +35,7 @@ public: } private: + GCAdapter::Adapter* adapter; bool polling = false; }; @@ -54,9 +55,11 @@ public: } private: + GCAdapter::Adapter* adapter; int analog_x_axis = -1; int analog_y_axis = -1; int controller_number = -1; bool polling = false; }; + } // namespace InputCommon -- cgit v1.2.3 From 5f0fa4cb8283d61ad2993273fa48b2fd3868a680 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 20:32:43 -0400 Subject: fix include thread --- src/input_common/gcadapter/gc_adapter.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 3d5c41f9a..a32ca0464 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "common/common_types.h" #include "common/threadsafe_queue.h" -- cgit v1.2.3 From 968d631aa59a0a4e51e219eaa143d2b95593c3e7 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 21:15:58 -0400 Subject: std::arrays where appropriate, clear q in adapter class, other touch ups --- src/input_common/gcadapter/gc_adapter.cpp | 16 +++++++++------- src/input_common/gcadapter/gc_adapter.h | 5 +++-- src/input_common/gcadapter/gc_poller.cpp | 16 +--------------- src/input_common/gcadapter/gc_poller.h | 4 ++-- 4 files changed, 15 insertions(+), 26 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 0696a96c7..498bd0d6e 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -134,7 +134,7 @@ void Adapter::Read() { payload_size = payload_size_in; } - GCPadStatus pad[4]; + std::array pad; if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) { LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, @@ -224,9 +224,7 @@ void Adapter::Setup() { current_status = NO_ADAPTER_DETECTED; } - for (int i = 0; i < 4; i++) { - adapter_controllers_status[i] = ControllerTypes::None; - } + adapter_controllers_status.fill(ControllerTypes::None); libusb_device** devs; // pointer to list of connected usb devices @@ -332,9 +330,7 @@ void Adapter::Reset() { adapter_input_thread.join(); } - for (int i = 0; i < 4; i++) { - adapter_controllers_status[i] = ControllerTypes::None; - } + adapter_controllers_status.fill(ControllerTypes::None); current_status = NO_ADAPTER_DETECTED; @@ -354,10 +350,16 @@ void Adapter::ResetDeviceType(int port) { } void Adapter::BeginConfiguration() { + for (auto& pq : pad_queue) { + pq.Clear(); + } configuring = true; } void Adapter::EndConfiguration() { + for (auto& pq : pad_queue) { + pq.Clear(); + } configuring = false; } diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index a32ca0464..cb0dd0ab1 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -96,6 +96,8 @@ public: std::array, 4>& GetPadQueue(); std::array& GetPadState(); + std::array, 4>& GetPadQueue() const; + std::array& GetPadState() const; private: /// Singleton instance. @@ -139,8 +141,7 @@ private: int current_status = NO_ADAPTER_DETECTED; libusb_device_handle* usb_adapter_handle = nullptr; - ControllerTypes adapter_controllers_status[4] = {ControllerTypes::None, ControllerTypes::None, - ControllerTypes::None, ControllerTypes::None}; + std::array adapter_controllers_status{}; std::mutex s_mutex; diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 0e591baca..ac4126cb6 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -56,9 +56,7 @@ GCButtonFactory::GCButtonFactory() { adapter = GCAdapter::Adapter::GetInstance(); } -GCButton::~GCButton() { - // GCAdapter::Shutdown(); -} +GCButton::~GCButton() = default; std::unique_ptr GCButtonFactory::Create(const Common::ParamPackage& params) { int button_id = params.Get("button", 0); @@ -163,17 +161,11 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { void GCButtonFactory::BeginConfiguration() { polling = true; - for (int i = 0; i < 4; i++) { - adapter->GetPadQueue()[i].Clear(); - } adapter->BeginConfiguration(); } void GCButtonFactory::EndConfiguration() { polling = false; - for (int i = 0; i < 4; i++) { - adapter->GetPadQueue()[i].Clear(); - } adapter->EndConfiguration(); } @@ -265,17 +257,11 @@ std::unique_ptr GCAnalogFactory::Create(const Common::Param void GCAnalogFactory::BeginConfiguration() { polling = true; - for (int i = 0; i < 4; i++) { - adapter->GetPadQueue()[i].Clear(); - } adapter->BeginConfiguration(); } void GCAnalogFactory::EndConfiguration() { polling = false; - for (int i = 0; i < 4; i++) { - adapter->GetPadQueue()[i].Clear(); - } adapter->EndConfiguration(); } diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index 29b8c0b7c..31ff1c123 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h @@ -30,7 +30,7 @@ public: void BeginConfiguration(); void EndConfiguration(); - bool IsPolling() { + bool IsPolling() const { return polling; } @@ -50,7 +50,7 @@ public: void BeginConfiguration(); void EndConfiguration(); - bool IsPolling() { + bool IsPolling() const { return polling; } -- cgit v1.2.3 From 46b4461fbb0514dd50c096ef896b1752d81079d0 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 21:50:58 -0400 Subject: shared_ptr for the GC adapter class, constexpr constants --- src/input_common/gcadapter/gc_adapter.cpp | 40 +++++++++++++------------------ src/input_common/gcadapter/gc_adapter.h | 40 +++++++++++++------------------ src/input_common/gcadapter/gc_poller.cpp | 24 +++++++++---------- src/input_common/gcadapter/gc_poller.h | 8 +++---- 4 files changed, 48 insertions(+), 64 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 498bd0d6e..db72d7633 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -6,7 +6,6 @@ #include "input_common/gcadapter/gc_adapter.h" namespace GCAdapter { -Adapter* Adapter::adapter_instance{nullptr}; Adapter::Adapter() { if (usb_adapter_handle != nullptr) { @@ -20,13 +19,6 @@ Adapter::Adapter() { StartScanThread(); } -Adapter* Adapter::GetInstance() { - if (!adapter_instance) { - adapter_instance = new Adapter; - } - return adapter_instance; -} - GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { GCPadStatus pad = {}; bool get_origin = false; @@ -151,34 +143,26 @@ void Adapter::Read() { } // Accounting for a threshold here because of some controller variance - if (pad[port].stick_x > - pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD || - pad[port].stick_x < - pad_constants.MAIN_STICK_CENTER_X - pad_constants.THRESHOLD) { + if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || + pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::StickX; pad[port].axis_value = pad[port].stick_x; pad_queue[port].Push(pad[port]); } - if (pad[port].stick_y > - pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD || - pad[port].stick_y < - pad_constants.MAIN_STICK_CENTER_Y - pad_constants.THRESHOLD) { + if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || + pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::StickY; pad[port].axis_value = pad[port].stick_y; pad_queue[port].Push(pad[port]); } - if (pad[port].substick_x > - pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD || - pad[port].substick_x < - pad_constants.C_STICK_CENTER_X - pad_constants.THRESHOLD) { + if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || + pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::SubstickX; pad[port].axis_value = pad[port].substick_x; pad_queue[port].Push(pad[port]); } - if (pad[port].substick_y > - pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD || - pad[port].substick_y < - pad_constants.C_STICK_CENTER_Y - pad_constants.THRESHOLD) { + if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || + pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::SubstickY; pad[port].axis_value = pad[port].substick_y; pad_queue[port].Push(pad[port]); @@ -367,8 +351,16 @@ std::array, 4>& Adapter::GetPadQueue() { return pad_queue; } +const std::array, 4>& Adapter::GetPadQueue() const { + return pad_queue; +} + std::array& Adapter::GetPadState() { return state; } +const std::array& Adapter::GetPadState() const { + return state; +} + } // end of namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index cb0dd0ab1..d7a57e7b7 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -46,17 +46,6 @@ enum class PadAxes : u8 { TriggerRight, Undefined, }; -const struct GCPadConstants { - const u8 MAIN_STICK_CENTER_X = 0x80; - const u8 MAIN_STICK_CENTER_Y = 0x80; - const u8 MAIN_STICK_RADIUS = 0x7f; - const u8 C_STICK_CENTER_X = 0x80; - const u8 C_STICK_CENTER_Y = 0x80; - const u8 C_STICK_RADIUS = 0x7f; - - const u8 TRIGGER_CENTER = 20; - const u8 THRESHOLD = 10; -} pad_constants; struct GCPadStatus { u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits @@ -67,6 +56,15 @@ struct GCPadStatus { u8 trigger_left; // 0 <= trigger_left <= 255 u8 trigger_right; // 0 <= trigger_right <= 255 + static constexpr u8 MAIN_STICK_CENTER_X = 0x80; + static constexpr u8 MAIN_STICK_CENTER_Y = 0x80; + static constexpr u8 MAIN_STICK_RADIUS = 0x7f; + static constexpr u8 C_STICK_CENTER_X = 0x80; + static constexpr u8 C_STICK_CENTER_Y = 0x80; + static constexpr u8 C_STICK_RADIUS = 0x7f; + static constexpr u8 TRIGGER_CENTER = 20; + static constexpr u8 THRESHOLD = 10; + u8 port; PadAxes axis = PadAxes::Undefined; u8 axis_value = 255; @@ -87,28 +85,22 @@ enum { /// Singleton Adapter class class Adapter { public: - /// For retreiving the singleton instance - static Adapter* GetInstance(); + /// Initialize the GC Adapter capture and read sequence + Adapter(); + /// Close the adapter read thread and release the adapter + ~Adapter(); /// Used for polling void BeginConfiguration(); void EndConfiguration(); std::array, 4>& GetPadQueue(); + const std::array, 4>& GetPadQueue() const; + std::array& GetPadState(); - std::array, 4>& GetPadQueue() const; - std::array& GetPadState() const; + const std::array& GetPadState() const; private: - /// Singleton instance. - static Adapter* adapter_instance; - - /// Initialize the GC Adapter capture and read sequence - Adapter(); - - /// Close the adapter read thread and release the adapter - ~Adapter(); - GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); void PadToState(GCPadStatus pad, GCState& state); diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index ac4126cb6..ad8b4b431 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -14,7 +14,8 @@ namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) + explicit GCButton(int port_, int button_, int axis_, + std::shared_ptr adapter) : port(port_), button(button_), gcadapter(adapter) {} ~GCButton() override; @@ -26,13 +27,13 @@ public: private: const int port; const int button; - GCAdapter::Adapter* gcadapter; + std::shared_ptr gcadapter; }; class GCAxisButton final : public Input::ButtonDevice { public: explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, - GCAdapter::Adapter* adapter) + std::shared_ptr adapter) : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), gcadapter(adapter) {} @@ -49,12 +50,11 @@ private: const int axis; float threshold; bool trigger_if_greater; - GCAdapter::Adapter* gcadapter; + std::shared_ptr gcadapter; }; -GCButtonFactory::GCButtonFactory() { - adapter = GCAdapter::Adapter::GetInstance(); -} +GCButtonFactory::GCButtonFactory(std::shared_ptr adapter_) + : adapter(adapter_) {} GCButton::~GCButton() = default; @@ -171,7 +171,8 @@ void GCButtonFactory::EndConfiguration() { class GCAnalog final : public Input::AnalogDevice { public: - GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) + GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, + std::shared_ptr adapter) : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} float GetAxis(int axis) const { @@ -230,13 +231,12 @@ private: const int axis_y; const float deadzone; mutable std::mutex mutex; - GCAdapter::Adapter* gcadapter; + std::shared_ptr gcadapter; }; /// An analog device factory that creates analog devices from GC Adapter -GCAnalogFactory::GCAnalogFactory() { - adapter = GCAdapter::Adapter::GetInstance(); -}; +GCAnalogFactory::GCAnalogFactory(std::shared_ptr adapter_) + : adapter(adapter_) {} /** * Creates analog device from joystick axes diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index 31ff1c123..d3a56da5b 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h @@ -15,7 +15,7 @@ namespace InputCommon { */ class GCButtonFactory final : public Input::Factory { public: - GCButtonFactory(); + GCButtonFactory(std::shared_ptr adapter_); /** * Creates a button device from a button press @@ -35,14 +35,14 @@ public: } private: - GCAdapter::Adapter* adapter; + std::shared_ptr adapter; bool polling = false; }; /// An analog device factory that creates analog devices from GC Adapter class GCAnalogFactory final : public Input::Factory { public: - GCAnalogFactory(); + GCAnalogFactory(std::shared_ptr adapter_); std::unique_ptr Create(const Common::ParamPackage& params) override; Common::ParamPackage GetNextInput(); @@ -55,7 +55,7 @@ public: } private: - GCAdapter::Adapter* adapter; + std::shared_ptr adapter; int analog_x_axis = -1; int analog_y_axis = -1; int controller_number = -1; -- cgit v1.2.3 From 0f729ef0785eba2a2bd8e9581da4050209c2611d Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 22:58:53 -0400 Subject: fix for sleep using stl --- src/input_common/gcadapter/gc_adapter.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index db72d7633..e3bcb24f6 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include +#include #include "common/logging/log.h" #include "input_common/gcadapter/gc_adapter.h" @@ -182,7 +184,7 @@ void Adapter::ScanThreadFunc() { std::lock_guard lk(initialization_mutex); Setup(); } - Sleep(500); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); } } -- cgit v1.2.3 From 28046ae3a9cd5e32c7cae1cf64aa005502bf1749 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 23:56:56 -0400 Subject: Tidy up the pointers, use pair over tuple where appropriate --- src/input_common/gcadapter/gc_poller.cpp | 36 +++++++++++++++----------------- src/input_common/gcadapter/gc_poller.h | 6 ++++-- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index ad8b4b431..be7c600a2 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -14,8 +14,7 @@ namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, int axis_, - std::shared_ptr adapter) + explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) : port(port_), button(button_), gcadapter(adapter) {} ~GCButton() override; @@ -27,13 +26,13 @@ public: private: const int port; const int button; - std::shared_ptr gcadapter; + GCAdapter::Adapter* gcadapter; }; class GCAxisButton final : public Input::ButtonDevice { public: explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, - std::shared_ptr adapter) + GCAdapter::Adapter* adapter) : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), gcadapter(adapter) {} @@ -50,11 +49,11 @@ private: const int axis; float threshold; bool trigger_if_greater; - std::shared_ptr gcadapter; + GCAdapter::Adapter* gcadapter; }; GCButtonFactory::GCButtonFactory(std::shared_ptr adapter_) - : adapter(adapter_) {} + : adapter(std::move(adapter_)) {} GCButton::~GCButton() = default; @@ -75,11 +74,12 @@ std::unique_ptr GCButtonFactory::Create(const Common::Param trigger_if_greater = true; LOG_ERROR(Input, "Unknown direction {}", direction_name); } - return std::make_unique(port, axis, threshold, trigger_if_greater, adapter); + return std::make_unique(port, axis, threshold, trigger_if_greater, + adapter.get()); } std::unique_ptr button = - std::make_unique(port, button_id, params.Get("axis", 0), adapter); + std::make_unique(port, button_id, params.Get("axis", 0), adapter.get()); return std::move(button); } @@ -171,8 +171,7 @@ void GCButtonFactory::EndConfiguration() { class GCAnalog final : public Input::AnalogDevice { public: - GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, - std::shared_ptr adapter) + GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} float GetAxis(int axis) const { @@ -183,7 +182,7 @@ public: return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; } - std::tuple GetAnalog(int axis_x, int axis_y) const { + std::pair GetAnalog(int axis_x, int axis_y) const { float x = GetAxis(axis_x); float y = GetAxis(axis_y); @@ -196,17 +195,17 @@ public: y /= r; } - return std::make_tuple(x, y); + return {x, y}; } std::tuple GetStatus() const override { const auto [x, y] = GetAnalog(axis_x, axis_y); const float r = std::sqrt((x * x) + (y * y)); if (r > deadzone) { - return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), - y / r * (r - deadzone) / (1 - deadzone)); + return {x / r * (r - deadzone) / (1 - deadzone), + y / r * (r - deadzone) / (1 - deadzone)}; } - return std::make_tuple(0.0f, 0.0f); + return {0.0f, 0.0f}; } bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { @@ -231,12 +230,12 @@ private: const int axis_y; const float deadzone; mutable std::mutex mutex; - std::shared_ptr gcadapter; + GCAdapter::Adapter* gcadapter; }; /// An analog device factory that creates analog devices from GC Adapter GCAnalogFactory::GCAnalogFactory(std::shared_ptr adapter_) - : adapter(adapter_) {} + : adapter(std::move(adapter_)) {} /** * Creates analog device from joystick axes @@ -246,13 +245,12 @@ GCAnalogFactory::GCAnalogFactory(std::shared_ptr adapter_) * - "axis_y": the index of the axis to be bind as y-axis */ std::unique_ptr GCAnalogFactory::Create(const Common::ParamPackage& params) { - const std::string guid = params.Get("guid", "0"); const int port = params.Get("port", 0); const int axis_x = params.Get("axis_x", 0); const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); - return std::make_unique(port, axis_x, axis_y, deadzone, adapter); + return std::make_unique(port, axis_x, axis_y, deadzone, adapter.get()); } void GCAnalogFactory::BeginConfiguration() { diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index d3a56da5b..e96af7d51 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h @@ -6,6 +6,7 @@ #include #include "core/frontend/input.h" +#include "input_common/gcadapter/gc_adapter.h" namespace InputCommon { @@ -15,7 +16,7 @@ namespace InputCommon { */ class GCButtonFactory final : public Input::Factory { public: - GCButtonFactory(std::shared_ptr adapter_); + explicit GCButtonFactory(std::shared_ptr adapter_); /** * Creates a button device from a button press @@ -42,7 +43,8 @@ private: /// An analog device factory that creates analog devices from GC Adapter class GCAnalogFactory final : public Input::Factory { public: - GCAnalogFactory(std::shared_ptr adapter_); + explicit GCAnalogFactory(std::shared_ptr adapter_); + std::unique_ptr Create(const Common::ParamPackage& params) override; Common::ParamPackage GetNextInput(); -- cgit v1.2.3 From fcc23139f622963c86e7f53c8b96f6f214294032 Mon Sep 17 00:00:00 2001 From: Ameer Date: Mon, 22 Jun 2020 18:11:59 -0400 Subject: std::array and const reference passing of non-trivial objects --- src/input_common/gcadapter/gc_adapter.cpp | 21 ++++++++++----------- src/input_common/gcadapter/gc_adapter.h | 6 +++--- 2 files changed, 13 insertions(+), 14 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index e3bcb24f6..745f1be27 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -21,7 +21,7 @@ Adapter::Adapter() { StartScanThread(); } -GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { +GCPadStatus Adapter::CheckStatus(int port, const std::array& adapter_payload) { GCPadStatus pad = {}; bool get_origin = false; @@ -88,7 +88,7 @@ GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { return pad; } -void Adapter::PadToState(GCPadStatus pad, GCState& state) { +void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B); state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); @@ -112,15 +112,15 @@ void Adapter::PadToState(GCPadStatus pad, GCState& state) { void Adapter::Read() { LOG_INFO(Input, "GC Adapter Read() thread started"); - int payload_size_in; - u8 adapter_payload[37]; + int payload_size_in, payload_size; + std::array adapter_payload; + std::array controller_payload_copy; + std::array pad; + while (adapter_thread_running) { - libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload, + libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), sizeof(adapter_payload), &payload_size_in, 32); - - int payload_size = 0; - u8 controller_payload_copy[37]; - + payload_size = 0; { std::lock_guard lk(s_mutex); std::copy(std::begin(adapter_payload), std::end(adapter_payload), @@ -128,7 +128,6 @@ void Adapter::Read() { payload_size = payload_size_in; } - std::array pad; if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) { LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, @@ -365,4 +364,4 @@ const std::array& Adapter::GetPadState() const { return state; } -} // end of namespace GCAdapter +} // namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index d7a57e7b7..ff0202e3b 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -101,9 +101,9 @@ public: const std::array& GetPadState() const; private: - GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); + GCPadStatus CheckStatus(int port, const std::array& adapter_payload); - void PadToState(GCPadStatus pad, GCState& state); + void PadToState(const GCPadStatus& pad, GCState& state); void Read(); void ScanThreadFunc(); @@ -154,4 +154,4 @@ private: std::array state; }; -} // end of namespace GCAdapter +} // namespace GCAdapter -- cgit v1.2.3 From d4e07fd95e999e34562428c628985a6eb1fb532d Mon Sep 17 00:00:00 2001 From: Ameer Date: Tue, 23 Jun 2020 12:47:58 -0400 Subject: Fix deallocation of GC Adapter --- src/input_common/gcadapter/gc_adapter.cpp | 10 +++++++--- src/input_common/gcadapter/gc_adapter.h | 1 - 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 745f1be27..887cde263 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -130,6 +130,7 @@ void Adapter::Read() { if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) { + // TODO: It might be worthwhile to Shutdown GC Adapter if we encounter errors here LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, controller_payload_copy[0]); } else { @@ -200,6 +201,7 @@ void Adapter::StartScanThread() { } void Adapter::StopScanThread() { + detect_thread_running = false; detect_thread.join(); } @@ -298,8 +300,6 @@ void Adapter::GetGCEndpoint(libusb_device* device) { Adapter::~Adapter() { StopScanThread(); Reset(); - - current_status = NO_ADAPTER_DETECTED; } void Adapter::Reset() { @@ -312,11 +312,11 @@ void Adapter::Reset() { } if (adapter_thread_running) { + adapter_thread_running = false; adapter_input_thread.join(); } adapter_controllers_status.fill(ControllerTypes::None); - current_status = NO_ADAPTER_DETECTED; if (usb_adapter_handle) { @@ -324,6 +324,10 @@ void Adapter::Reset() { libusb_close(usb_adapter_handle); usb_adapter_handle = nullptr; } + + if (libusb_ctx) { + libusb_exit(libusb_ctx); + } } bool Adapter::DeviceConnected(int port) { diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index ff0202e3b..7aed0b480 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -82,7 +82,6 @@ enum { ADAPTER_DETECTED = 1, }; -/// Singleton Adapter class class Adapter { public: /// Initialize the GC Adapter capture and read sequence -- cgit v1.2.3 From 743e1f02a06187164d55f4208b8e85742abd4498 Mon Sep 17 00:00:00 2001 From: Ameer Date: Tue, 23 Jun 2020 17:37:15 -0400 Subject: cleanup check access, read, and factory GetNextInput funcs. Use size rather than magic number --- src/input_common/gcadapter/gc_adapter.cpp | 169 ++++++++++++++---------------- src/input_common/gcadapter/gc_adapter.h | 8 +- src/input_common/gcadapter/gc_poller.cpp | 74 ++++--------- 3 files changed, 101 insertions(+), 150 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 887cde263..9437d628f 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -21,57 +21,38 @@ Adapter::Adapter() { StartScanThread(); } -GCPadStatus Adapter::CheckStatus(int port, const std::array& adapter_payload) { +GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_payload) { GCPadStatus pad = {}; bool get_origin = false; ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); - if (type != ControllerTypes::None) + if (type != ControllerTypes::None) { get_origin = true; + } adapter_controllers_status[port] = type; - if (adapter_controllers_status[port] != ControllerTypes::None) { - u8 b1 = adapter_payload[1 + (9 * port) + 1]; - u8 b2 = adapter_payload[1 + (9 * port) + 2]; + constexpr std::array b1_buttons{ + PAD_BUTTON_A, PAD_BUTTON_B, PAD_BUTTON_X, PAD_BUTTON_Y, + PAD_BUTTON_LEFT, PAD_BUTTON_RIGHT, PAD_BUTTON_DOWN, PAD_BUTTON_UP}; - if (b1 & (1 << 0)) { - pad.button |= PAD_BUTTON_A; - } - if (b1 & (1 << 1)) { - pad.button |= PAD_BUTTON_B; - } - if (b1 & (1 << 2)) { - pad.button |= PAD_BUTTON_X; - } - if (b1 & (1 << 3)) { - pad.button |= PAD_BUTTON_Y; - } + constexpr std::array b2_buttons{PAD_BUTTON_START, PAD_TRIGGER_Z, PAD_TRIGGER_R, + PAD_TRIGGER_L}; - if (b1 & (1 << 4)) { - pad.button |= PAD_BUTTON_LEFT; - } - if (b1 & (1 << 5)) { - pad.button |= PAD_BUTTON_RIGHT; - } - if (b1 & (1 << 6)) { - pad.button |= PAD_BUTTON_DOWN; - } - if (b1 & (1 << 7)) { - pad.button |= PAD_BUTTON_UP; - } + 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]; - if (b2 & (1 << 0)) { - pad.button |= PAD_BUTTON_START; - } - if (b2 & (1 << 1)) { - pad.button |= PAD_TRIGGER_Z; - } - if (b2 & (1 << 2)) { - pad.button |= PAD_TRIGGER_R; + for (int i = 0; i < b1_buttons.size(); i++) { + if (b1 & (1 << i)) { + pad.button |= b1_buttons[i]; + } } - if (b2 & (1 << 3)) { - pad.button |= PAD_TRIGGER_L; + + for (int j = 0; j < b2_buttons.size(); j++) { + if (b2 & (1 << j)) { + pad.button |= b2_buttons[j]; + } } if (get_origin) { @@ -112,65 +93,65 @@ void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { void Adapter::Read() { LOG_INFO(Input, "GC Adapter Read() thread started"); - int payload_size_in, payload_size; + int payload_size_in, payload_size_copy; std::array adapter_payload; - std::array controller_payload_copy; - std::array pad; + std::array adapter_payload_copy; + std::array pads; while (adapter_thread_running) { libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), sizeof(adapter_payload), &payload_size_in, 32); - payload_size = 0; + payload_size_copy = 0; { std::lock_guard lk(s_mutex); std::copy(std::begin(adapter_payload), std::end(adapter_payload), - std::begin(controller_payload_copy)); - payload_size = payload_size_in; + std::begin(adapter_payload_copy)); + payload_size_copy = payload_size_in; } - if (payload_size != sizeof(controller_payload_copy) || - controller_payload_copy[0] != LIBUSB_DT_HID) { + if (payload_size_copy != sizeof(adapter_payload_copy) || + adapter_payload_copy[0] != LIBUSB_DT_HID) { // TODO: It might be worthwhile to Shutdown GC Adapter if we encounter errors here - LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, - controller_payload_copy[0]); + LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size_copy, + adapter_payload_copy[0]); } else { - for (int port = 0; port < 4; port++) { - pad[port] = CheckStatus(port, controller_payload_copy); + for (int port = 0; port < pads.size(); port++) { + pads[port] = GetPadStatus(port, adapter_payload_copy); } } - for (int port = 0; port < 4; port++) { + for (int port = 0; port < pads.size(); port++) { if (DeviceConnected(port) && configuring) { - if (pad[port].button != PAD_GET_ORIGIN) { - pad_queue[port].Push(pad[port]); + if (pads[port].button != PAD_GET_ORIGIN) { + pad_queue[port].Push(pads[port]); } // Accounting for a threshold here because of some controller variance - if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || - pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { - pad[port].axis = GCAdapter::PadAxes::StickX; - pad[port].axis_value = pad[port].stick_x; - pad_queue[port].Push(pad[port]); + if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD || + pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) { + pads[port].axis = GCAdapter::PadAxes::StickX; + pads[port].axis_value = pads[port].stick_x; + pad_queue[port].Push(pads[port]); } - if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || - pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { - pad[port].axis = GCAdapter::PadAxes::StickY; - pad[port].axis_value = pad[port].stick_y; - pad_queue[port].Push(pad[port]); + if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD || + pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) { + pads[port].axis = GCAdapter::PadAxes::StickY; + pads[port].axis_value = pads[port].stick_y; + pad_queue[port].Push(pads[port]); } - if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || - pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { - pad[port].axis = GCAdapter::PadAxes::SubstickX; - pad[port].axis_value = pad[port].substick_x; - pad_queue[port].Push(pad[port]); + if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD || + pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) { + pads[port].axis = GCAdapter::PadAxes::SubstickX; + pads[port].axis_value = pads[port].substick_x; + pad_queue[port].Push(pads[port]); } - if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || - pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { - pad[port].axis = GCAdapter::PadAxes::SubstickY; - pad[port].axis_value = pad[port].substick_y; - pad_queue[port].Push(pad[port]); + if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD || + pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) { + pads[port].axis = GCAdapter::PadAxes::SubstickY; + pads[port].axis_value = pads[port].substick_y; + pad_queue[port].Push(pads[port]); } } - PadToState(pad[port], state[port]); + PadToState(pads[port], state[port]); } std::this_thread::yield(); } @@ -215,11 +196,11 @@ void Adapter::Setup() { libusb_device** devs; // pointer to list of connected usb devices - int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices + const int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices for (int i = 0; i < cnt; i++) { if (CheckDeviceAccess(devs[i])) { - // GC Adapter found, registering it + // GC Adapter found and accessible, registering it GetGCEndpoint(devs[i]); break; } @@ -228,10 +209,11 @@ void Adapter::Setup() { bool Adapter::CheckDeviceAccess(libusb_device* device) { libusb_device_descriptor desc; - int ret = libusb_get_device_descriptor(device, &desc); - if (ret) { + const int get_descriptor_error = libusb_get_device_descriptor(device, &desc); + if (get_descriptor_error) { // could not acquire the descriptor, no point in trying to use it. - LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", ret); + LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", + get_descriptor_error); return false; } @@ -239,35 +221,36 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { // This isn’t the device we are looking for. return false; } - ret = libusb_open(device, &usb_adapter_handle); + const int open_error = libusb_open(device, &usb_adapter_handle); - if (ret == LIBUSB_ERROR_ACCESS) { + if (open_error == LIBUSB_ERROR_ACCESS) { LOG_ERROR(Input, "Yuzu can not gain access to this device: ID %04X:%04X.", desc.idVendor, desc.idProduct); return false; } - if (ret) { - LOG_ERROR(Input, "libusb_open failed to open device with error = %d", ret); + if (open_error) { + LOG_ERROR(Input, "libusb_open failed to open device with error = %d", open_error); return false; } - ret = libusb_kernel_driver_active(usb_adapter_handle, 0); - if (ret == 1) { - ret = libusb_detach_kernel_driver(usb_adapter_handle, 0); - if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { - LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret); + 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) { + LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", + kernel_driver_error); } } - if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { + if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) { libusb_close(usb_adapter_handle); usb_adapter_handle = nullptr; return false; } - ret = libusb_claim_interface(usb_adapter_handle, 0); - if (ret) { - LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", ret); + const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0); + if (interface_claim_error) { + LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", interface_claim_error); libusb_close(usb_adapter_handle); usb_adapter_handle = nullptr; return false; diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 7aed0b480..abfe0d7b3 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -37,6 +37,12 @@ enum PadButton { }; +/// Used to loop through the and assign button in poller +static constexpr std::array PadButtonArray{ + PAD_BUTTON_LEFT, PAD_BUTTON_RIGHT, PAD_BUTTON_DOWN, PAD_BUTTON_UP, + PAD_TRIGGER_Z, PAD_TRIGGER_R, PAD_TRIGGER_L, PAD_BUTTON_A, + PAD_BUTTON_B, PAD_BUTTON_X, PAD_BUTTON_Y, PAD_BUTTON_START}; + enum class PadAxes : u8 { StickX, StickY, @@ -100,7 +106,7 @@ public: const std::array& GetPadState() const; private: - GCPadStatus CheckStatus(int port, const std::array& adapter_payload); + GCPadStatus GetPadStatus(int port, const std::array& adapter_payload); void PadToState(const GCPadStatus& pad, GCState& state); diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index be7c600a2..977261884 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -58,8 +58,8 @@ GCButtonFactory::GCButtonFactory(std::shared_ptr adapter_) GCButton::~GCButton() = default; std::unique_ptr GCButtonFactory::Create(const Common::ParamPackage& params) { - int button_id = params.Get("button", 0); - int port = params.Get("port", 0); + const int button_id = params.Get("button", 0); + const int port = params.Get("port", 0); // For Axis buttons, used by the binary sticks. if (params.Has("axis")) { const int axis = params.Get("axis", 0); @@ -86,61 +86,22 @@ std::unique_ptr GCButtonFactory::Create(const Common::Param Common::ParamPackage GCButtonFactory::GetNextInput() { Common::ParamPackage params; GCAdapter::GCPadStatus pad; - for (int i = 0; i < 4; i++) { - while (adapter->GetPadQueue()[i].Pop(pad)) { + auto& queue = adapter->GetPadQueue(); + for (int port = 0; port < queue.size(); port++) { + while (queue[port].Pop(pad)) { // This while loop will break on the earliest detected button params.Set("engine", "gcpad"); - params.Set("port", i); + params.Set("port", port); // I was debating whether to keep these verbose for ease of reading // or to use a while loop shifting the bits to test and set the value. - if (pad.button & GCAdapter::PAD_BUTTON_A) { - params.Set("button", GCAdapter::PAD_BUTTON_A); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_B) { - params.Set("button", GCAdapter::PAD_BUTTON_B); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_X) { - params.Set("button", GCAdapter::PAD_BUTTON_X); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_Y) { - params.Set("button", GCAdapter::PAD_BUTTON_Y); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_DOWN) { - params.Set("button", GCAdapter::PAD_BUTTON_DOWN); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_LEFT) { - params.Set("button", GCAdapter::PAD_BUTTON_LEFT); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_RIGHT) { - params.Set("button", GCAdapter::PAD_BUTTON_RIGHT); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_UP) { - params.Set("button", GCAdapter::PAD_BUTTON_UP); - break; - } - if (pad.button & GCAdapter::PAD_TRIGGER_L) { - params.Set("button", GCAdapter::PAD_TRIGGER_L); - break; - } - if (pad.button & GCAdapter::PAD_TRIGGER_R) { - params.Set("button", GCAdapter::PAD_TRIGGER_R); - break; - } - if (pad.button & GCAdapter::PAD_TRIGGER_Z) { - params.Set("button", GCAdapter::PAD_TRIGGER_Z); - break; - } - if (pad.button & GCAdapter::PAD_BUTTON_START) { - params.Set("button", GCAdapter::PAD_BUTTON_START); - break; + + for (auto button : GCAdapter::PadButtonArray) { + if (pad.button & button) { + params.Set("button", button); + break; + } } + // For Axis button implementation if (pad.axis != GCAdapter::PadAxes::Undefined) { params.Set("axis", static_cast(pad.axis)); @@ -265,8 +226,9 @@ void GCAnalogFactory::EndConfiguration() { Common::ParamPackage GCAnalogFactory::GetNextInput() { GCAdapter::GCPadStatus pad; - for (int i = 0; i < 4; i++) { - while (adapter->GetPadQueue()[i].Pop(pad)) { + auto& queue = adapter->GetPadQueue(); + for (int port = 0; port < queue.size(); port++) { + while (queue[port].Pop(pad)) { if (pad.axis == GCAdapter::PadAxes::Undefined || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { continue; @@ -276,8 +238,8 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { const u8 axis = static_cast(pad.axis); if (analog_x_axis == -1) { analog_x_axis = axis; - controller_number = i; - } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == i) { + controller_number = port; + } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == port) { analog_y_axis = axis; } } -- cgit v1.2.3 From c18dc9c707235d7ba6fe230cb3045f2c13d04e62 Mon Sep 17 00:00:00 2001 From: Ameer Date: Wed, 24 Jun 2020 11:39:30 -0400 Subject: padbutton enum class and struct initiailization --- src/input_common/gcadapter/gc_adapter.cpp | 31 +++++++++++++------------------ src/input_common/gcadapter/gc_adapter.h | 30 +++++++++++++++--------------- src/input_common/gcadapter/gc_poller.cpp | 7 ++++--- 3 files changed, 32 insertions(+), 36 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 9437d628f..bba9bcc69 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -33,11 +33,13 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa adapter_controllers_status[port] = type; constexpr std::array b1_buttons{ - PAD_BUTTON_A, PAD_BUTTON_B, PAD_BUTTON_X, PAD_BUTTON_Y, - PAD_BUTTON_LEFT, PAD_BUTTON_RIGHT, PAD_BUTTON_DOWN, PAD_BUTTON_UP}; + PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X, + PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, + PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP}; - constexpr std::array b2_buttons{PAD_BUTTON_START, PAD_TRIGGER_Z, PAD_TRIGGER_R, - PAD_TRIGGER_L}; + constexpr std::array b2_buttons{ + PadButton::PAD_BUTTON_START, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R, + PadButton::PAD_TRIGGER_L}; if (adapter_controllers_status[port] != ControllerTypes::None) { const u8 b1 = adapter_payload[1 + (9 * port) + 1]; @@ -45,13 +47,13 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa for (int i = 0; i < b1_buttons.size(); i++) { if (b1 & (1 << i)) { - pad.button |= b1_buttons[i]; + pad.button |= static_cast(b1_buttons[i]); } } for (int j = 0; j < b2_buttons.size(); j++) { if (b2 & (1 << j)) { - pad.button |= b2_buttons[j]; + pad.button |= static_cast(b2_buttons[j]); } } @@ -70,18 +72,11 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa } void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { - state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); - state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B); - state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); - state.buttons.insert_or_assign(PAD_BUTTON_Y, pad.button & PAD_BUTTON_Y); - state.buttons.insert_or_assign(PAD_BUTTON_LEFT, pad.button & PAD_BUTTON_LEFT); - state.buttons.insert_or_assign(PAD_BUTTON_RIGHT, pad.button & PAD_BUTTON_RIGHT); - state.buttons.insert_or_assign(PAD_BUTTON_DOWN, pad.button & PAD_BUTTON_DOWN); - state.buttons.insert_or_assign(PAD_BUTTON_UP, pad.button & PAD_BUTTON_UP); - state.buttons.insert_or_assign(PAD_BUTTON_START, pad.button & PAD_BUTTON_START); - state.buttons.insert_or_assign(PAD_TRIGGER_Z, pad.button & PAD_TRIGGER_Z); - state.buttons.insert_or_assign(PAD_TRIGGER_L, pad.button & PAD_TRIGGER_L); - state.buttons.insert_or_assign(PAD_TRIGGER_R, pad.button & PAD_TRIGGER_R); + for (auto button : PadButtonArray) { + u16 button_value = static_cast(button); + state.buttons.insert_or_assign(button_value, pad.button & button_value); + } + state.axes.insert_or_assign(static_cast(PadAxes::StickX), pad.stick_x); state.axes.insert_or_assign(static_cast(PadAxes::StickY), pad.stick_y); state.axes.insert_or_assign(static_cast(PadAxes::SubstickX), pad.substick_x); diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index abfe0d7b3..91aa9622b 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -19,7 +19,7 @@ enum { PAD_ERR_STATUS = 0x8000, }; -enum PadButton { +enum class PadButton { PAD_BUTTON_LEFT = 0x0001, PAD_BUTTON_RIGHT = 0x0002, PAD_BUTTON_DOWN = 0x0004, @@ -34,14 +34,14 @@ enum PadButton { PAD_BUTTON_START = 0x1000, // Below is for compatibility with "AxisButton" type PAD_STICK = 0x2000, - }; /// Used to loop through the and assign button in poller static constexpr std::array PadButtonArray{ - PAD_BUTTON_LEFT, PAD_BUTTON_RIGHT, PAD_BUTTON_DOWN, PAD_BUTTON_UP, - PAD_TRIGGER_Z, PAD_TRIGGER_R, PAD_TRIGGER_L, PAD_BUTTON_A, - PAD_BUTTON_B, PAD_BUTTON_X, PAD_BUTTON_Y, PAD_BUTTON_START}; + 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}; enum class PadAxes : u8 { StickX, @@ -54,13 +54,13 @@ enum class PadAxes : u8 { }; struct GCPadStatus { - u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits - u8 stick_x; // 0 <= stick_x <= 255 - u8 stick_y; // 0 <= stick_y <= 255 - u8 substick_x; // 0 <= substick_x <= 255 - u8 substick_y; // 0 <= substick_y <= 255 - u8 trigger_left; // 0 <= trigger_left <= 255 - u8 trigger_right; // 0 <= trigger_right <= 255 + u16 button{}; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits + u8 stick_x{}; // 0 <= stick_x <= 255 + u8 stick_y{}; // 0 <= stick_y <= 255 + u8 substick_x{}; // 0 <= substick_x <= 255 + u8 substick_y{}; // 0 <= substick_y <= 255 + u8 trigger_left{}; // 0 <= trigger_left <= 255 + u8 trigger_right{}; // 0 <= trigger_right <= 255 static constexpr u8 MAIN_STICK_CENTER_X = 0x80; static constexpr u8 MAIN_STICK_CENTER_Y = 0x80; @@ -71,9 +71,9 @@ struct GCPadStatus { static constexpr u8 TRIGGER_CENTER = 20; static constexpr u8 THRESHOLD = 10; - u8 port; - PadAxes axis = PadAxes::Undefined; - u8 axis_value = 255; + u8 port{}; + PadAxes axis{PadAxes::Undefined}; + u8 axis_value{255}; }; struct GCState { diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 977261884..06e16880f 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -96,8 +96,9 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { // or to use a while loop shifting the bits to test and set the value. for (auto button : GCAdapter::PadButtonArray) { - if (pad.button & button) { - params.Set("button", button); + u16 button_value = static_cast(button); + if (pad.button & button_value) { + params.Set("button", button_value); break; } } @@ -105,7 +106,7 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { // For Axis button implementation if (pad.axis != GCAdapter::PadAxes::Undefined) { params.Set("axis", static_cast(pad.axis)); - params.Set("button", GCAdapter::PAD_STICK); + params.Set("button", static_cast(GCAdapter::PadButton::PAD_STICK)); if (pad.axis_value > 128) { params.Set("direction", "+"); params.Set("threshold", "0.5"); -- cgit v1.2.3 From 3f739514e3fa5f3e5c64fa8172c796bd686f6e7d Mon Sep 17 00:00:00 2001 From: Ameer Date: Thu, 25 Jun 2020 19:31:51 -0400 Subject: Stop reading loop if error is encountered --- src/input_common/gcadapter/gc_adapter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index bba9bcc69..173b5c325 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -95,7 +95,7 @@ void Adapter::Read() { while (adapter_thread_running) { libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), - sizeof(adapter_payload), &payload_size_in, 32); + sizeof(adapter_payload), &payload_size_in, 16); payload_size_copy = 0; { std::lock_guard lk(s_mutex); @@ -106,9 +106,9 @@ void Adapter::Read() { if (payload_size_copy != sizeof(adapter_payload_copy) || adapter_payload_copy[0] != LIBUSB_DT_HID) { - // TODO: It might be worthwhile to Shutdown GC Adapter if we encounter errors here LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size_copy, adapter_payload_copy[0]); + adapter_thread_running = false; // error reading from adapter, stop reading. } else { for (int port = 0; port < pads.size(); port++) { pads[port] = GetPadStatus(port, adapter_payload_copy); @@ -291,14 +291,14 @@ void Adapter::Reset() { if (adapter_thread_running) { adapter_thread_running = false; - adapter_input_thread.join(); } + adapter_input_thread.join(); adapter_controllers_status.fill(ControllerTypes::None); current_status = NO_ADAPTER_DETECTED; if (usb_adapter_handle) { - libusb_release_interface(usb_adapter_handle, 0); + libusb_release_interface(usb_adapter_handle, 1); libusb_close(usb_adapter_handle); usb_adapter_handle = nullptr; } -- cgit v1.2.3 From ecbc8137117d6d37b231fc2745b281b6e7077e16 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Thu, 25 Jun 2020 19:46:50 -0400 Subject: const& to button in button array Co-authored-by: VolcaEM <63682805+VolcaEM@users.noreply.github.com> --- src/input_common/gcadapter/gc_adapter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 173b5c325..80355a40d 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -72,7 +72,7 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa } void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { - for (auto button : PadButtonArray) { + for (auto const& button : PadButtonArray) { u16 button_value = static_cast(button); state.buttons.insert_or_assign(button_value, pad.button & button_value); } @@ -213,7 +213,7 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { } if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) { - // This isn’t the device we are looking for. + // This isnÂ’t the device we are looking for. return false; } const int open_error = libusb_open(device, &usb_adapter_handle); -- cgit v1.2.3 From bd697bef039ddf307ed7ddf3d9daf0fd7ec1ee8f Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Fri, 26 Jun 2020 23:46:49 -0400 Subject: left const auto&, comment punctuation. Co-authored-by: Morph <39850852+Morph1984@users.noreply.github.com> --- src/input_common/gcadapter/gc_adapter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 80355a40d..774246bdf 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -72,7 +72,7 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa } void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { - for (auto const& button : PadButtonArray) { + for (const auto& button : PadButtonArray) { u16 button_value = static_cast(button); state.buttons.insert_or_assign(button_value, pad.button & button_value); } @@ -213,7 +213,7 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { } if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) { - // This isnÂ’t the device we are looking for. + // This isn't the device we are looking for. return false; } const int open_error = libusb_open(device, &usb_adapter_handle); -- cgit v1.2.3 From dfdf87d844de6226fd043bb840de8525d8fbc0d1 Mon Sep 17 00:00:00 2001 From: Ameer Date: Tue, 30 Jun 2020 11:44:55 -0400 Subject: fix implicit conversion of size_t type to int --- src/input_common/gcadapter/gc_adapter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 774246bdf..b509b3e46 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -191,7 +191,7 @@ void Adapter::Setup() { libusb_device** devs; // pointer to list of connected usb devices - const int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices + const std::size_t cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices for (int i = 0; i < cnt; i++) { if (CheckDeviceAccess(devs[i])) { -- cgit v1.2.3 From a76e11e7f0c97222388bede03325aa71f1434510 Mon Sep 17 00:00:00 2001 From: Ameer Date: Tue, 30 Jun 2020 17:28:02 -0400 Subject: Address feedback regarding increments, const vars, and general cleanup --- src/input_common/gcadapter/gc_adapter.cpp | 28 ++++++++++++++-------------- src/input_common/gcadapter/gc_poller.cpp | 17 +++++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index b509b3e46..b98b85441 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -45,13 +45,13 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa const u8 b1 = adapter_payload[1 + (9 * port) + 1]; const u8 b2 = adapter_payload[1 + (9 * port) + 2]; - for (int i = 0; i < b1_buttons.size(); i++) { + for (std::size_t i = 0; i < b1_buttons.size(); ++i) { if (b1 & (1 << i)) { pad.button |= static_cast(b1_buttons[i]); } } - for (int j = 0; j < b2_buttons.size(); j++) { + for (std::size_t j = 0; j < b2_buttons.size(); ++j) { if (b2 & (1 << j)) { pad.button |= static_cast(b2_buttons[j]); } @@ -73,7 +73,7 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { for (const auto& button : PadButtonArray) { - u16 button_value = static_cast(button); + const u16 button_value = static_cast(button); state.buttons.insert_or_assign(button_value, pad.button & button_value); } @@ -86,7 +86,7 @@ void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { } void Adapter::Read() { - LOG_INFO(Input, "GC Adapter Read() thread started"); + LOG_DEBUG(Input, "GC Adapter Read() thread started"); int payload_size_in, payload_size_copy; std::array adapter_payload; @@ -109,12 +109,10 @@ void Adapter::Read() { LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size_copy, adapter_payload_copy[0]); adapter_thread_running = false; // error reading from adapter, stop reading. - } else { - for (int port = 0; port < pads.size(); port++) { - pads[port] = GetPadStatus(port, adapter_payload_copy); - } + break; } - for (int port = 0; port < pads.size(); port++) { + for (std::size_t port = 0; port < pads.size(); ++port) { + pads[port] = GetPadStatus(port, adapter_payload_copy); if (DeviceConnected(port) && configuring) { if (pads[port].button != PAD_GET_ORIGIN) { pad_queue[port].Push(pads[port]); @@ -189,14 +187,16 @@ void Adapter::Setup() { adapter_controllers_status.fill(ControllerTypes::None); - libusb_device** devs; // pointer to list of connected usb devices + // pointer to list of connected usb devices + libusb_device** devices; - const std::size_t cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices + // populate the list of devices, get the count + const std::size_t device_count = libusb_get_device_list(libusb_ctx, &devices); - for (int i = 0; i < cnt; i++) { - if (CheckDeviceAccess(devs[i])) { + for (std::size_t index = 0; index < device_count; ++index) { + if (CheckDeviceAccess(devices[index])) { // GC Adapter found and accessible, registering it - GetGCEndpoint(devs[i]); + GetGCEndpoint(devices[index]); break; } } diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 06e16880f..a9de9fedf 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -39,9 +39,9 @@ public: bool GetStatus() const override { const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; if (trigger_if_greater) { - return axis_value > 0.10f; // TODO(ameerj) : Fix threshold. + return axis_value > threshold; // TODO(ameerj) : Fix threshold. } - return axis_value < -0.10f; + return axis_value < -threshold; } private: @@ -87,16 +87,13 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { Common::ParamPackage params; GCAdapter::GCPadStatus pad; auto& queue = adapter->GetPadQueue(); - for (int port = 0; port < queue.size(); port++) { + for (std::size_t port = 0; port < queue.size(); ++port) { while (queue[port].Pop(pad)) { // This while loop will break on the earliest detected button params.Set("engine", "gcpad"); - params.Set("port", port); - // I was debating whether to keep these verbose for ease of reading - // or to use a while loop shifting the bits to test and set the value. - - for (auto button : GCAdapter::PadButtonArray) { - u16 button_value = static_cast(button); + params.Set("port", static_cast(port)); + for (const auto& button : GCAdapter::PadButtonArray) { + const u16 button_value = static_cast(button); if (pad.button & button_value) { params.Set("button", button_value); break; @@ -228,7 +225,7 @@ void GCAnalogFactory::EndConfiguration() { Common::ParamPackage GCAnalogFactory::GetNextInput() { GCAdapter::GCPadStatus pad; auto& queue = adapter->GetPadQueue(); - for (int port = 0; port < queue.size(); port++) { + for (std::size_t port = 0; port < queue.size(); ++port) { while (queue[port].Pop(pad)) { if (pad.axis == GCAdapter::PadAxes::Undefined || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { -- cgit v1.2.3 From 34a590e50966341cec5f1874410931decbce284f Mon Sep 17 00:00:00 2001 From: Ameer Date: Wed, 1 Jul 2020 12:52:50 -0400 Subject: Reset adapter state on init, fixes errors relating driver hang from unexpected unplug --- src/input_common/gcadapter/gc_adapter.cpp | 8 ++++++++ src/input_common/gcadapter/gc_adapter.h | 1 + 2 files changed, 9 insertions(+) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index b98b85441..1ddb9cdb4 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -97,6 +97,7 @@ void Adapter::Read() { libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), sizeof(adapter_payload), &payload_size_in, 16); payload_size_copy = 0; + // this mutex might be redundant? { std::lock_guard lk(s_mutex); std::copy(std::begin(adapter_payload), std::end(adapter_payload), @@ -265,10 +266,17 @@ void Adapter::GetGCEndpoint(libusb_device* device) { const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { input_endpoint = endpoint->bEndpointAddress; + } else { + output_endpoint = endpoint->bEndpointAddress; } } } } + // 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); adapter_thread_running = true; current_status = ADAPTER_DETECTED; diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 91aa9622b..4a8e2644c 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -152,6 +152,7 @@ private: libusb_context* libusb_ctx; u8 input_endpoint = 0; + u8 output_endpoint = 0; bool configuring = false; -- cgit v1.2.3 From 6b7c8e469b2d7943cd7771648659bc362e5d1ddd Mon Sep 17 00:00:00 2001 From: Ameer Date: Thu, 2 Jul 2020 15:54:44 -0400 Subject: Add LR triggers as axes, half press to initiate a press, add GC axis id in config, clarify some code blocks for better readability --- src/input_common/gcadapter/gc_adapter.cpp | 12 ++++++++++++ src/input_common/gcadapter/gc_poller.cpp | 32 +++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 1ddb9cdb4..82f97572f 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -144,6 +144,18 @@ void Adapter::Read() { pads[port].axis_value = pads[port].substick_y; pad_queue[port].Push(pads[port]); } + if (pads[port].trigger_left > pads[port].TRIGGER_CENTER + pads[port].THRESHOLD || + pads[port].trigger_left < pads[port].TRIGGER_CENTER - pads[port].THRESHOLD) { + pads[port].axis = GCAdapter::PadAxes::TriggerLeft; + pads[port].axis_value = pads[port].trigger_left; + pad_queue[port].Push(pads[port]); + } + if (pads[port].trigger_right > pads[port].TRIGGER_CENTER + pads[port].THRESHOLD || + pads[port].trigger_right < pads[port].TRIGGER_CENTER - pads[port].THRESHOLD) { + pads[port].axis = GCAdapter::PadAxes::TriggerRight; + pads[port].axis_value = pads[port].trigger_right; + pad_queue[port].Push(pads[port]); + } } PadToState(pads[port], state[port]); } diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index a9de9fedf..a04c507b8 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -34,7 +34,13 @@ public: explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, GCAdapter::Adapter* adapter) : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), - gcadapter(adapter) {} + gcadapter(adapter) { + // L/R triggers range is only in positive direction beginning near 0 + // 0.0 threshold equates to near half trigger press, but threshold accounts for variability. + if (axis > 3) { + threshold *= -0.5; + } + } bool GetStatus() const override { const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; @@ -60,10 +66,20 @@ GCButton::~GCButton() = default; std::unique_ptr GCButtonFactory::Create(const Common::ParamPackage& params) { const int button_id = params.Get("button", 0); const int port = params.Get("port", 0); + + constexpr int PAD_STICK_ID = static_cast(GCAdapter::PadButton::PAD_STICK); + + // button is not an axis/stick button + if (button_id != PAD_STICK_ID) { + std::unique_ptr button = + std::make_unique(port, button_id, params.Get("axis", 0), adapter.get()); + return std::move(button); + } + // For Axis buttons, used by the binary sticks. - if (params.Has("axis")) { + if (button_id == PAD_STICK_ID) { const int axis = params.Get("axis", 0); - const float threshold = params.Get("threshold", 0.5f); + const float threshold = params.Get("threshold", 0.25f); const std::string direction_name = params.Get("direction", ""); bool trigger_if_greater; if (direction_name == "+") { @@ -77,10 +93,6 @@ std::unique_ptr GCButtonFactory::Create(const Common::Param return std::make_unique(port, axis, threshold, trigger_if_greater, adapter.get()); } - - std::unique_ptr button = - std::make_unique(port, button_id, params.Get("axis", 0), adapter.get()); - return std::move(button); } Common::ParamPackage GCButtonFactory::GetNextInput() { @@ -106,10 +118,10 @@ Common::ParamPackage GCButtonFactory::GetNextInput() { params.Set("button", static_cast(GCAdapter::PadButton::PAD_STICK)); if (pad.axis_value > 128) { params.Set("direction", "+"); - params.Set("threshold", "0.5"); + params.Set("threshold", "0.25"); } else { params.Set("direction", "-"); - params.Set("threshold", "-0.5"); + params.Set("threshold", "-0.25"); } break; } @@ -232,7 +244,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { continue; } // An analog device needs two axes, so we need to store the axis for later and wait for - // a second SDL event. The axes also must be from the same joystick. + // a second input event. The axes also must be from the same joystick. const u8 axis = static_cast(pad.axis); if (analog_x_axis == -1) { analog_x_axis = axis; -- cgit v1.2.3 From e69d715e3d42aaa5e99ebe8e578e64242d049a8c Mon Sep 17 00:00:00 2001 From: Ameer Date: Fri, 3 Jul 2020 11:52:07 -0400 Subject: Address lioncash feedback: Log formatting, extern const PadButtonArray, little touch ups --- src/input_common/gcadapter/gc_adapter.cpp | 40 ++++++++++++++++++++----------- src/input_common/gcadapter/gc_adapter.h | 7 +----- src/input_common/gcadapter/gc_poller.cpp | 9 +++---- 3 files changed, 32 insertions(+), 24 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 82f97572f..f58b0e11c 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -9,6 +9,14 @@ namespace GCAdapter { +/// Used to loop through and assign button in poller +constexpr std::array 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, +}; + Adapter::Adapter() { if (usb_adapter_handle != nullptr) { return; @@ -32,27 +40,31 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array& adapter_pa adapter_controllers_status[port] = type; - constexpr std::array b1_buttons{ + static constexpr std::array b1_buttons{ PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, - PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP}; + PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP, + }; - constexpr std::array b2_buttons{ - PadButton::PAD_BUTTON_START, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R, - PadButton::PAD_TRIGGER_L}; + static constexpr std::array b2_buttons{ + PadButton::PAD_BUTTON_START, + PadButton::PAD_TRIGGER_Z, + PadButton::PAD_TRIGGER_R, + PadButton::PAD_TRIGGER_L, + }; 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]; for (std::size_t i = 0; i < b1_buttons.size(); ++i) { - if (b1 & (1 << i)) { + if ((b1 & (1U << i)) != 0) { pad.button |= static_cast(b1_buttons[i]); } } for (std::size_t j = 0; j < b2_buttons.size(); ++j) { - if (b2 & (1 << j)) { + if ((b2 & (1U << j)) != 0) { pad.button |= static_cast(b2_buttons[j]); } } @@ -107,7 +119,7 @@ void Adapter::Read() { if (payload_size_copy != sizeof(adapter_payload_copy) || adapter_payload_copy[0] != LIBUSB_DT_HID) { - LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size_copy, + LOG_ERROR(Input, "error reading payload (size: {}, type: {:02x})", payload_size_copy, adapter_payload_copy[0]); adapter_thread_running = false; // error reading from adapter, stop reading. break; @@ -220,7 +232,7 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { const int get_descriptor_error = libusb_get_device_descriptor(device, &desc); if (get_descriptor_error) { // could not acquire the descriptor, no point in trying to use it. - LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", + LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: {}", get_descriptor_error); return false; } @@ -232,12 +244,12 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { const int open_error = libusb_open(device, &usb_adapter_handle); if (open_error == LIBUSB_ERROR_ACCESS) { - LOG_ERROR(Input, "Yuzu can not gain access to this device: ID %04X:%04X.", desc.idVendor, - desc.idProduct); + LOG_ERROR(Input, "Yuzu can not gain access to this device: ID {:04X}:{:04X}.", + desc.idVendor, desc.idProduct); return false; } if (open_error) { - LOG_ERROR(Input, "libusb_open failed to open device with error = %d", open_error); + LOG_ERROR(Input, "libusb_open failed to open device with error = {}", open_error); return false; } @@ -245,7 +257,7 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { 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) { - LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", + LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}", kernel_driver_error); } } @@ -258,7 +270,7 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0); if (interface_claim_error) { - LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", interface_claim_error); + LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error); libusb_close(usb_adapter_handle); usb_adapter_handle = nullptr; return false; diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 4a8e2644c..161d522ac 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -36,12 +36,7 @@ enum class PadButton { PAD_STICK = 0x2000, }; -/// Used to loop through the and assign button in poller -static constexpr std::array 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}; +extern const std::array PadButtonArray; enum class PadAxes : u8 { StickX, diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index a04c507b8..385ce8430 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -14,7 +14,7 @@ namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) + explicit GCButton(int port_, int button_, GCAdapter::Adapter* adapter) : port(port_), button(button_), gcadapter(adapter) {} ~GCButton() override; @@ -45,7 +45,9 @@ public: bool GetStatus() const override { const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; if (trigger_if_greater) { - return axis_value > threshold; // TODO(ameerj) : Fix threshold. + // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently + // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick + return axis_value > threshold; } return axis_value < -threshold; } @@ -71,8 +73,7 @@ std::unique_ptr GCButtonFactory::Create(const Common::Param // button is not an axis/stick button if (button_id != PAD_STICK_ID) { - std::unique_ptr button = - std::make_unique(port, button_id, params.Get("axis", 0), adapter.get()); + auto button = std::make_unique(port, button_id, adapter.get()); return std::move(button); } -- cgit v1.2.3 From d00972fce1fe5f2eb13c7e5d7e4e56036cb6bc91 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sat, 4 Jul 2020 00:40:48 -0400 Subject: Fix for always firing triggers on some controllers, trigger threshold more universal --- src/input_common/gcadapter/gc_adapter.cpp | 6 ++---- src/input_common/gcadapter/gc_adapter.h | 4 +++- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/input_common/gcadapter') diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index f58b0e11c..b39d2a3fb 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -156,14 +156,12 @@ void Adapter::Read() { pads[port].axis_value = pads[port].substick_y; pad_queue[port].Push(pads[port]); } - if (pads[port].trigger_left > pads[port].TRIGGER_CENTER + pads[port].THRESHOLD || - pads[port].trigger_left < pads[port].TRIGGER_CENTER - pads[port].THRESHOLD) { + if (pads[port].trigger_left > pads[port].TRIGGER_THRESHOLD) { pads[port].axis = GCAdapter::PadAxes::TriggerLeft; pads[port].axis_value = pads[port].trigger_left; pad_queue[port].Push(pads[port]); } - if (pads[port].trigger_right > pads[port].TRIGGER_CENTER + pads[port].THRESHOLD || - pads[port].trigger_right < pads[port].TRIGGER_CENTER - pads[port].THRESHOLD) { + if (pads[port].trigger_right > pads[port].TRIGGER_THRESHOLD) { pads[port].axis = GCAdapter::PadAxes::TriggerRight; pads[port].axis_value = pads[port].trigger_right; pad_queue[port].Push(pads[port]); diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 161d522ac..0ea6263eb 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -63,9 +63,11 @@ struct GCPadStatus { static constexpr u8 C_STICK_CENTER_X = 0x80; static constexpr u8 C_STICK_CENTER_Y = 0x80; static constexpr u8 C_STICK_RADIUS = 0x7f; - static constexpr u8 TRIGGER_CENTER = 20; static constexpr u8 THRESHOLD = 10; + // 256/4, at least a quarter press to count as a press. For polling mostly + static constexpr u8 TRIGGER_THRESHOLD = 64; + u8 port{}; PadAxes axis{PadAxes::Undefined}; u8 axis_value{255}; -- cgit v1.2.3