summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/udp_client.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/drivers/udp_client.h')
-rw-r--r--src/input_common/drivers/udp_client.h127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/input_common/drivers/udp_client.h b/src/input_common/drivers/udp_client.h
new file mode 100644
index 000000000..58b2e921d
--- /dev/null
+++ b/src/input_common/drivers/udp_client.h
@@ -0,0 +1,127 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included
4
5#pragma once
6
7#include <optional>
8
9#include "common/common_types.h"
10#include "common/thread.h"
11#include "input_common/input_engine.h"
12
13namespace InputCommon::CemuhookUDP {
14
15class Socket;
16
17namespace Response {
18struct PadData;
19struct PortInfo;
20struct TouchPad;
21struct Version;
22} // namespace Response
23
24enum class PadTouch {
25 Click,
26 Undefined,
27};
28
29struct UDPPadStatus {
30 std::string host{"127.0.0.1"};
31 u16 port{26760};
32 std::size_t pad_index{};
33};
34
35struct DeviceStatus {
36 std::mutex update_mutex;
37
38 // calibration data for scaling the device's touch area to 3ds
39 struct CalibrationData {
40 u16 min_x{};
41 u16 min_y{};
42 u16 max_x{};
43 u16 max_y{};
44 };
45 std::optional<CalibrationData> touch_calibration;
46};
47
48/**
49 * A button device factory representing a keyboard. It receives keyboard events and forward them
50 * to all button devices it created.
51 */
52class UDPClient final : public InputCommon::InputEngine {
53public:
54 explicit UDPClient(const std::string& input_engine_);
55 ~UDPClient();
56
57 void ReloadSockets();
58
59private:
60 struct PadData {
61 std::size_t pad_index{};
62 bool connected{};
63 DeviceStatus status;
64 u64 packet_sequence{};
65
66 std::chrono::time_point<std::chrono::steady_clock> last_update;
67 };
68
69 struct ClientConnection {
70 ClientConnection();
71 ~ClientConnection();
72 std::string host{"127.0.0.1"};
73 u16 port{26760};
74 s8 active{-1};
75 std::unique_ptr<Socket> socket;
76 std::thread thread;
77 };
78
79 // For shutting down, clear all data, join all threads, release usb
80 void Reset();
81
82 // Translates configuration to client number
83 std::size_t GetClientNumber(std::string_view host, u16 port) const;
84
85 void OnVersion(Response::Version);
86 void OnPortInfo(Response::PortInfo);
87 void OnPadData(Response::PadData, std::size_t client);
88 void StartCommunication(std::size_t client, const std::string& host, u16 port);
89 const PadIdentifier GetPadIdentifier(std::size_t pad_index) const;
90
91 // Allocate clients for 8 udp servers
92 static constexpr std::size_t MAX_UDP_CLIENTS = 8;
93 static constexpr std::size_t PADS_PER_CLIENT = 4;
94 std::array<PadData, MAX_UDP_CLIENTS * PADS_PER_CLIENT> pads{};
95 std::array<ClientConnection, MAX_UDP_CLIENTS> clients{};
96};
97
98/// An async job allowing configuration of the touchpad calibration.
99class CalibrationConfigurationJob {
100public:
101 enum class Status {
102 Initialized,
103 Ready,
104 Stage1Completed,
105 Completed,
106 };
107 /**
108 * Constructs and starts the job with the specified parameter.
109 *
110 * @param status_callback Callback for job status updates
111 * @param data_callback Called when calibration data is ready
112 */
113 explicit CalibrationConfigurationJob(const std::string& host, u16 port,
114 std::function<void(Status)> status_callback,
115 std::function<void(u16, u16, u16, u16)> data_callback);
116 ~CalibrationConfigurationJob();
117 void Stop();
118
119private:
120 Common::Event complete_event;
121};
122
123void TestCommunication(const std::string& host, u16 port,
124 const std::function<void()>& success_callback,
125 const std::function<void()>& failure_callback);
126
127} // namespace InputCommon::CemuhookUDP