From e9a07b15dacfd86095beef7f60885491f350c725 Mon Sep 17 00:00:00 2001 From: IXtreme Date: Fri, 20 Dec 2024 01:05:41 -0500 Subject: [PATCH] Fix a few bugs related to image loading (wrap it all in a try catch lol) --- src/main.cpp | 11 ++++++++--- src/mpris_connection.cpp | 34 +++++++++++++++++++++------------- src/mpris_connection.hpp | 1 + 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 00dc7b3..533f505 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -191,14 +191,22 @@ int main(int argc, char *argv[]) { // std::cout << *it << std::endl; // } + + double lasttime = 0.0; // Main game loop while (!WindowShouldClose() && run) // Detect window close button or ESC key, or a quit from the menu { + if (GetTime() > lasttime+3 ) { + lasttime = GetTime(); + current_player.Refresh(); + } + BeginDrawing(); ClearBackground(DARKGRAY); + // start ImGui content rlImGuiBegin(); @@ -246,9 +254,6 @@ int main(int argc, char *argv[]) { if (ImGui::Begin("Player Control")) { ImGui::Text("%s", current_player.GetIdentity().c_str()); - ImGui::Text( - "%s", - current_player.GetCurrentMetadata()["mpris:artUrl"].get().c_str()); if (ImGui::Button("Prev")) { current_player.Prev(); } diff --git a/src/mpris_connection.cpp b/src/mpris_connection.cpp index 2340bef..fecaf2a 100644 --- a/src/mpris_connection.cpp +++ b/src/mpris_connection.cpp @@ -14,16 +14,17 @@ MprisPlayer::MprisPlayer(std::string servicename) { bus_connection = sdbus::createSessionBusConnection(); sdbus::ServiceName dest{servicename}; sdbus::ObjectPath objec{"/org/mpris/MediaPlayer2"}; - + image_path_cache = "NULL"; PlayerProxy = sdbus::createProxy(dest, objec); mp2 = "org.mpris.MediaPlayer2.Player"; - playpause = "PlayPause"; Refresh(); } void MprisPlayer::PausePlay() { - auto method = PlayerProxy->createMethodCall(mp2, playpause); - auto reply = PlayerProxy->callMethod(method); + auto method = PlayerProxy->createMethodCall(mp2, sdbus::MethodName("PlayPause")); + try { + auto reply = PlayerProxy->callMethod(method); + } catch (const std::exception& e) {} } void MprisPlayer::Next() { @@ -35,7 +36,9 @@ void MprisPlayer::Next() { void MprisPlayer::Prev() { auto method = PlayerProxy->createMethodCall(mp2, sdbus::MethodName("Previous")); - auto reply = PlayerProxy->callMethod(method); + try { + auto reply = PlayerProxy->callMethod(method); + } catch (const std::exception &e) {} } std::string MprisPlayer::GetIdentity() { @@ -46,18 +49,23 @@ std::unordered_map MprisPlayer::GetCurrentMetadata( return current_metadata; } void MprisPlayer::Refresh() { - identity = PlayerProxy->getProperty("Identity") - .onInterface("org.mpris.MediaPlayer2") - .get(); - current_metadata = PlayerProxy->getProperty("Metadata") - .onInterface("org.mpris.MediaPlayer2.Player") - .get>(); + try { + identity = PlayerProxy->getProperty("Identity") + .onInterface("org.mpris.MediaPlayer2") + .get(); + current_metadata = PlayerProxy->getProperty("Metadata") + .onInterface("org.mpris.MediaPlayer2.Player") + .get>(); + if (current_metadata["mpris:artUrl"].get() != image_path_cache) { + image_path_cache = current_metadata["mpris:artUrl"].get(); + UpdateTexture(); + } + } catch (const std::exception& e) {} return; } void MprisPlayer::UpdateTexture() { - auto metadata = GetCurrentMetadata(); - std::string filename = metadata["mpris:artUrl"].get(); + std::string filename = image_path_cache; if (filename.length() > 0) { if (filename.c_str()[0] == 'f') { std::cout << "parsing as file" << std::endl; diff --git a/src/mpris_connection.hpp b/src/mpris_connection.hpp index c493d9f..eacd5d4 100644 --- a/src/mpris_connection.hpp +++ b/src/mpris_connection.hpp @@ -29,6 +29,7 @@ class MprisPlayer { sdbus::InterfaceName mp2; sdbus::MethodName playpause; RayTexture tex; + std::string image_path_cache; explicit MprisPlayer(std::string servicename); void PausePlay(); void Refresh();