From 3a425349ec443fcbf144c8f4b0a4e7af6a47547f Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Sat, 15 Jan 2022 17:58:52 -0700 Subject: [PATCH 1/6] RawDevice: introduce system_path() method --- src/raw_stream.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/raw_stream.rs b/src/raw_stream.rs index d6c5a71..3d8f1b0 100644 --- a/src/raw_stream.rs +++ b/src/raw_stream.rs @@ -2,7 +2,7 @@ use std::fs::{File, OpenOptions}; use std::io::Write; use std::mem::MaybeUninit; use std::os::unix::io::{AsRawFd, RawFd}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::{io, mem}; use crate::constants::*; @@ -68,6 +68,7 @@ pub struct RawDevice { supported_snd: Option>, pub(crate) event_buf: Vec, grabbed: bool, + system_path: PathBuf, } #[derive(Debug, Clone)] @@ -225,6 +226,7 @@ impl RawDevice { auto_repeat, event_buf: Vec::new(), grabbed: false, + system_path: path.to_path_buf(), }) } @@ -383,6 +385,11 @@ impl RawDevice { self.supported_snd.as_deref() } + /// Returns the system path used to open the device. + pub fn system_path(&self) -> &Path { + self.system_path.as_ref() + } + /// Read a maximum of `num` events into the internal buffer. If the underlying fd is not /// O_NONBLOCK, this will block. /// From e3e7d339bd4e490d459178d7ef237c6c0246505c Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Sat, 15 Jan 2022 19:46:27 -0700 Subject: [PATCH 2/6] Device: introduce system_path() method --- src/sync_stream.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sync_stream.rs b/src/sync_stream.rs index 7ee5455..f3ce73f 100644 --- a/src/sync_stream.rs +++ b/src/sync_stream.rs @@ -253,6 +253,11 @@ impl Device { self.raw.supported_sounds() } + /// Returns the system path used to open the device. + pub fn system_path(&self) -> &Path { + self.raw.system_path() + } + /// Retrieve the current keypress state directly via kernel syscall. pub fn get_key_state(&self) -> io::Result> { self.raw.get_key_state() From 824302d0f65d4298ae3461d081238e96af4ed995 Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Sun, 16 Jan 2022 18:34:09 -0700 Subject: [PATCH 3/6] Revert "Device: introduce system_path() method" This reverts commit e3e7d339bd4e490d459178d7ef237c6c0246505c. --- src/sync_stream.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/sync_stream.rs b/src/sync_stream.rs index f3ce73f..7ee5455 100644 --- a/src/sync_stream.rs +++ b/src/sync_stream.rs @@ -253,11 +253,6 @@ impl Device { self.raw.supported_sounds() } - /// Returns the system path used to open the device. - pub fn system_path(&self) -> &Path { - self.raw.system_path() - } - /// Retrieve the current keypress state directly via kernel syscall. pub fn get_key_state(&self) -> io::Result> { self.raw.get_key_state() From 4d957a4dacabf78c3125f26b7de2413ff736b6e4 Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Sun, 16 Jan 2022 18:34:10 -0700 Subject: [PATCH 4/6] Revert "RawDevice: introduce system_path() method" This reverts commit 3a425349ec443fcbf144c8f4b0a4e7af6a47547f. --- src/raw_stream.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/raw_stream.rs b/src/raw_stream.rs index 3d8f1b0..d6c5a71 100644 --- a/src/raw_stream.rs +++ b/src/raw_stream.rs @@ -2,7 +2,7 @@ use std::fs::{File, OpenOptions}; use std::io::Write; use std::mem::MaybeUninit; use std::os::unix::io::{AsRawFd, RawFd}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::{io, mem}; use crate::constants::*; @@ -68,7 +68,6 @@ pub struct RawDevice { supported_snd: Option>, pub(crate) event_buf: Vec, grabbed: bool, - system_path: PathBuf, } #[derive(Debug, Clone)] @@ -226,7 +225,6 @@ impl RawDevice { auto_repeat, event_buf: Vec::new(), grabbed: false, - system_path: path.to_path_buf(), }) } @@ -385,11 +383,6 @@ impl RawDevice { self.supported_snd.as_deref() } - /// Returns the system path used to open the device. - pub fn system_path(&self) -> &Path { - self.system_path.as_ref() - } - /// Read a maximum of `num` events into the internal buffer. If the underlying fd is not /// O_NONBLOCK, this will block. /// From 07095dbbbc32d9075506fb6fbe81e06217b0c069 Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Sun, 16 Jan 2022 18:33:52 -0700 Subject: [PATCH 5/6] enumerate should return a tuple, (PathBuf, Device) --- src/lib.rs | 7 ++++--- src/raw_stream.rs | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 88372a4..29c3be0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,6 +87,7 @@ pub mod uinput; mod tokio_stream; use std::fmt; +use std::path::PathBuf; use std::time::{Duration, SystemTime}; // pub use crate::constants::FFEffect::*; @@ -249,9 +250,9 @@ pub struct EnumerateDevices { inner: raw_stream::EnumerateDevices, } impl Iterator for EnumerateDevices { - type Item = Device; - fn next(&mut self) -> Option { - self.inner.next().map(Device::from_raw_device) + type Item = (PathBuf, Device); + fn next(&mut self) -> Option<(PathBuf, Device)> { + self.inner.next().map(|(pb, dev)| (pb, Device::from_raw_device(dev))) } } diff --git a/src/raw_stream.rs b/src/raw_stream.rs index d6c5a71..e34fc51 100644 --- a/src/raw_stream.rs +++ b/src/raw_stream.rs @@ -2,7 +2,7 @@ use std::fs::{File, OpenOptions}; use std::io::Write; use std::mem::MaybeUninit; use std::os::unix::io::{AsRawFd, RawFd}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::{io, mem}; use crate::constants::*; @@ -656,8 +656,8 @@ pub struct EnumerateDevices { readdir: Option, } impl Iterator for EnumerateDevices { - type Item = RawDevice; - fn next(&mut self) -> Option { + type Item = (PathBuf, RawDevice); + fn next(&mut self) -> Option<(PathBuf, RawDevice)> { use std::os::unix::ffi::OsStrExt; let readdir = self.readdir.as_mut()?; loop { @@ -666,7 +666,7 @@ impl Iterator for EnumerateDevices { let fname = path.file_name().unwrap(); if fname.as_bytes().starts_with(b"event") { if let Ok(dev) = RawDevice::open(&path) { - return Some(dev); + return Some((path, dev)); } } } From 773e8066c5ce1e7899e86da64e6dcdc81884fec2 Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Mon, 17 Jan 2022 20:03:59 -0700 Subject: [PATCH 6/6] examples: update _pick_device.rs example --- examples/_pick_device.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/_pick_device.rs b/examples/_pick_device.rs index 8dac874..9dd62d0 100644 --- a/examples/_pick_device.rs +++ b/examples/_pick_device.rs @@ -9,7 +9,9 @@ pub fn pick_device() -> evdev::Device { if let Some(dev_file) = args.next() { evdev::Device::open(dev_file).unwrap() } else { - let mut devices = evdev::enumerate().collect::>(); + let mut devices = evdev::enumerate() + .map(|t| t.1) + .collect::>(); // readdir returns them in reverse order from their eventN names for some reason devices.reverse(); for (i, d) in devices.iter().enumerate() {