2016-11-02 15:22:45 -04:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-19 20:25:54 -05:00
|
|
|
#include "settings.hpp"
|
|
|
|
|
|
|
|
#if not WITH_XRM
|
|
|
|
#error "Not built with support for xcb-xrm..."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <xcb/xcb_xrm.h>
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
#include "common.hpp"
|
2017-01-19 20:25:54 -05:00
|
|
|
#include "errors.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
#include "x11/connection.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2017-01-19 20:25:54 -05:00
|
|
|
DEFINE_ERROR(xresource_error);
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
class xresource_manager {
|
|
|
|
public:
|
2017-01-19 20:25:54 -05:00
|
|
|
explicit xresource_manager(xcb_connection_t* conn) : m_xrm(xcb_xrm_database_from_default(conn)) {
|
|
|
|
if (m_xrm == nullptr) {
|
|
|
|
throw application_error("xcb_xrm_database_from_default()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~xresource_manager() {
|
|
|
|
xcb_xrm_database_free(m_xrm);
|
|
|
|
}
|
2016-12-09 03:02:47 -05:00
|
|
|
|
2017-01-19 20:25:54 -05:00
|
|
|
operator bool() const {
|
|
|
|
return m_xrm != nullptr;
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2017-01-19 20:25:54 -05:00
|
|
|
template <typename T>
|
|
|
|
T require(const char* name) const {
|
|
|
|
char* result{nullptr};
|
2017-01-23 11:54:20 -05:00
|
|
|
if (xcb_xrm_resource_get_string(m_xrm, string_util::replace(name, "*", ".").c_str(), nullptr, &result) == -1) {
|
2017-01-19 20:25:54 -05:00
|
|
|
throw xresource_error(sstream() << "X resource \"" << name << "\" not found");
|
|
|
|
} else if (result == nullptr) {
|
|
|
|
throw xresource_error(sstream() << "X resource \"" << name << "\" not found");
|
|
|
|
}
|
|
|
|
return convert<T>(string(result));
|
|
|
|
}
|
2016-12-14 21:30:41 -05:00
|
|
|
|
2017-01-19 20:25:54 -05:00
|
|
|
template <typename T>
|
|
|
|
T get(const char* name, const T& fallback) const {
|
|
|
|
try {
|
|
|
|
return convert<T>(require<T>(name));
|
|
|
|
} catch (const xresource_error) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
protected:
|
2017-01-19 20:25:54 -05:00
|
|
|
template <typename T>
|
|
|
|
T convert(string&& value) const;
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
private:
|
2017-01-19 20:25:54 -05:00
|
|
|
xcb_xrm_database_t* m_xrm;
|
2016-11-02 15:22:45 -04:00
|
|
|
};
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|