Skip to content

Commit

Permalink
Merge pull request #112 from haimgel/feature/dependencies-upgrade
Browse files Browse the repository at this point in the history
Dependencies upgrade
  • Loading branch information
haimgel committed Aug 21, 2022
2 parents 26f5678 + 6bb341a commit 4b1cbf0
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 227 deletions.
278 changes: 121 additions & 157 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
@@ -1,8 +1,8 @@
[package]
name = "display_switch"
version = "1.1.0"
version = "1.2.0"
authors = ["Haim Gelfenbeyn <haim@g8n.me>"]
edition = "2018"
edition = "2021"
description = "An utility to watch for USB device connect/disconnect events and switch display inputs via DDC/CI"
readme = "README.md"
repository = "https://github.com/haimgel/display-switch/"
Expand All @@ -13,9 +13,9 @@ serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"

[dependencies]
config = "^0.11"
config = { version = "^0.13", features = ["ini"], default-features = false }
paste = "^1"
dirs = "^3.0"
dirs = "^4.0"
serde = { version = "^1.0", features = ["derive"] }
anyhow = "^1.0"
log = "^0.4"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
@@ -1 +1 @@
1.55.0
1.63.0
17 changes: 9 additions & 8 deletions src/configuration.rs
Expand Up @@ -91,11 +91,11 @@ impl InputSources {
impl Configuration {
pub fn load() -> Result<Self> {
let config_file_name = Self::config_file_name()?;
let mut settings = config::Config::default();
settings
.merge(config::File::from(config_file_name.clone()))?
.merge(config::Environment::with_prefix("DISPLAY_SWITCH"))?;
let config = settings.try_into::<Self>()?;
let builder = config::Config::builder()
.add_source(config::File::from(config_file_name.clone()))
.add_source(config::Environment::with_prefix("DISPLAY_SWITCH"));

let config = builder.build()?.try_deserialize()?;
info!("Configuration loaded ({:?}): {:?}", config_file_name, config);
Ok(config)
}
Expand Down Expand Up @@ -180,9 +180,10 @@ mod tests {
}

fn load_test_config(config_str: &str) -> Result<Configuration, ConfigError> {
let mut settings = config::Config::default();
settings.merge(config::File::from_str(config_str, Ini)).unwrap();
settings.try_into::<Configuration>()
config::Config::builder()
.add_source(config::File::from_str(config_str, Ini))
.build()?
.try_deserialize()
}

#[test]
Expand Down
42 changes: 21 additions & 21 deletions src/display_control.rs
Expand Up @@ -48,26 +48,26 @@ fn are_display_names_unique(displays: &[Display]) -> bool {
}

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);
}
}
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> {
Expand Down Expand Up @@ -121,7 +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) {
try_switch_display(&mut display.handle, &display_name, input);
try_switch_display(&mut display.handle, &display_name, input);
} else {
info!(
"Display {} is not configured to switch on USB {}",
Expand Down
7 changes: 6 additions & 1 deletion src/logging.rs
Expand Up @@ -12,7 +12,12 @@ use crate::configuration::Configuration;

pub fn init_logging() -> Result<()> {
Ok(CombinedLogger::init(vec![
TermLogger::new(LevelFilter::Debug, Config::default(), TerminalMode::Mixed, ColorChoice::Auto),
TermLogger::new(
LevelFilter::Debug,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
),
WriteLogger::new(
LevelFilter::Debug,
Config::default(),
Expand Down
10 changes: 5 additions & 5 deletions src/platform/pnp_detect_libusb.rs
Expand Up @@ -5,7 +5,7 @@

use crate::usb::{device2str, UsbCallback};
use anyhow::{anyhow, Result};
use rusb::{Context, Device, HotplugBuilder, UsbContext, Registration};
use rusb::{Context, Device, HotplugBuilder, Registration, UsbContext};

/// Detection of plugged in / removed USB devices: uses "libusb" and should work on Linux
/// and MacOS, but not on Windows: libusb does not support hotplug on Windows.
Expand Down Expand Up @@ -36,10 +36,10 @@ impl PnPDetectLibusb {
if rusb::has_hotplug() {
let context = Context::new()?;

let mut _reg: std::option::Option<Registration<rusb::Context>> = Some(
HotplugBuilder::new()
.enumerate(true)
.register(&context, Box::new(self))?,
let _reg: std::option::Option<Registration<rusb::Context>> = Some(
HotplugBuilder::new()
.enumerate(true)
.register(&context, Box::new(self))?,
);

loop {
Expand Down
61 changes: 31 additions & 30 deletions src/platform/pnp_detect_windows.rs
Expand Up @@ -106,34 +106,35 @@ impl PnPDetectWindows {

/// Create an invisible window to handle WM_DEVICECHANGE message
fn create_window(&mut self) {
let winapi_class_name: Vec<u16> = OsStr::new("DisplaySwitchPnPDetectWindowClass")
.encode_wide()
.chain(once(0))
.collect();
let hinstance = unsafe { GetModuleHandleW(std::ptr::null()) };
let winapi_class_name: Vec<u16> = OsStr::new("DisplaySwitchPnPDetectWindowClass")
.encode_wide()
.chain(once(0))
.collect();
let hinstance = unsafe { GetModuleHandleW(std::ptr::null()) };

let wc = WNDCLASSW {
style: 0,
lpfnWndProc: Some(Self::window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: hinstance,
hIcon: 0 as HICON,
hCursor: 0 as HCURSOR,
hbrBackground: 0 as HBRUSH,
lpszMenuName: 0 as LPCWSTR,
lpszClassName: winapi_class_name.as_ptr(),
};
let wc = WNDCLASSW {
style: 0,
lpfnWndProc: Some(Self::window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: hinstance,
hIcon: 0 as HICON,
hCursor: 0 as HCURSOR,
hbrBackground: 0 as HBRUSH,
lpszMenuName: 0 as LPCWSTR,
lpszClassName: winapi_class_name.as_ptr(),
};

let error_code = unsafe { RegisterClassW(&wc) };
assert_ne!(error_code, 0, "failed to register the window class");
let error_code = unsafe { RegisterClassW(&wc) };
assert_ne!(error_code, 0, "failed to register the window class");

let window_name: Vec<u16> = OsStr::new("DisplaySwitchPnPDetectWindow")
.encode_wide()
.chain(once(0))
.collect();
let window_name: Vec<u16> = OsStr::new("DisplaySwitchPnPDetectWindow")
.encode_wide()
.chain(once(0))
.collect();

let hwnd = unsafe { CreateWindowExW(
let hwnd = unsafe {
CreateWindowExW(
0,
winapi_class_name.as_ptr(),
window_name.as_ptr(),
Expand All @@ -147,12 +148,12 @@ impl PnPDetectWindows {
hinstance,
self as *mut Self as *mut winapi::ctypes::c_void,
//std::ptr::null_mut(),
) };
)
};

if hwnd.is_null() {
panic!("Something went wrong while creating a window");
}
self.hwnd = hwnd;

if hwnd.is_null() {
panic!("Something went wrong while creating a window");
}
self.hwnd = hwnd;
}
}

0 comments on commit 4b1cbf0

Please sign in to comment.