2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
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
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
// Returns true whether this device is "virtual/emulated", not linked
|
|
|
|
// to any actual physical device. Mostly used by keyboard and mouse devices,
|
|
|
|
// and to avoid uselessly recreating the device unless really necessary.
|
|
|
|
// Doesn't necessarily need to be set to true if the device is virtual.
|
|
|
|
virtual bool IsVirtualDevice() const { return false; }
|
|
|
|
|
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;
|
|
|
|
|
2021-05-15 09:14:11 +00:00
|
|
|
// Use this to change the order in which devices are sorted in their list.
|
|
|
|
// A higher priority means it will be one of the first ones (smaller index), making it more
|
|
|
|
// likely to be index 0, which is automatically set as the default device when there isn't one.
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
// Every platform should have at least one device with priority >= 0.
|
2021-05-15 09:14:11 +00:00
|
|
|
virtual int GetSortPriority() const { return 0; }
|
|
|
|
|
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:
|
2021-09-04 04:43:19 +00:00
|
|
|
int m_id = 0;
|
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;
|
2021-09-04 04:43:19 +00:00
|
|
|
Device::Input* input = nullptr;
|
2020-02-12 22:49:17 +00:00
|
|
|
Clock::time_point press_time;
|
|
|
|
std::optional<Clock::time_point> release_time;
|
2021-09-04 04:43:19 +00:00
|
|
|
ControlState smoothness = 0;
|
2020-02-12 22:49:17 +00:00
|
|
|
};
|
|
|
|
|
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;
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
bool HasDefaultDevice() const;
|
2016-06-25 19:46:39 +00:00
|
|
|
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
|
|
|
|
2022-09-07 20:37:14 +00:00
|
|
|
std::recursive_mutex& GetDevicesMutex() const { return m_devices_mutex; }
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
protected:
|
2021-05-15 08:32:00 +00:00
|
|
|
// Exclusively needed when reading/writing "m_devices"
|
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
|