| 1 |
// Copyright 2019 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "InputCommon/ControllerInterface/DualShockUDPClient/DualShockUDPClient.h" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <array> |
| 8 |
#include <chrono> |
| 9 |
#include <tuple> |
| 10 |
|
| 11 |
#include <SFML/Network/SocketSelector.hpp> |
| 12 |
#include <SFML/Network/UdpSocket.hpp> |
| 13 |
#include <fmt/format.h> |
| 14 |
|
| 15 |
#include "Common/Config/Config.h" |
| 16 |
#include "Common/Flag.h" |
| 17 |
#include "Common/Logging/Log.h" |
| 18 |
#include "Common/MathUtil.h" |
| 19 |
#include "Common/Random.h" |
| 20 |
#include "Common/ScopeGuard.h" |
| 21 |
#include "Common/StringUtil.h" |
| 22 |
#include "Common/Thread.h" |
| 23 |
#include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 24 |
#include "InputCommon/ControllerInterface/DualShockUDPClient/DualShockUDPProto.h" |
| 25 |
#include "SFML/Network/IpAddress.hpp" |
| 26 |
|
| 27 |
namespace ciface::DualShockUDPClient |
| 28 |
{ |
| 29 |
constexpr std::string_view DUALSHOCKUDP_SOURCE_NAME = "DSUClient"; |
| 30 |
|
| 31 |
namespace Settings |
| 32 |
{ |
| 33 |
const Config::Info<std::string> SERVER_ADDRESS{ |
| 34 |
{Config::System::DualShockUDPClient, "Server", "IPAddress"}, ""}; |
| 35 |
const Config::Info<int> SERVER_PORT{{Config::System::DualShockUDPClient, "Server", "Port"}, 0}; |
| 36 |
const Config::Info<std::string> SERVERS{{Config::System::DualShockUDPClient, "Server", "Entries"}, |
| 37 |
""}; |
| 38 |
const Config::Info<bool> SERVERS_ENABLED{{Config::System::DualShockUDPClient, "Server", "Enabled"}, |
| 39 |
false}; |
| 40 |
} // namespace Settings |
| 41 |
|
| 42 |
// Clock type used for querying timeframes |
| 43 |
using SteadyClock = std::chrono::steady_clock; |
| 44 |
|
| 45 |
class Device final : public Core::Device |
| 46 |
{ |
| 47 |
private: |
| 48 |
template <class T> |
| 49 |
class Button final : public Input |
| 50 |
{ |
| 51 |
public: |
| 52 |
Button(const char* name, const T& buttons, T mask) |
| 53 |
: m_name(name), m_buttons(buttons), m_mask(mask) |
| 54 |
{ |
| 55 |
} |
| 56 |
std::string GetName() const override { return m_name; } |
| 57 |
ControlState GetState() const override { return (m_buttons & m_mask) != 0; } |
| 58 |
|
| 59 |
private: |
| 60 |
const char* const m_name; |
| 61 |
const T& m_buttons; |
| 62 |
const T m_mask; |
| 63 |
}; |
| 64 |
|
| 65 |
template <class T> |
| 66 |
class AnalogInput : public Input |
| 67 |
{ |
| 68 |
public: |
| 69 |
AnalogInput(const char* name, const T& input, ControlState range, ControlState offset = 0) |
| 70 |
: m_name(name), m_input(input), m_range(range), m_offset(offset) |
| 71 |
{ |
| 72 |
} |
| 73 |
std::string GetName() const final { return m_name; } |
| 74 |
ControlState GetState() const final { return (ControlState(m_input) + m_offset) / m_range; } |
| 75 |
|
| 76 |
private: |
| 77 |
const char* m_name; |
| 78 |
const T& m_input; |
| 79 |
const ControlState m_range; |
| 80 |
const ControlState m_offset; |
| 81 |
}; |
| 82 |
|
| 83 |
class TouchInput final : public AnalogInput<int> |
| 84 |
{ |
| 85 |
public: |
| 86 |
using AnalogInput::AnalogInput; |
| 87 |
bool IsDetectable() const override { return false; } |
| 88 |
}; |
| 89 |
|
| 90 |
class MotionInput final : public AnalogInput<float> |
| 91 |
{ |
| 92 |
public: |
| 93 |
using AnalogInput::AnalogInput; |
| 94 |
bool IsDetectable() const override { return false; } |
| 95 |
}; |
| 96 |
|
| 97 |
using AccelerometerInput = MotionInput; |
| 98 |
using GyroInput = MotionInput; |
| 99 |
|
| 100 |
class BatteryInput final : public Input |
| 101 |
{ |
| 102 |
public: |
| 103 |
using BatteryState = Proto::DsBattery; |
| 104 |
|
| 105 |
BatteryInput(const BatteryState& battery) : m_battery(battery) {} |
| 106 |
|
| 107 |
std::string GetName() const override { return "Battery"; } |
| 108 |
|
| 109 |
ControlState GetState() const override |
| 110 |
{ |
| 111 |
switch (m_battery) |
| 112 |
{ |
| 113 |
case BatteryState::Charging: // We don't actually know the battery level in this case |
| 114 |
case BatteryState::Charged: |
| 115 |
return BATTERY_INPUT_MAX_VALUE; |
| 116 |
default: |
| 117 |
return ControlState(m_battery) / ControlState(BatteryState::Full) * BATTERY_INPUT_MAX_VALUE; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
bool IsDetectable() const override { return false; } |
| 122 |
|
| 123 |
private: |
| 124 |
const BatteryState& m_battery; |
| 125 |
}; |
| 126 |
|
| 127 |
public: |
| 128 |
Core::DeviceRemoval UpdateInput() override; |
| 129 |
|
| 130 |
Device(std::string name, int index, std::string server_address, u16 server_port, u32 client_uid); |
| 131 |
|
| 132 |
std::string GetName() const final override; |
| 133 |
std::string GetSource() const final override; |
| 134 |
std::optional<int> GetPreferredId() const final override; |
| 135 |
// Always add these at the end, given their hotplug nature |
| 136 |
int GetSortPriority() const override { return -5; } |
| 137 |
|
| 138 |
private: |
| 139 |
void ResetPadData(); |
| 140 |
|
| 141 |
const std::string m_name; |
| 142 |
const int m_index; |
| 143 |
sf::UdpSocket m_socket; |
| 144 |
SteadyClock::time_point m_next_reregister = SteadyClock::time_point::min(); |
| 145 |
Proto::MessageType::PadDataResponse m_pad_data{}; |
| 146 |
Proto::Touch m_prev_touch{}; |
| 147 |
bool m_prev_touch_valid = false; |
| 148 |
int m_touch_x = 0; |
| 149 |
int m_touch_y = 0; |
| 150 |
const std::string m_server_address; |
| 151 |
const u16 m_server_port; |
| 152 |
|
| 153 |
s16 m_touch_x_min = 0; |
| 154 |
s16 m_touch_y_min = 0; |
| 155 |
s16 m_touch_x_max = 0; |
| 156 |
s16 m_touch_y_max = 0; |
| 157 |
|
| 158 |
const u32 m_client_uid; |
| 159 |
}; |
| 160 |
|
| 161 |
using MathUtil::GRAVITY_ACCELERATION; |
| 162 |
constexpr auto SERVER_REREGISTER_INTERVAL = std::chrono::seconds{1}; |
| 163 |
constexpr auto SERVER_LISTPORTS_INTERVAL = std::chrono::seconds{1}; |
| 164 |
constexpr int TOUCH_X_AXIS_MAX = 1000; |
| 165 |
constexpr int TOUCH_Y_AXIS_MAX = 500; |
| 166 |
constexpr auto THREAD_MAX_WAIT_INTERVAL = std::chrono::milliseconds{250}; |
| 167 |
constexpr auto SERVER_UNRESPONSIVE_INTERVAL = std::chrono::seconds{1}; // Can be 0 |
| 168 |
constexpr u32 SERVER_ASKED_PADS = 4; |
| 169 |
|
| 170 |
struct Server |
| 171 |
{ |
| 172 |
Server(std::string description, std::string address, u16 port) |
| 173 |
: m_description{std::move(description)}, m_address{std::move(address)}, m_port{port} |
| 174 |
{ |
| 175 |
} |
| 176 |
Server(const Server&) = delete; |
| 177 |
Server(Server&& other) noexcept |
| 178 |
{ |
| 179 |
m_description = std::move(other.m_description); |
| 180 |
m_address = std::move(other.m_address); |
| 181 |
m_port = other.m_port; |
| 182 |
m_port_info = other.m_port_info; |
| 183 |
} |
| 184 |
|
| 185 |
Server& operator=(const Server&) = delete; |
| 186 |
Server& operator=(Server&&) = delete; |
| 187 |
|
| 188 |
~Server() = default; |
| 189 |
|
| 190 |
std::string m_description; |
| 191 |
std::string m_address; |
| 192 |
u16 m_port = 0; |
| 193 |
std::array<Proto::MessageType::PortInfo, Proto::PORT_COUNT> m_port_info{}; |
| 194 |
sf::UdpSocket m_socket; |
| 195 |
SteadyClock::time_point m_disconnect_time = SteadyClock::now(); |
| 196 |
}; |
| 197 |
|
| 198 |
class InputBackend final : public ciface::InputBackend |
| 199 |
{ |
| 200 |
public: |
| 201 |
InputBackend(ControllerInterface* controller_interface); |
| 202 |
~InputBackend() override; |
| 203 |
void PopulateDevices() override; |
| 204 |
|
| 205 |
private: |
| 206 |
void ConfigChanged(); |
| 207 |
void Restart(); |
| 208 |
|
| 209 |
void HotplugThreadFunc(); |
| 210 |
void StartHotplugThread(); |
| 211 |
void StopHotplugThread(); |
| 212 |
|
| 213 |
bool m_servers_enabled = false; |
| 214 |
std::vector<Server> m_servers; |
| 215 |
u32 m_client_uid = 0; |
| 216 |
SteadyClock::time_point m_next_listports_time; |
| 217 |
std::thread m_hotplug_thread; |
| 218 |
Common::Flag m_hotplug_thread_running; |
| 219 |
Config::ConfigChangedCallbackID m_config_change_callback_id; |
| 220 |
}; |
| 221 |
|
| 222 |
std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface) |
| 223 |
{ |
| 224 |
return std::make_unique<InputBackend>(controller_interface); |
| 225 |
} |
| 226 |
|
| 227 |
static bool IsSameController(const Proto::MessageType::PortInfo& a, |
| 228 |
const Proto::MessageType::PortInfo& b) |
| 229 |
{ |
| 230 |
// compare everything but battery_status |
| 231 |
return std::tie(a.pad_id, a.pad_state, a.model, a.connection_type, a.pad_mac_address) == |
| 232 |
std::tie(b.pad_id, b.pad_state, b.model, b.connection_type, b.pad_mac_address); |
| 233 |
} |
| 234 |
|
| 235 |
void InputBackend::HotplugThreadFunc() |
| 236 |
{ |
| 237 |
Common::SetCurrentThreadName("DualShockUDPClient Hotplug Thread"); |
| 238 |
INFO_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient hotplug thread started"); |
| 239 |
Common::ScopeGuard thread_stop_guard{ |
| 240 |
[] { INFO_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient hotplug thread stopped"); }}; |
| 241 |
|
| 242 |
std::vector<bool> timed_out_servers(m_servers.size(), false); |
| 243 |
|
| 244 |
while (m_hotplug_thread_running.IsSet()) |
| 245 |
{ |
| 246 |
using namespace std::chrono; |
| 247 |
using namespace std::chrono_literals; |
| 248 |
|
| 249 |
const auto now = SteadyClock::now(); |
| 250 |
if (now >= m_next_listports_time) |
| 251 |
{ |
| 252 |
m_next_listports_time = now + SERVER_LISTPORTS_INTERVAL; |
| 253 |
|
| 254 |
for (size_t i = 0; i < m_servers.size(); ++i) |
| 255 |
{ |
| 256 |
auto& server = m_servers[i]; |
| 257 |
Proto::Message<Proto::MessageType::ListPorts> msg(m_client_uid); |
| 258 |
auto& list_ports = msg.m_message; |
| 259 |
// We ask for x possible devices. We will receive a message for every connected device. |
| 260 |
list_ports.pad_request_count = SERVER_ASKED_PADS; |
| 261 |
list_ports.pad_ids = {0, 1, 2, 3}; |
| 262 |
msg.Finish(); |
| 263 |
if (std::optional<sf::IpAddress> server_ip = sf::IpAddress::resolve(server.m_address)) |
| 264 |
{ |
| 265 |
if (server.m_socket.send(&list_ports, sizeof list_ports, *server_ip, server.m_port) != |
| 266 |
sf::Socket::Status::Done) |
| 267 |
{ |
| 268 |
ERROR_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient HotplugThreadFunc send failed"); |
| 269 |
} |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
ERROR_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient failed to resolve {}", |
| 274 |
server.m_address); |
| 275 |
} |
| 276 |
timed_out_servers[i] = true; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
sf::SocketSelector selector; |
| 281 |
for (auto& server : m_servers) |
| 282 |
{ |
| 283 |
selector.add(server.m_socket); |
| 284 |
} |
| 285 |
|
| 286 |
auto timeout = duration_cast<milliseconds>(m_next_listports_time - SteadyClock::now()); |
| 287 |
|
| 288 |
// Receive controller port info within a time from our request. |
| 289 |
// Run this even if we sent no new requests, to disconnect devices, |
| 290 |
// sleep (wait) the thread and catch old responses. |
| 291 |
do |
| 292 |
{ |
| 293 |
// Selector's wait treats a timeout of zero as infinite timeout, which we don't want, |
| 294 |
// but we also don't want risk waiting for the whole SERVER_LISTPORTS_INTERVAL and hang |
| 295 |
// the thead trying to close this one in case we received no answers. |
| 296 |
const auto current_timeout = std::max(std::min(timeout, THREAD_MAX_WAIT_INTERVAL), 1ms); |
| 297 |
timeout -= current_timeout; |
| 298 |
// This will return at the first answer |
| 299 |
if (selector.wait(sf::milliseconds(current_timeout.count()))) |
| 300 |
{ |
| 301 |
// Now check all the servers because we don't know which one(s) sent a reply |
| 302 |
for (size_t i = 0; i < m_servers.size(); ++i) |
| 303 |
{ |
| 304 |
auto& server = m_servers[i]; |
| 305 |
if (!selector.isReady(server.m_socket)) |
| 306 |
{ |
| 307 |
continue; |
| 308 |
} |
| 309 |
|
| 310 |
Proto::Message<Proto::MessageType::FromServer> msg; |
| 311 |
std::size_t received_bytes; |
| 312 |
std::optional<sf::IpAddress> sender; |
| 313 |
u16 port; |
| 314 |
if (server.m_socket.receive(&msg, sizeof(msg), received_bytes, sender, port) != |
| 315 |
sf::Socket::Status::Done) |
| 316 |
{ |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
if (auto port_info = msg.CheckAndCastTo<Proto::MessageType::PortInfo>()) |
| 321 |
{ |
| 322 |
server.m_disconnect_time = SteadyClock::now() + SERVER_UNRESPONSIVE_INTERVAL; |
| 323 |
// We have receive at least one valid update, that's enough. This is needed to avoid |
| 324 |
// false positive when checking for disconnection in case our thread waited too long |
| 325 |
timed_out_servers[i] = false; |
| 326 |
|
| 327 |
const bool port_changed = |
| 328 |
!IsSameController(*port_info, server.m_port_info[port_info->pad_id]); |
| 329 |
if (port_changed) |
| 330 |
{ |
| 331 |
server.m_port_info[port_info->pad_id] = *port_info; |
| 332 |
// Just remove and re-add all the devices for simplicity |
| 333 |
GetControllerInterface().PlatformPopulateDevices([this] { PopulateDevices(); }); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
if (!m_hotplug_thread_running.IsSet()) // Avoid hanging the thread for too long |
| 339 |
return; |
| 340 |
} while (timeout > 0ms); |
| 341 |
|
| 342 |
// If we have failed to receive any information from the server (or even send it), |
| 343 |
// disconnect all devices from it (after enough time has elapsed, to avoid false positives). |
| 344 |
for (size_t i = 0; i < m_servers.size(); ++i) |
| 345 |
{ |
| 346 |
auto& server = m_servers[i]; |
| 347 |
if (timed_out_servers[i] && SteadyClock::now() >= server.m_disconnect_time) |
| 348 |
{ |
| 349 |
bool any_connected = false; |
| 350 |
for (size_t port_index = 0; port_index < server.m_port_info.size(); port_index++) |
| 351 |
{ |
| 352 |
any_connected = any_connected || |
| 353 |
server.m_port_info[port_index].pad_state == Proto::DsState::Connected; |
| 354 |
server.m_port_info[port_index] = {}; |
| 355 |
server.m_port_info[port_index].pad_id = static_cast<u8>(port_index); |
| 356 |
} |
| 357 |
// We can't only remove devices added by this server as we wouldn't know which they are |
| 358 |
if (any_connected) |
| 359 |
GetControllerInterface().PlatformPopulateDevices([this] { PopulateDevices(); }); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
void InputBackend::StartHotplugThread() |
| 366 |
{ |
| 367 |
// Mark the thread as running. |
| 368 |
if (!m_hotplug_thread_running.TestAndSet()) |
| 369 |
{ |
| 370 |
// It was already running. |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
m_hotplug_thread = std::thread(&InputBackend::HotplugThreadFunc, this); |
| 375 |
} |
| 376 |
|
| 377 |
void InputBackend::StopHotplugThread() |
| 378 |
{ |
| 379 |
// Tell the hotplug thread to stop. |
| 380 |
if (!m_hotplug_thread_running.TestAndClear()) |
| 381 |
{ |
| 382 |
// It wasn't running, we're done. |
| 383 |
return; |
| 384 |
} |
| 385 |
|
| 386 |
m_hotplug_thread.join(); |
| 387 |
|
| 388 |
for (auto& server : m_servers) |
| 389 |
{ |
| 390 |
server.m_socket.unbind(); // interrupt blocking socket |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// Also just start |
| 395 |
void InputBackend::Restart() |
| 396 |
{ |
| 397 |
INFO_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient Restart"); |
| 398 |
|
| 399 |
StopHotplugThread(); |
| 400 |
|
| 401 |
for (auto& server : m_servers) |
| 402 |
{ |
| 403 |
for (size_t port_index = 0; port_index < server.m_port_info.size(); port_index++) |
| 404 |
{ |
| 405 |
server.m_port_info[port_index] = {}; |
| 406 |
server.m_port_info[port_index].pad_id = static_cast<u8>(port_index); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
// Only removes devices as servers have been cleaned |
| 411 |
GetControllerInterface().PlatformPopulateDevices([this] { PopulateDevices(); }); |
| 412 |
|
| 413 |
m_client_uid = Common::Random::GenerateValue<u32>(); |
| 414 |
m_next_listports_time = SteadyClock::now(); |
| 415 |
|
| 416 |
if (m_servers_enabled && !m_servers.empty()) |
| 417 |
StartHotplugThread(); |
| 418 |
} |
| 419 |
|
| 420 |
void InputBackend::ConfigChanged() |
| 421 |
{ |
| 422 |
const bool servers_enabled = Config::Get(Settings::SERVERS_ENABLED); |
| 423 |
const std::string servers_setting = Config::Get(Settings::SERVERS); |
| 424 |
|
| 425 |
std::string new_servers_setting; |
| 426 |
for (const auto& server : m_servers) |
| 427 |
{ |
| 428 |
new_servers_setting += |
| 429 |
fmt::format("{}:{}:{};", server.m_description, server.m_address, server.m_port); |
| 430 |
} |
| 431 |
|
| 432 |
if (servers_enabled != m_servers_enabled || servers_setting != new_servers_setting) |
| 433 |
{ |
| 434 |
// Stop the thread before writing to m_servers |
| 435 |
StopHotplugThread(); |
| 436 |
|
| 437 |
m_servers_enabled = servers_enabled; |
| 438 |
m_servers.clear(); |
| 439 |
|
| 440 |
const auto server_details = SplitString(servers_setting, ';'); |
| 441 |
for (const auto& server_detail : server_details) |
| 442 |
{ |
| 443 |
const auto server_info = SplitString(server_detail, ':'); |
| 444 |
if (server_info.size() < 3) |
| 445 |
continue; |
| 446 |
|
| 447 |
const std::string description = server_info[0]; |
| 448 |
const std::string server_address = server_info[1]; |
| 449 |
const auto port = std::stoi(server_info[2]); |
| 450 |
if (port >= std::numeric_limits<u16>::max()) |
| 451 |
{ |
| 452 |
continue; |
| 453 |
} |
| 454 |
u16 server_port = static_cast<u16>(port); |
| 455 |
|
| 456 |
m_servers.emplace_back(description, server_address, server_port); |
| 457 |
} |
| 458 |
Restart(); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
InputBackend::InputBackend(ControllerInterface* controller_interface) |
| 463 |
: ciface::InputBackend(controller_interface) |
| 464 |
{ |
| 465 |
// The following is added for backwards compatibility |
| 466 |
const auto server_address_setting = Config::Get(Settings::SERVER_ADDRESS); |
| 467 |
const auto server_port_setting = Config::Get(Settings::SERVER_PORT); |
| 468 |
|
| 469 |
if (!server_address_setting.empty() && server_port_setting != 0) |
| 470 |
{ |
| 471 |
const auto& servers_setting = Config::Get(ciface::DualShockUDPClient::Settings::SERVERS); |
| 472 |
Config::SetBaseOrCurrent(ciface::DualShockUDPClient::Settings::SERVERS, |
| 473 |
servers_setting + fmt::format("{}:{}:{};", "DS4", |
| 474 |
server_address_setting, |
| 475 |
server_port_setting)); |
| 476 |
Config::SetBase(Settings::SERVER_ADDRESS, ""); |
| 477 |
Config::SetBase(Settings::SERVER_PORT, 0); |
| 478 |
} |
| 479 |
|
| 480 |
m_config_change_callback_id = |
| 481 |
Config::AddConfigChangedCallback(std::bind(&InputBackend::ConfigChanged, this)); |
| 482 |
// Call it immediately to load settings |
| 483 |
ConfigChanged(); |
| 484 |
} |
| 485 |
|
| 486 |
// This can be called by the host thread as well as the hotplug thread, concurrently. |
| 487 |
// So use PlatformPopulateDevices(). |
| 488 |
// m_servers is already safe because it can only be modified when the DSU thread is not running, |
| 489 |
// from the main thread |
| 490 |
void InputBackend::PopulateDevices() |
| 491 |
{ |
| 492 |
INFO_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient PopulateDevices"); |
| 493 |
|
| 494 |
// m_servers has already been updated so we can't use it to know which devices we removed, |
| 495 |
// also it's good to remove all of them before adding new ones so that their id will be set |
| 496 |
// correctly if they have the same name |
| 497 |
GetControllerInterface().RemoveDevice( |
| 498 |
[](const auto* dev) { return dev->GetSource() == DUALSHOCKUDP_SOURCE_NAME; }); |
| 499 |
|
| 500 |
// Users might have created more than one server on the same IP/Port. |
| 501 |
// Devices might end up being duplicated (if the server responds two all requests) |
| 502 |
// but they won't conflict. |
| 503 |
for (const auto& server : m_servers) |
| 504 |
{ |
| 505 |
for (size_t port_index = 0; port_index < server.m_port_info.size(); port_index++) |
| 506 |
{ |
| 507 |
const Proto::MessageType::PortInfo& port_info = server.m_port_info[port_index]; |
| 508 |
if (port_info.pad_state != Proto::DsState::Connected) |
| 509 |
continue; |
| 510 |
|
| 511 |
GetControllerInterface().AddDevice( |
| 512 |
std::make_shared<Device>(server.m_description, static_cast<int>(port_index), |
| 513 |
server.m_address, server.m_port, m_client_uid)); |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
InputBackend::~InputBackend() |
| 519 |
{ |
| 520 |
Config::RemoveConfigChangedCallback(m_config_change_callback_id); |
| 521 |
StopHotplugThread(); |
| 522 |
} |
| 523 |
|
| 524 |
Device::Device(std::string name, int index, std::string server_address, u16 server_port, |
| 525 |
u32 client_uid) |
| 526 |
: m_name{std::move(name)}, m_index{index}, m_server_address{std::move(server_address)}, |
| 527 |
m_server_port{server_port}, m_client_uid(client_uid) |
| 528 |
{ |
| 529 |
m_socket.setBlocking(false); |
| 530 |
|
| 531 |
AddInput(new AnalogInput<u8>("Pad W", m_pad_data.button_dpad_left_analog, 255)); |
| 532 |
AddInput(new AnalogInput<u8>("Pad S", m_pad_data.button_dpad_down_analog, 255)); |
| 533 |
AddInput(new AnalogInput<u8>("Pad E", m_pad_data.button_dpad_right_analog, 255)); |
| 534 |
AddInput(new AnalogInput<u8>("Pad N", m_pad_data.button_dpad_up_analog, 255)); |
| 535 |
AddInput(new AnalogInput<u8>("Square", m_pad_data.button_square_analog, 255)); |
| 536 |
AddInput(new AnalogInput<u8>("Cross", m_pad_data.button_cross_analog, 255)); |
| 537 |
AddInput(new AnalogInput<u8>("Circle", m_pad_data.button_circle_analog, 255)); |
| 538 |
AddInput(new AnalogInput<u8>("Triangle", m_pad_data.button_triangle_analog, 255)); |
| 539 |
AddInput(new AnalogInput<u8>("L1", m_pad_data.button_l1_analog, 255)); |
| 540 |
AddInput(new AnalogInput<u8>("R1", m_pad_data.button_r1_analog, 255)); |
| 541 |
|
| 542 |
AddInput(new AnalogInput<u8>("L2", m_pad_data.trigger_l2, 255)); |
| 543 |
AddInput(new AnalogInput<u8>("R2", m_pad_data.trigger_r2, 255)); |
| 544 |
|
| 545 |
AddInput(new Button<u8>("L3", m_pad_data.button_states1, 0x2)); |
| 546 |
AddInput(new Button<u8>("R3", m_pad_data.button_states1, 0x4)); |
| 547 |
AddInput(new Button<u8>("Share", m_pad_data.button_states1, 0x1)); |
| 548 |
AddInput(new Button<u8>("Options", m_pad_data.button_states1, 0x8)); |
| 549 |
AddInput(new Button<u8>("PS", m_pad_data.button_ps, 0x1)); |
| 550 |
AddInput(new Button<u8>("Touch Button", m_pad_data.button_touch, 0x1)); |
| 551 |
|
| 552 |
AddInput(new AnalogInput<u8>("Left X-", m_pad_data.left_stick_x, -128, -128)); |
| 553 |
AddInput(new AnalogInput<u8>("Left X+", m_pad_data.left_stick_x, 127, -128)); |
| 554 |
AddInput(new AnalogInput<u8>("Left Y-", m_pad_data.left_stick_y_inverted, -128, -128)); |
| 555 |
AddInput(new AnalogInput<u8>("Left Y+", m_pad_data.left_stick_y_inverted, 127, -128)); |
| 556 |
AddInput(new AnalogInput<u8>("Right X-", m_pad_data.right_stick_x, -128, -128)); |
| 557 |
AddInput(new AnalogInput<u8>("Right X+", m_pad_data.right_stick_x, 127, -128)); |
| 558 |
AddInput(new AnalogInput<u8>("Right Y-", m_pad_data.right_stick_y_inverted, -128, -128)); |
| 559 |
AddInput(new AnalogInput<u8>("Right Y+", m_pad_data.right_stick_y_inverted, 127, -128)); |
| 560 |
|
| 561 |
AddInput(new TouchInput("Touch X-", m_touch_x, -TOUCH_X_AXIS_MAX)); |
| 562 |
AddInput(new TouchInput("Touch X+", m_touch_x, TOUCH_X_AXIS_MAX)); |
| 563 |
AddInput(new TouchInput("Touch Y-", m_touch_y, -TOUCH_Y_AXIS_MAX)); |
| 564 |
AddInput(new TouchInput("Touch Y+", m_touch_y, TOUCH_Y_AXIS_MAX)); |
| 565 |
|
| 566 |
// Convert Gs to meters per second squared |
| 567 |
constexpr auto accel_scale = 1.0 / GRAVITY_ACCELERATION; |
| 568 |
|
| 569 |
AddInput(new AccelerometerInput("Accel Up", m_pad_data.accelerometer_y_g, -accel_scale)); |
| 570 |
AddInput(new AccelerometerInput("Accel Down", m_pad_data.accelerometer_y_g, accel_scale)); |
| 571 |
AddInput(new AccelerometerInput("Accel Left", m_pad_data.accelerometer_x_g, accel_scale)); |
| 572 |
AddInput(new AccelerometerInput("Accel Right", m_pad_data.accelerometer_x_g, -accel_scale)); |
| 573 |
AddInput(new AccelerometerInput("Accel Forward", m_pad_data.accelerometer_z_g, accel_scale)); |
| 574 |
AddInput(new AccelerometerInput("Accel Backward", m_pad_data.accelerometer_z_g, -accel_scale)); |
| 575 |
|
| 576 |
// Convert degrees per second to radians per second |
| 577 |
constexpr auto gyro_scale = 360.0 / MathUtil::TAU; |
| 578 |
|
| 579 |
AddInput(new GyroInput("Gyro Pitch Up", m_pad_data.gyro_pitch_deg_s, gyro_scale)); |
| 580 |
AddInput(new GyroInput("Gyro Pitch Down", m_pad_data.gyro_pitch_deg_s, -gyro_scale)); |
| 581 |
AddInput(new GyroInput("Gyro Roll Left", m_pad_data.gyro_roll_deg_s, -gyro_scale)); |
| 582 |
AddInput(new GyroInput("Gyro Roll Right", m_pad_data.gyro_roll_deg_s, gyro_scale)); |
| 583 |
AddInput(new GyroInput("Gyro Yaw Left", m_pad_data.gyro_yaw_deg_s, -gyro_scale)); |
| 584 |
AddInput(new GyroInput("Gyro Yaw Right", m_pad_data.gyro_yaw_deg_s, gyro_scale)); |
| 585 |
|
| 586 |
AddInput(new BatteryInput(m_pad_data.battery_status)); |
| 587 |
|
| 588 |
m_touch_x_min = 0; |
| 589 |
m_touch_y_min = 0; |
| 590 |
// DS4 touchpad max values |
| 591 |
m_touch_x_max = 1919; |
| 592 |
m_touch_y_max = 941; |
| 593 |
|
| 594 |
ResetPadData(); |
| 595 |
} |
| 596 |
|
| 597 |
void Device::ResetPadData() |
| 598 |
{ |
| 599 |
m_pad_data = Proto::MessageType::PadDataResponse{}; |
| 600 |
|
| 601 |
// Make sure they start from resting values, not from 0 |
| 602 |
m_touch_x = m_touch_x_min + ((m_touch_x_max - m_touch_x_min) / 2.0); |
| 603 |
m_touch_y = m_touch_y_min + ((m_touch_y_max - m_touch_y_min) / 2.0); |
| 604 |
m_pad_data.left_stick_x = 128; |
| 605 |
m_pad_data.left_stick_y_inverted = 128; |
| 606 |
m_pad_data.right_stick_x = 128; |
| 607 |
m_pad_data.right_stick_y_inverted = 128; |
| 608 |
m_pad_data.touch1.x = m_touch_x; |
| 609 |
m_pad_data.touch1.y = m_touch_y; |
| 610 |
} |
| 611 |
|
| 612 |
std::string Device::GetName() const |
| 613 |
{ |
| 614 |
return m_name; |
| 615 |
} |
| 616 |
|
| 617 |
std::string Device::GetSource() const |
| 618 |
{ |
| 619 |
return std::string(DUALSHOCKUDP_SOURCE_NAME); |
| 620 |
} |
| 621 |
|
| 622 |
Core::DeviceRemoval Device::UpdateInput() |
| 623 |
{ |
| 624 |
// Regularly tell the UDP server to feed us controller data |
| 625 |
const auto now = SteadyClock::now(); |
| 626 |
if (now >= m_next_reregister) |
| 627 |
{ |
| 628 |
m_next_reregister = now + SERVER_REREGISTER_INTERVAL; |
| 629 |
|
| 630 |
Proto::Message<Proto::MessageType::PadDataRequest> msg(m_client_uid); |
| 631 |
auto& data_req = msg.m_message; |
| 632 |
data_req.register_flags = Proto::RegisterFlags::PadID; |
| 633 |
data_req.pad_id_to_register = m_index; |
| 634 |
msg.Finish(); |
| 635 |
if (m_socket.send(&data_req, sizeof(data_req), sf::IpAddress::resolve(m_server_address).value(), |
| 636 |
m_server_port) != sf::Socket::Status::Done) |
| 637 |
{ |
| 638 |
ERROR_LOG_FMT(CONTROLLERINTERFACE, "DualShockUDPClient UpdateInput send failed"); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
// Receive and handle controller data |
| 643 |
Proto::Message<Proto::MessageType::FromServer> msg; |
| 644 |
std::size_t received_bytes; |
| 645 |
std::optional<sf::IpAddress> sender; |
| 646 |
u16 port; |
| 647 |
while (m_socket.receive(&msg, sizeof msg, received_bytes, sender, port) == |
| 648 |
sf::Socket::Status::Done) |
| 649 |
{ |
| 650 |
if (auto pad_data = msg.CheckAndCastTo<Proto::MessageType::PadDataResponse>()) |
| 651 |
{ |
| 652 |
m_pad_data = *pad_data; |
| 653 |
|
| 654 |
// Update touch pad relative coordinates |
| 655 |
if (m_pad_data.touch1.id != m_prev_touch.id) |
| 656 |
m_prev_touch_valid = false; |
| 657 |
if (m_prev_touch_valid) |
| 658 |
{ |
| 659 |
m_touch_x += m_pad_data.touch1.x - m_prev_touch.x; |
| 660 |
m_touch_y += m_pad_data.touch1.y - m_prev_touch.y; |
| 661 |
m_touch_x = std::clamp(m_touch_x, -TOUCH_X_AXIS_MAX, TOUCH_X_AXIS_MAX); |
| 662 |
m_touch_y = std::clamp(m_touch_y, -TOUCH_Y_AXIS_MAX, TOUCH_Y_AXIS_MAX); |
| 663 |
} |
| 664 |
m_prev_touch = m_pad_data.touch1; |
| 665 |
m_prev_touch_valid = true; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
return Core::DeviceRemoval::Keep; |
| 670 |
} |
| 671 |
|
| 672 |
std::optional<int> Device::GetPreferredId() const |
| 673 |
{ |
| 674 |
return m_index; |
| 675 |
} |
| 676 |
|
| 677 |
} // namespace ciface::DualShockUDPClient |