2019-08-24 13:57:49 +00:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
2021-02-27 15:26:33 +00:00
|
|
|
#include <random>
|
2019-08-24 13:57:49 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include "common/logging/log.h"
|
2021-04-14 23:07:40 +00:00
|
|
|
#include "common/settings.h"
|
2019-08-24 13:57:49 +00:00
|
|
|
#include "input_common/udp/client.h"
|
|
|
|
#include "input_common/udp/protocol.h"
|
|
|
|
|
|
|
|
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 17:42:05 +00:00
|
|
|
explicit Socket(const std::string& host, u16 port, SocketCallback callback_)
|
2020-10-14 06:51:14 +00:00
|
|
|
: callback(std::move(callback_)), timer(io_service),
|
2021-03-30 17:42:05 +00:00
|
|
|
socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(GenerateRandomClientId()) {
|
2020-03-04 06:46:05 +00:00
|
|
|
boost::system::error_code ec{};
|
|
|
|
auto ipv4 = boost::asio::ip::make_address_v4(host, ec);
|
2020-03-17 11:29:25 +00:00
|
|
|
if (ec.value() != boost::system::errc::success) {
|
2020-03-04 06:46:05 +00: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 13:57:49 +00: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 15:26:33 +00:00
|
|
|
u32 GenerateRandomClientId() const {
|
|
|
|
std::random_device device;
|
|
|
|
return device();
|
|
|
|
}
|
|
|
|
|
2020-11-15 11:22:01 +00:00
|
|
|
void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) {
|
2019-08-24 13:57:49 +00: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));
|
2021-05-13 18:26:07 +00:00
|
|
|
SanitizeMotion(pad_data);
|
2019-08-24 13:57:49 +00:00
|
|
|
callback.pad_data(std::move(pad_data));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StartReceive();
|
|
|
|
}
|
|
|
|
|
2020-11-15 11:22:01 +00:00
|
|
|
void HandleSend(const boost::system::error_code&) {
|
2020-03-04 06:46:05 +00:00
|
|
|
boost::system::error_code _ignored{};
|
2019-08-24 13:57:49 +00:00
|
|
|
// Send a request for getting port info for the pad
|
2021-03-30 17:42:05 +00:00
|
|
|
const Request::PortInfo port_info{4, {0, 1, 2, 3}};
|
2019-11-03 06:04:28 +00:00
|
|
|
const auto port_message = Request::Create(port_info, client_id);
|
2019-08-24 13:57:49 +00:00
|
|
|
std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
|
2020-03-04 06:46:05 +00:00
|
|
|
socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored);
|
2019-08-24 13:57:49 +00:00
|
|
|
|
|
|
|
// Send a request for getting pad data for the pad
|
2020-10-14 06:51:14 +00:00
|
|
|
const Request::PadData pad_data{
|
2021-03-30 17:42:05 +00:00
|
|
|
Request::PadData::Flags::AllPorts,
|
|
|
|
0,
|
2020-10-14 06:51:14 +00:00
|
|
|
EMPTY_MAC_ADDRESS,
|
|
|
|
};
|
2019-11-03 06:04:28 +00:00
|
|
|
const auto pad_message = Request::Create(pad_data, client_id);
|
2019-08-24 13:57:49 +00:00
|
|
|
std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
|
2020-03-04 06:46:05 +00:00
|
|
|
socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored);
|
2019-08-24 13:57:49 +00:00
|
|
|
StartSend(timer.expiry());
|
|
|
|
}
|
|
|
|
|
2021-05-13 18:26:07 +00:00
|
|
|
void SanitizeMotion(Response::PadData& data) {
|
|
|
|
// Zero out any non number value
|
|
|
|
if (!std::isnormal(data.gyro.pitch)) {
|
|
|
|
data.gyro.pitch = 0;
|
|
|
|
}
|
|
|
|
if (!std::isnormal(data.gyro.roll)) {
|
|
|
|
data.gyro.roll = 0;
|
|
|
|
}
|
|
|
|
if (!std::isnormal(data.gyro.yaw)) {
|
|
|
|
data.gyro.yaw = 0;
|
|
|
|
}
|
|
|
|
if (!std::isnormal(data.accel.x)) {
|
|
|
|
data.accel.x = 0;
|
|
|
|
}
|
|
|
|
if (!std::isnormal(data.accel.y)) {
|
|
|
|
data.accel.y = 0;
|
|
|
|
}
|
|
|
|
if (!std::isnormal(data.accel.z)) {
|
|
|
|
data.accel.z = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 13:57:49 +00:00
|
|
|
SocketCallback callback;
|
|
|
|
boost::asio::io_service io_service;
|
|
|
|
boost::asio::basic_waitable_timer<clock> timer;
|
|
|
|
udp::socket socket;
|
|
|
|
|
2021-02-27 15:26:33 +00:00
|
|
|
const u32 client_id;
|
2019-08-24 13:57:49 +00: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();
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:35:42 +00:00
|
|
|
Client::Client() {
|
|
|
|
LOG_INFO(Input, "Udp Initialization started");
|
2021-01-10 14:36:31 +00:00
|
|
|
finger_id.fill(MAX_TOUCH_FINGERS);
|
2020-11-18 04:16:29 +00:00
|
|
|
ReloadSockets();
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client() {
|
2020-09-05 02:35:42 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
Client::ClientConnection::ClientConnection() = default;
|
2021-02-09 22:36:29 +00:00
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
Client::ClientConnection::~ClientConnection() = default;
|
2021-02-09 22:36:29 +00:00
|
|
|
|
2020-09-05 02:35:42 +00:00
|
|
|
std::vector<Common::ParamPackage> Client::GetInputDevices() const {
|
|
|
|
std::vector<Common::ParamPackage> devices;
|
2021-03-30 17:42:05 +00:00
|
|
|
for (std::size_t pad = 0; pad < pads.size(); pad++) {
|
|
|
|
if (!DeviceConnected(pad)) {
|
2020-09-05 02:35:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-30 17:42:05 +00:00
|
|
|
std::string name = fmt::format("UDP Controller {}", pad);
|
2020-09-05 02:35:42 +00:00
|
|
|
devices.emplace_back(Common::ParamPackage{
|
|
|
|
{"class", "cemuhookudp"},
|
|
|
|
{"display", std::move(name)},
|
2021-03-30 17:42:05 +00:00
|
|
|
{"port", std::to_string(pad)},
|
2020-09-05 02:35:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return devices;
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
bool Client::DeviceConnected(std::size_t pad) const {
|
2020-09-05 02:35:42 +00:00
|
|
|
// Use last timestamp to detect if the socket has stopped sending data
|
2020-11-18 04:16:29 +00:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2021-03-30 17:42:05 +00:00
|
|
|
const auto time_difference = static_cast<u64>(
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now - pads[pad].last_update).count());
|
|
|
|
return time_difference < 1000 && pads[pad].connected;
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
void Client::ReloadSockets() {
|
|
|
|
Reset();
|
|
|
|
|
2021-06-28 19:58:16 +00:00
|
|
|
std::stringstream servers_ss(static_cast<std::string>(Settings::values.udp_input_servers));
|
2020-11-18 04:16:29 +00:00
|
|
|
std::string server_token;
|
|
|
|
std::size_t client = 0;
|
|
|
|
while (std::getline(servers_ss, server_token, ',')) {
|
2021-01-01 18:32:29 +00:00
|
|
|
if (client == MAX_UDP_CLIENTS) {
|
2020-11-18 04:16:29 +00: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 17:42:05 +00: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 04:16:29 +00:00
|
|
|
}
|
2021-03-30 17:42:05 +00:00
|
|
|
StartCommunication(client++, udp_input_address, udp_input_port);
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 04:16:29 +00:00
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
std::size_t Client::GetClientNumber(std::string_view host, u16 port) const {
|
2020-11-18 04:16:29 +00:00
|
|
|
for (std::size_t client = 0; client < clients.size(); client++) {
|
|
|
|
if (clients[client].active == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-30 17:42:05 +00:00
|
|
|
if (clients[client].host == host && clients[client].port == port) {
|
2020-11-18 04:16:29 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 18:32:29 +00:00
|
|
|
return MAX_UDP_CLIENTS;
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 04:50:35 +00:00
|
|
|
void Client::OnVersion([[maybe_unused]] Response::Version data) {
|
2019-08-24 13:57:49 +00:00
|
|
|
LOG_TRACE(Input, "Version packet received: {}", data.version);
|
|
|
|
}
|
|
|
|
|
2020-11-24 04:50:35 +00:00
|
|
|
void Client::OnPortInfo([[maybe_unused]] Response::PortInfo data) {
|
2019-08-24 13:57:49 +00:00
|
|
|
LOG_TRACE(Input, "PortInfo packet received: {}", data.model);
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
void Client::OnPadData(Response::PadData data, std::size_t client) {
|
2021-03-30 17:42:05 +00: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 20:22:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-24 13:57:49 +00:00
|
|
|
LOG_TRACE(Input, "PadData packet received");
|
2021-03-30 17:42:05 +00:00
|
|
|
if (data.packet_counter == pads[pad_index].packet_sequence) {
|
2019-08-24 13:57:49 +00:00
|
|
|
LOG_WARNING(
|
|
|
|
Input,
|
|
|
|
"PadData packet dropped because its stale info. Current count: {} Packet count: {}",
|
2021-03-30 17:42:05 +00:00
|
|
|
pads[pad_index].packet_sequence, data.packet_counter);
|
|
|
|
pads[pad_index].connected = false;
|
2019-08-24 13:57:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-30 17:42:05 +00:00
|
|
|
|
|
|
|
clients[client].active = 1;
|
|
|
|
pads[pad_index].connected = true;
|
|
|
|
pads[pad_index].packet_sequence = data.packet_counter;
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2021-03-30 17:42:05 +00: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-10-14 06:51:14 +00:00
|
|
|
const Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw};
|
2021-03-30 17:42:05 +00:00
|
|
|
pads[pad_index].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y});
|
2020-09-05 02:35:42 +00:00
|
|
|
// Gyroscope values are not it the correct scale from better joy.
|
2020-09-05 04:47:56 +00:00
|
|
|
// Dividing by 312 allows us to make one full turn = 1 turn
|
2020-09-05 02:35:42 +00:00
|
|
|
// This must be a configurable valued called sensitivity
|
2021-03-30 17:42:05 +00:00
|
|
|
pads[pad_index].motion.SetGyroscope(raw_gyroscope / 312.0f);
|
|
|
|
pads[pad_index].motion.UpdateRotation(time_difference);
|
|
|
|
pads[pad_index].motion.UpdateOrientation(time_difference);
|
2019-08-24 13:57:49 +00:00
|
|
|
|
2020-09-05 02:35:42 +00:00
|
|
|
{
|
2021-03-30 17:42:05 +00:00
|
|
|
std::lock_guard guard(pads[pad_index].status.update_mutex);
|
|
|
|
pads[pad_index].status.motion_status = pads[pad_index].motion.GetMotion();
|
2019-08-24 13:57:49 +00:00
|
|
|
|
2021-01-03 04:04:50 +00:00
|
|
|
for (std::size_t id = 0; id < data.touch.size(); ++id) {
|
2021-01-01 18:32:29 +00:00
|
|
|
UpdateTouchInput(data.touch[id], client, id);
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:35:42 +00:00
|
|
|
if (configuring) {
|
2021-03-30 17:42:05 +00:00
|
|
|
const Common::Vec3f gyroscope = pads[pad_index].motion.GetGyroscope();
|
|
|
|
const Common::Vec3f accelerometer = pads[pad_index].motion.GetAcceleration();
|
|
|
|
UpdateYuzuSettings(client, data.info.id, accelerometer, gyroscope);
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
void Client::StartCommunication(std::size_t client, const std::string& host, u16 port) {
|
2019-08-24 13:57:49 +00:00
|
|
|
SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
|
|
|
|
[this](Response::PortInfo info) { OnPortInfo(info); },
|
2020-11-18 04:16:29 +00:00
|
|
|
[this, client](Response::PadData data) { OnPadData(data, client); }};
|
2021-03-30 17:42:05 +00:00
|
|
|
LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port);
|
2020-11-18 04:16:29 +00:00
|
|
|
clients[client].host = host;
|
|
|
|
clients[client].port = port;
|
|
|
|
clients[client].active = 0;
|
2021-03-30 17:42:05 +00:00
|
|
|
clients[client].socket = std::make_unique<Socket>(host, port, callback);
|
2020-09-05 02:35:42 +00:00
|
|
|
clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()};
|
2021-03-30 17:42:05 +00:00
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
// Set motion parameters
|
|
|
|
// SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode
|
|
|
|
// Real HW values are unknown, 0.0001 is an approximate to Standard
|
2021-03-30 17:42:05 +00:00
|
|
|
for (std::size_t pad = 0; pad < PADS_PER_CLIENT; pad++) {
|
|
|
|
pads[client * PADS_PER_CLIENT + pad].motion.SetGyroThreshold(0.0001f);
|
|
|
|
}
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::Reset() {
|
2020-10-14 06:51:14 +00:00
|
|
|
for (auto& client : clients) {
|
2020-11-18 04:16:29 +00:00
|
|
|
if (client.thread.joinable()) {
|
|
|
|
client.active = -1;
|
|
|
|
client.socket->Stop();
|
|
|
|
client.thread.join();
|
|
|
|
}
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
void Client::UpdateYuzuSettings(std::size_t client, std::size_t pad_index,
|
|
|
|
const Common::Vec3<float>& acc, const Common::Vec3<float>& gyro) {
|
2020-09-26 08:32:28 +00:00
|
|
|
if (gyro.Length() > 0.2f) {
|
2021-01-01 18:32:29 +00:00
|
|
|
LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {})", client,
|
|
|
|
gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2]);
|
2020-09-26 08:32:28 +00:00
|
|
|
}
|
2020-11-18 04:16:29 +00:00
|
|
|
UDPPadStatus pad{
|
|
|
|
.host = clients[client].host,
|
|
|
|
.port = clients[client].port,
|
2021-03-30 17:42:05 +00:00
|
|
|
.pad_index = pad_index,
|
2020-11-18 04:16:29 +00:00
|
|
|
};
|
2021-01-03 04:04:50 +00:00
|
|
|
for (std::size_t i = 0; i < 3; ++i) {
|
2020-09-26 08:32:28 +00:00
|
|
|
if (gyro[i] > 5.0f || gyro[i] < -5.0f) {
|
2020-09-05 04:47:56 +00:00
|
|
|
pad.motion = static_cast<PadMotion>(i);
|
|
|
|
pad.motion_value = gyro[i];
|
2020-11-18 04:16:29 +00:00
|
|
|
pad_queue.Push(pad);
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
2020-09-26 08:32:28 +00:00
|
|
|
if (acc[i] > 1.75f || acc[i] < -1.75f) {
|
2020-09-05 04:47:56 +00:00
|
|
|
pad.motion = static_cast<PadMotion>(i + 3);
|
|
|
|
pad.motion_value = acc[i];
|
2020-11-18 04:16:29 +00:00
|
|
|
pad_queue.Push(pad);
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 04:04:50 +00:00
|
|
|
std::optional<std::size_t> Client::GetUnusedFingerID() const {
|
|
|
|
std::size_t first_free_id = 0;
|
2021-01-01 18:32:29 +00:00
|
|
|
while (first_free_id < MAX_TOUCH_FINGERS) {
|
|
|
|
if (!std::get<2>(touch_status[first_free_id])) {
|
|
|
|
return first_free_id;
|
|
|
|
} else {
|
|
|
|
first_free_id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-01-03 04:04:50 +00:00
|
|
|
void Client::UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id) {
|
2021-01-01 18:32:29 +00:00
|
|
|
// TODO: Use custom calibration per device
|
2021-06-28 19:58:16 +00:00
|
|
|
const Common::ParamPackage touch_param(Settings::values.touch_device.GetValue());
|
2021-01-01 18:32:29 +00:00
|
|
|
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));
|
2021-01-10 14:36:31 +00:00
|
|
|
const std::size_t touch_id = client * 2 + id;
|
2021-01-01 18:32:29 +00:00
|
|
|
if (touch_pad.is_active) {
|
2021-01-10 14:36:31 +00:00
|
|
|
if (finger_id[touch_id] == MAX_TOUCH_FINGERS) {
|
2021-01-01 18:32:29 +00:00
|
|
|
const auto first_free_id = GetUnusedFingerID();
|
|
|
|
if (!first_free_id) {
|
|
|
|
// Invalid finger id skip to next input
|
|
|
|
return;
|
|
|
|
}
|
2021-01-10 14:36:31 +00:00
|
|
|
finger_id[touch_id] = *first_free_id;
|
2021-01-01 18:32:29 +00:00
|
|
|
}
|
2021-01-10 14:36:31 +00:00
|
|
|
auto& [x, y, pressed] = touch_status[finger_id[touch_id]];
|
2021-01-01 18:32:29 +00:00
|
|
|
x = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) /
|
|
|
|
static_cast<float>(max_x - min_x);
|
|
|
|
y = static_cast<float>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) /
|
|
|
|
static_cast<float>(max_y - min_y);
|
|
|
|
pressed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:36:31 +00:00
|
|
|
if (finger_id[touch_id] != MAX_TOUCH_FINGERS) {
|
|
|
|
touch_status[finger_id[touch_id]] = {};
|
|
|
|
finger_id[touch_id] = MAX_TOUCH_FINGERS;
|
2021-01-01 18:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:35:42 +00:00
|
|
|
void Client::BeginConfiguration() {
|
2020-11-18 04:16:29 +00:00
|
|
|
pad_queue.Clear();
|
2020-09-05 02:35:42 +00:00
|
|
|
configuring = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::EndConfiguration() {
|
2020-11-18 04:16:29 +00:00
|
|
|
pad_queue.Clear();
|
2020-09-05 02:35:42 +00:00
|
|
|
configuring = false;
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) {
|
2021-03-30 17:42:05 +00:00
|
|
|
const std::size_t client_number = GetClientNumber(host, port);
|
|
|
|
if (client_number == MAX_UDP_CLIENTS || pad >= PADS_PER_CLIENT) {
|
|
|
|
return pads[0].status;
|
2020-11-18 04:16:29 +00:00
|
|
|
}
|
2021-03-30 17:42:05 +00:00
|
|
|
return pads[(client_number * PADS_PER_CLIENT) + pad].status;
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const {
|
2021-03-30 17:42:05 +00:00
|
|
|
const std::size_t client_number = GetClientNumber(host, port);
|
|
|
|
if (client_number == MAX_UDP_CLIENTS || pad >= PADS_PER_CLIENT) {
|
|
|
|
return pads[0].status;
|
2020-11-18 04:16:29 +00:00
|
|
|
}
|
2021-03-30 17:42:05 +00:00
|
|
|
return pads[(client_number * PADS_PER_CLIENT) + pad].status;
|
2020-09-05 02:35:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 18:32:29 +00:00
|
|
|
Input::TouchStatus& Client::GetTouchState() {
|
|
|
|
return touch_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Input::TouchStatus& Client::GetTouchState() const {
|
|
|
|
return touch_status;
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() {
|
2020-09-05 02:35:42 +00:00
|
|
|
return pad_queue;
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
const Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() const {
|
2020-09-05 02:35:42 +00:00
|
|
|
return pad_queue;
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 17:42:05 +00:00
|
|
|
void TestCommunication(const std::string& host, u16 port,
|
2020-10-16 10:22:26 +00:00
|
|
|
const std::function<void()>& success_callback,
|
|
|
|
const std::function<void()>& failure_callback) {
|
2019-08-24 13:57:49 +00:00
|
|
|
std::thread([=] {
|
|
|
|
Common::Event success_event;
|
2020-10-16 10:23:48 +00:00
|
|
|
SocketCallback callback{
|
|
|
|
.version = [](Response::Version) {},
|
|
|
|
.port_info = [](Response::PortInfo) {},
|
|
|
|
.pad_data = [&](Response::PadData) { success_event.Set(); },
|
|
|
|
};
|
2021-03-30 17:42:05 +00:00
|
|
|
Socket socket{host, port, std::move(callback)};
|
2019-08-24 13:57:49 +00:00
|
|
|
std::thread worker_thread{SocketLoop, &socket};
|
2021-03-30 17:42:05 +00:00
|
|
|
const bool result =
|
|
|
|
success_event.WaitUntil(std::chrono::steady_clock::now() + std::chrono::seconds(10));
|
2019-08-24 13:57:49 +00:00
|
|
|
socket.Stop();
|
|
|
|
worker_thread.join();
|
2019-11-03 07:07:04 +00:00
|
|
|
if (result) {
|
2019-08-24 13:57:49 +00:00
|
|
|
success_callback();
|
2019-11-03 07:07:04 +00:00
|
|
|
} else {
|
2019-08-24 13:57:49 +00:00
|
|
|
failure_callback();
|
2019-11-03 07:07:04 +00:00
|
|
|
}
|
2020-08-11 15:08:10 +00:00
|
|
|
}).detach();
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CalibrationConfigurationJob::CalibrationConfigurationJob(
|
2021-03-30 17:42:05 +00:00
|
|
|
const std::string& host, u16 port, std::function<void(Status)> status_callback,
|
2019-08-24 13:57:49 +00:00
|
|
|
std::function<void(u16, u16, u16, u16)> data_callback) {
|
|
|
|
|
2020-07-12 19:49:40 +00:00
|
|
|
std::thread([=, this] {
|
2019-08-24 13:57:49 +00:00
|
|
|
constexpr u16 CALIBRATION_THRESHOLD = 100;
|
|
|
|
|
2019-11-03 07:07:04 +00:00
|
|
|
u16 min_x{UINT16_MAX};
|
|
|
|
u16 min_y{UINT16_MAX};
|
|
|
|
u16 max_x{};
|
|
|
|
u16 max_y{};
|
2019-08-24 13:57:49 +00:00
|
|
|
|
|
|
|
Status current_status{Status::Initialized};
|
2020-11-15 11:22:01 +00:00
|
|
|
SocketCallback callback{[](Response::Version) {}, [](Response::PortInfo) {},
|
2019-08-24 13:57:49 +00:00
|
|
|
[&](Response::PadData data) {
|
|
|
|
if (current_status == Status::Initialized) {
|
|
|
|
// Receiving data means the communication is ready now
|
|
|
|
current_status = Status::Ready;
|
|
|
|
status_callback(current_status);
|
|
|
|
}
|
2021-01-01 18:32:29 +00:00
|
|
|
if (data.touch[0].is_active == 0) {
|
2019-08-24 13:57:49 +00:00
|
|
|
return;
|
2019-11-03 07:07:04 +00:00
|
|
|
}
|
2021-01-01 18:32:29 +00:00
|
|
|
LOG_DEBUG(Input, "Current touch: {} {}", data.touch[0].x,
|
|
|
|
data.touch[0].y);
|
|
|
|
min_x = std::min(min_x, static_cast<u16>(data.touch[0].x));
|
|
|
|
min_y = std::min(min_y, static_cast<u16>(data.touch[0].y));
|
2019-08-24 13:57:49 +00:00
|
|
|
if (current_status == Status::Ready) {
|
|
|
|
// First touch - min data (min_x/min_y)
|
|
|
|
current_status = Status::Stage1Completed;
|
|
|
|
status_callback(current_status);
|
|
|
|
}
|
2021-01-01 18:32:29 +00:00
|
|
|
if (data.touch[0].x - min_x > CALIBRATION_THRESHOLD &&
|
|
|
|
data.touch[0].y - min_y > CALIBRATION_THRESHOLD) {
|
2019-08-24 13:57:49 +00:00
|
|
|
// Set the current position as max value and finishes
|
|
|
|
// configuration
|
2021-01-01 18:32:29 +00:00
|
|
|
max_x = data.touch[0].x;
|
|
|
|
max_y = data.touch[0].y;
|
2019-08-24 13:57:49 +00:00
|
|
|
current_status = Status::Completed;
|
|
|
|
data_callback(min_x, min_y, max_x, max_y);
|
|
|
|
status_callback(current_status);
|
|
|
|
|
|
|
|
complete_event.Set();
|
|
|
|
}
|
|
|
|
}};
|
2021-03-30 17:42:05 +00:00
|
|
|
Socket socket{host, port, std::move(callback)};
|
2019-08-24 13:57:49 +00:00
|
|
|
std::thread worker_thread{SocketLoop, &socket};
|
|
|
|
complete_event.Wait();
|
|
|
|
socket.Stop();
|
|
|
|
worker_thread.join();
|
2020-08-11 15:08:10 +00:00
|
|
|
}).detach();
|
2019-08-24 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CalibrationConfigurationJob::~CalibrationConfigurationJob() {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalibrationConfigurationJob::Stop() {
|
|
|
|
complete_event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon::CemuhookUDP
|