summaryrefslogtreecommitdiff
path: root/src/input_common/udp/client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/udp/client.cpp')
-rw-r--r--src/input_common/udp/client.cpp185
1 files changed, 150 insertions, 35 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index e63c73c4f..9d0b9f31d 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -2,15 +2,13 @@
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 <algorithm>
6#include <array>
7#include <chrono> 5#include <chrono>
8#include <cstring> 6#include <cstring>
9#include <functional> 7#include <functional>
10#include <thread> 8#include <thread>
11#include <boost/asio.hpp> 9#include <boost/asio.hpp>
12#include <boost/bind.hpp>
13#include "common/logging/log.h" 10#include "common/logging/log.h"
11#include "core/settings.h"
14#include "input_common/udp/client.h" 12#include "input_common/udp/client.h"
15#include "input_common/udp/protocol.h" 13#include "input_common/udp/protocol.h"
16 14
@@ -132,21 +130,59 @@ static void SocketLoop(Socket* socket) {
132 socket->Loop(); 130 socket->Loop();
133} 131}
134 132
135Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port, 133Client::Client() {
136 u8 pad_index, u32 client_id) 134 LOG_INFO(Input, "Udp Initialization started");
137 : status(std::move(status)) { 135 for (std::size_t client = 0; client < clients.size(); client++) {
138 StartCommunication(host, port, pad_index, client_id); 136 u8 pad = client % 4;
137 StartCommunication(client, Settings::values.udp_input_address,
138 Settings::values.udp_input_port, pad, 24872);
139 // Set motion parameters
140 // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode
141 // Real HW values are unknown, 0.0001 is an approximate to Standard
142 clients[client].motion.SetGyroThreshold(0.0001f);
143 }
139} 144}
140 145
141Client::~Client() { 146Client::~Client() {
142 socket->Stop(); 147 Reset();
143 thread.join(); 148}
149
150std::vector<Common::ParamPackage> Client::GetInputDevices() const {
151 std::vector<Common::ParamPackage> devices;
152 for (std::size_t client = 0; client < clients.size(); client++) {
153 if (!DeviceConnected(client)) {
154 continue;
155 }
156 std::string name = fmt::format("UDP Controller {}", client);
157 devices.emplace_back(Common::ParamPackage{
158 {"class", "cemuhookudp"},
159 {"display", std::move(name)},
160 {"port", std::to_string(client)},
161 });
162 }
163 return devices;
144} 164}
145 165
166bool Client::DeviceConnected(std::size_t pad) const {
167 // Use last timestamp to detect if the socket has stopped sending data
168 const auto now = std::chrono::system_clock::now();
169 u64 time_difference =
170 std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update)
171 .count();
172 return time_difference < 1000 && clients[pad].active == 1;
173}
174
175void Client::ReloadUDPClient() {
176 for (std::size_t client = 0; client < clients.size(); client++) {
177 ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client);
178 }
179}
146void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { 180void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) {
147 socket->Stop(); 181 // client number must be determined from host / port and pad index
148 thread.join(); 182 std::size_t client = pad_index;
149 StartCommunication(host, port, pad_index, client_id); 183 clients[client].socket->Stop();
184 clients[client].thread.join();
185 StartCommunication(client, host, port, pad_index, client_id);
150} 186}
151 187
152void Client::OnVersion(Response::Version data) { 188void Client::OnVersion(Response::Version data) {
@@ -158,23 +194,35 @@ void Client::OnPortInfo(Response::PortInfo data) {
158} 194}
159 195
160void Client::OnPadData(Response::PadData data) { 196void Client::OnPadData(Response::PadData data) {
197 // client number must be determined from host / port and pad index
198 std::size_t client = data.info.id;
161 LOG_TRACE(Input, "PadData packet received"); 199 LOG_TRACE(Input, "PadData packet received");
162 if (data.packet_counter <= packet_sequence) { 200 if (data.packet_counter == clients[client].packet_sequence) {
163 LOG_WARNING( 201 LOG_WARNING(
164 Input, 202 Input,
165 "PadData packet dropped because its stale info. Current count: {} Packet count: {}", 203 "PadData packet dropped because its stale info. Current count: {} Packet count: {}",
166 packet_sequence, data.packet_counter); 204 clients[client].packet_sequence, data.packet_counter);
167 return; 205 return;
168 } 206 }
169 packet_sequence = data.packet_counter; 207 clients[client].active = data.info.is_pad_active;
170 // TODO: Check how the Switch handles motions and how the CemuhookUDP motion 208 clients[client].packet_sequence = data.packet_counter;
171 // directions correspond to the ones of the Switch 209 const auto now = std::chrono::system_clock::now();
172 Common::Vec3f accel = Common::MakeVec<float>(data.accel.x, data.accel.y, data.accel.z); 210 u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>(
173 Common::Vec3f gyro = Common::MakeVec<float>(data.gyro.pitch, data.gyro.yaw, data.gyro.roll); 211 now - clients[client].last_motion_update)
174 { 212 .count();
175 std::lock_guard guard(status->update_mutex); 213 clients[client].last_motion_update = now;
214 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});
216 // Gyroscope values are not it the correct scale from better joy.
217 // Dividing by 312 allows us to make one full turn = 1 turn
218 // This must be a configurable valued called sensitivity
219 clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f);
220 clients[client].motion.UpdateRotation(time_difference);
221 clients[client].motion.UpdateOrientation(time_difference);
176 222
177 status->motion_status = {accel, gyro}; 223 {
224 std::lock_guard guard(clients[client].status.update_mutex);
225 clients[client].status.motion_status = clients[client].motion.GetMotion();
178 226
179 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 227 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates
180 // between a simple "tap" and a hard press that causes the touch screen to click. 228 // between a simple "tap" and a hard press that causes the touch screen to click.
@@ -183,11 +231,11 @@ void Client::OnPadData(Response::PadData data) {
183 float x = 0; 231 float x = 0;
184 float y = 0; 232 float y = 0;
185 233
186 if (is_active && status->touch_calibration) { 234 if (is_active && clients[client].status.touch_calibration) {
187 const u16 min_x = status->touch_calibration->min_x; 235 const u16 min_x = clients[client].status.touch_calibration->min_x;
188 const u16 max_x = status->touch_calibration->max_x; 236 const u16 max_x = clients[client].status.touch_calibration->max_x;
189 const u16 min_y = status->touch_calibration->min_y; 237 const u16 min_y = clients[client].status.touch_calibration->min_y;
190 const u16 max_y = status->touch_calibration->max_y; 238 const u16 max_y = clients[client].status.touch_calibration->max_y;
191 239
192 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / 240 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) /
193 static_cast<float>(max_x - min_x); 241 static_cast<float>(max_x - min_x);
@@ -195,17 +243,86 @@ void Client::OnPadData(Response::PadData data) {
195 static_cast<float>(max_y - min_y); 243 static_cast<float>(max_y - min_y);
196 } 244 }
197 245
198 status->touch_status = {x, y, is_active}; 246 clients[client].status.touch_status = {x, y, is_active};
247
248 if (configuring) {
249 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
250 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
251 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active);
252 }
199 } 253 }
200} 254}
201 255
202void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id) { 256void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index,
257 u32 client_id) {
203 SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, 258 SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
204 [this](Response::PortInfo info) { OnPortInfo(info); }, 259 [this](Response::PortInfo info) { OnPortInfo(info); },
205 [this](Response::PadData data) { OnPadData(data); }}; 260 [this](Response::PadData data) { OnPadData(data); }};
206 LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); 261 LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port);
207 socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); 262 clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback);
208 thread = std::thread{SocketLoop, this->socket.get()}; 263 clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()};
264}
265
266void Client::Reset() {
267 for (std::size_t client = 0; client < clients.size(); client++) {
268 clients[client].socket->Stop();
269 clients[client].thread.join();
270 }
271}
272
273void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
274 const Common::Vec3<float>& gyro, bool touch) {
275 if (gyro.Length() > 0.2f) {
276 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}",
277 client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch);
278 }
279 UDPPadStatus pad;
280 if (touch) {
281 pad.touch = PadTouch::Click;
282 pad_queue[client].Push(pad);
283 }
284 for (size_t i = 0; i < 3; ++i) {
285 if (gyro[i] > 5.0f || gyro[i] < -5.0f) {
286 pad.motion = static_cast<PadMotion>(i);
287 pad.motion_value = gyro[i];
288 pad_queue[client].Push(pad);
289 }
290 if (acc[i] > 1.75f || acc[i] < -1.75f) {
291 pad.motion = static_cast<PadMotion>(i + 3);
292 pad.motion_value = acc[i];
293 pad_queue[client].Push(pad);
294 }
295 }
296}
297
298void Client::BeginConfiguration() {
299 for (auto& pq : pad_queue) {
300 pq.Clear();
301 }
302 configuring = true;
303}
304
305void Client::EndConfiguration() {
306 for (auto& pq : pad_queue) {
307 pq.Clear();
308 }
309 configuring = false;
310}
311
312DeviceStatus& Client::GetPadState(std::size_t pad) {
313 return clients[pad].status;
314}
315
316const DeviceStatus& Client::GetPadState(std::size_t pad) const {
317 return clients[pad].status;
318}
319
320std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() {
321 return pad_queue;
322}
323
324const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const {
325 return pad_queue;
209} 326}
210 327
211void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 328void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id,
@@ -225,8 +342,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie
225 } else { 342 } else {
226 failure_callback(); 343 failure_callback();
227 } 344 }
228 }) 345 }).detach();
229 .detach();
230} 346}
231 347
232CalibrationConfigurationJob::CalibrationConfigurationJob( 348CalibrationConfigurationJob::CalibrationConfigurationJob(
@@ -280,8 +396,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
280 complete_event.Wait(); 396 complete_event.Wait();
281 socket.Stop(); 397 socket.Stop();
282 worker_thread.join(); 398 worker_thread.join();
283 }) 399 }).detach();
284 .detach();
285} 400}
286 401
287CalibrationConfigurationJob::~CalibrationConfigurationJob() { 402CalibrationConfigurationJob::~CalibrationConfigurationJob() {