From 191ba267729d626c5fc3829ffd1c5708d0ec99f6 Mon Sep 17 00:00:00 2001 From: kunkundi Date: Fri, 25 Sep 2026 00:29:24 +0800 Subject: [PATCH] [feat] add Linux headless mode and interactive console --- .github/workflows/build.yml | 5 + README.md | 8 +- README_EN.md | 8 +- apps/desktop/scripts/linux/package_deb.sh | 7 +- apps/desktop/src/app/main.cpp | 75 +- .../src/config_center/config_center.cpp | 215 +++--- .../desktop/src/config_center/config_center.h | 6 + .../src/gui/application/gui_application.cpp | 94 ++- .../src/gui/application/gui_application.h | 1 + .../assets/localization/localization_data.h | 59 ++ apps/desktop/src/gui/runtime/gui_runtime.cpp | 8 + .../src/gui/runtime/peer_event_handler.cpp | 6 + apps/desktop/src/platform/linux/autostart.cpp | 2 + .../headless/desktop_session_selection.h | 85 +++ .../linux/headless/headless_console.cpp | 357 ++++++++++ .../linux/headless/headless_console.h | 78 +++ .../linux/headless/headless_session.cpp | 657 ++++++++++++++++++ .../linux/headless/headless_session.h | 23 + .../linux/headless/headless_settings.cpp | 153 ++++ .../linux/headless/headless_settings.h | 36 + .../screen_capturer/screen_capturer_x11.cpp | 101 +-- apps/desktop/xmake/targets.lua | 15 +- docker/linux-build/Dockerfile | 2 + docs/BUILD.md | 4 +- docs/BUILD_EN.md | 4 +- docs/FAQ.md | 5 + docs/HEADLESS.md | 155 +++++ docs/HEADLESS_EN.md | 152 ++++ 28 files changed, 2120 insertions(+), 201 deletions(-) create mode 100644 apps/desktop/src/platform/linux/headless/desktop_session_selection.h create mode 100644 apps/desktop/src/platform/linux/headless/headless_console.cpp create mode 100644 apps/desktop/src/platform/linux/headless/headless_console.h create mode 100644 apps/desktop/src/platform/linux/headless/headless_session.cpp create mode 100644 apps/desktop/src/platform/linux/headless/headless_session.h create mode 100644 apps/desktop/src/platform/linux/headless/headless_settings.cpp create mode 100644 apps/desktop/src/platform/linux/headless/headless_settings.h create mode 100644 docs/HEADLESS.md create mode 100644 docs/HEADLESS_EN.md diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5f1e7c43..c7f78163 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -149,6 +149,11 @@ jobs: xmake run --root crossdesk_wire_test xmake run --root keyboard_state_wire_test + - name: Install Linux headless runtime dependencies + run: | + apt-get update + apt-get install -y --no-install-recommends xvfb xfce4 dbus + - name: Package run: | chmod +x ${{ matrix.package_script }} diff --git a/README.md b/README.md index fcecdc88..343be53e 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,9 @@ CrossDesk 支持 Windows、macOS、Linux 之间的远程访问,也可从浏览 | 实时画面与声音 | H.264 / AV1、30 / 60 fps 设置、硬件编解码选项、远端声音播放;实际能力取决于设备与构建配置 | | 多设备与多显示器 | 近期连接、设备别名、会话标签与远端显示器切换 | | 输入与协作 | 键鼠输入、远端光标同步、组合键、文本剪贴板同步、文件传输 | -| 网络与部署 | P2P 直连、TURN 中继、SRTP 加密选项、自托管信令与中继服务 | +| 网络与部署 | P2P 直连、TURN 中继、SRTP 媒体加密、自托管信令与中继服务 | | Windows 锁屏控制 | 通过 CrossDesk Service 支持锁屏、登录界面及安全桌面的输入转发 | +| Linux 无头运行 | 复用已有桌面和应用,或创建独立 Xvfb 虚拟桌面;支持画面采集与键鼠控制,见 [运行说明](docs/HEADLESS.md) | 各客户端的入口和能力有所不同;桌面会话操作见下文,iOS 的具体功能见 [开发说明](apps/ios/README.md)。 @@ -93,7 +94,7 @@ sudo apt install "./crossdesk-linux-amd64-.deb" ### 从浏览器连接 -1. 保持被控电脑上的 CrossDesk 运行,并确认已连接服务器;在被控端“设置”中保持 **启用 SRTP** 开启。 +1. 保持被控电脑上的 CrossDesk 运行,并确认已连接服务器。 2. 打开 [Web 客户端](https://web.crossdesk.cn/),输入 **远程设备 ID** 和 **密码**,点击“连接”。 3. 连接后使用页面提供的显示器、鼠标模式和键盘控件操作远端;手机和平板也可通过浏览器访问。 @@ -125,7 +126,7 @@ sudo apt install "./crossdesk-linux-amd64-.deb" 点击主窗口右上角 **☰ → 设置**,修改后点击“确认”保存。建议在建立连接前调整;会话进行中部分选项不可修改。设置列表可向下滚动,查看自托管、启动方式与文件保存路径。

- 当前设置界面:画质、帧率、画面偏好、编解码、中继与 SRTP + 设置界面示例

| 设置 | 说明 | @@ -134,7 +135,6 @@ sudo apt install "./crossdesk-linux-amd64-.deb" | 画面偏好 | 在帧率优先、画质优先、平衡之间选择 | | 视频编码格式 | H.264 / AV1;硬件编解码是否可用取决于平台、硬件与构建选项 | | 启用中继服务 | 允许借助 TURN 中继建立连接;P2P 连接失败时先检查此项 | -| 启用 SRTP | 媒体传输加密选项,两端配置需兼容 | | 自托管配置 | 设置服务器地址、信令端口与中继端口,并勾选启用 | | 开机自启 / 启用守护进程 | 调整启动与进程守护行为,按界面提示重启后生效 | | 文件保存路径 | 选择接收文件的目录 | diff --git a/README_EN.md b/README_EN.md index fbf2bcf4..5eca2a56 100644 --- a/README_EN.md +++ b/README_EN.md @@ -51,8 +51,9 @@ See the [screenshot notes](docs/images/README.md) for image sources and refresh | Live video and audio | H.264 / AV1, 30 / 60 fps settings, hardware codec options, and remote audio; availability depends on hardware and build configuration | | Devices and displays | Recent connections, device aliases, session tabs, and remote display switching | | Input and sharing | Keyboard/mouse input, remote cursor synchronization, shortcuts, text clipboard synchronization, and file transfer | -| Networking and deployment | Direct P2P connections, TURN relay, an SRTP encryption option, and self-hosted signaling/relay services | +| Networking and deployment | Direct P2P connections, TURN relay, SRTP media encryption, and self-hosted signaling/relay services | | Windows protected desktops | CrossDesk Service forwards input on lock screens, sign-in screens, and secure desktops | +| Linux headless operation | Reuse existing desktops and applications or create separate Xvfb desktops, with capture and input; see [setup](docs/HEADLESS_EN.md) | Controls and capabilities vary by client. Desktop controls are described below; see the [iOS guide](apps/ios/README.md) for native mobile features. @@ -93,7 +94,7 @@ The eye button shows or hides the local password; the pencil button changes it. ### Connect from a browser -1. Keep CrossDesk running and connected to the server on the host computer, with **Enable SRTP** enabled in its Settings. +1. Keep CrossDesk running and connected to the server on the host computer. 2. Open the [Web Client](https://web.crossdesk.cn/), enter the **Remote Device ID** and **Password**, and click **Connect**. 3. Use the page's display, mouse-mode, and keyboard controls during the session. Phone and tablet browsers can also connect. @@ -125,7 +126,7 @@ Desktop clients synchronize text clipboard contents. Configure the receiving dir Open **☰ → Settings** in the top-right corner and click **OK** to save. Configure the session before connecting; some settings are disabled during an active session. Scroll down for self-hosting, startup behavior, and the file save path.

- Current settings for quality, frame rate, video preference, codecs, relay, and SRTP + Example settings interface

