2019-08-24 15:57:49 +02:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-02-27 16:26:33 +01:00
|
|
|
#include <random>
|
2019-08-24 15:57:49 +02:00
|
|
|
#include <boost/asio.hpp>
|
2021-10-25 19:53:14 +02:00
|
|
|
#include <fmt/format.h>
|
2021-09-21 00:33:19 +02:00
|
|
|
|
2019-08-24 15:57:49 +02:00
|
|
|
#include "common/logging/log.h"
|
2021-09-21 00:33:19 +02:00
|
|
|
#include "common/param_package.h"
|
2021-04-15 01:07:40 +02:00
|
|
|
#include "common/settings.h"
|
2021-09-21 00:33:19 +02:00
|
|
|
#include "input_common/drivers/udp_client.h"
|
2021-09-20 23:57:55 +02:00
|
|
|
#include "input_common/helpers/udp_protocol.h"
|
2019-08-24 15:57:49 +02:00
|
|
|
|
|
|
|
using boost::asio::ip::udp;
|
|
|
|
|
|
|
|
namespace InputCommon::CemuhookUDP {
|
|
|
|
|
|
|
|
struct SocketCallback {
|
|
|
|
std::function<void(Response::Version)> version;
|
|
|
|
std::function<void(Response::PortInfo)> port_info;
|
|
|
|
std::function<void(Response::PadData)> pad_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Socket {
|
|
|
|
public:
|
|
|
|
using clock = std::chrono::system_clock;
|
|
|
|
|
2021-03-30 19:42:05 +02:00
|
|
|
explicit Socket(const std::string& host, u16 port, SocketCallback callback_)
|
2020-10-14 08:51:14 +02:00
|
|
|
: callback(std::move(callback_)), timer(io_service),
|
2021-03-30 19:42:05 +02:00
|
|
|
socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(GenerateRandomClientId()) {
|
2020-03-04 07:46:05 +01:00
|
|
|
boost::system::error_code ec{};
|
|
|
|
auto ipv4 = boost::asio::ip::make_address_v4(host, ec);
|
2020-03-17 12:29:25 +01:00
|
|
|
if (ec.value() != boost::system::errc::success) {
|
2020-03-04 07:46:05 +01:00
|
|
|
LOG_ERROR(Input, "Invalid IPv4 address \"{}\" provided to socket", host);
|
|
|
|
ipv4 = boost::asio::ip::address_v4{};
|
|
|
|
}
|
|
|
|
|
|
|
|
send_endpoint = {udp::endpoint(ipv4, port)};
|
|
|
|
}
|
2019-08-24 15:57:49 +02:00
|
|
|
|
|
|
|
void Stop() {
|
|
|
|
io_service.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Loop() {
|
|
|
|
io_service.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartSend(const clock::time_point& from) {
|
|
|
|
timer.expires_at(from + std::chrono::seconds(3));
|
|
|
|
timer.async_wait([this](const boost::system::error_code& error) { HandleSend(error); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartReceive() {
|
|
|
|
socket.async_receive_from(
|
|
|
|
boost::asio::buffer(receive_buffer), receive_endpoint,
|
|
|
|
[this](const boost::system::error_code& error, std::size_t bytes_transferred) {
|
|
|
|
HandleReceive(error, bytes_transferred);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-02-27 16:26:33 +01:00
|
|
|
u32 GenerateRandomClientId() const {
|
|
|
|
std::random_device device;
|
|
|
|
return device();
|
|
|
|
}
|
|
|
|
|
2020-11-15 12:22:01 +01:00
|
|
|
void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) {
|
2019-08-24 15:57:49 +02:00
|
|
|
if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) {
|
|
|
|
switch (*type) {
|
|
|
|
case Type::Version: {
|
|
|
|
Response::Version version;
|
|
|
|
std::memcpy(&version, &receive_buffer[sizeof(Header)], sizeof(Response::Version));
|
|
|
|
callback.version(std::move(version));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::PortInfo: {
|
|
|
|
Response::PortInfo port_info;
|
|
|
|
std::memcpy(&port_info, &receive_buffer[sizeof(Header)],
|
|
|
|
sizeof(Response::PortInfo));
|
|
|
|
callback.port_info(std::move(port_info));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::PadData: {
|
|
|
|
Response::PadData pad_data;
|
|
|
|
std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData));
|
|
|
|
callback.pad_data(std::move(pad_data));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StartReceive();
|
|
|
|
}
|
|
|
|
|
2020-11-15 12:22:01 +01:00
|
|
|
void HandleSend(const boost::system::error_code&) {
|
2020-03-04 07:46:05 +01:00
|
|
|
boost::system::error_code _ignored{};
|
2019-08-24 15:57:49 +02:00
|
|
|
// Send a request for getting port info for the pad
|
2021-03-30 19:42:05 +02:00
|
|
|
const Request::PortInfo port_info{4, {0, 1, 2, 3}};
|
2019-11-03 07:04:28 +01:00
|
|
|
const auto port_message = Request::Create(port_info, client_id);
|
2019-08-24 15:57:49 +02:00
|
|
|
std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
|
2020-03-04 07:46:05 +01:00
|
|
|
socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored);
|
2019-08-24 15:57:49 +02:00
|
|
|
|
|
|
|
// Send a request for getting pad data for the pad
|
2020-10-14 08:51:14 +02:00
|
|
|
const Request::PadData pad_data{
|
2021-11-26 22:45:37 +01:00
|
|
|
Request::RegisterFlags::AllPads,
|
2021-03-30 19:42:05 +02:00
|
|
|
0,
|
2020-10-14 08:51:14 +02:00
|
|
|
EMPTY_MAC_ADDRESS,
|
|
|
|
};
|
2019-11-03 07:04:28 +01:00
|
|
|
const auto pad_message = Request::Create(pad_data, client_id);
|
2019-08-24 15:57:49 +02:00
|
|
|
std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
|
2020-03-04 07:46:05 +01:00
|
|
|
socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored);
|
2019-08-24 15:57:49 +02:00
|
|
|
StartSend(timer.expiry());
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketCallback callback;
|
|
|
|
boost::asio::io_service io_service;
|
|
|
|
boost::asio::basic_waitable_timer<clock> timer;
|
|
|
|
udp::socket socket;
|
|
|
|
|
2021-02-27 16:26:33 +01:00
|
|
|
const u32 client_id;
|
2019-08-24 15:57:49 +02:00
|
|
|
|
|
|
|
static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>);
|
|
|
|
static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>);
|
|
|
|
std::array<u8, PORT_INFO_SIZE> send_buffer1;
|
|
|
|
std::array<u8, PAD_DATA_SIZE> send_buffer2;
|
|
|
|
udp::endpoint send_endpoint;
|
|
|
|
|
|
|
|
std::array<u8, MAX_PACKET_SIZE> receive_buffer;
|
|
|
|
udp::endpoint receive_endpoint;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void SocketLoop(Socket* socket) {
|
|
|
|
socket->StartReceive();
|
|
|
|
socket->StartSend(Socket::clock::now());
|
|
|
|
socket->Loop();
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:05:23 +01:00
|
|
|
UDPClient::UDPClient(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
|
2020-09-05 04:35:42 +02:00
|
|
|
LOG_INFO(Input, "Udp Initialization started");
|
2020-11-18 05:16:29 +01:00
|
|
|
ReloadSockets();
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
UDPClient::~UDPClient() {
|
2020-09-05 04:35:42 +02:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
UDPClient::ClientConnection::ClientConnection() = default;
|
2021-02-09 23:36:29 +01:00
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
UDPClient::ClientConnection::~ClientConnection() = default;
|
2021-02-09 23:36:29 +01:00
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
void UDPClient::ReloadSockets() {
|
2020-11-18 05:16:29 +01:00
|
|
|
Reset();
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
std::stringstream servers_ss(Settings::values.udp_input_servers.GetValue());
|
2020-11-18 05:16:29 +01:00
|
|
|
std::string server_token;
|
|
|
|
std::size_t client = 0;
|
|
|
|
while (std::getline(servers_ss, server_token, ',')) {
|
2021-01-01 19:32:29 +01:00
|
|
|
if (client == MAX_UDP_CLIENTS) {
|
2020-11-18 05:16:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::stringstream server_ss(server_token);
|
|
|
|
std::string token;
|
|
|
|
std::getline(server_ss, token, ':');
|
|
|
|
std::string udp_input_address = token;
|
|
|
|
std::getline(server_ss, token, ':');
|
|
|
|
char* temp;
|
|
|
|
const u16 udp_input_port = static_cast<u16>(std::strtol(token.c_str(), &temp, 0));
|
|
|
|
if (*temp != '\0') {
|
|
|
|
LOG_ERROR(Input, "Port number is not valid {}", token);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:42:05 +02:00
|
|
|
const std::size_t client_number = GetClientNumber(udp_input_address, udp_input_port);
|
|
|
|
if (client_number != MAX_UDP_CLIENTS) {
|
|
|
|
LOG_ERROR(Input, "Duplicated UDP servers found");
|
|
|
|
continue;
|
2020-11-18 05:16:29 +01:00
|
|
|
}
|
2021-03-30 19:42:05 +02:00
|
|
|
StartCommunication(client++, udp_input_address, udp_input_port);
|
2020-09-05 04:35:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 05:16:29 +01:00
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
std::size_t UDPClient::GetClientNumber(std::string_view host, u16 port) const {
|
2020-11-18 05:16:29 +01:00
|
|
|
for (std::size_t client = 0; client < clients.size(); client++) {
|
|
|
|
if (clients[client].active == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-30 19:42:05 +02:00
|
|
|
if (clients[client].host == host && clients[client].port == port) {
|
2020-11-18 05:16:29 +01:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 19:32:29 +01:00
|
|
|
return MAX_UDP_CLIENTS;
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
void UDPClient::OnVersion([[maybe_unused]] Response::Version data) {
|
2019-08-24 15:57:49 +02:00
|
|
|
LOG_TRACE(Input, "Version packet received: {}", data.version);
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
void UDPClient::OnPortInfo([[maybe_unused]] Response::PortInfo data) {
|
2019-08-24 15:57:49 +02:00
|
|
|
LOG_TRACE(Input, "PortInfo packet received: {}", data.model);
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
void UDPClient::OnPadData(Response::PadData data, std::size_t client) {
|
2021-03-30 19:42:05 +02:00
|
|
|
const std::size_t pad_index = (client * PADS_PER_CLIENT) + data.info.id;
|
|
|
|
|
|
|
|
if (pad_index >= pads.size()) {
|
|
|
|
LOG_ERROR(Input, "Invalid pad id {}", data.info.id);
|
2020-12-27 21:22:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-24 15:57:49 +02:00
|
|
|
LOG_TRACE(Input, "PadData packet received");
|
2021-03-30 19:42:05 +02:00
|
|
|
if (data.packet_counter == pads[pad_index].packet_sequence) {
|
2019-08-24 15:57:49 +02:00
|
|
|
LOG_WARNING(
|
|
|
|
Input,
|
|
|
|
"PadData packet dropped because its stale info. Current count: {} Packet count: {}",
|
2021-03-30 19:42:05 +02:00
|
|
|
pads[pad_index].packet_sequence, data.packet_counter);
|
|
|
|
pads[pad_index].connected = false;
|
2019-08-24 15:57:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-30 19:42:05 +02:00
|
|
|
|
|
|
|
clients[client].active = 1;
|
|
|
|
pads[pad_index].connected = true;
|
|
|
|
pads[pad_index].packet_sequence = data.packet_counter;
|
|
|
|
|
2020-11-18 05:16:29 +01:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2021-03-30 19:42:05 +02:00
|
|
|
const auto time_difference = static_cast<u64>(
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(now - pads[pad_index].last_update)
|
|
|
|
.count());
|
|
|
|
pads[pad_index].last_update = now;
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
// Gyroscope values are not it the correct scale from better joy.
|
2020-09-05 06:47:56 +02:00
|
|
|
// Dividing by 312 allows us to make one full turn = 1 turn
|
2020-09-05 04:35:42 +02:00
|
|
|
// This must be a configurable valued called sensitivity
|
2021-09-21 00:33:19 +02:00
|
|
|
const float gyro_scale = 1.0f / 312.0f;
|
|
|
|
|
|
|
|
const BasicMotion motion{
|
|
|
|
.gyro_x = data.gyro.pitch * gyro_scale,
|
|
|
|
.gyro_y = data.gyro.roll * gyro_scale,
|
|
|
|
.gyro_z = -data.gyro.yaw * gyro_scale,
|
|
|
|
.accel_x = data.accel.x,
|
|
|
|
.accel_y = -data.accel.z,
|
|
|
|
.accel_z = data.accel.y,
|
|
|
|
.delta_timestamp = time_difference,
|
|
|
|
};
|
|
|
|
const PadIdentifier identifier = GetPadIdentifier(pad_index);
|
|
|
|
SetMotion(identifier, 0, motion);
|
2021-10-21 06:18:04 +02:00
|
|
|
|
|
|
|
for (std::size_t id = 0; id < data.touch.size(); ++id) {
|
|
|
|
const auto touch_pad = data.touch[id];
|
2021-11-26 22:45:37 +01:00
|
|
|
const auto touch_axis_x_id =
|
|
|
|
static_cast<int>(id == 0 ? PadAxes::Touch1X : PadAxes::Touch2X);
|
|
|
|
const auto touch_axis_y_id =
|
|
|
|
static_cast<int>(id == 0 ? PadAxes::Touch1Y : PadAxes::Touch2Y);
|
|
|
|
const auto touch_button_id =
|
|
|
|
static_cast<int>(id == 0 ? PadButton::Touch1 : PadButton::touch2);
|
2021-10-21 06:18:04 +02:00
|
|
|
|
|
|
|
// TODO: Use custom calibration per device
|
|
|
|
const Common::ParamPackage touch_param(Settings::values.touch_device.GetValue());
|
|
|
|
const u16 min_x = static_cast<u16>(touch_param.Get("min_x", 100));
|
|
|
|
const u16 min_y = static_cast<u16>(touch_param.Get("min_y", 50));
|
|
|
|
const u16 max_x = static_cast<u16>(touch_param.Get("max_x", 1800));
|
|
|
|
const u16 max_y = static_cast<u16>(touch_param.Get("max_y", 850));
|
|
|
|
|
|
|
|
const f32 x =
|
|
|
|
static_cast<f32>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) /
|
|
|
|
static_cast<f32>(max_x - min_x);
|
|
|
|
const f32 y =
|
|
|
|
static_cast<f32>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) /
|
|
|
|
static_cast<f32>(max_y - min_y);
|
|
|
|
|
|
|
|
if (touch_pad.is_active) {
|
2021-11-26 22:45:37 +01:00
|
|
|
SetAxis(identifier, touch_axis_x_id, x);
|
|
|
|
SetAxis(identifier, touch_axis_y_id, y);
|
|
|
|
SetButton(identifier, touch_button_id, true);
|
2021-10-21 06:18:04 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-26 22:45:37 +01:00
|
|
|
SetAxis(identifier, touch_axis_x_id, 0);
|
|
|
|
SetAxis(identifier, touch_axis_y_id, 0);
|
|
|
|
SetButton(identifier, touch_button_id, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetAxis(identifier, static_cast<int>(PadAxes::LeftStickX),
|
|
|
|
(data.left_stick_x - 127.0f) / 127.0f);
|
|
|
|
SetAxis(identifier, static_cast<int>(PadAxes::LeftStickY),
|
|
|
|
(data.left_stick_y - 127.0f) / 127.0f);
|
|
|
|
SetAxis(identifier, static_cast<int>(PadAxes::RightStickX),
|
|
|
|
(data.right_stick_x - 127.0f) / 127.0f);
|
|
|
|
SetAxis(identifier, static_cast<int>(PadAxes::RightStickY),
|
|
|
|
(data.right_stick_y - 127.0f) / 127.0f);
|
|
|
|
|
|
|
|
static constexpr std::array<PadButton, 16> buttons{
|
|
|
|
PadButton::Share, PadButton::L3, PadButton::R3, PadButton::Options,
|
|
|
|
PadButton::Up, PadButton::Right, PadButton::Down, PadButton::Left,
|
|
|
|
PadButton::L2, PadButton::R2, PadButton::L1, PadButton::R1,
|
|
|
|
PadButton::Triangle, PadButton::Circle, PadButton::Cross, PadButton::Square};
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < buttons.size(); ++i) {
|
|
|
|
const bool button_status = (data.digital_button & (1U << i)) != 0;
|
|
|
|
const int button = static_cast<int>(buttons[i]);
|
|
|
|
SetButton(identifier, button, button_status);
|
2021-10-21 06:18:04 +02:00
|
|
|
}
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
void UDPClient::StartCommunication(std::size_t client, const std::string& host, u16 port) {
|
2019-08-24 15:57:49 +02:00
|
|
|
SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
|
|
|
|
[this](Response::PortInfo info) { OnPortInfo(info); },
|
2020-11-18 05:16:29 +01:00
|
|
|
[this, client](Response::PadData data) { OnPadData(data, client); }};
|
2021-03-30 19:42:05 +02:00
|
|
|
LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port);
|
2021-10-25 19:53:14 +02:00
|
|
|
clients[client].uuid = GetHostUUID(host);
|
2020-11-18 05:16:29 +01:00
|
|
|
clients[client].host = host;
|
|
|
|
clients[client].port = port;
|
|
|
|
clients[client].active = 0;
|
2021-03-30 19:42:05 +02:00
|
|
|
clients[client].socket = std::make_unique<Socket>(host, port, callback);
|
2020-09-05 04:35:42 +02:00
|
|
|
clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()};
|
2021-09-21 00:33:19 +02:00
|
|
|
for (std::size_t index = 0; index < PADS_PER_CLIENT; ++index) {
|
|
|
|
const PadIdentifier identifier = GetPadIdentifier(client * PADS_PER_CLIENT + index);
|
|
|
|
PreSetController(identifier);
|
2021-03-30 19:42:05 +02:00
|
|
|
}
|
2020-09-05 04:35:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
const PadIdentifier UDPClient::GetPadIdentifier(std::size_t pad_index) const {
|
|
|
|
const std::size_t client = pad_index / PADS_PER_CLIENT;
|
|
|
|
return {
|
2021-10-25 19:53:14 +02:00
|
|
|
.guid = clients[client].uuid,
|
2021-09-21 00:33:19 +02:00
|
|
|
.port = static_cast<std::size_t>(clients[client].port),
|
|
|
|
.pad = pad_index,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:53:14 +02:00
|
|
|
const Common::UUID UDPClient::GetHostUUID(const std::string host) const {
|
|
|
|
const auto ip = boost::asio::ip::address_v4::from_string(host);
|
|
|
|
const auto hex_host = fmt::format("{:06x}", ip.to_ulong());
|
|
|
|
return Common::UUID{hex_host};
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:33:19 +02:00
|
|
|
void UDPClient::Reset() {
|
2020-10-14 08:51:14 +02:00
|
|
|
for (auto& client : clients) {
|
2020-11-18 05:16:29 +01:00
|
|
|
if (client.thread.joinable()) {
|
|
|
|
client.active = -1;
|
|
|
|
client.socket->Stop();
|
|
|
|
client.thread.join();
|
|
|
|
}
|
2020-09-05 04:35:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:45:37 +01:00
|
|
|
std::vector<Common::ParamPackage> UDPClient::GetInputDevices() const {
|
|
|
|
std::vector<Common::ParamPackage> devices;
|
|
|
|
if (!Settings::values.enable_udp_controller) {
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
for (std::size_t client = 0; client < clients.size(); client++) {
|
|
|
|
if (clients[client].active != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (std::size_t index = 0; index < PADS_PER_CLIENT; ++index) {
|
|
|
|
const std::size_t pad_index = client * PADS_PER_CLIENT + index;
|
|
|
|
if (!pads[pad_index].connected) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto pad_identifier = GetPadIdentifier(pad_index);
|
|
|
|
Common::ParamPackage identifier{};
|
|
|
|
identifier.Set("engine", GetEngineName());
|
|
|
|
identifier.Set("display", fmt::format("UDP Controller {}", pad_identifier.pad));
|
|
|
|
identifier.Set("guid", pad_identifier.guid.Format());
|
|
|
|
identifier.Set("port", static_cast<int>(pad_identifier.port));
|
|
|
|
identifier.Set("pad", static_cast<int>(pad_identifier.pad));
|
|
|
|
devices.emplace_back(identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonMapping UDPClient::GetButtonMappingForDevice(const Common::ParamPackage& params) {
|
|
|
|
// This list excludes any button that can't be really mapped
|
|
|
|
static constexpr std::array<std::pair<Settings::NativeButton::Values, PadButton>, 18>
|
|
|
|
switch_to_dsu_button = {
|
|
|
|
std::pair{Settings::NativeButton::A, PadButton::Circle},
|
|
|
|
{Settings::NativeButton::B, PadButton::Cross},
|
|
|
|
{Settings::NativeButton::X, PadButton::Triangle},
|
|
|
|
{Settings::NativeButton::Y, PadButton::Square},
|
|
|
|
{Settings::NativeButton::Plus, PadButton::Options},
|
|
|
|
{Settings::NativeButton::Minus, PadButton::Share},
|
|
|
|
{Settings::NativeButton::DLeft, PadButton::Left},
|
|
|
|
{Settings::NativeButton::DUp, PadButton::Up},
|
|
|
|
{Settings::NativeButton::DRight, PadButton::Right},
|
|
|
|
{Settings::NativeButton::DDown, PadButton::Down},
|
|
|
|
{Settings::NativeButton::L, PadButton::L1},
|
|
|
|
{Settings::NativeButton::R, PadButton::R1},
|
|
|
|
{Settings::NativeButton::ZL, PadButton::L2},
|
|
|
|
{Settings::NativeButton::ZR, PadButton::R2},
|
|
|
|
{Settings::NativeButton::SL, PadButton::L2},
|
|
|
|
{Settings::NativeButton::SR, PadButton::R2},
|
|
|
|
{Settings::NativeButton::LStick, PadButton::L3},
|
|
|
|
{Settings::NativeButton::RStick, PadButton::R3},
|
|
|
|
};
|
|
|
|
if (!params.Has("guid") || !params.Has("port") || !params.Has("pad")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonMapping mapping{};
|
|
|
|
for (const auto& [switch_button, dsu_button] : switch_to_dsu_button) {
|
|
|
|
Common::ParamPackage button_params{};
|
|
|
|
button_params.Set("engine", GetEngineName());
|
|
|
|
button_params.Set("guid", params.Get("guid", ""));
|
|
|
|
button_params.Set("port", params.Get("port", 0));
|
|
|
|
button_params.Set("pad", params.Get("pad", 0));
|
|
|
|
button_params.Set("button", static_cast<int>(dsu_button));
|
|
|
|
mapping.insert_or_assign(switch_button, std::move(button_params));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalogMapping UDPClient::GetAnalogMappingForDevice(const Common::ParamPackage& params) {
|
|
|
|
if (!params.Has("guid") || !params.Has("port") || !params.Has("pad")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalogMapping mapping = {};
|
|
|
|
Common::ParamPackage left_analog_params;
|
|
|
|
left_analog_params.Set("engine", GetEngineName());
|
|
|
|
left_analog_params.Set("guid", params.Get("guid", ""));
|
|
|
|
left_analog_params.Set("port", params.Get("port", 0));
|
|
|
|
left_analog_params.Set("pad", params.Get("pad", 0));
|
|
|
|
left_analog_params.Set("axis_x", static_cast<int>(PadAxes::LeftStickX));
|
|
|
|
left_analog_params.Set("axis_y", static_cast<int>(PadAxes::LeftStickY));
|
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params));
|
|
|
|
Common::ParamPackage right_analog_params;
|
|
|
|
right_analog_params.Set("engine", GetEngineName());
|
|
|
|
right_analog_params.Set("guid", params.Get("guid", ""));
|
|
|
|
right_analog_params.Set("port", params.Get("port", 0));
|
|
|
|
right_analog_params.Set("pad", params.Get("pad", 0));
|
|
|
|
right_analog_params.Set("axis_x", static_cast<int>(PadAxes::RightStickX));
|
|
|
|
right_analog_params.Set("axis_y", static_cast<int>(PadAxes::RightStickY));
|
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
MotionMapping UDPClient::GetMotionMappingForDevice(const Common::ParamPackage& params) {
|
|
|
|
if (!params.Has("guid") || !params.Has("port") || !params.Has("pad")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
MotionMapping mapping = {};
|
|
|
|
Common::ParamPackage motion_params;
|
|
|
|
motion_params.Set("engine", GetEngineName());
|
|
|
|
motion_params.Set("guid", params.Get("guid", ""));
|
|
|
|
motion_params.Set("port", params.Get("port", 0));
|
|
|
|
motion_params.Set("pad", params.Get("pad", 0));
|
|
|
|
motion_params.Set("motion", 0);
|
|
|
|
mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, std::move(motion_params));
|
|
|
|
mapping.insert_or_assign(Settings::NativeMotion::MotionRight, std::move(motion_params));
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Input::ButtonNames UDPClient::GetUIButtonName(const Common::ParamPackage& params) const {
|
|
|
|
PadButton button = static_cast<PadButton>(params.Get("button", 0));
|
|
|
|
switch (button) {
|
|
|
|
case PadButton::Left:
|
|
|
|
return Common::Input::ButtonNames::ButtonLeft;
|
|
|
|
case PadButton::Right:
|
|
|
|
return Common::Input::ButtonNames::ButtonRight;
|
|
|
|
case PadButton::Down:
|
|
|
|
return Common::Input::ButtonNames::ButtonDown;
|
|
|
|
case PadButton::Up:
|
|
|
|
return Common::Input::ButtonNames::ButtonUp;
|
|
|
|
case PadButton::L1:
|
|
|
|
return Common::Input::ButtonNames::L1;
|
|
|
|
case PadButton::L2:
|
|
|
|
return Common::Input::ButtonNames::L2;
|
|
|
|
case PadButton::L3:
|
|
|
|
return Common::Input::ButtonNames::L3;
|
|
|
|
case PadButton::R1:
|
|
|
|
return Common::Input::ButtonNames::R1;
|
|
|
|
case PadButton::R2:
|
|
|
|
return Common::Input::ButtonNames::R2;
|
|
|
|
case PadButton::R3:
|
|
|
|
return Common::Input::ButtonNames::R3;
|
|
|
|
case PadButton::Circle:
|
|
|
|
return Common::Input::ButtonNames::Circle;
|
|
|
|
case PadButton::Cross:
|
|
|
|
return Common::Input::ButtonNames::Cross;
|
|
|
|
case PadButton::Square:
|
|
|
|
return Common::Input::ButtonNames::Square;
|
|
|
|
case PadButton::Triangle:
|
|
|
|
return Common::Input::ButtonNames::Triangle;
|
|
|
|
case PadButton::Share:
|
|
|
|
return Common::Input::ButtonNames::Share;
|
|
|
|
case PadButton::Options:
|
|
|
|
return Common::Input::ButtonNames::Options;
|
|
|
|
default:
|
|
|
|
return Common::Input::ButtonNames::Undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Input::ButtonNames UDPClient::GetUIName(const Common::ParamPackage& params) const {
|
|
|
|
if (params.Has("button")) {
|
|
|
|
return GetUIButtonName(params);
|
|
|
|
}
|
|
|
|
if (params.Has("axis")) {
|
|
|
|
return Common::Input::ButtonNames::Value;
|
|
|
|
}
|
|
|
|
if (params.Has("motion")) {
|
|
|
|
return Common::Input::ButtonNames::Engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Common::Input::ButtonNames::Invalid;
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:42:05 +02:00
|
|
|
void TestCommunication(const std::string& host, u16 port,
|
2020-10-16 12:22:26 +02:00
|
|
|
const std::function<void()>& success_callback,
|
|
|
|
const std::function<void()>& failure_callback) {
|
2019-08-24 15:57:49 +02:00
|
|
|
std::thread([=] {
|
|
|
|
Common::Event success_event;
|
2020-10-16 12:23:48 +02:00
|
|
|
SocketCallback callback{
|
|
|
|
.version = [](Response::Version) {},
|
|
|
|
.port_info = [](Response::PortInfo) {},
|
|
|
|
.pad_data = [&](Response::PadData) { success_event.Set(); },
|
|
|
|
};
|
2021-03-30 19:42:05 +02:00
|
|
|
Socket socket{host, port, std::move(callback)};
|
2019-08-24 15:57:49 +02:00
|
|
|
std::thread worker_thread{SocketLoop, &socket};
|
2021-03-30 19:42:05 +02:00
|
|
|
const bool result =
|
|
|
|
success_event.WaitUntil(std::chrono::steady_clock::now() + std::chrono::seconds(10));
|
2019-08-24 15:57:49 +02:00
|
|
|
socket.Stop();
|
|
|
|
worker_thread.join();
|
2019-11-03 08:07:04 +01:00
|
|
|
if (result) {
|
2019-08-24 15:57:49 +02:00
|
|
|
success_callback();
|
2019-11-03 08:07:04 +01:00
|
|
|
} else {
|
2019-08-24 15:57:49 +02:00
|
|
|
failure_callback();
|
2019-11-03 08:07:04 +01:00
|
|
|
}
|
2020-08-11 17:08:10 +02:00
|
|
|
}).detach();
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CalibrationConfigurationJob::CalibrationConfigurationJob(
|
2021-03-30 19:42:05 +02:00
|
|
|
const std::string& host, u16 port, std::function<void(Status)> status_callback,
|
2019-08-24 15:57:49 +02:00
|
|
|
std::function<void(u16, u16, u16, u16)> data_callback) {
|
|
|
|
|
2020-07-12 21:49:40 +02:00
|
|
|
std::thread([=, this] {
|
2019-08-24 15:57:49 +02:00
|
|
|
Status current_status{Status::Initialized};
|
2021-10-20 02:35:38 +02:00
|
|
|
SocketCallback callback{
|
|
|
|
[](Response::Version) {}, [](Response::PortInfo) {},
|
|
|
|
[&](Response::PadData data) {
|
|
|
|
static constexpr u16 CALIBRATION_THRESHOLD = 100;
|
|
|
|
static constexpr u16 MAX_VALUE = UINT16_MAX;
|
|
|
|
|
|
|
|
if (current_status == Status::Initialized) {
|
|
|
|
// Receiving data means the communication is ready now
|
|
|
|
current_status = Status::Ready;
|
|
|
|
status_callback(current_status);
|
|
|
|
}
|
|
|
|
const auto& touchpad_0 = data.touch[0];
|
|
|
|
if (touchpad_0.is_active == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG_DEBUG(Input, "Current touch: {} {}", touchpad_0.x, touchpad_0.y);
|
|
|
|
const u16 min_x = std::min(MAX_VALUE, static_cast<u16>(touchpad_0.x));
|
|
|
|
const u16 min_y = std::min(MAX_VALUE, static_cast<u16>(touchpad_0.y));
|
|
|
|
if (current_status == Status::Ready) {
|
|
|
|
// First touch - min data (min_x/min_y)
|
|
|
|
current_status = Status::Stage1Completed;
|
|
|
|
status_callback(current_status);
|
|
|
|
}
|
|
|
|
if (touchpad_0.x - min_x > CALIBRATION_THRESHOLD &&
|
|
|
|
touchpad_0.y - min_y > CALIBRATION_THRESHOLD) {
|
|
|
|
// Set the current position as max value and finishes configuration
|
|
|
|
const u16 max_x = touchpad_0.x;
|
|
|
|
const u16 max_y = touchpad_0.y;
|
|
|
|
current_status = Status::Completed;
|
|
|
|
data_callback(min_x, min_y, max_x, max_y);
|
|
|
|
status_callback(current_status);
|
|
|
|
|
|
|
|
complete_event.Set();
|
|
|
|
}
|
|
|
|
}};
|
2021-03-30 19:42:05 +02:00
|
|
|
Socket socket{host, port, std::move(callback)};
|
2019-08-24 15:57:49 +02:00
|
|
|
std::thread worker_thread{SocketLoop, &socket};
|
|
|
|
complete_event.Wait();
|
|
|
|
socket.Stop();
|
|
|
|
worker_thread.join();
|
2020-08-11 17:08:10 +02:00
|
|
|
}).detach();
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CalibrationConfigurationJob::~CalibrationConfigurationJob() {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalibrationConfigurationJob::Stop() {
|
|
|
|
complete_event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon::CemuhookUDP
|