| 1 |
|
| 2 |
|
| 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 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
enum class InputChannel |
| 49 |
{ |
| 50 |
Host, |
| 51 |
SerialInterface, |
| 52 |
Bluetooth, |
| 53 |
FreeLook, |
| 54 |
Count, |
| 55 |
}; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
class ControllerInterface : public ciface::Core::DeviceContainer |
| 66 |
{ |
| 67 |
public: |
| 68 |
enum class WindowChangeReason |
| 69 |
{ |
| 70 |
|
| 71 |
Exit, |
| 72 |
Other |
| 73 |
}; |
| 74 |
|
| 75 |
enum class RefreshReason |
| 76 |
{ |
| 77 |
|
| 78 |
WindowChangeOnly, |
| 79 |
|
| 80 |
|
| 81 |
Other |
| 82 |
}; |
| 83 |
|
| 84 |
ControllerInterface() : m_is_init(false) {} |
| 85 |
void Initialize(const WindowSystemInfo& wsi); |
| 86 |
|
| 87 |
void ChangeWindow(void* hwnd, WindowChangeReason reason = WindowChangeReason::Other); |
| 88 |
|
| 89 |
void RefreshDevices(RefreshReason reason = RefreshReason::Other); |
| 90 |
void Shutdown(); |
| 91 |
bool AddDevice(std::shared_ptr<ciface::Core::Device> device); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback, |
| 96 |
bool force_devices_release = false); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void PlatformPopulateDevices(const std::function<void()>& callback); |
| 101 |
bool IsInit() const { return m_is_init; } |
| 102 |
void UpdateInput(); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void SetAspectRatioAdjustment(float); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
Common::Vec2 GetWindowInputScale() const; |
| 111 |
|
| 112 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 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 |
} |
| 177 |
|
| 178 |
extern ControllerInterface g_controller_interface; |