polybar/src/x11/xresources.cpp

89 lines
1.9 KiB
C++
Raw Normal View History

2016-11-02 19:22:45 +00:00
#include <X11/Xlib.h>
#include <cstring>
2016-11-25 12:55:15 +00:00
#include <utility>
2016-11-20 22:04:31 +00:00
#include "utils/factory.hpp"
#include "x11/connection.hpp"
2016-11-02 19:22:45 +00:00
#include "x11/xresources.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
2016-11-20 22:04:31 +00:00
/**
2016-12-09 08:02:47 +00:00
* Create instance
2016-11-20 22:04:31 +00:00
*/
2016-12-09 11:43:31 +00:00
xresource_manager::make_type xresource_manager::make() {
return *factory_util::singleton<xresource_manager>(static_cast<Display*>(connection::make()));
2016-11-20 22:04:31 +00:00
}
/**
* Construct manager instance
*/
xresource_manager::xresource_manager(Display* dsp) {
2016-11-02 19:22:45 +00:00
XrmInitialize();
if ((m_manager = XResourceManagerString(dsp)) != nullptr) {
m_db = XrmGetStringDatabase(m_manager);
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
2016-12-09 21:54:30 +00:00
/**
* Deconstruct instance
*/
xresource_manager::~xresource_manager() {
if (m_manager != nullptr) {
XrmDestroyDatabase(m_db);
}
2016-12-09 21:54:30 +00:00
}
2016-11-20 22:04:31 +00:00
/**
* Get string value from the X resource db
*/
string xresource_manager::get_string(string name, string fallback) const {
2016-11-25 12:55:15 +00:00
auto result = load_value(move(name), "String", 64);
if (result.empty()) {
return fallback;
2016-11-25 12:55:15 +00:00
}
return result;
2016-11-02 19:22:45 +00:00
}
2016-11-20 22:04:31 +00:00
/**
* Get float value from the X resource db
*/
float xresource_manager::get_float(string name, float fallback) const {
2016-11-25 12:55:15 +00:00
auto result = load_value(move(name), "String", 64);
if (result.empty()) {
return fallback;
2016-11-25 12:55:15 +00:00
}
return strtof(result.c_str(), nullptr);
2016-11-02 19:22:45 +00:00
}
2016-11-20 22:04:31 +00:00
/**
* Get integer value from the X resource db
*/
int xresource_manager::get_int(string name, int fallback) const {
2016-11-25 12:55:15 +00:00
auto result = load_value(move(name), "String", 64);
if (result.empty()) {
return fallback;
2016-11-25 12:55:15 +00:00
}
return atoi(result.c_str());
2016-11-02 19:22:45 +00:00
}
2016-11-20 22:04:31 +00:00
/**
* Query the database for given key
*/
2016-11-25 12:55:15 +00:00
string xresource_manager::load_value(const string& key, const string& res_type, size_t n) const {
if (m_manager != nullptr) {
2017-01-19 19:19:17 +00:00
char* type = nullptr;
XrmValue ret{};
2016-11-02 19:22:45 +00:00
2017-01-19 19:19:17 +00:00
if (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};
}
}
2016-11-02 19:22:45 +00:00
}
return "";
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END