2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
#include <chrono>
|
2016-06-26 03:34:09 +00:00
|
|
|
#include <memory>
|
2016-06-12 15:31:41 +00:00
|
|
|
#include <mutex>
|
2019-03-09 15:57:37 +00:00
|
|
|
#include <optional>
|
2013-06-17 00:07:10 +00:00
|
|
|
#include <string>
|
2019-05-29 22:42:22 +00:00
|
|
|
#include <string_view>
|
2013-06-17 00:07:10 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
// idk in case I wanted to change it to double or something, idk what's best
|
2014-08-11 17:43:26 +00:00
|
|
|
typedef double ControlState;
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
// 100Hz which homebrew docs very roughly imply is within WiiMote normal
|
|
|
|
// range, used for periodic haptic effects though often ignored by devices
|
|
|
|
// TODO: Make this configurable.
|
|
|
|
constexpr int RUMBLE_PERIOD_MS = 10;
|
2019-11-17 16:42:49 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
// This needs to be at least as long as the longest rumble that might ever be played.
|
|
|
|
// Too short and it's going to stop in the middle of a long effect.
|
|
|
|
// Infinite values are invalid for ramp effects and probably not sensible.
|
|
|
|
constexpr int RUMBLE_LENGTH_MS = 1000 * 10;
|
|
|
|
|
2019-11-17 16:42:49 +00:00
|
|
|
// All inputs (other than accel/gyro) return 1.0 as their maximum value.
|
|
|
|
// Battery inputs will almost always be mapped to the "Battery" setting which is a percentage.
|
|
|
|
// If someone actually wants to map a battery input to a regular control they can divide by 100.
|
|
|
|
// I think this is better than requiring multiplication by 100 for the most common usage.
|
|
|
|
constexpr ControlState BATTERY_INPUT_MAX_VALUE = 100.0;
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
namespace Core
|
|
|
|
{
|
|
|
|
class Device
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Input;
|
|
|
|
class Output;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Control
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Control includes inputs and outputs
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
class Control // input or output
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-05-29 22:42:22 +00:00
|
|
|
virtual ~Control() = default;
|
2013-06-17 00:07:10 +00:00
|
|
|
virtual std::string GetName() const = 0;
|
2014-03-09 20:14:26 +00:00
|
|
|
virtual Input* ToInput() { return nullptr; }
|
|
|
|
virtual Output* ToOutput() { return nullptr; }
|
2019-03-14 00:47:52 +00:00
|
|
|
|
|
|
|
// May be overridden to allow multiple valid names.
|
|
|
|
// Useful for backwards-compatible configurations when names change.
|
2019-05-29 22:42:22 +00:00
|
|
|
virtual bool IsMatchingName(std::string_view name) const;
|
2013-06-17 00:07:10 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Input
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// An input on a device
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
class Input : public Control
|
|
|
|
{
|
|
|
|
public:
|
2019-02-27 01:46:21 +00:00
|
|
|
// Things like absolute axes/ absolute mouse position should override this to prevent
|
|
|
|
// undesirable behavior in our mapping logic.
|
2020-02-22 16:27:43 +00:00
|
|
|
virtual bool IsDetectable() const { return true; }
|
2019-02-27 01:46:21 +00:00
|
|
|
|
|
|
|
// Implementations should return a value from 0.0 to 1.0 across their normal range.
|
|
|
|
// One input should be provided for each "direction". (e.g. 2 for each axis)
|
|
|
|
// If possible, negative values may be returned in situations where an opposing input is
|
|
|
|
// activated. (e.g. When an underlying axis, X, is currently negative, "Axis X-", will return a
|
|
|
|
// positive value and "Axis X+" may return a negative value.)
|
|
|
|
// Doing so is solely to allow our input detection logic to better detect false positives.
|
|
|
|
// This is necessary when making use of "FullAnalogSurface" as multiple inputs will be seen
|
|
|
|
// increasing from 0.0 to 1.0 as a user tries to map just one. The negative values provide a
|
|
|
|
// view of the underlying axis. (Negative values are clamped off before they reach
|
|
|
|
// expression-parser or controller-emu)
|
2013-06-17 00:07:10 +00:00
|
|
|
virtual ControlState GetState() const = 0;
|
2019-02-27 01:46:21 +00:00
|
|
|
|
2014-03-08 00:54:44 +00:00
|
|
|
Input* ToInput() override { return this; }
|
2020-01-26 19:58:20 +00:00
|
|
|
|
|
|
|
// Overridden by CombinedInput,
|
|
|
|
// so hotkey logic knows Ctrl, L_Ctrl, and R_Ctrl are the same,
|
|
|
|
// and so input detection can return the parent name.
|
|
|
|
virtual bool IsChild(const Input*) const { return false; }
|
2013-06-17 00:07:10 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-04-21 04:16:07 +00:00
|
|
|
class RelativeInput : public Input
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool IsDetectable() const override { return false; }
|
|
|
|
};
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Output
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// An output on a device
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
class Output : public Control
|
|
|
|
{
|
|
|
|
public:
|
2019-03-09 15:57:37 +00:00
|
|
|
virtual ~Output() = default;
|
2013-06-17 00:07:10 +00:00
|
|
|
virtual void SetState(ControlState state) = 0;
|
2014-03-08 00:54:44 +00:00
|
|
|
Output* ToOutput() override { return this; }
|
2013-06-17 00:07:10 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
virtual ~Device();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-07-14 08:25:52 +00:00
|
|
|
int GetId() const { return m_id; }
|
|
|
|
void SetId(int id) { m_id = id; }
|
2013-06-17 00:07:10 +00:00
|
|
|
virtual std::string GetName() const = 0;
|
|
|
|
virtual std::string GetSource() const = 0;
|
2017-11-09 20:14:21 +00:00
|
|
|
std::string GetQualifiedName() const;
|
2014-11-13 08:55:14 +00:00
|
|
|
virtual void UpdateInput() {}
|
2019-03-09 15:57:37 +00:00
|
|
|
|
|
|
|
// May be overridden to implement hotplug removal.
|
|
|
|
// Currently handled on a per-backend basis but this could change.
|
2016-07-14 15:45:59 +00:00
|
|
|
virtual bool IsValid() const { return true; }
|
2019-03-09 15:57:37 +00:00
|
|
|
|
|
|
|
// (e.g. Xbox 360 controllers have controller number LEDs which should match the ID we use.)
|
|
|
|
virtual std::optional<int> GetPreferredId() const;
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
const std::vector<Input*>& Inputs() const { return m_inputs; }
|
|
|
|
const std::vector<Output*>& Outputs() const { return m_outputs; }
|
2019-03-09 15:57:37 +00:00
|
|
|
|
2020-01-26 19:58:20 +00:00
|
|
|
Input* GetParentMostInput(Input* input) const;
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
Input* FindInput(std::string_view name) const;
|
|
|
|
Output* FindOutput(std::string_view name) const;
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void AddInput(Input* const i);
|
|
|
|
void AddOutput(Output* const o);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-14 00:47:52 +00:00
|
|
|
class FullAnalogSurface final : public Input
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
FullAnalogSurface(Input* low, Input* high) : m_low(*low), m_high(*high) {}
|
2019-02-27 01:46:21 +00:00
|
|
|
ControlState GetState() const override;
|
2019-03-14 00:47:52 +00:00
|
|
|
std::string GetName() const override;
|
2019-05-29 22:42:22 +00:00
|
|
|
bool IsMatchingName(std::string_view name) const override;
|
2018-04-12 12:18:04 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
private:
|
|
|
|
Input& m_low;
|
|
|
|
Input& m_high;
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
void AddAnalogInputs(Input* low, Input* high)
|
|
|
|
{
|
|
|
|
AddInput(low);
|
|
|
|
AddInput(high);
|
|
|
|
AddInput(new FullAnalogSurface(low, high));
|
|
|
|
AddInput(new FullAnalogSurface(high, low));
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:33:59 +00:00
|
|
|
void AddCombinedInput(std::string name, const std::pair<std::string, std::string>& inputs);
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
private:
|
2016-07-14 08:25:52 +00:00
|
|
|
int m_id;
|
2014-01-31 00:51:21 +00:00
|
|
|
std::vector<Input*> m_inputs;
|
|
|
|
std::vector<Output*> m_outputs;
|
2013-06-17 00:07:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// DeviceQualifier
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Device qualifier used to match devices.
|
|
|
|
// Currently has ( source, id, name ) properties which match a device
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
class DeviceQualifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DeviceQualifier() : cid(-1) {}
|
2019-05-29 22:46:44 +00:00
|
|
|
DeviceQualifier(std::string source_, const int id_, std::string name_)
|
|
|
|
: source(std::move(source_)), cid(id_), name(std::move(name_))
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void FromDevice(const Device* const dev);
|
|
|
|
void FromString(const std::string& str);
|
|
|
|
std::string ToString() const;
|
2017-06-03 23:32:46 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
bool operator==(const DeviceQualifier& devq) const;
|
2017-06-03 23:32:46 +00:00
|
|
|
bool operator!=(const DeviceQualifier& devq) const;
|
|
|
|
|
|
|
|
bool operator==(const Device* dev) const;
|
|
|
|
bool operator!=(const Device* dev) const;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 00:51:21 +00:00
|
|
|
std::string source;
|
|
|
|
int cid;
|
|
|
|
std::string name;
|
2013-06-17 00:07:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DeviceContainer
|
|
|
|
{
|
|
|
|
public:
|
2020-02-12 22:49:17 +00:00
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
|
|
|
|
struct InputDetection
|
|
|
|
{
|
|
|
|
std::shared_ptr<Device> device;
|
|
|
|
Device::Input* input;
|
|
|
|
Clock::time_point press_time;
|
|
|
|
std::optional<Clock::time_point> release_time;
|
|
|
|
ControlState smoothness;
|
|
|
|
};
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
Device::Input* FindInput(std::string_view name, const Device* def_dev) const;
|
|
|
|
Device::Output* FindOutput(std::string_view name, const Device* def_dev) const;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
std::vector<std::string> GetAllDeviceStrings() const;
|
|
|
|
std::string GetDefaultDeviceString() const;
|
|
|
|
std::shared_ptr<Device> FindDevice(const DeviceQualifier& devq) const;
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2017-02-08 00:25:27 +00:00
|
|
|
bool HasConnectedDevice(const DeviceQualifier& qualifier) const;
|
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
std::vector<InputDetection> DetectInput(const std::vector<std::string>& device_strings,
|
|
|
|
std::chrono::milliseconds initial_wait,
|
|
|
|
std::chrono::milliseconds confirmation_wait,
|
|
|
|
std::chrono::milliseconds maximum_wait) const;
|
2019-02-28 00:10:18 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
protected:
|
2020-01-22 00:52:58 +00:00
|
|
|
mutable std::recursive_mutex m_devices_mutex;
|
2016-06-25 19:46:39 +00:00
|
|
|
std::vector<std::shared_ptr<Device>> m_devices;
|
2013-06-17 00:07:10 +00:00
|
|
|
};
|
2019-01-08 15:28:24 +00:00
|
|
|
} // namespace Core
|
|
|
|
} // namespace ciface
|