diff options
| author | 2019-08-24 15:57:49 +0200 | |
|---|---|---|
| committer | 2020-01-23 20:55:26 +0100 | |
| commit | ac3690f2057fb93ce18f156ff5ffd720a6d6f60c (patch) | |
| tree | d0ec80a2537b992146d34f5bf17ba0cc549bd88e /src/input_common/udp/client.cpp | |
| parent | Merge pull request #3341 from bunnei/time-posix-myrule (diff) | |
| download | yuzu-ac3690f2057fb93ce18f156ff5ffd720a6d6f60c.tar.gz yuzu-ac3690f2057fb93ce18f156ff5ffd720a6d6f60c.tar.xz yuzu-ac3690f2057fb93ce18f156ff5ffd720a6d6f60c.zip | |
Input: UDP Client to provide motion and touch controls
An implementation of the cemuhook motion/touch protocol, this adds the
ability for users to connect several different devices to citra to send
direct motion and touch data to citra.
Co-Authored-By: jroweboy <jroweboy@gmail.com>
Diffstat (limited to 'src/input_common/udp/client.cpp')
| -rw-r--r-- | src/input_common/udp/client.cpp | 283 |
1 files changed, 283 insertions, 0 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp new file mode 100644 index 000000000..c31236c7c --- /dev/null +++ b/src/input_common/udp/client.cpp | |||
| @@ -0,0 +1,283 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <chrono> | ||
| 8 | #include <cstring> | ||
| 9 | #include <functional> | ||
| 10 | #include <thread> | ||
| 11 | #include <boost/asio.hpp> | ||
| 12 | #include <boost/bind.hpp> | ||
| 13 | #include "common/logging/log.h" | ||
| 14 | #include "input_common/udp/client.h" | ||
| 15 | #include "input_common/udp/protocol.h" | ||
| 16 | |||
| 17 | using boost::asio::ip::address_v4; | ||
| 18 | using boost::asio::ip::udp; | ||
| 19 | |||
| 20 | namespace InputCommon::CemuhookUDP { | ||
| 21 | |||
| 22 | struct SocketCallback { | ||
| 23 | std::function<void(Response::Version)> version; | ||
| 24 | std::function<void(Response::PortInfo)> port_info; | ||
| 25 | std::function<void(Response::PadData)> pad_data; | ||
| 26 | }; | ||
| 27 | |||
| 28 | class Socket { | ||
| 29 | public: | ||
| 30 | using clock = std::chrono::system_clock; | ||
| 31 | |||
| 32 | explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id, | ||
| 33 | SocketCallback callback) | ||
| 34 | : client_id(client_id), timer(io_service), | ||
| 35 | send_endpoint(udp::endpoint(address_v4::from_string(host), port)), | ||
| 36 | socket(io_service, udp::endpoint(udp::v4(), 0)), pad_index(pad_index), | ||
| 37 | callback(std::move(callback)) {} | ||
| 38 | |||
| 39 | void Stop() { | ||
| 40 | io_service.stop(); | ||
| 41 | } | ||
| 42 | |||
| 43 | void Loop() { | ||
| 44 | io_service.run(); | ||
| 45 | } | ||
| 46 | |||
| 47 | void StartSend(const clock::time_point& from) { | ||
| 48 | timer.expires_at(from + std::chrono::seconds(3)); | ||
| 49 | timer.async_wait([this](const boost::system::error_code& error) { HandleSend(error); }); | ||
| 50 | } | ||
| 51 | |||
| 52 | void StartReceive() { | ||
| 53 | socket.async_receive_from( | ||
| 54 | boost::asio::buffer(receive_buffer), receive_endpoint, | ||
| 55 | [this](const boost::system::error_code& error, std::size_t bytes_transferred) { | ||
| 56 | HandleReceive(error, bytes_transferred); | ||
| 57 | }); | ||
| 58 | } | ||
| 59 | |||
| 60 | private: | ||
| 61 | void HandleReceive(const boost::system::error_code& error, std::size_t bytes_transferred) { | ||
| 62 | if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) { | ||
| 63 | switch (*type) { | ||
| 64 | case Type::Version: { | ||
| 65 | Response::Version version; | ||
| 66 | std::memcpy(&version, &receive_buffer[sizeof(Header)], sizeof(Response::Version)); | ||
| 67 | callback.version(std::move(version)); | ||
| 68 | break; | ||
| 69 | } | ||
| 70 | case Type::PortInfo: { | ||
| 71 | Response::PortInfo port_info; | ||
| 72 | std::memcpy(&port_info, &receive_buffer[sizeof(Header)], | ||
| 73 | sizeof(Response::PortInfo)); | ||
| 74 | callback.port_info(std::move(port_info)); | ||
| 75 | break; | ||
| 76 | } | ||
| 77 | case Type::PadData: { | ||
| 78 | Response::PadData pad_data; | ||
| 79 | std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData)); | ||
| 80 | callback.pad_data(std::move(pad_data)); | ||
| 81 | break; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | StartReceive(); | ||
| 86 | } | ||
| 87 | |||
| 88 | void HandleSend(const boost::system::error_code& error) { | ||
| 89 | // Send a request for getting port info for the pad | ||
| 90 | Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; | ||
| 91 | auto port_message = Request::Create(port_info, client_id); | ||
| 92 | std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); | ||
| 93 | std::size_t len = socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint); | ||
| 94 | |||
| 95 | // Send a request for getting pad data for the pad | ||
| 96 | Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; | ||
| 97 | auto pad_message = Request::Create(pad_data, client_id); | ||
| 98 | std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); | ||
| 99 | std::size_t len2 = socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint); | ||
| 100 | StartSend(timer.expiry()); | ||
| 101 | } | ||
| 102 | |||
| 103 | SocketCallback callback; | ||
| 104 | boost::asio::io_service io_service; | ||
| 105 | boost::asio::basic_waitable_timer<clock> timer; | ||
| 106 | udp::socket socket; | ||
| 107 | |||
| 108 | u32 client_id; | ||
| 109 | u8 pad_index; | ||
| 110 | |||
| 111 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); | ||
| 112 | static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); | ||
| 113 | std::array<u8, PORT_INFO_SIZE> send_buffer1; | ||
| 114 | std::array<u8, PAD_DATA_SIZE> send_buffer2; | ||
| 115 | udp::endpoint send_endpoint; | ||
| 116 | |||
| 117 | std::array<u8, MAX_PACKET_SIZE> receive_buffer; | ||
| 118 | udp::endpoint receive_endpoint; | ||
| 119 | }; | ||
| 120 | |||
| 121 | static void SocketLoop(Socket* socket) { | ||
| 122 | socket->StartReceive(); | ||
| 123 | socket->StartSend(Socket::clock::now()); | ||
| 124 | socket->Loop(); | ||
| 125 | } | ||
| 126 | |||
| 127 | Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port, | ||
| 128 | u8 pad_index, u32 client_id) | ||
| 129 | : status(status) { | ||
| 130 | StartCommunication(host, port, pad_index, client_id); | ||
| 131 | } | ||
| 132 | |||
| 133 | Client::~Client() { | ||
| 134 | socket->Stop(); | ||
| 135 | thread.join(); | ||
| 136 | } | ||
| 137 | |||
| 138 | void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { | ||
| 139 | socket->Stop(); | ||
| 140 | thread.join(); | ||
| 141 | StartCommunication(host, port, pad_index, client_id); | ||
| 142 | } | ||
| 143 | |||
| 144 | void Client::OnVersion(Response::Version data) { | ||
| 145 | LOG_TRACE(Input, "Version packet received: {}", data.version); | ||
| 146 | } | ||
| 147 | |||
| 148 | void Client::OnPortInfo(Response::PortInfo data) { | ||
| 149 | LOG_TRACE(Input, "PortInfo packet received: {}", data.model); | ||
| 150 | } | ||
| 151 | |||
| 152 | void Client::OnPadData(Response::PadData data) { | ||
| 153 | LOG_TRACE(Input, "PadData packet received"); | ||
| 154 | if (data.packet_counter <= packet_sequence) { | ||
| 155 | LOG_WARNING( | ||
| 156 | Input, | ||
| 157 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", | ||
| 158 | packet_sequence, data.packet_counter); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | packet_sequence = data.packet_counter; | ||
| 162 | // TODO: Check how the Switch handles motions and how the CemuhookUDP motion | ||
| 163 | // directions correspond to the ones of the Switch | ||
| 164 | Common::Vec3f accel = Common::MakeVec<float>(data.accel.x, data.accel.y, data.accel.z); | ||
| 165 | Common::Vec3f gyro = Common::MakeVec<float>(data.gyro.pitch, data.gyro.yaw, data.gyro.roll); | ||
| 166 | { | ||
| 167 | std::lock_guard guard(status->update_mutex); | ||
| 168 | |||
| 169 | status->motion_status = {accel, gyro}; | ||
| 170 | |||
| 171 | // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates | ||
| 172 | // between a simple "tap" and a hard press that causes the touch screen to click. | ||
| 173 | bool is_active = data.touch_1.is_active != 0; | ||
| 174 | |||
| 175 | float x = 0; | ||
| 176 | float y = 0; | ||
| 177 | |||
| 178 | if (is_active && status->touch_calibration) { | ||
| 179 | u16 min_x = status->touch_calibration->min_x; | ||
| 180 | u16 max_x = status->touch_calibration->max_x; | ||
| 181 | u16 min_y = status->touch_calibration->min_y; | ||
| 182 | u16 max_y = status->touch_calibration->max_y; | ||
| 183 | |||
| 184 | x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / | ||
| 185 | static_cast<float>(max_x - min_x); | ||
| 186 | y = (std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - min_y) / | ||
| 187 | static_cast<float>(max_y - min_y); | ||
| 188 | } | ||
| 189 | |||
| 190 | status->touch_status = {x, y, is_active}; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id) { | ||
| 195 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, | ||
| 196 | [this](Response::PortInfo info) { OnPortInfo(info); }, | ||
| 197 | [this](Response::PadData data) { OnPadData(data); }}; | ||
| 198 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); | ||
| 199 | socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); | ||
| 200 | thread = std::thread{SocketLoop, this->socket.get()}; | ||
| 201 | } | ||
| 202 | |||
| 203 | void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, | ||
| 204 | std::function<void()> success_callback, | ||
| 205 | std::function<void()> failure_callback) { | ||
| 206 | std::thread([=] { | ||
| 207 | Common::Event success_event; | ||
| 208 | SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {}, | ||
| 209 | [&](Response::PadData data) { success_event.Set(); }}; | ||
| 210 | Socket socket{host, port, pad_index, client_id, callback}; | ||
| 211 | std::thread worker_thread{SocketLoop, &socket}; | ||
| 212 | bool result = success_event.WaitFor(std::chrono::seconds(8)); | ||
| 213 | socket.Stop(); | ||
| 214 | worker_thread.join(); | ||
| 215 | if (result) | ||
| 216 | success_callback(); | ||
| 217 | else | ||
| 218 | failure_callback(); | ||
| 219 | }) | ||
| 220 | .detach(); | ||
| 221 | } | ||
| 222 | |||
| 223 | CalibrationConfigurationJob::CalibrationConfigurationJob( | ||
| 224 | const std::string& host, u16 port, u8 pad_index, u32 client_id, | ||
| 225 | std::function<void(Status)> status_callback, | ||
| 226 | std::function<void(u16, u16, u16, u16)> data_callback) { | ||
| 227 | |||
| 228 | std::thread([=] { | ||
| 229 | constexpr u16 CALIBRATION_THRESHOLD = 100; | ||
| 230 | |||
| 231 | u16 min_x{UINT16_MAX}, min_y{UINT16_MAX}; | ||
| 232 | u16 max_x, max_y; | ||
| 233 | |||
| 234 | Status current_status{Status::Initialized}; | ||
| 235 | SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {}, | ||
| 236 | [&](Response::PadData data) { | ||
| 237 | if (current_status == Status::Initialized) { | ||
| 238 | // Receiving data means the communication is ready now | ||
| 239 | current_status = Status::Ready; | ||
| 240 | status_callback(current_status); | ||
| 241 | } | ||
| 242 | if (!data.touch_1.is_active) | ||
| 243 | return; | ||
| 244 | LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, | ||
| 245 | data.touch_1.y); | ||
| 246 | min_x = std::min(min_x, static_cast<u16>(data.touch_1.x)); | ||
| 247 | min_y = std::min(min_y, static_cast<u16>(data.touch_1.y)); | ||
| 248 | if (current_status == Status::Ready) { | ||
| 249 | // First touch - min data (min_x/min_y) | ||
| 250 | current_status = Status::Stage1Completed; | ||
| 251 | status_callback(current_status); | ||
| 252 | } | ||
| 253 | if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && | ||
| 254 | data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { | ||
| 255 | // Set the current position as max value and finishes | ||
| 256 | // configuration | ||
| 257 | max_x = data.touch_1.x; | ||
| 258 | max_y = data.touch_1.y; | ||
| 259 | current_status = Status::Completed; | ||
| 260 | data_callback(min_x, min_y, max_x, max_y); | ||
| 261 | status_callback(current_status); | ||
| 262 | |||
| 263 | complete_event.Set(); | ||
| 264 | } | ||
| 265 | }}; | ||
| 266 | Socket socket{host, port, pad_index, client_id, callback}; | ||
| 267 | std::thread worker_thread{SocketLoop, &socket}; | ||
| 268 | complete_event.Wait(); | ||
| 269 | socket.Stop(); | ||
| 270 | worker_thread.join(); | ||
| 271 | }) | ||
| 272 | .detach(); | ||
| 273 | } | ||
| 274 | |||
| 275 | CalibrationConfigurationJob::~CalibrationConfigurationJob() { | ||
| 276 | Stop(); | ||
| 277 | } | ||
| 278 | |||
| 279 | void CalibrationConfigurationJob::Stop() { | ||
| 280 | complete_event.Set(); | ||
| 281 | } | ||
| 282 | |||
| 283 | } // namespace InputCommon::CemuhookUDP | ||