WIP sni dbus-menu support.

This commit is contained in:
topisani 2018-10-04 18:03:01 +02:00
parent 0eee8eade7
commit 108b1092e5
8 changed files with 112 additions and 75 deletions

View File

@ -11,7 +11,8 @@ class Item {
~Item(); ~Item();
int icon_size; int icon_size;
int effective_icon_size; int effective_icon_size;
Gtk::Image* image; Gtk::Widget* widget = nullptr;
Gtk::Image* image = nullptr;
std::string category; std::string category;
std::string id; std::string id;
std::string status; std::string status;

View File

@ -32,6 +32,7 @@ wayland_cursor = dependency('wayland-cursor')
wayland_protos = dependency('wayland-protocols') wayland_protos = dependency('wayland-protocols')
wlroots = dependency('wlroots', fallback: ['wlroots', 'wlroots']) wlroots = dependency('wlroots', fallback: ['wlroots', 'wlroots'])
gtkmm = dependency('gtkmm-3.0') gtkmm = dependency('gtkmm-3.0')
dbusmenu_gtk = dependency('dbusmenu-gtk3-0.4')
jsoncpp = dependency('jsoncpp') jsoncpp = dependency('jsoncpp')
sigcpp = dependency('sigc++-2.0') sigcpp = dependency('sigc++-2.0')
libnl = dependency('libnl-3.0', required: false) libnl = dependency('libnl-3.0', required: false)
@ -90,6 +91,7 @@ executable(
libinput, libinput,
wayland_cursor, wayland_cursor,
gtkmm, gtkmm,
dbusmenu_gtk,
libnl, libnl,
libnlgen, libnlgen,
libpulse libpulse

View File

@ -54,10 +54,14 @@ client_protos_headers += gdbus_header.process('./dbus-status-notifier-watcher.xm
client_protos_src += gdbus_code.process('./dbus-status-notifier-item.xml') client_protos_src += gdbus_code.process('./dbus-status-notifier-item.xml')
client_protos_headers += gdbus_header.process('./dbus-status-notifier-item.xml') client_protos_headers += gdbus_header.process('./dbus-status-notifier-item.xml')
client_protos_src += gdbus_code.process('./dbus-menu.xml')
client_protos_headers += gdbus_header.process('./dbus-menu.xml')
lib_client_protos = static_library( lib_client_protos = static_library(
'client_protos', 'client_protos',
client_protos_src + client_protos_headers, client_protos_src + client_protos_headers,
dependencies: [wayland_client, gtkmm] dependencies: [wayland_client, gtkmm],
include_directories: include_directories('..'),
) # for the include directory ) # for the include directory
client_protos = declare_dependency( client_protos = declare_dependency(

View File

@ -142,7 +142,7 @@ int waybar::modules::Network::getExternalInterface()
int ifidx = -1; int ifidx = -1;
/* Prepare request. */ /* Prepare request. */
uint32_t reqlen = NLMSG_SPACE(sizeof(*rt)); constexpr uint32_t reqlen = NLMSG_SPACE(sizeof(*rt));
char req[reqlen] = {0}; char req[reqlen] = {0};
/* Build the RTM_GETROUTE request. */ /* Build the RTM_GETROUTE request. */
@ -228,7 +228,7 @@ int waybar::modules::Network::getExternalInterface()
break; break;
} }
for (uint32_t i = 0; i < dstlen; i += 1) { for (uint32_t i = 0; i < dstlen; i += 1) {
c |= *(unsigned char *)(RTA_DATA(attr) + i); c |= *((unsigned char *)RTA_DATA(attr) + i);
} }
has_destination = (c == 0); has_destination = (c == 0);
break; break;

View File

@ -145,4 +145,4 @@ void waybar::modules::SNI::Host::addRegisteredItem(const gchar* service)
getBusNameAndObjectPath(service, &bus_name, &object_path); getBusNameAndObjectPath(service, &bus_name, &object_path);
items.emplace_back(bus_name, object_path, dp_); items.emplace_back(bus_name, object_path, dp_);
} }

View File

@ -1,29 +1,27 @@
#include "modules/sni/sni.hpp" #include "modules/sni/sni.hpp"
#include <libdbusmenu-gtk/dbusmenu-gtk.h>
#include <iostream> #include <iostream>
waybar::modules::SNI::Item::Item(std::string bus_name, std::string object_path, waybar::modules::SNI::Item::Item(std::string bus_name, std::string object_path,
Glib::Dispatcher& dp) Glib::Dispatcher &dp)
: icon_size(16), effective_icon_size(0), : icon_size(16), effective_icon_size(0),
image(Gtk::manage(new Gtk::Image())), image(Gtk::manage(new Gtk::Image())), bus_name_(bus_name),
bus_name_(bus_name), object_path_(object_path), dp_(dp) object_path_(object_path), dp_(dp) {
{
cancellable_ = g_cancellable_new(); cancellable_ = g_cancellable_new();
sn_org_kde_status_notifier_item_proxy_new_for_bus(G_BUS_TYPE_SESSION, sn_org_kde_status_notifier_item_proxy_new_for_bus(
G_DBUS_PROXY_FLAGS_NONE, bus_name_.c_str(), object_path_.c_str(), G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, bus_name_.c_str(),
cancellable_, &Item::proxyReady, this); object_path_.c_str(), cancellable_, &Item::proxyReady, this);
} }
waybar::modules::SNI::Item::~Item() waybar::modules::SNI::Item::~Item() {}
{
}
void waybar::modules::SNI::Item::proxyReady(GObject* obj, GAsyncResult* res, void waybar::modules::SNI::Item::proxyReady(GObject *obj, GAsyncResult *res,
gpointer data) gpointer data) {
{ GError *error = nullptr;
GError* error = nullptr; SnOrgKdeStatusNotifierItem *proxy =
SnOrgKdeStatusNotifierItem* proxy = sn_org_kde_status_notifier_item_proxy_new_for_bus_finish(res, &error);
sn_org_kde_status_notifier_item_proxy_new_for_bus_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)) {
g_error_free(error); g_error_free(error);
return; return;
@ -37,18 +35,24 @@ void waybar::modules::SNI::Item::proxyReady(GObject* obj, GAsyncResult* res,
} }
auto conn = g_dbus_proxy_get_connection(G_DBUS_PROXY(proxy)); auto conn = g_dbus_proxy_get_connection(G_DBUS_PROXY(proxy));
g_dbus_connection_call(conn, item->bus_name_.c_str(), g_dbus_connection_call(conn, item->bus_name_.c_str(),
item->object_path_.c_str(), "org.freedesktop.DBus.Properties", "GetAll", item->object_path_.c_str(),
g_variant_new("(s)", "org.kde.StatusNotifierItem"), "org.freedesktop.DBus.Properties", "GetAll",
G_VARIANT_TYPE("(a{sv})"), G_DBUS_CALL_FLAGS_NONE, -1, g_variant_new("(s)", "org.kde.StatusNotifierItem"),
item->cancellable_, &Item::getAll, data); 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, static auto nonull(const char *c) {
gpointer data) if (c != nullptr)
{ return c;
GError* error = nullptr; return "";
}
void waybar::modules::SNI::Item::getAll(GObject *obj, GAsyncResult *res,
gpointer data) {
GError *error = nullptr;
auto conn = G_DBUS_CONNECTION(obj); auto conn = G_DBUS_CONNECTION(obj);
GVariant* properties = g_dbus_connection_call_finish(conn, res, &error); GVariant *properties = g_dbus_connection_call_finish(conn, 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)) {
g_error_free(error); g_error_free(error);
return; return;
@ -59,41 +63,41 @@ void waybar::modules::SNI::Item::getAll(GObject* obj, GAsyncResult* res,
g_error_free(error); g_error_free(error);
return; return;
} }
GVariantIter* it = nullptr; GVariantIter *it = nullptr;
g_variant_get(properties, "(a{sv})", &it); g_variant_get(properties, "(a{sv})", &it);
gchar* key; gchar *key;
GVariant* value; GVariant *value;
while (g_variant_iter_next(it, "{sv}", &key, &value)) { while (g_variant_iter_next(it, "{sv}", &key, &value)) {
if (g_strcmp0(key, "Category") == 0) { if (g_strcmp0(key, "Category") == 0) {
item->category = g_variant_dup_string(value, nullptr); item->category = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "Id") == 0) { } else if (g_strcmp0(key, "Id") == 0) {
item->id = g_variant_dup_string(value, nullptr); item->id = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "Title") == 0) { } else if (g_strcmp0(key, "Title") == 0) {
item->title = g_variant_dup_string(value, nullptr); item->title = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "Status") == 0) { } else if (g_strcmp0(key, "Status") == 0) {
item->status = g_variant_dup_string(value, nullptr); item->status = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "WindowId") == 0) { } else if (g_strcmp0(key, "WindowId") == 0) {
item->window_id = g_variant_get_int32 (value); item->window_id = g_variant_get_int32(value);
} else if (g_strcmp0(key, "IconName") == 0) { } else if (g_strcmp0(key, "IconName") == 0) {
item->icon_name = g_variant_dup_string(value, nullptr); item->icon_name = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "IconPixmap") == 0) { } else if (g_strcmp0(key, "IconPixmap") == 0) {
// TODO: icon pixmap // TODO: icon pixmap
} else if (g_strcmp0(key, "OverlayIconName") == 0) { } else if (g_strcmp0(key, "OverlayIconName") == 0) {
item->overlay_icon_name = g_variant_dup_string(value, nullptr); item->overlay_icon_name = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "OverlayIconPixmap") == 0) { } else if (g_strcmp0(key, "OverlayIconPixmap") == 0) {
// TODO: overlay_icon_pixmap // TODO: overlay_icon_pixmap
} else if (g_strcmp0(key, "AttentionIconName") == 0) { } else if (g_strcmp0(key, "AttentionIconName") == 0) {
item->attention_icon_name = g_variant_dup_string(value, nullptr); item->attention_icon_name = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "AttentionIconPixmap") == 0) { } else if (g_strcmp0(key, "AttentionIconPixmap") == 0) {
// TODO: attention_icon_pixmap // TODO: attention_icon_pixmap
} else if (g_strcmp0(key, "AttentionMovieName") == 0) { } else if (g_strcmp0(key, "AttentionMovieName") == 0) {
item->attention_movie_name = g_variant_dup_string(value, nullptr); item->attention_movie_name = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "ToolTip") == 0) { } else if (g_strcmp0(key, "ToolTip") == 0) {
// TODO: tooltip // TODO: tooltip
} else if (g_strcmp0(key, "IconThemePath") == 0) { } else if (g_strcmp0(key, "IconThemePath") == 0) {
item->icon_theme_path = g_variant_dup_string(value, nullptr); item->icon_theme_path = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "Menu") == 0) { } else if (g_strcmp0(key, "Menu") == 0) {
item->menu = g_variant_dup_string(value, nullptr); item->menu = nonull(g_variant_dup_string(value, nullptr));
} else if (g_strcmp0(key, "ItemIsMenu") == 0) { } else if (g_strcmp0(key, "ItemIsMenu") == 0) {
item->item_is_menu = g_variant_get_boolean(value); item->item_is_menu = g_variant_get_boolean(value);
} }
@ -103,31 +107,37 @@ void waybar::modules::SNI::Item::getAll(GObject* obj, GAsyncResult* res,
g_variant_iter_free(it); g_variant_iter_free(it);
g_variant_unref(properties); g_variant_unref(properties);
if (item->id.empty() || item->category.empty() || item->status.empty()) { if (item->id.empty() || item->category.empty() || item->status.empty()) {
std::cerr << "Invalid Status Notifier Item: " + item->bus_name_ + "," std::cerr << "Invalid Status Notifier Item: " + item->bus_name_ + "," +
+ item->object_path_ << std::endl; item->object_path_
<< std::endl;
return; return;
} }
if (!item->icon_theme_path.empty()) { if (!item->icon_theme_path.empty()) {
GtkIconTheme* icon_theme = gtk_icon_theme_get_default(); GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
gtk_icon_theme_append_search_path(icon_theme, gtk_icon_theme_append_search_path(icon_theme,
item->icon_theme_path.c_str()); item->icon_theme_path.c_str());
} }
item->updateImage(); item->updateImage();
item->dp_.emit(); item->dp_.emit();
// TODO: handle change // TODO: handle change
} }
void waybar::modules::SNI::Item::updateImage() void waybar::modules::SNI::Item::updateImage() {
{
if (!icon_name.empty()) { if (!icon_name.empty()) {
auto pixbuf = getIconByName(icon_name, icon_size); auto pixbuf = getIconByName(icon_name, icon_size);
if (pixbuf->gobj() == nullptr) { if (pixbuf->gobj() == nullptr) {
// Try to find icons specified by path and filename // Try to find icons specified by path and filename
pixbuf = Gdk::Pixbuf::create_from_file(icon_name); try {
if (pixbuf->gobj() != nullptr) { pixbuf = Gdk::Pixbuf::create_from_file(icon_name);
// An icon specified by path and filename may be the wrong size for the tray if (pixbuf->gobj() != nullptr) {
pixbuf->scale_simple(icon_size - 2, icon_size - 2, // An icon specified by path and filename may be the wrong size for
Gdk::InterpType::INTERP_BILINEAR); // 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);
} }
} }
if (pixbuf->gobj() == nullptr) { if (pixbuf->gobj() == nullptr) {
@ -138,14 +148,37 @@ 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);
} }
auto *evt_box = Gtk::manage(new Gtk::EventBox());
evt_box->add(*image);
widget = evt_box;
if (!menu.empty()) {
auto *dbmenu = dbusmenu_gtkmenu_new(bus_name_.data(), menu.data());
Gtk::Menu *dmenu = Glib::wrap(GTK_MENU(dbmenu), false);
if (dbmenu && dmenu) {
widget->signal_button_press_event().connect(
[this, dmenu](GdkEventButton *btn) {
if (!dmenu->get_parent()) {
dmenu->reparent(*image);
}
if (!dmenu->get_attach_widget()) {
dmenu->attach_to_widget(*widget);
}
dmenu->popup(btn->button, btn->time);
return true;
});
}
} else {
widget->signal_button_press_event().connect([this](GdkEventButton *btn) {
std::cout << this->menu << std::endl;
return true;
});
}
} }
Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::getIconByName( Glib::RefPtr<Gdk::Pixbuf>
std::string name, int request_size) waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
{
int icon_size = 0; int icon_size = 0;
Glib::RefPtr<Gtk::IconTheme> icon_theme = Glib::RefPtr<Gtk::IconTheme> icon_theme = Gtk::IconTheme::get_default();
Gtk::IconTheme::get_default();
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 size : sizes) { for (auto size : sizes) {
@ -161,5 +194,5 @@ Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::getIconByName(
icon_size = request_size; icon_size = request_size;
} }
return icon_theme->load_icon(name.c_str(), icon_size, return icon_theme->load_icon(name.c_str(), icon_size,
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE); Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
} }

View File

@ -166,4 +166,4 @@ void waybar::modules::SNI::Watcher::updateRegisteredItems(
obj, items); obj, items);
g_variant_unref(variant); g_variant_unref(variant);
g_free(items); g_free(items);
} }

View File

@ -2,16 +2,15 @@
#include <iostream> #include <iostream>
waybar::modules::SNI::Tray::Tray(const Json::Value& config) waybar::modules::SNI::Tray::Tray(const Json::Value &config)
: config_(config), watcher_(), host_(dp) : config_(config), watcher_(), host_(dp) {}
{
}
auto waybar::modules::SNI::Tray::update() -> void auto waybar::modules::SNI::Tray::update() -> void {
{
for (auto item : host_.items) { for (auto item : host_.items) {
item.image->set_tooltip_text(item.title); if (item.widget) {
box_.pack_start(*item.image); item.widget->set_tooltip_text(item.title);
box_.pack_start(*item.widget);
}
} }
if (box_.get_children().size() > 0) { if (box_.get_children().size() > 0) {
box_.set_name("tray"); box_.set_name("tray");
@ -21,6 +20,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_;
}