emulated_devices.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/settings.h"
  11. #include "core/hid/hid_types.h"
  12. #include "core/hid/motion_input.h"
  13. namespace Core::HID {
  14. using KeyboardDevices =
  15. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeKeyboard::NumKeyboardKeys>;
  16. using KeyboardModifierDevices =
  17. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeKeyboard::NumKeyboardMods>;
  18. using MouseButtonDevices =
  19. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeMouseButton::NumMouseButtons>;
  20. using MouseButtonParams =
  21. std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
  22. using KeyboardValues = std::array<Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
  23. using KeyboardModifierValues =
  24. std::array<Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
  25. using MouseButtonValues =
  26. std::array<Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
  27. struct MousePosition {
  28. s32 x;
  29. s32 y;
  30. s32 delta_wheel_x;
  31. s32 delta_wheel_y;
  32. };
  33. struct DeviceStatus {
  34. // Data from input_common
  35. KeyboardValues keyboard_values{};
  36. KeyboardModifierValues keyboard_moddifier_values{};
  37. MouseButtonValues mouse_button_values{};
  38. // Data for HID serices
  39. KeyboardKey keyboard_state{};
  40. KeyboardModifier keyboard_moddifier_state{};
  41. MouseButton mouse_button_state{};
  42. MousePosition mouse_position_state{};
  43. };
  44. enum class DeviceTriggerType {
  45. Keyboard,
  46. KeyboardModdifier,
  47. Mouse,
  48. };
  49. struct InterfaceUpdateCallback {
  50. std::function<void(DeviceTriggerType)> on_change;
  51. };
  52. class EmulatedDevices {
  53. public:
  54. /**
  55. * Contains all input data related to external devices that aren't necesarily a controller
  56. * like keyboard and mouse
  57. */
  58. EmulatedDevices();
  59. ~EmulatedDevices();
  60. YUZU_NON_COPYABLE(EmulatedDevices);
  61. YUZU_NON_MOVEABLE(EmulatedDevices);
  62. /// Removes all callbacks created from input devices
  63. void UnloadInput();
  64. /// Sets the emulated console into configuring mode. Locking all HID service events from being
  65. /// moddified
  66. void EnableConfiguration();
  67. /// Returns the emulated console to the normal behaivour
  68. void DisableConfiguration();
  69. /// Returns true if the emulated device is on configuring mode
  70. bool IsConfiguring() const;
  71. /// Reload all input devices
  72. void ReloadInput();
  73. /// Overrides current mapped devices with the stored configuration and reloads all input devices
  74. void ReloadFromSettings();
  75. /// Saves the current mapped configuration
  76. void SaveCurrentConfig();
  77. /// Reverts any mapped changes made that weren't saved
  78. void RestoreConfig();
  79. /// Returns the current mapped motion device
  80. Common::ParamPackage GetMouseButtonParam(std::size_t index) const;
  81. /**
  82. * Updates the current mapped mouse button device
  83. * @param ParamPackage with controller data to be mapped
  84. */
  85. void SetMouseButtonParam(std::size_t index, Common::ParamPackage param);
  86. /// Returns the latest status of button input from the keyboard with parameters
  87. KeyboardValues GetKeyboardValues() const;
  88. /// Returns the latest status of button input from the keyboard modifiers with parameters
  89. KeyboardModifierValues GetKeyboardModdifierValues() const;
  90. /// Returns the latest status of button input from the mouse with parameters
  91. MouseButtonValues GetMouseButtonsValues() const;
  92. /// Returns the latest status of button input from the keyboard
  93. KeyboardKey GetKeyboard() const;
  94. /// Returns the latest status of button input from the keyboard modifiers
  95. KeyboardModifier GetKeyboardModifier() const;
  96. /// Returns the latest status of button input from the mouse
  97. MouseButton GetMouseButtons() const;
  98. /// Returns the latest mouse coordinates
  99. MousePosition GetMousePosition() const;
  100. /**
  101. * Adds a callback to the list of events
  102. * @param ConsoleUpdateCallback that will be triggered
  103. * @return an unique key corresponding to the callback index in the list
  104. */
  105. int SetCallback(InterfaceUpdateCallback update_callback);
  106. /**
  107. * Removes a callback from the list stopping any future events to this object
  108. * @param Key corresponding to the callback index in the list
  109. */
  110. void DeleteCallback(int key);
  111. private:
  112. /**
  113. * Updates the touch status of the console
  114. * @param callback: A CallbackStatus containing the key status
  115. * @param index: key ID to be updated
  116. */
  117. void SetKeyboardButton(Input::CallbackStatus callback, std::size_t index);
  118. /**
  119. * Updates the touch status of the console
  120. * @param callback: A CallbackStatus containing the modifier key status
  121. * @param index: modifier key ID to be updated
  122. */
  123. void SetKeyboardModifier(Input::CallbackStatus callback, std::size_t index);
  124. /**
  125. * Updates the touch status of the console
  126. * @param callback: A CallbackStatus containing the button status
  127. * @param index: Button ID of the to be updated
  128. */
  129. void SetMouseButton(Input::CallbackStatus callback, std::size_t index);
  130. /**
  131. * Triggers a callback that something has changed on the device status
  132. * @param Input type of the event to trigger
  133. */
  134. void TriggerOnChange(DeviceTriggerType type);
  135. bool is_configuring{false};
  136. MouseButtonParams mouse_button_params;
  137. KeyboardDevices keyboard_devices;
  138. KeyboardModifierDevices keyboard_modifier_devices;
  139. MouseButtonDevices mouse_button_devices;
  140. mutable std::mutex mutex;
  141. std::unordered_map<int, InterfaceUpdateCallback> callback_list;
  142. int last_callback_key = 0;
  143. // Stores the current status of all external device input
  144. DeviceStatus device_status;
  145. };
  146. } // namespace Core::HID