From cca5227210e70ef389fa103c7c28077ce5dad34c Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Thu, 13 Oct 2022 21:47:57 -0400 Subject: [PATCH] Add config value for inhibitor default state. --- include/modules/idle_inhibitor.hpp | 1 + man/waybar-idle-inhibitor.5.scd | 5 +++ src/modules/idle_inhibitor.cpp | 63 ++++++++++++++++++------------ 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/include/modules/idle_inhibitor.hpp b/include/modules/idle_inhibitor.hpp index ac0bcf02..8378e58d 100644 --- a/include/modules/idle_inhibitor.hpp +++ b/include/modules/idle_inhibitor.hpp @@ -20,6 +20,7 @@ class IdleInhibitor : public ALabel { private: bool handleToggle(GdkEventButton* const& e); + void toggleStatus(); const Bar& bar_; struct zwp_idle_inhibitor_v1* idle_inhibitor_; diff --git a/man/waybar-idle-inhibitor.5.scd b/man/waybar-idle-inhibitor.5.scd index b341a494..f85456d8 100644 --- a/man/waybar-idle-inhibitor.5.scd +++ b/man/waybar-idle-inhibitor.5.scd @@ -63,6 +63,11 @@ screensaving, also known as "presentation mode". typeof: double ++ Threshold to be used when scrolling. +*start-activated*: ++ + typeof: bool ++ + default: *false* ++ + Whether the inhibit should be activated when starting waybar. + *timeout*: ++ typeof: double ++ The number of minutes the inhibit should last. diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index 3302abb6..1133ebda 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -16,6 +16,13 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& throw std::runtime_error("idle-inhibit not available"); } + if (waybar::modules::IdleInhibitor::modules.empty() + && config_["start-activated"].isBool() + && config_["start-activated"].asBool() != status + ) { + toggleStatus(); + } + event_box_.add_events(Gdk::BUTTON_PRESS_MASK); event_box_.signal_button_press_event().connect( sigc::mem_fun(*this, &IdleInhibitor::handleToggle)); @@ -78,34 +85,38 @@ auto waybar::modules::IdleInhibitor::update() -> void { ALabel::update(); } +void waybar::modules::IdleInhibitor::toggleStatus() { + 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); + } +} + 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); - } + toggleStatus(); // Make all other idle inhibitor modules update for (auto const& module : waybar::modules::IdleInhibitor::modules) {