summaryrefslogtreecommitdiff
path: root/src/input_common/udp
diff options
context:
space:
mode:
authorGravatar Ameer2020-06-21 13:02:43 -0400
committerGravatar Ameer2020-06-21 13:02:43 -0400
commit0076a08d04017036b12405bfb933fa9272f8b0cd (patch)
tree69eda77b0bc11a6f13a002f62517c333fa7635a2 /src/input_common/udp
parentAdd libusb dependency (diff)
downloadyuzu-0076a08d04017036b12405bfb933fa9272f8b0cd.tar.gz
yuzu-0076a08d04017036b12405bfb933fa9272f8b0cd.tar.xz
yuzu-0076a08d04017036b12405bfb933fa9272f8b0cd.zip
Cleanup after linter
Diffstat (limited to 'src/input_common/udp')
-rw-r--r--src/input_common/udp/client.cpp140
-rw-r--r--src/input_common/udp/client.h2
-rw-r--r--src/input_common/udp/protocol.h32
-rw-r--r--src/input_common/udp/udp.cpp18
4 files changed, 76 insertions, 116 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index befa4c86d..da5227058 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -59,8 +59,7 @@ public:
59 void StartReceive() { 59 void StartReceive() {
60 socket.async_receive_from( 60 socket.async_receive_from(
61 boost::asio::buffer(receive_buffer), receive_endpoint, 61 boost::asio::buffer(receive_buffer), receive_endpoint,
62 [this](const boost::system::error_code& error, std::size_t bytes_transferred) 62 [this](const boost::system::error_code& error, std::size_t bytes_transferred) {
63 {
64 HandleReceive(error, bytes_transferred); 63 HandleReceive(error, bytes_transferred);
65 }); 64 });
66 } 65 }
@@ -212,27 +211,21 @@ void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index,
212void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 211void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id,
213 std::function<void()> success_callback, 212 std::function<void()> success_callback,
214 std::function<void()> failure_callback) { 213 std::function<void()> failure_callback) {
215 std::thread([=] 214 std::thread([=] {
216 { 215 Common::Event success_event;
217 Common::Event success_event; 216 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
218 SocketCallback callback{[](Response::Version version) 217 [&](Response::PadData data) { success_event.Set(); }};
219 { 218 Socket socket{host, port, pad_index, client_id, std::move(callback)};
220 }, 219 std::thread worker_thread{SocketLoop, &socket};
221 [](Response::PortInfo info) 220 bool result = success_event.WaitFor(std::chrono::seconds(8));
222 { 221 socket.Stop();
223 }, 222 worker_thread.join();
224 [&](Response::PadData data) { success_event.Set(); }}; 223 if (result) {
225 Socket socket{host, port, pad_index, client_id, std::move(callback)}; 224 success_callback();
226 std::thread worker_thread{SocketLoop, &socket}; 225 } else {
227 bool result = success_event.WaitFor(std::chrono::seconds(8)); 226 failure_callback();
228 socket.Stop(); 227 }
229 worker_thread.join(); 228 })
230 if (result) {
231 success_callback();
232 } else {
233 failure_callback();
234 }
235 })
236 .detach(); 229 .detach();
237} 230}
238 231
@@ -241,60 +234,53 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
241 std::function<void(Status)> status_callback, 234 std::function<void(Status)> status_callback,
242 std::function<void(u16, u16, u16, u16)> data_callback) { 235 std::function<void(u16, u16, u16, u16)> data_callback) {
243 236
244 std::thread([=] 237 std::thread([=] {
245 { 238 constexpr u16 CALIBRATION_THRESHOLD = 100;
246 constexpr u16 CALIBRATION_THRESHOLD = 100; 239
247 240 u16 min_x{UINT16_MAX};
248 u16 min_x{UINT16_MAX}; 241 u16 min_y{UINT16_MAX};
249 u16 min_y{UINT16_MAX}; 242 u16 max_x{};
250 u16 max_x{}; 243 u16 max_y{};
251 u16 max_y{}; 244
252 245 Status current_status{Status::Initialized};
253 Status current_status{Status::Initialized}; 246 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
254 SocketCallback callback{[](Response::Version version) 247 [&](Response::PadData data) {
255 { 248 if (current_status == Status::Initialized) {
256 }, 249 // Receiving data means the communication is ready now
257 [](Response::PortInfo info) 250 current_status = Status::Ready;
258 { 251 status_callback(current_status);
259 }, 252 }
260 [&](Response::PadData data) 253 if (!data.touch_1.is_active) {
261 { 254 return;
262 if (current_status == Status::Initialized) { 255 }
263 // Receiving data means the communication is ready now 256 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x,
264 current_status = Status::Ready; 257 data.touch_1.y);
265 status_callback(current_status); 258 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x));
266 } 259 min_y = std::min(min_y, static_cast<u16>(data.touch_1.y));
267 if (!data.touch_1.is_active) { 260 if (current_status == Status::Ready) {
268 return; 261 // First touch - min data (min_x/min_y)
269 } 262 current_status = Status::Stage1Completed;
270 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 263 status_callback(current_status);
271 data.touch_1.y); 264 }
272 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x)); 265 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD &&
273 min_y = std::min(min_y, static_cast<u16>(data.touch_1.y)); 266 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) {
274 if (current_status == Status::Ready) { 267 // Set the current position as max value and finishes
275 // First touch - min data (min_x/min_y) 268 // configuration
276 current_status = Status::Stage1Completed; 269 max_x = data.touch_1.x;
277 status_callback(current_status); 270 max_y = data.touch_1.y;
278 } 271 current_status = Status::Completed;
279 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && 272 data_callback(min_x, min_y, max_x, max_y);
280 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { 273 status_callback(current_status);
281 // Set the current position as max value and finishes 274
282 // configuration 275 complete_event.Set();
283 max_x = data.touch_1.x; 276 }
284 max_y = data.touch_1.y; 277 }};
285 current_status = Status::Completed; 278 Socket socket{host, port, pad_index, client_id, std::move(callback)};
286 data_callback(min_x, min_y, max_x, max_y); 279 std::thread worker_thread{SocketLoop, &socket};
287 status_callback(current_status); 280 complete_event.Wait();
288 281 socket.Stop();
289 complete_event.Set(); 282 worker_thread.join();
290 } 283 })
291 }};
292 Socket socket{host, port, pad_index, client_id, std::move(callback)};
293 std::thread worker_thread{SocketLoop, &socket};
294 complete_event.Wait();
295 socket.Stop();
296 worker_thread.join();
297 })
298 .detach(); 284 .detach();
299} 285}
300 286
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index b58e319b6..b8c654755 100644
--- a/src/input_common/udp/client.h
+++ b/src/input_common/udp/client.h
@@ -40,7 +40,6 @@ struct DeviceStatus {
40 u16 max_x{}; 40 u16 max_x{};
41 u16 max_y{}; 41 u16 max_y{};
42 }; 42 };
43
44 std::optional<CalibrationData> touch_calibration; 43 std::optional<CalibrationData> touch_calibration;
45}; 44};
46 45
@@ -73,7 +72,6 @@ public:
73 Stage1Completed, 72 Stage1Completed,
74 Completed, 73 Completed,
75 }; 74 };
76
77 /** 75 /**
78 * Constructs and starts the job with the specified parameter. 76 * Constructs and starts the job with the specified parameter.
79 * 77 *
diff --git a/src/input_common/udp/protocol.h b/src/input_common/udp/protocol.h
index 2b31846db..3ba4d1fc8 100644
--- a/src/input_common/udp/protocol.h
+++ b/src/input_common/udp/protocol.h
@@ -35,7 +35,6 @@ struct Header {
35 ///> the data 35 ///> the data
36 Type type{}; 36 Type type{};
37}; 37};
38
39static_assert(sizeof(Header) == 20, "UDP Message Header struct has wrong size"); 38static_assert(sizeof(Header) == 20, "UDP Message Header struct has wrong size");
40static_assert(std::is_trivially_copyable_v<Header>, "UDP Message Header is not trivially copyable"); 39static_assert(std::is_trivially_copyable_v<Header>, "UDP Message Header is not trivially copyable");
41 40
@@ -55,9 +54,7 @@ constexpr Type GetMessageType();
55 54
56namespace Request { 55namespace Request {
57 56
58struct Version { 57struct Version {};
59};
60
61/** 58/**
62 * Requests the server to send information about what controllers are plugged into the ports 59 * Requests the server to send information about what controllers are plugged into the ports
63 * In citra's case, we only have one controller, so for simplicity's sake, we can just send a 60 * In citra's case, we only have one controller, so for simplicity's sake, we can just send a
@@ -65,14 +62,12 @@ struct Version {
65 * nice to make this configurable 62 * nice to make this configurable
66 */ 63 */
67constexpr u32 MAX_PORTS = 4; 64constexpr u32 MAX_PORTS = 4;
68
69struct PortInfo { 65struct PortInfo {
70 u32_le pad_count{}; ///> Number of ports to request data for 66 u32_le pad_count{}; ///> Number of ports to request data for
71 std::array<u8, MAX_PORTS> port; 67 std::array<u8, MAX_PORTS> port;
72}; 68};
73
74static_assert(std::is_trivially_copyable_v<PortInfo>, 69static_assert(std::is_trivially_copyable_v<PortInfo>,
75 "UDP Request PortInfo is not trivially copyable"); 70 "UDP Request PortInfo is not trivially copyable");
76 71
77/** 72/**
78 * Request the latest pad information from the server. If the server hasn't received this message 73 * Request the latest pad information from the server. If the server hasn't received this message
@@ -85,7 +80,6 @@ struct PadData {
85 Id, 80 Id,
86 Mac, 81 Mac,
87 }; 82 };
88
89 /// Determines which method will be used as a look up for the controller 83 /// Determines which method will be used as a look up for the controller
90 Flags flags{}; 84 Flags flags{};
91 /// Index of the port of the controller to retrieve data about 85 /// Index of the port of the controller to retrieve data about
@@ -93,10 +87,9 @@ struct PadData {
93 /// Mac address of the controller to retrieve data about 87 /// Mac address of the controller to retrieve data about
94 MacAddress mac; 88 MacAddress mac;
95}; 89};
96
97static_assert(sizeof(PadData) == 8, "UDP Request PadData struct has wrong size"); 90static_assert(sizeof(PadData) == 8, "UDP Request PadData struct has wrong size");
98static_assert(std::is_trivially_copyable_v<PadData>, 91static_assert(std::is_trivially_copyable_v<PadData>,
99 "UDP Request PadData is not trivially copyable"); 92 "UDP Request PadData is not trivially copyable");
100 93
101/** 94/**
102 * Creates a message with the proper header data that can be sent to the server. 95 * Creates a message with the proper header data that can be sent to the server.
@@ -121,10 +114,9 @@ namespace Response {
121struct Version { 114struct Version {
122 u16_le version{}; 115 u16_le version{};
123}; 116};
124
125static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size"); 117static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size");
126static_assert(std::is_trivially_copyable_v<Version>, 118static_assert(std::is_trivially_copyable_v<Version>,
127 "UDP Response Version is not trivially copyable"); 119 "UDP Response Version is not trivially copyable");
128 120
129struct PortInfo { 121struct PortInfo {
130 u8 id{}; 122 u8 id{};
@@ -135,10 +127,9 @@ struct PortInfo {
135 u8 battery{}; 127 u8 battery{};
136 u8 is_pad_active{}; 128 u8 is_pad_active{};
137}; 129};
138
139static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size"); 130static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size");
140static_assert(std::is_trivially_copyable_v<PortInfo>, 131static_assert(std::is_trivially_copyable_v<PortInfo>,
141 "UDP Response PortInfo is not trivially copyable"); 132 "UDP Response PortInfo is not trivially copyable");
142 133
143#pragma pack(push, 1) 134#pragma pack(push, 1)
144struct PadData { 135struct PadData {
@@ -215,16 +206,16 @@ struct PadData {
215 206
216static_assert(sizeof(PadData) == 80, "UDP Response PadData struct has wrong size "); 207static_assert(sizeof(PadData) == 80, "UDP Response PadData struct has wrong size ");
217static_assert(std::is_trivially_copyable_v<PadData>, 208static_assert(std::is_trivially_copyable_v<PadData>,
218 "UDP Response PadData is not trivially copyable"); 209 "UDP Response PadData is not trivially copyable");
219 210
220static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE, 211static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE,
221 "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>"); 212 "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>");
222 213
223static_assert(sizeof(PadData::AnalogButton) == 12, 214static_assert(sizeof(PadData::AnalogButton) == 12,
224 "UDP Response AnalogButton struct has wrong size "); 215 "UDP Response AnalogButton struct has wrong size ");
225static_assert(sizeof(PadData::TouchPad) == 6, "UDP Response TouchPad struct has wrong size "); 216static_assert(sizeof(PadData::TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
226static_assert(sizeof(PadData::Accelerometer) == 12, 217static_assert(sizeof(PadData::Accelerometer) == 12,
227 "UDP Response Accelerometer struct has wrong size "); 218 "UDP Response Accelerometer struct has wrong size ");
228static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size "); 219static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size ");
229 220
230/** 221/**
@@ -241,27 +232,22 @@ template <>
241constexpr Type GetMessageType<Request::Version>() { 232constexpr Type GetMessageType<Request::Version>() {
242 return Type::Version; 233 return Type::Version;
243} 234}
244
245template <> 235template <>
246constexpr Type GetMessageType<Request::PortInfo>() { 236constexpr Type GetMessageType<Request::PortInfo>() {
247 return Type::PortInfo; 237 return Type::PortInfo;
248} 238}
249
250template <> 239template <>
251constexpr Type GetMessageType<Request::PadData>() { 240constexpr Type GetMessageType<Request::PadData>() {
252 return Type::PadData; 241 return Type::PadData;
253} 242}
254
255template <> 243template <>
256constexpr Type GetMessageType<Response::Version>() { 244constexpr Type GetMessageType<Response::Version>() {
257 return Type::Version; 245 return Type::Version;
258} 246}
259
260template <> 247template <>
261constexpr Type GetMessageType<Response::PortInfo>() { 248constexpr Type GetMessageType<Response::PortInfo>() {
262 return Type::PortInfo; 249 return Type::PortInfo;
263} 250}
264
265template <> 251template <>
266constexpr Type GetMessageType<Response::PadData>() { 252constexpr Type GetMessageType<Response::PadData>() {
267 return Type::PadData; 253 return Type::PadData;
diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp
index 343c3985e..8c6ef1394 100644
--- a/src/input_common/udp/udp.cpp
+++ b/src/input_common/udp/udp.cpp
@@ -16,10 +16,7 @@ namespace InputCommon::CemuhookUDP {
16 16
17class UDPTouchDevice final : public Input::TouchDevice { 17class UDPTouchDevice final : public Input::TouchDevice {
18public: 18public:
19 explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) 19 explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
20 : status(std::move(status_)) {
21 }
22
23 std::tuple<float, float, bool> GetStatus() const override { 20 std::tuple<float, float, bool> GetStatus() const override {
24 std::lock_guard guard(status->update_mutex); 21 std::lock_guard guard(status->update_mutex);
25 return status->touch_status; 22 return status->touch_status;
@@ -31,10 +28,7 @@ private:
31 28
32class UDPMotionDevice final : public Input::MotionDevice { 29class UDPMotionDevice final : public Input::MotionDevice {
33public: 30public:
34 explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) 31 explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
35 : status(std::move(status_)) {
36 }
37
38 std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override { 32 std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override {
39 std::lock_guard guard(status->update_mutex); 33 std::lock_guard guard(status->update_mutex);
40 return status->motion_status; 34 return status->motion_status;
@@ -46,9 +40,7 @@ private:
46 40
47class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { 41class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> {
48public: 42public:
49 explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) 43 explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
50 : status(std::move(status_)) {
51 }
52 44
53 std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override { 45 std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override {
54 { 46 {
@@ -69,9 +61,7 @@ private:
69 61
70class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { 62class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> {
71public: 63public:
72 explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) 64 explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
73 : status(std::move(status_)) {
74 }
75 65
76 std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { 66 std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override {
77 return std::make_unique<UDPMotionDevice>(status); 67 return std::make_unique<UDPMotionDevice>(status);