From 87b32ad8c4dfea67bed45d498d9402487384fdea Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sat, 10 Jun 2023 01:55:32 +0800 Subject: [PATCH] use autostart for --tray in linux because pkexec not work well when start it with --server --- res/rustdesk-link.desktop | 1 - res/rustdesk.desktop | 2 +- src/common.rs | 12 ++++++------ src/core_main.rs | 8 ++++++++ src/platform/linux.rs | 22 ++++++++++++++++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/res/rustdesk-link.desktop b/res/rustdesk-link.desktop index 7e22e9ea2..c64781aeb 100644 --- a/res/rustdesk-link.desktop +++ b/res/rustdesk-link.desktop @@ -8,4 +8,3 @@ Icon=rustdesk Terminal=false Type=Application StartupNotify=false -Version=1.5 diff --git a/res/rustdesk.desktop b/res/rustdesk.desktop index f31a16dec..b8abafa7f 100644 --- a/res/rustdesk.desktop +++ b/res/rustdesk.desktop @@ -1,5 +1,5 @@ [Desktop Entry] -Version=1.5 +Version=1.2.0 Name=RustDesk GenericName=Remote Desktop Comment=Remote Desktop diff --git a/src/common.rs b/src/common.rs index 8e5a81ed5..0cd656613 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1071,17 +1071,17 @@ pub fn check_process(arg: &str, same_uid: bool) -> bool { .map(|x| x.user_id()) .unwrap_or_default(); for (_, p) in sys.processes().iter() { + if p.pid().to_string() == std::process::id().to_string() { + continue; + } if same_uid && p.user_id() != my_uid { continue; } - if p.cmd().is_empty() || p.cmd()[0] != app { + if p.exe().to_string_lossy() != app { continue; } - if arg.is_empty() { - if p.cmd().len() == 1 { - return true; - } - } else if p.cmd().len() > 1 && p.cmd()[1] == arg { + let parg = if p.cmd().len() <= 1 { "" } else { &p.cmd()[1] }; + if arg == parg { return true; } } diff --git a/src/core_main.rs b/src/core_main.rs index 725cf92fc..6aa7d7db7 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -41,6 +41,14 @@ pub fn core_main() -> Option> { } i += 1; } + #[cfg(any(target_os = "linux", target_os = "windows"))] + if args.is_empty() { + #[cfg(target_os = "linux")] + hbb_common::allow_err!(crate::platform::check_autostart_config()); + if crate::check_process("--server", false) && !crate::check_process("--tray", true) { + hbb_common::allow_err!(crate::run_me(vec!["--tray"])); + } + } #[cfg(not(debug_assertions))] #[cfg(not(any(target_os = "android", target_os = "ios")))] register_breakdown_handler(breakdown_callback); diff --git a/src/platform/linux.rs b/src/platform/linux.rs index c0feada73..fa8acef48 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -11,6 +11,7 @@ use hbb_common::{ }; use std::{ cell::RefCell, + io::Write, path::{Path, PathBuf}, process::{Child, Command}, string::String, @@ -1170,3 +1171,24 @@ fn check_if_stop_service() { )); } } + +pub fn check_autostart_config() -> ResultType<()> { + let home = std::env::var("HOME").unwrap_or_default(); + let path = format!("{home}/.config/autostart"); + let file = format!("{path}/rustdesk.desktop"); + std::fs::create_dir_all(&path).ok(); + if !Path::new(&file).exists() { + // write text to the desktop file + let mut file = std::fs::File::create(&file)?; + file.write_all( + " +[Desktop Entry] +Type=Application +Exec=rustdesk --tray +NoDisplay=false + " + .as_bytes(), + )?; + } + Ok(()) +}