refactor: Cleanup

This commit is contained in:
Michael Carlberg 2016-11-22 01:22:47 +01:00
parent c442e47f93
commit 0c64b64ff8
7 changed files with 178 additions and 242 deletions

View File

@ -32,31 +32,15 @@ class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::propert
void parse(string data, bool force = false);
protected:
void refresh_window();
void create_monitor();
void setup_monitor();
void configure_geom();
void restack_window();
void map_window();
void set_wmhints();
int get_centerx();
int get_innerwidth();
void reconfigure_window();
void handle(const evt::button_press& evt);
void handle(const evt::expose& evt);
void handle(const evt::property_notify& evt);
void on_alignment_change(const alignment align);
void on_attribute_set(const attribute attr);
void on_attribute_unset(const attribute attr);
void on_attribute_toggle(const attribute attr);
void on_action_block_open(const mousebtn btn, string cmd);
void on_action_block_close(const mousebtn btn);
void on_color_change(const gc gc_, uint32_t color);
void on_font_change(int index);
void on_pixel_offset(int px);
void on_tray_report(uint16_t slots);
private:
connection& m_connection;
const config& m_conf;
@ -64,17 +48,17 @@ class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::propert
unique_ptr<tray_manager> m_tray;
unique_ptr<renderer> m_renderer;
bar_settings m_opts;
xcb_window_t m_window;
xcb_screen_t* m_screen;
size m_screensize{};
bool m_sinkattached{false};
string m_lastinput;
xcb_window_t m_window;
bar_settings m_opts;
alignment m_trayalign{alignment::NONE};
uint8_t m_trayclients{0};
std::mutex m_mutex;
string m_lastinput;
};
di::injector<unique_ptr<bar>> configure_bar();

View File

