Skip to content

Commit

Permalink
add monitor detection fallback to skip cap check (#102)
Browse files Browse the repository at this point in the history
l
  • Loading branch information
braye authored Mar 30, 2024
1 parent 78e4718 commit bb6b8d0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/brightness/ddcutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub struct DdcUtil {

impl DdcUtil {
pub fn new(name: &str, min_brightness: u64) -> Result<Self, Box<dyn Error>> {
let mut display = find_display_by_name(name).ok_or("Unable to find display")?;
let mut display = find_display_by_name(name, true)
.or_else(|| find_display_by_name(name, false))
.ok_or("Unable to find display")?;
let max_brightness = get_max_brightness(&mut display)?;

Ok(Self {
Expand Down Expand Up @@ -63,11 +65,16 @@ fn get_max_brightness(display: &mut Display) -> Result<u64, Box<dyn Error>> {
.maximum() as u64)
}

fn find_display_by_name(name: &str) -> Option<Display> {
fn find_display_by_name(name: &str, check_caps: bool) -> Option<Display> {
let displays = ddc_hi::Display::enumerate()
.into_iter()
.filter_map(|mut display| {
display.update_capabilities().ok().map(|_| {
let caps = if check_caps {
display.update_capabilities()
} else {
Ok(())
};
caps.ok().map(|_| {
let empty = "".to_string();
let merged = format!(
"{} {}",
Expand All @@ -80,15 +87,21 @@ fn find_display_by_name(name: &str) -> Option<Display> {
.collect_vec();

log::debug!(
"Discovered displays: {:?}",
"Discovered displays (check_caps={}): {:?}",
check_caps,
displays.iter().map(|(name, _)| name).collect_vec()
);

displays.into_iter().find_map(|(merged, display)| {
merged
.contains(name)
.then(|| {
log::debug!("Using display '{}' for config '{}'", merged, name);
log::debug!(
"Using display '{}' for config '{}' (check_caps={})",
merged,
name,
check_caps
);
})
.map(|_| display)
})
Expand Down

0 comments on commit bb6b8d0

Please sign in to comment.