summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/touch_from_button.cpp15
-rw-r--r--src/input_common/udp/client.cpp121
-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
5 files changed, 116 insertions, 92 deletions
diff --git a/src/input_common/touch_from_button.cpp b/src/input_common/touch_from_button.cpp
index a07124a86..5226e70df 100644
--- a/src/input_common/touch_from_button.cpp
+++ b/src/input_common/touch_from_button.cpp
@@ -25,18 +25,19 @@ public:
25 } 25 }
26 } 26 }
27 27
28 std::tuple<float, float, bool> GetStatus() const override { 28 Input::TouchStatus GetStatus() const override {
29 for (const auto& m : map) { 29 Input::TouchStatus touch_status = {};
30 const bool state = std::get<0>(m)->GetStatus(); 30 for (size_t id = 0; id < map.size() && id < touch_status.size(); id++) {
31 const bool state = std::get<0>(map[id])->GetStatus();
31 if (state) { 32 if (state) {
32 const float x = static_cast<float>(std::get<1>(m)) / 33 const float x = static_cast<float>(std::get<1>(map[id])) /
33 static_cast<int>(Layout::ScreenUndocked::Width); 34 static_cast<int>(Layout::ScreenUndocked::Width);
34 const float y = static_cast<float>(std::get<2>(m)) / 35 const float y = static_cast<float>(std::get<2>(map[id])) /
35 static_cast<int>(Layout::ScreenUndocked::Height); 36 static_cast<int>(Layout::ScreenUndocked::Height);
36 return {x, y, true}; 37 touch_status[id] = {x, y, true};
37 } 38 }
38 } 39 }
39 return {}; 40 return touch_status;
40 } 41 }
41 42
42private: 43private:
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);
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index 00c8b09f5..1cd251ec8 100644
--- a/src/input_common/udp/client.h
+++ b/src/input_common/udp/client.h
@@ -29,6 +29,7 @@ namespace Response {
29struct PadData; 29struct PadData;
30struct PortInfo; 30struct PortInfo;
31struct Version; 31struct Version;
32struct TouchPad;
32} // namespace Response 33} // namespace Response
33 34
34enum class PadMotion { 35enum class PadMotion {
@@ -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<size_t> GetUnusedFingerID() const;
133
134 // Merges and updates all touch inputs into the touch_status array
135 void UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, 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<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