refactor: format tray && partial fix for #235

This commit is contained in:
Alex 2019-04-17 14:19:04 +02:00
parent 93a1bafb46
commit 346ec68578
9 changed files with 244 additions and 267 deletions

View File

@ -1,42 +1,43 @@
#pragma once
#include <glibmm/refptr.h>
#include <dbus-status-notifier-watcher.h>
#include <giomm.h>
#include <glibmm/refptr.h>
#include <json/json.h>
#include <tuple>
#include <dbus-status-notifier-watcher.h>
#include "modules/sni/item.hpp"
namespace waybar::modules::SNI {
class Host {
public:
Host(const std::size_t id, const Json::Value&,
const std::function<void(std::unique_ptr<Item>&)>&,
const std::function<void(std::unique_ptr<Item>&)>&);
~Host();
private:
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
void nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring, const Glib::ustring&);
void nameVanished(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
static void proxyReady(GObject*, GAsyncResult*, gpointer);
static void registerHost(GObject*, GAsyncResult*, gpointer);
static void itemRegistered(SnWatcher*, const gchar*, gpointer);
static void itemUnregistered(SnWatcher*, const gchar*, gpointer);
public:
Host(const std::size_t id, const Json::Value&, const std::function<void(std::unique_ptr<Item>&)>&,
const std::function<void(std::unique_ptr<Item>&)>&);
~Host();
std::tuple<std::string, std::string> getBusNameAndObjectPath(const std::string);
void addRegisteredItem(std::string service);
private:
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
void nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring,
const Glib::ustring&);
void nameVanished(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
static void proxyReady(GObject*, GAsyncResult*, gpointer);
static void registerHost(GObject*, GAsyncResult*, gpointer);
static void itemRegistered(SnWatcher*, const gchar*, gpointer);
static void itemUnregistered(SnWatcher*, const gchar*, gpointer);
std::vector<std::unique_ptr<Item>> items_;
const std::string bus_name_;
const std::string object_path_;
std::size_t bus_name_id_;
std::size_t watcher_id_;
GCancellable* cancellable_ = nullptr;
SnWatcher* watcher_ = nullptr;
const Json::Value &config_;
const std::function<void(std::unique_ptr<Item>&)> on_add_;
const std::function<void(std::unique_ptr<Item>&)> on_remove_;
std::tuple<std::string, std::string> getBusNameAndObjectPath(const std::string);
void addRegisteredItem(std::string service);
std::vector<std::unique_ptr<Item>> items_;
const std::string bus_name_;
const std::string object_path_;
std::size_t bus_name_id_;
std::size_t watcher_id_;
GCancellable* cancellable_ = nullptr;
SnWatcher* watcher_ = nullptr;
const Json::Value& config_;
const std::function<void(std::unique_ptr<Item>&)> on_add_;
const std::function<void(std::unique_ptr<Item>&)> on_remove_;
};
}
} // namespace waybar::modules::SNI

View File

@ -1,11 +1,11 @@
#pragma once
#include <dbus-status-notifier-item.h>
#include <glibmm/refptr.h>
#include <giomm/dbusproxy.h>
#include <glibmm/refptr.h>
#include <gtkmm/eventbox.h>
#include <gtkmm/image.h>
#include <gtkmm/icontheme.h>
#include <gtkmm/image.h>
#include <gtkmm/menu.h>
#include <json/json.h>
#include <libdbusmenu-gtk/dbusmenu-gtk.h>
@ -19,7 +19,7 @@
namespace waybar::modules::SNI {
class Item : public sigc::trackable {
public:
public:
Item(std::string, std::string, const Json::Value&);
~Item() = default;
@ -44,24 +44,24 @@ public:
std::string attention_movie_name;
std::string icon_theme_path;
std::string menu;
DbusmenuGtkMenu *dbus_menu = nullptr;
Gtk::Menu *gtk_menu = nullptr;
DbusmenuGtkMenu* dbus_menu = nullptr;
Gtk::Menu* gtk_menu = nullptr;
bool item_is_menu;
private:
private:
void proxyReady(Glib::RefPtr<Gio::AsyncResult>& result);
void setProperty(const Glib::ustring& name, Glib::VariantBase& value);
void getUpdatedProperties();
void processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& result);
void onSignal(const Glib::ustring& sender_name, const Glib::ustring& signal_name,
const Glib::VariantContainerBase& arguments);
const Glib::VariantContainerBase& arguments);
void updateImage();
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant *variant);
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant* variant);
Glib::RefPtr<Gdk::Pixbuf> getIconByName(std::string name, int size);
static void onMenuDestroyed(Item *self);
bool makeMenu(GdkEventButton *const &ev);
bool handleClick(GdkEventButton *const & /*ev*/);
static void onMenuDestroyed(Item* self);
bool makeMenu(GdkEventButton* const& ev);
bool handleClick(GdkEventButton* const& /*ev*/);
Glib::RefPtr<Gio::Cancellable> cancellable_;
Glib::RefPtr<Gio::DBus::Proxy> proxy_;
@ -69,4 +69,4 @@ private:
bool update_pending_;
};
} // namespace waybar::modules::SNI
} // namespace waybar::modules::SNI

