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.cpp118
1 files changed, 74 insertions, 44 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 412d57896..e7e50d789 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -136,6 +136,7 @@ static void SocketLoop(Socket* socket) {
136 136
137Client::Client() { 137Client::Client() {
138 LOG_INFO(Input, "Udp Initialization started"); 138 LOG_INFO(Input, "Udp Initialization started");
139 finger_id.fill(MAX_TOUCH_FINGERS);
139 ReloadSockets(); 140 ReloadSockets();
140} 141}
141 142
@@ -176,7 +177,7 @@ void Client::ReloadSockets() {
176 std::string server_token; 177 std::string server_token;
177 std::size_t client = 0; 178 std::size_t client = 0;
178 while (std::getline(servers_ss, server_token, ',')) { 179 while (std::getline(servers_ss, server_token, ',')) {
179 if (client == max_udp_clients) { 180 if (client == MAX_UDP_CLIENTS) {
180 break; 181 break;
181 } 182 }
182 std::stringstream server_ss(server_token); 183 std::stringstream server_ss(server_token);
@@ -194,7 +195,7 @@ void Client::ReloadSockets() {
194 for (std::size_t pad = 0; pad < 4; ++pad) { 195 for (std::size_t pad = 0; pad < 4; ++pad) {
195 const std::size_t client_number = 196 const std::size_t client_number =
196 GetClientNumber(udp_input_address, udp_input_port, pad); 197 GetClientNumber(udp_input_address, udp_input_port, pad);
197 if (client_number != max_udp_clients) { 198 if (client_number != MAX_UDP_CLIENTS) {
198 LOG_ERROR(Input, "Duplicated UDP servers found"); 199 LOG_ERROR(Input, "Duplicated UDP servers found");
199 continue; 200 continue;
200 } 201 }
@@ -213,7 +214,7 @@ std::size_t Client::GetClientNumber(std::string_view host, u16 port, std::size_t
213 return client; 214 return client;
214 } 215 }
215 } 216 }
216 return max_udp_clients; 217 return MAX_UDP_CLIENTS;
217} 218}
218 219
219void Client::OnVersion([[maybe_unused]] Response::Version data) { 220void Client::OnVersion([[maybe_unused]] Response::Version data) {
@@ -259,33 +260,14 @@ void Client::OnPadData(Response::PadData data, std::size_t client) {
259 std::lock_guard guard(clients[client].status.update_mutex); 260 std::lock_guard guard(clients[client].status.update_mutex);
260 clients[client].status.motion_status = clients[client].motion.GetMotion(); 261 clients[client].status.motion_status = clients[client].motion.GetMotion();
261 262
262 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 263 for (std::size_t id = 0; id < data.touch.size(); ++id) {
263 // between a simple "tap" and a hard press that causes the touch screen to click. 264 UpdateTouchInput(data.touch[id], client, id);
264 const bool is_active = data.touch_1.is_active != 0;
265
266 float x = 0;
267 float y = 0;
268
269 if (is_active && clients[client].status.touch_calibration) {
270 const u16 min_x = clients[client].status.touch_calibration->min_x;
271 const u16 max_x = clients[client].status.touch_calibration->max_x;
272 const u16 min_y = clients[client].status.touch_calibration->min_y;
273 const u16 max_y = clients[client].status.touch_calibration->max_y;
274
275 x = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) -
276 min_x) /
277 static_cast<float>(max_x - min_x);
278 y = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) -
279 min_y) /
280 static_cast<float>(max_y - min_y);
281 } 265 }
282 266
283 clients[client].status.touch_status = {x, y, is_active};
284
285 if (configuring) { 267 if (configuring) {
286 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); 268 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
287 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); 269 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
288 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); 270 UpdateYuzuSettings(client, accelerometer, gyroscope);
289 } 271 }
290 } 272 }
291} 273}
@@ -320,21 +302,17 @@ void Client::Reset() {
320} 302}
321 303
322void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, 304void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
323 const Common::Vec3<float>& gyro, bool touch) { 305 const Common::Vec3<float>& gyro) {
324 if (gyro.Length() > 0.2f) { 306 if (gyro.Length() > 0.2f) {
325 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", 307 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {})", client,
326 client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); 308 gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2]);
327 } 309 }
328 UDPPadStatus pad{ 310 UDPPadStatus pad{
329 .host = clients[client].host, 311 .host = clients[client].host,
330 .port = clients[client].port, 312 .port = clients[client].port,
331 .pad_index = clients[client].pad_index, 313 .pad_index = clients[client].pad_index,
332 }; 314 };
333 if (touch) { 315 for (std::size_t i = 0; i < 3; ++i) {
334 pad.touch = PadTouch::Click;
335 pad_queue.Push(pad);
336 }
337 for (size_t i = 0; i < 3; ++i) {
338 if (gyro[i] > 5.0f || gyro[i] < -5.0f) { 316 if (gyro[i] > 5.0f || gyro[i] < -5.0f) {
339 pad.motion = static_cast<PadMotion>(i); 317 pad.motion = static_cast<PadMotion>(i);
340 pad.motion_value = gyro[i]; 318 pad.motion_value = gyro[i];
@@ -348,6 +326,50 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& a
348 } 326 }
349} 327}
350 328
329std::optional<std::size_t> Client::GetUnusedFingerID() const {
330 std::size_t first_free_id = 0;
331 while (first_free_id < MAX_TOUCH_FINGERS) {
332 if (!std::get<2>(touch_status[first_free_id])) {
333 return first_free_id;
334 } else {
335 first_free_id++;
336 }
337 }
338 return std::nullopt;
339}
340
341void Client::UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id) {
342 // TODO: Use custom calibration per device
343 const Common::ParamPackage touch_param(Settings::values.touch_device);
344 const u16 min_x = static_cast<u16>(touch_param.Get("min_x", 100));
345 const u16 min_y = static_cast<u16>(touch_param.Get("min_y", 50));
346 const u16 max_x = static_cast<u16>(touch_param.Get("max_x", 1800));
347 const u16 max_y = static_cast<u16>(touch_param.Get("max_y", 850));
348 const std::size_t touch_id = client * 2 + id;
349 if (touch_pad.is_active) {
350 if (finger_id[touch_id] == MAX_TOUCH_FINGERS) {
351 const auto first_free_id = GetUnusedFingerID();
352 if (!first_free_id) {
353 // Invalid finger id skip to next input
354 return;
355 }
356 finger_id[touch_id] = *first_free_id;
357 }
358 auto& [x, y, pressed] = touch_status[finger_id[touch_id]];
359 x = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) /
360 static_cast<float>(max_x - min_x);
361 y = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) /
362 static_cast<float>(max_y - min_y);
363 pressed = true;
364 return;
365 }
366
367 if (finger_id[touch_id] != MAX_TOUCH_FINGERS) {
368 touch_status[finger_id[touch_id]] = {};
369 finger_id[touch_id] = MAX_TOUCH_FINGERS;
370 }
371}
372
351void Client::BeginConfiguration() { 373void Client::BeginConfiguration() {
352 pad_queue.Clear(); 374 pad_queue.Clear();
353 configuring = true; 375 configuring = true;
@@ -360,7 +382,7 @@ void Client::EndConfiguration() {
360 382
361DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) { 383DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) {
362 const std::size_t client_number = GetClientNumber(host, port, pad); 384 const std::size_t client_number = GetClientNumber(host, port, pad);
363 if (client_number == max_udp_clients) { 385 if (client_number == MAX_UDP_CLIENTS) {
364 return clients[0].status; 386 return clients[0].status;
365 } 387 }
366 return clients[client_number].status; 388 return clients[client_number].status;
@@ -368,12 +390,20 @@ DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t
368 390
369const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const { 391const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const {
370 const std::size_t client_number = GetClientNumber(host, port, pad); 392 const std::size_t client_number = GetClientNumber(host, port, pad);
371 if (client_number == max_udp_clients) { 393 if (client_number == MAX_UDP_CLIENTS) {
372 return clients[0].status; 394 return clients[0].status;
373 } 395 }
374 return clients[client_number].status; 396 return clients[client_number].status;
375} 397}
376 398
399Input::TouchStatus& Client::GetTouchState() {
400 return touch_status;
401}
402
403const Input::TouchStatus& Client::GetTouchState() const {
404 return touch_status;
405}
406
377Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() { 407Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() {
378 return pad_queue; 408 return pad_queue;
379} 409}
@@ -426,24 +456,24 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
426 current_status = Status::Ready; 456 current_status = Status::Ready;
427 status_callback(current_status); 457 status_callback(current_status);
428 } 458 }
429 if (data.touch_1.is_active == 0) { 459 if (data.touch[0].is_active == 0) {
430 return; 460 return;
431 } 461 }
432 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 462 LOG_DEBUG(Input, "Current touch: {} {}", data.touch[0].x,
433 data.touch_1.y); 463 data.touch[0].y);
434 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x)); 464 min_x = std::min(min_x, static_cast<u16>(data.touch[0].x));
435 min_y = std::min(min_y, static_cast<u16>(data.touch_1.y)); 465 min_y = std::min(min_y, static_cast<u16>(data.touch[0].y));
436 if (current_status == Status::Ready) { 466 if (current_status == Status::Ready) {
437 // First touch - min data (min_x/min_y) 467 // First touch - min data (min_x/min_y)
438 current_status = Status::Stage1Completed; 468 current_status = Status::Stage1Completed;
439 status_callback(current_status); 469 status_callback(current_status);
440 } 470 }
441 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && 471 if (data.touch[0].x - min_x > CALIBRATION_THRESHOLD &&
442 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { 472 data.touch[0].y - min_y > CALIBRATION_THRESHOLD) {
443 // Set the current position as max value and finishes 473 // Set the current position as max value and finishes
444 // configuration 474 // configuration
445 max_x = data.touch_1.x; 475 max_x = data.touch[0].x;
446 max_y = data.touch_1.y; 476 max_y = data.touch[0].y;
447 current_status = Status::Completed; 477 current_status = Status::Completed;
448 data_callback(min_x, min_y, max_x, max_y); 478 data_callback(min_x, min_y, max_x, max_y);
449 status_callback(current_status); 479 status_callback(current_status);