1 // Copyright 2013 Dolphin Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4 
5 #include "InputCommon/ControllerInterface/Device.h"
6 
7 #include <algorithm>
8 #include <cmath>
9 #include <memory>
10 #include <sstream>
11 #include <string>
12 #include <tuple>
13 
14 #include <fmt/format.h>
15 
16 #include "Common/MathUtil.h"
17 #include "Common/Thread.h"
18 
19 namespace ciface::Core
20 {
21 // Compared to an input's current state (ideally 1.0) minus abs(initial_state) (ideally 0.0).
22 // Note: Detect() logic assumes this is greater than 0.5.
23 constexpr ControlState INPUT_DETECT_THRESHOLD = 0.55;
24 
25 class CombinedInput final : public Device::Input
26 {
27 public:
28   using Inputs = std::pair<Device::Input*, Device::Input*>;
29 
CombinedInput(std::string name,const Inputs & inputs)30   CombinedInput(std::string name, const Inputs& inputs) : m_name(std::move(name)), m_inputs(inputs)
31   {
32   }
GetState() const33   ControlState GetState() const override
34   {
35     ControlState result = 0;
36 
37     if (m_inputs.first)
38       result = m_inputs.first->GetState();
39 
40     if (m_inputs.second)
41       result = std::max(result, m_inputs.second->GetState());
42 
43     return result;
44   }
GetName() const45   std::string GetName() const override { return m_name; }
IsDetectable() const46   bool IsDetectable() const override { return false; }
IsChild(const Input * input) const47   bool IsChild(const Input* input) const override
48   {
49     return m_inputs.first == input || m_inputs.second == input;
50   }
51 
52 private:
53   const std::string m_name;
54   const std::pair<Device::Input*, Device::Input*> m_inputs;
55 };
56 
~Device()57 Device::~Device()
58 {
59   // delete inputs
60   for (Device::Input* input : m_inputs)
61     delete input;
62 
63   // delete outputs
64   for (Device::Output* output : m_outputs)
65     delete output;
66 }
67 
GetPreferredId() const68 std::optional<int> Device::GetPreferredId() const
69 {
70   return {};
71 }
72 
AddInput(Device::Input * const i)73 void Device::AddInput(Device::Input* const i)
74 {
75   m_inputs.push_back(i);
76 }
77 
AddOutput(Device::Output * const o)78 void Device::AddOutput(Device::Output* const o)
79 {
80   m_outputs.push_back(o);
81 }
82 
GetQualifiedName() const83 std::string Device::GetQualifiedName() const
84 {
85   return fmt::format("{}/{}/{}", GetSource(), GetId(), GetName());
86 }
87 
GetParentMostInput(Input * child) const88 auto Device::GetParentMostInput(Input* child) const -> Input*
89 {
90   for (auto* input : m_inputs)
91   {
92     if (input->IsChild(child))
93     {
94       // Running recursively is currently unnecessary but it doesn't hurt.
95       return GetParentMostInput(input);
96     }
97   }
98 
99   return child;
100 }
101 
FindInput(std::string_view name) const102 Device::Input* Device::FindInput(std::string_view name) const
103 {
104   for (Input* input : m_inputs)
105   {
106     if (input->IsMatchingName(name))
107       return input;
108   }
109 
110   return nullptr;
111 }
112 
FindOutput(std::string_view name) const113 Device::Output* Device::FindOutput(std::string_view name) const
114 {
115   for (Output* output : m_outputs)
116   {
117     if (output->IsMatchingName(name))
118       return output;
119   }
120 
121   return nullptr;
122 }
123 
IsMatchingName(std::string_view name) const124 bool Device::Control::IsMatchingName(std::string_view name) const
125 {
126   return GetName() == name;
127 }
128 
GetState() const129 ControlState Device::FullAnalogSurface::GetState() const
130 {
131   return (1 + std::max(0.0, m_high.GetState()) - std::max(0.0, m_low.GetState())) / 2;
132 }
133 
GetName() const134 std::string Device::FullAnalogSurface::GetName() const
135 {
136   // E.g. "Full Axis X+"
137   return "Full " + m_high.GetName();
138 }
139 
IsMatchingName(std::string_view name) const140 bool Device::FullAnalogSurface::IsMatchingName(std::string_view name) const
141 {
142   if (Control::IsMatchingName(name))
143     return true;
144 
145   // Old naming scheme was "Axis X-+" which is too visually similar to "Axis X+".
146   // This has caused countless problems for users with mysterious misconfigurations.
147   // We match this old name to support old configurations.
148   const auto old_name = m_low.GetName() + *m_high.GetName().rbegin();
149 
150   return old_name == name;
151 }
152 
AddCombinedInput(std::string name,const std::pair<std::string,std::string> & inputs)153 void Device::AddCombinedInput(std::string name, const std::pair<std::string, std::string>& inputs)
154 {
155   AddInput(new CombinedInput(std::move(name), {FindInput(inputs.first), FindInput(inputs.second)}));
156 }
157 
158 //
159 // DeviceQualifier :: ToString
160 //
161 // Get string from a device qualifier / serialize
162 //
ToString() const163 std::string DeviceQualifier::ToString() const
164 {
165   if (source.empty() && (cid < 0) && name.empty())
166     return "";
167 
168   std::ostringstream ss;
169   ss << source << '/';
170   if (cid > -1)
171     ss << cid;
172   ss << '/' << name;
173 
174   return ss.str();
175 }
176 
177 //
178 // DeviceQualifier :: FromString
179 //
180 // Set a device qualifier from a string / unserialize
181 //
FromString(const std::string & str)182 void DeviceQualifier::FromString(const std::string& str)
183 {
184   *this = {};
185 
186   std::istringstream ss(str);
187 
188   std::getline(ss, source, '/');
189 
190   // silly
191   std::getline(ss, name, '/');
192   std::istringstream(name) >> cid;
193 
194   std::getline(ss, name);
195 }
196 
197 //
198 // DeviceQualifier :: FromDevice
199 //
200 // Set a device qualifier from a device
201 //
FromDevice(const Device * const dev)202 void DeviceQualifier::FromDevice(const Device* const dev)
203 {
204   name = dev->GetName();
205   cid = dev->GetId();
206   source = dev->GetSource();
207 }
208 
operator ==(const Device * const dev) const209 bool DeviceQualifier::operator==(const Device* const dev) const
210 {
211   if (dev->GetId() == cid)
212     if (dev->GetName() == name)
213       if (dev->GetSource() == source)
214         return true;
215 
216   return false;
217 }
218 
operator !=(const Device * const dev) const219 bool DeviceQualifier::operator!=(const Device* const dev) const
220 {
221   return !operator==(dev);
222 }
223 
operator ==(const DeviceQualifier & devq) const224 bool DeviceQualifier::operator==(const DeviceQualifier& devq) const
225 {
226   return std::tie(cid, name, source) == std::tie(devq.cid, devq.name, devq.source);
227 }
228 
operator !=(const DeviceQualifier & devq) const229 bool DeviceQualifier::operator!=(const DeviceQualifier& devq) const
230 {
231   return !operator==(devq);
232 }
233 
FindDevice(const DeviceQualifier & devq) const234 std::shared_ptr<Device> DeviceContainer::FindDevice(const DeviceQualifier& devq) const
235 {
236   std::lock_guard lk(m_devices_mutex);
237   for (const auto& d : m_devices)
238   {
239     if (devq == d.get())
240       return d;
241   }
242 
243   return nullptr;
244 }
245 
GetAllDeviceStrings() const246 std::vector<std::string> DeviceContainer::GetAllDeviceStrings() const
247 {
248   std::lock_guard lk(m_devices_mutex);
249 
250   std::vector<std::string> device_strings;
251   DeviceQualifier device_qualifier;
252 
253   for (const auto& d : m_devices)
254   {
255     device_qualifier.FromDevice(d.get());
256     device_strings.emplace_back(device_qualifier.ToString());
257   }
258 
259   return device_strings;
260 }
261 
GetDefaultDeviceString() const262 std::string DeviceContainer::GetDefaultDeviceString() const
263 {
264   std::lock_guard lk(m_devices_mutex);
265   if (m_devices.empty())
266     return "";
267 
268   DeviceQualifier device_qualifier;
269   device_qualifier.FromDevice(m_devices[0].get());
270   return device_qualifier.ToString();
271 }
272 
FindInput(std::string_view name,const Device * def_dev) const273 Device::Input* DeviceContainer::FindInput(std::string_view name, const Device* def_dev) const
274 {
275   if (def_dev)
276   {
277     Device::Input* const inp = def_dev->FindInput(name);
278     if (inp)
279       return inp;
280   }
281 
282   std::lock_guard lk(m_devices_mutex);
283   for (const auto& d : m_devices)
284   {
285     Device::Input* const i = d->FindInput(name);
286 
287     if (i)
288       return i;
289   }
290 
291   return nullptr;
292 }
293 
FindOutput(std::string_view name,const Device * def_dev) const294 Device::Output* DeviceContainer::FindOutput(std::string_view name, const Device* def_dev) const
295 {
296   return def_dev->FindOutput(name);
297 }
298 
HasConnectedDevice(const DeviceQualifier & qualifier) const299 bool DeviceContainer::HasConnectedDevice(const DeviceQualifier& qualifier) const
300 {
301   const auto device = FindDevice(qualifier);
302   return device != nullptr && device->IsValid();
303 }
304 
305 // Wait for inputs on supplied devices.
306 // Inputs are only considered if they are first seen in a neutral state.
307 // This is useful for crazy flightsticks that have certain buttons that are always held down
308 // and also properly handles detection when using "FullAnalogSurface" inputs.
309 // Multiple detections are returned until the various timeouts have been reached.
DetectInput(const std::vector<std::string> & device_strings,std::chrono::milliseconds initial_wait,std::chrono::milliseconds confirmation_wait,std::chrono::milliseconds maximum_wait) const310 auto DeviceContainer::DetectInput(const std::vector<std::string>& device_strings,
311                                   std::chrono::milliseconds initial_wait,
312                                   std::chrono::milliseconds confirmation_wait,
313                                   std::chrono::milliseconds maximum_wait) const
314     -> std::vector<InputDetection>
315 {
316   struct InputState
317   {
318     InputState(ciface::Core::Device::Input* input_) : input{input_} { stats.Push(0.0); }
319 
320     ciface::Core::Device::Input* input;
321     ControlState initial_state = input->GetState();
322     ControlState last_state = initial_state;
323     MathUtil::RunningVariance<ControlState> stats;
324 
325     // Prevent multiiple detections until after release.
326     bool is_ready = true;
327 
328     void Update()
329     {
330       const auto new_state = input->GetState();
331 
332       if (!is_ready && new_state < (1 - INPUT_DETECT_THRESHOLD))
333       {
334         last_state = new_state;
335         is_ready = true;
336         stats.Clear();
337       }
338 
339       const auto difference = new_state - last_state;
340       stats.Push(difference);
341       last_state = new_state;
342     }
343 
344     bool IsPressed()
345     {
346       if (!is_ready)
347         return false;
348 
349       // We want an input that was initially 0.0 and currently 1.0.
350       const auto detection_score = (last_state - std::abs(initial_state));
351       return detection_score > INPUT_DETECT_THRESHOLD;
352     }
353   };
354 
355   struct DeviceState
356   {
357     std::shared_ptr<Device> device;
358 
359     std::vector<InputState> input_states;
360   };
361 
362   // Acquire devices and initial input states.
363   std::vector<DeviceState> device_states;
364   for (const auto& device_string : device_strings)
365   {
366     DeviceQualifier dq;
367     dq.FromString(device_string);
368     auto device = FindDevice(dq);
369 
370     if (!device)
371       continue;
372 
373     std::vector<InputState> input_states;
374 
375     for (auto* input : device->Inputs())
376     {
377       // Don't detect things like absolute cursor positions, accelerometers, or gyroscopes.
378       if (!input->IsDetectable())
379         continue;
380 
381       // Undesirable axes will have negative values here when trying to map a
382       // "FullAnalogSurface".
383       input_states.push_back(InputState{input});
384     }
385 
386     if (!input_states.empty())
387       device_states.emplace_back(DeviceState{std::move(device), std::move(input_states)});
388   }
389 
390   if (device_states.empty())
391     return {};
392 
393   std::vector<InputDetection> detections;
394 
395   const auto start_time = Clock::now();
396   while (true)
397   {
398     const auto now = Clock::now();
399     const auto elapsed_time = now - start_time;
400 
401     if (elapsed_time >= maximum_wait || (detections.empty() && elapsed_time >= initial_wait) ||
402         (!detections.empty() && detections.back().release_time.has_value() &&
403          now >= *detections.back().release_time + confirmation_wait))
404     {
405       break;
406     }
407 
408     Common::SleepCurrentThread(10);
409 
410     for (auto& device_state : device_states)
411     {
412       for (std::size_t i = 0; i != device_state.input_states.size(); ++i)
413       {
414         auto& input_state = device_state.input_states[i];
415         input_state.Update();
416 
417         if (input_state.IsPressed())
418         {
419           input_state.is_ready = false;
420 
421           // Digital presses will evaluate as 1 here.
422           // Analog presses will evaluate greater than 1.
423           const auto smoothness =
424               1 / std::sqrt(input_state.stats.Variance() / input_state.stats.Mean());
425 
426           InputDetection new_detection;
427           new_detection.device = device_state.device;
428           new_detection.input = input_state.input;
429           new_detection.press_time = Clock::now();
430           new_detection.smoothness = smoothness;
431 
432           // We found an input. Add it to our detections.
433           detections.emplace_back(std::move(new_detection));
434         }
435       }
436     }
437 
438     // Check for any releases of our detected inputs.
439     for (auto& d : detections)
440     {
441       if (!d.release_time.has_value() && d.input->GetState() < (1 - INPUT_DETECT_THRESHOLD))
442         d.release_time = Clock::now();
443     }
444   }
445 
446   return detections;
447 }
448 }  // namespace ciface::Core
449