2018-11-22 14:47:23 +00:00
|
|
|
#include "modules/sni/watcher.hpp"
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2019-05-18 23:44:45 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2023-10-20 20:39:10 +00:00
|
|
|
#include "util/scope_guard.hpp"
|
|
|
|
|
2018-09-04 21:50:08 +00:00
|
|
|
using namespace waybar::modules::SNI;
|
|
|
|
|
2020-02-19 11:06:35 +00:00
|
|
|
Watcher::Watcher()
|
2019-04-17 12:19:04 +00:00
|
|
|
: 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_(sn_watcher_skeleton_new()) {}
|
|
|
|
|
|
|
|
Watcher::~Watcher() {
|
2023-07-01 06:25:24 +00:00
|
|
|
if (hosts_ != nullptr) {
|
|
|
|
g_slist_free_full(hosts_, gfWatchFree);
|
|
|
|
hosts_ = nullptr;
|
|
|
|
}
|
2023-06-06 20:06:11 +00:00
|
|
|
if (items_ != nullptr) {
|
2019-04-17 12:19:04 +00:00
|
|
|
g_slist_free_full(items_, gfWatchFree);
|
2019-04-24 10:37:24 +00:00
|
|
|
items_ = nullptr;
|
2019-04-17 12:19:04 +00:00
|
|
|
}
|
2020-02-19 11:06:35 +00:00
|
|
|
Gio::DBus::unown_name(bus_name_id_);
|
2019-05-13 12:27:01 +00:00
|
|
|
auto iface = G_DBUS_INTERFACE_SKELETON(watcher_);
|
2019-05-17 11:51:55 +00:00
|
|
|
g_dbus_interface_skeleton_unexport(iface);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
|
2018-08-29 18:36:39 +00:00
|
|
|
GError* error = nullptr;
|
2023-10-20 20:39:10 +00:00
|
|
|
waybar::util::scope_guard error_deleter([error]() {
|
|
|
|
if (error) {
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
});
|
2022-04-06 06:37:19 +00:00
|
|
|
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_), conn->gobj(),
|
|
|
|
"/StatusNotifierWatcher", &error);
|
2018-08-29 18:36:39 +00:00
|
|
|
if (error != nullptr) {
|
2019-05-10 12:56:28 +00:00
|
|
|
// Don't print an error when a watcher is already present
|
|
|
|
if (error->code != 2) {
|
2020-02-19 11:06:35 +00:00
|
|
|
spdlog::error("Watcher: {}", error->message);
|
2019-05-10 12:56:28 +00:00
|
|
|
}
|
2018-08-29 18:36:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
g_signal_connect_swapped(watcher_, "handle-register-item",
|
|
|
|
G_CALLBACK(&Watcher::handleRegisterItem), this);
|
|
|
|
g_signal_connect_swapped(watcher_, "handle-register-host",
|
|
|
|
G_CALLBACK(&Watcher::handleRegisterHost), this);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
gboolean Watcher::handleRegisterHost(Watcher* obj, GDBusMethodInvocation* invocation,
|
|
|
|
const gchar* service) {
|
2018-08-29 18:36:39 +00:00
|
|
|
const gchar* bus_name = service;
|
|
|
|
const gchar* object_path = "/StatusNotifierHost";
|
|
|
|
|
|
|
|
if (*service == '/') {
|
|
|
|
bus_name = g_dbus_method_invocation_get_sender(invocation);
|
|
|
|
object_path = service;
|
|
|
|
}
|
|
|
|
if (g_dbus_is_name(bus_name) == FALSE) {
|
2022-04-06 06:37:19 +00:00
|
|
|
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
|
|
|
|
"D-Bus bus name '%s' is not valid", bus_name);
|
2018-08-29 18:36:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
auto watch = gfWatchFind(obj->hosts_, bus_name, object_path);
|
|
|
|
if (watch != nullptr) {
|
2019-04-17 12:19:04 +00:00
|
|
|
g_dbus_method_invocation_return_error(
|
2022-04-06 06:37:19 +00:00
|
|
|
invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
|
2019-04-17 12:19:04 +00:00
|
|
|
"Status Notifier Host with bus name '%s' and object path '%s' is already registered",
|
2022-04-06 06:37:19 +00:00
|
|
|
bus_name, object_path);
|
2018-08-29 18:36:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2018-09-04 21:50:08 +00:00
|
|
|
watch = gfWatchNew(GF_WATCH_TYPE_HOST, service, bus_name, object_path, obj);
|
2018-08-29 18:36:39 +00:00
|
|
|
obj->hosts_ = g_slist_prepend(obj->hosts_, watch);
|
2018-10-26 08:27:15 +00:00
|
|
|
if (!sn_watcher_get_is_host_registered(obj->watcher_)) {
|
|
|
|
sn_watcher_set_is_host_registered(obj->watcher_, TRUE);
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_emit_host_registered(obj->watcher_);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_complete_register_host(obj->watcher_, invocation);
|
2018-08-29 18:36:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
gboolean Watcher::handleRegisterItem(Watcher* obj, GDBusMethodInvocation* invocation,
|
|
|
|
const gchar* service) {
|
2018-08-29 18:36:39 +00:00
|
|
|
const gchar* bus_name = service;
|
|
|
|
const gchar* object_path = "/StatusNotifierItem";
|
|
|
|
|
|
|
|
if (*service == '/') {
|
|
|
|
bus_name = g_dbus_method_invocation_get_sender(invocation);
|
|
|
|
object_path = service;
|
|
|
|
}
|
|
|
|
if (g_dbus_is_name(bus_name) == FALSE) {
|
2022-04-06 06:37:19 +00:00
|
|
|
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
|
|
|
|
"D-Bus bus name '%s' is not valid", bus_name);
|
2018-08-29 18:36:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
auto watch = gfWatchFind(obj->items_, bus_name, object_path);
|
|
|
|
if (watch != nullptr) {
|
|
|
|
g_warning("Status Notifier Item with bus name '%s' and object path '%s' is already registered",
|
2022-04-06 06:37:19 +00:00
|
|
|
bus_name, object_path);
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_complete_register_item(obj->watcher_, invocation);
|
2018-08-29 18:36:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2018-09-04 21:50:08 +00:00
|
|
|
watch = gfWatchNew(GF_WATCH_TYPE_ITEM, service, bus_name, object_path, obj);
|
2018-08-29 18:36:39 +00:00
|
|
|
obj->items_ = g_slist_prepend(obj->items_, watch);
|
|
|
|
obj->updateRegisteredItems(obj->watcher_);
|
|
|
|
gchar* tmp = g_strdup_printf("%s%s", bus_name, object_path);
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_emit_item_registered(obj->watcher_, tmp);
|
2018-08-29 18:36:39 +00:00
|
|
|
g_free(tmp);
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_complete_register_item(obj->watcher_, invocation);
|
2018-08-29 18:36:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-09-04 21:50:08 +00:00
|
|
|
Watcher::GfWatch* Watcher::gfWatchFind(GSList* list, const gchar* bus_name,
|
2019-04-17 12:19:04 +00:00
|
|
|
const gchar* object_path) {
|
|
|
|
for (GSList* l = list; l != nullptr; l = g_slist_next(l)) {
|
2019-04-24 10:37:24 +00:00
|
|
|
auto watch = static_cast<GfWatch*>(l->data);
|
2019-04-17 12:19:04 +00:00
|
|
|
if (g_strcmp0(watch->bus_name, bus_name) == 0 &&
|
|
|
|
g_strcmp0(watch->object_path, object_path) == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
return watch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void Watcher::gfWatchFree(gpointer data) {
|
2019-04-24 10:37:24 +00:00
|
|
|
auto watch = static_cast<GfWatch*>(data);
|
2019-04-17 12:19:04 +00:00
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
if (watch->watch_id > 0) {
|
|
|
|
g_bus_unwatch_name(watch->watch_id);
|
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
|
|
|
|
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) {
|
2018-08-29 18:36:39 +00:00
|
|
|
GfWatch* watch = g_new0(GfWatch, 1);
|
|
|
|
watch->type = type;
|
2018-09-04 21:50:08 +00:00
|
|
|
watch->watcher = watcher;
|
2018-08-29 18:36:39 +00:00
|
|
|
watch->service = g_strdup(service);
|
|
|
|
watch->bus_name = g_strdup(bus_name);
|
|
|
|
watch->object_path = g_strdup(object_path);
|
2022-04-06 06:37:19 +00:00
|
|
|
watch->watch_id = g_bus_watch_name(G_BUS_TYPE_SESSION, bus_name, G_BUS_NAME_WATCHER_FLAGS_NONE,
|
|
|
|
nullptr, &Watcher::nameVanished, watch, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
return watch;
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void Watcher::nameVanished(GDBusConnection* connection, const char* name, gpointer data) {
|
|
|
|
auto watch = static_cast<GfWatch*>(data);
|
2018-09-04 21:50:08 +00:00
|
|
|
if (watch->type == GF_WATCH_TYPE_HOST) {
|
|
|
|
watch->watcher->hosts_ = g_slist_remove(watch->watcher->hosts_, watch);
|
2019-04-17 12:19:04 +00:00
|
|
|
if (watch->watcher->hosts_ == nullptr) {
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_set_is_host_registered(watch->watcher->watcher_, FALSE);
|
|
|
|
sn_watcher_emit_host_registered(watch->watcher->watcher_);
|
2018-09-04 21:50:08 +00:00
|
|
|
}
|
|
|
|
} else if (watch->type == GF_WATCH_TYPE_ITEM) {
|
|
|
|
watch->watcher->items_ = g_slist_remove(watch->watcher->items_, watch);
|
|
|
|
watch->watcher->updateRegisteredItems(watch->watcher->watcher_);
|
|
|
|
gchar* tmp = g_strdup_printf("%s%s", watch->bus_name, watch->object_path);
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_emit_item_unregistered(watch->watcher->watcher_, tmp);
|
2018-09-04 21:50:08 +00:00
|
|
|
g_free(tmp);
|
|
|
|
}
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void Watcher::updateRegisteredItems(SnWatcher* obj) {
|
2018-08-29 18:36:39 +00:00
|
|
|
GVariantBuilder builder;
|
2019-04-17 12:19:04 +00:00
|
|
|
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
|
2018-08-29 18:36:39 +00:00
|
|
|
for (GSList* l = items_; l != nullptr; l = g_slist_next(l)) {
|
2022-04-06 06:37:19 +00:00
|
|
|
auto watch = static_cast<GfWatch*>(l->data);
|
2019-04-24 10:37:24 +00:00
|
|
|
gchar* item = g_strdup_printf("%s%s", watch->bus_name, watch->object_path);
|
2018-09-04 21:50:08 +00:00
|
|
|
g_variant_builder_add(&builder, "s", item);
|
|
|
|
g_free(item);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
GVariant* variant = g_variant_builder_end(&builder);
|
2018-09-04 21:50:08 +00:00
|
|
|
const gchar** items = g_variant_get_strv(variant, nullptr);
|
2018-10-26 08:05:54 +00:00
|
|
|
sn_watcher_set_registered_items(obj, items);
|
2018-08-29 18:36:39 +00:00
|
|
|
g_variant_unref(variant);
|
|
|
|
g_free(items);
|
2019-05-18 23:44:45 +00:00
|
|
|
}
|