#include "linux_tray.h"
#define ITEM_IFACE "org.kde.StatusNotifierItem"
#define WATCHER "org.kde.StatusNotifierWatcher"
#define ITEM_PATH "/StatusNotifierItem"
static const char item_xml[] =
"<node><interface name='" ITEM_IFACE "'>"
"<method name='Activate'><arg type='i' direction='in'/><arg type='i' direction='in'/></method>"
"<method name='ContextMenu'><arg type='i' direction='in'/><arg type='i' direction='in'/></method>"
"<method name='SecondaryActivate'><arg type='i' direction='in'/><arg type='i' direction='in'/></method>"
"<property name='Category' type='s' access='read'/><property name='Id' type='s' access='read'/>"
"<property name='Title' type='s' access='read'/><property name='Status' type='s' access='read'/>"
"<property name='WindowId' type='u' access='read'/><property name='IconName' type='s' access='read'/>"
"<property name='IconPixmap' type='a(iiay)' access='read'/>"
"<property name='ToolTip' type='(sa(iiay)ss)' access='read'/>"
"<property name='ItemIsMenu' type='b' access='read'/><property name='Menu' type='o' access='read'/>"
"</interface></node>";
static GVariant *pixmaps;
static void (*activate)(void);
static void (*dismiss_popup)(void);
static GDBusProxy *watcher;
static char *owner;
static gboolean registered;
static guint retry;
static GVariant *get_property(GDBusConnection *bus G_GNUC_UNUSED, const gchar *sender G_GNUC_UNUSED, const gchar *path G_GNUC_UNUSED,
const gchar *iface G_GNUC_UNUSED, const gchar *name, GError **error G_GNUC_UNUSED, gpointer data G_GNUC_UNUSED) {
if (g_str_equal(name, "Category")) return g_variant_new_string("ApplicationStatus");
if (g_str_equal(name, "Id")) return g_variant_new_string("GentDesktop");
if (g_str_equal(name, "Title")) return g_variant_new_string("Gent Desktop");
if (g_str_equal(name, "Status")) return g_variant_new_string("Active");
if (g_str_equal(name, "WindowId")) return g_variant_new_uint32(0);
if (g_str_equal(name, "IconName")) return g_variant_new_string("");
if (g_str_equal(name, "IconPixmap")) return g_variant_ref(pixmaps);
if (g_str_equal(name, "ToolTip"))
return g_variant_new("(s@a(iiay)ss)", "", pixmaps, "Gent Desktop", "");
if (g_str_equal(name, "ItemIsMenu")) return g_variant_new_boolean(FALSE);
if (g_str_equal(name, "Menu")) return g_variant_new_object_path("/Menu");
return NULL;
}
static void method_call(GDBusConnection *bus G_GNUC_UNUSED, const gchar *sender G_GNUC_UNUSED, const gchar *path G_GNUC_UNUSED,
const gchar *iface G_GNUC_UNUSED, const gchar *method, GVariant *args,
GDBusMethodInvocation *call, gpointer data G_GNUC_UNUSED) {
gint x, y;
g_variant_get(args, "(ii)", &x, &y);
gent_tray_anchor(x, y);
g_dbus_method_invocation_return_value(call, NULL);
if (g_str_equal(method, "Activate")) {
activate();
} else {
dismiss_popup();
gent_tray_open_menu();
}
}
static void host_visibility(void) {
g_autoptr(GVariant) host = g_dbus_proxy_get_cached_property(watcher, "IsStatusNotifierHostRegistered");
gent_tray_legacy_visible(!registered || (host && !g_variant_get_boolean(host)));
}
static gboolean register_item(gpointer unused);
static void registered_item(GObject *bus, GAsyncResult *result, gpointer attempt) {
g_autoptr(GVariant) reply = g_dbus_connection_call_finish(G_DBUS_CONNECTION(bus), result, NULL);
if (g_strcmp0(owner, attempt) == 0) {
registered = reply != NULL;
host_visibility();
if (!registered) retry = g_timeout_add_seconds(5, register_item, NULL);
}
g_free(attempt);
}
static gboolean register_item(gpointer unused G_GNUC_UNUSED) {
retry = 0;
if (owner)
g_dbus_connection_call(g_dbus_proxy_get_connection(watcher), owner, "/StatusNotifierWatcher",
WATCHER, "RegisterStatusNotifierItem", g_variant_new("(s)", ITEM_PATH),
G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE, 5000, NULL,
registered_item, g_strdup(owner));
return G_SOURCE_REMOVE;
}
static void watcher_changed(GObject *proxy G_GNUC_UNUSED, GParamSpec *property G_GNUC_UNUSED, gpointer data G_GNUC_UNUSED) {
g_autofree char *current = g_dbus_proxy_get_name_owner(watcher);
if (g_strcmp0(owner, current) == 0) return;
if (retry) { g_source_remove(retry); retry = 0; }
g_free(owner);
owner = g_steal_pointer(¤t);
registered = FALSE;
gent_tray_legacy_visible(TRUE);
register_item(NULL);
}
static void host_properties(GDBusProxy *proxy G_GNUC_UNUSED, GVariant *changed G_GNUC_UNUSED, const gchar *const *invalid G_GNUC_UNUSED, gpointer data G_GNUC_UNUSED) {
host_visibility();
}
static void host_signal(GDBusProxy *proxy G_GNUC_UNUSED, const gchar *sender G_GNUC_UNUSED, const gchar *signal,
GVariant *parameters G_GNUC_UNUSED, gpointer data G_GNUC_UNUSED) {
if (g_str_equal(signal, "StatusNotifierHostRegistered") && registered)
gent_tray_legacy_visible(FALSE);
else if (g_str_equal(signal, "StatusNotifierHostUnregistered")) host_visibility();
}
static void connected(GObject *source G_GNUC_UNUSED, GAsyncResult *result, gpointer data G_GNUC_UNUSED) {
g_autoptr(GError) error = NULL;
watcher = g_dbus_proxy_new_for_bus_finish(result, &error);
if (!watcher) return; static const GDBusInterfaceVTable methods = {.method_call = method_call, .get_property = get_property};
g_autoptr(GDBusNodeInfo) xml = g_dbus_node_info_new_for_xml(item_xml, &error);
if (!xml || !g_dbus_connection_register_object(g_dbus_proxy_get_connection(watcher), ITEM_PATH,
xml->interfaces[0], &methods, NULL, NULL, &error)) {
g_warning("Gent Desktop could not export its StatusNotifier item: %s", error->message);
return;
}
g_signal_connect(watcher, "notify::g-name-owner", G_CALLBACK(watcher_changed), NULL);
g_signal_connect(watcher, "g-properties-changed", G_CALLBACK(host_properties), NULL);
g_signal_connect(watcher, "g-signal", G_CALLBACK(host_signal), NULL);
watcher_changed(G_OBJECT(watcher), NULL, NULL);
}
void gent_sni_start(GdkPixbuf *icon, void (*primary)(void), void (*dismiss)(void)) {
activate = primary;
dismiss_popup = dismiss;
gint width = gdk_pixbuf_get_width(icon), height = gdk_pixbuf_get_height(icon);
gsize size = (gsize)width * height * 4;
guchar *argb = g_malloc(size);
const guchar *rgba = gdk_pixbuf_read_pixels(icon);
for (gsize i = 0; i < size; i += 4) {
argb[i] = rgba[i + 3];
memcpy(argb + i + 1, rgba + i, 3);
}
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a(iiay)"));
GVariant *bytes = g_variant_new_from_data(G_VARIANT_TYPE("ay"), argb, size, TRUE, g_free, argb);
g_variant_builder_add(&builder, "(ii@ay)", width, height, bytes);
pixmaps = g_variant_ref_sink(g_variant_builder_end(&builder));
g_dbus_proxy_new_for_bus(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL,
WATCHER, "/StatusNotifierWatcher", WATCHER, NULL, connected, NULL);
}