add wrapper function _get_values_of_seat0_tries for attempts

Signed-off-by: Sahil Yeole <sahilyeole93@gmail.com>
This commit is contained in:
Sahil Yeole 2023-07-31 20:54:05 +05:30
parent cf6fbae30a
commit cc9f69d63b

View File

@ -124,59 +124,67 @@ fn line_values(indices: &[usize], line: &str) -> Vec<String> {
#[inline] #[inline]
pub fn get_values_of_seat0(indices: &[usize]) -> Vec<String> { pub fn get_values_of_seat0(indices: &[usize]) -> Vec<String> {
_get_values_of_seat0(indices, true, 20) _get_values_of_seat0_tries(indices, true, 20)
} }
#[inline] #[inline]
pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec<String> { pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec<String> {
_get_values_of_seat0(indices, false, 20) _get_values_of_seat0_tries(indices, false, 20)
} }
fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec<String> { fn _get_values_of_seat0_tries(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec<String> {
for _ in 0..attempts { for _ in 0..attempts{
if let Ok(output) = run_loginctl(None) { let value = _get_values_of_seat0(indices, ignore_gdm_wayland);
for line in String::from_utf8_lossy(&output.stdout).lines() { if value != line_values(indices, "") {
if line.contains("seat0") { return value;
if let Some(sid) = line.split_whitespace().next() { }
if is_active(sid) { // Wait for 300ms and try again
if ignore_gdm_wayland { std::thread::sleep(std::time::Duration::from_millis(300));
if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) }
&& get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND line_values(indices, "")
{ }
continue;
}
}
return line_values(indices, line);
}
}
}
}
// some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool) -> Vec<String> {
for line in String::from_utf8_lossy(&output.stdout).lines() { 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 let Some(sid) = line.split_whitespace().next() {
if is_active(sid) { if is_active(sid) {
let d = get_display_server_of_session(sid);
if ignore_gdm_wayland { if ignore_gdm_wayland {
if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) if is_gdm_user(line.split_whitespace().nth(2).unwrap_or(""))
&& d == DISPLAY_SERVER_WAYLAND && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND
{ {
continue; continue;
} }
} }
if d == "tty" {
continue;
}
return line_values(indices, line); return line_values(indices, line);
} }
} }
} }
} }
// Wait for 300ms and try again // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73
std::thread::sleep(std::time::Duration::from_millis(300)); 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);
}
}
}
} }
return line_values(indices, "");
line_values(indices, "")
} }
pub fn is_active(sid: &str) -> bool { pub fn is_active(sid: &str) -> bool {