From 78823fda6d0229b126477636a2b7616815974811 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Mon, 27 Jun 2016 22:22:33 +0200 Subject: [PATCH] fix(mpd): Broadcast connection state changes once --- include/modules/mpd.hpp | 5 ++++- src/modules/mpd.cpp | 11 +++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/modules/mpd.hpp b/include/modules/mpd.hpp index 778f6ca0..2107bdb9 100644 --- a/include/modules/mpd.hpp +++ b/include/modules/mpd.hpp @@ -63,9 +63,12 @@ namespace modules std::chrono::system_clock::time_point synced_at; float sync_interval = 1.0f; - bool clickable_progress = false; std::string progress_fill, progress_empty, progress_indicator; + // This flag is used to let thru a broadcast once every time + // the connection state changes + concurrency::Atomic connection_state_broadcasted { true }; + public: explicit MpdModule(std::string name); ~MpdModule(); diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index 50721aef..df67cc73 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -25,6 +25,7 @@ MpdModule::MpdModule(std::string name_) TAG_ICON_RANDOM, TAG_ICON_REPEAT, TAG_ICON_REPEAT_ONE, TAG_ICON_PREV, TAG_ICON_STOP, TAG_ICON_PLAY, TAG_ICON_PAUSE, TAG_ICON_NEXT, TAG_ICON_SEEKB, TAG_ICON_SEEKF }); + this->formatter->add(FORMAT_OFFLINE, "", { TAG_LABEL_OFFLINE }); if (this->formatter->has(TAG_ICON_PLAY) || this->formatter->has(TAG_TOGGLE)) @@ -107,9 +108,11 @@ void MpdModule::start() bool MpdModule::has_event() { - bool has_event = false; + bool has_event = !this->connection_state_broadcasted(); if (!this->mpd->connected()) { + this->connection_state_broadcasted = false; + try { this->mpd->connect(); } catch (mpd::Exception &e) { @@ -117,11 +120,15 @@ bool MpdModule::has_event() } if (!this->mpd->connected()) { - this->sleep(3s); + this->sleep(2s); return false; } } + if (!this->connection_state_broadcasted) { + this->connection_state_broadcasted = true; + } + if (!this->status) { this->status = this->mpd->get_status(); this->status->update(-1, this->mpd);