diff options
Diffstat (limited to 'src/input_common/udp')
| -rw-r--r-- | src/input_common/udp/client.cpp | 226 | ||||
| -rw-r--r-- | src/input_common/udp/client.h | 89 | ||||
| -rw-r--r-- | src/input_common/udp/udp.cpp | 175 | ||||
| -rw-r--r-- | src/input_common/udp/udp.h | 54 |
4 files changed, 404 insertions, 140 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 3f4eaf448..7039d6fc3 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 | ||
| @@ -27,11 +26,11 @@ class Socket { | |||
| 27 | public: | 26 | public: |
| 28 | using clock = std::chrono::system_clock; | 27 | using clock = std::chrono::system_clock; |
| 29 | 28 | ||
| 30 | explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id, | 29 | explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_, |
| 31 | SocketCallback callback) | 30 | SocketCallback callback_) |
| 32 | : callback(std::move(callback)), timer(io_service), | 31 | : callback(std::move(callback_)), timer(io_service), |
| 33 | socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id), | 32 | socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_), |
| 34 | pad_index(pad_index) { | 33 | pad_index(pad_index_) { |
| 35 | boost::system::error_code ec{}; | 34 | boost::system::error_code ec{}; |
| 36 | auto ipv4 = boost::asio::ip::make_address_v4(host, ec); | 35 | auto ipv4 = boost::asio::ip::make_address_v4(host, ec); |
| 37 | if (ec.value() != boost::system::errc::success) { | 36 | if (ec.value() != boost::system::errc::success) { |
| @@ -94,13 +93,17 @@ private: | |||
| 94 | void HandleSend(const boost::system::error_code& error) { | 93 | void HandleSend(const boost::system::error_code& error) { |
| 95 | boost::system::error_code _ignored{}; | 94 | boost::system::error_code _ignored{}; |
| 96 | // Send a request for getting port info for the pad | 95 | // Send a request for getting port info for the pad |
| 97 | Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; | 96 | const Request::PortInfo port_info{1, {static_cast<u8>(pad_index), 0, 0, 0}}; |
| 98 | const auto port_message = Request::Create(port_info, client_id); | 97 | const auto port_message = Request::Create(port_info, client_id); |
| 99 | std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); | 98 | std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); |
| 100 | socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); | 99 | socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); |
| 101 | 100 | ||
| 102 | // Send a request for getting pad data for the pad | 101 | // Send a request for getting pad data for the pad |
| 103 | Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; | 102 | const Request::PadData pad_data{ |
| 103 | Request::PadData::Flags::Id, | ||
| 104 | static_cast<u8>(pad_index), | ||
| 105 | EMPTY_MAC_ADDRESS, | ||
| 106 | }; | ||
| 104 | const auto pad_message = Request::Create(pad_data, client_id); | 107 | const auto pad_message = Request::Create(pad_data, client_id); |
| 105 | std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); | 108 | std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); |
| 106 | socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); | 109 | socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); |
| @@ -113,7 +116,7 @@ private: | |||
| 113 | udp::socket socket; | 116 | udp::socket socket; |
| 114 | 117 | ||
| 115 | u32 client_id{}; | 118 | u32 client_id{}; |
| 116 | u8 pad_index{}; | 119 | std::size_t pad_index{}; |
| 117 | 120 | ||
| 118 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); | 121 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); |
| 119 | static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); | 122 | static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); |
| @@ -131,21 +134,59 @@ static void SocketLoop(Socket* socket) { | |||
| 131 | socket->Loop(); | 134 | socket->Loop(); |
| 132 | } | 135 | } |
| 133 | 136 | ||
| 134 | Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port, | 137 | Client::Client() { |
| 135 | u8 pad_index, u32 client_id) | 138 | LOG_INFO(Input, "Udp Initialization started"); |
| 136 | : status(std::move(status)) { | 139 | for (std::size_t client = 0; client < clients.size(); client++) { |
| 137 | StartCommunication(host, port, pad_index, client_id); | 140 | const auto pad = client % 4; |
| 141 | StartCommunication(client, Settings::values.udp_input_address, | ||
| 142 | Settings::values.udp_input_port, pad, 24872); | ||
| 143 | // Set motion parameters | ||
| 144 | // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode | ||
| 145 | // Real HW values are unknown, 0.0001 is an approximate to Standard | ||
| 146 | clients[client].motion.SetGyroThreshold(0.0001f); | ||
| 147 | } | ||
| 138 | } | 148 | } |
| 139 | 149 | ||
| 140 | Client::~Client() { | 150 | Client::~Client() { |
| 141 | socket->Stop(); | 151 | Reset(); |
| 142 | thread.join(); | 152 | } |
| 153 | |||
| 154 | std::vector<Common::ParamPackage> Client::GetInputDevices() const { | ||
| 155 | std::vector<Common::ParamPackage> devices; | ||
| 156 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 157 | if (!DeviceConnected(client)) { | ||
| 158 | continue; | ||
| 159 | } | ||
| 160 | std::string name = fmt::format("UDP Controller {}", client); | ||
| 161 | devices.emplace_back(Common::ParamPackage{ | ||
| 162 | {"class", "cemuhookudp"}, | ||
| 163 | {"display", std::move(name)}, | ||
| 164 | {"port", std::to_string(client)}, | ||
| 165 | }); | ||
| 166 | } | ||
| 167 | return devices; | ||
| 143 | } | 168 | } |
| 144 | 169 | ||
| 145 | void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { | 170 | bool Client::DeviceConnected(std::size_t pad) const { |
| 146 | socket->Stop(); | 171 | // Use last timestamp to detect if the socket has stopped sending data |
| 147 | thread.join(); | 172 | const auto now = std::chrono::system_clock::now(); |
| 148 | StartCommunication(host, port, pad_index, client_id); | 173 | const auto time_difference = static_cast<u64>( |
| 174 | std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update) | ||
| 175 | .count()); | ||
| 176 | return time_difference < 1000 && clients[pad].active == 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | void Client::ReloadUDPClient() { | ||
| 180 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 181 | ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | void Client::ReloadSocket(const std::string& host, u16 port, std::size_t pad_index, u32 client_id) { | ||
| 185 | // client number must be determined from host / port and pad index | ||
| 186 | const std::size_t client = pad_index; | ||
| 187 | clients[client].socket->Stop(); | ||
| 188 | clients[client].thread.join(); | ||
| 189 | StartCommunication(client, host, port, pad_index, client_id); | ||
| 149 | } | 190 | } |
| 150 | 191 | ||
| 151 | void Client::OnVersion(Response::Version data) { | 192 | void Client::OnVersion(Response::Version data) { |
| @@ -157,23 +198,36 @@ void Client::OnPortInfo(Response::PortInfo data) { | |||
| 157 | } | 198 | } |
| 158 | 199 | ||
| 159 | void Client::OnPadData(Response::PadData data) { | 200 | void Client::OnPadData(Response::PadData data) { |
| 201 | // Client number must be determined from host / port and pad index | ||
| 202 | const std::size_t client = data.info.id; | ||
| 160 | LOG_TRACE(Input, "PadData packet received"); | 203 | LOG_TRACE(Input, "PadData packet received"); |
| 161 | if (data.packet_counter <= packet_sequence) { | 204 | if (data.packet_counter == clients[client].packet_sequence) { |
| 162 | LOG_WARNING( | 205 | LOG_WARNING( |
| 163 | Input, | 206 | Input, |
| 164 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", | 207 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", |
| 165 | packet_sequence, data.packet_counter); | 208 | clients[client].packet_sequence, data.packet_counter); |
| 166 | return; | 209 | return; |
| 167 | } | 210 | } |
| 168 | packet_sequence = data.packet_counter; | 211 | clients[client].active = data.info.is_pad_active; |
| 169 | // TODO: Check how the Switch handles motions and how the CemuhookUDP motion | 212 | clients[client].packet_sequence = data.packet_counter; |
| 170 | // directions correspond to the ones of the Switch | 213 | const auto now = std::chrono::system_clock::now(); |
| 171 | Common::Vec3f accel = Common::MakeVec<float>(data.accel.x, data.accel.y, data.accel.z); | 214 | const auto time_difference = |
| 172 | Common::Vec3f gyro = Common::MakeVec<float>(data.gyro.pitch, data.gyro.yaw, data.gyro.roll); | 215 | static_cast<u64>(std::chrono::duration_cast<std::chrono::microseconds>( |
| 173 | { | 216 | now - clients[client].last_motion_update) |
| 174 | std::lock_guard guard(status->update_mutex); | 217 | .count()); |
| 218 | clients[client].last_motion_update = now; | ||
| 219 | const Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; | ||
| 220 | clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); | ||
| 221 | // Gyroscope values are not it the correct scale from better joy. | ||
| 222 | // Dividing by 312 allows us to make one full turn = 1 turn | ||
| 223 | // This must be a configurable valued called sensitivity | ||
| 224 | clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f); | ||
| 225 | clients[client].motion.UpdateRotation(time_difference); | ||
| 226 | clients[client].motion.UpdateOrientation(time_difference); | ||
| 175 | 227 | ||
| 176 | status->motion_status = {accel, gyro}; | 228 | { |
| 229 | std::lock_guard guard(clients[client].status.update_mutex); | ||
| 230 | clients[client].status.motion_status = clients[client].motion.GetMotion(); | ||
| 177 | 231 | ||
| 178 | // 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 |
| 179 | // 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. |
| @@ -182,41 +236,115 @@ void Client::OnPadData(Response::PadData data) { | |||
| 182 | float x = 0; | 236 | float x = 0; |
| 183 | float y = 0; | 237 | float y = 0; |
| 184 | 238 | ||
| 185 | if (is_active && status->touch_calibration) { | 239 | if (is_active && clients[client].status.touch_calibration) { |
| 186 | const u16 min_x = status->touch_calibration->min_x; | 240 | const u16 min_x = clients[client].status.touch_calibration->min_x; |
| 187 | const u16 max_x = status->touch_calibration->max_x; | 241 | const u16 max_x = clients[client].status.touch_calibration->max_x; |
| 188 | const u16 min_y = status->touch_calibration->min_y; | 242 | const u16 min_y = clients[client].status.touch_calibration->min_y; |
| 189 | const u16 max_y = status->touch_calibration->max_y; | 243 | const u16 max_y = clients[client].status.touch_calibration->max_y; |
| 190 | 244 | ||
| 191 | x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / | 245 | x = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - |
| 246 | min_x) / | ||
| 192 | static_cast<float>(max_x - min_x); | 247 | static_cast<float>(max_x - min_x); |
| 193 | y = (std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - min_y) / | 248 | y = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - |
| 249 | min_y) / | ||
| 194 | static_cast<float>(max_y - min_y); | 250 | static_cast<float>(max_y - min_y); |
| 195 | } | 251 | } |
| 196 | 252 | ||
| 197 | status->touch_status = {x, y, is_active}; | 253 | clients[client].status.touch_status = {x, y, is_active}; |
| 254 | |||
| 255 | if (configuring) { | ||
| 256 | const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); | ||
| 257 | const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); | ||
| 258 | UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); | ||
| 259 | } | ||
| 198 | } | 260 | } |
| 199 | } | 261 | } |
| 200 | 262 | ||
| 201 | void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id) { | 263 | void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, |
| 264 | std::size_t pad_index, u32 client_id) { | ||
| 202 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, | 265 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, |
| 203 | [this](Response::PortInfo info) { OnPortInfo(info); }, | 266 | [this](Response::PortInfo info) { OnPortInfo(info); }, |
| 204 | [this](Response::PadData data) { OnPadData(data); }}; | 267 | [this](Response::PadData data) { OnPadData(data); }}; |
| 205 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); | 268 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); |
| 206 | socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); | 269 | clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); |
| 207 | thread = std::thread{SocketLoop, this->socket.get()}; | 270 | clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; |
| 271 | } | ||
| 272 | |||
| 273 | void Client::Reset() { | ||
| 274 | for (auto& client : clients) { | ||
| 275 | client.socket->Stop(); | ||
| 276 | client.thread.join(); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, | ||
| 281 | const Common::Vec3<float>& gyro, bool touch) { | ||
| 282 | if (gyro.Length() > 0.2f) { | ||
| 283 | LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", | ||
| 284 | client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); | ||
| 285 | } | ||
| 286 | UDPPadStatus pad; | ||
| 287 | if (touch) { | ||
| 288 | pad.touch = PadTouch::Click; | ||
| 289 | pad_queue[client].Push(pad); | ||
| 290 | } | ||
| 291 | for (size_t i = 0; i < 3; ++i) { | ||
| 292 | if (gyro[i] > 5.0f || gyro[i] < -5.0f) { | ||
| 293 | pad.motion = static_cast<PadMotion>(i); | ||
| 294 | pad.motion_value = gyro[i]; | ||
| 295 | pad_queue[client].Push(pad); | ||
| 296 | } | ||
| 297 | if (acc[i] > 1.75f || acc[i] < -1.75f) { | ||
| 298 | pad.motion = static_cast<PadMotion>(i + 3); | ||
| 299 | pad.motion_value = acc[i]; | ||
| 300 | pad_queue[client].Push(pad); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | void Client::BeginConfiguration() { | ||
| 306 | for (auto& pq : pad_queue) { | ||
| 307 | pq.Clear(); | ||
| 308 | } | ||
| 309 | configuring = true; | ||
| 310 | } | ||
| 311 | |||
| 312 | void Client::EndConfiguration() { | ||
| 313 | for (auto& pq : pad_queue) { | ||
| 314 | pq.Clear(); | ||
| 315 | } | ||
| 316 | configuring = false; | ||
| 208 | } | 317 | } |
| 209 | 318 | ||
| 210 | void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, | 319 | DeviceStatus& Client::GetPadState(std::size_t pad) { |
| 211 | std::function<void()> success_callback, | 320 | return clients[pad].status; |
| 212 | std::function<void()> failure_callback) { | 321 | } |
| 322 | |||
| 323 | const DeviceStatus& Client::GetPadState(std::size_t pad) const { | ||
| 324 | return clients[pad].status; | ||
| 325 | } | ||
| 326 | |||
| 327 | std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() { | ||
| 328 | return pad_queue; | ||
| 329 | } | ||
| 330 | |||
| 331 | const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const { | ||
| 332 | return pad_queue; | ||
| 333 | } | ||
| 334 | |||
| 335 | void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, | ||
| 336 | const std::function<void()>& success_callback, | ||
| 337 | const std::function<void()>& failure_callback) { | ||
| 213 | std::thread([=] { | 338 | std::thread([=] { |
| 214 | Common::Event success_event; | 339 | Common::Event success_event; |
| 215 | SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {}, | 340 | SocketCallback callback{ |
| 216 | [&](Response::PadData data) { success_event.Set(); }}; | 341 | .version = [](Response::Version) {}, |
| 342 | .port_info = [](Response::PortInfo) {}, | ||
| 343 | .pad_data = [&](Response::PadData) { success_event.Set(); }, | ||
| 344 | }; | ||
| 217 | Socket socket{host, port, pad_index, client_id, std::move(callback)}; | 345 | Socket socket{host, port, pad_index, client_id, std::move(callback)}; |
| 218 | std::thread worker_thread{SocketLoop, &socket}; | 346 | std::thread worker_thread{SocketLoop, &socket}; |
| 219 | bool result = success_event.WaitFor(std::chrono::seconds(8)); | 347 | const bool result = success_event.WaitFor(std::chrono::seconds(8)); |
| 220 | socket.Stop(); | 348 | socket.Stop(); |
| 221 | worker_thread.join(); | 349 | worker_thread.join(); |
| 222 | if (result) { | 350 | if (result) { |
| @@ -228,7 +356,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie | |||
| 228 | } | 356 | } |
| 229 | 357 | ||
| 230 | CalibrationConfigurationJob::CalibrationConfigurationJob( | 358 | CalibrationConfigurationJob::CalibrationConfigurationJob( |
| 231 | const std::string& host, u16 port, u8 pad_index, u32 client_id, | 359 | const std::string& host, u16 port, std::size_t pad_index, u32 client_id, |
| 232 | std::function<void(Status)> status_callback, | 360 | std::function<void(Status)> status_callback, |
| 233 | std::function<void(u16, u16, u16, u16)> data_callback) { | 361 | std::function<void(u16, u16, u16, u16)> data_callback) { |
| 234 | 362 | ||
| @@ -248,7 +376,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( | |||
| 248 | current_status = Status::Ready; | 376 | current_status = Status::Ready; |
| 249 | status_callback(current_status); | 377 | status_callback(current_status); |
| 250 | } | 378 | } |
| 251 | if (!data.touch_1.is_active) { | 379 | if (data.touch_1.is_active == 0) { |
| 252 | return; | 380 | return; |
| 253 | } | 381 | } |
| 254 | LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, | 382 | LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, |
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index b8c654755..747e0c0a2 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h | |||
| @@ -12,8 +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" |
| 19 | #include "core/frontend/input.h" | ||
| 20 | #include "input_common/motion_input.h" | ||
| 17 | 21 | ||
| 18 | namespace InputCommon::CemuhookUDP { | 22 | namespace InputCommon::CemuhookUDP { |
| 19 | 23 | ||
| @@ -28,9 +32,30 @@ struct PortInfo; | |||
| 28 | struct Version; | 32 | struct Version; |
| 29 | } // namespace Response | 33 | } // namespace Response |
| 30 | 34 | ||
| 35 | enum class PadMotion { | ||
| 36 | GyroX, | ||
| 37 | GyroY, | ||
| 38 | GyroZ, | ||
| 39 | AccX, | ||
| 40 | AccY, | ||
| 41 | AccZ, | ||
| 42 | Undefined, | ||
| 43 | }; | ||
| 44 | |||
| 45 | enum class PadTouch { | ||
| 46 | Click, | ||
| 47 | Undefined, | ||
| 48 | }; | ||
| 49 | |||
| 50 | struct UDPPadStatus { | ||
| 51 | PadTouch touch{PadTouch::Undefined}; | ||
| 52 | PadMotion motion{PadMotion::Undefined}; | ||
| 53 | f32 motion_value{0.0f}; | ||
| 54 | }; | ||
| 55 | |||
| 31 | struct DeviceStatus { | 56 | struct DeviceStatus { |
| 32 | std::mutex update_mutex; | 57 | std::mutex update_mutex; |
| 33 | std::tuple<Common::Vec3<float>, Common::Vec3<float>> motion_status; | 58 | Input::MotionStatus motion_status; |
| 34 | std::tuple<float, float, bool> touch_status; | 59 | std::tuple<float, float, bool> touch_status; |
| 35 | 60 | ||
| 36 | // calibration data for scaling the device's touch area to 3ds | 61 | // calibration data for scaling the device's touch area to 3ds |
| @@ -45,22 +70,58 @@ struct DeviceStatus { | |||
| 45 | 70 | ||
| 46 | class Client { | 71 | class Client { |
| 47 | public: | 72 | public: |
| 48 | explicit Client(std::shared_ptr<DeviceStatus> status, const std::string& host = DEFAULT_ADDR, | 73 | // Initialize the UDP client capture and read sequence |
| 49 | u16 port = DEFAULT_PORT, u8 pad_index = 0, u32 client_id = 24872); | 74 | Client(); |
| 75 | |||
| 76 | // Close and release the client | ||
| 50 | ~Client(); | 77 | ~Client(); |
| 51 | void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, | 78 | |
| 52 | u32 client_id = 24872); | 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(); | ||
| 87 | void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, | ||
| 88 | std::size_t pad_index = 0, u32 client_id = 24872); | ||
| 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; | ||
| 53 | 95 | ||
| 54 | private: | 96 | private: |
| 97 | struct ClientData { | ||
| 98 | std::unique_ptr<Socket> socket; | ||
| 99 | DeviceStatus status; | ||
| 100 | std::thread thread; | ||
| 101 | u64 packet_sequence = 0; | ||
| 102 | u8 active = 0; | ||
| 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 | |||
| 55 | void OnVersion(Response::Version); | 113 | void OnVersion(Response::Version); |
| 56 | void OnPortInfo(Response::PortInfo); | 114 | void OnPortInfo(Response::PortInfo); |
| 57 | void OnPadData(Response::PadData); | 115 | void OnPadData(Response::PadData); |
| 58 | 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, |
| 117 | std::size_t pad_index, 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; | ||
| 59 | 122 | ||
| 60 | std::unique_ptr<Socket> socket; | 123 | std::array<ClientData, 4> clients; |
| 61 | std::shared_ptr<DeviceStatus> status; | 124 | std::array<Common::SPSCQueue<UDPPadStatus>, 4> pad_queue; |
| 62 | std::thread thread; | ||
| 63 | u64 packet_sequence = 0; | ||
| 64 | }; | 125 | }; |
| 65 | 126 | ||
| 66 | /// An async job allowing configuration of the touchpad calibration. | 127 | /// An async job allowing configuration of the touchpad calibration. |
| @@ -78,7 +139,7 @@ public: | |||
| 78 | * @param status_callback Callback for job status updates | 139 | * @param status_callback Callback for job status updates |
| 79 | * @param data_callback Called when calibration data is ready | 140 | * @param data_callback Called when calibration data is ready |
| 80 | */ | 141 | */ |
| 81 | explicit CalibrationConfigurationJob(const std::string& host, u16 port, u8 pad_index, | 142 | explicit CalibrationConfigurationJob(const std::string& host, u16 port, std::size_t pad_index, |
| 82 | u32 client_id, std::function<void(Status)> status_callback, | 143 | u32 client_id, std::function<void(Status)> status_callback, |
| 83 | std::function<void(u16, u16, u16, u16)> data_callback); | 144 | std::function<void(u16, u16, u16, u16)> data_callback); |
| 84 | ~CalibrationConfigurationJob(); | 145 | ~CalibrationConfigurationJob(); |
| @@ -88,8 +149,8 @@ private: | |||
| 88 | Common::Event complete_event; | 149 | Common::Event complete_event; |
| 89 | }; | 150 | }; |
| 90 | 151 | ||
| 91 | void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, | 152 | void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, |
| 92 | std::function<void()> success_callback, | 153 | const std::function<void()>& success_callback, |
| 93 | std::function<void()> failure_callback); | 154 | const std::function<void()>& failure_callback); |
| 94 | 155 | ||
| 95 | } // namespace InputCommon::CemuhookUDP | 156 | } // namespace InputCommon::CemuhookUDP |
diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index 8c6ef1394..71a76a7aa 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp | |||
| @@ -1,99 +1,142 @@ | |||
| 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 <mutex> | 5 | #include <mutex> |
| 6 | #include <optional> | 6 | #include <utility> |
| 7 | #include <tuple> | 7 | #include "common/assert.h" |
| 8 | 8 | #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" | 9 | #include "input_common/udp/client.h" |
| 13 | #include "input_common/udp/udp.h" | 10 | #include "input_common/udp/udp.h" |
| 14 | 11 | ||
| 15 | namespace InputCommon::CemuhookUDP { | 12 | namespace InputCommon { |
| 16 | 13 | ||
| 17 | class UDPTouchDevice final : public Input::TouchDevice { | 14 | class UDPMotion final : public Input::MotionDevice { |
| 18 | public: | 15 | public: |
| 19 | explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 16 | explicit UDPMotion(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_) |
| 20 | std::tuple<float, float, bool> GetStatus() const override { | 17 | : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} |
| 21 | std::lock_guard guard(status->update_mutex); | 18 | |
| 22 | return status->touch_status; | 19 | Input::MotionStatus GetStatus() const override { |
| 20 | return client->GetPadState(pad).motion_status; | ||
| 23 | } | 21 | } |
| 24 | 22 | ||
| 25 | private: | 23 | private: |
| 26 | std::shared_ptr<DeviceStatus> status; | 24 | const std::string ip; |
| 25 | const int port; | ||
| 26 | const u32 pad; | ||
| 27 | CemuhookUDP::Client* client; | ||
| 28 | mutable std::mutex mutex; | ||
| 27 | }; | 29 | }; |
| 28 | 30 | ||
| 29 | class UDPMotionDevice final : public Input::MotionDevice { | 31 | /// A motion device factory that creates motion devices from JC Adapter |
| 30 | public: | 32 | UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_) |
| 31 | explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 33 | : client(std::move(client_)) {} |
| 32 | std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override { | 34 | |
| 33 | std::lock_guard guard(status->update_mutex); | 35 | /** |
| 34 | return status->motion_status; | 36 | * Creates motion device |
| 35 | } | 37 | * @param params contains parameters for creating the device: |
| 38 | * - "port": the nth jcpad on the adapter | ||
| 39 | */ | ||
| 40 | std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) { | ||
| 41 | auto ip = params.Get("ip", "127.0.0.1"); | ||
| 42 | const auto port = params.Get("port", 26760); | ||
| 43 | const auto pad = static_cast<u32>(params.Get("pad_index", 0)); | ||
| 44 | |||
| 45 | return std::make_unique<UDPMotion>(std::move(ip), port, pad, client.get()); | ||
| 46 | } | ||
| 36 | 47 | ||
| 37 | private: | 48 | void UDPMotionFactory::BeginConfiguration() { |
| 38 | std::shared_ptr<DeviceStatus> status; | 49 | polling = true; |
| 39 | }; | 50 | client->BeginConfiguration(); |
| 51 | } | ||
| 40 | 52 | ||
| 41 | class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { | 53 | void UDPMotionFactory::EndConfiguration() { |
| 42 | public: | 54 | polling = false; |
| 43 | explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 55 | client->EndConfiguration(); |
| 44 | 56 | } | |
| 45 | std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override { | 57 | |
| 46 | { | 58 | Common::ParamPackage UDPMotionFactory::GetNextInput() { |
| 47 | std::lock_guard guard(status->update_mutex); | 59 | Common::ParamPackage params; |
| 48 | status->touch_calibration = DeviceStatus::CalibrationData{}; | 60 | CemuhookUDP::UDPPadStatus pad; |
| 49 | // These default values work well for DS4 but probably not other touch inputs | 61 | auto& queue = client->GetPadQueue(); |
| 50 | status->touch_calibration->min_x = params.Get("min_x", 100); | 62 | for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) { |
| 51 | status->touch_calibration->min_y = params.Get("min_y", 50); | 63 | while (queue[pad_number].Pop(pad)) { |
| 52 | status->touch_calibration->max_x = params.Get("max_x", 1800); | 64 | if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) { |
| 53 | status->touch_calibration->max_y = params.Get("max_y", 850); | 65 | continue; |
| 66 | } | ||
| 67 | params.Set("engine", "cemuhookudp"); | ||
| 68 | params.Set("ip", "127.0.0.1"); | ||
| 69 | params.Set("port", 26760); | ||
| 70 | params.Set("pad_index", static_cast<int>(pad_number)); | ||
| 71 | params.Set("motion", static_cast<u16>(pad.motion)); | ||
| 72 | return params; | ||
| 54 | } | 73 | } |
| 55 | return std::make_unique<UDPTouchDevice>(status); | ||
| 56 | } | 74 | } |
| 75 | return params; | ||
| 76 | } | ||
| 57 | 77 | ||
| 58 | private: | 78 | class UDPTouch final : public Input::TouchDevice { |
| 59 | std::shared_ptr<DeviceStatus> status; | ||
| 60 | }; | ||
| 61 | |||
| 62 | class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { | ||
| 63 | public: | 79 | public: |
| 64 | explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 80 | explicit UDPTouch(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_) |
| 81 | : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} | ||
| 65 | 82 | ||
| 66 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { | 83 | std::tuple<float, float, bool> GetStatus() const override { |
| 67 | return std::make_unique<UDPMotionDevice>(status); | 84 | return client->GetPadState(pad).touch_status; |
| 68 | } | 85 | } |
| 69 | 86 | ||
| 70 | private: | 87 | private: |
| 71 | std::shared_ptr<DeviceStatus> status; | 88 | const std::string ip; |
| 89 | const int port; | ||
| 90 | const u32 pad; | ||
| 91 | CemuhookUDP::Client* client; | ||
| 92 | mutable std::mutex mutex; | ||
| 72 | }; | 93 | }; |
| 73 | 94 | ||
| 74 | State::State() { | 95 | /// A motion device factory that creates motion devices from JC Adapter |
| 75 | auto status = std::make_shared<DeviceStatus>(); | 96 | UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_) |
| 76 | client = | 97 | : client(std::move(client_)) {} |
| 77 | std::make_unique<Client>(status, Settings::values.udp_input_address, | 98 | |
| 78 | Settings::values.udp_input_port, Settings::values.udp_pad_index); | 99 | /** |
| 79 | 100 | * Creates motion device | |
| 80 | Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", | 101 | * @param params contains parameters for creating the device: |
| 81 | std::make_shared<UDPTouchFactory>(status)); | 102 | * - "port": the nth jcpad on the adapter |
| 82 | Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", | 103 | */ |
| 83 | std::make_shared<UDPMotionFactory>(status)); | 104 | std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) { |
| 105 | auto ip = params.Get("ip", "127.0.0.1"); | ||
| 106 | const auto port = params.Get("port", 26760); | ||
| 107 | const auto pad = static_cast<u32>(params.Get("pad_index", 0)); | ||
| 108 | |||
| 109 | return std::make_unique<UDPTouch>(std::move(ip), port, pad, client.get()); | ||
| 84 | } | 110 | } |
| 85 | 111 | ||
| 86 | State::~State() { | 112 | void UDPTouchFactory::BeginConfiguration() { |
| 87 | Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp"); | 113 | polling = true; |
| 88 | Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp"); | 114 | client->BeginConfiguration(); |
| 89 | } | 115 | } |
| 90 | 116 | ||
| 91 | void State::ReloadUDPClient() { | 117 | void UDPTouchFactory::EndConfiguration() { |
| 92 | client->ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, | 118 | polling = false; |
| 93 | Settings::values.udp_pad_index); | 119 | client->EndConfiguration(); |
| 94 | } | 120 | } |
| 95 | 121 | ||
| 96 | std::unique_ptr<State> Init() { | 122 | Common::ParamPackage UDPTouchFactory::GetNextInput() { |
| 97 | return std::make_unique<State>(); | 123 | Common::ParamPackage params; |
| 124 | CemuhookUDP::UDPPadStatus pad; | ||
| 125 | auto& queue = client->GetPadQueue(); | ||
| 126 | for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) { | ||
| 127 | while (queue[pad_number].Pop(pad)) { | ||
| 128 | if (pad.touch == CemuhookUDP::PadTouch::Undefined) { | ||
| 129 | continue; | ||
| 130 | } | ||
| 131 | params.Set("engine", "cemuhookudp"); | ||
| 132 | params.Set("ip", "127.0.0.1"); | ||
| 133 | params.Set("port", 26760); | ||
| 134 | params.Set("pad_index", static_cast<int>(pad_number)); | ||
| 135 | params.Set("touch", static_cast<u16>(pad.touch)); | ||
| 136 | return params; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | return params; | ||
| 98 | } | 140 | } |
| 99 | } // namespace InputCommon::CemuhookUDP | 141 | |
| 142 | } // namespace InputCommon | ||
diff --git a/src/input_common/udp/udp.h b/src/input_common/udp/udp.h index 4f83f0441..ea3fd4175 100644 --- a/src/input_common/udp/udp.h +++ b/src/input_common/udp/udp.h | |||
| @@ -1,25 +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 "core/frontend/input.h" | ||
| 9 | #include "input_common/udp/client.h" | ||
| 8 | 10 | ||
| 9 | namespace InputCommon::CemuhookUDP { | 11 | namespace InputCommon { |
| 10 | 12 | ||
| 11 | class Client; | 13 | /// A motion device factory that creates motion devices from udp clients |
| 12 | 14 | class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { | |
| 13 | class State { | ||
| 14 | public: | 15 | public: |
| 15 | State(); | 16 | explicit UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_); |
| 16 | ~State(); | 17 | |
| 17 | void ReloadUDPClient(); | 18 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override; |
| 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 | } | ||
| 18 | 29 | ||
| 19 | private: | 30 | private: |
| 20 | std::unique_ptr<Client> client; | 31 | std::shared_ptr<CemuhookUDP::Client> client; |
| 32 | bool polling = false; | ||
| 21 | }; | 33 | }; |
| 22 | 34 | ||
| 23 | std::unique_ptr<State> Init(); | 35 | /// A touch device factory that creates touch devices from udp clients |
| 36 | class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { | ||
| 37 | public: | ||
| 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 | |||
| 52 | private: | ||
| 53 | std::shared_ptr<CemuhookUDP::Client> client; | ||
| 54 | bool polling = false; | ||
| 55 | }; | ||
| 24 | 56 | ||
| 25 | } // namespace InputCommon::CemuhookUDP | 57 | } // namespace InputCommon |