2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-25 07:42:31 +00:00
|
|
|
#include <boost/optional.hpp>
|
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"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "utils/env.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "utils/string.hpp"
|
2016-11-02 19:22:45 +00:00
|
|
|
#include "x11/xresources.hpp"
|
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
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
#define GET_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name, var)
|
|
|
|
#define REQ_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name)
|
|
|
|
|
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-01 06:54:45 +00:00
|
|
|
static constexpr const char* KEY_INHERIT{"inherit"};
|
|
|
|
|
2016-11-18 16:40:16 +00:00
|
|
|
explicit config(const logger& logger, const xresource_manager& xrm) : m_logger(logger), m_xrm(xrm) {}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
void load(string file, string barname);
|
|
|
|
string filepath() const;
|
|
|
|
string bar_section() const;
|
|
|
|
vector<string> defined_bars() 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
|
|
|
|
*/
|
|
|
|
template <typename T>
|
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
|
|
|
}
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
/**
|
|
|
|
* Get parameter for the current bar by name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-13 17:24:01 +00:00
|
|
|
T get(const string& key) const {
|
2016-06-15 03:32:35 +00:00
|
|
|
return get<T>(bar_section(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of a variable by section and parameter name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-13 17:24:01 +00:00
|
|
|
T get(const string& section, const string& key) const {
|
|
|
|
optional<T> value{opt<T>(section, key)};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
if (value == none) {
|
2016-10-11 06:18:25 +00:00
|
|
|
throw key_error("Missing parameter [" + section + "." + key + "]");
|
2016-12-13 17:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string string_value{opt<string>(section, key).get()};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
if (!string_value.empty()) {
|
|
|
|
return dereference<T>(section, key, opt<string>(section, key).get(), value.get());
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
return move(value.get());
|
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
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-13 17:24:01 +00:00
|
|
|
T get(const string& section, const string& key, const T& default_value) const {
|
|
|
|
optional<T> value{opt<T>(section, key)};
|
|
|
|
|
|
|
|
if (value == none) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string string_value{opt<string>(section, key).get()};
|
|
|
|
|
|
|
|
if (!string_value.empty()) {
|
|
|
|
return dereference<T>(section, key, opt<string>(section, key).get(), value.get());
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
return move(value.get());
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values for the current bar by name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-13 17:24:01 +00:00
|
|
|
vector<T> get_list(const string& key) const {
|
2016-06-15 03:32:35 +00:00
|
|
|
return get_list<T>(bar_section(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-12-13 17:24:01 +00:00
|
|
|
vector<T> get_list(const string& section, const string& key) const {
|
2016-06-15 03:32:35 +00:00
|
|
|
vector<T> vec;
|
2016-12-13 17:24:01 +00:00
|
|
|
optional<T> value;
|
|
|
|
|
|
|
|
while ((value = opt<T>(section, key + "-" + to_string(vec.size()))) != none) {
|
|
|
|
string string_value{get<string>(section, key + "-" + to_string(vec.size()))};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
if (!string_value.empty()) {
|
|
|
|
vec.emplace_back(dereference<T>(section, key, move(string_value), move(value.get())));
|
|
|
|
} else {
|
|
|
|
vec.emplace_back(move(value.get()));
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
2016-10-11 06:18:25 +00:00
|
|
|
throw key_error("Missing parameter [" + section + "." + key + "-0]");
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
* with a default list in case the list isn't defined
|
|
|
|
*/
|
|
|
|
template <typename T>
|
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-06-15 03:32:35 +00:00
|
|
|
vector<T> vec;
|
2016-12-13 17:24:01 +00:00
|
|
|
optional<T> value;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
while ((value = opt<T>(section, key + "-" + to_string(vec.size()))) != none) {
|
|
|
|
string string_value{get<string>(section, key + "-" + to_string(vec.size()))};
|
|
|
|
|
|
|
|
if (!string_value.empty()) {
|
|
|
|
vec.emplace_back(dereference<T>(section, key, move(string_value), move(value.get())));
|
|
|
|
} else {
|
|
|
|
vec.emplace_back(move(value.get()));
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
|
|
|
return default_value;
|
|
|
|
else
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-12-13 17:24:01 +00:00
|
|
|
void read();
|
|
|
|
void copy_inherited();
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T convert(string&& value) const;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const optional<T> opt(const string& section, const string& key) const {
|
|
|
|
sectionmap_t::const_iterator s;
|
|
|
|
valuemap_t::const_iterator v;
|
|
|
|
|
|
|
|
if ((s = m_sections.find(section)) != m_sections.end() && (v = s->second.find(key)) != s->second.end()) {
|
|
|
|
return {convert<T>(string{v->second})};
|
|
|
|
}
|
|
|
|
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
|
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-13 17:24:01 +00:00
|
|
|
T dereference(const string& section, const string& key, const string& var, 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) {
|
|
|
|
return dereference_env<T>(path.substr(4), fallback);
|
|
|
|
} else if (path.compare(0, 5, "xrdb:") == 0) {
|
|
|
|
return dereference_xrdb<T>(path.substr(5), fallback);
|
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 {
|
2016-12-13 17:24:01 +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}
|
|
|
|
* ${self.key}
|
|
|
|
* ${section.key}
|
|
|
|
*/
|
|
|
|
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-11-18 16:40:16 +00:00
|
|
|
m_logger.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-11-19 18:18:28 +00:00
|
|
|
section = string_util::replace(section, "BAR", bar_section(), 0, 3);
|
|
|
|
section = string_util::replace(section, "root", bar_section(), 0, 4);
|
|
|
|
section = string_util::replace(section, "self", current_section, 0, 4);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
optional<T> result{opt<T>(section, key)};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
if (result == none) {
|
|
|
|
throw value_error("Unexisting reference defined [" + section + "." + key + "]");
|
|
|
|
}
|
2016-10-19 07:16:08 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
return dereference<T>(section, key, opt<string>(section, key).get(), result.get());
|
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>
|
|
|
|
T dereference_env(string var, T fallback) const {
|
|
|
|
size_t pos;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-18 16:40:16 +00:00
|
|
|
if ((pos = var.find(":")) != string::npos) {
|
2016-12-13 17:24:01 +00:00
|
|
|
fallback = convert<T>(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())) {
|
|
|
|
return convert<T>(env_util::get(var.c_str()));
|
|
|
|
} else {
|
|
|
|
return fallback;
|
|
|
|
}
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dereference X resource db value defined using:
|
|
|
|
* ${xrdb:key}
|
|
|
|
* ${xrdb:key:fallback value}
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T dereference_xrdb(string var, T fallback) const {
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
if ((pos = var.find(":")) != string::npos) {
|
2016-12-13 17:24:01 +00:00
|
|
|
return convert<T>(m_xrm.get_string(var.substr(0, pos), var.substr(pos + 1)));
|
2016-11-18 16:40:16 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-13 17:24:01 +00:00
|
|
|
string str{m_xrm.get_string(var, "")};
|
|
|
|
return str.empty() ? fallback : convert<T>(move(str));
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const logger& m_logger;
|
2016-10-19 07:16:08 +00:00
|
|
|
const xresource_manager& m_xrm;
|
2016-06-15 03:32:35 +00:00
|
|
|
string m_file;
|
|
|
|
string m_current_bar;
|
2016-12-13 17:24:01 +00:00
|
|
|
sectionmap_t m_sections;
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
2016-10-24 23:55:59 +00:00
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Configure injection module
|
|
|
|
*/
|
2016-12-05 19:41:00 +00:00
|
|
|
inline const config& make_confreader() {
|
|
|
|
shared_ptr<config> instance = factory_util::singleton<config>(make_logger(), make_xresource_manager());
|
|
|
|
return static_cast<config&>(*instance);
|
2016-10-24 23:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|