summaryrefslogtreecommitdiff
path: root/src/input_common/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/helpers')
-rw-r--r--src/input_common/helpers/stick_from_buttons.cpp304
-rw-r--r--src/input_common/helpers/stick_from_buttons.h30
-rw-r--r--src/input_common/helpers/touch_from_buttons.cpp81
-rw-r--r--src/input_common/helpers/touch_from_buttons.h22
-rw-r--r--src/input_common/helpers/udp_protocol.cpp78
-rw-r--r--src/input_common/helpers/udp_protocol.h290
6 files changed, 805 insertions, 0 deletions
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp
new file mode 100644
index 000000000..77fcd655e
--- /dev/null
+++ b/src/input_common/helpers/stick_from_buttons.cpp
@@ -0,0 +1,304 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <chrono>
6#include <cmath>
7#include "common/math_util.h"
8#include "common/settings.h"
9#include "input_common/helpers/stick_from_buttons.h"
10
11namespace InputCommon {
12
13class Stick final : public Common::Input::InputDevice {
14public:
15 using Button = std::unique_ptr<Common::Input::InputDevice>;
16
17 Stick(Button up_, Button down_, Button left_, Button right_, Button modifier_,
18 float modifier_scale_, float modifier_angle_)
19 : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
20 right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
21 modifier_angle(modifier_angle_) {
22 Common::Input::InputCallback button_up_callback{
23 [this](Common::Input::CallbackStatus callback_) { UpdateUpButtonStatus(callback_); }};
24 Common::Input::InputCallback button_down_callback{
25 [this](Common::Input::CallbackStatus callback_) { UpdateDownButtonStatus(callback_); }};
26 Common::Input::InputCallback button_left_callback{
27 [this](Common::Input::CallbackStatus callback_) { UpdateLeftButtonStatus(callback_); }};
28 Common::Input::InputCallback button_right_callback{
29 [this](Common::Input::CallbackStatus callback_) {
30 UpdateRightButtonStatus(callback_);
31 }};
32 Common::Input::InputCallback button_modifier_callback{
33 [this](Common::Input::CallbackStatus callback_) { UpdateModButtonStatus(callback_); }};
34 up->SetCallback(button_up_callback);
35 down->SetCallback(button_down_callback);
36 left->SetCallback(button_left_callback);
37 right->SetCallback(button_right_callback);
38 modifier->SetCallback(button_modifier_callback);
39 last_x_axis_value = 0.0f;
40 last_y_axis_value = 0.0f;
41 }
42
43 bool IsAngleGreater(float old_angle, float new_angle) const {
44 constexpr float TAU = Common::PI * 2.0f;
45 // Use wider angle to ease the transition.
46 constexpr float aperture = TAU * 0.15f;
47 const float top_limit = new_angle + aperture;
48 return (old_angle > new_angle && old_angle <= top_limit) ||
49 (old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
50 }
51
52 bool IsAngleSmaller(float old_angle, float new_angle) const {
53 constexpr float TAU = Common::PI * 2.0f;
54 // Use wider angle to ease the transition.
55 constexpr float aperture = TAU * 0.15f;
56 const float bottom_limit = new_angle - aperture;
57 return (old_angle >= bottom_limit && old_angle < new_angle) ||
58 (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
59 }
60
61 float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
62 constexpr float TAU = Common::PI * 2.0f;
63 float new_angle = angle;
64
65 auto time_difference = static_cast<float>(
66 std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count());
67 time_difference /= 1000.0f * 1000.0f;
68 if (time_difference > 0.5f) {
69 time_difference = 0.5f;
70 }
71
72 if (IsAngleGreater(new_angle, goal_angle)) {
73 new_angle -= modifier_angle * time_difference;
74 if (new_angle < 0) {
75 new_angle += TAU;
76 }
77 if (!IsAngleGreater(new_angle, goal_angle)) {
78 return goal_angle;
79 }
80 } else if (IsAngleSmaller(new_angle, goal_angle)) {
81 new_angle += modifier_angle * time_difference;
82 if (new_angle >= TAU) {
83 new_angle -= TAU;
84 }
85 if (!IsAngleSmaller(new_angle, goal_angle)) {
86 return goal_angle;
87 }
88 } else {
89 return goal_angle;
90 }
91 return new_angle;
92 }
93
94 void SetGoalAngle(bool r, bool l, bool u, bool d) {
95 // Move to the right
96 if (r && !u && !d) {
97 goal_angle = 0.0f;
98 }
99
100 // Move to the upper right
101 if (r && u && !d) {
102 goal_angle = Common::PI * 0.25f;
103 }
104
105 // Move up
106 if (u && !l && !r) {
107 goal_angle = Common::PI * 0.5f;
108 }
109
110 // Move to the upper left
111 if (l && u && !d) {
112 goal_angle = Common::PI * 0.75f;
113 }
114
115 // Move to the left
116 if (l && !u && !d) {
117 goal_angle = Common::PI;
118 }
119
120 // Move to the bottom left
121 if (l && !u && d) {
122 goal_angle = Common::PI * 1.25f;
123 }
124
125 // Move down
126 if (d && !l && !r) {
127 goal_angle = Common::PI * 1.5f;
128 }
129
130 // Move to the bottom right
131 if (r && !u && d) {
132 goal_angle = Common::PI * 1.75f;
133 }
134 }
135
136 void UpdateUpButtonStatus(Common::Input::CallbackStatus button_callback) {
137 up_status = button_callback.button_status.value;
138 UpdateStatus();
139 }
140
141 void UpdateDownButtonStatus(Common::Input::CallbackStatus button_callback) {
142 down_status = button_callback.button_status.value;
143 UpdateStatus();
144 }
145
146 void UpdateLeftButtonStatus(Common::Input::CallbackStatus button_callback) {
147 left_status = button_callback.button_status.value;
148 UpdateStatus();
149 }
150
151 void UpdateRightButtonStatus(Common::Input::CallbackStatus button_callback) {
152 right_status = button_callback.button_status.value;
153 UpdateStatus();
154 }
155
156 void UpdateModButtonStatus(Common::Input::CallbackStatus button_callback) {
157 modifier_status = button_callback.button_status.value;
158 UpdateStatus();
159 }
160
161 void UpdateStatus() {
162 const float coef = modifier_status ? modifier_scale : 1.0f;
163
164 bool r = right_status;
165 bool l = left_status;
166 bool u = up_status;
167 bool d = down_status;
168
169 // Eliminate contradictory movements
170 if (r && l) {
171 r = false;
172 l = false;
173 }
174 if (u && d) {
175 u = false;
176 d = false;
177 }
178
179 // Move if a key is pressed
180 if (r || l || u || d) {
181 amplitude = coef;
182 } else {
183 amplitude = 0;
184 }
185
186 const auto now = std::chrono::steady_clock::now();
187 const auto time_difference = static_cast<u64>(
188 std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
189
190 if (time_difference < 10) {
191 // Disable analog mode if inputs are too fast
192 SetGoalAngle(r, l, u, d);
193 angle = goal_angle;
194 } else {
195 angle = GetAngle(now);
196 SetGoalAngle(r, l, u, d);
197 }
198
199 last_update = now;
200 Common::Input::CallbackStatus status{
201 .type = Common::Input::InputType::Stick,
202 .stick_status = GetStatus(),
203 };
204 last_x_axis_value = status.stick_status.x.raw_value;
205 last_y_axis_value = status.stick_status.y.raw_value;
206 TriggerOnChange(status);
207 }
208
209 void ForceUpdate() override {
210 up->ForceUpdate();
211 down->ForceUpdate();
212 left->ForceUpdate();
213 right->ForceUpdate();
214 modifier->ForceUpdate();
215 }
216
217 void SoftUpdate() override {
218 Common::Input::CallbackStatus status{
219 .type = Common::Input::InputType::Stick,
220 .stick_status = GetStatus(),
221 };
222 if (last_x_axis_value == status.stick_status.x.raw_value &&
223 last_y_axis_value == status.stick_status.y.raw_value) {
224 return;
225 }
226 last_x_axis_value = status.stick_status.x.raw_value;
227 last_y_axis_value = status.stick_status.y.raw_value;
228 TriggerOnChange(status);
229 }
230
231 Common::Input::StickStatus GetStatus() const {
232 Common::Input::StickStatus status{};
233 status.x.properties = properties;
234 status.y.properties = properties;
235 if (Settings::values.emulate_analog_keyboard) {
236 const auto now = std::chrono::steady_clock::now();
237 float angle_ = GetAngle(now);
238 status.x.raw_value = std::cos(angle_) * amplitude;
239 status.y.raw_value = std::sin(angle_) * amplitude;
240 return status;
241 }
242 constexpr float SQRT_HALF = 0.707106781f;
243 int x = 0, y = 0;
244 if (right_status) {
245 ++x;
246 }
247 if (left_status) {
248 --x;
249 }
250 if (up_status) {
251 ++y;
252 }
253 if (down_status) {
254 --y;
255 }
256 const float coef = modifier_status ? modifier_scale : 1.0f;
257 status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF);
258 status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF);
259 return status;
260 }
261
262private:
263 Button up;
264 Button down;
265 Button left;
266 Button right;
267 Button modifier;
268 float modifier_scale;
269 float modifier_angle;
270 float angle{};
271 float goal_angle{};
272 float amplitude{};
273 bool up_status;
274 bool down_status;
275 bool left_status;
276 bool right_status;
277 bool modifier_status;
278 float last_x_axis_value;
279 float last_y_axis_value;
280 const Common::Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false};
281 std::chrono::time_point<std::chrono::steady_clock> last_update;
282};
283
284std::unique_ptr<Common::Input::InputDevice> StickFromButton::Create(
285 const Common::ParamPackage& params) {
286 const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
287 auto up = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
288 params.Get("up", null_engine));
289 auto down = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
290 params.Get("down", null_engine));
291 auto left = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
292 params.Get("left", null_engine));
293 auto right = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
294 params.Get("right", null_engine));
295 auto modifier = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
296 params.Get("modifier", null_engine));
297 auto modifier_scale = params.Get("modifier_scale", 0.5f);
298 auto modifier_angle = params.Get("modifier_angle", 5.5f);
299 return std::make_unique<Stick>(std::move(up), std::move(down), std::move(left),
300 std::move(right), std::move(modifier), modifier_scale,
301 modifier_angle);
302}
303
304} // namespace InputCommon
diff --git a/src/input_common/helpers/stick_from_buttons.h b/src/input_common/helpers/stick_from_buttons.h
new file mode 100644
index 000000000..437ace4f7
--- /dev/null
+++ b/src/input_common/helpers/stick_from_buttons.h
@@ -0,0 +1,30 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/input.h"
8
9namespace InputCommon {
10
11/**
12 * An analog device factory that takes direction button devices and combines them into a analog
13 * device.
14 */
15class StickFromButton final : public Common::Input::Factory<Common::Input::InputDevice> {
16public:
17 /**
18 * Creates an analog device from direction button devices
19 * @param params contains parameters for creating the device:
20 * - "up": a serialized ParamPackage for creating a button device for up direction
21 * - "down": a serialized ParamPackage for creating a button device for down direction
22 * - "left": a serialized ParamPackage for creating a button device for left direction
23 * - "right": a serialized ParamPackage for creating a button device for right direction
24 * - "modifier": a serialized ParamPackage for creating a button device as the modifier
25 * - "modifier_scale": a float for the multiplier the modifier gives to the position
26 */
27 std::unique_ptr<Common::Input::InputDevice> Create(const Common::ParamPackage& params) override;
28};
29
30} // namespace InputCommon
diff --git a/src/input_common/helpers/touch_from_buttons.cpp b/src/input_common/helpers/touch_from_buttons.cpp
new file mode 100644
index 000000000..35d60bc90
--- /dev/null
+++ b/src/input_common/helpers/touch_from_buttons.cpp
@@ -0,0 +1,81 @@
1// Copyright 2020 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include "common/settings.h"
7#include "core/frontend/framebuffer_layout.h"
8#include "input_common/helpers/touch_from_buttons.h"
9
10namespace InputCommon {
11
12class TouchFromButtonDevice final : public Common::Input::InputDevice {
13public:
14 using Button = std::unique_ptr<Common::Input::InputDevice>;
15 TouchFromButtonDevice(Button button_, int touch_id_, float x_, float y_)
16 : button(std::move(button_)), touch_id(touch_id_), x(x_), y(y_) {
17 Common::Input::InputCallback button_up_callback{
18 [this](Common::Input::CallbackStatus callback_) { UpdateButtonStatus(callback_); }};
19 last_button_value = false;
20 button->SetCallback(button_up_callback);
21 button->ForceUpdate();
22 }
23
24 void ForceUpdate() override {
25 button->ForceUpdate();
26 }
27
28 Common::Input::TouchStatus GetStatus(bool pressed) const {
29 const Common::Input::ButtonStatus button_status{
30 .value = pressed,
31 };
32 Common::Input::TouchStatus status{
33 .pressed = button_status,
34 .x = {},
35 .y = {},
36 .id = touch_id,
37 };
38 status.x.properties = properties;
39 status.y.properties = properties;
40
41 if (!pressed) {
42 return status;
43 }
44
45 status.x.raw_value = x;
46 status.y.raw_value = y;
47 return status;
48 }
49
50 void UpdateButtonStatus(Common::Input::CallbackStatus button_callback) {
51 const Common::Input::CallbackStatus status{
52 .type = Common::Input::InputType::Touch,
53 .touch_status = GetStatus(button_callback.button_status.value),
54 };
55 if (last_button_value != button_callback.button_status.value) {
56 last_button_value = button_callback.button_status.value;
57 TriggerOnChange(status);
58 }
59 }
60
61private:
62 Button button;
63 bool last_button_value;
64 const int touch_id;
65 const float x;
66 const float y;
67 const Common::Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false};
68};
69
70std::unique_ptr<Common::Input::InputDevice> TouchFromButton::Create(
71 const Common::ParamPackage& params) {
72 const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
73 auto button = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
74 params.Get("button", null_engine));
75 const auto touch_id = params.Get("touch_id", 0);
76 const float x = params.Get("x", 0.0f) / 1280.0f;
77 const float y = params.Get("y", 0.0f) / 720.0f;
78 return std::make_unique<TouchFromButtonDevice>(std::move(button), touch_id, x, y);
79}
80
81} // namespace InputCommon
diff --git a/src/input_common/helpers/touch_from_buttons.h b/src/input_common/helpers/touch_from_buttons.h
new file mode 100644
index 000000000..628f18215
--- /dev/null
+++ b/src/input_common/helpers/touch_from_buttons.h
@@ -0,0 +1,22 @@
1// Copyright 2020 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/input.h"
8
9namespace InputCommon {
10
11/**
12 * A touch device factory that takes a list of button devices and combines them into a touch device.
13 */
14class TouchFromButton final : public Common::Input::Factory<Common::Input::InputDevice> {
15public:
16 /**
17 * Creates a touch device from a list of button devices
18 */
19 std::unique_ptr<Common::Input::InputDevice> Create(const Common::ParamPackage& params) override;
20};
21
22} // namespace InputCommon
diff --git a/src/input_common/helpers/udp_protocol.cpp b/src/input_common/helpers/udp_protocol.cpp
new file mode 100644
index 000000000..cdeab7e11
--- /dev/null
+++ b/src/input_common/helpers/udp_protocol.cpp
@@ -0,0 +1,78 @@
1// Copyright 2018 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstddef>
6#include <cstring>
7#include "common/logging/log.h"
8#include "input_common/helpers/udp_protocol.h"
9
10namespace InputCommon::CemuhookUDP {
11
12static constexpr std::size_t GetSizeOfResponseType(Type t) {
13 switch (t) {
14 case Type::Version:
15 return sizeof(Response::Version);
16 case Type::PortInfo:
17 return sizeof(Response::PortInfo);
18 case Type::PadData:
19 return sizeof(Response::PadData);
20 }
21 return 0;
22}
23
24namespace Response {
25
26/**
27 * Returns Type if the packet is valid, else none
28 *
29 * Note: Modifies the buffer to zero out the crc (since thats the easiest way to check without
30 * copying the buffer)
31 */
32std::optional<Type> Validate(u8* data, std::size_t size) {
33 if (size < sizeof(Header)) {
34 return std::nullopt;
35 }
36 Header header{};
37 std::memcpy(&header, data, sizeof(Header));
38 if (header.magic != SERVER_MAGIC) {
39 LOG_ERROR(Input, "UDP Packet has an unexpected magic value");
40 return std::nullopt;
41 }
42 if (header.protocol_version != PROTOCOL_VERSION) {
43 LOG_ERROR(Input, "UDP Packet protocol mismatch");
44 return std::nullopt;
45 }
46 if (header.type < Type::Version || header.type > Type::PadData) {
47 LOG_ERROR(Input, "UDP Packet is an unknown type");
48 return std::nullopt;
49 }
50
51 // Packet size must equal sizeof(Header) + sizeof(Data)
52 // and also verify that the packet info mentions the correct size. Since the spec includes the
53 // type of the packet as part of the data, we need to include it in size calculations here
54 // ie: payload_length == sizeof(T) + sizeof(Type)
55 const std::size_t data_len = GetSizeOfResponseType(header.type);
56 if (header.payload_length != data_len + sizeof(Type) || size < data_len + sizeof(Header)) {
57 LOG_ERROR(
58 Input,
59 "UDP Packet payload length doesn't match. Received: {} PayloadLength: {} Expected: {}",
60 size, header.payload_length, data_len + sizeof(Type));
61 return std::nullopt;
62 }
63
64 const u32 crc32 = header.crc;
65 boost::crc_32_type result;
66 // zero out the crc in the buffer and then run the crc against it
67 std::memset(&data[offsetof(Header, crc)], 0, sizeof(u32_le));
68
69 result.process_bytes(data, data_len + sizeof(Header));
70 if (crc32 != result.checksum()) {
71 LOG_ERROR(Input, "UDP Packet CRC check failed. Offset: {}", offsetof(Header, crc));
72 return std::nullopt;
73 }
74 return header.type;
75}
76} // namespace Response
77
78} // namespace InputCommon::CemuhookUDP
diff --git a/src/input_common/helpers/udp_protocol.h b/src/input_common/helpers/udp_protocol.h
new file mode 100644
index 000000000..bcba12c58
--- /dev/null
+++ b/src/input_common/helpers/udp_protocol.h
@@ -0,0 +1,290 @@
1// Copyright 2018 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <optional>
9#include <type_traits>
10
11#include <boost/crc.hpp>
12
13#include "common/bit_field.h"
14#include "common/swap.h"
15
16namespace InputCommon::CemuhookUDP {
17
18constexpr std::size_t MAX_PACKET_SIZE = 100;
19constexpr u16 PROTOCOL_VERSION = 1001;
20constexpr u32 CLIENT_MAGIC = 0x43555344; // DSUC (but flipped for LE)
21constexpr u32 SERVER_MAGIC = 0x53555344; // DSUS (but flipped for LE)
22
23enum class Type : u32 {
24 Version = 0x00100000,
25 PortInfo = 0x00100001,
26 PadData = 0x00100002,
27};
28
29struct Header {
30 u32_le magic{};
31 u16_le protocol_version{};
32 u16_le payload_length{};
33 u32_le crc{};
34 u32_le id{};
35 ///> In the protocol, the type of the packet is not part of the header, but its convenient to
36 ///> include in the header so the callee doesn't have to duplicate the type twice when building
37 ///> the data
38 Type type{};
39};
40static_assert(sizeof(Header) == 20, "UDP Message Header struct has wrong size");
41static_assert(std::is_trivially_copyable_v<Header>, "UDP Message Header is not trivially copyable");
42
43using MacAddress = std::array<u8, 6>;
44constexpr MacAddress EMPTY_MAC_ADDRESS = {0, 0, 0, 0, 0, 0};
45
46#pragma pack(push, 1)
47template <typename T>
48struct Message {
49 Header header{};
50 T data;
51};
52#pragma pack(pop)
53
54template <typename T>
55constexpr Type GetMessageType();
56
57namespace Request {
58
59enum RegisterFlags : u8 {
60 AllPads,
61 PadID,
62 PadMACAdddress,
63};
64
65struct Version {};
66/**
67 * Requests the server to send information about what controllers are plugged into the ports
68 * In citra's case, we only have one controller, so for simplicity's sake, we can just send a
69 * request explicitly for the first controller port and leave it at that. In the future it would be
70 * nice to make this configurable
71 */
72constexpr u32 MAX_PORTS = 4;
73struct PortInfo {
74 u32_le pad_count{}; ///> Number of ports to request data for
75 std::array<u8, MAX_PORTS> port;
76};
77static_assert(std::is_trivially_copyable_v<PortInfo>,
78 "UDP Request PortInfo is not trivially copyable");
79
80/**
81 * Request the latest pad information from the server. If the server hasn't received this message
82 * from the client in a reasonable time frame, the server will stop sending updates. The default
83 * timeout seems to be 5 seconds.
84 */
85struct PadData {
86 /// Determines which method will be used as a look up for the controller
87 RegisterFlags flags{};
88 /// Index of the port of the controller to retrieve data about
89 u8 port_id{};
90 /// Mac address of the controller to retrieve data about
91 MacAddress mac;
92};
93static_assert(sizeof(PadData) == 8, "UDP Request PadData struct has wrong size");
94static_assert(std::is_trivially_copyable_v<PadData>,
95 "UDP Request PadData is not trivially copyable");
96
97/**
98 * Creates a message with the proper header data that can be sent to the server.
99 * @param data Request body to send
100 * @param client_id ID of the udp client (usually not checked on the server)
101 */
102template <typename T>
103Message<T> Create(const T data, const u32 client_id = 0) {
104 boost::crc_32_type crc;
105 Header header{
106 CLIENT_MAGIC, PROTOCOL_VERSION, sizeof(T) + sizeof(Type), 0, client_id, GetMessageType<T>(),
107 };
108 Message<T> message{header, data};
109 crc.process_bytes(&message, sizeof(Message<T>));
110 message.header.crc = crc.checksum();
111 return message;
112}
113} // namespace Request
114
115namespace Response {
116
117enum class ConnectionType : u8 {
118 None,
119 Usb,
120 Bluetooth,
121};
122
123enum class State : u8 {
124 Disconnected,
125 Reserved,
126 Connected,
127};
128
129enum class Model : u8 {
130 None,
131 PartialGyro,
132 FullGyro,
133 Generic,
134};
135
136enum class Battery : u8 {
137 None = 0x00,
138 Dying = 0x01,
139 Low = 0x02,
140 Medium = 0x03,
141 High = 0x04,
142 Full = 0x05,
143 Charging = 0xEE,
144 Charged = 0xEF,
145};
146
147struct Version {
148 u16_le version{};
149};
150static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size");
151static_assert(std::is_trivially_copyable_v<Version>,
152 "UDP Response Version is not trivially copyable");
153
154struct PortInfo {
155 u8 id{};
156 State state{};
157 Model model{};
158 ConnectionType connection_type{};
159 MacAddress mac;
160 Battery battery{};
161 u8 is_pad_active{};
162};
163static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size");
164static_assert(std::is_trivially_copyable_v<PortInfo>,
165 "UDP Response PortInfo is not trivially copyable");
166
167struct TouchPad {
168 u8 is_active{};
169 u8 id{};
170 u16_le x{};
171 u16_le y{};
172};
173static_assert(sizeof(TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
174
175#pragma pack(push, 1)
176struct PadData {
177 PortInfo info{};
178 u32_le packet_counter{};
179
180 u16_le digital_button{};
181 // The following union isn't trivially copyable but we don't use this input anyway.
182 // union DigitalButton {
183 // u16_le button;
184 // BitField<0, 1, u16> button_1; // Share
185 // BitField<1, 1, u16> button_2; // L3
186 // BitField<2, 1, u16> button_3; // R3
187 // BitField<3, 1, u16> button_4; // Options
188 // BitField<4, 1, u16> button_5; // Up
189 // BitField<5, 1, u16> button_6; // Right
190 // BitField<6, 1, u16> button_7; // Down
191 // BitField<7, 1, u16> button_8; // Left
192 // BitField<8, 1, u16> button_9; // L2
193 // BitField<9, 1, u16> button_10; // R2
194 // BitField<10, 1, u16> button_11; // L1
195 // BitField<11, 1, u16> button_12; // R1
196 // BitField<12, 1, u16> button_13; // Triangle
197 // BitField<13, 1, u16> button_14; // Circle
198 // BitField<14, 1, u16> button_15; // Cross
199 // BitField<15, 1, u16> button_16; // Square
200 // } digital_button;
201
202 u8 home;
203 /// If the device supports a "click" on the touchpad, this will change to 1 when a click happens
204 u8 touch_hard_press{};
205 u8 left_stick_x{};
206 u8 left_stick_y{};
207 u8 right_stick_x{};
208 u8 right_stick_y{};
209
210 struct AnalogButton {
211 u8 button_dpad_left_analog{};
212 u8 button_dpad_down_analog{};
213 u8 button_dpad_right_analog{};
214 u8 button_dpad_up_analog{};
215 u8 button_square_analog{};
216 u8 button_cross_analog{};
217 u8 button_circle_analog{};
218 u8 button_triangle_analog{};
219 u8 button_r1_analog{};
220 u8 button_l1_analog{};
221 u8 trigger_r2{};
222 u8 trigger_l2{};
223 } analog_button;
224
225 std::array<TouchPad, 2> touch;
226
227 u64_le motion_timestamp;
228
229 struct Accelerometer {
230 float x{};
231 float y{};
232 float z{};
233 } accel;
234
235 struct Gyroscope {
236 float pitch{};
237 float yaw{};
238 float roll{};
239 } gyro;
240};
241#pragma pack(pop)
242
243static_assert(sizeof(PadData) == 80, "UDP Response PadData struct has wrong size ");
244static_assert(std::is_trivially_copyable_v<PadData>,
245 "UDP Response PadData is not trivially copyable");
246
247static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE,
248 "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>");
249
250static_assert(sizeof(PadData::AnalogButton) == 12,
251 "UDP Response AnalogButton struct has wrong size ");
252static_assert(sizeof(PadData::Accelerometer) == 12,
253 "UDP Response Accelerometer struct has wrong size ");
254static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size ");
255
256/**
257 * Create a Response Message from the data
258 * @param data array of bytes sent from the server
259 * @return boost::none if it failed to parse or Type if it succeeded. The client can then safely
260 * copy the data into the appropriate struct for that Type
261 */
262std::optional<Type> Validate(u8* data, std::size_t size);
263
264} // namespace Response
265
266template <>
267constexpr Type GetMessageType<Request::Version>() {
268 return Type::Version;
269}
270template <>
271constexpr Type GetMessageType<Request::PortInfo>() {
272 return Type::PortInfo;
273}
274template <>
275constexpr Type GetMessageType<Request::PadData>() {
276 return Type::PadData;
277}
278template <>
279constexpr Type GetMessageType<Response::Version>() {
280 return Type::Version;
281}
282template <>
283constexpr Type GetMessageType<Response::PortInfo>() {
284 return Type::PortInfo;
285}
286template <>
287constexpr Type GetMessageType<Response::PadData>() {
288 return Type::PadData;
289}
290} // namespace InputCommon::CemuhookUDP