Skip to content

Commit

Permalink
Merge pull request #91 from adosikas/patch-1
Browse files Browse the repository at this point in the history
Don't switch source when already set
  • Loading branch information
haimgel authored May 29, 2022
2 parents f8773c0 + 7076fcc commit 804a146
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/display_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::configuration::{Configuration, SwitchDirection};
use crate::input_source::InputSource;

use anyhow::{Error, Result};
use ddc_hi::{Ddc, Display};
use ddc_hi::{Ddc, Display, Handle};
use shell_words;
use std::collections::HashSet;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -47,6 +47,29 @@ fn are_display_names_unique(displays: &[Display]) -> bool {
displays.iter().all(|display| hash.insert(display_name(display, None)))
}

fn try_switch_display(handle: &mut Handle, display_name: &str, input: InputSource) {
match handle.get_vcp_feature(INPUT_SELECT) {
Ok(raw_source) => {
if raw_source.value() & 0xff == input.value() {
info!("Display {} is already set to {}", display_name, input);
return;
}
}
Err(err) => {
warn!("Failed to get current input for display {}: {:?}", display_name, err);
}
}
debug!("Setting display {} to {}", display_name, input);
match handle.set_vcp_feature(INPUT_SELECT, input.value()) {
Ok(_) => {
info!("Display {} set to {}", display_name, input);
}
Err(err) => {
error!("Failed to set display {} to {} ({:?})", display_name, input, err);
}
}
}

fn displays() -> Vec<Display> {
let displays = Display::enumerate();
if !displays.is_empty() {
Expand Down Expand Up @@ -98,15 +121,7 @@ pub fn switch(config: &Configuration, switch_direction: SwitchDirection) {
let input_sources = config.configuration_for_monitor(&display_name);
debug!("Input sources found for display {}: {:?}", display_name, input_sources);
if let Some(input) = input_sources.source(switch_direction) {
debug!("Setting display {} to {}", display_name, input);
match display.handle.set_vcp_feature(INPUT_SELECT, input.value()) {
Ok(_) => {
info!("Display {} set to {}", display_name, input);
}
Err(err) => {
error!("Failed to set display {} to {} ({:?})", display_name, input, err);
}
}
try_switch_display(&mut display.handle, &display_name, input);
} else {
info!(
"Display {} is not configured to switch on USB {}",
Expand Down

0 comments on commit 804a146

Please sign in to comment.