diff options
| -rw-r--r-- | src/core/frontend/input.h | 217 |
1 files changed, 0 insertions, 217 deletions
diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h deleted file mode 100644 index f1747c5b2..000000000 --- a/src/core/frontend/input.h +++ /dev/null | |||
| @@ -1,217 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include <tuple> | ||
| 11 | #include <unordered_map> | ||
| 12 | #include <utility> | ||
| 13 | #include "common/logging/log.h" | ||
| 14 | #include "common/param_package.h" | ||
| 15 | #include "common/quaternion.h" | ||
| 16 | #include "common/vector_math.h" | ||
| 17 | |||
| 18 | namespace Input { | ||
| 19 | |||
| 20 | enum class AnalogDirection : u8 { | ||
| 21 | RIGHT, | ||
| 22 | LEFT, | ||
| 23 | UP, | ||
| 24 | DOWN, | ||
| 25 | }; | ||
| 26 | struct AnalogProperties { | ||
| 27 | float deadzone; | ||
| 28 | float range; | ||
| 29 | float threshold; | ||
| 30 | }; | ||
| 31 | template <typename StatusType> | ||
| 32 | struct InputCallback { | ||
| 33 | std::function<void(StatusType)> on_change; | ||
| 34 | }; | ||
| 35 | |||
| 36 | /// An abstract class template for an input device (a button, an analog input, etc.). | ||
| 37 | template <typename StatusType> | ||
| 38 | class InputDevice { | ||
| 39 | public: | ||
| 40 | virtual ~InputDevice() = default; | ||
| 41 | virtual StatusType GetStatus() const { | ||
| 42 | return {}; | ||
| 43 | } | ||
| 44 | virtual StatusType GetRawStatus() const { | ||
| 45 | return GetStatus(); | ||
| 46 | } | ||
| 47 | virtual AnalogProperties GetAnalogProperties() const { | ||
| 48 | return {}; | ||
| 49 | } | ||
| 50 | virtual bool GetAnalogDirectionStatus([[maybe_unused]] AnalogDirection direction) const { | ||
| 51 | return {}; | ||
| 52 | } | ||
| 53 | virtual bool SetRumblePlay([[maybe_unused]] f32 amp_low, [[maybe_unused]] f32 freq_low, | ||
| 54 | [[maybe_unused]] f32 amp_high, | ||
| 55 | [[maybe_unused]] f32 freq_high) const { | ||
| 56 | return {}; | ||
| 57 | } | ||
| 58 | void SetCallback(InputCallback<StatusType> callback_) { | ||
| 59 | callback = std::move(callback_); | ||
| 60 | } | ||
| 61 | void TriggerOnChange() { | ||
| 62 | if (callback.on_change) { | ||
| 63 | callback.on_change(GetStatus()); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | private: | ||
| 68 | InputCallback<StatusType> callback; | ||
| 69 | }; | ||
| 70 | |||
| 71 | /// An abstract class template for a factory that can create input devices. | ||
| 72 | template <typename InputDeviceType> | ||
| 73 | class Factory { | ||
| 74 | public: | ||
| 75 | virtual ~Factory() = default; | ||
| 76 | virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0; | ||
| 77 | }; | ||
| 78 | |||
| 79 | namespace Impl { | ||
| 80 | |||
| 81 | template <typename InputDeviceType> | ||
| 82 | using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>; | ||
| 83 | |||
| 84 | template <typename InputDeviceType> | ||
| 85 | struct FactoryList { | ||
| 86 | static FactoryListType<InputDeviceType> list; | ||
| 87 | }; | ||
| 88 | |||
| 89 | template <typename InputDeviceType> | ||
| 90 | FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::list; | ||
| 91 | |||
| 92 | } // namespace Impl | ||
| 93 | |||
| 94 | /** | ||
| 95 | * Registers an input device factory. | ||
| 96 | * @tparam InputDeviceType the type of input devices the factory can create | ||
| 97 | * @param name the name of the factory. Will be used to match the "engine" parameter when creating | ||
| 98 | * a device | ||
| 99 | * @param factory the factory object to register | ||
| 100 | */ | ||
| 101 | template <typename InputDeviceType> | ||
| 102 | void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) { | ||
| 103 | auto pair = std::make_pair(name, std::move(factory)); | ||
| 104 | if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) { | ||
| 105 | LOG_ERROR(Input, "Factory '{}' already registered", name); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | /** | ||
| 110 | * Unregisters an input device factory. | ||
| 111 | * @tparam InputDeviceType the type of input devices the factory can create | ||
| 112 | * @param name the name of the factory to unregister | ||
| 113 | */ | ||
| 114 | template <typename InputDeviceType> | ||
| 115 | void UnregisterFactory(const std::string& name) { | ||
| 116 | if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) { | ||
| 117 | LOG_ERROR(Input, "Factory '{}' not registered", name); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | /** | ||
| 122 | * Create an input device from given paramters. | ||
| 123 | * @tparam InputDeviceType the type of input devices to create | ||
| 124 | * @param params a serialized ParamPackage string contains all parameters for creating the device | ||
| 125 | */ | ||
| 126 | template <typename InputDeviceType> | ||
| 127 | std::unique_ptr<InputDeviceType> CreateDevice(const std::string& params) { | ||
| 128 | const Common::ParamPackage package(params); | ||
| 129 | const std::string engine = package.Get("engine", "null"); | ||
| 130 | const auto& factory_list = Impl::FactoryList<InputDeviceType>::list; | ||
| 131 | const auto pair = factory_list.find(engine); | ||
| 132 | if (pair == factory_list.end()) { | ||
| 133 | if (engine != "null") { | ||
| 134 | LOG_ERROR(Input, "Unknown engine name: {}", engine); | ||
| 135 | } | ||
| 136 | return std::make_unique<InputDeviceType>(); | ||
| 137 | } | ||
| 138 | return pair->second->Create(package); | ||
| 139 | } | ||
| 140 | |||
| 141 | /** | ||
| 142 | * A button device is an input device that returns bool as status. | ||
| 143 | * true for pressed; false for released. | ||
| 144 | */ | ||
| 145 | using ButtonDevice = InputDevice<bool>; | ||
| 146 | |||
| 147 | /** | ||
| 148 | * An analog device is an input device that returns a tuple of x and y coordinates as status. The | ||
| 149 | * coordinates are within the unit circle. x+ is defined as right direction, and y+ is defined as up | ||
| 150 | * direction | ||
| 151 | */ | ||
| 152 | using AnalogDevice = InputDevice<std::tuple<float, float>>; | ||
| 153 | |||
| 154 | /** | ||
| 155 | * A vibration device is an input device that returns an unsigned byte as status. | ||
| 156 | * It represents whether the vibration device supports vibration or not. | ||
| 157 | * If the status returns 1, it supports vibration. Otherwise, it does not support vibration. | ||
| 158 | */ | ||
| 159 | using VibrationDevice = InputDevice<u8>; | ||
| 160 | |||
| 161 | /** | ||
| 162 | * A motion status is an object that returns a tuple of accelerometer state vector, | ||
| 163 | * gyroscope state vector, rotation state vector, orientation state matrix and quaterion state | ||
| 164 | * vector. | ||
| 165 | * | ||
| 166 | * For both 3D vectors: | ||
| 167 | * x+ is the same direction as RIGHT on D-pad. | ||
| 168 | * y+ is normal to the touch screen, pointing outward. | ||
| 169 | * z+ is the same direction as UP on D-pad. | ||
| 170 | * | ||
| 171 | * For accelerometer state vector | ||
| 172 | * Units: g (gravitational acceleration) | ||
| 173 | * | ||
| 174 | * For gyroscope state vector: | ||
| 175 | * Orientation is determined by right-hand rule. | ||
| 176 | * Units: deg/sec | ||
| 177 | * | ||
| 178 | * For rotation state vector | ||
| 179 | * Units: rotations | ||
| 180 | * | ||
| 181 | * For orientation state matrix | ||
| 182 | * x vector | ||
| 183 | * y vector | ||
| 184 | * z vector | ||
| 185 | * | ||
| 186 | * For quaternion state vector | ||
| 187 | * xyz vector | ||
| 188 | * w float | ||
| 189 | */ | ||
| 190 | using MotionStatus = std::tuple<Common::Vec3<float>, Common::Vec3<float>, Common::Vec3<float>, | ||
| 191 | std::array<Common::Vec3f, 3>, Common::Quaternion<f32>>; | ||
| 192 | |||
| 193 | /** | ||
| 194 | * A motion device is an input device that returns a motion status object | ||
| 195 | */ | ||
| 196 | using MotionDevice = InputDevice<MotionStatus>; | ||
| 197 | |||
| 198 | /** | ||
| 199 | * A touch status is an object that returns an array of 16 tuple elements of two floats and a bool. | ||
| 200 | * The floats are x and y coordinates in the range 0.0 - 1.0, and the bool indicates whether it is | ||
| 201 | * pressed. | ||
| 202 | */ | ||
| 203 | using TouchStatus = std::array<std::tuple<float, float, bool>, 16>; | ||
| 204 | |||
| 205 | /** | ||
| 206 | * A touch device is an input device that returns a touch status object | ||
| 207 | */ | ||
| 208 | using TouchDevice = InputDevice<TouchStatus>; | ||
| 209 | |||
| 210 | /** | ||
| 211 | * A mouse device is an input device that returns a tuple of two floats and four ints. | ||
| 212 | * The first two floats are X and Y device coordinates of the mouse (from 0-1). | ||
| 213 | * The s32s are the mouse wheel. | ||
| 214 | */ | ||
| 215 | using MouseDevice = InputDevice<std::tuple<float, float, s32, s32>>; | ||
| 216 | |||
| 217 | } // namespace Input | ||