summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/math_util.h4
-rw-r--r--src/common/vector_math.h75
-rw-r--r--src/input_common/CMakeLists.txt29
-rwxr-xr-xsrc/input_common/analog_from_button.cpp18
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp35
-rw-r--r--src/input_common/gcadapter/gc_adapter.h6
-rw-r--r--src/input_common/gcadapter/gc_poller.cpp48
-rw-r--r--src/input_common/keyboard.cpp5
-rw-r--r--src/input_common/main.cpp4
-rw-r--r--src/input_common/motion_emu.cpp41
-rw-r--r--src/input_common/motion_from_button.cpp2
-rw-r--r--src/input_common/motion_input.cpp38
-rw-r--r--src/input_common/motion_input.h10
-rw-r--r--src/input_common/sdl/sdl_impl.cpp77
-rw-r--r--src/input_common/touch_from_button.cpp8
-rw-r--r--src/input_common/udp/client.cpp65
-rw-r--r--src/input_common/udp/client.h14
-rw-r--r--src/input_common/udp/udp.cpp28
-rw-r--r--src/yuzu/configuration/configure_motion_touch.cpp2
19 files changed, 306 insertions, 203 deletions
diff --git a/src/common/math_util.h b/src/common/math_util.h
index b35ad8507..7cec80d57 100644
--- a/src/common/math_util.h
+++ b/src/common/math_util.h
@@ -20,8 +20,8 @@ struct Rectangle {
20 20
21 constexpr Rectangle() = default; 21 constexpr Rectangle() = default;
22 22
23 constexpr Rectangle(T left, T top, T right, T bottom) 23 constexpr Rectangle(T left_, T top_, T right_, T bottom_)
24 : left(left), top(top), right(right), bottom(bottom) {} 24 : left(left_), top(top_), right(right_), bottom(bottom_) {}
25 25
26 [[nodiscard]] T GetWidth() const { 26 [[nodiscard]] T GetWidth() const {
27 if constexpr (std::is_floating_point_v<T>) { 27 if constexpr (std::is_floating_point_v<T>) {
diff --git a/src/common/vector_math.h b/src/common/vector_math.h
index 2a0fcf541..22dba3c2d 100644
--- a/src/common/vector_math.h
+++ b/src/common/vector_math.h
@@ -87,7 +87,13 @@ public:
87 87
88 template <typename V> 88 template <typename V>
89 [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { 89 [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const {
90 return {x * f, y * f}; 90 using TV = decltype(T{} * V{});
91 using C = std::common_type_t<T, V>;
92
93 return {
94 static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
95 static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
96 };
91 } 97 }
92 98
93 template <typename V> 99 template <typename V>
@@ -98,7 +104,13 @@ public:
98 104
99 template <typename V> 105 template <typename V>
100 [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { 106 [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const {
101 return {x / f, y / f}; 107 using TV = decltype(T{} / V{});
108 using C = std::common_type_t<T, V>;
109
110 return {
111 static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
112 static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
113 };
102 } 114 }
103 115
104 template <typename V> 116 template <typename V>
@@ -168,7 +180,10 @@ public:
168 180
169template <typename T, typename V> 181template <typename T, typename V>
170[[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { 182[[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) {
171 return Vec2<T>(f * vec.x, f * vec.y); 183 using C = std::common_type_t<T, V>;
184
185 return Vec2<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)),
186 static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)));
172} 187}
173 188
174using Vec2f = Vec2<float>; 189using Vec2f = Vec2<float>;
@@ -237,7 +252,14 @@ public:
237 252
238 template <typename V> 253 template <typename V>
239 [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { 254 [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const {
240 return {x * f, y * f, z * f}; 255 using TV = decltype(T{} * V{});
256 using C = std::common_type_t<T, V>;
257
258 return {
259 static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
260 static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
261 static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)),
262 };
241 } 263 }
242 264
243 template <typename V> 265 template <typename V>
@@ -247,7 +269,14 @@ public:
247 } 269 }
248 template <typename V> 270 template <typename V>
249 [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { 271 [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const {
250 return {x / f, y / f, z / f}; 272 using TV = decltype(T{} / V{});
273 using C = std::common_type_t<T, V>;
274
275 return {
276 static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
277 static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
278 static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)),
279 };
251 } 280 }
252 281
253 template <typename V> 282 template <typename V>
@@ -367,7 +396,11 @@ public:
367 396
368template <typename T, typename V> 397template <typename T, typename V>
369[[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { 398[[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) {
370 return Vec3<T>(f * vec.x, f * vec.y, f * vec.z); 399 using C = std::common_type_t<T, V>;
400
401 return Vec3<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)),
402 static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)),
403 static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.z)));
371} 404}
372 405
373template <> 406template <>
@@ -446,7 +479,15 @@ public:
446 479
447 template <typename V> 480 template <typename V>
448 [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { 481 [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const {
449 return {x * f, y * f, z * f, w * f}; 482 using TV = decltype(T{} * V{});
483 using C = std::common_type_t<T, V>;
484
485 return {
486 static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
487 static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
488 static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)),
489 static_cast<TV>(static_cast<C>(w) * static_cast<C>(f)),
490 };
450 } 491 }
451 492
452 template <typename V> 493 template <typename V>
@@ -457,7 +498,15 @@ public:
457 498
458 template <typename V> 499 template <typename V>
459 [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { 500 [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const {
460 return {x / f, y / f, z / f, w / f}; 501 using TV = decltype(T{} / V{});
502 using C = std::common_type_t<T, V>;
503
504 return {
505 static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
506 static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
507 static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)),
508 static_cast<TV>(static_cast<C>(w) / static_cast<C>(f)),
509 };
461 } 510 }
462 511
463 template <typename V> 512 template <typename V>
@@ -582,7 +631,15 @@ public:
582 631
583template <typename T, typename V> 632template <typename T, typename V>
584[[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { 633[[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) {
585 return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; 634 using TV = decltype(V{} * T{});
635 using C = std::common_type_t<T, V>;
636
637 return {
638 static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.x)),
639 static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.y)),
640 static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.z)),
641 static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.w)),
642 };
586} 643}
587 644
588using Vec4f = Vec4<float>; 645using Vec4f = Vec4<float>;
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt
index c84685214..7b39a38c1 100644
--- a/src/input_common/CMakeLists.txt
+++ b/src/input_common/CMakeLists.txt
@@ -29,6 +29,35 @@ add_library(input_common STATIC
29 udp/udp.h 29 udp/udp.h
30) 30)
31 31
32if (MSVC)
33 target_compile_options(input_common PRIVATE
34 # 'expression' : signed/unsigned mismatch
35 /we4018
36 # 'argument' : conversion from 'type1' to 'type2', possible loss of data (floating-point)
37 /we4244
38 # 'conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch
39 /we4245
40 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
41 /we4254
42 # 'var' : conversion from 'size_t' to 'type', possible loss of data
43 /we4267
44 # 'context' : truncation from 'type1' to 'type2'
45 /we4305
46 )
47else()
48 target_compile_options(input_common PRIVATE
49 -Werror=conversion
50 -Werror=ignored-qualifiers
51 -Werror=implicit-fallthrough
52 -Werror=reorder
53 -Werror=shadow
54 -Werror=sign-compare
55 -Werror=unused-but-set-parameter
56 -Werror=unused-but-set-variable
57 -Werror=unused-variable
58 )
59endif()
60
32if(SDL2_FOUND) 61if(SDL2_FOUND)
33 target_sources(input_common PRIVATE 62 target_sources(input_common PRIVATE
34 sdl/sdl_impl.cpp 63 sdl/sdl_impl.cpp
diff --git a/src/input_common/analog_from_button.cpp b/src/input_common/analog_from_button.cpp
index 6cabdaa3c..74744d7f3 100755
--- a/src/input_common/analog_from_button.cpp
+++ b/src/input_common/analog_from_button.cpp
@@ -20,18 +20,22 @@ public:
20 constexpr float SQRT_HALF = 0.707106781f; 20 constexpr float SQRT_HALF = 0.707106781f;
21 int x = 0, y = 0; 21 int x = 0, y = 0;
22 22
23 if (right->GetStatus()) 23 if (right->GetStatus()) {
24 ++x; 24 ++x;
25 if (left->GetStatus()) 25 }
26 if (left->GetStatus()) {
26 --x; 27 --x;
27 if (up->GetStatus()) 28 }
29 if (up->GetStatus()) {
28 ++y; 30 ++y;
29 if (down->GetStatus()) 31 }
32 if (down->GetStatus()) {
30 --y; 33 --y;
34 }
31 35
32 float coef = modifier->GetStatus() ? modifier_scale : 1.0f; 36 const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
33 return std::make_tuple(x * coef * (y == 0 ? 1.0f : SQRT_HALF), 37 return std::make_tuple(static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF),
34 y * coef * (x == 0 ? 1.0f : SQRT_HALF)); 38 static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF));
35 } 39 }
36 40
37 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { 41 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp
index 89c148aba..c95feb0d7 100644
--- a/src/input_common/gcadapter/gc_adapter.cpp
+++ b/src/input_common/gcadapter/gc_adapter.cpp
@@ -21,7 +21,7 @@
21 21
22namespace GCAdapter { 22namespace GCAdapter {
23 23
24/// Used to loop through and assign button in poller 24// Used to loop through and assign button in poller
25constexpr std::array<PadButton, 12> PadButtonArray{ 25constexpr std::array<PadButton, 12> PadButtonArray{
26 PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN, 26 PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN,
27 PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R, 27 PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R,
@@ -29,6 +29,18 @@ constexpr std::array<PadButton, 12> PadButtonArray{
29 PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START, 29 PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START,
30}; 30};
31 31
32static void PadToState(const GCPadStatus& pad, GCState& out_state) {
33 for (const auto& button : PadButtonArray) {
34 const auto button_key = static_cast<u16>(button);
35 const auto button_value = (pad.button & button_key) != 0;
36 out_state.buttons.insert_or_assign(static_cast<s32>(button_key), button_value);
37 }
38
39 for (std::size_t i = 0; i < pad.axis_values.size(); ++i) {
40 out_state.axes.insert_or_assign(static_cast<u32>(i), pad.axis_values[i]);
41 }
42}
43
32Adapter::Adapter() { 44Adapter::Adapter() {
33 if (usb_adapter_handle != nullptr) { 45 if (usb_adapter_handle != nullptr) {
34 return; 46 return;
@@ -78,17 +90,17 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad
78 90
79 for (std::size_t i = 0; i < b1_buttons.size(); ++i) { 91 for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
80 if ((b1 & (1U << i)) != 0) { 92 if ((b1 & (1U << i)) != 0) {
81 pad.button |= static_cast<u16>(b1_buttons[i]); 93 pad.button = static_cast<u16>(pad.button | static_cast<u16>(b1_buttons[i]));
82 } 94 }
83 } 95 }
84 96
85 for (std::size_t j = 0; j < b2_buttons.size(); ++j) { 97 for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
86 if ((b2 & (1U << j)) != 0) { 98 if ((b2 & (1U << j)) != 0) {
87 pad.button |= static_cast<u16>(b2_buttons[j]); 99 pad.button = static_cast<u16>(pad.button | static_cast<u16>(b2_buttons[j]));
88 } 100 }
89 } 101 }
90 for (PadAxes axis : axes) { 102 for (PadAxes axis : axes) {
91 const std::size_t index = static_cast<std::size_t>(axis); 103 const auto index = static_cast<std::size_t>(axis);
92 pad.axis_values[index] = adapter_payload[offset + 3 + index]; 104 pad.axis_values[index] = adapter_payload[offset + 3 + index];
93 } 105 }
94 106
@@ -100,17 +112,6 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad
100 return pad; 112 return pad;
101} 113}
102 114
103void Adapter::PadToState(const GCPadStatus& pad, GCState& state) {
104 for (const auto& button : PadButtonArray) {
105 const u16 button_value = static_cast<u16>(button);
106 state.buttons.insert_or_assign(button_value, pad.button & button_value);
107 }
108
109 for (size_t i = 0; i < pad.axis_values.size(); ++i) {
110 state.axes.insert_or_assign(static_cast<u8>(i), pad.axis_values[i]);
111 }
112}
113
114void Adapter::Read() { 115void Adapter::Read() {
115 LOG_DEBUG(Input, "GC Adapter Read() thread started"); 116 LOG_DEBUG(Input, "GC Adapter Read() thread started");
116 117
@@ -250,7 +251,7 @@ void Adapter::GetGCEndpoint(libusb_device* device) {
250 const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; 251 const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
251 for (u8 e = 0; e < interface->bNumEndpoints; e++) { 252 for (u8 e = 0; e < interface->bNumEndpoints; e++) {
252 const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; 253 const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
253 if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { 254 if ((endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) != 0) {
254 input_endpoint = endpoint->bEndpointAddress; 255 input_endpoint = endpoint->bEndpointAddress;
255 } else { 256 } else {
256 output_endpoint = endpoint->bEndpointAddress; 257 output_endpoint = endpoint->bEndpointAddress;
@@ -419,7 +420,7 @@ const std::array<GCState, 4>& Adapter::GetPadState() const {
419 return state; 420 return state;
420} 421}
421 422
422int Adapter::GetOriginValue(int port, int axis) const { 423int Adapter::GetOriginValue(u32 port, u32 axis) const {
423 return origin_status[port].axis_values[axis]; 424 return origin_status[port].axis_values[axis];
424} 425}
425 426
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h
index 75bf9fe74..4f5f3de8e 100644
--- a/src/input_common/gcadapter/gc_adapter.h
+++ b/src/input_common/gcadapter/gc_adapter.h
@@ -60,7 +60,7 @@ struct GCPadStatus {
60 60
61struct GCState { 61struct GCState {
62 std::unordered_map<int, bool> buttons; 62 std::unordered_map<int, bool> buttons;
63 std::unordered_map<int, u16> axes; 63 std::unordered_map<u32, u16> axes;
64}; 64};
65 65
66enum class ControllerTypes { None, Wired, Wireless }; 66enum class ControllerTypes { None, Wired, Wireless };
@@ -89,13 +89,11 @@ public:
89 std::array<GCState, 4>& GetPadState(); 89 std::array<GCState, 4>& GetPadState();
90 const std::array<GCState, 4>& GetPadState() const; 90 const std::array<GCState, 4>& GetPadState() const;
91 91
92 int GetOriginValue(int port, int axis) const; 92 int GetOriginValue(u32 port, u32 axis) const;
93 93
94private: 94private:
95 GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); 95 GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload);
96 96
97 void PadToState(const GCPadStatus& pad, GCState& state);
98
99 void Read(); 97 void Read();
100 98
101 /// Resets status of device connected to port 99 /// Resets status of device connected to port
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp
index 92e9e8e89..893556916 100644
--- a/src/input_common/gcadapter/gc_poller.cpp
+++ b/src/input_common/gcadapter/gc_poller.cpp
@@ -15,7 +15,7 @@ namespace InputCommon {
15 15
16class GCButton final : public Input::ButtonDevice { 16class GCButton final : public Input::ButtonDevice {
17public: 17public:
18 explicit GCButton(int port_, int button_, const GCAdapter::Adapter* adapter) 18 explicit GCButton(u32 port_, int button_, const GCAdapter::Adapter* adapter)
19 : port(port_), button(button_), gcadapter(adapter) {} 19 : port(port_), button(button_), gcadapter(adapter) {}
20 20
21 ~GCButton() override; 21 ~GCButton() override;
@@ -28,14 +28,14 @@ public:
28 } 28 }
29 29
30private: 30private:
31 const int port; 31 const u32 port;
32 const int button; 32 const int button;
33 const GCAdapter::Adapter* gcadapter; 33 const GCAdapter::Adapter* gcadapter;
34}; 34};
35 35
36class GCAxisButton final : public Input::ButtonDevice { 36class GCAxisButton final : public Input::ButtonDevice {
37public: 37public:
38 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, 38 explicit GCAxisButton(u32 port_, u32 axis_, float threshold_, bool trigger_if_greater_,
39 const GCAdapter::Adapter* adapter) 39 const GCAdapter::Adapter* adapter)
40 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), 40 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
41 gcadapter(adapter), 41 gcadapter(adapter),
@@ -56,8 +56,8 @@ public:
56 } 56 }
57 57
58private: 58private:
59 const int port; 59 const u32 port;
60 const int axis; 60 const u32 axis;
61 float threshold; 61 float threshold;
62 bool trigger_if_greater; 62 bool trigger_if_greater;
63 const GCAdapter::Adapter* gcadapter; 63 const GCAdapter::Adapter* gcadapter;
@@ -70,8 +70,8 @@ GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
70GCButton::~GCButton() = default; 70GCButton::~GCButton() = default;
71 71
72std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { 72std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) {
73 const int button_id = params.Get("button", 0); 73 const auto button_id = params.Get("button", 0);
74 const int port = params.Get("port", 0); 74 const auto port = static_cast<u32>(params.Get("port", 0));
75 75
76 constexpr int PAD_STICK_ID = static_cast<u16>(GCAdapter::PadButton::PAD_STICK); 76 constexpr int PAD_STICK_ID = static_cast<u16>(GCAdapter::PadButton::PAD_STICK);
77 77
@@ -149,25 +149,27 @@ void GCButtonFactory::EndConfiguration() {
149 149
150class GCAnalog final : public Input::AnalogDevice { 150class GCAnalog final : public Input::AnalogDevice {
151public: 151public:
152 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, 152 explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_,
153 const GCAdapter::Adapter* adapter, float range_) 153 const GCAdapter::Adapter* adapter, float range_)
154 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), 154 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter),
155 origin_value_x(static_cast<float>(adapter->GetOriginValue(port_, axis_x_))), 155 origin_value_x(static_cast<float>(adapter->GetOriginValue(port_, axis_x_))),
156 origin_value_y(static_cast<float>(adapter->GetOriginValue(port_, axis_y_))), 156 origin_value_y(static_cast<float>(adapter->GetOriginValue(port_, axis_y_))),
157 range(range_) {} 157 range(range_) {}
158 158
159 float GetAxis(int axis) const { 159 float GetAxis(u32 axis) const {
160 if (gcadapter->DeviceConnected(port)) { 160 if (gcadapter->DeviceConnected(port)) {
161 std::lock_guard lock{mutex}; 161 std::lock_guard lock{mutex};
162 const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; 162 const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y;
163 return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / (100.0f * range); 163 const auto axis_value =
164 static_cast<float>(gcadapter->GetPadState()[port].axes.at(axis));
165 return (axis_value - origin_value) / (100.0f * range);
164 } 166 }
165 return 0.0f; 167 return 0.0f;
166 } 168 }
167 169
168 std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { 170 std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const {
169 float x = GetAxis(axis_x); 171 float x = GetAxis(analog_axis_x);
170 float y = GetAxis(axis_y); 172 float y = GetAxis(analog_axis_y);
171 173
172 // Make sure the coordinates are in the unit circle, 174 // Make sure the coordinates are in the unit circle,
173 // otherwise normalize it. 175 // otherwise normalize it.
@@ -208,9 +210,9 @@ public:
208 } 210 }
209 211
210private: 212private:
211 const int port; 213 const u32 port;
212 const int axis_x; 214 const u32 axis_x;
213 const int axis_y; 215 const u32 axis_y;
214 const float deadzone; 216 const float deadzone;
215 const GCAdapter::Adapter* gcadapter; 217 const GCAdapter::Adapter* gcadapter;
216 const float origin_value_x; 218 const float origin_value_x;
@@ -231,11 +233,11 @@ GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
231 * - "axis_y": the index of the axis to be bind as y-axis 233 * - "axis_y": the index of the axis to be bind as y-axis
232 */ 234 */
233std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) { 235std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) {
234 const int port = params.Get("port", 0); 236 const auto port = static_cast<u32>(params.Get("port", 0));
235 const int axis_x = params.Get("axis_x", 0); 237 const auto axis_x = static_cast<u32>(params.Get("axis_x", 0));
236 const int axis_y = params.Get("axis_y", 1); 238 const auto axis_y = static_cast<u32>(params.Get("axis_y", 1));
237 const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); 239 const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
238 const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); 240 const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f);
239 241
240 return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range); 242 return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range);
241} 243}
@@ -256,7 +258,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() {
256 for (std::size_t port = 0; port < queue.size(); ++port) { 258 for (std::size_t port = 0; port < queue.size(); ++port) {
257 while (queue[port].Pop(pad)) { 259 while (queue[port].Pop(pad)) {
258 if (pad.axis == GCAdapter::PadAxes::Undefined || 260 if (pad.axis == GCAdapter::PadAxes::Undefined ||
259 std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { 261 std::abs((static_cast<float>(pad.axis_value) - 128.0f) / 128.0f) < 0.1f) {
260 continue; 262 continue;
261 } 263 }
262 // An analog device needs two axes, so we need to store the axis for later and wait for 264 // An analog device needs two axes, so we need to store the axis for later and wait for
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp
index afb8e6612..24a6f7a33 100644
--- a/src/input_common/keyboard.cpp
+++ b/src/input_common/keyboard.cpp
@@ -49,8 +49,9 @@ public:
49 void ChangeKeyStatus(int key_code, bool pressed) { 49 void ChangeKeyStatus(int key_code, bool pressed) {
50 std::lock_guard guard{mutex}; 50 std::lock_guard guard{mutex};
51 for (const KeyButtonPair& pair : list) { 51 for (const KeyButtonPair& pair : list) {
52 if (pair.key_code == key_code) 52 if (pair.key_code == key_code) {
53 pair.key_button->status.store(pressed); 53 pair.key_button->status.store(pressed);
54 }
54 } 55 }
55 } 56 }
56 57
@@ -73,7 +74,7 @@ KeyButton::~KeyButton() {
73} 74}
74 75
75std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) { 76std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) {
76 int key_code = params.Get("code", 0); 77 const int key_code = params.Get("code", 0);
77 std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list); 78 std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list);
78 key_button_list->AddKeyButton(key_code, button.get()); 79 key_button_list->AddKeyButton(key_code, button.get());
79 return button; 80 return button;
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
index 3d97d95f7..d32fd8b81 100644
--- a/src/input_common/main.cpp
+++ b/src/input_common/main.cpp
@@ -196,6 +196,10 @@ ButtonMapping InputSubsystem::GetButtonMappingForDevice(const Common::ParamPacka
196 return impl->GetButtonMappingForDevice(device); 196 return impl->GetButtonMappingForDevice(device);
197} 197}
198 198
199MotionMapping InputSubsystem::GetMotionMappingForDevice(const Common::ParamPackage& device) const {
200 return impl->GetMotionMappingForDevice(device);
201}
202
199GCAnalogFactory* InputSubsystem::GetGCAnalogs() { 203GCAnalogFactory* InputSubsystem::GetGCAnalogs() {
200 return impl->gcanalog.get(); 204 return impl->gcanalog.get();
201} 205}
diff --git a/src/input_common/motion_emu.cpp b/src/input_common/motion_emu.cpp
index 69fd3c1d2..d4da5596b 100644
--- a/src/input_common/motion_emu.cpp
+++ b/src/input_common/motion_emu.cpp
@@ -18,11 +18,11 @@ namespace InputCommon {
18// Implementation class of the motion emulation device 18// Implementation class of the motion emulation device
19class MotionEmuDevice { 19class MotionEmuDevice {
20public: 20public:
21 MotionEmuDevice(int update_millisecond, float sensitivity) 21 explicit MotionEmuDevice(int update_millisecond_, float sensitivity_)
22 : update_millisecond(update_millisecond), 22 : update_millisecond(update_millisecond_),
23 update_duration(std::chrono::duration_cast<std::chrono::steady_clock::duration>( 23 update_duration(std::chrono::duration_cast<std::chrono::steady_clock::duration>(
24 std::chrono::milliseconds(update_millisecond))), 24 std::chrono::milliseconds(update_millisecond))),
25 sensitivity(sensitivity), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {} 25 sensitivity(sensitivity_), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {}
26 26
27 ~MotionEmuDevice() { 27 ~MotionEmuDevice() {
28 if (motion_emu_thread.joinable()) { 28 if (motion_emu_thread.joinable()) {
@@ -37,16 +37,18 @@ public:
37 } 37 }
38 38
39 void Tilt(int x, int y) { 39 void Tilt(int x, int y) {
40 auto mouse_move = Common::MakeVec(x, y) - mouse_origin; 40 if (!is_tilting) {
41 if (is_tilting) { 41 return;
42 std::lock_guard guard{tilt_mutex}; 42 }
43 if (mouse_move.x == 0 && mouse_move.y == 0) { 43
44 tilt_angle = 0; 44 std::lock_guard guard{tilt_mutex};
45 } else { 45 const auto mouse_move = Common::MakeVec(x, y) - mouse_origin;
46 tilt_direction = mouse_move.Cast<float>(); 46 if (mouse_move.x == 0 && mouse_move.y == 0) {
47 tilt_angle = 47 tilt_angle = 0;
48 std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, Common::PI * 0.5f); 48 } else {
49 } 49 tilt_direction = mouse_move.Cast<float>();
50 tilt_angle =
51 std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, Common::PI * 0.5f);
50 } 52 }
51 } 53 }
52 54
@@ -86,11 +88,10 @@ private:
86 void MotionEmuThread() { 88 void MotionEmuThread() {
87 auto update_time = std::chrono::steady_clock::now(); 89 auto update_time = std::chrono::steady_clock::now();
88 Common::Quaternion<float> q = Common::MakeQuaternion(Common::Vec3<float>(), 0); 90 Common::Quaternion<float> q = Common::MakeQuaternion(Common::Vec3<float>(), 0);
89 Common::Quaternion<float> old_q;
90 91
91 while (!shutdown_event.WaitUntil(update_time)) { 92 while (!shutdown_event.WaitUntil(update_time)) {
92 update_time += update_duration; 93 update_time += update_duration;
93 old_q = q; 94 const Common::Quaternion<float> old_q = q;
94 95
95 { 96 {
96 std::lock_guard guard{tilt_mutex}; 97 std::lock_guard guard{tilt_mutex};
@@ -100,14 +101,14 @@ private:
100 Common::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), tilt_angle); 101 Common::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), tilt_angle);
101 } 102 }
102 103
103 auto inv_q = q.Inverse(); 104 const auto inv_q = q.Inverse();
104 105
105 // Set the gravity vector in world space 106 // Set the gravity vector in world space
106 auto gravity = Common::MakeVec(0.0f, -1.0f, 0.0f); 107 auto gravity = Common::MakeVec(0.0f, -1.0f, 0.0f);
107 108
108 // Find the angular rate vector in world space 109 // Find the angular rate vector in world space
109 auto angular_rate = ((q - old_q) * inv_q).xyz * 2; 110 auto angular_rate = ((q - old_q) * inv_q).xyz * 2;
110 angular_rate *= 1000 / update_millisecond / Common::PI * 180; 111 angular_rate *= static_cast<float>(1000 / update_millisecond) / Common::PI * 180.0f;
111 112
112 // Transform the two vectors from world space to 3DS space 113 // Transform the two vectors from world space to 3DS space
113 gravity = QuaternionRotate(inv_q, gravity); 114 gravity = QuaternionRotate(inv_q, gravity);
@@ -136,7 +137,7 @@ private:
136// can forward all the inputs to the implementation only when it is valid. 137// can forward all the inputs to the implementation only when it is valid.
137class MotionEmuDeviceWrapper : public Input::MotionDevice { 138class MotionEmuDeviceWrapper : public Input::MotionDevice {
138public: 139public:
139 MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) { 140 explicit MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) {
140 device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity); 141 device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity);
141 } 142 }
142 143
@@ -148,8 +149,8 @@ public:
148}; 149};
149 150
150std::unique_ptr<Input::MotionDevice> MotionEmu::Create(const Common::ParamPackage& params) { 151std::unique_ptr<Input::MotionDevice> MotionEmu::Create(const Common::ParamPackage& params) {
151 int update_period = params.Get("update_period", 100); 152 const int update_period = params.Get("update_period", 100);
152 float sensitivity = params.Get("sensitivity", 0.01f); 153 const float sensitivity = params.Get("sensitivity", 0.01f);
153 auto device_wrapper = std::make_unique<MotionEmuDeviceWrapper>(update_period, sensitivity); 154 auto device_wrapper = std::make_unique<MotionEmuDeviceWrapper>(update_period, sensitivity);
154 // Previously created device is disconnected here. Having two motion devices for 3DS is not 155 // Previously created device is disconnected here. Having two motion devices for 3DS is not
155 // expected. 156 // expected.
diff --git a/src/input_common/motion_from_button.cpp b/src/input_common/motion_from_button.cpp
index 9d459f963..29045a673 100644
--- a/src/input_common/motion_from_button.cpp
+++ b/src/input_common/motion_from_button.cpp
@@ -11,7 +11,7 @@ class MotionKey final : public Input::MotionDevice {
11public: 11public:
12 using Button = std::unique_ptr<Input::ButtonDevice>; 12 using Button = std::unique_ptr<Input::ButtonDevice>;
13 13
14 MotionKey(Button key_) : key(std::move(key_)) {} 14 explicit MotionKey(Button key_) : key(std::move(key_)) {}
15 15
16 Input::MotionStatus GetStatus() const override { 16 Input::MotionStatus GetStatus() const override {
17 17
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp
index e89019723..f77ba535d 100644
--- a/src/input_common/motion_input.cpp
+++ b/src/input_common/motion_input.cpp
@@ -8,8 +8,7 @@
8 8
9namespace InputCommon { 9namespace InputCommon {
10 10
11MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) 11MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {}
12 : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {}
13 12
14void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { 13void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
15 accel = acceleration; 14 accel = acceleration;
@@ -59,7 +58,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const {
59} 58}
60 59
61void MotionInput::UpdateRotation(u64 elapsed_time) { 60void MotionInput::UpdateRotation(u64 elapsed_time) {
62 const f32 sample_period = elapsed_time / 1000000.0f; 61 const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
63 if (sample_period > 0.1f) { 62 if (sample_period > 0.1f) {
64 return; 63 return;
65 } 64 }
@@ -75,7 +74,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) {
75 f32 q2 = quat.xyz[0]; 74 f32 q2 = quat.xyz[0];
76 f32 q3 = quat.xyz[1]; 75 f32 q3 = quat.xyz[1];
77 f32 q4 = quat.xyz[2]; 76 f32 q4 = quat.xyz[2];
78 const f32 sample_period = elapsed_time / 1000000.0f; 77 const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
79 78
80 // Ignore invalid elapsed time 79 // Ignore invalid elapsed time
81 if (sample_period > 0.1f) { 80 if (sample_period > 0.1f) {
@@ -203,21 +202,21 @@ Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_m
203 std::random_device device; 202 std::random_device device;
204 std::mt19937 gen(device()); 203 std::mt19937 gen(device());
205 std::uniform_int_distribution<s16> distribution(-1000, 1000); 204 std::uniform_int_distribution<s16> distribution(-1000, 1000);
206 const Common::Vec3f gyroscope = { 205 const Common::Vec3f gyroscope{
207 distribution(gen) * 0.001f, 206 static_cast<f32>(distribution(gen)) * 0.001f,
208 distribution(gen) * 0.001f, 207 static_cast<f32>(distribution(gen)) * 0.001f,
209 distribution(gen) * 0.001f, 208 static_cast<f32>(distribution(gen)) * 0.001f,
210 }; 209 };
211 const Common::Vec3f accelerometer = { 210 const Common::Vec3f accelerometer{
212 distribution(gen) * 0.001f, 211 static_cast<f32>(distribution(gen)) * 0.001f,
213 distribution(gen) * 0.001f, 212 static_cast<f32>(distribution(gen)) * 0.001f,
214 distribution(gen) * 0.001f, 213 static_cast<f32>(distribution(gen)) * 0.001f,
215 }; 214 };
216 const Common::Vec3f rotation = {}; 215 constexpr Common::Vec3f rotation;
217 const std::array<Common::Vec3f, 3> orientation = { 216 constexpr std::array orientation{
218 Common::Vec3f{1.0f, 0, 0}, 217 Common::Vec3f{1.0f, 0.0f, 0.0f},
219 Common::Vec3f{0, 1.0f, 0}, 218 Common::Vec3f{0.0f, 1.0f, 0.0f},
220 Common::Vec3f{0, 0, 1.0f}, 219 Common::Vec3f{0.0f, 0.0f, 1.0f},
221 }; 220 };
222 return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation}; 221 return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation};
223} 222}
@@ -247,9 +246,6 @@ void MotionInput::SetOrientationFromAccelerometer() {
247 const f32 sample_period = 0.015f; 246 const f32 sample_period = 0.015f;
248 247
249 const auto normal_accel = accel.Normalized(); 248 const auto normal_accel = accel.Normalized();
250 const f32 ax = -normal_accel.x;
251 const f32 ay = normal_accel.y;
252 const f32 az = -normal_accel.z;
253 249
254 while (!IsCalibrated(0.01f) && ++iterations < 100) { 250 while (!IsCalibrated(0.01f) && ++iterations < 100) {
255 // Short name local variable for readability 251 // Short name local variable for readability
@@ -258,7 +254,7 @@ void MotionInput::SetOrientationFromAccelerometer() {
258 f32 q3 = quat.xyz[1]; 254 f32 q3 = quat.xyz[1];
259 f32 q4 = quat.xyz[2]; 255 f32 q4 = quat.xyz[2];
260 256
261 Common::Vec3f rad_gyro = {}; 257 Common::Vec3f rad_gyro;
262 const f32 ax = -normal_accel.x; 258 const f32 ax = -normal_accel.x;
263 const f32 ay = normal_accel.y; 259 const f32 ay = normal_accel.y;
264 const f32 az = -normal_accel.z; 260 const f32 az = -normal_accel.z;
diff --git a/src/input_common/motion_input.h b/src/input_common/motion_input.h
index 6342d0318..abb957f04 100644
--- a/src/input_common/motion_input.h
+++ b/src/input_common/motion_input.h
@@ -22,7 +22,7 @@ public:
22 MotionInput& operator=(MotionInput&&) = default; 22 MotionInput& operator=(MotionInput&&) = default;
23 23
24 void SetAcceleration(const Common::Vec3f& acceleration); 24 void SetAcceleration(const Common::Vec3f& acceleration);
25 void SetGyroscope(const Common::Vec3f& acceleration); 25 void SetGyroscope(const Common::Vec3f& gyroscope);
26 void SetQuaternion(const Common::Quaternion<f32>& quaternion); 26 void SetQuaternion(const Common::Quaternion<f32>& quaternion);
27 void SetGyroDrift(const Common::Vec3f& drift); 27 void SetGyroDrift(const Common::Vec3f& drift);
28 void SetGyroThreshold(f32 threshold); 28 void SetGyroThreshold(f32 threshold);
@@ -49,16 +49,16 @@ private:
49 void SetOrientationFromAccelerometer(); 49 void SetOrientationFromAccelerometer();
50 50
51 // PID constants 51 // PID constants
52 const f32 kp; 52 f32 kp;
53 const f32 ki; 53 f32 ki;
54 const f32 kd; 54 f32 kd;
55 55
56 // PID errors 56 // PID errors
57 Common::Vec3f real_error; 57 Common::Vec3f real_error;
58 Common::Vec3f integral_error; 58 Common::Vec3f integral_error;
59 Common::Vec3f derivative_error; 59 Common::Vec3f derivative_error;
60 60
61 Common::Quaternion<f32> quat; 61 Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f};
62 Common::Vec3f rotations; 62 Common::Vec3f rotations;
63 Common::Vec3f accel; 63 Common::Vec3f accel;
64 Common::Vec3f gyro; 64 Common::Vec3f gyro;
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index bd480570a..8c2cef35d 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -56,9 +56,9 @@ static int SDLEventWatcher(void* user_data, SDL_Event* event) {
56class SDLJoystick { 56class SDLJoystick {
57public: 57public:
58 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, 58 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick,
59 SDL_GameController* gamecontroller) 59 SDL_GameController* game_controller)
60 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, 60 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose},
61 sdl_controller{gamecontroller, &SDL_GameControllerClose} {} 61 sdl_controller{game_controller, &SDL_GameControllerClose} {}
62 62
63 void SetButton(int button, bool value) { 63 void SetButton(int button, bool value) {
64 std::lock_guard lock{mutex}; 64 std::lock_guard lock{mutex};
@@ -77,10 +77,10 @@ public:
77 77
78 float GetAxis(int axis, float range) const { 78 float GetAxis(int axis, float range) const {
79 std::lock_guard lock{mutex}; 79 std::lock_guard lock{mutex};
80 return state.axes.at(axis) / (32767.0f * range); 80 return static_cast<float>(state.axes.at(axis)) / (32767.0f * range);
81 } 81 }
82 82
83 bool RumblePlay(f32 amp_low, f32 amp_high, int time) { 83 bool RumblePlay(f32 amp_low, f32 amp_high, u32 time) {
84 const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF); 84 const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF);
85 const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF); 85 const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF);
86 // Lower drastically the number of state changes 86 // Lower drastically the number of state changes
@@ -124,7 +124,7 @@ public:
124 return std::make_tuple(x, y); 124 return std::make_tuple(x, y);
125 } 125 }
126 126
127 const InputCommon::MotionInput& GetMotion() const { 127 const MotionInput& GetMotion() const {
128 return motion; 128 return motion;
129 } 129 }
130 130
@@ -172,15 +172,15 @@ private:
172 } state; 172 } state;
173 std::string guid; 173 std::string guid;
174 int port; 174 int port;
175 u16 last_state_rumble_high; 175 u16 last_state_rumble_high = 0;
176 u16 last_state_rumble_low; 176 u16 last_state_rumble_low = 0;
177 std::chrono::time_point<std::chrono::system_clock> last_vibration; 177 std::chrono::time_point<std::chrono::system_clock> last_vibration;
178 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; 178 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
179 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; 179 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller;
180 mutable std::mutex mutex; 180 mutable std::mutex mutex;
181 181
182 // motion is initalized without PID values as motion input is not aviable for SDL2 182 // Motion is initialized without PID values as motion input is not aviable for SDL2
183 InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f}; 183 MotionInput motion{0.0f, 0.0f, 0.0f};
184}; 184};
185 185
186std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { 186std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) {
@@ -192,7 +192,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g
192 nullptr, nullptr); 192 nullptr, nullptr);
193 it->second.emplace_back(std::move(joystick)); 193 it->second.emplace_back(std::move(joystick));
194 } 194 }
195 return it->second[port]; 195 return it->second[static_cast<std::size_t>(port)];
196 } 196 }
197 auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr); 197 auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr);
198 return joystick_map[guid].emplace_back(std::move(joystick)); 198 return joystick_map[guid].emplace_back(std::move(joystick));
@@ -212,7 +212,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
212 return sdl_joystick == joystick->GetSDLJoystick(); 212 return sdl_joystick == joystick->GetSDLJoystick();
213 }); 213 });
214 if (vec_it != map_it->second.end()) { 214 if (vec_it != map_it->second.end()) {
215 // This is the common case: There is already an existing SDL_Joystick maped to a 215 // This is the common case: There is already an existing SDL_Joystick mapped to a
216 // SDLJoystick. return the SDLJoystick 216 // SDLJoystick. return the SDLJoystick
217 return *vec_it; 217 return *vec_it;
218 } 218 }
@@ -220,7 +220,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
220 // Search for a SDLJoystick without a mapped SDL_Joystick... 220 // Search for a SDLJoystick without a mapped SDL_Joystick...
221 const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), 221 const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(),
222 [](const std::shared_ptr<SDLJoystick>& joystick) { 222 [](const std::shared_ptr<SDLJoystick>& joystick) {
223 return !joystick->GetSDLJoystick(); 223 return joystick->GetSDLJoystick() == nullptr;
224 }); 224 });
225 if (nullptr_it != map_it->second.end()) { 225 if (nullptr_it != map_it->second.end()) {
226 // ... and map it 226 // ... and map it
@@ -273,22 +273,21 @@ void SDLState::InitJoystick(int joystick_index) {
273void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { 273void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
274 const std::string guid = GetGUID(sdl_joystick); 274 const std::string guid = GetGUID(sdl_joystick);
275 275
276 std::shared_ptr<SDLJoystick> joystick; 276 std::shared_ptr<SDLJoystick> found_joystick;
277 { 277 {
278 std::lock_guard lock{joystick_map_mutex}; 278 std::lock_guard lock{joystick_map_mutex};
279 // This call to guid is safe since the joystick is guaranteed to be in the map 279 // This call to guid is safe since the joystick is guaranteed to be in the map
280 const auto& joystick_guid_list = joystick_map[guid]; 280 const auto& joystick_guid_list = joystick_map[guid];
281 const auto joystick_it = 281 const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
282 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), 282 [&sdl_joystick](const auto& joystick) {
283 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { 283 return joystick->GetSDLJoystick() == sdl_joystick;
284 return joystick->GetSDLJoystick() == sdl_joystick; 284 });
285 }); 285 found_joystick = *joystick_it;
286 joystick = *joystick_it;
287 } 286 }
288 287
289 // Destruct SDL_Joystick outside the lock guard because SDL can internally call the 288 // Destruct SDL_Joystick outside the lock guard because SDL can internally call the
290 // event callback which locks the mutex again. 289 // event callback which locks the mutex again.
291 joystick->SetSDLJoystick(nullptr, nullptr); 290 found_joystick->SetSDLJoystick(nullptr, nullptr);
292} 291}
293 292
294void SDLState::HandleGameControllerEvent(const SDL_Event& event) { 293void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
@@ -392,8 +391,8 @@ private:
392 391
393class SDLAnalog final : public Input::AnalogDevice { 392class SDLAnalog final : public Input::AnalogDevice {
394public: 393public:
395 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_, 394 explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_,
396 float range_) 395 float deadzone_, float range_)
397 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), 396 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_),
398 range(range_) {} 397 range(range_) {}
399 398
@@ -672,13 +671,13 @@ SDLState::SDLState() {
672 RegisterFactory<ButtonDevice>("sdl", button_factory); 671 RegisterFactory<ButtonDevice>("sdl", button_factory);
673 RegisterFactory<MotionDevice>("sdl", motion_factory); 672 RegisterFactory<MotionDevice>("sdl", motion_factory);
674 673
675 // If the frontend is going to manage the event loop, then we dont start one here 674 // If the frontend is going to manage the event loop, then we don't start one here
676 start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); 675 start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0;
677 if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { 676 if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) {
678 LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); 677 LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
679 return; 678 return;
680 } 679 }
681 has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); 680 has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0;
682 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { 681 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) {
683 LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); 682 LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError());
684 } 683 }
@@ -723,8 +722,8 @@ std::vector<Common::ParamPackage> SDLState::GetInputDevices() {
723 std::vector<Common::ParamPackage> devices; 722 std::vector<Common::ParamPackage> devices;
724 for (const auto& [key, value] : joystick_map) { 723 for (const auto& [key, value] : joystick_map) {
725 for (const auto& joystick : value) { 724 for (const auto& joystick : value) {
726 auto joy = joystick->GetSDLJoystick(); 725 auto* joy = joystick->GetSDLJoystick();
727 if (auto controller = joystick->GetSDLGameController()) { 726 if (auto* controller = joystick->GetSDLGameController()) {
728 std::string name = 727 std::string name =
729 fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort()); 728 fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort());
730 devices.emplace_back(Common::ParamPackage{ 729 devices.emplace_back(Common::ParamPackage{
@@ -748,7 +747,7 @@ std::vector<Common::ParamPackage> SDLState::GetInputDevices() {
748} 747}
749 748
750namespace { 749namespace {
751Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, u8 axis, 750Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis,
752 float value = 0.1f) { 751 float value = 0.1f) {
753 Common::ParamPackage params({{"engine", "sdl"}}); 752 Common::ParamPackage params({{"engine", "sdl"}});
754 params.Set("port", port); 753 params.Set("port", port);
@@ -764,7 +763,7 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid
764 return params; 763 return params;
765} 764}
766 765
767Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, u8 button) { 766Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) {
768 Common::ParamPackage params({{"engine", "sdl"}}); 767 Common::ParamPackage params({{"engine", "sdl"}});
769 params.Set("port", port); 768 params.Set("port", port);
770 params.Set("guid", std::move(guid)); 769 params.Set("guid", std::move(guid));
@@ -772,7 +771,7 @@ Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid
772 return params; 771 return params;
773} 772}
774 773
775Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, u8 hat, u8 value) { 774Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) {
776 Common::ParamPackage params({{"engine", "sdl"}}); 775 Common::ParamPackage params({{"engine", "sdl"}});
777 776
778 params.Set("port", port); 777 params.Set("port", port);
@@ -802,17 +801,19 @@ Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Eve
802 case SDL_JOYAXISMOTION: { 801 case SDL_JOYAXISMOTION: {
803 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); 802 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
804 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 803 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
805 event.jaxis.axis, event.jaxis.value); 804 static_cast<s32>(event.jaxis.axis),
805 event.jaxis.value);
806 } 806 }
807 case SDL_JOYBUTTONUP: { 807 case SDL_JOYBUTTONUP: {
808 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); 808 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
809 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 809 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
810 event.jbutton.button); 810 static_cast<s32>(event.jbutton.button));
811 } 811 }
812 case SDL_JOYHATMOTION: { 812 case SDL_JOYHATMOTION: {
813 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); 813 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
814 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 814 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
815 event.jhat.hat, event.jhat.value); 815 static_cast<s32>(event.jhat.hat),
816 static_cast<s32>(event.jhat.value));
816 } 817 }
817 } 818 }
818 return {}; 819 return {};
@@ -823,17 +824,19 @@ Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Eve
823 case SDL_JOYAXISMOTION: { 824 case SDL_JOYAXISMOTION: {
824 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); 825 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
825 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 826 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
826 event.jaxis.axis, event.jaxis.value); 827 static_cast<s32>(event.jaxis.axis),
828 event.jaxis.value);
827 } 829 }
828 case SDL_JOYBUTTONUP: { 830 case SDL_JOYBUTTONUP: {
829 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); 831 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
830 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 832 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
831 event.jbutton.button); 833 static_cast<s32>(event.jbutton.button));
832 } 834 }
833 case SDL_JOYHATMOTION: { 835 case SDL_JOYHATMOTION: {
834 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); 836 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
835 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), 837 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
836 event.jhat.hat, event.jhat.value); 838 static_cast<s32>(event.jhat.hat),
839 static_cast<s32>(event.jhat.value));
837 } 840 }
838 } 841 }
839 return {}; 842 return {};
@@ -1062,7 +1065,7 @@ public:
1062 if (event.type == SDL_JOYAXISMOTION) { 1065 if (event.type == SDL_JOYAXISMOTION) {
1063 const auto axis = event.jaxis.axis; 1066 const auto axis = event.jaxis.axis;
1064 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); 1067 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
1065 const auto controller = joystick->GetSDLGameController(); 1068 auto* const controller = joystick->GetSDLGameController();
1066 if (controller) { 1069 if (controller) {
1067 const auto axis_left_x = 1070 const auto axis_left_x =
1068 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) 1071 SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX)
diff --git a/src/input_common/touch_from_button.cpp b/src/input_common/touch_from_button.cpp
index 98da0ef1a..c37716aae 100644
--- a/src/input_common/touch_from_button.cpp
+++ b/src/input_common/touch_from_button.cpp
@@ -11,9 +11,11 @@ namespace InputCommon {
11class TouchFromButtonDevice final : public Input::TouchDevice { 11class TouchFromButtonDevice final : public Input::TouchDevice {
12public: 12public:
13 TouchFromButtonDevice() { 13 TouchFromButtonDevice() {
14 for (const auto& config_entry : 14 const auto button_index =
15 Settings::values.touch_from_button_maps[Settings::values.touch_from_button_map_index] 15 static_cast<std::size_t>(Settings::values.touch_from_button_map_index);
16 .buttons) { 16 const auto& buttons = Settings::values.touch_from_button_maps[button_index].buttons;
17
18 for (const auto& config_entry : buttons) {
17 const Common::ParamPackage package{config_entry}; 19 const Common::ParamPackage package{config_entry};
18 map.emplace_back( 20 map.emplace_back(
19 Input::CreateDevice<Input::ButtonDevice>(config_entry), 21 Input::CreateDevice<Input::ButtonDevice>(config_entry),
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 9d0b9f31d..bb109562c 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -26,11 +26,11 @@ class Socket {
26public: 26public:
27 using clock = std::chrono::system_clock; 27 using clock = std::chrono::system_clock;
28 28
29 explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id, 29 explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_,
30 SocketCallback callback) 30 SocketCallback callback_)
31 : callback(std::move(callback)), timer(io_service), 31 : callback(std::move(callback_)), timer(io_service),
32 socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id), 32 socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_),
33 pad_index(pad_index) { 33 pad_index(pad_index_) {
34 boost::system::error_code ec{}; 34 boost::system::error_code ec{};
35 auto ipv4 = boost::asio::ip::make_address_v4(host, ec); 35 auto ipv4 = boost::asio::ip::make_address_v4(host, ec);
36 if (ec.value() != boost::system::errc::success) { 36 if (ec.value() != boost::system::errc::success) {
@@ -93,13 +93,17 @@ private:
93 void HandleSend(const boost::system::error_code& error) { 93 void HandleSend(const boost::system::error_code& error) {
94 boost::system::error_code _ignored{}; 94 boost::system::error_code _ignored{};
95 // Send a request for getting port info for the pad 95 // Send a request for getting port info for the pad
96 Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; 96 const Request::PortInfo port_info{1, {static_cast<u8>(pad_index), 0, 0, 0}};
97 const auto port_message = Request::Create(port_info, client_id); 97 const auto port_message = Request::Create(port_info, client_id);
98 std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); 98 std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
99 socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); 99 socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored);
100 100
101 // Send a request for getting pad data for the pad 101 // Send a request for getting pad data for the pad
102 Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; 102 const Request::PadData pad_data{
103 Request::PadData::Flags::Id,
104 static_cast<u8>(pad_index),
105 EMPTY_MAC_ADDRESS,
106 };
103 const auto pad_message = Request::Create(pad_data, client_id); 107 const auto pad_message = Request::Create(pad_data, client_id);
104 std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); 108 std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
105 socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); 109 socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored);
@@ -112,7 +116,7 @@ private:
112 udp::socket socket; 116 udp::socket socket;
113 117
114 u32 client_id{}; 118 u32 client_id{};
115 u8 pad_index{}; 119 std::size_t pad_index{};
116 120
117 static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); 121 static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>);
118 static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); 122 static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>);
@@ -133,7 +137,7 @@ static void SocketLoop(Socket* socket) {
133Client::Client() { 137Client::Client() {
134 LOG_INFO(Input, "Udp Initialization started"); 138 LOG_INFO(Input, "Udp Initialization started");
135 for (std::size_t client = 0; client < clients.size(); client++) { 139 for (std::size_t client = 0; client < clients.size(); client++) {
136 u8 pad = client % 4; 140 const auto pad = client % 4;
137 StartCommunication(client, Settings::values.udp_input_address, 141 StartCommunication(client, Settings::values.udp_input_address,
138 Settings::values.udp_input_port, pad, 24872); 142 Settings::values.udp_input_port, pad, 24872);
139 // Set motion parameters 143 // Set motion parameters
@@ -166,9 +170,9 @@ std::vector<Common::ParamPackage> Client::GetInputDevices() const {
166bool Client::DeviceConnected(std::size_t pad) const { 170bool Client::DeviceConnected(std::size_t pad) const {
167 // Use last timestamp to detect if the socket has stopped sending data 171 // Use last timestamp to detect if the socket has stopped sending data
168 const auto now = std::chrono::system_clock::now(); 172 const auto now = std::chrono::system_clock::now();
169 u64 time_difference = 173 const auto time_difference = static_cast<u64>(
170 std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update) 174 std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update)
171 .count(); 175 .count());
172 return time_difference < 1000 && clients[pad].active == 1; 176 return time_difference < 1000 && clients[pad].active == 1;
173} 177}
174 178
@@ -177,9 +181,9 @@ void Client::ReloadUDPClient() {
177 ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); 181 ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client);
178 } 182 }
179} 183}
180void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { 184void Client::ReloadSocket(const std::string& host, u16 port, std::size_t pad_index, u32 client_id) {
181 // client number must be determined from host / port and pad index 185 // client number must be determined from host / port and pad index
182 std::size_t client = pad_index; 186 const std::size_t client = pad_index;
183 clients[client].socket->Stop(); 187 clients[client].socket->Stop();
184 clients[client].thread.join(); 188 clients[client].thread.join();
185 StartCommunication(client, host, port, pad_index, client_id); 189 StartCommunication(client, host, port, pad_index, client_id);
@@ -194,8 +198,8 @@ void Client::OnPortInfo(Response::PortInfo data) {
194} 198}
195 199
196void Client::OnPadData(Response::PadData data) { 200void Client::OnPadData(Response::PadData data) {
197 // client number must be determined from host / port and pad index 201 // Client number must be determined from host / port and pad index
198 std::size_t client = data.info.id; 202 const std::size_t client = data.info.id;
199 LOG_TRACE(Input, "PadData packet received"); 203 LOG_TRACE(Input, "PadData packet received");
200 if (data.packet_counter == clients[client].packet_sequence) { 204 if (data.packet_counter == clients[client].packet_sequence) {
201 LOG_WARNING( 205 LOG_WARNING(
@@ -207,11 +211,12 @@ void Client::OnPadData(Response::PadData data) {
207 clients[client].active = data.info.is_pad_active; 211 clients[client].active = data.info.is_pad_active;
208 clients[client].packet_sequence = data.packet_counter; 212 clients[client].packet_sequence = data.packet_counter;
209 const auto now = std::chrono::system_clock::now(); 213 const auto now = std::chrono::system_clock::now();
210 u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>( 214 const auto time_difference =
211 now - clients[client].last_motion_update) 215 static_cast<u64>(std::chrono::duration_cast<std::chrono::microseconds>(
212 .count(); 216 now - clients[client].last_motion_update)
217 .count());
213 clients[client].last_motion_update = now; 218 clients[client].last_motion_update = now;
214 Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; 219 const Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw};
215 clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); 220 clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y});
216 // Gyroscope values are not it the correct scale from better joy. 221 // Gyroscope values are not it the correct scale from better joy.
217 // Dividing by 312 allows us to make one full turn = 1 turn 222 // Dividing by 312 allows us to make one full turn = 1 turn
@@ -237,9 +242,11 @@ void Client::OnPadData(Response::PadData data) {
237 const u16 min_y = clients[client].status.touch_calibration->min_y; 242 const u16 min_y = clients[client].status.touch_calibration->min_y;
238 const u16 max_y = clients[client].status.touch_calibration->max_y; 243 const u16 max_y = clients[client].status.touch_calibration->max_y;
239 244
240 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / 245 x = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) -
246 min_x) /
241 static_cast<float>(max_x - min_x); 247 static_cast<float>(max_x - min_x);
242 y = (std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - min_y) / 248 y = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) -
249 min_y) /
243 static_cast<float>(max_y - min_y); 250 static_cast<float>(max_y - min_y);
244 } 251 }
245 252
@@ -253,8 +260,8 @@ void Client::OnPadData(Response::PadData data) {
253 } 260 }
254} 261}
255 262
256void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, 263void Client::StartCommunication(std::size_t client, const std::string& host, u16 port,
257 u32 client_id) { 264 std::size_t pad_index, u32 client_id) {
258 SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, 265 SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
259 [this](Response::PortInfo info) { OnPortInfo(info); }, 266 [this](Response::PortInfo info) { OnPortInfo(info); },
260 [this](Response::PadData data) { OnPadData(data); }}; 267 [this](Response::PadData data) { OnPadData(data); }};
@@ -264,9 +271,9 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16
264} 271}
265 272
266void Client::Reset() { 273void Client::Reset() {
267 for (std::size_t client = 0; client < clients.size(); client++) { 274 for (auto& client : clients) {
268 clients[client].socket->Stop(); 275 client.socket->Stop();
269 clients[client].thread.join(); 276 client.thread.join();
270 } 277 }
271} 278}
272 279
@@ -325,7 +332,7 @@ const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() cons
325 return pad_queue; 332 return pad_queue;
326} 333}
327 334
328void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 335void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id,
329 std::function<void()> success_callback, 336 std::function<void()> success_callback,
330 std::function<void()> failure_callback) { 337 std::function<void()> failure_callback) {
331 std::thread([=] { 338 std::thread([=] {
@@ -346,7 +353,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie
346} 353}
347 354
348CalibrationConfigurationJob::CalibrationConfigurationJob( 355CalibrationConfigurationJob::CalibrationConfigurationJob(
349 const std::string& host, u16 port, u8 pad_index, u32 client_id, 356 const std::string& host, u16 port, std::size_t pad_index, u32 client_id,
350 std::function<void(Status)> status_callback, 357 std::function<void(Status)> status_callback,
351 std::function<void(u16, u16, u16, u16)> data_callback) { 358 std::function<void(u16, u16, u16, u16)> data_callback) {
352 359
@@ -366,7 +373,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
366 current_status = Status::Ready; 373 current_status = Status::Ready;
367 status_callback(current_status); 374 status_callback(current_status);
368 } 375 }
369 if (!data.touch_1.is_active) { 376 if (data.touch_1.is_active == 0) {
370 return; 377 return;
371 } 378 }
372 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 379 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x,
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index 523dc6a7a..2491a03a2 100644
--- a/src/input_common/udp/client.h
+++ b/src/input_common/udp/client.h
@@ -84,8 +84,8 @@ public:
84 84
85 bool DeviceConnected(std::size_t pad) const; 85 bool DeviceConnected(std::size_t pad) const;
86 void ReloadUDPClient(); 86 void ReloadUDPClient();
87 void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, 87 void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760,
88 u32 client_id = 24872); 88 std::size_t pad_index = 0, u32 client_id = 24872);
89 89
90 std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue(); 90 std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue();
91 const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const; 91 const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const;
@@ -99,7 +99,7 @@ private:
99 DeviceStatus status; 99 DeviceStatus status;
100 std::thread thread; 100 std::thread thread;
101 u64 packet_sequence = 0; 101 u64 packet_sequence = 0;
102 u8 active; 102 u8 active = 0;
103 103
104 // Realtime values 104 // Realtime values
105 // motion is initalized with PID values for drift correction on joycons 105 // motion is initalized with PID values for drift correction on joycons
@@ -113,8 +113,8 @@ private:
113 void OnVersion(Response::Version); 113 void OnVersion(Response::Version);
114 void OnPortInfo(Response::PortInfo); 114 void OnPortInfo(Response::PortInfo);
115 void OnPadData(Response::PadData); 115 void OnPadData(Response::PadData);
116 void StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, 116 void StartCommunication(std::size_t client, const std::string& host, u16 port,
117 u32 client_id); 117 std::size_t pad_index, u32 client_id);
118 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, 118 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
119 const Common::Vec3<float>& gyro, bool touch); 119 const Common::Vec3<float>& gyro, bool touch);
120 120
@@ -139,7 +139,7 @@ public:
139 * @param status_callback Callback for job status updates 139 * @param status_callback Callback for job status updates
140 * @param data_callback Called when calibration data is ready 140 * @param data_callback Called when calibration data is ready
141 */ 141 */
142 explicit CalibrationConfigurationJob(const std::string& host, u16 port, u8 pad_index, 142 explicit CalibrationConfigurationJob(const std::string& host, u16 port, std::size_t pad_index,
143 u32 client_id, std::function<void(Status)> status_callback, 143 u32 client_id, std::function<void(Status)> status_callback,
144 std::function<void(u16, u16, u16, u16)> data_callback); 144 std::function<void(u16, u16, u16, u16)> data_callback);
145 ~CalibrationConfigurationJob(); 145 ~CalibrationConfigurationJob();
@@ -149,7 +149,7 @@ private:
149 Common::Event complete_event; 149 Common::Event complete_event;
150}; 150};
151 151
152void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 152void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id,
153 std::function<void()> success_callback, 153 std::function<void()> success_callback,
154 std::function<void()> failure_callback); 154 std::function<void()> failure_callback);
155 155
diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp
index eba077a36..71a76a7aa 100644
--- a/src/input_common/udp/udp.cpp
+++ b/src/input_common/udp/udp.cpp
@@ -2,8 +2,6 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <atomic>
6#include <list>
7#include <mutex> 5#include <mutex>
8#include <utility> 6#include <utility>
9#include "common/assert.h" 7#include "common/assert.h"
@@ -15,8 +13,8 @@ namespace InputCommon {
15 13
16class UDPMotion final : public Input::MotionDevice { 14class UDPMotion final : public Input::MotionDevice {
17public: 15public:
18 UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) 16 explicit UDPMotion(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_)
19 : ip(ip_), port(port_), pad(pad_), client(client_) {} 17 : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
20 18
21 Input::MotionStatus GetStatus() const override { 19 Input::MotionStatus GetStatus() const override {
22 return client->GetPadState(pad).motion_status; 20 return client->GetPadState(pad).motion_status;
@@ -25,7 +23,7 @@ public:
25private: 23private:
26 const std::string ip; 24 const std::string ip;
27 const int port; 25 const int port;
28 const int pad; 26 const u32 pad;
29 CemuhookUDP::Client* client; 27 CemuhookUDP::Client* client;
30 mutable std::mutex mutex; 28 mutable std::mutex mutex;
31}; 29};
@@ -40,11 +38,11 @@ UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_)
40 * - "port": the nth jcpad on the adapter 38 * - "port": the nth jcpad on the adapter
41 */ 39 */
42std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) { 40std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) {
43 const std::string ip = params.Get("ip", "127.0.0.1"); 41 auto ip = params.Get("ip", "127.0.0.1");
44 const int port = params.Get("port", 26760); 42 const auto port = params.Get("port", 26760);
45 const int pad = params.Get("pad_index", 0); 43 const auto pad = static_cast<u32>(params.Get("pad_index", 0));
46 44
47 return std::make_unique<UDPMotion>(ip, port, pad, client.get()); 45 return std::make_unique<UDPMotion>(std::move(ip), port, pad, client.get());
48} 46}
49 47
50void UDPMotionFactory::BeginConfiguration() { 48void UDPMotionFactory::BeginConfiguration() {
@@ -79,7 +77,7 @@ Common::ParamPackage UDPMotionFactory::GetNextInput() {
79 77
80class UDPTouch final : public Input::TouchDevice { 78class UDPTouch final : public Input::TouchDevice {
81public: 79public:
82 UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) 80 explicit UDPTouch(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_)
83 : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} 81 : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
84 82
85 std::tuple<float, float, bool> GetStatus() const override { 83 std::tuple<float, float, bool> GetStatus() const override {
@@ -89,7 +87,7 @@ public:
89private: 87private:
90 const std::string ip; 88 const std::string ip;
91 const int port; 89 const int port;
92 const int pad; 90 const u32 pad;
93 CemuhookUDP::Client* client; 91 CemuhookUDP::Client* client;
94 mutable std::mutex mutex; 92 mutable std::mutex mutex;
95}; 93};
@@ -104,11 +102,11 @@ UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_)
104 * - "port": the nth jcpad on the adapter 102 * - "port": the nth jcpad on the adapter
105 */ 103 */
106std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) { 104std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) {
107 const std::string ip = params.Get("ip", "127.0.0.1"); 105 auto ip = params.Get("ip", "127.0.0.1");
108 const int port = params.Get("port", 26760); 106 const auto port = params.Get("port", 26760);
109 const int pad = params.Get("pad_index", 0); 107 const auto pad = static_cast<u32>(params.Get("pad_index", 0));
110 108
111 return std::make_unique<UDPTouch>(ip, port, pad, client.get()); 109 return std::make_unique<UDPTouch>(std::move(ip), port, pad, client.get());
112} 110}
113 111
114void UDPTouchFactory::BeginConfiguration() { 112void UDPTouchFactory::BeginConfiguration() {
diff --git a/src/yuzu/configuration/configure_motion_touch.cpp b/src/yuzu/configuration/configure_motion_touch.cpp
index c7d085151..170574d9b 100644
--- a/src/yuzu/configuration/configure_motion_touch.cpp
+++ b/src/yuzu/configuration/configure_motion_touch.cpp
@@ -193,7 +193,7 @@ void ConfigureMotionTouch::OnCemuhookUDPTest() {
193 udp_test_in_progress = true; 193 udp_test_in_progress = true;
194 InputCommon::CemuhookUDP::TestCommunication( 194 InputCommon::CemuhookUDP::TestCommunication(
195 ui->udp_server->text().toStdString(), static_cast<u16>(ui->udp_port->text().toInt()), 195 ui->udp_server->text().toStdString(), static_cast<u16>(ui->udp_port->text().toInt()),
196 static_cast<u8>(ui->udp_pad_index->currentIndex()), 24872, 196 static_cast<u32>(ui->udp_pad_index->currentIndex()), 24872,
197 [this] { 197 [this] {
198 LOG_INFO(Frontend, "UDP input test success"); 198 LOG_INFO(Frontend, "UDP input test success");
199 QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, true)); 199 QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, true));