Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't switch source when already set #91

Merged
merged 3 commits into from
May 29, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() == input.value() {
haimgel marked this conversation as resolved.
Show resolved Hide resolved
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