Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/CoreDevice.h

raw symbols SCIP
1 // Copyright 2013 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <chrono>
7 #include <limits>
8 #include <memory>
9 #include <mutex>
10 #include <optional>
11 #include <span>
12 #include <string>
13 #include <string_view>
14 #include <vector>
15
16 #include "Common/CommonTypes.h"
17
18 // idk in case I wanted to change it to double or something, idk what's best
19 typedef double ControlState;
20
21 namespace ciface
22 {
23 // 100Hz which homebrew docs very roughly imply is within WiiMote normal
24 // range, used for periodic haptic effects though often ignored by devices
25 // TODO: Make this configurable.
26 constexpr int RUMBLE_PERIOD_MS = 10;
27
28 // This needs to be at least as long as the longest rumble that might ever be played.
29 // Too short and it's going to stop in the middle of a long effect.
30 // Infinite values are invalid for ramp effects and probably not sensible.
31 constexpr int RUMBLE_LENGTH_MS = 1000 * 10;
32
33 // All inputs (other than accel/gyro) return 1.0 as their maximum value.
34 // Battery inputs will almost always be mapped to the "Battery" setting which is a percentage.
35 // If someone actually wants to map a battery input to a regular control they can divide by 100.
36 // I think this is better than requiring multiplication by 100 for the most common usage.
37 constexpr ControlState BATTERY_INPUT_MAX_VALUE = 100.0;
38
39 namespace Core
40 {
41 enum class DeviceRemoval
42 {
43 Remove,
44 Keep,
45 };
46
47 class Device
48 {
49 public:
50 class Input;
51 class Output;
52
53 //
54 // Control
55 //
56 // Control includes inputs and outputs
57 //
58 class Control // input or output
59 {
60 public:
61 virtual ~Control() = default;
62 virtual std::string GetName() const = 0;
63 virtual Input* ToInput() { return nullptr; }
64 virtual Output* ToOutput() { return nullptr; }
65
66 // May be overridden to allow multiple valid names.
67 // Useful for backwards-compatible configurations when names change.
68 virtual bool IsMatchingName(std::string_view name) const;
69
70 // May be overridden to hide in UI.
71 // Useful for backwards-compatible configurations when names change.
72 virtual bool IsHidden() const;
73 };
74
75 //
76 // Input
77 //
78 // An input on a device
79 //
80 class Input : public Control
81 {
82 public:
83 // Things like absolute axes/ absolute mouse position should override this to prevent
84 // undesirable behavior in our mapping logic.
85 virtual bool IsDetectable() const { return true; }
86
87 // Implementations should return a value from 0.0 to 1.0 across their normal range.
88 // One input should be provided for each "direction". (e.g. 2 for each axis)
89 // If possible, negative values may be returned in situations where an opposing input is
90 // activated. (e.g. When an underlying axis, X, is currently negative, "Axis X-", will return a
91 // positive value and "Axis X+" may return a negative value.)
92 // Doing so is solely to allow our input detection logic to better detect false positives.
93 // This is necessary when making use of "FullAnalogSurface" as multiple inputs will be seen
94 // increasing from 0.0 to 1.0 as a user tries to map just one. The negative values provide a
95 // view of the underlying axis. (Negative values are clamped off before they reach
96 // expression-parser or controller-emu)
97 virtual ControlState GetState() const = 0;
98
99 Input* ToInput() override { return this; }
100
101 // Overridden by CombinedInput,
102 // so hotkey logic knows Ctrl, L_Ctrl, and R_Ctrl are the same,
103 // and so input detection can return the parent name.
104 virtual bool IsChild(const Input*) const { return false; }
105 };
106
107 class RelativeInput : public Input
108 {
109 public:
110 bool IsDetectable() const override { return false; }
111 };
112
113 //
114 // Output
115 //
116 // An output on a device
117 //
118 class Output : public Control
119 {
120 public:
121 ~Output() override = default;
122 virtual void SetState(ControlState state) = 0;
123 Output* ToOutput() override { return this; }
124 };
125
126 virtual ~Device();
127
128 int GetId() const { return m_id; }
129 void SetId(int id) { m_id = id; }
130 virtual std::string GetName() const = 0;
131 virtual std::string GetSource() const = 0;
132 std::string GetQualifiedName() const;
133 virtual DeviceRemoval UpdateInput() { return DeviceRemoval::Keep; }
134
135 // May be overridden to implement hotplug removal.
136 // Currently handled on a per-backend basis but this could change.
137 virtual bool IsValid() const { return true; }
138
139 // Returns true whether this device is "virtual/emulated", not linked
140 // to any actual physical device. Mostly used by keyboard and mouse devices,
141 // and to avoid uselessly recreating the device unless really necessary.
142 // Doesn't necessarily need to be set to true if the device is virtual.
143 virtual bool IsVirtualDevice() const { return false; }
144
145 // (e.g. Xbox 360 controllers have controller number LEDs which should match the ID we use.)
146 virtual std::optional<int> GetPreferredId() const;
147
148 // Use this to change the order in which devices are sorted in their list.
149 // A higher priority means it will be one of the first ones (smaller index), making it more
150 // likely to be index 0, which is automatically set as the default device when there isn't one.
151 // Every platform should have at least one device with priority >= 0.
152 static constexpr int DEFAULT_DEVICE_SORT_PRIORITY = std::numeric_limits<int>::max();
153 virtual int GetSortPriority() const { return 0; }
154
155 const std::vector<Input*>& Inputs() const { return m_inputs; }
156 const std::vector<Output*>& Outputs() const { return m_outputs; }
157
158 Input* GetParentMostInput(Input* input) const;
159
160 Input* FindInput(std::string_view name) const;
161 Output* FindOutput(std::string_view name) const;
162
163 protected:
164 void AddInput(Input* const i);
165 void AddOutput(Output* const o);
166
167 // Pass Inputs for center-neutral (- and +) directions of some axis.
168 // This function adds those Inputs and also a FullAnalogSurface Input for each direction.
169 // This is only needed when it's not known if the particular axis is neutral in the center
170 // or neutral on one of the extremes.
171 // Some e.g. DInput devices expose a trigger across the full analog surface
172 // but we have no way of knowing this until the user actually maps the Input,
173 // so both center-neutral and full-surface Inputs need to be created in that case.
174 void AddFullAnalogSurfaceInputs(Input* low, Input* high);
175
176 void AddCombinedInput(std::string name, const std::pair<std::string, std::string>& inputs);
177
178 private:
179 int m_id = 0;
180 std::vector<Input*> m_inputs;
181 std::vector<Output*> m_outputs;
182 };
183
184 //
185 // DeviceQualifier
186 //
187 // Device qualifier used to match devices.
188 // Currently has ( source, id, name ) properties which match a device
189 //
190 class DeviceQualifier
191 {
192 public:
193 DeviceQualifier() : cid(-1) {}
194 DeviceQualifier(std::string source_, const int id_, std::string name_)
195 : source(std::move(source_)), cid(id_), name(std::move(name_))
196 {
197 }
198 void FromDevice(const Device* const dev);
199 void FromString(const std::string& str);
200 std::string ToString() const;
201
202 bool operator==(const DeviceQualifier& devq) const;
203
204 bool operator==(const Device* dev) const;
205
206 std::string source;
207 int cid;
208 std::string name;
209 };
210
211 class DeviceContainer
212 {
213 public:
214 struct InputDetection
215 {
216 std::shared_ptr<Device> device;
217 Device::Input* input = nullptr;
218 Clock::time_point press_time;
219 std::optional<Clock::time_point> release_time;
220 ControlState smoothness = 0;
221
222 bool IsAnalogPress() const { return smoothness > 1.00001; }
223 };
224
225 Device::Input* FindInput(std::string_view name, const Device* def_dev) const;
226 Device::Output* FindOutput(std::string_view name, const Device* def_dev) const;
227
228 std::vector<std::shared_ptr<Device>> GetAllDevices() const;
229 std::vector<std::string> GetAllDeviceStrings() const;
230 bool HasDefaultDevice() const;
231 std::string GetDefaultDeviceString() const;
232 std::shared_ptr<Device> FindDevice(const DeviceQualifier& devq) const;
233
234 bool HasConnectedDevice(const DeviceQualifier& qualifier) const;
235
236 std::recursive_mutex& GetDevicesMutex() const { return m_devices_mutex; }
237
238 protected:
239 // Exclusively needed when reading/writing the "m_devices" array.
240 // Not needed when individually reading/writing a single device ptr.
241 mutable std::recursive_mutex m_devices_mutex;
242 std::vector<std::shared_ptr<Device>> m_devices;
243 };
244
245 // Wait for inputs on supplied devices.
246 // Inputs are only considered if they are first seen in a neutral state.
247 // This is useful for wacky flight sticks that have certain buttons that are always held down
248 // and also properly handles detection when using "FullAnalogSurface" inputs.
249 // Multiple detections are returned until the various timeouts have been reached.
250 class InputDetector
251 {
252 public:
253 using Detection = DeviceContainer::InputDetection;
254 using Results = std::vector<Detection>;
255
256 InputDetector();
257 ~InputDetector();
258
259 void Start(const DeviceContainer& container, std::span<const std::string> device_strings);
260 void Update(std::chrono::milliseconds initial_wait, std::chrono::milliseconds confirmation_wait,
261 std::chrono::milliseconds maximum_wait);
262 bool IsComplete() const;
263
264 const Results& GetResults() const;
265
266 // move-return'd to prevent copying.
267 Results TakeResults();
268
269 private:
270 struct Impl;
271
272 Clock::time_point m_start_time;
273 Results m_detections;
274 std::unique_ptr<Impl> m_state;
275 };
276
277 } // namespace Core
278 } // namespace ciface