emulated_devices.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <array>
  6. #include <functional>
  7. #include <memory>
  8. #include <mutex>
  9. #include <unordered_map>
  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 MouseAnalogDevices = 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 MouseAnalogValues =
  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. MouseAnalogValues mouse_analog_values{};
  46. MouseStickValue mouse_stick_value{};
  47. // Data for HID serices
  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. };
  59. struct InterfaceUpdateCallback {
  60. std::function<void(DeviceTriggerType)> on_change;
  61. };
  62. class EmulatedDevices {
  63. public:
  64. /**
  65. * Contains all input data related to external devices that aren't necesarily a controller
  66. * This includes devices such as the keyboard or mouse
  67. */
  68. explicit EmulatedDevices();
  69. ~EmulatedDevices();
  70. YUZU_NON_COPYABLE(EmulatedDevices);
  71. YUZU_NON_MOVEABLE(EmulatedDevices);
  72. /// Removes all callbacks created from input devices
  73. void UnloadInput();
  74. /**
  75. * Sets the emulated devices into configuring mode
  76. * This prevents the modification of the HID state of the emulated devices by input commands
  77. */
  78. void EnableConfiguration();
  79. /// Returns the emulated devices into normal mode, allowing the modification of the HID state
  80. void DisableConfiguration();
  81. /// Returns true if the emulated device is in configuring mode
  82. bool IsConfiguring() const;
  83. /// Reload all input devices
  84. void ReloadInput();
  85. /// Overrides current mapped devices with the stored configuration and reloads all input devices
  86. void ReloadFromSettings();
  87. /// Saves the current mapped configuration
  88. void SaveCurrentConfig();
  89. /// Reverts any mapped changes made that weren't saved
  90. void RestoreConfig();
  91. /// Returns the latest status of button input from the keyboard with parameters
  92. KeyboardValues GetKeyboardValues() const;
  93. /// Returns the latest status of button input from the keyboard modifiers with parameters
  94. KeyboardModifierValues GetKeyboardModdifierValues() const;
  95. /// Returns the latest status of button input from the mouse with parameters
  96. MouseButtonValues GetMouseButtonsValues() const;
  97. /// Returns the latest status of button input from the keyboard
  98. KeyboardKey GetKeyboard() const;
  99. /// Returns the latest status of button input from the keyboard modifiers
  100. KeyboardModifier GetKeyboardModifier() const;
  101. /// Returns the latest status of button input from the mouse
  102. MouseButton GetMouseButtons() const;
  103. /// Returns the latest mouse coordinates
  104. MousePosition GetMousePosition() const;
  105. /// Returns the latest mouse wheel change
  106. AnalogStickState GetMouseWheel() const;
  107. /**
  108. * Adds a callback to the list of events
  109. * @param update_callback InterfaceUpdateCallback that will be triggered
  110. * @return an unique key corresponding to the callback index in the list
  111. */
  112. int SetCallback(InterfaceUpdateCallback update_callback);
  113. /**
  114. * Removes a callback from the list stopping any future events to this object
  115. * @param key Key corresponding to the callback index in the list
  116. */
  117. void DeleteCallback(int key);
  118. private:
  119. /// Helps assigning a value to keyboard_state
  120. void UpdateKey(std::size_t key_index, bool status);
  121. /**
  122. * Updates the touch status of the keyboard device
  123. * @param callback A CallbackStatus containing the key status
  124. * @param index key ID to be updated
  125. */
  126. void SetKeyboardButton(const Common::Input::CallbackStatus& callback, std::size_t index);
  127. /**
  128. * Updates the keyboard status of the keyboard device
  129. * @param callback A CallbackStatus containing the modifier key status
  130. * @param index modifier key ID to be updated
  131. */
  132. void SetKeyboardModifier(const Common::Input::CallbackStatus& callback, std::size_t index);
  133. /**
  134. * Updates the mouse button status of the mouse device
  135. * @param callback A CallbackStatus containing the button status
  136. * @param index Button ID to be updated
  137. */
  138. void SetMouseButton(const Common::Input::CallbackStatus& callback, std::size_t index);
  139. /**
  140. * Updates the mouse wheel status of the mouse device
  141. * @param callback A CallbackStatus containing the wheel status
  142. * @param index wheel ID to be updated
  143. */
  144. void SetMouseAnalog(const Common::Input::CallbackStatus& callback, std::size_t index);
  145. /**
  146. * Updates the mouse position status of the mouse device
  147. * @param callback A CallbackStatus containing the position status
  148. */
  149. void SetMouseStick(const Common::Input::CallbackStatus& callback);
  150. /**
  151. * Triggers a callback that something has changed on the device status
  152. * @param type Input type of the event to trigger
  153. */
  154. void TriggerOnChange(DeviceTriggerType type);
  155. bool is_configuring{false};
  156. KeyboardDevices keyboard_devices;
  157. KeyboardModifierDevices keyboard_modifier_devices;
  158. MouseButtonDevices mouse_button_devices;
  159. MouseAnalogDevices mouse_analog_devices;
  160. MouseStickDevice mouse_stick_device;
  161. mutable std::mutex mutex;
  162. std::unordered_map<int, InterfaceUpdateCallback> callback_list;
  163. int last_callback_key = 0;
  164. // Stores the current status of all external device input
  165. DeviceStatus device_status;
  166. };
  167. } // namespace Core::HID