From 171ecd53aa0fe8f6fff45b0c53969e2d86e6a3f8 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 19 Apr 2019 11:56:40 +0200 Subject: [PATCH] refactor(Bar): roundtrip before setup widgets --- include/bar.hpp | 6 ++++++ include/client.hpp | 1 - src/bar.cpp | 37 ++++++++++++------------------------- src/client.cpp | 12 +++--------- 4 files changed, 21 insertions(+), 35 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 1b4816e..73de06b 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -38,6 +38,12 @@ class Bar { bool vertical = false; private: + static inline const std::string MIN_HEIGHT_MSG = + "Requested height: {} exceeds the minimum height: {} required by the modules"; + static inline const std::string MIN_WIDTH_MSG = + "Requested width: {} exceeds the minimum width: {} required by the modules"; + static inline const std::string BAR_SIZE_MSG = + "Bar configured (width: {}, height: {}) for output: {}"; static void layerSurfaceHandleConfigure(void *, struct zwlr_layer_surface_v1 *, uint32_t, uint32_t, uint32_t); static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *); diff --git a/include/client.hpp b/include/client.hpp index c831bc0..78eaf87 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -21,7 +21,6 @@ class Client { struct wl_registry * registry = nullptr; struct zwlr_layer_shell_v1 * layer_shell = nullptr; struct zxdg_output_manager_v1 * xdg_output_manager = nullptr; - struct wl_seat * seat = nullptr; struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager = nullptr; std::vector> bars; diff --git a/src/bar.cpp b/src/bar.cpp index 6662dd7..a718a79 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -14,7 +14,6 @@ waybar::Bar::Bar(struct waybar_output* w_output) window.set_title("waybar"); window.set_name("waybar"); window.set_decorated(false); - window.set_resizable(false); if (output->config["position"] == "right" || output->config["position"] == "left") { height_ = 0; @@ -29,15 +28,11 @@ waybar::Bar::Bar(struct waybar_output* w_output) gdk_wayland_window_set_use_custom_surface(gdk_window); surface = gdk_wayland_window_get_wl_surface(gdk_window); - // Convert to button code for every module that is used. - setupAltFormatKeyForModuleList("modules-left"); - setupAltFormatKeyForModuleList("modules-right"); - setupAltFormatKeyForModuleList("modules-center"); std::size_t layer = output->config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; + auto client = waybar::Client::inst(); layer_surface = zwlr_layer_shell_v1_get_layer_surface( - waybar::Client::inst()->layer_shell, surface, output->output, layer, "waybar"); - + client->layer_shell, surface, output->output, layer, "waybar"); static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { .configure = layerSurfaceHandleConfigure, .closed = layerSurfaceHandleClosed, @@ -72,6 +67,7 @@ waybar::Bar::Bar(struct waybar_output* w_output) zwlr_layer_surface_v1_set_size(layer_surface, width, height); wl_surface_commit(surface); + wl_display_roundtrip(client->wl_display); setupWidgets(); } @@ -136,7 +132,6 @@ void waybar::Bar::handleSignal(int signal) { void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, uint32_t height) { auto o = static_cast(data); - zwlr_layer_surface_v1_ack_configure(surface, serial); if (width != o->width_ || height != o->height_) { o->width_ = width; o->height_ = height; @@ -146,31 +141,18 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf int min_width, min_height; o->window.get_size(min_width, min_height); if (o->height_ < static_cast(min_height)) { - std::cout << fmt::format( - "Requested height: {} exceeds the minimum \ -height: {} required by the modules", - o->height_, - min_height) - << std::endl; + std::cout << fmt::format(MIN_HEIGHT_MSG, o->height_, min_height) << std::endl; o->height_ = min_height; } if (o->width_ < static_cast(min_width)) { - std::cout << fmt::format( - "Requested width: {} exceeds the minimum \ -width: {} required by the modules", - o->height_, - min_width) - << std::endl; + std::cout << fmt::format(MIN_WIDTH_MSG, o->height_, min_width) << std::endl; o->width_ = min_width; } - std::cout << fmt::format("Bar configured (width: {}, height: {}) for output: {}", - o->width_, - o->height_, - o->output->name) - << std::endl; + std::cout << fmt::format(BAR_SIZE_MSG, o->width_, o->height_, o->output->name) << std::endl; wl_surface_commit(o->surface); } + zwlr_layer_surface_v1_ack_configure(surface, serial); } void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) { @@ -227,6 +209,11 @@ auto waybar::Bar::setupWidgets() -> void { box_.set_center_widget(center_); box_.pack_end(right_, true, true); + // Convert to button code for every module that is used. + setupAltFormatKeyForModuleList("modules-left"); + setupAltFormatKeyForModuleList("modules-right"); + setupAltFormatKeyForModuleList("modules-center"); + Factory factory(*this, output->config); getModules(factory, "modules-left"); getModules(factory, "modules-center"); diff --git a/src/client.cpp b/src/client.cpp index bec28b5..752ad91 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -39,17 +39,13 @@ void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, uint wl_registry_bind(registry, name, &wl_output_interface, version)); client->outputs_.emplace_back(new struct waybar_output({wl_output, "", name, nullptr})); client->handleOutput(client->outputs_.back()); - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - client->seat = static_cast( - wl_registry_bind(registry, name, &wl_seat_interface, version)); } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { client->xdg_output_manager = static_cast(wl_registry_bind( registry, name, &zxdg_output_manager_v1_interface, ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)); } else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) { - waybar::Client::inst()->idle_inhibit_manager = - static_cast( - wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1)); + client->idle_inhibit_manager = static_cast( + wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1)); } } @@ -216,10 +212,9 @@ void waybar::Client::bindInterfaces() { }; wl_registry_add_listener(registry, ®istry_listener, this); wl_display_roundtrip(wl_display); - if (!layer_shell || !seat || !xdg_output_manager) { + if (!layer_shell || !xdg_output_manager) { throw std::runtime_error("Failed to acquire required resources."); } - wl_display_roundtrip(wl_display); } int waybar::Client::main(int argc, char *argv[]) { @@ -266,7 +261,6 @@ int waybar::Client::main(int argc, char *argv[]) { zwlr_layer_shell_v1_destroy(layer_shell); zwp_idle_inhibit_manager_v1_destroy(idle_inhibit_manager); wl_registry_destroy(registry); - wl_seat_destroy(seat); wl_display_disconnect(wl_display); return 0; }