summaryrefslogtreecommitdiff
path: root/src/input_common/input_engine.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/input_engine.h')
-rw-r--r--src/input_common/input_engine.h229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h
new file mode 100644
index 000000000..390581c94
--- /dev/null
+++ b/src/input_common/input_engine.h
@@ -0,0 +1,229 @@
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 <functional>
8#include <mutex>
9#include <unordered_map>
10
11#include "common/common_types.h"
12#include "common/input.h"
13#include "common/param_package.h"
14#include "common/uuid.h"
15#include "input_common/main.h"
16
17// Pad Identifier of data source
18struct PadIdentifier {
19 Common::UUID guid{};
20 std::size_t port{};
21 std::size_t pad{};
22
23 friend constexpr bool operator==(const PadIdentifier&, const PadIdentifier&) = default;
24};
25
26// Basic motion data containing data from the sensors and a timestamp in microseconds
27struct BasicMotion {
28 float gyro_x{};
29 float gyro_y{};
30 float gyro_z{};
31 float accel_x{};
32 float accel_y{};
33 float accel_z{};
34 u64 delta_timestamp{};
35};
36
37// Stages of a battery charge
38enum class BatteryLevel {
39 Empty,
40 Critical,
41 Low,
42 Medium,
43 Full,
44 Charging,
45};
46
47// Types of input that are stored in the engine
48enum class EngineInputType {
49 None,
50 Button,
51 HatButton,
52 Analog,
53 Motion,
54 Battery,
55};
56
57namespace std {
58// Hash used to create lists from PadIdentifier data
59template <>
60struct hash<PadIdentifier> {
61 size_t operator()(const PadIdentifier& pad_id) const noexcept {
62 u64 hash_value = pad_id.guid.uuid[1] ^ pad_id.guid.uuid[0];
63 hash_value ^= (static_cast<u64>(pad_id.port) << 32);
64 hash_value ^= static_cast<u64>(pad_id.pad);
65 return static_cast<size_t>(hash_value);
66 }
67};
68
69} // namespace std
70
71namespace InputCommon {
72
73// Data from the engine and device needed for creating a ParamPackage
74struct MappingData {
75 std::string engine{};
76 PadIdentifier pad{};
77 EngineInputType type{};
78 int index{};
79 bool button_value{};
80 std::string hat_name{};
81 f32 axis_value{};
82 BasicMotion motion_value{};
83};
84
85// Triggered if data changed on the controller
86struct UpdateCallback {
87 std::function<void()> on_change;
88};
89
90// Triggered if data changed on the controller and the engine is on configuring mode
91struct MappingCallback {
92 std::function<void(MappingData)> on_data;
93};
94
95// Input Identifier of data source
96struct InputIdentifier {
97 PadIdentifier identifier;
98 EngineInputType type;
99 int index;
100 UpdateCallback callback;
101};
102
103class InputEngine {
104public:
105 explicit InputEngine(std::string input_engine_) : input_engine{std::move(input_engine_)} {}
106
107 virtual ~InputEngine() = default;
108
109 // Enable configuring mode for mapping
110 void BeginConfiguration();
111
112 // Disable configuring mode for mapping
113 void EndConfiguration();
114
115 // Sets a led pattern for a controller
116 virtual void SetLeds([[maybe_unused]] const PadIdentifier& identifier,
117 [[maybe_unused]] const Common::Input::LedStatus& led_status) {}
118
119 // Sets rumble to a controller
120 virtual Common::Input::VibrationError SetRumble(
121 [[maybe_unused]] const PadIdentifier& identifier,
122 [[maybe_unused]] const Common::Input::VibrationStatus& vibration) {
123 return Common::Input::VibrationError::NotSupported;
124 }
125
126 // Sets polling mode to a controller
127 virtual Common::Input::PollingError SetPollingMode(
128 [[maybe_unused]] const PadIdentifier& identifier,
129 [[maybe_unused]] const Common::Input::PollingMode vibration) {
130 return Common::Input::PollingError::NotSupported;
131 }
132
133 // Returns the engine name
134 [[nodiscard]] const std::string& GetEngineName() const;
135
136 /// Used for automapping features
137 virtual std::vector<Common::ParamPackage> GetInputDevices() const {
138 return {};
139 }
140
141 /// Retrieves the button mappings for the given device
142 virtual ButtonMapping GetButtonMappingForDevice(
143 [[maybe_unused]] const Common::ParamPackage& params) {
144 return {};
145 }
146
147 /// Retrieves the analog mappings for the given device
148 virtual AnalogMapping GetAnalogMappingForDevice(
149 [[maybe_unused]] const Common::ParamPackage& params) {
150 return {};
151 }
152
153 /// Retrieves the motion mappings for the given device
154 virtual MotionMapping GetMotionMappingForDevice(
155 [[maybe_unused]] const Common::ParamPackage& params) {
156 return {};
157 }
158
159 /// Retrieves the name of the given input.
160 virtual Common::Input::ButtonNames GetUIName(
161 [[maybe_unused]] const Common::ParamPackage& params) const {
162 return Common::Input::ButtonNames::Engine;
163 }
164
165 /// Retrieves the index number of the given hat button direction
166 virtual u8 GetHatButtonId([[maybe_unused]] const std::string& direction_name) const {
167 return 0;
168 }
169
170 void PreSetController(const PadIdentifier& identifier);
171 void PreSetButton(const PadIdentifier& identifier, int button);
172 void PreSetHatButton(const PadIdentifier& identifier, int button);
173 void PreSetAxis(const PadIdentifier& identifier, int axis);
174 void PreSetMotion(const PadIdentifier& identifier, int motion);
175 void ResetButtonState();
176 void ResetAnalogState();
177
178 bool GetButton(const PadIdentifier& identifier, int button) const;
179 bool GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const;
180 f32 GetAxis(const PadIdentifier& identifier, int axis) const;
181 BatteryLevel GetBattery(const PadIdentifier& identifier) const;
182 BasicMotion GetMotion(const PadIdentifier& identifier, int motion) const;
183
184 int SetCallback(InputIdentifier input_identifier);
185 void SetMappingCallback(MappingCallback callback);
186 void DeleteCallback(int key);
187
188protected:
189 void SetButton(const PadIdentifier& identifier, int button, bool value);
190 void SetHatButton(const PadIdentifier& identifier, int button, u8 value);
191 void SetAxis(const PadIdentifier& identifier, int axis, f32 value);
192 void SetBattery(const PadIdentifier& identifier, BatteryLevel value);
193 void SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value);
194
195 virtual std::string GetHatButtonName([[maybe_unused]] u8 direction_value) const {
196 return "Unknown";
197 }
198
199private:
200 struct ControllerData {
201 std::unordered_map<int, bool> buttons;
202 std::unordered_map<int, u8> hat_buttons;
203 std::unordered_map<int, float> axes;
204 std::unordered_map<int, BasicMotion> motions;
205 BatteryLevel battery{};
206 };
207
208 void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value);
209 void TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value);
210 void TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value);
211 void TriggerOnBatteryChange(const PadIdentifier& identifier, BatteryLevel value);
212 void TriggerOnMotionChange(const PadIdentifier& identifier, int motion,
213 const BasicMotion& value);
214
215 bool IsInputIdentifierEqual(const InputIdentifier& input_identifier,
216 const PadIdentifier& identifier, EngineInputType type,
217 int index) const;
218
219 mutable std::mutex mutex;
220 mutable std::mutex mutex_callback;
221 bool configuring{false};
222 const std::string input_engine;
223 int last_callback_key = 0;
224 std::unordered_map<PadIdentifier, ControllerData> controller_list;
225 std::unordered_map<int, InputIdentifier> callback_list;
226 MappingCallback mapping_callback;
227};
228
229} // namespace InputCommon