summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common')
-rwxr-xr-xsrc/input_common/analog_from_button.cpp18
-rw-r--r--src/input_common/gcadapter/gc_poller.cpp10
-rw-r--r--src/input_common/mouse/mouse_input.cpp32
-rw-r--r--src/input_common/mouse/mouse_input.h7
-rw-r--r--src/input_common/mouse/mouse_poller.cpp13
-rw-r--r--src/input_common/sdl/sdl_impl.cpp25
-rw-r--r--src/input_common/settings.h1
-rw-r--r--src/input_common/touch_from_button.cpp15
-rw-r--r--src/input_common/udp/client.cpp122
-rw-r--r--src/input_common/udp/client.h27
-rw-r--r--src/input_common/udp/protocol.h16
-rw-r--r--src/input_common/udp/udp.cpp36
12 files changed, 217 insertions, 105 deletions
diff --git a/src/input_common/analog_from_button.cpp b/src/input_common/analog_from_button.cpp
index 40b516f85..770893687 100755
--- a/src/input_common/analog_from_button.cpp
+++ b/src/input_common/analog_from_button.cpp
@@ -2,6 +2,7 @@
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 <atomic>
5#include <chrono> 6#include <chrono>
6#include <cmath> 7#include <cmath>
7#include <thread> 8#include <thread>
@@ -20,13 +21,16 @@ public:
20 : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)), 21 : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
21 right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_), 22 right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
22 modifier_angle(modifier_angle_) { 23 modifier_angle(modifier_angle_) {
24 update_thread_running.store(true);
23 update_thread = std::thread(&Analog::UpdateStatus, this); 25 update_thread = std::thread(&Analog::UpdateStatus, this);
24 } 26 }
25 27
26 ~Analog() override { 28 ~Analog() override {
27 update_thread_running = false; 29 if (update_thread_running.load()) {
28 if (update_thread.joinable()) { 30 update_thread_running.store(false);
29 update_thread.join(); 31 if (update_thread.joinable()) {
32 update_thread.join();
33 }
30 } 34 }
31 } 35 }
32 36
@@ -58,7 +62,7 @@ public:
58 } 62 }
59 63
60 void UpdateStatus() { 64 void UpdateStatus() {
61 while (update_thread_running) { 65 while (update_thread_running.load()) {
62 const float coef = modifier->GetStatus() ? modifier_scale : 1.0f; 66 const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
63 67
64 bool r = right->GetStatus(); 68 bool r = right->GetStatus();
@@ -135,6 +139,10 @@ public:
135 static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF)); 139 static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF));
136 } 140 }
137 141
142 Input::AnalogProperties GetAnalogProperties() const override {
143 return {modifier_scale, 1.0f, 0.5f};
144 }
145
138 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { 146 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
139 switch (direction) { 147 switch (direction) {
140 case Input::AnalogDirection::RIGHT: 148 case Input::AnalogDirection::RIGHT:
@@ -160,7 +168,7 @@ private:
160 float angle{}; 168 float angle{};
161 float amplitude{}; 169 float amplitude{};
162 std::thread update_thread; 170 std::thread update_thread;
163 bool update_thread_running{true}; 171 std::atomic<bool> update_thread_running{};
164}; 172};
165 173
166std::unique_ptr<Input::AnalogDevice> AnalogFromButton::Create(const Common::ParamPackage& params) { 174std::unique_ptr<Input::AnalogDevice> AnalogFromButton::Create(const Common::ParamPackage& params) {
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp
index 9670bdeb2..1b6ded8d6 100644
--- a/src/input_common/gcadapter/gc_poller.cpp
+++ b/src/input_common/gcadapter/gc_poller.cpp
@@ -185,6 +185,16 @@ public:
185 return {0.0f, 0.0f}; 185 return {0.0f, 0.0f};
186 } 186 }
187 187
188 std::tuple<float, float> GetRawStatus() const override {
189 const float x = GetAxis(axis_x);
190 const float y = GetAxis(axis_y);
191 return {x, y};
192 }
193
194 Input::AnalogProperties GetAnalogProperties() const override {
195 return {deadzone, range, 0.5f};
196 }
197
188 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { 198 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
189 const auto [x, y] = GetStatus(); 199 const auto [x, y] = GetStatus();
190 const float directional_deadzone = 0.5f; 200 const float directional_deadzone = 0.5f;
diff --git a/src/input_common/mouse/mouse_input.cpp b/src/input_common/mouse/mouse_input.cpp
index 10786a541..67a584d53 100644
--- a/src/input_common/mouse/mouse_input.cpp
+++ b/src/input_common/mouse/mouse_input.cpp
@@ -2,6 +2,7 @@
2// Licensed under GPLv2+ 2// Licensed under GPLv2+
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "core/settings.h"
5#include "input_common/mouse/mouse_input.h" 6#include "input_common/mouse/mouse_input.h"
6 7
7namespace MouseInput { 8namespace MouseInput {
@@ -36,6 +37,9 @@ void Mouse::UpdateThread() {
36 if (configuring) { 37 if (configuring) {
37 UpdateYuzuSettings(); 38 UpdateYuzuSettings();
38 } 39 }
40 if (mouse_panning_timout++ > 8) {
41 StopPanning();
42 }
39 std::this_thread::sleep_for(std::chrono::milliseconds(update_time)); 43 std::this_thread::sleep_for(std::chrono::milliseconds(update_time));
40 } 44 }
41} 45}
@@ -65,8 +69,34 @@ void Mouse::PressButton(int x, int y, int button_) {
65 mouse_info[button_index].data.pressed = true; 69 mouse_info[button_index].data.pressed = true;
66} 70}
67 71
68void Mouse::MouseMove(int x, int y) { 72void Mouse::StopPanning() {
73 for (MouseInfo& info : mouse_info) {
74 if (Settings::values.mouse_panning) {
75 info.data.axis = {};
76 info.tilt_speed = 0;
77 info.last_mouse_change = {};
78 }
79 }
80}
81
82void Mouse::MouseMove(int x, int y, int center_x, int center_y) {
69 for (MouseInfo& info : mouse_info) { 83 for (MouseInfo& info : mouse_info) {
84 if (Settings::values.mouse_panning) {
85 const auto mouse_change = Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y);
86 mouse_panning_timout = 0;
87
88 if (mouse_change.y == 0 && mouse_change.x == 0) {
89 continue;
90 }
91
92 info.last_mouse_change = (info.last_mouse_change * 0.8f) + (mouse_change * 0.2f);
93 info.data.axis = {static_cast<int>(16 * info.last_mouse_change.x),
94 static_cast<int>(16 * -info.last_mouse_change.y)};
95 info.tilt_direction = info.last_mouse_change;
96 info.tilt_speed = info.tilt_direction.Normalize() * info.sensitivity;
97 continue;
98 }
99
70 if (info.data.pressed) { 100 if (info.data.pressed) {
71 const auto mouse_move = Common::MakeVec(x, y) - info.mouse_origin; 101 const auto mouse_move = Common::MakeVec(x, y) - info.mouse_origin;
72 const auto mouse_change = Common::MakeVec(x, y) - info.last_mouse_position; 102 const auto mouse_change = Common::MakeVec(x, y) - info.last_mouse_position;
diff --git a/src/input_common/mouse/mouse_input.h b/src/input_common/mouse/mouse_input.h
index 58803c1bf..46aa676c1 100644
--- a/src/input_common/mouse/mouse_input.h
+++ b/src/input_common/mouse/mouse_input.h
@@ -57,8 +57,10 @@ public:
57 * Signals that mouse has moved. 57 * Signals that mouse has moved.
58 * @param x the x-coordinate of the cursor 58 * @param x the x-coordinate of the cursor
59 * @param y the y-coordinate of the cursor 59 * @param y the y-coordinate of the cursor
60 * @param center_x the x-coordinate of the middle of the screen
61 * @param center_y the y-coordinate of the middle of the screen
60 */ 62 */
61 void MouseMove(int x, int y); 63 void MouseMove(int x, int y, int center_x, int center_y);
62 64
63 /** 65 /**
64 * Signals that a motion sensor tilt has ended. 66 * Signals that a motion sensor tilt has ended.
@@ -74,11 +76,13 @@ public:
74private: 76private:
75 void UpdateThread(); 77 void UpdateThread();
76 void UpdateYuzuSettings(); 78 void UpdateYuzuSettings();
79 void StopPanning();
77 80
78 struct MouseInfo { 81 struct MouseInfo {
79 InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f}; 82 InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f};
80 Common::Vec2<int> mouse_origin; 83 Common::Vec2<int> mouse_origin;
81 Common::Vec2<int> last_mouse_position; 84 Common::Vec2<int> last_mouse_position;
85 Common::Vec2<float> last_mouse_change;
82 bool is_tilting = false; 86 bool is_tilting = false;
83 float sensitivity{0.120f}; 87 float sensitivity{0.120f};
84 88
@@ -94,5 +98,6 @@ private:
94 Common::SPSCQueue<MouseStatus> mouse_queue; 98 Common::SPSCQueue<MouseStatus> mouse_queue;
95 bool configuring{false}; 99 bool configuring{false};
96 bool update_thread_running{true}; 100 bool update_thread_running{true};
101 int mouse_panning_timout{};
97}; 102};
98} // namespace MouseInput 103} // namespace MouseInput
diff --git a/src/input_common/mouse/mouse_poller.cpp b/src/input_common/mouse/mouse_poller.cpp
index 508eb0c7d..bb56787ee 100644
--- a/src/input_common/mouse/mouse_poller.cpp
+++ b/src/input_common/mouse/mouse_poller.cpp
@@ -6,6 +6,7 @@
6#include <utility> 6#include <utility>
7 7
8#include "common/threadsafe_queue.h" 8#include "common/threadsafe_queue.h"
9#include "core/settings.h"
9#include "input_common/mouse/mouse_input.h" 10#include "input_common/mouse/mouse_input.h"
10#include "input_common/mouse/mouse_poller.h" 11#include "input_common/mouse/mouse_poller.h"
11 12
@@ -71,7 +72,7 @@ public:
71 std::lock_guard lock{mutex}; 72 std::lock_guard lock{mutex};
72 const auto axis_value = 73 const auto axis_value =
73 static_cast<float>(mouse_input->GetMouseState(button).axis.at(axis)); 74 static_cast<float>(mouse_input->GetMouseState(button).axis.at(axis));
74 return axis_value / (100.0f * range); 75 return axis_value * Settings::values.mouse_panning_sensitivity / (100.0f * range);
75 } 76 }
76 77
77 std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { 78 std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const {
@@ -106,6 +107,16 @@ public:
106 return {0.0f, 0.0f}; 107 return {0.0f, 0.0f};
107 } 108 }
108 109
110 std::tuple<float, float> GetRawStatus() const override {
111 const float x = GetAxis(axis_x);
112 const float y = GetAxis(axis_y);
113 return {x, y};
114 }
115
116 Input::AnalogProperties GetAnalogProperties() const override {
117 return {deadzone, range, 0.5f};
118 }
119
109private: 120private:
110 const u32 button; 121 const u32 button;
111 const u32 axis_x; 122 const u32 axis_x;
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index d32eb732a..a88ae452f 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -81,10 +81,14 @@ public:
81 } 81 }
82 82
83 bool RumblePlay(u16 amp_low, u16 amp_high) { 83 bool RumblePlay(u16 amp_low, u16 amp_high) {
84 constexpr u32 rumble_max_duration_ms = 1000;
85
84 if (sdl_controller) { 86 if (sdl_controller) {
85 return SDL_GameControllerRumble(sdl_controller.get(), amp_low, amp_high, 0) == 0; 87 return SDL_GameControllerRumble(sdl_controller.get(), amp_low, amp_high,
88 rumble_max_duration_ms) == 0;
86 } else if (sdl_joystick) { 89 } else if (sdl_joystick) {
87 return SDL_JoystickRumble(sdl_joystick.get(), amp_low, amp_high, 0) == 0; 90 return SDL_JoystickRumble(sdl_joystick.get(), amp_low, amp_high,
91 rumble_max_duration_ms) == 0;
88 } 92 }
89 93
90 return false; 94 return false;
@@ -373,6 +377,16 @@ public:
373 return {}; 377 return {};
374 } 378 }
375 379
380 std::tuple<float, float> GetRawStatus() const override {
381 const float x = joystick->GetAxis(axis_x, range);
382 const float y = joystick->GetAxis(axis_y, range);
383 return {x, -y};
384 }
385
386 Input::AnalogProperties GetAnalogProperties() const override {
387 return {deadzone, range, 0.5f};
388 }
389
376 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { 390 bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
377 const auto [x, y] = GetStatus(); 391 const auto [x, y] = GetStatus();
378 const float directional_deadzone = 0.5f; 392 const float directional_deadzone = 0.5f;
@@ -703,6 +717,13 @@ SDLState::SDLState() {
703 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { 717 if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) {
704 LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); 718 LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError());
705 } 719 }
720// these hints are only defined on sdl2.0.9 or higher
721#if SDL_VERSION_ATLEAST(2, 0, 9)
722#if !SDL_VERSION_ATLEAST(2, 0, 12)
723 // There are also hints to toggle the individual drivers if needed.
724 SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI, "0");
725#endif
726#endif
706 727
707 SDL_AddEventWatch(&SDLEventWatcher, this); 728 SDL_AddEventWatch(&SDLEventWatcher, this);
708 729
diff --git a/src/input_common/settings.h b/src/input_common/settings.h
index 75486554b..a59f5d461 100644
--- a/src/input_common/settings.h
+++ b/src/input_common/settings.h
@@ -340,6 +340,7 @@ enum class ControllerType {
340 LeftJoycon, 340 LeftJoycon,
341 RightJoycon, 341 RightJoycon,
342 Handheld, 342 Handheld,
343 GameCube,
343}; 344};
344 345
345struct PlayerInput { 346struct PlayerInput {
diff --git a/src/input_common/touch_from_button.cpp b/src/input_common/touch_from_button.cpp
index a07124a86..ffbe4f2ed 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 (std::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..c4afa4174 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
@@ -143,6 +144,10 @@ Client::~Client() {
143 Reset(); 144 Reset();
144} 145}
145 146
147Client::ClientData::ClientData() = default;
148
149Client::ClientData::~ClientData() = default;
150
146std::vector<Common::ParamPackage> Client::GetInputDevices() const { 151std::vector<Common::ParamPackage> Client::GetInputDevices() const {
147 std::vector<Common::ParamPackage> devices; 152 std::vector<Common::ParamPackage> devices;
148 for (std::size_t client = 0; client < clients.size(); client++) { 153 for (std::size_t client = 0; client < clients.size(); client++) {
@@ -176,7 +181,7 @@ void Client::ReloadSockets() {
176 std::string server_token; 181 std::string server_token;
177 std::size_t client = 0; 182 std::size_t client = 0;
178 while (std::getline(servers_ss, server_token, ',')) { 183 while (std::getline(servers_ss, server_token, ',')) {
179 if (client == max_udp_clients) { 184 if (client == MAX_UDP_CLIENTS) {
180 break; 185 break;
181 } 186 }
182 std::stringstream server_ss(server_token); 187 std::stringstream server_ss(server_token);
@@ -194,7 +199,7 @@ void Client::ReloadSockets() {
194 for (std::size_t pad = 0; pad < 4; ++pad) { 199 for (std::size_t pad = 0; pad < 4; ++pad) {
195 const std::size_t client_number = 200 const std::size_t client_number =
196 GetClientNumber(udp_input_address, udp_input_port, pad); 201 GetClientNumber(udp_input_address, udp_input_port, pad);
197 if (client_number != max_udp_clients) { 202 if (client_number != MAX_UDP_CLIENTS) {
198 LOG_ERROR(Input, "Duplicated UDP servers found"); 203 LOG_ERROR(Input, "Duplicated UDP servers found");
199 continue; 204 continue;
200 } 205 }
@@ -213,7 +218,7 @@ std::size_t Client::GetClientNumber(std::string_view host, u16 port, std::size_t
213 return client; 218 return client;
214 } 219 }
215 } 220 }
216 return max_udp_clients; 221 return MAX_UDP_CLIENTS;
217} 222}
218 223
219void Client::OnVersion([[maybe_unused]] Response::Version data) { 224void Client::OnVersion([[maybe_unused]] Response::Version data) {
@@ -259,33 +264,14 @@ void Client::OnPadData(Response::PadData data, std::size_t client) {
259 std::lock_guard guard(clients[client].status.update_mutex); 264 std::lock_guard guard(clients[client].status.update_mutex);
260 clients[client].status.motion_status = clients[client].motion.GetMotion(); 265 clients[client].status.motion_status = clients[client].motion.GetMotion();
261 266
262 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 267 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. 268 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 } 269 }
282 270
283 clients[client].status.touch_status = {x, y, is_active};
284
285 if (configuring) { 271 if (configuring) {
286 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); 272 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
287 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); 273 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
288 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); 274 UpdateYuzuSettings(client, accelerometer, gyroscope);
289 } 275 }
290 } 276 }
291} 277}
@@ -320,21 +306,17 @@ void Client::Reset() {
320} 306}
321 307
322void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, 308void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
323 const Common::Vec3<float>& gyro, bool touch) { 309 const Common::Vec3<float>& gyro) {
324 if (gyro.Length() > 0.2f) { 310 if (gyro.Length() > 0.2f) {
325 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", 311 LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {})", client,
326 client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); 312 gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2]);
327 } 313 }
328 UDPPadStatus pad{ 314 UDPPadStatus pad{
329 .host = clients[client].host, 315 .host = clients[client].host,
330 .port = clients[client].port, 316 .port = clients[client].port,
331 .pad_index = clients[client].pad_index, 317 .pad_index = clients[client].pad_index,
332 }; 318 };
333 if (touch) { 319 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) { 320 if (gyro[i] > 5.0f || gyro[i] < -5.0f) {
339 pad.motion = static_cast<PadMotion>(i); 321 pad.motion = static_cast<PadMotion>(i);
340 pad.motion_value = gyro[i]; 322 pad.motion_value = gyro[i];
@@ -348,6 +330,50 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& a
348 } 330 }
349} 331}
350 332
333std::optional<std::size_t> Client::GetUnusedFingerID() const {
334 std::size_t first_free_id = 0;
335 while (first_free_id < MAX_TOUCH_FINGERS) {
336 if (!std::get<2>(touch_status[first_free_id])) {
337 return first_free_id;
338 } else {
339 first_free_id++;
340 }
341 }
342 return std::nullopt;
343}
344
345void Client::UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id) {
346 // TODO: Use custom calibration per device
347 const Common::ParamPackage touch_param(Settings::values.touch_device);
348 const u16 min_x = static_cast<u16>(touch_param.Get("min_x", 100));
349 const u16 min_y = static_cast<u16>(touch_param.Get("min_y", 50));
350 const u16 max_x = static_cast<u16>(touch_param.Get("max_x", 1800));
351 const u16 max_y = static_cast<u16>(touch_param.Get("max_y", 850));
352 const std::size_t touch_id = client * 2 + id;
353 if (touch_pad.is_active) {
354 if (finger_id[touch_id] == MAX_TOUCH_FINGERS) {
355 const auto first_free_id = GetUnusedFingerID();
356 if (!first_free_id) {
357 // Invalid finger id skip to next input
358 return;
359 }
360 finger_id[touch_id] = *first_free_id;
361 }
362 auto& [x, y, pressed] = touch_status[finger_id[touch_id]];
363 x = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) /
364 static_cast<float>(max_x - min_x);
365 y = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) /
366 static_cast<float>(max_y - min_y);
367 pressed = true;
368 return;
369 }
370
371 if (finger_id[touch_id] != MAX_TOUCH_FINGERS) {
372 touch_status[finger_id[touch_id]] = {};
373 finger_id[touch_id] = MAX_TOUCH_FINGERS;
374 }
375}
376
351void Client::BeginConfiguration() { 377void Client::BeginConfiguration() {
352 pad_queue.Clear(); 378 pad_queue.Clear();
353 configuring = true; 379 configuring = true;
@@ -360,7 +386,7 @@ void Client::EndConfiguration() {
360 386
361DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) { 387DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) {
362 const std::size_t client_number = GetClientNumber(host, port, pad); 388 const std::size_t client_number = GetClientNumber(host, port, pad);
363 if (client_number == max_udp_clients) { 389 if (client_number == MAX_UDP_CLIENTS) {
364 return clients[0].status; 390 return clients[0].status;
365 } 391 }
366 return clients[client_number].status; 392 return clients[client_number].status;
@@ -368,12 +394,20 @@ DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t
368 394
369const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const { 395const 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); 396 const std::size_t client_number = GetClientNumber(host, port, pad);
371 if (client_number == max_udp_clients) { 397 if (client_number == MAX_UDP_CLIENTS) {
372 return clients[0].status; 398 return clients[0].status;
373 } 399 }
374 return clients[client_number].status; 400 return clients[client_number].status;
375} 401}
376 402
403Input::TouchStatus& Client::GetTouchState() {
404 return touch_status;
405}
406
407const Input::TouchStatus& Client::GetTouchState() const {
408 return touch_status;
409}
410
377Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() { 411Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() {
378 return pad_queue; 412 return pad_queue;
379} 413}
@@ -426,24 +460,24 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
426 current_status = Status::Ready; 460 current_status = Status::Ready;
427 status_callback(current_status); 461 status_callback(current_status);
428 } 462 }
429 if (data.touch_1.is_active == 0) { 463 if (data.touch[0].is_active == 0) {
430 return; 464 return;
431 } 465 }
432 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 466 LOG_DEBUG(Input, "Current touch: {} {}", data.touch[0].x,
433 data.touch_1.y); 467 data.touch[0].y);
434 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x)); 468 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)); 469 min_y = std::min(min_y, static_cast<u16>(data.touch[0].y));
436 if (current_status == Status::Ready) { 470 if (current_status == Status::Ready) {
437 // First touch - min data (min_x/min_y) 471 // First touch - min data (min_x/min_y)
438 current_status = Status::Stage1Completed; 472 current_status = Status::Stage1Completed;
439 status_callback(current_status); 473 status_callback(current_status);
440 } 474 }
441 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && 475 if (data.touch[0].x - min_x > CALIBRATION_THRESHOLD &&
442 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { 476 data.touch[0].y - min_y > CALIBRATION_THRESHOLD) {
443 // Set the current position as max value and finishes 477 // Set the current position as max value and finishes
444 // configuration 478 // configuration
445 max_x = data.touch_1.x; 479 max_x = data.touch[0].x;
446 max_y = data.touch_1.y; 480 max_y = data.touch[0].y;
447 current_status = Status::Completed; 481 current_status = Status::Completed;
448 data_callback(min_x, min_y, max_x, max_y); 482 data_callback(min_x, min_y, max_x, max_y);
449 status_callback(current_status); 483 status_callback(current_status);
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index 00c8b09f5..a523f6124 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,8 +93,14 @@ 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 {
101 ClientData();
102 ~ClientData();
103
98 std::string host{"127.0.0.1"}; 104 std::string host{"127.0.0.1"};
99 u16 port{26760}; 105 u16 port{26760};
100 std::size_t pad_index{}; 106 std::size_t pad_index{};
@@ -122,14 +128,25 @@ private:
122 void StartCommunication(std::size_t client, const std::string& host, u16 port, 128 void StartCommunication(std::size_t client, const std::string& host, u16 port,
123 std::size_t pad_index, u32 client_id); 129 std::size_t pad_index, u32 client_id);
124 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, 130 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
125 const Common::Vec3<float>& gyro, bool touch); 131 const Common::Vec3<float>& gyro);
132
133 // Returns an unused finger id, if there is no fingers available std::nullopt will be
134 // returned
135 std::optional<std::size_t> GetUnusedFingerID() const;
136
137 // Merges and updates all touch inputs into the touch_status array
138 void UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id);
126 139
127 bool configuring = false; 140 bool configuring = false;
128 141
129 // Allocate clients for 8 udp servers 142 // Allocate clients for 8 udp servers
130 const std::size_t max_udp_clients = 32; 143 static constexpr std::size_t MAX_UDP_CLIENTS = 4 * 8;
131 std::array<ClientData, 4 * 8> clients; 144 // Each client can have up 2 touch inputs
132 Common::SPSCQueue<UDPPadStatus> pad_queue; 145 static constexpr std::size_t MAX_TOUCH_FINGERS = MAX_UDP_CLIENTS * 2;
146 std::array<ClientData, MAX_UDP_CLIENTS> clients{};
147 Common::SPSCQueue<UDPPadStatus> pad_queue{};
148 Input::TouchStatus touch_status{};
149 std::array<std::size_t, MAX_TOUCH_FINGERS> finger_id{};
133}; 150};
134 151
135/// An async job allowing configuration of the touchpad calibration. 152/// 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..9829da6f0 100644
--- a/src/input_common/udp/udp.cpp
+++ b/src/input_common/udp/udp.cpp
@@ -78,14 +78,14 @@ 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:
86 const std::string ip; 86 const std::string ip;
87 const u16 port; 87 [[maybe_unused]] const u16 port;
88 const u16 pad; 88 [[maybe_unused]] const u16 pad;
89 CemuhookUDP::Client* client; 89 CemuhookUDP::Client* client;
90 mutable std::mutex mutex; 90 mutable std::mutex mutex;
91}; 91};
@@ -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