2021-10-21 11:56:52 -07:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
2019-08-24 06:57:49 -07:00
|
|
|
// Licensed under GPLv2 or any later version
|
2021-10-21 11:56:52 -07:00
|
|
|
// Refer to the license.txt file included.
|
2019-08-24 06:57:49 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <optional>
|
2021-09-20 15:33:19 -07:00
|
|
|
|
2019-08-24 06:57:49 -07:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/thread.h"
|
2021-09-20 15:33:19 -07:00
|
|
|
#include "input_common/input_engine.h"
|
2019-08-24 06:57:49 -07:00
|
|
|
|
|
|
|
namespace InputCommon::CemuhookUDP {
|
|
|
|
|
|
|
|
class Socket;
|
|
|
|
|
|
|
|
namespace Response {
|
|
|
|
struct PadData;
|
|
|
|
struct PortInfo;
|
2021-01-01 10:32:29 -08:00
|
|
|
struct TouchPad;
|
2021-01-02 20:04:50 -08:00
|
|
|
struct Version;
|
2019-08-24 06:57:49 -07:00
|
|
|
} // namespace Response
|
|
|
|
|
2020-09-04 19:35:42 -07:00
|
|
|
enum class PadTouch {
|
|
|
|
Click,
|
|
|
|
Undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UDPPadStatus {
|
2020-11-17 20:16:29 -08:00
|
|
|
std::string host{"127.0.0.1"};
|
|
|
|
u16 port{26760};
|
|
|
|
std::size_t pad_index{};
|
2020-09-04 19:35:42 -07:00
|
|
|
};
|
|
|
|
|
2019-08-24 06:57:49 -07:00
|
|
|
struct DeviceStatus {
|
|
|
|
std::mutex update_mutex;
|
|
|
|
|
|
|
|
// calibration data for scaling the device's touch area to 3ds
|
|
|
|
struct CalibrationData {
|
2019-11-02 23:04:28 -07:00
|
|
|
u16 min_x{};
|
|
|
|
u16 min_y{};
|
|
|
|
u16 max_x{};
|
|
|
|
u16 max_y{};
|
2019-08-24 06:57:49 -07:00
|
|
|
};
|
|
|
|
std::optional<CalibrationData> touch_calibration;
|
|
|
|
};
|
|
|
|
|
2021-09-20 15:33:19 -07:00
|
|
|
/**
|
|
|
|
* A button device factory representing a keyboard. It receives keyboard events and forward them
|
|
|
|
* to all button devices it created.
|
|
|
|
*/
|
|
|
|
class UDPClient final : public InputCommon::InputEngine {
|
2019-08-24 06:57:49 -07:00
|
|
|
public:
|
2021-09-20 15:33:19 -07:00
|
|
|
explicit UDPClient(const std::string& input_engine_);
|
|
|
|
~UDPClient();
|
2020-09-04 19:35:42 -07:00
|
|
|
|
2020-11-17 20:16:29 -08:00
|
|
|
void ReloadSockets();
|
2019-08-24 06:57:49 -07:00
|
|
|
|
|
|
|
private:
|
2021-03-30 10:42:05 -07:00
|
|
|
struct PadData {
|
2020-11-17 20:16:29 -08:00
|
|
|
std::size_t pad_index{};
|
2021-03-30 10:42:05 -07:00
|
|
|
bool connected{};
|
2020-09-04 19:35:42 -07:00
|
|
|
DeviceStatus status;
|
2020-11-17 20:16:29 -08:00
|
|
|
u64 packet_sequence{};
|
2020-09-04 19:35:42 -07:00
|
|
|
|
2021-03-30 10:42:05 -07:00
|
|
|
std::chrono::time_point<std::chrono::steady_clock> last_update;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ClientConnection {
|
|
|
|
ClientConnection();
|
|
|
|
~ClientConnection();
|
2021-10-25 10:53:14 -07:00
|
|
|
Common::UUID uuid{"7F000001"};
|
2021-03-30 10:42:05 -07:00
|
|
|
std::string host{"127.0.0.1"};
|
|
|
|
u16 port{26760};
|
|
|
|
s8 active{-1};
|
|
|
|
std::unique_ptr<Socket> socket;
|
|
|
|
std::thread thread;
|
2020-09-04 19:35:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// For shutting down, clear all data, join all threads, release usb
|
|
|
|
void Reset();
|
|
|
|
|
2020-11-17 20:16:29 -08:00
|
|
|
// Translates configuration to client number
|
2021-03-30 10:42:05 -07:00
|
|
|
std::size_t GetClientNumber(std::string_view host, u16 port) const;
|
2020-11-17 20:16:29 -08:00
|
|
|
|
2019-08-24 06:57:49 -07:00
|
|
|
void OnVersion(Response::Version);
|
|
|
|
void OnPortInfo(Response::PortInfo);
|
2020-11-17 20:16:29 -08:00
|
|
|
void OnPadData(Response::PadData, std::size_t client);
|
2021-03-30 10:42:05 -07:00
|
|
|
void StartCommunication(std::size_t client, const std::string& host, u16 port);
|
2021-09-20 15:33:19 -07:00
|
|
|
const PadIdentifier GetPadIdentifier(std::size_t pad_index) const;
|
2021-10-25 10:53:14 -07:00
|
|
|
const Common::UUID GetHostUUID(const std::string host) const;
|
2019-08-24 06:57:49 -07:00
|
|
|
|
2020-11-17 20:16:29 -08:00
|
|
|
// Allocate clients for 8 udp servers
|
2021-03-30 10:42:05 -07:00
|
|
|
static constexpr std::size_t MAX_UDP_CLIENTS = 8;
|
|
|
|
static constexpr std::size_t PADS_PER_CLIENT = 4;
|
|
|
|
std::array<PadData, MAX_UDP_CLIENTS * PADS_PER_CLIENT> pads{};
|
|
|
|
std::array<ClientConnection, MAX_UDP_CLIENTS> clients{};
|
2019-08-24 06:57:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An async job allowing configuration of the touchpad calibration.
|
|
|
|
class CalibrationConfigurationJob {
|
|
|
|
public:
|
|
|
|
enum class Status {
|
|
|
|
Initialized,
|
|
|
|
Ready,
|
|
|
|
Stage1Completed,
|
|
|
|
Completed,
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Constructs and starts the job with the specified parameter.
|
|
|
|
*
|
|
|
|
* @param status_callback Callback for job status updates
|
|
|
|
* @param data_callback Called when calibration data is ready
|
|
|
|
*/
|
2021-03-30 10:42:05 -07:00
|
|
|
explicit CalibrationConfigurationJob(const std::string& host, u16 port,
|
2021-02-27 07:26:33 -08:00
|
|
|
std::function<void(Status)> status_callback,
|
2019-08-24 06:57:49 -07:00
|
|
|
std::function<void(u16, u16, u16, u16)> data_callback);
|
|
|
|
~CalibrationConfigurationJob();
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Event complete_event;
|
|
|
|
};
|
|
|
|
|
2021-03-30 10:42:05 -07:00
|
|
|
void TestCommunication(const std::string& host, u16 port,
|
2020-10-16 03:22:26 -07:00
|
|
|
const std::function<void()>& success_callback,
|
|
|
|
const std::function<void()>& failure_callback);
|
2019-08-24 06:57:49 -07:00
|
|
|
|
|
|
|
} // namespace InputCommon::CemuhookUDP
|