Merge pull request #3370 from torgiren/idle_inhibitor

allow dynamic timeout setting for idle inhibitor
This commit is contained in:
Alexis Rouillard
2026-07-04 01:02:30 +02:00
committed by GitHub
3 changed files with 110 additions and 19 deletions
+8 -1
View File
@@ -22,10 +22,13 @@ class IdleInhibitor : public ALabel {
auto refresh(int) -> void override; auto refresh(int) -> void override;
static std::list<waybar::AModule*> modules; static std::list<waybar::AModule*> modules;
static bool status; static bool status;
static long deactivationTime;
private: private:
bool handleToggle(GdkEventButton* const& e) override; bool handleToggle(GdkEventButton* const& e) override;
void toggleStatus(); bool handleScroll(GdkEventScroll* e) override;
void toggleStatus(int force_status = -1);
void setupIdleNotification(); void setupIdleNotification();
void teardownIdleNotification(); void teardownIdleNotification();
static void handleIdled(void* data, ext_idle_notification_v1* notification); static void handleIdled(void* data, ext_idle_notification_v1* notification);
@@ -34,6 +37,10 @@ class IdleInhibitor : public ALabel {
const Bar& bar_; const Bar& bar_;
struct zwp_idle_inhibitor_v1* idle_inhibitor_; struct zwp_idle_inhibitor_v1* idle_inhibitor_;
int pid_; int pid_;
bool dynamicTimeout;
short timeout;
short timeout_step;
bool wait_for_activity_; bool wait_for_activity_;
}; };
+34 -5
View File
@@ -41,15 +41,15 @@ screensaver, also known as "presentation mode".
*on-click*: ++ *on-click*: ++
typeof: string ++ 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*: ++ *on-click-middle*: ++
typeof: string ++ 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*: ++ *on-click-right*: ++
typeof: string ++ 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*: ++ *on-update*: ++
typeof: string ++ typeof: string ++
@@ -57,11 +57,11 @@ screensaver, also known as "presentation mode".
*on-scroll-up*: ++ *on-scroll-up*: ++
typeof: string ++ 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*: ++ *on-scroll-down*: ++
typeof: string ++ 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*: ++ *smooth-scrolling-threshold*: ++
typeof: double ++ typeof: double ++
@@ -76,6 +76,16 @@ screensaver, also known as "presentation mode".
typeof: double ++ typeof: double ++
The number of minutes the inhibition should last. 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.
*wait-for-activity*: ++ *wait-for-activity*: ++
typeof: bool ++ typeof: bool ++
default: *false* ++ default: *false* ++
@@ -124,6 +134,10 @@ screensaver, also known as "presentation mode".
*{icon}*: Icon, as defined in *format-icons* *{icon}*: Icon, as defined in *format-icons*
*{timeout}*: Timeout in minutes
*{timeleft}*: Time left in minutes
# EXAMPLES # EXAMPLES
Basic usage with timeout: Basic usage with timeout:
@@ -139,6 +153,21 @@ Basic usage with timeout:
} }
``` ```
With dynamic timeouts (scroll to adjust the timeout):
```
"idle_inhibitor": {
"format": "{status} {timeleft}/{timeout}",
"format-icons": {
"activated": "",
"deactivated": ""
},
"timeout": 480,
"dynamic-timeouts": true,
"timeout-step": 10
}
```
With external control via signals (can be toggled with `pkill -SIGRTMIN+8 waybar`): With external control via signals (can be toggled with `pkill -SIGRTMIN+8 waybar`):
``` ```
+65 -10
View File
@@ -6,6 +6,7 @@
std::list<waybar::AModule*> waybar::modules::IdleInhibitor::modules; std::list<waybar::AModule*> waybar::modules::IdleInhibitor::modules;
bool waybar::modules::IdleInhibitor::status = false; bool waybar::modules::IdleInhibitor::status = false;
long waybar::modules::IdleInhibitor::deactivationTime = time(nullptr);
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar, waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
const Json::Value& config) const Json::Value& config)
@@ -15,6 +16,8 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar&
idle_notification_(nullptr), idle_notification_(nullptr),
idle_timeout_ms_(0), idle_timeout_ms_(0),
pid_(-1), pid_(-1),
timeout(config_["timeout"].asDouble()),
timeout_step(config_["timeout-step"].isDouble() ? config_["timeout-step"].asDouble() : 10),
wait_for_activity_(false) { wait_for_activity_(false) {
if (waybar::Client::inst()->idle_inhibit_manager == nullptr) { if (waybar::Client::inst()->idle_inhibit_manager == nullptr) {
throw std::runtime_error("idle-inhibit not available"); throw std::runtime_error("idle-inhibit not available");
@@ -35,10 +38,14 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar&
toggleStatus(); 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( event_box_.signal_button_press_event().connect(
sigc::mem_fun(*this, &IdleInhibitor::handleToggle)); sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &IdleInhibitor::handleScroll));
// Add this to the modules list // Add this to the modules list
waybar::modules::IdleInhibitor::modules.push_back(this); waybar::modules::IdleInhibitor::modules.push_back(this);
@@ -79,7 +86,9 @@ auto waybar::modules::IdleInhibitor::update() -> void {
} }
std::string status_text = status ? "activated" : "deactivated"; std::string status_text = status ? "activated" : "deactivated";
int timeleft = (deactivationTime - time(nullptr)) / 60;
updateLabelAndTooltipForState(status_text, format_, "{status}", fmt::arg("status", status_text), updateLabelAndTooltipForState(status_text, format_, "{status}", fmt::arg("status", status_text),
fmt::arg("timeout", timeout), fmt::arg("timeleft", timeleft),
fmt::arg("icon", getIcon(0, status_text))); fmt::arg("icon", getIcon(0, status_text)));
label_.get_style_context()->add_class(status_text); label_.get_style_context()->add_class(status_text);
// Call parent update // Call parent update
@@ -97,18 +106,19 @@ auto waybar::modules::IdleInhibitor::refresh(int sig) -> void {
} }
} }
void waybar::modules::IdleInhibitor::toggleStatus() { void waybar::modules::IdleInhibitor::toggleStatus(int force_status) {
status = !status; status = !status;
if (force_status != -1) {
status = force_status;
}
if (timeout_.connected()) { if (timeout_.connected()) {
/* cancel any already active timeout handler */ /* cancel any already active timeout handler */
timeout_.disconnect(); timeout_.disconnect();
} }
if (status && config_["timeout"].isNumeric()) { if (status && timeout) {
auto timeoutMins = config_["timeout"].asDouble(); idle_timeout_ms_ = static_cast<int>(timeout * 60) * 1000;
int timeoutSecs = timeoutMins * 60;
idle_timeout_ms_ = timeoutSecs * 1000;
// If wait-for-activity is enabled, set up idle notification // If wait-for-activity is enabled, set up idle notification
if (wait_for_activity_) { if (wait_for_activity_) {
@@ -117,21 +127,28 @@ void waybar::modules::IdleInhibitor::toggleStatus() {
teardownIdleNotification(); teardownIdleNotification();
setupIdleNotification(); setupIdleNotification();
} else { } else {
// Original behavior: simple timeout // Original behavior: countdown timeout with per-minute updates
deactivationTime = time(nullptr) + timeout * 60;
timeout_ = Glib::signal_timeout().connect_seconds( timeout_ = Glib::signal_timeout().connect_seconds(
[]() { []() {
/* intentionally not tied to a module instance lifetime /* intentionally not tied to a module instance lifetime
* as the output with `this` can be disconnected * as the output with `this` can be disconnected
*/ */
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"); spdlog::info("deactivating idle_inhibitor by timeout");
status = false; status = false;
continueRunning = false;
}
for (auto const& module : waybar::modules::IdleInhibitor::modules) { for (auto const& module : waybar::modules::IdleInhibitor::modules) {
module->update(); module->update();
} }
/* disconnect */ return continueRunning;
return false;
}, },
timeoutSecs); 60);
} }
} else { } else {
// When deactivated, tear down idle notification // When deactivated, tear down idle notification
@@ -141,7 +158,11 @@ void waybar::modules::IdleInhibitor::toggleStatus() {
bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
if (e->button == 1) { if (e->button == 1) {
if (config_["dynamic-timeout"].asBool()) {
toggleStatus(1);
} else {
toggleStatus(); toggleStatus();
}
// Make all other idle inhibitor modules update // Make all other idle inhibitor modules update
for (auto const& module : waybar::modules::IdleInhibitor::modules) { for (auto const& module : waybar::modules::IdleInhibitor::modules) {
@@ -150,11 +171,45 @@ 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); ALabel::handleToggle(e);
return true; 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;
}
void waybar::modules::IdleInhibitor::handleIdled(void* data, void waybar::modules::IdleInhibitor::handleIdled(void* data,
ext_idle_notification_v1* /*notification*/) { ext_idle_notification_v1* /*notification*/) {
spdlog::info("deactivating idle_inhibitor due to user inactivity"); spdlog::info("deactivating idle_inhibitor due to user inactivity");