2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
2017-01-11 02:07:28 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
#include <unordered_map>
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
2016-11-25 12:55:15 +00:00
|
|
|
#include "errors.hpp"
|
2017-01-20 01:25:54 +00:00
|
|
|
#include "settings.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "utils/env.hpp"
|
2016-12-19 21:01:37 +00:00
|
|
|
#include "utils/file.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "utils/string.hpp"
|
2017-01-26 16:17:02 +00:00
|
|
|
#if WITH_XRM
|
2016-11-02 19:22:45 +00:00
|
|
|
#include "x11/xresources.hpp"
|
2017-01-20 01:25:54 +00:00
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
DEFINE_ERROR(value_error);
|
|
|
|
DEFINE_ERROR(key_error);
|
|
|
|
|
|
|
|
class config {
|
|
|
|
public:
|
2016-12-13 17:24:01 +00:00
|
|
|
using valuemap_t = std::unordered_map<string, string>;
|
|
|
|
using sectionmap_t = std::unordered_map<string, valuemap_t>;
|
|
|
|
|
2016-12-09 08:40:46 +00:00
|
|
|
using make_type = const config&;
|
2016-12-09 10:32:41 +00:00
|
|
|
static make_type make(string path = "", string bar = "");
|
2016-12-01 06:54:45 +00:00
|
|
|
|
2017-01-20 01:25:54 +00:00
|
|
|
explicit config(const logger& logger, string&& path = "", string&& bar = "");
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
string filepath() const;
|
2016-12-14 04:12:15 +00:00
|
|
|
string section() const;
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
void warn_deprecated(const string& section, const string& key, string replacement) const;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-29 14:53:32 +00:00
|
|
|
/**
|
|
|
|
* Returns true if a given parameter exists
|
|
|
|
*/
|
2016-12-13 17:24:01 +00:00
|
|
|
bool has(const string& section, const string& key) const {
|
|
|
|
auto it = m_sections.find(section);
|
|
|
|
return it != m_sections.end() && it->second.find(key) != it->second.end();
|
2016-11-29 14:53:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 19:03:08 +00:00
|
|
|
/**
|
|
|
|
* Set parameter value
|
|
|
|
*/
|
|
|
|
void set(const string& section, const string& key, string&& value) {
|
|
|
|
auto it = m_sections.find(section);
|
|
|
|
if (it == m_sections.end()) {
|
|
|
|
valuemap_t values;
|
|
|
|
values[key] = value;
|
|
|
|
m_sections[section] = move(values);
|
|
|
|
}
|
|
|
|
auto it2 = it->second.find(key);
|
|
|
|
if ((it2 = it->second.find(key)) == it->second.end()) {
|
|
|
|
it2 = it->second.emplace_hint(it2, key, value);
|
|
|
|
} else {
|
|
|
|
it2->second = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
/**
|
|
|
|
* Get parameter for the current bar by name
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-13 17:24:01 +00:00
|
|
|
T get(const string& key) const {
|
2016-12-14 04:12:15 +00:00
|
|
|
return get<T>(section(), key);
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of a variable by section and parameter name
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-13 17:24:01 +00:00
|
|
|
T get(const string& section, const string& key) const {
|
2016-12-15 16:13:15 +00:00
|
|
|
auto it = m_sections.find(section);
|
|
|
|
if (it == m_sections.end() || it->second.find(key) == it->second.end()) {
|
2017-01-26 19:10:33 +00:00
|
|
|
throw key_error("Missing parameter \"" + section + "." + key + "\"");
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
2016-12-15 16:13:15 +00:00
|
|
|
return dereference<T>(section, key, it->second.at(key), convert<T>(string{it->second.at(key)}));
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of a variable by section and parameter name
|
|
|
|
* with a default value in case the parameter isn't defined
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-13 17:24:01 +00:00
|
|
|
T get(const string& section, const string& key, const T& default_value) const {
|
2016-12-15 16:13:15 +00:00
|
|
|
try {
|
|
|
|
string string_value{get<string>(section, key)};
|
2016-12-15 19:57:03 +00:00
|
|
|
T result{convert<T>(string{string_value})};
|
2016-12-15 16:13:15 +00:00
|
|
|
return dereference<T>(move(section), move(key), move(string_value), move(result));
|
|
|
|
} catch (const key_error& err) {
|
2016-12-13 17:24:01 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values for the current bar by name
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-13 17:24:01 +00:00
|
|
|
vector<T> get_list(const string& key) const {
|
2016-12-14 04:12:15 +00:00
|
|
|
return get_list<T>(section(), key);
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-13 17:24:01 +00:00
|
|
|
vector<T> get_list(const string& section, const string& key) const {
|
2016-12-15 16:13:15 +00:00
|
|
|
vector<T> results;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
string string_value{get<string>(section, key + "-" + to_string(results.size()))};
|
2016-12-15 19:57:03 +00:00
|
|
|
T value{convert<T>(string{string_value})};
|
2016-12-15 16:13:15 +00:00
|
|
|
|
|
|
|
if (!string_value.empty()) {
|
|
|
|
results.emplace_back(dereference<T>(section, key, move(string_value), move(value)));
|
|
|
|
} else {
|
|
|
|
results.emplace_back(move(value));
|
|
|
|
}
|
|
|
|
} catch (const key_error& err) {
|
|
|
|
break;
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 16:13:15 +00:00
|
|
|
if (results.empty()) {
|
2017-01-26 19:10:33 +00:00
|
|
|
throw key_error("Missing parameter \"" + section + "." + key + "-0\"");
|
2016-12-15 16:13:15 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-15 16:13:15 +00:00
|
|
|
return results;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
* with a default list in case the list isn't defined
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-13 17:24:01 +00:00
|
|
|
vector<T> get_list(const string& section, const string& key, const vector<T>& default_value) const {
|
2016-12-15 16:13:15 +00:00
|
|
|
vector<T> results;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
string string_value{get<string>(section, key + "-" + to_string(results.size()))};
|
2016-12-15 19:57:03 +00:00
|
|
|
T value{convert<T>(string{string_value})};
|
2016-12-15 16:13:15 +00:00
|
|
|
|
|
|
|
if (!string_value.empty()) {
|
|
|
|
results.emplace_back(dereference<T>(section, key, move(string_value), move(value)));
|
|
|
|
} else {
|
|
|
|
results.emplace_back(move(value));
|
|
|
|
}
|
|
|
|
} catch (const key_error& err) {
|
|
|
|
break;
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 16:13:15 +00:00
|
|
|
if (!results.empty()) {
|
|
|
|
return results;
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_value;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 04:12:15 +00:00
|
|
|
/**
|
|
|
|
* Attempt to load value using the deprecated key name. If successful show a
|
|
|
|
* warning message. If it fails load the value using the new key and given
|
|
|
|
* fallback value
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-14 04:12:15 +00:00
|
|
|
T deprecated(const string& section, const string& old, const string& newkey, const T& fallback) const {
|
|
|
|
try {
|
|
|
|
T value{get<T>(section, old)};
|
|
|
|
warn_deprecated(section, old, newkey);
|
|
|
|
return value;
|
|
|
|
} catch (const key_error& err) {
|
|
|
|
return get<T>(section, newkey, fallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see deprecated<T>
|
|
|
|
*/
|
2016-12-30 22:32:05 +00:00
|
|
|
template <typename T = string>
|
2016-12-14 04:12:15 +00:00
|
|
|
T deprecated_list(const string& section, const string& old, const string& newkey, const vector<T>& fallback) const {
|
|
|
|
try {
|
|
|
|
vector<T> value{get_list<T>(section, old)};
|
|
|
|
warn_deprecated(section, old, newkey);
|
|
|
|
return value;
|
|
|
|
} catch (const key_error& err) {
|
|
|
|
return get_list<T>(section, newkey, fallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
protected:
|
2016-12-14 04:12:15 +00:00
|
|
|
void parse_file();
|
2016-12-13 17:24:01 +00:00
|
|
|
void copy_inherited();
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T convert(string&& value) const;
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
/**
|
2016-11-18 16:40:16 +00:00
|
|
|
* Dereference value reference
|
2016-06-15 03:32:35 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-14 19:14:31 +00:00
|
|
|
T dereference(const string& section, const string& key, const string& var, const T& fallback) const {
|
2016-11-18 16:40:16 +00:00
|
|
|
if (var.substr(0, 2) != "${" || var.substr(var.length() - 1) != "}") {
|
2016-12-13 17:24:01 +00:00
|
|
|
return fallback;
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-18 16:40:16 +00:00
|
|
|
auto path = var.substr(2, var.length() - 3);
|
|
|
|
size_t pos;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
if (path.compare(0, 4, "env:") == 0) {
|
2017-01-20 01:25:54 +00:00
|
|
|
return dereference_env<T>(path.substr(4));
|
2016-12-13 17:24:01 +00:00
|
|
|
} else if (path.compare(0, 5, "xrdb:") == 0) {
|
2017-01-20 01:25:54 +00:00
|
|
|
return dereference_xrdb<T>(path.substr(5));
|
2016-12-19 21:01:37 +00:00
|
|
|
} else if (path.compare(0, 5, "file:") == 0) {
|
2017-01-20 01:25:54 +00:00
|
|
|
return dereference_file<T>(path.substr(5));
|
2016-11-18 16:40:16 +00:00
|
|
|
} else if ((pos = path.find(".")) != string::npos) {
|
2016-11-19 18:18:28 +00:00
|
|
|
return dereference_local<T>(path.substr(0, pos), path.substr(pos + 1), section);
|
2016-11-18 16:40:16 +00:00
|
|
|
} else {
|
2017-01-26 19:10:33 +00:00
|
|
|
throw value_error("Invalid reference defined at \"" + section + "." + key + "\"");
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-18 16:40:16 +00:00
|
|
|
/**
|
|
|
|
* Dereference local value reference defined using:
|
|
|
|
* ${root.key}
|
2017-01-26 18:33:14 +00:00
|
|
|
* ${root.key:fallback}
|
2016-11-18 16:40:16 +00:00
|
|
|
* ${self.key}
|
2017-01-26 18:33:14 +00:00
|
|
|
* ${self.key:fallback}
|
2016-11-18 16:40:16 +00:00
|
|
|
* ${section.key}
|
2017-01-26 18:33:14 +00:00
|
|
|
* ${section.key:fallback}
|
2016-11-18 16:40:16 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-13 17:24:01 +00:00
|
|
|
T dereference_local(string section, const string& key, const string& current_section) const {
|
|
|
|
if (section == "BAR") {
|
2016-12-15 16:13:15 +00:00
|
|
|
m_log.warn("${BAR.key} is deprecated. Use ${root.key} instead");
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
2016-10-19 07:16:08 +00:00
|
|
|
|
2016-12-14 04:12:15 +00:00
|
|
|
section = string_util::replace(section, "BAR", this->section(), 0, 3);
|
|
|
|
section = string_util::replace(section, "root", this->section(), 0, 4);
|
2016-11-19 18:18:28 +00:00
|
|
|
section = string_util::replace(section, "self", current_section, 0, 4);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-15 16:13:15 +00:00
|
|
|
try {
|
2016-12-15 19:57:03 +00:00
|
|
|
string string_value{get<string>(section, key)};
|
|
|
|
T result{convert<T>(string{string_value})};
|
|
|
|
return dereference<T>(string(section), move(key), move(string_value), move(result));
|
2016-12-15 16:13:15 +00:00
|
|
|
} catch (const key_error& err) {
|
2017-01-26 18:33:14 +00:00
|
|
|
size_t pos;
|
|
|
|
if ((pos = key.find(':')) != string::npos) {
|
|
|
|
string fallback = key.substr(pos + 1);
|
2017-01-26 19:10:33 +00:00
|
|
|
m_log.info("The reference ${%s.%s} does not exist, using defined fallback value \"%s\"", section,
|
|
|
|
key.substr(0, pos), fallback);
|
2017-01-26 18:33:14 +00:00
|
|
|
return convert<T>(move(fallback));
|
|
|
|
}
|
|
|
|
throw value_error("The reference ${" + section + "." + key + "} does not exist (no fallback set)");
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
2016-10-19 07:16:08 +00:00
|
|
|
|
2016-11-18 16:40:16 +00:00
|
|
|
/**
|
|
|
|
* Dereference environment variable reference defined using:
|
|
|
|
* ${env:key}
|
|
|
|
* ${env:key:fallback value}
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-20 01:25:54 +00:00
|
|
|
T dereference_env(string var) const {
|
2016-11-18 16:40:16 +00:00
|
|
|
size_t pos;
|
2017-01-20 01:25:54 +00:00
|
|
|
string env_default;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2017-01-26 18:33:14 +00:00
|
|
|
if ((pos = var.find(':')) != string::npos) {
|
2016-12-14 19:14:31 +00:00
|
|
|
env_default = var.substr(pos + 1);
|
2016-11-18 16:40:16 +00:00
|
|
|
var.erase(pos);
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
if (env_util::has(var.c_str())) {
|
2016-12-14 19:14:31 +00:00
|
|
|
string env_value{env_util::get(var.c_str())};
|
2017-01-20 01:31:55 +00:00
|
|
|
m_log.info("Environment var reference ${%s} found (value=%s)", var, env_value);
|
2016-12-14 19:14:31 +00:00
|
|
|
return convert<T>(move(env_value));
|
2016-12-15 16:13:15 +00:00
|
|
|
} else if (!env_default.empty()) {
|
2017-01-20 01:31:55 +00:00
|
|
|
m_log.info("Environment var ${%s} is undefined, using defined fallback value \"%s\"", var, env_default);
|
|
|
|
return convert<T>(move(env_default));
|
2016-12-13 17:24:01 +00:00
|
|
|
} else {
|
2017-01-20 01:31:55 +00:00
|
|
|
throw value_error(sstream() << "Environment var ${" << var << "} does not exist (no fallback set)");
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dereference X resource db value defined using:
|
|
|
|
* ${xrdb:key}
|
|
|
|
* ${xrdb:key:fallback value}
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-20 01:25:54 +00:00
|
|
|
T dereference_xrdb(string var) const {
|
2016-11-18 16:40:16 +00:00
|
|
|
size_t pos;
|
2017-01-26 16:17:02 +00:00
|
|
|
#if not WITH_XRM
|
2017-01-20 01:25:54 +00:00
|
|
|
m_log.warn("No built-in support for xrdb (requires xcb-util-xrm). Using default value for `%s`", var);
|
2017-01-26 18:33:14 +00:00
|
|
|
if ((pos = var.find(':')) != string::npos) {
|
2017-01-26 16:17:02 +00:00
|
|
|
return convert<T>(var.substr(pos + 1));
|
2017-01-20 01:25:54 +00:00
|
|
|
}
|
2017-01-26 16:17:02 +00:00
|
|
|
return convert<T>("");
|
2017-01-20 01:25:54 +00:00
|
|
|
#else
|
|
|
|
if (!m_xrm) {
|
|
|
|
throw application_error("xrm is not initialized");
|
|
|
|
}
|
2016-11-18 16:40:16 +00:00
|
|
|
|
2017-01-20 01:25:54 +00:00
|
|
|
string fallback;
|
2017-01-26 18:33:14 +00:00
|
|
|
if ((pos = var.find(':')) != string::npos) {
|
2017-01-20 01:25:54 +00:00
|
|
|
fallback = var.substr(pos + 1);
|
|
|
|
var.erase(pos);
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2017-01-20 01:25:54 +00:00
|
|
|
try {
|
|
|
|
auto value = m_xrm->require<string>(var.c_str());
|
|
|
|
m_log.info("Found matching X resource \"%s\" (value=%s)", var, value);
|
|
|
|
return convert<T>(move(value));
|
|
|
|
} catch (const xresource_error& err) {
|
|
|
|
if (!fallback.empty()) {
|
|
|
|
m_log.warn("%s, using defined fallback value \"%s\"", err.what(), fallback);
|
|
|
|
return convert<T>(move(fallback));
|
|
|
|
}
|
|
|
|
throw value_error(sstream() << err.what() << " (no fallback set)");
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 21:01:37 +00:00
|
|
|
/**
|
|
|
|
* Dereference file reference by reading its contents
|
|
|
|
* ${file:/absolute/file/path}
|
2017-01-20 01:31:55 +00:00
|
|
|
* ${file:/absolute/file/path:fallback value}
|
2016-12-19 21:01:37 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-20 01:25:54 +00:00
|
|
|
T dereference_file(string var) const {
|
|
|
|
size_t pos;
|
|
|
|
string fallback;
|
2017-01-26 18:33:14 +00:00
|
|
|
if ((pos = var.find(':')) != string::npos) {
|
2017-01-20 01:25:54 +00:00
|
|
|
fallback = var.substr(pos + 1);
|
|
|
|
var.erase(pos);
|
2016-12-19 21:01:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 01:31:55 +00:00
|
|
|
if (file_util::exists(var)) {
|
|
|
|
m_log.info("File reference \"%s\" found", var);
|
|
|
|
return convert<T>(string_util::trim(file_util::contents(var), '\n'));
|
|
|
|
} else if (!fallback.empty()) {
|
|
|
|
m_log.warn("File reference \"%s\" not found, using defined fallback value \"%s\"", var, fallback);
|
|
|
|
return convert<T>(move(fallback));
|
|
|
|
} else {
|
|
|
|
throw value_error(sstream() << "The file \"" << var << "\" does not exist (no fallback set)");
|
|
|
|
}
|
2016-12-19 21:01:37 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
private:
|
2016-12-09 08:02:47 +00:00
|
|
|
static constexpr const char* KEY_INHERIT{"inherit"};
|
2016-12-15 16:13:15 +00:00
|
|
|
const logger& m_log;
|
2016-06-15 03:32:35 +00:00
|
|
|
string m_file;
|
2016-12-14 04:12:15 +00:00
|
|
|
string m_barname;
|
2016-12-15 02:30:41 +00:00
|
|
|
sectionmap_t m_sections{};
|
2017-01-26 16:17:02 +00:00
|
|
|
#if WITH_XRM
|
2017-01-20 01:25:54 +00:00
|
|
|
unique_ptr<xresource_manager> m_xrm;
|
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|