Merge pull request #60 from nicokoch/master

Add `Device::send_event` and `RawDevice::send_event`.
This commit is contained in:
Noa 2021-12-07 12:56:36 -06:00 committed by GitHub
commit ab22becc48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,31 @@
use evdev::{EventType, InputEvent, LedType};
mod _pick_device;
fn main() {
let mut d = _pick_device::pick_device();
println!("{}", d);
println!("Blinking the Keyboard LEDS...");
for i in 0..5 {
let on = i % 2 != 0;
d.send_events(&[
InputEvent::new(
EventType::LED,
LedType::LED_CAPSL.0,
if on { i32::MAX } else { 0 },
),
InputEvent::new(
EventType::LED,
LedType::LED_NUML.0,
if on { i32::MAX } else { 0 },
),
InputEvent::new(
EventType::LED,
LedType::LED_SCROLLL.0,
if on { i32::MAX } else { 0 },
),
])
.unwrap();
std::thread::sleep(std::time::Duration::from_secs(1));
}
}

View file

@ -1,4 +1,5 @@
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::Write;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::io::{AsRawFd, RawFd};
use std::path::Path; use std::path::Path;
@ -610,6 +611,17 @@ impl RawDevice {
} }
Ok(()) Ok(())
} }
/// Send an event to the device.
///
/// Events that are typically sent to devices are
/// [EventType::LED] (turn device LEDs on and off),
/// [EventType::SOUND] (play a sound on the device)
/// and [EventType::FORCEFEEDBACK] (play force feedback effects on the device, i.e. rumble).
pub fn send_events(&mut self, events: &[InputEvent]) -> io::Result<()> {
let bytes = unsafe { crate::cast_to_bytes(events) };
self.file.write_all(bytes)
}
} }
impl AsRawFd for RawDevice { impl AsRawFd for RawDevice {

View file

@ -342,6 +342,16 @@ impl Device {
pub fn ungrab(&mut self) -> io::Result<()> { pub fn ungrab(&mut self) -> io::Result<()> {
self.raw.ungrab() self.raw.ungrab()
} }
/// Send an event to the device.
///
/// Events that are typically sent to devices are
/// [EventType::LED] (turn device LEDs on and off),
/// [EventType::SOUND] (play a sound on the device)
/// and [EventType::FORCEFEEDBACK] (play force feedback effects on the device, i.e. rumble).
pub fn send_events(&mut self, events: &[InputEvent]) -> io::Result<()> {
self.raw.send_events(events)
}
} }
impl AsRawFd for Device { impl AsRawFd for Device {