2023-03-17 23:02:40 +00:00
|
|
|
#include "SNI.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "Widget.h"
|
2023-05-03 18:57:24 +00:00
|
|
|
#include "Config.h"
|
|
|
|
|
|
|
|
#ifdef WITH_SNI
|
2023-03-17 23:02:40 +00:00
|
|
|
|
|
|
|
#include <sni-watcher.h>
|
|
|
|
#include <sni-item.h>
|
|
|
|
#include <gio/gio.h>
|
2023-04-30 21:13:41 +00:00
|
|
|
#include <libdbusmenu-gtk/menu.h>
|
2023-03-17 23:02:40 +00:00
|
|
|
|
2023-07-24 23:45:26 +00:00
|
|
|
#include <cstdio>
|
2023-07-26 12:00:00 +00:00
|
|
|
#include <unordered_set>
|
2023-03-17 23:02:40 +00:00
|
|
|
|
|
|
|
namespace SNI
|
|
|
|
{
|
|
|
|
sniWatcher* watcherSkeleton;
|
|
|
|
guint watcherID;
|
|
|
|
GDBusConnection* dbusConnection = nullptr;
|
|
|
|
|
|
|
|
guint hostID;
|
|
|
|
|
|
|
|
struct Item
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
std::string object;
|
2023-07-28 17:11:13 +00:00
|
|
|
size_t w = 0;
|
|
|
|
size_t h = 0;
|
2023-11-05 21:03:50 +00:00
|
|
|
GdkPixbuf* pixbuf = nullptr;
|
2023-04-30 16:19:40 +00:00
|
|
|
|
2023-07-28 17:11:13 +00:00
|
|
|
std::string tooltip = "";
|
2023-04-30 21:13:41 +00:00
|
|
|
|
2023-07-28 17:11:13 +00:00
|
|
|
std::string menuObjectPath = "";
|
2023-04-30 21:13:41 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
Texture* textureWidget = nullptr;
|
2023-07-28 17:11:13 +00:00
|
|
|
EventBox* gtkEvent = nullptr;
|
2024-02-16 19:03:34 +00:00
|
|
|
GtkMenu* dbusMenu = nullptr;
|
2023-07-28 16:56:51 +00:00
|
|
|
|
2023-07-28 17:11:13 +00:00
|
|
|
int watcherID = -1;
|
2023-08-23 22:09:05 +00:00
|
|
|
int propertyChangeWatcherID = -1;
|
2024-03-18 01:26:09 +00:00
|
|
|
|
|
|
|
bool gathering = false;
|
2023-03-17 23:02:40 +00:00
|
|
|
};
|
2024-03-18 01:26:09 +00:00
|
|
|
std::vector<std::unique_ptr<Item>> items;
|
2023-03-17 23:02:40 +00:00
|
|
|
|
2023-05-04 13:04:06 +00:00
|
|
|
std::unordered_map<std::string, Item> clientsToQuery;
|
2023-07-26 12:00:00 +00:00
|
|
|
std::unordered_set<std::string> reloadedNames;
|
2023-04-14 21:44:30 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
// Gtk stuff
|
|
|
|
// TODO: Investigate if the two box approach is still needed, since we now actually add/delete items
|
2023-03-17 23:02:40 +00:00
|
|
|
Widget* parentBox;
|
|
|
|
Widget* iconBox;
|
|
|
|
|
2023-11-05 21:03:50 +00:00
|
|
|
// Swap channels for the render format and create a pixbuf out of it
|
|
|
|
static GdkPixbuf* ToPixbuf(uint8_t* sniData, int32_t width, int32_t height)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < width * height; i++)
|
|
|
|
{
|
|
|
|
struct Px
|
|
|
|
{
|
|
|
|
// This should be bgra...
|
|
|
|
// Since source is ARGB32 in network order(=big-endian)
|
|
|
|
// and x86 Linux is little-endian, we *should* swap b and r...
|
|
|
|
uint8_t a, r, g, b;
|
|
|
|
};
|
|
|
|
Px& pixel = ((Px*)sniData)[i];
|
|
|
|
// Swap to create rgba
|
|
|
|
pixel = {pixel.r, pixel.g, pixel.b, pixel.a};
|
|
|
|
}
|
|
|
|
return gdk_pixbuf_new_from_data(
|
|
|
|
sniData, GDK_COLORSPACE_RGB, true, 8, width, height, width * 4,
|
|
|
|
+[](uint8_t* data, void*)
|
|
|
|
{
|
|
|
|
delete[] data;
|
|
|
|
},
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2023-12-03 21:48:00 +00:00
|
|
|
static bool ItemMatchesFilter(const Item& item, const std::string& filter, bool& wasExplicitOverride)
|
2023-11-05 21:03:50 +00:00
|
|
|
{
|
2023-12-03 21:48:00 +00:00
|
|
|
auto& filterString = item.tooltip == "" ? item.object : item.tooltip;
|
|
|
|
if (filterString.find(filter) != std::string::npos)
|
2023-11-05 21:03:50 +00:00
|
|
|
{
|
2023-12-03 21:48:00 +00:00
|
|
|
wasExplicitOverride = true;
|
|
|
|
return true;
|
2023-11-05 21:03:50 +00:00
|
|
|
}
|
2023-12-03 21:48:00 +00:00
|
|
|
else if (filter == "*" && !wasExplicitOverride)
|
2023-11-05 21:03:50 +00:00
|
|
|
{
|
2023-12-03 21:48:00 +00:00
|
|
|
return true;
|
2023-11-05 21:03:50 +00:00
|
|
|
}
|
2023-12-03 21:48:00 +00:00
|
|
|
return false;
|
2023-11-05 21:03:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
template<typename OnFinishFn>
|
|
|
|
inline void GatherItemProperties(Item& item, OnFinishFn&& onFinish)
|
2023-03-17 23:02:40 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
// Only gather once.
|
|
|
|
if (item.gathering)
|
|
|
|
return;
|
|
|
|
item.gathering = true;
|
|
|
|
struct AsyncData
|
|
|
|
{
|
|
|
|
Item& item;
|
|
|
|
OnFinishFn onFinish;
|
|
|
|
};
|
|
|
|
auto onAsyncResult = [](GObject*, GAsyncResult* result, void* dataPtr)
|
2023-03-17 23:02:40 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
// Data *must* be manually freed!
|
|
|
|
AsyncData* data = (AsyncData*)dataPtr;
|
|
|
|
data->item.gathering = false;
|
|
|
|
|
2023-03-17 23:02:40 +00:00
|
|
|
GError* err = nullptr;
|
2024-03-18 01:26:09 +00:00
|
|
|
GVariant* allPropertiesWrapped = g_dbus_connection_call_finish(dbusConnection, result, &err);
|
2023-03-17 23:02:40 +00:00
|
|
|
if (err)
|
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
LOG("SNI: g_dbus_connection_call failed with: " << err->message);
|
2023-03-17 23:02:40 +00:00
|
|
|
g_error_free(err);
|
2024-03-18 01:26:09 +00:00
|
|
|
delete data;
|
|
|
|
return;
|
2023-03-17 23:02:40 +00:00
|
|
|
}
|
2023-08-22 20:30:38 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
// Unwrap tuple
|
|
|
|
GVariant* allProperties = g_variant_get_child_value(allPropertiesWrapped, 0);
|
|
|
|
auto getProperty = [&](const std::string_view& prop)
|
2023-08-22 13:14:13 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
return g_variant_lookup_value(allProperties, prop.data(), nullptr);
|
|
|
|
};
|
2023-08-22 13:14:13 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
// Query tooltip(Steam e.g. doesn't have one)
|
|
|
|
GVariant* tooltip = getProperty("ToolTip");
|
|
|
|
if (tooltip)
|
2023-08-22 13:14:13 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
const gchar* title = nullptr;
|
|
|
|
if (g_variant_is_container(tooltip) && g_variant_n_children(tooltip) >= 4)
|
|
|
|
{
|
|
|
|
// According to spec, ToolTip is of type (sa(iiab)ss) => 4 children
|
|
|
|
// Most icons only set the "title" component (e.g. Discord, KeePassXC, ...)
|
|
|
|
g_variant_get_child(tooltip, 2, "s", &title);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TeamViewer only exposes a string, which is not according to spec!
|
|
|
|
title = g_variant_get_string(tooltip, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (title != nullptr)
|
|
|
|
{
|
|
|
|
data->item.tooltip = title;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("SNI: Error querying tooltip");
|
|
|
|
}
|
|
|
|
LOG("SNI: Title: " << data->item.tooltip);
|
|
|
|
g_variant_unref(tooltip);
|
2023-08-22 13:14:13 +00:00
|
|
|
}
|
2023-04-30 16:19:40 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
// Query menu
|
|
|
|
GVariant* menuPath = getProperty("Menu");
|
|
|
|
if (menuPath)
|
|
|
|
{
|
|
|
|
const char* objectPath;
|
|
|
|
g_variant_get(menuPath, "o", &objectPath);
|
|
|
|
LOG("SNI: Menu object path: " << objectPath);
|
2023-04-30 21:13:41 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
data->item.menuObjectPath = objectPath;
|
2023-04-30 21:13:41 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
g_variant_unref(menuPath);
|
|
|
|
}
|
2023-04-30 21:13:41 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
bool wasExplicitOverride = false;
|
|
|
|
for (auto& [filter, disabled] : Config::Get().sniDisabled)
|
2023-12-03 21:48:31 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
if (ItemMatchesFilter(data->item, filter, wasExplicitOverride))
|
2023-12-03 21:48:31 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
if (disabled)
|
|
|
|
{
|
|
|
|
LOG("SNI: Disabling item due to config");
|
|
|
|
// We're done here.
|
|
|
|
delete data;
|
|
|
|
return;
|
|
|
|
}
|
2023-12-03 21:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
// First try icon theme querying
|
|
|
|
std::string iconName;
|
|
|
|
wasExplicitOverride = false;
|
|
|
|
for (auto& [filter, name] : Config::Get().sniIconNames)
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
if (ItemMatchesFilter(data->item, filter, wasExplicitOverride))
|
|
|
|
{
|
|
|
|
iconName = name;
|
|
|
|
}
|
2023-12-03 21:48:00 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
if (iconName == "")
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
GVariant* iconNameVar = getProperty("IconName");
|
|
|
|
if (iconNameVar)
|
|
|
|
{
|
|
|
|
iconName = g_variant_get_string(iconNameVar, nullptr);
|
2023-12-03 21:48:00 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
g_variant_unref(iconNameVar);
|
|
|
|
}
|
2023-12-03 21:48:00 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
if (iconName != "")
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
GError* err = nullptr;
|
|
|
|
GtkIconTheme* defaultTheme = gtk_icon_theme_get_default();
|
|
|
|
GdkPixbuf* pixbuf = gtk_icon_theme_load_icon(defaultTheme, iconName.c_str(), 64, GTK_ICON_LOOKUP_FORCE_SVG, &err);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
LOG("SNI: gtk_icon_theme_load_icon failed: " << err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
else if (pixbuf)
|
|
|
|
{
|
|
|
|
LOG("SNI: Creating icon from \"" << iconName << "\"");
|
|
|
|
data->item.pixbuf = pixbuf;
|
|
|
|
data->item.w = gdk_pixbuf_get_width(pixbuf);
|
|
|
|
data->item.h = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
}
|
2023-12-03 21:48:00 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
|
|
|
|
if (data->item.pixbuf == nullptr)
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
GVariant* iconPixmap = getProperty("IconPixmap");
|
|
|
|
if (iconPixmap == nullptr)
|
|
|
|
{
|
|
|
|
// All icon locations have failed, bail.
|
|
|
|
LOG("SNI: Cannot create item due to missing icon!");
|
|
|
|
delete data;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GVariantIter* arrIter = nullptr;
|
|
|
|
g_variant_get(iconPixmap, "a(iiay)", &arrIter);
|
|
|
|
|
|
|
|
if (g_variant_iter_n_children(arrIter) != 0)
|
|
|
|
{
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
GVariantIter* dataIter = nullptr;
|
|
|
|
g_variant_iter_next(arrIter, "(iiay)", &width, &height, &dataIter);
|
|
|
|
|
|
|
|
LOG("SNI: Width: " << width);
|
|
|
|
LOG("SNI: Height: " << height);
|
|
|
|
data->item.w = width;
|
|
|
|
data->item.h = height;
|
|
|
|
uint8_t* iconData = new uint8_t[width * height * 4];
|
|
|
|
|
|
|
|
uint8_t px = 0;
|
|
|
|
int i = 0;
|
|
|
|
while (g_variant_iter_next(dataIter, "y", &px))
|
|
|
|
{
|
|
|
|
iconData[i] = px;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
LOG("SNI: Creating icon from pixmap");
|
|
|
|
data->item.pixbuf = ToPixbuf(iconData, width, height);
|
|
|
|
|
|
|
|
g_variant_iter_free(dataIter);
|
|
|
|
}
|
|
|
|
g_variant_iter_free(arrIter);
|
|
|
|
g_variant_unref(iconPixmap);
|
2023-12-03 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
data->onFinish(data->item);
|
|
|
|
delete data;
|
|
|
|
g_variant_unref(allProperties);
|
|
|
|
g_variant_unref(allPropertiesWrapped);
|
|
|
|
};
|
|
|
|
|
|
|
|
// The tuples will be owned by g_dbus_connection_call, so no cleanup needed
|
|
|
|
AsyncData* data = new AsyncData{item, onFinish};
|
|
|
|
GError* err = nullptr;
|
|
|
|
GVariant* params[1];
|
|
|
|
params[0] = g_variant_new_string("org.kde.StatusNotifierItem");
|
|
|
|
GVariant* paramsTuple = g_variant_new_tuple(params, 1);
|
|
|
|
g_dbus_connection_call(dbusConnection, item.name.c_str(), item.object.c_str(), "org.freedesktop.DBus.Properties", "GetAll", paramsTuple,
|
|
|
|
G_VARIANT_TYPE("(a{sv})"), G_DBUS_CALL_FLAGS_NONE, -1, nullptr, +onAsyncResult, data);
|
|
|
|
if (err)
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
LOG("SNI: g_dbus_connection_call failed with: " << err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DestroyItem(Item& item)
|
|
|
|
{
|
|
|
|
g_bus_unwatch_name(item.watcherID);
|
|
|
|
g_dbus_connection_signal_unsubscribe(dbusConnection, item.propertyChangeWatcherID);
|
|
|
|
g_object_unref(item.pixbuf);
|
|
|
|
// dbus menu will be deleted by automatically when the parent widget is destroyed
|
|
|
|
}
|
2023-12-03 21:48:00 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
static void AddSNIItem(Item& item)
|
|
|
|
{
|
|
|
|
if (!item.pixbuf)
|
|
|
|
return;
|
|
|
|
auto eventBox = Widget::Create<EventBox>();
|
|
|
|
item.gtkEvent = eventBox.get();
|
2023-12-03 21:48:00 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
eventBox->SetOnCreate(
|
|
|
|
[&](Widget& w)
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
// Create the menu if it wasn't already created
|
|
|
|
if (!item.dbusMenu)
|
2023-12-03 21:48:00 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
item.dbusMenu = (GtkMenu*)dbusmenu_gtkmenu_new(item.name.data(), item.menuObjectPath.data());
|
|
|
|
gtk_menu_attach_to_widget(item.dbusMenu, item.gtkEvent->Get(), nullptr);
|
2023-12-03 21:48:00 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
auto clickFn = [](GtkWidget*, GdkEventButton* event, void* data) -> gboolean
|
|
|
|
{
|
|
|
|
if (event->button == 1)
|
|
|
|
{
|
|
|
|
GtkMenu* menu = (GtkMenu*)data;
|
|
|
|
|
|
|
|
gtk_menu_popup_at_pointer(menu, (GdkEvent*)event);
|
|
|
|
LOG("SNI: Opened popup!");
|
|
|
|
}
|
|
|
|
return GDK_EVENT_STOP;
|
|
|
|
};
|
|
|
|
g_signal_connect(w.Get(), "button-release-event", G_CALLBACK(+clickFn), item.dbusMenu);
|
|
|
|
});
|
2023-12-03 21:48:00 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
LOG("SNI: Add " << item.name << " to widget");
|
|
|
|
auto texture = Widget::Create<Texture>();
|
|
|
|
bool wasExplicitOverride = false;
|
|
|
|
int size = 24;
|
|
|
|
for (auto& [filter, iconSize] : Config::Get().sniIconSizes)
|
|
|
|
{
|
|
|
|
if (ItemMatchesFilter(item, filter, wasExplicitOverride))
|
|
|
|
{
|
|
|
|
size = iconSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wasExplicitOverride = false;
|
|
|
|
for (auto& [filter, padding] : Config::Get().sniPaddingTop)
|
|
|
|
{
|
|
|
|
if (ItemMatchesFilter(item, filter, wasExplicitOverride))
|
|
|
|
{
|
|
|
|
texture->AddPaddingTop(padding);
|
2023-12-03 21:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
bool rotatedIcons = (Config::Get().location == 'L' || Config::Get().location == 'R') && Config::Get().iconsAlwaysUp;
|
|
|
|
Utils::SetTransform(*texture, {size, true, Alignment::Fill}, {size, true, Alignment::Fill, 0, rotatedIcons ? 6 : 0});
|
|
|
|
texture->SetBuf(item.pixbuf, item.w, item.h);
|
|
|
|
texture->SetTooltip(item.tooltip);
|
|
|
|
texture->SetAngle(Utils::GetAngle() == 270 ? 90 : 0);
|
|
|
|
|
|
|
|
item.textureWidget = texture.get();
|
2023-12-03 21:48:00 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
eventBox->AddChild(std::move(texture));
|
|
|
|
iconBox->AddChild(std::move(eventBox));
|
2023-03-17 23:02:40 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
static void RemoveSNIItem(Item& item)
|
2024-02-16 19:03:34 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
iconBox->RemoveChild(item.gtkEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TimerResult UpdateWidgets(Box&);
|
|
|
|
|
|
|
|
void WidgetSNI(Widget& parent)
|
|
|
|
{
|
|
|
|
if (RuntimeConfig::Get().hasSNI == false || Config::Get().enableSNI == false)
|
2024-02-16 19:03:34 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
return;
|
2024-02-16 19:03:34 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
// Add parent box
|
|
|
|
auto box = Widget::Create<Box>();
|
|
|
|
box->AddClass("widget");
|
|
|
|
box->AddClass("sni");
|
|
|
|
Utils::SetTransform(*box, {-1, false, Alignment::Fill});
|
|
|
|
|
|
|
|
auto container = Widget::Create<Box>();
|
|
|
|
container->AddTimer<Box>(UpdateWidgets, 1000, TimerDispatchBehaviour::LateDispatch);
|
|
|
|
container->SetSpacing({4, false});
|
|
|
|
container->SetOrientation(Utils::GetOrientation());
|
|
|
|
Utils::SetTransform(*container, {-1, true, Alignment::Fill, 0, 8});
|
2024-02-16 19:03:34 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
iconBox = container.get();
|
|
|
|
parentBox = box.get();
|
|
|
|
box->AddChild(std::move(container));
|
|
|
|
parent.AddChild(std::move(box));
|
|
|
|
}
|
2023-04-30 15:46:16 +00:00
|
|
|
|
2023-03-18 15:05:51 +00:00
|
|
|
static void DBusNameVanished(GDBusConnection*, const char* name, void*)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(items.begin(), items.end(),
|
2024-03-18 01:26:09 +00:00
|
|
|
[&](const std::unique_ptr<Item>& item)
|
2023-03-18 15:05:51 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
return item->name == name;
|
2023-03-18 15:05:51 +00:00
|
|
|
});
|
|
|
|
if (it != items.end())
|
|
|
|
{
|
2023-04-30 16:21:12 +00:00
|
|
|
LOG("SNI: " << name << " vanished!");
|
2024-03-18 01:26:09 +00:00
|
|
|
RemoveSNIItem(*it->get());
|
|
|
|
DestroyItem(*it->get());
|
2023-03-18 15:05:51 +00:00
|
|
|
items.erase(it);
|
2023-07-28 16:56:51 +00:00
|
|
|
return;
|
2023-03-18 15:05:51 +00:00
|
|
|
}
|
2023-07-28 16:56:51 +00:00
|
|
|
|
|
|
|
auto toRegisterIt = clientsToQuery.find(name);
|
|
|
|
if (toRegisterIt != clientsToQuery.end())
|
2023-03-18 15:05:51 +00:00
|
|
|
{
|
2023-07-28 16:56:51 +00:00
|
|
|
clientsToQuery.erase(toRegisterIt);
|
2023-03-18 15:05:51 +00:00
|
|
|
}
|
2023-07-28 16:56:51 +00:00
|
|
|
|
|
|
|
LOG("SNI: Cannot remove unregistered bus name " << name);
|
|
|
|
return;
|
2023-03-18 15:05:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
static void ItemPropertyChanged(GDBusConnection*, const char* senderName, const char* object, const char*, const char* signalName, GVariant*,
|
|
|
|
void* name)
|
2023-04-30 15:46:16 +00:00
|
|
|
{
|
2023-07-26 11:28:50 +00:00
|
|
|
LOG("SNI: Reloading " << (const char*)name << " " << object << " (Sender: " << senderName << ")");
|
2023-08-23 22:09:05 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
// Check if we're interested.
|
|
|
|
if (strcmp(signalName, "NewIcon") != 0 && strcmp(signalName, "NewToolTip") != 0)
|
|
|
|
return;
|
2023-08-23 22:09:05 +00:00
|
|
|
|
2024-03-18 01:26:09 +00:00
|
|
|
GError* err = nullptr;
|
|
|
|
g_dbus_connection_flush_sync(dbusConnection, nullptr, &err);
|
|
|
|
if (err)
|
2023-04-30 15:46:16 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
LOG("SNI: g_dbus_connection_call_sync failed: " << err->message);
|
|
|
|
g_error_free(err);
|
2023-04-30 15:46:16 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
auto itemIt = std::find_if(items.begin(), items.end(),
|
|
|
|
[&](const std::unique_ptr<Item>& item)
|
|
|
|
{
|
|
|
|
return item->name == (const char*)name;
|
|
|
|
});
|
|
|
|
if (itemIt == items.end())
|
2023-05-04 13:04:06 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
LOG("SNI: Couldn't update " << name << "!");
|
|
|
|
return;
|
2023-05-04 13:04:06 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
GatherItemProperties(**itemIt,
|
|
|
|
[](Item& item)
|
|
|
|
{
|
|
|
|
item.textureWidget->SetBuf(item.pixbuf, item.w, item.h);
|
|
|
|
item.textureWidget->SetTooltip(item.tooltip);
|
|
|
|
});
|
2023-04-30 15:46:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 21:44:30 +00:00
|
|
|
static TimerResult UpdateWidgets(Box&)
|
|
|
|
{
|
2023-07-28 16:58:00 +00:00
|
|
|
// Flush connection, so we hopefully don't deadlock with any client
|
2024-03-18 01:26:09 +00:00
|
|
|
// TODO: Figure out if this is still needed when using gathering async
|
2023-07-28 16:58:00 +00:00
|
|
|
GError* err = nullptr;
|
|
|
|
g_dbus_connection_flush_sync(dbusConnection, nullptr, &err);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
LOG("SNI: g_dbus_connection_call_sync failed: " << err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:57:24 +00:00
|
|
|
if (RuntimeConfig::Get().hasSNI == false || Config::Get().enableSNI == false)
|
|
|
|
{
|
|
|
|
// Don't bother
|
|
|
|
return TimerResult::Delete;
|
|
|
|
}
|
2023-05-04 13:04:06 +00:00
|
|
|
for (auto& [name, client] : clientsToQuery)
|
2023-04-14 21:44:30 +00:00
|
|
|
{
|
|
|
|
LOG("SNI: Creating Item " << client.name << " " << client.object);
|
2024-03-18 01:26:09 +00:00
|
|
|
std::unique_ptr<Item> item = std::make_unique<Item>();
|
|
|
|
item->name = std::move(client.name);
|
|
|
|
item->object = std::move(client.object);
|
|
|
|
Item& itemRef = *item;
|
2023-04-14 21:44:30 +00:00
|
|
|
items.push_back(std::move(item));
|
2024-03-18 01:26:09 +00:00
|
|
|
auto onGatherFinish = [](Item& item)
|
2023-04-14 21:44:30 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
if (item.pixbuf == nullptr)
|
2023-05-04 14:14:18 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
return;
|
2023-05-04 14:14:18 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
// Add handler for removing
|
|
|
|
item.watcherID = g_bus_watch_name_on_connection(dbusConnection, item.name.c_str(), G_BUS_NAME_WATCHER_FLAGS_NONE, nullptr,
|
|
|
|
DBusNameVanished, nullptr, nullptr);
|
|
|
|
|
|
|
|
// Add handler for icon change
|
|
|
|
char* staticBuf = new char[item.name.size() + 1]{0x0};
|
|
|
|
memcpy(staticBuf, item.name.c_str(), item.name.size());
|
|
|
|
LOG("SNI: Allocating static name buffer for " << item.name);
|
|
|
|
item.propertyChangeWatcherID = g_dbus_connection_signal_subscribe(
|
|
|
|
dbusConnection, item.name.c_str(), "org.kde.StatusNotifierItem", nullptr, nullptr, nullptr, G_DBUS_SIGNAL_FLAGS_NONE,
|
|
|
|
ItemPropertyChanged, staticBuf,
|
|
|
|
+[](void* ptr)
|
2023-05-04 14:14:18 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
LOG("SNI: Delete static name buffer for " << (char*)ptr);
|
|
|
|
delete[] (char*)ptr;
|
|
|
|
});
|
|
|
|
AddSNIItem(item);
|
|
|
|
};
|
|
|
|
GatherItemProperties(itemRef, onGatherFinish);
|
2023-05-03 18:57:24 +00:00
|
|
|
}
|
2024-03-18 01:26:09 +00:00
|
|
|
clientsToQuery.clear();
|
|
|
|
return TimerResult::Ok;
|
2023-04-14 21:44:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 23:02:40 +00:00
|
|
|
// Methods
|
2023-04-14 21:44:30 +00:00
|
|
|
static bool RegisterItem(sniWatcher* watcher, GDBusMethodInvocation* invocation, const char* service)
|
2023-03-17 23:02:40 +00:00
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
std::string object;
|
|
|
|
if (strncmp(service, "/", 1) == 0)
|
|
|
|
{
|
|
|
|
// service is object (used by e.g. ayatana -> steam, discord)
|
|
|
|
object = service;
|
|
|
|
name = g_dbus_method_invocation_get_sender(invocation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// service is bus name (used by e.g. Telegram)
|
|
|
|
name = service;
|
|
|
|
object = "/StatusNotifierItem";
|
|
|
|
}
|
2023-03-18 15:05:51 +00:00
|
|
|
auto it = std::find_if(items.begin(), items.end(),
|
2024-03-18 01:26:09 +00:00
|
|
|
[&](const std::unique_ptr<Item>& item)
|
2023-03-18 15:05:51 +00:00
|
|
|
{
|
2024-03-18 01:26:09 +00:00
|
|
|
return item->name == name && item->object == object;
|
2023-03-18 15:05:51 +00:00
|
|
|
});
|
2023-03-17 23:02:40 +00:00
|
|
|
if (it != items.end())
|
|
|
|
{
|
|
|
|
LOG("Rejecting " << name << " " << object);
|
2023-04-14 21:44:30 +00:00
|
|
|
return false;
|
2023-03-17 23:02:40 +00:00
|
|
|
}
|
2023-04-14 21:44:30 +00:00
|
|
|
sni_watcher_emit_status_notifier_item_registered(watcher, service);
|
|
|
|
sni_watcher_complete_register_status_notifier_item(watcher, invocation);
|
2023-03-17 23:02:40 +00:00
|
|
|
LOG("SNI: Registered Item " << name << " " << object);
|
2023-05-04 13:04:06 +00:00
|
|
|
clientsToQuery[name] = {name, std::move(object)};
|
2023-04-14 21:44:30 +00:00
|
|
|
return true;
|
2023-03-17 23:02:40 +00:00
|
|
|
}
|
2023-04-14 21:44:30 +00:00
|
|
|
|
2023-03-17 23:02:40 +00:00
|
|
|
static void RegisterHost(sniWatcher*, GDBusMethodInvocation*, const char*)
|
|
|
|
{
|
|
|
|
LOG("TODO: Implement RegisterHost!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signals
|
|
|
|
static void ItemRegistered(sniWatcher*, const char*, void*)
|
|
|
|
{
|
|
|
|
// Don't care, since watcher and host will always be from gBar (at least for now)
|
|
|
|
}
|
|
|
|
static void ItemUnregistered(sniWatcher*, const char*, void*)
|
|
|
|
{
|
|
|
|
// Don't care, since watcher and host will always be from gBar (at least for now)
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
2023-05-03 18:57:24 +00:00
|
|
|
if (RuntimeConfig::Get().hasSNI == false || Config::Get().enableSNI == false)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-17 23:02:40 +00:00
|
|
|
auto busAcquired = [](GDBusConnection* connection, const char*, void*)
|
|
|
|
{
|
|
|
|
GError* err = nullptr;
|
|
|
|
g_dbus_interface_skeleton_export((GDBusInterfaceSkeleton*)watcherSkeleton, connection, "/StatusNotifierWatcher", &err);
|
|
|
|
if (err)
|
|
|
|
{
|
2023-05-03 18:57:24 +00:00
|
|
|
LOG("SNI: Failed to connect to dbus! Disabling SNI. Error: " << err->message);
|
|
|
|
RuntimeConfig::Get().hasSNI = false;
|
2023-03-17 23:02:40 +00:00
|
|
|
g_error_free(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbusConnection = connection;
|
|
|
|
|
|
|
|
// Connect methods and signals
|
|
|
|
g_signal_connect(watcherSkeleton, "handle-register-status-notifier-item", G_CALLBACK(RegisterItem), nullptr);
|
|
|
|
g_signal_connect(watcherSkeleton, "handle-register-status-notifier-host", G_CALLBACK(RegisterHost), nullptr);
|
|
|
|
|
|
|
|
g_signal_connect(watcherSkeleton, "status-notifier-item-registered", G_CALLBACK(ItemRegistered), nullptr);
|
|
|
|
g_signal_connect(watcherSkeleton, "status-notifier-item-unregistered", G_CALLBACK(ItemUnregistered), nullptr);
|
|
|
|
|
|
|
|
// Host is always available
|
|
|
|
sni_watcher_set_is_status_notifier_host_registered(watcherSkeleton, true);
|
2024-03-18 01:26:09 +00:00
|
|
|
sni_watcher_emit_status_notifier_host_registered(watcherSkeleton);
|
2023-03-17 23:02:40 +00:00
|
|
|
};
|
|
|
|
auto emptyCallback = [](GDBusConnection*, const char*, void*) {};
|
2023-07-28 17:11:13 +00:00
|
|
|
auto lostName = [](GDBusConnection*, const char*, void*)
|
2023-03-17 23:02:40 +00:00
|
|
|
{
|
2023-05-03 18:57:24 +00:00
|
|
|
LOG("SNI: Lost Name! Disabling SNI!");
|
|
|
|
RuntimeConfig::Get().hasSNI = false;
|
2023-03-17 23:02:40 +00:00
|
|
|
};
|
2023-08-23 13:29:13 +00:00
|
|
|
auto flags = G_BUS_NAME_OWNER_FLAGS_REPLACE;
|
2023-03-17 23:02:40 +00:00
|
|
|
g_bus_own_name(G_BUS_TYPE_SESSION, "org.kde.StatusNotifierWatcher", (GBusNameOwnerFlags)flags, +busAcquired, +emptyCallback, +lostName,
|
|
|
|
nullptr, nullptr);
|
|
|
|
watcherSkeleton = sni_watcher_skeleton_new();
|
|
|
|
|
|
|
|
std::string hostName = "org.kde.StatusNotifierHost-" + std::to_string(getpid());
|
|
|
|
g_bus_own_name(G_BUS_TYPE_SESSION, hostName.c_str(), (GBusNameOwnerFlags)flags, +emptyCallback, +emptyCallback, +emptyCallback, nullptr,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {}
|
|
|
|
}
|
2023-05-03 18:57:24 +00:00
|
|
|
#endif
|