diff options
| author | 2021-12-18 13:57:14 +0800 | |
|---|---|---|
| committer | 2021-12-18 13:57:14 +0800 | |
| commit | e49184e6069a9d791d2df3c1958f5c4b1187e124 (patch) | |
| tree | b776caf722e0be0e680f67b0ad0842628162ef1c /src/input_common/drivers/udp_client.cpp | |
| parent | Implement convert legacy to generic (diff) | |
| parent | Merge pull request #7570 from ameerj/favorites-expanded (diff) | |
| download | yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.gz yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.xz yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.zip | |
Merge branch 'yuzu-emu:master' into convert_legacy
Diffstat (limited to 'src/input_common/drivers/udp_client.cpp')
| -rw-r--r-- | src/input_common/drivers/udp_client.cpp | 591 |
1 files changed, 591 insertions, 0 deletions
diff --git a/src/input_common/drivers/udp_client.cpp b/src/input_common/drivers/udp_client.cpp new file mode 100644 index 000000000..4ab991a7d --- /dev/null +++ b/src/input_common/drivers/udp_client.cpp | |||
| @@ -0,0 +1,591 @@ | |||
| 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 <random> | ||
| 6 | #include <boost/asio.hpp> | ||
| 7 | #include <fmt/format.h> | ||
| 8 | |||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "common/param_package.h" | ||
| 11 | #include "common/settings.h" | ||
| 12 | #include "input_common/drivers/udp_client.h" | ||
| 13 | #include "input_common/helpers/udp_protocol.h" | ||
| 14 | |||
| 15 | using boost::asio::ip::udp; | ||
| 16 | |||
| 17 | namespace InputCommon::CemuhookUDP { | ||
| 18 | |||
| 19 | struct SocketCallback { | ||
| 20 | std::function<void(Response::Version)> version; | ||
| 21 | std::function<void(Response::PortInfo)> port_info; | ||
| 22 | std::function<void(Response::PadData)> pad_data; | ||
| 23 | }; | ||
| 24 | |||
| 25 | class Socket { | ||
| 26 | public: | ||
| 27 | using clock = std::chrono::system_clock; | ||
| 28 | |||
| 29 | explicit Socket(const std::string& host, u16 port, SocketCallback callback_) | ||
| 30 | : callback(std::move(callback_)), timer(io_service), | ||
| 31 | socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(GenerateRandomClientId()) { | ||
| 32 | boost::system::error_code ec{}; | ||
| 33 | auto ipv4 = boost::asio::ip::make_address_v4(host, ec); | ||
| 34 | if (ec.value() != boost::system::errc::success) { | ||
| 35 | LOG_ERROR(Input, "Invalid IPv4 address \"{}\" provided to socket", host); | ||
| 36 | ipv4 = boost::asio::ip::address_v4{}; | ||
| 37 | } | ||
| 38 | |||
| 39 | send_endpoint = {udp::endpoint(ipv4, port)}; | ||
| 40 | } | ||
| 41 | |||
| 42 | void Stop() { | ||
| 43 | io_service.stop(); | ||
| 44 | } | ||
| 45 | |||
| 46 | void Loop() { | ||
| 47 | io_service.run(); | ||
| 48 | } | ||
| 49 | |||
| 50 | void StartSend(const clock::time_point& from) { | ||
| 51 | timer.expires_at(from + std::chrono::seconds(3)); | ||
| 52 | timer.async_wait([this](const boost::system::error_code& error) { HandleSend(error); }); | ||
| 53 | } | ||
| 54 | |||
| 55 | void StartReceive() { | ||
| 56 | socket.async_receive_from( | ||
| 57 | boost::asio::buffer(receive_buffer), receive_endpoint, | ||
| 58 | [this](const boost::system::error_code& error, std::size_t bytes_transferred) { | ||
| 59 | HandleReceive(error, bytes_transferred); | ||
| 60 | }); | ||
| 61 | } | ||
| 62 | |||
| 63 | private: | ||
| 64 | u32 GenerateRandomClientId() const { | ||
| 65 | std::random_device device; | ||
| 66 | return device(); | ||
| 67 | } | ||
| 68 | |||
| 69 | void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) { | ||
| 70 | if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) { | ||
| 71 | switch (*type) { | ||
| 72 | case Type::Version: { | ||
| 73 | Response::Version version; | ||
| 74 | std::memcpy(&version, &receive_buffer[sizeof(Header)], sizeof(Response::Version)); | ||
| 75 | callback.version(std::move(version)); | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | case Type::PortInfo: { | ||
| 79 | Response::PortInfo port_info; | ||
| 80 | std::memcpy(&port_info, &receive_buffer[sizeof(Header)], | ||
| 81 | sizeof(Response::PortInfo)); | ||
| 82 | callback.port_info(std::move(port_info)); | ||
| 83 | break; | ||
| 84 | } | ||
| 85 | case Type::PadData: { | ||
| 86 | Response::PadData pad_data; | ||
| 87 | std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData)); | ||
| 88 | callback.pad_data(std::move(pad_data)); | ||
| 89 | break; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | StartReceive(); | ||
| 94 | } | ||
| 95 | |||
| 96 | void HandleSend(const boost::system::error_code&) { | ||
| 97 | boost::system::error_code _ignored{}; | ||
| 98 | // Send a request for getting port info for the pad | ||
| 99 | const Request::PortInfo port_info{4, {0, 1, 2, 3}}; | ||
| 100 | const auto port_message = Request::Create(port_info, client_id); | ||
| 101 | std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); | ||
| 102 | socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); | ||
| 103 | |||
| 104 | // Send a request for getting pad data for the pad | ||
| 105 | const Request::PadData pad_data{ | ||
| 106 | Request::RegisterFlags::AllPads, | ||
| 107 | 0, | ||
| 108 | EMPTY_MAC_ADDRESS, | ||
| 109 | }; | ||
| 110 | const auto pad_message = Request::Create(pad_data, client_id); | ||
| 111 | std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); | ||
| 112 | socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); | ||
| 113 | StartSend(timer.expiry()); | ||
| 114 | } | ||
| 115 | |||
| 116 | SocketCallback callback; | ||
| 117 | boost::asio::io_service io_service; | ||
| 118 | boost::asio::basic_waitable_timer<clock> timer; | ||
| 119 | udp::socket socket; | ||
| 120 | |||
| 121 | const u32 client_id; | ||
| 122 | |||
| 123 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); | ||
| 124 | static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); | ||
| 125 | std::array<u8, PORT_INFO_SIZE> send_buffer1; | ||
| 126 | std::array<u8, PAD_DATA_SIZE> send_buffer2; | ||
| 127 | udp::endpoint send_endpoint; | ||
| 128 | |||
| 129 | std::array<u8, MAX_PACKET_SIZE> receive_buffer; | ||
| 130 | udp::endpoint receive_endpoint; | ||
| 131 | }; | ||
| 132 | |||
| 133 | static void SocketLoop(Socket* socket) { | ||
| 134 | socket->StartReceive(); | ||
| 135 | socket->StartSend(Socket::clock::now()); | ||
| 136 | socket->Loop(); | ||
| 137 | } | ||
| 138 | |||
| 139 | UDPClient::UDPClient(std::string input_engine_) : InputEngine(std::move(input_engine_)) { | ||
| 140 | LOG_INFO(Input, "Udp Initialization started"); | ||
| 141 | ReloadSockets(); | ||
| 142 | } | ||
| 143 | |||
| 144 | UDPClient::~UDPClient() { | ||
| 145 | Reset(); | ||
| 146 | } | ||
| 147 | |||
| 148 | UDPClient::ClientConnection::ClientConnection() = default; | ||
| 149 | |||
| 150 | UDPClient::ClientConnection::~ClientConnection() = default; | ||
| 151 | |||
| 152 | void UDPClient::ReloadSockets() { | ||
| 153 | Reset(); | ||
| 154 | |||
| 155 | std::stringstream servers_ss(Settings::values.udp_input_servers.GetValue()); | ||
| 156 | std::string server_token; | ||
| 157 | std::size_t client = 0; | ||
| 158 | while (std::getline(servers_ss, server_token, ',')) { | ||
| 159 | if (client == MAX_UDP_CLIENTS) { | ||
| 160 | break; | ||
| 161 | } | ||
| 162 | std::stringstream server_ss(server_token); | ||
| 163 | std::string token; | ||
| 164 | std::getline(server_ss, token, ':'); | ||
| 165 | std::string udp_input_address = token; | ||
| 166 | std::getline(server_ss, token, ':'); | ||
| 167 | char* temp; | ||
| 168 | const u16 udp_input_port = static_cast<u16>(std::strtol(token.c_str(), &temp, 0)); | ||
| 169 | if (*temp != '\0') { | ||
| 170 | LOG_ERROR(Input, "Port number is not valid {}", token); | ||
| 171 | continue; | ||
| 172 | } | ||
| 173 | |||
| 174 | const std::size_t client_number = GetClientNumber(udp_input_address, udp_input_port); | ||
| 175 | if (client_number != MAX_UDP_CLIENTS) { | ||
| 176 | LOG_ERROR(Input, "Duplicated UDP servers found"); | ||
| 177 | continue; | ||
| 178 | } | ||
| 179 | StartCommunication(client++, udp_input_address, udp_input_port); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | std::size_t UDPClient::GetClientNumber(std::string_view host, u16 port) const { | ||
| 184 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 185 | if (clients[client].active == -1) { | ||
| 186 | continue; | ||
| 187 | } | ||
| 188 | if (clients[client].host == host && clients[client].port == port) { | ||
| 189 | return client; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | return MAX_UDP_CLIENTS; | ||
| 193 | } | ||
| 194 | |||
| 195 | void UDPClient::OnVersion([[maybe_unused]] Response::Version data) { | ||
| 196 | LOG_TRACE(Input, "Version packet received: {}", data.version); | ||
| 197 | } | ||
| 198 | |||
| 199 | void UDPClient::OnPortInfo([[maybe_unused]] Response::PortInfo data) { | ||
| 200 | LOG_TRACE(Input, "PortInfo packet received: {}", data.model); | ||
| 201 | } | ||
| 202 | |||
| 203 | void UDPClient::OnPadData(Response::PadData data, std::size_t client) { | ||
| 204 | const std::size_t pad_index = (client * PADS_PER_CLIENT) + data.info.id; | ||
| 205 | |||
| 206 | if (pad_index >= pads.size()) { | ||
| 207 | LOG_ERROR(Input, "Invalid pad id {}", data.info.id); | ||
| 208 | return; | ||
| 209 | } | ||
| 210 | |||
| 211 | LOG_TRACE(Input, "PadData packet received"); | ||
| 212 | if (data.packet_counter == pads[pad_index].packet_sequence) { | ||
| 213 | LOG_WARNING( | ||
| 214 | Input, | ||
| 215 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", | ||
| 216 | pads[pad_index].packet_sequence, data.packet_counter); | ||
| 217 | pads[pad_index].connected = false; | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | |||
| 221 | clients[client].active = 1; | ||
| 222 | pads[pad_index].connected = true; | ||
| 223 | pads[pad_index].packet_sequence = data.packet_counter; | ||
| 224 | |||
| 225 | const auto now = std::chrono::steady_clock::now(); | ||
| 226 | const auto time_difference = static_cast<u64>( | ||
| 227 | std::chrono::duration_cast<std::chrono::microseconds>(now - pads[pad_index].last_update) | ||
| 228 | .count()); | ||
| 229 | pads[pad_index].last_update = now; | ||
| 230 | |||
| 231 | // Gyroscope values are not it the correct scale from better joy. | ||
| 232 | // Dividing by 312 allows us to make one full turn = 1 turn | ||
| 233 | // This must be a configurable valued called sensitivity | ||
| 234 | const float gyro_scale = 1.0f / 312.0f; | ||
| 235 | |||
| 236 | const BasicMotion motion{ | ||
| 237 | .gyro_x = data.gyro.pitch * gyro_scale, | ||
| 238 | .gyro_y = data.gyro.roll * gyro_scale, | ||
| 239 | .gyro_z = -data.gyro.yaw * gyro_scale, | ||
| 240 | .accel_x = data.accel.x, | ||
| 241 | .accel_y = -data.accel.z, | ||
| 242 | .accel_z = data.accel.y, | ||
| 243 | .delta_timestamp = time_difference, | ||
| 244 | }; | ||
| 245 | const PadIdentifier identifier = GetPadIdentifier(pad_index); | ||
| 246 | SetMotion(identifier, 0, motion); | ||
| 247 | |||
| 248 | for (std::size_t id = 0; id < data.touch.size(); ++id) { | ||
| 249 | const auto touch_pad = data.touch[id]; | ||
| 250 | const auto touch_axis_x_id = | ||
| 251 | static_cast<int>(id == 0 ? PadAxes::Touch1X : PadAxes::Touch2X); | ||
| 252 | const auto touch_axis_y_id = | ||
| 253 | static_cast<int>(id == 0 ? PadAxes::Touch1Y : PadAxes::Touch2Y); | ||
| 254 | const auto touch_button_id = | ||
| 255 | static_cast<int>(id == 0 ? PadButton::Touch1 : PadButton::touch2); | ||
| 256 | |||
| 257 | // TODO: Use custom calibration per device | ||
| 258 | const Common::ParamPackage touch_param(Settings::values.touch_device.GetValue()); | ||
| 259 | const u16 min_x = static_cast<u16>(touch_param.Get("min_x", 100)); | ||
| 260 | const u16 min_y = static_cast<u16>(touch_param.Get("min_y", 50)); | ||
| 261 | const u16 max_x = static_cast<u16>(touch_param.Get("max_x", 1800)); | ||
| 262 | const u16 max_y = static_cast<u16>(touch_param.Get("max_y", 850)); | ||
| 263 | |||
| 264 | const f32 x = | ||
| 265 | static_cast<f32>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) / | ||
| 266 | static_cast<f32>(max_x - min_x); | ||
| 267 | const f32 y = | ||
| 268 | static_cast<f32>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) / | ||
| 269 | static_cast<f32>(max_y - min_y); | ||
| 270 | |||
| 271 | if (touch_pad.is_active) { | ||
| 272 | SetAxis(identifier, touch_axis_x_id, x); | ||
| 273 | SetAxis(identifier, touch_axis_y_id, y); | ||
| 274 | SetButton(identifier, touch_button_id, true); | ||
| 275 | continue; | ||
| 276 | } | ||
| 277 | SetAxis(identifier, touch_axis_x_id, 0); | ||
| 278 | SetAxis(identifier, touch_axis_y_id, 0); | ||
| 279 | SetButton(identifier, touch_button_id, false); | ||
| 280 | } | ||
| 281 | |||
| 282 | SetAxis(identifier, static_cast<int>(PadAxes::LeftStickX), | ||
| 283 | (data.left_stick_x - 127.0f) / 127.0f); | ||
| 284 | SetAxis(identifier, static_cast<int>(PadAxes::LeftStickY), | ||
| 285 | (data.left_stick_y - 127.0f) / 127.0f); | ||
| 286 | SetAxis(identifier, static_cast<int>(PadAxes::RightStickX), | ||
| 287 | (data.right_stick_x - 127.0f) / 127.0f); | ||
| 288 | SetAxis(identifier, static_cast<int>(PadAxes::RightStickY), | ||
| 289 | (data.right_stick_y - 127.0f) / 127.0f); | ||
| 290 | |||
| 291 | static constexpr std::array<PadButton, 16> buttons{ | ||
| 292 | PadButton::Share, PadButton::L3, PadButton::R3, PadButton::Options, | ||
| 293 | PadButton::Up, PadButton::Right, PadButton::Down, PadButton::Left, | ||
| 294 | PadButton::L2, PadButton::R2, PadButton::L1, PadButton::R1, | ||
| 295 | PadButton::Triangle, PadButton::Circle, PadButton::Cross, PadButton::Square}; | ||
| 296 | |||
| 297 | for (std::size_t i = 0; i < buttons.size(); ++i) { | ||
| 298 | const bool button_status = (data.digital_button & (1U << i)) != 0; | ||
| 299 | const int button = static_cast<int>(buttons[i]); | ||
| 300 | SetButton(identifier, button, button_status); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | void UDPClient::StartCommunication(std::size_t client, const std::string& host, u16 port) { | ||
| 305 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, | ||
| 306 | [this](Response::PortInfo info) { OnPortInfo(info); }, | ||
| 307 | [this, client](Response::PadData data) { OnPadData(data, client); }}; | ||
| 308 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); | ||
| 309 | clients[client].uuid = GetHostUUID(host); | ||
| 310 | clients[client].host = host; | ||
| 311 | clients[client].port = port; | ||
| 312 | clients[client].active = 0; | ||
| 313 | clients[client].socket = std::make_unique<Socket>(host, port, callback); | ||
| 314 | clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; | ||
| 315 | for (std::size_t index = 0; index < PADS_PER_CLIENT; ++index) { | ||
| 316 | const PadIdentifier identifier = GetPadIdentifier(client * PADS_PER_CLIENT + index); | ||
| 317 | PreSetController(identifier); | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | const PadIdentifier UDPClient::GetPadIdentifier(std::size_t pad_index) const { | ||
| 322 | const std::size_t client = pad_index / PADS_PER_CLIENT; | ||
| 323 | return { | ||
| 324 | .guid = clients[client].uuid, | ||
| 325 | .port = static_cast<std::size_t>(clients[client].port), | ||
| 326 | .pad = pad_index, | ||
| 327 | }; | ||
| 328 | } | ||
| 329 | |||
| 330 | const Common::UUID UDPClient::GetHostUUID(const std::string host) const { | ||
| 331 | const auto ip = boost::asio::ip::address_v4::from_string(host); | ||
| 332 | const auto hex_host = fmt::format("{:06x}", ip.to_ulong()); | ||
| 333 | return Common::UUID{hex_host}; | ||
| 334 | } | ||
| 335 | |||
| 336 | void UDPClient::Reset() { | ||
| 337 | for (auto& client : clients) { | ||
| 338 | if (client.thread.joinable()) { | ||
| 339 | client.active = -1; | ||
| 340 | client.socket->Stop(); | ||
| 341 | client.thread.join(); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | std::vector<Common::ParamPackage> UDPClient::GetInputDevices() const { | ||
| 347 | std::vector<Common::ParamPackage> devices; | ||
| 348 | if (!Settings::values.enable_udp_controller) { | ||
| 349 | return devices; | ||
| 350 | } | ||
| 351 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 352 | if (clients[client].active != 1) { | ||
| 353 | continue; | ||
| 354 | } | ||
| 355 | for (std::size_t index = 0; index < PADS_PER_CLIENT; ++index) { | ||
| 356 | const std::size_t pad_index = client * PADS_PER_CLIENT + index; | ||
| 357 | if (!pads[pad_index].connected) { | ||
| 358 | continue; | ||
| 359 | } | ||
| 360 | const auto pad_identifier = GetPadIdentifier(pad_index); | ||
| 361 | Common::ParamPackage identifier{}; | ||
| 362 | identifier.Set("engine", GetEngineName()); | ||
| 363 | identifier.Set("display", fmt::format("UDP Controller {}", pad_identifier.pad)); | ||
| 364 | identifier.Set("guid", pad_identifier.guid.Format()); | ||
| 365 | identifier.Set("port", static_cast<int>(pad_identifier.port)); | ||
| 366 | identifier.Set("pad", static_cast<int>(pad_identifier.pad)); | ||
| 367 | devices.emplace_back(identifier); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | return devices; | ||
| 371 | } | ||
| 372 | |||
| 373 | ButtonMapping UDPClient::GetButtonMappingForDevice(const Common::ParamPackage& params) { | ||
| 374 | // This list excludes any button that can't be really mapped | ||
| 375 | static constexpr std::array<std::pair<Settings::NativeButton::Values, PadButton>, 18> | ||
| 376 | switch_to_dsu_button = { | ||
| 377 | std::pair{Settings::NativeButton::A, PadButton::Circle}, | ||
| 378 | {Settings::NativeButton::B, PadButton::Cross}, | ||
| 379 | {Settings::NativeButton::X, PadButton::Triangle}, | ||
| 380 | {Settings::NativeButton::Y, PadButton::Square}, | ||
| 381 | {Settings::NativeButton::Plus, PadButton::Options}, | ||
| 382 | {Settings::NativeButton::Minus, PadButton::Share}, | ||
| 383 | {Settings::NativeButton::DLeft, PadButton::Left}, | ||
| 384 | {Settings::NativeButton::DUp, PadButton::Up}, | ||
| 385 | {Settings::NativeButton::DRight, PadButton::Right}, | ||
| 386 | {Settings::NativeButton::DDown, PadButton::Down}, | ||
| 387 | {Settings::NativeButton::L, PadButton::L1}, | ||
| 388 | {Settings::NativeButton::R, PadButton::R1}, | ||
| 389 | {Settings::NativeButton::ZL, PadButton::L2}, | ||
| 390 | {Settings::NativeButton::ZR, PadButton::R2}, | ||
| 391 | {Settings::NativeButton::SL, PadButton::L2}, | ||
| 392 | {Settings::NativeButton::SR, PadButton::R2}, | ||
| 393 | {Settings::NativeButton::LStick, PadButton::L3}, | ||
| 394 | {Settings::NativeButton::RStick, PadButton::R3}, | ||
| 395 | }; | ||
| 396 | if (!params.Has("guid") || !params.Has("port") || !params.Has("pad")) { | ||
| 397 | return {}; | ||
| 398 | } | ||
| 399 | |||
| 400 | ButtonMapping mapping{}; | ||
| 401 | for (const auto& [switch_button, dsu_button] : switch_to_dsu_button) { | ||
| 402 | Common::ParamPackage button_params{}; | ||
| 403 | button_params.Set("engine", GetEngineName()); | ||
| 404 | button_params.Set("guid", params.Get("guid", "")); | ||
| 405 | button_params.Set("port", params.Get("port", 0)); | ||
| 406 | button_params.Set("pad", params.Get("pad", 0)); | ||
| 407 | button_params.Set("button", static_cast<int>(dsu_button)); | ||
| 408 | mapping.insert_or_assign(switch_button, std::move(button_params)); | ||
| 409 | } | ||
| 410 | |||
| 411 | return mapping; | ||
| 412 | } | ||
| 413 | |||
| 414 | AnalogMapping UDPClient::GetAnalogMappingForDevice(const Common::ParamPackage& params) { | ||
| 415 | if (!params.Has("guid") || !params.Has("port") || !params.Has("pad")) { | ||
| 416 | return {}; | ||
| 417 | } | ||
| 418 | |||
| 419 | AnalogMapping mapping = {}; | ||
| 420 | Common::ParamPackage left_analog_params; | ||
| 421 | left_analog_params.Set("engine", GetEngineName()); | ||
| 422 | left_analog_params.Set("guid", params.Get("guid", "")); | ||
| 423 | left_analog_params.Set("port", params.Get("port", 0)); | ||
| 424 | left_analog_params.Set("pad", params.Get("pad", 0)); | ||
| 425 | left_analog_params.Set("axis_x", static_cast<int>(PadAxes::LeftStickX)); | ||
| 426 | left_analog_params.Set("axis_y", static_cast<int>(PadAxes::LeftStickY)); | ||
| 427 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params)); | ||
| 428 | Common::ParamPackage right_analog_params; | ||
| 429 | right_analog_params.Set("engine", GetEngineName()); | ||
| 430 | right_analog_params.Set("guid", params.Get("guid", "")); | ||
| 431 | right_analog_params.Set("port", params.Get("port", 0)); | ||
| 432 | right_analog_params.Set("pad", params.Get("pad", 0)); | ||
| 433 | right_analog_params.Set("axis_x", static_cast<int>(PadAxes::RightStickX)); | ||
| 434 | right_analog_params.Set("axis_y", static_cast<int>(PadAxes::RightStickY)); | ||
| 435 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params)); | ||
| 436 | return mapping; | ||
| 437 | } | ||
| 438 | |||
| 439 | MotionMapping UDPClient::GetMotionMappingForDevice(const Common::ParamPackage& params) { | ||
| 440 | if (!params.Has("guid") || !params.Has("port") || !params.Has("pad")) { | ||
| 441 | return {}; | ||
| 442 | } | ||
| 443 | |||
| 444 | MotionMapping mapping = {}; | ||
| 445 | Common::ParamPackage motion_params; | ||
| 446 | motion_params.Set("engine", GetEngineName()); | ||
| 447 | motion_params.Set("guid", params.Get("guid", "")); | ||
| 448 | motion_params.Set("port", params.Get("port", 0)); | ||
| 449 | motion_params.Set("pad", params.Get("pad", 0)); | ||
| 450 | motion_params.Set("motion", 0); | ||
| 451 | mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, std::move(motion_params)); | ||
| 452 | mapping.insert_or_assign(Settings::NativeMotion::MotionRight, std::move(motion_params)); | ||
| 453 | return mapping; | ||
| 454 | } | ||
| 455 | |||
| 456 | Common::Input::ButtonNames UDPClient::GetUIButtonName(const Common::ParamPackage& params) const { | ||
| 457 | PadButton button = static_cast<PadButton>(params.Get("button", 0)); | ||
| 458 | switch (button) { | ||
| 459 | case PadButton::Left: | ||
| 460 | return Common::Input::ButtonNames::ButtonLeft; | ||
| 461 | case PadButton::Right: | ||
| 462 | return Common::Input::ButtonNames::ButtonRight; | ||
| 463 | case PadButton::Down: | ||
| 464 | return Common::Input::ButtonNames::ButtonDown; | ||
| 465 | case PadButton::Up: | ||
| 466 | return Common::Input::ButtonNames::ButtonUp; | ||
| 467 | case PadButton::L1: | ||
| 468 | return Common::Input::ButtonNames::L1; | ||
| 469 | case PadButton::L2: | ||
| 470 | return Common::Input::ButtonNames::L2; | ||
| 471 | case PadButton::L3: | ||
| 472 | return Common::Input::ButtonNames::L3; | ||
| 473 | case PadButton::R1: | ||
| 474 | return Common::Input::ButtonNames::R1; | ||
| 475 | case PadButton::R2: | ||
| 476 | return Common::Input::ButtonNames::R2; | ||
| 477 | case PadButton::R3: | ||
| 478 | return Common::Input::ButtonNames::R3; | ||
| 479 | case PadButton::Circle: | ||
| 480 | return Common::Input::ButtonNames::Circle; | ||
| 481 | case PadButton::Cross: | ||
| 482 | return Common::Input::ButtonNames::Cross; | ||
| 483 | case PadButton::Square: | ||
| 484 | return Common::Input::ButtonNames::Square; | ||
| 485 | case PadButton::Triangle: | ||
| 486 | return Common::Input::ButtonNames::Triangle; | ||
| 487 | case PadButton::Share: | ||
| 488 | return Common::Input::ButtonNames::Share; | ||
| 489 | case PadButton::Options: | ||
| 490 | return Common::Input::ButtonNames::Options; | ||
| 491 | default: | ||
| 492 | return Common::Input::ButtonNames::Undefined; | ||
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | Common::Input::ButtonNames UDPClient::GetUIName(const Common::ParamPackage& params) const { | ||
| 497 | if (params.Has("button")) { | ||
| 498 | return GetUIButtonName(params); | ||
| 499 | } | ||
| 500 | if (params.Has("axis")) { | ||
| 501 | return Common::Input::ButtonNames::Value; | ||
| 502 | } | ||
| 503 | if (params.Has("motion")) { | ||
| 504 | return Common::Input::ButtonNames::Engine; | ||
| 505 | } | ||
| 506 | |||
| 507 | return Common::Input::ButtonNames::Invalid; | ||
| 508 | } | ||
| 509 | |||
| 510 | void TestCommunication(const std::string& host, u16 port, | ||
| 511 | const std::function<void()>& success_callback, | ||
| 512 | const std::function<void()>& failure_callback) { | ||
| 513 | std::thread([=] { | ||
| 514 | Common::Event success_event; | ||
| 515 | SocketCallback callback{ | ||
| 516 | .version = [](Response::Version) {}, | ||
| 517 | .port_info = [](Response::PortInfo) {}, | ||
| 518 | .pad_data = [&](Response::PadData) { success_event.Set(); }, | ||
| 519 | }; | ||
| 520 | Socket socket{host, port, std::move(callback)}; | ||
| 521 | std::thread worker_thread{SocketLoop, &socket}; | ||
| 522 | const bool result = | ||
| 523 | success_event.WaitUntil(std::chrono::steady_clock::now() + std::chrono::seconds(10)); | ||
| 524 | socket.Stop(); | ||
| 525 | worker_thread.join(); | ||
| 526 | if (result) { | ||
| 527 | success_callback(); | ||
| 528 | } else { | ||
| 529 | failure_callback(); | ||
| 530 | } | ||
| 531 | }).detach(); | ||
| 532 | } | ||
| 533 | |||
| 534 | CalibrationConfigurationJob::CalibrationConfigurationJob( | ||
| 535 | const std::string& host, u16 port, std::function<void(Status)> status_callback, | ||
| 536 | std::function<void(u16, u16, u16, u16)> data_callback) { | ||
| 537 | |||
| 538 | std::thread([=, this] { | ||
| 539 | Status current_status{Status::Initialized}; | ||
| 540 | SocketCallback callback{ | ||
| 541 | [](Response::Version) {}, [](Response::PortInfo) {}, | ||
| 542 | [&](Response::PadData data) { | ||
| 543 | static constexpr u16 CALIBRATION_THRESHOLD = 100; | ||
| 544 | static constexpr u16 MAX_VALUE = UINT16_MAX; | ||
| 545 | |||
| 546 | if (current_status == Status::Initialized) { | ||
| 547 | // Receiving data means the communication is ready now | ||
| 548 | current_status = Status::Ready; | ||
| 549 | status_callback(current_status); | ||
| 550 | } | ||
| 551 | const auto& touchpad_0 = data.touch[0]; | ||
| 552 | if (touchpad_0.is_active == 0) { | ||
| 553 | return; | ||
| 554 | } | ||
| 555 | LOG_DEBUG(Input, "Current touch: {} {}", touchpad_0.x, touchpad_0.y); | ||
| 556 | const u16 min_x = std::min(MAX_VALUE, static_cast<u16>(touchpad_0.x)); | ||
| 557 | const u16 min_y = std::min(MAX_VALUE, static_cast<u16>(touchpad_0.y)); | ||
| 558 | if (current_status == Status::Ready) { | ||
| 559 | // First touch - min data (min_x/min_y) | ||
| 560 | current_status = Status::Stage1Completed; | ||
| 561 | status_callback(current_status); | ||
| 562 | } | ||
| 563 | if (touchpad_0.x - min_x > CALIBRATION_THRESHOLD && | ||
| 564 | touchpad_0.y - min_y > CALIBRATION_THRESHOLD) { | ||
| 565 | // Set the current position as max value and finishes configuration | ||
| 566 | const u16 max_x = touchpad_0.x; | ||
| 567 | const u16 max_y = touchpad_0.y; | ||
| 568 | current_status = Status::Completed; | ||
| 569 | data_callback(min_x, min_y, max_x, max_y); | ||
| 570 | status_callback(current_status); | ||
| 571 | |||
| 572 | complete_event.Set(); | ||
| 573 | } | ||
| 574 | }}; | ||
| 575 | Socket socket{host, port, std::move(callback)}; | ||
| 576 | std::thread worker_thread{SocketLoop, &socket}; | ||
| 577 | complete_event.Wait(); | ||
| 578 | socket.Stop(); | ||
| 579 | worker_thread.join(); | ||
| 580 | }).detach(); | ||
| 581 | } | ||
| 582 | |||
| 583 | CalibrationConfigurationJob::~CalibrationConfigurationJob() { | ||
| 584 | Stop(); | ||
| 585 | } | ||
| 586 | |||
| 587 | void CalibrationConfigurationJob::Stop() { | ||
| 588 | complete_event.Set(); | ||
| 589 | } | ||
| 590 | |||
| 591 | } // namespace InputCommon::CemuhookUDP | ||