fix mismatch of current display index and current display name

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-05-18 22:56:16 +08:00
parent ac96ddaecb
commit c4cefdb54b
2 changed files with 13 additions and 9 deletions

View File

@ -1043,6 +1043,8 @@ impl Connection {
}) })
.into(); .into();
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
video_service::try_reset_current_display();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{ {
pi.resolutions = Some(SupportedResolutions { pi.resolutions = Some(SupportedResolutions {
resolutions: video_service::get_current_display_name() resolutions: video_service::get_current_display_name()

View File

@ -561,11 +561,6 @@ fn run(sp: GenericService) -> ResultType<()> {
log::debug!("Broadcasting display switch"); log::debug!("Broadcasting display switch");
let mut misc = Misc::new(); let mut misc = Misc::new();
let display_name = get_current_display_name().unwrap_or_default(); let display_name = get_current_display_name().unwrap_or_default();
println!(
"REMOVE ME ============================ display_name: {:?}, is_virtual: {}",
display_name,
is_virtual_display(&display_name)
);
let original_resolution = get_original_resolution(&display_name, c.width, c.height); let original_resolution = get_original_resolution(&display_name, c.width, c.height);
misc.set_switch_display(SwitchDisplay { misc.set_switch_display(SwitchDisplay {
display: c.current as _, display: c.current as _,
@ -967,6 +962,15 @@ pub fn is_inited_msg() -> Option<Message> {
None None
} }
// switch to primary display if long time (30 seconds) no users
#[inline]
pub fn try_reset_current_display() {
if LAST_ACTIVE.lock().unwrap().elapsed().as_secs() >= 30 {
*CURRENT_DISPLAY.lock().unwrap() = usize::MAX;
}
*LAST_ACTIVE.lock().unwrap() = time::Instant::now();
}
pub async fn get_displays() -> ResultType<(usize, Vec<DisplayInfo>)> { pub async fn get_displays() -> ResultType<(usize, Vec<DisplayInfo>)> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
@ -974,10 +978,6 @@ pub async fn get_displays() -> ResultType<(usize, Vec<DisplayInfo>)> {
return super::wayland::get_displays().await; return super::wayland::get_displays().await;
} }
} }
// switch to primary display if long time (30 seconds) no users
if LAST_ACTIVE.lock().unwrap().elapsed().as_secs() >= 30 {
*CURRENT_DISPLAY.lock().unwrap() = usize::MAX;
}
Ok(get_displays_2(&try_get_displays()?)) Ok(get_displays_2(&try_get_displays()?))
} }
@ -1071,6 +1071,8 @@ pub fn get_current_display() -> ResultType<(usize, usize, Display)> {
get_current_display_2(try_get_displays()?) get_current_display_2(try_get_displays()?)
} }
// `try_reset_current_display` is needed because `get_displays` may change the current display,
// which may cause the mismatch of current display and the current display name.
pub fn get_current_display_name() -> ResultType<String> { pub fn get_current_display_name() -> ResultType<String> {
Ok(get_current_display_2(try_get_displays()?)?.2.name()) Ok(get_current_display_2(try_get_displays()?)?.2.name())
} }