From 807ef323577852976610fd0cd3d13701a9c25004 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 18 Apr 2019 17:43:16 +0200 Subject: [PATCH] refactor: format && better output management --- include/bar.hpp | 110 ++++++------ include/client.hpp | 66 +++++--- include/factory.hpp | 3 +- include/modules/network.hpp | 2 +- include/util/json.hpp | 3 + src/bar.cpp | 288 ++++++++++---------------------- src/client.cpp | 267 ++++++++++++++++++++--------- src/main.cpp | 19 +-- src/modules/idle_inhibitor.cpp | 23 ++- src/modules/network.cpp | 6 +- src/modules/sni/host.cpp | 14 +- src/modules/sni/tray.cpp | 1 - src/modules/sway/workspaces.cpp | 152 ++++++++--------- 13 files changed, 479 insertions(+), 475 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index bb48aeb..1957354 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -1,73 +1,63 @@ #pragma once -#include #include -#include #include +#include #include +#include +#include "IModule.hpp" +#include "idle-inhibit-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "xdg-output-unstable-v1-client-protocol.h" -#include "idle-inhibit-unstable-v1-client-protocol.h" -#include "IModule.hpp" namespace waybar { -class Client; class Factory; - -class Bar { - public: - Bar(const Client&, std::unique_ptr&&, uint32_t); - Bar(const Bar&) = delete; - ~Bar() = default; - - auto toggle() -> void; - void handleSignal(int); - - const Client& client; - Gtk::Window window; - struct wl_surface *surface; - struct zwlr_layer_surface_v1 *layer_surface; - std::unique_ptr output; - std::string output_name; - uint32_t wl_name; - bool visible = true; - bool vertical = false; - private: - static void handleLogicalPosition(void *, struct zxdg_output_v1 *, int32_t, - int32_t); - static void handleLogicalSize(void *, struct zxdg_output_v1 *, int32_t, - int32_t); - static void handleDone(void *, struct zxdg_output_v1 *); - static void handleName(void *, struct zxdg_output_v1 *, const char *); - static void handleDescription(void *, struct zxdg_output_v1 *, - const char *); - static void layerSurfaceHandleConfigure(void *, - struct zwlr_layer_surface_v1 *, uint32_t, uint32_t, uint32_t); - 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; - void getModules(const Factory&, const std::string&); - - uint32_t width_ = 0; - uint32_t height_ = 30; - Json::Value config_; - Glib::RefPtr style_context_; - Glib::RefPtr css_provider_; - struct zxdg_output_v1 *xdg_output_; - Gtk::Box left_; - Gtk::Box center_; - Gtk::Box right_; - Gtk::Box box_; - std::vector> modules_left_; - std::vector> modules_center_; - std::vector> modules_right_; +struct waybar_output { + struct wl_output *output; + std::string name; + uint32_t wl_name; + struct zxdg_output_v1 *xdg_output; + Json::Value config; }; -} +class Bar { + public: + Bar(struct waybar_output* w_output); + Bar(const Bar &) = delete; + ~Bar() = default; + + auto toggle() -> void; + void handleSignal(int); + + struct waybar_output* output; + Gtk::Window window; + struct wl_surface *surface; + struct zwlr_layer_surface_v1 *layer_surface; + bool visible = true; + bool vertical = false; + + private: + static void layerSurfaceHandleConfigure(void *, struct zwlr_layer_surface_v1 *, uint32_t, + uint32_t, uint32_t); + static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *); + + void destroyOutput(); + void onWindowRealize(); + auto setupWidgets() -> void; + void getModules(const Factory &, const std::string &); + void setupAltFormatKeyForModule(const std::string &module_name); + void setupAltFormatKeyForModuleList(const char *module_list_name); + + uint32_t width_ = 0; + uint32_t height_ = 30; + Gtk::Box left_; + Gtk::Box center_; + Gtk::Box right_; + Gtk::Box box_; + std::vector> modules_left_; + std::vector> modules_center_; + std::vector> modules_right_; +}; + +} // namespace waybar diff --git a/include/client.hpp b/include/client.hpp index 5f0e9e5..5185508 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -1,41 +1,55 @@ #pragma once -#include -#include #include #include -#include #include +#include +#include +#include #include "bar.hpp" namespace waybar { class Client { - public: - Client(int argc, char *argv[]); - int main(int argc, char *argv[]); + public: + static Client *inst(); + int main(int argc, char *argv[]); - Glib::RefPtr gtk_app; - std::string css_file; - std::string config_file; - Glib::RefPtr gdk_display; - struct wl_display *wl_display = nullptr; - 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; + Glib::RefPtr gtk_app; + Glib::RefPtr gdk_display; + struct wl_display *wl_display = nullptr; + 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; - private: - void setupConfigs(const std::string& config, const std::string& style); - void bindInterfaces(); - const std::string getValidPath(std::vector paths); + private: + Client(); + void setupConfigs(const std::string &config, const std::string &style); + void bindInterfaces(); + const std::string getValidPath(std::vector paths); + void handleOutput(std::unique_ptr &output); + bool isValidOutput(const Json::Value &config, std::unique_ptr &output); + auto setupConfig() -> void; + auto setupCss() -> void; - static void handleGlobal(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, uint32_t version); - static void handleGlobalRemove(void *data, - struct wl_registry *registry, uint32_t name); + static void handleGlobal(void *data, struct wl_registry *registry, uint32_t name, + const char *interface, uint32_t version); + static void handleGlobalRemove(void *data, struct wl_registry *registry, uint32_t name); + static void handleLogicalPosition(void *, struct zxdg_output_v1 *, int32_t, int32_t); + static void handleLogicalSize(void *, struct zxdg_output_v1 *, int32_t, int32_t); + static void handleDone(void *, struct zxdg_output_v1 *); + static void handleName(void *, struct zxdg_output_v1 *, const char *); + static void handleDescription(void *, struct zxdg_output_v1 *, const char *); + + Json::Value config_; + std::string css_file_; + std::string config_file_; + Glib::RefPtr style_context_; + Glib::RefPtr css_provider_; + std::vector> outputs_; }; -} +} // namespace waybar diff --git a/include/factory.hpp b/include/factory.hpp index 2f55046..a31ef8f 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -28,11 +28,10 @@ #endif #include "modules/temperature.hpp" #include "modules/custom.hpp" +#include "bar.hpp" namespace waybar { -class Bar; - class Factory { public: Factory(const Bar& bar, const Json::Value& config); diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 90805cb..bbfe884 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -21,7 +21,7 @@ class Network : public ALabel { auto update() -> void; private: static const uint8_t MAX_RETRY = 5; - static const uint8_t EPOLL_MAX = 255; + static const uint8_t EPOLL_MAX = 200; static int handleEvents(struct nl_msg*, void*); static int handleScan(struct nl_msg*, void*); diff --git a/include/util/json.hpp b/include/util/json.hpp index 7671fa1..a9841ab 100644 --- a/include/util/json.hpp +++ b/include/util/json.hpp @@ -14,6 +14,9 @@ struct JsonParser { { Json::Value root; std::string err; + if (data.empty()) { + return root; + } bool res = reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err); if (!res) diff --git a/src/bar.cpp b/src/bar.cpp index 4183547..a16a81f 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -1,32 +1,26 @@ #include "bar.hpp" #include "client.hpp" #include "factory.hpp" -#include "util/json.hpp" -waybar::Bar::Bar(const Client& client, - std::unique_ptr &&p_output, uint32_t p_wl_name) - : client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL}, - surface(nullptr), layer_surface(nullptr), - output(std::move(p_output)), wl_name(p_wl_name), - left_(Gtk::ORIENTATION_HORIZONTAL, 0), center_(Gtk::ORIENTATION_HORIZONTAL, 0), - right_(Gtk::ORIENTATION_HORIZONTAL, 0), box_(Gtk::ORIENTATION_HORIZONTAL, 0) -{ - static const struct zxdg_output_v1_listener xdgOutputListener = { - .logical_position = handleLogicalPosition, - .logical_size = handleLogicalSize, - .done = handleDone, - .name = handleName, - .description = handleDescription, - }; - xdg_output_ = - zxdg_output_manager_v1_get_xdg_output(client.xdg_output_manager, *output); - zxdg_output_v1_add_listener(xdg_output_, &xdgOutputListener, this); +waybar::Bar::Bar(struct waybar_output* w_output) + : output(w_output), + window{Gtk::WindowType::WINDOW_TOPLEVEL}, + surface(nullptr), + layer_surface(nullptr), + left_(Gtk::ORIENTATION_HORIZONTAL, 0), + center_(Gtk::ORIENTATION_HORIZONTAL, 0), + right_(Gtk::ORIENTATION_HORIZONTAL, 0), + box_(Gtk::ORIENTATION_HORIZONTAL, 0) { window.set_title("waybar"); window.set_name("waybar"); window.set_decorated(false); window.set_resizable(false); - setupConfig(); - setupCss(); + + if (output->config["position"] == "right" || output->config["position"] == "left") { + height_ = 0; + width_ = 30; + } + window.set_size_request(width_, height_); auto gtk_window = window.gobj(); auto gtk_widget = GTK_WIDGET(gtk_window); @@ -34,87 +28,37 @@ waybar::Bar::Bar(const Client& client, auto gdk_window = window.get_window()->gobj(); gdk_wayland_window_set_use_custom_surface(gdk_window); surface = gdk_wayland_window_get_wl_surface(gdk_window); -} - -void waybar::Bar::initBar() -{ - // Converting string to button code rn as to avoid doing it later - auto setupAltFormatKeyForModule = [this](const std::string& module_name){ - if (config_.isMember(module_name)) { - Json::Value& module = config_[module_name]; - if (module.isMember("format-alt")) { - if (module.isMember("format-alt-click")) { - Json::Value& click = module["format-alt-click"]; - if (click.isString()) { - std::string str_click = click.asString(); - - if (str_click == "click-right") { - module["format-alt-click"] = 3u; - } else if (str_click == "click-middle") { - module["format-alt-click"] = 2u; - } else if (str_click == "click-backward") { - module["format-alt-click"] = 8u; - } else if (str_click == "click-forward") { - module["format-alt-click"] = 9u; - } else { - module["format-alt-click"] = 1u; // default click-left - } - } else { - module["format-alt-click"] = 1u; - } - } else { - module["format-alt-click"] = 1u; - } - - } - } - }; - - auto setupAltFormatKeyForModuleList = [this, &setupAltFormatKeyForModule](const char* module_list_name) { - if (config_.isMember(module_list_name)) { - Json::Value& modules = config_[module_list_name]; - for (const Json::Value& module_name : modules) { - if (module_name.isString()) { - setupAltFormatKeyForModule(module_name.asString()); - } - } - } - }; // Convert to button code for every module that is used. setupAltFormatKeyForModuleList("modules-left"); setupAltFormatKeyForModuleList("modules-right"); setupAltFormatKeyForModuleList("modules-center"); - std::size_t layer = 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, "waybar"); + std::size_t layer = output->config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP + : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; + layer_surface = zwlr_layer_shell_v1_get_layer_surface(waybar::Client::inst()->layer_shell, + surface, output->output, layer, "waybar"); static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { - .configure = layerSurfaceHandleConfigure, - .closed = layerSurfaceHandleClosed, + .configure = layerSurfaceHandleConfigure, + .closed = layerSurfaceHandleClosed, }; zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this); - if (config_["position"] == "right" || config_["position"] == "left") { - height_ = 0; - width_ = 30; - } - - auto height = config_["height"].isUInt() ? config_["height"].asUInt() : height_; - auto width = config_["width"].isUInt() ? config_["width"].asUInt() : width_; + auto height = output->config["height"].isUInt() ? output->config["height"].asUInt() : height_; + auto width = output->config["width"].isUInt() ? output->config["width"].asUInt() : width_; std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; - if (config_["position"] == "bottom") { + if (output->config["position"] == "bottom") { anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; - } else if (config_["position"] == "left") { + } else if (output->config["position"] == "left") { anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT; - } else if (config_["position"] == "right") { + } else if (output->config["position"] == "right") { anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM || anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) { anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - } else if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT || anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) { + } else if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT || + anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) { anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; left_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0); center_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0); @@ -132,98 +76,66 @@ void waybar::Bar::initBar() setupWidgets(); } -void waybar::Bar::handleLogicalPosition(void* /*data*/, - struct zxdg_output_v1* /*zxdg_output_v1*/, int32_t /*x*/, int32_t /*y*/) -{ - // Nothing here -} +// Converting string to button code rn as to avoid doing it later +void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) { + if (output->config.isMember(module_name)) { + Json::Value& module = output->config[module_name]; + if (module.isMember("format-alt")) { + if (module.isMember("format-alt-click")) { + Json::Value& click = module["format-alt-click"]; + if (click.isString()) { + std::string str_click = click.asString(); -void waybar::Bar::handleLogicalSize(void* /*data*/, - struct zxdg_output_v1* /*zxdg_output_v1*/, int32_t /*width*/, - int32_t /*height*/) -{ - // Nothing here -} - -void waybar::Bar::handleDone(void* /*data*/, - struct zxdg_output_v1* /*zxdg_output_v1*/) -{ - // 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; + if (str_click == "click-right") { + module["format-alt-click"] = 3u; + } else if (str_click == "click-middle") { + module["format-alt-click"] = 2u; + } else if (str_click == "click-backward") { + module["format-alt-click"] = 8u; + } else if (str_click == "click-forward") { + module["format-alt-click"] = 9u; + } else { + module["format-alt-click"] = 1u; // default click-left + } + } else { + module["format-alt-click"] = 1u; + } + } else { + module["format-alt-click"] = 1u; } } - 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; +void waybar::Bar::setupAltFormatKeyForModuleList(const char* module_list_name) { + if (output->config.isMember(module_list_name)) { + Json::Value& modules = output->config[module_list_name]; + for (const Json::Value& module_name : modules) { + if (module_name.isString()) { + setupAltFormatKeyForModule(module_name.asString()); } } - found = in_array; - } else { - found = o->isValidOutput(o->config_); - } - if (!found) { - wl_output_destroy(*o->output); - zxdg_output_v1_destroy(o->xdg_output_); - } else { - o->initBar(); } } -void waybar::Bar::handleDescription(void* /*data*/, - struct zxdg_output_v1* /*zxdg_output_v1*/, const char* /*description*/) -{ - // Nothing here -} - -void waybar::Bar::handleSignal(int signal) -{ +void waybar::Bar::handleSignal(int signal) { for (auto& module : modules_left_) { auto* custom = dynamic_cast(module.get()); - if(custom) custom->refresh(signal); + if (custom) custom->refresh(signal); } for (auto& module : modules_center_) { auto* custom = dynamic_cast(module.get()); - if(custom) custom->refresh(signal); + if (custom) custom->refresh(signal); } for (auto& module : modules_right_) { auto* custom = dynamic_cast(module.get()); - if(custom) custom->refresh(signal); + if (custom) custom->refresh(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); +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; @@ -234,37 +146,38 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, 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( + "Requested height: {} exceeds the minimum \ +height: {} required by the modules", + 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( + "Requested width: {} exceeds the minimum \ +width: {} required by the modules", + 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 configured (width: {}, height: {}) for output: {}", o->width_, + o->height_, o->output->name) + << std::endl; wl_surface_commit(o->surface); } } -void waybar::Bar::layerSurfaceHandleClosed(void* data, - struct zwlr_layer_surface_v1* /*surface*/) -{ - auto o = static_cast(data); +void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) { + auto o = static_cast(data); zwlr_layer_surface_v1_destroy(o->layer_surface); - wl_output_destroy(*o->output); - zxdg_output_v1_destroy(o->xdg_output_); o->modules_left_.clear(); o->modules_center_.clear(); o->modules_right_.clear(); } -auto waybar::Bar::toggle() -> void -{ +auto waybar::Bar::toggle() -> void { visible = !visible; auto zone = visible ? height_ : 0; if (!visible) { @@ -276,35 +189,9 @@ auto waybar::Bar::toggle() -> void wl_surface_commit(surface); } -auto waybar::Bar::setupConfig() -> void -{ - std::ifstream file(client.config_file); - if (!file.is_open()) { - throw std::runtime_error("Can't open config file"); - } - std::string str((std::istreambuf_iterator(file)), - std::istreambuf_iterator()); - util::JsonParser parser; - config_ = parser.parse(str); -} - -auto waybar::Bar::setupCss() -> void -{ - css_provider_ = Gtk::CssProvider::create(); - style_context_ = Gtk::StyleContext::create(); - - // Load our css file, wherever that may be hiding - if (css_provider_->load_from_path(client.css_file)) { - Glib::RefPtr screen = window.get_screen(); - style_context_->add_provider_for_screen(screen, css_provider_, - GTK_STYLE_PROVIDER_PRIORITY_USER); - } -} - -void waybar::Bar::getModules(const Factory& factory, const std::string& pos) -{ - if (config_[pos].isArray()) { - for (const auto &name : config_[pos]) { +void waybar::Bar::getModules(const Factory& factory, const std::string& pos) { + if (output->config[pos].isArray()) { + for (const auto& name : output->config[pos]) { try { auto module = factory.makeModule(name.asString()); if (pos == "modules-left") { @@ -330,14 +217,13 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos) } } -auto waybar::Bar::setupWidgets() -> void -{ +auto waybar::Bar::setupWidgets() -> void { window.add(box_); box_.pack_start(left_, true, true); box_.set_center_widget(center_); box_.pack_end(right_, true, true); - Factory factory(*this, config_); + Factory factory(*this, output->config); getModules(factory, "modules-left"); getModules(factory, "modules-center"); getModules(factory, "modules-right"); diff --git a/src/client.cpp b/src/client.cpp index d05e256..0d1d877 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,25 +1,20 @@ #include "client.hpp" -#include "util/clara.hpp" +#include #include +#include "util/clara.hpp" +#include "util/json.hpp" -waybar::Client::Client(int argc, char* argv[]) - : gtk_app(Gtk::Application::create(argc, argv, "fr.arouillard.waybar")), - gdk_display(Gdk::Display::get_default()) -{ - if (!gdk_display) { - throw std::runtime_error("Can't find display"); - } - if (!GDK_IS_WAYLAND_DISPLAY(gdk_display->gobj())) { - throw std::runtime_error("Bar need to run under Wayland"); - } - wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj()); +waybar::Client::Client() {} + +waybar::Client *waybar::Client::inst() { + static Client *c = new Client(); + return c; } -const std::string waybar::Client::getValidPath(std::vector paths) -{ +const std::string waybar::Client::getValidPath(std::vector paths) { wordexp_t p; - for (const std::string &path: paths) { + for (const std::string &path : paths) { if (wordexp(path.c_str(), &p, 0) == 0) { if (access(*p.we_wordv, F_OK) == 0) { std::string result = *p.we_wordv; @@ -33,77 +28,189 @@ const std::string waybar::Client::getValidPath(std::vector paths) return std::string(); } -void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, uint32_t version) -{ - auto o = static_cast(data); +void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, uint32_t name, + const char *interface, uint32_t version) { + auto client = static_cast(data); if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - o->layer_shell = static_cast( - wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, version)); + client->layer_shell = static_cast( + wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, version)); } else if (strcmp(interface, wl_output_interface.name) == 0) { - auto output = std::make_unique(); - *output = static_cast(wl_registry_bind(registry, name, - &wl_output_interface, version)); - if (o->xdg_output_manager != nullptr) { - o->bars.emplace_back(std::make_unique(*o, std::move(output), name)); - } + auto wl_output = static_cast( + 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) { - o->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) { - o->xdg_output_manager = static_cast( - wl_registry_bind(registry, name, - &zxdg_output_manager_v1_interface, ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)); + 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) { - o->idle_inhibit_manager = static_cast( - wl_registry_bind(registry, name, - &zwp_idle_inhibit_manager_v1_interface, 1)); + waybar::Client::inst()->idle_inhibit_manager = + static_cast( + wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1)); } } -void waybar::Client::handleGlobalRemove(void* data, - struct wl_registry* /*registry*/, uint32_t name) -{ - auto o = static_cast(data); - for (auto it = o->bars.begin(); it != o->bars.end(); ++it) { - if ((*it)->wl_name == name) { - auto output_name = (*it)->output_name; - o->bars.erase(it); +void waybar::Client::handleGlobalRemove(void *data, struct wl_registry * /*registry*/, + uint32_t name) { + auto client = static_cast(data); + for (auto it = client->bars.begin(); it != client->bars.end();) { + if ((*it)->output->wl_name == name) { + auto output_name = (*it)->output->name; + (*it)->window.close(); + it = client->bars.erase(it); std::cout << "Bar removed from output: " + output_name << std::endl; - break; + } else { + ++it; } } + auto it = std::find_if(client->outputs_.begin(), client->outputs_.end(), + [&name](const auto &output) { return output->wl_name == name; }); + if (it != client->outputs_.end()) { + zxdg_output_v1_destroy((*it)->xdg_output); + wl_output_destroy((*it)->output); + client->outputs_.erase(it); + } } -void waybar::Client::setupConfigs(const std::string& config, const std::string& style) -{ - config_file = config.empty() ? getValidPath({ - "$XDG_CONFIG_HOME/waybar/config", - "$HOME/.config/waybar/config", - "$HOME/waybar/config", - "/etc/xdg/waybar/config", - "./resources/config", - }) : config; - css_file = style.empty() ? getValidPath({ - "$XDG_CONFIG_HOME/waybar/style.css", - "$HOME/.config/waybar/style.css", - "$HOME/waybar/style.css", - "/etc/xdg/waybar/style.css", - "./resources/style.css", - }) : style; - if (css_file.empty() || config_file.empty()) { +void waybar::Client::handleOutput(std::unique_ptr &output) { + static const struct zxdg_output_v1_listener xdgOutputListener = { + .logical_position = handleLogicalPosition, + .logical_size = handleLogicalSize, + .done = handleDone, + .name = handleName, + .description = handleDescription, + }; + output->xdg_output = zxdg_output_manager_v1_get_xdg_output(xdg_output_manager, output->output); + zxdg_output_v1_add_listener(output->xdg_output, &xdgOutputListener, &output->wl_name); +} + +void waybar::Client::handleLogicalPosition(void * /*data*/, + struct zxdg_output_v1 * /*zxdg_output_v1*/, + int32_t /*x*/, int32_t /*y*/) { + // Nothing here +} + +void waybar::Client::handleLogicalSize(void * /*data*/, struct zxdg_output_v1 * /*zxdg_output_v1*/, + int32_t /*width*/, int32_t /*height*/) { + // Nothing here +} + +void waybar::Client::handleDone(void * /*data*/, struct zxdg_output_v1 * /*zxdg_output_v1*/) { + // Nothing here +} + +bool waybar::Client::isValidOutput(const Json::Value &config, + std::unique_ptr &output) { + bool found = true; + if (config["output"].isArray()) { + bool in_array = false; + for (auto const &output_conf : config["output"]) { + if (output_conf.isString() && output_conf.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::Client::handleName(void *data, struct zxdg_output_v1 * /*xdg_output*/, + const char *name) { + auto wl_name = *static_cast(data); + auto client = waybar::Client::inst(); + auto it = std::find_if(client->outputs_.begin(), client->outputs_.end(), + [&wl_name](const auto &output) { return output->wl_name == wl_name; }); + if (it == client->outputs_.end()) { + std::cerr << "Unable to find valid output" << std::endl; + return; + } + (*it)->name = name; + bool found = true; + if (client->config_.isArray()) { + bool in_array = false; + for (auto const &config : client->config_) { + if (config.isObject() && client->isValidOutput(config, *it)) { + in_array = true; + (*it)->config = config; + break; + } + } + found = in_array; + } else { + (*it)->config = client->config_; + found = client->isValidOutput((*it)->config, *it); + } + if (!found) { + wl_output_destroy((*it)->output); + zxdg_output_v1_destroy((*it)->xdg_output); + } else { + client->bars.emplace_back(std::make_unique(it->get())); + Glib::RefPtr screen = client->bars.back()->window.get_screen(); + client->style_context_->add_provider_for_screen(screen, client->css_provider_, + GTK_STYLE_PROVIDER_PRIORITY_USER); + } +} + +void waybar::Client::handleDescription(void * /*data*/, struct zxdg_output_v1 * /*zxdg_output_v1*/, + const char * /*description*/) { + // Nothing here +} + +void waybar::Client::setupConfigs(const std::string &config, const std::string &style) { + config_file_ = config.empty() ? getValidPath({ + "$XDG_CONFIG_HOME/waybar/config", + "$HOME/.config/waybar/config", + "$HOME/waybar/config", + "/etc/xdg/waybar/config", + "./resources/config", + }) + : config; + css_file_ = style.empty() ? getValidPath({ + "$XDG_CONFIG_HOME/waybar/style.css", + "$HOME/.config/waybar/style.css", + "$HOME/waybar/style.css", + "/etc/xdg/waybar/style.css", + "./resources/style.css", + }) + : style; + if (css_file_.empty() || config_file_.empty()) { throw std::runtime_error("Missing required resources files"); } - std::cout << "Resources files: " + config_file + ", " + css_file << std::endl; + std::cout << "Resources files: " + config_file_ + ", " + css_file_ << std::endl; } -void waybar::Client::bindInterfaces() -{ +auto waybar::Client::setupConfig() -> void { + std::ifstream file(config_file_); + if (!file.is_open()) { + throw std::runtime_error("Can't open config file"); + } + std::string str((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + util::JsonParser parser; + config_ = parser.parse(str); +} + +auto waybar::Client::setupCss() -> void { + css_provider_ = Gtk::CssProvider::create(); + style_context_ = Gtk::StyleContext::create(); + + // Load our css file, wherever that may be hiding + if (!css_provider_->load_from_path(css_file_)) { + throw std::runtime_error("Can't open style file"); + } +} + +void waybar::Client::bindInterfaces() { registry = wl_display_get_registry(wl_display); static const struct wl_registry_listener registry_listener = { - .global = handleGlobal, - .global_remove = handleGlobalRemove, + .global = handleGlobal, + .global_remove = handleGlobalRemove, }; wl_registry_add_listener(registry, ®istry_listener, this); wl_display_roundtrip(wl_display); @@ -113,18 +220,26 @@ void waybar::Client::bindInterfaces() wl_display_roundtrip(wl_display); } -int waybar::Client::main(int argc, char* argv[]) -{ +int waybar::Client::main(int argc, char *argv[]) { + gtk_app = Gtk::Application::create(argc, argv, "fr.arouillard.waybar"); + gdk_display = Gdk::Display::get_default(); + if (!gdk_display) { + throw std::runtime_error("Can't find display"); + } + if (!GDK_IS_WAYLAND_DISPLAY(gdk_display->gobj())) { + throw std::runtime_error("Bar need to run under Wayland"); + } + wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj()); bool show_help = false; bool show_version = false; std::string config; std::string style; std::string bar_id; - auto cli = clara::detail::Help(show_help) - | clara::detail::Opt(show_version)["-v"]["--version"]("Show version") - | clara::detail::Opt(config, "config")["-c"]["--config"]("Config path") - | clara::detail::Opt(style, "style")["-s"]["--style"]("Style path") - | clara::detail::Opt(bar_id, "id")["-b"]["--bar"]("Bar id"); + auto cli = clara::detail::Help(show_help) | + clara::detail::Opt(show_version)["-v"]["--version"]("Show version") | + clara::detail::Opt(config, "config")["-c"]["--config"]("Config path") | + clara::detail::Opt(style, "style")["-s"]["--style"]("Style path") | + clara::detail::Opt(bar_id, "id")["-b"]["--bar"]("Bar id"); auto res = cli.parse(clara::detail::Args(argc, argv)); if (!res) { std::cerr << "Error in command line: " << res.errorMessage() << std::endl; @@ -139,6 +254,8 @@ int waybar::Client::main(int argc, char* argv[]) return 0; } setupConfigs(config, style); + setupConfig(); + setupCss(); bindInterfaces(); gtk_app->hold(); gtk_app->run(); diff --git a/src/main.cpp b/src/main.cpp index c3909aa..59471aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,31 +2,26 @@ #include #include "client.hpp" -namespace waybar { - -static Client* client; - -} // namespace waybar - int main(int argc, char* argv[]) { try { - waybar::Client c(argc, argv); - waybar::client = &c; + auto client = waybar::Client::inst(); std::signal(SIGUSR1, [](int /*signal*/) { - for (auto& bar : waybar::client->bars) { + for (auto& bar : waybar::Client::inst()->bars) { bar->toggle(); } }); for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) { - std::signal(sig, [](int sig /*signal*/) { - for (auto& bar : waybar::client->bars) { + std::signal(sig, [](int sig) { + for (auto& bar : waybar::Client::inst()->bars) { bar->handleSignal(sig); } }); } - return c.main(argc, argv); + auto ret = client->main(argc, argv); + delete client; + return ret; } catch (const std::exception& e) { std::cerr << e.what() << std::endl; return 1; diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index 430433f..7de1013 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -1,9 +1,9 @@ #include "modules/idle_inhibitor.hpp" #include "util/command.hpp" -waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar, const Json::Value& config) - : ALabel(config, "{status}"), bar_(bar), status_("deactivated"), idle_inhibitor_(nullptr) -{ +waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar, + const Json::Value& config) + : ALabel(config, "{status}"), bar_(bar), status_("deactivated"), idle_inhibitor_(nullptr) { label_.set_name("idle_inhibitor"); if (!id.empty()) { label_.get_style_context()->add_class(id); @@ -14,21 +14,18 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& dp.emit(); } -waybar::modules::IdleInhibitor::~IdleInhibitor() -{ - if(idle_inhibitor_) { +waybar::modules::IdleInhibitor::~IdleInhibitor() { + if (idle_inhibitor_) { zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); idle_inhibitor_ = nullptr; } } -auto waybar::modules::IdleInhibitor::update() -> void -{ +auto waybar::modules::IdleInhibitor::update() -> void { label_.set_markup( - fmt::format(format_, fmt::arg("status", status_), - fmt::arg("icon", getIcon(0, status_)))); + fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_)))); label_.get_style_context()->add_class(status_); - if(tooltipEnabled()) { + if (tooltipEnabled()) { label_.set_tooltip_text(status_); } } @@ -39,10 +36,10 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { if (idle_inhibitor_) { zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); idle_inhibitor_ = nullptr; - status_ = "deactivated"; + status_ = "deactivated"; } else { idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor( - bar_.client.idle_inhibit_manager, bar_.surface); + waybar::Client::inst()->idle_inhibit_manager, bar_.surface); status_ = "activated"; } if (config_["on-click"].isString() && e->button == 1) { diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 36587e6..55e1f39 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -72,7 +72,7 @@ void waybar::modules::Network::createInfoSocket() } { ev_fd_ = eventfd(0, EFD_NONBLOCK); - struct epoll_event event; + struct epoll_event event = {0}; event.events = EPOLLIN | EPOLLET; event.data.fd = ev_fd_; if (epoll_ctl(efd_, EPOLL_CTL_ADD, ev_fd_, &event) == -1) { @@ -81,7 +81,7 @@ void waybar::modules::Network::createInfoSocket() } { auto fd = nl_socket_get_fd(info_sock_); - struct epoll_event event; + struct epoll_event event = {0}; event.events = EPOLLIN | EPOLLET | EPOLLRDHUP; event.data.fd = fd; if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &event) == -1) { @@ -114,7 +114,7 @@ void waybar::modules::Network::worker() } thread_timer_.sleep_for(interval_); }; - struct epoll_event events[EPOLL_MAX]; + struct epoll_event events[EPOLL_MAX] = {{0}}; thread_ = [this, &events] { int ec = epoll_wait(efd_, events, EPOLL_MAX, -1); if (ec > 0) { diff --git a/src/modules/sni/host.cpp b/src/modules/sni/host.cpp index e12d0bc..8cfe5dd 100644 --- a/src/modules/sni/host.cpp +++ b/src/modules/sni/host.cpp @@ -16,7 +16,19 @@ Host::Host(const std::size_t id, const Json::Value& config, on_add_(on_add), on_remove_(on_remove) {} -Host::~Host() { Gio::DBus::unwatch_name(bus_name_id_); } +Host::~Host() { + if (bus_name_id_ > 0) { + Gio::DBus::unwatch_name(bus_name_id_); + bus_name_id_ = 0; + } + if (watcher_id_ > 0) { + Gio::DBus::unwatch_name(watcher_id_); + watcher_id_ = 0; + } + g_cancellable_cancel(cancellable_); + g_clear_object(&cancellable_); + g_clear_object(&watcher_); +} void Host::busAcquired(const Glib::RefPtr& conn, Glib::ustring name) { watcher_id_ = Gio::DBus::watch_name(conn, "org.kde.StatusNotifierWatcher", diff --git a/src/modules/sni/tray.cpp b/src/modules/sni/tray.cpp index 746656a..c2578aa 100644 --- a/src/modules/sni/tray.cpp +++ b/src/modules/sni/tray.cpp @@ -8,7 +8,6 @@ waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar, const Js watcher_(), host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1), std::bind(&Tray::onRemove, this, std::placeholders::_1)) { - std::cout << "Tray is in beta, so there may be bugs or even be unusable." << std::endl; if (config_["spacing"].isUInt()) { box_.set_spacing(config_["spacing"].asUInt()); } diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index 3f1bcef..8a12eb7 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -1,11 +1,11 @@ #include "modules/sway/workspaces.hpp" -waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar& bar, - const Json::Value& config) - : bar_(bar), config_(config), - box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), - scrolling_(false) -{ +waybar::modules::sway::Workspaces::Workspaces(const std::string &id, const Bar &bar, + const Json::Value &config) + : bar_(bar), + config_(config), + box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), + scrolling_(false) { box_.set_name("workspaces"); if (!id.empty()) { box_.get_style_context()->add_class(id); @@ -15,8 +15,7 @@ waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar& worker(); } -void waybar::modules::sway::Workspaces::worker() -{ +void waybar::modules::sway::Workspaces::worker() { thread_ = [this] { try { if (!workspaces_.empty()) { @@ -24,36 +23,35 @@ void waybar::modules::sway::Workspaces::worker() } { std::lock_guard lock(mutex_); - auto res = ipc_.sendCmd(IPC_GET_WORKSPACES); + auto res = ipc_.sendCmd(IPC_GET_WORKSPACES); if (thread_.isRunning()) { workspaces_ = parser_.parse(res.payload); } } dp.emit(); - } catch (const std::exception& e) { + } catch (const std::exception &e) { std::cerr << "Workspaces: " << e.what() << std::endl; } }; } -auto waybar::modules::sway::Workspaces::update() -> void -{ - bool needReorder = false; +auto waybar::modules::sway::Workspaces::update() -> void { + bool needReorder = false; std::lock_guard lock(mutex_); for (auto it = buttons_.begin(); it != buttons_.end();) { - auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), - [it](auto node) -> bool { return node["name"].asString() == it->first; }); + auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), [it](auto node) -> bool { + return node["name"].asString() == it->first; + }); if (ws == workspaces_.end() || - (!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output_name)) { - it = buttons_.erase(it); + (!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output->name)) { + it = buttons_.erase(it); needReorder = true; } else { ++it; } } - for (auto const& node : workspaces_) { - if (!config_["all-outputs"].asBool() - && bar_.output_name != node["output"].asString()) { + for (auto const &node : workspaces_) { + if (!config_["all-outputs"].asBool() && bar_.output->name != node["output"].asString()) { continue; } auto it = buttons_.find(node["name"].asString()); @@ -80,16 +78,17 @@ auto waybar::modules::sway::Workspaces::update() -> void if (needReorder) { box_.reorder_child(button, getWorkspaceIndex(node["name"].asString())); } - auto icon = getIcon(node["name"].asString(), node); + auto icon = getIcon(node["name"].asString(), node); std::string output = icon; if (config_["format"].isString()) { auto format = config_["format"].asString(); - output = fmt::format(format, fmt::arg("icon", icon), - fmt::arg("name", trimWorkspaceName(node["name"].asString())), - fmt::arg("index", node["num"].asString())); + output = fmt::format(format, + fmt::arg("icon", icon), + fmt::arg("name", trimWorkspaceName(node["name"].asString())), + fmt::arg("index", node["num"].asString())); } if (!config_["disable-markup"].asBool()) { - static_cast(button.get_children()[0])->set_markup(output); + static_cast(button.get_children()[0])->set_markup(output); } else { button.set_label(output); } @@ -101,34 +100,33 @@ auto waybar::modules::sway::Workspaces::update() -> void } } -void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) -{ - auto icon = getIcon(node["name"].asString(), node); +void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) { + auto icon = getIcon(node["name"].asString(), node); auto format = config_["format"].isString() - ? fmt::format(config_["format"].asString(), fmt::arg("icon", icon), - fmt::arg("name", trimWorkspaceName(node["name"].asString())), - fmt::arg("index", node["num"].asString())) - : icon; - auto pair = buttons_.emplace(node["name"].asString(), format); + ? fmt::format(config_["format"].asString(), + fmt::arg("icon", icon), + fmt::arg("name", trimWorkspaceName(node["name"].asString())), + fmt::arg("index", node["num"].asString())) + : icon; + auto pair = buttons_.emplace(node["name"].asString(), format); auto &button = pair.first->second; if (!config_["disable-markup"].asBool()) { - static_cast(button.get_children()[0])->set_markup(format); + static_cast(button.get_children()[0])->set_markup(format); } box_.pack_start(button, false, false, 0); button.set_relief(Gtk::RELIEF_NONE); button.signal_clicked().connect([this, pair] { try { std::lock_guard lock(mutex_); - auto cmd = fmt::format("workspace \"{}\"", pair.first->first); + auto cmd = fmt::format("workspace \"{}\"", pair.first->first); ipc_.sendCmd(IPC_COMMAND, cmd); - } catch (const std::exception& e) { + } catch (const std::exception &e) { std::cerr << e.what() << std::endl; } }); if (!config_["disable-scroll"].asBool()) { button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK); - button.signal_scroll_event() - .connect(sigc::mem_fun(*this, &Workspaces::handleScroll)); + button.signal_scroll_event().connect(sigc::mem_fun(*this, &Workspaces::handleScroll)); } box_.reorder_child(button, getWorkspaceIndex(node["name"].asString())); if (node["focused"].asBool()) { @@ -141,14 +139,13 @@ void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) button.get_style_context()->add_class("urgent"); } - onButtonReady(node, button); + onButtonReady(node, button); } std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name, - const Json::Value &node) -{ - std::vector keys = { name, "urgent", "focused", "visible", "default" }; - for (auto const& key : keys) { + const Json::Value &node) { + std::vector keys = {name, "urgent", "focused", "visible", "default"}; + for (auto const &key : keys) { if (key == "focused" || key == "visible" || key == "urgent") { if (config_["format-icons"][key].isString() && node[key].asBool()) { return config_["format-icons"][key].asString(); @@ -160,14 +157,13 @@ std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name, return name; } -bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) -{ +bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) { // Avoid concurrent scroll event if (scrolling_) { return false; } std::lock_guard lock(mutex_); - uint8_t idx; + uint8_t idx; scrolling_ = true; for (idx = 0; idx < workspaces_.size(); idx += 1) { if (workspaces_[idx]["focused"].asBool()) { @@ -180,15 +176,14 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) } std::string name; if (e->direction == GDK_SCROLL_UP) { - name = getCycleWorkspace(idx, true); + name = getCycleWorkspace(idx, true); } if (e->direction == GDK_SCROLL_DOWN) { - name = getCycleWorkspace(idx, false); + name = getCycleWorkspace(idx, false); } if (e->direction == GDK_SCROLL_SMOOTH) { gdouble delta_x, delta_y; - gdk_event_get_scroll_deltas(reinterpret_cast(e), - &delta_x, &delta_y); + gdk_event_get_scroll_deltas(reinterpret_cast(e), &delta_x, &delta_y); if (delta_y < 0) { name = getCycleWorkspace(idx, true); } else if (delta_y > 0) { @@ -204,17 +199,17 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) return true; } -const std::string waybar::modules::sway::Workspaces::getCycleWorkspace( - uint8_t focused_workspace, bool prev) const -{ - auto inc = prev ? -1 : 1; - int size = workspaces_.size(); - uint8_t idx = 0; +const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(uint8_t focused_workspace, + bool prev) const { + auto inc = prev ? -1 : 1; + int size = workspaces_.size(); + uint8_t idx = 0; for (int i = focused_workspace; i < size && i >= 0; i += inc) { - bool same_output = (workspaces_[i]["output"].asString() == bar_.output_name - && !config_["all-outputs"].asBool()) || config_["all-outputs"].asBool(); + bool same_output = (workspaces_[i]["output"].asString() == bar_.output->name && + !config_["all-outputs"].asBool()) || + config_["all-outputs"].asBool(); bool same_name = - workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString(); + workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString(); if (same_output && !same_name) { return workspaces_[i]["name"].asString(); } @@ -230,42 +225,39 @@ const std::string waybar::modules::sway::Workspaces::getCycleWorkspace( return ""; } -uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const -{ +uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const { uint16_t idx = 0; for (const auto &workspace : workspaces_) { if (workspace["name"].asString() == name) { return idx; } - if (!(!config_["all-outputs"].asBool() && workspace["output"].asString() != bar_.output_name)) { + if (!(!config_["all-outputs"].asBool() && + workspace["output"].asString() != bar_.output->name)) { idx += 1; } } return workspaces_.size(); } -std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name) -{ +std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name) { std::size_t found = name.find(":"); - if (found!=std::string::npos) { - return name.substr(found+1); + if (found != std::string::npos) { + return name.substr(found + 1); } return name; } -void waybar::modules::sway::Workspaces::onButtonReady(const Json::Value& node, Gtk::Button& button) -{ - if (config_["current-only"].asBool()) { - if (node["focused"].asBool()) { - button.show(); - } else { - button.hide(); - } - } else { - button.show(); - } +void waybar::modules::sway::Workspaces::onButtonReady(const Json::Value &node, + Gtk::Button & button) { + if (config_["current-only"].asBool()) { + if (node["focused"].asBool()) { + button.show(); + } else { + button.hide(); + } + } else { + button.show(); + } } -waybar::modules::sway::Workspaces::operator Gtk::Widget &() { - return box_; -} +waybar::modules::sway::Workspaces::operator Gtk::Widget &() { return box_; }