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}")
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.
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

View file

@ -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<GCPadStatus> 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};
Adapter::Adapter() {
if (usb_adapter_handle != nullptr) {
return;
}
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;
static bool adapter_thread_running;
StartScanThread();
}
static std::mutex initialization_mutex;
static std::thread detect_thread;
static bool detect_thread_running = false;
Adapter* Adapter::GetInstance() {
if (!adapter_instance) {
adapter_instance = new Adapter;
}
return adapter_instance;
}
static libusb_context* libusb_ctx;
static u8 input_endpoint = 0;
static bool configuring = false;
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<u8>(PadAxes::StickX), pad.stick_x);
state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickY), pad.stick_y);
state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickX), pad.substick_x);
state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickY), pad.substick_y);
state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerLeft), pad.trigger_left);
state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerRight), pad.trigger_right);
}
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<std::mutex> 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<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
return pad_queue;
}
std::array<GCState, 4>& Adapter::GetPadState() {
return state;
}
} // end of namespace GCAdapter

View file

@ -8,6 +8,9 @@
#include <mutex>
#include <libusb.h>
#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};
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 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;
PadAxes axis = PadAxes::Undefined;
u8 axis_value = 255;
};
@ -64,51 +76,88 @@ struct GCState {
std::unordered_map<int, u16> 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();
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
/// Initialize the GC Adapter capture and read sequence
void Init();
/// Used for polling
void BeginConfiguration();
void EndConfiguration();
/// Close the adapter read thread and release the adapter
void Shutdown();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
std::array<GCState, 4>& GetPadState();
/// Begin scanning for the GC Adapter.
void StartScanThread();
private:
/// Singleton instance.
static Adapter* adapter_instance;
/// Stop scanning for the adapter
void StopScanThread();
/// Initialize the GC Adapter capture and read sequence
Adapter();
/// Returns true if there is a device connected to port
bool DeviceConnected(int port);
/// Close the adapter read thread and release the adapter
~Adapter();
/// Resets status of device connected to port
void ResetDeviceType(int port);
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
/// Returns true if we successfully gain access to GC Adapter
bool CheckDeviceAccess(libusb_device* device);
void PadToState(GCPadStatus pad, GCState& state);
/// Captures GC Adapter endpoint address,
void GetGCEndpoint(libusb_device* device);
void Read();
void ScanThreadFunc();
/// Begin scanning for the GC Adapter.
void StartScanThread();
/// For shutting down, clear all data, join all threads, release usb
void Reset();
/// Stop scanning for the adapter
void StopScanThread();
/// For use in initialization, querying devices to find the adapter
void Setup();
/// Returns true if there is a device connected to port
bool DeviceConnected(int port);
/// Used for polling
void BeginConfiguration();
/// Resets status of device connected to port
void ResetDeviceType(int port);
void EndConfiguration();
/// 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();
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<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;
std::array<GCState, 4> state;
};
} // end of namespace GCAdapter

View file

@ -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<GCPadStatus> 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<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;
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::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);
}
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<u8>(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<float, float> 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<Input::AnalogDevice> 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<GCAnalog>(port, axis_x, axis_y, deadzone);
return std::make_unique<GCAnalog>(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<u8>(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

View file

@ -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

View file

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

View file

@ -32,10 +32,8 @@ class MotionEmu;
/// Gets the motion emulation factory.
MotionEmu* GetMotionEmu();
class GCButtonFactory;
class GCAnalogFactory;
GCButtonFactory* GetGCButtons();
GCAnalogFactory* GetGCAnalogs();
/// 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 {
failure_callback();
}
}).detach();
})
.detach();
}
CalibrationConfigurationJob::CalibrationConfigurationJob(
@ -279,7 +280,8 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
complete_event.Wait();
socket.Stop();
worker_thread.join();
}).detach();
})
.detach();
}
CalibrationConfigurationJob::~CalibrationConfigurationJob() {

View file

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