vibed a cached downloader for images
This commit is contained in:
136
include/services/dbus/mpris.hpp
Normal file
136
include/services/dbus/mpris.hpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <gtkmm.h>
|
||||
#include <giomm.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "services/notificationController.hpp"
|
||||
#include "giomm/dbusconnection.h"
|
||||
#include "giomm/dbusproxy.h"
|
||||
|
||||
class MprisController {
|
||||
public:
|
||||
MprisController() {
|
||||
// 1. Connect to the Session Bus
|
||||
Gio::DBus::Connection::get(
|
||||
Gio::DBus::BusType::SESSION,
|
||||
sigc::mem_fun(*this, &MprisController::on_bus_connected)
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
Glib::RefPtr<Gio::DBus::Connection> m_connection;
|
||||
Glib::RefPtr<Gio::DBus::Proxy> m_proxy;
|
||||
|
||||
void 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);
|
||||
|
||||
// 2. Create a Proxy to the media player
|
||||
// NOTE: In a real app, you must find the name dynamically (see Step 2 below).
|
||||
// For now, ensure a player like Spotify or VLC is running.
|
||||
// Try "org.mpris.MediaPlayer2.spotify" or "org.mpris.MediaPlayer2.vlc"
|
||||
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;
|
||||
|
||||
// Get initial state
|
||||
print_metadata();
|
||||
|
||||
// 3. Listen for changes (Song change, Pause/Play)
|
||||
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 print_metadata() {
|
||||
// Retrieve the cached property "Metadata"
|
||||
Glib::VariantBase metadata_var;
|
||||
m_proxy->get_cached_property(metadata_var, "Metadata");
|
||||
|
||||
if (!metadata_var) {
|
||||
std::cout << "No metadata available." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Unpack Metadata (Type: a{sv})
|
||||
// This is a dictionary of string -> variant
|
||||
using MetadataMap = std::map<Glib::ustring, Glib::VariantBase>;
|
||||
MetadataMap metadata_map;
|
||||
|
||||
// Cast the variant to a container and iterate
|
||||
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();
|
||||
notifactionController->showSpotifyNotification(title, artist, artwork_url);
|
||||
|
||||
}
|
||||
|
||||
// Called when the song changes
|
||||
void on_properties_changed(const Gio::DBus::Proxy::MapChangedProperties& changed_properties,
|
||||
const std::vector<Glib::ustring>& invalidated_properties) {
|
||||
(void)invalidated_properties;
|
||||
|
||||
// Only refresh when Metadata is updated
|
||||
if (changed_properties.find("Metadata") != changed_properties.end()) {
|
||||
// You could parse 'changed_properties' directly, but it's easier to just pull the new cache
|
||||
print_metadata();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
// Call this to toggle play/pause
|
||||
void toggle_play() {
|
||||
if(m_proxy) {
|
||||
m_proxy->call("PlayPause");
|
||||
}
|
||||
}
|
||||
|
||||
// Call this to skip
|
||||
void next_song() {
|
||||
if(m_proxy) {
|
||||
m_proxy->call("Next");
|
||||
}
|
||||
}
|
||||
};
|
||||
74
include/services/dbus/notification.hpp
Normal file
74
include/services/dbus/notification.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <giomm.h>
|
||||
#include <gtkmm.h>
|
||||
#include <memory>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <vector>
|
||||
|
||||
#include "gdkmm/monitor.h"
|
||||
#include "giomm/dbusconnection.h"
|
||||
#include "giomm/dbusownname.h"
|
||||
#include "glib.h"
|
||||
|
||||
const Glib::ustring introspection_xml = R"(
|
||||
<node>
|
||||
<interface name="org.freedesktop.Notifications">
|
||||
<method name="GetCapabilities">
|
||||
<arg name="capabilities" type="as" direction="out"/>
|
||||
</method>
|
||||
<method name="Notify">
|
||||
<arg name="app_name" type="s" direction="in"/>
|
||||
<arg name="replaces_id" type="u" direction="in"/>
|
||||
<arg name="app_icon" type="s" direction="in"/>
|
||||
<arg name="summary" type="s" direction="in"/>
|
||||
<arg name="body" type="s" direction="in"/>
|
||||
<arg name="actions" type="as" direction="in"/>
|
||||
<arg name="hints" type="a{sv}" direction="in"/>
|
||||
<arg name="expire_timeout" type="i" direction="in"/>
|
||||
<arg name="id" type="u" direction="out"/>
|
||||
</method>
|
||||
<method name="CloseNotification">
|
||||
<arg name="id" type="u" direction="in"/>
|
||||
</method>
|
||||
<method name="GetServerInformation">
|
||||
<arg name="name" type="s" direction="out"/>
|
||||
<arg name="vendor" type="s" direction="out"/>
|
||||
<arg name="version" type="s" direction="out"/>
|
||||
<arg name="spec_version" type="s" direction="out"/>
|
||||
</method>
|
||||
</interface>
|
||||
</node>
|
||||
)";
|
||||
|
||||
class NotificationService {
|
||||
|
||||
public:
|
||||
NotificationService() : notificationIdCounter(1) {
|
||||
Gio::DBus::own_name(
|
||||
Gio::DBus::BusType::SESSION,
|
||||
"org.freedesktop.Notifications",
|
||||
sigc::mem_fun(*this, &NotificationService::onBusAcquired),
|
||||
{}, // Name acquired slot (optional)
|
||||
{}, // Name lost slot (optional)
|
||||
Gio::DBus::BusNameOwnerFlags::REPLACE);
|
||||
}
|
||||
|
||||
void onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name);
|
||||
|
||||
private:
|
||||
guint notificationIdCounter;
|
||||
const Gio::DBus::InterfaceVTable &getMessageInterfaceVTable();
|
||||
void on_method_call(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
||||
const Glib::ustring &sender,
|
||||
const Glib::ustring &object_path,
|
||||
const Glib::ustring &interface_name,
|
||||
const Glib::ustring &method_name,
|
||||
const Glib::VariantContainerBase ¶meters,
|
||||
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation);
|
||||
|
||||
void handle_notify(const Glib::VariantContainerBase ¶meters,
|
||||
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation);
|
||||
void createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message);
|
||||
};
|
||||
Reference in New Issue
Block a user