diff options
| author | 2021-11-27 11:52:08 +0100 | |
|---|---|---|
| committer | 2021-11-27 11:52:08 +0100 | |
| commit | 564f10527745f870621c08bbb5d16badee0ed861 (patch) | |
| tree | e8ac8dee60086facf1837393882865f5df18c95e /src/input_common/input_engine.h | |
| parent | Merge pull request #7431 from liushuyu/fix-linux-decoding (diff) | |
| parent | config: Remove vibration configuration (diff) | |
| download | yuzu-564f10527745f870621c08bbb5d16badee0ed861.tar.gz yuzu-564f10527745f870621c08bbb5d16badee0ed861.tar.xz yuzu-564f10527745f870621c08bbb5d16badee0ed861.zip | |
Merge pull request #7255 from german77/kraken
Project Kraken: Input rewrite
Diffstat (limited to 'src/input_common/input_engine.h')
| -rw-r--r-- | src/input_common/input_engine.h | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h new file mode 100644 index 000000000..02272b3f8 --- /dev/null +++ b/src/input_common/input_engine.h | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | // Copyright 2021 yuzu 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 <mutex> | ||
| 9 | #include <unordered_map> | ||
| 10 | |||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "common/input.h" | ||
| 13 | #include "common/param_package.h" | ||
| 14 | #include "common/uuid.h" | ||
| 15 | #include "input_common/main.h" | ||
| 16 | |||
| 17 | // Pad Identifier of data source | ||
| 18 | struct PadIdentifier { | ||
| 19 | Common::UUID guid{}; | ||
| 20 | std::size_t port{}; | ||
| 21 | std::size_t pad{}; | ||
| 22 | |||
| 23 | friend constexpr bool operator==(const PadIdentifier&, const PadIdentifier&) = default; | ||
| 24 | }; | ||
| 25 | |||
| 26 | // Basic motion data containing data from the sensors and a timestamp in microsecons | ||
| 27 | struct BasicMotion { | ||
| 28 | float gyro_x; | ||
| 29 | float gyro_y; | ||
| 30 | float gyro_z; | ||
| 31 | float accel_x; | ||
| 32 | float accel_y; | ||
| 33 | float accel_z; | ||
| 34 | u64 delta_timestamp; | ||
| 35 | }; | ||
| 36 | |||
| 37 | // Stages of a battery charge | ||
| 38 | enum class BatteryLevel { | ||
| 39 | Empty, | ||
| 40 | Critical, | ||
| 41 | Low, | ||
| 42 | Medium, | ||
| 43 | Full, | ||
| 44 | Charging, | ||
| 45 | }; | ||
| 46 | |||
| 47 | // Types of input that are stored in the engine | ||
| 48 | enum class EngineInputType { | ||
| 49 | None, | ||
| 50 | Button, | ||
| 51 | HatButton, | ||
| 52 | Analog, | ||
| 53 | Motion, | ||
| 54 | Battery, | ||
| 55 | }; | ||
| 56 | |||
| 57 | namespace std { | ||
| 58 | // Hash used to create lists from PadIdentifier data | ||
| 59 | template <> | ||
| 60 | struct hash<PadIdentifier> { | ||
| 61 | size_t operator()(const PadIdentifier& pad_id) const noexcept { | ||
| 62 | u64 hash_value = pad_id.guid.uuid[1] ^ pad_id.guid.uuid[0]; | ||
| 63 | hash_value ^= (static_cast<u64>(pad_id.port) << 32); | ||
| 64 | hash_value ^= static_cast<u64>(pad_id.pad); | ||
| 65 | return static_cast<size_t>(hash_value); | ||
| 66 | } | ||
| 67 | }; | ||
| 68 | |||
| 69 | } // namespace std | ||
| 70 | |||
| 71 | namespace InputCommon { | ||
| 72 | |||
| 73 | // Data from the engine and device needed for creating a ParamPackage | ||
| 74 | struct MappingData { | ||
| 75 | std::string engine{}; | ||
| 76 | PadIdentifier pad{}; | ||
| 77 | EngineInputType type{}; | ||
| 78 | int index{}; | ||
| 79 | bool button_value{}; | ||
| 80 | std::string hat_name{}; | ||
| 81 | f32 axis_value{}; | ||
| 82 | BasicMotion motion_value{}; | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Triggered if data changed on the controller | ||
| 86 | struct UpdateCallback { | ||
| 87 | std::function<void()> on_change; | ||
| 88 | }; | ||
| 89 | |||
| 90 | // Triggered if data changed on the controller and the engine is on configuring mode | ||
| 91 | struct MappingCallback { | ||
| 92 | std::function<void(MappingData)> on_data; | ||
| 93 | }; | ||
| 94 | |||
| 95 | // Input Identifier of data source | ||
| 96 | struct InputIdentifier { | ||
| 97 | PadIdentifier identifier; | ||
| 98 | EngineInputType type; | ||
| 99 | int index; | ||
| 100 | UpdateCallback callback; | ||
| 101 | }; | ||
| 102 | |||
| 103 | class InputEngine { | ||
| 104 | public: | ||
| 105 | explicit InputEngine(const std::string& input_engine_) : input_engine(input_engine_) { | ||
| 106 | callback_list.clear(); | ||
| 107 | } | ||
| 108 | |||
| 109 | virtual ~InputEngine() = default; | ||
| 110 | |||
| 111 | // Enable configuring mode for mapping | ||
| 112 | void BeginConfiguration(); | ||
| 113 | |||
| 114 | // Disable configuring mode for mapping | ||
| 115 | void EndConfiguration(); | ||
| 116 | |||
| 117 | // Sets a led pattern for a controller | ||
| 118 | virtual void SetLeds([[maybe_unused]] const PadIdentifier& identifier, | ||
| 119 | [[maybe_unused]] const Common::Input::LedStatus led_status) { | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | // Sets rumble to a controller | ||
| 124 | virtual Common::Input::VibrationError SetRumble( | ||
| 125 | [[maybe_unused]] const PadIdentifier& identifier, | ||
| 126 | [[maybe_unused]] const Common::Input::VibrationStatus vibration) { | ||
| 127 | return Common::Input::VibrationError::NotSupported; | ||
| 128 | } | ||
| 129 | |||
| 130 | // Sets polling mode to a controller | ||
| 131 | virtual Common::Input::PollingError SetPollingMode( | ||
| 132 | [[maybe_unused]] const PadIdentifier& identifier, | ||
| 133 | [[maybe_unused]] const Common::Input::PollingMode vibration) { | ||
| 134 | return Common::Input::PollingError::NotSupported; | ||
| 135 | } | ||
| 136 | |||
| 137 | // Returns the engine name | ||
| 138 | [[nodiscard]] const std::string& GetEngineName() const; | ||
| 139 | |||
| 140 | /// Used for automapping features | ||
| 141 | virtual std::vector<Common::ParamPackage> GetInputDevices() const { | ||
| 142 | return {}; | ||
| 143 | }; | ||
| 144 | |||
| 145 | /// Retrieves the button mappings for the given device | ||
| 146 | virtual InputCommon::ButtonMapping GetButtonMappingForDevice( | ||
| 147 | [[maybe_unused]] const Common::ParamPackage& params) { | ||
| 148 | return {}; | ||
| 149 | }; | ||
| 150 | |||
| 151 | /// Retrieves the analog mappings for the given device | ||
| 152 | virtual InputCommon::AnalogMapping GetAnalogMappingForDevice( | ||
| 153 | [[maybe_unused]] const Common::ParamPackage& params) { | ||
| 154 | return {}; | ||
| 155 | }; | ||
| 156 | |||
| 157 | /// Retrieves the motion mappings for the given device | ||
| 158 | virtual InputCommon::MotionMapping GetMotionMappingForDevice( | ||
| 159 | [[maybe_unused]] const Common::ParamPackage& params) { | ||
| 160 | return {}; | ||
| 161 | }; | ||
| 162 | |||
| 163 | /// Retrieves the name of the given input. | ||
| 164 | virtual Common::Input::ButtonNames GetUIName( | ||
| 165 | [[maybe_unused]] const Common::ParamPackage& params) const { | ||
| 166 | return Common::Input::ButtonNames::Engine; | ||
| 167 | }; | ||
| 168 | |||
| 169 | /// Retrieves the index number of the given hat button direction | ||
| 170 | virtual u8 GetHatButtonId([[maybe_unused]] const std::string& direction_name) const { | ||
| 171 | return 0; | ||
| 172 | }; | ||
| 173 | |||
| 174 | void PreSetController(const PadIdentifier& identifier); | ||
| 175 | void PreSetButton(const PadIdentifier& identifier, int button); | ||
| 176 | void PreSetHatButton(const PadIdentifier& identifier, int button); | ||
| 177 | void PreSetAxis(const PadIdentifier& identifier, int axis); | ||
| 178 | void PreSetMotion(const PadIdentifier& identifier, int motion); | ||
| 179 | void ResetButtonState(); | ||
| 180 | void ResetAnalogState(); | ||
| 181 | |||
| 182 | bool GetButton(const PadIdentifier& identifier, int button) const; | ||
| 183 | bool GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const; | ||
| 184 | f32 GetAxis(const PadIdentifier& identifier, int axis) const; | ||
| 185 | BatteryLevel GetBattery(const PadIdentifier& identifier) const; | ||
| 186 | BasicMotion GetMotion(const PadIdentifier& identifier, int motion) const; | ||
| 187 | |||
| 188 | int SetCallback(InputIdentifier input_identifier); | ||
| 189 | void SetMappingCallback(MappingCallback callback); | ||
| 190 | void DeleteCallback(int key); | ||
| 191 | |||
| 192 | protected: | ||
| 193 | void SetButton(const PadIdentifier& identifier, int button, bool value); | ||
| 194 | void SetHatButton(const PadIdentifier& identifier, int button, u8 value); | ||
| 195 | void SetAxis(const PadIdentifier& identifier, int axis, f32 value); | ||
| 196 | void SetBattery(const PadIdentifier& identifier, BatteryLevel value); | ||
| 197 | void SetMotion(const PadIdentifier& identifier, int motion, BasicMotion value); | ||
| 198 | |||
| 199 | virtual std::string GetHatButtonName([[maybe_unused]] u8 direction_value) const { | ||
| 200 | return "Unknown"; | ||
| 201 | } | ||
| 202 | |||
| 203 | private: | ||
| 204 | struct ControllerData { | ||
| 205 | std::unordered_map<int, bool> buttons; | ||
| 206 | std::unordered_map<int, u8> hat_buttons; | ||
| 207 | std::unordered_map<int, float> axes; | ||
| 208 | std::unordered_map<int, BasicMotion> motions; | ||
| 209 | BatteryLevel battery; | ||
| 210 | }; | ||
| 211 | |||
| 212 | void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value); | ||
| 213 | void TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value); | ||
| 214 | void TriggerOnAxisChange(const PadIdentifier& identifier, int button, f32 value); | ||
| 215 | void TriggerOnBatteryChange(const PadIdentifier& identifier, BatteryLevel value); | ||
| 216 | void TriggerOnMotionChange(const PadIdentifier& identifier, int motion, BasicMotion value); | ||
| 217 | |||
| 218 | bool IsInputIdentifierEqual(const InputIdentifier& input_identifier, | ||
| 219 | const PadIdentifier& identifier, EngineInputType type, | ||
| 220 | int index) const; | ||
| 221 | |||
| 222 | mutable std::mutex mutex; | ||
| 223 | mutable std::mutex mutex_callback; | ||
| 224 | bool configuring{false}; | ||
| 225 | const std::string input_engine; | ||
| 226 | int last_callback_key = 0; | ||
| 227 | std::unordered_map<PadIdentifier, ControllerData> controller_list; | ||
| 228 | std::unordered_map<int, InputIdentifier> callback_list; | ||
| 229 | MappingCallback mapping_callback; | ||
| 230 | }; | ||
| 231 | |||
| 232 | } // namespace InputCommon | ||