refactor: format tray && partial fix for #235
This commit is contained in:
parent
93a1bafb46
commit
346ec68578
|
@ -1,42 +1,43 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <glibmm/refptr.h>
|
#include <dbus-status-notifier-watcher.h>
|
||||||
#include <giomm.h>
|
#include <giomm.h>
|
||||||
|
#include <glibmm/refptr.h>
|
||||||
#include <json/json.h>
|
#include <json/json.h>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <dbus-status-notifier-watcher.h>
|
|
||||||
#include "modules/sni/item.hpp"
|
#include "modules/sni/item.hpp"
|
||||||
|
|
||||||
namespace waybar::modules::SNI {
|
namespace waybar::modules::SNI {
|
||||||
|
|
||||||
class Host {
|
class Host {
|
||||||
public:
|
public:
|
||||||
Host(const std::size_t id, const Json::Value&,
|
Host(const std::size_t id, const Json::Value&, const std::function<void(std::unique_ptr<Item>&)>&,
|
||||||
const std::function<void(std::unique_ptr<Item>&)>&,
|
const std::function<void(std::unique_ptr<Item>&)>&);
|
||||||
const std::function<void(std::unique_ptr<Item>&)>&);
|
~Host();
|
||||||
~Host();
|
|
||||||
private:
|
|
||||||
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
|
|
||||||
void nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring, const Glib::ustring&);
|
|
||||||
void nameVanished(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
|
|
||||||
static void proxyReady(GObject*, GAsyncResult*, gpointer);
|
|
||||||
static void registerHost(GObject*, GAsyncResult*, gpointer);
|
|
||||||
static void itemRegistered(SnWatcher*, const gchar*, gpointer);
|
|
||||||
static void itemUnregistered(SnWatcher*, const gchar*, gpointer);
|
|
||||||
|
|
||||||
std::tuple<std::string, std::string> getBusNameAndObjectPath(const std::string);
|
private:
|
||||||
void addRegisteredItem(std::string service);
|
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
|
||||||
|
void nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring,
|
||||||
|
const Glib::ustring&);
|
||||||
|
void nameVanished(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
|
||||||
|
static void proxyReady(GObject*, GAsyncResult*, gpointer);
|
||||||
|
static void registerHost(GObject*, GAsyncResult*, gpointer);
|
||||||
|
static void itemRegistered(SnWatcher*, const gchar*, gpointer);
|
||||||
|
static void itemUnregistered(SnWatcher*, const gchar*, gpointer);
|
||||||
|
|
||||||
std::vector<std::unique_ptr<Item>> items_;
|
std::tuple<std::string, std::string> getBusNameAndObjectPath(const std::string);
|
||||||
const std::string bus_name_;
|
void addRegisteredItem(std::string service);
|
||||||
const std::string object_path_;
|
|
||||||
std::size_t bus_name_id_;
|
std::vector<std::unique_ptr<Item>> items_;
|
||||||
std::size_t watcher_id_;
|
const std::string bus_name_;
|
||||||
GCancellable* cancellable_ = nullptr;
|
const std::string object_path_;
|
||||||
SnWatcher* watcher_ = nullptr;
|
std::size_t bus_name_id_;
|
||||||
const Json::Value &config_;
|
std::size_t watcher_id_;
|
||||||
const std::function<void(std::unique_ptr<Item>&)> on_add_;
|
GCancellable* cancellable_ = nullptr;
|
||||||
const std::function<void(std::unique_ptr<Item>&)> on_remove_;
|
SnWatcher* watcher_ = nullptr;
|
||||||
|
const Json::Value& config_;
|
||||||
|
const std::function<void(std::unique_ptr<Item>&)> on_add_;
|
||||||
|
const std::function<void(std::unique_ptr<Item>&)> on_remove_;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace waybar::modules::SNI
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <dbus-status-notifier-item.h>
|
#include <dbus-status-notifier-item.h>
|
||||||
#include <glibmm/refptr.h>
|
|
||||||
#include <giomm/dbusproxy.h>
|
#include <giomm/dbusproxy.h>
|
||||||
|
#include <glibmm/refptr.h>
|
||||||
#include <gtkmm/eventbox.h>
|
#include <gtkmm/eventbox.h>
|
||||||
#include <gtkmm/image.h>
|
|
||||||
#include <gtkmm/icontheme.h>
|
#include <gtkmm/icontheme.h>
|
||||||
|
#include <gtkmm/image.h>
|
||||||
#include <gtkmm/menu.h>
|
#include <gtkmm/menu.h>
|
||||||
#include <json/json.h>
|
#include <json/json.h>
|
||||||
#include <libdbusmenu-gtk/dbusmenu-gtk.h>
|
#include <libdbusmenu-gtk/dbusmenu-gtk.h>
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
namespace waybar::modules::SNI {
|
namespace waybar::modules::SNI {
|
||||||
|
|
||||||
class Item : public sigc::trackable {
|
class Item : public sigc::trackable {
|
||||||
public:
|
public:
|
||||||
Item(std::string, std::string, const Json::Value&);
|
Item(std::string, std::string, const Json::Value&);
|
||||||
~Item() = default;
|
~Item() = default;
|
||||||
|
|
||||||
|
@ -44,24 +44,24 @@ public:
|
||||||
std::string attention_movie_name;
|
std::string attention_movie_name;
|
||||||
std::string icon_theme_path;
|
std::string icon_theme_path;
|
||||||
std::string menu;
|
std::string menu;
|
||||||
DbusmenuGtkMenu *dbus_menu = nullptr;
|
DbusmenuGtkMenu* dbus_menu = nullptr;
|
||||||
Gtk::Menu *gtk_menu = nullptr;
|
Gtk::Menu* gtk_menu = nullptr;
|
||||||
bool item_is_menu;
|
bool item_is_menu;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void proxyReady(Glib::RefPtr<Gio::AsyncResult>& result);
|
void proxyReady(Glib::RefPtr<Gio::AsyncResult>& result);
|
||||||
void setProperty(const Glib::ustring& name, Glib::VariantBase& value);
|
void setProperty(const Glib::ustring& name, Glib::VariantBase& value);
|
||||||
void getUpdatedProperties();
|
void getUpdatedProperties();
|
||||||
void processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& result);
|
void processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& result);
|
||||||
void onSignal(const Glib::ustring& sender_name, const Glib::ustring& signal_name,
|
void onSignal(const Glib::ustring& sender_name, const Glib::ustring& signal_name,
|
||||||
const Glib::VariantContainerBase& arguments);
|
const Glib::VariantContainerBase& arguments);
|
||||||
|
|
||||||
void updateImage();
|
void updateImage();
|
||||||
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant *variant);
|
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant* variant);
|
||||||
Glib::RefPtr<Gdk::Pixbuf> getIconByName(std::string name, int size);
|
Glib::RefPtr<Gdk::Pixbuf> getIconByName(std::string name, int size);
|
||||||
static void onMenuDestroyed(Item *self);
|
static void onMenuDestroyed(Item* self);
|
||||||
bool makeMenu(GdkEventButton *const &ev);
|
bool makeMenu(GdkEventButton* const& ev);
|
||||||
bool handleClick(GdkEventButton *const & /*ev*/);
|
bool handleClick(GdkEventButton* const& /*ev*/);
|
||||||
|
|
||||||
Glib::RefPtr<Gio::Cancellable> cancellable_;
|
Glib::RefPtr<Gio::Cancellable> cancellable_;
|
||||||
Glib::RefPtr<Gio::DBus::Proxy> proxy_;
|
Glib::RefPtr<Gio::DBus::Proxy> proxy_;
|
||||||
|
@ -69,4 +69,4 @@ private:
|
||||||
bool update_pending_;
|
bool update_pending_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules::SNI
|
} // namespace waybar::modules::SNI
|
||||||
|
|
|
@ -1,29 +1,30 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include "bar.hpp"
|
|
||||||
#include "util/json.hpp"
|
|
||||||
#include "IModule.hpp"
|
#include "IModule.hpp"
|
||||||
#include "modules/sni/watcher.hpp"
|
#include "bar.hpp"
|
||||||
#include "modules/sni/host.hpp"
|
#include "modules/sni/host.hpp"
|
||||||
|
#include "modules/sni/watcher.hpp"
|
||||||
|
#include "util/json.hpp"
|
||||||
|
|
||||||
namespace waybar::modules::SNI {
|
namespace waybar::modules::SNI {
|
||||||
|
|
||||||
class Tray : public IModule {
|
class Tray : public IModule {
|
||||||
public:
|
public:
|
||||||
Tray(const std::string&, const Bar&, const Json::Value&);
|
Tray(const std::string&, const Bar&, const Json::Value&);
|
||||||
~Tray() = default;
|
~Tray() = default;
|
||||||
auto update() -> void;
|
auto update() -> void;
|
||||||
operator Gtk::Widget &();
|
operator Gtk::Widget&();
|
||||||
private:
|
|
||||||
void onAdd(std::unique_ptr<Item>& item);
|
|
||||||
void onRemove(std::unique_ptr<Item>& item);
|
|
||||||
|
|
||||||
static inline std::size_t nb_hosts_ = 0;
|
private:
|
||||||
const Json::Value& config_;
|
void onAdd(std::unique_ptr<Item>& item);
|
||||||
Gtk::Box box_;
|
void onRemove(std::unique_ptr<Item>& item);
|
||||||
SNI::Watcher watcher_ ;
|
|
||||||
SNI::Host host_;
|
static inline std::size_t nb_hosts_ = 0;
|
||||||
|
const Json::Value& config_;
|
||||||
|
Gtk::Box box_;
|
||||||
|
SNI::Watcher watcher_;
|
||||||
|
SNI::Host host_;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace waybar::modules::SNI
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <dbus-status-notifier-watcher.h>
|
||||||
#include <giomm.h>
|
#include <giomm.h>
|
||||||
#include <glibmm/refptr.h>
|
#include <glibmm/refptr.h>
|
||||||
#include <dbus-status-notifier-watcher.h>
|
|
||||||
|
|
||||||
namespace waybar::modules::SNI {
|
namespace waybar::modules::SNI {
|
||||||
|
|
||||||
class Watcher {
|
class Watcher {
|
||||||
public:
|
public:
|
||||||
Watcher();
|
Watcher();
|
||||||
~Watcher() = default;
|
~Watcher();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType;
|
typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -23,17 +23,13 @@ private:
|
||||||
guint watch_id;
|
guint watch_id;
|
||||||
} GfWatch;
|
} GfWatch;
|
||||||
|
|
||||||
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
|
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection> &, Glib::ustring);
|
||||||
static gboolean handleRegisterHost(Watcher *, GDBusMethodInvocation *,
|
static gboolean handleRegisterHost(Watcher *, GDBusMethodInvocation *, const gchar *);
|
||||||
const gchar *);
|
static gboolean handleRegisterItem(Watcher *, GDBusMethodInvocation *, const gchar *);
|
||||||
static gboolean handleRegisterItem(Watcher *, GDBusMethodInvocation *,
|
static GfWatch *gfWatchFind(GSList *list, const gchar *bus_name, const gchar *object_path);
|
||||||
const gchar *);
|
static GfWatch *gfWatchNew(GfWatchType, const gchar *, const gchar *, const gchar *, Watcher *);
|
||||||
static GfWatch *gfWatchFind(GSList *list, const gchar *bus_name,
|
static void nameVanished(GDBusConnection *connection, const char *name, gpointer data);
|
||||||
const gchar *object_path);
|
static void gfWatchFree(gpointer data);
|
||||||
static GfWatch *gfWatchNew(GfWatchType, const gchar *, const gchar *,
|
|
||||||
const gchar *, Watcher *);
|
|
||||||
static void nameVanished(GDBusConnection *connection, const char *name,
|
|
||||||
gpointer data);
|
|
||||||
|
|
||||||
void updateRegisteredItems(SnWatcher *obj);
|
void updateRegisteredItems(SnWatcher *obj);
|
||||||
|
|
||||||
|
@ -42,7 +38,8 @@ private:
|
||||||
GSList *hosts_ = nullptr;
|
GSList *hosts_ = nullptr;
|
||||||
GSList *items_ = nullptr;
|
GSList *items_ = nullptr;
|
||||||
SnWatcher *watcher_ = nullptr;
|
SnWatcher *watcher_ = nullptr;
|
||||||
|
gulong handler_item_id_;
|
||||||
|
gulong handler_host_id_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules::SNI
|
} // namespace waybar::modules::SNI
|
||||||
|
|
||||||
|
|
11
src/main.cpp
11
src/main.cpp
|
@ -1,26 +1,25 @@
|
||||||
#include "client.hpp"
|
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "client.hpp"
|
||||||
|
|
||||||
namespace waybar {
|
namespace waybar {
|
||||||
|
|
||||||
static Client* client;
|
static Client* client;
|
||||||
|
|
||||||
} // namespace waybar
|
} // namespace waybar
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[]) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
waybar::Client c(argc, argv);
|
waybar::Client c(argc, argv);
|
||||||
waybar::client = &c;
|
waybar::client = &c;
|
||||||
std::signal(SIGUSR1, [] (int /*signal*/) {
|
std::signal(SIGUSR1, [](int /*signal*/) {
|
||||||
for (auto& bar : waybar::client->bars) {
|
for (auto& bar : waybar::client->bars) {
|
||||||
bar->toggle();
|
bar->toggle();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) {
|
for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) {
|
||||||
std::signal(sig, [] (int sig/*signal*/) {
|
std::signal(sig, [](int sig /*signal*/) {
|
||||||
for (auto& bar : waybar::client->bars) {
|
for (auto& bar : waybar::client->bars) {
|
||||||
bar->handleSignal(sig);
|
bar->handleSignal(sig);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,55 +4,45 @@
|
||||||
|
|
||||||
using namespace waybar::modules::SNI;
|
using namespace waybar::modules::SNI;
|
||||||
|
|
||||||
Host::Host(const std::size_t id, const Json::Value &config,
|
Host::Host(const std::size_t id, const Json::Value& config,
|
||||||
const std::function<void(std::unique_ptr<Item>&)>& on_add,
|
const std::function<void(std::unique_ptr<Item>&)>& on_add,
|
||||||
const std::function<void(std::unique_ptr<Item>&)>& on_remove)
|
const std::function<void(std::unique_ptr<Item>&)>& on_remove)
|
||||||
: bus_name_("org.kde.StatusNotifierHost-" + std::to_string(getpid()) + "-" + std::to_string(id)),
|
: bus_name_("org.kde.StatusNotifierHost-" + std::to_string(getpid()) + "-" +
|
||||||
object_path_("/StatusNotifierHost/" + std::to_string(id)),
|
std::to_string(id)),
|
||||||
bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name_,
|
object_path_("/StatusNotifierHost/" + std::to_string(id)),
|
||||||
sigc::mem_fun(*this, &Host::busAcquired))),
|
bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name_,
|
||||||
config_(config), on_add_(on_add), on_remove_(on_remove)
|
sigc::mem_fun(*this, &Host::busAcquired))),
|
||||||
{
|
config_(config),
|
||||||
}
|
on_add_(on_add),
|
||||||
|
on_remove_(on_remove) {}
|
||||||
|
|
||||||
Host::~Host()
|
Host::~Host() { Gio::DBus::unwatch_name(bus_name_id_); }
|
||||||
{
|
|
||||||
Gio::DBus::unwatch_name(bus_name_id_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Host::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name)
|
void Host::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
|
||||||
{
|
|
||||||
watcher_id_ = Gio::DBus::watch_name(conn, "org.kde.StatusNotifierWatcher",
|
watcher_id_ = Gio::DBus::watch_name(conn, "org.kde.StatusNotifierWatcher",
|
||||||
sigc::mem_fun(*this, &Host::nameAppeared), sigc::mem_fun(*this, &Host::nameVanished));
|
sigc::mem_fun(*this, &Host::nameAppeared),
|
||||||
|
sigc::mem_fun(*this, &Host::nameVanished));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name,
|
void Host::nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name,
|
||||||
const Glib::ustring& name_owner)
|
const Glib::ustring& name_owner) {
|
||||||
{
|
|
||||||
if (cancellable_ != nullptr) {
|
if (cancellable_ != nullptr) {
|
||||||
// TODO
|
// TODO
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cancellable_ = g_cancellable_new();
|
cancellable_ = g_cancellable_new();
|
||||||
sn_watcher_proxy_new(
|
sn_watcher_proxy_new(conn->gobj(), G_DBUS_PROXY_FLAGS_NONE, "org.kde.StatusNotifierWatcher",
|
||||||
conn->gobj(),
|
"/StatusNotifierWatcher", cancellable_, &Host::proxyReady, this);
|
||||||
G_DBUS_PROXY_FLAGS_NONE,
|
|
||||||
"org.kde.StatusNotifierWatcher",
|
|
||||||
"/StatusNotifierWatcher",
|
|
||||||
cancellable_, &Host::proxyReady, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::nameVanished(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name)
|
void Host::nameVanished(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name) {
|
||||||
{
|
|
||||||
g_cancellable_cancel(cancellable_);
|
g_cancellable_cancel(cancellable_);
|
||||||
g_clear_object(&cancellable_);
|
g_clear_object(&cancellable_);
|
||||||
g_clear_object(&watcher_);
|
g_clear_object(&watcher_);
|
||||||
items_.clear();
|
items_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::proxyReady(GObject* src, GAsyncResult* res,
|
void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
|
||||||
gpointer data)
|
|
||||||
{
|
|
||||||
GError* error = nullptr;
|
GError* error = nullptr;
|
||||||
SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
|
SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
|
||||||
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
||||||
|
@ -60,21 +50,18 @@ void Host::proxyReady(GObject* src, GAsyncResult* res,
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto host = static_cast<SNI::Host *>(data);
|
auto host = static_cast<SNI::Host*>(data);
|
||||||
host->watcher_ = watcher;
|
host->watcher_ = watcher;
|
||||||
if (error != nullptr) {
|
if (error != nullptr) {
|
||||||
std::cerr << error->message << std::endl;
|
std::cerr << error->message << std::endl;
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sn_watcher_call_register_host(
|
sn_watcher_call_register_host(host->watcher_, host->object_path_.c_str(), host->cancellable_,
|
||||||
host->watcher_, host->object_path_.c_str(), host->cancellable_,
|
&Host::registerHost, data);
|
||||||
&Host::registerHost, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::registerHost(GObject* src, GAsyncResult* res,
|
void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
|
||||||
gpointer data)
|
|
||||||
{
|
|
||||||
GError* error = nullptr;
|
GError* error = nullptr;
|
||||||
sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
|
sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
|
||||||
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
||||||
|
@ -82,16 +69,14 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto host = static_cast<SNI::Host *>(data);
|
auto host = static_cast<SNI::Host*>(data);
|
||||||
if (error != nullptr) {
|
if (error != nullptr) {
|
||||||
std::cerr << error->message << std::endl;
|
std::cerr << error->message << std::endl;
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
g_signal_connect(host->watcher_, "item-registered",
|
g_signal_connect(host->watcher_, "item-registered", G_CALLBACK(&Host::itemRegistered), data);
|
||||||
G_CALLBACK(&Host::itemRegistered), data);
|
g_signal_connect(host->watcher_, "item-unregistered", G_CALLBACK(&Host::itemUnregistered), data);
|
||||||
g_signal_connect(host->watcher_, "item-unregistered",
|
|
||||||
G_CALLBACK(&Host::itemUnregistered), data);
|
|
||||||
auto items = sn_watcher_dup_registered_items(host->watcher_);
|
auto items = sn_watcher_dup_registered_items(host->watcher_);
|
||||||
if (items) {
|
if (items) {
|
||||||
for (uint32_t i = 0; items[i] != nullptr; i += 1) {
|
for (uint32_t i = 0; items[i] != nullptr; i += 1) {
|
||||||
|
@ -101,16 +86,13 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
|
||||||
g_strfreev(items);
|
g_strfreev(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::itemRegistered(SnWatcher* watcher, const gchar* service, gpointer data)
|
void Host::itemRegistered(SnWatcher* watcher, const gchar* service, gpointer data) {
|
||||||
{
|
auto host = static_cast<SNI::Host*>(data);
|
||||||
auto host = static_cast<SNI::Host *>(data);
|
|
||||||
host->addRegisteredItem(service);
|
host->addRegisteredItem(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::itemUnregistered(
|
void Host::itemUnregistered(SnWatcher* watcher, const gchar* service, gpointer data) {
|
||||||
SnWatcher* watcher, const gchar* service, gpointer data)
|
auto host = static_cast<SNI::Host*>(data);
|
||||||
{
|
|
||||||
auto host = static_cast<SNI::Host *>(data);
|
|
||||||
auto [bus_name, object_path] = host->getBusNameAndObjectPath(service);
|
auto [bus_name, object_path] = host->getBusNameAndObjectPath(service);
|
||||||
for (auto it = host->items_.begin(); it != host->items_.end(); ++it) {
|
for (auto it = host->items_.begin(); it != host->items_.end(); ++it) {
|
||||||
if ((*it)->bus_name == bus_name && (*it)->object_path == object_path) {
|
if ((*it)->bus_name == bus_name && (*it)->object_path == object_path) {
|
||||||
|
@ -121,9 +103,7 @@ void Host::itemUnregistered(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(
|
std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(const std::string service) {
|
||||||
const std::string service)
|
|
||||||
{
|
|
||||||
auto it = service.find("/");
|
auto it = service.find("/");
|
||||||
if (it != std::string::npos) {
|
if (it != std::string::npos) {
|
||||||
return {service.substr(0, it), service.substr(it)};
|
return {service.substr(0, it), service.substr(it)};
|
||||||
|
@ -131,8 +111,7 @@ std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(
|
||||||
return {service, "/StatusNotifierItem"};
|
return {service, "/StatusNotifierItem"};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::addRegisteredItem(std::string service)
|
void Host::addRegisteredItem(std::string service) {
|
||||||
{
|
|
||||||
auto [bus_name, object_path] = getBusNameAndObjectPath(service);
|
auto [bus_name, object_path] = getBusNameAndObjectPath(service);
|
||||||
items_.emplace_back(new Item(bus_name, object_path, config_));
|
items_.emplace_back(new Item(bus_name, object_path, config_));
|
||||||
on_add_(items_.back());
|
on_add_(items_.back());
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "modules/sni/item.hpp"
|
#include "modules/sni/item.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <glibmm/main.h>
|
#include <glibmm/main.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace Glib;
|
using namespace Glib;
|
||||||
|
|
||||||
|
@ -9,23 +9,26 @@ static const ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name;
|
||||||
static const unsigned UPDATE_DEBOUNCE_TIME = 10;
|
static const unsigned UPDATE_DEBOUNCE_TIME = 10;
|
||||||
|
|
||||||
waybar::modules::SNI::Item::Item(std::string bn, std::string op, const Json::Value& config)
|
waybar::modules::SNI::Item::Item(std::string bn, std::string op, const Json::Value& config)
|
||||||
: bus_name(bn), object_path(op), icon_size(16), effective_icon_size(0),
|
: bus_name(bn),
|
||||||
icon_theme(Gtk::IconTheme::create()), update_pending_(false) {
|
object_path(op),
|
||||||
|
icon_size(16),
|
||||||
|
effective_icon_size(0),
|
||||||
|
icon_theme(Gtk::IconTheme::create()),
|
||||||
|
update_pending_(false) {
|
||||||
if (config["icon-size"].isUInt()) {
|
if (config["icon-size"].isUInt()) {
|
||||||
icon_size = config["icon-size"].asUInt();
|
icon_size = config["icon-size"].asUInt();
|
||||||
}
|
}
|
||||||
default_icon_path_ = Gtk::IconTheme::get_default()->get_search_path();
|
default_icon_path_ = Gtk::IconTheme::get_default()->get_search_path();
|
||||||
event_box.add(image);
|
event_box.add(image);
|
||||||
event_box.add_events(Gdk::BUTTON_PRESS_MASK);
|
event_box.add_events(Gdk::BUTTON_PRESS_MASK);
|
||||||
event_box.signal_button_press_event().connect(
|
event_box.signal_button_press_event().connect(sigc::mem_fun(*this, &Item::handleClick));
|
||||||
sigc::mem_fun(*this, &Item::handleClick));
|
|
||||||
|
|
||||||
cancellable_ = Gio::Cancellable::create();
|
cancellable_ = Gio::Cancellable::create();
|
||||||
|
|
||||||
auto interface = Glib::wrap(sn_item_interface_info(), true);
|
auto interface = Glib::wrap(sn_item_interface_info(), true);
|
||||||
Gio::DBus::Proxy::create_for_bus(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name,
|
Gio::DBus::Proxy::create_for_bus(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name, object_path,
|
||||||
object_path, SNI_INTERFACE_NAME, sigc::mem_fun(*this, &Item::proxyReady),
|
SNI_INTERFACE_NAME, sigc::mem_fun(*this, &Item::proxyReady),
|
||||||
cancellable_, interface);
|
cancellable_, interface);
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
|
void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
|
||||||
|
@ -33,7 +36,7 @@ void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& resu
|
||||||
this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result);
|
this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result);
|
||||||
/* Properties are already cached during object creation */
|
/* Properties are already cached during object creation */
|
||||||
auto cached_properties = this->proxy_->get_cached_property_names();
|
auto cached_properties = this->proxy_->get_cached_property_names();
|
||||||
for (const auto& name: cached_properties) {
|
for (const auto& name : cached_properties) {
|
||||||
Glib::VariantBase value;
|
Glib::VariantBase value;
|
||||||
this->proxy_->get_cached_property(value, name);
|
this->proxy_->get_cached_property(value, name);
|
||||||
setProperty(name, value);
|
setProperty(name, value);
|
||||||
|
@ -42,30 +45,28 @@ void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& resu
|
||||||
this->proxy_->signal_signal().connect(sigc::mem_fun(*this, &Item::onSignal));
|
this->proxy_->signal_signal().connect(sigc::mem_fun(*this, &Item::onSignal));
|
||||||
|
|
||||||
if (this->id.empty() || this->category.empty() || this->status.empty()) {
|
if (this->id.empty() || this->category.empty() || this->status.empty()) {
|
||||||
std::cerr << "Invalid Status Notifier Item: " + this->bus_name + "," +
|
std::cerr << "Invalid Status Notifier Item: " + this->bus_name + "," + this->object_path
|
||||||
this->object_path << std::endl;
|
<< std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->updateImage();
|
this->updateImage();
|
||||||
// this->event_box.set_tooltip_text(this->title);
|
// this->event_box.set_tooltip_text(this->title);
|
||||||
|
|
||||||
} catch (const Glib::Error& err) {
|
} catch (const Glib::Error& err) {
|
||||||
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(),
|
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(), object_path.c_str(),
|
||||||
object_path.c_str(), err.what().c_str());
|
err.what().c_str());
|
||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(),
|
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(), object_path.c_str(),
|
||||||
object_path.c_str(), err.what());
|
err.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T>
|
||||||
T get_variant(VariantBase& value) {
|
T get_variant(VariantBase& value) {
|
||||||
return VariantBase::cast_dynamic<Variant<T>>(value).get();
|
return VariantBase::cast_dynamic<Variant<T>>(value).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void waybar::modules::SNI::Item::setProperty(const ustring& name, VariantBase& value) {
|
||||||
waybar::modules::SNI::Item::setProperty(const ustring& name,
|
|
||||||
VariantBase& value) {
|
|
||||||
if (name == "Category") {
|
if (name == "Category") {
|
||||||
category = get_variant<std::string>(value);
|
category = get_variant<std::string>(value);
|
||||||
} else if (name == "Id") {
|
} else if (name == "Id") {
|
||||||
|
@ -106,20 +107,15 @@ waybar::modules::SNI::Item::setProperty(const ustring& name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void waybar::modules::SNI::Item::getUpdatedProperties() {
|
||||||
waybar::modules::SNI::Item::getUpdatedProperties() {
|
|
||||||
update_pending_ = false;
|
update_pending_ = false;
|
||||||
|
|
||||||
auto params = VariantContainerBase::create_tuple({
|
auto params = VariantContainerBase::create_tuple({Variant<ustring>::create(SNI_INTERFACE_NAME)});
|
||||||
Variant<ustring>::create(SNI_INTERFACE_NAME)
|
|
||||||
});
|
|
||||||
proxy_->call("org.freedesktop.DBus.Properties.GetAll",
|
proxy_->call("org.freedesktop.DBus.Properties.GetAll",
|
||||||
sigc::mem_fun(*this, &Item::processUpdatedProperties), params);
|
sigc::mem_fun(*this, &Item::processUpdatedProperties), params);
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void waybar::modules::SNI::Item::processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& _result) {
|
||||||
waybar::modules::SNI::Item::processUpdatedProperties(
|
|
||||||
Glib::RefPtr<Gio::AsyncResult>& _result) {
|
|
||||||
try {
|
try {
|
||||||
auto result = proxy_->call_finish(_result);
|
auto result = proxy_->call_finish(_result);
|
||||||
// extract "a{sv}" from VariantContainerBase
|
// extract "a{sv}" from VariantContainerBase
|
||||||
|
@ -127,7 +123,7 @@ waybar::modules::SNI::Item::processUpdatedProperties(
|
||||||
result.get_child(properties_variant);
|
result.get_child(properties_variant);
|
||||||
auto properties = properties_variant.get();
|
auto properties = properties_variant.get();
|
||||||
|
|
||||||
for (const auto& [name, value]: properties) {
|
for (const auto& [name, value] : properties) {
|
||||||
VariantBase old_value;
|
VariantBase old_value;
|
||||||
proxy_->get_cached_property(old_value, name);
|
proxy_->get_cached_property(old_value, name);
|
||||||
if (!value.equal(old_value)) {
|
if (!value.equal(old_value)) {
|
||||||
|
@ -145,41 +141,34 @@ waybar::modules::SNI::Item::processUpdatedProperties(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void waybar::modules::SNI::Item::onSignal(const ustring& sender_name, const ustring& signal_name,
|
||||||
waybar::modules::SNI::Item::onSignal(const ustring& sender_name,
|
const VariantContainerBase& arguments) {
|
||||||
const ustring& signal_name, const VariantContainerBase& arguments) {
|
|
||||||
if (!update_pending_ && signal_name.compare(0, 3, "New") == 0) {
|
if (!update_pending_ && signal_name.compare(0, 3, "New") == 0) {
|
||||||
/* Debounce signals and schedule update of all properties.
|
/* Debounce signals and schedule update of all properties.
|
||||||
* Based on behavior of Plasma dataengine for StatusNotifierItem.
|
* Based on behavior of Plasma dataengine for StatusNotifierItem.
|
||||||
*/
|
*/
|
||||||
update_pending_ = true;
|
update_pending_ = true;
|
||||||
Glib::signal_timeout().connect_once(
|
Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &Item::getUpdatedProperties),
|
||||||
sigc::mem_fun(*this, &Item::getUpdatedProperties), UPDATE_DEBOUNCE_TIME);
|
UPDATE_DEBOUNCE_TIME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pixbuf_data_deleter(const guint8* data) { g_free((void*)data); }
|
||||||
|
|
||||||
static void
|
Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::extractPixBuf(GVariant* variant) {
|
||||||
pixbuf_data_deleter(const guint8* data) {
|
GVariantIter* it;
|
||||||
g_free((void*) data);
|
|
||||||
}
|
|
||||||
|
|
||||||
Glib::RefPtr<Gdk::Pixbuf>
|
|
||||||
waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
|
|
||||||
GVariantIter *it;
|
|
||||||
g_variant_get(variant, "a(iiay)", &it);
|
g_variant_get(variant, "a(iiay)", &it);
|
||||||
if (it == nullptr) {
|
if (it == nullptr) {
|
||||||
return Glib::RefPtr<Gdk::Pixbuf>{};
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
||||||
}
|
}
|
||||||
GVariant *val;
|
GVariant* val;
|
||||||
gint lwidth = 0;
|
gint lwidth = 0;
|
||||||
gint lheight = 0;
|
gint lheight = 0;
|
||||||
gint width;
|
gint width;
|
||||||
gint height;
|
gint height;
|
||||||
guchar *array = nullptr;
|
guchar* array = nullptr;
|
||||||
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
|
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
|
||||||
if (width > 0 && height > 0 && val != nullptr &&
|
if (width > 0 && height > 0 && val != nullptr && width * height > lwidth * lheight) {
|
||||||
width * height > lwidth * lheight) {
|
|
||||||
auto size = g_variant_get_size(val);
|
auto size = g_variant_get_size(val);
|
||||||
/* Sanity check */
|
/* Sanity check */
|
||||||
if (size == 4U * width * height) {
|
if (size == 4U * width * height) {
|
||||||
|
@ -189,7 +178,7 @@ waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
|
||||||
if (array != nullptr) {
|
if (array != nullptr) {
|
||||||
g_free(array);
|
g_free(array);
|
||||||
}
|
}
|
||||||
array = static_cast<guchar *>(g_memdup(data, size));
|
array = static_cast<guchar*>(g_memdup(data, size));
|
||||||
lwidth = width;
|
lwidth = width;
|
||||||
lheight = height;
|
lheight = height;
|
||||||
}
|
}
|
||||||
|
@ -206,15 +195,13 @@ waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
|
||||||
array[i + 2] = array[i + 3];
|
array[i + 2] = array[i + 3];
|
||||||
array[i + 3] = alpha;
|
array[i + 3] = alpha;
|
||||||
}
|
}
|
||||||
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB,
|
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB, true, 8, lwidth,
|
||||||
true, 8, lwidth, lheight, 4 * lwidth,
|
lheight, 4 * lwidth, &pixbuf_data_deleter);
|
||||||
&pixbuf_data_deleter);
|
|
||||||
}
|
}
|
||||||
return Glib::RefPtr<Gdk::Pixbuf>{};
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::SNI::Item::updateImage()
|
void waybar::modules::SNI::Item::updateImage() {
|
||||||
{
|
|
||||||
image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
|
image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
|
||||||
image.set_pixel_size(icon_size);
|
image.set_pixel_size(icon_size);
|
||||||
if (!icon_name.empty()) {
|
if (!icon_name.empty()) {
|
||||||
|
@ -229,31 +216,29 @@ void waybar::modules::SNI::Item::updateImage()
|
||||||
if (pixbuf->gobj() != nullptr) {
|
if (pixbuf->gobj() != nullptr) {
|
||||||
// An icon specified by path and filename may be the wrong size for
|
// An icon specified by path and filename may be the wrong size for
|
||||||
// the tray
|
// the tray
|
||||||
pixbuf = pixbuf->scale_simple(icon_size, icon_size,
|
pixbuf = pixbuf->scale_simple(icon_size, icon_size, Gdk::InterpType::INTERP_BILINEAR);
|
||||||
Gdk::InterpType::INTERP_BILINEAR);
|
|
||||||
image.set(pixbuf);
|
image.set(pixbuf);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
image.set(getIconByName(icon_name, icon_size));
|
image.set(getIconByName(icon_name, icon_size));
|
||||||
}
|
}
|
||||||
} catch (Glib::Error &e) {
|
} catch (Glib::Error& e) {
|
||||||
std::cerr << "Exception: " << e.what() << std::endl;
|
std::cerr << "Exception: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
} else if (icon_pixmap) {
|
} else if (icon_pixmap) {
|
||||||
// An icon extracted may be the wrong size for the tray
|
// An icon extracted may be the wrong size for the tray
|
||||||
icon_pixmap = icon_pixmap->scale_simple(icon_size, icon_size,
|
icon_pixmap = icon_pixmap->scale_simple(icon_size, icon_size, Gdk::InterpType::INTERP_BILINEAR);
|
||||||
Gdk::InterpType::INTERP_BILINEAR);
|
|
||||||
image.set(icon_pixmap);
|
image.set(icon_pixmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::RefPtr<Gdk::Pixbuf>
|
Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::getIconByName(std::string name,
|
||||||
waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
|
int request_size) {
|
||||||
int tmp_size = 0;
|
int tmp_size = 0;
|
||||||
icon_theme->rescan_if_needed();
|
icon_theme->rescan_if_needed();
|
||||||
auto sizes = icon_theme->get_icon_sizes(name.c_str());
|
auto sizes = icon_theme->get_icon_sizes(name.c_str());
|
||||||
|
|
||||||
for (auto const &size : sizes) {
|
for (auto const& size : sizes) {
|
||||||
// -1 == scalable
|
// -1 == scalable
|
||||||
if (size == request_size || size == -1) {
|
if (size == request_size || size == -1) {
|
||||||
tmp_size = request_size;
|
tmp_size = request_size;
|
||||||
|
@ -269,17 +254,15 @@ waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
|
||||||
tmp_size = request_size;
|
tmp_size = request_size;
|
||||||
}
|
}
|
||||||
return icon_theme->load_icon(name.c_str(), tmp_size,
|
return icon_theme->load_icon(name.c_str(), tmp_size,
|
||||||
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
|
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::SNI::Item::onMenuDestroyed(Item *self)
|
void waybar::modules::SNI::Item::onMenuDestroyed(Item* self) {
|
||||||
{
|
|
||||||
self->gtk_menu = nullptr;
|
self->gtk_menu = nullptr;
|
||||||
self->dbus_menu = nullptr;
|
self->dbus_menu = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool waybar::modules::SNI::Item::makeMenu(GdkEventButton *const &ev)
|
bool waybar::modules::SNI::Item::makeMenu(GdkEventButton* const& ev) {
|
||||||
{
|
|
||||||
if (gtk_menu == nullptr) {
|
if (gtk_menu == nullptr) {
|
||||||
if (!menu.empty()) {
|
if (!menu.empty()) {
|
||||||
dbus_menu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data());
|
dbus_menu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data());
|
||||||
|
@ -302,11 +285,9 @@ bool waybar::modules::SNI::Item::makeMenu(GdkEventButton *const &ev)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool waybar::modules::SNI::Item::handleClick(GdkEventButton *const &ev) {
|
bool waybar::modules::SNI::Item::handleClick(GdkEventButton* const& ev) {
|
||||||
auto parameters = VariantContainerBase::create_tuple({
|
auto parameters = VariantContainerBase::create_tuple(
|
||||||
Variant<int>::create(ev->x),
|
{Variant<int>::create(ev->x), Variant<int>::create(ev->y)});
|
||||||
Variant<int>::create(ev->y)
|
|
||||||
});
|
|
||||||
if ((ev->button == 1 && item_is_menu) || ev->button == 3) {
|
if ((ev->button == 1 && item_is_menu) || ev->button == 3) {
|
||||||
if (!makeMenu(ev)) {
|
if (!makeMenu(ev)) {
|
||||||
proxy_->call("ContextMenu", parameters);
|
proxy_->call("ContextMenu", parameters);
|
||||||
|
|
|
@ -2,14 +2,12 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar,
|
waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||||
const Json::Value &config)
|
|
||||||
: config_(config),
|
: config_(config),
|
||||||
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
|
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
|
||||||
watcher_(), host_(nb_hosts_, config,
|
watcher_(),
|
||||||
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)) {
|
||||||
{
|
|
||||||
std::cout << "Tray is in beta, so there may be bugs or even be unusable." << std::endl;
|
std::cout << "Tray is in beta, so there may be bugs or even be unusable." << std::endl;
|
||||||
if (config_["spacing"].isUInt()) {
|
if (config_["spacing"].isUInt()) {
|
||||||
box_.set_spacing(config_["spacing"].asUInt());
|
box_.set_spacing(config_["spacing"].asUInt());
|
||||||
|
@ -17,14 +15,12 @@ waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar,
|
||||||
nb_hosts_ += 1;
|
nb_hosts_ += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::SNI::Tray::onAdd(std::unique_ptr<Item>& item)
|
void waybar::modules::SNI::Tray::onAdd(std::unique_ptr<Item>& item) {
|
||||||
{
|
|
||||||
box_.pack_start(item->event_box);
|
box_.pack_start(item->event_box);
|
||||||
dp.emit();
|
dp.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::SNI::Tray::onRemove(std::unique_ptr<Item>& item)
|
void waybar::modules::SNI::Tray::onRemove(std::unique_ptr<Item>& item) {
|
||||||
{
|
|
||||||
box_.remove(item->event_box);
|
box_.remove(item->event_box);
|
||||||
dp.emit();
|
dp.emit();
|
||||||
}
|
}
|
||||||
|
@ -38,6 +34,4 @@ auto waybar::modules::SNI::Tray::update() -> void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
waybar::modules::SNI::Tray::operator Gtk::Widget &() {
|
waybar::modules::SNI::Tray::operator Gtk::Widget&() { return box_; }
|
||||||
return box_;
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,33 +5,50 @@
|
||||||
using namespace waybar::modules::SNI;
|
using namespace waybar::modules::SNI;
|
||||||
|
|
||||||
Watcher::Watcher()
|
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", sigc::mem_fun(*this, &Watcher::busAcquired),
|
"org.kde.StatusNotifierWatcher",
|
||||||
Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(),
|
sigc::mem_fun(*this, &Watcher::busAcquired),
|
||||||
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)),
|
Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(),
|
||||||
watcher_(sn_watcher_skeleton_new())
|
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
||||||
{
|
Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)),
|
||||||
|
watcher_(sn_watcher_skeleton_new()) {}
|
||||||
|
|
||||||
|
Watcher::~Watcher() {
|
||||||
|
if (bus_name_id_ > 0) {
|
||||||
|
g_bus_unown_name(bus_name_id_);
|
||||||
|
bus_name_id_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hosts_ != NULL) {
|
||||||
|
g_slist_free_full(hosts_, gfWatchFree);
|
||||||
|
hosts_ = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (items_ != NULL) {
|
||||||
|
g_slist_free_full(items_, gfWatchFree);
|
||||||
|
items_ = NULL;
|
||||||
|
}
|
||||||
|
g_signal_handler_disconnect(watcher_, handler_host_id_);
|
||||||
|
g_signal_handler_disconnect(watcher_, handler_item_id_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name)
|
void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
|
||||||
{
|
|
||||||
GError* error = nullptr;
|
GError* error = nullptr;
|
||||||
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_),
|
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_), conn->gobj(),
|
||||||
conn->gobj(), "/StatusNotifierWatcher", &error);
|
"/StatusNotifierWatcher", &error);
|
||||||
if (error != nullptr) {
|
if (error != nullptr) {
|
||||||
std::cerr << error->message << std::endl;
|
std::cerr << error->message << std::endl;
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
g_signal_connect_swapped(watcher_, "handle-register-item",
|
handler_item_id_ = g_signal_connect_swapped(watcher_, "handle-register-item",
|
||||||
G_CALLBACK(&Watcher::handleRegisterItem), this);
|
G_CALLBACK(&Watcher::handleRegisterItem), this);
|
||||||
g_signal_connect_swapped(watcher_, "handle-register-host",
|
handler_host_id_ = g_signal_connect_swapped(watcher_, "handle-register-host",
|
||||||
G_CALLBACK(&Watcher::handleRegisterHost), this);
|
G_CALLBACK(&Watcher::handleRegisterHost), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean Watcher::handleRegisterHost(Watcher* obj,
|
gboolean Watcher::handleRegisterHost(Watcher* obj, GDBusMethodInvocation* invocation,
|
||||||
GDBusMethodInvocation* invocation, const gchar* service)
|
const gchar* service) {
|
||||||
{
|
|
||||||
const gchar* bus_name = service;
|
const gchar* bus_name = service;
|
||||||
const gchar* object_path = "/StatusNotifierHost";
|
const gchar* object_path = "/StatusNotifierHost";
|
||||||
|
|
||||||
|
@ -40,15 +57,16 @@ gboolean Watcher::handleRegisterHost(Watcher* obj,
|
||||||
object_path = service;
|
object_path = service;
|
||||||
}
|
}
|
||||||
if (g_dbus_is_name(bus_name) == FALSE) {
|
if (g_dbus_is_name(bus_name) == FALSE) {
|
||||||
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
|
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
|
||||||
G_DBUS_ERROR_INVALID_ARGS, "D-Bus bus name '%s' is not valid", bus_name);
|
"D-Bus bus name '%s' is not valid", bus_name);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
auto watch = gfWatchFind(obj->hosts_, bus_name, object_path);
|
auto watch = gfWatchFind(obj->hosts_, bus_name, object_path);
|
||||||
if (watch != nullptr) {
|
if (watch != nullptr) {
|
||||||
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
|
g_dbus_method_invocation_return_error(
|
||||||
G_DBUS_ERROR_INVALID_ARGS, "Status Notifier Host with bus name '%s' and object path '%s' is already registered",
|
invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
|
||||||
bus_name, object_path);
|
"Status Notifier Host with bus name '%s' and object path '%s' is already registered",
|
||||||
|
bus_name, object_path);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
watch = gfWatchNew(GF_WATCH_TYPE_HOST, service, bus_name, object_path, obj);
|
watch = gfWatchNew(GF_WATCH_TYPE_HOST, service, bus_name, object_path, obj);
|
||||||
|
@ -61,9 +79,8 @@ gboolean Watcher::handleRegisterHost(Watcher* obj,
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean Watcher::handleRegisterItem(Watcher* obj,
|
gboolean Watcher::handleRegisterItem(Watcher* obj, GDBusMethodInvocation* invocation,
|
||||||
GDBusMethodInvocation* invocation, const gchar* service)
|
const gchar* service) {
|
||||||
{
|
|
||||||
const gchar* bus_name = service;
|
const gchar* bus_name = service;
|
||||||
const gchar* object_path = "/StatusNotifierItem";
|
const gchar* object_path = "/StatusNotifierItem";
|
||||||
|
|
||||||
|
@ -72,14 +89,14 @@ gboolean Watcher::handleRegisterItem(Watcher* obj,
|
||||||
object_path = service;
|
object_path = service;
|
||||||
}
|
}
|
||||||
if (g_dbus_is_name(bus_name) == FALSE) {
|
if (g_dbus_is_name(bus_name) == FALSE) {
|
||||||
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
|
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
|
||||||
G_DBUS_ERROR_INVALID_ARGS, "D-Bus bus name '%s' is not valid", bus_name);
|
"D-Bus bus name '%s' is not valid", bus_name);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
auto watch = gfWatchFind(obj->items_, bus_name, object_path);
|
auto watch = gfWatchFind(obj->items_, bus_name, object_path);
|
||||||
if (watch != nullptr) {
|
if (watch != nullptr) {
|
||||||
g_warning("Status Notifier Item with bus name '%s' and object path '%s' is already registered",
|
g_warning("Status Notifier Item with bus name '%s' and object path '%s' is already registered",
|
||||||
bus_name, object_path);
|
bus_name, object_path);
|
||||||
sn_watcher_complete_register_item(obj->watcher_, invocation);
|
sn_watcher_complete_register_item(obj->watcher_, invocation);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -94,40 +111,49 @@ gboolean Watcher::handleRegisterItem(Watcher* obj,
|
||||||
}
|
}
|
||||||
|
|
||||||
Watcher::GfWatch* Watcher::gfWatchFind(GSList* list, const gchar* bus_name,
|
Watcher::GfWatch* Watcher::gfWatchFind(GSList* list, const gchar* bus_name,
|
||||||
const gchar* object_path)
|
const gchar* object_path) {
|
||||||
{
|
for (GSList* l = list; l != nullptr; l = g_slist_next(l)) {
|
||||||
for (GSList* l = list; l != nullptr; l = g_slist_next (l)) {
|
|
||||||
GfWatch* watch = static_cast<GfWatch*>(l->data);
|
GfWatch* watch = static_cast<GfWatch*>(l->data);
|
||||||
if (g_strcmp0 (watch->bus_name, bus_name) == 0
|
if (g_strcmp0(watch->bus_name, bus_name) == 0 &&
|
||||||
&& g_strcmp0 (watch->object_path, object_path) == 0) {
|
g_strcmp0(watch->object_path, object_path) == 0) {
|
||||||
return watch;
|
return watch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Watcher::GfWatch* Watcher::gfWatchNew(GfWatchType type, const gchar* service,
|
void Watcher::gfWatchFree(gpointer data) {
|
||||||
const gchar* bus_name, const gchar* object_path, Watcher* watcher)
|
GfWatch* watch;
|
||||||
{
|
|
||||||
|
watch = (GfWatch*)data;
|
||||||
|
|
||||||
|
if (watch->watch_id > 0) g_bus_unwatch_name(watch->watch_id);
|
||||||
|
|
||||||
|
g_free(watch->service);
|
||||||
|
g_free(watch->bus_name);
|
||||||
|
g_free(watch->object_path);
|
||||||
|
|
||||||
|
g_free(watch);
|
||||||
|
}
|
||||||
|
|
||||||
|
Watcher::GfWatch* Watcher::gfWatchNew(GfWatchType type, const gchar* service, const gchar* bus_name,
|
||||||
|
const gchar* object_path, Watcher* watcher) {
|
||||||
GfWatch* watch = g_new0(GfWatch, 1);
|
GfWatch* watch = g_new0(GfWatch, 1);
|
||||||
watch->type = type;
|
watch->type = type;
|
||||||
watch->watcher = watcher;
|
watch->watcher = watcher;
|
||||||
watch->service = g_strdup(service);
|
watch->service = g_strdup(service);
|
||||||
watch->bus_name = g_strdup(bus_name);
|
watch->bus_name = g_strdup(bus_name);
|
||||||
watch->object_path = g_strdup(object_path);
|
watch->object_path = g_strdup(object_path);
|
||||||
watch->watch_id = g_bus_watch_name(G_BUS_TYPE_SESSION, bus_name,
|
watch->watch_id = g_bus_watch_name(G_BUS_TYPE_SESSION, bus_name, G_BUS_NAME_WATCHER_FLAGS_NONE,
|
||||||
G_BUS_NAME_WATCHER_FLAGS_NONE, nullptr, &Watcher::nameVanished, watch,
|
nullptr, &Watcher::nameVanished, watch, nullptr);
|
||||||
nullptr);
|
|
||||||
return watch;
|
return watch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Watcher::nameVanished(GDBusConnection* connection, const char* name,
|
void Watcher::nameVanished(GDBusConnection* connection, const char* name, gpointer data) {
|
||||||
gpointer data)
|
auto watch = static_cast<GfWatch*>(data);
|
||||||
{
|
|
||||||
auto watch = static_cast<GfWatch *>(data);
|
|
||||||
if (watch->type == GF_WATCH_TYPE_HOST) {
|
if (watch->type == GF_WATCH_TYPE_HOST) {
|
||||||
watch->watcher->hosts_ = g_slist_remove(watch->watcher->hosts_, watch);
|
watch->watcher->hosts_ = g_slist_remove(watch->watcher->hosts_, watch);
|
||||||
if (watch->watcher->hosts_ == nullptr) {
|
if (watch->watcher->hosts_ == nullptr) {
|
||||||
sn_watcher_set_is_host_registered(watch->watcher->watcher_, FALSE);
|
sn_watcher_set_is_host_registered(watch->watcher->watcher_, FALSE);
|
||||||
sn_watcher_emit_host_registered(watch->watcher->watcher_);
|
sn_watcher_emit_host_registered(watch->watcher->watcher_);
|
||||||
}
|
}
|
||||||
|
@ -140,10 +166,9 @@ void Watcher::nameVanished(GDBusConnection* connection, const char* name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Watcher::updateRegisteredItems(SnWatcher* obj)
|
void Watcher::updateRegisteredItems(SnWatcher* obj) {
|
||||||
{
|
|
||||||
GVariantBuilder builder;
|
GVariantBuilder builder;
|
||||||
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
|
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
|
||||||
for (GSList* l = items_; l != nullptr; l = g_slist_next(l)) {
|
for (GSList* l = items_; l != nullptr; l = g_slist_next(l)) {
|
||||||
GfWatch* watch = static_cast<GfWatch*>(l->data);
|
GfWatch* watch = static_cast<GfWatch*>(l->data);
|
||||||
gchar* item = g_strdup_printf("%s%s", watch->bus_name, watch->object_path);
|
gchar* item = g_strdup_printf("%s%s", watch->bus_name, watch->object_path);
|
||||||
|
|
Loading…
Reference in New Issue