summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerInterface
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2019-02-27 18:10:18 -0600
committerJordan Woyak <jordan.woyak@gmail.com>2019-03-03 18:39:02 -0600
commitc389d68186ea7fa95d763d7bb55bc09e5a45c4cd (patch)
tree9712b3e25f700cdbeb5b75652078867f5d8e548a /Source/Core/InputCommon/ControllerInterface
parent48b69ca01841ba724049895a83d8d88ec95d66de (diff)
ControllerInterface/DolphinQt: Make mapping "all devices" way less hacky.
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface')
-rw-r--r--Source/Core/InputCommon/ControllerInterface/Device.cpp87
-rw-r--r--Source/Core/InputCommon/ControllerInterface/Device.h3
2 files changed, 85 insertions, 5 deletions
diff --git a/Source/Core/InputCommon/ControllerInterface/Device.cpp b/Source/Core/InputCommon/ControllerInterface/Device.cpp
index efe1372711..14f5cb23d9 100644
--- a/Source/Core/InputCommon/ControllerInterface/Device.cpp
+++ b/Source/Core/InputCommon/ControllerInterface/Device.cpp
@@ -11,16 +11,15 @@
#include <tuple>
#include "Common/StringUtil.h"
+#include "Common/Thread.h"
namespace ciface
{
namespace Core
{
-//
-// Device :: ~Device
-//
-// Destructor, delete all inputs/outputs on device destruction
-//
+// Compared to an input's current state (ideally 1.0) minus abs(initial_state) (ideally 0.0).
+constexpr ControlState INPUT_DETECT_THRESHOLD = 0.55;
+
Device::~Device()
{
// delete inputs
@@ -220,5 +219,83 @@ bool DeviceContainer::HasConnectedDevice(const DeviceQualifier& qualifier) const
const auto device = FindDevice(qualifier);
return device != nullptr && device->IsValid();
}
+
+// Wait for input on a particular device.
+// Inputs are considered if they are first seen in a neutral state.
+// This is useful for crazy flightsticks that have certain buttons that are always held down
+// and also properly handles detection when using "FullAnalogSurface" inputs.
+// Upon input, return the detected Device and Input, else return nullptrs
+std::pair<std::shared_ptr<Device>, Device::Input*>
+DeviceContainer::DetectInput(u32 wait_ms, std::vector<std::string> device_strings)
+{
+ struct InputState
+ {
+ ciface::Core::Device::Input& input;
+ ControlState initial_state;
+ };
+
+ struct DeviceState
+ {
+ std::shared_ptr<Device> device;
+
+ std::vector<InputState> input_states;
+ };
+
+ // Acquire devices and initial input states.
+ std::vector<DeviceState> device_states;
+ for (auto& device_string : device_strings)
+ {
+ DeviceQualifier dq;
+ dq.FromString(device_string);
+ auto device = FindDevice(dq);
+
+ if (!device)
+ continue;
+
+ std::vector<InputState> input_states;
+
+ for (auto* input : device->Inputs())
+ {
+ // Don't detect things like absolute cursor position.
+ if (!input->IsDetectable())
+ continue;
+
+ // Undesirable axes will have negative values here when trying to map a
+ // "FullAnalogSurface".
+ input_states.push_back({*input, input->GetState()});
+ }
+
+ if (!input_states.empty())
+ device_states.emplace_back(DeviceState{std::move(device), std::move(input_states)});
+ }
+
+ if (device_states.empty())
+ return {};
+
+ u32 time = 0;
+ while (time < wait_ms)
+ {
+ Common::SleepCurrentThread(10);
+ time += 10;
+
+ for (auto& device_state : device_states)
+ {
+ device_state.device->UpdateInput();
+ for (auto& input_state : device_state.input_states)
+ {
+ // We want an input that was initially 0.0 and currently 1.0.
+ const auto detection_score =
+ (input_state.input.GetState() - std::abs(input_state.initial_state));
+
+ if (detection_score > INPUT_DETECT_THRESHOLD)
+ return {device_state.device, &input_state.input};
+ }
+ }
+ }
+
+ // No input was detected. :'(
+ return {};
+}
+
} // namespace Core
} // namespace ciface
diff --git a/Source/Core/InputCommon/ControllerInterface/Device.h b/Source/Core/InputCommon/ControllerInterface/Device.h
index 0ee2b31444..7d7623ba03 100644
--- a/Source/Core/InputCommon/ControllerInterface/Device.h
+++ b/Source/Core/InputCommon/ControllerInterface/Device.h
@@ -172,6 +172,9 @@ public:
bool HasConnectedDevice(const DeviceQualifier& qualifier) const;
+ std::pair<std::shared_ptr<Device>, Device::Input*>
+ DetectInput(u32 wait_ms, std::vector<std::string> device_strings);
+
protected:
mutable std::mutex m_devices_mutex;
std::vector<std::shared_ptr<Device>> m_devices;