1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/include/x11/xresources.hpp

65 lines
1.4 KiB
C++
Raw Normal View History

2016-11-02 15:22:45 -04: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 15:22:45 -04:00
#include "common.hpp"
#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
DEFINE_ERROR(xresource_error);
2016-11-02 15:22:45 -04: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 03:02:47 -05:00
operator bool() const {
return m_xrm != nullptr;
}
2016-11-02 15:22:45 -04: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-14 21:30:41 -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:
template <typename T>
T convert(string&& value) const;
2016-11-02 15:22:45 -04:00
private:
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