| Setting | Purpose | @@ -134,7 +135,6 @@ Open **☰ → Settings** in the top-right corner and click **OK** to save. Conf | Video Preference | Choose frame-rate priority, quality priority, or balanced adaptation | | Video Encode Format | H.264 / AV1; hardware codec availability depends on the platform, device, and build options | | Enable TURN Service | Allow relay-assisted connections; check this setting if P2P fails | -| SRTP | Media encryption option; configurations on both ends must be compatible | | Self-Hosted Config | Set the host, signaling port, and TURN port, then enable the checkbox | | Auto Start / Enable Daemon | Configure startup and process supervision; restart as indicated by the UI | | File Save Path | Choose where received files are saved | diff --git a/apps/desktop/scripts/linux/package_deb.sh b/apps/desktop/scripts/linux/package_deb.sh index fa320f65..3e52dd0d 100755 --- a/apps/desktop/scripts/linux/package_deb.sh +++ b/apps/desktop/scripts/linux/package_deb.sh @@ -21,6 +21,10 @@ DESCRIPTION="A simple cross-platform remote desktop client." ALSA_RUNTIME_DEP="libasound2 | libasound2t64" PORTAL_RUNTIME_RECOMMENDS="xdg-desktop-portal, xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr" TRAY_RUNTIME_RECOMMENDS="libayatana-appindicator3-1 | libappindicator3-1" +# Every published package includes the prerequisites for all Linux headless +# paths, including a complete independent desktop. Older Ubuntu releases ship +# dbus-run-session in dbus; newer releases split it into dbus-daemon. +HEADLESS_RUNTIME_DEPS="xvfb, xfce4, dbus-daemon | dbus" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)" @@ -167,7 +171,8 @@ Depends: libc6 (>= 2.31), libstdc++6 (>= 10), libx11-6, libxext6, libxrender1, libxft2, libxrandr2, libxfixes3, libxcursor1, libxi6, libxcb1, libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0, libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, $ALSA_RUNTIME_DEP, libsndio7.0, - libxcb-shm0, libpulse0, libdrm2, libdbus-1-3, libgl1 + libxcb-shm0, libpulse0, libdrm2, libdbus-1-3, libgl1, + $HEADLESS_RUNTIME_DEPS Recommends: $RECOMMENDS Priority: optional Section: utils diff --git a/apps/desktop/src/app/main.cpp b/apps/desktop/src/app/main.cpp index bf489082..64cd97c7 100644 --- a/apps/desktop/src/app/main.cpp +++ b/apps/desktop/src/app/main.cpp @@ -6,6 +6,7 @@ #endif #endif +#include #include #include #include @@ -26,6 +27,8 @@ #include "render.h" #ifdef __linux__ +#include "platform/linux/headless/headless_console.h" +#include "platform/linux/headless/headless_session.h" #include "platform/linux/privacy/privacy_guard.h" #endif @@ -179,22 +182,7 @@ int HandleServiceCliCommand(const std::string& command) { } // namespace #endif -int main(int argc, char* argv[]) { -#ifdef __linux__ - // Run before GUI, configuration, logging or daemon initialization. The - // guard owns native privacy resources and restores them if this app exits. - if (argc == 2 && - std::strcmp(argv[1], crossdesk::kLinuxPrivacyGuardArgument) == 0) { - return crossdesk::RunLinuxPrivacyGuard(); - } -#endif -#ifdef _WIN32 - if (argc == 2 && - std::strcmp(argv[1], crossdesk::kSlintRendererProbeArgument) == 0) { - return crossdesk::RunSlintRendererProbe(); - } -#endif - +static int RunApplication(int argc, char* argv[]) { // Service operations and config migration can log before the GUI starts. // Configure the directory first, including CLI and daemon child paths. auto path_manager = std::make_unique("CrossDesk"); @@ -218,8 +206,7 @@ int main(int argc, char* argv[]) { if (is_child) { // child process: run render directly crossdesk::Render render; - render.Run(); - return 0; + return render.Run(); } #ifdef _WIN32 @@ -233,6 +220,14 @@ int main(int argc, char* argv[]) { enable_daemon = config_center.IsEnableDaemon(); } +#ifdef __linux__ + // The headless supervisor owns the display and application lifetime. A + // detached daemon would outlive its display; systemd can restart the whole + // session instead. + if (std::getenv("CROSSDESK_HEADLESS_ACTIVE") || + crossdesk::HeadlessConsole::Instance().active()) enable_daemon = false; +#endif + if (enable_daemon) { // start daemon with restart monitoring Daemon daemon("CrossDesk"); @@ -251,6 +246,46 @@ int main(int argc, char* argv[]) { // run without daemon: direct execution crossdesk::Render render; - render.Run(); - return 0; + return render.Run(); +} + +int main(int argc, char* argv[]) { +#ifdef __linux__ + // Run before GUI, configuration, logging or display initialization. + if (argc == 2 && + std::strcmp(argv[1], crossdesk::kLinuxPrivacyGuardArgument) == 0) { + return crossdesk::RunLinuxPrivacyGuard(); + } + auto& console = crossdesk::HeadlessConsole::Instance(); + if (argc == 2 && std::strcmp(argv[1], "--headless-help") == 0) { + crossdesk::PathManager paths("CrossDesk"); + CSimpleIniA config; + config.SetUnicode(true); + config.LoadFile((paths.GetCachePath() / "config.ini").string().c_str()); + console.SetLanguage(static_cast(config.GetLongValue("Settings", "language", 0))); + std::cout << console.Text("console_cli_help"); + return 0; + } + const int result = crossdesk::RunWithLinuxDisplay( + argc, argv, [&] { return RunApplication(argc, argv); }, [&] { + crossdesk::PathManager paths("CrossDesk"); + if (console.Enable(paths.GetLogPath(), paths.GetCachePath() / "config.ini")) return true; + std::cerr << "无法初始化日志或控制台,启动失败。\n"; + return false; + }); + if (console.active()) { + console.Notify(result == 0 ? console.Text("console_exited") + : console.Text("console_stopped") + " " + std::to_string(result) + + "; " + console.Text("console_log_file") + ": " + console.diagnostic_path()); + } + return result; +#else +#ifdef _WIN32 + if (argc == 2 && + std::strcmp(argv[1], crossdesk::kSlintRendererProbeArgument) == 0) { + return crossdesk::RunSlintRendererProbe(); + } +#endif + return RunApplication(argc, argv); +#endif } diff --git a/apps/desktop/src/config_center/config_center.cpp b/apps/desktop/src/config_center/config_center.cpp index 4361a566..1015e6e9 100644 --- a/apps/desktop/src/config_center/config_center.cpp +++ b/apps/desktop/src/config_center/config_center.cpp @@ -1,5 +1,11 @@ #include "config_center.h" +#include +#include +#include +#include +#include + #include "autostart.h" #include "rd_log.h" @@ -128,13 +134,58 @@ int ConfigCenter::Load() { file_transfer_save_path_ = ""; } - if (persist_config_migration && ini_.SaveFile(config_path_.c_str()) < 0) { + if (persist_config_migration && CommitIni() < 0) { return -1; } return 0; } +int ConfigCenter::CommitIni() { + // Write beside the destination and rename only after a complete save. A + // failed write must not truncate the last usable configuration. + static std::atomic sequence{0}; + const std::string temporary = config_path_ + ".tmp-" + + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + + "-" + std::to_string(sequence++); + std::error_code error; + if (ini_.SaveFile(temporary.c_str()) < 0) { + LOG_ERROR("Failed to write configuration: {}", config_path_); + std::filesystem::remove(temporary, error); + return -1; + } + const auto previous = std::filesystem::status(config_path_, error); + if (!error && std::filesystem::is_regular_file(previous)) { + std::filesystem::permissions(temporary, previous.permissions(), error); + if (error) { + LOG_ERROR("Failed to preserve configuration permissions: {}", error.message()); + std::filesystem::remove(temporary, error); + return -1; + } + } + std::filesystem::rename(temporary, config_path_, error); + if (!error) return 0; + LOG_ERROR("Failed to replace configuration {}: {}", config_path_, error.message()); + std::filesystem::remove(temporary, error); + return -1; +} + +int ConfigCenter::StoreValues( + std::initializer_list> values) { + std::vector>> previous; + for (const auto& value : values) { + const char* old = ini_.GetValue(section_, value.first, nullptr); + previous.emplace_back(value.first, old ? std::optional(old) : std::nullopt); + ini_.SetValue(section_, value.first, value.second.c_str()); + } + if (CommitIni() == 0) return 0; + for (const auto& value : previous) { + if (value.second) ini_.SetValue(section_, value.first, value.second->c_str()); + else ini_.Delete(section_, value.first); + } + return -1; +} + int ConfigCenter::Save() { ini_.SetLongValue(section_, "language", static_cast(language_)); ini_.SetLongValue(section_, "screen_capture_method", @@ -165,7 +216,7 @@ int ConfigCenter::Save() { ini_.SetValue(section_, "file_transfer_save_path", file_transfer_save_path_.c_str()); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); + SI_Error rc = CommitIni(); if (rc < 0) { return -1; } @@ -176,12 +227,9 @@ int ConfigCenter::Save() { // setters int ConfigCenter::SetLanguage(LANGUAGE language) { + const int value = static_cast(language); + if (value < 0 || value > 2 || StoreValues({{"language", std::to_string(value)}}) != 0) return -1; language_ = language; - ini_.SetLongValue(section_, "language", static_cast(language_)); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } return 0; } @@ -190,7 +238,7 @@ int ConfigCenter::SetScreenCaptureMethod(ScreenCaptureMethod method) { return -1; } ini_.SetLongValue(section_, "screen_capture_method", static_cast(method)); - if (ini_.SaveFile(config_path_.c_str()) < 0) { + if (CommitIni() < 0) { ini_.SetLongValue(section_, "screen_capture_method", static_cast(screen_capture_method_)); return -1; @@ -199,41 +247,25 @@ int ConfigCenter::SetScreenCaptureMethod(ScreenCaptureMethod method) { return 0; } -int ConfigCenter::SetVideoEncodeFormat( - VIDEO_ENCODE_FORMAT video_encode_format) { - video_encode_format_ = video_encode_format; - ini_.SetLongValue(section_, "video_encode_format", - static_cast(video_encode_format_)); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } +int ConfigCenter::SetVideoEncodeFormat(VIDEO_ENCODE_FORMAT format) { + if (format != VIDEO_ENCODE_FORMAT::H264 && format != VIDEO_ENCODE_FORMAT::AV1) return -1; + if (StoreValues({{"video_encode_format", std::to_string(static_cast(format))}}) != 0) return -1; + video_encode_format_ = format; return 0; } -int ConfigCenter::SetHardwareVideoCodec(bool hardware_video_codec) { - hardware_video_codec_ = hardware_video_codec; - ini_.SetBoolValue(section_, "hardware_video_codec", hardware_video_codec_); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } +int ConfigCenter::SetHardwareVideoCodec(bool enabled) { + if (enabled && !IsHardwareVideoCodecAvailable()) return -1; + if (StoreValues({{"hardware_video_codec", enabled ? "true" : "false"}}) != 0) return -1; + hardware_video_codec_ = enabled; return 0; } -int ConfigCenter::SetTurnMode(TURN_MODE turn_mode) { - if (!IsValidTurnModeValue(static_cast(turn_mode))) { - return -1; - } - - turn_mode_ = turn_mode; - ini_.SetLongValue(section_, "turn_mode", static_cast(turn_mode_)); - ini_.SetBoolValue(section_, "enable_turn", - turn_mode_ != TURN_MODE::DISABLED); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } +int ConfigCenter::SetTurnMode(TURN_MODE mode) { + if (!IsValidTurnModeValue(static_cast(mode))) return -1; + if (StoreValues({{"turn_mode", std::to_string(static_cast(mode))}, + {"enable_turn", mode == TURN_MODE::DISABLED ? "false" : "true"}}) != 0) return -1; + turn_mode_ = mode; return 0; } @@ -249,90 +281,47 @@ int ConfigCenter::SetTurn(bool enable_turn, bool force_relay) { : TURN_MODE::FORCE_UDP); } -int ConfigCenter::SetServerHost(const std::string& signal_server_host) { - signal_server_host_ = signal_server_host; - ini_.SetValue(section_, "signal_server_host", signal_server_host_.c_str()); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } +int ConfigCenter::SetServerHost(const std::string& host) { + if (host.empty() || host.size() > 253 || host.find_first_of("/\\@?#") != std::string::npos || + std::any_of(host.begin(), host.end(), [](unsigned char c) { return c <= 32 || c == 127; })) return -1; + if (StoreValues({{"signal_server_host", host}}) != 0) return -1; + signal_server_host_ = host; return 0; } -int ConfigCenter::SetServerPort(int signal_server_port) { - signal_server_port_ = signal_server_port; - ini_.SetLongValue(section_, "signal_server_port", - static_cast(signal_server_port_)); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } +int ConfigCenter::SetServerPort(int port) { + if (port < 1 || port > 65535 || StoreValues({{"signal_server_port", std::to_string(port)}}) != 0) return -1; + signal_server_port_ = port; return 0; } -int ConfigCenter::SetSelfHosted(bool enable_self_hosted) { - enable_self_hosted_ = enable_self_hosted; - ini_.SetBoolValue(section_, "enable_self_hosted", enable_self_hosted_); - - // load from config if self hosted is enabled - if (enable_self_hosted_) { - const char* signal_server_host_value = - ini_.GetValue(section_, "signal_server_host", nullptr); - if (signal_server_host_value != nullptr && - strlen(signal_server_host_value) > 0) { - signal_server_host_ = signal_server_host_value; - } - const char* signal_server_port_value = - ini_.GetValue(section_, "signal_server_port", nullptr); - if (signal_server_port_value != nullptr && - strlen(signal_server_port_value) > 0) { - signal_server_port_ = static_cast( - ini_.GetLongValue(section_, "signal_server_port", 0)); - } - ini_.SetValue(section_, "signal_server_host", signal_server_host_.c_str()); - ini_.SetLongValue(section_, "signal_server_port", - static_cast(signal_server_port_)); - } - - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } +int ConfigCenter::SetSelfHosted(bool enabled) { + if (enabled && (signal_server_host_.empty() || signal_server_port_ < 1 || signal_server_port_ > 65535)) return -1; + if (StoreValues({{"enable_self_hosted", enabled ? "true" : "false"}}) != 0) return -1; + enable_self_hosted_ = enabled; return 0; } -int ConfigCenter::SetAutostart(bool enable_autostart) { - enable_autostart_ = enable_autostart; - bool success = false; - if (enable_autostart) { - success = EnableAutostart("CrossDesk"); - } else { - success = DisableAutostart("CrossDesk"); - } - - ini_.SetBoolValue(section_, "enable_autostart", enable_autostart_); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } - - if (!success) { +int ConfigCenter::SetAutostart(bool enabled) { + const bool previous = IsAutostartEnabled("CrossDesk"); + if (enabled != previous && !(enabled ? EnableAutostart("CrossDesk") : DisableAutostart("CrossDesk"))) { LOG_ERROR("SetAutostart failed"); return -1; } - + if (StoreValues({{"enable_autostart", enabled ? "true" : "false"}}) != 0) { + if (enabled != previous) { + if (previous) EnableAutostart("CrossDesk"); + else DisableAutostart("CrossDesk"); + } + return -1; + } + enable_autostart_ = enabled; return 0; } -int ConfigCenter::SetDaemon(bool enable_daemon) { - enable_daemon_ = enable_daemon; - - ini_.SetBoolValue(section_, "enable_daemon", enable_daemon_); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } - +int ConfigCenter::SetDaemon(bool enabled) { + if (StoreValues({{"enable_daemon", enabled ? "true" : "false"}}) != 0) return -1; + enable_daemon_ = enabled; return 0; } @@ -340,7 +329,7 @@ int ConfigCenter::SetPrivacyScreen(bool enable_privacy_screen) { const bool previous = enable_privacy_screen_.load(); if (previous == enable_privacy_screen) return 0; ini_.SetBoolValue(section_, "enable_privacy_screen", enable_privacy_screen); - if (ini_.SaveFile(config_path_.c_str()) < 0) { + if (CommitIni() < 0) { ini_.SetBoolValue(section_, "enable_privacy_screen", previous); LOG_ERROR("Failed to save automatic privacy screen preference"); return -1; @@ -355,7 +344,7 @@ int ConfigCenter::SetPortableServicePromptSuppressed(bool suppressed) { portable_service_prompt_suppressed_ = suppressed; ini_.SetBoolValue(section_, "portable_service_prompt_suppressed", portable_service_prompt_suppressed_); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); + SI_Error rc = CommitIni(); if (rc < 0) { return -1; } @@ -432,17 +421,15 @@ bool ConfigCenter::IsPortableServicePromptSuppressed() const { } int ConfigCenter::SetFileTransferSavePath(const std::string& path) { + if (path.size() >= 512 || std::any_of(path.begin(), path.end(), [](unsigned char c) { return c < 32 || c == 127; })) return -1; + std::lock_guard lock(file_path_mutex_); + if (StoreValues({{"file_transfer_save_path", path}}) != 0) return -1; file_transfer_save_path_ = path; - ini_.SetValue(section_, "file_transfer_save_path", - file_transfer_save_path_.c_str()); - SI_Error rc = ini_.SaveFile(config_path_.c_str()); - if (rc < 0) { - return -1; - } return 0; } std::string ConfigCenter::GetFileTransferSavePath() const { + std::lock_guard lock(file_path_mutex_); return file_transfer_save_path_; } } // namespace crossdesk diff --git a/apps/desktop/src/config_center/config_center.h b/apps/desktop/src/config_center/config_center.h index 4350c86d..d8da2714 100644 --- a/apps/desktop/src/config_center/config_center.h +++ b/apps/desktop/src/config_center/config_center.h @@ -8,6 +8,9 @@ #define _CONFIG_CENTER_H_ #include +#include +#include +#include #include #include "SimpleIni.h" @@ -73,6 +76,9 @@ class ConfigCenter { int Save(); private: + int CommitIni(); + int StoreValues(std::initializer_list> values); + mutable std::mutex file_path_mutex_; std::string config_path_; CSimpleIniA ini_; const char* section_ = "Settings"; diff --git a/apps/desktop/src/gui/application/gui_application.cpp b/apps/desktop/src/gui/application/gui_application.cpp index 615b069c..34dd264e 100644 --- a/apps/desktop/src/gui/application/gui_application.cpp +++ b/apps/desktop/src/gui/application/gui_application.cpp @@ -54,6 +54,9 @@ #include "platform/linux/gui/cuda_graphics.h" #include "platform/linux/gui/tray/linux_tray.h" +#include "platform/linux/headless/headless_console.h" +// Xlib defines Status as a macro, which collides with UpdateChecker::Status. +#undef Status #endif #include "rd_log.h" #include "server_window_state.h" @@ -990,6 +993,15 @@ int GuiApplication::Run() { } InitializeSettings(); +#ifdef __linux__ + HeadlessConsole::Instance().BindSettings( + config_center_.get(), [this] { + std::lock_guard lock(password_change_mutex_); + return HasActiveSession() || password_change_pending_ || credential_recovery_in_progress_; + }, [this](SettingEffect effect) { + ApplySettingsFromConfig(effect == SettingEffect::reconnect); + }); +#endif if (!InitializeSDL()) { return -1; } @@ -1760,6 +1772,17 @@ void GuiApplication::Tick() { if (!peer_) { CreateConnectionPeer(); } +#ifdef __linux__ + HeadlessConsole::Instance().Poll( + signal_connected_.load(), + [this](const std::string& password) { RequestPasswordChange(password); }, + [this] { + const auto password = GenerateRandomPassword(password_saved_); + if (password) RequestPasswordChange(*password); + else HeadlessConsole::Instance().NotifyKey("console_random_failed"); + }, + [this] { exit_ = true; }); +#endif SDL_Event event; #if defined(__APPLE__) @@ -2019,7 +2042,17 @@ void GuiApplication::ShareLocalCursorState() { } bool GuiApplication::RequestPasswordChange(const std::string &password) { - if (password.size() != 6) { + const auto notify = [](const std::string& message) { +#ifdef __linux__ + HeadlessConsole::Instance().NotifyKey(message.c_str()); +#else + (void)message; +#endif + }; + if (password.size() != 6 || + password.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") != + std::string::npos) { + notify("console_password_invalid"); return false; } @@ -2027,6 +2060,7 @@ bool GuiApplication::RequestPasswordChange(const std::string &password) { offline_warning_text_ = localization::signal_disconnected[localization_language_index_]; show_offline_warning_window_ = true; + notify("console_password_offline"); return true; } @@ -2034,6 +2068,7 @@ bool GuiApplication::RequestPasswordChange(const std::string &password) { { std::lock_guard lock(password_change_mutex_); if (password_change_pending_) { + notify("console_password_pending"); return true; } request_id = @@ -2066,6 +2101,7 @@ bool GuiApplication::RequestPasswordChange(const std::string &password) { offline_warning_text_ = localization::failed[localization_language_index_]; show_offline_warning_window_ = true; LOG_ERROR("Could not durably stage password change"); + notify("console_password_stage_failed"); return true; } @@ -2086,6 +2122,9 @@ bool GuiApplication::RequestPasswordChange(const std::string &password) { if (!settings_.ClearPendingPasswordChange()) { LOG_WARN("Could not clear unsent pending password change"); } + notify("console_password_send_failed"); + } else { + notify("console_password_submitted"); } return true; } @@ -2123,6 +2162,10 @@ void GuiApplication::HandlePasswordChangeResult() { } if (!succeeded) { +#ifdef __linux__ + HeadlessConsole::Instance().NotifyKey( + uncertain ? "console_password_uncertain" : "console_password_rejected"); +#endif if (uncertain) { LOG_WARN("Password change outcome is unknown: {}; re-authenticating", error); @@ -2150,6 +2193,9 @@ void GuiApplication::HandlePasswordChangeResult() { } LOG_INFO("Password changed successfully for [{}]", client_id_); +#ifdef __linux__ + HeadlessConsole::Instance().NotifyKey("console_password_confirmed"); +#endif if (peer_) { CloseConnectionPeer(); } @@ -2181,12 +2227,18 @@ void GuiApplication::HandleCredentialRecovery() { } if (promote_pending) { +#ifdef __linux__ + HeadlessConsole::Instance().NotifyKey("console_password_recovered"); +#endif if (!settings_.PromotePendingPasswordChange()) { LOG_WARN("Recovered credential is active but promotion remains pending"); } else { LOG_INFO("Recovered password change with pending credential"); } } else if (clear_pending) { +#ifdef __linux__ + HeadlessConsole::Instance().NotifyKey("console_password_retained"); +#endif if (!settings_.ClearPendingPasswordChange()) { LOG_WARN("Active credential recovered but pending record could not be " "cleared"); @@ -3142,7 +3194,6 @@ void GuiApplication::SaveSettingsFromUi() { config_center_->SetHardwareVideoCodec(enable_hardware_video_codec_); config_center_->SetTurn(enable_turn_, main->get_force_relay_enabled()); main->set_force_relay_enabled(config_center_->IsForceRelay()); - config_center_->SetSelfHosted(enable_self_hosted_); config_center_->SetAutostart(enable_autostart_); config_center_->SetDaemon(enable_daemon_); #ifdef _WIN32 @@ -3179,6 +3230,28 @@ void GuiApplication::SaveSettingsFromUi() { config_center_->SetServerPort(*port); } + config_center_->SetSelfHosted(enable_self_hosted_); + ApplySettingsFromConfig(!HasActiveSession()); +} + +void GuiApplication::ApplySettingsFromConfig(bool reconnect) { + language_button_value_ = static_cast(config_center_->GetLanguage()); + localization_language_index_ = localization::detail::ClampLanguageIndex(language_button_value_); + localization_language_ = config_center_->GetLanguage(); + video_encode_format_button_value_ = static_cast(config_center_->GetVideoEncodeFormat()); + enable_hardware_video_codec_ = config_center_->IsHardwareVideoCodec(); + enable_turn_ = config_center_->IsEnableTurn(); + enable_self_hosted_ = config_center_->IsSelfHosted(); + enable_autostart_ = config_center_->IsEnableAutostart(); + enable_daemon_ = config_center_->IsEnableDaemon(); + privacy_.SetText({localization::privacy_screen_unlock_hint[localization_language_index_]}); + const auto path = config_center_->GetFileTransferSavePath(); + std::snprintf(file_transfer_save_path_buf_, sizeof(file_transfer_save_path_buf_), "%s", path.c_str()); + const auto host = config_center_->GetSignalServerHost(); + std::snprintf(signal_server_ip_self_, sizeof(signal_server_ip_self_), "%s", host.c_str()); + const int port = config_center_->GetSignalServerPort(); + const auto port_text = port > 0 ? std::to_string(port) : std::string{}; + std::snprintf(signal_server_port_self_, sizeof(signal_server_port_self_), "%s", port_text.c_str()); language_button_value_last_ = language_button_value_; video_encode_format_button_value_last_ = video_encode_format_button_value_; enable_hardware_video_codec_last_ = enable_hardware_video_codec_; @@ -3187,11 +3260,17 @@ void GuiApplication::SaveSettingsFromUi() { enable_autostart_last_ = enable_autostart_; enable_daemon_last_ = enable_daemon_; file_transfer_save_path_last_ = path; - ui_->localized_language = -1; - - if (!HasActiveSession()) { +#ifdef __linux__ + HeadlessConsole::Instance().SetLanguage(localization_language_index_); +#endif + if (ui_) { + ui_->localized_language = -1; + ResetSettingsUi(); + } + if (reconnect && !HasActiveSession()) { + // Tick recreates the peer after asynchronous cleanup, using the committed + // configuration. Keep the application and its console running throughout. CloseAllRemoteSessions(); - CreateConnectionPeer(); } } @@ -3465,6 +3544,9 @@ bool GuiApplication::OpenUrl(const std::string& url) { } void GuiApplication::Cleanup() { +#ifdef __linux__ + HeadlessConsole::Instance().BindSettings(nullptr, {}, {}); +#endif if (!ui_) { SDL_Quit(); return; diff --git a/apps/desktop/src/gui/application/gui_application.h b/apps/desktop/src/gui/application/gui_application.h index fe2f6027..bef7d322 100644 --- a/apps/desktop/src/gui/application/gui_application.h +++ b/apps/desktop/src/gui/application/gui_application.h @@ -71,6 +71,7 @@ private: void UpdateLocalization(); void ResetSettingsUi(); void SaveSettingsFromUi(); + void ApplySettingsFromConfig(bool reconnect); #if _WIN32 && CROSSDESK_PORTABLE void CheckPortableWindowsService(); void StartPortableWindowsServiceInstall(); diff --git a/apps/desktop/src/gui/assets/localization/localization_data.h b/apps/desktop/src/gui/assets/localization/localization_data.h index 307ed89d..9c5a946d 100644 --- a/apps/desktop/src/gui/assets/localization/localization_data.h +++ b/apps/desktop/src/gui/assets/localization/localization_data.h @@ -20,6 +20,65 @@ struct TranslationRow { // Single source of truth for all UI strings. #define CROSSDESK_LOCALIZATION_ALL(X) \ + X(console_cli_help, u8"Linux 无头运行选项:\n --headless 自动选择已有桌面并启用控制台\n --headless-display DISPLAY 高级:指定已有显示会话\n --headless-size WIDTHxHEIGHT 新建虚拟屏幕;默认1920x1080,偶数尺寸320..8192\n --headless-session EXECUTABLE 启动桌面环境(startxfce4;none 仅启动裸屏幕)\n --no-headless 禁用自动回退\n正常启动会自动选择已有桌面,无需填写显示编号。控制台输入 settings 管理设置。\n", "Linux headless options:\n --headless Select an existing desktop and enable the console\n --headless-display DISPLAY Advanced: override desktop selection\n --headless-size WIDTHxHEIGHT New virtual display; default 1920x1080, even 320..8192\n --headless-session EXECUTABLE Start a desktop (startxfce4; none for a bare display)\n --no-headless Disable automatic fallback\nNormal startup selects your existing desktop automatically. Enter settings in the console to configure it.\n", u8"Параметры запуска без монитора:\n --headless Выбрать рабочий стол и включить консоль\n --headless-display DISPLAY Дополнительно: указать сеанс дисплея\n --headless-size WIDTHxHEIGHT Новый виртуальный экран; 1920x1080 по умолчанию, чётные размеры 320..8192\n --headless-session EXECUTABLE Запустить рабочий стол (startxfce4; none — только экран)\n --no-headless Отключить автоматический резервный запуск\nРабочий стол выбирается автоматически. Команда settings открывает настройки консоли.\n") \ + X(console_title, u8"CrossDesk 无头控制台", "CrossDesk headless console", u8"Консоль CrossDesk") \ + X(console_log_file, u8"日志文件", "Log file", u8"Файл журнала") \ + X(console_help, u8"命令: status 查看ID/密码/状态;settings 设置;password 修改密码;random 随机密码;help 帮助;quit 退出", "Commands: status, settings, password, random, help, quit", u8"Команды: status, settings, password, random, help, quit") \ + X(console_status, u8"状态", "Status", u8"Состояние") \ + X(console_online, u8"在线", "Online", u8"В сети") \ + X(console_offline, u8"未连接服务器", "Disconnected from server", u8"Нет подключения к серверу") \ + X(console_id, u8"设备 ID", "Device ID", u8"ID устройства") \ + X(console_password, u8"连接密码", "Connection password", u8"Пароль подключения") \ + X(console_waiting, u8"等待分配", "Waiting for assignment", u8"Ожидание назначения") \ + X(console_password_hidden, u8"(仅在交互终端显示)", "(shown only in an interactive terminal)", u8"(виден только в интерактивном терминале)") \ + X(console_cancelled, u8"已取消修改。", "Change cancelled.", u8"Изменение отменено.") \ + X(console_password_invalid, u8"密码必须是6位英文字母或数字,未提交修改。", "Password must be 6 ASCII letters or digits; no change submitted.", u8"Пароль должен содержать 6 латинских букв или цифр. Изменение не отправлено.") \ + X(console_password_offline, u8"尚未连接服务器,暂时无法修改密码。", "Connect to the server before changing the password.", u8"Для смены пароля подключитесь к серверу.") \ + X(console_password_prompt, u8"输入新密码(6位英文字母或数字;输入 cancel 取消):", "New password (6 ASCII letters or digits; cancel to abort):", u8"Новый пароль (6 латинских букв или цифр; cancel — отмена):") \ + X(console_quitting, u8"正在退出 CrossDesk……", "Exiting CrossDesk...", u8"Выход из CrossDesk...") \ + X(console_unknown_command, u8"未知命令,输入 help 查看帮助。", "Unknown command. Enter help for commands.", u8"Неизвестная команда. Введите help для справки.") \ + X(console_line_too_long, u8"命令过长,已忽略。", "Command too long; ignored.", u8"Команда слишком длинная и проигнорирована.") \ + X(console_random_failed, u8"无法生成随机密码,请重试。", "Unable to generate a random password; try again.", u8"Не удалось создать случайный пароль. Повторите попытку.") \ + X(console_password_pending, u8"密码修改正在处理中,请等待服务器确认。", "Password change pending; wait for server confirmation.", u8"Смена пароля выполняется. Дождитесь подтверждения сервера.") \ + X(console_password_stage_failed, u8"无法保存密码修改记录,未提交修改。请检查日志。", "Unable to save password recovery record; no change submitted. Check the log.", u8"Не удалось сохранить запись восстановления пароля. Изменение не отправлено. Проверьте журнал.") \ + X(console_password_send_failed, u8"发送密码修改请求失败,请等待连接恢复后重试。", "Failed to submit password change; retry after reconnection.", u8"Не удалось отправить запрос смены пароля. Повторите после подключения.") \ + X(console_password_submitted, u8"已提交密码修改,正在等待服务器确认……", "Password change submitted; waiting for server confirmation...", u8"Запрос смены пароля отправлен. Ожидание подтверждения...") \ + X(console_password_uncertain, u8"密码修改结果尚未确认,正在重新连接服务器核实。", "Password change not yet confirmed; reconnecting to verify.", u8"Смена пароля не подтверждена. Повторное подключение для проверки.") \ + X(console_password_rejected, u8"服务器未接受密码修改,原密码保持不变。详情见日志。", "Server rejected the password change; the original password remains. See the log.", u8"Сервер отклонил смену пароля. Старый пароль сохранён. См. журнал.") \ + X(console_password_confirmed, u8"服务器已确认密码修改,正在重新连接。", "Server confirmed the password change; reconnecting.", u8"Сервер подтвердил смену пароля. Повторное подключение.") \ + X(console_password_recovered, u8"重新连接确认:新密码已生效。", "Reconnection confirmed: the new password is active.", u8"Повторное подключение подтвердило новый пароль.") \ + X(console_password_retained, u8"重新连接确认:继续使用原密码。", "Reconnection confirmed: the original password remains active.", u8"Повторное подключение подтвердило прежний пароль.") \ + X(console_exited, u8"CrossDesk 已退出。", "CrossDesk exited.", u8"CrossDesk завершён.") \ + X(console_stopped, u8"CrossDesk 已停止,退出码", "CrossDesk stopped; exit code", u8"CrossDesk остановлен; код выхода") \ + X(console_settings_title, u8"设置(修改后立即保存)", "Settings (each change is saved immediately)", u8"Настройки (изменения сохраняются сразу)") \ + X(console_settings_prompt, u8"输入编号或选项名称修改;back 返回。也可使用 settings set 名称 值。", "Enter an item number or key; back to return. Shortcut: settings set KEY VALUE.", u8"Введите номер или имя параметра; back — назад. Команда: settings set KEY VALUE.") \ + X(console_settings_value, u8"输入新值(cancel 取消)", "Enter a new value (cancel to abort)", u8"Введите новое значение (cancel — отмена)") \ + X(console_settings_unavailable, u8"设置尚未初始化,请稍后重试。", "Settings are not ready; try again shortly.", u8"Настройки ещё не готовы. Повторите позже.") \ + X(console_settings_invalid, u8"无效的选项或值,未保存。请按菜单列出的格式输入。", "Invalid setting or value; not saved. Use the format shown in the menu.", u8"Неверный параметр или значение. Ничего не сохранено. Используйте указанный формат.") \ + X(console_settings_save_failed, u8"保存设置失败,原设置保持不变。请检查日志。", "Failed to save settings; the previous value is retained. Check the log.", u8"Не удалось сохранить настройки. Прежнее значение сохранено. Проверьте журнал.") \ + X(console_settings_busy, u8"有远程会话或密码修改正在进行,暂时不能更改连接设置。", "Connection settings cannot change during a remote session or password change.", u8"Нельзя менять параметры подключения во время удалённого сеанса или смены пароля.") \ + X(console_settings_saved, u8"已保存并生效。", "Saved and applied.", u8"Сохранено и применено.") \ + X(console_settings_unchanged, u8"设置未改变。", "Setting unchanged.", u8"Настройка не изменилась.") \ + X(console_settings_reconnect, u8"已保存,正在重新连接服务器。", "Saved; reconnecting to the server.", u8"Сохранено. Повторное подключение к серверу.") \ + X(console_settings_next_connection, u8"已保存,将用于后续连接。", "Saved; applies to subsequent connections.", u8"Сохранено. Будет применено к следующим подключениям.") \ + X(console_settings_next_file, u8"已保存,将用于后续接收的文件。", "Saved; applies to subsequently received files.", u8"Сохранено. Будет применено к следующим получаемым файлам.") \ + X(console_settings_autostart, u8"已保存,在下次图形桌面登录时生效。", "Saved; applies at the next graphical desktop login.", u8"Сохранено. Будет применено при следующем входе в графический сеанс.") \ + X(console_settings_daemon, u8"已保存,普通模式重启后生效;无头控制台仍保持前台运行。", "Saved for the next normal-mode start; the headless console stays in the foreground.", u8"Сохранено для следующего запуска в обычном режиме. Консоль остаётся на переднем плане.") \ + X(console_settings_hardware_unavailable, u8"当前构建不支持硬件编解码,未保存。", "Hardware codecs are unavailable in this build; not saved.", u8"Аппаратные кодеки недоступны в этой сборке. Не сохранено.") \ + X(console_settings_server_required, u8"请先设置有效的服务器地址和端口,再启用自托管。", "Set a valid server host and port before enabling self-hosting.", u8"Укажите адрес и порт сервера перед включением собственного сервера.") \ + X(console_settings_path_invalid, u8"请选择已存在且可写的绝对目录路径;default 使用默认目录。", "Use an existing writable absolute directory, or default for the default location.", u8"Укажите существующий доступный для записи абсолютный путь к каталогу или default.") \ + X(console_settings_readonly, u8"该选项由系统或控制端管理,不可在此修改。", "This option is managed by the system or controller and is read-only here.", u8"Этот параметр управляется системой или клиентом и здесь недоступен для изменения.") \ + X(console_setting_language, u8"语言", "Language", u8"Язык") \ + X(console_setting_codec, u8"视频编码", "Video codec", u8"Видеокодек") \ + X(console_setting_hardware, u8"硬件编解码", "Hardware codecs", u8"Аппаратные кодеки") \ + X(console_setting_turn, u8"中继模式", "TURN relay mode", u8"Режим ретрансляции TURN") \ + X(console_setting_self_hosted, u8"自托管", "Self-hosted server", u8"Собственный сервер") \ + X(console_setting_host, u8"服务器地址", "Server host", u8"Адрес сервера") \ + X(console_setting_port, u8"信令端口", "Signaling port", u8"Порт сигнализации") \ + X(console_setting_privacy, u8"连接时自动开启隐私屏", "Privacy screen on connection", u8"Приватный экран при подключении") \ + X(console_setting_files, u8"文件保存目录", "Received file directory", u8"Каталог получаемых файлов") \ + X(console_setting_autostart, u8"登录桌面时自启", "Start at desktop login", u8"Запуск при входе в систему") \ + X(console_setting_daemon, u8"守护进程(无头控制台中不生效)", "Daemon (inactive in headless console)", u8"Фоновый режим (не действует в консоли)") \ X(privacy_screen, u8"隐私屏", "Privacy screen", u8"Приватный экран") \ X(privacy_enable, u8"开启隐私屏", "Enable privacy screen", u8"Включить приватный экран") \ X(privacy_disable, u8"关闭隐私屏", "Disable privacy screen", u8"Выключить приватный экран") \ diff --git a/apps/desktop/src/gui/runtime/gui_runtime.cpp b/apps/desktop/src/gui/runtime/gui_runtime.cpp index 3463b594..ebb2591a 100644 --- a/apps/desktop/src/gui/runtime/gui_runtime.cpp +++ b/apps/desktop/src/gui/runtime/gui_runtime.cpp @@ -14,6 +14,9 @@ #include "localization.h" #include "platform/video_renderer.h" #include "rd_log.h" +#ifdef __linux__ +#include "platform/linux/headless/headless_console.h" +#endif namespace crossdesk { @@ -92,6 +95,11 @@ int GuiRuntime::CreateConnectionPeer() { const char *active_identity = self_hosted ? self_hosted_user_id_ : client_id_with_password_; +#ifdef __linux__ + // Publish the committed identity; an unconfirmed recovery credential must + // not be advertised as the current connection password. + HeadlessConsole::Instance().SetIdentity(active_identity); +#endif const std::string login_identity = try_pending_identity ? pending_identity : std::string(active_identity); std::memset(connection_login_identity_, 0, diff --git a/apps/desktop/src/gui/runtime/peer_event_handler.cpp b/apps/desktop/src/gui/runtime/peer_event_handler.cpp index 7547343f..ccf3f6e2 100644 --- a/apps/desktop/src/gui/runtime/peer_event_handler.cpp +++ b/apps/desktop/src/gui/runtime/peer_event_handler.cpp @@ -22,6 +22,9 @@ #include "platform/video_renderer.h" #include "rd_log.h" #include "runtime/gui_runtime.h" +#ifdef __linux__ +#include "platform/linux/headless/headless_console.h" +#endif #if _WIN32 #include "interactive_state.h" @@ -496,6 +499,9 @@ void PeerEventHandler::OnNetStatusReport( LOG_INFO("Use client id [{}] and save id into cache file", id); runtime->settings_.Save(); } +#ifdef __linux__ + HeadlessConsole::Instance().SetIdentity(id + "@" + password); +#endif } std::string remote_id(user_id, user_id_size); diff --git a/apps/desktop/src/platform/linux/autostart.cpp b/apps/desktop/src/platform/linux/autostart.cpp index d66f683c..cccf1c85 100644 --- a/apps/desktop/src/platform/linux/autostart.cpp +++ b/apps/desktop/src/platform/linux/autostart.cpp @@ -11,6 +11,8 @@ namespace crossdesk::platform { namespace { std::filesystem::path AutostartPath(const std::string& app_name) { + const char* config = std::getenv("XDG_CONFIG_HOME"); + if (config && *config) return std::filesystem::path(config) / "autostart" / (app_name + ".desktop"); const char* home = std::getenv("HOME"); if (!home || !home[0]) return {}; return std::filesystem::path(home) / ".config" / "autostart" / diff --git a/apps/desktop/src/platform/linux/headless/desktop_session_selection.h b/apps/desktop/src/platform/linux/headless/desktop_session_selection.h new file mode 100644 index 00000000..505480ae --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/desktop_session_selection.h @@ -0,0 +1,85 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-09-25 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _DESKTOP_SESSION_SELECTION_H_ +#define _DESKTOP_SESSION_SELECTION_H_ + +#include +#include +#include +#include + +namespace crossdesk { + +// Metadata for an online X11 user session, already filtered to our uid. +struct DesktopLoginSession { + std::string display; + bool current = false; + bool primary = false; + bool active = false; + bool remote = false; +}; + +inline std::string NormalizeDesktopDisplay(std::string display) { + if (display.compare(0, 5, "unix:") == 0) display.erase(0, 4); + if (display.empty() || display[0] != ':') return display; + if (display.size() > 2 && display.compare(display.size() - 2, 2, ".0") == 0) { + display.resize(display.size() - 2); + } + return display; +} + +inline int DesktopSessionPriority( + const std::string& display, + const std::vector& sessions) { + int priority = 0; + for (const auto& session : sessions) { + if (NormalizeDesktopDisplay(session.display) != + NormalizeDesktopDisplay(display)) continue; + const int rank = session.current ? 6 + : session.primary && session.active ? 5 + : session.active && !session.remote ? 4 + : session.active ? 3 + : session.primary ? 2 : 1; + priority = std::max(priority, rank); + } + return priority; +} + +inline bool DesktopDisplayLess(const std::string& left, + const std::string& right) { + const auto a = NormalizeDesktopDisplay(left); + const auto b = NormalizeDesktopDisplay(right); + const auto is_number = [](const std::string& value) { + return value.size() > 1 && value[0] == ':' && + value.find_first_not_of("0123456789", 1) == std::string::npos; + }; + // Stable numeric order when logind is unavailable or sessions have equal + // priority; do not depend on filesystem enumeration order (or sort :10 < :2). + if (is_number(a) && is_number(b) && a.size() != b.size()) { + return a.size() < b.size(); + } + return a < b; +} + +inline std::optional SelectDesktopSession( + const std::vector& available, + const std::vector& sessions) { + if (available.empty()) return std::nullopt; + size_t selected = 0; + for (size_t i = 1; i < available.size(); ++i) { + const int candidate = DesktopSessionPriority(available[i], sessions); + const int current = DesktopSessionPriority(available[selected], sessions); + if (candidate > current || + (candidate == current && + DesktopDisplayLess(available[i], available[selected]))) selected = i; + } + return selected; +} + +} // namespace crossdesk + +#endif \ No newline at end of file diff --git a/apps/desktop/src/platform/linux/headless/headless_console.cpp b/apps/desktop/src/platform/linux/headless/headless_console.cpp new file mode 100644 index 00000000..54d5a07e --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/headless_console.cpp @@ -0,0 +1,357 @@ +#include "headless_console.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "rd_log.h" +#include "config_center.h" +#include "localization.h" + +namespace crossdesk { +namespace { +bool ValidPassword(const std::string& password) { + if (password.size() != 6) return false; + for (unsigned char c : password) { + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9'))) return false; + } + return true; +} +} // namespace + +HeadlessConsole& HeadlessConsole::Instance() { + static HeadlessConsole console; + return console; +} + +HeadlessConsole::~HeadlessConsole() { + if (!active()) return; + std::cout.flush(); + std::cerr.flush(); + fflush(nullptr); + dup2(output_, STDOUT_FILENO); + dup2(error_, STDERR_FILENO); + close(output_); + close(error_); +} + +bool HeadlessConsole::Enable(const std::filesystem::path& log_directory, + const std::filesystem::path& config_file) { + if (active()) return true; + std::error_code error; + std::filesystem::create_directories(log_directory, error); + if (error) return false; + // Existing loggers keep their own rotating files and filenames. Suppress + // only stdout/stderr copies; do not create a second diagnostic log stream. + const int quiet = open("/dev/null", O_WRONLY | O_CLOEXEC); + if (quiet < 0) return false; + const int out = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3); + const int err = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3); + if (out < 0 || err < 0) { + if (out >= 0) close(out); + if (err >= 0) close(err); + close(quiet); + return false; + } + std::cout.flush(); + std::cerr.flush(); + fflush(nullptr); + if (dup2(quiet, STDOUT_FILENO) < 0 || dup2(quiet, STDERR_FILENO) < 0) { + dup2(out, STDOUT_FILENO); + dup2(err, STDERR_FILENO); + close(out); + close(err); + close(quiet); + return false; + } + close(quiet); + output_ = out; + error_ = err; + interactive_ = isatty(STDIN_FILENO) && isatty(output_); + // Initialize after redirecting output so even the logger's first message + // stays in its original crossdesk-YYYYMMDD-HHMMSS.log file. + try { + InitLogger(log_directory.string()); + for (const auto& sink : get_logger()->sinks()) { + if (const auto file = + std::dynamic_pointer_cast(sink)) { + diagnostic_path_ = file->filename(); + break; + } + } + } catch (const std::exception&) { + dup2(output_, STDOUT_FILENO); + dup2(error_, STDERR_FILENO); + close(output_); + close(error_); + output_ = error_ = -1; + return false; + } + if (!config_file.empty()) { + ConfigCenter config(config_file.string()); + SetLanguage(static_cast(config.GetLanguage())); + } + Write(Text("console_title") + "\n" + Text("console_log_file") + ": " + diagnostic_path_ + "\n"); + Help(); + return true; +} + +void HeadlessConsole::Write(const std::string& text) { + if (!active()) return; + size_t offset = 0; + while (offset < text.size()) { + const ssize_t count = write(output_, text.data() + offset, text.size() - offset); + if (count < 0 && errno == EINTR) continue; + if (count <= 0) break; + offset += static_cast(count); + } +} + +void HeadlessConsole::Notify(const std::string& message) { + if (active()) Write(message + "\n"); +} + +void HeadlessConsole::SetIdentity(const std::string& identity) { + if (!active()) return; + const auto separator = identity.find('@'); + const std::string id = identity.substr(0, separator); + const std::string password = separator == std::string::npos + ? "" : identity.substr(separator + 1); + // Credentials are rendered to a terminal, never interpreted as terminal + // escape sequences and never forwarded to the diagnostic log. + if (id.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_") != + std::string::npos || (!password.empty() && !ValidPassword(password))) return; + std::lock_guard lock(identity_mutex_); + id_ = id; + password_ = password; +} + +const std::string& HeadlessConsole::Text(const char* key) const { + return localization::detail::GetTranslatedText(key, language_); +} + +void HeadlessConsole::NotifyKey(const char* key) { Notify(Text(key)); } + +void HeadlessConsole::SetLanguage(int language) { + language = localization::detail::ClampLanguageIndex(language); + if (language_ != language) { + language_ = language; + displayed_ = false; + } +} + +void HeadlessConsole::BindSettings(ConfigCenter* config, + std::function connection_busy, + std::function applied) { + config_ = config; + connection_busy_ = std::move(connection_busy); + settings_applied_ = std::move(applied); + if (config_) SetLanguage(static_cast(config_->GetLanguage())); +} + +void HeadlessConsole::Help() { NotifyKey("console_help"); } + +void HeadlessConsole::ShowSettings() { + if (!config_) { NotifyKey("console_settings_unavailable"); return; } + Write("\n" + Text("console_settings_title") + "\n"); + const auto settings = GetConsoleSettings(*config_); + for (size_t i = 0; i < settings.size(); ++i) { + const auto& item = settings[i]; + std::string value = item.value; + for (auto& c : value) if (static_cast(c) < 32 || c == 127) c = '?'; + Write(std::to_string(i + 1) + ". " + Text(item.label) + " [" + item.key + + "] = " + value + " (" + item.choices + ")\n"); + } + NotifyKey("console_settings_prompt"); +} + +void HeadlessConsole::ChangeSetting(const std::string& key, const std::string& value) { + if (!config_) { NotifyKey("console_settings_unavailable"); return; } + const auto result = ApplyConsoleSetting(*config_, key, value, + connection_busy_ && connection_busy_()); + SetLanguage(static_cast(config_->GetLanguage())); + if (result.saved && result.effect != SettingEffect::none && settings_applied_) { + settings_applied_(result.effect); + } + NotifyKey(result.message); +} + +bool HeadlessConsole::SettingsCommand(const std::string& line) { + if (line == "settings" || line == "settings show") { + waiting_password_ = false; + selected_setting_.clear(); + settings_menu_ = line == "settings"; + ShowSettings(); + return true; + } + if (line.compare(0, 13, "settings set ") == 0) { + waiting_password_ = false; + selected_setting_.clear(); + const auto separator = line.find(' ', 13); + if (separator == std::string::npos) NotifyKey("console_settings_invalid"); + else ChangeSetting(line.substr(13, separator - 13), line.substr(separator + 1)); + return true; + } + if (!settings_menu_) return false; + if (line == "back" || (line == "cancel" && selected_setting_.empty())) { + settings_menu_ = false; + selected_setting_.clear(); + Help(); + return true; + } + if (line == "cancel") { + selected_setting_.clear(); + NotifyKey("console_cancelled"); + ShowSettings(); + return true; + } + // Status and quit remain available while navigating the menu. + if (line == "status" || line == "help" || line == "?" || line == "quit" || line == "exit") return false; + if (!selected_setting_.empty()) { + ChangeSetting(selected_setting_, line); + selected_setting_.clear(); + ShowSettings(); + return true; + } + if (!config_) { NotifyKey("console_settings_unavailable"); return true; } + const auto settings = GetConsoleSettings(*config_); + size_t index = 0; + const auto parsed = std::from_chars(line.data(), line.data() + line.size(), index); + if (parsed.ec == std::errc{} && parsed.ptr == line.data() + line.size() && + index > 0 && index <= settings.size()) { + selected_setting_ = settings[index - 1].key; + } else { + for (const auto& setting : settings) if (setting.key == line) selected_setting_ = setting.key; + } + if (selected_setting_.empty()) { + NotifyKey("console_settings_invalid"); + } else { + const auto item = std::find_if(settings.begin(), settings.end(), + [&](const auto& setting) { return setting.key == selected_setting_; }); + Write(Text(item->label) + " [" + item->choices + "]\n" + Text("console_settings_value") + ":\n"); + } + return true; +} + +void HeadlessConsole::ShowStatus(bool connected) { + { + std::lock_guard lock(identity_mutex_); + displayed_id_ = id_; + displayed_password_ = password_; + } + displayed_connected_ = connected; + displayed_ = true; + Write("\n" + Text("console_status") + ": " + Text(connected ? "console_online" : "console_offline") + + "\n" + Text("console_id") + ": " + (displayed_id_.empty() ? Text("console_waiting") : displayed_id_) + + "\n" + Text("console_password") + ": " + + (!interactive_ ? Text("console_password_hidden") + : displayed_password_.empty() ? Text("console_waiting") : displayed_password_) + "\n"); +} + +void HeadlessConsole::Command( + const std::string& line, bool connected, + const std::function& password, + const std::function& random_password, + const std::function& quit) { + if (line == "quit" || line == "exit") { + input_closed_ = true; + NotifyKey("console_quitting"); + quit(); + return; + } + if (SettingsCommand(line)) return; + if (waiting_password_) { + waiting_password_ = false; + if (line.empty() || line == "cancel") { + NotifyKey("console_cancelled"); + } else if (!ValidPassword(line)) { + NotifyKey("console_password_invalid"); + } else { + password(line); + } + } else if (line == "status") { + ShowStatus(connected); + } else if (line == "help" || line == "?") { + Help(); + } else if (line == "password" || line == "passwd") { + if (!connected) { + NotifyKey("console_password_offline"); + } else { + waiting_password_ = true; + NotifyKey("console_password_prompt"); + } + } else if (line.compare(0, 9, "password ") == 0 || + line.compare(0, 7, "passwd ") == 0) { + const std::string value = line.substr(line.find(' ') + 1); + if (!ValidPassword(value)) { + NotifyKey("console_password_invalid"); + } else if (!connected) { + NotifyKey("console_password_offline"); + } else { + password(value); + } + } else if (line == "random") { + if (connected) random_password(); + else NotifyKey("console_password_offline"); + } else if (!line.empty()) { + NotifyKey("console_unknown_command"); + } +} + +void HeadlessConsole::Poll( + bool connected, const std::function& password, + const std::function& random_password, + const std::function& quit) { + if (!active()) return; + if (config_) SetLanguage(static_cast(config_->GetLanguage())); + bool changed = !displayed_ || displayed_connected_ != connected; + { + std::lock_guard lock(identity_mutex_); + changed |= displayed_id_ != id_ || displayed_password_ != password_; + } + if (changed) ShowStatus(connected); + if (input_closed_) return; + // A job launched in the background must not steal input or receive SIGTTIN. + if (isatty(STDIN_FILENO) && tcgetpgrp(STDIN_FILENO) != getpgrp()) return; + struct pollfd input { STDIN_FILENO, POLLIN, 0 }; + if (poll(&input, 1, 0) <= 0) return; + if (!(input.revents & (POLLIN | POLLHUP))) { + if (input.revents & (POLLERR | POLLNVAL)) input_closed_ = true; + return; + } + char buffer[256]; + const ssize_t count = read(STDIN_FILENO, buffer, sizeof(buffer)); + if (count <= 0) { + if (count == 0 || (errno != EINTR && errno != EAGAIN)) input_closed_ = true; + return; + } + for (ssize_t i = 0; i < count && !input_closed_; ++i) { + const char c = buffer[i]; + if (c == '\n') { + if (!discarding_line_) { + if (!input_.empty() && input_.back() == '\r') input_.pop_back(); + Command(input_, connected, password, random_password, quit); + } + input_.clear(); + discarding_line_ = false; + } else if (!discarding_line_) { + if (input_.size() == 1024) { + input_.clear(); + discarding_line_ = true; + waiting_password_ = false; + NotifyKey("console_line_too_long"); + } else { + input_ += c; + } + } + } +} +} // namespace crossdesk diff --git a/apps/desktop/src/platform/linux/headless/headless_console.h b/apps/desktop/src/platform/linux/headless/headless_console.h new file mode 100644 index 00000000..e5dfbedb --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/headless_console.h @@ -0,0 +1,78 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-09-25 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _HEADLESS_CONSOLE_H_ +#define _HEADLESS_CONSOLE_H_ + +#include +#include +#include +#include + +#include "headless_settings.h" + +namespace crossdesk { + +// Enabled before display discovery and fork, with no input thread. The GUI +// thread polls commands; transport callbacks only publish credential snapshots. +class HeadlessConsole { + public: + static HeadlessConsole& Instance(); + ~HeadlessConsole(); + bool Enable(const std::filesystem::path& log_directory, + const std::filesystem::path& config_file = {}); + bool active() const { return output_ >= 0; } + const std::string& diagnostic_path() const { return diagnostic_path_; } + void SetIdentity(const std::string& identity); + void Notify(const std::string& message); + const std::string& Text(const char* key) const; + void NotifyKey(const char* key); + void SetLanguage(int language); + void BindSettings(ConfigCenter* config, std::function connection_busy, + std::function applied); + void Poll(bool connected, const std::function& password, + const std::function& random_password, + const std::function& quit); + + private: + HeadlessConsole() = default; + void Write(const std::string& text); + void ShowStatus(bool connected); + void Help(); + void ShowSettings(); + void ChangeSetting(const std::string& key, const std::string& value); + bool SettingsCommand(const std::string& line); + void Command(const std::string& line, bool connected, + const std::function& password, + const std::function& random_password, + const std::function& quit); + + int output_ = -1; + int error_ = -1; + bool interactive_ = false; + bool input_closed_ = false; + bool waiting_password_ = false; + bool discarding_line_ = false; + std::string input_; + std::string diagnostic_path_; + std::mutex identity_mutex_; + std::string id_; + std::string password_; + std::string displayed_id_; + std::string displayed_password_; + bool displayed_connected_ = false; + bool displayed_ = false; + int language_ = 0; + ConfigCenter* config_ = nullptr; + std::function connection_busy_; + std::function settings_applied_; + bool settings_menu_ = false; + std::string selected_setting_; +}; + +} // namespace crossdesk + +#endif \ No newline at end of file diff --git a/apps/desktop/src/platform/linux/headless/headless_session.cpp b/apps/desktop/src/platform/linux/headless/headless_session.cpp new file mode 100644 index 00000000..0ec2c732 --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/headless_session.cpp @@ -0,0 +1,657 @@ +#include "headless_session.h" +#include "desktop_session_selection.h" +#include "rd_log.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace crossdesk { +namespace { +using Clock = std::chrono::steady_clock; +using namespace std::chrono_literals; +volatile sig_atomic_t stop_signal = 0; +void RequestStop(int signal) { stop_signal = signal; } + +// Bootstrap runs before GUI initialization, and may fork. Reuse the existing +// application logger when the headless console has silenced standard streams. +// Callers without a prepared console retain their normal stderr diagnostics. +bool log_diagnostics_to_file = false; +class Diagnostic { + public: + explicit Diagnostic(spdlog::level::level_enum level = spdlog::level::err) + : level_(level) {} + template Diagnostic& operator<<(const T& value) { + text_ << value; + return *this; + } + ~Diagnostic() { + std::string message = text_.str(); + if (!log_diagnostics_to_file) { + std::cerr << message; + return; + } + if (!message.empty() && message.back() == '\n') message.pop_back(); + get_logger()->log(level_, "{}", message); + } + private: + spdlog::level::level_enum level_; + std::ostringstream text_; +}; + +bool HasEnv(const char* name) { + const char* value = std::getenv(name); + return value && *value; +} + +bool ParseDimension(const std::string& value, int& result) { + const auto parsed = + std::from_chars(value.data(), value.data() + value.size(), result); + return parsed.ec == std::errc{} && parsed.ptr == value.data() + value.size() && + result >= 320 && result <= 8192 && result % 2 == 0; +} + +struct Options { + bool force = false; + bool disabled = false; + bool help = false; + bool new_session = false; + std::string size = "1920x1080"; + std::string session; + std::string display; +}; + +bool ParseOptions(int argc, char* argv[], Options& options) { + for (int i = 1; i < argc; ++i) { + const std::string arg = argv[i]; + if (arg == "--headless") { + options.force = true; + } else if (arg == "--no-headless") { + options.disabled = true; + } else if (arg == "--headless-help") { + options.help = true; + } else if (arg == "--headless-size" || arg == "--headless-session" || + arg == "--headless-display") { + if (++i == argc || argv[i][0] == '\0') { + Diagnostic() << arg << " requires a value\n"; + return false; + } + if (arg == "--headless-size") { + options.size = argv[i]; + options.new_session = true; + } else if (arg == "--headless-session") { + options.session = argv[i]; + options.new_session = true; + } else { + options.display = argv[i]; + } + options.force = true; + } else if (arg.rfind("--headless", 0) == 0) { + Diagnostic() << "Unknown option: " << arg << '\n'; + return false; + } + } + int width = 0, height = 0; + const auto separator = options.size.find('x'); + if (separator == std::string::npos || + !ParseDimension(options.size.substr(0, separator), width) || + !ParseDimension(options.size.substr(separator + 1), height)) { + Diagnostic() << "--headless-size requires WIDTHxHEIGHT with even dimensions " + "between 320 and 8192\n"; + return false; + } + if (options.force && options.disabled) { + Diagnostic() << "--no-headless conflicts with --headless options\n"; + return false; + } + if (!options.display.empty() && options.new_session) { + Diagnostic() << "--headless-display reuses an existing desktop and cannot be " + "combined with --headless-size or --headless-session\n"; + return false; + } + return true; +} + +struct ExistingDesktop { + std::string display; + std::optional authority; +}; + +std::vector GetDesktopLoginSessions() { + // Optional runtime integration: non-systemd distributions and containers + // still discover X11 desktops without a build/runtime dependency on logind. + struct Library { + void* handle = dlopen("libsystemd.so.0", RTLD_LAZY | RTLD_LOCAL); + ~Library() { if (handle) dlclose(handle); } + } library; + if (!library.handle) return {}; + using UserSessions = int (*)(uid_t, int, char***); + using SessionString = int (*)(const char*, char**); + using SessionFlag = int (*)(const char*); + using UserDisplay = int (*)(uid_t, char**); + using PidSession = int (*)(pid_t, char**); + const auto list = reinterpret_cast( + dlsym(library.handle, "sd_uid_get_sessions")); + const auto type = reinterpret_cast( + dlsym(library.handle, "sd_session_get_type")); + const auto session_class = reinterpret_cast( + dlsym(library.handle, "sd_session_get_class")); + const auto state = reinterpret_cast( + dlsym(library.handle, "sd_session_get_state")); + const auto display = reinterpret_cast( + dlsym(library.handle, "sd_session_get_display")); + const auto active = reinterpret_cast( + dlsym(library.handle, "sd_session_is_active")); + const auto remote = reinterpret_cast( + dlsym(library.handle, "sd_session_is_remote")); + const auto primary = reinterpret_cast( + dlsym(library.handle, "sd_uid_get_display")); + const auto current = reinterpret_cast( + dlsym(library.handle, "sd_pid_get_session")); + if (!list || !type || !session_class || !state || !display) return {}; + auto read = [](SessionString function, const char* id) { + char* value = nullptr; + const int status = function(id, &value); + std::string result = status >= 0 && value ? value : ""; + free(value); + return result; + }; + char* value = nullptr; + std::string primary_id, current_id; + if (primary && primary(getuid(), &value) >= 0 && value) primary_id = value; + free(value); + value = nullptr; + if (current && current(0, &value) >= 0 && value) current_id = value; + free(value); + if (current_id.empty() && HasEnv("XDG_SESSION_ID")) { + current_id = std::getenv("XDG_SESSION_ID"); + } + char** ids = nullptr; + const int count = list(getuid(), 0, &ids); + if (count < 0 || !ids) return {}; + std::vector sessions; + for (int i = 0; i < count; ++i) { + const std::string kind = read(session_class, ids[i]); + const std::string status = read(state, ids[i]); + if (read(type, ids[i]) == "x11" && + (kind == "user" || kind == "user-early") && + (status == "online" || status == "active")) { + const auto address = NormalizeDesktopDisplay(read(display, ids[i])); + // Only local displays, never greeters, SSH forwarding or other users. + if (address.size() > 1 && address[0] == ':' && + address.find_first_not_of("0123456789.", 1) == std::string::npos) { + sessions.push_back({address, current_id == ids[i], primary_id == ids[i], + active && active(ids[i]) > 0, + remote && remote(ids[i]) > 0}); + } + } + free(ids[i]); + } + free(ids); + return sessions; +} + +void SetAuthority(const std::optional& authority) { + if (authority) { + setenv("XAUTHORITY", authority->c_str(), 1); + } else { + unsetenv("XAUTHORITY"); + } +} + +Window ReadWindowProperty(Display* display, Window window, Atom property) { + Atom type = None; + int format = 0; + unsigned long count = 0, remaining = 0; + unsigned char* data = nullptr; + Window result = None; + if (XGetWindowProperty(display, window, property, 0, 1, False, XA_WINDOW, + &type, &format, &count, &remaining, &data) == Success && + type == XA_WINDOW && format == 32 && count == 1 && data) { + result = *reinterpret_cast(data); + } + if (data) XFree(data); + return result; +} + +bool HasDesktop(const std::string& address) { + Display* display = XOpenDisplay(address.c_str()); + if (!display) return false; + // A root framebuffer alone may be the empty Xvfb that caused the black + // background. Require a live EWMH window manager, including its self-check. + // Discovery runs before any threads; a WM disappearing mid-probe is benign. + auto previous = XSetErrorHandler([](Display*, XErrorEvent*) { return 0; }); + const Window root = DefaultRootWindow(display); + XWindowAttributes geometry{}; + const Atom property = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", True); + const Window manager = property == None ? None + : ReadWindowProperty(display, root, property); + const bool valid = manager != None && + ReadWindowProperty(display, manager, property) == manager && + XGetWindowAttributes(display, root, &geometry) && + geometry.width > 1 && geometry.height > 1; + XCloseDisplay(display); + XSetErrorHandler(previous); + return valid; +} + +std::vector FindExistingDesktops( + const std::string& requested, + const std::vector& sessions) { + const char* value = std::getenv("XAUTHORITY"); + const std::optional original = + value ? std::optional(value) : std::nullopt; + std::vector> authorities{original}; + // An explicit authority must be respected, including for automation and + // isolated tests. Otherwise also try GDM's per-user authority file. + if (!original) { + const std::string gdm = "/run/user/" + std::to_string(getuid()) + + "/gdm/Xauthority"; + struct stat info{}; + if (stat(gdm.c_str(), &info) == 0 && info.st_uid == getuid() && + S_ISREG(info.st_mode) && access(gdm.c_str(), R_OK) == 0) { + authorities.emplace_back(gdm); + } + } + auto probe = [&](const std::string& address) -> std::optional { + for (const auto& authority : authorities) { + SetAuthority(authority); + if (HasDesktop(address)) return ExistingDesktop{address, authority}; + } + return std::nullopt; + }; + std::vector matches; + if (!requested.empty()) { + if (auto match = probe(requested)) matches.push_back(*match); + } else if (HasEnv("DISPLAY")) { + // An explicit, usable desktop is unambiguous even with several logins. + if (auto match = probe(std::getenv("DISPLAY"))) matches.push_back(*match); + } + if (requested.empty() && matches.empty()) { + std::vector candidates; + // logind may identify a desktop whose X socket is owned by root. Its + // session still belongs to our uid and must pass the same X11/WM probes. + for (const auto& session : sessions) candidates.push_back(session.display); + std::error_code error; + std::filesystem::directory_iterator it("/tmp/.X11-unix", error), end; + for (; !error && it != end; it.increment(error)) { + const std::string name = it->path().filename().string(); + struct stat info{}; + if (name.size() > 1 && name[0] == 'X' && + name.find_first_not_of("0123456789", 1) == std::string::npos && + lstat(it->path().c_str(), &info) == 0 && S_ISSOCK(info.st_mode) && + info.st_uid == getuid()) { + candidates.push_back(":" + name.substr(1)); + } + } + std::sort(candidates.begin(), candidates.end(), DesktopDisplayLess); + candidates.erase(std::unique(candidates.begin(), candidates.end()), + candidates.end()); + for (const auto& address : candidates) { + if (auto match = probe(address)) matches.push_back(*match); + } + } + SetAuthority(original); + return matches; +} + +void ConfigureHeadlessX11(const std::string& display) { + // Capture, input and all GUI components must use the same X11 desktop, + // including when inheriting Wayland/DRM or NVIDIA PRIME hints from a shell. + setenv("DISPLAY", display.c_str(), 1); + setenv("XDG_SESSION_TYPE", "x11", 1); + unsetenv("WAYLAND_DISPLAY"); + unsetenv("WAYLAND_SOCKET"); + setenv("CROSSDESK_SCREEN_BACKEND", "x11", 1); + setenv("SDL_VIDEODRIVER", "x11", 1); + setenv("SLINT_BACKEND", "winit-software", 1); + setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1); + unsetenv("__NV_PRIME_RENDER_OFFLOAD"); + unsetenv("__NV_PRIME_RENDER_OFFLOAD_PROVIDER"); + unsetenv("__GLX_VENDOR_LIBRARY_NAME"); +} + +std::string FindExecutable(const std::string& name) { + if (name.find('/') != std::string::npos) { + return access(name.c_str(), X_OK) == 0 ? name : std::string{}; + } + const char* path = std::getenv("PATH"); + std::string paths = path ? path : "/usr/bin:/bin"; + size_t start = 0; + do { + const auto end = paths.find(':', start); + const std::string directory = paths.substr(start, end - start); + const std::string candidate = + (directory.empty() ? "." : directory) + "/" + name; + if (access(candidate.c_str(), X_OK) == 0) return candidate; + if (end == std::string::npos) break; + start = end + 1; + } while (start <= paths.size()); + return {}; +} + +// Xauthority's binary format consists of a family and four counted strings, +// all lengths in network byte order. FamilyWild and an empty display number +// allow the cookie to be prepared before Xvfb allocates its display number. +class PrivateAuthority { + public: + bool Create() { + char directory[] = "/tmp/crossdesk-headless-XXXXXX"; + if (!mkdtemp(directory)) return false; // mode 0700 + directory_ = directory; + path_ = directory_ + "/Xauthority"; + unsigned char cookie[16]; + size_t offset = 0; + while (offset < sizeof(cookie)) { + const ssize_t count = getrandom(cookie + offset, sizeof(cookie) - offset, 0); + if (count < 0 && errno == EINTR) continue; + if (count <= 0) return false; + offset += count; + } + const int fd = open(path_.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, + S_IRUSR | S_IWUSR); + if (fd < 0) return false; + FILE* file = fdopen(fd, "wb"); + if (!file) { + close(fd); + return false; + } + auto word = [file](unsigned int value) { + return fputc((value >> 8) & 255, file) != EOF && + fputc(value & 255, file) != EOF; + }; + constexpr char protocol[] = "MIT-MAGIC-COOKIE-1"; + const bool written = word(65535) && word(0) && word(0) && + word(sizeof(protocol) - 1) && + fwrite(protocol, 1, sizeof(protocol) - 1, file) == + sizeof(protocol) - 1 && + word(sizeof(cookie)) && + fwrite(cookie, 1, sizeof(cookie), file) == sizeof(cookie); + const bool closed = fclose(file) == 0; + return written && closed; + } + ~PrivateAuthority() { + if (!path_.empty()) unlink(path_.c_str()); + if (!directory_.empty()) rmdir(directory_.c_str()); + } + const std::string& path() const { return path_; } + + private: + std::string directory_; + std::string path_; +}; + +class SignalGuard { + public: + SignalGuard() { + stop_signal = 0; + struct sigaction action {}; + action.sa_handler = RequestStop; + sigemptyset(&action.sa_mask); + sigaction(SIGINT, &action, &old_int_); + sigaction(SIGTERM, &action, &old_term_); + sigaction(SIGHUP, &action, &old_hup_); + } + ~SignalGuard() { + sigaction(SIGINT, &old_int_, nullptr); + sigaction(SIGTERM, &old_term_, nullptr); + sigaction(SIGHUP, &old_hup_, nullptr); + } + + private: + struct sigaction old_int_ {}, old_term_ {}, old_hup_ {}; +}; + +class Child { + public: + Child() = default; + Child(const Child&) = delete; + Child& operator=(const Child&) = delete; + ~Child() { Stop(); } + + bool Start(const std::function& run, bool own_group = true) { + own_group_ = own_group; + const pid_t parent = getpid(); + pid_ = fork(); + if (pid_ == 0) { + if (own_group_) setpgid(0, 0); + signal(SIGINT, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGHUP, SIG_DFL); + // Also stop direct children if the supervisor itself is killed. + if (prctl(PR_SET_PDEATHSIG, SIGTERM) != 0 || getppid() != parent) _exit(1); + _exit(run()); + } + if (pid_ < 0) return false; + if (own_group_) setpgid(pid_, pid_); + return true; + } + + bool Exited(int* code = nullptr) const { + if (pid_ <= 0) return false; + siginfo_t info{}; + // Leave the child waitable until Stop(), preventing pid/group reuse. + if (waitid(P_PID, pid_, &info, WEXITED | WNOHANG | WNOWAIT) != 0 || + info.si_pid == 0) return false; + if (code) { + *code = info.si_code == CLD_EXITED ? info.si_status : 128 + info.si_status; + } + return true; + } + + void Stop() { + if (pid_ <= 0) return; + const pid_t target = own_group_ ? -pid_ : pid_; + kill(target, SIGTERM); + const auto deadline = Clock::now() + 2s; + while (!Exited() && Clock::now() < deadline) std::this_thread::sleep_for(25ms); + kill(target, SIGKILL); // Include descendants of desktop session scripts. + while (waitpid(pid_, nullptr, 0) < 0 && errno == EINTR) {} + pid_ = -1; + } + + private: + pid_t pid_ = -1; + bool own_group_ = true; +}; + +int Exec(const std::vector& arguments) { + std::vector argv; + for (const auto& arg : arguments) argv.push_back(const_cast(arg.c_str())); + argv.push_back(nullptr); + execv(argv[0], argv.data()); + Diagnostic() << "Cannot start " << arguments[0] << ": " << std::strerror(errno) + << '\n'; + return 127; +} + +bool StartServer(Child& server, const std::string& executable, + const Options& options, const PrivateAuthority& authority, + std::string& display) { + int pipe_fds[2]; + if (pipe2(pipe_fds, O_CLOEXEC | O_NONBLOCK) != 0) return false; + const bool started = server.Start([&] { + close(pipe_fds[0]); + fcntl(pipe_fds[1], F_SETFD, 0); + return Exec({executable, "-displayfd", std::to_string(pipe_fds[1]), + "-screen", "0", options.size + "x24", "-nolisten", "tcp", + "-auth", authority.path(), "-noreset"}); + }); + close(pipe_fds[1]); + std::string number; + const auto deadline = Clock::now() + 10s; + while (started && !stop_signal && !server.Exited() && Clock::now() < deadline) { + struct pollfd descriptor { pipe_fds[0], POLLIN, 0 }; + if (poll(&descriptor, 1, 50) <= 0) continue; + char buffer[32]; + const ssize_t size = read(pipe_fds[0], buffer, sizeof(buffer)); + if (size <= 0) break; + number.append(buffer, size); + if (number.find('\n') != std::string::npos || number.size() > 16) break; + } + close(pipe_fds[0]); + if (number.size() < 2 || number.back() != '\n') return false; + number.pop_back(); + if (number.find_first_not_of("0123456789") != std::string::npos) return false; + display = ":" + number; + return true; +} + +int RunHeadless(const Options& options, const std::function& run) { + const std::string xvfb = FindExecutable("Xvfb"); + if (xvfb.empty()) { + Diagnostic() << "No usable display. Headless mode requires Xvfb; on Debian/Ubuntu " + "install it with: sudo apt install xvfb\n"; + return 1; + } + std::string session; + std::string dbus; + if (!options.session.empty() && options.session != "none") { + session = FindExecutable(options.session); + dbus = FindExecutable("dbus-run-session"); + if (session.empty() || dbus.empty()) { + Diagnostic() << "Headless desktop requires an executable --headless-session " + "and dbus-run-session (Debian/Ubuntu: dbus-daemon)\n"; + return 1; + } + } + SignalGuard signals; + PrivateAuthority authority; + if (!authority.Create()) { + Diagnostic() << "Cannot create private headless Xauthority: " + << std::strerror(errno) << '\n'; + return 1; + } + Child server; + std::string display; + if (!StartServer(server, xvfb, options, authority, display)) { + Diagnostic() << "Xvfb failed to become ready within 10 seconds\n"; + return stop_signal ? 128 + stop_signal : 1; + } + ConfigureHeadlessX11(display); + setenv("XAUTHORITY", authority.path().c_str(), 1); + setenv("CROSSDESK_HEADLESS_ACTIVE", "1", 1); + + Display* probe = XOpenDisplay(nullptr); + if (!probe) { + Diagnostic() << "Cannot connect to the private Xvfb display\n"; + return 1; + } + XSetScreenSaver(probe, 0, 0, DefaultBlanking, DefaultExposures); + XCloseDisplay(probe); + Diagnostic(spdlog::level::info) << "CrossDesk headless display " << display << " (" << options.size + << "), XAUTHORITY=" << authority.path() << '\n'; + Child desktop; + if (!session.empty() && !desktop.Start([&] { + unsetenv("DBUS_SESSION_BUS_ADDRESS"); + unsetenv("SESSION_MANAGER"); + return Exec({dbus, "--", session}); + })) return 1; + Child application; + // Keep an interactive application in the foreground terminal group so its + // console can read commands. Xvfb and the optional desktop remain isolated. + if (!application.Start(run, !isatty(STDIN_FILENO))) return 1; + int result = 1; + while (!stop_signal) { + if (application.Exited(&result)) return result; + if (server.Exited() || desktop.Exited()) { + Diagnostic() << "Headless display or desktop session exited; stopping CrossDesk\n"; + return 1; + } + std::this_thread::sleep_for(50ms); + } + return 128 + stop_signal; +} +} // namespace + +int RunWithLinuxDisplay(int argc, char* argv[], + const std::function& run_application, + const std::function& prepare_headless) { + log_diagnostics_to_file = false; + Options options; + if (!ParseOptions(argc, argv, options)) return 2; + if (options.help) { + std::cout << "Linux headless options:\n" + " --headless Reuse an existing desktop when available\n" + " --headless-display DISPLAY Advanced: override automatic desktop selection\n" + " --headless-size WIDTHxHEIGHT New Xvfb; default 1920x1080; even, 320..8192\n" + " --headless-session EXECUTABLE New desktop (startxfce4, or none for bare Xvfb)\n" + " --no-headless Disable automatic Xvfb fallback\n" + "Normal startup automatically discovers and selects your existing " + "desktop. No display number is required. A new Xvfb does not " + "contain your existing apps.\n"; + return 0; + } + XInitThreads(); + if (!options.force) { + if (options.disabled || HasEnv("WAYLAND_DISPLAY") || HasEnv("WAYLAND_SOCKET")) { + return run_application(); + } + // Preserve normal graphical launches before SSH/headless discovery. + if (HasEnv("DISPLAY")) { + if (Display* display = XOpenDisplay(nullptr)) { + XCloseDisplay(display); + return run_application(); + } + } + } + if (prepare_headless) { + if (!prepare_headless()) return 1; + log_diagnostics_to_file = true; + } + if (!options.new_session) { + const auto sessions = GetDesktopLoginSessions(); + const auto desktops = FindExistingDesktops(options.display, sessions); + std::vector available; + for (const auto& desktop : desktops) available.push_back(desktop.display); + if (auto selected = SelectDesktopSession(available, sessions)) { + const auto& desktop = desktops[*selected]; + SetAuthority(desktop.authority); + ConfigureHeadlessX11(desktop.display); + Diagnostic(spdlog::level::info) << "CrossDesk reusing existing desktop " << desktop.display + << "; Dock, panels and running applications are preserved\n"; + if (desktops.size() > 1) { + Diagnostic(spdlog::level::info) << "Automatically selected from " << desktops.size() + << " available desktops using login-session priority\n"; + } + // The desktop belongs to the user, not this process. Never supervise or + // terminate its X server, window manager or applications on exit. + return run_application(); + } + if (!options.display.empty()) { + Diagnostic() << "No accessible desktop/window manager on " << options.display + << ". Check DISPLAY and XAUTHORITY; no new desktop was created.\n"; + return 1; + } + } + if (options.session.empty() || options.session == "none") { + Diagnostic(spdlog::level::warn) << "Starting a separate bare Xvfb display: it has no Dock, panels " + "or existing applications. To include a desktop environment, " + "use --headless-session startxfce4 after installing Xfce.\n"; + } + return RunHeadless(options, run_application); +} +} // namespace crossdesk diff --git a/apps/desktop/src/platform/linux/headless/headless_session.h b/apps/desktop/src/platform/linux/headless/headless_session.h new file mode 100644 index 00000000..ab939fb1 --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/headless_session.h @@ -0,0 +1,23 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-09-25 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _HEADLESS_SESSION_H_ +#define _HEADLESS_SESSION_H_ + +#include + +namespace crossdesk { + +// Must run before logging, SDL, Slint or any worker threads are initialized. +// Automatically selects the user's existing desktop, or supervises a private Xvfb +// server and the application as children for the duration of a headless session. +int RunWithLinuxDisplay(int argc, char* argv[], + const std::function& run_application, + const std::function& prepare_headless = {}); + +} // namespace crossdesk + +#endif \ No newline at end of file diff --git a/apps/desktop/src/platform/linux/headless/headless_settings.cpp b/apps/desktop/src/platform/linux/headless/headless_settings.cpp new file mode 100644 index 00000000..32f33581 --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/headless_settings.cpp @@ -0,0 +1,153 @@ +#include "headless_settings.h" + +#include + +#include +#include +#include +#include +#include +#include + +#include "config_center.h" + +namespace crossdesk { +namespace { +std::string Lower(std::string value) { + for (auto& c : value) if (c >= 'A' && c <= 'Z') c += 'a' - 'A'; + return value; +} +std::optional Boolean(const std::string& value) { + if (value == "on" || value == "true" || value == "1") return true; + if (value == "off" || value == "false" || value == "0") return false; + return std::nullopt; +} +const char* OnOff(bool value) { return value ? "on" : "off"; } +const char* languages[] = {"zh-CN", "en-US", "ru-RU"}; +const char* turn_modes[] = {"off", "auto", "udp", "tcp"}; +} // namespace + +std::vector GetConsoleSettings(const ConfigCenter& config) { + const auto language = std::clamp(static_cast(config.GetLanguage()), 0, 2); + const auto turn = std::clamp(static_cast(config.GetTurnMode()), 0, 3); + return { + {"language", "console_setting_language", languages[language], "zh-CN / en-US / ru-RU"}, + {"codec", "console_setting_codec", config.GetVideoEncodeFormat() == ConfigCenter::VIDEO_ENCODE_FORMAT::AV1 ? "av1" : "h264", "h264 / av1"}, + {"hardware", "console_setting_hardware", OnOff(config.IsHardwareVideoCodec()), "on / off"}, + {"turn", "console_setting_turn", turn_modes[turn], "off / auto / udp / tcp"}, + {"self_hosted", "console_setting_self_hosted", OnOff(config.IsSelfHosted()), "on / off"}, + {"server_host", "console_setting_host", config.GetSignalServerHost(), "example.com"}, + {"server_port", "console_setting_port", std::to_string(config.GetSignalServerPort()), "1..65535"}, + {"privacy", "console_setting_privacy", OnOff(config.IsEnablePrivacyScreen()), "on / off"}, + {"file_path", "console_setting_files", config.GetFileTransferSavePath().empty() ? "default" : config.GetFileTransferSavePath(), "/path/to/directory / default"}, + {"autostart", "console_setting_autostart", OnOff(config.IsEnableAutostart()), "on / off"}, + {"daemon", "console_setting_daemon", OnOff(config.IsEnableDaemon()), "on / off"}, + }; +} + +SettingResult ApplyConsoleSetting(ConfigCenter& config, const std::string& key, + const std::string& input, bool connection_busy) { + SettingResult result; + if (key == "capture_method" || key == "fps" || key == "quality") { + result.message = "console_settings_readonly"; + return result; + } + const bool network = key == "codec" || key == "hardware" || key == "turn" || + key == "self_hosted" || key == "server_host" || key == "server_port"; + if (network && connection_busy) { + result.message = "console_settings_busy"; + return result; + } + const std::string value = Lower(input); + int status = -1; + SettingEffect effect = SettingEffect::immediate; + const char* message = "console_settings_saved"; + std::string canonical = input; + std::function save; + if (key == "language") { + int language = value == "zh-cn" || value == "zh" || value == "0" ? 0 + : value == "en-us" || value == "en" || value == "1" ? 1 + : value == "ru-ru" || value == "ru" || value == "2" ? 2 : -1; + if (language < 0) return result; + canonical = languages[language]; + save = [&config, language] { return config.SetLanguage(static_cast(language)); }; + } else if (key == "codec") { + if (value != "h264" && value != "av1") return result; + canonical = value; + save = [&] { return config.SetVideoEncodeFormat(value == "h264" ? ConfigCenter::VIDEO_ENCODE_FORMAT::H264 : ConfigCenter::VIDEO_ENCODE_FORMAT::AV1); }; + } else if (key == "turn") { + const auto found = std::find(std::begin(turn_modes), std::end(turn_modes), value); + if (found == std::end(turn_modes)) return result; + const int mode = static_cast(found - std::begin(turn_modes)); + canonical = value; + save = [&config, mode] { return config.SetTurnMode(static_cast(mode)); }; + } else if (key == "server_host") { + if (input.empty() || input.size() > 253 || + input.find_first_of("/\\@?#") != std::string::npos || + std::any_of(input.begin(), input.end(), [](unsigned char c) { return c <= 32 || c == 127; })) return result; + save = [&] { return config.SetServerHost(input); }; + } else if (key == "server_port") { + int port = 0; + const auto parsed = std::from_chars(input.data(), input.data() + input.size(), port); + if (parsed.ec != std::errc{} || parsed.ptr != input.data() + input.size() || port < 1 || port > 65535) return result; + canonical = std::to_string(port); + save = [&config, port] { return config.SetServerPort(port); }; + } else if (key == "file_path") { + std::string path = input == "default" ? "" : input; + if (path.compare(0, 2, "~/") == 0 && std::getenv("HOME")) path.replace(0, 1, std::getenv("HOME")); + std::error_code error; + if (!path.empty() && (path.size() >= 512 || !std::filesystem::path(path).is_absolute() || + !std::filesystem::is_directory(path, error) || access(path.c_str(), W_OK | X_OK) != 0 || + std::any_of(path.begin(), path.end(), [](unsigned char c) { return c < 32 || c == 127; }))) { + result.message = "console_settings_path_invalid"; + return result; + } + canonical = path.empty() ? "default" : path; + save = [&config, path] { return config.SetFileTransferSavePath(path); }; + effect = SettingEffect::next_file; + message = "console_settings_next_file"; + } else { + const auto boolean = Boolean(value); + if (!boolean) return result; + const bool enabled = *boolean; + canonical = OnOff(enabled); + if (key == "hardware") { + if (enabled && !ConfigCenter::IsHardwareVideoCodecAvailable()) { + result.message = "console_settings_hardware_unavailable"; + return result; + } + save = [&config, enabled] { return config.SetHardwareVideoCodec(enabled); }; + } else if (key == "self_hosted") { + if (enabled && (config.GetSignalServerHost().empty() || config.GetSignalServerPort() <= 0 || config.GetSignalServerPort() > 65535)) { + result.message = "console_settings_server_required"; + return result; + } + save = [&config, enabled] { return config.SetSelfHosted(enabled); }; + } else if (key == "privacy") { + save = [&config, enabled] { return config.SetPrivacyScreen(enabled); }; + effect = SettingEffect::next_connection; + message = "console_settings_next_connection"; + } else if (key == "autostart") { + save = [&config, enabled] { return config.SetAutostart(enabled); }; + effect = SettingEffect::next_login; + message = "console_settings_autostart"; + } else if (key == "daemon") { + save = [&config, enabled] { return config.SetDaemon(enabled); }; + effect = SettingEffect::normal_restart; + message = "console_settings_daemon"; + } else return result; + } + for (const auto& setting : GetConsoleSettings(config)) { + if (key != "autostart" && key != "hardware" && setting.key == key && setting.value == canonical) { + return {true, SettingEffect::none, "console_settings_unchanged"}; + } + } + status = save(); + if (status != 0) return {false, SettingEffect::none, "console_settings_save_failed"}; + if (network && ((key != "server_host" && key != "server_port") || config.IsSelfHosted())) { + effect = SettingEffect::reconnect; + message = "console_settings_reconnect"; + } + return {true, effect, message}; +} +} // namespace crossdesk diff --git a/apps/desktop/src/platform/linux/headless/headless_settings.h b/apps/desktop/src/platform/linux/headless/headless_settings.h new file mode 100644 index 00000000..49bf132b --- /dev/null +++ b/apps/desktop/src/platform/linux/headless/headless_settings.h @@ -0,0 +1,36 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-09-25 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _HEADLESS_SETTINGS_H_ +#define _HEADLESS_SETTINGS_H_ + +#include +#include + +namespace crossdesk { +class ConfigCenter; + +enum class SettingEffect { none, immediate, reconnect, next_connection, next_file, + next_login, normal_restart }; +struct ConsoleSetting { + std::string key; + const char* label; + std::string value; + const char* choices; +}; + +struct SettingResult { + bool saved = false; + SettingEffect effect = SettingEffect::none; + const char* message = "console_settings_invalid"; +}; + +std::vector GetConsoleSettings(const ConfigCenter& config); +SettingResult ApplyConsoleSetting(ConfigCenter& config, const std::string& key, + const std::string& value, bool connection_busy); +} // namespace crossdesk + +#endif \ No newline at end of file diff --git a/apps/desktop/src/platform/linux/screen_capturer/screen_capturer_x11.cpp b/apps/desktop/src/platform/linux/screen_capturer/screen_capturer_x11.cpp index 9665b954..eb4f966e 100644 --- a/apps/desktop/src/platform/linux/screen_capturer/screen_capturer_x11.cpp +++ b/apps/desktop/src/platform/linux/screen_capturer/screen_capturer_x11.cpp @@ -67,6 +67,7 @@ ScreenCapturerX11::~ScreenCapturerX11() { Destroy(); } int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { Destroy(); + if (!cb) return -1; display_ = XOpenDisplay(nullptr); if (!display_) { @@ -75,59 +76,57 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { } root_ = DefaultRootWindow(display_); - screen_res_ = XRRGetScreenResources(display_, root_); - if (!screen_res_) { - LOG_ERROR("Failed to get screen resources"); - XCloseDisplay(display_); - display_ = nullptr; - return 1; + XWindowAttributes attr{}; + if (!XGetWindowAttributes(display_, root_, &attr)) { + LOG_ERROR("Failed to get X11 root window geometry"); + Destroy(); + return -1; } - - for (int i = 0; i < screen_res_->noutput; ++i) { - RROutput output = screen_res_->outputs[i]; - XRROutputInfo* output_info = - XRRGetOutputInfo(display_, screen_res_, output); - - if (output_info->connection == RR_Connected && output_info->crtc != 0) { - XRRCrtcInfo* crtc_info = - XRRGetCrtcInfo(display_, screen_res_, output_info->crtc); - - std::string name(output_info->name); - - if (name.empty()) { - name = "Display" + std::to_string(i + 1); - } - - display_info_list_.push_back(DisplayInfo( - (void*)display_, name, true, crtc_info->x, crtc_info->y, - crtc_info->x + crtc_info->width, crtc_info->y + crtc_info->height)); - - XRRFreeCrtcInfo(crtc_info); - } - - if (output_info) { - XRRFreeOutputInfo(output_info); - } - } - - XWindowAttributes attr; - XGetWindowAttributes(display_, root_, &attr); - - width_ = attr.width; - height_ = attr.height; - - if ((width_ & 1) != 0 || (height_ & 1) != 0) { - LOG_WARN("X11 root size {}x{} is not even, aligning down to {}x{} for NV12", - width_, height_, width_ & ~1, height_ & ~1); - width_ &= ~1; - height_ &= ~1; - } - + width_ = attr.width & ~1; + height_ = attr.height & ~1; if (width_ <= 1 || height_ <= 1) { LOG_ERROR("Invalid capture size after alignment: {}x{}", width_, height_); + Destroy(); return -2; } + int event_base = 0, error_base = 0; + if (XRRQueryExtension(display_, &event_base, &error_base)) { + screen_res_ = XRRGetScreenResources(display_, root_); + } + if (screen_res_) { + for (int i = 0; i < screen_res_->noutput; ++i) { + XRROutputInfo* output = + XRRGetOutputInfo(display_, screen_res_, screen_res_->outputs[i]); + if (!output) continue; + if (output->connection == RR_Connected && output->crtc != 0) { + XRRCrtcInfo* crtc = XRRGetCrtcInfo(display_, screen_res_, output->crtc); + if (crtc) { + std::string name(output->name, output->nameLen); + if (name.empty()) name = "Display" + std::to_string(i + 1); + // Reject stale/offscreen CRTCs after hot-unplug. The root drawable + // remains a usable virtual screen even when no output is connected. + if (crtc->width > 1 && crtc->height > 1 && crtc->x >= 0 && + crtc->y >= 0 && + crtc->x + crtc->width <= static_cast(attr.width) && + crtc->y + crtc->height <= static_cast(attr.height)) { + display_info_list_.emplace_back( + display_, name, true, crtc->x, crtc->y, + crtc->x + crtc->width, crtc->y + crtc->height); + } + XRRFreeCrtcInfo(crtc); + } + } + XRRFreeOutputInfo(output); + } + } + if (display_info_list_.empty()) { + display_info_list_.emplace_back(display_, "Virtual Display", true, + 0, 0, width_, height_); + LOG_INFO("No active X11 outputs; capturing virtual root window {}x{}", + width_, height_); + } + fps_ = fps; callback_ = cb; @@ -136,6 +135,7 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { if (!ProbeCapture()) { LOG_ERROR("X11 backend probe failed, XGetImage is not usable"); + Destroy(); return -3; } @@ -147,6 +147,10 @@ int ScreenCapturerX11::Destroy() { y_plane_.clear(); uv_plane_.clear(); + display_info_list_.clear(); + monitor_index_ = initial_monitor_index_ = 0; + root_ = 0; + callback_ = nullptr; if (screen_res_) { XRRFreeScreenResources(screen_res_); @@ -162,6 +166,7 @@ int ScreenCapturerX11::Destroy() { int ScreenCapturerX11::Start(bool show_cursor) { if (running_) return 0; + if (!display_ || display_info_list_.empty() || !callback_) return -1; show_cursor_ = show_cursor; running_ = true; paused_ = false; @@ -206,6 +211,8 @@ int ScreenCapturerX11::Resume(int monitor_index) { } int ScreenCapturerX11::SwitchTo(int monitor_index) { + if (monitor_index < 0 || + monitor_index >= static_cast(display_info_list_.size())) return -1; monitor_index_ = monitor_index; return 0; } diff --git a/apps/desktop/xmake/targets.lua b/apps/desktop/xmake/targets.lua index 5ca9dbda..bd287c42 100644 --- a/apps/desktop/xmake/targets.lua +++ b/apps/desktop/xmake/targets.lua @@ -280,6 +280,17 @@ function setup_targets() "apps/desktop/src/platform/linux/common") end + if is_os("linux") then + target("headless_console") + set_kind("object") + add_deps("rd_log", "config_center", "assets") + add_includedirs("apps/desktop/src", {public = true}) + add_files("apps/desktop/src/platform/linux/headless/headless_console.cpp", + "apps/desktop/src/platform/linux/headless/headless_settings.cpp") + target_end() + + end + target("speaker_capturer") set_kind("object") add_deps("rd_log", "crossdesk_wire") @@ -450,6 +461,7 @@ function setup_targets() "apps/desktop/src/platform/macos/gui/tray") elseif is_os("linux") then add_links("GL") + add_deps("headless_console") add_files("apps/desktop/src/platform/linux/gui/tray/linux_tray.cpp", "apps/desktop/src/platform/linux/gui/cuda_graphics.cpp", "apps/desktop/src/platform/common/gui/opengl_video_renderer.cpp", @@ -527,7 +539,8 @@ function setup_targets() "apps/desktop/src/platform/macos/daemon_backend.cpp") elseif is_os("linux") then add_files("apps/desktop/src/platform/common/posix_daemon_backend.cpp", - "apps/desktop/src/platform/linux/daemon_backend.cpp") + "apps/desktop/src/platform/linux/daemon_backend.cpp", + "apps/desktop/src/platform/linux/headless/headless_session.cpp") end after_build(copy_slint_runtime) after_build(copy_usbmmidd_package) diff --git a/docker/linux-build/Dockerfile b/docker/linux-build/Dockerfile index dd455392..4e192c56 100644 --- a/docker/linux-build/Dockerfile +++ b/docker/linux-build/Dockerfile @@ -90,7 +90,9 @@ RUN apt-get update \ python3 \ python3-pip \ unzip \ + xfce4 \ xutils-dev \ + xvfb \ xz-utils \ yasm \ zip \ diff --git a/docs/BUILD.md b/docs/BUILD.md index 7aef7730..60331f83 100644 --- a/docs/BUILD.md +++ b/docs/BUILD.md @@ -57,13 +57,13 @@ xmake r crossdesk ## Linux Linux 构建支持 Ubuntu 20.04 及以上版本的 amd64 和 arm64。发布安装包以 -Ubuntu 20.04/glibc 2.31 为兼容基线。以下依赖命令以 Ubuntu 20.04 为例;其他发行版或较新版本需按其软件源调整包名和工具链,复现 CI 可使用下文的构建镜像。先安装基础编译依赖: +Ubuntu 20.04/glibc 2.31 为兼容基线。以下依赖命令以 Ubuntu 20.04 为例;其他发行版或较新版本需按其软件源调整包名和工具链,复现 CI 可使用下文的构建镜像。先安装基础编译和无头运行依赖: ```bash sudo apt-get update sudo apt-get install -y \ git curl unzip build-essential gcc-10 g++-10 python3-pip \ - pkg-config binutils dpkg-dev \ + pkg-config binutils dpkg-dev xvfb xfce4 dbus \ libx11-dev libxext-dev libxrender-dev libxft-dev libxrandr-dev \ libxinerama-dev libxcursor-dev libxi-dev libxfixes-dev libxv-dev \ libxtst-dev libxcb-randr0-dev libxcb-xtest0-dev libxcb-xinerama0-dev \ diff --git a/docs/BUILD_EN.md b/docs/BUILD_EN.md index ab348fa5..3f84a05c 100644 --- a/docs/BUILD_EN.md +++ b/docs/BUILD_EN.md @@ -60,13 +60,13 @@ Linux builds support Ubuntu 20.04 or later on amd64 and arm64. Release packages use Ubuntu 20.04/glibc 2.31 as their compatibility baseline. The dependency commands below target Ubuntu 20.04; adapt package names and toolchains for other distributions or newer versions, or use the build image below to -reproduce CI. Install the base build dependencies first: +reproduce CI. Install the base build and headless runtime dependencies first: ```bash sudo apt-get update sudo apt-get install -y \ git curl unzip build-essential gcc-10 g++-10 python3-pip \ - pkg-config binutils dpkg-dev \ + pkg-config binutils dpkg-dev xvfb xfce4 dbus \ libx11-dev libxext-dev libxrender-dev libxft-dev libxrandr-dev \ libxinerama-dev libxcursor-dev libxi-dev libxfixes-dev libxv-dev \ libxtst-dev libxcb-randr0-dev libxcb-xtest0-dev libxcb-xinerama0-dev \ diff --git a/docs/FAQ.md b/docs/FAQ.md index 7e13f336..ba731e60 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -24,6 +24,10 @@ Wayland 画面捕获依赖启用了 Wayland 支持的构建、宿主系统 PipeWire 0.3 和桌面门户授权;无法捕获时也可在 X11 会话中对比验证。构建选项见 [Linux 构建说明](BUILD.md#linux)。 +## Linux 电脑未连接显示器或没有图形会话 + +正常启动 CrossDesk 即可自动发现并选择当前用户的已有 X11 桌面,保留原来的 Dock 和应用,无需知道显示编号;多个会话会根据登录状态和活动状态自动选择。只看到 CrossDesk、背景一片黑,通常是创建了未运行桌面环境的裸 Xvfb;请改为复用原桌面。需要独立虚拟桌面时安装 `xvfb xfce4 dbus`,使用 `--headless-size 1920x1080 --headless-session startxfce4`。部署与限制见 [Linux 无头运行](HEADLESS.md)。 + ## Windows 锁屏后无法操作 在被控电脑检查 CrossDesk Service 是否安装、是否运行,并保持 CrossDesk 客户端运行。便携版可在“设置 → 锁屏控制服务”安装。服务状态检查命令见 [Windows 服务说明](../README.md#windows-service)。 @@ -84,6 +88,7 @@ Windows 安装包和便携包内置了 Amyuni 的 `usbmmidd_v2` 虚拟显示器 | P2P connection failed | Enable TURN relay in Settings and reconnect. For self-hosting, check Coturn and relay/media firewall ports. Review [release compatibility notes](https://github.com/kunkundi/crossdesk/releases). | | Black screen / no input on macOS | Grant Screen Recording and Accessibility, then reopen the app. A changed app path or signature may require fresh permission grants. | | No capture on Linux Wayland | Check the Wayland build option, host PipeWire 0.3, and desktop portal permission. Compare with an X11 session. See [Linux build instructions](BUILD_EN.md#linux). | +| Linux host has no monitor or graphical session | Start CrossDesk normally to automatically discover and select your existing desktop, preserving its Dock and apps; no display number is needed. A bare Xvfb shows only CrossDesk on a black background; use an existing session or install Xfce and specify `--headless-session startxfce4`. See [Linux headless operation](HEADLESS_EN.md). | | Cannot control a Windows lock screen | Install/start CrossDesk Service and keep the host client running. See [service commands](../README_EN.md#windows-service). | | Windows host has no monitor attached | The Windows installer and portable archive bundle Amyuni's `usbmmidd_v2` virtual display driver (in the `usbmmidd_v2` folder with its License.txt). On each connection to a host without a monitor (capture method Auto), CrossDesk starts streaming in compatibility mode and, in the background, asks the CrossDesk Service session helper to install the Microsoft-signed driver (first time only) and plug one virtual monitor at 1920×1080; capture then switches to DXGI/WGC on that monitor and it is unplugged when the session ends. Without the service only an elevated CrossDesk can plug it itself; otherwise the host stays in **headless compatibility mode**: GDI capture at an OS-fixed resolution (often 1024×768) with reduced frame rate; the host log records why. Input, privacy screen and file transfer still work. A real monitor or an HDMI/DP dummy plug also restores GPU capture. | | Removing the bundled virtual display driver | The uninstaller removes the driver when CrossDesk installed it and leaves a driver you installed yourself. For the portable build run `deviceinstaller64 enableidd 0`, `deviceinstaller64 stop usbmmidd` and `deviceinstaller64 remove usbmmidd` as administrator from the `usbmmidd_v2` folder, or uninstall "USB Mobile Monitor Virtual Display" in Device Manager. | diff --git a/docs/HEADLESS.md b/docs/HEADLESS.md new file mode 100644 index 00000000..97bac797 --- /dev/null +++ b/docs/HEADLESS.md @@ -0,0 +1,155 @@ +# Linux 无头运行 + +[返回 README](../README.md) · [English](HEADLESS_EN.md) + +无显示器的 Linux 主机可复用已有 X11 桌面,也可通过 Xvfb 创建独立虚拟桌面。画面采集、键鼠、光标和剪贴板使用同一个显示会话。 + +## 保留原来的 Ubuntu 桌面和应用 + +正常打开 CrossDesk 即可,程序会自动发现并选择当前用户的已有桌面,保留 Dock、任务栏、壁纸和已打开应用。无需知道 `:0`、`:10` 等显示编号,也无需设置 `DISPLAY`: + +```bash +# 安装包 +crossdesk +# 源码构建 +xmake r crossdesk +``` + +多个可用桌面同时存在时也会自动选择:优先启动环境中的桌面,其次是当前登录会话、系统标记的活动主桌面、其他活动桌面和仍在线的桌面。没有系统会话信息或优先级相同时,按稳定的显示顺序选择,避免每次启动切到不同桌面。失效会话与没有窗口管理器的空白 Xvfb 不会作为已有桌面选中。 + +复用已有桌面不依赖 Xvfb,也不会在退出 CrossDesk 时结束桌面或其他应用。若原会话已锁屏,远程会先看到系统锁屏界面,需要用该用户的系统密码解锁。 + +自动发现使用系统登录会话信息和当前用户的 X11 套接字,并验证窗口管理器确实可用。没有 systemd/logind 的发行版仍可自动发现并选择桌面。认证使用现有 `XAUTHORITY`(未设置时使用 Xlib 默认认证位置),必要时尝试当前用户的 GDM 认证文件。不会自动选择其他用户的桌面或登录欢迎界面。 + +`--headless` 可用于显式触发这套自动选择流程,例如忽略终端里失效的 Wayland 环境变量。`--headless-display` 仅供需要覆盖默认选择的高级使用者或调试使用: + +```bash +crossdesk --headless-display :10 # 示例编号,以实际会话为准 +``` + +此覆盖选项只连接指定的已有桌面;无法连接或没有运行窗口管理器时会报错,不会悄悄创建另一块屏幕。 + +**不要添加 `--headless-size` 或 `--headless-session` 来连接原桌面**:它们用于创建独立 Xvfb。原桌面的分辨率由原来的显示服务管理。 + +## 命令行控制台 + +无头模式下,终端只显示设备 ID、连接密码、在线状态和操作结果,常规日志写入文件。在 SSH / 无图形环境下普通启动会自动进入此模式;也可显式启用: + +```bash +xmake r crossdesk --headless +``` + +程序运行后,在同一个终端输入命令并回车: + +| 命令 | 作用 | +| --- | --- | +| `status` | 显示本机 ID、当前连接密码和在线状态 | +| `settings` | 打开设置菜单,输入编号或名称修改;`back` 返回 | +| `settings show` | 仅查看设置 | +| `settings set 名称 值` | 直接修改一个设置 | +| `password` | 提示输入新密码;6 位英文字母或数字,输入 `cancel` 取消 | +| `password Ab12Cd` | 直接提交新密码(示例,请自行选择密码) | +| `random` | 生成随机密码并提交修改 | +| `help` | 显示帮助 | +| `quit` | 关闭 CrossDesk;复用模式下不会关闭原桌面 | + +设置菜单与 GUI 共用 `config.ini`,每项修改校验通过后立即保存,无需另执行保存命令。输入 `cancel` 放弃正在编辑的值。控制台状态、菜单、密码提示和帮助跟随语言设置,支持中文、英文和俄文;命令名称保持不变。 + +| 设置名称 | 可选值 / 格式 | 生效方式 | +| --- | --- | --- | +| `language` | `zh-CN` / `en-US` / `ru-RU` | 立即切换控制台和 GUI 语言,下次启动保留 | +| `codec` | `h264` / `av1` | 保存后重新连接 | +| `hardware` | `on` / `off` | 保存后重新连接;构建不支持时拒绝开启 | +| `turn` | `off` / `auto` / `udp` / `tcp` | 保存后重新连接;`udp` / `tcp` 表示强制中继 | +| `server_host` | 主机名或 IP,不含协议与路径 | 自托管未启用时保存备用,已启用时重连 | +| `server_port` | `1`–`65535` | 同上 | +| `self_hosted` | `on` / `off` | 先配置地址、端口,再开启;保存后重连 | +| `privacy` | `on` / `off` | 后续远程连接的自动隐私屏行为 | +| `file_path` | 已存在且可写的绝对目录;`default` 恢复默认 | 后续接收的文件;路径可含空格,无需额外引号 | +| `autostart` | `on` / `off` | 下次图形桌面登录时启动;不等于系统服务开机启动 | +| `daemon` | `on` / `off` | 普通模式下次启动生效;无头控制台仍保持前台 | + +例如,在运行中的控制台输入: + +```text +settings +1 +en-US +back +settings set codec av1 +settings set file_path /home/user/Received Files +``` + +存在远程会话(含正在连接的会话)或尚未完成的密码修改时,连接相关设置会被拒绝,避免中断会话。修改服务器地址和端口前,应先结束远程会话。切换自托管可能切换到该服务器对应的 ID 和密码,以重新连接后的 `status` 为准。画质和帧率由控制端管理,Linux 无头模式采用 X11 采集,这些不提供额外的全局修改入口。 + +修改密码需要连接到信令服务器。控制台会显示提交、确认和重新连接的结果;仅在服务器确认后采用新密码,并沿用 GUI 的凭据持久化与超时恢复流程。离线、密码格式错误或修改尚未完成时,会给出明确提示。密码修改会触发重新连接。 + +日志路径会在启动时显示,继续使用原有的 `crossdesk-YYYYMMDD-HHMMSS.log` 和 `minirtc-YYYYMMDD-HHMMSS.log`,文件命名和原有轮转规则不变,不额外创建无头模式日志文件。无头启动诊断写入 CrossDesk 日志;终端日志副本和第三方组件直接写向标准输出/标准错误的内容被静默处理。ID/密码状态输出直接送往终端,不经过日志系统。非交互运行(例如 systemd、重定向输出)默认不显示明文密码;标准输入关闭不会终止后台服务。 + +交互控制台在前台运行,内置 daemon 设置在此模式下不会将进程分离。当前已有旧版本进程时,需要退出旧实例后启动新版本才能使用控制台。 + +## 创建独立虚拟桌面 + +Debian/Ubuntu 安装包将 Xvfb、Xfce 和 D-Bus 会话工具声明为强依赖,使用 APT 安装时会自动补齐,即使指定 `--no-install-recommends` 也不会跳过。源码直接运行时,需在开发环境中准备同样的运行依赖;项目构建镜像和 CI 已包含这些组件。 + +原来没有图形会话,或者希望单独开一套桌面时,可启动独立 Xfce 会话。Ubuntu / Debian 示例(安装包用户无需重复安装依赖): + +```bash +sudo apt install xvfb xfce4 dbus +crossdesk --headless-size 1920x1080 --headless-session startxfce4 +``` + +默认分辨率为 1920×1080,宽高必须是 320–8192 范围内的偶数。`--headless-session` 接收一个可执行程序名称或路径,通过独立的 `dbus-run-session` 启动。需要参数时,创建可执行脚本,在脚本中用 `exec` 启动桌面或窗口管理器。 + +仅做画面采集调试时,可以明确启动裸 Xvfb: + +```bash +sudo apt install xvfb +crossdesk --headless-session none +``` + +裸 Xvfb 只提供屏幕,**不会生成 Dock、任务栏或原来的应用窗口**。只使用 `--headless-size` 而未指定桌面会话也属于这一情况,启动诊断日志会记录提示。此前“只看到 CrossDesk,其余一片黑”就是因为进入了这种独立裸显示。 + +## 自动选择与限制 + +- 普通图形启动继续使用现有 `DISPLAY` 或 Wayland 会话。X11 没有活动 RandR 输出或没有 RandR 扩展时,采集可用的根窗口。 +- `--headless`,或没有可连接的 `DISPLAY` 且未设置 Wayland 环境时,会先自动选择已有 X11 桌面,再回退到独立 Xvfb;没有桌面环境时,回退的裸 Xvfb 会明确提示其不包含 Dock 和原应用。 +- `--headless-display` 与 `--headless-size` / `--headless-session` 不能混用。 +- `--no-headless` 禁用自动发现和回退;`--headless-help` 查看命令说明。 +- 不会在显示会话之间迁移应用,也不会把运行中的会话自动切换到另一块屏幕。物理会话已经失效时,另建 Xvfb 无法恢复其应用。 + +## 后台运行 + +创建独立 Xvfb 时,在前台保留一个管理进程,监控 Xvfb、桌面会话和 CrossDesk。退出程序、发送 SIGINT / SIGTERM / SIGHUP,或任一受监控进程退出时,会清理该次会话的子进程与临时认证文件。显示编号由 Xvfb 自动分配,避免占用已有显示;TCP 监听关闭,X11 连接使用随机认证 cookie,认证文件仅当前用户可读。 + +启动诊断日志会记录 `DISPLAY` 和 `XAUTHORITY` 路径;在同一用户的终端设置这两个变量后,可以将其他应用启动到虚拟桌面。认证文件会随会话退出被删除。 + +需要开机后保持服务,可创建用户级 systemd 单元 `~/.config/systemd/user/crossdesk-headless.service`,并将 `ExecStart` 改为实际程序路径: + +```ini +[Unit] +Description=CrossDesk virtual desktop + +[Service] +Type=simple +ExecStart=/usr/bin/crossdesk --headless-size 1920x1080 --headless-session startxfce4 +Restart=on-failure +RestartSec=5 +KillMode=mixed +TimeoutStopSec=15 + +[Install] +WantedBy=default.target +``` + +```bash +systemctl --user daemon-reload +systemctl --user enable --now crossdesk-headless.service +journalctl --user -u crossdesk-headless.service -f +``` + +需要复用原桌面的服务应将 `ExecStart` 改为 `/usr/bin/crossdesk`(自动发现和选择;确保已有图形会话登录)。如需在用户未登录时运行用户服务,管理员需为该用户启用 linger:`sudo loginctl enable-linger 用户名`。独立 Xvfb 模式下内置 daemon 选项不会生效;systemd 负责重启整个虚拟桌面。停止该模式会结束虚拟桌面中的应用,请先保存工作。 + +## 运行限制 + +虚拟显示的 GUI 使用软件渲染,画面走 X11 → NV12 采集。视频编码仍遵循 CrossDesk 设置和本机编码器能力。声音依赖当前用户可用的音频服务;Xvfb 本身不提供虚拟声卡。此实现针对 Linux;Windows 无显示器支持见 [常见问题](FAQ.md)。 diff --git a/docs/HEADLESS_EN.md b/docs/HEADLESS_EN.md new file mode 100644 index 00000000..248c898c --- /dev/null +++ b/docs/HEADLESS_EN.md @@ -0,0 +1,152 @@ +# Linux headless operation + +[README](../README_EN.md) · [中文](HEADLESS.md) + +CrossDesk can reuse an existing X11 desktop or create a separate Xvfb desktop on a Linux host without a monitor. Capture, input, cursor and clipboard use the same display. + +## Preserve the existing desktop and applications + +Start CrossDesk normally. It automatically discovers and selects your existing desktop, preserving its Dock, panel, wallpaper and open applications. No display number or `DISPLAY` configuration is required: + +```bash +crossdesk +# Source build: +xmake r crossdesk +``` + +With multiple desktops, selection prefers the startup environment's desktop, then the current login session, the active primary desktop, other active desktops, and online desktops. Equal-priority desktops use a stable display order, also used when systemd/logind metadata is unavailable. Inaccessible sessions and bare Xvfb displays without a window manager are excluded from existing-desktop selection. + +Reusing a desktop does not require Xvfb. Exiting CrossDesk does not terminate that desktop or its applications. A locked session initially shows the system lock screen; unlock it with that user's system password. + +Discovery combines the current user's login-session metadata with their X11 sockets and verifies a live window manager. It works without systemd/logind as well. It respects `XAUTHORITY` or Xlib's default location when unset, and can try the user's GDM authority file. Other users' desktops and login greeters are not automatically selected. + +`--headless` explicitly requests automatic selection, for example when the shell has stale Wayland environment variables. The display override is retained only for advanced use and debugging: + +```bash +crossdesk --headless-display :10 # Example display, not needed for normal startup. +``` + +The override fails if the specified desktop is inaccessible or has no window manager; it never substitutes a blank Xvfb. + +Do not add `--headless-size` or `--headless-session` to reuse an existing desktop: those options create a separate Xvfb. The existing display server controls its resolution. + +## Terminal console + +Headless mode keeps routine logs in files and displays the device ID, connection password, online status and command results in the terminal. SSH launches without a graphical environment enable this automatically; to request it explicitly: + +```bash +xmake r crossdesk --headless +``` + +Enter commands in that terminal while CrossDesk is running: + +| Command | Action | +| --- | --- | +| `status` | Show the device ID, current password and connection state | +| `settings` | Open the numbered settings menu; `back` returns | +| `settings show` | Show settings without entering the menu | +| `settings set KEY VALUE` | Change a setting directly | +| `password` | Prompt for a new password; six ASCII letters or digits, or `cancel` | +| `password Ab12Cd` | Submit a password directly (example only) | +| `random` | Generate and submit a random password | +| `help` | Print command help | +| `quit` | Exit CrossDesk, preserving a reused desktop | + +The menu shares the GUI's `config.ini`. Each valid change is saved immediately; `cancel` discards the value being edited. Console status, menus, password messages and help follow the saved Chinese, English or Russian language setting. Command names remain the same. + +| Key | Values / format | When applied | +| --- | --- | --- | +| `language` | `zh-CN` / `en-US` / `ru-RU` | Immediately, including the GUI; retained on restart | +| `codec` | `h264` / `av1` | Reconnect after saving | +| `hardware` | `on` / `off` | Reconnect; enabling requires a build with hardware codec support | +| `turn` | `off` / `auto` / `udp` / `tcp` | Reconnect; UDP/TCP force relay use | +| `server_host` | Hostname or IP without URL scheme/path | Reconnect if self-hosting is enabled; otherwise saved for later | +| `server_port` | `1`–`65535` | Same as server host | +| `self_hosted` | `on` / `off` | Configure host/port first, then enable; reconnect | +| `privacy` | `on` / `off` | Automatic privacy screen on subsequent connections | +| `file_path` | Existing writable absolute directory, or `default` | Subsequent received files; spaces need no extra quotes | +| `autostart` | `on` / `off` | Next graphical desktop login, not a system boot service | +| `daemon` | `on` / `off` | Next normal-mode start; the headless console remains in the foreground | + +Example commands inside the running console: + +```text +settings +1 +en-US +back +settings set codec av1 +settings set file_path /home/user/Received Files +``` + +Connection settings cannot change while a remote session is connected/connecting or a password change is unresolved. Switching signaling servers may change the device identity; use `status` after reconnection. Quality/frame rate are controlled by the viewer, and headless Linux capture uses X11; those options are not additional mutable global settings. + +Password changes require a signaling connection. The console reports submission, server confirmation and reconnection, using the GUI's existing persistence and timeout recovery flow. Invalid input, offline changes and pending requests produce concise messages. A password change reconnects the host. + +Startup prints the existing log location. Logs retain their original `crossdesk-YYYYMMDD-HHMMSS.log` and `minirtc-YYYYMMDD-HHMMSS.log` names and rotation policy, with no extra headless log file. Bootstrap diagnostics use the CrossDesk logger; terminal log copies and direct third-party stdout/stderr output are silenced. Credential display goes directly to the terminal, not through the logger. Noninteractive output (services or redirection) hides the plaintext password. End-of-input does not terminate a background service. + +The interactive console stays in the foreground regardless of the built-in daemon setting. Exit any older instance and start the new build to use these commands. + +## Create a separate virtual desktop + +Debian/Ubuntu packages require Xvfb, Xfce and the D-Bus session tools through `Depends`. APT installs them even with `--no-install-recommends`. Source builds need the same runtime prerequisites in the development environment; the project build image and CI include them. + +For a host without a desktop session, or to start an independent Xfce session (package users do not need to reinstall the dependencies): + +```bash +sudo apt install xvfb xfce4 dbus +crossdesk --headless-size 1920x1080 --headless-session startxfce4 +``` + +The default size is 1920×1080; dimensions must be even, between 320 and 8192. `--headless-session` accepts an executable name or path and runs it under a private `dbus-run-session`. Use an executable script ending in `exec` if arguments are required. + +For capture debugging, explicitly request a bare display: + +```bash +sudo apt install xvfb +crossdesk --headless-session none +``` + +Bare Xvfb only provides a screen. It has no Dock, panel or existing application windows. Specifying only `--headless-size` also creates a bare display and records a diagnostic in the log. This explains a screen that shows CrossDesk against a black background. + +## Selection and limits + +- Ordinary graphical launches retain the supplied X11 or Wayland session. X11 root capture works without active RandR outputs or a RandR extension. +- `--headless`, or startup without an accessible X11 display or advertised Wayland session, automatically selects an existing desktop before falling back to Xvfb. Bare Xvfb fallback explicitly reports that it has no desktop environment or existing apps. +- `--headless-display` cannot be combined with `--headless-size` or `--headless-session`. +- `--no-headless` disables automatic discovery/fallback; `--headless-help` prints usage. +- Applications are not migrated between sessions. Creating Xvfb cannot recover applications from an unavailable graphical session. + +## Run as a service + +When creating a private display, the foreground supervisor owns Xvfb, the optional desktop session and CrossDesk. Application exit, SIGINT/SIGTERM/SIGHUP, or a monitored process exiting tears down that session's child processes and temporary authorization file. Xvfb allocates an unused display number, disables TCP listening and requires a random authentication cookie. The authority file is private to the current user. Startup diagnostic logs record `DISPLAY` and `XAUTHORITY`; use them from a terminal as the same user to launch additional applications on the virtual desktop. + +Example `~/.config/systemd/user/crossdesk-headless.service` (adjust the executable path): + +```ini +[Unit] +Description=CrossDesk virtual desktop + +[Service] +Type=simple +ExecStart=/usr/bin/crossdesk --headless-size 1920x1080 --headless-session startxfce4 +Restart=on-failure +RestartSec=5 +KillMode=mixed +TimeoutStopSec=15 + +[Install] +WantedBy=default.target +``` + +```bash +systemctl --user daemon-reload +systemctl --user enable --now crossdesk-headless.service +journalctl --user -u crossdesk-headless.service -f +``` + +To reuse an existing desktop instead, set `ExecStart=/usr/bin/crossdesk`; discovery and selection are automatic, but a graphical session must already be logged in. An administrator can enable a user service before login with `sudo loginctl enable-linger USERNAME`. The built-in daemon setting is bypassed when creating a private Xvfb; systemd can restart the whole session. Save work before stopping the service, which also closes applications on the virtual desktop. + +## Runtime limitations + +The GUI uses software rendering and capture uses X11 → NV12. Video encoding follows the configured codec and available encoders. Audio requires an available user audio service; Xvfb does not provide a sound device. This implementation targets Linux. For Windows hosts without monitors, see the [FAQ](FAQ.md).