From f3d1f3b232a5e3267008568196397b03fab244d2 Mon Sep 17 00:00:00 2001 From: slowsage <84777606+slowsage@users.noreply.github.com> Date: Sat, 1 Nov 2025 16:00:41 -0400 Subject: [PATCH] core: fix dbus inhibit lock counting (#175) Fixes #74 (root cause) Fixes #111 --- src/core/Hypridle.cpp | 21 +++++++++------------ src/core/Hypridle.hpp | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index 13537c7..5edc32f 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -371,15 +371,8 @@ bool CHypridle::unregisterDbusInhibitCookie(const CHypridle::SDbusInhibitCookie& return true; } -bool CHypridle::unregisterDbusInhibitCookies(const std::string& ownerID) { - const auto IT = std::remove_if(m_sDBUSState.inhibitCookies.begin(), m_sDBUSState.inhibitCookies.end(), - [&ownerID](const CHypridle::SDbusInhibitCookie& item) { return item.ownerID == ownerID; }); - - if (IT == m_sDBUSState.inhibitCookies.end()) - return false; - - m_sDBUSState.inhibitCookies.erase(IT, m_sDBUSState.inhibitCookies.end()); - return true; +size_t CHypridle::unregisterDbusInhibitCookies(const std::string& ownerID) { + return std::erase_if(m_sDBUSState.inhibitCookies, [&ownerID](const CHypridle::SDbusInhibitCookie& item) { return item.ownerID == ownerID; }); } static void handleDbusLogin(sdbus::Message msg) { @@ -461,6 +454,7 @@ static void handleDbusBlockInhibitsPropertyChanged(sdbus::Message msg) { static uint32_t handleDbusScreensaver(std::string app, std::string reason, uint32_t cookie, bool inhibit, const char* sender) { std::string ownerID = sender; + bool cookieFound = false; if (!inhibit) { Debug::log(TRACE, "Read uninhibit cookie: {}", cookie); @@ -471,6 +465,7 @@ static uint32_t handleDbusScreensaver(std::string app, std::string reason, uint3 app = COOKIE.app; reason = COOKIE.reason; ownerID = COOKIE.ownerID; + cookieFound = true; if (!g_pHypridle->unregisterDbusInhibitCookie(COOKIE)) Debug::log(WARN, "BUG THIS: attempted to unregister unknown cookie"); @@ -481,7 +476,7 @@ static uint32_t handleDbusScreensaver(std::string app, std::string reason, uint3 if (inhibit) g_pHypridle->onInhibit(true); - else + else if (cookieFound) g_pHypridle->onInhibit(false); static uint32_t cookieID = 1337; @@ -506,9 +501,11 @@ static void handleDbusNameOwnerChanged(sdbus::Message msg) { if (!newOwner.empty()) return; - if (g_pHypridle->unregisterDbusInhibitCookies(oldOwner)) { + size_t removed = g_pHypridle->unregisterDbusInhibitCookies(oldOwner); + if (removed > 0) { Debug::log(LOG, "App with owner {} disconnected", oldOwner); - g_pHypridle->onInhibit(false); + for (size_t i = 0; i < removed; i++) + g_pHypridle->onInhibit(false); } } diff --git a/src/core/Hypridle.hpp b/src/core/Hypridle.hpp index 395b07c..11bab5d 100644 --- a/src/core/Hypridle.hpp +++ b/src/core/Hypridle.hpp @@ -44,7 +44,7 @@ class CHypridle { SDbusInhibitCookie getDbusInhibitCookie(uint32_t cookie); void registerDbusInhibitCookie(SDbusInhibitCookie& cookie); bool unregisterDbusInhibitCookie(const SDbusInhibitCookie& cookie); - bool unregisterDbusInhibitCookies(const std::string& ownerID); + size_t unregisterDbusInhibitCookies(const std::string& ownerID); void handleInhibitOnDbusSleep(bool toSleep); void inhibitSleep();