MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ refresh_devices()

size_t MayaFlux::Core::HIDBackend::refresh_devices ( )
overridevirtual

Refresh the device list.

Returns
Number of devices found

Re-enumerates available devices. May trigger device callbacks for newly connected or disconnected devices.

Implements MayaFlux::Core::IInputBackend.

Definition at line 131 of file HIDBackend.cpp.

132{
133 if (!m_initialized.load()) {
134 return 0;
135 }
136
137 std::lock_guard lock(m_devices_mutex);
138
139 std::unordered_set<std::string> previous_paths;
140 for (const auto& [id, info] : m_enumerated_devices) {
141 previous_paths.insert(info.path);
142 }
143
144 std::unordered_set<std::string> current_paths;
145
146 hid_device_info* devs = hid_enumerate(0x0, 0x0);
147 hid_device_info* cur = devs;
148
149 while (cur) {
150 if (!m_config.filters.empty()) {
151 if (!matches_any_filter(cur->vendor_id, cur->product_id,
152 cur->usage_page, cur->usage)) {
153 cur = cur->next;
154 continue;
155 }
156 }
157
158 std::string path(cur->path);
159 current_paths.insert(path);
160
161 bool is_new = (previous_paths.find(path) == previous_paths.end());
162
163 uint32_t dev_id = find_or_assign_device_id(path);
164
165 HIDDeviceInfoExt info;
166 info.id = dev_id;
167 info.backend_type = InputType::HID;
168 info.vendor_id = cur->vendor_id;
169 info.product_id = cur->product_id;
170 info.usage_page = cur->usage_page;
171 info.usage = cur->usage;
172 info.release_number = cur->release_number;
173 info.interface_number = cur->interface_number;
174 info.path = path;
175 info.is_connected = true;
176
177 if (cur->manufacturer_string) {
178 std::wstring ws(cur->manufacturer_string);
179 info.manufacturer.resize(ws.length());
180 std::ranges::transform(ws, info.manufacturer.begin(), [](wchar_t c) { return static_cast<char>(c); });
181 }
182 if (cur->product_string) {
183 std::wstring ws(cur->product_string);
184 info.name.resize(ws.length());
185 std::ranges::transform(ws, info.name.begin(), [](wchar_t c) { return static_cast<char>(c); });
186 } else {
187 info.name = "HID Device " + std::to_string(cur->vendor_id) + ":" + std::to_string(cur->product_id);
188 }
189 if (cur->serial_number) {
190 std::wstring ws(cur->serial_number);
191 info.serial_number.resize(ws.length());
192 std::ranges::transform(ws, info.serial_number.begin(), [](wchar_t c) { return static_cast<char>(c); });
193 }
194
195 m_enumerated_devices[dev_id] = info;
196
197 if (is_new) {
199 "HID device found: {} (VID:{:04X} PID:{:04X})",
200 info.name, info.vendor_id, info.product_id);
201 notify_device_change(info, true);
202 }
203
204 cur = cur->next;
205 }
206
207 hid_free_enumeration(devs);
208
209 for (auto it = m_enumerated_devices.begin(); it != m_enumerated_devices.end();) {
210 if (current_paths.find(it->second.path) == current_paths.end()) {
212 "HID device disconnected: {}", it->second.name);
213
214 auto open_it = m_open_devices.find(it->first);
215 if (open_it != m_open_devices.end()) {
216 if (open_it->second->handle) {
217 hid_close(open_it->second->handle);
218 }
219 m_open_devices.erase(open_it);
220 }
221
222 notify_device_change(it->second, false);
223 it = m_enumerated_devices.erase(it);
224 } else {
225 ++it;
226 }
227 }
228
229 return m_enumerated_devices.size();
230}
#define MF_INFO(comp, ctx,...)
bool matches_any_filter(uint16_t vid, uint16_t pid, uint16_t usage_page, uint16_t usage) const
std::atomic< bool > m_initialized
uint32_t find_or_assign_device_id(const std::string &path)
std::unordered_map< uint32_t, HIDDeviceInfoExt > m_enumerated_devices
void notify_device_change(const InputDeviceInfo &info, bool connected)
std::unordered_map< uint32_t, std::shared_ptr< HIDDeviceState > > m_open_devices
@ HID
Generic HID devices (game controllers, custom hardware)
@ InputBackend
Input device backend (HID, MIDI, OSC)
@ Core
Core engine, backend, subsystems.
std::vector< HIDDeviceFilter > filters
Device filters (empty = all devices)

References MayaFlux::Core::InputDeviceInfo::backend_type, MayaFlux::Journal::Core, MayaFlux::Core::HIDBackend::Config::filters, find_or_assign_device_id(), MayaFlux::Core::HID, MayaFlux::Core::InputDeviceInfo::id, MayaFlux::Journal::InputBackend, MayaFlux::Core::HIDDeviceInfoExt::interface_number, MayaFlux::Core::InputDeviceInfo::is_connected, m_config, m_devices_mutex, m_enumerated_devices, m_initialized, m_open_devices, MayaFlux::Core::InputDeviceInfo::manufacturer, matches_any_filter(), MF_INFO, MayaFlux::Core::InputDeviceInfo::name, notify_device_change(), MayaFlux::Core::HIDDeviceInfoExt::path, MayaFlux::Core::InputDeviceInfo::product_id, MayaFlux::Core::HIDDeviceInfoExt::release_number, MayaFlux::Core::InputDeviceInfo::serial_number, MayaFlux::Core::HIDDeviceInfoExt::usage, MayaFlux::Core::HIDDeviceInfoExt::usage_page, and MayaFlux::Core::InputDeviceInfo::vendor_id.

Referenced by initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: