Files
polybar/src/x11/xresources.cpp
Michael Carlberg 1b5979ba10 feat(config): Default reference values
- Add fallback values to env/xrdb references:

  ${env:UNEXISTING:fallback value}
  ${xrdb:UNEXISTING:fallback value}

- Replace ${BAR.key} with ${root.key} and display
  deprecation warning if the former is used
2016-11-18 18:04:24 +01:00

56 lines
1.3 KiB
C++

#include <X11/Xlib.h>
#include "x11/xlib.hpp"
#include "x11/xresources.hpp"
LEMONBUDDY_NS
xresource_manager::xresource_manager() {
XrmInitialize();
if (xlib::get_display() == nullptr)
return;
if ((m_manager = XResourceManagerString(xlib::get_display())) == nullptr)
return;
if ((m_db = XrmGetStringDatabase(m_manager)) == nullptr)
return;
}
string xresource_manager::get_string(string name, string fallback) const {
auto result = load_value(name, "String", 64);
if (result.empty())
return fallback;
return result;
}
float xresource_manager::get_float(string name, float fallback) const {
auto result = load_value(name, "String", 64);
if (result.empty())
return fallback;
return strtof(result.c_str(), 0);
}
int xresource_manager::get_int(string name, int fallback) const {
auto result = load_value(name, "String", 64);
if (result.empty())
return fallback;
return atoi(result.c_str());
}
string xresource_manager::load_value(string key, string res_type, size_t n) const {
if (m_manager == nullptr)
return "";
char* type = nullptr;
XrmValue ret;
XrmGetResource(m_db, key.c_str(), res_type.c_str(), &type, &ret);
if (ret.addr != nullptr && !std::strncmp(res_type.c_str(), type, n)) {
return {ret.addr};
}
return "";
}
LEMONBUDDY_NS_END