summaryrefslogtreecommitdiff
path: root/src/input_common/input_engine.h
diff options
context:
space:
mode:
authorGravatar german772021-09-20 16:34:05 -0500
committerGravatar Narr the Reg2021-11-24 20:30:22 -0600
commitea7b1fbc673e40d5e51f2d185ebe8542741164fa (patch)
treef5bbd57f06b71d7fdd34c373d4b0032154b55d42 /src/input_common/input_engine.h
parentcore/hid: Move motion_input, create input converter and hid_types (diff)
downloadyuzu-ea7b1fbc673e40d5e51f2d185ebe8542741164fa.tar.gz
yuzu-ea7b1fbc673e40d5e51f2d185ebe8542741164fa.tar.xz
yuzu-ea7b1fbc673e40d5e51f2d185ebe8542741164fa.zip
input_common: Create input_engine
Diffstat (limited to 'src/input_common/input_engine.h')
-rw-r--r--src/input_common/input_engine.h224
1 files changed, 224 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..86a8e00d8
--- /dev/null
+++ b/src/input_common/input_engine.h
@@ -0,0 +1,224 @@
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 microsecons
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 std::size_t index;
100 UpdateCallback callback;
101};
102
103class InputEngine {
104public:
105 explicit InputEngine(const std::string& input_engine_) : input_engine(input_engine_) {
106 callback_list.clear();
107 }
108
109 virtual ~InputEngine() = default;
110
111 // Enable configuring mode for mapping
112 void BeginConfiguration();
113
114 // Disable configuring mode for mapping
115 void EndConfiguration();
116
117 // Sets rumble to a controller
118 virtual bool SetRumble([[maybe_unused]] const PadIdentifier& identifier,
119 [[maybe_unused]] const Input::VibrationStatus vibration) {
120 return false;
121 }
122
123 // Sets a led pattern for a controller
124 virtual void SetLeds([[maybe_unused]] const PadIdentifier& identifier,
125 [[maybe_unused]] const Input::LedStatus led_status) {
126 return;
127 }
128
129 // Returns the engine name
130 [[nodiscard]] const std::string& GetEngineName() const;
131
132 /// Used for automapping features
133 virtual std::vector<Common::ParamPackage> GetInputDevices() const {
134 return {};
135 };
136
137 /// Retrieves the button mappings for the given device
138 virtual InputCommon::ButtonMapping GetButtonMappingForDevice(
139 [[maybe_unused]] const Common::ParamPackage& params) {
140 return {};
141 };
142
143 /// Retrieves the analog mappings for the given device
144 virtual InputCommon::AnalogMapping GetAnalogMappingForDevice(
145 [[maybe_unused]] const Common::ParamPackage& params) {
146 return {};
147 };
148
149 /// Retrieves the motion mappings for the given device
150 virtual InputCommon::MotionMapping GetMotionMappingForDevice(
151 [[maybe_unused]] const Common::ParamPackage& params) {
152 return {};
153 };
154
155 /// Retrieves the name of the given input.
156 virtual std::string GetUIName([[maybe_unused]] const Common::ParamPackage& params) const {
157 return GetEngineName();
158 };
159
160 /// Retrieves the index number of the given hat button direction
161 virtual u8 GetHatButtonId([[maybe_unused]] const std::string direction_name) const {
162 return 0;
163 };
164
165 void PreSetController(const PadIdentifier& identifier);
166 void PreSetButton(const PadIdentifier& identifier, int button);
167 void PreSetHatButton(const PadIdentifier& identifier, int button);
168 void PreSetAxis(const PadIdentifier& identifier, int axis);
169 void PreSetMotion(const PadIdentifier& identifier, int motion);
170 void ResetButtonState();
171 void ResetAnalogState();
172
173 bool GetButton(const PadIdentifier& identifier, int button) const;
174 bool GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const;
175 f32 GetAxis(const PadIdentifier& identifier, int axis) const;
176 BatteryLevel GetBattery(const PadIdentifier& identifier) const;
177 BasicMotion GetMotion(const PadIdentifier& identifier, int motion) const;
178
179 int SetCallback(InputIdentifier input_identifier);
180 void SetMappingCallback(MappingCallback callback);
181 void DeleteCallback(int key);
182
183protected:
184 void SetButton(const PadIdentifier& identifier, int button, bool value);
185 void SetHatButton(const PadIdentifier& identifier, int button, u8 value);
186 void SetAxis(const PadIdentifier& identifier, int axis, f32 value);
187 void SetBattery(const PadIdentifier& identifier, BatteryLevel value);
188 void SetMotion(const PadIdentifier& identifier, int motion, BasicMotion value);
189
190 virtual std::string GetHatButtonName([[maybe_unused]] u8 direction_value) const {
191 return "Unknown";
192 }
193
194private:
195 struct ControllerData {
196 std::unordered_map<int, bool> buttons;
197 std::unordered_map<int, u8> hat_buttons;
198 std::unordered_map<int, float> axes;
199 std::unordered_map<int, BasicMotion> motions;
200 BatteryLevel battery;
201 };
202
203 void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value);
204 void TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value);
205 void TriggerOnAxisChange(const PadIdentifier& identifier, int button, f32 value);
206 void TriggerOnBatteryChange(const PadIdentifier& identifier, BatteryLevel value);
207 void TriggerOnMotionChange(const PadIdentifier& identifier, int motion, BasicMotion value);
208
209 bool IsInputIdentifierEqual(const InputIdentifier& input_identifier,
210 const PadIdentifier& identifier, EngineInputType type,
211 std::size_t index) const;
212
213 mutable std::mutex mutex;
214 mutable std::mutex mutex_callback;
215 bool configuring{false};
216 bool is_callback_enabled{true};
217 const std::string input_engine;
218 int last_callback_key = 0;
219 std::unordered_map<PadIdentifier, ControllerData> controller_list;
220 std::unordered_map<int, InputIdentifier> callback_list;
221 MappingCallback mapping_callback;
222};
223
224} // namespace InputCommon