#include "components/config.hpp" #include #include #include "cairo/utils.hpp" #include "utils/color.hpp" #include "utils/env.hpp" #include "utils/factory.hpp" #include "utils/string.hpp" POLYBAR_NS namespace chrono = std::chrono; /** * Create instance */ config::make_type config::make(string path, string bar) { return *factory_util::singleton>(logger::make(), move(path), move(bar)); } /** * Get path of loaded file */ const string& config::filepath() const { return m_file; } /** * Get the section name of the bar in use */ string config::section() const { return "bar/" + m_barname; } void config::use_xrm() { #if WITH_XRM /* * Initialize the xresource manager if there are any xrdb refs * present in the configuration */ if (!m_xrm) { m_log.info("Enabling xresource manager"); m_xrm.reset(new xresource_manager{connection::make()}); } #endif } void config::set_sections(sectionmap_t sections) { m_sections = move(sections); copy_inherited(); } void config::set_included(file_list included) { m_included = move(included); } /** * Print a deprecation warning if the given parameter is set */ void config::warn_deprecated(const string& section, const string& key, string replacement) const { try { auto value = get(section, key); m_log.warn( "The config parameter `%s.%s` is deprecated, use `%s.%s` instead.", section, key, section, move(replacement)); } catch (const key_error& err) { } } /** * Look for sections set up to inherit from a base section * and copy the missing parameters * * [sub/section] * inherit = base/section */ void config::copy_inherited() { for (auto&& section : m_sections) { for (auto&& param : section.second) { if (param.first == "inherit") { // Get name of base section auto inherit = param.second; if ((inherit = dereference(section.first, param.first, inherit, inherit)).empty()) { throw value_error("Invalid section \"\" defined for \"" + section.first + ".inherit\""); } // Find and validate base section auto base_section = m_sections.find(inherit); if (base_section == m_sections.end()) { throw value_error("Invalid section \"" + inherit + "\" defined for \"" + section.first + ".inherit\""); } m_log.trace("config: Copying missing params (sub=\"%s\", base=\"%s\")", section.first, inherit); /* * Iterate the base and copy the parameters that haven't been defined * for the sub-section */ for (auto&& base_param : base_section->second) { section.second.emplace(base_param.first, base_param.second); } } } } } template <> string config::convert(string&& value) const { return forward(value); } template <> const char* config::convert(string&& value) const { return value.c_str(); } template <> char config::convert(string&& value) const { return value.c_str()[0]; } template <> int config::convert(string&& value) const { return std::strtol(value.c_str(), nullptr, 10); } template <> short config::convert(string&& value) const { return static_cast(std::strtol(value.c_str(), nullptr, 10)); } template <> bool config::convert(string&& value) const { string lower{string_util::lower(forward(value))}; return (lower == "true" || lower == "yes" || lower == "on" || lower == "1"); } template <> float config::convert(string&& value) const { return std::strtof(value.c_str(), nullptr); } template <> double config::convert(string&& value) const { return std::strtod(value.c_str(), nullptr); } template <> long config::convert(string&& value) const { return std::strtol(value.c_str(), nullptr, 10); } template <> long long config::convert(string&& value) const { return std::strtoll(value.c_str(), nullptr, 10); } template <> unsigned char config::convert(string&& value) const { return std::strtoul(value.c_str(), nullptr, 10); } template <> unsigned short config::convert(string&& value) const { return std::strtoul(value.c_str(), nullptr, 10); } template <> unsigned int config::convert(string&& value) const { return std::strtoul(value.c_str(), nullptr, 10); } template <> unsigned long config::convert(string&& value) const { unsigned long v{std::strtoul(value.c_str(), nullptr, 10)}; return v < ULONG_MAX ? v : 0UL; } template <> unsigned long long config::convert(string&& value) const { unsigned long long v{std::strtoull(value.c_str(), nullptr, 10)}; return v < ULLONG_MAX ? v : 0ULL; } template <> chrono::seconds config::convert(string&& value) const { return chrono::seconds{convert(forward(value))}; } template <> chrono::milliseconds config::convert(string&& value) const { return chrono::milliseconds{convert(forward(value))}; } template <> chrono::duration config::convert(string&& value) const { return chrono::duration{convert(forward(value))}; } template <> rgba config::convert(string&& value) const { return rgba{value}; } template <> cairo_operator_t config::convert(string&& value) const { return cairo::utils::str2operator(forward(value), CAIRO_OPERATOR_OVER); } POLYBAR_NS_END