1 #include "components/config.hpp"
2
3 #include <climits>
4 #include <fstream>
5
6 #include "cairo/utils.hpp"
7 #include "utils/color.hpp"
8 #include "utils/env.hpp"
9 #include "utils/factory.hpp"
10 #include "utils/string.hpp"
11
12 POLYBAR_NS
13
14 namespace chrono = std::chrono;
15
16 /**
17 * Create instance
18 */
make(string path,string bar)19 config::make_type config::make(string path, string bar) {
20 return *factory_util::singleton<std::remove_reference_t<config::make_type>>(logger::make(), move(path), move(bar));
21 }
22
23 /**
24 * Get path of loaded file
25 */
filepath() const26 const string& config::filepath() const {
27 return m_file;
28 }
29
30 /**
31 * Get the section name of the bar in use
32 */
section() const33 string config::section() const {
34 return "bar/" + m_barname;
35 }
36
use_xrm()37 void config::use_xrm() {
38 #if WITH_XRM
39 /*
40 * Initialize the xresource manager if there are any xrdb refs
41 * present in the configuration
42 */
43 if (!m_xrm) {
44 m_log.info("Enabling xresource manager");
45 m_xrm.reset(new xresource_manager{connection::make()});
46 }
47 #endif
48 }
49
set_sections(sectionmap_t sections)50 void config::set_sections(sectionmap_t sections) {
51 m_sections = move(sections);
52 copy_inherited();
53 }
54
set_included(file_list included)55 void config::set_included(file_list included) {
56 m_included = move(included);
57 }
58
59 /**
60 * Print a deprecation warning if the given parameter is set
61 */
warn_deprecated(const string & section,const string & key,string replacement) const62 void config::warn_deprecated(const string& section, const string& key, string replacement) const {
63 try {
64 auto value = get<string>(section, key);
65 m_log.warn(
66 "The config parameter `%s.%s` is deprecated, use `%s.%s` instead.", section, key, section, move(replacement));
67 } catch (const key_error& err) {
68 }
69 }
70
71 /**
72 * Look for sections set up to inherit from a base section
73 * and copy the missing parameters
74 *
75 * Multiple sections can be specified, separated by a space.
76 *
77 * [sub/section]
78 * inherit = section1 section2
79 */
copy_inherited()80 void config::copy_inherited() {
81 for (auto&& section : m_sections) {
82 std::vector<string> inherit_sections;
83
84 // Collect all sections to be inherited
85 for (auto&& param : section.second) {
86 string key_name = param.first;
87 if (key_name == "inherit") {
88 auto inherit = param.second;
89 inherit = dereference<string>(section.first, key_name, inherit, inherit);
90
91 std::vector<string> sections = string_util::split(std::move(inherit), ' ');
92
93 inherit_sections.insert(inherit_sections.end(), sections.begin(), sections.end());
94
95 } else if (key_name.find("inherit") == 0) {
96 // Legacy support for keys that just start with 'inherit'
97 m_log.warn(
98 "\"%s.%s\": Using anything other than 'inherit' for inheriting section keys is deprecated. "
99 "The 'inherit' key supports multiple section names separated by a space.",
100 section.first, key_name);
101
102 auto inherit = param.second;
103 inherit = dereference<string>(section.first, key_name, inherit, inherit);
104 if (inherit.empty() || m_sections.find(inherit) == m_sections.end()) {
105 throw value_error(
106 "Invalid section \"" + inherit + "\" defined for \"" + section.first + "." + key_name + "\"");
107 }
108
109 inherit_sections.push_back(std::move(inherit));
110 }
111 }
112
113 for (const auto& base_name : inherit_sections) {
114 const auto base_section = m_sections.find(base_name);
115 if (base_section == m_sections.end()) {
116 throw value_error("Invalid section \"" + base_name + "\" defined for \"" + section.first + ".inherit\"");
117 }
118
119 m_log.trace("config: Inheriting keys from \"%s\" in \"%s\"", base_name, section.first);
120
121 /*
122 * Iterate the base and copy the parameters that haven't been defined
123 * yet.
124 */
125 for (auto&& base_param : base_section->second) {
126 section.second.emplace(base_param.first, base_param.second);
127 }
128 }
129 }
130 }
131
132 template <>
convert(string && value) const133 string config::convert(string&& value) const {
134 return forward<string>(value);
135 }
136
137 template <>
convert(string && value) const138 const char* config::convert(string&& value) const {
139 return value.c_str();
140 }
141
142 template <>
convert(string && value) const143 char config::convert(string&& value) const {
144 return value.c_str()[0];
145 }
146
147 template <>
convert(string && value) const148 int config::convert(string&& value) const {
149 return std::strtol(value.c_str(), nullptr, 10);
150 }
151
152 template <>
convert(string && value) const153 short config::convert(string&& value) const {
154 return static_cast<short>(std::strtol(value.c_str(), nullptr, 10));
155 }
156
157 template <>
convert(string && value) const158 bool config::convert(string&& value) const {
159 string lower{string_util::lower(forward<string>(value))};
160
161 return (lower == "true" || lower == "yes" || lower == "on" || lower == "1");
162 }
163
164 template <>
convert(string && value) const165 float config::convert(string&& value) const {
166 return std::strtof(value.c_str(), nullptr);
167 }
168
169 template <>
convert(string && value) const170 double config::convert(string&& value) const {
171 return std::strtod(value.c_str(), nullptr);
172 }
173
174 template <>
convert(string && value) const175 long config::convert(string&& value) const {
176 return std::strtol(value.c_str(), nullptr, 10);
177 }
178
179 template <>
convert(string && value) const180 long long config::convert(string&& value) const {
181 return std::strtoll(value.c_str(), nullptr, 10);
182 }
183
184 template <>
convert(string && value) const185 unsigned char config::convert(string&& value) const {
186 return std::strtoul(value.c_str(), nullptr, 10);
187 }
188
189 template <>
convert(string && value) const190 unsigned short config::convert(string&& value) const {
191 return std::strtoul(value.c_str(), nullptr, 10);
192 }
193
194 template <>
convert(string && value) const195 unsigned int config::convert(string&& value) const {
196 return std::strtoul(value.c_str(), nullptr, 10);
197 }
198
199 template <>
convert(string && value) const200 unsigned long config::convert(string&& value) const {
201 unsigned long v{std::strtoul(value.c_str(), nullptr, 10)};
202 return v < ULONG_MAX ? v : 0UL;
203 }
204
205 template <>
convert(string && value) const206 unsigned long long config::convert(string&& value) const {
207 unsigned long long v{std::strtoull(value.c_str(), nullptr, 10)};
208 return v < ULLONG_MAX ? v : 0ULL;
209 }
210
211 template <>
convert(string && value) const212 chrono::seconds config::convert(string&& value) const {
213 return chrono::seconds{convert<chrono::seconds::rep>(forward<string>(value))};
214 }
215
216 template <>
convert(string && value) const217 chrono::milliseconds config::convert(string&& value) const {
218 return chrono::milliseconds{convert<chrono::milliseconds::rep>(forward<string>(value))};
219 }
220
221 template <>
convert(string && value) const222 chrono::duration<double> config::convert(string&& value) const {
223 return chrono::duration<double>{convert<double>(forward<string>(value))};
224 }
225
226 template <>
convert(string && value) const227 rgba config::convert(string&& value) const {
228 if (value.empty()) {
229 return rgba{};
230 }
231
232 rgba ret{value};
233
234 if (!ret.has_color()) {
235 throw value_error("\"" + value + "\" is an invalid color value.");
236 }
237
238 return ret;
239 }
240
241 template <>
convert(string && value) const242 cairo_operator_t config::convert(string&& value) const {
243 return cairo::utils::str2operator(forward<string>(value), CAIRO_OPERATOR_OVER);
244 }
245
246 POLYBAR_NS_END
247