summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/service/hid/controllers/npad.cpp3
-rw-r--r--src/input_common/main.cpp46
-rw-r--r--src/input_common/main.h14
-rw-r--r--src/input_common/udp/client.cpp187
-rw-r--r--src/input_common/udp/client.h74
-rw-r--r--src/input_common/udp/udp.cpp181
-rw-r--r--src/input_common/udp/udp.h61
-rw-r--r--src/yuzu/configuration/configure_input_player.cpp22
8 files changed, 449 insertions, 139 deletions
diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp
index 2e06372a4..510fa3071 100644
--- a/src/core/hle/service/hid/controllers/npad.cpp
+++ b/src/core/hle/service/hid/controllers/npad.cpp
@@ -397,8 +397,7 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8*
397 std::tie(motion_devices[e].accel, motion_devices[e].gyro, 397 std::tie(motion_devices[e].accel, motion_devices[e].gyro,
398 motion_devices[e].rotation, motion_devices[e].orientation) = 398 motion_devices[e].rotation, motion_devices[e].orientation) =
399 device->GetStatus(); 399 device->GetStatus();
400 sixaxis_at_rest = 400 sixaxis_at_rest = sixaxis_at_rest && motion_devices[e].gyro.Length2() < 0.0001f;
401 sixaxis_at_rest && motion_devices[e].gyro.Length2() < 0.00005f;
402 } 401 }
403 } 402 }
404 } 403 }
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
index ea1a1cee6..062ec66b5 100644
--- a/src/input_common/main.cpp
+++ b/src/input_common/main.cpp
@@ -12,6 +12,7 @@
12#include "input_common/main.h" 12#include "input_common/main.h"
13#include "input_common/motion_emu.h" 13#include "input_common/motion_emu.h"
14#include "input_common/touch_from_button.h" 14#include "input_common/touch_from_button.h"
15#include "input_common/udp/client.h"
15#include "input_common/udp/udp.h" 16#include "input_common/udp/udp.h"
16#ifdef HAVE_SDL2 17#ifdef HAVE_SDL2
17#include "input_common/sdl/sdl.h" 18#include "input_common/sdl/sdl.h"
@@ -40,7 +41,11 @@ struct InputSubsystem::Impl {
40 sdl = SDL::Init(); 41 sdl = SDL::Init();
41#endif 42#endif
42 43
43 udp = CemuhookUDP::Init(); 44 udp = std::make_shared<InputCommon::CemuhookUDP::Client>();
45 udpmotion = std::make_shared<UDPMotionFactory>(udp);
46 Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", udpmotion);
47 udptouch = std::make_shared<UDPTouchFactory>(udp);
48 Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", udptouch);
44 } 49 }
45 50
46 void Shutdown() { 51 void Shutdown() {
@@ -53,12 +58,17 @@ struct InputSubsystem::Impl {
53#ifdef HAVE_SDL2 58#ifdef HAVE_SDL2
54 sdl.reset(); 59 sdl.reset();
55#endif 60#endif
56 udp.reset();
57 Input::UnregisterFactory<Input::ButtonDevice>("gcpad"); 61 Input::UnregisterFactory<Input::ButtonDevice>("gcpad");
58 Input::UnregisterFactory<Input::AnalogDevice>("gcpad"); 62 Input::UnregisterFactory<Input::AnalogDevice>("gcpad");
59 63
60 gcbuttons.reset(); 64 gcbuttons.reset();
61 gcanalog.reset(); 65 gcanalog.reset();
66
67 Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp");
68 Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp");
69
70 udpmotion.reset();
71 udptouch.reset();
62 } 72 }
63 73
64 [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const { 74 [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const {
@@ -109,14 +119,28 @@ struct InputSubsystem::Impl {
109 return {}; 119 return {};
110 } 120 }
111 121
122 [[nodiscard]] MotionMapping GetMotionMappingForDevice(
123 const Common::ParamPackage& params) const {
124 if (!params.Has("class") || params.Get("class", "") == "any") {
125 return {};
126 }
127 if (params.Get("class", "") == "cemuhookudp") {
128 // TODO return the correct motion device
129 return {};
130 }
131 return {};
132 }
133
112 std::shared_ptr<Keyboard> keyboard; 134 std::shared_ptr<Keyboard> keyboard;
113 std::shared_ptr<MotionEmu> motion_emu; 135 std::shared_ptr<MotionEmu> motion_emu;
114#ifdef HAVE_SDL2 136#ifdef HAVE_SDL2
115 std::unique_ptr<SDL::State> sdl; 137 std::unique_ptr<SDL::State> sdl;
116#endif 138#endif
117 std::unique_ptr<CemuhookUDP::State> udp;
118 std::shared_ptr<GCButtonFactory> gcbuttons; 139 std::shared_ptr<GCButtonFactory> gcbuttons;
119 std::shared_ptr<GCAnalogFactory> gcanalog; 140 std::shared_ptr<GCAnalogFactory> gcanalog;
141 std::shared_ptr<UDPMotionFactory> udpmotion;
142 std::shared_ptr<UDPTouchFactory> udptouch;
143 std::shared_ptr<CemuhookUDP::Client> udp;
120}; 144};
121 145
122InputSubsystem::InputSubsystem() : impl{std::make_unique<Impl>()} {} 146InputSubsystem::InputSubsystem() : impl{std::make_unique<Impl>()} {}
@@ -175,6 +199,22 @@ const GCButtonFactory* InputSubsystem::GetGCButtons() const {
175 return impl->gcbuttons.get(); 199 return impl->gcbuttons.get();
176} 200}
177 201
202UDPMotionFactory* InputSubsystem::GetUDPMotions() {
203 return impl->udpmotion.get();
204}
205
206const UDPMotionFactory* InputSubsystem::GetUDPMotions() const {
207 return impl->udpmotion.get();
208}
209
210UDPTouchFactory* InputSubsystem::GetUDPTouch() {
211 return impl->udptouch.get();
212}
213
214const UDPTouchFactory* InputSubsystem::GetUDPTouch() const {
215 return impl->udptouch.get();
216}
217
178void InputSubsystem::ReloadInputDevices() { 218void InputSubsystem::ReloadInputDevices() {
179 if (!impl->udp) { 219 if (!impl->udp) {
180 return; 220 return;
diff --git a/src/input_common/main.h b/src/input_common/main.h
index 18f44dcc3..dded3f1ef 100644
--- a/src/input_common/main.h
+++ b/src/input_common/main.h
@@ -54,6 +54,8 @@ public:
54 54
55class GCAnalogFactory; 55class GCAnalogFactory;
56class GCButtonFactory; 56class GCButtonFactory;
57class UDPMotionFactory;
58class UDPTouchFactory;
57class Keyboard; 59class Keyboard;
58class MotionEmu; 60class MotionEmu;
59 61
@@ -123,6 +125,18 @@ public:
123 /// Retrieves the underlying GameCube button handler. 125 /// Retrieves the underlying GameCube button handler.
124 [[nodiscard]] const GCButtonFactory* GetGCButtons() const; 126 [[nodiscard]] const GCButtonFactory* GetGCButtons() const;
125 127
128 /// Retrieves the underlying udp motion handler.
129 [[nodiscard]] UDPMotionFactory* GetUDPMotions();
130
131 /// Retrieves the underlying udp motion handler.
132 [[nodiscard]] const UDPMotionFactory* GetUDPMotions() const;
133
134 /// Retrieves the underlying udp touch handler.
135 [[nodiscard]] UDPTouchFactory* GetUDPTouch();
136
137 /// Retrieves the underlying udp touch handler.
138 [[nodiscard]] const UDPTouchFactory* GetUDPTouch() const;
139
126 /// Reloads the input devices 140 /// Reloads the input devices
127 void ReloadInputDevices(); 141 void ReloadInputDevices();
128 142
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 91e13482d..e0c796040 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -2,14 +2,13 @@
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 <algorithm>
6#include <array>
7#include <chrono> 5#include <chrono>
8#include <cstring> 6#include <cstring>
9#include <functional> 7#include <functional>
10#include <thread> 8#include <thread>
11#include <boost/asio.hpp> 9#include <boost/asio.hpp>
12#include "common/logging/log.h" 10#include "common/logging/log.h"
11#include "core/settings.h"
13#include "input_common/udp/client.h" 12#include "input_common/udp/client.h"
14#include "input_common/udp/protocol.h" 13#include "input_common/udp/protocol.h"
15 14
@@ -131,21 +130,60 @@ static void SocketLoop(Socket* socket) {
131 socket->Loop(); 130 socket->Loop();
132} 131}
133 132
134Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port, 133Client::Client() {
135 u8 pad_index, u32 client_id) 134 LOG_INFO(Input, "Udp Initialization started");
136 : status(std::move(status)) { 135 for (std::size_t client = 0; client < clients.size(); client++) {
137 StartCommunication(host, port, pad_index, client_id); 136 u8 pad = client % 4;
137 StartCommunication(client, Settings::values.udp_input_address,
138 Settings::values.udp_input_port, pad, 24872);
139 // Set motion parameters
140 // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode
141 // Real HW values are unkown, 0.0001 is an aproximate to Standard
142 clients[client].motion.SetGyroThreshold(0.0001f);
143 }
138} 144}
139 145
140Client::~Client() { 146Client::~Client() {
141 socket->Stop(); 147 Reset();
142 thread.join(); 148}
149
150std::vector<Common::ParamPackage> Client::GetInputDevices() const {
151 std::vector<Common::ParamPackage> devices;
152 for (std::size_t client = 0; client < clients.size(); client++) {
153 if (!DeviceConnected(client)) {
154 continue;
155 }
156 std::string name = fmt::format("UDP Controller{} {} {}", clients[client].active,
157 clients[client].active == 1, client);
158 devices.emplace_back(Common::ParamPackage{
159 {"class", "cemuhookudp"},
160 {"display", std::move(name)},
161 {"port", std::to_string(client)},
162 });
163 }
164 return devices;
143} 165}
144 166
167bool Client::DeviceConnected(std::size_t pad) const {
168 // Use last timestamp to detect if the socket has stopped sending data
169 const auto now = std::chrono::system_clock::now();
170 u64 time_difference =
171 std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update)
172 .count();
173 return time_difference < 1000 && clients[pad].active == 1;
174}
175
176void Client::ReloadUDPClient() {
177 for (std::size_t client = 0; client < clients.size(); client++) {
178 ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client);
179 }
180}
145void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { 181void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) {
146 socket->Stop(); 182 // client number must be determined from host / port and pad index
147 thread.join(); 183 std::size_t client = pad_index;
148 StartCommunication(host, port, pad_index, client_id); 184 clients[client].socket->Stop();
185 clients[client].thread.join();
186 StartCommunication(client, host, port, pad_index, client_id);
149} 187}
150 188
151void Client::OnVersion(Response::Version data) { 189void Client::OnVersion(Response::Version data) {
@@ -157,31 +195,39 @@ void Client::OnPortInfo(Response::PortInfo data) {
157} 195}
158 196
159void Client::OnPadData(Response::PadData data) { 197void Client::OnPadData(Response::PadData data) {
198 // client number must be determined from host / port and pad index
199 std::size_t client = data.info.id;
160 LOG_TRACE(Input, "PadData packet received"); 200 LOG_TRACE(Input, "PadData packet received");
161 if (data.packet_counter <= packet_sequence) { 201 if (data.packet_counter == clients[client].packet_sequence) {
162 LOG_WARNING( 202 LOG_WARNING(
163 Input, 203 Input,
164 "PadData packet dropped because its stale info. Current count: {} Packet count: {}", 204 "PadData packet dropped because its stale info. Current count: {} Packet count: {}",
165 packet_sequence, data.packet_counter); 205 clients[client].packet_sequence, data.packet_counter);
166 return; 206 return;
167 } 207 }
168 packet_sequence = data.packet_counter; 208 clients[client].active = data.info.is_pad_active;
169 // TODO: Check how the Switch handles motions and how the CemuhookUDP motion 209 clients[client].packet_sequence = data.packet_counter;
170 // directions correspond to the ones of the Switch 210 const auto now = std::chrono::system_clock::now();
171 Common::Vec3f accel = Common::MakeVec<float>(data.accel.x, data.accel.y, data.accel.z); 211 u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>(
172 Common::Vec3f gyro = Common::MakeVec<float>(data.gyro.pitch, data.gyro.yaw, data.gyro.roll); 212 now - clients[client].last_motion_update)
173 213 .count();
174 // TODO: Calculate the correct rotation vector and orientation matrix 214 clients[client].last_motion_update = now;
175 const auto rotation = Common::MakeVec(0.0f, 0.0f, 0.0f); 215 Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw};
176 const std::array orientation{ 216 clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y});
177 Common::Vec3f(1.0f, 0.0f, 0.0f), 217 // Gyroscope values are not it the correct scale from better joy.
178 Common::Vec3f(0.0f, 1.0f, 0.0f), 218 // By dividing by 312 allow us to make one full turn = 1 turn
179 Common::Vec3f(0.0f, 0.0f, 1.0f), 219 // This must be a configurable valued called sensitivity
180 }; 220 clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f);
181 { 221 clients[client].motion.UpdateRotation(time_difference);
182 std::lock_guard guard(status->update_mutex); 222 clients[client].motion.UpdateOrientation(time_difference);
223 Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
224 Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
225 Common::Vec3f rotation = clients[client].motion.GetRotations();
226 std::array<Common::Vec3f, 3> orientation = clients[client].motion.GetOrientation();
183 227
184 status->motion_status = {accel, gyro, rotation, orientation}; 228 {
229 std::lock_guard guard(clients[client].status.update_mutex);
230 clients[client].status.motion_status = {accelerometer, gyroscope, rotation, orientation};
185 231
186 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 232 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates
187 // between a simple "tap" and a hard press that causes the touch screen to click. 233 // between a simple "tap" and a hard press that causes the touch screen to click.
@@ -190,11 +236,11 @@ void Client::OnPadData(Response::PadData data) {
190 float x = 0; 236 float x = 0;
191 float y = 0; 237 float y = 0;
192 238
193 if (is_active && status->touch_calibration) { 239 if (is_active && clients[client].status.touch_calibration) {
194 const u16 min_x = status->touch_calibration->min_x; 240 const u16 min_x = clients[client].status.touch_calibration->min_x;
195 const u16 max_x = status->touch_calibration->max_x; 241 const u16 max_x = clients[client].status.touch_calibration->max_x;
196 const u16 min_y = status->touch_calibration->min_y; 242 const u16 min_y = clients[client].status.touch_calibration->min_y;
197 const u16 max_y = status->touch_calibration->max_y; 243 const u16 max_y = clients[client].status.touch_calibration->max_y;
198 244
199 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / 245 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) /
200 static_cast<float>(max_x - min_x); 246 static_cast<float>(max_x - min_x);
@@ -202,17 +248,82 @@ void Client::OnPadData(Response::PadData data) {
202 static_cast<float>(max_y - min_y); 248 static_cast<float>(max_y - min_y);
203 } 249 }
204 250
205 status->touch_status = {x, y, is_active}; 251 clients[client].status.touch_status = {x, y, is_active};
252
253 if (configuring) {
254 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active);
255 }
206 } 256 }
207} 257}
208 258
209void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id) { 259void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index,
260 u32 client_id) {
210 SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, 261 SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
211 [this](Response::PortInfo info) { OnPortInfo(info); }, 262 [this](Response::PortInfo info) { OnPortInfo(info); },
212 [this](Response::PadData data) { OnPadData(data); }}; 263 [this](Response::PadData data) { OnPadData(data); }};
213 LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); 264 LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port);
214 socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); 265 clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback);
215 thread = std::thread{SocketLoop, this->socket.get()}; 266 clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()};
267}
268
269void Client::Reset() {
270 for (std::size_t client = 0; client < clients.size(); client++) {
271 clients[client].socket->Stop();
272 clients[client].thread.join();
273 }
274}
275
276void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
277 const Common::Vec3<float>& gyro, bool touch) {
278 if (configuring) {
279 UDPPadStatus pad;
280 if (touch) {
281 pad.touch = PadTouch::Click;
282 pad_queue[client].Push(pad);
283 }
284 for (size_t i = 0; i < 3; ++i) {
285 if (gyro[i] > 6.0f || gyro[i] < -6.0f) {
286 pad.motion = static_cast<PadMotion>(i);
287 pad.motion_value = gyro[i];
288 pad_queue[client].Push(pad);
289 }
290 if (acc[i] > 2.0f || acc[i] < -2.0f) {
291 pad.motion = static_cast<PadMotion>(i + 3);
292 pad.motion_value = acc[i];
293 pad_queue[client].Push(pad);
294 }
295 }
296 }
297}
298
299void Client::BeginConfiguration() {
300 for (auto& pq : pad_queue) {
301 pq.Clear();
302 }
303 configuring = true;
304}
305
306void Client::EndConfiguration() {
307 for (auto& pq : pad_queue) {
308 pq.Clear();
309 }
310 configuring = false;
311}
312
313DeviceStatus& Client::GetPadState(std::size_t pad) {
314 return clients[pad].status;
315}
316
317const DeviceStatus& Client::GetPadState(std::size_t pad) const {
318 return clients[pad].status;
319}
320
321std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() {
322 return pad_queue;
323}
324
325const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const {
326 return pad_queue;
216} 327}
217 328
218void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 329void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id,
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index a73283ae8..523dc6a7a 100644
--- a/src/input_common/udp/client.h
+++ b/src/input_common/udp/client.h
@@ -12,9 +12,12 @@
12#include <thread> 12#include <thread>
13#include <tuple> 13#include <tuple>
14#include "common/common_types.h" 14#include "common/common_types.h"
15#include "common/param_package.h"
15#include "common/thread.h" 16#include "common/thread.h"
17#include "common/threadsafe_queue.h"
16#include "common/vector_math.h" 18#include "common/vector_math.h"
17#include "core/frontend/input.h" 19#include "core/frontend/input.h"
20#include "input_common/motion_input.h"
18 21
19namespace InputCommon::CemuhookUDP { 22namespace InputCommon::CemuhookUDP {
20 23
@@ -29,6 +32,27 @@ struct PortInfo;
29struct Version; 32struct Version;
30} // namespace Response 33} // namespace Response
31 34
35enum class PadMotion {
36 GyroX,
37 GyroY,
38 GyroZ,
39 AccX,
40 AccY,
41 AccZ,
42 Undefined,
43};
44
45enum class PadTouch {
46 Click,
47 Undefined,
48};
49
50struct UDPPadStatus {
51 PadTouch touch{PadTouch::Undefined};
52 PadMotion motion{PadMotion::Undefined};
53 f32 motion_value{0.0f};
54};
55
32struct DeviceStatus { 56struct DeviceStatus {
33 std::mutex update_mutex; 57 std::mutex update_mutex;
34 Input::MotionStatus motion_status; 58 Input::MotionStatus motion_status;
@@ -46,22 +70,58 @@ struct DeviceStatus {
46 70
47class Client { 71class Client {
48public: 72public:
49 explicit Client(std::shared_ptr<DeviceStatus> status, const std::string& host = DEFAULT_ADDR, 73 // Initialize the UDP client capture and read sequence
50 u16 port = DEFAULT_PORT, u8 pad_index = 0, u32 client_id = 24872); 74 Client();
75
76 // Close and release the client
51 ~Client(); 77 ~Client();
78
79 // Used for polling
80 void BeginConfiguration();
81 void EndConfiguration();
82
83 std::vector<Common::ParamPackage> GetInputDevices() const;
84
85 bool DeviceConnected(std::size_t pad) const;
86 void ReloadUDPClient();
52 void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, 87 void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0,
53 u32 client_id = 24872); 88 u32 client_id = 24872);
54 89
90 std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue();
91 const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const;
92
93 DeviceStatus& GetPadState(std::size_t pad);
94 const DeviceStatus& GetPadState(std::size_t pad) const;
95
55private: 96private:
97 struct ClientData {
98 std::unique_ptr<Socket> socket;
99 DeviceStatus status;
100 std::thread thread;
101 u64 packet_sequence = 0;
102 u8 active;
103
104 // Realtime values
105 // motion is initalized with PID values for drift correction on joycons
106 InputCommon::MotionInput motion{0.3f, 0.005f, 0.0f};
107 std::chrono::time_point<std::chrono::system_clock> last_motion_update;
108 };
109
110 // For shutting down, clear all data, join all threads, release usb
111 void Reset();
112
56 void OnVersion(Response::Version); 113 void OnVersion(Response::Version);
57 void OnPortInfo(Response::PortInfo); 114 void OnPortInfo(Response::PortInfo);
58 void OnPadData(Response::PadData); 115 void OnPadData(Response::PadData);
59 void StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id); 116 void StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index,
117 u32 client_id);
118 void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
119 const Common::Vec3<float>& gyro, bool touch);
120
121 bool configuring = false;
60 122
61 std::unique_ptr<Socket> socket; 123 std::array<ClientData, 4> clients;
62 std::shared_ptr<DeviceStatus> status; 124 std::array<Common::SPSCQueue<UDPPadStatus>, 4> pad_queue;
63 std::thread thread;
64 u64 packet_sequence = 0;
65}; 125};
66 126
67/// An async job allowing configuration of the touchpad calibration. 127/// An async job allowing configuration of the touchpad calibration.
diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp
index 03bae5752..eba077a36 100644
--- a/src/input_common/udp/udp.cpp
+++ b/src/input_common/udp/udp.cpp
@@ -1,105 +1,144 @@
1// Copyright 2018 Citra Emulator Project 1// Copyright 2020 yuzu Emulator Project
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>
6#include <list>
5#include <mutex> 7#include <mutex>
6#include <optional> 8#include <utility>
7#include <tuple> 9#include "common/assert.h"
8 10#include "common/threadsafe_queue.h"
9#include "common/param_package.h"
10#include "core/frontend/input.h"
11#include "core/settings.h"
12#include "input_common/udp/client.h" 11#include "input_common/udp/client.h"
13#include "input_common/udp/udp.h" 12#include "input_common/udp/udp.h"
14 13
15namespace InputCommon::CemuhookUDP { 14namespace InputCommon {
16 15
17class UDPTouchDevice final : public Input::TouchDevice { 16class UDPMotion final : public Input::MotionDevice {
18public: 17public:
19 explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} 18 UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
20 std::tuple<float, float, bool> GetStatus() const override { 19 : ip(ip_), port(port_), pad(pad_), client(client_) {}
21 std::lock_guard guard(status->update_mutex);
22 return status->touch_status;
23 }
24
25private:
26 std::shared_ptr<DeviceStatus> status;
27};
28 20
29class UDPMotionDevice final : public Input::MotionDevice {
30public:
31 explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
32 Input::MotionStatus GetStatus() const override { 21 Input::MotionStatus GetStatus() const override {
33 std::lock_guard guard(status->update_mutex); 22 return client->GetPadState(pad).motion_status;
34 return status->motion_status;
35 } 23 }
36 24
37private: 25private:
38 std::shared_ptr<DeviceStatus> status; 26 const std::string ip;
27 const int port;
28 const int pad;
29 CemuhookUDP::Client* client;
30 mutable std::mutex mutex;
39}; 31};
40 32
41class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { 33/// A motion device factory that creates motion devices from JC Adapter
42public: 34UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_)
43 explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} 35 : client(std::move(client_)) {}
44 36
45 std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override { 37/**
46 { 38 * Creates motion device
47 std::lock_guard guard(status->update_mutex); 39 * @param params contains parameters for creating the device:
48 status->touch_calibration = DeviceStatus::CalibrationData{}; 40 * - "port": the nth jcpad on the adapter
49 // These default values work well for DS4 but probably not other touch inputs 41 */
50 status->touch_calibration->min_x = params.Get("min_x", 100); 42std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) {
51 status->touch_calibration->min_y = params.Get("min_y", 50); 43 const std::string ip = params.Get("ip", "127.0.0.1");
52 status->touch_calibration->max_x = params.Get("max_x", 1800); 44 const int port = params.Get("port", 26760);
53 status->touch_calibration->max_y = params.Get("max_y", 850); 45 const int pad = params.Get("pad_index", 0);
46
47 return std::make_unique<UDPMotion>(ip, port, pad, client.get());
48}
49
50void UDPMotionFactory::BeginConfiguration() {
51 polling = true;
52 client->BeginConfiguration();
53}
54
55void UDPMotionFactory::EndConfiguration() {
56 polling = false;
57 client->EndConfiguration();
58}
59
60Common::ParamPackage UDPMotionFactory::GetNextInput() {
61 Common::ParamPackage params;
62 CemuhookUDP::UDPPadStatus pad;
63 auto& queue = client->GetPadQueue();
64 for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
65 while (queue[pad_number].Pop(pad)) {
66 if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) {
67 continue;
68 }
69 params.Set("engine", "cemuhookudp");
70 params.Set("ip", "127.0.0.1");
71 params.Set("port", 26760);
72 params.Set("pad_index", static_cast<int>(pad_number));
73 params.Set("motion", static_cast<u16>(pad.motion));
74 return params;
54 } 75 }
55 return std::make_unique<UDPTouchDevice>(status);
56 } 76 }
77 return params;
78}
57 79
58private: 80class UDPTouch final : public Input::TouchDevice {
59 std::shared_ptr<DeviceStatus> status;
60};
61
62class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> {
63public: 81public:
64 explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} 82 UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
83 : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
65 84
66 std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { 85 std::tuple<float, float, bool> GetStatus() const override {
67 return std::make_unique<UDPMotionDevice>(status); 86 return client->GetPadState(pad).touch_status;
68 } 87 }
69 88
70private: 89private:
71 std::shared_ptr<DeviceStatus> status; 90 const std::string ip;
91 const int port;
92 const int pad;
93 CemuhookUDP::Client* client;
94 mutable std::mutex mutex;
72}; 95};
73 96
74State::State() { 97/// A motion device factory that creates motion devices from JC Adapter
75 auto status = std::make_shared<DeviceStatus>(); 98UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_)
76 client = 99 : client(std::move(client_)) {}
77 std::make_unique<Client>(status, Settings::values.udp_input_address, 100
78 Settings::values.udp_input_port, Settings::values.udp_pad_index); 101/**
79 102 * Creates motion device
80 motion_factory = std::make_shared<UDPMotionFactory>(status); 103 * @param params contains parameters for creating the device:
81 touch_factory = std::make_shared<UDPTouchFactory>(status); 104 * - "port": the nth jcpad on the adapter
82 105 */
83 Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", motion_factory); 106std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) {
84 Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", touch_factory); 107 const std::string ip = params.Get("ip", "127.0.0.1");
108 const int port = params.Get("port", 26760);
109 const int pad = params.Get("pad_index", 0);
110
111 return std::make_unique<UDPTouch>(ip, port, pad, client.get());
85} 112}
86 113
87State::~State() { 114void UDPTouchFactory::BeginConfiguration() {
88 Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp"); 115 polling = true;
89 Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp"); 116 client->BeginConfiguration();
90} 117}
91 118
92std::vector<Common::ParamPackage> State::GetInputDevices() const { 119void UDPTouchFactory::EndConfiguration() {
93 // TODO support binding udp devices 120 polling = false;
94 return {}; 121 client->EndConfiguration();
95} 122}
96 123
97void State::ReloadUDPClient() { 124Common::ParamPackage UDPTouchFactory::GetNextInput() {
98 client->ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, 125 Common::ParamPackage params;
99 Settings::values.udp_pad_index); 126 CemuhookUDP::UDPPadStatus pad;
127 auto& queue = client->GetPadQueue();
128 for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
129 while (queue[pad_number].Pop(pad)) {
130 if (pad.touch == CemuhookUDP::PadTouch::Undefined) {
131 continue;
132 }
133 params.Set("engine", "cemuhookudp");
134 params.Set("ip", "127.0.0.1");
135 params.Set("port", 26760);
136 params.Set("pad_index", static_cast<int>(pad_number));
137 params.Set("touch", static_cast<u16>(pad.touch));
138 return params;
139 }
140 }
141 return params;
100} 142}
101 143
102std::unique_ptr<State> Init() { 144} // namespace InputCommon
103 return std::make_unique<State>();
104}
105} // namespace InputCommon::CemuhookUDP
diff --git a/src/input_common/udp/udp.h b/src/input_common/udp/udp.h
index 672a5c812..ea3fd4175 100644
--- a/src/input_common/udp/udp.h
+++ b/src/input_common/udp/udp.h
@@ -1,32 +1,57 @@
1// Copyright 2018 Citra Emulator Project 1// Copyright 2020 yuzu Emulator Project
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#pragma once 5#pragma once
6 6
7#include <memory> 7#include <memory>
8#include <vector> 8#include "core/frontend/input.h"
9#include "common/param_package.h" 9#include "input_common/udp/client.h"
10 10
11namespace InputCommon::CemuhookUDP { 11namespace InputCommon {
12 12
13class Client; 13/// A motion device factory that creates motion devices from udp clients
14class UDPMotionFactory; 14class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> {
15class UDPTouchFactory;
16
17class State {
18public: 15public:
19 State(); 16 explicit UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_);
20 ~State(); 17
21 void ReloadUDPClient(); 18 std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override;
22 std::vector<Common::ParamPackage> GetInputDevices() const; 19
20 Common::ParamPackage GetNextInput();
21
22 /// For device input configuration/polling
23 void BeginConfiguration();
24 void EndConfiguration();
25
26 bool IsPolling() const {
27 return polling;
28 }
23 29
24private: 30private:
25 std::unique_ptr<Client> client; 31 std::shared_ptr<CemuhookUDP::Client> client;
26 std::shared_ptr<UDPMotionFactory> motion_factory; 32 bool polling = false;
27 std::shared_ptr<UDPTouchFactory> touch_factory;
28}; 33};
29 34
30std::unique_ptr<State> Init(); 35/// A touch device factory that creates touch devices from udp clients
36class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> {
37public:
38 explicit UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_);
39
40 std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override;
41
42 Common::ParamPackage GetNextInput();
43
44 /// For device input configuration/polling
45 void BeginConfiguration();
46 void EndConfiguration();
47
48 bool IsPolling() const {
49 return polling;
50 }
51
52private:
53 std::shared_ptr<CemuhookUDP::Client> client;
54 bool polling = false;
55};
31 56
32} // namespace InputCommon::CemuhookUDP 57} // namespace InputCommon
diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp
index 7f4b794dc..55ea7ccde 100644
--- a/src/yuzu/configuration/configure_input_player.cpp
+++ b/src/yuzu/configuration/configure_input_player.cpp
@@ -18,6 +18,7 @@
18#include "core/hle/service/sm/sm.h" 18#include "core/hle/service/sm/sm.h"
19#include "input_common/gcadapter/gc_poller.h" 19#include "input_common/gcadapter/gc_poller.h"
20#include "input_common/main.h" 20#include "input_common/main.h"
21#include "input_common/udp/udp.h"
21#include "ui_configure_input_player.h" 22#include "ui_configure_input_player.h"
22#include "yuzu/configuration/config.h" 23#include "yuzu/configuration/config.h"
23#include "yuzu/configuration/configure_input_player.h" 24#include "yuzu/configuration/configure_input_player.h"
@@ -149,6 +150,14 @@ QString ButtonToText(const Common::ParamPackage& param) {
149 return GetKeyName(param.Get("code", 0)); 150 return GetKeyName(param.Get("code", 0));
150 } 151 }
151 152
153 if (param.Get("engine", "") == "cemuhookudp") {
154 if (param.Has("pad_index")) {
155 const QString motion_str = QString::fromStdString(param.Get("pad_index", ""));
156 return QObject::tr("Motion %1").arg(motion_str);
157 }
158 return GetKeyName(param.Get("code", 0));
159 }
160
152 if (param.Get("engine", "") == "sdl") { 161 if (param.Get("engine", "") == "sdl") {
153 if (param.Has("hat")) { 162 if (param.Has("hat")) {
154 const QString hat_str = QString::fromStdString(param.Get("hat", "")); 163 const QString hat_str = QString::fromStdString(param.Get("hat", ""));
@@ -455,6 +464,13 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
455 return; 464 return;
456 } 465 }
457 } 466 }
467 if (input_subsystem->GetUDPMotions()->IsPolling()) {
468 params = input_subsystem->GetUDPMotions()->GetNextInput();
469 if (params.Has("engine")) {
470 SetPollingResult(params, false);
471 return;
472 }
473 }
458 for (auto& poller : device_pollers) { 474 for (auto& poller : device_pollers) {
459 params = poller->GetNextInput(); 475 params = poller->GetNextInput();
460 if (params.Has("engine")) { 476 if (params.Has("engine")) {
@@ -746,6 +762,10 @@ void ConfigureInputPlayer::HandleClick(
746 input_subsystem->GetGCAnalogs()->BeginConfiguration(); 762 input_subsystem->GetGCAnalogs()->BeginConfiguration();
747 } 763 }
748 764
765 if (type == InputCommon::Polling::DeviceType::Motion) {
766 input_subsystem->GetUDPMotions()->BeginConfiguration();
767 }
768
749 timeout_timer->start(2500); // Cancel after 2.5 seconds 769 timeout_timer->start(2500); // Cancel after 2.5 seconds
750 poll_timer->start(50); // Check for new inputs every 50ms 770 poll_timer->start(50); // Check for new inputs every 50ms
751} 771}
@@ -763,6 +783,8 @@ void ConfigureInputPlayer::SetPollingResult(const Common::ParamPackage& params,
763 input_subsystem->GetGCButtons()->EndConfiguration(); 783 input_subsystem->GetGCButtons()->EndConfiguration();
764 input_subsystem->GetGCAnalogs()->EndConfiguration(); 784 input_subsystem->GetGCAnalogs()->EndConfiguration();
765 785
786 input_subsystem->GetUDPMotions()->EndConfiguration();
787
766 if (!abort) { 788 if (!abort) {
767 (*input_setter)(params); 789 (*input_setter)(params);
768 } 790 }