Skip to main content

openlogi_hid/
hotplug.rs

1//! OS HID hotplug events, bridged from the shared `async-hid` backend.
2
3use futures_lite::{Stream, StreamExt as _};
4
5use crate::inventory::InventoryError;
6use crate::transport::hid_backend;
7
8/// A HID node appeared on or vanished from the OS device tree.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum HotplugEvent {
11    /// A device node was connected.
12    Connected,
13    /// A device node was disconnected.
14    Disconnected,
15}
16
17/// Subscribe to OS HID hotplug events through the shared process-wide backend.
18pub fn watch_hotplug() -> Result<impl Stream<Item = HotplugEvent> + Send + Unpin, InventoryError> {
19    let stream = hid_backend().watch().map_err(InventoryError::Hid)?;
20    Ok(stream.map(|event| match event {
21        async_hid::DeviceEvent::Connected(_) => HotplugEvent::Connected,
22        async_hid::DeviceEvent::Disconnected(_) => HotplugEvent::Disconnected,
23    }))
24}