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