polybar/src/x11/xresources.cpp

88 lines
1.9 KiB
C++
Raw Normal View History

2016-11-02 19:22:45 +00:00
#include <X11/Xlib.h>
2016-11-25 12:55:15 +00:00
#include <utility>
2016-11-20 22:04:31 +00:00
#include "utils/factory.hpp"
2016-11-02 19:22:45 +00:00
#include "x11/xlib.hpp"
#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
/**
* Configure injection module
*/
di::injector<const xresource_manager&> configure_xresource_manager() {
auto instance = factory_util::generic_singleton<xresource_manager>();
return di::make_injector(di::bind<>().to(instance));
}
/**
* Construct manager instance
*/
2016-11-02 19:22:45 +00:00
xresource_manager::xresource_manager() {
XrmInitialize();
2016-11-25 12:55:15 +00:00
if (xlib::get_display() == nullptr) {
2016-11-02 19:22:45 +00:00
return;
2016-11-25 12:55:15 +00:00
}
if ((m_manager = XResourceManagerString(xlib::get_display())) == nullptr) {
2016-11-02 19:22:45 +00:00
return;
2016-11-25 12:55:15 +00:00
}
if ((m_db = XrmGetStringDatabase(m_manager)) == nullptr) {
2016-11-02 19:22:45 +00:00
return;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +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) {
2016-11-02 19:22:45 +00:00
return "";
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
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 "";
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END