[feat] expose system controls and verify remote unlock completion

This commit is contained in:
dijunkun
2026-11-06 20:46:11 +08:00
parent 6bfcd5cf3d
commit dd4baf2fc4
16 changed files with 222 additions and 15 deletions
@@ -7,11 +7,54 @@
#include "screen_lock.h"
#include <dbus/dbus.h>
#include <unistd.h>
#include "rd_log.h"
namespace crossdesk {
ScreenLockState QueryScreenLock() {
dbus_threads_init_default();
DBusConnection* connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, nullptr);
if (!connection) return ScreenLockState::unknown;
dbus_connection_set_exit_on_disconnect(connection, false);
ScreenLockState state = ScreenLockState::unknown;
DBusMessage* query = dbus_message_new_method_call("org.freedesktop.login1",
"/org/freedesktop/login1", "org.freedesktop.login1.Manager", "GetSessionByPID");
if (query) {
dbus_uint32_t pid = getpid();
dbus_message_append_args(query, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID);
DBusMessage* session = dbus_connection_send_with_reply_and_block(connection, query, 500, nullptr);
dbus_message_unref(query);
const char* path = nullptr;
if (session && dbus_message_get_args(session, nullptr, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
query = dbus_message_new_method_call("org.freedesktop.login1", path,
"org.freedesktop.DBus.Properties", "Get");
const char* interface = "org.freedesktop.login1.Session";
const char* property = "LockedHint";
if (query) {
dbus_message_append_args(query, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID);
DBusMessage* reply = dbus_connection_send_with_reply_and_block(connection, query, 500, nullptr);
dbus_message_unref(query);
DBusMessageIter outer, value;
if (reply && dbus_message_iter_init(reply, &outer) && dbus_message_iter_get_arg_type(&outer) == DBUS_TYPE_VARIANT) {
dbus_message_iter_recurse(&outer, &value);
if (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_BOOLEAN) {
dbus_bool_t locked = false;
dbus_message_iter_get_basic(&value, &locked);
state = locked ? ScreenLockState::locked : ScreenLockState::unlocked;
}
}
if (reply) dbus_message_unref(reply);
}
}
if (session) dbus_message_unref(session);
}
dbus_connection_close(connection);
dbus_connection_unref(connection);
return state;
}
bool LockScreen() {
dbus_threads_init_default();
DBusError error;
@@ -7,11 +7,23 @@
#include "screen_lock.h"
#include <dlfcn.h>
#include <ApplicationServices/ApplicationServices.h>
#include "rd_log.h"
namespace crossdesk {
ScreenLockState QueryScreenLock() {
const auto session = CGSessionCopyCurrentDictionary();
if (!session) return ScreenLockState::unknown;
const auto console = CFDictionaryGetValue(session, kCGSessionOnConsoleKey);
const auto locked = CFDictionaryGetValue(session, CFSTR("CGSSessionScreenIsLocked"));
const auto state = locked == kCFBooleanTrue ? ScreenLockState::locked
: console == kCFBooleanTrue ? ScreenLockState::unlocked : ScreenLockState::unknown;
CFRelease(session);
return state;
}
bool LockScreen() {
// macOS has no public immediate-lock API. Resolve login.framework at
// runtime so an OS without this private entry point fails gracefully.
@@ -5,6 +5,7 @@
*/
#include "screen_lock.h"
#include "service/interactive_desktop.h"
#include <windows.h>
@@ -12,6 +13,13 @@
namespace crossdesk {
ScreenLockState QueryScreenLock() {
std::string reason;
if (IsCurrentSessionUnlocked(&reason)) return ScreenLockState::unlocked;
return reason.rfind("session_not_unlocked", 0) == 0 ? ScreenLockState::locked
: ScreenLockState::unknown;
}
bool LockScreen() {
if (LockWorkStation()) return true;
LOG_WARN("LockWorkStation failed: {}", GetLastError());