Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add D-Bus signals to notify session lock and unlock states #609

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/auth/Fingerprint.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Fingerprint.hpp"
#include "../core/hyprlock.hpp"
#include "../core/DBusManager.hpp"
#include "../helpers/Log.hpp"
#include "../config/ConfigManager.hpp"

Expand Down Expand Up @@ -48,8 +49,10 @@ CFingerprint::~CFingerprint() {
}

void CFingerprint::init() {
m_sDBUSState.connection = sdbus::createSystemBusConnection();
m_sDBUSState.login = sdbus::createProxy(*m_sDBUSState.connection, sdbus::ServiceName{"org.freedesktop.login1"}, sdbus::ObjectPath{"/org/freedesktop/login1"});
auto& dbusManager = DBusManager::getInstance();
m_sDBUSState.connection = dbusManager.getConnection();
m_sDBUSState.login = dbusManager.getLoginProxy();

m_sDBUSState.login->getPropertyAsync("PreparingForSleep").onInterface(LOGIN_MANAGER).uponReplyInvoke([this](std::optional<sdbus::Error> e, sdbus::Variant preparingForSleep) {
if (e) {
Debug::log(WARN, "fprint: Failed getting value for PreparingForSleep: {}", e->what());
Expand All @@ -62,6 +65,7 @@ void CFingerprint::init() {
inhibitSleep();
startVerify();
});

m_sDBUSState.login->uponSignal("PrepareForSleep").onInterface(LOGIN_MANAGER).call([this](bool start) {
Debug::log(LOG, "fprint: PrepareForSleep (start: {})", start);
if (start) {
Expand Down Expand Up @@ -97,10 +101,6 @@ void CFingerprint::terminate() {
releaseDevice();
}

std::shared_ptr<sdbus::IConnection> CFingerprint::getConnection() {
return m_sDBUSState.connection;
}

void CFingerprint::inhibitSleep() {
m_sDBUSState.login->callMethodAsync("Inhibit")
.onInterface(LOGIN_MANAGER)
Expand Down
4 changes: 1 addition & 3 deletions src/auth/Fingerprint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ class CFingerprint : public IAuthImplementation {
virtual std::optional<std::string> getLastPrompt();
virtual void terminate();

std::shared_ptr<sdbus::IConnection> getConnection();

private:
struct SDBUSState {
std::string message = "";

std::shared_ptr<sdbus::IConnection> connection;
std::unique_ptr<sdbus::IProxy> login;
std::shared_ptr<sdbus::IProxy> login;
std::unique_ptr<sdbus::IProxy> device;
sdbus::UnixFd inhibitLock;

Expand Down
83 changes: 83 additions & 0 deletions src/core/DBusManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "DBusManager.hpp"
#include "../helpers/Log.hpp"

DBusManager& DBusManager::getInstance() {
static DBusManager instance;
return instance;
}

DBusManager::DBusManager() {
initializeConnection();
}

DBusManager::~DBusManager() {
// Resources are automatically cleaned up.
}

void DBusManager::initializeConnection() {
try {
m_pConnection = sdbus::createSystemBusConnection();

const sdbus::ServiceName destination{"org.freedesktop.login1"};
const sdbus::ObjectPath loginPath{"/org/freedesktop/login1"};
const sdbus::ObjectPath sessionPath{"/org/freedesktop/login1/session/auto"};

m_pLoginProxy = sdbus::createProxy(*m_pConnection, destination, loginPath);
m_pSessionProxy = sdbus::createProxy(*m_pConnection, destination, sessionPath);

Debug::log(LOG, "[DBusManager] Initialized D-Bus connection. Service: {}. Login path: {}, Session path: {}",
std::string(destination), std::string(loginPath), std::string(sessionPath));
} catch (const sdbus::Error& e) {
Debug::log(ERR, "[DBusManager] D-Bus connection initialization failed: {}", e.what());
}
}

std::shared_ptr<sdbus::IConnection> DBusManager::getConnection() {
return m_pConnection;
}

std::shared_ptr<sdbus::IProxy> DBusManager::getLoginProxy() {
if (!m_pLoginProxy)
initializeConnection();

return m_pLoginProxy;
}

std::shared_ptr<sdbus::IProxy> DBusManager::getSessionProxy() {
if (!m_pSessionProxy)
initializeConnection();

return m_pSessionProxy;
}

void DBusManager::setLockedHint(bool locked) {
if (!m_pSessionProxy) {
Debug::log(WARN, "[DBusManager] Cannot set locked hint: Proxy is not initialized.");
return;
}

try {
const sdbus::ServiceName interface{"org.freedesktop.login1.Session"};
m_pSessionProxy->callMethod("SetLockedHint").onInterface(interface).withArguments(locked);

Debug::log(LOG, "[DBusManager] Sent 'SetLockedHint({})' on {}", locked, std::string(interface));
} catch (const sdbus::Error& e) {
Debug::log(WARN, "[DBusManager] Failed to send 'SetLockedHint({})': {}", locked, e.what());
}
}

void DBusManager::sendUnlockSignal() {
if (!m_pSessionProxy) {
Debug::log(WARN, "[DBusManager] Unlock signal skipped: Proxy is not initialized.");
return;
}

try {
const sdbus::ServiceName interface{"org.freedesktop.login1.Session"};
m_pSessionProxy->callMethod("Unlock").onInterface(interface);

Debug::log(LOG, "[DBusManager] Sent 'Unlock' on {}", std::string(interface));
} catch (const sdbus::Error& e) {
Debug::log(WARN, "[DBusManager] Unlock signal failed: {}", e.what());
}
}
27 changes: 27 additions & 0 deletions src/core/DBusManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <memory>
#include <string>
#include <sdbus-c++/sdbus-c++.h>

class DBusManager {
public:
static DBusManager& getInstance();

std::shared_ptr<sdbus::IConnection> getConnection();
std::shared_ptr<sdbus::IProxy> getLoginProxy();
std::shared_ptr<sdbus::IProxy> getSessionProxy();

void setLockedHint(bool locked);
void sendUnlockSignal();

private:
DBusManager();
~DBusManager();

void initializeConnection();

std::shared_ptr<sdbus::IConnection> m_pConnection;
std::shared_ptr<sdbus::IProxy> m_pLoginProxy;
std::shared_ptr<sdbus::IProxy> m_pSessionProxy;
};
15 changes: 13 additions & 2 deletions src/core/hyprlock.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hyprlock.hpp"
#include "DBusManager.hpp"
#include "../helpers/Log.hpp"
#include "../config/ConfigManager.hpp"
#include "../renderer/Renderer.hpp"
Expand All @@ -18,7 +19,6 @@
#include <filesystem>
#include <fstream>
#include <algorithm>
#include <sdbus-c++/sdbus-c++.h>
#include <hyprutils/os/Process.hpp>

using namespace Hyprutils::OS;
Expand Down Expand Up @@ -422,8 +422,14 @@ void CHyprlock::run() {
exit(1);
}

auto& dbusManager = DBusManager::getInstance();
const auto dbusConn = dbusManager.getConnection();
dbusManager.setLockedHint(true);

const auto fingerprintAuth = g_pAuth->getImpl(AUTH_IMPL_FINGERPRINT);
const auto dbusConn = (fingerprintAuth) ? ((CFingerprint*)fingerprintAuth.get())->getConnection() : nullptr;
if (fingerprintAuth){
fingerprintAuth->init();
}

registerSignalAction(SIGUSR1, handleUnlockSignal, SA_RESTART);
registerSignalAction(SIGUSR2, handleForceUpdateSignal);
Expand Down Expand Up @@ -995,6 +1001,11 @@ void CHyprlock::releaseSessionLock() {
ext_session_lock_v1_unlock_and_destroy(m_sLockState.lock);
m_sLockState.lock = nullptr;

// Notify unlock via D-Bus.
auto& dbusManager = DBusManager::getInstance();
dbusManager.sendUnlockSignal();
dbusManager.setLockedHint(false);

Debug::log(LOG, "Unlocked, exiting!");

m_bTerminate = true;
Expand Down
2 changes: 1 addition & 1 deletion src/core/hyprlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class CHyprlock {
std::mutex timerRequestMutex;
bool timerEvent = false;
} m_sLoopState;

std::vector<std::shared_ptr<CTimer>> m_vTimers;

std::vector<uint32_t> m_vPressedKeys;
Expand Down