fix(custom): close endless scripts

pull/124/head
Alexis 2018-12-08 12:58:47 +01:00
parent 7befd27059
commit 1b13f9e38c
2 changed files with 17 additions and 6 deletions

View File

@ -12,6 +12,7 @@ namespace waybar::modules {
class Custom : public ALabel {
public:
Custom(const std::string, const Json::Value&);
~Custom();
auto update() -> void;
private:
void delayWorker();
@ -27,6 +28,7 @@ class Custom : public ALabel {
waybar::util::SleeperThread thread_;
waybar::util::command::res output_;
waybar::util::JsonParser parser_;
FILE* fp_;
};
}

View File

@ -2,7 +2,7 @@
waybar::modules::Custom::Custom(const std::string name,
const Json::Value& config)
: ALabel(config, "{}"), name_(name)
: ALabel(config, "{}"), name_(name), fp_(nullptr)
{
if (config_["exec"].isString()) {
if (interval_.count() > 0) {
@ -15,6 +15,14 @@ waybar::modules::Custom::Custom(const std::string name,
}
}
waybar::modules::Custom::~Custom()
{
if (fp_) {
pclose(fp_);
fp_ = nullptr;
}
}
void waybar::modules::Custom::delayWorker()
{
thread_ = [this] {
@ -38,15 +46,16 @@ void waybar::modules::Custom::delayWorker()
void waybar::modules::Custom::continuousWorker()
{
auto cmd = config_["exec"].asString();
FILE* fp(popen(cmd.c_str(), "r"));
if (!fp) {
fp_ = popen(cmd.c_str(), "r");
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
thread_ = [this, fp] {
thread_ = [this] {
char* buff = nullptr;
size_t len = 0;
if (getline(&buff, &len, fp) == -1) {
pclose(fp);
if (getline(&buff, &len, fp_) == -1) {
pclose(fp_);
fp_ = nullptr;
thread_.stop();
output_ = { 1, "" };
std::cerr << name_ + " just stopped, is it endless?" << std::endl;