From 311c34ecbc61a3dd4f1f9b4954693bf134e17604 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 24 Apr 2019 12:37:24 +0200 Subject: [PATCH] feat(Bar): handle widget size changes --- include/ALabel.hpp | 2 +- include/bar.hpp | 2 +- include/client.hpp | 4 +- include/modules/sni/item.hpp | 4 +- include/modules/sway/ipc/client.hpp | 4 +- include/modules/sway/mode.hpp | 2 +- include/modules/sway/window.hpp | 6 +-- include/modules/sway/workspaces.hpp | 4 +- resources/config | 2 +- src/ALabel.cpp | 6 +-- src/bar.cpp | 80 ++++++++++++++++++++--------- src/client.cpp | 8 ++- src/factory.cpp | 2 +- src/modules/backlight.cpp | 4 +- src/modules/battery.cpp | 2 +- src/modules/clock.cpp | 5 +- src/modules/cpu.cpp | 6 +-- src/modules/idle_inhibitor.cpp | 4 +- src/modules/memory.cpp | 6 +-- src/modules/network.cpp | 4 +- src/modules/pulseaudio.cpp | 23 ++++----- src/modules/sni/host.cpp | 9 ++-- src/modules/sni/item.cpp | 53 +++++++++---------- src/modules/sni/tray.cpp | 28 ++++++---- src/modules/sni/watcher.cpp | 22 ++++---- src/modules/sway/mode.cpp | 4 +- src/modules/sway/window.cpp | 8 +-- src/modules/sway/workspaces.cpp | 8 +-- 28 files changed, 175 insertions(+), 137 deletions(-) diff --git a/include/ALabel.hpp b/include/ALabel.hpp index d8e0f882..cf8e5619 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -10,7 +10,7 @@ namespace waybar { class ALabel : public IModule { public: - ALabel(const Json::Value &, const std::string format, uint16_t interval = 0); + ALabel(const Json::Value &, const std::string &format, uint16_t interval = 0); virtual ~ALabel(); virtual auto update() -> void; virtual std::string getIcon(uint16_t, const std::string &alt = ""); diff --git a/include/bar.hpp b/include/bar.hpp index 73de06bf..5c6b6758 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -56,7 +56,7 @@ class Bar { void setupAltFormatKeyForModuleList(const char *module_list_name); uint32_t width_ = 0; - uint32_t height_ = 30; + uint32_t height_ = 1; Gtk::Box left_; Gtk::Box center_; Gtk::Box right_; diff --git a/include/client.hpp b/include/client.hpp index 78eaf87d..06fb174a 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -25,10 +25,10 @@ class Client { std::vector> bars; private: - Client(); + Client() = default; void setupConfigs(const std::string &config, const std::string &style); void bindInterfaces(); - const std::string getValidPath(std::vector paths); + const std::string getValidPath(const std::vector &paths); void handleOutput(std::unique_ptr &output); bool isValidOutput(const Json::Value &config, std::unique_ptr &output); auto setupConfig() -> void; diff --git a/include/modules/sni/item.hpp b/include/modules/sni/item.hpp index 2da7776b..ee209956 100644 --- a/include/modules/sni/item.hpp +++ b/include/modules/sni/item.hpp @@ -20,7 +20,7 @@ namespace waybar::modules::SNI { class Item : public sigc::trackable { public: - Item(std::string, std::string, const Json::Value&); + Item(const std::string&, const std::string&, const Json::Value&); ~Item() = default; std::string bus_name; @@ -58,7 +58,7 @@ class Item : public sigc::trackable { void updateImage(); Glib::RefPtr extractPixBuf(GVariant* variant); - Glib::RefPtr getIconByName(std::string name, int size); + Glib::RefPtr getIconByName(const std::string& name, int size); static void onMenuDestroyed(Item* self); bool makeMenu(GdkEventButton* const& ev); bool handleClick(GdkEventButton* const& /*ev*/); diff --git a/include/modules/sway/ipc/client.hpp b/include/modules/sway/ipc/client.hpp index 48ec18df..6ab3c6cc 100644 --- a/include/modules/sway/ipc/client.hpp +++ b/include/modules/sway/ipc/client.hpp @@ -24,8 +24,8 @@ class Ipc { Json::Value payload; }; - sigc::signal signal_event; - sigc::signal signal_cmd; + sigc::signal signal_event; + sigc::signal signal_cmd; void sendCmd(uint32_t type, const std::string &payload = ""); void subscribe(const std::string &payload); diff --git a/include/modules/sway/mode.hpp b/include/modules/sway/mode.hpp index 9f3b0e02..6b787ea6 100644 --- a/include/modules/sway/mode.hpp +++ b/include/modules/sway/mode.hpp @@ -16,7 +16,7 @@ class Mode : public ALabel { auto update() -> void; private: - void onEvent(const struct Ipc::ipc_response); + void onEvent(const struct Ipc::ipc_response&); void worker(); const Bar& bar_; diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index d43ed25a..17527932 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -17,10 +17,10 @@ class Window : public ALabel { auto update() -> void; private: - void onEvent(const struct Ipc::ipc_response); - void onCmd(const struct Ipc::ipc_response); + void onEvent(const struct Ipc::ipc_response&); + void onCmd(const struct Ipc::ipc_response&); void worker(); - std::tuple getFocusedNode(Json::Value nodes); + std::tuple getFocusedNode(const Json::Value& nodes); void getFocusedWindow(); const Bar& bar_; diff --git a/include/modules/sway/workspaces.hpp b/include/modules/sway/workspaces.hpp index c7b6c92f..42aebdb6 100644 --- a/include/modules/sway/workspaces.hpp +++ b/include/modules/sway/workspaces.hpp @@ -19,8 +19,8 @@ class Workspaces : public IModule { operator Gtk::Widget&(); private: - void onCmd(const struct Ipc::ipc_response); - void onEvent(const struct Ipc::ipc_response); + void onCmd(const struct Ipc::ipc_response&); + void onEvent(const struct Ipc::ipc_response&); void worker(); bool filterButtons(); Gtk::Button& addButton(const Json::Value&); diff --git a/resources/config b/resources/config index 9aa669f5..2c1f69c3 100644 --- a/resources/config +++ b/resources/config @@ -1,7 +1,7 @@ { "layer": "top", // Waybar at top layer // "position": "bottom", // Waybar position (top|bottom|left|right) - // "height": 30, // Waybar height + "height": 30, // Waybar height (to be removed for auto height) // "width": 1280, // Waybar width // Choose the order of the modules "modules-left": ["sway/workspaces", "sway/mode", "custom/media"], diff --git a/src/ALabel.cpp b/src/ALabel.cpp index f72a90fe..315104c3 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -3,11 +3,11 @@ #include -waybar::ALabel::ALabel(const Json::Value& config, const std::string format, uint16_t interval) +waybar::ALabel::ALabel(const Json::Value& config, const std::string& format, uint16_t interval) : config_(config), format_(config_["format"].isString() ? config_["format"].asString() : format), interval_(config_["interval"] == "once" - ? std::chrono::seconds(100000000) + ? std::chrono::seconds(std::numeric_limits::infinity()) : std::chrono::seconds( config_["interval"].isUInt() ? config_["interval"].asUInt() : interval)), default_format_(format_) { @@ -33,7 +33,7 @@ waybar::ALabel::ALabel(const Json::Value& config, const std::string format, uint } waybar::ALabel::~ALabel() { - for (const auto &pid : pid_) { + for (const auto& pid : pid_) { if (pid != -1) { kill(-pid, 9); } diff --git a/src/bar.cpp b/src/bar.cpp index a718a790..effcc84b 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -17,7 +17,7 @@ waybar::Bar::Bar(struct waybar_output* w_output) if (output->config["position"] == "right" || output->config["position"] == "left") { height_ = 0; - width_ = 30; + width_ = 1; } window.set_size_request(width_, height_); @@ -42,6 +42,38 @@ waybar::Bar::Bar(struct waybar_output* w_output) auto height = output->config["height"].isUInt() ? output->config["height"].asUInt() : height_; auto width = output->config["width"].isUInt() ? output->config["width"].asUInt() : width_; + window.signal_configure_event().connect_notify([&](GdkEventConfigure* ev) { + auto tmp_height = height_; + auto tmp_width = width_; + if (ev->height > static_cast(height_)) { + // Default minimal value + if (height_ != 1) { + std::cout << fmt::format(MIN_HEIGHT_MSG, height_, ev->height) << std::endl; + } + if (output->config["height"].isUInt()) { + std::cout << "Height size is defined in the config file so it will stay like that" + << std::endl; + } else { + tmp_height = ev->height; + } + } + if (ev->width > static_cast(width_)) { + // Default minimal value + if (width_ != 1) { + std::cout << fmt::format(MIN_WIDTH_MSG, width_, ev->width) << std::endl; + } + if (output->config["width"].isUInt()) { + std::cout << "Height size is defined in the config file so it will stay like that" + << std::endl; + } else { + tmp_width = ev->width; + } + } + if (tmp_width != width_ || tmp_height != height_) { + zwlr_layer_surface_v1_set_size(layer_surface, tmp_width, tmp_height); + } + }); + std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; if (output->config["position"] == "bottom") { anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; @@ -83,21 +115,21 @@ void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) { std::string str_click = click.asString(); if (str_click == "click-right") { - module["format-alt-click"] = 3u; + module["format-alt-click"] = 3U; } else if (str_click == "click-middle") { - module["format-alt-click"] = 2u; + module["format-alt-click"] = 2U; } else if (str_click == "click-backward") { - module["format-alt-click"] = 8u; + module["format-alt-click"] = 8U; } else if (str_click == "click-forward") { - module["format-alt-click"] = 9u; + module["format-alt-click"] = 9U; } else { - module["format-alt-click"] = 1u; // default click-left + module["format-alt-click"] = 1U; // default click-left } } else { - module["format-alt-click"] = 1u; + module["format-alt-click"] = 1U; } } else { - module["format-alt-click"] = 1u; + module["format-alt-click"] = 1U; } } } @@ -117,15 +149,21 @@ void waybar::Bar::setupAltFormatKeyForModuleList(const char* module_list_name) { void waybar::Bar::handleSignal(int signal) { for (auto& module : modules_left_) { auto* custom = dynamic_cast(module.get()); - if (custom) custom->refresh(signal); + if (custom != nullptr) { + custom->refresh(signal); + } } for (auto& module : modules_center_) { auto* custom = dynamic_cast(module.get()); - if (custom) custom->refresh(signal); + if (custom != nullptr) { + custom->refresh(signal); + } } for (auto& module : modules_right_) { auto* custom = dynamic_cast(module.get()); - if (custom) custom->refresh(signal); + if (custom != nullptr) { + custom->refresh(signal); + } } } @@ -137,19 +175,13 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf o->height_ = height; o->window.set_size_request(o->width_, o->height_); o->window.resize(o->width_, o->height_); - - int min_width, min_height; - o->window.get_size(min_width, min_height); - if (o->height_ < static_cast(min_height)) { - 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(MIN_WIDTH_MSG, o->height_, min_width) << std::endl; - o->width_ = min_width; - } - std::cout << fmt::format(BAR_SIZE_MSG, o->width_, o->height_, o->output->name) << std::endl; - + zwlr_layer_surface_v1_set_exclusive_zone(o->layer_surface, + o->vertical ? o->width_ : o->height_); + std::cout << fmt::format(BAR_SIZE_MSG, + o->width_ == 1 ? "auto" : std::to_string(o->width_), + o->height_ == 1 ? "auto" : std::to_string(o->height_), + o->output->name) + << std::endl; wl_surface_commit(o->surface); } zwlr_layer_surface_v1_ack_configure(surface, serial); diff --git a/src/client.cpp b/src/client.cpp index 752ad910..c4b9ce19 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -4,14 +4,12 @@ #include "util/clara.hpp" #include "util/json.hpp" -waybar::Client::Client() {} - waybar::Client *waybar::Client::inst() { - static Client *c = new Client(); + static auto c = new Client(); return c; } -const std::string waybar::Client::getValidPath(std::vector paths) { +const std::string waybar::Client::getValidPath(const std::vector &paths) { wordexp_t p; for (const std::string &path : paths) { @@ -212,7 +210,7 @@ void waybar::Client::bindInterfaces() { }; wl_registry_add_listener(registry, ®istry_listener, this); wl_display_roundtrip(wl_display); - if (!layer_shell || !xdg_output_manager) { + if (layer_shell == nullptr || xdg_output_manager == nullptr) { throw std::runtime_error("Failed to acquire required resources."); } } diff --git a/src/factory.cpp b/src/factory.cpp index 974d2765..d5015fe2 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -4,7 +4,7 @@ waybar::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_(bar), waybar::IModule* waybar::Factory::makeModule(const std::string& name) const { try { - auto hash_pos = name.find("#"); + auto hash_pos = name.find('#'); auto ref = name.substr(0, hash_pos); auto id = hash_pos != std::string::npos ? name.substr(hash_pos + 1) : ""; if (ref == "battery") { diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index 2cdd5a9e..ea09b4ef 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -120,9 +120,9 @@ waybar::modules::Backlight::Backlight(const std::string &name, const Json::Value auto udev_fd = udev_monitor_get_fd(mon.get()); - auto epoll_fd = FileDescriptor{epoll_create1(0)}; + auto epoll_fd = FileDescriptor{epoll_create1(EPOLL_CLOEXEC)}; check_neq(epoll_fd.get(), -1, "epoll init failed: "); - epoll_event ctl_event; + epoll_event ctl_event{}; ctl_event.events = EPOLLIN; ctl_event.data.fd = udev_fd; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index aabdcd68..f2e6a88c 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -115,7 +115,7 @@ const std::string waybar::modules::Battery::getState(uint8_t capacity) const { if (config_["states"].isObject()) { for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) { if (it->isUInt() && it.key().isString()) { - states.push_back({it.key().asString(), it->asUInt()}); + states.emplace_back(it.key().asString(), it->asUInt()); } } } diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 3822a2da..de0c27ed 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -14,10 +14,11 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) auto sub_m = std::chrono::duration_cast(time_s.time_since_epoch()).count() % interval_.count(); - if (sub_m > 0) + if (sub_m > 0) { thread_.sleep_until(timeout - std::chrono::seconds(sub_m - 1)); - else + } else { thread_.sleep_until(timeout - std::chrono::seconds(sub_m)); + } }; } diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index da092119..880a05a5 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -25,7 +25,7 @@ auto waybar::modules::Cpu::update() -> void { uint16_t waybar::modules::Cpu::getCpuLoad() { struct sysinfo info = {0}; if (sysinfo(&info) == 0) { - float f_load = 1.f / (1u << SI_LOAD_SHIFT); + float f_load = 1.F / (1U << SI_LOAD_SHIFT); uint16_t load = info.loads[0] * f_load * 100 / get_nprocs(); return load; } @@ -70,7 +70,7 @@ std::vector> waybar::modules::Cpu::parseCpuinfo() { } std::stringstream sline(line.substr(5)); std::vector times; - for (size_t time; sline >> time; times.push_back(time)) + for (size_t time = 0; sline >> time; times.push_back(time)) ; size_t idle_time = 0; @@ -79,7 +79,7 @@ std::vector> waybar::modules::Cpu::parseCpuinfo() { idle_time = times[3]; total_time = std::accumulate(times.begin(), times.end(), 0); } - cpuinfo.push_back({idle_time, total_time}); + cpuinfo.emplace_back(idle_time, total_time); } return cpuinfo; } diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index 8eeab619..afade55f 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -19,7 +19,7 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& } waybar::modules::IdleInhibitor::~IdleInhibitor() { - if (idle_inhibitor_) { + if (idle_inhibitor_ != nullptr) { zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); idle_inhibitor_ = nullptr; } @@ -41,7 +41,7 @@ auto waybar::modules::IdleInhibitor::update() -> void { bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { if (e->button == 1) { label_.get_style_context()->remove_class(status_); - if (idle_inhibitor_) { + if (idle_inhibitor_ != nullptr) { zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); idle_inhibitor_ = nullptr; status_ = "deactivated"; diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index d8f35c08..1ebaef39 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -28,19 +28,19 @@ auto waybar::modules::Memory::update() -> void { } void waybar::modules::Memory::parseMeminfo() { - long memfree = -1, membuffer = -1, memcache = -1, memavail = -1; + int64_t memfree = -1, membuffer = -1, memcache = -1, memavail = -1; std::ifstream info(data_dir_); if (!info.is_open()) { throw std::runtime_error("Can't open " + data_dir_); } std::string line; while (getline(info, line)) { - auto posDelim = line.find(":"); + auto posDelim = line.find(':'); if (posDelim == std::string::npos) { continue; } std::string name = line.substr(0, posDelim); - long value = std::stol(line.substr(posDelim + 1)); + int64_t value = std::stol(line.substr(posDelim + 1)); if (name.compare("MemTotal") == 0) { memtotal_ = value; diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 5ba364db..0a041db1 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -68,7 +68,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_VALID, NL_CB_CUSTOM, handleEvents, this); - efd_ = epoll_create1(0); + efd_ = epoll_create1(EPOLL_CLOEXEC); if (efd_ < 0) { throw std::runtime_error("Can't create epoll"); } @@ -114,7 +114,7 @@ void waybar::modules::Network::worker() { } thread_timer_.sleep_for(interval_); }; - std::array events; + std::array events{}; thread_ = [this, &events] { int ec = epoll_wait(efd_, events.data(), EPOLL_MAX, -1); if (ec > 0) { diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 7cb095c8..ed2fd67c 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -99,9 +99,13 @@ bool waybar::modules::Pulseaudio::handleScroll(GdkEventScroll *e) { } if (direction_up) { - if (volume_ + 1 < 100) pa_cvolume_inc(&pa_volume, change); + if (volume_ + 1 < 100) { + pa_cvolume_inc(&pa_volume, change); + } } else { - if (volume_ - 1 > 0) pa_cvolume_dec(&pa_volume, change); + if (volume_ - 1 > 0) { + pa_cvolume_dec(&pa_volume, change); + } } pa_context_set_sink_volume_by_index(context_, sink_idx_, &pa_volume, volumeModifyCb, this); @@ -116,13 +120,8 @@ void waybar::modules::Pulseaudio::subscribeCb(pa_context * conte pa_subscription_event_type_t type, uint32_t idx, void *data) { unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK; - - switch (facility) { - case PA_SUBSCRIPTION_EVENT_SINK: - pa_context_get_sink_info_by_index(context, idx, sinkInfoCb, data); - break; - default: - break; + if (facility == PA_SUBSCRIPTION_EVENT_SINK) { + pa_context_get_sink_info_by_index(context, idx, sinkInfoCb, data); } } @@ -131,7 +130,7 @@ void waybar::modules::Pulseaudio::subscribeCb(pa_context * conte */ void waybar::modules::Pulseaudio::volumeModifyCb(pa_context *c, int success, void *data) { auto pa = static_cast(data); - if (success) { + if (success != 0) { pa_context_get_sink_info_by_index(pa->context_, pa->sink_idx_, sinkInfoCb, data); } } @@ -146,10 +145,10 @@ void waybar::modules::Pulseaudio::sinkInfoCb(pa_context * /*context*/, const pa_ pa->pa_volume_ = i->volume; float volume = static_cast(pa_cvolume_avg(&(pa->pa_volume_))) / float{PA_VOLUME_NORM}; pa->sink_idx_ = i->index; - pa->volume_ = std::round(volume * 100.0f); + pa->volume_ = std::round(volume * 100.0F); pa->muted_ = i->mute != 0; pa->desc_ = i->description; - pa->port_name_ = i->active_port ? i->active_port->name : "Unknown"; + pa->port_name_ = i->active_port != nullptr ? i->active_port->name : "Unknown"; pa->dp.emit(); } } diff --git a/src/modules/sni/host.cpp b/src/modules/sni/host.cpp index 2be0ba8a..b9204bcc 100644 --- a/src/modules/sni/host.cpp +++ b/src/modules/sni/host.cpp @@ -1,8 +1,7 @@ #include "modules/sni/host.hpp" - #include -using namespace waybar::modules::SNI; +namespace waybar::modules::SNI { Host::Host(const std::size_t id, const Json::Value& config, const std::function&)>& on_add, @@ -96,7 +95,7 @@ void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) { g_signal_connect(host->watcher_, "item-registered", G_CALLBACK(&Host::itemRegistered), data); g_signal_connect(host->watcher_, "item-unregistered", G_CALLBACK(&Host::itemUnregistered), data); auto items = sn_watcher_dup_registered_items(host->watcher_); - if (items) { + if (items != nullptr) { for (uint32_t i = 0; items[i] != nullptr; i += 1) { host->addRegisteredItem(items[i]); } @@ -122,7 +121,7 @@ void Host::itemUnregistered(SnWatcher* watcher, const gchar* service, gpointer d } std::tuple Host::getBusNameAndObjectPath(const std::string service) { - auto it = service.find("/"); + auto it = service.find('/'); if (it != std::string::npos) { return {service.substr(0, it), service.substr(it)}; } @@ -134,3 +133,5 @@ void Host::addRegisteredItem(std::string service) { items_.emplace_back(new Item(bus_name, object_path, config_)); on_add_(items_.back()); } + +} \ No newline at end of file diff --git a/src/modules/sni/item.cpp b/src/modules/sni/item.cpp index 601f8356..cfb5fef0 100644 --- a/src/modules/sni/item.cpp +++ b/src/modules/sni/item.cpp @@ -1,14 +1,13 @@ #include "modules/sni/item.hpp" - #include #include -using namespace Glib; +namespace waybar::modules::SNI { -static const ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name; -static const unsigned UPDATE_DEBOUNCE_TIME = 10; +static const Glib::ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name; +static const unsigned UPDATE_DEBOUNCE_TIME = 10; -waybar::modules::SNI::Item::Item(std::string bn, std::string op, const Json::Value& config) +Item::Item(const std::string& bn, const std::string& op, const Json::Value& config) : bus_name(bn), object_path(op), icon_size(16), @@ -34,7 +33,7 @@ waybar::modules::SNI::Item::Item(std::string bn, std::string op, const Json::Val interface); } -void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr& result) { +void Item::proxyReady(Glib::RefPtr& result) { try { this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result); /* Properties are already cached during object creation */ @@ -69,11 +68,11 @@ void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr& resu } template -T get_variant(VariantBase& value) { - return VariantBase::cast_dynamic>(value).get(); +T get_variant(Glib::VariantBase& value) { + return Glib::VariantBase::cast_dynamic>(value).get(); } -void waybar::modules::SNI::Item::setProperty(const ustring& name, VariantBase& value) { +void Item::setProperty(const Glib::ustring& name, Glib::VariantBase& value) { if (name == "Category") { category = get_variant(value); } else if (name == "Id") { @@ -112,29 +111,30 @@ void waybar::modules::SNI::Item::setProperty(const ustring& name, VariantBase& v } } -void waybar::modules::SNI::Item::getUpdatedProperties() { +void Item::getUpdatedProperties() { update_pending_ = false; - auto params = VariantContainerBase::create_tuple({Variant::create(SNI_INTERFACE_NAME)}); + auto params = Glib::VariantContainerBase::create_tuple( + {Glib::Variant::create(SNI_INTERFACE_NAME)}); proxy_->call("org.freedesktop.DBus.Properties.GetAll", sigc::mem_fun(*this, &Item::processUpdatedProperties), params); }; -void waybar::modules::SNI::Item::processUpdatedProperties(Glib::RefPtr& _result) { +void Item::processUpdatedProperties(Glib::RefPtr& _result) { try { auto result = proxy_->call_finish(_result); // extract "a{sv}" from VariantContainerBase - Variant> properties_variant; + Glib::Variant> properties_variant; result.get_child(properties_variant); auto properties = properties_variant.get(); for (const auto& [name, value] : properties) { - VariantBase old_value; + Glib::VariantBase old_value; proxy_->get_cached_property(old_value, name); if (!value.equal(old_value)) { proxy_->set_cached_property(name, value); - setProperty(name, const_cast(value)); + setProperty(name, const_cast(value)); } } @@ -147,8 +147,8 @@ void waybar::modules::SNI::Item::processUpdatedProperties(Glib::RefPtr waybar::modules::SNI::Item::extractPixBuf(GVariant* variant) { +Glib::RefPtr Item::extractPixBuf(GVariant* variant) { GVariantIter* it; g_variant_get(variant, "a(iiay)", &it); if (it == nullptr) { @@ -213,7 +213,7 @@ Glib::RefPtr waybar::modules::SNI::Item::extractPixBuf(GVariant* va return Glib::RefPtr{}; } -void waybar::modules::SNI::Item::updateImage() { +void Item::updateImage() { image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU); image.set_pixel_size(icon_size); if (!icon_name.empty()) { @@ -244,8 +244,7 @@ void waybar::modules::SNI::Item::updateImage() { } } -Glib::RefPtr waybar::modules::SNI::Item::getIconByName(std::string name, - int request_size) { +Glib::RefPtr Item::getIconByName(const std::string& name, int request_size) { int tmp_size = 0; icon_theme->rescan_if_needed(); auto sizes = icon_theme->get_icon_sizes(name.c_str()); @@ -277,12 +276,12 @@ Glib::RefPtr waybar::modules::SNI::Item::getIconByName(std::string name.c_str(), tmp_size, Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE); } -void waybar::modules::SNI::Item::onMenuDestroyed(Item* self) { +void Item::onMenuDestroyed(Item* self) { self->gtk_menu = nullptr; self->dbus_menu = nullptr; } -bool waybar::modules::SNI::Item::makeMenu(GdkEventButton* const& ev) { +bool Item::makeMenu(GdkEventButton* const& ev) { if (gtk_menu == nullptr) { if (!menu.empty()) { dbus_menu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data()); @@ -305,9 +304,9 @@ bool waybar::modules::SNI::Item::makeMenu(GdkEventButton* const& ev) { return false; } -bool waybar::modules::SNI::Item::handleClick(GdkEventButton* const& ev) { - auto parameters = VariantContainerBase::create_tuple( - {Variant::create(ev->x), Variant::create(ev->y)}); +bool Item::handleClick(GdkEventButton* const& ev) { + auto parameters = Glib::VariantContainerBase::create_tuple( + {Glib::Variant::create(ev->x), Glib::Variant::create(ev->y)}); if ((ev->button == 1 && item_is_menu) || ev->button == 3) { if (!makeMenu(ev)) { proxy_->call("ContextMenu", parameters); @@ -322,3 +321,5 @@ bool waybar::modules::SNI::Item::handleClick(GdkEventButton* const& ev) { } return false; } + +} // namespace waybar::modules::SNI \ No newline at end of file diff --git a/src/modules/sni/tray.cpp b/src/modules/sni/tray.cpp index c2578aab..500df42f 100644 --- a/src/modules/sni/tray.cpp +++ b/src/modules/sni/tray.cpp @@ -1,36 +1,42 @@ #include "modules/sni/tray.hpp" - #include -waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config) +namespace waybar::modules::SNI { + +Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config) : config_(config), box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), - watcher_(), host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1), std::bind(&Tray::onRemove, this, std::placeholders::_1)) { + box_.set_name("tray"); + if (!id.empty()) { + box_.get_style_context()->add_class(id); + } if (config_["spacing"].isUInt()) { box_.set_spacing(config_["spacing"].asUInt()); } nb_hosts_ += 1; + dp.emit(); } -void waybar::modules::SNI::Tray::onAdd(std::unique_ptr& item) { +void Tray::onAdd(std::unique_ptr& item) { box_.pack_start(item->event_box); dp.emit(); } -void waybar::modules::SNI::Tray::onRemove(std::unique_ptr& item) { +void Tray::onRemove(std::unique_ptr& item) { box_.remove(item->event_box); dp.emit(); } -auto waybar::modules::SNI::Tray::update() -> void { - if (box_.get_children().size() > 0) { - box_.set_name("tray"); - box_.show_all(); +auto Tray::update() -> void { + if (box_.get_children().empty()) { + box_.hide(); } else { - box_.set_name(""); + box_.show_all(); } } -waybar::modules::SNI::Tray::operator Gtk::Widget&() { return box_; } +Tray::operator Gtk::Widget&() { return box_; } + +} \ No newline at end of file diff --git a/src/modules/sni/watcher.cpp b/src/modules/sni/watcher.cpp index 366526fd..5799305f 100644 --- a/src/modules/sni/watcher.cpp +++ b/src/modules/sni/watcher.cpp @@ -19,14 +19,14 @@ Watcher::~Watcher() { bus_name_id_ = 0; } - if (hosts_ != NULL) { + if (hosts_ != nullptr) { g_slist_free_full(hosts_, gfWatchFree); - hosts_ = NULL; + hosts_ = nullptr; } - if (items_ != NULL) { + if (items_ != nullptr) { g_slist_free_full(items_, gfWatchFree); - items_ = NULL; + items_ = nullptr; } g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(watcher_)); } @@ -122,7 +122,7 @@ gboolean Watcher::handleRegisterItem(Watcher* obj, GDBusMethodInvocation* invoca Watcher::GfWatch* Watcher::gfWatchFind(GSList* list, const gchar* bus_name, const gchar* object_path) { for (GSList* l = list; l != nullptr; l = g_slist_next(l)) { - GfWatch* watch = static_cast(l->data); + auto watch = static_cast(l->data); if (g_strcmp0(watch->bus_name, bus_name) == 0 && g_strcmp0(watch->object_path, object_path) == 0) { return watch; @@ -132,11 +132,11 @@ Watcher::GfWatch* Watcher::gfWatchFind(GSList* list, const gchar* bus_name, } void Watcher::gfWatchFree(gpointer data) { - GfWatch* watch; + auto watch = static_cast(data); - watch = (GfWatch*)data; - - if (watch->watch_id > 0) g_bus_unwatch_name(watch->watch_id); + if (watch->watch_id > 0) { + g_bus_unwatch_name(watch->watch_id); + } g_free(watch->service); g_free(watch->bus_name); @@ -184,8 +184,8 @@ void Watcher::updateRegisteredItems(SnWatcher* obj) { GVariantBuilder builder; g_variant_builder_init(&builder, G_VARIANT_TYPE("as")); for (GSList* l = items_; l != nullptr; l = g_slist_next(l)) { - GfWatch* watch = static_cast(l->data); - gchar* item = g_strdup_printf("%s%s", watch->bus_name, watch->object_path); + auto watch = static_cast(l->data); + gchar* item = g_strdup_printf("%s%s", watch->bus_name, watch->object_path); g_variant_builder_add(&builder, "s", item); g_free(item); } diff --git a/src/modules/sway/mode.cpp b/src/modules/sway/mode.cpp index fabc2f97..f3aa8b0c 100644 --- a/src/modules/sway/mode.cpp +++ b/src/modules/sway/mode.cpp @@ -8,14 +8,14 @@ Mode::Mode(const std::string& id, const Bar& bar, const Json::Value& config) if (!id.empty()) { label_.get_style_context()->add_class(id); } - ipc_.subscribe("[ \"mode\" ]"); + ipc_.subscribe(R"(["mode"])"); ipc_.signal_event.connect(sigc::mem_fun(*this, &Mode::onEvent)); // Launch worker worker(); dp.emit(); } -void Mode::onEvent(const struct Ipc::ipc_response res) { +void Mode::onEvent(const struct Ipc::ipc_response &res) { if (res.payload["change"] != "default") { mode_ = res.payload["change"].asString(); } else { diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 192e6ff5..740fd44e 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -12,7 +12,7 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) label_.set_hexpand(true); label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END); } - ipc_.subscribe("[\"window\",\"workspace\"]"); + ipc_.subscribe(R"(["window","workspace"])"); ipc_.signal_event.connect(sigc::mem_fun(*this, &Window::onEvent)); ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Window::onCmd)); getFocusedWindow(); @@ -20,7 +20,7 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) worker(); } -void Window::onEvent(const struct Ipc::ipc_response res) { +void Window::onEvent(const struct Ipc::ipc_response& res) { auto data = res.payload; // Check for waybar prevents flicker when hovering window module if ((data["change"] == "focus" || data["change"] == "title") && @@ -38,7 +38,7 @@ void Window::onEvent(const struct Ipc::ipc_response res) { } } -void Window::onCmd(const struct Ipc::ipc_response res) { +void Window::onCmd(const struct Ipc::ipc_response& res) { auto [id, name] = getFocusedNode(res.payload["nodes"]); windowId_ = id; window_ = name; @@ -62,7 +62,7 @@ auto Window::update() -> void { } } -std::tuple Window::getFocusedNode(Json::Value nodes) { +std::tuple Window::getFocusedNode(const Json::Value& nodes) { for (auto const& node : nodes) { if (node["focused"].asBool() && node["type"] == "con") { return {node["id"].asInt(), node["name"].asString()}; diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index 401bb1ae..e38d91f7 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -11,7 +11,7 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value if (!id.empty()) { box_.get_style_context()->add_class(id); } - ipc_.subscribe("[ \"workspace\" ]"); + ipc_.subscribe(R"(["workspace"])"); ipc_.signal_event.connect(sigc::mem_fun(*this, &Workspaces::onEvent)); ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Workspaces::onCmd)); ipc_.sendCmd(IPC_GET_WORKSPACES); @@ -19,9 +19,9 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value worker(); } -void Workspaces::onEvent(const struct Ipc::ipc_response res) { ipc_.sendCmd(IPC_GET_WORKSPACES); } +void Workspaces::onEvent(const struct Ipc::ipc_response &res) { ipc_.sendCmd(IPC_GET_WORKSPACES); } -void Workspaces::onCmd(const struct Ipc::ipc_response res) { +void Workspaces::onCmd(const struct Ipc::ipc_response &res) { if (res.type == IPC_GET_WORKSPACES) { if (res.payload.isArray()) { std::lock_guard lock(mutex_); @@ -209,7 +209,7 @@ const std::string Workspaces::getCycleWorkspace(std::vector::iterat } std::string Workspaces::trimWorkspaceName(std::string name) { - std::size_t found = name.find(":"); + std::size_t found = name.find(':'); if (found != std::string::npos) { return name.substr(found + 1); }