Files
Waybar/src/modules/custom.cpp
T

275 lines
7.7 KiB
C++
Raw Normal View History

#include "modules/custom.hpp"
2020-05-25 09:21:04 +02:00
2019-05-18 19:44:45 -04:00
#include <spdlog/spdlog.h>
#include "util/scope_guard.hpp"
2019-05-22 12:20:13 +02:00
waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
const Json::Value& config, const std::string& output_name)
2022-11-24 12:28:52 +01:00
: ALabel(config, "custom-" + name, id, "{}"),
name_(name),
output_name_(output_name),
id_(id),
percentage_(0),
fp_(nullptr),
pid_(-1) {
2020-05-25 09:21:04 +02:00
dp.emit();
2023-12-28 19:38:01 -08:00
if (!config_["signal"].empty() && config_["interval"].empty() &&
2023-12-28 19:36:21 -08:00
config_["restart-interval"].empty()) {
2023-09-18 14:56:14 -07:00
waitingWorker();
} else if (interval_.count() > 0) {
2020-05-24 18:27:10 +02:00
delayWorker();
} else if (config_["exec"].isString()) {
continuousWorker();
}
2018-08-20 14:50:45 +02:00
}
2019-04-18 17:52:00 +02:00
waybar::modules::Custom::~Custom() {
if (pid_ != -1) {
2020-05-22 20:57:41 +02:00
killpg(pid_, SIGTERM);
waitpid(pid_, NULL, 0);
pid_ = -1;
2018-12-08 12:58:47 +01:00
}
}
2019-04-18 17:52:00 +02:00
void waybar::modules::Custom::delayWorker() {
2018-11-23 11:57:37 +01:00
thread_ = [this] {
2018-08-18 17:27:40 +02:00
bool can_update = true;
2018-10-26 09:27:16 +02:00
if (config_["exec-if"].isString()) {
2020-05-24 18:27:10 +02:00
output_ = util::command::execNoRead(config_["exec-if"].asString());
if (output_.exit_code != 0) {
2018-08-18 17:27:40 +02:00
can_update = false;
2020-05-25 09:21:04 +02:00
dp.emit();
2018-08-18 17:27:40 +02:00
}
}
if (can_update) {
2020-05-24 18:27:10 +02:00
if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString(), output_name_);
2020-05-24 18:27:10 +02:00
}
2018-08-20 14:50:45 +02:00
dp.emit();
2018-08-18 17:27:40 +02:00
}
2018-11-23 11:57:37 +01:00
thread_.sleep_for(interval_);
};
}
2019-04-18 17:52:00 +02:00
void waybar::modules::Custom::continuousWorker() {
2018-09-18 23:15:37 +02:00
auto cmd = config_["exec"].asString();
pid_ = -1;
fp_ = util::command::open(cmd, pid_, output_name_);
2018-12-08 12:58:47 +01:00
if (!fp_) {
2018-09-18 23:15:37 +02:00
throw std::runtime_error("Unable to open " + cmd);
}
2020-05-24 21:33:38 +02:00
thread_ = [this, cmd] {
2022-04-06 08:37:19 +02:00
char* buff = nullptr;
waybar::util::ScopeGuard buff_deleter([buff]() {
if (buff) {
free(buff);
}
});
2018-09-18 23:15:37 +02:00
size_t len = 0;
2018-12-08 12:58:47 +01:00
if (getline(&buff, &len, fp_) == -1) {
int exit_code = 1;
if (fp_) {
exit_code = WEXITSTATUS(util::command::close(fp_, pid_));
fp_ = nullptr;
}
if (exit_code != 0) {
2019-04-18 17:52:00 +02:00
output_ = {exit_code, ""};
dp.emit();
2019-05-18 19:44:45 -04:00
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
}
2020-03-25 22:30:22 +01:00
if (config_["restart-interval"].isUInt()) {
2020-05-24 21:33:38 +02:00
pid_ = -1;
2020-08-28 01:16:41 +03:00
thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt()));
fp_ = util::command::open(cmd, pid_, output_name_);
2020-05-24 21:33:38 +02:00
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
} else {
thread_.stop();
2020-03-26 09:18:47 +01:00
return;
}
2020-05-24 21:33:38 +02:00
} else {
std::string output = buff;
2018-09-18 23:15:37 +02:00
2020-05-24 21:33:38 +02:00
// Remove last newline
if (!output.empty() && output[output.length() - 1] == '\n') {
output.erase(output.length() - 1);
2020-03-26 09:18:47 +01:00
}
2020-05-24 21:33:38 +02:00
output_ = {0, output};
dp.emit();
}
2018-09-18 23:15:37 +02:00
};
}
2023-09-18 14:56:14 -07:00
void waybar::modules::Custom::waitingWorker() {
thread_ = [this] {
bool can_update = true;
if (config_["exec-if"].isString()) {
output_ = util::command::execNoRead(config_["exec-if"].asString());
if (output_.exit_code != 0) {
can_update = false;
dp.emit();
}
}
if (can_update) {
if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString(), output_name_);
2023-09-18 14:56:14 -07:00
}
dp.emit();
}
thread_.sleep();
};
}
2019-05-22 12:22:56 +02:00
void waybar::modules::Custom::refresh(int sig) {
2019-04-18 17:52:00 +02:00
if (sig == SIGRTMIN + config_["signal"].asInt()) {
2019-03-18 18:46:44 +01:00
thread_.wake_up();
}
}
2020-09-06 22:47:34 +03:00
void waybar::modules::Custom::handleEvent() {
if (!config_["exec-on-event"].isBool() || config_["exec-on-event"].asBool()) {
thread_.wake_up();
}
}
2019-05-18 16:07:55 +02:00
bool waybar::modules::Custom::handleScroll(GdkEventScroll* e) {
2022-11-24 12:28:52 +01:00
auto ret = ALabel::handleScroll(e);
2020-09-06 22:47:34 +03:00
handleEvent();
2019-05-18 16:07:55 +02:00
return ret;
}
bool waybar::modules::Custom::handleToggle(GdkEventButton* const& e) {
2022-11-24 12:28:52 +01:00
auto ret = ALabel::handleToggle(e);
2020-09-06 22:47:34 +03:00
handleEvent();
2019-05-18 16:07:55 +02:00
return ret;
}
2019-04-18 17:52:00 +02:00
auto waybar::modules::Custom::update() -> void {
// Hide label if output is empty
2020-05-25 09:21:04 +02:00
if ((config_["exec"].isString() || config_["exec-if"].isString()) &&
(output_.out.empty() || output_.exit_code != 0)) {
2018-12-18 17:30:54 +01:00
event_box_.hide();
2018-08-10 17:05:12 +02:00
} else {
2018-11-01 00:40:44 +01:00
if (config_["return-type"].asString() == "json") {
parseOutputJson();
} else {
parseOutputRaw();
}
2023-01-16 13:24:55 -08:00
auto str = fmt::format(fmt::runtime(format_), text_, fmt::arg("alt", alt_),
2019-06-03 09:50:35 +02:00
fmt::arg("icon", getIcon(percentage_, alt_)),
fmt::arg("percentage", percentage_));
if (str.empty()) {
2019-05-27 00:05:21 +02:00
event_box_.hide();
} else {
2022-11-24 12:28:52 +01:00
label_.set_markup(str);
2019-05-27 00:08:16 +02:00
if (tooltipEnabled()) {
if (text_ == tooltip_) {
2022-11-24 12:28:52 +01:00
if (label_.get_tooltip_markup() != str) {
label_.set_tooltip_markup(str);
}
} else if (config_["tooltip-format"].isString()) {
auto tooltip = config_["tooltip-format"].asString();
tooltip = fmt::format(fmt::runtime(tooltip), text_, fmt::arg("alt", alt_),
fmt::arg("icon", getIcon(percentage_, alt_)),
fmt::arg("percentage", percentage_));
label_.set_tooltip_markup(tooltip);
2019-05-27 00:08:16 +02:00
} else {
2022-11-24 12:28:52 +01:00
if (label_.get_tooltip_markup() != tooltip_) {
label_.set_tooltip_markup(tooltip_);
}
2019-05-27 00:08:16 +02:00
}
}
auto style = label_.get_style_context();
auto classes = style->list_classes();
2019-05-27 00:08:16 +02:00
for (auto const& c : classes) {
if (c == id_) continue;
style->remove_class(c);
2019-05-27 00:08:16 +02:00
}
for (auto const& c : class_) {
style->add_class(c);
2019-05-27 00:08:16 +02:00
}
style->add_class("flat");
style->add_class("text-button");
style->add_class(MODULE_CLASS);
2019-05-27 00:05:21 +02:00
event_box_.show();
}
}
2020-04-12 18:30:21 +02:00
// Call parent update
2022-11-24 12:28:52 +01:00
ALabel::update();
}
2019-04-18 17:52:00 +02:00
void waybar::modules::Custom::parseOutputRaw() {
std::istringstream output(output_.out);
2022-04-06 08:37:19 +02:00
std::string line;
int i = 0;
while (getline(output, line)) {
Glib::ustring validated_line = line;
2023-10-31 23:31:58 +03:00
if (!validated_line.validate()) {
validated_line = validated_line.make_valid();
}
if (i == 0) {
2019-03-01 17:02:50 +01:00
if (config_["escape"].isBool() && config_["escape"].asBool()) {
text_ = Glib::Markup::escape_text(validated_line);
2024-04-18 14:17:30 +00:00
tooltip_ = Glib::Markup::escape_text(validated_line);
2019-03-01 17:02:50 +01:00
} else {
text_ = validated_line;
2024-04-18 14:17:30 +00:00
tooltip_ = validated_line;
2019-03-01 17:02:50 +01:00
}
tooltip_ = validated_line;
2019-04-15 10:18:27 +02:00
class_.clear();
} else if (i == 1) {
2024-04-18 14:17:30 +00:00
if (config_["escape"].isBool() && config_["escape"].asBool()) {
tooltip_ = Glib::Markup::escape_text(validated_line);
} else {
tooltip_ = validated_line;
2024-04-19 06:21:10 +02:00
}
} else if (i == 2) {
class_.push_back(validated_line);
} else {
break;
}
i++;
}
2018-11-01 00:40:44 +01:00
}
2019-04-18 17:52:00 +02:00
void waybar::modules::Custom::parseOutputJson() {
2018-11-01 00:40:44 +01:00
std::istringstream output(output_.out);
2022-04-06 08:37:19 +02:00
std::string line;
2019-04-15 10:18:27 +02:00
class_.clear();
2018-11-01 00:40:44 +01:00
while (getline(output, line)) {
auto parsed = parser_.parse(line);
2019-03-01 17:02:50 +01:00
if (config_["escape"].isBool() && config_["escape"].asBool()) {
text_ = Glib::Markup::escape_text(parsed["text"].asString());
} else {
text_ = parsed["text"].asString();
}
if (config_["escape"].isBool() && config_["escape"].asBool()) {
alt_ = Glib::Markup::escape_text(parsed["alt"].asString());
} else {
alt_ = parsed["alt"].asString();
}
2024-04-18 14:17:30 +00:00
if (config_["escape"].isBool() && config_["escape"].asBool()) {
tooltip_ = Glib::Markup::escape_text(parsed["tooltip"].asString());
} else {
tooltip_ = parsed["tooltip"].asString();
}
2019-04-15 10:18:27 +02:00
if (parsed["class"].isString()) {
class_.push_back(parsed["class"].asString());
} else if (parsed["class"].isArray()) {
2019-04-18 17:52:00 +02:00
for (auto const& c : parsed["class"]) {
2019-04-15 10:18:27 +02:00
class_.push_back(c.asString());
}
}
2023-01-13 22:39:59 +01:00
if (!parsed["percentage"].asString().empty() && parsed["percentage"].isNumeric()) {
2023-01-23 09:25:02 +01:00
percentage_ = (int)lround(parsed["percentage"].asFloat());
} else {
percentage_ = 0;
}
2018-11-01 00:40:44 +01:00
break;
}
}