udp_client.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-FileCopyrightText: 2018 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <optional>
  5. #include "common/common_types.h"
  6. #include "common/thread.h"
  7. #include "input_common/input_engine.h"
  8. namespace InputCommon::CemuhookUDP {
  9. class Socket;
  10. namespace Response {
  11. enum class Battery : u8;
  12. struct PadData;
  13. struct PortInfo;
  14. struct TouchPad;
  15. struct Version;
  16. } // namespace Response
  17. enum class PadTouch {
  18. Click,
  19. Undefined,
  20. };
  21. struct UDPPadStatus {
  22. std::string host{"127.0.0.1"};
  23. u16 port{26760};
  24. std::size_t pad_index{};
  25. };
  26. struct DeviceStatus {
  27. std::mutex update_mutex;
  28. // calibration data for scaling the device's touch area to 3ds
  29. struct CalibrationData {
  30. u16 min_x{};
  31. u16 min_y{};
  32. u16 max_x{};
  33. u16 max_y{};
  34. };
  35. std::optional<CalibrationData> touch_calibration;
  36. };
  37. /**
  38. * A button device factory representing a keyboard. It receives keyboard events and forward them
  39. * to all button devices it created.
  40. */
  41. class UDPClient final : public InputEngine {
  42. public:
  43. explicit UDPClient(std::string input_engine_);
  44. ~UDPClient() override;
  45. void ReloadSockets();
  46. /// Used for automapping features
  47. std::vector<Common::ParamPackage> GetInputDevices() const override;
  48. ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
  49. AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
  50. MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override;
  51. Common::Input::ButtonNames GetUIName(const Common::ParamPackage& params) const override;
  52. bool IsStickInverted(const Common::ParamPackage& params) override;
  53. private:
  54. enum class PadButton {
  55. Undefined = 0x0000,
  56. Share = 0x0001,
  57. L3 = 0x0002,
  58. R3 = 0x0004,
  59. Options = 0x0008,
  60. Up = 0x0010,
  61. Right = 0x0020,
  62. Down = 0x0040,
  63. Left = 0x0080,
  64. L2 = 0x0100,
  65. R2 = 0x0200,
  66. L1 = 0x0400,
  67. R1 = 0x0800,
  68. Triangle = 0x1000,
  69. Circle = 0x2000,
  70. Cross = 0x4000,
  71. Square = 0x8000,
  72. Touch1 = 0x10000,
  73. Touch2 = 0x20000,
  74. Home = 0x40000,
  75. TouchHardPress = 0x80000,
  76. };
  77. enum class PadAxes : u8 {
  78. LeftStickX,
  79. LeftStickY,
  80. RightStickX,
  81. RightStickY,
  82. AnalogLeft,
  83. AnalogDown,
  84. AnalogRight,
  85. AnalogUp,
  86. AnalogSquare,
  87. AnalogCross,
  88. AnalogCircle,
  89. AnalogTriangle,
  90. AnalogR1,
  91. AnalogL1,
  92. AnalogR2,
  93. AnalogL3,
  94. AnalogR3,
  95. Touch1X,
  96. Touch1Y,
  97. Touch2X,
  98. Touch2Y,
  99. Undefined,
  100. };
  101. struct PadData {
  102. std::size_t pad_index{};
  103. bool connected{};
  104. DeviceStatus status;
  105. u64 packet_sequence{};
  106. std::chrono::time_point<std::chrono::steady_clock> last_update;
  107. };
  108. struct ClientConnection {
  109. ClientConnection();
  110. ~ClientConnection();
  111. Common::UUID uuid{"00000000-0000-0000-0000-00007F000001"};
  112. std::string host{"127.0.0.1"};
  113. u16 port{26760};
  114. s8 active{-1};
  115. std::unique_ptr<Socket> socket;
  116. std::thread thread;
  117. };
  118. // For shutting down, clear all data, join all threads, release usb
  119. void Reset();
  120. // Translates configuration to client number
  121. std::size_t GetClientNumber(std::string_view host, u16 port) const;
  122. // Translates UDP battery level to input engine battery level
  123. Common::Input::BatteryLevel GetBatteryLevel(Response::Battery battery) const;
  124. void OnVersion(Response::Version);
  125. void OnPortInfo(Response::PortInfo);
  126. void OnPadData(Response::PadData, std::size_t client);
  127. void StartCommunication(std::size_t client, const std::string& host, u16 port);
  128. PadIdentifier GetPadIdentifier(std::size_t pad_index) const;
  129. Common::UUID GetHostUUID(const std::string& host) const;
  130. Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const;
  131. // Allocate clients for 8 udp servers
  132. static constexpr std::size_t MAX_UDP_CLIENTS = 8;
  133. static constexpr std::size_t PADS_PER_CLIENT = 4;
  134. std::array<PadData, MAX_UDP_CLIENTS * PADS_PER_CLIENT> pads{};
  135. std::array<ClientConnection, MAX_UDP_CLIENTS> clients{};
  136. };
  137. /// An async job allowing configuration of the touchpad calibration.
  138. class CalibrationConfigurationJob {
  139. public:
  140. enum class Status {
  141. Initialized,
  142. Ready,
  143. Stage1Completed,
  144. Completed,
  145. };
  146. /**
  147. * Constructs and starts the job with the specified parameter.
  148. *
  149. * @param status_callback Callback for job status updates
  150. * @param data_callback Called when calibration data is ready
  151. */
  152. explicit CalibrationConfigurationJob(const std::string& host, u16 port,
  153. std::function<void(Status)> status_callback,
  154. std::function<void(u16, u16, u16, u16)> data_callback);
  155. ~CalibrationConfigurationJob();
  156. void Stop();
  157. private:
  158. Common::Event complete_event;
  159. };
  160. void TestCommunication(const std::string& host, u16 port,
  161. const std::function<void()>& success_callback,
  162. const std::function<void()>& failure_callback);
  163. } // namespace InputCommon::CemuhookUDP