2018-08-10 14:26:46 +00:00
|
|
|
#include "modules/custom.hpp"
|
|
|
|
|
2018-08-21 08:50:09 +00:00
|
|
|
waybar::modules::Custom::Custom(const std::string name,
|
2018-08-20 12:50:45 +00:00
|
|
|
const Json::Value& config)
|
2018-08-26 23:36:25 +00:00
|
|
|
: ALabel(config, "{}"), name_(name)
|
2018-08-10 14:26:46 +00:00
|
|
|
{
|
2018-08-16 12:29:41 +00:00
|
|
|
if (!config_["exec"]) {
|
|
|
|
throw std::runtime_error(name_ + " has no exec path.");
|
|
|
|
}
|
2018-09-18 21:15:37 +00:00
|
|
|
if (config_["interval"]) {
|
|
|
|
delayWorker();
|
|
|
|
} else {
|
|
|
|
continuousWorker();
|
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 21:15:37 +00:00
|
|
|
void waybar::modules::Custom::delayWorker()
|
2018-08-20 12:50:45 +00:00
|
|
|
{
|
2018-09-18 21:15:37 +00:00
|
|
|
auto interval = config_["interval"].asUInt();
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_ = [this, interval] {
|
2018-08-18 15:27:40 +00:00
|
|
|
bool can_update = true;
|
|
|
|
if (config_["exec-if"]) {
|
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-08-26 19:41:34 +00:00
|
|
|
label_.hide();
|
2018-08-28 09:10:36 +00:00
|
|
|
label_.set_name("");
|
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-08-16 12:29:41 +00:00
|
|
|
thread_.sleep_for(chrono::seconds(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();
|
|
|
|
FILE* fp(popen(cmd.c_str(), "r"));
|
|
|
|
if (!fp) {
|
|
|
|
throw std::runtime_error("Unable to open " + cmd);
|
|
|
|
}
|
|
|
|
thread_ = [this, fp] {
|
|
|
|
char* buff = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
if (getline(&buff, &len, fp) == -1) {
|
2018-09-18 21:21:08 +00:00
|
|
|
pclose(fp);
|
2018-09-18 21:15:37 +00:00
|
|
|
thread_.stop();
|
|
|
|
output_ = { 1, "" };
|
|
|
|
dp.emit();
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-10 14:26:46 +00:00
|
|
|
auto waybar::modules::Custom::update() -> void
|
|
|
|
{
|
|
|
|
// Hide label if output is empty
|
2018-09-05 17:20:19 +00:00
|
|
|
if (output_.out.empty() || output_.exit_code != 0) {
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.hide();
|
2018-08-28 09:10:36 +00:00
|
|
|
label_.set_name("");
|
2018-08-10 15:05:12 +00:00
|
|
|
} else {
|
2018-08-28 09:10:36 +00:00
|
|
|
label_.set_name("custom-" + name_);
|
2018-09-05 17:20:19 +00:00
|
|
|
auto str = fmt::format(format_, output_.out);
|
2018-08-16 15:09:51 +00:00
|
|
|
label_.set_text(str);
|
|
|
|
label_.set_tooltip_text(str);
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.show();
|
2018-08-10 14:26:46 +00:00
|
|
|
}
|
2018-09-05 17:20:19 +00:00
|
|
|
}
|