HID Gadget Emulation in Rust
Rust crate for interfacing with Linux HID Gadget devices (/dev/hidgX).
Since all functionality is dependent on Linux function calls, this crate only compiles for Linux systems.
Crates
- hidg-core - core abstractions and low level interface (not for end users)
- hidg - std interface which supports synchronous operation only
- tokio-hidg - async interface for tokio runtime
- async-hidg - async interface for other runtimes
Features
- fromstr - implements [core::str::FromStr] implementation for some types
- display - implements [std::fmt::Display] implementation for some types
- phf - use phf in [core::str::FromStr] trait implementations
- serde - enables serde support for some types
- keyboard - enables keyboard class support
- mouse - enables mouse class support
Usage examples
Keyboard input simulation:
use hidg::{Class, Device, Keyboard, Key, Led, StateChange};
fn main() -> std::io::Result<()> {
let mut device = Device::<Keyboard>::open(0)?;
let mut input = Keyboard.input();
input.press_key(Key::LeftCtrl);
input.press_key(Key::A);
device.input(&input)?;
println!("Keys: {:?}", input.pressed().collect::<Vec<Key>>());
input.release_key(Key::LeftCtrl);
input.release_key(Key::A);
device.input(&input)?;
let mut output = Keyboard.output();
device.output(&mut output)?;
println!("LEDs: {:?}", output.lit().collect::<Vec<Led>>());
Ok(())
}
Mouse input simulation:
use hidg::{Button, Class, Device, Mouse, StateChange, ValueChange};
fn main() -> std::io::Result<()> {
let mut device = Device::<Mouse>::open("hidg0")?;
let mut input = Mouse.input();
input.press_button(Button::Primary);
input.change_pointer((150, 50), false);
device.input(&input)?;
input.change_pointer((70, -30), true);
println!("Buttons: {:?}", input.pressed().collect::<Vec<Button>>());
input.release_button(Button::Primary);
device.input(&input)?;
Ok(())
}