2021-08-13 13:08:27 +00:00
|
|
|
#include "config.hpp"
|
|
|
|
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
|
2022-06-25 10:16:40 +00:00
|
|
|
#include <filesystem>
|
2021-08-13 13:08:27 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "util/json.hpp"
|
|
|
|
|
2022-06-25 10:16:40 +00:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
2021-08-13 13:08:27 +00:00
|
|
|
namespace waybar {
|
|
|
|
|
2021-08-13 14:19:47 +00:00
|
|
|
const std::vector<std::string> Config::CONFIG_DIRS = {
|
2022-04-06 06:37:19 +00:00
|
|
|
"$XDG_CONFIG_HOME/waybar/", "$HOME/.config/waybar/", "$HOME/waybar/",
|
|
|
|
"/etc/xdg/waybar/", SYSCONFDIR "/xdg/waybar/", "./resources/",
|
2021-08-13 14:19:47 +00:00
|
|
|
};
|
|
|
|
|
2022-06-25 10:16:40 +00:00
|
|
|
const char *Config::CONFIG_PATH_ENV = "WAYBAR_CONFIG_DIR";
|
|
|
|
|
|
|
|
std::optional<std::string> tryExpandPath(const std::string base, const std::string filename) {
|
|
|
|
fs::path path;
|
|
|
|
|
|
|
|
if (filename != "") {
|
|
|
|
path = fs::path(base) / fs::path(filename);
|
|
|
|
} else {
|
|
|
|
path = fs::path(base);
|
|
|
|
}
|
|
|
|
|
2022-09-07 08:33:57 +00:00
|
|
|
spdlog::debug("Try expanding: {}", path.string());
|
2022-06-25 10:16:40 +00:00
|
|
|
|
2021-08-13 13:08:27 +00:00
|
|
|
wordexp_t p;
|
2021-08-13 14:19:47 +00:00
|
|
|
if (wordexp(path.c_str(), &p, 0) == 0) {
|
|
|
|
if (access(*p.we_wordv, F_OK) == 0) {
|
|
|
|
std::string result = *p.we_wordv;
|
|
|
|
wordfree(&p);
|
2022-09-07 08:33:57 +00:00
|
|
|
spdlog::debug("Found config file: {}", path.string());
|
2021-08-13 14:19:47 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
wordfree(&p);
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-08-13 13:08:27 +00:00
|
|
|
|
2021-08-13 14:19:47 +00:00
|
|
|
std::optional<std::string> Config::findConfigPath(const std::vector<std::string> &names,
|
|
|
|
const std::vector<std::string> &dirs) {
|
2022-06-25 10:16:40 +00:00
|
|
|
if (const char *dir = std::getenv(Config::CONFIG_PATH_ENV)) {
|
|
|
|
for (const auto &name : names) {
|
|
|
|
if (auto res = tryExpandPath(dir, name); res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:19:47 +00:00
|
|
|
for (const auto &dir : dirs) {
|
|
|
|
for (const auto &name : names) {
|
2022-06-25 10:16:40 +00:00
|
|
|
if (auto res = tryExpandPath(dir, name); res) {
|
2021-08-13 14:19:47 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
2021-08-13 14:19:47 +00:00
|
|
|
return std::nullopt;
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 05:16:37 +00:00
|
|
|
void Config::setupConfig(Json::Value &dst, const std::string &config_file, int depth) {
|
2021-08-13 13:08:27 +00:00
|
|
|
if (depth > 100) {
|
|
|
|
throw std::runtime_error("Aborting due to likely recursive include in config files");
|
|
|
|
}
|
|
|
|
std::ifstream file(config_file);
|
|
|
|
if (!file.is_open()) {
|
|
|
|
throw std::runtime_error("Can't open config file");
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
2021-08-13 13:08:27 +00:00
|
|
|
util::JsonParser parser;
|
2022-04-06 06:37:19 +00:00
|
|
|
Json::Value tmp_config = parser.parse(str);
|
2021-08-13 13:08:27 +00:00
|
|
|
if (tmp_config.isArray()) {
|
|
|
|
for (auto &config_part : tmp_config) {
|
|
|
|
resolveConfigIncludes(config_part, depth);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolveConfigIncludes(tmp_config, depth);
|
|
|
|
}
|
2021-09-14 05:16:37 +00:00
|
|
|
mergeConfig(dst, tmp_config);
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::resolveConfigIncludes(Json::Value &config, int depth) {
|
|
|
|
Json::Value includes = config["include"];
|
|
|
|
if (includes.isArray()) {
|
|
|
|
for (const auto &include : includes) {
|
|
|
|
spdlog::info("Including resource file: {}", include.asString());
|
2022-06-25 10:16:40 +00:00
|
|
|
setupConfig(config, tryExpandPath(include.asString(), "").value_or(""), ++depth);
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
} else if (includes.isString()) {
|
|
|
|
spdlog::info("Including resource file: {}", includes.asString());
|
2022-06-25 10:16:40 +00:00
|
|
|
setupConfig(config, tryExpandPath(includes.asString(), "").value_or(""), ++depth);
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::mergeConfig(Json::Value &a_config_, Json::Value &b_config_) {
|
|
|
|
if (!a_config_) {
|
|
|
|
// For the first config
|
|
|
|
a_config_ = b_config_;
|
|
|
|
} else if (a_config_.isObject() && b_config_.isObject()) {
|
|
|
|
for (const auto &key : b_config_.getMemberNames()) {
|
2021-09-15 15:14:12 +00:00
|
|
|
// [] creates key with default value. Use `get` to avoid that.
|
|
|
|
if (a_config_.get(key, Json::Value::nullSingleton()).isObject() &&
|
|
|
|
b_config_[key].isObject()) {
|
2021-08-13 13:08:27 +00:00
|
|
|
mergeConfig(a_config_[key], b_config_[key]);
|
2021-09-15 15:14:12 +00:00
|
|
|
} else if (!a_config_.isMember(key)) {
|
2021-09-14 05:16:37 +00:00
|
|
|
// do not allow overriding value set by top or previously included config
|
2021-08-13 13:08:27 +00:00
|
|
|
a_config_[key] = b_config_[key];
|
2021-09-15 15:14:12 +00:00
|
|
|
} else {
|
|
|
|
spdlog::trace("Option {} is already set; ignoring value {}", key, b_config_[key]);
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
spdlog::error("Cannot merge config, conflicting or invalid JSON types");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool isValidOutput(const Json::Value &config, const std::string &name,
|
|
|
|
const std::string &identifier) {
|
|
|
|
if (config["output"].isArray()) {
|
|
|
|
for (auto const &output_conf : config["output"]) {
|
2023-03-13 00:44:07 +00:00
|
|
|
if (output_conf.isString()) {
|
|
|
|
auto config_output = output_conf.asString();
|
|
|
|
if (config_output.substr(0, 1) == "!") {
|
|
|
|
if (config_output.substr(1) == name || config_output.substr(1) == identifier) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config_output == name || config_output == identifier) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (config_output.substr(0, 1) == "*") {
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (config["output"].isString()) {
|
|
|
|
auto config_output = config["output"].asString();
|
|
|
|
if (!config_output.empty()) {
|
|
|
|
if (config_output.substr(0, 1) == "!") {
|
|
|
|
return config_output.substr(1) != name && config_output.substr(1) != identifier;
|
|
|
|
}
|
|
|
|
return config_output == name || config_output == identifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:19:47 +00:00
|
|
|
void Config::load(const std::string &config) {
|
|
|
|
auto file = config.empty() ? findConfigPath({"config", "config.jsonc"}) : config;
|
|
|
|
if (!file) {
|
|
|
|
throw std::runtime_error("Missing required resource files");
|
|
|
|
}
|
|
|
|
config_file_ = file.value();
|
|
|
|
spdlog::info("Using configuration file {}", config_file_);
|
2024-03-24 19:39:15 +00:00
|
|
|
config_ = Json::Value();
|
2021-09-14 05:16:37 +00:00
|
|
|
setupConfig(config_, config_file_, 0);
|
2021-08-13 13:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Json::Value> Config::getOutputConfigs(const std::string &name,
|
|
|
|
const std::string &identifier) {
|
|
|
|
std::vector<Json::Value> configs;
|
|
|
|
if (config_.isArray()) {
|
|
|
|
for (auto const &config : config_) {
|
|
|
|
if (config.isObject() && isValidOutput(config, name, identifier)) {
|
|
|
|
configs.push_back(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isValidOutput(config_, name, identifier)) {
|
|
|
|
configs.push_back(config_);
|
|
|
|
}
|
|
|
|
return configs;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace waybar
|