diff options
Diffstat (limited to 'src/input_common/drivers/udp_client.h')
| -rw-r--r-- | src/input_common/drivers/udp_client.h | 185 |
1 files changed, 185 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..1adc947c4 --- /dev/null +++ b/src/input_common/drivers/udp_client.h | |||
| @@ -0,0 +1,185 @@ | |||
| 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 <optional> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "common/thread.h" | ||
| 11 | #include "input_common/input_engine.h" | ||
| 12 | |||
| 13 | namespace InputCommon::CemuhookUDP { | ||
| 14 | |||
| 15 | class Socket; | ||
| 16 | |||
| 17 | namespace Response { | ||
| 18 | struct PadData; | ||
| 19 | struct PortInfo; | ||
| 20 | struct TouchPad; | ||
| 21 | struct Version; | ||
| 22 | } // namespace Response | ||
| 23 | |||
| 24 | enum class PadTouch { | ||
| 25 | Click, | ||
| 26 | Undefined, | ||
| 27 | }; | ||
| 28 | |||
| 29 | struct UDPPadStatus { | ||
| 30 | std::string host{"127.0.0.1"}; | ||
| 31 | u16 port{26760}; | ||
| 32 | std::size_t pad_index{}; | ||
| 33 | }; | ||
| 34 | |||
| 35 | struct 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 | */ | ||
| 52 | class UDPClient final : public InputEngine { | ||
| 53 | public: | ||
| 54 | explicit UDPClient(std::string input_engine_); | ||
| 55 | ~UDPClient() override; | ||
| 56 | |||
| 57 | void ReloadSockets(); | ||
| 58 | |||
| 59 | /// Used for automapping features | ||
| 60 | std::vector<Common::ParamPackage> GetInputDevices() const override; | ||
| 61 | ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override; | ||
| 62 | AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override; | ||
| 63 | MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override; | ||
| 64 | Common::Input::ButtonNames GetUIName(const Common::ParamPackage& params) const override; | ||
| 65 | |||
| 66 | private: | ||
| 67 | enum class PadButton { | ||
| 68 | Undefined = 0x0000, | ||
| 69 | Share = 0x0001, | ||
| 70 | L3 = 0x0002, | ||
| 71 | R3 = 0x0004, | ||
| 72 | Options = 0x0008, | ||
| 73 | Up = 0x0010, | ||
| 74 | Right = 0x0020, | ||
| 75 | Down = 0x0040, | ||
| 76 | Left = 0x0080, | ||
| 77 | L2 = 0x0100, | ||
| 78 | R2 = 0x0200, | ||
| 79 | L1 = 0x0400, | ||
| 80 | R1 = 0x0800, | ||
| 81 | Triangle = 0x1000, | ||
| 82 | Circle = 0x2000, | ||
| 83 | Cross = 0x4000, | ||
| 84 | Square = 0x8000, | ||
| 85 | Touch1 = 0x10000, | ||
| 86 | touch2 = 0x20000, | ||
| 87 | }; | ||
| 88 | |||
| 89 | enum class PadAxes : u8 { | ||
| 90 | LeftStickX, | ||
| 91 | LeftStickY, | ||
| 92 | RightStickX, | ||
| 93 | RightStickY, | ||
| 94 | AnalogLeft, | ||
| 95 | AnalogDown, | ||
| 96 | AnalogRight, | ||
| 97 | AnalogUp, | ||
| 98 | AnalogSquare, | ||
| 99 | AnalogCross, | ||
| 100 | AnalogCircle, | ||
| 101 | AnalogTriangle, | ||
| 102 | AnalogR1, | ||
| 103 | AnalogL1, | ||
| 104 | AnalogR2, | ||
| 105 | AnalogL3, | ||
| 106 | AnalogR3, | ||
| 107 | Touch1X, | ||
| 108 | Touch1Y, | ||
| 109 | Touch2X, | ||
| 110 | Touch2Y, | ||
| 111 | Undefined, | ||
| 112 | }; | ||
| 113 | |||
| 114 | struct PadData { | ||
| 115 | std::size_t pad_index{}; | ||
| 116 | bool connected{}; | ||
| 117 | DeviceStatus status; | ||
| 118 | u64 packet_sequence{}; | ||
| 119 | |||
| 120 | std::chrono::time_point<std::chrono::steady_clock> last_update; | ||
| 121 | }; | ||
| 122 | |||
| 123 | struct ClientConnection { | ||
| 124 | ClientConnection(); | ||
| 125 | ~ClientConnection(); | ||
| 126 | Common::UUID uuid{"7F000001"}; | ||
| 127 | std::string host{"127.0.0.1"}; | ||
| 128 | u16 port{26760}; | ||
| 129 | s8 active{-1}; | ||
| 130 | std::unique_ptr<Socket> socket; | ||
| 131 | std::thread thread; | ||
| 132 | }; | ||
| 133 | |||
| 134 | // For shutting down, clear all data, join all threads, release usb | ||
| 135 | void Reset(); | ||
| 136 | |||
| 137 | // Translates configuration to client number | ||
| 138 | std::size_t GetClientNumber(std::string_view host, u16 port) const; | ||
| 139 | |||
| 140 | void OnVersion(Response::Version); | ||
| 141 | void OnPortInfo(Response::PortInfo); | ||
| 142 | void OnPadData(Response::PadData, std::size_t client); | ||
| 143 | void StartCommunication(std::size_t client, const std::string& host, u16 port); | ||
| 144 | const PadIdentifier GetPadIdentifier(std::size_t pad_index) const; | ||
| 145 | const Common::UUID GetHostUUID(const std::string host) const; | ||
| 146 | |||
| 147 | Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const; | ||
| 148 | |||
| 149 | // Allocate clients for 8 udp servers | ||
| 150 | static constexpr std::size_t MAX_UDP_CLIENTS = 8; | ||
| 151 | static constexpr std::size_t PADS_PER_CLIENT = 4; | ||
| 152 | std::array<PadData, MAX_UDP_CLIENTS * PADS_PER_CLIENT> pads{}; | ||
| 153 | std::array<ClientConnection, MAX_UDP_CLIENTS> clients{}; | ||
| 154 | }; | ||
| 155 | |||
| 156 | /// An async job allowing configuration of the touchpad calibration. | ||
| 157 | class CalibrationConfigurationJob { | ||
| 158 | public: | ||
| 159 | enum class Status { | ||
| 160 | Initialized, | ||
| 161 | Ready, | ||
| 162 | Stage1Completed, | ||
| 163 | Completed, | ||
| 164 | }; | ||
| 165 | /** | ||
| 166 | * Constructs and starts the job with the specified parameter. | ||
| 167 | * | ||
| 168 | * @param status_callback Callback for job status updates | ||
| 169 | * @param data_callback Called when calibration data is ready | ||
| 170 | */ | ||
| 171 | explicit CalibrationConfigurationJob(const std::string& host, u16 port, | ||
| 172 | std::function<void(Status)> status_callback, | ||
| 173 | std::function<void(u16, u16, u16, u16)> data_callback); | ||
| 174 | ~CalibrationConfigurationJob(); | ||
| 175 | void Stop(); | ||
| 176 | |||
| 177 | private: | ||
| 178 | Common::Event complete_event; | ||
| 179 | }; | ||
| 180 | |||
| 181 | void TestCommunication(const std::string& host, u16 port, | ||
| 182 | const std::function<void()>& success_callback, | ||
| 183 | const std::function<void()>& failure_callback); | ||
| 184 | |||
| 185 | } // namespace InputCommon::CemuhookUDP | ||