2022-07-20 10:44:27 -07:00
|
|
|
use crate::ResultType;
|
|
|
|
|
2022-10-11 20:36:19 -07:00
|
|
|
lazy_static::lazy_static! {
|
2023-01-09 03:10:42 -05:00
|
|
|
pub static ref DISTRO: Distro = Distro::new();
|
2022-10-11 20:36:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-09 03:10:42 -05:00
|
|
|
pub struct Distro {
|
2022-10-11 20:36:19 -07:00
|
|
|
pub name: String,
|
|
|
|
pub version_id: String,
|
|
|
|
}
|
|
|
|
|
2023-01-09 03:10:42 -05:00
|
|
|
impl Distro {
|
2022-10-11 20:36:19 -07:00
|
|
|
fn new() -> Self {
|
|
|
|
let name = run_cmds("awk -F'=' '/^NAME=/ {print $2}' /etc/os-release".to_owned())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.trim()
|
|
|
|
.trim_matches('"')
|
|
|
|
.to_string();
|
|
|
|
let version_id =
|
|
|
|
run_cmds("awk -F'=' '/^VERSION_ID=/ {print $2}' /etc/os-release".to_owned())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.trim()
|
|
|
|
.trim_matches('"')
|
|
|
|
.to_string();
|
|
|
|
Self { name, version_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:44:27 -07:00
|
|
|
pub fn get_display_server() -> String {
|
2022-10-17 07:55:14 -07:00
|
|
|
let mut session = get_values_of_seat0([0].to_vec())[0].clone();
|
|
|
|
if session.is_empty() {
|
|
|
|
// loginctl has not given the expected output. try something else.
|
|
|
|
if let Ok(sid) = std::env::var("XDG_SESSION_ID") {
|
|
|
|
// could also execute "cat /proc/self/sessionid"
|
|
|
|
session = sid.to_owned();
|
|
|
|
}
|
|
|
|
if session.is_empty() {
|
|
|
|
session = run_cmds("cat /proc/self/sessionid".to_owned()).unwrap_or_default();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:44:27 -07:00
|
|
|
get_display_server_of_session(&session)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_display_server_of_session(session: &str) -> String {
|
2022-12-08 17:08:31 +08:00
|
|
|
let mut display_server = if let Ok(output) =
|
|
|
|
run_loginctl(Some(vec!["show-session", "-p", "Type", session]))
|
2022-07-20 10:44:27 -07:00
|
|
|
// Check session type of the session
|
|
|
|
{
|
|
|
|
let display_server = String::from_utf8_lossy(&output.stdout)
|
|
|
|
.replace("Type=", "")
|
|
|
|
.trim_end()
|
|
|
|
.into();
|
|
|
|
if display_server == "tty" {
|
|
|
|
// If the type is tty...
|
2022-10-16 21:45:59 +02:00
|
|
|
if let Ok(output) = run_loginctl(Some(vec!["show-session", "-p", "TTY", session]))
|
2022-07-20 10:44:27 -07:00
|
|
|
// Get the tty number
|
|
|
|
{
|
|
|
|
let tty: String = String::from_utf8_lossy(&output.stdout)
|
|
|
|
.replace("TTY=", "")
|
|
|
|
.trim_end()
|
|
|
|
.into();
|
|
|
|
if let Ok(xorg_results) = run_cmds(format!("ps -e | grep \"{}.\\\\+Xorg\"", tty))
|
|
|
|
// And check if Xorg is running on that tty
|
|
|
|
{
|
|
|
|
if xorg_results.trim_end().to_string() != "" {
|
|
|
|
// If it is, manually return "x11", otherwise return tty
|
2022-12-08 23:07:22 +08:00
|
|
|
return "x11".to_owned();
|
2022-07-20 10:44:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 23:05:51 +08:00
|
|
|
display_server
|
2022-07-20 10:44:27 -07:00
|
|
|
} else {
|
|
|
|
"".to_owned()
|
2022-12-08 17:08:31 +08:00
|
|
|
};
|
2022-12-08 17:43:46 +08:00
|
|
|
if display_server.is_empty() {
|
|
|
|
// loginctl has not given the expected output. try something else.
|
|
|
|
if let Ok(sestype) = std::env::var("XDG_SESSION_TYPE") {
|
|
|
|
display_server = sestype;
|
|
|
|
}
|
2022-07-20 10:44:27 -07:00
|
|
|
}
|
2022-12-08 17:08:31 +08:00
|
|
|
// If the session is not a tty, then just return the type as usual
|
|
|
|
display_server
|
2022-07-20 10:44:27 -07:00
|
|
|
}
|
|
|
|
|
2022-10-17 07:55:14 -07:00
|
|
|
pub fn get_values_of_seat0(indices: Vec<usize>) -> Vec<String> {
|
2022-10-16 21:45:59 +02:00
|
|
|
if let Ok(output) = run_loginctl(None) {
|
2022-07-20 10:44:27 -07:00
|
|
|
for line in String::from_utf8_lossy(&output.stdout).lines() {
|
|
|
|
if line.contains("seat0") {
|
|
|
|
if let Some(sid) = line.split_whitespace().nth(0) {
|
|
|
|
if is_active(sid) {
|
2022-10-17 07:55:14 -07:00
|
|
|
return indices
|
|
|
|
.into_iter()
|
|
|
|
.map(|idx| line.split_whitespace().nth(idx).unwrap_or("").to_owned())
|
|
|
|
.collect::<Vec<String>>();
|
2022-07-20 10:44:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73
|
2022-10-16 21:45:59 +02:00
|
|
|
if let Ok(output) = run_loginctl(None) {
|
2022-07-20 10:44:27 -07:00
|
|
|
for line in String::from_utf8_lossy(&output.stdout).lines() {
|
|
|
|
if let Some(sid) = line.split_whitespace().nth(0) {
|
|
|
|
let d = get_display_server_of_session(sid);
|
|
|
|
if is_active(sid) && d != "tty" {
|
2022-10-17 07:55:14 -07:00
|
|
|
return indices
|
|
|
|
.into_iter()
|
|
|
|
.map(|idx| line.split_whitespace().nth(idx).unwrap_or("").to_owned())
|
|
|
|
.collect::<Vec<String>>();
|
2022-07-20 10:44:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:55:14 -07:00
|
|
|
return indices
|
|
|
|
.iter()
|
|
|
|
.map(|_x| "".to_owned())
|
|
|
|
.collect::<Vec<String>>();
|
2022-07-20 10:44:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_active(sid: &str) -> bool {
|
2022-12-08 17:08:31 +08:00
|
|
|
if let Ok(output) = run_loginctl(Some(vec!["show-session", "-p", "State", sid])) {
|
2022-07-20 10:44:27 -07:00
|
|
|
String::from_utf8_lossy(&output.stdout).contains("active")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_cmds(cmds: String) -> ResultType<String> {
|
|
|
|
let output = std::process::Command::new("sh")
|
|
|
|
.args(vec!["-c", &cmds])
|
|
|
|
.output()?;
|
|
|
|
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
|
|
|
}
|
2022-10-16 21:45:59 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "flatpak"))]
|
|
|
|
fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> {
|
|
|
|
let mut cmd = std::process::Command::new("loginctl");
|
|
|
|
if let Some(a) = args {
|
|
|
|
return cmd.args(a).output();
|
|
|
|
}
|
|
|
|
cmd.output()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "flatpak")]
|
|
|
|
fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> {
|
|
|
|
let mut l_args = String::from("loginctl");
|
|
|
|
if let Some(a) = args {
|
|
|
|
l_args = format!("{} {}", l_args, a.join(" "));
|
|
|
|
}
|
|
|
|
std::process::Command::new("flatpak-spawn")
|
|
|
|
.args(vec![String::from("--host"), l_args])
|
|
|
|
.output()
|
|
|
|
}
|