summaryrefslogtreecommitdiff
path: root/src/input_common/udp/client.cpp
diff options
context:
space:
mode:
authorGravatar german2020-09-04 21:35:42 -0500
committerGravatar german2020-09-04 21:48:13 -0500
commit6ee8eab670acfed494ade355d77a32c57f7c9585 (patch)
treeca91a7ca7ac7861ae48c5456870eee425fc8c209 /src/input_common/udp/client.cpp
parentRemove RealMotionDevice (diff)
downloadyuzu-6ee8eab670acfed494ade355d77a32c57f7c9585.tar.gz
yuzu-6ee8eab670acfed494ade355d77a32c57f7c9585.tar.xz
yuzu-6ee8eab670acfed494ade355d77a32c57f7c9585.zip
Add cemu hook changes related to PR #4609
Diffstat (limited to 'src/input_common/udp/client.cpp')
-rw-r--r--src/input_common/udp/client.cpp187
1 files changed, 149 insertions, 38 deletions
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,