yuzu-emu
/
yuzu
Archived
1
0
Fork 0

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
This commit is contained in:
Ameer 2020-06-21 18:43:01 -04:00
parent c94583d867
commit 121af3646d
9 changed files with 288 additions and 227 deletions

View File

@ -328,6 +328,12 @@ elseif(SDL2_FOUND)
target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}") target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}")
endif() endif()
# Ensure libusb is properly configured (based on dolphin libusb include)
find_package(LibUSB)
add_subdirectory(externals/libusb)
set(LIBUSB_LIBRARIES usb)
# Prefer the -pthread flag on Linux. # Prefer the -pthread flag on Linux.
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)

View File

@ -3,45 +3,41 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/threadsafe_queue.h"
#include "input_common/gcadapter/gc_adapter.h" #include "input_common/gcadapter/gc_adapter.h"
Common::SPSCQueue<GCPadStatus> pad_queue[4];
struct GCState state[4];
namespace GCAdapter { namespace GCAdapter {
Adapter* Adapter::adapter_instance{nullptr};
static libusb_device_handle* usb_adapter_handle = nullptr; Adapter::Adapter() {
static u8 adapter_controllers_status[4] = { if (usb_adapter_handle != nullptr) {
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, return;
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; }
LOG_INFO(Input, "GC Adapter Initialization started");
static std::mutex s_mutex; current_status = NO_ADAPTER_DETECTED;
libusb_init(&libusb_ctx);
static std::thread adapter_input_thread; StartScanThread();
static bool adapter_thread_running; }
static std::mutex initialization_mutex; Adapter* Adapter::GetInstance() {
static std::thread detect_thread; if (!adapter_instance) {
static bool detect_thread_running = false; adapter_instance = new Adapter;
}
return adapter_instance;
}
static libusb_context* libusb_ctx; GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) {
static u8 input_endpoint = 0;
static bool configuring = false;
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) {
GCPadStatus pad = {}; GCPadStatus pad = {};
bool get_origin = false; bool get_origin = false;
u8 type = adapter_payload[1 + (9 * port)] >> 4; ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4);
if (type) if (type != ControllerTypes::None)
get_origin = true; get_origin = true;
adapter_controllers_status[port] = type; 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 b1 = adapter_payload[1 + (9 * port) + 1];
u8 b2 = adapter_payload[1 + (9 * port) + 2]; 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.button |= PAD_GET_ORIGIN;
} }
pad.stickX = adapter_payload[1 + (9 * port) + 3]; pad.stick_x = adapter_payload[1 + (9 * port) + 3];
pad.stickY = adapter_payload[1 + (9 * port) + 4]; pad.stick_y = adapter_payload[1 + (9 * port) + 4];
pad.substickX = adapter_payload[1 + (9 * port) + 5]; pad.substick_x = adapter_payload[1 + (9 * port) + 5];
pad.substickY = adapter_payload[1 + (9 * port) + 6]; pad.substick_y = adapter_payload[1 + (9 * port) + 6];
pad.triggerLeft = adapter_payload[1 + (9 * port) + 7]; pad.trigger_left = adapter_payload[1 + (9 * port) + 7];
pad.triggerRight = adapter_payload[1 + (9 * port) + 8]; pad.trigger_right = adapter_payload[1 + (9 * port) + 8];
} }
return pad; return pad;
} }
void PadToState(GCPadStatus pad, GCState& state) { void Adapter::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_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_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_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_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_L, pad.button & PAD_TRIGGER_L);
state.buttons.insert_or_assign(PAD_TRIGGER_R, pad.button & PAD_TRIGGER_R); 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(static_cast<u8>(PadAxes::StickX), pad.stick_x);
state.axes.insert_or_assign(STICK_Y, pad.stickY); state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickY), pad.stick_y);
state.axes.insert_or_assign(SUBSTICK_X, pad.substickX); state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickX), pad.substick_x);
state.axes.insert_or_assign(SUBSTICK_Y, pad.substickY); state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickY), pad.substick_y);
state.axes.insert_or_assign(TRIGGER_LEFT, pad.triggerLeft); state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerLeft), pad.trigger_left);
state.axes.insert_or_assign(TRIGGER_RIGHT, pad.triggerRight); state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerRight), pad.trigger_right);
} }
static void Read() { void Adapter::Read() {
LOG_INFO(Input, "GC Adapter Read() thread started"); LOG_INFO(Input, "GC Adapter Read() thread started");
int payload_size_in; int payload_size_in;
@ -145,8 +140,9 @@ static void Read() {
LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size,
controller_payload_copy[0]); controller_payload_copy[0]);
} else { } else {
for (int i = 0; i < 4; i++) for (int port = 0; port < 4; port++) {
pad[i] = CheckStatus(i, controller_payload_copy); pad[port] = CheckStatus(port, controller_payload_copy);
}
} }
for (int port = 0; port < 4; port++) { for (int port = 0; port < 4; port++) {
if (DeviceConnected(port) && configuring) { if (DeviceConnected(port) && configuring) {
@ -155,28 +151,36 @@ static void Read() {
} }
// Accounting for a threshold here because of some controller variance // Accounting for a threshold here because of some controller variance
if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || if (pad[port].stick_x >
pad[port].stickX < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD ||
pad[port].axis_which = STICK_X; pad[port].stick_x <
pad[port].axis_value = pad[port].stickX; 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]); pad_queue[port].Push(pad[port]);
} }
if (pad[port].stickY > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || if (pad[port].stick_y >
pad[port].stickY < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD ||
pad[port].axis_which = STICK_Y; pad[port].stick_y <
pad[port].axis_value = pad[port].stickY; 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]); pad_queue[port].Push(pad[port]);
} }
if (pad[port].substickX > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || if (pad[port].substick_x >
pad[port].substickX < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD ||
pad[port].axis_which = SUBSTICK_X; pad[port].substick_x <
pad[port].axis_value = pad[port].substickX; 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]); pad_queue[port].Push(pad[port]);
} }
if (pad[port].substickY > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || if (pad[port].substick_y >
pad[port].substickY < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD ||
pad[port].axis_which = SUBSTICK_Y; pad[port].substick_y <
pad[port].axis_value = pad[port].substickY; 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]); 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"); LOG_INFO(Input, "GC Adapter scanning thread started");
while (detect_thread_running) { while (detect_thread_running) {
@ -198,20 +202,7 @@ static void ScanThreadFunc() {
} }
} }
void Init() { void Adapter::StartScanThread() {
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) { if (detect_thread_running) {
return; return;
} }
@ -220,21 +211,21 @@ void StartScanThread() {
} }
detect_thread_running = true; detect_thread_running = true;
detect_thread = std::thread(ScanThreadFunc); detect_thread = std::thread([=] { ScanThreadFunc(); });
} }
void StopScanThread() { void Adapter::StopScanThread() {
detect_thread.join(); detect_thread.join();
} }
static void Setup() { void Adapter::Setup() {
// Reset the error status in case the adapter gets unplugged // Reset the error status in case the adapter gets unplugged
if (current_status < 0) { if (current_status < 0) {
current_status = NO_ADAPTER_DETECTED; 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; adapter_controllers_status[i] = ControllerTypes::None;
} }
libusb_device** devs; // pointer to list of connected usb devices 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; libusb_device_descriptor desc;
int ret = libusb_get_device_descriptor(device, &desc); int ret = libusb_get_device_descriptor(device, &desc);
if (ret) { if (ret) {
@ -300,7 +291,7 @@ static bool CheckDeviceAccess(libusb_device* device) {
return true; return true;
} }
static void GetGCEndpoint(libusb_device* device) { void Adapter::GetGCEndpoint(libusb_device* device) {
libusb_config_descriptor* config = nullptr; libusb_config_descriptor* config = nullptr;
libusb_get_config_descriptor(device, 0, &config); libusb_get_config_descriptor(device, 0, &config);
for (u8 ic = 0; ic < config->bNumInterfaces; ic++) { for (u8 ic = 0; ic < config->bNumInterfaces; ic++) {
@ -318,18 +309,17 @@ static void GetGCEndpoint(libusb_device* device) {
adapter_thread_running = true; adapter_thread_running = true;
current_status = ADAPTER_DETECTED; 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(); StopScanThread();
Reset(); Reset();
current_status = NO_ADAPTER_DETECTED; current_status = NO_ADAPTER_DETECTED;
} }
static void Reset() { void Adapter::Reset() {
std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock); std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock);
if (!lock.try_lock()) { if (!lock.try_lock()) {
return; return;
@ -343,7 +333,7 @@ static void Reset() {
} }
for (int i = 0; i < 4; i++) { 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; current_status = NO_ADAPTER_DETECTED;
@ -355,20 +345,28 @@ static void Reset() {
} }
} }
bool DeviceConnected(int port) { bool Adapter::DeviceConnected(int port) {
return adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE; return adapter_controllers_status[port] != ControllerTypes::None;
} }
void ResetDeviceType(int port) { void Adapter::ResetDeviceType(int port) {
adapter_controllers_status[port] = ControllerTypes::CONTROLLER_NONE; adapter_controllers_status[port] = ControllerTypes::None;
} }
void BeginConfiguration() { void Adapter::BeginConfiguration() {
configuring = true; configuring = true;
} }
void EndConfiguration() { void Adapter::EndConfiguration() {
configuring = false; configuring = false;
} }
std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
return pad_queue;
}
std::array<GCState, 4>& Adapter::GetPadState() {
return state;
}
} // end of namespace GCAdapter } // end of namespace GCAdapter

View File

@ -8,6 +8,9 @@
#include <mutex> #include <mutex>
#include <libusb.h> #include <libusb.h>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/threadsafe_queue.h"
namespace GCAdapter {
enum { enum {
PAD_USE_ORIGIN = 0x0080, 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 { struct GCPadStatus {
u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
u8 stickX; // 0 <= stickX <= 255 u8 stick_x; // 0 <= stick_x <= 255
u8 stickY; // 0 <= stickY <= 255 u8 stick_y; // 0 <= stick_y <= 255
u8 substickX; // 0 <= substickX <= 255 u8 substick_x; // 0 <= substick_x <= 255
u8 substickY; // 0 <= substickY <= 255 u8 substick_y; // 0 <= substick_y <= 255
u8 triggerLeft; // 0 <= triggerLeft <= 255 u8 trigger_left; // 0 <= trigger_left <= 255
u8 triggerRight; // 0 <= triggerRight <= 255 u8 trigger_right; // 0 <= trigger_right <= 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 port;
u8 axis_which = 255; PadAxes axis = PadAxes::Undefined;
u8 axis_value = 255; u8 axis_value = 255;
}; };
@ -64,24 +76,42 @@ struct GCState {
std::unordered_map<int, u16> axes; std::unordered_map<int, u16> axes;
}; };
namespace GCAdapter { enum class ControllerTypes { None, Wired, Wireless };
enum ControllerTypes { CONTROLLER_NONE = 0, CONTROLLER_WIRED = 1, CONTROLLER_WIRELESS = 2 };
enum { enum {
NO_ADAPTER_DETECTED = 0, NO_ADAPTER_DETECTED = 0,
ADAPTER_DETECTED = 1, ADAPTER_DETECTED = 1,
}; };
// Current adapter status: detected/not detected/in error (holds the error code) /// Singleton Adapter class
static int current_status = NO_ADAPTER_DETECTED; class Adapter {
public:
/// For retreiving the singleton instance
static Adapter* GetInstance();
/// Used for polling
void BeginConfiguration();
void EndConfiguration();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
std::array<GCState, 4>& GetPadState();
private:
/// Singleton instance.
static Adapter* adapter_instance;
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
/// Initialize the GC Adapter capture and read sequence /// Initialize the GC Adapter capture and read sequence
void Init(); Adapter();
/// Close the adapter read thread and release the adapter /// Close the adapter read thread and release the adapter
void Shutdown(); ~Adapter();
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
void PadToState(GCPadStatus pad, GCState& state);
void Read();
void ScanThreadFunc();
/// Begin scanning for the GC Adapter. /// Begin scanning for the GC Adapter.
void StartScanThread(); void StartScanThread();
@ -106,9 +136,28 @@ void Reset();
/// For use in initialization, querying devices to find the adapter /// For use in initialization, querying devices to find the adapter
void Setup(); void Setup();
/// Used for polling int current_status = NO_ADAPTER_DETECTED;
void BeginConfiguration(); libusb_device_handle* usb_adapter_handle = nullptr;
ControllerTypes adapter_controllers_status[4] = {ControllerTypes::None, ControllerTypes::None,
ControllerTypes::None, ControllerTypes::None};
void EndConfiguration(); 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<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;
std::array<GCState, 4> state;
};
} // end of namespace GCAdapter } // end of namespace GCAdapter

View File

@ -10,35 +10,34 @@
#include "input_common/gcadapter/gc_adapter.h" #include "input_common/gcadapter/gc_adapter.h"
#include "input_common/gcadapter/gc_poller.h" #include "input_common/gcadapter/gc_poller.h"
// Using extern as to avoid multply defined symbols.
extern Common::SPSCQueue<GCPadStatus> pad_queue[4];
extern struct GCState state[4];
namespace InputCommon { namespace InputCommon {
class GCButton final : public Input::ButtonDevice { class GCButton final : public Input::ButtonDevice {
public: 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; ~GCButton() override;
bool GetStatus() const override { bool GetStatus() const override {
return state[port].buttons.at(button); return gcadapter->GetPadState()[port].buttons.at(button);
} }
private: private:
const int port; const int port;
const int button; const int button;
GCAdapter::Adapter* gcadapter;
}; };
class GCAxisButton final : public Input::ButtonDevice { class GCAxisButton final : public Input::ButtonDevice {
public: public:
explicit GCAxisButton(int port_, int axis_, float threshold_, bool 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_) { GCAdapter::Adapter* adapter)
} : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
gcadapter(adapter) {}
bool GetStatus() const override { 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) { if (trigger_if_greater) {
return axis_value > 0.10f; // TODO(ameerj) : Fix threshold. return axis_value > 0.10f; // TODO(ameerj) : Fix threshold.
} }
@ -50,14 +49,15 @@ private:
const int axis; const int axis;
float threshold; float threshold;
bool trigger_if_greater; bool trigger_if_greater;
GCAdapter::Adapter* gcadapter;
}; };
GCButtonFactory::GCButtonFactory() { GCButtonFactory::GCButtonFactory() {
GCAdapter::Init(); adapter = GCAdapter::Adapter::GetInstance();
} }
GCButton::~GCButton() { GCButton::~GCButton() {
GCAdapter::Shutdown(); // GCAdapter::Shutdown();
} }
std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) {
@ -77,76 +77,76 @@ std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::Param
trigger_if_greater = true; trigger_if_greater = true;
LOG_ERROR(Input, "Unknown direction {}", direction_name); LOG_ERROR(Input, "Unknown direction {}", direction_name);
} }
return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater); return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, adapter);
} }
std::unique_ptr<GCButton> button = std::unique_ptr<GCButton> button =
std::make_unique<GCButton>(port, button_id, params.Get("axis", 0)); std::make_unique<GCButton>(port, button_id, params.Get("axis", 0), adapter);
return std::move(button); return std::move(button);
} }
Common::ParamPackage GCButtonFactory::GetNextInput() { Common::ParamPackage GCButtonFactory::GetNextInput() {
Common::ParamPackage params; Common::ParamPackage params;
GCPadStatus pad; GCAdapter::GCPadStatus pad;
for (int i = 0; i < 4; i++) { 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 // This while loop will break on the earliest detected button
params.Set("engine", "gcpad"); params.Set("engine", "gcpad");
params.Set("port", i); params.Set("port", i);
// I was debating whether to keep these verbose for ease of reading // 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. // or to use a while loop shifting the bits to test and set the value.
if (pad.button & PAD_BUTTON_A) { if (pad.button & GCAdapter::PAD_BUTTON_A) {
params.Set("button", PAD_BUTTON_A); params.Set("button", GCAdapter::PAD_BUTTON_A);
break; break;
} }
if (pad.button & PAD_BUTTON_B) { if (pad.button & GCAdapter::PAD_BUTTON_B) {
params.Set("button", PAD_BUTTON_B); params.Set("button", GCAdapter::PAD_BUTTON_B);
break; break;
} }
if (pad.button & PAD_BUTTON_X) { if (pad.button & GCAdapter::PAD_BUTTON_X) {
params.Set("button", PAD_BUTTON_X); params.Set("button", GCAdapter::PAD_BUTTON_X);
break; break;
} }
if (pad.button & PAD_BUTTON_Y) { if (pad.button & GCAdapter::PAD_BUTTON_Y) {
params.Set("button", PAD_BUTTON_Y); params.Set("button", GCAdapter::PAD_BUTTON_Y);
break; break;
} }
if (pad.button & PAD_BUTTON_DOWN) { if (pad.button & GCAdapter::PAD_BUTTON_DOWN) {
params.Set("button", PAD_BUTTON_DOWN); params.Set("button", GCAdapter::PAD_BUTTON_DOWN);
break; break;
} }
if (pad.button & PAD_BUTTON_LEFT) { if (pad.button & GCAdapter::PAD_BUTTON_LEFT) {
params.Set("button", PAD_BUTTON_LEFT); params.Set("button", GCAdapter::PAD_BUTTON_LEFT);
break; break;
} }
if (pad.button & PAD_BUTTON_RIGHT) { if (pad.button & GCAdapter::PAD_BUTTON_RIGHT) {
params.Set("button", PAD_BUTTON_RIGHT); params.Set("button", GCAdapter::PAD_BUTTON_RIGHT);
break; break;
} }
if (pad.button & PAD_BUTTON_UP) { if (pad.button & GCAdapter::PAD_BUTTON_UP) {
params.Set("button", PAD_BUTTON_UP); params.Set("button", GCAdapter::PAD_BUTTON_UP);
break; break;
} }
if (pad.button & PAD_TRIGGER_L) { if (pad.button & GCAdapter::PAD_TRIGGER_L) {
params.Set("button", PAD_TRIGGER_L); params.Set("button", GCAdapter::PAD_TRIGGER_L);
break; break;
} }
if (pad.button & PAD_TRIGGER_R) { if (pad.button & GCAdapter::PAD_TRIGGER_R) {
params.Set("button", PAD_TRIGGER_R); params.Set("button", GCAdapter::PAD_TRIGGER_R);
break; break;
} }
if (pad.button & PAD_TRIGGER_Z) { if (pad.button & GCAdapter::PAD_TRIGGER_Z) {
params.Set("button", PAD_TRIGGER_Z); params.Set("button", GCAdapter::PAD_TRIGGER_Z);
break; break;
} }
if (pad.button & PAD_BUTTON_START) { if (pad.button & GCAdapter::PAD_BUTTON_START) {
params.Set("button", PAD_BUTTON_START); params.Set("button", GCAdapter::PAD_BUTTON_START);
break; break;
} }
// For Axis button implementation // For Axis button implementation
if (pad.axis_which != 255) { if (pad.axis != GCAdapter::PadAxes::Undefined) {
params.Set("axis", pad.axis_which); params.Set("axis", static_cast<u8>(pad.axis));
params.Set("button", PAD_STICK); params.Set("button", GCAdapter::PAD_STICK);
if (pad.axis_value > 128) { if (pad.axis_value > 128) {
params.Set("direction", "+"); params.Set("direction", "+");
params.Set("threshold", "0.5"); params.Set("threshold", "0.5");
@ -164,30 +164,30 @@ Common::ParamPackage GCButtonFactory::GetNextInput() {
void GCButtonFactory::BeginConfiguration() { void GCButtonFactory::BeginConfiguration() {
polling = true; polling = true;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
pad_queue[i].Clear(); adapter->GetPadQueue()[i].Clear();
} }
GCAdapter::BeginConfiguration(); adapter->BeginConfiguration();
} }
void GCButtonFactory::EndConfiguration() { void GCButtonFactory::EndConfiguration() {
polling = false; polling = false;
for (int i = 0; i < 4; i++) { 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 { class GCAnalog final : public Input::AnalogDevice {
public: public:
GCAnalog(int port_, int axis_x_, int axis_y_, float 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_) {} : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {}
float GetAxis(int axis) const { float GetAxis(int axis) const {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
// division is not by a perfect 128 to account for some variance in center location // 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 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range
// [20-230] // [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<float, float> GetAnalog(int axis_x, int axis_y) const { std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const {
@ -238,10 +238,13 @@ private:
const int axis_y; const int axis_y;
const float deadzone; const float deadzone;
mutable std::mutex mutex; mutable std::mutex mutex;
GCAdapter::Adapter* gcadapter;
}; };
/// An analog device factory that creates analog devices from GC Adapter /// 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 * Creates analog device from joystick axes
@ -257,35 +260,36 @@ std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::Param
const int axis_y = params.Get("axis_y", 1); const int axis_y = params.Get("axis_y", 1);
const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f);
return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone); return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter);
} }
void GCAnalogFactory::BeginConfiguration() { void GCAnalogFactory::BeginConfiguration() {
polling = true; polling = true;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
pad_queue[i].Clear(); adapter->GetPadQueue()[i].Clear();
} }
GCAdapter::BeginConfiguration(); adapter->BeginConfiguration();
} }
void GCAnalogFactory::EndConfiguration() { void GCAnalogFactory::EndConfiguration() {
polling = false; polling = false;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
pad_queue[i].Clear(); adapter->GetPadQueue()[i].Clear();
} }
GCAdapter::EndConfiguration(); adapter->EndConfiguration();
} }
Common::ParamPackage GCAnalogFactory::GetNextInput() { Common::ParamPackage GCAnalogFactory::GetNextInput() {
GCPadStatus pad; GCAdapter::GCPadStatus pad;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
while (pad_queue[i].Pop(pad)) { while (adapter->GetPadQueue()[i].Pop(pad)) {
if (pad.axis_which == 255 || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { if (pad.axis == GCAdapter::PadAxes::Undefined ||
std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) {
continue; continue;
} }
// An analog device needs two axes, so we need to store the axis for later and wait for // 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 SDL event. The axes also must be from the same joystick.
const int axis = pad.axis_which; const u8 axis = static_cast<u8>(pad.axis);
if (analog_x_axis == -1) { if (analog_x_axis == -1) {
analog_x_axis = axis; analog_x_axis = axis;
controller_number = i; controller_number = i;
@ -307,4 +311,5 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() {
} }
return params; return params;
} }
} // namespace InputCommon } // namespace InputCommon

View File

@ -35,6 +35,7 @@ public:
} }
private: private:
GCAdapter::Adapter* adapter;
bool polling = false; bool polling = false;
}; };
@ -54,9 +55,11 @@ public:
} }
private: private:
GCAdapter::Adapter* adapter;
int analog_x_axis = -1; int analog_x_axis = -1;
int analog_y_axis = -1; int analog_y_axis = -1;
int controller_number = -1; int controller_number = -1;
bool polling = false; bool polling = false;
}; };
} // namespace InputCommon } // namespace InputCommon

