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; class connection;
namespace modules { namespace modules {
/**
* Wrapper used to update the event mask of the
* currently active to enable title tracking
*/
class active_window { class active_window {
public: public:
explicit active_window(xcb_window_t win) explicit active_window(xcb_window_t win);
: m_connection(configure_connection().create<decltype(m_connection)>()), m_window(m_connection, win) { ~active_window();
try { bool match(const xcb_window_t win) const;
m_window.change_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE); string title(xcb_ewmh_connection_t* ewmh) const;
} 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 "";
}
}
private: private:
connection& m_connection; connection& m_connection;
@ -72,18 +32,18 @@ namespace modules {
*/ */
class xwindow_module : public static_module<xwindow_module>, public xpp::event::sink<evt::property_notify> { class xwindow_module : public static_module<xwindow_module>, public xpp::event::sink<evt::property_notify> {
public: public:
using static_module::static_module; xwindow_module(const bar_settings&, const logger&, const config&, string);
void setup(); void setup();
void teardown(); void teardown();
void handle(const evt::property_notify& evt); void handle(const evt::property_notify& evt);
void update(); void update();
string get_output();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
private: private:
static constexpr const char* TAG_LABEL{"<label>"}; static constexpr const char* TAG_LABEL{"<label>"};
connection& m_connection;
ewmh_connection_t m_ewmh; ewmh_connection_t m_ewmh;
unique_ptr<active_window> m_active; unique_ptr<active_window> m_active;
label_t m_label; label_t m_label;

View File

@ -13,12 +13,65 @@ namespace modules {
template class module<xwindow_module>; template class module<xwindow_module>;
template class static_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 * Bootstrap the module
*/ */
void xwindow_module::setup() { void xwindow_module::setup() {
connection& conn{configure_connection().create<decltype(conn)>()};
// Initialize ewmh atoms // Initialize ewmh atoms
if ((m_ewmh = ewmh_util::initialize()) == nullptr) { if ((m_ewmh = ewmh_util::initialize()) == nullptr) {
throw module_error("Failed to initialize ewmh atoms"); throw module_error("Failed to initialize ewmh atoms");
@ -42,11 +95,10 @@ namespace modules {
} }
// Make sure we get notified when root properties change // Make sure we get notified when root properties change
window root{conn, conn.root()}; m_connection.ensure_event_mask(m_connection.root(), XCB_EVENT_MASK_PROPERTY_CHANGE);
root.ensure_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE);
// Connect with the event registry // Connect with the event registry
conn.attach_sink(this, 1); m_connection.attach_sink(this, 1);
// Trigger the initial draw event // Trigger the initial draw event
update(); update();
@ -56,8 +108,7 @@ namespace modules {
* Disconnect from the event registry * Disconnect from the event registry
*/ */
void xwindow_module::teardown() { void xwindow_module::teardown() {
connection& conn{configure_connection().create<decltype(conn)>()}; m_connection.detach_sink(this, 1);
conn.detach_sink(this, 1);
} }
/** /**
@ -102,14 +153,6 @@ namespace modules {
broadcast(); 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 * Output content as defined in the config
*/ */