From b5b7ca104b3b3804a5519f6f44f5218133819d97 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sat, 26 Oct 2024 17:42:52 +0200 Subject: [PATCH] Little refactor --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/{ => drm_impl}/card.rs | 5 ----- src/drm_impl/mod.rs | 2 ++ src/main.rs | 12 ++++++------ 5 files changed, 16 insertions(+), 11 deletions(-) rename src/{ => drm_impl}/card.rs (90%) create mode 100644 src/drm_impl/mod.rs diff --git a/Cargo.lock b/Cargo.lock index de04ec7..33de0c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,7 @@ version = "0.1.0" dependencies = [ "anyhow", "drm", + "glob", ] [[package]] @@ -92,6 +93,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "libc" version = "0.2.159" diff --git a/Cargo.toml b/Cargo.toml index 8bc7a2a..54f164e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +glob = "0.3.1" anyhow = "1.0.91" drm = "0.14.1" \ No newline at end of file diff --git a/src/card.rs b/src/drm_impl/card.rs similarity index 90% rename from src/card.rs rename to src/drm_impl/card.rs index 8fdb31f..79c5e9b 100644 --- a/src/card.rs +++ b/src/drm_impl/card.rs @@ -3,19 +3,14 @@ use drm::control::Device as ControlDevice; use std::os::fd::{AsFd, BorrowedFd}; use std::fmt::Debug; -// A simple wrapper for a device node. pub struct Card(std::fs::File); -/// Implementing [`AsFd`] is a prerequisite to implementing the traits found -/// in this crate. Here, we are just calling [`File::as_fd()`] on the inner -/// [`File`]. impl AsFd for Card { fn as_fd(&self) -> BorrowedFd<'_> { self.0.as_fd() } } -/// Simple helper methods for opening a `Card`. impl Card { pub fn open(path: &str) -> anyhow::Result { let mut options = std::fs::OpenOptions::new(); diff --git a/src/drm_impl/mod.rs b/src/drm_impl/mod.rs new file mode 100644 index 0000000..1c1abf0 --- /dev/null +++ b/src/drm_impl/mod.rs @@ -0,0 +1,2 @@ +mod card; +pub use card::Card; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7b555d..a57693d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ use drm::control::Device; -mod card; +mod drm_impl; fn main() { - let gpu = match card::Card::open("/dev/dri/card1") { + let gpu = match drm_impl::Card::open("/dev/dri/card1") { Ok(gpu) => gpu, Err(err) => { panic!("Impossible to open DRM file : {:?}", err); @@ -14,7 +14,7 @@ fn main() { print_connectors(&gpu); } -fn print_connectors(gpu: &card::Card) { +fn print_connectors(gpu: &drm_impl::Card) { let resource_handles = gpu.resource_handles().unwrap(); println!("Connectors:"); @@ -23,7 +23,7 @@ fn print_connectors(gpu: &card::Card) { } } -fn print_connector(gpu: &card::Card, connector_handle: &drm::control::connector::Handle) { +fn print_connector(gpu: &drm_impl::Card, connector_handle: &drm::control::connector::Handle) { match gpu.get_connector(*connector_handle, false) { Ok(connector) => { println!("\t{} ({:?})", connector.interface().as_str(), connector.state()); @@ -37,7 +37,7 @@ fn print_connector(gpu: &card::Card, connector_handle: &drm::control::connector: }; } -fn format_encoder(gpu: &card::Card, encoder_handle: Option) -> String { +fn format_encoder(gpu: &drm_impl::Card, encoder_handle: Option) -> String { match encoder_handle.and_then(|handle| gpu.get_encoder(handle).ok()) { Some(encoder) => { let crtc_info = format_crtc(&gpu, encoder.crtc()); @@ -48,7 +48,7 @@ fn format_encoder(gpu: &card::Card, encoder_handle: Option) -> String { +fn format_crtc(gpu: &drm_impl::Card, crtc_handle: Option) -> String { match crtc_handle.and_then(|handle| Some(gpu.get_crtc(handle))) { Some(crtc) => format!("{:?}", crtc), None => format!("No CRTC data")