2019-02-17 15:27:54 +01:00
|
|
|
#include "modules/idle_inhibitor.hpp"
|
2019-03-03 22:02:34 +01:00
|
|
|
#include "util/command.hpp"
|
2019-02-17 15:27:54 +01:00
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
|
|
|
|
const Json::Value& config)
|
2019-04-23 15:56:38 +02:00
|
|
|
: ALabel(config, "{status}"),
|
|
|
|
bar_(bar),
|
|
|
|
status_("deactivated"),
|
|
|
|
idle_inhibitor_(nullptr),
|
|
|
|
pid_(-1) {
|
2019-02-17 15:27:54 +01:00
|
|
|
label_.set_name("idle_inhibitor");
|
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
|
|
|
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
|
|
|
event_box_.signal_button_press_event().connect(
|
2019-03-03 22:02:34 +01:00
|
|
|
sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
|
2019-02-17 15:27:54 +01:00
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
2019-04-24 12:37:24 +02:00
|
|
|
if (idle_inhibitor_ != nullptr) {
|
2019-02-17 15:27:54 +01:00
|
|
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
|
|
|
idle_inhibitor_ = nullptr;
|
|
|
|
}
|
2019-04-23 15:56:38 +02:00
|
|
|
if (pid_ != -1) {
|
|
|
|
kill(-pid_, 9);
|
|
|
|
pid_ = -1;
|
|
|
|
}
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
auto waybar::modules::IdleInhibitor::update() -> void {
|
2019-02-17 15:27:54 +01:00
|
|
|
label_.set_markup(
|
2019-04-18 17:43:16 +02:00
|
|
|
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_);
|
2019-04-18 17:43:16 +02:00
|
|
|
if (tooltipEnabled()) {
|
2019-02-22 16:58:36 +01:00
|
|
|
label_.set_tooltip_text(status_);
|
|
|
|
}
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:02:34 +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_);
|
2019-04-24 12:37:24 +02:00
|
|
|
if (idle_inhibitor_ != nullptr) {
|
2019-02-17 15:27:54 +01:00
|
|
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
|
|
|
idle_inhibitor_ = nullptr;
|
2019-04-18 17:52:00 +02:00
|
|
|
status_ = "deactivated";
|
2019-02-17 15:27:54 +01:00
|
|
|
} else {
|
|
|
|
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
|
2019-04-18 17:43:16 +02:00
|
|
|
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
|
2019-02-17 15:27:54 +01:00
|
|
|
status_ = "activated";
|
|
|
|
}
|
2019-05-02 16:56:45 +02:00
|
|
|
click_param = status_;
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|
2019-05-02 17:51:01 +02:00
|
|
|
ALabel::handleToggle(e);
|
|
|
|
return true;
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|