|
|
@@ -1,157 +0,0 @@
|
|
1
|
|
-use crate::ResultType;
|
|
2
|
|
-
|
|
3
|
|
-lazy_static::lazy_static! {
|
|
4
|
|
- pub static ref DISTRO: Distro = Distro::new();
|
|
5
|
|
-}
|
|
6
|
|
-
|
|
7
|
|
-pub struct Distro {
|
|
8
|
|
- pub name: String,
|
|
9
|
|
- pub version_id: String,
|
|
10
|
|
-}
|
|
11
|
|
-
|
|
12
|
|
-impl Distro {
|
|
13
|
|
- fn new() -> Self {
|
|
14
|
|
- let name = run_cmds("awk -F'=' '/^NAME=/ {print $2}' /etc/os-release".to_owned())
|
|
15
|
|
- .unwrap_or_default()
|
|
16
|
|
- .trim()
|
|
17
|
|
- .trim_matches('"')
|
|
18
|
|
- .to_string();
|
|
19
|
|
- let version_id =
|
|
20
|
|
- run_cmds("awk -F'=' '/^VERSION_ID=/ {print $2}' /etc/os-release".to_owned())
|
|
21
|
|
- .unwrap_or_default()
|
|
22
|
|
- .trim()
|
|
23
|
|
- .trim_matches('"')
|
|
24
|
|
- .to_string();
|
|
25
|
|
- Self { name, version_id }
|
|
26
|
|
- }
|
|
27
|
|
-}
|
|
28
|
|
-
|
|
29
|
|
-pub fn get_display_server() -> String {
|
|
30
|
|
- let mut session = get_values_of_seat0([0].to_vec())[0].clone();
|
|
31
|
|
- if session.is_empty() {
|
|
32
|
|
- // loginctl has not given the expected output. try something else.
|
|
33
|
|
- if let Ok(sid) = std::env::var("XDG_SESSION_ID") {
|
|
34
|
|
- // could also execute "cat /proc/self/sessionid"
|
|
35
|
|
- session = sid;
|
|
36
|
|
- }
|
|
37
|
|
- if session.is_empty() {
|
|
38
|
|
- session = run_cmds("cat /proc/self/sessionid".to_owned()).unwrap_or_default();
|
|
39
|
|
- }
|
|
40
|
|
- }
|
|
41
|
|
-
|
|
42
|
|
- get_display_server_of_session(&session)
|
|
43
|
|
-}
|
|
44
|
|
-
|
|
45
|
|
-fn get_display_server_of_session(session: &str) -> String {
|
|
46
|
|
- let mut display_server = if let Ok(output) =
|
|
47
|
|
- run_loginctl(Some(vec!["show-session", "-p", "Type", session]))
|
|
48
|
|
- // Check session type of the session
|
|
49
|
|
- {
|
|
50
|
|
- let display_server = String::from_utf8_lossy(&output.stdout)
|
|
51
|
|
- .replace("Type=", "")
|
|
52
|
|
- .trim_end()
|
|
53
|
|
- .into();
|
|
54
|
|
- if display_server == "tty" {
|
|
55
|
|
- // If the type is tty...
|
|
56
|
|
- if let Ok(output) = run_loginctl(Some(vec!["show-session", "-p", "TTY", session]))
|
|
57
|
|
- // Get the tty number
|
|
58
|
|
- {
|
|
59
|
|
- let tty: String = String::from_utf8_lossy(&output.stdout)
|
|
60
|
|
- .replace("TTY=", "")
|
|
61
|
|
- .trim_end()
|
|
62
|
|
- .into();
|
|
63
|
|
- if let Ok(xorg_results) = run_cmds(format!("ps -e | grep \"{tty}.\\\\+Xorg\""))
|
|
64
|
|
- // And check if Xorg is running on that tty
|
|
65
|
|
- {
|
|
66
|
|
- if xorg_results.trim_end() != "" {
|
|
67
|
|
- // If it is, manually return "x11", otherwise return tty
|
|
68
|
|
- return "x11".to_owned();
|
|
69
|
|
- }
|
|
70
|
|
- }
|
|
71
|
|
- }
|
|
72
|
|
- }
|
|
73
|
|
- display_server
|
|
74
|
|
- } else {
|
|
75
|
|
- "".to_owned()
|
|
76
|
|
- };
|
|
77
|
|
- if display_server.is_empty() || display_server == "tty" {
|
|
78
|
|
- // loginctl has not given the expected output. try something else.
|
|
79
|
|
- if let Ok(sestype) = std::env::var("XDG_SESSION_TYPE") {
|
|
80
|
|
- display_server = sestype;
|
|
81
|
|
- }
|
|
82
|
|
- }
|
|
83
|
|
- // If the session is not a tty, then just return the type as usual
|
|
84
|
|
- display_server
|
|
85
|
|
-}
|
|
86
|
|
-
|
|
87
|
|
-pub fn get_values_of_seat0(indices: Vec<usize>) -> Vec<String> {
|
|
88
|
|
- if let Ok(output) = run_loginctl(None) {
|
|
89
|
|
- for line in String::from_utf8_lossy(&output.stdout).lines() {
|
|
90
|
|
- if line.contains("seat0") {
|
|
91
|
|
- if let Some(sid) = line.split_whitespace().next() {
|
|
92
|
|
- if is_active(sid) {
|
|
93
|
|
- return indices
|
|
94
|
|
- .into_iter()
|
|
95
|
|
- .map(|idx| line.split_whitespace().nth(idx).unwrap_or("").to_owned())
|
|
96
|
|
- .collect::<Vec<String>>();
|
|
97
|
|
- }
|
|
98
|
|
- }
|
|
99
|
|
- }
|
|
100
|
|
- }
|
|
101
|
|
- }
|
|
102
|
|
-
|
|
103
|
|
- // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73
|
|
104
|
|
- if let Ok(output) = run_loginctl(None) {
|
|
105
|
|
- for line in String::from_utf8_lossy(&output.stdout).lines() {
|
|
106
|
|
- if let Some(sid) = line.split_whitespace().next() {
|
|
107
|
|
- let d = get_display_server_of_session(sid);
|
|
108
|
|
- if is_active(sid) && d != "tty" {
|
|
109
|
|
- return indices
|
|
110
|
|
- .into_iter()
|
|
111
|
|
- .map(|idx| line.split_whitespace().nth(idx).unwrap_or("").to_owned())
|
|
112
|
|
- .collect::<Vec<String>>();
|
|
113
|
|
- }
|
|
114
|
|
- }
|
|
115
|
|
- }
|
|
116
|
|
- }
|
|
117
|
|
-
|
|
118
|
|
- return indices
|
|
119
|
|
- .iter()
|
|
120
|
|
- .map(|_x| "".to_owned())
|
|
121
|
|
- .collect::<Vec<String>>();
|
|
122
|
|
-}
|
|
123
|
|
-
|
|
124
|
|
-fn is_active(sid: &str) -> bool {
|
|
125
|
|
- if let Ok(output) = run_loginctl(Some(vec!["show-session", "-p", "State", sid])) {
|
|
126
|
|
- String::from_utf8_lossy(&output.stdout).contains("active")
|
|
127
|
|
- } else {
|
|
128
|
|
- false
|
|
129
|
|
- }
|
|
130
|
|
-}
|
|
131
|
|
-
|
|
132
|
|
-pub fn run_cmds(cmds: String) -> ResultType<String> {
|
|
133
|
|
- let output = std::process::Command::new("sh")
|
|
134
|
|
- .args(vec!["-c", &cmds])
|
|
135
|
|
- .output()?;
|
|
136
|
|
- Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
|
137
|
|
-}
|
|
138
|
|
-
|
|
139
|
|
-#[cfg(not(feature = "flatpak"))]
|
|
140
|
|
-fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> {
|
|
141
|
|
- let mut cmd = std::process::Command::new("loginctl");
|
|
142
|
|
- if let Some(a) = args {
|
|
143
|
|
- return cmd.args(a).output();
|
|
144
|
|
- }
|
|
145
|
|
- cmd.output()
|
|
146
|
|
-}
|
|
147
|
|
-
|
|
148
|
|
-#[cfg(feature = "flatpak")]
|
|
149
|
|
-fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> {
|
|
150
|
|
- let mut l_args = String::from("loginctl");
|
|
151
|
|
- if let Some(a) = args {
|
|
152
|
|
- l_args = format!("{} {}", l_args, a.join(" "));
|
|
153
|
|
- }
|
|
154
|
|
- std::process::Command::new("flatpak-spawn")
|
|
155
|
|
- .args(vec![String::from("--host"), l_args])
|
|
156
|
|
- .output()
|
|
157
|
|
-}
|