https://github.com/katyo/hidg-rs
Linux HID Gadget Emulation in Rust
https://github.com/katyo/hidg-rs
Last synced: about 1 year ago
JSON representation
Linux HID Gadget Emulation in Rust
- Host: GitHub
- URL: https://github.com/katyo/hidg-rs
- Owner: katyo
- License: mit
- Created: 2022-09-21T18:00:33.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-11T04:58:55.000Z (about 2 years ago)
- Last Synced: 2025-04-02T04:45:24.224Z (about 1 year ago)
- Language: Rust
- Size: 71.3 KB
- Stars: 10
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HID Gadget Emulation in Rust
[](https://github.com/katyo/hidg-rs)
[](https://crates.io/crates/hidg)
[](https://docs.rs/hidg)
[](https://opensource.org/licenses/MIT)
[](https://github.com/katyo/hidg-rs/actions?query=workflow%3ARust)
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](https://crates.io/crates/hidg-core) - core abstractions and low level interface (not for end users)
- **[hidg](https://crates.io/crates/hidg)** - std interface which supports synchronous operation only
- [tokio-hidg](https://crates.io/crates/tokio-hidg) - async interface for [tokio](https://tokio.rs/) runtime
- [async-hidg](https://crates.io/crates/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](https://crates.io/crates/phf) in [core::str::FromStr] trait implementations
- *serde* - enables [serde](https://crates.io/crates/serde) support for some types
- *keyboard* - enables keyboard class support
- *mouse* - enables mouse class support
## Usage examples
Keyboard input simulation:
```rust,no_run
use hidg::{Class, Device, Keyboard, Key, Led, StateChange};
fn main() -> std::io::Result<()> {
let mut device = Device::::open(0)?; // open device
// Create input report
let mut input = Keyboard.input();
// Press left ctrl modifier
input.press_key(Key::LeftCtrl);
// Press key 'A'
input.press_key(Key::A);
// Send input report
device.input(&input)?;
// Get pressed keys
println!("Keys: {:?}", input.pressed().collect::>());
// Release left ctrl modifier
input.release_key(Key::LeftCtrl);
// Release key 'A'
input.release_key(Key::A);
// Send input report
device.input(&input)?;
// Create output report
let mut output = Keyboard.output();
// Receive output report
device.output(&mut output)?;
// Print lit LEDs
println!("LEDs: {:?}", output.lit().collect::>());
Ok(())
}
```
Mouse input simulation:
```rust,no_run
use hidg::{Button, Class, Device, Mouse, StateChange, ValueChange};
fn main() -> std::io::Result<()> {
let mut device = Device::::open("hidg0")?; // open device
// Create input report
let mut input = Mouse.input();
// Press primary button
input.press_button(Button::Primary);
// Update pointer coordinates
input.change_pointer((150, 50), false);
// Send input report
device.input(&input)?;
// Move pointer relatively
input.change_pointer((70, -30), true);
// Get pressed buttons
println!("Buttons: {:?}", input.pressed().collect::>());
// Release primary button
input.release_button(Button::Primary);
// Send input report
device.input(&input)?;
Ok(())
}
```