summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp40
-rw-r--r--src/input_common/gcadapter/gc_adapter.h40
-rw-r--r--src/input_common/gcadapter/gc_poller.cpp24
-rw-r--r--src/input_common/gcadapter/gc_poller.h8
-rw-r--r--src/input_common/main.cpp6
5 files changed, 52 insertions, 66 deletions
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 @@
6#include "input_common/gcadapter/gc_adapter.h" 6#include "input_common/gcadapter/gc_adapter.h"
7 7
8namespace GCAdapter { 8namespace GCAdapter {
9Adapter* Adapter::adapter_instance{nullptr};
10 9
11Adapter::Adapter() { 10Adapter::Adapter() {
12 if (usb_adapter_handle != nullptr) { 11 if (usb_adapter_handle != nullptr) {
@@ -20,13 +19,6 @@ Adapter::Adapter() {
20 StartScanThread(); 19 StartScanThread();
21} 20}
22 21
23Adapter* Adapter::GetInstance() {
24 if (!adapter_instance) {
25 adapter_instance = new Adapter;
26 }
27 return adapter_instance;
28}
29
30GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { 22GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) {
31 GCPadStatus pad = {}; 23 GCPadStatus pad = {};
32 bool get_origin = false; 24 bool get_origin = false;
@@ -151,34 +143,26 @@ void Adapter::Read() {
151 } 143 }
152 144
153 // Accounting for a threshold here because of some controller variance 145 // Accounting for a threshold here because of some controller variance
154 if (pad[port].stick_x > 146 if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD ||
155 pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD || 147 pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) {
156 pad[port].stick_x <
157 pad_constants.MAIN_STICK_CENTER_X - pad_constants.THRESHOLD) {
158 pad[port].axis = GCAdapter::PadAxes::StickX; 148 pad[port].axis = GCAdapter::PadAxes::StickX;
159 pad[port].axis_value = pad[port].stick_x; 149 pad[port].axis_value = pad[port].stick_x;
160 pad_queue[port].Push(pad[port]); 150 pad_queue[port].Push(pad[port]);
161 } 151 }
162 if (pad[port].stick_y > 152 if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD ||
163 pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD || 153 pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) {
164 pad[port].stick_y <
165 pad_constants.MAIN_STICK_CENTER_Y - pad_constants.THRESHOLD) {
166 pad[port].axis = GCAdapter::PadAxes::StickY; 154 pad[port].axis = GCAdapter::PadAxes::StickY;
167 pad[port].axis_value = pad[port].stick_y; 155 pad[port].axis_value = pad[port].stick_y;
168 pad_queue[port].Push(pad[port]); 156 pad_queue[port].Push(pad[port]);
169 } 157 }
170 if (pad[port].substick_x > 158 if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD ||
171 pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD || 159 pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) {
172 pad[port].substick_x <
173 pad_constants.C_STICK_CENTER_X - pad_constants.THRESHOLD) {
174 pad[port].axis = GCAdapter::PadAxes::SubstickX; 160 pad[port].axis = GCAdapter::PadAxes::SubstickX;
175 pad[port].axis_value = pad[port].substick_x; 161 pad[port].axis_value = pad[port].substick_x;
176 pad_queue[port].Push(pad[port]); 162 pad_queue[port].Push(pad[port]);
177 } 163 }
178 if (pad[port].substick_y > 164 if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD ||
179 pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD || 165 pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) {
180 pad[port].substick_y <
181 pad_constants.C_STICK_CENTER_Y - pad_constants.THRESHOLD) {
182 pad[port].axis = GCAdapter::PadAxes::SubstickY; 166 pad[port].axis = GCAdapter::PadAxes::SubstickY;
183 pad[port].axis_value = pad[port].substick_y; 167 pad[port].axis_value = pad[port].substick_y;
184 pad_queue[port].Push(pad[port]); 168 pad_queue[port].Push(pad[port]);
@@ -367,8 +351,16 @@ std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
367 return pad_queue; 351 return pad_queue;
368} 352}
369 353
354const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
355 return pad_queue;
356}
357
370std::array<GCState, 4>& Adapter::GetPadState() { 358std::array<GCState, 4>& Adapter::GetPadState() {
371 return state; 359 return state;
372} 360}
373 361
362const std::array<GCState, 4>& Adapter::GetPadState() const {
363 return state;
364}
365
374} // end of namespace GCAdapter 366} // 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 {
46 TriggerRight, 46 TriggerRight,
47 Undefined, 47 Undefined,
48}; 48};
49const struct GCPadConstants {
50 const u8 MAIN_STICK_CENTER_X = 0x80;
51 const u8 MAIN_STICK_CENTER_Y = 0x80;
52 const u8 MAIN_STICK_RADIUS = 0x7f;
53 const u8 C_STICK_CENTER_X = 0x80;
54 const u8 C_STICK_CENTER_Y = 0x80;
55 const u8 C_STICK_RADIUS = 0x7f;
56
57 const u8 TRIGGER_CENTER = 20;
58 const u8 THRESHOLD = 10;
59} pad_constants;
60 49
61struct GCPadStatus { 50struct GCPadStatus {
62 u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits 51 u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
@@ -67,6 +56,15 @@ struct GCPadStatus {
67 u8 trigger_left; // 0 <= trigger_left <= 255 56 u8 trigger_left; // 0 <= trigger_left <= 255
68 u8 trigger_right; // 0 <= trigger_right <= 255 57 u8 trigger_right; // 0 <= trigger_right <= 255
69 58
59 static constexpr u8 MAIN_STICK_CENTER_X = 0x80;
60 static constexpr u8 MAIN_STICK_CENTER_Y = 0x80;
61 static constexpr u8 MAIN_STICK_RADIUS = 0x7f;
62 static constexpr u8 C_STICK_CENTER_X = 0x80;
63 static constexpr u8 C_STICK_CENTER_Y = 0x80;
64 static constexpr u8 C_STICK_RADIUS = 0x7f;
65 static constexpr u8 TRIGGER_CENTER = 20;
66 static constexpr u8 THRESHOLD = 10;
67
70 u8 port; 68 u8 port;
71 PadAxes axis = PadAxes::Undefined; 69 PadAxes axis = PadAxes::Undefined;
72 u8 axis_value = 255; 70 u8 axis_value = 255;
@@ -87,28 +85,22 @@ enum {
87/// Singleton Adapter class 85/// Singleton Adapter class
88class Adapter { 86class Adapter {
89public: 87public:
90 /// For retreiving the singleton instance 88 /// Initialize the GC Adapter capture and read sequence
91 static Adapter* GetInstance(); 89 Adapter();
92 90
91 /// Close the adapter read thread and release the adapter
92 ~Adapter();
93 /// Used for polling 93 /// Used for polling
94 void BeginConfiguration(); 94 void BeginConfiguration();
95 void EndConfiguration(); 95 void EndConfiguration();
96 96
97 std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue(); 97 std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
98 const std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const;
99
98 std::array<GCState, 4>& GetPadState(); 100 std::array<GCState, 4>& GetPadState();
99 std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const; 101 const std::array<GCState, 4>& GetPadState() const;
100 std::array<GCState, 4>& GetPadState() const;
101 102
102private: 103private:
103 /// Singleton instance.
104 static Adapter* adapter_instance;
105
106 /// Initialize the GC Adapter capture and read sequence
107 Adapter();
108
109 /// Close the adapter read thread and release the adapter
110 ~Adapter();
111
112 GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); 104 GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
113 105
114 void PadToState(GCPadStatus pad, GCState& state); 106 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 {
14 14
15class GCButton final : public Input::ButtonDevice { 15class GCButton final : public Input::ButtonDevice {
16public: 16public:
17 explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) 17 explicit GCButton(int port_, int button_, int axis_,
18 std::shared_ptr<GCAdapter::Adapter> adapter)
18 : port(port_), button(button_), gcadapter(adapter) {} 19 : port(port_), button(button_), gcadapter(adapter) {}
19 20
20 ~GCButton() override; 21 ~GCButton() override;
@@ -26,13 +27,13 @@ public:
26private: 27private:
27 const int port; 28 const int port;
28 const int button; 29 const int button;
29 GCAdapter::Adapter* gcadapter; 30 std::shared_ptr<GCAdapter::Adapter> gcadapter;
30}; 31};
31 32
32class GCAxisButton final : public Input::ButtonDevice { 33class GCAxisButton final : public Input::ButtonDevice {
33public: 34public:
34 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, 35 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_,
35 GCAdapter::Adapter* adapter) 36 std::shared_ptr<GCAdapter::Adapter> adapter)
36 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), 37 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
37 gcadapter(adapter) {} 38 gcadapter(adapter) {}
38 39
@@ -49,12 +50,11 @@ private:
49 const int axis; 50 const int axis;
50 float threshold; 51 float threshold;
51 bool trigger_if_greater; 52 bool trigger_if_greater;
52 GCAdapter::Adapter* gcadapter; 53 std::shared_ptr<GCAdapter::Adapter> gcadapter;
53}; 54};
54 55
55GCButtonFactory::GCButtonFactory() { 56GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
56 adapter = GCAdapter::Adapter::GetInstance(); 57 : adapter(adapter_) {}
57}
58 58
59GCButton::~GCButton() = default; 59GCButton::~GCButton() = default;
60 60
@@ -171,7 +171,8 @@ void GCButtonFactory::EndConfiguration() {
171 171
172class GCAnalog final : public Input::AnalogDevice { 172class GCAnalog final : public Input::AnalogDevice {
173public: 173public:
174 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) 174 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_,
175 std::shared_ptr<GCAdapter::Adapter> adapter)
175 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} 176 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {}
176 177
177 float GetAxis(int axis) const { 178 float GetAxis(int axis) const {
@@ -230,13 +231,12 @@ private:
230 const int axis_y; 231 const int axis_y;
231 const float deadzone; 232 const float deadzone;
232 mutable std::mutex mutex; 233 mutable std::mutex mutex;
233 GCAdapter::Adapter* gcadapter; 234 std::shared_ptr<GCAdapter::Adapter> gcadapter;
234}; 235};
235 236
236/// An analog device factory that creates analog devices from GC Adapter 237/// An analog device factory that creates analog devices from GC Adapter
237GCAnalogFactory::GCAnalogFactory() { 238GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
238 adapter = GCAdapter::Adapter::GetInstance(); 239 : adapter(adapter_) {}
239};
240 240
241/** 241/**
242 * Creates analog device from joystick axes 242 * 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 {
15 */ 15 */
16class GCButtonFactory final : public Input::Factory<Input::ButtonDevice> { 16class GCButtonFactory final : public Input::Factory<Input::ButtonDevice> {
17public: 17public:
18 GCButtonFactory(); 18 GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_);
19 19
20 /** 20 /**
21 * Creates a button device from a button press 21 * Creates a button device from a button press
@@ -35,14 +35,14 @@ public:
35 } 35 }
36 36
37private: 37private:
38 GCAdapter::Adapter* adapter; 38 std::shared_ptr<GCAdapter::Adapter> adapter;
39 bool polling = false; 39 bool polling = false;
40}; 40};
41 41
42/// An analog device factory that creates analog devices from GC Adapter 42/// An analog device factory that creates analog devices from GC Adapter
43class GCAnalogFactory final : public Input::Factory<Input::AnalogDevice> { 43class GCAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
44public: 44public:
45 GCAnalogFactory(); 45 GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_);
46 std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override; 46 std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override;
47 Common::ParamPackage GetNextInput(); 47 Common::ParamPackage GetNextInput();
48 48
@@ -55,7 +55,7 @@ public:
55 } 55 }
56 56
57private: 57private:
58 GCAdapter::Adapter* adapter; 58 std::shared_ptr<GCAdapter::Adapter> adapter;
59 int analog_x_axis = -1; 59 int analog_x_axis = -1;
60 int analog_y_axis = -1; 60 int analog_y_axis = -1;
61 int controller_number = -1; 61 int controller_number = -1;
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
index 89dddf7cf..fc399db7e 100644
--- a/src/input_common/main.cpp
+++ b/src/input_common/main.cpp
@@ -25,13 +25,15 @@ static std::shared_ptr<MotionEmu> motion_emu;
25static std::unique_ptr<SDL::State> sdl; 25static std::unique_ptr<SDL::State> sdl;
26#endif 26#endif
27static std::unique_ptr<CemuhookUDP::State> udp; 27static std::unique_ptr<CemuhookUDP::State> udp;
28static std::shared_ptr<GCAdapter::Adapter> gcadapter;
28static std::shared_ptr<GCButtonFactory> gcbuttons; 29static std::shared_ptr<GCButtonFactory> gcbuttons;
29static std::shared_ptr<GCAnalogFactory> gcanalog; 30static std::shared_ptr<GCAnalogFactory> gcanalog;
30 31
31void Init() { 32void Init() {
32 gcbuttons = std::make_shared<GCButtonFactory>(); 33 gcadapter = std::make_shared<GCAdapter::Adapter>();
34 gcbuttons = std::make_shared<GCButtonFactory>(gcadapter);
33 Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons); 35 Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons);
34 gcanalog = std::make_shared<GCAnalogFactory>(); 36 gcanalog = std::make_shared<GCAnalogFactory>(gcadapter);
35 Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog); 37 Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog);
36 38
37 keyboard = std::make_shared<Keyboard>(); 39 keyboard = std::make_shared<Keyboard>();