From 39500f1d052a92077d95e648eda70aff66c31ace Mon Sep 17 00:00:00 2001 From: Victor Brekenfeld Date: Wed, 15 Apr 2020 21:42:33 +0200 Subject: [PATCH] Fix primary_gpu --- src/backend/udev.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/backend/udev.rs b/src/backend/udev.rs index a22b29d..f88972f 100644 --- a/src/backend/udev.rs +++ b/src/backend/udev.rs @@ -174,26 +174,32 @@ pub fn primary_gpu>(seat: S) -> IoResult> { enumerator.match_subsystem("drm")?; enumerator.match_sysname("card[0-9]*")?; - let mut result = None; - for device in enumerator.scan_devices()? { - if device - .property_value("ID_SEAT") - .map(|x| x.to_os_string()) - .unwrap_or_else(|| OsString::from("seat0")) - == *seat.as_ref() - { - if let Some(pci) = device.parent_with_subsystem(Path::new("pci"))? { - if let Some(id) = pci.attribute_value("boot_vga") { - if id == "1" { - result = Some(device); + if let Some(path) = enumerator + .scan_devices()? + .filter_map(|device| { + if device + .property_value("ID_SEAT") + .map(|x| x.to_os_string()) + .unwrap_or_else(|| OsString::from("seat0")) + == *seat.as_ref() + { + if let Ok(Some(pci)) = device.parent_with_subsystem(Path::new("pci")) { + if let Some(id) = pci.attribute_value("boot_vga") { + if id == "1" { + return Some(device); + } } } - } else if result.is_none() { - result = Some(device); - } - } + }; + None + }) + .flat_map(|device| device.devnode().map(PathBuf::from)) + .next() + { + Ok(Some(path)) + } else { + all_gpus(seat).map(|all| all.into_iter().next()) } - Ok(result.and_then(|device| device.devnode().map(PathBuf::from))) } /// Returns the paths of all available GPU devices