close one notification to close all
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "services/dbus/mpris.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include <map>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "helpers/string.hpp"
|
||||
@@ -19,14 +20,21 @@ MprisController::MprisController() {
|
||||
sigc::mem_fun(*this, &MprisController::on_bus_connected));
|
||||
}
|
||||
|
||||
sigc::signal<void(const MprisPlayer2Message &)> &
|
||||
MprisController::signal_mpris_updated() {
|
||||
sigc::signal<void(const MprisPlayer2Message &)> &MprisController::signal_mpris_updated() {
|
||||
return mprisUpdatedSignal;
|
||||
}
|
||||
|
||||
sigc::signal<void(MprisController::PlaybackStatus)> &MprisController::signal_playback_status_changed() {
|
||||
return playbackStatusChangedSignal;
|
||||
}
|
||||
|
||||
sigc::signal<void(int64_t)> &MprisController::signal_playback_position_changed() {
|
||||
return playbackPositionChangedSignal;
|
||||
}
|
||||
|
||||
void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &result) {
|
||||
if (!result) {
|
||||
std::cerr << "DBus Connection Error: null async result" << std::endl;
|
||||
spdlog::error("DBus Connection Error: null async result");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -42,7 +50,7 @@ void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &res
|
||||
);
|
||||
|
||||
if (m_proxy) {
|
||||
std::cout << "Connected to: " << player_bus_name << std::endl;
|
||||
spdlog::info("Connected to: {}", player_bus_name);
|
||||
|
||||
signalNotification();
|
||||
|
||||
@@ -51,7 +59,7 @@ void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &res
|
||||
}
|
||||
|
||||
} catch (const Glib::Error &ex) {
|
||||
std::cerr << "DBus Connection Error: " << ex.what() << std::endl;
|
||||
spdlog::error("DBus Connection Error: {}", ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,12 +72,12 @@ void MprisController::signalNotification() {
|
||||
m_proxy->get_cached_property(metadata_var, "Metadata");
|
||||
|
||||
if (!metadata_var) {
|
||||
std::cout << "No metadata available." << std::endl;
|
||||
spdlog::info("No metadata available.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!metadata_var.is_of_type(Glib::VariantType("a{sv}"))) {
|
||||
std::cout << "Unexpected metadata type." << std::endl;
|
||||
spdlog::error("Unexpected metadata type.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -81,7 +89,9 @@ void MprisController::signalNotification() {
|
||||
|
||||
metadata_map = variant_dict.get();
|
||||
|
||||
std::string title, artist, artwork_url;
|
||||
std::string title, artwork_url;
|
||||
std::vector<std::string> artist;
|
||||
|
||||
if (metadata_map.count("xesam:title")) {
|
||||
const auto &title_base = metadata_map["xesam:title"];
|
||||
if (title_base.is_of_type(Glib::VariantType("s"))) {
|
||||
@@ -96,11 +106,13 @@ void MprisController::signalNotification() {
|
||||
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
|
||||
for (const auto &a : artists) {
|
||||
artist.push_back(a);
|
||||
}
|
||||
}
|
||||
} else if (artist_var.is_of_type(Glib::VariantType("s"))) {
|
||||
auto artist_str = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(artist_var);
|
||||
artist = artist_str.get();
|
||||
artist.push_back(artist_str.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,21 +129,21 @@ void MprisController::signalNotification() {
|
||||
const auto &length_base = metadata_map["mpris:length"];
|
||||
if (length_base.is_of_type(Glib::VariantType("x"))) {
|
||||
auto length_var = Glib::VariantBase::cast_dynamic<Glib::Variant<gint64>>(length_base);
|
||||
length_us = static_cast<int64_t>(length_var.get());
|
||||
length_us = static_cast<int64_t>(length_var.get());
|
||||
} else if (length_base.is_of_type(Glib::VariantType("t"))) {
|
||||
auto length_var = Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(length_base);
|
||||
length_us = static_cast<int64_t>(length_var.get());
|
||||
length_us = static_cast<int64_t>(length_var.get());
|
||||
}
|
||||
}
|
||||
|
||||
MprisPlayer2Message mpris;
|
||||
mpris.title = StringHelper::trimToSize(title, 30);
|
||||
mpris.artist = StringHelper::trimToSize(artist, 30);
|
||||
mpris.artist = artist;
|
||||
mpris.artwork_url = artwork_url;
|
||||
mpris.play_pause = [this]() { this->toggle_play(); };
|
||||
mpris.next = [this]() { this->next_song(); };
|
||||
mpris.previous = [this]() { this->previous_song(); };
|
||||
mpris.length_ms = length_us;
|
||||
mpris.length_ms = length_us; // Convert microseconds to milliseconds
|
||||
mprisUpdatedSignal.emit(mpris);
|
||||
}
|
||||
|
||||
@@ -141,6 +153,31 @@ void MprisController::on_properties_changed(const Gio::DBus::Proxy::MapChangedPr
|
||||
if (changed_properties.find("Metadata") != changed_properties.end()) {
|
||||
signalNotification();
|
||||
}
|
||||
|
||||
if (changed_properties.find("PlaybackStatus") != changed_properties.end()) {
|
||||
auto status_var = changed_properties.at("PlaybackStatus");
|
||||
if (status_var.is_of_type(Glib::VariantType("s"))) {
|
||||
auto status = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(status_var).get();
|
||||
spdlog::info("Playback Status changed to: {}", status.raw());
|
||||
auto parsedStatusIt = playbackStatusMap.find(static_cast<std::string>(status));
|
||||
|
||||
if (parsedStatusIt != playbackStatusMap.end()) {
|
||||
currentPlaybackStatus = parsedStatusIt->second;
|
||||
playbackStatusChangedSignal.emit(currentPlaybackStatus);
|
||||
} else {
|
||||
spdlog::error("Unknown playback status: {}", status.raw());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed_properties.find("Position") != changed_properties.end()) {
|
||||
auto position_var = changed_properties.at("Position");
|
||||
if (position_var.is_of_type(Glib::VariantType("x"))) {
|
||||
auto position = Glib::VariantBase::cast_dynamic<Glib::Variant<gint64>>(position_var).get();
|
||||
spdlog::info("Position changed to: {}", position);
|
||||
playbackPositionChangedSignal.emit(static_cast<int64_t>(position));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MprisController::previous_song() {
|
||||
@@ -149,7 +186,6 @@ void MprisController::previous_song() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MprisController::toggle_play() {
|
||||
if (m_proxy) {
|
||||
m_proxy->call("PlayPause");
|
||||
@@ -173,6 +209,6 @@ void MprisController::emit_seeked(int64_t position_us) {
|
||||
|
||||
m_proxy->call("Seek", params);
|
||||
} catch (const Glib::Error &ex) {
|
||||
std::cerr << "Error seeking: " << ex.what() << std::endl;
|
||||
spdlog::error("Error seeking: {}", ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user