Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/ControllerInterface.h

raw symbols SCIP
1 // Copyright 2010 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <atomic>
7 #include <functional>
8 #include <memory>
9 #include <mutex>
10
11 #include "Common/HookableEvent.h"
12 #include "Common/Matrix.h"
13 #include "Common/WindowSystemInfo.h"
14
15 #include "InputCommon/ControllerInterface/CoreDevice.h"
16 #include "InputCommon/ControllerInterface/InputBackend.h"
17
18 // enable disable sources
19 #ifdef _WIN32
20 #define CIFACE_USE_WIN32
21 #endif
22 #ifdef HAVE_X11
23 #define CIFACE_USE_XLIB
24 #endif
25 #if defined(__APPLE__)
26 #define CIFACE_USE_OSX
27 #endif
28 #if defined(HAVE_LIBEVDEV) && defined(HAVE_LIBUDEV)
29 #define CIFACE_USE_EVDEV
30 #endif
31 #if defined(USE_PIPES)
32 #define CIFACE_USE_PIPES
33 #endif
34 #define CIFACE_USE_DUALSHOCKUDPCLIENT
35 #if defined(HAVE_SDL3)
36 #define CIFACE_USE_SDL
37 #endif
38 #if defined(HAVE_HIDAPI)
39 #define CIFACE_USE_STEAMDECK
40 #endif
41
42 namespace ciface
43 {
44 // A thread local "input channel" is maintained to handle the state of relative inputs.
45 // This allows simultaneous use of relative inputs across different input contexts.
46 // e.g. binding relative mouse movements to both GameCube controllers and FreeLook.
47 // These operate at different rates and processing one would break the other without separate state.
48 enum class InputChannel
49 {
50 Host,
51 SerialInterface,
52 Bluetooth,
53 FreeLook,
54 Count,
55 };
56
57 } // namespace ciface
58
59 //
60 // ControllerInterface
61 //
62 // Some crazy shit I made to control different device inputs and outputs
63 // from lots of different sources, hopefully more easily.
64 //
65 class ControllerInterface : public ciface::Core::DeviceContainer
66 {
67 public:
68 enum class WindowChangeReason
69 {
70 // Application is shutting down
71 Exit,
72 Other
73 };
74
75 enum class RefreshReason
76 {
77 // Only the window changed.
78 WindowChangeOnly,
79 // User requested, or any other internal reason (e.g. init).
80 // The window might have changed anyway.
81 Other
82 };
83
84 ControllerInterface() : m_is_init(false) {}
85 void Initialize(const WindowSystemInfo& wsi);
86 // Only call from one thread at a time.
87 void ChangeWindow(void* hwnd, WindowChangeReason reason = WindowChangeReason::Other);
88 // Can be called by any thread at any time (when initialized).
89 void RefreshDevices(RefreshReason reason = RefreshReason::Other);
90 void Shutdown();
91 bool AddDevice(std::shared_ptr<ciface::Core::Device> device);
92 // Removes all the devices the function returns true to.
93 // If all the devices shared ptrs need to be destroyed immediately,
94 // set force_devices_release to true.
95 void RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback,
96 bool force_devices_release = false);
97 // This is mandatory to use on device populations functions that can be called concurrently by
98 // more than one thread, or that are called by a single other thread.
99 // Without this, our devices list might end up in a mixed state.
100 void PlatformPopulateDevices(const std::function<void()>& callback);
101 bool IsInit() const { return m_is_init; }
102 void UpdateInput();
103
104 // Set adjustment from the full render window aspect-ratio to the drawn aspect-ratio.
105 // Used to fit mouse cursor inputs to the relevant region of the render window.
106 void SetAspectRatioAdjustment(float);
107
108 // Calculated from the aspect-ratio adjustment.
109 // Inputs based on window coordinates should be multiplied by this.
110 Common::Vec2 GetWindowInputScale() const;
111
112 // Request that the mouse cursor should be centered in the render window at the next opportunity.
113 void SetMouseCenteringRequested(bool center);
114
115 bool IsMouseCenteringRequested() const;
116
117 [[nodiscard]] Common::EventHook
118 RegisterDevicesChangedCallback(Common::HookableEvent<>::CallbackType callback);
119
120 static void SetCurrentInputChannel(ciface::InputChannel);
121 static ciface::InputChannel GetCurrentInputChannel();
122
123 WindowSystemInfo GetWindowSystemInfo() const;
124
125 private:
126 void ClearDevices();
127
128 void InvokeDevicesChangedCallbacks();
129
130 Common::HookableEvent<> m_devices_changed_event;
131
132 mutable std::recursive_mutex m_devices_population_mutex;
133 std::atomic<bool> m_is_init;
134 // This is now always protected by m_devices_population_mutex, so
135 // it doesn't really need to be a counter or atomic anymore (it could be a raw bool),
136 // but we keep it so for simplicity, in case we changed the design.
137 std::atomic<int> m_populating_devices_counter;
138 WindowSystemInfo m_wsi;
139 std::atomic<float> m_aspect_ratio_adjustment = 1;
140 std::atomic<bool> m_requested_mouse_centering = false;
141
142 std::vector<std::unique_ptr<ciface::InputBackend>> m_input_backends;
143 };
144
145 namespace ciface
146 {
147 template <typename T>
148 class RelativeInputState
149 {
150 public:
151 void Update()
152 {
153 const auto channel = int(ControllerInterface::GetCurrentInputChannel());
154
155 m_value[channel] = m_delta[channel];
156 m_delta[channel] = {};
157 }
158
159 T GetValue() const
160 {
161 const auto channel = int(ControllerInterface::GetCurrentInputChannel());
162
163 return m_value[channel];
164 }
165
166 void Move(T delta)
167 {
168 for (auto& d : m_delta)
169 d += delta;
170 }
171
172 private:
173 std::array<T, int(InputChannel::Count)> m_value;
174 std::array<T, int(InputChannel::Count)> m_delta;
175 };
176 } // namespace ciface
177
178 extern ControllerInterface g_controller_interface;