[feat] add lock screen on disconnect option, refs #125

This commit is contained in:
dijunkun
2026-10-25 01:35:40 +08:00
parent 63bc5648c0
commit 0cbdbb0c96
17 changed files with 249 additions and 4 deletions
@@ -40,6 +40,7 @@ std::vector<ConsoleSetting> GetConsoleSettings(const ConfigCenter& config) {
{"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"},
{"lock_on_disconnect", "lock_on_disconnect", OnOff(config.IsLockOnDisconnect()), "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"},
@@ -120,6 +121,8 @@ SettingResult ApplyConsoleSetting(ConfigCenter& config, const std::string& key,
save = [&config, enabled] { return config.SetPrivacyScreen(enabled); };
effect = SettingEffect::next_connection;
message = "console_settings_next_connection";
} else if (key == "lock_on_disconnect") {
save = [&config, enabled] { return config.SetLockOnDisconnect(enabled); };
} else if (key == "autostart") {
save = [&config, enabled] { return config.SetAutostart(enabled); };
effect = SettingEffect::next_login;
@@ -0,0 +1,60 @@
/*
* @Author: DI JUNKUN
* @Date: 2026-10-25
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
*/
#include "screen_lock.h"
#include <dbus/dbus.h>
#include "rd_log.h"
namespace crossdesk {
bool LockScreen() {
dbus_threads_init_default();
DBusError error;
dbus_error_init(&error);
DBusConnection* connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
if (!connection) {
LOG_WARN("Cannot connect to screen lock service: {}",
error.message ? error.message : "session bus unavailable");
dbus_error_free(&error);
return false;
}
dbus_connection_set_exit_on_disconnect(connection, false);
// Address the current desktop's session bus, including headless desktops;
// never lock every logged-in user or an unrelated system login session.
struct Service { const char* name; const char* path; };
constexpr Service services[] = {
{"org.freedesktop.ScreenSaver", "/ScreenSaver"},
{"org.gnome.ScreenSaver", "/org/gnome/ScreenSaver"},
{"org.cinnamon.ScreenSaver", "/org/cinnamon/ScreenSaver"},
{"org.mate.ScreenSaver", "/org/mate/ScreenSaver"},
{"org.xfce.ScreenSaver", "/org/xfce/ScreenSaver"},
};
bool accepted = false;
for (const auto& service : services) {
DBusMessage* message = dbus_message_new_method_call(
service.name, service.path, service.name, "Lock");
if (!message) break;
dbus_message_set_auto_start(message, false);
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
connection, message, 1000, &error);
dbus_message_unref(message);
if (reply) {
accepted = dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN;
dbus_message_unref(reply);
}
dbus_error_free(&error);
if (accepted) break;
}
dbus_connection_close(connection);
dbus_connection_unref(connection);
if (!accepted) LOG_WARN("No desktop screen lock service accepted the request");
return accepted;
}
} // namespace crossdesk
@@ -0,0 +1,35 @@
/*
* @Author: DI JUNKUN
* @Date: 2026-10-25
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
*/
#include "screen_lock.h"
#include <dlfcn.h>
#include "rd_log.h"
namespace crossdesk {
bool LockScreen() {
// macOS has no public immediate-lock API. Resolve login.framework at
// runtime so an OS without this private entry point fails gracefully.
static void* framework = dlopen(
"/System/Library/PrivateFrameworks/login.framework/login",
RTLD_LAZY | RTLD_LOCAL);
using LockScreenFunction = int (*)();
static const auto lock = framework
? reinterpret_cast<LockScreenFunction>(
dlsym(framework, "SACLockScreenImmediate"))
: nullptr;
if (!lock) {
LOG_WARN("macOS screen lock API is unavailable");
return false;
}
const int result = lock();
if (result != 0) LOG_WARN("macOS screen lock request failed: {}", result);
return result == 0;
}
} // namespace crossdesk
@@ -45,6 +45,7 @@ int GuiApplication::RunUnattended(void* stop_event) {
if (CreateConnectionPeer() != 0) CloseConnectionPeer();
}
HandleConnectionStatusChange();
HandleScreenLock();
HandleWindowsServiceIntegration();
devices_.UpdateInteractions();
ShareLocalCursorState();
@@ -71,6 +72,7 @@ int GuiApplication::RunUnattended(void* stop_event) {
devices_.DestroyDevices();
devices_.DestroyFactories();
privacy_.Shutdown();
HandleScreenLock();
WriteUnattendedStatus(settings_.ActiveIdentity(), false, session);
SDL_Quit();
return 0;
@@ -0,0 +1,21 @@
/*
* @Author: DI JUNKUN
* @Date: 2026-10-25
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
*/
#include "screen_lock.h"
#include <windows.h>
#include "rd_log.h"
namespace crossdesk {
bool LockScreen() {
if (LockWorkStation()) return true;
LOG_WARN("LockWorkStation failed: {}", GetLastError());
return false;
}
} // namespace crossdesk