View File

@ -1,29 +1,30 @@
#pragma once
#include <fmt/format.h>
#include "bar.hpp"
#include "util/json.hpp"
#include "IModule.hpp"
#include "modules/sni/watcher.hpp"
#include "bar.hpp"
#include "modules/sni/host.hpp"
#include "modules/sni/watcher.hpp"
#include "util/json.hpp"
namespace waybar::modules::SNI {
class Tray : public IModule {
public:
Tray(const std::string&, const Bar&, const Json::Value&);
~Tray() = default;
auto update() -> void;
operator Gtk::Widget &();
private:
void onAdd(std::unique_ptr<Item>& item);
void onRemove(std::unique_ptr<Item>& item);
public:
Tray(const std::string&, const Bar&, const Json::Value&);
~Tray() = default;
auto update() -> void;
operator Gtk::Widget&();
static inline std::size_t nb_hosts_ = 0;
const Json::Value& config_;
Gtk::Box box_;
SNI::Watcher watcher_ ;
SNI::Host host_;
private:
void onAdd(std::unique_ptr<Item>& item);
void onRemove(std::unique_ptr<Item>& item);
static inline std::size_t nb_hosts_ = 0;
const Json::Value& config_;
Gtk::Box box_;
SNI::Watcher watcher_;
SNI::Host host_;
};
}
} // namespace waybar::modules::SNI

View File

@ -1,17 +1,17 @@
#pragma once
#include <dbus-status-notifier-watcher.h>
#include <giomm.h>
#include <glibmm/refptr.h>
#include <dbus-status-notifier-watcher.h>
namespace waybar::modules::SNI {
class Watcher {
public:
public:
Watcher();
~Watcher() = default;
~Watcher();
private:
private:
typedef enum { GF_WATCH_TYPE_HOST, GF_WATCH_TYPE_ITEM } GfWatchType;
typedef struct {
@ -23,17 +23,13 @@ private:
guint watch_id;
} GfWatch;
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
static gboolean handleRegisterHost(Watcher *, GDBusMethodInvocation *,
const gchar *);
static gboolean handleRegisterItem(Watcher *, GDBusMethodInvocation *,
const gchar *);
static GfWatch *gfWatchFind(GSList *list, const gchar *bus_name,
const gchar *object_path);
static GfWatch *gfWatchNew(GfWatchType, const gchar *, const gchar *,
const gchar *, Watcher *);
static void nameVanished(GDBusConnection *connection, const char *name,
gpointer data);
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection> &, Glib::ustring);
static gboolean handleRegisterHost(Watcher *, GDBusMethodInvocation *, const gchar *);
static gboolean handleRegisterItem(Watcher *, GDBusMethodInvocation *, const gchar *);
static GfWatch *gfWatchFind(GSList *list, const gchar *bus_name, const gchar *object_path);
static GfWatch *gfWatchNew(GfWatchType, const gchar *, const gchar *, const gchar *, Watcher *);
static void nameVanished(GDBusConnection *connection, const char *name, gpointer data);
static void gfWatchFree(gpointer data);
void updateRegisteredItems(SnWatcher *obj);
@ -42,7 +38,8 @@ private:
GSList *hosts_ = nullptr;
GSList *items_ = nullptr;
SnWatcher *watcher_ = nullptr;
gulong handler_item_id_;
gulong handler_host_id_;
};
} // namespace waybar::modules::SNI
} // namespace waybar::modules::SNI

