diff --git a/libs/hbb_common/src/platform/linux.rs b/libs/hbb_common/src/platform/linux.rs index 2b14fcd67..4aeeefb58 100644 --- a/libs/hbb_common/src/platform/linux.rs +++ b/libs/hbb_common/src/platform/linux.rs @@ -124,7 +124,7 @@ fn line_values(indices: &[usize], line: &str) -> Vec { #[inline] pub fn get_values_of_seat0(indices: &[usize]) -> Vec { - _get_values_of_seat0(indices, true) + _get_values_of_seat0(indices, true, 20) } #[inline] @@ -132,55 +132,51 @@ pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec { _get_values_of_seat0(indices, false) } -fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool) -> Vec { - let mut retry_attempt = 0; - loop{ - if let Ok(output) = run_loginctl(None) { - for line in String::from_utf8_lossy(&output.stdout).lines() { - if line.contains("seat0") { +fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec { + for _ in 0..attempts { + if let Ok(output) = run_loginctl(None) { + for line in String::from_utf8_lossy(&output.stdout).lines() { + if line.contains("seat0") { + if let Some(sid) = line.split_whitespace().next() { + if is_active(sid) { + if ignore_gdm_wayland { + if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) + && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND + { + continue; + } + } + return line_values(indices, line); + } + } + } + } + + // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 + for line in String::from_utf8_lossy(&output.stdout).lines() { if let Some(sid) = line.split_whitespace().next() { if is_active(sid) { + let d = get_display_server_of_session(sid); if ignore_gdm_wayland { if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) - && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND + && d == DISPLAY_SERVER_WAYLAND { continue; } } + if d == "tty" { + continue; + } return line_values(indices, line); } } } } - // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 - for line in String::from_utf8_lossy(&output.stdout).lines() { - if let Some(sid) = line.split_whitespace().next() { - if is_active(sid) { - let d = get_display_server_of_session(sid); - if ignore_gdm_wayland { - if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) - && d == DISPLAY_SERVER_WAYLAND - { - continue; - } - } - if d == "tty" { - continue; - } - return line_values(indices, line); - } - } - } - } - - if retry_attempt >= 1 { - return line_values(indices, ""); - } - // Increment the retry_attempt counter and wait for 5 seconds before retrying - retry_attempt += 1; - std::thread::sleep(Duration::from_secs(5)); + // Wait for 300ms and try again + std::thread::sleep(std::time::Duration::from_millis(300)); } + return line_values(indices, ""); } pub fn is_active(sid: &str) -> bool {