summaryrefslogtreecommitdiff
path: root/src/input_common/main.h
diff options
context:
space:
mode:
authorGravatar Lioncash2020-08-27 15:16:47 -0400
committerGravatar Lioncash2020-08-27 16:11:17 -0400
commit9e1b0af25907f7a8b960aa5c1e7d931691f40196 (patch)
tree9bfda0b559cb025da3bc65168d28ee78144daa20 /src/input_common/main.h
parentMerge pull request #4530 from Morph1984/mjolnir-p1 (diff)
downloadyuzu-9e1b0af25907f7a8b960aa5c1e7d931691f40196.tar.gz
yuzu-9e1b0af25907f7a8b960aa5c1e7d931691f40196.tar.xz
yuzu-9e1b0af25907f7a8b960aa5c1e7d931691f40196.zip
input_common: Eliminate most global state
Abstracts most of the input mechanisms under an InputSubsystem class that is managed by the frontends, eliminating any static constructors and destructors. This gets rid of global accessor functions and also allows the frontends to have a more fine-grained control over the lifecycle of the input subsystem. This also makes it explicit which interfaces rely on the input subsystem instead of making it opaque in the interface functions. All that remains to migrate over is the factories, which can be done in a separate change.
Diffstat (limited to 'src/input_common/main.h')
-rw-r--r--src/input_common/main.h130
1 files changed, 84 insertions, 46 deletions
diff --git a/src/input_common/main.h b/src/input_common/main.h
index e706c3750..f66308163 100644
--- a/src/input_common/main.h
+++ b/src/input_common/main.h
@@ -16,52 +16,6 @@ class ParamPackage;
16} 16}
17 17
18namespace InputCommon { 18namespace InputCommon {
19
20/// Initializes and registers all built-in input device factories.
21void Init();
22
23/// Deregisters all built-in input device factories and shuts them down.
24void Shutdown();
25
26class Keyboard;
27
28/// Gets the keyboard button device factory.
29Keyboard* GetKeyboard();
30
31class MotionEmu;
32
33/// Gets the motion emulation factory.
34MotionEmu* GetMotionEmu();
35
36GCButtonFactory* GetGCButtons();
37
38GCAnalogFactory* GetGCAnalogs();
39
40/// Generates a serialized param package for creating a keyboard button device
41std::string GenerateKeyboardParam(int key_code);
42
43/// Generates a serialized param package for creating an analog device taking input from keyboard
44std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
45 int key_modifier, float modifier_scale);
46
47/**
48 * Return a list of available input devices that this Factory can create a new device with.
49 * Each returned Parampackage should have a `display` field used for display, a class field for
50 * backends to determine if this backend is meant to service the request and any other information
51 * needed to identify this in the backend later.
52 */
53std::vector<Common::ParamPackage> GetInputDevices();
54
55/**
56 * Given a ParamPackage for a Device returned from `GetInputDevices`, attempt to get the default
57 * mapping for the device. This is currently only implemented for the sdl backend devices.
58 */
59using ButtonMapping = std::unordered_map<Settings::NativeButton::Values, Common::ParamPackage>;
60using AnalogMapping = std::unordered_map<Settings::NativeAnalog::Values, Common::ParamPackage>;
61
62ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage&);
63AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage&);
64
65namespace Polling { 19namespace Polling {
66 20
67enum class DeviceType { Button, AnalogPreferred }; 21enum class DeviceType { Button, AnalogPreferred };
@@ -90,4 +44,88 @@ public:
90// Get all DevicePoller from all backends for a specific device type 44// Get all DevicePoller from all backends for a specific device type
91std::vector<std::unique_ptr<DevicePoller>> GetPollers(DeviceType type); 45std::vector<std::unique_ptr<DevicePoller>> GetPollers(DeviceType type);
92} // namespace Polling 46} // namespace Polling
47
48class GCAnalogFactory;
49class GCButtonFactory;
50class Keyboard;
51class MotionEmu;
52
53/**
54 * Given a ParamPackage for a Device returned from `GetInputDevices`, attempt to get the default
55 * mapping for the device. This is currently only implemented for the SDL backend devices.
56 */
57using AnalogMapping = std::unordered_map<Settings::NativeAnalog::Values, Common::ParamPackage>;
58using ButtonMapping = std::unordered_map<Settings::NativeButton::Values, Common::ParamPackage>;
59
60class InputSubsystem {
61public:
62 explicit InputSubsystem();
63 ~InputSubsystem();
64
65 InputSubsystem(const InputSubsystem&) = delete;
66 InputSubsystem& operator=(const InputSubsystem&) = delete;
67
68 InputSubsystem(InputSubsystem&&) = delete;
69 InputSubsystem& operator=(InputSubsystem&&) = delete;
70
71 /// Initializes and registers all built-in input device factories.
72 void Initialize();
73
74 /// Unregisters all built-in input device factories and shuts them down.
75 void Shutdown();
76
77 /// Retrieves the underlying keyboard device.
78 [[nodiscard]] Keyboard* GetKeyboard();
79
80 /// Retrieves the underlying keyboard device.
81 [[nodiscard]] const Keyboard* GetKeyboard() const;
82
83 /// Retrieves the underlying motion emulation factory.
84 [[nodiscard]] MotionEmu* GetMotionEmu();
85
86 /// Retrieves the underlying motion emulation factory.
87 [[nodiscard]] const MotionEmu* GetMotionEmu() const;
88
89 /**
90 * Returns all available input devices that this Factory can create a new device with.
91 * Each returned ParamPackage should have a `display` field used for display, a class field for
92 * backends to determine if this backend is meant to service the request and any other
93 * information needed to identify this in the backend later.
94 */
95 [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const;
96
97 /// Retrieves the analog mappings for the given device.
98 [[nodiscard]] AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& device) const;
99
100 /// Retrieves the button mappings for the given device.
101 [[nodiscard]] ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& device) const;
102
103 /// Retrieves the underlying GameCube analog handler.
104 [[nodiscard]] GCAnalogFactory* GetGCAnalogs();
105
106 /// Retrieves the underlying GameCube analog handler.
107 [[nodiscard]] const GCAnalogFactory* GetGCAnalogs() const;
108
109 /// Retrieves the underlying GameCube button handler.
110 [[nodiscard]] GCButtonFactory* GetGCButtons();
111
112 /// Retrieves the underlying GameCube button handler.
113 [[nodiscard]] const GCButtonFactory* GetGCButtons() const;
114
115 /// Get all DevicePoller from all backends for a specific device type
116 [[nodiscard]] std::vector<std::unique_ptr<Polling::DevicePoller>> GetPollers(
117 Polling::DeviceType type) const;
118
119private:
120 struct Impl;
121 std::unique_ptr<Impl> impl;
122};
123
124/// Generates a serialized param package for creating a keyboard button device
125std::string GenerateKeyboardParam(int key_code);
126
127/// Generates a serialized param package for creating an analog device taking input from keyboard
128std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
129 int key_modifier, float modifier_scale);
130
93} // namespace InputCommon 131} // namespace InputCommon