2016-11-02 19:22:45 +00:00
|
|
|
#include <X11/Xlib.h>
|
2016-12-05 19:41:00 +00:00
|
|
|
#include <cstring>
|
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
|
|
|
/**
|
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 static_cast<xresource_manager::make_type>(
|
2016-12-14 14:09:11 +00:00
|
|
|
*factory_util::singleton<std::remove_reference_t<xresource_manager::make_type>>(xlib::get_display()));
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct manager instance
|
|
|
|
*/
|
2016-12-21 22:22:02 +00:00
|
|
|
xresource_manager::xresource_manager(Display* dsp) : m_display(forward<decltype(dsp)>(dsp)) {
|
2016-11-02 19:22:45 +00:00
|
|
|
XrmInitialize();
|
|
|
|
|
2016-12-21 22:22:02 +00:00
|
|
|
if ((m_manager = XResourceManagerString(m_display)) != nullptr) {
|
2016-12-14 14:09:11 +00:00
|
|
|
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) {
|
|
|
|
XFree(m_manager);
|
|
|
|
}
|
2016-12-14 14:09:11 +00:00
|
|
|
if (m_db != 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
|
|
|
|
*/
|
2016-11-18 16:40:16 +00:00
|
|
|
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()) {
|
2016-11-18 16:40:16 +00:00
|
|
|
return fallback;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-18 16:40:16 +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
|
|
|
|
*/
|
2016-11-18 16:40:16 +00:00
|
|
|
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()) {
|
2016-11-18 16:40:16 +00:00
|
|
|
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
|
|
|
|
*/
|
2016-11-18 16:40:16 +00:00
|
|
|
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()) {
|
2016-11-18 16:40:16 +00:00
|
|
|
return fallback;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-18 16:40:16 +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;
|
2016-12-15 08:29:14 +00:00
|
|
|
XrmValue ret{};
|
2016-11-02 19:22:45 +00:00
|
|
|
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
|