polybar/src/x11/xlib.cpp

45 lines
994 B
C++
Raw Normal View History

2016-12-21 22:22:02 +00:00
#include <X11/X.h>
#include "utils/factory.hpp"
2016-12-23 04:18:58 +00:00
#include "x11/xlib.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
namespace xlib {
2016-12-21 22:22:02 +00:00
namespace detail {
display_lock::display_lock(Display* display) : m_display(forward<decltype(display)>(display)) {
XLockDisplay(m_display);
}
2016-12-21 22:22:02 +00:00
display_lock::~display_lock() {
XUnlockDisplay(m_display);
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
2016-12-21 22:22:02 +00:00
Display* get_display() {
static Display* display{XOpenDisplay(nullptr)};
return display;
}
Visual* get_visual(int screen, uint8_t depth) {
static shared_ptr<Visual> visual;
if (!visual) {
2016-12-15 08:29:14 +00:00
XVisualInfo info{};
2016-12-21 22:22:02 +00:00
if (XMatchVisualInfo(get_display(), screen, depth, TrueColor, &info)) {
visual = shared_ptr<Visual>(info.visual, [=](Visual* v) { XFree(v); });
}
2016-11-02 19:22:45 +00:00
}
2016-12-21 22:22:02 +00:00
return &*visual;
2016-11-02 19:22:45 +00:00
}
Colormap create_colormap(int screen) {
2016-12-21 22:22:02 +00:00
return XDefaultColormap(get_display(), screen);
}
inline auto make_display_lock() {
2016-12-21 22:22:02 +00:00
return make_unique<detail::display_lock>(get_display());
2016-11-02 19:22:45 +00:00
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END