From 645a3142a19747c252d24cc71414d40a8eb5f7dc Mon Sep 17 00:00:00 2001
From: patrick96
Date: Wed, 27 Dec 2017 16:34:24 +0100
Subject: [PATCH] fix(mpd): Always update mpd data
Only updating when an mpd event occurred would cause issues when mpd was
playing and the machine was put to sleep because the elapsed time was
calculated by taking the time difference of the last update and now
which would give you wrong numbers, if the machine was in standby in
between.
Since the update function on the module is only called once a second (or
when an event happens), we can just update the data every time without a
huge performance hit.
Fixes #915
---
include/adapters/mpd.hpp | 1 -
src/adapters/mpd.cpp | 12 ++++--------
src/modules/mpd.cpp | 10 ++++++----
3 files changed, 10 insertions(+), 13 deletions(-)
diff --git a/include/adapters/mpd.hpp b/include/adapters/mpd.hpp
index 5e7dbfa8..03cc2862 100644
--- a/include/adapters/mpd.hpp
+++ b/include/adapters/mpd.hpp
@@ -140,7 +140,6 @@ namespace mpd {
void fetch_data(mpdconnection* conn);
void update(int event, mpdconnection* connection);
- void update_timer();
bool random() const;
bool repeat() const;
diff --git a/src/adapters/mpd.cpp b/src/adapters/mpd.cpp
index c4e2f731..459551a0 100644
--- a/src/adapters/mpd.cpp
+++ b/src/adapters/mpd.cpp
@@ -370,6 +370,10 @@ namespace mpd {
}
void mpdstatus::update(int event, mpdconnection* connection) {
+ /*
+ * Only update if either the player state (play, stop, pause, seek, ...), the options (random, repeat, ...),
+ * or the playlist has been changed
+ */
if (connection == nullptr || !static_cast(event & (MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST))) {
return;
}
@@ -395,14 +399,6 @@ namespace mpd {
}
}
- void mpdstatus::update_timer() {
- auto diff = chrono::system_clock::now() - m_updated_at;
- auto dur = chrono::duration_cast(diff);
- m_elapsed_time_ms += dur.count();
- m_elapsed_time = m_elapsed_time_ms / 1000 + 0.5f;
- m_updated_at = chrono::system_clock::now();
- }
-
bool mpdstatus::random() const {
return m_random;
}
diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp
index 155663e7..3d67bc3b 100644
--- a/src/modules/mpd.cpp
+++ b/src/modules/mpd.cpp
@@ -162,13 +162,10 @@ namespace modules {
int idle_flags = 0;
if ((idle_flags = m_mpd->noidle()) != 0) {
+ // Update status on every event
m_status->update(idle_flags, m_mpd.get());
return true;
}
-
- if (m_status->match_state(mpdstate::PLAYING)) {
- m_status->update_timer();
- }
} catch (const mpd_exception& err) {
m_log.err("%s: %s", name(), err.what());
m_mpd.reset();
@@ -203,6 +200,11 @@ namespace modules {
}
}
+ if (m_status->match_state(mpdstate::PLAYING)) {
+ // Always update the status while playing
+ m_status->update(-1, m_mpd.get());
+ }
+
string artist;
string album;
string title;