summaryrefslogtreecommitdiff
path: root/src/input_common/udp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/udp')
-rw-r--r--src/input_common/udp/client.cpp118
-rw-r--r--src/input_common/udp/client.h24
-rw-r--r--src/input_common/udp/protocol.h16
-rw-r--r--src/input_common/udp/udp.cpp32
4 files changed, 104 insertions, 86 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);
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index 00c8b09f5..822f9c550 100644
--- a/src/input_common/udp/client.h
+++ b/src/input_common/udp/client.h
@@ -28,6 +28,7 @@ class Socket;
28namespace Response { 28namespace Response {
29struct PadData; 29struct PadData;
30struct PortInfo; 30struct PortInfo;
31struct TouchPad;
31struct Version; 32struct Version;
32} // namespace Response 33} // namespace Response
33 34
@@ -50,7 +51,6 @@ struct UDPPadStatus {
50 std::string host{"127.0.0.1"}; 51 std::string host{"127.0.0.1"};
51 u16 port{26760}; 52 u16 port{26760};
52 std::size_t pad_index{}; 53 std::size_t pad_index{};
53 PadTouch touch{PadTouch::Undefined};
54 PadMotion motion{PadMotion::Undefined}; 54 PadMotion motion{PadMotion::Undefined};
55 f32 motion_value{0.0f}; 55 f32 motion_value{0.0f};
56}; 56};
@@ -93,6 +93,9 @@ public:
93 DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad); 93 DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad);
94 const DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad) const; 94 const DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad) const;
95 95
96 Input::TouchStatus& GetTouchState();
97 const Input::TouchStatus& GetTouchState() const;
98
96private: 99private:
97 struct ClientData { 100 struct ClientData {
98 std::string host{"127.0.0.1"}; 101 std::string host{"127.0.0.1"};
@@ -122,14 +125,25 @@ private:
122 void StartCommunication(std::size_t client, const std::string& host, u16 port, 125 void StartCommunication(std::size_t client, const std::string& host, u16 port,
123 std::size_t pad_index, u32 client_id); 126 std::size_t pad_index, u32 client_id);
124 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, 127 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
125 const Common::Vec3<float>& gyro, bool touch); 128 const Common::Vec3<float>& gyro);
129
130 // Returns an unused finger id, if there is no fingers available std::nullopt will be
131 // returned
132 std::optional<std::size_t> GetUnusedFingerID() const;
133
134 // Merges and updates all touch inputs into the touch_status array
135 void UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id);
126 136
127 bool configuring = false; 137 bool configuring = false;
128 138
129 // Allocate clients for 8 udp servers 139 // Allocate clients for 8 udp servers
130 const std::size_t max_udp_clients = 32; 140 static constexpr std::size_t MAX_UDP_CLIENTS = 4 * 8;
131 std::array<ClientData, 4 * 8> clients; 141 // Each client can have up 2 touch inputs
132 Common::SPSCQueue<UDPPadStatus> pad_queue; 142 static constexpr std::size_t MAX_TOUCH_FINGERS = MAX_UDP_CLIENTS * 2;
143 std::array<ClientData, MAX_UDP_CLIENTS> clients{};
144 Common::SPSCQueue<UDPPadStatus> pad_queue{};
145 Input::TouchStatus touch_status{};
146 std::array<std::size_t, MAX_TOUCH_FINGERS> finger_id{};
133}; 147};
134 148
135/// An async job allowing configuration of the touchpad calibration. 149/// An async job allowing configuration of the touchpad calibration.
diff --git a/src/input_common/udp/protocol.h b/src/input_common/udp/protocol.h
index fc1aea4b9..a3d276697 100644
--- a/src/input_common/udp/protocol.h
+++ b/src/input_common/udp/protocol.h
@@ -140,6 +140,14 @@ static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong si
140static_assert(std::is_trivially_copyable_v<PortInfo>, 140static_assert(std::is_trivially_copyable_v<PortInfo>,
141 "UDP Response PortInfo is not trivially copyable"); 141 "UDP Response PortInfo is not trivially copyable");
142 142
143struct TouchPad {
144 u8 is_active{};
145 u8 id{};
146 u16_le x{};
147 u16_le y{};
148};
149static_assert(sizeof(TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
150
143#pragma pack(push, 1) 151#pragma pack(push, 1)
144struct PadData { 152struct PadData {
145 PortInfo info{}; 153 PortInfo info{};
@@ -190,12 +198,7 @@ struct PadData {
190 u8 button_13{}; 198 u8 button_13{};
191 } analog_button; 199 } analog_button;
192 200
193 struct TouchPad { 201 std::array<TouchPad, 2> touch;
194 u8 is_active{};
195 u8 id{};
196 u16_le x{};
197 u16_le y{};
198 } touch_1, touch_2;
199 202
200 u64_le motion_timestamp; 203 u64_le motion_timestamp;
201 204
@@ -222,7 +225,6 @@ static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE,
222 225
223static_assert(sizeof(PadData::AnalogButton) == 12, 226static_assert(sizeof(PadData::AnalogButton) == 12,
224 "UDP Response AnalogButton struct has wrong size "); 227 "UDP Response AnalogButton struct has wrong size ");
225static_assert(sizeof(PadData::TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
226static_assert(sizeof(PadData::Accelerometer) == 12, 228static_assert(sizeof(PadData::Accelerometer) == 12,
227 "UDP Response Accelerometer struct has wrong size "); 229 "UDP Response Accelerometer struct has wrong size ");
228static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size "); 230static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size ");
diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp
index c5da27a38..b630281a0 100644
--- a/src/input_common/udp/udp.cpp
+++ b/src/input_common/udp/udp.cpp
@@ -78,8 +78,8 @@ public:
78 explicit UDPTouch(std::string ip_, u16 port_, u16 pad_, CemuhookUDP::Client* client_) 78 explicit UDPTouch(std::string ip_, u16 port_, u16 pad_, CemuhookUDP::Client* client_)
79 : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} 79 : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
80 80
81 std::tuple<float, float, bool> GetStatus() const override { 81 Input::TouchStatus GetStatus() const override {
82 return client->GetPadState(ip, port, pad).touch_status; 82 return client->GetTouchState();
83 } 83 }
84 84
85private: 85private:
@@ -107,32 +107,4 @@ std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamP
107 return std::make_unique<UDPTouch>(std::move(ip), port, pad, client.get()); 107 return std::make_unique<UDPTouch>(std::move(ip), port, pad, client.get());
108} 108}
109 109
110void UDPTouchFactory::BeginConfiguration() {
111 polling = true;
112 client->BeginConfiguration();
113}
114
115void UDPTouchFactory::EndConfiguration() {
116 polling = false;
117 client->EndConfiguration();
118}
119
120Common::ParamPackage UDPTouchFactory::GetNextInput() {
121 Common::ParamPackage params;
122 CemuhookUDP::UDPPadStatus pad;
123 auto& queue = client->GetPadQueue();
124 while (queue.Pop(pad)) {
125 if (pad.touch == CemuhookUDP::PadTouch::Undefined) {
126 continue;
127 }
128 params.Set("engine", "cemuhookudp");
129 params.Set("ip", pad.host);
130 params.Set("port", static_cast<u16>(pad.port));
131 params.Set("pad_index", static_cast<u16>(pad.pad_index));
132 params.Set("touch", static_cast<u16>(pad.touch));
133 return params;
134 }
135 return params;
136}
137
138} // namespace InputCommon 110} // namespace InputCommon