2019-02-17 14:27:54 +00:00
|
|
|
#include "modules/idle_inhibitor.hpp"
|
2020-10-22 05:45:03 +00:00
|
|
|
|
|
|
|
#include "idle-inhibit-unstable-v1-client-protocol.h"
|
2019-03-03 21:02:34 +00:00
|
|
|
#include "util/command.hpp"
|
2019-02-17 14:27:54 +00:00
|
|
|
|
2020-11-01 17:14:05 +00:00
|
|
|
std::list<waybar::AModule*> waybar::modules::IdleInhibitor::modules;
|
2022-04-06 06:37:19 +00:00
|
|
|
bool waybar::modules::IdleInhibitor::status = false;
|
2020-11-01 17:09:48 +00:00
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
|
|
|
|
const Json::Value& config)
|
2019-05-22 10:06:24 +00:00
|
|
|
: ALabel(config, "idle_inhibitor", id, "{status}"),
|
2019-04-23 13:56:38 +00:00
|
|
|
bar_(bar),
|
2020-11-01 13:38:58 +00:00
|
|
|
idle_inhibitor_(nullptr),
|
2019-04-23 13:56:38 +00:00
|
|
|
pid_(-1) {
|
2021-02-09 04:52:29 +00:00
|
|
|
if (waybar::Client::inst()->idle_inhibit_manager == nullptr) {
|
|
|
|
throw std::runtime_error("idle-inhibit not available");
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:27:54 +00:00
|
|
|
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
|
|
|
event_box_.signal_button_press_event().connect(
|
2019-03-03 21:02:34 +00:00
|
|
|
sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
|
2020-11-01 13:33:28 +00:00
|
|
|
|
2020-11-01 17:14:05 +00:00
|
|
|
// Add this to the modules list
|
|
|
|
waybar::modules::IdleInhibitor::modules.push_back(this);
|
2020-11-01 13:33:28 +00:00
|
|
|
|
2019-02-17 14:27:54 +00:00
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
2020-11-01 13:33:28 +00:00
|
|
|
if (idle_inhibitor_ != nullptr) {
|
|
|
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
|
|
|
idle_inhibitor_ = nullptr;
|
2019-02-17 14:27:54 +00:00
|
|
|
}
|
2020-11-01 13:33:28 +00:00
|
|
|
|
2020-11-01 17:14:05 +00:00
|
|
|
// Remove this from the modules list
|
|
|
|
waybar::modules::IdleInhibitor::modules.remove(this);
|
2020-11-01 13:33:28 +00:00
|
|
|
|
2019-04-23 13:56:38 +00:00
|
|
|
if (pid_ != -1) {
|
|
|
|
kill(-pid_, 9);
|
|
|
|
pid_ = -1;
|
|
|
|
}
|
2019-02-17 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
auto waybar::modules::IdleInhibitor::update() -> void {
|
2020-10-31 16:31:27 +00:00
|
|
|
// Check status
|
2020-11-01 18:25:41 +00:00
|
|
|
if (status) {
|
2020-11-01 18:17:51 +00:00
|
|
|
label_.get_style_context()->remove_class("deactivated");
|
2020-11-01 13:33:28 +00:00
|
|
|
if (idle_inhibitor_ == nullptr) {
|
|
|
|
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
|
2022-04-06 06:37:19 +00:00
|
|
|
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
|
2020-11-01 13:33:28 +00:00
|
|
|
}
|
2020-10-31 16:31:27 +00:00
|
|
|
} else {
|
2020-11-01 18:17:51 +00:00
|
|
|
label_.get_style_context()->remove_class("activated");
|
2020-11-01 13:33:28 +00:00
|
|
|
if (idle_inhibitor_ != nullptr) {
|
|
|
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
|
|
|
idle_inhibitor_ = nullptr;
|
|
|
|
}
|
2020-10-31 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 18:25:41 +00:00
|
|
|
std::string status_text = status ? "activated" : "deactivated";
|
2022-04-06 06:37:19 +00:00
|
|
|
label_.set_markup(fmt::format(format_, fmt::arg("status", status_text),
|
|
|
|
fmt::arg("icon", getIcon(0, status_text))));
|
2020-11-01 18:25:41 +00:00
|
|
|
label_.get_style_context()->add_class(status_text);
|
2019-04-18 15:43:16 +00:00
|
|
|
if (tooltipEnabled()) {
|
2020-11-01 18:25:41 +00:00
|
|
|
label_.set_tooltip_text(status_text);
|
2019-02-22 15:58:36 +00:00
|
|
|
}
|
2020-04-12 16:30:21 +00:00
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
2019-02-17 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 21:02:34 +00:00
|
|
|
bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
|
|
|
|
if (e->button == 1) {
|
2020-11-01 18:25:41 +00:00
|
|
|
status = !status;
|
2020-10-31 16:31:27 +00:00
|
|
|
|
2020-11-01 18:25:41 +00:00
|
|
|
// Make all other idle inhibitor modules update
|
|
|
|
for (auto const& module : waybar::modules::IdleInhibitor::modules) {
|
|
|
|
if (module != this) {
|
|
|
|
module->update();
|
|
|
|
}
|
2020-10-31 17:30:25 +00:00
|
|
|
}
|
2020-10-31 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 15:51:01 +00:00
|
|
|
ALabel::handleToggle(e);
|
|
|
|
return true;
|
2019-02-17 14:27:54 +00:00
|
|
|
}
|