2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2016-11-25 02:42:31 -05:00
|
|
|
#include <boost/optional.hpp>
|
2016-06-14 23:32:35 -04:00
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
2016-11-25 07:55:15 -05:00
|
|
|
#include "errors.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "utils/env.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "utils/string.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
#include "x11/xresources.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-15 07:15:56 -04: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-14 23:32:35 -04:00
|
|
|
using ptree = boost::property_tree::ptree;
|
|
|
|
|
|
|
|
DEFINE_ERROR(value_error);
|
|
|
|
DEFINE_ERROR(key_error);
|
|
|
|
|
|
|
|
class config {
|
|
|
|
public:
|
2016-11-18 11:40:16 -05:00
|
|
|
explicit config(const logger& logger, const xresource_manager& xrm) : m_logger(logger), m_xrm(xrm) {}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
void load(string file, string barname);
|
|
|
|
string filepath() const;
|
|
|
|
string bar_section() const;
|
|
|
|
vector<string> defined_bars() const;
|
|
|
|
string build_path(const string& section, const string& key) const;
|
2016-11-25 07:55:15 -05:00
|
|
|
void warn_deprecated(const string& section, const string& key, string replacement) const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-29 09:53:32 -05:00
|
|
|
/**
|
|
|
|
* Returns true if a given parameter exists
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
bool has(string section, string key) const {
|
|
|
|
auto val = m_ptree.get_optional<T>(build_path(section, key));
|
|
|
|
return (val != boost::none);
|
|
|
|
}
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
/**
|
|
|
|
* Get parameter for the current bar by name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T get(string key) const {
|
|
|
|
return get<T>(bar_section(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of a variable by section and parameter name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T get(string section, string key) const {
|
|
|
|
auto val = m_ptree.get_optional<T>(build_path(section, key));
|
|
|
|
|
|
|
|
if (val == boost::none)
|
2016-10-11 02:18:25 -04:00
|
|
|
throw key_error("Missing parameter [" + section + "." + key + "]");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key));
|
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
return dereference<T>(section, key, str_val, val.get());
|
2016-06-14 23:32:35 -04: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>
|
|
|
|
T get(string section, string key, T default_value) const {
|
|
|
|
auto val = m_ptree.get_optional<T>(build_path(section, key));
|
|
|
|
auto str_val = m_ptree.get_optional<string>(build_path(section, key));
|
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
return dereference<T>(section, key, str_val.get_value_or(""), val.get_value_or(default_value));
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values for the current bar by name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-11-12 06:56:39 -05:00
|
|
|
vector<T> get_list(string key) const {
|
2016-06-14 23:32:35 -04:00
|
|
|
return get_list<T>(bar_section(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
vector<T> get_list(string section, string key) const {
|
|
|
|
vector<T> vec;
|
2016-11-20 17:04:31 -05:00
|
|
|
boost::optional<T> value;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
while ((value = m_ptree.get_optional<T>(build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) {
|
2016-06-14 23:32:35 -04:00
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key) + "-" + to_string(vec.size()));
|
2016-11-18 11:40:16 -05:00
|
|
|
vec.emplace_back(dereference<T>(section, key, str_val, value.get()));
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
2016-10-11 02:18:25 -04:00
|
|
|
throw key_error("Missing parameter [" + section + "." + key + "-0]");
|
2016-06-14 23:32:35 -04: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>
|
|
|
|
vector<T> get_list(string section, string key, vector<T> default_value) const {
|
|
|
|
vector<T> vec;
|
2016-11-20 17:04:31 -05:00
|
|
|
boost::optional<T> value;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
while ((value = m_ptree.get_optional<T>(build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) {
|
2016-06-14 23:32:35 -04:00
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key) + "-" + to_string(vec.size()));
|
2016-11-18 11:40:16 -05:00
|
|
|
vec.emplace_back(dereference<T>(section, key, str_val, value.get()));
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
|
|
|
return default_value;
|
|
|
|
else
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
2016-11-18 11:40:16 -05:00
|
|
|
* Dereference value reference
|
2016-06-14 23:32:35 -04:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-11-18 11:40:16 -05:00
|
|
|
T dereference(string section, string key, string var, const T value) const {
|
|
|
|
if (var.substr(0, 2) != "${" || var.substr(var.length() - 1) != "}") {
|
|
|
|
return value;
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
auto path = var.substr(2, var.length() - 3);
|
|
|
|
size_t pos;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
if (path.find("env:") == 0) {
|
2016-11-18 11:40:16 -05:00
|
|
|
return dereference_env<T>(path.substr(4), value);
|
|
|
|
} else if (path.find("xrdb:") == 0) {
|
|
|
|
return dereference_xrdb<T>(path.substr(5), value);
|
|
|
|
} else if ((pos = path.find(".")) != string::npos) {
|
2016-11-19 13:18:28 -05:00
|
|
|
return dereference_local<T>(path.substr(0, pos), path.substr(pos + 1), section);
|
2016-11-18 11:40:16 -05:00
|
|
|
} else {
|
|
|
|
throw value_error("Invalid reference defined at [" + build_path(section, key) + "]");
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
2016-11-18 11:40:16 -05:00
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
/**
|
|
|
|
* Dereference local value reference defined using:
|
|
|
|
* ${root.key}
|
|
|
|
* ${self.key}
|
|
|
|
* ${section.key}
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-11-19 13:18:28 -05:00
|
|
|
T dereference_local(string section, string key, string current_section) const {
|
2016-11-18 11:40:16 -05:00
|
|
|
if (section == "BAR")
|
|
|
|
m_logger.warn("${BAR.key} is deprecated. Use ${root.key} instead");
|
2016-10-19 03:16:08 -04:00
|
|
|
|
2016-11-19 13:18:28 -05: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-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
auto path = build_path(section, key);
|
|
|
|
auto result = m_ptree.get_optional<T>(path);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
if (result == boost::none)
|
|
|
|
throw value_error("Unexisting reference defined [" + path + "]");
|
2016-10-19 03:16:08 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
return dereference<T>(section, key, m_ptree.get<string>(path), result.get());
|
|
|
|
}
|
2016-10-19 03:16:08 -04:00
|
|
|
|
2016-11-18 11:40:16 -05: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-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
if ((pos = var.find(":")) != string::npos) {
|
|
|
|
fallback = boost::lexical_cast<T>(var.substr(pos + 1));
|
|
|
|
var.erase(pos);
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
if (env_util::has(var.c_str()))
|
|
|
|
return boost::lexical_cast<T>(env_util::get(var.c_str()));
|
2016-11-18 11:40:16 -05:00
|
|
|
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
fallback = boost::lexical_cast<T>(var.substr(pos + 1));
|
|
|
|
var.erase(pos);
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-18 11:40:16 -05:00
|
|
|
if (std::is_same<string, T>::value)
|
|
|
|
return boost::lexical_cast<T>(m_xrm.get_string(var, boost::lexical_cast<string>(fallback)));
|
|
|
|
else if (std::is_same<float, T>::value)
|
|
|
|
return boost::lexical_cast<T>(m_xrm.get_float(var, boost::lexical_cast<float>(fallback)));
|
|
|
|
else if (std::is_same<int, T>::value)
|
|
|
|
return boost::lexical_cast<T>(m_xrm.get_int(var, boost::lexical_cast<int>(fallback)));
|
|
|
|
return fallback;
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const logger& m_logger;
|
2016-10-19 03:16:08 -04:00
|
|
|
const xresource_manager& m_xrm;
|
2016-06-14 23:32:35 -04:00
|
|
|
ptree m_ptree;
|
|
|
|
string m_file;
|
|
|
|
string m_current_bar;
|
|
|
|
};
|
|
|
|
|
2016-10-24 19:55:59 -04:00
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Configure injection module
|
|
|
|
*/
|
|
|
|
template <typename T = const config&>
|
|
|
|
di::injector<T> configure_config() {
|
|
|
|
return di::make_injector(configure_logger(), configure_xresource_manager());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|