| 64 | } |
| 65 | |
| 66 | void waybar::modules::Custom::continuousWorker() { |
| 67 | auto cmd = config_["exec"].asString(); |
| 68 | pid_ = -1; |
| 69 | fp_ = util::command::open(cmd, pid_, output_name_); |
| 70 | if (!fp_) { |
| 71 | throw std::runtime_error("Unable to open " + cmd); |
| 72 | } |
| 73 | thread_ = [this, cmd] { |
| 74 | char* buff = nullptr; |
| 75 | waybar::util::ScopeGuard buff_deleter([&buff]() { |
| 76 | if (buff) { |
| 77 | free(buff); |
| 78 | } |
| 79 | }); |
| 80 | size_t len = 0; |
| 81 | if (getline(&buff, &len, fp_) == -1) { |
| 82 | int exit_code = 1; |
| 83 | if (fp_) { |
| 84 | exit_code = WEXITSTATUS(util::command::close(fp_, pid_)); |
| 85 | fp_ = nullptr; |
| 86 | } |
| 87 | if (exit_code != 0) { |
| 88 | output_ = {exit_code, ""}; |
| 89 | dp.emit(); |
| 90 | spdlog::error("{} stopped unexpectedly, is it endless?", name_); |
| 91 | } |
| 92 | if (config_["restart-interval"].isNumeric()) { |
| 93 | pid_ = -1; |
| 94 | thread_.sleep_for(std::chrono::milliseconds( |
| 95 | std::max(1L, // Minimum 1ms due to millisecond precision |
| 96 | static_cast<long>(config_["restart-interval"].asDouble() * 1000)))); |
| 97 | fp_ = util::command::open(cmd, pid_, output_name_); |
| 98 | if (!fp_) { |
| 99 | throw std::runtime_error("Unable to open " + cmd); |
| 100 | } |
| 101 | } else { |
| 102 | thread_.stop(); |
| 103 | return; |
| 104 | } |
| 105 | } else { |
| 106 | std::string output = buff; |
| 107 | |
| 108 | // Remove last newline |
| 109 | if (!output.empty() && output[output.length() - 1] == '\n') { |
| 110 | output.erase(output.length() - 1); |
| 111 | } |
| 112 | output_ = {0, output}; |
| 113 | dp.emit(); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | void waybar::modules::Custom::waitingWorker() { |
| 119 | thread_ = [this] { |