udp_client.h 5.3 KB

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