Use the same StatusNotifierWatcher for all trays

pull/598/head
Guillaume Maudoux 2020-02-19 12:06:35 +01:00
parent 9b0660e751
commit 047c2929c1
4 changed files with 20 additions and 7 deletions

View File

@ -21,7 +21,7 @@ class Tray : public AModule {
static inline std::size_t nb_hosts_ = 0; static inline std::size_t nb_hosts_ = 0;
Gtk::Box box_; Gtk::Box box_;
SNI::Watcher watcher_; SNI::Watcher::singleton watcher_;
SNI::Host host_; SNI::Host host_;
}; };

View File

@ -7,10 +7,24 @@
namespace waybar::modules::SNI { namespace waybar::modules::SNI {
class Watcher { class Watcher {
private:
Watcher();
public: public:
Watcher(std::size_t id);
~Watcher(); ~Watcher();
using singleton = std::shared_ptr<Watcher>;
static singleton getInstance() {
static std::weak_ptr<Watcher> weak;
std::shared_ptr<Watcher> strong = weak.lock();
if (!strong) {
strong = std::shared_ptr<Watcher>(new Watcher());
weak = strong;
}
return strong;
}
private: private:
typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType; typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType;
@ -34,7 +48,6 @@ class Watcher {
void updateRegisteredItems(SnWatcher *obj); void updateRegisteredItems(SnWatcher *obj);
uint32_t bus_name_id_; uint32_t bus_name_id_;
uint32_t watcher_id_;
GSList * hosts_ = nullptr; GSList * hosts_ = nullptr;
GSList * items_ = nullptr; GSList * items_ = nullptr;
SnWatcher *watcher_ = nullptr; SnWatcher *watcher_ = nullptr;

View File

@ -6,7 +6,7 @@ namespace waybar::modules::SNI {
Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config) Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config)
: AModule(config, "tray", id), : AModule(config, "tray", id),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), 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), host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1),
std::bind(&Tray::onRemove, this, std::placeholders::_1)) { std::bind(&Tray::onRemove, this, std::placeholders::_1)) {
spdlog::warn( spdlog::warn(

View File

@ -3,14 +3,13 @@
using namespace waybar::modules::SNI; 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, : bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION,
"org.kde.StatusNotifierWatcher", "org.kde.StatusNotifierWatcher",
sigc::mem_fun(*this, &Watcher::busAcquired), sigc::mem_fun(*this, &Watcher::busAcquired),
Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(), Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(),
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)), Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)),
watcher_id_(id),
watcher_(sn_watcher_skeleton_new()) {} watcher_(sn_watcher_skeleton_new()) {}
Watcher::~Watcher() { Watcher::~Watcher() {
@ -23,6 +22,7 @@ Watcher::~Watcher() {
g_slist_free_full(items_, gfWatchFree); g_slist_free_full(items_, gfWatchFree);
items_ = nullptr; items_ = nullptr;
} }
Gio::DBus::unown_name(bus_name_id_);
auto iface = G_DBUS_INTERFACE_SKELETON(watcher_); auto iface = G_DBUS_INTERFACE_SKELETON(watcher_);
g_dbus_interface_skeleton_unexport(iface); g_dbus_interface_skeleton_unexport(iface);
} }
@ -34,7 +34,7 @@ void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib:
if (error != nullptr) { if (error != nullptr) {
// Don't print an error when a watcher is already present // Don't print an error when a watcher is already present
if (error->code != 2) { if (error->code != 2) {
spdlog::error("Watcher {}: {}", watcher_id_, error->message); spdlog::error("Watcher: {}", error->message);
} }
g_error_free(error); g_error_free(error);
return; return;