From 047c2929c115783f0fbd6c0169e64af8eb127c02 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 19 Feb 2020 12:06:35 +0100 Subject: [PATCH 1/2] Use the same StatusNotifierWatcher for all trays --- include/modules/sni/tray.hpp | 2 +- include/modules/sni/watcher.hpp | 17 +++++++++++++++-- src/modules/sni/tray.cpp | 2 +- src/modules/sni/watcher.cpp | 6 +++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/modules/sni/tray.hpp b/include/modules/sni/tray.hpp index e0aced1f..aaa1e4f9 100644 --- a/include/modules/sni/tray.hpp +++ b/include/modules/sni/tray.hpp @@ -21,7 +21,7 @@ class Tray : public AModule { static inline std::size_t nb_hosts_ = 0; Gtk::Box box_; - SNI::Watcher watcher_; + SNI::Watcher::singleton watcher_; SNI::Host host_; }; diff --git a/include/modules/sni/watcher.hpp b/include/modules/sni/watcher.hpp index 599380af..5a55d2d3 100644 --- a/include/modules/sni/watcher.hpp +++ b/include/modules/sni/watcher.hpp @@ -7,10 +7,24 @@ namespace waybar::modules::SNI { class Watcher { + private: + Watcher(); + public: - Watcher(std::size_t id); ~Watcher(); + using singleton = std::shared_ptr; + static singleton getInstance() { + static std::weak_ptr weak; + + std::shared_ptr strong = weak.lock(); + if (!strong) { + strong = std::shared_ptr(new Watcher()); + weak = strong; + } + return strong; + } + private: typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType; @@ -34,7 +48,6 @@ class Watcher { void updateRegisteredItems(SnWatcher *obj); uint32_t bus_name_id_; - uint32_t watcher_id_; GSList * hosts_ = nullptr; GSList * items_ = nullptr; SnWatcher *watcher_ = nullptr; diff --git a/src/modules/sni/tray.cpp b/src/modules/sni/tray.cpp index a698a979..e16837f5 100644 --- a/src/modules/sni/tray.cpp +++ b/src/modules/sni/tray.cpp @@ -6,7 +6,7 @@ namespace waybar::modules::SNI { Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config) : AModule(config, "tray", id), box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), - watcher_(nb_hosts_), + watcher_(SNI::Watcher::getInstance()), host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1), std::bind(&Tray::onRemove, this, std::placeholders::_1)) { spdlog::warn( diff --git a/src/modules/sni/watcher.cpp b/src/modules/sni/watcher.cpp index 1db37088..73b3eaca 100644 --- a/src/modules/sni/watcher.cpp +++ b/src/modules/sni/watcher.cpp @@ -3,14 +3,13 @@ using namespace waybar::modules::SNI; -Watcher::Watcher(std::size_t id) +Watcher::Watcher() : bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, "org.kde.StatusNotifierWatcher", sigc::mem_fun(*this, &Watcher::busAcquired), Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(), Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)), - watcher_id_(id), watcher_(sn_watcher_skeleton_new()) {} Watcher::~Watcher() { @@ -23,6 +22,7 @@ Watcher::~Watcher() { g_slist_free_full(items_, gfWatchFree); items_ = nullptr; } + Gio::DBus::unown_name(bus_name_id_); auto iface = G_DBUS_INTERFACE_SKELETON(watcher_); g_dbus_interface_skeleton_unexport(iface); } @@ -34,7 +34,7 @@ void Watcher::busAcquired(const Glib::RefPtr& conn, Glib: if (error != nullptr) { // Don't print an error when a watcher is already present if (error->code != 2) { - spdlog::error("Watcher {}: {}", watcher_id_, error->message); + spdlog::error("Watcher: {}", error->message); } g_error_free(error); return; From 9a5f5114c438e358a300f99ce0089489d61c11aa Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 19 Feb 2020 12:28:36 +0100 Subject: [PATCH 2/2] pulse: track default source/sink changes --- src/modules/pulseaudio.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 1c709d0e..46dc4266 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -52,7 +52,8 @@ void waybar::modules::Pulseaudio::contextStateCb(pa_context *c, void *data) { pa_context_set_subscribe_callback(c, subscribeCb, data); pa_context_subscribe( c, - static_cast(static_cast(PA_SUBSCRIPTION_MASK_SINK) | + static_cast(static_cast(PA_SUBSCRIPTION_MASK_SERVER) | + static_cast(PA_SUBSCRIPTION_MASK_SINK) | static_cast(PA_SUBSCRIPTION_MASK_SOURCE)), nullptr, nullptr); @@ -109,7 +110,9 @@ void waybar::modules::Pulseaudio::subscribeCb(pa_context * conte if (operation != PA_SUBSCRIPTION_EVENT_CHANGE) { return; } - if (facility == PA_SUBSCRIPTION_EVENT_SINK) { + if (facility == PA_SUBSCRIPTION_EVENT_SERVER) { + pa_context_get_server_info(context, serverInfoCb, data); + } else if (facility == PA_SUBSCRIPTION_EVENT_SINK) { pa_context_get_sink_info_by_index(context, idx, sinkInfoCb, data); } else if (facility == PA_SUBSCRIPTION_EVENT_SOURCE) { pa_context_get_source_info_by_index(context, idx, sourceInfoCb, data); @@ -214,7 +217,7 @@ auto waybar::modules::Pulseaudio::update() -> void { } else { label_.get_style_context()->remove_class("muted"); } - format = + format = config_[format_name].isString() ? config_[format_name].asString() : format; } // TODO: find a better way to split source/sink