Little refactor

This commit is contained in:
Florian RICHER 2024-10-26 17:42:52 +02:00
parent 0d99991827
commit b5b7ca104b
5 changed files with 16 additions and 11 deletions

7
Cargo.lock generated
View file

@ -80,6 +80,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"drm", "drm",
"glob",
] ]
[[package]] [[package]]
@ -92,6 +93,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.159" version = "0.2.159"

View file

@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
glob = "0.3.1"
anyhow = "1.0.91" anyhow = "1.0.91"
drm = "0.14.1" drm = "0.14.1"

View file

@ -3,19 +3,14 @@ use drm::control::Device as ControlDevice;
use std::os::fd::{AsFd, BorrowedFd}; use std::os::fd::{AsFd, BorrowedFd};
use std::fmt::Debug; use std::fmt::Debug;
// A simple wrapper for a device node.
pub struct Card(std::fs::File); 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 { impl AsFd for Card {
fn as_fd(&self) -> BorrowedFd<'_> { fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd() self.0.as_fd()
} }
} }
/// Simple helper methods for opening a `Card`.
impl Card { impl Card {
pub fn open(path: &str) -> anyhow::Result<Self> { pub fn open(path: &str) -> anyhow::Result<Self> {
let mut options = std::fs::OpenOptions::new(); let mut options = std::fs::OpenOptions::new();

2
src/drm_impl/mod.rs Normal file
View file

@ -0,0 +1,2 @@
mod card;
pub use card::Card;

View file

@ -1,9 +1,9 @@
use drm::control::Device; use drm::control::Device;
mod card; mod drm_impl;
fn main() { fn main() {
let gpu = match card::Card::open("/dev/dri/card1") { let gpu = match drm_impl::Card::open("/dev/dri/card1") {
Ok(gpu) => gpu, Ok(gpu) => gpu,
Err(err) => { Err(err) => {
panic!("Impossible to open DRM file : {:?}", err); panic!("Impossible to open DRM file : {:?}", err);
@ -14,7 +14,7 @@ fn main() {
print_connectors(&gpu); print_connectors(&gpu);
} }
fn print_connectors(gpu: &card::Card) { fn print_connectors(gpu: &drm_impl::Card) {
let resource_handles = gpu.resource_handles().unwrap(); let resource_handles = gpu.resource_handles().unwrap();
println!("Connectors:"); 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) { match gpu.get_connector(*connector_handle, false) {
Ok(connector) => { Ok(connector) => {
println!("\t{} ({:?})", connector.interface().as_str(), connector.state()); 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<drm::control::encoder::Handle>) -> String { fn format_encoder(gpu: &drm_impl::Card, encoder_handle: Option<drm::control::encoder::Handle>) -> String {
match encoder_handle.and_then(|handle| gpu.get_encoder(handle).ok()) { match encoder_handle.and_then(|handle| gpu.get_encoder(handle).ok()) {
Some(encoder) => { Some(encoder) => {
let crtc_info = format_crtc(&gpu, encoder.crtc()); let crtc_info = format_crtc(&gpu, encoder.crtc());
@ -48,7 +48,7 @@ fn format_encoder(gpu: &card::Card, encoder_handle: Option<drm::control::encoder
} }
} }
fn format_crtc(gpu: &card::Card, crtc_handle: Option<drm::control::crtc::Handle>) -> String { fn format_crtc(gpu: &drm_impl::Card, crtc_handle: Option<drm::control::crtc::Handle>) -> String {
match crtc_handle.and_then(|handle| Some(gpu.get_crtc(handle))) { match crtc_handle.and_then(|handle| Some(gpu.get_crtc(handle))) {
Some(crtc) => format!("{:?}", crtc), Some(crtc) => format!("{:?}", crtc),
None => format!("No CRTC data") None => format!("No CRTC data")