2018-11-22 14:47:23 +00:00
|
|
|
#include "modules/sni/item.hpp"
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2019-03-30 01:40:28 +00:00
|
|
|
#include <glibmm/main.h>
|
2019-04-17 12:19:04 +00:00
|
|
|
#include <iostream>
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
using namespace Glib;
|
|
|
|
|
|
|
|
static const ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name;
|
2019-03-30 01:40:28 +00:00
|
|
|
static const unsigned UPDATE_DEBOUNCE_TIME = 10;
|
2019-03-28 02:56:19 +00:00
|
|
|
|
2018-11-22 14:47:23 +00:00
|
|
|
waybar::modules::SNI::Item::Item(std::string bn, std::string op, const Json::Value& config)
|
2019-04-17 12:19:04 +00:00
|
|
|
: bus_name(bn),
|
|
|
|
object_path(op),
|
|
|
|
icon_size(16),
|
|
|
|
effective_icon_size(0),
|
|
|
|
icon_theme(Gtk::IconTheme::create()),
|
|
|
|
update_pending_(false) {
|
2018-11-22 14:47:23 +00:00
|
|
|
if (config["icon-size"].isUInt()) {
|
|
|
|
icon_size = config["icon-size"].asUInt();
|
2018-10-26 12:53:39 +00:00
|
|
|
}
|
2019-04-11 13:28:38 +00:00
|
|
|
default_icon_path_ = Gtk::IconTheme::get_default()->get_search_path();
|
2018-11-22 14:47:23 +00:00
|
|
|
event_box.add(image);
|
|
|
|
event_box.add_events(Gdk::BUTTON_PRESS_MASK);
|
2019-04-17 12:19:04 +00:00
|
|
|
event_box.signal_button_press_event().connect(sigc::mem_fun(*this, &Item::handleClick));
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
cancellable_ = Gio::Cancellable::create();
|
2018-10-04 16:47:06 +00:00
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
auto interface = Glib::wrap(sn_item_interface_info(), true);
|
2019-04-17 12:19:04 +00:00
|
|
|
Gio::DBus::Proxy::create_for_bus(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name, object_path,
|
|
|
|
SNI_INTERFACE_NAME, sigc::mem_fun(*this, &Item::proxyReady),
|
|
|
|
cancellable_, interface);
|
2018-10-04 16:03:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
|
|
|
|
try {
|
|
|
|
this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result);
|
|
|
|
/* Properties are already cached during object creation */
|
|
|
|
auto cached_properties = this->proxy_->get_cached_property_names();
|
2019-04-17 12:19:04 +00:00
|
|
|
for (const auto& name : cached_properties) {
|
2019-03-28 02:56:19 +00:00
|
|
|
Glib::VariantBase value;
|
|
|
|
this->proxy_->get_cached_property(value, name);
|
|
|
|
setProperty(name, value);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2019-03-28 02:56:19 +00:00
|
|
|
|
2019-03-30 01:40:28 +00:00
|
|
|
this->proxy_->signal_signal().connect(sigc::mem_fun(*this, &Item::onSignal));
|
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
if (this->id.empty() || this->category.empty() || this->status.empty()) {
|
2019-04-17 12:19:04 +00:00
|
|
|
std::cerr << "Invalid Status Notifier Item: " + this->bus_name + "," + this->object_path
|
|
|
|
<< std::endl;
|
2019-03-28 02:56:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->updateImage();
|
|
|
|
// this->event_box.set_tooltip_text(this->title);
|
|
|
|
|
|
|
|
} catch (const Glib::Error& err) {
|
2019-04-17 12:19:04 +00:00
|
|
|
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(), object_path.c_str(),
|
|
|
|
err.what().c_str());
|
2019-03-28 02:56:19 +00:00
|
|
|
} catch (const std::exception& err) {
|
2019-04-17 12:19:04 +00:00
|
|
|
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(), object_path.c_str(),
|
|
|
|
err.what());
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2019-03-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
template <typename T>
|
2019-03-28 02:56:19 +00:00
|
|
|
T get_variant(VariantBase& value) {
|
2019-04-17 12:19:04 +00:00
|
|
|
return VariantBase::cast_dynamic<Variant<T>>(value).get();
|
2019-03-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void waybar::modules::SNI::Item::setProperty(const ustring& name, VariantBase& value) {
|
2019-03-28 02:56:19 +00:00
|
|
|
if (name == "Category") {
|
|
|
|
category = get_variant<std::string>(value);
|
|
|
|
} else if (name == "Id") {
|
|
|
|
id = get_variant<std::string>(value);
|
|
|
|
} else if (name == "Title") {
|
|
|
|
title = get_variant<std::string>(value);
|
|
|
|
} else if (name == "Status") {
|
|
|
|
status = get_variant<std::string>(value);
|
|
|
|
} else if (name == "WindowId") {
|
|
|
|
window_id = get_variant<int32_t>(value);
|
|
|
|
} else if (name == "IconName") {
|
|
|
|
icon_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "IconPixmap") {
|
|
|
|
icon_pixmap = this->extractPixBuf(value.gobj());
|
|
|
|
} else if (name == "OverlayIconName") {
|
|
|
|
overlay_icon_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "OverlayIconPixmap") {
|
|
|
|
// TODO: overlay_icon_pixmap
|
|
|
|
} else if (name == "AttentionIconName") {
|
|
|
|
attention_icon_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "AttentionIconPixmap") {
|
|
|
|
// TODO: attention_icon_pixmap
|
|
|
|
} else if (name == "AttentionMovieName") {
|
|
|
|
attention_movie_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "ToolTip") {
|
|
|
|
// TODO: tooltip
|
|
|
|
} else if (name == "IconThemePath") {
|
|
|
|
icon_theme_path = get_variant<std::string>(value);
|
2019-04-11 13:20:39 +00:00
|
|
|
if (!icon_theme_path.empty()) {
|
2019-04-11 13:28:38 +00:00
|
|
|
std::vector<Glib::ustring> paths(default_icon_path_);
|
|
|
|
paths.push_back(icon_theme_path);
|
|
|
|
icon_theme->set_search_path(paths);
|
2019-04-11 13:20:39 +00:00
|
|
|
}
|
2019-03-28 02:56:19 +00:00
|
|
|
} else if (name == "Menu") {
|
|
|
|
menu = get_variant<std::string>(value);
|
|
|
|
} else if (name == "ItemIsMenu") {
|
|
|
|
item_is_menu = get_variant<bool>(value);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void waybar::modules::SNI::Item::getUpdatedProperties() {
|
2019-03-30 01:40:28 +00:00
|
|
|
update_pending_ = false;
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
auto params = VariantContainerBase::create_tuple({Variant<ustring>::create(SNI_INTERFACE_NAME)});
|
2019-03-30 01:40:28 +00:00
|
|
|
proxy_->call("org.freedesktop.DBus.Properties.GetAll",
|
2019-04-17 12:19:04 +00:00
|
|
|
sigc::mem_fun(*this, &Item::processUpdatedProperties), params);
|
2019-03-30 01:40:28 +00:00
|
|
|
};
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void waybar::modules::SNI::Item::processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& _result) {
|
2019-03-30 01:40:28 +00:00
|
|
|
try {
|
|
|
|
auto result = proxy_->call_finish(_result);
|
|
|
|
// extract "a{sv}" from VariantContainerBase
|
|
|
|
Variant<std::map<ustring, VariantBase>> properties_variant;
|
|
|
|
result.get_child(properties_variant);
|
|
|
|
auto properties = properties_variant.get();
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
for (const auto& [name, value] : properties) {
|
2019-03-30 01:40:28 +00:00
|
|
|
VariantBase old_value;
|
|
|
|
proxy_->get_cached_property(old_value, name);
|
|
|
|
if (!value.equal(old_value)) {
|
|
|
|
proxy_->set_cached_property(name, value);
|
|
|
|
setProperty(name, const_cast<VariantBase&>(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->updateImage();
|
|
|
|
// this->event_box.set_tooltip_text(this->title);
|
|
|
|
} catch (const Glib::Error& err) {
|
|
|
|
g_warning("Failed to update properties: %s", err.what().c_str());
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
g_warning("Failed to update properties: %s", err.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void waybar::modules::SNI::Item::onSignal(const ustring& sender_name, const ustring& signal_name,
|
|
|
|
const VariantContainerBase& arguments) {
|
2019-03-30 01:40:28 +00:00
|
|
|
if (!update_pending_ && signal_name.compare(0, 3, "New") == 0) {
|
|
|
|
/* Debounce signals and schedule update of all properties.
|
|
|
|
* Based on behavior of Plasma dataengine for StatusNotifierItem.
|
|
|
|
*/
|
|
|
|
update_pending_ = true;
|
2019-04-17 12:19:04 +00:00
|
|
|
Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &Item::getUpdatedProperties),
|
|
|
|
UPDATE_DEBOUNCE_TIME);
|
2019-03-30 01:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
static void pixbuf_data_deleter(const guint8* data) { g_free((void*)data); }
|
2019-03-30 01:40:28 +00:00
|
|
|
|
2019-04-17 12:19:04 +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>{};
|
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
GVariant* val;
|
2018-09-09 18:46:26 +00:00
|
|
|
gint lwidth = 0;
|
|
|
|
gint lheight = 0;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
2019-04-17 12:19:04 +00:00
|
|
|
guchar* array = nullptr;
|
2018-09-09 18:46:26 +00:00
|
|
|
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
|
2019-04-17 12:19:04 +00:00
|
|
|
if (width > 0 && height > 0 && val != nullptr && width * height > lwidth * lheight) {
|
2018-10-04 16:47:06 +00:00
|
|
|
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
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
array = static_cast<guchar*>(g_memdup(data, size));
|
2018-10-04 16:47:06 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB, true, 8, lwidth,
|
|
|
|
lheight, 4 * lwidth, &pixbuf_data_deleter);
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void waybar::modules::SNI::Item::updateImage() {
|
2018-11-22 14:47:23 +00:00
|
|
|
image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
|
|
|
|
image.set_pixel_size(icon_size);
|
2018-08-29 18:36:39 +00:00
|
|
|
if (!icon_name.empty()) {
|
2018-10-26 09:59:03 +00:00
|
|
|
try {
|
2018-08-29 18:36:39 +00:00
|
|
|
// Try to find icons specified by path and filename
|
2018-11-02 19:59:41 +00:00
|
|
|
#ifdef FILESYSTEM_EXPERIMENTAL
|
|
|
|
if (std::experimental::filesystem::exists(icon_name)) {
|
|
|
|
#else
|
2018-10-26 09:59:03 +00:00
|
|
|
if (std::filesystem::exists(icon_name)) {
|
2018-11-02 19:59:41 +00:00
|
|
|
#endif
|
2018-10-26 09:59:03 +00:00
|
|
|
auto pixbuf = Gdk::Pixbuf::create_from_file(icon_name);
|
2018-10-04 16:03:01 +00:00
|
|
|
if (pixbuf->gobj() != nullptr) {
|
|
|
|
// An icon specified by path and filename may be the wrong size for
|
|
|
|
// the tray
|
2019-04-17 12:19:04 +00:00
|
|
|
pixbuf = pixbuf->scale_simple(icon_size, icon_size, Gdk::InterpType::INTERP_BILINEAR);
|
2018-11-22 14:47:23 +00:00
|
|
|
image.set(pixbuf);
|
2018-10-04 16:03:01 +00:00
|
|
|
}
|
2018-10-26 09:59:03 +00:00
|
|
|
} else {
|
2018-11-22 14:47:23 +00:00
|
|
|
image.set(getIconByName(icon_name, icon_size));
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
} catch (Glib::Error& e) {
|
2018-10-26 09:59:03 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2018-09-09 18:46:26 +00:00
|
|
|
} else if (icon_pixmap) {
|
2018-10-26 12:53:39 +00:00
|
|
|
// An icon extracted may be the wrong size for the tray
|
2019-04-17 12:19:04 +00:00
|
|
|
icon_pixmap = icon_pixmap->scale_simple(icon_size, icon_size, Gdk::InterpType::INTERP_BILINEAR);
|
2018-11-22 14:47:23 +00:00
|
|
|
image.set(icon_pixmap);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::getIconByName(std::string name,
|
|
|
|
int request_size) {
|
2018-10-26 12:53:39 +00:00
|
|
|
int tmp_size = 0;
|
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
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
for (auto const& size : sizes) {
|
2018-08-29 18:36:39 +00:00
|
|
|
// -1 == scalable
|
|
|
|
if (size == request_size || size == -1) {
|
2018-10-26 12:53:39 +00:00
|
|
|
tmp_size = request_size;
|
2018-08-29 18:36:39 +00:00
|
|
|
break;
|
2019-04-15 10:10:37 +00:00
|
|
|
} else if (size < request_size) {
|
2018-10-26 12:53:39 +00:00
|
|
|
tmp_size = size;
|
2019-04-15 10:10:37 +00:00
|
|
|
} else if (size > tmp_size && tmp_size > 0) {
|
|
|
|
tmp_size = request_size;
|
|
|
|
break;
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-26 12:53:39 +00:00
|
|
|
if (tmp_size == 0) {
|
|
|
|
tmp_size = request_size;
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2018-10-26 12:53:39 +00:00
|
|
|
return icon_theme->load_icon(name.c_str(), tmp_size,
|
2019-04-17 12:19:04 +00:00
|
|
|
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
|
2018-10-04 16:03:01 +00:00
|
|
|
}
|
2018-09-17 21:32:05 +00:00
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
void waybar::modules::SNI::Item::onMenuDestroyed(Item* self) {
|
2018-11-22 14:47:23 +00:00
|
|
|
self->gtk_menu = nullptr;
|
|
|
|
self->dbus_menu = nullptr;
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
bool waybar::modules::SNI::Item::makeMenu(GdkEventButton* const& ev) {
|
2018-11-22 14:47:23 +00:00
|
|
|
if (gtk_menu == nullptr) {
|
|
|
|
if (!menu.empty()) {
|
|
|
|
dbus_menu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data());
|
|
|
|
if (dbus_menu != nullptr) {
|
|
|
|
g_object_ref_sink(G_OBJECT(dbus_menu));
|
|
|
|
g_object_weak_ref(G_OBJECT(dbus_menu), (GWeakNotify)onMenuDestroyed, this);
|
|
|
|
gtk_menu = Glib::wrap(GTK_MENU(dbus_menu));
|
|
|
|
gtk_menu->attach_to_widget(event_box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gtk_menu != nullptr) {
|
|
|
|
#if GTK_CHECK_VERSION(3, 22, 0)
|
|
|
|
gtk_menu->popup_at_pointer(reinterpret_cast<GdkEvent*>(ev));
|
|
|
|
#else
|
|
|
|
gtk_menu->popup(ev->button, ev->time);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
bool waybar::modules::SNI::Item::handleClick(GdkEventButton* const& ev) {
|
|
|
|
auto parameters = VariantContainerBase::create_tuple(
|
|
|
|
{Variant<int>::create(ev->x), Variant<int>::create(ev->y)});
|
2018-11-22 14:47:23 +00:00
|
|
|
if ((ev->button == 1 && item_is_menu) || ev->button == 3) {
|
|
|
|
if (!makeMenu(ev)) {
|
2019-03-28 02:56:19 +00:00
|
|
|
proxy_->call("ContextMenu", parameters);
|
|
|
|
return true;
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|
2018-11-22 14:47:23 +00:00
|
|
|
} else if (ev->button == 1) {
|
2019-03-28 02:56:19 +00:00
|
|
|
proxy_->call("Activate", parameters);
|
|
|
|
return true;
|
2018-11-22 14:47:23 +00:00
|
|
|
} else if (ev->button == 2) {
|
2019-03-28 02:56:19 +00:00
|
|
|
proxy_->call("SecondaryActivate", parameters);
|
|
|
|
return true;
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
2018-11-22 14:47:23 +00:00
|
|
|
return false;
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|