client.h 2.8 KB

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