emulated_devices.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <unordered_map>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "common/input.h"
  12. #include "common/param_package.h"
  13. #include "common/settings.h"
  14. #include "core/hid/hid_types.h"
  15. namespace Core::HID {
  16. using KeyboardDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
  17. Settings::NativeKeyboard::NumKeyboardKeys>;
  18. using KeyboardModifierDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
  19. Settings::NativeKeyboard::NumKeyboardMods>;
  20. using MouseButtonDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
  21. Settings::NativeMouseButton::NumMouseButtons>;
  22. using MouseWheelDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
  23. Settings::NativeMouseWheel::NumMouseWheels>;
  24. using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>;
  25. using MouseButtonParams =
  26. std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
  27. using KeyboardValues =
  28. std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
  29. using KeyboardModifierValues =
  30. std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
  31. using MouseButtonValues =
  32. std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
  33. using MouseWheelValues =
  34. std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>;
  35. using MouseStickValue = Common::Input::TouchStatus;
  36. struct MousePosition {
  37. f32 x;
  38. f32 y;
  39. };
  40. struct DeviceStatus {
  41. // Data from input_common
  42. KeyboardValues keyboard_values{};
  43. KeyboardModifierValues keyboard_moddifier_values{};
  44. MouseButtonValues mouse_button_values{};
  45. MouseWheelValues mouse_wheel_values{};
  46. MouseStickValue mouse_stick_value{};
  47. // Data for HID services
  48. KeyboardKey keyboard_state{};
  49. KeyboardModifier keyboard_moddifier_state{};
  50. MouseButton mouse_button_state{};
  51. MousePosition mouse_position_state{};
  52. AnalogStickState mouse_wheel_state{};
  53. };
  54. enum class DeviceTriggerType {
  55. Keyboard,
  56. KeyboardModdifier,
  57. Mouse,
  58. RingController,
  59. };
  60. struct InterfaceUpdateCallback {
  61. std::function<void(DeviceTriggerType)> on_change;
  62. };
  63. class EmulatedDevices {
  64. public:
  65. /**
  66. * Contains all input data related to external devices that aren't necessarily a controller
  67. * This includes devices such as the keyboard or mouse
  68. */
  69. explicit EmulatedDevices();
  70. ~EmulatedDevices();
  71. YUZU_NON_COPYABLE(EmulatedDevices);
  72. YUZU_NON_MOVEABLE(EmulatedDevices);
  73. /// Removes all callbacks created from input devices
  74. void UnloadInput();
  75. /**
  76. * Sets the emulated devices into configuring mode
  77. * This prevents the modification of the HID state of the emulated devices by input commands
  78. */
  79. void EnableConfiguration();
  80. /// Returns the emulated devices into normal mode, allowing the modification of the HID state
  81. void DisableConfiguration();
  82. /// Returns true if the emulated device is in configuring mode
  83. bool IsConfiguring() const;
  84. /// Reload all input devices
  85. void ReloadInput();
  86. /// Overrides current mapped devices with the stored configuration and reloads all input devices
  87. void ReloadFromSettings();
  88. /// Saves the current mapped configuration
  89. void SaveCurrentConfig();
  90. /// Reverts any mapped changes made that weren't saved
  91. void RestoreConfig();
  92. /// Returns the latest status of button input from the keyboard with parameters
  93. KeyboardValues GetKeyboardValues() const;
  94. /// Returns the latest status of button input from the keyboard modifiers with parameters
  95. KeyboardModifierValues GetKeyboardModdifierValues() const;
  96. /// Returns the latest status of button input from the mouse with parameters
  97. MouseButtonValues GetMouseButtonsValues() const;
  98. /// Returns the latest status of button input from the keyboard
  99. KeyboardKey GetKeyboard() const;
  100. /// Returns the latest status of button input from the keyboard modifiers
  101. KeyboardModifier GetKeyboardModifier() const;
  102. /// Returns the latest status of button input from the mouse
  103. MouseButton GetMouseButtons() const;
  104. /// Returns the latest mouse coordinates
  105. MousePosition GetMousePosition() const;
  106. /// Returns the latest mouse wheel change
  107. AnalogStickState GetMouseWheel() const;
  108. /**
  109. * Adds a callback to the list of events
  110. * @param update_callback InterfaceUpdateCallback that will be triggered
  111. * @return an unique key corresponding to the callback index in the list
  112. */
  113. int SetCallback(InterfaceUpdateCallback update_callback);
  114. /**
  115. * Removes a callback from the list stopping any future events to this object
  116. * @param key Key corresponding to the callback index in the list
  117. */
  118. void DeleteCallback(int key);
  119. private:
  120. /// Helps assigning a value to keyboard_state
  121. void UpdateKey(std::size_t key_index, bool status);
  122. /**
  123. * Updates the touch status of the keyboard device
  124. * @param callback A CallbackStatus containing the key status
  125. * @param index key ID to be updated
  126. */
  127. void SetKeyboardButton(const Common::Input::CallbackStatus& callback, std::size_t index);
  128. /**
  129. * Updates the keyboard status of the keyboard device
  130. * @param callback A CallbackStatus containing the modifier key status
  131. * @param index modifier key ID to be updated
  132. */
  133. void SetKeyboardModifier(const Common::Input::CallbackStatus& callback, std::size_t index);
  134. /**
  135. * Updates the mouse button status of the mouse device
  136. * @param callback A CallbackStatus containing the button status
  137. * @param index Button ID to be updated
  138. */
  139. void SetMouseButton(const Common::Input::CallbackStatus& callback, std::size_t index);
  140. /**
  141. * Updates the mouse wheel status of the mouse device
  142. * @param callback A CallbackStatus containing the wheel status
  143. * @param index wheel ID to be updated
  144. */
  145. void SetMouseWheel(const Common::Input::CallbackStatus& callback, std::size_t index);
  146. /**
  147. * Updates the mouse position status of the mouse device
  148. * @param callback A CallbackStatus containing the position status
  149. */
  150. void SetMousePosition(const Common::Input::CallbackStatus& callback);
  151. /**
  152. * Triggers a callback that something has changed on the device status
  153. * @param type Input type of the event to trigger
  154. */
  155. void TriggerOnChange(DeviceTriggerType type);
  156. bool is_configuring{false};
  157. KeyboardDevices keyboard_devices;
  158. KeyboardModifierDevices keyboard_modifier_devices;
  159. MouseButtonDevices mouse_button_devices;
  160. MouseWheelDevices mouse_wheel_devices;
  161. MouseStickDevice mouse_stick_device;
  162. mutable std::mutex mutex;
  163. mutable std::mutex callback_mutex;
  164. std::unordered_map<int, InterfaceUpdateCallback> callback_list;
  165. int last_callback_key = 0;
  166. // Stores the current status of all external device input
  167. DeviceStatus device_status;
  168. };
  169. } // namespace Core::HID