1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-18 13:55:11 -05:00

refactor(xwindow): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-01 08:35:59 +01:00
parent 121d367205
commit 9f8dabfc8d
2 changed files with 64 additions and 61 deletions

View file

@ -14,52 +14,12 @@ POLYBAR_NS
class connection;
namespace modules {
/**
* Wrapper used to update the event mask of the
* currently active to enable title tracking
*/
class active_window {
public:
explicit active_window(xcb_window_t win)
: m_connection(configure_connection().create<decltype(m_connection)>()), m_window(m_connection, win) {
try {
m_window.change_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE);
} catch (const xpp::x::error::window& err) {
}
}
~active_window() {
try {
m_window.change_event_mask(XCB_EVENT_MASK_NO_EVENT);
} catch (const xpp::x::error::window& err) {
}
}
/**
* Check if current window matches passed value
*/
bool match(const xcb_window_t win) const {
return m_window == win;
}
/**
* Get the title by returning the first non-empty value of:
* _NET_WM_VISIBLE_NAME
* _NET_WM_NAME
*/
string title(xcb_ewmh_connection_t* ewmh) {
string title;
if (!(title = ewmh_util::get_visible_name(ewmh, m_window)).empty()) {
return title;
} else if (!(title = ewmh_util::get_wm_name(ewmh, m_window)).empty()) {
return title;
} else if (!(title = icccm_util::get_wm_name(m_connection, m_window)).empty()) {
return title;
} else {
return "";
}
}
explicit active_window(xcb_window_t win);
~active_window();
bool match(const xcb_window_t win) const;
string title(xcb_ewmh_connection_t* ewmh) const;
private:
connection& m_connection;
@ -72,18 +32,18 @@ namespace modules {
*/
class xwindow_module : public static_module<xwindow_module>, public xpp::event::sink<evt::property_notify> {
public:
using static_module::static_module;
xwindow_module(const bar_settings&, const logger&, const config&, string);
void setup();
void teardown();
void handle(const evt::property_notify& evt);
void update();
string get_output();
bool build(builder* builder, const string& tag) const;
private:
static constexpr const char* TAG_LABEL{"<label>"};
connection& m_connection;
ewmh_connection_t m_ewmh;
unique_ptr<active_window> m_active;
label_t m_label;

View file

@ -13,12 +13,65 @@ namespace modules {
template class module<xwindow_module>;
template class static_module<xwindow_module>;
/**
* Wrapper used to update the event mask of the
* currently active to enable title tracking
*/
active_window::active_window(xcb_window_t win)
: m_connection(configure_connection().create<decltype(m_connection)>()), m_window(m_connection, win) {
try {
m_window.change_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE);
} catch (const xpp::x::error::window& err) {
}
}
/**
* Deconstruct window object
*/
active_window::~active_window() {
try {
m_window.change_event_mask(XCB_EVENT_MASK_NO_EVENT);
} catch (const xpp::x::error::window& err) {
}
}
/**
* Check if current window matches passed value
*/
bool active_window::match(const xcb_window_t win) const {
return m_window == win;
}
/**
* Get the title by returning the first non-empty value of:
* _NET_WM_VISIBLE_NAME
* _NET_WM_NAME
*/
string active_window::title(xcb_ewmh_connection_t* ewmh) const {
string title;
if (!(title = ewmh_util::get_visible_name(ewmh, m_window)).empty()) {
return title;
} else if (!(title = ewmh_util::get_wm_name(ewmh, m_window)).empty()) {
return title;
} else if (!(title = icccm_util::get_wm_name(m_connection, m_window)).empty()) {
return title;
} else {
return "";
}
}
/**
* Construct module
*/
xwindow_module::xwindow_module(const bar_settings& bar, const logger& logger, const config& config, string name)
: static_module<xwindow_module>(bar, logger, config, name)
, m_connection(configure_connection().create<connection&>()) {}
/**
* Bootstrap the module
*/
void xwindow_module::setup() {
connection& conn{configure_connection().create<decltype(conn)>()};
// Initialize ewmh atoms
if ((m_ewmh = ewmh_util::initialize()) == nullptr) {
throw module_error("Failed to initialize ewmh atoms");
@ -42,11 +95,10 @@ namespace modules {
}
// Make sure we get notified when root properties change
window root{conn, conn.root()};
root.ensure_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE);
m_connection.ensure_event_mask(m_connection.root(), XCB_EVENT_MASK_PROPERTY_CHANGE);
// Connect with the event registry
conn.attach_sink(this, 1);
m_connection.attach_sink(this, 1);
// Trigger the initial draw event
update();
@ -56,8 +108,7 @@ namespace modules {
* Disconnect from the event registry
*/
void xwindow_module::teardown() {
connection& conn{configure_connection().create<decltype(conn)>()};
conn.detach_sink(this, 1);
m_connection.detach_sink(this, 1);
}
/**
@ -102,14 +153,6 @@ namespace modules {
broadcast();
}
/**
* Generate the module output
*/
string xwindow_module::get_output() {
m_builder->append(static_module::get_output());
return m_builder->flush();
}
/**
* Output content as defined in the config
*/