Added signal support and wait-for-activity bool.
This commit is contained in:
+115
-15
@@ -11,11 +11,17 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar&
|
||||
: ALabel(config, "idle_inhibitor", id, "{status}", 0, false, true),
|
||||
bar_(bar),
|
||||
idle_inhibitor_(nullptr),
|
||||
pid_(-1) {
|
||||
pid_(-1),
|
||||
wait_for_activity_(false) {
|
||||
if (waybar::Client::inst()->idle_inhibit_manager == nullptr) {
|
||||
throw std::runtime_error("idle-inhibit not available");
|
||||
}
|
||||
|
||||
// Read the wait-for-activity config option
|
||||
if (config_["wait-for-activity"].isBool()) {
|
||||
wait_for_activity_ = config_["wait-for-activity"].asBool();
|
||||
}
|
||||
|
||||
if (waybar::modules::IdleInhibitor::modules.empty() && config_["start-activated"].isBool() &&
|
||||
config_["start-activated"].asBool() != status) {
|
||||
toggleStatus();
|
||||
@@ -32,6 +38,8 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar&
|
||||
}
|
||||
|
||||
waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
||||
teardownActivityMonitoring();
|
||||
|
||||
if (idle_inhibitor_ != nullptr) {
|
||||
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
||||
idle_inhibitor_ = nullptr;
|
||||
@@ -77,6 +85,17 @@ auto waybar::modules::IdleInhibitor::update() -> void {
|
||||
ALabel::update();
|
||||
}
|
||||
|
||||
auto waybar::modules::IdleInhibitor::refresh(int sig) -> void {
|
||||
if (config_["signal"].isInt() && sig == SIGRTMIN + config_["signal"].asInt()) {
|
||||
toggleStatus();
|
||||
|
||||
// Make all other idle inhibitor modules update
|
||||
for (auto const& module : waybar::modules::IdleInhibitor::modules) {
|
||||
module->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void waybar::modules::IdleInhibitor::toggleStatus() {
|
||||
status = !status;
|
||||
|
||||
@@ -89,20 +108,30 @@ void waybar::modules::IdleInhibitor::toggleStatus() {
|
||||
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);
|
||||
// If wait-for-activity is enabled, set up activity monitoring
|
||||
if (wait_for_activity_) {
|
||||
setupActivityMonitoring();
|
||||
resetActivityTimeout();
|
||||
} else {
|
||||
// Original behavior: simple timeout
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
// When deactivated, tear down activity monitoring
|
||||
teardownActivityMonitoring();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,3 +150,74 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
|
||||
ALabel::handleToggle(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool waybar::modules::IdleInhibitor::handleMotion(GdkEventMotion* const& e) {
|
||||
if (wait_for_activity_ && status) {
|
||||
resetActivityTimeout();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool waybar::modules::IdleInhibitor::handleKey(GdkEventKey* const& e) {
|
||||
if (wait_for_activity_ && status) {
|
||||
resetActivityTimeout();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void waybar::modules::IdleInhibitor::resetActivityTimeout() {
|
||||
if (!config_["timeout"].isNumeric()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (activity_timeout_.connected()) {
|
||||
activity_timeout_.disconnect();
|
||||
}
|
||||
|
||||
auto timeoutMins = config_["timeout"].asDouble();
|
||||
int timeoutSecs = timeoutMins * 60;
|
||||
|
||||
activity_timeout_ = Glib::signal_timeout().connect_seconds(
|
||||
[]() {
|
||||
spdlog::info("deactivating idle_inhibitor due to inactivity");
|
||||
status = false;
|
||||
for (auto const& module : waybar::modules::IdleInhibitor::modules) {
|
||||
module->update();
|
||||
}
|
||||
return false;
|
||||
},
|
||||
timeoutSecs);
|
||||
}
|
||||
|
||||
void waybar::modules::IdleInhibitor::setupActivityMonitoring() {
|
||||
// Don't set up if already connected
|
||||
if (motion_connection_.connected() || key_connection_.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Enable motion and key event monitoring on the bar window
|
||||
auto window = bar_.window.get_window();
|
||||
if (window) {
|
||||
window->set_events(window->get_events() | Gdk::POINTER_MOTION_MASK | Gdk::KEY_PRESS_MASK);
|
||||
}
|
||||
|
||||
// Connect to the bar window's event signals
|
||||
motion_connection_ = bar_.window.signal_motion_notify_event().connect(
|
||||
sigc::mem_fun(*this, &IdleInhibitor::handleMotion));
|
||||
key_connection_ = bar_.window.signal_key_press_event().connect(
|
||||
sigc::mem_fun(*this, &IdleInhibitor::handleKey));
|
||||
}
|
||||
|
||||
void waybar::modules::IdleInhibitor::teardownActivityMonitoring() {
|
||||
if (activity_timeout_.connected()) {
|
||||
activity_timeout_.disconnect();
|
||||
}
|
||||
|
||||
if (motion_connection_.connected()) {
|
||||
motion_connection_.disconnect();
|
||||
}
|
||||
|
||||
if (key_connection_.connected()) {
|
||||
key_connection_.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user