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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <tuple>
|
|
|
|
#include "common/common_types.h"
|
2020-09-05 02:35:42 +00:00
|
|
|
#include "common/param_package.h"
|
2019-08-24 13:57:49 +00:00
|
|
|
#include "common/thread.h"
|
2020-09-05 02:35:42 +00:00
|
|
|
#include "common/threadsafe_queue.h"
|
2019-08-24 13:57:49 +00:00
|
|
|
#include "common/vector_math.h"
|
2020-09-03 00:59:34 +00:00
|
|
|
#include "core/frontend/input.h"
|
2020-09-05 02:35:42 +00:00
|
|
|
#include "input_common/motion_input.h"
|
2019-08-24 13:57:49 +00:00
|
|
|
|
|
|
|
namespace InputCommon::CemuhookUDP {
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
constexpr char DEFAULT_SRV[] = "127.0.0.1:26760";
|
2019-08-24 13:57:49 +00:00
|
|
|
|
|
|
|
class Socket;
|
|
|
|
|
|
|
|
namespace Response {
|
|
|
|
struct PadData;
|
|
|
|
struct PortInfo;
|
2021-01-01 18:32:29 +00:00
|
|
|
struct TouchPad;
|
2021-01-03 04:04:50 +00:00
|
|
|
struct Version;
|
2019-08-24 13:57:49 +00:00
|
|
|
} // namespace Response
|
|
|
|
|
2020-09-05 02:35:42 +00:00
|
|
|
enum class PadMotion {
|
|
|
|
GyroX,
|
|
|
|
GyroY,
|
|
|
|
GyroZ,
|
|
|
|
AccX,
|
|
|
|
AccY,
|
|
|
|
AccZ,
|
|
|
|
Undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class PadTouch {
|
|
|
|
Click,
|
|
|
|
Undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UDPPadStatus {
|
2020-11-18 04:16:29 +00:00
|
|
|
std::string host{"127.0.0.1"};
|
|
|
|
u16 port{26760};
|
|
|
|
std::size_t pad_index{};
|
2020-09-05 02:35:42 +00:00
|
|
|
PadMotion motion{PadMotion::Undefined};
|
|
|
|
f32 motion_value{0.0f};
|
|
|
|
};
|
|
|
|
|
2019-08-24 13:57:49 +00:00
|
|
|
struct DeviceStatus {
|
|
|
|
std::mutex update_mutex;
|
2020-09-03 00:59:34 +00:00
|
|
|
Input::MotionStatus motion_status;
|
2019-08-24 13:57:49 +00:00
|
|
|
std::tuple<float, float, bool> touch_status;
|
|
|
|
|
|
|
|
// calibration data for scaling the device's touch area to 3ds
|
|
|
|
struct CalibrationData {
|
2019-11-03 06:04:28 +00:00
|
|
|
u16 min_x{};
|
|
|
|
u16 min_y{};
|
|
|
|
u16 max_x{};
|
|
|
|
u16 max_y{};
|
2019-08-24 13:57:49 +00:00
|
|
|
};
|
|
|
|
std::optional<CalibrationData> touch_calibration;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Client {
|
|
|
|
public:
|
2020-09-05 02:35:42 +00:00
|
|
|
// Initialize the UDP client capture and read sequence
|
|
|
|
Client();
|
|
|
|
|
|
|
|
// Close and release the client
|
2019-08-24 13:57:49 +00:00
|
|
|
~Client();
|
2020-09-05 02:35:42 +00:00
|
|
|
|
|
|
|
// Used for polling
|
|
|
|
void BeginConfiguration();
|
|
|
|
void EndConfiguration();
|
|
|
|
|
|
|
|
std::vector<Common::ParamPackage> GetInputDevices() const;
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
bool DeviceConnected(std::size_t client) const;
|
|
|
|
void ReloadSockets();
|
2019-08-24 13:57:49 +00:00
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
Common::SPSCQueue<UDPPadStatus>& GetPadQueue();
|
|
|
|
const Common::SPSCQueue<UDPPadStatus>& GetPadQueue() const;
|
2020-09-05 02:35:42 +00:00
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad);
|
|
|
|
const DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad) const;
|
2020-09-05 02:35:42 +00:00
|
|
|
|
2021-01-01 18:32:29 +00:00
|
|
|
Input::TouchStatus& GetTouchState();
|
|
|
|
const Input::TouchStatus& GetTouchState() const;
|
|
|
|
|
2019-08-24 13:57:49 +00:00
|
|
|
private:
|
2020-09-05 02:35:42 +00:00
|
|
|
struct ClientData {
|
2021-02-09 22:36:29 +00:00
|
|
|
ClientData();
|
|
|
|
~ClientData();
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
std::string host{"127.0.0.1"};
|
|
|
|
u16 port{26760};
|
|
|
|
std::size_t pad_index{};
|
2020-09-05 02:35:42 +00:00
|
|
|
std::unique_ptr<Socket> socket;
|
|
|
|
DeviceStatus status;
|
|
|
|
std::thread thread;
|
2020-11-18 04:16:29 +00:00
|
|
|
u64 packet_sequence{};
|
|
|
|
s8 active{-1};
|
2020-09-05 02:35:42 +00:00
|
|
|
|
|
|
|
// Realtime values
|
|
|
|
// motion is initalized with PID values for drift correction on joycons
|
|
|
|
InputCommon::MotionInput motion{0.3f, 0.005f, 0.0f};
|
2020-11-18 04:16:29 +00:00
|
|
|
std::chrono::time_point<std::chrono::steady_clock> last_motion_update;
|
2020-09-05 02:35:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// For shutting down, clear all data, join all threads, release usb
|
|
|
|
void Reset();
|
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
// Translates configuration to client number
|
|
|
|
std::size_t GetClientNumber(std::string_view host, u16 port, std::size_t pad) const;
|
|
|
|
|
2019-08-24 13:57:49 +00:00
|
|
|
void OnVersion(Response::Version);
|
|
|
|
void OnPortInfo(Response::PortInfo);
|
2020-11-18 04:16:29 +00:00
|
|
|
void OnPadData(Response::PadData, std::size_t client);
|
2020-10-14 06:51:14 +00:00
|
|
|
void StartCommunication(std::size_t client, const std::string& host, u16 port,
|
2021-02-27 15:26:33 +00:00
|
|
|
std::size_t pad_index);
|
2020-09-05 02:35:42 +00:00
|
|
|
void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
|
2021-01-01 18:32:29 +00:00
|
|
|
const Common::Vec3<float>& gyro);
|
|
|
|
|
|
|
|
// Returns an unused finger id, if there is no fingers available std::nullopt will be
|
|
|
|
// returned
|
2021-01-03 04:04:50 +00:00
|
|
|
std::optional<std::size_t> GetUnusedFingerID() const;
|
2021-01-01 18:32:29 +00:00
|
|
|
|
|
|
|
// Merges and updates all touch inputs into the touch_status array
|
2021-01-03 04:04:50 +00:00
|
|
|
void UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id);
|
2020-09-05 02:35:42 +00:00
|
|
|
|
|
|
|
bool configuring = false;
|
2019-08-24 13:57:49 +00:00
|
|
|
|
2020-11-18 04:16:29 +00:00
|
|
|
// Allocate clients for 8 udp servers
|
2021-01-01 18:32:29 +00:00
|
|
|
static constexpr std::size_t MAX_UDP_CLIENTS = 4 * 8;
|
|
|
|
// Each client can have up 2 touch inputs
|
|
|
|
static constexpr std::size_t MAX_TOUCH_FINGERS = MAX_UDP_CLIENTS * 2;
|
|
|
|
std::array<ClientData, MAX_UDP_CLIENTS> clients{};
|
|
|
|
Common::SPSCQueue<UDPPadStatus> pad_queue{};
|
|
|
|
Input::TouchStatus touch_status{};
|
2021-01-03 04:04:50 +00:00
|
|
|
std::array<std::size_t, MAX_TOUCH_FINGERS> finger_id{};
|
2019-08-24 13:57:49 +00: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
|
|
|
|
*/
|
2020-10-14 06:51:14 +00:00
|
|
|
explicit CalibrationConfigurationJob(const std::string& host, u16 port, std::size_t pad_index,
|
2021-02-27 15:26:33 +00:00
|
|
|
std::function<void(Status)> status_callback,
|
2019-08-24 13:57:49 +00:00
|
|
|
std::function<void(u16, u16, u16, u16)> data_callback);
|
|
|
|
~CalibrationConfigurationJob();
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Event complete_event;
|
|
|
|
};
|
|
|
|
|
2021-02-27 15:26:33 +00:00
|
|
|
void TestCommunication(const std::string& host, u16 port, std::size_t pad_index,
|
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
|
|
|
|
|
|
|
} // namespace InputCommon::CemuhookUDP
|