From 1266f0b6f3047133422a185edc6ade3f8524ad73 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 22 Oct 2024 02:39:07 +0100 Subject: [PATCH 1/6] move to sdbus-cpp2 --- CMakeLists.txt | 2 +- src/core/Hypridle.cpp | 75 ++++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47333c2..3d60b42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ pkg_check_modules( wayland-protocols hyprlang>=0.4.0 hyprutils>=0.2.0 - sdbus-c++) + sdbus-c++>=0.2.0) file(GLOB_RECURSE SRCFILES CONFIGURE_DEPENDS "src/*.cpp") add_executable(hypridle ${SRCFILES}) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index fc2f6c5..8616873 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -153,7 +153,7 @@ void CHypridle::enterEventLoop() { if (pollfds[0].revents & POLLIN /* dbus */) { Debug::log(TRACE, "got dbus event"); - while (m_sDBUSState.connection->processPendingRequest()) { + while (m_sDBUSState.connection->processPendingEvent()) { ; } } @@ -171,7 +171,7 @@ void CHypridle::enterEventLoop() { if (pollfdsCount > 2 && pollfds[2].revents & POLLIN /* dbus2 */) { Debug::log(TRACE, "got dbus event"); - while (m_sDBUSState.screenSaverServiceConnection->processPendingRequest()) { + while (m_sDBUSState.screenSaverServiceConnection->processPendingEvent()) { ; } } @@ -356,14 +356,14 @@ bool CHypridle::unregisterDbusInhibitCookies(const std::string& ownerID) { return true; } -void handleDbusLogin(sdbus::Message& msg) { +static void handleDbusLogin(sdbus::Message msg) { // lock & unlock static auto* const PLOCKCMD = (Hyprlang::STRING const*)g_pConfigManager->getValuePtr("general:lock_cmd"); static auto* const PUNLOCKCMD = (Hyprlang::STRING const*)g_pConfigManager->getValuePtr("general:unlock_cmd"); Debug::log(LOG, "Got dbus .Session"); - const auto MEMBER = msg.getMemberName(); + const std::string MEMBER = msg.getMemberName(); if (MEMBER == "Lock") { Debug::log(LOG, "Got Lock from dbus"); @@ -381,8 +381,8 @@ void handleDbusLogin(sdbus::Message& msg) { } } -void handleDbusSleep(sdbus::Message& msg) { - const auto MEMBER = msg.getMemberName(); +static void handleDbusSleep(sdbus::Message msg) { + const std::string MEMBER = msg.getMemberName(); if (MEMBER != "PrepareForSleep") return; @@ -421,7 +421,7 @@ void handleDbusBlockInhibits(const std::string& inhibits) { } } -void handleDbusBlockInhibitsPropertyChanged(sdbus::Message& msg) { +static void handleDbusBlockInhibitsPropertyChanged(sdbus::Message msg) { std::string interface; std::map changedProperties; msg >> interface >> changedProperties; @@ -430,16 +430,10 @@ void handleDbusBlockInhibitsPropertyChanged(sdbus::Message& msg) { } } -void handleDbusScreensaver(sdbus::MethodCall call, bool inhibit) { - std::string app = "?", reason = "?"; - std::string ownerID = call.getSender(); +static int handleDbusScreensaver(std::string app, std::string reason, uint32_t cookie, bool inhibit, const char* sender) { + std::string ownerID = sender; - if (inhibit) { - call >> app; - call >> reason; - } else { - uint32_t cookie = 0; - call >> cookie; + if (!inhibit) { Debug::log(TRACE, "Read uninhibit cookie: {}", cookie); const auto COOKIE = g_pHypridle->getDbusInhibitCookie(cookie); if (COOKIE.cookie == 0) { @@ -449,9 +443,8 @@ void handleDbusScreensaver(sdbus::MethodCall call, bool inhibit) { reason = COOKIE.reason; ownerID = COOKIE.ownerID; - if (!g_pHypridle->unregisterDbusInhibitCookie(COOKIE)) { + if (!g_pHypridle->unregisterDbusInhibitCookie(COOKIE)) Debug::log(WARN, "BUG THIS: attempted to unregister unknown cookie"); - }; } } @@ -467,21 +460,17 @@ void handleDbusScreensaver(sdbus::MethodCall call, bool inhibit) { if (inhibit) { auto cookie = CHypridle::SDbusInhibitCookie{uint32_t{cookieID}, app, reason, ownerID}; - auto reply = call.createReply(); - reply << uint32_t{cookieID++}; - reply.send(); - - Debug::log(LOG, "Cookie {} sent", cookieID - 1); + Debug::log(LOG, "Cookie {} sent", cookieID); g_pHypridle->registerDbusInhibitCookie(cookie); - } else { - auto reply = call.createReply(); - reply.send(); - Debug::log(TRACE, "Uninhibit response sent"); + + return cookieID++; } + + return 0; } -void handleDbusNameOwnerChanged(sdbus::Message& msg) { +static void handleDbusNameOwnerChanged(sdbus::Message msg) { std::string name, oldOwner, newOwner; msg >> name >> oldOwner >> newOwner; @@ -498,8 +487,8 @@ void CHypridle::setupDBUS() { static auto const IGNORE_DBUS_INHIBIT = **(Hyprlang::INT* const*)g_pConfigManager->getValuePtr("general:ignore_dbus_inhibit"); static auto const IGNORE_SYSTEMD_INHIBIT = **(Hyprlang::INT* const*)g_pConfigManager->getValuePtr("general:ignore_systemd_inhibit"); - auto proxy = sdbus::createProxy("org.freedesktop.login1", "/org/freedesktop/login1"); - auto method = proxy->createMethodCall("org.freedesktop.login1.Manager", "GetSession"); + auto proxy = sdbus::createProxy(sdbus::ServiceName{"org.freedesktop.login1"}, sdbus::ObjectPath{"/org/freedesktop/login1"}); + auto method = proxy->createMethodCall(sdbus::InterfaceName{"org.freedesktop.login1.Manager"}, sdbus::MethodName{"GetSession"}); method << "auto"; sdbus::ObjectPath path; @@ -507,18 +496,17 @@ void CHypridle::setupDBUS() { auto reply = proxy->callMethod(method); reply >> path; - m_sDBUSState.connection->addMatch("type='signal',path='" + path + "',interface='org.freedesktop.login1.Session'", handleDbusLogin, sdbus::floating_slot_t{}); - m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.login1.Manager'", handleDbusSleep, sdbus::floating_slot_t{}); + m_sDBUSState.connection->addMatch("type='signal',path='" + path + "',interface='org.freedesktop.login1.Session'", ::handleDbusLogin); + m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.login1.Manager'", ::handleDbusSleep); } catch (std::exception& e) { Debug::log(WARN, "Couldn't connect to logind service ({})", e.what()); } Debug::log(LOG, "Using dbus path {}", path.c_str()); if (!IGNORE_SYSTEMD_INHIBIT) { - m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.DBus.Properties'", handleDbusBlockInhibitsPropertyChanged, - sdbus::floating_slot_t{}); + m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.DBus.Properties'", ::handleDbusBlockInhibitsPropertyChanged); try { - std::string value = proxy->getProperty("BlockInhibited").onInterface("org.freedesktop.login1.Manager"); + std::string value = (proxy->getProperty("BlockInhibited").onInterface("org.freedesktop.login1.Manager")).get(); handleDbusBlockInhibits(value); } catch (std::exception& e) { Debug::log(WARN, "Couldn't retrieve current systemd inhibits ({})", e.what()); } } @@ -531,21 +519,26 @@ void CHypridle::setupDBUS() { }; try { - m_sDBUSState.screenSaverServiceConnection = sdbus::createSessionBusConnection("org.freedesktop.ScreenSaver"); + m_sDBUSState.screenSaverServiceConnection = sdbus::createSessionBusConnection(sdbus::ServiceName{"org.freedesktop.ScreenSaver"}); for (const std::string& path : paths) { try { - auto obj = sdbus::createObject(*m_sDBUSState.screenSaverServiceConnection, path); - obj->registerMethod("org.freedesktop.ScreenSaver", "Inhibit", "ss", "u", [&](sdbus::MethodCall c) { handleDbusScreensaver(c, true); }); - obj->registerMethod("org.freedesktop.ScreenSaver", "UnInhibit", "u", "", [&](sdbus::MethodCall c) { handleDbusScreensaver(c, false); }); - obj->finishRegistration(); + auto obj = sdbus::createObject(*m_sDBUSState.screenSaverServiceConnection, sdbus::ObjectPath{path}); + + obj->addVTable(sdbus::registerMethod("Inhibit").implementedAs([object = obj.get()](std::string s1, std::string s2) { + return handleDbusScreensaver(s1, s2, 0, true, object->getCurrentlyProcessedMessage().getSender()); + }), + sdbus::registerMethod("UnInhibit").implementedAs([object = obj.get()](uint32_t c) { + handleDbusScreensaver("", "", c, true, object->getCurrentlyProcessedMessage().getSender()); + })) + .forInterface(sdbus::InterfaceName{"org.freedesktop.ScreenSaver"}); m_sDBUSState.screenSaverObjects.push_back(std::move(obj)); } catch (std::exception& e) { Debug::log(ERR, "Failed registering for {}, perhaps taken?\nerr: {}", path, e.what()); } } m_sDBUSState.screenSaverServiceConnection->addMatch("type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'", - handleDbusNameOwnerChanged, sdbus::floating_slot_t{}); + ::handleDbusNameOwnerChanged); } catch (std::exception& e) { Debug::log(ERR, "Couldn't connect to session dbus\nerr: {}", e.what()); } } } From 36852ab1bddc08f6531eb3ccbc0aa69409a25f3f Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 22 Oct 2024 02:47:49 +0100 Subject: [PATCH 2/6] nice --- src/core/Hypridle.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index 8616873..e06287c 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -487,14 +487,11 @@ void CHypridle::setupDBUS() { static auto const IGNORE_DBUS_INHIBIT = **(Hyprlang::INT* const*)g_pConfigManager->getValuePtr("general:ignore_dbus_inhibit"); static auto const IGNORE_SYSTEMD_INHIBIT = **(Hyprlang::INT* const*)g_pConfigManager->getValuePtr("general:ignore_systemd_inhibit"); - auto proxy = sdbus::createProxy(sdbus::ServiceName{"org.freedesktop.login1"}, sdbus::ObjectPath{"/org/freedesktop/login1"}); - auto method = proxy->createMethodCall(sdbus::InterfaceName{"org.freedesktop.login1.Manager"}, sdbus::MethodName{"GetSession"}); - method << "auto"; + auto proxy = sdbus::createProxy(sdbus::ServiceName{"org.freedesktop.login1"}, sdbus::ObjectPath{"/org/freedesktop/login1"}); sdbus::ObjectPath path; try { - auto reply = proxy->callMethod(method); - reply >> path; + proxy->callMethod("GetSession").onInterface("org.freedesktop.login1.Manager").storeResultsTo(path); m_sDBUSState.connection->addMatch("type='signal',path='" + path + "',interface='org.freedesktop.login1.Session'", ::handleDbusLogin); m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.login1.Manager'", ::handleDbusSleep); From e27b45cebe4e508da980e2c7463939d6e6a31494 Mon Sep 17 00:00:00 2001 From: Mihai Fufezan Date: Tue, 22 Oct 2024 08:14:35 +0300 Subject: [PATCH 3/6] nix/overlays: add sdbus overlay --- nix/overlays.nix | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nix/overlays.nix b/nix/overlays.nix index 335c632..c9cccd7 100644 --- a/nix/overlays.nix +++ b/nix/overlays.nix @@ -15,6 +15,7 @@ in { hypridle = lib.composeManyExtensions [ inputs.hyprlang.overlays.default inputs.hyprutils.overlays.default + inputs.self.overlays.sdbuscpp (final: prev: { hypridle = prev.callPackage ./default.nix { stdenv = prev.gcc13Stdenv; @@ -23,4 +24,17 @@ in { }; }) ]; + + sdbuscpp = final: prev: { + sdbus-cpp = prev.sdbus-cpp.overrideAttrs (self: super: { + version = "2.0.0"; + + src = final.fetchFromGitHub { + owner = "Kistler-group"; + repo = "sdbus-cpp"; + rev = "refs/tags/v${self.version}"; + hash = "sha256-W8V5FRhV3jtERMFrZ4gf30OpIQLYoj2yYGpnYOmH2+g="; + }; + }); + }; } From 87b13086c33c7410aed414d8a0923dcdde0c7b9a Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 22 Oct 2024 12:23:08 +0100 Subject: [PATCH 4/6] e --- src/core/Hypridle.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index e06287c..e6c01e0 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -487,16 +487,19 @@ void CHypridle::setupDBUS() { static auto const IGNORE_DBUS_INHIBIT = **(Hyprlang::INT* const*)g_pConfigManager->getValuePtr("general:ignore_dbus_inhibit"); static auto const IGNORE_SYSTEMD_INHIBIT = **(Hyprlang::INT* const*)g_pConfigManager->getValuePtr("general:ignore_systemd_inhibit"); - auto proxy = sdbus::createProxy(sdbus::ServiceName{"org.freedesktop.login1"}, sdbus::ObjectPath{"/org/freedesktop/login1"}); + auto systemConnection = sdbus::createSystemBusConnection(); + auto proxy = sdbus::createProxy(*systemConnection, sdbus::ServiceName{"org.freedesktop.login1"}, sdbus::ObjectPath{"/org/freedesktop/login1"}); sdbus::ObjectPath path; try { - proxy->callMethod("GetSession").onInterface("org.freedesktop.login1.Manager").storeResultsTo(path); + proxy->callMethod("GetSession").onInterface("org.freedesktop.login1.Manager").withArguments(std::string{"auto"}).storeResultsTo(path); m_sDBUSState.connection->addMatch("type='signal',path='" + path + "',interface='org.freedesktop.login1.Session'", ::handleDbusLogin); m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.login1.Manager'", ::handleDbusSleep); } catch (std::exception& e) { Debug::log(WARN, "Couldn't connect to logind service ({})", e.what()); } + systemConnection.reset(); + Debug::log(LOG, "Using dbus path {}", path.c_str()); if (!IGNORE_SYSTEMD_INHIBIT) { From 7c65ecff0a8d075e2bbda26c5029665e8895384d Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 22 Oct 2024 18:46:00 +0100 Subject: [PATCH 5/6] e --- src/core/Hypridle.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index e6c01e0..8f99918 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -498,8 +498,6 @@ void CHypridle::setupDBUS() { m_sDBUSState.connection->addMatch("type='signal',path='/org/freedesktop/login1',interface='org.freedesktop.login1.Manager'", ::handleDbusSleep); } catch (std::exception& e) { Debug::log(WARN, "Couldn't connect to logind service ({})", e.what()); } - systemConnection.reset(); - Debug::log(LOG, "Using dbus path {}", path.c_str()); if (!IGNORE_SYSTEMD_INHIBIT) { @@ -541,4 +539,6 @@ void CHypridle::setupDBUS() { ::handleDbusNameOwnerChanged); } catch (std::exception& e) { Debug::log(ERR, "Couldn't connect to session dbus\nerr: {}", e.what()); } } + + systemConnection.reset(); } From 7aee569cbfed2209b153c800091685af0a6aca2b Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:45:01 +0100 Subject: [PATCH 6/6] XD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> --- src/core/Hypridle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index 8f99918..4de0a42 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -527,7 +527,7 @@ void CHypridle::setupDBUS() { return handleDbusScreensaver(s1, s2, 0, true, object->getCurrentlyProcessedMessage().getSender()); }), sdbus::registerMethod("UnInhibit").implementedAs([object = obj.get()](uint32_t c) { - handleDbusScreensaver("", "", c, true, object->getCurrentlyProcessedMessage().getSender()); + handleDbusScreensaver("", "", c, false, object->getCurrentlyProcessedMessage().getSender()); })) .forInterface(sdbus::InterfaceName{"org.freedesktop.ScreenSaver"});