client.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <functional>
  6. #include <memory>
  7. #include <mutex>
  8. #include <optional>
  9. #include <string>
  10. #include <thread>
  11. #include <tuple>
  12. #include <vector>
  13. #include "common/common_types.h"
  14. #include "common/thread.h"
  15. #include "common/vector_math.h"
  16. namespace InputCommon::CemuhookUDP {
  17. constexpr u16 DEFAULT_PORT = 26760;
  18. constexpr char DEFAULT_ADDR[] = "127.0.0.1";
  19. class Socket;
  20. namespace Response {
  21. struct PadData;
  22. struct PortInfo;
  23. struct Version;
  24. } // namespace Response
  25. struct DeviceStatus {
  26. std::mutex update_mutex;
  27. std::tuple<Common::Vec3<float>, Common::Vec3<float>> motion_status;
  28. std::tuple<float, float, bool> touch_status;
  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. class Client {
  39. public:
  40. explicit Client(std::shared_ptr<DeviceStatus> status, const std::string& host = DEFAULT_ADDR,
  41. u16 port = DEFAULT_PORT, u8 pad_index = 0, u32 client_id = 24872);
  42. ~Client();
  43. void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0,
  44. u32 client_id = 24872);
  45. private:
  46. void OnVersion(Response::Version);
  47. void OnPortInfo(Response::PortInfo);
  48. void OnPadData(Response::PadData);
  49. void StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id);
  50. std::unique_ptr<Socket> socket;
  51. std::shared_ptr<DeviceStatus> status;
  52. std::thread thread;
  53. u64 packet_sequence = 0;
  54. };
  55. /// An async job allowing configuration of the touchpad calibration.
  56. class CalibrationConfigurationJob {
  57. public:
  58. enum class Status {
  59. Initialized,
  60. Ready,
  61. Stage1Completed,
  62. Completed,
  63. };
  64. /**
  65. * Constructs and starts the job with the specified parameter.
  66. *
  67. * @param status_callback Callback for job status updates
  68. * @param data_callback Called when calibration data is ready
  69. */
  70. explicit CalibrationConfigurationJob(const std::string& host, u16 port, u8 pad_index,
  71. u32 client_id, std::function<void(Status)> status_callback,
  72. std::function<void(u16, u16, u16, u16)> data_callback);
  73. ~CalibrationConfigurationJob();
  74. void Stop();
  75. private:
  76. Common::Event complete_event;
  77. };
  78. void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id,
  79. std::function<void()> success_callback,
  80. std::function<void()> failure_callback);
  81. } // namespace InputCommon::CemuhookUDP