linux_wayland_support: init merge, windows build

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2022-07-07 01:27:21 +08:00
Unverified
parent 6533b30cac
commit aae6e2b16b
30 changed files with 1902 additions and 656 deletions

View File

@@ -143,7 +143,75 @@ pub fn get_cursor_data(hcursor: u64) -> ResultType<CursorData> {
}
}
fn start_uinput_service() {
use crate::server::input_service::uinput::service;
std::thread::spawn(|| {
service::start_service_control();
});
std::thread::spawn(|| {
service::start_service_keyboard();
});
std::thread::spawn(|| {
service::start_service_mouse();
});
}
fn try_start_user_service(username: &str) {
if username == "" || username == "root" {
return;
}
if let Ok(mut cur_username) =
run_cmds("ps -ef | grep -E 'rustdesk +--server' | awk '{print $1}' | head -1".to_owned())
{
cur_username = cur_username.trim().to_owned();
if cur_username != "root" && cur_username != username {
let _ = run_cmds(format!(
"systemctl --machine={}@.host --user stop rustdesk",
&cur_username
));
} else if cur_username == username {
return;
}
}
let _ = run_cmds(format!(
"systemctl --machine={}@.host --user start rustdesk",
username
));
}
fn try_stop_user_service() {
if let Ok(mut username) =
run_cmds("ps -ef | grep -E 'rustdesk +--server' | awk '{print $1}' | head -1".to_owned())
{
username = username.trim().to_owned();
if username != "root" {
let _ = run_cmds(format!(
"systemctl --machine={}@.host --user stop rustdesk",
&username
));
}
}
}
fn stop_server(server: &mut Option<std::process::Child>) {
if let Some(mut ps) = server.take() {
allow_err!(ps.kill());
std::thread::sleep(std::time::Duration::from_millis(30));
match ps.try_wait() {
Ok(Some(_status)) => {}
Ok(None) => {
let _res = ps.wait();
}
Err(e) => log::error!("error attempting to wait: {e}"),
}
}
}
pub fn start_os_service() {
start_uinput_service();
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let mut uid = "".to_owned();
@@ -157,85 +225,106 @@ pub fn start_os_service() {
let mut cm0 = false;
let mut last_restart = std::time::Instant::now();
while running.load(Ordering::SeqCst) {
let cm = get_cm();
let tmp = get_active_userid();
let mut start_new = false;
if tmp != uid && !tmp.is_empty() {
uid = tmp;
log::info!("uid of seat0: {}", uid);
let gdm = format!("/run/user/{}/gdm/Xauthority", uid);
let mut auth = get_env_tries("XAUTHORITY", &uid, 10);
if auth.is_empty() {
auth = if std::path::Path::new(&gdm).exists() {
gdm
} else {
let username = get_active_username();
if username == "root" {
format!("/{}/.Xauthority", username)
let username = get_active_username();
let is_wayland = current_is_wayland();
if username == "root" || !is_wayland {
// try stop user service
try_stop_user_service();
// try start subprocess "--server"
let cm = get_cm();
let tmp = get_active_userid();
let mut start_new = false;
if tmp != uid && !tmp.is_empty() {
uid = tmp;
log::info!("uid of seat0: {}", uid);
let gdm = format!("/run/user/{}/gdm/Xauthority", uid);
let mut auth = get_env_tries("XAUTHORITY", &uid, 10);
if auth.is_empty() {
auth = if std::path::Path::new(&gdm).exists() {
gdm
} else {
let tmp = format!("/home/{}/.Xauthority", username);
if std::path::Path::new(&tmp).exists() {
tmp
let username = get_active_username();
if username == "root" {
format!("/{}/.Xauthority", username)
} else {
format!("/var/lib/{}/.Xauthority", username)
let tmp = format!("/home/{}/.Xauthority", username);
if std::path::Path::new(&tmp).exists() {
tmp
} else {
format!("/var/lib/{}/.Xauthority", username)
}
}
}
};
}
let mut d = get_env("DISPLAY", &uid);
if d.is_empty() {
d = get_display();
}
if d.is_empty() {
d = ":0".to_owned();
}
d = d.replace(&whoami::hostname(), "").replace("localhost", "");
log::info!("DISPLAY: {}", d);
log::info!("XAUTHORITY: {}", auth);
std::env::set_var("XAUTHORITY", auth);
std::env::set_var("DISPLAY", d);
if let Some(ps) = server.as_mut() {
allow_err!(ps.kill());
std::thread::sleep(std::time::Duration::from_millis(30));
last_restart = std::time::Instant::now();
}
} else if !cm
&& ((cm0 && last_restart.elapsed().as_secs() > 60)
|| last_restart.elapsed().as_secs() > 3600)
{
// restart server if new connections all closed, or every one hour,
// as a workaround to resolve "SpotUdp" (dns resolve)
// and x server get displays failure issue
if let Some(ps) = server.as_mut() {
allow_err!(ps.kill());
std::thread::sleep(std::time::Duration::from_millis(30));
last_restart = std::time::Instant::now();
log::info!("restart server");
}
}
if let Some(ps) = server.as_mut() {
match ps.try_wait() {
Ok(Some(_)) => {
server = None;
start_new = true;
};
}
_ => {}
let mut d = get_env("DISPLAY", &uid);
if d.is_empty() {
d = get_display();
}
if d.is_empty() {
d = ":0".to_owned();
}
d = d.replace(&whoami::hostname(), "").replace("localhost", "");
log::info!("DISPLAY: {}", d);
log::info!("XAUTHORITY: {}", auth);
std::env::set_var("XAUTHORITY", auth);
std::env::set_var("DISPLAY", d);
if let Some(ps) = server.as_mut() {
allow_err!(ps.kill());
std::thread::sleep(std::time::Duration::from_millis(30));
last_restart = std::time::Instant::now();
}
} else if !cm
&& ((cm0 && last_restart.elapsed().as_secs() > 60)
|| last_restart.elapsed().as_secs() > 3600)
{
// restart server if new connections all closed, or every one hour,
// as a workaround to resolve "SpotUdp" (dns resolve)
// and x server get displays failure issue
if let Some(ps) = server.as_mut() {
allow_err!(ps.kill());
std::thread::sleep(std::time::Duration::from_millis(30));
last_restart = std::time::Instant::now();
log::info!("restart server");
}
}
if let Some(ps) = server.as_mut() {
match ps.try_wait() {
Ok(Some(_)) => {
server = None;
start_new = true;
}
_ => {}
}
} else {
start_new = true;
}
if start_new {
match crate::run_me(vec!["--server"]) {
Ok(ps) => server = Some(ps),
Err(err) => {
log::error!("Failed to start server: {}", err);
}
}
}
cm0 = cm;
} else if username != "" {
if username != "gdm" {
// try kill subprocess "--server"
stop_server(&mut server);
// try start user service
try_start_user_service(&username);
}
} else {
start_new = true;
try_stop_user_service();
stop_server(&mut server);
}
if start_new {
match crate::run_me(vec!["--server"]) {
Ok(ps) => server = Some(ps),
Err(err) => {
log::error!("Failed to start server: {}", err);
}
}
}
cm0 = cm;
std::thread::sleep(std::time::Duration::from_millis(super::SERVICE_INTERVAL));
}
try_stop_user_service();
if let Some(ps) = server.take().as_mut() {
allow_err!(ps.kill());
}