2018-08-10 16:26:46 +02:00
|
|
|
#include "modules/custom.hpp"
|
2019-05-19 01:44:45 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
2018-08-10 16:26:46 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
waybar::modules::Custom::Custom(const std::string& name, const Json::Value& config)
|
2019-04-23 15:56:38 +02:00
|
|
|
: ALabel(config, "{}"), name_(name), fp_(nullptr), pid_(-1) {
|
2018-12-18 17:30:54 +01:00
|
|
|
label_.set_name("custom-" + name_);
|
2018-11-24 17:21:46 +01:00
|
|
|
if (config_["exec"].isString()) {
|
|
|
|
if (interval_.count() > 0) {
|
|
|
|
delayWorker();
|
|
|
|
} else {
|
|
|
|
continuousWorker();
|
|
|
|
}
|
2018-09-18 23:15:37 +02:00
|
|
|
}
|
2018-12-18 17:30:54 +01:00
|
|
|
dp.emit();
|
2018-08-20 14:50:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
waybar::modules::Custom::~Custom() {
|
2019-04-23 15:56:38 +02:00
|
|
|
if (pid_ != -1) {
|
|
|
|
kill(-pid_, 9);
|
|
|
|
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()) {
|
2018-08-18 17:54:20 +02:00
|
|
|
auto res = waybar::util::command::exec(config_["exec-if"].asString());
|
|
|
|
if (res.exit_code != 0) {
|
2018-08-18 17:27:40 +02:00
|
|
|
can_update = false;
|
2018-12-18 17:30:54 +01:00
|
|
|
event_box_.hide();
|
2018-08-18 17:27:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_update) {
|
2018-09-05 19:20:19 +02:00
|
|
|
output_ = waybar::util::command::exec(config_["exec"].asString());
|
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_);
|
2018-08-10 16:26:46 +02:00
|
|
|
};
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-10 16:26:46 +02:00
|
|
|
|
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();
|
2019-04-23 15:56:38 +02:00
|
|
|
pid_ = -1;
|
|
|
|
fp_ = util::command::open(cmd, pid_);
|
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);
|
|
|
|
}
|
2019-04-23 15:56:38 +02:00
|
|
|
thread_ = [&] {
|
2019-04-18 17:52:00 +02:00
|
|
|
char* buff = nullptr;
|
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) {
|
2019-02-16 09:48:27 +01:00
|
|
|
int exit_code = 1;
|
2018-12-08 13:57:56 +01:00
|
|
|
if (fp_) {
|
2019-04-23 15:56:38 +02:00
|
|
|
exit_code = WEXITSTATUS(util::command::close(fp_, pid_));
|
2018-12-08 13:57:56 +01:00
|
|
|
fp_ = nullptr;
|
|
|
|
}
|
2018-09-18 23:15:37 +02:00
|
|
|
thread_.stop();
|
2019-02-16 09:48:27 +01:00
|
|
|
if (exit_code != 0) {
|
2019-04-18 17:52:00 +02:00
|
|
|
output_ = {exit_code, ""};
|
2019-02-16 09:48:27 +01:00
|
|
|
dp.emit();
|
2019-05-19 01:44:45 +02:00
|
|
|
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
|
2019-02-16 09:48:27 +01:00
|
|
|
}
|
2018-09-18 23:15:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string output = buff;
|
|
|
|
|
|
|
|
// Remove last newline
|
2019-04-18 17:52:00 +02:00
|
|
|
if (!output.empty() && output[output.length() - 1] == '\n') {
|
|
|
|
output.erase(output.length() - 1);
|
2018-09-18 23:15:37 +02:00
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
output_ = {0, output};
|
2018-09-18 23:15:37 +02:00
|
|
|
dp.emit();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Custom::refresh(int sig /*signal*/) {
|
|
|
|
if (sig == SIGRTMIN + config_["signal"].asInt()) {
|
2019-03-18 18:46:44 +01:00
|
|
|
thread_.wake_up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:07:55 +02:00
|
|
|
bool waybar::modules::Custom::handleScroll(GdkEventScroll* e) {
|
|
|
|
auto ret = ALabel::handleScroll(e);
|
|
|
|
thread_.wake_up();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool waybar::modules::Custom::handleToggle(GdkEventButton* const& e) {
|
|
|
|
auto ret = ALabel::handleToggle(e);
|
|
|
|
thread_.wake_up();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto waybar::modules::Custom::update() -> void {
|
2018-08-10 16:26:46 +02:00
|
|
|
// Hide label if output is empty
|
2018-11-24 17:21:46 +01:00
|
|
|
if (config_["exec"].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();
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto str = fmt::format(format_,
|
|
|
|
text_,
|
|
|
|
fmt::arg("alt", alt_),
|
|
|
|
fmt::arg("icon", getIcon(percentage_, alt_)),
|
|
|
|
fmt::arg("percentage", percentage_));
|
2018-11-21 20:49:09 +01:00
|
|
|
label_.set_markup(str);
|
2019-02-22 11:35:26 +01:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (text_ == tooltip_) {
|
|
|
|
label_.set_tooltip_text(str);
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_text(tooltip_);
|
|
|
|
}
|
2018-10-30 21:28:31 +01:00
|
|
|
}
|
2019-04-15 10:18:27 +02:00
|
|
|
auto classes = label_.get_style_context()->list_classes();
|
2019-04-18 17:52:00 +02:00
|
|
|
for (auto const& c : classes) {
|
2019-04-15 10:18:27 +02:00
|
|
|
label_.get_style_context()->remove_class(c);
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
for (auto const& c : class_) {
|
2019-04-15 10:18:27 +02:00
|
|
|
label_.get_style_context()->add_class(c);
|
2018-10-30 21:28:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
event_box_.show();
|
2018-08-10 16:26:46 +02:00
|
|
|
}
|
2018-10-30 21:28:31 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Custom::parseOutputRaw() {
|
2018-10-30 21:28:31 +01:00
|
|
|
std::istringstream output(output_.out);
|
2019-04-18 17:52:00 +02:00
|
|
|
std::string line;
|
|
|
|
int i = 0;
|
2018-10-30 21:28:31 +01:00
|
|
|
while (getline(output, line)) {
|
|
|
|
if (i == 0) {
|
2019-03-01 17:02:50 +01:00
|
|
|
if (config_["escape"].isBool() && config_["escape"].asBool()) {
|
|
|
|
text_ = Glib::Markup::escape_text(line);
|
|
|
|
} else {
|
|
|
|
text_ = line;
|
|
|
|
}
|
2018-10-30 21:28:31 +01:00
|
|
|
tooltip_ = line;
|
2019-04-15 10:18:27 +02:00
|
|
|
class_.clear();
|
2018-10-30 21:28:31 +01:00
|
|
|
} else if (i == 1) {
|
|
|
|
tooltip_ = line;
|
|
|
|
} else if (i == 2) {
|
2019-04-15 10:18:27 +02:00
|
|
|
class_.push_back(line);
|
2018-10-30 21:28:31 +01:00
|
|
|
} 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);
|
2019-04-18 17:52:00 +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();
|
|
|
|
}
|
2018-11-01 00:40:44 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 11:35:58 +01:00
|
|
|
if (!parsed["percentage"].asString().empty() && parsed["percentage"].isUInt()) {
|
|
|
|
percentage_ = parsed["percentage"].asUInt();
|
2018-12-26 03:52:05 +01:00
|
|
|
} else {
|
|
|
|
percentage_ = 0;
|
|
|
|
}
|
2018-11-01 00:40:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-11-24 17:21:46 +01:00
|
|
|
}
|