View File

@ -1,26 +1,25 @@
#include "client.hpp"
#include <csignal>
#include <iostream>
#include "client.hpp"
namespace waybar {
static Client* client;
} // namespace waybar
} // namespace waybar
int main(int argc, char* argv[])
{
int main(int argc, char* argv[]) {
try {
waybar::Client c(argc, argv);
waybar::client = &c;
std::signal(SIGUSR1, [] (int /*signal*/) {
std::signal(SIGUSR1, [](int /*signal*/) {
for (auto& bar : waybar::client->bars) {
bar->toggle();
}
});
for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) {
std::signal(sig, [] (int sig/*signal*/) {
std::signal(sig, [](int sig /*signal*/) {
for (auto& bar : waybar::client->bars) {
bar->handleSignal(sig);
}

View File

@ -4,55 +4,45 @@
using namespace waybar::modules::SNI;
Host::Host(const std::size_t id, const Json::Value &config,
const std::function<void(std::unique_ptr<Item>&)>& on_add,
const std::function<void(std::unique_ptr<Item>&)>& on_remove)
: bus_name_("org.kde.StatusNotifierHost-" + std::to_string(getpid()) + "-" + std::to_string(id)),
object_path_("/StatusNotifierHost/" + std::to_string(id)),
bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name_,
sigc::mem_fun(*this, &Host::busAcquired))),
config_(config), on_add_(on_add), on_remove_(on_remove)
{
}
Host::Host(const std::size_t id, const Json::Value& config,
const std::function<void(std::unique_ptr<Item>&)>& on_add,
const std::function<void(std::unique_ptr<Item>&)>& on_remove)
: bus_name_("org.kde.StatusNotifierHost-" + std::to_string(getpid()) + "-" +
std::to_string(id)),
object_path_("/StatusNotifierHost/" + std::to_string(id)),
bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name_,
sigc::mem_fun(*this, &Host::busAcquired))),
config_(config),
on_add_(on_add),
on_remove_(on_remove) {}
Host::~Host()
{
Gio::DBus::unwatch_name(bus_name_id_);
}
Host::~Host() { Gio::DBus::unwatch_name(bus_name_id_); }
void Host::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name)
{
void Host::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
watcher_id_ = Gio::DBus::watch_name(conn, "org.kde.StatusNotifierWatcher",
sigc::mem_fun(*this, &Host::nameAppeared), sigc::mem_fun(*this, &Host::nameVanished));
sigc::mem_fun(*this, &Host::nameAppeared),
sigc::mem_fun(*this, &Host::nameVanished));
}
void Host::nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name,
const Glib::ustring& name_owner)
{
const Glib::ustring& name_owner) {
if (cancellable_ != nullptr) {
// TODO
return;
}
cancellable_ = g_cancellable_new();
sn_watcher_proxy_new(
conn->gobj(),
G_DBUS_PROXY_FLAGS_NONE,
"org.kde.StatusNotifierWatcher",
"/StatusNotifierWatcher",
cancellable_, &Host::proxyReady, this);
sn_watcher_proxy_new(conn->gobj(), G_DBUS_PROXY_FLAGS_NONE, "org.kde.StatusNotifierWatcher",
"/StatusNotifierWatcher", cancellable_, &Host::proxyReady, this);
}
void Host::nameVanished(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name)
{
void Host::nameVanished(const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name) {
g_cancellable_cancel(cancellable_);
g_clear_object(&cancellable_);
g_clear_object(&watcher_);
items_.clear();
}
void Host::proxyReady(GObject* src, GAsyncResult* res,
gpointer data)
{
void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
@ -60,21 +50,18 @@ void Host::proxyReady(GObject* src, GAsyncResult* res,
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host *>(data);
auto host = static_cast<SNI::Host*>(data);
host->watcher_ = watcher;
if (error != nullptr) {
std::cerr << error->message << std::endl;
g_error_free(error);
return;
}
sn_watcher_call_register_host(
host->watcher_, host->object_path_.c_str(), host->cancellable_,
&Host::registerHost, data);
sn_watcher_call_register_host(host->watcher_, host->object_path_.c_str(), host->cancellable_,
&Host::registerHost, data);
}
void Host::registerHost(GObject* src, GAsyncResult* res,
gpointer data)
{
void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
@ -82,16 +69,14 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host *>(data);
auto host = static_cast<SNI::Host*>(data);
if (error != nullptr) {
std::cerr << error->message << std::endl;
g_error_free(error);
return;
return;
}
g_signal_connect(host->watcher_, "item-registered",
G_CALLBACK(&Host::itemRegistered), data);
g_signal_connect(host->watcher_, "item-unregistered",
G_CALLBACK(&Host::itemUnregistered), data);
g_signal_connect(host->watcher_, "item-registered", G_CALLBACK(&Host::itemRegistered), data);
g_signal_connect(host->watcher_, "item-unregistered", G_CALLBACK(&Host::itemUnregistered), data);
auto items = sn_watcher_dup_registered_items(host->watcher_);
if (items) {
for (uint32_t i = 0; items[i] != nullptr; i += 1) {
@ -101,16 +86,13 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
g_strfreev(items);
}
void Host::itemRegistered(SnWatcher* watcher, const gchar* service, gpointer data)
{
auto host = static_cast<SNI::Host *>(data);
void Host::itemRegistered(SnWatcher* watcher, const gchar* service, gpointer data) {
auto host = static_cast<SNI::Host*>(data);
host->addRegisteredItem(service);
}
void Host::itemUnregistered(
SnWatcher* watcher, const gchar* service, gpointer data)
{
auto host = static_cast<SNI::Host *>(data);
void Host::itemUnregistered(SnWatcher* watcher, const gchar* service, gpointer data) {
auto host = static_cast<SNI::Host*>(data);
auto [bus_name, object_path] = host->getBusNameAndObjectPath(service);
for (auto it = host->items_.begin(); it != host->items_.end(); ++it) {
if ((*it)->bus_name == bus_name && (*it)->object_path == object_path) {
@ -121,9 +103,7 @@ void Host::itemUnregistered(
}
}
std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(
const std::string service)
{
std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(const std::string service) {
auto it = service.find("/");
if (it != std::string::npos) {
return {service.substr(0, it), service.substr(it)};
@ -131,8 +111,7 @@ std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(
return {service, "/StatusNotifierItem"};
}
void Host::addRegisteredItem(std::string service)
{
void Host::addRegisteredItem(std::string service) {
auto [bus_name, object_path] = getBusNameAndObjectPath(service);
items_.emplace_back(new Item(bus_name, object_path, config_));
on_add_(items_.back());

View File

@ -1,7 +1,7 @@
#include "modules/sni/item.hpp"
#include <iostream>
#include <glibmm/main.h>
#include <iostream>
using namespace Glib;
@ -9,23 +9,26 @@ static const ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name;
static const unsigned UPDATE_DEBOUNCE_TIME = 10;
waybar::modules::SNI::Item::Item(std::string bn, std::string op, const Json::Value& config)
: bus_name(bn), object_path(op), icon_size(16), effective_icon_size(0),
icon_theme(Gtk::IconTheme::create()), update_pending_(false) {
: bus_name(bn),
object_path(op),
icon_size(16),
effective_icon_size(0),
icon_theme(Gtk::IconTheme::create()),
update_pending_(false) {
if (config["icon-size"].isUInt()) {
icon_size = config["icon-size"].asUInt();
}
default_icon_path_ = Gtk::IconTheme::get_default()->get_search_path();
event_box.add(image);
event_box.add_events(Gdk::BUTTON_PRESS_MASK);
event_box.signal_button_press_event().connect(
sigc::mem_fun(*this, &Item::handleClick));
event_box.signal_button_press_event().connect(sigc::mem_fun(*this, &Item::handleClick));
cancellable_ = Gio::Cancellable::create();
auto interface = Glib::wrap(sn_item_interface_info(), true);
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);
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);
}
void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
@ -33,7 +36,7 @@ void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& resu
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();
for (const auto& name: cached_properties) {
for (const auto& name : cached_properties) {
Glib::VariantBase value;
this->proxy_->get_cached_property(value, name);
setProperty(name, value);
@ -42,30 +45,28 @@ void waybar::modules::SNI::Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& resu
this->proxy_->signal_signal().connect(sigc::mem_fun(*this, &Item::onSignal));
if (this->id.empty() || this->category.empty() || this->status.empty()) {
std::cerr << "Invalid Status Notifier Item: " + this->bus_name + "," +
this->object_path << std::endl;
std::cerr << "Invalid Status Notifier Item: " + this->bus_name + "," + this->object_path
<< std::endl;
return;
}
this->updateImage();
// this->event_box.set_tooltip_text(this->title);
} catch (const Glib::Error& err) {
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(),
object_path.c_str(), err.what().c_str());
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(), object_path.c_str(),
err.what().c_str());
} catch (const std::exception& err) {
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(),
object_path.c_str(), err.what());
g_error("Failed to create DBus Proxy for %s %s: %s", bus_name.c_str(), object_path.c_str(),
err.what());
}
}
template<typename T>
template <typename T>
T get_variant(VariantBase& value) {
return VariantBase::cast_dynamic<Variant<T>>(value).get();
return VariantBase::cast_dynamic<Variant<T>>(value).get();
}
void
waybar::modules::SNI::Item::setProperty(const ustring& name,
VariantBase& value) {
void waybar::modules::SNI::Item::setProperty(const ustring& name, VariantBase& value) {
if (name == "Category") {
category = get_variant<std::string>(value);
} else if (name == "Id") {
@ -106,20 +107,15 @@ waybar::modules::SNI::Item::setProperty(const ustring& name,
}
}
void
waybar::modules::SNI::Item::getUpdatedProperties() {
void waybar::modules::SNI::Item::getUpdatedProperties() {
update_pending_ = false;
auto params = VariantContainerBase::create_tuple({
Variant<ustring>::create(SNI_INTERFACE_NAME)
});
auto params = VariantContainerBase::create_tuple({Variant<ustring>::create(SNI_INTERFACE_NAME)});
proxy_->call("org.freedesktop.DBus.Properties.GetAll",
sigc::mem_fun(*this, &Item::processUpdatedProperties), params);
sigc::mem_fun(*this, &Item::processUpdatedProperties), params);
};
void
waybar::modules::SNI::Item::processUpdatedProperties(
Glib::RefPtr<Gio::AsyncResult>& _result) {
void waybar::modules::SNI::Item::processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& _result) {
try {
auto result = proxy_->call_finish(_result);
// extract "a{sv}" from VariantContainerBase
@ -127,7 +123,7 @@ waybar::modules::SNI::Item::processUpdatedProperties(
result.get_child(properties_variant);
auto properties = properties_variant.get();
for (const auto& [name, value]: properties) {
for (const auto& [name, value] : properties) {
VariantBase old_value;
proxy_->get_cached_property(old_value, name);
if (!value.equal(old_value)) {
@ -145,41 +141,34 @@ waybar::modules::SNI::Item::processUpdatedProperties(
}
}
void
waybar::modules::SNI::Item::onSignal(const ustring& sender_name,
const ustring& signal_name, const VariantContainerBase& arguments) {
void waybar::modules::SNI::Item::onSignal(const ustring& sender_name, const ustring& signal_name,
const VariantContainerBase& arguments) {
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;
Glib::signal_timeout().connect_once(
sigc::mem_fun(*this, &Item::getUpdatedProperties), UPDATE_DEBOUNCE_TIME);
Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &Item::getUpdatedProperties),
UPDATE_DEBOUNCE_TIME);
}
}
static void pixbuf_data_deleter(const guint8* data) { g_free((void*)data); }
static void
pixbuf_data_deleter(const guint8* data) {
g_free((void*) data);
}
Glib::RefPtr<Gdk::Pixbuf>
waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
GVariantIter *it;
Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::extractPixBuf(GVariant* variant) {
GVariantIter* it;
g_variant_get(variant, "a(iiay)", &it);
if (it == nullptr) {
return Glib::RefPtr<Gdk::Pixbuf>{};
}
GVariant *val;
GVariant* val;
gint lwidth = 0;
gint lheight = 0;
gint width;
gint height;
guchar *array = nullptr;
guchar* array = nullptr;
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
if (width > 0 && height > 0 && val != nullptr &&
width * height > lwidth * lheight) {
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) {
@ -189,7 +178,7 @@ waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
if (array != nullptr) {
g_free(array);
}
array = static_cast<guchar *>(g_memdup(data, size));
array = static_cast<guchar*>(g_memdup(data, size));
lwidth = width;
lheight = height;
}
@ -206,15 +195,13 @@ waybar::modules::SNI::Item::extractPixBuf(GVariant *variant) {
array[i + 2] = array[i + 3];
array[i + 3] = alpha;
}
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB,
true, 8, lwidth, lheight, 4 * lwidth,
&pixbuf_data_deleter);
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB, true, 8, lwidth,
lheight, 4 * lwidth, &pixbuf_data_deleter);
}
return Glib::RefPtr<Gdk::Pixbuf>{};
}
void waybar::modules::SNI::Item::updateImage()
{
void waybar::modules::SNI::Item::updateImage() {
image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
image.set_pixel_size(icon_size);
if (!icon_name.empty()) {
@ -229,31 +216,29 @@ void waybar::modules::SNI::Item::updateImage()
if (pixbuf->gobj() != nullptr) {
// An icon specified by path and filename may be the wrong size for
// the tray
pixbuf = pixbuf->scale_simple(icon_size, icon_size,
Gdk::InterpType::INTERP_BILINEAR);
pixbuf = pixbuf->scale_simple(icon_size, icon_size, Gdk::InterpType::INTERP_BILINEAR);
image.set(pixbuf);
}
} else {
image.set(getIconByName(icon_name, icon_size));
}
} catch (Glib::Error &e) {
} catch (Glib::Error& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
} else if (icon_pixmap) {
// An icon extracted may be the wrong size for the tray
icon_pixmap = icon_pixmap->scale_simple(icon_size, icon_size,
Gdk::InterpType::INTERP_BILINEAR);
icon_pixmap = icon_pixmap->scale_simple(icon_size, icon_size, Gdk::InterpType::INTERP_BILINEAR);
image.set(icon_pixmap);
}
}
Glib::RefPtr<Gdk::Pixbuf>
waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::getIconByName(std::string name,
int request_size) {
int tmp_size = 0;
icon_theme->rescan_if_needed();
auto sizes = icon_theme->get_icon_sizes(name.c_str());
for (auto const &size : sizes) {
for (auto const& size : sizes) {
// -1 == scalable
if (size == request_size || size == -1) {
tmp_size = request_size;
@ -269,17 +254,15 @@ waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
tmp_size = request_size;
}
return icon_theme->load_icon(name.c_str(), tmp_size,
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
}
void waybar::modules::SNI::Item::onMenuDestroyed(Item *self)
{
void waybar::modules::SNI::Item::onMenuDestroyed(Item* self) {
self->gtk_menu = nullptr;
self->dbus_menu = nullptr;
}
bool waybar::modules::SNI::Item::makeMenu(GdkEventButton *const &ev)
{
bool waybar::modules::SNI::Item::makeMenu(GdkEventButton* const& ev) {
if (gtk_menu == nullptr) {
if (!menu.empty()) {
dbus_menu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data());
@ -302,11 +285,9 @@ bool waybar::modules::SNI::Item::makeMenu(GdkEventButton *const &ev)
return false;
}
bool waybar::modules::SNI::Item::handleClick(GdkEventButton *const &ev) {
auto parameters = VariantContainerBase::create_tuple({
Variant<int>::create(ev->x),
Variant<int>::create(ev->y)
});
bool waybar::modules::SNI::Item::handleClick(GdkEventButton* const& ev) {
auto parameters = VariantContainerBase::create_tuple(
{Variant<int>::create(ev->x), Variant<int>::create(ev->y)});
if ((ev->button == 1 && item_is_menu) || ev->button == 3) {
if (!makeMenu(ev)) {
proxy_->call("ContextMenu", parameters);

View File

@ -2,14 +2,12 @@
#include <iostream>
waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar,
const Json::Value &config)
waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config)
: config_(config),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
watcher_(), host_(nb_hosts_, config,
std::bind(&Tray::onAdd, this, std::placeholders::_1),
std::bind(&Tray::onRemove, this, std::placeholders::_1))
{
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
watcher_(),
host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1),
std::bind(&Tray::onRemove, this, std::placeholders::_1)) {
std::cout << "Tray is in beta, so there may be bugs or even be unusable." << std::endl;
if (config_["spacing"].isUInt()) {
box_.set_spacing(config_["spacing"].asUInt());
@ -17,14 +15,12 @@ waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar,
nb_hosts_ += 1;
}
void waybar::modules::SNI::Tray::onAdd(std::unique_ptr<Item>& item)
{
void waybar::modules::SNI::Tray::onAdd(std::unique_ptr<Item>& item) {
box_.pack_start(item->event_box);
dp.emit();
}
void waybar::modules::SNI::Tray::onRemove(std::unique_ptr<Item>& item)
{
void waybar::modules::SNI::Tray::onRemove(std::unique_ptr<Item>& item) {
box_.remove(item->event_box);
dp.emit();
}
@ -38,6 +34,4 @@ auto waybar::modules::SNI::Tray::update() -> void {
}
}
waybar::modules::SNI::Tray::operator Gtk::Widget &() {
return box_;
}
waybar::modules::SNI::Tray::operator Gtk::Widget&() { return box_; }

View File

@ -5,33 +5,50 @@
using namespace waybar::modules::SNI;
Watcher::Watcher()
: bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION,
"org.kde.StatusNotifierWatcher", sigc::mem_fun(*this, &Watcher::busAcquired),
Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(),
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)),
watcher_(sn_watcher_skeleton_new())
{
: bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION,
"org.kde.StatusNotifierWatcher",
sigc::mem_fun(*this, &Watcher::busAcquired),
Gio::DBus::SlotNameAcquired(), Gio::DBus::SlotNameLost(),
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE)),
watcher_(sn_watcher_skeleton_new()) {}
Watcher::~Watcher() {
if (bus_name_id_ > 0) {
g_bus_unown_name(bus_name_id_);
bus_name_id_ = 0;
}
if (hosts_ != NULL) {
g_slist_free_full(hosts_, gfWatchFree);
hosts_ = NULL;
}
if (items_ != NULL) {
g_slist_free_full(items_, gfWatchFree);
items_ = NULL;
}
g_signal_handler_disconnect(watcher_, handler_host_id_);
g_signal_handler_disconnect(watcher_, handler_item_id_);
}
void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name)
{
void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
GError* error = nullptr;
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_),
conn->gobj(), "/StatusNotifierWatcher", &error);
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_), conn->gobj(),
"/StatusNotifierWatcher", &error);
if (error != nullptr) {
std::cerr << error->message << std::endl;
g_error_free(error);
return;
}
g_signal_connect_swapped(watcher_, "handle-register-item",
G_CALLBACK(&Watcher::handleRegisterItem), this);
g_signal_connect_swapped(watcher_, "handle-register-host",
G_CALLBACK(&Watcher::handleRegisterHost), this);
handler_item_id_ = g_signal_connect_swapped(watcher_, "handle-register-item",
G_CALLBACK(&Watcher::handleRegisterItem), this);
handler_host_id_ = g_signal_connect_swapped(watcher_, "handle-register-host",
G_CALLBACK(&Watcher::handleRegisterHost), this);
}
gboolean Watcher::handleRegisterHost(Watcher* obj,
GDBusMethodInvocation* invocation, const gchar* service)
{
gboolean Watcher::handleRegisterHost(Watcher* obj, GDBusMethodInvocation* invocation,
const gchar* service) {
const gchar* bus_name = service;
const gchar* object_path = "/StatusNotifierHost";
@ -40,15 +57,16 @@ gboolean Watcher::handleRegisterHost(Watcher* obj,
object_path = service;
}
if (g_dbus_is_name(bus_name) == FALSE) {
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_INVALID_ARGS, "D-Bus bus name '%s' is not valid", bus_name);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"D-Bus bus name '%s' is not valid", bus_name);
return TRUE;
}
auto watch = gfWatchFind(obj->hosts_, bus_name, object_path);
if (watch != nullptr) {
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_INVALID_ARGS, "Status Notifier Host with bus name '%s' and object path '%s' is already registered",
bus_name, object_path);
g_dbus_method_invocation_return_error(
invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"Status Notifier Host with bus name '%s' and object path '%s' is already registered",
bus_name, object_path);
return TRUE;
}
watch = gfWatchNew(GF_WATCH_TYPE_HOST, service, bus_name, object_path, obj);
@ -61,9 +79,8 @@ gboolean Watcher::handleRegisterHost(Watcher* obj,
return TRUE;
}
gboolean Watcher::handleRegisterItem(Watcher* obj,
GDBusMethodInvocation* invocation, const gchar* service)
{
gboolean Watcher::handleRegisterItem(Watcher* obj, GDBusMethodInvocation* invocation,
const gchar* service) {
const gchar* bus_name = service;
const gchar* object_path = "/StatusNotifierItem";
@ -72,14 +89,14 @@ gboolean Watcher::handleRegisterItem(Watcher* obj,
object_path = service;
}
if (g_dbus_is_name(bus_name) == FALSE) {
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_INVALID_ARGS, "D-Bus bus name '%s' is not valid", bus_name);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"D-Bus bus name '%s' is not valid", bus_name);
return TRUE;
}
auto watch = gfWatchFind(obj->items_, bus_name, object_path);
if (watch != nullptr) {
g_warning("Status Notifier Item with bus name '%s' and object path '%s' is already registered",
bus_name, object_path);
bus_name, object_path);
sn_watcher_complete_register_item(obj->watcher_, invocation);
return TRUE;
}
@ -94,40 +111,49 @@ gboolean Watcher::handleRegisterItem(Watcher* obj,
}
Watcher::GfWatch* Watcher::gfWatchFind(GSList* list, const gchar* bus_name,
const gchar* object_path)
{
for (GSList* l = list; l != nullptr; l = g_slist_next (l)) {
const gchar* object_path) {
for (GSList* l = list; l != nullptr; l = g_slist_next(l)) {
GfWatch* watch = static_cast<GfWatch*>(l->data);
if (g_strcmp0 (watch->bus_name, bus_name) == 0
&& g_strcmp0 (watch->object_path, object_path) == 0) {
if (g_strcmp0(watch->bus_name, bus_name) == 0 &&
g_strcmp0(watch->object_path, object_path) == 0) {
return watch;
}
}
return nullptr;
}
Watcher::GfWatch* Watcher::gfWatchNew(GfWatchType type, const gchar* service,
const gchar* bus_name, const gchar* object_path, Watcher* watcher)
{
void Watcher::gfWatchFree(gpointer data) {
GfWatch* watch;
watch = (GfWatch*)data;
if (watch->watch_id > 0) g_bus_unwatch_name(watch->watch_id);
g_free(watch->service);
g_free(watch->bus_name);
g_free(watch->object_path);
g_free(watch);
}
Watcher::GfWatch* Watcher::gfWatchNew(GfWatchType type, const gchar* service, const gchar* bus_name,
const gchar* object_path, Watcher* watcher) {
GfWatch* watch = g_new0(GfWatch, 1);
watch->type = type;
watch->watcher = watcher;
watch->service = g_strdup(service);
watch->bus_name = g_strdup(bus_name);
watch->object_path = g_strdup(object_path);
watch->watch_id = g_bus_watch_name(G_BUS_TYPE_SESSION, bus_name,
G_BUS_NAME_WATCHER_FLAGS_NONE, nullptr, &Watcher::nameVanished, watch,
nullptr);
watch->watch_id = g_bus_watch_name(G_BUS_TYPE_SESSION, bus_name, G_BUS_NAME_WATCHER_FLAGS_NONE,
nullptr, &Watcher::nameVanished, watch, nullptr);
return watch;
}
void Watcher::nameVanished(GDBusConnection* connection, const char* name,
gpointer data)
{
auto watch = static_cast<GfWatch *>(data);
void Watcher::nameVanished(GDBusConnection* connection, const char* name, gpointer data) {
auto watch = static_cast<GfWatch*>(data);
if (watch->type == GF_WATCH_TYPE_HOST) {
watch->watcher->hosts_ = g_slist_remove(watch->watcher->hosts_, watch);
if (watch->watcher->hosts_ == nullptr) {
if (watch->watcher->hosts_ == nullptr) {
sn_watcher_set_is_host_registered(watch->watcher->watcher_, FALSE);
sn_watcher_emit_host_registered(watch->watcher->watcher_);
}
@ -140,10 +166,9 @@ void Watcher::nameVanished(GDBusConnection* connection, const char* name,
}
}
void Watcher::updateRegisteredItems(SnWatcher* obj)
{
void Watcher::updateRegisteredItems(SnWatcher* obj) {
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
for (GSList* l = items_; l != nullptr; l = g_slist_next(l)) {
GfWatch* watch = static_cast<GfWatch*>(l->data);
gchar* item = g_strdup_printf("%s%s", watch->bus_name, watch->object_path);