diff options
Diffstat (limited to 'src/input_common/gcadapter')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 11 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.h | 15 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 42 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.h | 2 |
4 files changed, 44 insertions, 26 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 6d9f4d9eb..38210ffcb 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #include <chrono> | 5 | #include <chrono> |
| 6 | #include <thread> | 6 | #include <thread> |
| 7 | #include <libusb.h> | ||
| 7 | #include "common/logging/log.h" | 8 | #include "common/logging/log.h" |
| 8 | #include "input_common/gcadapter/gc_adapter.h" | 9 | #include "input_common/gcadapter/gc_adapter.h" |
| 9 | 10 | ||
| @@ -33,7 +34,7 @@ Adapter::Adapter() { | |||
| 33 | } | 34 | } |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { | 37 | GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { |
| 37 | GCPadStatus pad = {}; | 38 | GCPadStatus pad = {}; |
| 38 | bool get_origin = false; | 39 | bool get_origin = false; |
| 39 | 40 | ||
| @@ -198,7 +199,7 @@ void Adapter::StartScanThread() { | |||
| 198 | } | 199 | } |
| 199 | 200 | ||
| 200 | detect_thread_running = true; | 201 | detect_thread_running = true; |
| 201 | detect_thread = std::thread([=] { ScanThreadFunc(); }); | 202 | detect_thread = std::thread(&Adapter::ScanThreadFunc, this); |
| 202 | } | 203 | } |
| 203 | 204 | ||
| 204 | void Adapter::StopScanThread() { | 205 | void Adapter::StopScanThread() { |
| @@ -227,7 +228,7 @@ void Adapter::Setup() { | |||
| 227 | } | 228 | } |
| 228 | 229 | ||
| 229 | if (devices != nullptr) { | 230 | if (devices != nullptr) { |
| 230 | for (std::size_t index = 0; index < device_count; ++index) { | 231 | for (std::size_t index = 0; index < static_cast<std::size_t>(device_count); ++index) { |
| 231 | if (CheckDeviceAccess(devices[index])) { | 232 | if (CheckDeviceAccess(devices[index])) { |
| 232 | // GC Adapter found and accessible, registering it | 233 | // GC Adapter found and accessible, registering it |
| 233 | GetGCEndpoint(devices[index]); | 234 | GetGCEndpoint(devices[index]); |
| @@ -357,11 +358,11 @@ void Adapter::Reset() { | |||
| 357 | } | 358 | } |
| 358 | } | 359 | } |
| 359 | 360 | ||
| 360 | bool Adapter::DeviceConnected(int port) { | 361 | bool Adapter::DeviceConnected(std::size_t port) { |
| 361 | return adapter_controllers_status[port] != ControllerTypes::None; | 362 | return adapter_controllers_status[port] != ControllerTypes::None; |
| 362 | } | 363 | } |
| 363 | 364 | ||
| 364 | void Adapter::ResetDeviceType(int port) { | 365 | void Adapter::ResetDeviceType(std::size_t port) { |
| 365 | adapter_controllers_status[port] = ControllerTypes::None; | 366 | adapter_controllers_status[port] = ControllerTypes::None; |
| 366 | } | 367 | } |
| 367 | 368 | ||
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index b1c2a1958..e2cdd6255 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h | |||
| @@ -8,10 +8,13 @@ | |||
| 8 | #include <mutex> | 8 | #include <mutex> |
| 9 | #include <thread> | 9 | #include <thread> |
| 10 | #include <unordered_map> | 10 | #include <unordered_map> |
| 11 | #include <libusb.h> | ||
| 12 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| 13 | #include "common/threadsafe_queue.h" | 12 | #include "common/threadsafe_queue.h" |
| 14 | 13 | ||
| 14 | struct libusb_context; | ||
| 15 | struct libusb_device; | ||
| 16 | struct libusb_device_handle; | ||
| 17 | |||
| 15 | namespace GCAdapter { | 18 | namespace GCAdapter { |
| 16 | 19 | ||
| 17 | enum { | 20 | enum { |
| @@ -97,6 +100,9 @@ public: | |||
| 97 | void BeginConfiguration(); | 100 | void BeginConfiguration(); |
| 98 | void EndConfiguration(); | 101 | void EndConfiguration(); |
| 99 | 102 | ||
| 103 | /// Returns true if there is a device connected to port | ||
| 104 | bool DeviceConnected(std::size_t port); | ||
| 105 | |||
| 100 | std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue(); | 106 | std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue(); |
| 101 | const std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const; | 107 | const std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const; |
| 102 | 108 | ||
| @@ -104,7 +110,7 @@ public: | |||
| 104 | const std::array<GCState, 4>& GetPadState() const; | 110 | const std::array<GCState, 4>& GetPadState() const; |
| 105 | 111 | ||
| 106 | private: | 112 | private: |
| 107 | GCPadStatus GetPadStatus(int port, const std::array<u8, 37>& adapter_payload); | 113 | GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); |
| 108 | 114 | ||
| 109 | void PadToState(const GCPadStatus& pad, GCState& state); | 115 | void PadToState(const GCPadStatus& pad, GCState& state); |
| 110 | 116 | ||
| @@ -116,11 +122,8 @@ private: | |||
| 116 | /// Stop scanning for the adapter | 122 | /// Stop scanning for the adapter |
| 117 | void StopScanThread(); | 123 | void StopScanThread(); |
| 118 | 124 | ||
| 119 | /// Returns true if there is a device connected to port | ||
| 120 | bool DeviceConnected(int port); | ||
| 121 | |||
| 122 | /// Resets status of device connected to port | 125 | /// Resets status of device connected to port |
| 123 | void ResetDeviceType(int port); | 126 | void ResetDeviceType(std::size_t port); |
| 124 | 127 | ||
| 125 | /// Returns true if we successfully gain access to GC Adapter | 128 | /// Returns true if we successfully gain access to GC Adapter |
| 126 | bool CheckDeviceAccess(libusb_device* device); | 129 | bool CheckDeviceAccess(libusb_device* device); |
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 385ce8430..b20419ec3 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include <list> | 6 | #include <list> |
| 7 | #include <mutex> | 7 | #include <mutex> |
| 8 | #include <utility> | 8 | #include <utility> |
| 9 | #include "common/assert.h" | ||
| 9 | #include "common/threadsafe_queue.h" | 10 | #include "common/threadsafe_queue.h" |
| 10 | #include "input_common/gcadapter/gc_adapter.h" | 11 | #include "input_common/gcadapter/gc_adapter.h" |
| 11 | #include "input_common/gcadapter/gc_poller.h" | 12 | #include "input_common/gcadapter/gc_poller.h" |
| @@ -20,7 +21,10 @@ public: | |||
| 20 | ~GCButton() override; | 21 | ~GCButton() override; |
| 21 | 22 | ||
| 22 | bool GetStatus() const override { | 23 | bool GetStatus() const override { |
| 23 | return gcadapter->GetPadState()[port].buttons.at(button); | 24 | if (gcadapter->DeviceConnected(port)) { |
| 25 | return gcadapter->GetPadState()[port].buttons.at(button); | ||
| 26 | } | ||
| 27 | return false; | ||
| 24 | } | 28 | } |
| 25 | 29 | ||
| 26 | private: | 30 | private: |
| @@ -43,13 +47,17 @@ public: | |||
| 43 | } | 47 | } |
| 44 | 48 | ||
| 45 | bool GetStatus() const override { | 49 | bool GetStatus() const override { |
| 46 | const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; | 50 | if (gcadapter->DeviceConnected(port)) { |
| 47 | if (trigger_if_greater) { | 51 | const float axis_value = |
| 48 | // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently | 52 | (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; |
| 49 | // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick | 53 | if (trigger_if_greater) { |
| 50 | return axis_value > threshold; | 54 | // TODO: Might be worthwile to set a slider for the trigger threshold. It is |
| 55 | // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick | ||
| 56 | return axis_value > threshold; | ||
| 57 | } | ||
| 58 | return axis_value < -threshold; | ||
| 51 | } | 59 | } |
| 52 | return axis_value < -threshold; | 60 | return false; |
| 53 | } | 61 | } |
| 54 | 62 | ||
| 55 | private: | 63 | private: |
| @@ -94,9 +102,12 @@ std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::Param | |||
| 94 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, | 102 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, |
| 95 | adapter.get()); | 103 | adapter.get()); |
| 96 | } | 104 | } |
| 105 | |||
| 106 | UNREACHABLE(); | ||
| 107 | return nullptr; | ||
| 97 | } | 108 | } |
| 98 | 109 | ||
| 99 | Common::ParamPackage GCButtonFactory::GetNextInput() { | 110 | Common::ParamPackage GCButtonFactory::GetNextInput() const { |
| 100 | Common::ParamPackage params; | 111 | Common::ParamPackage params; |
| 101 | GCAdapter::GCPadStatus pad; | 112 | GCAdapter::GCPadStatus pad; |
| 102 | auto& queue = adapter->GetPadQueue(); | 113 | auto& queue = adapter->GetPadQueue(); |
| @@ -147,11 +158,14 @@ public: | |||
| 147 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} | 158 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} |
| 148 | 159 | ||
| 149 | float GetAxis(int axis) const { | 160 | float GetAxis(int axis) const { |
| 150 | std::lock_guard lock{mutex}; | 161 | if (gcadapter->DeviceConnected(port)) { |
| 151 | // division is not by a perfect 128 to account for some variance in center location | 162 | std::lock_guard lock{mutex}; |
| 152 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range | 163 | // division is not by a perfect 128 to account for some variance in center location |
| 153 | // [20-230] | 164 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range |
| 154 | return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; | 165 | // [20-230] |
| 166 | return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; | ||
| 167 | } | ||
| 168 | return 0.0f; | ||
| 155 | } | 169 | } |
| 156 | 170 | ||
| 157 | std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { | 171 | std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { |
| @@ -249,7 +263,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { | |||
| 249 | const u8 axis = static_cast<u8>(pad.axis); | 263 | const u8 axis = static_cast<u8>(pad.axis); |
| 250 | if (analog_x_axis == -1) { | 264 | if (analog_x_axis == -1) { |
| 251 | analog_x_axis = axis; | 265 | analog_x_axis = axis; |
| 252 | controller_number = port; | 266 | controller_number = static_cast<int>(port); |
| 253 | } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == port) { | 267 | } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == port) { |
| 254 | analog_y_axis = axis; | 268 | analog_y_axis = axis; |
| 255 | } | 269 | } |
diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index e96af7d51..0527f328f 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h | |||
| @@ -25,7 +25,7 @@ public: | |||
| 25 | */ | 25 | */ |
| 26 | std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override; | 26 | std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override; |
| 27 | 27 | ||
| 28 | Common::ParamPackage GetNextInput(); | 28 | Common::ParamPackage GetNextInput() const; |
| 29 | 29 | ||
| 30 | /// For device input configuration/polling | 30 | /// For device input configuration/polling |
| 31 | void BeginConfiguration(); | 31 | void BeginConfiguration(); |