From 35ffeb740f35181298e15d3e31622cff0b9bbe4b Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Sun, 25 Apr 2021 16:50:27 +0200 Subject: [PATCH] Stop background effect, when it gets out of scope (#1226) --- include/hyperion/BGEffectHandler.h | 44 ++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/include/hyperion/BGEffectHandler.h b/include/hyperion/BGEffectHandler.h index 9c0dfcbd..ae3362f0 100644 --- a/include/hyperion/BGEffectHandler.h +++ b/include/hyperion/BGEffectHandler.h @@ -4,6 +4,7 @@ #include #include #include +#include /// /// @brief Handle the background Effect settings, reacts on runtime to settings changes @@ -14,11 +15,19 @@ class BGEffectHandler : public QObject public: BGEffectHandler(Hyperion* hyperion) - : QObject(hyperion) - , _hyperion(hyperion) + : QObject(hyperion) + , _hyperion(hyperion) + , _prioMuxer(_hyperion->getMuxerInstance()) + , _isBgEffectConfigured(false) { // listen for config changes - connect(_hyperion, &Hyperion::settingsChanged, this, &BGEffectHandler::handleSettingsUpdate); + connect(_hyperion, &Hyperion::settingsChanged, + [=](settings::type type, const QJsonDocument& config) { this->handleSettingsUpdate(type, config); } + ); + + connect(_prioMuxer, &PriorityMuxer::prioritiesChanged, + [=]() { this->handlePriorityUpdate(); } + ); // initialization handleSettingsUpdate(settings::BGEFFECT, _hyperion->getSetting(settings::BGEFFECT)); @@ -34,14 +43,18 @@ private slots: { if(type == settings::BGEFFECT) { - const QJsonObject& BGEffectConfig = config.object(); + _isBgEffectConfigured = false; + _bgEffectConfig = config; + const QJsonObject& BGEffectConfig = _bgEffectConfig.object(); #define BGCONFIG_ARRAY bgColorConfig.toArray() // clear background priority _hyperion->clear(PriorityMuxer::BG_PRIORITY); // initial background effect/color if (BGEffectConfig["enable"].toBool(true)) { + _isBgEffectConfigured = true; + const QString bgTypeConfig = BGEffectConfig["type"].toString("effect"); const QString bgEffectConfig = BGEffectConfig["effect"].toString("Warm mood blobs"); const QJsonValue bgColorConfig = BGEffectConfig["color"]; @@ -63,12 +76,33 @@ private slots: Info(Logger::getInstance("HYPERION"),"Initial background effect '%s' %s", QSTRING_CSTR(bgEffectConfig), ((result == 0) ? "started" : "failed")); } } - #undef BGCONFIG_ARRAY } } + /// + /// @brief Handle priority updates. + /// In case the background effect is not current priority, stop BG-effect to save resources; otherwise start effect again. + /// + void handlePriorityUpdate() + { + if (_prioMuxer->getCurrentPriority() != PriorityMuxer::BG_PRIORITY && _prioMuxer->hasPriority(PriorityMuxer::BG_PRIORITY)) + { + Debug(Logger::getInstance("HYPERION"),"Stop background (color-) effect as it moved out of scope"); + _hyperion->clear(PriorityMuxer::BG_PRIORITY); + } + else if (_prioMuxer->getCurrentPriority() == PriorityMuxer::LOWEST_PRIORITY && _isBgEffectConfigured) + { + emit handleSettingsUpdate (settings::BGEFFECT, _bgEffectConfig); + } + } + private: /// Hyperion instance pointer Hyperion* _hyperion; + /// priority muxer instance + PriorityMuxer* _prioMuxer; + + QJsonDocument _bgEffectConfig; + bool _isBgEffectConfigured; };