2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

shared_ptr for the GC adapter class, constexpr constants

This commit is contained in:
Ameer 2020-06-21 21:50:58 -04:00
parent 968d631aa5
commit 46b4461fbb
5 changed files with 55 additions and 69 deletions

View file

@ -6,7 +6,6 @@
#include "input_common/gcadapter/gc_adapter.h" #include "input_common/gcadapter/gc_adapter.h"
namespace GCAdapter { namespace GCAdapter {
Adapter* Adapter::adapter_instance{nullptr};
Adapter::Adapter() { Adapter::Adapter() {
if (usb_adapter_handle != nullptr) { if (usb_adapter_handle != nullptr) {
@ -20,13 +19,6 @@ Adapter::Adapter() {
StartScanThread(); StartScanThread();
} }
Adapter* Adapter::GetInstance() {
if (!adapter_instance) {
adapter_instance = new Adapter;
}
return adapter_instance;
}
GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) {
GCPadStatus pad = {}; GCPadStatus pad = {};
bool get_origin = false; bool get_origin = false;
@ -151,34 +143,26 @@ void Adapter::Read() {
} }
// Accounting for a threshold here because of some controller variance // Accounting for a threshold here because of some controller variance
if (pad[port].stick_x > if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD ||
pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD || pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) {
pad[port].stick_x <
pad_constants.MAIN_STICK_CENTER_X - pad_constants.THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::StickX; pad[port].axis = GCAdapter::PadAxes::StickX;
pad[port].axis_value = pad[port].stick_x; pad[port].axis_value = pad[port].stick_x;
pad_queue[port].Push(pad[port]); pad_queue[port].Push(pad[port]);
} }
if (pad[port].stick_y > if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD ||
pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD || pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) {
pad[port].stick_y <
pad_constants.MAIN_STICK_CENTER_Y - pad_constants.THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::StickY; pad[port].axis = GCAdapter::PadAxes::StickY;
pad[port].axis_value = pad[port].stick_y; pad[port].axis_value = pad[port].stick_y;
pad_queue[port].Push(pad[port]); pad_queue[port].Push(pad[port]);
} }
if (pad[port].substick_x > if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD ||
pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD || pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) {
pad[port].substick_x <
pad_constants.C_STICK_CENTER_X - pad_constants.THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::SubstickX; pad[port].axis = GCAdapter::PadAxes::SubstickX;
pad[port].axis_value = pad[port].substick_x; pad[port].axis_value = pad[port].substick_x;
pad_queue[port].Push(pad[port]); pad_queue[port].Push(pad[port]);
} }
if (pad[port].substick_y > if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD ||
pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD || pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) {
pad[port].substick_y <
pad_constants.C_STICK_CENTER_Y - pad_constants.THRESHOLD) {
pad[port].axis = GCAdapter::PadAxes::SubstickY; pad[port].axis = GCAdapter::PadAxes::SubstickY;
pad[port].axis_value = pad[port].substick_y; pad[port].axis_value = pad[port].substick_y;
pad_queue[port].Push(pad[port]); pad_queue[port].Push(pad[port]);
@ -367,8 +351,16 @@ std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
return pad_queue; return pad_queue;
} }
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
return pad_queue;
}
std::array<GCState, 4>& Adapter::GetPadState() { std::array<GCState, 4>& Adapter::GetPadState() {
return state; return state;
} }
const std::array<GCState, 4>& Adapter::GetPadState() const {
return state;
}
} // end of namespace GCAdapter } // end of namespace GCAdapter

View file

