nice spotify notification
This commit is contained in:
132
src/services/dbus/mpris.cpp
Normal file
132
src/services/dbus/mpris.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "services/dbus/mpris.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
#include "helpers/string.hpp"
|
||||
#include "services/notificationController.hpp"
|
||||
|
||||
#include "giomm/dbusconnection.h"
|
||||
#include "giomm/dbusproxy.h"
|
||||
|
||||
MprisController::MprisController() {
|
||||
// 1. Connect to the Session Bus
|
||||
Gio::DBus::Connection::get(
|
||||
Gio::DBus::BusType::SESSION,
|
||||
sigc::mem_fun(*this, &MprisController::on_bus_connected));
|
||||
}
|
||||
|
||||
void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &result) {
|
||||
if (!result) {
|
||||
std::cerr << "DBus Connection Error: null async result" << std::endl;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
m_connection = Gio::DBus::Connection::get_finish(result);
|
||||
|
||||
std::string player_bus_name = "org.mpris.MediaPlayer2.spotify";
|
||||
|
||||
m_proxy = Gio::DBus::Proxy::create_sync(
|
||||
m_connection,
|
||||
player_bus_name, // The Bus Name
|
||||
"/org/mpris/MediaPlayer2", // The Object Path
|
||||
"org.mpris.MediaPlayer2.Player" // The Interface
|
||||
);
|
||||
|
||||
if (m_proxy) {
|
||||
std::cout << "Connected to: " << player_bus_name << std::endl;
|
||||
|
||||
// uncomment if launch notification on start
|
||||
launchNotification();
|
||||
|
||||
m_proxy->signal_properties_changed().connect(
|
||||
sigc::mem_fun(*this, &MprisController::on_properties_changed));
|
||||
}
|
||||
|
||||
} catch (const Glib::Error &ex) {
|
||||
std::cerr << "DBus Connection Error: " << ex.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void MprisController::launchNotification() {
|
||||
if (!m_proxy) {
|
||||
return;
|
||||
}
|
||||
|
||||
Glib::VariantBase metadata_var;
|
||||
m_proxy->get_cached_property(metadata_var, "Metadata");
|
||||
|
||||
if (!metadata_var) {
|
||||
std::cout << "No metadata available." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
using MetadataMap = std::map<Glib::ustring, Glib::VariantBase>;
|
||||
MetadataMap metadata_map;
|
||||
|
||||
Glib::Variant<MetadataMap> variant_dict =
|
||||
Glib::VariantBase::cast_dynamic<Glib::Variant<MetadataMap>>(metadata_var);
|
||||
|
||||
metadata_map = variant_dict.get();
|
||||
|
||||
std::string title, artist, artwork_url;
|
||||
|
||||
if (metadata_map.count("xesam:title")) {
|
||||
auto title_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(metadata_map["xesam:title"]);
|
||||
title = title_var.get();
|
||||
}
|
||||
|
||||
if (metadata_map.count("xesam:artist")) {
|
||||
auto artist_var = metadata_map["xesam:artist"];
|
||||
|
||||
if (artist_var.is_of_type(Glib::VariantType("as"))) {
|
||||
auto artists = Glib::VariantBase::cast_dynamic<Glib::Variant<std::vector<Glib::ustring>>>(artist_var).get();
|
||||
if (!artists.empty()) {
|
||||
artist = artists[0]; // Take the first artist
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata_map.count("mpris:artUrl")) {
|
||||
auto art_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(metadata_map["mpris:artUrl"]);
|
||||
artwork_url = art_var.get();
|
||||
}
|
||||
|
||||
auto notifactionController = NotificationController::getInstance();
|
||||
|
||||
MprisController::MprisPlayer2Message mpris;
|
||||
mpris.title = StringHelper::trimToSize(title, 30);
|
||||
mpris.artist = StringHelper::trimToSize(artist, 30);
|
||||
mpris.artwork_url = artwork_url;
|
||||
mpris.play_pause = [this]() { this->toggle_play(); };
|
||||
mpris.next = [this]() { this->next_song(); };
|
||||
mpris.previous = [this]() { this->previous_song(); };
|
||||
notifactionController->showSpotifyNotification(mpris);
|
||||
}
|
||||
|
||||
void MprisController::on_properties_changed(const Gio::DBus::Proxy::MapChangedProperties &changed_properties,
|
||||
const std::vector<Glib::ustring> &) {
|
||||
|
||||
if (changed_properties.find("Metadata") != changed_properties.end()) {
|
||||
launchNotification();
|
||||
}
|
||||
}
|
||||
|
||||
void MprisController::previous_song() {
|
||||
if (m_proxy) {
|
||||
m_proxy->call("Previous");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MprisController::toggle_play() {
|
||||
if (m_proxy) {
|
||||
m_proxy->call("PlayPause");
|
||||
}
|
||||
}
|
||||
|
||||
void MprisController::next_song() {
|
||||
if (m_proxy) {
|
||||
m_proxy->call("Next");
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,27 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "services/dbus/mpris.hpp"
|
||||
#include "services/textureCache.hpp"
|
||||
|
||||
#include "gdkmm/display.h"
|
||||
#include "giomm/listmodel.h"
|
||||
#include "glibmm/main.h"
|
||||
#include "gtk4-layer-shell.h"
|
||||
#include "gtkmm/box.h"
|
||||
#include "gtkmm/button.h"
|
||||
#include "gtkmm/centerbox.h"
|
||||
#include "gtkmm/image.h"
|
||||
#include "gtkmm/label.h"
|
||||
#include "gtkmm/window.h"
|
||||
#include "services/textureCache.hpp"
|
||||
|
||||
std::shared_ptr<NotificationController> NotificationController::instance = nullptr;
|
||||
|
||||
NotificationController::NotificationController() {
|
||||
if (NotificationController::instance) {
|
||||
throw std::runtime_error("use getInstance()!");
|
||||
}
|
||||
|
||||
auto display = Gdk::Display::get_default();
|
||||
if (!display) {
|
||||
return;
|
||||
@@ -33,82 +41,110 @@ NotificationController::NotificationController() {
|
||||
}
|
||||
}
|
||||
|
||||
void NotificationController::showSpotifyNotification(const std::string &title, const std::string &message, const std::string &artwork_url) {
|
||||
void NotificationController::showSpotifyNotification(MprisController::MprisPlayer2Message mpris) {
|
||||
for (const auto &monitor : this->activeMonitors) {
|
||||
|
||||
auto win = new Gtk::Window();
|
||||
win->set_title(title);
|
||||
win->set_default_size(300, 100);
|
||||
|
||||
gtk_layer_init_for_window(win->gobj());
|
||||
gtk_layer_set_monitor(win->gobj(), monitor->gobj());
|
||||
gtk_layer_set_layer(win->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
|
||||
gtk_layer_set_margin(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
|
||||
|
||||
win->add_css_class("notification-popup");
|
||||
auto win = std::make_shared<Gtk::Window>();
|
||||
win->set_title(mpris.title);
|
||||
this->baseWindowSetup(win, monitor);
|
||||
|
||||
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
|
||||
|
||||
if (auto texture = TextureCacheService::getInstance()->getTexture(artwork_url)) {
|
||||
if (auto texture = TextureCacheService::getInstance()->getTexture(mpris.artwork_url)) {
|
||||
auto img = Gtk::make_managed<Gtk::Image>(texture);
|
||||
// make it larger
|
||||
img->set_pixel_size(64);
|
||||
img->set_pixel_size(64);
|
||||
container->append(*img);
|
||||
}
|
||||
|
||||
auto text_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5);
|
||||
text_box->set_halign(Gtk::Align::CENTER);
|
||||
text_box->set_valign(Gtk::Align::CENTER);
|
||||
|
||||
auto title_label = Gtk::make_managed<Gtk::Label>("<b>" + title + "</b>");
|
||||
auto rightArea = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5);
|
||||
rightArea->set_halign(Gtk::Align::CENTER);
|
||||
rightArea->set_valign(Gtk::Align::CENTER);
|
||||
// make sure rigfht area takes the remaining space
|
||||
rightArea->set_hexpand(true);
|
||||
// also set a max width
|
||||
|
||||
auto title_label = Gtk::make_managed<Gtk::Label>("<b>" + mpris.title + "</b>");
|
||||
title_label->set_use_markup(true);
|
||||
title_label->set_halign(Gtk::Align::START);
|
||||
text_box->append(*title_label);
|
||||
title_label->set_halign(Gtk::Align::START);
|
||||
rightArea->append(*title_label);
|
||||
|
||||
auto artistLabel = Gtk::make_managed<Gtk::Label>(mpris.artist);
|
||||
artistLabel->set_halign(Gtk::Align::CENTER);
|
||||
rightArea->append(*artistLabel);
|
||||
|
||||
auto message_label = Gtk::make_managed<Gtk::Label>(message);
|
||||
message_label->set_halign(Gtk::Align::START);
|
||||
text_box->append(*message_label);
|
||||
auto buttonBox = Gtk::make_managed<Gtk::CenterBox>();
|
||||
buttonBox->add_css_class("notification-button-box");
|
||||
buttonBox->set_hexpand(true);
|
||||
|
||||
container->append(*text_box);
|
||||
auto backButton = Gtk::make_managed<Gtk::Button>("<");
|
||||
backButton->add_css_class("flat-button");
|
||||
auto playPauseButton = Gtk::make_managed<Gtk::Button>("=<");
|
||||
playPauseButton->add_css_class("flat-button");
|
||||
auto nextButton = Gtk::make_managed<Gtk::Button>(">");
|
||||
nextButton->add_css_class("flat-button");
|
||||
|
||||
backButton->signal_clicked().connect([mpris]() {
|
||||
if (mpris.previous) {
|
||||
mpris.previous();
|
||||
}
|
||||
});
|
||||
|
||||
playPauseButton->signal_clicked().connect([mpris]() {
|
||||
if (mpris.play_pause) {
|
||||
mpris.play_pause();
|
||||
}
|
||||
});
|
||||
|
||||
nextButton->signal_clicked().connect([mpris]() {
|
||||
if (mpris.next) {
|
||||
mpris.next();
|
||||
}
|
||||
});
|
||||
buttonBox->set_start_widget(*backButton);
|
||||
buttonBox->set_center_widget(*playPauseButton);
|
||||
buttonBox->set_end_widget(*nextButton);
|
||||
|
||||
// rightArea->append(*buttonBox);
|
||||
|
||||
container->append(*rightArea);
|
||||
|
||||
win->set_child(*container);
|
||||
win->show();
|
||||
|
||||
// Auto close after 3 seconds for demo purposes
|
||||
Glib::signal_timeout().connect([win]() {
|
||||
win->close();
|
||||
delete win;
|
||||
return false; // Don't repeat
|
||||
},
|
||||
3000);
|
||||
}
|
||||
}
|
||||
|
||||
void NotificationController::baseWindowSetup(std::shared_ptr<Gtk::Window> win, std::shared_ptr<Gdk::Monitor> monitor) {
|
||||
win->set_default_size(300, 100);
|
||||
gtk_layer_init_for_window(win->gobj());
|
||||
gtk_layer_set_monitor(win->gobj(), monitor->gobj());
|
||||
gtk_layer_set_layer(win->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
|
||||
gtk_layer_set_margin(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
|
||||
win->add_css_class("notification-popup");
|
||||
}
|
||||
|
||||
void NotificationController::showNotificationOnAllMonitors(const std::string &title, const std::string &message) {
|
||||
for (const auto &monitor : this->activeMonitors) {
|
||||
auto win = new Gtk::Window();
|
||||
auto win = std::make_shared<Gtk::Window>();
|
||||
win->set_title(title);
|
||||
win->set_default_size(300, 100);
|
||||
|
||||
gtk_layer_init_for_window(win->gobj());
|
||||
gtk_layer_set_monitor(win->gobj(), monitor->gobj());
|
||||
gtk_layer_set_layer(win->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
|
||||
gtk_layer_set_margin(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
|
||||
this->baseWindowSetup(win, monitor);
|
||||
|
||||
win->add_css_class("notification-popup");
|
||||
auto label = Gtk::make_managed<Gtk::Label>(message);
|
||||
label->set_use_markup(true);
|
||||
win->set_child(*label);
|
||||
|
||||
win->show();
|
||||
|
||||
Glib::signal_timeout().connect([win]() {
|
||||
win->close();
|
||||
delete win;
|
||||
return false; // Don't repeat
|
||||
},
|
||||
3000);
|
||||
|
||||
Reference in New Issue
Block a user