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
|
|
|
|
2020-11-01 18:14:05 +01:00
|
|
|
std::list<waybar::AModule*> waybar::modules::IdleInhibitor::modules;
|
|
|
|
std::string waybar::modules::IdleInhibitor::status = "deactivated";
|
2020-11-01 18:09:48 +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-05-22 12:06:24 +02:00
|
|
|
: ALabel(config, "idle_inhibitor", id, "{status}"),
|
2019-04-23 15:56:38 +02:00
|
|
|
bar_(bar),
|
2020-11-01 14:38:58 +01:00
|
|
|
idle_inhibitor_(nullptr),
|
2019-04-23 15:56:38 +02:00
|
|
|
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(
|
2019-03-03 22:02:34 +01:00
|
|
|
sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
|
2020-11-01 14:33:28 +01:00
|
|
|
|
2020-11-01 18:14:05 +01:00
|
|
|
// Add this to the modules list
|
|
|
|
waybar::modules::IdleInhibitor::modules.push_back(this);
|
2020-11-01 14:33:28 +01:00
|
|
|
|
2019-02-17 15:27:54 +01:00
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
2020-11-01 14:33:28 +01:00
|
|
|
if (idle_inhibitor_ != nullptr) {
|
|
|
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
|
|
|
idle_inhibitor_ = nullptr;
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|
2020-11-01 14:33:28 +01:00
|
|
|
|
2020-11-01 18:14:05 +01:00
|
|
|
// Remove this from the modules list
|
|
|
|
waybar::modules::IdleInhibitor::modules.remove(this);
|
2020-11-01 14:33:28 +01:00
|
|
|
|
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 {
|
2020-10-31 17:31:27 +01:00
|
|
|
// Check status
|
2020-11-01 18:14:05 +01:00
|
|
|
std::string status = waybar::modules::IdleInhibitor::status;
|
2020-11-01 14:33:28 +01:00
|
|
|
if (status == "activated") {
|
|
|
|
if (idle_inhibitor_ == nullptr) {
|
|
|
|
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
|
|
|
|
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
|
|
|
|
}
|
2020-10-31 17:31:27 +01:00
|
|
|
} else {
|
2020-11-01 14:33:28 +01:00
|
|
|
if (idle_inhibitor_ != nullptr) {
|
|
|
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
|
|
|
idle_inhibitor_ = nullptr;
|
|
|
|
}
|
2020-10-31 17:31:27 +01:00
|
|
|
}
|
|
|
|
|
2019-02-17 15:27:54 +01:00
|
|
|
label_.set_markup(
|
2020-11-01 14:33:28 +01:00
|
|
|
fmt::format(format_, fmt::arg("status", status), fmt::arg("icon", getIcon(0, status))));
|
|
|
|
label_.get_style_context()->add_class(status);
|
2019-04-18 17:43:16 +02:00
|
|
|
if (tooltipEnabled()) {
|
2020-11-01 14:33:28 +01:00
|
|
|
label_.set_tooltip_text(status);
|
2019-02-22 16:58:36 +01:00
|
|
|
}
|
2020-04-12 18:30:21 +02:00
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
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) {
|
2020-11-01 18:14:05 +01:00
|
|
|
std::string status = waybar::modules::IdleInhibitor::status;
|
2020-11-01 14:33:28 +01:00
|
|
|
label_.get_style_context()->remove_class(status);
|
|
|
|
if (status == "activated") {
|
|
|
|
status = "deactivated";
|
2019-02-17 15:27:54 +01:00
|
|
|
} else {
|
2020-11-01 14:33:28 +01:00
|
|
|
status = "activated";
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|
2020-11-01 18:14:05 +01:00
|
|
|
waybar::modules::IdleInhibitor::status = status;
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|
2020-10-31 17:31:27 +01:00
|
|
|
|
2020-10-31 18:30:25 +01:00
|
|
|
// Make all other idle inhibitor modules update
|
2020-11-01 18:14:05 +01:00
|
|
|
for (auto const& module : waybar::modules::IdleInhibitor::modules) {
|
2020-10-31 18:30:25 +01:00
|
|
|
if (module != this) {
|
|
|
|
module->update();
|
|
|
|
}
|
2020-10-31 17:31:27 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 17:51:01 +02:00
|
|
|
ALabel::handleToggle(e);
|
|
|
|
return true;
|
2019-02-17 15:27:54 +01:00
|
|
|
}
|