From 0ddcf26a45b15e7e96dc7c917556239369d63014 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 1 Feb 2019 21:45:59 +0100 Subject: [PATCH] feat: output configuration --- include/bar.hpp | 5 +++- resources/config | 2 +- src/bar.cpp | 64 ++++++++++++++++++++++++++++++++++++++--- src/modules/network.cpp | 2 +- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 924cdb53..0c467d9a 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -18,7 +18,7 @@ class Bar { public: Bar(const Client&, std::unique_ptr&&, uint32_t); Bar(const Bar&) = delete; - ~Bar() = default; + ~Bar(); auto toggle() -> void; @@ -44,6 +44,9 @@ class Bar { static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *); + void initBar(); + bool isValidOutput(const Json::Value &config); + void destroyOutput(); auto setupConfig() -> void; auto setupWidgets() -> void; auto setupCss() -> void; diff --git a/resources/config b/resources/config index 43c17c48..180a192c 100644 --- a/resources/config +++ b/resources/config @@ -80,4 +80,4 @@ "max-length": 40, "exec": "$HOME/.config/waybar/mediaplayer.py 2> /dev/null" // Script in resources folder } -} +} \ No newline at end of file diff --git a/src/bar.cpp b/src/bar.cpp index 0561af58..61a24111 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -33,8 +33,16 @@ waybar::Bar::Bar(const Client& client, GdkWindow *gdk_window = gtk_widget_get_window(wrap); gdk_wayland_window_set_use_custom_surface(gdk_window); surface = gdk_wayland_window_get_wl_surface(gdk_window); +} - std::size_t layer_top = config_["layer"] == "top" +waybar::Bar::~Bar() +{ + destroyOutput(); +} + +void waybar::Bar::initBar() +{ + std::size_t layer_top = config_["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; layer_surface = zwlr_layer_shell_v1_get_layer_surface( client.layer_shell, surface, *output, layer_top, "waybar"); @@ -43,8 +51,7 @@ waybar::Bar::Bar(const Client& client, .configure = layerSurfaceHandleConfigure, .closed = layerSurfaceHandleClosed, }; - zwlr_layer_surface_v1_add_listener(layer_surface, - &layer_surface_listener, this); + zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this); std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; @@ -65,6 +72,17 @@ waybar::Bar::Bar(const Client& client, setupWidgets(); } +void waybar::Bar::destroyOutput() +{ + if (layer_surface != nullptr) { + zwlr_layer_surface_v1_destroy(layer_surface); + } + if (surface != nullptr) { + wl_surface_destroy(surface); + } + wl_output_destroy(*output); +} + void waybar::Bar::handleLogicalPosition(void* /*data*/, struct zxdg_output_v1* /*zxdg_output_v1*/, int32_t /*x*/, int32_t /*y*/) { @@ -84,11 +102,49 @@ void waybar::Bar::handleDone(void* /*data*/, // Nothing here } +bool waybar::Bar::isValidOutput(const Json::Value &config) +{ + bool found = true; + if (config["output"].isArray()) { + bool in_array = false; + for (auto const &output : config["output"]) { + if (output.isString() && output.asString() == output_name) { + in_array = true; + break; + } + } + found = in_array; + } + if (config["output"].isString() && config["output"].asString() != output_name) { + found = false; + } + return found; +} + void waybar::Bar::handleName(void* data, struct zxdg_output_v1* /*xdg_output*/, const char* name) { auto o = static_cast(data); o->output_name = name; + bool found = true; + if (o->config_.isArray()) { + bool in_array = false; + for (auto const &config : o->config_) { + if (config.isObject() && o->isValidOutput(config)) { + in_array = true; + o->config_ = config; + break; + } + } + found = in_array; + } else { + found = o->isValidOutput(o->config_); + } + if (!found) { + o->destroyOutput(); + } else { + o->initBar(); + } } void waybar::Bar::handleDescription(void* /*data*/, @@ -102,7 +158,6 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, uint32_t height) { auto o = static_cast(data); - o->window.show_all(); zwlr_layer_surface_v1_ack_configure(surface, serial); if (width != o->width_ || height != o->height_) { o->width_ = width; @@ -221,4 +276,5 @@ auto waybar::Bar::setupWidgets() -> void for (auto const& module : modules_right_) { right_.pack_end(*module, false, false, 0); } + window.show_all(); } diff --git a/src/modules/network.cpp b/src/modules/network.cpp index ea3a16c6..b234930c 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -65,7 +65,7 @@ void waybar::modules::Network::createInfoSocket() } nl_socket_disable_seq_check(info_sock_); nl_socket_set_nonblocking(info_sock_); - nl_socket_modify_cb(info_sock_, NL_CB_MSG_IN, NL_CB_CUSTOM, handleEvents, this); + nl_socket_modify_cb(info_sock_, NL_CB_VALID, NL_CB_CUSTOM, handleEvents, this); efd_ = epoll_create1(0); if (efd_ < 0) { throw std::runtime_error("Can't create epoll");