2018-08-10 14:26:46 +00:00
|
|
|
#include "modules/custom.hpp"
|
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::Custom::Custom(const std::string& name,
|
2018-08-20 12:50:45 +00:00
|
|
|
const Json::Value& config)
|
2018-12-08 11:58:47 +00:00
|
|
|
: ALabel(config, "{}"), name_(name), fp_(nullptr)
|
2018-08-10 14:26:46 +00:00
|
|
|
{
|
2018-12-18 16:30:54 +00:00
|
|
|
label_.set_name("custom-" + name_);
|
2018-11-24 16:21:46 +00:00
|
|
|
if (config_["exec"].isString()) {
|
|
|
|
if (interval_.count() > 0) {
|
|
|
|
delayWorker();
|
|
|
|
} else {
|
|
|
|
continuousWorker();
|
|
|
|
}
|
2018-09-18 21:15:37 +00:00
|
|
|
}
|
2018-12-18 16:30:54 +00:00
|
|
|
dp.emit();
|
2018-08-20 12:50:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 11:58:47 +00:00
|
|
|
waybar::modules::Custom::~Custom()
|
|
|
|
{
|
|
|
|
if (fp_) {
|
|
|
|
pclose(fp_);
|
|
|
|
fp_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 21:15:37 +00:00
|
|
|
void waybar::modules::Custom::delayWorker()
|
2018-08-20 12:50:45 +00:00
|
|
|
{
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_ = [this] {
|
2018-08-18 15:27:40 +00:00
|
|
|
bool can_update = true;
|
2018-10-26 07:27:16 +00:00
|
|
|
if (config_["exec-if"].isString()) {
|
2018-08-18 15:54:20 +00:00
|
|
|
auto res = waybar::util::command::exec(config_["exec-if"].asString());
|
|
|
|
if (res.exit_code != 0) {
|
2018-08-18 15:27:40 +00:00
|
|
|
can_update = false;
|
2018-12-18 16:30:54 +00:00
|
|
|
event_box_.hide();
|
2018-08-18 15:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_update) {
|
2018-09-05 17:20:19 +00:00
|
|
|
output_ = waybar::util::command::exec(config_["exec"].asString());
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2018-08-18 15:27:40 +00:00
|
|
|
}
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_.sleep_for(interval_);
|
2018-08-10 14:26:46 +00:00
|
|
|
};
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-10 14:26:46 +00:00
|
|
|
|
2018-09-18 21:15:37 +00:00
|
|
|
void waybar::modules::Custom::continuousWorker()
|
|
|
|
{
|
|
|
|
auto cmd = config_["exec"].asString();
|
2018-12-08 11:58:47 +00:00
|
|
|
fp_ = popen(cmd.c_str(), "r");
|
|
|
|
if (!fp_) {
|
2018-09-18 21:15:37 +00:00
|
|
|
throw std::runtime_error("Unable to open " + cmd);
|
|
|
|
}
|
2018-12-08 11:58:47 +00:00
|
|
|
thread_ = [this] {
|
2018-09-18 21:15:37 +00:00
|
|
|
char* buff = nullptr;
|
|
|
|
size_t len = 0;
|
2018-12-08 11:58:47 +00:00
|
|
|
if (getline(&buff, &len, fp_) == -1) {
|
2019-02-16 08:48:27 +00:00
|
|
|
int exit_code = 1;
|
2018-12-08 12:57:56 +00:00
|
|
|
if (fp_) {
|
2019-02-16 08:48:27 +00:00
|
|
|
exit_code = WEXITSTATUS(pclose(fp_));
|
2018-12-08 12:57:56 +00:00
|
|
|
fp_ = nullptr;
|
|
|
|
}
|
2018-09-18 21:15:37 +00:00
|
|
|
thread_.stop();
|
2019-02-16 08:48:27 +00:00
|
|
|
if (exit_code != 0) {
|
|
|
|
output_ = { exit_code, "" };
|
|
|
|
dp.emit();
|
|
|
|
std::cerr << name_ + " just stopped unexpectedly, is it endless?" << std::endl;
|
|
|
|
}
|
2018-09-18 21:15:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string output = buff;
|
|
|
|
|
|
|
|
// Remove last newline
|
|
|
|
if (!output.empty() && output[output.length()-1] == '\n') {
|
|
|
|
output.erase(output.length()-1);
|
|
|
|
}
|
|
|
|
output_ = { 0, output };
|
|
|
|
dp.emit();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:46:44 +00:00
|
|
|
void waybar::modules::Custom::refresh(int sig /*signal*/)
|
|
|
|
{
|
|
|
|
if(sig == SIGRTMIN + config_["signal"].asInt()) {
|
|
|
|
thread_.wake_up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 14:26:46 +00:00
|
|
|
auto waybar::modules::Custom::update() -> void
|
|
|
|
{
|
|
|
|
// Hide label if output is empty
|
2018-11-24 16:21:46 +00:00
|
|
|
if (config_["exec"].isString() && (output_.out.empty() || output_.exit_code != 0)) {
|
2018-12-18 16:30:54 +00:00
|
|
|
event_box_.hide();
|
2018-08-10 15:05:12 +00:00
|
|
|
} else {
|
2018-10-31 23:40:44 +00:00
|
|
|
if (config_["return-type"].asString() == "json") {
|
|
|
|
parseOutputJson();
|
|
|
|
} else {
|
|
|
|
parseOutputRaw();
|
|
|
|
}
|
|
|
|
|
2018-12-26 02:52:05 +00:00
|
|
|
auto str = fmt::format(format_, text_,
|
2019-02-25 21:04:09 +00:00
|
|
|
fmt::arg("alt", alt_),
|
2019-03-25 23:19:55 +00:00
|
|
|
fmt::arg("icon", getIcon(percentage_, alt_)),
|
2018-12-26 02:52:05 +00:00
|
|
|
fmt::arg("percentage", percentage_));
|
2018-11-21 19:49:09 +00:00
|
|
|
label_.set_markup(str);
|
2019-02-22 10:35:26 +00:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (text_ == tooltip_) {
|
|
|
|
label_.set_tooltip_text(str);
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_text(tooltip_);
|
|
|
|
}
|
2018-10-30 20:28:31 +00:00
|
|
|
}
|
|
|
|
if (class_ != "") {
|
|
|
|
if (prevclass_ != "") {
|
|
|
|
label_.get_style_context()->remove_class(prevclass_);
|
|
|
|
}
|
|
|
|
label_.get_style_context()->add_class(class_);
|
|
|
|
prevclass_ = class_;
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class(prevclass_);
|
|
|
|
prevclass_ = "";
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
event_box_.show();
|
2018-08-10 14:26:46 +00:00
|
|
|
}
|
2018-10-30 20:28:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 23:40:44 +00:00
|
|
|
void waybar::modules::Custom::parseOutputRaw()
|
2018-10-30 20:28:31 +00:00
|
|
|
{
|
|
|
|
std::istringstream output(output_.out);
|
|
|
|
std::string line;
|
|
|
|
int i = 0;
|
|
|
|
while (getline(output, line)) {
|
|
|
|
if (i == 0) {
|
2019-03-01 16:02:50 +00:00
|
|
|
if (config_["escape"].isBool() && config_["escape"].asBool()) {
|
|
|
|
text_ = Glib::Markup::escape_text(line);
|
|
|
|
} else {
|
|
|
|
text_ = line;
|
|
|
|
}
|
2018-10-30 20:28:31 +00:00
|
|
|
tooltip_ = line;
|
|
|
|
class_ = "";
|
|
|
|
} else if (i == 1) {
|
|
|
|
tooltip_ = line;
|
|
|
|
} else if (i == 2) {
|
|
|
|
class_ = line;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2018-10-31 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::Custom::parseOutputJson()
|
|
|
|
{
|
|
|
|
std::istringstream output(output_.out);
|
|
|
|
std::string line;
|
|
|
|
while (getline(output, line)) {
|
|
|
|
auto parsed = parser_.parse(line);
|
2019-03-01 16:02:50 +00: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-10-31 23:40:44 +00:00
|
|
|
tooltip_ = parsed["tooltip"].asString();
|
|
|
|
class_ = parsed["class"].asString();
|
2018-12-26 10:35:58 +00:00
|
|
|
if (!parsed["percentage"].asString().empty() && parsed["percentage"].isUInt()) {
|
|
|
|
percentage_ = parsed["percentage"].asUInt();
|
2018-12-26 02:52:05 +00:00
|
|
|
} else {
|
|
|
|
percentage_ = 0;
|
|
|
|
}
|
2018-10-31 23:40:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-24 16:21:46 +00:00
|
|
|
}
|