From 1071b9f7c5ca6217173a10bb13ab5b8cbddfd4de Mon Sep 17 00:00:00 2001 From: Louis des Landes Date: Sun, 10 Apr 2022 22:47:52 +0930 Subject: [PATCH] Add a config to set a timeout for the idle_inhibitor module --- include/modules/idle_inhibitor.hpp | 2 ++ man/waybar-idle-inhibitor.5.scd | 7 ++++++- src/modules/idle_inhibitor.cpp | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/modules/idle_inhibitor.hpp b/include/modules/idle_inhibitor.hpp index c7ee6800..334c8e20 100644 --- a/include/modules/idle_inhibitor.hpp +++ b/include/modules/idle_inhibitor.hpp @@ -9,6 +9,8 @@ namespace waybar::modules { class IdleInhibitor : public ALabel { + sigc::connection timeout_; + public: IdleInhibitor(const std::string&, const waybar::Bar&, const Json::Value&); ~IdleInhibitor(); diff --git a/man/waybar-idle-inhibitor.5.scd b/man/waybar-idle-inhibitor.5.scd index 0b0bdd03..9e8976b7 100644 --- a/man/waybar-idle-inhibitor.5.scd +++ b/man/waybar-idle-inhibitor.5.scd @@ -63,6 +63,10 @@ screensaving, also known as "presentation mode". typeof: double ++ Threshold to be used when scrolling. +*timeout*: ++ + typeof: double ++ + The number of minutes the inhibit should last. + *tooltip*: ++ typeof: bool ++ default: true ++ @@ -82,6 +86,7 @@ screensaving, also known as "presentation mode". "format-icons": { "activated": "", "deactivated": "" - } + }, + "timeout": 30.5 } ``` diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index cf79565e..fed64b27 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -72,6 +72,29 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { if (e->button == 1) { status = !status; + if (timeout_.connected()) { + /* cancel any already active timeout handler */ + timeout_.disconnect(); + } + + if (status && config_["timeout"].isNumeric()) { + auto timeoutMins = config_["timeout"].asDouble(); + int timeoutSecs = timeoutMins * 60; + + timeout_ = Glib::signal_timeout().connect_seconds([]() { + /* intentionally not tied to a module instance lifetime + * as the output with `this` can be disconnected + */ + spdlog::info("deactivating idle_inhibitor by timeout"); + status = false; + for (auto const& module : waybar::modules::IdleInhibitor::modules) { + module->update(); + } + /* disconnect */ + return false; + }, timeoutSecs); + } + // Make all other idle inhibitor modules update for (auto const& module : waybar::modules::IdleInhibitor::modules) { if (module != this) {