emulated_console.h 5.8 KB

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