Waybar/src/modules/idle_inhibitor.cpp

85 lines
2.6 KiB
C++
Raw Normal View History

2019-02-17 14:27:54 +00:00
#include "modules/idle_inhibitor.hpp"
#include "util/command.hpp"
2019-02-17 14:27:54 +00:00
std::list<waybar::AModule*> waybar::modules::IdleInhibitor::idle_inhibitor_modules;
std::string waybar::modules::IdleInhibitor::idle_inhibitor_status = "deactivated";
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
const Json::Value& config)
: ALabel(config, "idle_inhibitor", id, "{status}"),
bar_(bar),
idle_inhibitor_(nullptr),
pid_(-1) {
2019-02-17 14:27:54 +00:00
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(
sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
// Add this to the Client's idle_inhibitor_modules
waybar::modules::IdleInhibitor::idle_inhibitor_modules.push_back(this);
2019-02-17 14:27:54 +00:00
dp.emit();
}
waybar::modules::IdleInhibitor::~IdleInhibitor() {
if (idle_inhibitor_ != nullptr) {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
idle_inhibitor_ = nullptr;
2019-02-17 14:27:54 +00:00
}
// Remove this from the Client's idle_inhibitor_modules
waybar::modules::IdleInhibitor::idle_inhibitor_modules.remove(this);
if (pid_ != -1) {
kill(-pid_, 9);
pid_ = -1;
}
2019-02-17 14:27:54 +00:00
}
auto waybar::modules::IdleInhibitor::update() -> void {
// Check status
std::string status = waybar::modules::IdleInhibitor::idle_inhibitor_status;
if (status == "activated") {
if (idle_inhibitor_ == nullptr) {
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
}
} else {
if (idle_inhibitor_ != nullptr) {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
idle_inhibitor_ = nullptr;
}
}
2019-02-17 14:27:54 +00:00
label_.set_markup(
fmt::format(format_, fmt::arg("status", status), fmt::arg("icon", getIcon(0, status))));
label_.get_style_context()->add_class(status);
if (tooltipEnabled()) {
label_.set_tooltip_text(status);
}
2020-04-12 16:30:21 +00:00
// Call parent update
ALabel::update();
2019-02-17 14:27:54 +00:00
}
bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
if (e->button == 1) {
std::string status = waybar::modules::IdleInhibitor::idle_inhibitor_status;
label_.get_style_context()->remove_class(status);
if (status == "activated") {
status = "deactivated";
2019-02-17 14:27:54 +00:00
} else {
status = "activated";
2019-02-17 14:27:54 +00:00
}
waybar::modules::IdleInhibitor::idle_inhibitor_status = status;
2019-02-17 14:27:54 +00:00
}
// Make all other idle inhibitor modules update
for (auto const& module : waybar::modules::IdleInhibitor::idle_inhibitor_modules) {
if (module != this) {
module->update();
}
}
2019-05-02 15:51:01 +00:00
ALabel::handleToggle(e);
return true;
2019-02-17 14:27:54 +00:00
}