diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 945712703..00e09967d 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -830,7 +830,7 @@ pub fn current_resolution(name: &str) -> ResultType { bail!("Failed to find current resolution for {}", name); } -pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<()> { +pub fn change_resolution_directly(name: &str, width: usize, height: usize) -> ResultType<()> { Command::new("xrandr") .args(vec![ "--output", diff --git a/src/platform/macos.rs b/src/platform/macos.rs index d0d1cc89c..506390506 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -658,7 +658,7 @@ pub fn current_resolution(name: &str) -> ResultType { } } -pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<()> { +pub fn change_resolution_directly(name: &str, width: usize, height: usize) -> ResultType<()> { let display = name.parse::().map_err(|e| anyhow!(e))?; unsafe { if NO == MacSetMode(display, width as _, height as _) { diff --git a/src/platform/mod.rs b/src/platform/mod.rs index e382b0b13..1aead8543 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -44,6 +44,24 @@ pub fn breakdown_callback() { crate::input_service::release_device_modifiers(); } +#[cfg(not(any(target_os = "android", target_os = "ios")))] +pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<()> { + hbb_common::log::warn!("Change resolution of '{}' to ({},{})", name, width, height); + + let cur_resolution = current_resolution(name)?; + // For MacOS + // to-do: Make sure the following comparison works. + // For Linux + // Just run "xrandr", dpi may not be taken into consideration. + // For Windows + // dmPelsWidth and dmPelsHeight is the same to width and height + // Because this process is running in dpi awareness mode. + if cur_resolution.width as usize == width && cur_resolution.height as usize == height { + return Ok(()); + } + change_resolution_directly(name, width, height) +} + // Android #[cfg(target_os = "android")] pub fn get_active_username() -> String { diff --git a/src/platform/windows.rs b/src/platform/windows.rs index e2bb2ca83..b009dfb20 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -1867,10 +1867,10 @@ pub fn resolutions(name: &str) -> Vec { } pub fn current_resolution(name: &str) -> ResultType { + let device_name = str_to_device_name(name); unsafe { let mut dm: DEVMODEW = std::mem::zeroed(); dm.dmSize = std::mem::size_of::() as _; - let device_name = str_to_device_name(name); if EnumDisplaySettingsW(device_name.as_ptr(), ENUM_CURRENT_SETTINGS, &mut dm) == 0 { bail!( "failed to get currrent resolution, errno={}", @@ -1886,21 +1886,10 @@ pub fn current_resolution(name: &str) -> ResultType { } } - - -pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<()> { +pub(super) fn change_resolution_directly(name: &str, width: usize, height: usize) -> ResultType<()> { let device_name = str_to_device_name(name); unsafe { let mut dm: DEVMODEW = std::mem::zeroed(); - if FALSE == EnumDisplaySettingsW(device_name.as_ptr() as _, ENUM_CURRENT_SETTINGS, &mut dm) - { - bail!("EnumDisplaySettingsW failed, errno={}", GetLastError()); - } - // dmPelsWidth and dmPelsHeight is the same to width and height - // Because this process is running in dpi awareness mode. - if dm.dmPelsWidth == width as u32 && dm.dmPelsHeight == height as u32 { - return Ok(()); - } dm.dmPelsWidth = width as _; dm.dmPelsHeight = height as _; dm.dmFields = DM_PELSHEIGHT | DM_PELSWIDTH;