| 1 |
|
| 2 |
|
| 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 |
|
| 19 |
typedef double ControlState; |
| 20 |
|
| 21 |
namespace ciface |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
constexpr int RUMBLE_PERIOD_MS = 10; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
constexpr int RUMBLE_LENGTH_MS = 1000 * 10; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
class Control |
| 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 |
|
| 67 |
|
| 68 |
virtual bool IsMatchingName(std::string_view name) const; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
virtual bool IsHidden() const; |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
class Input : public Control |
| 81 |
{ |
| 82 |
public: |
| 83 |
|
| 84 |
|
| 85 |
virtual bool IsDetectable() const { return true; } |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
virtual ControlState GetState() const = 0; |
| 98 |
|
| 99 |
Input* ToInput() override { return this; } |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 136 |
|
| 137 |
virtual bool IsValid() const { return true; } |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
virtual bool IsVirtualDevice() const { return false; } |
| 144 |
|
| 145 |
|
| 146 |
virtual std::optional<int> GetPreferredId() const; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 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 |
|
| 240 |
|
| 241 |
mutable std::recursive_mutex m_devices_mutex; |
| 242 |
std::vector<std::shared_ptr<Device>> m_devices; |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 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 |
|
| 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 |
} |
| 278 |
} |