From 6a740e3651623f2335e1538748df6871ad56b457 Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Sun, 27 Oct 2024 11:41:35 +0100 Subject: [PATCH 1/8] Add function to enumerate devices --- drm-ffi/drm-sys/src/bindings.rs | 2 +- examples/enumerate.rs | 7 +++++++ src/node/mod.rs | 20 +++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 examples/enumerate.rs diff --git a/drm-ffi/drm-sys/src/bindings.rs b/drm-ffi/drm-sys/src/bindings.rs index 9143a0e..fb673aa 100644 --- a/drm-ffi/drm-sys/src/bindings.rs +++ b/drm-ffi/drm-sys/src/bindings.rs @@ -1,4 +1,4 @@ -/* automatically generated by rust-bindgen 0.69.4 */ +/* automatically generated by rust-bindgen 0.69.5 */ pub const DRM_NAME: &[u8; 4] = b"drm\0"; pub const DRM_MIN_ORDER: u32 = 5; diff --git a/examples/enumerate.rs b/examples/enumerate.rs new file mode 100644 index 0000000..bab8c72 --- /dev/null +++ b/examples/enumerate.rs @@ -0,0 +1,7 @@ +use drm::node; + +pub fn main() { + let devices = node::drm_get_devices(); + + println!("{:?}", devices.unwrap()); +} \ No newline at end of file diff --git a/src/node/mod.rs b/src/node/mod.rs index 5fcecb3..439f4ef 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -4,7 +4,7 @@ pub mod constants; use std::error::Error; use std::fmt::{self, Debug, Display, Formatter}; -use std::io; +use std::{fs, io}; use std::os::unix::io::AsFd; use std::path::{Path, PathBuf}; @@ -380,3 +380,21 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result { ), )) } + +/// Returns a Vector with all DRM Nodes we managed to find. There might be duplicates. +pub fn drm_get_devices() -> Result, io::Error> { + let mut devices: Vec = vec![]; + + fs::read_dir("/dev")? + .for_each(|entry| { + if entry.is_ok() { + let device_path = entry.unwrap().path(); + let node = DrmNode::from_path(device_path); + if node.is_ok() { + devices.push(node.unwrap()) + } + } + }); + + Ok(devices) +} From 9bc03d263b4cccca9fd41b5664cbb45ce4e342a1 Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 08:15:55 +0100 Subject: [PATCH 2/8] Update src/node/mod.rs Co-authored-by: John Nunley --- src/node/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index 439f4ef..3e1dd1b 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -390,8 +390,8 @@ pub fn drm_get_devices() -> Result, io::Error> { if entry.is_ok() { let device_path = entry.unwrap().path(); let node = DrmNode::from_path(device_path); - if node.is_ok() { - devices.push(node.unwrap()) + if let Ok(node) = node { + devices.push(node) } } }); From 629028ce611a1860baa75093b1a2a629f1976f9e Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 08:16:10 +0100 Subject: [PATCH 3/8] Update src/node/mod.rs Co-authored-by: Marijn Suijten --- src/node/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index 3e1dd1b..b4eda14 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -381,7 +381,7 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result { )) } -/// Returns a Vector with all DRM Nodes we managed to find. There might be duplicates. +/// Returns a [`Vec`] with all DRM Nodes we managed to find. There might be duplicates. pub fn drm_get_devices() -> Result, io::Error> { let mut devices: Vec = vec![]; From 7d9cf3d0344bce812a960022f47ad9202cde7a5b Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 08:28:34 +0100 Subject: [PATCH 4/8] Remove unneeded type hint --- src/node/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index b4eda14..61c9400 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -383,7 +383,7 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result { /// Returns a [`Vec`] with all DRM Nodes we managed to find. There might be duplicates. pub fn drm_get_devices() -> Result, io::Error> { - let mut devices: Vec = vec![]; + let mut devices = vec![]; fs::read_dir("/dev")? .for_each(|entry| { From 6c18b864da66ffe03abd671cfc09ecdbaaa26f1a Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 08:55:48 +0100 Subject: [PATCH 5/8] Simplify entry check --- src/node/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index 61c9400..dfab6dc 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -387,9 +387,8 @@ pub fn drm_get_devices() -> Result, io::Error> { fs::read_dir("/dev")? .for_each(|entry| { - if entry.is_ok() { - let device_path = entry.unwrap().path(); - let node = DrmNode::from_path(device_path); + if let Ok(entry) = entry { + let node = DrmNode::from_path(entry.path()); if let Ok(node) = node { devices.push(node) } From 737b01ddeb8bdecd718a58ab89fd5dcf3452b89b Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 10:09:50 +0100 Subject: [PATCH 6/8] Update code to address review comments --- examples/enumerate.rs | 9 +++++---- src/node/constants.rs | 8 ++++++++ src/node/mod.rs | 23 ++++++++--------------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/enumerate.rs b/examples/enumerate.rs index bab8c72..e585983 100644 --- a/examples/enumerate.rs +++ b/examples/enumerate.rs @@ -1,7 +1,8 @@ -use drm::node; - pub fn main() { - let devices = node::drm_get_devices(); + let devices = drm::node::devices().unwrap(); + + for dev in devices { + println!("{:?}", dev); + } - println!("{:?}", devices.unwrap()); } \ No newline at end of file diff --git a/src/node/constants.rs b/src/node/constants.rs index 4828842..c8d55a6 100644 --- a/src/node/constants.rs +++ b/src/node/constants.rs @@ -43,3 +43,11 @@ pub const RENDER_NAME: &str = "renderD"; /// Render DRM node prefix. #[cfg(target_os = "openbsd")] pub const RENDER_NAME: &str = "drmR"; + +/// Directory to search DRM devices +#[cfg(target_os = "openbsd")] +pub const DRM_DIR_NAME: &str = "/dev"; + +/// Directory to search DRM devices +#[cfg(not(target_os = "openbsd"))] +pub const DRM_DIR_NAME: &str = "/dev/dri"; \ No newline at end of file diff --git a/src/node/mod.rs b/src/node/mod.rs index dfab6dc..9921032 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -381,19 +381,12 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result { )) } -/// Returns a [`Vec`] with all DRM Nodes we managed to find. There might be duplicates. -pub fn drm_get_devices() -> Result, io::Error> { - let mut devices = vec![]; - - fs::read_dir("/dev")? - .for_each(|entry| { - if let Ok(entry) = entry { - let node = DrmNode::from_path(entry.path()); - if let Ok(node) = node { - devices.push(node) - } - } - }); - - Ok(devices) +/// Returns an iterator with all DRM Nodes we managed to find. There might be duplicates. +pub fn devices() -> io::Result> { + let result = fs::read_dir(DRM_DIR_NAME)? + .into_iter() + .filter_map(|entry| entry.ok()) + .filter_map(|entry| DrmNode::from_path(entry.path()).ok()); + + Ok(result) } From 73e2c2ab6969b8df2adc95b8040a0319c235e57c Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 10:17:52 +0100 Subject: [PATCH 7/8] Remove unnecessary into_iter call --- src/node/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index 9921032..ee80dc9 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -384,7 +384,6 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result { /// Returns an iterator with all DRM Nodes we managed to find. There might be duplicates. pub fn devices() -> io::Result> { let result = fs::read_dir(DRM_DIR_NAME)? - .into_iter() .filter_map(|entry| entry.ok()) .filter_map(|entry| DrmNode::from_path(entry.path()).ok()); From d7684bea4116efcf4b030bad932ecf0a55e74a56 Mon Sep 17 00:00:00 2001 From: Jean Hertel Date: Mon, 28 Oct 2024 16:19:32 +0100 Subject: [PATCH 8/8] Format code --- examples/enumerate.rs | 3 +-- src/node/constants.rs | 2 +- src/node/mod.rs | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/enumerate.rs b/examples/enumerate.rs index e585983..4bc29b2 100644 --- a/examples/enumerate.rs +++ b/examples/enumerate.rs @@ -4,5 +4,4 @@ pub fn main() { for dev in devices { println!("{:?}", dev); } - -} \ No newline at end of file +} diff --git a/src/node/constants.rs b/src/node/constants.rs index c8d55a6..a0f9eeb 100644 --- a/src/node/constants.rs +++ b/src/node/constants.rs @@ -50,4 +50,4 @@ pub const DRM_DIR_NAME: &str = "/dev"; /// Directory to search DRM devices #[cfg(not(target_os = "openbsd"))] -pub const DRM_DIR_NAME: &str = "/dev/dri"; \ No newline at end of file +pub const DRM_DIR_NAME: &str = "/dev/dri"; diff --git a/src/node/mod.rs b/src/node/mod.rs index ee80dc9..0a955aa 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -4,9 +4,9 @@ pub mod constants; use std::error::Error; use std::fmt::{self, Debug, Display, Formatter}; -use std::{fs, io}; use std::os::unix::io::AsFd; use std::path::{Path, PathBuf}; +use std::{fs, io}; use rustix::fs::{fstat, major, minor, stat, Dev as dev_t, Stat};