@ -62,7 +62,7 @@ struct line_settings {
struct bar_settings {
monitor_t monitor;
edge origin{edge::BOTTOM};
edge origin{edge::TOP};
struct size size{0, 0};
position pos{0, 0};
@ -88,6 +88,17 @@ struct bar_settings {
string locale;
bool force_docking{false};
const xcb_rectangle_t inner_area() const {
xcb_rectangle_t rect{pos.x, pos.y, size.w, size.h};
rect.y += borders.at(edge::TOP).size;
rect.height -= borders.at(edge::TOP).size;
rect.height -= borders.at(edge::BOTTOM).size;
rect.x += borders.at(edge::LEFT).size;
rect.width -= borders.at(edge::LEFT).size;
rect.width -= borders.at(edge::RIGHT).size;
return rect;
}
};
struct action_block {

View File

@ -11,7 +11,7 @@ namespace chrono = std::chrono;
namespace time_util {
using clock_t = chrono::high_resolution_clock;
template <typename T, typename Dur = chrono::milliseconds>
template <typename T, typename Dur = chrono::microseconds>
auto measure(const T& expr) noexcept {
auto start = clock_t::now();
expr();

View File

@ -36,7 +36,11 @@ di::injector<unique_ptr<bar>> configure_bar() {
* Construct bar instance
*/
bar::bar(connection& conn, const config& config, const logger& logger, unique_ptr<tray_manager> tray_manager)
: m_connection(conn), m_conf(config), m_log(logger), m_tray(forward<decltype(tray_manager)>(tray_manager)) {}
: m_connection(conn)
, m_conf(config)
, m_log(logger)
, m_tray(forward<decltype(tray_manager)>(tray_manager))
, m_screen(conn.screen()) {}
/**
* Cleanup signal handlers and destroy the bar window
@ -58,14 +62,8 @@ bar::~bar() {
g_signals::parser::string_write = nullptr;
g_signals::tray::report_slotcount = nullptr; // }}}
if (m_tray) {
m_tray.reset();
}
if (m_sinkattached) {
m_connection.detach_sink(this, 1);
}
}
/**
* Create required components
@ -75,21 +73,35 @@ bar::~bar() {
void bar::bootstrap(bool nodraw) {
auto bs = m_conf.bar_section();
m_screen = m_connection.screen();
auto geom = m_connection.get_geometry(m_screen->root);
m_screensize.w = geom->width;
m_screensize.h = geom->height;
setup_monitor();
m_log.trace("bar: Load config values");
{
m_opts.locale = m_conf.get<string>(bs, "locale", "");
m_opts.separator = string_util::trim(m_conf.get<string>(bs, "separator", ""), '"');
m_opts.wmname = m_conf.get<string>(bs, "wm-name", "polybar-" + bs.substr(4) + "_" + m_opts.monitor->name);
m_opts.wmname = string_util::replace(m_opts.wmname, " ", "-");
create_monitor();
if (m_conf.get<bool>(bs, "bottom", false))
m_opts.origin = edge::BOTTOM;
// Set bar colors {{{
GET_CONFIG_VALUE(bs, m_opts.force_docking, "dock");
GET_CONFIG_VALUE(bs, m_opts.spacing, "spacing");
GET_CONFIG_VALUE(bs, m_opts.padding.left, "padding-left");
GET_CONFIG_VALUE(bs, m_opts.padding.right, "padding-right");
GET_CONFIG_VALUE(bs, m_opts.module_margin.left, "module-margin-left");
GET_CONFIG_VALUE(bs, m_opts.module_margin.right, "module-margin-right");
m_opts.background = color::parse(m_conf.get<string>(bs, "background", color_util::hex<uint16_t>(m_opts.background)));
m_opts.foreground = color::parse(m_conf.get<string>(bs, "foreground", color_util::hex<uint16_t>(m_opts.foreground)));
m_opts.strut.top = m_conf.get<int>("global/wm", "margin-top", 0);
m_opts.strut.bottom = m_conf.get<int>("global/wm", "margin-bottom", 0);
}
m_log.trace("bar: Load color values");
{
m_opts.background =
color::parse(m_conf.get<string>(bs, "background", color_util::hex<uint16_t>(m_opts.background)));
m_opts.foreground =
color::parse(m_conf.get<string>(bs, "foreground", color_util::hex<uint16_t>(m_opts.foreground)));
auto linecolor = color::parse(m_conf.get<string>(bs, "linecolor", "#f00"));
auto lineheight = m_conf.get<int>(bs, "lineheight", 0);
@ -107,10 +119,10 @@ void bar::bootstrap(bool nodraw) {
} catch (const key_error& err) {
m_opts.underline.color = linecolor;
}
}
// }}}
// Set border values {{{
m_log.trace("bar: Load border values");
{
auto bsize = m_conf.get<int>(bs, "border-size", 0);
auto bcolor = m_conf.get<string>(bs, "border-color", "#00000000");
@ -129,34 +141,7 @@ void bar::bootstrap(bool nodraw) {
m_opts.borders.emplace(edge::RIGHT, border_settings{});
m_opts.borders[edge::RIGHT].size = m_conf.get<int>(bs, "border-right", bsize);
m_opts.borders[edge::RIGHT].color = color::parse(m_conf.get<string>(bs, "border-right-color", bcolor));
// }}}
// Set size and position {{{
if (m_conf.get<bool>(bs, "bottom", false))
m_opts.origin = edge::BOTTOM;
else
m_opts.origin = edge::TOP;
GET_CONFIG_VALUE(bs, m_opts.force_docking, "dock");
GET_CONFIG_VALUE(bs, m_opts.spacing, "spacing");
GET_CONFIG_VALUE(bs, m_opts.padding.left, "padding-left");
GET_CONFIG_VALUE(bs, m_opts.padding.right, "padding-right");
GET_CONFIG_VALUE(bs, m_opts.module_margin.left, "module-margin-left");
GET_CONFIG_VALUE(bs, m_opts.module_margin.right, "module-margin-right");
m_opts.strut.top = m_conf.get<int>("global/wm", "margin-top", 0);
m_opts.strut.bottom = m_conf.get<int>("global/wm", "margin-bottom", 0);
// }}}
// Set the WM_NAME value {{{
// Required early for --print-wmname
m_opts.wmname = m_conf.get<string>(bs, "wm-name", "polybar-" + bs.substr(4) + "_" + m_opts.monitor->name);
m_opts.wmname = string_util::replace(m_opts.wmname, " ", "-");
// }}}
// Check nodraw flag {{{
}
if (nodraw) {
m_log.trace("bar: Abort bootstrap routine (reason: nodraw)");
@ -164,11 +149,8 @@ void bar::bootstrap(bool nodraw) {
return;
}
// }}}
// Connect signal handlers and attach sink {{{
m_log.trace("bar: Attach parser callbacks");
m_log.trace("bar: Attach parser signal handlers");
{
// clang-format off
g_signals::parser::alignment_change = [this](const alignment align) {
m_renderer->set_alignment(align);
@ -204,12 +186,10 @@ void bar::bootstrap(bool nodraw) {
m_renderer->draw_textstring(text, len);
};
// clang-format on
}
m_log.trace("bar: Attaching sink to registry");
m_connection.attach_sink(this, 1);
m_sinkattached = true;
// }}}
configure_geom();
@ -217,11 +197,10 @@ void bar::bootstrap(bool nodraw) {
m_window = m_renderer->window();
restack_window();
set_wmhints();
map_window();
reconfigure_window();
m_connection.map_window(m_window);
m_connection.flush();
}
/**
@ -283,7 +262,7 @@ void bar::bootstrap_tray() {
} else if (settings.align == alignment::LEFT) {
settings.orig_x = m_opts.pos.x + m_opts.borders.at(edge::LEFT).size;
} else if (settings.align == alignment::CENTER) {
settings.orig_x = get_centerx() - (settings.width / 2);
settings.orig_x = m_opts.center.x - (settings.width / 2);
}
// Set user-defined background color
@ -331,7 +310,17 @@ void bar::bootstrap_tray() {
// Add tray update callback unless explicitly disabled
if (!m_conf.get<bool>(bs, "tray-detached", false)) {
g_signals::tray::report_slotcount = bind(&bar::on_tray_report, this, placeholders::_1);
g_signals::tray::report_slotcount = [this](uint16_t slots) {
m_log.trace("bar: Tray reports %lu slots", slots);
if (m_trayclients != slots) {
m_trayclients = slots;
if (!m_lastinput.empty()) {
parse(m_lastinput, true);
}
}
};
}
// Put the tray next to the bar in the window stack
@ -410,13 +399,6 @@ void bar::parse(string data, bool force) {
m_renderer->end();
}
/**
* Refresh the bar window by clearing and redrawing the pixmaps
*/
void bar::refresh_window() {
m_log.info("Refresh bar window");
}
/**
* Configure geometry values
*/
@ -468,7 +450,10 @@ void bar::configure_geom() {
m_opts.size.w = math_util::cap<int>(m_opts.size.w, 0, m_opts.monitor->w);
m_opts.size.h = math_util::cap<int>(m_opts.size.h, 0, m_opts.monitor->h);
m_opts.center.y = (m_opts.size.h + m_opts.borders[edge::TOP].size - m_opts.borders[edge::BOTTOM].size) / 2;
m_opts.center.y = m_opts.size.h + m_opts.borders[edge::TOP].size - m_opts.borders[edge::BOTTOM].size;
m_opts.center.y /= 2;
m_opts.center.x = m_opts.pos.x + m_opts.size.w - m_opts.borders[edge::RIGHT].size + m_opts.borders[edge::LEFT].size;
m_opts.center.x /= 2;
m_log.info("Bar geometry %ix%i+%i+%i", m_opts.size.w, m_opts.size.h, m_opts.pos.x, m_opts.pos.y);
}
@ -476,7 +461,7 @@ void bar::configure_geom() {
/**
* Create monitor object
*/
void bar::create_monitor() {
void bar::setup_monitor() {
m_log.trace("bar: Create monitor from matching X RandR output");
auto strict = m_conf.get<bool>(m_conf.bar_section(), "monitor-strict", false);
@ -545,9 +530,10 @@ void bar::restack_window() {
}
/**
* Map window and reconfigure its position
* Reconfigure window by updating atom values
* and moving it to the correct position
*/
void bar::map_window() {
void bar::reconfigure_window() {
auto geom = m_connection.get_geometry(m_screen->root);
auto w = m_opts.size.w + m_opts.offset.x;
auto h = m_opts.size.h + m_opts.offset.y;
@ -560,67 +546,39 @@ void bar::map_window() {
h += m_opts.strut.bottom;
}
if (m_opts.origin == edge::BOTTOM && m_opts.monitor->y + m_opts.monitor->h < m_screensize.h) {
h += m_screensize.h - (m_opts.monitor->y + m_opts.monitor->h);
if (m_opts.origin == edge::BOTTOM && m_opts.monitor->y + m_opts.monitor->h < geom->height) {
h += geom->height - (m_opts.monitor->y + m_opts.monitor->h);
} else if (m_opts.origin != edge::BOTTOM) {
h += m_opts.monitor->y;
}
window win{m_connection, m_window};
win.map_checked();
win.reconfigure_struts(w, h, x, m_opts.origin == edge::BOTTOM);
win.reconfigure_pos(x, y);
}
/**
* Set window atom values
*/
void bar::set_wmhints() {
m_log.trace("bar: Set WM_NAME");
m_log.trace("bar: Set window WM_NAME");
xcb_icccm_set_wm_name(m_connection, m_window, XCB_ATOM_STRING, 8, m_opts.wmname.size(), m_opts.wmname.c_str());
xcb_icccm_set_wm_class(m_connection, m_window, 15, "polybar\0Polybar");
m_log.trace("bar: Set WM_NORMAL_HINTS");
m_log.trace("bar: Set window WM_NORMAL_HINTS");
xcb_size_hints_t hints;
xcb_icccm_size_hints_set_position(&hints, true, m_opts.pos.x, m_opts.pos.y);
xcb_icccm_size_hints_set_size(&hints, true, m_opts.size.w, m_opts.size.h);
xcb_icccm_set_wm_normal_hints(m_connection, m_window, &hints);
m_log.trace("bar: Set _NET_WM_WINDOW_TYPE");
m_log.trace("bar: Set window _NET_WM_WINDOW_TYPE");
wm_util::set_windowtype(m_connection, m_window, {_NET_WM_WINDOW_TYPE_DOCK});
m_log.trace("bar: Set _NET_WM_STATE");
m_log.trace("bar: Set window _NET_WM_STATE");
wm_util::set_wmstate(m_connection, m_window, {_NET_WM_STATE_STICKY, _NET_WM_STATE_ABOVE});
m_log.trace("bar: Set _NET_WM_DESKTOP");
m_log.trace("bar: Set window _NET_WM_DESKTOP");
wm_util::set_wmdesktop(m_connection, m_window, 0xFFFFFFFF);
m_log.trace("bar: Set _NET_WM_PID");
m_log.trace("bar: Set window _NET_WM_PID");
wm_util::set_wmpid(m_connection, m_window, getpid());
}
/**
* Get the horizontal center pos
*/
int bar::get_centerx() {
int x = m_opts.pos.x;
x += m_opts.size.w;
x -= m_opts.borders[edge::RIGHT].size;
x += m_opts.borders[edge::LEFT].size;
x /= 2;
return x;
}
/**
* Get the inner width of the bar
*/
int bar::get_innerwidth() {
auto w = m_opts.size.w;
w -= m_opts.borders[edge::RIGHT].size;
w -= m_opts.borders[edge::LEFT].size;
return w;
}
/**
* Event handler for XCB_BUTTON_PRESS events
*
@ -714,28 +672,6 @@ void bar::handle(const evt::property_notify& evt) {
} catch (const exception& err) {
m_log.warn("Failed to emit bar window's visibility change event");
}
} else if (evt->atom == _XROOTMAP_ID) {
refresh_window();
} else if (evt->atom == _XSETROOT_ID) {
refresh_window();
} else if (evt->atom == ESETROOT_PMAP_ID) {
refresh_window();
}
}
/**
* Proess systray report
*/
void bar::on_tray_report(uint16_t slots) {
if (m_trayclients == slots) {
return;
}
m_log.trace("bar: tray_report(%lu)", slots);
m_trayclients = slots;
if (!m_lastinput.empty()) {
parse(m_lastinput, true);
}
}

View File

@ -17,7 +17,7 @@ eventloop::~eventloop() noexcept {
module->stop();
module.reset();
});
m_log.trace("eventloop: Deconstruction of %s took %lu ms.", module_name, cleanup_ms);
m_log.trace("eventloop: Deconstruction of %s took %lu microsec.", module_name, cleanup_ms);
}
}
}

View File

@ -27,15 +27,15 @@ renderer::renderer(connection& conn, const logger& logger, unique_ptr<font_manag
: m_connection(conn), m_log(logger), m_fontmanager(forward<decltype(font_manager)>(font_manager)), m_bar(bar) {
auto screen = m_connection.screen();
m_log.trace("bar: Get true color visual");
m_log.trace("renderer: Get true color visual");
m_visual = m_connection.visual_type(screen, 32).get();
m_log.trace("bar: Create colormap");
m_log.trace("renderer: Create colormap");
m_colormap = m_connection.generate_id();
m_connection.create_colormap(XCB_COLORMAP_ALLOC_NONE, m_colormap, screen->root, m_visual->visual_id);
m_window = m_connection.generate_id();
m_log.trace("bar: Create window %s", m_connection.id(m_window));
m_log.trace("renderer: Create window %s", m_connection.id(m_window));
{
uint32_t mask{0};
uint32_t values[16]{0};
@ -56,10 +56,10 @@ renderer::renderer(connection& conn, const logger& logger, unique_ptr<font_manag
}
m_pixmap = m_connection.generate_id();
m_log.trace("bar: Create pixmap (xid=%s)", m_connection.id(m_pixmap));
m_log.trace("renderer: Create pixmap (xid=%s)", m_connection.id(m_pixmap));
m_connection.create_pixmap(32, m_pixmap, m_window, m_bar.size.w, m_bar.size.h);
m_log.trace("bar: Create gcontexts");
m_log.trace("renderer: Create gcontexts");
{
// clang-format off
vector<uint32_t> colors {
@ -85,12 +85,12 @@ renderer::renderer(connection& conn, const logger& logger, unique_ptr<font_manag
xutils::pack_values(mask, &params, value_list);
m_gcontexts.emplace(gc(i), m_connection.generate_id());
m_log.trace("bar: Create gcontext (gc=%i, xid=%s)", i, m_connection.id(m_gcontexts.at(gc(i))));
m_log.trace("renderer: Create gcontext (gc=%i, xid=%s)", i, m_connection.id(m_gcontexts.at(gc(i))));
m_connection.create_gc(m_gcontexts.at(gc(i)), m_pixmap, mask, value_list);
}
}
m_log.trace("bar: Load fonts");
m_log.trace("renderer: Load fonts");
{
auto fonts_loaded = false;
auto fontindex = 0;
@ -144,6 +144,20 @@ void renderer::begin() {
}
void renderer::end() {
redraw();
m_fontmanager->destroy_xftdraw();
#ifdef DEBUG
debughints();
#endif
m_reserve = 0;
m_reserve_at = edge::NONE;
}
void renderer::redraw() {
m_log.info("renderer: redrawing");
xcb_rectangle_t rect{0, 0, m_bar.size.w, m_bar.size.h};
if (m_reserve_at == edge::LEFT) {
@ -158,19 +172,8 @@ void renderer::end() {
m_connection.copy_area(
m_pixmap, m_window, m_gcontexts.at(gc::FG), rect.x, rect.y, rect.x, rect.y, rect.width, rect.height);
m_connection.flush();
m_fontmanager->destroy_xftdraw();
#ifdef DEBUG
debughints();
#endif
m_reserve = 0;
m_reserve_at = edge::NONE;
}
void renderer::redraw() {}
void renderer::reserve_space(edge side, uint16_t w) {
m_log.trace_x("renderer: reserve_space(%i, %i)", static_cast<uint8_t>(side), w);
m_reserve = w;

View File

@ -219,10 +219,12 @@ void tray_manager::deactivate() { // {{{
g_signals::bar::visibility_change = nullptr;
}
if (!m_connection.connection_has_error()) {
if (m_connection.get_selection_owner_unchecked(m_atom).owner<xcb_window_t>() == m_tray) {
m_log.trace("tray: Unset selection owner");
m_connection.set_selection_owner(XCB_NONE, m_atom, XCB_CURRENT_TIME);
}
}
m_log.trace("tray: Unembed clients");
m_clients.clear();