MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
InputBackend.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6
7/**
8 * @brief Callback signature for input events
9 */
10using InputCallback = std::function<void(const InputValue&)>;
11
12/**
13 * @brief Callback signature for device connection/disconnection events
14 */
15using DeviceCallback = std::function<void(const InputDeviceInfo&, bool connected)>;
16
17/**
18 * @brief Encode a null-terminated wide string as UTF-8.
19 * @param ws Wide string such as a HIDAPI product string or error message, or null.
20 * @return UTF-8 text; empty for null. Unpaired surrogates and invalid code
21 * points become '?'.
22 *
23 * Handles both 16-bit (Windows) and 32-bit wchar_t.
24 */
25MAYAFLUX_API std::string wide_to_utf8(const wchar_t* ws);
26
27/**
28 * @class IInputBackend
29 * @brief Abstract interface for input device backends
30 *
31 * Follows the same pattern as IAudioBackend and IGraphicsBackend.
32 * Each concrete implementation (HID, MIDI, OSC, Serial) provides:
33 * - Device enumeration
34 * - Connection lifecycle management
35 * - Input event delivery via callbacks
36 *
37 * Unlike audio (which has separate AudioDevice/AudioStream classes),
38 * input backends unify device management and data flow since input
39 * devices are typically simpler and don't require the same level of
40 * configuration as audio streams.
41 */
42class MAYAFLUX_API IInputBackend {
43public:
44 virtual ~IInputBackend() = default;
45
46 // ─────────────────────────────────────────────────────────────────────
47 // Lifecycle
48 // ─────────────────────────────────────────────────────────────────────
49
50 /**
51 * @brief Initialize the input backend
52 * @return true if initialization succeeded
53 *
54 * Should discover available devices but not start polling/listening.
55 */
56 virtual bool initialize() = 0;
57
58 /**
59 * @brief Start listening for input events
60 *
61 * Begins polling/callback registration for all opened devices.
62 * Input events will be delivered via registered callbacks.
63 */
64 virtual void start() = 0;
65
66 /**
67 * @brief Stop listening for input events
68 *
69 * Pauses input delivery without closing devices.
70 */
71 virtual void stop() = 0;
72
73 /**
74 * @brief Shutdown and release all resources
75 *
76 * Closes all devices and releases backend resources.
77 * After this call, initialize() must be called again to use the backend.
78 */
79 virtual void shutdown() = 0;
80
81 /**
82 * @brief Check if backend is initialized
83 */
84 [[nodiscard]] virtual bool is_initialized() const = 0;
85
86 /**
87 * @brief Check if backend is actively listening
88 */
89 [[nodiscard]] virtual bool is_running() const = 0;
90
91 // ─────────────────────────────────────────────────────────────────────
92 // Device Management
93 // ─────────────────────────────────────────────────────────────────────
94
95 /**
96 * @brief Get list of available devices
97 * @return Vector of device info structures
98 *
99 * Returns cached device list. Call refresh_devices() to update.
100 */
101 [[nodiscard]] virtual std::vector<InputDeviceInfo> get_devices() const = 0;
102
103 /**
104 * @brief Refresh the device list
105 * @return Number of devices found
106 *
107 * Re-enumerates available devices. May trigger device callbacks
108 * for newly connected or disconnected devices.
109 */
110 virtual size_t refresh_devices() = 0;
111
112 /**
113 * @brief Open a device for input
114 * @param device_id Device identifier from get_devices()
115 * @return true if device was opened successfully
116 */
117 virtual bool open_device(uint32_t device_id) = 0;
118
119 /**
120 * @brief Close a previously opened device
121 * @param device_id Device identifier
122 */
123 virtual void close_device(uint32_t device_id) = 0;
124
125 /**
126 * @brief Check if a device is currently open
127 */
128 [[nodiscard]] virtual bool is_device_open(uint32_t device_id) const = 0;
129
130 /**
131 * @brief Get list of currently open device IDs
132 */
133 [[nodiscard]] virtual std::vector<uint32_t> get_open_devices() const = 0;
134
135 // ─────────────────────────────────────────────────────────────────────
136 // Callbacks
137 // ─────────────────────────────────────────────────────────────────────
138
139 /**
140 * @brief Register callback for input values
141 * @param callback Function to call when input arrives
142 *
143 * The callback may be called from a backend-specific thread.
144 * Implementations should document their threading model.
145 */
147
148 /**
149 * @brief Register callback for device connect/disconnect events
150 * @param callback Function to call when device state changes
151 */
153
154 // ─────────────────────────────────────────────────────────────────────
155 // Backend Information
156 // ─────────────────────────────────────────────────────────────────────
157
158 /**
159 * @brief Get backend type
160 */
161 [[nodiscard]] virtual InputType get_type() const = 0;
162
163 /**
164 * @brief Get backend name/identifier string
165 */
166 [[nodiscard]] virtual std::string get_name() const = 0;
167
168 /**
169 * @brief Get backend version string
170 */
171 [[nodiscard]] virtual std::string get_version() const = 0;
172};
173
174} // namespace MayaFlux::Core
PickCallback callback
Definition Picker.cpp:22
virtual bool is_initialized() const =0
Check if backend is initialized.
virtual std::vector< InputDeviceInfo > get_devices() const =0
Get list of available devices.
virtual bool is_device_open(uint32_t device_id) const =0
Check if a device is currently open.
virtual void set_device_callback(DeviceCallback callback)=0
Register callback for device connect/disconnect events.
virtual InputType get_type() const =0
Get backend type.
virtual std::string get_name() const =0
Get backend name/identifier string.
virtual bool open_device(uint32_t device_id)=0
Open a device for input.
virtual void set_input_callback(InputCallback callback)=0
Register callback for input values.
virtual void stop()=0
Stop listening for input events.
virtual bool initialize()=0
Initialize the input backend.
virtual std::vector< uint32_t > get_open_devices() const =0
Get list of currently open device IDs.
virtual ~IInputBackend()=default
virtual void start()=0
Start listening for input events.
virtual void shutdown()=0
Shutdown and release all resources.
virtual size_t refresh_devices()=0
Refresh the device list.
virtual std::string get_version() const =0
Get backend version string.
virtual bool is_running() const =0
Check if backend is actively listening.
virtual void close_device(uint32_t device_id)=0
Close a previously opened device.
Abstract interface for input device backends.
InputType
Input backend type enumeration.
std::function< void(const InputValue &)> InputCallback
Callback signature for input events.
std::function< void(const InputDeviceInfo &, bool connected)> DeviceCallback
Callback signature for device connection/disconnection events.
MAYAFLUX_API std::string wide_to_utf8(const wchar_t *ws)
Encode a null-terminated wide string as UTF-8.
Information about a connected input device.
Generic input value container.