summaryrefslogtreecommitdiff
path: root/src/core/hid/emulated_devices.h
diff options
context:
space:
mode:
authorGravatar Feng Chen2021-12-18 13:57:14 +0800
committerGravatar GitHub2021-12-18 13:57:14 +0800
commite49184e6069a9d791d2df3c1958f5c4b1187e124 (patch)
treeb776caf722e0be0e680f67b0ad0842628162ef1c /src/core/hid/emulated_devices.h
parentImplement convert legacy to generic (diff)
parentMerge pull request #7570 from ameerj/favorites-expanded (diff)
downloadyuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.gz
yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.xz
yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.zip
Merge branch 'yuzu-emu:master' into convert_legacy
Diffstat (limited to 'src/core/hid/emulated_devices.h')
-rw-r--r--src/core/hid/emulated_devices.h210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/core/hid/emulated_devices.h b/src/core/hid/emulated_devices.h
new file mode 100644
index 000000000..790d3b411
--- /dev/null
+++ b/src/core/hid/emulated_devices.h
@@ -0,0 +1,210 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <unordered_map>
12
13#include "common/common_types.h"
14#include "common/input.h"
15#include "common/param_package.h"
16#include "common/settings.h"
17#include "core/hid/hid_types.h"
18
19namespace Core::HID {
20using KeyboardDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
21 Settings::NativeKeyboard::NumKeyboardKeys>;
22using KeyboardModifierDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
23 Settings::NativeKeyboard::NumKeyboardMods>;
24using MouseButtonDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
25 Settings::NativeMouseButton::NumMouseButtons>;
26using MouseAnalogDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
27 Settings::NativeMouseWheel::NumMouseWheels>;
28using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>;
29
30using MouseButtonParams =
31 std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
32
33using KeyboardValues =
34 std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
35using KeyboardModifierValues =
36 std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
37using MouseButtonValues =
38 std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
39using MouseAnalogValues =
40 std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>;
41using MouseStickValue = Common::Input::TouchStatus;
42
43struct MousePosition {
44 f32 x;
45 f32 y;
46};
47
48struct DeviceStatus {
49 // Data from input_common
50 KeyboardValues keyboard_values{};
51 KeyboardModifierValues keyboard_moddifier_values{};
52 MouseButtonValues mouse_button_values{};
53 MouseAnalogValues mouse_analog_values{};
54 MouseStickValue mouse_stick_value{};
55
56 // Data for HID serices
57 KeyboardKey keyboard_state{};
58 KeyboardModifier keyboard_moddifier_state{};
59 MouseButton mouse_button_state{};
60 MousePosition mouse_position_state{};
61 AnalogStickState mouse_wheel_state{};
62};
63
64enum class DeviceTriggerType {
65 Keyboard,
66 KeyboardModdifier,
67 Mouse,
68};
69
70struct InterfaceUpdateCallback {
71 std::function<void(DeviceTriggerType)> on_change;
72};
73
74class EmulatedDevices {
75public:
76 /**
77 * Contains all input data related to external devices that aren't necesarily a controller
78 * This includes devices such as the keyboard or mouse
79 */
80 explicit EmulatedDevices();
81 ~EmulatedDevices();
82
83 YUZU_NON_COPYABLE(EmulatedDevices);
84 YUZU_NON_MOVEABLE(EmulatedDevices);
85
86 /// Removes all callbacks created from input devices
87 void UnloadInput();
88
89 /**
90 * Sets the emulated devices into configuring mode
91 * This prevents the modification of the HID state of the emulated devices by input commands
92 */
93 void EnableConfiguration();
94
95 /// Returns the emulated devices into normal mode, allowing the modification of the HID state
96 void DisableConfiguration();
97
98 /// Returns true if the emulated device is in configuring mode
99 bool IsConfiguring() const;
100
101 /// Reload all input devices
102 void ReloadInput();
103
104 /// Overrides current mapped devices with the stored configuration and reloads all input devices
105 void ReloadFromSettings();
106
107 /// Saves the current mapped configuration
108 void SaveCurrentConfig();
109
110 /// Reverts any mapped changes made that weren't saved
111 void RestoreConfig();
112
113 /// Returns the latest status of button input from the keyboard with parameters
114 KeyboardValues GetKeyboardValues() const;
115
116 /// Returns the latest status of button input from the keyboard modifiers with parameters
117 KeyboardModifierValues GetKeyboardModdifierValues() const;
118
119 /// Returns the latest status of button input from the mouse with parameters
120 MouseButtonValues GetMouseButtonsValues() const;
121
122 /// Returns the latest status of button input from the keyboard
123 KeyboardKey GetKeyboard() const;
124
125 /// Returns the latest status of button input from the keyboard modifiers
126 KeyboardModifier GetKeyboardModifier() const;
127
128 /// Returns the latest status of button input from the mouse
129 MouseButton GetMouseButtons() const;
130
131 /// Returns the latest mouse coordinates
132 MousePosition GetMousePosition() const;
133
134 /// Returns the latest mouse wheel change
135 AnalogStickState GetMouseWheel() const;
136
137 /**
138 * Adds a callback to the list of events
139 * @param update_callback InterfaceUpdateCallback that will be triggered
140 * @return an unique key corresponding to the callback index in the list
141 */
142 int SetCallback(InterfaceUpdateCallback update_callback);
143
144 /**
145 * Removes a callback from the list stopping any future events to this object
146 * @param key Key corresponding to the callback index in the list
147 */
148 void DeleteCallback(int key);
149
150private:
151 /// Helps assigning a value to keyboard_state
152 void UpdateKey(std::size_t key_index, bool status);
153
154 /**
155 * Updates the touch status of the keyboard device
156 * @param callback A CallbackStatus containing the key status
157 * @param index key ID to be updated
158 */
159 void SetKeyboardButton(const Common::Input::CallbackStatus& callback, std::size_t index);
160
161 /**
162 * Updates the keyboard status of the keyboard device
163 * @param callback A CallbackStatus containing the modifier key status
164 * @param index modifier key ID to be updated
165 */
166 void SetKeyboardModifier(const Common::Input::CallbackStatus& callback, std::size_t index);
167
168 /**
169 * Updates the mouse button status of the mouse device
170 * @param callback A CallbackStatus containing the button status
171 * @param index Button ID to be updated
172 */
173 void SetMouseButton(const Common::Input::CallbackStatus& callback, std::size_t index);
174
175 /**
176 * Updates the mouse wheel status of the mouse device
177 * @param callback A CallbackStatus containing the wheel status
178 * @param index wheel ID to be updated
179 */
180 void SetMouseAnalog(const Common::Input::CallbackStatus& callback, std::size_t index);
181
182 /**
183 * Updates the mouse position status of the mouse device
184 * @param callback A CallbackStatus containing the position status
185 */
186 void SetMouseStick(const Common::Input::CallbackStatus& callback);
187
188 /**
189 * Triggers a callback that something has changed on the device status
190 * @param type Input type of the event to trigger
191 */
192 void TriggerOnChange(DeviceTriggerType type);
193
194 bool is_configuring{false};
195
196 KeyboardDevices keyboard_devices;
197 KeyboardModifierDevices keyboard_modifier_devices;
198 MouseButtonDevices mouse_button_devices;
199 MouseAnalogDevices mouse_analog_devices;
200 MouseStickDevice mouse_stick_device;
201
202 mutable std::mutex mutex;
203 std::unordered_map<int, InterfaceUpdateCallback> callback_list;
204 int last_callback_key = 0;
205
206 // Stores the current status of all external device input
207 DeviceStatus device_status;
208};
209
210} // namespace Core::HID