summaryrefslogtreecommitdiff
path: root/src/input_common/udp/client.cpp
diff options
context:
space:
mode:
authorGravatar german2021-01-01 12:32:29 -0600
committerGravatar german2021-01-15 09:05:17 -0600
commitd8df9a16bd4f4517b024c17446a94915493d7f3d (patch)
treeaed2de583aa94dd11259bece37e55f4263f07336 /src/input_common/udp/client.cpp
parentAllow all touch inputs at the same time and remove config options that are no... (diff)
downloadyuzu-d8df9a16bd4f4517b024c17446a94915493d7f3d.tar.gz
yuzu-d8df9a16bd4f4517b024c17446a94915493d7f3d.tar.xz
yuzu-d8df9a16bd4f4517b024c17446a94915493d7f3d.zip
Allow to return up to 16 touch inputs per engine
Diffstat (limited to 'src/input_common/udp/client.cpp')
-rw-r--r--src/input_common/udp/client.cpp121
1 files changed, 78 insertions, 43 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 412d57896..53648cb53 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -136,6 +136,9 @@ 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 for (size_t id = 0; id < MAX_TOUCH_FINGERS; id++) {
140 finger_id[id] = MAX_UDP_CLIENTS * 2;
141 }
139 ReloadSockets(); 142 ReloadSockets();
140} 143}
141 144
@@ -176,7 +179,7 @@ void Client::ReloadSockets() {
176 std::string server_token; 179 std::string server_token;
177 std::size_t client = 0; 180 std::size_t client = 0;
178 while (std::getline(servers_ss, server_token, ',')) { 181 while (std::getline(servers_ss, server_token, ',')) {
179 if (client == max_udp_clients) { 182 if (client == MAX_UDP_CLIENTS) {
180 break; 183 break;
181 } 184 }
182 std::stringstream server_ss(server_token); 185 std::stringstream server_ss(server_token);
@@ -194,7 +197,7 @@ void Client::ReloadSockets() {
194 for (std::size_t pad = 0; pad < 4; ++pad) { 197 for (std::size_t pad = 0; pad < 4; ++pad) {
195 const std::size_t client_number = 198 const std::size_t client_number =
196 GetClientNumber(udp_input_address, udp_input_port, pad); 199 GetClientNumber(udp_input_address, udp_input_port, pad);
197 if (client_number != max_udp_clients) { 200 if (client_number != MAX_UDP_CLIENTS) {
198 LOG_ERROR(Input, "Duplicated UDP servers found"); 201 LOG_ERROR(Input, "Duplicated UDP servers found");
199 continue; 202 continue;
200 } 203 }
@@ -213,7 +216,7 @@ std::size_t Client::GetClientNumber(std::string_view host, u16 port, std::size_t
213 return client; 216 return client;
214 } 217 }
215 } 218 }
216 return max_udp_clients; 219 return MAX_UDP_CLIENTS;
217} 220}
218 221
219void Client::OnVersion([[maybe_unused]] Response::Version data) { 222void Client::OnVersion([[maybe_unused]] Response::Version data) {
@@ -259,33 +262,14 @@ void Client::OnPadData(Response::PadData data, std::size_t client) {
259 std::lock_guard guard(clients[client].status.update_mutex); 262 std::lock_guard guard(clients[client].status.update_mutex);
260 clients[client].status.motion_status = clients[client].motion.GetMotion(); 263 clients[client].status.motion_status = clients[client].motion.GetMotion();
261 264
262 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 265 for (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. 266 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 } 267 }
282 268
283 clients[client].status.touch_status = {x, y, is_active};
284
285 if (configuring) { 269 if (configuring) {
286 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); 270 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
287 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); 271 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
288 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); 272 UpdateYuzuSettings(client, accelerometer, gyroscope);
289 } 273 }
290 } 274 }
291} 275}
@@ -320,20 +304,16 @@ void Client::Reset() {
320} 304}
321 305
322void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, 306void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
323 const Common::Vec3<float>& gyro, bool touch) { 307 const Common::Vec3<float>& gyro) {
324 if (gyro.Length() > 0.2f) { 308 if (gyro.Length() > 0.2f) {
325 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", 309 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {})", client,
326 client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); 310 gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2]);
327 } 311 }
328 UDPPadStatus pad{ 312 UDPPadStatus pad{
329 .host = clients[client].host, 313 .host = clients[client].host,
330 .port = clients[client].port, 314 .port = clients[client].port,
331 .pad_index = clients[client].pad_index, 315 .pad_index = clients[client].pad_index,
332 }; 316 };
333 if (touch) {
334 pad.touch = PadTouch::Click;
335 pad_queue.Push(pad);
336 }
337 for (size_t i = 0; i < 3; ++i) { 317 for (size_t i = 0; i < 3; ++i) {
338 if (gyro[i] > 5.0f || gyro[i] < -5.0f) { 318 if (gyro[i] > 5.0f || gyro[i] < -5.0f) {
339 pad.motion = static_cast<PadMotion>(i); 319 pad.motion = static_cast<PadMotion>(i);
@@ -348,6 +328,53 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& a
348 } 328 }
349} 329}
350 330
331std::optional<size_t> Client::GetUnusedFingerID() const {
332 size_t first_free_id = 0;
333 while (first_free_id < MAX_TOUCH_FINGERS) {
334 if (!std::get<2>(touch_status[first_free_id])) {
335 return first_free_id;
336 } else {
337 first_free_id++;
338 }
339 }
340 return std::nullopt;
341}
342
343void Client::UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, size_t id) {
344 // TODO: Use custom calibration per device
345 const Common::ParamPackage touch_param(Settings::values.touch_device);
346 const u16 min_x = static_cast<u16>(touch_param.Get("min_x", 100));
347 const u16 min_y = static_cast<u16>(touch_param.Get("min_y", 50));
348 const u16 max_x = static_cast<u16>(touch_param.Get("max_x", 1800));
349 const u16 max_y = static_cast<u16>(touch_param.Get("max_y", 850));
350
351 if (touch_pad.is_active) {
352 if (finger_id[client * 2 + id] == MAX_TOUCH_FINGERS) {
353 const auto first_free_id = GetUnusedFingerID();
354 if (!first_free_id) {
355 // Invalid finger id skip to next input
356 return;
357 }
358 finger_id[client * 2 + id] = first_free_id.value();
359 }
360 auto& [x, y, pressed] = touch_status[finger_id[client * 2 + id]];
361 x = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) /
362 static_cast<float>(max_x - min_x);
363 y = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) /
364 static_cast<float>(max_y - min_y);
365 pressed = true;
366 return;
367 }
368
369 if (finger_id[client * 2 + id] != MAX_TOUCH_FINGERS) {
370 auto& [x, y, pressed] = touch_status[finger_id[client * 2 + id]];
371 x = 0;
372 y = 0;
373 pressed = false;
374 finger_id[client * 2 + id] = MAX_TOUCH_FINGERS;
375 }
376}
377
351void Client::BeginConfiguration() { 378void Client::BeginConfiguration() {
352 pad_queue.Clear(); 379 pad_queue.Clear();
353 configuring = true; 380 configuring = true;
@@ -360,7 +387,7 @@ void Client::EndConfiguration() {
360 387
361DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) { 388DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) {
362 const std::size_t client_number = GetClientNumber(host, port, pad); 389 const std::size_t client_number = GetClientNumber(host, port, pad);
363 if (client_number == max_udp_clients) { 390 if (client_number == MAX_UDP_CLIENTS) {
364 return clients[0].status; 391 return clients[0].status;
365 } 392 }
366 return clients[client_number].status; 393 return clients[client_number].status;
@@ -368,12 +395,20 @@ DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t
368 395
369const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const { 396const 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); 397 const std::size_t client_number = GetClientNumber(host, port, pad);
371 if (client_number == max_udp_clients) { 398 if (client_number == MAX_UDP_CLIENTS) {
372 return clients[0].status; 399 return clients[0].status;
373 } 400 }
374 return clients[client_number].status; 401 return clients[client_number].status;
375} 402}
376 403
404Input::TouchStatus& Client::GetTouchState() {
405 return touch_status;
406}
407
408const Input::TouchStatus& Client::GetTouchState() const {
409 return touch_status;
410}
411
377Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() { 412Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() {
378 return pad_queue; 413 return pad_queue;
379} 414}
@@ -426,24 +461,24 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
426 current_status = Status::Ready; 461 current_status = Status::Ready;
427 status_callback(current_status); 462 status_callback(current_status);
428 } 463 }
429 if (data.touch_1.is_active == 0) { 464 if (data.touch[0].is_active == 0) {
430 return; 465 return;
431 } 466 }
432 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 467 LOG_DEBUG(Input, "Current touch: {} {}", data.touch[0].x,
433 data.touch_1.y); 468 data.touch[0].y);
434 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x)); 469 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)); 470 min_y = std::min(min_y, static_cast<u16>(data.touch[0].y));
436 if (current_status == Status::Ready) { 471 if (current_status == Status::Ready) {
437 // First touch - min data (min_x/min_y) 472 // First touch - min data (min_x/min_y)
438 current_status = Status::Stage1Completed; 473 current_status = Status::Stage1Completed;
439 status_callback(current_status); 474 status_callback(current_status);
440 } 475 }
441 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && 476 if (data.touch[0].x - min_x > CALIBRATION_THRESHOLD &&
442 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { 477 data.touch[0].y - min_y > CALIBRATION_THRESHOLD) {
443 // Set the current position as max value and finishes 478 // Set the current position as max value and finishes
444 // configuration 479 // configuration
445 max_x = data.touch_1.x; 480 max_x = data.touch[0].x;
446 max_y = data.touch_1.y; 481 max_y = data.touch[0].y;
447 current_status = Status::Completed; 482 current_status = Status::Completed;
448 data_callback(min_x, min_y, max_x, max_y); 483 data_callback(min_x, min_y, max_x, max_y);
449 status_callback(current_status); 484 status_callback(current_status);