-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DBusManager to centralize dbus connection
- Loading branch information
Showing
6 changed files
with
137 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#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_connection = 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_loginProxy = sdbus::createProxy(*m_connection, destination, loginPath); | ||
m_sessionProxy = sdbus::createProxy(*m_connection, 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() { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
return m_connection; | ||
} | ||
|
||
std::shared_ptr<sdbus::IProxy> DBusManager::getLoginProxy() { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
if (!m_loginProxy) { | ||
initializeConnection(); | ||
} | ||
return m_loginProxy; | ||
} | ||
|
||
std::shared_ptr<sdbus::IProxy> DBusManager::getSessionProxy() { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
if (!m_sessionProxy) { | ||
initializeConnection(); | ||
} | ||
return m_sessionProxy; | ||
} | ||
|
||
void DBusManager::setLockedHint(bool locked) { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
if (!m_sessionProxy) { | ||
Debug::log(WARN, "[DBusManager] Cannot set locked hint: Proxy is not initialized."); | ||
return; | ||
} | ||
|
||
try { | ||
const sdbus::ServiceName interface{"org.freedesktop.login1.Session"}; | ||
m_sessionProxy->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() { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
if (!m_sessionProxy) { | ||
Debug::log(WARN, "[DBusManager] Unlock signal skipped: Proxy is not initialized."); | ||
return; | ||
} | ||
|
||
try { | ||
const sdbus::ServiceName interface{"org.freedesktop.login1.Session"}; | ||
m_sessionProxy->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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <mutex> | ||
#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_connection; | ||
std::shared_ptr<sdbus::IProxy> m_loginProxy; | ||
std::shared_ptr<sdbus::IProxy> m_sessionProxy; | ||
|
||
std::mutex m_mutex; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters