Expand description
§hidra
A pure-Rust HID library.
hidra talks to HID devices through native OS interfaces:
| Target | Native | nusb (feature nusb) |
|---|---|---|
| Linux | hidraw device nodes + sysfs enumeration | USB interrupt/control transfers via nusb |
| Windows | hid.dll / SetupAPI via windows-sys declarations | as above |
| macOS | IOHIDManager via direct framework FFI | as above |
On WebAssembly the backend is always
WebHID via web-sys.
use hidra::{Hidra, Nusb};
let native = Hidra::new()?; // the OS HID stack
let usb = Hidra::<Nusb>::builder().build()?; // raw USB transfersThey are different types, so a handle that holds either is the caller’s to
declare; examples/backends.rs shows the whole of it.
Following nusb’s model, every Hidra / HidDevice I/O method returns
an impl Future. On native targets bring MaybeFuture into scope to drive
it blocking with .wait(), or .await it under any async runtime (no
executor dependency, wake-ups are plain Wakers like nusb).
On wasm32 the same Hidra / HidDevice types are backed by WebHID;
there is no blocking mode, so always .await their futures (no .wait()).
Discovery is WebHID-shaped: Hidra::request_device shows the browser’s
device chooser (filtered with DeviceFilter) and Hidra::get_devices
lists previously granted devices. descriptor offers report-descriptor
primitives that work everywhere.
use hidra::MaybeFuture;
let api = hidra::Hidra::new()?;
for dev in api.device_list() {
println!("{:04x}:{:04x} {}", dev.vendor_id(), dev.product_id(),
dev.product_string().unwrap_or("<unnamed>"));
}
let device = api.open(0x046d, 0xc216).wait()?;
device.write(&[0x00, 0x01, 0x02]).wait()?; // report ID 0 + payload
let mut buf = [0u8; 64];
let len = device.read(&mut buf).wait()?;Modules§
- descriptor
- HID report descriptor primitives.
- report_
info - WebHID-style collection primitives, also usable standalone.
Structs§
- ApiVersion
- hidra’s version, mirroring
hid_version(). - Device
Filter - WebHID-only public surface: the device filter for
Hidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. WebHID-only public surface: the device filter forHidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. WebHID-only public surface: the device filter forHidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. A device filter forcrate::Hidra::request_device, mirroringWebHID’sHIDDeviceFilterdictionary. Unset fields match anything. - Device
Info - Information about a connected HID device, as returned by enumeration.
- Event
Listener Handle - WebHID-only public surface: the device filter for
Hidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. WebHID-only public surface: the device filter forHidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. WebHID-only public surface: the device filter forHidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. Keeps an event listener (and the Rust closure backing it) alive; removes the listener when dropped. - HidDevice
- An HID device exposed by the browser, backed by
WebHID. - Hidra
- Entry point to the library, backed by
WebHID(navigator.hid). - Input
Report Stream - WebHID-only public surface: the device filter for
Hidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. WebHID-only public surface: the device filter forHidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. WebHID-only public surface: the device filter forHidra::request_device, the listener handle returned by the event hooks, and the buffered input report stream fromHidDevice::start_reading. A buffered reader overinputreportevents, created bycrate::HidDevice::start_reading.
Enums§
Constants§
- MAX_
REPORT_ DESCRIPTOR_ SIZE - Largest report descriptor a HID device can have
(
HID_API_MAX_REPORT_DESCRIPTOR_SIZE).
Functions§
- version
- Library version (
hid_versionequivalent). - version_
str - Library version string (
hid_version_strequivalent).
Type Aliases§
- HidResult
- Convenience alias used by every fallible API in this crate.