polybar-dwm/include/x11/xresources.hpp

65 lines
1.4 KiB
C++
Raw Normal View History

2016-11-02 19:22:45 +00:00
#pragma once
#include "settings.hpp"
#if not WITH_XRM
#error "Not built with support for xcb-xrm..."
#endif
#include <xcb/xcb_xrm.h>
2016-11-02 19:22:45 +00:00
#include "common.hpp"
#include "errors.hpp"
#include "utils/string.hpp"
#include "x11/connection.hpp"
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
DEFINE_ERROR(xresource_error);
2016-11-02 19:22:45 +00:00
class xresource_manager {
public:
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 08:02:47 +00:00
operator bool() const {
return m_xrm != nullptr;
}
2016-11-02 19:22:45 +00:00
template <typename T>
T require(const char* name) const {
char* result{nullptr};
if (xcb_xrm_resource_get_string(m_xrm, string_util::replace(name, "*", ".").c_str(), nullptr, &result) == -1) {
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-15 02:30:41 +00: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 19:22:45 +00:00
protected:
template <typename T>
T convert(string&& value) const;
2016-11-02 19:22:45 +00:00
private:
xcb_xrm_database_t* m_xrm;
2016-11-02 19:22:45 +00:00
};
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END