refactor(xwindow): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-23 01:03:38 +01:00
parent 417d90f411
commit 9479b5abe2
2 changed files with 25 additions and 36 deletions

View file

@ -1,8 +1,5 @@
#pragma once #pragma once
#include <bitset>
#include "components/config.hpp"
#include "modules/meta/static_module.hpp" #include "modules/meta/static_module.hpp"
#include "x11/events.hpp" #include "x11/events.hpp"
#include "x11/ewmh.hpp" #include "x11/ewmh.hpp"
@ -16,14 +13,15 @@ class connection;
namespace modules { namespace modules {
class active_window { class active_window {
public: public:
explicit active_window(xcb_window_t win); explicit active_window(xcb_connection_t* conn, xcb_window_t win);
~active_window(); ~active_window();
bool match(const xcb_window_t win) const; bool match(const xcb_window_t win) const;
string title(xcb_ewmh_connection_t* ewmh) const; string title(xcb_ewmh_connection_t* ewmh) const;
private: private:
connection& m_connection; xcb_connection_t* m_connection{nullptr};
window m_window{m_connection}; xcb_window_t m_window{XCB_NONE};
}; };
/** /**
@ -35,10 +33,12 @@ namespace modules {
explicit xwindow_module(const bar_settings&, string); explicit xwindow_module(const bar_settings&, string);
void teardown(); void teardown();
void handle(const evt::property_notify& evt); void update(bool force = false);
void update();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
protected:
void handle(const evt::property_notify& evt);
private: private:
static constexpr const char* TAG_LABEL{"<label>"}; static constexpr const char* TAG_LABEL{"<label>"};

View file

@ -16,10 +16,10 @@ namespace modules {
* Wrapper used to update the event mask of the * Wrapper used to update the event mask of the
* currently active to enable title tracking * currently active to enable title tracking
*/ */
active_window::active_window(xcb_window_t win) : m_connection(connection::make()), m_window(m_connection, win) { active_window::active_window(xcb_connection_t* conn, xcb_window_t win) : m_connection(conn), m_window(win) {
try { if (m_window != XCB_NONE) {
m_window.change_event_mask(XCB_EVENT_MASK_PROPERTY_CHANGE); const uint32_t mask{XCB_EVENT_MASK_PROPERTY_CHANGE};
} catch (const xpp::x::error::window& err) { xcb_change_window_attributes(m_connection, m_window, XCB_CW_EVENT_MASK, &mask);
} }
} }
@ -27,9 +27,9 @@ namespace modules {
* Deconstruct window object * Deconstruct window object
*/ */
active_window::~active_window() { active_window::~active_window() {
try { if (m_window != XCB_NONE) {
m_window.change_event_mask(XCB_EVENT_MASK_NO_EVENT); const uint32_t mask{XCB_EVENT_MASK_NO_EVENT};
} catch (const xpp::x::error::window& err) { xcb_change_window_attributes(m_connection, m_window, XCB_CW_EVENT_MASK, &mask);
} }
} }
@ -81,14 +81,6 @@ namespace modules {
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%title%"); m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%title%");
} }
// No need to setup X components if we can't show the title
if (!m_label || !m_label->has_token("%title%")) {
return;
}
// Make sure we get notified when root properties change
m_connection.ensure_event_mask(m_connection.root(), XCB_EVENT_MASK_PROPERTY_CHANGE);
// Connect with the event registry // Connect with the event registry
m_connection.attach_sink(this, SINK_PRIORITY_MODULE); m_connection.attach_sink(this, SINK_PRIORITY_MODULE);
@ -108,9 +100,9 @@ namespace modules {
*/ */
void xwindow_module::handle(const evt::property_notify& evt) { void xwindow_module::handle(const evt::property_notify& evt) {
if (evt->atom == _NET_ACTIVE_WINDOW) { if (evt->atom == _NET_ACTIVE_WINDOW) {
update(); update(true);
} else if (evt->atom == _NET_CURRENT_DESKTOP) { } else if (evt->atom == _NET_CURRENT_DESKTOP) {
update(); update(true);
} else if (evt->atom == _NET_WM_VISIBLE_NAME) { } else if (evt->atom == _NET_WM_VISIBLE_NAME) {
update(); update();
} else if (evt->atom == _NET_WM_NAME) { } else if (evt->atom == _NET_WM_NAME) {
@ -123,25 +115,22 @@ namespace modules {
/** /**
* Update the currently active window and query its title * Update the currently active window and query its title
*/ */
void xwindow_module::update() { void xwindow_module::update(bool force) {
xcb_window_t win{ewmh_util::get_active_window(m_ewmh.get())}; xcb_window_t win;
string title;
if (m_active && m_active->match(win)) { if (force) {
title = m_active->title(m_ewmh.get());
} else if (win != XCB_NONE) {
m_active = factory_util::unique<active_window>(win);
title = m_active->title(m_ewmh.get());
} else {
m_active.reset(); m_active.reset();
} }
if (!m_active && (win = ewmh_util::get_active_window(&*m_ewmh)) != XCB_NONE) {
m_active = make_unique<active_window>(m_connection, win);
}
if (m_label) { if (m_label) {
m_label->reset_tokens(); m_label->reset_tokens();
m_label->replace_token("%title%", title); m_label->replace_token("%title%", m_active ? m_active->title(&*m_ewmh) : "");
} }
// Emit notification to trigger redraw
broadcast(); broadcast();
} }