@ -46,17 +46,6 @@ enum class PadAxes : u8 {
TriggerRight, TriggerRight,
Undefined, 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
@ -67,6 +56,15 @@ struct GCPadStatus {
u8 trigger_left; // 0 <= trigger_left <= 255 u8 trigger_left; // 0 <= trigger_left <= 255
u8 trigger_right; // 0 <= trigger_right <= 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; u8 port;
PadAxes axis = PadAxes::Undefined; PadAxes axis = PadAxes::Undefined;
u8 axis_value = 255; u8 axis_value = 255;
@ -87,28 +85,22 @@ enum {
/// Singleton Adapter class /// Singleton Adapter class
class Adapter { class Adapter {
public: 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();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const;
std::array<GCState, 4>& GetPadState() const;
private:
/// Singleton instance.
static Adapter* adapter_instance;
/// Initialize the GC Adapter capture and read sequence /// Initialize the GC Adapter capture and read sequence
Adapter(); Adapter();
/// Close the adapter read thread and release the adapter /// Close the adapter read thread and release the adapter
~Adapter(); ~Adapter();
/// Used for polling
void BeginConfiguration();
void EndConfiguration();
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const;
std::array<GCState, 4>& GetPadState();
const std::array<GCState, 4>& GetPadState() const;
private:
GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
void PadToState(GCPadStatus pad, GCState& state); void PadToState(GCPadStatus pad, GCState& state);

View file

@ -14,7 +14,8 @@ namespace InputCommon {
class GCButton final : public Input::ButtonDevice { class GCButton final : public Input::ButtonDevice {
public: public:
explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) explicit GCButton(int port_, int button_, int axis_,
std::shared_ptr<GCAdapter::Adapter> adapter)
: port(port_), button(button_), gcadapter(adapter) {} : port(port_), button(button_), gcadapter(adapter) {}
~GCButton() override; ~GCButton() override;
@ -26,13 +27,13 @@ public:
private: private:
const int port; const int port;
const int button; const int button;
GCAdapter::Adapter* gcadapter; std::shared_ptr<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_,
GCAdapter::Adapter* adapter) std::shared_ptr<GCAdapter::Adapter> adapter)
: port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
gcadapter(adapter) {} gcadapter(adapter) {}
@ -49,12 +50,11 @@ private:
const int axis; const int axis;
float threshold; float threshold;
bool trigger_if_greater; bool trigger_if_greater;
GCAdapter::Adapter* gcadapter; std::shared_ptr<GCAdapter::Adapter> gcadapter;
}; };
GCButtonFactory::GCButtonFactory() { GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
adapter = GCAdapter::Adapter::GetInstance(); : adapter(adapter_) {}
}
GCButton::~GCButton() = default; GCButton::~GCButton() = default;
@ -171,7 +171,8 @@ void GCButtonFactory::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_, GCAdapter::Adapter* adapter) GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_,
std::shared_ptr<GCAdapter::Adapter> adapter)
: port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {}
float GetAxis(int axis) const { float GetAxis(int axis) const {
@ -230,13 +231,12 @@ 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; std::shared_ptr<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(std::shared_ptr<GCAdapter::Adapter> adapter_)
adapter = GCAdapter::Adapter::GetInstance(); : adapter(adapter_) {}
};
/** /**
* Creates analog device from joystick axes * Creates analog device from joystick axes

View file

@ -15,7 +15,7 @@ namespace InputCommon {
*/ */
class GCButtonFactory final : public Input::Factory<Input::ButtonDevice> { class GCButtonFactory final : public Input::Factory<Input::ButtonDevice> {
public: public:
GCButtonFactory(); GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_);
/** /**
* Creates a button device from a button press * Creates a button device from a button press
@ -35,14 +35,14 @@ public:
} }
private: private:
GCAdapter::Adapter* adapter; std::shared_ptr<GCAdapter::Adapter> adapter;
bool polling = false; bool polling = false;
}; };
/// An analog device factory that creates analog devices from GC Adapter /// An analog device factory that creates analog devices from GC Adapter
class GCAnalogFactory final : public Input::Factory<Input::AnalogDevice> { class GCAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
public: public:
GCAnalogFactory(); GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_);
std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override; std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override;
Common::ParamPackage GetNextInput(); Common::ParamPackage GetNextInput();
@ -55,7 +55,7 @@ public:
} }
private: private:
GCAdapter::Adapter* adapter; std::shared_ptr<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;

View file

@ -25,13 +25,15 @@ static std::shared_ptr<MotionEmu> motion_emu;
static std::unique_ptr<SDL::State> sdl; static std::unique_ptr<SDL::State> sdl;
#endif #endif
static std::unique_ptr<CemuhookUDP::State> udp; static std::unique_ptr<CemuhookUDP::State> udp;
static std::shared_ptr<GCAdapter::Adapter> gcadapter;
static std::shared_ptr<GCButtonFactory> gcbuttons; static std::shared_ptr<GCButtonFactory> gcbuttons;
static std::shared_ptr<GCAnalogFactory> gcanalog; static std::shared_ptr<GCAnalogFactory> gcanalog;
void Init() { void Init() {
gcbuttons = std::make_shared<GCButtonFactory>(); gcadapter = std::make_shared<GCAdapter::Adapter>();
gcbuttons = std::make_shared<GCButtonFactory>(gcadapter);
Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons); Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons);
gcanalog = std::make_shared<GCAnalogFactory>(); gcanalog = std::make_shared<GCAnalogFactory>(gcadapter);
Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog); Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog);
keyboard = std::make_shared<Keyboard>(); keyboard = std::make_shared<Keyboard>();