emulated_console.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2021 yuzu 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 <mutex>
  7. #include <unordered_map>
  8. #include "common/input.h"
  9. #include "common/param_package.h"
  10. #include "common/point.h"
  11. #include "common/quaternion.h"
  12. #include "common/settings.h"
  13. #include "common/vector_math.h"
  14. #include "core/hid/hid_types.h"
  15. #include "core/hid/motion_input.h"
  16. namespace Core::HID {
  17. struct ConsoleMotionInfo {
  18. Input::MotionStatus raw_status;
  19. MotionInput emulated{};
  20. };
  21. using ConsoleMotionDevices = std::unique_ptr<Input::InputDevice>;
  22. using TouchDevices = std::array<std::unique_ptr<Input::InputDevice>, 16>;
  23. using ConsoleMotionParams = Common::ParamPackage;
  24. using TouchParams = std::array<Common::ParamPackage, 16>;
  25. using ConsoleMotionValues = ConsoleMotionInfo;
  26. using TouchValues = std::array<Input::TouchStatus, 16>;
  27. struct TouchFinger {
  28. u64_le last_touch{};
  29. Common::Point<float> position{};
  30. u32_le id{};
  31. bool pressed{};
  32. TouchAttribute attribute{};
  33. };
  34. // Contains all motion related data that is used on the services
  35. struct ConsoleMotion {
  36. bool is_at_rest{};
  37. Common::Vec3f accel{};
  38. Common::Vec3f gyro{};
  39. Common::Vec3f rotation{};
  40. std::array<Common::Vec3f, 3> orientation{};
  41. Common::Quaternion<f32> quaternion{};
  42. };
  43. using TouchFingerState = std::array<TouchFinger, 16>;
  44. struct ConsoleStatus {
  45. // Data from input_common
  46. ConsoleMotionValues motion_values{};
  47. TouchValues touch_values{};
  48. // Data for HID services
  49. ConsoleMotion motion_state{};
  50. TouchFingerState touch_state{};
  51. };
  52. enum class ConsoleTriggerType {
  53. Motion,
  54. Touch,
  55. All,
  56. };
  57. struct ConsoleUpdateCallback {
  58. std::function<void(ConsoleTriggerType)> on_change;
  59. };
  60. class EmulatedConsole {
  61. public:
  62. /**
  63. * Contains all input data related to the console like motion and touch input
  64. */
  65. EmulatedConsole();
  66. ~EmulatedConsole();
  67. YUZU_NON_COPYABLE(EmulatedConsole);
  68. YUZU_NON_MOVEABLE(EmulatedConsole);
  69. /// Removes all callbacks created from input devices
  70. void UnloadInput();
  71. /// Sets the emulated console into configuring mode. Locking all HID service events from being
  72. /// moddified
  73. void EnableConfiguration();
  74. /// Returns the emulated console to the normal behaivour
  75. void DisableConfiguration();
  76. /// Returns true if the emulated console is on configuring mode
  77. bool IsConfiguring() const;
  78. /// Reload all input devices
  79. void ReloadInput();
  80. /// Overrides current mapped devices with the stored configuration and reloads all input devices
  81. void ReloadFromSettings();
  82. /// Saves the current mapped configuration
  83. void SaveCurrentConfig();
  84. /// Reverts any mapped changes made that weren't saved
  85. void RestoreConfig();
  86. // Returns the current mapped motion device
  87. Common::ParamPackage GetMotionParam() const;
  88. /**
  89. * Updates the current mapped motion device
  90. * @param ParamPackage with controller data to be mapped
  91. */
  92. void SetMotionParam(Common::ParamPackage param);
  93. /// Returns the latest status of motion input from the console with parameters
  94. ConsoleMotionValues GetMotionValues() const;
  95. /// Returns the latest status of touch input from the console with parameters
  96. TouchValues GetTouchValues() const;
  97. /// Returns the latest status of motion input from the console
  98. ConsoleMotion GetMotion() const;
  99. /// Returns the latest status of touch input from the console
  100. TouchFingerState GetTouch() const;
  101. /**
  102. * Adds a callback to the list of events
  103. * @param ConsoleUpdateCallback that will be triggered
  104. * @return an unique key corresponding to the callback index in the list
  105. */
  106. int SetCallback(ConsoleUpdateCallback update_callback);
  107. /**
  108. * Removes a callback from the list stopping any future events to this object
  109. * @param Key corresponding to the callback index in the list
  110. */
  111. void DeleteCallback(int key);
  112. private:
  113. /**
  114. * Updates the motion status of the console
  115. * @param A CallbackStatus containing gyro and accelerometer data
  116. */
  117. void SetMotion(Input::CallbackStatus callback);
  118. /**
  119. * Updates the touch status of the console
  120. * @param callback: A CallbackStatus containing the touch position
  121. * @param index: Finger ID to be updated
  122. */
  123. void SetTouch(Input::CallbackStatus callback, std::size_t index);
  124. /**
  125. * Triggers a callback that something has changed on the console status
  126. * @param Input type of the event to trigger
  127. */
  128. void TriggerOnChange(ConsoleTriggerType type);
  129. bool is_configuring{false};
  130. f32 motion_sensitivity{0.01f};
  131. ConsoleMotionParams motion_params;
  132. TouchParams touch_params;
  133. ConsoleMotionDevices motion_devices;
  134. TouchDevices touch_devices;
  135. mutable std::mutex mutex;
  136. std::unordered_map<int, ConsoleUpdateCallback> callback_list;
  137. int last_callback_key = 0;
  138. // Stores the current status of all console input
  139. ConsoleStatus console;
  140. };
  141. } // namespace Core::HID