View File

@ -2,11 +2,13 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <iostream>
#include <memory> #include <memory>
#include <thread> #include <thread>
#include <libusb.h> #include <libusb.h>
#include "common/param_package.h" #include "common/param_package.h"
#include "input_common/analog_from_button.h" #include "input_common/analog_from_button.h"
#include "input_common/gcadapter/gc_adapter.h"
#include "input_common/gcadapter/gc_poller.h" #include "input_common/gcadapter/gc_poller.h"
#include "input_common/keyboard.h" #include "input_common/keyboard.h"
#include "input_common/main.h" #include "input_common/main.h"

View File

@ -32,10 +32,8 @@ class MotionEmu;
/// Gets the motion emulation factory. /// Gets the motion emulation factory.
MotionEmu* GetMotionEmu(); MotionEmu* GetMotionEmu();
class GCButtonFactory;
class GCAnalogFactory;
GCButtonFactory* GetGCButtons(); GCButtonFactory* GetGCButtons();
GCAnalogFactory* GetGCAnalogs(); GCAnalogFactory* GetGCAnalogs();
/// Generates a serialized param package for creating a keyboard button device /// Generates a serialized param package for creating a keyboard button device

View File

@ -225,7 +225,8 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie
} else { } else {
failure_callback(); failure_callback();
} }
}).detach(); })
.detach();
} }
CalibrationConfigurationJob::CalibrationConfigurationJob( CalibrationConfigurationJob::CalibrationConfigurationJob(
@ -279,7 +280,8 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
complete_event.Wait(); complete_event.Wait();
socket.Stop(); socket.Stop();
worker_thread.join(); worker_thread.join();
}).detach(); })
.detach();
} }
CalibrationConfigurationJob::~CalibrationConfigurationJob() { CalibrationConfigurationJob::~CalibrationConfigurationJob() {

View File

@ -262,8 +262,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
button->setContextMenuPolicy(Qt::CustomContextMenu); button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(button, &QPushButton::clicked, [=] { connect(button, &QPushButton::clicked, [=] {
HandleClick( HandleClick(button_map[button_id],
button_map[button_id],
[=](Common::ParamPackage params) { [=](Common::ParamPackage params) {
// Workaround for ZL & ZR for analog triggers like on XBOX controllors. // Workaround for ZL & ZR for analog triggers like on XBOX controllors.
// Analog triggers (from controllers like the XBOX controller) would not // Analog triggers (from controllers like the XBOX controller) would not
@ -306,8 +305,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
analog_button->setContextMenuPolicy(Qt::CustomContextMenu); analog_button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(analog_button, &QPushButton::clicked, [=]() { connect(analog_button, &QPushButton::clicked, [=]() {
HandleClick( HandleClick(analog_map_buttons[analog_id][sub_button_id],
analog_map_buttons[analog_id][sub_button_id],
[=](const Common::ParamPackage& params) { [=](const Common::ParamPackage& params) {
SetAnalogButton(params, analogs_param[analog_id], SetAnalogButton(params, analogs_param[analog_id],
analog_sub_buttons[sub_button_id]); analog_sub_buttons[sub_button_id]);