2018-08-29 18:36:39 +00:00
|
|
|
#include "modules/sni/sni.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
2018-10-04 16:47:06 +00:00
|
|
|
#include <libdbusmenu-gtk/dbusmenu-gtk.h>
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2018-09-04 21:50:08 +00:00
|
|
|
waybar::modules::SNI::Item::Item(std::string bn, std::string op,
|
2018-10-04 16:47:06 +00:00
|
|
|
Glib::Dispatcher *dp)
|
|
|
|
: bus_name(bn), object_path(op), event_box(), icon_size(16),
|
|
|
|
effective_icon_size(0), image(Gtk::manage(new Gtk::Image())), dp_(dp) {
|
2018-09-04 21:50:08 +00:00
|
|
|
event_box.add(*image);
|
2018-09-17 21:32:05 +00:00
|
|
|
event_box.add_events(Gdk::BUTTON_PRESS_MASK);
|
2018-10-04 16:47:06 +00:00
|
|
|
event_box.signal_button_press_event().connect(
|
|
|
|
sigc::mem_fun(*this, &Item::handleClick));
|
2018-08-29 18:36:39 +00:00
|
|
|
cancellable_ = g_cancellable_new();
|
2018-10-04 16:03:01 +00:00
|
|
|
sn_org_kde_status_notifier_item_proxy_new_for_bus(
|
2018-10-04 16:47:06 +00:00
|
|
|
G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, bus_name.c_str(),
|
|
|
|
object_path.c_str(), cancellable_, &Item::proxyReady, this);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 16:03:01 +00:00
|
|
|
void waybar::modules::SNI::Item::proxyReady(GObject *obj, GAsyncResult *res,
|
|
|
|
gpointer data) {
|
|
|
|
GError *error = nullptr;
|
|
|
|
SnOrgKdeStatusNotifierItem *proxy =
|
|
|
|
sn_org_kde_status_notifier_item_proxy_new_for_bus_finish(res, &error);
|
2018-08-29 18:36:39 +00:00
|
|
|
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto item = static_cast<SNI::Item *>(data);
|
|
|
|
item->proxy_ = proxy;
|
|
|
|
if (error) {
|
|
|
|
std::cerr << error->message << std::endl;
|
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto conn = g_dbus_proxy_get_connection(G_DBUS_PROXY(proxy));
|
2018-10-04 16:47:06 +00:00
|
|
|
|
2018-09-04 21:50:08 +00:00
|
|
|
g_dbus_connection_call(conn, item->bus_name.c_str(),
|
2018-10-04 16:47:06 +00:00
|
|
|
item->object_path.c_str(),
|
2018-10-04 16:03:01 +00:00
|
|
|
"org.freedesktop.DBus.Properties", "GetAll",
|
|
|
|
g_variant_new("(s)", "org.kde.StatusNotifierItem"),
|
|
|
|
G_VARIANT_TYPE("(a{sv})"), G_DBUS_CALL_FLAGS_NONE, -1,
|
|
|
|
item->cancellable_, &Item::getAll, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::SNI::Item::getAll(GObject *obj, GAsyncResult *res,
|
|
|
|
gpointer data) {
|
|
|
|
GError *error = nullptr;
|
2018-08-29 18:36:39 +00:00
|
|
|
auto conn = G_DBUS_CONNECTION(obj);
|
2018-10-04 16:03:01 +00:00
|
|
|
GVariant *properties = g_dbus_connection_call_finish(conn, res, &error);
|
2018-08-29 18:36:39 +00:00
|
|
|
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto item = static_cast<SNI::Item *>(data);
|
|
|
|
if (error) {
|
|
|
|
std::cerr << error->message << std::endl;
|
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-04 16:03:01 +00:00
|
|
|
GVariantIter *it = nullptr;
|
2018-08-29 18:36:39 +00:00
|
|
|
g_variant_get(properties, "(a{sv})", &it);
|
2018-10-04 16:03:01 +00:00
|
|
|
gchar *key;
|
|
|
|
GVariant *value;
|
2018-08-29 18:36:39 +00:00
|
|
|
while (g_variant_iter_next(it, "{sv}", &key, &value)) {
|
|
|
|
if (g_strcmp0(key, "Category") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->category = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "Id") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->id = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "Title") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->title = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "Status") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->status = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "WindowId") == 0) {
|
2018-10-04 16:03:01 +00:00
|
|
|
item->window_id = g_variant_get_int32(value);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "IconName") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->icon_name = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "IconPixmap") == 0) {
|
2018-09-09 18:46:26 +00:00
|
|
|
item->icon_pixmap = item->extractPixBuf(value);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "OverlayIconName") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->overlay_icon_name = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "OverlayIconPixmap") == 0) {
|
|
|
|
// TODO: overlay_icon_pixmap
|
|
|
|
} else if (g_strcmp0(key, "AttentionIconName") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->attention_icon_name = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "AttentionIconPixmap") == 0) {
|
|
|
|
// TODO: attention_icon_pixmap
|
|
|
|
} else if (g_strcmp0(key, "AttentionMovieName") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->attention_movie_name = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "ToolTip") == 0) {
|
|
|
|
// TODO: tooltip
|
|
|
|
} else if (g_strcmp0(key, "IconThemePath") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->icon_theme_path = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "Menu") == 0) {
|
2018-08-29 18:36:39 +00:00
|
|
|
item->menu = g_variant_dup_string(value, nullptr);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else if (g_strcmp0(key, "ItemIsMenu") == 0) {
|
|
|
|
item->item_is_menu = g_variant_get_boolean(value);
|
|
|
|
}
|
|
|
|
g_variant_unref(value);
|
|
|
|
g_free(key);
|
|
|
|
}
|
|
|
|
g_variant_iter_free(it);
|
|
|
|
g_variant_unref(properties);
|
|
|
|
if (item->id.empty() || item->category.empty() || item->status.empty()) {
|
2018-10-04 16:47:06 +00:00
|
|
|
std::cerr << "Invalid Status Notifier Item: " + item->bus_name + "," +
|
|
|
|
item->object_path
|
2018-10-04 16:03:01 +00:00
|
|
|
<< std::endl;
|
2018-08-29 18:36:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!item->icon_theme_path.empty()) {
|
2018-10-04 16:03:01 +00:00
|
|
|
GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
|
2018-08-29 18:36:39 +00:00
|
|
|
gtk_icon_theme_append_search_path(icon_theme,
|
2018-10-04 16:03:01 +00:00
|
|
|
item->icon_theme_path.c_str());
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
item->updateImage();
|
2018-09-04 21:50:08 +00:00
|
|
|
item->dp_->emit();
|
2018-08-29 18:36:39 +00:00
|
|
|
// TODO: handle change
|
|
|
|
}
|
|
|
|
|
2018-10-04 16:47:06 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf>
|
|
|
|
waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
|
|
|
|
GVariantIter *it;
|
2018-09-09 18:46:26 +00:00
|
|
|
g_variant_get(variant, "a(iiay)", &it);
|
|
|
|
if (it == nullptr) {
|
|
|
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
|
|
|
}
|
2018-10-04 16:47:06 +00:00
|
|
|
GVariant *val;
|
2018-09-09 18:46:26 +00:00
|
|
|
gint lwidth = 0;
|
|
|
|
gint lheight = 0;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
2018-10-04 16:47:06 +00:00
|
|
|
guchar *array = nullptr;
|
2018-09-09 18:46:26 +00:00
|
|
|
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
|
2018-10-04 16:47:06 +00:00
|
|
|
if (width > 0 && height > 0 && val != nullptr &&
|
|
|
|
width * height > lwidth * lheight) {
|
|
|
|
auto size = g_variant_get_size(val);
|
|
|
|
/* Sanity check */
|
|
|
|
if (size == 4U * width * height) {
|
|
|
|
/* Find the largest image */
|
|
|
|
gconstpointer data = g_variant_get_data(val);
|
|
|
|
if (data != nullptr) {
|
|
|
|
if (array != nullptr) {
|
|
|
|
g_free(array);
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
2018-10-04 16:47:06 +00:00
|
|
|
array = static_cast<guchar *>(g_memdup(data, size));
|
|
|
|
lwidth = width;
|
|
|
|
lheight = height;
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
g_variant_iter_free(it);
|
|
|
|
if (array != nullptr) {
|
|
|
|
/* argb to rgba */
|
|
|
|
for (uint32_t i = 0; i < 4U * lwidth * lheight; i += 4) {
|
|
|
|
guchar alpha = array[i];
|
|
|
|
array[i] = array[i + 1];
|
|
|
|
array[i + 1] = array[i + 2];
|
|
|
|
array[i + 2] = array[i + 3];
|
|
|
|
array[i + 3] = alpha;
|
|
|
|
}
|
|
|
|
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB,
|
2018-10-04 16:47:06 +00:00
|
|
|
true, 8, lwidth, lheight, 4 * lwidth);
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:36:39 +00:00
|
|
|
void waybar::modules::SNI::Item::updateImage()
|
|
|
|
{
|
2018-08-29 18:36:39 +00:00
|
|
|
if (!icon_name.empty()) {
|
|
|
|
auto pixbuf = getIconByName(icon_name, icon_size);
|
|
|
|
if (pixbuf->gobj() == nullptr) {
|
|
|
|
// Try to find icons specified by path and filename
|
2018-10-04 16:03:01 +00:00
|
|
|
try {
|
|
|
|
pixbuf = Gdk::Pixbuf::create_from_file(icon_name);
|
|
|
|
if (pixbuf->gobj() != nullptr) {
|
|
|
|
// An icon specified by path and filename may be the wrong size for
|
|
|
|
// the tray
|
|
|
|
pixbuf->scale_simple(icon_size - 2, icon_size - 2,
|
|
|
|
Gdk::InterpType::INTERP_BILINEAR);
|
|
|
|
}
|
|
|
|
} catch (Glib::Error &e) {
|
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
|
|
|
pixbuf = getIconByName("image-missing", icon_size);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pixbuf->gobj() == nullptr) {
|
|
|
|
pixbuf = getIconByName("image-missing", icon_size);
|
|
|
|
}
|
|
|
|
image->set(pixbuf);
|
2018-09-09 18:46:26 +00:00
|
|
|
} else if (icon_pixmap) {
|
|
|
|
image->set(icon_pixmap);
|
2018-08-29 18:36:39 +00:00
|
|
|
} else {
|
|
|
|
image->set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
|
|
|
|
image->set_pixel_size(icon_size);
|
|
|
|
}
|
2018-10-04 16:03:01 +00:00
|
|
|
if (!menu.empty()) {
|
2018-10-04 16:47:06 +00:00
|
|
|
auto *dbmenu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data());
|
|
|
|
if (dbmenu)
|
|
|
|
gtk_menu = Glib::wrap(GTK_MENU(dbmenu), false);
|
2018-10-04 16:03:01 +00:00
|
|
|
}
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 16:03:01 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf>
|
|
|
|
waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
|
2018-08-29 18:36:39 +00:00
|
|
|
int icon_size = 0;
|
2018-10-04 16:03:01 +00:00
|
|
|
Glib::RefPtr<Gtk::IconTheme> icon_theme = Gtk::IconTheme::get_default();
|
2018-08-29 18:36:39 +00:00
|
|
|
icon_theme->rescan_if_needed();
|
|
|
|
auto sizes = icon_theme->get_icon_sizes(name.c_str());
|
2018-10-04 16:47:06 +00:00
|
|
|
|
|
|
|
for (auto const &size : sizes) {
|
2018-08-29 18:36:39 +00:00
|
|
|
// -1 == scalable
|
|
|
|
if (size == request_size || size == -1) {
|
|
|
|
icon_size = request_size;
|
|
|
|
break;
|
|
|
|
} else if (size < request_size || size > icon_size) {
|
|
|
|
icon_size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (icon_size == 0) {
|
|
|
|
icon_size = request_size;
|
|
|
|
}
|
|
|
|
return icon_theme->load_icon(name.c_str(), icon_size,
|
2018-10-04 16:03:01 +00:00
|
|
|
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
|
|
|
|
}
|
2018-09-17 21:32:05 +00:00
|
|
|
|
2018-10-04 16:47:06 +00:00
|
|
|
void waybar::modules::SNI::Item::handleActivate(GObject *src, GAsyncResult *res,
|
|
|
|
gpointer data) {
|
2018-09-17 21:32:05 +00:00
|
|
|
auto item = static_cast<SNI::Item *>(data);
|
|
|
|
sn_org_kde_status_notifier_item_call_activate_finish(item->proxy_, res,
|
2018-10-04 16:47:06 +00:00
|
|
|
nullptr);
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 16:47:06 +00:00
|
|
|
void waybar::modules::SNI::Item::handleSecondaryActivate(GObject *src,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer data) {
|
2018-09-17 21:32:05 +00:00
|
|
|
auto item = static_cast<SNI::Item *>(data);
|
|
|
|
sn_org_kde_status_notifier_item_call_secondary_activate_finish(item->proxy_,
|
2018-10-04 16:47:06 +00:00
|
|
|
res, nullptr);
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 16:47:06 +00:00
|
|
|
bool waybar::modules::SNI::Item::handleClick(GdkEventButton *const &ev) {
|
2018-09-17 21:32:05 +00:00
|
|
|
if (ev->type == GDK_BUTTON_PRESS) {
|
2018-10-04 16:47:06 +00:00
|
|
|
if (gtk_menu) {
|
|
|
|
if (!gtk_menu->get_attach_widget()) {
|
|
|
|
gtk_menu->attach_to_widget(event_box);
|
|
|
|
}
|
|
|
|
gtk_menu->popup(ev->button, ev->time);
|
|
|
|
} else {
|
|
|
|
sn_org_kde_status_notifier_item_call_activate(
|
|
|
|
proxy_, ev->x, ev->y, nullptr, &Item::handleActivate, this);
|
|
|
|
}
|
2018-09-17 21:32:05 +00:00
|
|
|
} else if (ev->type == GDK_2BUTTON_PRESS) {
|
2018-10-04 16:47:06 +00:00
|
|
|
sn_org_kde_status_notifier_item_call_secondary_activate(
|
|
|
|
proxy_, ev->x, ev->y, nullptr, &Item::handleSecondaryActivate, this);
|
2018-09-17 21:32:05 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|