diff options
| author | 2017-03-17 14:59:39 -0400 | |
|---|---|---|
| committer | 2017-03-17 14:59:39 -0400 | |
| commit | 423ab5e2bcf5a522e5f412447c05f648df57a14c (patch) | |
| tree | 1e60eaeffa59229254a47f885d2fe2cbbdc1a5c0 /src/core/frontend/input.h | |
| parent | Merge pull request #2618 from wwylele/log-less-filename (diff) | |
| parent | qt/config_input: don't connect for null button (diff) | |
| download | yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.gz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.xz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.zip | |
Merge pull request #2497 from wwylele/input-2
Refactor input emulation & add SDL gamepad support
Diffstat (limited to 'src/core/frontend/input.h')
| -rw-r--r-- | src/core/frontend/input.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h new file mode 100644 index 000000000..0a5713dc0 --- /dev/null +++ b/src/core/frontend/input.h | |||
| @@ -0,0 +1,110 @@ | |||
| 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 <memory> | ||
| 8 | #include <string> | ||
| 9 | #include <tuple> | ||
| 10 | #include <unordered_map> | ||
| 11 | #include <utility> | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | #include "common/param_package.h" | ||
| 14 | |||
| 15 | namespace Input { | ||
| 16 | |||
| 17 | /// An abstract class template for an input device (a button, an analog input, etc.). | ||
| 18 | template <typename StatusType> | ||
| 19 | class InputDevice { | ||
| 20 | public: | ||
| 21 | virtual ~InputDevice() = default; | ||
| 22 | virtual StatusType GetStatus() const { | ||
| 23 | return {}; | ||
| 24 | } | ||
| 25 | }; | ||
| 26 | |||
| 27 | /// An abstract class template for a factory that can create input devices. | ||
| 28 | template <typename InputDeviceType> | ||
| 29 | class Factory { | ||
| 30 | public: | ||
| 31 | virtual ~Factory() = default; | ||
| 32 | virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0; | ||
| 33 | }; | ||
| 34 | |||
| 35 | namespace Impl { | ||
| 36 | |||
| 37 | template <typename InputDeviceType> | ||
| 38 | using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>; | ||
| 39 | |||
| 40 | template <typename InputDeviceType> | ||
| 41 | struct FactoryList { | ||
| 42 | static FactoryListType<InputDeviceType> list; | ||
| 43 | }; | ||
| 44 | |||
| 45 | template <typename InputDeviceType> | ||
| 46 | FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::list; | ||
| 47 | |||
| 48 | } // namespace Impl | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Registers an input device factory. | ||
| 52 | * @tparam InputDeviceType the type of input devices the factory can create | ||
| 53 | * @param name the name of the factory. Will be used to match the "engine" parameter when creating | ||
| 54 | * a device | ||
| 55 | * @param factory the factory object to register | ||
| 56 | */ | ||
| 57 | template <typename InputDeviceType> | ||
| 58 | void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) { | ||
| 59 | auto pair = std::make_pair(name, std::move(factory)); | ||
| 60 | if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) { | ||
| 61 | LOG_ERROR(Input, "Factory %s already registered", name.c_str()); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Unregisters an input device factory. | ||
| 67 | * @tparam InputDeviceType the type of input devices the factory can create | ||
| 68 | * @param name the name of the factory to unregister | ||
| 69 | */ | ||
| 70 | template <typename InputDeviceType> | ||
| 71 | void UnregisterFactory(const std::string& name) { | ||
| 72 | if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) { | ||
| 73 | LOG_ERROR(Input, "Factory %s not registered", name.c_str()); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Create an input device from given paramters. | ||
| 79 | * @tparam InputDeviceType the type of input devices to create | ||
| 80 | * @param params a serialized ParamPackage string contains all parameters for creating the device | ||
| 81 | */ | ||
| 82 | template <typename InputDeviceType> | ||
| 83 | std::unique_ptr<InputDeviceType> CreateDevice(const std::string& params) { | ||
| 84 | const Common::ParamPackage package(params); | ||
| 85 | const std::string engine = package.Get("engine", "null"); | ||
| 86 | const auto& factory_list = Impl::FactoryList<InputDeviceType>::list; | ||
| 87 | const auto pair = factory_list.find(engine); | ||
| 88 | if (pair == factory_list.end()) { | ||
| 89 | if (engine != "null") { | ||
| 90 | LOG_ERROR(Input, "Unknown engine name: %s", engine.c_str()); | ||
| 91 | } | ||
| 92 | return std::make_unique<InputDeviceType>(); | ||
| 93 | } | ||
| 94 | return pair->second->Create(package); | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * A button device is an input device that returns bool as status. | ||
| 99 | * true for pressed; false for released. | ||
| 100 | */ | ||
| 101 | using ButtonDevice = InputDevice<bool>; | ||
| 102 | |||
| 103 | /** | ||
| 104 | * An analog device is an input device that returns a tuple of x and y coordinates as status. The | ||
| 105 | * coordinates are within the unit circle. x+ is defined as right direction, and y+ is defined as up | ||
| 106 | * direction | ||
| 107 | */ | ||
| 108 | using AnalogDevice = InputDevice<std::tuple<float, float>>; | ||
| 109 | |||
| 110 | } // namespace Input | ||