waybar/src/modules/idle_inhibitor.cpp

68 lines
2.0 KiB
C++
Raw Normal View History

2019-02-17 15:27:54 +01:00
#include "modules/idle_inhibitor.hpp"
#include "util/command.hpp"
2019-02-17 15:27:54 +01:00
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
const Json::Value& config)
: ALabel(config, "idle_inhibitor", id, "{status}"),
bar_(bar),
status_("deactivated"),
pid_(-1) {
2019-02-17 15:27:54 +01:00
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(
sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
2019-02-17 15:27:54 +01:00
dp.emit();
}
waybar::modules::IdleInhibitor::~IdleInhibitor() {
if (waybar::Client::inst()->idle_inhibitor != nullptr) {
zwp_idle_inhibitor_v1_destroy(waybar::Client::inst()->idle_inhibitor);
waybar::Client::inst()->idle_inhibitor = nullptr;
2019-02-17 15:27:54 +01:00
}
if (pid_ != -1) {
kill(-pid_, 9);
pid_ = -1;
}
2019-02-17 15:27:54 +01:00
}
auto waybar::modules::IdleInhibitor::update() -> void {
// Check status
if (waybar::Client::inst()->idle_inhibitor != nullptr) {
status_ = "activated";
} else {
status_ = "deactivated";
}
2019-02-17 15:27:54 +01:00
label_.set_markup(
fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_))));
2019-03-10 10:29:06 +01:00
label_.get_style_context()->add_class(status_);
if (tooltipEnabled()) {
label_.set_tooltip_text(status_);
}
2020-04-12 18:30:21 +02:00
// Call parent update
ALabel::update();
2019-02-17 15:27:54 +01:00
}
bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
if (e->button == 1) {
2019-03-10 10:29:06 +01:00
label_.get_style_context()->remove_class(status_);
if (waybar::Client::inst()->idle_inhibitor != nullptr) {
zwp_idle_inhibitor_v1_destroy(waybar::Client::inst()->idle_inhibitor);
waybar::Client::inst()->idle_inhibitor = nullptr;
2019-04-18 17:52:00 +02:00
status_ = "deactivated";
2019-02-17 15:27:54 +01:00
} else {
waybar::Client::inst()->idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
2019-02-17 15:27:54 +01:00
status_ = "activated";
}
click_param = status_;
2019-02-17 15:27:54 +01:00
}
// Make all modules update
for (auto const& bar : waybar::Client::inst()->bars) {
bar->updateAll();
}
2019-05-02 17:51:01 +02:00
ALabel::handleToggle(e);
return true;
2019-02-17 15:27:54 +01:00
}