diff options
Diffstat (limited to 'src/input_common/udp')
| -rw-r--r-- | src/input_common/udp/client.cpp | 185 | ||||
| -rw-r--r-- | src/input_common/udp/client.h | 77 | ||||
| -rw-r--r-- | src/input_common/udp/udp.cpp | 177 | ||||
| -rw-r--r-- | src/input_common/udp/udp.h | 54 |
4 files changed, 373 insertions, 120 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index e63c73c4f..9d0b9f31d 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp | |||
| @@ -2,15 +2,13 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <chrono> | 5 | #include <chrono> |
| 8 | #include <cstring> | 6 | #include <cstring> |
| 9 | #include <functional> | 7 | #include <functional> |
| 10 | #include <thread> | 8 | #include <thread> |
| 11 | #include <boost/asio.hpp> | 9 | #include <boost/asio.hpp> |
| 12 | #include <boost/bind.hpp> | ||
| 13 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 11 | #include "core/settings.h" | ||
| 14 | #include "input_common/udp/client.h" | 12 | #include "input_common/udp/client.h" |
| 15 | #include "input_common/udp/protocol.h" | 13 | #include "input_common/udp/protocol.h" |
| 16 | 14 | ||
| @@ -132,21 +130,59 @@ static void SocketLoop(Socket* socket) { | |||
| 132 | socket->Loop(); | 130 | socket->Loop(); |
| 133 | } | 131 | } |
| 134 | 132 | ||
| 135 | Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port, | 133 | Client::Client() { |
| 136 | u8 pad_index, u32 client_id) | 134 | LOG_INFO(Input, "Udp Initialization started"); |
| 137 | : status(std::move(status)) { | 135 | for (std::size_t client = 0; client < clients.size(); client++) { |
| 138 | StartCommunication(host, port, pad_index, client_id); | 136 | u8 pad = client % 4; |
| 137 | StartCommunication(client, Settings::values.udp_input_address, | ||
| 138 | Settings::values.udp_input_port, pad, 24872); | ||
| 139 | // Set motion parameters | ||
| 140 | // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode | ||
| 141 | // Real HW values are unknown, 0.0001 is an approximate to Standard | ||
| 142 | clients[client].motion.SetGyroThreshold(0.0001f); | ||
| 143 | } | ||
| 139 | } | 144 | } |
| 140 | 145 | ||
| 141 | Client::~Client() { | 146 | Client::~Client() { |
| 142 | socket->Stop(); | 147 | Reset(); |
| 143 | thread.join(); | 148 | } |
| 149 | |||
| 150 | std::vector<Common::ParamPackage> Client::GetInputDevices() const { | ||
| 151 | std::vector<Common::ParamPackage> devices; | ||
| 152 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 153 | if (!DeviceConnected(client)) { | ||
| 154 | continue; | ||
| 155 | } | ||
| 156 | std::string name = fmt::format("UDP Controller {}", client); | ||
| 157 | devices.emplace_back(Common::ParamPackage{ | ||
| 158 | {"class", "cemuhookudp"}, | ||
| 159 | {"display", std::move(name)}, | ||
| 160 | {"port", std::to_string(client)}, | ||
| 161 | }); | ||
| 162 | } | ||
| 163 | return devices; | ||
| 144 | } | 164 | } |
| 145 | 165 | ||
| 166 | bool Client::DeviceConnected(std::size_t pad) const { | ||
| 167 | // Use last timestamp to detect if the socket has stopped sending data | ||
| 168 | const auto now = std::chrono::system_clock::now(); | ||
| 169 | u64 time_difference = | ||
| 170 | std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update) | ||
| 171 | .count(); | ||
| 172 | return time_difference < 1000 && clients[pad].active == 1; | ||
| 173 | } | ||
| 174 | |||
| 175 | void Client::ReloadUDPClient() { | ||
| 176 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 177 | ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); | ||
| 178 | } | ||
| 179 | } | ||
| 146 | void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { | 180 | void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { |
| 147 | socket->Stop(); | 181 | // client number must be determined from host / port and pad index |
| 148 | thread.join(); | 182 | std::size_t client = pad_index; |
| 149 | StartCommunication(host, port, pad_index, client_id); | 183 | clients[client].socket->Stop(); |
| 184 | clients[client].thread.join(); | ||
| 185 | StartCommunication(client, host, port, pad_index, client_id); | ||
| 150 | } | 186 | } |
| 151 | 187 | ||
| 152 | void Client::OnVersion(Response::Version data) { | 188 | void Client::OnVersion(Response::Version data) { |
| @@ -158,23 +194,35 @@ void Client::OnPortInfo(Response::PortInfo data) { | |||
| 158 | } | 194 | } |
| 159 | 195 | ||
| 160 | void Client::OnPadData(Response::PadData data) { | 196 | void Client::OnPadData(Response::PadData data) { |
| 197 | // client number must be determined from host / port and pad index | ||
| 198 | std::size_t client = data.info.id; | ||
| 161 | LOG_TRACE(Input, "PadData packet received"); | 199 | LOG_TRACE(Input, "PadData packet received"); |
| 162 | if (data.packet_counter <= packet_sequence) { | 200 | if (data.packet_counter == clients[client].packet_sequence) { |
| 163 | LOG_WARNING( | 201 | LOG_WARNING( |
| 164 | Input, | 202 | Input, |
| 165 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", | 203 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", |
| 166 | packet_sequence, data.packet_counter); | 204 | clients[client].packet_sequence, data.packet_counter); |
| 167 | return; | 205 | return; |
| 168 | } | 206 | } |
| 169 | packet_sequence = data.packet_counter; | 207 | clients[client].active = data.info.is_pad_active; |
| 170 | // TODO: Check how the Switch handles motions and how the CemuhookUDP motion | 208 | clients[client].packet_sequence = data.packet_counter; |
| 171 | // directions correspond to the ones of the Switch | 209 | const auto now = std::chrono::system_clock::now(); |
| 172 | Common::Vec3f accel = Common::MakeVec<float>(data.accel.x, data.accel.y, data.accel.z); | 210 | u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>( |
| 173 | Common::Vec3f gyro = Common::MakeVec<float>(data.gyro.pitch, data.gyro.yaw, data.gyro.roll); | 211 | now - clients[client].last_motion_update) |
| 174 | { | 212 | .count(); |
| 175 | std::lock_guard guard(status->update_mutex); | 213 | clients[client].last_motion_update = now; |
| 214 | Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; | ||
| 215 | clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); | ||
| 216 | // Gyroscope values are not it the correct scale from better joy. | ||
| 217 | // Dividing by 312 allows us to make one full turn = 1 turn | ||
| 218 | // This must be a configurable valued called sensitivity | ||
| 219 | clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f); | ||
| 220 | clients[client].motion.UpdateRotation(time_difference); | ||
| 221 | clients[client].motion.UpdateOrientation(time_difference); | ||
| 176 | 222 | ||
| 177 | status->motion_status = {accel, gyro}; | 223 | { |
| 224 | std::lock_guard guard(clients[client].status.update_mutex); | ||
| 225 | clients[client].status.motion_status = clients[client].motion.GetMotion(); | ||
| 178 | 226 | ||
| 179 | // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates | 227 | // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates |
| 180 | // between a simple "tap" and a hard press that causes the touch screen to click. | 228 | // between a simple "tap" and a hard press that causes the touch screen to click. |
| @@ -183,11 +231,11 @@ void Client::OnPadData(Response::PadData data) { | |||
| 183 | float x = 0; | 231 | float x = 0; |
| 184 | float y = 0; | 232 | float y = 0; |
| 185 | 233 | ||
| 186 | if (is_active && status->touch_calibration) { | 234 | if (is_active && clients[client].status.touch_calibration) { |
| 187 | const u16 min_x = status->touch_calibration->min_x; | 235 | const u16 min_x = clients[client].status.touch_calibration->min_x; |
| 188 | const u16 max_x = status->touch_calibration->max_x; | 236 | const u16 max_x = clients[client].status.touch_calibration->max_x; |
| 189 | const u16 min_y = status->touch_calibration->min_y; | 237 | const u16 min_y = clients[client].status.touch_calibration->min_y; |
| 190 | const u16 max_y = status->touch_calibration->max_y; | 238 | const u16 max_y = clients[client].status.touch_calibration->max_y; |
| 191 | 239 | ||
| 192 | x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / | 240 | x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / |
| 193 | static_cast<float>(max_x - min_x); | 241 | static_cast<float>(max_x - min_x); |
| @@ -195,17 +243,86 @@ void Client::OnPadData(Response::PadData data) { | |||
| 195 | static_cast<float>(max_y - min_y); | 243 | static_cast<float>(max_y - min_y); |
| 196 | } | 244 | } |
| 197 | 245 | ||
| 198 | status->touch_status = {x, y, is_active}; | 246 | clients[client].status.touch_status = {x, y, is_active}; |
| 247 | |||
| 248 | if (configuring) { | ||
| 249 | const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); | ||
| 250 | const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); | ||
| 251 | UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); | ||
| 252 | } | ||
| 199 | } | 253 | } |
| 200 | } | 254 | } |
| 201 | 255 | ||
| 202 | void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id) { | 256 | void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, |
| 257 | u32 client_id) { | ||
| 203 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, | 258 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, |
| 204 | [this](Response::PortInfo info) { OnPortInfo(info); }, | 259 | [this](Response::PortInfo info) { OnPortInfo(info); }, |
| 205 | [this](Response::PadData data) { OnPadData(data); }}; | 260 | [this](Response::PadData data) { OnPadData(data); }}; |
| 206 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); | 261 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); |
| 207 | socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); | 262 | clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); |
| 208 | thread = std::thread{SocketLoop, this->socket.get()}; | 263 | clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; |
| 264 | } | ||
| 265 | |||
| 266 | void Client::Reset() { | ||
| 267 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 268 | clients[client].socket->Stop(); | ||
| 269 | clients[client].thread.join(); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, | ||
| 274 | const Common::Vec3<float>& gyro, bool touch) { | ||
| 275 | if (gyro.Length() > 0.2f) { | ||
| 276 | LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", | ||
| 277 | client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); | ||
| 278 | } | ||
| 279 | UDPPadStatus pad; | ||
| 280 | if (touch) { | ||
| 281 | pad.touch = PadTouch::Click; | ||
| 282 | pad_queue[client].Push(pad); | ||
| 283 | } | ||
| 284 | for (size_t i = 0; i < 3; ++i) { | ||
| 285 | if (gyro[i] > 5.0f || gyro[i] < -5.0f) { | ||
| 286 | pad.motion = static_cast<PadMotion>(i); | ||
| 287 | pad.motion_value = gyro[i]; | ||
| 288 | pad_queue[client].Push(pad); | ||
| 289 | } | ||
| 290 | if (acc[i] > 1.75f || acc[i] < -1.75f) { | ||
| 291 | pad.motion = static_cast<PadMotion>(i + 3); | ||
| 292 | pad.motion_value = acc[i]; | ||
| 293 | pad_queue[client].Push(pad); | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | void Client::BeginConfiguration() { | ||
| 299 | for (auto& pq : pad_queue) { | ||
| 300 | pq.Clear(); | ||
| 301 | } | ||
| 302 | configuring = true; | ||
| 303 | } | ||
| 304 | |||
| 305 | void Client::EndConfiguration() { | ||
| 306 | for (auto& pq : pad_queue) { | ||
| 307 | pq.Clear(); | ||
| 308 | } | ||
| 309 | configuring = false; | ||
| 310 | } | ||
| 311 | |||
| 312 | DeviceStatus& Client::GetPadState(std::size_t pad) { | ||
| 313 | return clients[pad].status; | ||
| 314 | } | ||
| 315 | |||
| 316 | const DeviceStatus& Client::GetPadState(std::size_t pad) const { | ||
| 317 | return clients[pad].status; | ||
| 318 | } | ||
| 319 | |||
| 320 | std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() { | ||
| 321 | return pad_queue; | ||
| 322 | } | ||
| 323 | |||
| 324 | const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const { | ||
| 325 | return pad_queue; | ||
| 209 | } | 326 | } |
| 210 | 327 | ||
| 211 | void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, | 328 | void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, |
| @@ -225,8 +342,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie | |||
| 225 | } else { | 342 | } else { |
| 226 | failure_callback(); | 343 | failure_callback(); |
| 227 | } | 344 | } |
| 228 | }) | 345 | }).detach(); |
| 229 | .detach(); | ||
| 230 | } | 346 | } |
| 231 | 347 | ||
| 232 | CalibrationConfigurationJob::CalibrationConfigurationJob( | 348 | CalibrationConfigurationJob::CalibrationConfigurationJob( |
| @@ -280,8 +396,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( | |||
| 280 | complete_event.Wait(); | 396 | complete_event.Wait(); |
| 281 | socket.Stop(); | 397 | socket.Stop(); |
| 282 | worker_thread.join(); | 398 | worker_thread.join(); |
| 283 | }) | 399 | }).detach(); |
| 284 | .detach(); | ||
| 285 | } | 400 | } |
| 286 | 401 | ||
| 287 | CalibrationConfigurationJob::~CalibrationConfigurationJob() { | 402 | CalibrationConfigurationJob::~CalibrationConfigurationJob() { |
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index b8c654755..523dc6a7a 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h | |||
| @@ -12,8 +12,12 @@ | |||
| 12 | #include <thread> | 12 | #include <thread> |
| 13 | #include <tuple> | 13 | #include <tuple> |
| 14 | #include "common/common_types.h" | 14 | #include "common/common_types.h" |
| 15 | #include "common/param_package.h" | ||
| 15 | #include "common/thread.h" | 16 | #include "common/thread.h" |
| 17 | #include "common/threadsafe_queue.h" | ||
| 16 | #include "common/vector_math.h" | 18 | #include "common/vector_math.h" |
| 19 | #include "core/frontend/input.h" | ||
| 20 | #include "input_common/motion_input.h" | ||
| 17 | 21 | ||
| 18 | namespace InputCommon::CemuhookUDP { | 22 | namespace InputCommon::CemuhookUDP { |
| 19 | 23 | ||
| @@ -28,9 +32,30 @@ struct PortInfo; | |||
| 28 | struct Version; | 32 | struct Version; |
| 29 | } // namespace Response | 33 | } // namespace Response |
| 30 | 34 | ||
| 35 | enum class PadMotion { | ||
| 36 | GyroX, | ||
| 37 | GyroY, | ||
| 38 | GyroZ, | ||
| 39 | AccX, | ||
| 40 | AccY, | ||
| 41 | AccZ, | ||
| 42 | Undefined, | ||
| 43 | }; | ||
| 44 | |||
| 45 | enum class PadTouch { | ||
| 46 | Click, | ||
| 47 | Undefined, | ||
| 48 | }; | ||
| 49 | |||
| 50 | struct UDPPadStatus { | ||
| 51 | PadTouch touch{PadTouch::Undefined}; | ||
| 52 | PadMotion motion{PadMotion::Undefined}; | ||
| 53 | f32 motion_value{0.0f}; | ||
| 54 | }; | ||
| 55 | |||
| 31 | struct DeviceStatus { | 56 | struct DeviceStatus { |
| 32 | std::mutex update_mutex; | 57 | std::mutex update_mutex; |
| 33 | std::tuple<Common::Vec3<float>, Common::Vec3<float>> motion_status; | 58 | Input::MotionStatus motion_status; |
| 34 | std::tuple<float, float, bool> touch_status; | 59 | std::tuple<float, float, bool> touch_status; |
| 35 | 60 | ||
| 36 | // calibration data for scaling the device's touch area to 3ds | 61 | // calibration data for scaling the device's touch area to 3ds |
| @@ -45,22 +70,58 @@ struct DeviceStatus { | |||
| 45 | 70 | ||
| 46 | class Client { | 71 | class Client { |
| 47 | public: | 72 | public: |
| 48 | explicit Client(std::shared_ptr<DeviceStatus> status, const std::string& host = DEFAULT_ADDR, | 73 | // Initialize the UDP client capture and read sequence |
| 49 | u16 port = DEFAULT_PORT, u8 pad_index = 0, u32 client_id = 24872); | 74 | Client(); |
| 75 | |||
| 76 | // Close and release the client | ||
| 50 | ~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 ReloadUDPClient(); | ||
| 51 | void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, | 87 | void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, |
| 52 | u32 client_id = 24872); | 88 | u32 client_id = 24872); |
| 53 | 89 | ||
| 90 | std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue(); | ||
| 91 | const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const; | ||
| 92 | |||
| 93 | DeviceStatus& GetPadState(std::size_t pad); | ||
| 94 | const DeviceStatus& GetPadState(std::size_t pad) const; | ||
| 95 | |||
| 54 | private: | 96 | private: |
| 97 | struct ClientData { | ||
| 98 | std::unique_ptr<Socket> socket; | ||
| 99 | DeviceStatus status; | ||
| 100 | std::thread thread; | ||
| 101 | u64 packet_sequence = 0; | ||
| 102 | u8 active; | ||
| 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::system_clock> last_motion_update; | ||
| 108 | }; | ||
| 109 | |||
| 110 | // For shutting down, clear all data, join all threads, release usb | ||
| 111 | void Reset(); | ||
| 112 | |||
| 55 | void OnVersion(Response::Version); | 113 | void OnVersion(Response::Version); |
| 56 | void OnPortInfo(Response::PortInfo); | 114 | void OnPortInfo(Response::PortInfo); |
| 57 | void OnPadData(Response::PadData); | 115 | void OnPadData(Response::PadData); |
| 58 | void StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id); | 116 | void StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, |
| 117 | u32 client_id); | ||
| 118 | void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, | ||
| 119 | const Common::Vec3<float>& gyro, bool touch); | ||
| 120 | |||
| 121 | bool configuring = false; | ||
| 59 | 122 | ||
| 60 | std::unique_ptr<Socket> socket; | 123 | std::array<ClientData, 4> clients; |
| 61 | std::shared_ptr<DeviceStatus> status; | 124 | std::array<Common::SPSCQueue<UDPPadStatus>, 4> pad_queue; |
| 62 | std::thread thread; | ||
| 63 | u64 packet_sequence = 0; | ||
| 64 | }; | 125 | }; |
| 65 | 126 | ||
| 66 | /// An async job allowing configuration of the touchpad calibration. | 127 | /// An async job allowing configuration of the touchpad calibration. |
diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index 8c6ef1394..eba077a36 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp | |||
| @@ -1,99 +1,144 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | 1 | // Copyright 2020 yuzu Emulator Project |
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <atomic> | ||
| 6 | #include <list> | ||
| 5 | #include <mutex> | 7 | #include <mutex> |
| 6 | #include <optional> | 8 | #include <utility> |
| 7 | #include <tuple> | 9 | #include "common/assert.h" |
| 8 | 10 | #include "common/threadsafe_queue.h" | |
| 9 | #include "common/param_package.h" | ||
| 10 | #include "core/frontend/input.h" | ||
| 11 | #include "core/settings.h" | ||
| 12 | #include "input_common/udp/client.h" | 11 | #include "input_common/udp/client.h" |
| 13 | #include "input_common/udp/udp.h" | 12 | #include "input_common/udp/udp.h" |
| 14 | 13 | ||
| 15 | namespace InputCommon::CemuhookUDP { | 14 | namespace InputCommon { |
| 16 | 15 | ||
| 17 | class UDPTouchDevice final : public Input::TouchDevice { | 16 | class UDPMotion final : public Input::MotionDevice { |
| 18 | public: | 17 | public: |
| 19 | explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 18 | UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) |
| 20 | std::tuple<float, float, bool> GetStatus() const override { | 19 | : ip(ip_), port(port_), pad(pad_), client(client_) {} |
| 21 | std::lock_guard guard(status->update_mutex); | 20 | |
| 22 | return status->touch_status; | 21 | Input::MotionStatus GetStatus() const override { |
| 22 | return client->GetPadState(pad).motion_status; | ||
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | private: | 25 | private: |
| 26 | std::shared_ptr<DeviceStatus> status; | 26 | const std::string ip; |
| 27 | const int port; | ||
| 28 | const int pad; | ||
| 29 | CemuhookUDP::Client* client; | ||
| 30 | mutable std::mutex mutex; | ||
| 27 | }; | 31 | }; |
| 28 | 32 | ||
| 29 | class UDPMotionDevice final : public Input::MotionDevice { | 33 | /// A motion device factory that creates motion devices from JC Adapter |
| 30 | public: | 34 | UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_) |
| 31 | explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 35 | : client(std::move(client_)) {} |
| 32 | std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override { | 36 | |
| 33 | std::lock_guard guard(status->update_mutex); | 37 | /** |
| 34 | return status->motion_status; | 38 | * Creates motion device |
| 35 | } | 39 | * @param params contains parameters for creating the device: |
| 40 | * - "port": the nth jcpad on the adapter | ||
| 41 | */ | ||
| 42 | std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) { | ||
| 43 | const std::string ip = params.Get("ip", "127.0.0.1"); | ||
| 44 | const int port = params.Get("port", 26760); | ||
| 45 | const int pad = params.Get("pad_index", 0); | ||
| 46 | |||
| 47 | return std::make_unique<UDPMotion>(ip, port, pad, client.get()); | ||
| 48 | } | ||
| 36 | 49 | ||
| 37 | private: | 50 | void UDPMotionFactory::BeginConfiguration() { |
| 38 | std::shared_ptr<DeviceStatus> status; | 51 | polling = true; |
| 39 | }; | 52 | client->BeginConfiguration(); |
| 53 | } | ||
| 40 | 54 | ||
| 41 | class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { | 55 | void UDPMotionFactory::EndConfiguration() { |
| 42 | public: | 56 | polling = false; |
| 43 | explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 57 | client->EndConfiguration(); |
| 44 | 58 | } | |
| 45 | std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override { | 59 | |
| 46 | { | 60 | Common::ParamPackage UDPMotionFactory::GetNextInput() { |
| 47 | std::lock_guard guard(status->update_mutex); | 61 | Common::ParamPackage params; |
| 48 | status->touch_calibration = DeviceStatus::CalibrationData{}; | 62 | CemuhookUDP::UDPPadStatus pad; |
| 49 | // These default values work well for DS4 but probably not other touch inputs | 63 | auto& queue = client->GetPadQueue(); |
| 50 | status->touch_calibration->min_x = params.Get("min_x", 100); | 64 | for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) { |
| 51 | status->touch_calibration->min_y = params.Get("min_y", 50); | 65 | while (queue[pad_number].Pop(pad)) { |
| 52 | status->touch_calibration->max_x = params.Get("max_x", 1800); | 66 | if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) { |
| 53 | status->touch_calibration->max_y = params.Get("max_y", 850); | 67 | continue; |
| 68 | } | ||
| 69 | params.Set("engine", "cemuhookudp"); | ||
| 70 | params.Set("ip", "127.0.0.1"); | ||
| 71 | params.Set("port", 26760); | ||
| 72 | params.Set("pad_index", static_cast<int>(pad_number)); | ||
| 73 | params.Set("motion", static_cast<u16>(pad.motion)); | ||
| 74 | return params; | ||
| 54 | } | 75 | } |
| 55 | return std::make_unique<UDPTouchDevice>(status); | ||
| 56 | } | 76 | } |
| 77 | return params; | ||
| 78 | } | ||
| 57 | 79 | ||
| 58 | private: | 80 | class UDPTouch final : public Input::TouchDevice { |
| 59 | std::shared_ptr<DeviceStatus> status; | ||
| 60 | }; | ||
| 61 | |||
| 62 | class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { | ||
| 63 | public: | 81 | public: |
| 64 | explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} | 82 | UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) |
| 83 | : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} | ||
| 65 | 84 | ||
| 66 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { | 85 | std::tuple<float, float, bool> GetStatus() const override { |
| 67 | return std::make_unique<UDPMotionDevice>(status); | 86 | return client->GetPadState(pad).touch_status; |
| 68 | } | 87 | } |
| 69 | 88 | ||
| 70 | private: | 89 | private: |
| 71 | std::shared_ptr<DeviceStatus> status; | 90 | const std::string ip; |
| 91 | const int port; | ||
| 92 | const int pad; | ||
| 93 | CemuhookUDP::Client* client; | ||
| 94 | mutable std::mutex mutex; | ||
| 72 | }; | 95 | }; |
| 73 | 96 | ||
| 74 | State::State() { | 97 | /// A motion device factory that creates motion devices from JC Adapter |
| 75 | auto status = std::make_shared<DeviceStatus>(); | 98 | UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_) |
| 76 | client = | 99 | : client(std::move(client_)) {} |
| 77 | std::make_unique<Client>(status, Settings::values.udp_input_address, | 100 | |
| 78 | Settings::values.udp_input_port, Settings::values.udp_pad_index); | 101 | /** |
| 79 | 102 | * Creates motion device | |
| 80 | Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", | 103 | * @param params contains parameters for creating the device: |
| 81 | std::make_shared<UDPTouchFactory>(status)); | 104 | * - "port": the nth jcpad on the adapter |
| 82 | Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", | 105 | */ |
| 83 | std::make_shared<UDPMotionFactory>(status)); | 106 | std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) { |
| 107 | const std::string ip = params.Get("ip", "127.0.0.1"); | ||
| 108 | const int port = params.Get("port", 26760); | ||
| 109 | const int pad = params.Get("pad_index", 0); | ||
| 110 | |||
| 111 | return std::make_unique<UDPTouch>(ip, port, pad, client.get()); | ||
| 84 | } | 112 | } |
| 85 | 113 | ||
| 86 | State::~State() { | 114 | void UDPTouchFactory::BeginConfiguration() { |
| 87 | Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp"); | 115 | polling = true; |
| 88 | Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp"); | 116 | client->BeginConfiguration(); |
| 89 | } | 117 | } |
| 90 | 118 | ||
| 91 | void State::ReloadUDPClient() { | 119 | void UDPTouchFactory::EndConfiguration() { |
| 92 | client->ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, | 120 | polling = false; |
| 93 | Settings::values.udp_pad_index); | 121 | client->EndConfiguration(); |
| 94 | } | 122 | } |
| 95 | 123 | ||
| 96 | std::unique_ptr<State> Init() { | 124 | Common::ParamPackage UDPTouchFactory::GetNextInput() { |
| 97 | return std::make_unique<State>(); | 125 | Common::ParamPackage params; |
| 126 | CemuhookUDP::UDPPadStatus pad; | ||
| 127 | auto& queue = client->GetPadQueue(); | ||
| 128 | for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) { | ||
| 129 | while (queue[pad_number].Pop(pad)) { | ||
| 130 | if (pad.touch == CemuhookUDP::PadTouch::Undefined) { | ||
| 131 | continue; | ||
| 132 | } | ||
| 133 | params.Set("engine", "cemuhookudp"); | ||
| 134 | params.Set("ip", "127.0.0.1"); | ||
| 135 | params.Set("port", 26760); | ||
| 136 | params.Set("pad_index", static_cast<int>(pad_number)); | ||
| 137 | params.Set("touch", static_cast<u16>(pad.touch)); | ||
| 138 | return params; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | return params; | ||
| 98 | } | 142 | } |
| 99 | } // namespace InputCommon::CemuhookUDP | 143 | |
| 144 | } // namespace InputCommon | ||
diff --git a/src/input_common/udp/udp.h b/src/input_common/udp/udp.h index 4f83f0441..ea3fd4175 100644 --- a/src/input_common/udp/udp.h +++ b/src/input_common/udp/udp.h | |||
| @@ -1,25 +1,57 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | 1 | // Copyright 2020 yuzu Emulator Project |
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include "core/frontend/input.h" | ||
| 9 | #include "input_common/udp/client.h" | ||
| 8 | 10 | ||
| 9 | namespace InputCommon::CemuhookUDP { | 11 | namespace InputCommon { |
| 10 | 12 | ||
| 11 | class Client; | 13 | /// A motion device factory that creates motion devices from udp clients |
| 12 | 14 | class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { | |
| 13 | class State { | ||
| 14 | public: | 15 | public: |
| 15 | State(); | 16 | explicit UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_); |
| 16 | ~State(); | 17 | |
| 17 | void ReloadUDPClient(); | 18 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override; |
| 19 | |||
| 20 | Common::ParamPackage GetNextInput(); | ||
| 21 | |||
| 22 | /// For device input configuration/polling | ||
| 23 | void BeginConfiguration(); | ||
| 24 | void EndConfiguration(); | ||
| 25 | |||
| 26 | bool IsPolling() const { | ||
| 27 | return polling; | ||
| 28 | } | ||
| 18 | 29 | ||
| 19 | private: | 30 | private: |
| 20 | std::unique_ptr<Client> client; | 31 | std::shared_ptr<CemuhookUDP::Client> client; |
| 32 | bool polling = false; | ||
| 21 | }; | 33 | }; |
| 22 | 34 | ||
| 23 | std::unique_ptr<State> Init(); | 35 | /// A touch device factory that creates touch devices from udp clients |
| 36 | class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { | ||
| 37 | public: | ||
| 38 | explicit UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_); | ||
| 39 | |||
| 40 | std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override; | ||
| 41 | |||
| 42 | Common::ParamPackage GetNextInput(); | ||
| 43 | |||
| 44 | /// For device input configuration/polling | ||
| 45 | void BeginConfiguration(); | ||
| 46 | void EndConfiguration(); | ||
| 47 | |||
| 48 | bool IsPolling() const { | ||
| 49 | return polling; | ||
| 50 | } | ||
| 51 | |||
| 52 | private: | ||
| 53 | std::shared_ptr<CemuhookUDP::Client> client; | ||
| 54 | bool polling = false; | ||
| 55 | }; | ||
| 24 | 56 | ||
| 25 | } // namespace InputCommon::CemuhookUDP | 57 | } // namespace InputCommon |