summaryrefslogtreecommitdiff
path: root/src/core/hid/emulated_controller.h
diff options
context:
space:
mode:
authorGravatar german772021-09-20 19:43:16 -0500
committerGravatar Narr the Reg2021-11-24 20:30:23 -0600
commitc3f54ff2329d79bdbb273678b5123cf0b1cd090c (patch)
tree671542346e2f692b2cef8dc4da6ccdb0b9e21dc2 /src/core/hid/emulated_controller.h
parentyuzu_cmd: Use new input (diff)
downloadyuzu-c3f54ff2329d79bdbb273678b5123cf0b1cd090c.tar.gz
yuzu-c3f54ff2329d79bdbb273678b5123cf0b1cd090c.tar.xz
yuzu-c3f54ff2329d79bdbb273678b5123cf0b1cd090c.zip
core/hid: Add emulated controllers
Diffstat (limited to 'src/core/hid/emulated_controller.h')
-rw-r--r--src/core/hid/emulated_controller.h232
1 files changed, 232 insertions, 0 deletions
diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h
new file mode 100644
index 000000000..94db9b00b
--- /dev/null
+++ b/src/core/hid/emulated_controller.h
@@ -0,0 +1,232 @@
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/input.h"
12#include "common/param_package.h"
13#include "common/point.h"
14#include "common/quaternion.h"
15#include "common/settings.h"
16#include "common/vector_math.h"
17#include "core/hid/hid_types.h"
18#include "core/hid/motion_input.h"
19
20namespace Core::HID {
21
22struct ControllerMotionInfo {
23 Input::MotionStatus raw_status;
24 MotionInput emulated{};
25};
26
27using ButtonDevices =
28 std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeButton::NumButtons>;
29using StickDevices =
30 std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
31using ControllerMotionDevices =
32 std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeMotion::NumMotions>;
33using TriggerDevices =
34 std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
35using BatteryDevices = std::array<std::unique_ptr<Input::InputDevice>, 2>;
36
37using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
38using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
39using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
40using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
41using BatteryParams = std::array<Common::ParamPackage, 2>;
42
43using ButtonValues = std::array<Input::ButtonStatus, Settings::NativeButton::NumButtons>;
44using SticksValues = std::array<Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
45using TriggerValues = std::array<Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
46using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
47using ColorValues = std::array<Input::BodyColorStatus, 3>;
48using BatteryValues = std::array<Input::BatteryStatus, 3>;
49using VibrationValues = std::array<Input::VibrationStatus, 2>;
50
51struct AnalogSticks {
52 AnalogStickState left;
53 AnalogStickState right;
54};
55
56struct ControllerColors {
57 NpadControllerColor fullkey;
58 NpadControllerColor left;
59 NpadControllerColor right;
60};
61
62struct BatteryLevelState {
63 NpadPowerInfo dual;
64 NpadPowerInfo left;
65 NpadPowerInfo right;
66};
67
68struct ControllerMotion {
69 bool is_at_rest;
70 Common::Vec3f accel{};
71 Common::Vec3f gyro{};
72 Common::Vec3f rotation{};
73 std::array<Common::Vec3f, 3> orientation{};
74};
75
76using MotionState = std::array<ControllerMotion, 2>;
77
78struct ControllerStatus {
79 // Data from input_common
80 ButtonValues button_values{};
81 SticksValues stick_values{};
82 ControllerMotionValues motion_values{};
83 TriggerValues trigger_values{};
84 ColorValues color_values{};
85 BatteryValues battery_values{};
86 VibrationValues vibration_values{};
87
88 // Data for Nintendo devices
89 NpadButtonState npad_button_state{};
90 DebugPadButton debug_pad_button_state{};
91 AnalogSticks analog_stick_state{};
92 MotionState motion_state{};
93 NpadGcTriggerState gc_trigger_state{};
94 ControllerColors colors_state{};
95 BatteryLevelState battery_state{};
96};
97enum class ControllerTriggerType {
98 Button,
99 Stick,
100 Trigger,
101 Motion,
102 Color,
103 Battery,
104 Vibration,
105 Connected,
106 Disconnected,
107 Type,
108 All,
109};
110
111struct ControllerUpdateCallback {
112 std::function<void(ControllerTriggerType)> on_change;
113};
114
115class EmulatedController {
116public:
117 /**
118 * TODO: Write description
119 *
120 * @param npad_id_type
121 */
122 explicit EmulatedController(NpadIdType npad_id_type_);
123 ~EmulatedController();
124
125 YUZU_NON_COPYABLE(EmulatedController);
126 YUZU_NON_MOVEABLE(EmulatedController);
127
128 static NpadType MapSettingsTypeToNPad(Settings::ControllerType type);
129 static Settings::ControllerType MapNPadToSettingsType(NpadType type);
130
131 /// Gets the NpadIdType for this controller.
132 NpadIdType GetNpadIdType() const;
133
134 /// Sets the NpadType for this controller.
135 void SetNpadType(NpadType npad_type_);
136
137 /// Gets the NpadType for this controller.
138 NpadType GetNpadType() const;
139
140 void Connect();
141 void Disconnect();
142
143 bool IsConnected() const;
144 bool IsVibrationEnabled() const;
145
146 void ReloadFromSettings();
147 void ReloadInput();
148 void UnloadInput();
149
150 void EnableConfiguration();
151 void DisableConfiguration();
152 bool IsConfiguring() const;
153 void SaveCurrentConfig();
154 void RestoreConfig();
155
156 std::vector<Common::ParamPackage> GetMappedDevices() const;
157
158 Common::ParamPackage GetButtonParam(std::size_t index) const;
159 Common::ParamPackage GetStickParam(std::size_t index) const;
160 Common::ParamPackage GetMotionParam(std::size_t index) const;
161
162 void SetButtonParam(std::size_t index, Common::ParamPackage param);
163 void SetStickParam(std::size_t index, Common::ParamPackage param);
164 void SetMotionParam(std::size_t index, Common::ParamPackage param);
165
166 ButtonValues GetButtonsValues() const;
167 SticksValues GetSticksValues() const;
168 TriggerValues GetTriggersValues() const;
169 ControllerMotionValues GetMotionValues() const;
170 ColorValues GetColorsValues() const;
171 BatteryValues GetBatteryValues() const;
172
173 NpadButtonState GetNpadButtons() const;
174 DebugPadButton GetDebugPadButtons() const;
175 AnalogSticks GetSticks() const;
176 NpadGcTriggerState GetTriggers() const;
177 MotionState GetMotions() const;
178 ControllerColors GetColors() const;
179 BatteryLevelState GetBattery() const;
180
181 bool SetVibration(std::size_t device_index, VibrationValue vibration);
182 int TestVibration(std::size_t device_index);
183
184 int SetCallback(ControllerUpdateCallback update_callback);
185 void DeleteCallback(int key);
186
187private:
188 /**
189 * Sets the status of a button. Applies toggle properties to the output.
190 *
191 * @param A CallbackStatus and a button index number
192 */
193 void SetButton(Input::CallbackStatus callback, std::size_t index);
194 void SetStick(Input::CallbackStatus callback, std::size_t index);
195 void SetTrigger(Input::CallbackStatus callback, std::size_t index);
196 void SetMotion(Input::CallbackStatus callback, std::size_t index);
197 void SetBattery(Input::CallbackStatus callback, std::size_t index);
198
199 /**
200 * Triggers a callback that something has changed
201 *
202 * @param Input type of the trigger
203 */
204 void TriggerOnChange(ControllerTriggerType type);
205
206 NpadIdType npad_id_type;
207 NpadType npad_type{NpadType::None};
208 bool is_connected{false};
209 bool is_configuring{false};
210 bool is_vibration_enabled{true};
211 f32 motion_sensitivity{0.01f};
212
213 ButtonParams button_params;
214 StickParams stick_params;
215 ControllerMotionParams motion_params;
216 TriggerParams trigger_params;
217 BatteryParams battery_params;
218
219 ButtonDevices button_devices;
220 StickDevices stick_devices;
221 ControllerMotionDevices motion_devices;
222 TriggerDevices trigger_devices;
223 BatteryDevices battery_devices;
224 // VibrationDevices vibration_devices;
225
226 mutable std::mutex mutex;
227 std::unordered_map<int, ControllerUpdateCallback> callback_list;
228 int last_callback_key = 0;
229 ControllerStatus controller;
230};
231
232} // namespace Core::HID