diff options
Diffstat (limited to 'src/input_common/udp/client.h')
| -rw-r--r-- | src/input_common/udp/client.h | 183 |
1 files changed, 0 insertions, 183 deletions
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h deleted file mode 100644 index 380f9bb76..000000000 --- a/src/input_common/udp/client.h +++ /dev/null | |||
| @@ -1,183 +0,0 @@ | |||
| 1 | // Copyright 2018 Citra 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 <functional> | ||
| 8 | #include <memory> | ||
| 9 | #include <mutex> | ||
| 10 | #include <optional> | ||
| 11 | #include <string> | ||
| 12 | #include <thread> | ||
| 13 | #include <tuple> | ||
| 14 | #include "common/common_types.h" | ||
| 15 | #include "common/param_package.h" | ||
| 16 | #include "common/thread.h" | ||
| 17 | #include "common/threadsafe_queue.h" | ||
| 18 | #include "common/vector_math.h" | ||
| 19 | #include "core/frontend/input.h" | ||
| 20 | #include "input_common/motion_input.h" | ||
| 21 | |||
| 22 | namespace InputCommon::CemuhookUDP { | ||
| 23 | |||
| 24 | class Socket; | ||
| 25 | |||
| 26 | namespace Response { | ||
| 27 | struct PadData; | ||
| 28 | struct PortInfo; | ||
| 29 | struct TouchPad; | ||
| 30 | struct Version; | ||
| 31 | } // namespace Response | ||
| 32 | |||
| 33 | enum class PadMotion { | ||
| 34 | GyroX, | ||
| 35 | GyroY, | ||
| 36 | GyroZ, | ||
| 37 | AccX, | ||
| 38 | AccY, | ||
| 39 | AccZ, | ||
| 40 | Undefined, | ||
| 41 | }; | ||
| 42 | |||
| 43 | enum class PadTouch { | ||
| 44 | Click, | ||
| 45 | Undefined, | ||
| 46 | }; | ||
| 47 | |||
| 48 | struct UDPPadStatus { | ||
| 49 | std::string host{"127.0.0.1"}; | ||
| 50 | u16 port{26760}; | ||
| 51 | std::size_t pad_index{}; | ||
| 52 | PadMotion motion{PadMotion::Undefined}; | ||
| 53 | f32 motion_value{0.0f}; | ||
| 54 | }; | ||
| 55 | |||
| 56 | struct DeviceStatus { | ||
| 57 | std::mutex update_mutex; | ||
| 58 | Input::MotionStatus motion_status; | ||
| 59 | std::tuple<float, float, bool> touch_status; | ||
| 60 | |||
| 61 | // calibration data for scaling the device's touch area to 3ds | ||
| 62 | struct CalibrationData { | ||
| 63 | u16 min_x{}; | ||
| 64 | u16 min_y{}; | ||
| 65 | u16 max_x{}; | ||
| 66 | u16 max_y{}; | ||
| 67 | }; | ||
| 68 | std::optional<CalibrationData> touch_calibration; | ||
| 69 | }; | ||
| 70 | |||
| 71 | class Client { | ||
| 72 | public: | ||
| 73 | // Initialize the UDP client capture and read sequence | ||
| 74 | Client(); | ||
| 75 | |||
| 76 | // Close and release the client | ||
| 77 | ~Client(); | ||
| 78 | |||
| 79 | // Used for polling | ||
| 80 | void BeginConfiguration(); | ||
| 81 | void EndConfiguration(); | ||
| 82 | |||
| 83 | std::vector<Common::ParamPackage> GetInputDevices() const; | ||
| 84 | |||
| 85 | bool DeviceConnected(std::size_t pad) const; | ||
| 86 | void ReloadSockets(); | ||
| 87 | |||
| 88 | Common::SPSCQueue<UDPPadStatus>& GetPadQueue(); | ||
| 89 | const Common::SPSCQueue<UDPPadStatus>& GetPadQueue() const; | ||
| 90 | |||
| 91 | DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad); | ||
| 92 | const DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad) const; | ||
| 93 | |||
| 94 | Input::TouchStatus& GetTouchState(); | ||
| 95 | const Input::TouchStatus& GetTouchState() const; | ||
| 96 | |||
| 97 | private: | ||
| 98 | struct PadData { | ||
| 99 | std::size_t pad_index{}; | ||
| 100 | bool connected{}; | ||
| 101 | DeviceStatus status; | ||
| 102 | u64 packet_sequence{}; | ||
| 103 | |||
| 104 | // Realtime values | ||
| 105 | // motion is initalized with PID values for drift correction on joycons | ||
| 106 | InputCommon::MotionInput motion{0.3f, 0.005f, 0.0f}; | ||
| 107 | std::chrono::time_point<std::chrono::steady_clock> last_update; | ||
| 108 | }; | ||
| 109 | |||
| 110 | struct ClientConnection { | ||
| 111 | ClientConnection(); | ||
| 112 | ~ClientConnection(); | ||
| 113 | std::string host{"127.0.0.1"}; | ||
| 114 | u16 port{26760}; | ||
| 115 | s8 active{-1}; | ||
| 116 | std::unique_ptr<Socket> socket; | ||
| 117 | std::thread thread; | ||
| 118 | }; | ||
| 119 | |||
| 120 | // For shutting down, clear all data, join all threads, release usb | ||
| 121 | void Reset(); | ||
| 122 | |||
| 123 | // Translates configuration to client number | ||
| 124 | std::size_t GetClientNumber(std::string_view host, u16 port) const; | ||
| 125 | |||
| 126 | void OnVersion(Response::Version); | ||
| 127 | void OnPortInfo(Response::PortInfo); | ||
| 128 | void OnPadData(Response::PadData, std::size_t client); | ||
| 129 | void StartCommunication(std::size_t client, const std::string& host, u16 port); | ||
| 130 | void UpdateYuzuSettings(std::size_t client, std::size_t pad_index, | ||
| 131 | const Common::Vec3<float>& acc, const Common::Vec3<float>& gyro); | ||
| 132 | |||
| 133 | // Returns an unused finger id, if there is no fingers available std::nullopt will be | ||
| 134 | // returned | ||
| 135 | std::optional<std::size_t> GetUnusedFingerID() const; | ||
| 136 | |||
| 137 | // Merges and updates all touch inputs into the touch_status array | ||
| 138 | void UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id); | ||
| 139 | |||
| 140 | bool configuring = false; | ||
| 141 | |||
| 142 | // Allocate clients for 8 udp servers | ||
| 143 | static constexpr std::size_t MAX_UDP_CLIENTS = 8; | ||
| 144 | static constexpr std::size_t PADS_PER_CLIENT = 4; | ||
| 145 | // Each client can have up 2 touch inputs | ||
| 146 | static constexpr std::size_t MAX_TOUCH_FINGERS = MAX_UDP_CLIENTS * 2; | ||
| 147 | std::array<PadData, MAX_UDP_CLIENTS * PADS_PER_CLIENT> pads{}; | ||
| 148 | std::array<ClientConnection, MAX_UDP_CLIENTS> clients{}; | ||
| 149 | Common::SPSCQueue<UDPPadStatus> pad_queue{}; | ||
| 150 | Input::TouchStatus touch_status{}; | ||
| 151 | std::array<std::size_t, MAX_TOUCH_FINGERS> finger_id{}; | ||
| 152 | }; | ||
| 153 | |||
| 154 | /// An async job allowing configuration of the touchpad calibration. | ||
| 155 | class CalibrationConfigurationJob { | ||
| 156 | public: | ||
| 157 | enum class Status { | ||
| 158 | Initialized, | ||
| 159 | Ready, | ||
| 160 | Stage1Completed, | ||
| 161 | Completed, | ||
| 162 | }; | ||
| 163 | /** | ||
| 164 | * Constructs and starts the job with the specified parameter. | ||
| 165 | * | ||
| 166 | * @param status_callback Callback for job status updates | ||
| 167 | * @param data_callback Called when calibration data is ready | ||
| 168 | */ | ||
| 169 | explicit CalibrationConfigurationJob(const std::string& host, u16 port, | ||
| 170 | std::function<void(Status)> status_callback, | ||
| 171 | std::function<void(u16, u16, u16, u16)> data_callback); | ||
| 172 | ~CalibrationConfigurationJob(); | ||
| 173 | void Stop(); | ||
| 174 | |||
| 175 | private: | ||
| 176 | Common::Event complete_event; | ||
| 177 | }; | ||
| 178 | |||
| 179 | void TestCommunication(const std::string& host, u16 port, | ||
| 180 | const std::function<void()>& success_callback, | ||
| 181 | const std::function<void()>& failure_callback); | ||
| 182 | |||
| 183 | } // namespace InputCommon::CemuhookUDP | ||