From 82a668ef6231a52e2fcf4046a8881bb3abc29b50 Mon Sep 17 00:00:00 2001 From: Marcin Fabrykowski Date: Thu, 4 Apr 2024 23:27:06 +0200 Subject: [PATCH] allow dynamic timeout setting Signed-off-by: Marcin Fabrykowski --- include/modules/idle_inhibitor.hpp | 9 +++- man/waybar-idle-inhibitor.5.scd | 37 ++++++++++++-- src/modules/idle_inhibitor.cpp | 80 +++++++++++++++++++++++++----- 3 files changed, 107 insertions(+), 19 deletions(-) diff --git a/include/modules/idle_inhibitor.hpp b/include/modules/idle_inhibitor.hpp index 22bd808fc..6fad67067 100644 --- a/include/modules/idle_inhibitor.hpp +++ b/include/modules/idle_inhibitor.hpp @@ -17,14 +17,21 @@ class IdleInhibitor : public ALabel { auto update() -> void override; static std::list modules; static bool status; + static long deactivationTime; private: bool handleToggle(GdkEventButton* const& e) override; - void toggleStatus(); + bool handleScroll(GdkEventScroll* e) override; + + void toggleStatus(int force_status = -1); const Bar& bar_; struct zwp_idle_inhibitor_v1* idle_inhibitor_; int pid_; + + bool dynamicTimeout; + short timeout; + short timeout_step; }; } // namespace waybar::modules diff --git a/man/waybar-idle-inhibitor.5.scd b/man/waybar-idle-inhibitor.5.scd index 71b3b30c6..b8a1270e1 100644 --- a/man/waybar-idle-inhibitor.5.scd +++ b/man/waybar-idle-inhibitor.5.scd @@ -41,15 +41,15 @@ screensaver, also known as "presentation mode". *on-click*: ++ typeof: string ++ - Command to execute when clicked on the module. A click also toggles the state + Command to execute when clicked on the module. A click also toggles the state (enable only if dynamic timeouts are enabled). *on-click-middle*: ++ typeof: string ++ - Command to execute when middle-clicked on the module using mousewheel. + Command to execute when middle-clicked on the module using mousewheel. (reset the timeout to the initial value if dynamic timeouts are enabled). *on-click-right*: ++ typeof: string ++ - Command to execute when you right-click on the module. + Command to execute when you right-click on the module. (deactivate the inhibit if dynamic timeouts are enabled). *on-update*: ++ typeof: string ++ @@ -57,11 +57,11 @@ screensaver, also known as "presentation mode". *on-scroll-up*: ++ typeof: string ++ - Command to execute when scrolling up on the module. + Command to execute when scrolling up on the module. (increase the timeout if dynamic timeouts are enabled). *on-scroll-down*: ++ typeof: string ++ - Command to execute when scrolling down on the module. + Command to execute when scrolling down on the module. (decrease the timeout if dynamic timeouts are enabled). *smooth-scrolling-threshold*: ++ typeof: double ++ @@ -76,6 +76,16 @@ screensaver, also known as "presentation mode". typeof: double ++ The number of minutes the inhibition should last. +*timeout_step*: ++ + typeof: double ++ + default: 10 ++ + The number of minutes to add or subtract when scrolling (when dynamic timeouts are enabled). + +*dynamic-timeouts*: ++ + typeof: bool ++ + default: false ++ + Option to enable dynamic timeouts. + *tooltip*: ++ typeof: bool ++ default: true ++ @@ -95,6 +105,10 @@ screensaver, also known as "presentation mode". *{icon}*: Icon, as defined in *format-icons* +*{timeout}*: Timeout in minutes + +*{timeleft}*: Time left in minutes + # EXAMPLES ``` @@ -107,3 +121,16 @@ screensaver, also known as "presentation mode". "timeout": 30.5 } ``` + +``` +"idle_inhibitor": { + "format": "{status} {timeleft}/{timeout}", + "format-icons": { + "activated": "", + "deactivated": "" + }, + "timeout": 480, + "dynamic-timeouts": true, + "timeout-step": 10 +} +``` diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index a5fc9ac73..56568e7e4 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -5,13 +5,16 @@ std::list waybar::modules::IdleInhibitor::modules; bool waybar::modules::IdleInhibitor::status = false; +long waybar::modules::IdleInhibitor::deactivationTime = time(nullptr); waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar, const Json::Value& config) : ALabel(config, "idle_inhibitor", id, "{status}", 0, false, true), bar_(bar), idle_inhibitor_(nullptr), - pid_(-1) { + pid_(-1), + timeout(config_["timeout"].asDouble()), + timeout_step(config_["timeout-step"].isDouble() ? config_["timeout-step"].asDouble() : 10) { if (waybar::Client::inst()->idle_inhibit_manager == nullptr) { throw std::runtime_error("idle-inhibit not available"); } @@ -21,10 +24,14 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& toggleStatus(); } - event_box_.add_events(Gdk::BUTTON_PRESS_MASK); + deactivationTime = time(nullptr) + timeout * 60; + + event_box_.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK); event_box_.signal_button_press_event().connect( sigc::mem_fun(*this, &IdleInhibitor::handleToggle)); + event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &IdleInhibitor::handleScroll)); + // Add this to the modules list waybar::modules::IdleInhibitor::modules.push_back(this); @@ -63,7 +70,9 @@ auto waybar::modules::IdleInhibitor::update() -> void { } std::string status_text = status ? "activated" : "deactivated"; + int timeleft = (deactivationTime - time(nullptr)) / 60; label_.set_markup(fmt::format(fmt::runtime(format_), fmt::arg("status", status_text), + fmt::arg("timeout", timeout), fmt::arg("timeleft", timeleft), fmt::arg("icon", getIcon(0, status_text)))); label_.get_style_context()->add_class(status_text); if (tooltipEnabled()) { @@ -77,38 +86,49 @@ auto waybar::modules::IdleInhibitor::update() -> void { ALabel::update(); } -void waybar::modules::IdleInhibitor::toggleStatus() { +void waybar::modules::IdleInhibitor::toggleStatus(int force_status) { status = !status; + if (force_status != -1) { + status = force_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; - + if (status && timeout) { + deactivationTime = time(nullptr) + timeout * 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; + bool continueRunning = true; + int timeleft = (deactivationTime - time(nullptr)) / 60; + spdlog::info("updating timeleft. deactivation timestamp: {}, minutes left: {}", + deactivationTime, timeleft); + if (timeleft <= 0) { + spdlog::info("deactivating idle_inhibitor by timeout"); + status = false; + continueRunning = false; + } for (auto const& module : waybar::modules::IdleInhibitor::modules) { module->update(); } - /* disconnect */ - return false; + return continueRunning; }, - timeoutSecs); + 60); } } bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { if (e->button == 1) { - toggleStatus(); + if (config_["dynamic-timeout"].asBool()) { + toggleStatus(1); + } else { + toggleStatus(); + } // Make all other idle inhibitor modules update for (auto const& module : waybar::modules::IdleInhibitor::modules) { @@ -117,7 +137,41 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { } } } + if (e->button == 3) { + toggleStatus(0); + // Make all other idle inhibitor modules update + for (auto const& module : waybar::modules::IdleInhibitor::modules) { + if (module != this) { + module->update(); + } + } + } + if (e->button == 2) { + toggleStatus(0); + timeout = config_["timeout"].asDouble(); + } ALabel::handleToggle(e); return true; } + +bool waybar::modules::IdleInhibitor::handleScroll(GdkEventScroll* e) { + if (!config_["dynamic-timeout"].asBool()) { + return true; + } + auto dir = AModule::getScrollDir(e); + if (dir == SCROLL_DIR::NONE) { + return true; + } + toggleStatus(0); + double step = dir == SCROLL_DIR::UP ? 1 : -1; + step *= timeout_step; + timeout += step; + if (timeout < 0) { + timeout = 0; + } + deactivationTime = time(nullptr) + timeout * 60; + + ALabel::handleScroll(e); + return true; +}