From d4483e2bbd891a6776bf28e36dd4e78d8d970254 Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Thu, 15 Oct 2026 23:02:08 +0800 Subject: [PATCH] Tray icon (AppIndicator via dlopen, GtkStatusIcon fallback); show restarts the service Tray menu: open main window, allow switch (synced), quit. Quit stops the systemd user unit (found via /proc/self/cgroup) so Restart=always does not bring it back; `uuremote-server show` starts the installed unit when the host is not running. SIGUSR1 handler is installed before the pid file is written. Verified on Ubuntu 24.04 GNOME (X11). Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.en.md | 3 ++ README.md | 1 + c_engine/assist_window.c | 70 ++++++++++++++++++++++++++++++++++- docs/release-notes/v0.1.13.md | 17 +++++++++ src/assist.rs | 3 +- src/main.rs | 39 +++++++++++++++++-- 8 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 docs/release-notes/v0.1.13.md diff --git a/Cargo.lock b/Cargo.lock index 733ac0b..afb3c22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2649,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uu-remote-linux" -version = "0.1.12" +version = "0.1.13" dependencies = [ "base64 0.22.1", "bytes", diff --git a/Cargo.toml b/Cargo.toml index efedcce..96a3b3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu-remote-linux" -version = "0.1.12" +version = "0.1.13" edition = "2021" authors = ["uu-remote-linux contributors"] description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients" diff --git a/README.en.md b/README.en.md index 62f940b..35b8c37 100644 --- a/README.en.md +++ b/README.en.md @@ -191,6 +191,9 @@ connect from 开始协助 (remote assistance) in UU Remote. - **Signed in**: the host is in your device list and remote assistance keeps working (same device ID). A background service keeps the window hidden; open it with `uuremote-server show` or the "UU Remote Linux Host" launcher. Closing the window only hides it. +- **Tray icon**: a background host shows a tray icon with Open main window, the allow switch and Quit. GNOME needs + the AppIndicator extension (bundled on Ubuntu); Xfce / Cinnamon / KDE work as is. Quit stops `uuremote-native.service`; + the launcher starts it again. - **Settings** (as in the official client): allow switch (off by default); verification by temporary code, custom code (8–16 letters and digits) or either; temporary code rotation after each session / hourly / 12 h / 24 h / never; copy buttons for the ID and the code. Stored in the 0600 config. diff --git a/README.md b/README.md index 76a6e82..c55889d 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,7 @@ X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放 - **未登录**:启动后直接显示主窗口,用访客身份提供远程协助,同时显示扫码登录二维码。扫码登录后自动切换到账号模式。 - **已登录**:本机出现在你的设备列表里,可以直接连;远程协助也照常可用(同一设备 ID)。服务在后台运行时窗口默认隐藏,用 `uuremote-server show` 或应用菜单里的「UU 远程 Linux 主机」打开。关闭窗口只是隐藏,不会停止服务。 +- **托盘图标**:后台运行时顶栏/任务栏有托盘图标,菜单里可以「打开主窗口」、切换「允许他人远程协助」、「退出」。GNOME 需要 AppIndicator 扩展(Ubuntu 自带),Xfce / Cinnamon / KDE 直接可用。从托盘退出会停止 `uuremote-native.service`,之后从应用菜单打开会重新启动服务。 - **设置**(与官方一致):「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位,需同时包含字母和数字) / 两种都可以;临时验证码自动刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码旁各有「复制」按钮。设置保存在本机配置文件(0600)。 - **只在本机窗口显示**:验证码不会写进日志。没有图形界面时,单独运行 `uuremote-server assist` 会在终端显示(`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`)。 - **安全**:拿到 ID 和验证码的人可以完全控制这台电脑(桌面、文件、终端)。只发给信任的人,用完关掉开关或刷新验证码。 diff --git a/c_engine/assist_window.c b/c_engine/assist_window.c index 1bbeaca..0581910 100644 --- a/c_engine/assist_window.c +++ b/c_engine/assist_window.c @@ -7,6 +7,7 @@ #include #include #include +#include static const char *CSS = "window { background-color: #f5f6f8; }\n" @@ -27,7 +28,7 @@ static const char *CSS = ".status.ok { color: #16a34a; }\n" ".status.error { color: #dc2626; }\n"; -enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8 }; +enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 }; enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 }; typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State; @@ -47,6 +48,9 @@ static GtkWidget *refresh_items[5], *footer_label; // Account card: QR login before sign-in, user info (+ logout) after. static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label; static int show_assist = 1, show_account; +// Tray (host service only): AppIndicator via dlopen (no build dependency), else GtkStatusIcon. +static GtkWidget *tray_menu, *tray_allow; +static int suppress_tray; static int cur_verify, cur_refresh, cur_has_custom; static char current_id[64], current_code[64]; static int code_visible = 1, suppress_switch, start_hidden, close_hides; @@ -274,6 +278,68 @@ static GtkWidget *build_account_card(void) { return card; } +static void present_window(void) { if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); } } +static void on_tray_open(GtkMenuItem *i, gpointer d) { (void)i; (void)d; present_window(); } +static void on_tray_allow(GtkCheckMenuItem *i, gpointer d) { + (void)d; + if (!suppress_tray) post(gtk_check_menu_item_get_active(i) ? CMD_ALLOW : CMD_DENY); +} +static void on_tray_quit(GtkMenuItem *i, gpointer d) { + (void)i; (void)d; + GtkWidget *dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, + "退出后本机将下线:设备列表和远程协助都无法连接,直到再次启动。确定退出吗?"); + gtk_window_set_title(GTK_WINDOW(dlg), "UU 远程 Linux 主机"); + if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) post(CMD_QUIT); + gtk_widget_destroy(dlg); +} +static void on_status_activate(GtkStatusIcon *i, gpointer d) { (void)i; (void)d; present_window(); } +static void on_status_menu(GtkStatusIcon *i, guint button, guint time, gpointer d) { + (void)i; (void)button; (void)time; (void)d; + gtk_menu_popup_at_pointer(GTK_MENU(tray_menu), NULL); +} +static void build_tray(void) { + tray_menu = gtk_menu_new(); + GtkWidget *open = gtk_menu_item_new_with_label("打开主窗口"); + g_signal_connect(open, "activate", G_CALLBACK(on_tray_open), NULL); + tray_allow = gtk_check_menu_item_new_with_label("允许他人远程协助"); + g_signal_connect(tray_allow, "toggled", G_CALLBACK(on_tray_allow), NULL); + GtkWidget *quit = gtk_menu_item_new_with_label("退出"); + g_signal_connect(quit, "activate", G_CALLBACK(on_tray_quit), NULL); + gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), open); + gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), tray_allow); + gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), gtk_separator_menu_item_new()); + gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), quit); + gtk_widget_show_all(tray_menu); + const char *icon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), "uu-remote-linux") ? "uu-remote-linux" : "computer"; + + // StatusNotifierItem (GNOME AppIndicator extension, KDE): libayatana-appindicator3 / libappindicator3. + const char *libs[] = { "libayatana-appindicator3.so.1", "libappindicator3.so.1" }; + for (int i = 0; i < 2; i++) { + void *h = dlopen(libs[i], RTLD_LAZY | RTLD_LOCAL); + if (!h) continue; + void *(*ind_new)(const char *, const char *, int) = (void *(*)(const char *, const char *, int))dlsym(h, "app_indicator_new"); + void (*set_status)(void *, int) = (void (*)(void *, int))dlsym(h, "app_indicator_set_status"); + void (*set_menu)(void *, GtkMenu *) = (void (*)(void *, GtkMenu *))dlsym(h, "app_indicator_set_menu"); + void (*set_title)(void *, const char *) = (void (*)(void *, const char *))dlsym(h, "app_indicator_set_title"); + if (ind_new && set_status && set_menu) { + void *ind = ind_new("uu-remote-linux", icon, 0 /* APPLICATION_STATUS */); + if (set_title) set_title(ind, "UU 远程 Linux 主机"); + set_menu(ind, GTK_MENU(tray_menu)); + set_status(ind, 1 /* ACTIVE */); + return; // keep the library loaded for the indicator's lifetime + } + dlclose(h); + } + // XEmbed system tray (Xfce, Cinnamon, MATE...). + G_GNUC_BEGIN_IGNORE_DEPRECATIONS + GtkStatusIcon *status = gtk_status_icon_new_from_icon_name(icon); + gtk_status_icon_set_tooltip_text(status, "UU 远程 Linux 主机"); + gtk_status_icon_set_title(status, "UU 远程 Linux 主机"); + g_signal_connect(status, "activate", G_CALLBACK(on_status_activate), NULL); + g_signal_connect(status, "popup-menu", G_CALLBACK(on_status_menu), NULL); + G_GNUC_END_IGNORE_DEPRECATIONS +} + static void build_window(void) { GtkCssProvider *css = gtk_css_provider_new(); gtk_css_provider_load_from_data(css, CSS, -1, NULL); @@ -398,6 +464,7 @@ static void build_window(void) { gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0); if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行"); render_code(); + if (close_hides) build_tray(); gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window))); gtk_widget_set_visible(assist_card, show_assist); gtk_widget_set_visible(hint_label, show_assist); @@ -465,6 +532,7 @@ static gboolean apply_state(gpointer data) { suppress_switch = 1; gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed); suppress_switch = 0; + if (tray_allow) { suppress_tray = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tray_allow), s->allowed); suppress_tray = 0; } cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom; suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0; if (s->refresh >= 0 && s->refresh < 5) { diff --git a/docs/release-notes/v0.1.13.md b/docs/release-notes/v0.1.13.md new file mode 100644 index 0000000..91e3a85 --- /dev/null +++ b/docs/release-notes/v0.1.13.md @@ -0,0 +1,17 @@ +托盘图标。/ Tray icon. + +### 新增 / Added +- **托盘图标**:后台运行时,顶栏/任务栏会显示托盘图标,菜单里有「打开主窗口」、「允许他人远程协助」(与主窗口开关同步)和「退出」。 + - GNOME 通过 AppIndicator 显示(Ubuntu 自带扩展;运行时加载 libayatana-appindicator,无需额外编译依赖);Xfce / Cinnamon / KDE 等使用系统托盘。 + - 「退出」会停止 `uuremote-native.service`(不会被自动重启);之后从应用菜单「UU 远程 Linux 主机」打开,会重新启动服务并显示主窗口。 + ✅ Ubuntu 24.04 GNOME(X11)实机验收:图标、菜单、开关同步、退出、从应用菜单重新启动。⚠️ Xfce 等系统托盘路径尚未实机验收。 + Tray icon with open / allow / quit; Quit stops the user service and the launcher restarts it. Verified on Ubuntu GNOME. + +### 改进 / Improved +- `uuremote-server show`:主机没在运行时,优先启动已安装的 `uuremote-native.service`,而不是另起一个不受管理的进程。 +- 修复启动时的一个时序问题:先注册窗口唤起信号再写入 PID 文件,避免过早的 `show` 意外结束主机进程。 + +### 下载 / Download +`uu-remote-linux-v0.1.13-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。 + +**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.** diff --git a/src/assist.rs b/src/assist.rs index 3e6685e..1ba1ce3 100644 --- a/src/assist.rs +++ b/src/assist.rs @@ -68,7 +68,7 @@ fn valid_custom(code: &str) -> bool { && code.bytes().any(|b| b.is_ascii_alphabetic()) && code.bytes().any(|b| b.is_ascii_digit()) } -pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout } +pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout, Quit } #[cfg(uu_gtk)] extern "C" { @@ -156,6 +156,7 @@ impl LocalUi { 6 => if let Some(r) = Refresh::from_index(index) { return Command::Refresh(r) }, 7 => return Command::Custom(text), 8 => return Command::Logout, + 9 => return Command::Quit, _ => tokio::time::sleep(std::time::Duration::from_millis(150)).await, } }, diff --git a/src/main.rs b/src/main.rs index 662a211..6e36ede 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,14 +63,19 @@ fn restart_host() -> ! { eprintln!("restart failed: {error}"); std::process::exit(1); } +/// systemd user unit running this process (from /proc/self/cgroup), e.g. "uuremote-native.service". +fn systemd_unit() -> Option { + std::env::var_os("INVOCATION_ID")?; + let cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?; + cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned)) +} fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() } /// SIGUSR1 (`uuremote-server show`) opens the host window. fn present_on_usr1(ui: std::sync::Arc) { + // Install the handler before publishing the pid: SIGUSR1's default action terminates the process. + let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return }; write_host_pid(); - tokio::spawn(async move { - let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return }; - while usr1.recv().await.is_some() { ui.present(); } - }); + tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } }); } fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") } @@ -94,6 +99,24 @@ fn show_running_host() -> Result<(), Box> { if let Some(pid) = running { if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); } } + // Stopped from the tray but installed as a user service: start the service (not a second, + // unmanaged host), wait for it to publish its pid, then ask it to show the window. + let unit = std::env::var("UU_SERVICE_UNIT").unwrap_or_else(|_| "uuremote-native.service".into()); + let installed = std::process::Command::new("systemctl").args(["--user", "cat", &unit]) + .stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok_and(|s| s.success()); + if installed && std::process::Command::new("systemctl").args(["--user", "start", &unit]).status().is_ok_and(|s| s.success()) { + let before = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok(); + for _ in 0..60 { + std::thread::sleep(std::time::Duration::from_millis(500)); + let fresh = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok().is_some_and(|t| Some(t) != before); + let pid = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::().ok()); + if let (true, Some(pid)) = (fresh, pid) { + std::thread::sleep(std::time::Duration::from_millis(500)); // signal handler installed right after the pid + if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); } + } + } + return Err("the UU remote host service started but did not open its window".into()); + } use std::os::unix::process::CommandExt; Err(Box::new(std::process::Command::new("/proc/self/exe").env("UU_SHOW_WINDOW", "1").exec())) } @@ -524,6 +547,14 @@ async fn serve(mut capture: Arc, input: Arc, shutd SignalingEvent::AssistCommand(assist::Command::Verify(v)) => { if let Some(s) = assist.as_mut() { s.set_verify(v)?; } } SignalingEvent::AssistCommand(assist::Command::Refresh(r)) => { if let Some(s) = assist.as_mut() { s.set_refresh(r)?; } } SignalingEvent::AssistCommand(assist::Command::Custom(c)) => { if let Some(s) = assist.as_mut() { s.set_custom(c)?; } } + SignalingEvent::AssistCommand(assist::Command::Quit) => { + // Tray 退出: under systemd (Restart=always) stop the unit itself, else just exit. + log::info!("Quit requested from the tray"); + if let Some(unit) = systemd_unit() { + let _ = std::process::Command::new("systemctl").args(["--user", "stop", "--no-block", &unit]).status(); + } + return Ok(()); + } SignalingEvent::AssistCommand(assist::Command::Logout) => { if let Some(s) = assist.as_mut().filter(|s| s.is_account()) { s.logout().await;